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 )
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 )
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) )
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 */
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)
44 #define TM_PUTTY (0xFFFF)
46 #define compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
59 const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
61 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
62 const wchar_t sel_nl[] = SEL_NL;
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.
70 #define UCSGET(a, x) \
71 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
74 * Detect the various aliases of U+0020 SPACE.
76 #define IS_SPACE_CHR(chr) \
77 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
82 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
85 * Internal prototypes.
87 static void resizeline(Terminal *, termline *, int);
88 static termline *lineptr(Terminal *, int, int, int);
89 static void unlineptr(termline *);
90 static void do_paint(Terminal *, Context, int);
91 static void erase_lots(Terminal *, int, int, int);
92 static void swap_screen(Terminal *, int, int, int);
93 static void update_sbar(Terminal *);
94 static void deselect(Terminal *);
95 static void term_print_finish(Terminal *);
96 #ifdef OPTIMISE_SCROLL
97 static void scroll_display(Terminal *, int, int, int);
98 #endif /* OPTIMISE_SCROLL */
100 static termline *newline(Terminal *term, int cols, int bce)
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);
109 line->cols = line->size = cols;
110 line->lattr = LATTR_NORM;
111 line->temporary = FALSE;
117 static void freeline(termline *line)
125 static void unlineptr(termline *line)
132 * Diagnostic function: verify that a termline has a correct
133 * combining character structure.
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.
140 static void cc_check(termline *line)
142 unsigned char *flags;
145 assert(line->size >= line->cols);
147 flags = snewn(line->size, unsigned char);
149 for (i = 0; i < line->size; i++)
150 flags[i] = (i < line->cols);
152 for (i = 0; i < line->cols; i++) {
154 while (line->chars[j].cc_next) {
155 j += line->chars[j].cc_next;
156 assert(j >= line->cols && j < line->size);
165 assert(j >= line->cols && j < line->size);
168 if (line->chars[j].cc_next)
169 j += line->chars[j].cc_next;
176 for (i = 0; i < line->size; i++)
177 j += (flags[i] != 0);
179 assert(j == line->size);
183 * Add a combining character to a character cell.
185 static void add_cc(termline *line, int col, unsigned long chr)
189 assert(col >= 0 && col < line->cols);
192 * Start by extending the cols array if the free list is empty.
194 if (!line->cc_free) {
196 line->size += 16 + (line->size - line->cols) / 2;
197 line->chars = sresize(line->chars, line->size, termchar);
199 while (n < line->size) {
200 if (n+1 < line->size)
201 line->chars[n].cc_next = 1;
203 line->chars[n].cc_next = 0;
209 * Now walk the cc list of the cell in question.
211 while (line->chars[col].cc_next)
212 col += line->chars[col].cc_next;
215 * `col' now points at the last cc currently in this cell; so
216 * we simply add another one.
218 newcc = line->cc_free;
219 if (line->chars[newcc].cc_next)
220 line->cc_free = newcc + line->chars[newcc].cc_next;
223 line->chars[newcc].cc_next = 0;
224 line->chars[newcc].chr = chr;
225 line->chars[col].cc_next = newcc - col;
227 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
231 * Clear the combining character list in a character cell.
233 static void clear_cc(termline *line, int col)
235 int oldfree, origcol = col;
237 assert(col >= 0 && col < line->cols);
239 if (!line->chars[col].cc_next)
240 return; /* nothing needs doing */
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;
247 line->chars[col].cc_next = oldfree - col;
249 line->chars[col].cc_next = 0;
251 line->chars[origcol].cc_next = 0;
253 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
257 * Compare two character cells for equality. Special case required
258 * in do_paint() where we override what we expect the chr and attr
261 static int termchars_equal_override(termchar *a, termchar *b,
262 unsigned long bchr, unsigned long battr)
267 if (a->attr != battr)
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 */
274 if (a->chr != b->chr)
280 static int termchars_equal(termchar *a, termchar *b)
282 return termchars_equal_override(a, b, b->chr, b->attr);
286 * Copy a character cell. (Requires a pointer to the destination
287 * termline, so as to access its free list.)
289 static void copy_termchar(termline *destline, int x, termchar *src)
291 clear_cc(destline, x);
293 destline->chars[x] = *src; /* copy everything except cc-list */
294 destline->chars[x].cc_next = 0; /* and make sure this is zero */
296 while (src->cc_next) {
298 add_cc(destline, x, src->chr);
301 cc_check(destline); /* XXX-REMOVE-BEFORE-RELEASE */
305 * Move a character cell within its termline.
307 static void move_termchar(termline *line, termchar *dest, termchar *src)
309 /* First clear the cc list from the original char, just in case. */
310 clear_cc(line, dest - line->chars);
312 /* Move the character cell and adjust its cc_next. */
313 *dest = *src; /* copy everything except cc-list */
315 dest->cc_next = src->cc_next - (dest-src);
317 /* Ensure the original cell doesn't have a cc list. */
320 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
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.)
336 static void add(struct buf *b, unsigned char c)
338 if (b->len >= b->size) {
339 b->size = (b->len * 3 / 2) + 512;
340 b->data = sresize(b->data, b->size, unsigned char);
342 b->data[b->len++] = c;
344 static int get(struct buf *b)
346 return b->data[b->len++];
348 static void makerle(struct buf *b, termline *ldata,
349 void (*makeliteral)(struct buf *b, termchar *c,
350 unsigned long *state))
352 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
353 termchar *c = ldata->chars;
354 unsigned long state = 0, oldstate;
361 prevlen = prevpos = 0;
366 makeliteral(b, c++, &state);
367 thislen = b->len - thispos;
368 if (thislen == prevlen &&
369 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
371 * This literal precisely matches the previous one.
372 * Turn it into a run if it's worthwhile.
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
381 if (thislen > 1 || prev2) {
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.
390 hdrsize--; /* remove the literal at prevpos */
394 prevpos -= prevlen;/* and possibly another one */
398 assert(prevpos == hdrpos + 1);
400 b->len = prevpos+prevlen;
402 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
404 b->len = prevpos+prevlen+1;
406 * Terminate the previous run of ordinary
409 assert(hdrsize >= 1 && hdrsize <= 128);
410 b->data[hdrpos] = hdrsize - 1;
413 runlen = prev2 ? 3 : 2;
415 while (n > 0 && runlen < 129) {
419 makeliteral(b, c, &state);
420 tmplen = b->len - tmppos;
422 if (tmplen != thislen ||
423 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
425 break; /* run over */
430 assert(runlen >= 2 && runlen <= 129);
431 b->data[runpos] = runlen + 0x80 - 2;
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.
455 * This character isn't (yet) part of a run. Add it to
459 if (hdrsize == 128) {
460 b->data[hdrpos] = hdrsize - 1;
464 prevlen = prevpos = 0;
473 assert(hdrsize <= 128);
474 b->data[hdrpos] = hdrsize - 1;
479 static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
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:
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
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!)
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
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));
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));
537 *state = c->chr & ~0xFF;
539 static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
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).
548 if (c->attr < 0x8000) {
549 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
550 add(b, (unsigned char)(c->attr & 0xFF));
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));
558 static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)
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
566 * I don't use the stateful encoding in makeliteral_chr.
568 unsigned long zstate;
577 makeliteral_chr(b, c, &zstate);
582 makeliteral_chr(b, &z, &zstate);
585 static termline *decompressline(unsigned char *data, int *bytes_used);
587 static unsigned char *compressline(termline *ldata)
589 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
592 * First, store the column count, 7 bits at a time, least
593 * significant `digit' first, with the high bit set on all but
599 add(b, (unsigned char)((n & 0x7F) | 0x80));
602 add(b, (unsigned char)(n));
606 * Next store the lattrs; same principle.
609 int n = ldata->lattr;
611 add(b, (unsigned char)((n & 0x7F) | 0x80));
614 add(b, (unsigned char)(n));
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.
622 * All of these have a common basic format:
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.
628 * The format of the `literals' varies between the fragments.
630 makerle(b, ldata, makeliteral_chr);
631 makerle(b, ldata, makeliteral_attr);
632 makerle(b, ldata, makeliteral_cc);
635 * Diagnostics: ensure that the compressed data really does
636 * decompress to the right thing.
638 * XXX-REMOVE-BEFORE-RELEASE: This is a bit performance-heavy
639 * to be leaving in production code.
641 #ifndef CHECK_SB_COMPRESSION
647 #ifdef DIAGNOSTIC_SB_COMPRESSION
648 for (i = 0; i < b->len; i++) {
649 printf(" %02x ", b->data[i]);
654 dcl = decompressline(b->data, &dused);
655 assert(b->len == dused);
656 assert(ldata->cols == dcl->cols);
657 assert(ldata->lattr == dcl->lattr);
658 for (i = 0; i < ldata->cols; i++)
659 assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
661 #ifdef DIAGNOSTIC_SB_COMPRESSION
662 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
663 ldata->cols, 4 * ldata->cols, dused,
664 (double)dused / (4 * ldata->cols));
672 * Trim the allocated memory so we don't waste any, and return.
674 return sresize(b->data, b->len, unsigned char);
677 static void readrle(struct buf *b, termline *ldata,
678 void (*readliteral)(struct buf *b, termchar *c,
679 termline *ldata, unsigned long *state))
682 unsigned long state = 0;
684 while (n < ldata->cols) {
690 int pos = b->len, count = hdr + 2 - 0x80;
692 assert(n < ldata->cols);
694 readliteral(b, ldata->chars + n, ldata, &state);
698 /* Just a sequence of consecutive literals. */
702 assert(n < ldata->cols);
703 readliteral(b, ldata->chars + n, ldata, &state);
709 assert(n == ldata->cols);
711 static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,
712 unsigned long *state)
717 * 00000000-0000007F: 0xxxxxxx
718 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
719 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
720 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
721 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
726 c->chr = byte | *state;
727 } else if (byte < 0xC0) {
728 c->chr = (byte &~ 0xC0) << 8;
730 } else if (byte < 0xE0) {
731 c->chr = (byte &~ 0xE0) << 16;
732 c->chr |= get(b) << 8;
734 } else if (byte < 0xF0) {
735 c->chr = (byte &~ 0xF0) << 24;
736 c->chr |= get(b) << 16;
737 c->chr |= get(b) << 8;
740 assert(byte == 0xF0);
741 c->chr = get(b) << 24;
742 c->chr |= get(b) << 16;
743 c->chr |= get(b) << 8;
746 *state = c->chr & ~0xFF;
748 static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,
749 unsigned long *state)
764 static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,
765 unsigned long *state)
768 unsigned long zstate;
769 int x = c - ldata->chars;
775 readliteral_chr(b, &n, ldata, &zstate);
778 add_cc(ldata, x, n.chr);
782 static termline *decompressline(unsigned char *data, int *bytes_used)
784 int ncols, byte, shift;
785 struct buf buffer, *b = &buffer;
792 * First read in the column count.
797 ncols |= (byte & 0x7F) << shift;
799 } while (byte & 0x80);
802 * Now create the output termline.
804 ldata = snew(termline);
805 ldata->chars = snewn(ncols, termchar);
806 ldata->cols = ldata->size = ncols;
807 ldata->temporary = TRUE;
811 * We must set all the cc pointers in ldata->chars to 0 right
812 * now, so that cc diagnostics that verify the integrity of the
813 * whole line will make sense while we're in the middle of
818 for (i = 0; i < ldata->cols; i++)
819 ldata->chars[i].cc_next = 0;
823 * Now read in the lattr.
825 ldata->lattr = shift = 0;
828 ldata->lattr |= (byte & 0x7F) << shift;
830 } while (byte & 0x80);
833 * Now we read in each of the RLE streams in turn.
835 readrle(b, ldata, readliteral_chr);
836 readrle(b, ldata, readliteral_attr);
837 readrle(b, ldata, readliteral_cc);
839 /* Return the number of bytes read, for diagnostic purposes. */
841 *bytes_used = b->len;
847 * Resize a line to make it `cols' columns wide.
849 static void resizeline(Terminal *term, termline *line, int cols)
853 if (line->cols != cols) {
855 oldcols = line->cols;
858 * This line is the wrong length, which probably means it
859 * hasn't been accessed since a resize. Resize it now.
861 * First, go through all the characters that will be thrown
862 * out in the resize (if we're shrinking the line) and
863 * return their cc lists to the cc free list.
865 for (i = cols; i < oldcols; i++)
869 * If we're shrinking the line, we now bodily move the
870 * entire cc section from where it started to where it now
871 * needs to be. (We have to do this before the resize, so
872 * that the data we're copying is still there. However, if
873 * we're expanding, we have to wait until _after_ the
874 * resize so that the space we're copying into is there.)
877 memmove(line->chars + cols, line->chars + oldcols,
878 (line->size - line->cols) * TSIZE);
881 * Now do the actual resize, leaving the _same_ amount of
882 * cc space as there was to begin with.
884 line->size += cols - oldcols;
885 line->chars = sresize(line->chars, line->size, TTYPE);
889 * If we're expanding the line, _now_ we move the cc
893 memmove(line->chars + cols, line->chars + oldcols,
894 (line->size - line->cols) * TSIZE);
897 * Go through what's left of the original line, and adjust
898 * the first cc_next pointer in each list. (All the
899 * subsequent ones are still valid because they are
900 * relative offsets within the cc block.) Also do the same
901 * to the head of the cc_free list.
903 for (i = 0; i < oldcols && i < cols; i++)
904 if (line->chars[i].cc_next)
905 line->chars[i].cc_next += cols - oldcols;
907 line->cc_free += cols - oldcols;
910 * And finally fill in the new space with erase chars. (We
911 * don't have to worry about cc lists here, because we
912 * _know_ the erase char doesn't have one.)
914 for (i = oldcols; i < cols; i++)
915 line->chars[i] = term->basic_erase_char;
917 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
922 * Get the number of lines in the scrollback.
924 static int sblines(Terminal *term)
926 int sblines = count234(term->scrollback);
927 if (term->cfg.erase_to_scrollback &&
928 term->alt_which && term->alt_screen) {
929 sblines += term->alt_sblines;
935 * Retrieve a line of the screen or of the scrollback, according to
936 * whether the y coordinate is non-negative or negative
939 static termline *lineptr(Terminal *term, int y, int lineno, int screen)
946 whichtree = term->screen;
953 if (term->cfg.erase_to_scrollback &&
954 term->alt_which && term->alt_screen) {
955 altlines = term->alt_sblines;
958 whichtree = term->scrollback;
959 treeindex = y + altlines + count234(term->scrollback);
961 whichtree = term->alt_screen;
962 treeindex = y + term->alt_sblines;
963 /* treeindex = y + count234(term->alt_screen); */
966 if (whichtree == term->scrollback) {
967 unsigned char *cline = index234(whichtree, treeindex);
968 line = decompressline(cline, NULL);
970 line = index234(whichtree, treeindex);
973 /* We assume that we don't screw up and retrieve something out of range. */
974 assert(line != NULL);
976 resizeline(term, line, term->cols);
977 /* FIXME: should we sort the compressed scrollback out here? */
982 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
983 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
986 * Set up power-on settings for the terminal.
988 static void power_on(Terminal *term)
990 term->curs.x = term->curs.y = 0;
991 term->alt_x = term->alt_y = 0;
992 term->savecurs.x = term->savecurs.y = 0;
993 term->alt_t = term->marg_t = 0;
994 if (term->rows != -1)
995 term->alt_b = term->marg_b = term->rows - 1;
997 term->alt_b = term->marg_b = 0;
998 if (term->cols != -1) {
1000 for (i = 0; i < term->cols; i++)
1001 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1003 term->alt_om = term->dec_om = term->cfg.dec_om;
1004 term->alt_ins = term->insert = FALSE;
1005 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
1006 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1007 term->alt_cset = term->cset = term->save_cset = 0;
1008 term->alt_utf = term->utf = term->save_utf = 0;
1009 term->utf_state = 0;
1010 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
1011 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
1013 term->in_vbell = FALSE;
1014 term->cursor_on = 1;
1015 term->big_cursor = 0;
1016 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
1017 term->term_editing = term->term_echoing = FALSE;
1018 term->app_cursor_keys = term->cfg.app_cursor;
1019 term->app_keypad_keys = term->cfg.app_keypad;
1020 term->use_bce = term->cfg.bce;
1021 term->blink_is_real = term->cfg.blinktext;
1022 term->erase_char = term->basic_erase_char;
1023 term->alt_which = 0;
1024 term_print_finish(term);
1027 for (i = 0; i < 256; i++)
1028 term->wordness[i] = term->cfg.wordness[i];
1031 swap_screen(term, 1, FALSE, FALSE);
1032 erase_lots(term, FALSE, TRUE, TRUE);
1033 swap_screen(term, 0, FALSE, FALSE);
1034 erase_lots(term, FALSE, TRUE, TRUE);
1039 * Force a screen update.
1041 void term_update(Terminal *term)
1044 ctx = get_ctx(term->frontend);
1046 int need_sbar_update = term->seen_disp_event;
1047 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
1048 term->disptop = 0; /* return to main screen */
1049 term->seen_disp_event = 0;
1050 need_sbar_update = TRUE;
1053 if (need_sbar_update)
1055 do_paint(term, ctx, TRUE);
1056 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
1062 * Called from front end when a keypress occurs, to trigger
1063 * anything magical that needs to happen in that situation.
1065 void term_seen_key_event(Terminal *term)
1068 * On any keypress, clear the bell overload mechanism
1069 * completely, on the grounds that large numbers of
1070 * beeps coming from deliberate key action are likely
1071 * to be intended (e.g. beeps from filename completion
1072 * blocking repeatedly).
1074 term->beep_overloaded = FALSE;
1075 while (term->beephead) {
1076 struct beeptime *tmp = term->beephead;
1077 term->beephead = tmp->next;
1080 term->beeptail = NULL;
1084 * Reset the scrollback on keypress, if we're doing that.
1086 if (term->cfg.scroll_on_key) {
1087 term->disptop = 0; /* return to main screen */
1088 term->seen_disp_event = 1;
1093 * Same as power_on(), but an external function.
1095 void term_pwron(Terminal *term)
1098 if (term->ldisc) /* cause ldisc to notice changes */
1099 ldisc_send(term->ldisc, NULL, 0, 0);
1105 static void set_erase_char(Terminal *term)
1107 term->erase_char = term->basic_erase_char;
1109 term->erase_char.attr = (term->curr_attr &
1110 (ATTR_FGMASK | ATTR_BGMASK));
1114 * When the user reconfigures us, we need to check the forbidden-
1115 * alternate-screen config option, disable raw mouse mode if the
1116 * user has disabled mouse reporting, and abandon a print job if
1117 * the user has disabled printing.
1119 void term_reconfig(Terminal *term, Config *cfg)
1122 * Before adopting the new config, check all those terminal
1123 * settings which control power-on defaults; and if they've
1124 * changed, we will modify the current state as well as the
1125 * default one. The full list is: Auto wrap mode, DEC Origin
1126 * Mode, BCE, blinking text, character classes.
1128 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
1131 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
1132 reset_decom = (term->cfg.dec_om != cfg->dec_om);
1133 reset_bce = (term->cfg.bce != cfg->bce);
1134 reset_blink = (term->cfg.blinktext != cfg->blinktext);
1135 reset_charclass = 0;
1136 for (i = 0; i < lenof(term->cfg.wordness); i++)
1137 if (term->cfg.wordness[i] != cfg->wordness[i])
1138 reset_charclass = 1;
1141 * If the bidi or shaping settings have changed, flush the bidi
1144 if (term->cfg.arabicshaping != cfg->arabicshaping ||
1145 term->cfg.bidi != cfg->bidi) {
1146 for (i = 0; i < term->bidi_cache_size; i++) {
1147 sfree(term->pre_bidi_cache[i].chars);
1148 sfree(term->post_bidi_cache[i].chars);
1149 term->pre_bidi_cache[i].width = -1;
1150 term->pre_bidi_cache[i].chars = NULL;
1151 term->post_bidi_cache[i].width = -1;
1152 term->post_bidi_cache[i].chars = NULL;
1156 term->cfg = *cfg; /* STRUCTURE COPY */
1159 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1161 term->alt_om = term->dec_om = term->cfg.dec_om;
1163 term->use_bce = term->cfg.bce;
1164 set_erase_char(term);
1167 term->blink_is_real = term->cfg.blinktext;
1168 if (reset_charclass)
1169 for (i = 0; i < 256; i++)
1170 term->wordness[i] = term->cfg.wordness[i];
1172 if (term->cfg.no_alt_screen)
1173 swap_screen(term, 0, FALSE, FALSE);
1174 if (term->cfg.no_mouse_rep) {
1175 term->xterm_mouse = 0;
1176 set_raw_mouse_mode(term->frontend, 0);
1178 if (term->cfg.no_remote_charset) {
1179 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
1180 term->sco_acs = term->alt_sco_acs = 0;
1183 if (!*term->cfg.printer) {
1184 term_print_finish(term);
1189 * Clear the scrollback.
1191 void term_clrsb(Terminal *term)
1195 while ((line = delpos234(term->scrollback, 0)) != NULL) {
1196 sfree(line); /* this is compressed data, not a termline */
1198 term->tempsblines = 0;
1199 term->alt_sblines = 0;
1204 * Initialise the terminal.
1206 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
1212 * Allocate a new Terminal structure and initialise the fields
1215 term = snew(Terminal);
1216 term->frontend = frontend;
1217 term->ucsdata = ucsdata;
1218 term->cfg = *mycfg; /* STRUCTURE COPY */
1219 term->logctx = NULL;
1220 term->compatibility_level = TM_PUTTY;
1221 strcpy(term->id_string, "\033[?6c");
1222 term->last_blink = term->last_tblink = 0;
1223 term->paste_buffer = NULL;
1224 term->paste_len = 0;
1225 term->last_paste = 0;
1226 bufchain_init(&term->inbuf);
1227 bufchain_init(&term->printer_buf);
1228 term->printing = term->only_printing = FALSE;
1229 term->print_job = NULL;
1230 term->vt52_mode = FALSE;
1231 term->cr_lf_return = FALSE;
1232 term->seen_disp_event = FALSE;
1233 term->xterm_mouse = term->mouse_is_down = FALSE;
1234 term->reset_132 = FALSE;
1235 term->blinker = term->tblinker = 0;
1236 term->has_focus = 1;
1237 term->repeat_off = FALSE;
1238 term->termstate = TOPLEVEL;
1239 term->selstate = NO_SELECTION;
1242 term->screen = term->alt_screen = term->scrollback = NULL;
1243 term->tempsblines = 0;
1244 term->alt_sblines = 0;
1246 term->disptext = NULL;
1247 term->dispcursx = term->dispcursy = -1;
1250 term->rows = term->cols = -1;
1252 term->beephead = term->beeptail = NULL;
1253 #ifdef OPTIMISE_SCROLL
1254 term->scrollhead = term->scrolltail = NULL;
1255 #endif /* OPTIMISE_SCROLL */
1257 term->lastbeep = FALSE;
1258 term->beep_overloaded = FALSE;
1259 term->attr_mask = 0xffffffff;
1260 term->resize_fn = NULL;
1261 term->resize_ctx = NULL;
1262 term->in_term_out = FALSE;
1264 term->ltemp_size = 0;
1265 term->wcFrom = NULL;
1267 term->wcFromTo_size = 0;
1269 term->bidi_cache_size = 0;
1270 term->pre_bidi_cache = term->post_bidi_cache = NULL;
1273 term->basic_erase_char.chr = CSET_ASCII | ' ';
1274 term->basic_erase_char.attr = ATTR_DEFAULT;
1275 term->basic_erase_char.cc_next = 0;
1276 term->erase_char = term->basic_erase_char;
1281 void term_free(Terminal *term)
1284 struct beeptime *beep;
1287 while ((line = delpos234(term->scrollback, 0)) != NULL)
1288 sfree(line); /* compressed data, not a termline */
1289 freetree234(term->scrollback);
1290 while ((line = delpos234(term->screen, 0)) != NULL)
1292 freetree234(term->screen);
1293 while ((line = delpos234(term->alt_screen, 0)) != NULL)
1295 freetree234(term->alt_screen);
1296 if (term->disptext) {
1297 for (i = 0; i < term->rows; i++)
1298 freeline(term->disptext[i]);
1300 sfree(term->disptext);
1301 while (term->beephead) {
1302 beep = term->beephead;
1303 term->beephead = beep->next;
1306 bufchain_clear(&term->inbuf);
1308 printer_finish_job(term->print_job);
1309 bufchain_clear(&term->printer_buf);
1310 sfree(term->paste_buffer);
1312 sfree(term->wcFrom);
1315 for (i = 0; i < term->bidi_cache_size; i++) {
1316 sfree(term->pre_bidi_cache[i].chars);
1317 sfree(term->post_bidi_cache[i].chars);
1319 sfree(term->pre_bidi_cache);
1320 sfree(term->post_bidi_cache);
1326 * Set up the terminal for a given size.
1328 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
1331 termline **newdisp, *line;
1332 int i, j, oldrows = term->rows;
1334 int save_alt_which = term->alt_which;
1336 if (newrows == term->rows && newcols == term->cols &&
1337 newsavelines == term->savelines)
1338 return; /* nothing to do */
1341 swap_screen(term, 0, FALSE, FALSE);
1343 term->alt_t = term->marg_t = 0;
1344 term->alt_b = term->marg_b = newrows - 1;
1346 if (term->rows == -1) {
1347 term->scrollback = newtree234(NULL);
1348 term->screen = newtree234(NULL);
1349 term->tempsblines = 0;
1354 * Resize the screen and scrollback. We only need to shift
1355 * lines around within our data structures, because lineptr()
1356 * will take care of resizing each individual line if
1359 * - If the new screen is longer, we shunt lines in from temporary
1360 * scrollback if possible, otherwise we add new blank lines at
1363 * - If the new screen is shorter, we remove any blank lines at
1364 * the bottom if possible, otherwise shunt lines above the cursor
1365 * to scrollback if possible, otherwise delete lines below the
1368 * - Then, if the new scrollback length is less than the
1369 * amount of scrollback we actually have, we must throw some
1372 sblen = count234(term->scrollback);
1373 /* Do this loop to expand the screen if newrows > rows */
1374 assert(term->rows == count234(term->screen));
1375 while (term->rows < newrows) {
1376 if (term->tempsblines > 0) {
1377 unsigned char *cline;
1378 /* Insert a line from the scrollback at the top of the screen. */
1379 assert(sblen >= term->tempsblines);
1380 cline = delpos234(term->scrollback, --sblen);
1381 line = decompressline(cline, NULL);
1383 line->temporary = FALSE; /* reconstituted line is now real */
1384 term->tempsblines -= 1;
1385 addpos234(term->screen, line, 0);
1387 term->savecurs.y += 1;
1389 /* Add a new blank line at the bottom of the screen. */
1390 line = newline(term, newcols, FALSE);
1391 addpos234(term->screen, line, count234(term->screen));
1395 /* Do this loop to shrink the screen if newrows < rows */
1396 while (term->rows > newrows) {
1397 if (term->curs.y < term->rows - 1) {
1398 /* delete bottom row, unless it contains the cursor */
1399 sfree(delpos234(term->screen, term->rows - 1));
1401 /* push top row to scrollback */
1402 line = delpos234(term->screen, 0);
1403 addpos234(term->scrollback, compressline(line), sblen++);
1405 term->tempsblines += 1;
1407 term->savecurs.y -= 1;
1411 assert(term->rows == newrows);
1412 assert(count234(term->screen) == newrows);
1414 /* Delete any excess lines from the scrollback. */
1415 while (sblen > newsavelines) {
1416 line = delpos234(term->scrollback, 0);
1420 if (sblen < term->tempsblines)
1421 term->tempsblines = sblen;
1422 assert(count234(term->scrollback) <= newsavelines);
1423 assert(count234(term->scrollback) >= term->tempsblines);
1426 /* Make a new displayed text buffer. */
1427 newdisp = snewn(newrows, termline *);
1428 for (i = 0; i < newrows; i++) {
1429 newdisp[i] = newline(term, newcols, FALSE);
1430 for (j = 0; j < newcols; j++)
1431 newdisp[i]->chars[i].attr = ATTR_INVALID;
1433 if (term->disptext) {
1434 for (i = 0; i < oldrows; i++)
1435 freeline(term->disptext[i]);
1437 sfree(term->disptext);
1438 term->disptext = newdisp;
1439 term->dispcursx = term->dispcursy = -1;
1441 /* Make a new alternate screen. */
1442 newalt = newtree234(NULL);
1443 for (i = 0; i < newrows; i++) {
1444 line = newline(term, newcols, TRUE);
1445 addpos234(newalt, line, i);
1447 if (term->alt_screen) {
1448 while (NULL != (line = delpos234(term->alt_screen, 0)))
1450 freetree234(term->alt_screen);
1452 term->alt_screen = newalt;
1453 term->alt_sblines = 0;
1455 term->tabs = sresize(term->tabs, newcols, unsigned char);
1458 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1459 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1462 /* Check that the cursor positions are still valid. */
1463 if (term->savecurs.y < 0)
1464 term->savecurs.y = 0;
1465 if (term->savecurs.y >= newrows)
1466 term->savecurs.y = newrows - 1;
1467 if (term->curs.y < 0)
1469 if (term->curs.y >= newrows)
1470 term->curs.y = newrows - 1;
1471 if (term->curs.x >= newcols)
1472 term->curs.x = newcols - 1;
1473 term->alt_x = term->alt_y = 0;
1474 term->wrapnext = term->alt_wnext = FALSE;
1476 term->rows = newrows;
1477 term->cols = newcols;
1478 term->savelines = newsavelines;
1480 swap_screen(term, save_alt_which, FALSE, FALSE);
1484 if (term->resize_fn)
1485 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1489 * Hand a function and context pointer to the terminal which it can
1490 * use to notify a back end of resizes.
1492 void term_provide_resize_fn(Terminal *term,
1493 void (*resize_fn)(void *, int, int),
1496 term->resize_fn = resize_fn;
1497 term->resize_ctx = resize_ctx;
1498 if (term->cols > 0 && term->rows > 0)
1499 resize_fn(resize_ctx, term->cols, term->rows);
1502 /* Find the bottom line on the screen that has any content.
1503 * If only the top line has content, returns 0.
1504 * If no lines have content, return -1.
1506 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1509 for (i = count234(screen) - 1; i >= 0; i--) {
1510 termline *line = index234(screen, i);
1512 for (j = 0; j < line->cols; j++)
1513 if (!termchars_equal(&line->chars[j], &term->erase_char))
1515 if (j != line->cols) break;
1521 * Swap screens. If `reset' is TRUE and we have been asked to
1522 * switch to the alternate screen, we must bring most of its
1523 * configuration from the main screen and erase the contents of the
1524 * alternate screen completely. (This is even true if we're already
1525 * on it! Blame xterm.)
1527 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
1533 reset = FALSE; /* do no weird resetting if which==0 */
1535 if (which != term->alt_which) {
1536 term->alt_which = which;
1538 ttr = term->alt_screen;
1539 term->alt_screen = term->screen;
1541 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
1543 if (!reset && !keep_cur_pos)
1544 term->curs.x = term->alt_x;
1547 if (!reset && !keep_cur_pos)
1548 term->curs.y = term->alt_y;
1551 if (!reset) term->marg_t = term->alt_t;
1554 if (!reset) term->marg_b = term->alt_b;
1557 if (!reset) term->dec_om = term->alt_om;
1560 if (!reset) term->wrap = term->alt_wrap;
1563 if (!reset) term->wrapnext = term->alt_wnext;
1564 term->alt_wnext = t;
1566 if (!reset) term->insert = term->alt_ins;
1569 if (!reset) term->cset = term->alt_cset;
1572 if (!reset) term->utf = term->alt_utf;
1575 if (!reset) term->sco_acs = term->alt_sco_acs;
1576 term->alt_sco_acs = t;
1579 if (reset && term->screen) {
1581 * Yes, this _is_ supposed to honour background-colour-erase.
1583 erase_lots(term, FALSE, TRUE, TRUE);
1588 * Update the scroll bar.
1590 static void update_sbar(Terminal *term)
1592 int nscroll = sblines(term);
1593 set_sbar(term->frontend, nscroll + term->rows,
1594 nscroll + term->disptop, term->rows);
1598 * Check whether the region bounded by the two pointers intersects
1599 * the scroll region, and de-select the on-screen selection if so.
1601 static void check_selection(Terminal *term, pos from, pos to)
1603 if (poslt(from, term->selend) && poslt(term->selstart, to))
1608 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1609 * for backward.) `sb' is TRUE if the scrolling is permitted to
1610 * affect the scrollback buffer.
1612 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
1615 int i, seltop, olddisptop, shift;
1617 if (topline != 0 || term->alt_which != 0)
1620 olddisptop = term->disptop;
1624 line = delpos234(term->screen, botline);
1625 resizeline(term, line, term->cols);
1626 for (i = 0; i < term->cols; i++)
1627 copy_termchar(line, i, &term->erase_char);
1628 line->lattr = LATTR_NORM;
1629 addpos234(term->screen, line, topline);
1631 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1633 if (term->selstart.y > botline) {
1634 term->selstart.y = botline + 1;
1635 term->selstart.x = 0;
1638 if (term->selend.y >= topline && term->selend.y <= botline) {
1640 if (term->selend.y > botline) {
1641 term->selend.y = botline + 1;
1650 line = delpos234(term->screen, topline);
1651 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
1652 if (sb && term->savelines > 0) {
1653 int sblen = count234(term->scrollback);
1655 * We must add this line to the scrollback. We'll
1656 * remove a line from the top of the scrollback if
1657 * the scrollback is full.
1659 if (sblen == term->savelines) {
1660 unsigned char *cline;
1663 cline = delpos234(term->scrollback, 0);
1666 term->tempsblines += 1;
1668 addpos234(term->scrollback, compressline(line), sblen);
1670 line = newline(term, term->cols, TRUE);
1673 * If the user is currently looking at part of the
1674 * scrollback, and they haven't enabled any options
1675 * that are going to reset the scrollback as a
1676 * result of this movement, then the chances are
1677 * they'd like to keep looking at the same line. So
1678 * we move their viewpoint at the same rate as the
1679 * scroll, at least until their viewpoint hits the
1680 * top end of the scrollback buffer, at which point
1681 * we don't have the choice any more.
1683 * Thanks to Jan Holmen Holsten for the idea and
1684 * initial implementation.
1686 if (term->disptop > -term->savelines && term->disptop < 0)
1689 resizeline(term, line, term->cols);
1690 for (i = 0; i < term->cols; i++)
1691 copy_termchar(line, i, &term->erase_char);
1692 line->lattr = LATTR_NORM;
1693 addpos234(term->screen, line, botline);
1696 * If the selection endpoints move into the scrollback,
1697 * we keep them moving until they hit the top. However,
1698 * of course, if the line _hasn't_ moved into the
1699 * scrollback then we don't do this, and cut them off
1700 * at the top of the scroll region.
1702 * This applies to selstart and selend (for an existing
1703 * selection), and also selanchor (for one being
1704 * selected as we speak).
1706 seltop = sb ? -term->savelines : topline;
1708 if (term->selstate != NO_SELECTION) {
1709 if (term->selstart.y >= seltop &&
1710 term->selstart.y <= botline) {
1712 if (term->selstart.y < seltop) {
1713 term->selstart.y = seltop;
1714 term->selstart.x = 0;
1717 if (term->selend.y >= seltop && term->selend.y <= botline) {
1719 if (term->selend.y < seltop) {
1720 term->selend.y = seltop;
1724 if (term->selanchor.y >= seltop &&
1725 term->selanchor.y <= botline) {
1726 term->selanchor.y--;
1727 if (term->selanchor.y < seltop) {
1728 term->selanchor.y = seltop;
1729 term->selanchor.x = 0;
1737 #ifdef OPTIMISE_SCROLL
1738 shift += term->disptop - olddisptop;
1739 if (shift < term->rows && shift > -term->rows && shift != 0)
1740 scroll_display(term, topline, botline, shift);
1741 #endif /* OPTIMISE_SCROLL */
1744 #ifdef OPTIMISE_SCROLL
1746 * Add a scroll of a region on the screen into the pending scroll list.
1747 * `lines' is +ve for scrolling forward, -ve for backward.
1749 * If the scroll is on the same area as the last scroll in the list,
1752 static void save_scroll(Terminal *term, int topline, int botline, int lines)
1754 struct scrollregion *newscroll;
1755 if (term->scrolltail &&
1756 term->scrolltail->topline == topline &&
1757 term->scrolltail->botline == botline) {
1758 term->scrolltail->lines += lines;
1760 newscroll = snew(struct scrollregion);
1761 newscroll->topline = topline;
1762 newscroll->botline = botline;
1763 newscroll->lines = lines;
1764 newscroll->next = NULL;
1766 if (!term->scrollhead)
1767 term->scrollhead = newscroll;
1769 term->scrolltail->next = newscroll;
1770 term->scrolltail = newscroll;
1775 * Scroll the physical display, and our conception of it in disptext.
1777 static void scroll_display(Terminal *term, int topline, int botline, int lines)
1779 int distance, nlines, i, j;
1781 distance = lines > 0 ? lines : -lines;
1782 nlines = botline - topline + 1 - distance;
1784 for (i = 0; i < nlines; i++)
1785 for (j = 0; j < term->cols; j++)
1786 copy_termchar(term->disptext[start+i], j,
1787 term->disptext[start+i+distance]->chars+j);
1788 if (term->dispcursy >= 0 &&
1789 term->dispcursy >= topline + distance &&
1790 term->dispcursy < topline + distance + nlines)
1791 term->dispcursy -= distance;
1792 for (i = 0; i < distance; i++)
1793 for (j = 0; j < term->cols; j++)
1794 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
1796 for (i = nlines; i-- ;)
1797 for (j = 0; j < term->cols; j++)
1798 copy_termchar(term->disptext[start+i+distance], j,
1799 term->disptext[start+i]->chars+j);
1800 if (term->dispcursy >= 0 &&
1801 term->dispcursy >= topline &&
1802 term->dispcursy < topline + nlines)
1803 term->dispcursy += distance;
1804 for (i = 0; i < distance; i++)
1805 for (j = 0; j < term->cols; j++)
1806 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
1808 save_scroll(term, topline, botline, lines);
1810 #endif /* OPTIMISE_SCROLL */
1813 * Move the cursor to a given position, clipping at boundaries. We
1814 * may or may not want to clip at the scroll margin: marg_clip is 0
1815 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1816 * even _being_ outside the margins.
1818 static void move(Terminal *term, int x, int y, int marg_clip)
1822 if (x >= term->cols)
1825 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1828 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1834 if (y >= term->rows)
1838 term->wrapnext = FALSE;
1842 * Save or restore the cursor and SGR mode.
1844 static void save_cursor(Terminal *term, int save)
1847 term->savecurs = term->curs;
1848 term->save_attr = term->curr_attr;
1849 term->save_cset = term->cset;
1850 term->save_utf = term->utf;
1851 term->save_wnext = term->wrapnext;
1852 term->save_csattr = term->cset_attr[term->cset];
1853 term->save_sco_acs = term->sco_acs;
1855 term->curs = term->savecurs;
1856 /* Make sure the window hasn't shrunk since the save */
1857 if (term->curs.x >= term->cols)
1858 term->curs.x = term->cols - 1;
1859 if (term->curs.y >= term->rows)
1860 term->curs.y = term->rows - 1;
1862 term->curr_attr = term->save_attr;
1863 term->cset = term->save_cset;
1864 term->utf = term->save_utf;
1865 term->wrapnext = term->save_wnext;
1867 * wrapnext might reset to False if the x position is no
1868 * longer at the rightmost edge.
1870 if (term->wrapnext && term->curs.x < term->cols-1)
1871 term->wrapnext = FALSE;
1872 term->cset_attr[term->cset] = term->save_csattr;
1873 term->sco_acs = term->save_sco_acs;
1874 set_erase_char(term);
1879 * This function is called before doing _anything_ which affects
1880 * only part of a line of text. It is used to mark the boundary
1881 * between two character positions, and it indicates that some sort
1882 * of effect is going to happen on only one side of that boundary.
1884 * The effect of this function is to check whether a CJK
1885 * double-width character is straddling the boundary, and to remove
1886 * it and replace it with two spaces if so. (Of course, one or
1887 * other of those spaces is then likely to be replaced with
1888 * something else again, as a result of whatever happens next.)
1890 * Also, if the boundary is at the right-hand _edge_ of the screen,
1891 * it implies something deliberate is being done to the rightmost
1892 * column position; hence we must clear LATTR_WRAPPED2.
1894 * The input to the function is the coordinates of the _second_
1895 * character of the pair.
1897 static void check_boundary(Terminal *term, int x, int y)
1901 /* Validate input coordinates, just in case. */
1902 if (x == 0 || x > term->cols)
1905 ldata = scrlineptr(y);
1906 if (x == term->cols) {
1907 ldata->lattr &= ~LATTR_WRAPPED2;
1909 if (ldata->chars[x].chr == UCSWIDE) {
1910 clear_cc(ldata, x-1);
1912 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
1913 ldata->chars[x] = ldata->chars[x-1];
1919 * Erase a large portion of the screen: the whole screen, or the
1920 * whole line, or parts thereof.
1922 static void erase_lots(Terminal *term,
1923 int line_only, int from_begin, int to_end)
1927 int erasing_lines_from_top = 0;
1930 start.y = term->curs.y;
1932 end.y = term->curs.y + 1;
1934 erase_lattr = FALSE;
1949 if (!from_begin || !to_end)
1950 check_boundary(term, term->curs.x, term->curs.y);
1951 check_selection(term, start, end);
1953 /* Clear screen also forces a full window redraw, just in case. */
1954 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1955 term_invalidate(term);
1957 /* Lines scrolled away shouldn't be brought back on if the terminal
1959 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1960 erasing_lines_from_top = 1;
1962 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1963 /* If it's a whole number of lines, starting at the top, and
1964 * we're fully erasing them, erase by scrolling and keep the
1965 * lines in the scrollback. */
1966 int scrolllines = end.y;
1967 if (end.y == term->rows) {
1968 /* Shrink until we find a non-empty row.*/
1969 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1971 if (scrolllines > 0)
1972 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1974 termline *ldata = scrlineptr(start.y);
1975 while (poslt(start, end)) {
1976 if (start.x == term->cols) {
1978 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1980 ldata->lattr = LATTR_NORM;
1982 copy_termchar(ldata, start.x, &term->erase_char);
1984 if (incpos(start) && start.y < term->rows) {
1985 ldata = scrlineptr(start.y);
1990 /* After an erase of lines from the top of the screen, we shouldn't
1991 * bring the lines back again if the terminal enlarges (since the user or
1992 * application has explictly thrown them away). */
1993 if (erasing_lines_from_top && !(term->alt_which))
1994 term->tempsblines = 0;
1998 * Insert or delete characters within the current line. n is +ve if
1999 * insertion is desired, and -ve for deletion.
2001 static void insch(Terminal *term, int n)
2003 int dir = (n < 0 ? -1 : +1);
2008 n = (n < 0 ? -n : n);
2009 if (n > term->cols - term->curs.x)
2010 n = term->cols - term->curs.x;
2011 m = term->cols - term->curs.x - n;
2012 cursplus.y = term->curs.y;
2013 cursplus.x = term->curs.x + n;
2014 check_selection(term, term->curs, cursplus);
2015 check_boundary(term, term->curs.x, term->curs.y);
2017 check_boundary(term, term->curs.x + n, term->curs.y);
2018 ldata = scrlineptr(term->curs.y);
2020 for (j = 0; j < m; j++)
2021 move_termchar(ldata,
2022 ldata->chars + term->curs.x + j,
2023 ldata->chars + term->curs.x + j + n);
2025 copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
2028 move_termchar(ldata,
2029 ldata->chars + term->curs.x + j + n,
2030 ldata->chars + term->curs.x + j);
2032 copy_termchar(ldata, term->curs.x + n, &term->erase_char);
2037 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2038 * whether the mode is a DEC private one or a normal one.)
2040 static void toggle_mode(Terminal *term, int mode, int query, int state)
2042 unsigned long ticks;
2046 case 1: /* DECCKM: application cursor keys */
2047 term->app_cursor_keys = state;
2049 case 2: /* DECANM: VT52 mode */
2050 term->vt52_mode = !state;
2051 if (term->vt52_mode) {
2052 term->blink_is_real = FALSE;
2053 term->vt52_bold = FALSE;
2055 term->blink_is_real = term->cfg.blinktext;
2058 case 3: /* DECCOLM: 80/132 columns */
2060 if (!term->cfg.no_remote_resize)
2061 request_resize(term->frontend, state ? 132 : 80, term->rows);
2062 term->reset_132 = state;
2063 term->alt_t = term->marg_t = 0;
2064 term->alt_b = term->marg_b = term->rows - 1;
2065 move(term, 0, 0, 0);
2066 erase_lots(term, FALSE, TRUE, TRUE);
2068 case 5: /* DECSCNM: reverse video */
2070 * Toggle reverse video. If we receive an OFF within the
2071 * visual bell timeout period after an ON, we trigger an
2072 * effective visual bell, so that ESC[?5hESC[?5l will
2073 * always be an actually _visible_ visual bell.
2075 ticks = GETTICKCOUNT();
2076 /* turn off a previous vbell to avoid inconsistencies */
2077 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2078 term->in_vbell = FALSE;
2079 if (term->rvideo && !state && /* we're turning it off... */
2080 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
2081 /* If there's no vbell timeout already, or this one lasts
2082 * longer, replace vbell_timeout with ours. */
2083 if (!term->in_vbell ||
2084 (term->rvbell_startpoint - term->vbell_startpoint <
2086 term->vbell_startpoint = term->rvbell_startpoint;
2087 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
2088 } else if (!term->rvideo && state) {
2089 /* This is an ON, so we notice the time and save it. */
2090 term->rvbell_startpoint = ticks;
2092 term->rvideo = state;
2093 term->seen_disp_event = TRUE;
2097 case 6: /* DECOM: DEC origin mode */
2098 term->dec_om = state;
2100 case 7: /* DECAWM: auto wrap */
2103 case 8: /* DECARM: auto key repeat */
2104 term->repeat_off = !state;
2106 case 10: /* DECEDM: set local edit mode */
2107 term->term_editing = state;
2108 if (term->ldisc) /* cause ldisc to notice changes */
2109 ldisc_send(term->ldisc, NULL, 0, 0);
2111 case 25: /* DECTCEM: enable/disable cursor */
2112 compatibility2(OTHER, VT220);
2113 term->cursor_on = state;
2114 term->seen_disp_event = TRUE;
2116 case 47: /* alternate screen */
2117 compatibility(OTHER);
2119 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
2122 case 1000: /* xterm mouse 1 */
2123 term->xterm_mouse = state ? 1 : 0;
2124 set_raw_mouse_mode(term->frontend, state);
2126 case 1002: /* xterm mouse 2 */
2127 term->xterm_mouse = state ? 2 : 0;
2128 set_raw_mouse_mode(term->frontend, state);
2130 case 1047: /* alternate screen */
2131 compatibility(OTHER);
2133 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
2136 case 1048: /* save/restore cursor */
2137 if (!term->cfg.no_alt_screen)
2138 save_cursor(term, state);
2139 if (!state) term->seen_disp_event = TRUE;
2141 case 1049: /* cursor & alternate screen */
2142 if (state && !term->cfg.no_alt_screen)
2143 save_cursor(term, state);
2144 if (!state) term->seen_disp_event = TRUE;
2145 compatibility(OTHER);
2147 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
2148 if (!state && !term->cfg.no_alt_screen)
2149 save_cursor(term, state);
2154 case 4: /* IRM: set insert mode */
2155 compatibility(VT102);
2156 term->insert = state;
2158 case 12: /* SRM: set echo mode */
2159 term->term_echoing = !state;
2160 if (term->ldisc) /* cause ldisc to notice changes */
2161 ldisc_send(term->ldisc, NULL, 0, 0);
2163 case 20: /* LNM: Return sends ... */
2164 term->cr_lf_return = state;
2166 case 34: /* WYULCURM: Make cursor BIG */
2167 compatibility2(OTHER, VT220);
2168 term->big_cursor = !state;
2173 * Process an OSC sequence: set window title or icon name.
2175 static void do_osc(Terminal *term)
2178 while (term->osc_strlen--)
2179 term->wordness[(unsigned char)
2180 term->osc_string[term->osc_strlen]] = term->esc_args[0];
2182 term->osc_string[term->osc_strlen] = '\0';
2183 switch (term->esc_args[0]) {
2186 if (!term->cfg.no_remote_wintitle)
2187 set_icon(term->frontend, term->osc_string);
2188 if (term->esc_args[0] == 1)
2190 /* fall through: parameter 0 means set both */
2193 if (!term->cfg.no_remote_wintitle)
2194 set_title(term->frontend, term->osc_string);
2201 * ANSI printing routines.
2203 static void term_print_setup(Terminal *term)
2205 bufchain_clear(&term->printer_buf);
2206 term->print_job = printer_start_job(term->cfg.printer);
2208 static void term_print_flush(Terminal *term)
2213 while ((size = bufchain_size(&term->printer_buf)) > 5) {
2214 bufchain_prefix(&term->printer_buf, &data, &len);
2217 printer_job_data(term->print_job, data, len);
2218 bufchain_consume(&term->printer_buf, len);
2221 static void term_print_finish(Terminal *term)
2227 if (!term->printing && !term->only_printing)
2228 return; /* we need do nothing */
2230 term_print_flush(term);
2231 while ((size = bufchain_size(&term->printer_buf)) > 0) {
2232 bufchain_prefix(&term->printer_buf, &data, &len);
2234 if (c == '\033' || c == '\233') {
2235 bufchain_consume(&term->printer_buf, size);
2238 printer_job_data(term->print_job, &c, 1);
2239 bufchain_consume(&term->printer_buf, 1);
2242 printer_finish_job(term->print_job);
2243 term->print_job = NULL;
2244 term->printing = term->only_printing = FALSE;
2248 * Remove everything currently in `inbuf' and stick it up on the
2249 * in-memory display. There's a big state machine in here to
2250 * process escape sequences...
2252 void term_out(Terminal *term)
2256 unsigned char localbuf[256], *chars;
2261 chars = NULL; /* placate compiler warnings */
2262 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
2266 bufchain_prefix(&term->inbuf, &ret, &nchars);
2267 if (nchars > sizeof(localbuf))
2268 nchars = sizeof(localbuf);
2269 memcpy(localbuf, ret, nchars);
2270 bufchain_consume(&term->inbuf, nchars);
2272 assert(chars != NULL);
2278 * Optionally log the session traffic to a file. Useful for
2279 * debugging and possibly also useful for actual logging.
2281 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
2282 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
2288 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2289 * be able to display 8-bit characters, but I'll let that go 'cause
2294 * If we're printing, add the character to the printer
2297 if (term->printing) {
2298 bufchain_add(&term->printer_buf, &c, 1);
2301 * If we're in print-only mode, we use a much simpler
2302 * state machine designed only to recognise the ESC[4i
2303 * termination sequence.
2305 if (term->only_printing) {
2307 term->print_state = 1;
2308 else if (c == (unsigned char)'\233')
2309 term->print_state = 2;
2310 else if (c == '[' && term->print_state == 1)
2311 term->print_state = 2;
2312 else if (c == '4' && term->print_state == 2)
2313 term->print_state = 3;
2314 else if (c == 'i' && term->print_state == 3)
2315 term->print_state = 4;
2317 term->print_state = 0;
2318 if (term->print_state == 4) {
2319 term_print_finish(term);
2325 /* First see about all those translations. */
2326 if (term->termstate == TOPLEVEL) {
2328 switch (term->utf_state) {
2331 /* UTF-8 must be stateless so we ignore iso2022. */
2332 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2333 c = term->ucsdata->unitab_ctrl[c];
2334 else c = ((unsigned char)c) | CSET_ASCII;
2336 } else if ((c & 0xe0) == 0xc0) {
2337 term->utf_size = term->utf_state = 1;
2338 term->utf_char = (c & 0x1f);
2339 } else if ((c & 0xf0) == 0xe0) {
2340 term->utf_size = term->utf_state = 2;
2341 term->utf_char = (c & 0x0f);
2342 } else if ((c & 0xf8) == 0xf0) {
2343 term->utf_size = term->utf_state = 3;
2344 term->utf_char = (c & 0x07);
2345 } else if ((c & 0xfc) == 0xf8) {
2346 term->utf_size = term->utf_state = 4;
2347 term->utf_char = (c & 0x03);
2348 } else if ((c & 0xfe) == 0xfc) {
2349 term->utf_size = term->utf_state = 5;
2350 term->utf_char = (c & 0x01);
2361 if ((c & 0xC0) != 0x80) {
2364 term->utf_state = 0;
2367 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2368 if (--term->utf_state)
2373 /* Is somebody trying to be evil! */
2375 (c < 0x800 && term->utf_size >= 2) ||
2376 (c < 0x10000 && term->utf_size >= 3) ||
2377 (c < 0x200000 && term->utf_size >= 4) ||
2378 (c < 0x4000000 && term->utf_size >= 5))
2381 /* Unicode line separator and paragraph separator are CR-LF */
2382 if (c == 0x2028 || c == 0x2029)
2385 /* High controls are probably a Baaad idea too. */
2389 /* The UTF-16 surrogates are not nice either. */
2390 /* The standard give the option of decoding these:
2391 * I don't want to! */
2392 if (c >= 0xD800 && c < 0xE000)
2395 /* ISO 10646 characters now limited to UTF-16 range. */
2399 /* This is currently a TagPhobic application.. */
2400 if (c >= 0xE0000 && c <= 0xE007F)
2403 /* U+FEFF is best seen as a null. */
2406 /* But U+FFFE is an error. */
2407 if (c == 0xFFFE || c == 0xFFFF)
2412 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2413 else if(term->sco_acs &&
2414 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
2416 if (term->sco_acs == 2) c |= 0x80;
2419 switch (term->cset_attr[term->cset]) {
2421 * Linedraw characters are different from 'ESC ( B'
2422 * only for a small range. For ones outside that
2423 * range, make sure we use the same font as well as
2424 * the same encoding.
2427 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2428 c = term->ucsdata->unitab_ctrl[c];
2430 c = ((unsigned char) c) | CSET_LINEDRW;
2434 /* If UK-ASCII, make the '#' a LineDraw Pound */
2436 c = '}' | CSET_LINEDRW;
2439 /*FALLTHROUGH*/ case CSET_ASCII:
2440 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2441 c = term->ucsdata->unitab_ctrl[c];
2443 c = ((unsigned char) c) | CSET_ASCII;
2446 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
2452 /* How about C1 controls ? */
2453 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2454 !term->vt52_mode && has_compat(VT220)) {
2455 term->termstate = SEEN_ESC;
2456 term->esc_query = FALSE;
2457 c = '@' + (c & 0x1F);
2460 /* Or the GL control. */
2461 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2462 if (term->curs.x && !term->wrapnext)
2464 term->wrapnext = FALSE;
2465 /* destructive backspace might be disabled */
2466 if (!term->cfg.no_dbackspace) {
2467 copy_termchar(scrlineptr(term->curs.y),
2468 term->curs.x, &term->erase_char);
2471 /* Or normal C0 controls. */
2472 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
2474 case '\005': /* ENQ: terminal type query */
2475 /* Strictly speaking this is VT100 but a VT100 defaults to
2476 * no response. Other terminals respond at their option.
2478 * Don't put a CR in the default string as this tends to
2479 * upset some weird software.
2481 * An xterm returns "xterm" (5 characters)
2483 compatibility(ANSIMIN);
2485 char abuf[256], *s, *d;
2487 for (s = term->cfg.answerback, d = abuf; *s; s++) {
2489 if (*s >= 'a' && *s <= 'z')
2490 *d++ = (*s - ('a' - 1));
2491 else if ((*s >= '@' && *s <= '_') ||
2492 *s == '?' || (*s & 0x80))
2497 } else if (*s == '^') {
2502 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2506 case '\007': /* BEL: Bell */
2508 struct beeptime *newbeep;
2509 unsigned long ticks;
2511 ticks = GETTICKCOUNT();
2513 if (!term->beep_overloaded) {
2514 newbeep = snew(struct beeptime);
2515 newbeep->ticks = ticks;
2516 newbeep->next = NULL;
2517 if (!term->beephead)
2518 term->beephead = newbeep;
2520 term->beeptail->next = newbeep;
2521 term->beeptail = newbeep;
2526 * Throw out any beeps that happened more than
2529 while (term->beephead &&
2530 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
2531 struct beeptime *tmp = term->beephead;
2532 term->beephead = tmp->next;
2534 if (!term->beephead)
2535 term->beeptail = NULL;
2539 if (term->cfg.bellovl && term->beep_overloaded &&
2540 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
2542 * If we're currently overloaded and the
2543 * last beep was more than s seconds ago,
2544 * leave overload mode.
2546 term->beep_overloaded = FALSE;
2547 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2548 term->nbeeps >= term->cfg.bellovl_n) {
2550 * Now, if we have n or more beeps
2551 * remaining in the queue, go into overload
2554 term->beep_overloaded = TRUE;
2556 term->lastbeep = ticks;
2559 * Perform an actual beep if we're not overloaded.
2561 if (!term->cfg.bellovl || !term->beep_overloaded) {
2562 beep(term->frontend, term->cfg.beep);
2563 if (term->cfg.beep == BELL_VISUAL) {
2564 term->in_vbell = TRUE;
2565 term->vbell_startpoint = ticks;
2569 term->seen_disp_event = TRUE;
2572 case '\b': /* BS: Back space */
2573 if (term->curs.x == 0 &&
2574 (term->curs.y == 0 || term->wrap == 0))
2576 else if (term->curs.x == 0 && term->curs.y > 0)
2577 term->curs.x = term->cols - 1, term->curs.y--;
2578 else if (term->wrapnext)
2579 term->wrapnext = FALSE;
2582 term->seen_disp_event = TRUE;
2584 case '\016': /* LS1: Locking-shift one */
2585 compatibility(VT100);
2588 case '\017': /* LS0: Locking-shift zero */
2589 compatibility(VT100);
2592 case '\033': /* ESC: Escape */
2593 if (term->vt52_mode)
2594 term->termstate = VT52_ESC;
2596 compatibility(ANSIMIN);
2597 term->termstate = SEEN_ESC;
2598 term->esc_query = FALSE;
2601 case '\015': /* CR: Carriage return */
2603 term->wrapnext = FALSE;
2604 term->seen_disp_event = TRUE;
2605 term->paste_hold = 0;
2607 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2609 case '\014': /* FF: Form feed */
2610 if (has_compat(SCOANSI)) {
2611 move(term, 0, 0, 0);
2612 erase_lots(term, FALSE, FALSE, TRUE);
2614 term->wrapnext = FALSE;
2615 term->seen_disp_event = 1;
2618 case '\013': /* VT: Line tabulation */
2619 compatibility(VT100);
2620 case '\012': /* LF: Line feed */
2621 if (term->curs.y == term->marg_b)
2622 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2623 else if (term->curs.y < term->rows - 1)
2625 if (term->cfg.lfhascr)
2627 term->wrapnext = FALSE;
2628 term->seen_disp_event = 1;
2629 term->paste_hold = 0;
2631 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2633 case '\t': /* HT: Character tabulation */
2635 pos old_curs = term->curs;
2636 termline *ldata = scrlineptr(term->curs.y);
2640 } while (term->curs.x < term->cols - 1 &&
2641 !term->tabs[term->curs.x]);
2643 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
2644 if (term->curs.x >= term->cols / 2)
2645 term->curs.x = term->cols / 2 - 1;
2647 if (term->curs.x >= term->cols)
2648 term->curs.x = term->cols - 1;
2651 check_selection(term, old_curs, term->curs);
2653 term->seen_disp_event = TRUE;
2657 switch (term->termstate) {
2659 /* Only graphic characters get this far;
2660 * ctrls are stripped above */
2662 termline *cline = scrlineptr(term->curs.y);
2667 width = wcwidth((wchar_t) c);
2669 if (term->wrapnext && term->wrap && width > 0) {
2670 cline->lattr |= LATTR_WRAPPED;
2671 if (term->curs.y == term->marg_b)
2672 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2673 else if (term->curs.y < term->rows - 1)
2676 term->wrapnext = FALSE;
2677 cline = scrlineptr(term->curs.y);
2679 if (term->insert && width > 0)
2681 if (term->selstate != NO_SELECTION) {
2682 pos cursplus = term->curs;
2684 check_selection(term, term->curs, cursplus);
2686 if (((c & CSET_MASK) == CSET_ASCII ||
2687 (c & CSET_MASK) == 0) &&
2689 logtraffic(term->logctx, (unsigned char) c,
2695 * If we're about to display a double-width
2696 * character starting in the rightmost
2697 * column, then we do something special
2698 * instead. We must print a space in the
2699 * last column of the screen, then wrap;
2700 * and we also set LATTR_WRAPPED2 which
2701 * instructs subsequent cut-and-pasting not
2702 * only to splice this line to the one
2703 * after it, but to ignore the space in the
2704 * last character position as well.
2705 * (Because what was actually output to the
2706 * terminal was presumably just a sequence
2707 * of CJK characters, and we don't want a
2708 * space to be pasted in the middle of
2709 * those just because they had the
2710 * misfortune to start in the wrong parity
2711 * column. xterm concurs.)
2713 check_boundary(term, term->curs.x, term->curs.y);
2714 check_boundary(term, term->curs.x+2, term->curs.y);
2715 if (term->curs.x == term->cols-1) {
2716 copy_termchar(cline, term->curs.x,
2718 cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;
2719 if (term->curs.y == term->marg_b)
2720 scroll(term, term->marg_t, term->marg_b,
2722 else if (term->curs.y < term->rows - 1)
2725 /* Now we must check_boundary again, of course. */
2726 check_boundary(term, term->curs.x, term->curs.y);
2727 check_boundary(term, term->curs.x+2, term->curs.y);
2731 clear_cc(cline, term->curs.x);
2732 cline->chars[term->curs.x].chr = c;
2733 cline->chars[term->curs.x].attr = term->curr_attr;
2738 clear_cc(cline, term->curs.x);
2739 cline->chars[term->curs.x].chr = UCSWIDE;
2740 cline->chars[term->curs.x].attr = term->curr_attr;
2744 check_boundary(term, term->curs.x, term->curs.y);
2745 check_boundary(term, term->curs.x+1, term->curs.y);
2748 clear_cc(cline, term->curs.x);
2749 cline->chars[term->curs.x].chr = c;
2750 cline->chars[term->curs.x].attr = term->curr_attr;
2754 if (term->curs.x > 0) {
2755 int x = term->curs.x - 1;
2757 /* If we're in wrapnext state, the character
2758 * to combine with is _here_, not to our left. */
2763 * If the previous character is
2764 * UCSWIDE, back up another one.
2766 if (cline->chars[x].chr == UCSWIDE) {
2771 add_cc(cline, x, c);
2772 term->seen_disp_event = 1;
2779 if (term->curs.x == term->cols) {
2781 term->wrapnext = TRUE;
2782 if (term->wrap && term->vt52_mode) {
2783 cline->lattr |= LATTR_WRAPPED;
2784 if (term->curs.y == term->marg_b)
2785 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2786 else if (term->curs.y < term->rows - 1)
2789 term->wrapnext = FALSE;
2792 term->seen_disp_event = 1;
2798 * This state is virtually identical to SEEN_ESC, with the
2799 * exception that we have an OSC sequence in the pipeline,
2800 * and _if_ we see a backslash, we process it.
2804 term->termstate = TOPLEVEL;
2807 /* else fall through */
2809 if (c >= ' ' && c <= '/') {
2810 if (term->esc_query)
2811 term->esc_query = -1;
2813 term->esc_query = c;
2816 term->termstate = TOPLEVEL;
2817 switch (ANSI(c, term->esc_query)) {
2818 case '[': /* enter CSI mode */
2819 term->termstate = SEEN_CSI;
2820 term->esc_nargs = 1;
2821 term->esc_args[0] = ARG_DEFAULT;
2822 term->esc_query = FALSE;
2824 case ']': /* OSC: xterm escape sequences */
2825 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2826 compatibility(OTHER);
2827 term->termstate = SEEN_OSC;
2828 term->esc_args[0] = 0;
2830 case '7': /* DECSC: save cursor */
2831 compatibility(VT100);
2832 save_cursor(term, TRUE);
2834 case '8': /* DECRC: restore cursor */
2835 compatibility(VT100);
2836 save_cursor(term, FALSE);
2837 term->seen_disp_event = TRUE;
2839 case '=': /* DECKPAM: Keypad application mode */
2840 compatibility(VT100);
2841 term->app_keypad_keys = TRUE;
2843 case '>': /* DECKPNM: Keypad numeric mode */
2844 compatibility(VT100);
2845 term->app_keypad_keys = FALSE;
2847 case 'D': /* IND: exactly equivalent to LF */
2848 compatibility(VT100);
2849 if (term->curs.y == term->marg_b)
2850 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2851 else if (term->curs.y < term->rows - 1)
2853 term->wrapnext = FALSE;
2854 term->seen_disp_event = TRUE;
2856 case 'E': /* NEL: exactly equivalent to CR-LF */
2857 compatibility(VT100);
2859 if (term->curs.y == term->marg_b)
2860 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2861 else if (term->curs.y < term->rows - 1)
2863 term->wrapnext = FALSE;
2864 term->seen_disp_event = TRUE;
2866 case 'M': /* RI: reverse index - backwards LF */
2867 compatibility(VT100);
2868 if (term->curs.y == term->marg_t)
2869 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2870 else if (term->curs.y > 0)
2872 term->wrapnext = FALSE;
2873 term->seen_disp_event = TRUE;
2875 case 'Z': /* DECID: terminal type query */
2876 compatibility(VT100);
2878 ldisc_send(term->ldisc, term->id_string,
2879 strlen(term->id_string), 0);
2881 case 'c': /* RIS: restore power-on settings */
2882 compatibility(VT100);
2884 if (term->ldisc) /* cause ldisc to notice changes */
2885 ldisc_send(term->ldisc, NULL, 0, 0);
2886 if (term->reset_132) {
2887 if (!term->cfg.no_remote_resize)
2888 request_resize(term->frontend, 80, term->rows);
2889 term->reset_132 = 0;
2892 term->seen_disp_event = TRUE;
2894 case 'H': /* HTS: set a tab */
2895 compatibility(VT100);
2896 term->tabs[term->curs.x] = TRUE;
2899 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2900 compatibility(VT100);
2906 for (i = 0; i < term->rows; i++) {
2907 ldata = scrlineptr(i);
2908 for (j = 0; j < term->cols; j++) {
2909 copy_termchar(ldata, j,
2910 &term->basic_erase_char);
2911 ldata->chars[j].chr = 'E';
2913 ldata->lattr = LATTR_NORM;
2916 term->seen_disp_event = TRUE;
2917 scrtop.x = scrtop.y = 0;
2919 scrbot.y = term->rows;
2920 check_selection(term, scrtop, scrbot);
2924 case ANSI('3', '#'):
2925 case ANSI('4', '#'):
2926 case ANSI('5', '#'):
2927 case ANSI('6', '#'):
2928 compatibility(VT100);
2932 switch (ANSI(c, term->esc_query)) {
2933 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2936 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2939 case ANSI('5', '#'): /* DECSWL: normal */
2940 nlattr = LATTR_NORM;
2942 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2943 nlattr = LATTR_WIDE;
2946 scrlineptr(term->curs.y)->lattr = nlattr;
2949 /* GZD4: G0 designate 94-set */
2950 case ANSI('A', '('):
2951 compatibility(VT100);
2952 if (!term->cfg.no_remote_charset)
2953 term->cset_attr[0] = CSET_GBCHR;
2955 case ANSI('B', '('):
2956 compatibility(VT100);
2957 if (!term->cfg.no_remote_charset)
2958 term->cset_attr[0] = CSET_ASCII;
2960 case ANSI('0', '('):
2961 compatibility(VT100);
2962 if (!term->cfg.no_remote_charset)
2963 term->cset_attr[0] = CSET_LINEDRW;
2965 case ANSI('U', '('):
2966 compatibility(OTHER);
2967 if (!term->cfg.no_remote_charset)
2968 term->cset_attr[0] = CSET_SCOACS;
2970 /* G1D4: G1-designate 94-set */
2971 case ANSI('A', ')'):
2972 compatibility(VT100);
2973 if (!term->cfg.no_remote_charset)
2974 term->cset_attr[1] = CSET_GBCHR;
2976 case ANSI('B', ')'):
2977 compatibility(VT100);
2978 if (!term->cfg.no_remote_charset)
2979 term->cset_attr[1] = CSET_ASCII;
2981 case ANSI('0', ')'):
2982 compatibility(VT100);
2983 if (!term->cfg.no_remote_charset)
2984 term->cset_attr[1] = CSET_LINEDRW;
2986 case ANSI('U', ')'):
2987 compatibility(OTHER);
2988 if (!term->cfg.no_remote_charset)
2989 term->cset_attr[1] = CSET_SCOACS;
2991 /* DOCS: Designate other coding system */
2992 case ANSI('8', '%'): /* Old Linux code */
2993 case ANSI('G', '%'):
2994 compatibility(OTHER);
2995 if (!term->cfg.no_remote_charset)
2998 case ANSI('@', '%'):
2999 compatibility(OTHER);
3000 if (!term->cfg.no_remote_charset)
3006 term->termstate = TOPLEVEL; /* default */
3008 if (term->esc_nargs <= ARGS_MAX) {
3009 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
3010 term->esc_args[term->esc_nargs - 1] = 0;
3011 term->esc_args[term->esc_nargs - 1] =
3012 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
3014 term->termstate = SEEN_CSI;
3015 } else if (c == ';') {
3016 if (++term->esc_nargs <= ARGS_MAX)
3017 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
3018 term->termstate = SEEN_CSI;
3019 } else if (c < '@') {
3020 if (term->esc_query)
3021 term->esc_query = -1;
3023 term->esc_query = TRUE;
3025 term->esc_query = c;
3026 term->termstate = SEEN_CSI;
3028 switch (ANSI(c, term->esc_query)) {
3029 case 'A': /* CUU: move up N lines */
3030 move(term, term->curs.x,
3031 term->curs.y - def(term->esc_args[0], 1), 1);
3032 term->seen_disp_event = TRUE;
3034 case 'e': /* VPR: move down N lines */
3035 compatibility(ANSI);
3037 case 'B': /* CUD: Cursor down */
3038 move(term, term->curs.x,
3039 term->curs.y + def(term->esc_args[0], 1), 1);
3040 term->seen_disp_event = TRUE;
3042 case ANSI('c', '>'): /* DA: report xterm version */
3043 compatibility(OTHER);
3044 /* this reports xterm version 136 so that VIM can
3045 use the drag messages from the mouse reporting */
3047 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
3049 case 'a': /* HPR: move right N cols */
3050 compatibility(ANSI);
3052 case 'C': /* CUF: Cursor right */
3053 move(term, term->curs.x + def(term->esc_args[0], 1),
3055 term->seen_disp_event = TRUE;
3057 case 'D': /* CUB: move left N cols */
3058 move(term, term->curs.x - def(term->esc_args[0], 1),
3060 term->seen_disp_event = TRUE;
3062 case 'E': /* CNL: move down N lines and CR */
3063 compatibility(ANSI);
3065 term->curs.y + def(term->esc_args[0], 1), 1);
3066 term->seen_disp_event = TRUE;
3068 case 'F': /* CPL: move up N lines and CR */
3069 compatibility(ANSI);
3071 term->curs.y - def(term->esc_args[0], 1), 1);
3072 term->seen_disp_event = TRUE;
3075 case '`': /* HPA: set horizontal posn */
3076 compatibility(ANSI);
3077 move(term, def(term->esc_args[0], 1) - 1,
3079 term->seen_disp_event = TRUE;
3081 case 'd': /* VPA: set vertical posn */
3082 compatibility(ANSI);
3083 move(term, term->curs.x,
3084 ((term->dec_om ? term->marg_t : 0) +
3085 def(term->esc_args[0], 1) - 1),
3086 (term->dec_om ? 2 : 0));
3087 term->seen_disp_event = TRUE;
3090 case 'f': /* HVP: set horz and vert posns at once */
3091 if (term->esc_nargs < 2)
3092 term->esc_args[1] = ARG_DEFAULT;
3093 move(term, def(term->esc_args[1], 1) - 1,
3094 ((term->dec_om ? term->marg_t : 0) +
3095 def(term->esc_args[0], 1) - 1),
3096 (term->dec_om ? 2 : 0));
3097 term->seen_disp_event = TRUE;
3099 case 'J': /* ED: erase screen or parts of it */
3101 unsigned int i = def(term->esc_args[0], 0) + 1;
3104 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
3107 term->seen_disp_event = TRUE;
3109 case 'K': /* EL: erase line or parts of it */
3111 unsigned int i = def(term->esc_args[0], 0) + 1;
3114 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
3116 term->seen_disp_event = TRUE;
3118 case 'L': /* IL: insert lines */
3119 compatibility(VT102);
3120 if (term->curs.y <= term->marg_b)
3121 scroll(term, term->curs.y, term->marg_b,
3122 -def(term->esc_args[0], 1), FALSE);
3123 term->seen_disp_event = TRUE;
3125 case 'M': /* DL: delete lines */
3126 compatibility(VT102);
3127 if (term->curs.y <= term->marg_b)
3128 scroll(term, term->curs.y, term->marg_b,
3129 def(term->esc_args[0], 1),
3131 term->seen_disp_event = TRUE;
3133 case '@': /* ICH: insert chars */
3134 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3135 compatibility(VT102);
3136 insch(term, def(term->esc_args[0], 1));
3137 term->seen_disp_event = TRUE;
3139 case 'P': /* DCH: delete chars */
3140 compatibility(VT102);
3141 insch(term, -def(term->esc_args[0], 1));
3142 term->seen_disp_event = TRUE;
3144 case 'c': /* DA: terminal type query */
3145 compatibility(VT100);
3146 /* This is the response for a VT102 */
3148 ldisc_send(term->ldisc, term->id_string,
3149 strlen(term->id_string), 0);
3151 case 'n': /* DSR: cursor position query */
3153 if (term->esc_args[0] == 6) {
3155 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
3157 ldisc_send(term->ldisc, buf, strlen(buf), 0);
3158 } else if (term->esc_args[0] == 5) {
3159 ldisc_send(term->ldisc, "\033[0n", 4, 0);
3163 case 'h': /* SM: toggle modes to high */
3165 compatibility(VT100);
3168 for (i = 0; i < term->esc_nargs; i++)
3169 toggle_mode(term, term->esc_args[i],
3170 term->esc_query, TRUE);
3173 case 'i': /* MC: Media copy */
3175 compatibility(VT100);
3177 if (term->esc_nargs != 1) break;
3178 if (term->esc_args[0] == 5 && *term->cfg.printer) {
3179 term->printing = TRUE;
3180 term->only_printing = !term->esc_query;
3181 term->print_state = 0;
3182 term_print_setup(term);
3183 } else if (term->esc_args[0] == 4 &&
3185 term_print_finish(term);
3189 case 'l': /* RM: toggle modes to low */
3191 compatibility(VT100);
3194 for (i = 0; i < term->esc_nargs; i++)
3195 toggle_mode(term, term->esc_args[i],
3196 term->esc_query, FALSE);
3199 case 'g': /* TBC: clear tabs */
3200 compatibility(VT100);
3201 if (term->esc_nargs == 1) {
3202 if (term->esc_args[0] == 0) {
3203 term->tabs[term->curs.x] = FALSE;
3204 } else if (term->esc_args[0] == 3) {
3206 for (i = 0; i < term->cols; i++)
3207 term->tabs[i] = FALSE;
3211 case 'r': /* DECSTBM: set scroll margins */
3212 compatibility(VT100);
3213 if (term->esc_nargs <= 2) {
3215 top = def(term->esc_args[0], 1) - 1;
3216 bot = (term->esc_nargs <= 1
3217 || term->esc_args[1] == 0 ?
3219 def(term->esc_args[1], term->rows)) - 1;
3220 if (bot >= term->rows)
3221 bot = term->rows - 1;
3222 /* VTTEST Bug 9 - if region is less than 2 lines
3223 * don't change region.
3225 if (bot - top > 0) {
3230 * I used to think the cursor should be
3231 * placed at the top of the newly marginned
3232 * area. Apparently not: VMS TPU falls over
3235 * Well actually it should for
3238 term->curs.y = (term->dec_om ?
3240 term->seen_disp_event = TRUE;
3244 case 'm': /* SGR: set graphics rendition */
3247 * A VT100 without the AVO only had one
3248 * attribute, either underline or
3249 * reverse video depending on the
3250 * cursor type, this was selected by
3254 * This is sometimes DIM, eg on the
3257 * This is sometimes INVIS various ANSI.
3259 * This like 22 disables BOLD, DIM and INVIS
3261 * The ANSI colours appear on any
3262 * terminal that has colour (obviously)
3263 * but the interaction between sgr0 and
3264 * the colours varies but is usually
3265 * related to the background colour
3266 * erase item. The interaction between
3267 * colour attributes and the mono ones
3268 * is also very implementation
3271 * The 39 and 49 attributes are likely
3272 * to be unimplemented.
3275 for (i = 0; i < term->esc_nargs; i++) {
3276 switch (def(term->esc_args[i], 0)) {
3277 case 0: /* restore defaults */
3278 term->curr_attr = term->default_attr;
3280 case 1: /* enable bold */
3281 compatibility(VT100AVO);
3282 term->curr_attr |= ATTR_BOLD;
3284 case 21: /* (enable double underline) */
3285 compatibility(OTHER);
3286 case 4: /* enable underline */
3287 compatibility(VT100AVO);
3288 term->curr_attr |= ATTR_UNDER;
3290 case 5: /* enable blink */
3291 compatibility(VT100AVO);
3292 term->curr_attr |= ATTR_BLINK;
3294 case 6: /* SCO light bkgrd */
3295 compatibility(SCOANSI);
3296 term->blink_is_real = FALSE;
3297 term->curr_attr |= ATTR_BLINK;
3299 case 7: /* enable reverse video */
3300 term->curr_attr |= ATTR_REVERSE;
3302 case 10: /* SCO acs off */
3303 compatibility(SCOANSI);
3304 if (term->cfg.no_remote_charset) break;
3305 term->sco_acs = 0; break;
3306 case 11: /* SCO acs on */
3307 compatibility(SCOANSI);
3308 if (term->cfg.no_remote_charset) break;
3309 term->sco_acs = 1; break;
3310 case 12: /* SCO acs on, |0x80 */
3311 compatibility(SCOANSI);
3312 if (term->cfg.no_remote_charset) break;
3313 term->sco_acs = 2; break;
3314 case 22: /* disable bold */
3315 compatibility2(OTHER, VT220);
3316 term->curr_attr &= ~ATTR_BOLD;
3318 case 24: /* disable underline */
3319 compatibility2(OTHER, VT220);
3320 term->curr_attr &= ~ATTR_UNDER;
3322 case 25: /* disable blink */
3323 compatibility2(OTHER, VT220);
3324 term->curr_attr &= ~ATTR_BLINK;
3326 case 27: /* disable reverse video */
3327 compatibility2(OTHER, VT220);
3328 term->curr_attr &= ~ATTR_REVERSE;
3339 term->curr_attr &= ~ATTR_FGMASK;
3341 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
3351 /* xterm-style bright foreground */
3352 term->curr_attr &= ~ATTR_FGMASK;
3354 ((term->esc_args[i] - 90 + 16)
3357 case 39: /* default-foreground */
3358 term->curr_attr &= ~ATTR_FGMASK;
3359 term->curr_attr |= ATTR_DEFFG;
3370 term->curr_attr &= ~ATTR_BGMASK;
3372 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
3382 /* xterm-style bright background */
3383 term->curr_attr &= ~ATTR_BGMASK;
3385 ((term->esc_args[i] - 100 + 16)
3388 case 49: /* default-background */
3389 term->curr_attr &= ~ATTR_BGMASK;
3390 term->curr_attr |= ATTR_DEFBG;
3394 set_erase_char(term);
3397 case 's': /* save cursor */
3398 save_cursor(term, TRUE);
3400 case 'u': /* restore cursor */
3401 save_cursor(term, FALSE);
3402 term->seen_disp_event = TRUE;
3404 case 't': /* DECSLPP: set page size - ie window height */
3406 * VT340/VT420 sequence DECSLPP, DEC only allows values
3407 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3408 * illegal values (eg first arg 1..9) for window changing
3411 if (term->esc_nargs <= 1
3412 && (term->esc_args[0] < 1 ||
3413 term->esc_args[0] >= 24)) {
3414 compatibility(VT340TEXT);
3415 if (!term->cfg.no_remote_resize)
3416 request_resize(term->frontend, term->cols,
3417 def(term->esc_args[0], 24));
3419 } else if (term->esc_nargs >= 1 &&
3420 term->esc_args[0] >= 1 &&
3421 term->esc_args[0] < 24) {
3422 compatibility(OTHER);
3424 switch (term->esc_args[0]) {
3428 set_iconic(term->frontend, FALSE);
3431 set_iconic(term->frontend, TRUE);
3434 if (term->esc_nargs >= 3) {
3435 if (!term->cfg.no_remote_resize)
3436 move_window(term->frontend,
3437 def(term->esc_args[1], 0),
3438 def(term->esc_args[2], 0));
3442 /* We should resize the window to a given
3443 * size in pixels here, but currently our
3444 * resizing code isn't healthy enough to
3449 set_zorder(term->frontend, TRUE);
3452 /* move to bottom */
3453 set_zorder(term->frontend, FALSE);
3456 refresh_window(term->frontend);
3459 if (term->esc_nargs >= 3) {
3460 if (!term->cfg.no_remote_resize)
3461 request_resize(term->frontend,
3462 def(term->esc_args[2], term->cfg.width),
3463 def(term->esc_args[1], term->cfg.height));
3467 if (term->esc_nargs >= 2)
3468 set_zoomed(term->frontend,
3474 ldisc_send(term->ldisc,
3475 is_iconic(term->frontend) ?
3476 "\033[1t" : "\033[2t", 4, 0);
3480 get_window_pos(term->frontend, &x, &y);
3481 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3482 ldisc_send(term->ldisc, buf, len, 0);
3487 get_window_pixels(term->frontend, &x, &y);
3488 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3489 ldisc_send(term->ldisc, buf, len, 0);
3494 len = sprintf(buf, "\033[8;%d;%dt",
3495 term->rows, term->cols);
3496 ldisc_send(term->ldisc, buf, len, 0);
3501 * Hmmm. Strictly speaking we
3502 * should return `the size of the
3503 * screen in characters', but
3504 * that's not easy: (a) window
3505 * furniture being what it is it's
3506 * hard to compute, and (b) in
3507 * resize-font mode maximising the
3508 * window wouldn't change the
3509 * number of characters. *shrug*. I
3510 * think we'll ignore it for the
3511 * moment and see if anyone
3512 * complains, and then ask them
3513 * what they would like it to do.
3518 !term->cfg.no_remote_qtitle) {
3519 p = get_window_title(term->frontend, TRUE);
3521 ldisc_send(term->ldisc, "\033]L", 3, 0);
3522 ldisc_send(term->ldisc, p, len, 0);
3523 ldisc_send(term->ldisc, "\033\\", 2, 0);
3528 !term->cfg.no_remote_qtitle) {
3529 p = get_window_title(term->frontend,FALSE);
3531 ldisc_send(term->ldisc, "\033]l", 3, 0);
3532 ldisc_send(term->ldisc, p, len, 0);
3533 ldisc_send(term->ldisc, "\033\\", 2, 0);
3539 case 'S': /* SU: Scroll up */
3540 compatibility(SCOANSI);
3541 scroll(term, term->marg_t, term->marg_b,
3542 def(term->esc_args[0], 1), TRUE);
3543 term->wrapnext = FALSE;
3544 term->seen_disp_event = TRUE;
3546 case 'T': /* SD: Scroll down */
3547 compatibility(SCOANSI);
3548 scroll(term, term->marg_t, term->marg_b,
3549 -def(term->esc_args[0], 1), TRUE);
3550 term->wrapnext = FALSE;
3551 term->seen_disp_event = TRUE;
3553 case ANSI('|', '*'): /* DECSNLS */
3555 * Set number of lines on screen
3556 * VT420 uses VGA like hardware and can
3557 * support any size in reasonable range
3558 * (24..49 AIUI) with no default specified.
3560 compatibility(VT420);
3561 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
3562 if (!term->cfg.no_remote_resize)
3563 request_resize(term->frontend, term->cols,
3564 def(term->esc_args[0],
3569 case ANSI('|', '$'): /* DECSCPP */
3571 * Set number of columns per page
3572 * Docs imply range is only 80 or 132, but
3575 compatibility(VT340TEXT);
3576 if (term->esc_nargs <= 1) {
3577 if (!term->cfg.no_remote_resize)
3578 request_resize(term->frontend,
3579 def(term->esc_args[0],
3580 term->cfg.width), term->rows);
3584 case 'X': /* ECH: write N spaces w/o moving cursor */
3585 /* XXX VTTEST says this is vt220, vt510 manual
3587 compatibility(ANSIMIN);
3589 int n = def(term->esc_args[0], 1);
3591 int p = term->curs.x;
3592 termline *cline = scrlineptr(term->curs.y);
3594 if (n > term->cols - term->curs.x)
3595 n = term->cols - term->curs.x;
3596 cursplus = term->curs;
3598 check_boundary(term, term->curs.x, term->curs.y);
3599 check_boundary(term, term->curs.x+n, term->curs.y);
3600 check_selection(term, term->curs, cursplus);
3602 copy_termchar(cline, p++,
3604 term->seen_disp_event = TRUE;
3607 case 'x': /* DECREQTPARM: report terminal characteristics */
3608 compatibility(VT100);
3611 int i = def(term->esc_args[0], 0);
3612 if (i == 0 || i == 1) {
3613 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3615 ldisc_send(term->ldisc, buf, 20, 0);
3619 case 'Z': /* CBT: BackTab for xterm */
3620 compatibility(OTHER);
3622 int i = def(term->esc_args[0], 1);
3623 pos old_curs = term->curs;
3625 for(;i>0 && term->curs.x>0; i--) {
3628 } while (term->curs.x >0 &&
3629 !term->tabs[term->curs.x]);
3631 check_selection(term, old_curs, term->curs);
3634 case ANSI('c', '='): /* Hide or Show Cursor */
3635 compatibility(SCOANSI);
3636 switch(term->esc_args[0]) {
3637 case 0: /* hide cursor */
3638 term->cursor_on = FALSE;
3640 case 1: /* restore cursor */
3641 term->big_cursor = FALSE;
3642 term->cursor_on = TRUE;
3644 case 2: /* block cursor */
3645 term->big_cursor = TRUE;
3646 term->cursor_on = TRUE;
3650 case ANSI('C', '='):
3652 * set cursor start on scanline esc_args[0] and
3653 * end on scanline esc_args[1].If you set
3654 * the bottom scan line to a value less than
3655 * the top scan line, the cursor will disappear.
3657 compatibility(SCOANSI);
3658 if (term->esc_nargs >= 2) {
3659 if (term->esc_args[0] > term->esc_args[1])
3660 term->cursor_on = FALSE;
3662 term->cursor_on = TRUE;
3665 case ANSI('D', '='):
3666 compatibility(SCOANSI);
3667 term->blink_is_real = FALSE;
3668 if (term->esc_args[0]>=1)
3669 term->curr_attr |= ATTR_BLINK;
3671 term->curr_attr &= ~ATTR_BLINK;
3673 case ANSI('E', '='):
3674 compatibility(SCOANSI);
3675 term->blink_is_real = (term->esc_args[0] >= 1);
3677 case ANSI('F', '='): /* set normal foreground */
3678 compatibility(SCOANSI);
3679 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3681 (sco2ansicolour[term->esc_args[0] & 0x7] |
3682 ((term->esc_args[0] & 0x8) << 1)) <<
3684 term->curr_attr &= ~ATTR_FGMASK;
3685 term->curr_attr |= colour;
3686 term->default_attr &= ~ATTR_FGMASK;
3687 term->default_attr |= colour;
3690 case ANSI('G', '='): /* set normal background */
3691 compatibility(SCOANSI);
3692 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3694 (sco2ansicolour[term->esc_args[0] & 0x7] |
3695 ((term->esc_args[0] & 0x8) << 1)) <<
3697 term->curr_attr &= ~ATTR_BGMASK;
3698 term->curr_attr |= colour;
3699 term->default_attr &= ~ATTR_BGMASK;
3700 term->default_attr |= colour;
3703 case ANSI('L', '='):
3704 compatibility(SCOANSI);
3705 term->use_bce = (term->esc_args[0] <= 0);
3706 set_erase_char(term);
3708 case ANSI('p', '"'): /* DECSCL: set compat level */
3710 * Allow the host to make this emulator a
3711 * 'perfect' VT102. This first appeared in
3712 * the VT220, but we do need to get back to
3713 * PuTTY mode so I won't check it.
3715 * The arg in 40..42,50 are a PuTTY extension.
3716 * The 2nd arg, 8bit vs 7bit is not checked.
3718 * Setting VT102 mode should also change
3719 * the Fkeys to generate PF* codes as a
3720 * real VT102 has no Fkeys. The VT220 does
3721 * this, F11..F13 become ESC,BS,LF other
3722 * Fkeys send nothing.
3724 * Note ESC c will NOT change this!
3727 switch (term->esc_args[0]) {
3729 term->compatibility_level &= ~TM_VTXXX;
3730 term->compatibility_level |= TM_VT102;
3733 term->compatibility_level &= ~TM_VTXXX;
3734 term->compatibility_level |= TM_VT220;
3738 if (term->esc_args[0] > 60 &&
3739 term->esc_args[0] < 70)
3740 term->compatibility_level |= TM_VTXXX;
3744 term->compatibility_level &= TM_VTXXX;
3747 term->compatibility_level = TM_PUTTY;
3750 term->compatibility_level = TM_SCOANSI;
3754 term->compatibility_level = TM_PUTTY;
3760 /* Change the response to CSI c */
3761 if (term->esc_args[0] == 50) {
3764 strcpy(term->id_string, "\033[?");
3765 for (i = 1; i < term->esc_nargs; i++) {
3767 strcat(term->id_string, ";");
3768 sprintf(lbuf, "%d", term->esc_args[i]);
3769 strcat(term->id_string, lbuf);
3771 strcat(term->id_string, "c");
3774 /* Is this a good idea ?
3775 * Well we should do a soft reset at this point ...
3777 if (!has_compat(VT420) && has_compat(VT100)) {
3778 if (!term->cfg.no_remote_resize) {
3779 if (term->reset_132)
3780 request_resize(132, 24);
3782 request_resize(80, 24);
3790 term->osc_w = FALSE;
3792 case 'P': /* Linux palette sequence */
3793 term->termstate = SEEN_OSC_P;
3794 term->osc_strlen = 0;
3796 case 'R': /* Linux palette reset */
3797 palette_reset(term->frontend);
3798 term_invalidate(term);
3799 term->termstate = TOPLEVEL;
3801 case 'W': /* word-set */
3802 term->termstate = SEEN_OSC_W;
3815 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3819 * Grotty hack to support xterm and DECterm title
3820 * sequences concurrently.
3822 if (term->esc_args[0] == 2) {
3823 term->esc_args[0] = 1;
3826 /* else fall through */
3828 term->termstate = OSC_STRING;
3829 term->osc_strlen = 0;
3834 * This OSC stuff is EVIL. It takes just one character to get into
3835 * sysline mode and it's not initially obvious how to get out.
3836 * So I've added CR and LF as string aborts.
3837 * This shouldn't effect compatibility as I believe embedded
3838 * control characters are supposed to be interpreted (maybe?)
3839 * and they don't display anything useful anyway.
3843 if (c == '\012' || c == '\015') {
3844 term->termstate = TOPLEVEL;
3845 } else if (c == 0234 || c == '\007') {
3847 * These characters terminate the string; ST and BEL
3848 * terminate the sequence and trigger instant
3849 * processing of it, whereas ESC goes back to SEEN_ESC
3850 * mode unless it is followed by \, in which case it is
3851 * synonymous with ST in the first place.
3854 term->termstate = TOPLEVEL;
3855 } else if (c == '\033')
3856 term->termstate = OSC_MAYBE_ST;
3857 else if (term->osc_strlen < OSC_STR_MAX)
3858 term->osc_string[term->osc_strlen++] = (char)c;
3862 int max = (term->osc_strlen == 0 ? 21 : 16);
3864 if ((int)c >= '0' && (int)c <= '9')
3866 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
3868 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
3871 term->termstate = TOPLEVEL;
3874 term->osc_string[term->osc_strlen++] = val;
3875 if (term->osc_strlen >= 7) {
3876 palette_set(term->frontend, term->osc_string[0],
3877 term->osc_string[1] * 16 + term->osc_string[2],
3878 term->osc_string[3] * 16 + term->osc_string[4],
3879 term->osc_string[5] * 16 + term->osc_string[6]);
3880 term_invalidate(term);
3881 term->termstate = TOPLEVEL;
3897 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3900 term->termstate = OSC_STRING;
3901 term->osc_strlen = 0;
3905 term->termstate = TOPLEVEL;
3906 term->seen_disp_event = TRUE;
3909 move(term, term->curs.x, term->curs.y - 1, 1);
3912 move(term, term->curs.x, term->curs.y + 1, 1);
3915 move(term, term->curs.x + 1, term->curs.y, 1);
3918 move(term, term->curs.x - 1, term->curs.y, 1);
3921 * From the VT100 Manual
3922 * NOTE: The special graphics characters in the VT100
3923 * are different from those in the VT52
3925 * From VT102 manual:
3926 * 137 _ Blank - Same
3927 * 140 ` Reserved - Humm.
3928 * 141 a Solid rectangle - Similar
3929 * 142 b 1/ - Top half of fraction for the
3930 * 143 c 3/ - subscript numbers below.
3933 * 146 f Degrees - Same
3934 * 147 g Plus or minus - Same
3936 * 151 i Ellipsis (dots)
3939 * 154 l Bar at scan 0
3940 * 155 m Bar at scan 1
3941 * 156 n Bar at scan 2
3942 * 157 o Bar at scan 3 - Similar
3943 * 160 p Bar at scan 4 - Similar
3944 * 161 q Bar at scan 5 - Similar
3945 * 162 r Bar at scan 6 - Same
3946 * 163 s Bar at scan 7 - Similar
3961 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
3964 term->cset_attr[term->cset = 0] = CSET_ASCII;
3967 move(term, 0, 0, 0);
3970 if (term->curs.y == 0)
3971 scroll(term, 0, term->rows - 1, -1, TRUE);
3972 else if (term->curs.y > 0)
3974 term->wrapnext = FALSE;
3977 erase_lots(term, FALSE, FALSE, TRUE);
3981 erase_lots(term, TRUE, FALSE, TRUE);
3985 /* XXX Print cursor line */
3988 /* XXX Start controller mode */
3991 /* XXX Stop controller mode */
3995 term->termstate = VT52_Y1;
3999 ldisc_send(term->ldisc, "\033/Z", 3, 0);
4002 term->app_keypad_keys = TRUE;
4005 term->app_keypad_keys = FALSE;
4008 /* XXX This should switch to VT100 mode not current or default
4009 * VT mode. But this will only have effect in a VT220+
4012 term->vt52_mode = FALSE;
4013 term->blink_is_real = term->cfg.blinktext;
4017 /* XXX Enter auto print mode */
4020 /* XXX Exit auto print mode */
4023 /* XXX Print screen */
4029 /* compatibility(ATARI) */
4030 move(term, 0, 0, 0);
4031 erase_lots(term, FALSE, FALSE, TRUE);
4035 /* compatibility(ATARI) */
4036 if (term->curs.y <= term->marg_b)
4037 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4040 /* compatibility(ATARI) */
4041 if (term->curs.y <= term->marg_b)
4042 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4045 /* compatibility(ATARI) */
4046 term->termstate = VT52_FG;
4049 /* compatibility(ATARI) */
4050 term->termstate = VT52_BG;
4053 /* compatibility(ATARI) */
4054 erase_lots(term, FALSE, TRUE, FALSE);
4058 /* compatibility(ATARI) */
4059 term->cursor_on = TRUE;
4062 /* compatibility(ATARI) */
4063 term->cursor_on = FALSE;
4065 /* case 'j': Save cursor position - broken on ST */
4066 /* case 'k': Restore cursor position */
4068 /* compatibility(ATARI) */
4069 erase_lots(term, TRUE, TRUE, TRUE);
4071 term->wrapnext = FALSE;
4074 /* compatibility(ATARI) */
4075 erase_lots(term, TRUE, TRUE, FALSE);
4078 /* compatibility(ATARI) */
4079 term->curr_attr |= ATTR_REVERSE;
4082 /* compatibility(ATARI) */
4083 term->curr_attr &= ~ATTR_REVERSE;
4085 case 'v': /* wrap Autowrap on - Wyse style */
4086 /* compatibility(ATARI) */
4089 case 'w': /* Autowrap off */
4090 /* compatibility(ATARI) */
4095 /* compatibility(OTHER) */
4096 term->vt52_bold = FALSE;
4097 term->curr_attr = ATTR_DEFAULT;
4098 set_erase_char(term);
4101 /* compatibility(VI50) */
4102 term->curr_attr |= ATTR_UNDER;
4105 /* compatibility(VI50) */
4106 term->curr_attr &= ~ATTR_UNDER;
4109 /* compatibility(VI50) */
4110 term->vt52_bold = TRUE;
4111 term->curr_attr |= ATTR_BOLD;
4114 /* compatibility(VI50) */
4115 term->vt52_bold = FALSE;
4116 term->curr_attr &= ~ATTR_BOLD;
4122 term->termstate = VT52_Y2;
4123 move(term, term->curs.x, c - ' ', 0);
4126 term->termstate = TOPLEVEL;
4127 move(term, c - ' ', term->curs.y, 0);
4132 term->termstate = TOPLEVEL;
4133 term->curr_attr &= ~ATTR_FGMASK;
4134 term->curr_attr &= ~ATTR_BOLD;
4135 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
4136 if ((c & 0x8) || term->vt52_bold)
4137 term->curr_attr |= ATTR_BOLD;
4139 set_erase_char(term);
4142 term->termstate = TOPLEVEL;
4143 term->curr_attr &= ~ATTR_BGMASK;
4144 term->curr_attr &= ~ATTR_BLINK;
4145 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
4147 /* Note: bold background */
4149 term->curr_attr |= ATTR_BLINK;
4151 set_erase_char(term);
4154 default: break; /* placate gcc warning about enum use */
4156 if (term->selstate != NO_SELECTION) {
4157 pos cursplus = term->curs;
4159 check_selection(term, term->curs, cursplus);
4163 term_print_flush(term);
4164 logflush(term->logctx);
4168 * To prevent having to run the reasonably tricky bidi algorithm
4169 * too many times, we maintain a cache of the last lineful of data
4170 * fed to the algorithm on each line of the display.
4172 static int term_bidi_cache_hit(Terminal *term, int line,
4173 termchar *lbefore, int width)
4177 if (!term->pre_bidi_cache)
4178 return FALSE; /* cache doesn't even exist yet! */
4180 if (line >= term->bidi_cache_size)
4181 return FALSE; /* cache doesn't have this many lines */
4183 if (!term->pre_bidi_cache[line].chars)
4184 return FALSE; /* cache doesn't contain _this_ line */
4186 if (term->pre_bidi_cache[line].width != width)
4187 return FALSE; /* line is wrong width */
4189 for (i = 0; i < width; i++)
4190 if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i))
4191 return FALSE; /* line doesn't match cache */
4193 return TRUE; /* it didn't match. */
4196 static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
4197 termchar *lafter, int width)
4199 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
4200 int j = term->bidi_cache_size;
4201 term->bidi_cache_size = line+1;
4202 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
4203 term->bidi_cache_size,
4204 struct bidi_cache_entry);
4205 term->post_bidi_cache = sresize(term->post_bidi_cache,
4206 term->bidi_cache_size,
4207 struct bidi_cache_entry);
4208 while (j < term->bidi_cache_size) {
4209 term->pre_bidi_cache[j].chars =
4210 term->post_bidi_cache[j].chars = NULL;
4211 term->pre_bidi_cache[j].width =
4212 term->post_bidi_cache[j].width = -1;
4217 sfree(term->pre_bidi_cache[line].chars);
4218 sfree(term->post_bidi_cache[line].chars);
4220 term->pre_bidi_cache[line].width = width;
4221 term->pre_bidi_cache[line].chars = snewn(width, termchar);
4222 term->post_bidi_cache[line].width = width;
4223 term->post_bidi_cache[line].chars = snewn(width, termchar);
4225 memcpy(term->pre_bidi_cache[line].chars, lbefore, width * TSIZE);
4226 memcpy(term->post_bidi_cache[line].chars, lafter, width * TSIZE);
4230 * Given a context, update the window. Out of paranoia, we don't
4231 * allow WM_PAINT responses to do scrolling optimisations.
4233 static void do_paint(Terminal *term, Context ctx, int may_optimise)
4235 int i, it, j, our_curs_y, our_curs_x;
4240 termchar cursor_background;
4241 unsigned long ticks;
4242 #ifdef OPTIMISE_SCROLL
4243 struct scrollregion *sr;
4244 #endif /* OPTIMISE_SCROLL */
4246 cursor_background = term->basic_erase_char;
4249 ch = snewn(chlen, wchar_t);
4252 * Check the visual bell state.
4254 if (term->in_vbell) {
4255 ticks = GETTICKCOUNT();
4256 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
4257 term->in_vbell = FALSE;
4260 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4263 * screen array, disptop, scrtop,
4265 * cfg.blinkpc, blink_is_real, tblinker,
4266 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4269 /* Has the cursor position or type changed ? */
4270 if (term->cursor_on) {
4271 if (term->has_focus) {
4272 if (term->blinker || !term->cfg.blink_cur)
4273 cursor = TATTR_ACTCURS;
4277 cursor = TATTR_PASCURS;
4279 cursor |= TATTR_RIGHTCURS;
4282 our_curs_y = term->curs.y - term->disptop;
4285 * Adjust the cursor position in the case where it's
4286 * resting on the right-hand half of a CJK wide character.
4287 * xterm's behaviour here, which seems adequate to me, is
4288 * to display the cursor covering the _whole_ character,
4289 * exactly as if it were one space to the left.
4291 termline *ldata = lineptr(term->curs.y);
4292 our_curs_x = term->curs.x;
4293 if (our_curs_x > 0 &&
4294 ldata->chars[our_curs_x].chr == UCSWIDE)
4300 * If the cursor is not where it was last time we painted, and
4301 * its previous position is visible on screen, invalidate its
4302 * previous position.
4304 if (term->dispcursy >= 0 &&
4305 (term->curstype != cursor ||
4306 term->dispcursy != our_curs_y ||
4307 term->dispcursx != our_curs_x)) {
4308 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
4311 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
4312 dispcurs[-1].attr |= ATTR_INVALID;
4313 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
4314 dispcurs[1].attr |= ATTR_INVALID;
4315 dispcurs->attr |= ATTR_INVALID;
4319 term->dispcursx = term->dispcursy = -1;
4321 #ifdef OPTIMISE_SCROLL
4323 sr = term->scrollhead;
4325 struct scrollregion *next = sr->next;
4326 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
4330 term->scrollhead = term->scrolltail = NULL;
4331 #endif /* OPTIMISE_SCROLL */
4333 /* The normal screen data */
4334 for (i = 0; i < term->rows; i++) {
4337 int dirty_line, dirty_run, selected;
4338 unsigned long attr = 0, cset = 0;
4339 int updated_line = 0;
4342 int last_run_dirty = 0;
4344 scrpos.y = i + term->disptop;
4345 ldata = lineptr(scrpos.y);
4347 dirty_run = dirty_line = (ldata->lattr !=
4348 term->disptext[i]->lattr);
4349 term->disptext[i]->lattr = ldata->lattr;
4351 /* Do Arabic shaping and bidi. */
4352 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4354 if (!term_bidi_cache_hit(term, i, ldata->chars, term->cols)) {
4356 if (term->wcFromTo_size < term->cols) {
4357 term->wcFromTo_size = term->cols;
4358 term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,
4360 term->wcTo = sresize(term->wcTo, term->wcFromTo_size,
4364 for(it=0; it<term->cols ; it++)
4366 unsigned long uc = (ldata->chars[it].chr);
4368 switch (uc & CSET_MASK) {
4370 if (!term->cfg.rawcnp) {
4371 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4375 uc = term->ucsdata->unitab_line[uc & 0xFF];
4378 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4381 switch (uc & CSET_MASK) {
4383 uc = term->ucsdata->unitab_font[uc & 0xFF];
4386 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4390 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4392 term->wcFrom[it].index = it;
4396 do_bidi(term->wcFrom, term->cols);
4398 /* this is saved iff done from inside the shaping */
4399 if(!term->cfg.bidi && term->cfg.arabicshaping)
4400 for(it=0; it<term->cols; it++)
4401 term->wcTo[it] = term->wcFrom[it];
4403 if(!term->cfg.arabicshaping)
4404 do_shape(term->wcFrom, term->wcTo, term->cols);
4406 if (term->ltemp_size < ldata->size) {
4407 term->ltemp_size = ldata->size;
4408 term->ltemp = sresize(term->ltemp, term->ltemp_size,
4412 memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);
4414 for(it=0; it<term->cols ; it++)
4416 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4417 if (term->ltemp[it].cc_next)
4418 term->ltemp[it].cc_next -=
4419 it - term->wcTo[it].index;
4421 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4422 term->ltemp[it].chr = term->wcTo[it].wc;
4424 term_bidi_cache_store(term, i, ldata->chars,
4425 term->ltemp, ldata->size);
4427 lchars = term->ltemp;
4429 lchars = term->post_bidi_cache[i].chars;
4432 lchars = ldata->chars;
4434 for (j = 0; j < term->cols; j++) {
4435 unsigned long tattr, tchar;
4436 termchar *d = lchars + j;
4437 int break_run, do_copy;
4443 switch (tchar & CSET_MASK) {
4445 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4448 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4451 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
4454 if (j < term->cols-1 && d[1].chr == UCSWIDE)
4457 /* Video reversing things */
4458 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4459 if (term->seltype == LEXICOGRAPHIC)
4460 selected = (posle(term->selstart, scrpos) &&
4461 poslt(scrpos, term->selend));
4463 selected = (posPle(term->selstart, scrpos) &&
4464 posPlt(scrpos, term->selend));
4468 ^ (selected ? ATTR_REVERSE : 0));
4470 /* 'Real' blinking ? */
4471 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4472 if (term->has_focus && term->tblinker) {
4473 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
4475 tattr &= ~ATTR_BLINK;
4479 * Check the font we'll _probably_ be using to see if
4480 * the character is wide when we don't want it to be.
4482 if (tchar != term->disptext[i]->chars[j].chr ||
4483 tattr != (term->disptext[i]->chars[j].attr &~
4485 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
4486 tattr |= ATTR_NARROW;
4487 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
4488 tattr |= ATTR_NARROW;
4490 /* Cursor here ? Save the 'background' */
4491 if (i == our_curs_y && j == our_curs_x) {
4493 cursor_background.chr = tchar;
4494 cursor_background.attr = tattr;
4495 /* For once, this cc_next field is an absolute index in lchars */
4497 cursor_background.cc_next = d->cc_next + j;
4499 cursor_background.cc_next = 0;
4500 term->dispcursx = j;
4501 term->dispcursy = i;
4504 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4507 break_run = ((tattr ^ attr) & term->attr_mask) != 0;
4509 /* Special hack for VT100 Linedraw glyphs */
4510 if (tchar >= 0x23BA && tchar <= 0x23BD)
4514 * Separate out sequences of characters that have the
4515 * same CSET, if that CSET is a magic one.
4517 if (CSET_OF(tchar) != cset)
4521 * Break on both sides of any combined-character cell.
4523 if (d->cc_next != 0 ||
4524 (j > 0 && d[-1].cc_next != 0))
4527 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
4528 if (term->disptext[i]->chars[j].chr == tchar &&
4529 term->disptext[i]->chars[j].attr == tattr)
4531 else if (!dirty_run && ccount == 1)
4536 if ((dirty_run || last_run_dirty) && ccount > 0) {
4537 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4543 cset = CSET_OF(tchar);
4544 if (term->ucsdata->dbcs_screenfont)
4545 last_run_dirty = dirty_run;
4546 dirty_run = dirty_line;
4550 if (!termchars_equal_override(&term->disptext[i]->chars[j],
4556 if (ccount >= chlen) {
4557 chlen = ccount + 256;
4558 ch = sresize(ch, chlen, wchar_t);
4560 ch[ccount++] = (wchar_t) tchar;
4565 while (dd->cc_next) {
4566 unsigned long schar;
4571 switch (schar & CSET_MASK) {
4573 schar = term->ucsdata->unitab_line[schar & 0xFF];
4576 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4579 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4583 if (ccount >= chlen) {
4584 chlen = ccount + 256;
4585 ch = sresize(ch, chlen, wchar_t);
4587 ch[ccount++] = (wchar_t) schar;
4590 attr |= TATTR_COMBINING;
4594 copy_termchar(term->disptext[i], j, d);
4595 term->disptext[i]->chars[j].chr = tchar;
4596 term->disptext[i]->chars[j].attr = tattr;
4599 /* If it's a wide char step along to the next one. */
4600 if (tattr & ATTR_WIDE) {
4601 if (++j < term->cols) {
4604 * By construction above, the cursor should not
4605 * be on the right-hand half of this character.
4608 assert(!(i == our_curs_y && j == our_curs_x));
4609 if (!termchars_equal(&term->disptext[i]->chars[j], d))
4611 copy_termchar(term->disptext[i], j, d);
4615 if (dirty_run && ccount > 0) {
4616 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4620 /* Cursor on this line ? (and changed) */
4621 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
4622 ch[0] = (wchar_t) cursor_background.chr;
4623 attr = cursor_background.attr | cursor;
4626 if (cursor_background.cc_next) {
4627 termchar *dd = ldata->chars + cursor_background.cc_next;
4630 unsigned long schar;
4633 switch (schar & CSET_MASK) {
4635 schar = term->ucsdata->unitab_line[schar & 0xFF];
4638 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4641 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4645 if (ccount >= chlen) {
4646 chlen = ccount + 256;
4647 ch = sresize(ch, chlen, wchar_t);
4649 ch[ccount++] = (wchar_t) schar;
4657 attr |= TATTR_COMBINING;
4660 do_cursor(ctx, our_curs_x, i, ch, ccount, attr, ldata->lattr);
4661 term->curstype = cursor;
4671 * Flick the switch that says if blinking things should be shown or hidden.
4674 void term_blink(Terminal *term, int flg)
4676 long now, blink_diff;
4678 now = GETTICKCOUNT();
4679 blink_diff = now - term->last_tblink;
4681 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4682 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
4683 term->last_tblink = now;
4684 term->tblinker = !term->tblinker;
4689 term->last_blink = now;
4693 blink_diff = now - term->last_blink;
4695 /* Make sure the cursor blinks no faster than system blink rate */
4696 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
4699 term->last_blink = now;
4700 term->blinker = !term->blinker;
4704 * Invalidate the whole screen so it will be repainted in full.
4706 void term_invalidate(Terminal *term)
4710 for (i = 0; i < term->rows; i++)
4711 for (j = 0; j < term->cols; j++)
4712 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4716 * Paint the window in response to a WM_PAINT message.
4718 void term_paint(Terminal *term, Context ctx,
4719 int left, int top, int right, int bottom, int immediately)
4722 if (left < 0) left = 0;
4723 if (top < 0) top = 0;
4724 if (right >= term->cols) right = term->cols-1;
4725 if (bottom >= term->rows) bottom = term->rows-1;
4727 for (i = top; i <= bottom && i < term->rows; i++) {
4728 if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
4729 for (j = left; j <= right && j < term->cols; j++)
4730 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4732 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
4733 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4736 /* This should happen soon enough, also for some reason it sometimes
4737 * fails to actually do anything when re-sizing ... painting the wrong
4741 do_paint (term, ctx, FALSE);
4745 * Attempt to scroll the scrollback. The second parameter gives the
4746 * position we want to scroll to; the first is +1 to denote that
4747 * this position is relative to the beginning of the scrollback, -1
4748 * to denote it is relative to the end, and 0 to denote that it is
4749 * relative to the current position.
4751 void term_scroll(Terminal *term, int rel, int where)
4753 int sbtop = -sblines(term);
4754 #ifdef OPTIMISE_SCROLL
4755 int olddisptop = term->disptop;
4757 #endif /* OPTIMISE_SCROLL */
4759 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4760 if (term->disptop < sbtop)
4761 term->disptop = sbtop;
4762 if (term->disptop > 0)
4765 #ifdef OPTIMISE_SCROLL
4766 shift = (term->disptop - olddisptop);
4767 if (shift < term->rows && shift > -term->rows)
4768 scroll_display(term, 0, term->rows - 1, shift);
4769 #endif /* OPTIMISE_SCROLL */
4773 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
4776 wchar_t *wbptr; /* where next char goes within workbuf */
4778 int wblen = 0; /* workbuf len */
4779 int buflen; /* amount of memory allocated to workbuf */
4781 buflen = 5120; /* Default size */
4782 workbuf = snewn(buflen, wchar_t);
4783 wbptr = workbuf; /* start filling here */
4784 old_top_x = top.x; /* needed for rect==1 */
4786 while (poslt(top, bottom)) {
4788 termline *ldata = lineptr(top.y);
4792 * nlpos will point at the maximum position on this line we
4793 * should copy up to. So we start it at the end of the
4797 nlpos.x = term->cols;
4800 * ... move it backwards if there's unused space at the end
4801 * of the line (and also set `nl' if this is the case,
4802 * because in normal selection mode this means we need a
4803 * newline at the end)...
4805 if (!(ldata->lattr & LATTR_WRAPPED)) {
4806 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
4807 !ldata->chars[nlpos.x - 1].cc_next &&
4810 if (poslt(nlpos, bottom))
4812 } else if (ldata->lattr & LATTR_WRAPPED2) {
4813 /* Ignore the last char on the line in a WRAPPED2 line. */
4818 * ... and then clip it to the terminal x coordinate if
4819 * we're doing rectangular selection. (In this case we
4820 * still did the above, so that copying e.g. the right-hand
4821 * column from a table doesn't fill with spaces on the
4825 if (nlpos.x > bottom.x)
4827 nl = (top.y < bottom.y);
4830 while (poslt(top, bottom) && poslt(top, nlpos)) {
4833 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4835 wchar_t cbuf[16], *p;
4839 if (ldata->chars[x].chr == UCSWIDE) {
4845 int uc = ldata->chars[x].chr;
4847 switch (uc & CSET_MASK) {
4849 if (!term->cfg.rawcnp) {
4850 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4854 uc = term->ucsdata->unitab_line[uc & 0xFF];
4857 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4860 switch (uc & CSET_MASK) {
4862 uc = term->ucsdata->unitab_font[uc & 0xFF];
4865 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4869 set = (uc & CSET_MASK);
4870 c = (uc & ~CSET_MASK);
4874 if (DIRECT_FONT(uc)) {
4875 if (c >= ' ' && c != 0x7F) {
4879 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4881 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
4882 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4886 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4890 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
4897 for (p = cbuf; *p; p++) {
4898 /* Enough overhead for trailing NL and nul */
4899 if (wblen >= buflen - 16) {
4901 workbuf = sresize(workbuf, buflen, wchar_t);
4902 wbptr = workbuf + wblen;
4908 if (ldata->chars[x].cc_next)
4909 x += ldata->chars[x].cc_next;
4917 for (i = 0; i < sel_nl_sz; i++) {
4919 *wbptr++ = sel_nl[i];
4923 top.x = rect ? old_top_x : 0;
4927 #if SELECTION_NUL_TERMINATED
4931 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
4932 if (buflen > 0) /* indicates we allocated this buffer */
4936 void term_copyall(Terminal *term)
4940 tree234 *screen = term->screen;
4941 top.y = -sblines(term);
4943 bottom.y = find_last_nonempty_line(term, screen);
4944 bottom.x = term->cols;
4945 clipme(term, top, bottom, 0, TRUE);
4949 * The wordness array is mainly for deciding the disposition of the
4950 * US-ASCII characters.
4952 static int wordtype(Terminal *term, int uc)
4955 int start, end, ctype;
4957 static const struct ucsword ucs_words[] = {
4963 0x037e, 0x037e, 1}, /* Greek question mark */
4965 0x0387, 0x0387, 1}, /* Greek ano teleia */
4967 0x055a, 0x055f, 1}, /* Armenian punctuation */
4969 0x0589, 0x0589, 1}, /* Armenian full stop */
4971 0x0700, 0x070d, 1}, /* Syriac punctuation */
4973 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4975 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4977 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4979 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4981 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4983 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4985 0x2000, 0x200a, 0}, /* Various spaces */
4987 0x2070, 0x207f, 2}, /* superscript */
4989 0x2080, 0x208f, 2}, /* subscript */
4991 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4993 0x3000, 0x3000, 0}, /* ideographic space */
4995 0x3001, 0x3020, 1}, /* ideographic punctuation */
4997 0x303f, 0x309f, 3}, /* Hiragana */
4999 0x30a0, 0x30ff, 3}, /* Katakana */
5001 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5003 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5005 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5007 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5009 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5011 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5013 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5015 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5017 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5021 const struct ucsword *wptr;
5023 switch (uc & CSET_MASK) {
5025 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
5028 uc = term->ucsdata->unitab_line[uc & 0xFF];
5031 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
5034 switch (uc & CSET_MASK) {
5036 uc = term->ucsdata->unitab_font[uc & 0xFF];
5039 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
5043 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5044 * fail as there's such a thing as a double width space. :-(
5046 if (term->ucsdata->dbcs_screenfont &&
5047 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5051 return term->wordness[uc];
5053 for (wptr = ucs_words; wptr->start; wptr++) {
5054 if (uc >= wptr->start && uc <= wptr->end)
5062 * Spread the selection outwards according to the selection mode.
5064 static pos sel_spread_half(Terminal *term, pos p, int dir)
5068 int topy = -sblines(term);
5070 ldata = lineptr(p.y);
5072 switch (term->selmode) {
5075 * In this mode, every character is a separate unit, except
5076 * for runs of spaces at the end of a non-wrapping line.
5078 if (!(ldata->lattr & LATTR_WRAPPED)) {
5079 termchar *q = ldata->chars + term->cols;
5080 while (q > ldata->chars &&
5081 IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next)
5083 if (q == ldata->chars + term->cols)
5085 if (p.x >= q - ldata->chars)
5086 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
5091 * In this mode, the units are maximal runs of characters
5092 * whose `wordness' has the same value.
5094 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
5097 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
5098 term->cols-1 : term->cols);
5099 if (p.x < maxcols-1) {
5100 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
5105 if (ldata->lattr & LATTR_WRAPPED) {
5107 ldata2 = lineptr(p.y+1);
5108 if (wordtype(term, UCSGET(ldata2->chars, 0))
5125 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
5134 ldata2 = lineptr(p.y-1);
5135 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
5136 term->cols-1 : term->cols);
5137 if (ldata2->lattr & LATTR_WRAPPED) {
5138 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
5156 * In this mode, every line is a unit.
5158 p.x = (dir == -1 ? 0 : term->cols - 1);
5166 static void sel_spread(Terminal *term)
5168 if (term->seltype == LEXICOGRAPHIC) {
5169 term->selstart = sel_spread_half(term, term->selstart, -1);
5170 decpos(term->selend);
5171 term->selend = sel_spread_half(term, term->selend, +1);
5172 incpos(term->selend);
5176 void term_do_paste(Terminal *term)
5181 get_clip(term->frontend, &data, &len);
5182 if (data && len > 0) {
5185 term_seen_key_event(term); /* pasted data counts */
5187 if (term->paste_buffer)
5188 sfree(term->paste_buffer);
5189 term->paste_pos = term->paste_hold = term->paste_len = 0;
5190 term->paste_buffer = snewn(len, wchar_t);
5193 while (p < data + len) {
5194 while (p < data + len &&
5195 !(p <= data + len - sel_nl_sz &&
5196 !memcmp(p, sel_nl, sizeof(sel_nl))))
5201 for (i = 0; i < p - q; i++) {
5202 term->paste_buffer[term->paste_len++] = q[i];
5206 if (p <= data + len - sel_nl_sz &&
5207 !memcmp(p, sel_nl, sizeof(sel_nl))) {
5208 term->paste_buffer[term->paste_len++] = '\015';
5214 /* Assume a small paste will be OK in one go. */
5215 if (term->paste_len < 256) {
5217 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
5218 if (term->paste_buffer)
5219 sfree(term->paste_buffer);
5220 term->paste_buffer = 0;
5221 term->paste_pos = term->paste_hold = term->paste_len = 0;
5224 get_clip(term->frontend, NULL, NULL);
5227 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
5228 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
5232 int raw_mouse = (term->xterm_mouse &&
5233 !term->cfg.no_mouse_rep &&
5234 !(term->cfg.mouse_override && shift));
5235 int default_seltype;
5239 if (a == MA_DRAG && !raw_mouse)
5240 term_scroll(term, 0, -1);
5242 if (y >= term->rows) {
5244 if (a == MA_DRAG && !raw_mouse)
5245 term_scroll(term, 0, +1);
5254 if (x >= term->cols)
5257 selpoint.y = y + term->disptop;
5259 ldata = lineptr(selpoint.y);
5260 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
5265 int encstate = 0, r, c;
5272 encstate = 0x20; /* left button down */
5283 case MBT_WHEEL_DOWN:
5286 default: break; /* placate gcc warning about enum use */
5290 if (term->xterm_mouse == 1)
5296 term->mouse_is_down = 0;
5299 if (term->mouse_is_down == braw)
5301 term->mouse_is_down = braw;
5303 default: break; /* placate gcc warning about enum use */
5312 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
5313 ldisc_send(term->ldisc, abuf, 6, 0);
5319 * Set the selection type (rectangular or normal) at the start
5320 * of a selection attempt, from the state of Alt.
5322 if (!alt ^ !term->cfg.rect_select)
5323 default_seltype = RECTANGULAR;
5325 default_seltype = LEXICOGRAPHIC;
5327 if (term->selstate == NO_SELECTION) {
5328 term->seltype = default_seltype;
5331 if (bcooked == MBT_SELECT && a == MA_CLICK) {
5333 term->selstate = ABOUT_TO;
5334 term->seltype = default_seltype;
5335 term->selanchor = selpoint;
5336 term->selmode = SM_CHAR;
5337 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
5339 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
5340 term->selstate = DRAGGING;
5341 term->selstart = term->selanchor = selpoint;
5342 term->selend = term->selstart;
5343 incpos(term->selend);
5345 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
5346 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
5347 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
5349 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
5350 term->selstate == SELECTED) {
5351 if (term->seltype == LEXICOGRAPHIC) {
5353 * For normal selection, we extend by moving
5354 * whichever end of the current selection is closer
5357 if (posdiff(selpoint, term->selstart) <
5358 posdiff(term->selend, term->selstart) / 2) {
5359 term->selanchor = term->selend;
5360 decpos(term->selanchor);
5362 term->selanchor = term->selstart;
5366 * For rectangular selection, we have a choice of
5367 * _four_ places to put selanchor and selpoint: the
5368 * four corners of the selection.
5370 if (2*selpoint.x < term->selstart.x + term->selend.x)
5371 term->selanchor.x = term->selend.x-1;
5373 term->selanchor.x = term->selstart.x;
5375 if (2*selpoint.y < term->selstart.y + term->selend.y)
5376 term->selanchor.y = term->selend.y;
5378 term->selanchor.y = term->selstart.y;
5380 term->selstate = DRAGGING;
5382 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
5383 term->selanchor = selpoint;
5384 term->selstate = DRAGGING;
5385 if (term->seltype == LEXICOGRAPHIC) {
5387 * For normal selection, we set (selstart,selend) to
5388 * (selpoint,selanchor) in some order.
5390 if (poslt(selpoint, term->selanchor)) {
5391 term->selstart = selpoint;
5392 term->selend = term->selanchor;
5393 incpos(term->selend);
5395 term->selstart = term->selanchor;
5396 term->selend = selpoint;
5397 incpos(term->selend);
5401 * For rectangular selection, we may need to
5402 * interchange x and y coordinates (if the user has
5403 * dragged in the -x and +y directions, or vice versa).
5405 term->selstart.x = min(term->selanchor.x, selpoint.x);
5406 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
5407 term->selstart.y = min(term->selanchor.y, selpoint.y);
5408 term->selend.y = max(term->selanchor.y, selpoint.y);
5411 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
5413 if (term->selstate == DRAGGING) {
5415 * We've completed a selection. We now transfer the
5416 * data to the clipboard.
5418 clipme(term, term->selstart, term->selend,
5419 (term->seltype == RECTANGULAR), FALSE);
5420 term->selstate = SELECTED;
5422 term->selstate = NO_SELECTION;
5423 } else if (bcooked == MBT_PASTE
5425 #if MULTICLICK_ONLY_EVENT
5426 || a == MA_2CLK || a == MA_3CLK
5429 request_paste(term->frontend);
5435 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
5436 unsigned int modifiers, unsigned int flags)
5440 int prependesc = FALSE;
5444 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
5445 for (i = 0; i < tlen; i++)
5446 fprintf(stderr, " %04x", (unsigned)text[i]);
5447 fprintf(stderr, "\n");
5451 if ((flags & PKF_REPEAT) && term->repeat_off)
5454 /* Currently, Meta always just prefixes everything with ESC. */
5455 if (modifiers & PKM_META)
5457 modifiers &= ~PKM_META;
5460 * Alt is only used for Alt+keypad, which isn't supported yet, so
5463 modifiers &= ~PKM_ALT;
5465 /* Standard local function keys */
5466 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
5468 if (keysym == PK_PAGEUP)
5469 /* scroll up one page */;
5470 if (keysym == PK_PAGEDOWN)
5471 /* scroll down on page */;
5472 if (keysym == PK_INSERT)
5473 term_do_paste(term);
5476 if (keysym == PK_PAGEUP)
5477 /* scroll up one line */;
5478 if (keysym == PK_PAGEDOWN)
5479 /* scroll down one line */;
5480 /* Control-Numlock for app-keypad mode switch */
5481 if (keysym == PK_PF1)
5482 term->app_keypad_keys ^= 1;
5486 if (modifiers & PKM_ALT) {
5487 /* Alt+F4 (close) */
5488 /* Alt+Return (full screen) */
5489 /* Alt+Space (system menu) */
5492 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5493 text[0] >= 0x20 && text[0] <= 0x7e) {
5494 /* ASCII chars + Control */
5495 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5496 (text[0] >= 0x61 && text[0] <= 0x7a))
5500 * Control-2 should return ^@ (0x00), Control-6 should return
5501 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5502 * the DOS keyboard handling did it, and we have nothing better
5503 * to do with the key combo in question, we'll also map
5504 * Control-Backquote to ^\ (0x1C).
5507 case ' ': text[0] = 0x00; break;
5508 case '-': text[0] = 0x1f; break;
5509 case '/': text[0] = 0x1f; break;
5510 case '2': text[0] = 0x00; break;
5511 case '3': text[0] = 0x1b; break;
5512 case '4': text[0] = 0x1c; break;
5513 case '5': text[0] = 0x1d; break;
5514 case '6': text[0] = 0x1e; break;
5515 case '7': text[0] = 0x1f; break;
5516 case '8': text[0] = 0x7f; break;
5517 case '`': text[0] = 0x1c; break;
5522 /* Nethack keypad */
5523 if (term->cfg.nethack_keypad) {
5526 case PK_KP1: c = 'b'; break;
5527 case PK_KP2: c = 'j'; break;
5528 case PK_KP3: c = 'n'; break;
5529 case PK_KP4: c = 'h'; break;
5530 case PK_KP5: c = '.'; break;
5531 case PK_KP6: c = 'l'; break;
5532 case PK_KP7: c = 'y'; break;
5533 case PK_KP8: c = 'k'; break;
5534 case PK_KP9: c = 'u'; break;
5535 default: break; /* else gcc warns `enum value not used' */
5539 if (modifiers & PKM_CONTROL)
5541 else if (modifiers & PKM_SHIFT)
5549 /* Numeric Keypad */
5550 if (PK_ISKEYPAD(keysym)) {
5554 * In VT400 mode, PFn always emits an escape sequence. In
5555 * Linux and tilde modes, this only happens in app keypad mode.
5557 if (term->cfg.funky_type == FUNKY_VT400 ||
5558 ((term->cfg.funky_type == FUNKY_LINUX ||
5559 term->cfg.funky_type == FUNKY_TILDE) &&
5560 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5562 case PK_PF1: xkey = 'P'; break;
5563 case PK_PF2: xkey = 'Q'; break;
5564 case PK_PF3: xkey = 'R'; break;
5565 case PK_PF4: xkey = 'S'; break;
5566 default: break; /* else gcc warns `enum value not used' */
5569 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5571 case PK_KP0: xkey = 'p'; break;
5572 case PK_KP1: xkey = 'q'; break;
5573 case PK_KP2: xkey = 'r'; break;
5574 case PK_KP3: xkey = 's'; break;
5575 case PK_KP4: xkey = 't'; break;
5576 case PK_KP5: xkey = 'u'; break;
5577 case PK_KP6: xkey = 'v'; break;
5578 case PK_KP7: xkey = 'w'; break;
5579 case PK_KP8: xkey = 'x'; break;
5580 case PK_KP9: xkey = 'y'; break;
5581 case PK_KPDECIMAL: xkey = 'n'; break;
5582 case PK_KPENTER: xkey = 'M'; break;
5583 default: break; /* else gcc warns `enum value not used' */
5585 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5587 * xterm can't see the layout of the keypad, so it has
5588 * to rely on the X keysyms returned by the keys.
5589 * Hence, we look at the strings here, not the PuTTY
5590 * keysyms (which describe the layout).
5594 if (modifiers & PKM_SHIFT)
5599 case '/': xkey = 'o'; break;
5600 case '*': xkey = 'j'; break;
5601 case '-': xkey = 'm'; break;
5605 * In all other modes, we try to retain the layout of
5606 * the DEC keypad in application mode.
5610 /* This key covers the '-' and ',' keys on a VT220 */
5611 if (modifiers & PKM_SHIFT)
5612 xkey = 'm'; /* VT220 '-' */
5614 xkey = 'l'; /* VT220 ',' */
5616 case PK_KPMINUS: xkey = 'm'; break;
5617 case PK_KPCOMMA: xkey = 'l'; break;
5618 default: break; /* else gcc warns `enum value not used' */
5623 if (term->vt52_mode) {
5624 if (xkey >= 'P' && xkey <= 'S')
5625 p += sprintf((char *) p, "\x1B%c", xkey);
5627 p += sprintf((char *) p, "\x1B?%c", xkey);
5629 p += sprintf((char *) p, "\x1BO%c", xkey);
5632 /* Not in application mode -- treat the number pad as arrow keys? */
5633 if ((flags & PKF_NUMLOCK) == 0) {
5635 case PK_KP0: keysym = PK_INSERT; break;
5636 case PK_KP1: keysym = PK_END; break;
5637 case PK_KP2: keysym = PK_DOWN; break;
5638 case PK_KP3: keysym = PK_PAGEDOWN; break;
5639 case PK_KP4: keysym = PK_LEFT; break;
5640 case PK_KP5: keysym = PK_REST; break;
5641 case PK_KP6: keysym = PK_RIGHT; break;
5642 case PK_KP7: keysym = PK_HOME; break;
5643 case PK_KP8: keysym = PK_UP; break;
5644 case PK_KP9: keysym = PK_PAGEUP; break;
5645 default: break; /* else gcc warns `enum value not used' */
5650 /* Miscellaneous keys */
5657 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5658 else if (modifiers == PKM_SHIFT)
5659 /* We do the opposite of what is configured */
5660 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5666 else if (modifiers == PKM_SHIFT)
5667 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5670 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5672 if (modifiers == PKM_CONTROL)
5677 case PK_KPENTER: /* Odd keypad modes handled above */
5678 if (modifiers == 0) {
5680 if (term->cr_lf_return)
5684 default: break; /* else gcc warns `enum value not used' */
5687 /* SCO function keys and editing keys */
5688 if (term->cfg.funky_type == FUNKY_SCO) {
5689 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5690 static char const codes[] =
5691 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5692 int index = keysym - PK_F1;
5694 if (modifiers & PKM_SHIFT) index += 12;
5695 if (modifiers & PKM_CONTROL) index += 24;
5696 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5699 if (PK_ISEDITING(keysym)) {
5703 case PK_DELETE: *p++ = 0x7f; goto done;
5704 case PK_HOME: xkey = 'H'; break;
5705 case PK_INSERT: xkey = 'L'; break;
5706 case PK_END: xkey = 'F'; break;
5707 case PK_PAGEUP: xkey = 'I'; break;
5708 case PK_PAGEDOWN: xkey = 'G'; break;
5709 default: break; /* else gcc warns `enum value not used' */
5711 p += sprintf((char *) p, "\x1B[%c", xkey);
5715 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5718 if (term->cfg.funky_type == FUNKY_XTERM) {
5719 /* Xterm shuffles these keys, apparently. */
5721 case PK_HOME: keysym = PK_INSERT; break;
5722 case PK_INSERT: keysym = PK_HOME; break;
5723 case PK_DELETE: keysym = PK_END; break;
5724 case PK_END: keysym = PK_PAGEUP; break;
5725 case PK_PAGEUP: keysym = PK_DELETE; break;
5726 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
5727 default: break; /* else gcc warns `enum value not used' */
5732 if (term->cfg.rxvt_homeend &&
5733 (keysym == PK_HOME || keysym == PK_END)) {
5734 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5738 if (term->vt52_mode) {
5742 * A real VT52 doesn't have these, and a VT220 doesn't
5743 * send anything for them in VT52 mode.
5746 case PK_HOME: xkey = 'H'; break;
5747 case PK_INSERT: xkey = 'L'; break;
5748 case PK_DELETE: xkey = 'M'; break;
5749 case PK_END: xkey = 'E'; break;
5750 case PK_PAGEUP: xkey = 'I'; break;
5751 case PK_PAGEDOWN: xkey = 'G'; break;
5752 default: xkey=0; break; /* else gcc warns `enum value not used'*/
5754 p += sprintf((char *) p, "\x1B%c", xkey);
5759 case PK_HOME: code = 1; break;
5760 case PK_INSERT: code = 2; break;
5761 case PK_DELETE: code = 3; break;
5762 case PK_END: code = 4; break;
5763 case PK_PAGEUP: code = 5; break;
5764 case PK_PAGEDOWN: code = 6; break;
5765 default: code = 0; break; /* else gcc warns `enum value not used' */
5767 p += sprintf((char *) p, "\x1B[%d~", code);
5771 if (PK_ISFKEY(keysym)) {
5772 /* Map Shift+F1-F10 to F11-F20 */
5773 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5775 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5777 /* XXX This overrides the XTERM/VT52 mode below */
5779 if (keysym >= PK_F6) offt++;
5780 if (keysym >= PK_F12) offt++;
5781 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5782 'P' + keysym - PK_F1 - offt);
5785 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5786 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5789 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5790 if (term->vt52_mode)
5791 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5793 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5796 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5800 if (PK_ISCURSOR(keysym)) {
5804 case PK_UP: xkey = 'A'; break;
5805 case PK_DOWN: xkey = 'B'; break;
5806 case PK_RIGHT: xkey = 'C'; break;
5807 case PK_LEFT: xkey = 'D'; break;
5808 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5809 default: xkey = 0; break; /* else gcc warns `enum value not used' */
5811 if (term->vt52_mode)
5812 p += sprintf((char *) p, "\x1B%c", xkey);
5814 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5816 /* Useful mapping of Ctrl-arrows */
5817 if (modifiers == PKM_CONTROL)
5821 p += sprintf((char *) p, "\x1BO%c", xkey);
5823 p += sprintf((char *) p, "\x1B[%c", xkey);
5829 if (p > output || tlen > 0) {
5831 * Interrupt an ongoing paste. I'm not sure
5832 * this is sensible, but for the moment it's
5833 * preferable to having to faff about buffering
5839 * We need not bother about stdin backlogs
5840 * here, because in GUI PuTTY we can't do
5841 * anything about it anyway; there's no means
5842 * of asking Windows to hold off on KEYDOWN
5843 * messages. We _have_ to buffer everything
5846 term_seen_key_event(term);
5850 fprintf(stderr, "sending ESC\n");
5852 ldisc_send(term->ldisc, "\x1b", 1, 1);
5857 fprintf(stderr, "sending %d bytes:", p - output);
5858 for (i = 0; i < p - output; i++)
5859 fprintf(stderr, " %02x", output[i]);
5860 fprintf(stderr, "\n");
5862 ldisc_send(term->ldisc, output, p - output, 1);
5863 } else if (tlen > 0) {
5865 fprintf(stderr, "sending %d unichars:", tlen);
5866 for (i = 0; i < tlen; i++)
5867 fprintf(stderr, " %04x", (unsigned) text[i]);
5868 fprintf(stderr, "\n");
5870 luni_send(term->ldisc, text, tlen, 1);
5875 void term_nopaste(Terminal *term)
5877 if (term->paste_len == 0)
5879 sfree(term->paste_buffer);
5880 term->paste_buffer = NULL;
5881 term->paste_len = 0;
5884 int term_paste_pending(Terminal *term)
5886 return term->paste_len != 0;
5889 void term_paste(Terminal *term)
5891 long now, paste_diff;
5893 if (term->paste_len == 0)
5896 /* Don't wait forever to paste */
5897 if (term->paste_hold) {
5898 now = GETTICKCOUNT();
5899 paste_diff = now - term->last_paste;
5900 if (paste_diff >= 0 && paste_diff < 450)
5903 term->paste_hold = 0;
5905 while (term->paste_pos < term->paste_len) {
5907 while (n + term->paste_pos < term->paste_len) {
5908 if (term->paste_buffer[term->paste_pos + n++] == '\015')
5912 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
5913 term->paste_pos += n;
5915 if (term->paste_pos < term->paste_len) {
5916 term->paste_hold = 1;
5920 sfree(term->paste_buffer);
5921 term->paste_buffer = NULL;
5922 term->paste_len = 0;
5925 static void deselect(Terminal *term)
5927 term->selstate = NO_SELECTION;
5928 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
5931 void term_deselect(Terminal *term)
5937 int term_ldisc(Terminal *term, int option)
5939 if (option == LD_ECHO)
5940 return term->term_echoing;
5941 if (option == LD_EDIT)
5942 return term->term_editing;
5946 int term_data(Terminal *term, int is_stderr, const char *data, int len)
5948 bufchain_add(&term->inbuf, data, len);
5950 if (!term->in_term_out) {
5951 term->in_term_out = TRUE;
5952 term_blink(term, 1);
5954 term->in_term_out = FALSE;
5958 * term_out() always completely empties inbuf. Therefore,
5959 * there's no reason at all to return anything other than zero
5960 * from this function, because there _can't_ be a question of
5961 * the remote side needing to wait until term_out() has cleared
5964 * This is a slightly suboptimal way to deal with SSH2 - in
5965 * principle, the window mechanism would allow us to continue
5966 * to accept data on forwarded ports and X connections even
5967 * while the terminal processing was going slowly - but we
5968 * can't do the 100% right thing without moving the terminal
5969 * processing into a separate thread, and that might hurt
5970 * portability. So we manage stdout buffering the old SSH1 way:
5971 * if the terminal processing goes slowly, the whole SSH
5972 * connection stops accepting data until it's ready.
5974 * In practice, I can't imagine this causing serious trouble.
5979 void term_provide_logctx(Terminal *term, void *logctx)
5981 term->logctx = logctx;