Improve ctrl-w
authorSimon Andersson <simon.andersson@magine.com>
Fri, 24 Jul 2015 21:24:50 +0000 (23:24 +0200)
committerSimon Andersson <simon.andersson@magine.com>
Fri, 24 Jul 2015 21:24:50 +0000 (23:24 +0200)
Stop at word boundry, leaving the trailing space, the way ctrl-w usually
works.

src/curses/ux_input.c

index 216015912f76b6ed20863f7f849506a7027efdc4..f27cef9c87831796fb8c5f3fbbc288a252a7176f 100644 (file)
@@ -450,18 +450,16 @@ zchar os_read_line (int max, zchar *buf, int timeout, int width, int continued)
            break;
        case ZC_DEL_WORD:
                if (scrpos != 0) {
-                       /* Search for start of preceding word */
-                       int i;
-                       for (i = scrpos - 1; i > 0 && buf[i] != ' '; i--) {}
-
+                       int newoffset = start_of_prev_word(scrpos, buf);
                        searchpos = -1;
-                       int delta = scrpos - i;
+                       int delta = scrpos - newoffset;
                        int oldlen = len;
                        int oldscrpos = scrpos;
                        len -= delta;
                        scrpos -= delta;
                        scrnmove(x + scrpos, x + oldscrpos, len - scrpos);
                        memmove(buf + scrpos, buf + oldscrpos, len - scrpos);
+                       int i = newoffset;
                        for (i = len; i <= oldlen ; i++) {
                                mvaddch(y, x + i, ' ');
                        }
@@ -698,3 +696,21 @@ void *memmove(void *s, void *t, size_t n)
 }
 
 #endif /* NO_MEMMOVE */
+
+
+/*
+ * Search for start of preceding word
+ * param currpos marker position
+ * param buf input buffer
+ * returns new position
+ */
+int start_of_prev_word(int currpos, const zchar* buf) {
+       int i, j;
+       for (i = currpos - 1; i > 0 && buf[i] == ' '; i--) {}
+       j = i;
+       for (; i > 0 && buf[i] != ' '; i--) {}
+       if (i < j && i != 0) {
+               i += 1;
+       }
+       return i;
+}