From: NeilBrown Date: Fri, 8 Sep 2023 05:35:51 +0000 (+1000) Subject: input: Add Keystroke-sequence interface X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=140cd989a8e8ae70718d84928d56f4b42332a664;p=edlib.git input: Add Keystroke-sequence interface Rather than having two copies for code to convert a string (from a menu selection) to a sequence of keystrokes, put the code in lib-input. As the keys are space-separated, allow :SPC to be used for a space "- ". Signed-off-by: NeilBrown --- diff --git a/DOC/TODO.md b/DOC/TODO.md index 698954d2..ecdcd66c 100644 --- a/DOC/TODO.md +++ b/DOC/TODO.md @@ -824,6 +824,7 @@ Module features ### lib-menu +- [ ] remove that blank line at the end of menus - [X] Enable lib-menu to show short-cut keys - [X] menu-bar to which we can add menus from which commands are sent - [ ] track movement so entry under cursor can be highlighted diff --git a/lib-input.c b/lib-input.c index 7cf94553..49670772 100644 --- a/lib-input.c +++ b/lib-input.c @@ -161,6 +161,7 @@ static const char *safe ctrl_map[][2] = { { ":A:LF", ":A:C-J" }, { ":A:Tab", ":A:C-I" }, { ":A:Del", ":A:C-?" }, + { ":SPC", "- " }, }; static const char *map_key(const char *key safe) @@ -227,6 +228,35 @@ DEF_CMD(keystroke) return Efallthrough; } +DEF_CMD(keystroke_sequence) +{ + struct pane *home = ci->home; + const char *c = ci->str; + const char *e, *dash; + int ret; + + if (!c) + return Enoarg; + while ((e = strchr(c, ' ')) != NULL) { + dash = ""; + if (*c != ':' || e == c+1) + dash = "-"; + ret = call("Keystroke", home, 0, NULL, + strconcat(home, dash, + strnsave(home, c, e - c))); + if (ret < 0) + return Efail; + c = e+1; + } + dash = ""; + if (*c != ':' || c[1] == '\0') + dash = "-"; + ret = call("Keystroke", home, 0, NULL, strconcat(home, dash, c)); + if (ret < 0) + return Efail; + return 1; +} + static int tspec_diff_ms(struct timespec *a safe, struct timespec *b safe) { return ((a->tv_sec - b->tv_sec) * 1000 + @@ -465,6 +495,7 @@ static void register_map(void) return; im_map = key_alloc(); key_add(im_map, "Keystroke", &keystroke); + key_add(im_map, "Keystroke-sequence", &keystroke_sequence); key_add(im_map, "Mouse-event", &mouse_event); key_add(im_map, "Mouse-grab", &mouse_grab); key_add(im_map, "Mode:set-mode", &set_mode); diff --git a/lib-menubar.c b/lib-menubar.c index ad72d55f..6af799d0 100644 --- a/lib-menubar.c +++ b/lib-menubar.c @@ -283,33 +283,11 @@ DEF_CMD(menubar_done) { struct pane *home = ci->home; struct mbinfo *mbi = home->data; - char *dash; - const char *c, *e; if (mbi->child) pane_focus(mbi->child); - if (!ci->str) - /* Abort ?? */ - return 1; - c = ci->str; - while ((e = strchr(c, ' ')) != NULL) { - int ret; - - dash = ""; - if (*c != ':' || e == c+1) - dash = "-"; - ret = call("Keystroke", home, 0, NULL, - strconcat(home, dash, - strnsave(home, c, e - c))); - if (ret < 0) - return Efail; - c = e+1; - } - dash = ""; - if (*c != ':' || c[1] == '\0') - dash = "-"; - return call("Keystroke", home, 0, NULL, - strconcat(home, dash, c)); + call("Keystroke-sequence", home, 0, NULL, ci->str); + return 1; } DEF_CMD(menubar_root) diff --git a/mode-emacs.c b/mode-emacs.c index 119ff35a..d344fda3 100644 --- a/mode-emacs.c +++ b/mode-emacs.c @@ -2389,8 +2389,6 @@ DEF_CMD(emacs_selection_menu) DEF_CMD(emacs_selection_menu_action) { struct pane *home = ci->home; - char *dash; - const char *e; const char *c = ci->str; if (!c) @@ -2401,23 +2399,7 @@ DEF_CMD(emacs_selection_menu_action) return 1; } - while ((e = strchr(c, ' ')) != NULL) { - int ret; - - dash = ""; - if (*c != ':' || e == c+1) - dash = "-"; - ret = call("Keystroke", home, 0, NULL, - strconcat(home, dash, - strnsave(home, c, e - c))); - if (ret < 0) - return Efail; - c = e+1; - } - dash = ""; - if (*c != ':' || c[1] == '\0') - dash = "-"; - call("Keystroke", home, 0, NULL, strconcat(home, dash, c)); + call("Keystroke-sequence", home, 0, NULL, c); return 1; }