vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 53354614294ec863311569445ac5fd708fe56d89
parent 3d5e4bac6cafacb572135b7efbe1493d1b995813
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 28 Jan 2017 14:03:57 +0100

vis: keep track of most recently processed keys of input queue

Currently the key handling functions do not know through which mapping
they were invoked. As an example the `count` handler exploits the
implementation detail that the input queue is stored in contiguous
memory, meaning `keys[-1]` gives access to the digit being pressed.

This adds infrastructure to keep track of the two most recently processed
keys of the input queue.

The information is guaranteed to be accurate for the initial invocation
of the key handler but will be overwritten in case new keys are pushed
to the input queue (e.g. through vis_keys_feed).

Diffstat:
Mvis-core.h | 4+++-
Mvis.c | 8++++++++
Mvis.h | 3+++
3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/vis-core.h b/vis-core.h @@ -174,7 +174,9 @@ struct Vis { Map *options; /* ":set"-options */ Map *keymap; /* key translation before any bindings are matched */ bool keymap_disabled; /* ignore key map for next key press, gets automatically re-enabled */ - char key[64]; /* last pressed key as reported from the UI */ + char key[VIS_KEY_LENGTH_MAX]; /* last pressed key as reported from the UI */ + char key_current[VIS_KEY_LENGTH_MAX];/* current key being processed by the input queue */ + char key_prev[VIS_KEY_LENGTH_MAX]; /* previous key which was processed by the input queue */ Buffer input_queue; /* holds pending input keys */ Buffer *keys; /* currently active keys buffer (either the input_queue or a macro) */ bool errorhandler; /* whether we are currently in an error handler, used to avoid recursion */ diff --git a/vis.c b/vis.c @@ -995,6 +995,10 @@ static void vis_keys_process(Vis *vis, size_t pos) { end = start; } else if (binding) { /* exact match */ if (binding->action) { + size_t len = binding_end - start; + strcpy(vis->key_prev, vis->key_current); + strncpy(vis->key_current, start, len); + vis->key_current[len] = '\0'; end = (char*)binding->action->func(vis, binding_end, &binding->action->arg); if (!end) { end = start; @@ -1017,6 +1021,10 @@ static void vis_keys_process(Vis *vis, size_t pos) { action = map_get(vis->actions, start+1); end[-1] = tmp; if (action) { + size_t len = end - start; + strcpy(vis->key_prev, vis->key_current); + strncpy(vis->key_current, start, len); + vis->key_current[len] = '\0'; end = (char*)action->func(vis, end, &action->arg); if (!end) { end = start; diff --git a/vis.h b/vis.h @@ -27,6 +27,9 @@ typedef struct Win Win; #define VIS_COMPLETE "vis-complete" #endif +/* maximum bytes needed for string representation of a (pseudo) key */ +#define VIS_KEY_LENGTH_MAX 64 + typedef struct { void (*init)(Vis*); void (*start)(Vis*);