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);
219 * Same as power_on(), but an external function.
221 void term_pwron(void) {
230 * Clear the scrollback.
232 void term_clrsb(void) {
233 disptop = sbtop = scrtop;
238 * Initialise the terminal.
240 void term_init(void) {
241 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
242 disptext = wanttext = NULL;
251 * Set up the terminal for a given size.
253 void term_size(int newrows, int newcols, int newsavelines) {
254 unsigned long *newtext, *newdisp, *newwant, *newalt;
255 int i, j, crows, ccols;
257 int save_alt_which = alt_which;
259 if (newrows == rows && newcols == cols && newsavelines == savelines)
260 return; /* nothing to do */
266 alt_b = marg_b = newrows - 1;
268 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
269 sbbot = newtext + newsavelines*(newcols+1);
270 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
271 newtext[i] = erase_char;
273 crows = rows + (scrtop - sbtop) / (cols+1);
274 if (crows > newrows+newsavelines)
275 crows = newrows+newsavelines;
279 disptop = newtext + (crows-newrows)*(newcols+1);
280 ccols = (cols < newcols ? cols : newcols);
281 for (i=0; i<crows; i++) {
282 int oldidx = (rows - crows + i) * (cols+1);
283 int newidx = (newrows - crows + i) * (newcols+1);
285 for (j=0; j<ccols; j++)
286 disptop[newidx+j] = scrtop[oldidx+j];
287 disptop[newidx+newcols] =
288 (cols == newcols ? scrtop[oldidx+cols]
289 : (scrtop[oldidx+cols]&LATTR_MODE));
293 sbtop = disptop = newtext;
299 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
300 for (i=0; i<newrows*(newcols+1); i++)
301 newdisp[i] = ATTR_INVALID;
305 newwant = smalloc (newrows*(newcols+1)*TSIZE);
306 for (i=0; i<newrows*(newcols+1); i++)
307 newwant[i] = ATTR_INVALID;
311 newalt = smalloc (newrows*(newcols+1)*TSIZE);
312 for (i=0; i<newrows*(newcols+1); i++)
313 newalt[i] = erase_char;
318 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
320 tabs = srealloc (tabs, newcols*sizeof(*tabs));
323 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
324 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
328 curs_y += newrows - rows;
331 if (curs_y >= newrows)
333 if (curs_x >= newcols)
336 wrapnext = alt_wnext = FALSE;
340 savelines = newsavelines;
343 swap_screen(save_alt_which);
352 static void swap_screen (int which) {
356 if (which == alt_which)
361 for (t=0; t<rows*(cols+1); t++) {
362 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
365 t = curs_x; curs_x = alt_x; alt_x = t;
366 t = curs_y; curs_y = alt_y; alt_y = t;
367 t = marg_t; marg_t = alt_t; alt_t = t;
368 t = marg_b; marg_b = alt_b; alt_b = t;
369 t = dec_om; dec_om = alt_om; alt_om = t;
370 t = wrap; wrap = alt_wrap; alt_wrap = t;
371 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
372 t = insert; insert = alt_ins; alt_ins = t;
373 t = cset; cset = alt_cset; alt_cset = t;
379 * Update the scroll bar.
381 static void update_sbar(void) {
384 min = (sbtop - text) / (cols+1);
385 set_sbar ((scrtop - text) / (cols+1) + rows - min,
386 (disptop - text) / (cols+1) - min,
391 * Check whether the region bounded by the two pointers intersects
392 * the scroll region, and de-select the on-screen selection if so.
394 static void check_selection (unsigned long *from, unsigned long *to) {
395 if (from < selend && selstart < to)
400 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
401 * for backward.) `sb' is TRUE if the scrolling is permitted to
402 * affect the scrollback buffer.
404 static void scroll (int topline, int botline, int lines, int sb) {
405 unsigned long *scroll_top;
406 unsigned long *newscr;
407 int scroll_size, size, i;
408 int scrtop_is_disptop = (scrtop==disptop);
409 static int recursive = 0;
411 /* Only scroll more than the window if we're doing a 10% scroll */
412 if (!recursive && lines > botline - topline + 1)
413 lines = botline - topline + 1;
415 scroll_top = scrtop + topline*(cols+1);
416 size = (lines < 0 ? -lines : lines) * (cols+1);
417 scroll_size = (botline - topline + 1) * (cols+1) - size;
419 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
421 * Since we're going to scroll the top line and we're on the
422 * scrolling screen let's also effect the scrollback buffer.
424 * This is normally done by moving the position the screen
425 * painter reads from to reduce the amount of memory copying
428 if (scroll_size >= 0 && !recursive) {
429 newscr = scrtop + lines * (cols+1);
431 if (newscr > sbbot && botline == rows-1) {
432 /* We've hit the bottom of memory, so we have to do a
433 * physical scroll. But instead of just 1 line do it
434 * by 10% of the available memory.
436 * If the scroll region isn't the whole screen then we can't
437 * do this as it stands. We would need to recover the bottom
438 * of the screen from the scroll buffer after being sure that
439 * it doesn't get deleted.
442 i = (rows+savelines)/10;
444 /* Make it simple and ensure safe recursion */
445 if ( i<savelines-1) {
447 scroll(topline, botline, i, sb);
450 newscr = scrtop - i * (cols+1);
451 if (scrtop_is_disptop) disptop = newscr;
455 newscr = scrtop + lines * (cols+1);
458 if (newscr <= sbbot) {
459 if (scrtop_is_disptop) disptop = newscr;
462 if (botline == rows-1 )
463 for (i = 0; i < size; i++)
464 scrtop[i+scroll_size] = erase_char;
469 if (botline != rows-1) {
470 /* This fastscroll only works for full window scrolls.
471 * If the caller wanted a partial one we have to reverse
472 * scroll the bottom of the screen.
474 scroll(botline-lines+1, rows-1, -lines, 0);
479 /* If we can't scroll by memory remapping do it physically.
480 * But rather than expensivly doing the scroll buffer just
481 * scroll the screen. All it means is that sometimes we choose
482 * to not add lines from a scroll region to the scroll buffer.
485 if (savelines <= 400) {
486 sbtop -= lines * (cols+1);
489 scroll_size += scroll_top - sbtop;
495 /* Ho hum, expensive scroll required. */
497 sbtop -= lines * (cols+1);
500 scroll_size += scroll_top - sbtop;
507 if (scroll_size < 0) {
514 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
515 for (i = 0; i < size; i++)
516 scroll_top[i+scroll_size] = erase_char;
517 if (selstart > scroll_top &&
518 selstart < scroll_top + size + scroll_size) {
520 if (selstart < scroll_top)
521 selstart = scroll_top;
523 if (selend > scroll_top &&
524 selend < scroll_top + size + scroll_size) {
526 if (selend < scroll_top)
529 if (scrtop_is_disptop)
532 if (disptop > scroll_top &&
533 disptop < scroll_top + size + scroll_size) {
535 if (disptop < scroll_top)
536 disptop = scroll_top;
540 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
541 for (i = 0; i < size; i++)
542 scroll_top[i] = erase_char;
543 if (selstart > scroll_top &&
544 selstart < scroll_top + size + scroll_size) {
546 if (selstart > scroll_top + size + scroll_size)
547 selstart = scroll_top + size + scroll_size;
549 if (selend > scroll_top &&
550 selend < scroll_top + size + scroll_size) {
552 if (selend > scroll_top + size + scroll_size)
553 selend = scroll_top + size + scroll_size;
555 if (scrtop_is_disptop)
557 else if (disptop > scroll_top &&
558 disptop < scroll_top + size + scroll_size) {
560 if (disptop > scroll_top + size + scroll_size)
561 disptop = scroll_top + size + scroll_size;
567 * Move the cursor to a given position, clipping at boundaries. We
568 * may or may not want to clip at the scroll margin: marg_clip is 0
569 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
570 * even _being_ outside the margins.
572 static void move (int x, int y, int marg_clip) {
578 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
580 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
594 * Save or restore the cursor and SGR mode.
596 static void save_cursor(int save) {
600 save_attr = curr_attr;
602 save_csattr = cset_attr[cset];
606 /* Make sure the window hasn't shrunk since the save */
607 if (curs_x >= cols) curs_x = cols-1;
608 if (curs_y >= rows) curs_y = rows-1;
610 curr_attr = save_attr;
612 cset_attr[cset] = save_csattr;
614 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
619 * Erase a large portion of the screen: the whole screen, or the
620 * whole line, or parts thereof.
622 static void erase_lots (int line_only, int from_begin, int to_end) {
623 unsigned long *startpos, *endpos;
626 startpos = cpos - curs_x;
627 endpos = startpos + cols;
628 /* I've removed the +1 so that the Wide screen stuff is not
629 * removed when it shouldn't be.
633 endpos = startpos + rows * (cols+1);
639 check_selection (startpos, endpos);
641 /* Clear screen also forces a full window redraw, just in case. */
642 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
645 while (startpos < endpos)
646 *startpos++ = erase_char;
650 * Insert or delete characters within the current line. n is +ve if
651 * insertion is desired, and -ve for deletion.
653 static void insch (int n) {
654 int dir = (n < 0 ? -1 : +1);
657 n = (n < 0 ? -n : n);
658 if (n > cols - curs_x)
660 m = cols - curs_x - n;
661 check_selection (cpos, cpos+n);
663 memmove (cpos, cpos+n, m*TSIZE);
665 cpos[m++] = erase_char;
667 memmove (cpos+n, cpos, m*TSIZE);
669 cpos[n] = erase_char;
674 * Toggle terminal mode `mode' to state `state'. (`query' indicates
675 * whether the mode is a DEC private one or a normal one.)
677 static void toggle_mode (int mode, int query, int state) {
678 if (query) switch (mode) {
679 case 1: /* application cursor keys */
680 app_cursor_keys = state;
682 case 2: /* VT52 mode */
685 case 3: /* 80/132 columns */
687 request_resize (state ? 132 : 80, rows, 1);
690 case 5: /* reverse video */
692 seen_disp_event = TRUE;
693 if (state) term_update();
695 case 6: /* DEC origin mode */
698 case 7: /* auto wrap */
701 case 8: /* auto key repeat */
704 case 25: /* enable/disable cursor */
705 compatibility(VT220);
707 seen_disp_event = TRUE;
709 case 47: /* alternate screen */
710 compatibility(OTHER);
715 } else switch (mode) {
716 case 4: /* set insert mode */
717 compatibility(VT102);
720 case 12: /* set echo mode */
722 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
723 * have a long RTT and the telnet client/daemon doesn't understand
726 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
727 * tty and _really_ confuses some programs.
729 compatibility(VT220);
730 ldisc = (state? &ldisc_simple : &ldisc_term);
732 case 20: /* Return sends ... */
733 cr_lf_return = state;
739 * Process an OSC sequence: set window title or icon name.
741 static void do_osc(void) {
744 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
746 osc_string[osc_strlen] = '\0';
747 switch (esc_args[0]) {
750 set_icon (osc_string);
751 if (esc_args[0] == 1)
753 /* fall through: parameter 0 means set both */
756 set_title (osc_string);
763 * Remove everything currently in `inbuf' and stick it up on the
764 * in-memory display. There's a big state machine in here to
765 * process escape sequences...
767 void term_out(void) {
770 static int beep_overload = 0;
773 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
775 c = inbuf[inbuf_reap];
778 * Optionally log the session traffic to a file. Useful for
779 * debugging and possibly also useful for actual logging.
782 static FILE *fp = NULL;
783 if (!fp) fp = fopen(logfile, "wb");
784 if (fp) fputc (c, fp);
786 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
787 * be able to display 8-bit characters, but I'll let that go 'cause
790 if( ( (c&0x60) == 0 || c == '\177') &&
791 termstate < DO_CTRLS &&
792 ( (c&0x80) == 0 || has_compat(VT220))) {
794 case '\005': /* terminal type query */
795 /* Strictly speaking this is VT100 but a VT100 defaults to
796 * no response. Other terminals respond at their option.
798 * Don't put a CR in the default string as this tends to
799 * upset some weird software.
801 * An xterm returns "xterm" (5 characters)
803 compatibility(OTHER);
804 ldisc->send ("PuTTY", 5);
808 if(beep_count>6) beep_overload=1;
812 if (curs_x == 0 && curs_y == 0)
814 else if (curs_x == 0 && curs_y > 0)
815 curs_x = cols-1, curs_y--;
821 seen_disp_event = TRUE;
824 compatibility(VT100);
828 compatibility(VT100);
833 termstate = VT52_ESC;
835 compatibility(ANSIMIN);
836 termstate = SEEN_ESC;
840 compatibility(VT220);
841 termstate = SEEN_CSI;
843 esc_args[0] = ARG_DEFAULT;
847 compatibility(VT220);
848 termstate = SEEN_OSC;
855 seen_disp_event = TRUE;
860 compatibility(VT100);
862 if (curs_y == marg_b)
863 scroll (marg_t, marg_b, 1, TRUE);
864 else if (curs_y < rows-1)
875 unsigned long *old_cpos = cpos;
876 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
880 } while (curs_x < cols-1 && !tabs[curs_x]);
882 if ((*p & LATTR_MODE) != LATTR_NORM)
884 if (curs_x >= cols/2)
894 check_selection (old_cpos, cpos);
896 seen_disp_event = TRUE;
900 else switch (termstate) {
902 /* Only graphic characters get this far, ctrls are stripped above */
904 cpos[1] |= ATTR_WRAPPED;
905 if (curs_y == marg_b)
906 scroll (marg_t, marg_b, 1, TRUE);
907 else if (curs_y < rows-1)
915 if (selstate != NO_SELECTION)
916 check_selection (cpos, cpos+1);
917 switch (cset_attr[cset]) {
918 /* Linedraw characters are different from 'ESC ( B' only
919 * for a small range, for ones outside that range make sure
920 * we use the same font as well as the same encoding.
923 if (c<0x60 || c>0x7F)
924 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
927 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
930 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
931 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
935 if (curs_x == cols) {
944 termstate = TOPLEVEL;
948 * This state is virtually identical to SEEN_ESC, with the
949 * exception that we have an OSC sequence in the pipeline,
950 * and _if_ we see a backslash, we process it.
954 termstate = TOPLEVEL;
957 /* else fall through */
959 termstate = TOPLEVEL;
961 case ' ': /* some weird sequence? */
962 compatibility(VT220);
963 termstate = IGNORE_NEXT;
965 case '[': /* enter CSI mode */
966 termstate = SEEN_CSI;
968 esc_args[0] = ARG_DEFAULT;
971 case ']': /* xterm escape sequences */
972 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
973 compatibility(OTHER);
974 termstate = SEEN_OSC;
977 case '(': /* should set GL */
978 compatibility(VT100);
981 case ')': /* should set GR */
982 compatibility(VT100);
985 case '7': /* save cursor */
986 compatibility(VT100);
989 case '8': /* restore cursor */
990 compatibility(VT100);
992 seen_disp_event = TRUE;
995 compatibility(VT100);
996 app_keypad_keys = TRUE;
999 compatibility(VT100);
1000 app_keypad_keys = FALSE;
1002 case 'D': /* exactly equivalent to LF */
1003 compatibility(VT100);
1004 if (curs_y == marg_b)
1005 scroll (marg_t, marg_b, 1, TRUE);
1006 else if (curs_y < rows-1)
1010 seen_disp_event = TRUE;
1012 case 'E': /* exactly equivalent to CR-LF */
1013 compatibility(VT100);
1015 if (curs_y == marg_b)
1016 scroll (marg_t, marg_b, 1, TRUE);
1017 else if (curs_y < rows-1)
1021 seen_disp_event = TRUE;
1023 case 'M': /* reverse index - backwards LF */
1024 compatibility(VT100);
1025 if (curs_y == marg_t)
1026 scroll (marg_t, marg_b, -1, TRUE);
1027 else if (curs_y > 0)
1031 seen_disp_event = TRUE;
1033 case 'Z': /* terminal type query */
1034 compatibility(VT100);
1035 ldisc->send (id_string, strlen(id_string));
1037 case 'c': /* restore power-on settings */
1038 compatibility(VT100);
1041 request_resize (80, rows, 1);
1046 seen_disp_event = TRUE;
1048 case '#': /* ESC # 8 fills screen with Es :-) */
1049 compatibility(VT100);
1050 termstate = SEEN_ESCHASH;
1052 case 'H': /* set a tab */
1053 compatibility(VT100);
1054 tabs[curs_x] = TRUE;
1059 termstate = TOPLEVEL; /* default */
1062 if (esc_nargs <= ARGS_MAX) {
1063 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1064 esc_args[esc_nargs-1] = 0;
1065 esc_args[esc_nargs-1] =
1066 10 * esc_args[esc_nargs-1] + c - '0';
1068 termstate = SEEN_CSI;
1072 if (++esc_nargs <= ARGS_MAX)
1073 esc_args[esc_nargs-1] = ARG_DEFAULT;
1074 termstate = SEEN_CSI;
1078 if( esc_query ) esc_query = -1;
1079 else if( c == '?' ) esc_query = TRUE;
1081 termstate = SEEN_CSI;
1083 else switch (ANSI(c,esc_query)) {
1084 case 'A': /* move up N lines */
1085 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1086 seen_disp_event = TRUE;
1088 case 'e': /* move down N lines */
1089 compatibility(ANSI);
1091 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1092 seen_disp_event = TRUE;
1094 case 'a': /* move right N cols */
1095 compatibility(ANSI);
1097 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1098 seen_disp_event = TRUE;
1100 case 'D': /* move left N cols */
1101 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1102 seen_disp_event = TRUE;
1104 case 'E': /* move down N lines and CR */
1105 compatibility(ANSI);
1106 move (0, curs_y + def(esc_args[0], 1), 1);
1107 seen_disp_event = TRUE;
1109 case 'F': /* move up N lines and CR */
1110 compatibility(ANSI);
1111 move (0, curs_y - def(esc_args[0], 1), 1);
1112 seen_disp_event = TRUE;
1114 case 'G': case '`': /* set horizontal posn */
1115 compatibility(ANSI);
1116 move (def(esc_args[0], 1) - 1, curs_y, 0);
1117 seen_disp_event = TRUE;
1119 case 'd': /* set vertical posn */
1120 compatibility(ANSI);
1121 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1123 seen_disp_event = TRUE;
1125 case 'H': case 'f': /* set horz and vert posns at once */
1127 esc_args[1] = ARG_DEFAULT;
1128 move (def(esc_args[1], 1) - 1,
1129 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1131 seen_disp_event = TRUE;
1133 case 'J': /* erase screen or parts of it */
1135 unsigned int i = def(esc_args[0], 0) + 1;
1138 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1141 seen_disp_event = TRUE;
1143 case 'K': /* erase line or parts of it */
1145 unsigned int i = def(esc_args[0], 0) + 1;
1148 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1150 seen_disp_event = TRUE;
1152 case 'L': /* insert lines */
1153 compatibility(VT102);
1154 if (curs_y <= marg_b)
1155 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1156 seen_disp_event = TRUE;
1158 case 'M': /* delete lines */
1159 compatibility(VT102);
1160 if (curs_y <= marg_b)
1161 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1162 seen_disp_event = TRUE;
1164 case '@': /* insert chars */
1165 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1166 compatibility(VT102);
1167 insch (def(esc_args[0], 1));
1168 seen_disp_event = TRUE;
1170 case 'P': /* delete chars */
1171 compatibility(VT102);
1172 insch (-def(esc_args[0], 1));
1173 seen_disp_event = TRUE;
1175 case 'c': /* terminal type query */
1176 compatibility(VT100);
1177 /* This is the response for a VT102 */
1178 ldisc->send (id_string, strlen(id_string));
1180 case 'n': /* cursor position query */
1181 if (esc_args[0] == 6) {
1183 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1184 ldisc->send (buf, strlen(buf));
1186 else if (esc_args[0] == 5) {
1187 ldisc->send ("\033[0n", 4);
1190 case 'h': /* toggle modes to high */
1192 compatibility(VT100);
1195 for (i=0; i<esc_nargs; i++)
1196 toggle_mode (esc_args[i], esc_query, TRUE);
1199 case 'l': /* toggle modes to low */
1201 compatibility(VT100);
1204 for (i=0; i<esc_nargs; i++)
1205 toggle_mode (esc_args[i], esc_query, FALSE);
1208 case 'g': /* clear tabs */
1209 compatibility(VT100);
1210 if (esc_nargs == 1) {
1211 if (esc_args[0] == 0) {
1212 tabs[curs_x] = FALSE;
1213 } else if (esc_args[0] == 3) {
1215 for (i = 0; i < cols; i++)
1220 case 'r': /* set scroll margins */
1221 compatibility(VT100);
1222 if (esc_nargs <= 2) {
1224 /* VTTEST Bug 9 if the first arg is default _or_ zero
1225 * this is a full screen region irrespective of 2nd arg.
1227 if (esc_args[0] <= 0) {
1231 top = def(esc_args[0], 1) - 1;
1232 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1233 def(esc_args[1], rows)) - 1;
1242 * I used to think the cursor should be
1243 * placed at the top of the newly marginned
1244 * area. Apparently not: VMS TPU falls over
1247 * Well actually it should for Origin mode - RDB
1249 curs_y = (dec_om ? marg_t : 0);
1251 seen_disp_event = TRUE;
1255 case 'm': /* set graphics rendition */
1258 * A VT100 without the AVO only had one attribute, either
1259 * underline or reverse video depending on the cursor type,
1260 * this was selected by CSI 7m.
1263 * This is DIM on the VT100-AVO and VT102
1265 * This is BLINK on the VT100-AVO and VT102+
1267 * This is INVIS on the VT100-AVO and VT102
1269 * This like 22 disables BOLD, DIM and INVIS
1271 * The ANSI colours appear on any terminal that has colour
1272 * (obviously) but the interaction between sgr0 and the
1273 * colours varies but is usually related to the background
1274 * colour erase item.
1275 * The interaction between colour attributes and the mono
1276 * ones is also very implementation dependent.
1278 * The 39 and 49 attributes are likely to be unimplemented.
1281 for (i=0; i<esc_nargs; i++) {
1282 switch (def(esc_args[i], 0)) {
1283 case 0: /* restore defaults */
1284 curr_attr = ATTR_DEFAULT; break;
1285 case 1: /* enable bold */
1286 compatibility(VT100AVO);
1287 curr_attr |= ATTR_BOLD; break;
1288 case 21: /* (enable double underline) */
1289 compatibility(OTHER);
1290 case 4: /* enable underline */
1291 compatibility(VT100AVO);
1292 curr_attr |= ATTR_UNDER; break;
1293 case 5: /* enable blink */
1294 compatibility(VT100AVO);
1295 curr_attr |= ATTR_BLINK; break;
1296 case 7: /* enable reverse video */
1297 curr_attr |= ATTR_REVERSE; break;
1298 case 22: /* disable bold */
1299 compatibility(VT220);
1300 curr_attr &= ~ATTR_BOLD; break;
1301 case 24: /* disable underline */
1302 compatibility(VT220);
1303 curr_attr &= ~ATTR_UNDER; break;
1304 case 25: /* disable blink */
1305 compatibility(VT220);
1306 curr_attr &= ~ATTR_BLINK; break;
1307 case 27: /* disable reverse video */
1308 compatibility(VT220);
1309 curr_attr &= ~ATTR_REVERSE; break;
1310 case 30: case 31: case 32: case 33:
1311 case 34: case 35: case 36: case 37:
1313 curr_attr &= ~ATTR_FGMASK;
1314 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1316 case 39: /* default-foreground */
1317 curr_attr &= ~ATTR_FGMASK;
1318 curr_attr |= ATTR_DEFFG;
1320 case 40: case 41: case 42: case 43:
1321 case 44: case 45: case 46: case 47:
1323 curr_attr &= ~ATTR_BGMASK;
1324 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1326 case 49: /* default-background */
1327 curr_attr &= ~ATTR_BGMASK;
1328 curr_attr |= ATTR_DEFBG;
1335 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1339 case 's': /* save cursor */
1342 case 'u': /* restore cursor */
1343 save_cursor (FALSE);
1344 seen_disp_event = TRUE;
1346 case 't': /* set page size - ie window height */
1348 * VT340/VT420 sequence DECSLPP, DEC only allows values
1349 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1350 * illegal values (eg first arg 1..9) for window changing
1353 compatibility(VT340TEXT);
1354 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1355 request_resize (cols, def(esc_args[0], 24), 0);
1359 case ANSI('|', '*'):
1360 /* VT420 sequence DECSNLS
1361 * Set number of lines on screen
1362 * VT420 uses VGA like hardware and can support any size in
1363 * reasonable range (24..49 AIUI) with no default specified.
1365 compatibility(VT420);
1366 if (esc_nargs==1 && esc_args[0]>0) {
1367 request_resize (cols, def(esc_args[0], cfg.height), 0);
1371 case ANSI('|', '$'):
1372 /* VT340/VT420 sequence DECSCPP
1373 * Set number of columns per page
1374 * Docs imply range is only 80 or 132, but I'll allow any.
1376 compatibility(VT340TEXT);
1378 request_resize (def(esc_args[0], cfg.width), rows, 0);
1382 case 'X': /* write N spaces w/o moving cursor */
1383 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1384 compatibility(ANSIMIN);
1386 int n = def(esc_args[0], 1);
1387 unsigned long *p = cpos;
1388 if (n > cols - curs_x)
1390 check_selection (cpos, cpos+n);
1393 seen_disp_event = TRUE;
1396 case 'x': /* report terminal characteristics */
1397 compatibility(VT100);
1400 int i = def(esc_args[0], 0);
1401 if (i == 0 || i == 1) {
1402 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1404 ldisc->send (buf, 20);
1409 compatibility(OTHER);
1410 use_bce = (esc_args[0]<=0);
1411 erase_char = ERASE_CHAR;
1413 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1416 compatibility(OTHER);
1417 blink_is_real = (esc_args[0]>=1);
1420 /* Allow the host to make this emulator a 'perfect' VT102.
1421 * This first appeared in the VT220, but we do need to get
1422 * back to PuTTY mode so I won't check it.
1424 * The arg == 60 is a PuTTY extension.
1425 * The 2nd arg, 8bit vs 7bit is not obeyed.
1427 * Setting VT102 mode should also change the Fkeys to
1428 * generate PF* codes as a real VT102 has no Fkeys.
1429 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1432 * Note ESC c will NOT change this!
1435 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1436 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1437 else compatibility_level = TM_PUTTY;
1443 /* VT100 only here, checked above */
1446 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1449 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1452 default: /* specifically, 'B' */
1453 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1456 if( !has_compat(VT220) || c != '%' )
1457 termstate = TOPLEVEL;
1462 case 'P': /* Linux palette sequence */
1463 termstate = SEEN_OSC_P;
1466 case 'R': /* Linux palette reset */
1469 termstate = TOPLEVEL;
1471 case 'W': /* word-set */
1472 termstate = SEEN_OSC_W;
1475 case '0': case '1': case '2': case '3': case '4':
1476 case '5': case '6': case '7': case '8': case '9':
1477 esc_args[0] = 10 * esc_args[0] + c - '0';
1481 * Grotty hack to support xterm and DECterm title
1482 * sequences concurrently.
1484 if (esc_args[0] == 2) {
1488 /* else fall through */
1490 termstate = OSC_STRING;
1496 * This OSC stuff is EVIL. It takes just one character to get into
1497 * sysline mode and it's not initially obvious how to get out.
1498 * So I've added CR and LF as string aborts.
1499 * This shouldn't effect compatibility as I believe embedded
1500 * control characters are supposed to be interpreted (maybe?)
1501 * and they don't display anything useful anyway.
1505 if (c == '\n' || c == '\r') {
1506 termstate = TOPLEVEL;
1507 } else if (c == 0234 || c == '\007' ) {
1509 * These characters terminate the string; ST and BEL
1510 * terminate the sequence and trigger instant
1511 * processing of it, whereas ESC goes back to SEEN_ESC
1512 * mode unless it is followed by \, in which case it is
1513 * synonymous with ST in the first place.
1516 termstate = TOPLEVEL;
1517 } else if (c == '\033')
1518 termstate = OSC_MAYBE_ST;
1519 else if (osc_strlen < OSC_STR_MAX)
1520 osc_string[osc_strlen++] = c;
1524 int max = (osc_strlen == 0 ? 21 : 16);
1526 if (c >= '0' && c <= '9')
1528 else if (c >= 'A' && c <= 'A'+max-10)
1530 else if (c >= 'a' && c <= 'a'+max-10)
1533 termstate = TOPLEVEL;
1534 osc_string[osc_strlen++] = val;
1535 if (osc_strlen >= 7) {
1536 palette_set (osc_string[0],
1537 osc_string[1] * 16 + osc_string[2],
1538 osc_string[3] * 16 + osc_string[4],
1539 osc_string[5] * 16 + osc_string[6]);
1541 termstate = TOPLEVEL;
1547 case '0': case '1': case '2': case '3': case '4':
1548 case '5': case '6': case '7': case '8': case '9':
1549 esc_args[0] = 10 * esc_args[0] + c - '0';
1552 termstate = OSC_STRING;
1559 unsigned long nlattr;
1565 n = rows * (cols+1);
1567 *p++ = ATTR_DEFAULT | 'E';
1569 seen_disp_event = TRUE;
1570 check_selection (scrtop, scrtop + rows * (cols+1));
1573 case '3': nlattr = LATTR_TOP; if(0) {
1574 case '4': nlattr = LATTR_BOT; } if(0) {
1575 case '5': nlattr = LATTR_NORM; } if(0) {
1576 case '6': nlattr = LATTR_WIDE; }
1578 p = scrtop + curs_y * (cols+1) + cols;
1583 termstate = TOPLEVEL;
1586 termstate = TOPLEVEL;
1587 seen_disp_event = TRUE;
1590 move (curs_x, curs_y - 1, 1);
1593 move (curs_x, curs_y + 1, 1);
1596 move (curs_x + 1, curs_y, 1);
1599 move (curs_x - 1, curs_y, 1);
1602 cset_attr[cset=0] = ATTR_LINEDRW;
1605 cset_attr[cset=0] = ATTR_ASCII;
1612 scroll (0, rows-1, -1, TRUE);
1613 else if (curs_y > 0)
1619 erase_lots(FALSE, FALSE, TRUE);
1623 erase_lots(TRUE, FALSE, TRUE);
1626 /* XXX Print cursor line */
1629 /* XXX Start controller mode */
1632 /* XXX Stop controller mode */
1635 termstate = VT52_Y1;
1638 ldisc->send ("\033/Z", 3);
1641 app_cursor_keys = TRUE;
1644 app_cursor_keys = FALSE;
1647 /* XXX This should switch to VT100 mode not current or default
1648 * VT mode. But this will only have effect in a VT220+
1654 /* XXX Enter auto print mode */
1657 /* XXX Exit auto print mode */
1660 /* XXX Print screen */
1665 termstate = VT52_Y2;
1666 move(curs_x, c-' ', 0);
1669 termstate = TOPLEVEL;
1670 move(c-' ', curs_y, 0);
1673 if (selstate != NO_SELECTION)
1674 check_selection (cpos, cpos+1);
1680 if(!beep_count) beep_overload=0;
1682 else if(beep_count && beep_count<5 && cfg.beep)
1687 * Compare two lines to determine whether they are sufficiently
1688 * alike to scroll-optimise one to the other. Return the degree of
1691 static int linecmp (unsigned long *a, unsigned long *b) {
1694 for (i=n=0; i < cols; i++)
1695 n += (*a++ == *b++);
1700 * Given a context, update the window. Out of paranoia, we don't
1701 * allow WM_PAINT responses to do scrolling optimisations.
1703 static void do_paint (Context ctx, int may_optimise){
1704 int i, j, start, our_curs_y;
1705 unsigned long attr, rv, cursor;
1710 if (blinker || !cfg.blink_cur)
1711 cursor = ATTR_ACTCURS;
1716 cursor = ATTR_PASCURS;
1719 rv = (rvideo ? ATTR_REVERSE : 0);
1720 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1722 for (i=0; i<rows; i++) {
1723 int idx = i*(cols+1);
1724 int lattr = (disptop[idx+cols] & LATTR_MODE);
1725 for (j=0; j<=cols; j++,idx++) {
1726 unsigned long *d = disptop+idx;
1727 wanttext[idx] = lattr | ((*d ^ rv
1728 ^ (selstart <= d && d < selend ?
1729 ATTR_REVERSE : 0)) |
1730 (i==our_curs_y && j==curs_x ? cursor : 0));
1732 if (blink_is_real) {
1733 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1735 wanttext[idx] &= ATTR_MASK;
1736 wanttext[idx] += ' ';
1738 wanttext[idx] &= ~ATTR_BLINK;
1744 * We would perform scrolling optimisations in here, if they
1745 * didn't have a nasty tendency to cause the whole sodding
1746 * program to hang for a second at speed-critical moments.
1747 * We'll leave it well alone...
1750 for (i=0; i<rows; i++) {
1751 int idx = i*(cols+1);
1752 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1754 for (j=0; j<=cols; j++,idx++) {
1755 unsigned long t = wanttext[idx];
1756 int needs_update = (j < cols && t != disptext[idx]);
1757 int keep_going = (start != -1 && needs_update &&
1758 (t & ATTR_MASK) == attr &&
1759 j-start < sizeof(ch));
1760 if (start != -1 && !keep_going) {
1761 do_text (ctx, start, i, ch, j-start, attr, lattr);
1767 attr = t & ATTR_MASK;
1769 ch[j-start] = (char) (t & CHAR_MASK);
1777 * Flick the switch that says if blinking things should be shown or hidden.
1780 void term_blink(int flg) {
1781 static long last_blink = 0;
1782 static long last_tblink = 0;
1783 long now, blink_diff;
1785 now = GetTickCount();
1786 blink_diff = now-last_tblink;
1788 /* Make sure the text blinks no more than 2Hz */
1789 if (blink_diff<0 || blink_diff>450)
1792 tblinker = !tblinker;
1801 blink_diff = now-last_blink;
1803 /* Make sure the cursor blinks no more than 2Hz */
1804 if (blink_diff>=0 && blink_diff<450)
1812 * Invalidate the whole screen so it will be repainted in full.
1814 void term_invalidate(void) {
1817 for (i=0; i<rows*(cols+1); i++)
1818 disptext[i] = ATTR_INVALID;
1822 * Paint the window in response to a WM_PAINT message.
1824 void term_paint (Context ctx, int l, int t, int r, int b) {
1825 int i, j, left, top, right, bottom;
1827 left = l / font_width;
1828 right = (r - 1) / font_width;
1829 top = t / font_height;
1830 bottom = (b - 1) / font_height;
1831 for (i = top; i <= bottom && i < rows ; i++)
1833 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1834 for (j = left; j <= right && j < cols ; j++)
1835 disptext[i*(cols+1)+j] = ATTR_INVALID;
1837 for (j = left/2; j <= right/2+1 && j < cols ; j++)
1838 disptext[i*(cols+1)+j] = ATTR_INVALID;
1841 /* This should happen soon enough, also for some reason it sometimes
1842 * fails to actually do anything when re-sizing ... painting the wrong
1844 do_paint (ctx, FALSE);
1849 * Attempt to scroll the scrollback. The second parameter gives the
1850 * position we want to scroll to; the first is +1 to denote that
1851 * this position is relative to the beginning of the scrollback, -1
1852 * to denote it is relative to the end, and 0 to denote that it is
1853 * relative to the current position.
1855 void term_scroll (int rel, int where) {
1856 int n = where * (cols+1);
1858 disptop = (rel < 0 ? scrtop :
1859 rel > 0 ? sbtop : disptop) + n;
1860 if (disptop < sbtop)
1862 if (disptop > scrtop)
1869 * Spread the selection outwards according to the selection mode.
1871 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1872 unsigned long *linestart, *lineend;
1876 x = (p - text) % (cols+1);
1878 lineend = linestart + cols;
1883 * In this mode, every character is a separate unit, except
1884 * for runs of spaces at the end of a non-wrapping line.
1886 if (!(linestart[cols] & ATTR_WRAPPED)) {
1887 unsigned long *q = lineend;
1888 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1893 p = (dir == -1 ? q : lineend - 1);
1898 * In this mode, the units are maximal runs of characters
1899 * whose `wordness' has the same value.
1901 wvalue = wordness[*p & CHAR_MASK];
1903 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1906 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1912 * In this mode, every line is a unit.
1914 p = (dir == -1 ? linestart : lineend - 1);
1920 static void sel_spread (void) {
1921 selstart = sel_spread_half (selstart, -1);
1922 selend = sel_spread_half (selend - 1, +1) + 1;
1925 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1926 unsigned long *selpoint;
1929 if (y>=rows) y = rows-1;
1937 if (x>=cols) x = cols-1;
1939 selpoint = disptop + y * (cols+1);
1940 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
1945 if (b == MB_SELECT && a == MA_CLICK) {
1947 selstate = ABOUT_TO;
1948 selanchor = selpoint;
1950 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1952 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1953 selstate = DRAGGING;
1954 selstart = selanchor = selpoint;
1955 selend = selstart + 1;
1957 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1958 (b == MB_EXTEND && a != MA_RELEASE)) {
1959 if (selstate == ABOUT_TO && selanchor == selpoint)
1961 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1962 if (selpoint-selstart < (selend-selstart)/2)
1963 selanchor = selend - 1;
1965 selanchor = selstart;
1966 selstate = DRAGGING;
1968 if (selstate != ABOUT_TO && selstate != DRAGGING)
1969 selanchor = selpoint;
1970 selstate = DRAGGING;
1971 if (selpoint < selanchor) {
1972 selstart = selpoint;
1973 selend = selanchor + 1;
1975 selstart = selanchor;
1976 selend = selpoint + 1;
1979 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1980 if (selstate == DRAGGING) {
1982 * We've completed a selection. We now transfer the
1983 * data to the clipboard.
1985 unsigned char *p = selspace;
1986 unsigned long *q = selstart;
1988 while (q < selend) {
1990 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1991 unsigned long *nlpos = lineend;
1993 if (!(*nlpos & ATTR_WRAPPED)) {
1994 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1999 while (q < nlpos && q < selend)
2000 *p++ = (unsigned char) (*q++ & CHAR_MASK);
2003 for (i=0; i<sizeof(sel_nl); i++)
2006 q = lineend + 1; /* start of next line */
2008 write_clip (selspace, p - selspace);
2009 selstate = SELECTED;
2011 selstate = NO_SELECTION;
2012 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2016 get_clip((void **) &data, &len);
2020 if (paste_buffer) sfree(paste_buffer);
2021 paste_pos = paste_hold = paste_len = 0;
2022 paste_buffer = smalloc(len);
2025 while (p < data+len) {
2026 while (p < data+len &&
2027 !(p <= data+len-sizeof(sel_nl) &&
2028 !memcmp(p, sel_nl, sizeof(sel_nl))))
2036 c=xlat_kbd2tty(q[i]);
2037 paste_buffer[paste_len++] = c;
2041 if (p <= data+len-sizeof(sel_nl) &&
2042 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2043 paste_buffer[paste_len++] = '\r';
2044 p += sizeof(sel_nl);
2049 /* Assume a small paste will be OK in one go. */
2050 if (paste_len<256) {
2051 ldisc->send (paste_buffer, paste_len);
2052 if (paste_buffer) sfree(paste_buffer);
2054 paste_pos = paste_hold = paste_len = 0;
2057 get_clip(NULL, NULL);
2063 void term_nopaste() {
2064 if(paste_len == 0) return;
2065 sfree(paste_buffer);
2071 static long last_paste = 0;
2072 long now, paste_diff;
2074 if(paste_len == 0) return;
2076 /* Don't wait forever to paste */
2078 now = GetTickCount();
2079 paste_diff = now-last_paste;
2080 if (paste_diff>=0 && paste_diff<450)
2085 while(paste_pos<paste_len)
2087 char c = paste_buffer[paste_pos++];
2088 ldisc->send (&c, 1);
2095 sfree(paste_buffer);
2100 static void deselect (void) {
2101 selstate = NO_SELECTION;
2102 selstart = selend = scrtop;
2105 void term_deselect (void) {