aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-05-27 11:46:46 +0200
committerMarc André Tanner <mat@brain-dump.org>2017-05-27 11:51:34 +0200
commitfa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb (patch)
tree8214be29aedfd691d5c29e5c232ac4b697da128e /vis-lua.c
parentc053762a5eb1470d0737d6f3b0ea051ca9540fac (diff)
downloadvis-fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb.tar.gz
vis-fa93b6c3292acdbfe71c9a341ba319ecc0d0d9cb.tar.xz
vis-lua: expose currently active key bindings through API
Close #563
Diffstat (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 58403d4..ba74d27 100644
--- 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 },