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)
124 static void unlineptr(termline *line)
131 * Compress and decompress a termline into an RLE-based format for
132 * storing in scrollback. (Since scrollback almost never needs to
133 * be modified and exists in huge quantities, this is a sensible
134 * tradeoff, particularly since it allows us to continue adding
135 * features to the main termchar structure without proportionally
136 * bloating the terminal emulator's memory footprint unless those
137 * features are in constant use.)
143 static void add(struct buf *b, unsigned char c)
145 if (b->len >= b->size) {
146 b->size = (b->len * 3 / 2) + 512;
147 b->data = sresize(b->data, b->size, unsigned char);
149 b->data[b->len++] = c;
151 static int get(struct buf *b)
153 return b->data[b->len++];
155 static void makerle(struct buf *b, termline *ldata,
156 void (*makeliteral)(struct buf *b, termchar *c,
157 unsigned long *state))
159 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
160 termchar *c = ldata->chars;
161 unsigned long state = 0, oldstate;
168 prevlen = prevpos = 0;
173 makeliteral(b, c++, &state);
174 thislen = b->len - thispos;
175 if (thislen == prevlen &&
176 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
178 * This literal precisely matches the previous one.
179 * Turn it into a run if it's worthwhile.
181 * With one-byte literals, it costs us two bytes to
182 * encode a run, plus another byte to write the header
183 * to resume normal output; so a three-element run is
184 * neutral, and anything beyond that is unconditionally
185 * worthwhile. With two-byte literals or more, even a
188 if (thislen > 1 || prev2) {
192 * It's worth encoding a run. Start at prevpos,
193 * unless hdrsize==0 in which case we can back up
194 * another one and start by overwriting hdrpos.
197 hdrsize--; /* remove the literal at prevpos */
201 prevpos -= prevlen;/* and possibly another one */
205 assert(prevpos == hdrpos + 1);
207 b->len = prevpos+prevlen;
209 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
211 b->len = prevpos+prevlen+1;
213 * Terminate the previous run of ordinary
216 assert(hdrsize >= 1 && hdrsize <= 128);
217 b->data[hdrpos] = hdrsize - 1;
220 runlen = prev2 ? 3 : 2;
222 while (n > 0 && runlen < 129) {
226 makeliteral(b, c, &state);
227 tmplen = b->len - tmppos;
229 if (tmplen != thislen ||
230 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
232 break; /* run over */
237 assert(runlen >= 2 && runlen <= 129);
238 b->data[runpos] = runlen + 0x80 - 2;
247 * Just flag that the previous two literals were
248 * identical, in case we find a third identical one
249 * we want to turn into a run.
262 * This character isn't (yet) part of a run. Add it to
266 if (hdrsize == 128) {
267 b->data[hdrpos] = hdrsize - 1;
271 prevlen = prevpos = 0;
280 assert(hdrsize <= 128);
281 b->data[hdrpos] = hdrsize - 1;
286 static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
289 * My encoding for characters is UTF-8-like, in that it stores
290 * 7-bit ASCII in one byte and uses high-bit-set bytes as
291 * introducers to indicate a longer sequence. However, it's
292 * unlike UTF-8 in that it doesn't need to be able to
293 * resynchronise, and therefore I don't want to waste two bits
294 * per byte on having recognisable continuation characters.
295 * Also I don't want to rule out the possibility that I may one
296 * day use values 0x80000000-0xFFFFFFFF for interesting
297 * purposes, so unlike UTF-8 I need a full 32-bit range.
298 * Accordingly, here is my encoding:
300 * 00000000-0000007F: 0xxxxxxx (but see below)
301 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
302 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
303 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
304 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
306 * (`Z' is like `x' but is always going to be zero since the
307 * values I'm encoding don't go above 2^32. In principle the
308 * five-byte form of the encoding could extend to 2^35, and
309 * there could be six-, seven-, eight- and nine-byte forms as
310 * well to allow up to 64-bit values to be encoded. But that's
311 * completely unnecessary for these purposes!)
313 * The encoding as written above would be very simple, except
314 * that 7-bit ASCII can occur in several different ways in the
315 * terminal data; sometimes it crops up in the D800 page
316 * (CSET_ASCII) but at other times it's in the 0000 page (real
317 * Unicode). Therefore, this encoding is actually _stateful_:
318 * the one-byte encoding of 00-7F actually indicates `reuse the
319 * upper three bytes of the last character', and to encode an
320 * absolute value of 00-7F you need to use the two-byte form
323 if ((c->chr & ~0x7F) == *state) {
324 add(b, (unsigned char)(c->chr & 0x7F));
325 } else if (c->chr < 0x4000) {
326 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
327 add(b, (unsigned char)(c->chr & 0xFF));
328 } else if (c->chr < 0x200000) {
329 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
330 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
331 add(b, (unsigned char)(c->chr & 0xFF));
332 } else if (c->chr < 0x10000000) {
333 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
334 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
335 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
336 add(b, (unsigned char)(c->chr & 0xFF));
339 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
340 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
341 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
342 add(b, (unsigned char)(c->chr & 0xFF));
344 *state = c->chr & ~0xFF;
346 static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
349 * My encoding for attributes is 16-bit-granular and assumes
350 * that the top bit of the word is never required. I either
351 * store a two-byte value with the top bit clear (indicating
352 * just that value), or a four-byte value with the top bit set
353 * (indicating the same value with its top bit clear).
355 if (c->attr < 0x8000) {
356 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
357 add(b, (unsigned char)(c->attr & 0xFF));
359 add(b, (unsigned char)(((c->attr >> 24) & 0x7F) | 0x80));
360 add(b, (unsigned char)((c->attr >> 16) & 0xFF));
361 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
362 add(b, (unsigned char)(c->attr & 0xFF));
366 static termline *decompressline(unsigned char *data, int *bytes_used);
368 static unsigned char *compressline(termline *ldata)
370 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
373 * First, store the column count, 7 bits at a time, least
374 * significant `digit' first, with the high bit set on all but
380 add(b, (unsigned char)((n & 0x7F) | 0x80));
383 add(b, (unsigned char)(n));
387 * Next store the lattrs; same principle.
390 int n = ldata->lattr;
392 add(b, (unsigned char)((n & 0x7F) | 0x80));
395 add(b, (unsigned char)(n));
399 * Now we store a sequence of separate run-length encoded
400 * fragments, each containing exactly as many symbols as there
401 * are columns in the ldata.
403 * All of these have a common basic format:
405 * - a byte 00-7F indicates that X+1 literals follow it
406 * - a byte 80-FF indicates that a single literal follows it
407 * and expects to be repeated (X-0x80)+2 times.
409 * The format of the `literals' varies between the fragments.
411 makerle(b, ldata, makeliteral_chr);
412 makerle(b, ldata, makeliteral_attr);
415 * Diagnostics: ensure that the compressed data really does
416 * decompress to the right thing.
418 #ifndef CHECK_SB_COMPRESSION
423 #ifdef DIAGNOSTIC_SB_COMPRESSION
425 for (i = 0; i < b->len; i++) {
426 printf(" %02x ", b->data[i]);
431 dcl = decompressline(b->data, &dused);
432 assert(b->len == dused);
433 assert(ldata->cols == dcl->cols);
434 assert(ldata->lattr == dcl->lattr);
435 assert(!memcmp(ldata->chars, dcl->chars, ldata->cols * TSIZE));
437 #ifdef DIAGNOSTIC_SB_COMPRESSION
438 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
439 ldata->cols, 4 * ldata->cols, dused,
440 (double)dused / (4 * ldata->cols));
448 * Trim the allocated memory so we don't waste any, and return.
450 return sresize(b->data, b->len, unsigned char);
453 static void readrle(struct buf *b, termline *ldata,
454 void (*readliteral)(struct buf *b, termchar *c,
455 unsigned long *state))
458 unsigned long state = 0;
460 while (n < ldata->cols) {
466 int pos = b->len, count = hdr + 2 - 0x80;
468 assert(n < ldata->cols);
470 readliteral(b, ldata->chars + n, &state);
474 /* Just a sequence of consecutive literals. */
478 assert(n < ldata->cols);
479 readliteral(b, ldata->chars + n, &state);
485 assert(n == ldata->cols);
487 static void readliteral_chr(struct buf *b, termchar *c, unsigned long *state)
492 * 00000000-0000007F: 0xxxxxxx
493 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
494 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
495 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
496 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
501 c->chr = byte | *state;
502 } else if (byte < 0xC0) {
503 c->chr = (byte &~ 0xC0) << 8;
505 } else if (byte < 0xE0) {
506 c->chr = (byte &~ 0xE0) << 16;
507 c->chr |= get(b) << 8;
509 } else if (byte < 0xF0) {
510 c->chr = (byte &~ 0xF0) << 24;
511 c->chr |= get(b) << 16;
512 c->chr |= get(b) << 8;
515 assert(byte == 0xF0);
516 c->chr = get(b) << 24;
517 c->chr |= get(b) << 16;
518 c->chr |= get(b) << 8;
521 *state = c->chr & ~0xFF;
523 static void readliteral_attr(struct buf *b, termchar *c, unsigned long *state)
539 static termline *decompressline(unsigned char *data, int *bytes_used)
541 int ncols, byte, shift;
542 struct buf buffer, *b = &buffer;
549 * First read in the column count.
554 ncols |= (byte & 0x7F) << shift;
556 } while (byte & 0x80);
559 * Now create the output termline.
561 ldata = snew(termline);
562 ldata->chars = snewn(ncols, termchar);
564 ldata->temporary = TRUE;
567 * Now read in the lattr.
569 ldata->lattr = shift = 0;
572 ldata->lattr |= (byte & 0x7F) << shift;
574 } while (byte & 0x80);
577 * Now we read in each of the RLE streams in turn.
579 readrle(b, ldata, readliteral_chr);
580 readrle(b, ldata, readliteral_attr);
582 /* Return the number of bytes read, for diagnostic purposes. */
584 *bytes_used = b->len;
590 * Resize a line to make it `cols' columns wide.
592 static void resizeline(Terminal *term, termline *line, int cols)
596 if (line->cols != cols) {
598 * This line is the wrong length, which probably means it
599 * hasn't been accessed since a resize. Resize it now.
602 line->chars = sresize(line->chars, cols, TTYPE);
604 for (i = oldlen; i < cols; i++)
605 line->chars[i] = term->basic_erase_char;
610 * Get the number of lines in the scrollback.
612 static int sblines(Terminal *term)
614 int sblines = count234(term->scrollback);
615 if (term->cfg.erase_to_scrollback &&
616 term->alt_which && term->alt_screen) {
617 sblines += term->alt_sblines;
623 * Retrieve a line of the screen or of the scrollback, according to
624 * whether the y coordinate is non-negative or negative
627 static termline *lineptr(Terminal *term, int y, int lineno, int screen)
634 whichtree = term->screen;
641 if (term->cfg.erase_to_scrollback &&
642 term->alt_which && term->alt_screen) {
643 altlines = term->alt_sblines;
646 whichtree = term->scrollback;
647 treeindex = y + altlines + count234(term->scrollback);
649 whichtree = term->alt_screen;
650 treeindex = y + term->alt_sblines;
651 /* treeindex = y + count234(term->alt_screen); */
654 if (whichtree == term->scrollback) {
655 unsigned char *cline = index234(whichtree, treeindex);
656 line = decompressline(cline, NULL);
658 line = index234(whichtree, treeindex);
661 /* We assume that we don't screw up and retrieve something out of range. */
662 assert(line != NULL);
664 resizeline(term, line, term->cols);
665 /* FIXME: should we sort the compressed scrollback out here? */
670 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
671 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
674 * Set up power-on settings for the terminal.
676 static void power_on(Terminal *term)
678 term->curs.x = term->curs.y = 0;
679 term->alt_x = term->alt_y = 0;
680 term->savecurs.x = term->savecurs.y = 0;
681 term->alt_t = term->marg_t = 0;
682 if (term->rows != -1)
683 term->alt_b = term->marg_b = term->rows - 1;
685 term->alt_b = term->marg_b = 0;
686 if (term->cols != -1) {
688 for (i = 0; i < term->cols; i++)
689 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
691 term->alt_om = term->dec_om = term->cfg.dec_om;
692 term->alt_ins = term->insert = FALSE;
693 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
694 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
695 term->alt_cset = term->cset = term->save_cset = 0;
696 term->alt_utf = term->utf = term->save_utf = 0;
698 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
699 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
701 term->in_vbell = FALSE;
703 term->big_cursor = 0;
704 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
705 term->term_editing = term->term_echoing = FALSE;
706 term->app_cursor_keys = term->cfg.app_cursor;
707 term->app_keypad_keys = term->cfg.app_keypad;
708 term->use_bce = term->cfg.bce;
709 term->blink_is_real = term->cfg.blinktext;
710 term->erase_char = term->basic_erase_char;
712 term_print_finish(term);
715 for (i = 0; i < 256; i++)
716 term->wordness[i] = term->cfg.wordness[i];
719 swap_screen(term, 1, FALSE, FALSE);
720 erase_lots(term, FALSE, TRUE, TRUE);
721 swap_screen(term, 0, FALSE, FALSE);
722 erase_lots(term, FALSE, TRUE, TRUE);
727 * Force a screen update.
729 void term_update(Terminal *term)
732 ctx = get_ctx(term->frontend);
734 int need_sbar_update = term->seen_disp_event;
735 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
736 term->disptop = 0; /* return to main screen */
737 term->seen_disp_event = 0;
738 need_sbar_update = TRUE;
741 /* Allocate temporary buffers for Arabic shaping and bidi. */
742 if (!term->cfg.arabicshaping || !term->cfg.bidi)
744 term->wcFrom = sresize(term->wcFrom, term->cols, bidi_char);
745 term->ltemp = sresize(term->ltemp, term->cols+1, termchar);
746 term->wcTo = sresize(term->wcTo, term->cols, bidi_char);
749 if (need_sbar_update)
751 do_paint(term, ctx, TRUE);
752 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
758 * Called from front end when a keypress occurs, to trigger
759 * anything magical that needs to happen in that situation.
761 void term_seen_key_event(Terminal *term)
764 * On any keypress, clear the bell overload mechanism
765 * completely, on the grounds that large numbers of
766 * beeps coming from deliberate key action are likely
767 * to be intended (e.g. beeps from filename completion
768 * blocking repeatedly).
770 term->beep_overloaded = FALSE;
771 while (term->beephead) {
772 struct beeptime *tmp = term->beephead;
773 term->beephead = tmp->next;
776 term->beeptail = NULL;
780 * Reset the scrollback on keypress, if we're doing that.
782 if (term->cfg.scroll_on_key) {
783 term->disptop = 0; /* return to main screen */
784 term->seen_disp_event = 1;
789 * Same as power_on(), but an external function.
791 void term_pwron(Terminal *term)
794 if (term->ldisc) /* cause ldisc to notice changes */
795 ldisc_send(term->ldisc, NULL, 0, 0);
802 static void set_erase_char(Terminal *term)
804 term->erase_char = term->basic_erase_char;
806 term->erase_char.attr = (term->curr_attr &
807 (ATTR_FGMASK | ATTR_BGMASK));
811 * When the user reconfigures us, we need to check the forbidden-
812 * alternate-screen config option, disable raw mouse mode if the
813 * user has disabled mouse reporting, and abandon a print job if
814 * the user has disabled printing.
816 void term_reconfig(Terminal *term, Config *cfg)
819 * Before adopting the new config, check all those terminal
820 * settings which control power-on defaults; and if they've
821 * changed, we will modify the current state as well as the
822 * default one. The full list is: Auto wrap mode, DEC Origin
823 * Mode, BCE, blinking text, character classes.
825 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
828 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
829 reset_decom = (term->cfg.dec_om != cfg->dec_om);
830 reset_bce = (term->cfg.bce != cfg->bce);
831 reset_blink = (term->cfg.blinktext != cfg->blinktext);
833 for (i = 0; i < lenof(term->cfg.wordness); i++)
834 if (term->cfg.wordness[i] != cfg->wordness[i])
837 term->cfg = *cfg; /* STRUCTURE COPY */
840 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
842 term->alt_om = term->dec_om = term->cfg.dec_om;
844 term->use_bce = term->cfg.bce;
845 set_erase_char(term);
848 term->blink_is_real = term->cfg.blinktext;
850 for (i = 0; i < 256; i++)
851 term->wordness[i] = term->cfg.wordness[i];
853 if (term->cfg.no_alt_screen)
854 swap_screen(term, 0, FALSE, FALSE);
855 if (term->cfg.no_mouse_rep) {
856 term->xterm_mouse = 0;
857 set_raw_mouse_mode(term->frontend, 0);
859 if (term->cfg.no_remote_charset) {
860 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
861 term->sco_acs = term->alt_sco_acs = 0;
864 if (!*term->cfg.printer) {
865 term_print_finish(term);
870 * Clear the scrollback.
872 void term_clrsb(Terminal *term)
876 while ((line = delpos234(term->scrollback, 0)) != NULL) {
877 sfree(line); /* this is compressed data, not a termline */
879 term->tempsblines = 0;
880 term->alt_sblines = 0;
885 * Initialise the terminal.
887 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
893 * Allocate a new Terminal structure and initialise the fields
896 term = snew(Terminal);
897 term->frontend = frontend;
898 term->ucsdata = ucsdata;
899 term->cfg = *mycfg; /* STRUCTURE COPY */
901 term->compatibility_level = TM_PUTTY;
902 strcpy(term->id_string, "\033[?6c");
903 term->last_blink = term->last_tblink = 0;
904 term->paste_buffer = NULL;
906 term->last_paste = 0;
907 bufchain_init(&term->inbuf);
908 bufchain_init(&term->printer_buf);
909 term->printing = term->only_printing = FALSE;
910 term->print_job = NULL;
911 term->vt52_mode = FALSE;
912 term->cr_lf_return = FALSE;
913 term->seen_disp_event = FALSE;
914 term->xterm_mouse = term->mouse_is_down = FALSE;
915 term->reset_132 = FALSE;
916 term->blinker = term->tblinker = 0;
918 term->repeat_off = FALSE;
919 term->termstate = TOPLEVEL;
920 term->selstate = NO_SELECTION;
923 term->screen = term->alt_screen = term->scrollback = NULL;
924 term->tempsblines = 0;
925 term->alt_sblines = 0;
927 term->disptext = NULL;
928 term->dispcursx = term->dispcursy = -1;
931 term->rows = term->cols = -1;
933 term->beephead = term->beeptail = NULL;
934 #ifdef OPTIMISE_SCROLL
935 term->scrollhead = term->scrolltail = NULL;
936 #endif /* OPTIMISE_SCROLL */
938 term->lastbeep = FALSE;
939 term->beep_overloaded = FALSE;
940 term->attr_mask = 0xffffffff;
941 term->resize_fn = NULL;
942 term->resize_ctx = NULL;
943 term->in_term_out = FALSE;
948 term->bidi_cache_size = 0;
949 term->pre_bidi_cache = term->post_bidi_cache = NULL;
951 term->basic_erase_char.chr = CSET_ASCII | ' ';
952 term->basic_erase_char.attr = ATTR_DEFAULT;
957 void term_free(Terminal *term)
960 struct beeptime *beep;
963 while ((line = delpos234(term->scrollback, 0)) != NULL)
964 sfree(line); /* compressed data, not a termline */
965 freetree234(term->scrollback);
966 while ((line = delpos234(term->screen, 0)) != NULL)
968 freetree234(term->screen);
969 while ((line = delpos234(term->alt_screen, 0)) != NULL)
971 freetree234(term->alt_screen);
972 if (term->disptext) {
973 for (i = 0; i < term->rows; i++)
974 freeline(term->disptext[i]);
976 sfree(term->disptext);
977 while (term->beephead) {
978 beep = term->beephead;
979 term->beephead = beep->next;
982 bufchain_clear(&term->inbuf);
984 printer_finish_job(term->print_job);
985 bufchain_clear(&term->printer_buf);
986 sfree(term->paste_buffer);
991 for (i = 0; i < term->bidi_cache_size; i++) {
992 sfree(term->pre_bidi_cache[i]);
993 sfree(term->post_bidi_cache[i]);
995 sfree(term->pre_bidi_cache);
996 sfree(term->post_bidi_cache);
1002 * Set up the terminal for a given size.
1004 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
1007 termline **newdisp, *line;
1008 int i, j, oldrows = term->rows;
1010 int save_alt_which = term->alt_which;
1012 if (newrows == term->rows && newcols == term->cols &&
1013 newsavelines == term->savelines)
1014 return; /* nothing to do */
1017 swap_screen(term, 0, FALSE, FALSE);
1019 term->alt_t = term->marg_t = 0;
1020 term->alt_b = term->marg_b = newrows - 1;
1022 if (term->rows == -1) {
1023 term->scrollback = newtree234(NULL);
1024 term->screen = newtree234(NULL);
1025 term->tempsblines = 0;
1030 * Resize the screen and scrollback. We only need to shift
1031 * lines around within our data structures, because lineptr()
1032 * will take care of resizing each individual line if
1035 * - If the new screen is longer, we shunt lines in from temporary
1036 * scrollback if possible, otherwise we add new blank lines at
1039 * - If the new screen is shorter, we remove any blank lines at
1040 * the bottom if possible, otherwise shunt lines above the cursor
1041 * to scrollback if possible, otherwise delete lines below the
1044 * - Then, if the new scrollback length is less than the
1045 * amount of scrollback we actually have, we must throw some
1048 sblen = count234(term->scrollback);
1049 /* Do this loop to expand the screen if newrows > rows */
1050 assert(term->rows == count234(term->screen));
1051 while (term->rows < newrows) {
1052 if (term->tempsblines > 0) {
1053 unsigned char *cline;
1054 /* Insert a line from the scrollback at the top of the screen. */
1055 assert(sblen >= term->tempsblines);
1056 cline = delpos234(term->scrollback, --sblen);
1057 line = decompressline(cline, NULL);
1059 line->temporary = FALSE; /* reconstituted line is now real */
1060 term->tempsblines -= 1;
1061 addpos234(term->screen, line, 0);
1063 term->savecurs.y += 1;
1065 /* Add a new blank line at the bottom of the screen. */
1066 line = newline(term, newcols, FALSE);
1067 addpos234(term->screen, line, count234(term->screen));
1071 /* Do this loop to shrink the screen if newrows < rows */
1072 while (term->rows > newrows) {
1073 if (term->curs.y < term->rows - 1) {
1074 /* delete bottom row, unless it contains the cursor */
1075 sfree(delpos234(term->screen, term->rows - 1));
1077 /* push top row to scrollback */
1078 line = delpos234(term->screen, 0);
1079 addpos234(term->scrollback, compressline(line), sblen++);
1081 term->tempsblines += 1;
1083 term->savecurs.y -= 1;
1087 assert(term->rows == newrows);
1088 assert(count234(term->screen) == newrows);
1090 /* Delete any excess lines from the scrollback. */
1091 while (sblen > newsavelines) {
1092 line = delpos234(term->scrollback, 0);
1096 if (sblen < term->tempsblines)
1097 term->tempsblines = sblen;
1098 assert(count234(term->scrollback) <= newsavelines);
1099 assert(count234(term->scrollback) >= term->tempsblines);
1102 /* Make a new displayed text buffer. */
1103 newdisp = snewn(newrows, termline *);
1104 for (i = 0; i < newrows; i++) {
1105 newdisp[i] = newline(term, newcols, FALSE);
1106 for (j = 0; j < newcols; j++)
1107 newdisp[i]->chars[i].attr = ATTR_INVALID;
1109 if (term->disptext) {
1110 for (i = 0; i < oldrows; i++)
1111 freeline(term->disptext[i]);
1113 sfree(term->disptext);
1114 term->disptext = newdisp;
1115 term->dispcursx = term->dispcursy = -1;
1117 /* Make a new alternate screen. */
1118 newalt = newtree234(NULL);
1119 for (i = 0; i < newrows; i++) {
1120 line = newline(term, newcols, TRUE);
1121 addpos234(newalt, line, i);
1123 if (term->alt_screen) {
1124 while (NULL != (line = delpos234(term->alt_screen, 0)))
1126 freetree234(term->alt_screen);
1128 term->alt_screen = newalt;
1129 term->alt_sblines = 0;
1131 term->tabs = sresize(term->tabs, newcols, unsigned char);
1134 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1135 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
1138 /* Check that the cursor positions are still valid. */
1139 if (term->savecurs.y < 0)
1140 term->savecurs.y = 0;
1141 if (term->savecurs.y >= newrows)
1142 term->savecurs.y = newrows - 1;
1143 if (term->curs.y < 0)
1145 if (term->curs.y >= newrows)
1146 term->curs.y = newrows - 1;
1147 if (term->curs.x >= newcols)
1148 term->curs.x = newcols - 1;
1149 term->alt_x = term->alt_y = 0;
1150 term->wrapnext = term->alt_wnext = FALSE;
1152 term->rows = newrows;
1153 term->cols = newcols;
1154 term->savelines = newsavelines;
1157 swap_screen(term, save_alt_which, FALSE, FALSE);
1161 if (term->resize_fn)
1162 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1166 * Hand a function and context pointer to the terminal which it can
1167 * use to notify a back end of resizes.
1169 void term_provide_resize_fn(Terminal *term,
1170 void (*resize_fn)(void *, int, int),
1173 term->resize_fn = resize_fn;
1174 term->resize_ctx = resize_ctx;
1175 if (term->cols > 0 && term->rows > 0)
1176 resize_fn(resize_ctx, term->cols, term->rows);
1179 /* Find the bottom line on the screen that has any content.
1180 * If only the top line has content, returns 0.
1181 * If no lines have content, return -1.
1183 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1186 for (i = count234(screen) - 1; i >= 0; i--) {
1187 termline *line = index234(screen, i);
1189 for (j = 0; j < line->cols; j++) {
1190 if (line->chars[j].chr != term->erase_char.chr ||
1191 line->chars[j].attr != term->erase_char.attr) break;
1193 if (j != line->cols) break;
1199 * Swap screens. If `reset' is TRUE and we have been asked to
1200 * switch to the alternate screen, we must bring most of its
1201 * configuration from the main screen and erase the contents of the
1202 * alternate screen completely. (This is even true if we're already
1203 * on it! Blame xterm.)
1205 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
1211 reset = FALSE; /* do no weird resetting if which==0 */
1213 if (which != term->alt_which) {
1214 term->alt_which = which;
1216 ttr = term->alt_screen;
1217 term->alt_screen = term->screen;
1219 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
1221 if (!reset && !keep_cur_pos)
1222 term->curs.x = term->alt_x;
1225 if (!reset && !keep_cur_pos)
1226 term->curs.y = term->alt_y;
1229 if (!reset) term->marg_t = term->alt_t;
1232 if (!reset) term->marg_b = term->alt_b;
1235 if (!reset) term->dec_om = term->alt_om;
1238 if (!reset) term->wrap = term->alt_wrap;
1241 if (!reset) term->wrapnext = term->alt_wnext;
1242 term->alt_wnext = t;
1244 if (!reset) term->insert = term->alt_ins;
1247 if (!reset) term->cset = term->alt_cset;
1250 if (!reset) term->utf = term->alt_utf;
1253 if (!reset) term->sco_acs = term->alt_sco_acs;
1254 term->alt_sco_acs = t;
1257 if (reset && term->screen) {
1259 * Yes, this _is_ supposed to honour background-colour-erase.
1261 erase_lots(term, FALSE, TRUE, TRUE);
1265 * This might not be possible if we're called during
1273 * Update the scroll bar.
1275 static void update_sbar(Terminal *term)
1277 int nscroll = sblines(term);
1278 set_sbar(term->frontend, nscroll + term->rows,
1279 nscroll + term->disptop, term->rows);
1283 * Check whether the region bounded by the two pointers intersects
1284 * the scroll region, and de-select the on-screen selection if so.
1286 static void check_selection(Terminal *term, pos from, pos to)
1288 if (poslt(from, term->selend) && poslt(term->selstart, to))
1293 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1294 * for backward.) `sb' is TRUE if the scrolling is permitted to
1295 * affect the scrollback buffer.
1297 * NB this function invalidates all pointers into lines of the
1298 * screen data structures. In particular, you MUST call fix_cpos
1299 * after calling scroll() and before doing anything else that
1300 * uses the cpos shortcut pointer.
1302 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
1305 int i, seltop, olddisptop, shift;
1307 if (topline != 0 || term->alt_which != 0)
1310 olddisptop = term->disptop;
1314 line = delpos234(term->screen, botline);
1315 resizeline(term, line, term->cols);
1316 for (i = 0; i < term->cols; i++)
1317 line->chars[i] = term->erase_char;
1318 line->lattr = LATTR_NORM;
1319 addpos234(term->screen, line, topline);
1321 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1323 if (term->selstart.y > botline) {
1324 term->selstart.y = botline + 1;
1325 term->selstart.x = 0;
1328 if (term->selend.y >= topline && term->selend.y <= botline) {
1330 if (term->selend.y > botline) {
1331 term->selend.y = botline + 1;
1340 line = delpos234(term->screen, topline);
1341 if (sb && term->savelines > 0) {
1342 int sblen = count234(term->scrollback);
1344 * We must add this line to the scrollback. We'll
1345 * remove a line from the top of the scrollback if
1346 * the scrollback is full.
1348 if (sblen == term->savelines) {
1349 unsigned char *cline;
1352 cline = delpos234(term->scrollback, 0);
1355 term->tempsblines += 1;
1357 addpos234(term->scrollback, compressline(line), sblen);
1359 line = newline(term, term->cols, TRUE);
1362 * If the user is currently looking at part of the
1363 * scrollback, and they haven't enabled any options
1364 * that are going to reset the scrollback as a
1365 * result of this movement, then the chances are
1366 * they'd like to keep looking at the same line. So
1367 * we move their viewpoint at the same rate as the
1368 * scroll, at least until their viewpoint hits the
1369 * top end of the scrollback buffer, at which point
1370 * we don't have the choice any more.
1372 * Thanks to Jan Holmen Holsten for the idea and
1373 * initial implementation.
1375 if (term->disptop > -term->savelines && term->disptop < 0)
1378 resizeline(term, line, term->cols);
1379 for (i = 0; i < term->cols; i++)
1380 line->chars[i] = term->erase_char;
1381 line->lattr = LATTR_NORM;
1382 addpos234(term->screen, line, botline);
1385 * If the selection endpoints move into the scrollback,
1386 * we keep them moving until they hit the top. However,
1387 * of course, if the line _hasn't_ moved into the
1388 * scrollback then we don't do this, and cut them off
1389 * at the top of the scroll region.
1391 * This applies to selstart and selend (for an existing
1392 * selection), and also selanchor (for one being
1393 * selected as we speak).
1395 seltop = sb ? -term->savelines : topline;
1397 if (term->selstate != NO_SELECTION) {
1398 if (term->selstart.y >= seltop &&
1399 term->selstart.y <= botline) {
1401 if (term->selstart.y < seltop) {
1402 term->selstart.y = seltop;
1403 term->selstart.x = 0;
1406 if (term->selend.y >= seltop && term->selend.y <= botline) {
1408 if (term->selend.y < seltop) {
1409 term->selend.y = seltop;
1413 if (term->selanchor.y >= seltop &&
1414 term->selanchor.y <= botline) {
1415 term->selanchor.y--;
1416 if (term->selanchor.y < seltop) {
1417 term->selanchor.y = seltop;
1418 term->selanchor.x = 0;
1426 #ifdef OPTIMISE_SCROLL
1427 shift += term->disptop - olddisptop;
1428 if (shift < term->rows && shift > -term->rows && shift != 0)
1429 scroll_display(term, topline, botline, shift);
1430 #endif /* OPTIMISE_SCROLL */
1433 #ifdef OPTIMISE_SCROLL
1435 * Add a scroll of a region on the screen into the pending scroll list.
1436 * `lines' is +ve for scrolling forward, -ve for backward.
1438 * If the scroll is on the same area as the last scroll in the list,
1441 static void save_scroll(Terminal *term, int topline, int botline, int lines)
1443 struct scrollregion *newscroll;
1444 if (term->scrolltail &&
1445 term->scrolltail->topline == topline &&
1446 term->scrolltail->botline == botline) {
1447 term->scrolltail->lines += lines;
1449 newscroll = snew(struct scrollregion);
1450 newscroll->topline = topline;
1451 newscroll->botline = botline;
1452 newscroll->lines = lines;
1453 newscroll->next = NULL;
1455 if (!term->scrollhead)
1456 term->scrollhead = newscroll;
1458 term->scrolltail->next = newscroll;
1459 term->scrolltail = newscroll;
1464 * Scroll the physical display, and our conception of it in disptext.
1466 static void scroll_display(Terminal *term, int topline, int botline, int lines)
1468 int distance, nlines, i;
1470 distance = lines > 0 ? lines : -lines;
1471 nlines = botline - topline + 1 - distance;
1473 for (i = 0; i < nlines; i++)
1474 memcpy(term->disptext[start+i]->chars,
1475 term->disptext[start+i+distance]->chars,
1476 term->cols * TSIZE);
1477 if (term->dispcursy >= 0 &&
1478 term->dispcursy >= topline + distance &&
1479 term->dispcursy < topline + distance + nlines)
1480 term->dispcursy -= distance;
1481 for (i = 0; i < distance; i++)
1482 for (j = 0; j < term->cols; j++)
1483 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
1485 for (i = nlines; i-- ;)
1486 memcpy(term->disptext[start+i+distance]->chars,
1487 term->disptext[start+i]->chars,
1488 term->cols * TSIZE);
1489 if (term->dispcursy >= 0 &&
1490 term->dispcursy >= topline &&
1491 term->dispcursy < topline + nlines)
1492 term->dispcursy += distance;
1493 for (i = 0; i < distance; i++)
1494 for (j = 0; j < term->cols; j++)
1495 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
1497 save_scroll(term, topline, botline, lines);
1499 #endif /* OPTIMISE_SCROLL */
1502 * Move the cursor to a given position, clipping at boundaries. We
1503 * may or may not want to clip at the scroll margin: marg_clip is 0
1504 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1505 * even _being_ outside the margins.
1507 static void move(Terminal *term, int x, int y, int marg_clip)
1511 if (x >= term->cols)
1514 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1517 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1523 if (y >= term->rows)
1528 term->wrapnext = FALSE;
1532 * Save or restore the cursor and SGR mode.
1534 static void save_cursor(Terminal *term, int save)
1537 term->savecurs = term->curs;
1538 term->save_attr = term->curr_attr;
1539 term->save_cset = term->cset;
1540 term->save_utf = term->utf;
1541 term->save_wnext = term->wrapnext;
1542 term->save_csattr = term->cset_attr[term->cset];
1543 term->save_sco_acs = term->sco_acs;
1545 term->curs = term->savecurs;
1546 /* Make sure the window hasn't shrunk since the save */
1547 if (term->curs.x >= term->cols)
1548 term->curs.x = term->cols - 1;
1549 if (term->curs.y >= term->rows)
1550 term->curs.y = term->rows - 1;
1552 term->curr_attr = term->save_attr;
1553 term->cset = term->save_cset;
1554 term->utf = term->save_utf;
1555 term->wrapnext = term->save_wnext;
1557 * wrapnext might reset to False if the x position is no
1558 * longer at the rightmost edge.
1560 if (term->wrapnext && term->curs.x < term->cols-1)
1561 term->wrapnext = FALSE;
1562 term->cset_attr[term->cset] = term->save_csattr;
1563 term->sco_acs = term->save_sco_acs;
1565 set_erase_char(term);
1570 * This function is called before doing _anything_ which affects
1571 * only part of a line of text. It is used to mark the boundary
1572 * between two character positions, and it indicates that some sort
1573 * of effect is going to happen on only one side of that boundary.
1575 * The effect of this function is to check whether a CJK
1576 * double-width character is straddling the boundary, and to remove
1577 * it and replace it with two spaces if so. (Of course, one or
1578 * other of those spaces is then likely to be replaced with
1579 * something else again, as a result of whatever happens next.)
1581 * Also, if the boundary is at the right-hand _edge_ of the screen,
1582 * it implies something deliberate is being done to the rightmost
1583 * column position; hence we must clear LATTR_WRAPPED2.
1585 * The input to the function is the coordinates of the _second_
1586 * character of the pair.
1588 static void check_boundary(Terminal *term, int x, int y)
1592 /* Validate input coordinates, just in case. */
1593 if (x == 0 || x > term->cols)
1596 ldata = scrlineptr(y);
1597 if (x == term->cols) {
1598 ldata->lattr &= ~LATTR_WRAPPED2;
1600 if (ldata->chars[x].chr == UCSWIDE) {
1601 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
1602 ldata->chars[x] = ldata->chars[x-1];
1608 * Erase a large portion of the screen: the whole screen, or the
1609 * whole line, or parts thereof.
1611 static void erase_lots(Terminal *term,
1612 int line_only, int from_begin, int to_end)
1616 int erasing_lines_from_top = 0;
1619 start.y = term->curs.y;
1621 end.y = term->curs.y + 1;
1623 erase_lattr = FALSE;
1638 if (!from_begin || !to_end)
1639 check_boundary(term, term->curs.x, term->curs.y);
1640 check_selection(term, start, end);
1642 /* Clear screen also forces a full window redraw, just in case. */
1643 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1644 term_invalidate(term);
1646 /* Lines scrolled away shouldn't be brought back on if the terminal
1648 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1649 erasing_lines_from_top = 1;
1651 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1652 /* If it's a whole number of lines, starting at the top, and
1653 * we're fully erasing them, erase by scrolling and keep the
1654 * lines in the scrollback. */
1655 int scrolllines = end.y;
1656 if (end.y == term->rows) {
1657 /* Shrink until we find a non-empty row.*/
1658 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1660 if (scrolllines > 0)
1661 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1664 termline *ldata = scrlineptr(start.y);
1665 while (poslt(start, end)) {
1666 if (start.x == term->cols) {
1668 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1670 ldata->lattr = LATTR_NORM;
1672 ldata->chars[start.x] = term->erase_char;
1674 if (incpos(start) && start.y < term->rows) {
1675 ldata = scrlineptr(start.y);
1680 /* After an erase of lines from the top of the screen, we shouldn't
1681 * bring the lines back again if the terminal enlarges (since the user or
1682 * application has explictly thrown them away). */
1683 if (erasing_lines_from_top && !(term->alt_which))
1684 term->tempsblines = 0;
1688 * Insert or delete characters within the current line. n is +ve if
1689 * insertion is desired, and -ve for deletion.
1691 static void insch(Terminal *term, int n)
1693 int dir = (n < 0 ? -1 : +1);
1698 n = (n < 0 ? -n : n);
1699 if (n > term->cols - term->curs.x)
1700 n = term->cols - term->curs.x;
1701 m = term->cols - term->curs.x - n;
1702 cursplus.y = term->curs.y;
1703 cursplus.x = term->curs.x + n;
1704 check_selection(term, term->curs, cursplus);
1705 check_boundary(term, term->curs.x, term->curs.y);
1707 check_boundary(term, term->curs.x + n, term->curs.y);
1708 ldata = scrlineptr(term->curs.y);
1710 memmove(ldata->chars + term->curs.x, ldata->chars + term->curs.x + n,
1713 ldata->chars[term->curs.x + m++] = term->erase_char;
1715 memmove(ldata->chars + term->curs.x + n, ldata->chars + term->curs.x,
1718 ldata->chars[term->curs.x + n] = term->erase_char;
1723 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1724 * whether the mode is a DEC private one or a normal one.)
1726 static void toggle_mode(Terminal *term, int mode, int query, int state)
1728 unsigned long ticks;
1732 case 1: /* DECCKM: application cursor keys */
1733 term->app_cursor_keys = state;
1735 case 2: /* DECANM: VT52 mode */
1736 term->vt52_mode = !state;
1737 if (term->vt52_mode) {
1738 term->blink_is_real = FALSE;
1739 term->vt52_bold = FALSE;
1741 term->blink_is_real = term->cfg.blinktext;
1744 case 3: /* DECCOLM: 80/132 columns */
1746 if (!term->cfg.no_remote_resize)
1747 request_resize(term->frontend, state ? 132 : 80, term->rows);
1748 term->reset_132 = state;
1749 term->alt_t = term->marg_t = 0;
1750 term->alt_b = term->marg_b = term->rows - 1;
1751 move(term, 0, 0, 0);
1752 erase_lots(term, FALSE, TRUE, TRUE);
1754 case 5: /* DECSCNM: reverse video */
1756 * Toggle reverse video. If we receive an OFF within the
1757 * visual bell timeout period after an ON, we trigger an
1758 * effective visual bell, so that ESC[?5hESC[?5l will
1759 * always be an actually _visible_ visual bell.
1761 ticks = GETTICKCOUNT();
1762 /* turn off a previous vbell to avoid inconsistencies */
1763 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1764 term->in_vbell = FALSE;
1765 if (term->rvideo && !state && /* we're turning it off... */
1766 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1767 /* If there's no vbell timeout already, or this one lasts
1768 * longer, replace vbell_timeout with ours. */
1769 if (!term->in_vbell ||
1770 (term->rvbell_startpoint - term->vbell_startpoint <
1772 term->vbell_startpoint = term->rvbell_startpoint;
1773 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1774 } else if (!term->rvideo && state) {
1775 /* This is an ON, so we notice the time and save it. */
1776 term->rvbell_startpoint = ticks;
1778 term->rvideo = state;
1779 term->seen_disp_event = TRUE;
1783 case 6: /* DECOM: DEC origin mode */
1784 term->dec_om = state;
1786 case 7: /* DECAWM: auto wrap */
1789 case 8: /* DECARM: auto key repeat */
1790 term->repeat_off = !state;
1792 case 10: /* DECEDM: set local edit mode */
1793 term->term_editing = state;
1794 if (term->ldisc) /* cause ldisc to notice changes */
1795 ldisc_send(term->ldisc, NULL, 0, 0);
1797 case 25: /* DECTCEM: enable/disable cursor */
1798 compatibility2(OTHER, VT220);
1799 term->cursor_on = state;
1800 term->seen_disp_event = TRUE;
1802 case 47: /* alternate screen */
1803 compatibility(OTHER);
1805 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1808 case 1000: /* xterm mouse 1 */
1809 term->xterm_mouse = state ? 1 : 0;
1810 set_raw_mouse_mode(term->frontend, state);
1812 case 1002: /* xterm mouse 2 */
1813 term->xterm_mouse = state ? 2 : 0;
1814 set_raw_mouse_mode(term->frontend, state);
1816 case 1047: /* alternate screen */
1817 compatibility(OTHER);
1819 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1822 case 1048: /* save/restore cursor */
1823 if (!term->cfg.no_alt_screen)
1824 save_cursor(term, state);
1825 if (!state) term->seen_disp_event = TRUE;
1827 case 1049: /* cursor & alternate screen */
1828 if (state && !term->cfg.no_alt_screen)
1829 save_cursor(term, state);
1830 if (!state) term->seen_disp_event = TRUE;
1831 compatibility(OTHER);
1833 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1834 if (!state && !term->cfg.no_alt_screen)
1835 save_cursor(term, state);
1840 case 4: /* IRM: set insert mode */
1841 compatibility(VT102);
1842 term->insert = state;
1844 case 12: /* SRM: set echo mode */
1845 term->term_echoing = !state;
1846 if (term->ldisc) /* cause ldisc to notice changes */
1847 ldisc_send(term->ldisc, NULL, 0, 0);
1849 case 20: /* LNM: Return sends ... */
1850 term->cr_lf_return = state;
1852 case 34: /* WYULCURM: Make cursor BIG */
1853 compatibility2(OTHER, VT220);
1854 term->big_cursor = !state;
1859 * Process an OSC sequence: set window title or icon name.
1861 static void do_osc(Terminal *term)
1864 while (term->osc_strlen--)
1865 term->wordness[(unsigned char)
1866 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1868 term->osc_string[term->osc_strlen] = '\0';
1869 switch (term->esc_args[0]) {
1872 if (!term->cfg.no_remote_wintitle)
1873 set_icon(term->frontend, term->osc_string);
1874 if (term->esc_args[0] == 1)
1876 /* fall through: parameter 0 means set both */
1879 if (!term->cfg.no_remote_wintitle)
1880 set_title(term->frontend, term->osc_string);
1887 * ANSI printing routines.
1889 static void term_print_setup(Terminal *term)
1891 bufchain_clear(&term->printer_buf);
1892 term->print_job = printer_start_job(term->cfg.printer);
1894 static void term_print_flush(Terminal *term)
1899 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1900 bufchain_prefix(&term->printer_buf, &data, &len);
1903 printer_job_data(term->print_job, data, len);
1904 bufchain_consume(&term->printer_buf, len);
1907 static void term_print_finish(Terminal *term)
1913 if (!term->printing && !term->only_printing)
1914 return; /* we need do nothing */
1916 term_print_flush(term);
1917 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1918 bufchain_prefix(&term->printer_buf, &data, &len);
1920 if (c == '\033' || c == '\233') {
1921 bufchain_consume(&term->printer_buf, size);
1924 printer_job_data(term->print_job, &c, 1);
1925 bufchain_consume(&term->printer_buf, 1);
1928 printer_finish_job(term->print_job);
1929 term->print_job = NULL;
1930 term->printing = term->only_printing = FALSE;
1934 * Remove everything currently in `inbuf' and stick it up on the
1935 * in-memory display. There's a big state machine in here to
1936 * process escape sequences...
1938 void term_out(Terminal *term)
1942 unsigned char localbuf[256], *chars;
1947 chars = NULL; /* placate compiler warnings */
1948 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1952 bufchain_prefix(&term->inbuf, &ret, &nchars);
1953 if (nchars > sizeof(localbuf))
1954 nchars = sizeof(localbuf);
1955 memcpy(localbuf, ret, nchars);
1956 bufchain_consume(&term->inbuf, nchars);
1958 assert(chars != NULL);
1964 * Optionally log the session traffic to a file. Useful for
1965 * debugging and possibly also useful for actual logging.
1967 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1968 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1974 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1975 * be able to display 8-bit characters, but I'll let that go 'cause
1980 * If we're printing, add the character to the printer
1983 if (term->printing) {
1984 bufchain_add(&term->printer_buf, &c, 1);
1987 * If we're in print-only mode, we use a much simpler
1988 * state machine designed only to recognise the ESC[4i
1989 * termination sequence.
1991 if (term->only_printing) {
1993 term->print_state = 1;
1994 else if (c == (unsigned char)'\233')
1995 term->print_state = 2;
1996 else if (c == '[' && term->print_state == 1)
1997 term->print_state = 2;
1998 else if (c == '4' && term->print_state == 2)
1999 term->print_state = 3;
2000 else if (c == 'i' && term->print_state == 3)
2001 term->print_state = 4;
2003 term->print_state = 0;
2004 if (term->print_state == 4) {
2005 term_print_finish(term);
2011 /* First see about all those translations. */
2012 if (term->termstate == TOPLEVEL) {
2014 switch (term->utf_state) {
2017 /* UTF-8 must be stateless so we ignore iso2022. */
2018 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2019 c = term->ucsdata->unitab_ctrl[c];
2020 else c = ((unsigned char)c) | CSET_ASCII;
2022 } else if ((c & 0xe0) == 0xc0) {
2023 term->utf_size = term->utf_state = 1;
2024 term->utf_char = (c & 0x1f);
2025 } else if ((c & 0xf0) == 0xe0) {
2026 term->utf_size = term->utf_state = 2;
2027 term->utf_char = (c & 0x0f);
2028 } else if ((c & 0xf8) == 0xf0) {
2029 term->utf_size = term->utf_state = 3;
2030 term->utf_char = (c & 0x07);
2031 } else if ((c & 0xfc) == 0xf8) {
2032 term->utf_size = term->utf_state = 4;
2033 term->utf_char = (c & 0x03);
2034 } else if ((c & 0xfe) == 0xfc) {
2035 term->utf_size = term->utf_state = 5;
2036 term->utf_char = (c & 0x01);
2047 if ((c & 0xC0) != 0x80) {
2050 term->utf_state = 0;
2053 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2054 if (--term->utf_state)
2059 /* Is somebody trying to be evil! */
2061 (c < 0x800 && term->utf_size >= 2) ||
2062 (c < 0x10000 && term->utf_size >= 3) ||
2063 (c < 0x200000 && term->utf_size >= 4) ||
2064 (c < 0x4000000 && term->utf_size >= 5))
2067 /* Unicode line separator and paragraph separator are CR-LF */
2068 if (c == 0x2028 || c == 0x2029)
2071 /* High controls are probably a Baaad idea too. */
2075 /* The UTF-16 surrogates are not nice either. */
2076 /* The standard give the option of decoding these:
2077 * I don't want to! */
2078 if (c >= 0xD800 && c < 0xE000)
2081 /* ISO 10646 characters now limited to UTF-16 range. */
2085 /* This is currently a TagPhobic application.. */
2086 if (c >= 0xE0000 && c <= 0xE007F)
2089 /* U+FEFF is best seen as a null. */
2092 /* But U+FFFE is an error. */
2093 if (c == 0xFFFE || c == 0xFFFF)
2098 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2099 else if(term->sco_acs &&
2100 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
2102 if (term->sco_acs == 2) c |= 0x80;
2105 switch (term->cset_attr[term->cset]) {
2107 * Linedraw characters are different from 'ESC ( B'
2108 * only for a small range. For ones outside that
2109 * range, make sure we use the same font as well as
2110 * the same encoding.
2113 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2114 c = term->ucsdata->unitab_ctrl[c];
2116 c = ((unsigned char) c) | CSET_LINEDRW;
2120 /* If UK-ASCII, make the '#' a LineDraw Pound */
2122 c = '}' | CSET_LINEDRW;
2125 /*FALLTHROUGH*/ case CSET_ASCII:
2126 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2127 c = term->ucsdata->unitab_ctrl[c];
2129 c = ((unsigned char) c) | CSET_ASCII;
2132 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
2138 /* How about C1 controls ? */
2139 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2140 !term->vt52_mode && has_compat(VT220)) {
2141 term->termstate = SEEN_ESC;
2142 term->esc_query = FALSE;
2143 c = '@' + (c & 0x1F);
2146 /* Or the GL control. */
2147 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2148 if (term->curs.x && !term->wrapnext)
2150 term->wrapnext = FALSE;
2152 /* destructive backspace might be disabled */
2153 if (!term->cfg.no_dbackspace) {
2154 term->cpos->chr = ' ' | CSET_ASCII;
2155 term->cpos->attr = term->curr_attr;
2158 /* Or normal C0 controls. */
2159 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
2161 case '\005': /* ENQ: terminal type query */
2162 /* Strictly speaking this is VT100 but a VT100 defaults to
2163 * no response. Other terminals respond at their option.
2165 * Don't put a CR in the default string as this tends to
2166 * upset some weird software.
2168 * An xterm returns "xterm" (5 characters)
2170 compatibility(ANSIMIN);
2172 char abuf[256], *s, *d;
2174 for (s = term->cfg.answerback, d = abuf; *s; s++) {
2176 if (*s >= 'a' && *s <= 'z')
2177 *d++ = (*s - ('a' - 1));
2178 else if ((*s >= '@' && *s <= '_') ||
2179 *s == '?' || (*s & 0x80))
2184 } else if (*s == '^') {
2189 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2193 case '\007': /* BEL: Bell */
2195 struct beeptime *newbeep;
2196 unsigned long ticks;
2198 ticks = GETTICKCOUNT();
2200 if (!term->beep_overloaded) {
2201 newbeep = snew(struct beeptime);
2202 newbeep->ticks = ticks;
2203 newbeep->next = NULL;
2204 if (!term->beephead)
2205 term->beephead = newbeep;
2207 term->beeptail->next = newbeep;
2208 term->beeptail = newbeep;
2213 * Throw out any beeps that happened more than
2216 while (term->beephead &&
2217 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
2218 struct beeptime *tmp = term->beephead;
2219 term->beephead = tmp->next;
2221 if (!term->beephead)
2222 term->beeptail = NULL;
2226 if (term->cfg.bellovl && term->beep_overloaded &&
2227 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
2229 * If we're currently overloaded and the
2230 * last beep was more than s seconds ago,
2231 * leave overload mode.
2233 term->beep_overloaded = FALSE;
2234 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2235 term->nbeeps >= term->cfg.bellovl_n) {
2237 * Now, if we have n or more beeps
2238 * remaining in the queue, go into overload
2241 term->beep_overloaded = TRUE;
2243 term->lastbeep = ticks;
2246 * Perform an actual beep if we're not overloaded.
2248 if (!term->cfg.bellovl || !term->beep_overloaded) {
2249 beep(term->frontend, term->cfg.beep);
2250 if (term->cfg.beep == BELL_VISUAL) {
2251 term->in_vbell = TRUE;
2252 term->vbell_startpoint = ticks;
2256 term->seen_disp_event = TRUE;
2259 case '\b': /* BS: Back space */
2260 if (term->curs.x == 0 &&
2261 (term->curs.y == 0 || term->wrap == 0))
2263 else if (term->curs.x == 0 && term->curs.y > 0)
2264 term->curs.x = term->cols - 1, term->curs.y--;
2265 else if (term->wrapnext)
2266 term->wrapnext = FALSE;
2270 term->seen_disp_event = TRUE;
2272 case '\016': /* LS1: Locking-shift one */
2273 compatibility(VT100);
2276 case '\017': /* LS0: Locking-shift zero */
2277 compatibility(VT100);
2280 case '\033': /* ESC: Escape */
2281 if (term->vt52_mode)
2282 term->termstate = VT52_ESC;
2284 compatibility(ANSIMIN);
2285 term->termstate = SEEN_ESC;
2286 term->esc_query = FALSE;
2289 case '\015': /* CR: Carriage return */
2291 term->wrapnext = FALSE;
2293 term->seen_disp_event = TRUE;
2294 term->paste_hold = 0;
2296 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2298 case '\014': /* FF: Form feed */
2299 if (has_compat(SCOANSI)) {
2300 move(term, 0, 0, 0);
2301 erase_lots(term, FALSE, FALSE, TRUE);
2303 term->wrapnext = FALSE;
2304 term->seen_disp_event = 1;
2307 case '\013': /* VT: Line tabulation */
2308 compatibility(VT100);
2309 case '\012': /* LF: Line feed */
2310 if (term->curs.y == term->marg_b)
2311 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2312 else if (term->curs.y < term->rows - 1)
2314 if (term->cfg.lfhascr)
2317 term->wrapnext = FALSE;
2318 term->seen_disp_event = 1;
2319 term->paste_hold = 0;
2321 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2323 case '\t': /* HT: Character tabulation */
2325 pos old_curs = term->curs;
2326 termline *ldata = scrlineptr(term->curs.y);
2330 } while (term->curs.x < term->cols - 1 &&
2331 !term->tabs[term->curs.x]);
2333 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
2334 if (term->curs.x >= term->cols / 2)
2335 term->curs.x = term->cols / 2 - 1;
2337 if (term->curs.x >= term->cols)
2338 term->curs.x = term->cols - 1;
2342 check_selection(term, old_curs, term->curs);
2344 term->seen_disp_event = TRUE;
2348 switch (term->termstate) {
2350 /* Only graphic characters get this far;
2351 * ctrls are stripped above */
2352 if (term->wrapnext && term->wrap) {
2353 scrlineptr(term->curs.y)->lattr |= LATTR_WRAPPED;
2354 if (term->curs.y == term->marg_b)
2355 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2356 else if (term->curs.y < term->rows - 1)
2360 term->wrapnext = FALSE;
2364 if (term->selstate != NO_SELECTION) {
2365 pos cursplus = term->curs;
2367 check_selection(term, term->curs, cursplus);
2369 if (((c & CSET_MASK) == CSET_ASCII || (c & CSET_MASK) == 0) &&
2371 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
2377 width = wcwidth((wchar_t) c);
2381 * If we're about to display a double-width
2382 * character starting in the rightmost
2383 * column, then we do something special
2384 * instead. We must print a space in the
2385 * last column of the screen, then wrap;
2386 * and we also set LATTR_WRAPPED2 which
2387 * instructs subsequent cut-and-pasting not
2388 * only to splice this line to the one
2389 * after it, but to ignore the space in the
2390 * last character position as well.
2391 * (Because what was actually output to the
2392 * terminal was presumably just a sequence
2393 * of CJK characters, and we don't want a
2394 * space to be pasted in the middle of
2395 * those just because they had the
2396 * misfortune to start in the wrong parity
2397 * column. xterm concurs.)
2399 check_boundary(term, term->curs.x, term->curs.y);
2400 check_boundary(term, term->curs.x+2, term->curs.y);
2401 if (term->curs.x == term->cols-1) {
2402 term->cpos->chr = CSET_ASCII | ' ';
2403 term->cpos->attr = term->curr_attr;
2404 scrlineptr(term->curs.y)->lattr |=
2405 LATTR_WRAPPED | LATTR_WRAPPED2;
2406 if (term->curs.y == term->marg_b)
2407 scroll(term, term->marg_t, term->marg_b,
2409 else if (term->curs.y < term->rows - 1)
2413 /* Now we must check_boundary again, of course. */
2414 check_boundary(term, term->curs.x, term->curs.y);
2415 check_boundary(term, term->curs.x+2, term->curs.y);
2417 term->cpos->chr = c;
2418 term->cpos->attr = term->curr_attr;
2420 term->cpos->chr = UCSWIDE;
2421 term->cpos->attr = term->curr_attr;
2425 check_boundary(term, term->curs.x, term->curs.y);
2426 check_boundary(term, term->curs.x+1, term->curs.y);
2427 term->cpos->chr = c;
2428 term->cpos->attr = term->curr_attr;
2436 if (term->curs.x == term->cols) {
2439 term->wrapnext = TRUE;
2440 if (term->wrap && term->vt52_mode) {
2441 scrlineptr(term->curs.y)->lattr |= LATTR_WRAPPED;
2442 if (term->curs.y == term->marg_b)
2443 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2444 else if (term->curs.y < term->rows - 1)
2448 term->wrapnext = FALSE;
2451 term->seen_disp_event = 1;
2456 * This state is virtually identical to SEEN_ESC, with the
2457 * exception that we have an OSC sequence in the pipeline,
2458 * and _if_ we see a backslash, we process it.
2462 term->termstate = TOPLEVEL;
2465 /* else fall through */
2467 if (c >= ' ' && c <= '/') {
2468 if (term->esc_query)
2469 term->esc_query = -1;
2471 term->esc_query = c;
2474 term->termstate = TOPLEVEL;
2475 switch (ANSI(c, term->esc_query)) {
2476 case '[': /* enter CSI mode */
2477 term->termstate = SEEN_CSI;
2478 term->esc_nargs = 1;
2479 term->esc_args[0] = ARG_DEFAULT;
2480 term->esc_query = FALSE;
2482 case ']': /* OSC: xterm escape sequences */
2483 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2484 compatibility(OTHER);
2485 term->termstate = SEEN_OSC;
2486 term->esc_args[0] = 0;
2488 case '7': /* DECSC: save cursor */
2489 compatibility(VT100);
2490 save_cursor(term, TRUE);
2492 case '8': /* DECRC: restore cursor */
2493 compatibility(VT100);
2494 save_cursor(term, FALSE);
2495 term->seen_disp_event = TRUE;
2497 case '=': /* DECKPAM: Keypad application mode */
2498 compatibility(VT100);
2499 term->app_keypad_keys = TRUE;
2501 case '>': /* DECKPNM: Keypad numeric mode */
2502 compatibility(VT100);
2503 term->app_keypad_keys = FALSE;
2505 case 'D': /* IND: exactly equivalent to LF */
2506 compatibility(VT100);
2507 if (term->curs.y == term->marg_b)
2508 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2509 else if (term->curs.y < term->rows - 1)
2512 term->wrapnext = FALSE;
2513 term->seen_disp_event = TRUE;
2515 case 'E': /* NEL: exactly equivalent to CR-LF */
2516 compatibility(VT100);
2518 if (term->curs.y == term->marg_b)
2519 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2520 else if (term->curs.y < term->rows - 1)
2523 term->wrapnext = FALSE;
2524 term->seen_disp_event = TRUE;
2526 case 'M': /* RI: reverse index - backwards LF */
2527 compatibility(VT100);
2528 if (term->curs.y == term->marg_t)
2529 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2530 else if (term->curs.y > 0)
2533 term->wrapnext = FALSE;
2534 term->seen_disp_event = TRUE;
2536 case 'Z': /* DECID: terminal type query */
2537 compatibility(VT100);
2539 ldisc_send(term->ldisc, term->id_string,
2540 strlen(term->id_string), 0);
2542 case 'c': /* RIS: restore power-on settings */
2543 compatibility(VT100);
2545 if (term->ldisc) /* cause ldisc to notice changes */
2546 ldisc_send(term->ldisc, NULL, 0, 0);
2547 if (term->reset_132) {
2548 if (!term->cfg.no_remote_resize)
2549 request_resize(term->frontend, 80, term->rows);
2550 term->reset_132 = 0;
2554 term->seen_disp_event = TRUE;
2556 case 'H': /* HTS: set a tab */
2557 compatibility(VT100);
2558 term->tabs[term->curs.x] = TRUE;
2561 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2562 compatibility(VT100);
2568 for (i = 0; i < term->rows; i++) {
2569 ldata = scrlineptr(i);
2570 for (j = 0; j < term->cols; j++) {
2571 ldata->chars[j] = term->basic_erase_char;
2572 ldata->chars[j].chr = 'E';
2574 ldata->lattr = LATTR_NORM;
2577 term->seen_disp_event = TRUE;
2578 scrtop.x = scrtop.y = 0;
2580 scrbot.y = term->rows;
2581 check_selection(term, scrtop, scrbot);
2585 case ANSI('3', '#'):
2586 case ANSI('4', '#'):
2587 case ANSI('5', '#'):
2588 case ANSI('6', '#'):
2589 compatibility(VT100);
2593 switch (ANSI(c, term->esc_query)) {
2594 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2597 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2600 case ANSI('5', '#'): /* DECSWL: normal */
2601 nlattr = LATTR_NORM;
2603 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2604 nlattr = LATTR_WIDE;
2607 scrlineptr(term->curs.y)->lattr = nlattr;
2610 /* GZD4: G0 designate 94-set */
2611 case ANSI('A', '('):
2612 compatibility(VT100);
2613 if (!term->cfg.no_remote_charset)
2614 term->cset_attr[0] = CSET_GBCHR;
2616 case ANSI('B', '('):
2617 compatibility(VT100);
2618 if (!term->cfg.no_remote_charset)
2619 term->cset_attr[0] = CSET_ASCII;
2621 case ANSI('0', '('):
2622 compatibility(VT100);
2623 if (!term->cfg.no_remote_charset)
2624 term->cset_attr[0] = CSET_LINEDRW;
2626 case ANSI('U', '('):
2627 compatibility(OTHER);
2628 if (!term->cfg.no_remote_charset)
2629 term->cset_attr[0] = CSET_SCOACS;
2631 /* G1D4: G1-designate 94-set */
2632 case ANSI('A', ')'):
2633 compatibility(VT100);
2634 if (!term->cfg.no_remote_charset)
2635 term->cset_attr[1] = CSET_GBCHR;
2637 case ANSI('B', ')'):
2638 compatibility(VT100);
2639 if (!term->cfg.no_remote_charset)
2640 term->cset_attr[1] = CSET_ASCII;
2642 case ANSI('0', ')'):
2643 compatibility(VT100);
2644 if (!term->cfg.no_remote_charset)
2645 term->cset_attr[1] = CSET_LINEDRW;
2647 case ANSI('U', ')'):
2648 compatibility(OTHER);
2649 if (!term->cfg.no_remote_charset)
2650 term->cset_attr[1] = CSET_SCOACS;
2652 /* DOCS: Designate other coding system */
2653 case ANSI('8', '%'): /* Old Linux code */
2654 case ANSI('G', '%'):
2655 compatibility(OTHER);
2656 if (!term->cfg.no_remote_charset)
2659 case ANSI('@', '%'):
2660 compatibility(OTHER);
2661 if (!term->cfg.no_remote_charset)
2667 term->termstate = TOPLEVEL; /* default */
2669 if (term->esc_nargs <= ARGS_MAX) {
2670 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2671 term->esc_args[term->esc_nargs - 1] = 0;
2672 term->esc_args[term->esc_nargs - 1] =
2673 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2675 term->termstate = SEEN_CSI;
2676 } else if (c == ';') {
2677 if (++term->esc_nargs <= ARGS_MAX)
2678 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2679 term->termstate = SEEN_CSI;
2680 } else if (c < '@') {
2681 if (term->esc_query)
2682 term->esc_query = -1;
2684 term->esc_query = TRUE;
2686 term->esc_query = c;
2687 term->termstate = SEEN_CSI;
2689 switch (ANSI(c, term->esc_query)) {
2690 case 'A': /* CUU: move up N lines */
2691 move(term, term->curs.x,
2692 term->curs.y - def(term->esc_args[0], 1), 1);
2693 term->seen_disp_event = TRUE;
2695 case 'e': /* VPR: move down N lines */
2696 compatibility(ANSI);
2698 case 'B': /* CUD: Cursor down */
2699 move(term, term->curs.x,
2700 term->curs.y + def(term->esc_args[0], 1), 1);
2701 term->seen_disp_event = TRUE;
2703 case ANSI('c', '>'): /* DA: report xterm version */
2704 compatibility(OTHER);
2705 /* this reports xterm version 136 so that VIM can
2706 use the drag messages from the mouse reporting */
2708 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2710 case 'a': /* HPR: move right N cols */
2711 compatibility(ANSI);
2713 case 'C': /* CUF: Cursor right */
2714 move(term, term->curs.x + def(term->esc_args[0], 1),
2716 term->seen_disp_event = TRUE;
2718 case 'D': /* CUB: move left N cols */
2719 move(term, term->curs.x - def(term->esc_args[0], 1),
2721 term->seen_disp_event = TRUE;
2723 case 'E': /* CNL: move down N lines and CR */
2724 compatibility(ANSI);
2726 term->curs.y + def(term->esc_args[0], 1), 1);
2727 term->seen_disp_event = TRUE;
2729 case 'F': /* CPL: move up N lines and CR */
2730 compatibility(ANSI);
2732 term->curs.y - def(term->esc_args[0], 1), 1);
2733 term->seen_disp_event = TRUE;
2736 case '`': /* HPA: set horizontal posn */
2737 compatibility(ANSI);
2738 move(term, def(term->esc_args[0], 1) - 1,
2740 term->seen_disp_event = TRUE;
2742 case 'd': /* VPA: set vertical posn */
2743 compatibility(ANSI);
2744 move(term, term->curs.x,
2745 ((term->dec_om ? term->marg_t : 0) +
2746 def(term->esc_args[0], 1) - 1),
2747 (term->dec_om ? 2 : 0));
2748 term->seen_disp_event = TRUE;
2751 case 'f': /* HVP: set horz and vert posns at once */
2752 if (term->esc_nargs < 2)
2753 term->esc_args[1] = ARG_DEFAULT;
2754 move(term, def(term->esc_args[1], 1) - 1,
2755 ((term->dec_om ? term->marg_t : 0) +
2756 def(term->esc_args[0], 1) - 1),
2757 (term->dec_om ? 2 : 0));
2758 term->seen_disp_event = TRUE;
2760 case 'J': /* ED: erase screen or parts of it */
2762 unsigned int i = def(term->esc_args[0], 0) + 1;
2765 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2768 term->seen_disp_event = TRUE;
2770 case 'K': /* EL: erase line or parts of it */
2772 unsigned int i = def(term->esc_args[0], 0) + 1;
2775 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2777 term->seen_disp_event = TRUE;
2779 case 'L': /* IL: insert lines */
2780 compatibility(VT102);
2781 if (term->curs.y <= term->marg_b)
2782 scroll(term, term->curs.y, term->marg_b,
2783 -def(term->esc_args[0], 1), FALSE);
2785 term->seen_disp_event = TRUE;
2787 case 'M': /* DL: delete lines */
2788 compatibility(VT102);
2789 if (term->curs.y <= term->marg_b)
2790 scroll(term, term->curs.y, term->marg_b,
2791 def(term->esc_args[0], 1),
2794 term->seen_disp_event = TRUE;
2796 case '@': /* ICH: insert chars */
2797 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2798 compatibility(VT102);
2799 insch(term, def(term->esc_args[0], 1));
2800 term->seen_disp_event = TRUE;
2802 case 'P': /* DCH: delete chars */
2803 compatibility(VT102);
2804 insch(term, -def(term->esc_args[0], 1));
2805 term->seen_disp_event = TRUE;
2807 case 'c': /* DA: terminal type query */
2808 compatibility(VT100);
2809 /* This is the response for a VT102 */
2811 ldisc_send(term->ldisc, term->id_string,
2812 strlen(term->id_string), 0);
2814 case 'n': /* DSR: cursor position query */
2816 if (term->esc_args[0] == 6) {
2818 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2820 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2821 } else if (term->esc_args[0] == 5) {
2822 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2826 case 'h': /* SM: toggle modes to high */
2828 compatibility(VT100);
2831 for (i = 0; i < term->esc_nargs; i++)
2832 toggle_mode(term, term->esc_args[i],
2833 term->esc_query, TRUE);
2836 case 'i': /* MC: Media copy */
2838 compatibility(VT100);
2840 if (term->esc_nargs != 1) break;
2841 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2842 term->printing = TRUE;
2843 term->only_printing = !term->esc_query;
2844 term->print_state = 0;
2845 term_print_setup(term);
2846 } else if (term->esc_args[0] == 4 &&
2848 term_print_finish(term);
2852 case 'l': /* RM: toggle modes to low */
2854 compatibility(VT100);
2857 for (i = 0; i < term->esc_nargs; i++)
2858 toggle_mode(term, term->esc_args[i],
2859 term->esc_query, FALSE);
2862 case 'g': /* TBC: clear tabs */
2863 compatibility(VT100);
2864 if (term->esc_nargs == 1) {
2865 if (term->esc_args[0] == 0) {
2866 term->tabs[term->curs.x] = FALSE;
2867 } else if (term->esc_args[0] == 3) {
2869 for (i = 0; i < term->cols; i++)
2870 term->tabs[i] = FALSE;
2874 case 'r': /* DECSTBM: set scroll margins */
2875 compatibility(VT100);
2876 if (term->esc_nargs <= 2) {
2878 top = def(term->esc_args[0], 1) - 1;
2879 bot = (term->esc_nargs <= 1
2880 || term->esc_args[1] == 0 ?
2882 def(term->esc_args[1], term->rows)) - 1;
2883 if (bot >= term->rows)
2884 bot = term->rows - 1;
2885 /* VTTEST Bug 9 - if region is less than 2 lines
2886 * don't change region.
2888 if (bot - top > 0) {
2893 * I used to think the cursor should be
2894 * placed at the top of the newly marginned
2895 * area. Apparently not: VMS TPU falls over
2898 * Well actually it should for
2901 term->curs.y = (term->dec_om ?
2904 term->seen_disp_event = TRUE;
2908 case 'm': /* SGR: set graphics rendition */
2911 * A VT100 without the AVO only had one
2912 * attribute, either underline or
2913 * reverse video depending on the
2914 * cursor type, this was selected by
2918 * This is sometimes DIM, eg on the
2921 * This is sometimes INVIS various ANSI.
2923 * This like 22 disables BOLD, DIM and INVIS
2925 * The ANSI colours appear on any
2926 * terminal that has colour (obviously)
2927 * but the interaction between sgr0 and
2928 * the colours varies but is usually
2929 * related to the background colour
2930 * erase item. The interaction between
2931 * colour attributes and the mono ones
2932 * is also very implementation
2935 * The 39 and 49 attributes are likely
2936 * to be unimplemented.
2939 for (i = 0; i < term->esc_nargs; i++) {
2940 switch (def(term->esc_args[i], 0)) {
2941 case 0: /* restore defaults */
2942 term->curr_attr = term->default_attr;
2944 case 1: /* enable bold */
2945 compatibility(VT100AVO);
2946 term->curr_attr |= ATTR_BOLD;
2948 case 21: /* (enable double underline) */
2949 compatibility(OTHER);
2950 case 4: /* enable underline */
2951 compatibility(VT100AVO);
2952 term->curr_attr |= ATTR_UNDER;
2954 case 5: /* enable blink */
2955 compatibility(VT100AVO);
2956 term->curr_attr |= ATTR_BLINK;
2958 case 6: /* SCO light bkgrd */
2959 compatibility(SCOANSI);
2960 term->blink_is_real = FALSE;
2961 term->curr_attr |= ATTR_BLINK;
2963 case 7: /* enable reverse video */
2964 term->curr_attr |= ATTR_REVERSE;
2966 case 10: /* SCO acs off */
2967 compatibility(SCOANSI);
2968 if (term->cfg.no_remote_charset) break;
2969 term->sco_acs = 0; break;
2970 case 11: /* SCO acs on */
2971 compatibility(SCOANSI);
2972 if (term->cfg.no_remote_charset) break;
2973 term->sco_acs = 1; break;
2974 case 12: /* SCO acs on, |0x80 */
2975 compatibility(SCOANSI);
2976 if (term->cfg.no_remote_charset) break;
2977 term->sco_acs = 2; break;
2978 case 22: /* disable bold */
2979 compatibility2(OTHER, VT220);
2980 term->curr_attr &= ~ATTR_BOLD;
2982 case 24: /* disable underline */
2983 compatibility2(OTHER, VT220);
2984 term->curr_attr &= ~ATTR_UNDER;
2986 case 25: /* disable blink */
2987 compatibility2(OTHER, VT220);
2988 term->curr_attr &= ~ATTR_BLINK;
2990 case 27: /* disable reverse video */
2991 compatibility2(OTHER, VT220);
2992 term->curr_attr &= ~ATTR_REVERSE;
3003 term->curr_attr &= ~ATTR_FGMASK;
3005 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
3015 /* xterm-style bright foreground */
3016 term->curr_attr &= ~ATTR_FGMASK;
3018 ((term->esc_args[i] - 90 + 16)
3021 case 39: /* default-foreground */
3022 term->curr_attr &= ~ATTR_FGMASK;
3023 term->curr_attr |= ATTR_DEFFG;
3034 term->curr_attr &= ~ATTR_BGMASK;
3036 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
3046 /* xterm-style bright background */
3047 term->curr_attr &= ~ATTR_BGMASK;
3049 ((term->esc_args[i] - 100 + 16)
3052 case 49: /* default-background */
3053 term->curr_attr &= ~ATTR_BGMASK;
3054 term->curr_attr |= ATTR_DEFBG;
3058 set_erase_char(term);
3061 case 's': /* save cursor */
3062 save_cursor(term, TRUE);
3064 case 'u': /* restore cursor */
3065 save_cursor(term, FALSE);
3066 term->seen_disp_event = TRUE;
3068 case 't': /* DECSLPP: set page size - ie window height */
3070 * VT340/VT420 sequence DECSLPP, DEC only allows values
3071 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3072 * illegal values (eg first arg 1..9) for window changing
3075 if (term->esc_nargs <= 1
3076 && (term->esc_args[0] < 1 ||
3077 term->esc_args[0] >= 24)) {
3078 compatibility(VT340TEXT);
3079 if (!term->cfg.no_remote_resize)
3080 request_resize(term->frontend, term->cols,
3081 def(term->esc_args[0], 24));
3083 } else if (term->esc_nargs >= 1 &&
3084 term->esc_args[0] >= 1 &&
3085 term->esc_args[0] < 24) {
3086 compatibility(OTHER);
3088 switch (term->esc_args[0]) {
3092 set_iconic(term->frontend, FALSE);
3095 set_iconic(term->frontend, TRUE);
3098 if (term->esc_nargs >= 3) {
3099 if (!term->cfg.no_remote_resize)
3100 move_window(term->frontend,
3101 def(term->esc_args[1], 0),
3102 def(term->esc_args[2], 0));
3106 /* We should resize the window to a given
3107 * size in pixels here, but currently our
3108 * resizing code isn't healthy enough to
3113 set_zorder(term->frontend, TRUE);
3116 /* move to bottom */
3117 set_zorder(term->frontend, FALSE);
3120 refresh_window(term->frontend);
3123 if (term->esc_nargs >= 3) {
3124 if (!term->cfg.no_remote_resize)
3125 request_resize(term->frontend,
3126 def(term->esc_args[2], term->cfg.width),
3127 def(term->esc_args[1], term->cfg.height));
3131 if (term->esc_nargs >= 2)
3132 set_zoomed(term->frontend,
3138 ldisc_send(term->ldisc,
3139 is_iconic(term->frontend) ?
3140 "\033[1t" : "\033[2t", 4, 0);
3144 get_window_pos(term->frontend, &x, &y);
3145 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3146 ldisc_send(term->ldisc, buf, len, 0);
3151 get_window_pixels(term->frontend, &x, &y);
3152 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3153 ldisc_send(term->ldisc, buf, len, 0);
3158 len = sprintf(buf, "\033[8;%d;%dt",
3159 term->rows, term->cols);
3160 ldisc_send(term->ldisc, buf, len, 0);
3165 * Hmmm. Strictly speaking we
3166 * should return `the size of the
3167 * screen in characters', but
3168 * that's not easy: (a) window
3169 * furniture being what it is it's
3170 * hard to compute, and (b) in
3171 * resize-font mode maximising the
3172 * window wouldn't change the
3173 * number of characters. *shrug*. I
3174 * think we'll ignore it for the
3175 * moment and see if anyone
3176 * complains, and then ask them
3177 * what they would like it to do.
3182 !term->cfg.no_remote_qtitle) {
3183 p = get_window_title(term->frontend, TRUE);
3185 ldisc_send(term->ldisc, "\033]L", 3, 0);
3186 ldisc_send(term->ldisc, p, len, 0);
3187 ldisc_send(term->ldisc, "\033\\", 2, 0);
3192 !term->cfg.no_remote_qtitle) {
3193 p = get_window_title(term->frontend,FALSE);
3195 ldisc_send(term->ldisc, "\033]l", 3, 0);
3196 ldisc_send(term->ldisc, p, len, 0);
3197 ldisc_send(term->ldisc, "\033\\", 2, 0);
3203 case 'S': /* SU: Scroll up */
3204 compatibility(SCOANSI);
3205 scroll(term, term->marg_t, term->marg_b,
3206 def(term->esc_args[0], 1), TRUE);
3208 term->wrapnext = FALSE;
3209 term->seen_disp_event = TRUE;
3211 case 'T': /* SD: Scroll down */
3212 compatibility(SCOANSI);
3213 scroll(term, term->marg_t, term->marg_b,
3214 -def(term->esc_args[0], 1), TRUE);
3216 term->wrapnext = FALSE;
3217 term->seen_disp_event = TRUE;
3219 case ANSI('|', '*'): /* DECSNLS */
3221 * Set number of lines on screen
3222 * VT420 uses VGA like hardware and can
3223 * support any size in reasonable range
3224 * (24..49 AIUI) with no default specified.
3226 compatibility(VT420);
3227 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
3228 if (!term->cfg.no_remote_resize)
3229 request_resize(term->frontend, term->cols,
3230 def(term->esc_args[0],
3235 case ANSI('|', '$'): /* DECSCPP */
3237 * Set number of columns per page
3238 * Docs imply range is only 80 or 132, but
3241 compatibility(VT340TEXT);
3242 if (term->esc_nargs <= 1) {
3243 if (!term->cfg.no_remote_resize)
3244 request_resize(term->frontend,
3245 def(term->esc_args[0],
3246 term->cfg.width), term->rows);
3250 case 'X': /* ECH: write N spaces w/o moving cursor */
3251 /* XXX VTTEST says this is vt220, vt510 manual
3253 compatibility(ANSIMIN);
3255 int n = def(term->esc_args[0], 1);
3257 termchar *p = term->cpos;
3258 if (n > term->cols - term->curs.x)
3259 n = term->cols - term->curs.x;
3260 cursplus = term->curs;
3262 check_boundary(term, term->curs.x, term->curs.y);
3263 check_boundary(term, term->curs.x+n, term->curs.y);
3264 check_selection(term, term->curs, cursplus);
3266 *p++ = term->erase_char;
3267 term->seen_disp_event = TRUE;
3270 case 'x': /* DECREQTPARM: report terminal characteristics */
3271 compatibility(VT100);
3274 int i = def(term->esc_args[0], 0);
3275 if (i == 0 || i == 1) {
3276 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3278 ldisc_send(term->ldisc, buf, 20, 0);
3282 case 'Z': /* CBT: BackTab for xterm */
3283 compatibility(OTHER);
3285 int i = def(term->esc_args[0], 1);
3286 pos old_curs = term->curs;
3288 for(;i>0 && term->curs.x>0; i--) {
3291 } while (term->curs.x >0 &&
3292 !term->tabs[term->curs.x]);
3295 check_selection(term, old_curs, term->curs);
3298 case ANSI('c', '='): /* Hide or Show Cursor */
3299 compatibility(SCOANSI);
3300 switch(term->esc_args[0]) {
3301 case 0: /* hide cursor */
3302 term->cursor_on = FALSE;
3304 case 1: /* restore cursor */
3305 term->big_cursor = FALSE;
3306 term->cursor_on = TRUE;
3308 case 2: /* block cursor */
3309 term->big_cursor = TRUE;
3310 term->cursor_on = TRUE;
3314 case ANSI('C', '='):
3316 * set cursor start on scanline esc_args[0] and
3317 * end on scanline esc_args[1].If you set
3318 * the bottom scan line to a value less than
3319 * the top scan line, the cursor will disappear.
3321 compatibility(SCOANSI);
3322 if (term->esc_nargs >= 2) {
3323 if (term->esc_args[0] > term->esc_args[1])
3324 term->cursor_on = FALSE;
3326 term->cursor_on = TRUE;
3329 case ANSI('D', '='):
3330 compatibility(SCOANSI);
3331 term->blink_is_real = FALSE;
3332 if (term->esc_args[0]>=1)
3333 term->curr_attr |= ATTR_BLINK;
3335 term->curr_attr &= ~ATTR_BLINK;
3337 case ANSI('E', '='):
3338 compatibility(SCOANSI);
3339 term->blink_is_real = (term->esc_args[0] >= 1);
3341 case ANSI('F', '='): /* set normal foreground */
3342 compatibility(SCOANSI);
3343 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3345 (sco2ansicolour[term->esc_args[0] & 0x7] |
3346 ((term->esc_args[0] & 0x8) << 1)) <<
3348 term->curr_attr &= ~ATTR_FGMASK;
3349 term->curr_attr |= colour;
3350 term->default_attr &= ~ATTR_FGMASK;
3351 term->default_attr |= colour;
3354 case ANSI('G', '='): /* set normal background */
3355 compatibility(SCOANSI);
3356 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
3358 (sco2ansicolour[term->esc_args[0] & 0x7] |
3359 ((term->esc_args[0] & 0x8) << 1)) <<
3361 term->curr_attr &= ~ATTR_BGMASK;
3362 term->curr_attr |= colour;
3363 term->default_attr &= ~ATTR_BGMASK;
3364 term->default_attr |= colour;
3367 case ANSI('L', '='):
3368 compatibility(SCOANSI);
3369 term->use_bce = (term->esc_args[0] <= 0);
3370 set_erase_char(term);
3372 case ANSI('p', '"'): /* DECSCL: set compat level */
3374 * Allow the host to make this emulator a
3375 * 'perfect' VT102. This first appeared in
3376 * the VT220, but we do need to get back to
3377 * PuTTY mode so I won't check it.
3379 * The arg in 40..42,50 are a PuTTY extension.
3380 * The 2nd arg, 8bit vs 7bit is not checked.
3382 * Setting VT102 mode should also change
3383 * the Fkeys to generate PF* codes as a
3384 * real VT102 has no Fkeys. The VT220 does
3385 * this, F11..F13 become ESC,BS,LF other
3386 * Fkeys send nothing.
3388 * Note ESC c will NOT change this!
3391 switch (term->esc_args[0]) {
3393 term->compatibility_level &= ~TM_VTXXX;
3394 term->compatibility_level |= TM_VT102;
3397 term->compatibility_level &= ~TM_VTXXX;
3398 term->compatibility_level |= TM_VT220;
3402 if (term->esc_args[0] > 60 &&
3403 term->esc_args[0] < 70)
3404 term->compatibility_level |= TM_VTXXX;
3408 term->compatibility_level &= TM_VTXXX;
3411 term->compatibility_level = TM_PUTTY;
3414 term->compatibility_level = TM_SCOANSI;
3418 term->compatibility_level = TM_PUTTY;
3424 /* Change the response to CSI c */
3425 if (term->esc_args[0] == 50) {
3428 strcpy(term->id_string, "\033[?");
3429 for (i = 1; i < term->esc_nargs; i++) {
3431 strcat(term->id_string, ";");
3432 sprintf(lbuf, "%d", term->esc_args[i]);
3433 strcat(term->id_string, lbuf);
3435 strcat(term->id_string, "c");
3438 /* Is this a good idea ?
3439 * Well we should do a soft reset at this point ...
3441 if (!has_compat(VT420) && has_compat(VT100)) {
3442 if (!term->cfg.no_remote_resize) {
3443 if (term->reset_132)
3444 request_resize(132, 24);
3446 request_resize(80, 24);
3454 term->osc_w = FALSE;
3456 case 'P': /* Linux palette sequence */
3457 term->termstate = SEEN_OSC_P;
3458 term->osc_strlen = 0;
3460 case 'R': /* Linux palette reset */
3461 palette_reset(term->frontend);
3462 term_invalidate(term);
3463 term->termstate = TOPLEVEL;
3465 case 'W': /* word-set */
3466 term->termstate = SEEN_OSC_W;
3479 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3483 * Grotty hack to support xterm and DECterm title
3484 * sequences concurrently.
3486 if (term->esc_args[0] == 2) {
3487 term->esc_args[0] = 1;
3490 /* else fall through */
3492 term->termstate = OSC_STRING;
3493 term->osc_strlen = 0;
3498 * This OSC stuff is EVIL. It takes just one character to get into
3499 * sysline mode and it's not initially obvious how to get out.
3500 * So I've added CR and LF as string aborts.
3501 * This shouldn't effect compatibility as I believe embedded
3502 * control characters are supposed to be interpreted (maybe?)
3503 * and they don't display anything useful anyway.
3507 if (c == '\012' || c == '\015') {
3508 term->termstate = TOPLEVEL;
3509 } else if (c == 0234 || c == '\007') {
3511 * These characters terminate the string; ST and BEL
3512 * terminate the sequence and trigger instant
3513 * processing of it, whereas ESC goes back to SEEN_ESC
3514 * mode unless it is followed by \, in which case it is
3515 * synonymous with ST in the first place.
3518 term->termstate = TOPLEVEL;
3519 } else if (c == '\033')
3520 term->termstate = OSC_MAYBE_ST;
3521 else if (term->osc_strlen < OSC_STR_MAX)
3522 term->osc_string[term->osc_strlen++] = (char)c;
3526 int max = (term->osc_strlen == 0 ? 21 : 16);
3528 if ((int)c >= '0' && (int)c <= '9')
3530 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
3532 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
3535 term->termstate = TOPLEVEL;
3538 term->osc_string[term->osc_strlen++] = val;
3539 if (term->osc_strlen >= 7) {
3540 palette_set(term->frontend, term->osc_string[0],
3541 term->osc_string[1] * 16 + term->osc_string[2],
3542 term->osc_string[3] * 16 + term->osc_string[4],
3543 term->osc_string[5] * 16 + term->osc_string[6]);
3544 term_invalidate(term);
3545 term->termstate = TOPLEVEL;
3561 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3564 term->termstate = OSC_STRING;
3565 term->osc_strlen = 0;
3569 term->termstate = TOPLEVEL;
3570 term->seen_disp_event = TRUE;
3573 move(term, term->curs.x, term->curs.y - 1, 1);
3576 move(term, term->curs.x, term->curs.y + 1, 1);
3579 move(term, term->curs.x + 1, term->curs.y, 1);
3582 move(term, term->curs.x - 1, term->curs.y, 1);
3585 * From the VT100 Manual
3586 * NOTE: The special graphics characters in the VT100
3587 * are different from those in the VT52
3589 * From VT102 manual:
3590 * 137 _ Blank - Same
3591 * 140 ` Reserved - Humm.
3592 * 141 a Solid rectangle - Similar
3593 * 142 b 1/ - Top half of fraction for the
3594 * 143 c 3/ - subscript numbers below.
3597 * 146 f Degrees - Same
3598 * 147 g Plus or minus - Same
3600 * 151 i Ellipsis (dots)
3603 * 154 l Bar at scan 0
3604 * 155 m Bar at scan 1
3605 * 156 n Bar at scan 2
3606 * 157 o Bar at scan 3 - Similar
3607 * 160 p Bar at scan 4 - Similar
3608 * 161 q Bar at scan 5 - Similar
3609 * 162 r Bar at scan 6 - Same
3610 * 163 s Bar at scan 7 - Similar
3625 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
3628 term->cset_attr[term->cset = 0] = CSET_ASCII;
3631 move(term, 0, 0, 0);
3634 if (term->curs.y == 0)
3635 scroll(term, 0, term->rows - 1, -1, TRUE);
3636 else if (term->curs.y > 0)
3639 term->wrapnext = FALSE;
3642 erase_lots(term, FALSE, FALSE, TRUE);
3646 erase_lots(term, TRUE, FALSE, TRUE);
3650 /* XXX Print cursor line */
3653 /* XXX Start controller mode */
3656 /* XXX Stop controller mode */
3660 term->termstate = VT52_Y1;
3664 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3667 term->app_keypad_keys = TRUE;
3670 term->app_keypad_keys = FALSE;
3673 /* XXX This should switch to VT100 mode not current or default
3674 * VT mode. But this will only have effect in a VT220+
3677 term->vt52_mode = FALSE;
3678 term->blink_is_real = term->cfg.blinktext;
3682 /* XXX Enter auto print mode */
3685 /* XXX Exit auto print mode */
3688 /* XXX Print screen */
3694 /* compatibility(ATARI) */
3695 move(term, 0, 0, 0);
3696 erase_lots(term, FALSE, FALSE, TRUE);
3700 /* compatibility(ATARI) */
3701 if (term->curs.y <= term->marg_b)
3702 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3705 /* compatibility(ATARI) */
3706 if (term->curs.y <= term->marg_b)
3707 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3710 /* compatibility(ATARI) */
3711 term->termstate = VT52_FG;
3714 /* compatibility(ATARI) */
3715 term->termstate = VT52_BG;
3718 /* compatibility(ATARI) */
3719 erase_lots(term, FALSE, TRUE, FALSE);
3723 /* compatibility(ATARI) */
3724 term->cursor_on = TRUE;
3727 /* compatibility(ATARI) */
3728 term->cursor_on = FALSE;
3730 /* case 'j': Save cursor position - broken on ST */
3731 /* case 'k': Restore cursor position */
3733 /* compatibility(ATARI) */
3734 erase_lots(term, TRUE, TRUE, TRUE);
3736 term->wrapnext = FALSE;
3740 /* compatibility(ATARI) */
3741 erase_lots(term, TRUE, TRUE, FALSE);
3744 /* compatibility(ATARI) */
3745 term->curr_attr |= ATTR_REVERSE;
3748 /* compatibility(ATARI) */
3749 term->curr_attr &= ~ATTR_REVERSE;
3751 case 'v': /* wrap Autowrap on - Wyse style */
3752 /* compatibility(ATARI) */
3755 case 'w': /* Autowrap off */
3756 /* compatibility(ATARI) */
3761 /* compatibility(OTHER) */
3762 term->vt52_bold = FALSE;
3763 term->curr_attr = ATTR_DEFAULT;
3764 set_erase_char(term);
3767 /* compatibility(VI50) */
3768 term->curr_attr |= ATTR_UNDER;
3771 /* compatibility(VI50) */
3772 term->curr_attr &= ~ATTR_UNDER;
3775 /* compatibility(VI50) */
3776 term->vt52_bold = TRUE;
3777 term->curr_attr |= ATTR_BOLD;
3780 /* compatibility(VI50) */
3781 term->vt52_bold = FALSE;
3782 term->curr_attr &= ~ATTR_BOLD;
3788 term->termstate = VT52_Y2;
3789 move(term, term->curs.x, c - ' ', 0);
3792 term->termstate = TOPLEVEL;
3793 move(term, c - ' ', term->curs.y, 0);
3798 term->termstate = TOPLEVEL;
3799 term->curr_attr &= ~ATTR_FGMASK;
3800 term->curr_attr &= ~ATTR_BOLD;
3801 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3802 if ((c & 0x8) || term->vt52_bold)
3803 term->curr_attr |= ATTR_BOLD;
3805 set_erase_char(term);
3808 term->termstate = TOPLEVEL;
3809 term->curr_attr &= ~ATTR_BGMASK;
3810 term->curr_attr &= ~ATTR_BLINK;
3811 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3813 /* Note: bold background */
3815 term->curr_attr |= ATTR_BLINK;
3817 set_erase_char(term);
3820 default: break; /* placate gcc warning about enum use */
3822 if (term->selstate != NO_SELECTION) {
3823 pos cursplus = term->curs;
3825 check_selection(term, term->curs, cursplus);
3829 term_print_flush(term);
3830 logflush(term->logctx);
3834 * To prevent having to run the reasonably tricky bidi algorithm
3835 * too many times, we maintain a cache of the last lineful of data
3836 * fed to the algorithm on each line of the display.
3838 static int term_bidi_cache_hit(Terminal *term, int line,
3839 termchar *lbefore, int width)
3841 if (!term->pre_bidi_cache)
3842 return FALSE; /* cache doesn't even exist yet! */
3844 if (line >= term->bidi_cache_size)
3845 return FALSE; /* cache doesn't have this many lines */
3847 if (!term->pre_bidi_cache[line])
3848 return FALSE; /* cache doesn't contain _this_ line */
3850 if (!memcmp(term->pre_bidi_cache[line], lbefore, width * TSIZE))
3851 return TRUE; /* aha! the line matches the cache */
3853 return FALSE; /* it didn't match. */
3856 static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
3857 termchar *lafter, int width)
3859 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
3860 int j = term->bidi_cache_size;
3861 term->bidi_cache_size = line+1;
3862 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
3863 term->bidi_cache_size,
3865 term->post_bidi_cache = sresize(term->post_bidi_cache,
3866 term->bidi_cache_size,
3868 while (j < term->bidi_cache_size) {
3869 term->pre_bidi_cache[j] = term->post_bidi_cache[j] = NULL;
3874 sfree(term->pre_bidi_cache[line]);
3875 sfree(term->post_bidi_cache[line]);
3877 term->pre_bidi_cache[line] = snewn(width, termchar);
3878 term->post_bidi_cache[line] = snewn(width, termchar);
3880 memcpy(term->pre_bidi_cache[line], lbefore, width * TSIZE);
3881 memcpy(term->post_bidi_cache[line], lafter, width * TSIZE);
3885 * Given a context, update the window. Out of paranoia, we don't
3886 * allow WM_PAINT responses to do scrolling optimisations.
3888 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3890 int i, it, j, our_curs_y, our_curs_x;
3894 termchar cursor_background;
3895 unsigned long ticks;
3896 #ifdef OPTIMISE_SCROLL
3897 struct scrollregion *sr;
3898 #endif /* OPTIMISE_SCROLL */
3900 cursor_background = term->basic_erase_char;
3903 * Check the visual bell state.
3905 if (term->in_vbell) {
3906 ticks = GETTICKCOUNT();
3907 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3908 term->in_vbell = FALSE;
3911 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3914 * screen array, disptop, scrtop,
3916 * cfg.blinkpc, blink_is_real, tblinker,
3917 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3920 /* Has the cursor position or type changed ? */
3921 if (term->cursor_on) {
3922 if (term->has_focus) {
3923 if (term->blinker || !term->cfg.blink_cur)
3924 cursor = TATTR_ACTCURS;
3928 cursor = TATTR_PASCURS;
3930 cursor |= TATTR_RIGHTCURS;
3933 our_curs_y = term->curs.y - term->disptop;
3936 * Adjust the cursor position in the case where it's
3937 * resting on the right-hand half of a CJK wide character.
3938 * xterm's behaviour here, which seems adequate to me, is
3939 * to display the cursor covering the _whole_ character,
3940 * exactly as if it were one space to the left.
3942 termline *ldata = lineptr(term->curs.y);
3943 our_curs_x = term->curs.x;
3944 if (our_curs_x > 0 &&
3945 ldata->chars[our_curs_x].chr == UCSWIDE)
3951 * If the cursor is not where it was last time we painted, and
3952 * its previous position is visible on screen, invalidate its
3953 * previous position.
3955 if (term->dispcursy >= 0 &&
3956 (term->curstype != cursor ||
3957 term->dispcursy != our_curs_y ||
3958 term->dispcursx != our_curs_x)) {
3959 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
3962 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
3963 dispcurs[-1].attr |= ATTR_INVALID;
3964 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
3965 dispcurs[1].attr |= ATTR_INVALID;
3966 dispcurs->attr |= ATTR_INVALID;
3970 term->dispcursx = term->dispcursy = -1;
3972 #ifdef OPTIMISE_SCROLL
3974 sr = term->scrollhead;
3976 struct scrollregion *next = sr->next;
3977 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3981 term->scrollhead = term->scrolltail = NULL;
3982 #endif /* OPTIMISE_SCROLL */
3984 /* The normal screen data */
3985 for (i = 0; i < term->rows; i++) {
3988 int dirty_line, dirty_run, selected;
3989 unsigned long attr = 0, cset = 0;
3990 int updated_line = 0;
3993 int last_run_dirty = 0;
3995 scrpos.y = i + term->disptop;
3996 ldata = lineptr(scrpos.y);
3998 dirty_run = dirty_line = (ldata->lattr !=
3999 term->disptext[i]->lattr);
4000 term->disptext[i]->lattr = ldata->lattr;
4002 /* Do Arabic shaping and bidi. */
4003 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4005 if (!term_bidi_cache_hit(term, i, ldata->chars, term->cols)) {
4007 for(it=0; it<term->cols ; it++)
4009 unsigned long uc = (ldata->chars[it].chr);
4011 switch (uc & CSET_MASK) {
4013 if (!term->cfg.rawcnp) {
4014 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4018 uc = term->ucsdata->unitab_line[uc & 0xFF];
4021 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4024 switch (uc & CSET_MASK) {
4026 uc = term->ucsdata->unitab_font[uc & 0xFF];
4029 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4033 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4035 term->wcFrom[it].index = it;
4039 do_bidi(term->wcFrom, term->cols);
4041 /* this is saved iff done from inside the shaping */
4042 if(!term->cfg.bidi && term->cfg.arabicshaping)
4043 for(it=0; it<term->cols; it++)
4044 term->wcTo[it] = term->wcFrom[it];
4046 if(!term->cfg.arabicshaping)
4047 do_shape(term->wcFrom, term->wcTo, term->cols);
4049 for(it=0; it<term->cols ; it++)
4051 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4053 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4054 term->ltemp[it].chr = term->wcTo[it].wc;
4056 term_bidi_cache_store(term, i, ldata->chars,
4057 term->ltemp, term->cols);
4058 lchars = term->ltemp;
4060 lchars = term->post_bidi_cache[i];
4063 lchars = ldata->chars;
4065 for (j = 0; j < term->cols; j++) {
4066 unsigned long tattr, tchar;
4067 termchar *d = lchars + j;
4074 switch (tchar & CSET_MASK) {
4076 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4079 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4082 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
4085 if (j < term->cols-1 && d[1].chr == UCSWIDE)
4088 /* Video reversing things */
4089 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4090 if (term->seltype == LEXICOGRAPHIC)
4091 selected = (posle(term->selstart, scrpos) &&
4092 poslt(scrpos, term->selend));
4094 selected = (posPle(term->selstart, scrpos) &&
4095 posPlt(scrpos, term->selend));
4099 ^ (selected ? ATTR_REVERSE : 0));
4101 /* 'Real' blinking ? */
4102 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4103 if (term->has_focus && term->tblinker) {
4104 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
4106 tattr &= ~ATTR_BLINK;
4110 * Check the font we'll _probably_ be using to see if
4111 * the character is wide when we don't want it to be.
4113 if (tchar != term->disptext[i]->chars[j].chr ||
4114 tattr != (term->disptext[i]->chars[j].attr &~
4116 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
4117 tattr |= ATTR_NARROW;
4118 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
4119 tattr |= ATTR_NARROW;
4121 /* Cursor here ? Save the 'background' */
4122 if (i == our_curs_y && j == our_curs_x) {
4123 cursor_background.chr = tchar;
4124 cursor_background.attr = tattr;
4125 term->dispcursx = j;
4126 term->dispcursy = i;
4129 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4132 break_run = (((tattr ^ attr) & term->attr_mask) ||
4133 j - start >= sizeof(ch));
4135 /* Special hack for VT100 Linedraw glyphs */
4136 if (tchar >= 0x23BA && tchar <= 0x23BD)
4140 * Separate out sequences of characters that have the
4141 * same CSET, if that CSET is a magic one.
4143 if (CSET_OF(tchar) != cset)
4146 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
4147 if (term->disptext[i]->chars[j].chr == tchar &&
4148 term->disptext[i]->chars[j].attr == tattr)
4150 else if (!dirty_run && ccount == 1)
4155 if ((dirty_run || last_run_dirty) && ccount > 0) {
4156 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4162 cset = CSET_OF(tchar);
4163 if (term->ucsdata->dbcs_screenfont)
4164 last_run_dirty = dirty_run;
4165 dirty_run = dirty_line;
4168 if (term->disptext[i]->chars[j].chr != tchar ||
4169 term->disptext[i]->chars[j].attr != tattr)
4171 ch[ccount++] = (wchar_t) tchar;
4172 term->disptext[i]->chars[j].chr = tchar;
4173 term->disptext[i]->chars[j].attr = tattr;
4175 /* If it's a wide char step along to the next one. */
4176 if (tattr & ATTR_WIDE) {
4177 if (++j < term->cols) {
4180 * By construction above, the cursor should not
4181 * be on the right-hand half of this character.
4184 assert(!(i == our_curs_y && j == our_curs_x));
4185 if (memcmp(&term->disptext[i]->chars[j],
4188 term->disptext[i]->chars[j] = *d;
4192 if (dirty_run && ccount > 0) {
4193 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4197 /* Cursor on this line ? (and changed) */
4198 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
4199 ch[0] = (wchar_t) cursor_background.chr;
4200 attr = cursor_background.attr | cursor;
4201 do_cursor(ctx, our_curs_x, i, ch, 1, attr, ldata->lattr);
4202 term->curstype = cursor;
4210 * Flick the switch that says if blinking things should be shown or hidden.
4213 void term_blink(Terminal *term, int flg)
4215 long now, blink_diff;
4217 now = GETTICKCOUNT();
4218 blink_diff = now - term->last_tblink;
4220 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4221 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
4222 term->last_tblink = now;
4223 term->tblinker = !term->tblinker;
4228 term->last_blink = now;
4232 blink_diff = now - term->last_blink;
4234 /* Make sure the cursor blinks no faster than system blink rate */
4235 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
4238 term->last_blink = now;
4239 term->blinker = !term->blinker;
4243 * Invalidate the whole screen so it will be repainted in full.
4245 void term_invalidate(Terminal *term)
4249 for (i = 0; i < term->rows; i++)
4250 for (j = 0; j < term->cols; j++)
4251 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4255 * Paint the window in response to a WM_PAINT message.
4257 void term_paint(Terminal *term, Context ctx,
4258 int left, int top, int right, int bottom, int immediately)
4261 if (left < 0) left = 0;
4262 if (top < 0) top = 0;
4263 if (right >= term->cols) right = term->cols-1;
4264 if (bottom >= term->rows) bottom = term->rows-1;
4266 for (i = top; i <= bottom && i < term->rows; i++) {
4267 if (term->disptext[i]->lattr == LATTR_NORM)
4268 for (j = left; j <= right && j < term->cols; j++)
4269 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4271 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
4272 term->disptext[i]->chars[j].attr = ATTR_INVALID;
4275 /* This should happen soon enough, also for some reason it sometimes
4276 * fails to actually do anything when re-sizing ... painting the wrong
4280 do_paint (term, ctx, FALSE);
4284 * Attempt to scroll the scrollback. The second parameter gives the
4285 * position we want to scroll to; the first is +1 to denote that
4286 * this position is relative to the beginning of the scrollback, -1
4287 * to denote it is relative to the end, and 0 to denote that it is
4288 * relative to the current position.
4290 void term_scroll(Terminal *term, int rel, int where)
4292 int sbtop = -sblines(term);
4293 #ifdef OPTIMISE_SCROLL
4294 int olddisptop = term->disptop;
4296 #endif /* OPTIMISE_SCROLL */
4298 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4299 if (term->disptop < sbtop)
4300 term->disptop = sbtop;
4301 if (term->disptop > 0)
4304 #ifdef OPTIMISE_SCROLL
4305 shift = (term->disptop - olddisptop);
4306 if (shift < term->rows && shift > -term->rows)
4307 scroll_display(term, 0, term->rows - 1, shift);
4308 #endif /* OPTIMISE_SCROLL */
4312 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
4315 wchar_t *wbptr; /* where next char goes within workbuf */
4317 int wblen = 0; /* workbuf len */
4318 int buflen; /* amount of memory allocated to workbuf */
4320 buflen = 5120; /* Default size */
4321 workbuf = snewn(buflen, wchar_t);
4322 wbptr = workbuf; /* start filling here */
4323 old_top_x = top.x; /* needed for rect==1 */
4325 while (poslt(top, bottom)) {
4327 termline *ldata = lineptr(top.y);
4331 * nlpos will point at the maximum position on this line we
4332 * should copy up to. So we start it at the end of the
4336 nlpos.x = term->cols;
4339 * ... move it backwards if there's unused space at the end
4340 * of the line (and also set `nl' if this is the case,
4341 * because in normal selection mode this means we need a
4342 * newline at the end)...
4344 if (!(ldata->lattr & LATTR_WRAPPED)) {
4345 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
4348 if (poslt(nlpos, bottom))
4350 } else if (ldata->lattr & LATTR_WRAPPED2) {
4351 /* Ignore the last char on the line in a WRAPPED2 line. */
4356 * ... and then clip it to the terminal x coordinate if
4357 * we're doing rectangular selection. (In this case we
4358 * still did the above, so that copying e.g. the right-hand
4359 * column from a table doesn't fill with spaces on the
4363 if (nlpos.x > bottom.x)
4365 nl = (top.y < bottom.y);
4368 while (poslt(top, bottom) && poslt(top, nlpos)) {
4371 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4373 wchar_t cbuf[16], *p;
4374 int uc = ldata->chars[top.x].chr;
4377 if (uc == UCSWIDE) {
4382 switch (uc & CSET_MASK) {
4384 if (!term->cfg.rawcnp) {
4385 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4389 uc = term->ucsdata->unitab_line[uc & 0xFF];
4392 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4395 switch (uc & CSET_MASK) {
4397 uc = term->ucsdata->unitab_font[uc & 0xFF];
4400 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4404 set = (uc & CSET_MASK);
4405 c = (uc & ~CSET_MASK);
4409 if (DIRECT_FONT(uc)) {
4410 if (c >= ' ' && c != 0x7F) {
4414 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
4416 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
4417 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
4421 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
4425 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
4432 for (p = cbuf; *p; p++) {
4433 /* Enough overhead for trailing NL and nul */
4434 if (wblen >= buflen - 16) {
4436 workbuf = sresize(workbuf, buflen, wchar_t);
4437 wbptr = workbuf + wblen;
4446 for (i = 0; i < sel_nl_sz; i++) {
4448 *wbptr++ = sel_nl[i];
4452 top.x = rect ? old_top_x : 0;
4456 #if SELECTION_NUL_TERMINATED
4460 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
4461 if (buflen > 0) /* indicates we allocated this buffer */
4465 void term_copyall(Terminal *term)
4469 tree234 *screen = term->screen;
4470 top.y = -sblines(term);
4472 bottom.y = find_last_nonempty_line(term, screen);
4473 bottom.x = term->cols;
4474 clipme(term, top, bottom, 0, TRUE);
4478 * The wordness array is mainly for deciding the disposition of the
4479 * US-ASCII characters.
4481 static int wordtype(Terminal *term, int uc)
4484 int start, end, ctype;
4486 static const struct ucsword ucs_words[] = {
4492 0x037e, 0x037e, 1}, /* Greek question mark */
4494 0x0387, 0x0387, 1}, /* Greek ano teleia */
4496 0x055a, 0x055f, 1}, /* Armenian punctuation */
4498 0x0589, 0x0589, 1}, /* Armenian full stop */
4500 0x0700, 0x070d, 1}, /* Syriac punctuation */
4502 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4504 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4506 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4508 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4510 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4512 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4514 0x2000, 0x200a, 0}, /* Various spaces */
4516 0x2070, 0x207f, 2}, /* superscript */
4518 0x2080, 0x208f, 2}, /* subscript */
4520 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4522 0x3000, 0x3000, 0}, /* ideographic space */
4524 0x3001, 0x3020, 1}, /* ideographic punctuation */
4526 0x303f, 0x309f, 3}, /* Hiragana */
4528 0x30a0, 0x30ff, 3}, /* Katakana */
4530 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4532 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4534 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4536 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4538 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4540 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4542 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4544 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4546 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4550 const struct ucsword *wptr;
4552 switch (uc & CSET_MASK) {
4554 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4557 uc = term->ucsdata->unitab_line[uc & 0xFF];
4560 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4563 switch (uc & CSET_MASK) {
4565 uc = term->ucsdata->unitab_font[uc & 0xFF];
4568 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4572 /* For DBCS fonts I can't do anything useful. Even this will sometimes
4573 * fail as there's such a thing as a double width space. :-(
4575 if (term->ucsdata->dbcs_screenfont &&
4576 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
4580 return term->wordness[uc];
4582 for (wptr = ucs_words; wptr->start; wptr++) {
4583 if (uc >= wptr->start && uc <= wptr->end)
4591 * Spread the selection outwards according to the selection mode.
4593 static pos sel_spread_half(Terminal *term, pos p, int dir)
4597 int topy = -sblines(term);
4599 ldata = lineptr(p.y);
4601 switch (term->selmode) {
4604 * In this mode, every character is a separate unit, except
4605 * for runs of spaces at the end of a non-wrapping line.
4607 if (!(ldata->lattr & LATTR_WRAPPED)) {
4608 termchar *q = ldata->chars + term->cols;
4609 while (q > ldata->chars && IS_SPACE_CHR(q[-1].chr))
4611 if (q == ldata->chars + term->cols)
4613 if (p.x >= q - ldata->chars)
4614 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
4619 * In this mode, the units are maximal runs of characters
4620 * whose `wordness' has the same value.
4622 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
4625 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
4626 term->cols-1 : term->cols);
4627 if (p.x < maxcols-1) {
4628 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
4633 if (ldata->lattr & LATTR_WRAPPED) {
4635 ldata2 = lineptr(p.y+1);
4636 if (wordtype(term, UCSGET(ldata2->chars, 0))
4653 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
4662 ldata2 = lineptr(p.y-1);
4663 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
4664 term->cols-1 : term->cols);
4665 if (ldata2->lattr & LATTR_WRAPPED) {
4666 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
4684 * In this mode, every line is a unit.
4686 p.x = (dir == -1 ? 0 : term->cols - 1);
4694 static void sel_spread(Terminal *term)
4696 if (term->seltype == LEXICOGRAPHIC) {
4697 term->selstart = sel_spread_half(term, term->selstart, -1);
4698 decpos(term->selend);
4699 term->selend = sel_spread_half(term, term->selend, +1);
4700 incpos(term->selend);
4704 void term_do_paste(Terminal *term)
4709 get_clip(term->frontend, &data, &len);
4710 if (data && len > 0) {
4713 term_seen_key_event(term); /* pasted data counts */
4715 if (term->paste_buffer)
4716 sfree(term->paste_buffer);
4717 term->paste_pos = term->paste_hold = term->paste_len = 0;
4718 term->paste_buffer = snewn(len, wchar_t);
4721 while (p < data + len) {
4722 while (p < data + len &&
4723 !(p <= data + len - sel_nl_sz &&
4724 !memcmp(p, sel_nl, sizeof(sel_nl))))
4729 for (i = 0; i < p - q; i++) {
4730 term->paste_buffer[term->paste_len++] = q[i];
4734 if (p <= data + len - sel_nl_sz &&
4735 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4736 term->paste_buffer[term->paste_len++] = '\015';
4742 /* Assume a small paste will be OK in one go. */
4743 if (term->paste_len < 256) {
4745 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4746 if (term->paste_buffer)
4747 sfree(term->paste_buffer);
4748 term->paste_buffer = 0;
4749 term->paste_pos = term->paste_hold = term->paste_len = 0;
4752 get_clip(term->frontend, NULL, NULL);
4755 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4756 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4760 int raw_mouse = (term->xterm_mouse &&
4761 !term->cfg.no_mouse_rep &&
4762 !(term->cfg.mouse_override && shift));
4763 int default_seltype;
4767 if (a == MA_DRAG && !raw_mouse)
4768 term_scroll(term, 0, -1);
4770 if (y >= term->rows) {
4772 if (a == MA_DRAG && !raw_mouse)
4773 term_scroll(term, 0, +1);
4782 if (x >= term->cols)
4785 selpoint.y = y + term->disptop;
4787 ldata = lineptr(selpoint.y);
4788 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
4793 int encstate = 0, r, c;
4800 encstate = 0x20; /* left button down */
4811 case MBT_WHEEL_DOWN:
4814 default: break; /* placate gcc warning about enum use */
4818 if (term->xterm_mouse == 1)
4824 term->mouse_is_down = 0;
4827 if (term->mouse_is_down == braw)
4829 term->mouse_is_down = braw;
4831 default: break; /* placate gcc warning about enum use */
4840 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4841 ldisc_send(term->ldisc, abuf, 6, 0);
4847 * Set the selection type (rectangular or normal) at the start
4848 * of a selection attempt, from the state of Alt.
4850 if (!alt ^ !term->cfg.rect_select)
4851 default_seltype = RECTANGULAR;
4853 default_seltype = LEXICOGRAPHIC;
4855 if (term->selstate == NO_SELECTION) {
4856 term->seltype = default_seltype;
4859 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4861 term->selstate = ABOUT_TO;
4862 term->seltype = default_seltype;
4863 term->selanchor = selpoint;
4864 term->selmode = SM_CHAR;
4865 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4867 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4868 term->selstate = DRAGGING;
4869 term->selstart = term->selanchor = selpoint;
4870 term->selend = term->selstart;
4871 incpos(term->selend);
4873 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4874 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4875 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4877 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4878 term->selstate == SELECTED) {
4879 if (term->seltype == LEXICOGRAPHIC) {
4881 * For normal selection, we extend by moving
4882 * whichever end of the current selection is closer
4885 if (posdiff(selpoint, term->selstart) <
4886 posdiff(term->selend, term->selstart) / 2) {
4887 term->selanchor = term->selend;
4888 decpos(term->selanchor);
4890 term->selanchor = term->selstart;
4894 * For rectangular selection, we have a choice of
4895 * _four_ places to put selanchor and selpoint: the
4896 * four corners of the selection.
4898 if (2*selpoint.x < term->selstart.x + term->selend.x)
4899 term->selanchor.x = term->selend.x-1;
4901 term->selanchor.x = term->selstart.x;
4903 if (2*selpoint.y < term->selstart.y + term->selend.y)
4904 term->selanchor.y = term->selend.y;
4906 term->selanchor.y = term->selstart.y;
4908 term->selstate = DRAGGING;
4910 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4911 term->selanchor = selpoint;
4912 term->selstate = DRAGGING;
4913 if (term->seltype == LEXICOGRAPHIC) {
4915 * For normal selection, we set (selstart,selend) to
4916 * (selpoint,selanchor) in some order.
4918 if (poslt(selpoint, term->selanchor)) {
4919 term->selstart = selpoint;
4920 term->selend = term->selanchor;
4921 incpos(term->selend);
4923 term->selstart = term->selanchor;
4924 term->selend = selpoint;
4925 incpos(term->selend);
4929 * For rectangular selection, we may need to
4930 * interchange x and y coordinates (if the user has
4931 * dragged in the -x and +y directions, or vice versa).
4933 term->selstart.x = min(term->selanchor.x, selpoint.x);
4934 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4935 term->selstart.y = min(term->selanchor.y, selpoint.y);
4936 term->selend.y = max(term->selanchor.y, selpoint.y);
4939 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4941 if (term->selstate == DRAGGING) {
4943 * We've completed a selection. We now transfer the
4944 * data to the clipboard.
4946 clipme(term, term->selstart, term->selend,
4947 (term->seltype == RECTANGULAR), FALSE);
4948 term->selstate = SELECTED;
4950 term->selstate = NO_SELECTION;
4951 } else if (bcooked == MBT_PASTE
4953 #if MULTICLICK_ONLY_EVENT
4954 || a == MA_2CLK || a == MA_3CLK
4957 request_paste(term->frontend);
4963 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4964 unsigned int modifiers, unsigned int flags)
4968 int prependesc = FALSE;
4972 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4973 for (i = 0; i < tlen; i++)
4974 fprintf(stderr, " %04x", (unsigned)text[i]);
4975 fprintf(stderr, "\n");
4979 if ((flags & PKF_REPEAT) && term->repeat_off)
4982 /* Currently, Meta always just prefixes everything with ESC. */
4983 if (modifiers & PKM_META)
4985 modifiers &= ~PKM_META;
4988 * Alt is only used for Alt+keypad, which isn't supported yet, so
4991 modifiers &= ~PKM_ALT;
4993 /* Standard local function keys */
4994 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4996 if (keysym == PK_PAGEUP)
4997 /* scroll up one page */;
4998 if (keysym == PK_PAGEDOWN)
4999 /* scroll down on page */;
5000 if (keysym == PK_INSERT)
5001 term_do_paste(term);
5004 if (keysym == PK_PAGEUP)
5005 /* scroll up one line */;
5006 if (keysym == PK_PAGEDOWN)
5007 /* scroll down one line */;
5008 /* Control-Numlock for app-keypad mode switch */
5009 if (keysym == PK_PF1)
5010 term->app_keypad_keys ^= 1;
5014 if (modifiers & PKM_ALT) {
5015 /* Alt+F4 (close) */
5016 /* Alt+Return (full screen) */
5017 /* Alt+Space (system menu) */
5020 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5021 text[0] >= 0x20 && text[0] <= 0x7e) {
5022 /* ASCII chars + Control */
5023 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5024 (text[0] >= 0x61 && text[0] <= 0x7a))
5028 * Control-2 should return ^@ (0x00), Control-6 should return
5029 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5030 * the DOS keyboard handling did it, and we have nothing better
5031 * to do with the key combo in question, we'll also map
5032 * Control-Backquote to ^\ (0x1C).
5035 case ' ': text[0] = 0x00; break;
5036 case '-': text[0] = 0x1f; break;
5037 case '/': text[0] = 0x1f; break;
5038 case '2': text[0] = 0x00; break;
5039 case '3': text[0] = 0x1b; break;
5040 case '4': text[0] = 0x1c; break;
5041 case '5': text[0] = 0x1d; break;
5042 case '6': text[0] = 0x1e; break;
5043 case '7': text[0] = 0x1f; break;
5044 case '8': text[0] = 0x7f; break;
5045 case '`': text[0] = 0x1c; break;
5050 /* Nethack keypad */
5051 if (term->cfg.nethack_keypad) {
5054 case PK_KP1: c = 'b'; break;
5055 case PK_KP2: c = 'j'; break;
5056 case PK_KP3: c = 'n'; break;
5057 case PK_KP4: c = 'h'; break;
5058 case PK_KP5: c = '.'; break;
5059 case PK_KP6: c = 'l'; break;
5060 case PK_KP7: c = 'y'; break;
5061 case PK_KP8: c = 'k'; break;
5062 case PK_KP9: c = 'u'; break;
5063 default: break; /* else gcc warns `enum value not used' */
5067 if (modifiers & PKM_CONTROL)
5069 else if (modifiers & PKM_SHIFT)
5077 /* Numeric Keypad */
5078 if (PK_ISKEYPAD(keysym)) {
5082 * In VT400 mode, PFn always emits an escape sequence. In
5083 * Linux and tilde modes, this only happens in app keypad mode.
5085 if (term->cfg.funky_type == FUNKY_VT400 ||
5086 ((term->cfg.funky_type == FUNKY_LINUX ||
5087 term->cfg.funky_type == FUNKY_TILDE) &&
5088 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5090 case PK_PF1: xkey = 'P'; break;
5091 case PK_PF2: xkey = 'Q'; break;
5092 case PK_PF3: xkey = 'R'; break;
5093 case PK_PF4: xkey = 'S'; break;
5094 default: break; /* else gcc warns `enum value not used' */
5097 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5099 case PK_KP0: xkey = 'p'; break;
5100 case PK_KP1: xkey = 'q'; break;
5101 case PK_KP2: xkey = 'r'; break;
5102 case PK_KP3: xkey = 's'; break;
5103 case PK_KP4: xkey = 't'; break;
5104 case PK_KP5: xkey = 'u'; break;
5105 case PK_KP6: xkey = 'v'; break;
5106 case PK_KP7: xkey = 'w'; break;
5107 case PK_KP8: xkey = 'x'; break;
5108 case PK_KP9: xkey = 'y'; break;
5109 case PK_KPDECIMAL: xkey = 'n'; break;
5110 case PK_KPENTER: xkey = 'M'; break;
5111 default: break; /* else gcc warns `enum value not used' */
5113 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5115 * xterm can't see the layout of the keypad, so it has
5116 * to rely on the X keysyms returned by the keys.
5117 * Hence, we look at the strings here, not the PuTTY
5118 * keysyms (which describe the layout).
5122 if (modifiers & PKM_SHIFT)
5127 case '/': xkey = 'o'; break;
5128 case '*': xkey = 'j'; break;
5129 case '-': xkey = 'm'; break;
5133 * In all other modes, we try to retain the layout of
5134 * the DEC keypad in application mode.
5138 /* This key covers the '-' and ',' keys on a VT220 */
5139 if (modifiers & PKM_SHIFT)
5140 xkey = 'm'; /* VT220 '-' */
5142 xkey = 'l'; /* VT220 ',' */
5144 case PK_KPMINUS: xkey = 'm'; break;
5145 case PK_KPCOMMA: xkey = 'l'; break;
5146 default: break; /* else gcc warns `enum value not used' */
5151 if (term->vt52_mode) {
5152 if (xkey >= 'P' && xkey <= 'S')
5153 p += sprintf((char *) p, "\x1B%c", xkey);
5155 p += sprintf((char *) p, "\x1B?%c", xkey);
5157 p += sprintf((char *) p, "\x1BO%c", xkey);
5160 /* Not in application mode -- treat the number pad as arrow keys? */
5161 if ((flags & PKF_NUMLOCK) == 0) {
5163 case PK_KP0: keysym = PK_INSERT; break;
5164 case PK_KP1: keysym = PK_END; break;
5165 case PK_KP2: keysym = PK_DOWN; break;
5166 case PK_KP3: keysym = PK_PAGEDOWN; break;
5167 case PK_KP4: keysym = PK_LEFT; break;
5168 case PK_KP5: keysym = PK_REST; break;
5169 case PK_KP6: keysym = PK_RIGHT; break;
5170 case PK_KP7: keysym = PK_HOME; break;
5171 case PK_KP8: keysym = PK_UP; break;
5172 case PK_KP9: keysym = PK_PAGEUP; break;
5173 default: break; /* else gcc warns `enum value not used' */
5178 /* Miscellaneous keys */
5185 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5186 else if (modifiers == PKM_SHIFT)
5187 /* We do the opposite of what is configured */
5188 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5194 else if (modifiers == PKM_SHIFT)
5195 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5198 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5200 if (modifiers == PKM_CONTROL)
5205 case PK_KPENTER: /* Odd keypad modes handled above */
5206 if (modifiers == 0) {
5208 if (term->cr_lf_return)
5212 default: break; /* else gcc warns `enum value not used' */
5215 /* SCO function keys and editing keys */
5216 if (term->cfg.funky_type == FUNKY_SCO) {
5217 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5218 static char const codes[] =
5219 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5220 int index = keysym - PK_F1;
5222 if (modifiers & PKM_SHIFT) index += 12;
5223 if (modifiers & PKM_CONTROL) index += 24;
5224 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5227 if (PK_ISEDITING(keysym)) {
5231 case PK_DELETE: *p++ = 0x7f; goto done;
5232 case PK_HOME: xkey = 'H'; break;
5233 case PK_INSERT: xkey = 'L'; break;
5234 case PK_END: xkey = 'F'; break;
5235 case PK_PAGEUP: xkey = 'I'; break;
5236 case PK_PAGEDOWN: xkey = 'G'; break;
5237 default: break; /* else gcc warns `enum value not used' */
5239 p += sprintf((char *) p, "\x1B[%c", xkey);
5243 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5246 if (term->cfg.funky_type == FUNKY_XTERM) {
5247 /* Xterm shuffles these keys, apparently. */
5249 case PK_HOME: keysym = PK_INSERT; break;
5250 case PK_INSERT: keysym = PK_HOME; break;
5251 case PK_DELETE: keysym = PK_END; break;
5252 case PK_END: keysym = PK_PAGEUP; break;
5253 case PK_PAGEUP: keysym = PK_DELETE; break;
5254 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
5255 default: break; /* else gcc warns `enum value not used' */
5260 if (term->cfg.rxvt_homeend &&
5261 (keysym == PK_HOME || keysym == PK_END)) {
5262 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5266 if (term->vt52_mode) {
5270 * A real VT52 doesn't have these, and a VT220 doesn't
5271 * send anything for them in VT52 mode.
5274 case PK_HOME: xkey = 'H'; break;
5275 case PK_INSERT: xkey = 'L'; break;
5276 case PK_DELETE: xkey = 'M'; break;
5277 case PK_END: xkey = 'E'; break;
5278 case PK_PAGEUP: xkey = 'I'; break;
5279 case PK_PAGEDOWN: xkey = 'G'; break;
5280 default: xkey=0; break; /* else gcc warns `enum value not used'*/
5282 p += sprintf((char *) p, "\x1B%c", xkey);
5287 case PK_HOME: code = 1; break;
5288 case PK_INSERT: code = 2; break;
5289 case PK_DELETE: code = 3; break;
5290 case PK_END: code = 4; break;
5291 case PK_PAGEUP: code = 5; break;
5292 case PK_PAGEDOWN: code = 6; break;
5293 default: code = 0; break; /* else gcc warns `enum value not used' */
5295 p += sprintf((char *) p, "\x1B[%d~", code);
5299 if (PK_ISFKEY(keysym)) {
5300 /* Map Shift+F1-F10 to F11-F20 */
5301 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5303 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5305 /* XXX This overrides the XTERM/VT52 mode below */
5307 if (keysym >= PK_F6) offt++;
5308 if (keysym >= PK_F12) offt++;
5309 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5310 'P' + keysym - PK_F1 - offt);
5313 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5314 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5317 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5318 if (term->vt52_mode)
5319 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5321 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5324 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5328 if (PK_ISCURSOR(keysym)) {
5332 case PK_UP: xkey = 'A'; break;
5333 case PK_DOWN: xkey = 'B'; break;
5334 case PK_RIGHT: xkey = 'C'; break;
5335 case PK_LEFT: xkey = 'D'; break;
5336 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5337 default: xkey = 0; break; /* else gcc warns `enum value not used' */
5339 if (term->vt52_mode)
5340 p += sprintf((char *) p, "\x1B%c", xkey);
5342 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5344 /* Useful mapping of Ctrl-arrows */
5345 if (modifiers == PKM_CONTROL)
5349 p += sprintf((char *) p, "\x1BO%c", xkey);
5351 p += sprintf((char *) p, "\x1B[%c", xkey);
5357 if (p > output || tlen > 0) {
5359 * Interrupt an ongoing paste. I'm not sure
5360 * this is sensible, but for the moment it's
5361 * preferable to having to faff about buffering
5367 * We need not bother about stdin backlogs
5368 * here, because in GUI PuTTY we can't do
5369 * anything about it anyway; there's no means
5370 * of asking Windows to hold off on KEYDOWN
5371 * messages. We _have_ to buffer everything
5374 term_seen_key_event(term);
5378 fprintf(stderr, "sending ESC\n");
5380 ldisc_send(term->ldisc, "\x1b", 1, 1);
5385 fprintf(stderr, "sending %d bytes:", p - output);
5386 for (i = 0; i < p - output; i++)
5387 fprintf(stderr, " %02x", output[i]);
5388 fprintf(stderr, "\n");
5390 ldisc_send(term->ldisc, output, p - output, 1);
5391 } else if (tlen > 0) {
5393 fprintf(stderr, "sending %d unichars:", tlen);
5394 for (i = 0; i < tlen; i++)
5395 fprintf(stderr, " %04x", (unsigned) text[i]);
5396 fprintf(stderr, "\n");
5398 luni_send(term->ldisc, text, tlen, 1);
5403 void term_nopaste(Terminal *term)
5405 if (term->paste_len == 0)
5407 sfree(term->paste_buffer);
5408 term->paste_buffer = NULL;
5409 term->paste_len = 0;
5412 int term_paste_pending(Terminal *term)
5414 return term->paste_len != 0;
5417 void term_paste(Terminal *term)
5419 long now, paste_diff;
5421 if (term->paste_len == 0)
5424 /* Don't wait forever to paste */
5425 if (term->paste_hold) {
5426 now = GETTICKCOUNT();
5427 paste_diff = now - term->last_paste;
5428 if (paste_diff >= 0 && paste_diff < 450)
5431 term->paste_hold = 0;
5433 while (term->paste_pos < term->paste_len) {
5435 while (n + term->paste_pos < term->paste_len) {
5436 if (term->paste_buffer[term->paste_pos + n++] == '\015')
5440 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
5441 term->paste_pos += n;
5443 if (term->paste_pos < term->paste_len) {
5444 term->paste_hold = 1;
5448 sfree(term->paste_buffer);
5449 term->paste_buffer = NULL;
5450 term->paste_len = 0;
5453 static void deselect(Terminal *term)
5455 term->selstate = NO_SELECTION;
5456 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
5459 void term_deselect(Terminal *term)
5465 int term_ldisc(Terminal *term, int option)
5467 if (option == LD_ECHO)
5468 return term->term_echoing;
5469 if (option == LD_EDIT)
5470 return term->term_editing;
5474 int term_data(Terminal *term, int is_stderr, const char *data, int len)
5476 bufchain_add(&term->inbuf, data, len);
5478 if (!term->in_term_out) {
5479 term->in_term_out = TRUE;
5480 term_blink(term, 1);
5482 term->in_term_out = FALSE;
5486 * term_out() always completely empties inbuf. Therefore,
5487 * there's no reason at all to return anything other than zero
5488 * from this function, because there _can't_ be a question of
5489 * the remote side needing to wait until term_out() has cleared
5492 * This is a slightly suboptimal way to deal with SSH2 - in
5493 * principle, the window mechanism would allow us to continue
5494 * to accept data on forwarded ports and X connections even
5495 * while the terminal processing was going slowly - but we
5496 * can't do the 100% right thing without moving the terminal
5497 * processing into a separate thread, and that might hurt
5498 * portability. So we manage stdout buffering the old SSH1 way:
5499 * if the terminal processing goes slowly, the whole SSH
5500 * connection stops accepting data until it's ready.
5502 * In practice, I can't imagine this causing serious trouble.
5507 void term_provide_logctx(Terminal *term, void *logctx)
5509 term->logctx = logctx;