vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 722733db9f1d1665e31c6c16327d5ca05e19f498
parent 18d4ac3690c689370d77219babffa82c20b97d0a
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 17 Feb 2016 21:02:13 +0100

vis-lua: add vis:motion_register function

It registers a lua function which will be called whenever the motion
is used.

Diffstat:
Mvis-lua.c | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git 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 },