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, void *frontend)
345 * Allocate a new Terminal structure and initialise the fields
348 term = smalloc(sizeof(Terminal));
349 term->frontend = frontend;
350 term->cfg = *mycfg; /* STRUCTURE COPY */
352 term->compatibility_level = TM_PUTTY;
353 strcpy(term->id_string, "\033[?6c");
354 term->last_blink = term->last_tblink = 0;
355 term->paste_buffer = NULL;
357 term->last_paste = 0;
358 bufchain_init(&term->inbuf);
359 bufchain_init(&term->printer_buf);
360 term->printing = term->only_printing = FALSE;
361 term->print_job = NULL;
362 term->vt52_mode = FALSE;
363 term->cr_lf_return = FALSE;
364 term->seen_disp_event = FALSE;
365 term->xterm_mouse = term->mouse_is_down = FALSE;
366 term->reset_132 = FALSE;
367 term->blinker = term->tblinker = 0;
369 term->repeat_off = FALSE;
370 term->termstate = TOPLEVEL;
371 term->selstate = NO_SELECTION;
374 term->screen = term->alt_screen = term->scrollback = NULL;
376 term->disptext = term->dispcurs = NULL;
379 term->rows = term->cols = -1;
381 term->beephead = term->beeptail = NULL;
383 term->lastbeep = FALSE;
384 term->beep_overloaded = FALSE;
385 term->attr_mask = 0xffffffff;
386 term->resize_fn = NULL;
387 term->resize_ctx = NULL;
393 * Set up the terminal for a given size.
395 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
398 unsigned long *newdisp, *line;
401 int save_alt_which = term->alt_which;
403 if (newrows == term->rows && newcols == term->cols &&
404 newsavelines == term->savelines)
405 return; /* nothing to do */
408 swap_screen(term, 0, FALSE, FALSE);
410 term->alt_t = term->marg_t = 0;
411 term->alt_b = term->marg_b = newrows - 1;
413 if (term->rows == -1) {
414 term->scrollback = newtree234(NULL);
415 term->screen = newtree234(NULL);
420 * Resize the screen and scrollback. We only need to shift
421 * lines around within our data structures, because lineptr()
422 * will take care of resizing each individual line if
425 * - If the new screen and the old screen differ in length, we
426 * must shunt some lines in from the scrollback or out to
429 * - If doing that fails to provide us with enough material to
430 * fill the new screen (i.e. the number of rows needed in
431 * the new screen exceeds the total number in the previous
432 * screen+scrollback), we must invent some blank lines to
435 * - Then, if the new scrollback length is less than the
436 * amount of scrollback we actually have, we must throw some
439 sblen = count234(term->scrollback);
440 /* Do this loop to expand the screen if newrows > rows */
441 for (i = term->rows; i < newrows; i++) {
443 line = delpos234(term->scrollback, --sblen);
445 line = smalloc(TSIZE * (newcols + 2));
447 for (j = 0; j <= newcols; j++)
448 line[j + 1] = ERASE_CHAR;
450 addpos234(term->screen, line, 0);
452 /* Do this loop to shrink the screen if newrows < rows */
453 for (i = newrows; i < term->rows; i++) {
454 line = delpos234(term->screen, 0);
455 addpos234(term->scrollback, line, sblen++);
457 assert(count234(term->screen) == newrows);
458 while (sblen > newsavelines) {
459 line = delpos234(term->scrollback, 0);
463 assert(count234(term->scrollback) <= newsavelines);
466 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
467 for (i = 0; i < newrows * (newcols + 1); i++)
468 newdisp[i] = ATTR_INVALID;
469 sfree(term->disptext);
470 term->disptext = newdisp;
471 term->dispcurs = NULL;
473 newalt = newtree234(NULL);
474 for (i = 0; i < newrows; i++) {
475 line = smalloc(TSIZE * (newcols + 2));
477 for (j = 0; j <= newcols; j++)
478 line[j + 1] = term->erase_char;
479 addpos234(newalt, line, i);
481 if (term->alt_screen) {
482 while (NULL != (line = delpos234(term->alt_screen, 0)))
484 freetree234(term->alt_screen);
486 term->alt_screen = newalt;
488 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
491 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
492 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
496 term->curs.y += newrows - term->rows;
497 if (term->curs.y < 0)
499 if (term->curs.y >= newrows)
500 term->curs.y = newrows - 1;
501 if (term->curs.x >= newcols)
502 term->curs.x = newcols - 1;
503 term->alt_x = term->alt_y = 0;
504 term->wrapnext = term->alt_wnext = FALSE;
506 term->rows = newrows;
507 term->cols = newcols;
508 term->savelines = newsavelines;
511 swap_screen(term, save_alt_which, FALSE, FALSE);
516 term->resize_fn(term->resize_ctx, term->cols, term->rows);
520 * Hand a function and context pointer to the terminal which it can
521 * use to notify a back end of resizes.
523 void term_provide_resize_fn(Terminal *term,
524 void (*resize_fn)(void *, int, int),
527 term->resize_fn = resize_fn;
528 term->resize_ctx = resize_ctx;
529 if (term->cols > 0 && term->rows > 0)
530 resize_fn(resize_ctx, term->cols, term->rows);
534 * Swap screens. If `reset' is TRUE and we have been asked to
535 * switch to the alternate screen, we must bring most of its
536 * configuration from the main screen and erase the contents of the
537 * alternate screen completely. (This is even true if we're already
538 * on it! Blame xterm.)
540 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
546 reset = FALSE; /* do no weird resetting if which==0 */
548 if (which != term->alt_which) {
549 term->alt_which = which;
551 ttr = term->alt_screen;
552 term->alt_screen = term->screen;
555 if (!reset && !keep_cur_pos)
556 term->curs.x = term->alt_x;
559 if (!reset && !keep_cur_pos)
560 term->curs.y = term->alt_y;
563 if (!reset) term->marg_t = term->alt_t;
566 if (!reset) term->marg_b = term->alt_b;
569 if (!reset) term->dec_om = term->alt_om;
572 if (!reset) term->wrap = term->alt_wrap;
575 if (!reset) term->wrapnext = term->alt_wnext;
578 if (!reset) term->insert = term->alt_ins;
581 if (!reset) term->cset = term->alt_cset;
584 if (!reset) term->utf = term->alt_utf;
587 if (!reset) term->sco_acs = term->alt_sco_acs;
588 term->alt_sco_acs = t;
591 if (reset && term->screen) {
593 * Yes, this _is_ supposed to honour background-colour-erase.
595 erase_lots(term, FALSE, TRUE, TRUE);
599 * This might not be possible if we're called during
607 * Update the scroll bar.
609 static void update_sbar(Terminal *term)
613 nscroll = count234(term->scrollback);
615 set_sbar(term->frontend, nscroll + term->rows,
616 nscroll + term->disptop, term->rows);
620 * Check whether the region bounded by the two pointers intersects
621 * the scroll region, and de-select the on-screen selection if so.
623 static void check_selection(Terminal *term, pos from, pos to)
625 if (poslt(from, term->selend) && poslt(term->selstart, to))
630 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
631 * for backward.) `sb' is TRUE if the scrolling is permitted to
632 * affect the scrollback buffer.
634 * NB this function invalidates all pointers into lines of the
635 * screen data structures. In particular, you MUST call fix_cpos
636 * after calling scroll() and before doing anything else that
637 * uses the cpos shortcut pointer.
639 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
641 unsigned long *line, *line2;
642 int i, seltop, olddisptop, shift;
644 if (topline != 0 || term->alt_which != 0)
647 olddisptop = term->disptop;
651 line = delpos234(term->screen, botline);
652 line = resizeline(line, term->cols);
653 for (i = 0; i < term->cols; i++)
654 line[i + 1] = term->erase_char;
655 line[term->cols + 1] = 0;
656 addpos234(term->screen, line, topline);
658 if (term->selstart.y >= topline && term->selstart.y <= botline) {
660 if (term->selstart.y > botline) {
661 term->selstart.y = botline;
662 term->selstart.x = 0;
665 if (term->selend.y >= topline && term->selend.y <= botline) {
667 if (term->selend.y > botline) {
668 term->selend.y = botline;
677 line = delpos234(term->screen, topline);
678 if (sb && term->savelines > 0) {
679 int sblen = count234(term->scrollback);
681 * We must add this line to the scrollback. We'll
682 * remove a line from the top of the scrollback to
683 * replace it, or allocate a new one if the
684 * scrollback isn't full.
686 if (sblen == term->savelines) {
687 sblen--, line2 = delpos234(term->scrollback, 0);
689 line2 = smalloc(TSIZE * (term->cols + 2));
690 line2[0] = term->cols;
692 addpos234(term->scrollback, line, sblen);
696 * If the user is currently looking at part of the
697 * scrollback, and they haven't enabled any options
698 * that are going to reset the scrollback as a
699 * result of this movement, then the chances are
700 * they'd like to keep looking at the same line. So
701 * we move their viewpoint at the same rate as the
702 * scroll, at least until their viewpoint hits the
703 * top end of the scrollback buffer, at which point
704 * we don't have the choice any more.
706 * Thanks to Jan Holmen Holsten for the idea and
707 * initial implementation.
709 if (term->disptop > -term->savelines && term->disptop < 0)
712 line = resizeline(line, term->cols);
713 for (i = 0; i < term->cols; i++)
714 line[i + 1] = term->erase_char;
715 line[term->cols + 1] = 0;
716 addpos234(term->screen, line, botline);
719 * If the selection endpoints move into the scrollback,
720 * we keep them moving until they hit the top. However,
721 * of course, if the line _hasn't_ moved into the
722 * scrollback then we don't do this, and cut them off
723 * at the top of the scroll region.
725 * This applies to selstart and selend (for an existing
726 * selection), and also selanchor (for one being
727 * selected as we speak).
729 seltop = sb ? -term->savelines : topline;
731 if (term->selstart.y >= seltop &&
732 term->selstart.y <= botline) {
734 if (term->selstart.y < seltop) {
735 term->selstart.y = seltop;
736 term->selstart.x = 0;
739 if (term->selend.y >= seltop && term->selend.y <= botline) {
741 if (term->selend.y < seltop) {
742 term->selend.y = seltop;
746 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
748 if (term->selanchor.y < seltop) {
749 term->selanchor.y = seltop;
750 term->selanchor.x = 0;
757 #ifdef OPTIMISE_SCROLL
758 shift += term->disptop - olddisptop;
759 if (shift < term->rows && shift > -term->rows && shift != 0)
760 scroll_display(term, topline, botline, shift);
761 #endif /* OPTIMISE_SCROLL */
764 #ifdef OPTIMISE_SCROLL
766 * Scroll the physical display, and our conception of it in disptext.
768 static void scroll_display(Terminal *term, int topline, int botline, int lines)
770 unsigned long *start, *end;
771 int distance, size, i;
773 start = term->disptext + topline * (term->cols + 1);
774 end = term->disptext + (botline + 1) * (term->cols + 1);
775 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
776 size = end - start - distance;
778 memmove(start, start + distance, size * TSIZE);
779 if (term->dispcurs >= start + distance &&
780 term->dispcurs <= start + distance + size)
781 term->dispcurs -= distance;
782 for (i = 0; i < distance; i++)
783 (start + size)[i] |= ATTR_INVALID;
785 memmove(start + distance, start, size * TSIZE);
786 if (term->dispcurs >= start && term->dispcurs <= start + size)
787 term->dispcurs += distance;
788 for (i = 0; i < distance; i++)
789 start[i] |= ATTR_INVALID;
791 do_scroll(term->frontend, topline, botline, lines);
793 #endif /* OPTIMISE_SCROLL */
796 * Move the cursor to a given position, clipping at boundaries. We
797 * may or may not want to clip at the scroll margin: marg_clip is 0
798 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
799 * even _being_ outside the margins.
801 static void move(Terminal *term, int x, int y, int marg_clip)
808 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
811 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
822 term->wrapnext = FALSE;
826 * Save or restore the cursor and SGR mode.
828 static void save_cursor(Terminal *term, int save)
831 term->savecurs = term->curs;
832 term->save_attr = term->curr_attr;
833 term->save_cset = term->cset;
834 term->save_utf = term->utf;
835 term->save_wnext = term->wrapnext;
836 term->save_csattr = term->cset_attr[term->cset];
837 term->save_sco_acs = term->sco_acs;
839 term->curs = term->savecurs;
840 /* Make sure the window hasn't shrunk since the save */
841 if (term->curs.x >= term->cols)
842 term->curs.x = term->cols - 1;
843 if (term->curs.y >= term->rows)
844 term->curs.y = term->rows - 1;
846 term->curr_attr = term->save_attr;
847 term->cset = term->save_cset;
848 term->utf = term->save_utf;
849 term->wrapnext = term->save_wnext;
851 * wrapnext might reset to False if the x position is no
852 * longer at the rightmost edge.
854 if (term->wrapnext && term->curs.x < term->cols-1)
855 term->wrapnext = FALSE;
856 term->cset_attr[term->cset] = term->save_csattr;
857 term->sco_acs = term->save_sco_acs;
860 term->erase_char = (' ' | ATTR_ASCII |
862 (ATTR_FGMASK | ATTR_BGMASK)));
867 * This function is called before doing _anything_ which affects
868 * only part of a line of text. It is used to mark the boundary
869 * between two character positions, and it indicates that some sort
870 * of effect is going to happen on only one side of that boundary.
872 * The effect of this function is to check whether a CJK
873 * double-width character is straddling the boundary, and to remove
874 * it and replace it with two spaces if so. (Of course, one or
875 * other of those spaces is then likely to be replaced with
876 * something else again, as a result of whatever happens next.)
878 * Also, if the boundary is at the right-hand _edge_ of the screen,
879 * it implies something deliberate is being done to the rightmost
880 * column position; hence we must clear LATTR_WRAPPED2.
882 * The input to the function is the coordinates of the _second_
883 * character of the pair.
885 static void check_boundary(Terminal *term, int x, int y)
887 unsigned long *ldata;
889 /* Validate input coordinates, just in case. */
890 if (x == 0 || x > term->cols)
894 if (x == term->cols) {
895 ldata[x] &= ~LATTR_WRAPPED2;
897 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
898 ldata[x-1] = ldata[x] =
899 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
905 * Erase a large portion of the screen: the whole screen, or the
906 * whole line, or parts thereof.
908 static void erase_lots(Terminal *term,
909 int line_only, int from_begin, int to_end)
913 unsigned long *ldata;
916 start.y = term->curs.y;
918 end.y = term->curs.y + 1;
935 if (!from_begin || !to_end)
936 check_boundary(term, term->curs.x, term->curs.y);
937 check_selection(term, start, end);
939 /* Clear screen also forces a full window redraw, just in case. */
940 if (start.y == 0 && start.x == 0 && end.y == term->rows)
941 term_invalidate(term);
943 ldata = lineptr(start.y);
944 while (poslt(start, end)) {
945 if (start.x == term->cols && !erase_lattr)
946 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
948 ldata[start.x] = term->erase_char;
949 if (incpos(start) && start.y < term->rows)
950 ldata = lineptr(start.y);
955 * Insert or delete characters within the current line. n is +ve if
956 * insertion is desired, and -ve for deletion.
958 static void insch(Terminal *term, int n)
960 int dir = (n < 0 ? -1 : +1);
963 unsigned long *ldata;
965 n = (n < 0 ? -n : n);
966 if (n > term->cols - term->curs.x)
967 n = term->cols - term->curs.x;
968 m = term->cols - term->curs.x - n;
969 cursplus.y = term->curs.y;
970 cursplus.x = term->curs.x + n;
971 check_selection(term, term->curs, cursplus);
972 check_boundary(term, term->curs.x, term->curs.y);
974 check_boundary(term, term->curs.x + n, term->curs.y);
975 ldata = lineptr(term->curs.y);
977 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
979 ldata[term->curs.x + m++] = term->erase_char;
981 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
983 ldata[term->curs.x + n] = term->erase_char;
988 * Toggle terminal mode `mode' to state `state'. (`query' indicates
989 * whether the mode is a DEC private one or a normal one.)
991 static void toggle_mode(Terminal *term, int mode, int query, int state)
997 case 1: /* application cursor keys */
998 term->app_cursor_keys = state;
1000 case 2: /* VT52 mode */
1001 term->vt52_mode = !state;
1002 if (term->vt52_mode) {
1003 term->blink_is_real = FALSE;
1004 term->vt52_bold = FALSE;
1006 term->blink_is_real = term->cfg.blinktext;
1009 case 3: /* 80/132 columns */
1011 if (!term->cfg.no_remote_resize)
1012 request_resize(term->frontend, state ? 132 : 80, term->rows);
1013 term->reset_132 = state;
1015 case 5: /* reverse video */
1017 * Toggle reverse video. If we receive an OFF within the
1018 * visual bell timeout period after an ON, we trigger an
1019 * effective visual bell, so that ESC[?5hESC[?5l will
1020 * always be an actually _visible_ visual bell.
1022 ticks = GETTICKCOUNT();
1023 /* turn off a previous vbell to avoid inconsistencies */
1024 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1025 term->in_vbell = FALSE;
1026 if (term->rvideo && !state && /* we're turning it off... */
1027 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1028 /* If there's no vbell timeout already, or this one lasts
1029 * longer, replace vbell_timeout with ours. */
1030 if (!term->in_vbell ||
1031 (term->rvbell_startpoint - term->vbell_startpoint <
1033 term->vbell_startpoint = term->rvbell_startpoint;
1034 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1035 } else if (!term->rvideo && state) {
1036 /* This is an ON, so we notice the time and save it. */
1037 term->rvbell_startpoint = ticks;
1039 term->rvideo = state;
1040 term->seen_disp_event = TRUE;
1044 case 6: /* DEC origin mode */
1045 term->dec_om = state;
1047 case 7: /* auto wrap */
1050 case 8: /* auto key repeat */
1051 term->repeat_off = !state;
1053 case 10: /* set local edit mode */
1054 term->term_editing = state;
1055 if (term->ldisc) /* cause ldisc to notice changes */
1056 ldisc_send(term->ldisc, NULL, 0, 0);
1058 case 25: /* enable/disable cursor */
1059 compatibility2(OTHER, VT220);
1060 term->cursor_on = state;
1061 term->seen_disp_event = TRUE;
1063 case 47: /* alternate screen */
1064 compatibility(OTHER);
1066 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1069 case 1000: /* xterm mouse 1 */
1070 term->xterm_mouse = state ? 1 : 0;
1071 set_raw_mouse_mode(term->frontend, state);
1073 case 1002: /* xterm mouse 2 */
1074 term->xterm_mouse = state ? 2 : 0;
1075 set_raw_mouse_mode(term->frontend, state);
1077 case 1047: /* alternate screen */
1078 compatibility(OTHER);
1080 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1083 case 1048: /* save/restore cursor */
1084 save_cursor(term, state);
1085 if (!state) term->seen_disp_event = TRUE;
1087 case 1049: /* cursor & alternate screen */
1089 save_cursor(term, state);
1090 if (!state) term->seen_disp_event = TRUE;
1091 compatibility(OTHER);
1093 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1095 save_cursor(term, state);
1100 case 4: /* set insert mode */
1101 compatibility(VT102);
1102 term->insert = state;
1104 case 12: /* set echo mode */
1105 term->term_echoing = !state;
1106 if (term->ldisc) /* cause ldisc to notice changes */
1107 ldisc_send(term->ldisc, NULL, 0, 0);
1109 case 20: /* Return sends ... */
1110 term->cr_lf_return = state;
1112 case 34: /* Make cursor BIG */
1113 compatibility2(OTHER, VT220);
1114 term->big_cursor = !state;
1119 * Process an OSC sequence: set window title or icon name.
1121 static void do_osc(Terminal *term)
1124 while (term->osc_strlen--)
1125 term->wordness[(unsigned char)
1126 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1128 term->osc_string[term->osc_strlen] = '\0';
1129 switch (term->esc_args[0]) {
1132 if (!term->cfg.no_remote_wintitle)
1133 set_icon(term->frontend, term->osc_string);
1134 if (term->esc_args[0] == 1)
1136 /* fall through: parameter 0 means set both */
1139 if (!term->cfg.no_remote_wintitle)
1140 set_title(term->frontend, term->osc_string);
1147 * ANSI printing routines.
1149 static void term_print_setup(Terminal *term)
1151 bufchain_clear(&term->printer_buf);
1152 term->print_job = printer_start_job(term->cfg.printer);
1154 static void term_print_flush(Terminal *term)
1159 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1160 bufchain_prefix(&term->printer_buf, &data, &len);
1163 printer_job_data(term->print_job, data, len);
1164 bufchain_consume(&term->printer_buf, len);
1167 static void term_print_finish(Terminal *term)
1173 if (!term->printing && !term->only_printing)
1174 return; /* we need do nothing */
1176 term_print_flush(term);
1177 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1178 bufchain_prefix(&term->printer_buf, &data, &len);
1180 if (c == '\033' || c == '\233') {
1181 bufchain_consume(&term->printer_buf, size);
1184 printer_job_data(term->print_job, &c, 1);
1185 bufchain_consume(&term->printer_buf, 1);
1188 printer_finish_job(term->print_job);
1189 term->print_job = NULL;
1190 term->printing = term->only_printing = FALSE;
1194 * Remove everything currently in `inbuf' and stick it up on the
1195 * in-memory display. There's a big state machine in here to
1196 * process escape sequences...
1198 void term_out(Terminal *term)
1201 unsigned char localbuf[256], *chars;
1206 chars = NULL; /* placate compiler warnings */
1207 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1211 bufchain_prefix(&term->inbuf, &ret, &nchars);
1212 if (nchars > sizeof(localbuf))
1213 nchars = sizeof(localbuf);
1214 memcpy(localbuf, ret, nchars);
1215 bufchain_consume(&term->inbuf, nchars);
1217 assert(chars != NULL);
1223 * Optionally log the session traffic to a file. Useful for
1224 * debugging and possibly also useful for actual logging.
1226 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1227 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1233 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1234 * be able to display 8-bit characters, but I'll let that go 'cause
1239 * If we're printing, add the character to the printer
1242 if (term->printing) {
1243 bufchain_add(&term->printer_buf, &c, 1);
1246 * If we're in print-only mode, we use a much simpler
1247 * state machine designed only to recognise the ESC[4i
1248 * termination sequence.
1250 if (term->only_printing) {
1252 term->print_state = 1;
1253 else if (c == (unsigned char)'\233')
1254 term->print_state = 2;
1255 else if (c == '[' && term->print_state == 1)
1256 term->print_state = 2;
1257 else if (c == '4' && term->print_state == 2)
1258 term->print_state = 3;
1259 else if (c == 'i' && term->print_state == 3)
1260 term->print_state = 4;
1262 term->print_state = 0;
1263 if (term->print_state == 4) {
1264 term_print_finish(term);
1270 /* First see about all those translations. */
1271 if (term->termstate == TOPLEVEL) {
1273 switch (term->utf_state) {
1276 /* UTF-8 must be stateless so we ignore iso2022. */
1277 if (unitab_ctrl[c] != 0xFF)
1279 else c = ((unsigned char)c) | ATTR_ASCII;
1281 } else if ((c & 0xe0) == 0xc0) {
1282 term->utf_size = term->utf_state = 1;
1283 term->utf_char = (c & 0x1f);
1284 } else if ((c & 0xf0) == 0xe0) {
1285 term->utf_size = term->utf_state = 2;
1286 term->utf_char = (c & 0x0f);
1287 } else if ((c & 0xf8) == 0xf0) {
1288 term->utf_size = term->utf_state = 3;
1289 term->utf_char = (c & 0x07);
1290 } else if ((c & 0xfc) == 0xf8) {
1291 term->utf_size = term->utf_state = 4;
1292 term->utf_char = (c & 0x03);
1293 } else if ((c & 0xfe) == 0xfc) {
1294 term->utf_size = term->utf_state = 5;
1295 term->utf_char = (c & 0x01);
1306 if ((c & 0xC0) != 0x80) {
1309 term->utf_state = 0;
1312 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1313 if (--term->utf_state)
1318 /* Is somebody trying to be evil! */
1320 (c < 0x800 && term->utf_size >= 2) ||
1321 (c < 0x10000 && term->utf_size >= 3) ||
1322 (c < 0x200000 && term->utf_size >= 4) ||
1323 (c < 0x4000000 && term->utf_size >= 5))
1326 /* Unicode line separator and paragraph separator are CR-LF */
1327 if (c == 0x2028 || c == 0x2029)
1330 /* High controls are probably a Baaad idea too. */
1334 /* The UTF-16 surrogates are not nice either. */
1335 /* The standard give the option of decoding these:
1336 * I don't want to! */
1337 if (c >= 0xD800 && c < 0xE000)
1340 /* ISO 10646 characters now limited to UTF-16 range. */
1344 /* This is currently a TagPhobic application.. */
1345 if (c >= 0xE0000 && c <= 0xE007F)
1348 /* U+FEFF is best seen as a null. */
1351 /* But U+FFFE is an error. */
1352 if (c == 0xFFFE || c == 0xFFFF)
1355 /* Oops this is a 16bit implementation */
1360 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1361 else if(term->sco_acs &&
1362 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1364 if (term->sco_acs == 2) c |= 0x80;
1367 switch (term->cset_attr[term->cset]) {
1369 * Linedraw characters are different from 'ESC ( B'
1370 * only for a small range. For ones outside that
1371 * range, make sure we use the same font as well as
1372 * the same encoding.
1375 if (unitab_ctrl[c] != 0xFF)
1378 c = ((unsigned char) c) | ATTR_LINEDRW;
1382 /* If UK-ASCII, make the '#' a LineDraw Pound */
1384 c = '}' | ATTR_LINEDRW;
1387 /*FALLTHROUGH*/ case ATTR_ASCII:
1388 if (unitab_ctrl[c] != 0xFF)
1391 c = ((unsigned char) c) | ATTR_ASCII;
1394 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1400 /* How about C1 controls ? */
1401 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1402 !term->vt52_mode && has_compat(VT220)) {
1403 term->termstate = SEEN_ESC;
1404 term->esc_query = FALSE;
1405 c = '@' + (c & 0x1F);
1408 /* Or the GL control. */
1409 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1410 if (term->curs.x && !term->wrapnext)
1412 term->wrapnext = FALSE;
1414 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1415 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1417 /* Or normal C0 controls. */
1418 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1420 case '\005': /* terminal type query */
1421 /* Strictly speaking this is VT100 but a VT100 defaults to
1422 * no response. Other terminals respond at their option.
1424 * Don't put a CR in the default string as this tends to
1425 * upset some weird software.
1427 * An xterm returns "xterm" (5 characters)
1429 compatibility(ANSIMIN);
1431 char abuf[256], *s, *d;
1433 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1435 if (*s >= 'a' && *s <= 'z')
1436 *d++ = (*s - ('a' - 1));
1437 else if ((*s >= '@' && *s <= '_') ||
1438 *s == '?' || (*s & 0x80))
1443 } else if (*s == '^') {
1448 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1454 struct beeptime *newbeep;
1455 unsigned long ticks;
1457 ticks = GETTICKCOUNT();
1459 if (!term->beep_overloaded) {
1460 newbeep = smalloc(sizeof(struct beeptime));
1461 newbeep->ticks = ticks;
1462 newbeep->next = NULL;
1463 if (!term->beephead)
1464 term->beephead = newbeep;
1466 term->beeptail->next = newbeep;
1467 term->beeptail = newbeep;
1472 * Throw out any beeps that happened more than
1475 while (term->beephead &&
1476 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1477 struct beeptime *tmp = term->beephead;
1478 term->beephead = tmp->next;
1480 if (!term->beephead)
1481 term->beeptail = NULL;
1485 if (term->cfg.bellovl && term->beep_overloaded &&
1486 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1488 * If we're currently overloaded and the
1489 * last beep was more than s seconds ago,
1490 * leave overload mode.
1492 term->beep_overloaded = FALSE;
1493 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1494 term->nbeeps >= term->cfg.bellovl_n) {
1496 * Now, if we have n or more beeps
1497 * remaining in the queue, go into overload
1500 term->beep_overloaded = TRUE;
1502 term->lastbeep = ticks;
1505 * Perform an actual beep if we're not overloaded.
1507 if (!term->cfg.bellovl || !term->beep_overloaded) {
1508 beep(term->frontend, term->cfg.beep);
1509 if (term->cfg.beep == BELL_VISUAL) {
1510 term->in_vbell = TRUE;
1511 term->vbell_startpoint = ticks;
1519 if (term->curs.x == 0 &&
1520 (term->curs.y == 0 || term->wrap == 0))
1522 else if (term->curs.x == 0 && term->curs.y > 0)
1523 term->curs.x = term->cols - 1, term->curs.y--;
1524 else if (term->wrapnext)
1525 term->wrapnext = FALSE;
1529 term->seen_disp_event = TRUE;
1532 compatibility(VT100);
1536 compatibility(VT100);
1540 if (term->vt52_mode)
1541 term->termstate = VT52_ESC;
1543 compatibility(ANSIMIN);
1544 term->termstate = SEEN_ESC;
1545 term->esc_query = FALSE;
1550 term->wrapnext = FALSE;
1552 term->seen_disp_event = TRUE;
1553 term->paste_hold = 0;
1555 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1558 if (has_compat(SCOANSI)) {
1559 move(term, 0, 0, 0);
1560 erase_lots(term, FALSE, FALSE, TRUE);
1562 term->wrapnext = FALSE;
1563 term->seen_disp_event = 1;
1567 compatibility(VT100);
1569 if (term->curs.y == term->marg_b)
1570 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1571 else if (term->curs.y < term->rows - 1)
1573 if (term->cfg.lfhascr)
1576 term->wrapnext = FALSE;
1577 term->seen_disp_event = 1;
1578 term->paste_hold = 0;
1580 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1584 pos old_curs = term->curs;
1585 unsigned long *ldata = lineptr(term->curs.y);
1589 } while (term->curs.x < term->cols - 1 &&
1590 !term->tabs[term->curs.x]);
1592 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1593 if (term->curs.x >= term->cols / 2)
1594 term->curs.x = term->cols / 2 - 1;
1596 if (term->curs.x >= term->cols)
1597 term->curs.x = term->cols - 1;
1601 check_selection(term, old_curs, term->curs);
1603 term->seen_disp_event = TRUE;
1607 switch (term->termstate) {
1609 /* Only graphic characters get this far;
1610 * ctrls are stripped above */
1611 if (term->wrapnext && term->wrap) {
1612 term->cpos[1] |= LATTR_WRAPPED;
1613 if (term->curs.y == term->marg_b)
1614 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1615 else if (term->curs.y < term->rows - 1)
1619 term->wrapnext = FALSE;
1623 if (term->selstate != NO_SELECTION) {
1624 pos cursplus = term->curs;
1626 check_selection(term, term->curs, cursplus);
1628 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1630 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1636 width = wcwidth((wchar_t) c);
1640 * If we're about to display a double-width
1641 * character starting in the rightmost
1642 * column, then we do something special
1643 * instead. We must print a space in the
1644 * last column of the screen, then wrap;
1645 * and we also set LATTR_WRAPPED2 which
1646 * instructs subsequent cut-and-pasting not
1647 * only to splice this line to the one
1648 * after it, but to ignore the space in the
1649 * last character position as well.
1650 * (Because what was actually output to the
1651 * terminal was presumably just a sequence
1652 * of CJK characters, and we don't want a
1653 * space to be pasted in the middle of
1654 * those just because they had the
1655 * misfortune to start in the wrong parity
1656 * column. xterm concurs.)
1658 check_boundary(term, term->curs.x, term->curs.y);
1659 check_boundary(term, term->curs.x+2, term->curs.y);
1660 if (term->curs.x == term->cols-1) {
1661 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1662 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1663 if (term->curs.y == term->marg_b)
1664 scroll(term, term->marg_t, term->marg_b,
1666 else if (term->curs.y < term->rows - 1)
1670 /* Now we must check_boundary again, of course. */
1671 check_boundary(term, term->curs.x, term->curs.y);
1672 check_boundary(term, term->curs.x+2, term->curs.y);
1674 *term->cpos++ = c | term->curr_attr;
1675 *term->cpos++ = UCSWIDE | term->curr_attr;
1679 check_boundary(term, term->curs.x, term->curs.y);
1680 check_boundary(term, term->curs.x+1, term->curs.y);
1681 *term->cpos++ = c | term->curr_attr;
1688 if (term->curs.x == term->cols) {
1691 term->wrapnext = TRUE;
1692 if (term->wrap && term->vt52_mode) {
1693 term->cpos[1] |= LATTR_WRAPPED;
1694 if (term->curs.y == term->marg_b)
1695 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1696 else if (term->curs.y < term->rows - 1)
1700 term->wrapnext = FALSE;
1703 term->seen_disp_event = 1;
1708 * This state is virtually identical to SEEN_ESC, with the
1709 * exception that we have an OSC sequence in the pipeline,
1710 * and _if_ we see a backslash, we process it.
1714 term->termstate = TOPLEVEL;
1717 /* else fall through */
1719 if (c >= ' ' && c <= '/') {
1720 if (term->esc_query)
1721 term->esc_query = -1;
1723 term->esc_query = c;
1726 term->termstate = TOPLEVEL;
1727 switch (ANSI(c, term->esc_query)) {
1728 case '[': /* enter CSI mode */
1729 term->termstate = SEEN_CSI;
1730 term->esc_nargs = 1;
1731 term->esc_args[0] = ARG_DEFAULT;
1732 term->esc_query = FALSE;
1734 case ']': /* xterm escape sequences */
1735 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1736 compatibility(OTHER);
1737 term->termstate = SEEN_OSC;
1738 term->esc_args[0] = 0;
1740 case '7': /* save cursor */
1741 compatibility(VT100);
1742 save_cursor(term, TRUE);
1744 case '8': /* restore cursor */
1745 compatibility(VT100);
1746 save_cursor(term, FALSE);
1747 term->seen_disp_event = TRUE;
1750 compatibility(VT100);
1751 term->app_keypad_keys = TRUE;
1754 compatibility(VT100);
1755 term->app_keypad_keys = FALSE;
1757 case 'D': /* exactly equivalent to LF */
1758 compatibility(VT100);
1759 if (term->curs.y == term->marg_b)
1760 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1761 else if (term->curs.y < term->rows - 1)
1764 term->wrapnext = FALSE;
1765 term->seen_disp_event = TRUE;
1767 case 'E': /* exactly equivalent to CR-LF */
1768 compatibility(VT100);
1770 if (term->curs.y == term->marg_b)
1771 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1772 else if (term->curs.y < term->rows - 1)
1775 term->wrapnext = FALSE;
1776 term->seen_disp_event = TRUE;
1778 case 'M': /* reverse index - backwards LF */
1779 compatibility(VT100);
1780 if (term->curs.y == term->marg_t)
1781 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1782 else if (term->curs.y > 0)
1785 term->wrapnext = FALSE;
1786 term->seen_disp_event = TRUE;
1788 case 'Z': /* terminal type query */
1789 compatibility(VT100);
1791 ldisc_send(term->ldisc, term->id_string,
1792 strlen(term->id_string), 0);
1794 case 'c': /* restore power-on settings */
1795 compatibility(VT100);
1797 if (term->ldisc) /* cause ldisc to notice changes */
1798 ldisc_send(term->ldisc, NULL, 0, 0);
1799 if (term->reset_132) {
1800 if (!term->cfg.no_remote_resize)
1801 request_resize(term->frontend, 80, term->rows);
1802 term->reset_132 = 0;
1806 term->seen_disp_event = TRUE;
1808 case 'H': /* set a tab */
1809 compatibility(VT100);
1810 term->tabs[term->curs.x] = TRUE;
1813 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1814 compatibility(VT100);
1816 unsigned long *ldata;
1820 for (i = 0; i < term->rows; i++) {
1822 for (j = 0; j < term->cols; j++)
1823 ldata[j] = ATTR_DEFAULT | 'E';
1824 ldata[term->cols] = 0;
1827 term->seen_disp_event = TRUE;
1828 scrtop.x = scrtop.y = 0;
1830 scrbot.y = term->rows;
1831 check_selection(term, scrtop, scrbot);
1835 case ANSI('3', '#'):
1836 case ANSI('4', '#'):
1837 case ANSI('5', '#'):
1838 case ANSI('6', '#'):
1839 compatibility(VT100);
1841 unsigned long nlattr;
1842 unsigned long *ldata;
1843 switch (ANSI(c, term->esc_query)) {
1844 case ANSI('3', '#'):
1847 case ANSI('4', '#'):
1850 case ANSI('5', '#'):
1851 nlattr = LATTR_NORM;
1853 default: /* spiritually case ANSI('6', '#'): */
1854 nlattr = LATTR_WIDE;
1857 ldata = lineptr(term->curs.y);
1858 ldata[term->cols] &= ~LATTR_MODE;
1859 ldata[term->cols] |= nlattr;
1863 case ANSI('A', '('):
1864 compatibility(VT100);
1865 if (!term->cfg.no_remote_charset)
1866 term->cset_attr[0] = ATTR_GBCHR;
1868 case ANSI('B', '('):
1869 compatibility(VT100);
1870 if (!term->cfg.no_remote_charset)
1871 term->cset_attr[0] = ATTR_ASCII;
1873 case ANSI('0', '('):
1874 compatibility(VT100);
1875 if (!term->cfg.no_remote_charset)
1876 term->cset_attr[0] = ATTR_LINEDRW;
1878 case ANSI('U', '('):
1879 compatibility(OTHER);
1880 if (!term->cfg.no_remote_charset)
1881 term->cset_attr[0] = ATTR_SCOACS;
1884 case ANSI('A', ')'):
1885 compatibility(VT100);
1886 if (!term->cfg.no_remote_charset)
1887 term->cset_attr[1] = ATTR_GBCHR;
1889 case ANSI('B', ')'):
1890 compatibility(VT100);
1891 if (!term->cfg.no_remote_charset)
1892 term->cset_attr[1] = ATTR_ASCII;
1894 case ANSI('0', ')'):
1895 compatibility(VT100);
1896 if (!term->cfg.no_remote_charset)
1897 term->cset_attr[1] = ATTR_LINEDRW;
1899 case ANSI('U', ')'):
1900 compatibility(OTHER);
1901 if (!term->cfg.no_remote_charset)
1902 term->cset_attr[1] = ATTR_SCOACS;
1905 case ANSI('8', '%'): /* Old Linux code */
1906 case ANSI('G', '%'):
1907 compatibility(OTHER);
1908 if (!term->cfg.no_remote_charset)
1911 case ANSI('@', '%'):
1912 compatibility(OTHER);
1913 if (!term->cfg.no_remote_charset)
1919 term->termstate = TOPLEVEL; /* default */
1921 if (term->esc_nargs <= ARGS_MAX) {
1922 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1923 term->esc_args[term->esc_nargs - 1] = 0;
1924 term->esc_args[term->esc_nargs - 1] =
1925 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1927 term->termstate = SEEN_CSI;
1928 } else if (c == ';') {
1929 if (++term->esc_nargs <= ARGS_MAX)
1930 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1931 term->termstate = SEEN_CSI;
1932 } else if (c < '@') {
1933 if (term->esc_query)
1934 term->esc_query = -1;
1936 term->esc_query = TRUE;
1938 term->esc_query = c;
1939 term->termstate = SEEN_CSI;
1941 switch (ANSI(c, term->esc_query)) {
1942 case 'A': /* move up N lines */
1943 move(term, term->curs.x,
1944 term->curs.y - def(term->esc_args[0], 1), 1);
1945 term->seen_disp_event = TRUE;
1947 case 'e': /* move down N lines */
1948 compatibility(ANSI);
1951 move(term, term->curs.x,
1952 term->curs.y + def(term->esc_args[0], 1), 1);
1953 term->seen_disp_event = TRUE;
1955 case ANSI('c', '>'): /* report xterm version */
1956 compatibility(OTHER);
1957 /* this reports xterm version 136 so that VIM can
1958 use the drag messages from the mouse reporting */
1960 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1962 case 'a': /* move right N cols */
1963 compatibility(ANSI);
1966 move(term, term->curs.x + def(term->esc_args[0], 1),
1968 term->seen_disp_event = TRUE;
1970 case 'D': /* move left N cols */
1971 move(term, term->curs.x - def(term->esc_args[0], 1),
1973 term->seen_disp_event = TRUE;
1975 case 'E': /* move down N lines and CR */
1976 compatibility(ANSI);
1978 term->curs.y + def(term->esc_args[0], 1), 1);
1979 term->seen_disp_event = TRUE;
1981 case 'F': /* move up N lines and CR */
1982 compatibility(ANSI);
1984 term->curs.y - def(term->esc_args[0], 1), 1);
1985 term->seen_disp_event = TRUE;
1988 case '`': /* set horizontal posn */
1989 compatibility(ANSI);
1990 move(term, def(term->esc_args[0], 1) - 1,
1992 term->seen_disp_event = TRUE;
1994 case 'd': /* set vertical posn */
1995 compatibility(ANSI);
1996 move(term, term->curs.x,
1997 ((term->dec_om ? term->marg_t : 0) +
1998 def(term->esc_args[0], 1) - 1),
1999 (term->dec_om ? 2 : 0));
2000 term->seen_disp_event = TRUE;
2003 case 'f': /* set horz and vert posns at once */
2004 if (term->esc_nargs < 2)
2005 term->esc_args[1] = ARG_DEFAULT;
2006 move(term, def(term->esc_args[1], 1) - 1,
2007 ((term->dec_om ? term->marg_t : 0) +
2008 def(term->esc_args[0], 1) - 1),
2009 (term->dec_om ? 2 : 0));
2010 term->seen_disp_event = TRUE;
2012 case 'J': /* erase screen or parts of it */
2014 unsigned int i = def(term->esc_args[0], 0) + 1;
2017 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2020 term->seen_disp_event = TRUE;
2022 case 'K': /* erase line or parts of it */
2024 unsigned int i = def(term->esc_args[0], 0) + 1;
2027 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2029 term->seen_disp_event = TRUE;
2031 case 'L': /* insert lines */
2032 compatibility(VT102);
2033 if (term->curs.y <= term->marg_b)
2034 scroll(term, term->curs.y, term->marg_b,
2035 -def(term->esc_args[0], 1), FALSE);
2037 term->seen_disp_event = TRUE;
2039 case 'M': /* delete lines */
2040 compatibility(VT102);
2041 if (term->curs.y <= term->marg_b)
2042 scroll(term, term->curs.y, term->marg_b,
2043 def(term->esc_args[0], 1),
2046 term->seen_disp_event = TRUE;
2048 case '@': /* insert chars */
2049 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2050 compatibility(VT102);
2051 insch(term, def(term->esc_args[0], 1));
2052 term->seen_disp_event = TRUE;
2054 case 'P': /* delete chars */
2055 compatibility(VT102);
2056 insch(term, -def(term->esc_args[0], 1));
2057 term->seen_disp_event = TRUE;
2059 case 'c': /* terminal type query */
2060 compatibility(VT100);
2061 /* This is the response for a VT102 */
2063 ldisc_send(term->ldisc, term->id_string,
2064 strlen(term->id_string), 0);
2066 case 'n': /* cursor position query */
2068 if (term->esc_args[0] == 6) {
2070 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2072 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2073 } else if (term->esc_args[0] == 5) {
2074 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2078 case 'h': /* toggle modes to high */
2080 compatibility(VT100);
2083 for (i = 0; i < term->esc_nargs; i++)
2084 toggle_mode(term, term->esc_args[i],
2085 term->esc_query, TRUE);
2090 compatibility(VT100);
2092 if (term->esc_nargs != 1) break;
2093 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2094 term->printing = TRUE;
2095 term->only_printing = !term->esc_query;
2096 term->print_state = 0;
2097 term_print_setup(term);
2098 } else if (term->esc_args[0] == 4 &&
2100 term_print_finish(term);
2104 case 'l': /* toggle modes to low */
2106 compatibility(VT100);
2109 for (i = 0; i < term->esc_nargs; i++)
2110 toggle_mode(term, term->esc_args[i],
2111 term->esc_query, FALSE);
2114 case 'g': /* clear tabs */
2115 compatibility(VT100);
2116 if (term->esc_nargs == 1) {
2117 if (term->esc_args[0] == 0) {
2118 term->tabs[term->curs.x] = FALSE;
2119 } else if (term->esc_args[0] == 3) {
2121 for (i = 0; i < term->cols; i++)
2122 term->tabs[i] = FALSE;
2126 case 'r': /* set scroll margins */
2127 compatibility(VT100);
2128 if (term->esc_nargs <= 2) {
2130 top = def(term->esc_args[0], 1) - 1;
2131 bot = (term->esc_nargs <= 1
2132 || term->esc_args[1] == 0 ?
2134 def(term->esc_args[1], term->rows)) - 1;
2135 if (bot >= term->rows)
2136 bot = term->rows - 1;
2137 /* VTTEST Bug 9 - if region is less than 2 lines
2138 * don't change region.
2140 if (bot - top > 0) {
2145 * I used to think the cursor should be
2146 * placed at the top of the newly marginned
2147 * area. Apparently not: VMS TPU falls over
2150 * Well actually it should for
2153 term->curs.y = (term->dec_om ?
2156 term->seen_disp_event = TRUE;
2160 case 'm': /* set graphics rendition */
2163 * A VT100 without the AVO only had one
2164 * attribute, either underline or
2165 * reverse video depending on the
2166 * cursor type, this was selected by
2170 * This is sometimes DIM, eg on the
2173 * This is sometimes INVIS various ANSI.
2175 * This like 22 disables BOLD, DIM and INVIS
2177 * The ANSI colours appear on any
2178 * terminal that has colour (obviously)
2179 * but the interaction between sgr0 and
2180 * the colours varies but is usually
2181 * related to the background colour
2182 * erase item. The interaction between
2183 * colour attributes and the mono ones
2184 * is also very implementation
2187 * The 39 and 49 attributes are likely
2188 * to be unimplemented.
2191 for (i = 0; i < term->esc_nargs; i++) {
2192 switch (def(term->esc_args[i], 0)) {
2193 case 0: /* restore defaults */
2194 term->curr_attr = ATTR_DEFAULT;
2196 case 1: /* enable bold */
2197 compatibility(VT100AVO);
2198 term->curr_attr |= ATTR_BOLD;
2200 case 21: /* (enable double underline) */
2201 compatibility(OTHER);
2202 case 4: /* enable underline */
2203 compatibility(VT100AVO);
2204 term->curr_attr |= ATTR_UNDER;
2206 case 5: /* enable blink */
2207 compatibility(VT100AVO);
2208 term->curr_attr |= ATTR_BLINK;
2210 case 7: /* enable reverse video */
2211 term->curr_attr |= ATTR_REVERSE;
2213 case 10: /* SCO acs off */
2214 compatibility(SCOANSI);
2215 if (term->cfg.no_remote_charset) break;
2216 term->sco_acs = 0; break;
2217 case 11: /* SCO acs on */
2218 compatibility(SCOANSI);
2219 if (term->cfg.no_remote_charset) break;
2220 term->sco_acs = 1; break;
2221 case 12: /* SCO acs on, |0x80 */
2222 compatibility(SCOANSI);
2223 if (term->cfg.no_remote_charset) break;
2224 term->sco_acs = 2; break;
2225 case 22: /* disable bold */
2226 compatibility2(OTHER, VT220);
2227 term->curr_attr &= ~ATTR_BOLD;
2229 case 24: /* disable underline */
2230 compatibility2(OTHER, VT220);
2231 term->curr_attr &= ~ATTR_UNDER;
2233 case 25: /* disable blink */
2234 compatibility2(OTHER, VT220);
2235 term->curr_attr &= ~ATTR_BLINK;
2237 case 27: /* disable reverse video */
2238 compatibility2(OTHER, VT220);
2239 term->curr_attr &= ~ATTR_REVERSE;
2250 term->curr_attr &= ~ATTR_FGMASK;
2252 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2254 case 39: /* default-foreground */
2255 term->curr_attr &= ~ATTR_FGMASK;
2256 term->curr_attr |= ATTR_DEFFG;
2267 term->curr_attr &= ~ATTR_BGMASK;
2269 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2271 case 49: /* default-background */
2272 term->curr_attr &= ~ATTR_BGMASK;
2273 term->curr_attr |= ATTR_DEFBG;
2278 term->erase_char = (' ' | ATTR_ASCII |
2284 case 's': /* save cursor */
2285 save_cursor(term, TRUE);
2287 case 'u': /* restore cursor */
2288 save_cursor(term, FALSE);
2289 term->seen_disp_event = TRUE;
2291 case 't': /* set page size - ie window height */
2293 * VT340/VT420 sequence DECSLPP, DEC only allows values
2294 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2295 * illegal values (eg first arg 1..9) for window changing
2298 if (term->esc_nargs <= 1
2299 && (term->esc_args[0] < 1 ||
2300 term->esc_args[0] >= 24)) {
2301 compatibility(VT340TEXT);
2302 if (!term->cfg.no_remote_resize)
2303 request_resize(term->frontend, term->cols,
2304 def(term->esc_args[0], 24));
2306 } else if (term->esc_nargs >= 1 &&
2307 term->esc_args[0] >= 1 &&
2308 term->esc_args[0] < 24) {
2309 compatibility(OTHER);
2311 switch (term->esc_args[0]) {
2315 set_iconic(term->frontend, FALSE);
2318 set_iconic(term->frontend, TRUE);
2321 if (term->esc_nargs >= 3) {
2322 if (!term->cfg.no_remote_resize)
2323 move_window(term->frontend,
2324 def(term->esc_args[1], 0),
2325 def(term->esc_args[2], 0));
2329 /* We should resize the window to a given
2330 * size in pixels here, but currently our
2331 * resizing code isn't healthy enough to
2336 set_zorder(term->frontend, TRUE);
2339 /* move to bottom */
2340 set_zorder(term->frontend, FALSE);
2343 refresh_window(term->frontend);
2346 if (term->esc_nargs >= 3) {
2347 if (!term->cfg.no_remote_resize)
2348 request_resize(term->frontend,
2349 def(term->esc_args[2], term->cfg.width),
2350 def(term->esc_args[1], term->cfg.height));
2354 if (term->esc_nargs >= 2)
2355 set_zoomed(term->frontend,
2361 ldisc_send(term->ldisc,
2362 is_iconic(term->frontend) ?
2363 "\033[1t" : "\033[2t", 4, 0);
2367 get_window_pos(term->frontend, &x, &y);
2368 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2369 ldisc_send(term->ldisc, buf, len, 0);
2374 get_window_pixels(term->frontend, &x, &y);
2375 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2376 ldisc_send(term->ldisc, buf, len, 0);
2381 len = sprintf(buf, "\033[8;%d;%dt",
2382 term->rows, term->cols);
2383 ldisc_send(term->ldisc, buf, len, 0);
2388 * Hmmm. Strictly speaking we
2389 * should return `the size of the
2390 * screen in characters', but
2391 * that's not easy: (a) window
2392 * furniture being what it is it's
2393 * hard to compute, and (b) in
2394 * resize-font mode maximising the
2395 * window wouldn't change the
2396 * number of characters. *shrug*. I
2397 * think we'll ignore it for the
2398 * moment and see if anyone
2399 * complains, and then ask them
2400 * what they would like it to do.
2405 p = get_window_title(term->frontend, TRUE);
2407 ldisc_send(term->ldisc, "\033]L", 3, 0);
2408 ldisc_send(term->ldisc, p, len, 0);
2409 ldisc_send(term->ldisc, "\033\\", 2, 0);
2414 p = get_window_title(term->frontend,FALSE);
2416 ldisc_send(term->ldisc, "\033]l", 3, 0);
2417 ldisc_send(term->ldisc, p, len, 0);
2418 ldisc_send(term->ldisc, "\033\\", 2, 0);
2425 compatibility(SCOANSI);
2426 scroll(term, term->marg_t, term->marg_b,
2427 def(term->esc_args[0], 1), TRUE);
2429 term->wrapnext = FALSE;
2430 term->seen_disp_event = TRUE;
2433 compatibility(SCOANSI);
2434 scroll(term, term->marg_t, term->marg_b,
2435 -def(term->esc_args[0], 1), TRUE);
2437 term->wrapnext = FALSE;
2438 term->seen_disp_event = TRUE;
2440 case ANSI('|', '*'):
2441 /* VT420 sequence DECSNLS
2442 * Set number of lines on screen
2443 * VT420 uses VGA like hardware and can support any size in
2444 * reasonable range (24..49 AIUI) with no default specified.
2446 compatibility(VT420);
2447 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2448 if (!term->cfg.no_remote_resize)
2449 request_resize(term->frontend, term->cols,
2450 def(term->esc_args[0],
2455 case ANSI('|', '$'):
2456 /* VT340/VT420 sequence DECSCPP
2457 * Set number of columns per page
2458 * Docs imply range is only 80 or 132, but I'll allow any.
2460 compatibility(VT340TEXT);
2461 if (term->esc_nargs <= 1) {
2462 if (!term->cfg.no_remote_resize)
2463 request_resize(term->frontend,
2464 def(term->esc_args[0],
2465 term->cfg.width), term->rows);
2469 case 'X': /* write N spaces w/o moving cursor */
2470 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2471 compatibility(ANSIMIN);
2473 int n = def(term->esc_args[0], 1);
2475 unsigned long *p = term->cpos;
2476 if (n > term->cols - term->curs.x)
2477 n = term->cols - term->curs.x;
2478 cursplus = term->curs;
2480 check_boundary(term, term->curs.x, term->curs.y);
2481 check_boundary(term, term->curs.x+n, term->curs.y);
2482 check_selection(term, term->curs, cursplus);
2484 *p++ = term->erase_char;
2485 term->seen_disp_event = TRUE;
2488 case 'x': /* report terminal characteristics */
2489 compatibility(VT100);
2492 int i = def(term->esc_args[0], 0);
2493 if (i == 0 || i == 1) {
2494 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2496 ldisc_send(term->ldisc, buf, 20, 0);
2500 case 'Z': /* BackTab for xterm */
2501 compatibility(OTHER);
2503 int i = def(term->esc_args[0], 1);
2504 pos old_curs = term->curs;
2506 for(;i>0 && term->curs.x>0; i--) {
2509 } while (term->curs.x >0 &&
2510 !term->tabs[term->curs.x]);
2513 check_selection(term, old_curs, term->curs);
2516 case ANSI('L', '='):
2517 compatibility(OTHER);
2518 term->use_bce = (term->esc_args[0] <= 0);
2519 term->erase_char = ERASE_CHAR;
2521 term->erase_char = (' ' | ATTR_ASCII |
2523 (ATTR_FGMASK | ATTR_BGMASK)));
2525 case ANSI('E', '='):
2526 compatibility(OTHER);
2527 term->blink_is_real = (term->esc_args[0] >= 1);
2529 case ANSI('p', '"'):
2531 * Allow the host to make this emulator a
2532 * 'perfect' VT102. This first appeared in
2533 * the VT220, but we do need to get back to
2534 * PuTTY mode so I won't check it.
2536 * The arg in 40..42,50 are a PuTTY extension.
2537 * The 2nd arg, 8bit vs 7bit is not checked.
2539 * Setting VT102 mode should also change
2540 * the Fkeys to generate PF* codes as a
2541 * real VT102 has no Fkeys. The VT220 does
2542 * this, F11..F13 become ESC,BS,LF other
2543 * Fkeys send nothing.
2545 * Note ESC c will NOT change this!
2548 switch (term->esc_args[0]) {
2550 term->compatibility_level &= ~TM_VTXXX;
2551 term->compatibility_level |= TM_VT102;
2554 term->compatibility_level &= ~TM_VTXXX;
2555 term->compatibility_level |= TM_VT220;
2559 if (term->esc_args[0] > 60 &&
2560 term->esc_args[0] < 70)
2561 term->compatibility_level |= TM_VTXXX;
2565 term->compatibility_level &= TM_VTXXX;
2568 term->compatibility_level = TM_PUTTY;
2571 term->compatibility_level = TM_SCOANSI;
2575 term->compatibility_level = TM_PUTTY;
2581 /* Change the response to CSI c */
2582 if (term->esc_args[0] == 50) {
2585 strcpy(term->id_string, "\033[?");
2586 for (i = 1; i < term->esc_nargs; i++) {
2588 strcat(term->id_string, ";");
2589 sprintf(lbuf, "%d", term->esc_args[i]);
2590 strcat(term->id_string, lbuf);
2592 strcat(term->id_string, "c");
2595 /* Is this a good idea ?
2596 * Well we should do a soft reset at this point ...
2598 if (!has_compat(VT420) && has_compat(VT100)) {
2599 if (!term->cfg.no_remote_resize) {
2600 if (term->reset_132)
2601 request_resize(132, 24);
2603 request_resize(80, 24);
2611 term->osc_w = FALSE;
2613 case 'P': /* Linux palette sequence */
2614 term->termstate = SEEN_OSC_P;
2615 term->osc_strlen = 0;
2617 case 'R': /* Linux palette reset */
2618 palette_reset(term->frontend);
2619 term_invalidate(term);
2620 term->termstate = TOPLEVEL;
2622 case 'W': /* word-set */
2623 term->termstate = SEEN_OSC_W;
2636 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2640 * Grotty hack to support xterm and DECterm title
2641 * sequences concurrently.
2643 if (term->esc_args[0] == 2) {
2644 term->esc_args[0] = 1;
2647 /* else fall through */
2649 term->termstate = OSC_STRING;
2650 term->osc_strlen = 0;
2655 * This OSC stuff is EVIL. It takes just one character to get into
2656 * sysline mode and it's not initially obvious how to get out.
2657 * So I've added CR and LF as string aborts.
2658 * This shouldn't effect compatibility as I believe embedded
2659 * control characters are supposed to be interpreted (maybe?)
2660 * and they don't display anything useful anyway.
2664 if (c == '\012' || c == '\015') {
2665 term->termstate = TOPLEVEL;
2666 } else if (c == 0234 || c == '\007') {
2668 * These characters terminate the string; ST and BEL
2669 * terminate the sequence and trigger instant
2670 * processing of it, whereas ESC goes back to SEEN_ESC
2671 * mode unless it is followed by \, in which case it is
2672 * synonymous with ST in the first place.
2675 term->termstate = TOPLEVEL;
2676 } else if (c == '\033')
2677 term->termstate = OSC_MAYBE_ST;
2678 else if (term->osc_strlen < OSC_STR_MAX)
2679 term->osc_string[term->osc_strlen++] = c;
2683 int max = (term->osc_strlen == 0 ? 21 : 16);
2685 if (c >= '0' && c <= '9')
2687 else if (c >= 'A' && c <= 'A' + max - 10)
2689 else if (c >= 'a' && c <= 'a' + max - 10)
2692 term->termstate = TOPLEVEL;
2695 term->osc_string[term->osc_strlen++] = val;
2696 if (term->osc_strlen >= 7) {
2697 palette_set(term->frontend, term->osc_string[0],
2698 term->osc_string[1] * 16 + term->osc_string[2],
2699 term->osc_string[3] * 16 + term->osc_string[4],
2700 term->osc_string[5] * 16 + term->osc_string[6]);
2701 term_invalidate(term);
2702 term->termstate = TOPLEVEL;
2718 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2721 term->termstate = OSC_STRING;
2722 term->osc_strlen = 0;
2726 term->termstate = TOPLEVEL;
2727 term->seen_disp_event = TRUE;
2730 move(term, term->curs.x, term->curs.y - 1, 1);
2733 move(term, term->curs.x, term->curs.y + 1, 1);
2736 move(term, term->curs.x + 1, term->curs.y, 1);
2739 move(term, term->curs.x - 1, term->curs.y, 1);
2742 * From the VT100 Manual
2743 * NOTE: The special graphics characters in the VT100
2744 * are different from those in the VT52
2746 * From VT102 manual:
2747 * 137 _ Blank - Same
2748 * 140 ` Reserved - Humm.
2749 * 141 a Solid rectangle - Similar
2750 * 142 b 1/ - Top half of fraction for the
2751 * 143 c 3/ - subscript numbers below.
2754 * 146 f Degrees - Same
2755 * 147 g Plus or minus - Same
2757 * 151 i Ellipsis (dots)
2760 * 154 l Bar at scan 0
2761 * 155 m Bar at scan 1
2762 * 156 n Bar at scan 2
2763 * 157 o Bar at scan 3 - Similar
2764 * 160 p Bar at scan 4 - Similar
2765 * 161 q Bar at scan 5 - Similar
2766 * 162 r Bar at scan 6 - Same
2767 * 163 s Bar at scan 7 - Similar
2782 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2785 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2788 move(term, 0, 0, 0);
2791 if (term->curs.y == 0)
2792 scroll(term, 0, term->rows - 1, -1, TRUE);
2793 else if (term->curs.y > 0)
2796 term->wrapnext = FALSE;
2799 erase_lots(term, FALSE, FALSE, TRUE);
2803 erase_lots(term, TRUE, FALSE, TRUE);
2807 /* XXX Print cursor line */
2810 /* XXX Start controller mode */
2813 /* XXX Stop controller mode */
2817 term->termstate = VT52_Y1;
2821 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2824 term->app_keypad_keys = TRUE;
2827 term->app_keypad_keys = FALSE;
2830 /* XXX This should switch to VT100 mode not current or default
2831 * VT mode. But this will only have effect in a VT220+
2834 term->vt52_mode = FALSE;
2835 term->blink_is_real = term->cfg.blinktext;
2839 /* XXX Enter auto print mode */
2842 /* XXX Exit auto print mode */
2845 /* XXX Print screen */
2851 /* compatibility(ATARI) */
2852 move(term, 0, 0, 0);
2853 erase_lots(term, FALSE, FALSE, TRUE);
2857 /* compatibility(ATARI) */
2858 if (term->curs.y <= term->marg_b)
2859 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2862 /* compatibility(ATARI) */
2863 if (term->curs.y <= term->marg_b)
2864 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2867 /* compatibility(ATARI) */
2868 term->termstate = VT52_FG;
2871 /* compatibility(ATARI) */
2872 term->termstate = VT52_BG;
2875 /* compatibility(ATARI) */
2876 erase_lots(term, FALSE, TRUE, FALSE);
2880 /* compatibility(ATARI) */
2881 term->cursor_on = TRUE;
2884 /* compatibility(ATARI) */
2885 term->cursor_on = FALSE;
2887 /* case 'j': Save cursor position - broken on ST */
2888 /* case 'k': Restore cursor position */
2890 /* compatibility(ATARI) */
2891 erase_lots(term, TRUE, TRUE, TRUE);
2893 term->wrapnext = FALSE;
2897 /* compatibility(ATARI) */
2898 erase_lots(term, TRUE, TRUE, FALSE);
2901 /* compatibility(ATARI) */
2902 term->curr_attr |= ATTR_REVERSE;
2905 /* compatibility(ATARI) */
2906 term->curr_attr &= ~ATTR_REVERSE;
2908 case 'v': /* wrap Autowrap on - Wyse style */
2909 /* compatibility(ATARI) */
2912 case 'w': /* Autowrap off */
2913 /* compatibility(ATARI) */
2918 /* compatibility(OTHER) */
2919 term->vt52_bold = FALSE;
2920 term->curr_attr = ATTR_DEFAULT;
2922 term->erase_char = (' ' | ATTR_ASCII |
2924 (ATTR_FGMASK | ATTR_BGMASK)));
2927 /* compatibility(VI50) */
2928 term->curr_attr |= ATTR_UNDER;
2931 /* compatibility(VI50) */
2932 term->curr_attr &= ~ATTR_UNDER;
2935 /* compatibility(VI50) */
2936 term->vt52_bold = TRUE;
2937 term->curr_attr |= ATTR_BOLD;
2940 /* compatibility(VI50) */
2941 term->vt52_bold = FALSE;
2942 term->curr_attr &= ~ATTR_BOLD;
2948 term->termstate = VT52_Y2;
2949 move(term, term->curs.x, c - ' ', 0);
2952 term->termstate = TOPLEVEL;
2953 move(term, c - ' ', term->curs.y, 0);
2958 term->termstate = TOPLEVEL;
2959 term->curr_attr &= ~ATTR_FGMASK;
2960 term->curr_attr &= ~ATTR_BOLD;
2961 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2962 if ((c & 0x8) || term->vt52_bold)
2963 term->curr_attr |= ATTR_BOLD;
2966 term->erase_char = (' ' | ATTR_ASCII |
2968 (ATTR_FGMASK | ATTR_BGMASK)));
2971 term->termstate = TOPLEVEL;
2972 term->curr_attr &= ~ATTR_BGMASK;
2973 term->curr_attr &= ~ATTR_BLINK;
2974 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2976 /* Note: bold background */
2978 term->curr_attr |= ATTR_BLINK;
2981 term->erase_char = (' ' | ATTR_ASCII |
2983 (ATTR_FGMASK | ATTR_BGMASK)));
2986 default: break; /* placate gcc warning about enum use */
2988 if (term->selstate != NO_SELECTION) {
2989 pos cursplus = term->curs;
2991 check_selection(term, term->curs, cursplus);
2995 term_print_flush(term);
3000 * Compare two lines to determine whether they are sufficiently
3001 * alike to scroll-optimise one to the other. Return the degree of
3004 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3008 for (i = n = 0; i < term->cols; i++)
3009 n += (*a++ == *b++);
3015 * Given a context, update the window. Out of paranoia, we don't
3016 * allow WM_PAINT responses to do scrolling optimisations.
3018 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3020 int i, j, our_curs_y, our_curs_x;
3021 unsigned long rv, cursor;
3024 long cursor_background = ERASE_CHAR;
3025 unsigned long ticks;
3028 * Check the visual bell state.
3030 if (term->in_vbell) {
3031 ticks = GETTICKCOUNT();
3032 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3033 term->in_vbell = FALSE;
3036 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3039 * screen array, disptop, scrtop,
3041 * cfg.blinkpc, blink_is_real, tblinker,
3042 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3045 /* Has the cursor position or type changed ? */
3046 if (term->cursor_on) {
3047 if (term->has_focus) {
3048 if (term->blinker || !term->cfg.blink_cur)
3049 cursor = TATTR_ACTCURS;
3053 cursor = TATTR_PASCURS;
3055 cursor |= TATTR_RIGHTCURS;
3058 our_curs_y = term->curs.y - term->disptop;
3061 * Adjust the cursor position in the case where it's
3062 * resting on the right-hand half of a CJK wide character.
3063 * xterm's behaviour here, which seems adequate to me, is
3064 * to display the cursor covering the _whole_ character,
3065 * exactly as if it were one space to the left.
3067 unsigned long *ldata = lineptr(term->curs.y);
3068 our_curs_x = term->curs.x;
3069 if (our_curs_x > 0 &&
3070 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3074 if (term->dispcurs && (term->curstype != cursor ||
3076 term->disptext + our_curs_y * (term->cols + 1) +
3078 if (term->dispcurs > term->disptext &&
3079 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3080 term->dispcurs[-1] |= ATTR_INVALID;
3081 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3082 term->dispcurs[1] |= ATTR_INVALID;
3083 *term->dispcurs |= ATTR_INVALID;
3086 term->dispcurs = NULL;
3088 /* The normal screen data */
3089 for (i = 0; i < term->rows; i++) {
3090 unsigned long *ldata;
3092 int idx, dirty_line, dirty_run, selected;
3093 unsigned long attr = 0;
3094 int updated_line = 0;
3097 int last_run_dirty = 0;
3099 scrpos.y = i + term->disptop;
3100 ldata = lineptr(scrpos.y);
3101 lattr = (ldata[term->cols] & LATTR_MODE);
3103 idx = i * (term->cols + 1);
3104 dirty_run = dirty_line = (ldata[term->cols] !=
3105 term->disptext[idx + term->cols]);
3106 term->disptext[idx + term->cols] = ldata[term->cols];
3108 for (j = 0; j < term->cols; j++, idx++) {
3109 unsigned long tattr, tchar;
3110 unsigned long *d = ldata + j;
3114 tchar = (*d & (CHAR_MASK | CSET_MASK));
3115 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3116 switch (tchar & CSET_MASK) {
3118 tchar = unitab_line[tchar & 0xFF];
3121 tchar = unitab_xterm[tchar & 0xFF];
3124 tchar = unitab_scoacs[tchar&0xFF];
3127 tattr |= (tchar & CSET_MASK);
3129 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3132 /* Video reversing things */
3133 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3134 if (term->seltype == LEXICOGRAPHIC)
3135 selected = (posle(term->selstart, scrpos) &&
3136 poslt(scrpos, term->selend));
3138 selected = (posPle(term->selstart, scrpos) &&
3139 posPlt(scrpos, term->selend));
3143 ^ (selected ? ATTR_REVERSE : 0));
3145 /* 'Real' blinking ? */
3146 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3147 if (term->has_focus && term->tblinker) {
3149 tattr &= ~CSET_MASK;
3152 tattr &= ~ATTR_BLINK;
3156 * Check the font we'll _probably_ be using to see if
3157 * the character is wide when we don't want it to be.
3159 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3160 if ((tattr & ATTR_WIDE) == 0 &&
3161 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3162 tattr |= ATTR_NARROW;
3163 } else if (term->disptext[idx]&ATTR_NARROW)
3164 tattr |= ATTR_NARROW;
3166 /* Cursor here ? Save the 'background' */
3167 if (i == our_curs_y && j == our_curs_x) {
3168 cursor_background = tattr | tchar;
3169 term->dispcurs = term->disptext + idx;
3172 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3175 break_run = (((tattr ^ attr) & term->attr_mask) ||
3176 j - start >= sizeof(ch));
3178 /* Special hack for VT100 Linedraw glyphs */
3179 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3180 && tchar <= 0xBD) break_run = TRUE;
3182 if (!dbcs_screenfont && !dirty_line) {
3183 if ((tchar | tattr) == term->disptext[idx])
3185 else if (!dirty_run && ccount == 1)
3190 if ((dirty_run || last_run_dirty) && ccount > 0) {
3191 do_text(ctx, start, i, ch, ccount, attr, lattr);
3197 if (dbcs_screenfont)
3198 last_run_dirty = dirty_run;
3199 dirty_run = dirty_line;
3202 if ((tchar | tattr) != term->disptext[idx])
3204 ch[ccount++] = (char) tchar;
3205 term->disptext[idx] = tchar | tattr;
3207 /* If it's a wide char step along to the next one. */
3208 if (tattr & ATTR_WIDE) {
3209 if (++j < term->cols) {
3213 * By construction above, the cursor should not
3214 * be on the right-hand half of this character.
3217 assert(!(i == our_curs_y && j == our_curs_x));
3218 if (term->disptext[idx] != *d)
3220 term->disptext[idx] = *d;
3224 if (dirty_run && ccount > 0) {
3225 do_text(ctx, start, i, ch, ccount, attr, lattr);
3229 /* Cursor on this line ? (and changed) */
3230 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3231 ch[0] = (char) (cursor_background & CHAR_MASK);
3232 attr = (cursor_background & ATTR_MASK) | cursor;
3233 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3234 term->curstype = cursor;
3240 * Flick the switch that says if blinking things should be shown or hidden.
3243 void term_blink(Terminal *term, int flg)
3245 long now, blink_diff;
3247 now = GETTICKCOUNT();
3248 blink_diff = now - term->last_tblink;
3250 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3251 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3252 term->last_tblink = now;
3253 term->tblinker = !term->tblinker;
3258 term->last_blink = now;
3262 blink_diff = now - term->last_blink;
3264 /* Make sure the cursor blinks no faster than system blink rate */
3265 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3268 term->last_blink = now;
3269 term->blinker = !term->blinker;
3273 * Invalidate the whole screen so it will be repainted in full.
3275 void term_invalidate(Terminal *term)
3279 for (i = 0; i < term->rows * (term->cols + 1); i++)
3280 term->disptext[i] = ATTR_INVALID;
3284 * Paint the window in response to a WM_PAINT message.
3286 void term_paint(Terminal *term, Context ctx,
3287 int left, int top, int right, int bottom, int immediately)
3290 if (left < 0) left = 0;
3291 if (top < 0) top = 0;
3292 if (right >= term->cols) right = term->cols-1;
3293 if (bottom >= term->rows) bottom = term->rows-1;
3295 for (i = top; i <= bottom && i < term->rows; i++) {
3296 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3297 LATTR_MODE) == LATTR_NORM)
3298 for (j = left; j <= right && j < term->cols; j++)
3299 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3301 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3302 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3305 /* This should happen soon enough, also for some reason it sometimes
3306 * fails to actually do anything when re-sizing ... painting the wrong
3310 do_paint (term, ctx, FALSE);
3314 * Attempt to scroll the scrollback. The second parameter gives the
3315 * position we want to scroll to; the first is +1 to denote that
3316 * this position is relative to the beginning of the scrollback, -1
3317 * to denote it is relative to the end, and 0 to denote that it is
3318 * relative to the current position.
3320 void term_scroll(Terminal *term, int rel, int where)
3322 int sbtop = -count234(term->scrollback);
3323 #ifdef OPTIMISE_SCROLL
3324 int olddisptop = term->disptop;
3326 #endif /* OPTIMISE_SCROLL */
3328 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3329 if (term->disptop < sbtop)
3330 term->disptop = sbtop;
3331 if (term->disptop > 0)
3334 #ifdef OPTIMISE_SCROLL
3335 shift = (term->disptop - olddisptop);
3336 if (shift < term->rows && shift > -term->rows)
3337 scroll_display(term, 0, term->rows - 1, shift);
3338 #endif /* OPTIMISE_SCROLL */
3342 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3345 wchar_t *wbptr; /* where next char goes within workbuf */
3347 int wblen = 0; /* workbuf len */
3348 int buflen; /* amount of memory allocated to workbuf */
3350 buflen = 5120; /* Default size */
3351 workbuf = smalloc(buflen * sizeof(wchar_t));
3352 wbptr = workbuf; /* start filling here */
3353 old_top_x = top.x; /* needed for rect==1 */
3355 while (poslt(top, bottom)) {
3357 unsigned long *ldata = lineptr(top.y);
3361 * nlpos will point at the maximum position on this line we
3362 * should copy up to. So we start it at the end of the
3366 nlpos.x = term->cols;
3369 * ... move it backwards if there's unused space at the end
3370 * of the line (and also set `nl' if this is the case,
3371 * because in normal selection mode this means we need a
3372 * newline at the end)...
3374 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3375 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3376 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3377 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3378 && poslt(top, nlpos))
3380 if (poslt(nlpos, bottom))
3382 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3383 /* Ignore the last char on the line in a WRAPPED2 line. */
3388 * ... and then clip it to the terminal x coordinate if
3389 * we're doing rectangular selection. (In this case we
3390 * still did the above, so that copying e.g. the right-hand
3391 * column from a table doesn't fill with spaces on the
3395 if (nlpos.x > bottom.x)
3397 nl = (top.y < bottom.y);
3400 while (poslt(top, bottom) && poslt(top, nlpos)) {
3403 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3405 wchar_t cbuf[16], *p;
3406 int uc = (ldata[top.x] & 0xFFFF);
3409 if (uc == UCSWIDE) {
3414 switch (uc & CSET_MASK) {
3416 if (!term->cfg.rawcnp) {
3417 uc = unitab_xterm[uc & 0xFF];
3421 uc = unitab_line[uc & 0xFF];
3424 uc = unitab_scoacs[uc&0xFF];
3427 switch (uc & CSET_MASK) {
3429 uc = unitab_font[uc & 0xFF];
3432 uc = unitab_oemcp[uc & 0xFF];
3436 set = (uc & CSET_MASK);
3437 c = (uc & CHAR_MASK);
3441 if (DIRECT_FONT(uc)) {
3442 if (c >= ' ' && c != 0x7F) {
3446 if (is_dbcs_leadbyte(font_codepage, (BYTE) c)) {
3448 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3449 rv = mb_to_wc(font_codepage, 0, buf, 2, wbuf, 4);
3453 rv = mb_to_wc(font_codepage, 0, buf, 1, wbuf, 4);
3457 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3464 for (p = cbuf; *p; p++) {
3465 /* Enough overhead for trailing NL and nul */
3466 if (wblen >= buflen - 16) {
3469 sizeof(wchar_t) * (buflen += 100));
3470 wbptr = workbuf + wblen;
3479 for (i = 0; i < sel_nl_sz; i++) {
3481 *wbptr++ = sel_nl[i];
3485 top.x = rect ? old_top_x : 0;
3487 #if SELECTION_NUL_TERMINATED
3491 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3492 if (buflen > 0) /* indicates we allocated this buffer */
3496 void term_copyall(Terminal *term)
3499 top.y = -count234(term->scrollback);
3501 clipme(term, top, term->curs, 0);
3505 * The wordness array is mainly for deciding the disposition of the
3506 * US-ASCII characters.
3508 static int wordtype(Terminal *term, int uc)
3511 int start, end, ctype;
3513 static const struct ucsword ucs_words[] = {
3519 0x037e, 0x037e, 1}, /* Greek question mark */
3521 0x0387, 0x0387, 1}, /* Greek ano teleia */
3523 0x055a, 0x055f, 1}, /* Armenian punctuation */
3525 0x0589, 0x0589, 1}, /* Armenian full stop */
3527 0x0700, 0x070d, 1}, /* Syriac punctuation */
3529 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3531 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3533 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3535 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3537 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3539 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3541 0x2000, 0x200a, 0}, /* Various spaces */
3543 0x2070, 0x207f, 2}, /* superscript */
3545 0x2080, 0x208f, 2}, /* subscript */
3547 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3549 0x3000, 0x3000, 0}, /* ideographic space */
3551 0x3001, 0x3020, 1}, /* ideographic punctuation */
3553 0x303f, 0x309f, 3}, /* Hiragana */
3555 0x30a0, 0x30ff, 3}, /* Katakana */
3557 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3559 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3561 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3563 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3565 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3567 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3569 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3571 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3573 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3577 const struct ucsword *wptr;
3579 uc &= (CSET_MASK | CHAR_MASK);
3581 switch (uc & CSET_MASK) {
3583 uc = unitab_xterm[uc & 0xFF];
3586 uc = unitab_line[uc & 0xFF];
3589 uc = unitab_scoacs[uc&0xFF];
3592 switch (uc & CSET_MASK) {
3594 uc = unitab_font[uc & 0xFF];
3597 uc = unitab_oemcp[uc & 0xFF];
3601 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3602 * fail as there's such a thing as a double width space. :-(
3604 if (dbcs_screenfont && font_codepage == line_codepage)
3608 return term->wordness[uc];
3610 for (wptr = ucs_words; wptr->start; wptr++) {
3611 if (uc >= wptr->start && uc <= wptr->end)
3619 * Spread the selection outwards according to the selection mode.
3621 static pos sel_spread_half(Terminal *term, pos p, int dir)
3623 unsigned long *ldata;
3625 int topy = -count234(term->scrollback);
3627 ldata = lineptr(p.y);
3629 switch (term->selmode) {
3632 * In this mode, every character is a separate unit, except
3633 * for runs of spaces at the end of a non-wrapping line.
3635 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3636 unsigned long *q = ldata + term->cols;
3637 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3639 if (q == ldata + term->cols)
3641 if (p.x >= q - ldata)
3642 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3647 * In this mode, the units are maximal runs of characters
3648 * whose `wordness' has the same value.
3650 wvalue = wordtype(term, UCSGET(ldata, p.x));
3653 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3654 term->cols-1 : term->cols);
3655 if (p.x < maxcols-1) {
3656 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3661 if (ldata[term->cols] & LATTR_WRAPPED) {
3662 unsigned long *ldata2;
3663 ldata2 = lineptr(p.y+1);
3664 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3677 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3682 unsigned long *ldata2;
3686 ldata2 = lineptr(p.y-1);
3687 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3688 term->cols-1 : term->cols);
3689 if (ldata2[term->cols] & LATTR_WRAPPED) {
3690 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3705 * In this mode, every line is a unit.
3707 p.x = (dir == -1 ? 0 : term->cols - 1);
3713 static void sel_spread(Terminal *term)
3715 if (term->seltype == LEXICOGRAPHIC) {
3716 term->selstart = sel_spread_half(term, term->selstart, -1);
3717 decpos(term->selend);
3718 term->selend = sel_spread_half(term, term->selend, +1);
3719 incpos(term->selend);
3723 void term_do_paste(Terminal *term)
3728 get_clip(term->frontend, &data, &len);
3729 if (data && len > 0) {
3732 term_seen_key_event(term); /* pasted data counts */
3734 if (term->paste_buffer)
3735 sfree(term->paste_buffer);
3736 term->paste_pos = term->paste_hold = term->paste_len = 0;
3737 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3740 while (p < data + len) {
3741 while (p < data + len &&
3742 !(p <= data + len - sel_nl_sz &&
3743 !memcmp(p, sel_nl, sizeof(sel_nl))))
3748 for (i = 0; i < p - q; i++) {
3749 term->paste_buffer[term->paste_len++] = q[i];
3753 if (p <= data + len - sel_nl_sz &&
3754 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3755 term->paste_buffer[term->paste_len++] = '\015';
3761 /* Assume a small paste will be OK in one go. */
3762 if (term->paste_len < 256) {
3764 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3765 if (term->paste_buffer)
3766 sfree(term->paste_buffer);
3767 term->paste_buffer = 0;
3768 term->paste_pos = term->paste_hold = term->paste_len = 0;
3771 get_clip(term->frontend, NULL, NULL);
3774 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3775 int shift, int ctrl, int alt)
3778 unsigned long *ldata;
3779 int raw_mouse = (term->xterm_mouse &&
3780 !term->cfg.no_mouse_rep &&
3781 !(term->cfg.mouse_override && shift));
3782 int default_seltype;
3786 if (a == MA_DRAG && !raw_mouse)
3787 term_scroll(term, 0, -1);
3789 if (y >= term->rows) {
3791 if (a == MA_DRAG && !raw_mouse)
3792 term_scroll(term, 0, +1);
3801 if (x >= term->cols)
3804 selpoint.y = y + term->disptop;
3806 ldata = lineptr(selpoint.y);
3807 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3811 int encstate = 0, r, c;
3818 encstate = 0x20; /* left button down */
3829 case MBT_WHEEL_DOWN:
3832 default: break; /* placate gcc warning about enum use */
3836 if (term->xterm_mouse == 1)
3842 term->mouse_is_down = 0;
3845 if (term->mouse_is_down == b)
3847 term->mouse_is_down = b;
3849 default: break; /* placate gcc warning about enum use */
3858 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3859 ldisc_send(term->ldisc, abuf, 6, 0);
3864 b = translate_button(term->frontend, b);
3867 * Set the selection type (rectangular or normal) at the start
3868 * of a selection attempt, from the state of Alt.
3870 if (!alt ^ !term->cfg.rect_select)
3871 default_seltype = RECTANGULAR;
3873 default_seltype = LEXICOGRAPHIC;
3875 if (term->selstate == NO_SELECTION) {
3876 term->seltype = default_seltype;
3879 if (b == MBT_SELECT && a == MA_CLICK) {
3881 term->selstate = ABOUT_TO;
3882 term->seltype = default_seltype;
3883 term->selanchor = selpoint;
3884 term->selmode = SM_CHAR;
3885 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3887 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3888 term->selstate = DRAGGING;
3889 term->selstart = term->selanchor = selpoint;
3890 term->selend = term->selstart;
3891 incpos(term->selend);
3893 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3894 (b == MBT_EXTEND && a != MA_RELEASE)) {
3895 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3897 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3898 if (term->seltype == LEXICOGRAPHIC) {
3900 * For normal selection, we extend by moving
3901 * whichever end of the current selection is closer
3904 if (posdiff(selpoint, term->selstart) <
3905 posdiff(term->selend, term->selstart) / 2) {
3906 term->selanchor = term->selend;
3907 decpos(term->selanchor);
3909 term->selanchor = term->selstart;
3913 * For rectangular selection, we have a choice of
3914 * _four_ places to put selanchor and selpoint: the
3915 * four corners of the selection.
3917 if (2*selpoint.x < term->selstart.x + term->selend.x)
3918 term->selanchor.x = term->selend.x-1;
3920 term->selanchor.x = term->selstart.x;
3922 if (2*selpoint.y < term->selstart.y + term->selend.y)
3923 term->selanchor.y = term->selend.y;
3925 term->selanchor.y = term->selstart.y;
3927 term->selstate = DRAGGING;
3929 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3930 term->selanchor = selpoint;
3931 term->selstate = DRAGGING;
3932 if (term->seltype == LEXICOGRAPHIC) {
3934 * For normal selection, we set (selstart,selend) to
3935 * (selpoint,selanchor) in some order.
3937 if (poslt(selpoint, term->selanchor)) {
3938 term->selstart = selpoint;
3939 term->selend = term->selanchor;
3940 incpos(term->selend);
3942 term->selstart = term->selanchor;
3943 term->selend = selpoint;
3944 incpos(term->selend);
3948 * For rectangular selection, we may need to
3949 * interchange x and y coordinates (if the user has
3950 * dragged in the -x and +y directions, or vice versa).
3952 term->selstart.x = min(term->selanchor.x, selpoint.x);
3953 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3954 term->selstart.y = min(term->selanchor.y, selpoint.y);
3955 term->selend.y = max(term->selanchor.y, selpoint.y);
3958 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3959 if (term->selstate == DRAGGING) {
3961 * We've completed a selection. We now transfer the
3962 * data to the clipboard.
3964 clipme(term, term->selstart, term->selend,
3965 (term->seltype == RECTANGULAR));
3966 term->selstate = SELECTED;
3968 term->selstate = NO_SELECTION;
3969 } else if (b == MBT_PASTE
3971 #if MULTICLICK_ONLY_EVENT
3972 || a == MA_2CLK || a == MA_3CLK
3975 request_paste(term->frontend);
3981 void term_nopaste(Terminal *term)
3983 if (term->paste_len == 0)
3985 sfree(term->paste_buffer);
3986 term->paste_buffer = NULL;
3987 term->paste_len = 0;
3990 int term_paste_pending(Terminal *term)
3992 return term->paste_len != 0;
3995 void term_paste(Terminal *term)
3997 long now, paste_diff;
3999 if (term->paste_len == 0)
4002 /* Don't wait forever to paste */
4003 if (term->paste_hold) {
4004 now = GETTICKCOUNT();
4005 paste_diff = now - term->last_paste;
4006 if (paste_diff >= 0 && paste_diff < 450)
4009 term->paste_hold = 0;
4011 while (term->paste_pos < term->paste_len) {
4013 while (n + term->paste_pos < term->paste_len) {
4014 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4018 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4019 term->paste_pos += n;
4021 if (term->paste_pos < term->paste_len) {
4022 term->paste_hold = 1;
4026 sfree(term->paste_buffer);
4027 term->paste_buffer = NULL;
4028 term->paste_len = 0;
4031 static void deselect(Terminal *term)
4033 term->selstate = NO_SELECTION;
4034 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4037 void term_deselect(Terminal *term)
4043 int term_ldisc(Terminal *term, int option)
4045 if (option == LD_ECHO)
4046 return term->term_echoing;
4047 if (option == LD_EDIT)
4048 return term->term_editing;
4053 * from_backend(), to get data from the backend for the terminal.
4055 int from_backend(void *vterm, int is_stderr, char *data, int len)
4057 Terminal *term = (Terminal *)vterm;
4061 bufchain_add(&term->inbuf, data, len);
4064 * term_out() always completely empties inbuf. Therefore,
4065 * there's no reason at all to return anything other than zero
4066 * from this function, because there _can't_ be a question of
4067 * the remote side needing to wait until term_out() has cleared
4070 * This is a slightly suboptimal way to deal with SSH2 - in
4071 * principle, the window mechanism would allow us to continue
4072 * to accept data on forwarded ports and X connections even
4073 * while the terminal processing was going slowly - but we
4074 * can't do the 100% right thing without moving the terminal
4075 * processing into a separate thread, and that might hurt
4076 * portability. So we manage stdout buffering the old SSH1 way:
4077 * if the terminal processing goes slowly, the whole SSH
4078 * connection stops accepting data until it's ready.
4080 * In practice, I can't imagine this causing serious trouble.
4085 void term_provide_logctx(Terminal *term, void *logctx)
4087 term->logctx = logctx;