Use the buffer manager to delete buffer, throw when a client is inserting

This commit is contained in:
Maxime Coste 2013-04-10 18:54:01 +02:00
parent 441f9a69ef
commit 9999e5698d
5 changed files with 31 additions and 6 deletions

View File

@ -37,8 +37,6 @@ void BufferManager::unregister_buffer(Buffer& buffer)
{
if (*it == &buffer)
{
if (ClientManager::has_instance())
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
m_buffers.erase(it);
return;
}
@ -46,6 +44,27 @@ void BufferManager::unregister_buffer(Buffer& buffer)
kak_assert(false);
}
void BufferManager::delete_buffer(Buffer& buffer)
{
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
{
if (*it == &buffer)
{
if (ClientManager::has_instance())
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
delete it->get();
return;
}
}
kak_assert(false);
}
void BufferManager::delete_buffer_if_exists(const String& name)
{
if (Buffer* buf = get_buffer_ifp(name))
delete_buffer(*buf);
}
Buffer* BufferManager::get_buffer_ifp(const String& name)
{
for (auto& buf : m_buffers)

View File

@ -22,6 +22,9 @@ public:
void register_buffer(Buffer& buffer);
void unregister_buffer(Buffer& buffer);
void delete_buffer(Buffer& buffer);
void delete_buffer_if_exists(const String& name);
iterator begin() const { return m_buffers.cbegin(); }
iterator end() const { return m_buffers.cend(); }
size_t count() const { return m_buffers.size(); }

View File

@ -138,6 +138,10 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
if (&client->context().buffer() != &buffer)
continue;
if (client->context().editor().is_editing())
throw runtime_error("client '" + client->name + "' is inserting in '" +
buffer.display_name() + '\'');
// 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.

View File

@ -52,7 +52,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
if (fd < 0)
throw runtime_error("unable to open " + filename);
delete BufferManager::instance().get_buffer_ifp(name);
BufferManager::instance().delete_buffer_if_exists(name);
Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
@ -234,7 +234,7 @@ void delete_buffer(const CommandParameters& params, Context& context)
if (manager.count() == 1)
throw runtime_error("buffer " + buffer.name() + " is the last one");
delete &buffer;
manager.delete_buffer(buffer);
}
template<typename Group>

View File

@ -133,8 +133,7 @@ Buffer* create_buffer_from_file(String filename)
const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
auto cleanup = on_scope_end([&]{ munmap((void*)data, st.st_size); close(fd); });
if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(filename))
delete buffer;
BufferManager::instance().delete_buffer_if_exists(filename);
const char* pos = data;
bool crlf = false;