diff options
| -rw-r--r-- | main.c | 8 | ||||
| -rw-r--r-- | vis-core.h | 1 | ||||
| -rw-r--r-- | vis.c | 34 | ||||
| -rw-r--r-- | vis.h | 14 |
4 files changed, 31 insertions, 26 deletions
@@ -1621,8 +1621,8 @@ static const char *replace(Vis *vis, const char *keys, const Arg *arg) { return NULL; vis_operator(vis, VIS_OP_REPLACE); vis_motion(vis, VIS_MOVE_NOP); - vis_keys_push(vis, keys); - vis_keys_push(vis, "<Escape>"); + vis_keys_feed(vis, keys); + vis_keys_feed(vis, "<Escape>"); return next; } @@ -1944,10 +1944,10 @@ static const char *openline(Vis *vis, const char *keys, const Arg *arg) { vis_operator(vis, VIS_OP_INSERT); if (arg->i > 0) { vis_motion(vis, VIS_MOVE_LINE_END); - vis_keys_push(vis, "<insert-newline>"); + vis_keys_feed(vis, "<insert-newline>"); } else { vis_motion(vis, VIS_MOVE_LINE_BEGIN); - vis_keys_push(vis, "<insert-newline><Up>"); + vis_keys_feed(vis, "<insert-newline><Up>"); } return keys; } @@ -158,6 +158,7 @@ struct Vis { Map *keymap; /* key translation before any bindings are matched */ Buffer input_queue; /* holds pending input keys */ Buffer *keys; /* currently active keys buffer (either the input_queue or a macro) */ + bool keyhandler; /* whether a key handling function is currently being called */ Action action; /* current action which is in progress */ Action action_prev; /* last operator action used by the repeat (dot) command */ Mode *mode; /* currently active mode, used to search for keybindings */ @@ -696,21 +696,19 @@ const char *vis_keys_next(Vis *vis, const char *keys) { return termkey_strpkey(termkey, keys, &key, TERMKEY_FORMAT_VIM); } -void vis_keys_input(Vis *vis, const char *input) { - vis_keys_push(vis, input); - vis_keys_process(vis); -} - static void vis_keys_process(Vis *vis) { Buffer *buf = vis->keys; char *keys = buf->data, *start = keys, *cur = keys, *end; - bool prefix = false; + bool prefix = false, unknown_key = false; KeyBinding *binding = NULL; + vis->keyhandler = true; while (cur && *cur) { - if (!(end = (char*)vis_keys_next(vis, cur))) - goto err; // XXX: can't parse key this should never happen + if (!(end = (char*)vis_keys_next(vis, cur))) { + unknown_key = true; + goto out; + } char tmp = *end; *end = '\0'; @@ -765,13 +763,15 @@ static void vis_keys_process(Vis *vis) { } } - buffer_put0(buf, start); - return; -err: - buffer_truncate(buf); +out: + if (unknown_key) + buffer_truncate(buf); + else + buffer_put0(buf, start); + vis->keyhandler = false; } -void vis_keys_push(Vis *vis, const char *input) { +void vis_keys_feed(Vis *vis, const char *input) { if (!input) return; if (vis->recording) @@ -780,6 +780,12 @@ void vis_keys_push(Vis *vis, const char *input) { macro_append(vis->macro_operator, input); if (!buffer_append0(vis->keys, input)) buffer_truncate(vis->keys); + /* if we are being called from within a keyhandler then appending + * the new keys to the end of the input queue is enough. they will + * be interpreted once the key handler returns and control reaches + * back to the vis_keys_process function. */ + if (!vis->keyhandler) + vis_keys_process(vis); } static const char *getkey(Vis *vis) { @@ -927,7 +933,7 @@ int vis_run(Vis *vis, int argc, char *argv[]) { const char *key; while ((key = getkey(vis))) - vis_keys_input(vis, key); + vis_keys_feed(vis, key); if (vis->mode->idle) timeout = &idle; @@ -429,14 +429,12 @@ const char *vis_keys_next(Vis*, const char *keys); * queue (or a previously recorded macro) to key handling functions (see struct * KeyAction) which consume the input. * - * this functions pushes/appends further input to the end of the input queue - * and immediately tries to interpret them */ -void vis_keys_input(Vis*, const char *input); -/* like the above, but only pushes the input to the input queue, does *not* - * try to interpret the contents of the input queue. intended to be used from - * key handlers. the remaining queue content will be interpreted once the - * key handler returns. */ -void vis_keys_push(Vis*, const char *input); + * this functions pushes/appends further input to the end of the input queue. + * if it is called from within a key handling function itself, the fed keys + * will be interpreted once the key handler returns. otherwhise the keys are + * immediately interpreted as if they were entered from a user. */ +void vis_keys_feed(Vis*, const char *keys); + /* inform vis that a signal occured, the return value indicates whether the signal * was handled by vis */ bool vis_signal_handler(Vis*, int signum, const siginfo_t *siginfo, const void *context); |
