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 * Get the number of lines in the scrollback.
115 static int sblines(Terminal *term)
117 int sblines = count234(term->scrollback);
118 if (term->cfg.erase_to_scrollback &&
119 term->alt_which && term->alt_screen) {
120 sblines += term->alt_sblines;
126 * Retrieve a line of the screen or of the scrollback, according to
127 * whether the y coordinate is non-negative or negative
130 static unsigned long *lineptr(Terminal *term, int y, int lineno)
132 unsigned long *line, *newline;
137 whichtree = term->screen;
141 if (term->cfg.erase_to_scrollback &&
142 term->alt_which && term->alt_screen) {
143 altlines = term->alt_sblines;
146 whichtree = term->scrollback;
147 treeindex = y + altlines + count234(term->scrollback);
149 whichtree = term->alt_screen;
150 treeindex = y + term->alt_sblines;
151 /* treeindex = y + count234(term->alt_screen); */
154 line = index234(whichtree, treeindex);
156 /* We assume that we don't screw up and retrieve something out of range. */
157 assert(line != NULL);
159 newline = resizeline(line, term->cols);
160 if (newline != line) {
161 delpos234(whichtree, treeindex);
162 addpos234(whichtree, newline, treeindex);
169 #define lineptr(x) lineptr(term,x,__LINE__)
172 * Set up power-on settings for the terminal.
174 static void power_on(Terminal *term)
176 term->curs.x = term->curs.y = 0;
177 term->alt_x = term->alt_y = 0;
178 term->savecurs.x = term->savecurs.y = 0;
179 term->alt_t = term->marg_t = 0;
180 if (term->rows != -1)
181 term->alt_b = term->marg_b = term->rows - 1;
183 term->alt_b = term->marg_b = 0;
184 if (term->cols != -1) {
186 for (i = 0; i < term->cols; i++)
187 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
189 term->alt_om = term->dec_om = term->cfg.dec_om;
190 term->alt_ins = term->insert = FALSE;
191 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
192 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
193 term->alt_cset = term->cset = term->save_cset = 0;
194 term->alt_utf = term->utf = term->save_utf = 0;
196 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
197 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
199 term->in_vbell = FALSE;
201 term->big_cursor = 0;
202 term->save_attr = term->curr_attr = ATTR_DEFAULT;
203 term->term_editing = term->term_echoing = FALSE;
204 term->app_cursor_keys = term->cfg.app_cursor;
205 term->app_keypad_keys = term->cfg.app_keypad;
206 term->use_bce = term->cfg.bce;
207 term->blink_is_real = term->cfg.blinktext;
208 term->erase_char = ERASE_CHAR;
210 term_print_finish(term);
213 for (i = 0; i < 256; i++)
214 term->wordness[i] = term->cfg.wordness[i];
217 swap_screen(term, 1, FALSE, FALSE);
218 erase_lots(term, FALSE, TRUE, TRUE);
219 swap_screen(term, 0, FALSE, FALSE);
220 erase_lots(term, FALSE, TRUE, TRUE);
225 * Force a screen update.
227 void term_update(Terminal *term)
230 ctx = get_ctx(term->frontend);
232 int need_sbar_update = term->seen_disp_event;
233 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
234 term->disptop = 0; /* return to main screen */
235 term->seen_disp_event = 0;
236 need_sbar_update = TRUE;
238 if (need_sbar_update)
240 do_paint(term, ctx, TRUE);
241 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
247 * Called from front end when a keypress occurs, to trigger
248 * anything magical that needs to happen in that situation.
250 void term_seen_key_event(Terminal *term)
253 * On any keypress, clear the bell overload mechanism
254 * completely, on the grounds that large numbers of
255 * beeps coming from deliberate key action are likely
256 * to be intended (e.g. beeps from filename completion
257 * blocking repeatedly).
259 term->beep_overloaded = FALSE;
260 while (term->beephead) {
261 struct beeptime *tmp = term->beephead;
262 term->beephead = tmp->next;
265 term->beeptail = NULL;
269 * Reset the scrollback on keypress, if we're doing that.
271 if (term->cfg.scroll_on_key) {
272 term->disptop = 0; /* return to main screen */
273 term->seen_disp_event = 1;
278 * Same as power_on(), but an external function.
280 void term_pwron(Terminal *term)
283 if (term->ldisc) /* cause ldisc to notice changes */
284 ldisc_send(term->ldisc, NULL, 0, 0);
292 * When the user reconfigures us, we need to check the forbidden-
293 * alternate-screen config option, disable raw mouse mode if the
294 * user has disabled mouse reporting, and abandon a print job if
295 * the user has disabled printing.
297 void term_reconfig(Terminal *term, Config *cfg)
300 * Before adopting the new config, check all those terminal
301 * settings which control power-on defaults; and if they've
302 * changed, we will modify the current state as well as the
303 * default one. The full list is: Auto wrap mode, DEC Origin
304 * Mode, BCE, blinking text, character classes.
306 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
309 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
310 reset_decom = (term->cfg.dec_om != cfg->dec_om);
311 reset_bce = (term->cfg.bce != cfg->bce);
312 reset_blink = (term->cfg.blinktext != cfg->blinktext);
314 for (i = 0; i < lenof(term->cfg.wordness); i++)
315 if (term->cfg.wordness[i] != cfg->wordness[i])
318 term->cfg = *cfg; /* STRUCTURE COPY */
321 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
323 term->alt_om = term->dec_om = term->cfg.dec_om;
325 term->use_bce = term->cfg.bce;
327 term->blink_is_real = term->cfg.blinktext;
329 for (i = 0; i < 256; i++)
330 term->wordness[i] = term->cfg.wordness[i];
332 if (term->cfg.no_alt_screen)
333 swap_screen(term, 0, FALSE, FALSE);
334 if (term->cfg.no_mouse_rep) {
335 term->xterm_mouse = 0;
336 set_raw_mouse_mode(term->frontend, 0);
338 if (term->cfg.no_remote_charset) {
339 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
340 term->sco_acs = term->alt_sco_acs = 0;
343 if (!*term->cfg.printer) {
344 term_print_finish(term);
349 * Clear the scrollback.
351 void term_clrsb(Terminal *term)
355 while ((line = delpos234(term->scrollback, 0)) != NULL) {
358 term->alt_sblines = 0;
363 * Initialise the terminal.
365 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
371 * Allocate a new Terminal structure and initialise the fields
374 term = smalloc(sizeof(Terminal));
375 term->frontend = frontend;
376 term->ucsdata = ucsdata;
377 term->cfg = *mycfg; /* STRUCTURE COPY */
379 term->compatibility_level = TM_PUTTY;
380 strcpy(term->id_string, "\033[?6c");
381 term->last_blink = term->last_tblink = 0;
382 term->paste_buffer = NULL;
384 term->last_paste = 0;
385 bufchain_init(&term->inbuf);
386 bufchain_init(&term->printer_buf);
387 term->printing = term->only_printing = FALSE;
388 term->print_job = NULL;
389 term->vt52_mode = FALSE;
390 term->cr_lf_return = FALSE;
391 term->seen_disp_event = FALSE;
392 term->xterm_mouse = term->mouse_is_down = FALSE;
393 term->reset_132 = FALSE;
394 term->blinker = term->tblinker = 0;
396 term->repeat_off = FALSE;
397 term->termstate = TOPLEVEL;
398 term->selstate = NO_SELECTION;
401 term->screen = term->alt_screen = term->scrollback = NULL;
402 term->alt_sblines = 0;
404 term->disptext = term->dispcurs = NULL;
407 term->rows = term->cols = -1;
409 term->beephead = term->beeptail = NULL;
410 #ifdef OPTIMISE_SCROLL
411 term->scrollhead = term->scrolltail = NULL;
412 #endif /* OPTIMISE_SCROLL */
414 term->lastbeep = FALSE;
415 term->beep_overloaded = FALSE;
416 term->attr_mask = 0xffffffff;
417 term->resize_fn = NULL;
418 term->resize_ctx = NULL;
423 void term_free(Terminal *term)
426 struct beeptime *beep;
428 while ((line = delpos234(term->scrollback, 0)) != NULL)
430 freetree234(term->scrollback);
431 while ((line = delpos234(term->screen, 0)) != NULL)
433 freetree234(term->screen);
434 while ((line = delpos234(term->alt_screen, 0)) != NULL)
436 freetree234(term->alt_screen);
437 sfree(term->disptext);
438 while (term->beephead) {
439 beep = term->beephead;
440 term->beephead = beep->next;
443 bufchain_clear(&term->inbuf);
445 printer_finish_job(term->print_job);
446 bufchain_clear(&term->printer_buf);
447 sfree(term->paste_buffer);
452 * Set up the terminal for a given size.
454 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
457 unsigned long *newdisp, *line;
460 int save_alt_which = term->alt_which;
462 if (newrows == term->rows && newcols == term->cols &&
463 newsavelines == term->savelines)
464 return; /* nothing to do */
467 swap_screen(term, 0, FALSE, FALSE);
469 term->alt_t = term->marg_t = 0;
470 term->alt_b = term->marg_b = newrows - 1;
472 if (term->rows == -1) {
473 term->scrollback = newtree234(NULL);
474 term->screen = newtree234(NULL);
479 * Resize the screen and scrollback. We only need to shift
480 * lines around within our data structures, because lineptr()
481 * will take care of resizing each individual line if
484 * - If the new screen and the old screen differ in length, we
485 * must shunt some lines in from the scrollback or out to
488 * - If doing that fails to provide us with enough material to
489 * fill the new screen (i.e. the number of rows needed in
490 * the new screen exceeds the total number in the previous
491 * screen+scrollback), we must invent some blank lines to
494 * - Then, if the new scrollback length is less than the
495 * amount of scrollback we actually have, we must throw some
498 sblen = count234(term->scrollback);
499 /* Do this loop to expand the screen if newrows > rows */
500 for (i = term->rows; i < newrows; i++) {
502 line = delpos234(term->scrollback, --sblen);
504 line = smalloc(TSIZE * (newcols + 2));
506 for (j = 0; j < newcols; j++)
507 line[j + 1] = ERASE_CHAR;
508 line[newcols + 1] = LATTR_NORM;
510 addpos234(term->screen, line, 0);
512 /* Do this loop to shrink the screen if newrows < rows */
513 for (i = newrows; i < term->rows; i++) {
514 line = delpos234(term->screen, 0);
515 addpos234(term->scrollback, line, sblen++);
517 assert(count234(term->screen) == newrows);
518 while (sblen > newsavelines) {
519 line = delpos234(term->scrollback, 0);
523 assert(count234(term->scrollback) <= newsavelines);
526 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
527 for (i = 0; i < newrows * (newcols + 1); i++)
528 newdisp[i] = ATTR_INVALID;
529 sfree(term->disptext);
530 term->disptext = newdisp;
531 term->dispcurs = NULL;
533 newalt = newtree234(NULL);
534 for (i = 0; i < newrows; i++) {
535 line = smalloc(TSIZE * (newcols + 2));
537 for (j = 0; j < newcols; j++)
538 line[j + 1] = term->erase_char;
539 line[newcols + 1] = LATTR_NORM;
540 addpos234(newalt, line, i);
542 if (term->alt_screen) {
543 while (NULL != (line = delpos234(term->alt_screen, 0)))
545 freetree234(term->alt_screen);
547 term->alt_screen = newalt;
548 term->alt_sblines = 0;
550 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
553 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
554 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
558 term->curs.y += newrows - term->rows;
559 if (term->curs.y < 0)
561 if (term->curs.y >= newrows)
562 term->curs.y = newrows - 1;
563 if (term->curs.x >= newcols)
564 term->curs.x = newcols - 1;
565 term->alt_x = term->alt_y = 0;
566 term->wrapnext = term->alt_wnext = FALSE;
568 term->rows = newrows;
569 term->cols = newcols;
570 term->savelines = newsavelines;
573 swap_screen(term, save_alt_which, FALSE, FALSE);
578 term->resize_fn(term->resize_ctx, term->cols, term->rows);
582 * Hand a function and context pointer to the terminal which it can
583 * use to notify a back end of resizes.
585 void term_provide_resize_fn(Terminal *term,
586 void (*resize_fn)(void *, int, int),
589 term->resize_fn = resize_fn;
590 term->resize_ctx = resize_ctx;
591 if (term->cols > 0 && term->rows > 0)
592 resize_fn(resize_ctx, term->cols, term->rows);
595 /* Find the bottom line on the screen that has any content.
596 * If only the top line has content, returns 0.
597 * If no lines have content, return -1.
599 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
602 for (i = count234(screen) - 1; i >= 0; i--) {
603 unsigned long *line = index234(screen, i);
606 for (j = 0; j < cols; j++) {
607 if (line[j + 1] != term->erase_char) break;
609 if (j != cols) break;
615 * Swap screens. If `reset' is TRUE and we have been asked to
616 * switch to the alternate screen, we must bring most of its
617 * configuration from the main screen and erase the contents of the
618 * alternate screen completely. (This is even true if we're already
619 * on it! Blame xterm.)
621 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
627 reset = FALSE; /* do no weird resetting if which==0 */
629 if (which != term->alt_which) {
630 term->alt_which = which;
632 ttr = term->alt_screen;
633 term->alt_screen = term->screen;
635 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
637 if (!reset && !keep_cur_pos)
638 term->curs.x = term->alt_x;
641 if (!reset && !keep_cur_pos)
642 term->curs.y = term->alt_y;
645 if (!reset) term->marg_t = term->alt_t;
648 if (!reset) term->marg_b = term->alt_b;
651 if (!reset) term->dec_om = term->alt_om;
654 if (!reset) term->wrap = term->alt_wrap;
657 if (!reset) term->wrapnext = term->alt_wnext;
660 if (!reset) term->insert = term->alt_ins;
663 if (!reset) term->cset = term->alt_cset;
666 if (!reset) term->utf = term->alt_utf;
669 if (!reset) term->sco_acs = term->alt_sco_acs;
670 term->alt_sco_acs = t;
673 if (reset && term->screen) {
675 * Yes, this _is_ supposed to honour background-colour-erase.
677 erase_lots(term, FALSE, TRUE, TRUE);
681 * This might not be possible if we're called during
689 * Update the scroll bar.
691 static void update_sbar(Terminal *term)
693 int nscroll = sblines(term);
694 set_sbar(term->frontend, nscroll + term->rows,
695 nscroll + term->disptop, term->rows);
699 * Check whether the region bounded by the two pointers intersects
700 * the scroll region, and de-select the on-screen selection if so.
702 static void check_selection(Terminal *term, pos from, pos to)
704 if (poslt(from, term->selend) && poslt(term->selstart, to))
709 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
710 * for backward.) `sb' is TRUE if the scrolling is permitted to
711 * affect the scrollback buffer.
713 * NB this function invalidates all pointers into lines of the
714 * screen data structures. In particular, you MUST call fix_cpos
715 * after calling scroll() and before doing anything else that
716 * uses the cpos shortcut pointer.
718 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
720 unsigned long *line, *line2;
721 int i, seltop, olddisptop, shift;
723 if (topline != 0 || term->alt_which != 0)
726 olddisptop = term->disptop;
730 line = delpos234(term->screen, botline);
731 line = resizeline(line, term->cols);
732 for (i = 0; i < term->cols; i++)
733 line[i + 1] = term->erase_char;
734 line[term->cols + 1] = 0;
735 addpos234(term->screen, line, topline);
737 if (term->selstart.y >= topline && term->selstart.y <= botline) {
739 if (term->selstart.y > botline) {
740 term->selstart.y = botline;
741 term->selstart.x = 0;
744 if (term->selend.y >= topline && term->selend.y <= botline) {
746 if (term->selend.y > botline) {
747 term->selend.y = botline;
756 line = delpos234(term->screen, topline);
757 if (sb && term->savelines > 0) {
758 int sblen = sblines(term);
760 * We must add this line to the scrollback. We'll
761 * remove a line from the top of the scrollback to
762 * replace it, or allocate a new one if the
763 * scrollback isn't full.
765 if (sblen == term->savelines) {
766 sblen--, line2 = delpos234(term->scrollback, 0);
768 line2 = smalloc(TSIZE * (term->cols + 2));
769 line2[0] = term->cols;
771 addpos234(term->scrollback, line, sblen);
775 * If the user is currently looking at part of the
776 * scrollback, and they haven't enabled any options
777 * that are going to reset the scrollback as a
778 * result of this movement, then the chances are
779 * they'd like to keep looking at the same line. So
780 * we move their viewpoint at the same rate as the
781 * scroll, at least until their viewpoint hits the
782 * top end of the scrollback buffer, at which point
783 * we don't have the choice any more.
785 * Thanks to Jan Holmen Holsten for the idea and
786 * initial implementation.
788 if (term->disptop > -term->savelines && term->disptop < 0)
791 line = resizeline(line, term->cols);
792 for (i = 0; i < term->cols; i++)
793 line[i + 1] = term->erase_char;
794 line[term->cols + 1] = LATTR_NORM;
795 addpos234(term->screen, line, botline);
798 * If the selection endpoints move into the scrollback,
799 * we keep them moving until they hit the top. However,
800 * of course, if the line _hasn't_ moved into the
801 * scrollback then we don't do this, and cut them off
802 * at the top of the scroll region.
804 * This applies to selstart and selend (for an existing
805 * selection), and also selanchor (for one being
806 * selected as we speak).
808 seltop = sb ? -term->savelines : topline;
810 if (term->selstart.y >= seltop &&
811 term->selstart.y <= botline) {
813 if (term->selstart.y < seltop) {
814 term->selstart.y = seltop;
815 term->selstart.x = 0;
818 if (term->selend.y >= seltop && term->selend.y <= botline) {
820 if (term->selend.y < seltop) {
821 term->selend.y = seltop;
825 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
827 if (term->selanchor.y < seltop) {
828 term->selanchor.y = seltop;
829 term->selanchor.x = 0;
836 #ifdef OPTIMISE_SCROLL
837 shift += term->disptop - olddisptop;
838 if (shift < term->rows && shift > -term->rows && shift != 0)
839 scroll_display(term, topline, botline, shift);
840 #endif /* OPTIMISE_SCROLL */
843 #ifdef OPTIMISE_SCROLL
845 * Add a scroll of a region on the screen into the pending scroll list.
846 * `lines' is +ve for scrolling forward, -ve for backward.
848 * If the scroll is on the same area as the last scroll in the list,
851 static void save_scroll(Terminal *term, int topline, int botline, int lines)
853 struct scrollregion *newscroll;
854 if (term->scrolltail &&
855 term->scrolltail->topline == topline &&
856 term->scrolltail->botline == botline) {
857 term->scrolltail->lines += lines;
859 newscroll = smalloc(sizeof(struct scrollregion));
860 newscroll->topline = topline;
861 newscroll->botline = botline;
862 newscroll->lines = lines;
863 newscroll->next = NULL;
865 if (!term->scrollhead)
866 term->scrollhead = newscroll;
868 term->scrolltail->next = newscroll;
869 term->scrolltail = newscroll;
874 * Scroll the physical display, and our conception of it in disptext.
876 static void scroll_display(Terminal *term, int topline, int botline, int lines)
878 unsigned long *start, *end;
879 int distance, size, i;
881 start = term->disptext + topline * (term->cols + 1);
882 end = term->disptext + (botline + 1) * (term->cols + 1);
883 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
884 size = end - start - distance;
886 memmove(start, start + distance, size * TSIZE);
887 if (term->dispcurs >= start + distance &&
888 term->dispcurs <= start + distance + size)
889 term->dispcurs -= distance;
890 for (i = 0; i < distance; i++)
891 (start + size)[i] |= ATTR_INVALID;
893 memmove(start + distance, start, size * TSIZE);
894 if (term->dispcurs >= start && term->dispcurs <= start + size)
895 term->dispcurs += distance;
896 for (i = 0; i < distance; i++)
897 start[i] |= ATTR_INVALID;
899 save_scroll(term, topline, botline, lines);
901 #endif /* OPTIMISE_SCROLL */
904 * Move the cursor to a given position, clipping at boundaries. We
905 * may or may not want to clip at the scroll margin: marg_clip is 0
906 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
907 * even _being_ outside the margins.
909 static void move(Terminal *term, int x, int y, int marg_clip)
916 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
919 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
930 term->wrapnext = FALSE;
934 * Save or restore the cursor and SGR mode.
936 static void save_cursor(Terminal *term, int save)
939 term->savecurs = term->curs;
940 term->save_attr = term->curr_attr;
941 term->save_cset = term->cset;
942 term->save_utf = term->utf;
943 term->save_wnext = term->wrapnext;
944 term->save_csattr = term->cset_attr[term->cset];
945 term->save_sco_acs = term->sco_acs;
947 term->curs = term->savecurs;
948 /* Make sure the window hasn't shrunk since the save */
949 if (term->curs.x >= term->cols)
950 term->curs.x = term->cols - 1;
951 if (term->curs.y >= term->rows)
952 term->curs.y = term->rows - 1;
954 term->curr_attr = term->save_attr;
955 term->cset = term->save_cset;
956 term->utf = term->save_utf;
957 term->wrapnext = term->save_wnext;
959 * wrapnext might reset to False if the x position is no
960 * longer at the rightmost edge.
962 if (term->wrapnext && term->curs.x < term->cols-1)
963 term->wrapnext = FALSE;
964 term->cset_attr[term->cset] = term->save_csattr;
965 term->sco_acs = term->save_sco_acs;
968 term->erase_char = (' ' | ATTR_ASCII |
970 (ATTR_FGMASK | ATTR_BGMASK)));
975 * This function is called before doing _anything_ which affects
976 * only part of a line of text. It is used to mark the boundary
977 * between two character positions, and it indicates that some sort
978 * of effect is going to happen on only one side of that boundary.
980 * The effect of this function is to check whether a CJK
981 * double-width character is straddling the boundary, and to remove
982 * it and replace it with two spaces if so. (Of course, one or
983 * other of those spaces is then likely to be replaced with
984 * something else again, as a result of whatever happens next.)
986 * Also, if the boundary is at the right-hand _edge_ of the screen,
987 * it implies something deliberate is being done to the rightmost
988 * column position; hence we must clear LATTR_WRAPPED2.
990 * The input to the function is the coordinates of the _second_
991 * character of the pair.
993 static void check_boundary(Terminal *term, int x, int y)
995 unsigned long *ldata;
997 /* Validate input coordinates, just in case. */
998 if (x == 0 || x > term->cols)
1002 if (x == term->cols) {
1003 ldata[x] &= ~LATTR_WRAPPED2;
1005 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
1006 ldata[x-1] = ldata[x] =
1007 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
1013 * Erase a large portion of the screen: the whole screen, or the
1014 * whole line, or parts thereof.
1016 static void erase_lots(Terminal *term,
1017 int line_only, int from_begin, int to_end)
1023 start.y = term->curs.y;
1025 end.y = term->curs.y + 1;
1027 erase_lattr = FALSE;
1042 if (!from_begin || !to_end)
1043 check_boundary(term, term->curs.x, term->curs.y);
1044 check_selection(term, start, end);
1046 /* Clear screen also forces a full window redraw, just in case. */
1047 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1048 term_invalidate(term);
1050 if (term->cfg.erase_to_scrollback &&
1051 start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr) {
1052 /* If it's a whole number of lines, starting at the top, and
1053 * we're fully erasing them, erase by scrolling and keep the
1054 * lines in the scrollback. */
1055 int scrolllines = end.y;
1056 if (end.y == term->rows) {
1057 /* Shrink until we find a non-empty row.*/
1058 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1060 if (scrolllines > 0)
1061 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1064 unsigned long *ldata = lineptr(start.y);
1065 while (poslt(start, end)) {
1066 if (start.x == term->cols) {
1068 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1070 ldata[start.x] = LATTR_NORM;
1072 ldata[start.x] = term->erase_char;
1074 if (incpos(start) && start.y < term->rows)
1075 ldata = lineptr(start.y);
1081 * Insert or delete characters within the current line. n is +ve if
1082 * insertion is desired, and -ve for deletion.
1084 static void insch(Terminal *term, int n)
1086 int dir = (n < 0 ? -1 : +1);
1089 unsigned long *ldata;
1091 n = (n < 0 ? -n : n);
1092 if (n > term->cols - term->curs.x)
1093 n = term->cols - term->curs.x;
1094 m = term->cols - term->curs.x - n;
1095 cursplus.y = term->curs.y;
1096 cursplus.x = term->curs.x + n;
1097 check_selection(term, term->curs, cursplus);
1098 check_boundary(term, term->curs.x, term->curs.y);
1100 check_boundary(term, term->curs.x + n, term->curs.y);
1101 ldata = lineptr(term->curs.y);
1103 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1105 ldata[term->curs.x + m++] = term->erase_char;
1107 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1109 ldata[term->curs.x + n] = term->erase_char;
1114 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1115 * whether the mode is a DEC private one or a normal one.)
1117 static void toggle_mode(Terminal *term, int mode, int query, int state)
1119 unsigned long ticks;
1123 case 1: /* application cursor keys */
1124 term->app_cursor_keys = state;
1126 case 2: /* VT52 mode */
1127 term->vt52_mode = !state;
1128 if (term->vt52_mode) {
1129 term->blink_is_real = FALSE;
1130 term->vt52_bold = FALSE;
1132 term->blink_is_real = term->cfg.blinktext;
1135 case 3: /* 80/132 columns */
1137 if (!term->cfg.no_remote_resize)
1138 request_resize(term->frontend, state ? 132 : 80, term->rows);
1139 term->reset_132 = state;
1141 case 5: /* reverse video */
1143 * Toggle reverse video. If we receive an OFF within the
1144 * visual bell timeout period after an ON, we trigger an
1145 * effective visual bell, so that ESC[?5hESC[?5l will
1146 * always be an actually _visible_ visual bell.
1148 ticks = GETTICKCOUNT();
1149 /* turn off a previous vbell to avoid inconsistencies */
1150 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1151 term->in_vbell = FALSE;
1152 if (term->rvideo && !state && /* we're turning it off... */
1153 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1154 /* If there's no vbell timeout already, or this one lasts
1155 * longer, replace vbell_timeout with ours. */
1156 if (!term->in_vbell ||
1157 (term->rvbell_startpoint - term->vbell_startpoint <
1159 term->vbell_startpoint = term->rvbell_startpoint;
1160 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1161 } else if (!term->rvideo && state) {
1162 /* This is an ON, so we notice the time and save it. */
1163 term->rvbell_startpoint = ticks;
1165 term->rvideo = state;
1166 term->seen_disp_event = TRUE;
1170 case 6: /* DEC origin mode */
1171 term->dec_om = state;
1173 case 7: /* auto wrap */
1176 case 8: /* auto key repeat */
1177 term->repeat_off = !state;
1179 case 10: /* set local edit mode */
1180 term->term_editing = state;
1181 if (term->ldisc) /* cause ldisc to notice changes */
1182 ldisc_send(term->ldisc, NULL, 0, 0);
1184 case 25: /* enable/disable cursor */
1185 compatibility2(OTHER, VT220);
1186 term->cursor_on = state;
1187 term->seen_disp_event = TRUE;
1189 case 47: /* alternate screen */
1190 compatibility(OTHER);
1192 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1195 case 1000: /* xterm mouse 1 */
1196 term->xterm_mouse = state ? 1 : 0;
1197 set_raw_mouse_mode(term->frontend, state);
1199 case 1002: /* xterm mouse 2 */
1200 term->xterm_mouse = state ? 2 : 0;
1201 set_raw_mouse_mode(term->frontend, state);
1203 case 1047: /* alternate screen */
1204 compatibility(OTHER);
1206 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1209 case 1048: /* save/restore cursor */
1210 save_cursor(term, state);
1211 if (!state) term->seen_disp_event = TRUE;
1213 case 1049: /* cursor & alternate screen */
1215 save_cursor(term, state);
1216 if (!state) term->seen_disp_event = TRUE;
1217 compatibility(OTHER);
1219 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1221 save_cursor(term, state);
1226 case 4: /* set insert mode */
1227 compatibility(VT102);
1228 term->insert = state;
1230 case 12: /* set echo mode */
1231 term->term_echoing = !state;
1232 if (term->ldisc) /* cause ldisc to notice changes */
1233 ldisc_send(term->ldisc, NULL, 0, 0);
1235 case 20: /* Return sends ... */
1236 term->cr_lf_return = state;
1238 case 34: /* Make cursor BIG */
1239 compatibility2(OTHER, VT220);
1240 term->big_cursor = !state;
1245 * Process an OSC sequence: set window title or icon name.
1247 static void do_osc(Terminal *term)
1250 while (term->osc_strlen--)
1251 term->wordness[(unsigned char)
1252 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1254 term->osc_string[term->osc_strlen] = '\0';
1255 switch (term->esc_args[0]) {
1258 if (!term->cfg.no_remote_wintitle)
1259 set_icon(term->frontend, term->osc_string);
1260 if (term->esc_args[0] == 1)
1262 /* fall through: parameter 0 means set both */
1265 if (!term->cfg.no_remote_wintitle)
1266 set_title(term->frontend, term->osc_string);
1273 * ANSI printing routines.
1275 static void term_print_setup(Terminal *term)
1277 bufchain_clear(&term->printer_buf);
1278 term->print_job = printer_start_job(term->cfg.printer);
1280 static void term_print_flush(Terminal *term)
1285 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1286 bufchain_prefix(&term->printer_buf, &data, &len);
1289 printer_job_data(term->print_job, data, len);
1290 bufchain_consume(&term->printer_buf, len);
1293 static void term_print_finish(Terminal *term)
1299 if (!term->printing && !term->only_printing)
1300 return; /* we need do nothing */
1302 term_print_flush(term);
1303 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1304 bufchain_prefix(&term->printer_buf, &data, &len);
1306 if (c == '\033' || c == '\233') {
1307 bufchain_consume(&term->printer_buf, size);
1310 printer_job_data(term->print_job, &c, 1);
1311 bufchain_consume(&term->printer_buf, 1);
1314 printer_finish_job(term->print_job);
1315 term->print_job = NULL;
1316 term->printing = term->only_printing = FALSE;
1320 * Remove everything currently in `inbuf' and stick it up on the
1321 * in-memory display. There's a big state machine in here to
1322 * process escape sequences...
1324 void term_out(Terminal *term)
1327 unsigned char localbuf[256], *chars;
1332 chars = NULL; /* placate compiler warnings */
1333 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1337 bufchain_prefix(&term->inbuf, &ret, &nchars);
1338 if (nchars > sizeof(localbuf))
1339 nchars = sizeof(localbuf);
1340 memcpy(localbuf, ret, nchars);
1341 bufchain_consume(&term->inbuf, nchars);
1343 assert(chars != NULL);
1349 * Optionally log the session traffic to a file. Useful for
1350 * debugging and possibly also useful for actual logging.
1352 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1353 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1359 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1360 * be able to display 8-bit characters, but I'll let that go 'cause
1365 * If we're printing, add the character to the printer
1368 if (term->printing) {
1369 bufchain_add(&term->printer_buf, &c, 1);
1372 * If we're in print-only mode, we use a much simpler
1373 * state machine designed only to recognise the ESC[4i
1374 * termination sequence.
1376 if (term->only_printing) {
1378 term->print_state = 1;
1379 else if (c == (unsigned char)'\233')
1380 term->print_state = 2;
1381 else if (c == '[' && term->print_state == 1)
1382 term->print_state = 2;
1383 else if (c == '4' && term->print_state == 2)
1384 term->print_state = 3;
1385 else if (c == 'i' && term->print_state == 3)
1386 term->print_state = 4;
1388 term->print_state = 0;
1389 if (term->print_state == 4) {
1390 term_print_finish(term);
1396 /* First see about all those translations. */
1397 if (term->termstate == TOPLEVEL) {
1399 switch (term->utf_state) {
1402 /* UTF-8 must be stateless so we ignore iso2022. */
1403 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1404 c = term->ucsdata->unitab_ctrl[c];
1405 else c = ((unsigned char)c) | ATTR_ASCII;
1407 } else if ((c & 0xe0) == 0xc0) {
1408 term->utf_size = term->utf_state = 1;
1409 term->utf_char = (c & 0x1f);
1410 } else if ((c & 0xf0) == 0xe0) {
1411 term->utf_size = term->utf_state = 2;
1412 term->utf_char = (c & 0x0f);
1413 } else if ((c & 0xf8) == 0xf0) {
1414 term->utf_size = term->utf_state = 3;
1415 term->utf_char = (c & 0x07);
1416 } else if ((c & 0xfc) == 0xf8) {
1417 term->utf_size = term->utf_state = 4;
1418 term->utf_char = (c & 0x03);
1419 } else if ((c & 0xfe) == 0xfc) {
1420 term->utf_size = term->utf_state = 5;
1421 term->utf_char = (c & 0x01);
1432 if ((c & 0xC0) != 0x80) {
1435 term->utf_state = 0;
1438 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1439 if (--term->utf_state)
1444 /* Is somebody trying to be evil! */
1446 (c < 0x800 && term->utf_size >= 2) ||
1447 (c < 0x10000 && term->utf_size >= 3) ||
1448 (c < 0x200000 && term->utf_size >= 4) ||
1449 (c < 0x4000000 && term->utf_size >= 5))
1452 /* Unicode line separator and paragraph separator are CR-LF */
1453 if (c == 0x2028 || c == 0x2029)
1456 /* High controls are probably a Baaad idea too. */
1460 /* The UTF-16 surrogates are not nice either. */
1461 /* The standard give the option of decoding these:
1462 * I don't want to! */
1463 if (c >= 0xD800 && c < 0xE000)
1466 /* ISO 10646 characters now limited to UTF-16 range. */
1470 /* This is currently a TagPhobic application.. */
1471 if (c >= 0xE0000 && c <= 0xE007F)
1474 /* U+FEFF is best seen as a null. */
1477 /* But U+FFFE is an error. */
1478 if (c == 0xFFFE || c == 0xFFFF)
1481 /* Oops this is a 16bit implementation */
1486 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1487 else if(term->sco_acs &&
1488 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1490 if (term->sco_acs == 2) c |= 0x80;
1493 switch (term->cset_attr[term->cset]) {
1495 * Linedraw characters are different from 'ESC ( B'
1496 * only for a small range. For ones outside that
1497 * range, make sure we use the same font as well as
1498 * the same encoding.
1501 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1502 c = term->ucsdata->unitab_ctrl[c];
1504 c = ((unsigned char) c) | ATTR_LINEDRW;
1508 /* If UK-ASCII, make the '#' a LineDraw Pound */
1510 c = '}' | ATTR_LINEDRW;
1513 /*FALLTHROUGH*/ case ATTR_ASCII:
1514 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1515 c = term->ucsdata->unitab_ctrl[c];
1517 c = ((unsigned char) c) | ATTR_ASCII;
1520 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1526 /* How about C1 controls ? */
1527 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1528 !term->vt52_mode && has_compat(VT220)) {
1529 term->termstate = SEEN_ESC;
1530 term->esc_query = FALSE;
1531 c = '@' + (c & 0x1F);
1534 /* Or the GL control. */
1535 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1536 if (term->curs.x && !term->wrapnext)
1538 term->wrapnext = FALSE;
1540 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1541 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1543 /* Or normal C0 controls. */
1544 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1546 case '\005': /* terminal type query */
1547 /* Strictly speaking this is VT100 but a VT100 defaults to
1548 * no response. Other terminals respond at their option.
1550 * Don't put a CR in the default string as this tends to
1551 * upset some weird software.
1553 * An xterm returns "xterm" (5 characters)
1555 compatibility(ANSIMIN);
1557 char abuf[256], *s, *d;
1559 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1561 if (*s >= 'a' && *s <= 'z')
1562 *d++ = (*s - ('a' - 1));
1563 else if ((*s >= '@' && *s <= '_') ||
1564 *s == '?' || (*s & 0x80))
1569 } else if (*s == '^') {
1574 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1580 struct beeptime *newbeep;
1581 unsigned long ticks;
1583 ticks = GETTICKCOUNT();
1585 if (!term->beep_overloaded) {
1586 newbeep = smalloc(sizeof(struct beeptime));
1587 newbeep->ticks = ticks;
1588 newbeep->next = NULL;
1589 if (!term->beephead)
1590 term->beephead = newbeep;
1592 term->beeptail->next = newbeep;
1593 term->beeptail = newbeep;
1598 * Throw out any beeps that happened more than
1601 while (term->beephead &&
1602 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1603 struct beeptime *tmp = term->beephead;
1604 term->beephead = tmp->next;
1606 if (!term->beephead)
1607 term->beeptail = NULL;
1611 if (term->cfg.bellovl && term->beep_overloaded &&
1612 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1614 * If we're currently overloaded and the
1615 * last beep was more than s seconds ago,
1616 * leave overload mode.
1618 term->beep_overloaded = FALSE;
1619 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1620 term->nbeeps >= term->cfg.bellovl_n) {
1622 * Now, if we have n or more beeps
1623 * remaining in the queue, go into overload
1626 term->beep_overloaded = TRUE;
1628 term->lastbeep = ticks;
1631 * Perform an actual beep if we're not overloaded.
1633 if (!term->cfg.bellovl || !term->beep_overloaded) {
1634 beep(term->frontend, term->cfg.beep);
1635 if (term->cfg.beep == BELL_VISUAL) {
1636 term->in_vbell = TRUE;
1637 term->vbell_startpoint = ticks;
1645 if (term->curs.x == 0 &&
1646 (term->curs.y == 0 || term->wrap == 0))
1648 else if (term->curs.x == 0 && term->curs.y > 0)
1649 term->curs.x = term->cols - 1, term->curs.y--;
1650 else if (term->wrapnext)
1651 term->wrapnext = FALSE;
1655 term->seen_disp_event = TRUE;
1658 compatibility(VT100);
1662 compatibility(VT100);
1666 if (term->vt52_mode)
1667 term->termstate = VT52_ESC;
1669 compatibility(ANSIMIN);
1670 term->termstate = SEEN_ESC;
1671 term->esc_query = FALSE;
1676 term->wrapnext = FALSE;
1678 term->seen_disp_event = TRUE;
1679 term->paste_hold = 0;
1681 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1684 if (has_compat(SCOANSI)) {
1685 move(term, 0, 0, 0);
1686 erase_lots(term, FALSE, FALSE, TRUE);
1688 term->wrapnext = FALSE;
1689 term->seen_disp_event = 1;
1693 compatibility(VT100);
1695 if (term->curs.y == term->marg_b)
1696 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1697 else if (term->curs.y < term->rows - 1)
1699 if (term->cfg.lfhascr)
1702 term->wrapnext = FALSE;
1703 term->seen_disp_event = 1;
1704 term->paste_hold = 0;
1706 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1710 pos old_curs = term->curs;
1711 unsigned long *ldata = lineptr(term->curs.y);
1715 } while (term->curs.x < term->cols - 1 &&
1716 !term->tabs[term->curs.x]);
1718 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1719 if (term->curs.x >= term->cols / 2)
1720 term->curs.x = term->cols / 2 - 1;
1722 if (term->curs.x >= term->cols)
1723 term->curs.x = term->cols - 1;
1727 check_selection(term, old_curs, term->curs);
1729 term->seen_disp_event = TRUE;
1733 switch (term->termstate) {
1735 /* Only graphic characters get this far;
1736 * ctrls are stripped above */
1737 if (term->wrapnext && term->wrap) {
1738 term->cpos[1] |= LATTR_WRAPPED;
1739 if (term->curs.y == term->marg_b)
1740 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1741 else if (term->curs.y < term->rows - 1)
1745 term->wrapnext = FALSE;
1749 if (term->selstate != NO_SELECTION) {
1750 pos cursplus = term->curs;
1752 check_selection(term, term->curs, cursplus);
1754 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1756 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1762 width = wcwidth((wchar_t) c);
1766 * If we're about to display a double-width
1767 * character starting in the rightmost
1768 * column, then we do something special
1769 * instead. We must print a space in the
1770 * last column of the screen, then wrap;
1771 * and we also set LATTR_WRAPPED2 which
1772 * instructs subsequent cut-and-pasting not
1773 * only to splice this line to the one
1774 * after it, but to ignore the space in the
1775 * last character position as well.
1776 * (Because what was actually output to the
1777 * terminal was presumably just a sequence
1778 * of CJK characters, and we don't want a
1779 * space to be pasted in the middle of
1780 * those just because they had the
1781 * misfortune to start in the wrong parity
1782 * column. xterm concurs.)
1784 check_boundary(term, term->curs.x, term->curs.y);
1785 check_boundary(term, term->curs.x+2, term->curs.y);
1786 if (term->curs.x == term->cols-1) {
1787 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1788 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1789 if (term->curs.y == term->marg_b)
1790 scroll(term, term->marg_t, term->marg_b,
1792 else if (term->curs.y < term->rows - 1)
1796 /* Now we must check_boundary again, of course. */
1797 check_boundary(term, term->curs.x, term->curs.y);
1798 check_boundary(term, term->curs.x+2, term->curs.y);
1800 *term->cpos++ = c | term->curr_attr;
1801 *term->cpos++ = UCSWIDE | term->curr_attr;
1805 check_boundary(term, term->curs.x, term->curs.y);
1806 check_boundary(term, term->curs.x+1, term->curs.y);
1807 *term->cpos++ = c | term->curr_attr;
1814 if (term->curs.x == term->cols) {
1817 term->wrapnext = TRUE;
1818 if (term->wrap && term->vt52_mode) {
1819 term->cpos[1] |= LATTR_WRAPPED;
1820 if (term->curs.y == term->marg_b)
1821 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1822 else if (term->curs.y < term->rows - 1)
1826 term->wrapnext = FALSE;
1829 term->seen_disp_event = 1;
1834 * This state is virtually identical to SEEN_ESC, with the
1835 * exception that we have an OSC sequence in the pipeline,
1836 * and _if_ we see a backslash, we process it.
1840 term->termstate = TOPLEVEL;
1843 /* else fall through */
1845 if (c >= ' ' && c <= '/') {
1846 if (term->esc_query)
1847 term->esc_query = -1;
1849 term->esc_query = c;
1852 term->termstate = TOPLEVEL;
1853 switch (ANSI(c, term->esc_query)) {
1854 case '[': /* enter CSI mode */
1855 term->termstate = SEEN_CSI;
1856 term->esc_nargs = 1;
1857 term->esc_args[0] = ARG_DEFAULT;
1858 term->esc_query = FALSE;
1860 case ']': /* xterm escape sequences */
1861 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1862 compatibility(OTHER);
1863 term->termstate = SEEN_OSC;
1864 term->esc_args[0] = 0;
1866 case '7': /* save cursor */
1867 compatibility(VT100);
1868 save_cursor(term, TRUE);
1870 case '8': /* restore cursor */
1871 compatibility(VT100);
1872 save_cursor(term, FALSE);
1873 term->seen_disp_event = TRUE;
1876 compatibility(VT100);
1877 term->app_keypad_keys = TRUE;
1880 compatibility(VT100);
1881 term->app_keypad_keys = FALSE;
1883 case 'D': /* exactly equivalent to LF */
1884 compatibility(VT100);
1885 if (term->curs.y == term->marg_b)
1886 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1887 else if (term->curs.y < term->rows - 1)
1890 term->wrapnext = FALSE;
1891 term->seen_disp_event = TRUE;
1893 case 'E': /* exactly equivalent to CR-LF */
1894 compatibility(VT100);
1896 if (term->curs.y == term->marg_b)
1897 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1898 else if (term->curs.y < term->rows - 1)
1901 term->wrapnext = FALSE;
1902 term->seen_disp_event = TRUE;
1904 case 'M': /* reverse index - backwards LF */
1905 compatibility(VT100);
1906 if (term->curs.y == term->marg_t)
1907 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1908 else if (term->curs.y > 0)
1911 term->wrapnext = FALSE;
1912 term->seen_disp_event = TRUE;
1914 case 'Z': /* terminal type query */
1915 compatibility(VT100);
1917 ldisc_send(term->ldisc, term->id_string,
1918 strlen(term->id_string), 0);
1920 case 'c': /* restore power-on settings */
1921 compatibility(VT100);
1923 if (term->ldisc) /* cause ldisc to notice changes */
1924 ldisc_send(term->ldisc, NULL, 0, 0);
1925 if (term->reset_132) {
1926 if (!term->cfg.no_remote_resize)
1927 request_resize(term->frontend, 80, term->rows);
1928 term->reset_132 = 0;
1932 term->seen_disp_event = TRUE;
1934 case 'H': /* set a tab */
1935 compatibility(VT100);
1936 term->tabs[term->curs.x] = TRUE;
1939 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1940 compatibility(VT100);
1942 unsigned long *ldata;
1946 for (i = 0; i < term->rows; i++) {
1948 for (j = 0; j < term->cols; j++)
1949 ldata[j] = ATTR_DEFAULT | 'E';
1950 ldata[term->cols] = 0;
1953 term->seen_disp_event = TRUE;
1954 scrtop.x = scrtop.y = 0;
1956 scrbot.y = term->rows;
1957 check_selection(term, scrtop, scrbot);
1961 case ANSI('3', '#'):
1962 case ANSI('4', '#'):
1963 case ANSI('5', '#'):
1964 case ANSI('6', '#'):
1965 compatibility(VT100);
1967 unsigned long nlattr;
1968 unsigned long *ldata;
1969 switch (ANSI(c, term->esc_query)) {
1970 case ANSI('3', '#'):
1973 case ANSI('4', '#'):
1976 case ANSI('5', '#'):
1977 nlattr = LATTR_NORM;
1979 default: /* spiritually case ANSI('6', '#'): */
1980 nlattr = LATTR_WIDE;
1983 ldata = lineptr(term->curs.y);
1984 ldata[term->cols] &= ~LATTR_MODE;
1985 ldata[term->cols] |= nlattr;
1989 case ANSI('A', '('):
1990 compatibility(VT100);
1991 if (!term->cfg.no_remote_charset)
1992 term->cset_attr[0] = ATTR_GBCHR;
1994 case ANSI('B', '('):
1995 compatibility(VT100);
1996 if (!term->cfg.no_remote_charset)
1997 term->cset_attr[0] = ATTR_ASCII;
1999 case ANSI('0', '('):
2000 compatibility(VT100);
2001 if (!term->cfg.no_remote_charset)
2002 term->cset_attr[0] = ATTR_LINEDRW;
2004 case ANSI('U', '('):
2005 compatibility(OTHER);
2006 if (!term->cfg.no_remote_charset)
2007 term->cset_attr[0] = ATTR_SCOACS;
2010 case ANSI('A', ')'):
2011 compatibility(VT100);
2012 if (!term->cfg.no_remote_charset)
2013 term->cset_attr[1] = ATTR_GBCHR;
2015 case ANSI('B', ')'):
2016 compatibility(VT100);
2017 if (!term->cfg.no_remote_charset)
2018 term->cset_attr[1] = ATTR_ASCII;
2020 case ANSI('0', ')'):
2021 compatibility(VT100);
2022 if (!term->cfg.no_remote_charset)
2023 term->cset_attr[1] = ATTR_LINEDRW;
2025 case ANSI('U', ')'):
2026 compatibility(OTHER);
2027 if (!term->cfg.no_remote_charset)
2028 term->cset_attr[1] = ATTR_SCOACS;
2031 case ANSI('8', '%'): /* Old Linux code */
2032 case ANSI('G', '%'):
2033 compatibility(OTHER);
2034 if (!term->cfg.no_remote_charset)
2037 case ANSI('@', '%'):
2038 compatibility(OTHER);
2039 if (!term->cfg.no_remote_charset)
2045 term->termstate = TOPLEVEL; /* default */
2047 if (term->esc_nargs <= ARGS_MAX) {
2048 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2049 term->esc_args[term->esc_nargs - 1] = 0;
2050 term->esc_args[term->esc_nargs - 1] =
2051 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2053 term->termstate = SEEN_CSI;
2054 } else if (c == ';') {
2055 if (++term->esc_nargs <= ARGS_MAX)
2056 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2057 term->termstate = SEEN_CSI;
2058 } else if (c < '@') {
2059 if (term->esc_query)
2060 term->esc_query = -1;
2062 term->esc_query = TRUE;
2064 term->esc_query = c;
2065 term->termstate = SEEN_CSI;
2067 switch (ANSI(c, term->esc_query)) {
2068 case 'A': /* move up N lines */
2069 move(term, term->curs.x,
2070 term->curs.y - def(term->esc_args[0], 1), 1);
2071 term->seen_disp_event = TRUE;
2073 case 'e': /* move down N lines */
2074 compatibility(ANSI);
2077 move(term, term->curs.x,
2078 term->curs.y + def(term->esc_args[0], 1), 1);
2079 term->seen_disp_event = TRUE;
2081 case ANSI('c', '>'): /* report xterm version */
2082 compatibility(OTHER);
2083 /* this reports xterm version 136 so that VIM can
2084 use the drag messages from the mouse reporting */
2086 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2088 case 'a': /* move right N cols */
2089 compatibility(ANSI);
2092 move(term, term->curs.x + def(term->esc_args[0], 1),
2094 term->seen_disp_event = TRUE;
2096 case 'D': /* move left N cols */
2097 move(term, term->curs.x - def(term->esc_args[0], 1),
2099 term->seen_disp_event = TRUE;
2101 case 'E': /* move down N lines and CR */
2102 compatibility(ANSI);
2104 term->curs.y + def(term->esc_args[0], 1), 1);
2105 term->seen_disp_event = TRUE;
2107 case 'F': /* move up N lines and CR */
2108 compatibility(ANSI);
2110 term->curs.y - def(term->esc_args[0], 1), 1);
2111 term->seen_disp_event = TRUE;
2114 case '`': /* set horizontal posn */
2115 compatibility(ANSI);
2116 move(term, def(term->esc_args[0], 1) - 1,
2118 term->seen_disp_event = TRUE;
2120 case 'd': /* set vertical posn */
2121 compatibility(ANSI);
2122 move(term, term->curs.x,
2123 ((term->dec_om ? term->marg_t : 0) +
2124 def(term->esc_args[0], 1) - 1),
2125 (term->dec_om ? 2 : 0));
2126 term->seen_disp_event = TRUE;
2129 case 'f': /* set horz and vert posns at once */
2130 if (term->esc_nargs < 2)
2131 term->esc_args[1] = ARG_DEFAULT;
2132 move(term, def(term->esc_args[1], 1) - 1,
2133 ((term->dec_om ? term->marg_t : 0) +
2134 def(term->esc_args[0], 1) - 1),
2135 (term->dec_om ? 2 : 0));
2136 term->seen_disp_event = TRUE;
2138 case 'J': /* erase screen or parts of it */
2140 unsigned int i = def(term->esc_args[0], 0) + 1;
2143 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2146 term->seen_disp_event = TRUE;
2148 case 'K': /* erase line or parts of it */
2150 unsigned int i = def(term->esc_args[0], 0) + 1;
2153 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2155 term->seen_disp_event = TRUE;
2157 case 'L': /* insert lines */
2158 compatibility(VT102);
2159 if (term->curs.y <= term->marg_b)
2160 scroll(term, term->curs.y, term->marg_b,
2161 -def(term->esc_args[0], 1), FALSE);
2163 term->seen_disp_event = TRUE;
2165 case 'M': /* delete lines */
2166 compatibility(VT102);
2167 if (term->curs.y <= term->marg_b)
2168 scroll(term, term->curs.y, term->marg_b,
2169 def(term->esc_args[0], 1),
2172 term->seen_disp_event = TRUE;
2174 case '@': /* insert chars */
2175 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2176 compatibility(VT102);
2177 insch(term, def(term->esc_args[0], 1));
2178 term->seen_disp_event = TRUE;
2180 case 'P': /* delete chars */
2181 compatibility(VT102);
2182 insch(term, -def(term->esc_args[0], 1));
2183 term->seen_disp_event = TRUE;
2185 case 'c': /* terminal type query */
2186 compatibility(VT100);
2187 /* This is the response for a VT102 */
2189 ldisc_send(term->ldisc, term->id_string,
2190 strlen(term->id_string), 0);
2192 case 'n': /* cursor position query */
2194 if (term->esc_args[0] == 6) {
2196 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2198 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2199 } else if (term->esc_args[0] == 5) {
2200 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2204 case 'h': /* toggle modes to high */
2206 compatibility(VT100);
2209 for (i = 0; i < term->esc_nargs; i++)
2210 toggle_mode(term, term->esc_args[i],
2211 term->esc_query, TRUE);
2216 compatibility(VT100);
2218 if (term->esc_nargs != 1) break;
2219 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2220 term->printing = TRUE;
2221 term->only_printing = !term->esc_query;
2222 term->print_state = 0;
2223 term_print_setup(term);
2224 } else if (term->esc_args[0] == 4 &&
2226 term_print_finish(term);
2230 case 'l': /* toggle modes to low */
2232 compatibility(VT100);
2235 for (i = 0; i < term->esc_nargs; i++)
2236 toggle_mode(term, term->esc_args[i],
2237 term->esc_query, FALSE);
2240 case 'g': /* clear tabs */
2241 compatibility(VT100);
2242 if (term->esc_nargs == 1) {
2243 if (term->esc_args[0] == 0) {
2244 term->tabs[term->curs.x] = FALSE;
2245 } else if (term->esc_args[0] == 3) {
2247 for (i = 0; i < term->cols; i++)
2248 term->tabs[i] = FALSE;
2252 case 'r': /* set scroll margins */
2253 compatibility(VT100);
2254 if (term->esc_nargs <= 2) {
2256 top = def(term->esc_args[0], 1) - 1;
2257 bot = (term->esc_nargs <= 1
2258 || term->esc_args[1] == 0 ?
2260 def(term->esc_args[1], term->rows)) - 1;
2261 if (bot >= term->rows)
2262 bot = term->rows - 1;
2263 /* VTTEST Bug 9 - if region is less than 2 lines
2264 * don't change region.
2266 if (bot - top > 0) {
2271 * I used to think the cursor should be
2272 * placed at the top of the newly marginned
2273 * area. Apparently not: VMS TPU falls over
2276 * Well actually it should for
2279 term->curs.y = (term->dec_om ?
2282 term->seen_disp_event = TRUE;
2286 case 'm': /* set graphics rendition */
2289 * A VT100 without the AVO only had one
2290 * attribute, either underline or
2291 * reverse video depending on the
2292 * cursor type, this was selected by
2296 * This is sometimes DIM, eg on the
2299 * This is sometimes INVIS various ANSI.
2301 * This like 22 disables BOLD, DIM and INVIS
2303 * The ANSI colours appear on any
2304 * terminal that has colour (obviously)
2305 * but the interaction between sgr0 and
2306 * the colours varies but is usually
2307 * related to the background colour
2308 * erase item. The interaction between
2309 * colour attributes and the mono ones
2310 * is also very implementation
2313 * The 39 and 49 attributes are likely
2314 * to be unimplemented.
2317 for (i = 0; i < term->esc_nargs; i++) {
2318 switch (def(term->esc_args[i], 0)) {
2319 case 0: /* restore defaults */
2320 term->curr_attr = ATTR_DEFAULT;
2322 case 1: /* enable bold */
2323 compatibility(VT100AVO);
2324 term->curr_attr |= ATTR_BOLD;
2326 case 21: /* (enable double underline) */
2327 compatibility(OTHER);
2328 case 4: /* enable underline */
2329 compatibility(VT100AVO);
2330 term->curr_attr |= ATTR_UNDER;
2332 case 5: /* enable blink */
2333 compatibility(VT100AVO);
2334 term->curr_attr |= ATTR_BLINK;
2336 case 7: /* enable reverse video */
2337 term->curr_attr |= ATTR_REVERSE;
2339 case 10: /* SCO acs off */
2340 compatibility(SCOANSI);
2341 if (term->cfg.no_remote_charset) break;
2342 term->sco_acs = 0; break;
2343 case 11: /* SCO acs on */
2344 compatibility(SCOANSI);
2345 if (term->cfg.no_remote_charset) break;
2346 term->sco_acs = 1; break;
2347 case 12: /* SCO acs on, |0x80 */
2348 compatibility(SCOANSI);
2349 if (term->cfg.no_remote_charset) break;
2350 term->sco_acs = 2; break;
2351 case 22: /* disable bold */
2352 compatibility2(OTHER, VT220);
2353 term->curr_attr &= ~ATTR_BOLD;
2355 case 24: /* disable underline */
2356 compatibility2(OTHER, VT220);
2357 term->curr_attr &= ~ATTR_UNDER;
2359 case 25: /* disable blink */
2360 compatibility2(OTHER, VT220);
2361 term->curr_attr &= ~ATTR_BLINK;
2363 case 27: /* disable reverse video */
2364 compatibility2(OTHER, VT220);
2365 term->curr_attr &= ~ATTR_REVERSE;
2376 term->curr_attr &= ~ATTR_FGMASK;
2378 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2388 /* xterm-style bright foreground */
2389 term->curr_attr &= ~ATTR_FGMASK;
2391 ((term->esc_args[i] - 90 + 16)
2394 case 39: /* default-foreground */
2395 term->curr_attr &= ~ATTR_FGMASK;
2396 term->curr_attr |= ATTR_DEFFG;
2407 term->curr_attr &= ~ATTR_BGMASK;
2409 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2419 /* xterm-style bright background */
2420 term->curr_attr &= ~ATTR_BGMASK;
2422 ((term->esc_args[i] - 100 + 16)
2425 case 49: /* default-background */
2426 term->curr_attr &= ~ATTR_BGMASK;
2427 term->curr_attr |= ATTR_DEFBG;
2432 term->erase_char = (' ' | ATTR_ASCII |
2438 case 's': /* save cursor */
2439 save_cursor(term, TRUE);
2441 case 'u': /* restore cursor */
2442 save_cursor(term, FALSE);
2443 term->seen_disp_event = TRUE;
2445 case 't': /* set page size - ie window height */
2447 * VT340/VT420 sequence DECSLPP, DEC only allows values
2448 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2449 * illegal values (eg first arg 1..9) for window changing
2452 if (term->esc_nargs <= 1
2453 && (term->esc_args[0] < 1 ||
2454 term->esc_args[0] >= 24)) {
2455 compatibility(VT340TEXT);
2456 if (!term->cfg.no_remote_resize)
2457 request_resize(term->frontend, term->cols,
2458 def(term->esc_args[0], 24));
2460 } else if (term->esc_nargs >= 1 &&
2461 term->esc_args[0] >= 1 &&
2462 term->esc_args[0] < 24) {
2463 compatibility(OTHER);
2465 switch (term->esc_args[0]) {
2469 set_iconic(term->frontend, FALSE);
2472 set_iconic(term->frontend, TRUE);
2475 if (term->esc_nargs >= 3) {
2476 if (!term->cfg.no_remote_resize)
2477 move_window(term->frontend,
2478 def(term->esc_args[1], 0),
2479 def(term->esc_args[2], 0));
2483 /* We should resize the window to a given
2484 * size in pixels here, but currently our
2485 * resizing code isn't healthy enough to
2490 set_zorder(term->frontend, TRUE);
2493 /* move to bottom */
2494 set_zorder(term->frontend, FALSE);
2497 refresh_window(term->frontend);
2500 if (term->esc_nargs >= 3) {
2501 if (!term->cfg.no_remote_resize)
2502 request_resize(term->frontend,
2503 def(term->esc_args[2], term->cfg.width),
2504 def(term->esc_args[1], term->cfg.height));
2508 if (term->esc_nargs >= 2)
2509 set_zoomed(term->frontend,
2515 ldisc_send(term->ldisc,
2516 is_iconic(term->frontend) ?
2517 "\033[1t" : "\033[2t", 4, 0);
2521 get_window_pos(term->frontend, &x, &y);
2522 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2523 ldisc_send(term->ldisc, buf, len, 0);
2528 get_window_pixels(term->frontend, &x, &y);
2529 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2530 ldisc_send(term->ldisc, buf, len, 0);
2535 len = sprintf(buf, "\033[8;%d;%dt",
2536 term->rows, term->cols);
2537 ldisc_send(term->ldisc, buf, len, 0);
2542 * Hmmm. Strictly speaking we
2543 * should return `the size of the
2544 * screen in characters', but
2545 * that's not easy: (a) window
2546 * furniture being what it is it's
2547 * hard to compute, and (b) in
2548 * resize-font mode maximising the
2549 * window wouldn't change the
2550 * number of characters. *shrug*. I
2551 * think we'll ignore it for the
2552 * moment and see if anyone
2553 * complains, and then ask them
2554 * what they would like it to do.
2559 p = get_window_title(term->frontend, TRUE);
2561 ldisc_send(term->ldisc, "\033]L", 3, 0);
2562 ldisc_send(term->ldisc, p, len, 0);
2563 ldisc_send(term->ldisc, "\033\\", 2, 0);
2568 p = get_window_title(term->frontend,FALSE);
2570 ldisc_send(term->ldisc, "\033]l", 3, 0);
2571 ldisc_send(term->ldisc, p, len, 0);
2572 ldisc_send(term->ldisc, "\033\\", 2, 0);
2579 compatibility(SCOANSI);
2580 scroll(term, term->marg_t, term->marg_b,
2581 def(term->esc_args[0], 1), TRUE);
2583 term->wrapnext = FALSE;
2584 term->seen_disp_event = TRUE;
2587 compatibility(SCOANSI);
2588 scroll(term, term->marg_t, term->marg_b,
2589 -def(term->esc_args[0], 1), TRUE);
2591 term->wrapnext = FALSE;
2592 term->seen_disp_event = TRUE;
2594 case ANSI('|', '*'):
2595 /* VT420 sequence DECSNLS
2596 * Set number of lines on screen
2597 * VT420 uses VGA like hardware and can support any size in
2598 * reasonable range (24..49 AIUI) with no default specified.
2600 compatibility(VT420);
2601 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2602 if (!term->cfg.no_remote_resize)
2603 request_resize(term->frontend, term->cols,
2604 def(term->esc_args[0],
2609 case ANSI('|', '$'):
2610 /* VT340/VT420 sequence DECSCPP
2611 * Set number of columns per page
2612 * Docs imply range is only 80 or 132, but I'll allow any.
2614 compatibility(VT340TEXT);
2615 if (term->esc_nargs <= 1) {
2616 if (!term->cfg.no_remote_resize)
2617 request_resize(term->frontend,
2618 def(term->esc_args[0],
2619 term->cfg.width), term->rows);
2623 case 'X': /* write N spaces w/o moving cursor */
2624 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2625 compatibility(ANSIMIN);
2627 int n = def(term->esc_args[0], 1);
2629 unsigned long *p = term->cpos;
2630 if (n > term->cols - term->curs.x)
2631 n = term->cols - term->curs.x;
2632 cursplus = term->curs;
2634 check_boundary(term, term->curs.x, term->curs.y);
2635 check_boundary(term, term->curs.x+n, term->curs.y);
2636 check_selection(term, term->curs, cursplus);
2638 *p++ = term->erase_char;
2639 term->seen_disp_event = TRUE;
2642 case 'x': /* report terminal characteristics */
2643 compatibility(VT100);
2646 int i = def(term->esc_args[0], 0);
2647 if (i == 0 || i == 1) {
2648 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2650 ldisc_send(term->ldisc, buf, 20, 0);
2654 case 'Z': /* BackTab for xterm */
2655 compatibility(OTHER);
2657 int i = def(term->esc_args[0], 1);
2658 pos old_curs = term->curs;
2660 for(;i>0 && term->curs.x>0; i--) {
2663 } while (term->curs.x >0 &&
2664 !term->tabs[term->curs.x]);
2667 check_selection(term, old_curs, term->curs);
2670 case ANSI('L', '='):
2671 compatibility(OTHER);
2672 term->use_bce = (term->esc_args[0] <= 0);
2673 term->erase_char = ERASE_CHAR;
2675 term->erase_char = (' ' | ATTR_ASCII |
2677 (ATTR_FGMASK | ATTR_BGMASK)));
2679 case ANSI('E', '='):
2680 compatibility(OTHER);
2681 term->blink_is_real = (term->esc_args[0] >= 1);
2683 case ANSI('p', '"'):
2685 * Allow the host to make this emulator a
2686 * 'perfect' VT102. This first appeared in
2687 * the VT220, but we do need to get back to
2688 * PuTTY mode so I won't check it.
2690 * The arg in 40..42,50 are a PuTTY extension.
2691 * The 2nd arg, 8bit vs 7bit is not checked.
2693 * Setting VT102 mode should also change
2694 * the Fkeys to generate PF* codes as a
2695 * real VT102 has no Fkeys. The VT220 does
2696 * this, F11..F13 become ESC,BS,LF other
2697 * Fkeys send nothing.
2699 * Note ESC c will NOT change this!
2702 switch (term->esc_args[0]) {
2704 term->compatibility_level &= ~TM_VTXXX;
2705 term->compatibility_level |= TM_VT102;
2708 term->compatibility_level &= ~TM_VTXXX;
2709 term->compatibility_level |= TM_VT220;
2713 if (term->esc_args[0] > 60 &&
2714 term->esc_args[0] < 70)
2715 term->compatibility_level |= TM_VTXXX;
2719 term->compatibility_level &= TM_VTXXX;
2722 term->compatibility_level = TM_PUTTY;
2725 term->compatibility_level = TM_SCOANSI;
2729 term->compatibility_level = TM_PUTTY;
2735 /* Change the response to CSI c */
2736 if (term->esc_args[0] == 50) {
2739 strcpy(term->id_string, "\033[?");
2740 for (i = 1; i < term->esc_nargs; i++) {
2742 strcat(term->id_string, ";");
2743 sprintf(lbuf, "%d", term->esc_args[i]);
2744 strcat(term->id_string, lbuf);
2746 strcat(term->id_string, "c");
2749 /* Is this a good idea ?
2750 * Well we should do a soft reset at this point ...
2752 if (!has_compat(VT420) && has_compat(VT100)) {
2753 if (!term->cfg.no_remote_resize) {
2754 if (term->reset_132)
2755 request_resize(132, 24);
2757 request_resize(80, 24);
2765 term->osc_w = FALSE;
2767 case 'P': /* Linux palette sequence */
2768 term->termstate = SEEN_OSC_P;
2769 term->osc_strlen = 0;
2771 case 'R': /* Linux palette reset */
2772 palette_reset(term->frontend);
2773 term_invalidate(term);
2774 term->termstate = TOPLEVEL;
2776 case 'W': /* word-set */
2777 term->termstate = SEEN_OSC_W;
2790 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2794 * Grotty hack to support xterm and DECterm title
2795 * sequences concurrently.
2797 if (term->esc_args[0] == 2) {
2798 term->esc_args[0] = 1;
2801 /* else fall through */
2803 term->termstate = OSC_STRING;
2804 term->osc_strlen = 0;
2809 * This OSC stuff is EVIL. It takes just one character to get into
2810 * sysline mode and it's not initially obvious how to get out.
2811 * So I've added CR and LF as string aborts.
2812 * This shouldn't effect compatibility as I believe embedded
2813 * control characters are supposed to be interpreted (maybe?)
2814 * and they don't display anything useful anyway.
2818 if (c == '\012' || c == '\015') {
2819 term->termstate = TOPLEVEL;
2820 } else if (c == 0234 || c == '\007') {
2822 * These characters terminate the string; ST and BEL
2823 * terminate the sequence and trigger instant
2824 * processing of it, whereas ESC goes back to SEEN_ESC
2825 * mode unless it is followed by \, in which case it is
2826 * synonymous with ST in the first place.
2829 term->termstate = TOPLEVEL;
2830 } else if (c == '\033')
2831 term->termstate = OSC_MAYBE_ST;
2832 else if (term->osc_strlen < OSC_STR_MAX)
2833 term->osc_string[term->osc_strlen++] = c;
2837 int max = (term->osc_strlen == 0 ? 21 : 16);
2839 if (c >= '0' && c <= '9')
2841 else if (c >= 'A' && c <= 'A' + max - 10)
2843 else if (c >= 'a' && c <= 'a' + max - 10)
2846 term->termstate = TOPLEVEL;
2849 term->osc_string[term->osc_strlen++] = val;
2850 if (term->osc_strlen >= 7) {
2851 palette_set(term->frontend, term->osc_string[0],
2852 term->osc_string[1] * 16 + term->osc_string[2],
2853 term->osc_string[3] * 16 + term->osc_string[4],
2854 term->osc_string[5] * 16 + term->osc_string[6]);
2855 term_invalidate(term);
2856 term->termstate = TOPLEVEL;
2872 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2875 term->termstate = OSC_STRING;
2876 term->osc_strlen = 0;
2880 term->termstate = TOPLEVEL;
2881 term->seen_disp_event = TRUE;
2884 move(term, term->curs.x, term->curs.y - 1, 1);
2887 move(term, term->curs.x, term->curs.y + 1, 1);
2890 move(term, term->curs.x + 1, term->curs.y, 1);
2893 move(term, term->curs.x - 1, term->curs.y, 1);
2896 * From the VT100 Manual
2897 * NOTE: The special graphics characters in the VT100
2898 * are different from those in the VT52
2900 * From VT102 manual:
2901 * 137 _ Blank - Same
2902 * 140 ` Reserved - Humm.
2903 * 141 a Solid rectangle - Similar
2904 * 142 b 1/ - Top half of fraction for the
2905 * 143 c 3/ - subscript numbers below.
2908 * 146 f Degrees - Same
2909 * 147 g Plus or minus - Same
2911 * 151 i Ellipsis (dots)
2914 * 154 l Bar at scan 0
2915 * 155 m Bar at scan 1
2916 * 156 n Bar at scan 2
2917 * 157 o Bar at scan 3 - Similar
2918 * 160 p Bar at scan 4 - Similar
2919 * 161 q Bar at scan 5 - Similar
2920 * 162 r Bar at scan 6 - Same
2921 * 163 s Bar at scan 7 - Similar
2936 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2939 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2942 move(term, 0, 0, 0);
2945 if (term->curs.y == 0)
2946 scroll(term, 0, term->rows - 1, -1, TRUE);
2947 else if (term->curs.y > 0)
2950 term->wrapnext = FALSE;
2953 erase_lots(term, FALSE, FALSE, TRUE);
2957 erase_lots(term, TRUE, FALSE, TRUE);
2961 /* XXX Print cursor line */
2964 /* XXX Start controller mode */
2967 /* XXX Stop controller mode */
2971 term->termstate = VT52_Y1;
2975 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2978 term->app_keypad_keys = TRUE;
2981 term->app_keypad_keys = FALSE;
2984 /* XXX This should switch to VT100 mode not current or default
2985 * VT mode. But this will only have effect in a VT220+
2988 term->vt52_mode = FALSE;
2989 term->blink_is_real = term->cfg.blinktext;
2993 /* XXX Enter auto print mode */
2996 /* XXX Exit auto print mode */
2999 /* XXX Print screen */
3005 /* compatibility(ATARI) */
3006 move(term, 0, 0, 0);
3007 erase_lots(term, FALSE, FALSE, TRUE);
3011 /* compatibility(ATARI) */
3012 if (term->curs.y <= term->marg_b)
3013 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3016 /* compatibility(ATARI) */
3017 if (term->curs.y <= term->marg_b)
3018 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3021 /* compatibility(ATARI) */
3022 term->termstate = VT52_FG;
3025 /* compatibility(ATARI) */
3026 term->termstate = VT52_BG;
3029 /* compatibility(ATARI) */
3030 erase_lots(term, FALSE, TRUE, FALSE);
3034 /* compatibility(ATARI) */
3035 term->cursor_on = TRUE;
3038 /* compatibility(ATARI) */
3039 term->cursor_on = FALSE;
3041 /* case 'j': Save cursor position - broken on ST */
3042 /* case 'k': Restore cursor position */
3044 /* compatibility(ATARI) */
3045 erase_lots(term, TRUE, TRUE, TRUE);
3047 term->wrapnext = FALSE;
3051 /* compatibility(ATARI) */
3052 erase_lots(term, TRUE, TRUE, FALSE);
3055 /* compatibility(ATARI) */
3056 term->curr_attr |= ATTR_REVERSE;
3059 /* compatibility(ATARI) */
3060 term->curr_attr &= ~ATTR_REVERSE;
3062 case 'v': /* wrap Autowrap on - Wyse style */
3063 /* compatibility(ATARI) */
3066 case 'w': /* Autowrap off */
3067 /* compatibility(ATARI) */
3072 /* compatibility(OTHER) */
3073 term->vt52_bold = FALSE;
3074 term->curr_attr = ATTR_DEFAULT;
3076 term->erase_char = (' ' | ATTR_ASCII |
3078 (ATTR_FGMASK | ATTR_BGMASK)));
3081 /* compatibility(VI50) */
3082 term->curr_attr |= ATTR_UNDER;
3085 /* compatibility(VI50) */
3086 term->curr_attr &= ~ATTR_UNDER;
3089 /* compatibility(VI50) */
3090 term->vt52_bold = TRUE;
3091 term->curr_attr |= ATTR_BOLD;
3094 /* compatibility(VI50) */
3095 term->vt52_bold = FALSE;
3096 term->curr_attr &= ~ATTR_BOLD;
3102 term->termstate = VT52_Y2;
3103 move(term, term->curs.x, c - ' ', 0);
3106 term->termstate = TOPLEVEL;
3107 move(term, c - ' ', term->curs.y, 0);
3112 term->termstate = TOPLEVEL;
3113 term->curr_attr &= ~ATTR_FGMASK;
3114 term->curr_attr &= ~ATTR_BOLD;
3115 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3116 if ((c & 0x8) || term->vt52_bold)
3117 term->curr_attr |= ATTR_BOLD;
3120 term->erase_char = (' ' | ATTR_ASCII |
3122 (ATTR_FGMASK | ATTR_BGMASK)));
3125 term->termstate = TOPLEVEL;
3126 term->curr_attr &= ~ATTR_BGMASK;
3127 term->curr_attr &= ~ATTR_BLINK;
3128 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3130 /* Note: bold background */
3132 term->curr_attr |= ATTR_BLINK;
3135 term->erase_char = (' ' | ATTR_ASCII |
3137 (ATTR_FGMASK | ATTR_BGMASK)));
3140 default: break; /* placate gcc warning about enum use */
3142 if (term->selstate != NO_SELECTION) {
3143 pos cursplus = term->curs;
3145 check_selection(term, term->curs, cursplus);
3149 term_print_flush(term);
3154 * Compare two lines to determine whether they are sufficiently
3155 * alike to scroll-optimise one to the other. Return the degree of
3158 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3162 for (i = n = 0; i < term->cols; i++)
3163 n += (*a++ == *b++);
3169 * Given a context, update the window. Out of paranoia, we don't
3170 * allow WM_PAINT responses to do scrolling optimisations.
3172 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3174 int i, j, our_curs_y, our_curs_x;
3175 unsigned long rv, cursor;
3178 long cursor_background = ERASE_CHAR;
3179 unsigned long ticks;
3180 #ifdef OPTIMISE_SCROLL
3181 struct scrollregion *sr;
3182 #endif /* OPTIMISE_SCROLL */
3185 * Check the visual bell state.
3187 if (term->in_vbell) {
3188 ticks = GETTICKCOUNT();
3189 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3190 term->in_vbell = FALSE;
3193 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3196 * screen array, disptop, scrtop,
3198 * cfg.blinkpc, blink_is_real, tblinker,
3199 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3202 /* Has the cursor position or type changed ? */
3203 if (term->cursor_on) {
3204 if (term->has_focus) {
3205 if (term->blinker || !term->cfg.blink_cur)
3206 cursor = TATTR_ACTCURS;
3210 cursor = TATTR_PASCURS;
3212 cursor |= TATTR_RIGHTCURS;
3215 our_curs_y = term->curs.y - term->disptop;
3218 * Adjust the cursor position in the case where it's
3219 * resting on the right-hand half of a CJK wide character.
3220 * xterm's behaviour here, which seems adequate to me, is
3221 * to display the cursor covering the _whole_ character,
3222 * exactly as if it were one space to the left.
3224 unsigned long *ldata = lineptr(term->curs.y);
3225 our_curs_x = term->curs.x;
3226 if (our_curs_x > 0 &&
3227 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3231 if (term->dispcurs && (term->curstype != cursor ||
3233 term->disptext + our_curs_y * (term->cols + 1) +
3235 if (term->dispcurs > term->disptext &&
3236 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3237 term->dispcurs[-1] |= ATTR_INVALID;
3238 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3239 term->dispcurs[1] |= ATTR_INVALID;
3240 *term->dispcurs |= ATTR_INVALID;
3243 term->dispcurs = NULL;
3245 #ifdef OPTIMISE_SCROLL
3247 sr = term->scrollhead;
3249 struct scrollregion *next = sr->next;
3250 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3254 term->scrollhead = term->scrolltail = NULL;
3255 #endif /* OPTIMISE_SCROLL */
3257 /* The normal screen data */
3258 for (i = 0; i < term->rows; i++) {
3259 unsigned long *ldata;
3261 int idx, dirty_line, dirty_run, selected;
3262 unsigned long attr = 0;
3263 int updated_line = 0;
3266 int last_run_dirty = 0;
3268 scrpos.y = i + term->disptop;
3269 ldata = lineptr(scrpos.y);
3270 lattr = (ldata[term->cols] & LATTR_MODE);
3272 idx = i * (term->cols + 1);
3273 dirty_run = dirty_line = (ldata[term->cols] !=
3274 term->disptext[idx + term->cols]);
3275 term->disptext[idx + term->cols] = ldata[term->cols];
3277 for (j = 0; j < term->cols; j++, idx++) {
3278 unsigned long tattr, tchar;
3279 unsigned long *d = ldata + j;
3283 tchar = (*d & (CHAR_MASK | CSET_MASK));
3284 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3285 switch (tchar & CSET_MASK) {
3287 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3290 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3293 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3296 tattr |= (tchar & CSET_MASK);
3298 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3301 /* Video reversing things */
3302 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3303 if (term->seltype == LEXICOGRAPHIC)
3304 selected = (posle(term->selstart, scrpos) &&
3305 poslt(scrpos, term->selend));
3307 selected = (posPle(term->selstart, scrpos) &&
3308 posPlt(scrpos, term->selend));
3312 ^ (selected ? ATTR_REVERSE : 0));
3314 /* 'Real' blinking ? */
3315 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3316 if (term->has_focus && term->tblinker) {
3317 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
3319 tattr &= ~ATTR_BLINK;
3323 * Check the font we'll _probably_ be using to see if
3324 * the character is wide when we don't want it to be.
3326 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3327 if ((tattr & ATTR_WIDE) == 0 &&
3328 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3329 tattr |= ATTR_NARROW;
3330 } else if (term->disptext[idx]&ATTR_NARROW)
3331 tattr |= ATTR_NARROW;
3333 /* Cursor here ? Save the 'background' */
3334 if (i == our_curs_y && j == our_curs_x) {
3335 cursor_background = tattr | tchar;
3336 term->dispcurs = term->disptext + idx;
3339 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3342 break_run = (((tattr ^ attr) & term->attr_mask) ||
3343 j - start >= sizeof(ch));
3345 /* Special hack for VT100 Linedraw glyphs */
3346 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3347 && tchar <= 0xBD) break_run = TRUE;
3349 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3350 if ((tchar | tattr) == term->disptext[idx])
3352 else if (!dirty_run && ccount == 1)
3357 if ((dirty_run || last_run_dirty) && ccount > 0) {
3358 do_text(ctx, start, i, ch, ccount, attr, lattr);
3364 if (term->ucsdata->dbcs_screenfont)
3365 last_run_dirty = dirty_run;
3366 dirty_run = dirty_line;
3369 if ((tchar | tattr) != term->disptext[idx])
3371 ch[ccount++] = (char) tchar;
3372 term->disptext[idx] = tchar | tattr;
3374 /* If it's a wide char step along to the next one. */
3375 if (tattr & ATTR_WIDE) {
3376 if (++j < term->cols) {
3380 * By construction above, the cursor should not
3381 * be on the right-hand half of this character.
3384 assert(!(i == our_curs_y && j == our_curs_x));
3385 if (term->disptext[idx] != *d)
3387 term->disptext[idx] = *d;
3391 if (dirty_run && ccount > 0) {
3392 do_text(ctx, start, i, ch, ccount, attr, lattr);
3396 /* Cursor on this line ? (and changed) */
3397 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3398 ch[0] = (char) (cursor_background & CHAR_MASK);
3399 attr = (cursor_background & ATTR_MASK) | cursor;
3400 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3401 term->curstype = cursor;
3407 * Flick the switch that says if blinking things should be shown or hidden.
3410 void term_blink(Terminal *term, int flg)
3412 long now, blink_diff;
3414 now = GETTICKCOUNT();
3415 blink_diff = now - term->last_tblink;
3417 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3418 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3419 term->last_tblink = now;
3420 term->tblinker = !term->tblinker;
3425 term->last_blink = now;
3429 blink_diff = now - term->last_blink;
3431 /* Make sure the cursor blinks no faster than system blink rate */
3432 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3435 term->last_blink = now;
3436 term->blinker = !term->blinker;
3440 * Invalidate the whole screen so it will be repainted in full.
3442 void term_invalidate(Terminal *term)
3446 for (i = 0; i < term->rows * (term->cols + 1); i++)
3447 term->disptext[i] = ATTR_INVALID;
3451 * Paint the window in response to a WM_PAINT message.
3453 void term_paint(Terminal *term, Context ctx,
3454 int left, int top, int right, int bottom, int immediately)
3457 if (left < 0) left = 0;
3458 if (top < 0) top = 0;
3459 if (right >= term->cols) right = term->cols-1;
3460 if (bottom >= term->rows) bottom = term->rows-1;
3462 for (i = top; i <= bottom && i < term->rows; i++) {
3463 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3464 LATTR_MODE) == LATTR_NORM)
3465 for (j = left; j <= right && j < term->cols; j++)
3466 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3468 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3469 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3472 /* This should happen soon enough, also for some reason it sometimes
3473 * fails to actually do anything when re-sizing ... painting the wrong
3477 do_paint (term, ctx, FALSE);
3481 * Attempt to scroll the scrollback. The second parameter gives the
3482 * position we want to scroll to; the first is +1 to denote that
3483 * this position is relative to the beginning of the scrollback, -1
3484 * to denote it is relative to the end, and 0 to denote that it is
3485 * relative to the current position.
3487 void term_scroll(Terminal *term, int rel, int where)
3489 int sbtop = -sblines(term);
3490 #ifdef OPTIMISE_SCROLL
3491 int olddisptop = term->disptop;
3493 #endif /* OPTIMISE_SCROLL */
3495 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3496 if (term->disptop < sbtop)
3497 term->disptop = sbtop;
3498 if (term->disptop > 0)
3501 #ifdef OPTIMISE_SCROLL
3502 shift = (term->disptop - olddisptop);
3503 if (shift < term->rows && shift > -term->rows)
3504 scroll_display(term, 0, term->rows - 1, shift);
3505 #endif /* OPTIMISE_SCROLL */
3509 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3512 wchar_t *wbptr; /* where next char goes within workbuf */
3514 int wblen = 0; /* workbuf len */
3515 int buflen; /* amount of memory allocated to workbuf */
3517 buflen = 5120; /* Default size */
3518 workbuf = smalloc(buflen * sizeof(wchar_t));
3519 wbptr = workbuf; /* start filling here */
3520 old_top_x = top.x; /* needed for rect==1 */
3522 while (poslt(top, bottom)) {
3524 unsigned long *ldata = lineptr(top.y);
3528 * nlpos will point at the maximum position on this line we
3529 * should copy up to. So we start it at the end of the
3533 nlpos.x = term->cols;
3536 * ... move it backwards if there's unused space at the end
3537 * of the line (and also set `nl' if this is the case,
3538 * because in normal selection mode this means we need a
3539 * newline at the end)...
3541 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3542 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3543 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3544 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3545 && poslt(top, nlpos))
3547 if (poslt(nlpos, bottom))
3549 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3550 /* Ignore the last char on the line in a WRAPPED2 line. */
3555 * ... and then clip it to the terminal x coordinate if
3556 * we're doing rectangular selection. (In this case we
3557 * still did the above, so that copying e.g. the right-hand
3558 * column from a table doesn't fill with spaces on the
3562 if (nlpos.x > bottom.x)
3564 nl = (top.y < bottom.y);
3567 while (poslt(top, bottom) && poslt(top, nlpos)) {
3570 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3572 wchar_t cbuf[16], *p;
3573 int uc = (ldata[top.x] & 0xFFFF);
3576 if (uc == UCSWIDE) {
3581 switch (uc & CSET_MASK) {
3583 if (!term->cfg.rawcnp) {
3584 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 set = (uc & CSET_MASK);
3604 c = (uc & CHAR_MASK);
3608 if (DIRECT_FONT(uc)) {
3609 if (c >= ' ' && c != 0x7F) {
3613 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3615 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3616 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3620 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3624 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3631 for (p = cbuf; *p; p++) {
3632 /* Enough overhead for trailing NL and nul */
3633 if (wblen >= buflen - 16) {
3636 sizeof(wchar_t) * (buflen += 100));
3637 wbptr = workbuf + wblen;
3646 for (i = 0; i < sel_nl_sz; i++) {
3648 *wbptr++ = sel_nl[i];
3652 top.x = rect ? old_top_x : 0;
3654 #if SELECTION_NUL_TERMINATED
3658 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3659 if (buflen > 0) /* indicates we allocated this buffer */
3663 void term_copyall(Terminal *term)
3666 top.y = -sblines(term);
3668 clipme(term, top, term->curs, 0);
3672 * The wordness array is mainly for deciding the disposition of the
3673 * US-ASCII characters.
3675 static int wordtype(Terminal *term, int uc)
3678 int start, end, ctype;
3680 static const struct ucsword ucs_words[] = {
3686 0x037e, 0x037e, 1}, /* Greek question mark */
3688 0x0387, 0x0387, 1}, /* Greek ano teleia */
3690 0x055a, 0x055f, 1}, /* Armenian punctuation */
3692 0x0589, 0x0589, 1}, /* Armenian full stop */
3694 0x0700, 0x070d, 1}, /* Syriac punctuation */
3696 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3698 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3700 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3702 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3704 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3706 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3708 0x2000, 0x200a, 0}, /* Various spaces */
3710 0x2070, 0x207f, 2}, /* superscript */
3712 0x2080, 0x208f, 2}, /* subscript */
3714 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3716 0x3000, 0x3000, 0}, /* ideographic space */
3718 0x3001, 0x3020, 1}, /* ideographic punctuation */
3720 0x303f, 0x309f, 3}, /* Hiragana */
3722 0x30a0, 0x30ff, 3}, /* Katakana */
3724 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3726 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3728 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3730 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3732 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3734 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3736 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3738 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3740 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3744 const struct ucsword *wptr;
3746 uc &= (CSET_MASK | CHAR_MASK);
3748 switch (uc & CSET_MASK) {
3750 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3753 uc = term->ucsdata->unitab_line[uc & 0xFF];
3756 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3759 switch (uc & CSET_MASK) {
3761 uc = term->ucsdata->unitab_font[uc & 0xFF];
3764 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3768 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3769 * fail as there's such a thing as a double width space. :-(
3771 if (term->ucsdata->dbcs_screenfont &&
3772 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3776 return term->wordness[uc];
3778 for (wptr = ucs_words; wptr->start; wptr++) {
3779 if (uc >= wptr->start && uc <= wptr->end)
3787 * Spread the selection outwards according to the selection mode.
3789 static pos sel_spread_half(Terminal *term, pos p, int dir)
3791 unsigned long *ldata;
3793 int topy = -sblines(term);
3795 ldata = lineptr(p.y);
3797 switch (term->selmode) {
3800 * In this mode, every character is a separate unit, except
3801 * for runs of spaces at the end of a non-wrapping line.
3803 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3804 unsigned long *q = ldata + term->cols;
3805 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3807 if (q == ldata + term->cols)
3809 if (p.x >= q - ldata)
3810 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3815 * In this mode, the units are maximal runs of characters
3816 * whose `wordness' has the same value.
3818 wvalue = wordtype(term, UCSGET(ldata, p.x));
3821 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3822 term->cols-1 : term->cols);
3823 if (p.x < maxcols-1) {
3824 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3829 if (ldata[term->cols] & LATTR_WRAPPED) {
3830 unsigned long *ldata2;
3831 ldata2 = lineptr(p.y+1);
3832 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3845 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3850 unsigned long *ldata2;
3854 ldata2 = lineptr(p.y-1);
3855 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3856 term->cols-1 : term->cols);
3857 if (ldata2[term->cols] & LATTR_WRAPPED) {
3858 if (wordtype(term, UCSGET(ldata2, maxcols-1))
3873 * In this mode, every line is a unit.
3875 p.x = (dir == -1 ? 0 : term->cols - 1);
3881 static void sel_spread(Terminal *term)
3883 if (term->seltype == LEXICOGRAPHIC) {
3884 term->selstart = sel_spread_half(term, term->selstart, -1);
3885 decpos(term->selend);
3886 term->selend = sel_spread_half(term, term->selend, +1);
3887 incpos(term->selend);
3891 void term_do_paste(Terminal *term)
3896 get_clip(term->frontend, &data, &len);
3897 if (data && len > 0) {
3900 term_seen_key_event(term); /* pasted data counts */
3902 if (term->paste_buffer)
3903 sfree(term->paste_buffer);
3904 term->paste_pos = term->paste_hold = term->paste_len = 0;
3905 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3908 while (p < data + len) {
3909 while (p < data + len &&
3910 !(p <= data + len - sel_nl_sz &&
3911 !memcmp(p, sel_nl, sizeof(sel_nl))))
3916 for (i = 0; i < p - q; i++) {
3917 term->paste_buffer[term->paste_len++] = q[i];
3921 if (p <= data + len - sel_nl_sz &&
3922 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3923 term->paste_buffer[term->paste_len++] = '\015';
3929 /* Assume a small paste will be OK in one go. */
3930 if (term->paste_len < 256) {
3932 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3933 if (term->paste_buffer)
3934 sfree(term->paste_buffer);
3935 term->paste_buffer = 0;
3936 term->paste_pos = term->paste_hold = term->paste_len = 0;
3939 get_clip(term->frontend, NULL, NULL);
3942 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
3943 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
3946 unsigned long *ldata;
3947 int raw_mouse = (term->xterm_mouse &&
3948 !term->cfg.no_mouse_rep &&
3949 !(term->cfg.mouse_override && shift));
3950 int default_seltype;
3954 if (a == MA_DRAG && !raw_mouse)
3955 term_scroll(term, 0, -1);
3957 if (y >= term->rows) {
3959 if (a == MA_DRAG && !raw_mouse)
3960 term_scroll(term, 0, +1);
3969 if (x >= term->cols)
3972 selpoint.y = y + term->disptop;
3974 ldata = lineptr(selpoint.y);
3975 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3979 int encstate = 0, r, c;
3986 encstate = 0x20; /* left button down */
3997 case MBT_WHEEL_DOWN:
4000 default: break; /* placate gcc warning about enum use */
4004 if (term->xterm_mouse == 1)
4010 term->mouse_is_down = 0;
4013 if (term->mouse_is_down == braw)
4015 term->mouse_is_down = braw;
4017 default: break; /* placate gcc warning about enum use */
4026 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4027 ldisc_send(term->ldisc, abuf, 6, 0);
4033 * Set the selection type (rectangular or normal) at the start
4034 * of a selection attempt, from the state of Alt.
4036 if (!alt ^ !term->cfg.rect_select)
4037 default_seltype = RECTANGULAR;
4039 default_seltype = LEXICOGRAPHIC;
4041 if (term->selstate == NO_SELECTION) {
4042 term->seltype = default_seltype;
4045 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4047 term->selstate = ABOUT_TO;
4048 term->seltype = default_seltype;
4049 term->selanchor = selpoint;
4050 term->selmode = SM_CHAR;
4051 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4053 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4054 term->selstate = DRAGGING;
4055 term->selstart = term->selanchor = selpoint;
4056 term->selend = term->selstart;
4057 incpos(term->selend);
4059 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4060 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4061 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4063 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4064 term->selstate == SELECTED) {
4065 if (term->seltype == LEXICOGRAPHIC) {
4067 * For normal selection, we extend by moving
4068 * whichever end of the current selection is closer
4071 if (posdiff(selpoint, term->selstart) <
4072 posdiff(term->selend, term->selstart) / 2) {
4073 term->selanchor = term->selend;
4074 decpos(term->selanchor);
4076 term->selanchor = term->selstart;
4080 * For rectangular selection, we have a choice of
4081 * _four_ places to put selanchor and selpoint: the
4082 * four corners of the selection.
4084 if (2*selpoint.x < term->selstart.x + term->selend.x)
4085 term->selanchor.x = term->selend.x-1;
4087 term->selanchor.x = term->selstart.x;
4089 if (2*selpoint.y < term->selstart.y + term->selend.y)
4090 term->selanchor.y = term->selend.y;
4092 term->selanchor.y = term->selstart.y;
4094 term->selstate = DRAGGING;
4096 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4097 term->selanchor = selpoint;
4098 term->selstate = DRAGGING;
4099 if (term->seltype == LEXICOGRAPHIC) {
4101 * For normal selection, we set (selstart,selend) to
4102 * (selpoint,selanchor) in some order.
4104 if (poslt(selpoint, term->selanchor)) {
4105 term->selstart = selpoint;
4106 term->selend = term->selanchor;
4107 incpos(term->selend);
4109 term->selstart = term->selanchor;
4110 term->selend = selpoint;
4111 incpos(term->selend);
4115 * For rectangular selection, we may need to
4116 * interchange x and y coordinates (if the user has
4117 * dragged in the -x and +y directions, or vice versa).
4119 term->selstart.x = min(term->selanchor.x, selpoint.x);
4120 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4121 term->selstart.y = min(term->selanchor.y, selpoint.y);
4122 term->selend.y = max(term->selanchor.y, selpoint.y);
4125 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4127 if (term->selstate == DRAGGING) {
4129 * We've completed a selection. We now transfer the
4130 * data to the clipboard.
4132 clipme(term, term->selstart, term->selend,
4133 (term->seltype == RECTANGULAR));
4134 term->selstate = SELECTED;
4136 term->selstate = NO_SELECTION;
4137 } else if (bcooked == MBT_PASTE
4139 #if MULTICLICK_ONLY_EVENT
4140 || a == MA_2CLK || a == MA_3CLK
4143 request_paste(term->frontend);
4149 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4150 unsigned int modifiers, unsigned int flags)
4154 int prependesc = FALSE;
4158 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4159 for (i = 0; i < tlen; i++)
4160 fprintf(stderr, " %04x", (unsigned)text[i]);
4161 fprintf(stderr, "\n");
4165 if ((flags & PKF_REPEAT) && term->repeat_off)
4168 /* Currently, Meta always just prefixes everything with ESC. */
4169 if (modifiers & PKM_META)
4171 modifiers &= ~PKM_META;
4174 * Alt is only used for Alt+keypad, which isn't supported yet, so
4177 modifiers &= ~PKM_ALT;
4179 /* Standard local function keys */
4180 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4182 if (keysym == PK_PAGEUP)
4183 /* scroll up one page */;
4184 if (keysym == PK_PAGEDOWN)
4185 /* scroll down on page */;
4186 if (keysym == PK_INSERT)
4187 term_do_paste(term);
4190 if (keysym == PK_PAGEUP)
4191 /* scroll up one line */;
4192 if (keysym == PK_PAGEDOWN)
4193 /* scroll down one line */;
4194 /* Control-Numlock for app-keypad mode switch */
4195 if (keysym == PK_PF1)
4196 term->app_keypad_keys ^= 1;
4200 if (modifiers & PKM_ALT) {
4201 /* Alt+F4 (close) */
4202 /* Alt+Return (full screen) */
4203 /* Alt+Space (system menu) */
4206 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4207 text[0] >= 0x20 && text[0] <= 0x7e) {
4208 /* ASCII chars + Control */
4209 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4210 (text[0] >= 0x61 && text[0] <= 0x7a))
4214 * Control-2 should return ^@ (0x00), Control-6 should return
4215 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4216 * the DOS keyboard handling did it, and we have nothing better
4217 * to do with the key combo in question, we'll also map
4218 * Control-Backquote to ^\ (0x1C).
4221 case ' ': text[0] = 0x00; break;
4222 case '-': text[0] = 0x1f; break;
4223 case '/': text[0] = 0x1f; break;
4224 case '2': text[0] = 0x00; break;
4225 case '3': text[0] = 0x1b; break;
4226 case '4': text[0] = 0x1c; break;
4227 case '5': text[0] = 0x1d; break;
4228 case '6': text[0] = 0x1e; break;
4229 case '7': text[0] = 0x1f; break;
4230 case '8': text[0] = 0x7f; break;
4231 case '`': text[0] = 0x1c; break;
4236 /* Nethack keypad */
4237 if (term->cfg.nethack_keypad) {
4240 case PK_KP1: c = 'b'; break;
4241 case PK_KP2: c = 'j'; break;
4242 case PK_KP3: c = 'n'; break;
4243 case PK_KP4: c = 'h'; break;
4244 case PK_KP5: c = '.'; break;
4245 case PK_KP6: c = 'l'; break;
4246 case PK_KP7: c = 'y'; break;
4247 case PK_KP8: c = 'k'; break;
4248 case PK_KP9: c = 'u'; break;
4249 default: break; /* else gcc warns `enum value not used' */
4253 if (modifiers & PKM_CONTROL)
4255 else if (modifiers & PKM_SHIFT)
4263 /* Numeric Keypad */
4264 if (PK_ISKEYPAD(keysym)) {
4268 * In VT400 mode, PFn always emits an escape sequence. In
4269 * Linux and tilde modes, this only happens in app keypad mode.
4271 if (term->cfg.funky_type == FUNKY_VT400 ||
4272 ((term->cfg.funky_type == FUNKY_LINUX ||
4273 term->cfg.funky_type == FUNKY_TILDE) &&
4274 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4276 case PK_PF1: xkey = 'P'; break;
4277 case PK_PF2: xkey = 'Q'; break;
4278 case PK_PF3: xkey = 'R'; break;
4279 case PK_PF4: xkey = 'S'; break;
4280 default: break; /* else gcc warns `enum value not used' */
4283 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4285 case PK_KP0: xkey = 'p'; break;
4286 case PK_KP1: xkey = 'q'; break;
4287 case PK_KP2: xkey = 'r'; break;
4288 case PK_KP3: xkey = 's'; break;
4289 case PK_KP4: xkey = 't'; break;
4290 case PK_KP5: xkey = 'u'; break;
4291 case PK_KP6: xkey = 'v'; break;
4292 case PK_KP7: xkey = 'w'; break;
4293 case PK_KP8: xkey = 'x'; break;
4294 case PK_KP9: xkey = 'y'; break;
4295 case PK_KPDECIMAL: xkey = 'n'; break;
4296 case PK_KPENTER: xkey = 'M'; break;
4297 default: break; /* else gcc warns `enum value not used' */
4299 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4301 * xterm can't see the layout of the keypad, so it has
4302 * to rely on the X keysyms returned by the keys.
4303 * Hence, we look at the strings here, not the PuTTY
4304 * keysyms (which describe the layout).
4308 if (modifiers & PKM_SHIFT)
4313 case '/': xkey = 'o'; break;
4314 case '*': xkey = 'j'; break;
4315 case '-': xkey = 'm'; break;
4319 * In all other modes, we try to retain the layout of
4320 * the DEC keypad in application mode.
4324 /* This key covers the '-' and ',' keys on a VT220 */
4325 if (modifiers & PKM_SHIFT)
4326 xkey = 'm'; /* VT220 '-' */
4328 xkey = 'l'; /* VT220 ',' */
4330 case PK_KPMINUS: xkey = 'm'; break;
4331 case PK_KPCOMMA: xkey = 'l'; break;
4332 default: break; /* else gcc warns `enum value not used' */
4337 if (term->vt52_mode) {
4338 if (xkey >= 'P' && xkey <= 'S')
4339 p += sprintf((char *) p, "\x1B%c", xkey);
4341 p += sprintf((char *) p, "\x1B?%c", xkey);
4343 p += sprintf((char *) p, "\x1BO%c", xkey);
4346 /* Not in application mode -- treat the number pad as arrow keys? */
4347 if ((flags & PKF_NUMLOCK) == 0) {
4349 case PK_KP0: keysym = PK_INSERT; break;
4350 case PK_KP1: keysym = PK_END; break;
4351 case PK_KP2: keysym = PK_DOWN; break;
4352 case PK_KP3: keysym = PK_PAGEDOWN; break;
4353 case PK_KP4: keysym = PK_LEFT; break;
4354 case PK_KP5: keysym = PK_REST; break;
4355 case PK_KP6: keysym = PK_RIGHT; break;
4356 case PK_KP7: keysym = PK_HOME; break;
4357 case PK_KP8: keysym = PK_UP; break;
4358 case PK_KP9: keysym = PK_PAGEUP; break;
4359 default: break; /* else gcc warns `enum value not used' */
4364 /* Miscellaneous keys */
4371 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4372 else if (modifiers == PKM_SHIFT)
4373 /* We do the opposite of what is configured */
4374 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4380 else if (modifiers == PKM_SHIFT)
4381 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4384 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4386 if (modifiers == PKM_CONTROL)
4391 case PK_KPENTER: /* Odd keypad modes handled above */
4392 if (modifiers == 0) {
4394 if (term->cr_lf_return)
4398 default: break; /* else gcc warns `enum value not used' */
4401 /* SCO function keys and editing keys */
4402 if (term->cfg.funky_type == FUNKY_SCO) {
4403 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4404 static char const codes[] =
4405 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4406 int index = keysym - PK_F1;
4408 if (modifiers & PKM_SHIFT) index += 12;
4409 if (modifiers & PKM_CONTROL) index += 24;
4410 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4413 if (PK_ISEDITING(keysym)) {
4417 case PK_DELETE: *p++ = 0x7f; goto done;
4418 case PK_HOME: xkey = 'H'; break;
4419 case PK_INSERT: xkey = 'L'; break;
4420 case PK_END: xkey = 'F'; break;
4421 case PK_PAGEUP: xkey = 'I'; break;
4422 case PK_PAGEDOWN: xkey = 'G'; break;
4423 default: break; /* else gcc warns `enum value not used' */
4425 p += sprintf((char *) p, "\x1B[%c", xkey);
4429 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4432 if (term->cfg.funky_type == FUNKY_XTERM) {
4433 /* Xterm shuffles these keys, apparently. */
4435 case PK_HOME: keysym = PK_INSERT; break;
4436 case PK_INSERT: keysym = PK_HOME; break;
4437 case PK_DELETE: keysym = PK_END; break;
4438 case PK_END: keysym = PK_PAGEUP; break;
4439 case PK_PAGEUP: keysym = PK_DELETE; break;
4440 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
4441 default: break; /* else gcc warns `enum value not used' */
4446 if (term->cfg.rxvt_homeend &&
4447 (keysym == PK_HOME || keysym == PK_END)) {
4448 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4452 if (term->vt52_mode) {
4456 * A real VT52 doesn't have these, and a VT220 doesn't
4457 * send anything for them in VT52 mode.
4460 case PK_HOME: xkey = 'H'; break;
4461 case PK_INSERT: xkey = 'L'; break;
4462 case PK_DELETE: xkey = 'M'; break;
4463 case PK_END: xkey = 'E'; break;
4464 case PK_PAGEUP: xkey = 'I'; break;
4465 case PK_PAGEDOWN: xkey = 'G'; break;
4466 default: break; /* else gcc warns `enum value not used' */
4468 p += sprintf((char *) p, "\x1B%c", xkey);
4473 case PK_HOME: code = 1; break;
4474 case PK_INSERT: code = 2; break;
4475 case PK_DELETE: code = 3; break;
4476 case PK_END: code = 4; break;
4477 case PK_PAGEUP: code = 5; break;
4478 case PK_PAGEDOWN: code = 6; break;
4479 default: break; /* else gcc warns `enum value not used' */
4481 p += sprintf((char *) p, "\x1B[%d~", code);
4485 if (PK_ISFKEY(keysym)) {
4486 /* Map Shift+F1-F10 to F11-F20 */
4487 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4489 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4491 /* XXX This overrides the XTERM/VT52 mode below */
4493 if (keysym >= PK_F6) offt++;
4494 if (keysym >= PK_F12) offt++;
4495 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4496 'P' + keysym - PK_F1 - offt);
4499 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4500 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4503 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4504 if (term->vt52_mode)
4505 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4507 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4510 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4514 if (PK_ISCURSOR(keysym)) {
4518 case PK_UP: xkey = 'A'; break;
4519 case PK_DOWN: xkey = 'B'; break;
4520 case PK_RIGHT: xkey = 'C'; break;
4521 case PK_LEFT: xkey = 'D'; break;
4522 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
4523 default: break; /* else gcc warns `enum value not used' */
4525 if (term->vt52_mode)
4526 p += sprintf((char *) p, "\x1B%c", xkey);
4528 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4530 /* Useful mapping of Ctrl-arrows */
4531 if (modifiers == PKM_CONTROL)
4535 p += sprintf((char *) p, "\x1BO%c", xkey);
4537 p += sprintf((char *) p, "\x1B[%c", xkey);
4543 if (p > output || tlen > 0) {
4545 * Interrupt an ongoing paste. I'm not sure
4546 * this is sensible, but for the moment it's
4547 * preferable to having to faff about buffering
4553 * We need not bother about stdin backlogs
4554 * here, because in GUI PuTTY we can't do
4555 * anything about it anyway; there's no means
4556 * of asking Windows to hold off on KEYDOWN
4557 * messages. We _have_ to buffer everything
4560 term_seen_key_event(term);
4564 fprintf(stderr, "sending ESC\n");
4566 ldisc_send(term->ldisc, "\x1b", 1, 1);
4571 fprintf(stderr, "sending %d bytes:", p - output);
4572 for (i = 0; i < p - output; i++)
4573 fprintf(stderr, " %02x", output[i]);
4574 fprintf(stderr, "\n");
4576 ldisc_send(term->ldisc, output, p - output, 1);
4577 } else if (tlen > 0) {
4579 fprintf(stderr, "sending %d unichars:", tlen);
4580 for (i = 0; i < tlen; i++)
4581 fprintf(stderr, " %04x", (unsigned) text[i]);
4582 fprintf(stderr, "\n");
4584 luni_send(term->ldisc, text, tlen, 1);
4589 void term_nopaste(Terminal *term)
4591 if (term->paste_len == 0)
4593 sfree(term->paste_buffer);
4594 term->paste_buffer = NULL;
4595 term->paste_len = 0;
4598 int term_paste_pending(Terminal *term)
4600 return term->paste_len != 0;
4603 void term_paste(Terminal *term)
4605 long now, paste_diff;
4607 if (term->paste_len == 0)
4610 /* Don't wait forever to paste */
4611 if (term->paste_hold) {
4612 now = GETTICKCOUNT();
4613 paste_diff = now - term->last_paste;
4614 if (paste_diff >= 0 && paste_diff < 450)
4617 term->paste_hold = 0;
4619 while (term->paste_pos < term->paste_len) {
4621 while (n + term->paste_pos < term->paste_len) {
4622 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4626 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4627 term->paste_pos += n;
4629 if (term->paste_pos < term->paste_len) {
4630 term->paste_hold = 1;
4634 sfree(term->paste_buffer);
4635 term->paste_buffer = NULL;
4636 term->paste_len = 0;
4639 static void deselect(Terminal *term)
4641 term->selstate = NO_SELECTION;
4642 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4645 void term_deselect(Terminal *term)
4651 int term_ldisc(Terminal *term, int option)
4653 if (option == LD_ECHO)
4654 return term->term_echoing;
4655 if (option == LD_EDIT)
4656 return term->term_editing;
4661 * from_backend(), to get data from the backend for the terminal.
4663 int from_backend(void *vterm, int is_stderr, const char *data, int len)
4665 Terminal *term = (Terminal *)vterm;
4669 bufchain_add(&term->inbuf, data, len);
4672 * term_out() always completely empties inbuf. Therefore,
4673 * there's no reason at all to return anything other than zero
4674 * from this function, because there _can't_ be a question of
4675 * the remote side needing to wait until term_out() has cleared
4678 * This is a slightly suboptimal way to deal with SSH2 - in
4679 * principle, the window mechanism would allow us to continue
4680 * to accept data on forwarded ports and X connections even
4681 * while the terminal processing was going slowly - but we
4682 * can't do the 100% right thing without moving the terminal
4683 * processing into a separate thread, and that might hurt
4684 * portability. So we manage stdout buffering the old SSH1 way:
4685 * if the terminal processing goes slowly, the whole SSH
4686 * connection stops accepting data until it's ready.
4688 * In practice, I can't imagine this causing serious trouble.
4693 void term_provide_logctx(Terminal *term, void *logctx)
4695 term->logctx = logctx;