diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2020-01-27 10:35:11 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2020-01-27 10:35:11 +0100 |
| commit | 978f3d70b9047608789e40a0b67e91d4fd19e60a (patch) | |
| tree | 225a916dd1ee5f1c35570954828c8aec9e9e10b4 /sam.c | |
| parent | 79859b57fc3556c2ed74950a5a5668c64face4e3 (diff) | |
| download | vis-978f3d70b9047608789e40a0b67e91d4fd19e60a.tar.gz vis-978f3d70b9047608789e40a0b67e91d4fd19e60a.tar.xz | |
vis: pass absolute path to pre/post save events
Diffstat (limited to 'sam.c')
| -rw-r--r-- | sam.c | 37 |
1 files changed, 24 insertions, 13 deletions
@@ -1623,23 +1623,28 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele } for (const char **name = argv[1] ? &argv[1] : (const char*[]){ filename, NULL }; *name; name++) { + + char *path = absolute_path(*name); + if (!path) + return false; + struct stat meta; - if (cmd->flags != '!' && file->stat.st_mtime && stat(*name, &meta) == 0 && + if (cmd->flags != '!' && file->stat.st_mtime && stat(path, &meta) == 0 && file->stat.st_mtime < meta.st_mtime) { vis_info_show(vis, "WARNING: file has been changed since reading it"); - return false; + goto err; } - if (!vis_event_emit(vis, VIS_EVENT_FILE_SAVE_PRE, file, *name) && cmd->flags != '!') { - vis_info_show(vis, "Rejected write to `%s' by pre-save hook", *name); - return false; + if (!vis_event_emit(vis, VIS_EVENT_FILE_SAVE_PRE, file, path) && cmd->flags != '!') { + vis_info_show(vis, "Rejected write to `%s' by pre-save hook", path); + goto err; } - TextSave *ctx = text_save_begin(text, *name, file->save_method); + TextSave *ctx = text_save_begin(text, path, file->save_method); if (!ctx) { const char *msg = errno ? strerror(errno) : "try changing `:set savemethod`"; - vis_info_show(vis, "Can't write `%s': %s", *name, msg); - return false; + vis_info_show(vis, "Can't write `%s': %s", path, msg); + goto err; } bool failure = false; @@ -1659,15 +1664,21 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele } if (failure || !text_save_commit(ctx)) { - vis_info_show(vis, "Can't write `%s': %s", *name, strerror(errno)); - return false; + vis_info_show(vis, "Can't write `%s': %s", path, strerror(errno)); + goto err; } if (!file->name) - file_name_set(file, *name); - if (strcmp(file->name, *name) == 0) + file_name_set(file, path); + if (strcmp(file->name, path) == 0) file->stat = text_stat(text); - vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, *name); + vis_event_emit(vis, VIS_EVENT_FILE_SAVE_POST, file, path); + free(path); + continue; + + err: + free(path); + return false; } return true; } |
