From 0a1b23e2a2467eb11673f507c044088f66386a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 10 Jan 2017 22:07:44 +0100 Subject: vis-lua: expose register names as vis:register_names() --- vis-lua.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/vis-lua.c b/vis-lua.c index 7e03c65..9936219 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -631,6 +631,42 @@ static int mark_names_iter(lua_State *L) { return 0; } +/*** + * 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 @@ -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); -- cgit v1.2.3