]> Sergey Matveev's repositories - st.git/blobdiff - st.c
Fix cursor move with wide glyphs
[st.git] / st.c
diff --git a/st.c b/st.c
index 6ba467d7cc903d90f1aa1b5aa01da592ad1b30e6..77c3e8a8d8d16e3004760a0432af437065e41fb6 100644 (file)
--- a/st.c
+++ b/st.c
@@ -86,8 +86,8 @@ enum escape_state {
 
 typedef struct {
        Glyph attr; /* current char attributes */
-       int x;
-       int y;
+       int x; /* terminal column */
+       int y; /* terminal row */
        char state;
 } TCursor;
 
@@ -939,7 +939,7 @@ ttyresize(int tw, int th)
 }
 
 void
-ttyhangup()
+ttyhangup(void)
 {
        /* Send SIGHUP to shell */
        kill(pid, SIGHUP);
@@ -1097,7 +1097,7 @@ tscrollup(int orig, int n)
 void
 selscroll(int orig, int n)
 {
-       if (sel.ob.x == -1)
+       if (sel.ob.x == -1 || sel.alt != IS_SET(MODE_ALTSCREEN))
                return;
 
        if (BETWEEN(sel.nb.y, orig, term.bot) != BETWEEN(sel.ne.y, orig, term.bot)) {
@@ -1728,6 +1728,7 @@ csihandle(void)
                }
                break;
        case 'S': /* SU -- Scroll <n> line up */
+               if (csiescseq.priv) break;
                DEFAULT(csiescseq.arg[0], 1);
                tscrollup(term.top, csiescseq.arg[0]);
                break;
@@ -1769,11 +1770,18 @@ csihandle(void)
        case 'm': /* SGR -- Terminal attribute (color) */
                tsetattr(csiescseq.arg, csiescseq.narg);
                break;
-       case 'n': /* DSR – Device Status Report (cursor position) */
-               if (csiescseq.arg[0] == 6) {
+       case 'n': /* DSR -- Device Status Report */
+               switch (csiescseq.arg[0]) {
+               case 5: /* Status Report "OK" `0n` */
+                       ttywrite("\033[0n", sizeof("\033[0n") - 1, 0);
+                       break;
+               case 6: /* Report Cursor Position (CPR) "<row>;<column>R" */
                        len = snprintf(buf, sizeof(buf), "\033[%i;%iR",
-                                       term.c.y+1, term.c.x+1);
+                                      term.c.y+1, term.c.x+1);
                        ttywrite(buf, len, 0);
+                       break;
+               default:
+                       goto unknown;
                }
                break;
        case 'r': /* DECSTBM -- Set Scrolling Region */
@@ -1932,8 +1940,10 @@ strhandle(void)
                        if (p && !strcmp(p, "?")) {
                                osc_color_response(j, 0, 1);
                        } else if (xsetcolorname(j, p)) {
-                               if (par == 104 && narg <= 1)
+                               if (par == 104 && narg <= 1) {
+                                       xloadcols();
                                        return; /* color reset without parameter */
+                               }
                                fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
                                        j, p ? p : "(null)");
                        } else {
@@ -2165,12 +2175,16 @@ tstrsequence(uchar c)
 void
 tcontrolcode(uchar ascii)
 {
+       size_t i;
+
        switch (ascii) {
        case '\t':   /* HT */
                tputtab(1);
                return;
        case '\b':   /* BS */
-               tmoveto(term.c.x-1, term.c.y);
+               for (i = 1; term.c.x && term.line[term.c.y][term.c.x - i].u == 0; ++i)
+                       ;
+               tmoveto(term.c.x - i, term.c.y);
                return;
        case '\r':   /* CR */
                tmoveto(0, term.c.y);
@@ -2321,6 +2335,7 @@ eschandle(uchar ascii)
                treset();
                resettitle();
                xloadcols();
+               xsetmode(0, MODE_HIDE);
                break;
        case '=': /* DECPAM -- Application keypad */
                xsetmode(1, MODE_APPKEYPAD);
@@ -2413,6 +2428,9 @@ check_control_code:
         * they must not cause conflicts with sequences.
         */
        if (control) {
+               /* in UTF-8 mode ignore handling C1 control characters */
+               if (IS_SET(MODE_UTF8) && ISCONTROLC1(u))
+                       return;
                tcontrolcode(u);
                /*
                 * control codes are not shown ever
@@ -2459,11 +2477,16 @@ check_control_code:
                gp = &term.line[term.c.y][term.c.x];
        }
 
-       if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
+       if (IS_SET(MODE_INSERT) && term.c.x+width < term.col) {
                memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
+               gp->mode &= ~ATTR_WIDE;
+       }
 
        if (term.c.x+width > term.col) {
-               tnewline(1);
+               if (IS_SET(MODE_WRAP))
+                       tnewline(1);
+               else
+                       tmoveto(term.col - width, term.c.y);
                gp = &term.line[term.c.y][term.c.x];
        }