From 8b31028096190ce0339e984bf36df5840d868990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 1 May 2016 11:28:16 +0200 Subject: vis: further cleanup input handling, introduce vis_keys_feed API --- main.c | 8 ++++---- vis-core.h | 1 + vis.c | 34 ++++++++++++++++++++-------------- vis.h | 14 ++++++-------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index 5272bd7..6556700 100644 --- a/main.c +++ b/main.c @@ -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, ""); + vis_keys_feed(vis, keys); + vis_keys_feed(vis, ""); 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, ""); + vis_keys_feed(vis, ""); } else { vis_motion(vis, VIS_MOVE_LINE_BEGIN); - vis_keys_push(vis, ""); + vis_keys_feed(vis, ""); } return keys; } diff --git a/vis-core.h b/vis-core.h index 606ff9b..08cf87b 100644 --- a/vis-core.h +++ b/vis-core.h @@ -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 */ diff --git a/vis.c b/vis.c index aaf446b..4e4f80f 100644 --- a/vis.c +++ b/vis.c @@ -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; diff --git a/vis.h b/vis.h index 8c6133f..8d9681e 100644 --- a/vis.h +++ b/vis.h @@ -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); -- cgit v1.2.3