Fix applying combining characters to double-width characters.
[u/mdw/putty] / terminal.c
CommitLineData
374330e2 1#include <stdio.h>
2#include <stdlib.h>
49bad831 3#include <ctype.h>
374330e2 4
e1c8e0ed 5#include <time.h>
c83de303 6#include <assert.h>
374330e2 7#include "putty.h"
887035a5 8#include "terminal.h"
9
10#define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
11#define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
12#define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
13#define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
14
15/* Product-order comparisons for rectangular block selection. */
16#define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
17#define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
18
19#define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
20#define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
374330e2 21
4eeb7d09 22#define VT52_PLUS
23
32874aea 24#define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
25#define CL_VT100 0x0002 /* VT100 */
26#define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
27#define CL_VT102 0x0008 /* VT102 */
28#define CL_VT220 0x0010 /* VT220 */
29#define CL_VT320 0x0020 /* VT320 */
30#define CL_VT420 0x0040 /* VT420 */
31#define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
32#define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
33#define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
34#define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
35#define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
e14a5a13 36
c9def1b8 37#define TM_VT100 (CL_ANSIMIN|CL_VT100)
38#define TM_VT100AVO (TM_VT100|CL_VT100AVO)
39#define TM_VT102 (TM_VT100AVO|CL_VT102)
40#define TM_VT220 (TM_VT102|CL_VT220)
41#define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
42#define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
43
44#define TM_PUTTY (0xFFFF)
e14a5a13 45
46#define compatibility(x) \
887035a5 47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
e14a5a13 49 break; \
50 }
c9def1b8 51#define compatibility2(x,y) \
887035a5 52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
c9def1b8 54 break; \
55 }
e14a5a13 56
887035a5 57#define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
374330e2 58
1b20b972 59const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
60
4eeb7d09 61#define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
887035a5 62const wchar_t sel_nl[] = SEL_NL;
374330e2 63
64/*
3c7366f8 65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
69 */
70#define UCSGET(a, x) \
36566009 71 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
72
73/*
74 * Detect the various aliases of U+0020 SPACE.
75 */
76#define IS_SPACE_CHR(chr) \
77 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
78
79/*
80 * Spot magic CSETs.
81 */
82#define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
3c7366f8 83
84/*
374330e2 85 * Internal prototypes.
86 */
36566009 87static void resizeline(Terminal *, termline *, int);
88static termline *lineptr(Terminal *, int, int, int);
89static void unlineptr(termline *);
887035a5 90static void do_paint(Terminal *, Context, int);
91static void erase_lots(Terminal *, int, int, int);
92static void swap_screen(Terminal *, int, int, int);
93static void update_sbar(Terminal *);
94static void deselect(Terminal *);
95static void term_print_finish(Terminal *);
37d2a505 96#ifdef OPTIMISE_SCROLL
97static void scroll_display(Terminal *, int, int, int);
98#endif /* OPTIMISE_SCROLL */
374330e2 99
36566009 100static termline *newline(Terminal *term, int cols, int bce)
101{
102 termline *line;
103 int j;
104
105 line = snew(termline);
106 line->chars = snewn(cols, termchar);
107 for (j = 0; j < cols; j++)
108 line->chars[j] = (bce ? term->erase_char : term->basic_erase_char);
c6958dfe 109 line->cols = line->size = cols;
36566009 110 line->lattr = LATTR_NORM;
111 line->temporary = FALSE;
c6958dfe 112 line->cc_free = 0;
36566009 113
114 return line;
115}
116
117static void freeline(termline *line)
118{
05bea829 119 if (line) {
120 sfree(line->chars);
121 sfree(line);
122 }
36566009 123}
124
125static void unlineptr(termline *line)
126{
127 if (line->temporary)
128 freeline(line);
129}
130
131/*
c6958dfe 132 * Diagnostic function: verify that a termline has a correct
133 * combining character structure.
4fe051a2 134 *
135 * XXX-REMOVE-BEFORE-RELEASE: This is a performance-intensive
136 * check. Although it's currently really useful for getting all the
137 * bugs out of the new cc stuff, it will want to be absent when we
138 * make a proper release.
c6958dfe 139 */
140static void cc_check(termline *line)
141{
142 unsigned char *flags;
143 int i, j;
144
145 assert(line->size >= line->cols);
146
147 flags = snewn(line->size, unsigned char);
148
149 for (i = 0; i < line->size; i++)
150 flags[i] = (i < line->cols);
151
152 for (i = 0; i < line->cols; i++) {
153 j = i;
154 while (line->chars[j].cc_next) {
155 j += line->chars[j].cc_next;
156 assert(j >= line->cols && j < line->size);
157 assert(!flags[j]);
158 flags[j] = TRUE;
159 }
160 }
161
162 j = line->cc_free;
163 if (j) {
164 while (1) {
165 assert(j >= line->cols && j < line->size);
166 assert(!flags[j]);
167 flags[j] = TRUE;
168 if (line->chars[j].cc_next)
169 j += line->chars[j].cc_next;
170 else
171 break;
172 }
173 }
174
175 j = 0;
176 for (i = 0; i < line->size; i++)
177 j += (flags[i] != 0);
178
179 assert(j == line->size);
180}
181
182/*
183 * Add a combining character to a character cell.
184 */
185static void add_cc(termline *line, int col, unsigned long chr)
186{
187 int newcc;
188
189 assert(col >= 0 && col < line->cols);
190
191 /*
192 * Start by extending the cols array if the free list is empty.
193 */
194 if (!line->cc_free) {
195 int n = line->size;
196 line->size += 16 + (line->size - line->cols) / 2;
197 line->chars = sresize(line->chars, line->size, termchar);
198 line->cc_free = n;
199 while (n < line->size) {
200 if (n+1 < line->size)
201 line->chars[n].cc_next = 1;
202 else
203 line->chars[n].cc_next = 0;
204 n++;
205 }
206 }
207
208 /*
209 * Now walk the cc list of the cell in question.
210 */
211 while (line->chars[col].cc_next)
212 col += line->chars[col].cc_next;
213
214 /*
215 * `col' now points at the last cc currently in this cell; so
216 * we simply add another one.
217 */
218 newcc = line->cc_free;
219 if (line->chars[newcc].cc_next)
220 line->cc_free = newcc + line->chars[newcc].cc_next;
221 else
222 line->cc_free = 0;
223 line->chars[newcc].cc_next = 0;
224 line->chars[newcc].chr = chr;
225 line->chars[col].cc_next = newcc - col;
226
4fe051a2 227 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 228}
229
230/*
231 * Clear the combining character list in a character cell.
232 */
233static void clear_cc(termline *line, int col)
234{
235 int oldfree, origcol = col;
236
237 assert(col >= 0 && col < line->cols);
238
239 if (!line->chars[col].cc_next)
240 return; /* nothing needs doing */
241
242 oldfree = line->cc_free;
243 line->cc_free = col + line->chars[col].cc_next;
244 while (line->chars[col].cc_next)
245 col += line->chars[col].cc_next;
246 if (oldfree)
247 line->chars[col].cc_next = oldfree - col;
248 else
249 line->chars[col].cc_next = 0;
250
251 line->chars[origcol].cc_next = 0;
252
4fe051a2 253 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 254}
255
256/*
257 * Compare two character cells for equality. Special case required
258 * in do_paint() where we override what we expect the chr and attr
259 * fields to be.
260 */
261static int termchars_equal_override(termchar *a, termchar *b,
262 unsigned long bchr, unsigned long battr)
263{
264 /* FULL-TERMCHAR */
265 if (a->chr != bchr)
266 return FALSE;
267 if (a->attr != battr)
268 return FALSE;
269 while (a->cc_next || b->cc_next) {
270 if (!a->cc_next || !b->cc_next)
271 return FALSE; /* one cc-list ends, other does not */
272 a += a->cc_next;
273 b += b->cc_next;
274 if (a->chr != b->chr)
275 return FALSE;
276 }
277 return TRUE;
278}
279
280static int termchars_equal(termchar *a, termchar *b)
281{
282 return termchars_equal_override(a, b, b->chr, b->attr);
283}
284
285/*
286 * Copy a character cell. (Requires a pointer to the destination
287 * termline, so as to access its free list.)
288 */
289static void copy_termchar(termline *destline, int x, termchar *src)
290{
291 clear_cc(destline, x);
292
293 destline->chars[x] = *src; /* copy everything except cc-list */
294 destline->chars[x].cc_next = 0; /* and make sure this is zero */
295
296 while (src->cc_next) {
297 src += src->cc_next;
298 add_cc(destline, x, src->chr);
299 }
4fe051a2 300
301 cc_check(destline); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 302}
303
304/*
305 * Move a character cell within its termline.
306 */
307static void move_termchar(termline *line, termchar *dest, termchar *src)
308{
309 /* First clear the cc list from the original char, just in case. */
310 clear_cc(line, dest - line->chars);
311
312 /* Move the character cell and adjust its cc_next. */
313 *dest = *src; /* copy everything except cc-list */
314 if (src->cc_next)
315 dest->cc_next = src->cc_next - (dest-src);
316
317 /* Ensure the original cell doesn't have a cc list. */
318 src->cc_next = 0;
4fe051a2 319
320 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 321}
322
323/*
36566009 324 * Compress and decompress a termline into an RLE-based format for
325 * storing in scrollback. (Since scrollback almost never needs to
326 * be modified and exists in huge quantities, this is a sensible
327 * tradeoff, particularly since it allows us to continue adding
328 * features to the main termchar structure without proportionally
329 * bloating the terminal emulator's memory footprint unless those
330 * features are in constant use.)
331 */
332struct buf {
333 unsigned char *data;
334 int len, size;
335};
336static void add(struct buf *b, unsigned char c)
337{
338 if (b->len >= b->size) {
339 b->size = (b->len * 3 / 2) + 512;
340 b->data = sresize(b->data, b->size, unsigned char);
341 }
342 b->data[b->len++] = c;
343}
344static int get(struct buf *b)
345{
346 return b->data[b->len++];
347}
348static void makerle(struct buf *b, termline *ldata,
349 void (*makeliteral)(struct buf *b, termchar *c,
350 unsigned long *state))
351{
352 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
353 termchar *c = ldata->chars;
354 unsigned long state = 0, oldstate;
355
356 n = ldata->cols;
357
358 hdrpos = b->len;
359 hdrsize = 0;
360 add(b, 0);
361 prevlen = prevpos = 0;
362 prev2 = FALSE;
363
364 while (n-- > 0) {
365 thispos = b->len;
366 makeliteral(b, c++, &state);
367 thislen = b->len - thispos;
368 if (thislen == prevlen &&
369 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
370 /*
371 * This literal precisely matches the previous one.
372 * Turn it into a run if it's worthwhile.
373 *
374 * With one-byte literals, it costs us two bytes to
375 * encode a run, plus another byte to write the header
376 * to resume normal output; so a three-element run is
377 * neutral, and anything beyond that is unconditionally
378 * worthwhile. With two-byte literals or more, even a
379 * 2-run is a win.
380 */
381 if (thislen > 1 || prev2) {
382 int runpos, runlen;
383
384 /*
385 * It's worth encoding a run. Start at prevpos,
386 * unless hdrsize==0 in which case we can back up
387 * another one and start by overwriting hdrpos.
388 */
389
390 hdrsize--; /* remove the literal at prevpos */
391 if (prev2) {
392 assert(hdrsize > 0);
393 hdrsize--;
394 prevpos -= prevlen;/* and possibly another one */
395 }
396
397 if (hdrsize == 0) {
398 assert(prevpos == hdrpos + 1);
399 runpos = hdrpos;
400 b->len = prevpos+prevlen;
401 } else {
402 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
403 runpos = prevpos;
404 b->len = prevpos+prevlen+1;
405 /*
406 * Terminate the previous run of ordinary
407 * literals.
408 */
409 assert(hdrsize >= 1 && hdrsize <= 128);
410 b->data[hdrpos] = hdrsize - 1;
411 }
412
413 runlen = prev2 ? 3 : 2;
414
415 while (n > 0 && runlen < 129) {
416 int tmppos, tmplen;
417 tmppos = b->len;
418 oldstate = state;
419 makeliteral(b, c, &state);
420 tmplen = b->len - tmppos;
421 b->len = tmppos;
422 if (tmplen != thislen ||
423 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
424 state = oldstate;
425 break; /* run over */
426 }
427 n--, c++, runlen++;
428 }
429
430 assert(runlen >= 2 && runlen <= 129);
431 b->data[runpos] = runlen + 0x80 - 2;
432
433 hdrpos = b->len;
434 hdrsize = 0;
435 add(b, 0);
436
437 continue;
438 } else {
439 /*
440 * Just flag that the previous two literals were
441 * identical, in case we find a third identical one
442 * we want to turn into a run.
443 */
444 prev2 = TRUE;
445 prevlen = thislen;
446 prevpos = thispos;
447 }
448 } else {
449 prev2 = FALSE;
450 prevlen = thislen;
451 prevpos = thispos;
452 }
453
454 /*
455 * This character isn't (yet) part of a run. Add it to
456 * hdrsize.
457 */
458 hdrsize++;
459 if (hdrsize == 128) {
460 b->data[hdrpos] = hdrsize - 1;
461 hdrpos = b->len;
462 hdrsize = 0;
463 add(b, 0);
464 prevlen = prevpos = 0;
465 prev2 = FALSE;
466 }
467 }
468
469 /*
470 * Clean up.
471 */
472 if (hdrsize > 0) {
473 assert(hdrsize <= 128);
474 b->data[hdrpos] = hdrsize - 1;
475 } else {
476 b->len = hdrpos;
477 }
478}
479static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
480{
481 /*
482 * My encoding for characters is UTF-8-like, in that it stores
483 * 7-bit ASCII in one byte and uses high-bit-set bytes as
484 * introducers to indicate a longer sequence. However, it's
485 * unlike UTF-8 in that it doesn't need to be able to
486 * resynchronise, and therefore I don't want to waste two bits
487 * per byte on having recognisable continuation characters.
488 * Also I don't want to rule out the possibility that I may one
489 * day use values 0x80000000-0xFFFFFFFF for interesting
490 * purposes, so unlike UTF-8 I need a full 32-bit range.
491 * Accordingly, here is my encoding:
492 *
493 * 00000000-0000007F: 0xxxxxxx (but see below)
494 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
495 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
496 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
497 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
498 *
499 * (`Z' is like `x' but is always going to be zero since the
500 * values I'm encoding don't go above 2^32. In principle the
501 * five-byte form of the encoding could extend to 2^35, and
502 * there could be six-, seven-, eight- and nine-byte forms as
503 * well to allow up to 64-bit values to be encoded. But that's
504 * completely unnecessary for these purposes!)
505 *
506 * The encoding as written above would be very simple, except
507 * that 7-bit ASCII can occur in several different ways in the
508 * terminal data; sometimes it crops up in the D800 page
509 * (CSET_ASCII) but at other times it's in the 0000 page (real
510 * Unicode). Therefore, this encoding is actually _stateful_:
511 * the one-byte encoding of 00-7F actually indicates `reuse the
512 * upper three bytes of the last character', and to encode an
513 * absolute value of 00-7F you need to use the two-byte form
514 * instead.
515 */
516 if ((c->chr & ~0x7F) == *state) {
517 add(b, (unsigned char)(c->chr & 0x7F));
518 } else if (c->chr < 0x4000) {
519 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
520 add(b, (unsigned char)(c->chr & 0xFF));
521 } else if (c->chr < 0x200000) {
522 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
523 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
524 add(b, (unsigned char)(c->chr & 0xFF));
525 } else if (c->chr < 0x10000000) {
526 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
527 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
528 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
529 add(b, (unsigned char)(c->chr & 0xFF));
530 } else {
531 add(b, 0xF0);
532 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
533 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
534 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
535 add(b, (unsigned char)(c->chr & 0xFF));
536 }
537 *state = c->chr & ~0xFF;
538}
539static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
540{
541 /*
542 * My encoding for attributes is 16-bit-granular and assumes
543 * that the top bit of the word is never required. I either
544 * store a two-byte value with the top bit clear (indicating
545 * just that value), or a four-byte value with the top bit set
546 * (indicating the same value with its top bit clear).
547 */
548 if (c->attr < 0x8000) {
549 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
550 add(b, (unsigned char)(c->attr & 0xFF));
551 } else {
552 add(b, (unsigned char)(((c->attr >> 24) & 0x7F) | 0x80));
553 add(b, (unsigned char)((c->attr >> 16) & 0xFF));
554 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
555 add(b, (unsigned char)(c->attr & 0xFF));
556 }
557}
c6958dfe 558static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)
559{
560 /*
561 * For combining characters, I just encode a bunch of ordinary
562 * chars using makeliteral_chr, and terminate with a \0
563 * character (which I know won't come up as a combining char
564 * itself).
565 *
566 * I don't use the stateful encoding in makeliteral_chr.
567 */
568 unsigned long zstate;
569 termchar z;
570
571 while (c->cc_next) {
572 c += c->cc_next;
573
574 assert(c->chr != 0);
575
576 zstate = 0;
577 makeliteral_chr(b, c, &zstate);
578 }
579
580 z.chr = 0;
581 zstate = 0;
582 makeliteral_chr(b, &z, &zstate);
583}
36566009 584
585static termline *decompressline(unsigned char *data, int *bytes_used);
586
587static unsigned char *compressline(termline *ldata)
588{
589 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
590
591 /*
592 * First, store the column count, 7 bits at a time, least
593 * significant `digit' first, with the high bit set on all but
594 * the last.
595 */
596 {
597 int n = ldata->cols;
598 while (n >= 128) {
599 add(b, (unsigned char)((n & 0x7F) | 0x80));
600 n >>= 7;
601 }
602 add(b, (unsigned char)(n));
603 }
604
605 /*
606 * Next store the lattrs; same principle.
607 */
608 {
609 int n = ldata->lattr;
610 while (n >= 128) {
611 add(b, (unsigned char)((n & 0x7F) | 0x80));
612 n >>= 7;
613 }
614 add(b, (unsigned char)(n));
615 }
616
617 /*
618 * Now we store a sequence of separate run-length encoded
619 * fragments, each containing exactly as many symbols as there
620 * are columns in the ldata.
621 *
622 * All of these have a common basic format:
623 *
624 * - a byte 00-7F indicates that X+1 literals follow it
625 * - a byte 80-FF indicates that a single literal follows it
626 * and expects to be repeated (X-0x80)+2 times.
627 *
628 * The format of the `literals' varies between the fragments.
629 */
630 makerle(b, ldata, makeliteral_chr);
631 makerle(b, ldata, makeliteral_attr);
c6958dfe 632 makerle(b, ldata, makeliteral_cc);
36566009 633
634 /*
635 * Diagnostics: ensure that the compressed data really does
636 * decompress to the right thing.
637 */
638#ifndef CHECK_SB_COMPRESSION
639 {
640 int dused;
641 termline *dcl;
c6958dfe 642 int i;
36566009 643
644#ifdef DIAGNOSTIC_SB_COMPRESSION
36566009 645 for (i = 0; i < b->len; i++) {
646 printf(" %02x ", b->data[i]);
647 }
648 printf("\n");
649#endif
650
651 dcl = decompressline(b->data, &dused);
652 assert(b->len == dused);
653 assert(ldata->cols == dcl->cols);
654 assert(ldata->lattr == dcl->lattr);
c6958dfe 655 for (i = 0; i < ldata->cols; i++)
656 assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
36566009 657
658#ifdef DIAGNOSTIC_SB_COMPRESSION
659 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
660 ldata->cols, 4 * ldata->cols, dused,
661 (double)dused / (4 * ldata->cols));
662#endif
663
664 freeline(dcl);
665 }
666#endif
667
668 /*
669 * Trim the allocated memory so we don't waste any, and return.
670 */
671 return sresize(b->data, b->len, unsigned char);
672}
673
674static void readrle(struct buf *b, termline *ldata,
675 void (*readliteral)(struct buf *b, termchar *c,
c6958dfe 676 termline *ldata, unsigned long *state))
36566009 677{
678 int n = 0;
679 unsigned long state = 0;
680
681 while (n < ldata->cols) {
682 int hdr = get(b);
683
684 if (hdr >= 0x80) {
685 /* A run. */
686
687 int pos = b->len, count = hdr + 2 - 0x80;
688 while (count--) {
689 assert(n < ldata->cols);
690 b->len = pos;
c6958dfe 691 readliteral(b, ldata->chars + n, ldata, &state);
36566009 692 n++;
693 }
694 } else {
695 /* Just a sequence of consecutive literals. */
696
697 int count = hdr + 1;
698 while (count--) {
699 assert(n < ldata->cols);
c6958dfe 700 readliteral(b, ldata->chars + n, ldata, &state);
36566009 701 n++;
702 }
703 }
704 }
705
706 assert(n == ldata->cols);
707}
c6958dfe 708static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,
709 unsigned long *state)
36566009 710{
711 int byte;
712
713 /*
714 * 00000000-0000007F: 0xxxxxxx
715 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
716 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
717 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
718 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
719 */
720
721 byte = get(b);
722 if (byte < 0x80) {
723 c->chr = byte | *state;
724 } else if (byte < 0xC0) {
725 c->chr = (byte &~ 0xC0) << 8;
726 c->chr |= get(b);
727 } else if (byte < 0xE0) {
728 c->chr = (byte &~ 0xE0) << 16;
729 c->chr |= get(b) << 8;
730 c->chr |= get(b);
731 } else if (byte < 0xF0) {
732 c->chr = (byte &~ 0xF0) << 24;
733 c->chr |= get(b) << 16;
734 c->chr |= get(b) << 8;
735 c->chr |= get(b);
736 } else {
737 assert(byte == 0xF0);
738 c->chr = get(b) << 24;
739 c->chr |= get(b) << 16;
740 c->chr |= get(b) << 8;
741 c->chr |= get(b);
742 }
743 *state = c->chr & ~0xFF;
744}
c6958dfe 745static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,
746 unsigned long *state)
36566009 747{
748 int val;
749
750 val = get(b) << 8;
751 val |= get(b);
752
753 if (val >= 0x8000) {
754 val <<= 16;
755 val |= get(b) << 8;
756 val |= get(b);
757 }
758
759 c->attr = val;
760}
c6958dfe 761static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,
762 unsigned long *state)
763{
764 termchar n;
765 unsigned long zstate;
766 int x = c - ldata->chars;
767
768 c->cc_next = 0;
769
770 while (1) {
771 zstate = 0;
772 readliteral_chr(b, &n, ldata, &zstate);
773 if (!n.chr)
774 break;
775 add_cc(ldata, x, n.chr);
776 }
777}
36566009 778
779static termline *decompressline(unsigned char *data, int *bytes_used)
780{
781 int ncols, byte, shift;
782 struct buf buffer, *b = &buffer;
783 termline *ldata;
784
785 b->data = data;
786 b->len = 0;
787
788 /*
789 * First read in the column count.
790 */
791 ncols = shift = 0;
792 do {
793 byte = get(b);
794 ncols |= (byte & 0x7F) << shift;
795 shift += 7;
796 } while (byte & 0x80);
797
798 /*
799 * Now create the output termline.
800 */
801 ldata = snew(termline);
802 ldata->chars = snewn(ncols, termchar);
c6958dfe 803 ldata->cols = ldata->size = ncols;
36566009 804 ldata->temporary = TRUE;
c6958dfe 805 ldata->cc_free = 0;
806
807 /*
808 * We must set all the cc pointers in ldata->chars to 0 right
809 * now, so that cc diagnostics that verify the integrity of the
810 * whole line will make sense while we're in the middle of
811 * building it up.
812 */
813 {
814 int i;
815 for (i = 0; i < ldata->cols; i++)
816 ldata->chars[i].cc_next = 0;
817 }
36566009 818
819 /*
820 * Now read in the lattr.
821 */
822 ldata->lattr = shift = 0;
823 do {
824 byte = get(b);
825 ldata->lattr |= (byte & 0x7F) << shift;
826 shift += 7;
827 } while (byte & 0x80);
828
829 /*
830 * Now we read in each of the RLE streams in turn.
831 */
832 readrle(b, ldata, readliteral_chr);
833 readrle(b, ldata, readliteral_attr);
c6958dfe 834 readrle(b, ldata, readliteral_cc);
36566009 835
836 /* Return the number of bytes read, for diagnostic purposes. */
837 if (bytes_used)
838 *bytes_used = b->len;
839
840 return ldata;
841}
842
374330e2 843/*
2371caac 844 * Resize a line to make it `cols' columns wide.
845 */
36566009 846static void resizeline(Terminal *term, termline *line, int cols)
2371caac 847{
848 int i, oldlen;
2371caac 849
36566009 850 if (line->cols != cols) {
2371caac 851 /*
852 * This line is the wrong length, which probably means it
853 * hasn't been accessed since a resize. Resize it now.
c6958dfe 854 *
855 * First, go through all the characters that will be thrown
856 * out in the resize (if we're shrinking the line) and
857 * return their cc lists to the cc free list.
858 */
859 for (i = cols; i < line->cols; i++)
860 clear_cc(line, i);
861
862 /*
863 * Now do the actual resize, leaving the _same_ amount of
864 * cc space as there was to begin with.
2371caac 865 */
36566009 866 oldlen = line->cols;
c6958dfe 867 line->size += cols - oldlen;
868 line->chars = sresize(line->chars, line->size, TTYPE);
36566009 869 line->cols = cols;
c6958dfe 870
871 /*
872 * Bodily move the entire cc section from where it started
873 * to where it now needs to be.
874 */
875 memmove(line->chars + line->cols, line->chars + oldlen,
876 (line->size - line->cols) * TSIZE);
877
878 /*
879 * Go through what's left of the original line, and adjust
880 * the first cc_next pointer in each list. (All the
881 * subsequent ones are still valid because they are
882 * relative offsets within the cc block.) Also do the same
883 * to the head of the cc_free list.
884 */
885 for (i = 0; i < oldlen && i < line->cols; i++)
886 if (line->chars[i].cc_next)
887 line->chars[i].cc_next += cols - oldlen;
888 if (line->cc_free)
889 line->cc_free += cols - oldlen;
890
891 /*
892 * And finally fill in the new space with erase chars. (We
893 * don't have to worry about cc lists here, because we
894 * _know_ the erase char doesn't have one.)
895 */
2371caac 896 for (i = oldlen; i < cols; i++)
36566009 897 line->chars[i] = term->basic_erase_char;
c6958dfe 898
4fe051a2 899 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
2371caac 900 }
2371caac 901}
902
903/*
876e5d5e 904 * Get the number of lines in the scrollback.
905 */
906static int sblines(Terminal *term)
907{
908 int sblines = count234(term->scrollback);
909 if (term->cfg.erase_to_scrollback &&
910 term->alt_which && term->alt_screen) {
911 sblines += term->alt_sblines;
912 }
913 return sblines;
914}
915
916/*
c83de303 917 * Retrieve a line of the screen or of the scrollback, according to
918 * whether the y coordinate is non-negative or negative
919 * (respectively).
920 */
36566009 921static termline *lineptr(Terminal *term, int y, int lineno, int screen)
32874aea 922{
36566009 923 termline *line;
c83de303 924 tree234 *whichtree;
2371caac 925 int treeindex;
c83de303 926
927 if (y >= 0) {
887035a5 928 whichtree = term->screen;
c83de303 929 treeindex = y;
930 } else {
876e5d5e 931 int altlines = 0;
36566009 932
933 assert(!screen);
934
876e5d5e 935 if (term->cfg.erase_to_scrollback &&
936 term->alt_which && term->alt_screen) {
937 altlines = term->alt_sblines;
938 }
939 if (y < -altlines) {
940 whichtree = term->scrollback;
941 treeindex = y + altlines + count234(term->scrollback);
942 } else {
943 whichtree = term->alt_screen;
944 treeindex = y + term->alt_sblines;
945 /* treeindex = y + count234(term->alt_screen); */
946 }
c83de303 947 }
36566009 948 if (whichtree == term->scrollback) {
949 unsigned char *cline = index234(whichtree, treeindex);
950 line = decompressline(cline, NULL);
951 } else {
952 line = index234(whichtree, treeindex);
953 }
c83de303 954
955 /* We assume that we don't screw up and retrieve something out of range. */
c83de303 956 assert(line != NULL);
c83de303 957
36566009 958 resizeline(term, line, term->cols);
959 /* FIXME: should we sort the compressed scrollback out here? */
c83de303 960
36566009 961 return line;
c83de303 962}
32874aea 963
36566009 964#define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
965#define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
887035a5 966
c83de303 967/*
374330e2 968 * Set up power-on settings for the terminal.
969 */
887035a5 970static void power_on(Terminal *term)
32874aea 971{
887035a5 972 term->curs.x = term->curs.y = 0;
973 term->alt_x = term->alt_y = 0;
974 term->savecurs.x = term->savecurs.y = 0;
975 term->alt_t = term->marg_t = 0;
976 if (term->rows != -1)
977 term->alt_b = term->marg_b = term->rows - 1;
374330e2 978 else
887035a5 979 term->alt_b = term->marg_b = 0;
980 if (term->cols != -1) {
374330e2 981 int i;
887035a5 982 for (i = 0; i < term->cols; i++)
983 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 984 }
64734920 985 term->alt_om = term->dec_om = term->cfg.dec_om;
f278d6f8 986 term->alt_ins = term->insert = FALSE;
987 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
64734920 988 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
f278d6f8 989 term->alt_cset = term->cset = term->save_cset = 0;
990 term->alt_utf = term->utf = term->save_utf = 0;
991 term->utf_state = 0;
992 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
36566009 993 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
887035a5 994 term->rvideo = 0;
995 term->in_vbell = FALSE;
996 term->cursor_on = 1;
997 term->big_cursor = 0;
69761fd5 998 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
887035a5 999 term->term_editing = term->term_echoing = FALSE;
64734920 1000 term->app_cursor_keys = term->cfg.app_cursor;
1001 term->app_keypad_keys = term->cfg.app_keypad;
1002 term->use_bce = term->cfg.bce;
1003 term->blink_is_real = term->cfg.blinktext;
36566009 1004 term->erase_char = term->basic_erase_char;
887035a5 1005 term->alt_which = 0;
1006 term_print_finish(term);
374330e2 1007 {
1008 int i;
1009 for (i = 0; i < 256; i++)
64734920 1010 term->wordness[i] = term->cfg.wordness[i];
374330e2 1011 }
887035a5 1012 if (term->screen) {
1013 swap_screen(term, 1, FALSE, FALSE);
1014 erase_lots(term, FALSE, TRUE, TRUE);
1015 swap_screen(term, 0, FALSE, FALSE);
1016 erase_lots(term, FALSE, TRUE, TRUE);
374330e2 1017 }
1018}
1019
1020/*
1021 * Force a screen update.
1022 */
887035a5 1023void term_update(Terminal *term)
32874aea 1024{
374330e2 1025 Context ctx;
a8327734 1026 ctx = get_ctx(term->frontend);
374330e2 1027 if (ctx) {
887035a5 1028 int need_sbar_update = term->seen_disp_event;
64734920 1029 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
887035a5 1030 term->disptop = 0; /* return to main screen */
1031 term->seen_disp_event = 0;
4f892125 1032 need_sbar_update = TRUE;
cabfd08c 1033 }
f0fccd51 1034
4f892125 1035 if (need_sbar_update)
887035a5 1036 update_sbar(term);
1037 do_paint(term, ctx, TRUE);
a8327734 1038 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
32874aea 1039 free_ctx(ctx);
374330e2 1040 }
1041}
1042
1043/*
2cb50250 1044 * Called from front end when a keypress occurs, to trigger
1045 * anything magical that needs to happen in that situation.
1046 */
887035a5 1047void term_seen_key_event(Terminal *term)
2cb50250 1048{
1049 /*
1050 * On any keypress, clear the bell overload mechanism
1051 * completely, on the grounds that large numbers of
1052 * beeps coming from deliberate key action are likely
1053 * to be intended (e.g. beeps from filename completion
1054 * blocking repeatedly).
1055 */
887035a5 1056 term->beep_overloaded = FALSE;
1057 while (term->beephead) {
1058 struct beeptime *tmp = term->beephead;
1059 term->beephead = tmp->next;
2cb50250 1060 sfree(tmp);
1061 }
887035a5 1062 term->beeptail = NULL;
1063 term->nbeeps = 0;
2cb50250 1064
1065 /*
1066 * Reset the scrollback on keypress, if we're doing that.
1067 */
64734920 1068 if (term->cfg.scroll_on_key) {
887035a5 1069 term->disptop = 0; /* return to main screen */
1070 term->seen_disp_event = 1;
2cb50250 1071 }
1072}
1073
1074/*
374330e2 1075 * Same as power_on(), but an external function.
1076 */
887035a5 1077void term_pwron(Terminal *term)
32874aea 1078{
887035a5 1079 power_on(term);
b9d7bcad 1080 if (term->ldisc) /* cause ldisc to notice changes */
1081 ldisc_send(term->ldisc, NULL, 0, 0);
887035a5 1082 term->disptop = 0;
1083 deselect(term);
1084 term_update(term);
374330e2 1085}
1086
36566009 1087static void set_erase_char(Terminal *term)
1088{
1089 term->erase_char = term->basic_erase_char;
1090 if (term->use_bce)
1091 term->erase_char.attr = (term->curr_attr &
1092 (ATTR_FGMASK | ATTR_BGMASK));
1093}
1094
374330e2 1095/*
0d2086c5 1096 * When the user reconfigures us, we need to check the forbidden-
b44b307a 1097 * alternate-screen config option, disable raw mouse mode if the
1098 * user has disabled mouse reporting, and abandon a print job if
1099 * the user has disabled printing.
0d2086c5 1100 */
64734920 1101void term_reconfig(Terminal *term, Config *cfg)
0d2086c5 1102{
64734920 1103 /*
1104 * Before adopting the new config, check all those terminal
1105 * settings which control power-on defaults; and if they've
1106 * changed, we will modify the current state as well as the
1107 * default one. The full list is: Auto wrap mode, DEC Origin
1108 * Mode, BCE, blinking text, character classes.
1109 */
1110 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
1111 int i;
1112
1113 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
1114 reset_decom = (term->cfg.dec_om != cfg->dec_om);
1115 reset_bce = (term->cfg.bce != cfg->bce);
1116 reset_blink = (term->cfg.blinktext != cfg->blinktext);
1117 reset_charclass = 0;
1118 for (i = 0; i < lenof(term->cfg.wordness); i++)
1119 if (term->cfg.wordness[i] != cfg->wordness[i])
1120 reset_charclass = 1;
1121
1122 term->cfg = *cfg; /* STRUCTURE COPY */
1123
1124 if (reset_wrap)
1125 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1126 if (reset_decom)
1127 term->alt_om = term->dec_om = term->cfg.dec_om;
7d5c3da4 1128 if (reset_bce) {
64734920 1129 term->use_bce = term->cfg.bce;
36566009 1130 set_erase_char(term);
7d5c3da4 1131 }
64734920 1132 if (reset_blink)
1133 term->blink_is_real = term->cfg.blinktext;
1134 if (reset_charclass)
1135 for (i = 0; i < 256; i++)
1136 term->wordness[i] = term->cfg.wordness[i];
1137
1138 if (term->cfg.no_alt_screen)
887035a5 1139 swap_screen(term, 0, FALSE, FALSE);
64734920 1140 if (term->cfg.no_mouse_rep) {
887035a5 1141 term->xterm_mouse = 0;
a8327734 1142 set_raw_mouse_mode(term->frontend, 0);
c0d36a72 1143 }
64734920 1144 if (term->cfg.no_remote_charset) {
36566009 1145 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
887035a5 1146 term->sco_acs = term->alt_sco_acs = 0;
1147 term->utf = 0;
0d2086c5 1148 }
64734920 1149 if (!*term->cfg.printer) {
887035a5 1150 term_print_finish(term);
b44b307a 1151 }
0d2086c5 1152}
1153
1154/*
374330e2 1155 * Clear the scrollback.
1156 */
887035a5 1157void term_clrsb(Terminal *term)
32874aea 1158{
36566009 1159 termline *line;
887035a5 1160 term->disptop = 0;
1161 while ((line = delpos234(term->scrollback, 0)) != NULL) {
36566009 1162 sfree(line); /* this is compressed data, not a termline */
4facdf84 1163 }
4683b9b6 1164 term->tempsblines = 0;
876e5d5e 1165 term->alt_sblines = 0;
887035a5 1166 update_sbar(term);
374330e2 1167}
1168
1169/*
1170 * Initialise the terminal.
1171 */
21d2b241 1172Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
1173 void *frontend)
32874aea 1174{
887035a5 1175 Terminal *term;
1176
1177 /*
1178 * Allocate a new Terminal structure and initialise the fields
1179 * that need it.
1180 */
3d88e64d 1181 term = snew(Terminal);
a8327734 1182 term->frontend = frontend;
21d2b241 1183 term->ucsdata = ucsdata;
64734920 1184 term->cfg = *mycfg; /* STRUCTURE COPY */
a8327734 1185 term->logctx = NULL;
887035a5 1186 term->compatibility_level = TM_PUTTY;
1187 strcpy(term->id_string, "\033[?6c");
1188 term->last_blink = term->last_tblink = 0;
1189 term->paste_buffer = NULL;
c80a3e8c 1190 term->paste_len = 0;
887035a5 1191 term->last_paste = 0;
1192 bufchain_init(&term->inbuf);
1193 bufchain_init(&term->printer_buf);
f278d6f8 1194 term->printing = term->only_printing = FALSE;
1195 term->print_job = NULL;
1196 term->vt52_mode = FALSE;
1197 term->cr_lf_return = FALSE;
1198 term->seen_disp_event = FALSE;
b9d7bcad 1199 term->xterm_mouse = term->mouse_is_down = FALSE;
f278d6f8 1200 term->reset_132 = FALSE;
1201 term->blinker = term->tblinker = 0;
1202 term->has_focus = 1;
1203 term->repeat_off = FALSE;
1204 term->termstate = TOPLEVEL;
1205 term->selstate = NO_SELECTION;
093a75b8 1206 term->curstype = 0;
887035a5 1207
1208 term->screen = term->alt_screen = term->scrollback = NULL;
4683b9b6 1209 term->tempsblines = 0;
876e5d5e 1210 term->alt_sblines = 0;
887035a5 1211 term->disptop = 0;
36566009 1212 term->disptext = NULL;
1213 term->dispcursx = term->dispcursy = -1;
887035a5 1214 term->tabs = NULL;
1215 deselect(term);
1216 term->rows = term->cols = -1;
1217 power_on(term);
1218 term->beephead = term->beeptail = NULL;
341eb978 1219#ifdef OPTIMISE_SCROLL
1220 term->scrollhead = term->scrolltail = NULL;
1221#endif /* OPTIMISE_SCROLL */
887035a5 1222 term->nbeeps = 0;
1223 term->lastbeep = FALSE;
1224 term->beep_overloaded = FALSE;
2647b103 1225 term->attr_mask = 0xffffffff;
51470298 1226 term->resize_fn = NULL;
1227 term->resize_ctx = NULL;
74aca06d 1228 term->in_term_out = FALSE;
f0fccd51 1229 term->ltemp = NULL;
c6958dfe 1230 term->ltemp_size = 0;
f0fccd51 1231 term->wcFrom = NULL;
1232 term->wcTo = NULL;
c6958dfe 1233 term->wcFromTo_size = 0;
f0fccd51 1234
1235 term->bidi_cache_size = 0;
1236 term->pre_bidi_cache = term->post_bidi_cache = NULL;
887035a5 1237
c6958dfe 1238 /* FULL-TERMCHAR */
36566009 1239 term->basic_erase_char.chr = CSET_ASCII | ' ';
1240 term->basic_erase_char.attr = ATTR_DEFAULT;
c6958dfe 1241 term->basic_erase_char.cc_next = 0;
1242 term->erase_char = term->basic_erase_char;
36566009 1243
887035a5 1244 return term;
374330e2 1245}
1246
fabd1805 1247void term_free(Terminal *term)
1248{
36566009 1249 termline *line;
fabd1805 1250 struct beeptime *beep;
f0fccd51 1251 int i;
fabd1805 1252
1253 while ((line = delpos234(term->scrollback, 0)) != NULL)
36566009 1254 sfree(line); /* compressed data, not a termline */
fabd1805 1255 freetree234(term->scrollback);
1256 while ((line = delpos234(term->screen, 0)) != NULL)
36566009 1257 freeline(line);
fabd1805 1258 freetree234(term->screen);
1259 while ((line = delpos234(term->alt_screen, 0)) != NULL)
36566009 1260 freeline(line);
fabd1805 1261 freetree234(term->alt_screen);
36566009 1262 if (term->disptext) {
1263 for (i = 0; i < term->rows; i++)
1264 freeline(term->disptext[i]);
1265 }
fabd1805 1266 sfree(term->disptext);
1267 while (term->beephead) {
1268 beep = term->beephead;
1269 term->beephead = beep->next;
1270 sfree(beep);
1271 }
1272 bufchain_clear(&term->inbuf);
1273 if(term->print_job)
1274 printer_finish_job(term->print_job);
1275 bufchain_clear(&term->printer_buf);
1276 sfree(term->paste_buffer);
f0fccd51 1277 sfree(term->ltemp);
1278 sfree(term->wcFrom);
1279 sfree(term->wcTo);
1280
1281 for (i = 0; i < term->bidi_cache_size; i++) {
1282 sfree(term->pre_bidi_cache[i]);
1283 sfree(term->post_bidi_cache[i]);
1284 }
1285 sfree(term->pre_bidi_cache);
1286 sfree(term->post_bidi_cache);
1287
fabd1805 1288 sfree(term);
1289}
1290
374330e2 1291/*
1292 * Set up the terminal for a given size.
1293 */
887035a5 1294void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
32874aea 1295{
2d466ffd 1296 tree234 *newalt;
36566009 1297 termline **newdisp, *line;
05bea829 1298 int i, j, oldrows = term->rows;
c83de303 1299 int sblen;
887035a5 1300 int save_alt_which = term->alt_which;
d6832394 1301
887035a5 1302 if (newrows == term->rows && newcols == term->cols &&
1303 newsavelines == term->savelines)
374330e2 1304 return; /* nothing to do */
1305
887035a5 1306 deselect(term);
1307 swap_screen(term, 0, FALSE, FALSE);
d6832394 1308
887035a5 1309 term->alt_t = term->marg_t = 0;
1310 term->alt_b = term->marg_b = newrows - 1;
374330e2 1311
887035a5 1312 if (term->rows == -1) {
1313 term->scrollback = newtree234(NULL);
1314 term->screen = newtree234(NULL);
4683b9b6 1315 term->tempsblines = 0;
887035a5 1316 term->rows = 0;
c83de303 1317 }
1318
1319 /*
1320 * Resize the screen and scrollback. We only need to shift
1321 * lines around within our data structures, because lineptr()
1322 * will take care of resizing each individual line if
1323 * necessary. So:
1324 *
4683b9b6 1325 * - If the new screen is longer, we shunt lines in from temporary
1326 * scrollback if possible, otherwise we add new blank lines at
1327 * the bottom.
1328 *
1329 * - If the new screen is shorter, we remove any blank lines at
1330 * the bottom if possible, otherwise shunt lines above the cursor
1331 * to scrollback if possible, otherwise delete lines below the
1332 * cursor.
c83de303 1333 *
1334 * - Then, if the new scrollback length is less than the
1335 * amount of scrollback we actually have, we must throw some
1336 * away.
1337 */
887035a5 1338 sblen = count234(term->scrollback);
cdd6c586 1339 /* Do this loop to expand the screen if newrows > rows */
4683b9b6 1340 assert(term->rows == count234(term->screen));
1341 while (term->rows < newrows) {
1342 if (term->tempsblines > 0) {
36566009 1343 unsigned char *cline;
4683b9b6 1344 /* Insert a line from the scrollback at the top of the screen. */
1345 assert(sblen >= term->tempsblines);
36566009 1346 cline = delpos234(term->scrollback, --sblen);
1347 line = decompressline(cline, NULL);
1348 sfree(cline);
1349 line->temporary = FALSE; /* reconstituted line is now real */
4683b9b6 1350 term->tempsblines -= 1;
1351 addpos234(term->screen, line, 0);
1352 term->curs.y += 1;
1353 term->savecurs.y += 1;
32874aea 1354 } else {
4683b9b6 1355 /* Add a new blank line at the bottom of the screen. */
36566009 1356 line = newline(term, newcols, FALSE);
4683b9b6 1357 addpos234(term->screen, line, count234(term->screen));
32874aea 1358 }
4683b9b6 1359 term->rows += 1;
cdd6c586 1360 }
1361 /* Do this loop to shrink the screen if newrows < rows */
4683b9b6 1362 while (term->rows > newrows) {
1363 if (term->curs.y < term->rows - 1) {
1364 /* delete bottom row, unless it contains the cursor */
1365 sfree(delpos234(term->screen, term->rows - 1));
1366 } else {
1367 /* push top row to scrollback */
1368 line = delpos234(term->screen, 0);
36566009 1369 addpos234(term->scrollback, compressline(line), sblen++);
1370 freeline(line);
4683b9b6 1371 term->tempsblines += 1;
1372 term->curs.y -= 1;
1373 term->savecurs.y -= 1;
1374 }
1375 term->rows -= 1;
c83de303 1376 }
4683b9b6 1377 assert(term->rows == newrows);
887035a5 1378 assert(count234(term->screen) == newrows);
4683b9b6 1379
1380 /* Delete any excess lines from the scrollback. */
c83de303 1381 while (sblen > newsavelines) {
887035a5 1382 line = delpos234(term->scrollback, 0);
c83de303 1383 sfree(line);
1384 sblen--;
c9def1b8 1385 }
4683b9b6 1386 if (sblen < term->tempsblines)
1387 term->tempsblines = sblen;
887035a5 1388 assert(count234(term->scrollback) <= newsavelines);
4683b9b6 1389 assert(count234(term->scrollback) >= term->tempsblines);
887035a5 1390 term->disptop = 0;
374330e2 1391
4683b9b6 1392 /* Make a new displayed text buffer. */
36566009 1393 newdisp = snewn(newrows, termline *);
1394 for (i = 0; i < newrows; i++) {
1395 newdisp[i] = newline(term, newcols, FALSE);
1396 for (j = 0; j < newcols; j++)
1397 newdisp[i]->chars[i].attr = ATTR_INVALID;
1398 }
1399 if (term->disptext) {
05bea829 1400 for (i = 0; i < oldrows; i++)
36566009 1401 freeline(term->disptext[i]);
1402 }
887035a5 1403 sfree(term->disptext);
1404 term->disptext = newdisp;
36566009 1405 term->dispcursx = term->dispcursy = -1;
374330e2 1406
4683b9b6 1407 /* Make a new alternate screen. */
4facdf84 1408 newalt = newtree234(NULL);
32874aea 1409 for (i = 0; i < newrows; i++) {
36566009 1410 line = newline(term, newcols, TRUE);
4facdf84 1411 addpos234(newalt, line, i);
1412 }
887035a5 1413 if (term->alt_screen) {
1414 while (NULL != (line = delpos234(term->alt_screen, 0)))
36566009 1415 freeline(line);
887035a5 1416 freetree234(term->alt_screen);
4facdf84 1417 }
887035a5 1418 term->alt_screen = newalt;
876e5d5e 1419 term->alt_sblines = 0;
374330e2 1420
3d88e64d 1421 term->tabs = sresize(term->tabs, newcols, unsigned char);
374330e2 1422 {
1423 int i;
887035a5 1424 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1425 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 1426 }
1427
4683b9b6 1428 /* Check that the cursor positions are still valid. */
1429 if (term->savecurs.y < 0)
1430 term->savecurs.y = 0;
1431 if (term->savecurs.y >= newrows)
1432 term->savecurs.y = newrows - 1;
887035a5 1433 if (term->curs.y < 0)
1434 term->curs.y = 0;
1435 if (term->curs.y >= newrows)
1436 term->curs.y = newrows - 1;
1437 if (term->curs.x >= newcols)
1438 term->curs.x = newcols - 1;
1439 term->alt_x = term->alt_y = 0;
1440 term->wrapnext = term->alt_wnext = FALSE;
1441
1442 term->rows = newrows;
1443 term->cols = newcols;
1444 term->savelines = newsavelines;
374330e2 1445
887035a5 1446 swap_screen(term, save_alt_which, FALSE, FALSE);
d6832394 1447
887035a5 1448 update_sbar(term);
1449 term_update(term);
51470298 1450 if (term->resize_fn)
1451 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1452}
1453
1454/*
1455 * Hand a function and context pointer to the terminal which it can
1456 * use to notify a back end of resizes.
1457 */
1458void term_provide_resize_fn(Terminal *term,
1459 void (*resize_fn)(void *, int, int),
1460 void *resize_ctx)
1461{
1462 term->resize_fn = resize_fn;
1463 term->resize_ctx = resize_ctx;
1464 if (term->cols > 0 && term->rows > 0)
1465 resize_fn(resize_ctx, term->cols, term->rows);
374330e2 1466}
1467
876e5d5e 1468/* Find the bottom line on the screen that has any content.
1469 * If only the top line has content, returns 0.
1470 * If no lines have content, return -1.
1471 */
1472static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1473{
1474 int i;
1475 for (i = count234(screen) - 1; i >= 0; i--) {
36566009 1476 termline *line = index234(screen, i);
876e5d5e 1477 int j;
c6958dfe 1478 for (j = 0; j < line->cols; j++)
1479 if (!termchars_equal(&line->chars[j], &term->erase_char))
1480 break;
36566009 1481 if (j != line->cols) break;
876e5d5e 1482 }
1483 return i;
1484}
1485
374330e2 1486/*
8f9b6a1a 1487 * Swap screens. If `reset' is TRUE and we have been asked to
1488 * switch to the alternate screen, we must bring most of its
1489 * configuration from the main screen and erase the contents of the
1490 * alternate screen completely. (This is even true if we're already
1491 * on it! Blame xterm.)
374330e2 1492 */
887035a5 1493static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
32874aea 1494{
374330e2 1495 int t;
4facdf84 1496 tree234 *ttr;
374330e2 1497
8f9b6a1a 1498 if (!which)
1499 reset = FALSE; /* do no weird resetting if which==0 */
1500
887035a5 1501 if (which != term->alt_which) {
1502 term->alt_which = which;
8f9b6a1a 1503
887035a5 1504 ttr = term->alt_screen;
1505 term->alt_screen = term->screen;
1506 term->screen = ttr;
876e5d5e 1507 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
887035a5 1508 t = term->curs.x;
8f9b6a1a 1509 if (!reset && !keep_cur_pos)
887035a5 1510 term->curs.x = term->alt_x;
1511 term->alt_x = t;
1512 t = term->curs.y;
8f9b6a1a 1513 if (!reset && !keep_cur_pos)
887035a5 1514 term->curs.y = term->alt_y;
1515 term->alt_y = t;
1516 t = term->marg_t;
1517 if (!reset) term->marg_t = term->alt_t;
1518 term->alt_t = t;
1519 t = term->marg_b;
1520 if (!reset) term->marg_b = term->alt_b;
1521 term->alt_b = t;
1522 t = term->dec_om;
1523 if (!reset) term->dec_om = term->alt_om;
1524 term->alt_om = t;
1525 t = term->wrap;
1526 if (!reset) term->wrap = term->alt_wrap;
1527 term->alt_wrap = t;
1528 t = term->wrapnext;
1529 if (!reset) term->wrapnext = term->alt_wnext;
1530 term->alt_wnext = t;
1531 t = term->insert;
1532 if (!reset) term->insert = term->alt_ins;
1533 term->alt_ins = t;
1534 t = term->cset;
1535 if (!reset) term->cset = term->alt_cset;
1536 term->alt_cset = t;
1537 t = term->utf;
1538 if (!reset) term->utf = term->alt_utf;
1539 term->alt_utf = t;
1540 t = term->sco_acs;
1541 if (!reset) term->sco_acs = term->alt_sco_acs;
1542 term->alt_sco_acs = t;
8f9b6a1a 1543 }
374330e2 1544
887035a5 1545 if (reset && term->screen) {
8f9b6a1a 1546 /*
1547 * Yes, this _is_ supposed to honour background-colour-erase.
1548 */
887035a5 1549 erase_lots(term, FALSE, TRUE, TRUE);
8f9b6a1a 1550 }
374330e2 1551}
1552
1553/*
374330e2 1554 * Update the scroll bar.
1555 */
887035a5 1556static void update_sbar(Terminal *term)
32874aea 1557{
876e5d5e 1558 int nscroll = sblines(term);
a8327734 1559 set_sbar(term->frontend, nscroll + term->rows,
1560 nscroll + term->disptop, term->rows);
374330e2 1561}
1562
1563/*
1564 * Check whether the region bounded by the two pointers intersects
1565 * the scroll region, and de-select the on-screen selection if so.
1566 */
887035a5 1567static void check_selection(Terminal *term, pos from, pos to)
32874aea 1568{
887035a5 1569 if (poslt(from, term->selend) && poslt(term->selstart, to))
1570 deselect(term);
374330e2 1571}
1572
1573/*
1574 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1575 * for backward.) `sb' is TRUE if the scrolling is permitted to
1576 * affect the scrollback buffer.
1577 */
887035a5 1578static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
32874aea 1579{
36566009 1580 termline *line;
7c66d7d9 1581 int i, seltop, olddisptop, shift;
c9def1b8 1582
887035a5 1583 if (topline != 0 || term->alt_which != 0)
4facdf84 1584 sb = FALSE;
1585
7c66d7d9 1586 olddisptop = term->disptop;
1587 shift = lines;
4facdf84 1588 if (lines < 0) {
1589 while (lines < 0) {
887035a5 1590 line = delpos234(term->screen, botline);
36566009 1591 resizeline(term, line, term->cols);
887035a5 1592 for (i = 0; i < term->cols; i++)
c6958dfe 1593 copy_termchar(line, i, &term->erase_char);
36566009 1594 line->lattr = LATTR_NORM;
887035a5 1595 addpos234(term->screen, line, topline);
1596
1597 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1598 term->selstart.y++;
1599 if (term->selstart.y > botline) {
5a8afc78 1600 term->selstart.y = botline + 1;
887035a5 1601 term->selstart.x = 0;
c9def1b8 1602 }
c9def1b8 1603 }
887035a5 1604 if (term->selend.y >= topline && term->selend.y <= botline) {
1605 term->selend.y++;
1606 if (term->selend.y > botline) {
5a8afc78 1607 term->selend.y = botline + 1;
887035a5 1608 term->selend.x = 0;
c9def1b8 1609 }
c9def1b8 1610 }
c9def1b8 1611
4facdf84 1612 lines++;
c9def1b8 1613 }
374330e2 1614 } else {
4facdf84 1615 while (lines > 0) {
887035a5 1616 line = delpos234(term->screen, topline);
4fe051a2 1617 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
887035a5 1618 if (sb && term->savelines > 0) {
4683b9b6 1619 int sblen = count234(term->scrollback);
4facdf84 1620 /*
1621 * We must add this line to the scrollback. We'll
36566009 1622 * remove a line from the top of the scrollback if
1623 * the scrollback is full.
4facdf84 1624 */
887035a5 1625 if (sblen == term->savelines) {
36566009 1626 unsigned char *cline;
1627
1628 sblen--;
1629 cline = delpos234(term->scrollback, 0);
1630 sfree(cline);
1631 } else
4683b9b6 1632 term->tempsblines += 1;
36566009 1633
1634 addpos234(term->scrollback, compressline(line), sblen);
1635
1636 line = newline(term, term->cols, TRUE);
a792432e 1637
1638 /*
1639 * If the user is currently looking at part of the
1640 * scrollback, and they haven't enabled any options
1641 * that are going to reset the scrollback as a
1642 * result of this movement, then the chances are
1643 * they'd like to keep looking at the same line. So
1644 * we move their viewpoint at the same rate as the
1645 * scroll, at least until their viewpoint hits the
1646 * top end of the scrollback buffer, at which point
1647 * we don't have the choice any more.
1648 *
1649 * Thanks to Jan Holmen Holsten for the idea and
1650 * initial implementation.
1651 */
887035a5 1652 if (term->disptop > -term->savelines && term->disptop < 0)
1653 term->disptop--;
4facdf84 1654 }
36566009 1655 resizeline(term, line, term->cols);
887035a5 1656 for (i = 0; i < term->cols; i++)
c6958dfe 1657 copy_termchar(line, i, &term->erase_char);
36566009 1658 line->lattr = LATTR_NORM;
887035a5 1659 addpos234(term->screen, line, botline);
4facdf84 1660
a792432e 1661 /*
1662 * If the selection endpoints move into the scrollback,
1663 * we keep them moving until they hit the top. However,
1664 * of course, if the line _hasn't_ moved into the
1665 * scrollback then we don't do this, and cut them off
1666 * at the top of the scroll region.
d48db8b4 1667 *
1668 * This applies to selstart and selend (for an existing
1669 * selection), and also selanchor (for one being
1670 * selected as we speak).
a792432e 1671 */
887035a5 1672 seltop = sb ? -term->savelines : topline;
1673
c989dbc4 1674 if (term->selstate != NO_SELECTION) {
1675 if (term->selstart.y >= seltop &&
1676 term->selstart.y <= botline) {
1677 term->selstart.y--;
1678 if (term->selstart.y < seltop) {
1679 term->selstart.y = seltop;
1680 term->selstart.x = 0;
1681 }
4facdf84 1682 }
c989dbc4 1683 if (term->selend.y >= seltop && term->selend.y <= botline) {
1684 term->selend.y--;
1685 if (term->selend.y < seltop) {
1686 term->selend.y = seltop;
1687 term->selend.x = 0;
1688 }
4facdf84 1689 }
c989dbc4 1690 if (term->selanchor.y >= seltop &&
1691 term->selanchor.y <= botline) {
1692 term->selanchor.y--;
1693 if (term->selanchor.y < seltop) {
1694 term->selanchor.y = seltop;
1695 term->selanchor.x = 0;
1696 }
d48db8b4 1697 }
1698 }
4facdf84 1699
1700 lines--;
c9def1b8 1701 }
374330e2 1702 }
7c66d7d9 1703#ifdef OPTIMISE_SCROLL
1704 shift += term->disptop - olddisptop;
1705 if (shift < term->rows && shift > -term->rows && shift != 0)
1706 scroll_display(term, topline, botline, shift);
1707#endif /* OPTIMISE_SCROLL */
374330e2 1708}
1709
37d2a505 1710#ifdef OPTIMISE_SCROLL
1711/*
341eb978 1712 * Add a scroll of a region on the screen into the pending scroll list.
1713 * `lines' is +ve for scrolling forward, -ve for backward.
1714 *
1715 * If the scroll is on the same area as the last scroll in the list,
1716 * merge them.
1717 */
03f60a62 1718static void save_scroll(Terminal *term, int topline, int botline, int lines)
341eb978 1719{
1720 struct scrollregion *newscroll;
1721 if (term->scrolltail &&
1722 term->scrolltail->topline == topline &&
1723 term->scrolltail->botline == botline) {
1724 term->scrolltail->lines += lines;
1725 } else {
3d88e64d 1726 newscroll = snew(struct scrollregion);
341eb978 1727 newscroll->topline = topline;
1728 newscroll->botline = botline;
1729 newscroll->lines = lines;
1730 newscroll->next = NULL;
1731
1732 if (!term->scrollhead)
1733 term->scrollhead = newscroll;
1734 else
1735 term->scrolltail->next = newscroll;
1736 term->scrolltail = newscroll;
1737 }
1738}
1739
1740/*
37d2a505 1741 * Scroll the physical display, and our conception of it in disptext.
1742 */
1743static void scroll_display(Terminal *term, int topline, int botline, int lines)
1744{
c6958dfe 1745 int distance, nlines, i, j;
37d2a505 1746
36566009 1747 distance = lines > 0 ? lines : -lines;
1748 nlines = botline - topline + 1 - distance;
37d2a505 1749 if (lines > 0) {
36566009 1750 for (i = 0; i < nlines; i++)
c6958dfe 1751 for (j = 0; j < term->cols; j++)
1752 copy_termchar(term->disptext[start+i], j,
1753 term->disptext[start+i+distance]->chars+j);
36566009 1754 if (term->dispcursy >= 0 &&
1755 term->dispcursy >= topline + distance &&
1756 term->dispcursy < topline + distance + nlines)
1757 term->dispcursy -= distance;
37d2a505 1758 for (i = 0; i < distance; i++)
36566009 1759 for (j = 0; j < term->cols; j++)
1760 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
37d2a505 1761 } else {
36566009 1762 for (i = nlines; i-- ;)
c6958dfe 1763 for (j = 0; j < term->cols; j++)
1764 copy_termchar(term->disptext[start+i+distance], j,
1765 term->disptext[start+i]->chars+j);
36566009 1766 if (term->dispcursy >= 0 &&
1767 term->dispcursy >= topline &&
1768 term->dispcursy < topline + nlines)
1769 term->dispcursy += distance;
37d2a505 1770 for (i = 0; i < distance; i++)
36566009 1771 for (j = 0; j < term->cols; j++)
1772 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
37d2a505 1773 }
341eb978 1774 save_scroll(term, topline, botline, lines);
37d2a505 1775}
1776#endif /* OPTIMISE_SCROLL */
1777
374330e2 1778/*
1779 * Move the cursor to a given position, clipping at boundaries. We
1780 * may or may not want to clip at the scroll margin: marg_clip is 0
1781 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1782 * even _being_ outside the margins.
1783 */
887035a5 1784static void move(Terminal *term, int x, int y, int marg_clip)
32874aea 1785{
374330e2 1786 if (x < 0)
1787 x = 0;
887035a5 1788 if (x >= term->cols)
1789 x = term->cols - 1;
374330e2 1790 if (marg_clip) {
887035a5 1791 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1792 y < term->marg_t)
1793 y = term->marg_t;
1794 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1795 y > term->marg_b)
1796 y = term->marg_b;
374330e2 1797 }
1798 if (y < 0)
1799 y = 0;
887035a5 1800 if (y >= term->rows)
1801 y = term->rows - 1;
1802 term->curs.x = x;
1803 term->curs.y = y;
887035a5 1804 term->wrapnext = FALSE;
374330e2 1805}
1806
1807/*
1808 * Save or restore the cursor and SGR mode.
1809 */
887035a5 1810static void save_cursor(Terminal *term, int save)
32874aea 1811{
374330e2 1812 if (save) {
887035a5 1813 term->savecurs = term->curs;
1814 term->save_attr = term->curr_attr;
1815 term->save_cset = term->cset;
1816 term->save_utf = term->utf;
1817 term->save_wnext = term->wrapnext;
1818 term->save_csattr = term->cset_attr[term->cset];
1819 term->save_sco_acs = term->sco_acs;
374330e2 1820 } else {
887035a5 1821 term->curs = term->savecurs;
c9def1b8 1822 /* Make sure the window hasn't shrunk since the save */
887035a5 1823 if (term->curs.x >= term->cols)
1824 term->curs.x = term->cols - 1;
1825 if (term->curs.y >= term->rows)
1826 term->curs.y = term->rows - 1;
1827
1828 term->curr_attr = term->save_attr;
1829 term->cset = term->save_cset;
1830 term->utf = term->save_utf;
1831 term->wrapnext = term->save_wnext;
8c028e65 1832 /*
1833 * wrapnext might reset to False if the x position is no
1834 * longer at the rightmost edge.
1835 */
887035a5 1836 if (term->wrapnext && term->curs.x < term->cols-1)
1837 term->wrapnext = FALSE;
1838 term->cset_attr[term->cset] = term->save_csattr;
1839 term->sco_acs = term->save_sco_acs;
36566009 1840 set_erase_char(term);
374330e2 1841 }
1842}
1843
1844/*
3c7366f8 1845 * This function is called before doing _anything_ which affects
1846 * only part of a line of text. It is used to mark the boundary
1847 * between two character positions, and it indicates that some sort
1848 * of effect is going to happen on only one side of that boundary.
1849 *
1850 * The effect of this function is to check whether a CJK
1851 * double-width character is straddling the boundary, and to remove
1852 * it and replace it with two spaces if so. (Of course, one or
1853 * other of those spaces is then likely to be replaced with
1854 * something else again, as a result of whatever happens next.)
1855 *
1856 * Also, if the boundary is at the right-hand _edge_ of the screen,
1857 * it implies something deliberate is being done to the rightmost
1858 * column position; hence we must clear LATTR_WRAPPED2.
1859 *
1860 * The input to the function is the coordinates of the _second_
1861 * character of the pair.
1862 */
1863static void check_boundary(Terminal *term, int x, int y)
1864{
36566009 1865 termline *ldata;
3c7366f8 1866
1867 /* Validate input coordinates, just in case. */
1868 if (x == 0 || x > term->cols)
1869 return;
1870
36566009 1871 ldata = scrlineptr(y);
3c7366f8 1872 if (x == term->cols) {
36566009 1873 ldata->lattr &= ~LATTR_WRAPPED2;
3c7366f8 1874 } else {
36566009 1875 if (ldata->chars[x].chr == UCSWIDE) {
c6958dfe 1876 clear_cc(ldata, x-1);
1877 clear_cc(ldata, x);
36566009 1878 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
1879 ldata->chars[x] = ldata->chars[x-1];
3c7366f8 1880 }
1881 }
1882}
1883
1884/*
374330e2 1885 * Erase a large portion of the screen: the whole screen, or the
1886 * whole line, or parts thereof.
1887 */
887035a5 1888static void erase_lots(Terminal *term,
1889 int line_only, int from_begin, int to_end)
32874aea 1890{
260f3dec 1891 pos start, end;
4facdf84 1892 int erase_lattr;
4683b9b6 1893 int erasing_lines_from_top = 0;
374330e2 1894
1895 if (line_only) {
887035a5 1896 start.y = term->curs.y;
4facdf84 1897 start.x = 0;
887035a5 1898 end.y = term->curs.y + 1;
4facdf84 1899 end.x = 0;
1900 erase_lattr = FALSE;
374330e2 1901 } else {
4facdf84 1902 start.y = 0;
1903 start.x = 0;
887035a5 1904 end.y = term->rows;
4facdf84 1905 end.x = 0;
1906 erase_lattr = TRUE;
1907 }
1908 if (!from_begin) {
887035a5 1909 start = term->curs;
374330e2 1910 }
4facdf84 1911 if (!to_end) {
887035a5 1912 end = term->curs;
2a6dfe0e 1913 incpos(end);
4facdf84 1914 }
3c7366f8 1915 if (!from_begin || !to_end)
1916 check_boundary(term, term->curs.x, term->curs.y);
887035a5 1917 check_selection(term, start, end);
e14a5a13 1918
1919 /* Clear screen also forces a full window redraw, just in case. */
887035a5 1920 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1921 term_invalidate(term);
e14a5a13 1922
4683b9b6 1923 /* Lines scrolled away shouldn't be brought back on if the terminal
1924 * resizes. */
1925 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1926 erasing_lines_from_top = 1;
1927
1928 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
876e5d5e 1929 /* If it's a whole number of lines, starting at the top, and
1930 * we're fully erasing them, erase by scrolling and keep the
1931 * lines in the scrollback. */
1932 int scrolllines = end.y;
1933 if (end.y == term->rows) {
1934 /* Shrink until we find a non-empty row.*/
1935 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1936 }
1937 if (scrolllines > 0)
1938 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
876e5d5e 1939 } else {
36566009 1940 termline *ldata = scrlineptr(start.y);
876e5d5e 1941 while (poslt(start, end)) {
1942 if (start.x == term->cols) {
1943 if (!erase_lattr)
36566009 1944 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
876e5d5e 1945 else
36566009 1946 ldata->lattr = LATTR_NORM;
876e5d5e 1947 } else {
c6958dfe 1948 copy_termchar(ldata, start.x, &term->erase_char);
36566009 1949 }
1950 if (incpos(start) && start.y < term->rows) {
1951 ldata = scrlineptr(start.y);
876e5d5e 1952 }
876e5d5e 1953 }
4facdf84 1954 }
4683b9b6 1955
1956 /* After an erase of lines from the top of the screen, we shouldn't
1957 * bring the lines back again if the terminal enlarges (since the user or
1958 * application has explictly thrown them away). */
1959 if (erasing_lines_from_top && !(term->alt_which))
1960 term->tempsblines = 0;
374330e2 1961}
1962
1963/*
1964 * Insert or delete characters within the current line. n is +ve if
1965 * insertion is desired, and -ve for deletion.
1966 */
887035a5 1967static void insch(Terminal *term, int n)
32874aea 1968{
374330e2 1969 int dir = (n < 0 ? -1 : +1);
c6958dfe 1970 int m, j;
4facdf84 1971 pos cursplus;
36566009 1972 termline *ldata;
374330e2 1973
1974 n = (n < 0 ? -n : n);
887035a5 1975 if (n > term->cols - term->curs.x)
1976 n = term->cols - term->curs.x;
1977 m = term->cols - term->curs.x - n;
1978 cursplus.y = term->curs.y;
1979 cursplus.x = term->curs.x + n;
1980 check_selection(term, term->curs, cursplus);
3c7366f8 1981 check_boundary(term, term->curs.x, term->curs.y);
1982 if (dir < 0)
1983 check_boundary(term, term->curs.x + n, term->curs.y);
36566009 1984 ldata = scrlineptr(term->curs.y);
374330e2 1985 if (dir < 0) {
c6958dfe 1986 for (j = 0; j < m; j++)
1987 move_termchar(ldata,
1988 ldata->chars + term->curs.x + j,
1989 ldata->chars + term->curs.x + j + n);
374330e2 1990 while (n--)
c6958dfe 1991 copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
374330e2 1992 } else {
c6958dfe 1993 for (j = m; j-- ;)
1994 move_termchar(ldata,
1995 ldata->chars + term->curs.x + j + n,
1996 ldata->chars + term->curs.x + j);
374330e2 1997 while (n--)
c6958dfe 1998 copy_termchar(ldata, term->curs.x + n, &term->erase_char);
374330e2 1999 }
2000}
2001
2002/*
2003 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2004 * whether the mode is a DEC private one or a normal one.)
2005 */
887035a5 2006static void toggle_mode(Terminal *term, int mode, int query, int state)
32874aea 2007{
286c6b86 2008 unsigned long ticks;
01c034ad 2009
32874aea 2010 if (query)
2011 switch (mode) {
a263e4f6 2012 case 1: /* DECCKM: application cursor keys */
887035a5 2013 term->app_cursor_keys = state;
32874aea 2014 break;
a263e4f6 2015 case 2: /* DECANM: VT52 mode */
887035a5 2016 term->vt52_mode = !state;
2017 if (term->vt52_mode) {
2018 term->blink_is_real = FALSE;
2019 term->vt52_bold = FALSE;
4eeb7d09 2020 } else {
64734920 2021 term->blink_is_real = term->cfg.blinktext;
4eeb7d09 2022 }
32874aea 2023 break;
a263e4f6 2024 case 3: /* DECCOLM: 80/132 columns */
887035a5 2025 deselect(term);
64734920 2026 if (!term->cfg.no_remote_resize)
a8327734 2027 request_resize(term->frontend, state ? 132 : 80, term->rows);
887035a5 2028 term->reset_132 = state;
7ce3b333 2029 term->alt_t = term->marg_t = 0;
2030 term->alt_b = term->marg_b = term->rows - 1;
2031 move(term, 0, 0, 0);
2032 erase_lots(term, FALSE, TRUE, TRUE);
32874aea 2033 break;
a263e4f6 2034 case 5: /* DECSCNM: reverse video */
32874aea 2035 /*
2036 * Toggle reverse video. If we receive an OFF within the
2037 * visual bell timeout period after an ON, we trigger an
2038 * effective visual bell, so that ESC[?5hESC[?5l will
2039 * always be an actually _visible_ visual bell.
2040 */
f7f27309 2041 ticks = GETTICKCOUNT();
286c6b86 2042 /* turn off a previous vbell to avoid inconsistencies */
887035a5 2043 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2044 term->in_vbell = FALSE;
2045 if (term->rvideo && !state && /* we're turning it off... */
2046 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
286c6b86 2047 /* If there's no vbell timeout already, or this one lasts
2048 * longer, replace vbell_timeout with ours. */
887035a5 2049 if (!term->in_vbell ||
2050 (term->rvbell_startpoint - term->vbell_startpoint <
2051 VBELL_TIMEOUT))
2052 term->vbell_startpoint = term->rvbell_startpoint;
2053 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
2054 } else if (!term->rvideo && state) {
32874aea 2055 /* This is an ON, so we notice the time and save it. */
887035a5 2056 term->rvbell_startpoint = ticks;
32874aea 2057 }
887035a5 2058 term->rvideo = state;
2059 term->seen_disp_event = TRUE;
32874aea 2060 if (state)
887035a5 2061 term_update(term);
32874aea 2062 break;
a263e4f6 2063 case 6: /* DECOM: DEC origin mode */
887035a5 2064 term->dec_om = state;
32874aea 2065 break;
a263e4f6 2066 case 7: /* DECAWM: auto wrap */
887035a5 2067 term->wrap = state;
32874aea 2068 break;
a263e4f6 2069 case 8: /* DECARM: auto key repeat */
887035a5 2070 term->repeat_off = !state;
32874aea 2071 break;
a263e4f6 2072 case 10: /* DECEDM: set local edit mode */
887035a5 2073 term->term_editing = state;
b9d7bcad 2074 if (term->ldisc) /* cause ldisc to notice changes */
2075 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 2076 break;
a263e4f6 2077 case 25: /* DECTCEM: enable/disable cursor */
32874aea 2078 compatibility2(OTHER, VT220);
887035a5 2079 term->cursor_on = state;
2080 term->seen_disp_event = TRUE;
32874aea 2081 break;
2082 case 47: /* alternate screen */
2083 compatibility(OTHER);
887035a5 2084 deselect(term);
64734920 2085 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
887035a5 2086 term->disptop = 0;
32874aea 2087 break;
2088 case 1000: /* xterm mouse 1 */
887035a5 2089 term->xterm_mouse = state ? 1 : 0;
a8327734 2090 set_raw_mouse_mode(term->frontend, state);
32874aea 2091 break;
2092 case 1002: /* xterm mouse 2 */
887035a5 2093 term->xterm_mouse = state ? 2 : 0;
a8327734 2094 set_raw_mouse_mode(term->frontend, state);
32874aea 2095 break;
8f9b6a1a 2096 case 1047: /* alternate screen */
2097 compatibility(OTHER);
887035a5 2098 deselect(term);
64734920 2099 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
887035a5 2100 term->disptop = 0;
8f9b6a1a 2101 break;
2102 case 1048: /* save/restore cursor */
4c80361f 2103 if (!term->cfg.no_alt_screen)
2104 save_cursor(term, state);
887035a5 2105 if (!state) term->seen_disp_event = TRUE;
8f9b6a1a 2106 break;
2107 case 1049: /* cursor & alternate screen */
4c80361f 2108 if (state && !term->cfg.no_alt_screen)
887035a5 2109 save_cursor(term, state);
2110 if (!state) term->seen_disp_event = TRUE;
8f9b6a1a 2111 compatibility(OTHER);
887035a5 2112 deselect(term);
64734920 2113 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
4c80361f 2114 if (!state && !term->cfg.no_alt_screen)
887035a5 2115 save_cursor(term, state);
2116 term->disptop = 0;
8f9b6a1a 2117 break;
32874aea 2118 } else
2119 switch (mode) {
a263e4f6 2120 case 4: /* IRM: set insert mode */
32874aea 2121 compatibility(VT102);
887035a5 2122 term->insert = state;
32874aea 2123 break;
a263e4f6 2124 case 12: /* SRM: set echo mode */
887035a5 2125 term->term_echoing = !state;
b9d7bcad 2126 if (term->ldisc) /* cause ldisc to notice changes */
2127 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 2128 break;
a263e4f6 2129 case 20: /* LNM: Return sends ... */
887035a5 2130 term->cr_lf_return = state;
32874aea 2131 break;
a263e4f6 2132 case 34: /* WYULCURM: Make cursor BIG */
4eeb7d09 2133 compatibility2(OTHER, VT220);
887035a5 2134 term->big_cursor = !state;
7594731b 2135 }
374330e2 2136}
2137
2138/*
2139 * Process an OSC sequence: set window title or icon name.
2140 */
887035a5 2141static void do_osc(Terminal *term)
32874aea 2142{
887035a5 2143 if (term->osc_w) {
2144 while (term->osc_strlen--)
2145 term->wordness[(unsigned char)
2146 term->osc_string[term->osc_strlen]] = term->esc_args[0];
374330e2 2147 } else {
887035a5 2148 term->osc_string[term->osc_strlen] = '\0';
2149 switch (term->esc_args[0]) {
374330e2 2150 case 0:
2151 case 1:
64734920 2152 if (!term->cfg.no_remote_wintitle)
a8327734 2153 set_icon(term->frontend, term->osc_string);
887035a5 2154 if (term->esc_args[0] == 1)
374330e2 2155 break;
2156 /* fall through: parameter 0 means set both */
2157 case 2:
2158 case 21:
64734920 2159 if (!term->cfg.no_remote_wintitle)
a8327734 2160 set_title(term->frontend, term->osc_string);
374330e2 2161 break;
2162 }
2163 }
2164}
2165
2166/*
b44b307a 2167 * ANSI printing routines.
2168 */
887035a5 2169static void term_print_setup(Terminal *term)
b44b307a 2170{
887035a5 2171 bufchain_clear(&term->printer_buf);
64734920 2172 term->print_job = printer_start_job(term->cfg.printer);
b44b307a 2173}
887035a5 2174static void term_print_flush(Terminal *term)
b44b307a 2175{
2176 void *data;
2177 int len;
2178 int size;
887035a5 2179 while ((size = bufchain_size(&term->printer_buf)) > 5) {
2180 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 2181 if (len > size-5)
2182 len = size-5;
887035a5 2183 printer_job_data(term->print_job, data, len);
2184 bufchain_consume(&term->printer_buf, len);
b44b307a 2185 }
2186}
887035a5 2187static void term_print_finish(Terminal *term)
b44b307a 2188{
2189 void *data;
2190 int len, size;
2191 char c;
2192
887035a5 2193 if (!term->printing && !term->only_printing)
1709795f 2194 return; /* we need do nothing */
2195
887035a5 2196 term_print_flush(term);
2197 while ((size = bufchain_size(&term->printer_buf)) > 0) {
2198 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 2199 c = *(char *)data;
2200 if (c == '\033' || c == '\233') {
887035a5 2201 bufchain_consume(&term->printer_buf, size);
b44b307a 2202 break;
2203 } else {
887035a5 2204 printer_job_data(term->print_job, &c, 1);
2205 bufchain_consume(&term->printer_buf, 1);
b44b307a 2206 }
2207 }
887035a5 2208 printer_finish_job(term->print_job);
2209 term->print_job = NULL;
2210 term->printing = term->only_printing = FALSE;
b44b307a 2211}
2212
2213/*
374330e2 2214 * Remove everything currently in `inbuf' and stick it up on the
2215 * in-memory display. There's a big state machine in here to
2216 * process escape sequences...
2217 */
887035a5 2218void term_out(Terminal *term)
32874aea 2219{
36566009 2220 unsigned long c;
2221 int unget;
a748a096 2222 unsigned char localbuf[256], *chars;
2223 int nchars = 0;
2224
2225 unget = -1;
2226
1709795f 2227 chars = NULL; /* placate compiler warnings */
887035a5 2228 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
a748a096 2229 if (unget == -1) {
2230 if (nchars == 0) {
2231 void *ret;
887035a5 2232 bufchain_prefix(&term->inbuf, &ret, &nchars);
a748a096 2233 if (nchars > sizeof(localbuf))
2234 nchars = sizeof(localbuf);
2235 memcpy(localbuf, ret, nchars);
887035a5 2236 bufchain_consume(&term->inbuf, nchars);
a748a096 2237 chars = localbuf;
2238 assert(chars != NULL);
2239 }
2240 c = *chars++;
2241 nchars--;
c9def1b8 2242
a748a096 2243 /*
2244 * Optionally log the session traffic to a file. Useful for
2245 * debugging and possibly also useful for actual logging.
2246 */
64734920 2247 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
a8327734 2248 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
a748a096 2249 } else {
2250 c = unget;
2251 unget = -1;
5a16642f 2252 }
2253
e14a5a13 2254 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2255 * be able to display 8-bit characters, but I'll let that go 'cause
2256 * of i18n.
2257 */
4eeb7d09 2258
b44b307a 2259 /*
2260 * If we're printing, add the character to the printer
2261 * buffer.
2262 */
887035a5 2263 if (term->printing) {
2264 bufchain_add(&term->printer_buf, &c, 1);
b44b307a 2265
2266 /*
2267 * If we're in print-only mode, we use a much simpler
2268 * state machine designed only to recognise the ESC[4i
2269 * termination sequence.
2270 */
887035a5 2271 if (term->only_printing) {
b44b307a 2272 if (c == '\033')
887035a5 2273 term->print_state = 1;
b44b307a 2274 else if (c == (unsigned char)'\233')
887035a5 2275 term->print_state = 2;
2276 else if (c == '[' && term->print_state == 1)
2277 term->print_state = 2;
2278 else if (c == '4' && term->print_state == 2)
2279 term->print_state = 3;
2280 else if (c == 'i' && term->print_state == 3)
2281 term->print_state = 4;
b44b307a 2282 else
887035a5 2283 term->print_state = 0;
2284 if (term->print_state == 4) {
2285 term_print_finish(term);
b44b307a 2286 }
2287 continue;
2288 }
2289 }
2290
4eeb7d09 2291 /* First see about all those translations. */
887035a5 2292 if (term->termstate == TOPLEVEL) {
2293 if (in_utf(term))
2294 switch (term->utf_state) {
4eeb7d09 2295 case 0:
2296 if (c < 0x80) {
8f22582c 2297 /* UTF-8 must be stateless so we ignore iso2022. */
21d2b241 2298 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2299 c = term->ucsdata->unitab_ctrl[c];
36566009 2300 else c = ((unsigned char)c) | CSET_ASCII;
8f22582c 2301 break;
4eeb7d09 2302 } else if ((c & 0xe0) == 0xc0) {
887035a5 2303 term->utf_size = term->utf_state = 1;
2304 term->utf_char = (c & 0x1f);
4eeb7d09 2305 } else if ((c & 0xf0) == 0xe0) {
887035a5 2306 term->utf_size = term->utf_state = 2;
2307 term->utf_char = (c & 0x0f);
4eeb7d09 2308 } else if ((c & 0xf8) == 0xf0) {
887035a5 2309 term->utf_size = term->utf_state = 3;
2310 term->utf_char = (c & 0x07);
4eeb7d09 2311 } else if ((c & 0xfc) == 0xf8) {
887035a5 2312 term->utf_size = term->utf_state = 4;
2313 term->utf_char = (c & 0x03);
4eeb7d09 2314 } else if ((c & 0xfe) == 0xfc) {
887035a5 2315 term->utf_size = term->utf_state = 5;
2316 term->utf_char = (c & 0x01);
4eeb7d09 2317 } else {
2318 c = UCSERR;
2319 break;
2320 }
2321 continue;
2322 case 1:
2323 case 2:
2324 case 3:
2325 case 4:
2326 case 5:
2327 if ((c & 0xC0) != 0x80) {
a748a096 2328 unget = c;
5a16642f 2329 c = UCSERR;
887035a5 2330 term->utf_state = 0;
4eeb7d09 2331 break;
2332 }
887035a5 2333 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2334 if (--term->utf_state)
4eeb7d09 2335 continue;
2336
887035a5 2337 c = term->utf_char;
4eeb7d09 2338
2339 /* Is somebody trying to be evil! */
2340 if (c < 0x80 ||
887035a5 2341 (c < 0x800 && term->utf_size >= 2) ||
2342 (c < 0x10000 && term->utf_size >= 3) ||
2343 (c < 0x200000 && term->utf_size >= 4) ||
2344 (c < 0x4000000 && term->utf_size >= 5))
4eeb7d09 2345 c = UCSERR;
2346
2347 /* Unicode line separator and paragraph separator are CR-LF */
2348 if (c == 0x2028 || c == 0x2029)
2349 c = 0x85;
2350
2351 /* High controls are probably a Baaad idea too. */
2352 if (c < 0xA0)
2353 c = 0xFFFD;
2354
2355 /* The UTF-16 surrogates are not nice either. */
2356 /* The standard give the option of decoding these:
2357 * I don't want to! */
2358 if (c >= 0xD800 && c < 0xE000)
2359 c = UCSERR;
2360
2361 /* ISO 10646 characters now limited to UTF-16 range. */
2362 if (c > 0x10FFFF)
2363 c = UCSERR;
2364
2365 /* This is currently a TagPhobic application.. */
2366 if (c >= 0xE0000 && c <= 0xE007F)
2367 continue;
2368
2369 /* U+FEFF is best seen as a null. */
2370 if (c == 0xFEFF)
2371 continue;
2372 /* But U+FFFE is an error. */
2373 if (c == 0xFFFE || c == 0xFFFF)
2374 c = UCSERR;
2375
4eeb7d09 2376 break;
d3cb5465 2377 }
2378 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
887035a5 2379 else if(term->sco_acs &&
92e23ad9 2380 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
d3cb5465 2381 {
5dc6132d 2382 if (term->sco_acs == 2) c |= 0x80;
36566009 2383 c |= CSET_SCOACS;
4eeb7d09 2384 } else {
887035a5 2385 switch (term->cset_attr[term->cset]) {
4eeb7d09 2386 /*
2387 * Linedraw characters are different from 'ESC ( B'
2388 * only for a small range. For ones outside that
2389 * range, make sure we use the same font as well as
2390 * the same encoding.
2391 */
36566009 2392 case CSET_LINEDRW:
21d2b241 2393 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2394 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 2395 else
36566009 2396 c = ((unsigned char) c) | CSET_LINEDRW;
4eeb7d09 2397 break;
2398
36566009 2399 case CSET_GBCHR:
4eeb7d09 2400 /* If UK-ASCII, make the '#' a LineDraw Pound */
2401 if (c == '#') {
36566009 2402 c = '}' | CSET_LINEDRW;
4eeb7d09 2403 break;
2404 }
36566009 2405 /*FALLTHROUGH*/ case CSET_ASCII:
21d2b241 2406 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2407 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 2408 else
36566009 2409 c = ((unsigned char) c) | CSET_ASCII;
4eeb7d09 2410 break;
36566009 2411 case CSET_SCOACS:
2412 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
d3cb5465 2413 break;
4eeb7d09 2414 }
2415 }
2416 }
2417
2418 /* How about C1 controls ? */
887035a5 2419 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2420 !term->vt52_mode && has_compat(VT220)) {
2421 term->termstate = SEEN_ESC;
2422 term->esc_query = FALSE;
4eeb7d09 2423 c = '@' + (c & 0x1F);
2424 }
2425
2426 /* Or the GL control. */
887035a5 2427 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2428 if (term->curs.x && !term->wrapnext)
2429 term->curs.x--;
2430 term->wrapnext = FALSE;
36566009 2431 /* destructive backspace might be disabled */
2432 if (!term->cfg.no_dbackspace) {
c6958dfe 2433 copy_termchar(scrlineptr(term->curs.y),
2434 term->curs.x, &term->erase_char);
36566009 2435 }
4eeb7d09 2436 } else
2437 /* Or normal C0 controls. */
36566009 2438 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
374330e2 2439 switch (c) {
a263e4f6 2440 case '\005': /* ENQ: terminal type query */
e14a5a13 2441 /* Strictly speaking this is VT100 but a VT100 defaults to
2442 * no response. Other terminals respond at their option.
2443 *
2444 * Don't put a CR in the default string as this tends to
2445 * upset some weird software.
2446 *
2447 * An xterm returns "xterm" (5 characters)
2448 */
32874aea 2449 compatibility(ANSIMIN);
b9d7bcad 2450 if (term->ldisc) {
e7fbcdd8 2451 char abuf[256], *s, *d;
32874aea 2452 int state = 0;
64734920 2453 for (s = term->cfg.answerback, d = abuf; *s; s++) {
32874aea 2454 if (state) {
2455 if (*s >= 'a' && *s <= 'z')
2456 *d++ = (*s - ('a' - 1));
2457 else if ((*s >= '@' && *s <= '_') ||
2458 *s == '?' || (*s & 0x80))
2459 *d++ = ('@' ^ *s);
2460 else if (*s == '~')
2461 *d++ = '^';
2462 state = 0;
2463 } else if (*s == '^') {
2464 state = 1;
2465 } else
4eeb7d09 2466 *d++ = *s;
e7fbcdd8 2467 }
b9d7bcad 2468 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2469 abuf, d - abuf, 0);
e7fbcdd8 2470 }
374330e2 2471 break;
a263e4f6 2472 case '\007': /* BEL: Bell */
156686ef 2473 {
2474 struct beeptime *newbeep;
286c6b86 2475 unsigned long ticks;
156686ef 2476
f7f27309 2477 ticks = GETTICKCOUNT();
156686ef 2478
887035a5 2479 if (!term->beep_overloaded) {
3d88e64d 2480 newbeep = snew(struct beeptime);
156686ef 2481 newbeep->ticks = ticks;
2482 newbeep->next = NULL;
887035a5 2483 if (!term->beephead)
2484 term->beephead = newbeep;
156686ef 2485 else
887035a5 2486 term->beeptail->next = newbeep;
2487 term->beeptail = newbeep;
2488 term->nbeeps++;
156686ef 2489 }
2490
2491 /*
2492 * Throw out any beeps that happened more than
2493 * t seconds ago.
2494 */
887035a5 2495 while (term->beephead &&
64734920 2496 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
887035a5 2497 struct beeptime *tmp = term->beephead;
2498 term->beephead = tmp->next;
156686ef 2499 sfree(tmp);
887035a5 2500 if (!term->beephead)
2501 term->beeptail = NULL;
2502 term->nbeeps--;
156686ef 2503 }
2504
64734920 2505 if (term->cfg.bellovl && term->beep_overloaded &&
2506 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
156686ef 2507 /*
2508 * If we're currently overloaded and the
2509 * last beep was more than s seconds ago,
2510 * leave overload mode.
2511 */
887035a5 2512 term->beep_overloaded = FALSE;
64734920 2513 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2514 term->nbeeps >= term->cfg.bellovl_n) {
156686ef 2515 /*
2516 * Now, if we have n or more beeps
2517 * remaining in the queue, go into overload
2518 * mode.
2519 */
887035a5 2520 term->beep_overloaded = TRUE;
156686ef 2521 }
887035a5 2522 term->lastbeep = ticks;
156686ef 2523
2524 /*
2525 * Perform an actual beep if we're not overloaded.
2526 */
64734920 2527 if (!term->cfg.bellovl || !term->beep_overloaded) {
2528 beep(term->frontend, term->cfg.beep);
2529 if (term->cfg.beep == BELL_VISUAL) {
887035a5 2530 term->in_vbell = TRUE;
2531 term->vbell_startpoint = ticks;
2532 term_update(term);
0ce89525 2533 }
156686ef 2534 }
6b7f4cba 2535 term->seen_disp_event = TRUE;
156686ef 2536 }
374330e2 2537 break;
a263e4f6 2538 case '\b': /* BS: Back space */
887035a5 2539 if (term->curs.x == 0 &&
2540 (term->curs.y == 0 || term->wrap == 0))
2541 /* do nothing */ ;
2542 else if (term->curs.x == 0 && term->curs.y > 0)
2543 term->curs.x = term->cols - 1, term->curs.y--;
2544 else if (term->wrapnext)
2545 term->wrapnext = FALSE;
374330e2 2546 else
887035a5 2547 term->curs.x--;
887035a5 2548 term->seen_disp_event = TRUE;
374330e2 2549 break;
a263e4f6 2550 case '\016': /* LS1: Locking-shift one */
32874aea 2551 compatibility(VT100);
887035a5 2552 term->cset = 1;
374330e2 2553 break;
a263e4f6 2554 case '\017': /* LS0: Locking-shift zero */
32874aea 2555 compatibility(VT100);
887035a5 2556 term->cset = 0;
374330e2 2557 break;
a263e4f6 2558 case '\033': /* ESC: Escape */
887035a5 2559 if (term->vt52_mode)
2560 term->termstate = VT52_ESC;
e14a5a13 2561 else {
2562 compatibility(ANSIMIN);
887035a5 2563 term->termstate = SEEN_ESC;
2564 term->esc_query = FALSE;
e14a5a13 2565 }
374330e2 2566 break;
a263e4f6 2567 case '\015': /* CR: Carriage return */
887035a5 2568 term->curs.x = 0;
2569 term->wrapnext = FALSE;
887035a5 2570 term->seen_disp_event = TRUE;
2571 term->paste_hold = 0;
a8327734 2572 if (term->logctx)
2573 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 2574 break;
a263e4f6 2575 case '\014': /* FF: Form feed */
1a837633 2576 if (has_compat(SCOANSI)) {
887035a5 2577 move(term, 0, 0, 0);
2578 erase_lots(term, FALSE, FALSE, TRUE);
2579 term->disptop = 0;
2580 term->wrapnext = FALSE;
2581 term->seen_disp_event = 1;
1a837633 2582 break;
2583 }
a263e4f6 2584 case '\013': /* VT: Line tabulation */
32874aea 2585 compatibility(VT100);
a263e4f6 2586 case '\012': /* LF: Line feed */
887035a5 2587 if (term->curs.y == term->marg_b)
2588 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2589 else if (term->curs.y < term->rows - 1)
2590 term->curs.y++;
64734920 2591 if (term->cfg.lfhascr)
887035a5 2592 term->curs.x = 0;
887035a5 2593 term->wrapnext = FALSE;
2594 term->seen_disp_event = 1;
2595 term->paste_hold = 0;
a8327734 2596 if (term->logctx)
2597 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 2598 break;
a263e4f6 2599 case '\t': /* HT: Character tabulation */
374330e2 2600 {
887035a5 2601 pos old_curs = term->curs;
36566009 2602 termline *ldata = scrlineptr(term->curs.y);
c9def1b8 2603
2604 do {
887035a5 2605 term->curs.x++;
2606 } while (term->curs.x < term->cols - 1 &&
2607 !term->tabs[term->curs.x]);
c9def1b8 2608
36566009 2609 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
887035a5 2610 if (term->curs.x >= term->cols / 2)
2611 term->curs.x = term->cols / 2 - 1;
32874aea 2612 } else {
887035a5 2613 if (term->curs.x >= term->cols)
2614 term->curs.x = term->cols - 1;
c9def1b8 2615 }
2616
887035a5 2617 check_selection(term, old_curs, term->curs);
374330e2 2618 }
887035a5 2619 term->seen_disp_event = TRUE;
374330e2 2620 break;
cabfd08c 2621 }
32874aea 2622 } else
887035a5 2623 switch (term->termstate) {
32874aea 2624 case TOPLEVEL:
887035a5 2625 /* Only graphic characters get this far;
2626 * ctrls are stripped above */
4eeb7d09 2627 {
c6958dfe 2628 termline *cline = scrlineptr(term->curs.y);
4eeb7d09 2629 int width = 0;
2630 if (DIRECT_CHAR(c))
2631 width = 1;
2632 if (!width)
2633 width = wcwidth((wchar_t) c);
c6958dfe 2634
2635 if (term->wrapnext && term->wrap && width > 0) {
2636 cline->lattr |= LATTR_WRAPPED;
2637 if (term->curs.y == term->marg_b)
2638 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2639 else if (term->curs.y < term->rows - 1)
2640 term->curs.y++;
2641 term->curs.x = 0;
2642 term->wrapnext = FALSE;
1c10e849 2643 cline = scrlineptr(term->curs.y);
c6958dfe 2644 }
2645 if (term->insert && width > 0)
2646 insch(term, width);
2647 if (term->selstate != NO_SELECTION) {
2648 pos cursplus = term->curs;
2649 incpos(cursplus);
2650 check_selection(term, term->curs, cursplus);
2651 }
2652 if (((c & CSET_MASK) == CSET_ASCII ||
2653 (c & CSET_MASK) == 0) &&
2654 term->logctx)
2655 logtraffic(term->logctx, (unsigned char) c,
2656 LGTYP_ASCII);
2657
4eeb7d09 2658 switch (width) {
2659 case 2:
3c7366f8 2660 /*
2661 * If we're about to display a double-width
2662 * character starting in the rightmost
2663 * column, then we do something special
2664 * instead. We must print a space in the
2665 * last column of the screen, then wrap;
2666 * and we also set LATTR_WRAPPED2 which
2667 * instructs subsequent cut-and-pasting not
2668 * only to splice this line to the one
2669 * after it, but to ignore the space in the
2670 * last character position as well.
2671 * (Because what was actually output to the
2672 * terminal was presumably just a sequence
2673 * of CJK characters, and we don't want a
2674 * space to be pasted in the middle of
2675 * those just because they had the
2676 * misfortune to start in the wrong parity
2677 * column. xterm concurs.)
2678 */
2679 check_boundary(term, term->curs.x, term->curs.y);
2680 check_boundary(term, term->curs.x+2, term->curs.y);
2681 if (term->curs.x == term->cols-1) {
c6958dfe 2682 copy_termchar(cline, term->curs.x,
2683 &term->erase_char);
2684 cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;
887035a5 2685 if (term->curs.y == term->marg_b)
2686 scroll(term, term->marg_t, term->marg_b,
2687 1, TRUE);
2688 else if (term->curs.y < term->rows - 1)
2689 term->curs.y++;
2690 term->curs.x = 0;
3c7366f8 2691 /* Now we must check_boundary again, of course. */
2692 check_boundary(term, term->curs.x, term->curs.y);
2693 check_boundary(term, term->curs.x+2, term->curs.y);
4eeb7d09 2694 }
c6958dfe 2695
2696 /* FULL-TERMCHAR */
2697 clear_cc(cline, term->curs.x);
2698 cline->chars[term->curs.x].chr = c;
2699 cline->chars[term->curs.x].attr = term->curr_attr;
2700
3c7366f8 2701 term->curs.x++;
c6958dfe 2702
2703 /* FULL-TERMCHAR */
2704 clear_cc(cline, term->curs.x);
2705 cline->chars[term->curs.x].chr = UCSWIDE;
2706 cline->chars[term->curs.x].attr = term->curr_attr;
2707
5a73255e 2708 break;
4eeb7d09 2709 case 1:
3c7366f8 2710 check_boundary(term, term->curs.x, term->curs.y);
2711 check_boundary(term, term->curs.x+1, term->curs.y);
c6958dfe 2712
2713 /* FULL-TERMCHAR */
2714 clear_cc(cline, term->curs.x);
2715 cline->chars[term->curs.x].chr = c;
2716 cline->chars[term->curs.x].attr = term->curr_attr;
2717
32874aea 2718 break;
c6958dfe 2719 case 0:
f00630be 2720 if (term->curs.x > 0) {
2721 int x = term->curs.x - 1;
2722
2723 /* If we're in wrapnext state, the character
2724 * to combine with is _here_, not to our left. */
2725 if (term->wrapnext)
2726 x++;
2727
2728 /*
2729 * If the previous character is
2730 * UCSWIDE, back up another one.
2731 */
2732 if (cline->chars[x].chr == UCSWIDE) {
2733 assert(x > 0);
2734 x--;
2735 }
2736
2737 add_cc(cline, x, c);
2738 term->seen_disp_event = 1;
2739 }
c6958dfe 2740 continue;
4eeb7d09 2741 default:
2742 continue;
32874aea 2743 }
c6958dfe 2744 term->curs.x++;
2745 if (term->curs.x == term->cols) {
2746 term->curs.x--;
2747 term->wrapnext = TRUE;
2748 if (term->wrap && term->vt52_mode) {
2749 cline->lattr |= LATTR_WRAPPED;
2750 if (term->curs.y == term->marg_b)
2751 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2752 else if (term->curs.y < term->rows - 1)
2753 term->curs.y++;
2754 term->curs.x = 0;
2755 term->wrapnext = FALSE;
2756 }
4eeb7d09 2757 }
c6958dfe 2758 term->seen_disp_event = 1;
e14a5a13 2759 }
374330e2 2760 break;
32874aea 2761
32874aea 2762 case OSC_MAYBE_ST:
2763 /*
2764 * This state is virtually identical to SEEN_ESC, with the
2765 * exception that we have an OSC sequence in the pipeline,
2766 * and _if_ we see a backslash, we process it.
2767 */
2768 if (c == '\\') {
887035a5 2769 do_osc(term);
2770 term->termstate = TOPLEVEL;
32874aea 2771 break;
e14a5a13 2772 }
32874aea 2773 /* else fall through */
2774 case SEEN_ESC:
4eeb7d09 2775 if (c >= ' ' && c <= '/') {
887035a5 2776 if (term->esc_query)
2777 term->esc_query = -1;
4eeb7d09 2778 else
887035a5 2779 term->esc_query = c;
32874aea 2780 break;
4eeb7d09 2781 }
887035a5 2782 term->termstate = TOPLEVEL;
2783 switch (ANSI(c, term->esc_query)) {
a263e4f6 2784 case '[': /* enter CSI mode */
887035a5 2785 term->termstate = SEEN_CSI;
2786 term->esc_nargs = 1;
2787 term->esc_args[0] = ARG_DEFAULT;
2788 term->esc_query = FALSE;
32874aea 2789 break;
a263e4f6 2790 case ']': /* OSC: xterm escape sequences */
32874aea 2791 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2792 compatibility(OTHER);
887035a5 2793 term->termstate = SEEN_OSC;
2794 term->esc_args[0] = 0;
32874aea 2795 break;
a263e4f6 2796 case '7': /* DECSC: save cursor */
32874aea 2797 compatibility(VT100);
887035a5 2798 save_cursor(term, TRUE);
32874aea 2799 break;
a263e4f6 2800 case '8': /* DECRC: restore cursor */
32874aea 2801 compatibility(VT100);
887035a5 2802 save_cursor(term, FALSE);
2803 term->seen_disp_event = TRUE;
32874aea 2804 break;
a263e4f6 2805 case '=': /* DECKPAM: Keypad application mode */
32874aea 2806 compatibility(VT100);
887035a5 2807 term->app_keypad_keys = TRUE;
32874aea 2808 break;
a263e4f6 2809 case '>': /* DECKPNM: Keypad numeric mode */
32874aea 2810 compatibility(VT100);
887035a5 2811 term->app_keypad_keys = FALSE;
32874aea 2812 break;
a263e4f6 2813 case 'D': /* IND: exactly equivalent to LF */
32874aea 2814 compatibility(VT100);
887035a5 2815 if (term->curs.y == term->marg_b)
2816 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2817 else if (term->curs.y < term->rows - 1)
2818 term->curs.y++;
887035a5 2819 term->wrapnext = FALSE;
2820 term->seen_disp_event = TRUE;
32874aea 2821 break;
a263e4f6 2822 case 'E': /* NEL: exactly equivalent to CR-LF */
32874aea 2823 compatibility(VT100);
887035a5 2824 term->curs.x = 0;
2825 if (term->curs.y == term->marg_b)
2826 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2827 else if (term->curs.y < term->rows - 1)
2828 term->curs.y++;
887035a5 2829 term->wrapnext = FALSE;
2830 term->seen_disp_event = TRUE;
32874aea 2831 break;
a263e4f6 2832 case 'M': /* RI: reverse index - backwards LF */
32874aea 2833 compatibility(VT100);
887035a5 2834 if (term->curs.y == term->marg_t)
2835 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2836 else if (term->curs.y > 0)
2837 term->curs.y--;
887035a5 2838 term->wrapnext = FALSE;
2839 term->seen_disp_event = TRUE;
32874aea 2840 break;
a263e4f6 2841 case 'Z': /* DECID: terminal type query */
32874aea 2842 compatibility(VT100);
b9d7bcad 2843 if (term->ldisc)
2844 ldisc_send(term->ldisc, term->id_string,
2845 strlen(term->id_string), 0);
32874aea 2846 break;
a263e4f6 2847 case 'c': /* RIS: restore power-on settings */
32874aea 2848 compatibility(VT100);
887035a5 2849 power_on(term);
b9d7bcad 2850 if (term->ldisc) /* cause ldisc to notice changes */
2851 ldisc_send(term->ldisc, NULL, 0, 0);
887035a5 2852 if (term->reset_132) {
64734920 2853 if (!term->cfg.no_remote_resize)
a8327734 2854 request_resize(term->frontend, 80, term->rows);
887035a5 2855 term->reset_132 = 0;
374330e2 2856 }
887035a5 2857 term->disptop = 0;
2858 term->seen_disp_event = TRUE;
32874aea 2859 break;
a263e4f6 2860 case 'H': /* HTS: set a tab */
32874aea 2861 compatibility(VT100);
887035a5 2862 term->tabs[term->curs.x] = TRUE;
32874aea 2863 break;
4eeb7d09 2864
a263e4f6 2865 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
4eeb7d09 2866 compatibility(VT100);
2867 {
36566009 2868 termline *ldata;
4eeb7d09 2869 int i, j;
2870 pos scrtop, scrbot;
2871
887035a5 2872 for (i = 0; i < term->rows; i++) {
36566009 2873 ldata = scrlineptr(i);
2874 for (j = 0; j < term->cols; j++) {
c6958dfe 2875 copy_termchar(ldata, j,
2876 &term->basic_erase_char);
36566009 2877 ldata->chars[j].chr = 'E';
2878 }
2879 ldata->lattr = LATTR_NORM;
4eeb7d09 2880 }
887035a5 2881 term->disptop = 0;
2882 term->seen_disp_event = TRUE;
4eeb7d09 2883 scrtop.x = scrtop.y = 0;
2884 scrbot.x = 0;
887035a5 2885 scrbot.y = term->rows;
2886 check_selection(term, scrtop, scrbot);
4eeb7d09 2887 }
2888 break;
2889
2890 case ANSI('3', '#'):
2891 case ANSI('4', '#'):
2892 case ANSI('5', '#'):
2893 case ANSI('6', '#'):
2894 compatibility(VT100);
2895 {
36566009 2896 int nlattr;
2897
887035a5 2898 switch (ANSI(c, term->esc_query)) {
a263e4f6 2899 case ANSI('3', '#'): /* DECDHL: 2*height, top */
4eeb7d09 2900 nlattr = LATTR_TOP;
2901 break;
a263e4f6 2902 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
4eeb7d09 2903 nlattr = LATTR_BOT;
2904 break;
a263e4f6 2905 case ANSI('5', '#'): /* DECSWL: normal */
4eeb7d09 2906 nlattr = LATTR_NORM;
2907 break;
a263e4f6 2908 default: /* case ANSI('6', '#'): DECDWL: 2*width */
4eeb7d09 2909 nlattr = LATTR_WIDE;
2910 break;
2911 }
36566009 2912 scrlineptr(term->curs.y)->lattr = nlattr;
4eeb7d09 2913 }
2914 break;
a263e4f6 2915 /* GZD4: G0 designate 94-set */
4eeb7d09 2916 case ANSI('A', '('):
2917 compatibility(VT100);
64734920 2918 if (!term->cfg.no_remote_charset)
36566009 2919 term->cset_attr[0] = CSET_GBCHR;
4eeb7d09 2920 break;
2921 case ANSI('B', '('):
2922 compatibility(VT100);
64734920 2923 if (!term->cfg.no_remote_charset)
36566009 2924 term->cset_attr[0] = CSET_ASCII;
4eeb7d09 2925 break;
2926 case ANSI('0', '('):
2927 compatibility(VT100);
64734920 2928 if (!term->cfg.no_remote_charset)
36566009 2929 term->cset_attr[0] = CSET_LINEDRW;
4eeb7d09 2930 break;
d3cb5465 2931 case ANSI('U', '('):
2932 compatibility(OTHER);
64734920 2933 if (!term->cfg.no_remote_charset)
36566009 2934 term->cset_attr[0] = CSET_SCOACS;
d3cb5465 2935 break;
a263e4f6 2936 /* G1D4: G1-designate 94-set */
4eeb7d09 2937 case ANSI('A', ')'):
2938 compatibility(VT100);
64734920 2939 if (!term->cfg.no_remote_charset)
36566009 2940 term->cset_attr[1] = CSET_GBCHR;
4eeb7d09 2941 break;
2942 case ANSI('B', ')'):
2943 compatibility(VT100);
64734920 2944 if (!term->cfg.no_remote_charset)
36566009 2945 term->cset_attr[1] = CSET_ASCII;
4eeb7d09 2946 break;
2947 case ANSI('0', ')'):
2948 compatibility(VT100);
64734920 2949 if (!term->cfg.no_remote_charset)
36566009 2950 term->cset_attr[1] = CSET_LINEDRW;
4eeb7d09 2951 break;
d3cb5465 2952 case ANSI('U', ')'):
2953 compatibility(OTHER);
64734920 2954 if (!term->cfg.no_remote_charset)
36566009 2955 term->cset_attr[1] = CSET_SCOACS;
d3cb5465 2956 break;
a263e4f6 2957 /* DOCS: Designate other coding system */
4eeb7d09 2958 case ANSI('8', '%'): /* Old Linux code */
2959 case ANSI('G', '%'):
2960 compatibility(OTHER);
64734920 2961 if (!term->cfg.no_remote_charset)
887035a5 2962 term->utf = 1;
4eeb7d09 2963 break;
2964 case ANSI('@', '%'):
2965 compatibility(OTHER);
64734920 2966 if (!term->cfg.no_remote_charset)
887035a5 2967 term->utf = 0;
4eeb7d09 2968 break;
374330e2 2969 }
2970 break;
32874aea 2971 case SEEN_CSI:
887035a5 2972 term->termstate = TOPLEVEL; /* default */
32874aea 2973 if (isdigit(c)) {
887035a5 2974 if (term->esc_nargs <= ARGS_MAX) {
2975 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2976 term->esc_args[term->esc_nargs - 1] = 0;
2977 term->esc_args[term->esc_nargs - 1] =
2978 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
32874aea 2979 }
887035a5 2980 term->termstate = SEEN_CSI;
32874aea 2981 } else if (c == ';') {
887035a5 2982 if (++term->esc_nargs <= ARGS_MAX)
2983 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2984 term->termstate = SEEN_CSI;
32874aea 2985 } else if (c < '@') {
887035a5 2986 if (term->esc_query)
2987 term->esc_query = -1;
32874aea 2988 else if (c == '?')
887035a5 2989 term->esc_query = TRUE;
32874aea 2990 else
887035a5 2991 term->esc_query = c;
2992 term->termstate = SEEN_CSI;
32874aea 2993 } else
887035a5 2994 switch (ANSI(c, term->esc_query)) {
a263e4f6 2995 case 'A': /* CUU: move up N lines */
887035a5 2996 move(term, term->curs.x,
2997 term->curs.y - def(term->esc_args[0], 1), 1);
2998 term->seen_disp_event = TRUE;
32874aea 2999 break;
a263e4f6 3000 case 'e': /* VPR: move down N lines */
32874aea 3001 compatibility(ANSI);
5442e92f 3002 /* FALLTHROUGH */
a263e4f6 3003 case 'B': /* CUD: Cursor down */
887035a5 3004 move(term, term->curs.x,
3005 term->curs.y + def(term->esc_args[0], 1), 1);
3006 term->seen_disp_event = TRUE;
32874aea 3007 break;
a263e4f6 3008 case ANSI('c', '>'): /* DA: report xterm version */
32874aea 3009 compatibility(OTHER);
3010 /* this reports xterm version 136 so that VIM can
3011 use the drag messages from the mouse reporting */
b9d7bcad 3012 if (term->ldisc)
3013 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
32874aea 3014 break;
a263e4f6 3015 case 'a': /* HPR: move right N cols */
5442e92f 3016 compatibility(ANSI);
3017 /* FALLTHROUGH */
a263e4f6 3018 case 'C': /* CUF: Cursor right */
887035a5 3019 move(term, term->curs.x + def(term->esc_args[0], 1),
3020 term->curs.y, 1);
3021 term->seen_disp_event = TRUE;
32874aea 3022 break;
a263e4f6 3023 case 'D': /* CUB: move left N cols */
887035a5 3024 move(term, term->curs.x - def(term->esc_args[0], 1),
3025 term->curs.y, 1);
3026 term->seen_disp_event = TRUE;
32874aea 3027 break;
a263e4f6 3028 case 'E': /* CNL: move down N lines and CR */
32874aea 3029 compatibility(ANSI);
887035a5 3030 move(term, 0,
3031 term->curs.y + def(term->esc_args[0], 1), 1);
3032 term->seen_disp_event = TRUE;
32874aea 3033 break;
a263e4f6 3034 case 'F': /* CPL: move up N lines and CR */
32874aea 3035 compatibility(ANSI);
887035a5 3036 move(term, 0,
3037 term->curs.y - def(term->esc_args[0], 1), 1);
3038 term->seen_disp_event = TRUE;
32874aea 3039 break;
a263e4f6 3040 case 'G': /* CHA */
3041 case '`': /* HPA: set horizontal posn */
32874aea 3042 compatibility(ANSI);
887035a5 3043 move(term, def(term->esc_args[0], 1) - 1,
3044 term->curs.y, 0);
3045 term->seen_disp_event = TRUE;
32874aea 3046 break;
a263e4f6 3047 case 'd': /* VPA: set vertical posn */
32874aea 3048 compatibility(ANSI);
887035a5 3049 move(term, term->curs.x,
3050 ((term->dec_om ? term->marg_t : 0) +
3051 def(term->esc_args[0], 1) - 1),
3052 (term->dec_om ? 2 : 0));
3053 term->seen_disp_event = TRUE;
32874aea 3054 break;
a263e4f6 3055 case 'H': /* CUP */
3056 case 'f': /* HVP: set horz and vert posns at once */
887035a5 3057 if (term->esc_nargs < 2)
3058 term->esc_args[1] = ARG_DEFAULT;
3059 move(term, def(term->esc_args[1], 1) - 1,
3060 ((term->dec_om ? term->marg_t : 0) +
3061 def(term->esc_args[0], 1) - 1),
3062 (term->dec_om ? 2 : 0));
3063 term->seen_disp_event = TRUE;
32874aea 3064 break;
a263e4f6 3065 case 'J': /* ED: erase screen or parts of it */
32874aea 3066 {
887035a5 3067 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 3068 if (i > 3)
3069 i = 0;
887035a5 3070 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
32874aea 3071 }
887035a5 3072 term->disptop = 0;
3073 term->seen_disp_event = TRUE;
32874aea 3074 break;
a263e4f6 3075 case 'K': /* EL: erase line or parts of it */
32874aea 3076 {
887035a5 3077 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 3078 if (i > 3)
3079 i = 0;
887035a5 3080 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
32874aea 3081 }
887035a5 3082 term->seen_disp_event = TRUE;
32874aea 3083 break;
a263e4f6 3084 case 'L': /* IL: insert lines */
32874aea 3085 compatibility(VT102);
887035a5 3086 if (term->curs.y <= term->marg_b)
3087 scroll(term, term->curs.y, term->marg_b,
3088 -def(term->esc_args[0], 1), FALSE);
887035a5 3089 term->seen_disp_event = TRUE;
32874aea 3090 break;
a263e4f6 3091 case 'M': /* DL: delete lines */
32874aea 3092 compatibility(VT102);
887035a5 3093 if (term->curs.y <= term->marg_b)
3094 scroll(term, term->curs.y, term->marg_b,
3095 def(term->esc_args[0], 1),
32874aea 3096 TRUE);
887035a5 3097 term->seen_disp_event = TRUE;
32874aea 3098 break;
a263e4f6 3099 case '@': /* ICH: insert chars */
32874aea 3100 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3101 compatibility(VT102);
887035a5 3102 insch(term, def(term->esc_args[0], 1));
3103 term->seen_disp_event = TRUE;
32874aea 3104 break;
a263e4f6 3105 case 'P': /* DCH: delete chars */
32874aea 3106 compatibility(VT102);
887035a5 3107 insch(term, -def(term->esc_args[0], 1));
3108 term->seen_disp_event = TRUE;
32874aea 3109 break;
a263e4f6 3110 case 'c': /* DA: terminal type query */
32874aea 3111 compatibility(VT100);
3112 /* This is the response for a VT102 */
b9d7bcad 3113 if (term->ldisc)
3114 ldisc_send(term->ldisc, term->id_string,
3115 strlen(term->id_string), 0);
32874aea 3116 break;
a263e4f6 3117 case 'n': /* DSR: cursor position query */
b9d7bcad 3118 if (term->ldisc) {
3119 if (term->esc_args[0] == 6) {
3120 char buf[32];
3121 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
3122 term->curs.x + 1);
3123 ldisc_send(term->ldisc, buf, strlen(buf), 0);
3124 } else if (term->esc_args[0] == 5) {
3125 ldisc_send(term->ldisc, "\033[0n", 4, 0);
3126 }
32874aea 3127 }
3128 break;
a263e4f6 3129 case 'h': /* SM: toggle modes to high */
32874aea 3130 case ANSI_QUE('h'):
3131 compatibility(VT100);
3132 {
3133 int i;
887035a5 3134 for (i = 0; i < term->esc_nargs; i++)
3135 toggle_mode(term, term->esc_args[i],
3136 term->esc_query, TRUE);
32874aea 3137 }
3138 break;
a263e4f6 3139 case 'i': /* MC: Media copy */
b44b307a 3140 case ANSI_QUE('i'):
3141 compatibility(VT100);
3142 {
887035a5 3143 if (term->esc_nargs != 1) break;
64734920 3144 if (term->esc_args[0] == 5 && *term->cfg.printer) {
887035a5 3145 term->printing = TRUE;
3146 term->only_printing = !term->esc_query;
3147 term->print_state = 0;
3148 term_print_setup(term);
3149 } else if (term->esc_args[0] == 4 &&
3150 term->printing) {
3151 term_print_finish(term);
b44b307a 3152 }
3153 }
3154 break;
a263e4f6 3155 case 'l': /* RM: toggle modes to low */
32874aea 3156 case ANSI_QUE('l'):
3157 compatibility(VT100);
3158 {
3159 int i;
887035a5 3160 for (i = 0; i < term->esc_nargs; i++)
3161 toggle_mode(term, term->esc_args[i],
3162 term->esc_query, FALSE);
32874aea 3163 }
3164 break;
a263e4f6 3165 case 'g': /* TBC: clear tabs */
32874aea 3166 compatibility(VT100);
887035a5 3167 if (term->esc_nargs == 1) {
3168 if (term->esc_args[0] == 0) {
3169 term->tabs[term->curs.x] = FALSE;
3170 } else if (term->esc_args[0] == 3) {
32874aea 3171 int i;
887035a5 3172 for (i = 0; i < term->cols; i++)
3173 term->tabs[i] = FALSE;
32874aea 3174 }
3175 }
3176 break;
a263e4f6 3177 case 'r': /* DECSTBM: set scroll margins */
32874aea 3178 compatibility(VT100);
887035a5 3179 if (term->esc_nargs <= 2) {
32874aea 3180 int top, bot;
887035a5 3181 top = def(term->esc_args[0], 1) - 1;
3182 bot = (term->esc_nargs <= 1
3183 || term->esc_args[1] == 0 ?
3184 term->rows :
3185 def(term->esc_args[1], term->rows)) - 1;
3186 if (bot >= term->rows)
3187 bot = term->rows - 1;
32874aea 3188 /* VTTEST Bug 9 - if region is less than 2 lines
3189 * don't change region.
3190 */
3191 if (bot - top > 0) {
887035a5 3192 term->marg_t = top;
3193 term->marg_b = bot;
3194 term->curs.x = 0;
32874aea 3195 /*
3196 * I used to think the cursor should be
3197 * placed at the top of the newly marginned
3198 * area. Apparently not: VMS TPU falls over
3199 * if so.
3200 *
887035a5 3201 * Well actually it should for
3202 * Origin mode - RDB
32874aea 3203 */
887035a5 3204 term->curs.y = (term->dec_om ?
3205 term->marg_t : 0);
887035a5 3206 term->seen_disp_event = TRUE;
32874aea 3207 }
3208 }
3209 break;
a263e4f6 3210 case 'm': /* SGR: set graphics rendition */
32874aea 3211 {
3212 /*
887035a5 3213 * A VT100 without the AVO only had one
3214 * attribute, either underline or
3215 * reverse video depending on the
3216 * cursor type, this was selected by
3217 * CSI 7m.
32874aea 3218 *
3219 * case 2:
887035a5 3220 * This is sometimes DIM, eg on the
3221 * GIGI and Linux
32874aea 3222 * case 8:
4eeb7d09 3223 * This is sometimes INVIS various ANSI.
32874aea 3224 * case 21:
3225 * This like 22 disables BOLD, DIM and INVIS
3226 *
887035a5 3227 * The ANSI colours appear on any
3228 * terminal that has colour (obviously)
3229 * but the interaction between sgr0 and
3230 * the colours varies but is usually
3231 * related to the background colour
3232 * erase item. The interaction between
3233 * colour attributes and the mono ones
3234 * is also very implementation
3235 * dependent.
32874aea 3236 *
887035a5 3237 * The 39 and 49 attributes are likely
3238 * to be unimplemented.
32874aea 3239 */
3240 int i;
887035a5 3241 for (i = 0; i < term->esc_nargs; i++) {
3242 switch (def(term->esc_args[i], 0)) {
32874aea 3243 case 0: /* restore defaults */
69761fd5 3244 term->curr_attr = term->default_attr;
32874aea 3245 break;
3246 case 1: /* enable bold */
3247 compatibility(VT100AVO);
887035a5 3248 term->curr_attr |= ATTR_BOLD;
32874aea 3249 break;
3250 case 21: /* (enable double underline) */
3251 compatibility(OTHER);
3252 case 4: /* enable underline */
3253 compatibility(VT100AVO);
887035a5 3254 term->curr_attr |= ATTR_UNDER;
32874aea 3255 break;
3256 case 5: /* enable blink */
3257 compatibility(VT100AVO);
887035a5 3258 term->curr_attr |= ATTR_BLINK;
32874aea 3259 break;
1b20b972 3260 case 6: /* SCO light bkgrd */
3261 compatibility(SCOANSI);
3262 term->blink_is_real = FALSE;
3263 term->curr_attr |= ATTR_BLINK;
3264 break;
32874aea 3265 case 7: /* enable reverse video */
887035a5 3266 term->curr_attr |= ATTR_REVERSE;
32874aea 3267 break;
d3cb5465 3268 case 10: /* SCO acs off */
3269 compatibility(SCOANSI);
64734920 3270 if (term->cfg.no_remote_charset) break;
887035a5 3271 term->sco_acs = 0; break;
d3cb5465 3272 case 11: /* SCO acs on */
3273 compatibility(SCOANSI);
64734920 3274 if (term->cfg.no_remote_charset) break;
887035a5 3275 term->sco_acs = 1; break;
5dc6132d 3276 case 12: /* SCO acs on, |0x80 */
d3cb5465 3277 compatibility(SCOANSI);
64734920 3278 if (term->cfg.no_remote_charset) break;
887035a5 3279 term->sco_acs = 2; break;
32874aea 3280 case 22: /* disable bold */
3281 compatibility2(OTHER, VT220);
887035a5 3282 term->curr_attr &= ~ATTR_BOLD;
32874aea 3283 break;
3284 case 24: /* disable underline */
3285 compatibility2(OTHER, VT220);
887035a5 3286 term->curr_attr &= ~ATTR_UNDER;
32874aea 3287 break;
3288 case 25: /* disable blink */
3289 compatibility2(OTHER, VT220);
887035a5 3290 term->curr_attr &= ~ATTR_BLINK;
32874aea 3291 break;
3292 case 27: /* disable reverse video */
3293 compatibility2(OTHER, VT220);
887035a5 3294 term->curr_attr &= ~ATTR_REVERSE;
32874aea 3295 break;
3296 case 30:
3297 case 31:
3298 case 32:
3299 case 33:
3300 case 34:
3301 case 35:
3302 case 36:
3303 case 37:
3304 /* foreground */
887035a5 3305 term->curr_attr &= ~ATTR_FGMASK;
3306 term->curr_attr |=
3307 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
32874aea 3308 break;
37ca32ed 3309 case 90:
3310 case 91:
3311 case 92:
3312 case 93:
3313 case 94:
3314 case 95:
3315 case 96:
3316 case 97:
3317 /* xterm-style bright foreground */
3318 term->curr_attr &= ~ATTR_FGMASK;
3319 term->curr_attr |=
3320 ((term->esc_args[i] - 90 + 16)
3321 << ATTR_FGSHIFT);
3322 break;
32874aea 3323 case 39: /* default-foreground */
887035a5 3324 term->curr_attr &= ~ATTR_FGMASK;
3325 term->curr_attr |= ATTR_DEFFG;
32874aea 3326 break;
3327 case 40:
3328 case 41:
3329 case 42:
3330 case 43:
3331 case 44:
3332 case 45:
3333 case 46:
3334 case 47:
3335 /* background */
887035a5 3336 term->curr_attr &= ~ATTR_BGMASK;
3337 term->curr_attr |=
3338 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
32874aea 3339 break;
37ca32ed 3340 case 100:
3341 case 101:
3342 case 102:
3343 case 103:
3344 case 104:
3345 case 105:
3346 case 106:
3347 case 107:
3348 /* xterm-style bright background */
3349 term->curr_attr &= ~ATTR_BGMASK;
3350 term->curr_attr |=
3351 ((term->esc_args[i] - 100 + 16)
3352 << ATTR_BGSHIFT);
3353 break;
32874aea 3354 case 49: /* default-background */
887035a5 3355 term->curr_attr &= ~ATTR_BGMASK;
3356 term->curr_attr |= ATTR_DEFBG;
32874aea 3357 break;
3358 }
3359 }
36566009 3360 set_erase_char(term);
32874aea 3361 }
3362 break;
3363 case 's': /* save cursor */
887035a5 3364 save_cursor(term, TRUE);
32874aea 3365 break;
3366 case 'u': /* restore cursor */
887035a5 3367 save_cursor(term, FALSE);
3368 term->seen_disp_event = TRUE;
32874aea 3369 break;
a263e4f6 3370 case 't': /* DECSLPP: set page size - ie window height */
374330e2 3371 /*
32874aea 3372 * VT340/VT420 sequence DECSLPP, DEC only allows values
3373 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3374 * illegal values (eg first arg 1..9) for window changing
3375 * and reports.
3376 */
887035a5 3377 if (term->esc_nargs <= 1
3378 && (term->esc_args[0] < 1 ||
3379 term->esc_args[0] >= 24)) {
68f9b3d9 3380 compatibility(VT340TEXT);
64734920 3381 if (!term->cfg.no_remote_resize)
a8327734 3382 request_resize(term->frontend, term->cols,
887035a5 3383 def(term->esc_args[0], 24));
3384 deselect(term);
3385 } else if (term->esc_nargs >= 1 &&
3386 term->esc_args[0] >= 1 &&
3387 term->esc_args[0] < 24) {
68f9b3d9 3388 compatibility(OTHER);
3389
887035a5 3390 switch (term->esc_args[0]) {
68f9b3d9 3391 int x, y, len;
3392 char buf[80], *p;
3393 case 1:
a8327734 3394 set_iconic(term->frontend, FALSE);
68f9b3d9 3395 break;
3396 case 2:
a8327734 3397 set_iconic(term->frontend, TRUE);
68f9b3d9 3398 break;
3399 case 3:
887035a5 3400 if (term->esc_nargs >= 3) {
64734920 3401 if (!term->cfg.no_remote_resize)
a8327734 3402 move_window(term->frontend,
3403 def(term->esc_args[1], 0),
887035a5 3404 def(term->esc_args[2], 0));
68f9b3d9 3405 }
3406 break;
3407 case 4:
3408 /* We should resize the window to a given
3409 * size in pixels here, but currently our
3410 * resizing code isn't healthy enough to
3411 * manage it. */
3412 break;
3413 case 5:
a8327734 3414 /* move to top */
3415 set_zorder(term->frontend, TRUE);
68f9b3d9 3416 break;
3417 case 6:
a8327734 3418 /* move to bottom */
3419 set_zorder(term->frontend, FALSE);
68f9b3d9 3420 break;
3421 case 7:
a8327734 3422 refresh_window(term->frontend);
68f9b3d9 3423 break;
3424 case 8:
887035a5 3425 if (term->esc_nargs >= 3) {
64734920 3426 if (!term->cfg.no_remote_resize)
a8327734 3427 request_resize(term->frontend,
64734920 3428 def(term->esc_args[2], term->cfg.width),
3429 def(term->esc_args[1], term->cfg.height));
68f9b3d9 3430 }
3431 break;
3432 case 9:
887035a5 3433 if (term->esc_nargs >= 2)
a8327734 3434 set_zoomed(term->frontend,
3435 term->esc_args[1] ?
887035a5 3436 TRUE : FALSE);
68f9b3d9 3437 break;
3438 case 11:
b9d7bcad 3439 if (term->ldisc)
3440 ldisc_send(term->ldisc,
a8327734 3441 is_iconic(term->frontend) ?
3442 "\033[1t" : "\033[2t", 4, 0);
68f9b3d9 3443 break;
3444 case 13:
b9d7bcad 3445 if (term->ldisc) {
a8327734 3446 get_window_pos(term->frontend, &x, &y);
b9d7bcad 3447 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3448 ldisc_send(term->ldisc, buf, len, 0);
3449 }
68f9b3d9 3450 break;
3451 case 14:
b9d7bcad 3452 if (term->ldisc) {
a8327734 3453 get_window_pixels(term->frontend, &x, &y);
b9d7bcad 3454 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3455 ldisc_send(term->ldisc, buf, len, 0);
3456 }
68f9b3d9 3457 break;
3458 case 18:
b9d7bcad 3459 if (term->ldisc) {
3460 len = sprintf(buf, "\033[8;%d;%dt",
3461 term->rows, term->cols);
3462 ldisc_send(term->ldisc, buf, len, 0);
3463 }
68f9b3d9 3464 break;
3465 case 19:
3466 /*
3467 * Hmmm. Strictly speaking we
3468 * should return `the size of the
3469 * screen in characters', but
3470 * that's not easy: (a) window
3471 * furniture being what it is it's
3472 * hard to compute, and (b) in
3473 * resize-font mode maximising the
3474 * window wouldn't change the
3475 * number of characters. *shrug*. I
3476 * think we'll ignore it for the
3477 * moment and see if anyone
3478 * complains, and then ask them
3479 * what they would like it to do.
3480 */
3481 break;
3482 case 20:
7fcdebd3 3483 if (term->ldisc &&
3484 !term->cfg.no_remote_qtitle) {
a8327734 3485 p = get_window_title(term->frontend, TRUE);
b9d7bcad 3486 len = strlen(p);
3487 ldisc_send(term->ldisc, "\033]L", 3, 0);
3488 ldisc_send(term->ldisc, p, len, 0);
3489 ldisc_send(term->ldisc, "\033\\", 2, 0);
3490 }
68f9b3d9 3491 break;
3492 case 21:
7fcdebd3 3493 if (term->ldisc &&
3494 !term->cfg.no_remote_qtitle) {
a8327734 3495 p = get_window_title(term->frontend,FALSE);
b9d7bcad 3496 len = strlen(p);
3497 ldisc_send(term->ldisc, "\033]l", 3, 0);
3498 ldisc_send(term->ldisc, p, len, 0);
3499 ldisc_send(term->ldisc, "\033\\", 2, 0);
3500 }
68f9b3d9 3501 break;
3502 }
32874aea 3503 }
3504 break;
a263e4f6 3505 case 'S': /* SU: Scroll up */
1a837633 3506 compatibility(SCOANSI);
887035a5 3507 scroll(term, term->marg_t, term->marg_b,
3508 def(term->esc_args[0], 1), TRUE);
887035a5 3509 term->wrapnext = FALSE;
3510 term->seen_disp_event = TRUE;
1a837633 3511 break;
a263e4f6 3512 case 'T': /* SD: Scroll down */
1a837633 3513 compatibility(SCOANSI);
887035a5 3514 scroll(term, term->marg_t, term->marg_b,
3515 -def(term->esc_args[0], 1), TRUE);
887035a5 3516 term->wrapnext = FALSE;
3517 term->seen_disp_event = TRUE;
1a837633 3518 break;
a263e4f6 3519 case ANSI('|', '*'): /* DECSNLS */
3520 /*
32874aea 3521 * Set number of lines on screen
a263e4f6 3522 * VT420 uses VGA like hardware and can
3523 * support any size in reasonable range
3524 * (24..49 AIUI) with no default specified.
32874aea 3525 */
3526 compatibility(VT420);
887035a5 3527 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
64734920 3528 if (!term->cfg.no_remote_resize)
a8327734 3529 request_resize(term->frontend, term->cols,
887035a5 3530 def(term->esc_args[0],
64734920 3531 term->cfg.height));
887035a5 3532 deselect(term);
32874aea 3533 }
3534 break;
a263e4f6 3535 case ANSI('|', '$'): /* DECSCPP */
3536 /*
32874aea 3537 * Set number of columns per page
a263e4f6 3538 * Docs imply range is only 80 or 132, but
3539 * I'll allow any.
32874aea 3540 */
3541 compatibility(VT340TEXT);
887035a5 3542 if (term->esc_nargs <= 1) {
64734920 3543 if (!term->cfg.no_remote_resize)
a8327734 3544 request_resize(term->frontend,
3545 def(term->esc_args[0],
64734920 3546 term->cfg.width), term->rows);
887035a5 3547 deselect(term);
32874aea 3548 }
3549 break;
a263e4f6 3550 case 'X': /* ECH: write N spaces w/o moving cursor */
3551 /* XXX VTTEST says this is vt220, vt510 manual
3552 * says vt100 */
32874aea 3553 compatibility(ANSIMIN);
3554 {
887035a5 3555 int n = def(term->esc_args[0], 1);
32874aea 3556 pos cursplus;
c6958dfe 3557 int p = term->curs.x;
3558 termline *cline = scrlineptr(term->curs.y);
3559
887035a5 3560 if (n > term->cols - term->curs.x)
3561 n = term->cols - term->curs.x;
3562 cursplus = term->curs;
32874aea 3563 cursplus.x += n;
3c7366f8 3564 check_boundary(term, term->curs.x, term->curs.y);
3565 check_boundary(term, term->curs.x+n, term->curs.y);
887035a5 3566 check_selection(term, term->curs, cursplus);
32874aea 3567 while (n--)
c6958dfe 3568 copy_termchar(cline, p++,
3569 &term->erase_char);
887035a5 3570 term->seen_disp_event = TRUE;
32874aea 3571 }
3572 break;
a263e4f6 3573 case 'x': /* DECREQTPARM: report terminal characteristics */
32874aea 3574 compatibility(VT100);
b9d7bcad 3575 if (term->ldisc) {
32874aea 3576 char buf[32];
887035a5 3577 int i = def(term->esc_args[0], 0);
32874aea 3578 if (i == 0 || i == 1) {
3579 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3580 buf[2] += i;
b9d7bcad 3581 ldisc_send(term->ldisc, buf, 20, 0);
32874aea 3582 }
3583 }
3584 break;
a263e4f6 3585 case 'Z': /* CBT: BackTab for xterm */
979f6987 3586 compatibility(OTHER);
3587 {
887035a5 3588 int i = def(term->esc_args[0], 1);
3589 pos old_curs = term->curs;
979f6987 3590
887035a5 3591 for(;i>0 && term->curs.x>0; i--) {
979f6987 3592 do {
887035a5 3593 term->curs.x--;
3594 } while (term->curs.x >0 &&
3595 !term->tabs[term->curs.x]);
979f6987 3596 }
887035a5 3597 check_selection(term, old_curs, term->curs);
979f6987 3598 }
3599 break;
1b20b972 3600 case ANSI('c', '='): /* Hide or Show Cursor */
3601 compatibility(SCOANSI);
3602 switch(term->esc_args[0]) {
3603 case 0: /* hide cursor */
3604 term->cursor_on = FALSE;
3605 break;
3606 case 1: /* restore cursor */
3607 term->big_cursor = FALSE;
3608 term->cursor_on = TRUE;
3609 break;
3610 case 2: /* block cursor */
3611 term->big_cursor = TRUE;
3612 term->cursor_on = TRUE;
3613 break;
3614 }
3615 break;
3616 case ANSI('C', '='):
3617 /*
3618 * set cursor start on scanline esc_args[0] and
3619 * end on scanline esc_args[1].If you set
3620 * the bottom scan line to a value less than
3621 * the top scan line, the cursor will disappear.
3622 */
3623 compatibility(SCOANSI);
3624 if (term->esc_nargs >= 2) {
3625 if (term->esc_args[0] > term->esc_args[1])
3626 term->cursor_on = FALSE;
3627 else
3628 term->cursor_on = TRUE;
3629 }
3630 break;
3631 case ANSI('D', '='):
3632 compatibility(SCOANSI);
3633 term->blink_is_real = FALSE;
3634 if (term->esc_args[0]>=1)
3635 term->curr_attr |= ATTR_BLINK;
3636 else
3637 term->curr_attr &= ~ATTR_BLINK;
3638 break;
3639 case ANSI('E', '='):
3640 compatibility(SCOANSI);
3641 term->blink_is_real = (term->esc_args[0] >= 1);
3642 break;
3643 case ANSI('F', '='): /* set normal foreground */
3644 compatibility(SCOANSI);
3645 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 3646 long colour =
3647 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 3648 ((term->esc_args[0] & 0x8) << 1)) <<
3649 ATTR_FGSHIFT;
69761fd5 3650 term->curr_attr &= ~ATTR_FGMASK;
3651 term->curr_attr |= colour;
3652 term->default_attr &= ~ATTR_FGMASK;
3653 term->default_attr |= colour;
1b20b972 3654 }
3655 break;
3656 case ANSI('G', '='): /* set normal background */
3657 compatibility(SCOANSI);
3658 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 3659 long colour =
3660 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 3661 ((term->esc_args[0] & 0x8) << 1)) <<
3662 ATTR_BGSHIFT;
69761fd5 3663 term->curr_attr &= ~ATTR_BGMASK;
3664 term->curr_attr |= colour;
3665 term->default_attr &= ~ATTR_BGMASK;
3666 term->default_attr |= colour;
1b20b972 3667 }
3668 break;
32874aea 3669 case ANSI('L', '='):
1b20b972 3670 compatibility(SCOANSI);
887035a5 3671 term->use_bce = (term->esc_args[0] <= 0);
36566009 3672 set_erase_char(term);
32874aea 3673 break;
a263e4f6 3674 case ANSI('p', '"'): /* DECSCL: set compat level */
887035a5 3675 /*
3676 * Allow the host to make this emulator a
3677 * 'perfect' VT102. This first appeared in
3678 * the VT220, but we do need to get back to
3679 * PuTTY mode so I won't check it.
e14a5a13 3680 *
4eeb7d09 3681 * The arg in 40..42,50 are a PuTTY extension.
32874aea 3682 * The 2nd arg, 8bit vs 7bit is not checked.
3683 *
887035a5 3684 * Setting VT102 mode should also change
3685 * the Fkeys to generate PF* codes as a
3686 * real VT102 has no Fkeys. The VT220 does
3687 * this, F11..F13 become ESC,BS,LF other
3688 * Fkeys send nothing.
32874aea 3689 *
3690 * Note ESC c will NOT change this!
374330e2 3691 */
32874aea 3692
887035a5 3693 switch (term->esc_args[0]) {
32874aea 3694 case 61:
887035a5 3695 term->compatibility_level &= ~TM_VTXXX;
3696 term->compatibility_level |= TM_VT102;
374330e2 3697 break;
32874aea 3698 case 62:
887035a5 3699 term->compatibility_level &= ~TM_VTXXX;
3700 term->compatibility_level |= TM_VT220;
32874aea 3701 break;
3702
3703 default:
887035a5 3704 if (term->esc_args[0] > 60 &&
3705 term->esc_args[0] < 70)
3706 term->compatibility_level |= TM_VTXXX;
32874aea 3707 break;
3708
3709 case 40:
887035a5 3710 term->compatibility_level &= TM_VTXXX;
374330e2 3711 break;
32874aea 3712 case 41:
887035a5 3713 term->compatibility_level = TM_PUTTY;
374330e2 3714 break;
32874aea 3715 case 42:
887035a5 3716 term->compatibility_level = TM_SCOANSI;
374330e2 3717 break;
32874aea 3718
3719 case ARG_DEFAULT:
887035a5 3720 term->compatibility_level = TM_PUTTY;
32874aea 3721 break;
3722 case 50:
3723 break;
3724 }
3725
3726 /* Change the response to CSI c */
887035a5 3727 if (term->esc_args[0] == 50) {
32874aea 3728 int i;
3729 char lbuf[64];
887035a5 3730 strcpy(term->id_string, "\033[?");
3731 for (i = 1; i < term->esc_nargs; i++) {
32874aea 3732 if (i != 1)
887035a5 3733 strcat(term->id_string, ";");
3734 sprintf(lbuf, "%d", term->esc_args[i]);
3735 strcat(term->id_string, lbuf);
32874aea 3736 }
887035a5 3737 strcat(term->id_string, "c");
32874aea 3738 }
3739#if 0
3740 /* Is this a good idea ?
3741 * Well we should do a soft reset at this point ...
3742 */
3743 if (!has_compat(VT420) && has_compat(VT100)) {
64734920 3744 if (!term->cfg.no_remote_resize) {
887035a5 3745 if (term->reset_132)
0d2086c5 3746 request_resize(132, 24);
3747 else
3748 request_resize(80, 24);
3749 }
374330e2 3750 }
32874aea 3751#endif
3752 break;
374330e2 3753 }
374330e2 3754 break;
32874aea 3755 case SEEN_OSC:
887035a5 3756 term->osc_w = FALSE;
32874aea 3757 switch (c) {
3758 case 'P': /* Linux palette sequence */
887035a5 3759 term->termstate = SEEN_OSC_P;
3760 term->osc_strlen = 0;
32874aea 3761 break;
3762 case 'R': /* Linux palette reset */
a8327734 3763 palette_reset(term->frontend);
887035a5 3764 term_invalidate(term);
3765 term->termstate = TOPLEVEL;
32874aea 3766 break;
3767 case 'W': /* word-set */
887035a5 3768 term->termstate = SEEN_OSC_W;
3769 term->osc_w = TRUE;
32874aea 3770 break;
3771 case '0':
3772 case '1':
3773 case '2':
3774 case '3':
3775 case '4':
3776 case '5':
3777 case '6':
3778 case '7':
3779 case '8':
3780 case '9':
887035a5 3781 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 3782 break;
3783 case 'L':
3784 /*
3785 * Grotty hack to support xterm and DECterm title
3786 * sequences concurrently.
3787 */
887035a5 3788 if (term->esc_args[0] == 2) {
3789 term->esc_args[0] = 1;
32874aea 3790 break;
3791 }
3792 /* else fall through */
3793 default:
887035a5 3794 term->termstate = OSC_STRING;
3795 term->osc_strlen = 0;
e14a5a13 3796 }
3797 break;
32874aea 3798 case OSC_STRING:
3799 /*
3800 * This OSC stuff is EVIL. It takes just one character to get into
3801 * sysline mode and it's not initially obvious how to get out.
3802 * So I've added CR and LF as string aborts.
3803 * This shouldn't effect compatibility as I believe embedded
3804 * control characters are supposed to be interpreted (maybe?)
3805 * and they don't display anything useful anyway.
3806 *
3807 * -- RDB
e14a5a13 3808 */
92e23ad9 3809 if (c == '\012' || c == '\015') {
887035a5 3810 term->termstate = TOPLEVEL;
32874aea 3811 } else if (c == 0234 || c == '\007') {
3812 /*
3813 * These characters terminate the string; ST and BEL
3814 * terminate the sequence and trigger instant
3815 * processing of it, whereas ESC goes back to SEEN_ESC
3816 * mode unless it is followed by \, in which case it is
3817 * synonymous with ST in the first place.
3818 */
887035a5 3819 do_osc(term);
3820 term->termstate = TOPLEVEL;
32874aea 3821 } else if (c == '\033')
887035a5 3822 term->termstate = OSC_MAYBE_ST;
3823 else if (term->osc_strlen < OSC_STR_MAX)
36566009 3824 term->osc_string[term->osc_strlen++] = (char)c;
374330e2 3825 break;
32874aea 3826 case SEEN_OSC_P:
374330e2 3827 {
887035a5 3828 int max = (term->osc_strlen == 0 ? 21 : 16);
32874aea 3829 int val;
36566009 3830 if ((int)c >= '0' && (int)c <= '9')
32874aea 3831 val = c - '0';
36566009 3832 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
32874aea 3833 val = c - 'A' + 10;
36566009 3834 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
32874aea 3835 val = c - 'a' + 10;
2d466ffd 3836 else {
887035a5 3837 term->termstate = TOPLEVEL;
2d466ffd 3838 break;
3839 }
887035a5 3840 term->osc_string[term->osc_strlen++] = val;
3841 if (term->osc_strlen >= 7) {
a8327734 3842 palette_set(term->frontend, term->osc_string[0],
887035a5 3843 term->osc_string[1] * 16 + term->osc_string[2],
3844 term->osc_string[3] * 16 + term->osc_string[4],
3845 term->osc_string[5] * 16 + term->osc_string[6]);
3846 term_invalidate(term);
3847 term->termstate = TOPLEVEL;
374330e2 3848 }
3849 }
3850 break;
32874aea 3851 case SEEN_OSC_W:
3852 switch (c) {
3853 case '0':
3854 case '1':
3855 case '2':
3856 case '3':
3857 case '4':
3858 case '5':
3859 case '6':
3860 case '7':
3861 case '8':
3862 case '9':
887035a5 3863 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 3864 break;
3865 default:
887035a5 3866 term->termstate = OSC_STRING;
3867 term->osc_strlen = 0;
ec55b220 3868 }
32874aea 3869 break;
32874aea 3870 case VT52_ESC:
887035a5 3871 term->termstate = TOPLEVEL;
3872 term->seen_disp_event = TRUE;
c9def1b8 3873 switch (c) {
32874aea 3874 case 'A':
887035a5 3875 move(term, term->curs.x, term->curs.y - 1, 1);
32874aea 3876 break;
3877 case 'B':
887035a5 3878 move(term, term->curs.x, term->curs.y + 1, 1);
32874aea 3879 break;
3880 case 'C':
887035a5 3881 move(term, term->curs.x + 1, term->curs.y, 1);
32874aea 3882 break;
3883 case 'D':
887035a5 3884 move(term, term->curs.x - 1, term->curs.y, 1);
32874aea 3885 break;
4eeb7d09 3886 /*
3887 * From the VT100 Manual
3888 * NOTE: The special graphics characters in the VT100
3889 * are different from those in the VT52
3890 *
3891 * From VT102 manual:
3892 * 137 _ Blank - Same
3893 * 140 ` Reserved - Humm.
3894 * 141 a Solid rectangle - Similar
3895 * 142 b 1/ - Top half of fraction for the
3896 * 143 c 3/ - subscript numbers below.
3897 * 144 d 5/
3898 * 145 e 7/
3899 * 146 f Degrees - Same
3900 * 147 g Plus or minus - Same
3901 * 150 h Right arrow
3902 * 151 i Ellipsis (dots)
3903 * 152 j Divide by
3904 * 153 k Down arrow
3905 * 154 l Bar at scan 0
3906 * 155 m Bar at scan 1
3907 * 156 n Bar at scan 2
3908 * 157 o Bar at scan 3 - Similar
3909 * 160 p Bar at scan 4 - Similar
3910 * 161 q Bar at scan 5 - Similar
3911 * 162 r Bar at scan 6 - Same
3912 * 163 s Bar at scan 7 - Similar
3913 * 164 t Subscript 0
3914 * 165 u Subscript 1
3915 * 166 v Subscript 2
3916 * 167 w Subscript 3
3917 * 170 x Subscript 4
3918 * 171 y Subscript 5
3919 * 172 z Subscript 6
3920 * 173 { Subscript 7
3921 * 174 | Subscript 8
3922 * 175 } Subscript 9
3923 * 176 ~ Paragraph
3924 *
3925 */
32874aea 3926 case 'F':
36566009 3927 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
32874aea 3928 break;
3929 case 'G':
36566009 3930 term->cset_attr[term->cset = 0] = CSET_ASCII;
32874aea 3931 break;
3932 case 'H':
887035a5 3933 move(term, 0, 0, 0);
32874aea 3934 break;
3935 case 'I':
887035a5 3936 if (term->curs.y == 0)
3937 scroll(term, 0, term->rows - 1, -1, TRUE);
3938 else if (term->curs.y > 0)
3939 term->curs.y--;
887035a5 3940 term->wrapnext = FALSE;
32874aea 3941 break;
3942 case 'J':
887035a5 3943 erase_lots(term, FALSE, FALSE, TRUE);
3944 term->disptop = 0;
c9def1b8 3945 break;
32874aea 3946 case 'K':
887035a5 3947 erase_lots(term, TRUE, FALSE, TRUE);
32874aea 3948 break;
4eeb7d09 3949#if 0
32874aea 3950 case 'V':
3951 /* XXX Print cursor line */
3952 break;
3953 case 'W':
3954 /* XXX Start controller mode */
3955 break;
3956 case 'X':
3957 /* XXX Stop controller mode */
3958 break;
4eeb7d09 3959#endif
32874aea 3960 case 'Y':
887035a5 3961 term->termstate = VT52_Y1;
32874aea 3962 break;
3963 case 'Z':
b9d7bcad 3964 if (term->ldisc)
3965 ldisc_send(term->ldisc, "\033/Z", 3, 0);
32874aea 3966 break;
3967 case '=':
887035a5 3968 term->app_keypad_keys = TRUE;
32874aea 3969 break;
3970 case '>':
887035a5 3971 term->app_keypad_keys = FALSE;
32874aea 3972 break;
3973 case '<':
3974 /* XXX This should switch to VT100 mode not current or default
3975 * VT mode. But this will only have effect in a VT220+
3976 * emulation.
3977 */
887035a5 3978 term->vt52_mode = FALSE;
64734920 3979 term->blink_is_real = term->cfg.blinktext;
32874aea 3980 break;
4eeb7d09 3981#if 0
32874aea 3982 case '^':
3983 /* XXX Enter auto print mode */
3984 break;
3985 case '_':
3986 /* XXX Exit auto print mode */
3987 break;
3988 case ']':
3989 /* XXX Print screen */
3990 break;
4eeb7d09 3991#endif
3992
3993#ifdef VT52_PLUS
3994 case 'E':
3995 /* compatibility(ATARI) */
887035a5 3996 move(term, 0, 0, 0);
3997 erase_lots(term, FALSE, FALSE, TRUE);
3998 term->disptop = 0;
4eeb7d09 3999 break;
4000 case 'L':
4001 /* compatibility(ATARI) */
887035a5 4002 if (term->curs.y <= term->marg_b)
4003 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4eeb7d09 4004 break;
4005 case 'M':
4006 /* compatibility(ATARI) */
887035a5 4007 if (term->curs.y <= term->marg_b)
4008 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4eeb7d09 4009 break;
4010 case 'b':
4011 /* compatibility(ATARI) */
887035a5 4012 term->termstate = VT52_FG;
4eeb7d09 4013 break;
4014 case 'c':
4015 /* compatibility(ATARI) */
887035a5 4016 term->termstate = VT52_BG;
4eeb7d09 4017 break;
4018 case 'd':
4019 /* compatibility(ATARI) */
887035a5 4020 erase_lots(term, FALSE, TRUE, FALSE);
4021 term->disptop = 0;
4eeb7d09 4022 break;
4023 case 'e':
4024 /* compatibility(ATARI) */
887035a5 4025 term->cursor_on = TRUE;
4eeb7d09 4026 break;
4027 case 'f':
4028 /* compatibility(ATARI) */
887035a5 4029 term->cursor_on = FALSE;
4eeb7d09 4030 break;
4031 /* case 'j': Save cursor position - broken on ST */
4032 /* case 'k': Restore cursor position */
4033 case 'l':
4034 /* compatibility(ATARI) */
887035a5 4035 erase_lots(term, TRUE, TRUE, TRUE);
4036 term->curs.x = 0;
4037 term->wrapnext = FALSE;
4eeb7d09 4038 break;
4039 case 'o':
4040 /* compatibility(ATARI) */
887035a5 4041 erase_lots(term, TRUE, TRUE, FALSE);
4eeb7d09 4042 break;
4043 case 'p':
4044 /* compatibility(ATARI) */
887035a5 4045 term->curr_attr |= ATTR_REVERSE;
4eeb7d09 4046 break;
4047 case 'q':
4048 /* compatibility(ATARI) */
887035a5 4049 term->curr_attr &= ~ATTR_REVERSE;
4eeb7d09 4050 break;
4051 case 'v': /* wrap Autowrap on - Wyse style */
4052 /* compatibility(ATARI) */
887035a5 4053 term->wrap = 1;
4eeb7d09 4054 break;
4055 case 'w': /* Autowrap off */
4056 /* compatibility(ATARI) */
887035a5 4057 term->wrap = 0;
4eeb7d09 4058 break;
4059
4060 case 'R':
4061 /* compatibility(OTHER) */
887035a5 4062 term->vt52_bold = FALSE;
4063 term->curr_attr = ATTR_DEFAULT;
36566009 4064 set_erase_char(term);
4eeb7d09 4065 break;
4066 case 'S':
4067 /* compatibility(VI50) */
887035a5 4068 term->curr_attr |= ATTR_UNDER;
4eeb7d09 4069 break;
4070 case 'W':
4071 /* compatibility(VI50) */
887035a5 4072 term->curr_attr &= ~ATTR_UNDER;
4eeb7d09 4073 break;
4074 case 'U':
4075 /* compatibility(VI50) */
887035a5 4076 term->vt52_bold = TRUE;
4077 term->curr_attr |= ATTR_BOLD;
4eeb7d09 4078 break;
4079 case 'T':
4080 /* compatibility(VI50) */
887035a5 4081 term->vt52_bold = FALSE;
4082 term->curr_attr &= ~ATTR_BOLD;
4eeb7d09 4083 break;
4084#endif
c9def1b8 4085 }
e14a5a13 4086 break;
32874aea 4087 case VT52_Y1:
887035a5 4088 term->termstate = VT52_Y2;
4089 move(term, term->curs.x, c - ' ', 0);
e14a5a13 4090 break;
32874aea 4091 case VT52_Y2:
887035a5 4092 term->termstate = TOPLEVEL;
4093 move(term, c - ' ', term->curs.y, 0);
e14a5a13 4094 break;
4eeb7d09 4095
4096#ifdef VT52_PLUS
4097 case VT52_FG:
887035a5 4098 term->termstate = TOPLEVEL;
4099 term->curr_attr &= ~ATTR_FGMASK;
4100 term->curr_attr &= ~ATTR_BOLD;
4101 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
4102 if ((c & 0x8) || term->vt52_bold)
4103 term->curr_attr |= ATTR_BOLD;
4104
36566009 4105 set_erase_char(term);
4eeb7d09 4106 break;
4107 case VT52_BG:
887035a5 4108 term->termstate = TOPLEVEL;
4109 term->curr_attr &= ~ATTR_BGMASK;
4110 term->curr_attr &= ~ATTR_BLINK;
4111 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
4eeb7d09 4112
4113 /* Note: bold background */
4114 if (c & 0x8)
887035a5 4115 term->curr_attr |= ATTR_BLINK;
4eeb7d09 4116
36566009 4117 set_erase_char(term);
4eeb7d09 4118 break;
4119#endif
2d466ffd 4120 default: break; /* placate gcc warning about enum use */
e14a5a13 4121 }
887035a5 4122 if (term->selstate != NO_SELECTION) {
4123 pos cursplus = term->curs;
4facdf84 4124 incpos(cursplus);
887035a5 4125 check_selection(term, term->curs, cursplus);
4facdf84 4126 }
374330e2 4127 }
b44b307a 4128
887035a5 4129 term_print_flush(term);
11cc5e30 4130 logflush(term->logctx);
374330e2 4131}
4132
374330e2 4133/*
f0fccd51 4134 * To prevent having to run the reasonably tricky bidi algorithm
4135 * too many times, we maintain a cache of the last lineful of data
4136 * fed to the algorithm on each line of the display.
4137 */
4138static int term_bidi_cache_hit(Terminal *term, int line,
36566009 4139 termchar *lbefore, int width)
f0fccd51 4140{
c6958dfe 4141 int i;
4142
f0fccd51 4143 if (!term->pre_bidi_cache)
4144 return FALSE; /* cache doesn't even exist yet! */
4145
4146 if (line >= term->bidi_cache_size)
4147 return FALSE; /* cache doesn't have this many lines */
4148
4149 if (!term->pre_bidi_cache[line])
4150 return FALSE; /* cache doesn't contain _this_ line */
4151
c6958dfe 4152 for (i = 0; i < width; i++)
4153 if (!termchars_equal(term->pre_bidi_cache[line] + i, lbefore + i))
4154 return FALSE; /* line doesn't match cache */
f0fccd51 4155
c6958dfe 4156 return TRUE; /* it didn't match. */
f0fccd51 4157}
4158
36566009 4159static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
4160 termchar *lafter, int width)
f0fccd51 4161{
4162 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
4163 int j = term->bidi_cache_size;
4164 term->bidi_cache_size = line+1;
4165 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
4166 term->bidi_cache_size,
36566009 4167 termchar *);
f0fccd51 4168 term->post_bidi_cache = sresize(term->post_bidi_cache,
4169 term->bidi_cache_size,
36566009 4170 termchar *);
f0fccd51 4171 while (j < term->bidi_cache_size) {
4172 term->pre_bidi_cache[j] = term->post_bidi_cache[j] = NULL;
4173 j++;
4174 }
4175 }
4176
4177 sfree(term->pre_bidi_cache[line]);
4178 sfree(term->post_bidi_cache[line]);
4179
36566009 4180 term->pre_bidi_cache[line] = snewn(width, termchar);
4181 term->post_bidi_cache[line] = snewn(width, termchar);
f0fccd51 4182
36566009 4183 memcpy(term->pre_bidi_cache[line], lbefore, width * TSIZE);
4184 memcpy(term->post_bidi_cache[line], lafter, width * TSIZE);
f0fccd51 4185}
4186
4187/*
374330e2 4188 * Given a context, update the window. Out of paranoia, we don't
4189 * allow WM_PAINT responses to do scrolling optimisations.
4190 */
887035a5 4191static void do_paint(Terminal *term, Context ctx, int may_optimise)
32874aea 4192{
f0fccd51 4193 int i, it, j, our_curs_y, our_curs_x;
36566009 4194 int rv, cursor;
4facdf84 4195 pos scrpos;
c6958dfe 4196 wchar_t *ch;
4197 int chlen;
36566009 4198 termchar cursor_background;
286c6b86 4199 unsigned long ticks;
341eb978 4200#ifdef OPTIMISE_SCROLL
4201 struct scrollregion *sr;
4202#endif /* OPTIMISE_SCROLL */
374330e2 4203
36566009 4204 cursor_background = term->basic_erase_char;
4205
c6958dfe 4206 chlen = 1024;
4207 ch = snewn(chlen, wchar_t);
4208
156686ef 4209 /*
4210 * Check the visual bell state.
4211 */
887035a5 4212 if (term->in_vbell) {
f7f27309 4213 ticks = GETTICKCOUNT();
887035a5 4214 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
f0fccd51 4215 term->in_vbell = FALSE;
4216 }
156686ef 4217
887035a5 4218 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4eeb7d09 4219
156686ef 4220 /* Depends on:
4221 * screen array, disptop, scrtop,
4222 * selection, rv,
4223 * cfg.blinkpc, blink_is_real, tblinker,
4eeb7d09 4224 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
156686ef 4225 */
4eeb7d09 4226
4227 /* Has the cursor position or type changed ? */
887035a5 4228 if (term->cursor_on) {
4229 if (term->has_focus) {
64734920 4230 if (term->blinker || !term->cfg.blink_cur)
4eeb7d09 4231 cursor = TATTR_ACTCURS;
32874aea 4232 else
4233 cursor = 0;
4234 } else
4eeb7d09 4235 cursor = TATTR_PASCURS;
887035a5 4236 if (term->wrapnext)
4eeb7d09 4237 cursor |= TATTR_RIGHTCURS;
32874aea 4238 } else
156686ef 4239 cursor = 0;
887035a5 4240 our_curs_y = term->curs.y - term->disptop;
3c7366f8 4241 {
4242 /*
4243 * Adjust the cursor position in the case where it's
4244 * resting on the right-hand half of a CJK wide character.
4245 * xterm's behaviour here, which seems adequate to me, is
4246 * to display the cursor covering the _whole_ character,
4247 * exactly as if it were one space to the left.
4248 */
36566009 4249 termline *ldata = lineptr(term->curs.y);
3c7366f8 4250 our_curs_x = term->curs.x;
4251 if (our_curs_x > 0 &&
36566009 4252 ldata->chars[our_curs_x].chr == UCSWIDE)
3c7366f8 4253 our_curs_x--;
36566009 4254 unlineptr(ldata);
3c7366f8 4255 }
887035a5 4256
36566009 4257 /*
4258 * If the cursor is not where it was last time we painted, and
4259 * its previous position is visible on screen, invalidate its
4260 * previous position.
4261 */
4262 if (term->dispcursy >= 0 &&
4263 (term->curstype != cursor ||
4264 term->dispcursy != our_curs_y ||
4265 term->dispcursx != our_curs_x)) {
4266 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
4267 term->dispcursx;
4268
4269 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
4270 dispcurs[-1].attr |= ATTR_INVALID;
4271 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
4272 dispcurs[1].attr |= ATTR_INVALID;
4273 dispcurs->attr |= ATTR_INVALID;
4274
887035a5 4275 term->curstype = 0;
4eeb7d09 4276 }
36566009 4277 term->dispcursx = term->dispcursy = -1;
4eeb7d09 4278
341eb978 4279#ifdef OPTIMISE_SCROLL
4280 /* Do scrolls */
4281 sr = term->scrollhead;
4282 while (sr) {
4283 struct scrollregion *next = sr->next;
4284 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
4285 sfree(sr);
4286 sr = next;
4287 }
4288 term->scrollhead = term->scrolltail = NULL;
4289#endif /* OPTIMISE_SCROLL */
4290
4eeb7d09 4291 /* The normal screen data */
887035a5 4292 for (i = 0; i < term->rows; i++) {
36566009 4293 termline *ldata;
4294 termchar *lchars;
4295 int dirty_line, dirty_run, selected;
4296 unsigned long attr = 0, cset = 0;
4eeb7d09 4297 int updated_line = 0;
4298 int start = 0;
4299 int ccount = 0;
4300 int last_run_dirty = 0;
4301
887035a5 4302 scrpos.y = i + term->disptop;
4facdf84 4303 ldata = lineptr(scrpos.y);
4eeb7d09 4304
36566009 4305 dirty_run = dirty_line = (ldata->lattr !=
4306 term->disptext[i]->lattr);
4307 term->disptext[i]->lattr = ldata->lattr;
4eeb7d09 4308
f0fccd51 4309 /* Do Arabic shaping and bidi. */
4310 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4311
36566009 4312 if (!term_bidi_cache_hit(term, i, ldata->chars, term->cols)) {
f0fccd51 4313
c6958dfe 4314 if (term->wcFromTo_size < term->cols) {
4315 term->wcFromTo_size = term->cols;
4316 term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,
4317 bidi_char);
4318 term->wcTo = sresize(term->wcTo, term->wcFromTo_size,
4319 bidi_char);
4320 }
4321
f0fccd51 4322 for(it=0; it<term->cols ; it++)
4323 {
36566009 4324 unsigned long uc = (ldata->chars[it].chr);
f0fccd51 4325
4326 switch (uc & CSET_MASK) {
36566009 4327 case CSET_LINEDRW:
f0fccd51 4328 if (!term->cfg.rawcnp) {
4329 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4330 break;
4331 }
36566009 4332 case CSET_ASCII:
f0fccd51 4333 uc = term->ucsdata->unitab_line[uc & 0xFF];
4334 break;
36566009 4335 case CSET_SCOACS:
f0fccd51 4336 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4337 break;
4338 }
4339 switch (uc & CSET_MASK) {
36566009 4340 case CSET_ACP:
f0fccd51 4341 uc = term->ucsdata->unitab_font[uc & 0xFF];
4342 break;
36566009 4343 case CSET_OEMCP:
f0fccd51 4344 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4345 break;
4346 }
4347
36566009 4348 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4349 (wchar_t)uc;
f0fccd51 4350 term->wcFrom[it].index = it;
4351 }
4352
4353 if(!term->cfg.bidi)
4354 do_bidi(term->wcFrom, term->cols);
4355
4356 /* this is saved iff done from inside the shaping */
4357 if(!term->cfg.bidi && term->cfg.arabicshaping)
4358 for(it=0; it<term->cols; it++)
4359 term->wcTo[it] = term->wcFrom[it];
4360
4361 if(!term->cfg.arabicshaping)
4362 do_shape(term->wcFrom, term->wcTo, term->cols);
4363
c6958dfe 4364 if (term->ltemp_size < ldata->size) {
4365 term->ltemp_size = ldata->size;
4366 term->ltemp = sresize(term->ltemp, term->ltemp_size,
4367 termchar);
4368 }
4369
4370 memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);
4371
f0fccd51 4372 for(it=0; it<term->cols ; it++)
4373 {
36566009 4374 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
c6958dfe 4375 if (term->ltemp[it].cc_next)
4376 term->ltemp[it].cc_next -=
4377 it - term->wcTo[it].index;
f0fccd51 4378
4379 if (term->wcTo[it].origwc != term->wcTo[it].wc)
36566009 4380 term->ltemp[it].chr = term->wcTo[it].wc;
f0fccd51 4381 }
36566009 4382 term_bidi_cache_store(term, i, ldata->chars,
c6958dfe 4383 term->ltemp, ldata->size);
4384
36566009 4385 lchars = term->ltemp;
f0fccd51 4386 } else {
36566009 4387 lchars = term->post_bidi_cache[i];
f0fccd51 4388 }
36566009 4389 } else
4390 lchars = ldata->chars;
f0fccd51 4391
36566009 4392 for (j = 0; j < term->cols; j++) {
4eeb7d09 4393 unsigned long tattr, tchar;
36566009 4394 termchar *d = lchars + j;
c6958dfe 4395 int break_run, do_copy;
4facdf84 4396 scrpos.x = j;
32874aea 4397
36566009 4398 tchar = d->chr;
4399 tattr = d->attr;
4400
4eeb7d09 4401 switch (tchar & CSET_MASK) {
36566009 4402 case CSET_ASCII:
21d2b241 4403 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4eeb7d09 4404 break;
36566009 4405 case CSET_LINEDRW:
21d2b241 4406 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4eeb7d09 4407 break;
36566009 4408 case CSET_SCOACS:
21d2b241 4409 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
d3cb5465 4410 break;
4eeb7d09 4411 }
36566009 4412 if (j < term->cols-1 && d[1].chr == UCSWIDE)
552b7a5c 4413 tattr |= ATTR_WIDE;
4eeb7d09 4414
4415 /* Video reversing things */
f278d6f8 4416 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4417 if (term->seltype == LEXICOGRAPHIC)
4418 selected = (posle(term->selstart, scrpos) &&
4419 poslt(scrpos, term->selend));
4420 else
4421 selected = (posPle(term->selstart, scrpos) &&
4422 posPlt(scrpos, term->selend));
4423 } else
4424 selected = FALSE;
4eeb7d09 4425 tattr = (tattr ^ rv
6908fed7 4426 ^ (selected ? ATTR_REVERSE : 0));
4eeb7d09 4427
4428 /* 'Real' blinking ? */
887035a5 4429 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4430 if (term->has_focus && term->tblinker) {
fc6f0fc2 4431 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
c9def1b8 4432 }
4eeb7d09 4433 tattr &= ~ATTR_BLINK;
c9def1b8 4434 }
374330e2 4435
5a73255e 4436 /*
4437 * Check the font we'll _probably_ be using to see if
4438 * the character is wide when we don't want it to be.
4439 */
36566009 4440 if (tchar != term->disptext[i]->chars[j].chr ||
4441 tattr != (term->disptext[i]->chars[j].attr &~
4442 ATTR_NARROW)) {
4443 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
5a73255e 4444 tattr |= ATTR_NARROW;
36566009 4445 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
5a73255e 4446 tattr |= ATTR_NARROW;
4447
4eeb7d09 4448 /* Cursor here ? Save the 'background' */
3c7366f8 4449 if (i == our_curs_y && j == our_curs_x) {
c6958dfe 4450 /* FULL-TERMCHAR */
36566009 4451 cursor_background.chr = tchar;
4452 cursor_background.attr = tattr;
c6958dfe 4453 /* For once, this cc_next field is an absolute index in lchars */
4454 if (d->cc_next)
4455 cursor_background.cc_next = d->cc_next + j;
4456 else
4457 cursor_background.cc_next = 0;
36566009 4458 term->dispcursx = j;
4459 term->dispcursy = i;
4eeb7d09 4460 }
374330e2 4461
36566009 4462 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4eeb7d09 4463 dirty_line = TRUE;
4464
c6958dfe 4465 break_run = ((tattr ^ attr) & term->attr_mask) != 0;
4eeb7d09 4466
4467 /* Special hack for VT100 Linedraw glyphs */
36566009 4468 if (tchar >= 0x23BA && tchar <= 0x23BD)
4469 break_run = TRUE;
4470
4471 /*
4472 * Separate out sequences of characters that have the
4473 * same CSET, if that CSET is a magic one.
4474 */
4475 if (CSET_OF(tchar) != cset)
4476 break_run = TRUE;
4eeb7d09 4477
c6958dfe 4478 /*
4479 * Break on both sides of any combined-character cell.
4480 */
4481 if (d->cc_next != 0 ||
4482 (j > 0 && d[-1].cc_next != 0))
4483 break_run = TRUE;
4484
21d2b241 4485 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
36566009 4486 if (term->disptext[i]->chars[j].chr == tchar &&
4487 term->disptext[i]->chars[j].attr == tattr)
4eeb7d09 4488 break_run = TRUE;
4489 else if (!dirty_run && ccount == 1)
4490 break_run = TRUE;
374330e2 4491 }
4eeb7d09 4492
4493 if (break_run) {
4494 if ((dirty_run || last_run_dirty) && ccount > 0) {
36566009 4495 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4eeb7d09 4496 updated_line = 1;
4497 }
4498 start = j;
4499 ccount = 0;
4500 attr = tattr;
36566009 4501 cset = CSET_OF(tchar);
21d2b241 4502 if (term->ucsdata->dbcs_screenfont)
4eeb7d09 4503 last_run_dirty = dirty_run;
4504 dirty_run = dirty_line;
4505 }
4506
c6958dfe 4507 do_copy = FALSE;
4508 if (!termchars_equal_override(&term->disptext[i]->chars[j],
4509 d, tchar, tattr)) {
4510 do_copy = TRUE;
4eeb7d09 4511 dirty_run = TRUE;
c6958dfe 4512 }
4513
4514 if (ccount >= chlen) {
4515 chlen = ccount + 256;
4516 ch = sresize(ch, chlen, wchar_t);
4517 }
36566009 4518 ch[ccount++] = (wchar_t) tchar;
c6958dfe 4519
4520 if (d->cc_next) {
4521 termchar *dd = d;
4522
4523 while (dd->cc_next) {
4524 unsigned long schar;
4525
4526 dd += dd->cc_next;
4527
4528 schar = dd->chr;
4529 switch (schar & CSET_MASK) {
4530 case CSET_ASCII:
4531 schar = term->ucsdata->unitab_line[schar & 0xFF];
4532 break;
4533 case CSET_LINEDRW:
4534 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4535 break;
4536 case CSET_SCOACS:
4537 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4538 break;
4539 }
4540
4541 if (ccount >= chlen) {
4542 chlen = ccount + 256;
4543 ch = sresize(ch, chlen, wchar_t);
4544 }
4545 ch[ccount++] = (wchar_t) schar;
4546 }
4547
4548 attr |= TATTR_COMBINING;
4549 }
4550
4551 if (do_copy) {
4552 copy_termchar(term->disptext[i], j, d);
4553 term->disptext[i]->chars[j].chr = tchar;
4554 term->disptext[i]->chars[j].attr = tattr;
4555 }
4eeb7d09 4556
4557 /* If it's a wide char step along to the next one. */
4558 if (tattr & ATTR_WIDE) {
887035a5 4559 if (++j < term->cols) {
4eeb7d09 4560 d++;
3c7366f8 4561 /*
4562 * By construction above, the cursor should not
4563 * be on the right-hand half of this character.
4564 * Ever.
4565 */
4566 assert(!(i == our_curs_y && j == our_curs_x));
c6958dfe 4567 if (!termchars_equal(&term->disptext[i]->chars[j], d))
4eeb7d09 4568 dirty_run = TRUE;
c6958dfe 4569 copy_termchar(term->disptext[i], j, d);
374330e2 4570 }
374330e2 4571 }
4eeb7d09 4572 }
4573 if (dirty_run && ccount > 0) {
36566009 4574 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4eeb7d09 4575 updated_line = 1;
4576 }
4577
4578 /* Cursor on this line ? (and changed) */
887035a5 4579 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
36566009 4580 ch[0] = (wchar_t) cursor_background.chr;
4581 attr = cursor_background.attr | cursor;
ac059c2e 4582 ccount = 1;
c6958dfe 4583
4584 if (cursor_background.cc_next) {
4585 termchar *dd = ldata->chars + cursor_background.cc_next;
4586
4587 while (1) {
4588 unsigned long schar;
4589
4590 schar = dd->chr;
4591 switch (schar & CSET_MASK) {
4592 case CSET_ASCII:
4593 schar = term->ucsdata->unitab_line[schar & 0xFF];
4594 break;
4595 case CSET_LINEDRW:
4596 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4597 break;
4598 case CSET_SCOACS:
4599 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4600 break;
4601 }
4602
4603 if (ccount >= chlen) {
4604 chlen = ccount + 256;
4605 ch = sresize(ch, chlen, wchar_t);
4606 }
4607 ch[ccount++] = (wchar_t) schar;
4608
4609 if (dd->cc_next)
4610 dd += dd->cc_next;
4611 else
4612 break;
4613 }
4614
4615 attr |= TATTR_COMBINING;
4616 }
4617
ac059c2e 4618 do_cursor(ctx, our_curs_x, i, ch, ccount, attr, ldata->lattr);
887035a5 4619 term->curstype = cursor;
374330e2 4620 }
36566009 4621
4622 unlineptr(ldata);
374330e2 4623 }
c6958dfe 4624
4625 sfree(ch);
374330e2 4626}
4627
4628/*
e14a5a13 4629 * Flick the switch that says if blinking things should be shown or hidden.
4630 */
4631
887035a5 4632void term_blink(Terminal *term, int flg)
32874aea 4633{
e14a5a13 4634 long now, blink_diff;
4635
f7f27309 4636 now = GETTICKCOUNT();
887035a5 4637 blink_diff = now - term->last_tblink;
c9def1b8 4638
1d3989ed 4639 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4640 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
887035a5 4641 term->last_tblink = now;
4642 term->tblinker = !term->tblinker;
c9def1b8 4643 }
4644
e14a5a13 4645 if (flg) {
887035a5 4646 term->blinker = 1;
4647 term->last_blink = now;
e14a5a13 4648 return;
32874aea 4649 }
e14a5a13 4650
887035a5 4651 blink_diff = now - term->last_blink;
e14a5a13 4652
f7f27309 4653 /* Make sure the cursor blinks no faster than system blink rate */
4654 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
32874aea 4655 return;
4656
887035a5 4657 term->last_blink = now;
4658 term->blinker = !term->blinker;
e14a5a13 4659}
4660
4661/*
374330e2 4662 * Invalidate the whole screen so it will be repainted in full.
4663 */
887035a5 4664void term_invalidate(Terminal *term)
32874aea 4665{
36566009 4666 int i, j;
374330e2 4667
36566009 4668 for (i = 0; i < term->rows; i++)
4669 for (j = 0; j < term->cols; j++)
4670 term->disptext[i]->chars[j].attr = ATTR_INVALID;
374330e2 4671}
4672
4673/*
4674 * Paint the window in response to a WM_PAINT message.
4675 */
887035a5 4676void term_paint(Terminal *term, Context ctx,
c1b55581 4677 int left, int top, int right, int bottom, int immediately)
32874aea 4678{
5a73255e 4679 int i, j;
4680 if (left < 0) left = 0;
4681 if (top < 0) top = 0;
887035a5 4682 if (right >= term->cols) right = term->cols-1;
4683 if (bottom >= term->rows) bottom = term->rows-1;
4684
4685 for (i = top; i <= bottom && i < term->rows; i++) {
a0f45c98 4686 if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
887035a5 4687 for (j = left; j <= right && j < term->cols; j++)
36566009 4688 term->disptext[i]->chars[j].attr = ATTR_INVALID;
c9def1b8 4689 else
887035a5 4690 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
36566009 4691 term->disptext[i]->chars[j].attr = ATTR_INVALID;
c9def1b8 4692 }
374330e2 4693
e14a5a13 4694 /* This should happen soon enough, also for some reason it sometimes
4695 * fails to actually do anything when re-sizing ... painting the wrong
4696 * window perhaps ?
32874aea 4697 */
c1b55581 4698 if (immediately)
887035a5 4699 do_paint (term, ctx, FALSE);
374330e2 4700}
4701
4702/*
4703 * Attempt to scroll the scrollback. The second parameter gives the
4704 * position we want to scroll to; the first is +1 to denote that
4705 * this position is relative to the beginning of the scrollback, -1
4706 * to denote it is relative to the end, and 0 to denote that it is
4707 * relative to the current position.
4708 */
887035a5 4709void term_scroll(Terminal *term, int rel, int where)
32874aea 4710{
876e5d5e 4711 int sbtop = -sblines(term);
37d2a505 4712#ifdef OPTIMISE_SCROLL
4713 int olddisptop = term->disptop;
4714 int shift;
4715#endif /* OPTIMISE_SCROLL */
887035a5 4716
4717 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4718 if (term->disptop < sbtop)
4719 term->disptop = sbtop;
4720 if (term->disptop > 0)
4721 term->disptop = 0;
4722 update_sbar(term);
37d2a505 4723#ifdef OPTIMISE_SCROLL
4724 shift = (term->disptop - olddisptop);
4725 if (shift < term->rows && shift > -term->rows)
4726 scroll_display(term, 0, term->rows - 1, shift);
4727#endif /* OPTIMISE_SCROLL */
887035a5 4728 term_update(term);
374330e2 4729}
4730
c076966d 4731static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
32874aea 4732{
4eeb7d09 4733 wchar_t *workbuf;
4734 wchar_t *wbptr; /* where next char goes within workbuf */
6908fed7 4735 int old_top_x;
32874aea 4736 int wblen = 0; /* workbuf len */
4737 int buflen; /* amount of memory allocated to workbuf */
bc1235d4 4738
4eeb7d09 4739 buflen = 5120; /* Default size */
3d88e64d 4740 workbuf = snewn(buflen, wchar_t);
4eeb7d09 4741 wbptr = workbuf; /* start filling here */
6908fed7 4742 old_top_x = top.x; /* needed for rect==1 */
bc1235d4 4743
4facdf84 4744 while (poslt(top, bottom)) {
bc1235d4 4745 int nl = FALSE;
36566009 4746 termline *ldata = lineptr(top.y);
260f3dec 4747 pos nlpos;
4facdf84 4748
6908fed7 4749 /*
4750 * nlpos will point at the maximum position on this line we
4751 * should copy up to. So we start it at the end of the
4752 * line...
4753 */
4facdf84 4754 nlpos.y = top.y;
887035a5 4755 nlpos.x = term->cols;
bc1235d4 4756
6908fed7 4757 /*
4758 * ... move it backwards if there's unused space at the end
4759 * of the line (and also set `nl' if this is the case,
4760 * because in normal selection mode this means we need a
4761 * newline at the end)...
4762 */
36566009 4763 if (!(ldata->lattr & LATTR_WRAPPED)) {
4764 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
4765 poslt(top, nlpos))
4eeb7d09 4766 decpos(nlpos);
4facdf84 4767 if (poslt(nlpos, bottom))
bc1235d4 4768 nl = TRUE;
36566009 4769 } else if (ldata->lattr & LATTR_WRAPPED2) {
3c7366f8 4770 /* Ignore the last char on the line in a WRAPPED2 line. */
4771 decpos(nlpos);
bc1235d4 4772 }
6908fed7 4773
4774 /*
4775 * ... and then clip it to the terminal x coordinate if
4776 * we're doing rectangular selection. (In this case we
4777 * still did the above, so that copying e.g. the right-hand
4778 * column from a table doesn't fill with spaces on the
4779 * right.)
4780 */
4781 if (rect) {
4782 if (nlpos.x > bottom.x)
4783 nlpos.x = bottom.x;
4784 nl = (top.y < bottom.y);
4785 }
4786
4facdf84 4787 while (poslt(top, bottom) && poslt(top, nlpos)) {
4eeb7d09 4788#if 0
4789 char cbuf[16], *p;
4790 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4791#else
4792 wchar_t cbuf[16], *p;
4eeb7d09 4793 int set, c;
c6958dfe 4794 int x = top.x;
4eeb7d09 4795
c6958dfe 4796 if (ldata->chars[x].chr == UCSWIDE) {
4eeb7d09 4797 top.x++;
4798 continue;
4799 }
4800
c6958dfe 4801 while (1) {
4802 int uc = ldata->chars[x].chr;
4803
4804 switch (uc & CSET_MASK) {
4805 case CSET_LINEDRW:
4806 if (!term->cfg.rawcnp) {
4807 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4808 break;
4809 }
4810 case CSET_ASCII:
4811 uc = term->ucsdata->unitab_line[uc & 0xFF];
4812 break;
4813 case CSET_SCOACS:
4814 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4815 break;
4816 }
4817 switch (uc & CSET_MASK) {
4818 case CSET_ACP:
4819 uc = term->ucsdata->unitab_font[uc & 0xFF];
4820 break;
4821 case CSET_OEMCP:
4822 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 4823 break;
32874aea 4824 }
4eeb7d09 4825
c6958dfe 4826 set = (uc & CSET_MASK);
4827 c = (uc & ~CSET_MASK);
4828 cbuf[0] = uc;
4829 cbuf[1] = 0;
4830
4831 if (DIRECT_FONT(uc)) {
4832 if (c >= ' ' && c != 0x7F) {
4833 char buf[4];
4834 WCHAR wbuf[4];
4835 int rv;
4836 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4837 buf[0] = c;
4838 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
4839 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4840 top.x++;
4841 } else {
4842 buf[0] = c;
4843 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4844 }
bc1235d4 4845
c6958dfe 4846 if (rv > 0) {
4847 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
4848 cbuf[rv] = 0;
4849 }
d3a22f79 4850 }
d3a22f79 4851 }
4eeb7d09 4852#endif
4853
c6958dfe 4854 for (p = cbuf; *p; p++) {
4855 /* Enough overhead for trailing NL and nul */
4856 if (wblen >= buflen - 16) {
4857 buflen += 100;
4858 workbuf = sresize(workbuf, buflen, wchar_t);
4859 wbptr = workbuf + wblen;
4860 }
4861 wblen++;
4862 *wbptr++ = *p;
4eeb7d09 4863 }
c6958dfe 4864
4865 if (ldata->chars[x].cc_next)
4866 x += ldata->chars[x].cc_next;
4867 else
4868 break;
bc1235d4 4869 }
4facdf84 4870 top.x++;
bc1235d4 4871 }
4872 if (nl) {
4873 int i;
4eeb7d09 4874 for (i = 0; i < sel_nl_sz; i++) {
32874aea 4875 wblen++;
bc1235d4 4876 *wbptr++ = sel_nl[i];
4877 }
4878 }
4facdf84 4879 top.y++;
6908fed7 4880 top.x = rect ? old_top_x : 0;
36566009 4881
4882 unlineptr(ldata);
bc1235d4 4883 }
e6346999 4884#if SELECTION_NUL_TERMINATED
4eeb7d09 4885 wblen++;
4886 *wbptr++ = 0;
e6346999 4887#endif
c076966d 4888 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
32874aea 4889 if (buflen > 0) /* indicates we allocated this buffer */
bc1235d4 4890 sfree(workbuf);
bc1235d4 4891}
4eeb7d09 4892
887035a5 4893void term_copyall(Terminal *term)
32874aea 4894{
4facdf84 4895 pos top;
40132dba 4896 pos bottom;
4897 tree234 *screen = term->screen;
876e5d5e 4898 top.y = -sblines(term);
4facdf84 4899 top.x = 0;
40132dba 4900 bottom.y = find_last_nonempty_line(term, screen);
4901 bottom.x = term->cols;
4902 clipme(term, top, bottom, 0, TRUE);
4eeb7d09 4903}
4904
4905/*
887035a5 4906 * The wordness array is mainly for deciding the disposition of the
4907 * US-ASCII characters.
4eeb7d09 4908 */
887035a5 4909static int wordtype(Terminal *term, int uc)
4eeb7d09 4910{
b9d7bcad 4911 struct ucsword {
4eeb7d09 4912 int start, end, ctype;
b9d7bcad 4913 };
4914 static const struct ucsword ucs_words[] = {
4eeb7d09 4915 {
4916 128, 160, 0}, {
4917 161, 191, 1}, {
4918 215, 215, 1}, {
4919 247, 247, 1}, {
4920 0x037e, 0x037e, 1}, /* Greek question mark */
4921 {
4922 0x0387, 0x0387, 1}, /* Greek ano teleia */
4923 {
4924 0x055a, 0x055f, 1}, /* Armenian punctuation */
4925 {
4926 0x0589, 0x0589, 1}, /* Armenian full stop */
4927 {
4928 0x0700, 0x070d, 1}, /* Syriac punctuation */
4929 {
4930 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4931 {
4932 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4933 {
4934 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4935 {
4936 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4937 {
4938 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4939 {
4940 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4941 {
4942 0x2000, 0x200a, 0}, /* Various spaces */
4943 {
4944 0x2070, 0x207f, 2}, /* superscript */
4945 {
4946 0x2080, 0x208f, 2}, /* subscript */
4947 {
4948 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4949 {
4950 0x3000, 0x3000, 0}, /* ideographic space */
4951 {
4952 0x3001, 0x3020, 1}, /* ideographic punctuation */
4953 {
4954 0x303f, 0x309f, 3}, /* Hiragana */
4955 {
4956 0x30a0, 0x30ff, 3}, /* Katakana */
4957 {
4958 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4959 {
4960 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4961 {
4962 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4963 {
4964 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4965 {
4966 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4967 {
4968 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4969 {
4970 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4971 {
4972 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4973 {
4974 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4975 {
4976 0, 0, 0}
4977 };
b9d7bcad 4978 const struct ucsword *wptr;
4eeb7d09 4979
4eeb7d09 4980 switch (uc & CSET_MASK) {
36566009 4981 case CSET_LINEDRW:
21d2b241 4982 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4eeb7d09 4983 break;
36566009 4984 case CSET_ASCII:
21d2b241 4985 uc = term->ucsdata->unitab_line[uc & 0xFF];
4eeb7d09 4986 break;
36566009 4987 case CSET_SCOACS:
21d2b241 4988 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
d3cb5465 4989 break;
4eeb7d09 4990 }
4991 switch (uc & CSET_MASK) {
36566009 4992 case CSET_ACP:
21d2b241 4993 uc = term->ucsdata->unitab_font[uc & 0xFF];
4eeb7d09 4994 break;
36566009 4995 case CSET_OEMCP:
21d2b241 4996 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 4997 break;
4998 }
4999
36566009 5000 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5a73255e 5001 * fail as there's such a thing as a double width space. :-(
5002 */
21d2b241 5003 if (term->ucsdata->dbcs_screenfont &&
5004 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5a73255e 5005 return (uc != ' ');
5006
4eeb7d09 5007 if (uc < 0x80)
887035a5 5008 return term->wordness[uc];
4eeb7d09 5009
5010 for (wptr = ucs_words; wptr->start; wptr++) {
5011 if (uc >= wptr->start && uc <= wptr->end)
5012 return wptr->ctype;
5013 }
5014
5015 return 2;
bc1235d4 5016}
5017
374330e2 5018/*
5019 * Spread the selection outwards according to the selection mode.
5020 */
887035a5 5021static pos sel_spread_half(Terminal *term, pos p, int dir)
32874aea 5022{
36566009 5023 termline *ldata;
374330e2 5024 short wvalue;
876e5d5e 5025 int topy = -sblines(term);
374330e2 5026
4facdf84 5027 ldata = lineptr(p.y);
374330e2 5028
887035a5 5029 switch (term->selmode) {
374330e2 5030 case SM_CHAR:
5031 /*
5032 * In this mode, every character is a separate unit, except
5033 * for runs of spaces at the end of a non-wrapping line.
5034 */
36566009 5035 if (!(ldata->lattr & LATTR_WRAPPED)) {
5036 termchar *q = ldata->chars + term->cols;
5037 while (q > ldata->chars && IS_SPACE_CHR(q[-1].chr))
374330e2 5038 q--;
36566009 5039 if (q == ldata->chars + term->cols)
374330e2 5040 q--;
36566009 5041 if (p.x >= q - ldata->chars)
5042 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
374330e2 5043 }
5044 break;
5045 case SM_WORD:
5046 /*
5047 * In this mode, the units are maximal runs of characters
5048 * whose `wordness' has the same value.
5049 */
36566009 5050 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
374330e2 5051 if (dir == +1) {
50ab4088 5052 while (1) {
36566009 5053 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
3c7366f8 5054 term->cols-1 : term->cols);
5055 if (p.x < maxcols-1) {
36566009 5056 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
50ab4088 5057 p.x++;
5058 else
5059 break;
5060 } else {
36566009 5061 if (ldata->lattr & LATTR_WRAPPED) {
5062 termline *ldata2;
50ab4088 5063 ldata2 = lineptr(p.y+1);
36566009 5064 if (wordtype(term, UCSGET(ldata2->chars, 0))
5065 == wvalue) {
50ab4088 5066 p.x = 0;
5067 p.y++;
36566009 5068 unlineptr(ldata);
50ab4088 5069 ldata = ldata2;
36566009 5070 } else {
5071 unlineptr(ldata2);
50ab4088 5072 break;
36566009 5073 }
50ab4088 5074 } else
5075 break;
5076 }
5077 }
374330e2 5078 } else {
50ab4088 5079 while (1) {
5080 if (p.x > 0) {
36566009 5081 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
50ab4088 5082 p.x--;
5083 else
5084 break;
5085 } else {
36566009 5086 termline *ldata2;
3c7366f8 5087 int maxcols;
50ab4088 5088 if (p.y <= topy)
5089 break;
5090 ldata2 = lineptr(p.y-1);
36566009 5091 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
3c7366f8 5092 term->cols-1 : term->cols);
36566009 5093 if (ldata2->lattr & LATTR_WRAPPED) {
5094 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
3c7366f8 5095 == wvalue) {
5096 p.x = maxcols-1;
5097 p.y--;
36566009 5098 unlineptr(ldata);
3c7366f8 5099 ldata = ldata2;
36566009 5100 } else {
5101 unlineptr(ldata2);
3c7366f8 5102 break;
36566009 5103 }
50ab4088 5104 } else
5105 break;
5106 }
5107 }
374330e2 5108 }
5109 break;
5110 case SM_LINE:
5111 /*
5112 * In this mode, every line is a unit.
5113 */
887035a5 5114 p.x = (dir == -1 ? 0 : term->cols - 1);
374330e2 5115 break;
5116 }
36566009 5117
5118 unlineptr(ldata);
374330e2 5119 return p;
5120}
5121
887035a5 5122static void sel_spread(Terminal *term)
32874aea 5123{
887035a5 5124 if (term->seltype == LEXICOGRAPHIC) {
5125 term->selstart = sel_spread_half(term, term->selstart, -1);
5126 decpos(term->selend);
5127 term->selend = sel_spread_half(term, term->selend, +1);
5128 incpos(term->selend);
6908fed7 5129 }
374330e2 5130}
5131
887035a5 5132void term_do_paste(Terminal *term)
568dd02f 5133{
5134 wchar_t *data;
5135 int len;
5136
a8327734 5137 get_clip(term->frontend, &data, &len);
2cb50250 5138 if (data && len > 0) {
568dd02f 5139 wchar_t *p, *q;
5140
887035a5 5141 term_seen_key_event(term); /* pasted data counts */
2cb50250 5142
887035a5 5143 if (term->paste_buffer)
5144 sfree(term->paste_buffer);
5145 term->paste_pos = term->paste_hold = term->paste_len = 0;
3d88e64d 5146 term->paste_buffer = snewn(len, wchar_t);
568dd02f 5147
5148 p = q = data;
5149 while (p < data + len) {
5150 while (p < data + len &&
5151 !(p <= data + len - sel_nl_sz &&
5152 !memcmp(p, sel_nl, sizeof(sel_nl))))
5153 p++;
5154
5155 {
5156 int i;
5157 for (i = 0; i < p - q; i++) {
887035a5 5158 term->paste_buffer[term->paste_len++] = q[i];
568dd02f 5159 }
5160 }
5161
5162 if (p <= data + len - sel_nl_sz &&
5163 !memcmp(p, sel_nl, sizeof(sel_nl))) {
92e23ad9 5164 term->paste_buffer[term->paste_len++] = '\015';
568dd02f 5165 p += sel_nl_sz;
5166 }
5167 q = p;
5168 }
5169
5170 /* Assume a small paste will be OK in one go. */
887035a5 5171 if (term->paste_len < 256) {
b9d7bcad 5172 if (term->ldisc)
5173 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
887035a5 5174 if (term->paste_buffer)
5175 sfree(term->paste_buffer);
5176 term->paste_buffer = 0;
5177 term->paste_pos = term->paste_hold = term->paste_len = 0;
568dd02f 5178 }
5179 }
a8327734 5180 get_clip(term->frontend, NULL, NULL);
568dd02f 5181}
5182
fc5b0934 5183void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
5184 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
32874aea 5185{
4facdf84 5186 pos selpoint;
36566009 5187 termline *ldata;
887035a5 5188 int raw_mouse = (term->xterm_mouse &&
64734920 5189 !term->cfg.no_mouse_rep &&
5190 !(term->cfg.mouse_override && shift));
6908fed7 5191 int default_seltype;
32874aea 5192
fff61f25 5193 if (y < 0) {
32874aea 5194 y = 0;
fff61f25 5195 if (a == MA_DRAG && !raw_mouse)
887035a5 5196 term_scroll(term, 0, -1);
fff61f25 5197 }
887035a5 5198 if (y >= term->rows) {
5199 y = term->rows - 1;
fff61f25 5200 if (a == MA_DRAG && !raw_mouse)
887035a5 5201 term_scroll(term, 0, +1);
fff61f25 5202 }
32874aea 5203 if (x < 0) {
5204 if (y > 0) {
887035a5 5205 x = term->cols - 1;
32874aea 5206 y--;
5207 } else
5208 x = 0;
094ed2a6 5209 }
887035a5 5210 if (x >= term->cols)
5211 x = term->cols - 1;
37508af4 5212
887035a5 5213 selpoint.y = y + term->disptop;
4facdf84 5214 selpoint.x = x;
5215 ldata = lineptr(selpoint.y);
36566009 5216 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
4facdf84 5217 selpoint.x /= 2;
36566009 5218 unlineptr(ldata);
374330e2 5219
fff61f25 5220 if (raw_mouse) {
01c034ad 5221 int encstate = 0, r, c;
5222 char abuf[16];
01c034ad 5223
b9d7bcad 5224 if (term->ldisc) {
5225
fc5b0934 5226 switch (braw) {
b9d7bcad 5227 case MBT_LEFT:
5228 encstate = 0x20; /* left button down */
5229 break;
5230 case MBT_MIDDLE:
5231 encstate = 0x21;
5232 break;
5233 case MBT_RIGHT:
5234 encstate = 0x22;
5235 break;
5236 case MBT_WHEEL_UP:
5237 encstate = 0x60;
5238 break;
5239 case MBT_WHEEL_DOWN:
5240 encstate = 0x61;
5241 break;
5242 default: break; /* placate gcc warning about enum use */
5243 }
5244 switch (a) {
5245 case MA_DRAG:
5246 if (term->xterm_mouse == 1)
5247 return;
5248 encstate += 0x20;
5249 break;
5250 case MA_RELEASE:
5251 encstate = 0x23;
5252 term->mouse_is_down = 0;
5253 break;
5254 case MA_CLICK:
fc5b0934 5255 if (term->mouse_is_down == braw)
b9d7bcad 5256 return;
fc5b0934 5257 term->mouse_is_down = braw;
b9d7bcad 5258 break;
5259 default: break; /* placate gcc warning about enum use */
5260 }
5261 if (shift)
5262 encstate += 0x04;
5263 if (ctrl)
5264 encstate += 0x10;
5265 r = y + 33;
5266 c = x + 33;
5267
5268 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
5269 ldisc_send(term->ldisc, abuf, 6, 0);
01c034ad 5270 }
01c034ad 5271 return;
5272 }
5273
6908fed7 5274 /*
5275 * Set the selection type (rectangular or normal) at the start
5276 * of a selection attempt, from the state of Alt.
5277 */
64734920 5278 if (!alt ^ !term->cfg.rect_select)
6908fed7 5279 default_seltype = RECTANGULAR;
5280 else
5281 default_seltype = LEXICOGRAPHIC;
5282
887035a5 5283 if (term->selstate == NO_SELECTION) {
5284 term->seltype = default_seltype;
6908fed7 5285 }
5286
fc5b0934 5287 if (bcooked == MBT_SELECT && a == MA_CLICK) {
887035a5 5288 deselect(term);
5289 term->selstate = ABOUT_TO;
5290 term->seltype = default_seltype;
5291 term->selanchor = selpoint;
5292 term->selmode = SM_CHAR;
fc5b0934 5293 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
887035a5 5294 deselect(term);
5295 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
5296 term->selstate = DRAGGING;
5297 term->selstart = term->selanchor = selpoint;
5298 term->selend = term->selstart;
5299 incpos(term->selend);
5300 sel_spread(term);
fc5b0934 5301 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
5302 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
887035a5 5303 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
374330e2 5304 return;
fc5b0934 5305 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
5306 term->selstate == SELECTED) {
887035a5 5307 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 5308 /*
5309 * For normal selection, we extend by moving
5310 * whichever end of the current selection is closer
5311 * to the mouse.
5312 */
887035a5 5313 if (posdiff(selpoint, term->selstart) <
5314 posdiff(term->selend, term->selstart) / 2) {
5315 term->selanchor = term->selend;
5316 decpos(term->selanchor);
6908fed7 5317 } else {
887035a5 5318 term->selanchor = term->selstart;
6908fed7 5319 }
4facdf84 5320 } else {
6908fed7 5321 /*
5322 * For rectangular selection, we have a choice of
5323 * _four_ places to put selanchor and selpoint: the
5324 * four corners of the selection.
5325 */
887035a5 5326 if (2*selpoint.x < term->selstart.x + term->selend.x)
5327 term->selanchor.x = term->selend.x-1;
6908fed7 5328 else
887035a5 5329 term->selanchor.x = term->selstart.x;
6908fed7 5330
887035a5 5331 if (2*selpoint.y < term->selstart.y + term->selend.y)
5332 term->selanchor.y = term->selend.y;
6908fed7 5333 else
887035a5 5334 term->selanchor.y = term->selstart.y;
4facdf84 5335 }
887035a5 5336 term->selstate = DRAGGING;
374330e2 5337 }
887035a5 5338 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
5339 term->selanchor = selpoint;
5340 term->selstate = DRAGGING;
5341 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 5342 /*
5343 * For normal selection, we set (selstart,selend) to
5344 * (selpoint,selanchor) in some order.
5345 */
887035a5 5346 if (poslt(selpoint, term->selanchor)) {
5347 term->selstart = selpoint;
5348 term->selend = term->selanchor;
5349 incpos(term->selend);
6908fed7 5350 } else {
887035a5 5351 term->selstart = term->selanchor;
5352 term->selend = selpoint;
5353 incpos(term->selend);
6908fed7 5354 }
374330e2 5355 } else {
6908fed7 5356 /*
5357 * For rectangular selection, we may need to
5358 * interchange x and y coordinates (if the user has
5359 * dragged in the -x and +y directions, or vice versa).
5360 */
887035a5 5361 term->selstart.x = min(term->selanchor.x, selpoint.x);
5362 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
5363 term->selstart.y = min(term->selanchor.y, selpoint.y);
5364 term->selend.y = max(term->selanchor.y, selpoint.y);
374330e2 5365 }
887035a5 5366 sel_spread(term);
fc5b0934 5367 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
5368 a == MA_RELEASE) {
887035a5 5369 if (term->selstate == DRAGGING) {
374330e2 5370 /*
5371 * We've completed a selection. We now transfer the
5372 * data to the clipboard.
5373 */
887035a5 5374 clipme(term, term->selstart, term->selend,
c076966d 5375 (term->seltype == RECTANGULAR), FALSE);
887035a5 5376 term->selstate = SELECTED;
374330e2 5377 } else
887035a5 5378 term->selstate = NO_SELECTION;
fc5b0934 5379 } else if (bcooked == MBT_PASTE
e6346999 5380 && (a == MA_CLICK
5381#if MULTICLICK_ONLY_EVENT
5382 || a == MA_2CLK || a == MA_3CLK
5383#endif
5384 )) {
a8327734 5385 request_paste(term->frontend);
374330e2 5386 }
5387
887035a5 5388 term_update(term);
374330e2 5389}
5390
6c50d421 5391void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
5392 unsigned int modifiers, unsigned int flags)
5393{
5394 char output[10];
5395 char *p = output;
5396 int prependesc = FALSE;
84b8d085 5397#if 0
6c50d421 5398 int i;
5399
5400 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
5401 for (i = 0; i < tlen; i++)
bf02e82e 5402 fprintf(stderr, " %04x", (unsigned)text[i]);
6c50d421 5403 fprintf(stderr, "\n");
84b8d085 5404#endif
6c50d421 5405
5406 /* XXX Num Lock */
5407 if ((flags & PKF_REPEAT) && term->repeat_off)
5408 return;
5409
5410 /* Currently, Meta always just prefixes everything with ESC. */
5411 if (modifiers & PKM_META)
5412 prependesc = TRUE;
5413 modifiers &= ~PKM_META;
5414
5415 /*
5416 * Alt is only used for Alt+keypad, which isn't supported yet, so
5417 * ignore it.
5418 */
5419 modifiers &= ~PKM_ALT;
5420
5421 /* Standard local function keys */
5422 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
5423 case PKM_SHIFT:
5424 if (keysym == PK_PAGEUP)
5425 /* scroll up one page */;
5426 if (keysym == PK_PAGEDOWN)
5427 /* scroll down on page */;
5428 if (keysym == PK_INSERT)
5429 term_do_paste(term);
5430 break;
5431 case PKM_CONTROL:
5432 if (keysym == PK_PAGEUP)
5433 /* scroll up one line */;
5434 if (keysym == PK_PAGEDOWN)
5435 /* scroll down one line */;
5436 /* Control-Numlock for app-keypad mode switch */
5437 if (keysym == PK_PF1)
5438 term->app_keypad_keys ^= 1;
5439 break;
5440 }
5441
5442 if (modifiers & PKM_ALT) {
5443 /* Alt+F4 (close) */
5444 /* Alt+Return (full screen) */
5445 /* Alt+Space (system menu) */
5446 }
5447
5448 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5449 text[0] >= 0x20 && text[0] <= 0x7e) {
5450 /* ASCII chars + Control */
bf02e82e 5451 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5452 (text[0] >= 0x61 && text[0] <= 0x7a))
6c50d421 5453 text[0] &= 0x1f;
5454 else {
5455 /*
5456 * Control-2 should return ^@ (0x00), Control-6 should return
5457 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5458 * the DOS keyboard handling did it, and we have nothing better
5459 * to do with the key combo in question, we'll also map
5460 * Control-Backquote to ^\ (0x1C).
5461 */
5462 switch (text[0]) {
5463 case ' ': text[0] = 0x00; break;
5464 case '-': text[0] = 0x1f; break;
5465 case '/': text[0] = 0x1f; break;
5466 case '2': text[0] = 0x00; break;
5467 case '3': text[0] = 0x1b; break;
5468 case '4': text[0] = 0x1c; break;
5469 case '5': text[0] = 0x1d; break;
5470 case '6': text[0] = 0x1e; break;
5471 case '7': text[0] = 0x1f; break;
5472 case '8': text[0] = 0x7f; break;
5473 case '`': text[0] = 0x1c; break;
5474 }
5475 }
5476 }
5477
5478 /* Nethack keypad */
5479 if (term->cfg.nethack_keypad) {
5480 char c = 0;
5481 switch (keysym) {
5482 case PK_KP1: c = 'b'; break;
5483 case PK_KP2: c = 'j'; break;
5484 case PK_KP3: c = 'n'; break;
5485 case PK_KP4: c = 'h'; break;
5486 case PK_KP5: c = '.'; break;
5487 case PK_KP6: c = 'l'; break;
5488 case PK_KP7: c = 'y'; break;
5489 case PK_KP8: c = 'k'; break;
5490 case PK_KP9: c = 'u'; break;
bf02e82e 5491 default: break; /* else gcc warns `enum value not used' */
6c50d421 5492 }
5493 if (c != 0) {
5494 if (c != '.') {
5495 if (modifiers & PKM_CONTROL)
5496 c &= 0x1f;
5497 else if (modifiers & PKM_SHIFT)
5498 c = toupper(c);
5499 }
5500 *p++ = c;
5501 goto done;
5502 }
5503 }
5504
5505 /* Numeric Keypad */
5506 if (PK_ISKEYPAD(keysym)) {
5507 int xkey = 0;
5508
5509 /*
5510 * In VT400 mode, PFn always emits an escape sequence. In
5511 * Linux and tilde modes, this only happens in app keypad mode.
5512 */
5513 if (term->cfg.funky_type == FUNKY_VT400 ||
5514 ((term->cfg.funky_type == FUNKY_LINUX ||
5515 term->cfg.funky_type == FUNKY_TILDE) &&
5516 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5517 switch (keysym) {
5518 case PK_PF1: xkey = 'P'; break;
5519 case PK_PF2: xkey = 'Q'; break;
5520 case PK_PF3: xkey = 'R'; break;
5521 case PK_PF4: xkey = 'S'; break;
bf02e82e 5522 default: break; /* else gcc warns `enum value not used' */
6c50d421 5523 }
5524 }
5525 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5526 switch (keysym) {
5527 case PK_KP0: xkey = 'p'; break;
5528 case PK_KP1: xkey = 'q'; break;
5529 case PK_KP2: xkey = 'r'; break;
5530 case PK_KP3: xkey = 's'; break;
5531 case PK_KP4: xkey = 't'; break;
5532 case PK_KP5: xkey = 'u'; break;
5533 case PK_KP6: xkey = 'v'; break;
5534 case PK_KP7: xkey = 'w'; break;
5535 case PK_KP8: xkey = 'x'; break;
5536 case PK_KP9: xkey = 'y'; break;
5537 case PK_KPDECIMAL: xkey = 'n'; break;
5538 case PK_KPENTER: xkey = 'M'; break;
bf02e82e 5539 default: break; /* else gcc warns `enum value not used' */
6c50d421 5540 }
5541 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5542 /*
5543 * xterm can't see the layout of the keypad, so it has
5544 * to rely on the X keysyms returned by the keys.
5545 * Hence, we look at the strings here, not the PuTTY
5546 * keysyms (which describe the layout).
5547 */
5548 switch (text[0]) {
5549 case '+':
5550 if (modifiers & PKM_SHIFT)
5551 xkey = 'l';
5552 else
5553 xkey = 'k';
5554 break;
5555 case '/': xkey = 'o'; break;
5556 case '*': xkey = 'j'; break;
5557 case '-': xkey = 'm'; break;
5558 }
5559 } else {
5560 /*
5561 * In all other modes, we try to retain the layout of
5562 * the DEC keypad in application mode.
5563 */
5564 switch (keysym) {
5565 case PK_KPBIGPLUS:
5566 /* This key covers the '-' and ',' keys on a VT220 */
5567 if (modifiers & PKM_SHIFT)
5568 xkey = 'm'; /* VT220 '-' */
5569 else
5570 xkey = 'l'; /* VT220 ',' */
5571 break;
5572 case PK_KPMINUS: xkey = 'm'; break;
5573 case PK_KPCOMMA: xkey = 'l'; break;
bf02e82e 5574 default: break; /* else gcc warns `enum value not used' */
6c50d421 5575 }
5576 }
5577 }
5578 if (xkey) {
5579 if (term->vt52_mode) {
5580 if (xkey >= 'P' && xkey <= 'S')
5581 p += sprintf((char *) p, "\x1B%c", xkey);
5582 else
5583 p += sprintf((char *) p, "\x1B?%c", xkey);
5584 } else
5585 p += sprintf((char *) p, "\x1BO%c", xkey);
5586 goto done;
5587 }
5588 /* Not in application mode -- treat the number pad as arrow keys? */
5589 if ((flags & PKF_NUMLOCK) == 0) {
5590 switch (keysym) {
5591 case PK_KP0: keysym = PK_INSERT; break;
5592 case PK_KP1: keysym = PK_END; break;
5593 case PK_KP2: keysym = PK_DOWN; break;
5594 case PK_KP3: keysym = PK_PAGEDOWN; break;
5595 case PK_KP4: keysym = PK_LEFT; break;
5596 case PK_KP5: keysym = PK_REST; break;
5597 case PK_KP6: keysym = PK_RIGHT; break;
5598 case PK_KP7: keysym = PK_HOME; break;
5599 case PK_KP8: keysym = PK_UP; break;
5600 case PK_KP9: keysym = PK_PAGEUP; break;
bf02e82e 5601 default: break; /* else gcc warns `enum value not used' */
6c50d421 5602 }
5603 }
5604 }
5605
5606 /* Miscellaneous keys */
5607 switch (keysym) {
5608 case PK_ESCAPE:
5609 *p++ = 0x1b;
5610 goto done;
5611 case PK_BACKSPACE:
5612 if (modifiers == 0)
5613 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5614 else if (modifiers == PKM_SHIFT)
5615 /* We do the opposite of what is configured */
5616 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5617 else break;
5618 goto done;
5619 case PK_TAB:
5620 if (modifiers == 0)
5621 *p++ = 0x09;
5622 else if (modifiers == PKM_SHIFT)
5623 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5624 else break;
5625 goto done;
5626 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5627 case PK_PAUSE:
5628 if (modifiers == PKM_CONTROL)
5629 *p++ = 26;
5630 else break;
5631 goto done;
5632 case PK_RETURN:
5633 case PK_KPENTER: /* Odd keypad modes handled above */
5634 if (modifiers == 0) {
5635 *p++ = 0x0d;
5636 if (term->cr_lf_return)
5637 *p++ = 0x0a;
5638 goto done;
5639 }
bf02e82e 5640 default: break; /* else gcc warns `enum value not used' */
6c50d421 5641 }
5642
5643 /* SCO function keys and editing keys */
5644 if (term->cfg.funky_type == FUNKY_SCO) {
5645 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5646 static char const codes[] =
5647 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5648 int index = keysym - PK_F1;
5649
5650 if (modifiers & PKM_SHIFT) index += 12;
5651 if (modifiers & PKM_CONTROL) index += 24;
5652 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5653 goto done;
5654 }
5655 if (PK_ISEDITING(keysym)) {
5656 int xkey = 0;
5657
5658 switch (keysym) {
5659 case PK_DELETE: *p++ = 0x7f; goto done;
5660 case PK_HOME: xkey = 'H'; break;
5661 case PK_INSERT: xkey = 'L'; break;
5662 case PK_END: xkey = 'F'; break;
5663 case PK_PAGEUP: xkey = 'I'; break;
5664 case PK_PAGEDOWN: xkey = 'G'; break;
bf02e82e 5665 default: break; /* else gcc warns `enum value not used' */
6c50d421 5666 }
5667 p += sprintf((char *) p, "\x1B[%c", xkey);
5668 }
5669 }
5670
5671 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5672 int code;
5673
5674 if (term->cfg.funky_type == FUNKY_XTERM) {
5675 /* Xterm shuffles these keys, apparently. */
5676 switch (keysym) {
5677 case PK_HOME: keysym = PK_INSERT; break;
5678 case PK_INSERT: keysym = PK_HOME; break;
5679 case PK_DELETE: keysym = PK_END; break;
5680 case PK_END: keysym = PK_PAGEUP; break;
5681 case PK_PAGEUP: keysym = PK_DELETE; break;
5682 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
bf02e82e 5683 default: break; /* else gcc warns `enum value not used' */
6c50d421 5684 }
5685 }
5686
5687 /* RXVT Home/End */
5688 if (term->cfg.rxvt_homeend &&
5689 (keysym == PK_HOME || keysym == PK_END)) {
5690 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5691 goto done;
5692 }
5693
5694 if (term->vt52_mode) {
5695 int xkey;
5696
5697 /*
5698 * A real VT52 doesn't have these, and a VT220 doesn't
5699 * send anything for them in VT52 mode.
5700 */
5701 switch (keysym) {
5702 case PK_HOME: xkey = 'H'; break;
5703 case PK_INSERT: xkey = 'L'; break;
5704 case PK_DELETE: xkey = 'M'; break;
5705 case PK_END: xkey = 'E'; break;
5706 case PK_PAGEUP: xkey = 'I'; break;
5707 case PK_PAGEDOWN: xkey = 'G'; break;
7ffdbc1a 5708 default: xkey=0; break; /* else gcc warns `enum value not used'*/
6c50d421 5709 }
5710 p += sprintf((char *) p, "\x1B%c", xkey);
5711 goto done;
5712 }
5713
5714 switch (keysym) {
5715 case PK_HOME: code = 1; break;
5716 case PK_INSERT: code = 2; break;
5717 case PK_DELETE: code = 3; break;
5718 case PK_END: code = 4; break;
5719 case PK_PAGEUP: code = 5; break;
5720 case PK_PAGEDOWN: code = 6; break;
7ffdbc1a 5721 default: code = 0; break; /* else gcc warns `enum value not used' */
6c50d421 5722 }
5723 p += sprintf((char *) p, "\x1B[%d~", code);
5724 goto done;
5725 }
5726
5727 if (PK_ISFKEY(keysym)) {
5728 /* Map Shift+F1-F10 to F11-F20 */
5729 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5730 keysym += 10;
5731 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5732 keysym <= PK_F14) {
5733 /* XXX This overrides the XTERM/VT52 mode below */
5734 int offt = 0;
5735 if (keysym >= PK_F6) offt++;
5736 if (keysym >= PK_F12) offt++;
5737 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5738 'P' + keysym - PK_F1 - offt);
5739 goto done;
5740 }
5741 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5742 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5743 goto done;
5744 }
5745 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5746 if (term->vt52_mode)
5747 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5748 else
5749 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5750 goto done;
5751 }
5752 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5753 goto done;
5754 }
5755
5756 if (PK_ISCURSOR(keysym)) {
5757 int xkey;
5758
5759 switch (keysym) {
5760 case PK_UP: xkey = 'A'; break;
5761 case PK_DOWN: xkey = 'B'; break;
5762 case PK_RIGHT: xkey = 'C'; break;
5763 case PK_LEFT: xkey = 'D'; break;
5764 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5342c096 5765 default: xkey = 0; break; /* else gcc warns `enum value not used' */
6c50d421 5766 }
5767 if (term->vt52_mode)
5768 p += sprintf((char *) p, "\x1B%c", xkey);
5769 else {
5770 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5771
5772 /* Useful mapping of Ctrl-arrows */
5773 if (modifiers == PKM_CONTROL)
5774 app_flg = !app_flg;
5775
5776 if (app_flg)
5777 p += sprintf((char *) p, "\x1BO%c", xkey);
5778 else
5779 p += sprintf((char *) p, "\x1B[%c", xkey);
5780 }
5781 goto done;
5782 }
5783
5784 done:
5785 if (p > output || tlen > 0) {
5786 /*
5787 * Interrupt an ongoing paste. I'm not sure
5788 * this is sensible, but for the moment it's
5789 * preferable to having to faff about buffering
5790 * things.
5791 */
5792 term_nopaste(term);
5793
5794 /*
5795 * We need not bother about stdin backlogs
5796 * here, because in GUI PuTTY we can't do
5797 * anything about it anyway; there's no means
5798 * of asking Windows to hold off on KEYDOWN
5799 * messages. We _have_ to buffer everything
5800 * we're sent.
5801 */
5802 term_seen_key_event(term);
5803
5804 if (prependesc) {
84b8d085 5805#if 0
6c50d421 5806 fprintf(stderr, "sending ESC\n");
84b8d085 5807#endif
6c50d421 5808 ldisc_send(term->ldisc, "\x1b", 1, 1);
5809 }
5810
5811 if (p > output) {
84b8d085 5812#if 0
6c50d421 5813 fprintf(stderr, "sending %d bytes:", p - output);
5814 for (i = 0; i < p - output; i++)
5815 fprintf(stderr, " %02x", output[i]);
5816 fprintf(stderr, "\n");
84b8d085 5817#endif
6c50d421 5818 ldisc_send(term->ldisc, output, p - output, 1);
5819 } else if (tlen > 0) {
84b8d085 5820#if 0
6c50d421 5821 fprintf(stderr, "sending %d unichars:", tlen);
5822 for (i = 0; i < tlen; i++)
bf02e82e 5823 fprintf(stderr, " %04x", (unsigned) text[i]);
6c50d421 5824 fprintf(stderr, "\n");
84b8d085 5825#endif
6c50d421 5826 luni_send(term->ldisc, text, tlen, 1);
5827 }
5828 }
5829}
5830
887035a5 5831void term_nopaste(Terminal *term)
32874aea 5832{
887035a5 5833 if (term->paste_len == 0)
32874aea 5834 return;
887035a5 5835 sfree(term->paste_buffer);
f278d6f8 5836 term->paste_buffer = NULL;
887035a5 5837 term->paste_len = 0;
c9def1b8 5838}
5839
887035a5 5840int term_paste_pending(Terminal *term)
0f660c8f 5841{
887035a5 5842 return term->paste_len != 0;
0f660c8f 5843}
5844
887035a5 5845void term_paste(Terminal *term)
32874aea 5846{
c9def1b8 5847 long now, paste_diff;
5848
887035a5 5849 if (term->paste_len == 0)
32874aea 5850 return;
c9def1b8 5851
5852 /* Don't wait forever to paste */
887035a5 5853 if (term->paste_hold) {
f7f27309 5854 now = GETTICKCOUNT();
887035a5 5855 paste_diff = now - term->last_paste;
32874aea 5856 if (paste_diff >= 0 && paste_diff < 450)
c9def1b8 5857 return;
5858 }
887035a5 5859 term->paste_hold = 0;
c9def1b8 5860
887035a5 5861 while (term->paste_pos < term->paste_len) {
8df7a775 5862 int n = 0;
887035a5 5863 while (n + term->paste_pos < term->paste_len) {
92e23ad9 5864 if (term->paste_buffer[term->paste_pos + n++] == '\015')
8df7a775 5865 break;
5866 }
b9d7bcad 5867 if (term->ldisc)
5868 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
887035a5 5869 term->paste_pos += n;
c9def1b8 5870
887035a5 5871 if (term->paste_pos < term->paste_len) {
5872 term->paste_hold = 1;
c9def1b8 5873 return;
5874 }
5875 }
887035a5 5876 sfree(term->paste_buffer);
5877 term->paste_buffer = NULL;
5878 term->paste_len = 0;
c9def1b8 5879}
5880
887035a5 5881static void deselect(Terminal *term)
32874aea 5882{
887035a5 5883 term->selstate = NO_SELECTION;
5884 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
374330e2 5885}
5886
887035a5 5887void term_deselect(Terminal *term)
32874aea 5888{
887035a5 5889 deselect(term);
5890 term_update(term);
374330e2 5891}
fe50e814 5892
887035a5 5893int term_ldisc(Terminal *term, int option)
32874aea 5894{
5895 if (option == LD_ECHO)
887035a5 5896 return term->term_echoing;
32874aea 5897 if (option == LD_EDIT)
887035a5 5898 return term->term_editing;
0965bee0 5899 return FALSE;
5900}
5901
fbf6cb3b 5902int term_data(Terminal *term, int is_stderr, const char *data, int len)
32874aea 5903{
887035a5 5904 bufchain_add(&term->inbuf, data, len);
5471d09a 5905
74aca06d 5906 if (!term->in_term_out) {
5907 term->in_term_out = TRUE;
5908 term_blink(term, 1);
5909 term_out(term);
5910 term->in_term_out = FALSE;
5911 }
5912
5471d09a 5913 /*
a748a096 5914 * term_out() always completely empties inbuf. Therefore,
5915 * there's no reason at all to return anything other than zero
5916 * from this function, because there _can't_ be a question of
5917 * the remote side needing to wait until term_out() has cleared
5918 * a backlog.
5919 *
5471d09a 5920 * This is a slightly suboptimal way to deal with SSH2 - in
5921 * principle, the window mechanism would allow us to continue
5922 * to accept data on forwarded ports and X connections even
5923 * while the terminal processing was going slowly - but we
5924 * can't do the 100% right thing without moving the terminal
5925 * processing into a separate thread, and that might hurt
5926 * portability. So we manage stdout buffering the old SSH1 way:
5927 * if the terminal processing goes slowly, the whole SSH
5928 * connection stops accepting data until it's ready.
a748a096 5929 *
5471d09a 5930 * In practice, I can't imagine this causing serious trouble.
5931 */
5932 return 0;
fe50e814 5933}
a8327734 5934
5935void term_provide_logctx(Terminal *term, void *logctx)
5936{
5937 term->logctx = logctx;
5938}