diff --git a/README.asciidoc b/README.asciidoc index 42b0ac7e..d78b034a 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -599,7 +599,8 @@ command `q!` has to be used). * `w[rite]a[ll]`: write all buffers that are associated to a file. * `q[uit][!]`: exit Kakoune, use quit! to force quitting even if there is some unsaved buffers remaining. - * `kill`: terminate the current session, all the clients as well as the server + * `kill[!]`: terminate the current session, all the clients as well as the server, + use kill! to ignore unsaved buffers * `w[a]q[!]`: write the current buffer (or all buffers when `waq` is used) and quit * `b[uffer] `: switch to buffer * `b[uffer]n[ext]`: switch to the next buffer diff --git a/src/commands.cc b/src/commands.cc index 119aa0fe..efa61f80 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -271,6 +271,28 @@ const CommandDesc writeall_cmd = { [](const ParametersParser&, Context&, const ShellContext&){ write_all_buffers(); } }; +static void ensure_all_buffers_are_saved() +{ + Vector names; + for (auto& buffer : BufferManager::instance()) + { + if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) + names.push_back(buffer->name()); + } + if (not names.empty()) + { + String message = "modified buffers remaining: ["; + for (auto it = names.begin(); it != names.end(); ++it) + { + if (it != names.begin()) + message += ", "; + message += *it; + } + message += "]"; + throw runtime_error(message); + } +} + const CommandDesc kill_cmd = { "kill", nullptr, @@ -279,6 +301,21 @@ const CommandDesc kill_cmd = { CommandFlags::None, CommandHelper{}, CommandCompleter{}, + [](const ParametersParser&, Context&, const ShellContext&){ + ensure_all_buffers_are_saved(); + throw kill_session{}; + } +}; + + +const CommandDesc force_kill_cmd = { + "kill!", + nullptr, + "kill current session, quit all clients and server, do not check for unsaved buffers", + no_params, + CommandFlags::None, + CommandHelper{}, + CommandCompleter{}, [](const ParametersParser&, Context&, const ShellContext&){ throw kill_session{}; } }; @@ -286,26 +323,7 @@ template void quit() { if (not force and ClientManager::instance().count() == 1) - { - Vector names; - for (auto& buffer : BufferManager::instance()) - { - if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) - names.push_back(buffer->name()); - } - if (not names.empty()) - { - String message = "modified buffers remaining: ["; - for (auto it = names.begin(); it != names.end(); ++it) - { - if (it != names.begin()) - message += ", "; - message += *it; - } - message += "]"; - throw runtime_error(message); - } - } + ensure_all_buffers_are_saved(); // unwind back to this client event handler. throw client_removed{ true }; } @@ -1872,8 +1890,9 @@ void register_commands() register_command(write_cmd); register_command(writeall_cmd); register_command(writeall_quit_cmd); - register_command(quit_cmd); register_command(kill_cmd); + register_command(force_kill_cmd); + register_command(quit_cmd); register_command(force_quit_cmd); register_command(write_quit_cmd); register_command(force_write_quit_cmd);