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