vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 3d057a321b417aac2c52ee7920ce2019896f8333 parent 380a57fcc8a0e8c20a02a4b3d0a05a3d825ae68a Author: Marc André Tanner <mat@brain-dump.org> Date: Sat, 27 May 2017 11:46:46 +0200 vis-lua: expose currently active key bindings through API Close #563 Diffstat:
| M | vis-core.h | | | 1 | + |
| M | vis-lua.c | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
| M | vis-modes.c | | | 2 | +- |
3 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/vis-core.h b/vis-core.h @@ -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); diff --git a/vis-lua.c b/vis-lua.c @@ -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 @@ -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;