8 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
9 #define CL_VT100 0x0002 /* VT100 */
10 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
11 #define CL_VT102 0x0008 /* VT102 */
12 #define CL_VT220 0x0010 /* VT220 */
13 #define CL_VT320 0x0020 /* VT320 */
14 #define CL_VT420 0x0040 /* VT420 */
15 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
16 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
17 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
18 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
19 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
21 #define TM_ANSIMIN (CL_ANSIMIN)
22 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
23 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
24 #define TM_VT102 (TM_VT100AVO|CL_VT102)
25 #define TM_VT220 (TM_VT102|CL_VT220)
26 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
27 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
29 #define TM_PUTTY (0xFFFF)
31 #define compatibility(x) \
32 if ( ((CL_##x)&compatibility_level) == 0 ) { \
36 #define compatibility2(x,y) \
37 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
42 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
44 static int compatibility_level = TM_PUTTY;
47 static unsigned long *text; /* buffer of text on terminal screen */
48 static unsigned long *scrtop; /* top of working screen */
49 static unsigned long *disptop; /* top of displayed screen */
50 static unsigned long *sbtop; /* top of scrollback */
51 static unsigned long *sbbot; /* furthest extent of scrollback */
52 static unsigned long *cpos; /* cursor position (convenience) */
53 static unsigned long *disptext; /* buffer of text on real screen */
54 static unsigned long *wanttext; /* buffer of text we want on screen */
55 static unsigned long *alttext; /* buffer of text on alt. screen */
57 static unsigned char *selspace; /* buffer for building selections in */
59 #define TSIZE (sizeof(*text))
60 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
62 static unsigned long curr_attr, save_attr;
63 static unsigned long erase_char = ERASE_CHAR;
65 static int curs_x, curs_y; /* cursor */
66 static int save_x, save_y; /* saved cursor position */
67 static int marg_t, marg_b; /* scroll margins */
68 static int dec_om; /* DEC origin mode flag */
69 static int wrap, wrapnext; /* wrap flags */
70 static int insert; /* insert-mode flag */
71 static int cset; /* 0 or 1: which char set */
72 static int save_cset, save_csattr; /* saved with cursor position */
73 static int rvideo; /* global reverse video flag */
74 static int cursor_on; /* cursor enabled flag */
75 static int reset_132; /* Flag ESC c resets to 80 cols */
76 static int use_bce; /* Use Background coloured erase */
77 static int blinker; /* When blinking is the cursor on ? */
78 static int tblinker; /* When the blinking text is on */
79 static int blink_is_real; /* Actually blink blinking text */
81 static unsigned long cset_attr[2];
84 * Saved settings on the alternate screen.
86 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
87 static int alt_t, alt_b;
90 #define ARGS_MAX 32 /* max # of esc sequence arguments */
91 #define ARG_DEFAULT 0 /* if an arg isn't specified */
92 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
93 static int esc_args[ARGS_MAX];
96 #define ANSI(x,y) ((x)+((y)<<8))
97 #define ANSI_QUE(x) ANSI(x,TRUE)
99 #define OSC_STR_MAX 2048
100 static int osc_strlen;
101 static char osc_string[OSC_STR_MAX+1];
104 static char id_string[1024] = "\033[?6c";
106 static unsigned char *tabs;
120 OSC_STRING, OSC_MAYBE_ST,
128 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
131 SM_CHAR, SM_WORD, SM_LINE
133 static unsigned long *selstart, *selend, *selanchor;
135 static short wordness[256] = {
136 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
137 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
138 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
139 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
140 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
141 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
142 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
143 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
146 static unsigned char sel_nl[] = SEL_NL;
147 static char * paste_buffer = 0;
148 static int paste_len, paste_pos, paste_hold;
151 * Internal prototypes.
153 static void do_paint (Context, int);
154 static void erase_lots (int, int, int);
155 static void swap_screen (int);
156 static void update_sbar (void);
157 static void deselect (void);
160 * Set up power-on settings for the terminal.
162 static void power_on(void) {
163 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
166 alt_b = marg_b = rows - 1;
171 for (i = 0; i < cols; i++)
172 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
174 alt_om = dec_om = cfg.dec_om;
175 alt_wnext = wrapnext = alt_ins = insert = FALSE;
176 alt_wrap = wrap = cfg.wrap_mode;
178 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
181 save_attr = curr_attr = ATTR_DEFAULT;
182 app_cursor_keys = cfg.app_cursor;
183 app_keypad_keys = cfg.app_keypad;
185 blink_is_real = cfg.blinktext;
186 erase_char = ERASE_CHAR;
190 for (i = 0; i < 256; i++)
191 wordness[i] = cfg.wordness[i];
195 erase_lots (FALSE, TRUE, TRUE);
197 erase_lots (FALSE, TRUE, TRUE);
202 * Force a screen update.
204 void term_update(void) {
208 if ( (seen_key_event && (cfg.scroll_on_key)) ||
209 (seen_disp_event && (!cfg.scroll_on_key)) ) {
211 seen_disp_event = seen_key_event = 0;
213 do_paint (ctx, TRUE);
214 sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1));
220 * Same as power_on(), but an external function.
222 void term_pwron(void) {
231 * Clear the scrollback.
233 void term_clrsb(void) {
234 disptop = sbtop = scrtop;
239 * Initialise the terminal.
241 void term_init(void) {
242 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
243 disptext = wanttext = NULL;
252 * Set up the terminal for a given size.
254 void term_size(int newrows, int newcols, int newsavelines) {
255 unsigned long *newtext, *newdisp, *newwant, *newalt;
256 int i, j, crows, ccols;
258 int save_alt_which = alt_which;
260 if (newrows == rows && newcols == cols && newsavelines == savelines)
261 return; /* nothing to do */
267 alt_b = marg_b = newrows - 1;
269 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
270 sbbot = newtext + newsavelines*(newcols+1);
271 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
272 newtext[i] = erase_char;
274 crows = rows + (scrtop - sbtop) / (cols+1);
275 if (crows > newrows+newsavelines)
276 crows = newrows+newsavelines;
280 disptop = newtext + (crows-newrows)*(newcols+1);
281 ccols = (cols < newcols ? cols : newcols);
282 for (i=0; i<crows; i++) {
283 int oldidx = (rows - crows + i) * (cols+1);
284 int newidx = (newrows - crows + i) * (newcols+1);
286 for (j=0; j<ccols; j++)
287 disptop[newidx+j] = scrtop[oldidx+j];
288 disptop[newidx+newcols] =
289 (cols == newcols ? scrtop[oldidx+cols]
290 : (scrtop[oldidx+cols]&LATTR_MODE));
294 sbtop = disptop = newtext;
300 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
301 for (i=0; i<newrows*(newcols+1); i++)
302 newdisp[i] = ATTR_INVALID;
306 newwant = smalloc (newrows*(newcols+1)*TSIZE);
307 for (i=0; i<newrows*(newcols+1); i++)
308 newwant[i] = ATTR_INVALID;
312 newalt = smalloc (newrows*(newcols+1)*TSIZE);
313 for (i=0; i<newrows*(newcols+1); i++)
314 newalt[i] = erase_char;
319 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
321 tabs = srealloc (tabs, newcols*sizeof(*tabs));
324 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
325 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
329 curs_y += newrows - rows;
332 if (curs_y >= newrows)
334 if (curs_x >= newcols)
337 wrapnext = alt_wnext = FALSE;
341 savelines = newsavelines;
344 swap_screen(save_alt_which);
353 static void swap_screen (int which) {
357 if (which == alt_which)
362 for (t=0; t<rows*(cols+1); t++) {
363 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
366 t = curs_x; curs_x = alt_x; alt_x = t;
367 t = curs_y; curs_y = alt_y; alt_y = t;
368 t = marg_t; marg_t = alt_t; alt_t = t;
369 t = marg_b; marg_b = alt_b; alt_b = t;
370 t = dec_om; dec_om = alt_om; alt_om = t;
371 t = wrap; wrap = alt_wrap; alt_wrap = t;
372 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
373 t = insert; insert = alt_ins; alt_ins = t;
374 t = cset; cset = alt_cset; alt_cset = t;
380 * Update the scroll bar.
382 static void update_sbar(void) {
385 min = (sbtop - text) / (cols+1);
386 set_sbar ((scrtop - text) / (cols+1) + rows - min,
387 (disptop - text) / (cols+1) - min,
392 * Check whether the region bounded by the two pointers intersects
393 * the scroll region, and de-select the on-screen selection if so.
395 static void check_selection (unsigned long *from, unsigned long *to) {
396 if (from < selend && selstart < to)
401 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
402 * for backward.) `sb' is TRUE if the scrolling is permitted to
403 * affect the scrollback buffer.
405 static void scroll (int topline, int botline, int lines, int sb) {
406 unsigned long *scroll_top;
407 unsigned long *newscr;
408 int scroll_size, size, i;
409 int scrtop_is_disptop = (scrtop==disptop);
410 static int recursive = 0;
412 /* Only scroll more than the window if we're doing a 10% scroll */
413 if (!recursive && lines > botline - topline + 1)
414 lines = botline - topline + 1;
416 scroll_top = scrtop + topline*(cols+1);
417 size = (lines < 0 ? -lines : lines) * (cols+1);
418 scroll_size = (botline - topline + 1) * (cols+1) - size;
420 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
422 * Since we're going to scroll the top line and we're on the
423 * scrolling screen let's also effect the scrollback buffer.
425 * This is normally done by moving the position the screen
426 * painter reads from to reduce the amount of memory copying
429 if (scroll_size >= 0 && !recursive) {
430 newscr = scrtop + lines * (cols+1);
432 if (newscr > sbbot && botline == rows-1) {
433 /* We've hit the bottom of memory, so we have to do a
434 * physical scroll. But instead of just 1 line do it
435 * by 10% of the available memory.
437 * If the scroll region isn't the whole screen then we can't
438 * do this as it stands. We would need to recover the bottom
439 * of the screen from the scroll buffer after being sure that
440 * it doesn't get deleted.
443 i = (rows+savelines)/10;
445 /* Make it simple and ensure safe recursion */
446 if ( i<savelines-1) {
448 scroll(topline, botline, i, sb);
451 newscr = scrtop - i * (cols+1);
452 if (scrtop_is_disptop) disptop = newscr;
456 newscr = scrtop + lines * (cols+1);
459 if (newscr <= sbbot) {
460 if (scrtop_is_disptop) disptop = newscr;
463 if (botline == rows-1 )
464 for (i = 0; i < size; i++)
465 scrtop[i+scroll_size] = erase_char;
470 if (botline != rows-1) {
471 /* This fastscroll only works for full window scrolls.
472 * If the caller wanted a partial one we have to reverse
473 * scroll the bottom of the screen.
475 scroll(botline-lines+1, rows-1, -lines, 0);
480 /* If we can't scroll by memory remapping do it physically.
481 * But rather than expensivly doing the scroll buffer just
482 * scroll the screen. All it means is that sometimes we choose
483 * to not add lines from a scroll region to the scroll buffer.
486 if (savelines <= 400) {
487 sbtop -= lines * (cols+1);
490 scroll_size += scroll_top - sbtop;
496 /* Ho hum, expensive scroll required. */
498 sbtop -= lines * (cols+1);
501 scroll_size += scroll_top - sbtop;
508 if (scroll_size < 0) {
515 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
516 for (i = 0; i < size; i++)
517 scroll_top[i+scroll_size] = erase_char;
518 if (selstart > scroll_top &&
519 selstart < scroll_top + size + scroll_size) {
521 if (selstart < scroll_top)
522 selstart = scroll_top;
524 if (selend > scroll_top &&
525 selend < scroll_top + size + scroll_size) {
527 if (selend < scroll_top)
530 if (scrtop_is_disptop)
533 if (disptop > scroll_top &&
534 disptop < scroll_top + size + scroll_size) {
536 if (disptop < scroll_top)
537 disptop = scroll_top;
541 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
542 for (i = 0; i < size; i++)
543 scroll_top[i] = erase_char;
544 if (selstart > scroll_top &&
545 selstart < scroll_top + size + scroll_size) {
547 if (selstart > scroll_top + size + scroll_size)
548 selstart = scroll_top + size + scroll_size;
550 if (selend > scroll_top &&
551 selend < scroll_top + size + scroll_size) {
553 if (selend > scroll_top + size + scroll_size)
554 selend = scroll_top + size + scroll_size;
556 if (scrtop_is_disptop)
558 else if (disptop > scroll_top &&
559 disptop < scroll_top + size + scroll_size) {
561 if (disptop > scroll_top + size + scroll_size)
562 disptop = scroll_top + size + scroll_size;
568 * Move the cursor to a given position, clipping at boundaries. We
569 * may or may not want to clip at the scroll margin: marg_clip is 0
570 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
571 * even _being_ outside the margins.
573 static void move (int x, int y, int marg_clip) {
579 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
581 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
595 * Save or restore the cursor and SGR mode.
597 static void save_cursor(int save) {
601 save_attr = curr_attr;
603 save_csattr = cset_attr[cset];
607 /* Make sure the window hasn't shrunk since the save */
608 if (curs_x >= cols) curs_x = cols-1;
609 if (curs_y >= rows) curs_y = rows-1;
611 curr_attr = save_attr;
613 cset_attr[cset] = save_csattr;
615 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
620 * Erase a large portion of the screen: the whole screen, or the
621 * whole line, or parts thereof.
623 static void erase_lots (int line_only, int from_begin, int to_end) {
624 unsigned long *startpos, *endpos;
627 startpos = cpos - curs_x;
628 endpos = startpos + cols;
629 /* I've removed the +1 so that the Wide screen stuff is not
630 * removed when it shouldn't be.
634 endpos = startpos + rows * (cols+1);
640 check_selection (startpos, endpos);
642 /* Clear screen also forces a full window redraw, just in case. */
643 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
646 while (startpos < endpos)
647 *startpos++ = erase_char;
651 * Insert or delete characters within the current line. n is +ve if
652 * insertion is desired, and -ve for deletion.
654 static void insch (int n) {
655 int dir = (n < 0 ? -1 : +1);
658 n = (n < 0 ? -n : n);
659 if (n > cols - curs_x)
661 m = cols - curs_x - n;
662 check_selection (cpos, cpos+n);
664 memmove (cpos, cpos+n, m*TSIZE);
666 cpos[m++] = erase_char;
668 memmove (cpos+n, cpos, m*TSIZE);
670 cpos[n] = erase_char;
675 * Toggle terminal mode `mode' to state `state'. (`query' indicates
676 * whether the mode is a DEC private one or a normal one.)
678 static void toggle_mode (int mode, int query, int state) {
679 if (query) switch (mode) {
680 case 1: /* application cursor keys */
681 app_cursor_keys = state;
683 case 2: /* VT52 mode */
686 case 3: /* 80/132 columns */
688 request_resize (state ? 132 : 80, rows, 1);
691 case 5: /* reverse video */
693 seen_disp_event = TRUE;
694 if (state) term_update();
696 case 6: /* DEC origin mode */
699 case 7: /* auto wrap */
702 case 8: /* auto key repeat */
705 case 25: /* enable/disable cursor */
706 compatibility(VT220);
708 seen_disp_event = TRUE;
710 case 47: /* alternate screen */
711 compatibility(OTHER);
716 } else switch (mode) {
717 case 4: /* set insert mode */
718 compatibility(VT102);
721 case 12: /* set echo mode */
723 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
724 * have a long RTT and the telnet client/daemon doesn't understand
727 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
728 * tty and _really_ confuses some programs.
730 compatibility(VT220);
731 ldisc = (state? &ldisc_simple : &ldisc_term);
733 case 20: /* Return sends ... */
734 cr_lf_return = state;
740 * Process an OSC sequence: set window title or icon name.
742 static void do_osc(void) {
745 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
747 osc_string[osc_strlen] = '\0';
748 switch (esc_args[0]) {
751 set_icon (osc_string);
752 if (esc_args[0] == 1)
754 /* fall through: parameter 0 means set both */
757 set_title (osc_string);
764 * Remove everything currently in `inbuf' and stick it up on the
765 * in-memory display. There's a big state machine in here to
766 * process escape sequences...
768 void term_out(void) {
771 static int beep_overload = 0;
774 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
776 c = inbuf[inbuf_reap];
779 * Optionally log the session traffic to a file. Useful for
780 * debugging and possibly also useful for actual logging.
783 static FILE *fp = NULL;
784 if (!fp) fp = fopen(logfile, "wb");
785 if (fp) fputc (c, fp);
787 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
788 * be able to display 8-bit characters, but I'll let that go 'cause
791 if( ( (c&0x60) == 0 || c == '\177') &&
792 termstate < DO_CTRLS &&
793 ( (c&0x80) == 0 || has_compat(VT220))) {
795 case '\005': /* terminal type query */
796 /* Strictly speaking this is VT100 but a VT100 defaults to
797 * no response. Other terminals respond at their option.
799 * Don't put a CR in the default string as this tends to
800 * upset some weird software.
802 * An xterm returns "xterm" (5 characters)
804 compatibility(OTHER);
805 ldisc->send ("PuTTY", 5);
809 if(beep_count>6) beep_overload=1;
813 if (curs_x == 0 && curs_y == 0)
815 else if (curs_x == 0 && curs_y > 0)
816 curs_x = cols-1, curs_y--;
822 seen_disp_event = TRUE;
825 compatibility(VT100);
829 compatibility(VT100);
834 termstate = VT52_ESC;
836 compatibility(ANSIMIN);
837 termstate = SEEN_ESC;
841 compatibility(VT220);
842 termstate = SEEN_CSI;
844 esc_args[0] = ARG_DEFAULT;
848 compatibility(VT220);
849 termstate = SEEN_OSC;
856 seen_disp_event = TRUE;
861 compatibility(VT100);
863 if (curs_y == marg_b)
864 scroll (marg_t, marg_b, 1, TRUE);
865 else if (curs_y < rows-1)
876 unsigned long *old_cpos = cpos;
877 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
881 } while (curs_x < cols-1 && !tabs[curs_x]);
883 if ((*p & LATTR_MODE) != LATTR_NORM)
885 if (curs_x >= cols/2)
895 check_selection (old_cpos, cpos);
897 seen_disp_event = TRUE;
901 else switch (termstate) {
903 /* Only graphic characters get this far, ctrls are stripped above */
905 cpos[1] |= ATTR_WRAPPED;
906 if (curs_y == marg_b)
907 scroll (marg_t, marg_b, 1, TRUE);
908 else if (curs_y < rows-1)
916 if (selstate != NO_SELECTION)
917 check_selection (cpos, cpos+1);
918 switch (cset_attr[cset]) {
919 /* Linedraw characters are different from 'ESC ( B' only
920 * for a small range, for ones outside that range make sure
921 * we use the same font as well as the same encoding.
924 if (c<0x60 || c>0x7F)
925 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
928 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
931 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
932 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
936 if (curs_x == cols) {
945 termstate = TOPLEVEL;
949 * This state is virtually identical to SEEN_ESC, with the
950 * exception that we have an OSC sequence in the pipeline,
951 * and _if_ we see a backslash, we process it.
955 termstate = TOPLEVEL;
958 /* else fall through */
960 termstate = TOPLEVEL;
962 case ' ': /* some weird sequence? */
963 compatibility(VT220);
964 termstate = IGNORE_NEXT;
966 case '[': /* enter CSI mode */
967 termstate = SEEN_CSI;
969 esc_args[0] = ARG_DEFAULT;
972 case ']': /* xterm escape sequences */
973 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
974 compatibility(OTHER);
975 termstate = SEEN_OSC;
978 case '(': /* should set GL */
979 compatibility(VT100);
982 case ')': /* should set GR */
983 compatibility(VT100);
986 case '7': /* save cursor */
987 compatibility(VT100);
990 case '8': /* restore cursor */
991 compatibility(VT100);
993 seen_disp_event = TRUE;
996 compatibility(VT100);
997 app_keypad_keys = TRUE;
1000 compatibility(VT100);
1001 app_keypad_keys = FALSE;
1003 case 'D': /* exactly equivalent to LF */
1004 compatibility(VT100);
1005 if (curs_y == marg_b)
1006 scroll (marg_t, marg_b, 1, TRUE);
1007 else if (curs_y < rows-1)
1011 seen_disp_event = TRUE;
1013 case 'E': /* exactly equivalent to CR-LF */
1014 compatibility(VT100);
1016 if (curs_y == marg_b)
1017 scroll (marg_t, marg_b, 1, TRUE);
1018 else if (curs_y < rows-1)
1022 seen_disp_event = TRUE;
1024 case 'M': /* reverse index - backwards LF */
1025 compatibility(VT100);
1026 if (curs_y == marg_t)
1027 scroll (marg_t, marg_b, -1, TRUE);
1028 else if (curs_y > 0)
1032 seen_disp_event = TRUE;
1034 case 'Z': /* terminal type query */
1035 compatibility(VT100);
1036 ldisc->send (id_string, strlen(id_string));
1038 case 'c': /* restore power-on settings */
1039 compatibility(VT100);
1042 request_resize (80, rows, 1);
1047 seen_disp_event = TRUE;
1049 case '#': /* ESC # 8 fills screen with Es :-) */
1050 compatibility(VT100);
1051 termstate = SEEN_ESCHASH;
1053 case 'H': /* set a tab */
1054 compatibility(VT100);
1055 tabs[curs_x] = TRUE;
1060 termstate = TOPLEVEL; /* default */
1063 if (esc_nargs <= ARGS_MAX) {
1064 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1065 esc_args[esc_nargs-1] = 0;
1066 esc_args[esc_nargs-1] =
1067 10 * esc_args[esc_nargs-1] + c - '0';
1069 termstate = SEEN_CSI;
1073 if (++esc_nargs <= ARGS_MAX)
1074 esc_args[esc_nargs-1] = ARG_DEFAULT;
1075 termstate = SEEN_CSI;
1079 if( esc_query ) esc_query = -1;
1080 else if( c == '?' ) esc_query = TRUE;
1082 termstate = SEEN_CSI;
1084 else switch (ANSI(c,esc_query)) {
1085 case 'A': /* move up N lines */
1086 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1087 seen_disp_event = TRUE;
1089 case 'e': /* move down N lines */
1090 compatibility(ANSI);
1092 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1093 seen_disp_event = TRUE;
1095 case 'a': /* move right N cols */
1096 compatibility(ANSI);
1098 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1099 seen_disp_event = TRUE;
1101 case 'D': /* move left N cols */
1102 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1103 seen_disp_event = TRUE;
1105 case 'E': /* move down N lines and CR */
1106 compatibility(ANSI);
1107 move (0, curs_y + def(esc_args[0], 1), 1);
1108 seen_disp_event = TRUE;
1110 case 'F': /* move up N lines and CR */
1111 compatibility(ANSI);
1112 move (0, curs_y - def(esc_args[0], 1), 1);
1113 seen_disp_event = TRUE;
1115 case 'G': case '`': /* set horizontal posn */
1116 compatibility(ANSI);
1117 move (def(esc_args[0], 1) - 1, curs_y, 0);
1118 seen_disp_event = TRUE;
1120 case 'd': /* set vertical posn */
1121 compatibility(ANSI);
1122 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1124 seen_disp_event = TRUE;
1126 case 'H': case 'f': /* set horz and vert posns at once */
1128 esc_args[1] = ARG_DEFAULT;
1129 move (def(esc_args[1], 1) - 1,
1130 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1132 seen_disp_event = TRUE;
1134 case 'J': /* erase screen or parts of it */
1136 unsigned int i = def(esc_args[0], 0) + 1;
1139 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1142 seen_disp_event = TRUE;
1144 case 'K': /* erase line or parts of it */
1146 unsigned int i = def(esc_args[0], 0) + 1;
1149 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1151 seen_disp_event = TRUE;
1153 case 'L': /* insert lines */
1154 compatibility(VT102);
1155 if (curs_y <= marg_b)
1156 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1157 seen_disp_event = TRUE;
1159 case 'M': /* delete lines */
1160 compatibility(VT102);
1161 if (curs_y <= marg_b)
1162 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1163 seen_disp_event = TRUE;
1165 case '@': /* insert chars */
1166 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1167 compatibility(VT102);
1168 insch (def(esc_args[0], 1));
1169 seen_disp_event = TRUE;
1171 case 'P': /* delete chars */
1172 compatibility(VT102);
1173 insch (-def(esc_args[0], 1));
1174 seen_disp_event = TRUE;
1176 case 'c': /* terminal type query */
1177 compatibility(VT100);
1178 /* This is the response for a VT102 */
1179 ldisc->send (id_string, strlen(id_string));
1181 case 'n': /* cursor position query */
1182 if (esc_args[0] == 6) {
1184 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1185 ldisc->send (buf, strlen(buf));
1187 else if (esc_args[0] == 5) {
1188 ldisc->send ("\033[0n", 4);
1191 case 'h': /* toggle modes to high */
1193 compatibility(VT100);
1196 for (i=0; i<esc_nargs; i++)
1197 toggle_mode (esc_args[i], esc_query, TRUE);
1200 case 'l': /* toggle modes to low */
1202 compatibility(VT100);
1205 for (i=0; i<esc_nargs; i++)
1206 toggle_mode (esc_args[i], esc_query, FALSE);
1209 case 'g': /* clear tabs */
1210 compatibility(VT100);
1211 if (esc_nargs == 1) {
1212 if (esc_args[0] == 0) {
1213 tabs[curs_x] = FALSE;
1214 } else if (esc_args[0] == 3) {
1216 for (i = 0; i < cols; i++)
1221 case 'r': /* set scroll margins */
1222 compatibility(VT100);
1223 if (esc_nargs <= 2) {
1225 top = def(esc_args[0], 1) - 1;
1226 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1227 def(esc_args[1], rows)) - 1;
1230 /* VTTEST Bug 9 - if region is less than 2 lines
1231 * don't change region.
1238 * I used to think the cursor should be
1239 * placed at the top of the newly marginned
1240 * area. Apparently not: VMS TPU falls over
1243 * Well actually it should for Origin mode - RDB
1245 curs_y = (dec_om ? marg_t : 0);
1247 seen_disp_event = TRUE;
1251 case 'm': /* set graphics rendition */
1254 * A VT100 without the AVO only had one attribute, either
1255 * underline or reverse video depending on the cursor type,
1256 * this was selected by CSI 7m.
1259 * This is DIM on the VT100-AVO and VT102
1261 * This is BLINK on the VT100-AVO and VT102+
1263 * This is INVIS on the VT100-AVO and VT102
1265 * This like 22 disables BOLD, DIM and INVIS
1267 * The ANSI colours appear on any terminal that has colour
1268 * (obviously) but the interaction between sgr0 and the
1269 * colours varies but is usually related to the background
1270 * colour erase item.
1271 * The interaction between colour attributes and the mono
1272 * ones is also very implementation dependent.
1274 * The 39 and 49 attributes are likely to be unimplemented.
1277 for (i=0; i<esc_nargs; i++) {
1278 switch (def(esc_args[i], 0)) {
1279 case 0: /* restore defaults */
1280 curr_attr = ATTR_DEFAULT; break;
1281 case 1: /* enable bold */
1282 compatibility(VT100AVO);
1283 curr_attr |= ATTR_BOLD; break;
1284 case 21: /* (enable double underline) */
1285 compatibility(OTHER);
1286 case 4: /* enable underline */
1287 compatibility(VT100AVO);
1288 curr_attr |= ATTR_UNDER; break;
1289 case 5: /* enable blink */
1290 compatibility(VT100AVO);
1291 curr_attr |= ATTR_BLINK; break;
1292 case 7: /* enable reverse video */
1293 curr_attr |= ATTR_REVERSE; break;
1294 case 22: /* disable bold */
1295 compatibility(VT220);
1296 curr_attr &= ~ATTR_BOLD; break;
1297 case 24: /* disable underline */
1298 compatibility(VT220);
1299 curr_attr &= ~ATTR_UNDER; break;
1300 case 25: /* disable blink */
1301 compatibility(VT220);
1302 curr_attr &= ~ATTR_BLINK; break;
1303 case 27: /* disable reverse video */
1304 compatibility(VT220);
1305 curr_attr &= ~ATTR_REVERSE; break;
1306 case 30: case 31: case 32: case 33:
1307 case 34: case 35: case 36: case 37:
1309 curr_attr &= ~ATTR_FGMASK;
1310 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1312 case 39: /* default-foreground */
1313 curr_attr &= ~ATTR_FGMASK;
1314 curr_attr |= ATTR_DEFFG;
1316 case 40: case 41: case 42: case 43:
1317 case 44: case 45: case 46: case 47:
1319 curr_attr &= ~ATTR_BGMASK;
1320 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1322 case 49: /* default-background */
1323 curr_attr &= ~ATTR_BGMASK;
1324 curr_attr |= ATTR_DEFBG;
1331 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1335 case 's': /* save cursor */
1338 case 'u': /* restore cursor */
1339 save_cursor (FALSE);
1340 seen_disp_event = TRUE;
1342 case 't': /* set page size - ie window height */
1344 * VT340/VT420 sequence DECSLPP, DEC only allows values
1345 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1346 * illegal values (eg first arg 1..9) for window changing
1349 compatibility(VT340TEXT);
1350 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1351 request_resize (cols, def(esc_args[0], 24), 0);
1355 case ANSI('|', '*'):
1356 /* VT420 sequence DECSNLS
1357 * Set number of lines on screen
1358 * VT420 uses VGA like hardware and can support any size in
1359 * reasonable range (24..49 AIUI) with no default specified.
1361 compatibility(VT420);
1362 if (esc_nargs==1 && esc_args[0]>0) {
1363 request_resize (cols, def(esc_args[0], cfg.height), 0);
1367 case ANSI('|', '$'):
1368 /* VT340/VT420 sequence DECSCPP
1369 * Set number of columns per page
1370 * Docs imply range is only 80 or 132, but I'll allow any.
1372 compatibility(VT340TEXT);
1374 request_resize (def(esc_args[0], cfg.width), rows, 0);
1378 case 'X': /* write N spaces w/o moving cursor */
1379 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1380 compatibility(ANSIMIN);
1382 int n = def(esc_args[0], 1);
1383 unsigned long *p = cpos;
1384 if (n > cols - curs_x)
1386 check_selection (cpos, cpos+n);
1389 seen_disp_event = TRUE;
1392 case 'x': /* report terminal characteristics */
1393 compatibility(VT100);
1396 int i = def(esc_args[0], 0);
1397 if (i == 0 || i == 1) {
1398 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1400 ldisc->send (buf, 20);
1405 compatibility(OTHER);
1406 use_bce = (esc_args[0]<=0);
1407 erase_char = ERASE_CHAR;
1409 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1412 compatibility(OTHER);
1413 blink_is_real = (esc_args[0]>=1);
1416 /* Allow the host to make this emulator a 'perfect' VT102.
1417 * This first appeared in the VT220, but we do need to get
1418 * back to PuTTY mode so I won't check it.
1420 * The arg == 60 is a PuTTY extension.
1421 * The 2nd arg, 8bit vs 7bit is not obeyed.
1423 * Setting VT102 mode should also change the Fkeys to
1424 * generate PF* codes as a real VT102 has no Fkeys.
1425 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1428 * Note ESC c will NOT change this!
1431 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1432 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1433 else compatibility_level = TM_PUTTY;
1439 /* VT100 only here, checked above */
1442 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1445 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1448 default: /* specifically, 'B' */
1449 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1452 if( !has_compat(VT220) || c != '%' )
1453 termstate = TOPLEVEL;
1458 case 'P': /* Linux palette sequence */
1459 termstate = SEEN_OSC_P;
1462 case 'R': /* Linux palette reset */
1465 termstate = TOPLEVEL;
1467 case 'W': /* word-set */
1468 termstate = SEEN_OSC_W;
1471 case '0': case '1': case '2': case '3': case '4':
1472 case '5': case '6': case '7': case '8': case '9':
1473 esc_args[0] = 10 * esc_args[0] + c - '0';
1477 * Grotty hack to support xterm and DECterm title
1478 * sequences concurrently.
1480 if (esc_args[0] == 2) {
1484 /* else fall through */
1486 termstate = OSC_STRING;
1492 * This OSC stuff is EVIL. It takes just one character to get into
1493 * sysline mode and it's not initially obvious how to get out.
1494 * So I've added CR and LF as string aborts.
1495 * This shouldn't effect compatibility as I believe embedded
1496 * control characters are supposed to be interpreted (maybe?)
1497 * and they don't display anything useful anyway.
1501 if (c == '\n' || c == '\r') {
1502 termstate = TOPLEVEL;
1503 } else if (c == 0234 || c == '\007' ) {
1505 * These characters terminate the string; ST and BEL
1506 * terminate the sequence and trigger instant
1507 * processing of it, whereas ESC goes back to SEEN_ESC
1508 * mode unless it is followed by \, in which case it is
1509 * synonymous with ST in the first place.
1512 termstate = TOPLEVEL;
1513 } else if (c == '\033')
1514 termstate = OSC_MAYBE_ST;
1515 else if (osc_strlen < OSC_STR_MAX)
1516 osc_string[osc_strlen++] = c;
1520 int max = (osc_strlen == 0 ? 21 : 16);
1522 if (c >= '0' && c <= '9')
1524 else if (c >= 'A' && c <= 'A'+max-10)
1526 else if (c >= 'a' && c <= 'a'+max-10)
1529 termstate = TOPLEVEL;
1530 osc_string[osc_strlen++] = val;
1531 if (osc_strlen >= 7) {
1532 palette_set (osc_string[0],
1533 osc_string[1] * 16 + osc_string[2],
1534 osc_string[3] * 16 + osc_string[4],
1535 osc_string[5] * 16 + osc_string[6]);
1537 termstate = TOPLEVEL;
1543 case '0': case '1': case '2': case '3': case '4':
1544 case '5': case '6': case '7': case '8': case '9':
1545 esc_args[0] = 10 * esc_args[0] + c - '0';
1548 termstate = OSC_STRING;
1555 unsigned long nlattr;
1561 n = rows * (cols+1);
1563 *p++ = ATTR_DEFAULT | 'E';
1565 seen_disp_event = TRUE;
1566 check_selection (scrtop, scrtop + rows * (cols+1));
1569 case '3': nlattr = LATTR_TOP; if(0) {
1570 case '4': nlattr = LATTR_BOT; } if(0) {
1571 case '5': nlattr = LATTR_NORM; } if(0) {
1572 case '6': nlattr = LATTR_WIDE; }
1574 p = scrtop + curs_y * (cols+1) + cols;
1579 termstate = TOPLEVEL;
1582 termstate = TOPLEVEL;
1583 seen_disp_event = TRUE;
1586 move (curs_x, curs_y - 1, 1);
1589 move (curs_x, curs_y + 1, 1);
1592 move (curs_x + 1, curs_y, 1);
1595 move (curs_x - 1, curs_y, 1);
1598 cset_attr[cset=0] = ATTR_LINEDRW;
1601 cset_attr[cset=0] = ATTR_ASCII;
1608 scroll (0, rows-1, -1, TRUE);
1609 else if (curs_y > 0)
1615 erase_lots(FALSE, FALSE, TRUE);
1619 erase_lots(TRUE, FALSE, TRUE);
1622 /* XXX Print cursor line */
1625 /* XXX Start controller mode */
1628 /* XXX Stop controller mode */
1631 termstate = VT52_Y1;
1634 ldisc->send ("\033/Z", 3);
1637 app_cursor_keys = TRUE;
1640 app_cursor_keys = FALSE;
1643 /* XXX This should switch to VT100 mode not current or default
1644 * VT mode. But this will only have effect in a VT220+
1650 /* XXX Enter auto print mode */
1653 /* XXX Exit auto print mode */
1656 /* XXX Print screen */
1661 termstate = VT52_Y2;
1662 move(curs_x, c-' ', 0);
1665 termstate = TOPLEVEL;
1666 move(c-' ', curs_y, 0);
1669 if (selstate != NO_SELECTION)
1670 check_selection (cpos, cpos+1);
1676 if(!beep_count) beep_overload=0;
1678 else if(beep_count && beep_count<5 && cfg.beep)
1683 * Compare two lines to determine whether they are sufficiently
1684 * alike to scroll-optimise one to the other. Return the degree of
1687 static int linecmp (unsigned long *a, unsigned long *b) {
1690 for (i=n=0; i < cols; i++)
1691 n += (*a++ == *b++);
1696 * Given a context, update the window. Out of paranoia, we don't
1697 * allow WM_PAINT responses to do scrolling optimisations.
1699 static void do_paint (Context ctx, int may_optimise){
1700 int i, j, start, our_curs_y;
1701 unsigned long attr, rv, cursor;
1706 if (blinker || !cfg.blink_cur)
1707 cursor = ATTR_ACTCURS;
1712 cursor = ATTR_PASCURS;
1715 rv = (rvideo ? ATTR_REVERSE : 0);
1716 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1718 for (i=0; i<rows; i++) {
1719 int idx = i*(cols+1);
1720 int lattr = (disptop[idx+cols] & LATTR_MODE);
1721 for (j=0; j<=cols; j++,idx++) {
1722 unsigned long *d = disptop+idx;
1723 wanttext[idx] = lattr | ((*d ^ rv
1724 ^ (selstart <= d && d < selend ?
1725 ATTR_REVERSE : 0)) |
1726 (i==our_curs_y && j==curs_x ? cursor : 0));
1728 if (blink_is_real) {
1729 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1731 wanttext[idx] &= ATTR_MASK;
1732 wanttext[idx] += ' ';
1734 wanttext[idx] &= ~ATTR_BLINK;
1740 * We would perform scrolling optimisations in here, if they
1741 * didn't have a nasty tendency to cause the whole sodding
1742 * program to hang for a second at speed-critical moments.
1743 * We'll leave it well alone...
1746 for (i=0; i<rows; i++) {
1747 int idx = i*(cols+1);
1748 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1750 for (j=0; j<=cols; j++,idx++) {
1751 unsigned long t = wanttext[idx];
1752 int needs_update = (j < cols && t != disptext[idx]);
1753 int keep_going = (start != -1 && needs_update &&
1754 (t & ATTR_MASK) == attr &&
1755 j-start < sizeof(ch));
1756 if (start != -1 && !keep_going) {
1757 do_text (ctx, start, i, ch, j-start, attr, lattr);
1763 attr = t & ATTR_MASK;
1765 ch[j-start] = (char) (t & CHAR_MASK);
1773 * Flick the switch that says if blinking things should be shown or hidden.
1776 void term_blink(int flg) {
1777 static long last_blink = 0;
1778 static long last_tblink = 0;
1779 long now, blink_diff;
1781 now = GetTickCount();
1782 blink_diff = now-last_tblink;
1784 /* Make sure the text blinks no more than 2Hz */
1785 if (blink_diff<0 || blink_diff>450)
1788 tblinker = !tblinker;
1797 blink_diff = now-last_blink;
1799 /* Make sure the cursor blinks no more than 2Hz */
1800 if (blink_diff>=0 && blink_diff<450)
1808 * Invalidate the whole screen so it will be repainted in full.
1810 void term_invalidate(void) {
1813 for (i=0; i<rows*(cols+1); i++)
1814 disptext[i] = ATTR_INVALID;
1818 * Paint the window in response to a WM_PAINT message.
1820 void term_paint (Context ctx, int l, int t, int r, int b) {
1821 int i, j, left, top, right, bottom;
1823 left = l / font_width;
1824 right = (r - 1) / font_width;
1825 top = t / font_height;
1826 bottom = (b - 1) / font_height;
1827 for (i = top; i <= bottom && i < rows ; i++)
1829 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1830 for (j = left; j <= right && j < cols ; j++)
1831 disptext[i*(cols+1)+j] = ATTR_INVALID;
1833 for (j = left/2; j <= right/2+1 && j < cols ; j++)
1834 disptext[i*(cols+1)+j] = ATTR_INVALID;
1837 /* This should happen soon enough, also for some reason it sometimes
1838 * fails to actually do anything when re-sizing ... painting the wrong
1840 do_paint (ctx, FALSE);
1845 * Attempt to scroll the scrollback. The second parameter gives the
1846 * position we want to scroll to; the first is +1 to denote that
1847 * this position is relative to the beginning of the scrollback, -1
1848 * to denote it is relative to the end, and 0 to denote that it is
1849 * relative to the current position.
1851 void term_scroll (int rel, int where) {
1852 int n = where * (cols+1);
1854 disptop = (rel < 0 ? scrtop :
1855 rel > 0 ? sbtop : disptop) + n;
1856 if (disptop < sbtop)
1858 if (disptop > scrtop)
1865 * Spread the selection outwards according to the selection mode.
1867 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1868 unsigned long *linestart, *lineend;
1872 x = (p - text) % (cols+1);
1874 lineend = linestart + cols;
1879 * In this mode, every character is a separate unit, except
1880 * for runs of spaces at the end of a non-wrapping line.
1882 if (!(linestart[cols] & ATTR_WRAPPED)) {
1883 unsigned long *q = lineend;
1884 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1889 p = (dir == -1 ? q : lineend - 1);
1894 * In this mode, the units are maximal runs of characters
1895 * whose `wordness' has the same value.
1897 wvalue = wordness[*p & CHAR_MASK];
1899 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1902 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1908 * In this mode, every line is a unit.
1910 p = (dir == -1 ? linestart : lineend - 1);
1916 static void sel_spread (void) {
1917 selstart = sel_spread_half (selstart, -1);
1918 selend = sel_spread_half (selend - 1, +1) + 1;
1921 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1922 unsigned long *selpoint;
1925 if (y>=rows) y = rows-1;
1933 if (x>=cols) x = cols-1;
1935 selpoint = disptop + y * (cols+1);
1936 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
1941 if (b == MB_SELECT && a == MA_CLICK) {
1943 selstate = ABOUT_TO;
1944 selanchor = selpoint;
1946 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1948 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1949 selstate = DRAGGING;
1950 selstart = selanchor = selpoint;
1951 selend = selstart + 1;
1953 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1954 (b == MB_EXTEND && a != MA_RELEASE)) {
1955 if (selstate == ABOUT_TO && selanchor == selpoint)
1957 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1958 if (selpoint-selstart < (selend-selstart)/2)
1959 selanchor = selend - 1;
1961 selanchor = selstart;
1962 selstate = DRAGGING;
1964 if (selstate != ABOUT_TO && selstate != DRAGGING)
1965 selanchor = selpoint;
1966 selstate = DRAGGING;
1967 if (selpoint < selanchor) {
1968 selstart = selpoint;
1969 selend = selanchor + 1;
1971 selstart = selanchor;
1972 selend = selpoint + 1;
1975 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1976 if (selstate == DRAGGING) {
1978 * We've completed a selection. We now transfer the
1979 * data to the clipboard.
1981 unsigned char *p = selspace;
1982 unsigned long *q = selstart;
1984 while (q < selend) {
1986 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1987 unsigned long *nlpos = lineend;
1989 if (!(*nlpos & ATTR_WRAPPED)) {
1990 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1995 while (q < nlpos && q < selend)
1996 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1999 for (i=0; i<sizeof(sel_nl); i++)
2002 q = lineend + 1; /* start of next line */
2004 write_clip (selspace, p - selspace, FALSE);
2005 selstate = SELECTED;
2007 selstate = NO_SELECTION;
2008 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2012 get_clip((void **) &data, &len);
2016 if (paste_buffer) sfree(paste_buffer);
2017 paste_pos = paste_hold = paste_len = 0;
2018 paste_buffer = smalloc(len);
2021 while (p < data+len) {
2022 while (p < data+len &&
2023 !(p <= data+len-sizeof(sel_nl) &&
2024 !memcmp(p, sel_nl, sizeof(sel_nl))))
2032 c=xlat_kbd2tty(q[i]);
2033 paste_buffer[paste_len++] = c;
2037 if (p <= data+len-sizeof(sel_nl) &&
2038 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2039 paste_buffer[paste_len++] = '\r';
2040 p += sizeof(sel_nl);
2045 /* Assume a small paste will be OK in one go. */
2046 if (paste_len<256) {
2047 ldisc->send (paste_buffer, paste_len);
2048 if (paste_buffer) sfree(paste_buffer);
2050 paste_pos = paste_hold = paste_len = 0;
2053 get_clip(NULL, NULL);
2059 void term_nopaste() {
2060 if(paste_len == 0) return;
2061 sfree(paste_buffer);
2067 static long last_paste = 0;
2068 long now, paste_diff;
2070 if(paste_len == 0) return;
2072 /* Don't wait forever to paste */
2074 now = GetTickCount();
2075 paste_diff = now-last_paste;
2076 if (paste_diff>=0 && paste_diff<450)
2081 while(paste_pos<paste_len)
2083 char c = paste_buffer[paste_pos++];
2084 ldisc->send (&c, 1);
2091 sfree(paste_buffer);
2096 static void deselect (void) {
2097 selstate = NO_SELECTION;
2098 selstart = selend = scrtop;
2101 void term_deselect (void) {