Change BufWritePre/BufWritePost logic

Run them in client context if possible, and give them the target
filename instead of the buffer name.

Fixes #823
This commit is contained in:
Maxime Coste 2016-10-01 17:07:50 +01:00
parent 4b6d4ec8eb
commit d50087eabe
2 changed files with 13 additions and 12 deletions

View File

@ -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());
}
}
}

View File

@ -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)