From 0791cac97ee689195e593cfca89c0b63fb721479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 22 Nov 2016 00:15:32 +0100 Subject: vis-lua: introduce pre-save hook The first argument is the file object while the second argument denotes the full path to which it will be written. Path might be `nil` if the file is going to be written to stdout. The Lua function is expected to return a boolean value indicating whether the write operation should proceed or be aborted. --- vis-lua.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index c48eb43..019f8cf 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -128,6 +128,7 @@ void vis_lua_init(Vis *vis) { } void vis_lua_start(Vis *vis) { } void vis_lua_quit(Vis *vis) { } void vis_lua_file_open(Vis *vis, File *file) { } +bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) { return true; } void vis_lua_file_save_post(Vis *vis, File *file, const char *path) { } void vis_lua_file_close(Vis *vis, File *file) { } void vis_lua_win_open(Vis *vis, Win *win) { } @@ -1544,6 +1545,23 @@ void vis_lua_file_open(Vis *vis, File *file) { lua_pop(L, 1); } +bool vis_lua_file_save_pre(Vis *vis, File *file, const char *path) { + lua_State *L = vis->lua; + vis_lua_event_get(L, "file_save_pre"); + if (lua_isfunction(L, -1)) { + obj_ref_new(L, file, "vis.file"); + if (path) + lua_pushstring(L, path); + else + lua_pushnil(L); + if (pcall(vis, L, 2, 1) != 0) + return false; + return lua_toboolean(L, -1); + } + lua_pop(L, 1); + return true; +} + void vis_lua_file_save_post(Vis *vis, File *file, const char *path) { lua_State *L = vis->lua; vis_lua_event_get(L, "file_save_post"); -- cgit v1.2.3