1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 19:11:40 +03:00
kakoune/src/buffer_manager.cc

123 lines
3.2 KiB
C++
Raw Normal View History

#include "buffer_manager.hh"
2011-09-09 23:24:18 +04:00
#include "assert.hh"
#include "buffer.hh"
#include "client_manager.hh"
#include "containers.hh"
2013-04-09 22:05:40 +04:00
#include "exception.hh"
#include "file.hh"
2013-04-09 22:05:40 +04:00
#include "string.hh"
namespace Kakoune
{
struct name_not_unique : runtime_error
{
name_not_unique() : runtime_error("buffer name is already in use") {}
};
BufferManager::~BufferManager()
{
// Move buffers to m_buffer_trash to avoid running BufClose
// hook while clearing m_buffers
m_buffer_trash = std::move(m_buffers);
for (auto& buffer : m_buffer_trash)
buffer->on_unregistered();
// Make sure not clients exists
ClientManager::instance().clear();
}
Buffer* BufferManager::create_buffer(String name, Buffer::Flags flags,
StringView data, timespec fs_timestamp)
{
auto path = real_path(parse_filename(name));
2012-08-08 21:36:40 +04:00
for (auto& buf : m_buffers)
{
if (buf->name() == name or
(buf->flags() & Buffer::Flags::File and buf->name() == path))
2012-08-08 21:36:40 +04:00
throw name_not_unique();
}
m_buffers.emplace(m_buffers.begin(),
new Buffer{std::move(name), flags, data, fs_timestamp});
auto& buffer = *m_buffers.front();
buffer.on_registered();
if (contains(m_buffer_trash, &buffer))
throw runtime_error("Buffer got removed during its creation");
return &buffer;
}
void BufferManager::delete_buffer(Buffer& buffer)
{
auto it = find_if(m_buffers, [&](const std::unique_ptr<Buffer>& p)
{ return p.get() == &buffer; });
kak_assert(it != m_buffers.end());
m_buffer_trash.emplace_back(std::move(*it));
m_buffers.erase(it);
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
buffer.on_unregistered();
}
2014-04-19 12:53:37 +04:00
Buffer* BufferManager::get_buffer_ifp(StringView name)
{
auto path = real_path(parse_filename(name));
2012-08-08 21:36:40 +04:00
for (auto& buf : m_buffers)
{
if (buf->name() == name or
(buf->flags() & Buffer::Flags::File and buf->name() == path))
2012-08-08 21:36:40 +04:00
return buf.get();
}
return nullptr;
}
2014-04-19 12:53:37 +04:00
Buffer& BufferManager::get_buffer(StringView name)
{
Buffer* res = get_buffer_ifp(name);
if (not res)
throw runtime_error(format("no such buffer '{}'", name));
return *res;
}
Buffer& BufferManager::get_first_buffer()
{
if (not contains_that(m_buffers, [](const std::unique_ptr<Buffer>& p)
{ return not (p->flags() & Buffer::Flags::Debug); }))
create_buffer("*scratch*", Buffer::Flags::None);
return *m_buffers.front();
}
void BufferManager::backup_modified_buffers()
{
for (auto& buf : m_buffers)
{
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified()
and not (buf->flags() & Buffer::Flags::ReadOnly))
write_buffer_to_backup_file(*buf);
}
}
void BufferManager::clear_buffer_trash()
{
for (auto& buffer : m_buffer_trash)
{
// Do that again, to be tolerant in some corner cases, where a buffer is
// deleted during its creation
ClientManager::instance().ensure_no_client_uses_buffer(*buffer);
ClientManager::instance().clear_window_trash();
buffer.reset();
}
m_buffer_trash.clear();
}
}