From ed0bbf0dc29e0107da604c83eebde4e17521c588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 13 May 2016 23:46:16 +0200 Subject: vis-lua: add win:map function for window local key mappings Based on a patch by Josh Wainwright. Close #306 --- README.md | 1 + vis-lua.c | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index be11bde..3c6b543 100644 --- a/README.md +++ b/README.md @@ -617,6 +617,7 @@ At this time there exists no API stability guarantees. - `cursors[1..#cursors]` array giving read access to all cursors - `cursor` primary cursor - `syntax` lexer name used for syntax highlighting or `nil` + - `map(mode, key, function)` map a Lua function to `key` in `mode`, local to the given window - `cursor` - `line` (1 based), `col` (1 based) - `to(line, col)` diff --git a/vis-lua.c b/vis-lua.c index 18259ef..a28efc7 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -376,12 +376,7 @@ static int open(lua_State *L) { return 1; } -static int map(lua_State *L) { - Vis *vis = obj_ref_check(L, 1, "vis"); - if (!vis) { - lua_pushnil(L); - return 1; - } +static int keymap(lua_State *L, Vis *vis, Win *win) { KeyBinding *binding = NULL; KeyAction *action = NULL; @@ -410,8 +405,13 @@ static int map(lua_State *L) { binding->action = action; - if (!vis_mode_map(vis, mode, true, key, binding)) - goto err; + if (win) { + if (!vis_window_mode_map(win, mode, true, key, binding)) + goto err; + } else { + if (!vis_mode_map(vis, mode, true, key, binding)) + goto err; + } lua_pushboolean(L, true); return 1; @@ -422,6 +422,15 @@ err: return 1; } +static int map(lua_State *L) { + Vis *vis = obj_ref_check(L, 1, "vis"); + if (!vis) { + lua_pushboolean(L, false); + return 1; + } + return keymap(L, vis, NULL); +} + static int motion(lua_State *L) { Vis *vis = obj_ref_check(L, 1, "vis"); enum VisMotion id = luaL_checkunsigned(L, 2); @@ -682,10 +691,20 @@ static int window_cursors_iterator(lua_State *L) { return 1; } +static int window_map(lua_State *L) { + Win *win = obj_ref_check(L, 1, "vis.window"); + if (!win) { + lua_pushboolean(L, false); + return 1; + } + return keymap(L, win->vis, win); +} + static const struct luaL_Reg window_funcs[] = { { "__index", window_index }, { "__newindex", window_newindex }, { "cursors_iterator", window_cursors_iterator }, + { "map", window_map }, { NULL, NULL }, }; -- cgit v1.2.3