aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-01-10 22:07:44 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-01-10 22:44:23 +0100
commit0a1b23e2a2467eb11673f507c044088f66386a55 (patch)
treea5d2b10b471b8e20beec246621a69aa13e15ba5e /vis-lua.c
parentb95e52adb89c56a135c25f22910acb5e28f2a7fb (diff)
downloadvis-0a1b23e2a2467eb11673f507c044088f66386a55.tar.gz
vis-0a1b23e2a2467eb11673f507c044088f66386a55.tar.xz
vis-lua: expose register names as vis:register_names()
Diffstat (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 7e03c65..9936219 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -632,6 +632,42 @@ static int mark_names_iter(lua_State *L) {
}
/***
+ * Create an iterator over all register names.
+ * @function register_names
+ * @return the new iterator
+ * @usage
+ * for reg in vis:register_names() do
+ * local value = vis.registers[reg]
+ * if value then
+ * -- do something with register value
+ * end
+ * end
+ */
+static int register_names_iter(lua_State *L);
+static int register_names(lua_State *L) {
+ enum VisRegister *handle = lua_newuserdata(L, sizeof *handle);
+ *handle = 0;
+ lua_pushcclosure(L, register_names_iter, 1);
+ return 1;
+}
+
+static int register_names_iter(lua_State *L) {
+ char reg = '\0';
+ enum VisRegister *handle = lua_touserdata(L, lua_upvalueindex(1));
+ if (*handle < LENGTH(vis_registers))
+ reg = vis_registers[*handle].name;
+ else if (VIS_REG_a <= *handle && *handle <= VIS_REG_z)
+ reg = 'a' + *handle - VIS_REG_a;
+ if (reg) {
+ char name[2] = { reg, '\0' };
+ lua_pushstring(L, name);
+ (*handle)++;
+ return 1;
+ }
+ return 0;
+}
+
+/***
* Execute a `:`-command.
* @function command
* @tparam string command the command to execute
@@ -1022,6 +1058,7 @@ static const struct luaL_Reg vis_lua[] = {
{ "files", files },
{ "windows", windows },
{ "mark_names", mark_names },
+ { "register_names", register_names },
{ "command", command },
{ "info", info },
{ "message", message },
@@ -1054,7 +1091,7 @@ static int registers_index(lua_State *L) {
goto err;
size_t len;
const char *value = vis_register_get(vis, reg, &len);
- lua_pushlstring(L, value, len);
+ lua_pushlstring(L, value ? value : "" , len);
return 1;
err:
lua_pushnil(L);