Fix buffer deletion from a client when another is editing it

This commit is contained in:
Maxime Coste 2012-11-07 14:02:23 +01:00
parent d6923af3c4
commit 0f7948848e
3 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,7 @@
#include "client_manager.hh" #include "client_manager.hh"
#include "event_manager.hh" #include "event_manager.hh"
#include "buffer_manager.hh"
namespace Kakoune namespace Kakoune
{ {
@ -62,6 +63,28 @@ Window& ClientManager::get_unused_window_for_buffer(Buffer& buffer) const
return buffer.new_window(); 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 void ClientManager::redraw_clients() const
{ {
for (auto& client : m_clients) for (auto& client : m_clients)

View File

@ -20,6 +20,7 @@ public:
size_t count() const { return m_clients.size(); } size_t count() const { return m_clients.size(); }
Window& get_unused_window_for_buffer(Buffer& buffer) const; Window& get_unused_window_for_buffer(Buffer& buffer) const;
void ensure_no_client_uses_buffer(Buffer& buffer);
void redraw_clients() const; void redraw_clients() const;
private: private:

View File

@ -401,20 +401,10 @@ void delete_buffer(const CommandParameters& params, Context& context)
if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified()) if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified())
throw runtime_error("buffer " + buffer->name() + " is modified"); throw runtime_error("buffer " + buffer->name() + " is modified");
if (&context.buffer() == buffer)
{
if (manager.count() == 1) if (manager.count() == 1)
throw runtime_error("buffer " + buffer->name() + " is the last one"); throw runtime_error("buffer " + buffer->name() + " is the last one");
for (auto& buf : manager)
{ ClientManager::instance().ensure_no_client_uses_buffer(*buffer);
if (buf != buffer)
{
Window& w = ClientManager::instance().get_unused_window_for_buffer(*buf);
context.change_editor(w);
break;
}
}
}
delete buffer; delete buffer;
} }