aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-05-13 23:46:16 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-05-14 00:00:39 +0200
commited0bbf0dc29e0107da604c83eebde4e17521c588 (patch)
treef308411fc05eec6f078fff2fc92f02caba56319d
parenta1881512f3050e8d6719a28b55846c1f4c93acab (diff)
downloadvis-ed0bbf0dc29e0107da604c83eebde4e17521c588.tar.gz
vis-ed0bbf0dc29e0107da604c83eebde4e17521c588.tar.xz
vis-lua: add win:map function for window local key mappings
Based on a patch by Josh Wainwright. Close #306
-rw-r--r--README.md1
-rw-r--r--vis-lua.c35
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 },
};