From fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 27 May 2017 11:46:46 +0200 Subject: vis-lua: expose currently active key bindings through API Close #563 --- vis-core.h | 1 + vis-lua.c | 38 ++++++++++++++++++++++++++++++++++++++ vis-modes.c | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/vis-core.h b/vis-core.h index c9a1ffc..1ffb980 100644 --- 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 index 58403d4..ba74d27 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -905,6 +905,43 @@ static int map(lua_State *L) { return keymap(L, vis, NULL); } +/*** + * 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. * @@ -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; -- cgit v1.2.3