diff options
| author | Maxime Coste <mawww@kakoune.org> | 2020-10-27 20:20:18 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2020-10-27 20:20:18 +1100 |
| commit | 90043e7df04359ec9ef629fa46fc6a524673be84 (patch) | |
| tree | 8acbaa48a5f8056a2953ecf5b25172a1e98f520b /src | |
| parent | 9a5cf2fc9f9d4afff586109239a1a3640c821727 (diff) | |
Replace `write -atomic` with `write -method <method>`
`-atomic` becomes `-method replace` and `-method overwrite` is now
supported explicitely instead of only available through the
writemethod option.
Fixes #3827
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cc | 36 | ||||
| -rw-r--r-- | src/main.cc | 1 |
2 files changed, 26 insertions, 11 deletions
diff --git a/src/commands.cc b/src/commands.cc index e453fbf2..aafad24f 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -456,12 +456,21 @@ const CommandDesc force_edit_cmd = { const ParameterDesc write_params{ { { "sync", { false, "force the synchronization of the file onto the filesystem" } }, - { "atomic", { false, "force the writemethod to replace" } }, + { "method", { true, "explicite writemethod (replace|overwrite)" } }, }, ParameterDesc::Flags::SwitchesOnlyAtStart, 0, 1 }; -void do_write_buffer(Context& context, Optional<String> filename, WriteFlags flags, bool atomic = false) +auto parse_write_method(StringView str) +{ + constexpr auto desc = enum_desc(Meta::Type<WriteMethod>{}); + auto it = find_if(desc, [str](const EnumDesc<WriteMethod>& d) { return d.name == str; }); + if (it == desc.end()) + throw runtime_error(format("invalid writemethod '{}'", str)); + return it->value; +} + +void do_write_buffer(Context& context, Optional<String> filename, WriteFlags flags, Optional<WriteMethod> write_method = {}) { Buffer& buffer = context.buffer(); const bool is_file = (bool)(buffer.flags() & Buffer::Flags::File); @@ -477,10 +486,10 @@ void do_write_buffer(Context& context, Optional<String> filename, WriteFlags fla throw runtime_error("cannot overwrite the buffer when in readonly mode"); auto effective_filename = not filename ? buffer.name() : parse_filename(*filename); - auto mode = atomic ? WriteMethod::Replace : context.options()["writemethod"].get<WriteMethod>(); + auto method = write_method.value_or_compute([&] { return context.options()["writemethod"].get<WriteMethod>(); }); context.hooks().run_hook(Hook::BufWritePre, effective_filename, context); - write_buffer_to_file(buffer, effective_filename, mode, flags); + write_buffer_to_file(buffer, effective_filename, method, flags); context.hooks().run_hook(Hook::BufWritePost, effective_filename, context); } @@ -491,7 +500,7 @@ void write_buffer(const ParametersParser& parser, Context& context, const ShellC parser.positional_count() > 0 ? parser[0] : Optional<String>{}, (parser.get_switch("sync") ? WriteFlags::Sync : WriteFlags::None) | (force ? WriteFlags::Force : WriteFlags::None), - (bool)parser.get_switch("atomic")); + parser.get_switch("method").map(parse_write_method)); } const CommandDesc write_cmd = { @@ -518,7 +527,7 @@ const CommandDesc force_write_cmd = { write_buffer<true>, }; -void write_all_buffers(const Context& context, bool sync = false, bool atomic = false) +void write_all_buffers(const Context& context, bool sync = false, Optional<WriteMethod> write_method = {}) { // Copy buffer list because hooks might be creating/deleting buffers Vector<SafePtr<Buffer>> buffers; @@ -532,10 +541,10 @@ void write_all_buffers(const Context& context, bool sync = false, bool atomic = buffer->is_modified()) and !(buffer->flags() & Buffer::Flags::ReadOnly)) { - auto mode = atomic ? WriteMethod::Replace : context.options()["writemethod"].get<WriteMethod>(); + auto method = write_method.value_or_compute([&] { return context.options()["writemethod"].get<WriteMethod>(); }); auto flags = sync ? WriteFlags::Sync : WriteFlags::None; buffer->run_hook_in_own_context(Hook::BufWritePre, buffer->name(), context.name()); - write_buffer_to_file(*buffer, buffer->name(), mode, flags); + write_buffer_to_file(*buffer, buffer->name(), method, flags); buffer->run_hook_in_own_context(Hook::BufWritePost, buffer->name(), context.name()); } } @@ -554,7 +563,8 @@ const CommandDesc write_all_cmd = { CommandCompleter{}, [](const ParametersParser& parser, Context& context, const ShellContext&){ write_all_buffers(context, - (bool)parser.get_switch("sync"), (bool)parser.get_switch("atomic")); + (bool)parser.get_switch("sync"), + parser.get_switch("method").map(parse_write_method)); } }; @@ -661,7 +671,9 @@ template<bool force> void write_quit(const ParametersParser& parser, Context& context, const ShellContext& shell_context) { - do_write_buffer(context, {}, parser.get_switch("sync") ? WriteFlags::Sync : WriteFlags::None); + do_write_buffer(context, {}, + parser.get_switch("sync") ? WriteFlags::Sync : WriteFlags::None, + parser.get_switch("method").map(parse_write_method)); quit<force>(parser, context, shell_context); } @@ -700,7 +712,9 @@ const CommandDesc write_all_quit_cmd = { CommandCompleter{}, [](const ParametersParser& parser, Context& context, const ShellContext& shell_context) { - write_all_buffers(context, (bool)parser.get_switch("sync")); + write_all_buffers(context, + (bool)parser.get_switch("sync"), + parser.get_switch("method").map(parse_write_method)); quit<false>(parser, context, shell_context); } }; diff --git a/src/main.cc b/src/main.cc index 518278a1..fbb074a0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -48,6 +48,7 @@ struct { "» {+u}set-option -remove{} support\n" "» prompts auto select {+i}menu{} completions on space\n" "» explicit completion support ({+b}<c-x>...{}) in prompts\n" + "» {+u}write -atomic{} was replaced with {+u}write -method <method>{}\n" }, { 20200901, "» daemon mode does not fork anymore\n" |
