OSDN Git Service

Delete unused source files for 1.98d.
[ffftp/ffftp.git] / contrib / putty / TERMINAL.C
diff --git a/contrib/putty/TERMINAL.C b/contrib/putty/TERMINAL.C
deleted file mode 100644 (file)
index baadad4..0000000
+++ /dev/null
@@ -1,6613 +0,0 @@
-/*\r
- * Terminal emulator.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <ctype.h>\r
-\r
-#include <time.h>\r
-#include <assert.h>\r
-#include "putty.h"\r
-#include "terminal.h"\r
-\r
-#define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )\r
-#define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )\r
-#define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )\r
-#define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )\r
-\r
-/* Product-order comparisons for rectangular block selection. */\r
-#define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )\r
-#define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )\r
-\r
-#define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )\r
-#define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )\r
-\r
-#define VT52_PLUS\r
-\r
-#define CL_ANSIMIN     0x0001         /* Codes in all ANSI like terminals. */\r
-#define CL_VT100       0x0002         /* VT100 */\r
-#define CL_VT100AVO    0x0004         /* VT100 +AVO; 132x24 (not 132x14) & attrs */\r
-#define CL_VT102       0x0008         /* VT102 */\r
-#define CL_VT220       0x0010         /* VT220 */\r
-#define CL_VT320       0x0020         /* VT320 */\r
-#define CL_VT420       0x0040         /* VT420 */\r
-#define CL_VT510       0x0080         /* VT510, NB VT510 includes ANSI */\r
-#define CL_VT340TEXT   0x0100         /* VT340 extensions that appear in the VT420 */\r
-#define CL_SCOANSI     0x1000         /* SCOANSI not in ANSIMIN. */\r
-#define CL_ANSI                0x2000         /* ANSI ECMA-48 not in the VT100..VT420 */\r
-#define CL_OTHER       0x4000         /* Others, Xterm, linux, putty, dunno, etc */\r
-\r
-#define TM_VT100       (CL_ANSIMIN|CL_VT100)\r
-#define TM_VT100AVO    (TM_VT100|CL_VT100AVO)\r
-#define TM_VT102       (TM_VT100AVO|CL_VT102)\r
-#define TM_VT220       (TM_VT102|CL_VT220)\r
-#define TM_VTXXX       (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)\r
-#define TM_SCOANSI     (CL_ANSIMIN|CL_SCOANSI)\r
-\r
-#define TM_PUTTY       (0xFFFF)\r
-\r
-#define UPDATE_DELAY    ((TICKSPERSEC+49)/50)/* ticks to defer window update */\r
-#define TBLINK_DELAY    ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/\r
-#define CBLINK_DELAY    (CURSORBLINK) /* ticks between cursor blinks */\r
-#define VBELL_DELAY     (VBELL_TIMEOUT) /* visual bell timeout in ticks */\r
-\r
-#define compatibility(x) \\r
-    if ( ((CL_##x)&term->compatibility_level) == 0 ) {         \\r
-       term->termstate=TOPLEVEL;                       \\r
-       break;                                          \\r
-    }\r
-#define compatibility2(x,y) \\r
-    if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \\r
-       term->termstate=TOPLEVEL;                       \\r
-       break;                                          \\r
-    }\r
-\r
-#define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )\r
-\r
-char *EMPTY_WINDOW_TITLE = "";\r
-\r
-const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };\r
-\r
-#define sel_nl_sz  (sizeof(sel_nl)/sizeof(wchar_t))\r
-const wchar_t sel_nl[] = SEL_NL;\r
-\r
-/*\r
- * Fetch the character at a particular position in a line array,\r
- * for purposes of `wordtype'. The reason this isn't just a simple\r
- * array reference is that if the character we find is UCSWIDE,\r
- * then we must look one space further to the left.\r
- */\r
-#define UCSGET(a, x) \\r
-    ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )\r
-\r
-/*\r
- * Detect the various aliases of U+0020 SPACE.\r
- */\r
-#define IS_SPACE_CHR(chr) \\r
-       ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))\r
-\r
-/*\r
- * Spot magic CSETs.\r
- */\r
-#define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)\r
-\r
-/*\r
- * Internal prototypes.\r
- */\r
-static void resizeline(Terminal *, termline *, int);\r
-static termline *lineptr(Terminal *, int, int, int);\r
-static void unlineptr(termline *);\r
-static void do_paint(Terminal *, Context, int);\r
-static void erase_lots(Terminal *, int, int, int);\r
-static int find_last_nonempty_line(Terminal *, tree234 *);\r
-static void swap_screen(Terminal *, int, int, int);\r
-static void update_sbar(Terminal *);\r
-static void deselect(Terminal *);\r
-static void term_print_finish(Terminal *);\r
-static void scroll(Terminal *, int, int, int, int);\r
-#ifdef OPTIMISE_SCROLL\r
-static void scroll_display(Terminal *, int, int, int);\r
-#endif /* OPTIMISE_SCROLL */\r
-\r
-static termline *newline(Terminal *term, int cols, int bce)\r
-{\r
-    termline *line;\r
-    int j;\r
-\r
-    line = snew(termline);\r
-    line->chars = snewn(cols, termchar);\r
-    for (j = 0; j < cols; j++)\r
-       line->chars[j] = (bce ? term->erase_char : term->basic_erase_char);\r
-    line->cols = line->size = cols;\r
-    line->lattr = LATTR_NORM;\r
-    line->temporary = FALSE;\r
-    line->cc_free = 0;\r
-\r
-    return line;\r
-}\r
-\r
-static void freeline(termline *line)\r
-{\r
-    if (line) {\r
-       sfree(line->chars);\r
-       sfree(line);\r
-    }\r
-}\r
-\r
-static void unlineptr(termline *line)\r
-{\r
-    if (line->temporary)\r
-       freeline(line);\r
-}\r
-\r
-#ifdef TERM_CC_DIAGS\r
-/*\r
- * Diagnostic function: verify that a termline has a correct\r
- * combining character structure.\r
- * \r
- * This is a performance-intensive check, so it's no longer enabled\r
- * by default.\r
- */\r
-static void cc_check(termline *line)\r
-{\r
-    unsigned char *flags;\r
-    int i, j;\r
-\r
-    assert(line->size >= line->cols);\r
-\r
-    flags = snewn(line->size, unsigned char);\r
-\r
-    for (i = 0; i < line->size; i++)\r
-       flags[i] = (i < line->cols);\r
-\r
-    for (i = 0; i < line->cols; i++) {\r
-       j = i;\r
-       while (line->chars[j].cc_next) {\r
-           j += line->chars[j].cc_next;\r
-           assert(j >= line->cols && j < line->size);\r
-           assert(!flags[j]);\r
-           flags[j] = TRUE;\r
-       }\r
-    }\r
-\r
-    j = line->cc_free;\r
-    if (j) {\r
-       while (1) {\r
-           assert(j >= line->cols && j < line->size);\r
-           assert(!flags[j]);\r
-           flags[j] = TRUE;\r
-           if (line->chars[j].cc_next)\r
-               j += line->chars[j].cc_next;\r
-           else\r
-               break;\r
-       }\r
-    }\r
-\r
-    j = 0;\r
-    for (i = 0; i < line->size; i++)\r
-       j += (flags[i] != 0);\r
-\r
-    assert(j == line->size);\r
-\r
-    sfree(flags);\r
-}\r
-#endif\r
-\r
-/*\r
- * Add a combining character to a character cell.\r
- */\r
-static void add_cc(termline *line, int col, unsigned long chr)\r
-{\r
-    int newcc;\r
-\r
-    assert(col >= 0 && col < line->cols);\r
-\r
-    /*\r
-     * Start by extending the cols array if the free list is empty.\r
-     */\r
-    if (!line->cc_free) {\r
-       int n = line->size;\r
-       line->size += 16 + (line->size - line->cols) / 2;\r
-       line->chars = sresize(line->chars, line->size, termchar);\r
-       line->cc_free = n;\r
-       while (n < line->size) {\r
-           if (n+1 < line->size)\r
-               line->chars[n].cc_next = 1;\r
-           else\r
-               line->chars[n].cc_next = 0;\r
-           n++;\r
-       }\r
-    }\r
-\r
-    /*\r
-     * Now walk the cc list of the cell in question.\r
-     */\r
-    while (line->chars[col].cc_next)\r
-       col += line->chars[col].cc_next;\r
-\r
-    /*\r
-     * `col' now points at the last cc currently in this cell; so\r
-     * we simply add another one.\r
-     */\r
-    newcc = line->cc_free;\r
-    if (line->chars[newcc].cc_next)\r
-       line->cc_free = newcc + line->chars[newcc].cc_next;\r
-    else\r
-       line->cc_free = 0;\r
-    line->chars[newcc].cc_next = 0;\r
-    line->chars[newcc].chr = chr;\r
-    line->chars[col].cc_next = newcc - col;\r
-\r
-#ifdef TERM_CC_DIAGS\r
-    cc_check(line);\r
-#endif\r
-}\r
-\r
-/*\r
- * Clear the combining character list in a character cell.\r
- */\r
-static void clear_cc(termline *line, int col)\r
-{\r
-    int oldfree, origcol = col;\r
-\r
-    assert(col >= 0 && col < line->cols);\r
-\r
-    if (!line->chars[col].cc_next)\r
-       return;                        /* nothing needs doing */\r
-\r
-    oldfree = line->cc_free;\r
-    line->cc_free = col + line->chars[col].cc_next;\r
-    while (line->chars[col].cc_next)\r
-       col += line->chars[col].cc_next;\r
-    if (oldfree)\r
-       line->chars[col].cc_next = oldfree - col;\r
-    else\r
-       line->chars[col].cc_next = 0;\r
-\r
-    line->chars[origcol].cc_next = 0;\r
-\r
-#ifdef TERM_CC_DIAGS\r
-    cc_check(line);\r
-#endif\r
-}\r
-\r
-/*\r
- * Compare two character cells for equality. Special case required\r
- * in do_paint() where we override what we expect the chr and attr\r
- * fields to be.\r
- */\r
-static int termchars_equal_override(termchar *a, termchar *b,\r
-                                   unsigned long bchr, unsigned long battr)\r
-{\r
-    /* FULL-TERMCHAR */\r
-    if (a->chr != bchr)\r
-       return FALSE;\r
-    if ((a->attr &~ DATTR_MASK) != (battr &~ DATTR_MASK))\r
-       return FALSE;\r
-    while (a->cc_next || b->cc_next) {\r
-       if (!a->cc_next || !b->cc_next)\r
-           return FALSE;              /* one cc-list ends, other does not */\r
-       a += a->cc_next;\r
-       b += b->cc_next;\r
-       if (a->chr != b->chr)\r
-           return FALSE;\r
-    }\r
-    return TRUE;\r
-}\r
-\r
-static int termchars_equal(termchar *a, termchar *b)\r
-{\r
-    return termchars_equal_override(a, b, b->chr, b->attr);\r
-}\r
-\r
-/*\r
- * Copy a character cell. (Requires a pointer to the destination\r
- * termline, so as to access its free list.)\r
- */\r
-static void copy_termchar(termline *destline, int x, termchar *src)\r
-{\r
-    clear_cc(destline, x);\r
-\r
-    destline->chars[x] = *src;        /* copy everything except cc-list */\r
-    destline->chars[x].cc_next = 0;    /* and make sure this is zero */\r
-\r
-    while (src->cc_next) {\r
-       src += src->cc_next;\r
-       add_cc(destline, x, src->chr);\r
-    }\r
-\r
-#ifdef TERM_CC_DIAGS\r
-    cc_check(destline);\r
-#endif\r
-}\r
-\r
-/*\r
- * Move a character cell within its termline.\r
- */\r
-static void move_termchar(termline *line, termchar *dest, termchar *src)\r
-{\r
-    /* First clear the cc list from the original char, just in case. */\r
-    clear_cc(line, dest - line->chars);\r
-\r
-    /* Move the character cell and adjust its cc_next. */\r
-    *dest = *src;                     /* copy everything except cc-list */\r
-    if (src->cc_next)\r
-       dest->cc_next = src->cc_next - (dest-src);\r
-\r
-    /* Ensure the original cell doesn't have a cc list. */\r
-    src->cc_next = 0;\r
-\r
-#ifdef TERM_CC_DIAGS\r
-    cc_check(line);\r
-#endif\r
-}\r
-\r
-/*\r
- * Compress and decompress a termline into an RLE-based format for\r
- * storing in scrollback. (Since scrollback almost never needs to\r
- * be modified and exists in huge quantities, this is a sensible\r
- * tradeoff, particularly since it allows us to continue adding\r
- * features to the main termchar structure without proportionally\r
- * bloating the terminal emulator's memory footprint unless those\r
- * features are in constant use.)\r
- */\r
-struct buf {\r
-    unsigned char *data;\r
-    int len, size;\r
-};\r
-static void add(struct buf *b, unsigned char c)\r
-{\r
-    if (b->len >= b->size) {\r
-       b->size = (b->len * 3 / 2) + 512;\r
-       b->data = sresize(b->data, b->size, unsigned char);\r
-    }\r
-    b->data[b->len++] = c;\r
-}\r
-static int get(struct buf *b)\r
-{\r
-    return b->data[b->len++];\r
-}\r
-static void makerle(struct buf *b, termline *ldata,\r
-                   void (*makeliteral)(struct buf *b, termchar *c,\r
-                                       unsigned long *state))\r
-{\r
-    int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;\r
-    termchar *c = ldata->chars;\r
-    unsigned long state = 0, oldstate;\r
-\r
-    n = ldata->cols;\r
-\r
-    hdrpos = b->len;\r
-    hdrsize = 0;\r
-    add(b, 0);\r
-    prevlen = prevpos = 0;\r
-    prev2 = FALSE;\r
-\r
-    while (n-- > 0) {\r
-       thispos = b->len;\r
-       makeliteral(b, c++, &state);\r
-       thislen = b->len - thispos;\r
-       if (thislen == prevlen &&\r
-           !memcmp(b->data + prevpos, b->data + thispos, thislen)) {\r
-           /*\r
-            * This literal precisely matches the previous one.\r
-            * Turn it into a run if it's worthwhile.\r
-            * \r
-            * With one-byte literals, it costs us two bytes to\r
-            * encode a run, plus another byte to write the header\r
-            * to resume normal output; so a three-element run is\r
-            * neutral, and anything beyond that is unconditionally\r
-            * worthwhile. With two-byte literals or more, even a\r
-            * 2-run is a win.\r
-            */\r
-           if (thislen > 1 || prev2) {\r
-               int runpos, runlen;\r
-\r
-               /*\r
-                * It's worth encoding a run. Start at prevpos,\r
-                * unless hdrsize==0 in which case we can back up\r
-                * another one and start by overwriting hdrpos.\r
-                */\r
-\r
-               hdrsize--;             /* remove the literal at prevpos */\r
-               if (prev2) {\r
-                   assert(hdrsize > 0);\r
-                   hdrsize--;\r
-                   prevpos -= prevlen;/* and possibly another one */\r
-               }\r
-\r
-               if (hdrsize == 0) {\r
-                   assert(prevpos == hdrpos + 1);\r
-                   runpos = hdrpos;\r
-                   b->len = prevpos+prevlen;\r
-               } else {\r
-                   memmove(b->data + prevpos+1, b->data + prevpos, prevlen);\r
-                   runpos = prevpos;\r
-                   b->len = prevpos+prevlen+1;\r
-                   /*\r
-                    * Terminate the previous run of ordinary\r
-                    * literals.\r
-                    */\r
-                   assert(hdrsize >= 1 && hdrsize <= 128);\r
-                   b->data[hdrpos] = hdrsize - 1;\r
-               }\r
-\r
-               runlen = prev2 ? 3 : 2;\r
-\r
-               while (n > 0 && runlen < 129) {\r
-                   int tmppos, tmplen;\r
-                   tmppos = b->len;\r
-                   oldstate = state;\r
-                   makeliteral(b, c, &state);\r
-                   tmplen = b->len - tmppos;\r
-                   b->len = tmppos;\r
-                   if (tmplen != thislen ||\r
-                       memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {\r
-                       state = oldstate;\r
-                       break;         /* run over */\r
-                   }\r
-                   n--, c++, runlen++;\r
-               }\r
-\r
-               assert(runlen >= 2 && runlen <= 129);\r
-               b->data[runpos] = runlen + 0x80 - 2;\r
-\r
-               hdrpos = b->len;\r
-               hdrsize = 0;\r
-               add(b, 0);\r
-               /* And ensure this run doesn't interfere with the next. */\r
-               prevlen = prevpos = 0;\r
-               prev2 = FALSE;\r
-\r
-               continue;\r
-           } else {\r
-               /*\r
-                * Just flag that the previous two literals were\r
-                * identical, in case we find a third identical one\r
-                * we want to turn into a run.\r
-                */\r
-               prev2 = TRUE;\r
-               prevlen = thislen;\r
-               prevpos = thispos;\r
-           }\r
-       } else {\r
-           prev2 = FALSE;\r
-           prevlen = thislen;\r
-           prevpos = thispos;\r
-       }\r
-\r
-       /*\r
-        * This character isn't (yet) part of a run. Add it to\r
-        * hdrsize.\r
-        */\r
-       hdrsize++;\r
-       if (hdrsize == 128) {\r
-           b->data[hdrpos] = hdrsize - 1;\r
-           hdrpos = b->len;\r
-           hdrsize = 0;\r
-           add(b, 0);\r
-           prevlen = prevpos = 0;\r
-           prev2 = FALSE;\r
-       }\r
-    }\r
-\r
-    /*\r
-     * Clean up.\r
-     */\r
-    if (hdrsize > 0) {\r
-       assert(hdrsize <= 128);\r
-       b->data[hdrpos] = hdrsize - 1;\r
-    } else {\r
-       b->len = hdrpos;\r
-    }\r
-}\r
-static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)\r
-{\r
-    /*\r
-     * My encoding for characters is UTF-8-like, in that it stores\r
-     * 7-bit ASCII in one byte and uses high-bit-set bytes as\r
-     * introducers to indicate a longer sequence. However, it's\r
-     * unlike UTF-8 in that it doesn't need to be able to\r
-     * resynchronise, and therefore I don't want to waste two bits\r
-     * per byte on having recognisable continuation characters.\r
-     * Also I don't want to rule out the possibility that I may one\r
-     * day use values 0x80000000-0xFFFFFFFF for interesting\r
-     * purposes, so unlike UTF-8 I need a full 32-bit range.\r
-     * Accordingly, here is my encoding:\r
-     * \r
-     * 00000000-0000007F: 0xxxxxxx (but see below)\r
-     * 00000080-00003FFF: 10xxxxxx xxxxxxxx\r
-     * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx\r
-     * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx\r
-     * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx\r
-     * \r
-     * (`Z' is like `x' but is always going to be zero since the\r
-     * values I'm encoding don't go above 2^32. In principle the\r
-     * five-byte form of the encoding could extend to 2^35, and\r
-     * there could be six-, seven-, eight- and nine-byte forms as\r
-     * well to allow up to 64-bit values to be encoded. But that's\r
-     * completely unnecessary for these purposes!)\r
-     * \r
-     * The encoding as written above would be very simple, except\r
-     * that 7-bit ASCII can occur in several different ways in the\r
-     * terminal data; sometimes it crops up in the D800 page\r
-     * (CSET_ASCII) but at other times it's in the 0000 page (real\r
-     * Unicode). Therefore, this encoding is actually _stateful_:\r
-     * the one-byte encoding of 00-7F actually indicates `reuse the\r
-     * upper three bytes of the last character', and to encode an\r
-     * absolute value of 00-7F you need to use the two-byte form\r
-     * instead.\r
-     */\r
-    if ((c->chr & ~0x7F) == *state) {\r
-       add(b, (unsigned char)(c->chr & 0x7F));\r
-    } else if (c->chr < 0x4000) {\r
-       add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));\r
-       add(b, (unsigned char)(c->chr & 0xFF));\r
-    } else if (c->chr < 0x200000) {\r
-       add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));\r
-       add(b, (unsigned char)((c->chr >> 8) & 0xFF));\r
-       add(b, (unsigned char)(c->chr & 0xFF));\r
-    } else if (c->chr < 0x10000000) {\r
-       add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));\r
-       add(b, (unsigned char)((c->chr >> 16) & 0xFF));\r
-       add(b, (unsigned char)((c->chr >> 8) & 0xFF));\r
-       add(b, (unsigned char)(c->chr & 0xFF));\r
-    } else {\r
-       add(b, 0xF0);\r
-       add(b, (unsigned char)((c->chr >> 24) & 0xFF));\r
-       add(b, (unsigned char)((c->chr >> 16) & 0xFF));\r
-       add(b, (unsigned char)((c->chr >> 8) & 0xFF));\r
-       add(b, (unsigned char)(c->chr & 0xFF));\r
-    }\r
-    *state = c->chr & ~0xFF;\r
-}\r
-static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)\r
-{\r
-    /*\r
-     * My encoding for attributes is 16-bit-granular and assumes\r
-     * that the top bit of the word is never required. I either\r
-     * store a two-byte value with the top bit clear (indicating\r
-     * just that value), or a four-byte value with the top bit set\r
-     * (indicating the same value with its top bit clear).\r
-     * \r
-     * However, first I permute the bits of the attribute value, so\r
-     * that the eight bits of colour (four in each of fg and bg)\r
-     * which are never non-zero unless xterm 256-colour mode is in\r
-     * use are placed higher up the word than everything else. This\r
-     * ensures that attribute values remain 16-bit _unless_ the\r
-     * user uses extended colour.\r
-     */\r
-    unsigned attr, colourbits;\r
-\r
-    attr = c->attr;\r
-\r
-    assert(ATTR_BGSHIFT > ATTR_FGSHIFT);\r
-\r
-    colourbits = (attr >> (ATTR_BGSHIFT + 4)) & 0xF;\r
-    colourbits <<= 4;\r
-    colourbits |= (attr >> (ATTR_FGSHIFT + 4)) & 0xF;\r
-\r
-    attr = (((attr >> (ATTR_BGSHIFT + 8)) << (ATTR_BGSHIFT + 4)) |\r
-           (attr & ((1 << (ATTR_BGSHIFT + 4))-1)));\r
-    attr = (((attr >> (ATTR_FGSHIFT + 8)) << (ATTR_FGSHIFT + 4)) |\r
-           (attr & ((1 << (ATTR_FGSHIFT + 4))-1)));\r
-\r
-    attr |= (colourbits << (32-9));\r
-\r
-    if (attr < 0x8000) {\r
-       add(b, (unsigned char)((attr >> 8) & 0xFF));\r
-       add(b, (unsigned char)(attr & 0xFF));\r
-    } else {\r
-       add(b, (unsigned char)(((attr >> 24) & 0x7F) | 0x80));\r
-       add(b, (unsigned char)((attr >> 16) & 0xFF));\r
-       add(b, (unsigned char)((attr >> 8) & 0xFF));\r
-       add(b, (unsigned char)(attr & 0xFF));\r
-    }\r
-}\r
-static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)\r
-{\r
-    /*\r
-     * For combining characters, I just encode a bunch of ordinary\r
-     * chars using makeliteral_chr, and terminate with a \0\r
-     * character (which I know won't come up as a combining char\r
-     * itself).\r
-     * \r
-     * I don't use the stateful encoding in makeliteral_chr.\r
-     */\r
-    unsigned long zstate;\r
-    termchar z;\r
-\r
-    while (c->cc_next) {\r
-       c += c->cc_next;\r
-\r
-       assert(c->chr != 0);\r
-\r
-       zstate = 0;\r
-       makeliteral_chr(b, c, &zstate);\r
-    }\r
-\r
-    z.chr = 0;\r
-    zstate = 0;\r
-    makeliteral_chr(b, &z, &zstate);\r
-}\r
-\r
-static termline *decompressline(unsigned char *data, int *bytes_used);\r
-\r
-static unsigned char *compressline(termline *ldata)\r
-{\r
-    struct buf buffer = { NULL, 0, 0 }, *b = &buffer;\r
-\r
-    /*\r
-     * First, store the column count, 7 bits at a time, least\r
-     * significant `digit' first, with the high bit set on all but\r
-     * the last.\r
-     */\r
-    {\r
-       int n = ldata->cols;\r
-       while (n >= 128) {\r
-           add(b, (unsigned char)((n & 0x7F) | 0x80));\r
-           n >>= 7;\r
-       }\r
-       add(b, (unsigned char)(n));\r
-    }\r
-\r
-    /*\r
-     * Next store the lattrs; same principle.\r
-     */\r
-    {\r
-       int n = ldata->lattr;\r
-       while (n >= 128) {\r
-           add(b, (unsigned char)((n & 0x7F) | 0x80));\r
-           n >>= 7;\r
-       }\r
-       add(b, (unsigned char)(n));\r
-    }\r
-\r
-    /*\r
-     * Now we store a sequence of separate run-length encoded\r
-     * fragments, each containing exactly as many symbols as there\r
-     * are columns in the ldata.\r
-     * \r
-     * All of these have a common basic format:\r
-     * \r
-     *  - a byte 00-7F indicates that X+1 literals follow it\r
-     *         - a byte 80-FF indicates that a single literal follows it\r
-     *           and expects to be repeated (X-0x80)+2 times.\r
-     * \r
-     * The format of the `literals' varies between the fragments.\r
-     */\r
-    makerle(b, ldata, makeliteral_chr);\r
-    makerle(b, ldata, makeliteral_attr);\r
-    makerle(b, ldata, makeliteral_cc);\r
-\r
-    /*\r
-     * Diagnostics: ensure that the compressed data really does\r
-     * decompress to the right thing.\r
-     * \r
-     * This is a bit performance-heavy for production code.\r
-     */\r
-#ifdef TERM_CC_DIAGS\r
-#ifndef CHECK_SB_COMPRESSION\r
-    {\r
-       int dused;\r
-       termline *dcl;\r
-       int i;\r
-\r
-#ifdef DIAGNOSTIC_SB_COMPRESSION\r
-       for (i = 0; i < b->len; i++) {\r
-           printf(" %02x ", b->data[i]);\r
-       }\r
-       printf("\n");\r
-#endif\r
-\r
-       dcl = decompressline(b->data, &dused);\r
-       assert(b->len == dused);\r
-       assert(ldata->cols == dcl->cols);\r
-       assert(ldata->lattr == dcl->lattr);\r
-       for (i = 0; i < ldata->cols; i++)\r
-           assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));\r
-\r
-#ifdef DIAGNOSTIC_SB_COMPRESSION\r
-       printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",\r
-              ldata->cols, 4 * ldata->cols, dused,\r
-              (double)dused / (4 * ldata->cols));\r
-#endif\r
-\r
-       freeline(dcl);\r
-    }\r
-#endif\r
-#endif /* TERM_CC_DIAGS */\r
-\r
-    /*\r
-     * Trim the allocated memory so we don't waste any, and return.\r
-     */\r
-    return sresize(b->data, b->len, unsigned char);\r
-}\r
-\r
-static void readrle(struct buf *b, termline *ldata,\r
-                   void (*readliteral)(struct buf *b, termchar *c,\r
-                                       termline *ldata, unsigned long *state))\r
-{\r
-    int n = 0;\r
-    unsigned long state = 0;\r
-\r
-    while (n < ldata->cols) {\r
-       int hdr = get(b);\r
-\r
-       if (hdr >= 0x80) {\r
-           /* A run. */\r
-\r
-           int pos = b->len, count = hdr + 2 - 0x80;\r
-           while (count--) {\r
-               assert(n < ldata->cols);\r
-               b->len = pos;\r
-               readliteral(b, ldata->chars + n, ldata, &state);\r
-               n++;\r
-           }\r
-       } else {\r
-           /* Just a sequence of consecutive literals. */\r
-\r
-           int count = hdr + 1;\r
-           while (count--) {\r
-               assert(n < ldata->cols);\r
-               readliteral(b, ldata->chars + n, ldata, &state);\r
-               n++;\r
-           }\r
-       }\r
-    }\r
-\r
-    assert(n == ldata->cols);\r
-}\r
-static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,\r
-                           unsigned long *state)\r
-{\r
-    int byte;\r
-\r
-    /*\r
-     * 00000000-0000007F: 0xxxxxxx\r
-     * 00000080-00003FFF: 10xxxxxx xxxxxxxx\r
-     * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx\r
-     * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx\r
-     * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx\r
-     */\r
-\r
-    byte = get(b);\r
-    if (byte < 0x80) {\r
-       c->chr = byte | *state;\r
-    } else if (byte < 0xC0) {\r
-       c->chr = (byte &~ 0xC0) << 8;\r
-       c->chr |= get(b);\r
-    } else if (byte < 0xE0) {\r
-       c->chr = (byte &~ 0xE0) << 16;\r
-       c->chr |= get(b) << 8;\r
-       c->chr |= get(b);\r
-    } else if (byte < 0xF0) {\r
-       c->chr = (byte &~ 0xF0) << 24;\r
-       c->chr |= get(b) << 16;\r
-       c->chr |= get(b) << 8;\r
-       c->chr |= get(b);\r
-    } else {\r
-       assert(byte == 0xF0);\r
-       c->chr = get(b) << 24;\r
-       c->chr |= get(b) << 16;\r
-       c->chr |= get(b) << 8;\r
-       c->chr |= get(b);\r
-    }\r
-    *state = c->chr & ~0xFF;\r
-}\r
-static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,\r
-                            unsigned long *state)\r
-{\r
-    unsigned val, attr, colourbits;\r
-\r
-    val = get(b) << 8;\r
-    val |= get(b);\r
-\r
-    if (val >= 0x8000) {\r
-       val &= ~0x8000;\r
-       val <<= 16;\r
-       val |= get(b) << 8;\r
-       val |= get(b);\r
-    }\r
-\r
-    colourbits = (val >> (32-9)) & 0xFF;\r
-    attr = (val & ((1<<(32-9))-1));\r
-\r
-    attr = (((attr >> (ATTR_FGSHIFT + 4)) << (ATTR_FGSHIFT + 8)) |\r
-           (attr & ((1 << (ATTR_FGSHIFT + 4))-1)));\r
-    attr = (((attr >> (ATTR_BGSHIFT + 4)) << (ATTR_BGSHIFT + 8)) |\r
-           (attr & ((1 << (ATTR_BGSHIFT + 4))-1)));\r
-\r
-    attr |= (colourbits >> 4) << (ATTR_BGSHIFT + 4);\r
-    attr |= (colourbits & 0xF) << (ATTR_FGSHIFT + 4);\r
-\r
-    c->attr = attr;\r
-}\r
-static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,\r
-                          unsigned long *state)\r
-{\r
-    termchar n;\r
-    unsigned long zstate;\r
-    int x = c - ldata->chars;\r
-\r
-    c->cc_next = 0;\r
-\r
-    while (1) {\r
-       zstate = 0;\r
-       readliteral_chr(b, &n, ldata, &zstate);\r
-       if (!n.chr)\r
-           break;\r
-       add_cc(ldata, x, n.chr);\r
-    }\r
-}\r
-\r
-static termline *decompressline(unsigned char *data, int *bytes_used)\r
-{\r
-    int ncols, byte, shift;\r
-    struct buf buffer, *b = &buffer;\r
-    termline *ldata;\r
-\r
-    b->data = data;\r
-    b->len = 0;\r
-\r
-    /*\r
-     * First read in the column count.\r
-     */\r
-    ncols = shift = 0;\r
-    do {\r
-       byte = get(b);\r
-       ncols |= (byte & 0x7F) << shift;\r
-       shift += 7;\r
-    } while (byte & 0x80);\r
-\r
-    /*\r
-     * Now create the output termline.\r
-     */\r
-    ldata = snew(termline);\r
-    ldata->chars = snewn(ncols, termchar);\r
-    ldata->cols = ldata->size = ncols;\r
-    ldata->temporary = TRUE;\r
-    ldata->cc_free = 0;\r
-\r
-    /*\r
-     * We must set all the cc pointers in ldata->chars to 0 right\r
-     * now, so that cc diagnostics that verify the integrity of the\r
-     * whole line will make sense while we're in the middle of\r
-     * building it up.\r
-     */\r
-    {\r
-       int i;\r
-       for (i = 0; i < ldata->cols; i++)\r
-           ldata->chars[i].cc_next = 0;\r
-    }\r
-\r
-    /*\r
-     * Now read in the lattr.\r
-     */\r
-    ldata->lattr = shift = 0;\r
-    do {\r
-       byte = get(b);\r
-       ldata->lattr |= (byte & 0x7F) << shift;\r
-       shift += 7;\r
-    } while (byte & 0x80);\r
-\r
-    /*\r
-     * Now we read in each of the RLE streams in turn.\r
-     */\r
-    readrle(b, ldata, readliteral_chr);\r
-    readrle(b, ldata, readliteral_attr);\r
-    readrle(b, ldata, readliteral_cc);\r
-\r
-    /* Return the number of bytes read, for diagnostic purposes. */\r
-    if (bytes_used)\r
-       *bytes_used = b->len;\r
-\r
-    return ldata;\r
-}\r
-\r
-/*\r
- * Resize a line to make it `cols' columns wide.\r
- */\r
-static void resizeline(Terminal *term, termline *line, int cols)\r
-{\r
-    int i, oldcols;\r
-\r
-    if (line->cols != cols) {\r
-\r
-       oldcols = line->cols;\r
-\r
-       /*\r
-        * This line is the wrong length, which probably means it\r
-        * hasn't been accessed since a resize. Resize it now.\r
-        * \r
-        * First, go through all the characters that will be thrown\r
-        * out in the resize (if we're shrinking the line) and\r
-        * return their cc lists to the cc free list.\r
-        */\r
-       for (i = cols; i < oldcols; i++)\r
-           clear_cc(line, i);\r
-\r
-       /*\r
-        * If we're shrinking the line, we now bodily move the\r
-        * entire cc section from where it started to where it now\r
-        * needs to be. (We have to do this before the resize, so\r
-        * that the data we're copying is still there. However, if\r
-        * we're expanding, we have to wait until _after_ the\r
-        * resize so that the space we're copying into is there.)\r
-        */\r
-       if (cols < oldcols)\r
-           memmove(line->chars + cols, line->chars + oldcols,\r
-                   (line->size - line->cols) * TSIZE);\r
-\r
-       /*\r
-        * Now do the actual resize, leaving the _same_ amount of\r
-        * cc space as there was to begin with.\r
-        */\r
-       line->size += cols - oldcols;\r
-       line->chars = sresize(line->chars, line->size, TTYPE);\r
-       line->cols = cols;\r
-\r
-       /*\r
-        * If we're expanding the line, _now_ we move the cc\r
-        * section.\r
-        */\r
-       if (cols > oldcols)\r
-           memmove(line->chars + cols, line->chars + oldcols,\r
-                   (line->size - line->cols) * TSIZE);\r
-\r
-       /*\r
-        * Go through what's left of the original line, and adjust\r
-        * the first cc_next pointer in each list. (All the\r
-        * subsequent ones are still valid because they are\r
-        * relative offsets within the cc block.) Also do the same\r
-        * to the head of the cc_free list.\r
-        */\r
-       for (i = 0; i < oldcols && i < cols; i++)\r
-           if (line->chars[i].cc_next)\r
-               line->chars[i].cc_next += cols - oldcols;\r
-       if (line->cc_free)\r
-           line->cc_free += cols - oldcols;\r
-\r
-       /*\r
-        * And finally fill in the new space with erase chars. (We\r
-        * don't have to worry about cc lists here, because we\r
-        * _know_ the erase char doesn't have one.)\r
-        */\r
-       for (i = oldcols; i < cols; i++)\r
-           line->chars[i] = term->basic_erase_char;\r
-\r
-#ifdef TERM_CC_DIAGS\r
-       cc_check(line);\r
-#endif\r
-    }\r
-}\r
-\r
-/*\r
- * Get the number of lines in the scrollback.\r
- */\r
-static int sblines(Terminal *term)\r
-{\r
-    int sblines = count234(term->scrollback);\r
-    if (term->cfg.erase_to_scrollback &&\r
-       term->alt_which && term->alt_screen) {\r
-           sblines += term->alt_sblines;\r
-    }\r
-    return sblines;\r
-}\r
-\r
-/*\r
- * Retrieve a line of the screen or of the scrollback, according to\r
- * whether the y coordinate is non-negative or negative\r
- * (respectively).\r
- */\r
-static termline *lineptr(Terminal *term, int y, int lineno, int screen)\r
-{\r
-    termline *line;\r
-    tree234 *whichtree;\r
-    int treeindex;\r
-\r
-    if (y >= 0) {\r
-       whichtree = term->screen;\r
-       treeindex = y;\r
-    } else {\r
-       int altlines = 0;\r
-\r
-       assert(!screen);\r
-\r
-       if (term->cfg.erase_to_scrollback &&\r
-           term->alt_which && term->alt_screen) {\r
-           altlines = term->alt_sblines;\r
-       }\r
-       if (y < -altlines) {\r
-           whichtree = term->scrollback;\r
-           treeindex = y + altlines + count234(term->scrollback);\r
-       } else {\r
-           whichtree = term->alt_screen;\r
-           treeindex = y + term->alt_sblines;\r
-           /* treeindex = y + count234(term->alt_screen); */\r
-       }\r
-    }\r
-    if (whichtree == term->scrollback) {\r
-       unsigned char *cline = index234(whichtree, treeindex);\r
-       line = decompressline(cline, NULL);\r
-    } else {\r
-       line = index234(whichtree, treeindex);\r
-    }\r
-\r
-    /* We assume that we don't screw up and retrieve something out of range. */\r
-    if (line == NULL) {\r
-       fatalbox("line==NULL in terminal.c\n"\r
-                "lineno=%d y=%d w=%d h=%d\n"\r
-                "count(scrollback=%p)=%d\n"\r
-                "count(screen=%p)=%d\n"\r
-                "count(alt=%p)=%d alt_sblines=%d\n"\r
-                "whichtree=%p treeindex=%d\n\n"\r
-                "Please contact <putty@projects.tartarus.org> "\r
-                "and pass on the above information.",\r
-                lineno, y, term->cols, term->rows,\r
-                term->scrollback, count234(term->scrollback),\r
-                term->screen, count234(term->screen),\r
-                term->alt_screen, count234(term->alt_screen), term->alt_sblines,\r
-                whichtree, treeindex);\r
-    }\r
-    assert(line != NULL);\r
-\r
-    resizeline(term, line, term->cols);\r
-    /* FIXME: should we sort the compressed scrollback out here? */\r
-\r
-    return line;\r
-}\r
-\r
-#define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)\r
-#define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)\r
-\r
-static void term_schedule_tblink(Terminal *term);\r
-static void term_schedule_cblink(Terminal *term);\r
-\r
-static void term_timer(void *ctx, long now)\r
-{\r
-    Terminal *term = (Terminal *)ctx;\r
-    int update = FALSE;\r
-\r
-    if (term->tblink_pending && now - term->next_tblink >= 0) {\r
-       term->tblinker = !term->tblinker;\r
-       term->tblink_pending = FALSE;\r
-       term_schedule_tblink(term);\r
-       update = TRUE;\r
-    }\r
-\r
-    if (term->cblink_pending && now - term->next_cblink >= 0) {\r
-       term->cblinker = !term->cblinker;\r
-       term->cblink_pending = FALSE;\r
-       term_schedule_cblink(term);\r
-       update = TRUE;\r
-    }\r
-\r
-    if (term->in_vbell && now - term->vbell_end >= 0) {\r
-       term->in_vbell = FALSE;\r
-       update = TRUE;\r
-    }\r
-\r
-    if (update ||\r
-       (term->window_update_pending && now - term->next_update >= 0))\r
-       term_update(term);\r
-}\r
-\r
-static void term_schedule_update(Terminal *term)\r
-{\r
-    if (!term->window_update_pending) {\r
-       term->window_update_pending = TRUE;\r
-       term->next_update = schedule_timer(UPDATE_DELAY, term_timer, term);\r
-    }\r
-}\r
-\r
-/*\r
- * Call this whenever the terminal window state changes, to queue\r
- * an update.\r
- */\r
-static void seen_disp_event(Terminal *term)\r
-{\r
-    term->seen_disp_event = TRUE;      /* for scrollback-reset-on-activity */\r
-    term_schedule_update(term);\r
-}\r
-\r
-/*\r
- * Call when the terminal's blinking-text settings change, or when\r
- * a text blink has just occurred.\r
- */\r
-static void term_schedule_tblink(Terminal *term)\r
-{\r
-    if (term->blink_is_real) {\r
-       if (!term->tblink_pending)\r
-           term->next_tblink = schedule_timer(TBLINK_DELAY, term_timer, term);\r
-       term->tblink_pending = TRUE;\r
-    } else {\r
-       term->tblinker = 1;            /* reset when not in use */\r
-       term->tblink_pending = FALSE;\r
-    }\r
-}\r
-\r
-/*\r
- * Likewise with cursor blinks.\r
- */\r
-static void term_schedule_cblink(Terminal *term)\r
-{\r
-    if (term->cfg.blink_cur && term->has_focus) {\r
-       if (!term->cblink_pending)\r
-           term->next_cblink = schedule_timer(CBLINK_DELAY, term_timer, term);\r
-       term->cblink_pending = TRUE;\r
-    } else {\r
-       term->cblinker = 1;            /* reset when not in use */\r
-       term->cblink_pending = FALSE;\r
-    }\r
-}\r
-\r
-/*\r
- * Call to reset cursor blinking on new output.\r
- */\r
-static void term_reset_cblink(Terminal *term)\r
-{\r
-    seen_disp_event(term);\r
-    term->cblinker = 1;\r
-    term->cblink_pending = FALSE;\r
-    term_schedule_cblink(term);\r
-}\r
-\r
-/*\r
- * Call to begin a visual bell.\r
- */\r
-static void term_schedule_vbell(Terminal *term, int already_started,\r
-                               long startpoint)\r
-{\r
-    long ticks_already_gone;\r
-\r
-    if (already_started)\r
-       ticks_already_gone = GETTICKCOUNT() - startpoint;\r
-    else\r
-       ticks_already_gone = 0;\r
-\r
-    if (ticks_already_gone < VBELL_DELAY) {\r
-       term->in_vbell = TRUE;\r
-       term->vbell_end = schedule_timer(VBELL_DELAY - ticks_already_gone,\r
-                                        term_timer, term);\r
-    } else {\r
-       term->in_vbell = FALSE;\r
-    }\r
-}\r
-\r
-/*\r
- * Set up power-on settings for the terminal.\r
- * If 'clear' is false, don't actually clear the primary screen, and\r
- * position the cursor below the last non-blank line (scrolling if\r
- * necessary).\r
- */\r
-static void power_on(Terminal *term, int clear)\r
-{\r
-    term->alt_x = term->alt_y = 0;\r
-    term->savecurs.x = term->savecurs.y = 0;\r
-    term->alt_savecurs.x = term->alt_savecurs.y = 0;\r
-    term->alt_t = term->marg_t = 0;\r
-    if (term->rows != -1)\r
-       term->alt_b = term->marg_b = term->rows - 1;\r
-    else\r
-       term->alt_b = term->marg_b = 0;\r
-    if (term->cols != -1) {\r
-       int i;\r
-       for (i = 0; i < term->cols; i++)\r
-           term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);\r
-    }\r
-    term->alt_om = term->dec_om = term->cfg.dec_om;\r
-    term->alt_ins = term->insert = FALSE;\r
-    term->alt_wnext = term->wrapnext =\r
-        term->save_wnext = term->alt_save_wnext = FALSE;\r
-    term->alt_wrap = term->wrap = term->cfg.wrap_mode;\r
-    term->alt_cset = term->cset = term->save_cset = term->alt_save_cset = 0;\r
-    term->alt_utf = term->utf = term->save_utf = term->alt_save_utf = 0;\r
-    term->utf_state = 0;\r
-    term->alt_sco_acs = term->sco_acs =\r
-        term->save_sco_acs = term->alt_save_sco_acs = 0;\r
-    term->cset_attr[0] = term->cset_attr[1] =\r
-        term->save_csattr = term->alt_save_csattr = CSET_ASCII;\r
-    term->rvideo = 0;\r
-    term->in_vbell = FALSE;\r
-    term->cursor_on = 1;\r
-    term->big_cursor = 0;\r
-    term->default_attr = term->save_attr =\r
-       term->alt_save_attr = term->curr_attr = ATTR_DEFAULT;\r
-    term->term_editing = term->term_echoing = FALSE;\r
-    term->app_cursor_keys = term->cfg.app_cursor;\r
-    term->app_keypad_keys = term->cfg.app_keypad;\r
-    term->use_bce = term->cfg.bce;\r
-    term->blink_is_real = term->cfg.blinktext;\r
-    term->erase_char = term->basic_erase_char;\r
-    term->alt_which = 0;\r
-    term_print_finish(term);\r
-    term->xterm_mouse = 0;\r
-    set_raw_mouse_mode(term->frontend, FALSE);\r
-    {\r
-       int i;\r
-       for (i = 0; i < 256; i++)\r
-           term->wordness[i] = term->cfg.wordness[i];\r
-    }\r
-    if (term->screen) {\r
-       swap_screen(term, 1, FALSE, FALSE);\r
-       erase_lots(term, FALSE, TRUE, TRUE);\r
-       swap_screen(term, 0, FALSE, FALSE);\r
-       if (clear)\r
-           erase_lots(term, FALSE, TRUE, TRUE);\r
-       term->curs.y = find_last_nonempty_line(term, term->screen) + 1;\r
-       if (term->curs.y == term->rows) {\r
-           term->curs.y--;\r
-           scroll(term, 0, term->rows - 1, 1, TRUE);\r
-       }\r
-    } else {\r
-       term->curs.y = 0;\r
-    }\r
-    term->curs.x = 0;\r
-    term_schedule_tblink(term);\r
-    term_schedule_cblink(term);\r
-}\r
-\r
-/*\r
- * Force a screen update.\r
- */\r
-void term_update(Terminal *term)\r
-{\r
-    Context ctx;\r
-\r
-    term->window_update_pending = FALSE;\r
-\r
-    ctx = get_ctx(term->frontend);\r
-    if (ctx) {\r
-       int need_sbar_update = term->seen_disp_event;\r
-       if (term->seen_disp_event && term->cfg.scroll_on_disp) {\r
-           term->disptop = 0;         /* return to main screen */\r
-           term->seen_disp_event = 0;\r
-           need_sbar_update = TRUE;\r
-       }\r
-\r
-       if (need_sbar_update)\r
-           update_sbar(term);\r
-       do_paint(term, ctx, TRUE);\r
-       sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);\r
-       free_ctx(ctx);\r
-    }\r
-}\r
-\r
-/*\r
- * Called from front end when a keypress occurs, to trigger\r
- * anything magical that needs to happen in that situation.\r
- */\r
-void term_seen_key_event(Terminal *term)\r
-{\r
-    /*\r
-     * On any keypress, clear the bell overload mechanism\r
-     * completely, on the grounds that large numbers of\r
-     * beeps coming from deliberate key action are likely\r
-     * to be intended (e.g. beeps from filename completion\r
-     * blocking repeatedly).\r
-     */\r
-    term->beep_overloaded = FALSE;\r
-    while (term->beephead) {\r
-       struct beeptime *tmp = term->beephead;\r
-       term->beephead = tmp->next;\r
-       sfree(tmp);\r
-    }\r
-    term->beeptail = NULL;\r
-    term->nbeeps = 0;\r
-\r
-    /*\r
-     * Reset the scrollback on keypress, if we're doing that.\r
-     */\r
-    if (term->cfg.scroll_on_key) {\r
-       term->disptop = 0;             /* return to main screen */\r
-       seen_disp_event(term);\r
-    }\r
-}\r
-\r
-/*\r
- * Same as power_on(), but an external function.\r
- */\r
-void term_pwron(Terminal *term, int clear)\r
-{\r
-    power_on(term, clear);\r
-    if (term->ldisc)                  /* cause ldisc to notice changes */\r
-       ldisc_send(term->ldisc, NULL, 0, 0);\r
-    term->disptop = 0;\r
-    deselect(term);\r
-    term_update(term);\r
-}\r
-\r
-static void set_erase_char(Terminal *term)\r
-{\r
-    term->erase_char = term->basic_erase_char;\r
-    if (term->use_bce)\r
-       term->erase_char.attr = (term->curr_attr &\r
-                                (ATTR_FGMASK | ATTR_BGMASK));\r
-}\r
-\r
-/*\r
- * When the user reconfigures us, we need to check the forbidden-\r
- * alternate-screen config option, disable raw mouse mode if the\r
- * user has disabled mouse reporting, and abandon a print job if\r
- * the user has disabled printing.\r
- */\r
-void term_reconfig(Terminal *term, Config *cfg)\r
-{\r
-    /*\r
-     * Before adopting the new config, check all those terminal\r
-     * settings which control power-on defaults; and if they've\r
-     * changed, we will modify the current state as well as the\r
-     * default one. The full list is: Auto wrap mode, DEC Origin\r
-     * Mode, BCE, blinking text, character classes.\r
-     */\r
-    int reset_wrap, reset_decom, reset_bce, reset_tblink, reset_charclass;\r
-    int i;\r
-\r
-    reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);\r
-    reset_decom = (term->cfg.dec_om != cfg->dec_om);\r
-    reset_bce = (term->cfg.bce != cfg->bce);\r
-    reset_tblink = (term->cfg.blinktext != cfg->blinktext);\r
-    reset_charclass = 0;\r
-    for (i = 0; i < lenof(term->cfg.wordness); i++)\r
-       if (term->cfg.wordness[i] != cfg->wordness[i])\r
-           reset_charclass = 1;\r
-\r
-    /*\r
-     * If the bidi or shaping settings have changed, flush the bidi\r
-     * cache completely.\r
-     */\r
-    if (term->cfg.arabicshaping != cfg->arabicshaping ||\r
-       term->cfg.bidi != cfg->bidi) {\r
-       for (i = 0; i < term->bidi_cache_size; i++) {\r
-           sfree(term->pre_bidi_cache[i].chars);\r
-           sfree(term->post_bidi_cache[i].chars);\r
-           term->pre_bidi_cache[i].width = -1;\r
-           term->pre_bidi_cache[i].chars = NULL;\r
-           term->post_bidi_cache[i].width = -1;\r
-           term->post_bidi_cache[i].chars = NULL;\r
-       }\r
-    }\r
-\r
-    term->cfg = *cfg;                 /* STRUCTURE COPY */\r
-\r
-    if (reset_wrap)\r
-       term->alt_wrap = term->wrap = term->cfg.wrap_mode;\r
-    if (reset_decom)\r
-       term->alt_om = term->dec_om = term->cfg.dec_om;\r
-    if (reset_bce) {\r
-       term->use_bce = term->cfg.bce;\r
-       set_erase_char(term);\r
-    }\r
-    if (reset_tblink) {\r
-       term->blink_is_real = term->cfg.blinktext;\r
-    }\r
-    if (reset_charclass)\r
-       for (i = 0; i < 256; i++)\r
-           term->wordness[i] = term->cfg.wordness[i];\r
-\r
-    if (term->cfg.no_alt_screen)\r
-       swap_screen(term, 0, FALSE, FALSE);\r
-    if (term->cfg.no_mouse_rep) {\r
-       term->xterm_mouse = 0;\r
-       set_raw_mouse_mode(term->frontend, 0);\r
-    }\r
-    if (term->cfg.no_remote_charset) {\r
-       term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;\r
-       term->sco_acs = term->alt_sco_acs = 0;\r
-       term->utf = 0;\r
-    }\r
-    if (!*term->cfg.printer) {\r
-       term_print_finish(term);\r
-    }\r
-    term_schedule_tblink(term);\r
-    term_schedule_cblink(term);\r
-}\r
-\r
-/*\r
- * Clear the scrollback.\r
- */\r
-void term_clrsb(Terminal *term)\r
-{\r
-    unsigned char *line;\r
-    term->disptop = 0;\r
-    while ((line = delpos234(term->scrollback, 0)) != NULL) {\r
-       sfree(line);            /* this is compressed data, not a termline */\r
-    }\r
-    term->tempsblines = 0;\r
-    term->alt_sblines = 0;\r
-    update_sbar(term);\r
-}\r
-\r
-/*\r
- * Initialise the terminal.\r
- */\r
-Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,\r
-                   void *frontend)\r
-{\r
-    Terminal *term;\r
-\r
-    /*\r
-     * Allocate a new Terminal structure and initialise the fields\r
-     * that need it.\r
-     */\r
-    term = snew(Terminal);\r
-    term->frontend = frontend;\r
-    term->ucsdata = ucsdata;\r
-    term->cfg = *mycfg;                       /* STRUCTURE COPY */\r
-    term->logctx = NULL;\r
-    term->compatibility_level = TM_PUTTY;\r
-    strcpy(term->id_string, "\033[?6c");\r
-    term->cblink_pending = term->tblink_pending = FALSE;\r
-    term->paste_buffer = NULL;\r
-    term->paste_len = 0;\r
-    term->last_paste = 0;\r
-    bufchain_init(&term->inbuf);\r
-    bufchain_init(&term->printer_buf);\r
-    term->printing = term->only_printing = FALSE;\r
-    term->print_job = NULL;\r
-    term->vt52_mode = FALSE;\r
-    term->cr_lf_return = FALSE;\r
-    term->seen_disp_event = FALSE;\r
-    term->mouse_is_down = FALSE;\r
-    term->reset_132 = FALSE;\r
-    term->cblinker = term->tblinker = 0;\r
-    term->has_focus = 1;\r
-    term->repeat_off = FALSE;\r
-    term->termstate = TOPLEVEL;\r
-    term->selstate = NO_SELECTION;\r
-    term->curstype = 0;\r
-\r
-    term->screen = term->alt_screen = term->scrollback = NULL;\r
-    term->tempsblines = 0;\r
-    term->alt_sblines = 0;\r
-    term->disptop = 0;\r
-    term->disptext = NULL;\r
-    term->dispcursx = term->dispcursy = -1;\r
-    term->tabs = NULL;\r
-    deselect(term);\r
-    term->rows = term->cols = -1;\r
-    power_on(term, TRUE);\r
-    term->beephead = term->beeptail = NULL;\r
-#ifdef OPTIMISE_SCROLL\r
-    term->scrollhead = term->scrolltail = NULL;\r
-#endif /* OPTIMISE_SCROLL */\r
-    term->nbeeps = 0;\r
-    term->lastbeep = FALSE;\r
-    term->beep_overloaded = FALSE;\r
-    term->attr_mask = 0xffffffff;\r
-    term->resize_fn = NULL;\r
-    term->resize_ctx = NULL;\r
-    term->in_term_out = FALSE;\r
-    term->ltemp = NULL;\r
-    term->ltemp_size = 0;\r
-    term->wcFrom = NULL;\r
-    term->wcTo = NULL;\r
-    term->wcFromTo_size = 0;\r
-\r
-    term->window_update_pending = FALSE;\r
-\r
-    term->bidi_cache_size = 0;\r
-    term->pre_bidi_cache = term->post_bidi_cache = NULL;\r
-\r
-    /* FULL-TERMCHAR */\r
-    term->basic_erase_char.chr = CSET_ASCII | ' ';\r
-    term->basic_erase_char.attr = ATTR_DEFAULT;\r
-    term->basic_erase_char.cc_next = 0;\r
-    term->erase_char = term->basic_erase_char;\r
-\r
-    return term;\r
-}\r
-\r
-void term_free(Terminal *term)\r
-{\r
-    termline *line;\r
-    struct beeptime *beep;\r
-    int i;\r
-\r
-    while ((line = delpos234(term->scrollback, 0)) != NULL)\r
-       sfree(line);                   /* compressed data, not a termline */\r
-    freetree234(term->scrollback);\r
-    while ((line = delpos234(term->screen, 0)) != NULL)\r
-       freeline(line);\r
-    freetree234(term->screen);\r
-    while ((line = delpos234(term->alt_screen, 0)) != NULL)\r
-       freeline(line);\r
-    freetree234(term->alt_screen);\r
-    if (term->disptext) {\r
-       for (i = 0; i < term->rows; i++)\r
-           freeline(term->disptext[i]);\r
-    }\r
-    sfree(term->disptext);\r
-    while (term->beephead) {\r
-       beep = term->beephead;\r
-       term->beephead = beep->next;\r
-       sfree(beep);\r
-    }\r
-    bufchain_clear(&term->inbuf);\r
-    if(term->print_job)\r
-       printer_finish_job(term->print_job);\r
-    bufchain_clear(&term->printer_buf);\r
-    sfree(term->paste_buffer);\r
-    sfree(term->ltemp);\r
-    sfree(term->wcFrom);\r
-    sfree(term->wcTo);\r
-\r
-    for (i = 0; i < term->bidi_cache_size; i++) {\r
-       sfree(term->pre_bidi_cache[i].chars);\r
-       sfree(term->post_bidi_cache[i].chars);\r
-    }\r
-    sfree(term->pre_bidi_cache);\r
-    sfree(term->post_bidi_cache);\r
-\r
-    expire_timer_context(term);\r
-\r
-    sfree(term);\r
-}\r
-\r
-/*\r
- * Set up the terminal for a given size.\r
- */\r
-void term_size(Terminal *term, int newrows, int newcols, int newsavelines)\r
-{\r
-    tree234 *newalt;\r
-    termline **newdisp, *line;\r
-    int i, j, oldrows = term->rows;\r
-    int sblen;\r
-    int save_alt_which = term->alt_which;\r
-\r
-    if (newrows == term->rows && newcols == term->cols &&\r
-       newsavelines == term->savelines)\r
-       return;                        /* nothing to do */\r
-\r
-    /* Behave sensibly if we're given zero (or negative) rows/cols */\r
-\r
-    if (newrows < 1) newrows = 1;\r
-    if (newcols < 1) newcols = 1;\r
-\r
-    deselect(term);\r
-    swap_screen(term, 0, FALSE, FALSE);\r
-\r
-    term->alt_t = term->marg_t = 0;\r
-    term->alt_b = term->marg_b = newrows - 1;\r
-\r
-    if (term->rows == -1) {\r
-       term->scrollback = newtree234(NULL);\r
-       term->screen = newtree234(NULL);\r
-       term->tempsblines = 0;\r
-       term->rows = 0;\r
-    }\r
-\r
-    /*\r
-     * Resize the screen and scrollback. We only need to shift\r
-     * lines around within our data structures, because lineptr()\r
-     * will take care of resizing each individual line if\r
-     * necessary. So:\r
-     * \r
-     *  - If the new screen is longer, we shunt lines in from temporary\r
-     *    scrollback if possible, otherwise we add new blank lines at\r
-     *    the bottom.\r
-     *\r
-     *  - If the new screen is shorter, we remove any blank lines at\r
-     *    the bottom if possible, otherwise shunt lines above the cursor\r
-     *    to scrollback if possible, otherwise delete lines below the\r
-     *    cursor.\r
-     * \r
-     *  - Then, if the new scrollback length is less than the\r
-     *    amount of scrollback we actually have, we must throw some\r
-     *    away.\r
-     */\r
-    sblen = count234(term->scrollback);\r
-    /* Do this loop to expand the screen if newrows > rows */\r
-    assert(term->rows == count234(term->screen));\r
-    while (term->rows < newrows) {\r
-       if (term->tempsblines > 0) {\r
-           unsigned char *cline;\r
-           /* Insert a line from the scrollback at the top of the screen. */\r
-           assert(sblen >= term->tempsblines);\r
-           cline = delpos234(term->scrollback, --sblen);\r
-           line = decompressline(cline, NULL);\r
-           sfree(cline);\r
-           line->temporary = FALSE;   /* reconstituted line is now real */\r
-           term->tempsblines -= 1;\r
-           addpos234(term->screen, line, 0);\r
-           term->curs.y += 1;\r
-           term->savecurs.y += 1;\r
-           term->alt_y += 1;\r
-           term->alt_savecurs.y += 1;\r
-       } else {\r
-           /* Add a new blank line at the bottom of the screen. */\r
-           line = newline(term, newcols, FALSE);\r
-           addpos234(term->screen, line, count234(term->screen));\r
-       }\r
-       term->rows += 1;\r
-    }\r
-    /* Do this loop to shrink the screen if newrows < rows */\r
-    while (term->rows > newrows) {\r
-       if (term->curs.y < term->rows - 1) {\r
-           /* delete bottom row, unless it contains the cursor */\r
-           sfree(delpos234(term->screen, term->rows - 1));\r
-       } else {\r
-           /* push top row to scrollback */\r
-           line = delpos234(term->screen, 0);\r
-           addpos234(term->scrollback, compressline(line), sblen++);\r
-           freeline(line);\r
-           term->tempsblines += 1;\r
-           term->curs.y -= 1;\r
-           term->savecurs.y -= 1;\r
-           term->alt_y -= 1;\r
-           term->alt_savecurs.y -= 1;\r
-       }\r
-       term->rows -= 1;\r
-    }\r
-    assert(term->rows == newrows);\r
-    assert(count234(term->screen) == newrows);\r
-\r
-    /* Delete any excess lines from the scrollback. */\r
-    while (sblen > newsavelines) {\r
-       line = delpos234(term->scrollback, 0);\r
-       sfree(line);\r
-       sblen--;\r
-    }\r
-    if (sblen < term->tempsblines)\r
-       term->tempsblines = sblen;\r
-    assert(count234(term->scrollback) <= newsavelines);\r
-    assert(count234(term->scrollback) >= term->tempsblines);\r
-    term->disptop = 0;\r
-\r
-    /* Make a new displayed text buffer. */\r
-    newdisp = snewn(newrows, termline *);\r
-    for (i = 0; i < newrows; i++) {\r
-       newdisp[i] = newline(term, newcols, FALSE);\r
-       for (j = 0; j < newcols; j++)\r
-           newdisp[i]->chars[j].attr = ATTR_INVALID;\r
-    }\r
-    if (term->disptext) {\r
-       for (i = 0; i < oldrows; i++)\r
-           freeline(term->disptext[i]);\r
-    }\r
-    sfree(term->disptext);\r
-    term->disptext = newdisp;\r
-    term->dispcursx = term->dispcursy = -1;\r
-\r
-    /* Make a new alternate screen. */\r
-    newalt = newtree234(NULL);\r
-    for (i = 0; i < newrows; i++) {\r
-       line = newline(term, newcols, TRUE);\r
-       addpos234(newalt, line, i);\r
-    }\r
-    if (term->alt_screen) {\r
-       while (NULL != (line = delpos234(term->alt_screen, 0)))\r
-           freeline(line);\r
-       freetree234(term->alt_screen);\r
-    }\r
-    term->alt_screen = newalt;\r
-    term->alt_sblines = 0;\r
-\r
-    term->tabs = sresize(term->tabs, newcols, unsigned char);\r
-    {\r
-       int i;\r
-       for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)\r
-           term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);\r
-    }\r
-\r
-    /* Check that the cursor positions are still valid. */\r
-    if (term->savecurs.y < 0)\r
-       term->savecurs.y = 0;\r
-    if (term->savecurs.y >= newrows)\r
-       term->savecurs.y = newrows - 1;\r
-    if (term->savecurs.x >= newcols)\r
-       term->savecurs.x = newcols - 1;\r
-    if (term->alt_savecurs.y < 0)\r
-       term->alt_savecurs.y = 0;\r
-    if (term->alt_savecurs.y >= newrows)\r
-       term->alt_savecurs.y = newrows - 1;\r
-    if (term->alt_savecurs.x >= newcols)\r
-       term->alt_savecurs.x = newcols - 1;\r
-    if (term->curs.y < 0)\r
-       term->curs.y = 0;\r
-    if (term->curs.y >= newrows)\r
-       term->curs.y = newrows - 1;\r
-    if (term->curs.x >= newcols)\r
-       term->curs.x = newcols - 1;\r
-    if (term->alt_y < 0)\r
-       term->alt_y = 0;\r
-    if (term->alt_y >= newrows)\r
-       term->alt_y = newrows - 1;\r
-    if (term->alt_x >= newcols)\r
-       term->alt_x = newcols - 1;\r
-    term->alt_x = term->alt_y = 0;\r
-    term->wrapnext = term->alt_wnext = FALSE;\r
-\r
-    term->rows = newrows;\r
-    term->cols = newcols;\r
-    term->savelines = newsavelines;\r
-\r
-    swap_screen(term, save_alt_which, FALSE, FALSE);\r
-\r
-    update_sbar(term);\r
-    term_update(term);\r
-    if (term->resize_fn)\r
-       term->resize_fn(term->resize_ctx, term->cols, term->rows);\r
-}\r
-\r
-/*\r
- * Hand a function and context pointer to the terminal which it can\r
- * use to notify a back end of resizes.\r
- */\r
-void term_provide_resize_fn(Terminal *term,\r
-                           void (*resize_fn)(void *, int, int),\r
-                           void *resize_ctx)\r
-{\r
-    term->resize_fn = resize_fn;\r
-    term->resize_ctx = resize_ctx;\r
-    if (resize_fn && term->cols > 0 && term->rows > 0)\r
-       resize_fn(resize_ctx, term->cols, term->rows);\r
-}\r
-\r
-/* Find the bottom line on the screen that has any content.\r
- * If only the top line has content, returns 0.\r
- * If no lines have content, return -1.\r
- */ \r
-static int find_last_nonempty_line(Terminal * term, tree234 * screen)\r
-{\r
-    int i;\r
-    for (i = count234(screen) - 1; i >= 0; i--) {\r
-       termline *line = index234(screen, i);\r
-       int j;\r
-       for (j = 0; j < line->cols; j++)\r
-           if (!termchars_equal(&line->chars[j], &term->erase_char))\r
-               break;\r
-       if (j != line->cols) break;\r
-    }\r
-    return i;\r
-}\r
-\r
-/*\r
- * Swap screens. If `reset' is TRUE and we have been asked to\r
- * switch to the alternate screen, we must bring most of its\r
- * configuration from the main screen and erase the contents of the\r
- * alternate screen completely. (This is even true if we're already\r
- * on it! Blame xterm.)\r
- */\r
-static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)\r
-{\r
-    int t;\r
-    pos tp;\r
-    tree234 *ttr;\r
-\r
-    if (!which)\r
-       reset = FALSE;                 /* do no weird resetting if which==0 */\r
-\r
-    if (which != term->alt_which) {\r
-       term->alt_which = which;\r
-\r
-       ttr = term->alt_screen;\r
-       term->alt_screen = term->screen;\r
-       term->screen = ttr;\r
-       term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;\r
-       t = term->curs.x;\r
-       if (!reset && !keep_cur_pos)\r
-           term->curs.x = term->alt_x;\r
-       term->alt_x = t;\r
-       t = term->curs.y;\r
-       if (!reset && !keep_cur_pos)\r
-           term->curs.y = term->alt_y;\r
-       term->alt_y = t;\r
-       t = term->marg_t;\r
-       if (!reset) term->marg_t = term->alt_t;\r
-       term->alt_t = t;\r
-       t = term->marg_b;\r
-       if (!reset) term->marg_b = term->alt_b;\r
-       term->alt_b = t;\r
-       t = term->dec_om;\r
-       if (!reset) term->dec_om = term->alt_om;\r
-       term->alt_om = t;\r
-       t = term->wrap;\r
-       if (!reset) term->wrap = term->alt_wrap;\r
-       term->alt_wrap = t;\r
-       t = term->wrapnext;\r
-       if (!reset) term->wrapnext = term->alt_wnext;\r
-       term->alt_wnext = t;\r
-       t = term->insert;\r
-       if (!reset) term->insert = term->alt_ins;\r
-       term->alt_ins = t;\r
-       t = term->cset;\r
-       if (!reset) term->cset = term->alt_cset;\r
-       term->alt_cset = t;\r
-       t = term->utf;\r
-       if (!reset) term->utf = term->alt_utf;\r
-       term->alt_utf = t;\r
-       t = term->sco_acs;\r
-       if (!reset) term->sco_acs = term->alt_sco_acs;\r
-       term->alt_sco_acs = t;\r
-\r
-       tp = term->savecurs;\r
-       if (!reset && !keep_cur_pos)\r
-           term->savecurs = term->alt_savecurs;\r
-       term->alt_savecurs = tp;\r
-        t = term->save_cset;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_cset = term->alt_save_cset;\r
-        term->alt_save_cset = t;\r
-        t = term->save_csattr;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_csattr = term->alt_save_csattr;\r
-        term->alt_save_csattr = t;\r
-        t = term->save_attr;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_attr = term->alt_save_attr;\r
-        term->alt_save_attr = t;\r
-        t = term->save_utf;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_utf = term->alt_save_utf;\r
-        term->alt_save_utf = t;\r
-        t = term->save_wnext;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_wnext = term->alt_save_wnext;\r
-        term->alt_save_wnext = t;\r
-        t = term->save_sco_acs;\r
-        if (!reset && !keep_cur_pos)\r
-            term->save_sco_acs = term->alt_save_sco_acs;\r
-        term->alt_save_sco_acs = t;\r
-    }\r
-\r
-    if (reset && term->screen) {\r
-       /*\r
-        * Yes, this _is_ supposed to honour background-colour-erase.\r
-        */\r
-       erase_lots(term, FALSE, TRUE, TRUE);\r
-    }\r
-}\r
-\r
-/*\r
- * Update the scroll bar.\r
- */\r
-static void update_sbar(Terminal *term)\r
-{\r
-    int nscroll = sblines(term);\r
-    set_sbar(term->frontend, nscroll + term->rows,\r
-            nscroll + term->disptop, term->rows);\r
-}\r
-\r
-/*\r
- * Check whether the region bounded by the two pointers intersects\r
- * the scroll region, and de-select the on-screen selection if so.\r
- */\r
-static void check_selection(Terminal *term, pos from, pos to)\r
-{\r
-    if (poslt(from, term->selend) && poslt(term->selstart, to))\r
-       deselect(term);\r
-}\r
-\r
-/*\r
- * Scroll the screen. (`lines' is +ve for scrolling forward, -ve\r
- * for backward.) `sb' is TRUE if the scrolling is permitted to\r
- * affect the scrollback buffer.\r
- */\r
-static void scroll(Terminal *term, int topline, int botline, int lines, int sb)\r
-{\r
-    termline *line;\r
-    int i, seltop;\r
-#ifdef OPTIMISE_SCROLL\r
-    int olddisptop, shift;\r
-#endif /* OPTIMISE_SCROLL */\r
-\r
-    if (topline != 0 || term->alt_which != 0)\r
-       sb = FALSE;\r
-\r
-#ifdef OPTIMISE_SCROLL\r
-    olddisptop = term->disptop;\r
-    shift = lines;\r
-#endif /* OPTIMISE_SCROLL */\r
-    if (lines < 0) {\r
-       while (lines < 0) {\r
-           line = delpos234(term->screen, botline);\r
-            resizeline(term, line, term->cols);\r
-           for (i = 0; i < term->cols; i++)\r
-               copy_termchar(line, i, &term->erase_char);\r
-           line->lattr = LATTR_NORM;\r
-           addpos234(term->screen, line, topline);\r
-\r
-           if (term->selstart.y >= topline && term->selstart.y <= botline) {\r
-               term->selstart.y++;\r
-               if (term->selstart.y > botline) {\r
-                   term->selstart.y = botline + 1;\r
-                   term->selstart.x = 0;\r
-               }\r
-           }\r
-           if (term->selend.y >= topline && term->selend.y <= botline) {\r
-               term->selend.y++;\r
-               if (term->selend.y > botline) {\r
-                   term->selend.y = botline + 1;\r
-                   term->selend.x = 0;\r
-               }\r
-           }\r
-\r
-           lines++;\r
-       }\r
-    } else {\r
-       while (lines > 0) {\r
-           line = delpos234(term->screen, topline);\r
-#ifdef TERM_CC_DIAGS\r
-           cc_check(line);\r
-#endif\r
-           if (sb && term->savelines > 0) {\r
-               int sblen = count234(term->scrollback);\r
-               /*\r
-                * We must add this line to the scrollback. We'll\r
-                * remove a line from the top of the scrollback if\r
-                * the scrollback is full.\r
-                */\r
-               if (sblen == term->savelines) {\r
-                   unsigned char *cline;\r
-\r
-                   sblen--;\r
-                   cline = delpos234(term->scrollback, 0);\r
-                   sfree(cline);\r
-               } else\r
-                   term->tempsblines += 1;\r
-\r
-               addpos234(term->scrollback, compressline(line), sblen);\r
-\r
-               /* now `line' itself can be reused as the bottom line */\r
-\r
-               /*\r
-                * If the user is currently looking at part of the\r
-                * scrollback, and they haven't enabled any options\r
-                * that are going to reset the scrollback as a\r
-                * result of this movement, then the chances are\r
-                * they'd like to keep looking at the same line. So\r
-                * we move their viewpoint at the same rate as the\r
-                * scroll, at least until their viewpoint hits the\r
-                * top end of the scrollback buffer, at which point\r
-                * we don't have the choice any more.\r
-                * \r
-                * Thanks to Jan Holmen Holsten for the idea and\r
-                * initial implementation.\r
-                */\r
-               if (term->disptop > -term->savelines && term->disptop < 0)\r
-                   term->disptop--;\r
-           }\r
-            resizeline(term, line, term->cols);\r
-           for (i = 0; i < term->cols; i++)\r
-               copy_termchar(line, i, &term->erase_char);\r
-           line->lattr = LATTR_NORM;\r
-           addpos234(term->screen, line, botline);\r
-\r
-           /*\r
-            * If the selection endpoints move into the scrollback,\r
-            * we keep them moving until they hit the top. However,\r
-            * of course, if the line _hasn't_ moved into the\r
-            * scrollback then we don't do this, and cut them off\r
-            * at the top of the scroll region.\r
-            * \r
-            * This applies to selstart and selend (for an existing\r
-            * selection), and also selanchor (for one being\r
-            * selected as we speak).\r
-            */\r
-           seltop = sb ? -term->savelines : topline;\r
-\r
-           if (term->selstate != NO_SELECTION) {\r
-               if (term->selstart.y >= seltop &&\r
-                   term->selstart.y <= botline) {\r
-                   term->selstart.y--;\r
-                   if (term->selstart.y < seltop) {\r
-                       term->selstart.y = seltop;\r
-                       term->selstart.x = 0;\r
-                   }\r
-               }\r
-               if (term->selend.y >= seltop && term->selend.y <= botline) {\r
-                   term->selend.y--;\r
-                   if (term->selend.y < seltop) {\r
-                       term->selend.y = seltop;\r
-                       term->selend.x = 0;\r
-                   }\r
-               }\r
-               if (term->selanchor.y >= seltop &&\r
-                   term->selanchor.y <= botline) {\r
-                   term->selanchor.y--;\r
-                   if (term->selanchor.y < seltop) {\r
-                       term->selanchor.y = seltop;\r
-                       term->selanchor.x = 0;\r
-                   }\r
-               }\r
-           }\r
-\r
-           lines--;\r
-       }\r
-    }\r
-#ifdef OPTIMISE_SCROLL\r
-    shift += term->disptop - olddisptop;\r
-    if (shift < term->rows && shift > -term->rows && shift != 0)\r
-       scroll_display(term, topline, botline, shift);\r
-#endif /* OPTIMISE_SCROLL */\r
-}\r
-\r
-#ifdef OPTIMISE_SCROLL\r
-/*\r
- * Add a scroll of a region on the screen into the pending scroll list.\r
- * `lines' is +ve for scrolling forward, -ve for backward.\r
- *\r
- * If the scroll is on the same area as the last scroll in the list,\r
- * merge them.\r
- */\r
-static void save_scroll(Terminal *term, int topline, int botline, int lines)\r
-{\r
-    struct scrollregion *newscroll;\r
-    if (term->scrolltail &&\r
-       term->scrolltail->topline == topline && \r
-       term->scrolltail->botline == botline) {\r
-       term->scrolltail->lines += lines;\r
-    } else {\r
-       newscroll = snew(struct scrollregion);\r
-       newscroll->topline = topline;\r
-       newscroll->botline = botline;\r
-       newscroll->lines = lines;\r
-       newscroll->next = NULL;\r
-\r
-       if (!term->scrollhead)\r
-           term->scrollhead = newscroll;\r
-       else\r
-           term->scrolltail->next = newscroll;\r
-       term->scrolltail = newscroll;\r
-    }\r
-}\r
-\r
-/*\r
- * Scroll the physical display, and our conception of it in disptext.\r
- */\r
-static void scroll_display(Terminal *term, int topline, int botline, int lines)\r
-{\r
-    int distance, nlines, i, j;\r
-\r
-    distance = lines > 0 ? lines : -lines;\r
-    nlines = botline - topline + 1 - distance;\r
-    if (lines > 0) {\r
-       for (i = 0; i < nlines; i++)\r
-           for (j = 0; j < term->cols; j++)\r
-               copy_termchar(term->disptext[i], j,\r
-                             term->disptext[i+distance]->chars+j);\r
-       if (term->dispcursy >= 0 &&\r
-           term->dispcursy >= topline + distance &&\r
-           term->dispcursy < topline + distance + nlines)\r
-           term->dispcursy -= distance;\r
-       for (i = 0; i < distance; i++)\r
-           for (j = 0; j < term->cols; j++)\r
-               term->disptext[nlines+i]->chars[j].attr |= ATTR_INVALID;\r
-    } else {\r
-       for (i = nlines; i-- ;)\r
-           for (j = 0; j < term->cols; j++)\r
-               copy_termchar(term->disptext[i+distance], j,\r
-                             term->disptext[i]->chars+j);\r
-       if (term->dispcursy >= 0 &&\r
-           term->dispcursy >= topline &&\r
-           term->dispcursy < topline + nlines)\r
-           term->dispcursy += distance;\r
-       for (i = 0; i < distance; i++)\r
-           for (j = 0; j < term->cols; j++)\r
-               term->disptext[i]->chars[j].attr |= ATTR_INVALID;\r
-    }\r
-    save_scroll(term, topline, botline, lines);\r
-}\r
-#endif /* OPTIMISE_SCROLL */\r
-\r
-/*\r
- * Move the cursor to a given position, clipping at boundaries. We\r
- * may or may not want to clip at the scroll margin: marg_clip is 0\r
- * not to, 1 to disallow _passing_ the margins, and 2 to disallow\r
- * even _being_ outside the margins.\r
- */\r
-static void move(Terminal *term, int x, int y, int marg_clip)\r
-{\r
-    if (x < 0)\r
-       x = 0;\r
-    if (x >= term->cols)\r
-       x = term->cols - 1;\r
-    if (marg_clip) {\r
-       if ((term->curs.y >= term->marg_t || marg_clip == 2) &&\r
-           y < term->marg_t)\r
-           y = term->marg_t;\r
-       if ((term->curs.y <= term->marg_b || marg_clip == 2) &&\r
-           y > term->marg_b)\r
-           y = term->marg_b;\r
-    }\r
-    if (y < 0)\r
-       y = 0;\r
-    if (y >= term->rows)\r
-       y = term->rows - 1;\r
-    term->curs.x = x;\r
-    term->curs.y = y;\r
-    term->wrapnext = FALSE;\r
-}\r
-\r
-/*\r
- * Save or restore the cursor and SGR mode.\r
- */\r
-static void save_cursor(Terminal *term, int save)\r
-{\r
-    if (save) {\r
-       term->savecurs = term->curs;\r
-       term->save_attr = term->curr_attr;\r
-       term->save_cset = term->cset;\r
-       term->save_utf = term->utf;\r
-       term->save_wnext = term->wrapnext;\r
-       term->save_csattr = term->cset_attr[term->cset];\r
-       term->save_sco_acs = term->sco_acs;\r
-    } else {\r
-       term->curs = term->savecurs;\r
-       /* Make sure the window hasn't shrunk since the save */\r
-       if (term->curs.x >= term->cols)\r
-           term->curs.x = term->cols - 1;\r
-       if (term->curs.y >= term->rows)\r
-           term->curs.y = term->rows - 1;\r
-\r
-       term->curr_attr = term->save_attr;\r
-       term->cset = term->save_cset;\r
-       term->utf = term->save_utf;\r
-       term->wrapnext = term->save_wnext;\r
-       /*\r
-        * wrapnext might reset to False if the x position is no\r
-        * longer at the rightmost edge.\r
-        */\r
-       if (term->wrapnext && term->curs.x < term->cols-1)\r
-           term->wrapnext = FALSE;\r
-       term->cset_attr[term->cset] = term->save_csattr;\r
-       term->sco_acs = term->save_sco_acs;\r
-       set_erase_char(term);\r
-    }\r
-}\r
-\r
-/*\r
- * This function is called before doing _anything_ which affects\r
- * only part of a line of text. It is used to mark the boundary\r
- * between two character positions, and it indicates that some sort\r
- * of effect is going to happen on only one side of that boundary.\r
- * \r
- * The effect of this function is to check whether a CJK\r
- * double-width character is straddling the boundary, and to remove\r
- * it and replace it with two spaces if so. (Of course, one or\r
- * other of those spaces is then likely to be replaced with\r
- * something else again, as a result of whatever happens next.)\r
- * \r
- * Also, if the boundary is at the right-hand _edge_ of the screen,\r
- * it implies something deliberate is being done to the rightmost\r
- * column position; hence we must clear LATTR_WRAPPED2.\r
- * \r
- * The input to the function is the coordinates of the _second_\r
- * character of the pair.\r
- */\r
-static void check_boundary(Terminal *term, int x, int y)\r
-{\r
-    termline *ldata;\r
-\r
-    /* Validate input coordinates, just in case. */\r
-    if (x == 0 || x > term->cols)\r
-       return;\r
-\r
-    ldata = scrlineptr(y);\r
-    if (x == term->cols) {\r
-       ldata->lattr &= ~LATTR_WRAPPED2;\r
-    } else {\r
-       if (ldata->chars[x].chr == UCSWIDE) {\r
-           clear_cc(ldata, x-1);\r
-           clear_cc(ldata, x);\r
-           ldata->chars[x-1].chr = ' ' | CSET_ASCII;\r
-           ldata->chars[x] = ldata->chars[x-1];\r
-       }\r
-    }\r
-}\r
-\r
-/*\r
- * Erase a large portion of the screen: the whole screen, or the\r
- * whole line, or parts thereof.\r
- */\r
-static void erase_lots(Terminal *term,\r
-                      int line_only, int from_begin, int to_end)\r
-{\r
-    pos start, end;\r
-    int erase_lattr;\r
-    int erasing_lines_from_top = 0;\r
-\r
-    if (line_only) {\r
-       start.y = term->curs.y;\r
-       start.x = 0;\r
-       end.y = term->curs.y + 1;\r
-       end.x = 0;\r
-       erase_lattr = FALSE;\r
-    } else {\r
-       start.y = 0;\r
-       start.x = 0;\r
-       end.y = term->rows;\r
-       end.x = 0;\r
-       erase_lattr = TRUE;\r
-    }\r
-    if (!from_begin) {\r
-       start = term->curs;\r
-    }\r
-    if (!to_end) {\r
-       end = term->curs;\r
-       incpos(end);\r
-    }\r
-    if (!from_begin || !to_end)\r
-       check_boundary(term, term->curs.x, term->curs.y);\r
-    check_selection(term, start, end);\r
-\r
-    /* Clear screen also forces a full window redraw, just in case. */\r
-    if (start.y == 0 && start.x == 0 && end.y == term->rows)\r
-       term_invalidate(term);\r
-\r
-    /* Lines scrolled away shouldn't be brought back on if the terminal\r
-     * resizes. */\r
-    if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)\r
-       erasing_lines_from_top = 1;\r
-\r
-    if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {\r
-       /* If it's a whole number of lines, starting at the top, and\r
-        * we're fully erasing them, erase by scrolling and keep the\r
-        * lines in the scrollback. */\r
-       int scrolllines = end.y;\r
-       if (end.y == term->rows) {\r
-           /* Shrink until we find a non-empty row.*/\r
-           scrolllines = find_last_nonempty_line(term, term->screen) + 1;\r
-       }\r
-       if (scrolllines > 0)\r
-           scroll(term, 0, scrolllines - 1, scrolllines, TRUE);\r
-    } else {\r
-       termline *ldata = scrlineptr(start.y);\r
-       while (poslt(start, end)) {\r
-           if (start.x == term->cols) {\r
-               if (!erase_lattr)\r
-                   ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);\r
-               else\r
-                   ldata->lattr = LATTR_NORM;\r
-           } else {\r
-               copy_termchar(ldata, start.x, &term->erase_char);\r
-           }\r
-           if (incpos(start) && start.y < term->rows) {\r
-               ldata = scrlineptr(start.y);\r
-           }\r
-       }\r
-    }\r
-\r
-    /* After an erase of lines from the top of the screen, we shouldn't\r
-     * bring the lines back again if the terminal enlarges (since the user or\r
-     * application has explictly thrown them away). */\r
-    if (erasing_lines_from_top && !(term->alt_which))\r
-       term->tempsblines = 0;\r
-}\r
-\r
-/*\r
- * Insert or delete characters within the current line. n is +ve if\r
- * insertion is desired, and -ve for deletion.\r
- */\r
-static void insch(Terminal *term, int n)\r
-{\r
-    int dir = (n < 0 ? -1 : +1);\r
-    int m, j;\r
-    pos cursplus;\r
-    termline *ldata;\r
-\r
-    n = (n < 0 ? -n : n);\r
-    if (n > term->cols - term->curs.x)\r
-       n = term->cols - term->curs.x;\r
-    m = term->cols - term->curs.x - n;\r
-    cursplus.y = term->curs.y;\r
-    cursplus.x = term->curs.x + n;\r
-    check_selection(term, term->curs, cursplus);\r
-    check_boundary(term, term->curs.x, term->curs.y);\r
-    if (dir < 0)\r
-       check_boundary(term, term->curs.x + n, term->curs.y);\r
-    ldata = scrlineptr(term->curs.y);\r
-    if (dir < 0) {\r
-       for (j = 0; j < m; j++)\r
-           move_termchar(ldata,\r
-                         ldata->chars + term->curs.x + j,\r
-                         ldata->chars + term->curs.x + j + n);\r
-       while (n--)\r
-           copy_termchar(ldata, term->curs.x + m++, &term->erase_char);\r
-    } else {\r
-       for (j = m; j-- ;)\r
-           move_termchar(ldata,\r
-                         ldata->chars + term->curs.x + j + n,\r
-                         ldata->chars + term->curs.x + j);\r
-       while (n--)\r
-           copy_termchar(ldata, term->curs.x + n, &term->erase_char);\r
-    }\r
-}\r
-\r
-/*\r
- * Toggle terminal mode `mode' to state `state'. (`query' indicates\r
- * whether the mode is a DEC private one or a normal one.)\r
- */\r
-static void toggle_mode(Terminal *term, int mode, int query, int state)\r
-{\r
-    if (query)\r
-       switch (mode) {\r
-         case 1:                      /* DECCKM: application cursor keys */\r
-           term->app_cursor_keys = state;\r
-           break;\r
-         case 2:                      /* DECANM: VT52 mode */\r
-           term->vt52_mode = !state;\r
-           if (term->vt52_mode) {\r
-               term->blink_is_real = FALSE;\r
-               term->vt52_bold = FALSE;\r
-           } else {\r
-               term->blink_is_real = term->cfg.blinktext;\r
-           }\r
-           term_schedule_tblink(term);\r
-           break;\r
-         case 3:                      /* DECCOLM: 80/132 columns */\r
-           deselect(term);\r
-           if (!term->cfg.no_remote_resize)\r
-               request_resize(term->frontend, state ? 132 : 80, term->rows);\r
-           term->reset_132 = state;\r
-           term->alt_t = term->marg_t = 0;\r
-           term->alt_b = term->marg_b = term->rows - 1;\r
-           move(term, 0, 0, 0);\r
-           erase_lots(term, FALSE, TRUE, TRUE);\r
-           break;\r
-         case 5:                      /* DECSCNM: reverse video */\r
-           /*\r
-            * Toggle reverse video. If we receive an OFF within the\r
-            * visual bell timeout period after an ON, we trigger an\r
-            * effective visual bell, so that ESC[?5hESC[?5l will\r
-            * always be an actually _visible_ visual bell.\r
-            */\r
-           if (term->rvideo && !state) {\r
-               /* This is an OFF, so set up a vbell */\r
-               term_schedule_vbell(term, TRUE, term->rvbell_startpoint);\r
-           } else if (!term->rvideo && state) {\r
-               /* This is an ON, so we notice the time and save it. */\r
-               term->rvbell_startpoint = GETTICKCOUNT();\r
-           }\r
-           term->rvideo = state;\r
-           seen_disp_event(term);\r
-           break;\r
-         case 6:                      /* DECOM: DEC origin mode */\r
-           term->dec_om = state;\r
-           break;\r
-         case 7:                      /* DECAWM: auto wrap */\r
-           term->wrap = state;\r
-           break;\r
-         case 8:                      /* DECARM: auto key repeat */\r
-           term->repeat_off = !state;\r
-           break;\r
-         case 10:                     /* DECEDM: set local edit mode */\r
-           term->term_editing = state;\r
-           if (term->ldisc)           /* cause ldisc to notice changes */\r
-               ldisc_send(term->ldisc, NULL, 0, 0);\r
-           break;\r
-         case 25:                     /* DECTCEM: enable/disable cursor */\r
-           compatibility2(OTHER, VT220);\r
-           term->cursor_on = state;\r
-           seen_disp_event(term);\r
-           break;\r
-         case 47:                     /* alternate screen */\r
-           compatibility(OTHER);\r
-           deselect(term);\r
-           swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);\r
-           term->disptop = 0;\r
-           break;\r
-         case 1000:                   /* xterm mouse 1 (normal) */\r
-           term->xterm_mouse = state ? 1 : 0;\r
-           set_raw_mouse_mode(term->frontend, state);\r
-           break;\r
-         case 1002:                   /* xterm mouse 2 (inc. button drags) */\r
-           term->xterm_mouse = state ? 2 : 0;\r
-           set_raw_mouse_mode(term->frontend, state);\r
-           break;\r
-         case 1047:                   /* alternate screen */\r
-           compatibility(OTHER);\r
-           deselect(term);\r
-           swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);\r
-           term->disptop = 0;\r
-           break;\r
-         case 1048:                   /* save/restore cursor */\r
-           if (!term->cfg.no_alt_screen)\r
-                save_cursor(term, state);\r
-           if (!state) seen_disp_event(term);\r
-           break;\r
-         case 1049:                   /* cursor & alternate screen */\r
-           if (state && !term->cfg.no_alt_screen)\r
-               save_cursor(term, state);\r
-           if (!state) seen_disp_event(term);\r
-           compatibility(OTHER);\r
-           deselect(term);\r
-           swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);\r
-           if (!state && !term->cfg.no_alt_screen)\r
-               save_cursor(term, state);\r
-           term->disptop = 0;\r
-           break;\r
-    } else\r
-       switch (mode) {\r
-         case 4:                      /* IRM: set insert mode */\r
-           compatibility(VT102);\r
-           term->insert = state;\r
-           break;\r
-         case 12:                     /* SRM: set echo mode */\r
-           term->term_echoing = !state;\r
-           if (term->ldisc)           /* cause ldisc to notice changes */\r
-               ldisc_send(term->ldisc, NULL, 0, 0);\r
-           break;\r
-         case 20:                     /* LNM: Return sends ... */\r
-           term->cr_lf_return = state;\r
-           break;\r
-         case 34:                     /* WYULCURM: Make cursor BIG */\r
-           compatibility2(OTHER, VT220);\r
-           term->big_cursor = !state;\r
-       }\r
-}\r
-\r
-/*\r
- * Process an OSC sequence: set window title or icon name.\r
- */\r
-static void do_osc(Terminal *term)\r
-{\r
-    if (term->osc_w) {\r
-       while (term->osc_strlen--)\r
-           term->wordness[(unsigned char)\r
-               term->osc_string[term->osc_strlen]] = term->esc_args[0];\r
-    } else {\r
-       term->osc_string[term->osc_strlen] = '\0';\r
-       switch (term->esc_args[0]) {\r
-         case 0:\r
-         case 1:\r
-           if (!term->cfg.no_remote_wintitle)\r
-               set_icon(term->frontend, term->osc_string);\r
-           if (term->esc_args[0] == 1)\r
-               break;\r
-           /* fall through: parameter 0 means set both */\r
-         case 2:\r
-         case 21:\r
-           if (!term->cfg.no_remote_wintitle)\r
-               set_title(term->frontend, term->osc_string);\r
-           break;\r
-       }\r
-    }\r
-}\r
-\r
-/*\r
- * ANSI printing routines.\r
- */\r
-static void term_print_setup(Terminal *term)\r
-{\r
-    bufchain_clear(&term->printer_buf);\r
-    term->print_job = printer_start_job(term->cfg.printer);\r
-}\r
-static void term_print_flush(Terminal *term)\r
-{\r
-    void *data;\r
-    int len;\r
-    int size;\r
-    while ((size = bufchain_size(&term->printer_buf)) > 5) {\r
-       bufchain_prefix(&term->printer_buf, &data, &len);\r
-       if (len > size-5)\r
-           len = size-5;\r
-       printer_job_data(term->print_job, data, len);\r
-       bufchain_consume(&term->printer_buf, len);\r
-    }\r
-}\r
-static void term_print_finish(Terminal *term)\r
-{\r
-    void *data;\r
-    int len, size;\r
-    char c;\r
-\r
-    if (!term->printing && !term->only_printing)\r
-       return;                        /* we need do nothing */\r
-\r
-    term_print_flush(term);\r
-    while ((size = bufchain_size(&term->printer_buf)) > 0) {\r
-       bufchain_prefix(&term->printer_buf, &data, &len);\r
-       c = *(char *)data;\r
-       if (c == '\033' || c == '\233') {\r
-           bufchain_consume(&term->printer_buf, size);\r
-           break;\r
-       } else {\r
-           printer_job_data(term->print_job, &c, 1);\r
-           bufchain_consume(&term->printer_buf, 1);\r
-       }\r
-    }\r
-    printer_finish_job(term->print_job);\r
-    term->print_job = NULL;\r
-    term->printing = term->only_printing = FALSE;\r
-}\r
-\r
-/*\r
- * Remove everything currently in `inbuf' and stick it up on the\r
- * in-memory display. There's a big state machine in here to\r
- * process escape sequences...\r
- */\r
-static void term_out(Terminal *term)\r
-{\r
-    unsigned long c;\r
-    int unget;\r
-    unsigned char localbuf[256], *chars;\r
-    int nchars = 0;\r
-\r
-    unget = -1;\r
-\r
-    chars = NULL;                     /* placate compiler warnings */\r
-    while (nchars > 0 || unget != -1 || bufchain_size(&term->inbuf) > 0) {\r
-       if (unget == -1) {\r
-           if (nchars == 0) {\r
-               void *ret;\r
-               bufchain_prefix(&term->inbuf, &ret, &nchars);\r
-               if (nchars > sizeof(localbuf))\r
-                   nchars = sizeof(localbuf);\r
-               memcpy(localbuf, ret, nchars);\r
-               bufchain_consume(&term->inbuf, nchars);\r
-               chars = localbuf;\r
-               assert(chars != NULL);\r
-           }\r
-           c = *chars++;\r
-           nchars--;\r
-\r
-           /*\r
-            * Optionally log the session traffic to a file. Useful for\r
-            * debugging and possibly also useful for actual logging.\r
-            */\r
-           if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)\r
-               logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);\r
-       } else {\r
-           c = unget;\r
-           unget = -1;\r
-       }\r
-\r
-       /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even\r
-        * be able to display 8-bit characters, but I'll let that go 'cause\r
-        * of i18n.\r
-        */\r
-\r
-       /*\r
-        * If we're printing, add the character to the printer\r
-        * buffer.\r
-        */\r
-       if (term->printing) {\r
-           bufchain_add(&term->printer_buf, &c, 1);\r
-\r
-           /*\r
-            * If we're in print-only mode, we use a much simpler\r
-            * state machine designed only to recognise the ESC[4i\r
-            * termination sequence.\r
-            */\r
-           if (term->only_printing) {\r
-               if (c == '\033')\r
-                   term->print_state = 1;\r
-               else if (c == (unsigned char)'\233')\r
-                   term->print_state = 2;\r
-               else if (c == '[' && term->print_state == 1)\r
-                   term->print_state = 2;\r
-               else if (c == '4' && term->print_state == 2)\r
-                   term->print_state = 3;\r
-               else if (c == 'i' && term->print_state == 3)\r
-                   term->print_state = 4;\r
-               else\r
-                   term->print_state = 0;\r
-               if (term->print_state == 4) {\r
-                   term_print_finish(term);\r
-               }\r
-               continue;\r
-           }\r
-       }\r
-\r
-       /* First see about all those translations. */\r
-       if (term->termstate == TOPLEVEL) {\r
-           if (in_utf(term))\r
-               switch (term->utf_state) {\r
-                 case 0:\r
-                   if (c < 0x80) {\r
-                       /* UTF-8 must be stateless so we ignore iso2022. */\r
-                       if (term->ucsdata->unitab_ctrl[c] != 0xFF) \r
-                            c = term->ucsdata->unitab_ctrl[c];\r
-                       else c = ((unsigned char)c) | CSET_ASCII;\r
-                       break;\r
-                   } else if ((c & 0xe0) == 0xc0) {\r
-                       term->utf_size = term->utf_state = 1;\r
-                       term->utf_char = (c & 0x1f);\r
-                   } else if ((c & 0xf0) == 0xe0) {\r
-                       term->utf_size = term->utf_state = 2;\r
-                       term->utf_char = (c & 0x0f);\r
-                   } else if ((c & 0xf8) == 0xf0) {\r
-                       term->utf_size = term->utf_state = 3;\r
-                       term->utf_char = (c & 0x07);\r
-                   } else if ((c & 0xfc) == 0xf8) {\r
-                       term->utf_size = term->utf_state = 4;\r
-                       term->utf_char = (c & 0x03);\r
-                   } else if ((c & 0xfe) == 0xfc) {\r
-                       term->utf_size = term->utf_state = 5;\r
-                       term->utf_char = (c & 0x01);\r
-                   } else {\r
-                       c = UCSERR;\r
-                       break;\r
-                   }\r
-                   continue;\r
-                 case 1:\r
-                 case 2:\r
-                 case 3:\r
-                 case 4:\r
-                 case 5:\r
-                   if ((c & 0xC0) != 0x80) {\r
-                       unget = c;\r
-                       c = UCSERR;\r
-                       term->utf_state = 0;\r
-                       break;\r
-                   }\r
-                   term->utf_char = (term->utf_char << 6) | (c & 0x3f);\r
-                   if (--term->utf_state)\r
-                       continue;\r
-\r
-                   c = term->utf_char;\r
-\r
-                   /* Is somebody trying to be evil! */\r
-                   if (c < 0x80 ||\r
-                       (c < 0x800 && term->utf_size >= 2) ||\r
-                       (c < 0x10000 && term->utf_size >= 3) ||\r
-                       (c < 0x200000 && term->utf_size >= 4) ||\r
-                       (c < 0x4000000 && term->utf_size >= 5))\r
-                       c = UCSERR;\r
-\r
-                   /* Unicode line separator and paragraph separator are CR-LF */\r
-                   if (c == 0x2028 || c == 0x2029)\r
-                       c = 0x85;\r
-\r
-                   /* High controls are probably a Baaad idea too. */\r
-                   if (c < 0xA0)\r
-                       c = 0xFFFD;\r
-\r
-                   /* The UTF-16 surrogates are not nice either. */\r
-                   /*       The standard give the option of decoding these: \r
-                    *       I don't want to! */\r
-                   if (c >= 0xD800 && c < 0xE000)\r
-                       c = UCSERR;\r
-\r
-                   /* ISO 10646 characters now limited to UTF-16 range. */\r
-                   if (c > 0x10FFFF)\r
-                       c = UCSERR;\r
-\r
-                   /* This is currently a TagPhobic application.. */\r
-                   if (c >= 0xE0000 && c <= 0xE007F)\r
-                       continue;\r
-\r
-                   /* U+FEFF is best seen as a null. */\r
-                   if (c == 0xFEFF)\r
-                       continue;\r
-                   /* But U+FFFE is an error. */\r
-                   if (c == 0xFFFE || c == 0xFFFF)\r
-                       c = UCSERR;\r
-\r
-                   break;\r
-           }\r
-           /* Are we in the nasty ACS mode? Note: no sco in utf mode. */\r
-           else if(term->sco_acs && \r
-                   (c!='\033' && c!='\012' && c!='\015' && c!='\b'))\r
-           {\r
-              if (term->sco_acs == 2) c |= 0x80;\r
-              c |= CSET_SCOACS;\r
-           } else {\r
-               switch (term->cset_attr[term->cset]) {\r
-                   /* \r
-                    * Linedraw characters are different from 'ESC ( B'\r
-                    * only for a small range. For ones outside that\r
-                    * range, make sure we use the same font as well as\r
-                    * the same encoding.\r
-                    */\r
-                 case CSET_LINEDRW:\r
-                   if (term->ucsdata->unitab_ctrl[c] != 0xFF)\r
-                       c = term->ucsdata->unitab_ctrl[c];\r
-                   else\r
-                       c = ((unsigned char) c) | CSET_LINEDRW;\r
-                   break;\r
-\r
-                 case CSET_GBCHR:\r
-                   /* If UK-ASCII, make the '#' a LineDraw Pound */\r
-                   if (c == '#') {\r
-                       c = '}' | CSET_LINEDRW;\r
-                       break;\r
-                   }\r
-                 /*FALLTHROUGH*/ case CSET_ASCII:\r
-                   if (term->ucsdata->unitab_ctrl[c] != 0xFF)\r
-                       c = term->ucsdata->unitab_ctrl[c];\r
-                   else\r
-                       c = ((unsigned char) c) | CSET_ASCII;\r
-                   break;\r
-               case CSET_SCOACS:\r
-                   if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;\r
-                   break;\r
-               }\r
-           }\r
-       }\r
-\r
-       /*\r
-        * How about C1 controls? \r
-        * Explicitly ignore SCI (0x9a), which we don't translate to DECID.\r
-        */\r
-       if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&\r
-           !term->vt52_mode && has_compat(VT220)) {\r
-           if (c == 0x9a)\r
-               c = 0;\r
-           else {\r
-               term->termstate = SEEN_ESC;\r
-               term->esc_query = FALSE;\r
-               c = '@' + (c & 0x1F);\r
-           }\r
-       }\r
-\r
-       /* Or the GL control. */\r
-       if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {\r
-           if (term->curs.x && !term->wrapnext)\r
-               term->curs.x--;\r
-           term->wrapnext = FALSE;\r
-           /* destructive backspace might be disabled */\r
-           if (!term->cfg.no_dbackspace) {\r
-               check_boundary(term, term->curs.x, term->curs.y);\r
-               check_boundary(term, term->curs.x+1, term->curs.y);\r
-               copy_termchar(scrlineptr(term->curs.y),\r
-                             term->curs.x, &term->erase_char);\r
-           }\r
-       } else\r
-           /* Or normal C0 controls. */\r
-       if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {\r
-           switch (c) {\r
-             case '\005':             /* ENQ: terminal type query */\r
-               /* \r
-                * Strictly speaking this is VT100 but a VT100 defaults to\r
-                * no response. Other terminals respond at their option.\r
-                *\r
-                * Don't put a CR in the default string as this tends to\r
-                * upset some weird software.\r
-                */\r
-               compatibility(ANSIMIN);\r
-               if (term->ldisc) {\r
-                   char abuf[lenof(term->cfg.answerback)], *s, *d;\r
-                   for (s = term->cfg.answerback, d = abuf; *s;) {\r
-                       char *n;\r
-                       char c = ctrlparse(s, &n);\r
-                       if (n) {\r
-                           *d++ = c;\r
-                           s = n;\r
-                       } else {\r
-                           *d++ = *s++;\r
-                       }\r
-                   }\r
-                   lpage_send(term->ldisc, DEFAULT_CODEPAGE,\r
-                              abuf, d - abuf, 0);\r
-               }\r
-               break;\r
-             case '\007':            /* BEL: Bell */\r
-               {\r
-                   struct beeptime *newbeep;\r
-                   unsigned long ticks;\r
-\r
-                   ticks = GETTICKCOUNT();\r
-\r
-                   if (!term->beep_overloaded) {\r
-                       newbeep = snew(struct beeptime);\r
-                       newbeep->ticks = ticks;\r
-                       newbeep->next = NULL;\r
-                       if (!term->beephead)\r
-                           term->beephead = newbeep;\r
-                       else\r
-                           term->beeptail->next = newbeep;\r
-                       term->beeptail = newbeep;\r
-                       term->nbeeps++;\r
-                   }\r
-\r
-                   /*\r
-                    * Throw out any beeps that happened more than\r
-                    * t seconds ago.\r
-                    */\r
-                   while (term->beephead &&\r
-                          term->beephead->ticks < ticks - term->cfg.bellovl_t) {\r
-                       struct beeptime *tmp = term->beephead;\r
-                       term->beephead = tmp->next;\r
-                       sfree(tmp);\r
-                       if (!term->beephead)\r
-                           term->beeptail = NULL;\r
-                       term->nbeeps--;\r
-                   }\r
-\r
-                   if (term->cfg.bellovl && term->beep_overloaded &&\r
-                       ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {\r
-                       /*\r
-                        * If we're currently overloaded and the\r
-                        * last beep was more than s seconds ago,\r
-                        * leave overload mode.\r
-                        */\r
-                       term->beep_overloaded = FALSE;\r
-                   } else if (term->cfg.bellovl && !term->beep_overloaded &&\r
-                              term->nbeeps >= term->cfg.bellovl_n) {\r
-                       /*\r
-                        * Now, if we have n or more beeps\r
-                        * remaining in the queue, go into overload\r
-                        * mode.\r
-                        */\r
-                       term->beep_overloaded = TRUE;\r
-                   }\r
-                   term->lastbeep = ticks;\r
-\r
-                   /*\r
-                    * Perform an actual beep if we're not overloaded.\r
-                    */\r
-                   if (!term->cfg.bellovl || !term->beep_overloaded) {\r
-                       do_beep(term->frontend, term->cfg.beep);\r
-\r
-                       if (term->cfg.beep == BELL_VISUAL) {\r
-                           term_schedule_vbell(term, FALSE, 0);\r
-                       }\r
-                   }\r
-                   seen_disp_event(term);\r
-               }\r
-               break;\r
-             case '\b':              /* BS: Back space */\r
-               if (term->curs.x == 0 &&\r
-                   (term->curs.y == 0 || term->wrap == 0))\r
-                   /* do nothing */ ;\r
-               else if (term->curs.x == 0 && term->curs.y > 0)\r
-                   term->curs.x = term->cols - 1, term->curs.y--;\r
-               else if (term->wrapnext)\r
-                   term->wrapnext = FALSE;\r
-               else\r
-                   term->curs.x--;\r
-               seen_disp_event(term);\r
-               break;\r
-             case '\016':            /* LS1: Locking-shift one */\r
-               compatibility(VT100);\r
-               term->cset = 1;\r
-               break;\r
-             case '\017':            /* LS0: Locking-shift zero */\r
-               compatibility(VT100);\r
-               term->cset = 0;\r
-               break;\r
-             case '\033':            /* ESC: Escape */\r
-               if (term->vt52_mode)\r
-                   term->termstate = VT52_ESC;\r
-               else {\r
-                   compatibility(ANSIMIN);\r
-                   term->termstate = SEEN_ESC;\r
-                   term->esc_query = FALSE;\r
-               }\r
-               break;\r
-             case '\015':            /* CR: Carriage return */\r
-               term->curs.x = 0;\r
-               term->wrapnext = FALSE;\r
-               seen_disp_event(term);\r
-               term->paste_hold = 0;\r
-\r
-        if (term->cfg.crhaslf) {  \r
-                 if (term->curs.y == term->marg_b)\r
-                   scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-                 else if (term->curs.y < term->rows - 1)\r
-                   term->curs.y++;\r
-        }\r
-               if (term->logctx)\r
-                   logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);\r
-               break;\r
-             case '\014':            /* FF: Form feed */\r
-               if (has_compat(SCOANSI)) {\r
-                   move(term, 0, 0, 0);\r
-                   erase_lots(term, FALSE, FALSE, TRUE);\r
-                   term->disptop = 0;\r
-                   term->wrapnext = FALSE;\r
-                   seen_disp_event(term);\r
-                   break;\r
-               }\r
-             case '\013':            /* VT: Line tabulation */\r
-               compatibility(VT100);\r
-             case '\012':            /* LF: Line feed */\r
-               if (term->curs.y == term->marg_b)\r
-                   scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-               else if (term->curs.y < term->rows - 1)\r
-                   term->curs.y++;\r
-               if (term->cfg.lfhascr)\r
-                   term->curs.x = 0;\r
-               term->wrapnext = FALSE;\r
-               seen_disp_event(term);\r
-               term->paste_hold = 0;\r
-               if (term->logctx)\r
-                   logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);\r
-               break;\r
-             case '\t':              /* HT: Character tabulation */\r
-               {\r
-                   pos old_curs = term->curs;\r
-                   termline *ldata = scrlineptr(term->curs.y);\r
-\r
-                   do {\r
-                       term->curs.x++;\r
-                   } while (term->curs.x < term->cols - 1 &&\r
-                            !term->tabs[term->curs.x]);\r
-\r
-                   if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {\r
-                       if (term->curs.x >= term->cols / 2)\r
-                           term->curs.x = term->cols / 2 - 1;\r
-                   } else {\r
-                       if (term->curs.x >= term->cols)\r
-                           term->curs.x = term->cols - 1;\r
-                   }\r
-\r
-                   check_selection(term, old_curs, term->curs);\r
-               }\r
-               seen_disp_event(term);\r
-               break;\r
-           }\r
-       } else\r
-           switch (term->termstate) {\r
-             case TOPLEVEL:\r
-               /* Only graphic characters get this far;\r
-                * ctrls are stripped above */\r
-               {\r
-                   termline *cline = scrlineptr(term->curs.y);\r
-                   int width = 0;\r
-                   if (DIRECT_CHAR(c))\r
-                       width = 1;\r
-                   if (!width)\r
-                       width = (term->cfg.cjk_ambig_wide ?\r
-                                mk_wcwidth_cjk((wchar_t) c) :\r
-                                mk_wcwidth((wchar_t) c));\r
-\r
-                   if (term->wrapnext && term->wrap && width > 0) {\r
-                       cline->lattr |= LATTR_WRAPPED;\r
-                       if (term->curs.y == term->marg_b)\r
-                           scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-                       else if (term->curs.y < term->rows - 1)\r
-                           term->curs.y++;\r
-                       term->curs.x = 0;\r
-                       term->wrapnext = FALSE;\r
-                       cline = scrlineptr(term->curs.y);\r
-                   }\r
-                   if (term->insert && width > 0)\r
-                       insch(term, width);\r
-                   if (term->selstate != NO_SELECTION) {\r
-                       pos cursplus = term->curs;\r
-                       incpos(cursplus);\r
-                       check_selection(term, term->curs, cursplus);\r
-                   }\r
-                   if (((c & CSET_MASK) == CSET_ASCII ||\r
-                        (c & CSET_MASK) == 0) &&\r
-                       term->logctx)\r
-                       logtraffic(term->logctx, (unsigned char) c,\r
-                                  LGTYP_ASCII);\r
-\r
-                   switch (width) {\r
-                     case 2:\r
-                       /*\r
-                        * If we're about to display a double-width\r
-                        * character starting in the rightmost\r
-                        * column, then we do something special\r
-                        * instead. We must print a space in the\r
-                        * last column of the screen, then wrap;\r
-                        * and we also set LATTR_WRAPPED2 which\r
-                        * instructs subsequent cut-and-pasting not\r
-                        * only to splice this line to the one\r
-                        * after it, but to ignore the space in the\r
-                        * last character position as well.\r
-                        * (Because what was actually output to the\r
-                        * terminal was presumably just a sequence\r
-                        * of CJK characters, and we don't want a\r
-                        * space to be pasted in the middle of\r
-                        * those just because they had the\r
-                        * misfortune to start in the wrong parity\r
-                        * column. xterm concurs.)\r
-                        */\r
-                       check_boundary(term, term->curs.x, term->curs.y);\r
-                       check_boundary(term, term->curs.x+2, term->curs.y);\r
-                       if (term->curs.x == term->cols-1) {\r
-                           copy_termchar(cline, term->curs.x,\r
-                                         &term->erase_char);\r
-                           cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;\r
-                           if (term->curs.y == term->marg_b)\r
-                               scroll(term, term->marg_t, term->marg_b,\r
-                                      1, TRUE);\r
-                           else if (term->curs.y < term->rows - 1)\r
-                               term->curs.y++;\r
-                           term->curs.x = 0;\r
-                           cline = scrlineptr(term->curs.y);\r
-                           /* Now we must check_boundary again, of course. */\r
-                           check_boundary(term, term->curs.x, term->curs.y);\r
-                           check_boundary(term, term->curs.x+2, term->curs.y);\r
-                       }\r
-\r
-                       /* FULL-TERMCHAR */\r
-                       clear_cc(cline, term->curs.x);\r
-                       cline->chars[term->curs.x].chr = c;\r
-                       cline->chars[term->curs.x].attr = term->curr_attr;\r
-\r
-                       term->curs.x++;\r
-\r
-                       /* FULL-TERMCHAR */\r
-                       clear_cc(cline, term->curs.x);\r
-                       cline->chars[term->curs.x].chr = UCSWIDE;\r
-                       cline->chars[term->curs.x].attr = term->curr_attr;\r
-\r
-                       break;\r
-                     case 1:\r
-                       check_boundary(term, term->curs.x, term->curs.y);\r
-                       check_boundary(term, term->curs.x+1, term->curs.y);\r
-\r
-                       /* FULL-TERMCHAR */\r
-                       clear_cc(cline, term->curs.x);\r
-                       cline->chars[term->curs.x].chr = c;\r
-                       cline->chars[term->curs.x].attr = term->curr_attr;\r
-\r
-                       break;\r
-                     case 0:\r
-                       if (term->curs.x > 0) {\r
-                           int x = term->curs.x - 1;\r
-\r
-                           /* If we're in wrapnext state, the character\r
-                            * to combine with is _here_, not to our left. */\r
-                           if (term->wrapnext)\r
-                               x++;\r
-\r
-                           /*\r
-                            * If the previous character is\r
-                            * UCSWIDE, back up another one.\r
-                            */\r
-                           if (cline->chars[x].chr == UCSWIDE) {\r
-                               assert(x > 0);\r
-                               x--;\r
-                           }\r
-\r
-                           add_cc(cline, x, c);\r
-                           seen_disp_event(term);\r
-                       }\r
-                       continue;\r
-                     default:\r
-                       continue;\r
-                   }\r
-                   term->curs.x++;\r
-                   if (term->curs.x == term->cols) {\r
-                       term->curs.x--;\r
-                       term->wrapnext = TRUE;\r
-                       if (term->wrap && term->vt52_mode) {\r
-                           cline->lattr |= LATTR_WRAPPED;\r
-                           if (term->curs.y == term->marg_b)\r
-                               scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-                           else if (term->curs.y < term->rows - 1)\r
-                               term->curs.y++;\r
-                           term->curs.x = 0;\r
-                           term->wrapnext = FALSE;\r
-                       }\r
-                   }\r
-                   seen_disp_event(term);\r
-               }\r
-               break;\r
-\r
-             case OSC_MAYBE_ST:\r
-               /*\r
-                * This state is virtually identical to SEEN_ESC, with the\r
-                * exception that we have an OSC sequence in the pipeline,\r
-                * and _if_ we see a backslash, we process it.\r
-                */\r
-               if (c == '\\') {\r
-                   do_osc(term);\r
-                   term->termstate = TOPLEVEL;\r
-                   break;\r
-               }\r
-               /* else fall through */\r
-             case SEEN_ESC:\r
-               if (c >= ' ' && c <= '/') {\r
-                   if (term->esc_query)\r
-                       term->esc_query = -1;\r
-                   else\r
-                       term->esc_query = c;\r
-                   break;\r
-               }\r
-               term->termstate = TOPLEVEL;\r
-               switch (ANSI(c, term->esc_query)) {\r
-                 case '[':             /* enter CSI mode */\r
-                   term->termstate = SEEN_CSI;\r
-                   term->esc_nargs = 1;\r
-                   term->esc_args[0] = ARG_DEFAULT;\r
-                   term->esc_query = FALSE;\r
-                   break;\r
-                 case ']':             /* OSC: xterm escape sequences */\r
-                   /* Compatibility is nasty here, xterm, linux, decterm yuk! */\r
-                   compatibility(OTHER);\r
-                   term->termstate = SEEN_OSC;\r
-                   term->esc_args[0] = 0;\r
-                   break;\r
-                 case '7':             /* DECSC: save cursor */\r
-                   compatibility(VT100);\r
-                   save_cursor(term, TRUE);\r
-                   break;\r
-                 case '8':             /* DECRC: restore cursor */\r
-                   compatibility(VT100);\r
-                   save_cursor(term, FALSE);\r
-                   seen_disp_event(term);\r
-                   break;\r
-                 case '=':             /* DECKPAM: Keypad application mode */\r
-                   compatibility(VT100);\r
-                   term->app_keypad_keys = TRUE;\r
-                   break;\r
-                 case '>':             /* DECKPNM: Keypad numeric mode */\r
-                   compatibility(VT100);\r
-                   term->app_keypad_keys = FALSE;\r
-                   break;\r
-                 case 'D':            /* IND: exactly equivalent to LF */\r
-                   compatibility(VT100);\r
-                   if (term->curs.y == term->marg_b)\r
-                       scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-                   else if (term->curs.y < term->rows - 1)\r
-                       term->curs.y++;\r
-                   term->wrapnext = FALSE;\r
-                   seen_disp_event(term);\r
-                   break;\r
-                 case 'E':            /* NEL: exactly equivalent to CR-LF */\r
-                   compatibility(VT100);\r
-                   term->curs.x = 0;\r
-                   if (term->curs.y == term->marg_b)\r
-                       scroll(term, term->marg_t, term->marg_b, 1, TRUE);\r
-                   else if (term->curs.y < term->rows - 1)\r
-                       term->curs.y++;\r
-                   term->wrapnext = FALSE;\r
-                   seen_disp_event(term);\r
-                   break;\r
-                 case 'M':            /* RI: reverse index - backwards LF */\r
-                   compatibility(VT100);\r
-                   if (term->curs.y == term->marg_t)\r
-                       scroll(term, term->marg_t, term->marg_b, -1, TRUE);\r
-                   else if (term->curs.y > 0)\r
-                       term->curs.y--;\r
-                   term->wrapnext = FALSE;\r
-                   seen_disp_event(term);\r
-                   break;\r
-                 case 'Z':            /* DECID: terminal type query */\r
-                   compatibility(VT100);\r
-                   if (term->ldisc)\r
-                       ldisc_send(term->ldisc, term->id_string,\r
-                                  strlen(term->id_string), 0);\r
-                   break;\r
-                 case 'c':            /* RIS: restore power-on settings */\r
-                   compatibility(VT100);\r
-                   power_on(term, TRUE);\r
-                   if (term->ldisc)   /* cause ldisc to notice changes */\r
-                       ldisc_send(term->ldisc, NULL, 0, 0);\r
-                   if (term->reset_132) {\r
-                       if (!term->cfg.no_remote_resize)\r
-                           request_resize(term->frontend, 80, term->rows);\r
-                       term->reset_132 = 0;\r
-                   }\r
-                   term->disptop = 0;\r
-                   seen_disp_event(term);\r
-                   break;\r
-                 case 'H':            /* HTS: set a tab */\r
-                   compatibility(VT100);\r
-                   term->tabs[term->curs.x] = TRUE;\r
-                   break;\r
-\r
-                 case ANSI('8', '#'):  /* DECALN: fills screen with Es :-) */\r
-                   compatibility(VT100);\r
-                   {\r
-                       termline *ldata;\r
-                       int i, j;\r
-                       pos scrtop, scrbot;\r
-\r
-                       for (i = 0; i < term->rows; i++) {\r
-                           ldata = scrlineptr(i);\r
-                           for (j = 0; j < term->cols; j++) {\r
-                               copy_termchar(ldata, j,\r
-                                             &term->basic_erase_char);\r
-                               ldata->chars[j].chr = 'E';\r
-                           }\r
-                           ldata->lattr = LATTR_NORM;\r
-                       }\r
-                       term->disptop = 0;\r
-                       seen_disp_event(term);\r
-                       scrtop.x = scrtop.y = 0;\r
-                       scrbot.x = 0;\r
-                       scrbot.y = term->rows;\r
-                       check_selection(term, scrtop, scrbot);\r
-                   }\r
-                   break;\r
-\r
-                 case ANSI('3', '#'):\r
-                 case ANSI('4', '#'):\r
-                 case ANSI('5', '#'):\r
-                 case ANSI('6', '#'):\r
-                   compatibility(VT100);\r
-                   {\r
-                       int nlattr;\r
-\r
-                       switch (ANSI(c, term->esc_query)) {\r
-                         case ANSI('3', '#'): /* DECDHL: 2*height, top */\r
-                           nlattr = LATTR_TOP;\r
-                           break;\r
-                         case ANSI('4', '#'): /* DECDHL: 2*height, bottom */\r
-                           nlattr = LATTR_BOT;\r
-                           break;\r
-                         case ANSI('5', '#'): /* DECSWL: normal */\r
-                           nlattr = LATTR_NORM;\r
-                           break;\r
-                         default: /* case ANSI('6', '#'): DECDWL: 2*width */\r
-                           nlattr = LATTR_WIDE;\r
-                           break;\r
-                       }\r
-                       scrlineptr(term->curs.y)->lattr = nlattr;\r
-                   }\r
-                   break;\r
-                 /* GZD4: G0 designate 94-set */\r
-                 case ANSI('A', '('):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[0] = CSET_GBCHR;\r
-                   break;\r
-                 case ANSI('B', '('):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[0] = CSET_ASCII;\r
-                   break;\r
-                 case ANSI('0', '('):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[0] = CSET_LINEDRW;\r
-                   break;\r
-                 case ANSI('U', '('): \r
-                   compatibility(OTHER);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[0] = CSET_SCOACS; \r
-                   break;\r
-                 /* G1D4: G1-designate 94-set */\r
-                 case ANSI('A', ')'):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[1] = CSET_GBCHR;\r
-                   break;\r
-                 case ANSI('B', ')'):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[1] = CSET_ASCII;\r
-                   break;\r
-                 case ANSI('0', ')'):\r
-                   compatibility(VT100);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[1] = CSET_LINEDRW;\r
-                   break;\r
-                 case ANSI('U', ')'): \r
-                   compatibility(OTHER);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->cset_attr[1] = CSET_SCOACS; \r
-                   break;\r
-                 /* DOCS: Designate other coding system */\r
-                 case ANSI('8', '%'):  /* Old Linux code */\r
-                 case ANSI('G', '%'):\r
-                   compatibility(OTHER);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->utf = 1;\r
-                   break;\r
-                 case ANSI('@', '%'):\r
-                   compatibility(OTHER);\r
-                   if (!term->cfg.no_remote_charset)\r
-                       term->utf = 0;\r
-                   break;\r
-               }\r
-               break;\r
-             case SEEN_CSI:\r
-               term->termstate = TOPLEVEL;  /* default */\r
-               if (isdigit(c)) {\r
-                   if (term->esc_nargs <= ARGS_MAX) {\r
-                       if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)\r
-                           term->esc_args[term->esc_nargs - 1] = 0;\r
-                       term->esc_args[term->esc_nargs - 1] =\r
-                           10 * term->esc_args[term->esc_nargs - 1] + c - '0';\r
-                   }\r
-                   term->termstate = SEEN_CSI;\r
-               } else if (c == ';') {\r
-                   if (term->esc_nargs < ARGS_MAX)\r
-                       term->esc_args[term->esc_nargs++] = ARG_DEFAULT;\r
-                   term->termstate = SEEN_CSI;\r
-               } else if (c < '@') {\r
-                   if (term->esc_query)\r
-                       term->esc_query = -1;\r
-                   else if (c == '?')\r
-                       term->esc_query = TRUE;\r
-                   else\r
-                       term->esc_query = c;\r
-                   term->termstate = SEEN_CSI;\r
-               } else\r
-                   switch (ANSI(c, term->esc_query)) {\r
-                     case 'A':       /* CUU: move up N lines */\r
-                       move(term, term->curs.x,\r
-                            term->curs.y - def(term->esc_args[0], 1), 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'e':         /* VPR: move down N lines */\r
-                       compatibility(ANSI);\r
-                       /* FALLTHROUGH */\r
-                     case 'B':         /* CUD: Cursor down */\r
-                       move(term, term->curs.x,\r
-                            term->curs.y + def(term->esc_args[0], 1), 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case ANSI('c', '>'):      /* DA: report xterm version */\r
-                       compatibility(OTHER);\r
-                       /* this reports xterm version 136 so that VIM can\r
-                          use the drag messages from the mouse reporting */\r
-                       if (term->ldisc)\r
-                           ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);\r
-                       break;\r
-                     case 'a':         /* HPR: move right N cols */\r
-                       compatibility(ANSI);\r
-                       /* FALLTHROUGH */\r
-                     case 'C':         /* CUF: Cursor right */ \r
-                       move(term, term->curs.x + def(term->esc_args[0], 1),\r
-                            term->curs.y, 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'D':       /* CUB: move left N cols */\r
-                       move(term, term->curs.x - def(term->esc_args[0], 1),\r
-                            term->curs.y, 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'E':       /* CNL: move down N lines and CR */\r
-                       compatibility(ANSI);\r
-                       move(term, 0,\r
-                            term->curs.y + def(term->esc_args[0], 1), 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'F':       /* CPL: move up N lines and CR */\r
-                       compatibility(ANSI);\r
-                       move(term, 0,\r
-                            term->curs.y - def(term->esc_args[0], 1), 1);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'G':       /* CHA */\r
-                     case '`':       /* HPA: set horizontal posn */\r
-                       compatibility(ANSI);\r
-                       move(term, def(term->esc_args[0], 1) - 1,\r
-                            term->curs.y, 0);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'd':       /* VPA: set vertical posn */\r
-                       compatibility(ANSI);\r
-                       move(term, term->curs.x,\r
-                            ((term->dec_om ? term->marg_t : 0) +\r
-                             def(term->esc_args[0], 1) - 1),\r
-                            (term->dec_om ? 2 : 0));\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'H':      /* CUP */\r
-                     case 'f':      /* HVP: set horz and vert posns at once */\r
-                       if (term->esc_nargs < 2)\r
-                           term->esc_args[1] = ARG_DEFAULT;\r
-                       move(term, def(term->esc_args[1], 1) - 1,\r
-                            ((term->dec_om ? term->marg_t : 0) +\r
-                             def(term->esc_args[0], 1) - 1),\r
-                            (term->dec_om ? 2 : 0));\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'J':       /* ED: erase screen or parts of it */\r
-                       {\r
-                           unsigned int i = def(term->esc_args[0], 0);\r
-                           if (i == 3) {\r
-                               /* Erase Saved Lines (xterm)\r
-                                * This follows Thomas Dickey's xterm. */\r
-                               term_clrsb(term);\r
-                           } else {\r
-                               i++;\r
-                               if (i > 3)\r
-                                   i = 0;\r
-                               erase_lots(term, FALSE, !!(i & 2), !!(i & 1));\r
-                           }\r
-                       }\r
-                       term->disptop = 0;\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'K':       /* EL: erase line or parts of it */\r
-                       {\r
-                           unsigned int i = def(term->esc_args[0], 0) + 1;\r
-                           if (i > 3)\r
-                               i = 0;\r
-                           erase_lots(term, TRUE, !!(i & 2), !!(i & 1));\r
-                       }\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'L':       /* IL: insert lines */\r
-                       compatibility(VT102);\r
-                       if (term->curs.y <= term->marg_b)\r
-                           scroll(term, term->curs.y, term->marg_b,\r
-                                  -def(term->esc_args[0], 1), FALSE);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'M':       /* DL: delete lines */\r
-                       compatibility(VT102);\r
-                       if (term->curs.y <= term->marg_b)\r
-                           scroll(term, term->curs.y, term->marg_b,\r
-                                  def(term->esc_args[0], 1),\r
-                                  TRUE);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case '@':       /* ICH: insert chars */\r
-                       /* XXX VTTEST says this is vt220, vt510 manual says vt102 */\r
-                       compatibility(VT102);\r
-                       insch(term, def(term->esc_args[0], 1));\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'P':       /* DCH: delete chars */\r
-                       compatibility(VT102);\r
-                       insch(term, -def(term->esc_args[0], 1));\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'c':       /* DA: terminal type query */\r
-                       compatibility(VT100);\r
-                       /* This is the response for a VT102 */\r
-                       if (term->ldisc)\r
-                           ldisc_send(term->ldisc, term->id_string,\r
-                                      strlen(term->id_string), 0);\r
-                       break;\r
-                     case 'n':       /* DSR: cursor position query */\r
-                       if (term->ldisc) {\r
-                           if (term->esc_args[0] == 6) {\r
-                               char buf[32];\r
-                               sprintf(buf, "\033[%d;%dR", term->curs.y + 1,\r
-                                       term->curs.x + 1);\r
-                               ldisc_send(term->ldisc, buf, strlen(buf), 0);\r
-                           } else if (term->esc_args[0] == 5) {\r
-                               ldisc_send(term->ldisc, "\033[0n", 4, 0);\r
-                           }\r
-                       }\r
-                       break;\r
-                     case 'h':       /* SM: toggle modes to high */\r
-                     case ANSI_QUE('h'):\r
-                       compatibility(VT100);\r
-                       {\r
-                           int i;\r
-                           for (i = 0; i < term->esc_nargs; i++)\r
-                               toggle_mode(term, term->esc_args[i],\r
-                                           term->esc_query, TRUE);\r
-                       }\r
-                       break;\r
-                     case 'i':         /* MC: Media copy */\r
-                     case ANSI_QUE('i'):\r
-                       compatibility(VT100);\r
-                       {\r
-                           if (term->esc_nargs != 1) break;\r
-                           if (term->esc_args[0] == 5 && *term->cfg.printer) {\r
-                               term->printing = TRUE;\r
-                               term->only_printing = !term->esc_query;\r
-                               term->print_state = 0;\r
-                               term_print_setup(term);\r
-                           } else if (term->esc_args[0] == 4 &&\r
-                                      term->printing) {\r
-                               term_print_finish(term);\r
-                           }\r
-                       }\r
-                       break;                  \r
-                     case 'l':       /* RM: toggle modes to low */\r
-                     case ANSI_QUE('l'):\r
-                       compatibility(VT100);\r
-                       {\r
-                           int i;\r
-                           for (i = 0; i < term->esc_nargs; i++)\r
-                               toggle_mode(term, term->esc_args[i],\r
-                                           term->esc_query, FALSE);\r
-                       }\r
-                       break;\r
-                     case 'g':       /* TBC: clear tabs */\r
-                       compatibility(VT100);\r
-                       if (term->esc_nargs == 1) {\r
-                           if (term->esc_args[0] == 0) {\r
-                               term->tabs[term->curs.x] = FALSE;\r
-                           } else if (term->esc_args[0] == 3) {\r
-                               int i;\r
-                               for (i = 0; i < term->cols; i++)\r
-                                   term->tabs[i] = FALSE;\r
-                           }\r
-                       }\r
-                       break;\r
-                     case 'r':       /* DECSTBM: set scroll margins */\r
-                       compatibility(VT100);\r
-                       if (term->esc_nargs <= 2) {\r
-                           int top, bot;\r
-                           top = def(term->esc_args[0], 1) - 1;\r
-                           bot = (term->esc_nargs <= 1\r
-                                  || term->esc_args[1] == 0 ?\r
-                                  term->rows :\r
-                                  def(term->esc_args[1], term->rows)) - 1;\r
-                           if (bot >= term->rows)\r
-                               bot = term->rows - 1;\r
-                           /* VTTEST Bug 9 - if region is less than 2 lines\r
-                            * don't change region.\r
-                            */\r
-                           if (bot - top > 0) {\r
-                               term->marg_t = top;\r
-                               term->marg_b = bot;\r
-                               term->curs.x = 0;\r
-                               /*\r
-                                * I used to think the cursor should be\r
-                                * placed at the top of the newly marginned\r
-                                * area. Apparently not: VMS TPU falls over\r
-                                * if so.\r
-                                *\r
-                                * Well actually it should for\r
-                                * Origin mode - RDB\r
-                                */\r
-                               term->curs.y = (term->dec_om ?\r
-                                               term->marg_t : 0);\r
-                               seen_disp_event(term);\r
-                           }\r
-                       }\r
-                       break;\r
-                     case 'm':       /* SGR: set graphics rendition */\r
-                       {\r
-                           /* \r
-                            * A VT100 without the AVO only had one\r
-                            * attribute, either underline or\r
-                            * reverse video depending on the\r
-                            * cursor type, this was selected by\r
-                            * CSI 7m.\r
-                            *\r
-                            * case 2:\r
-                            *  This is sometimes DIM, eg on the\r
-                            *  GIGI and Linux\r
-                            * case 8:\r
-                            *  This is sometimes INVIS various ANSI.\r
-                            * case 21:\r
-                            *  This like 22 disables BOLD, DIM and INVIS\r
-                            *\r
-                            * The ANSI colours appear on any\r
-                            * terminal that has colour (obviously)\r
-                            * but the interaction between sgr0 and\r
-                            * the colours varies but is usually\r
-                            * related to the background colour\r
-                            * erase item. The interaction between\r
-                            * colour attributes and the mono ones\r
-                            * is also very implementation\r
-                            * dependent.\r
-                            *\r
-                            * The 39 and 49 attributes are likely\r
-                            * to be unimplemented.\r
-                            */\r
-                           int i;\r
-                           for (i = 0; i < term->esc_nargs; i++) {\r
-                               switch (def(term->esc_args[i], 0)) {\r
-                                 case 0:       /* restore defaults */\r
-                                   term->curr_attr = term->default_attr;\r
-                                   break;\r
-                                 case 1:       /* enable bold */\r
-                                   compatibility(VT100AVO);\r
-                                   term->curr_attr |= ATTR_BOLD;\r
-                                   break;\r
-                                 case 21:      /* (enable double underline) */\r
-                                   compatibility(OTHER);\r
-                                 case 4:       /* enable underline */\r
-                                   compatibility(VT100AVO);\r
-                                   term->curr_attr |= ATTR_UNDER;\r
-                                   break;\r
-                                 case 5:       /* enable blink */\r
-                                   compatibility(VT100AVO);\r
-                                   term->curr_attr |= ATTR_BLINK;\r
-                                   break;\r
-                                 case 6:       /* SCO light bkgrd */\r
-                                   compatibility(SCOANSI);\r
-                                   term->blink_is_real = FALSE;\r
-                                   term->curr_attr |= ATTR_BLINK;\r
-                                   term_schedule_tblink(term);\r
-                                   break;\r
-                                 case 7:       /* enable reverse video */\r
-                                   term->curr_attr |= ATTR_REVERSE;\r
-                                   break;\r
-                                 case 10:      /* SCO acs off */\r
-                                   compatibility(SCOANSI);\r
-                                   if (term->cfg.no_remote_charset) break;\r
-                                   term->sco_acs = 0; break;\r
-                                 case 11:      /* SCO acs on */\r
-                                   compatibility(SCOANSI);\r
-                                   if (term->cfg.no_remote_charset) break;\r
-                                   term->sco_acs = 1; break;\r
-                                 case 12:      /* SCO acs on, |0x80 */\r
-                                   compatibility(SCOANSI);\r
-                                   if (term->cfg.no_remote_charset) break;\r
-                                   term->sco_acs = 2; break;\r
-                                 case 22:      /* disable bold */\r
-                                   compatibility2(OTHER, VT220);\r
-                                   term->curr_attr &= ~ATTR_BOLD;\r
-                                   break;\r
-                                 case 24:      /* disable underline */\r
-                                   compatibility2(OTHER, VT220);\r
-                                   term->curr_attr &= ~ATTR_UNDER;\r
-                                   break;\r
-                                 case 25:      /* disable blink */\r
-                                   compatibility2(OTHER, VT220);\r
-                                   term->curr_attr &= ~ATTR_BLINK;\r
-                                   break;\r
-                                 case 27:      /* disable reverse video */\r
-                                   compatibility2(OTHER, VT220);\r
-                                   term->curr_attr &= ~ATTR_REVERSE;\r
-                                   break;\r
-                                 case 30:\r
-                                 case 31:\r
-                                 case 32:\r
-                                 case 33:\r
-                                 case 34:\r
-                                 case 35:\r
-                                 case 36:\r
-                                 case 37:\r
-                                   /* foreground */\r
-                                   term->curr_attr &= ~ATTR_FGMASK;\r
-                                   term->curr_attr |=\r
-                                       (term->esc_args[i] - 30)<<ATTR_FGSHIFT;\r
-                                   break;\r
-                                 case 90:\r
-                                 case 91:\r
-                                 case 92:\r
-                                 case 93:\r
-                                 case 94:\r
-                                 case 95:\r
-                                 case 96:\r
-                                 case 97:\r
-                                   /* aixterm-style bright foreground */\r
-                                   term->curr_attr &= ~ATTR_FGMASK;\r
-                                   term->curr_attr |=\r
-                                       ((term->esc_args[i] - 90 + 8)\r
-                                         << ATTR_FGSHIFT);\r
-                                   break;\r
-                                 case 39:      /* default-foreground */\r
-                                   term->curr_attr &= ~ATTR_FGMASK;\r
-                                   term->curr_attr |= ATTR_DEFFG;\r
-                                   break;\r
-                                 case 40:\r
-                                 case 41:\r
-                                 case 42:\r
-                                 case 43:\r
-                                 case 44:\r
-                                 case 45:\r
-                                 case 46:\r
-                                 case 47:\r
-                                   /* background */\r
-                                   term->curr_attr &= ~ATTR_BGMASK;\r
-                                   term->curr_attr |=\r
-                                       (term->esc_args[i] - 40)<<ATTR_BGSHIFT;\r
-                                   break;\r
-                                 case 100:\r
-                                 case 101:\r
-                                 case 102:\r
-                                 case 103:\r
-                                 case 104:\r
-                                 case 105:\r
-                                 case 106:\r
-                                 case 107:\r
-                                   /* aixterm-style bright background */\r
-                                   term->curr_attr &= ~ATTR_BGMASK;\r
-                                   term->curr_attr |=\r
-                                       ((term->esc_args[i] - 100 + 8)\r
-                                         << ATTR_BGSHIFT);\r
-                                   break;\r
-                                 case 49:      /* default-background */\r
-                                   term->curr_attr &= ~ATTR_BGMASK;\r
-                                   term->curr_attr |= ATTR_DEFBG;\r
-                                   break;\r
-                                 case 38:   /* xterm 256-colour mode */\r
-                                   if (i+2 < term->esc_nargs &&\r
-                                       term->esc_args[i+1] == 5) {\r
-                                       term->curr_attr &= ~ATTR_FGMASK;\r
-                                       term->curr_attr |=\r
-                                           ((term->esc_args[i+2] & 0xFF)\r
-                                            << ATTR_FGSHIFT);\r
-                                       i += 2;\r
-                                   }\r
-                                   break;\r
-                                 case 48:   /* xterm 256-colour mode */\r
-                                   if (i+2 < term->esc_nargs &&\r
-                                       term->esc_args[i+1] == 5) {\r
-                                       term->curr_attr &= ~ATTR_BGMASK;\r
-                                       term->curr_attr |=\r
-                                           ((term->esc_args[i+2] & 0xFF)\r
-                                            << ATTR_BGSHIFT);\r
-                                       i += 2;\r
-                                   }\r
-                                   break;\r
-                               }\r
-                           }\r
-                           set_erase_char(term);\r
-                       }\r
-                       break;\r
-                     case 's':       /* save cursor */\r
-                       save_cursor(term, TRUE);\r
-                       break;\r
-                     case 'u':       /* restore cursor */\r
-                       save_cursor(term, FALSE);\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 't': /* DECSLPP: set page size - ie window height */\r
-                       /*\r
-                        * VT340/VT420 sequence DECSLPP, DEC only allows values\r
-                        *  24/25/36/48/72/144 other emulators (eg dtterm) use\r
-                        * illegal values (eg first arg 1..9) for window changing \r
-                        * and reports.\r
-                        */\r
-                       if (term->esc_nargs <= 1\r
-                           && (term->esc_args[0] < 1 ||\r
-                               term->esc_args[0] >= 24)) {\r
-                           compatibility(VT340TEXT);\r
-                           if (!term->cfg.no_remote_resize)\r
-                               request_resize(term->frontend, term->cols,\r
-                                              def(term->esc_args[0], 24));\r
-                           deselect(term);\r
-                       } else if (term->esc_nargs >= 1 &&\r
-                                  term->esc_args[0] >= 1 &&\r
-                                  term->esc_args[0] < 24) {\r
-                           compatibility(OTHER);\r
-\r
-                           switch (term->esc_args[0]) {\r
-                               int x, y, len;\r
-                               char buf[80], *p;\r
-                             case 1:\r
-                               set_iconic(term->frontend, FALSE);\r
-                               break;\r
-                             case 2:\r
-                               set_iconic(term->frontend, TRUE);\r
-                               break;\r
-                             case 3:\r
-                               if (term->esc_nargs >= 3) {\r
-                                   if (!term->cfg.no_remote_resize)\r
-                                       move_window(term->frontend,\r
-                                                   def(term->esc_args[1], 0),\r
-                                                   def(term->esc_args[2], 0));\r
-                               }\r
-                               break;\r
-                             case 4:\r
-                               /* We should resize the window to a given\r
-                                * size in pixels here, but currently our\r
-                                * resizing code isn't healthy enough to\r
-                                * manage it. */\r
-                               break;\r
-                             case 5:\r
-                               /* move to top */\r
-                               set_zorder(term->frontend, TRUE);\r
-                               break;\r
-                             case 6:\r
-                               /* move to bottom */\r
-                               set_zorder(term->frontend, FALSE);\r
-                               break;\r
-                             case 7:\r
-                               refresh_window(term->frontend);\r
-                               break;\r
-                             case 8:\r
-                               if (term->esc_nargs >= 3) {\r
-                                   if (!term->cfg.no_remote_resize)\r
-                                       request_resize(term->frontend,\r
-                                                      def(term->esc_args[2], term->cfg.width),\r
-                                                      def(term->esc_args[1], term->cfg.height));\r
-                               }\r
-                               break;\r
-                             case 9:\r
-                               if (term->esc_nargs >= 2)\r
-                                   set_zoomed(term->frontend,\r
-                                              term->esc_args[1] ?\r
-                                              TRUE : FALSE);\r
-                               break;\r
-                             case 11:\r
-                               if (term->ldisc)\r
-                                   ldisc_send(term->ldisc,\r
-                                              is_iconic(term->frontend) ?\r
-                                              "\033[2t" : "\033[1t", 4, 0);\r
-                               break;\r
-                             case 13:\r
-                               if (term->ldisc) {\r
-                                   get_window_pos(term->frontend, &x, &y);\r
-                                   len = sprintf(buf, "\033[3;%d;%dt", x, y);\r
-                                   ldisc_send(term->ldisc, buf, len, 0);\r
-                               }\r
-                               break;\r
-                             case 14:\r
-                               if (term->ldisc) {\r
-                                   get_window_pixels(term->frontend, &x, &y);\r
-                                   len = sprintf(buf, "\033[4;%d;%dt", y, x);\r
-                                   ldisc_send(term->ldisc, buf, len, 0);\r
-                               }\r
-                               break;\r
-                             case 18:\r
-                               if (term->ldisc) {\r
-                                   len = sprintf(buf, "\033[8;%d;%dt",\r
-                                                 term->rows, term->cols);\r
-                                   ldisc_send(term->ldisc, buf, len, 0);\r
-                               }\r
-                               break;\r
-                             case 19:\r
-                               /*\r
-                                * Hmmm. Strictly speaking we\r
-                                * should return `the size of the\r
-                                * screen in characters', but\r
-                                * that's not easy: (a) window\r
-                                * furniture being what it is it's\r
-                                * hard to compute, and (b) in\r
-                                * resize-font mode maximising the\r
-                                * window wouldn't change the\r
-                                * number of characters. *shrug*. I\r
-                                * think we'll ignore it for the\r
-                                * moment and see if anyone\r
-                                * complains, and then ask them\r
-                                * what they would like it to do.\r
-                                */\r
-                               break;\r
-                             case 20:\r
-                               if (term->ldisc &&\r
-                                   term->cfg.remote_qtitle_action != TITLE_NONE) {\r
-                                   if(term->cfg.remote_qtitle_action == TITLE_REAL)\r
-                                       p = get_window_title(term->frontend, TRUE);\r
-                                   else\r
-                                       p = EMPTY_WINDOW_TITLE;\r
-                                   len = strlen(p);\r
-                                   ldisc_send(term->ldisc, "\033]L", 3, 0);\r
-                                   ldisc_send(term->ldisc, p, len, 0);\r
-                                   ldisc_send(term->ldisc, "\033\\", 2, 0);\r
-                               }\r
-                               break;\r
-                             case 21:\r
-                               if (term->ldisc &&\r
-                                   term->cfg.remote_qtitle_action != TITLE_NONE) {\r
-                                   if(term->cfg.remote_qtitle_action == TITLE_REAL)\r
-                                       p = get_window_title(term->frontend, FALSE);\r
-                                   else\r
-                                       p = EMPTY_WINDOW_TITLE;\r
-                                   len = strlen(p);\r
-                                   ldisc_send(term->ldisc, "\033]l", 3, 0);\r
-                                   ldisc_send(term->ldisc, p, len, 0);\r
-                                   ldisc_send(term->ldisc, "\033\\", 2, 0);\r
-                               }\r
-                               break;\r
-                           }\r
-                       }\r
-                       break;\r
-                     case 'S':         /* SU: Scroll up */\r
-                       compatibility(SCOANSI);\r
-                       scroll(term, term->marg_t, term->marg_b,\r
-                              def(term->esc_args[0], 1), TRUE);\r
-                       term->wrapnext = FALSE;\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case 'T':         /* SD: Scroll down */\r
-                       compatibility(SCOANSI);\r
-                       scroll(term, term->marg_t, term->marg_b,\r
-                              -def(term->esc_args[0], 1), TRUE);\r
-                       term->wrapnext = FALSE;\r
-                       seen_disp_event(term);\r
-                       break;\r
-                     case ANSI('|', '*'): /* DECSNLS */\r
-                       /* \r
-                        * Set number of lines on screen\r
-                        * VT420 uses VGA like hardware and can\r
-                        * support any size in reasonable range\r
-                        * (24..49 AIUI) with no default specified.\r
-                        */\r
-                       compatibility(VT420);\r
-                       if (term->esc_nargs == 1 && term->esc_args[0] > 0) {\r
-                           if (!term->cfg.no_remote_resize)\r
-                               request_resize(term->frontend, term->cols,\r
-                                              def(term->esc_args[0],\r
-                                                  term->cfg.height));\r
-                           deselect(term);\r
-                       }\r
-                       break;\r
-                     case ANSI('|', '$'): /* DECSCPP */\r
-                       /*\r
-                        * Set number of columns per page\r
-                        * Docs imply range is only 80 or 132, but\r
-                        * I'll allow any.\r
-                        */\r
-                       compatibility(VT340TEXT);\r
-                       if (term->esc_nargs <= 1) {\r
-                           if (!term->cfg.no_remote_resize)\r
-                               request_resize(term->frontend,\r
-                                              def(term->esc_args[0],\r
-                                                  term->cfg.width), term->rows);\r
-                           deselect(term);\r
-                       }\r
-                       break;\r
-                     case 'X':     /* ECH: write N spaces w/o moving cursor */\r
-                       /* XXX VTTEST says this is vt220, vt510 manual\r
-                        * says vt100 */\r
-                       compatibility(ANSIMIN);\r
-                       {\r
-                           int n = def(term->esc_args[0], 1);\r
-                           pos cursplus;\r
-                           int p = term->curs.x;\r
-                           termline *cline = scrlineptr(term->curs.y);\r
-\r
-                           if (n > term->cols - term->curs.x)\r
-                               n = term->cols - term->curs.x;\r
-                           cursplus = term->curs;\r
-                           cursplus.x += n;\r
-                           check_boundary(term, term->curs.x, term->curs.y);\r
-                           check_boundary(term, term->curs.x+n, term->curs.y);\r
-                           check_selection(term, term->curs, cursplus);\r
-                           while (n--)\r
-                               copy_termchar(cline, p++,\r
-                                             &term->erase_char);\r
-                           seen_disp_event(term);\r
-                       }\r
-                       break;\r
-                     case 'x':       /* DECREQTPARM: report terminal characteristics */\r
-                       compatibility(VT100);\r
-                       if (term->ldisc) {\r
-                           char buf[32];\r
-                           int i = def(term->esc_args[0], 0);\r
-                           if (i == 0 || i == 1) {\r
-                               strcpy(buf, "\033[2;1;1;112;112;1;0x");\r
-                               buf[2] += i;\r
-                               ldisc_send(term->ldisc, buf, 20, 0);\r
-                           }\r
-                       }\r
-                       break;\r
-                     case 'Z':         /* CBT */\r
-                       compatibility(OTHER);\r
-                       {\r
-                           int i = def(term->esc_args[0], 1);\r
-                           pos old_curs = term->curs;\r
-\r
-                           for(;i>0 && term->curs.x>0; i--) {\r
-                               do {\r
-                                   term->curs.x--;\r
-                               } while (term->curs.x >0 &&\r
-                                        !term->tabs[term->curs.x]);\r
-                           }\r
-                           check_selection(term, old_curs, term->curs);\r
-                       }\r
-                       break;\r
-                     case ANSI('c', '='):      /* Hide or Show Cursor */\r
-                       compatibility(SCOANSI);\r
-                       switch(term->esc_args[0]) {\r
-                         case 0:  /* hide cursor */\r
-                           term->cursor_on = FALSE;\r
-                           break;\r
-                         case 1:  /* restore cursor */\r
-                           term->big_cursor = FALSE;\r
-                           term->cursor_on = TRUE;\r
-                           break;\r
-                         case 2:  /* block cursor */\r
-                           term->big_cursor = TRUE;\r
-                           term->cursor_on = TRUE;\r
-                           break;\r
-                       }\r
-                       break;\r
-                     case ANSI('C', '='):\r
-                       /*\r
-                        * set cursor start on scanline esc_args[0] and\r
-                        * end on scanline esc_args[1].If you set\r
-                        * the bottom scan line to a value less than\r
-                        * the top scan line, the cursor will disappear.\r
-                        */\r
-                       compatibility(SCOANSI);\r
-                       if (term->esc_nargs >= 2) {\r
-                           if (term->esc_args[0] > term->esc_args[1])\r
-                               term->cursor_on = FALSE;\r
-                           else\r
-                               term->cursor_on = TRUE;\r
-                       }\r
-                       break;\r
-                     case ANSI('D', '='):\r
-                       compatibility(SCOANSI);\r
-                       term->blink_is_real = FALSE;\r
-                       term_schedule_tblink(term);\r
-                       if (term->esc_args[0]>=1)\r
-                           term->curr_attr |= ATTR_BLINK;\r
-                       else\r
-                           term->curr_attr &= ~ATTR_BLINK;\r
-                       break;\r
-                     case ANSI('E', '='):\r
-                       compatibility(SCOANSI);\r
-                       term->blink_is_real = (term->esc_args[0] >= 1);\r
-                       term_schedule_tblink(term);\r
-                       break;\r
-                     case ANSI('F', '='):      /* set normal foreground */\r
-                       compatibility(SCOANSI);\r
-                       if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {\r
-                           long colour =\r
-                               (sco2ansicolour[term->esc_args[0] & 0x7] |\r
-                                (term->esc_args[0] & 0x8)) <<\r
-                               ATTR_FGSHIFT;\r
-                           term->curr_attr &= ~ATTR_FGMASK;\r
-                           term->curr_attr |= colour;\r
-                           term->default_attr &= ~ATTR_FGMASK;\r
-                           term->default_attr |= colour;\r
-                           set_erase_char(term);\r
-                       }\r
-                       break;\r
-                     case ANSI('G', '='):      /* set normal background */\r
-                       compatibility(SCOANSI);\r
-                       if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {\r
-                           long colour =\r
-                               (sco2ansicolour[term->esc_args[0] & 0x7] |\r
-                                (term->esc_args[0] & 0x8)) <<\r
-                               ATTR_BGSHIFT;\r
-                           term->curr_attr &= ~ATTR_BGMASK;\r
-                           term->curr_attr |= colour;\r
-                           term->default_attr &= ~ATTR_BGMASK;\r
-                           term->default_attr |= colour;\r
-                           set_erase_char(term);\r
-                       }\r
-                       break;\r
-                     case ANSI('L', '='):\r
-                       compatibility(SCOANSI);\r
-                       term->use_bce = (term->esc_args[0] <= 0);\r
-                       set_erase_char(term);\r
-                       break;\r
-                     case ANSI('p', '"'): /* DECSCL: set compat level */\r
-                       /*\r
-                        * Allow the host to make this emulator a\r
-                        * 'perfect' VT102. This first appeared in\r
-                        * the VT220, but we do need to get back to\r
-                        * PuTTY mode so I won't check it.\r
-                        *\r
-                        * The arg in 40..42,50 are a PuTTY extension.\r
-                        * The 2nd arg, 8bit vs 7bit is not checked.\r
-                        *\r
-                        * Setting VT102 mode should also change\r
-                        * the Fkeys to generate PF* codes as a\r
-                        * real VT102 has no Fkeys. The VT220 does\r
-                        * this, F11..F13 become ESC,BS,LF other\r
-                        * Fkeys send nothing.\r
-                        *\r
-                        * Note ESC c will NOT change this!\r
-                        */\r
-\r
-                       switch (term->esc_args[0]) {\r
-                         case 61:\r
-                           term->compatibility_level &= ~TM_VTXXX;\r
-                           term->compatibility_level |= TM_VT102;\r
-                           break;\r
-                         case 62:\r
-                           term->compatibility_level &= ~TM_VTXXX;\r
-                           term->compatibility_level |= TM_VT220;\r
-                           break;\r
-\r
-                         default:\r
-                           if (term->esc_args[0] > 60 &&\r
-                               term->esc_args[0] < 70)\r
-                               term->compatibility_level |= TM_VTXXX;\r
-                           break;\r
-\r
-                         case 40:\r
-                           term->compatibility_level &= TM_VTXXX;\r
-                           break;\r
-                         case 41:\r
-                           term->compatibility_level = TM_PUTTY;\r
-                           break;\r
-                         case 42:\r
-                           term->compatibility_level = TM_SCOANSI;\r
-                           break;\r
-\r
-                         case ARG_DEFAULT:\r
-                           term->compatibility_level = TM_PUTTY;\r
-                           break;\r
-                         case 50:\r
-                           break;\r
-                       }\r
-\r
-                       /* Change the response to CSI c */\r
-                       if (term->esc_args[0] == 50) {\r
-                           int i;\r
-                           char lbuf[64];\r
-                           strcpy(term->id_string, "\033[?");\r
-                           for (i = 1; i < term->esc_nargs; i++) {\r
-                               if (i != 1)\r
-                                   strcat(term->id_string, ";");\r
-                               sprintf(lbuf, "%d", term->esc_args[i]);\r
-                               strcat(term->id_string, lbuf);\r
-                           }\r
-                           strcat(term->id_string, "c");\r
-                       }\r
-#if 0\r
-                       /* Is this a good idea ? \r
-                        * Well we should do a soft reset at this point ...\r
-                        */\r
-                       if (!has_compat(VT420) && has_compat(VT100)) {\r
-                           if (!term->cfg.no_remote_resize) {\r
-                               if (term->reset_132)\r
-                                   request_resize(132, 24);\r
-                               else\r
-                                   request_resize(80, 24);\r
-                           }\r
-                       }\r
-#endif\r
-                       break;\r
-                   }\r
-               break;\r
-             case SEEN_OSC:\r
-               term->osc_w = FALSE;\r
-               switch (c) {\r
-                 case 'P':            /* Linux palette sequence */\r
-                   term->termstate = SEEN_OSC_P;\r
-                   term->osc_strlen = 0;\r
-                   break;\r
-                 case 'R':            /* Linux palette reset */\r
-                   palette_reset(term->frontend);\r
-                   term_invalidate(term);\r
-                   term->termstate = TOPLEVEL;\r
-                   break;\r
-                 case 'W':            /* word-set */\r
-                   term->termstate = SEEN_OSC_W;\r
-                   term->osc_w = TRUE;\r
-                   break;\r
-                 case '0':\r
-                 case '1':\r
-                 case '2':\r
-                 case '3':\r
-                 case '4':\r
-                 case '5':\r
-                 case '6':\r
-                 case '7':\r
-                 case '8':\r
-                 case '9':\r
-                   term->esc_args[0] = 10 * term->esc_args[0] + c - '0';\r
-                   break;\r
-                 case 'L':\r
-                   /*\r
-                    * Grotty hack to support xterm and DECterm title\r
-                    * sequences concurrently.\r
-                    */\r
-                   if (term->esc_args[0] == 2) {\r
-                       term->esc_args[0] = 1;\r
-                       break;\r
-                   }\r
-                   /* else fall through */\r
-                 default:\r
-                   term->termstate = OSC_STRING;\r
-                   term->osc_strlen = 0;\r
-               }\r
-               break;\r
-             case OSC_STRING:\r
-               /*\r
-                * This OSC stuff is EVIL. It takes just one character to get into\r
-                * sysline mode and it's not initially obvious how to get out.\r
-                * So I've added CR and LF as string aborts.\r
-                * This shouldn't effect compatibility as I believe embedded \r
-                * control characters are supposed to be interpreted (maybe?) \r
-                * and they don't display anything useful anyway.\r
-                *\r
-                * -- RDB\r
-                */\r
-               if (c == '\012' || c == '\015') {\r
-                   term->termstate = TOPLEVEL;\r
-               } else if (c == 0234 || c == '\007') {\r
-                   /*\r
-                    * These characters terminate the string; ST and BEL\r
-                    * terminate the sequence and trigger instant\r
-                    * processing of it, whereas ESC goes back to SEEN_ESC\r
-                    * mode unless it is followed by \, in which case it is\r
-                    * synonymous with ST in the first place.\r
-                    */\r
-                   do_osc(term);\r
-                   term->termstate = TOPLEVEL;\r
-               } else if (c == '\033')\r
-                   term->termstate = OSC_MAYBE_ST;\r
-               else if (term->osc_strlen < OSC_STR_MAX)\r
-                   term->osc_string[term->osc_strlen++] = (char)c;\r
-               break;\r
-             case SEEN_OSC_P:\r
-               {\r
-                   int max = (term->osc_strlen == 0 ? 21 : 15);\r
-                   int val;\r
-                   if ((int)c >= '0' && (int)c <= '9')\r
-                       val = c - '0';\r
-                   else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)\r
-                       val = c - 'A' + 10;\r
-                   else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)\r
-                       val = c - 'a' + 10;\r
-                   else {\r
-                       term->termstate = TOPLEVEL;\r
-                       break;\r
-                   }\r
-                   term->osc_string[term->osc_strlen++] = val;\r
-                   if (term->osc_strlen >= 7) {\r
-                       palette_set(term->frontend, term->osc_string[0],\r
-                                   term->osc_string[1] * 16 + term->osc_string[2],\r
-                                   term->osc_string[3] * 16 + term->osc_string[4],\r
-                                   term->osc_string[5] * 16 + term->osc_string[6]);\r
-                       term_invalidate(term);\r
-                       term->termstate = TOPLEVEL;\r
-                   }\r
-               }\r
-               break;\r
-             case SEEN_OSC_W:\r
-               switch (c) {\r
-                 case '0':\r
-                 case '1':\r
-                 case '2':\r
-                 case '3':\r
-                 case '4':\r
-                 case '5':\r
-                 case '6':\r
-                 case '7':\r
-                 case '8':\r
-                 case '9':\r
-                   term->esc_args[0] = 10 * term->esc_args[0] + c - '0';\r
-                   break;\r
-                 default:\r
-                   term->termstate = OSC_STRING;\r
-                   term->osc_strlen = 0;\r
-               }\r
-               break;\r
-             case VT52_ESC:\r
-               term->termstate = TOPLEVEL;\r
-               seen_disp_event(term);\r
-               switch (c) {\r
-                 case 'A':\r
-                   move(term, term->curs.x, term->curs.y - 1, 1);\r
-                   break;\r
-                 case 'B':\r
-                   move(term, term->curs.x, term->curs.y + 1, 1);\r
-                   break;\r
-                 case 'C':\r
-                   move(term, term->curs.x + 1, term->curs.y, 1);\r
-                   break;\r
-                 case 'D':\r
-                   move(term, term->curs.x - 1, term->curs.y, 1);\r
-                   break;\r
-                   /*\r
-                    * From the VT100 Manual\r
-                    * NOTE: The special graphics characters in the VT100\r
-                    *       are different from those in the VT52\r
-                    *\r
-                    * From VT102 manual:\r
-                    *       137 _  Blank             - Same\r
-                    *       140 `  Reserved          - Humm.\r
-                    *       141 a  Solid rectangle   - Similar\r
-                    *       142 b  1/                - Top half of fraction for the\r
-                    *       143 c  3/                - subscript numbers below.\r
-                    *       144 d  5/\r
-                    *       145 e  7/\r
-                    *       146 f  Degrees           - Same\r
-                    *       147 g  Plus or minus     - Same\r
-                    *       150 h  Right arrow\r
-                    *       151 i  Ellipsis (dots)\r
-                    *       152 j  Divide by\r
-                    *       153 k  Down arrow\r
-                    *       154 l  Bar at scan 0\r
-                    *       155 m  Bar at scan 1\r
-                    *       156 n  Bar at scan 2\r
-                    *       157 o  Bar at scan 3     - Similar\r
-                    *       160 p  Bar at scan 4     - Similar\r
-                    *       161 q  Bar at scan 5     - Similar\r
-                    *       162 r  Bar at scan 6     - Same\r
-                    *       163 s  Bar at scan 7     - Similar\r
-                    *       164 t  Subscript 0\r
-                    *       165 u  Subscript 1\r
-                    *       166 v  Subscript 2\r
-                    *       167 w  Subscript 3\r
-                    *       170 x  Subscript 4\r
-                    *       171 y  Subscript 5\r
-                    *       172 z  Subscript 6\r
-                    *       173 {  Subscript 7\r
-                    *       174 |  Subscript 8\r
-                    *       175 }  Subscript 9\r
-                    *       176 ~  Paragraph\r
-                    *\r
-                    */\r
-                 case 'F':\r
-                   term->cset_attr[term->cset = 0] = CSET_LINEDRW;\r
-                   break;\r
-                 case 'G':\r
-                   term->cset_attr[term->cset = 0] = CSET_ASCII;\r
-                   break;\r
-                 case 'H':\r
-                   move(term, 0, 0, 0);\r
-                   break;\r
-                 case 'I':\r
-                   if (term->curs.y == 0)\r
-                       scroll(term, 0, term->rows - 1, -1, TRUE);\r
-                   else if (term->curs.y > 0)\r
-                       term->curs.y--;\r
-                   term->wrapnext = FALSE;\r
-                   break;\r
-                 case 'J':\r
-                   erase_lots(term, FALSE, FALSE, TRUE);\r
-                   term->disptop = 0;\r
-                   break;\r
-                 case 'K':\r
-                   erase_lots(term, TRUE, FALSE, TRUE);\r
-                   break;\r
-#if 0\r
-                 case 'V':\r
-                   /* XXX Print cursor line */\r
-                   break;\r
-                 case 'W':\r
-                   /* XXX Start controller mode */\r
-                   break;\r
-                 case 'X':\r
-                   /* XXX Stop controller mode */\r
-                   break;\r
-#endif\r
-                 case 'Y':\r
-                   term->termstate = VT52_Y1;\r
-                   break;\r
-                 case 'Z':\r
-                   if (term->ldisc)\r
-                       ldisc_send(term->ldisc, "\033/Z", 3, 0);\r
-                   break;\r
-                 case '=':\r
-                   term->app_keypad_keys = TRUE;\r
-                   break;\r
-                 case '>':\r
-                   term->app_keypad_keys = FALSE;\r
-                   break;\r
-                 case '<':\r
-                   /* XXX This should switch to VT100 mode not current or default\r
-                    *     VT mode. But this will only have effect in a VT220+\r
-                    *     emulation.\r
-                    */\r
-                   term->vt52_mode = FALSE;\r
-                   term->blink_is_real = term->cfg.blinktext;\r
-                   term_schedule_tblink(term);\r
-                   break;\r
-#if 0\r
-                 case '^':\r
-                   /* XXX Enter auto print mode */\r
-                   break;\r
-                 case '_':\r
-                   /* XXX Exit auto print mode */\r
-                   break;\r
-                 case ']':\r
-                   /* XXX Print screen */\r
-                   break;\r
-#endif\r
-\r
-#ifdef VT52_PLUS\r
-                 case 'E':\r
-                   /* compatibility(ATARI) */\r
-                   move(term, 0, 0, 0);\r
-                   erase_lots(term, FALSE, FALSE, TRUE);\r
-                   term->disptop = 0;\r
-                   break;\r
-                 case 'L':\r
-                   /* compatibility(ATARI) */\r
-                   if (term->curs.y <= term->marg_b)\r
-                       scroll(term, term->curs.y, term->marg_b, -1, FALSE);\r
-                   break;\r
-                 case 'M':\r
-                   /* compatibility(ATARI) */\r
-                   if (term->curs.y <= term->marg_b)\r
-                       scroll(term, term->curs.y, term->marg_b, 1, TRUE);\r
-                   break;\r
-                 case 'b':\r
-                   /* compatibility(ATARI) */\r
-                   term->termstate = VT52_FG;\r
-                   break;\r
-                 case 'c':\r
-                   /* compatibility(ATARI) */\r
-                   term->termstate = VT52_BG;\r
-                   break;\r
-                 case 'd':\r
-                   /* compatibility(ATARI) */\r
-                   erase_lots(term, FALSE, TRUE, FALSE);\r
-                   term->disptop = 0;\r
-                   break;\r
-                 case 'e':\r
-                   /* compatibility(ATARI) */\r
-                   term->cursor_on = TRUE;\r
-                   break;\r
-                 case 'f':\r
-                   /* compatibility(ATARI) */\r
-                   term->cursor_on = FALSE;\r
-                   break;\r
-                   /* case 'j': Save cursor position - broken on ST */\r
-                   /* case 'k': Restore cursor position */\r
-                 case 'l':\r
-                   /* compatibility(ATARI) */\r
-                   erase_lots(term, TRUE, TRUE, TRUE);\r
-                   term->curs.x = 0;\r
-                   term->wrapnext = FALSE;\r
-                   break;\r
-                 case 'o':\r
-                   /* compatibility(ATARI) */\r
-                   erase_lots(term, TRUE, TRUE, FALSE);\r
-                   break;\r
-                 case 'p':\r
-                   /* compatibility(ATARI) */\r
-                   term->curr_attr |= ATTR_REVERSE;\r
-                   break;\r
-                 case 'q':\r
-                   /* compatibility(ATARI) */\r
-                   term->curr_attr &= ~ATTR_REVERSE;\r
-                   break;\r
-                 case 'v':            /* wrap Autowrap on - Wyse style */\r
-                   /* compatibility(ATARI) */\r
-                   term->wrap = 1;\r
-                   break;\r
-                 case 'w':            /* Autowrap off */\r
-                   /* compatibility(ATARI) */\r
-                   term->wrap = 0;\r
-                   break;\r
-\r
-                 case 'R':\r
-                   /* compatibility(OTHER) */\r
-                   term->vt52_bold = FALSE;\r
-                   term->curr_attr = ATTR_DEFAULT;\r
-                   set_erase_char(term);\r
-                   break;\r
-                 case 'S':\r
-                   /* compatibility(VI50) */\r
-                   term->curr_attr |= ATTR_UNDER;\r
-                   break;\r
-                 case 'W':\r
-                   /* compatibility(VI50) */\r
-                   term->curr_attr &= ~ATTR_UNDER;\r
-                   break;\r
-                 case 'U':\r
-                   /* compatibility(VI50) */\r
-                   term->vt52_bold = TRUE;\r
-                   term->curr_attr |= ATTR_BOLD;\r
-                   break;\r
-                 case 'T':\r
-                   /* compatibility(VI50) */\r
-                   term->vt52_bold = FALSE;\r
-                   term->curr_attr &= ~ATTR_BOLD;\r
-                   break;\r
-#endif\r
-               }\r
-               break;\r
-             case VT52_Y1:\r
-               term->termstate = VT52_Y2;\r
-               move(term, term->curs.x, c - ' ', 0);\r
-               break;\r
-             case VT52_Y2:\r
-               term->termstate = TOPLEVEL;\r
-               move(term, c - ' ', term->curs.y, 0);\r
-               break;\r
-\r
-#ifdef VT52_PLUS\r
-             case VT52_FG:\r
-               term->termstate = TOPLEVEL;\r
-               term->curr_attr &= ~ATTR_FGMASK;\r
-               term->curr_attr &= ~ATTR_BOLD;\r
-               term->curr_attr |= (c & 0xF) << ATTR_FGSHIFT;\r
-               set_erase_char(term);\r
-               break;\r
-             case VT52_BG:\r
-               term->termstate = TOPLEVEL;\r
-               term->curr_attr &= ~ATTR_BGMASK;\r
-               term->curr_attr &= ~ATTR_BLINK;\r
-               term->curr_attr |= (c & 0xF) << ATTR_BGSHIFT;\r
-               set_erase_char(term);\r
-               break;\r
-#endif\r
-             default: break;          /* placate gcc warning about enum use */\r
-           }\r
-       if (term->selstate != NO_SELECTION) {\r
-           pos cursplus = term->curs;\r
-           incpos(cursplus);\r
-           check_selection(term, term->curs, cursplus);\r
-       }\r
-    }\r
-\r
-    term_print_flush(term);\r
-    if (term->cfg.logflush)\r
-       logflush(term->logctx);\r
-}\r
-\r
-/*\r
- * To prevent having to run the reasonably tricky bidi algorithm\r
- * too many times, we maintain a cache of the last lineful of data\r
- * fed to the algorithm on each line of the display.\r
- */\r
-static int term_bidi_cache_hit(Terminal *term, int line,\r
-                              termchar *lbefore, int width)\r
-{\r
-    int i;\r
-\r
-    if (!term->pre_bidi_cache)\r
-       return FALSE;                  /* cache doesn't even exist yet! */\r
-\r
-    if (line >= term->bidi_cache_size)\r
-       return FALSE;                  /* cache doesn't have this many lines */\r
-\r
-    if (!term->pre_bidi_cache[line].chars)\r
-       return FALSE;                  /* cache doesn't contain _this_ line */\r
-\r
-    if (term->pre_bidi_cache[line].width != width)\r
-       return FALSE;                  /* line is wrong width */\r
-\r
-    for (i = 0; i < width; i++)\r
-       if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i))\r
-           return FALSE;              /* line doesn't match cache */\r
-\r
-    return TRUE;                      /* it didn't match. */\r
-}\r
-\r
-static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,\r
-                                 termchar *lafter, bidi_char *wcTo,\r
-                                 int width, int size)\r
-{\r
-    int i;\r
-\r
-    if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {\r
-       int j = term->bidi_cache_size;\r
-       term->bidi_cache_size = line+1;\r
-       term->pre_bidi_cache = sresize(term->pre_bidi_cache,\r
-                                      term->bidi_cache_size,\r
-                                      struct bidi_cache_entry);\r
-       term->post_bidi_cache = sresize(term->post_bidi_cache,\r
-                                       term->bidi_cache_size,\r
-                                       struct bidi_cache_entry);\r
-       while (j < term->bidi_cache_size) {\r
-           term->pre_bidi_cache[j].chars =\r
-               term->post_bidi_cache[j].chars = NULL;\r
-           term->pre_bidi_cache[j].width =\r
-               term->post_bidi_cache[j].width = -1;\r
-           term->pre_bidi_cache[j].forward =\r
-               term->post_bidi_cache[j].forward = NULL;\r
-           term->pre_bidi_cache[j].backward =\r
-               term->post_bidi_cache[j].backward = NULL;\r
-           j++;\r
-       }\r
-    }\r
-\r
-    sfree(term->pre_bidi_cache[line].chars);\r
-    sfree(term->post_bidi_cache[line].chars);\r
-    sfree(term->post_bidi_cache[line].forward);\r
-    sfree(term->post_bidi_cache[line].backward);\r
-\r
-    term->pre_bidi_cache[line].width = width;\r
-    term->pre_bidi_cache[line].chars = snewn(size, termchar);\r
-    term->post_bidi_cache[line].width = width;\r
-    term->post_bidi_cache[line].chars = snewn(size, termchar);\r
-    term->post_bidi_cache[line].forward = snewn(width, int);\r
-    term->post_bidi_cache[line].backward = snewn(width, int);\r
-\r
-    memcpy(term->pre_bidi_cache[line].chars, lbefore, size * TSIZE);\r
-    memcpy(term->post_bidi_cache[line].chars, lafter, size * TSIZE);\r
-    memset(term->post_bidi_cache[line].forward, 0, width * sizeof(int));\r
-    memset(term->post_bidi_cache[line].backward, 0, width * sizeof(int));\r
-\r
-    for (i = 0; i < width; i++) {\r
-       int p = wcTo[i].index;\r
-\r
-       assert(0 <= p && p < width);\r
-\r
-       term->post_bidi_cache[line].backward[i] = p;\r
-       term->post_bidi_cache[line].forward[p] = i;\r
-    }\r
-}\r
-\r
-/*\r
- * Prepare the bidi information for a screen line. Returns the\r
- * transformed list of termchars, or NULL if no transformation at\r
- * all took place (because bidi is disabled). If return was\r
- * non-NULL, auxiliary information such as the forward and reverse\r
- * mappings of permutation position are available in\r
- * term->post_bidi_cache[scr_y].*.\r
- */\r
-static termchar *term_bidi_line(Terminal *term, struct termline *ldata,\r
-                               int scr_y)\r
-{\r
-    termchar *lchars;\r
-    int it;\r
-\r
-    /* Do Arabic shaping and bidi. */\r
-    if(!term->cfg.bidi || !term->cfg.arabicshaping) {\r
-\r
-       if (!term_bidi_cache_hit(term, scr_y, ldata->chars, term->cols)) {\r
-\r
-           if (term->wcFromTo_size < term->cols) {\r
-               term->wcFromTo_size = term->cols;\r
-               term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,\r
-                                      bidi_char);\r
-               term->wcTo = sresize(term->wcTo, term->wcFromTo_size,\r
-                                    bidi_char);\r
-           }\r
-\r
-           for(it=0; it<term->cols ; it++)\r
-           {\r
-               unsigned long uc = (ldata->chars[it].chr);\r
-\r
-               switch (uc & CSET_MASK) {\r
-                 case CSET_LINEDRW:\r
-                   if (!term->cfg.rawcnp) {\r
-                       uc = term->ucsdata->unitab_xterm[uc & 0xFF];\r
-                       break;\r
-                   }\r
-                 case CSET_ASCII:\r
-                   uc = term->ucsdata->unitab_line[uc & 0xFF];\r
-                   break;\r
-                 case CSET_SCOACS:\r
-                   uc = term->ucsdata->unitab_scoacs[uc&0xFF];\r
-                   break;\r
-               }\r
-               switch (uc & CSET_MASK) {\r
-                 case CSET_ACP:\r
-                   uc = term->ucsdata->unitab_font[uc & 0xFF];\r
-                   break;\r
-                 case CSET_OEMCP:\r
-                   uc = term->ucsdata->unitab_oemcp[uc & 0xFF];\r
-                   break;\r
-               }\r
-\r
-               term->wcFrom[it].origwc = term->wcFrom[it].wc =\r
-                   (wchar_t)uc;\r
-               term->wcFrom[it].index = it;\r
-           }\r
-\r
-           if(!term->cfg.bidi)\r
-               do_bidi(term->wcFrom, term->cols);\r
-\r
-           /* this is saved iff done from inside the shaping */\r
-           if(!term->cfg.bidi && term->cfg.arabicshaping)\r
-               for(it=0; it<term->cols; it++)\r
-                   term->wcTo[it] = term->wcFrom[it];\r
-\r
-           if(!term->cfg.arabicshaping)\r
-               do_shape(term->wcFrom, term->wcTo, term->cols);\r
-\r
-           if (term->ltemp_size < ldata->size) {\r
-               term->ltemp_size = ldata->size;\r
-               term->ltemp = sresize(term->ltemp, term->ltemp_size,\r
-                                     termchar);\r
-           }\r
-\r
-           memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);\r
-\r
-           for(it=0; it<term->cols ; it++)\r
-           {\r
-               term->ltemp[it] = ldata->chars[term->wcTo[it].index];\r
-               if (term->ltemp[it].cc_next)\r
-                   term->ltemp[it].cc_next -=\r
-                   it - term->wcTo[it].index;\r
-\r
-               if (term->wcTo[it].origwc != term->wcTo[it].wc)\r
-                   term->ltemp[it].chr = term->wcTo[it].wc;\r
-           }\r
-           term_bidi_cache_store(term, scr_y, ldata->chars,\r
-                                 term->ltemp, term->wcTo,\r
-                                  term->cols, ldata->size);\r
-\r
-           lchars = term->ltemp;\r
-       } else {\r
-           lchars = term->post_bidi_cache[scr_y].chars;\r
-       }\r
-    } else {\r
-       lchars = NULL;\r
-    }\r
-\r
-    return lchars;\r
-}\r
-\r
-/*\r
- * Given a context, update the window. Out of paranoia, we don't\r
- * allow WM_PAINT responses to do scrolling optimisations.\r
- */\r
-static void do_paint(Terminal *term, Context ctx, int may_optimise)\r
-{\r
-    int i, j, our_curs_y, our_curs_x;\r
-    int rv, cursor;\r
-    pos scrpos;\r
-    wchar_t *ch;\r
-    int chlen;\r
-#ifdef OPTIMISE_SCROLL\r
-    struct scrollregion *sr;\r
-#endif /* OPTIMISE_SCROLL */\r
-    termchar *newline;\r
-\r
-    chlen = 1024;\r
-    ch = snewn(chlen, wchar_t);\r
-\r
-    newline = snewn(term->cols, termchar);\r
-\r
-    rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);\r
-\r
-    /* Depends on:\r
-     * screen array, disptop, scrtop,\r
-     * selection, rv, \r
-     * cfg.blinkpc, blink_is_real, tblinker, \r
-     * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext\r
-     */\r
-\r
-    /* Has the cursor position or type changed ? */\r
-    if (term->cursor_on) {\r
-       if (term->has_focus) {\r
-           if (term->cblinker || !term->cfg.blink_cur)\r
-               cursor = TATTR_ACTCURS;\r
-           else\r
-               cursor = 0;\r
-       } else\r
-           cursor = TATTR_PASCURS;\r
-       if (term->wrapnext)\r
-           cursor |= TATTR_RIGHTCURS;\r
-    } else\r
-       cursor = 0;\r
-    our_curs_y = term->curs.y - term->disptop;\r
-    {\r
-       /*\r
-        * Adjust the cursor position:\r
-        *  - for bidi\r
-        *  - in the case where it's resting on the right-hand half\r
-        *    of a CJK wide character. xterm's behaviour here,\r
-        *    which seems adequate to me, is to display the cursor\r
-        *    covering the _whole_ character, exactly as if it were\r
-        *    one space to the left.\r
-        */\r
-       termline *ldata = lineptr(term->curs.y);\r
-       termchar *lchars;\r
-\r
-       our_curs_x = term->curs.x;\r
-\r
-       if ( (lchars = term_bidi_line(term, ldata, our_curs_y)) != NULL) {\r
-           our_curs_x = term->post_bidi_cache[our_curs_y].forward[our_curs_x];\r
-       } else\r
-           lchars = ldata->chars;\r
-\r
-       if (our_curs_x > 0 &&\r
-           lchars[our_curs_x].chr == UCSWIDE)\r
-           our_curs_x--;\r
-\r
-       unlineptr(ldata);\r
-    }\r
-\r
-    /*\r
-     * If the cursor is not where it was last time we painted, and\r
-     * its previous position is visible on screen, invalidate its\r
-     * previous position.\r
-     */\r
-    if (term->dispcursy >= 0 &&\r
-       (term->curstype != cursor ||\r
-        term->dispcursy != our_curs_y ||\r
-        term->dispcursx != our_curs_x)) {\r
-       termchar *dispcurs = term->disptext[term->dispcursy]->chars +\r
-           term->dispcursx;\r
-\r
-       if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)\r
-           dispcurs[-1].attr |= ATTR_INVALID;\r
-       if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)\r
-           dispcurs[1].attr |= ATTR_INVALID;\r
-       dispcurs->attr |= ATTR_INVALID;\r
-\r
-       term->curstype = 0;\r
-    }\r
-    term->dispcursx = term->dispcursy = -1;\r
-\r
-#ifdef OPTIMISE_SCROLL\r
-    /* Do scrolls */\r
-    sr = term->scrollhead;\r
-    while (sr) {\r
-       struct scrollregion *next = sr->next;\r
-       do_scroll(ctx, sr->topline, sr->botline, sr->lines);\r
-       sfree(sr);\r
-       sr = next;\r
-    }\r
-    term->scrollhead = term->scrolltail = NULL;\r
-#endif /* OPTIMISE_SCROLL */\r
-\r
-    /* The normal screen data */\r
-    for (i = 0; i < term->rows; i++) {\r
-       termline *ldata;\r
-       termchar *lchars;\r
-       int dirty_line, dirty_run, selected;\r
-       unsigned long attr = 0, cset = 0;\r
-       int start = 0;\r
-       int ccount = 0;\r
-       int last_run_dirty = 0;\r
-       int laststart, dirtyrect;\r
-       int *backward;\r
-\r
-       scrpos.y = i + term->disptop;\r
-       ldata = lineptr(scrpos.y);\r
-\r
-       /* Do Arabic shaping and bidi. */\r
-       lchars = term_bidi_line(term, ldata, i);\r
-       if (lchars) {\r
-           backward = term->post_bidi_cache[i].backward;\r
-       } else {\r
-           lchars = ldata->chars;\r
-           backward = NULL;\r
-       }\r
-\r
-       /*\r
-        * First loop: work along the line deciding what we want\r
-        * each character cell to look like.\r
-        */\r
-       for (j = 0; j < term->cols; j++) {\r
-           unsigned long tattr, tchar;\r
-           termchar *d = lchars + j;\r
-           scrpos.x = backward ? backward[j] : j;\r
-\r
-           tchar = d->chr;\r
-           tattr = d->attr;\r
-\r
-            if (!term->cfg.ansi_colour)\r
-                tattr = (tattr & ~(ATTR_FGMASK | ATTR_BGMASK)) | \r
-                ATTR_DEFFG | ATTR_DEFBG;\r
-\r
-           if (!term->cfg.xterm_256_colour) {\r
-               int colour;\r
-               colour = (tattr & ATTR_FGMASK) >> ATTR_FGSHIFT;\r
-               if (colour >= 16 && colour < 256)\r
-                   tattr = (tattr &~ ATTR_FGMASK) | ATTR_DEFFG;\r
-               colour = (tattr & ATTR_BGMASK) >> ATTR_BGSHIFT;\r
-               if (colour >= 16 && colour < 256)\r
-                   tattr = (tattr &~ ATTR_BGMASK) | ATTR_DEFBG;\r
-           }\r
-\r
-           switch (tchar & CSET_MASK) {\r
-             case CSET_ASCII:\r
-               tchar = term->ucsdata->unitab_line[tchar & 0xFF];\r
-               break;\r
-             case CSET_LINEDRW:\r
-               tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];\r
-               break;\r
-             case CSET_SCOACS:  \r
-               tchar = term->ucsdata->unitab_scoacs[tchar&0xFF]; \r
-               break;\r
-           }\r
-           if (j < term->cols-1 && d[1].chr == UCSWIDE)\r
-               tattr |= ATTR_WIDE;\r
-\r
-           /* Video reversing things */\r
-           if (term->selstate == DRAGGING || term->selstate == SELECTED) {\r
-               if (term->seltype == LEXICOGRAPHIC)\r
-                   selected = (posle(term->selstart, scrpos) &&\r
-                               poslt(scrpos, term->selend));\r
-               else\r
-                   selected = (posPle(term->selstart, scrpos) &&\r
-                               posPlt(scrpos, term->selend));\r
-           } else\r
-               selected = FALSE;\r
-           tattr = (tattr ^ rv\r
-                    ^ (selected ? ATTR_REVERSE : 0));\r
-\r
-           /* 'Real' blinking ? */\r
-           if (term->blink_is_real && (tattr & ATTR_BLINK)) {\r
-               if (term->has_focus && term->tblinker) {\r
-                   tchar = term->ucsdata->unitab_line[(unsigned char)' '];\r
-               }\r
-               tattr &= ~ATTR_BLINK;\r
-           }\r
-\r
-           /*\r
-            * Check the font we'll _probably_ be using to see if \r
-            * the character is wide when we don't want it to be.\r
-            */\r
-           if (tchar != term->disptext[i]->chars[j].chr ||\r
-               tattr != (term->disptext[i]->chars[j].attr &~\r
-                         (ATTR_NARROW | DATTR_MASK))) {\r
-               if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)\r
-                   tattr |= ATTR_NARROW;\r
-           } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)\r
-               tattr |= ATTR_NARROW;\r
-\r
-           if (i == our_curs_y && j == our_curs_x) {\r
-               tattr |= cursor;\r
-               term->curstype = cursor;\r
-               term->dispcursx = j;\r
-               term->dispcursy = i;\r
-           }\r
-\r
-           /* FULL-TERMCHAR */\r
-           newline[j].attr = tattr;\r
-           newline[j].chr = tchar;\r
-           /* Combining characters are still read from lchars */\r
-           newline[j].cc_next = 0;\r
-       }\r
-\r
-       /*\r
-        * Now loop over the line again, noting where things have\r
-        * changed.\r
-        * \r
-        * During this loop, we keep track of where we last saw\r
-        * DATTR_STARTRUN. Any mismatch automatically invalidates\r
-        * _all_ of the containing run that was last printed: that\r
-        * is, any rectangle that was drawn in one go in the\r
-        * previous update should be either left completely alone\r
-        * or overwritten in its entirety. This, along with the\r
-        * expectation that front ends clip all text runs to their\r
-        * bounding rectangle, should solve any possible problems\r
-        * with fonts that overflow their character cells.\r
-        */\r
-       laststart = 0;\r
-       dirtyrect = FALSE;\r
-       for (j = 0; j < term->cols; j++) {\r
-           if (term->disptext[i]->chars[j].attr & DATTR_STARTRUN) {\r
-               laststart = j;\r
-               dirtyrect = FALSE;\r
-           }\r
-\r
-           if (term->disptext[i]->chars[j].chr != newline[j].chr ||\r
-               (term->disptext[i]->chars[j].attr &~ DATTR_MASK)\r
-               != newline[j].attr) {\r
-               int k;\r
-\r
-               if (!dirtyrect) {\r
-                   for (k = laststart; k < j; k++)\r
-                       term->disptext[i]->chars[k].attr |= ATTR_INVALID;\r
-\r
-                   dirtyrect = TRUE;\r
-               }\r
-           }\r
-\r
-           if (dirtyrect)\r
-               term->disptext[i]->chars[j].attr |= ATTR_INVALID;\r
-       }\r
-\r
-       /*\r
-        * Finally, loop once more and actually do the drawing.\r
-        */\r
-       dirty_run = dirty_line = (ldata->lattr !=\r
-                                 term->disptext[i]->lattr);\r
-       term->disptext[i]->lattr = ldata->lattr;\r
-\r
-       for (j = 0; j < term->cols; j++) {\r
-           unsigned long tattr, tchar;\r
-           int break_run, do_copy;\r
-           termchar *d = lchars + j;\r
-\r
-           tattr = newline[j].attr;\r
-           tchar = newline[j].chr;\r
-\r
-           if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)\r
-               dirty_line = TRUE;\r
-\r
-           break_run = ((tattr ^ attr) & term->attr_mask) != 0;\r
-\r
-           /* Special hack for VT100 Linedraw glyphs */\r
-           if (tchar >= 0x23BA && tchar <= 0x23BD)\r
-               break_run = TRUE;\r
-\r
-           /*\r
-            * Separate out sequences of characters that have the\r
-            * same CSET, if that CSET is a magic one.\r
-            */\r
-           if (CSET_OF(tchar) != cset)\r
-               break_run = TRUE;\r
-\r
-           /*\r
-            * Break on both sides of any combined-character cell.\r
-            */\r
-           if (d->cc_next != 0 ||\r
-               (j > 0 && d[-1].cc_next != 0))\r
-               break_run = TRUE;\r
-\r
-           if (!term->ucsdata->dbcs_screenfont && !dirty_line) {\r
-               if (term->disptext[i]->chars[j].chr == tchar &&\r
-                   (term->disptext[i]->chars[j].attr &~ DATTR_MASK) == tattr)\r
-                   break_run = TRUE;\r
-               else if (!dirty_run && ccount == 1)\r
-                   break_run = TRUE;\r
-           }\r
-\r
-           if (break_run) {\r
-               if ((dirty_run || last_run_dirty) && ccount > 0) {\r
-                   do_text(ctx, start, i, ch, ccount, attr,\r
-                           ldata->lattr);\r
-                   if (attr & (TATTR_ACTCURS | TATTR_PASCURS))\r
-                       do_cursor(ctx, start, i, ch, ccount, attr,\r
-                                 ldata->lattr);\r
-               }\r
-               start = j;\r
-               ccount = 0;\r
-               attr = tattr;\r
-               cset = CSET_OF(tchar);\r
-               if (term->ucsdata->dbcs_screenfont)\r
-                   last_run_dirty = dirty_run;\r
-               dirty_run = dirty_line;\r
-           }\r
-\r
-           do_copy = FALSE;\r
-           if (!termchars_equal_override(&term->disptext[i]->chars[j],\r
-                                         d, tchar, tattr)) {\r
-               do_copy = TRUE;\r
-               dirty_run = TRUE;\r
-           }\r
-\r
-           if (ccount >= chlen) {\r
-               chlen = ccount + 256;\r
-               ch = sresize(ch, chlen, wchar_t);\r
-           }\r
-           ch[ccount++] = (wchar_t) tchar;\r
-\r
-           if (d->cc_next) {\r
-               termchar *dd = d;\r
-\r
-               while (dd->cc_next) {\r
-                   unsigned long schar;\r
-\r
-                   dd += dd->cc_next;\r
-\r
-                   schar = dd->chr;\r
-                   switch (schar & CSET_MASK) {\r
-                     case CSET_ASCII:\r
-                       schar = term->ucsdata->unitab_line[schar & 0xFF];\r
-                       break;\r
-                     case CSET_LINEDRW:\r
-                       schar = term->ucsdata->unitab_xterm[schar & 0xFF];\r
-                       break;\r
-                     case CSET_SCOACS:\r
-                       schar = term->ucsdata->unitab_scoacs[schar&0xFF];\r
-                       break;\r
-                   }\r
-\r
-                   if (ccount >= chlen) {\r
-                       chlen = ccount + 256;\r
-                       ch = sresize(ch, chlen, wchar_t);\r
-                   }\r
-                   ch[ccount++] = (wchar_t) schar;\r
-               }\r
-\r
-               attr |= TATTR_COMBINING;\r
-           }\r
-\r
-           if (do_copy) {\r
-               copy_termchar(term->disptext[i], j, d);\r
-               term->disptext[i]->chars[j].chr = tchar;\r
-               term->disptext[i]->chars[j].attr = tattr;\r
-               if (start == j)\r
-                   term->disptext[i]->chars[j].attr |= DATTR_STARTRUN;\r
-           }\r
-\r
-           /* If it's a wide char step along to the next one. */\r
-           if (tattr & ATTR_WIDE) {\r
-               if (++j < term->cols) {\r
-                   d++;\r
-                   /*\r
-                    * By construction above, the cursor should not\r
-                    * be on the right-hand half of this character.\r
-                    * Ever.\r
-                    */\r
-                   assert(!(i == our_curs_y && j == our_curs_x));\r
-                   if (!termchars_equal(&term->disptext[i]->chars[j], d))\r
-                       dirty_run = TRUE;\r
-                   copy_termchar(term->disptext[i], j, d);\r
-               }\r
-           }\r
-       }\r
-       if (dirty_run && ccount > 0) {\r
-           do_text(ctx, start, i, ch, ccount, attr,\r
-                   ldata->lattr);\r
-           if (attr & (TATTR_ACTCURS | TATTR_PASCURS))\r
-               do_cursor(ctx, start, i, ch, ccount, attr,\r
-                         ldata->lattr);\r
-       }\r
-\r
-       unlineptr(ldata);\r
-    }\r
-\r
-    sfree(newline);\r
-    sfree(ch);\r
-}\r
-\r
-/*\r
- * Invalidate the whole screen so it will be repainted in full.\r
- */\r
-void term_invalidate(Terminal *term)\r
-{\r
-    int i, j;\r
-\r
-    for (i = 0; i < term->rows; i++)\r
-       for (j = 0; j < term->cols; j++)\r
-           term->disptext[i]->chars[j].attr |= ATTR_INVALID;\r
-\r
-    term_schedule_update(term);\r
-}\r
-\r
-/*\r
- * Paint the window in response to a WM_PAINT message.\r
- */\r
-void term_paint(Terminal *term, Context ctx,\r
-               int left, int top, int right, int bottom, int immediately)\r
-{\r
-    int i, j;\r
-    if (left < 0) left = 0;\r
-    if (top < 0) top = 0;\r
-    if (right >= term->cols) right = term->cols-1;\r
-    if (bottom >= term->rows) bottom = term->rows-1;\r
-\r
-    for (i = top; i <= bottom && i < term->rows; i++) {\r
-       if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)\r
-           for (j = left; j <= right && j < term->cols; j++)\r
-               term->disptext[i]->chars[j].attr |= ATTR_INVALID;\r
-       else\r
-           for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)\r
-               term->disptext[i]->chars[j].attr |= ATTR_INVALID;\r
-    }\r
-\r
-    if (immediately) {\r
-        do_paint (term, ctx, FALSE);\r
-    } else {\r
-       term_schedule_update(term);\r
-    }\r
-}\r
-\r
-/*\r
- * Attempt to scroll the scrollback. The second parameter gives the\r
- * position we want to scroll to; the first is +1 to denote that\r
- * this position is relative to the beginning of the scrollback, -1\r
- * to denote it is relative to the end, and 0 to denote that it is\r
- * relative to the current position.\r
- */\r
-void term_scroll(Terminal *term, int rel, int where)\r
-{\r
-    int sbtop = -sblines(term);\r
-#ifdef OPTIMISE_SCROLL\r
-    int olddisptop = term->disptop;\r
-    int shift;\r
-#endif /* OPTIMISE_SCROLL */\r
-\r
-    term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;\r
-    if (term->disptop < sbtop)\r
-       term->disptop = sbtop;\r
-    if (term->disptop > 0)\r
-       term->disptop = 0;\r
-    update_sbar(term);\r
-#ifdef OPTIMISE_SCROLL\r
-    shift = (term->disptop - olddisptop);\r
-    if (shift < term->rows && shift > -term->rows)\r
-       scroll_display(term, 0, term->rows - 1, shift);\r
-#endif /* OPTIMISE_SCROLL */\r
-    term_update(term);\r
-}\r
-\r
-/*\r
- * Scroll the scrollback to centre it on the beginning or end of the\r
- * current selection, if any.\r
- */\r
-void term_scroll_to_selection(Terminal *term, int which_end)\r
-{\r
-    pos target;\r
-    int y;\r
-    int sbtop = -sblines(term);\r
-\r
-    if (term->selstate != SELECTED)\r
-       return;\r
-    if (which_end)\r
-       target = term->selend;\r
-    else\r
-       target = term->selstart;\r
-\r
-    y = target.y - term->rows/2;\r
-    if (y < sbtop)\r
-       y = sbtop;\r
-    else if (y > 0)\r
-       y = 0;\r
-    term_scroll(term, -1, y);\r
-}\r
-\r
-/*\r
- * Helper routine for clipme(): growing buffer.\r
- */\r
-typedef struct {\r
-    int buflen;                    /* amount of allocated space in textbuf/attrbuf */\r
-    int bufpos;                    /* amount of actual data */\r
-    wchar_t *textbuf;      /* buffer for copied text */\r
-    wchar_t *textptr;      /* = textbuf + bufpos (current insertion point) */\r
-    int *attrbuf;          /* buffer for copied attributes */\r
-    int *attrptr;          /* = attrbuf + bufpos */\r
-} clip_workbuf;\r
-\r
-static void clip_addchar(clip_workbuf *b, wchar_t chr, int attr)\r
-{\r
-    if (b->bufpos >= b->buflen) {\r
-       b->buflen += 128;\r
-       b->textbuf = sresize(b->textbuf, b->buflen, wchar_t);\r
-       b->textptr = b->textbuf + b->bufpos;\r
-       b->attrbuf = sresize(b->attrbuf, b->buflen, int);\r
-       b->attrptr = b->attrbuf + b->bufpos;\r
-    }\r
-    *b->textptr++ = chr;\r
-    *b->attrptr++ = attr;\r
-    b->bufpos++;\r
-}\r
-\r
-static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)\r
-{\r
-    clip_workbuf buf;\r
-    int old_top_x;\r
-    int attr;\r
-\r
-    buf.buflen = 5120;                 \r
-    buf.bufpos = 0;\r
-    buf.textptr = buf.textbuf = snewn(buf.buflen, wchar_t);\r
-    buf.attrptr = buf.attrbuf = snewn(buf.buflen, int);\r
-\r
-    old_top_x = top.x;                /* needed for rect==1 */\r
-\r
-    while (poslt(top, bottom)) {\r
-       int nl = FALSE;\r
-       termline *ldata = lineptr(top.y);\r
-       pos nlpos;\r
-\r
-       /*\r
-        * nlpos will point at the maximum position on this line we\r
-        * should copy up to. So we start it at the end of the\r
-        * line...\r
-        */\r
-       nlpos.y = top.y;\r
-       nlpos.x = term->cols;\r
-\r
-       /*\r
-        * ... move it backwards if there's unused space at the end\r
-        * of the line (and also set `nl' if this is the case,\r
-        * because in normal selection mode this means we need a\r
-        * newline at the end)...\r
-        */\r
-       if (!(ldata->lattr & LATTR_WRAPPED)) {\r
-           while (nlpos.x &&\r
-                  IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&\r
-                  !ldata->chars[nlpos.x - 1].cc_next &&\r
-                  poslt(top, nlpos))\r
-               decpos(nlpos);\r
-           if (poslt(nlpos, bottom))\r
-               nl = TRUE;\r
-       } else if (ldata->lattr & LATTR_WRAPPED2) {\r
-           /* Ignore the last char on the line in a WRAPPED2 line. */\r
-           decpos(nlpos);\r
-       }\r
-\r
-       /*\r
-        * ... and then clip it to the terminal x coordinate if\r
-        * we're doing rectangular selection. (In this case we\r
-        * still did the above, so that copying e.g. the right-hand\r
-        * column from a table doesn't fill with spaces on the\r
-        * right.)\r
-        */\r
-       if (rect) {\r
-           if (nlpos.x > bottom.x)\r
-               nlpos.x = bottom.x;\r
-           nl = (top.y < bottom.y);\r
-       }\r
-\r
-       while (poslt(top, bottom) && poslt(top, nlpos)) {\r
-#if 0\r
-           char cbuf[16], *p;\r
-           sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));\r
-#else\r
-           wchar_t cbuf[16], *p;\r
-           int c;\r
-           int x = top.x;\r
-\r
-           if (ldata->chars[x].chr == UCSWIDE) {\r
-               top.x++;\r
-               continue;\r
-           }\r
-\r
-           while (1) {\r
-               int uc = ldata->chars[x].chr;\r
-                attr = ldata->chars[x].attr;\r
-\r
-               switch (uc & CSET_MASK) {\r
-                 case CSET_LINEDRW:\r
-                   if (!term->cfg.rawcnp) {\r
-                       uc = term->ucsdata->unitab_xterm[uc & 0xFF];\r
-                       break;\r
-                   }\r
-                 case CSET_ASCII:\r
-                   uc = term->ucsdata->unitab_line[uc & 0xFF];\r
-                   break;\r
-                 case CSET_SCOACS:\r
-                   uc = term->ucsdata->unitab_scoacs[uc&0xFF];\r
-                   break;\r
-               }\r
-               switch (uc & CSET_MASK) {\r
-                 case CSET_ACP:\r
-                   uc = term->ucsdata->unitab_font[uc & 0xFF];\r
-                   break;\r
-                 case CSET_OEMCP:\r
-                   uc = term->ucsdata->unitab_oemcp[uc & 0xFF];\r
-                   break;\r
-               }\r
-\r
-               c = (uc & ~CSET_MASK);\r
-#ifdef PLATFORM_IS_UTF16\r
-               if (uc > 0x10000 && uc < 0x110000) {\r
-                   cbuf[0] = 0xD800 | ((uc - 0x10000) >> 10);\r
-                   cbuf[1] = 0xDC00 | ((uc - 0x10000) & 0x3FF);\r
-                   cbuf[2] = 0;\r
-               } else\r
-#endif\r
-               {\r
-                   cbuf[0] = uc;\r
-                   cbuf[1] = 0;\r
-               }\r
-\r
-               if (DIRECT_FONT(uc)) {\r
-                   if (c >= ' ' && c != 0x7F) {\r
-                       char buf[4];\r
-                       WCHAR wbuf[4];\r
-                       int rv;\r
-                       if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {\r
-                           buf[0] = c;\r
-                           buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);\r
-                           rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);\r
-                           top.x++;\r
-                       } else {\r
-                           buf[0] = c;\r
-                           rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);\r
-                       }\r
-\r
-                       if (rv > 0) {\r
-                           memcpy(cbuf, wbuf, rv * sizeof(wchar_t));\r
-                           cbuf[rv] = 0;\r
-                       }\r
-                   }\r
-               }\r
-#endif\r
-\r
-               for (p = cbuf; *p; p++)\r
-                   clip_addchar(&buf, *p, attr);\r
-\r
-               if (ldata->chars[x].cc_next)\r
-                   x += ldata->chars[x].cc_next;\r
-               else\r
-                   break;\r
-           }\r
-           top.x++;\r
-       }\r
-       if (nl) {\r
-           int i;\r
-           for (i = 0; i < sel_nl_sz; i++)\r
-               clip_addchar(&buf, sel_nl[i], 0);\r
-       }\r
-       top.y++;\r
-       top.x = rect ? old_top_x : 0;\r
-\r
-       unlineptr(ldata);\r
-    }\r
-#if SELECTION_NUL_TERMINATED\r
-    clip_addchar(&buf, 0, 0);\r
-#endif\r
-    /* Finally, transfer all that to the clipboard. */\r
-    write_clip(term->frontend, buf.textbuf, buf.attrbuf, buf.bufpos, desel);\r
-    sfree(buf.textbuf);\r
-    sfree(buf.attrbuf);\r
-}\r
-\r
-void term_copyall(Terminal *term)\r
-{\r
-    pos top;\r
-    pos bottom;\r
-    tree234 *screen = term->screen;\r
-    top.y = -sblines(term);\r
-    top.x = 0;\r
-    bottom.y = find_last_nonempty_line(term, screen);\r
-    bottom.x = term->cols;\r
-    clipme(term, top, bottom, 0, TRUE);\r
-}\r
-\r
-/*\r
- * The wordness array is mainly for deciding the disposition of the\r
- * US-ASCII characters.\r
- */\r
-static int wordtype(Terminal *term, int uc)\r
-{\r
-    struct ucsword {\r
-       int start, end, ctype;\r
-    };\r
-    static const struct ucsword ucs_words[] = {\r
-       {\r
-       128, 160, 0}, {\r
-       161, 191, 1}, {\r
-       215, 215, 1}, {\r
-       247, 247, 1}, {\r
-       0x037e, 0x037e, 1},            /* Greek question mark */\r
-       {\r
-       0x0387, 0x0387, 1},            /* Greek ano teleia */\r
-       {\r
-       0x055a, 0x055f, 1},            /* Armenian punctuation */\r
-       {\r
-       0x0589, 0x0589, 1},            /* Armenian full stop */\r
-       {\r
-       0x0700, 0x070d, 1},            /* Syriac punctuation */\r
-       {\r
-       0x104a, 0x104f, 1},            /* Myanmar punctuation */\r
-       {\r
-       0x10fb, 0x10fb, 1},            /* Georgian punctuation */\r
-       {\r
-       0x1361, 0x1368, 1},            /* Ethiopic punctuation */\r
-       {\r
-       0x166d, 0x166e, 1},            /* Canadian Syl. punctuation */\r
-       {\r
-       0x17d4, 0x17dc, 1},            /* Khmer punctuation */\r
-       {\r
-       0x1800, 0x180a, 1},            /* Mongolian punctuation */\r
-       {\r
-       0x2000, 0x200a, 0},            /* Various spaces */\r
-       {\r
-       0x2070, 0x207f, 2},            /* superscript */\r
-       {\r
-       0x2080, 0x208f, 2},            /* subscript */\r
-       {\r
-       0x200b, 0x27ff, 1},            /* punctuation and symbols */\r
-       {\r
-       0x3000, 0x3000, 0},            /* ideographic space */\r
-       {\r
-       0x3001, 0x3020, 1},            /* ideographic punctuation */\r
-       {\r
-       0x303f, 0x309f, 3},            /* Hiragana */\r
-       {\r
-       0x30a0, 0x30ff, 3},            /* Katakana */\r
-       {\r
-       0x3300, 0x9fff, 3},            /* CJK Ideographs */\r
-       {\r
-       0xac00, 0xd7a3, 3},            /* Hangul Syllables */\r
-       {\r
-       0xf900, 0xfaff, 3},            /* CJK Ideographs */\r
-       {\r
-       0xfe30, 0xfe6b, 1},            /* punctuation forms */\r
-       {\r
-       0xff00, 0xff0f, 1},            /* half/fullwidth ASCII */\r
-       {\r
-       0xff1a, 0xff20, 1},            /* half/fullwidth ASCII */\r
-       {\r
-       0xff3b, 0xff40, 1},            /* half/fullwidth ASCII */\r
-       {\r
-       0xff5b, 0xff64, 1},            /* half/fullwidth ASCII */\r
-       {\r
-       0xfff0, 0xffff, 0},            /* half/fullwidth ASCII */\r
-       {\r
-       0, 0, 0}\r
-    };\r
-    const struct ucsword *wptr;\r
-\r
-    switch (uc & CSET_MASK) {\r
-      case CSET_LINEDRW:\r
-       uc = term->ucsdata->unitab_xterm[uc & 0xFF];\r
-       break;\r
-      case CSET_ASCII:\r
-       uc = term->ucsdata->unitab_line[uc & 0xFF];\r
-       break;\r
-      case CSET_SCOACS:  \r
-       uc = term->ucsdata->unitab_scoacs[uc&0xFF]; \r
-       break;\r
-    }\r
-    switch (uc & CSET_MASK) {\r
-      case CSET_ACP:\r
-       uc = term->ucsdata->unitab_font[uc & 0xFF];\r
-       break;\r
-      case CSET_OEMCP:\r
-       uc = term->ucsdata->unitab_oemcp[uc & 0xFF];\r
-       break;\r
-    }\r
-\r
-    /* For DBCS fonts I can't do anything useful. Even this will sometimes\r
-     * fail as there's such a thing as a double width space. :-(\r
-     */\r
-    if (term->ucsdata->dbcs_screenfont &&\r
-       term->ucsdata->font_codepage == term->ucsdata->line_codepage)\r
-       return (uc != ' ');\r
-\r
-    if (uc < 0x80)\r
-       return term->wordness[uc];\r
-\r
-    for (wptr = ucs_words; wptr->start; wptr++) {\r
-       if (uc >= wptr->start && uc <= wptr->end)\r
-           return wptr->ctype;\r
-    }\r
-\r
-    return 2;\r
-}\r
-\r
-/*\r
- * Spread the selection outwards according to the selection mode.\r
- */\r
-static pos sel_spread_half(Terminal *term, pos p, int dir)\r
-{\r
-    termline *ldata;\r
-    short wvalue;\r
-    int topy = -sblines(term);\r
-\r
-    ldata = lineptr(p.y);\r
-\r
-    switch (term->selmode) {\r
-      case SM_CHAR:\r
-       /*\r
-        * In this mode, every character is a separate unit, except\r
-        * for runs of spaces at the end of a non-wrapping line.\r
-        */\r
-       if (!(ldata->lattr & LATTR_WRAPPED)) {\r
-           termchar *q = ldata->chars + term->cols;\r
-           while (q > ldata->chars &&\r
-                  IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next)\r
-               q--;\r
-           if (q == ldata->chars + term->cols)\r
-               q--;\r
-           if (p.x >= q - ldata->chars)\r
-               p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);\r
-       }\r
-       break;\r
-      case SM_WORD:\r
-       /*\r
-        * In this mode, the units are maximal runs of characters\r
-        * whose `wordness' has the same value.\r
-        */\r
-       wvalue = wordtype(term, UCSGET(ldata->chars, p.x));\r
-       if (dir == +1) {\r
-           while (1) {\r
-               int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?\r
-                              term->cols-1 : term->cols);\r
-               if (p.x < maxcols-1) {\r
-                   if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)\r
-                       p.x++;\r
-                   else\r
-                       break;\r
-               } else {\r
-                   if (ldata->lattr & LATTR_WRAPPED) {\r
-                       termline *ldata2;\r
-                       ldata2 = lineptr(p.y+1);\r
-                       if (wordtype(term, UCSGET(ldata2->chars, 0))\r
-                           == wvalue) {\r
-                           p.x = 0;\r
-                           p.y++;\r
-                           unlineptr(ldata);\r
-                           ldata = ldata2;\r
-                       } else {\r
-                           unlineptr(ldata2);\r
-                           break;\r
-                       }\r
-                   } else\r
-                       break;\r
-               }\r
-           }\r
-       } else {\r
-           while (1) {\r
-               if (p.x > 0) {\r
-                   if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)\r
-                       p.x--;\r
-                   else\r
-                       break;\r
-               } else {\r
-                   termline *ldata2;\r
-                   int maxcols;\r
-                   if (p.y <= topy)\r
-                       break;\r
-                   ldata2 = lineptr(p.y-1);\r
-                   maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?\r
-                             term->cols-1 : term->cols);\r
-                   if (ldata2->lattr & LATTR_WRAPPED) {\r
-                       if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))\r
-                           == wvalue) {\r
-                           p.x = maxcols-1;\r
-                           p.y--;\r
-                           unlineptr(ldata);\r
-                           ldata = ldata2;\r
-                       } else {\r
-                           unlineptr(ldata2);\r
-                           break;\r
-                       }\r
-                   } else\r
-                       break;\r
-               }\r
-           }\r
-       }\r
-       break;\r
-      case SM_LINE:\r
-       /*\r
-        * In this mode, every line is a unit.\r
-        */\r
-       p.x = (dir == -1 ? 0 : term->cols - 1);\r
-       break;\r
-    }\r
-\r
-    unlineptr(ldata);\r
-    return p;\r
-}\r
-\r
-static void sel_spread(Terminal *term)\r
-{\r
-    if (term->seltype == LEXICOGRAPHIC) {\r
-       term->selstart = sel_spread_half(term, term->selstart, -1);\r
-       decpos(term->selend);\r
-       term->selend = sel_spread_half(term, term->selend, +1);\r
-       incpos(term->selend);\r
-    }\r
-}\r
-\r
-void term_do_paste(Terminal *term)\r
-{\r
-    wchar_t *data;\r
-    int len;\r
-\r
-    get_clip(term->frontend, &data, &len);\r
-    if (data && len > 0) {\r
-        wchar_t *p, *q;\r
-\r
-       term_seen_key_event(term);     /* pasted data counts */\r
-\r
-        if (term->paste_buffer)\r
-            sfree(term->paste_buffer);\r
-        term->paste_pos = term->paste_hold = term->paste_len = 0;\r
-        term->paste_buffer = snewn(len, wchar_t);\r
-\r
-        p = q = data;\r
-        while (p < data + len) {\r
-            while (p < data + len &&\r
-                   !(p <= data + len - sel_nl_sz &&\r
-                     !memcmp(p, sel_nl, sizeof(sel_nl))))\r
-                p++;\r
-\r
-            {\r
-                int i;\r
-                for (i = 0; i < p - q; i++) {\r
-                    term->paste_buffer[term->paste_len++] = q[i];\r
-                }\r
-            }\r
-\r
-            if (p <= data + len - sel_nl_sz &&\r
-                !memcmp(p, sel_nl, sizeof(sel_nl))) {\r
-                term->paste_buffer[term->paste_len++] = '\015';\r
-                p += sel_nl_sz;\r
-            }\r
-            q = p;\r
-        }\r
-\r
-        /* Assume a small paste will be OK in one go. */\r
-        if (term->paste_len < 256) {\r
-            if (term->ldisc)\r
-               luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);\r
-            if (term->paste_buffer)\r
-                sfree(term->paste_buffer);\r
-            term->paste_buffer = 0;\r
-            term->paste_pos = term->paste_hold = term->paste_len = 0;\r
-        }\r
-    }\r
-    get_clip(term->frontend, NULL, NULL);\r
-}\r
-\r
-void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,\r
-               Mouse_Action a, int x, int y, int shift, int ctrl, int alt)\r
-{\r
-    pos selpoint;\r
-    termline *ldata;\r
-    int raw_mouse = (term->xterm_mouse &&\r
-                    !term->cfg.no_mouse_rep &&\r
-                    !(term->cfg.mouse_override && shift));\r
-    int default_seltype;\r
-\r
-    if (y < 0) {\r
-       y = 0;\r
-       if (a == MA_DRAG && !raw_mouse)\r
-           term_scroll(term, 0, -1);\r
-    }\r
-    if (y >= term->rows) {\r
-       y = term->rows - 1;\r
-       if (a == MA_DRAG && !raw_mouse)\r
-           term_scroll(term, 0, +1);\r
-    }\r
-    if (x < 0) {\r
-       if (y > 0) {\r
-           x = term->cols - 1;\r
-           y--;\r
-       } else\r
-           x = 0;\r
-    }\r
-    if (x >= term->cols)\r
-       x = term->cols - 1;\r
-\r
-    selpoint.y = y + term->disptop;\r
-    ldata = lineptr(selpoint.y);\r
-\r
-    if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)\r
-       x /= 2;\r
-\r
-    /*\r
-     * Transform x through the bidi algorithm to find the _logical_\r
-     * click point from the physical one.\r
-     */\r
-    if (term_bidi_line(term, ldata, y) != NULL) {\r
-       x = term->post_bidi_cache[y].backward[x];\r
-    }\r
-\r
-    selpoint.x = x;\r
-    unlineptr(ldata);\r
-\r
-    /*\r
-     * If we're in the middle of a selection operation, we ignore raw\r
-     * mouse mode until it's done (we must have been not in raw mouse\r
-     * mode when it started).\r
-     * This makes use of Shift for selection reliable, and avoids the\r
-     * host seeing mouse releases for which they never saw corresponding\r
-     * presses.\r
-     */\r
-    if (raw_mouse &&\r
-       (term->selstate != ABOUT_TO) && (term->selstate != DRAGGING)) {\r
-       int encstate = 0, r, c;\r
-       char abuf[16];\r
-\r
-       if (term->ldisc) {\r
-\r
-           switch (braw) {\r
-             case MBT_LEFT:\r
-               encstate = 0x20;               /* left button down */\r
-               break;\r
-             case MBT_MIDDLE:\r
-               encstate = 0x21;\r
-               break;\r
-             case MBT_RIGHT:\r
-               encstate = 0x22;\r
-               break;\r
-             case MBT_WHEEL_UP:\r
-               encstate = 0x60;\r
-               break;\r
-             case MBT_WHEEL_DOWN:\r
-               encstate = 0x61;\r
-               break;\r
-             default: break;          /* placate gcc warning about enum use */\r
-           }\r
-           switch (a) {\r
-             case MA_DRAG:\r
-               if (term->xterm_mouse == 1)\r
-                   return;\r
-               encstate += 0x20;\r
-               break;\r
-             case MA_RELEASE:\r
-               encstate = 0x23;\r
-               term->mouse_is_down = 0;\r
-               break;\r
-             case MA_CLICK:\r
-               if (term->mouse_is_down == braw)\r
-                   return;\r
-               term->mouse_is_down = braw;\r
-               break;\r
-             default: break;          /* placate gcc warning about enum use */\r
-           }\r
-           if (shift)\r
-               encstate += 0x04;\r
-           if (ctrl)\r
-               encstate += 0x10;\r
-           r = y + 33;\r
-           c = x + 33;\r
-\r
-           sprintf(abuf, "\033[M%c%c%c", encstate, c, r);\r
-           ldisc_send(term->ldisc, abuf, 6, 0);\r
-       }\r
-       return;\r
-    }\r
-\r
-    /*\r
-     * Set the selection type (rectangular or normal) at the start\r
-     * of a selection attempt, from the state of Alt.\r
-     */\r
-    if (!alt ^ !term->cfg.rect_select)\r
-       default_seltype = RECTANGULAR;\r
-    else\r
-       default_seltype = LEXICOGRAPHIC;\r
-       \r
-    if (term->selstate == NO_SELECTION) {\r
-       term->seltype = default_seltype;\r
-    }\r
-\r
-    if (bcooked == MBT_SELECT && a == MA_CLICK) {\r
-       deselect(term);\r
-       term->selstate = ABOUT_TO;\r
-       term->seltype = default_seltype;\r
-       term->selanchor = selpoint;\r
-       term->selmode = SM_CHAR;\r
-    } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {\r
-       deselect(term);\r
-       term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);\r
-       term->selstate = DRAGGING;\r
-       term->selstart = term->selanchor = selpoint;\r
-       term->selend = term->selstart;\r
-       incpos(term->selend);\r
-       sel_spread(term);\r
-    } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||\r
-              (bcooked == MBT_EXTEND && a != MA_RELEASE)) {\r
-       if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))\r
-           return;\r
-       if (bcooked == MBT_EXTEND && a != MA_DRAG &&\r
-           term->selstate == SELECTED) {\r
-           if (term->seltype == LEXICOGRAPHIC) {\r
-               /*\r
-                * For normal selection, we extend by moving\r
-                * whichever end of the current selection is closer\r
-                * to the mouse.\r
-                */\r
-               if (posdiff(selpoint, term->selstart) <\r
-                   posdiff(term->selend, term->selstart) / 2) {\r
-                   term->selanchor = term->selend;\r
-                   decpos(term->selanchor);\r
-               } else {\r
-                   term->selanchor = term->selstart;\r
-               }\r
-           } else {\r
-               /*\r
-                * For rectangular selection, we have a choice of\r
-                * _four_ places to put selanchor and selpoint: the\r
-                * four corners of the selection.\r
-                */\r
-               if (2*selpoint.x < term->selstart.x + term->selend.x)\r
-                   term->selanchor.x = term->selend.x-1;\r
-               else\r
-                   term->selanchor.x = term->selstart.x;\r
-\r
-               if (2*selpoint.y < term->selstart.y + term->selend.y)\r
-                   term->selanchor.y = term->selend.y;\r
-               else\r
-                   term->selanchor.y = term->selstart.y;\r
-           }\r
-           term->selstate = DRAGGING;\r
-       }\r
-       if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)\r
-           term->selanchor = selpoint;\r
-       term->selstate = DRAGGING;\r
-       if (term->seltype == LEXICOGRAPHIC) {\r
-           /*\r
-            * For normal selection, we set (selstart,selend) to\r
-            * (selpoint,selanchor) in some order.\r
-            */\r
-           if (poslt(selpoint, term->selanchor)) {\r
-               term->selstart = selpoint;\r
-               term->selend = term->selanchor;\r
-               incpos(term->selend);\r
-           } else {\r
-               term->selstart = term->selanchor;\r
-               term->selend = selpoint;\r
-               incpos(term->selend);\r
-           }\r
-       } else {\r
-           /*\r
-            * For rectangular selection, we may need to\r
-            * interchange x and y coordinates (if the user has\r
-            * dragged in the -x and +y directions, or vice versa).\r
-            */\r
-           term->selstart.x = min(term->selanchor.x, selpoint.x);\r
-           term->selend.x = 1+max(term->selanchor.x, selpoint.x);\r
-           term->selstart.y = min(term->selanchor.y, selpoint.y);\r
-           term->selend.y =   max(term->selanchor.y, selpoint.y);\r
-       }\r
-       sel_spread(term);\r
-    } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&\r
-              a == MA_RELEASE) {\r
-       if (term->selstate == DRAGGING) {\r
-           /*\r
-            * We've completed a selection. We now transfer the\r
-            * data to the clipboard.\r
-            */\r
-           clipme(term, term->selstart, term->selend,\r
-                  (term->seltype == RECTANGULAR), FALSE);\r
-           term->selstate = SELECTED;\r
-       } else\r
-           term->selstate = NO_SELECTION;\r
-    } else if (bcooked == MBT_PASTE\r
-              && (a == MA_CLICK\r
-#if MULTICLICK_ONLY_EVENT\r
-                  || a == MA_2CLK || a == MA_3CLK\r
-#endif\r
-                  )) {\r
-       request_paste(term->frontend);\r
-    }\r
-\r
-    term_update(term);\r
-}\r
-\r
-int format_arrow_key(char *buf, Terminal *term, int xkey, int ctrl)\r
-{\r
-    char *p = buf;\r
-\r
-    if (term->vt52_mode)\r
-       p += sprintf((char *) p, "\x1B%c", xkey);\r
-    else {\r
-       int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);\r
-#if 0\r
-       /*\r
-        * RDB: VT100 & VT102 manuals both state the app cursor\r
-        * keys only work if the app keypad is on.\r
-        *\r
-        * SGT: That may well be true, but xterm disagrees and so\r
-        * does at least one application, so I've #if'ed this out\r
-        * and the behaviour is back to PuTTY's original: app\r
-        * cursor and app keypad are independently switchable\r
-        * modes. If anyone complains about _this_ I'll have to\r
-        * put in a configurable option.\r
-        */\r
-       if (!term->app_keypad_keys)\r
-           app_flg = 0;\r
-#endif\r
-       /* Useful mapping of Ctrl-arrows */\r
-       if (ctrl)\r
-           app_flg = !app_flg;\r
-\r
-       if (app_flg)\r
-           p += sprintf((char *) p, "\x1BO%c", xkey);\r
-       else\r
-           p += sprintf((char *) p, "\x1B[%c", xkey);\r
-    }\r
-\r
-    return p - buf;\r
-}\r
-\r
-void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,\r
-             unsigned int modifiers, unsigned int flags)\r
-{\r
-    char output[10];\r
-    char *p = output;\r
-    int prependesc = FALSE;\r
-#if 0\r
-    int i;\r
-\r
-    fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);\r
-    for (i = 0; i < tlen; i++)\r
-       fprintf(stderr, " %04x", (unsigned)text[i]);\r
-    fprintf(stderr, "\n");\r
-#endif\r
-\r
-    /* XXX Num Lock */\r
-    if ((flags & PKF_REPEAT) && term->repeat_off)\r
-       return;\r
-\r
-    /* Currently, Meta always just prefixes everything with ESC. */\r
-    if (modifiers & PKM_META)\r
-       prependesc = TRUE;\r
-    modifiers &= ~PKM_META;\r
-\r
-    /*\r
-     * Alt is only used for Alt+keypad, which isn't supported yet, so\r
-     * ignore it.\r
-     */\r
-    modifiers &= ~PKM_ALT;\r
-\r
-    /* Standard local function keys */\r
-    switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {\r
-      case PKM_SHIFT:\r
-       if (keysym == PK_PAGEUP)\r
-           /* scroll up one page */;\r
-       if (keysym == PK_PAGEDOWN)\r
-           /* scroll down on page */;\r
-       if (keysym == PK_INSERT)\r
-           term_do_paste(term);\r
-       break;\r
-      case PKM_CONTROL:\r
-       if (keysym == PK_PAGEUP)\r
-           /* scroll up one line */;\r
-       if (keysym == PK_PAGEDOWN)\r
-           /* scroll down one line */;\r
-       /* Control-Numlock for app-keypad mode switch */\r
-       if (keysym == PK_PF1)\r
-           term->app_keypad_keys ^= 1;\r
-       break;\r
-    }\r
-\r
-    if (modifiers & PKM_ALT) {\r
-       /* Alt+F4 (close) */\r
-       /* Alt+Return (full screen) */\r
-       /* Alt+Space (system menu) */\r
-    }\r
-\r
-    if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&\r
-       text[0] >= 0x20 && text[0] <= 0x7e) {\r
-       /* ASCII chars + Control */\r
-       if ((text[0] >= 0x40 && text[0] <= 0x5f) ||\r
-           (text[0] >= 0x61 && text[0] <= 0x7a))\r
-           text[0] &= 0x1f;\r
-       else {\r
-           /*\r
-            * Control-2 should return ^@ (0x00), Control-6 should return\r
-            * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since\r
-            * the DOS keyboard handling did it, and we have nothing better\r
-            * to do with the key combo in question, we'll also map\r
-            * Control-Backquote to ^\ (0x1C).\r
-            */\r
-           switch (text[0]) {\r
-             case ' ': text[0] = 0x00; break;\r
-             case '-': text[0] = 0x1f; break;\r
-             case '/': text[0] = 0x1f; break;\r
-             case '2': text[0] = 0x00; break;\r
-             case '3': text[0] = 0x1b; break;\r
-             case '4': text[0] = 0x1c; break;\r
-             case '5': text[0] = 0x1d; break;\r
-             case '6': text[0] = 0x1e; break;\r
-             case '7': text[0] = 0x1f; break;\r
-             case '8': text[0] = 0x7f; break;\r
-             case '`': text[0] = 0x1c; break;\r
-           }\r
-       }\r
-    }\r
-\r
-    /* Nethack keypad */\r
-    if (term->cfg.nethack_keypad) {\r
-       char c = 0;\r
-       switch (keysym) {\r
-         case PK_KP1: c = 'b'; break;\r
-         case PK_KP2: c = 'j'; break;\r
-         case PK_KP3: c = 'n'; break;\r
-         case PK_KP4: c = 'h'; break;\r
-         case PK_KP5: c = '.'; break;\r
-         case PK_KP6: c = 'l'; break;\r
-         case PK_KP7: c = 'y'; break;\r
-         case PK_KP8: c = 'k'; break;\r
-         case PK_KP9: c = 'u'; break;\r
-         default: break; /* else gcc warns `enum value not used' */\r
-       }\r
-       if (c != 0) {\r
-           if (c != '.') {\r
-               if (modifiers & PKM_CONTROL)\r
-                   c &= 0x1f;\r
-               else if (modifiers & PKM_SHIFT)\r
-                       c = toupper((unsigned char)c);\r
-           }\r
-           *p++ = c;\r
-           goto done;\r
-       }\r
-    }\r
-\r
-    /* Numeric Keypad */\r
-    if (PK_ISKEYPAD(keysym)) {\r
-       int xkey = 0;\r
-\r
-       /*\r
-        * In VT400 mode, PFn always emits an escape sequence.  In\r
-        * Linux and tilde modes, this only happens in app keypad mode.\r
-        */\r
-       if (term->cfg.funky_type == FUNKY_VT400 ||\r
-           ((term->cfg.funky_type == FUNKY_LINUX ||\r
-             term->cfg.funky_type == FUNKY_TILDE) &&\r
-            term->app_keypad_keys && !term->cfg.no_applic_k)) {\r
-           switch (keysym) {\r
-             case PK_PF1: xkey = 'P'; break;\r
-             case PK_PF2: xkey = 'Q'; break;\r
-             case PK_PF3: xkey = 'R'; break;\r
-             case PK_PF4: xkey = 'S'; break;\r
-             default: break; /* else gcc warns `enum value not used' */\r
-           }\r
-       }\r
-       if (term->app_keypad_keys && !term->cfg.no_applic_k) {\r
-           switch (keysym) {\r
-             case PK_KP0: xkey = 'p'; break;\r
-             case PK_KP1: xkey = 'q'; break;\r
-             case PK_KP2: xkey = 'r'; break;\r
-             case PK_KP3: xkey = 's'; break;\r
-             case PK_KP4: xkey = 't'; break;\r
-             case PK_KP5: xkey = 'u'; break;\r
-             case PK_KP6: xkey = 'v'; break;\r
-             case PK_KP7: xkey = 'w'; break;\r
-             case PK_KP8: xkey = 'x'; break;\r
-             case PK_KP9: xkey = 'y'; break;\r
-             case PK_KPDECIMAL: xkey = 'n'; break;\r
-             case PK_KPENTER: xkey = 'M'; break;\r
-             default: break; /* else gcc warns `enum value not used' */\r
-           }\r
-           if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {\r
-               /*\r
-                * xterm can't see the layout of the keypad, so it has\r
-                * to rely on the X keysyms returned by the keys.\r
-                * Hence, we look at the strings here, not the PuTTY\r
-                * keysyms (which describe the layout).\r
-                */\r
-               switch (text[0]) {\r
-                 case '+':\r
-                   if (modifiers & PKM_SHIFT)\r
-                       xkey = 'l';\r
-                   else\r
-                       xkey = 'k';\r
-                   break;\r
-                 case '/': xkey = 'o'; break;\r
-                 case '*': xkey = 'j'; break;\r
-                 case '-': xkey = 'm'; break;\r
-               }\r
-           } else {\r
-               /*\r
-                * In all other modes, we try to retain the layout of\r
-                * the DEC keypad in application mode.\r
-                */\r
-               switch (keysym) {\r
-                 case PK_KPBIGPLUS:\r
-                   /* This key covers the '-' and ',' keys on a VT220 */\r
-                   if (modifiers & PKM_SHIFT)\r
-                       xkey = 'm'; /* VT220 '-' */\r
-                   else\r
-                       xkey = 'l'; /* VT220 ',' */\r
-                   break;\r
-                 case PK_KPMINUS: xkey = 'm'; break;\r
-                 case PK_KPCOMMA: xkey = 'l'; break;\r
-                 default: break; /* else gcc warns `enum value not used' */\r
-               }\r
-           }\r
-       }\r
-       if (xkey) {\r
-           if (term->vt52_mode) {\r
-               if (xkey >= 'P' && xkey <= 'S')\r
-                   p += sprintf((char *) p, "\x1B%c", xkey);\r
-               else\r
-                   p += sprintf((char *) p, "\x1B?%c", xkey);\r
-           } else\r
-               p += sprintf((char *) p, "\x1BO%c", xkey);\r
-           goto done;\r
-       }\r
-       /* Not in application mode -- treat the number pad as arrow keys? */\r
-       if ((flags & PKF_NUMLOCK) == 0) {\r
-           switch (keysym) {\r
-             case PK_KP0: keysym = PK_INSERT; break;\r
-             case PK_KP1: keysym = PK_END; break;\r
-             case PK_KP2: keysym = PK_DOWN; break;\r
-             case PK_KP3: keysym = PK_PAGEDOWN; break;\r
-             case PK_KP4: keysym = PK_LEFT; break;\r
-             case PK_KP5: keysym = PK_REST; break;\r
-             case PK_KP6: keysym = PK_RIGHT; break;\r
-             case PK_KP7: keysym = PK_HOME; break;\r
-             case PK_KP8: keysym = PK_UP; break;\r
-             case PK_KP9: keysym = PK_PAGEUP; break;\r
-             default: break; /* else gcc warns `enum value not used' */\r
-           }\r
-       }\r
-    }\r
-\r
-    /* Miscellaneous keys */\r
-    switch (keysym) {\r
-      case PK_ESCAPE:\r
-       *p++ = 0x1b;\r
-       goto done;\r
-      case PK_BACKSPACE:\r
-           if (modifiers == 0)\r
-               *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);\r
-           else if (modifiers == PKM_SHIFT)\r
-               /* We do the opposite of what is configured */\r
-               *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);\r
-           else break;\r
-           goto done;\r
-      case PK_TAB:\r
-       if (modifiers == 0)\r
-           *p++ = 0x09;\r
-       else if (modifiers == PKM_SHIFT)\r
-           *p++ = 0x1B, *p++ = '[', *p++ = 'Z';\r
-       else break;\r
-       goto done;\r
-       /* XXX window.c has ctrl+shift+space sending 0xa0 */\r
-      case PK_PAUSE:\r
-       if (modifiers == PKM_CONTROL)\r
-           *p++ = 26;\r
-       else break;\r
-       goto done;\r
-      case PK_RETURN:\r
-      case PK_KPENTER: /* Odd keypad modes handled above */\r
-       if (modifiers == 0) {\r
-           *p++ = 0x0d;\r
-           if (term->cr_lf_return)\r
-               *p++ = 0x0a;\r
-           goto done;\r
-       }\r
-      default: break; /* else gcc warns `enum value not used' */\r
-    }\r
-\r
-    /* SCO function keys and editing keys */\r
-    if (term->cfg.funky_type == FUNKY_SCO) {\r
-       if (PK_ISFKEY(keysym) && keysym <= PK_F12) {\r
-           static char const codes[] =\r
-               "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";\r
-           int index = keysym - PK_F1;\r
-\r
-           if (modifiers & PKM_SHIFT) index += 12;\r
-           if (modifiers & PKM_CONTROL) index += 24;\r
-           p += sprintf((char *) p, "\x1B[%c", codes[index]);\r
-           goto done;\r
-       }\r
-       if (PK_ISEDITING(keysym)) {\r
-           int xkey = 0;\r
-\r
-           switch (keysym) {\r
-             case PK_DELETE:   *p++ = 0x7f; goto done;\r
-             case PK_HOME:     xkey = 'H'; break;\r
-             case PK_INSERT:   xkey = 'L'; break;\r
-             case PK_END:      xkey = 'F'; break;\r
-             case PK_PAGEUP:   xkey = 'I'; break;\r
-             case PK_PAGEDOWN: xkey = 'G'; break;\r
-             default: break; /* else gcc warns `enum value not used' */\r
-           }\r
-           p += sprintf((char *) p, "\x1B[%c", xkey);\r
-       }\r
-    }\r
-\r
-    if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {\r
-       int code;\r
-\r
-       if (term->cfg.funky_type == FUNKY_XTERM) {\r
-           /* Xterm shuffles these keys, apparently. */\r
-           switch (keysym) {\r
-             case PK_HOME:     keysym = PK_INSERT;   break;\r
-             case PK_INSERT:   keysym = PK_HOME;     break;\r
-             case PK_DELETE:   keysym = PK_END;      break;\r
-             case PK_END:      keysym = PK_PAGEUP;   break;\r
-             case PK_PAGEUP:   keysym = PK_DELETE;   break;\r
-             case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;\r
-             default: break; /* else gcc warns `enum value not used' */\r
-           }\r
-       }\r
-\r
-       /* RXVT Home/End */\r
-       if (term->cfg.rxvt_homeend &&\r
-           (keysym == PK_HOME || keysym == PK_END)) {\r
-           p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");\r
-           goto done;\r
-       }\r
-\r
-       if (term->vt52_mode) {\r
-           int xkey;\r
-\r
-           /*\r
-            * A real VT52 doesn't have these, and a VT220 doesn't\r
-            * send anything for them in VT52 mode.\r
-            */\r
-           switch (keysym) {\r
-             case PK_HOME:     xkey = 'H'; break;\r
-             case PK_INSERT:   xkey = 'L'; break;\r
-             case PK_DELETE:   xkey = 'M'; break;\r
-             case PK_END:      xkey = 'E'; break;\r
-             case PK_PAGEUP:   xkey = 'I'; break;\r
-             case PK_PAGEDOWN: xkey = 'G'; break;\r
-             default: xkey=0; break; /* else gcc warns `enum value not used'*/\r
-           }\r
-           p += sprintf((char *) p, "\x1B%c", xkey);\r
-           goto done;\r
-       }\r
-\r
-       switch (keysym) {\r
-         case PK_HOME:     code = 1; break;\r
-         case PK_INSERT:   code = 2; break;\r
-         case PK_DELETE:   code = 3; break;\r
-         case PK_END:      code = 4; break;\r
-         case PK_PAGEUP:   code = 5; break;\r
-         case PK_PAGEDOWN: code = 6; break;\r
-         default: code = 0; break; /* else gcc warns `enum value not used' */\r
-       }\r
-       p += sprintf((char *) p, "\x1B[%d~", code);\r
-       goto done;\r
-    }\r
-\r
-    if (PK_ISFKEY(keysym)) {\r
-       /* Map Shift+F1-F10 to F11-F20 */\r
-       if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))\r
-           keysym += 10;\r
-       if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&\r
-           keysym <= PK_F14) {\r
-           /* XXX This overrides the XTERM/VT52 mode below */\r
-           int offt = 0;\r
-           if (keysym >= PK_F6)  offt++;\r
-           if (keysym >= PK_F12) offt++;\r
-           p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",\r
-                        'P' + keysym - PK_F1 - offt);\r
-           goto done;\r
-       }\r
-       if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {\r
-           p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);\r
-           goto done;\r
-       }\r
-       if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {\r
-           if (term->vt52_mode)\r
-               p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);\r
-           else\r
-               p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);\r
-           goto done;\r
-       }\r
-       p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);\r
-       goto done;\r
-    }\r
-\r
-    if (PK_ISCURSOR(keysym)) {\r
-       int xkey;\r
-\r
-       switch (keysym) {\r
-         case PK_UP:    xkey = 'A'; break;\r
-         case PK_DOWN:  xkey = 'B'; break;\r
-         case PK_RIGHT: xkey = 'C'; break;\r
-         case PK_LEFT:  xkey = 'D'; break;\r
-         case PK_REST:  xkey = 'G'; break; /* centre key on number pad */\r
-         default: xkey = 0; break; /* else gcc warns `enum value not used' */\r
-       }\r
-       p += format_arrow_key(p, term, xkey, modifiers == PKM_CONTROL);\r
-       goto done;\r
-    }\r
-\r
-  done:\r
-    if (p > output || tlen > 0) {\r
-       /*\r
-        * Interrupt an ongoing paste. I'm not sure\r
-        * this is sensible, but for the moment it's\r
-        * preferable to having to faff about buffering\r
-        * things.\r
-        */\r
-       term_nopaste(term);\r
-\r
-       /*\r
-        * We need not bother about stdin backlogs\r
-        * here, because in GUI PuTTY we can't do\r
-        * anything about it anyway; there's no means\r
-        * of asking Windows to hold off on KEYDOWN\r
-        * messages. We _have_ to buffer everything\r
-        * we're sent.\r
-        */\r
-       term_seen_key_event(term);\r
-\r
-       if (prependesc) {\r
-#if 0\r
-           fprintf(stderr, "sending ESC\n");\r
-#endif\r
-           ldisc_send(term->ldisc, "\x1b", 1, 1);\r
-       }\r
-\r
-       if (p > output) {\r
-#if 0\r
-           fprintf(stderr, "sending %d bytes:", p - output);\r
-           for (i = 0; i < p - output; i++)\r
-               fprintf(stderr, " %02x", output[i]);\r
-           fprintf(stderr, "\n");\r
-#endif\r
-           ldisc_send(term->ldisc, output, p - output, 1);\r
-       } else if (tlen > 0) {\r
-#if 0\r
-           fprintf(stderr, "sending %d unichars:", tlen);\r
-           for (i = 0; i < tlen; i++)\r
-               fprintf(stderr, " %04x", (unsigned) text[i]);\r
-           fprintf(stderr, "\n");\r
-#endif\r
-           luni_send(term->ldisc, text, tlen, 1);\r
-       }\r
-    }\r
-}\r
-\r
-void term_nopaste(Terminal *term)\r
-{\r
-    if (term->paste_len == 0)\r
-       return;\r
-    sfree(term->paste_buffer);\r
-    term->paste_buffer = NULL;\r
-    term->paste_len = 0;\r
-}\r
-\r
-int term_paste_pending(Terminal *term)\r
-{\r
-    return term->paste_len != 0;\r
-}\r
-\r
-void term_paste(Terminal *term)\r
-{\r
-    long now, paste_diff;\r
-\r
-    if (term->paste_len == 0)\r
-       return;\r
-\r
-    /* Don't wait forever to paste */\r
-    if (term->paste_hold) {\r
-       now = GETTICKCOUNT();\r
-       paste_diff = now - term->last_paste;\r
-       if (paste_diff >= 0 && paste_diff < 450)\r
-           return;\r
-    }\r
-    term->paste_hold = 0;\r
-\r
-    while (term->paste_pos < term->paste_len) {\r
-       int n = 0;\r
-       while (n + term->paste_pos < term->paste_len) {\r
-           if (term->paste_buffer[term->paste_pos + n++] == '\015')\r
-               break;\r
-       }\r
-       if (term->ldisc)\r
-           luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);\r
-       term->paste_pos += n;\r
-\r
-       if (term->paste_pos < term->paste_len) {\r
-           term->paste_hold = 1;\r
-           return;\r
-       }\r
-    }\r
-    sfree(term->paste_buffer);\r
-    term->paste_buffer = NULL;\r
-    term->paste_len = 0;\r
-}\r
-\r
-static void deselect(Terminal *term)\r
-{\r
-    term->selstate = NO_SELECTION;\r
-    term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;\r
-}\r
-\r
-void term_deselect(Terminal *term)\r
-{\r
-    deselect(term);\r
-    term_update(term);\r
-}\r
-\r
-int term_ldisc(Terminal *term, int option)\r
-{\r
-    if (option == LD_ECHO)\r
-       return term->term_echoing;\r
-    if (option == LD_EDIT)\r
-       return term->term_editing;\r
-    return FALSE;\r
-}\r
-\r
-int term_data(Terminal *term, int is_stderr, const char *data, int len)\r
-{\r
-    bufchain_add(&term->inbuf, data, len);\r
-\r
-    if (!term->in_term_out) {\r
-       term->in_term_out = TRUE;\r
-       term_reset_cblink(term);\r
-       /*\r
-        * During drag-selects, we do not process terminal input,\r
-        * because the user will want the screen to hold still to\r
-        * be selected.\r
-        */\r
-       if (term->selstate != DRAGGING)\r
-           term_out(term);\r
-       term->in_term_out = FALSE;\r
-    }\r
-\r
-    /*\r
-     * term_out() always completely empties inbuf. Therefore,\r
-     * there's no reason at all to return anything other than zero\r
-     * from this function, because there _can't_ be a question of\r
-     * the remote side needing to wait until term_out() has cleared\r
-     * a backlog.\r
-     *\r
-     * This is a slightly suboptimal way to deal with SSH-2 - in\r
-     * principle, the window mechanism would allow us to continue\r
-     * to accept data on forwarded ports and X connections even\r
-     * while the terminal processing was going slowly - but we\r
-     * can't do the 100% right thing without moving the terminal\r
-     * processing into a separate thread, and that might hurt\r
-     * portability. So we manage stdout buffering the old SSH-1 way:\r
-     * if the terminal processing goes slowly, the whole SSH\r
-     * connection stops accepting data until it's ready.\r
-     *\r
-     * In practice, I can't imagine this causing serious trouble.\r
-     */\r
-    return 0;\r
-}\r
-\r
-/*\r
- * Write untrusted data to the terminal.\r
- * The only control character that should be honoured is \n (which\r
- * will behave as a CRLF).\r
- */\r
-int term_data_untrusted(Terminal *term, const char *data, int len)\r
-{\r
-    int i;\r
-    /* FIXME: more sophisticated checking? */\r
-    for (i = 0; i < len; i++) {\r
-       if (data[i] == '\n')\r
-           term_data(term, 1, "\r\n", 2);\r
-       else if (data[i] & 0x60)\r
-           term_data(term, 1, data + i, 1);\r
-    }\r
-    return 0; /* assumes that term_data() always returns 0 */\r
-}\r
-\r
-void term_provide_logctx(Terminal *term, void *logctx)\r
-{\r
-    term->logctx = logctx;\r
-}\r
-\r
-void term_set_focus(Terminal *term, int has_focus)\r
-{\r
-    term->has_focus = has_focus;\r
-    term_schedule_cblink(term);\r
-}\r
-\r
-/*\r
- * Provide "auto" settings for remote tty modes, suitable for an\r
- * application with a terminal window.\r
- */\r
-char *term_get_ttymode(Terminal *term, const char *mode)\r
-{\r
-    char *val = NULL;\r
-    if (strcmp(mode, "ERASE") == 0) {\r
-       val = term->cfg.bksp_is_delete ? "^?" : "^H";\r
-    }\r
-    /* FIXME: perhaps we should set ONLCR based on cfg.lfhascr as well? */\r
-    /* FIXME: or ECHO and friends based on local echo state? */\r
-    return dupstr(val);\r
-}\r
-\r
-struct term_userpass_state {\r
-    size_t curr_prompt;\r
-    int done_prompt;   /* printed out prompt yet? */\r
-    size_t pos;                /* cursor position */\r
-};\r
-\r
-/*\r
- * Process some terminal data in the course of username/password\r
- * input.\r
- */\r
-int term_get_userpass_input(Terminal *term, prompts_t *p,\r
-                           unsigned char *in, int inlen)\r
-{\r
-    struct term_userpass_state *s = (struct term_userpass_state *)p->data;\r
-    if (!s) {\r
-       /*\r
-        * First call. Set some stuff up.\r
-        */\r
-       p->data = s = snew(struct term_userpass_state);\r
-       s->curr_prompt = 0;\r
-       s->done_prompt = 0;\r
-       /* We only print the `name' caption if we have to... */\r
-       if (p->name_reqd && p->name) {\r
-           size_t l = strlen(p->name);\r
-           term_data_untrusted(term, p->name, l);\r
-           if (p->name[l-1] != '\n')\r
-               term_data_untrusted(term, "\n", 1);\r
-       }\r
-       /* ...but we always print any `instruction'. */\r
-       if (p->instruction) {\r
-           size_t l = strlen(p->instruction);\r
-           term_data_untrusted(term, p->instruction, l);\r
-           if (p->instruction[l-1] != '\n')\r
-               term_data_untrusted(term, "\n", 1);\r
-       }\r
-       /*\r
-        * Zero all the results, in case we abort half-way through.\r
-        */\r
-       {\r
-           int i;\r
-           for (i = 0; i < (int)p->n_prompts; i++)\r
-               memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);\r
-       }\r
-    }\r
-\r
-    while (s->curr_prompt < p->n_prompts) {\r
-\r
-       prompt_t *pr = p->prompts[s->curr_prompt];\r
-       int finished_prompt = 0;\r
-\r
-       if (!s->done_prompt) {\r
-           term_data_untrusted(term, pr->prompt, strlen(pr->prompt));\r
-           s->done_prompt = 1;\r
-           s->pos = 0;\r
-       }\r
-\r
-       /* Breaking out here ensures that the prompt is printed even\r
-        * if we're now waiting for user data. */\r
-       if (!in || !inlen) break;\r
-\r
-       /* FIXME: should we be using local-line-editing code instead? */\r
-       while (!finished_prompt && inlen) {\r
-           char c = *in++;\r
-           inlen--;\r
-           switch (c) {\r
-             case 10:\r
-             case 13:\r
-               term_data(term, 0, "\r\n", 2);\r
-               pr->result[s->pos] = '\0';\r
-               pr->result[pr->result_len - 1] = '\0';\r
-               /* go to next prompt, if any */\r
-               s->curr_prompt++;\r
-               s->done_prompt = 0;\r
-               finished_prompt = 1; /* break out */\r
-               break;\r
-             case 8:\r
-             case 127:\r
-               if (s->pos > 0) {\r
-                   if (pr->echo)\r
-                       term_data(term, 0, "\b \b", 3);\r
-                   s->pos--;\r
-               }\r
-               break;\r
-             case 21:\r
-             case 27:\r
-               while (s->pos > 0) {\r
-                   if (pr->echo)\r
-                       term_data(term, 0, "\b \b", 3);\r
-                   s->pos--;\r
-               }\r
-               break;\r
-             case 3:\r
-             case 4:\r
-               /* Immediate abort. */\r
-               term_data(term, 0, "\r\n", 2);\r
-               sfree(s);\r
-               p->data = NULL;\r
-               return 0; /* user abort */\r
-             default:\r
-               /*\r
-                * This simplistic check for printability is disabled\r
-                * when we're doing password input, because some people\r
-                * have control characters in their passwords.\r
-                */\r
-               if ((!pr->echo ||\r
-                    (c >= ' ' && c <= '~') ||\r
-                    ((unsigned char) c >= 160))\r
-                   && s->pos < pr->result_len - 1) {\r
-                   pr->result[s->pos++] = c;\r
-                   if (pr->echo)\r
-                       term_data(term, 0, &c, 1);\r
-               }\r
-               break;\r
-           }\r
-       }\r
-       \r
-    }\r
-\r
-    if (s->curr_prompt < p->n_prompts) {\r
-       return -1; /* more data required */\r
-    } else {\r
-       sfree(s);\r
-       p->data = NULL;\r
-       return +1; /* all done */\r
-    }\r
-}\r