diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-04-15 23:48:45 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-04-16 12:52:04 +0200 |
| commit | b02c26e64aca90c3d2f3af0e94a985dc43831655 (patch) | |
| tree | 9355af4fac27d7fa25ccf0605ff2eb91b14e291a | |
| parent | f3d3af1e836cd2896f47a5a219e85c84f0022ed5 (diff) | |
| download | vis-b02c26e64aca90c3d2f3af0e94a985dc43831655.tar.gz vis-b02c26e64aca90c3d2f3af0e94a985dc43831655.tar.xz | |
vis-lua: add window.cursors[] array
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | vis-lua.c | 38 |
2 files changed, 39 insertions, 0 deletions
@@ -599,6 +599,7 @@ At this time there exists no API stability guarantees. - `file` - `syntax` lexer name used for syntax highlighting or `nil` - `cursors_iterator()` + - `cursors[1..#cursors]` array giving read access to all cursors - `cursor` primary cursor - `line` (1 based), `col` (1 based) - `to(line, col)` @@ -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); |
