]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - terminal.c
Oops - fix total idiocies in applying that patch
[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 #ifdef LOG
582         {
583             static FILE *fp = NULL;
584             if (!fp) fp = fopen("putty.log", "wb");
585             if (fp) fputc (c, fp);
586         }
587 #endif
588         if( termstate < DO_CTRLS && (c&0x60) == 0 ) {
589             switch (c) {
590               case '\005':             /* terminal type query */
591                 ldisc->send ("PuTTY\r", 6);
592                 break;
593               case '\007':
594                 beep();
595                 disptop = scrtop;
596                 break;
597               case '\b':
598                 if (curs_x == 0 && curs_y > 0)
599                     curs_x = cols-1, curs_y--;
600                 else if (wrapnext)
601                     wrapnext = FALSE;
602                 else
603                     curs_x--;
604                 fix_cpos;
605                 seen_disp_event = TRUE;
606                 break;
607               case '\016':
608                 cset = 1;
609                 break;
610               case '\017':
611                 cset = 0;
612                 break;
613               case '\033':
614                 termstate = SEEN_ESC;
615                 break;
616               case 0233:
617                 termstate = SEEN_CSI;
618                 esc_nargs = 1;
619                 esc_args[0] = ARG_DEFAULT;
620                 esc_query = FALSE;
621                 break;
622               case 0235:
623                 termstate = SEEN_OSC;
624                 esc_args[0] = 0;
625                 break;
626               case '\r':
627                 curs_x = 0;
628                 wrapnext = FALSE;
629                 fix_cpos;
630                 seen_disp_event = TRUE;
631                 break;
632               case '\013':
633               case '\014':
634               case '\n':
635                 if (curs_y == marg_b)
636                     scroll (marg_t, marg_b, 1, TRUE);
637                 else if (curs_y < rows-1)
638                     curs_y++;
639                 if (cfg.lfhascr)
640                     curs_x = 0;
641                 fix_cpos;
642                 wrapnext = FALSE;
643                 seen_disp_event = 1;
644                 nl_count++;
645                 break;
646               case '\t':
647                 do {
648                     curs_x++;
649                 } while (curs_x < cols-1 && !tabs[curs_x]);
650                 if (curs_x >= cols)
651                     curs_x = cols-1;
652                 {
653                     unsigned long *old_cpos = cpos;
654                     fix_cpos;
655                     check_selection (old_cpos, cpos);
656                 }
657                 seen_disp_event = TRUE;
658                 break;
659             }
660         }
661         else switch (termstate) {
662           case TOPLEVEL:
663             if (c >= ' ' && c != 0234) {
664                 if (wrapnext) {
665                     cpos[1] = ATTR_WRAPPED;
666                     if (curs_y == marg_b)
667                         scroll (marg_t, marg_b, 1, TRUE);
668                     else if (curs_y < rows-1)
669                         curs_y++;
670                     curs_x = 0;
671                     fix_cpos;
672                     wrapnext = FALSE;
673                     nl_count++;
674                 }
675                 if (insert)
676                     insch (1);
677                 check_selection (cpos, cpos+1);
678                 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
679                     (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
680                 curs_x++;
681                 if (curs_x == cols) {
682                     cpos--;
683                     curs_x--;
684                     wrapnext = wrap;
685                 }
686                 seen_disp_event = 1;
687             }
688             break;
689
690           case IGNORE_NEXT:
691             termstate = TOPLEVEL;
692             break;
693           case OSC_MAYBE_ST:
694             /*
695              * This state is virtually identical to SEEN_ESC, with the
696              * exception that we have an OSC sequence in the pipeline,
697              * and _if_ we see a backslash, we process it.
698              */
699             if (c == '\\') {
700                 do_osc();
701                 termstate = TOPLEVEL;
702                 break;
703             }
704             /* else fall through */
705           case SEEN_ESC:
706             termstate = TOPLEVEL;
707             switch (c) {
708               case ' ':                /* some weird sequence? */
709                 termstate = IGNORE_NEXT;
710                 break;
711               case '[':                /* enter CSI mode */
712                 termstate = SEEN_CSI;
713                 esc_nargs = 1;
714                 esc_args[0] = ARG_DEFAULT;
715                 esc_query = FALSE;
716                 break;
717               case ']':                /* xterm escape sequences */
718                 termstate = SEEN_OSC;
719                 esc_args[0] = 0;
720                 break;
721               case '(':                /* should set GL */
722                 termstate = SET_GL;
723                 break;
724               case ')':                /* should set GR */
725                 termstate = SET_GR;
726                 break;
727               case '7':                /* save cursor */
728                 save_cursor (TRUE);
729                 break;
730               case '8':                /* restore cursor */
731                 save_cursor (FALSE);
732                 seen_disp_event = TRUE;
733                 break;
734               case '=':
735                 app_keypad_keys = TRUE;
736                 break;
737               case '>':
738                 app_keypad_keys = FALSE;
739                 break;
740               case 'D':                /* exactly equivalent to LF */
741                 if (curs_y == marg_b)
742                     scroll (marg_t, marg_b, 1, TRUE);
743                 else if (curs_y < rows-1)
744                     curs_y++;
745                 fix_cpos;
746                 wrapnext = FALSE;
747                 seen_disp_event = TRUE;
748                 nl_count++;
749                 break;
750               case 'E':                /* exactly equivalent to CR-LF */
751                 curs_x = 0;
752                 wrapnext = FALSE;
753                 if (curs_y == marg_b)
754                     scroll (marg_t, marg_b, 1, TRUE);
755                 else if (curs_y < rows-1)
756                     curs_y++;
757                 fix_cpos;
758                 wrapnext = FALSE;
759                 nl_count++;
760                 seen_disp_event = TRUE;
761                 break;
762               case 'M':                /* reverse index - backwards LF */
763                 if (curs_y == marg_t)
764                     scroll (marg_t, marg_b, -1, TRUE);
765                 else if (curs_y > 0)
766                     curs_y--;
767                 fix_cpos;
768                 wrapnext = FALSE;
769                 seen_disp_event = TRUE;
770                 break;
771               case 'Z':                /* terminal type query */
772                 ldisc->send ("\033[?6c", 5);
773                 break;
774               case 'c':                /* restore power-on settings */
775                 power_on();
776                 fix_cpos;
777                 disptop = scrtop;
778                 seen_disp_event = TRUE;
779                 break;
780               case '#':                /* ESC # 8 fills screen with Es :-) */
781                 termstate = SEEN_ESCHASH;
782                 break;
783               case 'H':                /* set a tab */
784                 tabs[curs_x] = TRUE;
785                 break;
786             }
787             break;
788           case SEEN_CSI:
789             termstate = TOPLEVEL;      /* default */
790             switch (c) {
791               case '0': case '1': case '2': case '3': case '4':
792               case '5': case '6': case '7': case '8': case '9':
793                 if (esc_nargs <= ARGS_MAX) {
794                     if (esc_args[esc_nargs-1] == ARG_DEFAULT)
795                         esc_args[esc_nargs-1] = 0;
796                     esc_args[esc_nargs-1] =
797                         10 * esc_args[esc_nargs-1] + c - '0';
798                 }
799                 termstate = SEEN_CSI;
800                 break;
801               case ';':
802                 if (++esc_nargs <= ARGS_MAX)
803                     esc_args[esc_nargs-1] = ARG_DEFAULT;
804                 termstate = SEEN_CSI;
805                 break;
806               case '?':
807                 esc_query = TRUE;
808                 termstate = SEEN_CSI;
809                 break;
810               case 'A':                /* move up N lines */
811                 move (curs_x, curs_y - def(esc_args[0], 1), 1);
812                 seen_disp_event = TRUE;
813                 break;
814               case 'B': case 'e':      /* move down N lines */
815                 move (curs_x, curs_y + def(esc_args[0], 1), 1);
816                 seen_disp_event = TRUE;
817                 break;
818               case 'C': case 'a':      /* move right N cols */
819                 move (curs_x + def(esc_args[0], 1), curs_y, 1);
820                 seen_disp_event = TRUE;
821                 break;
822               case 'D':                /* move left N cols */
823                 move (curs_x - def(esc_args[0], 1), curs_y, 1);
824                 seen_disp_event = TRUE;
825                 break;
826               case 'E':                /* move down N lines and CR */
827                 move (0, curs_y + def(esc_args[0], 1), 1);
828                 seen_disp_event = TRUE;
829                 break;
830               case 'F':                /* move up N lines and CR */
831                 move (0, curs_y - def(esc_args[0], 1), 1);
832                 seen_disp_event = TRUE;
833                 break;
834               case 'G': case '`':      /* set horizontal posn */
835                 move (def(esc_args[0], 1) - 1, curs_y, 0);
836                 seen_disp_event = TRUE;
837                 break;
838               case 'd':                /* set vertical posn */
839                 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
840                       (dec_om ? 2 : 0));
841                 seen_disp_event = TRUE;
842                 break;
843               case 'H': case 'f':      /* set horz and vert posns at once */
844                 if (esc_nargs < 2)
845                     esc_args[1] = ARG_DEFAULT;
846                 move (def(esc_args[1], 1) - 1,
847                       (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
848                       (dec_om ? 2 : 0));
849                 seen_disp_event = TRUE;
850                 break;
851               case 'J':                /* erase screen or parts of it */
852                 {
853                     unsigned int i = def(esc_args[0], 0) + 1;
854                     if (i > 3)
855                         i = 0;
856                     erase_lots(FALSE, !!(i & 2), !!(i & 1));
857                 }
858                 disptop = scrtop;
859                 seen_disp_event = TRUE;
860                 break;
861               case 'K':                /* erase line or parts of it */
862                 {
863                     unsigned int i = def(esc_args[0], 0) + 1;
864                     if (i > 3)
865                         i = 0;
866                     erase_lots(TRUE, !!(i & 2), !!(i & 1));
867                 }
868                 seen_disp_event = TRUE;
869                 break;
870               case 'L':                /* insert lines */
871                 if (curs_y <= marg_b)
872                     scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
873                 seen_disp_event = TRUE;
874                 break;
875               case 'M':                /* delete lines */
876                 if (curs_y <= marg_b)
877                     scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
878                 seen_disp_event = TRUE;
879                 break;
880               case '@':                /* insert chars */
881                 insch (def(esc_args[0], 1));
882                 seen_disp_event = TRUE;
883                 break;
884               case 'P':                /* delete chars */
885                 insch (-def(esc_args[0], 1));
886                 seen_disp_event = TRUE;
887                 break;
888               case 'c':                /* terminal type query */
889                 ldisc->send ("\033[?6c", 5);
890                 break;
891               case 'n':                /* cursor position query */
892                 if (esc_args[0] == 6) {
893                     char buf[32];
894                     sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
895                     ldisc->send (buf, strlen(buf));
896                 }
897                 break;
898               case 'h':                /* toggle a mode to high */
899                 toggle_mode (esc_args[0], esc_query, TRUE);
900                 break;
901               case 'l':                /* toggle a mode to low */
902                 toggle_mode (esc_args[0], esc_query, FALSE);
903                 break;
904               case 'g':                /* clear tabs */
905                 if (esc_nargs == 1) {
906                     if (esc_args[0] == 0) {
907                         tabs[curs_x] = FALSE;
908                     } else if (esc_args[0] == 3) {
909                         int i;
910                         for (i = 0; i < cols; i++)
911                             tabs[i] = FALSE;
912                     }
913                 }
914                 break;
915               case 'r':                /* set scroll margins */
916                 if (!esc_query && esc_nargs <= 2) {
917                     int top, bot;
918                     top = def(esc_args[0], 1) - 1;
919                     if (top < 0)
920                         top = 0;
921                     bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
922                            def(esc_args[1], rows)) - 1;
923                     if (bot >= rows)
924                         bot = rows-1;
925                     if (top <= bot) {
926                         marg_t = top;
927                         marg_b = bot;
928                         curs_x = 0;
929                         /*
930                          * I used to think the cursor should be
931                          * placed at the top of the newly marginned
932                          * area. Apparently not: VMS TPU falls over
933                          * if so.
934                          */
935                         curs_y = 0;
936                         fix_cpos;
937                         seen_disp_event = TRUE;
938                     }
939                 }
940                 break;
941               case 'm':                /* set graphics rendition */
942                 {
943                     int i;
944                     for (i=0; i<esc_nargs; i++) {
945                         switch (def(esc_args[i], 0)) {
946                           case 0:      /* restore defaults */
947                             curr_attr = ATTR_DEFAULT; break;
948                           case 1:      /* enable bold */
949                             curr_attr |= ATTR_BOLD; break;
950                           case 4:      /* enable underline */
951                           case 21:     /* (enable double underline) */
952                             curr_attr |= ATTR_UNDER; break;
953                           case 7:      /* enable reverse video */
954                             curr_attr |= ATTR_REVERSE; break;
955                           case 22:     /* disable bold */
956                             curr_attr &= ~ATTR_BOLD; break;
957                           case 24:     /* disable underline */
958                             curr_attr &= ~ATTR_UNDER; break;
959                           case 27:     /* disable reverse video */
960                             curr_attr &= ~ATTR_REVERSE; break;
961                           case 30: case 31: case 32: case 33:
962                           case 34: case 35: case 36: case 37:
963                             /* foreground */
964                             curr_attr &= ~ATTR_FGMASK;
965                             curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
966                             break;
967                           case 39:     /* default-foreground */
968                             curr_attr &= ~ATTR_FGMASK;
969                             curr_attr |= ATTR_DEFFG;
970                             break;
971                           case 40: case 41: case 42: case 43:
972                           case 44: case 45: case 46: case 47:
973                             /* background */
974                             curr_attr &= ~ATTR_BGMASK;
975                             curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
976                             break;
977                           case 49:     /* default-background */
978                             curr_attr &= ~ATTR_BGMASK;
979                             curr_attr |= ATTR_DEFBG;
980                             break;
981                         }
982                     }
983                 }
984                 break;
985               case 's':                /* save cursor */
986                 save_cursor (TRUE);
987                 break;
988               case 'u':                /* restore cursor */
989                 save_cursor (FALSE);
990                 seen_disp_event = TRUE;
991                 break;
992               case 't':                /* set page size - ie window height */
993                 request_resize (cols, def(esc_args[0], 24));
994                 deselect();
995                 break;
996               case 'X':                /* write N spaces w/o moving cursor */
997                 {
998                     int n = def(esc_args[0], 1);
999                     unsigned long *p = cpos;
1000                     if (n > cols - curs_x)
1001                         n = cols - curs_x;
1002                     check_selection (cpos, cpos+n);
1003                     while (n--)
1004                         *p++ = ERASE_CHAR;
1005                     seen_disp_event = TRUE;
1006                 }
1007                 break;
1008               case 'x':                /* report terminal characteristics */
1009                 {
1010                     char buf[32];
1011                     int i = def(esc_args[0], 0);
1012                     if (i == 0 || i == 1) {
1013                         strcpy (buf, "\033[2;1;1;112;112;1;0x");
1014                         buf[2] += i;
1015                         ldisc->send (buf, 20);
1016                     }
1017                 }
1018                 break;
1019             }
1020             break;
1021           case SET_GL:
1022           case SET_GR:
1023             switch (c) {
1024               case 'A':
1025                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1026                 break;
1027               case '0':
1028                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1029                 break;
1030               default:                 /* specifically, 'B' */
1031                 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1032                 break;
1033             }
1034             termstate = TOPLEVEL;
1035             break;
1036           case SEEN_OSC:
1037             osc_w = FALSE;
1038             switch (c) {
1039               case 'P':                /* Linux palette sequence */
1040                 termstate = SEEN_OSC_P;
1041                 osc_strlen = 0;
1042                 break;
1043               case 'R':                /* Linux palette reset */
1044                 palette_reset();
1045                 term_invalidate();
1046                 termstate = TOPLEVEL;
1047                 break;
1048               case 'W':                /* word-set */
1049                 termstate = SEEN_OSC_W;
1050                 osc_w = TRUE;
1051                 break;
1052               case '0': case '1': case '2': case '3': case '4':
1053               case '5': case '6': case '7': case '8': case '9':
1054                 esc_args[0] = 10 * esc_args[0] + c - '0';
1055                 break;
1056               case 'L':
1057                 /*
1058                  * Grotty hack to support xterm and DECterm title
1059                  * sequences concurrently.
1060                  */
1061                 if (esc_args[0] == 2) {
1062                     esc_args[0] = 1;
1063                     break;
1064                 }
1065                 /* else fall through */
1066               default:
1067                 termstate = OSC_STRING;
1068                 osc_strlen = 0;
1069             }
1070             break;
1071           case OSC_STRING:
1072             if (c == 0234 || c == '\007') {
1073                 /*
1074                  * These characters terminate the string; ST and BEL
1075                  * terminate the sequence and trigger instant
1076                  * processing of it, whereas ESC goes back to SEEN_ESC
1077                  * mode unless it is followed by \, in which case it is
1078                  * synonymous with ST in the first place.
1079                  */
1080                 do_osc();
1081                 termstate = TOPLEVEL;
1082             } else if (c == '\033')
1083                     termstate = OSC_MAYBE_ST;
1084             else if (osc_strlen < OSC_STR_MAX)
1085                 osc_string[osc_strlen++] = c;
1086             break;
1087           case SEEN_OSC_P:
1088             {
1089                 int max = (osc_strlen == 0 ? 21 : 16);
1090                 int val;
1091                 if (c >= '0' && c <= '9')
1092                     val = c - '0';
1093                 else if (c >= 'A' && c <= 'A'+max-10)
1094                     val = c - 'A' + 10;
1095                 else if (c >= 'a' && c <= 'a'+max-10)
1096                     val = c - 'a' + 10;
1097                 else
1098                     termstate = TOPLEVEL;
1099                 osc_string[osc_strlen++] = val;
1100                 if (osc_strlen >= 7) {
1101                     palette_set (osc_string[0],
1102                                  osc_string[1] * 16 + osc_string[2],
1103                                  osc_string[3] * 16 + osc_string[4],
1104                                  osc_string[5] * 16 + osc_string[6]);
1105                     term_invalidate();
1106                     termstate = TOPLEVEL;
1107                 }
1108             }
1109             break;
1110           case SEEN_OSC_W:
1111             switch (c) {
1112               case '0': case '1': case '2': case '3': case '4':
1113               case '5': case '6': case '7': case '8': case '9':
1114                 esc_args[0] = 10 * esc_args[0] + c - '0';
1115                 break;
1116               default:
1117                 termstate = OSC_STRING;
1118                 osc_strlen = 0;
1119             }
1120             break;
1121           case SEEN_ESCHASH:
1122             if (c == '8') {
1123                 unsigned long *p = scrtop;
1124                 int n = rows * (cols+1);
1125                 while (n--)
1126                     *p++ = ATTR_DEFAULT | 'E';
1127                 disptop = scrtop;
1128                 seen_disp_event = TRUE;
1129                 check_selection (scrtop, scrtop + rows * (cols+1));
1130             }
1131             termstate = TOPLEVEL;
1132             break;
1133         }
1134         check_selection (cpos, cpos+1);
1135     }
1136         
1137     if (nl_count > MAXNL)
1138         term_update();
1139 }
1140
1141 /*
1142  * Compare two lines to determine whether they are sufficiently
1143  * alike to scroll-optimise one to the other. Return the degree of
1144  * similarity.
1145  */
1146 static int linecmp (unsigned long *a, unsigned long *b) {
1147     int i, n;
1148
1149     for (i=n=0; i < cols; i++)
1150         n += (*a++ == *b++);
1151     return n;
1152 }
1153
1154 /*
1155  * Given a context, update the window. Out of paranoia, we don't
1156  * allow WM_PAINT responses to do scrolling optimisations.
1157  */
1158 static void do_paint (Context ctx, int may_optimise){ 
1159     int i, j, start, our_curs_y;
1160     unsigned long attr, rv, cursor;
1161     char ch[1024];
1162
1163     cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1164     rv = (rvideo ? ATTR_REVERSE : 0);
1165     our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1166
1167     for (i=0; i<rows; i++) {
1168         int idx = i*(cols+1);
1169         for (j=0; j<=cols; j++,idx++) {
1170             unsigned long *d = disptop+idx;
1171             wanttext[idx] = ((*d ^ rv
1172                               ^ (selstart <= d && d < selend ?
1173                                  ATTR_REVERSE : 0)) |
1174                              (i==our_curs_y && j==curs_x ? cursor : 0));
1175         }
1176     }
1177
1178     /*
1179      * We would perform scrolling optimisations in here, if they
1180      * didn't have a nasty tendency to cause the whole sodding
1181      * program to hang for a second at speed-critical moments.
1182      * We'll leave it well alone...
1183      */
1184
1185     for (i=0; i<rows; i++) {
1186         int idx = i*(cols+1);
1187         start = -1;
1188         for (j=0; j<=cols; j++,idx++) {
1189             unsigned long t = wanttext[idx];
1190             int needs_update = (j < cols && t != disptext[idx]);
1191             int keep_going = (start != -1 && needs_update &&
1192                               (t & ATTR_MASK) == attr &&
1193                               j-start < sizeof(ch));
1194             if (start != -1 && !keep_going) {
1195                 do_text (ctx, start, i, ch, j-start, attr);
1196                 start = -1;
1197             }
1198             if (needs_update) {
1199                 if (start == -1) {
1200                     start = j;
1201                     attr = t & ATTR_MASK;
1202                 }
1203                 ch[j-start] = (char) (t & CHAR_MASK);
1204             }
1205             disptext[idx] = t;
1206         }
1207     }
1208 }
1209
1210 /*
1211  * Invalidate the whole screen so it will be repainted in full.
1212  */
1213 void term_invalidate(void) {
1214     int i;
1215
1216     for (i=0; i<rows*(cols+1); i++)
1217         disptext[i] = ATTR_INVALID;
1218 }
1219
1220 /*
1221  * Paint the window in response to a WM_PAINT message.
1222  */
1223 void term_paint (Context ctx, int l, int t, int r, int b) {
1224     int i, j, left, top, right, bottom;
1225
1226     left = l / font_width;
1227     right = (r - 1) / font_width;
1228     top = t / font_height;
1229     bottom = (b - 1) / font_height;
1230     for (i = top; i <= bottom && i < rows ; i++)
1231       for (j = left; j <= right && j < cols ; j++)
1232             disptext[i*(cols+1)+j] = ATTR_INVALID;
1233
1234     do_paint (ctx, FALSE);
1235 }
1236
1237 /*
1238  * Attempt to scroll the scrollback. The second parameter gives the
1239  * position we want to scroll to; the first is +1 to denote that
1240  * this position is relative to the beginning of the scrollback, -1
1241  * to denote it is relative to the end, and 0 to denote that it is
1242  * relative to the current position.
1243  */
1244 void term_scroll (int rel, int where) {
1245     int n = where * (cols+1);
1246
1247     disptop = (rel < 0 ? scrtop :
1248                rel > 0 ? sbtop : disptop) + n;
1249     if (disptop < sbtop)
1250         disptop = sbtop;
1251     if (disptop > scrtop)
1252         disptop = scrtop;
1253     update_sbar();
1254     term_update();
1255 }
1256
1257 /*
1258  * Spread the selection outwards according to the selection mode.
1259  */
1260 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1261     unsigned long *linestart, *lineend;
1262     int x;
1263     short wvalue;
1264
1265     x = (p - text) % (cols+1);
1266     linestart = p - x;
1267     lineend = linestart + cols;
1268
1269     switch (selmode) {
1270       case SM_CHAR:
1271         /*
1272          * In this mode, every character is a separate unit, except
1273          * for runs of spaces at the end of a non-wrapping line.
1274          */
1275         if (!(linestart[cols] & ATTR_WRAPPED)) {
1276             unsigned long *q = lineend;
1277             while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1278                 q--;
1279             if (q == lineend)
1280                 q--;
1281             if (p >= q)
1282                 p = (dir == -1 ? q : lineend - 1);
1283         }
1284         break;
1285       case SM_WORD:
1286         /*
1287          * In this mode, the units are maximal runs of characters
1288          * whose `wordness' has the same value.
1289          */
1290         wvalue = wordness[*p & CHAR_MASK];
1291         if (dir == +1) {
1292             while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1293                 p++;
1294         } else {
1295             while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1296                 p--;
1297         }
1298         break;
1299       case SM_LINE:
1300         /*
1301          * In this mode, every line is a unit.
1302          */
1303         p = (dir == -1 ? linestart : lineend - 1);
1304         break;
1305     }
1306     return p;
1307 }
1308
1309 static void sel_spread (void) {
1310     selstart = sel_spread_half (selstart, -1);
1311     selend = sel_spread_half (selend - 1, +1) + 1;
1312 }
1313
1314 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1315     unsigned long *selpoint;
1316     
1317     if (y<0) y = 0;
1318     if (y>=rows) y = rows-1;
1319     if (x<0) {
1320         if (y > 0) {
1321             x = cols-1;
1322             y--;
1323         } else
1324             x = 0;
1325     }
1326     if (x>=cols) x = cols-1;
1327
1328     selpoint = disptop + y * (cols+1) + x;
1329
1330     if (b == MB_SELECT && a == MA_CLICK) {
1331         deselect();
1332         selstate = ABOUT_TO;
1333         selanchor = selpoint;
1334         selmode = SM_CHAR;
1335     } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1336         deselect();
1337         selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1338         selstate = DRAGGING;
1339         selstart = selanchor = selpoint;
1340         selend = selstart + 1;
1341         sel_spread();
1342     } else if ((b == MB_SELECT && a == MA_DRAG) ||
1343                (b == MB_EXTEND && a != MA_RELEASE)) {
1344         if (selstate == ABOUT_TO && selanchor == selpoint)
1345             return;
1346         if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1347             if (selpoint-selstart < (selend-selstart)/2)
1348                 selanchor = selend - 1;
1349             else
1350                 selanchor = selstart;
1351             selstate = DRAGGING;
1352         }
1353         if (selstate != ABOUT_TO && selstate != DRAGGING)
1354             selanchor = selpoint;
1355         selstate = DRAGGING;
1356         if (selpoint < selanchor) {
1357             selstart = selpoint;
1358             selend = selanchor + 1;
1359         } else {
1360             selstart = selanchor;
1361             selend = selpoint + 1;
1362         }
1363         sel_spread();
1364     } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1365         if (selstate == DRAGGING) {
1366             /*
1367              * We've completed a selection. We now transfer the
1368              * data to the clipboard.
1369              */
1370             unsigned char *p = selspace;
1371             unsigned long *q = selstart;
1372
1373             while (q < selend) {
1374                 int nl = FALSE;
1375                 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1376                 unsigned long *nlpos = lineend;
1377
1378                 if (!(*nlpos & ATTR_WRAPPED)) {
1379                     while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1380                         nlpos--;
1381                     if (nlpos < selend)
1382                         nl = TRUE;
1383                 }
1384                 while (q < nlpos && q < selend)
1385                     *p++ = (unsigned char) (*q++ & CHAR_MASK);
1386                 if (nl) {
1387                     int i;
1388                     for (i=0; i<sizeof(sel_nl); i++)
1389                         *p++ = sel_nl[i];
1390                 }
1391                 q = lineend + 1;       /* start of next line */
1392             }
1393             write_clip (selspace, p - selspace);
1394             selstate = SELECTED;
1395         } else
1396             selstate = NO_SELECTION;
1397     } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1398         char *data;
1399         int len;
1400
1401         get_clip((void **) &data, &len);
1402         if (data) {
1403             char *p, *q;
1404             p = q = data;
1405             while (p < data+len) {
1406                 while (p < data+len &&
1407                        !(p <= data+len-sizeof(sel_nl) &&
1408                          !memcmp(p, sel_nl, sizeof(sel_nl))))
1409                     p++;
1410
1411                 {
1412                     int i;
1413                     unsigned char c;
1414                     for(i=0;i<p-q;i++)
1415                     {
1416                         c=xlat_kbd2tty(q[i]);
1417                         ldisc->send(&c,1);
1418                     }
1419                 }
1420
1421                 if (p <= data+len-sizeof(sel_nl) &&
1422                     !memcmp(p, sel_nl, sizeof(sel_nl))) {
1423                     ldisc->send ("\r", 1);
1424                     p += sizeof(sel_nl);
1425                 }
1426                 q = p;
1427             }
1428         }
1429         get_clip(NULL, NULL);
1430     }
1431
1432     term_update();
1433 }
1434
1435 static void deselect (void) {
1436     selstate = NO_SELECTION;
1437     selstart = selend = scrtop;
1438 }
1439
1440 void term_deselect (void) {
1441     deselect();
1442     term_update();
1443 }