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"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2013-03-25 19:58:23 +01:00
|
|
|
#include "file.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "ranges.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2011-09-08 02:11:48 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-06-14 15:15:30 +02:00
|
|
|
BufferManager::~BufferManager()
|
|
|
|
{
|
2018-05-21 12:30:24 +02:00
|
|
|
// Move buffers to avoid running BufClose with buffers remaining in that list
|
|
|
|
BufferList buffers = std::move(m_buffers);
|
2016-07-04 20:20:22 +02:00
|
|
|
|
2018-05-21 12:30:24 +02:00
|
|
|
for (auto& buffer : buffers)
|
2016-07-10 17:01:33 +02:00
|
|
|
buffer->on_unregistered();
|
|
|
|
|
2016-03-03 14:55:35 +01:00
|
|
|
// Make sure not clients exists
|
2018-02-18 04:50:24 +01:00
|
|
|
if (ClientManager::has_instance())
|
2019-04-12 01:02:59 +02:00
|
|
|
ClientManager::instance().clear(true);
|
2012-06-14 15:15:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
Buffer* BufferManager::create_buffer(String name, Buffer::Flags flags, BufferLines lines, ByteOrderMark bom, EolFormat eolformat, FsStatus fs_status)
|
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))
|
2016-11-29 20:12:10 +01:00
|
|
|
throw runtime_error{"buffer name is already in use"};
|
2012-08-08 19:36:40 +02:00
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
m_buffers.push_back(std::make_unique<Buffer>(std::move(name), flags, lines, bom, eolformat, fs_status));
|
2018-04-05 02:31:41 +02:00
|
|
|
auto* buffer = m_buffers.back().get();
|
|
|
|
buffer->on_registered();
|
2016-07-10 17:01:33 +02:00
|
|
|
|
2018-04-05 02:31:41 +02:00
|
|
|
if (contains(m_buffer_trash, buffer))
|
2018-04-06 16:56:53 +02:00
|
|
|
throw runtime_error{"buffer got removed during its creation"};
|
2016-10-11 01:45:05 +02:00
|
|
|
|
2018-04-05 02:31:41 +02:00
|
|
|
return buffer;
|
2013-04-10 18:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferManager::delete_buffer(Buffer& buffer)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
2018-03-25 07:47:19 +02:00
|
|
|
auto it = find_if(m_buffers, [&](auto& p) { return p.get() == &buffer; });
|
2019-11-22 10:29:55 +01:00
|
|
|
if (it == m_buffers.end()) // we might be trying to recursively delete this buffer
|
|
|
|
return;
|
2019-02-16 10:02:46 +01: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);
|
2016-07-10 17:01:33 +02:00
|
|
|
|
2018-02-18 04:50:24 +01:00
|
|
|
if (ClientManager::has_instance())
|
|
|
|
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
|
2019-11-22 10:29:55 +01:00
|
|
|
|
|
|
|
buffer.on_unregistered();
|
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)
|
2016-11-29 20:12:10 +01:00
|
|
|
throw runtime_error{format("no such buffer '{}'", name)};
|
2013-03-21 19:09:31 +01:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:45:05 +02:00
|
|
|
Buffer& BufferManager::get_first_buffer()
|
|
|
|
{
|
2018-03-25 07:47:19 +02:00
|
|
|
if (all_of(m_buffers, [](auto& b) { return (b->flags() & Buffer::Flags::Debug); }))
|
2019-03-17 00:41:11 +01:00
|
|
|
create_buffer("*scratch*", Buffer::Flags::None,
|
2021-05-28 09:03:06 +02:00
|
|
|
{StringData::create({"*** this is a *scratch* buffer which won't be automatically saved ***\n"}),
|
|
|
|
StringData::create({"*** use it for notes or open a file buffer with the :edit command ***\n"})},
|
|
|
|
ByteOrderMark::None, EolFormat::Lf, {InvalidTime, {}, {}});
|
2016-10-11 01:45:05 +02:00
|
|
|
|
2018-04-05 02:31:41 +02:00
|
|
|
return *m_buffers.back();
|
2016-10-11 01:45:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 14:38:28 +02:00
|
|
|
void BufferManager::backup_modified_buffers()
|
|
|
|
{
|
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
2016-07-20 19:45:50 +02:00
|
|
|
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified()
|
2016-07-24 07:34:49 +02:00
|
|
|
and not (buf->flags() & Buffer::Flags::ReadOnly))
|
2014-10-13 14:38:28 +02:00
|
|
|
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
|
|
|
m_buffer_trash.clear();
|
2014-08-12 20:24:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 09:41:46 +01:00
|
|
|
void BufferManager::arrange_buffers(ConstArrayView<String> first_ones)
|
|
|
|
{
|
|
|
|
Vector<size_t> indices;
|
|
|
|
for (const auto& name : first_ones)
|
|
|
|
{
|
|
|
|
auto it = find_if(m_buffers, [&](auto& buf) { return buf->name() == name or buf->display_name() == name; });
|
|
|
|
if (it == m_buffers.end())
|
|
|
|
throw runtime_error{format("no such buffer '{}'", name)};
|
|
|
|
size_t index = it - m_buffers.begin();
|
|
|
|
if (contains(indices, index))
|
|
|
|
throw runtime_error{format("buffer '{}' appears more than once", name)};
|
|
|
|
indices.push_back(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferList res;
|
|
|
|
for (size_t index : indices)
|
|
|
|
res.push_back(std::move(m_buffers[index]));
|
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
res.push_back(std::move(buf));
|
|
|
|
}
|
|
|
|
m_buffers = std::move(res);
|
|
|
|
}
|
|
|
|
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|