commit 57668c86437fcb90f5f22d11227c117189edb641 from: Omar Polo date: Fri Nov 05 20:54:38 2021 UTC extract the key matching logic to its own function commit - cc9bf8f2ade16cc562056941de3e547886c6a928 commit + 57668c86437fcb90f5f22d11227c117189edb641 blob - 697a6d48e0bf491819a293bb15a8a1d54fefc2bc blob + 3a75c29e16e96a2fa4512edc1d9ca56b500fcd2a --- keymap.c +++ keymap.c @@ -195,3 +195,23 @@ again: return 1; } +int +lookup_key(struct kmap **map, struct thiskey *key, struct buffer *buf) +{ + struct keymap *k; + + TAILQ_FOREACH(k, &(*map)->m, keymaps) { + if (k->meta == key->meta && + k->key == key->key) { + if (k->fn == NULL) { + *map = &k->map; + return LK_ADVANCED_MAP; + } else { + k->fn(buf); + return LK_MATCHED; + } + } + } + + return LK_UNBOUND; +} blob - 44623d3f29e0d7c12e89998cdc9b6cbfde7d4291 blob + 131d2ab70be1e6397c32ef510771d647f2e5176f --- telescope.h +++ telescope.h @@ -270,6 +270,12 @@ struct keymap { interactivefn *fn; TAILQ_ENTRY(keymap) keymaps; +}; + +struct thiskey { + short meta; + int key; + uint32_t cp; }; struct cmd { @@ -311,6 +317,12 @@ void hist_push(struct histhead*, struct hist*); struct hist *hist_pop(struct histhead *); /* keymap.c */ +enum { + LK_ADVANCED_MAP, + LK_MATCHED, + LK_UNBOUND, +}; + int kbd(const char*); const char *unkbd(int); int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*)); blob - e2364d68c2f52581a94ee1e7259bea98722860b7 blob + 8013a0b3afc904f58baa021cdfeabe7bef2dfa82 --- ui.c +++ ui.c @@ -229,7 +229,7 @@ readkey(void) static void dispatch_stdio(int fd, short ev, void *d) { - struct keymap *k; + int lk; const char *keyname; char tmp[5] = {0}; @@ -254,29 +254,18 @@ dispatch_stdio(int fd, short ev, void *d) strlcat(keybuf, tmp, sizeof(keybuf)); } - TAILQ_FOREACH(k, ¤t_map->m, keymaps) { - if (k->meta == thiskey.meta && - k->key == thiskey.key) { - if (k->fn == NULL) - current_map = &k->map; - else { - current_map = base_map; - strlcpy(keybuf, "", sizeof(keybuf)); - k->fn(current_buffer()); - } - goto done; - } + lk = lookup_key(¤t_map, &thiskey, current_buffer()); + if (lk == LK_UNBOUND) { + if (current_map->unhandled_input != NULL) + current_map->unhandled_input(); + else + global_key_unbound(); } - - if (current_map->unhandled_input != NULL) - current_map->unhandled_input(); - else - global_key_unbound(); - - strlcpy(keybuf, "", sizeof(keybuf)); - current_map = base_map; + if (lk != LK_ADVANCED_MAP) { + current_map = base_map; + strlcpy(keybuf, "", sizeof(keybuf)); + } -done: if (side_window & SIDE_WINDOW_LEFT) recompute_help(); blob - 304d4349918e66bd89a4f073f46105e41eb35e22 blob + 82e8630ac9081bd7ae4242127e007b2b5ba664b5 --- ui.h +++ ui.h @@ -119,11 +119,6 @@ enum pairs { PMINIBUF, }; -struct thiskey { - short meta; - int key; - uint32_t cp; -}; extern struct thiskey thiskey; extern struct tab *current_tab;