1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-23 03:21:55 +03:00
kakoune/src/buffer_manager.cc

118 lines
2.7 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
{
2011-09-09 22:40:59 +04:00
struct name_not_unique : logic_error {};
BufferManager::~BufferManager()
{
// delete remaining buffers
while (not m_buffers.empty())
2012-08-08 21:36:40 +04:00
delete m_buffers.front().get();
}
2012-08-08 21:36:40 +04:00
void BufferManager::register_buffer(Buffer& buffer)
{
2014-04-19 12:53:37 +04:00
StringView name = buffer.name();
2012-08-08 21:36:40 +04:00
for (auto& buf : m_buffers)
{
if (buf->name() == name)
throw name_not_unique();
}
m_buffers.emplace(m_buffers.begin(), &buffer);
}
2012-08-08 21:36:40 +04:00
void BufferManager::unregister_buffer(Buffer& buffer)
{
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
{
2015-02-23 23:39:56 +03:00
if (it->get() == &buffer)
{
m_buffers.erase(it);
return;
}
}
for (auto it = m_buffer_trash.begin(); it != m_buffer_trash.end(); ++it)
{
2015-02-23 23:39:56 +03:00
if (it->get() == &buffer)
{
m_buffer_trash.erase(it);
return;
}
}
kak_assert(false);
}
void BufferManager::delete_buffer(Buffer& buffer)
{
2012-08-08 21:36:40 +04:00
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
{
2015-02-23 23:39:56 +03:00
if (it->get() == &buffer)
2012-08-08 21:36:40 +04:00
{
if (ClientManager::has_instance())
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
m_buffers.erase(it);
m_buffer_trash.emplace_back(&buffer);
2012-08-08 21:36:40 +04:00
return;
}
}
kak_assert(false);
}
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;
}
void BufferManager::set_last_used_buffer(Buffer& buffer)
{
2015-02-23 23:39:56 +03:00
auto it = find_if(m_buffers, [&buffer](const SafePtr<Buffer>& p)
{ return p.get() == &buffer; });
kak_assert(it != m_buffers.end());
m_buffers.erase(it);
m_buffers.emplace(m_buffers.begin(), &buffer);
}
void BufferManager::backup_modified_buffers()
{
for (auto& buf : m_buffers)
{
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified())
write_buffer_to_backup_file(*buf);
}
}
void BufferManager::clear_buffer_trash()
{
while (not m_buffer_trash.empty())
delete m_buffer_trash.back().get();
}
}