diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-05-27 11:46:46 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-05-27 11:51:34 +0200 |
| commit | fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb (patch) | |
| tree | 8214be29aedfd691d5c29e5c232ac4b697da128e | |
| parent | c053762a5eb1470d0737d6f3b0ea051ca9540fac (diff) | |
| download | vis-fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb.tar.gz vis-fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb.tar.xz | |
vis-lua: expose currently active key bindings through API
Close #563
| -rw-r--r-- | vis-core.h | 1 | ||||
| -rw-r--r-- | vis-lua.c | 38 | ||||
| -rw-r--r-- | vis-modes.c | 2 |
3 files changed, 40 insertions, 1 deletions
@@ -262,6 +262,7 @@ void vis_do(Vis *vis); void action_reset(Action*); size_t vis_text_insert_nl(Vis*, Text*, size_t pos); +Mode *mode_get(Vis*, enum VisMode); void mode_set(Vis *vis, Mode *new_mode); Macro *macro_get(Vis *vis, enum VisRegister); @@ -906,6 +906,43 @@ static int map(lua_State *L) { } /*** + * Get all currently active mappings of a mode. + * + * @function mappings + * @tparam int mode the mode to query + * @treturn table the active mappings and their associated help texts + * @usage + * local bindings = vis:mappings(vis.modes.NORMAL) + * for key, help in pairs(bindings) do + * -- do something + * end + * @see Vis:map + */ +static bool binding_collect(const char *key, void *value, void *ctx) { + lua_State *L = ctx; + KeyBinding *binding = value; + lua_getfield(L, -1, key); + bool new = lua_isnil(L, -1); + lua_pop(L, 1); + if (new) { + lua_pushstring(L, binding->alias ? binding->alias : binding->action->help); + lua_setfield(L, -2, key); + } + return true; +} + +static int mappings(lua_State *L) { + Vis *vis = obj_ref_check(L, 1, "vis"); + lua_newtable(L); + for (Mode *mode = mode_get(vis, luaL_checkint(L, 2)); mode; mode = mode->parent) { + if (!mode->bindings) + continue; + map_iterate(mode->bindings, binding_collect, vis->lua); + } + return 1; +} + +/*** * Execute a motion. * * @function motion @@ -1384,6 +1421,7 @@ static const struct luaL_Reg vis_lua[] = { { "info", info }, { "message", message }, { "map", map }, + { "mappings", mappings }, { "operator", operator }, { "operator_register", operator_register }, { "motion", motion }, diff --git a/vis-modes.c b/vis-modes.c index 4dbe0c7..0609022 100644 --- a/vis-modes.c +++ b/vis-modes.c @@ -70,7 +70,7 @@ void vis_binding_free(Vis *vis, KeyBinding *binding) { } } -static Mode *mode_get(Vis *vis, enum VisMode mode) { +Mode *mode_get(Vis *vis, enum VisMode mode) { if (mode < LENGTH(vis_modes)) return &vis_modes[mode]; return NULL; |
