From 0508cb652e6ec6c926b8fbaca7c79bdd386252fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 8 Dec 2016 10:02:44 +0100 Subject: vis-lua: add optional help parameter to mapping functions --- lua/plugins/textobject-lexer.lua | 2 +- lua/vis.lua | 20 +++++++++++--------- vis-lua.c | 8 ++++++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lua/plugins/textobject-lexer.lua b/lua/plugins/textobject-lexer.lua index a995083..4837935 100644 --- a/lua/plugins/textobject-lexer.lua +++ b/lua/plugins/textobject-lexer.lua @@ -31,4 +31,4 @@ vis:textobject_new("ii", function(win, pos) end return pos, pos -end) +end, "Current lexer token") diff --git a/lua/vis.lua b/lua/vis.lua index be46b79..84ebd90 100644 --- a/lua/vis.lua +++ b/lua/vis.lua @@ -22,13 +22,14 @@ end -- return the resulting position. -- @tparam string key the key to associate with the new mption -- @tparam function motion the motion logic implemented as Lua function +-- @tparam[opt] string help the single line help text as displayed in `:help` -- @treturn bool whether the new motion could be installed -- @usage -- vis:motion_new("", function(win, pos) -- return pos+1 --- end) +-- end, "Advance to next byte") -- -vis.motion_new = function(vis, key, motion) +vis.motion_new = function(vis, key, motion, help) local id = vis:motion_register(motion) if id < 0 then return false @@ -36,9 +37,9 @@ vis.motion_new = function(vis, key, motion) local binding = function() vis:motion(id) end - vis:map(vis.MODE_NORMAL, key, binding) - vis:map(vis.MODE_VISUAL, key, binding) - vis:map(vis.MODE_OPERATOR_PENDING, key, binding) + vis:map(vis.MODE_NORMAL, key, binding, help) + vis:map(vis.MODE_VISUAL, key, binding, help) + vis:map(vis.MODE_OPERATOR_PENDING, key, binding, help) return true end @@ -50,13 +51,14 @@ end -- expected to return the resulting range or `nil`. -- @tparam string key the key associated with the new text object -- @tparam function textobject the text object logic implemented as Lua function +-- @tparam[opt] string help the single line help text as displayed in `:help` -- @treturn bool whether the new text object could be installed -- @usage -- vis:textobject_new("", function(win, pos) -- return pos, pos+1 --- end) +-- end, "Single byte text object") -- -vis.textobject_new = function(vis, key, textobject) +vis.textobject_new = function(vis, key, textobject, help) local id = vis:textobject_register(textobject) if id < 0 then return false @@ -64,8 +66,8 @@ vis.textobject_new = function(vis, key, textobject) local binding = function() vis:textobject(id) end - vis:map(vis.MODE_VISUAL, key, binding) - vis:map(vis.MODE_OPERATOR_PENDING, key, binding) + vis:map(vis.MODE_VISUAL, key, binding, help) + vis:map(vis.MODE_OPERATOR_PENDING, key, binding, help) return true end diff --git a/vis-lua.c b/vis-lua.c index e37c1c0..fec7270 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -692,9 +692,9 @@ static int keymap(lua_State *L, Vis *vis, Win *win) { int mode = luaL_checkint(L, 2); const char *key = luaL_checkstring(L, 3); - if (!key || !lua_isfunction(L, 4)) goto err; + const char *help = luaL_optstring(L, 5, NULL); if (!(binding = calloc(1, sizeof *binding)) || !(action = calloc(1, sizeof *action))) goto err; @@ -706,7 +706,7 @@ static int keymap(lua_State *L, Vis *vis, Win *win) { *action = (KeyAction){ .name = NULL, - .help = NULL, + .help = help ? strdup(help) : help, .func = keymapping, .arg = (const Arg){ .v = func, @@ -739,7 +739,9 @@ err: * * @function map * @tparam int mode the mode to which the mapping should be added + * @tparam string key the key to map * @tparam function func the Lua function to handle the key mapping + * @tparam[opt] string help the single line help text as displayed in `:help` * @treturn bool whether the mapping was successfully established * @see Window:map */ @@ -1149,7 +1151,9 @@ static int window_cursors_iterator(lua_State *L) { * * @function map * @tparam int mode the mode to which the mapping should be added + * @tparam string key the key to map * @tparam function func the Lua function to handle the key mapping + * @tparam[opt] string help the single line help text as displayed in `:help` * @treturn bool whether the mapping was successfully established * @see Vis:map */ -- cgit v1.2.3