diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-02-17 21:02:13 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-02-18 16:50:40 +0100 |
| commit | b3862a3dca144d44ed27b74ffc04afa306f88a89 (patch) | |
| tree | c621b99de8ff35f141d395e805c7be11d0d6c5d4 | |
| parent | 44ff1f95ecc14521fb2a1cec67a628140fc46598 (diff) | |
| download | vis-b3862a3dca144d44ed27b74ffc04afa306f88a89.tar.gz vis-b3862a3dca144d44ed27b74ffc04afa306f88a89.tar.xz | |
vis-lua: add vis:motion_register function
It registers a lua function which will be called whenever the motion
is used.
| -rw-r--r-- | vis-lua.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -374,6 +374,29 @@ static int motion(lua_State *L) { return 1; } +static size_t motion_lua(Vis *vis, Win *win, void *data, size_t pos) { + lua_State *L = vis->lua; + if (!func_ref_get(L, data) || !obj_ref_new(L, win, "vis.window")) + return EPOS; + + lua_pushunsigned(L, pos); + if (pcall(vis, L, 2, 1) != 0) + return EPOS; + return luaL_checkunsigned(L, -1); +} + +static int motion_register(lua_State *L) { + int id = -1; + const void *func; + Vis *vis = obj_ref_check(L, 1, "vis"); + if (!vis || !lua_isfunction(L, 2) || !(func = func_ref_new(L))) + goto err; + id = vis_motion_register(vis, 0, (void*)func, motion_lua); +err: + lua_pushinteger(L, id); + return 1; +} + static int vis_index(lua_State *L) { Vis *vis = obj_ref_check(L, 1, "vis"); if (!vis) { @@ -437,6 +460,7 @@ static const struct luaL_Reg vis_lua[] = { { "open", open }, { "map", map }, { "motion", motion }, + { "motion_register", motion_register }, { "__index", vis_index }, { "__newindex", vis_newindex }, { NULL, NULL }, |
