vis

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

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

commit 14c57210a66fc3a71bbb0e3ff42598c6e4267c0c
parent b60ee38cd12dddac3ad5a77ece4dea19af5eea44
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 19 Dec 2016 14:05:17 +0100

vis-lua: implement vis.registers[] array

Notice that currently only single letter register names/array indices are
supported.

Register handling needs to be cleaned up at some point.

Diffstat:
Mvis-lua.c | 59++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)

diff --git a/vis-lua.c b/vis-lua.c @@ -526,7 +526,10 @@ static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) { * Events. * @tfield events events */ - +/*** + * Registers. + * @field registers array to access the register by single letter name + */ /*** * LPeg lexer support module. * @field lexers @@ -915,6 +918,11 @@ static int vis_index(lua_State *L) { return 1; } + if (strcmp(key, "registers") == 0) { + obj_ref_new(L, vis->ui, "vis.registers"); + return 1; + } + if (strcmp(key, "ui") == 0) { obj_ref_new(L, vis->ui, "vis.ui"); return 1; @@ -948,6 +956,51 @@ static const struct luaL_Reg ui_funcs[] = { { NULL, NULL }, }; +static int registers_index(lua_State *L) { + Vis *vis = lua_touserdata(L, lua_upvalueindex(1)); + const char *symbol = luaL_checkstring(L, 2); + if (!symbol || strlen(symbol) != 1) + goto err; + enum VisRegister reg = vis_register_from(NULL /* XXX */, symbol[0]); + if (reg >= VIS_REG_INVALID) + goto err; + size_t len; + const char *value = vis_register_get(vis, reg, &len); + lua_pushlstring(L, value, len); + return 1; +err: + lua_pushnil(L); + return 1; +} + +static int registers_newindex(lua_State *L) { + Vis *vis = lua_touserdata(L, lua_upvalueindex(1)); + const char *symbol = luaL_checkstring(L, 2); + if (!symbol || strlen(symbol) != 1) + return 0; + enum VisRegister reg = vis_register_from(NULL /* XXX */, symbol[0]); + if (reg >= VIS_REG_INVALID) + return 0; + luaL_checkstring(L, 3); + size_t len; + const char *value = lua_tolstring(L, 3, &len); + register_put(vis, &vis->registers[reg], value, len+1); + return 0; +} + +static int registers_len(lua_State *L) { + Vis *vis = lua_touserdata(L, lua_upvalueindex(1)); + lua_pushunsigned(L, LENGTH(vis->registers)); + return 1; +} + +static const struct luaL_Reg registers_funcs[] = { + { "__index", registers_index }, + { "__newindex", registers_newindex }, + { "__len", registers_len }, + { NULL, NULL }, +}; + /*** * A window object. * @type Window @@ -2003,6 +2056,10 @@ void vis_lua_init(Vis *vis) { lua_pushunsigned(L, vis->ui->colors(vis->ui)); lua_setfield(L, -2, "colors"); + obj_type_new(L, "vis.registers"); + lua_pushlightuserdata(L, vis); + luaL_setfuncs(L, registers_funcs, 1); + obj_type_new(L, "vis"); luaL_setfuncs(L, vis_lua, 0);