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 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
60 const wchar_t sel_nl[] = SEL_NL;
63 * Fetch the character at a particular position in a line array,
64 * for purposes of `wordtype'. The reason this isn't just a simple
65 * array reference is that if the character we find is UCSWIDE,
66 * then we must look one space further to the left.
68 #define UCSGET(a, x) \
69 ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \
70 (a)[(x)-1] : (a)[(x)] )
73 * Internal prototypes.
75 static unsigned long *resizeline(unsigned long *, int);
76 static unsigned long *lineptr(Terminal *, int, int);
77 static void do_paint(Terminal *, Context, int);
78 static void erase_lots(Terminal *, int, int, int);
79 static void swap_screen(Terminal *, int, int, int);
80 static void update_sbar(Terminal *);
81 static void deselect(Terminal *);
82 static void term_print_finish(Terminal *);
83 #ifdef OPTIMISE_SCROLL
84 static void scroll_display(Terminal *, int, int, int);
85 #endif /* OPTIMISE_SCROLL */
88 * Resize a line to make it `cols' columns wide.
90 static unsigned long *resizeline(unsigned long *line, int cols)
93 unsigned long lineattrs;
95 if (line[0] != (unsigned long)cols) {
97 * This line is the wrong length, which probably means it
98 * hasn't been accessed since a resize. Resize it now.
101 lineattrs = line[oldlen + 1];
102 line = srealloc(line, TSIZE * (2 + cols));
104 for (i = oldlen; i < cols; i++)
105 line[i + 1] = ERASE_CHAR;
106 line[cols + 1] = lineattrs & LATTR_MODE;
113 * Retrieve a line of the screen or of the scrollback, according to
114 * whether the y coordinate is non-negative or negative
117 static unsigned long *lineptr(Terminal *term, int y, int lineno)
119 unsigned long *line, *newline;
124 whichtree = term->screen;
127 whichtree = term->scrollback;
128 treeindex = y + count234(term->scrollback);
130 line = index234(whichtree, treeindex);
132 /* We assume that we don't screw up and retrieve something out of range. */
133 assert(line != NULL);
135 newline = resizeline(line, term->cols);
136 if (newline != line) {
137 delpos234(whichtree, treeindex);
138 addpos234(whichtree, newline, treeindex);
145 #define lineptr(x) lineptr(term,x,__LINE__)
148 * Set up power-on settings for the terminal.
150 static void power_on(Terminal *term)
152 term->curs.x = term->curs.y = 0;
153 term->alt_x = term->alt_y = 0;
154 term->savecurs.x = term->savecurs.y = 0;
155 term->alt_t = term->marg_t = 0;
156 if (term->rows != -1)
157 term->alt_b = term->marg_b = term->rows - 1;
159 term->alt_b = term->marg_b = 0;
160 if (term->cols != -1) {
162 for (i = 0; i < term->cols; i++)
163 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
165 term->alt_om = term->dec_om = term->cfg.dec_om;
166 term->alt_ins = term->insert = FALSE;
167 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
168 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
169 term->alt_cset = term->cset = term->save_cset = 0;
170 term->alt_utf = term->utf = term->save_utf = 0;
172 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
173 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
175 term->in_vbell = FALSE;
177 term->big_cursor = 0;
178 term->save_attr = term->curr_attr = ATTR_DEFAULT;
179 term->term_editing = term->term_echoing = FALSE;
180 term->app_cursor_keys = term->cfg.app_cursor;
181 term->app_keypad_keys = term->cfg.app_keypad;
182 term->use_bce = term->cfg.bce;
183 term->blink_is_real = term->cfg.blinktext;
184 term->erase_char = ERASE_CHAR;
186 term_print_finish(term);
189 for (i = 0; i < 256; i++)
190 term->wordness[i] = term->cfg.wordness[i];
193 swap_screen(term, 1, FALSE, FALSE);
194 erase_lots(term, FALSE, TRUE, TRUE);
195 swap_screen(term, 0, FALSE, FALSE);
196 erase_lots(term, FALSE, TRUE, TRUE);
201 * Force a screen update.
203 void term_update(Terminal *term)
206 ctx = get_ctx(term->frontend);
208 int need_sbar_update = term->seen_disp_event;
209 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
210 term->disptop = 0; /* return to main screen */
211 term->seen_disp_event = 0;
212 need_sbar_update = TRUE;
214 if (need_sbar_update)
216 do_paint(term, ctx, TRUE);
217 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
223 * Called from front end when a keypress occurs, to trigger
224 * anything magical that needs to happen in that situation.
226 void term_seen_key_event(Terminal *term)
229 * On any keypress, clear the bell overload mechanism
230 * completely, on the grounds that large numbers of
231 * beeps coming from deliberate key action are likely
232 * to be intended (e.g. beeps from filename completion
233 * blocking repeatedly).
235 term->beep_overloaded = FALSE;
236 while (term->beephead) {
237 struct beeptime *tmp = term->beephead;
238 term->beephead = tmp->next;
241 term->beeptail = NULL;
245 * Reset the scrollback on keypress, if we're doing that.
247 if (term->cfg.scroll_on_key) {
248 term->disptop = 0; /* return to main screen */
249 term->seen_disp_event = 1;
254 * Same as power_on(), but an external function.
256 void term_pwron(Terminal *term)
259 if (term->ldisc) /* cause ldisc to notice changes */
260 ldisc_send(term->ldisc, NULL, 0, 0);
268 * When the user reconfigures us, we need to check the forbidden-
269 * alternate-screen config option, disable raw mouse mode if the
270 * user has disabled mouse reporting, and abandon a print job if
271 * the user has disabled printing.
273 void term_reconfig(Terminal *term, Config *cfg)
276 * Before adopting the new config, check all those terminal
277 * settings which control power-on defaults; and if they've
278 * changed, we will modify the current state as well as the
279 * default one. The full list is: Auto wrap mode, DEC Origin
280 * Mode, BCE, blinking text, character classes.
282 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
285 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
286 reset_decom = (term->cfg.dec_om != cfg->dec_om);
287 reset_bce = (term->cfg.bce != cfg->bce);
288 reset_blink = (term->cfg.blinktext != cfg->blinktext);
290 for (i = 0; i < lenof(term->cfg.wordness); i++)
291 if (term->cfg.wordness[i] != cfg->wordness[i])
294 term->cfg = *cfg; /* STRUCTURE COPY */
297 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
299 term->alt_om = term->dec_om = term->cfg.dec_om;
301 term->use_bce = term->cfg.bce;
303 term->blink_is_real = term->cfg.blinktext;
305 for (i = 0; i < 256; i++)
306 term->wordness[i] = term->cfg.wordness[i];
308 if (term->cfg.no_alt_screen)
309 swap_screen(term, 0, FALSE, FALSE);
310 if (term->cfg.no_mouse_rep) {
311 term->xterm_mouse = 0;
312 set_raw_mouse_mode(term->frontend, 0);
314 if (term->cfg.no_remote_charset) {
315 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
316 term->sco_acs = term->alt_sco_acs = 0;
319 if (!*term->cfg.printer) {
320 term_print_finish(term);
325 * Clear the scrollback.
327 void term_clrsb(Terminal *term)
331 while ((line = delpos234(term->scrollback, 0)) != NULL) {
338 * Initialise the terminal.
340 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
346 * Allocate a new Terminal structure and initialise the fields
349 term = smalloc(sizeof(Terminal));
350 term->frontend = frontend;
351 term->ucsdata = ucsdata;
352 term->cfg = *mycfg; /* STRUCTURE COPY */
354 term->compatibility_level = TM_PUTTY;
355 strcpy(term->id_string, "\033[?6c");
356 term->last_blink = term->last_tblink = 0;
357 term->paste_buffer = NULL;
359 term->last_paste = 0;
360 bufchain_init(&term->inbuf);
361 bufchain_init(&term->printer_buf);
362 term->printing = term->only_printing = FALSE;
363 term->print_job = NULL;
364 term->vt52_mode = FALSE;
365 term->cr_lf_return = FALSE;
366 term->seen_disp_event = FALSE;
367 term->xterm_mouse = term->mouse_is_down = FALSE;
368 term->reset_132 = FALSE;
369 term->blinker = term->tblinker = 0;
371 term->repeat_off = FALSE;
372 term->termstate = TOPLEVEL;
373 term->selstate = NO_SELECTION;
376 term->screen = term->alt_screen = term->scrollback = NULL;
378 term->disptext = term->dispcurs = NULL;
381 term->rows = term->cols = -1;
383 term->beephead = term->beeptail = NULL;
385 term->lastbeep = FALSE;
386 term->beep_overloaded = FALSE;
387 term->attr_mask = 0xffffffff;
388 term->resize_fn = NULL;
389 term->resize_ctx = NULL;
394 void term_free(Terminal *term)
397 struct beeptime *beep;
399 while ((line = delpos234(term->scrollback, 0)) != NULL)
401 freetree234(term->scrollback);
402 while ((line = delpos234(term->screen, 0)) != NULL)
404 freetree234(term->screen);
405 while ((line = delpos234(term->alt_screen, 0)) != NULL)
407 freetree234(term->alt_screen);
408 sfree(term->disptext);
409 while (term->beephead) {
410 beep = term->beephead;
411 term->beephead = beep->next;
414 bufchain_clear(&term->inbuf);
416 printer_finish_job(term->print_job);
417 bufchain_clear(&term->printer_buf);
418 sfree(term->paste_buffer);
423 * Set up the terminal for a given size.
425 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
428 unsigned long *newdisp, *line;
431 int save_alt_which = term->alt_which;
433 if (newrows == term->rows && newcols == term->cols &&
434 newsavelines == term->savelines)
435 return; /* nothing to do */
438 swap_screen(term, 0, FALSE, FALSE);
440 term->alt_t = term->marg_t = 0;
441 term->alt_b = term->marg_b = newrows - 1;
443 if (term->rows == -1) {
444 term->scrollback = newtree234(NULL);
445 term->screen = newtree234(NULL);
450 * Resize the screen and scrollback. We only need to shift
451 * lines around within our data structures, because lineptr()
452 * will take care of resizing each individual line if
455 * - If the new screen and the old screen differ in length, we
456 * must shunt some lines in from the scrollback or out to
459 * - If doing that fails to provide us with enough material to
460 * fill the new screen (i.e. the number of rows needed in
461 * the new screen exceeds the total number in the previous
462 * screen+scrollback), we must invent some blank lines to
465 * - Then, if the new scrollback length is less than the
466 * amount of scrollback we actually have, we must throw some
469 sblen = count234(term->scrollback);
470 /* Do this loop to expand the screen if newrows > rows */
471 for (i = term->rows; i < newrows; i++) {
473 line = delpos234(term->scrollback, --sblen);
475 line = smalloc(TSIZE * (newcols + 2));
477 for (j = 0; j <= newcols; j++)
478 line[j + 1] = ERASE_CHAR;
480 addpos234(term->screen, line, 0);
482 /* Do this loop to shrink the screen if newrows < rows */
483 for (i = newrows; i < term->rows; i++) {
484 line = delpos234(term->screen, 0);
485 addpos234(term->scrollback, line, sblen++);
487 assert(count234(term->screen) == newrows);
488 while (sblen > newsavelines) {
489 line = delpos234(term->scrollback, 0);
493 assert(count234(term->scrollback) <= newsavelines);
496 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
497 for (i = 0; i < newrows * (newcols + 1); i++)
498 newdisp[i] = ATTR_INVALID;
499 sfree(term->disptext);
500 term->disptext = newdisp;
501 term->dispcurs = NULL;
503 newalt = newtree234(NULL);
504 for (i = 0; i < newrows; i++) {
505 line = smalloc(TSIZE * (newcols + 2));
507 for (j = 0; j <= newcols; j++)
508 line[j + 1] = term->erase_char;
509 addpos234(newalt, line, i);
511 if (term->alt_screen) {
512 while (NULL != (line = delpos234(term->alt_screen, 0)))
514 freetree234(term->alt_screen);
516 term->alt_screen = newalt;
518 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
521 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
522 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
526 term->curs.y += newrows - term->rows;
527 if (term->curs.y < 0)
529 if (term->curs.y >= newrows)
530 term->curs.y = newrows - 1;
531 if (term->curs.x >= newcols)
532 term->curs.x = newcols - 1;
533 term->alt_x = term->alt_y = 0;
534 term->wrapnext = term->alt_wnext = FALSE;
536 term->rows = newrows;
537 term->cols = newcols;
538 term->savelines = newsavelines;
541 swap_screen(term, save_alt_which, FALSE, FALSE);
546 term->resize_fn(term->resize_ctx, term->cols, term->rows);
550 * Hand a function and context pointer to the terminal which it can
551 * use to notify a back end of resizes.
553 void term_provide_resize_fn(Terminal *term,
554 void (*resize_fn)(void *, int, int),
557 term->resize_fn = resize_fn;
558 term->resize_ctx = resize_ctx;
559 if (term->cols > 0 && term->rows > 0)
560 resize_fn(resize_ctx, term->cols, term->rows);
564 * Swap screens. If `reset' is TRUE and we have been asked to
565 * switch to the alternate screen, we must bring most of its
566 * configuration from the main screen and erase the contents of the
567 * alternate screen completely. (This is even true if we're already
568 * on it! Blame xterm.)
570 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
576 reset = FALSE; /* do no weird resetting if which==0 */
578 if (which != term->alt_which) {
579 term->alt_which = which;
581 ttr = term->alt_screen;
582 term->alt_screen = term->screen;
585 if (!reset && !keep_cur_pos)
586 term->curs.x = term->alt_x;
589 if (!reset && !keep_cur_pos)
590 term->curs.y = term->alt_y;
593 if (!reset) term->marg_t = term->alt_t;
596 if (!reset) term->marg_b = term->alt_b;
599 if (!reset) term->dec_om = term->alt_om;
602 if (!reset) term->wrap = term->alt_wrap;
605 if (!reset) term->wrapnext = term->alt_wnext;
608 if (!reset) term->insert = term->alt_ins;
611 if (!reset) term->cset = term->alt_cset;
614 if (!reset) term->utf = term->alt_utf;
617 if (!reset) term->sco_acs = term->alt_sco_acs;
618 term->alt_sco_acs = t;
621 if (reset && term->screen) {
623 * Yes, this _is_ supposed to honour background-colour-erase.
625 erase_lots(term, FALSE, TRUE, TRUE);
629 * This might not be possible if we're called during
637 * Update the scroll bar.
639 static void update_sbar(Terminal *term)
643 nscroll = count234(term->scrollback);
645 set_sbar(term->frontend, nscroll + term->rows,
646 nscroll + term->disptop, term->rows);
650 * Check whether the region bounded by the two pointers intersects
651 * the scroll region, and de-select the on-screen selection if so.
653 static void check_selection(Terminal *term, pos from, pos to)
655 if (poslt(from, term->selend) && poslt(term->selstart, to))
660 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
661 * for backward.) `sb' is TRUE if the scrolling is permitted to
662 * affect the scrollback buffer.
664 * NB this function invalidates all pointers into lines of the
665 * screen data structures. In particular, you MUST call fix_cpos
666 * after calling scroll() and before doing anything else that
667 * uses the cpos shortcut pointer.
669 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
671 unsigned long *line, *line2;
672 int i, seltop, olddisptop, shift;
674 if (topline != 0 || term->alt_which != 0)
677 olddisptop = term->disptop;
681 line = delpos234(term->screen, botline);
682 line = resizeline(line, term->cols);
683 for (i = 0; i < term->cols; i++)
684 line[i + 1] = term->erase_char;
685 line[term->cols + 1] = 0;
686 addpos234(term->screen, line, topline);
688 if (term->selstart.y >= topline && term->selstart.y <= botline) {
690 if (term->selstart.y > botline) {
691 term->selstart.y = botline;
692 term->selstart.x = 0;
695 if (term->selend.y >= topline && term->selend.y <= botline) {
697 if (term->selend.y > botline) {
698 term->selend.y = botline;
707 line = delpos234(term->screen, topline);
708 if (sb && term->savelines > 0) {
709 int sblen = count234(term->scrollback);
711 * We must add this line to the scrollback. We'll
712 * remove a line from the top of the scrollback to
713 * replace it, or allocate a new one if the
714 * scrollback isn't full.
716 if (sblen == term->savelines) {
717 sblen--, line2 = delpos234(term->scrollback, 0);
719 line2 = smalloc(TSIZE * (term->cols + 2));
720 line2[0] = term->cols;
722 addpos234(term->scrollback, line, sblen);
726 * If the user is currently looking at part of the
727 * scrollback, and they haven't enabled any options
728 * that are going to reset the scrollback as a
729 * result of this movement, then the chances are
730 * they'd like to keep looking at the same line. So
731 * we move their viewpoint at the same rate as the
732 * scroll, at least until their viewpoint hits the
733 * top end of the scrollback buffer, at which point
734 * we don't have the choice any more.
736 * Thanks to Jan Holmen Holsten for the idea and
737 * initial implementation.
739 if (term->disptop > -term->savelines && term->disptop < 0)
742 line = resizeline(line, term->cols);
743 for (i = 0; i < term->cols; i++)
744 line[i + 1] = term->erase_char;
745 line[term->cols + 1] = 0;
746 addpos234(term->screen, line, botline);
749 * If the selection endpoints move into the scrollback,
750 * we keep them moving until they hit the top. However,
751 * of course, if the line _hasn't_ moved into the
752 * scrollback then we don't do this, and cut them off
753 * at the top of the scroll region.
755 * This applies to selstart and selend (for an existing
756 * selection), and also selanchor (for one being
757 * selected as we speak).
759 seltop = sb ? -term->savelines : topline;
761 if (term->selstart.y >= seltop &&
762 term->selstart.y <= botline) {
764 if (term->selstart.y < seltop) {
765 term->selstart.y = seltop;
766 term->selstart.x = 0;
769 if (term->selend.y >= seltop && term->selend.y <= botline) {
771 if (term->selend.y < seltop) {
772 term->selend.y = seltop;
776 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
778 if (term->selanchor.y < seltop) {
779 term->selanchor.y = seltop;
780 term->selanchor.x = 0;
787 #ifdef OPTIMISE_SCROLL
788 shift += term->disptop - olddisptop;
789 if (shift < term->rows && shift > -term->rows && shift != 0)
790 scroll_display(term, topline, botline, shift);
791 #endif /* OPTIMISE_SCROLL */
794 #ifdef OPTIMISE_SCROLL
796 * Scroll the physical display, and our conception of it in disptext.
798 static void scroll_display(Terminal *term, int topline, int botline, int lines)
800 unsigned long *start, *end;
801 int distance, size, i;
803 start = term->disptext + topline * (term->cols + 1);
804 end = term->disptext + (botline + 1) * (term->cols + 1);
805 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
806 size = end - start - distance;
808 memmove(start, start + distance, size * TSIZE);
809 if (term->dispcurs >= start + distance &&
810 term->dispcurs <= start + distance + size)
811 term->dispcurs -= distance;
812 for (i = 0; i < distance; i++)
813 (start + size)[i] |= ATTR_INVALID;
815 memmove(start + distance, start, size * TSIZE);
816 if (term->dispcurs >= start && term->dispcurs <= start + size)
817 term->dispcurs += distance;
818 for (i = 0; i < distance; i++)
819 start[i] |= ATTR_INVALID;
821 do_scroll(term->frontend, topline, botline, lines);
823 #endif /* OPTIMISE_SCROLL */
826 * Move the cursor to a given position, clipping at boundaries. We
827 * may or may not want to clip at the scroll margin: marg_clip is 0
828 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
829 * even _being_ outside the margins.
831 static void move(Terminal *term, int x, int y, int marg_clip)
838 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
841 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
852 term->wrapnext = FALSE;
856 * Save or restore the cursor and SGR mode.
858 static void save_cursor(Terminal *term, int save)
861 term->savecurs = term->curs;
862 term->save_attr = term->curr_attr;
863 term->save_cset = term->cset;
864 term->save_utf = term->utf;
865 term->save_wnext = term->wrapnext;
866 term->save_csattr = term->cset_attr[term->cset];
867 term->save_sco_acs = term->sco_acs;
869 term->curs = term->savecurs;
870 /* Make sure the window hasn't shrunk since the save */
871 if (term->curs.x >= term->cols)
872 term->curs.x = term->cols - 1;
873 if (term->curs.y >= term->rows)
874 term->curs.y = term->rows - 1;
876 term->curr_attr = term->save_attr;
877 term->cset = term->save_cset;
878 term->utf = term->save_utf;
879 term->wrapnext = term->save_wnext;
881 * wrapnext might reset to False if the x position is no
882 * longer at the rightmost edge.
884 if (term->wrapnext && term->curs.x < term->cols-1)
885 term->wrapnext = FALSE;
886 term->cset_attr[term->cset] = term->save_csattr;
887 term->sco_acs = term->save_sco_acs;
890 term->erase_char = (' ' | ATTR_ASCII |
892 (ATTR_FGMASK | ATTR_BGMASK)));
897 * This function is called before doing _anything_ which affects
898 * only part of a line of text. It is used to mark the boundary
899 * between two character positions, and it indicates that some sort
900 * of effect is going to happen on only one side of that boundary.
902 * The effect of this function is to check whether a CJK
903 * double-width character is straddling the boundary, and to remove
904 * it and replace it with two spaces if so. (Of course, one or
905 * other of those spaces is then likely to be replaced with
906 * something else again, as a result of whatever happens next.)
908 * Also, if the boundary is at the right-hand _edge_ of the screen,
909 * it implies something deliberate is being done to the rightmost
910 * column position; hence we must clear LATTR_WRAPPED2.
912 * The input to the function is the coordinates of the _second_
913 * character of the pair.
915 static void check_boundary(Terminal *term, int x, int y)
917 unsigned long *ldata;
919 /* Validate input coordinates, just in case. */
920 if (x == 0 || x > term->cols)
924 if (x == term->cols) {
925 ldata[x] &= ~LATTR_WRAPPED2;
927 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
928 ldata[x-1] = ldata[x] =
929 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
935 * Erase a large portion of the screen: the whole screen, or the
936 * whole line, or parts thereof.
938 static void erase_lots(Terminal *term,
939 int line_only, int from_begin, int to_end)
943 unsigned long *ldata;
946 start.y = term->curs.y;
948 end.y = term->curs.y + 1;
965 if (!from_begin || !to_end)
966 check_boundary(term, term->curs.x, term->curs.y);
967 check_selection(term, start, end);
969 /* Clear screen also forces a full window redraw, just in case. */
970 if (start.y == 0 && start.x == 0 && end.y == term->rows)
971 term_invalidate(term);
973 ldata = lineptr(start.y);
974 while (poslt(start, end)) {
975 if (start.x == term->cols && !erase_lattr)
976 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
978 ldata[start.x] = term->erase_char;
979 if (incpos(start) && start.y < term->rows)
980 ldata = lineptr(start.y);
985 * Insert or delete characters within the current line. n is +ve if
986 * insertion is desired, and -ve for deletion.
988 static void insch(Terminal *term, int n)
990 int dir = (n < 0 ? -1 : +1);
993 unsigned long *ldata;
995 n = (n < 0 ? -n : n);
996 if (n > term->cols - term->curs.x)
997 n = term->cols - term->curs.x;
998 m = term->cols - term->curs.x - n;
999 cursplus.y = term->curs.y;
1000 cursplus.x = term->curs.x + n;
1001 check_selection(term, term->curs, cursplus);
1002 check_boundary(term, term->curs.x, term->curs.y);
1004 check_boundary(term, term->curs.x + n, term->curs.y);
1005 ldata = lineptr(term->curs.y);
1007 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1009 ldata[term->curs.x + m++] = term->erase_char;
1011 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1013 ldata[term->curs.x + n] = term->erase_char;
1018 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1019 * whether the mode is a DEC private one or a normal one.)
1021 static void toggle_mode(Terminal *term, int mode, int query, int state)
1023 unsigned long ticks;
1027 case 1: /* application cursor keys */
1028 term->app_cursor_keys = state;
1030 case 2: /* VT52 mode */
1031 term->vt52_mode = !state;
1032 if (term->vt52_mode) {
1033 term->blink_is_real = FALSE;
1034 term->vt52_bold = FALSE;
1036 term->blink_is_real = term->cfg.blinktext;
1039 case 3: /* 80/132 columns */
1041 if (!term->cfg.no_remote_resize)
1042 request_resize(term->frontend, state ? 132 : 80, term->rows);
1043 term->reset_132 = state;
1045 case 5: /* reverse video */
1047 * Toggle reverse video. If we receive an OFF within the
1048 * visual bell timeout period after an ON, we trigger an
1049 * effective visual bell, so that ESC[?5hESC[?5l will
1050 * always be an actually _visible_ visual bell.
1052 ticks = GETTICKCOUNT();
1053 /* turn off a previous vbell to avoid inconsistencies */
1054 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1055 term->in_vbell = FALSE;
1056 if (term->rvideo && !state && /* we're turning it off... */
1057 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1058 /* If there's no vbell timeout already, or this one lasts
1059 * longer, replace vbell_timeout with ours. */
1060 if (!term->in_vbell ||
1061 (term->rvbell_startpoint - term->vbell_startpoint <
1063 term->vbell_startpoint = term->rvbell_startpoint;
1064 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1065 } else if (!term->rvideo && state) {
1066 /* This is an ON, so we notice the time and save it. */
1067 term->rvbell_startpoint = ticks;
1069 term->rvideo = state;
1070 term->seen_disp_event = TRUE;
1074 case 6: /* DEC origin mode */
1075 term->dec_om = state;
1077 case 7: /* auto wrap */
1080 case 8: /* auto key repeat */
1081 term->repeat_off = !state;
1083 case 10: /* set local edit mode */
1084 term->term_editing = state;
1085 if (term->ldisc) /* cause ldisc to notice changes */
1086 ldisc_send(term->ldisc, NULL, 0, 0);
1088 case 25: /* enable/disable cursor */
1089 compatibility2(OTHER, VT220);
1090 term->cursor_on = state;
1091 term->seen_disp_event = TRUE;
1093 case 47: /* alternate screen */
1094 compatibility(OTHER);
1096 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1099 case 1000: /* xterm mouse 1 */
1100 term->xterm_mouse = state ? 1 : 0;
1101 set_raw_mouse_mode(term->frontend, state);
1103 case 1002: /* xterm mouse 2 */
1104 term->xterm_mouse = state ? 2 : 0;
1105 set_raw_mouse_mode(term->frontend, state);
1107 case 1047: /* alternate screen */
1108 compatibility(OTHER);
1110 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1113 case 1048: /* save/restore cursor */
1114 save_cursor(term, state);
1115 if (!state) term->seen_disp_event = TRUE;
1117 case 1049: /* cursor & alternate screen */
1119 save_cursor(term, state);
1120 if (!state) term->seen_disp_event = TRUE;
1121 compatibility(OTHER);
1123 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1125 save_cursor(term, state);
1130 case 4: /* set insert mode */
1131 compatibility(VT102);
1132 term->insert = state;
1134 case 12: /* set echo mode */
1135 term->term_echoing = !state;
1136 if (term->ldisc) /* cause ldisc to notice changes */
1137 ldisc_send(term->ldisc, NULL, 0, 0);
1139 case 20: /* Return sends ... */
1140 term->cr_lf_return = state;
1142 case 34: /* Make cursor BIG */
1143 compatibility2(OTHER, VT220);
1144 term->big_cursor = !state;
1149 * Process an OSC sequence: set window title or icon name.
1151 static void do_osc(Terminal *term)
1154 while (term->osc_strlen--)
1155 term->wordness[(unsigned char)
1156 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1158 term->osc_string[term->osc_strlen] = '\0';
1159 switch (term->esc_args[0]) {
1162 if (!term->cfg.no_remote_wintitle)
1163 set_icon(term->frontend, term->osc_string);
1164 if (term->esc_args[0] == 1)
1166 /* fall through: parameter 0 means set both */
1169 if (!term->cfg.no_remote_wintitle)
1170 set_title(term->frontend, term->osc_string);
1177 * ANSI printing routines.
1179 static void term_print_setup(Terminal *term)
1181 bufchain_clear(&term->printer_buf);
1182 term->print_job = printer_start_job(term->cfg.printer);
1184 static void term_print_flush(Terminal *term)
1189 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1190 bufchain_prefix(&term->printer_buf, &data, &len);
1193 printer_job_data(term->print_job, data, len);
1194 bufchain_consume(&term->printer_buf, len);
1197 static void term_print_finish(Terminal *term)
1203 if (!term->printing && !term->only_printing)
1204 return; /* we need do nothing */
1206 term_print_flush(term);
1207 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1208 bufchain_prefix(&term->printer_buf, &data, &len);
1210 if (c == '\033' || c == '\233') {
1211 bufchain_consume(&term->printer_buf, size);
1214 printer_job_data(term->print_job, &c, 1);
1215 bufchain_consume(&term->printer_buf, 1);
1218 printer_finish_job(term->print_job);
1219 term->print_job = NULL;
1220 term->printing = term->only_printing = FALSE;
1224 * Remove everything currently in `inbuf' and stick it up on the
1225 * in-memory display. There's a big state machine in here to
1226 * process escape sequences...
1228 void term_out(Terminal *term)
1231 unsigned char localbuf[256], *chars;
1236 chars = NULL; /* placate compiler warnings */
1237 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1241 bufchain_prefix(&term->inbuf, &ret, &nchars);
1242 if (nchars > sizeof(localbuf))
1243 nchars = sizeof(localbuf);
1244 memcpy(localbuf, ret, nchars);
1245 bufchain_consume(&term->inbuf, nchars);
1247 assert(chars != NULL);
1253 * Optionally log the session traffic to a file. Useful for
1254 * debugging and possibly also useful for actual logging.
1256 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1257 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1263 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1264 * be able to display 8-bit characters, but I'll let that go 'cause
1269 * If we're printing, add the character to the printer
1272 if (term->printing) {
1273 bufchain_add(&term->printer_buf, &c, 1);
1276 * If we're in print-only mode, we use a much simpler
1277 * state machine designed only to recognise the ESC[4i
1278 * termination sequence.
1280 if (term->only_printing) {
1282 term->print_state = 1;
1283 else if (c == (unsigned char)'\233')
1284 term->print_state = 2;
1285 else if (c == '[' && term->print_state == 1)
1286 term->print_state = 2;
1287 else if (c == '4' && term->print_state == 2)
1288 term->print_state = 3;
1289 else if (c == 'i' && term->print_state == 3)
1290 term->print_state = 4;
1292 term->print_state = 0;
1293 if (term->print_state == 4) {
1294 term_print_finish(term);
1300 /* First see about all those translations. */
1301 if (term->termstate == TOPLEVEL) {
1303 switch (term->utf_state) {
1306 /* UTF-8 must be stateless so we ignore iso2022. */
1307 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1308 c = term->ucsdata->unitab_ctrl[c];
1309 else c = ((unsigned char)c) | ATTR_ASCII;
1311 } else if ((c & 0xe0) == 0xc0) {
1312 term->utf_size = term->utf_state = 1;
1313 term->utf_char = (c & 0x1f);
1314 } else if ((c & 0xf0) == 0xe0) {
1315 term->utf_size = term->utf_state = 2;
1316 term->utf_char = (c & 0x0f);
1317 } else if ((c & 0xf8) == 0xf0) {
1318 term->utf_size = term->utf_state = 3;
1319 term->utf_char = (c & 0x07);
1320 } else if ((c & 0xfc) == 0xf8) {
1321 term->utf_size = term->utf_state = 4;
1322 term->utf_char = (c & 0x03);
1323 } else if ((c & 0xfe) == 0xfc) {
1324 term->utf_size = term->utf_state = 5;
1325 term->utf_char = (c & 0x01);
1336 if ((c & 0xC0) != 0x80) {
1339 term->utf_state = 0;
1342 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1343 if (--term->utf_state)
1348 /* Is somebody trying to be evil! */
1350 (c < 0x800 && term->utf_size >= 2) ||
1351 (c < 0x10000 && term->utf_size >= 3) ||
1352 (c < 0x200000 && term->utf_size >= 4) ||
1353 (c < 0x4000000 && term->utf_size >= 5))
1356 /* Unicode line separator and paragraph separator are CR-LF */
1357 if (c == 0x2028 || c == 0x2029)
1360 /* High controls are probably a Baaad idea too. */
1364 /* The UTF-16 surrogates are not nice either. */
1365 /* The standard give the option of decoding these:
1366 * I don't want to! */
1367 if (c >= 0xD800 && c < 0xE000)
1370 /* ISO 10646 characters now limited to UTF-16 range. */
1374 /* This is currently a TagPhobic application.. */
1375 if (c >= 0xE0000 && c <= 0xE007F)
1378 /* U+FEFF is best seen as a null. */
1381 /* But U+FFFE is an error. */
1382 if (c == 0xFFFE || c == 0xFFFF)
1385 /* Oops this is a 16bit implementation */
1390 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1391 else if(term->sco_acs &&
1392 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1394 if (term->sco_acs == 2) c |= 0x80;
1397 switch (term->cset_attr[term->cset]) {
1399 * Linedraw characters are different from 'ESC ( B'
1400 * only for a small range. For ones outside that
1401 * range, make sure we use the same font as well as
1402 * the same encoding.
1405 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1406 c = term->ucsdata->unitab_ctrl[c];
1408 c = ((unsigned char) c) | ATTR_LINEDRW;
1412 /* If UK-ASCII, make the '#' a LineDraw Pound */
1414 c = '}' | ATTR_LINEDRW;
1417 /*FALLTHROUGH*/ case ATTR_ASCII:
1418 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1419 c = term->ucsdata->unitab_ctrl[c];
1421 c = ((unsigned char) c) | ATTR_ASCII;
1424 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1430 /* How about C1 controls ? */
1431 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1432 !term->vt52_mode && has_compat(VT220)) {
1433 term->termstate = SEEN_ESC;
1434 term->esc_query = FALSE;
1435 c = '@' + (c & 0x1F);
1438 /* Or the GL control. */
1439 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1440 if (term->curs.x && !term->wrapnext)
1442 term->wrapnext = FALSE;
1444 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1445 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1447 /* Or normal C0 controls. */
1448 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1450 case '\005': /* terminal type query */
1451 /* Strictly speaking this is VT100 but a VT100 defaults to
1452 * no response. Other terminals respond at their option.
1454 * Don't put a CR in the default string as this tends to
1455 * upset some weird software.
1457 * An xterm returns "xterm" (5 characters)
1459 compatibility(ANSIMIN);
1461 char abuf[256], *s, *d;
1463 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1465 if (*s >= 'a' && *s <= 'z')
1466 *d++ = (*s - ('a' - 1));
1467 else if ((*s >= '@' && *s <= '_') ||
1468 *s == '?' || (*s & 0x80))
1473 } else if (*s == '^') {
1478 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1484 struct beeptime *newbeep;
1485 unsigned long ticks;
1487 ticks = GETTICKCOUNT();
1489 if (!term->beep_overloaded) {
1490 newbeep = smalloc(sizeof(struct beeptime));
1491 newbeep->ticks = ticks;
1492 newbeep->next = NULL;
1493 if (!term->beephead)
1494 term->beephead = newbeep;
1496 term->beeptail->next = newbeep;
1497 term->beeptail = newbeep;
1502 * Throw out any beeps that happened more than
1505 while (term->beephead &&
1506 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1507 struct beeptime *tmp = term->beephead;
1508 term->beephead = tmp->next;
1510 if (!term->beephead)
1511 term->beeptail = NULL;
1515 if (term->cfg.bellovl && term->beep_overloaded &&
1516 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1518 * If we're currently overloaded and the
1519 * last beep was more than s seconds ago,
1520 * leave overload mode.
1522 term->beep_overloaded = FALSE;
1523 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1524 term->nbeeps >= term->cfg.bellovl_n) {
1526 * Now, if we have n or more beeps
1527 * remaining in the queue, go into overload
1530 term->beep_overloaded = TRUE;
1532 term->lastbeep = ticks;
1535 * Perform an actual beep if we're not overloaded.
1537 if (!term->cfg.bellovl || !term->beep_overloaded) {
1538 beep(term->frontend, term->cfg.beep);
1539 if (term->cfg.beep == BELL_VISUAL) {
1540 term->in_vbell = TRUE;
1541 term->vbell_startpoint = ticks;
1549 if (term->curs.x == 0 &&
1550 (term->curs.y == 0 || term->wrap == 0))
1552 else if (term->curs.x == 0 && term->curs.y > 0)
1553 term->curs.x = term->cols - 1, term->curs.y--;
1554 else if (term->wrapnext)
1555 term->wrapnext = FALSE;
1559 term->seen_disp_event = TRUE;
1562 compatibility(VT100);
1566 compatibility(VT100);
1570 if (term->vt52_mode)
1571 term->termstate = VT52_ESC;
1573 compatibility(ANSIMIN);
1574 term->termstate = SEEN_ESC;
1575 term->esc_query = FALSE;
1580 term->wrapnext = FALSE;
1582 term->seen_disp_event = TRUE;
1583 term->paste_hold = 0;
1585 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1588 if (has_compat(SCOANSI)) {
1589 move(term, 0, 0, 0);
1590 erase_lots(term, FALSE, FALSE, TRUE);
1592 term->wrapnext = FALSE;
1593 term->seen_disp_event = 1;
1597 compatibility(VT100);
1599 if (term->curs.y == term->marg_b)
1600 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1601 else if (term->curs.y < term->rows - 1)
1603 if (term->cfg.lfhascr)
1606 term->wrapnext = FALSE;
1607 term->seen_disp_event = 1;
1608 term->paste_hold = 0;
1610 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1614 pos old_curs = term->curs;
1615 unsigned long *ldata = lineptr(term->curs.y);
1619 } while (term->curs.x < term->cols - 1 &&
1620 !term->tabs[term->curs.x]);
1622 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1623 if (term->curs.x >= term->cols / 2)
1624 term->curs.x = term->cols / 2 - 1;
1626 if (term->curs.x >= term->cols)
1627 term->curs.x = term->cols - 1;
1631 check_selection(term, old_curs, term->curs);
1633 term->seen_disp_event = TRUE;
1637 switch (term->termstate) {
1639 /* Only graphic characters get this far;
1640 * ctrls are stripped above */
1641 if (term->wrapnext && term->wrap) {
1642 term->cpos[1] |= LATTR_WRAPPED;
1643 if (term->curs.y == term->marg_b)
1644 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1645 else if (term->curs.y < term->rows - 1)
1649 term->wrapnext = FALSE;
1653 if (term->selstate != NO_SELECTION) {
1654 pos cursplus = term->curs;
1656 check_selection(term, term->curs, cursplus);
1658 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1660 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1666 width = wcwidth((wchar_t) c);
1670 * If we're about to display a double-width
1671 * character starting in the rightmost
1672 * column, then we do something special
1673 * instead. We must print a space in the
1674 * last column of the screen, then wrap;
1675 * and we also set LATTR_WRAPPED2 which
1676 * instructs subsequent cut-and-pasting not
1677 * only to splice this line to the one
1678 * after it, but to ignore the space in the
1679 * last character position as well.
1680 * (Because what was actually output to the
1681 * terminal was presumably just a sequence
1682 * of CJK characters, and we don't want a
1683 * space to be pasted in the middle of
1684 * those just because they had the
1685 * misfortune to start in the wrong parity
1686 * column. xterm concurs.)
1688 check_boundary(term, term->curs.x, term->curs.y);
1689 check_boundary(term, term->curs.x+2, term->curs.y);
1690 if (term->curs.x == term->cols-1) {
1691 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1692 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1693 if (term->curs.y == term->marg_b)
1694 scroll(term, term->marg_t, term->marg_b,
1696 else if (term->curs.y < term->rows - 1)
1700 /* Now we must check_boundary again, of course. */
1701 check_boundary(term, term->curs.x, term->curs.y);
1702 check_boundary(term, term->curs.x+2, term->curs.y);
1704 *term->cpos++ = c | term->curr_attr;
1705 *term->cpos++ = UCSWIDE | term->curr_attr;
1709 check_boundary(term, term->curs.x, term->curs.y);
1710 check_boundary(term, term->curs.x+1, term->curs.y);
1711 *term->cpos++ = c | term->curr_attr;
1718 if (term->curs.x == term->cols) {
1721 term->wrapnext = TRUE;
1722 if (term->wrap && term->vt52_mode) {
1723 term->cpos[1] |= LATTR_WRAPPED;
1724 if (term->curs.y == term->marg_b)
1725 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1726 else if (term->curs.y < term->rows - 1)
1730 term->wrapnext = FALSE;
1733 term->seen_disp_event = 1;
1738 * This state is virtually identical to SEEN_ESC, with the
1739 * exception that we have an OSC sequence in the pipeline,
1740 * and _if_ we see a backslash, we process it.
1744 term->termstate = TOPLEVEL;
1747 /* else fall through */
1749 if (c >= ' ' && c <= '/') {
1750 if (term->esc_query)
1751 term->esc_query = -1;
1753 term->esc_query = c;
1756 term->termstate = TOPLEVEL;
1757 switch (ANSI(c, term->esc_query)) {
1758 case '[': /* enter CSI mode */
1759 term->termstate = SEEN_CSI;
1760 term->esc_nargs = 1;
1761 term->esc_args[0] = ARG_DEFAULT;
1762 term->esc_query = FALSE;
1764 case ']': /* xterm escape sequences */
1765 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1766 compatibility(OTHER);
1767 term->termstate = SEEN_OSC;
1768 term->esc_args[0] = 0;
1770 case '7': /* save cursor */
1771 compatibility(VT100);
1772 save_cursor(term, TRUE);
1774 case '8': /* restore cursor */
1775 compatibility(VT100);
1776 save_cursor(term, FALSE);
1777 term->seen_disp_event = TRUE;
1780 compatibility(VT100);
1781 term->app_keypad_keys = TRUE;
1784 compatibility(VT100);
1785 term->app_keypad_keys = FALSE;
1787 case 'D': /* exactly equivalent to LF */
1788 compatibility(VT100);
1789 if (term->curs.y == term->marg_b)
1790 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1791 else if (term->curs.y < term->rows - 1)
1794 term->wrapnext = FALSE;
1795 term->seen_disp_event = TRUE;
1797 case 'E': /* exactly equivalent to CR-LF */
1798 compatibility(VT100);
1800 if (term->curs.y == term->marg_b)
1801 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1802 else if (term->curs.y < term->rows - 1)
1805 term->wrapnext = FALSE;
1806 term->seen_disp_event = TRUE;
1808 case 'M': /* reverse index - backwards LF */
1809 compatibility(VT100);
1810 if (term->curs.y == term->marg_t)
1811 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1812 else if (term->curs.y > 0)
1815 term->wrapnext = FALSE;
1816 term->seen_disp_event = TRUE;
1818 case 'Z': /* terminal type query */
1819 compatibility(VT100);
1821 ldisc_send(term->ldisc, term->id_string,
1822 strlen(term->id_string), 0);
1824 case 'c': /* restore power-on settings */
1825 compatibility(VT100);
1827 if (term->ldisc) /* cause ldisc to notice changes */
1828 ldisc_send(term->ldisc, NULL, 0, 0);
1829 if (term->reset_132) {
1830 if (!term->cfg.no_remote_resize)
1831 request_resize(term->frontend, 80, term->rows);
1832 term->reset_132 = 0;
1836 term->seen_disp_event = TRUE;
1838 case 'H': /* set a tab */
1839 compatibility(VT100);
1840 term->tabs[term->curs.x] = TRUE;
1843 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1844 compatibility(VT100);
1846 unsigned long *ldata;
1850 for (i = 0; i < term->rows; i++) {
1852 for (j = 0; j < term->cols; j++)
1853 ldata[j] = ATTR_DEFAULT | 'E';
1854 ldata[term->cols] = 0;
1857 term->seen_disp_event = TRUE;
1858 scrtop.x = scrtop.y = 0;
1860 scrbot.y = term->rows;
1861 check_selection(term, scrtop, scrbot);
1865 case ANSI('3', '#'):
1866 case ANSI('4', '#'):
1867 case ANSI('5', '#'):
1868 case ANSI('6', '#'):
1869 compatibility(VT100);
1871 unsigned long nlattr;
1872 unsigned long *ldata;
1873 switch (ANSI(c, term->esc_query)) {
1874 case ANSI('3', '#'):
1877 case ANSI('4', '#'):
1880 case ANSI('5', '#'):
1881 nlattr = LATTR_NORM;
1883 default: /* spiritually case ANSI('6', '#'): */
1884 nlattr = LATTR_WIDE;
1887 ldata = lineptr(term->curs.y);
1888 ldata[term->cols] &= ~LATTR_MODE;
1889 ldata[term->cols] |= nlattr;
1893 case ANSI('A', '('):
1894 compatibility(VT100);
1895 if (!term->cfg.no_remote_charset)
1896 term->cset_attr[0] = ATTR_GBCHR;
1898 case ANSI('B', '('):
1899 compatibility(VT100);
1900 if (!term->cfg.no_remote_charset)
1901 term->cset_attr[0] = ATTR_ASCII;
1903 case ANSI('0', '('):
1904 compatibility(VT100);
1905 if (!term->cfg.no_remote_charset)
1906 term->cset_attr[0] = ATTR_LINEDRW;
1908 case ANSI('U', '('):
1909 compatibility(OTHER);
1910 if (!term->cfg.no_remote_charset)
1911 term->cset_attr[0] = ATTR_SCOACS;
1914 case ANSI('A', ')'):
1915 compatibility(VT100);
1916 if (!term->cfg.no_remote_charset)
1917 term->cset_attr[1] = ATTR_GBCHR;
1919 case ANSI('B', ')'):
1920 compatibility(VT100);
1921 if (!term->cfg.no_remote_charset)
1922 term->cset_attr[1] = ATTR_ASCII;
1924 case ANSI('0', ')'):
1925 compatibility(VT100);
1926 if (!term->cfg.no_remote_charset)
1927 term->cset_attr[1] = ATTR_LINEDRW;
1929 case ANSI('U', ')'):
1930 compatibility(OTHER);
1931 if (!term->cfg.no_remote_charset)
1932 term->cset_attr[1] = ATTR_SCOACS;
1935 case ANSI('8', '%'): /* Old Linux code */
1936 case ANSI('G', '%'):
1937 compatibility(OTHER);
1938 if (!term->cfg.no_remote_charset)
1941 case ANSI('@', '%'):
1942 compatibility(OTHER);
1943 if (!term->cfg.no_remote_charset)
1949 term->termstate = TOPLEVEL; /* default */
1951 if (term->esc_nargs <= ARGS_MAX) {
1952 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1953 term->esc_args[term->esc_nargs - 1] = 0;
1954 term->esc_args[term->esc_nargs - 1] =
1955 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1957 term->termstate = SEEN_CSI;
1958 } else if (c == ';') {
1959 if (++term->esc_nargs <= ARGS_MAX)
1960 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1961 term->termstate = SEEN_CSI;
1962 } else if (c < '@') {
1963 if (term->esc_query)
1964 term->esc_query = -1;
1966 term->esc_query = TRUE;
1968 term->esc_query = c;
1969 term->termstate = SEEN_CSI;
1971 switch (ANSI(c, term->esc_query)) {
1972 case 'A': /* move up N lines */
1973 move(term, term->curs.x,
1974 term->curs.y - def(term->esc_args[0], 1), 1);
1975 term->seen_disp_event = TRUE;
1977 case 'e': /* move down N lines */
1978 compatibility(ANSI);
1981 move(term, term->curs.x,
1982 term->curs.y + def(term->esc_args[0], 1), 1);
1983 term->seen_disp_event = TRUE;
1985 case ANSI('c', '>'): /* report xterm version */
1986 compatibility(OTHER);
1987 /* this reports xterm version 136 so that VIM can
1988 use the drag messages from the mouse reporting */
1990 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1992 case 'a': /* move right N cols */
1993 compatibility(ANSI);
1996 move(term, term->curs.x + def(term->esc_args[0], 1),
1998 term->seen_disp_event = TRUE;
2000 case 'D': /* move left N cols */
2001 move(term, term->curs.x - def(term->esc_args[0], 1),
2003 term->seen_disp_event = TRUE;
2005 case 'E': /* move down N lines and CR */
2006 compatibility(ANSI);
2008 term->curs.y + def(term->esc_args[0], 1), 1);
2009 term->seen_disp_event = TRUE;
2011 case 'F': /* move up N lines and CR */
2012 compatibility(ANSI);
2014 term->curs.y - def(term->esc_args[0], 1), 1);
2015 term->seen_disp_event = TRUE;
2018 case '`': /* set horizontal posn */
2019 compatibility(ANSI);
2020 move(term, def(term->esc_args[0], 1) - 1,
2022 term->seen_disp_event = TRUE;
2024 case 'd': /* set vertical posn */
2025 compatibility(ANSI);
2026 move(term, term->curs.x,
2027 ((term->dec_om ? term->marg_t : 0) +
2028 def(term->esc_args[0], 1) - 1),
2029 (term->dec_om ? 2 : 0));
2030 term->seen_disp_event = TRUE;
2033 case 'f': /* set horz and vert posns at once */
2034 if (term->esc_nargs < 2)
2035 term->esc_args[1] = ARG_DEFAULT;
2036 move(term, def(term->esc_args[1], 1) - 1,
2037 ((term->dec_om ? term->marg_t : 0) +
2038 def(term->esc_args[0], 1) - 1),
2039 (term->dec_om ? 2 : 0));
2040 term->seen_disp_event = TRUE;
2042 case 'J': /* erase screen or parts of it */
2044 unsigned int i = def(term->esc_args[0], 0) + 1;
2047 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2050 term->seen_disp_event = TRUE;
2052 case 'K': /* erase line or parts of it */
2054 unsigned int i = def(term->esc_args[0], 0) + 1;
2057 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2059 term->seen_disp_event = TRUE;
2061 case 'L': /* insert lines */
2062 compatibility(VT102);
2063 if (term->curs.y <= term->marg_b)
2064 scroll(term, term->curs.y, term->marg_b,
2065 -def(term->esc_args[0], 1), FALSE);
2067 term->seen_disp_event = TRUE;
2069 case 'M': /* delete lines */
2070 compatibility(VT102);
2071 if (term->curs.y <= term->marg_b)
2072 scroll(term, term->curs.y, term->marg_b,
2073 def(term->esc_args[0], 1),
2076 term->seen_disp_event = TRUE;
2078 case '@': /* insert chars */
2079 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2080 compatibility(VT102);
2081 insch(term, def(term->esc_args[0], 1));
2082 term->seen_disp_event = TRUE;
2084 case 'P': /* delete chars */
2085 compatibility(VT102);
2086 insch(term, -def(term->esc_args[0], 1));
2087 term->seen_disp_event = TRUE;
2089 case 'c': /* terminal type query */
2090 compatibility(VT100);
2091 /* This is the response for a VT102 */
2093 ldisc_send(term->ldisc, term->id_string,
2094 strlen(term->id_string), 0);
2096 case 'n': /* cursor position query */
2098 if (term->esc_args[0] == 6) {
2100 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2102 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2103 } else if (term->esc_args[0] == 5) {
2104 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2108 case 'h': /* toggle modes to high */
2110 compatibility(VT100);
2113 for (i = 0; i < term->esc_nargs; i++)
2114 toggle_mode(term, term->esc_args[i],
2115 term->esc_query, TRUE);
2120 compatibility(VT100);
2122 if (term->esc_nargs != 1) break;
2123 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2124 term->printing = TRUE;
2125 term->only_printing = !term->esc_query;
2126 term->print_state = 0;
2127 term_print_setup(term);
2128 } else if (term->esc_args[0] == 4 &&
2130 term_print_finish(term);
2134 case 'l': /* toggle modes to low */
2136 compatibility(VT100);
2139 for (i = 0; i < term->esc_nargs; i++)
2140 toggle_mode(term, term->esc_args[i],
2141 term->esc_query, FALSE);
2144 case 'g': /* clear tabs */
2145 compatibility(VT100);
2146 if (term->esc_nargs == 1) {
2147 if (term->esc_args[0] == 0) {
2148 term->tabs[term->curs.x] = FALSE;
2149 } else if (term->esc_args[0] == 3) {
2151 for (i = 0; i < term->cols; i++)
2152 term->tabs[i] = FALSE;
2156 case 'r': /* set scroll margins */
2157 compatibility(VT100);
2158 if (term->esc_nargs <= 2) {
2160 top = def(term->esc_args[0], 1) - 1;
2161 bot = (term->esc_nargs <= 1
2162 || term->esc_args[1] == 0 ?
2164 def(term->esc_args[1], term->rows)) - 1;
2165 if (bot >= term->rows)
2166 bot = term->rows - 1;
2167 /* VTTEST Bug 9 - if region is less than 2 lines
2168 * don't change region.
2170 if (bot - top > 0) {
2175 * I used to think the cursor should be
2176 * placed at the top of the newly marginned
2177 * area. Apparently not: VMS TPU falls over
2180 * Well actually it should for
2183 term->curs.y = (term->dec_om ?
2186 term->seen_disp_event = TRUE;
2190 case 'm': /* set graphics rendition */
2193 * A VT100 without the AVO only had one
2194 * attribute, either underline or
2195 * reverse video depending on the
2196 * cursor type, this was selected by
2200 * This is sometimes DIM, eg on the
2203 * This is sometimes INVIS various ANSI.
2205 * This like 22 disables BOLD, DIM and INVIS
2207 * The ANSI colours appear on any
2208 * terminal that has colour (obviously)
2209 * but the interaction between sgr0 and
2210 * the colours varies but is usually
2211 * related to the background colour
2212 * erase item. The interaction between
2213 * colour attributes and the mono ones
2214 * is also very implementation
2217 * The 39 and 49 attributes are likely
2218 * to be unimplemented.
2221 for (i = 0; i < term->esc_nargs; i++) {
2222 switch (def(term->esc_args[i], 0)) {
2223 case 0: /* restore defaults */
2224 term->curr_attr = ATTR_DEFAULT;
2226 case 1: /* enable bold */
2227 compatibility(VT100AVO);
2228 term->curr_attr |= ATTR_BOLD;
2230 case 21: /* (enable double underline) */
2231 compatibility(OTHER);
2232 case 4: /* enable underline */
2233 compatibility(VT100AVO);
2234 term->curr_attr |= ATTR_UNDER;
2236 case 5: /* enable blink */
2237 compatibility(VT100AVO);
2238 term->curr_attr |= ATTR_BLINK;
2240 case 7: /* enable reverse video */
2241 term->curr_attr |= ATTR_REVERSE;
2243 case 10: /* SCO acs off */
2244 compatibility(SCOANSI);
2245 if (term->cfg.no_remote_charset) break;
2246 term->sco_acs = 0; break;
2247 case 11: /* SCO acs on */
2248 compatibility(SCOANSI);
2249 if (term->cfg.no_remote_charset) break;
2250 term->sco_acs = 1; break;
2251 case 12: /* SCO acs on, |0x80 */
2252 compatibility(SCOANSI);
2253 if (term->cfg.no_remote_charset) break;
2254 term->sco_acs = 2; break;
2255 case 22: /* disable bold */
2256 compatibility2(OTHER, VT220);
2257 term->curr_attr &= ~ATTR_BOLD;
2259 case 24: /* disable underline */
2260 compatibility2(OTHER, VT220);
2261 term->curr_attr &= ~ATTR_UNDER;
2263 case 25: /* disable blink */
2264 compatibility2(OTHER, VT220);
2265 term->curr_attr &= ~ATTR_BLINK;
2267 case 27: /* disable reverse video */
2268 compatibility2(OTHER, VT220);
2269 term->curr_attr &= ~ATTR_REVERSE;
2280 term->curr_attr &= ~ATTR_FGMASK;
2282 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2284 case 39: /* default-foreground */
2285 term->curr_attr &= ~ATTR_FGMASK;
2286 term->curr_attr |= ATTR_DEFFG;
2297 term->curr_attr &= ~ATTR_BGMASK;
2299 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2301 case 49: /* default-background */
2302 term->curr_attr &= ~ATTR_BGMASK;
2303 term->curr_attr |= ATTR_DEFBG;
2308 term->erase_char = (' ' | ATTR_ASCII |
2314 case 's': /* save cursor */
2315 save_cursor(term, TRUE);
2317 case 'u': /* restore cursor */
2318 save_cursor(term, FALSE);
2319 term->seen_disp_event = TRUE;
2321 case 't': /* set page size - ie window height */
2323 * VT340/VT420 sequence DECSLPP, DEC only allows values
2324 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2325 * illegal values (eg first arg 1..9) for window changing
2328 if (term->esc_nargs <= 1
2329 && (term->esc_args[0] < 1 ||
2330 term->esc_args[0] >= 24)) {
2331 compatibility(VT340TEXT);
2332 if (!term->cfg.no_remote_resize)
2333 request_resize(term->frontend, term->cols,
2334 def(term->esc_args[0], 24));
2336 } else if (term->esc_nargs >= 1 &&
2337 term->esc_args[0] >= 1 &&
2338 term->esc_args[0] < 24) {
2339 compatibility(OTHER);
2341 switch (term->esc_args[0]) {
2345 set_iconic(term->frontend, FALSE);
2348 set_iconic(term->frontend, TRUE);
2351 if (term->esc_nargs >= 3) {
2352 if (!term->cfg.no_remote_resize)
2353 move_window(term->frontend,
2354 def(term->esc_args[1], 0),
2355 def(term->esc_args[2], 0));
2359 /* We should resize the window to a given
2360 * size in pixels here, but currently our
2361 * resizing code isn't healthy enough to
2366 set_zorder(term->frontend, TRUE);
2369 /* move to bottom */
2370 set_zorder(term->frontend, FALSE);
2373 refresh_window(term->frontend);
2376 if (term->esc_nargs >= 3) {
2377 if (!term->cfg.no_remote_resize)
2378 request_resize(term->frontend,
2379 def(term->esc_args[2], term->cfg.width),
2380 def(term->esc_args[1], term->cfg.height));
2384 if (term->esc_nargs >= 2)
2385 set_zoomed(term->frontend,
2391 ldisc_send(term->ldisc,
2392 is_iconic(term->frontend) ?
2393 "\033[1t" : "\033[2t", 4, 0);
2397 get_window_pos(term->frontend, &x, &y);
2398 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2399 ldisc_send(term->ldisc, buf, len, 0);
2404 get_window_pixels(term->frontend, &x, &y);
2405 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2406 ldisc_send(term->ldisc, buf, len, 0);
2411 len = sprintf(buf, "\033[8;%d;%dt",
2412 term->rows, term->cols);
2413 ldisc_send(term->ldisc, buf, len, 0);
2418 * Hmmm. Strictly speaking we
2419 * should return `the size of the
2420 * screen in characters', but
2421 * that's not easy: (a) window
2422 * furniture being what it is it's
2423 * hard to compute, and (b) in
2424 * resize-font mode maximising the
2425 * window wouldn't change the
2426 * number of characters. *shrug*. I
2427 * think we'll ignore it for the
2428 * moment and see if anyone
2429 * complains, and then ask them
2430 * what they would like it to do.
2435 p = get_window_title(term->frontend, TRUE);
2437 ldisc_send(term->ldisc, "\033]L", 3, 0);
2438 ldisc_send(term->ldisc, p, len, 0);
2439 ldisc_send(term->ldisc, "\033\\", 2, 0);
2444 p = get_window_title(term->frontend,FALSE);
2446 ldisc_send(term->ldisc, "\033]l", 3, 0);
2447 ldisc_send(term->ldisc, p, len, 0);
2448 ldisc_send(term->ldisc, "\033\\", 2, 0);
2455 compatibility(SCOANSI);
2456 scroll(term, term->marg_t, term->marg_b,
2457 def(term->esc_args[0], 1), TRUE);
2459 term->wrapnext = FALSE;
2460 term->seen_disp_event = TRUE;
2463 compatibility(SCOANSI);
2464 scroll(term, term->marg_t, term->marg_b,
2465 -def(term->esc_args[0], 1), TRUE);
2467 term->wrapnext = FALSE;
2468 term->seen_disp_event = TRUE;
2470 case ANSI('|', '*'):
2471 /* VT420 sequence DECSNLS
2472 * Set number of lines on screen
2473 * VT420 uses VGA like hardware and can support any size in
2474 * reasonable range (24..49 AIUI) with no default specified.
2476 compatibility(VT420);
2477 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2478 if (!term->cfg.no_remote_resize)
2479 request_resize(term->frontend, term->cols,
2480 def(term->esc_args[0],
2485 case ANSI('|', '$'):
2486 /* VT340/VT420 sequence DECSCPP
2487 * Set number of columns per page
2488 * Docs imply range is only 80 or 132, but I'll allow any.
2490 compatibility(VT340TEXT);
2491 if (term->esc_nargs <= 1) {
2492 if (!term->cfg.no_remote_resize)
2493 request_resize(term->frontend,
2494 def(term->esc_args[0],
2495 term->cfg.width), term->rows);
2499 case 'X': /* write N spaces w/o moving cursor */
2500 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2501 compatibility(ANSIMIN);
2503 int n = def(term->esc_args[0], 1);
2505 unsigned long *p = term->cpos;
2506 if (n > term->cols - term->curs.x)
2507 n = term->cols - term->curs.x;
2508 cursplus = term->curs;
2510 check_boundary(term, term->curs.x, term->curs.y);
2511 check_boundary(term, term->curs.x+n, term->curs.y);
2512 check_selection(term, term->curs, cursplus);
2514 *p++ = term->erase_char;
2515 term->seen_disp_event = TRUE;
2518 case 'x': /* report terminal characteristics */
2519 compatibility(VT100);
2522 int i = def(term->esc_args[0], 0);
2523 if (i == 0 || i == 1) {
2524 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2526 ldisc_send(term->ldisc, buf, 20, 0);
2530 case 'Z': /* BackTab for xterm */
2531 compatibility(OTHER);
2533 int i = def(term->esc_args[0], 1);
2534 pos old_curs = term->curs;
2536 for(;i>0 && term->curs.x>0; i--) {
2539 } while (term->curs.x >0 &&
2540 !term->tabs[term->curs.x]);
2543 check_selection(term, old_curs, term->curs);
2546 case ANSI('L', '='):
2547 compatibility(OTHER);
2548 term->use_bce = (term->esc_args[0] <= 0);
2549 term->erase_char = ERASE_CHAR;
2551 term->erase_char = (' ' | ATTR_ASCII |
2553 (ATTR_FGMASK | ATTR_BGMASK)));
2555 case ANSI('E', '='):
2556 compatibility(OTHER);
2557 term->blink_is_real = (term->esc_args[0] >= 1);
2559 case ANSI('p', '"'):
2561 * Allow the host to make this emulator a
2562 * 'perfect' VT102. This first appeared in
2563 * the VT220, but we do need to get back to
2564 * PuTTY mode so I won't check it.
2566 * The arg in 40..42,50 are a PuTTY extension.
2567 * The 2nd arg, 8bit vs 7bit is not checked.
2569 * Setting VT102 mode should also change
2570 * the Fkeys to generate PF* codes as a
2571 * real VT102 has no Fkeys. The VT220 does
2572 * this, F11..F13 become ESC,BS,LF other
2573 * Fkeys send nothing.
2575 * Note ESC c will NOT change this!
2578 switch (term->esc_args[0]) {
2580 term->compatibility_level &= ~TM_VTXXX;
2581 term->compatibility_level |= TM_VT102;
2584 term->compatibility_level &= ~TM_VTXXX;
2585 term->compatibility_level |= TM_VT220;
2589 if (term->esc_args[0] > 60 &&
2590 term->esc_args[0] < 70)
2591 term->compatibility_level |= TM_VTXXX;
2595 term->compatibility_level &= TM_VTXXX;
2598 term->compatibility_level = TM_PUTTY;
2601 term->compatibility_level = TM_SCOANSI;
2605 term->compatibility_level = TM_PUTTY;
2611 /* Change the response to CSI c */
2612 if (term->esc_args[0] == 50) {
2615 strcpy(term->id_string, "\033[?");
2616 for (i = 1; i < term->esc_nargs; i++) {
2618 strcat(term->id_string, ";");
2619 sprintf(lbuf, "%d", term->esc_args[i]);
2620 strcat(term->id_string, lbuf);
2622 strcat(term->id_string, "c");
2625 /* Is this a good idea ?
2626 * Well we should do a soft reset at this point ...
2628 if (!has_compat(VT420) && has_compat(VT100)) {
2629 if (!term->cfg.no_remote_resize) {
2630 if (term->reset_132)
2631 request_resize(132, 24);
2633 request_resize(80, 24);
2641 term->osc_w = FALSE;
2643 case 'P': /* Linux palette sequence */
2644 term->termstate = SEEN_OSC_P;
2645 term->osc_strlen = 0;
2647 case 'R': /* Linux palette reset */
2648 palette_reset(term->frontend);
2649 term_invalidate(term);
2650 term->termstate = TOPLEVEL;
2652 case 'W': /* word-set */
2653 term->termstate = SEEN_OSC_W;
2666 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2670 * Grotty hack to support xterm and DECterm title
2671 * sequences concurrently.
2673 if (term->esc_args[0] == 2) {
2674 term->esc_args[0] = 1;
2677 /* else fall through */
2679 term->termstate = OSC_STRING;
2680 term->osc_strlen = 0;
2685 * This OSC stuff is EVIL. It takes just one character to get into
2686 * sysline mode and it's not initially obvious how to get out.
2687 * So I've added CR and LF as string aborts.
2688 * This shouldn't effect compatibility as I believe embedded
2689 * control characters are supposed to be interpreted (maybe?)
2690 * and they don't display anything useful anyway.
2694 if (c == '\012' || c == '\015') {
2695 term->termstate = TOPLEVEL;
2696 } else if (c == 0234 || c == '\007') {
2698 * These characters terminate the string; ST and BEL
2699 * terminate the sequence and trigger instant
2700 * processing of it, whereas ESC goes back to SEEN_ESC
2701 * mode unless it is followed by \, in which case it is
2702 * synonymous with ST in the first place.
2705 term->termstate = TOPLEVEL;
2706 } else if (c == '\033')
2707 term->termstate = OSC_MAYBE_ST;
2708 else if (term->osc_strlen < OSC_STR_MAX)
2709 term->osc_string[term->osc_strlen++] = c;
2713 int max = (term->osc_strlen == 0 ? 21 : 16);
2715 if (c >= '0' && c <= '9')
2717 else if (c >= 'A' && c <= 'A' + max - 10)
2719 else if (c >= 'a' && c <= 'a' + max - 10)
2722 term->termstate = TOPLEVEL;
2725 term->osc_string[term->osc_strlen++] = val;
2726 if (term->osc_strlen >= 7) {
2727 palette_set(term->frontend, term->osc_string[0],
2728 term->osc_string[1] * 16 + term->osc_string[2],
2729 term->osc_string[3] * 16 + term->osc_string[4],
2730 term->osc_string[5] * 16 + term->osc_string[6]);
2731 term_invalidate(term);
2732 term->termstate = TOPLEVEL;
2748 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2751 term->termstate = OSC_STRING;
2752 term->osc_strlen = 0;
2756 term->termstate = TOPLEVEL;
2757 term->seen_disp_event = TRUE;
2760 move(term, term->curs.x, term->curs.y - 1, 1);
2763 move(term, term->curs.x, term->curs.y + 1, 1);
2766 move(term, term->curs.x + 1, term->curs.y, 1);
2769 move(term, term->curs.x - 1, term->curs.y, 1);
2772 * From the VT100 Manual
2773 * NOTE: The special graphics characters in the VT100
2774 * are different from those in the VT52
2776 * From VT102 manual:
2777 * 137 _ Blank - Same
2778 * 140 ` Reserved - Humm.
2779 * 141 a Solid rectangle - Similar
2780 * 142 b 1/ - Top half of fraction for the
2781 * 143 c 3/ - subscript numbers below.
2784 * 146 f Degrees - Same
2785 * 147 g Plus or minus - Same
2787 * 151 i Ellipsis (dots)
2790 * 154 l Bar at scan 0
2791 * 155 m Bar at scan 1
2792 * 156 n Bar at scan 2
2793 * 157 o Bar at scan 3 - Similar
2794 * 160 p Bar at scan 4 - Similar
2795 * 161 q Bar at scan 5 - Similar
2796 * 162 r Bar at scan 6 - Same
2797 * 163 s Bar at scan 7 - Similar
2812 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2815 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2818 move(term, 0, 0, 0);
2821 if (term->curs.y == 0)
2822 scroll(term, 0, term->rows - 1, -1, TRUE);
2823 else if (term->curs.y > 0)
2826 term->wrapnext = FALSE;
2829 erase_lots(term, FALSE, FALSE, TRUE);
2833 erase_lots(term, TRUE, FALSE, TRUE);
2837 /* XXX Print cursor line */
2840 /* XXX Start controller mode */
2843 /* XXX Stop controller mode */
2847 term->termstate = VT52_Y1;
2851 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2854 term->app_keypad_keys = TRUE;
2857 term->app_keypad_keys = FALSE;
2860 /* XXX This should switch to VT100 mode not current or default
2861 * VT mode. But this will only have effect in a VT220+
2864 term->vt52_mode = FALSE;
2865 term->blink_is_real = term->cfg.blinktext;
2869 /* XXX Enter auto print mode */
2872 /* XXX Exit auto print mode */
2875 /* XXX Print screen */
2881 /* compatibility(ATARI) */
2882 move(term, 0, 0, 0);
2883 erase_lots(term, FALSE, FALSE, TRUE);
2887 /* compatibility(ATARI) */
2888 if (term->curs.y <= term->marg_b)
2889 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2892 /* compatibility(ATARI) */
2893 if (term->curs.y <= term->marg_b)
2894 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2897 /* compatibility(ATARI) */
2898 term->termstate = VT52_FG;
2901 /* compatibility(ATARI) */
2902 term->termstate = VT52_BG;
2905 /* compatibility(ATARI) */
2906 erase_lots(term, FALSE, TRUE, FALSE);
2910 /* compatibility(ATARI) */
2911 term->cursor_on = TRUE;
2914 /* compatibility(ATARI) */
2915 term->cursor_on = FALSE;
2917 /* case 'j': Save cursor position - broken on ST */
2918 /* case 'k': Restore cursor position */
2920 /* compatibility(ATARI) */
2921 erase_lots(term, TRUE, TRUE, TRUE);
2923 term->wrapnext = FALSE;
2927 /* compatibility(ATARI) */
2928 erase_lots(term, TRUE, TRUE, FALSE);
2931 /* compatibility(ATARI) */
2932 term->curr_attr |= ATTR_REVERSE;
2935 /* compatibility(ATARI) */
2936 term->curr_attr &= ~ATTR_REVERSE;
2938 case 'v': /* wrap Autowrap on - Wyse style */
2939 /* compatibility(ATARI) */
2942 case 'w': /* Autowrap off */
2943 /* compatibility(ATARI) */
2948 /* compatibility(OTHER) */
2949 term->vt52_bold = FALSE;
2950 term->curr_attr = ATTR_DEFAULT;
2952 term->erase_char = (' ' | ATTR_ASCII |
2954 (ATTR_FGMASK | ATTR_BGMASK)));
2957 /* compatibility(VI50) */
2958 term->curr_attr |= ATTR_UNDER;
2961 /* compatibility(VI50) */
2962 term->curr_attr &= ~ATTR_UNDER;
2965 /* compatibility(VI50) */
2966 term->vt52_bold = TRUE;
2967 term->curr_attr |= ATTR_BOLD;
2970 /* compatibility(VI50) */
2971 term->vt52_bold = FALSE;
2972 term->curr_attr &= ~ATTR_BOLD;
2978 term->termstate = VT52_Y2;
2979 move(term, term->curs.x, c - ' ', 0);
2982 term->termstate = TOPLEVEL;
2983 move(term, c - ' ', term->curs.y, 0);
2988 term->termstate = TOPLEVEL;
2989 term->curr_attr &= ~ATTR_FGMASK;
2990 term->curr_attr &= ~ATTR_BOLD;
2991 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2992 if ((c & 0x8) || term->vt52_bold)
2993 term->curr_attr |= ATTR_BOLD;
2996 term->erase_char = (' ' | ATTR_ASCII |
2998 (ATTR_FGMASK | ATTR_BGMASK)));
3001 term->termstate = TOPLEVEL;
3002 term->curr_attr &= ~ATTR_BGMASK;
3003 term->curr_attr &= ~ATTR_BLINK;
3004 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3006 /* Note: bold background */
3008 term->curr_attr |= ATTR_BLINK;
3011 term->erase_char = (' ' | ATTR_ASCII |
3013 (ATTR_FGMASK | ATTR_BGMASK)));
3016 default: break; /* placate gcc warning about enum use */
3018 if (term->selstate != NO_SELECTION) {
3019 pos cursplus = term->curs;
3021 check_selection(term, term->curs, cursplus);
3025 term_print_flush(term);
3030 * Compare two lines to determine whether they are sufficiently
3031 * alike to scroll-optimise one to the other. Return the degree of
3034 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3038 for (i = n = 0; i < term->cols; i++)
3039 n += (*a++ == *b++);
3045 * Given a context, update the window. Out of paranoia, we don't
3046 * allow WM_PAINT responses to do scrolling optimisations.
3048 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3050 int i, j, our_curs_y, our_curs_x;
3051 unsigned long rv, cursor;
3054 long cursor_background = ERASE_CHAR;
3055 unsigned long ticks;
3058 * Check the visual bell state.
3060 if (term->in_vbell) {
3061 ticks = GETTICKCOUNT();
3062 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3063 term->in_vbell = FALSE;
3066 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3069 * screen array, disptop, scrtop,
3071 * cfg.blinkpc, blink_is_real, tblinker,
3072 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3075 /* Has the cursor position or type changed ? */
3076 if (term->cursor_on) {
3077 if (term->has_focus) {
3078 if (term->blinker || !term->cfg.blink_cur)
3079 cursor = TATTR_ACTCURS;
3083 cursor = TATTR_PASCURS;
3085 cursor |= TATTR_RIGHTCURS;
3088 our_curs_y = term->curs.y - term->disptop;
3091 * Adjust the cursor position in the case where it's
3092 * resting on the right-hand half of a CJK wide character.
3093 * xterm's behaviour here, which seems adequate to me, is
3094 * to display the cursor covering the _whole_ character,
3095 * exactly as if it were one space to the left.
3097 unsigned long *ldata = lineptr(term->curs.y);
3098 our_curs_x = term->curs.x;
3099 if (our_curs_x > 0 &&
3100 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3104 if (term->dispcurs && (term->curstype != cursor ||
3106 term->disptext + our_curs_y * (term->cols + 1) +
3108 if (term->dispcurs > term->disptext &&
3109 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3110 term->dispcurs[-1] |= ATTR_INVALID;
3111 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3112 term->dispcurs[1] |= ATTR_INVALID;
3113 *term->dispcurs |= ATTR_INVALID;
3116 term->dispcurs = NULL;
3118 /* The normal screen data */
3119 for (i = 0; i < term->rows; i++) {
3120 unsigned long *ldata;
3122 int idx, dirty_line, dirty_run, selected;
3123 unsigned long attr = 0;
3124 int updated_line = 0;
3127 int last_run_dirty = 0;
3129 scrpos.y = i + term->disptop;
3130 ldata = lineptr(scrpos.y);
3131 lattr = (ldata[term->cols] & LATTR_MODE);
3133 idx = i * (term->cols + 1);
3134 dirty_run = dirty_line = (ldata[term->cols] !=
3135 term->disptext[idx + term->cols]);
3136 term->disptext[idx + term->cols] = ldata[term->cols];
3138 for (j = 0; j < term->cols; j++, idx++) {
3139 unsigned long tattr, tchar;
3140 unsigned long *d = ldata + j;
3144 tchar = (*d & (CHAR_MASK | CSET_MASK));
3145 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3146 switch (tchar & CSET_MASK) {
3148 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3151 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3154 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3157 tattr |= (tchar & CSET_MASK);
3159 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3162 /* Video reversing things */
3163 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3164 if (term->seltype == LEXICOGRAPHIC)
3165 selected = (posle(term->selstart, scrpos) &&
3166 poslt(scrpos, term->selend));
3168 selected = (posPle(term->selstart, scrpos) &&
3169 posPlt(scrpos, term->selend));
3173 ^ (selected ? ATTR_REVERSE : 0));
3175 /* 'Real' blinking ? */
3176 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3177 if (term->has_focus && term->tblinker) {
3179 tattr &= ~CSET_MASK;
3182 tattr &= ~ATTR_BLINK;
3186 * Check the font we'll _probably_ be using to see if
3187 * the character is wide when we don't want it to be.
3189 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3190 if ((tattr & ATTR_WIDE) == 0 &&
3191 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3192 tattr |= ATTR_NARROW;
3193 } else if (term->disptext[idx]&ATTR_NARROW)
3194 tattr |= ATTR_NARROW;
3196 /* Cursor here ? Save the 'background' */
3197 if (i == our_curs_y && j == our_curs_x) {
3198 cursor_background = tattr | tchar;
3199 term->dispcurs = term->disptext + idx;
3202 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3205 break_run = (((tattr ^ attr) & term->attr_mask) ||
3206 j - start >= sizeof(ch));
3208 /* Special hack for VT100 Linedraw glyphs */
3209 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3210 && tchar <= 0xBD) break_run = TRUE;
3212 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3213 if ((tchar | tattr) == term->disptext[idx])
3215 else if (!dirty_run && ccount == 1)
3220 if ((dirty_run || last_run_dirty) && ccount > 0) {
3221 do_text(ctx, start, i, ch, ccount, attr, lattr);
3227 if (term->ucsdata->dbcs_screenfont)
3228 last_run_dirty = dirty_run;
3229 dirty_run = dirty_line;
3232 if ((tchar | tattr) != term->disptext[idx])
3234 ch[ccount++] = (char) tchar;
3235 term->disptext[idx] = tchar | tattr;
3237 /* If it's a wide char step along to the next one. */
3238 if (tattr & ATTR_WIDE) {
3239 if (++j < term->cols) {
3243 * By construction above, the cursor should not
3244 * be on the right-hand half of this character.
3247 assert(!(i == our_curs_y && j == our_curs_x));
3248 if (term->disptext[idx] != *d)
3250 term->disptext[idx] = *d;
3254 if (dirty_run && ccount > 0) {
3255 do_text(ctx, start, i, ch, ccount, attr, lattr);
3259 /* Cursor on this line ? (and changed) */
3260 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3261 ch[0] = (char) (cursor_background & CHAR_MASK);
3262 attr = (cursor_background & ATTR_MASK) | cursor;
3263 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3264 term->curstype = cursor;
3270 * Flick the switch that says if blinking things should be shown or hidden.
3273 void term_blink(Terminal *term, int flg)
3275 long now, blink_diff;
3277 now = GETTICKCOUNT();
3278 blink_diff = now - term->last_tblink;
3280 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3281 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3282 term->last_tblink = now;
3283 term->tblinker = !term->tblinker;
3288 term->last_blink = now;
3292 blink_diff = now - term->last_blink;
3294 /* Make sure the cursor blinks no faster than system blink rate */
3295 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3298 term->last_blink = now;
3299 term->blinker = !term->blinker;
3303 * Invalidate the whole screen so it will be repainted in full.
3305 void term_invalidate(Terminal *term)
3309 for (i = 0; i < term->rows * (term->cols + 1); i++)
3310 term->disptext[i] = ATTR_INVALID;
3314 * Paint the window in response to a WM_PAINT message.
3316 void term_paint(Terminal *term, Context ctx,
3317 int left, int top, int right, int bottom, int immediately)
3320 if (left < 0) left = 0;
3321 if (top < 0) top = 0;
3322 if (right >= term->cols) right = term->cols-1;
3323 if (bottom >= term->rows) bottom = term->rows-1;
3325 for (i = top; i <= bottom && i < term->rows; i++) {
3326 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3327 LATTR_MODE) == LATTR_NORM)
3328 for (j = left; j <= right && j < term->cols; j++)
3329 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3331 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3332 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3335 /* This should happen soon enough, also for some reason it sometimes
3336 * fails to actually do anything when re-sizing ... painting the wrong
3340 do_paint (term, ctx, FALSE);
3344 * Attempt to scroll the scrollback. The second parameter gives the
3345 * position we want to scroll to; the first is +1 to denote that
3346 * this position is relative to the beginning of the scrollback, -1
3347 * to denote it is relative to the end, and 0 to denote that it is
3348 * relative to the current position.
3350 void term_scroll(Terminal *term, int rel, int where)
3352 int sbtop = -count234(term->scrollback);
3353 #ifdef OPTIMISE_SCROLL
3354 int olddisptop = term->disptop;
3356 #endif /* OPTIMISE_SCROLL */
3358 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3359 if (term->disptop < sbtop)
3360 term->disptop = sbtop;
3361 if (term->disptop > 0)
3364 #ifdef OPTIMISE_SCROLL
3365 shift = (term->disptop - olddisptop);
3366 if (shift < term->rows && shift > -term->rows)
3367 scroll_display(term, 0, term->rows - 1, shift);
3368 #endif /* OPTIMISE_SCROLL */
3372 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3375 wchar_t *wbptr; /* where next char goes within workbuf */
3377 int wblen = 0; /* workbuf len */
3378 int buflen; /* amount of memory allocated to workbuf */
3380 buflen = 5120; /* Default size */
3381 workbuf = smalloc(buflen * sizeof(wchar_t));
3382 wbptr = workbuf; /* start filling here */
3383 old_top_x = top.x; /* needed for rect==1 */
3385 while (poslt(top, bottom)) {
3387 unsigned long *ldata = lineptr(top.y);
3391 * nlpos will point at the maximum position on this line we
3392 * should copy up to. So we start it at the end of the
3396 nlpos.x = term->cols;
3399 * ... move it backwards if there's unused space at the end
3400 * of the line (and also set `nl' if this is the case,
3401 * because in normal selection mode this means we need a
3402 * newline at the end)...
3404 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3405 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3406 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3407 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3408 && poslt(top, nlpos))
3410 if (poslt(nlpos, bottom))
3412 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3413 /* Ignore the last char on the line in a WRAPPED2 line. */
3418 * ... and then clip it to the terminal x coordinate if
3419 * we're doing rectangular selection. (In this case we
3420 * still did the above, so that copying e.g. the right-hand
3421 * column from a table doesn't fill with spaces on the
3425 if (nlpos.x > bottom.x)
3427 nl = (top.y < bottom.y);
3430 while (poslt(top, bottom) && poslt(top, nlpos)) {
3433 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3435 wchar_t cbuf[16], *p;
3436 int uc = (ldata[top.x] & 0xFFFF);
3439 if (uc == UCSWIDE) {
3444 switch (uc & CSET_MASK) {
3446 if (!term->cfg.rawcnp) {
3447 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3451 uc = term->ucsdata->unitab_line[uc & 0xFF];
3454 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3457 switch (uc & CSET_MASK) {
3459 uc = term->ucsdata->unitab_font[uc & 0xFF];
3462 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3466 set = (uc & CSET_MASK);
3467 c = (uc & CHAR_MASK);
3471 if (DIRECT_FONT(uc)) {
3472 if (c >= ' ' && c != 0x7F) {
3476 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3478 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3479 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3483 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3487 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3494 for (p = cbuf; *p; p++) {
3495 /* Enough overhead for trailing NL and nul */
3496 if (wblen >= buflen - 16) {
3499 sizeof(wchar_t) * (buflen += 100));
3500 wbptr = workbuf + wblen;
3509 for (i = 0; i < sel_nl_sz; i++) {
3511 *wbptr++ = sel_nl[i];
3515 top.x = rect ? old_top_x : 0;
3517 #if SELECTION_NUL_TERMINATED
3521 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3522 if (buflen > 0) /* indicates we allocated this buffer */
3526 void term_copyall(Terminal *term)
3529 top.y = -count234(term->scrollback);
3531 clipme(term, top, term->curs, 0);
3535 * The wordness array is mainly for deciding the disposition of the
3536 * US-ASCII characters.
3538 static int wordtype(Terminal *term, int uc)
3541 int start, end, ctype;
3543 static const struct ucsword ucs_words[] = {
3549 0x037e, 0x037e, 1}, /* Greek question mark */
3551 0x0387, 0x0387, 1}, /* Greek ano teleia */
3553 0x055a, 0x055f, 1}, /* Armenian punctuation */
3555 0x0589, 0x0589, 1}, /* Armenian full stop */
3557 0x0700, 0x070d, 1}, /* Syriac punctuation */
3559 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3561 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3563 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3565 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3567 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3569 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3571 0x2000, 0x200a, 0}, /* Various spaces */
3573 0x2070, 0x207f, 2}, /* superscript */
3575 0x2080, 0x208f, 2}, /* subscript */
3577 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3579 0x3000, 0x3000, 0}, /* ideographic space */
3581 0x3001, 0x3020, 1}, /* ideographic punctuation */
3583 0x303f, 0x309f, 3}, /* Hiragana */
3585 0x30a0, 0x30ff, 3}, /* Katakana */
3587 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3589 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3591 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3593 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3595 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3597 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3599 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3601 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3603 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3607 const struct ucsword *wptr;
3609 uc &= (CSET_MASK | CHAR_MASK);
3611 switch (uc & CSET_MASK) {
3613 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3616 uc = term->ucsdata->unitab_line[uc & 0xFF];
3619 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3622 switch (uc & CSET_MASK) {
3624 uc = term->ucsdata->unitab_font[uc & 0xFF];
3627 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3631 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3632 * fail as there's such a thing as a double width space. :-(
3634 if (term->ucsdata->dbcs_screenfont &&
3635 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3639 return term->wordness[uc];
3641 for (wptr = ucs_words; wptr->start; wptr++) {
3642 if (uc >= wptr->start && uc <= wptr->end)
3650 * Spread the selection outwards according to the selection mode.
3652 static pos sel_spread_half(Terminal *term, pos p, int dir)
3654 unsigned long *ldata;
3656 int topy = -count234(term->scrollback);
3658 ldata = lineptr(p.y);
3660 switch (term->selmode) {
3663 * In this mode, every character is a separate unit, except
3664 * for runs of spaces at the end of a non-wrapping line.
3666 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3667 unsigned long *q = ldata + term->cols;
3668 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3670 if (q == ldata + term->cols)
3672 if (p.x >= q - ldata)
3673 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3678 * In this mode, the units are maximal runs of characters
3679 * whose `wordness' has the same value.
3681 wvalue = wordtype(term, UCSGET(ldata, p.x));
3684 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3685 term->cols-1 : term->cols);
3686 if (p.x < maxcols-1) {
3687 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3692 if (ldata[term->cols] & LATTR_WRAPPED) {
3693 unsigned long *ldata2;
3694 ldata2 = lineptr(p.y+1);
3695 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3708 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3713 unsigned long *ldata2;
3717 ldata2 = lineptr(p.y-1);
3718 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3719 term->cols-1 : term->cols);
3720 if (ldata2[term->cols] & LATTR_WRAPPED) {
3721 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3736 * In this mode, every line is a unit.
3738 p.x = (dir == -1 ? 0 : term->cols - 1);
3744 static void sel_spread(Terminal *term)
3746 if (term->seltype == LEXICOGRAPHIC) {
3747 term->selstart = sel_spread_half(term, term->selstart, -1);
3748 decpos(term->selend);
3749 term->selend = sel_spread_half(term, term->selend, +1);
3750 incpos(term->selend);
3754 void term_do_paste(Terminal *term)
3759 get_clip(term->frontend, &data, &len);
3760 if (data && len > 0) {
3763 term_seen_key_event(term); /* pasted data counts */
3765 if (term->paste_buffer)
3766 sfree(term->paste_buffer);
3767 term->paste_pos = term->paste_hold = term->paste_len = 0;
3768 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3771 while (p < data + len) {
3772 while (p < data + len &&
3773 !(p <= data + len - sel_nl_sz &&
3774 !memcmp(p, sel_nl, sizeof(sel_nl))))
3779 for (i = 0; i < p - q; i++) {
3780 term->paste_buffer[term->paste_len++] = q[i];
3784 if (p <= data + len - sel_nl_sz &&
3785 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3786 term->paste_buffer[term->paste_len++] = '\015';
3792 /* Assume a small paste will be OK in one go. */
3793 if (term->paste_len < 256) {
3795 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3796 if (term->paste_buffer)
3797 sfree(term->paste_buffer);
3798 term->paste_buffer = 0;
3799 term->paste_pos = term->paste_hold = term->paste_len = 0;
3802 get_clip(term->frontend, NULL, NULL);
3805 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
3806 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
3809 unsigned long *ldata;
3810 int raw_mouse = (term->xterm_mouse &&
3811 !term->cfg.no_mouse_rep &&
3812 !(term->cfg.mouse_override && shift));
3813 int default_seltype;
3817 if (a == MA_DRAG && !raw_mouse)
3818 term_scroll(term, 0, -1);
3820 if (y >= term->rows) {
3822 if (a == MA_DRAG && !raw_mouse)
3823 term_scroll(term, 0, +1);
3832 if (x >= term->cols)
3835 selpoint.y = y + term->disptop;
3837 ldata = lineptr(selpoint.y);
3838 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3842 int encstate = 0, r, c;
3849 encstate = 0x20; /* left button down */
3860 case MBT_WHEEL_DOWN:
3863 default: break; /* placate gcc warning about enum use */
3867 if (term->xterm_mouse == 1)
3873 term->mouse_is_down = 0;
3876 if (term->mouse_is_down == braw)
3878 term->mouse_is_down = braw;
3880 default: break; /* placate gcc warning about enum use */
3889 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3890 ldisc_send(term->ldisc, abuf, 6, 0);
3896 * Set the selection type (rectangular or normal) at the start
3897 * of a selection attempt, from the state of Alt.
3899 if (!alt ^ !term->cfg.rect_select)
3900 default_seltype = RECTANGULAR;
3902 default_seltype = LEXICOGRAPHIC;
3904 if (term->selstate == NO_SELECTION) {
3905 term->seltype = default_seltype;
3908 if (bcooked == MBT_SELECT && a == MA_CLICK) {
3910 term->selstate = ABOUT_TO;
3911 term->seltype = default_seltype;
3912 term->selanchor = selpoint;
3913 term->selmode = SM_CHAR;
3914 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3916 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3917 term->selstate = DRAGGING;
3918 term->selstart = term->selanchor = selpoint;
3919 term->selend = term->selstart;
3920 incpos(term->selend);
3922 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
3923 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
3924 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3926 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
3927 term->selstate == SELECTED) {
3928 if (term->seltype == LEXICOGRAPHIC) {
3930 * For normal selection, we extend by moving
3931 * whichever end of the current selection is closer
3934 if (posdiff(selpoint, term->selstart) <
3935 posdiff(term->selend, term->selstart) / 2) {
3936 term->selanchor = term->selend;
3937 decpos(term->selanchor);
3939 term->selanchor = term->selstart;
3943 * For rectangular selection, we have a choice of
3944 * _four_ places to put selanchor and selpoint: the
3945 * four corners of the selection.
3947 if (2*selpoint.x < term->selstart.x + term->selend.x)
3948 term->selanchor.x = term->selend.x-1;
3950 term->selanchor.x = term->selstart.x;
3952 if (2*selpoint.y < term->selstart.y + term->selend.y)
3953 term->selanchor.y = term->selend.y;
3955 term->selanchor.y = term->selstart.y;
3957 term->selstate = DRAGGING;
3959 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3960 term->selanchor = selpoint;
3961 term->selstate = DRAGGING;
3962 if (term->seltype == LEXICOGRAPHIC) {
3964 * For normal selection, we set (selstart,selend) to
3965 * (selpoint,selanchor) in some order.
3967 if (poslt(selpoint, term->selanchor)) {
3968 term->selstart = selpoint;
3969 term->selend = term->selanchor;
3970 incpos(term->selend);
3972 term->selstart = term->selanchor;
3973 term->selend = selpoint;
3974 incpos(term->selend);
3978 * For rectangular selection, we may need to
3979 * interchange x and y coordinates (if the user has
3980 * dragged in the -x and +y directions, or vice versa).
3982 term->selstart.x = min(term->selanchor.x, selpoint.x);
3983 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3984 term->selstart.y = min(term->selanchor.y, selpoint.y);
3985 term->selend.y = max(term->selanchor.y, selpoint.y);
3988 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
3990 if (term->selstate == DRAGGING) {
3992 * We've completed a selection. We now transfer the
3993 * data to the clipboard.
3995 clipme(term, term->selstart, term->selend,
3996 (term->seltype == RECTANGULAR));
3997 term->selstate = SELECTED;
3999 term->selstate = NO_SELECTION;
4000 } else if (bcooked == MBT_PASTE
4002 #if MULTICLICK_ONLY_EVENT
4003 || a == MA_2CLK || a == MA_3CLK
4006 request_paste(term->frontend);
4012 void term_nopaste(Terminal *term)
4014 if (term->paste_len == 0)
4016 sfree(term->paste_buffer);
4017 term->paste_buffer = NULL;
4018 term->paste_len = 0;
4021 int term_paste_pending(Terminal *term)
4023 return term->paste_len != 0;
4026 void term_paste(Terminal *term)
4028 long now, paste_diff;
4030 if (term->paste_len == 0)
4033 /* Don't wait forever to paste */
4034 if (term->paste_hold) {
4035 now = GETTICKCOUNT();
4036 paste_diff = now - term->last_paste;
4037 if (paste_diff >= 0 && paste_diff < 450)
4040 term->paste_hold = 0;
4042 while (term->paste_pos < term->paste_len) {
4044 while (n + term->paste_pos < term->paste_len) {
4045 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4049 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4050 term->paste_pos += n;
4052 if (term->paste_pos < term->paste_len) {
4053 term->paste_hold = 1;
4057 sfree(term->paste_buffer);
4058 term->paste_buffer = NULL;
4059 term->paste_len = 0;
4062 static void deselect(Terminal *term)
4064 term->selstate = NO_SELECTION;
4065 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4068 void term_deselect(Terminal *term)
4074 int term_ldisc(Terminal *term, int option)
4076 if (option == LD_ECHO)
4077 return term->term_echoing;
4078 if (option == LD_EDIT)
4079 return term->term_editing;
4084 * from_backend(), to get data from the backend for the terminal.
4086 int from_backend(void *vterm, int is_stderr, char *data, int len)
4088 Terminal *term = (Terminal *)vterm;
4092 bufchain_add(&term->inbuf, data, len);
4095 * term_out() always completely empties inbuf. Therefore,
4096 * there's no reason at all to return anything other than zero
4097 * from this function, because there _can't_ be a question of
4098 * the remote side needing to wait until term_out() has cleared
4101 * This is a slightly suboptimal way to deal with SSH2 - in
4102 * principle, the window mechanism would allow us to continue
4103 * to accept data on forwarded ports and X connections even
4104 * while the terminal processing was going slowly - but we
4105 * can't do the 100% right thing without moving the terminal
4106 * processing into a separate thread, and that might hurt
4107 * portability. So we manage stdout buffering the old SSH1 way:
4108 * if the terminal processing goes slowly, the whole SSH
4109 * connection stops accepting data until it's ready.
4111 * In practice, I can't imagine this causing serious trouble.
4116 void term_provide_logctx(Terminal *term, void *logctx)
4118 term->logctx = logctx;