From cc2affea11781d4ad1e1b9d7a9c7d729064c2c10 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 13 Oct 2016 21:37:44 +0100 Subject: [PATCH] Always allow to delete a buffer, just recreate a scratch buffer if needed Fixes #850 --- src/buffer_manager.cc | 4 ++-- src/client.cc | 3 ++- src/client_manager.cc | 26 +++++++------------------- src/commands.cc | 3 --- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index bc88a752..ea6519e2 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -57,11 +57,11 @@ void BufferManager::delete_buffer(Buffer& buffer) { return p.get() == &buffer; }); kak_assert(it != m_buffers.end()); - ClientManager::instance().ensure_no_client_uses_buffer(buffer); - m_buffer_trash.emplace_back(std::move(*it)); m_buffers.erase(it); + ClientManager::instance().ensure_no_client_uses_buffer(buffer); + buffer.on_unregistered(); } diff --git a/src/client.cc b/src/client.cc index a2d06cf0..405a82d8 100644 --- a/src/client.cc +++ b/src/client.cc @@ -144,7 +144,8 @@ void Client::change_buffer(Buffer& buffer) if (m_buffer_reload_dialog_opened) close_buffer_reload_dialog(); - m_last_buffer = &m_window->buffer(); + auto* current = &m_window->buffer(); + m_last_buffer = contains(BufferManager::instance(), current) ? current : nullptr; auto& client_manager = ClientManager::instance(); m_window->options().unregister_watcher(*this); diff --git a/src/client_manager.cc b/src/client_manager.cc index 889f0777..666df922 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -109,33 +109,21 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) { for (auto& client : m_clients) { - client->context().jump_list().forget_buffer(buffer); + auto& context = client->context(); + context.jump_list().forget_buffer(buffer); if (client->last_buffer() == &buffer) client->set_last_buffer(nullptr); - if (&client->context().buffer() != &buffer) + if (&context.buffer() != &buffer) continue; - if (client->context().is_editing()) + if (context.is_editing()) throw runtime_error(format("client '{}' is inserting in buffer '{}'", - client->context().name(), + context.name(), buffer.display_name())); - if (Buffer* last_buffer = client->last_buffer()) - { - client->context().change_buffer(*last_buffer); - continue; - } - - for (auto& buf : BufferManager::instance()) - { - if (buf.get() != &buffer) - { - client->context().change_buffer(*buf); - client->set_last_buffer(nullptr); - break; - } - } + Buffer* last = client->last_buffer(); + context.change_buffer(last ? *last : BufferManager::instance().get_first_buffer()); } auto end = std::remove_if(m_free_windows.begin(), m_free_windows.end(), [&buffer](const WindowAndSelections& ws) diff --git a/src/commands.cc b/src/commands.cc index cae54822..b5034959 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -566,9 +566,6 @@ void delete_buffer(const ParametersParser& parser, Context& context, const Shell if (not force and (buffer.flags() & Buffer::Flags::File) and buffer.is_modified()) throw runtime_error(format("buffer '{}' is modified", buffer.name())); - if (manager.count() == 1) - throw runtime_error(format("buffer '{}' is the last one", buffer.name())); - manager.delete_buffer(buffer); }