From f555c00e0b0be14cd553c7c803d6136b86f2012b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 21 Nov 2016 23:32:28 +0100 Subject: vis-lua: pass path as second argument to file_save_post event hook The passed path can be different from file.name for instance when opening a file `a` and then doing `:w b` where file.name will be the former and path the latter. --- README.md | 2 +- sam.c | 2 +- vis-lua.c | 10 +++++++--- vis-lua.h | 2 +- vis.c | 10 ++++++---- vis.h | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 47417af..608e1c5 100644 --- a/README.md +++ b/README.md @@ -590,7 +590,7 @@ At this time there exists no API stability guarantees. - `start()` - `quit()` - `file_open(file)` - - `file_save_post(file)` triggered *after* a successfull write + - `file_save_post(file, path)` triggered *after* a successfull write to `path` - `file_close(file)` - `win_open(win)` - `win_close(win)` diff --git a/sam.c b/sam.c index 37a1c80..4bd10d5 100644 --- a/sam.c +++ b/sam.c @@ -1329,7 +1329,7 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Curs file_name_set(file, *name); if (strcmp(file->name, *name) == 0) file->stat = text_stat(text); - vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file); + vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, *name); } return true; } diff --git a/vis-lua.c b/vis-lua.c index f134b83..c48eb43 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -128,7 +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) { } -void vis_lua_file_save_post(Vis *vis, File *file) { } +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) { } void vis_lua_win_close(Vis *vis, Win *win) { } @@ -1544,12 +1544,16 @@ void vis_lua_file_open(Vis *vis, File *file) { lua_pop(L, 1); } -void vis_lua_file_save_post(Vis *vis, File *file) { +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"); if (lua_isfunction(L, -1)) { obj_ref_new(L, file, "vis.file"); - pcall(vis, L, 1, 0); + if (path) + lua_pushstring(L, path); + else + lua_pushnil(L); + pcall(vis, L, 2, 0); } lua_pop(L, 1); } diff --git a/vis-lua.h b/vis-lua.h index 9d19a19..7dab14e 100644 --- a/vis-lua.h +++ b/vis-lua.h @@ -23,7 +23,7 @@ void vis_lua_init(Vis*); void vis_lua_start(Vis*); void vis_lua_quit(Vis*); void vis_lua_file_open(Vis*, File*); -void vis_lua_file_save_post(Vis*, File*); +void vis_lua_file_save_post(Vis*, File*, const char *path); void vis_lua_file_close(Vis*, File*); void vis_lua_win_open(Vis*, Win*); void vis_lua_win_close(Vis*, Win*); diff --git a/vis.c b/vis.c index 341e6ed..dcd7457 100644 --- a/vis.c +++ b/vis.c @@ -64,12 +64,14 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { File *file = va_arg(ap, File*); if (file->internal) break; - if (id == VIS_EVENT_FILE_OPEN && vis->event->file_open) + if (id == VIS_EVENT_FILE_OPEN && vis->event->file_open) { vis->event->file_open(vis, file); - else if (id == VIS_EVENT_FILE_SAVE_POST && vis->event->file_save_post) - vis->event->file_save_post(vis, file); - else if (id == VIS_EVENT_FILE_CLOSE && vis->event->file_close) + } else if (id == VIS_EVENT_FILE_SAVE_POST && vis->event->file_save_post) { + const char *path = va_arg(ap, const char*); + vis->event->file_save_post(vis, file, path); + } else if (id == VIS_EVENT_FILE_CLOSE && vis->event->file_close) { vis->event->file_close(vis, file); + } break; } case VIS_EVENT_WIN_OPEN: diff --git a/vis.h b/vis.h index 2058669..f3366c7 100644 --- a/vis.h +++ b/vis.h @@ -32,7 +32,7 @@ typedef struct { void (*vis_start)(Vis*); void (*vis_quit)(Vis*); void (*file_open)(Vis*, File*); - void (*file_save_post)(Vis*, File*); + void (*file_save_post)(Vis*, File*, const char *path); void (*file_close)(Vis*, File*); void (*win_open)(Vis*, Win*); void (*win_close)(Vis*, Win*); -- cgit v1.2.3