aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2020-01-27 10:35:11 +0100
committerMarc André Tanner <mat@brain-dump.org>2020-01-27 10:35:11 +0100
commit978f3d70b9047608789e40a0b67e91d4fd19e60a (patch)
tree225a916dd1ee5f1c35570954828c8aec9e9e10b4
parent79859b57fc3556c2ed74950a5a5668c64face4e3 (diff)
downloadvis-978f3d70b9047608789e40a0b67e91d4fd19e60a.tar.gz
vis-978f3d70b9047608789e40a0b67e91d4fd19e60a.tar.xz
vis: pass absolute path to pre/post save events
-rw-r--r--sam.c37
-rw-r--r--vis-core.h2
-rw-r--r--vis.c2
3 files changed, 27 insertions, 14 deletions
diff --git a/sam.c b/sam.c
index b34ccb5..16f811c 100644
--- a/sam.c
+++ b/sam.c
@@ -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;
}
diff --git a/vis-core.h b/vis-core.h
index d8f5e1d..d409f88 100644
--- a/vis-core.h
+++ b/vis-core.h
@@ -269,6 +269,8 @@ Macro *macro_get(Vis *vis, enum VisRegister);
void window_selection_save(Win *win);
Win *window_new_file(Vis*, File*, enum UiOption);
+char *absolute_path(const char *path);
+
const char *file_name_get(File*);
void file_name_set(File*, const char *name);
diff --git a/vis.c b/vis.c
index 2c53f1a..985309e 100644
--- a/vis.c
+++ b/vis.c
@@ -144,7 +144,7 @@ static File *file_new_text(Vis *vis, Text *text) {
return file;
}
-static char *absolute_path(const char *name) {
+char *absolute_path(const char *name) {
if (!name)
return NULL;
char *copy1 = strdup(name);