From d50087eabee0fa0067a5f44462782332230bdf3e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 1 Oct 2016 17:07:50 +0100 Subject: [PATCH] Change BufWritePre/BufWritePost logic Run them in client context if possible, and give them the target filename instead of the buffer name. Fixes #823 --- src/commands.cc | 7 +++++++ src/file.cc | 18 ++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 74d9eb1e..699b2e4b 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -313,7 +313,10 @@ void write_buffer(const ParametersParser& parser, Context& context, const ShellC auto filename = parser.positional_count() == 0 ? buffer.name() : parse_filename(parser[0]); + + context.hooks().run_hook("BufWritePre", filename, context); write_buffer_to_file(buffer, filename); + context.hooks().run_hook("BufWritePost", filename, context); } const CommandDesc write_cmd = { @@ -334,7 +337,11 @@ void write_all_buffers() { if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified() and !(buffer->flags() & Buffer::Flags::ReadOnly)) + { + buffer->run_hook_in_own_context("BufWritePre", buffer->name()); write_buffer_to_file(*buffer, buffer->name()); + buffer->run_hook_in_own_context("BufWritePost", buffer->name()); + } } } diff --git a/src/file.cc b/src/file.cc index 1bd0406e..9940d7f8 100644 --- a/src/file.cc +++ b/src/file.cc @@ -256,23 +256,17 @@ void write_buffer_to_fd(Buffer& buffer, int fd) void write_buffer_to_file(Buffer& buffer, StringView filename) { - buffer.run_hook_in_own_context("BufWritePre", buffer.name()); + int fd = open(parse_filename(filename).c_str(), + O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (fd == -1) + throw file_access_error(filename, strerror(errno)); + auto close_fd = on_scope_end([fd]{ close(fd); }); - { - int fd = open(parse_filename(filename).c_str(), - O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (fd == -1) - throw file_access_error(filename, strerror(errno)); - auto close_fd = on_scope_end([fd]{ close(fd); }); - - write_buffer_to_fd(buffer, fd); - } + write_buffer_to_fd(buffer, fd); if ((buffer.flags() & Buffer::Flags::File) and real_path(filename) == real_path(buffer.name())) buffer.notify_saved(); - - buffer.run_hook_in_own_context("BufWritePost", buffer.name()); } void write_buffer_to_backup_file(Buffer& buffer)