From 8bd63d79f8efb51bace7228392f2afe3c99fb229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 9 Dec 2016 18:38:47 +0100 Subject: vis-lua: expose input key event in insert and replace modes --- lua/vis.lua | 2 ++ main.c | 2 ++ vis-lua.c | 35 ++++++++++++++++++++++++++++++++++- vis-lua.h | 7 +++++++ vis.c | 6 ++++++ vis.h | 2 ++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lua/vis.lua b/lua/vis.lua index 2cc13bd..98ba6e5 100644 --- a/lua/vis.lua +++ b/lua/vis.lua @@ -91,6 +91,7 @@ local events = { FILE_SAVE_POST = "Event::FILE_SAVE_POST", -- see @{file_save_post} FILE_SAVE_PRE = "Event::FILE_SAVE_PRE", -- see @{file_save_pre} INIT = "Event::INIT", -- see @{init} + INPUT = "Event::INPUT", -- see @{input} QUIT = "Event::QUIT", -- see @{quit} START = "Event::START", -- see @{start} THEME_CHANGE = "Event::THEME_CHANGE", -- see @{theme_change} @@ -106,6 +107,7 @@ events.file_open = function(...) events.emit(events.FILE_OPEN, ...) end events.file_save_post = function(...) events.emit(events.FILE_SAVE_POST, ...) end events.file_save_pre = function(...) return events.emit(events.FILE_SAVE_PRE, ...) end events.init = function(...) events.emit(events.INIT, ...) end +events.input = function(...) return events.emit(events.INPUT, ...) end events.quit = function(...) events.emit(events.QUIT, ...) end events.start = function(...) events.emit(events.START, ...) end events.theme_change = function(...) events.emit(events.THEME_CHANGE, ...) end diff --git a/main.c b/main.c index 843fb0d..6252194 100644 --- a/main.c +++ b/main.c @@ -2189,6 +2189,8 @@ int main(int argc, char *argv[]) { .init = vis_lua_init, .start = vis_lua_start, .quit = vis_lua_quit, + .mode_insert_input = vis_lua_mode_insert_input, + .mode_replace_input = vis_lua_mode_replace_input, .file_open = vis_lua_file_open, .file_save_pre = vis_lua_file_save_pre, .file_save_post = vis_lua_file_save_post, diff --git a/vis-lua.c b/vis-lua.c index 42196d1..9e94b28 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1740,7 +1740,7 @@ static const struct luaL_Reg file_lines_funcs[] = { * `vis.events` table. Users scripts should generally use the [Events](#events) * mechanism instead which multiplexes these core events. * - * @section Core Events + * @section Core_Events */ static void vis_lua_event_get(lua_State *L, const char *name) { @@ -2017,6 +2017,39 @@ void vis_lua_quit(Vis *vis) { vis->lua = NULL; } +/*** + * Input key event in either input or replace mode. + * @function input + * @tparam string key + * @treturn bool whether the key was cosumed or not + */ +static bool vis_lua_input(Vis *vis, const char *key, size_t len) { + if (vis->win->file->internal) + return false; + lua_State *L = vis->lua; + bool ret = false; + vis_lua_event_get(L, "input"); + if (lua_isfunction(L, -1)) { + lua_pushlstring(L, key, len); + if (pcall(vis, L, 1, 1) != 0) { + ret = lua_isboolean(L, -1) && lua_toboolean(L, -1); + lua_pop(L, 1); + } + } + lua_pop(L, 1); + return ret; +} + +void vis_lua_mode_insert_input(Vis *vis, const char *key, size_t len) { + if (!vis_lua_input(vis, key, len)) + vis_insert_key(vis, key, len); +} + +void vis_lua_mode_replace_input(Vis *vis, const char *key, size_t len) { + if (!vis_lua_input(vis, key, len)) + vis_replace_key(vis, key, len); +} + /*** * File open. * @function file_open diff --git a/vis-lua.h b/vis-lua.h index 71e30fc..4aba4a4 100644 --- a/vis-lua.h +++ b/vis-lua.h @@ -22,6 +22,13 @@ bool vis_lua_paths_get(Vis*, char **lpath, char **cpath); void vis_lua_init(Vis*); void vis_lua_start(Vis*); void vis_lua_quit(Vis*); +#if !CONFIG_LUA +#define vis_lua_mode_insert_input vis_insert_key +#define vis_lua_mode_replace_input vis_replace_key +#else +void vis_lua_mode_insert_input(Vis*, const char *key, size_t len); +void vis_lua_mode_replace_input(Vis*, const char *key, size_t len); +#endif void vis_lua_file_open(Vis*, File*); bool vis_lua_file_save_pre(Vis*, File*, const char *path); void vis_lua_file_save_post(Vis*, File*, const char *path); diff --git a/vis.c b/vis.c index 63cfa1c..62c7ddd 100644 --- a/vis.c +++ b/vis.c @@ -535,6 +535,12 @@ Vis *vis_new(Ui *ui, VisEvent *event) { goto err; vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL]; vis->event = event; + if (event) { + if (event->mode_insert_input) + vis_modes[VIS_MODE_INSERT].input = event->mode_insert_input; + if (event->mode_replace_input) + vis_modes[VIS_MODE_REPLACE].input = event->mode_replace_input; + } return vis; err: vis_free(vis); diff --git a/vis.h b/vis.h index 13f5454..fd0e300 100644 --- a/vis.h +++ b/vis.h @@ -31,6 +31,8 @@ typedef struct { void (*init)(Vis*); void (*start)(Vis*); void (*quit)(Vis*); + void (*mode_insert_input)(Vis*, const char *key, size_t len); + void (*mode_replace_input)(Vis*, const char *key, size_t len); void (*file_open)(Vis*, File*); bool (*file_save_pre)(Vis*, File*, const char *path); void (*file_save_post)(Vis*, File*, const char *path); -- cgit v1.2.3