From 8df64eae8924d4e2c2ca1b883e8149108ed4333d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 20 May 2016 12:59:03 +0200 Subject: vis-lua: cleanup vis.MODE_* constants handling --- vis-lua.c | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index b32011d..1f15eb7 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -10,6 +10,7 @@ #include "vis-lua.h" #include "vis-core.h" #include "text-motions.h" +#include "util.h" #ifndef VIS_PATH #define VIS_PATH "/usr/local/share/vis" @@ -553,35 +554,6 @@ static int vis_index(lua_State *L) { return 1; } - if (strcmp(key, "MODE_NORMAL") == 0) { - lua_pushunsigned(L, VIS_MODE_NORMAL); - return 1; - } - - if (strcmp(key, "MODE_OPERATOR_PENDING") == 0) { - lua_pushunsigned(L, VIS_MODE_OPERATOR_PENDING); - return 1; - } - - if (strcmp(key, "MODE_VISUAL") == 0) { - lua_pushunsigned(L, VIS_MODE_VISUAL); - return 1; - } - - if (strcmp(key, "MODE_VISUAL_LINE") == 0) { - lua_pushunsigned(L, VIS_MODE_VISUAL_LINE); - return 1; - } - - if (strcmp(key, "MODE_INSERT") == 0) { - lua_pushunsigned(L, VIS_MODE_INSERT); - return 1; - } - - if (strcmp(key, "MODE_REPLACE") == 0) { - lua_pushunsigned(L, VIS_MODE_REPLACE); - return 1; - } } return index_common(L); @@ -1170,9 +1142,26 @@ void vis_lua_init(Vis *vis) { luaL_setfuncs(L, window_cursor_funcs, 0); luaL_newmetatable(L, "vis.window.cursors"); luaL_setfuncs(L, window_cursors_funcs, 0); - /* vis module table with up value as the C pointer */ luaL_newmetatable(L, "vis"); luaL_setfuncs(L, vis_lua, 0); + + static const struct { + enum VisMode id; + const char *name; + } modes[] = { + { VIS_MODE_NORMAL, "MODE_NORMAL" }, + { VIS_MODE_OPERATOR_PENDING, "MODE_OPERATOR_PENDING" }, + { VIS_MODE_VISUAL, "MODE_VISUAL" }, + { VIS_MODE_VISUAL_LINE, "MODE_VISUAL_LINE" }, + { VIS_MODE_INSERT, "MODE_INSERT" }, + { VIS_MODE_REPLACE, "MODE_REPLACE" }, + }; + + for (size_t i = 0; i < LENGTH(modes); i++) { + lua_pushunsigned(L, modes[i].id); + lua_setfield(L, -2, modes[i].name); + } + obj_ref_new(L, vis, "vis"); lua_setglobal(L, "vis"); -- cgit v1.2.3