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);
110 line->lattr = LATTR_NORM;
111 line->temporary = FALSE;
116 static void freeline(termline *line)
122 static void unlineptr(termline *line)
129 * Compress and decompress a termline into an RLE-based format for
130 * storing in scrollback. (Since scrollback almost never needs to
131 * be modified and exists in huge quantities, this is a sensible
132 * tradeoff, particularly since it allows us to continue adding
133 * features to the main termchar structure without proportionally
134 * bloating the terminal emulator's memory footprint unless those
135 * features are in constant use.)
141 static void add(struct buf *b, unsigned char c)
143 if (b->len >= b->size) {
144 b->size = (b->len * 3 / 2) + 512;
145 b->data = sresize(b->data, b->size, unsigned char);
147 b->data[b->len++] = c;
149 static int get(struct buf *b)
151 return b->data[b->len++];
153 static void makerle(struct buf *b, termline *ldata,
154 void (*makeliteral)(struct buf *b, termchar *c,
155 unsigned long *state))
157 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
158 termchar *c = ldata->chars;
159 unsigned long state = 0, oldstate;
166 prevlen = prevpos = 0;
171 makeliteral(b, c++, &state);
172 thislen = b->len - thispos;
173 if (thislen == prevlen &&
174 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
176 * This literal precisely matches the previous one.
177 * Turn it into a run if it's worthwhile.
179 * With one-byte literals, it costs us two bytes to
180 * encode a run, plus another byte to write the header
181 * to resume normal output; so a three-element run is
182 * neutral, and anything beyond that is unconditionally
183 * worthwhile. With two-byte literals or more, even a
186 if (thislen > 1 || prev2) {
190 * It's worth encoding a run. Start at prevpos,
191 * unless hdrsize==0 in which case we can back up
192 * another one and start by overwriting hdrpos.
195 hdrsize--; /* remove the literal at prevpos */
199 prevpos -= prevlen;/* and possibly another one */
203 assert(prevpos == hdrpos + 1);
205 b->len = prevpos+prevlen;
207 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
209 b->len = prevpos+prevlen+1;
211 * Terminate the previous run of ordinary
214 assert(hdrsize >= 1 && hdrsize <= 128);
215 b->data[hdrpos] = hdrsize - 1;
218 runlen = prev2 ? 3 : 2;
220 while (n > 0 && runlen < 129) {
224 makeliteral(b, c, &state);
225 tmplen = b->len - tmppos;
227 if (tmplen != thislen ||
228 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
230 break; /* run over */
235 assert(runlen >= 2 && runlen <= 129);
236 b->data[runpos] = runlen + 0x80 - 2;
245 * Just flag that the previous two literals were
246 * identical, in case we find a third identical one
247 * we want to turn into a run.
260 * This character isn't (yet) part of a run. Add it to
264 if (hdrsize == 128) {
265 b->data[hdrpos] = hdrsize - 1;
269 prevlen = prevpos = 0;
278 assert(hdrsize <= 128);
279 b->data[hdrpos] = hdrsize - 1;
284 static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
287 * My encoding for characters is UTF-8-like, in that it stores
288 * 7-bit ASCII in one byte and uses high-bit-set bytes as
289 * introducers to indicate a longer sequence. However, it's
290 * unlike UTF-8 in that it doesn't need to be able to
291 * resynchronise, and therefore I don't want to waste two bits
292 * per byte on having recognisable continuation characters.
293 * Also I don't want to rule out the possibility that I may one
294 * day use values 0x80000000-0xFFFFFFFF for interesting
295 * purposes, so unlike UTF-8 I need a full 32-bit range.
296 * Accordingly, here is my encoding:
298 * 00000000-0000007F: 0xxxxxxx (but see below)
299 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
300 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
301 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
302 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
304 * (`Z' is like `x' but is always going to be zero since the
305 * values I'm encoding don't go above 2^32. In principle the
306 * five-byte form of the encoding could extend to 2^35, and
307 * there could be six-, seven-, eight- and nine-byte forms as
308 * well to allow up to 64-bit values to be encoded. But that's
309 * completely unnecessary for these purposes!)
311 * The encoding as written above would be very simple, except
312 * that 7-bit ASCII can occur in several different ways in the
313 * terminal data; sometimes it crops up in the D800 page
314 * (CSET_ASCII) but at other times it's in the 0000 page (real
315 * Unicode). Therefore, this encoding is actually _stateful_:
316 * the one-byte encoding of 00-7F actually indicates `reuse the
317 * upper three bytes of the last character', and to encode an
318 * absolute value of 00-7F you need to use the two-byte form
321 if ((c->chr & ~0x7F) == *state) {
322 add(b, (unsigned char)(c->chr & 0x7F));
323 } else if (c->chr < 0x4000) {
324 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
325 add(b, (unsigned char)(c->chr & 0xFF));
326 } else if (c->chr < 0x200000) {
327 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
328 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
329 add(b, (unsigned char)(c->chr & 0xFF));
330 } else if (c->chr < 0x10000000) {
331 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
332 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
333 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
334 add(b, (unsigned char)(c->chr & 0xFF));
337 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
338 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
339 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
340 add(b, (unsigned char)(c->chr & 0xFF));
342 *state = c->chr & ~0xFF;
344 static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
347 * My encoding for attributes is 16-bit-granular and assumes
348 * that the top bit of the word is never required. I either
349 * store a two-byte value with the top bit clear (indicating
350 * just that value), or a four-byte value with the top bit set
351 * (indicating the same value with its top bit clear).
353 if (c->attr < 0x8000) {
354 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
355 add(b, (unsigned char)(c->attr & 0xFF));
357 add(b, (unsigned char)(((c->attr >> 24) & 0x7F) | 0x80));
358 add(b, (unsigned char)((c->attr >> 16) & 0xFF));
359 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
360 add(b, (unsigned char)(c->attr & 0xFF));
364 static termline *decompressline(unsigned char *data, int *bytes_used);
366 static unsigned char *compressline(termline *ldata)
368 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
371 * First, store the column count, 7 bits at a time, least
372 * significant `digit' first, with the high bit set on all but
378 add(b, (unsigned char)((n & 0x7F) | 0x80));
381 add(b, (unsigned char)(n));
385 * Next store the lattrs; same principle.
388 int n = ldata->lattr;
390 add(b, (unsigned char)((n & 0x7F) | 0x80));
393 add(b, (unsigned char)(n));
397 * Now we store a sequence of separate run-length encoded
398 * fragments, each containing exactly as many symbols as there
399 * are columns in the ldata.
401 * All of these have a common basic format:
403 * - a byte 00-7F indicates that X+1 literals follow it
404 * - a byte 80-FF indicates that a single literal follows it
405 * and expects to be repeated (X-0x80)+2 times.
407 * The format of the `literals' varies between the fragments.
409 makerle(b, ldata, makeliteral_chr);
410 makerle(b, ldata, makeliteral_attr);
413 * Diagnostics: ensure that the compressed data really does
414 * decompress to the right thing.
416 #ifndef CHECK_SB_COMPRESSION
421 #ifdef DIAGNOSTIC_SB_COMPRESSION
423 for (i = 0; i < b->len; i++) {
424 printf(" %02x ", b->data[i]);
429 dcl = decompressline(b->data, &dused);
430 assert(b->len == dused);
431 assert(ldata->cols == dcl->cols);
432 assert(ldata->lattr == dcl->lattr);
433 assert(!memcmp(ldata->chars, dcl->chars, ldata->cols * TSIZE));
435 #ifdef DIAGNOSTIC_SB_COMPRESSION
436 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
437 ldata->cols, 4 * ldata->cols, dused,
438 (double)dused / (4 * ldata->cols));
446 * Trim the allocated memory so we don't waste any, and return.
448 return sresize(b->data, b->len, unsigned char);
451 static void readrle(struct buf *b, termline *ldata,
452 void (*readliteral)(struct buf *b, termchar *c,
453 unsigned long *state))
456 unsigned long state = 0;
458 while (n < ldata->cols) {
464 int pos = b->len, count = hdr + 2 - 0x80;
466 assert(n < ldata->cols);
468 readliteral(b, ldata->chars + n, &state);
472 /* Just a sequence of consecutive literals. */
476 assert(n < ldata->cols);
477 readliteral(b, ldata->chars + n, &state);
483 assert(n == ldata->cols);
485 static void readliteral_chr(struct buf *b, termchar *c, unsigned long *state)
490 * 00000000-0000007F: 0xxxxxxx
491 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
492 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
493 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
494 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
499 c->chr = byte | *state;
500 } else if (byte < 0xC0) {
501 c->chr = (byte &~ 0xC0) << 8;
503 } else if (byte < 0xE0) {
504 c->chr = (byte &~ 0xE0) << 16;
505 c->chr |= get(b) << 8;
507 } else if (byte < 0xF0) {
508 c->chr = (byte &~ 0xF0) << 24;
509 c->chr |= get(b) << 16;
510 c->chr |= get(b) << 8;
513 assert(byte == 0xF0);
514 c->chr = get(b) << 24;
515 c->chr |= get(b) << 16;
516 c->chr |= get(b) << 8;
519 *state = c->chr & ~0xFF;
521 static void readliteral_attr(struct buf *b, termchar *c, unsigned long *state)
537 static termline *decompressline(unsigned char *data, int *bytes_used)
539 int ncols, byte, shift;
540 struct buf buffer, *b = &buffer;
547 * First read in the column count.
552 ncols |= (byte & 0x7F) << shift;
554 } while (byte & 0x80);
557 * Now create the output termline.
559 ldata = snew(termline);
560 ldata->chars = snewn(ncols, termchar);
562 ldata->temporary = TRUE;
565 * Now read in the lattr.
567 ldata->lattr = shift = 0;
570 ldata->lattr |= (byte & 0x7F) << shift;
572 } while (byte & 0x80);
575 * Now we read in each of the RLE streams in turn.
577 readrle(b, ldata, readliteral_chr);
578 readrle(b, ldata, readliteral_attr);
580 /* Return the number of bytes read, for diagnostic purposes. */
582 *bytes_used = b->len;
588 * Resize a line to make it `cols' columns wide.
590 static void resizeline(Terminal *term, termline *line, int cols)
594 if (line->cols != cols) {
596 * This line is the wrong length, which probably means it
597 * hasn't been accessed since a resize. Resize it now.
600 line->chars = sresize(line->chars, cols, TTYPE);
602 for (i = oldlen; i < cols; i++)
603 line->chars[i] = term->basic_erase_char;
608 * Get the number of lines in the scrollback.
610 static int sblines(Terminal *term)
612 int sblines = count234(term->scrollback);
613 if (term->cfg.erase_to_scrollback &&
614 term->alt_which && term->alt_screen) {
615 sblines += term->alt_sblines;
621 * Retrieve a line of the screen or of the scrollback, according to
622 * whether the y coordinate is non-negative or negative
625 static termline *lineptr(Terminal *term, int y, int lineno, int screen)
632 whichtree = term->screen;
639 if (term->cfg.erase_to_scrollback &&
640 term->alt_which && term->alt_screen) {
641 altlines = term->alt_sblines;
644 whichtree = term->scrollback;
645 treeindex = y + altlines + count234(term->scrollback);
647 whichtree = term->alt_screen;
648 treeindex = y + term->alt_sblines;
649 /* treeindex = y + count234(term->alt_screen); */
652 if (whichtree == term->scrollback) {
653 unsigned char *cline = index234(whichtree, treeindex);
654 line = decompressline(cline, NULL);
656 line = index234(whichtree, treeindex);
659 /* We assume that we don't screw up and retrieve something out of range. */
660 assert(line != NULL);
662 resizeline(term, line, term->cols);
663 /* FIXME: should we sort the compressed scrollback out here? */
668 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
669 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
672 * Set up power-on settings for the terminal.
674 static void power_on(Terminal *term)
676 term->curs.x = term->curs.y = 0;
677 term->alt_x = term->alt_y = 0;
678 term->savecurs.x = term->savecurs.y = 0;
679 term->alt_t = term->marg_t = 0;
680 if (term->rows != -1)
681 term->alt_b = term->marg_b = term->rows - 1;
683 term->alt_b = term->marg_b = 0;
684 if (term->cols != -1) {
686 for (i = 0; i < term->cols; i++)
687 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
689 term->alt_om = term->dec_om = term->cfg.dec_om;
690 term->alt_ins = term->insert = FALSE;
691 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
692 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
693 term->alt_cset = term->cset = term->save_cset = 0;
694 term->alt_utf = term->utf = term->save_utf = 0;
696 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
697 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
699 term->in_vbell = FALSE;
701 term->big_cursor = 0;
702 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
703 term->term_editing = term->term_echoing = FALSE;
704 term->app_cursor_keys = term->cfg.app_cursor;
705 term->app_keypad_keys = term->cfg.app_keypad;
706 term->use_bce = term->cfg.bce;
707 term->blink_is_real = term->cfg.blinktext;
708 term->erase_char = term->basic_erase_char;
710 term_print_finish(term);
713 for (i = 0; i < 256; i++)
714 term->wordness[i] = term->cfg.wordness[i];
717 swap_screen(term, 1, FALSE, FALSE);
718 erase_lots(term, FALSE, TRUE, TRUE);
719 swap_screen(term, 0, FALSE, FALSE);
720 erase_lots(term, FALSE, TRUE, TRUE);
725 * Force a screen update.
727 void term_update(Terminal *term)
730 ctx = get_ctx(term->frontend);
732 int need_sbar_update = term->seen_disp_event;
733 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
734 term->disptop = 0; /* return to main screen */
735 term->seen_disp_event = 0;
736 need_sbar_update = TRUE;
739 /* Allocate temporary buffers for Arabic shaping and bidi. */
740 if (!term->cfg.arabicshaping || !term->cfg.bidi)
742 term->wcFrom = sresize(term->wcFrom, term->cols, bidi_char);
743 term->ltemp = sresize(term->ltemp, term->cols+1, termchar);
744 term->wcTo = sresize(term->wcTo, term->cols, bidi_char);
747 if (need_sbar_update)
749 do_paint(term, ctx, TRUE);
750 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
756 * Called from front end when a keypress occurs, to trigger
757 * anything magical that needs to happen in that situation.
759 void term_seen_key_event(Terminal *term)
762 * On any keypress, clear the bell overload mechanism
763 * completely, on the grounds that large numbers of
764 * beeps coming from deliberate key action are likely
765 * to be intended (e.g. beeps from filename completion
766 * blocking repeatedly).
768 term->beep_overloaded = FALSE;
769 while (term->beephead) {
770 struct beeptime *tmp = term->beephead;
771 term->beephead = tmp->next;
774 term->beeptail = NULL;
778 * Reset the scrollback on keypress, if we're doing that.
780 if (term->cfg.scroll_on_key) {
781 term->disptop = 0; /* return to main screen */
782 term->seen_disp_event = 1;
787 * Same as power_on(), but an external function.
789 void term_pwron(Terminal *term)
792 if (term->ldisc) /* cause ldisc to notice changes */
793 ldisc_send(term->ldisc, NULL, 0, 0);
800 static void set_erase_char(Terminal *term)
802 term->erase_char = term->basic_erase_char;
804 term->erase_char.attr = (term->curr_attr &
805 (ATTR_FGMASK | ATTR_BGMASK));
809 * When the user reconfigures us, we need to check the forbidden-
810 * alternate-screen config option, disable raw mouse mode if the
811 * user has disabled mouse reporting, and abandon a print job if
812 * the user has disabled printing.
814 void term_reconfig(Terminal *term, Config *cfg)
817 * Before adopting the new config, check all those terminal
818 * settings which control power-on defaults; and if they've
819 * changed, we will modify the current state as well as the
820 * default one. The full list is: Auto wrap mode, DEC Origin
821 * Mode, BCE, blinking text, character classes.
823 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
826 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
827 reset_decom = (term->cfg.dec_om != cfg->dec_om);
828 reset_bce = (term->cfg.bce != cfg->bce);
829 reset_blink = (term->cfg.blinktext != cfg->blinktext);
831 for (i = 0; i < lenof(term->cfg.wordness); i++)
832 if (term->cfg.wordness[i] != cfg->wordness[i])
835 term->cfg = *cfg; /* STRUCTURE COPY */
838 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
840 term->alt_om = term->dec_om = term->cfg.dec_om;
842 term->use_bce = term->cfg.bce;
843 set_erase_char(term);
846 term->blink_is_real = term->cfg.blinktext;
848 for (i = 0; i < 256; i++)
849 term->wordness[i] = term->cfg.wordness[i];
851 if (term->cfg.no_alt_screen)
852 swap_screen(term, 0, FALSE, FALSE);
853 if (term->cfg.no_mouse_rep) {
854 term->xterm_mouse = 0;
855 set_raw_mouse_mode(term->frontend, 0);
857 if (term->cfg.no_remote_charset) {
858 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
859 term->sco_acs = term->alt_sco_acs = 0;
862 if (!*term->cfg.printer) {
863 term_print_finish(term);
868 * Clear the scrollback.
870 void term_clrsb(Terminal *term)
874 while ((line = delpos234(term->scrollback, 0)) != NULL) {
875 sfree(line); /* this is compressed data, not a termline */
877 term->tempsblines = 0;
878 term->alt_sblines = 0;
883 * Initialise the terminal.
885 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
891 * Allocate a new Terminal structure and initialise the fields
894 term = snew(Terminal);
895 term->frontend = frontend;
896 term->ucsdata = ucsdata;
897 term->cfg = *mycfg; /* STRUCTURE COPY */
899 term->compatibility_level = TM_PUTTY;
900 strcpy(term->id_string, "\033[?6c");
901 term->last_blink = term->last_tblink = 0;
902 term->paste_buffer = NULL;
904 term->last_paste = 0;
905 bufchain_init(&term->inbuf);
906 bufchain_init(&term->printer_buf);
907 term->printing = term->only_printing = FALSE;
908 term->print_job = NULL;
909 term->vt52_mode = FALSE;
910 term->cr_lf_return = FALSE;
911 term->seen_disp_event = FALSE;
912 term->xterm_mouse = term->mouse_is_down = FALSE;
913 term->reset_132 = FALSE;
914 term->blinker = term->tblinker = 0;
916 term->repeat_off = FALSE;
917 term->termstate = TOPLEVEL;
918 term->selstate = NO_SELECTION;
921 term->screen = term->alt_screen = term->scrollback = NULL;
922 term->tempsblines = 0;
923 term->alt_sblines = 0;
925 term->disptext = NULL;
926 term->dispcursx = term->dispcursy = -1;
929 term->rows = term->cols = -1;
931 term->beephead = term->beeptail = NULL;
932 #ifdef OPTIMISE_SCROLL
933 term->scrollhead = term->scrolltail = NULL;
934 #endif /* OPTIMISE_SCROLL */
936 term->lastbeep = FALSE;
937 term->beep_overloaded = FALSE;
938 term->attr_mask = 0xffffffff;
939 term->resize_fn = NULL;
940 term->resize_ctx = NULL;
941 term->in_term_out = FALSE;
946 term->bidi_cache_size = 0;
947 term->pre_bidi_cache = term->post_bidi_cache = NULL;
949 term->basic_erase_char.chr = CSET_ASCII | ' ';
950 term->basic_erase_char.attr = ATTR_DEFAULT;
955 void term_free(Terminal *term)
958 struct beeptime *beep;
961 while ((line = delpos234(term->scrollback, 0)) != NULL)
962 sfree(line); /* compressed data, not a termline */
963 freetree234(term->scrollback);
964 while ((line = delpos234(term->screen, 0)) != NULL)
966 freetree234(term->screen);
967 while ((line = delpos234(term->alt_screen, 0)) != NULL)
969 freetree234(term->alt_screen);
970 if (term->disptext) {
971 for (i = 0; i < term->rows; i++)
972 freeline(term->disptext[i]);
974 sfree(term->disptext);
975 while (term->beephead) {
976 beep = term->beephead;
977 term->beephead = beep->next;
980 bufchain_clear(&term->inbuf);
982 printer_finish_job(term->print_job);
983 bufchain_clear(&term->printer_buf);
984 sfree(term->paste_buffer);
989 for (i = 0; i < term->bidi_cache_size; i++) {
990 sfree(term->pre_bidi_cache[i]);
991 sfree(term->post_bidi_cache[i]);
993 sfree(term->pre_bidi_cache);
994 sfree(term->post_bidi_cache);
1000 * Set up the terminal for a given size.
1002 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
1005 termline **newdisp, *line;
1008 int save_alt_which = term->alt_which;
1010 if (newrows == term->rows && newcols == term->cols &&
1011 newsavelines == term->savelines)
1012 return; /* nothing to do */
1015 swap_screen(term, 0, FALSE, FALSE);
1017 term->alt_t = term->marg_t = 0;
1018 term->alt_b = term->marg_b = newrows - 1;
1020 if (term->rows == -1) {
1021 term->scrollback = newtree234(NULL);
1022 term->screen = newtree234(NULL);
1023 term->tempsblines = 0;
1028 * Resize the screen and scrollback. We only need to shift
1029 * lines around within our data structures, because lineptr()
1030 * will take care of resizing each individual line if
1033 * - If the new screen is longer, we shunt lines in from temporary
1034 * scrollback if possible, otherwise we add new blank lines at
1037 * - If the new screen is shorter, we remove any blank lines at
1038 * the bottom if possible, otherwise shunt lines above the cursor
1039 * to scrollback if possible, otherwise delete lines below the
1042 * - Then, if the new scrollback length is less than the
1043 * amount of scrollback we actually have, we must throw some
1046 sblen = count234(term->scrollback);
1047 /* Do this loop to expand the screen if newrows > rows */
1048 assert(term->rows == count234(term->screen));
1049 while (term->rows < newrows) {
1050 if (term->tempsblines > 0) {
1051 unsigned char *cline;
1052 /* Insert a line from the scrollback at the top of the screen. */
1053 assert(sblen >= term->tempsblines);
1054 cline = delpos234(term->scrollback, --sblen);
1055 line = decompressline(cline, NULL);
1057 line->temporary = FALSE; /* reconstituted line is now real */
1058 term->tempsblines -= 1;
1059 addpos234(term->screen, line, 0);
1061 term->savecurs.y += 1;
1063 /* Add a new blank line at the bottom of the screen. */
1064 line = newline(term, newcols, FALSE);
1065 addpos234(term->screen, line, count234(term->screen));
1069 /* Do this loop to shrink the screen if newrows < rows */
1070 while (term->rows > newrows) {
1071 if (term->curs.y < term->rows - 1) {
1072 /* delete bottom row, unless it contains the cursor */
1073 sfree(delpos234(term->screen, term->rows - 1));
1075 /* push top row to scrollback */
1076 line = delpos234(term->screen, 0);
1077 addpos234(term->scrollback, compressline(line), sblen++);
1079 term->tempsblines += 1;
1081 term->savecurs.y -= 1;
1085 assert(term->rows == newrows);
1086 assert(count234(term->screen) == newrows);
1088 /* Delete any excess lines from the scrollback. */
1089 while (sblen > newsavelines) {
1090 line = delpos234(term->scrollback, 0);
1094 if (sblen < term->tempsblines)
1095 term->tempsblines = sblen;
1096 assert(count234(term->scrollback) <= newsavelines);
1097 assert(count234(term->scrollback) >= term->tempsblines);
1100 /* Make a new displayed text buffer. */
1101 newdisp = snewn(newrows, termline *);
1102 for (i = 0; i < newrows; i++) {
1103 newdisp[i] = newline(term, newcols, FALSE);
1104 for (j = 0; j < newcols; j++)
1105 newdisp[i]->chars[i].attr = ATTR_INVALID;
1107 if (term->disptext) {
1108 for (i = 0; i < term->rows; i++)
1109 freeline(term->disptext[i]);
1111 sfree(term->disptext);
1112 term->disptext = newdisp;
1113 term->dispcursx = term->dispcursy = -1;
1115 /* Make a new alternate screen. */
1116 newalt = newtree234(NULL);
1117 for (i = 0; i < newrows; i++) {
1118 line = newline(term, newcols, TRUE);
1119 addpos234(newalt, line, i);
1121 if (term->alt_screen) {
1122 while (NULL != (line = delpos234(term->alt_screen, 0)))
1124 freetree234(term->alt_screen);
1126 term->alt_screen = newalt;
1127 term->alt_sblines = 0;
1129 term->tabs = sresize(term->tabs, newcols, unsigned char);
1132 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1133 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1136 /* Check that the cursor positions are still valid. */
1137 if (term->savecurs.y < 0)
1138 term->savecurs.y = 0;
1139 if (term->savecurs.y >= newrows)
1140 term->savecurs.y = newrows - 1;
1141 if (term->curs.y < 0)
1143 if (term->curs.y >= newrows)
1144 term->curs.y = newrows - 1;
1145 if (term->curs.x >= newcols)
1146 term->curs.x = newcols - 1;
1147 term->alt_x = term->alt_y = 0;
1148 term->wrapnext = term->alt_wnext = FALSE;
1150 term->rows = newrows;
1151 term->cols = newcols;
1152 term->savelines = newsavelines;
1155 swap_screen(term, save_alt_which, FALSE, FALSE);
1159 if (term->resize_fn)
1160 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1164 * Hand a function and context pointer to the terminal which it can
1165 * use to notify a back end of resizes.
1167 void term_provide_resize_fn(Terminal *term,
1168 void (*resize_fn)(void *, int, int),
1171 term->resize_fn = resize_fn;
1172 term->resize_ctx = resize_ctx;
1173 if (term->cols > 0 && term->rows > 0)
1174 resize_fn(resize_ctx, term->cols, term->rows);
1177 /* Find the bottom line on the screen that has any content.
1178 * If only the top line has content, returns 0.
1179 * If no lines have content, return -1.
1181 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1184 for (i = count234(screen) - 1; i >= 0; i--) {
1185 termline *line = index234(screen, i);
1187 for (j = 0; j < line->cols; j++) {
1188 if (line->chars[j].chr != term->erase_char.chr ||
1189 line->chars[j].attr != term->erase_char.attr) break;
1191 if (j != line->cols) break;
1197 * Swap screens. If `reset' is TRUE and we have been asked to
1198 * switch to the alternate screen, we must bring most of its
1199 * configuration from the main screen and erase the contents of the
1200 * alternate screen completely. (This is even true if we're already
1201 * on it! Blame xterm.)
1203 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
1209 reset = FALSE; /* do no weird resetting if which==0 */
1211 if (which != term->alt_which) {
1212 term->alt_which = which;
1214 ttr = term->alt_screen;
1215 term->alt_screen = term->screen;
1217 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
1219 if (!reset && !keep_cur_pos)
1220 term->curs.x = term->alt_x;
1223 if (!reset && !keep_cur_pos)
1224 term->curs.y = term->alt_y;
1227 if (!reset) term->marg_t = term->alt_t;
1230 if (!reset) term->marg_b = term->alt_b;
1233 if (!reset) term->dec_om = term->alt_om;
1236 if (!reset) term->wrap = term->alt_wrap;
1239 if (!reset) term->wrapnext = term->alt_wnext;
1240 term->alt_wnext = t;
1242 if (!reset) term->insert = term->alt_ins;
1245 if (!reset) term->cset = term->alt_cset;
1248 if (!reset) term->utf = term->alt_utf;
1251 if (!reset) term->sco_acs = term->alt_sco_acs;
1252 term->alt_sco_acs = t;
1255 if (reset && term->screen) {
1257 * Yes, this _is_ supposed to honour background-colour-erase.
1259 erase_lots(term, FALSE, TRUE, TRUE);
1263 * This might not be possible if we're called during
1271 * Update the scroll bar.
1273 static void update_sbar(Terminal *term)
1275 int nscroll = sblines(term);
1276 set_sbar(term->frontend, nscroll + term->rows,
1277 nscroll + term->disptop, term->rows);
1281 * Check whether the region bounded by the two pointers intersects
1282 * the scroll region, and de-select the on-screen selection if so.
1284 static void check_selection(Terminal *term, pos from, pos to)
1286 if (poslt(from, term->selend) && poslt(term->selstart, to))
1291 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1292 * for backward.) `sb' is TRUE if the scrolling is permitted to
1293 * affect the scrollback buffer.
1295 * NB this function invalidates all pointers into lines of the
1296 * screen data structures. In particular, you MUST call fix_cpos
1297 * after calling scroll() and before doing anything else that
1298 * uses the cpos shortcut pointer.
1300 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
1303 int i, seltop, olddisptop, shift;
1305 if (topline != 0 || term->alt_which != 0)
1308 olddisptop = term->disptop;
1312 line = delpos234(term->screen, botline);
1313 resizeline(term, line, term->cols);
1314 for (i = 0; i < term->cols; i++)
1315 line->chars[i] = term->erase_char;
1316 line->lattr = LATTR_NORM;
1317 addpos234(term->screen, line, topline);
1319 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1321 if (term->selstart.y > botline) {
1322 term->selstart.y = botline + 1;
1323 term->selstart.x = 0;
1326 if (term->selend.y >= topline && term->selend.y <= botline) {
1328 if (term->selend.y > botline) {
1329 term->selend.y = botline + 1;
1338 line = delpos234(term->screen, topline);
1339 if (sb && term->savelines > 0) {
1340 int sblen = count234(term->scrollback);
1342 * We must add this line to the scrollback. We'll
1343 * remove a line from the top of the scrollback if
1344 * the scrollback is full.
1346 if (sblen == term->savelines) {
1347 unsigned char *cline;
1350 cline = delpos234(term->scrollback, 0);
1353 term->tempsblines += 1;
1355 addpos234(term->scrollback, compressline(line), sblen);
1357 line = newline(term, term->cols, TRUE);
1360 * If the user is currently looking at part of the
1361 * scrollback, and they haven't enabled any options
1362 * that are going to reset the scrollback as a
1363 * result of this movement, then the chances are
1364 * they'd like to keep looking at the same line. So
1365 * we move their viewpoint at the same rate as the
1366 * scroll, at least until their viewpoint hits the
1367 * top end of the scrollback buffer, at which point
1368 * we don't have the choice any more.
1370 * Thanks to Jan Holmen Holsten for the idea and
1371 * initial implementation.
1373 if (term->disptop > -term->savelines && term->disptop < 0)
1376 resizeline(term, line, term->cols);
1377 for (i = 0; i < term->cols; i++)
1378 line->chars[i] = term->erase_char;
1379 line->lattr = LATTR_NORM;
1380 addpos234(term->screen, line, botline);
1383 * If the selection endpoints move into the scrollback,
1384 * we keep them moving until they hit the top. However,
1385 * of course, if the line _hasn't_ moved into the
1386 * scrollback then we don't do this, and cut them off
1387 * at the top of the scroll region.
1389 * This applies to selstart and selend (for an existing
1390 * selection), and also selanchor (for one being
1391 * selected as we speak).
1393 seltop = sb ? -term->savelines : topline;
1395 if (term->selstate != NO_SELECTION) {
1396 if (term->selstart.y >= seltop &&
1397 term->selstart.y <= botline) {
1399 if (term->selstart.y < seltop) {
1400 term->selstart.y = seltop;
1401 term->selstart.x = 0;
1404 if (term->selend.y >= seltop && term->selend.y <= botline) {
1406 if (term->selend.y < seltop) {
1407 term->selend.y = seltop;
1411 if (term->selanchor.y >= seltop &&
1412 term->selanchor.y <= botline) {
1413 term->selanchor.y--;
1414 if (term->selanchor.y < seltop) {
1415 term->selanchor.y = seltop;
1416 term->selanchor.x = 0;
1424 #ifdef OPTIMISE_SCROLL
1425 shift += term->disptop - olddisptop;
1426 if (shift < term->rows && shift > -term->rows && shift != 0)
1427 scroll_display(term, topline, botline, shift);
1428 #endif /* OPTIMISE_SCROLL */
1431 #ifdef OPTIMISE_SCROLL
1433 * Add a scroll of a region on the screen into the pending scroll list.
1434 * `lines' is +ve for scrolling forward, -ve for backward.
1436 * If the scroll is on the same area as the last scroll in the list,
1439 static void save_scroll(Terminal *term, int topline, int botline, int lines)
1441 struct scrollregion *newscroll;
1442 if (term->scrolltail &&
1443 term->scrolltail->topline == topline &&
1444 term->scrolltail->botline == botline) {
1445 term->scrolltail->lines += lines;
1447 newscroll = snew(struct scrollregion);
1448 newscroll->topline = topline;
1449 newscroll->botline = botline;
1450 newscroll->lines = lines;
1451 newscroll->next = NULL;
1453 if (!term->scrollhead)
1454 term->scrollhead = newscroll;
1456 term->scrolltail->next = newscroll;
1457 term->scrolltail = newscroll;
1462 * Scroll the physical display, and our conception of it in disptext.
1464 static void scroll_display(Terminal *term, int topline, int botline, int lines)
1466 int distance, nlines, i;
1468 distance = lines > 0 ? lines : -lines;
1469 nlines = botline - topline + 1 - distance;
1471 for (i = 0; i < nlines; i++)
1472 memcpy(term->disptext[start+i]->chars,
1473 term->disptext[start+i+distance]->chars,
1474 term->cols * TSIZE);
1475 if (term->dispcursy >= 0 &&
1476 term->dispcursy >= topline + distance &&
1477 term->dispcursy < topline + distance + nlines)
1478 term->dispcursy -= distance;
1479 for (i = 0; i < distance; i++)
1480 for (j = 0; j < term->cols; j++)
1481 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
1483 for (i = nlines; i-- ;)
1484 memcpy(term->disptext[start+i+distance]->chars,
1485 term->disptext[start+i]->chars,
1486 term->cols * TSIZE);
1487 if (term->dispcursy >= 0 &&
1488 term->dispcursy >= topline &&
1489 term->dispcursy < topline + nlines)
1490 term->dispcursy += distance;
1491 for (i = 0; i < distance; i++)
1492 for (j = 0; j < term->cols; j++)
1493 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
1495 save_scroll(term, topline, botline, lines);
1497 #endif /* OPTIMISE_SCROLL */
1500 * Move the cursor to a given position, clipping at boundaries. We
1501 * may or may not want to clip at the scroll margin: marg_clip is 0
1502 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1503 * even _being_ outside the margins.
1505 static void move(Terminal *term, int x, int y, int marg_clip)
1509 if (x >= term->cols)
1512 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1515 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1521 if (y >= term->rows)
1526 term->wrapnext = FALSE;
1530 * Save or restore the cursor and SGR mode.
1532 static void save_cursor(Terminal *term, int save)
1535 term->savecurs = term->curs;
1536 term->save_attr = term->curr_attr;
1537 term->save_cset = term->cset;
1538 term->save_utf = term->utf;
1539 term->save_wnext = term->wrapnext;
1540 term->save_csattr = term->cset_attr[term->cset];
1541 term->save_sco_acs = term->sco_acs;
1543 term->curs = term->savecurs;
1544 /* Make sure the window hasn't shrunk since the save */
1545 if (term->curs.x >= term->cols)
1546 term->curs.x = term->cols - 1;
1547 if (term->curs.y >= term->rows)
1548 term->curs.y = term->rows - 1;
1550 term->curr_attr = term->save_attr;
1551 term->cset = term->save_cset;
1552 term->utf = term->save_utf;
1553 term->wrapnext = term->save_wnext;
1555 * wrapnext might reset to False if the x position is no
1556 * longer at the rightmost edge.
1558 if (term->wrapnext && term->curs.x < term->cols-1)
1559 term->wrapnext = FALSE;
1560 term->cset_attr[term->cset] = term->save_csattr;
1561 term->sco_acs = term->save_sco_acs;
1563 set_erase_char(term);
1568 * This function is called before doing _anything_ which affects
1569 * only part of a line of text. It is used to mark the boundary
1570 * between two character positions, and it indicates that some sort
1571 * of effect is going to happen on only one side of that boundary.
1573 * The effect of this function is to check whether a CJK
1574 * double-width character is straddling the boundary, and to remove
1575 * it and replace it with two spaces if so. (Of course, one or
1576 * other of those spaces is then likely to be replaced with
1577 * something else again, as a result of whatever happens next.)
1579 * Also, if the boundary is at the right-hand _edge_ of the screen,
1580 * it implies something deliberate is being done to the rightmost
1581 * column position; hence we must clear LATTR_WRAPPED2.
1583 * The input to the function is the coordinates of the _second_
1584 * character of the pair.
1586 static void check_boundary(Terminal *term, int x, int y)
1590 /* Validate input coordinates, just in case. */
1591 if (x == 0 || x > term->cols)
1594 ldata = scrlineptr(y);
1595 if (x == term->cols) {
1596 ldata->lattr &= ~LATTR_WRAPPED2;
1598 if (ldata->chars[x].chr == UCSWIDE) {
1599 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
1600 ldata->chars[x] = ldata->chars[x-1];
1606 * Erase a large portion of the screen: the whole screen, or the
1607 * whole line, or parts thereof.
1609 static void erase_lots(Terminal *term,
1610 int line_only, int from_begin, int to_end)
1614 int erasing_lines_from_top = 0;
1617 start.y = term->curs.y;
1619 end.y = term->curs.y + 1;
1621 erase_lattr = FALSE;
1636 if (!from_begin || !to_end)
1637 check_boundary(term, term->curs.x, term->curs.y);
1638 check_selection(term, start, end);
1640 /* Clear screen also forces a full window redraw, just in case. */
1641 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1642 term_invalidate(term);
1644 /* Lines scrolled away shouldn't be brought back on if the terminal
1646 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1647 erasing_lines_from_top = 1;
1649 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1650 /* If it's a whole number of lines, starting at the top, and
1651 * we're fully erasing them, erase by scrolling and keep the
1652 * lines in the scrollback. */
1653 int scrolllines = end.y;
1654 if (end.y == term->rows) {
1655 /* Shrink until we find a non-empty row.*/
1656 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1658 if (scrolllines > 0)
1659 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1662 termline *ldata = scrlineptr(start.y);
1663 while (poslt(start, end)) {
1664 if (start.x == term->cols) {
1666 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1668 ldata->lattr = LATTR_NORM;
1670 ldata->chars[start.x] = term->erase_char;
1672 if (incpos(start) && start.y < term->rows) {
1673 ldata = scrlineptr(start.y);
1678 /* After an erase of lines from the top of the screen, we shouldn't
1679 * bring the lines back again if the terminal enlarges (since the user or
1680 * application has explictly thrown them away). */
1681 if (erasing_lines_from_top && !(term->alt_which))
1682 term->tempsblines = 0;
1686 * Insert or delete characters within the current line. n is +ve if
1687 * insertion is desired, and -ve for deletion.
1689 static void insch(Terminal *term, int n)
1691 int dir = (n < 0 ? -1 : +1);
1696 n = (n < 0 ? -n : n);
1697 if (n > term->cols - term->curs.x)
1698 n = term->cols - term->curs.x;
1699 m = term->cols - term->curs.x - n;
1700 cursplus.y = term->curs.y;
1701 cursplus.x = term->curs.x + n;
1702 check_selection(term, term->curs, cursplus);
1703 check_boundary(term, term->curs.x, term->curs.y);
1705 check_boundary(term, term->curs.x + n, term->curs.y);
1706 ldata = scrlineptr(term->curs.y);
1708 memmove(ldata->chars + term->curs.x, ldata->chars + term->curs.x + n,
1711 ldata->chars[term->curs.x + m++] = term->erase_char;
1713 memmove(ldata->chars + term->curs.x + n, ldata->chars + term->curs.x,
1716 ldata->chars[term->curs.x + n] = term->erase_char;
1721 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1722 * whether the mode is a DEC private one or a normal one.)
1724 static void toggle_mode(Terminal *term, int mode, int query, int state)
1726 unsigned long ticks;
1730 case 1: /* DECCKM: application cursor keys */
1731 term->app_cursor_keys = state;
1733 case 2: /* DECANM: VT52 mode */
1734 term->vt52_mode = !state;
1735 if (term->vt52_mode) {
1736 term->blink_is_real = FALSE;
1737 term->vt52_bold = FALSE;
1739 term->blink_is_real = term->cfg.blinktext;
1742 case 3: /* DECCOLM: 80/132 columns */
1744 if (!term->cfg.no_remote_resize)
1745 request_resize(term->frontend, state ? 132 : 80, term->rows);
1746 term->reset_132 = state;
1747 term->alt_t = term->marg_t = 0;
1748 term->alt_b = term->marg_b = term->rows - 1;
1749 move(term, 0, 0, 0);
1750 erase_lots(term, FALSE, TRUE, TRUE);
1752 case 5: /* DECSCNM: reverse video */
1754 * Toggle reverse video. If we receive an OFF within the
1755 * visual bell timeout period after an ON, we trigger an
1756 * effective visual bell, so that ESC[?5hESC[?5l will
1757 * always be an actually _visible_ visual bell.
1759 ticks = GETTICKCOUNT();
1760 /* turn off a previous vbell to avoid inconsistencies */
1761 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1762 term->in_vbell = FALSE;
1763 if (term->rvideo && !state && /* we're turning it off... */
1764 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1765 /* If there's no vbell timeout already, or this one lasts
1766 * longer, replace vbell_timeout with ours. */
1767 if (!term->in_vbell ||
1768 (term->rvbell_startpoint - term->vbell_startpoint <
1770 term->vbell_startpoint = term->rvbell_startpoint;
1771 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1772 } else if (!term->rvideo && state) {
1773 /* This is an ON, so we notice the time and save it. */
1774 term->rvbell_startpoint = ticks;
1776 term->rvideo = state;
1777 term->seen_disp_event = TRUE;
1781 case 6: /* DECOM: DEC origin mode */
1782 term->dec_om = state;
1784 case 7: /* DECAWM: auto wrap */
1787 case 8: /* DECARM: auto key repeat */
1788 term->repeat_off = !state;
1790 case 10: /* DECEDM: set local edit mode */
1791 term->term_editing = state;
1792 if (term->ldisc) /* cause ldisc to notice changes */
1793 ldisc_send(term->ldisc, NULL, 0, 0);
1795 case 25: /* DECTCEM: enable/disable cursor */
1796 compatibility2(OTHER, VT220);
1797 term->cursor_on = state;
1798 term->seen_disp_event = TRUE;
1800 case 47: /* alternate screen */
1801 compatibility(OTHER);
1803 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1806 case 1000: /* xterm mouse 1 */
1807 term->xterm_mouse = state ? 1 : 0;
1808 set_raw_mouse_mode(term->frontend, state);
1810 case 1002: /* xterm mouse 2 */
1811 term->xterm_mouse = state ? 2 : 0;
1812 set_raw_mouse_mode(term->frontend, state);
1814 case 1047: /* alternate screen */
1815 compatibility(OTHER);
1817 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1820 case 1048: /* save/restore cursor */
1821 if (!term->cfg.no_alt_screen)
1822 save_cursor(term, state);
1823 if (!state) term->seen_disp_event = TRUE;
1825 case 1049: /* cursor & alternate screen */
1826 if (state && !term->cfg.no_alt_screen)
1827 save_cursor(term, state);
1828 if (!state) term->seen_disp_event = TRUE;
1829 compatibility(OTHER);
1831 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1832 if (!state && !term->cfg.no_alt_screen)
1833 save_cursor(term, state);
1838 case 4: /* IRM: set insert mode */
1839 compatibility(VT102);
1840 term->insert = state;
1842 case 12: /* SRM: set echo mode */
1843 term->term_echoing = !state;
1844 if (term->ldisc) /* cause ldisc to notice changes */
1845 ldisc_send(term->ldisc, NULL, 0, 0);
1847 case 20: /* LNM: Return sends ... */
1848 term->cr_lf_return = state;
1850 case 34: /* WYULCURM: Make cursor BIG */
1851 compatibility2(OTHER, VT220);
1852 term->big_cursor = !state;
1857 * Process an OSC sequence: set window title or icon name.
1859 static void do_osc(Terminal *term)
1862 while (term->osc_strlen--)
1863 term->wordness[(unsigned char)
1864 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1866 term->osc_string[term->osc_strlen] = '\0';
1867 switch (term->esc_args[0]) {
1870 if (!term->cfg.no_remote_wintitle)
1871 set_icon(term->frontend, term->osc_string);
1872 if (term->esc_args[0] == 1)
1874 /* fall through: parameter 0 means set both */
1877 if (!term->cfg.no_remote_wintitle)
1878 set_title(term->frontend, term->osc_string);
1885 * ANSI printing routines.
1887 static void term_print_setup(Terminal *term)
1889 bufchain_clear(&term->printer_buf);
1890 term->print_job = printer_start_job(term->cfg.printer);
1892 static void term_print_flush(Terminal *term)
1897 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1898 bufchain_prefix(&term->printer_buf, &data, &len);
1901 printer_job_data(term->print_job, data, len);
1902 bufchain_consume(&term->printer_buf, len);
1905 static void term_print_finish(Terminal *term)
1911 if (!term->printing && !term->only_printing)
1912 return; /* we need do nothing */
1914 term_print_flush(term);
1915 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1916 bufchain_prefix(&term->printer_buf, &data, &len);
1918 if (c == '\033' || c == '\233') {
1919 bufchain_consume(&term->printer_buf, size);
1922 printer_job_data(term->print_job, &c, 1);
1923 bufchain_consume(&term->printer_buf, 1);
1926 printer_finish_job(term->print_job);
1927 term->print_job = NULL;
1928 term->printing = term->only_printing = FALSE;
1932 * Remove everything currently in `inbuf' and stick it up on the
1933 * in-memory display. There's a big state machine in here to
1934 * process escape sequences...
1936 void term_out(Terminal *term)
1940 unsigned char localbuf[256], *chars;
1945 chars = NULL; /* placate compiler warnings */
1946 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1950 bufchain_prefix(&term->inbuf, &ret, &nchars);
1951 if (nchars > sizeof(localbuf))
1952 nchars = sizeof(localbuf);
1953 memcpy(localbuf, ret, nchars);
1954 bufchain_consume(&term->inbuf, nchars);
1956 assert(chars != NULL);
1962 * Optionally log the session traffic to a file. Useful for
1963 * debugging and possibly also useful for actual logging.
1965 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1966 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1972 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1973 * be able to display 8-bit characters, but I'll let that go 'cause
1978 * If we're printing, add the character to the printer
1981 if (term->printing) {
1982 bufchain_add(&term->printer_buf, &c, 1);
1985 * If we're in print-only mode, we use a much simpler
1986 * state machine designed only to recognise the ESC[4i
1987 * termination sequence.
1989 if (term->only_printing) {
1991 term->print_state = 1;
1992 else if (c == (unsigned char)'\233')
1993 term->print_state = 2;
1994 else if (c == '[' && term->print_state == 1)
1995 term->print_state = 2;
1996 else if (c == '4' && term->print_state == 2)
1997 term->print_state = 3;
1998 else if (c == 'i' && term->print_state == 3)
1999 term->print_state = 4;
2001 term->print_state = 0;
2002 if (term->print_state == 4) {
2003 term_print_finish(term);
2009 /* First see about all those translations. */
2010 if (term->termstate == TOPLEVEL) {
2012 switch (term->utf_state) {
2015 /* UTF-8 must be stateless so we ignore iso2022. */
2016 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2017 c = term->ucsdata->unitab_ctrl[c];
2018 else c = ((unsigned char)c) | CSET_ASCII;
2020 } else if ((c & 0xe0) == 0xc0) {
2021 term->utf_size = term->utf_state = 1;
2022 term->utf_char = (c & 0x1f);
2023 } else if ((c & 0xf0) == 0xe0) {
2024 term->utf_size = term->utf_state = 2;
2025 term->utf_char = (c & 0x0f);
2026 } else if ((c & 0xf8) == 0xf0) {
2027 term->utf_size = term->utf_state = 3;
2028 term->utf_char = (c & 0x07);
2029 } else if ((c & 0xfc) == 0xf8) {
2030 term->utf_size = term->utf_state = 4;
2031 term->utf_char = (c & 0x03);
2032 } else if ((c & 0xfe) == 0xfc) {
2033 term->utf_size = term->utf_state = 5;
2034 term->utf_char = (c & 0x01);
2045 if ((c & 0xC0) != 0x80) {
2048 term->utf_state = 0;
2051 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2052 if (--term->utf_state)
2057 /* Is somebody trying to be evil! */
2059 (c < 0x800 && term->utf_size >= 2) ||
2060 (c < 0x10000 && term->utf_size >= 3) ||
2061 (c < 0x200000 && term->utf_size >= 4) ||
2062 (c < 0x4000000 && term->utf_size >= 5))
2065 /* Unicode line separator and paragraph separator are CR-LF */
2066 if (c == 0x2028 || c == 0x2029)
2069 /* High controls are probably a Baaad idea too. */
2073 /* The UTF-16 surrogates are not nice either. */
2074 /* The standard give the option of decoding these:
2075 * I don't want to! */
2076 if (c >= 0xD800 && c < 0xE000)
2079 /* ISO 10646 characters now limited to UTF-16 range. */
2083 /* This is currently a TagPhobic application.. */
2084 if (c >= 0xE0000 && c <= 0xE007F)
2087 /* U+FEFF is best seen as a null. */
2090 /* But U+FFFE is an error. */
2091 if (c == 0xFFFE || c == 0xFFFF)
2096 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2097 else if(term->sco_acs &&
2098 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
2100 if (term->sco_acs == 2) c |= 0x80;
2103 switch (term->cset_attr[term->cset]) {
2105 * Linedraw characters are different from 'ESC ( B'
2106 * only for a small range. For ones outside that
2107 * range, make sure we use the same font as well as
2108 * the same encoding.
2111 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2112 c = term->ucsdata->unitab_ctrl[c];
2114 c = ((unsigned char) c) | CSET_LINEDRW;
2118 /* If UK-ASCII, make the '#' a LineDraw Pound */
2120 c = '}' | CSET_LINEDRW;
2123 /*FALLTHROUGH*/ case CSET_ASCII:
2124 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2125 c = term->ucsdata->unitab_ctrl[c];
2127 c = ((unsigned char) c) | CSET_ASCII;
2130 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
2136 /* How about C1 controls ? */
2137 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2138 !term->vt52_mode && has_compat(VT220)) {
2139 term->termstate = SEEN_ESC;
2140 term->esc_query = FALSE;
2141 c = '@' + (c & 0x1F);
2144 /* Or the GL control. */
2145 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2146 if (term->curs.x && !term->wrapnext)
2148 term->wrapnext = FALSE;
2150 /* destructive backspace might be disabled */
2151 if (!term->cfg.no_dbackspace) {
2152 term->cpos->chr = ' ' | CSET_ASCII;
2153 term->cpos->attr = term->curr_attr;
2156 /* Or normal C0 controls. */
2157 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
2159 case '\005': /* ENQ: terminal type query */
2160 /* Strictly speaking this is VT100 but a VT100 defaults to
2161 * no response. Other terminals respond at their option.
2163 * Don't put a CR in the default string as this tends to
2164 * upset some weird software.
2166 * An xterm returns "xterm" (5 characters)
2168 compatibility(ANSIMIN);
2170 char abuf[256], *s, *d;
2172 for (s = term->cfg.answerback, d = abuf; *s; s++) {
2174 if (*s >= 'a' && *s <= 'z')
2175 *d++ = (*s - ('a' - 1));
2176 else if ((*s >= '@' && *s <= '_') ||
2177 *s == '?' || (*s & 0x80))
2182 } else if (*s == '^') {
2187 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2191 case '\007': /* BEL: Bell */
2193 struct beeptime *newbeep;
2194 unsigned long ticks;
2196 ticks = GETTICKCOUNT();
2198 if (!term->beep_overloaded) {
2199 newbeep = snew(struct beeptime);
2200 newbeep->ticks = ticks;
2201 newbeep->next = NULL;
2202 if (!term->beephead)
2203 term->beephead = newbeep;
2205 term->beeptail->next = newbeep;
2206 term->beeptail = newbeep;
2211 * Throw out any beeps that happened more than
2214 while (term->beephead &&
2215 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
2216 struct beeptime *tmp = term->beephead;
2217 term->beephead = tmp->next;
2219 if (!term->beephead)
2220 term->beeptail = NULL;
2224 if (term->cfg.bellovl && term->beep_overloaded &&
2225 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
2227 * If we're currently overloaded and the
2228 * last beep was more than s seconds ago,
2229 * leave overload mode.
2231 term->beep_overloaded = FALSE;
2232 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2233 term->nbeeps >= term->cfg.bellovl_n) {
2235 * Now, if we have n or more beeps
2236 * remaining in the queue, go into overload
2239 term->beep_overloaded = TRUE;
2241 term->lastbeep = ticks;
2244 * Perform an actual beep if we're not overloaded.
2246 if (!term->cfg.bellovl || !term->beep_overloaded) {
2247 beep(term->frontend, term->cfg.beep);
2248 if (term->cfg.beep == BELL_VISUAL) {
2249 term->in_vbell = TRUE;
2250 term->vbell_startpoint = ticks;
2254 term->seen_disp_event = TRUE;
2257 case '\b': /* BS: Back space */
2258 if (term->curs.x == 0 &&
2259 (term->curs.y == 0 || term->wrap == 0))
2261 else if (term->curs.x == 0 && term->curs.y > 0)
2262 term->curs.x = term->cols - 1, term->curs.y--;
2263 else if (term->wrapnext)
2264 term->wrapnext = FALSE;
2268 term->seen_disp_event = TRUE;
2270 case '\016': /* LS1: Locking-shift one */
2271 compatibility(VT100);
2274 case '\017': /* LS0: Locking-shift zero */
2275 compatibility(VT100);
2278 case '\033': /* ESC: Escape */
2279 if (term->vt52_mode)
2280 term->termstate = VT52_ESC;
2282 compatibility(ANSIMIN);
2283 term->termstate = SEEN_ESC;
2284 term->esc_query = FALSE;
2287 case '\015': /* CR: Carriage return */
2289 term->wrapnext = FALSE;
2291 term->seen_disp_event = TRUE;
2292 term->paste_hold = 0;
2294 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2296 case '\014': /* FF: Form feed */
2297 if (has_compat(SCOANSI)) {
2298 move(term, 0, 0, 0);
2299 erase_lots(term, FALSE, FALSE, TRUE);
2301 term->wrapnext = FALSE;
2302 term->seen_disp_event = 1;
2305 case '\013': /* VT: Line tabulation */
2306 compatibility(VT100);
2307 case '\012': /* LF: Line feed */
2308 if (term->curs.y == term->marg_b)
2309 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2310 else if (term->curs.y < term->rows - 1)
2312 if (term->cfg.lfhascr)
2315 term->wrapnext = FALSE;
2316 term->seen_disp_event = 1;
2317 term->paste_hold = 0;
2319 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2321 case '\t': /* HT: Character tabulation */
2323 pos old_curs = term->curs;
2324 termline *ldata = scrlineptr(term->curs.y);
2328 } while (term->curs.x < term->cols - 1 &&
2329 !term->tabs[term->curs.x]);
2331 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
2332 if (term->curs.x >= term->cols / 2)
2333 term->curs.x = term->cols / 2 - 1;
2335 if (term->curs.x >= term->cols)
2336 term->curs.x = term->cols - 1;
2340 check_selection(term, old_curs, term->curs);
2342 term->seen_disp_event = TRUE;
2346 switch (term->termstate) {
2348 /* Only graphic characters get this far;
2349 * ctrls are stripped above */
2350 if (term->wrapnext && term->wrap) {
2351 scrlineptr(term->curs.y)->lattr |= LATTR_WRAPPED;
2352 if (term->curs.y == term->marg_b)
2353 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2354 else if (term->curs.y < term->rows - 1)
2358 term->wrapnext = FALSE;
2362 if (term->selstate != NO_SELECTION) {
2363 pos cursplus = term->curs;
2365 check_selection(term, term->curs, cursplus);
2367 if (((c & CSET_MASK) == CSET_ASCII || (c & CSET_MASK) == 0) &&
2369 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2375 width = wcwidth((wchar_t) c);
2379 * If we're about to display a double-width
2380 * character starting in the rightmost
2381 * column, then we do something special
2382 * instead. We must print a space in the
2383 * last column of the screen, then wrap;
2384 * and we also set LATTR_WRAPPED2 which
2385 * instructs subsequent cut-and-pasting not
2386 * only to splice this line to the one
2387 * after it, but to ignore the space in the
2388 * last character position as well.
2389 * (Because what was actually output to the
2390 * terminal was presumably just a sequence
2391 * of CJK characters, and we don't want a
2392 * space to be pasted in the middle of
2393 * those just because they had the
2394 * misfortune to start in the wrong parity
2395 * column. xterm concurs.)
2397 check_boundary(term, term->curs.x, term->curs.y);
2398 check_boundary(term, term->curs.x+2, term->curs.y);
2399 if (term->curs.x == term->cols-1) {
2400 term->cpos->chr = CSET_ASCII | ' ';
2401 term->cpos->attr = term->curr_attr;
2402 scrlineptr(term->curs.y)->lattr |=
2403 LATTR_WRAPPED | LATTR_WRAPPED2;
2404 if (term->curs.y == term->marg_b)
2405 scroll(term, term->marg_t, term->marg_b,
2407 else if (term->curs.y < term->rows - 1)
2411 /* Now we must check_boundary again, of course. */
2412 check_boundary(term, term->curs.x, term->curs.y);
2413 check_boundary(term, term->curs.x+2, term->curs.y);
2415 term->cpos->chr = c;
2416 term->cpos->attr = term->curr_attr;
2418 term->cpos->chr = UCSWIDE;
2419 term->cpos->attr = term->curr_attr;
2423 check_boundary(term, term->curs.x, term->curs.y);
2424 check_boundary(term, term->curs.x+1, term->curs.y);
2425 term->cpos->chr = c;
2426 term->cpos->attr = term->curr_attr;
2434 if (term->curs.x == term->cols) {
2437 term->wrapnext = TRUE;
2438 if (term->wrap && term->vt52_mode) {
2439 scrlineptr(term->curs.y)->lattr |= LATTR_WRAPPED;
2440 if (term->curs.y == term->marg_b)
2441 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2442 else if (term->curs.y < term->rows - 1)
2446 term->wrapnext = FALSE;
2449 term->seen_disp_event = 1;
2454 * This state is virtually identical to SEEN_ESC, with the
2455 * exception that we have an OSC sequence in the pipeline,
2456 * and _if_ we see a backslash, we process it.
2460 term->termstate = TOPLEVEL;
2463 /* else fall through */
2465 if (c >= ' ' && c <= '/') {
2466 if (term->esc_query)
2467 term->esc_query = -1;
2469 term->esc_query = c;
2472 term->termstate = TOPLEVEL;
2473 switch (ANSI(c, term->esc_query)) {
2474 case '[': /* enter CSI mode */
2475 term->termstate = SEEN_CSI;
2476 term->esc_nargs = 1;
2477 term->esc_args[0] = ARG_DEFAULT;
2478 term->esc_query = FALSE;
2480 case ']': /* OSC: xterm escape sequences */
2481 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2482 compatibility(OTHER);
2483 term->termstate = SEEN_OSC;
2484 term->esc_args[0] = 0;
2486 case '7': /* DECSC: save cursor */
2487 compatibility(VT100);
2488 save_cursor(term, TRUE);
2490 case '8': /* DECRC: restore cursor */
2491 compatibility(VT100);
2492 save_cursor(term, FALSE);
2493 term->seen_disp_event = TRUE;
2495 case '=': /* DECKPAM: Keypad application mode */
2496 compatibility(VT100);
2497 term->app_keypad_keys = TRUE;
2499 case '>': /* DECKPNM: Keypad numeric mode */
2500 compatibility(VT100);
2501 term->app_keypad_keys = FALSE;
2503 case 'D': /* IND: exactly equivalent to LF */
2504 compatibility(VT100);
2505 if (term->curs.y == term->marg_b)
2506 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2507 else if (term->curs.y < term->rows - 1)
2510 term->wrapnext = FALSE;
2511 term->seen_disp_event = TRUE;
2513 case 'E': /* NEL: exactly equivalent to CR-LF */
2514 compatibility(VT100);
2516 if (term->curs.y == term->marg_b)
2517 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2518 else if (term->curs.y < term->rows - 1)
2521 term->wrapnext = FALSE;
2522 term->seen_disp_event = TRUE;
2524 case 'M': /* RI: reverse index - backwards LF */
2525 compatibility(VT100);
2526 if (term->curs.y == term->marg_t)
2527 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2528 else if (term->curs.y > 0)
2531 term->wrapnext = FALSE;
2532 term->seen_disp_event = TRUE;
2534 case 'Z': /* DECID: terminal type query */
2535 compatibility(VT100);
2537 ldisc_send(term->ldisc, term->id_string,
2538 strlen(term->id_string), 0);
2540 case 'c': /* RIS: restore power-on settings */
2541 compatibility(VT100);
2543 if (term->ldisc) /* cause ldisc to notice changes */
2544 ldisc_send(term->ldisc, NULL, 0, 0);
2545 if (term->reset_132) {
2546 if (!term->cfg.no_remote_resize)
2547 request_resize(term->frontend, 80, term->rows);
2548 term->reset_132 = 0;
2552 term->seen_disp_event = TRUE;
2554 case 'H': /* HTS: set a tab */
2555 compatibility(VT100);
2556 term->tabs[term->curs.x] = TRUE;
2559 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2560 compatibility(VT100);
2566 for (i = 0; i < term->rows; i++) {
2567 ldata = scrlineptr(i);
2568 for (j = 0; j < term->cols; j++) {
2569 ldata->chars[j] = term->basic_erase_char;
2570 ldata->chars[j].chr = 'E';
2572 ldata->lattr = LATTR_NORM;
2575 term->seen_disp_event = TRUE;
2576 scrtop.x = scrtop.y = 0;
2578 scrbot.y = term->rows;
2579 check_selection(term, scrtop, scrbot);
2583 case ANSI('3', '#'):
2584 case ANSI('4', '#'):
2585 case ANSI('5', '#'):
2586 case ANSI('6', '#'):
2587 compatibility(VT100);
2591 switch (ANSI(c, term->esc_query)) {
2592 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2595 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2598 case ANSI('5', '#'): /* DECSWL: normal */
2599 nlattr = LATTR_NORM;
2601 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2602 nlattr = LATTR_WIDE;
2605 scrlineptr(term->curs.y)->lattr = nlattr;
2608 /* GZD4: G0 designate 94-set */
2609 case ANSI('A', '('):
2610 compatibility(VT100);
2611 if (!term->cfg.no_remote_charset)
2612 term->cset_attr[0] = CSET_GBCHR;
2614 case ANSI('B', '('):
2615 compatibility(VT100);
2616 if (!term->cfg.no_remote_charset)
2617 term->cset_attr[0] = CSET_ASCII;
2619 case ANSI('0', '('):
2620 compatibility(VT100);
2621 if (!term->cfg.no_remote_charset)
2622 term->cset_attr[0] = CSET_LINEDRW;
2624 case ANSI('U', '('):
2625 compatibility(OTHER);
2626 if (!term->cfg.no_remote_charset)
2627 term->cset_attr[0] = CSET_SCOACS;
2629 /* G1D4: G1-designate 94-set */
2630 case ANSI('A', ')'):
2631 compatibility(VT100);
2632 if (!term->cfg.no_remote_charset)
2633 term->cset_attr[1] = CSET_GBCHR;
2635 case ANSI('B', ')'):
2636 compatibility(VT100);
2637 if (!term->cfg.no_remote_charset)
2638 term->cset_attr[1] = CSET_ASCII;
2640 case ANSI('0', ')'):
2641 compatibility(VT100);
2642 if (!term->cfg.no_remote_charset)
2643 term->cset_attr[1] = CSET_LINEDRW;
2645 case ANSI('U', ')'):
2646 compatibility(OTHER);
2647 if (!term->cfg.no_remote_charset)
2648 term->cset_attr[1] = CSET_SCOACS;
2650 /* DOCS: Designate other coding system */
2651 case ANSI('8', '%'): /* Old Linux code */
2652 case ANSI('G', '%'):
2653 compatibility(OTHER);
2654 if (!term->cfg.no_remote_charset)
2657 case ANSI('@', '%'):
2658 compatibility(OTHER);
2659 if (!term->cfg.no_remote_charset)
2665 term->termstate = TOPLEVEL; /* default */
2667 if (term->esc_nargs <= ARGS_MAX) {
2668 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2669 term->esc_args[term->esc_nargs - 1] = 0;
2670 term->esc_args[term->esc_nargs - 1] =
2671 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2673 term->termstate = SEEN_CSI;
2674 } else if (c == ';') {
2675 if (++term->esc_nargs <= ARGS_MAX)
2676 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2677 term->termstate = SEEN_CSI;
2678 } else if (c < '@') {
2679 if (term->esc_query)
2680 term->esc_query = -1;
2682 term->esc_query = TRUE;
2684 term->esc_query = c;
2685 term->termstate = SEEN_CSI;
2687 switch (ANSI(c, term->esc_query)) {
2688 case 'A': /* CUU: move up N lines */
2689 move(term, term->curs.x,
2690 term->curs.y - def(term->esc_args[0], 1), 1);
2691 term->seen_disp_event = TRUE;
2693 case 'e': /* VPR: move down N lines */
2694 compatibility(ANSI);
2696 case 'B': /* CUD: Cursor down */
2697 move(term, term->curs.x,
2698 term->curs.y + def(term->esc_args[0], 1), 1);
2699 term->seen_disp_event = TRUE;
2701 case ANSI('c', '>'): /* DA: report xterm version */
2702 compatibility(OTHER);
2703 /* this reports xterm version 136 so that VIM can
2704 use the drag messages from the mouse reporting */
2706 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2708 case 'a': /* HPR: move right N cols */
2709 compatibility(ANSI);
2711 case 'C': /* CUF: Cursor right */
2712 move(term, term->curs.x + def(term->esc_args[0], 1),
2714 term->seen_disp_event = TRUE;
2716 case 'D': /* CUB: move left N cols */
2717 move(term, term->curs.x - def(term->esc_args[0], 1),
2719 term->seen_disp_event = TRUE;
2721 case 'E': /* CNL: move down N lines and CR */
2722 compatibility(ANSI);
2724 term->curs.y + def(term->esc_args[0], 1), 1);
2725 term->seen_disp_event = TRUE;
2727 case 'F': /* CPL: move up N lines and CR */
2728 compatibility(ANSI);
2730 term->curs.y - def(term->esc_args[0], 1), 1);
2731 term->seen_disp_event = TRUE;
2734 case '`': /* HPA: set horizontal posn */
2735 compatibility(ANSI);
2736 move(term, def(term->esc_args[0], 1) - 1,
2738 term->seen_disp_event = TRUE;
2740 case 'd': /* VPA: set vertical posn */
2741 compatibility(ANSI);
2742 move(term, term->curs.x,
2743 ((term->dec_om ? term->marg_t : 0) +
2744 def(term->esc_args[0], 1) - 1),
2745 (term->dec_om ? 2 : 0));
2746 term->seen_disp_event = TRUE;
2749 case 'f': /* HVP: set horz and vert posns at once */
2750 if (term->esc_nargs < 2)
2751 term->esc_args[1] = ARG_DEFAULT;
2752 move(term, def(term->esc_args[1], 1) - 1,
2753 ((term->dec_om ? term->marg_t : 0) +
2754 def(term->esc_args[0], 1) - 1),
2755 (term->dec_om ? 2 : 0));
2756 term->seen_disp_event = TRUE;
2758 case 'J': /* ED: erase screen or parts of it */
2760 unsigned int i = def(term->esc_args[0], 0) + 1;
2763 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2766 term->seen_disp_event = TRUE;
2768 case 'K': /* EL: erase line or parts of it */
2770 unsigned int i = def(term->esc_args[0], 0) + 1;
2773 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2775 term->seen_disp_event = TRUE;
2777 case 'L': /* IL: insert lines */
2778 compatibility(VT102);
2779 if (term->curs.y <= term->marg_b)
2780 scroll(term, term->curs.y, term->marg_b,
2781 -def(term->esc_args[0], 1), FALSE);
2783 term->seen_disp_event = TRUE;
2785 case 'M': /* DL: delete lines */
2786 compatibility(VT102);
2787 if (term->curs.y <= term->marg_b)
2788 scroll(term, term->curs.y, term->marg_b,
2789 def(term->esc_args[0], 1),
2792 term->seen_disp_event = TRUE;
2794 case '@': /* ICH: insert chars */
2795 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2796 compatibility(VT102);
2797 insch(term, def(term->esc_args[0], 1));
2798 term->seen_disp_event = TRUE;
2800 case 'P': /* DCH: delete chars */
2801 compatibility(VT102);
2802 insch(term, -def(term->esc_args[0], 1));
2803 term->seen_disp_event = TRUE;
2805 case 'c': /* DA: terminal type query */
2806 compatibility(VT100);
2807 /* This is the response for a VT102 */
2809 ldisc_send(term->ldisc, term->id_string,
2810 strlen(term->id_string), 0);
2812 case 'n': /* DSR: cursor position query */
2814 if (term->esc_args[0] == 6) {
2816 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2818 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2819 } else if (term->esc_args[0] == 5) {
2820 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2824 case 'h': /* SM: toggle modes to high */
2826 compatibility(VT100);
2829 for (i = 0; i < term->esc_nargs; i++)
2830 toggle_mode(term, term->esc_args[i],
2831 term->esc_query, TRUE);
2834 case 'i': /* MC: Media copy */
2836 compatibility(VT100);
2838 if (term->esc_nargs != 1) break;
2839 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2840 term->printing = TRUE;
2841 term->only_printing = !term->esc_query;
2842 term->print_state = 0;
2843 term_print_setup(term);
2844 } else if (term->esc_args[0] == 4 &&
2846 term_print_finish(term);
2850 case 'l': /* RM: toggle modes to low */
2852 compatibility(VT100);
2855 for (i = 0; i < term->esc_nargs; i++)
2856 toggle_mode(term, term->esc_args[i],
2857 term->esc_query, FALSE);
2860 case 'g': /* TBC: clear tabs */
2861 compatibility(VT100);
2862 if (term->esc_nargs == 1) {
2863 if (term->esc_args[0] == 0) {
2864 term->tabs[term->curs.x] = FALSE;
2865 } else if (term->esc_args[0] == 3) {
2867 for (i = 0; i < term->cols; i++)
2868 term->tabs[i] = FALSE;
2872 case 'r': /* DECSTBM: set scroll margins */
2873 compatibility(VT100);
2874 if (term->esc_nargs <= 2) {
2876 top = def(term->esc_args[0], 1) - 1;
2877 bot = (term->esc_nargs <= 1
2878 || term->esc_args[1] == 0 ?
2880 def(term->esc_args[1], term->rows)) - 1;
2881 if (bot >= term->rows)
2882 bot = term->rows - 1;
2883 /* VTTEST Bug 9 - if region is less than 2 lines
2884 * don't change region.
2886 if (bot - top > 0) {
2891 * I used to think the cursor should be
2892 * placed at the top of the newly marginned
2893 * area. Apparently not: VMS TPU falls over
2896 * Well actually it should for
2899 term->curs.y = (term->dec_om ?
2902 term->seen_disp_event = TRUE;
2906 case 'm': /* SGR: set graphics rendition */
2909 * A VT100 without the AVO only had one
2910 * attribute, either underline or
2911 * reverse video depending on the
2912 * cursor type, this was selected by
2916 * This is sometimes DIM, eg on the
2919 * This is sometimes INVIS various ANSI.
2921 * This like 22 disables BOLD, DIM and INVIS
2923 * The ANSI colours appear on any
2924 * terminal that has colour (obviously)
2925 * but the interaction between sgr0 and
2926 * the colours varies but is usually
2927 * related to the background colour
2928 * erase item. The interaction between
2929 * colour attributes and the mono ones
2930 * is also very implementation
2933 * The 39 and 49 attributes are likely
2934 * to be unimplemented.
2937 for (i = 0; i < term->esc_nargs; i++) {
2938 switch (def(term->esc_args[i], 0)) {
2939 case 0: /* restore defaults */
2940 term->curr_attr = term->default_attr;
2942 case 1: /* enable bold */
2943 compatibility(VT100AVO);
2944 term->curr_attr |= ATTR_BOLD;
2946 case 21: /* (enable double underline) */
2947 compatibility(OTHER);
2948 case 4: /* enable underline */
2949 compatibility(VT100AVO);
2950 term->curr_attr |= ATTR_UNDER;
2952 case 5: /* enable blink */
2953 compatibility(VT100AVO);
2954 term->curr_attr |= ATTR_BLINK;
2956 case 6: /* SCO light bkgrd */
2957 compatibility(SCOANSI);
2958 term->blink_is_real = FALSE;
2959 term->curr_attr |= ATTR_BLINK;
2961 case 7: /* enable reverse video */
2962 term->curr_attr |= ATTR_REVERSE;
2964 case 10: /* SCO acs off */
2965 compatibility(SCOANSI);
2966 if (term->cfg.no_remote_charset) break;
2967 term->sco_acs = 0; break;
2968 case 11: /* SCO acs on */
2969 compatibility(SCOANSI);
2970 if (term->cfg.no_remote_charset) break;
2971 term->sco_acs = 1; break;
2972 case 12: /* SCO acs on, |0x80 */
2973 compatibility(SCOANSI);
2974 if (term->cfg.no_remote_charset) break;
2975 term->sco_acs = 2; break;
2976 case 22: /* disable bold */
2977 compatibility2(OTHER, VT220);
2978 term->curr_attr &= ~ATTR_BOLD;
2980 case 24: /* disable underline */
2981 compatibility2(OTHER, VT220);
2982 term->curr_attr &= ~ATTR_UNDER;
2984 case 25: /* disable blink */
2985 compatibility2(OTHER, VT220);
2986 term->curr_attr &= ~ATTR_BLINK;
2988 case 27: /* disable reverse video */
2989 compatibility2(OTHER, VT220);
2990 term->curr_attr &= ~ATTR_REVERSE;
3001 term->curr_attr &= ~ATTR_FGMASK;
3003 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
3013 /* xterm-style bright foreground */
3014 term->curr_attr &= ~ATTR_FGMASK;
3016 ((term->esc_args[i] - 90 + 16)
3019 case 39: /* default-foreground */
3020 term->curr_attr &= ~ATTR_FGMASK;
3021 term->curr_attr |= ATTR_DEFFG;
3032 term->curr_attr &= ~ATTR_BGMASK;
3034 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
3044 /* xterm-style bright background */
3045 term->curr_attr &= ~ATTR_BGMASK;
3047 ((term->esc_args[i] - 100 + 16)
3050 case 49: /* default-background */
3051 term->curr_attr &= ~ATTR_BGMASK;
3052 term->curr_attr |= ATTR_DEFBG;
3056 set_erase_char(term);
3059 case 's': /* save cursor */
3060 save_cursor(term, TRUE);
3062 case 'u': /* restore cursor */
3063 save_cursor(term, FALSE);
3064 term->seen_disp_event = TRUE;
3066 case 't': /* DECSLPP: set page size - ie window height */
3068 * VT340/VT420 sequence DECSLPP, DEC only allows values
3069 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3070 * illegal values (eg first arg 1..9) for window changing
3073 if (term->esc_nargs <= 1
3074 && (term->esc_args[0] < 1 ||
3075 term->esc_args[0] >= 24)) {
3076 compatibility(VT340TEXT);
3077 if (!term->cfg.no_remote_resize)
3078 request_resize(term->frontend, term->cols,
3079 def(term->esc_args[0], 24));
3081 } else if (term->esc_nargs >= 1 &&
3082 term->esc_args[0] >= 1 &&
3083 term->esc_args[0] < 24) {
3084 compatibility(OTHER);
3086 switch (term->esc_args[0]) {
3090 set_iconic(term->frontend, FALSE);
3093 set_iconic(term->frontend, TRUE);
3096 if (term->esc_nargs >= 3) {
3097 if (!term->cfg.no_remote_resize)
3098 move_window(term->frontend,
3099 def(term->esc_args[1], 0),
3100 def(term->esc_args[2], 0));
3104 /* We should resize the window to a given
3105 * size in pixels here, but currently our
3106 * resizing code isn't healthy enough to
3111 set_zorder(term->frontend, TRUE);
3114 /* move to bottom */
3115 set_zorder(term->frontend, FALSE);
3118 refresh_window(term->frontend);
3121 if (term->esc_nargs >= 3) {
3122 if (!term->cfg.no_remote_resize)
3123 request_resize(term->frontend,
3124 def(term->esc_args[2], term->cfg.width),
3125 def(term->esc_args[1], term->cfg.height));
3129 if (term->esc_nargs >= 2)
3130 set_zoomed(term->frontend,
3136 ldisc_send(term->ldisc,
3137 is_iconic(term->frontend) ?
3138 "\033[1t" : "\033[2t", 4, 0);
3142 get_window_pos(term->frontend, &x, &y);
3143 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3144 ldisc_send(term->ldisc, buf, len, 0);
3149 get_window_pixels(term->frontend, &x, &y);
3150 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3151 ldisc_send(term->ldisc, buf, len, 0);
3156 len = sprintf(buf, "\033[8;%d;%dt",
3157 term->rows, term->cols);
3158 ldisc_send(term->ldisc, buf, len, 0);
3163 * Hmmm. Strictly speaking we
3164 * should return `the size of the
3165 * screen in characters', but
3166 * that's not easy: (a) window
3167 * furniture being what it is it's
3168 * hard to compute, and (b) in
3169 * resize-font mode maximising the
3170 * window wouldn't change the
3171 * number of characters. *shrug*. I
3172 * think we'll ignore it for the
3173 * moment and see if anyone
3174 * complains, and then ask them
3175 * what they would like it to do.
3180 !term->cfg.no_remote_qtitle) {
3181 p = get_window_title(term->frontend, TRUE);
3183 ldisc_send(term->ldisc, "\033]L", 3, 0);
3184 ldisc_send(term->ldisc, p, len, 0);
3185 ldisc_send(term->ldisc, "\033\\", 2, 0);
3190 !term->cfg.no_remote_qtitle) {
3191 p = get_window_title(term->frontend,FALSE);
3193 ldisc_send(term->ldisc, "\033]l", 3, 0);
3194 ldisc_send(term->ldisc, p, len, 0);
3195 ldisc_send(term->ldisc, "\033\\", 2, 0);
3201 case 'S': /* SU: Scroll up */
3202 compatibility(SCOANSI);
3203 scroll(term, term->marg_t, term->marg_b,
3204 def(term->esc_args[0], 1), TRUE);
3206 term->wrapnext = FALSE;
3207 term->seen_disp_event = TRUE;
3209 case 'T': /* SD: Scroll down */
3210 compatibility(SCOANSI);
3211 scroll(term, term->marg_t, term->marg_b,
3212 -def(term->esc_args[0], 1), TRUE);
3214 term->wrapnext = FALSE;
3215 term->seen_disp_event = TRUE;
3217 case ANSI('|', '*'): /* DECSNLS */
3219 * Set number of lines on screen
3220 * VT420 uses VGA like hardware and can
3221 * support any size in reasonable range
3222 * (24..49 AIUI) with no default specified.
3224 compatibility(VT420);
3225 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
3226 if (!term->cfg.no_remote_resize)
3227 request_resize(term->frontend, term->cols,
3228 def(term->esc_args[0],
3233 case ANSI('|', '$'): /* DECSCPP */
3235 * Set number of columns per page
3236 * Docs imply range is only 80 or 132, but
3239 compatibility(VT340TEXT);
3240 if (term->esc_nargs <= 1) {
3241 if (!term->cfg.no_remote_resize)
3242 request_resize(term->frontend,
3243 def(term->esc_args[0],
3244 term->cfg.width), term->rows);
3248 case 'X': /* ECH: write N spaces w/o moving cursor */
3249 /* XXX VTTEST says this is vt220, vt510 manual
3251 compatibility(ANSIMIN);
3253 int n = def(term->esc_args[0], 1);
3255 termchar *p = term->cpos;
3256 if (n > term->cols - term->curs.x)
3257 n = term->cols - term->curs.x;
3258 cursplus = term->curs;
3260 check_boundary(term, term->curs.x, term->curs.y);
3261 check_boundary(term, term->curs.x+n, term->curs.y);
3262 check_selection(term, term->curs, cursplus);
3264 *p++ = term->erase_char;
3265 term->seen_disp_event = TRUE;
3268 case 'x': /* DECREQTPARM: report terminal characteristics */
3269 compatibility(VT100);
3272 int i = def(term->esc_args[0], 0);
3273 if (i == 0 || i == 1) {
3274 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3276 ldisc_send(term->ldisc, buf, 20, 0);
3280 case 'Z': /* CBT: BackTab for xterm */
3281 compatibility(OTHER);
3283 int i = def(term->esc_args[0], 1);
3284 pos old_curs = term->curs;
3286 for(;i>0 && term->curs.x>0; i--) {
3289 } while (term->curs.x >0 &&
3290 !term->tabs[term->curs.x]);
3293 check_selection(term, old_curs, term->curs);
3296 case ANSI('c', '='): /* Hide or Show Cursor */
3297 compatibility(SCOANSI);
3298 switch(term->esc_args[0]) {
3299 case 0: /* hide cursor */
3300 term->cursor_on = FALSE;
3302 case 1: /* restore cursor */
3303 term->big_cursor = FALSE;
3304 term->cursor_on = TRUE;
3306 case 2: /* block cursor */
3307 term->big_cursor = TRUE;
3308 term->cursor_on = TRUE;
3312 case ANSI('C', '='):
3314 * set cursor start on scanline esc_args[0] and
3315 * end on scanline esc_args[1].If you set
3316 * the bottom scan line to a value less than
3317 * the top scan line, the cursor will disappear.
3319 compatibility(SCOANSI);
3320 if (term->esc_nargs >= 2) {
3321 if (term->esc_args[0] > term->esc_args[1])
3322 term->cursor_on = FALSE;
3324 term->cursor_on = TRUE;
3327 case ANSI('D', '='):
3328 compatibility(SCOANSI);
3329 term->blink_is_real = FALSE;
3330 if (term->esc_args[0]>=1)
3331 term->curr_attr |= ATTR_BLINK;
3333 term->curr_attr &= ~ATTR_BLINK;
3335 case ANSI('E', '='):
3336 compatibility(SCOANSI);
3337 term->blink_is_real = (term->esc_args[0] >= 1);
3339 case ANSI('F', '='): /* set normal foreground */
3340 compatibility(SCOANSI);
3341 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3343 (sco2ansicolour[term->esc_args[0] & 0x7] |
3344 ((term->esc_args[0] & 0x8) << 1)) <<
3346 term->curr_attr &= ~ATTR_FGMASK;
3347 term->curr_attr |= colour;
3348 term->default_attr &= ~ATTR_FGMASK;
3349 term->default_attr |= colour;
3352 case ANSI('G', '='): /* set normal background */
3353 compatibility(SCOANSI);
3354 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3356 (sco2ansicolour[term->esc_args[0] & 0x7] |
3357 ((term->esc_args[0] & 0x8) << 1)) <<
3359 term->curr_attr &= ~ATTR_BGMASK;
3360 term->curr_attr |= colour;
3361 term->default_attr &= ~ATTR_BGMASK;
3362 term->default_attr |= colour;
3365 case ANSI('L', '='):
3366 compatibility(SCOANSI);
3367 term->use_bce = (term->esc_args[0] <= 0);
3368 set_erase_char(term);
3370 case ANSI('p', '"'): /* DECSCL: set compat level */
3372 * Allow the host to make this emulator a
3373 * 'perfect' VT102. This first appeared in
3374 * the VT220, but we do need to get back to
3375 * PuTTY mode so I won't check it.
3377 * The arg in 40..42,50 are a PuTTY extension.
3378 * The 2nd arg, 8bit vs 7bit is not checked.
3380 * Setting VT102 mode should also change
3381 * the Fkeys to generate PF* codes as a
3382 * real VT102 has no Fkeys. The VT220 does
3383 * this, F11..F13 become ESC,BS,LF other
3384 * Fkeys send nothing.
3386 * Note ESC c will NOT change this!
3389 switch (term->esc_args[0]) {
3391 term->compatibility_level &= ~TM_VTXXX;
3392 term->compatibility_level |= TM_VT102;
3395 term->compatibility_level &= ~TM_VTXXX;
3396 term->compatibility_level |= TM_VT220;
3400 if (term->esc_args[0] > 60 &&
3401 term->esc_args[0] < 70)
3402 term->compatibility_level |= TM_VTXXX;
3406 term->compatibility_level &= TM_VTXXX;
3409 term->compatibility_level = TM_PUTTY;
3412 term->compatibility_level = TM_SCOANSI;
3416 term->compatibility_level = TM_PUTTY;
3422 /* Change the response to CSI c */
3423 if (term->esc_args[0] == 50) {
3426 strcpy(term->id_string, "\033[?");
3427 for (i = 1; i < term->esc_nargs; i++) {
3429 strcat(term->id_string, ";");
3430 sprintf(lbuf, "%d", term->esc_args[i]);
3431 strcat(term->id_string, lbuf);
3433 strcat(term->id_string, "c");
3436 /* Is this a good idea ?
3437 * Well we should do a soft reset at this point ...
3439 if (!has_compat(VT420) && has_compat(VT100)) {
3440 if (!term->cfg.no_remote_resize) {
3441 if (term->reset_132)
3442 request_resize(132, 24);
3444 request_resize(80, 24);
3452 term->osc_w = FALSE;
3454 case 'P': /* Linux palette sequence */
3455 term->termstate = SEEN_OSC_P;
3456 term->osc_strlen = 0;
3458 case 'R': /* Linux palette reset */
3459 palette_reset(term->frontend);
3460 term_invalidate(term);
3461 term->termstate = TOPLEVEL;
3463 case 'W': /* word-set */
3464 term->termstate = SEEN_OSC_W;
3477 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3481 * Grotty hack to support xterm and DECterm title
3482 * sequences concurrently.
3484 if (term->esc_args[0] == 2) {
3485 term->esc_args[0] = 1;
3488 /* else fall through */
3490 term->termstate = OSC_STRING;
3491 term->osc_strlen = 0;
3496 * This OSC stuff is EVIL. It takes just one character to get into
3497 * sysline mode and it's not initially obvious how to get out.
3498 * So I've added CR and LF as string aborts.
3499 * This shouldn't effect compatibility as I believe embedded
3500 * control characters are supposed to be interpreted (maybe?)
3501 * and they don't display anything useful anyway.
3505 if (c == '\012' || c == '\015') {
3506 term->termstate = TOPLEVEL;
3507 } else if (c == 0234 || c == '\007') {
3509 * These characters terminate the string; ST and BEL
3510 * terminate the sequence and trigger instant
3511 * processing of it, whereas ESC goes back to SEEN_ESC
3512 * mode unless it is followed by \, in which case it is
3513 * synonymous with ST in the first place.
3516 term->termstate = TOPLEVEL;
3517 } else if (c == '\033')
3518 term->termstate = OSC_MAYBE_ST;
3519 else if (term->osc_strlen < OSC_STR_MAX)
3520 term->osc_string[term->osc_strlen++] = (char)c;
3524 int max = (term->osc_strlen == 0 ? 21 : 16);
3526 if ((int)c >= '0' && (int)c <= '9')
3528 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
3530 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
3533 term->termstate = TOPLEVEL;
3536 term->osc_string[term->osc_strlen++] = val;
3537 if (term->osc_strlen >= 7) {
3538 palette_set(term->frontend, term->osc_string[0],
3539 term->osc_string[1] * 16 + term->osc_string[2],
3540 term->osc_string[3] * 16 + term->osc_string[4],
3541 term->osc_string[5] * 16 + term->osc_string[6]);
3542 term_invalidate(term);
3543 term->termstate = TOPLEVEL;
3559 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3562 term->termstate = OSC_STRING;
3563 term->osc_strlen = 0;
3567 term->termstate = TOPLEVEL;
3568 term->seen_disp_event = TRUE;
3571 move(term, term->curs.x, term->curs.y - 1, 1);
3574 move(term, term->curs.x, term->curs.y + 1, 1);
3577 move(term, term->curs.x + 1, term->curs.y, 1);
3580 move(term, term->curs.x - 1, term->curs.y, 1);
3583 * From the VT100 Manual
3584 * NOTE: The special graphics characters in the VT100
3585 * are different from those in the VT52
3587 * From VT102 manual:
3588 * 137 _ Blank - Same
3589 * 140 ` Reserved - Humm.
3590 * 141 a Solid rectangle - Similar
3591 * 142 b 1/ - Top half of fraction for the
3592 * 143 c 3/ - subscript numbers below.
3595 * 146 f Degrees - Same
3596 * 147 g Plus or minus - Same
3598 * 151 i Ellipsis (dots)
3601 * 154 l Bar at scan 0
3602 * 155 m Bar at scan 1
3603 * 156 n Bar at scan 2
3604 * 157 o Bar at scan 3 - Similar
3605 * 160 p Bar at scan 4 - Similar
3606 * 161 q Bar at scan 5 - Similar
3607 * 162 r Bar at scan 6 - Same
3608 * 163 s Bar at scan 7 - Similar
3623 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
3626 term->cset_attr[term->cset = 0] = CSET_ASCII;
3629 move(term, 0, 0, 0);
3632 if (term->curs.y == 0)
3633 scroll(term, 0, term->rows - 1, -1, TRUE);
3634 else if (term->curs.y > 0)
3637 term->wrapnext = FALSE;
3640 erase_lots(term, FALSE, FALSE, TRUE);
3644 erase_lots(term, TRUE, FALSE, TRUE);
3648 /* XXX Print cursor line */
3651 /* XXX Start controller mode */
3654 /* XXX Stop controller mode */
3658 term->termstate = VT52_Y1;
3662 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3665 term->app_keypad_keys = TRUE;
3668 term->app_keypad_keys = FALSE;
3671 /* XXX This should switch to VT100 mode not current or default
3672 * VT mode. But this will only have effect in a VT220+
3675 term->vt52_mode = FALSE;
3676 term->blink_is_real = term->cfg.blinktext;
3680 /* XXX Enter auto print mode */
3683 /* XXX Exit auto print mode */
3686 /* XXX Print screen */
3692 /* compatibility(ATARI) */
3693 move(term, 0, 0, 0);
3694 erase_lots(term, FALSE, FALSE, TRUE);
3698 /* compatibility(ATARI) */
3699 if (term->curs.y <= term->marg_b)
3700 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3703 /* compatibility(ATARI) */
3704 if (term->curs.y <= term->marg_b)
3705 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3708 /* compatibility(ATARI) */
3709 term->termstate = VT52_FG;
3712 /* compatibility(ATARI) */
3713 term->termstate = VT52_BG;
3716 /* compatibility(ATARI) */
3717 erase_lots(term, FALSE, TRUE, FALSE);
3721 /* compatibility(ATARI) */
3722 term->cursor_on = TRUE;
3725 /* compatibility(ATARI) */
3726 term->cursor_on = FALSE;
3728 /* case 'j': Save cursor position - broken on ST */
3729 /* case 'k': Restore cursor position */
3731 /* compatibility(ATARI) */
3732 erase_lots(term, TRUE, TRUE, TRUE);
3734 term->wrapnext = FALSE;
3738 /* compatibility(ATARI) */
3739 erase_lots(term, TRUE, TRUE, FALSE);
3742 /* compatibility(ATARI) */
3743 term->curr_attr |= ATTR_REVERSE;
3746 /* compatibility(ATARI) */
3747 term->curr_attr &= ~ATTR_REVERSE;
3749 case 'v': /* wrap Autowrap on - Wyse style */
3750 /* compatibility(ATARI) */
3753 case 'w': /* Autowrap off */
3754 /* compatibility(ATARI) */
3759 /* compatibility(OTHER) */
3760 term->vt52_bold = FALSE;
3761 term->curr_attr = ATTR_DEFAULT;
3762 set_erase_char(term);
3765 /* compatibility(VI50) */
3766 term->curr_attr |= ATTR_UNDER;
3769 /* compatibility(VI50) */
3770 term->curr_attr &= ~ATTR_UNDER;
3773 /* compatibility(VI50) */
3774 term->vt52_bold = TRUE;
3775 term->curr_attr |= ATTR_BOLD;
3778 /* compatibility(VI50) */
3779 term->vt52_bold = FALSE;
3780 term->curr_attr &= ~ATTR_BOLD;
3786 term->termstate = VT52_Y2;
3787 move(term, term->curs.x, c - ' ', 0);
3790 term->termstate = TOPLEVEL;
3791 move(term, c - ' ', term->curs.y, 0);
3796 term->termstate = TOPLEVEL;
3797 term->curr_attr &= ~ATTR_FGMASK;
3798 term->curr_attr &= ~ATTR_BOLD;
3799 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3800 if ((c & 0x8) || term->vt52_bold)
3801 term->curr_attr |= ATTR_BOLD;
3803 set_erase_char(term);
3806 term->termstate = TOPLEVEL;
3807 term->curr_attr &= ~ATTR_BGMASK;
3808 term->curr_attr &= ~ATTR_BLINK;
3809 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3811 /* Note: bold background */
3813 term->curr_attr |= ATTR_BLINK;
3815 set_erase_char(term);
3818 default: break; /* placate gcc warning about enum use */
3820 if (term->selstate != NO_SELECTION) {
3821 pos cursplus = term->curs;
3823 check_selection(term, term->curs, cursplus);
3827 term_print_flush(term);
3828 logflush(term->logctx);
3832 * To prevent having to run the reasonably tricky bidi algorithm
3833 * too many times, we maintain a cache of the last lineful of data
3834 * fed to the algorithm on each line of the display.
3836 static int term_bidi_cache_hit(Terminal *term, int line,
3837 termchar *lbefore, int width)
3839 if (!term->pre_bidi_cache)
3840 return FALSE; /* cache doesn't even exist yet! */
3842 if (line >= term->bidi_cache_size)
3843 return FALSE; /* cache doesn't have this many lines */
3845 if (!term->pre_bidi_cache[line])
3846 return FALSE; /* cache doesn't contain _this_ line */
3848 if (!memcmp(term->pre_bidi_cache[line], lbefore, width * TSIZE))
3849 return TRUE; /* aha! the line matches the cache */
3851 return FALSE; /* it didn't match. */
3854 static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
3855 termchar *lafter, int width)
3857 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
3858 int j = term->bidi_cache_size;
3859 term->bidi_cache_size = line+1;
3860 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
3861 term->bidi_cache_size,
3863 term->post_bidi_cache = sresize(term->post_bidi_cache,
3864 term->bidi_cache_size,
3866 while (j < term->bidi_cache_size) {
3867 term->pre_bidi_cache[j] = term->post_bidi_cache[j] = NULL;
3872 sfree(term->pre_bidi_cache[line]);
3873 sfree(term->post_bidi_cache[line]);
3875 term->pre_bidi_cache[line] = snewn(width, termchar);
3876 term->post_bidi_cache[line] = snewn(width, termchar);
3878 memcpy(term->pre_bidi_cache[line], lbefore, width * TSIZE);
3879 memcpy(term->post_bidi_cache[line], lafter, width * TSIZE);
3883 * Given a context, update the window. Out of paranoia, we don't
3884 * allow WM_PAINT responses to do scrolling optimisations.
3886 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3888 int i, it, j, our_curs_y, our_curs_x;
3892 termchar cursor_background;
3893 unsigned long ticks;
3894 #ifdef OPTIMISE_SCROLL
3895 struct scrollregion *sr;
3896 #endif /* OPTIMISE_SCROLL */
3898 cursor_background = term->basic_erase_char;
3901 * Check the visual bell state.
3903 if (term->in_vbell) {
3904 ticks = GETTICKCOUNT();
3905 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3906 term->in_vbell = FALSE;
3909 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3912 * screen array, disptop, scrtop,
3914 * cfg.blinkpc, blink_is_real, tblinker,
3915 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3918 /* Has the cursor position or type changed ? */
3919 if (term->cursor_on) {
3920 if (term->has_focus) {
3921 if (term->blinker || !term->cfg.blink_cur)
3922 cursor = TATTR_ACTCURS;
3926 cursor = TATTR_PASCURS;
3928 cursor |= TATTR_RIGHTCURS;
3931 our_curs_y = term->curs.y - term->disptop;
3934 * Adjust the cursor position in the case where it's
3935 * resting on the right-hand half of a CJK wide character.
3936 * xterm's behaviour here, which seems adequate to me, is
3937 * to display the cursor covering the _whole_ character,
3938 * exactly as if it were one space to the left.
3940 termline *ldata = lineptr(term->curs.y);
3941 our_curs_x = term->curs.x;
3942 if (our_curs_x > 0 &&
3943 ldata->chars[our_curs_x].chr == UCSWIDE)
3949 * If the cursor is not where it was last time we painted, and
3950 * its previous position is visible on screen, invalidate its
3951 * previous position.
3953 if (term->dispcursy >= 0 &&
3954 (term->curstype != cursor ||
3955 term->dispcursy != our_curs_y ||
3956 term->dispcursx != our_curs_x)) {
3957 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
3960 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
3961 dispcurs[-1].attr |= ATTR_INVALID;
3962 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
3963 dispcurs[1].attr |= ATTR_INVALID;
3964 dispcurs->attr |= ATTR_INVALID;
3968 term->dispcursx = term->dispcursy = -1;
3970 #ifdef OPTIMISE_SCROLL
3972 sr = term->scrollhead;
3974 struct scrollregion *next = sr->next;
3975 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3979 term->scrollhead = term->scrolltail = NULL;
3980 #endif /* OPTIMISE_SCROLL */
3982 /* The normal screen data */
3983 for (i = 0; i < term->rows; i++) {
3986 int dirty_line, dirty_run, selected;
3987 unsigned long attr = 0, cset = 0;
3988 int updated_line = 0;
3991 int last_run_dirty = 0;
3993 scrpos.y = i + term->disptop;
3994 ldata = lineptr(scrpos.y);
3996 dirty_run = dirty_line = (ldata->lattr !=
3997 term->disptext[i]->lattr);
3998 term->disptext[i]->lattr = ldata->lattr;
4000 /* Do Arabic shaping and bidi. */
4001 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4003 if (!term_bidi_cache_hit(term, i, ldata->chars, term->cols)) {
4005 for(it=0; it<term->cols ; it++)
4007 unsigned long uc = (ldata->chars[it].chr);
4009 switch (uc & CSET_MASK) {
4011 if (!term->cfg.rawcnp) {
4012 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4016 uc = term->ucsdata->unitab_line[uc & 0xFF];
4019 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4022 switch (uc & CSET_MASK) {
4024 uc = term->ucsdata->unitab_font[uc & 0xFF];
4027 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4031 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4033 term->wcFrom[it].index = it;
4037 do_bidi(term->wcFrom, term->cols);
4039 /* this is saved iff done from inside the shaping */
4040 if(!term->cfg.bidi && term->cfg.arabicshaping)
4041 for(it=0; it<term->cols; it++)
4042 term->wcTo[it] = term->wcFrom[it];
4044 if(!term->cfg.arabicshaping)
4045 do_shape(term->wcFrom, term->wcTo, term->cols);
4047 for(it=0; it<term->cols ; it++)
4049 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4051 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4052 term->ltemp[it].chr = term->wcTo[it].wc;
4054 term_bidi_cache_store(term, i, ldata->chars,
4055 term->ltemp, term->cols);
4056 lchars = term->ltemp;
4058 lchars = term->post_bidi_cache[i];
4061 lchars = ldata->chars;
4063 for (j = 0; j < term->cols; j++) {
4064 unsigned long tattr, tchar;
4065 termchar *d = lchars + j;
4072 switch (tchar & CSET_MASK) {
4074 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4077 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4080 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
4083 if (j < term->cols-1 && d[1].chr == UCSWIDE)
4086 /* Video reversing things */
4087 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4088 if (term->seltype == LEXICOGRAPHIC)
4089 selected = (posle(term->selstart, scrpos) &&
4090 poslt(scrpos, term->selend));
4092 selected = (posPle(term->selstart, scrpos) &&
4093 posPlt(scrpos, term->selend));
4097 ^ (selected ? ATTR_REVERSE : 0));
4099 /* 'Real' blinking ? */
4100 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4101 if (term->has_focus && term->tblinker) {
4102 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
4104 tattr &= ~ATTR_BLINK;
4108 * Check the font we'll _probably_ be using to see if
4109 * the character is wide when we don't want it to be.
4111 if (tchar != term->disptext[i]->chars[j].chr ||
4112 tattr != (term->disptext[i]->chars[j].attr &~
4114 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
4115 tattr |= ATTR_NARROW;
4116 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
4117 tattr |= ATTR_NARROW;
4119 /* Cursor here ? Save the 'background' */
4120 if (i == our_curs_y && j == our_curs_x) {
4121 cursor_background.chr = tchar;
4122 cursor_background.attr = tattr;
4123 term->dispcursx = j;
4124 term->dispcursy = i;
4127 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4130 break_run = (((tattr ^ attr) & term->attr_mask) ||
4131 j - start >= sizeof(ch));
4133 /* Special hack for VT100 Linedraw glyphs */
4134 if (tchar >= 0x23BA && tchar <= 0x23BD)
4138 * Separate out sequences of characters that have the
4139 * same CSET, if that CSET is a magic one.
4141 if (CSET_OF(tchar) != cset)
4144 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
4145 if (term->disptext[i]->chars[j].chr == tchar &&
4146 term->disptext[i]->chars[j].attr == tattr)
4148 else if (!dirty_run && ccount == 1)
4153 if ((dirty_run || last_run_dirty) && ccount > 0) {
4154 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4160 cset = CSET_OF(tchar);
4161 if (term->ucsdata->dbcs_screenfont)
4162 last_run_dirty = dirty_run;
4163 dirty_run = dirty_line;
4166 if (term->disptext[i]->chars[j].chr != tchar ||
4167 term->disptext[i]->chars[j].attr != tattr)
4169 ch[ccount++] = (wchar_t) tchar;
4170 term->disptext[i]->chars[j].chr = tchar;
4171 term->disptext[i]->chars[j].attr = tattr;
4173 /* If it's a wide char step along to the next one. */
4174 if (tattr & ATTR_WIDE) {
4175 if (++j < term->cols) {
4178 * By construction above, the cursor should not
4179 * be on the right-hand half of this character.
4182 assert(!(i == our_curs_y && j == our_curs_x));
4183 if (memcmp(&term->disptext[i]->chars[j],
4186 term->disptext[i]->chars[j] = *d;
4190 if (dirty_run && ccount > 0) {
4191 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4195 /* Cursor on this line ? (and changed) */
4196 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
4197 ch[0] = (wchar_t) cursor_background.chr;
4198 attr = cursor_background.attr | cursor;
4199 do_cursor(ctx, our_curs_x, i, ch, 1, attr, ldata->lattr);
4200 term->curstype = cursor;
4208 * Flick the switch that says if blinking things should be shown or hidden.
4211 void term_blink(Terminal *term, int flg)
4213 long now, blink_diff;
4215 now = GETTICKCOUNT();
4216 blink_diff = now - term->last_tblink;
4218 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4219 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
4220 term->last_tblink = now;
4221 term->tblinker = !term->tblinker;
4226 term->last_blink = now;
4230 blink_diff = now - term->last_blink;
4232 /* Make sure the cursor blinks no faster than system blink rate */
4233 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
4236 term->last_blink = now;
4237 term->blinker = !term->blinker;
4241 * Invalidate the whole screen so it will be repainted in full.
4243 void term_invalidate(Terminal *term)
4247 for (i = 0; i < term->rows; i++)
4248 for (j = 0; j < term->cols; j++)
4249 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4253 * Paint the window in response to a WM_PAINT message.
4255 void term_paint(Terminal *term, Context ctx,
4256 int left, int top, int right, int bottom, int immediately)
4259 if (left < 0) left = 0;
4260 if (top < 0) top = 0;
4261 if (right >= term->cols) right = term->cols-1;
4262 if (bottom >= term->rows) bottom = term->rows-1;
4264 for (i = top; i <= bottom && i < term->rows; i++) {
4265 if (term->disptext[i]->lattr == LATTR_NORM)
4266 for (j = left; j <= right && j < term->cols; j++)
4267 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4269 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
4270 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4273 /* This should happen soon enough, also for some reason it sometimes
4274 * fails to actually do anything when re-sizing ... painting the wrong
4278 do_paint (term, ctx, FALSE);
4282 * Attempt to scroll the scrollback. The second parameter gives the
4283 * position we want to scroll to; the first is +1 to denote that
4284 * this position is relative to the beginning of the scrollback, -1
4285 * to denote it is relative to the end, and 0 to denote that it is
4286 * relative to the current position.
4288 void term_scroll(Terminal *term, int rel, int where)
4290 int sbtop = -sblines(term);
4291 #ifdef OPTIMISE_SCROLL
4292 int olddisptop = term->disptop;
4294 #endif /* OPTIMISE_SCROLL */
4296 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4297 if (term->disptop < sbtop)
4298 term->disptop = sbtop;
4299 if (term->disptop > 0)
4302 #ifdef OPTIMISE_SCROLL
4303 shift = (term->disptop - olddisptop);
4304 if (shift < term->rows && shift > -term->rows)
4305 scroll_display(term, 0, term->rows - 1, shift);
4306 #endif /* OPTIMISE_SCROLL */
4310 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
4313 wchar_t *wbptr; /* where next char goes within workbuf */
4315 int wblen = 0; /* workbuf len */
4316 int buflen; /* amount of memory allocated to workbuf */
4318 buflen = 5120; /* Default size */
4319 workbuf = snewn(buflen, wchar_t);
4320 wbptr = workbuf; /* start filling here */
4321 old_top_x = top.x; /* needed for rect==1 */
4323 while (poslt(top, bottom)) {
4325 termline *ldata = lineptr(top.y);
4329 * nlpos will point at the maximum position on this line we
4330 * should copy up to. So we start it at the end of the
4334 nlpos.x = term->cols;
4337 * ... move it backwards if there's unused space at the end
4338 * of the line (and also set `nl' if this is the case,
4339 * because in normal selection mode this means we need a
4340 * newline at the end)...
4342 if (!(ldata->lattr & LATTR_WRAPPED)) {
4343 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
4346 if (poslt(nlpos, bottom))
4348 } else if (ldata->lattr & LATTR_WRAPPED2) {
4349 /* Ignore the last char on the line in a WRAPPED2 line. */
4354 * ... and then clip it to the terminal x coordinate if
4355 * we're doing rectangular selection. (In this case we
4356 * still did the above, so that copying e.g. the right-hand
4357 * column from a table doesn't fill with spaces on the
4361 if (nlpos.x > bottom.x)
4363 nl = (top.y < bottom.y);
4366 while (poslt(top, bottom) && poslt(top, nlpos)) {
4369 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4371 wchar_t cbuf[16], *p;
4372 int uc = ldata->chars[top.x].chr;
4375 if (uc == UCSWIDE) {
4380 switch (uc & CSET_MASK) {
4382 if (!term->cfg.rawcnp) {
4383 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4387 uc = term->ucsdata->unitab_line[uc & 0xFF];
4390 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4393 switch (uc & CSET_MASK) {
4395 uc = term->ucsdata->unitab_font[uc & 0xFF];
4398 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4402 set = (uc & CSET_MASK);
4403 c = (uc & ~CSET_MASK);
4407 if (DIRECT_FONT(uc)) {
4408 if (c >= ' ' && c != 0x7F) {
4412 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4414 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
4415 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4419 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4423 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
4430 for (p = cbuf; *p; p++) {
4431 /* Enough overhead for trailing NL and nul */
4432 if (wblen >= buflen - 16) {
4434 workbuf = sresize(workbuf, buflen, wchar_t);
4435 wbptr = workbuf + wblen;
4444 for (i = 0; i < sel_nl_sz; i++) {
4446 *wbptr++ = sel_nl[i];
4450 top.x = rect ? old_top_x : 0;
4454 #if SELECTION_NUL_TERMINATED
4458 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
4459 if (buflen > 0) /* indicates we allocated this buffer */
4463 void term_copyall(Terminal *term)
4467 tree234 *screen = term->screen;
4468 top.y = -sblines(term);
4470 bottom.y = find_last_nonempty_line(term, screen);
4471 bottom.x = term->cols;
4472 clipme(term, top, bottom, 0, TRUE);
4476 * The wordness array is mainly for deciding the disposition of the
4477 * US-ASCII characters.
4479 static int wordtype(Terminal *term, int uc)
4482 int start, end, ctype;
4484 static const struct ucsword ucs_words[] = {
4490 0x037e, 0x037e, 1}, /* Greek question mark */
4492 0x0387, 0x0387, 1}, /* Greek ano teleia */
4494 0x055a, 0x055f, 1}, /* Armenian punctuation */
4496 0x0589, 0x0589, 1}, /* Armenian full stop */
4498 0x0700, 0x070d, 1}, /* Syriac punctuation */
4500 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4502 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4504 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4506 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4508 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4510 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4512 0x2000, 0x200a, 0}, /* Various spaces */
4514 0x2070, 0x207f, 2}, /* superscript */
4516 0x2080, 0x208f, 2}, /* subscript */
4518 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4520 0x3000, 0x3000, 0}, /* ideographic space */
4522 0x3001, 0x3020, 1}, /* ideographic punctuation */
4524 0x303f, 0x309f, 3}, /* Hiragana */
4526 0x30a0, 0x30ff, 3}, /* Katakana */
4528 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4530 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4532 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4534 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4536 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4538 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4540 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4542 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4544 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4548 const struct ucsword *wptr;
4550 switch (uc & CSET_MASK) {
4552 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4555 uc = term->ucsdata->unitab_line[uc & 0xFF];
4558 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4561 switch (uc & CSET_MASK) {
4563 uc = term->ucsdata->unitab_font[uc & 0xFF];
4566 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4570 /* For DBCS fonts I can't do anything useful. Even this will sometimes
4571 * fail as there's such a thing as a double width space. :-(
4573 if (term->ucsdata->dbcs_screenfont &&
4574 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
4578 return term->wordness[uc];
4580 for (wptr = ucs_words; wptr->start; wptr++) {
4581 if (uc >= wptr->start && uc <= wptr->end)
4589 * Spread the selection outwards according to the selection mode.
4591 static pos sel_spread_half(Terminal *term, pos p, int dir)
4595 int topy = -sblines(term);
4597 ldata = lineptr(p.y);
4599 switch (term->selmode) {
4602 * In this mode, every character is a separate unit, except
4603 * for runs of spaces at the end of a non-wrapping line.
4605 if (!(ldata->lattr & LATTR_WRAPPED)) {
4606 termchar *q = ldata->chars + term->cols;
4607 while (q > ldata->chars && IS_SPACE_CHR(q[-1].chr))
4609 if (q == ldata->chars + term->cols)
4611 if (p.x >= q - ldata->chars)
4612 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
4617 * In this mode, the units are maximal runs of characters
4618 * whose `wordness' has the same value.
4620 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
4623 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
4624 term->cols-1 : term->cols);
4625 if (p.x < maxcols-1) {
4626 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
4631 if (ldata->lattr & LATTR_WRAPPED) {
4633 ldata2 = lineptr(p.y+1);
4634 if (wordtype(term, UCSGET(ldata2->chars, 0))
4651 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
4660 ldata2 = lineptr(p.y-1);
4661 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
4662 term->cols-1 : term->cols);
4663 if (ldata2->lattr & LATTR_WRAPPED) {
4664 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
4682 * In this mode, every line is a unit.
4684 p.x = (dir == -1 ? 0 : term->cols - 1);
4692 static void sel_spread(Terminal *term)
4694 if (term->seltype == LEXICOGRAPHIC) {
4695 term->selstart = sel_spread_half(term, term->selstart, -1);
4696 decpos(term->selend);
4697 term->selend = sel_spread_half(term, term->selend, +1);
4698 incpos(term->selend);
4702 void term_do_paste(Terminal *term)
4707 get_clip(term->frontend, &data, &len);
4708 if (data && len > 0) {
4711 term_seen_key_event(term); /* pasted data counts */
4713 if (term->paste_buffer)
4714 sfree(term->paste_buffer);
4715 term->paste_pos = term->paste_hold = term->paste_len = 0;
4716 term->paste_buffer = snewn(len, wchar_t);
4719 while (p < data + len) {
4720 while (p < data + len &&
4721 !(p <= data + len - sel_nl_sz &&
4722 !memcmp(p, sel_nl, sizeof(sel_nl))))
4727 for (i = 0; i < p - q; i++) {
4728 term->paste_buffer[term->paste_len++] = q[i];
4732 if (p <= data + len - sel_nl_sz &&
4733 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4734 term->paste_buffer[term->paste_len++] = '\015';
4740 /* Assume a small paste will be OK in one go. */
4741 if (term->paste_len < 256) {
4743 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4744 if (term->paste_buffer)
4745 sfree(term->paste_buffer);
4746 term->paste_buffer = 0;
4747 term->paste_pos = term->paste_hold = term->paste_len = 0;
4750 get_clip(term->frontend, NULL, NULL);
4753 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4754 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4758 int raw_mouse = (term->xterm_mouse &&
4759 !term->cfg.no_mouse_rep &&
4760 !(term->cfg.mouse_override && shift));
4761 int default_seltype;
4765 if (a == MA_DRAG && !raw_mouse)
4766 term_scroll(term, 0, -1);
4768 if (y >= term->rows) {
4770 if (a == MA_DRAG && !raw_mouse)
4771 term_scroll(term, 0, +1);
4780 if (x >= term->cols)
4783 selpoint.y = y + term->disptop;
4785 ldata = lineptr(selpoint.y);
4786 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
4791 int encstate = 0, r, c;
4798 encstate = 0x20; /* left button down */
4809 case MBT_WHEEL_DOWN:
4812 default: break; /* placate gcc warning about enum use */
4816 if (term->xterm_mouse == 1)
4822 term->mouse_is_down = 0;
4825 if (term->mouse_is_down == braw)
4827 term->mouse_is_down = braw;
4829 default: break; /* placate gcc warning about enum use */
4838 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4839 ldisc_send(term->ldisc, abuf, 6, 0);
4845 * Set the selection type (rectangular or normal) at the start
4846 * of a selection attempt, from the state of Alt.
4848 if (!alt ^ !term->cfg.rect_select)
4849 default_seltype = RECTANGULAR;
4851 default_seltype = LEXICOGRAPHIC;
4853 if (term->selstate == NO_SELECTION) {
4854 term->seltype = default_seltype;
4857 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4859 term->selstate = ABOUT_TO;
4860 term->seltype = default_seltype;
4861 term->selanchor = selpoint;
4862 term->selmode = SM_CHAR;
4863 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4865 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4866 term->selstate = DRAGGING;
4867 term->selstart = term->selanchor = selpoint;
4868 term->selend = term->selstart;
4869 incpos(term->selend);
4871 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4872 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4873 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4875 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4876 term->selstate == SELECTED) {
4877 if (term->seltype == LEXICOGRAPHIC) {
4879 * For normal selection, we extend by moving
4880 * whichever end of the current selection is closer
4883 if (posdiff(selpoint, term->selstart) <
4884 posdiff(term->selend, term->selstart) / 2) {
4885 term->selanchor = term->selend;
4886 decpos(term->selanchor);
4888 term->selanchor = term->selstart;
4892 * For rectangular selection, we have a choice of
4893 * _four_ places to put selanchor and selpoint: the
4894 * four corners of the selection.
4896 if (2*selpoint.x < term->selstart.x + term->selend.x)
4897 term->selanchor.x = term->selend.x-1;
4899 term->selanchor.x = term->selstart.x;
4901 if (2*selpoint.y < term->selstart.y + term->selend.y)
4902 term->selanchor.y = term->selend.y;
4904 term->selanchor.y = term->selstart.y;
4906 term->selstate = DRAGGING;
4908 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4909 term->selanchor = selpoint;
4910 term->selstate = DRAGGING;
4911 if (term->seltype == LEXICOGRAPHIC) {
4913 * For normal selection, we set (selstart,selend) to
4914 * (selpoint,selanchor) in some order.
4916 if (poslt(selpoint, term->selanchor)) {
4917 term->selstart = selpoint;
4918 term->selend = term->selanchor;
4919 incpos(term->selend);
4921 term->selstart = term->selanchor;
4922 term->selend = selpoint;
4923 incpos(term->selend);
4927 * For rectangular selection, we may need to
4928 * interchange x and y coordinates (if the user has
4929 * dragged in the -x and +y directions, or vice versa).
4931 term->selstart.x = min(term->selanchor.x, selpoint.x);
4932 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4933 term->selstart.y = min(term->selanchor.y, selpoint.y);
4934 term->selend.y = max(term->selanchor.y, selpoint.y);
4937 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4939 if (term->selstate == DRAGGING) {
4941 * We've completed a selection. We now transfer the
4942 * data to the clipboard.
4944 clipme(term, term->selstart, term->selend,
4945 (term->seltype == RECTANGULAR), FALSE);
4946 term->selstate = SELECTED;
4948 term->selstate = NO_SELECTION;
4949 } else if (bcooked == MBT_PASTE
4951 #if MULTICLICK_ONLY_EVENT
4952 || a == MA_2CLK || a == MA_3CLK
4955 request_paste(term->frontend);
4961 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4962 unsigned int modifiers, unsigned int flags)
4966 int prependesc = FALSE;
4970 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4971 for (i = 0; i < tlen; i++)
4972 fprintf(stderr, " %04x", (unsigned)text[i]);
4973 fprintf(stderr, "\n");
4977 if ((flags & PKF_REPEAT) && term->repeat_off)
4980 /* Currently, Meta always just prefixes everything with ESC. */
4981 if (modifiers & PKM_META)
4983 modifiers &= ~PKM_META;
4986 * Alt is only used for Alt+keypad, which isn't supported yet, so
4989 modifiers &= ~PKM_ALT;
4991 /* Standard local function keys */
4992 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4994 if (keysym == PK_PAGEUP)
4995 /* scroll up one page */;
4996 if (keysym == PK_PAGEDOWN)
4997 /* scroll down on page */;
4998 if (keysym == PK_INSERT)
4999 term_do_paste(term);
5002 if (keysym == PK_PAGEUP)
5003 /* scroll up one line */;
5004 if (keysym == PK_PAGEDOWN)
5005 /* scroll down one line */;
5006 /* Control-Numlock for app-keypad mode switch */
5007 if (keysym == PK_PF1)
5008 term->app_keypad_keys ^= 1;
5012 if (modifiers & PKM_ALT) {
5013 /* Alt+F4 (close) */
5014 /* Alt+Return (full screen) */
5015 /* Alt+Space (system menu) */
5018 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5019 text[0] >= 0x20 && text[0] <= 0x7e) {
5020 /* ASCII chars + Control */
5021 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5022 (text[0] >= 0x61 && text[0] <= 0x7a))
5026 * Control-2 should return ^@ (0x00), Control-6 should return
5027 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5028 * the DOS keyboard handling did it, and we have nothing better
5029 * to do with the key combo in question, we'll also map
5030 * Control-Backquote to ^\ (0x1C).
5033 case ' ': text[0] = 0x00; break;
5034 case '-': text[0] = 0x1f; break;
5035 case '/': text[0] = 0x1f; break;
5036 case '2': text[0] = 0x00; break;
5037 case '3': text[0] = 0x1b; break;
5038 case '4': text[0] = 0x1c; break;
5039 case '5': text[0] = 0x1d; break;
5040 case '6': text[0] = 0x1e; break;
5041 case '7': text[0] = 0x1f; break;
5042 case '8': text[0] = 0x7f; break;
5043 case '`': text[0] = 0x1c; break;
5048 /* Nethack keypad */
5049 if (term->cfg.nethack_keypad) {
5052 case PK_KP1: c = 'b'; break;
5053 case PK_KP2: c = 'j'; break;
5054 case PK_KP3: c = 'n'; break;
5055 case PK_KP4: c = 'h'; break;
5056 case PK_KP5: c = '.'; break;
5057 case PK_KP6: c = 'l'; break;
5058 case PK_KP7: c = 'y'; break;
5059 case PK_KP8: c = 'k'; break;
5060 case PK_KP9: c = 'u'; break;
5061 default: break; /* else gcc warns `enum value not used' */
5065 if (modifiers & PKM_CONTROL)
5067 else if (modifiers & PKM_SHIFT)
5075 /* Numeric Keypad */
5076 if (PK_ISKEYPAD(keysym)) {
5080 * In VT400 mode, PFn always emits an escape sequence. In
5081 * Linux and tilde modes, this only happens in app keypad mode.
5083 if (term->cfg.funky_type == FUNKY_VT400 ||
5084 ((term->cfg.funky_type == FUNKY_LINUX ||
5085 term->cfg.funky_type == FUNKY_TILDE) &&
5086 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5088 case PK_PF1: xkey = 'P'; break;
5089 case PK_PF2: xkey = 'Q'; break;
5090 case PK_PF3: xkey = 'R'; break;
5091 case PK_PF4: xkey = 'S'; break;
5092 default: break; /* else gcc warns `enum value not used' */
5095 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5097 case PK_KP0: xkey = 'p'; break;
5098 case PK_KP1: xkey = 'q'; break;
5099 case PK_KP2: xkey = 'r'; break;
5100 case PK_KP3: xkey = 's'; break;
5101 case PK_KP4: xkey = 't'; break;
5102 case PK_KP5: xkey = 'u'; break;
5103 case PK_KP6: xkey = 'v'; break;
5104 case PK_KP7: xkey = 'w'; break;
5105 case PK_KP8: xkey = 'x'; break;
5106 case PK_KP9: xkey = 'y'; break;
5107 case PK_KPDECIMAL: xkey = 'n'; break;
5108 case PK_KPENTER: xkey = 'M'; break;
5109 default: break; /* else gcc warns `enum value not used' */
5111 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5113 * xterm can't see the layout of the keypad, so it has
5114 * to rely on the X keysyms returned by the keys.
5115 * Hence, we look at the strings here, not the PuTTY
5116 * keysyms (which describe the layout).
5120 if (modifiers & PKM_SHIFT)
5125 case '/': xkey = 'o'; break;
5126 case '*': xkey = 'j'; break;
5127 case '-': xkey = 'm'; break;
5131 * In all other modes, we try to retain the layout of
5132 * the DEC keypad in application mode.
5136 /* This key covers the '-' and ',' keys on a VT220 */
5137 if (modifiers & PKM_SHIFT)
5138 xkey = 'm'; /* VT220 '-' */
5140 xkey = 'l'; /* VT220 ',' */
5142 case PK_KPMINUS: xkey = 'm'; break;
5143 case PK_KPCOMMA: xkey = 'l'; break;
5144 default: break; /* else gcc warns `enum value not used' */
5149 if (term->vt52_mode) {
5150 if (xkey >= 'P' && xkey <= 'S')
5151 p += sprintf((char *) p, "\x1B%c", xkey);
5153 p += sprintf((char *) p, "\x1B?%c", xkey);
5155 p += sprintf((char *) p, "\x1BO%c", xkey);
5158 /* Not in application mode -- treat the number pad as arrow keys? */
5159 if ((flags & PKF_NUMLOCK) == 0) {
5161 case PK_KP0: keysym = PK_INSERT; break;
5162 case PK_KP1: keysym = PK_END; break;
5163 case PK_KP2: keysym = PK_DOWN; break;
5164 case PK_KP3: keysym = PK_PAGEDOWN; break;
5165 case PK_KP4: keysym = PK_LEFT; break;
5166 case PK_KP5: keysym = PK_REST; break;
5167 case PK_KP6: keysym = PK_RIGHT; break;
5168 case PK_KP7: keysym = PK_HOME; break;
5169 case PK_KP8: keysym = PK_UP; break;
5170 case PK_KP9: keysym = PK_PAGEUP; break;
5171 default: break; /* else gcc warns `enum value not used' */
5176 /* Miscellaneous keys */
5183 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5184 else if (modifiers == PKM_SHIFT)
5185 /* We do the opposite of what is configured */
5186 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5192 else if (modifiers == PKM_SHIFT)
5193 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5196 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5198 if (modifiers == PKM_CONTROL)
5203 case PK_KPENTER: /* Odd keypad modes handled above */
5204 if (modifiers == 0) {
5206 if (term->cr_lf_return)
5210 default: break; /* else gcc warns `enum value not used' */
5213 /* SCO function keys and editing keys */
5214 if (term->cfg.funky_type == FUNKY_SCO) {
5215 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5216 static char const codes[] =
5217 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5218 int index = keysym - PK_F1;
5220 if (modifiers & PKM_SHIFT) index += 12;
5221 if (modifiers & PKM_CONTROL) index += 24;
5222 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5225 if (PK_ISEDITING(keysym)) {
5229 case PK_DELETE: *p++ = 0x7f; goto done;
5230 case PK_HOME: xkey = 'H'; break;
5231 case PK_INSERT: xkey = 'L'; break;
5232 case PK_END: xkey = 'F'; break;
5233 case PK_PAGEUP: xkey = 'I'; break;
5234 case PK_PAGEDOWN: xkey = 'G'; break;
5235 default: break; /* else gcc warns `enum value not used' */
5237 p += sprintf((char *) p, "\x1B[%c", xkey);
5241 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5244 if (term->cfg.funky_type == FUNKY_XTERM) {
5245 /* Xterm shuffles these keys, apparently. */
5247 case PK_HOME: keysym = PK_INSERT; break;
5248 case PK_INSERT: keysym = PK_HOME; break;
5249 case PK_DELETE: keysym = PK_END; break;
5250 case PK_END: keysym = PK_PAGEUP; break;
5251 case PK_PAGEUP: keysym = PK_DELETE; break;
5252 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
5253 default: break; /* else gcc warns `enum value not used' */
5258 if (term->cfg.rxvt_homeend &&
5259 (keysym == PK_HOME || keysym == PK_END)) {
5260 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5264 if (term->vt52_mode) {
5268 * A real VT52 doesn't have these, and a VT220 doesn't
5269 * send anything for them in VT52 mode.
5272 case PK_HOME: xkey = 'H'; break;
5273 case PK_INSERT: xkey = 'L'; break;
5274 case PK_DELETE: xkey = 'M'; break;
5275 case PK_END: xkey = 'E'; break;
5276 case PK_PAGEUP: xkey = 'I'; break;
5277 case PK_PAGEDOWN: xkey = 'G'; break;
5278 default: xkey=0; break; /* else gcc warns `enum value not used'*/
5280 p += sprintf((char *) p, "\x1B%c", xkey);
5285 case PK_HOME: code = 1; break;
5286 case PK_INSERT: code = 2; break;
5287 case PK_DELETE: code = 3; break;
5288 case PK_END: code = 4; break;
5289 case PK_PAGEUP: code = 5; break;
5290 case PK_PAGEDOWN: code = 6; break;
5291 default: code = 0; break; /* else gcc warns `enum value not used' */
5293 p += sprintf((char *) p, "\x1B[%d~", code);
5297 if (PK_ISFKEY(keysym)) {
5298 /* Map Shift+F1-F10 to F11-F20 */
5299 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5301 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5303 /* XXX This overrides the XTERM/VT52 mode below */
5305 if (keysym >= PK_F6) offt++;
5306 if (keysym >= PK_F12) offt++;
5307 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5308 'P' + keysym - PK_F1 - offt);
5311 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5312 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5315 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5316 if (term->vt52_mode)
5317 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5319 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5322 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5326 if (PK_ISCURSOR(keysym)) {
5330 case PK_UP: xkey = 'A'; break;
5331 case PK_DOWN: xkey = 'B'; break;
5332 case PK_RIGHT: xkey = 'C'; break;
5333 case PK_LEFT: xkey = 'D'; break;
5334 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5335 default: xkey = 0; break; /* else gcc warns `enum value not used' */
5337 if (term->vt52_mode)
5338 p += sprintf((char *) p, "\x1B%c", xkey);
5340 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5342 /* Useful mapping of Ctrl-arrows */
5343 if (modifiers == PKM_CONTROL)
5347 p += sprintf((char *) p, "\x1BO%c", xkey);
5349 p += sprintf((char *) p, "\x1B[%c", xkey);
5355 if (p > output || tlen > 0) {
5357 * Interrupt an ongoing paste. I'm not sure
5358 * this is sensible, but for the moment it's
5359 * preferable to having to faff about buffering
5365 * We need not bother about stdin backlogs
5366 * here, because in GUI PuTTY we can't do
5367 * anything about it anyway; there's no means
5368 * of asking Windows to hold off on KEYDOWN
5369 * messages. We _have_ to buffer everything
5372 term_seen_key_event(term);
5376 fprintf(stderr, "sending ESC\n");
5378 ldisc_send(term->ldisc, "\x1b", 1, 1);
5383 fprintf(stderr, "sending %d bytes:", p - output);
5384 for (i = 0; i < p - output; i++)
5385 fprintf(stderr, " %02x", output[i]);
5386 fprintf(stderr, "\n");
5388 ldisc_send(term->ldisc, output, p - output, 1);
5389 } else if (tlen > 0) {
5391 fprintf(stderr, "sending %d unichars:", tlen);
5392 for (i = 0; i < tlen; i++)
5393 fprintf(stderr, " %04x", (unsigned) text[i]);
5394 fprintf(stderr, "\n");
5396 luni_send(term->ldisc, text, tlen, 1);
5401 void term_nopaste(Terminal *term)
5403 if (term->paste_len == 0)
5405 sfree(term->paste_buffer);
5406 term->paste_buffer = NULL;
5407 term->paste_len = 0;
5410 int term_paste_pending(Terminal *term)
5412 return term->paste_len != 0;
5415 void term_paste(Terminal *term)
5417 long now, paste_diff;
5419 if (term->paste_len == 0)
5422 /* Don't wait forever to paste */
5423 if (term->paste_hold) {
5424 now = GETTICKCOUNT();
5425 paste_diff = now - term->last_paste;
5426 if (paste_diff >= 0 && paste_diff < 450)
5429 term->paste_hold = 0;
5431 while (term->paste_pos < term->paste_len) {
5433 while (n + term->paste_pos < term->paste_len) {
5434 if (term->paste_buffer[term->paste_pos + n++] == '\015')
5438 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
5439 term->paste_pos += n;
5441 if (term->paste_pos < term->paste_len) {
5442 term->paste_hold = 1;
5446 sfree(term->paste_buffer);
5447 term->paste_buffer = NULL;
5448 term->paste_len = 0;
5451 static void deselect(Terminal *term)
5453 term->selstate = NO_SELECTION;
5454 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
5457 void term_deselect(Terminal *term)
5463 int term_ldisc(Terminal *term, int option)
5465 if (option == LD_ECHO)
5466 return term->term_echoing;
5467 if (option == LD_EDIT)
5468 return term->term_editing;
5472 int term_data(Terminal *term, int is_stderr, const char *data, int len)
5474 bufchain_add(&term->inbuf, data, len);
5476 if (!term->in_term_out) {
5477 term->in_term_out = TRUE;
5478 term_blink(term, 1);
5480 term->in_term_out = FALSE;
5484 * term_out() always completely empties inbuf. Therefore,
5485 * there's no reason at all to return anything other than zero
5486 * from this function, because there _can't_ be a question of
5487 * the remote side needing to wait until term_out() has cleared
5490 * This is a slightly suboptimal way to deal with SSH2 - in
5491 * principle, the window mechanism would allow us to continue
5492 * to accept data on forwarded ports and X connections even
5493 * while the terminal processing was going slowly - but we
5494 * can't do the 100% right thing without moving the terminal
5495 * processing into a separate thread, and that might hurt
5496 * portability. So we manage stdout buffering the old SSH1 way:
5497 * if the terminal processing goes slowly, the whole SSH
5498 * connection stops accepting data until it's ready.
5500 * In practice, I can't imagine this causing serious trouble.
5505 void term_provide_logctx(Terminal *term, void *logctx)
5507 term->logctx = logctx;