diff options
| -rw-r--r-- | vis-lua.c | 39 |
1 files changed, 38 insertions, 1 deletions
@@ -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); |
