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);
185 * Add a combining character to a character cell.
187 static void add_cc(termline *line, int col, unsigned long chr)
191 assert(col >= 0 && col < line->cols);
194 * Start by extending the cols array if the free list is empty.
196 if (!line->cc_free) {
198 line->size += 16 + (line->size - line->cols) / 2;
199 line->chars = sresize(line->chars, line->size, termchar);
201 while (n < line->size) {
202 if (n+1 < line->size)
203 line->chars[n].cc_next = 1;
205 line->chars[n].cc_next = 0;
211 * Now walk the cc list of the cell in question.
213 while (line->chars[col].cc_next)
214 col += line->chars[col].cc_next;
217 * `col' now points at the last cc currently in this cell; so
218 * we simply add another one.
220 newcc = line->cc_free;
221 if (line->chars[newcc].cc_next)
222 line->cc_free = newcc + line->chars[newcc].cc_next;
225 line->chars[newcc].cc_next = 0;
226 line->chars[newcc].chr = chr;
227 line->chars[col].cc_next = newcc - col;
229 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
233 * Clear the combining character list in a character cell.
235 static void clear_cc(termline *line, int col)
237 int oldfree, origcol = col;
239 assert(col >= 0 && col < line->cols);
241 if (!line->chars[col].cc_next)
242 return; /* nothing needs doing */
244 oldfree = line->cc_free;
245 line->cc_free = col + line->chars[col].cc_next;
246 while (line->chars[col].cc_next)
247 col += line->chars[col].cc_next;
249 line->chars[col].cc_next = oldfree - col;
251 line->chars[col].cc_next = 0;
253 line->chars[origcol].cc_next = 0;
255 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
259 * Compare two character cells for equality. Special case required
260 * in do_paint() where we override what we expect the chr and attr
263 static int termchars_equal_override(termchar *a, termchar *b,
264 unsigned long bchr, unsigned long battr)
269 if (a->attr != battr)
271 while (a->cc_next || b->cc_next) {
272 if (!a->cc_next || !b->cc_next)
273 return FALSE; /* one cc-list ends, other does not */
276 if (a->chr != b->chr)
282 static int termchars_equal(termchar *a, termchar *b)
284 return termchars_equal_override(a, b, b->chr, b->attr);
288 * Copy a character cell. (Requires a pointer to the destination
289 * termline, so as to access its free list.)
291 static void copy_termchar(termline *destline, int x, termchar *src)
293 clear_cc(destline, x);
295 destline->chars[x] = *src; /* copy everything except cc-list */
296 destline->chars[x].cc_next = 0; /* and make sure this is zero */
298 while (src->cc_next) {
300 add_cc(destline, x, src->chr);
303 cc_check(destline); /* XXX-REMOVE-BEFORE-RELEASE */
307 * Move a character cell within its termline.
309 static void move_termchar(termline *line, termchar *dest, termchar *src)
311 /* First clear the cc list from the original char, just in case. */
312 clear_cc(line, dest - line->chars);
314 /* Move the character cell and adjust its cc_next. */
315 *dest = *src; /* copy everything except cc-list */
317 dest->cc_next = src->cc_next - (dest-src);
319 /* Ensure the original cell doesn't have a cc list. */
322 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
326 * Compress and decompress a termline into an RLE-based format for
327 * storing in scrollback. (Since scrollback almost never needs to
328 * be modified and exists in huge quantities, this is a sensible
329 * tradeoff, particularly since it allows us to continue adding
330 * features to the main termchar structure without proportionally
331 * bloating the terminal emulator's memory footprint unless those
332 * features are in constant use.)
338 static void add(struct buf *b, unsigned char c)
340 if (b->len >= b->size) {
341 b->size = (b->len * 3 / 2) + 512;
342 b->data = sresize(b->data, b->size, unsigned char);
344 b->data[b->len++] = c;
346 static int get(struct buf *b)
348 return b->data[b->len++];
350 static void makerle(struct buf *b, termline *ldata,
351 void (*makeliteral)(struct buf *b, termchar *c,
352 unsigned long *state))
354 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
355 termchar *c = ldata->chars;
356 unsigned long state = 0, oldstate;
363 prevlen = prevpos = 0;
368 makeliteral(b, c++, &state);
369 thislen = b->len - thispos;
370 if (thislen == prevlen &&
371 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
373 * This literal precisely matches the previous one.
374 * Turn it into a run if it's worthwhile.
376 * With one-byte literals, it costs us two bytes to
377 * encode a run, plus another byte to write the header
378 * to resume normal output; so a three-element run is
379 * neutral, and anything beyond that is unconditionally
380 * worthwhile. With two-byte literals or more, even a
383 if (thislen > 1 || prev2) {
387 * It's worth encoding a run. Start at prevpos,
388 * unless hdrsize==0 in which case we can back up
389 * another one and start by overwriting hdrpos.
392 hdrsize--; /* remove the literal at prevpos */
396 prevpos -= prevlen;/* and possibly another one */
400 assert(prevpos == hdrpos + 1);
402 b->len = prevpos+prevlen;
404 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
406 b->len = prevpos+prevlen+1;
408 * Terminate the previous run of ordinary
411 assert(hdrsize >= 1 && hdrsize <= 128);
412 b->data[hdrpos] = hdrsize - 1;
415 runlen = prev2 ? 3 : 2;
417 while (n > 0 && runlen < 129) {
421 makeliteral(b, c, &state);
422 tmplen = b->len - tmppos;
424 if (tmplen != thislen ||
425 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
427 break; /* run over */
432 assert(runlen >= 2 && runlen <= 129);
433 b->data[runpos] = runlen + 0x80 - 2;
438 /* And ensure this run doesn't interfere with the next. */
439 prevlen = prevpos = 0;
445 * Just flag that the previous two literals were
446 * identical, in case we find a third identical one
447 * we want to turn into a run.
460 * This character isn't (yet) part of a run. Add it to
464 if (hdrsize == 128) {
465 b->data[hdrpos] = hdrsize - 1;
469 prevlen = prevpos = 0;
478 assert(hdrsize <= 128);
479 b->data[hdrpos] = hdrsize - 1;
484 static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
487 * My encoding for characters is UTF-8-like, in that it stores
488 * 7-bit ASCII in one byte and uses high-bit-set bytes as
489 * introducers to indicate a longer sequence. However, it's
490 * unlike UTF-8 in that it doesn't need to be able to
491 * resynchronise, and therefore I don't want to waste two bits
492 * per byte on having recognisable continuation characters.
493 * Also I don't want to rule out the possibility that I may one
494 * day use values 0x80000000-0xFFFFFFFF for interesting
495 * purposes, so unlike UTF-8 I need a full 32-bit range.
496 * Accordingly, here is my encoding:
498 * 00000000-0000007F: 0xxxxxxx (but see below)
499 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
500 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
501 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
502 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
504 * (`Z' is like `x' but is always going to be zero since the
505 * values I'm encoding don't go above 2^32. In principle the
506 * five-byte form of the encoding could extend to 2^35, and
507 * there could be six-, seven-, eight- and nine-byte forms as
508 * well to allow up to 64-bit values to be encoded. But that's
509 * completely unnecessary for these purposes!)
511 * The encoding as written above would be very simple, except
512 * that 7-bit ASCII can occur in several different ways in the
513 * terminal data; sometimes it crops up in the D800 page
514 * (CSET_ASCII) but at other times it's in the 0000 page (real
515 * Unicode). Therefore, this encoding is actually _stateful_:
516 * the one-byte encoding of 00-7F actually indicates `reuse the
517 * upper three bytes of the last character', and to encode an
518 * absolute value of 00-7F you need to use the two-byte form
521 if ((c->chr & ~0x7F) == *state) {
522 add(b, (unsigned char)(c->chr & 0x7F));
523 } else if (c->chr < 0x4000) {
524 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
525 add(b, (unsigned char)(c->chr & 0xFF));
526 } else if (c->chr < 0x200000) {
527 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
528 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
529 add(b, (unsigned char)(c->chr & 0xFF));
530 } else if (c->chr < 0x10000000) {
531 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
532 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
533 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
534 add(b, (unsigned char)(c->chr & 0xFF));
537 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
538 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
539 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
540 add(b, (unsigned char)(c->chr & 0xFF));
542 *state = c->chr & ~0xFF;
544 static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
547 * My encoding for attributes is 16-bit-granular and assumes
548 * that the top bit of the word is never required. I either
549 * store a two-byte value with the top bit clear (indicating
550 * just that value), or a four-byte value with the top bit set
551 * (indicating the same value with its top bit clear).
553 if (c->attr < 0x8000) {
554 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
555 add(b, (unsigned char)(c->attr & 0xFF));
557 add(b, (unsigned char)(((c->attr >> 24) & 0x7F) | 0x80));
558 add(b, (unsigned char)((c->attr >> 16) & 0xFF));
559 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
560 add(b, (unsigned char)(c->attr & 0xFF));
563 static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)
566 * For combining characters, I just encode a bunch of ordinary
567 * chars using makeliteral_chr, and terminate with a \0
568 * character (which I know won't come up as a combining char
571 * I don't use the stateful encoding in makeliteral_chr.
573 unsigned long zstate;
582 makeliteral_chr(b, c, &zstate);
587 makeliteral_chr(b, &z, &zstate);
590 static termline *decompressline(unsigned char *data, int *bytes_used);
592 static unsigned char *compressline(termline *ldata)
594 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
597 * First, store the column count, 7 bits at a time, least
598 * significant `digit' first, with the high bit set on all but
604 add(b, (unsigned char)((n & 0x7F) | 0x80));
607 add(b, (unsigned char)(n));
611 * Next store the lattrs; same principle.
614 int n = ldata->lattr;
616 add(b, (unsigned char)((n & 0x7F) | 0x80));
619 add(b, (unsigned char)(n));
623 * Now we store a sequence of separate run-length encoded
624 * fragments, each containing exactly as many symbols as there
625 * are columns in the ldata.
627 * All of these have a common basic format:
629 * - a byte 00-7F indicates that X+1 literals follow it
630 * - a byte 80-FF indicates that a single literal follows it
631 * and expects to be repeated (X-0x80)+2 times.
633 * The format of the `literals' varies between the fragments.
635 makerle(b, ldata, makeliteral_chr);
636 makerle(b, ldata, makeliteral_attr);
637 makerle(b, ldata, makeliteral_cc);
640 * Diagnostics: ensure that the compressed data really does
641 * decompress to the right thing.
643 * XXX-REMOVE-BEFORE-RELEASE: This is a bit performance-heavy
644 * to be leaving in production code.
646 #ifndef CHECK_SB_COMPRESSION
652 #ifdef DIAGNOSTIC_SB_COMPRESSION
653 for (i = 0; i < b->len; i++) {
654 printf(" %02x ", b->data[i]);
659 dcl = decompressline(b->data, &dused);
660 assert(b->len == dused);
661 assert(ldata->cols == dcl->cols);
662 assert(ldata->lattr == dcl->lattr);
663 for (i = 0; i < ldata->cols; i++)
664 assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
666 #ifdef DIAGNOSTIC_SB_COMPRESSION
667 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
668 ldata->cols, 4 * ldata->cols, dused,
669 (double)dused / (4 * ldata->cols));
677 * Trim the allocated memory so we don't waste any, and return.
679 return sresize(b->data, b->len, unsigned char);
682 static void readrle(struct buf *b, termline *ldata,
683 void (*readliteral)(struct buf *b, termchar *c,
684 termline *ldata, unsigned long *state))
687 unsigned long state = 0;
689 while (n < ldata->cols) {
695 int pos = b->len, count = hdr + 2 - 0x80;
697 assert(n < ldata->cols);
699 readliteral(b, ldata->chars + n, ldata, &state);
703 /* Just a sequence of consecutive literals. */
707 assert(n < ldata->cols);
708 readliteral(b, ldata->chars + n, ldata, &state);
714 assert(n == ldata->cols);
716 static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,
717 unsigned long *state)
722 * 00000000-0000007F: 0xxxxxxx
723 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
724 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
725 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
726 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
731 c->chr = byte | *state;
732 } else if (byte < 0xC0) {
733 c->chr = (byte &~ 0xC0) << 8;
735 } else if (byte < 0xE0) {
736 c->chr = (byte &~ 0xE0) << 16;
737 c->chr |= get(b) << 8;
739 } else if (byte < 0xF0) {
740 c->chr = (byte &~ 0xF0) << 24;
741 c->chr |= get(b) << 16;
742 c->chr |= get(b) << 8;
745 assert(byte == 0xF0);
746 c->chr = get(b) << 24;
747 c->chr |= get(b) << 16;
748 c->chr |= get(b) << 8;
751 *state = c->chr & ~0xFF;
753 static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,
754 unsigned long *state)
769 static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,
770 unsigned long *state)
773 unsigned long zstate;
774 int x = c - ldata->chars;
780 readliteral_chr(b, &n, ldata, &zstate);
783 add_cc(ldata, x, n.chr);
787 static termline *decompressline(unsigned char *data, int *bytes_used)
789 int ncols, byte, shift;
790 struct buf buffer, *b = &buffer;
797 * First read in the column count.
802 ncols |= (byte & 0x7F) << shift;
804 } while (byte & 0x80);
807 * Now create the output termline.
809 ldata = snew(termline);
810 ldata->chars = snewn(ncols, termchar);
811 ldata->cols = ldata->size = ncols;
812 ldata->temporary = TRUE;
816 * We must set all the cc pointers in ldata->chars to 0 right
817 * now, so that cc diagnostics that verify the integrity of the
818 * whole line will make sense while we're in the middle of
823 for (i = 0; i < ldata->cols; i++)
824 ldata->chars[i].cc_next = 0;
828 * Now read in the lattr.
830 ldata->lattr = shift = 0;
833 ldata->lattr |= (byte & 0x7F) << shift;
835 } while (byte & 0x80);
838 * Now we read in each of the RLE streams in turn.
840 readrle(b, ldata, readliteral_chr);
841 readrle(b, ldata, readliteral_attr);
842 readrle(b, ldata, readliteral_cc);
844 /* Return the number of bytes read, for diagnostic purposes. */
846 *bytes_used = b->len;
852 * Resize a line to make it `cols' columns wide.
854 static void resizeline(Terminal *term, termline *line, int cols)
858 if (line->cols != cols) {
860 oldcols = line->cols;
863 * This line is the wrong length, which probably means it
864 * hasn't been accessed since a resize. Resize it now.
866 * First, go through all the characters that will be thrown
867 * out in the resize (if we're shrinking the line) and
868 * return their cc lists to the cc free list.
870 for (i = cols; i < oldcols; i++)
874 * If we're shrinking the line, we now bodily move the
875 * entire cc section from where it started to where it now
876 * needs to be. (We have to do this before the resize, so
877 * that the data we're copying is still there. However, if
878 * we're expanding, we have to wait until _after_ the
879 * resize so that the space we're copying into is there.)
882 memmove(line->chars + cols, line->chars + oldcols,
883 (line->size - line->cols) * TSIZE);
886 * Now do the actual resize, leaving the _same_ amount of
887 * cc space as there was to begin with.
889 line->size += cols - oldcols;
890 line->chars = sresize(line->chars, line->size, TTYPE);
894 * If we're expanding the line, _now_ we move the cc
898 memmove(line->chars + cols, line->chars + oldcols,
899 (line->size - line->cols) * TSIZE);
902 * Go through what's left of the original line, and adjust
903 * the first cc_next pointer in each list. (All the
904 * subsequent ones are still valid because they are
905 * relative offsets within the cc block.) Also do the same
906 * to the head of the cc_free list.
908 for (i = 0; i < oldcols && i < cols; i++)
909 if (line->chars[i].cc_next)
910 line->chars[i].cc_next += cols - oldcols;
912 line->cc_free += cols - oldcols;
915 * And finally fill in the new space with erase chars. (We
916 * don't have to worry about cc lists here, because we
917 * _know_ the erase char doesn't have one.)
919 for (i = oldcols; i < cols; i++)
920 line->chars[i] = term->basic_erase_char;
922 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
927 * Get the number of lines in the scrollback.
929 static int sblines(Terminal *term)
931 int sblines = count234(term->scrollback);
932 if (term->cfg.erase_to_scrollback &&
933 term->alt_which && term->alt_screen) {
934 sblines += term->alt_sblines;
940 * Retrieve a line of the screen or of the scrollback, according to
941 * whether the y coordinate is non-negative or negative
944 static termline *lineptr(Terminal *term, int y, int lineno, int screen)
951 whichtree = term->screen;
958 if (term->cfg.erase_to_scrollback &&
959 term->alt_which && term->alt_screen) {
960 altlines = term->alt_sblines;
963 whichtree = term->scrollback;
964 treeindex = y + altlines + count234(term->scrollback);
966 whichtree = term->alt_screen;
967 treeindex = y + term->alt_sblines;
968 /* treeindex = y + count234(term->alt_screen); */
971 if (whichtree == term->scrollback) {
972 unsigned char *cline = index234(whichtree, treeindex);
973 line = decompressline(cline, NULL);
975 line = index234(whichtree, treeindex);
978 /* We assume that we don't screw up and retrieve something out of range. */
979 assert(line != NULL);
981 resizeline(term, line, term->cols);
982 /* FIXME: should we sort the compressed scrollback out here? */
987 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
988 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
991 * Set up power-on settings for the terminal.
993 static void power_on(Terminal *term)
995 term->curs.x = term->curs.y = 0;
996 term->alt_x = term->alt_y = 0;
997 term->savecurs.x = term->savecurs.y = 0;
998 term->alt_t = term->marg_t = 0;
999 if (term->rows != -1)
1000 term->alt_b = term->marg_b = term->rows - 1;
1002 term->alt_b = term->marg_b = 0;
1003 if (term->cols != -1) {
1005 for (i = 0; i < term->cols; i++)
1006 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1008 term->alt_om = term->dec_om = term->cfg.dec_om;
1009 term->alt_ins = term->insert = FALSE;
1010 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
1011 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1012 term->alt_cset = term->cset = term->save_cset = 0;
1013 term->alt_utf = term->utf = term->save_utf = 0;
1014 term->utf_state = 0;
1015 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
1016 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
1018 term->in_vbell = FALSE;
1019 term->cursor_on = 1;
1020 term->big_cursor = 0;
1021 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
1022 term->term_editing = term->term_echoing = FALSE;
1023 term->app_cursor_keys = term->cfg.app_cursor;
1024 term->app_keypad_keys = term->cfg.app_keypad;
1025 term->use_bce = term->cfg.bce;
1026 term->blink_is_real = term->cfg.blinktext;
1027 term->erase_char = term->basic_erase_char;
1028 term->alt_which = 0;
1029 term_print_finish(term);
1032 for (i = 0; i < 256; i++)
1033 term->wordness[i] = term->cfg.wordness[i];
1036 swap_screen(term, 1, FALSE, FALSE);
1037 erase_lots(term, FALSE, TRUE, TRUE);
1038 swap_screen(term, 0, FALSE, FALSE);
1039 erase_lots(term, FALSE, TRUE, TRUE);
1044 * Force a screen update.
1046 void term_update(Terminal *term)
1049 ctx = get_ctx(term->frontend);
1051 int need_sbar_update = term->seen_disp_event;
1052 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
1053 term->disptop = 0; /* return to main screen */
1054 term->seen_disp_event = 0;
1055 need_sbar_update = TRUE;
1058 if (need_sbar_update)
1060 do_paint(term, ctx, TRUE);
1061 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
1067 * Called from front end when a keypress occurs, to trigger
1068 * anything magical that needs to happen in that situation.
1070 void term_seen_key_event(Terminal *term)
1073 * On any keypress, clear the bell overload mechanism
1074 * completely, on the grounds that large numbers of
1075 * beeps coming from deliberate key action are likely
1076 * to be intended (e.g. beeps from filename completion
1077 * blocking repeatedly).
1079 term->beep_overloaded = FALSE;
1080 while (term->beephead) {
1081 struct beeptime *tmp = term->beephead;
1082 term->beephead = tmp->next;
1085 term->beeptail = NULL;
1089 * Reset the scrollback on keypress, if we're doing that.
1091 if (term->cfg.scroll_on_key) {
1092 term->disptop = 0; /* return to main screen */
1093 term->seen_disp_event = 1;
1098 * Same as power_on(), but an external function.
1100 void term_pwron(Terminal *term)
1103 if (term->ldisc) /* cause ldisc to notice changes */
1104 ldisc_send(term->ldisc, NULL, 0, 0);
1110 static void set_erase_char(Terminal *term)
1112 term->erase_char = term->basic_erase_char;
1114 term->erase_char.attr = (term->curr_attr &
1115 (ATTR_FGMASK | ATTR_BGMASK));
1119 * When the user reconfigures us, we need to check the forbidden-
1120 * alternate-screen config option, disable raw mouse mode if the
1121 * user has disabled mouse reporting, and abandon a print job if
1122 * the user has disabled printing.
1124 void term_reconfig(Terminal *term, Config *cfg)
1127 * Before adopting the new config, check all those terminal
1128 * settings which control power-on defaults; and if they've
1129 * changed, we will modify the current state as well as the
1130 * default one. The full list is: Auto wrap mode, DEC Origin
1131 * Mode, BCE, blinking text, character classes.
1133 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
1136 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
1137 reset_decom = (term->cfg.dec_om != cfg->dec_om);
1138 reset_bce = (term->cfg.bce != cfg->bce);
1139 reset_blink = (term->cfg.blinktext != cfg->blinktext);
1140 reset_charclass = 0;
1141 for (i = 0; i < lenof(term->cfg.wordness); i++)
1142 if (term->cfg.wordness[i] != cfg->wordness[i])
1143 reset_charclass = 1;
1146 * If the bidi or shaping settings have changed, flush the bidi
1149 if (term->cfg.arabicshaping != cfg->arabicshaping ||
1150 term->cfg.bidi != cfg->bidi) {
1151 for (i = 0; i < term->bidi_cache_size; i++) {
1152 sfree(term->pre_bidi_cache[i].chars);
1153 sfree(term->post_bidi_cache[i].chars);
1154 term->pre_bidi_cache[i].width = -1;
1155 term->pre_bidi_cache[i].chars = NULL;
1156 term->post_bidi_cache[i].width = -1;
1157 term->post_bidi_cache[i].chars = NULL;
1161 term->cfg = *cfg; /* STRUCTURE COPY */
1164 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1166 term->alt_om = term->dec_om = term->cfg.dec_om;
1168 term->use_bce = term->cfg.bce;
1169 set_erase_char(term);
1172 term->blink_is_real = term->cfg.blinktext;
1173 if (reset_charclass)
1174 for (i = 0; i < 256; i++)
1175 term->wordness[i] = term->cfg.wordness[i];
1177 if (term->cfg.no_alt_screen)
1178 swap_screen(term, 0, FALSE, FALSE);
1179 if (term->cfg.no_mouse_rep) {
1180 term->xterm_mouse = 0;
1181 set_raw_mouse_mode(term->frontend, 0);
1183 if (term->cfg.no_remote_charset) {
1184 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
1185 term->sco_acs = term->alt_sco_acs = 0;
1188 if (!*term->cfg.printer) {
1189 term_print_finish(term);
1194 * Clear the scrollback.
1196 void term_clrsb(Terminal *term)
1200 while ((line = delpos234(term->scrollback, 0)) != NULL) {
1201 sfree(line); /* this is compressed data, not a termline */
1203 term->tempsblines = 0;
1204 term->alt_sblines = 0;
1209 * Initialise the terminal.
1211 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
1217 * Allocate a new Terminal structure and initialise the fields
1220 term = snew(Terminal);
1221 term->frontend = frontend;
1222 term->ucsdata = ucsdata;
1223 term->cfg = *mycfg; /* STRUCTURE COPY */
1224 term->logctx = NULL;
1225 term->compatibility_level = TM_PUTTY;
1226 strcpy(term->id_string, "\033[?6c");
1227 term->last_blink = term->last_tblink = 0;
1228 term->paste_buffer = NULL;
1229 term->paste_len = 0;
1230 term->last_paste = 0;
1231 bufchain_init(&term->inbuf);
1232 bufchain_init(&term->printer_buf);
1233 term->printing = term->only_printing = FALSE;
1234 term->print_job = NULL;
1235 term->vt52_mode = FALSE;
1236 term->cr_lf_return = FALSE;
1237 term->seen_disp_event = FALSE;
1238 term->xterm_mouse = term->mouse_is_down = FALSE;
1239 term->reset_132 = FALSE;
1240 term->blinker = term->tblinker = 0;
1241 term->has_focus = 1;
1242 term->repeat_off = FALSE;
1243 term->termstate = TOPLEVEL;
1244 term->selstate = NO_SELECTION;
1247 term->screen = term->alt_screen = term->scrollback = NULL;
1248 term->tempsblines = 0;
1249 term->alt_sblines = 0;
1251 term->disptext = NULL;
1252 term->dispcursx = term->dispcursy = -1;
1255 term->rows = term->cols = -1;
1257 term->beephead = term->beeptail = NULL;
1258 #ifdef OPTIMISE_SCROLL
1259 term->scrollhead = term->scrolltail = NULL;
1260 #endif /* OPTIMISE_SCROLL */
1262 term->lastbeep = FALSE;
1263 term->beep_overloaded = FALSE;
1264 term->attr_mask = 0xffffffff;
1265 term->resize_fn = NULL;
1266 term->resize_ctx = NULL;
1267 term->in_term_out = FALSE;
1269 term->ltemp_size = 0;
1270 term->wcFrom = NULL;
1272 term->wcFromTo_size = 0;
1274 term->bidi_cache_size = 0;
1275 term->pre_bidi_cache = term->post_bidi_cache = NULL;
1278 term->basic_erase_char.chr = CSET_ASCII | ' ';
1279 term->basic_erase_char.attr = ATTR_DEFAULT;
1280 term->basic_erase_char.cc_next = 0;
1281 term->erase_char = term->basic_erase_char;
1286 void term_free(Terminal *term)
1289 struct beeptime *beep;
1292 while ((line = delpos234(term->scrollback, 0)) != NULL)
1293 sfree(line); /* compressed data, not a termline */
1294 freetree234(term->scrollback);
1295 while ((line = delpos234(term->screen, 0)) != NULL)
1297 freetree234(term->screen);
1298 while ((line = delpos234(term->alt_screen, 0)) != NULL)
1300 freetree234(term->alt_screen);
1301 if (term->disptext) {
1302 for (i = 0; i < term->rows; i++)
1303 freeline(term->disptext[i]);
1305 sfree(term->disptext);
1306 while (term->beephead) {
1307 beep = term->beephead;
1308 term->beephead = beep->next;
1311 bufchain_clear(&term->inbuf);
1313 printer_finish_job(term->print_job);
1314 bufchain_clear(&term->printer_buf);
1315 sfree(term->paste_buffer);
1317 sfree(term->wcFrom);
1320 for (i = 0; i < term->bidi_cache_size; i++) {
1321 sfree(term->pre_bidi_cache[i].chars);
1322 sfree(term->post_bidi_cache[i].chars);
1324 sfree(term->pre_bidi_cache);
1325 sfree(term->post_bidi_cache);
1331 * Set up the terminal for a given size.
1333 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
1336 termline **newdisp, *line;
1337 int i, j, oldrows = term->rows;
1339 int save_alt_which = term->alt_which;
1341 if (newrows == term->rows && newcols == term->cols &&
1342 newsavelines == term->savelines)
1343 return; /* nothing to do */
1346 swap_screen(term, 0, FALSE, FALSE);
1348 term->alt_t = term->marg_t = 0;
1349 term->alt_b = term->marg_b = newrows - 1;
1351 if (term->rows == -1) {
1352 term->scrollback = newtree234(NULL);
1353 term->screen = newtree234(NULL);
1354 term->tempsblines = 0;
1359 * Resize the screen and scrollback. We only need to shift
1360 * lines around within our data structures, because lineptr()
1361 * will take care of resizing each individual line if
1364 * - If the new screen is longer, we shunt lines in from temporary
1365 * scrollback if possible, otherwise we add new blank lines at
1368 * - If the new screen is shorter, we remove any blank lines at
1369 * the bottom if possible, otherwise shunt lines above the cursor
1370 * to scrollback if possible, otherwise delete lines below the
1373 * - Then, if the new scrollback length is less than the
1374 * amount of scrollback we actually have, we must throw some
1377 sblen = count234(term->scrollback);
1378 /* Do this loop to expand the screen if newrows > rows */
1379 assert(term->rows == count234(term->screen));
1380 while (term->rows < newrows) {
1381 if (term->tempsblines > 0) {
1382 unsigned char *cline;
1383 /* Insert a line from the scrollback at the top of the screen. */
1384 assert(sblen >= term->tempsblines);
1385 cline = delpos234(term->scrollback, --sblen);
1386 line = decompressline(cline, NULL);
1388 line->temporary = FALSE; /* reconstituted line is now real */
1389 term->tempsblines -= 1;
1390 addpos234(term->screen, line, 0);
1392 term->savecurs.y += 1;
1394 /* Add a new blank line at the bottom of the screen. */
1395 line = newline(term, newcols, FALSE);
1396 addpos234(term->screen, line, count234(term->screen));
1400 /* Do this loop to shrink the screen if newrows < rows */
1401 while (term->rows > newrows) {
1402 if (term->curs.y < term->rows - 1) {
1403 /* delete bottom row, unless it contains the cursor */
1404 sfree(delpos234(term->screen, term->rows - 1));
1406 /* push top row to scrollback */
1407 line = delpos234(term->screen, 0);
1408 addpos234(term->scrollback, compressline(line), sblen++);
1410 term->tempsblines += 1;
1412 term->savecurs.y -= 1;
1416 assert(term->rows == newrows);
1417 assert(count234(term->screen) == newrows);
1419 /* Delete any excess lines from the scrollback. */
1420 while (sblen > newsavelines) {
1421 line = delpos234(term->scrollback, 0);
1425 if (sblen < term->tempsblines)
1426 term->tempsblines = sblen;
1427 assert(count234(term->scrollback) <= newsavelines);
1428 assert(count234(term->scrollback) >= term->tempsblines);
1431 /* Make a new displayed text buffer. */
1432 newdisp = snewn(newrows, termline *);
1433 for (i = 0; i < newrows; i++) {
1434 newdisp[i] = newline(term, newcols, FALSE);
1435 for (j = 0; j < newcols; j++)
1436 newdisp[i]->chars[i].attr = ATTR_INVALID;
1438 if (term->disptext) {
1439 for (i = 0; i < oldrows; i++)
1440 freeline(term->disptext[i]);
1442 sfree(term->disptext);
1443 term->disptext = newdisp;
1444 term->dispcursx = term->dispcursy = -1;
1446 /* Make a new alternate screen. */
1447 newalt = newtree234(NULL);
1448 for (i = 0; i < newrows; i++) {
1449 line = newline(term, newcols, TRUE);
1450 addpos234(newalt, line, i);
1452 if (term->alt_screen) {
1453 while (NULL != (line = delpos234(term->alt_screen, 0)))
1455 freetree234(term->alt_screen);
1457 term->alt_screen = newalt;
1458 term->alt_sblines = 0;
1460 term->tabs = sresize(term->tabs, newcols, unsigned char);
1463 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1464 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1467 /* Check that the cursor positions are still valid. */
1468 if (term->savecurs.y < 0)
1469 term->savecurs.y = 0;
1470 if (term->savecurs.y >= newrows)
1471 term->savecurs.y = newrows - 1;
1472 if (term->curs.y < 0)
1474 if (term->curs.y >= newrows)
1475 term->curs.y = newrows - 1;
1476 if (term->curs.x >= newcols)
1477 term->curs.x = newcols - 1;
1478 term->alt_x = term->alt_y = 0;
1479 term->wrapnext = term->alt_wnext = FALSE;
1481 term->rows = newrows;
1482 term->cols = newcols;
1483 term->savelines = newsavelines;
1485 swap_screen(term, save_alt_which, FALSE, FALSE);
1489 if (term->resize_fn)
1490 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1494 * Hand a function and context pointer to the terminal which it can
1495 * use to notify a back end of resizes.
1497 void term_provide_resize_fn(Terminal *term,
1498 void (*resize_fn)(void *, int, int),
1501 term->resize_fn = resize_fn;
1502 term->resize_ctx = resize_ctx;
1503 if (term->cols > 0 && term->rows > 0)
1504 resize_fn(resize_ctx, term->cols, term->rows);
1507 /* Find the bottom line on the screen that has any content.
1508 * If only the top line has content, returns 0.
1509 * If no lines have content, return -1.
1511 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1514 for (i = count234(screen) - 1; i >= 0; i--) {
1515 termline *line = index234(screen, i);
1517 for (j = 0; j < line->cols; j++)
1518 if (!termchars_equal(&line->chars[j], &term->erase_char))
1520 if (j != line->cols) break;
1526 * Swap screens. If `reset' is TRUE and we have been asked to
1527 * switch to the alternate screen, we must bring most of its
1528 * configuration from the main screen and erase the contents of the
1529 * alternate screen completely. (This is even true if we're already
1530 * on it! Blame xterm.)
1532 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
1538 reset = FALSE; /* do no weird resetting if which==0 */
1540 if (which != term->alt_which) {
1541 term->alt_which = which;
1543 ttr = term->alt_screen;
1544 term->alt_screen = term->screen;
1546 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
1548 if (!reset && !keep_cur_pos)
1549 term->curs.x = term->alt_x;
1552 if (!reset && !keep_cur_pos)
1553 term->curs.y = term->alt_y;
1556 if (!reset) term->marg_t = term->alt_t;
1559 if (!reset) term->marg_b = term->alt_b;
1562 if (!reset) term->dec_om = term->alt_om;
1565 if (!reset) term->wrap = term->alt_wrap;
1568 if (!reset) term->wrapnext = term->alt_wnext;
1569 term->alt_wnext = t;
1571 if (!reset) term->insert = term->alt_ins;
1574 if (!reset) term->cset = term->alt_cset;
1577 if (!reset) term->utf = term->alt_utf;
1580 if (!reset) term->sco_acs = term->alt_sco_acs;
1581 term->alt_sco_acs = t;
1584 if (reset && term->screen) {
1586 * Yes, this _is_ supposed to honour background-colour-erase.
1588 erase_lots(term, FALSE, TRUE, TRUE);
1593 * Update the scroll bar.
1595 static void update_sbar(Terminal *term)
1597 int nscroll = sblines(term);
1598 set_sbar(term->frontend, nscroll + term->rows,
1599 nscroll + term->disptop, term->rows);
1603 * Check whether the region bounded by the two pointers intersects
1604 * the scroll region, and de-select the on-screen selection if so.
1606 static void check_selection(Terminal *term, pos from, pos to)
1608 if (poslt(from, term->selend) && poslt(term->selstart, to))
1613 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1614 * for backward.) `sb' is TRUE if the scrolling is permitted to
1615 * affect the scrollback buffer.
1617 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
1620 int i, seltop, olddisptop, shift;
1622 if (topline != 0 || term->alt_which != 0)
1625 olddisptop = term->disptop;
1629 line = delpos234(term->screen, botline);
1630 resizeline(term, line, term->cols);
1631 for (i = 0; i < term->cols; i++)
1632 copy_termchar(line, i, &term->erase_char);
1633 line->lattr = LATTR_NORM;
1634 addpos234(term->screen, line, topline);
1636 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1638 if (term->selstart.y > botline) {
1639 term->selstart.y = botline + 1;
1640 term->selstart.x = 0;
1643 if (term->selend.y >= topline && term->selend.y <= botline) {
1645 if (term->selend.y > botline) {
1646 term->selend.y = botline + 1;
1655 line = delpos234(term->screen, topline);
1656 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
1657 if (sb && term->savelines > 0) {
1658 int sblen = count234(term->scrollback);
1660 * We must add this line to the scrollback. We'll
1661 * remove a line from the top of the scrollback if
1662 * the scrollback is full.
1664 if (sblen == term->savelines) {
1665 unsigned char *cline;
1668 cline = delpos234(term->scrollback, 0);
1671 term->tempsblines += 1;
1673 addpos234(term->scrollback, compressline(line), sblen);
1675 /* now `line' itself can be reused as the bottom line */
1678 * If the user is currently looking at part of the
1679 * scrollback, and they haven't enabled any options
1680 * that are going to reset the scrollback as a
1681 * result of this movement, then the chances are
1682 * they'd like to keep looking at the same line. So
1683 * we move their viewpoint at the same rate as the
1684 * scroll, at least until their viewpoint hits the
1685 * top end of the scrollback buffer, at which point
1686 * we don't have the choice any more.
1688 * Thanks to Jan Holmen Holsten for the idea and
1689 * initial implementation.
1691 if (term->disptop > -term->savelines && term->disptop < 0)
1694 resizeline(term, line, term->cols);
1695 for (i = 0; i < term->cols; i++)
1696 copy_termchar(line, i, &term->erase_char);
1697 line->lattr = LATTR_NORM;
1698 addpos234(term->screen, line, botline);
1701 * If the selection endpoints move into the scrollback,
1702 * we keep them moving until they hit the top. However,
1703 * of course, if the line _hasn't_ moved into the
1704 * scrollback then we don't do this, and cut them off
1705 * at the top of the scroll region.
1707 * This applies to selstart and selend (for an existing
1708 * selection), and also selanchor (for one being
1709 * selected as we speak).
1711 seltop = sb ? -term->savelines : topline;
1713 if (term->selstate != NO_SELECTION) {
1714 if (term->selstart.y >= seltop &&
1715 term->selstart.y <= botline) {
1717 if (term->selstart.y < seltop) {
1718 term->selstart.y = seltop;
1719 term->selstart.x = 0;
1722 if (term->selend.y >= seltop && term->selend.y <= botline) {
1724 if (term->selend.y < seltop) {
1725 term->selend.y = seltop;
1729 if (term->selanchor.y >= seltop &&
1730 term->selanchor.y <= botline) {
1731 term->selanchor.y--;
1732 if (term->selanchor.y < seltop) {
1733 term->selanchor.y = seltop;
1734 term->selanchor.x = 0;
1742 #ifdef OPTIMISE_SCROLL
1743 shift += term->disptop - olddisptop;
1744 if (shift < term->rows && shift > -term->rows && shift != 0)
1745 scroll_display(term, topline, botline, shift);
1746 #endif /* OPTIMISE_SCROLL */
1749 #ifdef OPTIMISE_SCROLL
1751 * Add a scroll of a region on the screen into the pending scroll list.
1752 * `lines' is +ve for scrolling forward, -ve for backward.
1754 * If the scroll is on the same area as the last scroll in the list,
1757 static void save_scroll(Terminal *term, int topline, int botline, int lines)
1759 struct scrollregion *newscroll;
1760 if (term->scrolltail &&
1761 term->scrolltail->topline == topline &&
1762 term->scrolltail->botline == botline) {
1763 term->scrolltail->lines += lines;
1765 newscroll = snew(struct scrollregion);
1766 newscroll->topline = topline;
1767 newscroll->botline = botline;
1768 newscroll->lines = lines;
1769 newscroll->next = NULL;
1771 if (!term->scrollhead)
1772 term->scrollhead = newscroll;
1774 term->scrolltail->next = newscroll;
1775 term->scrolltail = newscroll;
1780 * Scroll the physical display, and our conception of it in disptext.
1782 static void scroll_display(Terminal *term, int topline, int botline, int lines)
1784 int distance, nlines, i, j;
1786 distance = lines > 0 ? lines : -lines;
1787 nlines = botline - topline + 1 - distance;
1789 for (i = 0; i < nlines; i++)
1790 for (j = 0; j < term->cols; j++)
1791 copy_termchar(term->disptext[start+i], j,
1792 term->disptext[start+i+distance]->chars+j);
1793 if (term->dispcursy >= 0 &&
1794 term->dispcursy >= topline + distance &&
1795 term->dispcursy < topline + distance + nlines)
1796 term->dispcursy -= distance;
1797 for (i = 0; i < distance; i++)
1798 for (j = 0; j < term->cols; j++)
1799 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
1801 for (i = nlines; i-- ;)
1802 for (j = 0; j < term->cols; j++)
1803 copy_termchar(term->disptext[start+i+distance], j,
1804 term->disptext[start+i]->chars+j);
1805 if (term->dispcursy >= 0 &&
1806 term->dispcursy >= topline &&
1807 term->dispcursy < topline + nlines)
1808 term->dispcursy += distance;
1809 for (i = 0; i < distance; i++)
1810 for (j = 0; j < term->cols; j++)
1811 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
1813 save_scroll(term, topline, botline, lines);
1815 #endif /* OPTIMISE_SCROLL */
1818 * Move the cursor to a given position, clipping at boundaries. We
1819 * may or may not want to clip at the scroll margin: marg_clip is 0
1820 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1821 * even _being_ outside the margins.
1823 static void move(Terminal *term, int x, int y, int marg_clip)
1827 if (x >= term->cols)
1830 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1833 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1839 if (y >= term->rows)
1843 term->wrapnext = FALSE;
1847 * Save or restore the cursor and SGR mode.
1849 static void save_cursor(Terminal *term, int save)
1852 term->savecurs = term->curs;
1853 term->save_attr = term->curr_attr;
1854 term->save_cset = term->cset;
1855 term->save_utf = term->utf;
1856 term->save_wnext = term->wrapnext;
1857 term->save_csattr = term->cset_attr[term->cset];
1858 term->save_sco_acs = term->sco_acs;
1860 term->curs = term->savecurs;
1861 /* Make sure the window hasn't shrunk since the save */
1862 if (term->curs.x >= term->cols)
1863 term->curs.x = term->cols - 1;
1864 if (term->curs.y >= term->rows)
1865 term->curs.y = term->rows - 1;
1867 term->curr_attr = term->save_attr;
1868 term->cset = term->save_cset;
1869 term->utf = term->save_utf;
1870 term->wrapnext = term->save_wnext;
1872 * wrapnext might reset to False if the x position is no
1873 * longer at the rightmost edge.
1875 if (term->wrapnext && term->curs.x < term->cols-1)
1876 term->wrapnext = FALSE;
1877 term->cset_attr[term->cset] = term->save_csattr;
1878 term->sco_acs = term->save_sco_acs;
1879 set_erase_char(term);
1884 * This function is called before doing _anything_ which affects
1885 * only part of a line of text. It is used to mark the boundary
1886 * between two character positions, and it indicates that some sort
1887 * of effect is going to happen on only one side of that boundary.
1889 * The effect of this function is to check whether a CJK
1890 * double-width character is straddling the boundary, and to remove
1891 * it and replace it with two spaces if so. (Of course, one or
1892 * other of those spaces is then likely to be replaced with
1893 * something else again, as a result of whatever happens next.)
1895 * Also, if the boundary is at the right-hand _edge_ of the screen,
1896 * it implies something deliberate is being done to the rightmost
1897 * column position; hence we must clear LATTR_WRAPPED2.
1899 * The input to the function is the coordinates of the _second_
1900 * character of the pair.
1902 static void check_boundary(Terminal *term, int x, int y)
1906 /* Validate input coordinates, just in case. */
1907 if (x == 0 || x > term->cols)
1910 ldata = scrlineptr(y);
1911 if (x == term->cols) {
1912 ldata->lattr &= ~LATTR_WRAPPED2;
1914 if (ldata->chars[x].chr == UCSWIDE) {
1915 clear_cc(ldata, x-1);
1917 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
1918 ldata->chars[x] = ldata->chars[x-1];
1924 * Erase a large portion of the screen: the whole screen, or the
1925 * whole line, or parts thereof.
1927 static void erase_lots(Terminal *term,
1928 int line_only, int from_begin, int to_end)
1932 int erasing_lines_from_top = 0;
1935 start.y = term->curs.y;
1937 end.y = term->curs.y + 1;
1939 erase_lattr = FALSE;
1954 if (!from_begin || !to_end)
1955 check_boundary(term, term->curs.x, term->curs.y);
1956 check_selection(term, start, end);
1958 /* Clear screen also forces a full window redraw, just in case. */
1959 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1960 term_invalidate(term);
1962 /* Lines scrolled away shouldn't be brought back on if the terminal
1964 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1965 erasing_lines_from_top = 1;
1967 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1968 /* If it's a whole number of lines, starting at the top, and
1969 * we're fully erasing them, erase by scrolling and keep the
1970 * lines in the scrollback. */
1971 int scrolllines = end.y;
1972 if (end.y == term->rows) {
1973 /* Shrink until we find a non-empty row.*/
1974 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1976 if (scrolllines > 0)
1977 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1979 termline *ldata = scrlineptr(start.y);
1980 while (poslt(start, end)) {
1981 if (start.x == term->cols) {
1983 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1985 ldata->lattr = LATTR_NORM;
1987 copy_termchar(ldata, start.x, &term->erase_char);
1989 if (incpos(start) && start.y < term->rows) {
1990 ldata = scrlineptr(start.y);
1995 /* After an erase of lines from the top of the screen, we shouldn't
1996 * bring the lines back again if the terminal enlarges (since the user or
1997 * application has explictly thrown them away). */
1998 if (erasing_lines_from_top && !(term->alt_which))
1999 term->tempsblines = 0;
2003 * Insert or delete characters within the current line. n is +ve if
2004 * insertion is desired, and -ve for deletion.
2006 static void insch(Terminal *term, int n)
2008 int dir = (n < 0 ? -1 : +1);
2013 n = (n < 0 ? -n : n);
2014 if (n > term->cols - term->curs.x)
2015 n = term->cols - term->curs.x;
2016 m = term->cols - term->curs.x - n;
2017 cursplus.y = term->curs.y;
2018 cursplus.x = term->curs.x + n;
2019 check_selection(term, term->curs, cursplus);
2020 check_boundary(term, term->curs.x, term->curs.y);
2022 check_boundary(term, term->curs.x + n, term->curs.y);
2023 ldata = scrlineptr(term->curs.y);
2025 for (j = 0; j < m; j++)
2026 move_termchar(ldata,
2027 ldata->chars + term->curs.x + j,
2028 ldata->chars + term->curs.x + j + n);
2030 copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
2033 move_termchar(ldata,
2034 ldata->chars + term->curs.x + j + n,
2035 ldata->chars + term->curs.x + j);
2037 copy_termchar(ldata, term->curs.x + n, &term->erase_char);
2042 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2043 * whether the mode is a DEC private one or a normal one.)
2045 static void toggle_mode(Terminal *term, int mode, int query, int state)
2047 unsigned long ticks;
2051 case 1: /* DECCKM: application cursor keys */
2052 term->app_cursor_keys = state;
2054 case 2: /* DECANM: VT52 mode */
2055 term->vt52_mode = !state;
2056 if (term->vt52_mode) {
2057 term->blink_is_real = FALSE;
2058 term->vt52_bold = FALSE;
2060 term->blink_is_real = term->cfg.blinktext;
2063 case 3: /* DECCOLM: 80/132 columns */
2065 if (!term->cfg.no_remote_resize)
2066 request_resize(term->frontend, state ? 132 : 80, term->rows);
2067 term->reset_132 = state;
2068 term->alt_t = term->marg_t = 0;
2069 term->alt_b = term->marg_b = term->rows - 1;
2070 move(term, 0, 0, 0);
2071 erase_lots(term, FALSE, TRUE, TRUE);
2073 case 5: /* DECSCNM: reverse video */
2075 * Toggle reverse video. If we receive an OFF within the
2076 * visual bell timeout period after an ON, we trigger an
2077 * effective visual bell, so that ESC[?5hESC[?5l will
2078 * always be an actually _visible_ visual bell.
2080 ticks = GETTICKCOUNT();
2081 /* turn off a previous vbell to avoid inconsistencies */
2082 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2083 term->in_vbell = FALSE;
2084 if (term->rvideo && !state && /* we're turning it off... */
2085 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
2086 /* If there's no vbell timeout already, or this one lasts
2087 * longer, replace vbell_timeout with ours. */
2088 if (!term->in_vbell ||
2089 (term->rvbell_startpoint - term->vbell_startpoint <
2091 term->vbell_startpoint = term->rvbell_startpoint;
2092 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
2093 } else if (!term->rvideo && state) {
2094 /* This is an ON, so we notice the time and save it. */
2095 term->rvbell_startpoint = ticks;
2097 term->rvideo = state;
2098 term->seen_disp_event = TRUE;
2102 case 6: /* DECOM: DEC origin mode */
2103 term->dec_om = state;
2105 case 7: /* DECAWM: auto wrap */
2108 case 8: /* DECARM: auto key repeat */
2109 term->repeat_off = !state;
2111 case 10: /* DECEDM: set local edit mode */
2112 term->term_editing = state;
2113 if (term->ldisc) /* cause ldisc to notice changes */
2114 ldisc_send(term->ldisc, NULL, 0, 0);
2116 case 25: /* DECTCEM: enable/disable cursor */
2117 compatibility2(OTHER, VT220);
2118 term->cursor_on = state;
2119 term->seen_disp_event = TRUE;
2121 case 47: /* alternate screen */
2122 compatibility(OTHER);
2124 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
2127 case 1000: /* xterm mouse 1 */
2128 term->xterm_mouse = state ? 1 : 0;
2129 set_raw_mouse_mode(term->frontend, state);
2131 case 1002: /* xterm mouse 2 */
2132 term->xterm_mouse = state ? 2 : 0;
2133 set_raw_mouse_mode(term->frontend, state);
2135 case 1047: /* alternate screen */
2136 compatibility(OTHER);
2138 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
2141 case 1048: /* save/restore cursor */
2142 if (!term->cfg.no_alt_screen)
2143 save_cursor(term, state);
2144 if (!state) term->seen_disp_event = TRUE;
2146 case 1049: /* cursor & alternate screen */
2147 if (state && !term->cfg.no_alt_screen)
2148 save_cursor(term, state);
2149 if (!state) term->seen_disp_event = TRUE;
2150 compatibility(OTHER);
2152 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
2153 if (!state && !term->cfg.no_alt_screen)
2154 save_cursor(term, state);
2159 case 4: /* IRM: set insert mode */
2160 compatibility(VT102);
2161 term->insert = state;
2163 case 12: /* SRM: set echo mode */
2164 term->term_echoing = !state;
2165 if (term->ldisc) /* cause ldisc to notice changes */
2166 ldisc_send(term->ldisc, NULL, 0, 0);
2168 case 20: /* LNM: Return sends ... */
2169 term->cr_lf_return = state;
2171 case 34: /* WYULCURM: Make cursor BIG */
2172 compatibility2(OTHER, VT220);
2173 term->big_cursor = !state;
2178 * Process an OSC sequence: set window title or icon name.
2180 static void do_osc(Terminal *term)
2183 while (term->osc_strlen--)
2184 term->wordness[(unsigned char)
2185 term->osc_string[term->osc_strlen]] = term->esc_args[0];
2187 term->osc_string[term->osc_strlen] = '\0';
2188 switch (term->esc_args[0]) {
2191 if (!term->cfg.no_remote_wintitle)
2192 set_icon(term->frontend, term->osc_string);
2193 if (term->esc_args[0] == 1)
2195 /* fall through: parameter 0 means set both */
2198 if (!term->cfg.no_remote_wintitle)
2199 set_title(term->frontend, term->osc_string);
2206 * ANSI printing routines.
2208 static void term_print_setup(Terminal *term)
2210 bufchain_clear(&term->printer_buf);
2211 term->print_job = printer_start_job(term->cfg.printer);
2213 static void term_print_flush(Terminal *term)
2218 while ((size = bufchain_size(&term->printer_buf)) > 5) {
2219 bufchain_prefix(&term->printer_buf, &data, &len);
2222 printer_job_data(term->print_job, data, len);
2223 bufchain_consume(&term->printer_buf, len);
2226 static void term_print_finish(Terminal *term)
2232 if (!term->printing && !term->only_printing)
2233 return; /* we need do nothing */
2235 term_print_flush(term);
2236 while ((size = bufchain_size(&term->printer_buf)) > 0) {
2237 bufchain_prefix(&term->printer_buf, &data, &len);
2239 if (c == '\033' || c == '\233') {
2240 bufchain_consume(&term->printer_buf, size);
2243 printer_job_data(term->print_job, &c, 1);
2244 bufchain_consume(&term->printer_buf, 1);
2247 printer_finish_job(term->print_job);
2248 term->print_job = NULL;
2249 term->printing = term->only_printing = FALSE;
2253 * Remove everything currently in `inbuf' and stick it up on the
2254 * in-memory display. There's a big state machine in here to
2255 * process escape sequences...
2257 void term_out(Terminal *term)
2261 unsigned char localbuf[256], *chars;
2266 chars = NULL; /* placate compiler warnings */
2267 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
2271 bufchain_prefix(&term->inbuf, &ret, &nchars);
2272 if (nchars > sizeof(localbuf))
2273 nchars = sizeof(localbuf);
2274 memcpy(localbuf, ret, nchars);
2275 bufchain_consume(&term->inbuf, nchars);
2277 assert(chars != NULL);
2283 * Optionally log the session traffic to a file. Useful for
2284 * debugging and possibly also useful for actual logging.
2286 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
2287 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
2293 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2294 * be able to display 8-bit characters, but I'll let that go 'cause
2299 * If we're printing, add the character to the printer
2302 if (term->printing) {
2303 bufchain_add(&term->printer_buf, &c, 1);
2306 * If we're in print-only mode, we use a much simpler
2307 * state machine designed only to recognise the ESC[4i
2308 * termination sequence.
2310 if (term->only_printing) {
2312 term->print_state = 1;
2313 else if (c == (unsigned char)'\233')
2314 term->print_state = 2;
2315 else if (c == '[' && term->print_state == 1)
2316 term->print_state = 2;
2317 else if (c == '4' && term->print_state == 2)
2318 term->print_state = 3;
2319 else if (c == 'i' && term->print_state == 3)
2320 term->print_state = 4;
2322 term->print_state = 0;
2323 if (term->print_state == 4) {
2324 term_print_finish(term);
2330 /* First see about all those translations. */
2331 if (term->termstate == TOPLEVEL) {
2333 switch (term->utf_state) {
2336 /* UTF-8 must be stateless so we ignore iso2022. */
2337 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2338 c = term->ucsdata->unitab_ctrl[c];
2339 else c = ((unsigned char)c) | CSET_ASCII;
2341 } else if ((c & 0xe0) == 0xc0) {
2342 term->utf_size = term->utf_state = 1;
2343 term->utf_char = (c & 0x1f);
2344 } else if ((c & 0xf0) == 0xe0) {
2345 term->utf_size = term->utf_state = 2;
2346 term->utf_char = (c & 0x0f);
2347 } else if ((c & 0xf8) == 0xf0) {
2348 term->utf_size = term->utf_state = 3;
2349 term->utf_char = (c & 0x07);
2350 } else if ((c & 0xfc) == 0xf8) {
2351 term->utf_size = term->utf_state = 4;
2352 term->utf_char = (c & 0x03);
2353 } else if ((c & 0xfe) == 0xfc) {
2354 term->utf_size = term->utf_state = 5;
2355 term->utf_char = (c & 0x01);
2366 if ((c & 0xC0) != 0x80) {
2369 term->utf_state = 0;
2372 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2373 if (--term->utf_state)
2378 /* Is somebody trying to be evil! */
2380 (c < 0x800 && term->utf_size >= 2) ||
2381 (c < 0x10000 && term->utf_size >= 3) ||
2382 (c < 0x200000 && term->utf_size >= 4) ||
2383 (c < 0x4000000 && term->utf_size >= 5))
2386 /* Unicode line separator and paragraph separator are CR-LF */
2387 if (c == 0x2028 || c == 0x2029)
2390 /* High controls are probably a Baaad idea too. */
2394 /* The UTF-16 surrogates are not nice either. */
2395 /* The standard give the option of decoding these:
2396 * I don't want to! */
2397 if (c >= 0xD800 && c < 0xE000)
2400 /* ISO 10646 characters now limited to UTF-16 range. */
2404 /* This is currently a TagPhobic application.. */
2405 if (c >= 0xE0000 && c <= 0xE007F)
2408 /* U+FEFF is best seen as a null. */
2411 /* But U+FFFE is an error. */
2412 if (c == 0xFFFE || c == 0xFFFF)
2417 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2418 else if(term->sco_acs &&
2419 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
2421 if (term->sco_acs == 2) c |= 0x80;
2424 switch (term->cset_attr[term->cset]) {
2426 * Linedraw characters are different from 'ESC ( B'
2427 * only for a small range. For ones outside that
2428 * range, make sure we use the same font as well as
2429 * the same encoding.
2432 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2433 c = term->ucsdata->unitab_ctrl[c];
2435 c = ((unsigned char) c) | CSET_LINEDRW;
2439 /* If UK-ASCII, make the '#' a LineDraw Pound */
2441 c = '}' | CSET_LINEDRW;
2444 /*FALLTHROUGH*/ case CSET_ASCII:
2445 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2446 c = term->ucsdata->unitab_ctrl[c];
2448 c = ((unsigned char) c) | CSET_ASCII;
2451 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
2457 /* How about C1 controls ? */
2458 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2459 !term->vt52_mode && has_compat(VT220)) {
2460 term->termstate = SEEN_ESC;
2461 term->esc_query = FALSE;
2462 c = '@' + (c & 0x1F);
2465 /* Or the GL control. */
2466 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2467 if (term->curs.x && !term->wrapnext)
2469 term->wrapnext = FALSE;
2470 /* destructive backspace might be disabled */
2471 if (!term->cfg.no_dbackspace) {
2472 check_boundary(term, term->curs.x, term->curs.y);
2473 check_boundary(term, term->curs.x+1, term->curs.y);
2474 copy_termchar(scrlineptr(term->curs.y),
2475 term->curs.x, &term->erase_char);
2478 /* Or normal C0 controls. */
2479 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
2481 case '\005': /* ENQ: terminal type query */
2482 /* Strictly speaking this is VT100 but a VT100 defaults to
2483 * no response. Other terminals respond at their option.
2485 * Don't put a CR in the default string as this tends to
2486 * upset some weird software.
2488 * An xterm returns "xterm" (5 characters)
2490 compatibility(ANSIMIN);
2492 char abuf[256], *s, *d;
2494 for (s = term->cfg.answerback, d = abuf; *s; s++) {
2496 if (*s >= 'a' && *s <= 'z')
2497 *d++ = (*s - ('a' - 1));
2498 else if ((*s >= '@' && *s <= '_') ||
2499 *s == '?' || (*s & 0x80))
2504 } else if (*s == '^') {
2509 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2513 case '\007': /* BEL: Bell */
2515 struct beeptime *newbeep;
2516 unsigned long ticks;
2518 ticks = GETTICKCOUNT();
2520 if (!term->beep_overloaded) {
2521 newbeep = snew(struct beeptime);
2522 newbeep->ticks = ticks;
2523 newbeep->next = NULL;
2524 if (!term->beephead)
2525 term->beephead = newbeep;
2527 term->beeptail->next = newbeep;
2528 term->beeptail = newbeep;
2533 * Throw out any beeps that happened more than
2536 while (term->beephead &&
2537 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
2538 struct beeptime *tmp = term->beephead;
2539 term->beephead = tmp->next;
2541 if (!term->beephead)
2542 term->beeptail = NULL;
2546 if (term->cfg.bellovl && term->beep_overloaded &&
2547 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
2549 * If we're currently overloaded and the
2550 * last beep was more than s seconds ago,
2551 * leave overload mode.
2553 term->beep_overloaded = FALSE;
2554 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2555 term->nbeeps >= term->cfg.bellovl_n) {
2557 * Now, if we have n or more beeps
2558 * remaining in the queue, go into overload
2561 term->beep_overloaded = TRUE;
2563 term->lastbeep = ticks;
2566 * Perform an actual beep if we're not overloaded.
2568 if (!term->cfg.bellovl || !term->beep_overloaded) {
2569 beep(term->frontend, term->cfg.beep);
2570 if (term->cfg.beep == BELL_VISUAL) {
2571 term->in_vbell = TRUE;
2572 term->vbell_startpoint = ticks;
2576 term->seen_disp_event = TRUE;
2579 case '\b': /* BS: Back space */
2580 if (term->curs.x == 0 &&
2581 (term->curs.y == 0 || term->wrap == 0))
2583 else if (term->curs.x == 0 && term->curs.y > 0)
2584 term->curs.x = term->cols - 1, term->curs.y--;
2585 else if (term->wrapnext)
2586 term->wrapnext = FALSE;
2589 term->seen_disp_event = TRUE;
2591 case '\016': /* LS1: Locking-shift one */
2592 compatibility(VT100);
2595 case '\017': /* LS0: Locking-shift zero */
2596 compatibility(VT100);
2599 case '\033': /* ESC: Escape */
2600 if (term->vt52_mode)
2601 term->termstate = VT52_ESC;
2603 compatibility(ANSIMIN);
2604 term->termstate = SEEN_ESC;
2605 term->esc_query = FALSE;
2608 case '\015': /* CR: Carriage return */
2610 term->wrapnext = FALSE;
2611 term->seen_disp_event = TRUE;
2612 term->paste_hold = 0;
2614 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2616 case '\014': /* FF: Form feed */
2617 if (has_compat(SCOANSI)) {
2618 move(term, 0, 0, 0);
2619 erase_lots(term, FALSE, FALSE, TRUE);
2621 term->wrapnext = FALSE;
2622 term->seen_disp_event = 1;
2625 case '\013': /* VT: Line tabulation */
2626 compatibility(VT100);
2627 case '\012': /* LF: Line feed */
2628 if (term->curs.y == term->marg_b)
2629 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2630 else if (term->curs.y < term->rows - 1)
2632 if (term->cfg.lfhascr)
2634 term->wrapnext = FALSE;
2635 term->seen_disp_event = 1;
2636 term->paste_hold = 0;
2638 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2640 case '\t': /* HT: Character tabulation */
2642 pos old_curs = term->curs;
2643 termline *ldata = scrlineptr(term->curs.y);
2647 } while (term->curs.x < term->cols - 1 &&
2648 !term->tabs[term->curs.x]);
2650 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
2651 if (term->curs.x >= term->cols / 2)
2652 term->curs.x = term->cols / 2 - 1;
2654 if (term->curs.x >= term->cols)
2655 term->curs.x = term->cols - 1;
2658 check_selection(term, old_curs, term->curs);
2660 term->seen_disp_event = TRUE;
2664 switch (term->termstate) {
2666 /* Only graphic characters get this far;
2667 * ctrls are stripped above */
2669 termline *cline = scrlineptr(term->curs.y);
2674 width = wcwidth((wchar_t) c);
2676 if (term->wrapnext && term->wrap && width > 0) {
2677 cline->lattr |= LATTR_WRAPPED;
2678 if (term->curs.y == term->marg_b)
2679 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2680 else if (term->curs.y < term->rows - 1)
2683 term->wrapnext = FALSE;
2684 cline = scrlineptr(term->curs.y);
2686 if (term->insert && width > 0)
2688 if (term->selstate != NO_SELECTION) {
2689 pos cursplus = term->curs;
2691 check_selection(term, term->curs, cursplus);
2693 if (((c & CSET_MASK) == CSET_ASCII ||
2694 (c & CSET_MASK) == 0) &&
2696 logtraffic(term->logctx, (unsigned char) c,
2702 * If we're about to display a double-width
2703 * character starting in the rightmost
2704 * column, then we do something special
2705 * instead. We must print a space in the
2706 * last column of the screen, then wrap;
2707 * and we also set LATTR_WRAPPED2 which
2708 * instructs subsequent cut-and-pasting not
2709 * only to splice this line to the one
2710 * after it, but to ignore the space in the
2711 * last character position as well.
2712 * (Because what was actually output to the
2713 * terminal was presumably just a sequence
2714 * of CJK characters, and we don't want a
2715 * space to be pasted in the middle of
2716 * those just because they had the
2717 * misfortune to start in the wrong parity
2718 * column. xterm concurs.)
2720 check_boundary(term, term->curs.x, term->curs.y);
2721 check_boundary(term, term->curs.x+2, term->curs.y);
2722 if (term->curs.x == term->cols-1) {
2723 copy_termchar(cline, term->curs.x,
2725 cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;
2726 if (term->curs.y == term->marg_b)
2727 scroll(term, term->marg_t, term->marg_b,
2729 else if (term->curs.y < term->rows - 1)
2732 /* Now we must check_boundary again, of course. */
2733 check_boundary(term, term->curs.x, term->curs.y);
2734 check_boundary(term, term->curs.x+2, term->curs.y);
2738 clear_cc(cline, term->curs.x);
2739 cline->chars[term->curs.x].chr = c;
2740 cline->chars[term->curs.x].attr = term->curr_attr;
2745 clear_cc(cline, term->curs.x);
2746 cline->chars[term->curs.x].chr = UCSWIDE;
2747 cline->chars[term->curs.x].attr = term->curr_attr;
2751 check_boundary(term, term->curs.x, term->curs.y);
2752 check_boundary(term, term->curs.x+1, term->curs.y);
2755 clear_cc(cline, term->curs.x);
2756 cline->chars[term->curs.x].chr = c;
2757 cline->chars[term->curs.x].attr = term->curr_attr;
2761 if (term->curs.x > 0) {
2762 int x = term->curs.x - 1;
2764 /* If we're in wrapnext state, the character
2765 * to combine with is _here_, not to our left. */
2770 * If the previous character is
2771 * UCSWIDE, back up another one.
2773 if (cline->chars[x].chr == UCSWIDE) {
2778 add_cc(cline, x, c);
2779 term->seen_disp_event = 1;
2786 if (term->curs.x == term->cols) {
2788 term->wrapnext = TRUE;
2789 if (term->wrap && term->vt52_mode) {
2790 cline->lattr |= LATTR_WRAPPED;
2791 if (term->curs.y == term->marg_b)
2792 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2793 else if (term->curs.y < term->rows - 1)
2796 term->wrapnext = FALSE;
2799 term->seen_disp_event = 1;
2805 * This state is virtually identical to SEEN_ESC, with the
2806 * exception that we have an OSC sequence in the pipeline,
2807 * and _if_ we see a backslash, we process it.
2811 term->termstate = TOPLEVEL;
2814 /* else fall through */
2816 if (c >= ' ' && c <= '/') {
2817 if (term->esc_query)
2818 term->esc_query = -1;
2820 term->esc_query = c;
2823 term->termstate = TOPLEVEL;
2824 switch (ANSI(c, term->esc_query)) {
2825 case '[': /* enter CSI mode */
2826 term->termstate = SEEN_CSI;
2827 term->esc_nargs = 1;
2828 term->esc_args[0] = ARG_DEFAULT;
2829 term->esc_query = FALSE;
2831 case ']': /* OSC: xterm escape sequences */
2832 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2833 compatibility(OTHER);
2834 term->termstate = SEEN_OSC;
2835 term->esc_args[0] = 0;
2837 case '7': /* DECSC: save cursor */
2838 compatibility(VT100);
2839 save_cursor(term, TRUE);
2841 case '8': /* DECRC: restore cursor */
2842 compatibility(VT100);
2843 save_cursor(term, FALSE);
2844 term->seen_disp_event = TRUE;
2846 case '=': /* DECKPAM: Keypad application mode */
2847 compatibility(VT100);
2848 term->app_keypad_keys = TRUE;
2850 case '>': /* DECKPNM: Keypad numeric mode */
2851 compatibility(VT100);
2852 term->app_keypad_keys = FALSE;
2854 case 'D': /* IND: exactly equivalent to LF */
2855 compatibility(VT100);
2856 if (term->curs.y == term->marg_b)
2857 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2858 else if (term->curs.y < term->rows - 1)
2860 term->wrapnext = FALSE;
2861 term->seen_disp_event = TRUE;
2863 case 'E': /* NEL: exactly equivalent to CR-LF */
2864 compatibility(VT100);
2866 if (term->curs.y == term->marg_b)
2867 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2868 else if (term->curs.y < term->rows - 1)
2870 term->wrapnext = FALSE;
2871 term->seen_disp_event = TRUE;
2873 case 'M': /* RI: reverse index - backwards LF */
2874 compatibility(VT100);
2875 if (term->curs.y == term->marg_t)
2876 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2877 else if (term->curs.y > 0)
2879 term->wrapnext = FALSE;
2880 term->seen_disp_event = TRUE;
2882 case 'Z': /* DECID: terminal type query */
2883 compatibility(VT100);
2885 ldisc_send(term->ldisc, term->id_string,
2886 strlen(term->id_string), 0);
2888 case 'c': /* RIS: restore power-on settings */
2889 compatibility(VT100);
2891 if (term->ldisc) /* cause ldisc to notice changes */
2892 ldisc_send(term->ldisc, NULL, 0, 0);
2893 if (term->reset_132) {
2894 if (!term->cfg.no_remote_resize)
2895 request_resize(term->frontend, 80, term->rows);
2896 term->reset_132 = 0;
2899 term->seen_disp_event = TRUE;
2901 case 'H': /* HTS: set a tab */
2902 compatibility(VT100);
2903 term->tabs[term->curs.x] = TRUE;
2906 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2907 compatibility(VT100);
2913 for (i = 0; i < term->rows; i++) {
2914 ldata = scrlineptr(i);
2915 for (j = 0; j < term->cols; j++) {
2916 copy_termchar(ldata, j,
2917 &term->basic_erase_char);
2918 ldata->chars[j].chr = 'E';
2920 ldata->lattr = LATTR_NORM;
2923 term->seen_disp_event = TRUE;
2924 scrtop.x = scrtop.y = 0;
2926 scrbot.y = term->rows;
2927 check_selection(term, scrtop, scrbot);
2931 case ANSI('3', '#'):
2932 case ANSI('4', '#'):
2933 case ANSI('5', '#'):
2934 case ANSI('6', '#'):
2935 compatibility(VT100);
2939 switch (ANSI(c, term->esc_query)) {
2940 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2943 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2946 case ANSI('5', '#'): /* DECSWL: normal */
2947 nlattr = LATTR_NORM;
2949 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2950 nlattr = LATTR_WIDE;
2953 scrlineptr(term->curs.y)->lattr = nlattr;
2956 /* GZD4: G0 designate 94-set */
2957 case ANSI('A', '('):
2958 compatibility(VT100);
2959 if (!term->cfg.no_remote_charset)
2960 term->cset_attr[0] = CSET_GBCHR;
2962 case ANSI('B', '('):
2963 compatibility(VT100);
2964 if (!term->cfg.no_remote_charset)
2965 term->cset_attr[0] = CSET_ASCII;
2967 case ANSI('0', '('):
2968 compatibility(VT100);
2969 if (!term->cfg.no_remote_charset)
2970 term->cset_attr[0] = CSET_LINEDRW;
2972 case ANSI('U', '('):
2973 compatibility(OTHER);
2974 if (!term->cfg.no_remote_charset)
2975 term->cset_attr[0] = CSET_SCOACS;
2977 /* G1D4: G1-designate 94-set */
2978 case ANSI('A', ')'):
2979 compatibility(VT100);
2980 if (!term->cfg.no_remote_charset)
2981 term->cset_attr[1] = CSET_GBCHR;
2983 case ANSI('B', ')'):
2984 compatibility(VT100);
2985 if (!term->cfg.no_remote_charset)
2986 term->cset_attr[1] = CSET_ASCII;
2988 case ANSI('0', ')'):
2989 compatibility(VT100);
2990 if (!term->cfg.no_remote_charset)
2991 term->cset_attr[1] = CSET_LINEDRW;
2993 case ANSI('U', ')'):
2994 compatibility(OTHER);
2995 if (!term->cfg.no_remote_charset)
2996 term->cset_attr[1] = CSET_SCOACS;
2998 /* DOCS: Designate other coding system */
2999 case ANSI('8', '%'): /* Old Linux code */
3000 case ANSI('G', '%'):
3001 compatibility(OTHER);
3002 if (!term->cfg.no_remote_charset)
3005 case ANSI('@', '%'):
3006 compatibility(OTHER);
3007 if (!term->cfg.no_remote_charset)
3013 term->termstate = TOPLEVEL; /* default */
3015 if (term->esc_nargs <= ARGS_MAX) {
3016 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
3017 term->esc_args[term->esc_nargs - 1] = 0;
3018 term->esc_args[term->esc_nargs - 1] =
3019 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
3021 term->termstate = SEEN_CSI;
3022 } else if (c == ';') {
3023 if (++term->esc_nargs <= ARGS_MAX)
3024 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
3025 term->termstate = SEEN_CSI;
3026 } else if (c < '@') {
3027 if (term->esc_query)
3028 term->esc_query = -1;
3030 term->esc_query = TRUE;
3032 term->esc_query = c;
3033 term->termstate = SEEN_CSI;
3035 switch (ANSI(c, term->esc_query)) {
3036 case 'A': /* CUU: move up N lines */
3037 move(term, term->curs.x,
3038 term->curs.y - def(term->esc_args[0], 1), 1);
3039 term->seen_disp_event = TRUE;
3041 case 'e': /* VPR: move down N lines */
3042 compatibility(ANSI);
3044 case 'B': /* CUD: Cursor down */
3045 move(term, term->curs.x,
3046 term->curs.y + def(term->esc_args[0], 1), 1);
3047 term->seen_disp_event = TRUE;
3049 case ANSI('c', '>'): /* DA: report xterm version */
3050 compatibility(OTHER);
3051 /* this reports xterm version 136 so that VIM can
3052 use the drag messages from the mouse reporting */
3054 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
3056 case 'a': /* HPR: move right N cols */
3057 compatibility(ANSI);
3059 case 'C': /* CUF: Cursor right */
3060 move(term, term->curs.x + def(term->esc_args[0], 1),
3062 term->seen_disp_event = TRUE;
3064 case 'D': /* CUB: move left N cols */
3065 move(term, term->curs.x - def(term->esc_args[0], 1),
3067 term->seen_disp_event = TRUE;
3069 case 'E': /* CNL: move down N lines and CR */
3070 compatibility(ANSI);
3072 term->curs.y + def(term->esc_args[0], 1), 1);
3073 term->seen_disp_event = TRUE;
3075 case 'F': /* CPL: move up N lines and CR */
3076 compatibility(ANSI);
3078 term->curs.y - def(term->esc_args[0], 1), 1);
3079 term->seen_disp_event = TRUE;
3082 case '`': /* HPA: set horizontal posn */
3083 compatibility(ANSI);
3084 move(term, def(term->esc_args[0], 1) - 1,
3086 term->seen_disp_event = TRUE;
3088 case 'd': /* VPA: set vertical posn */
3089 compatibility(ANSI);
3090 move(term, term->curs.x,
3091 ((term->dec_om ? term->marg_t : 0) +
3092 def(term->esc_args[0], 1) - 1),
3093 (term->dec_om ? 2 : 0));
3094 term->seen_disp_event = TRUE;
3097 case 'f': /* HVP: set horz and vert posns at once */
3098 if (term->esc_nargs < 2)
3099 term->esc_args[1] = ARG_DEFAULT;
3100 move(term, def(term->esc_args[1], 1) - 1,
3101 ((term->dec_om ? term->marg_t : 0) +
3102 def(term->esc_args[0], 1) - 1),
3103 (term->dec_om ? 2 : 0));
3104 term->seen_disp_event = TRUE;
3106 case 'J': /* ED: erase screen or parts of it */
3108 unsigned int i = def(term->esc_args[0], 0) + 1;
3111 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
3114 term->seen_disp_event = TRUE;
3116 case 'K': /* EL: erase line or parts of it */
3118 unsigned int i = def(term->esc_args[0], 0) + 1;
3121 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
3123 term->seen_disp_event = TRUE;
3125 case 'L': /* IL: insert 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), FALSE);
3130 term->seen_disp_event = TRUE;
3132 case 'M': /* DL: delete lines */
3133 compatibility(VT102);
3134 if (term->curs.y <= term->marg_b)
3135 scroll(term, term->curs.y, term->marg_b,
3136 def(term->esc_args[0], 1),
3138 term->seen_disp_event = TRUE;
3140 case '@': /* ICH: insert chars */
3141 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3142 compatibility(VT102);
3143 insch(term, def(term->esc_args[0], 1));
3144 term->seen_disp_event = TRUE;
3146 case 'P': /* DCH: delete chars */
3147 compatibility(VT102);
3148 insch(term, -def(term->esc_args[0], 1));
3149 term->seen_disp_event = TRUE;
3151 case 'c': /* DA: terminal type query */
3152 compatibility(VT100);
3153 /* This is the response for a VT102 */
3155 ldisc_send(term->ldisc, term->id_string,
3156 strlen(term->id_string), 0);
3158 case 'n': /* DSR: cursor position query */
3160 if (term->esc_args[0] == 6) {
3162 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
3164 ldisc_send(term->ldisc, buf, strlen(buf), 0);
3165 } else if (term->esc_args[0] == 5) {
3166 ldisc_send(term->ldisc, "\033[0n", 4, 0);
3170 case 'h': /* SM: toggle modes to high */
3172 compatibility(VT100);
3175 for (i = 0; i < term->esc_nargs; i++)
3176 toggle_mode(term, term->esc_args[i],
3177 term->esc_query, TRUE);
3180 case 'i': /* MC: Media copy */
3182 compatibility(VT100);
3184 if (term->esc_nargs != 1) break;
3185 if (term->esc_args[0] == 5 && *term->cfg.printer) {
3186 term->printing = TRUE;
3187 term->only_printing = !term->esc_query;
3188 term->print_state = 0;
3189 term_print_setup(term);
3190 } else if (term->esc_args[0] == 4 &&
3192 term_print_finish(term);
3196 case 'l': /* RM: toggle modes to low */
3198 compatibility(VT100);
3201 for (i = 0; i < term->esc_nargs; i++)
3202 toggle_mode(term, term->esc_args[i],
3203 term->esc_query, FALSE);
3206 case 'g': /* TBC: clear tabs */
3207 compatibility(VT100);
3208 if (term->esc_nargs == 1) {
3209 if (term->esc_args[0] == 0) {
3210 term->tabs[term->curs.x] = FALSE;
3211 } else if (term->esc_args[0] == 3) {
3213 for (i = 0; i < term->cols; i++)
3214 term->tabs[i] = FALSE;
3218 case 'r': /* DECSTBM: set scroll margins */
3219 compatibility(VT100);
3220 if (term->esc_nargs <= 2) {
3222 top = def(term->esc_args[0], 1) - 1;
3223 bot = (term->esc_nargs <= 1
3224 || term->esc_args[1] == 0 ?
3226 def(term->esc_args[1], term->rows)) - 1;
3227 if (bot >= term->rows)
3228 bot = term->rows - 1;
3229 /* VTTEST Bug 9 - if region is less than 2 lines
3230 * don't change region.
3232 if (bot - top > 0) {
3237 * I used to think the cursor should be
3238 * placed at the top of the newly marginned
3239 * area. Apparently not: VMS TPU falls over
3242 * Well actually it should for
3245 term->curs.y = (term->dec_om ?
3247 term->seen_disp_event = TRUE;
3251 case 'm': /* SGR: set graphics rendition */
3254 * A VT100 without the AVO only had one
3255 * attribute, either underline or
3256 * reverse video depending on the
3257 * cursor type, this was selected by
3261 * This is sometimes DIM, eg on the
3264 * This is sometimes INVIS various ANSI.
3266 * This like 22 disables BOLD, DIM and INVIS
3268 * The ANSI colours appear on any
3269 * terminal that has colour (obviously)
3270 * but the interaction between sgr0 and
3271 * the colours varies but is usually
3272 * related to the background colour
3273 * erase item. The interaction between
3274 * colour attributes and the mono ones
3275 * is also very implementation
3278 * The 39 and 49 attributes are likely
3279 * to be unimplemented.
3282 for (i = 0; i < term->esc_nargs; i++) {
3283 switch (def(term->esc_args[i], 0)) {
3284 case 0: /* restore defaults */
3285 term->curr_attr = term->default_attr;
3287 case 1: /* enable bold */
3288 compatibility(VT100AVO);
3289 term->curr_attr |= ATTR_BOLD;
3291 case 21: /* (enable double underline) */
3292 compatibility(OTHER);
3293 case 4: /* enable underline */
3294 compatibility(VT100AVO);
3295 term->curr_attr |= ATTR_UNDER;
3297 case 5: /* enable blink */
3298 compatibility(VT100AVO);
3299 term->curr_attr |= ATTR_BLINK;
3301 case 6: /* SCO light bkgrd */
3302 compatibility(SCOANSI);
3303 term->blink_is_real = FALSE;
3304 term->curr_attr |= ATTR_BLINK;
3306 case 7: /* enable reverse video */
3307 term->curr_attr |= ATTR_REVERSE;
3309 case 10: /* SCO acs off */
3310 compatibility(SCOANSI);
3311 if (term->cfg.no_remote_charset) break;
3312 term->sco_acs = 0; break;
3313 case 11: /* SCO acs on */
3314 compatibility(SCOANSI);
3315 if (term->cfg.no_remote_charset) break;
3316 term->sco_acs = 1; break;
3317 case 12: /* SCO acs on, |0x80 */
3318 compatibility(SCOANSI);
3319 if (term->cfg.no_remote_charset) break;
3320 term->sco_acs = 2; break;
3321 case 22: /* disable bold */
3322 compatibility2(OTHER, VT220);
3323 term->curr_attr &= ~ATTR_BOLD;
3325 case 24: /* disable underline */
3326 compatibility2(OTHER, VT220);
3327 term->curr_attr &= ~ATTR_UNDER;
3329 case 25: /* disable blink */
3330 compatibility2(OTHER, VT220);
3331 term->curr_attr &= ~ATTR_BLINK;
3333 case 27: /* disable reverse video */
3334 compatibility2(OTHER, VT220);
3335 term->curr_attr &= ~ATTR_REVERSE;
3346 term->curr_attr &= ~ATTR_FGMASK;
3348 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
3358 /* xterm-style bright foreground */
3359 term->curr_attr &= ~ATTR_FGMASK;
3361 ((term->esc_args[i] - 90 + 16)
3364 case 39: /* default-foreground */
3365 term->curr_attr &= ~ATTR_FGMASK;
3366 term->curr_attr |= ATTR_DEFFG;
3377 term->curr_attr &= ~ATTR_BGMASK;
3379 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
3389 /* xterm-style bright background */
3390 term->curr_attr &= ~ATTR_BGMASK;
3392 ((term->esc_args[i] - 100 + 16)
3395 case 49: /* default-background */
3396 term->curr_attr &= ~ATTR_BGMASK;
3397 term->curr_attr |= ATTR_DEFBG;
3401 set_erase_char(term);
3404 case 's': /* save cursor */
3405 save_cursor(term, TRUE);
3407 case 'u': /* restore cursor */
3408 save_cursor(term, FALSE);
3409 term->seen_disp_event = TRUE;
3411 case 't': /* DECSLPP: set page size - ie window height */
3413 * VT340/VT420 sequence DECSLPP, DEC only allows values
3414 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3415 * illegal values (eg first arg 1..9) for window changing
3418 if (term->esc_nargs <= 1
3419 && (term->esc_args[0] < 1 ||
3420 term->esc_args[0] >= 24)) {
3421 compatibility(VT340TEXT);
3422 if (!term->cfg.no_remote_resize)
3423 request_resize(term->frontend, term->cols,
3424 def(term->esc_args[0], 24));
3426 } else if (term->esc_nargs >= 1 &&
3427 term->esc_args[0] >= 1 &&
3428 term->esc_args[0] < 24) {
3429 compatibility(OTHER);
3431 switch (term->esc_args[0]) {
3435 set_iconic(term->frontend, FALSE);
3438 set_iconic(term->frontend, TRUE);
3441 if (term->esc_nargs >= 3) {
3442 if (!term->cfg.no_remote_resize)
3443 move_window(term->frontend,
3444 def(term->esc_args[1], 0),
3445 def(term->esc_args[2], 0));
3449 /* We should resize the window to a given
3450 * size in pixels here, but currently our
3451 * resizing code isn't healthy enough to
3456 set_zorder(term->frontend, TRUE);
3459 /* move to bottom */
3460 set_zorder(term->frontend, FALSE);
3463 refresh_window(term->frontend);
3466 if (term->esc_nargs >= 3) {
3467 if (!term->cfg.no_remote_resize)
3468 request_resize(term->frontend,
3469 def(term->esc_args[2], term->cfg.width),
3470 def(term->esc_args[1], term->cfg.height));
3474 if (term->esc_nargs >= 2)
3475 set_zoomed(term->frontend,
3481 ldisc_send(term->ldisc,
3482 is_iconic(term->frontend) ?
3483 "\033[1t" : "\033[2t", 4, 0);
3487 get_window_pos(term->frontend, &x, &y);
3488 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3489 ldisc_send(term->ldisc, buf, len, 0);
3494 get_window_pixels(term->frontend, &x, &y);
3495 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3496 ldisc_send(term->ldisc, buf, len, 0);
3501 len = sprintf(buf, "\033[8;%d;%dt",
3502 term->rows, term->cols);
3503 ldisc_send(term->ldisc, buf, len, 0);
3508 * Hmmm. Strictly speaking we
3509 * should return `the size of the
3510 * screen in characters', but
3511 * that's not easy: (a) window
3512 * furniture being what it is it's
3513 * hard to compute, and (b) in
3514 * resize-font mode maximising the
3515 * window wouldn't change the
3516 * number of characters. *shrug*. I
3517 * think we'll ignore it for the
3518 * moment and see if anyone
3519 * complains, and then ask them
3520 * what they would like it to do.
3525 !term->cfg.no_remote_qtitle) {
3526 p = get_window_title(term->frontend, TRUE);
3528 ldisc_send(term->ldisc, "\033]L", 3, 0);
3529 ldisc_send(term->ldisc, p, len, 0);
3530 ldisc_send(term->ldisc, "\033\\", 2, 0);
3535 !term->cfg.no_remote_qtitle) {
3536 p = get_window_title(term->frontend,FALSE);
3538 ldisc_send(term->ldisc, "\033]l", 3, 0);
3539 ldisc_send(term->ldisc, p, len, 0);
3540 ldisc_send(term->ldisc, "\033\\", 2, 0);
3546 case 'S': /* SU: Scroll up */
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 'T': /* SD: Scroll down */
3554 compatibility(SCOANSI);
3555 scroll(term, term->marg_t, term->marg_b,
3556 -def(term->esc_args[0], 1), TRUE);
3557 term->wrapnext = FALSE;
3558 term->seen_disp_event = TRUE;
3560 case ANSI('|', '*'): /* DECSNLS */
3562 * Set number of lines on screen
3563 * VT420 uses VGA like hardware and can
3564 * support any size in reasonable range
3565 * (24..49 AIUI) with no default specified.
3567 compatibility(VT420);
3568 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
3569 if (!term->cfg.no_remote_resize)
3570 request_resize(term->frontend, term->cols,
3571 def(term->esc_args[0],
3576 case ANSI('|', '$'): /* DECSCPP */
3578 * Set number of columns per page
3579 * Docs imply range is only 80 or 132, but
3582 compatibility(VT340TEXT);
3583 if (term->esc_nargs <= 1) {
3584 if (!term->cfg.no_remote_resize)
3585 request_resize(term->frontend,
3586 def(term->esc_args[0],
3587 term->cfg.width), term->rows);
3591 case 'X': /* ECH: write N spaces w/o moving cursor */
3592 /* XXX VTTEST says this is vt220, vt510 manual
3594 compatibility(ANSIMIN);
3596 int n = def(term->esc_args[0], 1);
3598 int p = term->curs.x;
3599 termline *cline = scrlineptr(term->curs.y);
3601 if (n > term->cols - term->curs.x)
3602 n = term->cols - term->curs.x;
3603 cursplus = term->curs;
3605 check_boundary(term, term->curs.x, term->curs.y);
3606 check_boundary(term, term->curs.x+n, term->curs.y);
3607 check_selection(term, term->curs, cursplus);
3609 copy_termchar(cline, p++,
3611 term->seen_disp_event = TRUE;
3614 case 'x': /* DECREQTPARM: report terminal characteristics */
3615 compatibility(VT100);
3618 int i = def(term->esc_args[0], 0);
3619 if (i == 0 || i == 1) {
3620 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3622 ldisc_send(term->ldisc, buf, 20, 0);
3626 case 'Z': /* CBT: BackTab for xterm */
3627 compatibility(OTHER);
3629 int i = def(term->esc_args[0], 1);
3630 pos old_curs = term->curs;
3632 for(;i>0 && term->curs.x>0; i--) {
3635 } while (term->curs.x >0 &&
3636 !term->tabs[term->curs.x]);
3638 check_selection(term, old_curs, term->curs);
3641 case ANSI('c', '='): /* Hide or Show Cursor */
3642 compatibility(SCOANSI);
3643 switch(term->esc_args[0]) {
3644 case 0: /* hide cursor */
3645 term->cursor_on = FALSE;
3647 case 1: /* restore cursor */
3648 term->big_cursor = FALSE;
3649 term->cursor_on = TRUE;
3651 case 2: /* block cursor */
3652 term->big_cursor = TRUE;
3653 term->cursor_on = TRUE;
3657 case ANSI('C', '='):
3659 * set cursor start on scanline esc_args[0] and
3660 * end on scanline esc_args[1].If you set
3661 * the bottom scan line to a value less than
3662 * the top scan line, the cursor will disappear.
3664 compatibility(SCOANSI);
3665 if (term->esc_nargs >= 2) {
3666 if (term->esc_args[0] > term->esc_args[1])
3667 term->cursor_on = FALSE;
3669 term->cursor_on = TRUE;
3672 case ANSI('D', '='):
3673 compatibility(SCOANSI);
3674 term->blink_is_real = FALSE;
3675 if (term->esc_args[0]>=1)
3676 term->curr_attr |= ATTR_BLINK;
3678 term->curr_attr &= ~ATTR_BLINK;
3680 case ANSI('E', '='):
3681 compatibility(SCOANSI);
3682 term->blink_is_real = (term->esc_args[0] >= 1);
3684 case ANSI('F', '='): /* set normal foreground */
3685 compatibility(SCOANSI);
3686 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3688 (sco2ansicolour[term->esc_args[0] & 0x7] |
3689 ((term->esc_args[0] & 0x8) << 1)) <<
3691 term->curr_attr &= ~ATTR_FGMASK;
3692 term->curr_attr |= colour;
3693 term->default_attr &= ~ATTR_FGMASK;
3694 term->default_attr |= colour;
3697 case ANSI('G', '='): /* set normal background */
3698 compatibility(SCOANSI);
3699 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3701 (sco2ansicolour[term->esc_args[0] & 0x7] |
3702 ((term->esc_args[0] & 0x8) << 1)) <<
3704 term->curr_attr &= ~ATTR_BGMASK;
3705 term->curr_attr |= colour;
3706 term->default_attr &= ~ATTR_BGMASK;
3707 term->default_attr |= colour;
3710 case ANSI('L', '='):
3711 compatibility(SCOANSI);
3712 term->use_bce = (term->esc_args[0] <= 0);
3713 set_erase_char(term);
3715 case ANSI('p', '"'): /* DECSCL: set compat level */
3717 * Allow the host to make this emulator a
3718 * 'perfect' VT102. This first appeared in
3719 * the VT220, but we do need to get back to
3720 * PuTTY mode so I won't check it.
3722 * The arg in 40..42,50 are a PuTTY extension.
3723 * The 2nd arg, 8bit vs 7bit is not checked.
3725 * Setting VT102 mode should also change
3726 * the Fkeys to generate PF* codes as a
3727 * real VT102 has no Fkeys. The VT220 does
3728 * this, F11..F13 become ESC,BS,LF other
3729 * Fkeys send nothing.
3731 * Note ESC c will NOT change this!
3734 switch (term->esc_args[0]) {
3736 term->compatibility_level &= ~TM_VTXXX;
3737 term->compatibility_level |= TM_VT102;
3740 term->compatibility_level &= ~TM_VTXXX;
3741 term->compatibility_level |= TM_VT220;
3745 if (term->esc_args[0] > 60 &&
3746 term->esc_args[0] < 70)
3747 term->compatibility_level |= TM_VTXXX;
3751 term->compatibility_level &= TM_VTXXX;
3754 term->compatibility_level = TM_PUTTY;
3757 term->compatibility_level = TM_SCOANSI;
3761 term->compatibility_level = TM_PUTTY;
3767 /* Change the response to CSI c */
3768 if (term->esc_args[0] == 50) {
3771 strcpy(term->id_string, "\033[?");
3772 for (i = 1; i < term->esc_nargs; i++) {
3774 strcat(term->id_string, ";");
3775 sprintf(lbuf, "%d", term->esc_args[i]);
3776 strcat(term->id_string, lbuf);
3778 strcat(term->id_string, "c");
3781 /* Is this a good idea ?
3782 * Well we should do a soft reset at this point ...
3784 if (!has_compat(VT420) && has_compat(VT100)) {
3785 if (!term->cfg.no_remote_resize) {
3786 if (term->reset_132)
3787 request_resize(132, 24);
3789 request_resize(80, 24);
3797 term->osc_w = FALSE;
3799 case 'P': /* Linux palette sequence */
3800 term->termstate = SEEN_OSC_P;
3801 term->osc_strlen = 0;
3803 case 'R': /* Linux palette reset */
3804 palette_reset(term->frontend);
3805 term_invalidate(term);
3806 term->termstate = TOPLEVEL;
3808 case 'W': /* word-set */
3809 term->termstate = SEEN_OSC_W;
3822 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3826 * Grotty hack to support xterm and DECterm title
3827 * sequences concurrently.
3829 if (term->esc_args[0] == 2) {
3830 term->esc_args[0] = 1;
3833 /* else fall through */
3835 term->termstate = OSC_STRING;
3836 term->osc_strlen = 0;
3841 * This OSC stuff is EVIL. It takes just one character to get into
3842 * sysline mode and it's not initially obvious how to get out.
3843 * So I've added CR and LF as string aborts.
3844 * This shouldn't effect compatibility as I believe embedded
3845 * control characters are supposed to be interpreted (maybe?)
3846 * and they don't display anything useful anyway.
3850 if (c == '\012' || c == '\015') {
3851 term->termstate = TOPLEVEL;
3852 } else if (c == 0234 || c == '\007') {
3854 * These characters terminate the string; ST and BEL
3855 * terminate the sequence and trigger instant
3856 * processing of it, whereas ESC goes back to SEEN_ESC
3857 * mode unless it is followed by \, in which case it is
3858 * synonymous with ST in the first place.
3861 term->termstate = TOPLEVEL;
3862 } else if (c == '\033')
3863 term->termstate = OSC_MAYBE_ST;
3864 else if (term->osc_strlen < OSC_STR_MAX)
3865 term->osc_string[term->osc_strlen++] = (char)c;
3869 int max = (term->osc_strlen == 0 ? 21 : 16);
3871 if ((int)c >= '0' && (int)c <= '9')
3873 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
3875 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
3878 term->termstate = TOPLEVEL;
3881 term->osc_string[term->osc_strlen++] = val;
3882 if (term->osc_strlen >= 7) {
3883 palette_set(term->frontend, term->osc_string[0],
3884 term->osc_string[1] * 16 + term->osc_string[2],
3885 term->osc_string[3] * 16 + term->osc_string[4],
3886 term->osc_string[5] * 16 + term->osc_string[6]);
3887 term_invalidate(term);
3888 term->termstate = TOPLEVEL;
3904 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3907 term->termstate = OSC_STRING;
3908 term->osc_strlen = 0;
3912 term->termstate = TOPLEVEL;
3913 term->seen_disp_event = TRUE;
3916 move(term, term->curs.x, term->curs.y - 1, 1);
3919 move(term, term->curs.x, term->curs.y + 1, 1);
3922 move(term, term->curs.x + 1, term->curs.y, 1);
3925 move(term, term->curs.x - 1, term->curs.y, 1);
3928 * From the VT100 Manual
3929 * NOTE: The special graphics characters in the VT100
3930 * are different from those in the VT52
3932 * From VT102 manual:
3933 * 137 _ Blank - Same
3934 * 140 ` Reserved - Humm.
3935 * 141 a Solid rectangle - Similar
3936 * 142 b 1/ - Top half of fraction for the
3937 * 143 c 3/ - subscript numbers below.
3940 * 146 f Degrees - Same
3941 * 147 g Plus or minus - Same
3943 * 151 i Ellipsis (dots)
3946 * 154 l Bar at scan 0
3947 * 155 m Bar at scan 1
3948 * 156 n Bar at scan 2
3949 * 157 o Bar at scan 3 - Similar
3950 * 160 p Bar at scan 4 - Similar
3951 * 161 q Bar at scan 5 - Similar
3952 * 162 r Bar at scan 6 - Same
3953 * 163 s Bar at scan 7 - Similar
3968 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
3971 term->cset_attr[term->cset = 0] = CSET_ASCII;
3974 move(term, 0, 0, 0);
3977 if (term->curs.y == 0)
3978 scroll(term, 0, term->rows - 1, -1, TRUE);
3979 else if (term->curs.y > 0)
3981 term->wrapnext = FALSE;
3984 erase_lots(term, FALSE, FALSE, TRUE);
3988 erase_lots(term, TRUE, FALSE, TRUE);
3992 /* XXX Print cursor line */
3995 /* XXX Start controller mode */
3998 /* XXX Stop controller mode */
4002 term->termstate = VT52_Y1;
4006 ldisc_send(term->ldisc, "\033/Z", 3, 0);
4009 term->app_keypad_keys = TRUE;
4012 term->app_keypad_keys = FALSE;
4015 /* XXX This should switch to VT100 mode not current or default
4016 * VT mode. But this will only have effect in a VT220+
4019 term->vt52_mode = FALSE;
4020 term->blink_is_real = term->cfg.blinktext;
4024 /* XXX Enter auto print mode */
4027 /* XXX Exit auto print mode */
4030 /* XXX Print screen */
4036 /* compatibility(ATARI) */
4037 move(term, 0, 0, 0);
4038 erase_lots(term, FALSE, FALSE, TRUE);
4042 /* compatibility(ATARI) */
4043 if (term->curs.y <= term->marg_b)
4044 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4047 /* compatibility(ATARI) */
4048 if (term->curs.y <= term->marg_b)
4049 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4052 /* compatibility(ATARI) */
4053 term->termstate = VT52_FG;
4056 /* compatibility(ATARI) */
4057 term->termstate = VT52_BG;
4060 /* compatibility(ATARI) */
4061 erase_lots(term, FALSE, TRUE, FALSE);
4065 /* compatibility(ATARI) */
4066 term->cursor_on = TRUE;
4069 /* compatibility(ATARI) */
4070 term->cursor_on = FALSE;
4072 /* case 'j': Save cursor position - broken on ST */
4073 /* case 'k': Restore cursor position */
4075 /* compatibility(ATARI) */
4076 erase_lots(term, TRUE, TRUE, TRUE);
4078 term->wrapnext = FALSE;
4081 /* compatibility(ATARI) */
4082 erase_lots(term, TRUE, TRUE, FALSE);
4085 /* compatibility(ATARI) */
4086 term->curr_attr |= ATTR_REVERSE;
4089 /* compatibility(ATARI) */
4090 term->curr_attr &= ~ATTR_REVERSE;
4092 case 'v': /* wrap Autowrap on - Wyse style */
4093 /* compatibility(ATARI) */
4096 case 'w': /* Autowrap off */
4097 /* compatibility(ATARI) */
4102 /* compatibility(OTHER) */
4103 term->vt52_bold = FALSE;
4104 term->curr_attr = ATTR_DEFAULT;
4105 set_erase_char(term);
4108 /* compatibility(VI50) */
4109 term->curr_attr |= ATTR_UNDER;
4112 /* compatibility(VI50) */
4113 term->curr_attr &= ~ATTR_UNDER;
4116 /* compatibility(VI50) */
4117 term->vt52_bold = TRUE;
4118 term->curr_attr |= ATTR_BOLD;
4121 /* compatibility(VI50) */
4122 term->vt52_bold = FALSE;
4123 term->curr_attr &= ~ATTR_BOLD;
4129 term->termstate = VT52_Y2;
4130 move(term, term->curs.x, c - ' ', 0);
4133 term->termstate = TOPLEVEL;
4134 move(term, c - ' ', term->curs.y, 0);
4139 term->termstate = TOPLEVEL;
4140 term->curr_attr &= ~ATTR_FGMASK;
4141 term->curr_attr &= ~ATTR_BOLD;
4142 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
4143 if ((c & 0x8) || term->vt52_bold)
4144 term->curr_attr |= ATTR_BOLD;
4146 set_erase_char(term);
4149 term->termstate = TOPLEVEL;
4150 term->curr_attr &= ~ATTR_BGMASK;
4151 term->curr_attr &= ~ATTR_BLINK;
4152 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
4154 /* Note: bold background */
4156 term->curr_attr |= ATTR_BLINK;
4158 set_erase_char(term);
4161 default: break; /* placate gcc warning about enum use */
4163 if (term->selstate != NO_SELECTION) {
4164 pos cursplus = term->curs;
4166 check_selection(term, term->curs, cursplus);
4170 term_print_flush(term);
4171 logflush(term->logctx);
4175 * To prevent having to run the reasonably tricky bidi algorithm
4176 * too many times, we maintain a cache of the last lineful of data
4177 * fed to the algorithm on each line of the display.
4179 static int term_bidi_cache_hit(Terminal *term, int line,
4180 termchar *lbefore, int width)
4184 if (!term->pre_bidi_cache)
4185 return FALSE; /* cache doesn't even exist yet! */
4187 if (line >= term->bidi_cache_size)
4188 return FALSE; /* cache doesn't have this many lines */
4190 if (!term->pre_bidi_cache[line].chars)
4191 return FALSE; /* cache doesn't contain _this_ line */
4193 if (term->pre_bidi_cache[line].width != width)
4194 return FALSE; /* line is wrong width */
4196 for (i = 0; i < width; i++)
4197 if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i))
4198 return FALSE; /* line doesn't match cache */
4200 return TRUE; /* it didn't match. */
4203 static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
4204 termchar *lafter, int width)
4206 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
4207 int j = term->bidi_cache_size;
4208 term->bidi_cache_size = line+1;
4209 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
4210 term->bidi_cache_size,
4211 struct bidi_cache_entry);
4212 term->post_bidi_cache = sresize(term->post_bidi_cache,
4213 term->bidi_cache_size,
4214 struct bidi_cache_entry);
4215 while (j < term->bidi_cache_size) {
4216 term->pre_bidi_cache[j].chars =
4217 term->post_bidi_cache[j].chars = NULL;
4218 term->pre_bidi_cache[j].width =
4219 term->post_bidi_cache[j].width = -1;
4224 sfree(term->pre_bidi_cache[line].chars);
4225 sfree(term->post_bidi_cache[line].chars);
4227 term->pre_bidi_cache[line].width = width;
4228 term->pre_bidi_cache[line].chars = snewn(width, termchar);
4229 term->post_bidi_cache[line].width = width;
4230 term->post_bidi_cache[line].chars = snewn(width, termchar);
4232 memcpy(term->pre_bidi_cache[line].chars, lbefore, width * TSIZE);
4233 memcpy(term->post_bidi_cache[line].chars, lafter, width * TSIZE);
4237 * Given a context, update the window. Out of paranoia, we don't
4238 * allow WM_PAINT responses to do scrolling optimisations.
4240 static void do_paint(Terminal *term, Context ctx, int may_optimise)
4242 int i, it, j, our_curs_y, our_curs_x;
4247 termchar cursor_background;
4248 unsigned long ticks;
4249 #ifdef OPTIMISE_SCROLL
4250 struct scrollregion *sr;
4251 #endif /* OPTIMISE_SCROLL */
4253 cursor_background = term->basic_erase_char;
4256 ch = snewn(chlen, wchar_t);
4259 * Check the visual bell state.
4261 if (term->in_vbell) {
4262 ticks = GETTICKCOUNT();
4263 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
4264 term->in_vbell = FALSE;
4267 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4270 * screen array, disptop, scrtop,
4272 * cfg.blinkpc, blink_is_real, tblinker,
4273 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4276 /* Has the cursor position or type changed ? */
4277 if (term->cursor_on) {
4278 if (term->has_focus) {
4279 if (term->blinker || !term->cfg.blink_cur)
4280 cursor = TATTR_ACTCURS;
4284 cursor = TATTR_PASCURS;
4286 cursor |= TATTR_RIGHTCURS;
4289 our_curs_y = term->curs.y - term->disptop;
4292 * Adjust the cursor position in the case where it's
4293 * resting on the right-hand half of a CJK wide character.
4294 * xterm's behaviour here, which seems adequate to me, is
4295 * to display the cursor covering the _whole_ character,
4296 * exactly as if it were one space to the left.
4298 termline *ldata = lineptr(term->curs.y);
4299 our_curs_x = term->curs.x;
4300 if (our_curs_x > 0 &&
4301 ldata->chars[our_curs_x].chr == UCSWIDE)
4307 * If the cursor is not where it was last time we painted, and
4308 * its previous position is visible on screen, invalidate its
4309 * previous position.
4311 if (term->dispcursy >= 0 &&
4312 (term->curstype != cursor ||
4313 term->dispcursy != our_curs_y ||
4314 term->dispcursx != our_curs_x)) {
4315 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
4318 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
4319 dispcurs[-1].attr |= ATTR_INVALID;
4320 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
4321 dispcurs[1].attr |= ATTR_INVALID;
4322 dispcurs->attr |= ATTR_INVALID;
4326 term->dispcursx = term->dispcursy = -1;
4328 #ifdef OPTIMISE_SCROLL
4330 sr = term->scrollhead;
4332 struct scrollregion *next = sr->next;
4333 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
4337 term->scrollhead = term->scrolltail = NULL;
4338 #endif /* OPTIMISE_SCROLL */
4340 /* The normal screen data */
4341 for (i = 0; i < term->rows; i++) {
4344 int dirty_line, dirty_run, selected;
4345 unsigned long attr = 0, cset = 0;
4346 int updated_line = 0;
4349 int last_run_dirty = 0;
4351 scrpos.y = i + term->disptop;
4352 ldata = lineptr(scrpos.y);
4354 dirty_run = dirty_line = (ldata->lattr !=
4355 term->disptext[i]->lattr);
4356 term->disptext[i]->lattr = ldata->lattr;
4358 /* Do Arabic shaping and bidi. */
4359 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4361 if (!term_bidi_cache_hit(term, i, ldata->chars, term->cols)) {
4363 if (term->wcFromTo_size < term->cols) {
4364 term->wcFromTo_size = term->cols;
4365 term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,
4367 term->wcTo = sresize(term->wcTo, term->wcFromTo_size,
4371 for(it=0; it<term->cols ; it++)
4373 unsigned long uc = (ldata->chars[it].chr);
4375 switch (uc & CSET_MASK) {
4377 if (!term->cfg.rawcnp) {
4378 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4382 uc = term->ucsdata->unitab_line[uc & 0xFF];
4385 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4388 switch (uc & CSET_MASK) {
4390 uc = term->ucsdata->unitab_font[uc & 0xFF];
4393 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4397 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4399 term->wcFrom[it].index = it;
4403 do_bidi(term->wcFrom, term->cols);
4405 /* this is saved iff done from inside the shaping */
4406 if(!term->cfg.bidi && term->cfg.arabicshaping)
4407 for(it=0; it<term->cols; it++)
4408 term->wcTo[it] = term->wcFrom[it];
4410 if(!term->cfg.arabicshaping)
4411 do_shape(term->wcFrom, term->wcTo, term->cols);
4413 if (term->ltemp_size < ldata->size) {
4414 term->ltemp_size = ldata->size;
4415 term->ltemp = sresize(term->ltemp, term->ltemp_size,
4419 memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);
4421 for(it=0; it<term->cols ; it++)
4423 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4424 if (term->ltemp[it].cc_next)
4425 term->ltemp[it].cc_next -=
4426 it - term->wcTo[it].index;
4428 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4429 term->ltemp[it].chr = term->wcTo[it].wc;
4431 term_bidi_cache_store(term, i, ldata->chars,
4432 term->ltemp, ldata->size);
4434 lchars = term->ltemp;
4436 lchars = term->post_bidi_cache[i].chars;
4439 lchars = ldata->chars;
4441 for (j = 0; j < term->cols; j++) {
4442 unsigned long tattr, tchar;
4443 termchar *d = lchars + j;
4444 int break_run, do_copy;
4450 if (!term->cfg.ansi_colour)
4451 tattr = (tattr & ~(ATTR_FGMASK | ATTR_BGMASK)) |
4452 ATTR_DEFFG | ATTR_DEFBG;
4454 switch (tchar & CSET_MASK) {
4456 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4459 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4462 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
4465 if (j < term->cols-1 && d[1].chr == UCSWIDE)
4468 /* Video reversing things */
4469 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4470 if (term->seltype == LEXICOGRAPHIC)
4471 selected = (posle(term->selstart, scrpos) &&
4472 poslt(scrpos, term->selend));
4474 selected = (posPle(term->selstart, scrpos) &&
4475 posPlt(scrpos, term->selend));
4479 ^ (selected ? ATTR_REVERSE : 0));
4481 /* 'Real' blinking ? */
4482 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4483 if (term->has_focus && term->tblinker) {
4484 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
4486 tattr &= ~ATTR_BLINK;
4490 * Check the font we'll _probably_ be using to see if
4491 * the character is wide when we don't want it to be.
4493 if (tchar != term->disptext[i]->chars[j].chr ||
4494 tattr != (term->disptext[i]->chars[j].attr &~
4496 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
4497 tattr |= ATTR_NARROW;
4498 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
4499 tattr |= ATTR_NARROW;
4501 /* Cursor here ? Save the 'background' */
4502 if (i == our_curs_y && j == our_curs_x) {
4504 cursor_background.chr = tchar;
4505 cursor_background.attr = tattr;
4506 /* For once, this cc_next field is an absolute index in lchars */
4508 cursor_background.cc_next = d->cc_next + j;
4510 cursor_background.cc_next = 0;
4511 term->dispcursx = j;
4512 term->dispcursy = i;
4515 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4518 break_run = ((tattr ^ attr) & term->attr_mask) != 0;
4520 /* Special hack for VT100 Linedraw glyphs */
4521 if (tchar >= 0x23BA && tchar <= 0x23BD)
4525 * Separate out sequences of characters that have the
4526 * same CSET, if that CSET is a magic one.
4528 if (CSET_OF(tchar) != cset)
4532 * Break on both sides of any combined-character cell.
4534 if (d->cc_next != 0 ||
4535 (j > 0 && d[-1].cc_next != 0))
4538 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
4539 if (term->disptext[i]->chars[j].chr == tchar &&
4540 term->disptext[i]->chars[j].attr == tattr)
4542 else if (!dirty_run && ccount == 1)
4547 if ((dirty_run || last_run_dirty) && ccount > 0) {
4548 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4554 cset = CSET_OF(tchar);
4555 if (term->ucsdata->dbcs_screenfont)
4556 last_run_dirty = dirty_run;
4557 dirty_run = dirty_line;
4561 if (!termchars_equal_override(&term->disptext[i]->chars[j],
4567 if (ccount >= chlen) {
4568 chlen = ccount + 256;
4569 ch = sresize(ch, chlen, wchar_t);
4571 ch[ccount++] = (wchar_t) tchar;
4576 while (dd->cc_next) {
4577 unsigned long schar;
4582 switch (schar & CSET_MASK) {
4584 schar = term->ucsdata->unitab_line[schar & 0xFF];
4587 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4590 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4594 if (ccount >= chlen) {
4595 chlen = ccount + 256;
4596 ch = sresize(ch, chlen, wchar_t);
4598 ch[ccount++] = (wchar_t) schar;
4601 attr |= TATTR_COMBINING;
4605 copy_termchar(term->disptext[i], j, d);
4606 term->disptext[i]->chars[j].chr = tchar;
4607 term->disptext[i]->chars[j].attr = tattr;
4610 /* If it's a wide char step along to the next one. */
4611 if (tattr & ATTR_WIDE) {
4612 if (++j < term->cols) {
4615 * By construction above, the cursor should not
4616 * be on the right-hand half of this character.
4619 assert(!(i == our_curs_y && j == our_curs_x));
4620 if (!termchars_equal(&term->disptext[i]->chars[j], d))
4622 copy_termchar(term->disptext[i], j, d);
4626 if (dirty_run && ccount > 0) {
4627 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4631 /* Cursor on this line ? (and changed) */
4632 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
4633 ch[0] = (wchar_t) cursor_background.chr;
4634 attr = cursor_background.attr | cursor;
4637 if (cursor_background.cc_next) {
4638 termchar *dd = ldata->chars + cursor_background.cc_next;
4641 unsigned long schar;
4644 switch (schar & CSET_MASK) {
4646 schar = term->ucsdata->unitab_line[schar & 0xFF];
4649 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4652 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4656 if (ccount >= chlen) {
4657 chlen = ccount + 256;
4658 ch = sresize(ch, chlen, wchar_t);
4660 ch[ccount++] = (wchar_t) schar;
4668 attr |= TATTR_COMBINING;
4671 do_cursor(ctx, our_curs_x, i, ch, ccount, attr, ldata->lattr);
4672 term->curstype = cursor;
4682 * Flick the switch that says if blinking things should be shown or hidden.
4685 void term_blink(Terminal *term, int flg)
4687 long now, blink_diff;
4689 now = GETTICKCOUNT();
4690 blink_diff = now - term->last_tblink;
4692 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4693 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
4694 term->last_tblink = now;
4695 term->tblinker = !term->tblinker;
4700 term->last_blink = now;
4704 blink_diff = now - term->last_blink;
4706 /* Make sure the cursor blinks no faster than system blink rate */
4707 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
4710 term->last_blink = now;
4711 term->blinker = !term->blinker;
4715 * Invalidate the whole screen so it will be repainted in full.
4717 void term_invalidate(Terminal *term)
4721 for (i = 0; i < term->rows; i++)
4722 for (j = 0; j < term->cols; j++)
4723 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4727 * Paint the window in response to a WM_PAINT message.
4729 void term_paint(Terminal *term, Context ctx,
4730 int left, int top, int right, int bottom, int immediately)
4733 if (left < 0) left = 0;
4734 if (top < 0) top = 0;
4735 if (right >= term->cols) right = term->cols-1;
4736 if (bottom >= term->rows) bottom = term->rows-1;
4738 for (i = top; i <= bottom && i < term->rows; i++) {
4739 if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
4740 for (j = left; j <= right && j < term->cols; j++)
4741 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4743 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
4744 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4747 /* This should happen soon enough, also for some reason it sometimes
4748 * fails to actually do anything when re-sizing ... painting the wrong
4752 do_paint (term, ctx, FALSE);
4756 * Attempt to scroll the scrollback. The second parameter gives the
4757 * position we want to scroll to; the first is +1 to denote that
4758 * this position is relative to the beginning of the scrollback, -1
4759 * to denote it is relative to the end, and 0 to denote that it is
4760 * relative to the current position.
4762 void term_scroll(Terminal *term, int rel, int where)
4764 int sbtop = -sblines(term);
4765 #ifdef OPTIMISE_SCROLL
4766 int olddisptop = term->disptop;
4768 #endif /* OPTIMISE_SCROLL */
4770 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4771 if (term->disptop < sbtop)
4772 term->disptop = sbtop;
4773 if (term->disptop > 0)
4776 #ifdef OPTIMISE_SCROLL
4777 shift = (term->disptop - olddisptop);
4778 if (shift < term->rows && shift > -term->rows)
4779 scroll_display(term, 0, term->rows - 1, shift);
4780 #endif /* OPTIMISE_SCROLL */
4784 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
4787 wchar_t *wbptr; /* where next char goes within workbuf */
4789 int wblen = 0; /* workbuf len */
4790 int buflen; /* amount of memory allocated to workbuf */
4792 buflen = 5120; /* Default size */
4793 workbuf = snewn(buflen, wchar_t);
4794 wbptr = workbuf; /* start filling here */
4795 old_top_x = top.x; /* needed for rect==1 */
4797 while (poslt(top, bottom)) {
4799 termline *ldata = lineptr(top.y);
4803 * nlpos will point at the maximum position on this line we
4804 * should copy up to. So we start it at the end of the
4808 nlpos.x = term->cols;
4811 * ... move it backwards if there's unused space at the end
4812 * of the line (and also set `nl' if this is the case,
4813 * because in normal selection mode this means we need a
4814 * newline at the end)...
4816 if (!(ldata->lattr & LATTR_WRAPPED)) {
4817 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
4818 !ldata->chars[nlpos.x - 1].cc_next &&
4821 if (poslt(nlpos, bottom))
4823 } else if (ldata->lattr & LATTR_WRAPPED2) {
4824 /* Ignore the last char on the line in a WRAPPED2 line. */
4829 * ... and then clip it to the terminal x coordinate if
4830 * we're doing rectangular selection. (In this case we
4831 * still did the above, so that copying e.g. the right-hand
4832 * column from a table doesn't fill with spaces on the
4836 if (nlpos.x > bottom.x)
4838 nl = (top.y < bottom.y);
4841 while (poslt(top, bottom) && poslt(top, nlpos)) {
4844 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4846 wchar_t cbuf[16], *p;
4850 if (ldata->chars[x].chr == UCSWIDE) {
4856 int uc = ldata->chars[x].chr;
4858 switch (uc & CSET_MASK) {
4860 if (!term->cfg.rawcnp) {
4861 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4865 uc = term->ucsdata->unitab_line[uc & 0xFF];
4868 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4871 switch (uc & CSET_MASK) {
4873 uc = term->ucsdata->unitab_font[uc & 0xFF];
4876 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4880 set = (uc & CSET_MASK);
4881 c = (uc & ~CSET_MASK);
4885 if (DIRECT_FONT(uc)) {
4886 if (c >= ' ' && c != 0x7F) {
4890 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4892 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
4893 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4897 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4901 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
4908 for (p = cbuf; *p; p++) {
4909 /* Enough overhead for trailing NL and nul */
4910 if (wblen >= buflen - 16) {
4912 workbuf = sresize(workbuf, buflen, wchar_t);
4913 wbptr = workbuf + wblen;
4919 if (ldata->chars[x].cc_next)
4920 x += ldata->chars[x].cc_next;
4928 for (i = 0; i < sel_nl_sz; i++) {
4930 *wbptr++ = sel_nl[i];
4934 top.x = rect ? old_top_x : 0;
4938 #if SELECTION_NUL_TERMINATED
4942 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
4943 if (buflen > 0) /* indicates we allocated this buffer */
4947 void term_copyall(Terminal *term)
4951 tree234 *screen = term->screen;
4952 top.y = -sblines(term);
4954 bottom.y = find_last_nonempty_line(term, screen);
4955 bottom.x = term->cols;
4956 clipme(term, top, bottom, 0, TRUE);
4960 * The wordness array is mainly for deciding the disposition of the
4961 * US-ASCII characters.
4963 static int wordtype(Terminal *term, int uc)
4966 int start, end, ctype;
4968 static const struct ucsword ucs_words[] = {
4974 0x037e, 0x037e, 1}, /* Greek question mark */
4976 0x0387, 0x0387, 1}, /* Greek ano teleia */
4978 0x055a, 0x055f, 1}, /* Armenian punctuation */
4980 0x0589, 0x0589, 1}, /* Armenian full stop */
4982 0x0700, 0x070d, 1}, /* Syriac punctuation */
4984 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4986 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4988 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4990 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4992 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4994 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4996 0x2000, 0x200a, 0}, /* Various spaces */
4998 0x2070, 0x207f, 2}, /* superscript */
5000 0x2080, 0x208f, 2}, /* subscript */
5002 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5004 0x3000, 0x3000, 0}, /* ideographic space */
5006 0x3001, 0x3020, 1}, /* ideographic punctuation */
5008 0x303f, 0x309f, 3}, /* Hiragana */
5010 0x30a0, 0x30ff, 3}, /* Katakana */
5012 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5014 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5016 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5018 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5020 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5022 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5024 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5026 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5028 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5032 const struct ucsword *wptr;
5034 switch (uc & CSET_MASK) {
5036 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
5039 uc = term->ucsdata->unitab_line[uc & 0xFF];
5042 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
5045 switch (uc & CSET_MASK) {
5047 uc = term->ucsdata->unitab_font[uc & 0xFF];
5050 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
5054 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5055 * fail as there's such a thing as a double width space. :-(
5057 if (term->ucsdata->dbcs_screenfont &&
5058 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5062 return term->wordness[uc];
5064 for (wptr = ucs_words; wptr->start; wptr++) {
5065 if (uc >= wptr->start && uc <= wptr->end)
5073 * Spread the selection outwards according to the selection mode.
5075 static pos sel_spread_half(Terminal *term, pos p, int dir)
5079 int topy = -sblines(term);
5081 ldata = lineptr(p.y);
5083 switch (term->selmode) {
5086 * In this mode, every character is a separate unit, except
5087 * for runs of spaces at the end of a non-wrapping line.
5089 if (!(ldata->lattr & LATTR_WRAPPED)) {
5090 termchar *q = ldata->chars + term->cols;
5091 while (q > ldata->chars &&
5092 IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next)
5094 if (q == ldata->chars + term->cols)
5096 if (p.x >= q - ldata->chars)
5097 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
5102 * In this mode, the units are maximal runs of characters
5103 * whose `wordness' has the same value.
5105 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
5108 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
5109 term->cols-1 : term->cols);
5110 if (p.x < maxcols-1) {
5111 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
5116 if (ldata->lattr & LATTR_WRAPPED) {
5118 ldata2 = lineptr(p.y+1);
5119 if (wordtype(term, UCSGET(ldata2->chars, 0))
5136 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
5145 ldata2 = lineptr(p.y-1);
5146 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
5147 term->cols-1 : term->cols);
5148 if (ldata2->lattr & LATTR_WRAPPED) {
5149 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
5167 * In this mode, every line is a unit.
5169 p.x = (dir == -1 ? 0 : term->cols - 1);
5177 static void sel_spread(Terminal *term)
5179 if (term->seltype == LEXICOGRAPHIC) {
5180 term->selstart = sel_spread_half(term, term->selstart, -1);
5181 decpos(term->selend);
5182 term->selend = sel_spread_half(term, term->selend, +1);
5183 incpos(term->selend);
5187 void term_do_paste(Terminal *term)
5192 get_clip(term->frontend, &data, &len);
5193 if (data && len > 0) {
5196 term_seen_key_event(term); /* pasted data counts */
5198 if (term->paste_buffer)
5199 sfree(term->paste_buffer);
5200 term->paste_pos = term->paste_hold = term->paste_len = 0;
5201 term->paste_buffer = snewn(len, wchar_t);
5204 while (p < data + len) {
5205 while (p < data + len &&
5206 !(p <= data + len - sel_nl_sz &&
5207 !memcmp(p, sel_nl, sizeof(sel_nl))))
5212 for (i = 0; i < p - q; i++) {
5213 term->paste_buffer[term->paste_len++] = q[i];
5217 if (p <= data + len - sel_nl_sz &&
5218 !memcmp(p, sel_nl, sizeof(sel_nl))) {
5219 term->paste_buffer[term->paste_len++] = '\015';
5225 /* Assume a small paste will be OK in one go. */
5226 if (term->paste_len < 256) {
5228 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
5229 if (term->paste_buffer)
5230 sfree(term->paste_buffer);
5231 term->paste_buffer = 0;
5232 term->paste_pos = term->paste_hold = term->paste_len = 0;
5235 get_clip(term->frontend, NULL, NULL);
5238 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
5239 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
5243 int raw_mouse = (term->xterm_mouse &&
5244 !term->cfg.no_mouse_rep &&
5245 !(term->cfg.mouse_override && shift));
5246 int default_seltype;
5250 if (a == MA_DRAG && !raw_mouse)
5251 term_scroll(term, 0, -1);
5253 if (y >= term->rows) {
5255 if (a == MA_DRAG && !raw_mouse)
5256 term_scroll(term, 0, +1);
5265 if (x >= term->cols)
5268 selpoint.y = y + term->disptop;
5270 ldata = lineptr(selpoint.y);
5271 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
5276 int encstate = 0, r, c;
5283 encstate = 0x20; /* left button down */
5294 case MBT_WHEEL_DOWN:
5297 default: break; /* placate gcc warning about enum use */
5301 if (term->xterm_mouse == 1)
5307 term->mouse_is_down = 0;
5310 if (term->mouse_is_down == braw)
5312 term->mouse_is_down = braw;
5314 default: break; /* placate gcc warning about enum use */
5323 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
5324 ldisc_send(term->ldisc, abuf, 6, 0);
5330 * Set the selection type (rectangular or normal) at the start
5331 * of a selection attempt, from the state of Alt.
5333 if (!alt ^ !term->cfg.rect_select)
5334 default_seltype = RECTANGULAR;
5336 default_seltype = LEXICOGRAPHIC;
5338 if (term->selstate == NO_SELECTION) {
5339 term->seltype = default_seltype;
5342 if (bcooked == MBT_SELECT && a == MA_CLICK) {
5344 term->selstate = ABOUT_TO;
5345 term->seltype = default_seltype;
5346 term->selanchor = selpoint;
5347 term->selmode = SM_CHAR;
5348 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
5350 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
5351 term->selstate = DRAGGING;
5352 term->selstart = term->selanchor = selpoint;
5353 term->selend = term->selstart;
5354 incpos(term->selend);
5356 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
5357 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
5358 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
5360 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
5361 term->selstate == SELECTED) {
5362 if (term->seltype == LEXICOGRAPHIC) {
5364 * For normal selection, we extend by moving
5365 * whichever end of the current selection is closer
5368 if (posdiff(selpoint, term->selstart) <
5369 posdiff(term->selend, term->selstart) / 2) {
5370 term->selanchor = term->selend;
5371 decpos(term->selanchor);
5373 term->selanchor = term->selstart;
5377 * For rectangular selection, we have a choice of
5378 * _four_ places to put selanchor and selpoint: the
5379 * four corners of the selection.
5381 if (2*selpoint.x < term->selstart.x + term->selend.x)
5382 term->selanchor.x = term->selend.x-1;
5384 term->selanchor.x = term->selstart.x;
5386 if (2*selpoint.y < term->selstart.y + term->selend.y)
5387 term->selanchor.y = term->selend.y;
5389 term->selanchor.y = term->selstart.y;
5391 term->selstate = DRAGGING;
5393 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
5394 term->selanchor = selpoint;
5395 term->selstate = DRAGGING;
5396 if (term->seltype == LEXICOGRAPHIC) {
5398 * For normal selection, we set (selstart,selend) to
5399 * (selpoint,selanchor) in some order.
5401 if (poslt(selpoint, term->selanchor)) {
5402 term->selstart = selpoint;
5403 term->selend = term->selanchor;
5404 incpos(term->selend);
5406 term->selstart = term->selanchor;
5407 term->selend = selpoint;
5408 incpos(term->selend);
5412 * For rectangular selection, we may need to
5413 * interchange x and y coordinates (if the user has
5414 * dragged in the -x and +y directions, or vice versa).
5416 term->selstart.x = min(term->selanchor.x, selpoint.x);
5417 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
5418 term->selstart.y = min(term->selanchor.y, selpoint.y);
5419 term->selend.y = max(term->selanchor.y, selpoint.y);
5422 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
5424 if (term->selstate == DRAGGING) {
5426 * We've completed a selection. We now transfer the
5427 * data to the clipboard.
5429 clipme(term, term->selstart, term->selend,
5430 (term->seltype == RECTANGULAR), FALSE);
5431 term->selstate = SELECTED;
5433 term->selstate = NO_SELECTION;
5434 } else if (bcooked == MBT_PASTE
5436 #if MULTICLICK_ONLY_EVENT
5437 || a == MA_2CLK || a == MA_3CLK
5440 request_paste(term->frontend);
5446 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
5447 unsigned int modifiers, unsigned int flags)
5451 int prependesc = FALSE;
5455 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
5456 for (i = 0; i < tlen; i++)
5457 fprintf(stderr, " %04x", (unsigned)text[i]);
5458 fprintf(stderr, "\n");
5462 if ((flags & PKF_REPEAT) && term->repeat_off)
5465 /* Currently, Meta always just prefixes everything with ESC. */
5466 if (modifiers & PKM_META)
5468 modifiers &= ~PKM_META;
5471 * Alt is only used for Alt+keypad, which isn't supported yet, so
5474 modifiers &= ~PKM_ALT;
5476 /* Standard local function keys */
5477 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
5479 if (keysym == PK_PAGEUP)
5480 /* scroll up one page */;
5481 if (keysym == PK_PAGEDOWN)
5482 /* scroll down on page */;
5483 if (keysym == PK_INSERT)
5484 term_do_paste(term);
5487 if (keysym == PK_PAGEUP)
5488 /* scroll up one line */;
5489 if (keysym == PK_PAGEDOWN)
5490 /* scroll down one line */;
5491 /* Control-Numlock for app-keypad mode switch */
5492 if (keysym == PK_PF1)
5493 term->app_keypad_keys ^= 1;
5497 if (modifiers & PKM_ALT) {
5498 /* Alt+F4 (close) */
5499 /* Alt+Return (full screen) */
5500 /* Alt+Space (system menu) */
5503 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5504 text[0] >= 0x20 && text[0] <= 0x7e) {
5505 /* ASCII chars + Control */
5506 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5507 (text[0] >= 0x61 && text[0] <= 0x7a))
5511 * Control-2 should return ^@ (0x00), Control-6 should return
5512 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5513 * the DOS keyboard handling did it, and we have nothing better
5514 * to do with the key combo in question, we'll also map
5515 * Control-Backquote to ^\ (0x1C).
5518 case ' ': text[0] = 0x00; break;
5519 case '-': text[0] = 0x1f; break;
5520 case '/': text[0] = 0x1f; break;
5521 case '2': text[0] = 0x00; break;
5522 case '3': text[0] = 0x1b; break;
5523 case '4': text[0] = 0x1c; break;
5524 case '5': text[0] = 0x1d; break;
5525 case '6': text[0] = 0x1e; break;
5526 case '7': text[0] = 0x1f; break;
5527 case '8': text[0] = 0x7f; break;
5528 case '`': text[0] = 0x1c; break;
5533 /* Nethack keypad */
5534 if (term->cfg.nethack_keypad) {
5537 case PK_KP1: c = 'b'; break;
5538 case PK_KP2: c = 'j'; break;
5539 case PK_KP3: c = 'n'; break;
5540 case PK_KP4: c = 'h'; break;
5541 case PK_KP5: c = '.'; break;
5542 case PK_KP6: c = 'l'; break;
5543 case PK_KP7: c = 'y'; break;
5544 case PK_KP8: c = 'k'; break;
5545 case PK_KP9: c = 'u'; break;
5546 default: break; /* else gcc warns `enum value not used' */
5550 if (modifiers & PKM_CONTROL)
5552 else if (modifiers & PKM_SHIFT)
5560 /* Numeric Keypad */
5561 if (PK_ISKEYPAD(keysym)) {
5565 * In VT400 mode, PFn always emits an escape sequence. In
5566 * Linux and tilde modes, this only happens in app keypad mode.
5568 if (term->cfg.funky_type == FUNKY_VT400 ||
5569 ((term->cfg.funky_type == FUNKY_LINUX ||
5570 term->cfg.funky_type == FUNKY_TILDE) &&
5571 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5573 case PK_PF1: xkey = 'P'; break;
5574 case PK_PF2: xkey = 'Q'; break;
5575 case PK_PF3: xkey = 'R'; break;
5576 case PK_PF4: xkey = 'S'; break;
5577 default: break; /* else gcc warns `enum value not used' */
5580 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5582 case PK_KP0: xkey = 'p'; break;
5583 case PK_KP1: xkey = 'q'; break;
5584 case PK_KP2: xkey = 'r'; break;
5585 case PK_KP3: xkey = 's'; break;
5586 case PK_KP4: xkey = 't'; break;
5587 case PK_KP5: xkey = 'u'; break;
5588 case PK_KP6: xkey = 'v'; break;
5589 case PK_KP7: xkey = 'w'; break;
5590 case PK_KP8: xkey = 'x'; break;
5591 case PK_KP9: xkey = 'y'; break;
5592 case PK_KPDECIMAL: xkey = 'n'; break;
5593 case PK_KPENTER: xkey = 'M'; break;
5594 default: break; /* else gcc warns `enum value not used' */
5596 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5598 * xterm can't see the layout of the keypad, so it has
5599 * to rely on the X keysyms returned by the keys.
5600 * Hence, we look at the strings here, not the PuTTY
5601 * keysyms (which describe the layout).
5605 if (modifiers & PKM_SHIFT)
5610 case '/': xkey = 'o'; break;
5611 case '*': xkey = 'j'; break;
5612 case '-': xkey = 'm'; break;
5616 * In all other modes, we try to retain the layout of
5617 * the DEC keypad in application mode.
5621 /* This key covers the '-' and ',' keys on a VT220 */
5622 if (modifiers & PKM_SHIFT)
5623 xkey = 'm'; /* VT220 '-' */
5625 xkey = 'l'; /* VT220 ',' */
5627 case PK_KPMINUS: xkey = 'm'; break;
5628 case PK_KPCOMMA: xkey = 'l'; break;
5629 default: break; /* else gcc warns `enum value not used' */
5634 if (term->vt52_mode) {
5635 if (xkey >= 'P' && xkey <= 'S')
5636 p += sprintf((char *) p, "\x1B%c", xkey);
5638 p += sprintf((char *) p, "\x1B?%c", xkey);
5640 p += sprintf((char *) p, "\x1BO%c", xkey);
5643 /* Not in application mode -- treat the number pad as arrow keys? */
5644 if ((flags & PKF_NUMLOCK) == 0) {
5646 case PK_KP0: keysym = PK_INSERT; break;
5647 case PK_KP1: keysym = PK_END; break;
5648 case PK_KP2: keysym = PK_DOWN; break;
5649 case PK_KP3: keysym = PK_PAGEDOWN; break;
5650 case PK_KP4: keysym = PK_LEFT; break;
5651 case PK_KP5: keysym = PK_REST; break;
5652 case PK_KP6: keysym = PK_RIGHT; break;
5653 case PK_KP7: keysym = PK_HOME; break;
5654 case PK_KP8: keysym = PK_UP; break;
5655 case PK_KP9: keysym = PK_PAGEUP; break;
5656 default: break; /* else gcc warns `enum value not used' */
5661 /* Miscellaneous keys */
5668 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5669 else if (modifiers == PKM_SHIFT)
5670 /* We do the opposite of what is configured */
5671 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5677 else if (modifiers == PKM_SHIFT)
5678 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5681 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5683 if (modifiers == PKM_CONTROL)
5688 case PK_KPENTER: /* Odd keypad modes handled above */
5689 if (modifiers == 0) {
5691 if (term->cr_lf_return)
5695 default: break; /* else gcc warns `enum value not used' */
5698 /* SCO function keys and editing keys */
5699 if (term->cfg.funky_type == FUNKY_SCO) {
5700 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5701 static char const codes[] =
5702 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5703 int index = keysym - PK_F1;
5705 if (modifiers & PKM_SHIFT) index += 12;
5706 if (modifiers & PKM_CONTROL) index += 24;
5707 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5710 if (PK_ISEDITING(keysym)) {
5714 case PK_DELETE: *p++ = 0x7f; goto done;
5715 case PK_HOME: xkey = 'H'; break;
5716 case PK_INSERT: xkey = 'L'; break;
5717 case PK_END: xkey = 'F'; break;
5718 case PK_PAGEUP: xkey = 'I'; break;
5719 case PK_PAGEDOWN: xkey = 'G'; break;
5720 default: break; /* else gcc warns `enum value not used' */
5722 p += sprintf((char *) p, "\x1B[%c", xkey);
5726 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5729 if (term->cfg.funky_type == FUNKY_XTERM) {
5730 /* Xterm shuffles these keys, apparently. */
5732 case PK_HOME: keysym = PK_INSERT; break;
5733 case PK_INSERT: keysym = PK_HOME; break;
5734 case PK_DELETE: keysym = PK_END; break;
5735 case PK_END: keysym = PK_PAGEUP; break;
5736 case PK_PAGEUP: keysym = PK_DELETE; break;
5737 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
5738 default: break; /* else gcc warns `enum value not used' */
5743 if (term->cfg.rxvt_homeend &&
5744 (keysym == PK_HOME || keysym == PK_END)) {
5745 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5749 if (term->vt52_mode) {
5753 * A real VT52 doesn't have these, and a VT220 doesn't
5754 * send anything for them in VT52 mode.
5757 case PK_HOME: xkey = 'H'; break;
5758 case PK_INSERT: xkey = 'L'; break;
5759 case PK_DELETE: xkey = 'M'; break;
5760 case PK_END: xkey = 'E'; break;
5761 case PK_PAGEUP: xkey = 'I'; break;
5762 case PK_PAGEDOWN: xkey = 'G'; break;
5763 default: xkey=0; break; /* else gcc warns `enum value not used'*/
5765 p += sprintf((char *) p, "\x1B%c", xkey);
5770 case PK_HOME: code = 1; break;
5771 case PK_INSERT: code = 2; break;
5772 case PK_DELETE: code = 3; break;
5773 case PK_END: code = 4; break;
5774 case PK_PAGEUP: code = 5; break;
5775 case PK_PAGEDOWN: code = 6; break;
5776 default: code = 0; break; /* else gcc warns `enum value not used' */
5778 p += sprintf((char *) p, "\x1B[%d~", code);
5782 if (PK_ISFKEY(keysym)) {
5783 /* Map Shift+F1-F10 to F11-F20 */
5784 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5786 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5788 /* XXX This overrides the XTERM/VT52 mode below */
5790 if (keysym >= PK_F6) offt++;
5791 if (keysym >= PK_F12) offt++;
5792 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5793 'P' + keysym - PK_F1 - offt);
5796 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5797 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5800 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5801 if (term->vt52_mode)
5802 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5804 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5807 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5811 if (PK_ISCURSOR(keysym)) {
5815 case PK_UP: xkey = 'A'; break;
5816 case PK_DOWN: xkey = 'B'; break;
5817 case PK_RIGHT: xkey = 'C'; break;
5818 case PK_LEFT: xkey = 'D'; break;
5819 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5820 default: xkey = 0; break; /* else gcc warns `enum value not used' */
5822 if (term->vt52_mode)
5823 p += sprintf((char *) p, "\x1B%c", xkey);
5825 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5827 /* Useful mapping of Ctrl-arrows */
5828 if (modifiers == PKM_CONTROL)
5832 p += sprintf((char *) p, "\x1BO%c", xkey);
5834 p += sprintf((char *) p, "\x1B[%c", xkey);
5840 if (p > output || tlen > 0) {
5842 * Interrupt an ongoing paste. I'm not sure
5843 * this is sensible, but for the moment it's
5844 * preferable to having to faff about buffering
5850 * We need not bother about stdin backlogs
5851 * here, because in GUI PuTTY we can't do
5852 * anything about it anyway; there's no means
5853 * of asking Windows to hold off on KEYDOWN
5854 * messages. We _have_ to buffer everything
5857 term_seen_key_event(term);
5861 fprintf(stderr, "sending ESC\n");
5863 ldisc_send(term->ldisc, "\x1b", 1, 1);
5868 fprintf(stderr, "sending %d bytes:", p - output);
5869 for (i = 0; i < p - output; i++)
5870 fprintf(stderr, " %02x", output[i]);
5871 fprintf(stderr, "\n");
5873 ldisc_send(term->ldisc, output, p - output, 1);
5874 } else if (tlen > 0) {
5876 fprintf(stderr, "sending %d unichars:", tlen);
5877 for (i = 0; i < tlen; i++)
5878 fprintf(stderr, " %04x", (unsigned) text[i]);
5879 fprintf(stderr, "\n");
5881 luni_send(term->ldisc, text, tlen, 1);
5886 void term_nopaste(Terminal *term)
5888 if (term->paste_len == 0)
5890 sfree(term->paste_buffer);
5891 term->paste_buffer = NULL;
5892 term->paste_len = 0;
5895 int term_paste_pending(Terminal *term)
5897 return term->paste_len != 0;
5900 void term_paste(Terminal *term)
5902 long now, paste_diff;
5904 if (term->paste_len == 0)
5907 /* Don't wait forever to paste */
5908 if (term->paste_hold) {
5909 now = GETTICKCOUNT();
5910 paste_diff = now - term->last_paste;
5911 if (paste_diff >= 0 && paste_diff < 450)
5914 term->paste_hold = 0;
5916 while (term->paste_pos < term->paste_len) {
5918 while (n + term->paste_pos < term->paste_len) {
5919 if (term->paste_buffer[term->paste_pos + n++] == '\015')
5923 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
5924 term->paste_pos += n;
5926 if (term->paste_pos < term->paste_len) {
5927 term->paste_hold = 1;
5931 sfree(term->paste_buffer);
5932 term->paste_buffer = NULL;
5933 term->paste_len = 0;
5936 static void deselect(Terminal *term)
5938 term->selstate = NO_SELECTION;
5939 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
5942 void term_deselect(Terminal *term)
5948 int term_ldisc(Terminal *term, int option)
5950 if (option == LD_ECHO)
5951 return term->term_echoing;
5952 if (option == LD_EDIT)
5953 return term->term_editing;
5957 int term_data(Terminal *term, int is_stderr, const char *data, int len)
5959 bufchain_add(&term->inbuf, data, len);
5961 if (!term->in_term_out) {
5962 term->in_term_out = TRUE;
5963 term_blink(term, 1);
5965 term->in_term_out = FALSE;
5969 * term_out() always completely empties inbuf. Therefore,
5970 * there's no reason at all to return anything other than zero
5971 * from this function, because there _can't_ be a question of
5972 * the remote side needing to wait until term_out() has cleared
5975 * This is a slightly suboptimal way to deal with SSH2 - in
5976 * principle, the window mechanism would allow us to continue
5977 * to accept data on forwarded ports and X connections even
5978 * while the terminal processing was going slowly - but we
5979 * can't do the 100% right thing without moving the terminal
5980 * processing into a separate thread, and that might hurt
5981 * portability. So we manage stdout buffering the old SSH1 way:
5982 * if the terminal processing goes slowly, the whole SSH
5983 * connection stops accepting data until it's ready.
5985 * In practice, I can't imagine this causing serious trouble.
5990 void term_provide_logctx(Terminal *term, void *logctx)
5992 term->logctx = logctx;