Xinqi Bao's Git

man page update
[st.git] / st.c
diff --git a/st.c b/st.c
index 46cf2da..0aacf96 100644 (file)
--- a/st.c
+++ b/st.c
 #define ESC_ARG_SIZ   16
 #define STR_BUF_SIZ   ESC_BUF_SIZ
 #define STR_ARG_SIZ   ESC_ARG_SIZ
 #define ESC_ARG_SIZ   16
 #define STR_BUF_SIZ   ESC_BUF_SIZ
 #define STR_ARG_SIZ   ESC_ARG_SIZ
+#define HISTSIZE      2000
 
 /* macros */
 #define IS_SET(flag)           ((term.mode & (flag)) != 0)
 
 /* macros */
 #define IS_SET(flag)           ((term.mode & (flag)) != 0)
-#define ISCONTROLC0(c)         (BETWEEN(c, 0, 0x1f) || (c) == '\177')
+#define ISCONTROLC0(c)         (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
 #define ISCONTROLC1(c)         (BETWEEN(c, 0x80, 0x9f))
 #define ISCONTROL(c)           (ISCONTROLC0(c) || ISCONTROLC1(c))
 #define ISCONTROLC1(c)         (BETWEEN(c, 0x80, 0x9f))
 #define ISCONTROL(c)           (ISCONTROLC0(c) || ISCONTROLC1(c))
-#define ISDELIM(u)             (utf8strchr(worddelimiters, u) != NULL)
+#define ISDELIM(u)             (u && wcschr(worddelimiters, u))
+#define TLINE(y)               ((y) < term.scr ? term.hist[((y) + term.histi - \
+                               term.scr + HISTSIZE + 1) % HISTSIZE] : \
+                               term.line[(y) - term.scr])
 
 enum term_mode {
        MODE_WRAP        = 1 << 0,
 
 enum term_mode {
        MODE_WRAP        = 1 << 0,
@@ -51,7 +55,6 @@ enum term_mode {
        MODE_ECHO        = 1 << 4,
        MODE_PRINT       = 1 << 5,
        MODE_UTF8        = 1 << 6,
        MODE_ECHO        = 1 << 4,
        MODE_PRINT       = 1 << 5,
        MODE_UTF8        = 1 << 6,
-       MODE_SIXEL       = 1 << 7,
 };
 
 enum cursor_movement {
 };
 
 enum cursor_movement {
@@ -78,12 +81,11 @@ enum charset {
 enum escape_state {
        ESC_START      = 1,
        ESC_CSI        = 2,
 enum escape_state {
        ESC_START      = 1,
        ESC_CSI        = 2,
-       ESC_STR        = 4,  /* OSC, PM, APC */
+       ESC_STR        = 4,  /* DCS, OSC, PM, APC */
        ESC_ALTCHARSET = 8,
        ESC_STR_END    = 16, /* a final string was encountered */
        ESC_TEST       = 32, /* Enter in test mode */
        ESC_UTF8       = 64,
        ESC_ALTCHARSET = 8,
        ESC_STR_END    = 16, /* a final string was encountered */
        ESC_TEST       = 32, /* Enter in test mode */
        ESC_UTF8       = 64,
-       ESC_DCS        =128,
 };
 
 typedef struct {
 };
 
 typedef struct {
@@ -117,6 +119,9 @@ typedef struct {
        int col;      /* nb col */
        Line *line;   /* screen */
        Line *alt;    /* alternate screen */
        int col;      /* nb col */
        Line *line;   /* screen */
        Line *alt;    /* alternate screen */
+       Line hist[HISTSIZE]; /* history buffer */
+       int histi;    /* history index */
+       int scr;      /* scroll back */
        int *dirty;   /* dirtyness of lines */
        TCursor c;    /* cursor */
        int ocx;      /* old cursor col */
        int *dirty;   /* dirtyness of lines */
        TCursor c;    /* cursor */
        int ocx;      /* old cursor col */
@@ -129,13 +134,14 @@ typedef struct {
        int charset;  /* current charset */
        int icharset; /* selected charset for sequence */
        int *tabs;
        int charset;  /* current charset */
        int icharset; /* selected charset for sequence */
        int *tabs;
+       Rune lastc;   /* last printed char outside of sequence, 0 if control */
 } Term;
 
 /* CSI Escape sequence structs */
 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
 typedef struct {
        char buf[ESC_BUF_SIZ]; /* raw string */
 } Term;
 
 /* CSI Escape sequence structs */
 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
 typedef struct {
        char buf[ESC_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       size_t len;            /* raw string length */
        char priv;
        int arg[ESC_ARG_SIZ];
        int narg;              /* nb of args */
        char priv;
        int arg[ESC_ARG_SIZ];
        int narg;              /* nb of args */
@@ -146,8 +152,9 @@ typedef struct {
 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
 typedef struct {
        char type;             /* ESC type ... */
 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
 typedef struct {
        char type;             /* ESC type ... */
-       char buf[STR_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       char *buf;             /* allocated raw string */
+       size_t siz;            /* allocation size */
+       size_t len;            /* raw string length */
        char *args[STR_ARG_SIZ];
        int narg;              /* nb of args */
 } STREscape;
        char *args[STR_ARG_SIZ];
        int narg;              /* nb of args */
 } STREscape;
@@ -184,20 +191,20 @@ static void tnewline(int);
 static void tputtab(int);
 static void tputc(Rune);
 static void treset(void);
 static void tputtab(int);
 static void tputc(Rune);
 static void treset(void);
-static void tscrollup(int, int);
-static void tscrolldown(int, int);
-static void tsetattr(int *, int);
-static void tsetchar(Rune, Glyph *, int, int);
+static void tscrollup(int, int, int);
+static void tscrolldown(int, int, int);
+static void tsetattr(const int *, int);
+static void tsetchar(Rune, const Glyph *, int, int);
 static void tsetdirt(int, int);
 static void tsetscroll(int, int);
 static void tswapscreen(void);
 static void tsetdirt(int, int);
 static void tsetscroll(int, int);
 static void tswapscreen(void);
-static void tsetmode(int, int, int *, int);
+static void tsetmode(int, int, const int *, int);
 static int twrite(const char *, int, int);
 static void tfulldirt(void);
 static void tcontrolcode(uchar );
 static void tdectest(char );
 static void tdefutf8(char);
 static int twrite(const char *, int, int);
 static void tfulldirt(void);
 static void tcontrolcode(uchar );
 static void tdectest(char );
 static void tdefutf8(char);
-static int32_t tdefcolor(int *, int *, int);
+static int32_t tdefcolor(const int *, int *, int);
 static void tdeftran(char);
 static void tstrsequence(uchar);
 
 static void tdeftran(char);
 static void tstrsequence(uchar);
 
@@ -210,7 +217,6 @@ static void selsnap(int *, int *, int);
 static size_t utf8decode(const char *, Rune *, size_t);
 static Rune utf8decodebyte(char, size_t *);
 static char utf8encodebyte(Rune, size_t);
 static size_t utf8decode(const char *, Rune *, size_t);
 static Rune utf8decodebyte(char, size_t *);
 static char utf8encodebyte(Rune, size_t);
-static char *utf8strchr(char *, Rune);
 static size_t utf8validate(Rune *, size_t);
 
 static char *base64dec(const char *);
 static size_t utf8validate(Rune *, size_t);
 
 static char *base64dec(const char *);
@@ -227,10 +233,10 @@ static int iofd = 1;
 static int cmdfd;
 static pid_t pid;
 
 static int cmdfd;
 static pid_t pid;
 
-static uchar utfbyte[UTF_SIZ + 1] = {0x80,    0, 0xC0, 0xE0, 0xF0};
-static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
-static Rune utfmin[UTF_SIZ + 1] = {       0,    0,  0x80,  0x800,  0x10000};
-static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
+static const uchar utfbyte[UTF_SIZ + 1] = {0x80,    0, 0xC0, 0xE0, 0xF0};
+static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
+static const Rune utfmin[UTF_SIZ + 1] = {       0,    0,  0x80,  0x800,  0x10000};
+static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
 
 ssize_t
 xwrite(int fd, const char *s, size_t len)
 
 ssize_t
 xwrite(int fd, const char *s, size_t len)
@@ -270,12 +276,14 @@ xrealloc(void *p, size_t len)
 }
 
 char *
 }
 
 char *
-xstrdup(char *s)
+xstrdup(const char *s)
 {
 {
-       if ((s = strdup(s)) == NULL)
+       char *p;
+
+       if ((p = strdup(s)) == NULL)
                die("strdup: %s\n", strerror(errno));
 
                die("strdup: %s\n", strerror(errno));
 
-       return s;
+       return p;
 }
 
 size_t
 }
 
 size_t
@@ -337,23 +345,6 @@ utf8encodebyte(Rune u, size_t i)
        return utfbyte[i] | (u & ~utfmask[i]);
 }
 
        return utfbyte[i] | (u & ~utfmask[i]);
 }
 
-char *
-utf8strchr(char *s, Rune u)
-{
-       Rune r;
-       size_t i, j, len;
-
-       len = strlen(s);
-       for (i = 0, j = 0; i < len; i += j) {
-               if (!(j = utf8decode(&s[i], &r, len - i)))
-                       break;
-               if (r == u)
-                       return &(s[i]);
-       }
-
-       return NULL;
-}
-
 size_t
 utf8validate(Rune *u, size_t i)
 {
 size_t
 utf8validate(Rune *u, size_t i)
 {
@@ -383,8 +374,9 @@ static const char base64_digits[] = {
 char
 base64dec_getc(const char **src)
 {
 char
 base64dec_getc(const char **src)
 {
-       while (**src && !isprint(**src)) (*src)++;
-       return *((*src)++);
+       while (**src && !isprint(**src))
+               (*src)++;
+       return **src ? *((*src)++) : '=';  /* emulate padding if string ends */
 }
 
 char *
 }
 
 char *
@@ -402,6 +394,10 @@ base64dec(const char *src)
                int c = base64_digits[(unsigned char) base64dec_getc(&src)];
                int d = base64_digits[(unsigned char) base64dec_getc(&src)];
 
                int c = base64_digits[(unsigned char) base64dec_getc(&src)];
                int d = base64_digits[(unsigned char) base64dec_getc(&src)];
 
+               /* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
+               if (a == -1 || b == -1)
+                       break;
+
                *dst++ = (a << 2) | ((b & 0x30) >> 4);
                if (c == -1)
                        break;
                *dst++ = (a << 2) | ((b & 0x30) >> 4);
                if (c == -1)
                        break;
@@ -427,10 +423,10 @@ tlinelen(int y)
 {
        int i = term.col;
 
 {
        int i = term.col;
 
-       if (term.line[y][i - 1].mode & ATTR_WRAP)
+       if (TLINE(y)[i - 1].mode & ATTR_WRAP)
                return i;
 
                return i;
 
-       while (i > 0 && term.line[y][i - 1].u == ' ')
+       while (i > 0 && TLINE(y)[i - 1].u == ' ')
                --i;
 
        return i;
                --i;
 
        return i;
@@ -476,7 +472,7 @@ selextend(int col, int row, int type, int done)
        selnormalize();
        sel.type = type;
 
        selnormalize();
        sel.type = type;
 
-       if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type)
+       if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
                tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
 
        sel.mode = done ? SEL_IDLE : SEL_READY;
                tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
 
        sel.mode = done ? SEL_IDLE : SEL_READY;
@@ -531,7 +527,7 @@ selsnap(int *x, int *y, int direction)
 {
        int newx, newy, xt, yt;
        int delim, prevdelim;
 {
        int newx, newy, xt, yt;
        int delim, prevdelim;
-       Glyph *gp, *prevgp;
+       const Glyph *gp, *prevgp;
 
        switch (sel.snap) {
        case SNAP_WORD:
 
        switch (sel.snap) {
        case SNAP_WORD:
@@ -539,7 +535,7 @@ selsnap(int *x, int *y, int direction)
                 * Snap around if the word wraps around at the end or
                 * beginning of a line.
                 */
                 * Snap around if the word wraps around at the end or
                 * beginning of a line.
                 */
-               prevgp = &term.line[*y][*x];
+               prevgp = &TLINE(*y)[*x];
                prevdelim = ISDELIM(prevgp->u);
                for (;;) {
                        newx = *x + direction;
                prevdelim = ISDELIM(prevgp->u);
                for (;;) {
                        newx = *x + direction;
@@ -554,14 +550,14 @@ selsnap(int *x, int *y, int direction)
                                        yt = *y, xt = *x;
                                else
                                        yt = newy, xt = newx;
                                        yt = *y, xt = *x;
                                else
                                        yt = newy, xt = newx;
-                               if (!(term.line[yt][xt].mode & ATTR_WRAP))
+                               if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
                                        break;
                        }
 
                        if (newx >= tlinelen(newy))
                                break;
 
                                        break;
                        }
 
                        if (newx >= tlinelen(newy))
                                break;
 
-                       gp = &term.line[newy][newx];
+                       gp = &TLINE(newy)[newx];
                        delim = ISDELIM(gp->u);
                        if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
                                        || (delim && gp->u != prevgp->u)))
                        delim = ISDELIM(gp->u);
                        if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
                                        || (delim && gp->u != prevgp->u)))
@@ -582,14 +578,14 @@ selsnap(int *x, int *y, int direction)
                *x = (direction < 0) ? 0 : term.col - 1;
                if (direction < 0) {
                        for (; *y > 0; *y += direction) {
                *x = (direction < 0) ? 0 : term.col - 1;
                if (direction < 0) {
                        for (; *y > 0; *y += direction) {
-                               if (!(term.line[*y-1][term.col-1].mode
+                               if (!(TLINE(*y-1)[term.col-1].mode
                                                & ATTR_WRAP)) {
                                        break;
                                }
                        }
                } else if (direction > 0) {
                        for (; *y < term.row-1; *y += direction) {
                                                & ATTR_WRAP)) {
                                        break;
                                }
                        }
                } else if (direction > 0) {
                        for (; *y < term.row-1; *y += direction) {
-                               if (!(term.line[*y][term.col-1].mode
+                               if (!(TLINE(*y)[term.col-1].mode
                                                & ATTR_WRAP)) {
                                        break;
                                }
                                                & ATTR_WRAP)) {
                                        break;
                                }
@@ -604,7 +600,7 @@ getsel(void)
 {
        char *str, *ptr;
        int y, bufsize, lastx, linelen;
 {
        char *str, *ptr;
        int y, bufsize, lastx, linelen;
-       Glyph *gp, *last;
+       const Glyph *gp, *last;
 
        if (sel.ob.x == -1)
                return NULL;
 
        if (sel.ob.x == -1)
                return NULL;
@@ -620,13 +616,13 @@ getsel(void)
                }
 
                if (sel.type == SEL_RECTANGULAR) {
                }
 
                if (sel.type == SEL_RECTANGULAR) {
-                       gp = &term.line[y][sel.nb.x];
+                       gp = &TLINE(y)[sel.nb.x];
                        lastx = sel.ne.x;
                } else {
                        lastx = sel.ne.x;
                } else {
-                       gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
+                       gp = &TLINE(y)[sel.nb.y == y ? sel.nb.x : 0];
                        lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
                }
                        lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
                }
-               last = &term.line[y][MIN(lastx, linelen-1)];
+               last = &TLINE(y)[MIN(lastx, linelen-1)];
                while (last >= gp && last->u == ' ')
                        --last;
 
                while (last >= gp && last->u == ' ')
                        --last;
 
@@ -646,7 +642,8 @@ getsel(void)
                 * st.
                 * FIXME: Fix the computer world.
                 */
                 * st.
                 * FIXME: Fix the computer world.
                 */
-               if ((y < sel.ne.y || lastx >= linelen) && !(last->mode & ATTR_WRAP))
+               if ((y < sel.ne.y || lastx >= linelen) &&
+                   (!(last->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
                        *ptr++ = '\n';
        }
        *ptr = 0;
                        *ptr++ = '\n';
        }
        *ptr = 0;
@@ -677,7 +674,7 @@ die(const char *errstr, ...)
 void
 execsh(char *cmd, char **args)
 {
 void
 execsh(char *cmd, char **args)
 {
-       char *sh, *prog;
+       char *sh, *prog, *arg;
        const struct passwd *pw;
 
        errno = 0;
        const struct passwd *pw;
 
        errno = 0;
@@ -691,13 +688,20 @@ execsh(char *cmd, char **args)
        if ((sh = getenv("SHELL")) == NULL)
                sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
 
        if ((sh = getenv("SHELL")) == NULL)
                sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
 
-       if (args)
+       if (args) {
                prog = args[0];
                prog = args[0];
-       else if (utmp)
+               arg = NULL;
+       } else if (scroll) {
+               prog = scroll;
+               arg = utmp ? utmp : sh;
+       } else if (utmp) {
                prog = utmp;
                prog = utmp;
-       else
+               arg = NULL;
+       } else {
                prog = sh;
                prog = sh;
-       DEFAULT(args, ((char *[]) {prog, NULL}));
+               arg = NULL;
+       }
+       DEFAULT(args, ((char *[]) {prog, arg, NULL}));
 
        unsetenv("COLUMNS");
        unsetenv("LINES");
 
        unsetenv("COLUMNS");
        unsetenv("LINES");
@@ -731,9 +735,11 @@ sigchld(int a)
        if (pid != p)
                return;
 
        if (pid != p)
                return;
 
-       if (!WIFEXITED(stat) || WEXITSTATUS(stat))
-               die("child finished with error '%d'\n", stat);
-       exit(0);
+       if (WIFEXITED(stat) && WEXITSTATUS(stat))
+               die("child exited with status %d\n", WEXITSTATUS(stat));
+       else if (WIFSIGNALED(stat))
+               die("child terminated due to signal %d\n", WTERMSIG(stat));
+       _exit(0);
 }
 
 void
 }
 
 void
@@ -761,7 +767,7 @@ stty(char **args)
 }
 
 int
 }
 
 int
-ttynew(char *line, char *cmd, char *out, char **args)
+ttynew(const char *line, char *cmd, const char *out, char **args)
 {
        int m, s;
 
 {
        int m, s;
 
@@ -794,14 +800,15 @@ ttynew(char *line, char *cmd, char *out, char **args)
                break;
        case 0:
                close(iofd);
                break;
        case 0:
                close(iofd);
+               close(m);
                setsid(); /* create a new process group */
                dup2(s, 0);
                dup2(s, 1);
                dup2(s, 2);
                if (ioctl(s, TIOCSCTTY, NULL) < 0)
                        die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
                setsid(); /* create a new process group */
                dup2(s, 0);
                dup2(s, 1);
                dup2(s, 2);
                if (ioctl(s, TIOCSCTTY, NULL) < 0)
                        die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
-               close(s);
-               close(m);
+               if (s > 2)
+                       close(s);
 #ifdef __OpenBSD__
                if (pledge("stdio getpw proc exec", NULL) == -1)
                        die("pledge\n");
 #ifdef __OpenBSD__
                if (pledge("stdio getpw proc exec", NULL) == -1)
                        die("pledge\n");
@@ -826,27 +833,34 @@ ttyread(void)
 {
        static char buf[BUFSIZ];
        static int buflen = 0;
 {
        static char buf[BUFSIZ];
        static int buflen = 0;
-       int written;
-       int ret;
+       int ret, written;
 
        /* append read bytes to unprocessed bytes */
 
        /* append read bytes to unprocessed bytes */
-       if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
-               die("couldn't read from shell: %s\n", strerror(errno));
-       buflen += ret;
+       ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
 
 
-       written = twrite(buf, buflen, 0);
-       buflen -= written;
-       /* keep any uncomplete utf8 char for the next call */
-       if (buflen > 0)
-               memmove(buf, buf + written, buflen);
-
-       return ret;
+       switch (ret) {
+       case 0:
+               exit(0);
+       case -1:
+               die("couldn't read from shell: %s\n", strerror(errno));
+       default:
+               buflen += ret;
+               written = twrite(buf, buflen, 0);
+               buflen -= written;
+               /* keep any incomplete UTF-8 byte sequence for the next call */
+               if (buflen > 0)
+                       memmove(buf, buf + written, buflen);
+               return ret;
+       }
 }
 
 void
 ttywrite(const char *s, size_t n, int may_echo)
 {
        const char *next;
 }
 
 void
 ttywrite(const char *s, size_t n, int may_echo)
 {
        const char *next;
+       Arg arg = (Arg) { .i = term.scr };
+
+       kscrolldown(&arg);
 
        if (may_echo && IS_SET(MODE_ECHO))
                twrite(s, n, 1);
 
        if (may_echo && IS_SET(MODE_ECHO))
                twrite(s, n, 1);
@@ -1046,6 +1060,11 @@ tnew(int col, int row)
        treset();
 }
 
        treset();
 }
 
+int tisaltscr(void)
+{
+       return IS_SET(MODE_ALTSCREEN);
+}
+
 void
 tswapscreen(void)
 {
 void
 tswapscreen(void)
 {
@@ -1058,13 +1077,53 @@ tswapscreen(void)
 }
 
 void
 }
 
 void
-tscrolldown(int orig, int n)
+kscrolldown(const Arg* a)
+{
+       int n = a->i;
+
+       if (n < 0)
+               n = (term.row + n) / 2;
+
+       if (n > term.scr)
+               n = term.scr;
+
+       if (term.scr > 0) {
+               term.scr -= n;
+               selscroll(0, -n);
+               tfulldirt();
+       }
+}
+
+void
+kscrollup(const Arg* a)
+{
+       int n = a->i;
+
+       if (n < 0)
+               n = (term.row + n) / 2;
+
+       if (term.scr <= HISTSIZE-n) {
+               term.scr += n;
+               selscroll(0, n);
+               tfulldirt();
+       }
+}
+
+void
+tscrolldown(int orig, int n, int copyhist)
 {
        int i;
        Line temp;
 
        LIMIT(n, 0, term.bot-orig+1);
 
 {
        int i;
        Line temp;
 
        LIMIT(n, 0, term.bot-orig+1);
 
+       if (copyhist) {
+               term.histi = (term.histi - 1 + HISTSIZE) % HISTSIZE;
+               temp = term.hist[term.histi];
+               term.hist[term.histi] = term.line[term.bot];
+               term.line[term.bot] = temp;
+       }
+
        tsetdirt(orig, term.bot-n);
        tclearregion(0, term.bot-n+1, term.col-1, term.bot);
 
        tsetdirt(orig, term.bot-n);
        tclearregion(0, term.bot-n+1, term.col-1, term.bot);
 
@@ -1074,17 +1133,28 @@ tscrolldown(int orig, int n)
                term.line[i-n] = temp;
        }
 
                term.line[i-n] = temp;
        }
 
-       selscroll(orig, n);
+       if (term.scr == 0)
+               selscroll(orig, n);
 }
 
 void
 }
 
 void
-tscrollup(int orig, int n)
+tscrollup(int orig, int n, int copyhist)
 {
        int i;
        Line temp;
 
        LIMIT(n, 0, term.bot-orig+1);
 
 {
        int i;
        Line temp;
 
        LIMIT(n, 0, term.bot-orig+1);
 
+       if (copyhist) {
+               term.histi = (term.histi + 1) % HISTSIZE;
+               temp = term.hist[term.histi];
+               term.hist[term.histi] = term.line[orig];
+               term.line[orig] = temp;
+       }
+
+       if (term.scr > 0 && term.scr < HISTSIZE)
+               term.scr = MIN(term.scr + n, HISTSIZE-1);
+
        tclearregion(0, orig, term.col-1, orig+n-1);
        tsetdirt(orig+n, term.bot);
 
        tclearregion(0, orig, term.col-1, orig+n-1);
        tsetdirt(orig+n, term.bot);
 
@@ -1094,7 +1164,8 @@ tscrollup(int orig, int n)
                term.line[i+n] = temp;
        }
 
                term.line[i+n] = temp;
        }
 
-       selscroll(orig, -n);
+       if (term.scr == 0)
+               selscroll(orig, -n);
 }
 
 void
 }
 
 void
@@ -1103,27 +1174,17 @@ selscroll(int orig, int n)
        if (sel.ob.x == -1)
                return;
 
        if (sel.ob.x == -1)
                return;
 
-       if (BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
-               if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
+       if (BETWEEN(sel.nb.y, orig, term.bot) != BETWEEN(sel.ne.y, orig, term.bot)) {
+               selclear();
+       } else if (BETWEEN(sel.nb.y, orig, term.bot)) {
+               sel.ob.y += n;
+               sel.oe.y += n;
+               if (sel.ob.y < term.top || sel.ob.y > term.bot ||
+                   sel.oe.y < term.top || sel.oe.y > term.bot) {
                        selclear();
                        selclear();
-                       return;
-               }
-               if (sel.type == SEL_RECTANGULAR) {
-                       if (sel.ob.y < term.top)
-                               sel.ob.y = term.top;
-                       if (sel.oe.y > term.bot)
-                               sel.oe.y = term.bot;
                } else {
                } else {
-                       if (sel.ob.y < term.top) {
-                               sel.ob.y = term.top;
-                               sel.ob.x = 0;
-                       }
-                       if (sel.oe.y > term.bot) {
-                               sel.oe.y = term.bot;
-                               sel.oe.x = term.col;
-                       }
+                       selnormalize();
                }
                }
-               selnormalize();
        }
 }
 
        }
 }
 
@@ -1133,7 +1194,7 @@ tnewline(int first_col)
        int y = term.c.y;
 
        if (y == term.bot) {
        int y = term.c.y;
 
        if (y == term.bot) {
-               tscrollup(term.top, 1);
+               tscrollup(term.top, 1, 1);
        } else {
                y++;
        }
        } else {
                y++;
        }
@@ -1195,9 +1256,9 @@ tmoveto(int x, int y)
 }
 
 void
 }
 
 void
-tsetchar(Rune u, Glyph *attr, int x, int y)
+tsetchar(Rune u, const Glyph *attr, int x, int y)
 {
 {
-       static char *vt100_0[62] = { /* 0x41 - 0x7e */
+       static const char *vt100_0[62] = { /* 0x41 - 0x7e */
                "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
                0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
                0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
                "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
                0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
                0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
@@ -1298,18 +1359,18 @@ void
 tinsertblankline(int n)
 {
        if (BETWEEN(term.c.y, term.top, term.bot))
 tinsertblankline(int n)
 {
        if (BETWEEN(term.c.y, term.top, term.bot))
-               tscrolldown(term.c.y, n);
+               tscrolldown(term.c.y, n, 0);
 }
 
 void
 tdeleteline(int n)
 {
        if (BETWEEN(term.c.y, term.top, term.bot))
 }
 
 void
 tdeleteline(int n)
 {
        if (BETWEEN(term.c.y, term.top, term.bot))
-               tscrollup(term.c.y, n);
+               tscrollup(term.c.y, n, 0);
 }
 
 int32_t
 }
 
 int32_t
-tdefcolor(int *attr, int *npar, int l)
+tdefcolor(const int *attr, int *npar, int l)
 {
        int32_t idx = -1;
        uint r, g, b;
 {
        int32_t idx = -1;
        uint r, g, b;
@@ -1359,7 +1420,7 @@ tdefcolor(int *attr, int *npar, int l)
 }
 
 void
 }
 
 void
-tsetattr(int *attr, int l)
+tsetattr(const int *attr, int l)
 {
        int i;
        int32_t idx;
 {
        int i;
        int32_t idx;
@@ -1477,9 +1538,9 @@ tsetscroll(int t, int b)
 }
 
 void
 }
 
 void
-tsetmode(int priv, int set, int *args, int narg)
+tsetmode(int priv, int set, const int *args, int narg)
 {
 {
-       int alt, *lim;
+       int alt; const int *lim;
 
        for (lim = args + narg; args < lim; ++args) {
                if (priv) {
 
        for (lim = args + narg; args < lim; ++args) {
                if (priv) {
@@ -1573,6 +1634,7 @@ tsetmode(int priv, int set, int *args, int narg)
                        case 1015: /* urxvt mangled mouse mode; incompatible
                                      and can be mistaken for other control
                                      codes. */
                        case 1015: /* urxvt mangled mouse mode; incompatible
                                      and can be mistaken for other control
                                      codes. */
+                               break;
                        default:
                                fprintf(stderr,
                                        "erresc: unknown private set/reset mode %d\n",
                        default:
                                fprintf(stderr,
                                        "erresc: unknown private set/reset mode %d\n",
@@ -1654,6 +1716,12 @@ csihandle(void)
                if (csiescseq.arg[0] == 0)
                        ttywrite(vtiden, strlen(vtiden), 0);
                break;
                if (csiescseq.arg[0] == 0)
                        ttywrite(vtiden, strlen(vtiden), 0);
                break;
+       case 'b': /* REP -- if last char is printable print it <n> more times */
+               DEFAULT(csiescseq.arg[0], 1);
+               if (term.lastc)
+                       while (csiescseq.arg[0]-- > 0)
+                               tputc(term.lastc);
+               break;
        case 'C': /* CUF -- Cursor <n> Forward */
        case 'a': /* HPR -- Cursor <n> Forward */
                DEFAULT(csiescseq.arg[0], 1);
        case 'C': /* CUF -- Cursor <n> Forward */
        case 'a': /* HPR -- Cursor <n> Forward */
                DEFAULT(csiescseq.arg[0], 1);
@@ -1735,11 +1803,11 @@ csihandle(void)
                break;
        case 'S': /* SU -- Scroll <n> line up */
                DEFAULT(csiescseq.arg[0], 1);
                break;
        case 'S': /* SU -- Scroll <n> line up */
                DEFAULT(csiescseq.arg[0], 1);
-               tscrollup(term.top, csiescseq.arg[0]);
+               tscrollup(term.top, csiescseq.arg[0], 0);
                break;
        case 'T': /* SD -- Scroll <n> line down */
                DEFAULT(csiescseq.arg[0], 1);
                break;
        case 'T': /* SD -- Scroll <n> line down */
                DEFAULT(csiescseq.arg[0], 1);
-               tscrolldown(term.top, csiescseq.arg[0]);
+               tscrolldown(term.top, csiescseq.arg[0], 0);
                break;
        case 'L': /* IL -- Insert <n> blank lines */
                DEFAULT(csiescseq.arg[0], 1);
                break;
        case 'L': /* IL -- Insert <n> blank lines */
                DEFAULT(csiescseq.arg[0], 1);
@@ -1777,7 +1845,7 @@ csihandle(void)
                break;
        case 'n': /* DSR – Device Status Report (cursor position) */
                if (csiescseq.arg[0] == 6) {
                break;
        case 'n': /* DSR – Device Status Report (cursor position) */
                if (csiescseq.arg[0] == 6) {
-                       len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
+                       len = snprintf(buf, sizeof(buf), "\033[%i;%iR",
                                        term.c.y+1, term.c.x+1);
                        ttywrite(buf, len, 0);
                }
                                        term.c.y+1, term.c.x+1);
                        ttywrite(buf, len, 0);
                }
@@ -1814,7 +1882,7 @@ csihandle(void)
 void
 csidump(void)
 {
 void
 csidump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC[");
        uint c;
 
        fprintf(stderr, "ESC[");
@@ -1841,10 +1909,46 @@ csireset(void)
        memset(&csiescseq, 0, sizeof(csiescseq));
 }
 
        memset(&csiescseq, 0, sizeof(csiescseq));
 }
 
+void
+osc4_color_response(int num)
+{
+       int n;
+       char buf[32];
+       unsigned char r, g, b;
+
+       if (xgetcolor(num, &r, &g, &b)) {
+               fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num);
+               return;
+       }
+
+       n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
+                    num, r, r, g, g, b, b);
+
+       ttywrite(buf, n, 1);
+}
+
+void
+osc_color_response(int index, int num)
+{
+       int n;
+       char buf[32];
+       unsigned char r, g, b;
+
+       if (xgetcolor(index, &r, &g, &b)) {
+               fprintf(stderr, "erresc: failed to fetch osc color %d\n", index);
+               return;
+       }
+
+       n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
+                    num, r, r, g, g, b, b);
+
+       ttywrite(buf, n, 1);
+}
+
 void
 strhandle(void)
 {
 void
 strhandle(void)
 {
-       char *p = NULL;
+       char *p = NULL, *dec;
        int j, narg, par;
 
        term.esc &= ~(ESC_STR_END|ESC_STR);
        int j, narg, par;
 
        term.esc &= ~(ESC_STR_END|ESC_STR);
@@ -1855,15 +1959,21 @@ strhandle(void)
        case ']': /* OSC -- Operating System Command */
                switch (par) {
                case 0:
        case ']': /* OSC -- Operating System Command */
                switch (par) {
                case 0:
+                       if (narg > 1) {
+                               xsettitle(strescseq.args[1]);
+                               xseticontitle(strescseq.args[1]);
+                       }
+                       return;
                case 1:
                case 1:
+                       if (narg > 1)
+                               xseticontitle(strescseq.args[1]);
+                       return;
                case 2:
                        if (narg > 1)
                                xsettitle(strescseq.args[1]);
                        return;
                case 52:
                case 2:
                        if (narg > 1)
                                xsettitle(strescseq.args[1]);
                        return;
                case 52:
-                       if (narg > 2) {
-                               char *dec;
-
+                       if (narg > 2 && allowwindowops) {
                                dec = base64dec(strescseq.args[2]);
                                if (dec) {
                                        xsetsel(dec);
                                dec = base64dec(strescseq.args[2]);
                                if (dec) {
                                        xsetsel(dec);
@@ -1873,21 +1983,66 @@ strhandle(void)
                                }
                        }
                        return;
                                }
                        }
                        return;
+               case 10:
+                       if (narg < 2)
+                               break;
+
+                       p = strescseq.args[1];
+
+                       if (!strcmp(p, "?"))
+                               osc_color_response(defaultfg, 10);
+                       else if (xsetcolorname(defaultfg, p))
+                               fprintf(stderr, "erresc: invalid foreground color: %s\n", p);
+                       else
+                               tfulldirt();
+                       return;
+               case 11:
+                       if (narg < 2)
+                               break;
+
+                       p = strescseq.args[1];
+
+                       if (!strcmp(p, "?"))
+                               osc_color_response(defaultbg, 11);
+                       else if (xsetcolorname(defaultbg, p))
+                               fprintf(stderr, "erresc: invalid background color: %s\n", p);
+                       else
+                               tfulldirt();
+                       return;
+               case 12:
+                       if (narg < 2)
+                               break;
+
+                       p = strescseq.args[1];
+
+                       if (!strcmp(p, "?"))
+                               osc_color_response(defaultcs, 12);
+                       else if (xsetcolorname(defaultcs, p))
+                               fprintf(stderr, "erresc: invalid cursor color: %s\n", p);
+                       else
+                               tfulldirt();
+                       return;
                case 4: /* color set */
                        if (narg < 3)
                                break;
                        p = strescseq.args[2];
                        /* FALLTHROUGH */
                case 4: /* color set */
                        if (narg < 3)
                                break;
                        p = strescseq.args[2];
                        /* FALLTHROUGH */
-               case 104: /* color reset, here p = NULL */
+               case 104: /* color reset */
                        j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
                        j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
-                       if (xsetcolorname(j, p)) {
-                               fprintf(stderr, "erresc: invalid color %s\n", p);
+
+                       if (p && !strcmp(p, "?"))
+                               osc4_color_response(j);
+                       else if (xsetcolorname(j, p)) {
+                               if (par == 104 && narg <= 1)
+                                       return; /* color reset without parameter */
+                               fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
+                                       j, p ? p : "(null)");
                        } else {
                                /*
                                 * TODO if defaultbg color is changed, borders
                                 * are dirty
                                 */
                        } else {
                                /*
                                 * TODO if defaultbg color is changed, borders
                                 * are dirty
                                 */
-                               redraw();
+                               tfulldirt();
                        }
                        return;
                }
                        }
                        return;
                }
@@ -1896,7 +2051,6 @@ strhandle(void)
                xsettitle(strescseq.args[0]);
                return;
        case 'P': /* DCS -- Device Control String */
                xsettitle(strescseq.args[0]);
                return;
        case 'P': /* DCS -- Device Control String */
-               term.mode |= ESC_DCS;
        case '_': /* APC -- Application Program Command */
        case '^': /* PM -- Privacy Message */
                return;
        case '_': /* APC -- Application Program Command */
        case '^': /* PM -- Privacy Message */
                return;
@@ -1931,7 +2085,7 @@ strparse(void)
 void
 strdump(void)
 {
 void
 strdump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC%c", strescseq.type);
        uint c;
 
        fprintf(stderr, "ESC%c", strescseq.type);
@@ -1958,7 +2112,10 @@ strdump(void)
 void
 strreset(void)
 {
 void
 strreset(void)
 {
-       memset(&strescseq, 0, sizeof(strescseq));
+       strescseq = (STREscape){
+               .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
+               .siz = STR_BUF_SIZ,
+       };
 }
 
 void
 }
 
 void
@@ -2011,12 +2168,12 @@ void
 tdumpline(int n)
 {
        char buf[UTF_SIZ];
 tdumpline(int n)
 {
        char buf[UTF_SIZ];
-       Glyph *bp, *end;
+       const Glyph *bp, *end;
 
        bp = &term.line[n][0];
        end = &bp[MIN(tlinelen(n), term.col) - 1];
        if (bp != end || bp->u != ' ') {
 
        bp = &term.line[n][0];
        end = &bp[MIN(tlinelen(n), term.col) - 1];
        if (bp != end || bp->u != ' ') {
-               for ( ;bp <= end; ++bp)
+               for ( ; bp <= end; ++bp)
                        tprinter(buf, utf8encode(bp->u, buf));
        }
        tprinter("\n", 1);
                        tprinter(buf, utf8encode(bp->u, buf));
        }
        tprinter("\n", 1);
@@ -2087,12 +2244,9 @@ tdectest(char c)
 void
 tstrsequence(uchar c)
 {
 void
 tstrsequence(uchar c)
 {
-       strreset();
-
        switch (c) {
        case 0x90:   /* DCS -- Device Control String */
                c = 'P';
        switch (c) {
        case 0x90:   /* DCS -- Device Control String */
                c = 'P';
-               term.esc |= ESC_DCS;
                break;
        case 0x9f:   /* APC -- Application Program Command */
                c = '_';
                break;
        case 0x9f:   /* APC -- Application Program Command */
                c = '_';
@@ -2104,6 +2258,7 @@ tstrsequence(uchar c)
                c = ']';
                break;
        }
                c = ']';
                break;
        }
+       strreset();
        strescseq.type = c;
        term.esc |= ESC_STR;
 }
        strescseq.type = c;
        term.esc |= ESC_STR;
 }
@@ -2146,6 +2301,7 @@ tcontrolcode(uchar ascii)
                return;
        case '\032': /* SUB */
                tsetchar('?', &term.c.attr, term.c.x, term.c.y);
                return;
        case '\032': /* SUB */
                tsetchar('?', &term.c.attr, term.c.x, term.c.y);
+               /* FALLTHROUGH */
        case '\030': /* CAN */
                csireset();
                break;
        case '\030': /* CAN */
                csireset();
                break;
@@ -2241,7 +2397,7 @@ eschandle(uchar ascii)
                return 0;
        case 'D': /* IND -- Linefeed */
                if (term.c.y == term.bot) {
                return 0;
        case 'D': /* IND -- Linefeed */
                if (term.c.y == term.bot) {
-                       tscrollup(term.top, 1);
+                       tscrollup(term.top, 1, 1);
                } else {
                        tmoveto(term.c.x, term.c.y+1);
                }
                } else {
                        tmoveto(term.c.x, term.c.y+1);
                }
@@ -2254,7 +2410,7 @@ eschandle(uchar ascii)
                break;
        case 'M': /* RI -- Reverse index */
                if (term.c.y == term.top) {
                break;
        case 'M': /* RI -- Reverse index */
                if (term.c.y == term.top) {
-                       tscrolldown(term.top, 1);
+                       tscrolldown(term.top, 1, 1);
                } else {
                        tmoveto(term.c.x, term.c.y-1);
                }
                } else {
                        tmoveto(term.c.x, term.c.y-1);
                }
@@ -2262,7 +2418,7 @@ eschandle(uchar ascii)
        case 'Z': /* DECID -- Identify Terminal */
                ttywrite(vtiden, strlen(vtiden), 0);
                break;
        case 'Z': /* DECID -- Identify Terminal */
                ttywrite(vtiden, strlen(vtiden), 0);
                break;
-       case 'c': /* RIS -- Reset to inital state */
+       case 'c': /* RIS -- Reset to initial state */
                treset();
                resettitle();
                xloadcols();
                treset();
                resettitle();
                xloadcols();
@@ -2300,15 +2456,13 @@ tputc(Rune u)
        Glyph *gp;
 
        control = ISCONTROL(u);
        Glyph *gp;
 
        control = ISCONTROL(u);
-       if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
+       if (u < 127 || !IS_SET(MODE_UTF8)) {
                c[0] = u;
                width = len = 1;
        } else {
                len = utf8encode(u, c);
                c[0] = u;
                width = len = 1;
        } else {
                len = utf8encode(u, c);
-               if (!control && (width = wcwidth(u)) == -1) {
-                       memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
+               if (!control && (width = wcwidth(u)) == -1)
                        width = 1;
                        width = 1;
-               }
        }
 
        if (IS_SET(MODE_PRINT))
        }
 
        if (IS_SET(MODE_PRINT))
@@ -2323,25 +2477,12 @@ tputc(Rune u)
        if (term.esc & ESC_STR) {
                if (u == '\a' || u == 030 || u == 032 || u == 033 ||
                   ISCONTROLC1(u)) {
        if (term.esc & ESC_STR) {
                if (u == '\a' || u == 030 || u == 032 || u == 033 ||
                   ISCONTROLC1(u)) {
-                       term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
-                       if (IS_SET(MODE_SIXEL)) {
-                               /* TODO: render sixel */;
-                               term.mode &= ~MODE_SIXEL;
-                               return;
-                       }
+                       term.esc &= ~(ESC_START|ESC_STR);
                        term.esc |= ESC_STR_END;
                        goto check_control_code;
                }
 
                        term.esc |= ESC_STR_END;
                        goto check_control_code;
                }
 
-
-               if (IS_SET(MODE_SIXEL)) {
-                       /* TODO: implement sixel mode */
-                       return;
-               }
-               if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
-                       term.mode |= MODE_SIXEL;
-
-               if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
+               if (strescseq.len+len >= strescseq.siz) {
                        /*
                         * Here is a bug in terminals. If the user never sends
                         * some code to stop the str or esc command, then st
                        /*
                         * Here is a bug in terminals. If the user never sends
                         * some code to stop the str or esc command, then st
@@ -2355,7 +2496,10 @@ tputc(Rune u)
                         * term.esc = 0;
                         * strhandle();
                         */
                         * term.esc = 0;
                         * strhandle();
                         */
-                       return;
+                       if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
+                               return;
+                       strescseq.siz *= 2;
+                       strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
                }
 
                memmove(&strescseq.buf[strescseq.len], c, len);
                }
 
                memmove(&strescseq.buf[strescseq.len], c, len);
@@ -2374,6 +2518,8 @@ check_control_code:
                /*
                 * control codes are not shown ever
                 */
                /*
                 * control codes are not shown ever
                 */
+               if (!term.esc)
+                       term.lastc = 0;
                return;
        } else if (term.esc & ESC_START) {
                if (term.esc & ESC_CSI) {
                return;
        } else if (term.esc & ESC_START) {
                if (term.esc & ESC_CSI) {
@@ -2404,7 +2550,7 @@ check_control_code:
                 */
                return;
        }
                 */
                return;
        }
-       if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
+       if (selected(term.c.x, term.c.y))
                selclear();
 
        gp = &term.line[term.c.y][term.c.x];
                selclear();
 
        gp = &term.line[term.c.y][term.c.x];
@@ -2423,10 +2569,15 @@ check_control_code:
        }
 
        tsetchar(u, &term.c.attr, term.c.x, term.c.y);
        }
 
        tsetchar(u, &term.c.attr, term.c.x, term.c.y);
+       term.lastc = u;
 
        if (width == 2) {
                gp->mode |= ATTR_WIDE;
                if (term.c.x+1 < term.col) {
 
        if (width == 2) {
                gp->mode |= ATTR_WIDE;
                if (term.c.x+1 < term.col) {
+                       if (gp[1].mode == ATTR_WIDE && term.c.x+2 < term.col) {
+                               gp[2].u = ' ';
+                               gp[2].mode &= ~ATTR_WDUMMY;
+                       }
                        gp[1].u = '\0';
                        gp[1].mode = ATTR_WDUMMY;
                }
                        gp[1].u = '\0';
                        gp[1].mode = ATTR_WDUMMY;
                }
@@ -2446,7 +2597,7 @@ twrite(const char *buf, int buflen, int show_ctrl)
        int n;
 
        for (n = 0; n < buflen; n += charsize) {
        int n;
 
        for (n = 0; n < buflen; n += charsize) {
-               if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
+               if (IS_SET(MODE_UTF8)) {
                        /* process a complete utf8 char */
                        charsize = utf8decode(buf + n, &u, buflen - n);
                        if (charsize == 0)
                        /* process a complete utf8 char */
                        charsize = utf8decode(buf + n, &u, buflen - n);
                        if (charsize == 0)
@@ -2473,7 +2624,7 @@ twrite(const char *buf, int buflen, int show_ctrl)
 void
 tresize(int col, int row)
 {
 void
 tresize(int col, int row)
 {
-       int i;
+       int i, j;
        int minrow = MIN(row, term.row);
        int mincol = MIN(col, term.col);
        int *bp;
        int minrow = MIN(row, term.row);
        int mincol = MIN(col, term.col);
        int *bp;
@@ -2510,6 +2661,14 @@ tresize(int col, int row)
        term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
        term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
 
        term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
        term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
 
+       for (i = 0; i < HISTSIZE; i++) {
+               term.hist[i] = xrealloc(term.hist[i], col * sizeof(Glyph));
+               for (j = mincol; j < col; j++) {
+                       term.hist[i][j] = term.c.attr;
+                       term.hist[i][j].u = ' ';
+               }
+       }
+
        /* resize each row to new width, zero-pad if needed */
        for (i = 0; i < minrow; i++) {
                term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
        /* resize each row to new width, zero-pad if needed */
        for (i = 0; i < minrow; i++) {
                term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
@@ -2562,19 +2721,20 @@ void
 drawregion(int x1, int y1, int x2, int y2)
 {
        int y;
 drawregion(int x1, int y1, int x2, int y2)
 {
        int y;
+
        for (y = y1; y < y2; y++) {
                if (!term.dirty[y])
                        continue;
 
                term.dirty[y] = 0;
        for (y = y1; y < y2; y++) {
                if (!term.dirty[y])
                        continue;
 
                term.dirty[y] = 0;
-               xdrawline(term.line[y], x1, y, x2);
+               xdrawline(TLINE(y), x1, y, x2);
        }
 }
 
 void
 draw(void)
 {
        }
 }
 
 void
 draw(void)
 {
-       int cx = term.c.x;
+       int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
 
        if (!xstartdraw())
                return;
 
        if (!xstartdraw())
                return;
@@ -2588,10 +2748,14 @@ draw(void)
                cx--;
 
        drawregion(0, 0, term.col, term.row);
                cx--;
 
        drawregion(0, 0, term.col, term.row);
-       xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
-                       term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
-       term.ocx = cx, term.ocy = term.c.y;
+       if (term.scr == 0)
+               xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
+                               term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
+       term.ocx = cx;
+       term.ocy = term.c.y;
        xfinishdraw();
        xfinishdraw();
+       if (ocx != term.ocx || ocy != term.ocy)
+               xximspot(term.ocx, term.ocy);
 }
 
 void
 }
 
 void