aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-12-19 14:05:17 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-12-19 14:44:48 +0100
commit804741d4f869552981345bcdc0fd2cf4f472b899 (patch)
treea4a2cf6b3184f9898d59160fd48dbc3cb9ed017f /vis-lua.c
parenta23e33539d861d53906a353bcf7a748f37ae693f (diff)
downloadvis-804741d4f869552981345bcdc0fd2cf4f472b899.tar.gz
vis-804741d4f869552981345bcdc0fd2cf4f472b899.tar.xz
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 (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c59
1 files changed, 58 insertions, 1 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 3a27168..ce54691 100644
--- 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);