From 0f7948848e662f1bb3f299b7a8c3e7d335adae1f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 7 Nov 2012 14:02:23 +0100 Subject: [PATCH] Fix buffer deletion from a client when another is editing it --- src/client_manager.cc | 23 +++++++++++++++++++++++ src/client_manager.hh | 1 + src/commands.cc | 18 ++++-------------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index a265187a..07322fed 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -1,6 +1,7 @@ #include "client_manager.hh" #include "event_manager.hh" +#include "buffer_manager.hh" namespace Kakoune { @@ -62,6 +63,28 @@ Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer) const return buffer.new_window(); } +void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) +{ + for (auto& client : m_clients) + { + if (&client.context->buffer() != &buffer) + continue; + + // change client context to edit the first buffer which is not the + // specified one. As BufferManager stores buffer according to last + // access, this selects a sensible buffer to display. + for (auto& buf : BufferManager::instance()) + { + if (buf != &buffer) + { + Window& w = get_unused_window_for_buffer(*buf); + client.context->change_editor(w); + break; + } + } + } +} + void ClientManager::redraw_clients() const { for (auto& client : m_clients) diff --git a/src/client_manager.hh b/src/client_manager.hh index 99245348..131c68cf 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -20,6 +20,7 @@ public: size_t count() const { return m_clients.size(); } Window& get_unused_window_for_buffer(Buffer& buffer) const; + void ensure_no_client_uses_buffer(Buffer& buffer); void redraw_clients() const; private: diff --git a/src/commands.cc b/src/commands.cc index 7bd58031..98b14123 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -401,20 +401,10 @@ void delete_buffer(const CommandParameters& params, Context& context) if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified()) throw runtime_error("buffer " + buffer->name() + " is modified"); - if (&context.buffer() == buffer) - { - if (manager.count() == 1) - throw runtime_error("buffer " + buffer->name() + " is the last one"); - for (auto& buf : manager) - { - if (buf != buffer) - { - Window& w = ClientManager::instance().get_unused_window_for_buffer(*buf); - context.change_editor(w); - break; - } - } - } + if (manager.count() == 1) + throw runtime_error("buffer " + buffer->name() + " is the last one"); + + ClientManager::instance().ensure_no_client_uses_buffer(*buffer); delete buffer; }