aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-02-17 21:02:13 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-02-18 16:50:40 +0100
commitb3862a3dca144d44ed27b74ffc04afa306f88a89 (patch)
treec621b99de8ff35f141d395e805c7be11d0d6c5d4 /vis-lua.c
parent44ff1f95ecc14521fb2a1cec67a628140fc46598 (diff)
downloadvis-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.
Diffstat (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 6c195a1..f253a56 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -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 },