]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - terminal.c
Add multi-makefile management system
[PuTTY.git] / terminal.c
1 #include <windows.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "putty.h"
7
8 static unsigned long *text;            /* buffer of text on terminal screen */
9 static unsigned long *scrtop;          /* top of working screen */
10 static unsigned long *disptop;         /* top of displayed screen */
11 static unsigned long *sbtop;           /* top of scrollback */
12 static unsigned long *cpos;            /* cursor position (convenience) */
13 static unsigned long *disptext;        /* buffer of text on real screen */
14 static unsigned long *wanttext;        /* buffer of text we want on screen */
15 static unsigned long *alttext;         /* buffer of text on alt. screen */
16
17 static unsigned char *selspace;        /* buffer for building selections in */
18
19 #define TSIZE (sizeof(*text))
20 #define fix_cpos  do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
21
22 static unsigned long curr_attr, save_attr;
23
24 static int curs_x, curs_y;             /* cursor */
25 static int save_x, save_y;             /* saved cursor position */
26 static int marg_t, marg_b;             /* scroll margins */
27 static int dec_om;                     /* DEC origin mode flag */
28 static int wrap, wrapnext;             /* wrap flags */
29 static int insert;                     /* insert-mode flag */
30 static int cset;                       /* 0 or 1: which char set */
31 static int save_cset, save_csattr;     /* saved with cursor position */
32 static int rvideo;                     /* global reverse video flag */
33
34 static unsigned long cset_attr[2];
35
36 /*
37  * Saved settings on the alternate screen.
38  */
39 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
40 static int alt_t, alt_b;
41 static int alt_which;
42
43 #define ARGS_MAX 32                    /* max # of esc sequence arguments */
44 #define ARG_DEFAULT -1                 /* if an arg isn't specified */
45 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
46 static int esc_args[ARGS_MAX];
47 static int esc_nargs;
48 static int esc_query;
49
50 #define OSC_STR_MAX 2048
51 static int osc_strlen;
52 static char osc_string[OSC_STR_MAX+1];
53 static int osc_w;
54
55 static unsigned char *tabs;
56
57 #define MAXNL 5
58 static int nl_count;
59
60 static int scroll_heuristic;
61
62 static enum {
63     TOPLEVEL,
64     SEEN_ESC,
65     SEEN_CSI,
66     SEEN_OSC,
67     SEEN_OSC_W,
68
69     DO_CTRLS,
70
71     IGNORE_NEXT,
72     SET_GL, SET_GR,
73     SEEN_OSC_P,
74     OSC_STRING, OSC_MAYBE_ST,
75     SEEN_ESCHASH
76 } termstate;
77
78 static enum {
79     NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
80 } selstate;
81 static enum {
82     SM_CHAR, SM_WORD, SM_LINE
83 } selmode;
84 static unsigned long *selstart, *selend, *selanchor;
85
86 static short wordness[256] = {
87     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 */
88     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 */
89     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 */
90     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 */
91     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 */
92     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 */
93     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 */
94     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 */
95 };
96
97 static unsigned char sel_nl[] = SEL_NL;
98
99 /*
100  * Internal prototypes.
101  */
102 static void do_paint (Context, int);
103 static void erase_lots (int, int, int);
104 static void swap_screen (int);
105 static void update_sbar (void);
106 static void deselect (void);
107
108 /*
109  * Set up power-on settings for the terminal.
110  */
111 static void power_on(void) {
112     curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
113     alt_t = marg_t = 0;
114     if (rows != -1)
115         alt_b = marg_b = rows - 1;
116     else
117         alt_b = marg_b = 0;
118     if (cols != -1) {
119         int i;
120         for (i = 0; i < cols; i++)
121             tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
122     }
123     alt_om = dec_om = cfg.dec_om;
124     alt_wnext = wrapnext = alt_ins = insert = FALSE;
125     alt_wrap = wrap = cfg.wrap_mode;
126     alt_cset = cset = 0;
127     cset_attr[0] = cset_attr[1] = ATTR_ASCII;
128     rvideo = 0;
129     save_attr = curr_attr = ATTR_DEFAULT;
130     app_cursor_keys = cfg.app_cursor;
131     app_keypad_keys = cfg.app_keypad;
132     alt_which = 0;
133     {
134         int i;
135         for (i = 0; i < 256; i++)
136             wordness[i] = cfg.wordness[i];
137     }
138     if (text) {
139         swap_screen (1);
140         erase_lots (FALSE, TRUE, TRUE);
141         swap_screen (0);
142         erase_lots (FALSE, TRUE, TRUE);
143     }
144 }
145
146 /*
147  * Force a screen update.
148  */
149 void term_update(void) {
150     Context ctx;
151     ctx = get_ctx();
152     if (ctx) {
153         if ( (seen_key_event && (unscroll_event & US_KEY)) ||
154              (seen_disp_event && (unscroll_event & US_DISP)) ) {
155             disptop = scrtop;
156             seen_disp_event = seen_key_event = 0;
157         }
158         do_paint (ctx, TRUE);
159         free_ctx (ctx);
160         nl_count = 0;
161         scroll_heuristic = 0;
162     }
163 }
164
165 /*
166  * Same as power_on(), but an external function.
167  */
168 void term_pwron(void) {
169     power_on();
170     fix_cpos;
171     disptop = scrtop;
172     deselect();
173     term_update();
174 }
175
176 /*
177  * Clear the scrollback.
178  */
179 void term_clrsb(void) {
180     disptop = sbtop = scrtop;
181     update_sbar();
182 }
183
184 /*
185  * Initialise the terminal.
186  */
187 void term_init(void) {
188     text = sbtop = scrtop = disptop = cpos = NULL;
189     disptext = wanttext = NULL;
190     tabs = NULL;
191     selspace = NULL;
192     deselect();
193     rows = cols = -1;
194     nl_count = 0;
195     power_on();
196 }
197
198 /*
199  * Set up the terminal for a given size.
200  */
201 void term_size(int newrows, int newcols, int newsavelines) {
202     unsigned long *newtext, *newdisp, *newwant, *newalt;
203     int i, j, crows, ccols;
204
205     if (newrows == rows && newcols == cols && newsavelines == savelines)
206         return;                        /* nothing to do */
207
208     alt_t = marg_t = 0;
209     alt_b = marg_b = newrows - 1;
210
211     newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
212     disptop = newtext + newsavelines*(newcols+1);
213     for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
214         newtext[i] = ERASE_CHAR;
215     if (rows != -1) {
216         crows = rows + (scrtop - sbtop) / (cols+1);
217         if (crows > newrows+newsavelines)
218             crows = newrows+newsavelines;
219         ccols = (cols < newcols ? cols : newcols);
220         for (i=0; i<crows; i++) {
221             int oldidx = (rows + savelines - crows + i) * (cols+1);
222             int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
223             for (j=0; j<ccols; j++)
224                 newtext[newidx+j] = text[oldidx+j];
225             newtext[newidx+newcols] =
226                 (cols == newcols ? text[oldidx+cols] : 0);
227         }
228         sbtop = disptop - (crows - newrows) * (newcols+1);
229         if (sbtop > disptop)
230             sbtop = disptop;
231     } else
232         sbtop = disptop;
233     scrtop = disptop;
234     sfree (text);
235     text = newtext;
236
237     newdisp = smalloc (newrows*(newcols+1)*TSIZE);
238     for (i=0; i<newrows*(newcols+1); i++)
239         newdisp[i] = ATTR_INVALID;
240     sfree (disptext);
241     disptext = newdisp;
242
243     newwant = smalloc (newrows*(newcols+1)*TSIZE);
244     for (i=0; i<newrows*(newcols+1); i++)
245         newwant[i] = ATTR_INVALID;
246     sfree (wanttext);
247     wanttext = newwant;
248
249     newalt = smalloc (newrows*(newcols+1)*TSIZE);
250     for (i=0; i<newrows*(newcols+1); i++)
251         newalt[i] = ERASE_CHAR;
252     sfree (alttext);
253     alttext = newalt;
254
255     sfree (selspace);
256     selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
257
258     tabs = srealloc (tabs, newcols*sizeof(*tabs));
259     {
260         int i;
261         for (i = (cols > 0 ? cols : 0); i < newcols; i++)
262             tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
263     }
264
265     if (rows > 0)
266         curs_y += newrows - rows;
267     if (curs_y < 0)
268         curs_y = 0;
269     if (curs_y >= newrows)
270         curs_y = newrows-1;
271     if (curs_x >= newcols)
272         curs_x = newcols-1;
273     alt_x = alt_y = 0;
274     wrapnext = alt_wnext = FALSE;
275
276     rows = newrows;
277     cols = newcols;
278     savelines = newsavelines;
279     fix_cpos;
280
281     deselect();
282     update_sbar();
283     term_update();
284 }
285
286 /*
287  * Swap screens.
288  */
289 static void swap_screen (int which) {
290     int t;
291     unsigned long tt;
292
293     if (which == alt_which)
294         return;
295
296     alt_which = which;
297
298     for (t=0; t<rows*(cols+1); t++) {
299         tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
300     }
301
302     t = curs_x; curs_x = alt_x; alt_x = t;
303     t = curs_y; curs_y = alt_y; alt_y = t;
304     t = marg_t; marg_t = alt_t; alt_t = t;
305     t = marg_b; marg_b = alt_b; alt_b = t;
306     t = dec_om; dec_om = alt_om; alt_om = t;
307     t = wrap; wrap = alt_wrap; alt_wrap = t;
308     t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
309     t = insert; insert = alt_ins; alt_ins = t;
310     t = cset; cset = alt_cset; alt_cset = t;
311
312     fix_cpos;
313 }
314
315 /*
316  * Retrieve a character from `inbuf'.
317  */
318 static int inbuf_getc(void) {
319     if (inbuf_head == inbuf_reap)
320         return -1;                     /* EOF */
321     else {
322         int n = inbuf_reap;
323         inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
324         return inbuf[n];
325     }
326 }
327
328 /*
329  * Update the scroll bar.
330  */
331 static void update_sbar(void) {
332     int min;
333
334     min = (sbtop - text) / (cols+1);
335     set_sbar ((scrtop - text) / (cols+1) + rows - min,
336               (disptop - text) / (cols+1) - min,
337               rows);
338 }
339
340 /*
341  * Check whether the region bounded by the two pointers intersects
342  * the scroll region, and de-select the on-screen selection if so.
343  */
344 static void check_selection (unsigned long *from, unsigned long *to) {
345     if (from < selend && selstart < to)
346         deselect();
347 }
348
349 /*
350  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
351  * for backward.) `sb' is TRUE if the scrolling is permitted to
352  * affect the scrollback buffer.
353  */
354 static void scroll (int topline, int botline, int lines, int sb) {
355     unsigned long *scroll_top;
356     int scroll_size, size, i;
357
358     scroll_top = scrtop + topline*(cols+1);
359     size = (lines < 0 ? -lines : lines) * (cols+1);
360     scroll_size = (botline - topline + 1) * (cols+1) - size;
361
362     if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
363         /*
364          * Since we're going to scroll the top line and we're on the 
365          * scrolling screen let's also affect the scrollback buffer.
366          */
367         sbtop -= lines * (cols+1);
368         if (sbtop < text)
369             sbtop = text;
370         scroll_size += scroll_top - sbtop;
371         scroll_top = sbtop;
372         update_sbar();
373     }
374
375     if (scroll_size < 0) {
376         size += scroll_size;
377         scroll_size = 0;
378     }
379
380     if (lines > 0) {
381         if (scroll_size)
382             memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
383         for (i = 0; i < size; i++)
384             scroll_top[i+scroll_size] = ERASE_CHAR;
385         if (selstart > scroll_top &&
386             selstart < scroll_top + size + scroll_size) {
387             selstart -= size;
388             if (selstart < scroll_top)
389                 selstart = scroll_top;
390         }
391         if (selend > scroll_top &&
392             selend < scroll_top + size + scroll_size) {
393             selend -= size;
394             if (selend < scroll_top)
395                 selend = scroll_top;
396         }
397     } else {
398         if (scroll_size)
399             memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
400         for (i = 0; i < size; i++)
401             scroll_top[i] = ERASE_CHAR;
402         if (selstart > scroll_top &&
403             selstart < scroll_top + size + scroll_size) {
404             selstart += size;
405             if (selstart > scroll_top + size + scroll_size)
406                 selstart = scroll_top + size + scroll_size;
407         }
408         if (selend > scroll_top &&
409             selend < scroll_top + size + scroll_size) {
410             selend += size;
411             if (selend > scroll_top + size + scroll_size)
412                 selend = scroll_top + size + scroll_size;
413         }
414     }
415
416     scroll_heuristic += lines;
417 }
418
419 /*
420  * Move the cursor to a given position, clipping at boundaries. We
421  * may or may not want to clip at the scroll margin: marg_clip is 0
422  * not to, 1 to disallow _passing_ the margins, and 2 to disallow
423  * even _being_ outside the margins.
424  */
425 static void move (int x, int y, int marg_clip) {
426     if (x < 0)
427         x = 0;
428     if (x >= cols)
429         x = cols-1;
430     if (marg_clip) {
431         if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
432             y = marg_t;
433         if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
434             y = marg_b;
435     }
436     if (y < 0)
437         y = 0;
438     if (y >= rows)
439         y = rows-1;
440     curs_x = x;
441     curs_y = y;
442     fix_cpos;
443     wrapnext = FALSE;
444 }
445
446 /*
447  * Save or restore the cursor and SGR mode.
448  */
449 static void save_cursor(int save) {
450     if (save) {
451         save_x = curs_x;
452         save_y = curs_y;
453         save_attr = curr_attr;
454         save_cset = cset;
455         save_csattr = cset_attr[cset];
456     } else {
457         curs_x = save_x;
458         curs_y = save_y;
459         curr_attr = save_attr;
460         cset = save_cset;
461         cset_attr[cset] = save_csattr;
462         fix_cpos;
463     }
464 }
465
466 /*
467  * Erase a large portion of the screen: the whole screen, or the
468  * whole line, or parts thereof.
469  */
470 static void erase_lots (int line_only, int from_begin, int to_end) {
471     unsigned long *startpos, *endpos;
472
473     if (line_only) {
474         startpos = cpos - curs_x;
475         endpos = startpos + cols+1;
476     } else {
477         startpos = scrtop;
478         endpos = startpos + rows * (cols+1);
479     }
480     if (!from_begin)
481         startpos = cpos;
482     if (!to_end)
483         endpos = cpos;
484     check_selection (startpos, endpos);
485     while (startpos < endpos)
486         *startpos++ = ERASE_CHAR;
487 }
488
489 /*
490  * Insert or delete characters within the current line. n is +ve if
491  * insertion is desired, and -ve for deletion.
492  */
493 static void insch (int n) {
494     int dir = (n < 0 ? -1 : +1);
495     int m;
496
497     n = (n < 0 ? -n : n);
498     if (n > cols - curs_x)
499         n = cols - curs_x;
500     m = cols - curs_x - n;
501     check_selection (cpos, cpos+n);
502     if (dir < 0) {
503         memmove (cpos, cpos+n, m*TSIZE);
504         while (n--)
505             cpos[m++] = ERASE_CHAR;
506     } else {
507         memmove (cpos+n, cpos, m*TSIZE);
508         while (n--)
509             cpos[n] = ERASE_CHAR;
510     }
511 }
512
513 /*
514  * Toggle terminal mode `mode' to state `state'. (`query' indicates
515  * whether the mode is a DEC private one or a normal one.)
516  */
517 static void toggle_mode (int mode, int query, int state) {
518     if (query) switch (mode) {
519       case 1:                          /* application cursor keys */
520         app_cursor_keys = state;
521         break;
522       case 3:                          /* 80/132 columns */
523         deselect();
524         request_resize (state ? 132 : 80, rows);
525         break;
526       case 5:                          /* reverse video */
527         rvideo = state;
528         seen_disp_event = TRUE;
529         break;
530       case 6:                          /* DEC origin mode */
531         dec_om = state;
532         break;
533       case 7:                          /* auto wrap */
534         wrap = state;
535         break;
536       case 47:                         /* alternate screen */
537         deselect();
538         swap_screen (state);
539         disptop = scrtop;
540         break;
541     } else switch (mode) {
542       case 4:                          /* set insert mode */
543         insert = state;
544         break;
545     }
546 }
547
548 /*
549  * Process an OSC sequence: set window title or icon name.
550  */
551 static void do_osc(void) {
552     if (osc_w) {
553         while (osc_strlen--)
554             wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
555     } else {
556         osc_string[osc_strlen] = '\0';
557         switch (esc_args[0]) {
558           case 0:
559           case 1:
560             set_icon (osc_string);
561             if (esc_args[0] == 1)
562                 break;
563             /* fall through: parameter 0 means set both */
564           case 2:
565           case 21:
566             set_title (osc_string);
567             break;
568         }
569     }
570 }
571
572 /*
573  * Remove everything currently in `inbuf' and stick it up on the
574  * in-memory display. There's a big state machine in here to
575  * process escape sequences...
576  */
577 void term_out(void) {
578     int c;
579
580     while ( (c = inbuf_getc()) != -1) {
581         /*
582          * Optionally log the session traffic to a file. Useful for
583          * debugging and possibly also useful for actual logging.
584          */
585         if (logfile) {
586             static FILE *fp = NULL;
587             if (!fp) fp = fopen(logfile, "wb");
588             if (fp) fputc (c, fp);
589         }
590         if( termstate < DO_CTRLS && (c&0x60) == 0 ) {
591             switch (c) {
592               case '\005':             /* terminal type query */
593                 ldisc->send ("PuTTY\r", 6);
594                 break;
595               case '\007':
596                 beep();
597                 disptop = scrtop;
598                 break;
599               case '\b':
600                 if (curs_x == 0 && curs_y > 0)
601                     curs_x = cols-1, curs_y--;
602                 else if (wrapnext)
603                     wrapnext = FALSE;
604                 else
605                     curs_x--;
606                 fix_cpos;
607                 seen_disp_event = TRUE;
608                 break;
609               case '\016':
610                 cset = 1;
611                 break;
612               case '\017':
613                 cset = 0;
614                 break;
615               case '\033':
616                 termstate = SEEN_ESC;
617                 break;
618               case 0233:
619                 termstate = SEEN_CSI;
620                 esc_nargs = 1;
621                 esc_args[0] = ARG_DEFAULT;
622                 esc_query = FALSE;
623                 break;
624               case 0235:
625                 termstate = SEEN_OSC;
626                 esc_args[0] = 0;
627                 break;
628               case '\r':
629                 curs_x = 0;
630                 wrapnext = FALSE;
631                 fix_cpos;
632                 seen_disp_event = TRUE;
633                 break;
634               case '\013':
635               case '\014':
636               case '\n':
637                 if (curs_y == marg_b)
638                     scroll (marg_t, marg_b, 1, TRUE);
639                 else if (curs_y < rows-1)
640                     curs_y++;
641                 if (cfg.lfhascr)
642                     curs_x = 0;
643                 fix_cpos;
644                 wrapnext = FALSE;
645                 seen_disp_event = 1;
646                 nl_count++;
647                 break;
648               case '\t':
649                 do {
650                     curs_x++;
651                 } while (curs_x < cols-1 && !tabs[curs_x]);
652                 if (curs_x >= cols)
653                     curs_x = cols-1;
654                 {
655                     unsigned long *old_cpos = cpos;
656                     fix_cpos;
657                     check_selection (old_cpos, cpos);
658                 }
659                 seen_disp_event = TRUE;
660                 break;
661             }
662         }
663         else switch (termstate) {
664           case TOPLEVEL:
665             if (c >= ' ' && c != 0234) {
666                 if (wrapnext) {
667                     cpos[1] = ATTR_WRAPPED;
668                     if (curs_y == marg_b)
669                         scroll (marg_t, marg_b, 1, TRUE);
670                     else if (curs_y < rows-1)
671                         curs_y++;
672                     curs_x = 0;
673                     fix_cpos;
674                     wrapnext = FALSE;
675                     nl_count++;
676                 }
677                 if (insert)
678                     insch (1);
679                 check_selection (cpos, cpos+1);
680                 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
681                     (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
682                 curs_x++;
683                 if (curs_x == cols) {
684                     cpos--;
685                     curs_x--;
686                     wrapnext = wrap;
687                 }
688                 seen_disp_event = 1;
689             }
690             break;
691
692           case IGNORE_NEXT:
693             termstate = TOPLEVEL;
694             break;
695           case OSC_MAYBE_ST:
696             /*
697              * This state is virtually identical to SEEN_ESC, with the
698              * exception that we have an OSC sequence in the pipeline,
699              * and _if_ we see a backslash, we process it.
700              */
701             if (c == '\\') {
702                 do_osc();
703                 termstate = TOPLEVEL;
704                 break;
705             }
706             /* else fall through */
707           case SEEN_ESC:
708             termstate = TOPLEVEL;
709             switch (c) {
710               case ' ':                /* some weird sequence? */
711                 termstate = IGNORE_NEXT;
712                 break;
713               case '[':                /* enter CSI mode */
714                 termstate = SEEN_CSI;
715                 esc_nargs = 1;
716                 esc_args[0] = ARG_DEFAULT;
717                 esc_query = FALSE;
718                 break;
719               case ']':                /* xterm escape sequences */
720                 termstate = SEEN_OSC;
721                 esc_args[0] = 0;
722                 break;
723               case '(':                /* should set GL */
724                 termstate = SET_GL;
725                 break;
726               case ')':                /* should set GR */
727                 termstate = SET_GR;
728                 break;
729               case '7':                /* save cursor */
730                 save_cursor (TRUE);
731                 break;
732               case '8':                /* restore cursor */
733                 save_cursor (FALSE);
734                 seen_disp_event = TRUE;
735                 break;
736               case '=':
737                 app_keypad_keys = TRUE;
738                 break;
739               case '>':
740                 app_keypad_keys = FALSE;
741                 break;
742               case 'D':                /* exactly equivalent to LF */
743                 if (curs_y == marg_b)
744                     scroll (marg_t, marg_b, 1, TRUE);
745                 else if (curs_y < rows-1)
746                     curs_y++;
747                 fix_cpos;
748                 wrapnext = FALSE;
749                 seen_disp_event = TRUE;
750                 nl_count++;
751                 break;
752               case 'E':                /* exactly equivalent to CR-LF */
753                 curs_x = 0;
754                 wrapnext = FALSE;
755                 if (curs_y == marg_b)
756                     scroll (marg_t, marg_b, 1, TRUE);
757                 else if (curs_y < rows-1)
758                     curs_y++;
759                 fix_cpos;
760                 wrapnext = FALSE;
761                 nl_count++;
762                 seen_disp_event = TRUE;
763                 break;
764               case 'M':                /* reverse index - backwards LF */
765                 if (curs_y == marg_t)
766                     scroll (marg_t, marg_b, -1, TRUE);
767                 else if (curs_y > 0)
768                     curs_y--;
769                 fix_cpos;
770                 wrapnext = FALSE;
771                 seen_disp_event = TRUE;
772                 break;
773               case 'Z':                /* terminal type query */
774                 ldisc->send ("\033[?6c", 5);
775                 break;
776               case 'c':                /* restore power-on settings */
777                 power_on();
778                 fix_cpos;
779                 disptop = scrtop;
780                 seen_disp_event = TRUE;
781                 break;
782               case '#':                /* ESC # 8 fills screen with Es :-) */
783                 termstate = SEEN_ESCHASH;
784                 break;
785               case 'H':                /* set a tab */
786                 tabs[curs_x] = TRUE;
787                 break;
788             }
789             break;
790           case SEEN_CSI:
791             termstate = TOPLEVEL;      /* default */
792             switch (c) {
793               case '0': case '1': case '2': case '3': case '4':
794               case '5': case '6': case '7': case '8': case '9':
795                 if (esc_nargs <= ARGS_MAX) {
796                     if (esc_args[esc_nargs-1] == ARG_DEFAULT)
797                         esc_args[esc_nargs-1] = 0;
798                     esc_args[esc_nargs-1] =
799                         10 * esc_args[esc_nargs-1] + c - '0';
800                 }
801                 termstate = SEEN_CSI;
802                 break;
803               case ';':
804                 if (++esc_nargs <= ARGS_MAX)
805                     esc_args[esc_nargs-1] = ARG_DEFAULT;
806                 termstate = SEEN_CSI;
807                 break;
808               case '?':
809                 esc_query = TRUE;
810                 termstate = SEEN_CSI;
811                 break;
812               case 'A':                /* move up N lines */
813                 move (curs_x, curs_y - def(esc_args[0], 1), 1);
814                 seen_disp_event = TRUE;
815                 break;
816               case 'B': case 'e':      /* move down N lines */
817                 move (curs_x, curs_y + def(esc_args[0], 1), 1);
818                 seen_disp_event = TRUE;
819                 break;
820               case 'C': case 'a':      /* move right N cols */
821                 move (curs_x + def(esc_args[0], 1), curs_y, 1);
822                 seen_disp_event = TRUE;
823                 break;
824               case 'D':                /* move left N cols */
825                 move (curs_x - def(esc_args[0], 1), curs_y, 1);
826                 seen_disp_event = TRUE;
827                 break;
828               case 'E':                /* move down N lines and CR */
829                 move (0, curs_y + def(esc_args[0], 1), 1);
830                 seen_disp_event = TRUE;
831                 break;
832               case 'F':                /* move up N lines and CR */
833                 move (0, curs_y - def(esc_args[0], 1), 1);
834                 seen_disp_event = TRUE;
835                 break;
836               case 'G': case '`':      /* set horizontal posn */
837                 move (def(esc_args[0], 1) - 1, curs_y, 0);
838                 seen_disp_event = TRUE;
839                 break;
840               case 'd':                /* set vertical posn */
841                 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
842                       (dec_om ? 2 : 0));
843                 seen_disp_event = TRUE;
844                 break;
845               case 'H': case 'f':      /* set horz and vert posns at once */
846                 if (esc_nargs < 2)
847                     esc_args[1] = ARG_DEFAULT;
848                 move (def(esc_args[1], 1) - 1,
849                       (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
850                       (dec_om ? 2 : 0));
851                 seen_disp_event = TRUE;
852                 break;
853               case 'J':                /* erase screen or parts of it */
854                 {
855                     unsigned int i = def(esc_args[0], 0) + 1;
856                     if (i > 3)
857                         i = 0;
858                     erase_lots(FALSE, !!(i & 2), !!(i & 1));
859                 }
860                 disptop = scrtop;
861                 seen_disp_event = TRUE;
862                 break;
863               case 'K':                /* erase line or parts of it */
864                 {
865                     unsigned int i = def(esc_args[0], 0) + 1;
866                     if (i > 3)
867                         i = 0;
868                     erase_lots(TRUE, !!(i & 2), !!(i & 1));
869                 }
870                 seen_disp_event = TRUE;
871                 break;
872               case 'L':                /* insert lines */
873                 if (curs_y <= marg_b)
874                     scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
875                 seen_disp_event = TRUE;
876                 break;
877               case 'M':                /* delete lines */
878                 if (curs_y <= marg_b)
879                     scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
880                 seen_disp_event = TRUE;
881                 break;
882               case '@':                /* insert chars */
883                 insch (def(esc_args[0], 1));
884                 seen_disp_event = TRUE;
885                 break;
886               case 'P':                /* delete chars */
887                 insch (-def(esc_args[0], 1));
888                 seen_disp_event = TRUE;
889                 break;
890               case 'c':                /* terminal type query */
891                 ldisc->send ("\033[?6c", 5);
892                 break;
893               case 'n':                /* cursor position query */
894                 if (esc_args[0] == 6) {
895                     char buf[32];
896                     sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
897                     ldisc->send (buf, strlen(buf));
898                 }
899                 break;
900               case 'h':                /* toggle a mode to high */
901                 toggle_mode (esc_args[0], esc_query, TRUE);
902                 break;
903               case 'l':                /* toggle a mode to low */
904                 toggle_mode (esc_args[0], esc_query, FALSE);
905                 break;
906               case 'g':                /* clear tabs */
907                 if (esc_nargs == 1) {
908                     if (esc_args[0] == 0) {
909                         tabs[curs_x] = FALSE;
910                     } else if (esc_args[0] == 3) {
911                         int i;
912                         for (i = 0; i < cols; i++)
913                             tabs[i] = FALSE;
914                     }
915                 }
916                 break;
917               case 'r':                /* set scroll margins */
918                 if (!esc_query && esc_nargs <= 2) {
919                     int top, bot;
920                     top = def(esc_args[0], 1) - 1;
921                     if (top < 0)
922                         top = 0;
923                     bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
924                            def(esc_args[1], rows)) - 1;
925                     if (bot >= rows)
926                         bot = rows-1;
927                     if (top <= bot) {
928                         marg_t = top;
929                         marg_b = bot;
930                         curs_x = 0;
931                         /*
932                          * I used to think the cursor should be
933                          * placed at the top of the newly marginned
934                          * area. Apparently not: VMS TPU falls over
935                          * if so.
936                          */
937                         curs_y = 0;
938                         fix_cpos;
939                         seen_disp_event = TRUE;
940                     }
941                 }
942                 break;
943               case 'm':                /* set graphics rendition */
944                 {
945                     int i;
946                     for (i=0; i<esc_nargs; i++) {
947                         switch (def(esc_args[i], 0)) {
948                           case 0:      /* restore defaults */
949                             curr_attr = ATTR_DEFAULT; break;
950                           case 1:      /* enable bold */
951                             curr_attr |= ATTR_BOLD; break;
952                           case 4:      /* enable underline */
953                           case 21:     /* (enable double underline) */
954                             curr_attr |= ATTR_UNDER; break;
955                           case 7:      /* enable reverse video */
956                             curr_attr |= ATTR_REVERSE; break;
957                           case 22:     /* disable bold */
958                             curr_attr &= ~ATTR_BOLD; break;
959                           case 24:     /* disable underline */
960                             curr_attr &= ~ATTR_UNDER; break;
961                           case 27:     /* disable reverse video */
962                             curr_attr &= ~ATTR_REVERSE; break;
963                           case 30: case 31: case 32: case 33:
964                           case 34: case 35: case 36: case 37:
965                             /* foreground */
966                             curr_attr &= ~ATTR_FGMASK;
967                             curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
968                             break;
969                           case 39:     /* default-foreground */
970                             curr_attr &= ~ATTR_FGMASK;
971                             curr_attr |= ATTR_DEFFG;
972                             break;
973                           case 40: case 41: case 42: case 43:
974                           case 44: case 45: case 46: case 47:
975                             /* background */
976                             curr_attr &= ~ATTR_BGMASK;
977                             curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
978                             break;
979                           case 49:     /* default-background */
980                             curr_attr &= ~ATTR_BGMASK;
981                             curr_attr |= ATTR_DEFBG;
982                             break;
983                         }
984                     }
985                 }
986                 break;
987               case 's':                /* save cursor */
988                 save_cursor (TRUE);
989                 break;
990               case 'u':                /* restore cursor */
991                 save_cursor (FALSE);
992                 seen_disp_event = TRUE;
993                 break;
994               case 't':                /* set page size - ie window height */
995                 request_resize (cols, def(esc_args[0], 24));
996                 deselect();
997                 break;
998               case 'X':                /* write N spaces w/o moving cursor */
999                 {
1000                     int n = def(esc_args[0], 1);
1001                     unsigned long *p = cpos;
1002                     if (n > cols - curs_x)
1003                         n = cols - curs_x;
1004                     check_selection (cpos, cpos+n);
1005                     while (n--)
1006                         *p++ = ERASE_CHAR;
1007                     seen_disp_event = TRUE;
1008                 }
1009                 break;
1010               case 'x':                /* report terminal characteristics */
1011                 {
1012                     char buf[32];
1013                     int i = def(esc_args[0], 0);
1014                     if (i == 0 || i == 1) {
1015                         strcpy (buf, "\033[2;1;1;112;112;1;0x");
1016                         buf[2] += i;
1017                         ldisc->send (buf, 20);
1018                     }
1019                 }
1020                 break;
1021             }
1022             break;
1023           case SET_GL:
1024           case SET_GR:
1025             switch (c) {
1026               case 'A':
1027                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1028                 break;
1029               case '0':
1030                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1031                 break;
1032               default:                 /* specifically, 'B' */
1033                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1034                 break;
1035             }
1036             termstate = TOPLEVEL;
1037             break;
1038           case SEEN_OSC:
1039             osc_w = FALSE;
1040             switch (c) {
1041               case 'P':                /* Linux palette sequence */
1042                 termstate = SEEN_OSC_P;
1043                 osc_strlen = 0;
1044                 break;
1045               case 'R':                /* Linux palette reset */
1046                 palette_reset();
1047                 term_invalidate();
1048                 termstate = TOPLEVEL;
1049                 break;
1050               case 'W':                /* word-set */
1051                 termstate = SEEN_OSC_W;
1052                 osc_w = TRUE;
1053                 break;
1054               case '0': case '1': case '2': case '3': case '4':
1055               case '5': case '6': case '7': case '8': case '9':
1056                 esc_args[0] = 10 * esc_args[0] + c - '0';
1057                 break;
1058               case 'L':
1059                 /*
1060                  * Grotty hack to support xterm and DECterm title
1061                  * sequences concurrently.
1062                  */
1063                 if (esc_args[0] == 2) {
1064                     esc_args[0] = 1;
1065                     break;
1066                 }
1067                 /* else fall through */
1068               default:
1069                 termstate = OSC_STRING;
1070                 osc_strlen = 0;
1071             }
1072             break;
1073           case OSC_STRING:
1074             if (c == 0234 || c == '\007') {
1075                 /*
1076                  * These characters terminate the string; ST and BEL
1077                  * terminate the sequence and trigger instant
1078                  * processing of it, whereas ESC goes back to SEEN_ESC
1079                  * mode unless it is followed by \, in which case it is
1080                  * synonymous with ST in the first place.
1081                  */
1082                 do_osc();
1083                 termstate = TOPLEVEL;
1084             } else if (c == '\033')
1085                     termstate = OSC_MAYBE_ST;
1086             else if (osc_strlen < OSC_STR_MAX)
1087                 osc_string[osc_strlen++] = c;
1088             break;
1089           case SEEN_OSC_P:
1090             {
1091                 int max = (osc_strlen == 0 ? 21 : 16);
1092                 int val;
1093                 if (c >= '0' && c <= '9')
1094                     val = c - '0';
1095                 else if (c >= 'A' && c <= 'A'+max-10)
1096                     val = c - 'A' + 10;
1097                 else if (c >= 'a' && c <= 'a'+max-10)
1098                     val = c - 'a' + 10;
1099                 else
1100                     termstate = TOPLEVEL;
1101                 osc_string[osc_strlen++] = val;
1102                 if (osc_strlen >= 7) {
1103                     palette_set (osc_string[0],
1104                                  osc_string[1] * 16 + osc_string[2],
1105                                  osc_string[3] * 16 + osc_string[4],
1106                                  osc_string[5] * 16 + osc_string[6]);
1107                     term_invalidate();
1108                     termstate = TOPLEVEL;
1109                 }
1110             }
1111             break;
1112           case SEEN_OSC_W:
1113             switch (c) {
1114               case '0': case '1': case '2': case '3': case '4':
1115               case '5': case '6': case '7': case '8': case '9':
1116                 esc_args[0] = 10 * esc_args[0] + c - '0';
1117                 break;
1118               default:
1119                 termstate = OSC_STRING;
1120                 osc_strlen = 0;
1121             }
1122             break;
1123           case SEEN_ESCHASH:
1124             if (c == '8') {
1125                 unsigned long *p = scrtop;
1126                 int n = rows * (cols+1);
1127                 while (n--)
1128                     *p++ = ATTR_DEFAULT | 'E';
1129                 disptop = scrtop;
1130                 seen_disp_event = TRUE;
1131                 check_selection (scrtop, scrtop + rows * (cols+1));
1132             }
1133             termstate = TOPLEVEL;
1134             break;
1135         }
1136         check_selection (cpos, cpos+1);
1137     }
1138         
1139     if (nl_count > MAXNL)
1140         term_update();
1141 }
1142
1143 /*
1144  * Compare two lines to determine whether they are sufficiently
1145  * alike to scroll-optimise one to the other. Return the degree of
1146  * similarity.
1147  */
1148 static int linecmp (unsigned long *a, unsigned long *b) {
1149     int i, n;
1150
1151     for (i=n=0; i < cols; i++)
1152         n += (*a++ == *b++);
1153     return n;
1154 }
1155
1156 /*
1157  * Given a context, update the window. Out of paranoia, we don't
1158  * allow WM_PAINT responses to do scrolling optimisations.
1159  */
1160 static void do_paint (Context ctx, int may_optimise){ 
1161     int i, j, start, our_curs_y;
1162     unsigned long attr, rv, cursor;
1163     char ch[1024];
1164
1165     cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1166     rv = (rvideo ? ATTR_REVERSE : 0);
1167     our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1168
1169     for (i=0; i<rows; i++) {
1170         int idx = i*(cols+1);
1171         for (j=0; j<=cols; j++,idx++) {
1172             unsigned long *d = disptop+idx;
1173             wanttext[idx] = ((*d ^ rv
1174                               ^ (selstart <= d && d < selend ?
1175                                  ATTR_REVERSE : 0)) |
1176                              (i==our_curs_y && j==curs_x ? cursor : 0));
1177         }
1178     }
1179
1180     /*
1181      * We would perform scrolling optimisations in here, if they
1182      * didn't have a nasty tendency to cause the whole sodding
1183      * program to hang for a second at speed-critical moments.
1184      * We'll leave it well alone...
1185      */
1186
1187     for (i=0; i<rows; i++) {
1188         int idx = i*(cols+1);
1189         start = -1;
1190         for (j=0; j<=cols; j++,idx++) {
1191             unsigned long t = wanttext[idx];
1192             int needs_update = (j < cols && t != disptext[idx]);
1193             int keep_going = (start != -1 && needs_update &&
1194                               (t & ATTR_MASK) == attr &&
1195                               j-start < sizeof(ch));
1196             if (start != -1 && !keep_going) {
1197                 do_text (ctx, start, i, ch, j-start, attr);
1198                 start = -1;
1199             }
1200             if (needs_update) {
1201                 if (start == -1) {
1202                     start = j;
1203                     attr = t & ATTR_MASK;
1204                 }
1205                 ch[j-start] = (char) (t & CHAR_MASK);
1206             }
1207             disptext[idx] = t;
1208         }
1209     }
1210 }
1211
1212 /*
1213  * Invalidate the whole screen so it will be repainted in full.
1214  */
1215 void term_invalidate(void) {
1216     int i;
1217
1218     for (i=0; i<rows*(cols+1); i++)
1219         disptext[i] = ATTR_INVALID;
1220 }
1221
1222 /*
1223  * Paint the window in response to a WM_PAINT message.
1224  */
1225 void term_paint (Context ctx, int l, int t, int r, int b) {
1226     int i, j, left, top, right, bottom;
1227
1228     left = l / font_width;
1229     right = (r - 1) / font_width;
1230     top = t / font_height;
1231     bottom = (b - 1) / font_height;
1232     for (i = top; i <= bottom && i < rows ; i++)
1233       for (j = left; j <= right && j < cols ; j++)
1234             disptext[i*(cols+1)+j] = ATTR_INVALID;
1235
1236     do_paint (ctx, FALSE);
1237 }
1238
1239 /*
1240  * Attempt to scroll the scrollback. The second parameter gives the
1241  * position we want to scroll to; the first is +1 to denote that
1242  * this position is relative to the beginning of the scrollback, -1
1243  * to denote it is relative to the end, and 0 to denote that it is
1244  * relative to the current position.
1245  */
1246 void term_scroll (int rel, int where) {
1247     int n = where * (cols+1);
1248
1249     disptop = (rel < 0 ? scrtop :
1250                rel > 0 ? sbtop : disptop) + n;
1251     if (disptop < sbtop)
1252         disptop = sbtop;
1253     if (disptop > scrtop)
1254         disptop = scrtop;
1255     update_sbar();
1256     term_update();
1257 }
1258
1259 /*
1260  * Spread the selection outwards according to the selection mode.
1261  */
1262 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1263     unsigned long *linestart, *lineend;
1264     int x;
1265     short wvalue;
1266
1267     x = (p - text) % (cols+1);
1268     linestart = p - x;
1269     lineend = linestart + cols;
1270
1271     switch (selmode) {
1272       case SM_CHAR:
1273         /*
1274          * In this mode, every character is a separate unit, except
1275          * for runs of spaces at the end of a non-wrapping line.
1276          */
1277         if (!(linestart[cols] & ATTR_WRAPPED)) {
1278             unsigned long *q = lineend;
1279             while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1280                 q--;
1281             if (q == lineend)
1282                 q--;
1283             if (p >= q)
1284                 p = (dir == -1 ? q : lineend - 1);
1285         }
1286         break;
1287       case SM_WORD:
1288         /*
1289          * In this mode, the units are maximal runs of characters
1290          * whose `wordness' has the same value.
1291          */
1292         wvalue = wordness[*p & CHAR_MASK];
1293         if (dir == +1) {
1294             while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1295                 p++;
1296         } else {
1297             while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1298                 p--;
1299         }
1300         break;
1301       case SM_LINE:
1302         /*
1303          * In this mode, every line is a unit.
1304          */
1305         p = (dir == -1 ? linestart : lineend - 1);
1306         break;
1307     }
1308     return p;
1309 }
1310
1311 static void sel_spread (void) {
1312     selstart = sel_spread_half (selstart, -1);
1313     selend = sel_spread_half (selend - 1, +1) + 1;
1314 }
1315
1316 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1317     unsigned long *selpoint;
1318     
1319     if (y<0) y = 0;
1320     if (y>=rows) y = rows-1;
1321     if (x<0) {
1322         if (y > 0) {
1323             x = cols-1;
1324             y--;
1325         } else
1326             x = 0;
1327     }
1328     if (x>=cols) x = cols-1;
1329
1330     selpoint = disptop + y * (cols+1) + x;
1331
1332     if (b == MB_SELECT && a == MA_CLICK) {
1333         deselect();
1334         selstate = ABOUT_TO;
1335         selanchor = selpoint;
1336         selmode = SM_CHAR;
1337     } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1338         deselect();
1339         selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1340         selstate = DRAGGING;
1341         selstart = selanchor = selpoint;
1342         selend = selstart + 1;
1343         sel_spread();
1344     } else if ((b == MB_SELECT && a == MA_DRAG) ||
1345                (b == MB_EXTEND && a != MA_RELEASE)) {
1346         if (selstate == ABOUT_TO && selanchor == selpoint)
1347             return;
1348         if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1349             if (selpoint-selstart < (selend-selstart)/2)
1350                 selanchor = selend - 1;
1351             else
1352                 selanchor = selstart;
1353             selstate = DRAGGING;
1354         }
1355         if (selstate != ABOUT_TO && selstate != DRAGGING)
1356             selanchor = selpoint;
1357         selstate = DRAGGING;
1358         if (selpoint < selanchor) {
1359             selstart = selpoint;
1360             selend = selanchor + 1;
1361         } else {
1362             selstart = selanchor;
1363             selend = selpoint + 1;
1364         }
1365         sel_spread();
1366     } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1367         if (selstate == DRAGGING) {
1368             /*
1369              * We've completed a selection. We now transfer the
1370              * data to the clipboard.
1371              */
1372             unsigned char *p = selspace;
1373             unsigned long *q = selstart;
1374
1375             while (q < selend) {
1376                 int nl = FALSE;
1377                 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1378                 unsigned long *nlpos = lineend;
1379
1380                 if (!(*nlpos & ATTR_WRAPPED)) {
1381                     while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1382                         nlpos--;
1383                     if (nlpos < selend)
1384                         nl = TRUE;
1385                 }
1386                 while (q < nlpos && q < selend)
1387                     *p++ = (unsigned char) (*q++ & CHAR_MASK);
1388                 if (nl) {
1389                     int i;
1390                     for (i=0; i<sizeof(sel_nl); i++)
1391                         *p++ = sel_nl[i];
1392                 }
1393                 q = lineend + 1;       /* start of next line */
1394             }
1395             write_clip (selspace, p - selspace);
1396             selstate = SELECTED;
1397         } else
1398             selstate = NO_SELECTION;
1399     } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1400         char *data;
1401         int len;
1402
1403         get_clip((void **) &data, &len);
1404         if (data) {
1405             char *p, *q;
1406             p = q = data;
1407             while (p < data+len) {
1408                 while (p < data+len &&
1409                        !(p <= data+len-sizeof(sel_nl) &&
1410                          !memcmp(p, sel_nl, sizeof(sel_nl))))
1411                     p++;
1412
1413                 {
1414                     int i;
1415                     unsigned char c;
1416                     for(i=0;i<p-q;i++)
1417                     {
1418                         c=xlat_kbd2tty(q[i]);
1419                         ldisc->send(&c,1);
1420                     }
1421                 }
1422
1423                 if (p <= data+len-sizeof(sel_nl) &&
1424                     !memcmp(p, sel_nl, sizeof(sel_nl))) {
1425                     ldisc->send ("\r", 1);
1426                     p += sizeof(sel_nl);
1427                 }
1428                 q = p;
1429             }
1430         }
1431         get_clip(NULL, NULL);
1432     }
1433
1434     term_update();
1435 }
1436
1437 static void deselect (void) {
1438     selstate = NO_SELECTION;
1439     selstart = selend = scrtop;
1440 }
1441
1442 void term_deselect (void) {
1443     deselect();
1444     term_update();
1445 }