2011-09-08 02:11:48 +02:00
|
|
|
#include "buffer_manager.hh"
|
|
|
|
|
2011-09-09 21:24:18 +02:00
|
|
|
#include "assert.hh"
|
2011-09-08 02:11:48 +02:00
|
|
|
#include "buffer.hh"
|
2013-03-22 14:26:44 +01:00
|
|
|
#include "client_manager.hh"
|
2014-12-23 14:54:09 +01:00
|
|
|
#include "containers.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2013-03-25 19:58:23 +01:00
|
|
|
#include "file.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2011-09-08 02:11:48 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-07-08 14:43:40 +02:00
|
|
|
struct name_not_unique : runtime_error
|
|
|
|
{
|
|
|
|
name_not_unique() : runtime_error("buffer name is already in use") {}
|
|
|
|
};
|
2011-09-08 02:11:48 +02:00
|
|
|
|
2012-06-14 15:15:30 +02:00
|
|
|
BufferManager::~BufferManager()
|
|
|
|
{
|
2016-07-04 20:20:22 +02:00
|
|
|
// Move buffers to m_buffer_trash to avoid running BufClose
|
|
|
|
// hook while clearing m_buffers
|
|
|
|
m_buffer_trash = std::move(m_buffers);
|
|
|
|
|
2016-03-03 14:55:35 +01:00
|
|
|
// Make sure not clients exists
|
|
|
|
ClientManager::instance().clear();
|
2012-06-14 15:15:30 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 09:33:50 +02:00
|
|
|
Buffer* BufferManager::create_buffer(String name, Buffer::Flags flags,
|
|
|
|
StringView data, timespec fs_timestamp)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
2016-05-15 11:37:01 +02:00
|
|
|
auto path = real_path(parse_filename(name));
|
2012-08-08 19:36:40 +02:00
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
2016-05-15 11:37:01 +02:00
|
|
|
if (buf->name() == name or
|
|
|
|
(buf->flags() & Buffer::Flags::File and buf->name() == path))
|
2012-08-08 19:36:40 +02:00
|
|
|
throw name_not_unique();
|
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
|
2016-05-14 09:33:50 +02:00
|
|
|
m_buffers.emplace(m_buffers.begin(), new Buffer{std::move(name), flags,
|
|
|
|
data, fs_timestamp});
|
|
|
|
return m_buffers.front().get();
|
2013-04-10 18:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferManager::delete_buffer(Buffer& buffer)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
2016-05-14 09:33:50 +02:00
|
|
|
auto it = find_if(m_buffers, [&](const std::unique_ptr<Buffer>& p)
|
2015-09-08 14:10:22 +02:00
|
|
|
{ return p.get() == &buffer; });
|
|
|
|
kak_assert(it != m_buffers.end());
|
2014-08-12 20:24:09 +02:00
|
|
|
|
2016-03-02 14:30:11 +01:00
|
|
|
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
|
2015-09-08 14:10:22 +02:00
|
|
|
|
2016-05-14 09:33:50 +02:00
|
|
|
m_buffer_trash.emplace_back(std::move(*it));
|
2015-09-08 14:10:22 +02:00
|
|
|
m_buffers.erase(it);
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 10:53:37 +02:00
|
|
|
Buffer* BufferManager::get_buffer_ifp(StringView name)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
2015-03-12 20:40:10 +01:00
|
|
|
auto path = real_path(parse_filename(name));
|
2012-08-08 19:36:40 +02:00
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
2013-03-25 19:58:23 +01:00
|
|
|
if (buf->name() == name or
|
2015-03-12 20:40:10 +01:00
|
|
|
(buf->flags() & Buffer::Flags::File and buf->name() == path))
|
2012-08-08 19:36:40 +02:00
|
|
|
return buf.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 10:53:37 +02:00
|
|
|
Buffer& BufferManager::get_buffer(StringView name)
|
2013-03-21 19:09:31 +01:00
|
|
|
{
|
|
|
|
Buffer* res = get_buffer_ifp(name);
|
|
|
|
if (not res)
|
2015-06-01 20:06:35 +02:00
|
|
|
throw runtime_error(format("no such buffer '{}'", name));
|
2013-03-21 19:09:31 +01:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
2014-10-13 14:38:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 20:24:09 +02:00
|
|
|
void BufferManager::clear_buffer_trash()
|
|
|
|
{
|
2016-05-14 09:33:50 +02:00
|
|
|
for (auto& buffer : m_buffer_trash)
|
2015-08-06 22:51:44 +02:00
|
|
|
{
|
|
|
|
// Do that again, to be tolerant in some corner cases, where a buffer is
|
|
|
|
// deleted during its creation
|
2016-03-02 14:30:11 +01:00
|
|
|
ClientManager::instance().ensure_no_client_uses_buffer(*buffer);
|
2016-05-13 21:32:53 +02:00
|
|
|
ClientManager::instance().clear_window_trash();
|
2015-08-06 22:51:44 +02:00
|
|
|
|
2016-05-14 09:33:50 +02:00
|
|
|
buffer.reset();
|
2015-08-06 22:51:44 +02:00
|
|
|
}
|
2016-05-14 09:33:50 +02:00
|
|
|
|
|
|
|
m_buffer_trash.clear();
|
2014-08-12 20:24:09 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|