From a04408c5918376be63a1902fd71726dfff9edb3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 17 Feb 2016 21:50:10 +0100 Subject: vis-lua: expose vis:textobject_register --- vis-lua.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/vis-lua.c b/vis-lua.c index 3f3c4a3..0ea7d48 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -404,6 +404,29 @@ static int textobject(lua_State *L) { return 1; } + +static Filerange textobject_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 text_range_empty(); + lua_pushunsigned(L, pos); + if (pcall(vis, L, 2, 2) != 0) + return text_range_empty(); + return text_range_new(luaL_checkunsigned(L, -2), luaL_checkunsigned(L, -1)); +} + +static int textobject_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_textobject_register(vis, 0, (void*)func, textobject_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) { @@ -469,6 +492,7 @@ static const struct luaL_Reg vis_lua[] = { { "motion", motion }, { "motion_register", motion_register }, { "textobject", textobject }, + { "textobject_register", textobject_register }, { "__index", vis_index }, { "__newindex", vis_newindex }, { NULL, NULL }, -- cgit v1.2.3