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;
395 * Set up the terminal for a given size.
397 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
400 unsigned long *newdisp, *line;
403 int save_alt_which = term->alt_which;
405 if (newrows == term->rows && newcols == term->cols &&
406 newsavelines == term->savelines)
407 return; /* nothing to do */
410 swap_screen(term, 0, FALSE, FALSE);
412 term->alt_t = term->marg_t = 0;
413 term->alt_b = term->marg_b = newrows - 1;
415 if (term->rows == -1) {
416 term->scrollback = newtree234(NULL);
417 term->screen = newtree234(NULL);
422 * Resize the screen and scrollback. We only need to shift
423 * lines around within our data structures, because lineptr()
424 * will take care of resizing each individual line if
427 * - If the new screen and the old screen differ in length, we
428 * must shunt some lines in from the scrollback or out to
431 * - If doing that fails to provide us with enough material to
432 * fill the new screen (i.e. the number of rows needed in
433 * the new screen exceeds the total number in the previous
434 * screen+scrollback), we must invent some blank lines to
437 * - Then, if the new scrollback length is less than the
438 * amount of scrollback we actually have, we must throw some
441 sblen = count234(term->scrollback);
442 /* Do this loop to expand the screen if newrows > rows */
443 for (i = term->rows; i < newrows; i++) {
445 line = delpos234(term->scrollback, --sblen);
447 line = smalloc(TSIZE * (newcols + 2));
449 for (j = 0; j <= newcols; j++)
450 line[j + 1] = ERASE_CHAR;
452 addpos234(term->screen, line, 0);
454 /* Do this loop to shrink the screen if newrows < rows */
455 for (i = newrows; i < term->rows; i++) {
456 line = delpos234(term->screen, 0);
457 addpos234(term->scrollback, line, sblen++);
459 assert(count234(term->screen) == newrows);
460 while (sblen > newsavelines) {
461 line = delpos234(term->scrollback, 0);
465 assert(count234(term->scrollback) <= newsavelines);
468 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
469 for (i = 0; i < newrows * (newcols + 1); i++)
470 newdisp[i] = ATTR_INVALID;
471 sfree(term->disptext);
472 term->disptext = newdisp;
473 term->dispcurs = NULL;
475 newalt = newtree234(NULL);
476 for (i = 0; i < newrows; i++) {
477 line = smalloc(TSIZE * (newcols + 2));
479 for (j = 0; j <= newcols; j++)
480 line[j + 1] = term->erase_char;
481 addpos234(newalt, line, i);
483 if (term->alt_screen) {
484 while (NULL != (line = delpos234(term->alt_screen, 0)))
486 freetree234(term->alt_screen);
488 term->alt_screen = newalt;
490 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
493 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
494 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
498 term->curs.y += newrows - term->rows;
499 if (term->curs.y < 0)
501 if (term->curs.y >= newrows)
502 term->curs.y = newrows - 1;
503 if (term->curs.x >= newcols)
504 term->curs.x = newcols - 1;
505 term->alt_x = term->alt_y = 0;
506 term->wrapnext = term->alt_wnext = FALSE;
508 term->rows = newrows;
509 term->cols = newcols;
510 term->savelines = newsavelines;
513 swap_screen(term, save_alt_which, FALSE, FALSE);
518 term->resize_fn(term->resize_ctx, term->cols, term->rows);
522 * Hand a function and context pointer to the terminal which it can
523 * use to notify a back end of resizes.
525 void term_provide_resize_fn(Terminal *term,
526 void (*resize_fn)(void *, int, int),
529 term->resize_fn = resize_fn;
530 term->resize_ctx = resize_ctx;
531 if (term->cols > 0 && term->rows > 0)
532 resize_fn(resize_ctx, term->cols, term->rows);
536 * Swap screens. If `reset' is TRUE and we have been asked to
537 * switch to the alternate screen, we must bring most of its
538 * configuration from the main screen and erase the contents of the
539 * alternate screen completely. (This is even true if we're already
540 * on it! Blame xterm.)
542 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
548 reset = FALSE; /* do no weird resetting if which==0 */
550 if (which != term->alt_which) {
551 term->alt_which = which;
553 ttr = term->alt_screen;
554 term->alt_screen = term->screen;
557 if (!reset && !keep_cur_pos)
558 term->curs.x = term->alt_x;
561 if (!reset && !keep_cur_pos)
562 term->curs.y = term->alt_y;
565 if (!reset) term->marg_t = term->alt_t;
568 if (!reset) term->marg_b = term->alt_b;
571 if (!reset) term->dec_om = term->alt_om;
574 if (!reset) term->wrap = term->alt_wrap;
577 if (!reset) term->wrapnext = term->alt_wnext;
580 if (!reset) term->insert = term->alt_ins;
583 if (!reset) term->cset = term->alt_cset;
586 if (!reset) term->utf = term->alt_utf;
589 if (!reset) term->sco_acs = term->alt_sco_acs;
590 term->alt_sco_acs = t;
593 if (reset && term->screen) {
595 * Yes, this _is_ supposed to honour background-colour-erase.
597 erase_lots(term, FALSE, TRUE, TRUE);
601 * This might not be possible if we're called during
609 * Update the scroll bar.
611 static void update_sbar(Terminal *term)
615 nscroll = count234(term->scrollback);
617 set_sbar(term->frontend, nscroll + term->rows,
618 nscroll + term->disptop, term->rows);
622 * Check whether the region bounded by the two pointers intersects
623 * the scroll region, and de-select the on-screen selection if so.
625 static void check_selection(Terminal *term, pos from, pos to)
627 if (poslt(from, term->selend) && poslt(term->selstart, to))
632 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
633 * for backward.) `sb' is TRUE if the scrolling is permitted to
634 * affect the scrollback buffer.
636 * NB this function invalidates all pointers into lines of the
637 * screen data structures. In particular, you MUST call fix_cpos
638 * after calling scroll() and before doing anything else that
639 * uses the cpos shortcut pointer.
641 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
643 unsigned long *line, *line2;
644 int i, seltop, olddisptop, shift;
646 if (topline != 0 || term->alt_which != 0)
649 olddisptop = term->disptop;
653 line = delpos234(term->screen, botline);
654 line = resizeline(line, term->cols);
655 for (i = 0; i < term->cols; i++)
656 line[i + 1] = term->erase_char;
657 line[term->cols + 1] = 0;
658 addpos234(term->screen, line, topline);
660 if (term->selstart.y >= topline && term->selstart.y <= botline) {
662 if (term->selstart.y > botline) {
663 term->selstart.y = botline;
664 term->selstart.x = 0;
667 if (term->selend.y >= topline && term->selend.y <= botline) {
669 if (term->selend.y > botline) {
670 term->selend.y = botline;
679 line = delpos234(term->screen, topline);
680 if (sb && term->savelines > 0) {
681 int sblen = count234(term->scrollback);
683 * We must add this line to the scrollback. We'll
684 * remove a line from the top of the scrollback to
685 * replace it, or allocate a new one if the
686 * scrollback isn't full.
688 if (sblen == term->savelines) {
689 sblen--, line2 = delpos234(term->scrollback, 0);
691 line2 = smalloc(TSIZE * (term->cols + 2));
692 line2[0] = term->cols;
694 addpos234(term->scrollback, line, sblen);
698 * If the user is currently looking at part of the
699 * scrollback, and they haven't enabled any options
700 * that are going to reset the scrollback as a
701 * result of this movement, then the chances are
702 * they'd like to keep looking at the same line. So
703 * we move their viewpoint at the same rate as the
704 * scroll, at least until their viewpoint hits the
705 * top end of the scrollback buffer, at which point
706 * we don't have the choice any more.
708 * Thanks to Jan Holmen Holsten for the idea and
709 * initial implementation.
711 if (term->disptop > -term->savelines && term->disptop < 0)
714 line = resizeline(line, term->cols);
715 for (i = 0; i < term->cols; i++)
716 line[i + 1] = term->erase_char;
717 line[term->cols + 1] = 0;
718 addpos234(term->screen, line, botline);
721 * If the selection endpoints move into the scrollback,
722 * we keep them moving until they hit the top. However,
723 * of course, if the line _hasn't_ moved into the
724 * scrollback then we don't do this, and cut them off
725 * at the top of the scroll region.
727 * This applies to selstart and selend (for an existing
728 * selection), and also selanchor (for one being
729 * selected as we speak).
731 seltop = sb ? -term->savelines : topline;
733 if (term->selstart.y >= seltop &&
734 term->selstart.y <= botline) {
736 if (term->selstart.y < seltop) {
737 term->selstart.y = seltop;
738 term->selstart.x = 0;
741 if (term->selend.y >= seltop && term->selend.y <= botline) {
743 if (term->selend.y < seltop) {
744 term->selend.y = seltop;
748 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
750 if (term->selanchor.y < seltop) {
751 term->selanchor.y = seltop;
752 term->selanchor.x = 0;
759 #ifdef OPTIMISE_SCROLL
760 shift += term->disptop - olddisptop;
761 if (shift < term->rows && shift > -term->rows && shift != 0)
762 scroll_display(term, topline, botline, shift);
763 #endif /* OPTIMISE_SCROLL */
766 #ifdef OPTIMISE_SCROLL
768 * Scroll the physical display, and our conception of it in disptext.
770 static void scroll_display(Terminal *term, int topline, int botline, int lines)
772 unsigned long *start, *end;
773 int distance, size, i;
775 start = term->disptext + topline * (term->cols + 1);
776 end = term->disptext + (botline + 1) * (term->cols + 1);
777 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
778 size = end - start - distance;
780 memmove(start, start + distance, size * TSIZE);
781 if (term->dispcurs >= start + distance &&
782 term->dispcurs <= start + distance + size)
783 term->dispcurs -= distance;
784 for (i = 0; i < distance; i++)
785 (start + size)[i] |= ATTR_INVALID;
787 memmove(start + distance, start, size * TSIZE);
788 if (term->dispcurs >= start && term->dispcurs <= start + size)
789 term->dispcurs += distance;
790 for (i = 0; i < distance; i++)
791 start[i] |= ATTR_INVALID;
793 do_scroll(term->frontend, topline, botline, lines);
795 #endif /* OPTIMISE_SCROLL */
798 * Move the cursor to a given position, clipping at boundaries. We
799 * may or may not want to clip at the scroll margin: marg_clip is 0
800 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
801 * even _being_ outside the margins.
803 static void move(Terminal *term, int x, int y, int marg_clip)
810 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
813 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
824 term->wrapnext = FALSE;
828 * Save or restore the cursor and SGR mode.
830 static void save_cursor(Terminal *term, int save)
833 term->savecurs = term->curs;
834 term->save_attr = term->curr_attr;
835 term->save_cset = term->cset;
836 term->save_utf = term->utf;
837 term->save_wnext = term->wrapnext;
838 term->save_csattr = term->cset_attr[term->cset];
839 term->save_sco_acs = term->sco_acs;
841 term->curs = term->savecurs;
842 /* Make sure the window hasn't shrunk since the save */
843 if (term->curs.x >= term->cols)
844 term->curs.x = term->cols - 1;
845 if (term->curs.y >= term->rows)
846 term->curs.y = term->rows - 1;
848 term->curr_attr = term->save_attr;
849 term->cset = term->save_cset;
850 term->utf = term->save_utf;
851 term->wrapnext = term->save_wnext;
853 * wrapnext might reset to False if the x position is no
854 * longer at the rightmost edge.
856 if (term->wrapnext && term->curs.x < term->cols-1)
857 term->wrapnext = FALSE;
858 term->cset_attr[term->cset] = term->save_csattr;
859 term->sco_acs = term->save_sco_acs;
862 term->erase_char = (' ' | ATTR_ASCII |
864 (ATTR_FGMASK | ATTR_BGMASK)));
869 * This function is called before doing _anything_ which affects
870 * only part of a line of text. It is used to mark the boundary
871 * between two character positions, and it indicates that some sort
872 * of effect is going to happen on only one side of that boundary.
874 * The effect of this function is to check whether a CJK
875 * double-width character is straddling the boundary, and to remove
876 * it and replace it with two spaces if so. (Of course, one or
877 * other of those spaces is then likely to be replaced with
878 * something else again, as a result of whatever happens next.)
880 * Also, if the boundary is at the right-hand _edge_ of the screen,
881 * it implies something deliberate is being done to the rightmost
882 * column position; hence we must clear LATTR_WRAPPED2.
884 * The input to the function is the coordinates of the _second_
885 * character of the pair.
887 static void check_boundary(Terminal *term, int x, int y)
889 unsigned long *ldata;
891 /* Validate input coordinates, just in case. */
892 if (x == 0 || x > term->cols)
896 if (x == term->cols) {
897 ldata[x] &= ~LATTR_WRAPPED2;
899 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
900 ldata[x-1] = ldata[x] =
901 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
907 * Erase a large portion of the screen: the whole screen, or the
908 * whole line, or parts thereof.
910 static void erase_lots(Terminal *term,
911 int line_only, int from_begin, int to_end)
915 unsigned long *ldata;
918 start.y = term->curs.y;
920 end.y = term->curs.y + 1;
937 if (!from_begin || !to_end)
938 check_boundary(term, term->curs.x, term->curs.y);
939 check_selection(term, start, end);
941 /* Clear screen also forces a full window redraw, just in case. */
942 if (start.y == 0 && start.x == 0 && end.y == term->rows)
943 term_invalidate(term);
945 ldata = lineptr(start.y);
946 while (poslt(start, end)) {
947 if (start.x == term->cols && !erase_lattr)
948 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
950 ldata[start.x] = term->erase_char;
951 if (incpos(start) && start.y < term->rows)
952 ldata = lineptr(start.y);
957 * Insert or delete characters within the current line. n is +ve if
958 * insertion is desired, and -ve for deletion.
960 static void insch(Terminal *term, int n)
962 int dir = (n < 0 ? -1 : +1);
965 unsigned long *ldata;
967 n = (n < 0 ? -n : n);
968 if (n > term->cols - term->curs.x)
969 n = term->cols - term->curs.x;
970 m = term->cols - term->curs.x - n;
971 cursplus.y = term->curs.y;
972 cursplus.x = term->curs.x + n;
973 check_selection(term, term->curs, cursplus);
974 check_boundary(term, term->curs.x, term->curs.y);
976 check_boundary(term, term->curs.x + n, term->curs.y);
977 ldata = lineptr(term->curs.y);
979 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
981 ldata[term->curs.x + m++] = term->erase_char;
983 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
985 ldata[term->curs.x + n] = term->erase_char;
990 * Toggle terminal mode `mode' to state `state'. (`query' indicates
991 * whether the mode is a DEC private one or a normal one.)
993 static void toggle_mode(Terminal *term, int mode, int query, int state)
999 case 1: /* application cursor keys */
1000 term->app_cursor_keys = state;
1002 case 2: /* VT52 mode */
1003 term->vt52_mode = !state;
1004 if (term->vt52_mode) {
1005 term->blink_is_real = FALSE;
1006 term->vt52_bold = FALSE;
1008 term->blink_is_real = term->cfg.blinktext;
1011 case 3: /* 80/132 columns */
1013 if (!term->cfg.no_remote_resize)
1014 request_resize(term->frontend, state ? 132 : 80, term->rows);
1015 term->reset_132 = state;
1017 case 5: /* reverse video */
1019 * Toggle reverse video. If we receive an OFF within the
1020 * visual bell timeout period after an ON, we trigger an
1021 * effective visual bell, so that ESC[?5hESC[?5l will
1022 * always be an actually _visible_ visual bell.
1024 ticks = GETTICKCOUNT();
1025 /* turn off a previous vbell to avoid inconsistencies */
1026 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1027 term->in_vbell = FALSE;
1028 if (term->rvideo && !state && /* we're turning it off... */
1029 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1030 /* If there's no vbell timeout already, or this one lasts
1031 * longer, replace vbell_timeout with ours. */
1032 if (!term->in_vbell ||
1033 (term->rvbell_startpoint - term->vbell_startpoint <
1035 term->vbell_startpoint = term->rvbell_startpoint;
1036 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1037 } else if (!term->rvideo && state) {
1038 /* This is an ON, so we notice the time and save it. */
1039 term->rvbell_startpoint = ticks;
1041 term->rvideo = state;
1042 term->seen_disp_event = TRUE;
1046 case 6: /* DEC origin mode */
1047 term->dec_om = state;
1049 case 7: /* auto wrap */
1052 case 8: /* auto key repeat */
1053 term->repeat_off = !state;
1055 case 10: /* set local edit mode */
1056 term->term_editing = state;
1057 if (term->ldisc) /* cause ldisc to notice changes */
1058 ldisc_send(term->ldisc, NULL, 0, 0);
1060 case 25: /* enable/disable cursor */
1061 compatibility2(OTHER, VT220);
1062 term->cursor_on = state;
1063 term->seen_disp_event = TRUE;
1065 case 47: /* alternate screen */
1066 compatibility(OTHER);
1068 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1071 case 1000: /* xterm mouse 1 */
1072 term->xterm_mouse = state ? 1 : 0;
1073 set_raw_mouse_mode(term->frontend, state);
1075 case 1002: /* xterm mouse 2 */
1076 term->xterm_mouse = state ? 2 : 0;
1077 set_raw_mouse_mode(term->frontend, state);
1079 case 1047: /* alternate screen */
1080 compatibility(OTHER);
1082 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1085 case 1048: /* save/restore cursor */
1086 save_cursor(term, state);
1087 if (!state) term->seen_disp_event = TRUE;
1089 case 1049: /* cursor & alternate screen */
1091 save_cursor(term, state);
1092 if (!state) term->seen_disp_event = TRUE;
1093 compatibility(OTHER);
1095 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1097 save_cursor(term, state);
1102 case 4: /* set insert mode */
1103 compatibility(VT102);
1104 term->insert = state;
1106 case 12: /* set echo mode */
1107 term->term_echoing = !state;
1108 if (term->ldisc) /* cause ldisc to notice changes */
1109 ldisc_send(term->ldisc, NULL, 0, 0);
1111 case 20: /* Return sends ... */
1112 term->cr_lf_return = state;
1114 case 34: /* Make cursor BIG */
1115 compatibility2(OTHER, VT220);
1116 term->big_cursor = !state;
1121 * Process an OSC sequence: set window title or icon name.
1123 static void do_osc(Terminal *term)
1126 while (term->osc_strlen--)
1127 term->wordness[(unsigned char)
1128 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1130 term->osc_string[term->osc_strlen] = '\0';
1131 switch (term->esc_args[0]) {
1134 if (!term->cfg.no_remote_wintitle)
1135 set_icon(term->frontend, term->osc_string);
1136 if (term->esc_args[0] == 1)
1138 /* fall through: parameter 0 means set both */
1141 if (!term->cfg.no_remote_wintitle)
1142 set_title(term->frontend, term->osc_string);
1149 * ANSI printing routines.
1151 static void term_print_setup(Terminal *term)
1153 bufchain_clear(&term->printer_buf);
1154 term->print_job = printer_start_job(term->cfg.printer);
1156 static void term_print_flush(Terminal *term)
1161 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1162 bufchain_prefix(&term->printer_buf, &data, &len);
1165 printer_job_data(term->print_job, data, len);
1166 bufchain_consume(&term->printer_buf, len);
1169 static void term_print_finish(Terminal *term)
1175 if (!term->printing && !term->only_printing)
1176 return; /* we need do nothing */
1178 term_print_flush(term);
1179 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1180 bufchain_prefix(&term->printer_buf, &data, &len);
1182 if (c == '\033' || c == '\233') {
1183 bufchain_consume(&term->printer_buf, size);
1186 printer_job_data(term->print_job, &c, 1);
1187 bufchain_consume(&term->printer_buf, 1);
1190 printer_finish_job(term->print_job);
1191 term->print_job = NULL;
1192 term->printing = term->only_printing = FALSE;
1196 * Remove everything currently in `inbuf' and stick it up on the
1197 * in-memory display. There's a big state machine in here to
1198 * process escape sequences...
1200 void term_out(Terminal *term)
1203 unsigned char localbuf[256], *chars;
1208 chars = NULL; /* placate compiler warnings */
1209 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1213 bufchain_prefix(&term->inbuf, &ret, &nchars);
1214 if (nchars > sizeof(localbuf))
1215 nchars = sizeof(localbuf);
1216 memcpy(localbuf, ret, nchars);
1217 bufchain_consume(&term->inbuf, nchars);
1219 assert(chars != NULL);
1225 * Optionally log the session traffic to a file. Useful for
1226 * debugging and possibly also useful for actual logging.
1228 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1229 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1235 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1236 * be able to display 8-bit characters, but I'll let that go 'cause
1241 * If we're printing, add the character to the printer
1244 if (term->printing) {
1245 bufchain_add(&term->printer_buf, &c, 1);
1248 * If we're in print-only mode, we use a much simpler
1249 * state machine designed only to recognise the ESC[4i
1250 * termination sequence.
1252 if (term->only_printing) {
1254 term->print_state = 1;
1255 else if (c == (unsigned char)'\233')
1256 term->print_state = 2;
1257 else if (c == '[' && term->print_state == 1)
1258 term->print_state = 2;
1259 else if (c == '4' && term->print_state == 2)
1260 term->print_state = 3;
1261 else if (c == 'i' && term->print_state == 3)
1262 term->print_state = 4;
1264 term->print_state = 0;
1265 if (term->print_state == 4) {
1266 term_print_finish(term);
1272 /* First see about all those translations. */
1273 if (term->termstate == TOPLEVEL) {
1275 switch (term->utf_state) {
1278 /* UTF-8 must be stateless so we ignore iso2022. */
1279 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1280 c = term->ucsdata->unitab_ctrl[c];
1281 else c = ((unsigned char)c) | ATTR_ASCII;
1283 } else if ((c & 0xe0) == 0xc0) {
1284 term->utf_size = term->utf_state = 1;
1285 term->utf_char = (c & 0x1f);
1286 } else if ((c & 0xf0) == 0xe0) {
1287 term->utf_size = term->utf_state = 2;
1288 term->utf_char = (c & 0x0f);
1289 } else if ((c & 0xf8) == 0xf0) {
1290 term->utf_size = term->utf_state = 3;
1291 term->utf_char = (c & 0x07);
1292 } else if ((c & 0xfc) == 0xf8) {
1293 term->utf_size = term->utf_state = 4;
1294 term->utf_char = (c & 0x03);
1295 } else if ((c & 0xfe) == 0xfc) {
1296 term->utf_size = term->utf_state = 5;
1297 term->utf_char = (c & 0x01);
1308 if ((c & 0xC0) != 0x80) {
1311 term->utf_state = 0;
1314 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1315 if (--term->utf_state)
1320 /* Is somebody trying to be evil! */
1322 (c < 0x800 && term->utf_size >= 2) ||
1323 (c < 0x10000 && term->utf_size >= 3) ||
1324 (c < 0x200000 && term->utf_size >= 4) ||
1325 (c < 0x4000000 && term->utf_size >= 5))
1328 /* Unicode line separator and paragraph separator are CR-LF */
1329 if (c == 0x2028 || c == 0x2029)
1332 /* High controls are probably a Baaad idea too. */
1336 /* The UTF-16 surrogates are not nice either. */
1337 /* The standard give the option of decoding these:
1338 * I don't want to! */
1339 if (c >= 0xD800 && c < 0xE000)
1342 /* ISO 10646 characters now limited to UTF-16 range. */
1346 /* This is currently a TagPhobic application.. */
1347 if (c >= 0xE0000 && c <= 0xE007F)
1350 /* U+FEFF is best seen as a null. */
1353 /* But U+FFFE is an error. */
1354 if (c == 0xFFFE || c == 0xFFFF)
1357 /* Oops this is a 16bit implementation */
1362 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1363 else if(term->sco_acs &&
1364 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1366 if (term->sco_acs == 2) c |= 0x80;
1369 switch (term->cset_attr[term->cset]) {
1371 * Linedraw characters are different from 'ESC ( B'
1372 * only for a small range. For ones outside that
1373 * range, make sure we use the same font as well as
1374 * the same encoding.
1377 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1378 c = term->ucsdata->unitab_ctrl[c];
1380 c = ((unsigned char) c) | ATTR_LINEDRW;
1384 /* If UK-ASCII, make the '#' a LineDraw Pound */
1386 c = '}' | ATTR_LINEDRW;
1389 /*FALLTHROUGH*/ case ATTR_ASCII:
1390 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1391 c = term->ucsdata->unitab_ctrl[c];
1393 c = ((unsigned char) c) | ATTR_ASCII;
1396 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1402 /* How about C1 controls ? */
1403 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1404 !term->vt52_mode && has_compat(VT220)) {
1405 term->termstate = SEEN_ESC;
1406 term->esc_query = FALSE;
1407 c = '@' + (c & 0x1F);
1410 /* Or the GL control. */
1411 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1412 if (term->curs.x && !term->wrapnext)
1414 term->wrapnext = FALSE;
1416 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1417 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1419 /* Or normal C0 controls. */
1420 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1422 case '\005': /* terminal type query */
1423 /* Strictly speaking this is VT100 but a VT100 defaults to
1424 * no response. Other terminals respond at their option.
1426 * Don't put a CR in the default string as this tends to
1427 * upset some weird software.
1429 * An xterm returns "xterm" (5 characters)
1431 compatibility(ANSIMIN);
1433 char abuf[256], *s, *d;
1435 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1437 if (*s >= 'a' && *s <= 'z')
1438 *d++ = (*s - ('a' - 1));
1439 else if ((*s >= '@' && *s <= '_') ||
1440 *s == '?' || (*s & 0x80))
1445 } else if (*s == '^') {
1450 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1456 struct beeptime *newbeep;
1457 unsigned long ticks;
1459 ticks = GETTICKCOUNT();
1461 if (!term->beep_overloaded) {
1462 newbeep = smalloc(sizeof(struct beeptime));
1463 newbeep->ticks = ticks;
1464 newbeep->next = NULL;
1465 if (!term->beephead)
1466 term->beephead = newbeep;
1468 term->beeptail->next = newbeep;
1469 term->beeptail = newbeep;
1474 * Throw out any beeps that happened more than
1477 while (term->beephead &&
1478 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1479 struct beeptime *tmp = term->beephead;
1480 term->beephead = tmp->next;
1482 if (!term->beephead)
1483 term->beeptail = NULL;
1487 if (term->cfg.bellovl && term->beep_overloaded &&
1488 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1490 * If we're currently overloaded and the
1491 * last beep was more than s seconds ago,
1492 * leave overload mode.
1494 term->beep_overloaded = FALSE;
1495 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1496 term->nbeeps >= term->cfg.bellovl_n) {
1498 * Now, if we have n or more beeps
1499 * remaining in the queue, go into overload
1502 term->beep_overloaded = TRUE;
1504 term->lastbeep = ticks;
1507 * Perform an actual beep if we're not overloaded.
1509 if (!term->cfg.bellovl || !term->beep_overloaded) {
1510 beep(term->frontend, term->cfg.beep);
1511 if (term->cfg.beep == BELL_VISUAL) {
1512 term->in_vbell = TRUE;
1513 term->vbell_startpoint = ticks;
1521 if (term->curs.x == 0 &&
1522 (term->curs.y == 0 || term->wrap == 0))
1524 else if (term->curs.x == 0 && term->curs.y > 0)
1525 term->curs.x = term->cols - 1, term->curs.y--;
1526 else if (term->wrapnext)
1527 term->wrapnext = FALSE;
1531 term->seen_disp_event = TRUE;
1534 compatibility(VT100);
1538 compatibility(VT100);
1542 if (term->vt52_mode)
1543 term->termstate = VT52_ESC;
1545 compatibility(ANSIMIN);
1546 term->termstate = SEEN_ESC;
1547 term->esc_query = FALSE;
1552 term->wrapnext = FALSE;
1554 term->seen_disp_event = TRUE;
1555 term->paste_hold = 0;
1557 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1560 if (has_compat(SCOANSI)) {
1561 move(term, 0, 0, 0);
1562 erase_lots(term, FALSE, FALSE, TRUE);
1564 term->wrapnext = FALSE;
1565 term->seen_disp_event = 1;
1569 compatibility(VT100);
1571 if (term->curs.y == term->marg_b)
1572 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1573 else if (term->curs.y < term->rows - 1)
1575 if (term->cfg.lfhascr)
1578 term->wrapnext = FALSE;
1579 term->seen_disp_event = 1;
1580 term->paste_hold = 0;
1582 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1586 pos old_curs = term->curs;
1587 unsigned long *ldata = lineptr(term->curs.y);
1591 } while (term->curs.x < term->cols - 1 &&
1592 !term->tabs[term->curs.x]);
1594 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1595 if (term->curs.x >= term->cols / 2)
1596 term->curs.x = term->cols / 2 - 1;
1598 if (term->curs.x >= term->cols)
1599 term->curs.x = term->cols - 1;
1603 check_selection(term, old_curs, term->curs);
1605 term->seen_disp_event = TRUE;
1609 switch (term->termstate) {
1611 /* Only graphic characters get this far;
1612 * ctrls are stripped above */
1613 if (term->wrapnext && term->wrap) {
1614 term->cpos[1] |= LATTR_WRAPPED;
1615 if (term->curs.y == term->marg_b)
1616 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1617 else if (term->curs.y < term->rows - 1)
1621 term->wrapnext = FALSE;
1625 if (term->selstate != NO_SELECTION) {
1626 pos cursplus = term->curs;
1628 check_selection(term, term->curs, cursplus);
1630 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1632 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1638 width = wcwidth((wchar_t) c);
1642 * If we're about to display a double-width
1643 * character starting in the rightmost
1644 * column, then we do something special
1645 * instead. We must print a space in the
1646 * last column of the screen, then wrap;
1647 * and we also set LATTR_WRAPPED2 which
1648 * instructs subsequent cut-and-pasting not
1649 * only to splice this line to the one
1650 * after it, but to ignore the space in the
1651 * last character position as well.
1652 * (Because what was actually output to the
1653 * terminal was presumably just a sequence
1654 * of CJK characters, and we don't want a
1655 * space to be pasted in the middle of
1656 * those just because they had the
1657 * misfortune to start in the wrong parity
1658 * column. xterm concurs.)
1660 check_boundary(term, term->curs.x, term->curs.y);
1661 check_boundary(term, term->curs.x+2, term->curs.y);
1662 if (term->curs.x == term->cols-1) {
1663 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1664 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1665 if (term->curs.y == term->marg_b)
1666 scroll(term, term->marg_t, term->marg_b,
1668 else if (term->curs.y < term->rows - 1)
1672 /* Now we must check_boundary again, of course. */
1673 check_boundary(term, term->curs.x, term->curs.y);
1674 check_boundary(term, term->curs.x+2, term->curs.y);
1676 *term->cpos++ = c | term->curr_attr;
1677 *term->cpos++ = UCSWIDE | term->curr_attr;
1681 check_boundary(term, term->curs.x, term->curs.y);
1682 check_boundary(term, term->curs.x+1, term->curs.y);
1683 *term->cpos++ = c | term->curr_attr;
1690 if (term->curs.x == term->cols) {
1693 term->wrapnext = TRUE;
1694 if (term->wrap && term->vt52_mode) {
1695 term->cpos[1] |= LATTR_WRAPPED;
1696 if (term->curs.y == term->marg_b)
1697 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1698 else if (term->curs.y < term->rows - 1)
1702 term->wrapnext = FALSE;
1705 term->seen_disp_event = 1;
1710 * This state is virtually identical to SEEN_ESC, with the
1711 * exception that we have an OSC sequence in the pipeline,
1712 * and _if_ we see a backslash, we process it.
1716 term->termstate = TOPLEVEL;
1719 /* else fall through */
1721 if (c >= ' ' && c <= '/') {
1722 if (term->esc_query)
1723 term->esc_query = -1;
1725 term->esc_query = c;
1728 term->termstate = TOPLEVEL;
1729 switch (ANSI(c, term->esc_query)) {
1730 case '[': /* enter CSI mode */
1731 term->termstate = SEEN_CSI;
1732 term->esc_nargs = 1;
1733 term->esc_args[0] = ARG_DEFAULT;
1734 term->esc_query = FALSE;
1736 case ']': /* xterm escape sequences */
1737 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1738 compatibility(OTHER);
1739 term->termstate = SEEN_OSC;
1740 term->esc_args[0] = 0;
1742 case '7': /* save cursor */
1743 compatibility(VT100);
1744 save_cursor(term, TRUE);
1746 case '8': /* restore cursor */
1747 compatibility(VT100);
1748 save_cursor(term, FALSE);
1749 term->seen_disp_event = TRUE;
1752 compatibility(VT100);
1753 term->app_keypad_keys = TRUE;
1756 compatibility(VT100);
1757 term->app_keypad_keys = FALSE;
1759 case 'D': /* exactly equivalent to LF */
1760 compatibility(VT100);
1761 if (term->curs.y == term->marg_b)
1762 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1763 else if (term->curs.y < term->rows - 1)
1766 term->wrapnext = FALSE;
1767 term->seen_disp_event = TRUE;
1769 case 'E': /* exactly equivalent to CR-LF */
1770 compatibility(VT100);
1772 if (term->curs.y == term->marg_b)
1773 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1774 else if (term->curs.y < term->rows - 1)
1777 term->wrapnext = FALSE;
1778 term->seen_disp_event = TRUE;
1780 case 'M': /* reverse index - backwards LF */
1781 compatibility(VT100);
1782 if (term->curs.y == term->marg_t)
1783 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1784 else if (term->curs.y > 0)
1787 term->wrapnext = FALSE;
1788 term->seen_disp_event = TRUE;
1790 case 'Z': /* terminal type query */
1791 compatibility(VT100);
1793 ldisc_send(term->ldisc, term->id_string,
1794 strlen(term->id_string), 0);
1796 case 'c': /* restore power-on settings */
1797 compatibility(VT100);
1799 if (term->ldisc) /* cause ldisc to notice changes */
1800 ldisc_send(term->ldisc, NULL, 0, 0);
1801 if (term->reset_132) {
1802 if (!term->cfg.no_remote_resize)
1803 request_resize(term->frontend, 80, term->rows);
1804 term->reset_132 = 0;
1808 term->seen_disp_event = TRUE;
1810 case 'H': /* set a tab */
1811 compatibility(VT100);
1812 term->tabs[term->curs.x] = TRUE;
1815 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1816 compatibility(VT100);
1818 unsigned long *ldata;
1822 for (i = 0; i < term->rows; i++) {
1824 for (j = 0; j < term->cols; j++)
1825 ldata[j] = ATTR_DEFAULT | 'E';
1826 ldata[term->cols] = 0;
1829 term->seen_disp_event = TRUE;
1830 scrtop.x = scrtop.y = 0;
1832 scrbot.y = term->rows;
1833 check_selection(term, scrtop, scrbot);
1837 case ANSI('3', '#'):
1838 case ANSI('4', '#'):
1839 case ANSI('5', '#'):
1840 case ANSI('6', '#'):
1841 compatibility(VT100);
1843 unsigned long nlattr;
1844 unsigned long *ldata;
1845 switch (ANSI(c, term->esc_query)) {
1846 case ANSI('3', '#'):
1849 case ANSI('4', '#'):
1852 case ANSI('5', '#'):
1853 nlattr = LATTR_NORM;
1855 default: /* spiritually case ANSI('6', '#'): */
1856 nlattr = LATTR_WIDE;
1859 ldata = lineptr(term->curs.y);
1860 ldata[term->cols] &= ~LATTR_MODE;
1861 ldata[term->cols] |= nlattr;
1865 case ANSI('A', '('):
1866 compatibility(VT100);
1867 if (!term->cfg.no_remote_charset)
1868 term->cset_attr[0] = ATTR_GBCHR;
1870 case ANSI('B', '('):
1871 compatibility(VT100);
1872 if (!term->cfg.no_remote_charset)
1873 term->cset_attr[0] = ATTR_ASCII;
1875 case ANSI('0', '('):
1876 compatibility(VT100);
1877 if (!term->cfg.no_remote_charset)
1878 term->cset_attr[0] = ATTR_LINEDRW;
1880 case ANSI('U', '('):
1881 compatibility(OTHER);
1882 if (!term->cfg.no_remote_charset)
1883 term->cset_attr[0] = ATTR_SCOACS;
1886 case ANSI('A', ')'):
1887 compatibility(VT100);
1888 if (!term->cfg.no_remote_charset)
1889 term->cset_attr[1] = ATTR_GBCHR;
1891 case ANSI('B', ')'):
1892 compatibility(VT100);
1893 if (!term->cfg.no_remote_charset)
1894 term->cset_attr[1] = ATTR_ASCII;
1896 case ANSI('0', ')'):
1897 compatibility(VT100);
1898 if (!term->cfg.no_remote_charset)
1899 term->cset_attr[1] = ATTR_LINEDRW;
1901 case ANSI('U', ')'):
1902 compatibility(OTHER);
1903 if (!term->cfg.no_remote_charset)
1904 term->cset_attr[1] = ATTR_SCOACS;
1907 case ANSI('8', '%'): /* Old Linux code */
1908 case ANSI('G', '%'):
1909 compatibility(OTHER);
1910 if (!term->cfg.no_remote_charset)
1913 case ANSI('@', '%'):
1914 compatibility(OTHER);
1915 if (!term->cfg.no_remote_charset)
1921 term->termstate = TOPLEVEL; /* default */
1923 if (term->esc_nargs <= ARGS_MAX) {
1924 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1925 term->esc_args[term->esc_nargs - 1] = 0;
1926 term->esc_args[term->esc_nargs - 1] =
1927 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1929 term->termstate = SEEN_CSI;
1930 } else if (c == ';') {
1931 if (++term->esc_nargs <= ARGS_MAX)
1932 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1933 term->termstate = SEEN_CSI;
1934 } else if (c < '@') {
1935 if (term->esc_query)
1936 term->esc_query = -1;
1938 term->esc_query = TRUE;
1940 term->esc_query = c;
1941 term->termstate = SEEN_CSI;
1943 switch (ANSI(c, term->esc_query)) {
1944 case 'A': /* move up N lines */
1945 move(term, term->curs.x,
1946 term->curs.y - def(term->esc_args[0], 1), 1);
1947 term->seen_disp_event = TRUE;
1949 case 'e': /* move down N lines */
1950 compatibility(ANSI);
1953 move(term, term->curs.x,
1954 term->curs.y + def(term->esc_args[0], 1), 1);
1955 term->seen_disp_event = TRUE;
1957 case ANSI('c', '>'): /* report xterm version */
1958 compatibility(OTHER);
1959 /* this reports xterm version 136 so that VIM can
1960 use the drag messages from the mouse reporting */
1962 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1964 case 'a': /* move right N cols */
1965 compatibility(ANSI);
1968 move(term, term->curs.x + def(term->esc_args[0], 1),
1970 term->seen_disp_event = TRUE;
1972 case 'D': /* move left N cols */
1973 move(term, term->curs.x - def(term->esc_args[0], 1),
1975 term->seen_disp_event = TRUE;
1977 case 'E': /* move down N lines and CR */
1978 compatibility(ANSI);
1980 term->curs.y + def(term->esc_args[0], 1), 1);
1981 term->seen_disp_event = TRUE;
1983 case 'F': /* move up N lines and CR */
1984 compatibility(ANSI);
1986 term->curs.y - def(term->esc_args[0], 1), 1);
1987 term->seen_disp_event = TRUE;
1990 case '`': /* set horizontal posn */
1991 compatibility(ANSI);
1992 move(term, def(term->esc_args[0], 1) - 1,
1994 term->seen_disp_event = TRUE;
1996 case 'd': /* set vertical posn */
1997 compatibility(ANSI);
1998 move(term, term->curs.x,
1999 ((term->dec_om ? term->marg_t : 0) +
2000 def(term->esc_args[0], 1) - 1),
2001 (term->dec_om ? 2 : 0));
2002 term->seen_disp_event = TRUE;
2005 case 'f': /* set horz and vert posns at once */
2006 if (term->esc_nargs < 2)
2007 term->esc_args[1] = ARG_DEFAULT;
2008 move(term, def(term->esc_args[1], 1) - 1,
2009 ((term->dec_om ? term->marg_t : 0) +
2010 def(term->esc_args[0], 1) - 1),
2011 (term->dec_om ? 2 : 0));
2012 term->seen_disp_event = TRUE;
2014 case 'J': /* erase screen or parts of it */
2016 unsigned int i = def(term->esc_args[0], 0) + 1;
2019 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2022 term->seen_disp_event = TRUE;
2024 case 'K': /* erase line or parts of it */
2026 unsigned int i = def(term->esc_args[0], 0) + 1;
2029 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2031 term->seen_disp_event = TRUE;
2033 case 'L': /* insert lines */
2034 compatibility(VT102);
2035 if (term->curs.y <= term->marg_b)
2036 scroll(term, term->curs.y, term->marg_b,
2037 -def(term->esc_args[0], 1), FALSE);
2039 term->seen_disp_event = TRUE;
2041 case 'M': /* delete lines */
2042 compatibility(VT102);
2043 if (term->curs.y <= term->marg_b)
2044 scroll(term, term->curs.y, term->marg_b,
2045 def(term->esc_args[0], 1),
2048 term->seen_disp_event = TRUE;
2050 case '@': /* insert chars */
2051 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2052 compatibility(VT102);
2053 insch(term, def(term->esc_args[0], 1));
2054 term->seen_disp_event = TRUE;
2056 case 'P': /* delete chars */
2057 compatibility(VT102);
2058 insch(term, -def(term->esc_args[0], 1));
2059 term->seen_disp_event = TRUE;
2061 case 'c': /* terminal type query */
2062 compatibility(VT100);
2063 /* This is the response for a VT102 */
2065 ldisc_send(term->ldisc, term->id_string,
2066 strlen(term->id_string), 0);
2068 case 'n': /* cursor position query */
2070 if (term->esc_args[0] == 6) {
2072 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2074 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2075 } else if (term->esc_args[0] == 5) {
2076 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2080 case 'h': /* toggle modes to high */
2082 compatibility(VT100);
2085 for (i = 0; i < term->esc_nargs; i++)
2086 toggle_mode(term, term->esc_args[i],
2087 term->esc_query, TRUE);
2092 compatibility(VT100);
2094 if (term->esc_nargs != 1) break;
2095 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2096 term->printing = TRUE;
2097 term->only_printing = !term->esc_query;
2098 term->print_state = 0;
2099 term_print_setup(term);
2100 } else if (term->esc_args[0] == 4 &&
2102 term_print_finish(term);
2106 case 'l': /* toggle modes to low */
2108 compatibility(VT100);
2111 for (i = 0; i < term->esc_nargs; i++)
2112 toggle_mode(term, term->esc_args[i],
2113 term->esc_query, FALSE);
2116 case 'g': /* clear tabs */
2117 compatibility(VT100);
2118 if (term->esc_nargs == 1) {
2119 if (term->esc_args[0] == 0) {
2120 term->tabs[term->curs.x] = FALSE;
2121 } else if (term->esc_args[0] == 3) {
2123 for (i = 0; i < term->cols; i++)
2124 term->tabs[i] = FALSE;
2128 case 'r': /* set scroll margins */
2129 compatibility(VT100);
2130 if (term->esc_nargs <= 2) {
2132 top = def(term->esc_args[0], 1) - 1;
2133 bot = (term->esc_nargs <= 1
2134 || term->esc_args[1] == 0 ?
2136 def(term->esc_args[1], term->rows)) - 1;
2137 if (bot >= term->rows)
2138 bot = term->rows - 1;
2139 /* VTTEST Bug 9 - if region is less than 2 lines
2140 * don't change region.
2142 if (bot - top > 0) {
2147 * I used to think the cursor should be
2148 * placed at the top of the newly marginned
2149 * area. Apparently not: VMS TPU falls over
2152 * Well actually it should for
2155 term->curs.y = (term->dec_om ?
2158 term->seen_disp_event = TRUE;
2162 case 'm': /* set graphics rendition */
2165 * A VT100 without the AVO only had one
2166 * attribute, either underline or
2167 * reverse video depending on the
2168 * cursor type, this was selected by
2172 * This is sometimes DIM, eg on the
2175 * This is sometimes INVIS various ANSI.
2177 * This like 22 disables BOLD, DIM and INVIS
2179 * The ANSI colours appear on any
2180 * terminal that has colour (obviously)
2181 * but the interaction between sgr0 and
2182 * the colours varies but is usually
2183 * related to the background colour
2184 * erase item. The interaction between
2185 * colour attributes and the mono ones
2186 * is also very implementation
2189 * The 39 and 49 attributes are likely
2190 * to be unimplemented.
2193 for (i = 0; i < term->esc_nargs; i++) {
2194 switch (def(term->esc_args[i], 0)) {
2195 case 0: /* restore defaults */
2196 term->curr_attr = ATTR_DEFAULT;
2198 case 1: /* enable bold */
2199 compatibility(VT100AVO);
2200 term->curr_attr |= ATTR_BOLD;
2202 case 21: /* (enable double underline) */
2203 compatibility(OTHER);
2204 case 4: /* enable underline */
2205 compatibility(VT100AVO);
2206 term->curr_attr |= ATTR_UNDER;
2208 case 5: /* enable blink */
2209 compatibility(VT100AVO);
2210 term->curr_attr |= ATTR_BLINK;
2212 case 7: /* enable reverse video */
2213 term->curr_attr |= ATTR_REVERSE;
2215 case 10: /* SCO acs off */
2216 compatibility(SCOANSI);
2217 if (term->cfg.no_remote_charset) break;
2218 term->sco_acs = 0; break;
2219 case 11: /* SCO acs on */
2220 compatibility(SCOANSI);
2221 if (term->cfg.no_remote_charset) break;
2222 term->sco_acs = 1; break;
2223 case 12: /* SCO acs on, |0x80 */
2224 compatibility(SCOANSI);
2225 if (term->cfg.no_remote_charset) break;
2226 term->sco_acs = 2; break;
2227 case 22: /* disable bold */
2228 compatibility2(OTHER, VT220);
2229 term->curr_attr &= ~ATTR_BOLD;
2231 case 24: /* disable underline */
2232 compatibility2(OTHER, VT220);
2233 term->curr_attr &= ~ATTR_UNDER;
2235 case 25: /* disable blink */
2236 compatibility2(OTHER, VT220);
2237 term->curr_attr &= ~ATTR_BLINK;
2239 case 27: /* disable reverse video */
2240 compatibility2(OTHER, VT220);
2241 term->curr_attr &= ~ATTR_REVERSE;
2252 term->curr_attr &= ~ATTR_FGMASK;
2254 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2256 case 39: /* default-foreground */
2257 term->curr_attr &= ~ATTR_FGMASK;
2258 term->curr_attr |= ATTR_DEFFG;
2269 term->curr_attr &= ~ATTR_BGMASK;
2271 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2273 case 49: /* default-background */
2274 term->curr_attr &= ~ATTR_BGMASK;
2275 term->curr_attr |= ATTR_DEFBG;
2280 term->erase_char = (' ' | ATTR_ASCII |
2286 case 's': /* save cursor */
2287 save_cursor(term, TRUE);
2289 case 'u': /* restore cursor */
2290 save_cursor(term, FALSE);
2291 term->seen_disp_event = TRUE;
2293 case 't': /* set page size - ie window height */
2295 * VT340/VT420 sequence DECSLPP, DEC only allows values
2296 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2297 * illegal values (eg first arg 1..9) for window changing
2300 if (term->esc_nargs <= 1
2301 && (term->esc_args[0] < 1 ||
2302 term->esc_args[0] >= 24)) {
2303 compatibility(VT340TEXT);
2304 if (!term->cfg.no_remote_resize)
2305 request_resize(term->frontend, term->cols,
2306 def(term->esc_args[0], 24));
2308 } else if (term->esc_nargs >= 1 &&
2309 term->esc_args[0] >= 1 &&
2310 term->esc_args[0] < 24) {
2311 compatibility(OTHER);
2313 switch (term->esc_args[0]) {
2317 set_iconic(term->frontend, FALSE);
2320 set_iconic(term->frontend, TRUE);
2323 if (term->esc_nargs >= 3) {
2324 if (!term->cfg.no_remote_resize)
2325 move_window(term->frontend,
2326 def(term->esc_args[1], 0),
2327 def(term->esc_args[2], 0));
2331 /* We should resize the window to a given
2332 * size in pixels here, but currently our
2333 * resizing code isn't healthy enough to
2338 set_zorder(term->frontend, TRUE);
2341 /* move to bottom */
2342 set_zorder(term->frontend, FALSE);
2345 refresh_window(term->frontend);
2348 if (term->esc_nargs >= 3) {
2349 if (!term->cfg.no_remote_resize)
2350 request_resize(term->frontend,
2351 def(term->esc_args[2], term->cfg.width),
2352 def(term->esc_args[1], term->cfg.height));
2356 if (term->esc_nargs >= 2)
2357 set_zoomed(term->frontend,
2363 ldisc_send(term->ldisc,
2364 is_iconic(term->frontend) ?
2365 "\033[1t" : "\033[2t", 4, 0);
2369 get_window_pos(term->frontend, &x, &y);
2370 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2371 ldisc_send(term->ldisc, buf, len, 0);
2376 get_window_pixels(term->frontend, &x, &y);
2377 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2378 ldisc_send(term->ldisc, buf, len, 0);
2383 len = sprintf(buf, "\033[8;%d;%dt",
2384 term->rows, term->cols);
2385 ldisc_send(term->ldisc, buf, len, 0);
2390 * Hmmm. Strictly speaking we
2391 * should return `the size of the
2392 * screen in characters', but
2393 * that's not easy: (a) window
2394 * furniture being what it is it's
2395 * hard to compute, and (b) in
2396 * resize-font mode maximising the
2397 * window wouldn't change the
2398 * number of characters. *shrug*. I
2399 * think we'll ignore it for the
2400 * moment and see if anyone
2401 * complains, and then ask them
2402 * what they would like it to do.
2407 p = get_window_title(term->frontend, TRUE);
2409 ldisc_send(term->ldisc, "\033]L", 3, 0);
2410 ldisc_send(term->ldisc, p, len, 0);
2411 ldisc_send(term->ldisc, "\033\\", 2, 0);
2416 p = get_window_title(term->frontend,FALSE);
2418 ldisc_send(term->ldisc, "\033]l", 3, 0);
2419 ldisc_send(term->ldisc, p, len, 0);
2420 ldisc_send(term->ldisc, "\033\\", 2, 0);
2427 compatibility(SCOANSI);
2428 scroll(term, term->marg_t, term->marg_b,
2429 def(term->esc_args[0], 1), TRUE);
2431 term->wrapnext = FALSE;
2432 term->seen_disp_event = TRUE;
2435 compatibility(SCOANSI);
2436 scroll(term, term->marg_t, term->marg_b,
2437 -def(term->esc_args[0], 1), TRUE);
2439 term->wrapnext = FALSE;
2440 term->seen_disp_event = TRUE;
2442 case ANSI('|', '*'):
2443 /* VT420 sequence DECSNLS
2444 * Set number of lines on screen
2445 * VT420 uses VGA like hardware and can support any size in
2446 * reasonable range (24..49 AIUI) with no default specified.
2448 compatibility(VT420);
2449 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2450 if (!term->cfg.no_remote_resize)
2451 request_resize(term->frontend, term->cols,
2452 def(term->esc_args[0],
2457 case ANSI('|', '$'):
2458 /* VT340/VT420 sequence DECSCPP
2459 * Set number of columns per page
2460 * Docs imply range is only 80 or 132, but I'll allow any.
2462 compatibility(VT340TEXT);
2463 if (term->esc_nargs <= 1) {
2464 if (!term->cfg.no_remote_resize)
2465 request_resize(term->frontend,
2466 def(term->esc_args[0],
2467 term->cfg.width), term->rows);
2471 case 'X': /* write N spaces w/o moving cursor */
2472 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2473 compatibility(ANSIMIN);
2475 int n = def(term->esc_args[0], 1);
2477 unsigned long *p = term->cpos;
2478 if (n > term->cols - term->curs.x)
2479 n = term->cols - term->curs.x;
2480 cursplus = term->curs;
2482 check_boundary(term, term->curs.x, term->curs.y);
2483 check_boundary(term, term->curs.x+n, term->curs.y);
2484 check_selection(term, term->curs, cursplus);
2486 *p++ = term->erase_char;
2487 term->seen_disp_event = TRUE;
2490 case 'x': /* report terminal characteristics */
2491 compatibility(VT100);
2494 int i = def(term->esc_args[0], 0);
2495 if (i == 0 || i == 1) {
2496 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2498 ldisc_send(term->ldisc, buf, 20, 0);
2502 case 'Z': /* BackTab for xterm */
2503 compatibility(OTHER);
2505 int i = def(term->esc_args[0], 1);
2506 pos old_curs = term->curs;
2508 for(;i>0 && term->curs.x>0; i--) {
2511 } while (term->curs.x >0 &&
2512 !term->tabs[term->curs.x]);
2515 check_selection(term, old_curs, term->curs);
2518 case ANSI('L', '='):
2519 compatibility(OTHER);
2520 term->use_bce = (term->esc_args[0] <= 0);
2521 term->erase_char = ERASE_CHAR;
2523 term->erase_char = (' ' | ATTR_ASCII |
2525 (ATTR_FGMASK | ATTR_BGMASK)));
2527 case ANSI('E', '='):
2528 compatibility(OTHER);
2529 term->blink_is_real = (term->esc_args[0] >= 1);
2531 case ANSI('p', '"'):
2533 * Allow the host to make this emulator a
2534 * 'perfect' VT102. This first appeared in
2535 * the VT220, but we do need to get back to
2536 * PuTTY mode so I won't check it.
2538 * The arg in 40..42,50 are a PuTTY extension.
2539 * The 2nd arg, 8bit vs 7bit is not checked.
2541 * Setting VT102 mode should also change
2542 * the Fkeys to generate PF* codes as a
2543 * real VT102 has no Fkeys. The VT220 does
2544 * this, F11..F13 become ESC,BS,LF other
2545 * Fkeys send nothing.
2547 * Note ESC c will NOT change this!
2550 switch (term->esc_args[0]) {
2552 term->compatibility_level &= ~TM_VTXXX;
2553 term->compatibility_level |= TM_VT102;
2556 term->compatibility_level &= ~TM_VTXXX;
2557 term->compatibility_level |= TM_VT220;
2561 if (term->esc_args[0] > 60 &&
2562 term->esc_args[0] < 70)
2563 term->compatibility_level |= TM_VTXXX;
2567 term->compatibility_level &= TM_VTXXX;
2570 term->compatibility_level = TM_PUTTY;
2573 term->compatibility_level = TM_SCOANSI;
2577 term->compatibility_level = TM_PUTTY;
2583 /* Change the response to CSI c */
2584 if (term->esc_args[0] == 50) {
2587 strcpy(term->id_string, "\033[?");
2588 for (i = 1; i < term->esc_nargs; i++) {
2590 strcat(term->id_string, ";");
2591 sprintf(lbuf, "%d", term->esc_args[i]);
2592 strcat(term->id_string, lbuf);
2594 strcat(term->id_string, "c");
2597 /* Is this a good idea ?
2598 * Well we should do a soft reset at this point ...
2600 if (!has_compat(VT420) && has_compat(VT100)) {
2601 if (!term->cfg.no_remote_resize) {
2602 if (term->reset_132)
2603 request_resize(132, 24);
2605 request_resize(80, 24);
2613 term->osc_w = FALSE;
2615 case 'P': /* Linux palette sequence */
2616 term->termstate = SEEN_OSC_P;
2617 term->osc_strlen = 0;
2619 case 'R': /* Linux palette reset */
2620 palette_reset(term->frontend);
2621 term_invalidate(term);
2622 term->termstate = TOPLEVEL;
2624 case 'W': /* word-set */
2625 term->termstate = SEEN_OSC_W;
2638 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2642 * Grotty hack to support xterm and DECterm title
2643 * sequences concurrently.
2645 if (term->esc_args[0] == 2) {
2646 term->esc_args[0] = 1;
2649 /* else fall through */
2651 term->termstate = OSC_STRING;
2652 term->osc_strlen = 0;
2657 * This OSC stuff is EVIL. It takes just one character to get into
2658 * sysline mode and it's not initially obvious how to get out.
2659 * So I've added CR and LF as string aborts.
2660 * This shouldn't effect compatibility as I believe embedded
2661 * control characters are supposed to be interpreted (maybe?)
2662 * and they don't display anything useful anyway.
2666 if (c == '\012' || c == '\015') {
2667 term->termstate = TOPLEVEL;
2668 } else if (c == 0234 || c == '\007') {
2670 * These characters terminate the string; ST and BEL
2671 * terminate the sequence and trigger instant
2672 * processing of it, whereas ESC goes back to SEEN_ESC
2673 * mode unless it is followed by \, in which case it is
2674 * synonymous with ST in the first place.
2677 term->termstate = TOPLEVEL;
2678 } else if (c == '\033')
2679 term->termstate = OSC_MAYBE_ST;
2680 else if (term->osc_strlen < OSC_STR_MAX)
2681 term->osc_string[term->osc_strlen++] = c;
2685 int max = (term->osc_strlen == 0 ? 21 : 16);
2687 if (c >= '0' && c <= '9')
2689 else if (c >= 'A' && c <= 'A' + max - 10)
2691 else if (c >= 'a' && c <= 'a' + max - 10)
2694 term->termstate = TOPLEVEL;
2697 term->osc_string[term->osc_strlen++] = val;
2698 if (term->osc_strlen >= 7) {
2699 palette_set(term->frontend, term->osc_string[0],
2700 term->osc_string[1] * 16 + term->osc_string[2],
2701 term->osc_string[3] * 16 + term->osc_string[4],
2702 term->osc_string[5] * 16 + term->osc_string[6]);
2703 term_invalidate(term);
2704 term->termstate = TOPLEVEL;
2720 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2723 term->termstate = OSC_STRING;
2724 term->osc_strlen = 0;
2728 term->termstate = TOPLEVEL;
2729 term->seen_disp_event = TRUE;
2732 move(term, term->curs.x, term->curs.y - 1, 1);
2735 move(term, term->curs.x, term->curs.y + 1, 1);
2738 move(term, term->curs.x + 1, term->curs.y, 1);
2741 move(term, term->curs.x - 1, term->curs.y, 1);
2744 * From the VT100 Manual
2745 * NOTE: The special graphics characters in the VT100
2746 * are different from those in the VT52
2748 * From VT102 manual:
2749 * 137 _ Blank - Same
2750 * 140 ` Reserved - Humm.
2751 * 141 a Solid rectangle - Similar
2752 * 142 b 1/ - Top half of fraction for the
2753 * 143 c 3/ - subscript numbers below.
2756 * 146 f Degrees - Same
2757 * 147 g Plus or minus - Same
2759 * 151 i Ellipsis (dots)
2762 * 154 l Bar at scan 0
2763 * 155 m Bar at scan 1
2764 * 156 n Bar at scan 2
2765 * 157 o Bar at scan 3 - Similar
2766 * 160 p Bar at scan 4 - Similar
2767 * 161 q Bar at scan 5 - Similar
2768 * 162 r Bar at scan 6 - Same
2769 * 163 s Bar at scan 7 - Similar
2784 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2787 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2790 move(term, 0, 0, 0);
2793 if (term->curs.y == 0)
2794 scroll(term, 0, term->rows - 1, -1, TRUE);
2795 else if (term->curs.y > 0)
2798 term->wrapnext = FALSE;
2801 erase_lots(term, FALSE, FALSE, TRUE);
2805 erase_lots(term, TRUE, FALSE, TRUE);
2809 /* XXX Print cursor line */
2812 /* XXX Start controller mode */
2815 /* XXX Stop controller mode */
2819 term->termstate = VT52_Y1;
2823 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2826 term->app_keypad_keys = TRUE;
2829 term->app_keypad_keys = FALSE;
2832 /* XXX This should switch to VT100 mode not current or default
2833 * VT mode. But this will only have effect in a VT220+
2836 term->vt52_mode = FALSE;
2837 term->blink_is_real = term->cfg.blinktext;
2841 /* XXX Enter auto print mode */
2844 /* XXX Exit auto print mode */
2847 /* XXX Print screen */
2853 /* compatibility(ATARI) */
2854 move(term, 0, 0, 0);
2855 erase_lots(term, FALSE, FALSE, TRUE);
2859 /* compatibility(ATARI) */
2860 if (term->curs.y <= term->marg_b)
2861 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2864 /* compatibility(ATARI) */
2865 if (term->curs.y <= term->marg_b)
2866 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2869 /* compatibility(ATARI) */
2870 term->termstate = VT52_FG;
2873 /* compatibility(ATARI) */
2874 term->termstate = VT52_BG;
2877 /* compatibility(ATARI) */
2878 erase_lots(term, FALSE, TRUE, FALSE);
2882 /* compatibility(ATARI) */
2883 term->cursor_on = TRUE;
2886 /* compatibility(ATARI) */
2887 term->cursor_on = FALSE;
2889 /* case 'j': Save cursor position - broken on ST */
2890 /* case 'k': Restore cursor position */
2892 /* compatibility(ATARI) */
2893 erase_lots(term, TRUE, TRUE, TRUE);
2895 term->wrapnext = FALSE;
2899 /* compatibility(ATARI) */
2900 erase_lots(term, TRUE, TRUE, FALSE);
2903 /* compatibility(ATARI) */
2904 term->curr_attr |= ATTR_REVERSE;
2907 /* compatibility(ATARI) */
2908 term->curr_attr &= ~ATTR_REVERSE;
2910 case 'v': /* wrap Autowrap on - Wyse style */
2911 /* compatibility(ATARI) */
2914 case 'w': /* Autowrap off */
2915 /* compatibility(ATARI) */
2920 /* compatibility(OTHER) */
2921 term->vt52_bold = FALSE;
2922 term->curr_attr = ATTR_DEFAULT;
2924 term->erase_char = (' ' | ATTR_ASCII |
2926 (ATTR_FGMASK | ATTR_BGMASK)));
2929 /* compatibility(VI50) */
2930 term->curr_attr |= ATTR_UNDER;
2933 /* compatibility(VI50) */
2934 term->curr_attr &= ~ATTR_UNDER;
2937 /* compatibility(VI50) */
2938 term->vt52_bold = TRUE;
2939 term->curr_attr |= ATTR_BOLD;
2942 /* compatibility(VI50) */
2943 term->vt52_bold = FALSE;
2944 term->curr_attr &= ~ATTR_BOLD;
2950 term->termstate = VT52_Y2;
2951 move(term, term->curs.x, c - ' ', 0);
2954 term->termstate = TOPLEVEL;
2955 move(term, c - ' ', term->curs.y, 0);
2960 term->termstate = TOPLEVEL;
2961 term->curr_attr &= ~ATTR_FGMASK;
2962 term->curr_attr &= ~ATTR_BOLD;
2963 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2964 if ((c & 0x8) || term->vt52_bold)
2965 term->curr_attr |= ATTR_BOLD;
2968 term->erase_char = (' ' | ATTR_ASCII |
2970 (ATTR_FGMASK | ATTR_BGMASK)));
2973 term->termstate = TOPLEVEL;
2974 term->curr_attr &= ~ATTR_BGMASK;
2975 term->curr_attr &= ~ATTR_BLINK;
2976 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2978 /* Note: bold background */
2980 term->curr_attr |= ATTR_BLINK;
2983 term->erase_char = (' ' | ATTR_ASCII |
2985 (ATTR_FGMASK | ATTR_BGMASK)));
2988 default: break; /* placate gcc warning about enum use */
2990 if (term->selstate != NO_SELECTION) {
2991 pos cursplus = term->curs;
2993 check_selection(term, term->curs, cursplus);
2997 term_print_flush(term);
3002 * Compare two lines to determine whether they are sufficiently
3003 * alike to scroll-optimise one to the other. Return the degree of
3006 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3010 for (i = n = 0; i < term->cols; i++)
3011 n += (*a++ == *b++);
3017 * Given a context, update the window. Out of paranoia, we don't
3018 * allow WM_PAINT responses to do scrolling optimisations.
3020 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3022 int i, j, our_curs_y, our_curs_x;
3023 unsigned long rv, cursor;
3026 long cursor_background = ERASE_CHAR;
3027 unsigned long ticks;
3030 * Check the visual bell state.
3032 if (term->in_vbell) {
3033 ticks = GETTICKCOUNT();
3034 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3035 term->in_vbell = FALSE;
3038 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3041 * screen array, disptop, scrtop,
3043 * cfg.blinkpc, blink_is_real, tblinker,
3044 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3047 /* Has the cursor position or type changed ? */
3048 if (term->cursor_on) {
3049 if (term->has_focus) {
3050 if (term->blinker || !term->cfg.blink_cur)
3051 cursor = TATTR_ACTCURS;
3055 cursor = TATTR_PASCURS;
3057 cursor |= TATTR_RIGHTCURS;
3060 our_curs_y = term->curs.y - term->disptop;
3063 * Adjust the cursor position in the case where it's
3064 * resting on the right-hand half of a CJK wide character.
3065 * xterm's behaviour here, which seems adequate to me, is
3066 * to display the cursor covering the _whole_ character,
3067 * exactly as if it were one space to the left.
3069 unsigned long *ldata = lineptr(term->curs.y);
3070 our_curs_x = term->curs.x;
3071 if (our_curs_x > 0 &&
3072 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3076 if (term->dispcurs && (term->curstype != cursor ||
3078 term->disptext + our_curs_y * (term->cols + 1) +
3080 if (term->dispcurs > term->disptext &&
3081 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3082 term->dispcurs[-1] |= ATTR_INVALID;
3083 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3084 term->dispcurs[1] |= ATTR_INVALID;
3085 *term->dispcurs |= ATTR_INVALID;
3088 term->dispcurs = NULL;
3090 /* The normal screen data */
3091 for (i = 0; i < term->rows; i++) {
3092 unsigned long *ldata;
3094 int idx, dirty_line, dirty_run, selected;
3095 unsigned long attr = 0;
3096 int updated_line = 0;
3099 int last_run_dirty = 0;
3101 scrpos.y = i + term->disptop;
3102 ldata = lineptr(scrpos.y);
3103 lattr = (ldata[term->cols] & LATTR_MODE);
3105 idx = i * (term->cols + 1);
3106 dirty_run = dirty_line = (ldata[term->cols] !=
3107 term->disptext[idx + term->cols]);
3108 term->disptext[idx + term->cols] = ldata[term->cols];
3110 for (j = 0; j < term->cols; j++, idx++) {
3111 unsigned long tattr, tchar;
3112 unsigned long *d = ldata + j;
3116 tchar = (*d & (CHAR_MASK | CSET_MASK));
3117 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3118 switch (tchar & CSET_MASK) {
3120 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3123 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3126 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3129 tattr |= (tchar & CSET_MASK);
3131 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3134 /* Video reversing things */
3135 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3136 if (term->seltype == LEXICOGRAPHIC)
3137 selected = (posle(term->selstart, scrpos) &&
3138 poslt(scrpos, term->selend));
3140 selected = (posPle(term->selstart, scrpos) &&
3141 posPlt(scrpos, term->selend));
3145 ^ (selected ? ATTR_REVERSE : 0));
3147 /* 'Real' blinking ? */
3148 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3149 if (term->has_focus && term->tblinker) {
3151 tattr &= ~CSET_MASK;
3154 tattr &= ~ATTR_BLINK;
3158 * Check the font we'll _probably_ be using to see if
3159 * the character is wide when we don't want it to be.
3161 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3162 if ((tattr & ATTR_WIDE) == 0 &&
3163 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3164 tattr |= ATTR_NARROW;
3165 } else if (term->disptext[idx]&ATTR_NARROW)
3166 tattr |= ATTR_NARROW;
3168 /* Cursor here ? Save the 'background' */
3169 if (i == our_curs_y && j == our_curs_x) {
3170 cursor_background = tattr | tchar;
3171 term->dispcurs = term->disptext + idx;
3174 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3177 break_run = (((tattr ^ attr) & term->attr_mask) ||
3178 j - start >= sizeof(ch));
3180 /* Special hack for VT100 Linedraw glyphs */
3181 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3182 && tchar <= 0xBD) break_run = TRUE;
3184 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3185 if ((tchar | tattr) == term->disptext[idx])
3187 else if (!dirty_run && ccount == 1)
3192 if ((dirty_run || last_run_dirty) && ccount > 0) {
3193 do_text(ctx, start, i, ch, ccount, attr, lattr);
3199 if (term->ucsdata->dbcs_screenfont)
3200 last_run_dirty = dirty_run;
3201 dirty_run = dirty_line;
3204 if ((tchar | tattr) != term->disptext[idx])
3206 ch[ccount++] = (char) tchar;
3207 term->disptext[idx] = tchar | tattr;
3209 /* If it's a wide char step along to the next one. */
3210 if (tattr & ATTR_WIDE) {
3211 if (++j < term->cols) {
3215 * By construction above, the cursor should not
3216 * be on the right-hand half of this character.
3219 assert(!(i == our_curs_y && j == our_curs_x));
3220 if (term->disptext[idx] != *d)
3222 term->disptext[idx] = *d;
3226 if (dirty_run && ccount > 0) {
3227 do_text(ctx, start, i, ch, ccount, attr, lattr);
3231 /* Cursor on this line ? (and changed) */
3232 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3233 ch[0] = (char) (cursor_background & CHAR_MASK);
3234 attr = (cursor_background & ATTR_MASK) | cursor;
3235 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3236 term->curstype = cursor;
3242 * Flick the switch that says if blinking things should be shown or hidden.
3245 void term_blink(Terminal *term, int flg)
3247 long now, blink_diff;
3249 now = GETTICKCOUNT();
3250 blink_diff = now - term->last_tblink;
3252 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3253 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3254 term->last_tblink = now;
3255 term->tblinker = !term->tblinker;
3260 term->last_blink = now;
3264 blink_diff = now - term->last_blink;
3266 /* Make sure the cursor blinks no faster than system blink rate */
3267 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3270 term->last_blink = now;
3271 term->blinker = !term->blinker;
3275 * Invalidate the whole screen so it will be repainted in full.
3277 void term_invalidate(Terminal *term)
3281 for (i = 0; i < term->rows * (term->cols + 1); i++)
3282 term->disptext[i] = ATTR_INVALID;
3286 * Paint the window in response to a WM_PAINT message.
3288 void term_paint(Terminal *term, Context ctx,
3289 int left, int top, int right, int bottom, int immediately)
3292 if (left < 0) left = 0;
3293 if (top < 0) top = 0;
3294 if (right >= term->cols) right = term->cols-1;
3295 if (bottom >= term->rows) bottom = term->rows-1;
3297 for (i = top; i <= bottom && i < term->rows; i++) {
3298 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3299 LATTR_MODE) == LATTR_NORM)
3300 for (j = left; j <= right && j < term->cols; j++)
3301 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3303 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3304 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3307 /* This should happen soon enough, also for some reason it sometimes
3308 * fails to actually do anything when re-sizing ... painting the wrong
3312 do_paint (term, ctx, FALSE);
3316 * Attempt to scroll the scrollback. The second parameter gives the
3317 * position we want to scroll to; the first is +1 to denote that
3318 * this position is relative to the beginning of the scrollback, -1
3319 * to denote it is relative to the end, and 0 to denote that it is
3320 * relative to the current position.
3322 void term_scroll(Terminal *term, int rel, int where)
3324 int sbtop = -count234(term->scrollback);
3325 #ifdef OPTIMISE_SCROLL
3326 int olddisptop = term->disptop;
3328 #endif /* OPTIMISE_SCROLL */
3330 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3331 if (term->disptop < sbtop)
3332 term->disptop = sbtop;
3333 if (term->disptop > 0)
3336 #ifdef OPTIMISE_SCROLL
3337 shift = (term->disptop - olddisptop);
3338 if (shift < term->rows && shift > -term->rows)
3339 scroll_display(term, 0, term->rows - 1, shift);
3340 #endif /* OPTIMISE_SCROLL */
3344 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3347 wchar_t *wbptr; /* where next char goes within workbuf */
3349 int wblen = 0; /* workbuf len */
3350 int buflen; /* amount of memory allocated to workbuf */
3352 buflen = 5120; /* Default size */
3353 workbuf = smalloc(buflen * sizeof(wchar_t));
3354 wbptr = workbuf; /* start filling here */
3355 old_top_x = top.x; /* needed for rect==1 */
3357 while (poslt(top, bottom)) {
3359 unsigned long *ldata = lineptr(top.y);
3363 * nlpos will point at the maximum position on this line we
3364 * should copy up to. So we start it at the end of the
3368 nlpos.x = term->cols;
3371 * ... move it backwards if there's unused space at the end
3372 * of the line (and also set `nl' if this is the case,
3373 * because in normal selection mode this means we need a
3374 * newline at the end)...
3376 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3377 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3378 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3379 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3380 && poslt(top, nlpos))
3382 if (poslt(nlpos, bottom))
3384 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3385 /* Ignore the last char on the line in a WRAPPED2 line. */
3390 * ... and then clip it to the terminal x coordinate if
3391 * we're doing rectangular selection. (In this case we
3392 * still did the above, so that copying e.g. the right-hand
3393 * column from a table doesn't fill with spaces on the
3397 if (nlpos.x > bottom.x)
3399 nl = (top.y < bottom.y);
3402 while (poslt(top, bottom) && poslt(top, nlpos)) {
3405 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3407 wchar_t cbuf[16], *p;
3408 int uc = (ldata[top.x] & 0xFFFF);
3411 if (uc == UCSWIDE) {
3416 switch (uc & CSET_MASK) {
3418 if (!term->cfg.rawcnp) {
3419 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3423 uc = term->ucsdata->unitab_line[uc & 0xFF];
3426 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3429 switch (uc & CSET_MASK) {
3431 uc = term->ucsdata->unitab_font[uc & 0xFF];
3434 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3438 set = (uc & CSET_MASK);
3439 c = (uc & CHAR_MASK);
3443 if (DIRECT_FONT(uc)) {
3444 if (c >= ' ' && c != 0x7F) {
3448 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3450 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3451 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3455 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3459 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3466 for (p = cbuf; *p; p++) {
3467 /* Enough overhead for trailing NL and nul */
3468 if (wblen >= buflen - 16) {
3471 sizeof(wchar_t) * (buflen += 100));
3472 wbptr = workbuf + wblen;
3481 for (i = 0; i < sel_nl_sz; i++) {
3483 *wbptr++ = sel_nl[i];
3487 top.x = rect ? old_top_x : 0;
3489 #if SELECTION_NUL_TERMINATED
3493 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3494 if (buflen > 0) /* indicates we allocated this buffer */
3498 void term_copyall(Terminal *term)
3501 top.y = -count234(term->scrollback);
3503 clipme(term, top, term->curs, 0);
3507 * The wordness array is mainly for deciding the disposition of the
3508 * US-ASCII characters.
3510 static int wordtype(Terminal *term, int uc)
3513 int start, end, ctype;
3515 static const struct ucsword ucs_words[] = {
3521 0x037e, 0x037e, 1}, /* Greek question mark */
3523 0x0387, 0x0387, 1}, /* Greek ano teleia */
3525 0x055a, 0x055f, 1}, /* Armenian punctuation */
3527 0x0589, 0x0589, 1}, /* Armenian full stop */
3529 0x0700, 0x070d, 1}, /* Syriac punctuation */
3531 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3533 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3535 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3537 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3539 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3541 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3543 0x2000, 0x200a, 0}, /* Various spaces */
3545 0x2070, 0x207f, 2}, /* superscript */
3547 0x2080, 0x208f, 2}, /* subscript */
3549 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3551 0x3000, 0x3000, 0}, /* ideographic space */
3553 0x3001, 0x3020, 1}, /* ideographic punctuation */
3555 0x303f, 0x309f, 3}, /* Hiragana */
3557 0x30a0, 0x30ff, 3}, /* Katakana */
3559 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3561 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3563 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3565 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3567 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3569 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3571 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3573 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3575 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3579 const struct ucsword *wptr;
3581 uc &= (CSET_MASK | CHAR_MASK);
3583 switch (uc & CSET_MASK) {
3585 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3588 uc = term->ucsdata->unitab_line[uc & 0xFF];
3591 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3594 switch (uc & CSET_MASK) {
3596 uc = term->ucsdata->unitab_font[uc & 0xFF];
3599 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3603 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3604 * fail as there's such a thing as a double width space. :-(
3606 if (term->ucsdata->dbcs_screenfont &&
3607 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3611 return term->wordness[uc];
3613 for (wptr = ucs_words; wptr->start; wptr++) {
3614 if (uc >= wptr->start && uc <= wptr->end)
3622 * Spread the selection outwards according to the selection mode.
3624 static pos sel_spread_half(Terminal *term, pos p, int dir)
3626 unsigned long *ldata;
3628 int topy = -count234(term->scrollback);
3630 ldata = lineptr(p.y);
3632 switch (term->selmode) {
3635 * In this mode, every character is a separate unit, except
3636 * for runs of spaces at the end of a non-wrapping line.
3638 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3639 unsigned long *q = ldata + term->cols;
3640 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3642 if (q == ldata + term->cols)
3644 if (p.x >= q - ldata)
3645 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3650 * In this mode, the units are maximal runs of characters
3651 * whose `wordness' has the same value.
3653 wvalue = wordtype(term, UCSGET(ldata, p.x));
3656 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3657 term->cols-1 : term->cols);
3658 if (p.x < maxcols-1) {
3659 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3664 if (ldata[term->cols] & LATTR_WRAPPED) {
3665 unsigned long *ldata2;
3666 ldata2 = lineptr(p.y+1);
3667 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3680 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3685 unsigned long *ldata2;
3689 ldata2 = lineptr(p.y-1);
3690 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3691 term->cols-1 : term->cols);
3692 if (ldata2[term->cols] & LATTR_WRAPPED) {
3693 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3708 * In this mode, every line is a unit.
3710 p.x = (dir == -1 ? 0 : term->cols - 1);
3716 static void sel_spread(Terminal *term)
3718 if (term->seltype == LEXICOGRAPHIC) {
3719 term->selstart = sel_spread_half(term, term->selstart, -1);
3720 decpos(term->selend);
3721 term->selend = sel_spread_half(term, term->selend, +1);
3722 incpos(term->selend);
3726 void term_do_paste(Terminal *term)
3731 get_clip(term->frontend, &data, &len);
3732 if (data && len > 0) {
3735 term_seen_key_event(term); /* pasted data counts */
3737 if (term->paste_buffer)
3738 sfree(term->paste_buffer);
3739 term->paste_pos = term->paste_hold = term->paste_len = 0;
3740 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3743 while (p < data + len) {
3744 while (p < data + len &&
3745 !(p <= data + len - sel_nl_sz &&
3746 !memcmp(p, sel_nl, sizeof(sel_nl))))
3751 for (i = 0; i < p - q; i++) {
3752 term->paste_buffer[term->paste_len++] = q[i];
3756 if (p <= data + len - sel_nl_sz &&
3757 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3758 term->paste_buffer[term->paste_len++] = '\015';
3764 /* Assume a small paste will be OK in one go. */
3765 if (term->paste_len < 256) {
3767 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3768 if (term->paste_buffer)
3769 sfree(term->paste_buffer);
3770 term->paste_buffer = 0;
3771 term->paste_pos = term->paste_hold = term->paste_len = 0;
3774 get_clip(term->frontend, NULL, NULL);
3777 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3778 int shift, int ctrl, int alt)
3781 unsigned long *ldata;
3782 int raw_mouse = (term->xterm_mouse &&
3783 !term->cfg.no_mouse_rep &&
3784 !(term->cfg.mouse_override && shift));
3785 int default_seltype;
3789 if (a == MA_DRAG && !raw_mouse)
3790 term_scroll(term, 0, -1);
3792 if (y >= term->rows) {
3794 if (a == MA_DRAG && !raw_mouse)
3795 term_scroll(term, 0, +1);
3804 if (x >= term->cols)
3807 selpoint.y = y + term->disptop;
3809 ldata = lineptr(selpoint.y);
3810 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3814 int encstate = 0, r, c;
3821 encstate = 0x20; /* left button down */
3832 case MBT_WHEEL_DOWN:
3835 default: break; /* placate gcc warning about enum use */
3839 if (term->xterm_mouse == 1)
3845 term->mouse_is_down = 0;
3848 if (term->mouse_is_down == b)
3850 term->mouse_is_down = b;
3852 default: break; /* placate gcc warning about enum use */
3861 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3862 ldisc_send(term->ldisc, abuf, 6, 0);
3867 b = translate_button(term->frontend, b);
3870 * Set the selection type (rectangular or normal) at the start
3871 * of a selection attempt, from the state of Alt.
3873 if (!alt ^ !term->cfg.rect_select)
3874 default_seltype = RECTANGULAR;
3876 default_seltype = LEXICOGRAPHIC;
3878 if (term->selstate == NO_SELECTION) {
3879 term->seltype = default_seltype;
3882 if (b == MBT_SELECT && a == MA_CLICK) {
3884 term->selstate = ABOUT_TO;
3885 term->seltype = default_seltype;
3886 term->selanchor = selpoint;
3887 term->selmode = SM_CHAR;
3888 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3890 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3891 term->selstate = DRAGGING;
3892 term->selstart = term->selanchor = selpoint;
3893 term->selend = term->selstart;
3894 incpos(term->selend);
3896 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3897 (b == MBT_EXTEND && a != MA_RELEASE)) {
3898 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3900 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3901 if (term->seltype == LEXICOGRAPHIC) {
3903 * For normal selection, we extend by moving
3904 * whichever end of the current selection is closer
3907 if (posdiff(selpoint, term->selstart) <
3908 posdiff(term->selend, term->selstart) / 2) {
3909 term->selanchor = term->selend;
3910 decpos(term->selanchor);
3912 term->selanchor = term->selstart;
3916 * For rectangular selection, we have a choice of
3917 * _four_ places to put selanchor and selpoint: the
3918 * four corners of the selection.
3920 if (2*selpoint.x < term->selstart.x + term->selend.x)
3921 term->selanchor.x = term->selend.x-1;
3923 term->selanchor.x = term->selstart.x;
3925 if (2*selpoint.y < term->selstart.y + term->selend.y)
3926 term->selanchor.y = term->selend.y;
3928 term->selanchor.y = term->selstart.y;
3930 term->selstate = DRAGGING;
3932 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3933 term->selanchor = selpoint;
3934 term->selstate = DRAGGING;
3935 if (term->seltype == LEXICOGRAPHIC) {
3937 * For normal selection, we set (selstart,selend) to
3938 * (selpoint,selanchor) in some order.
3940 if (poslt(selpoint, term->selanchor)) {
3941 term->selstart = selpoint;
3942 term->selend = term->selanchor;
3943 incpos(term->selend);
3945 term->selstart = term->selanchor;
3946 term->selend = selpoint;
3947 incpos(term->selend);
3951 * For rectangular selection, we may need to
3952 * interchange x and y coordinates (if the user has
3953 * dragged in the -x and +y directions, or vice versa).
3955 term->selstart.x = min(term->selanchor.x, selpoint.x);
3956 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3957 term->selstart.y = min(term->selanchor.y, selpoint.y);
3958 term->selend.y = max(term->selanchor.y, selpoint.y);
3961 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3962 if (term->selstate == DRAGGING) {
3964 * We've completed a selection. We now transfer the
3965 * data to the clipboard.
3967 clipme(term, term->selstart, term->selend,
3968 (term->seltype == RECTANGULAR));
3969 term->selstate = SELECTED;
3971 term->selstate = NO_SELECTION;
3972 } else if (b == MBT_PASTE
3974 #if MULTICLICK_ONLY_EVENT
3975 || a == MA_2CLK || a == MA_3CLK
3978 request_paste(term->frontend);
3984 void term_nopaste(Terminal *term)
3986 if (term->paste_len == 0)
3988 sfree(term->paste_buffer);
3989 term->paste_buffer = NULL;
3990 term->paste_len = 0;
3993 int term_paste_pending(Terminal *term)
3995 return term->paste_len != 0;
3998 void term_paste(Terminal *term)
4000 long now, paste_diff;
4002 if (term->paste_len == 0)
4005 /* Don't wait forever to paste */
4006 if (term->paste_hold) {
4007 now = GETTICKCOUNT();
4008 paste_diff = now - term->last_paste;
4009 if (paste_diff >= 0 && paste_diff < 450)
4012 term->paste_hold = 0;
4014 while (term->paste_pos < term->paste_len) {
4016 while (n + term->paste_pos < term->paste_len) {
4017 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4021 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4022 term->paste_pos += n;
4024 if (term->paste_pos < term->paste_len) {
4025 term->paste_hold = 1;
4029 sfree(term->paste_buffer);
4030 term->paste_buffer = NULL;
4031 term->paste_len = 0;
4034 static void deselect(Terminal *term)
4036 term->selstate = NO_SELECTION;
4037 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4040 void term_deselect(Terminal *term)
4046 int term_ldisc(Terminal *term, int option)
4048 if (option == LD_ECHO)
4049 return term->term_echoing;
4050 if (option == LD_EDIT)
4051 return term->term_editing;
4056 * from_backend(), to get data from the backend for the terminal.
4058 int from_backend(void *vterm, int is_stderr, char *data, int len)
4060 Terminal *term = (Terminal *)vterm;
4064 bufchain_add(&term->inbuf, data, len);
4067 * term_out() always completely empties inbuf. Therefore,
4068 * there's no reason at all to return anything other than zero
4069 * from this function, because there _can't_ be a question of
4070 * the remote side needing to wait until term_out() has cleared
4073 * This is a slightly suboptimal way to deal with SSH2 - in
4074 * principle, the window mechanism would allow us to continue
4075 * to accept data on forwarded ports and X connections even
4076 * while the terminal processing was going slowly - but we
4077 * can't do the 100% right thing without moving the terminal
4078 * processing into a separate thread, and that might hurt
4079 * portability. So we manage stdout buffering the old SSH1 way:
4080 * if the terminal processing goes slowly, the whole SSH
4081 * connection stops accepting data until it's ready.
4083 * In practice, I can't imagine this causing serious trouble.
4088 void term_provide_logctx(Terminal *term, void *logctx)
4090 term->logctx = logctx;