vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit e43e534c14c8d8aa5dbb500548d8effc8bc3a1d4 parent 1adb6f1a8483e2af03ab099e9f3e3d0a5a0333ed Author: Marc André Tanner <mat@brain-dump.org> Date: Sat, 14 Jan 2017 10:44:30 +0100 vis-lua: allow mode changes by setting vis.mode Diffstat:
| M | vis-lua.c | | | 15 | ++++++++++++++- |
| M | vis-modes.c | | | 3 | ++- |
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/vis-lua.c b/vis-lua.c @@ -1054,6 +1054,19 @@ static int vis_index(lua_State *L) { return index_common(L); } +static int vis_newindex(lua_State *L) { + Vis *vis = obj_ref_check(L, 1, "vis"); + if (lua_isstring(L, 2)) { + const char *key = lua_tostring(L, 2); + if (strcmp(key, "mode") == 0) { + enum VisMode mode = luaL_checkunsigned(L, 3); + vis_mode_switch(vis, mode); + return 0; + } + } + return newindex_common(L); +} + static const struct luaL_Reg vis_lua[] = { { "files", files }, { "windows", windows }, @@ -1072,7 +1085,7 @@ static const struct luaL_Reg vis_lua[] = { { "feedkeys", feedkeys }, { "action_register", action_register }, { "__index", vis_index }, - { "__newindex", newindex_common }, + { "__newindex", vis_newindex }, { NULL, NULL }, }; diff --git a/vis-modes.c b/vis-modes.c @@ -84,7 +84,8 @@ void mode_set(Vis *vis, Mode *new_mode) { } void vis_mode_switch(Vis *vis, enum VisMode mode) { - mode_set(vis, &vis_modes[mode]); + if (mode < LENGTH(vis_modes)) + mode_set(vis, &vis_modes[mode]); } enum VisMode vis_mode_from(Vis *vis, const char *name) {