Add key bindings for jumping wordwise
authorSimon Andersson <simon.andersson@magine.com>
Fri, 24 Jul 2015 22:09:15 +0000 (00:09 +0200)
committerSimon Andersson <simon.andersson@magine.com>
Fri, 24 Jul 2015 22:09:15 +0000 (00:09 +0200)
meta-f, meta-b, works as in emacs/readline

src/common/frotz.h
src/curses/ux_input.c

index 19e1e4dbded00ebab4123d57a16cb482057e2928..a3c70c734cbee70014357671893420222532367c 100644 (file)
@@ -285,6 +285,8 @@ typedef struct {
 #define ZC_HKEY_MAX 0x15
 #define ZC_ESCAPE 0x1b
 #define ZC_DEL_WORD 0x1c
+#define ZC_WORD_RIGHT 0x1d
+#define ZC_WORD_LEFT 0x1e
 #define ZC_ASCII_MIN 0x20
 #define ZC_ASCII_MAX 0x7e
 #define ZC_BAD 0x7f
index f27cef9c87831796fb8c5f3fbbc288a252a7176f..a681742570ed2ebf14e52fc8d789ab119c506f3a 100644 (file)
@@ -187,6 +187,8 @@ static int unix_read_char(int extkeys)
            case 'x': return ZC_HKEY_QUIT;
            case 'd': return ZC_HKEY_DEBUG;
            case 'h': return ZC_HKEY_HELP;
+           case 'f': return ZC_WORD_RIGHT;
+           case 'b': return ZC_WORD_LEFT;
            default: continue;  /* Ignore unknown combinations. */
            }
        /* The standard function key block. */
@@ -225,6 +227,8 @@ static int unix_read_char(int extkeys)
        case MOD_META | 'x': return ZC_HKEY_QUIT;
        case MOD_META | 'd': return ZC_HKEY_DEBUG;
        case MOD_META | 'h': return ZC_HKEY_HELP;
+       case MOD_META | 'f': return ZC_WORD_RIGHT;
+       case MOD_META | 'b': return ZC_WORD_LEFT;
 
 /* these are the emacs-editing characters */
        case MOD_CTRL ^ 'B': return ZC_ARROW_LEFT;
@@ -491,7 +495,16 @@ zchar os_read_line (int max, zchar *buf, int timeout, int width, int continued)
        case ZC_ARROW_RIGHT: if (scrpos < len) scrpos++; continue;
        case KEY_HOME: scrpos = 0; continue;
        case KEY_END: scrpos = len; continue;
-
+       case ZC_WORD_RIGHT:
+               if (scrpos < len) {
+                       scrpos = end_of_next_word(scrpos, buf, len);
+               }
+               continue;
+       case ZC_WORD_LEFT:
+               if (scrpos > 0) {
+                       scrpos = start_of_prev_word(scrpos, buf);
+               }
+               continue;
        case KEY_IC:            /* Insert Character */
            insert_flag = !insert_flag;
            continue;
@@ -714,3 +727,18 @@ int start_of_prev_word(int currpos, const zchar* buf) {
        }
        return i;
 }
+
+/*
+ * Search for end of next word
+ * param currpos marker position
+ * param buf input buffer
+ * param len length of buf
+ * returns new position
+ */
+int end_of_next_word(int currpos, const zchar* buf, int len) {
+       int i, j;
+       for (i = currpos; i < len && buf[i] == ' '; i++) {}
+       j = i;
+       for (; i < len && buf[i] != ' '; i++) {}
+       return i;
+}