diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index bc88a7527..ea6519e28 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 a2d06cf09..405a82d84 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 889f07779..666df9227 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 cae548223..b5034959e 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); }