From b02c26e64aca90c3d2f3af0e94a985dc43831655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 15 Apr 2016 23:48:45 +0200 Subject: vis-lua: add window.cursors[] array --- vis-lua.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index 5ba1865..3533681 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -521,6 +521,11 @@ static int window_index(lua_State *L) { return 1; } + if (strcmp(key, "cursors") == 0) { + obj_ref_new(L, win->view, "vis.window.cursors"); + return 1; + } + if (strcmp(key, "syntax") == 0) { const char *syntax = view_syntax_get(win->view); if (syntax) @@ -581,6 +586,37 @@ static const struct luaL_Reg window_funcs[] = { { NULL, NULL }, }; +static int window_cursors_index(lua_State *L) { + View *view = obj_ref_check(L, 1, "vis.window.cursors"); + if (!view) + goto err; + size_t index = luaL_checkunsigned(L, 2); + size_t count = view_cursors_count(view); + if (index == 0 || index > count) + goto err; + for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) { + if (!--index) { + obj_ref_new(L, c, "vis.window.cursor"); + return 1; + } + } +err: + lua_pushnil(L); + return 1; +} + +static int window_cursors_len(lua_State *L) { + View *view = obj_ref_check(L, 1, "vis.window.cursors"); + lua_pushunsigned(L, view ? view_cursors_count(view) : 0); + return 1; +} + +static const struct luaL_Reg window_cursors_funcs[] = { + { "__index", window_cursors_index }, + { "__len", window_cursors_len }, + { NULL, NULL }, +}; + static int window_cursor_index(lua_State *L) { Cursor *cur = obj_ref_check(L, 1, "vis.window.cursor"); if (!cur) { @@ -948,6 +984,8 @@ void vis_lua_start(Vis *vis) { luaL_setfuncs(L, window_funcs, 0); luaL_newmetatable(L, "vis.window.cursor"); luaL_setfuncs(L, window_cursor_funcs, 0); + luaL_newmetatable(L, "vis.window.cursors"); + luaL_setfuncs(L, window_cursors_funcs, 0); /* vis module table with up value as the C pointer */ luaL_newmetatable(L, "vis"); luaL_setfuncs(L, vis_lua, 0); -- cgit v1.2.3