Allow replacement of UTF-8 characters in vi mode.
authoranton <anton@openbsd.org>
Wed, 31 May 2017 06:59:12 +0000 (06:59 +0000)
committeranton <anton@openbsd.org>
Wed, 31 May 2017 06:59:12 +0000 (06:59 +0000)
Reported by Walter Alejandro Iglesias on tech@.

ok schwarze@ tb@

bin/ksh/vi.c
regress/bin/ksh/vi/vi.sh

index fd6c325..ec8aa99 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vi.c,v 1.45 2017/05/28 07:27:01 anton Exp $   */
+/*     $OpenBSD: vi.c,v 1.46 2017/05/31 06:59:12 anton Exp $   */
 
 /*
  *     vi command editing
@@ -926,13 +926,24 @@ vi_cmd(int argcnt, const char *cmd)
                        if (cmd[1] == 0)
                                vi_error();
                        else {
-                               int     n;
-
-                               if (es->cursor + argcnt > es->linelen)
+                               c1 = 0;
+                               for (cur = es->cursor;
+                                   cur < es->linelen; cur++) {
+                                       if (!isu8cont(es->cbuf[cur]))
+                                               c1++;
+                                       if (c1 > argcnt)
+                                               break;
+                               }
+                               if (argcnt > c1)
                                        return -1;
-                               for (n = 0; n < argcnt; ++n)
-                                       es->cbuf[es->cursor + n] = cmd[1];
-                               es->cursor += n - 1;
+
+                               del_range(es->cursor, cur);
+                               while (argcnt-- > 0)
+                                       putbuf(&cmd[1], 1, 0);
+                               while (es->cursor > 0)
+                                       if (!isu8cont(es->cbuf[--es->cursor]))
+                                               break;
+                               es->cbuf[es->linelen] = '\0';
                        }
                        break;
 
index 710528a..7a4603a 100644 (file)
@@ -134,6 +134,9 @@ testseq "abcdef\00334h2Rxy\0033" " $ abcdef\b\b\b\b\bxyxy\b\r\naxyxyf"
 
 # r: Replace character.
 testseq "abcd\00332h2rxiy" " $ abcd\b\b\bxx\byxd\b\b\r\naxyxd"
+testseq "\0303\0266\0033ro" " $ \0303\0266\bo \b\b\r\no"
+testseq "\0342\0202\0254\0033ro" " $ \0342\0202\0254\bo  \b\b\b\r\no"
+testseq "\0303\0266\00332ro" " $ \0303\0266\b\a\r\n\0303\0266"
 
 # S: Substitute whole line.
 testseq "oldst\0033Snew" " $ oldst\b\b\b\b\b     \r $ new\r\nnew"