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"
|
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
|
|
|
|
{
|
|
|
|
|
2011-09-09 22:40:59 +04:00
|
|
|
struct name_not_unique : logic_error {};
|
2011-09-08 04:11:48 +04:00
|
|
|
|
2012-06-14 17:15:30 +04:00
|
|
|
BufferManager::~BufferManager()
|
|
|
|
{
|
|
|
|
// delete remaining buffers
|
|
|
|
while (not m_buffers.empty())
|
2012-08-08 21:36:40 +04:00
|
|
|
delete m_buffers.front().get();
|
2012-06-14 17:15:30 +04:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:36:40 +04:00
|
|
|
void BufferManager::register_buffer(Buffer& buffer)
|
2011-09-08 04:11:48 +04:00
|
|
|
{
|
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();
|
|
|
|
}
|
2011-09-08 04:11:48 +04:00
|
|
|
|
2012-12-28 16:50:02 +04:00
|
|
|
m_buffers.emplace(m_buffers.begin(), &buffer);
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:36:40 +04:00
|
|
|
void BufferManager::unregister_buffer(Buffer& buffer)
|
2013-04-10 20:54:01 +04:00
|
|
|
{
|
|
|
|
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == &buffer)
|
|
|
|
{
|
|
|
|
m_buffers.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferManager::delete_buffer(Buffer& buffer)
|
2011-09-08 04:11:48 +04:00
|
|
|
{
|
2012-08-08 21:36:40 +04:00
|
|
|
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
|
2012-03-26 18:21:49 +04:00
|
|
|
{
|
2012-08-08 21:36:40 +04:00
|
|
|
if (*it == &buffer)
|
|
|
|
{
|
2013-03-22 17:26:44 +04:00
|
|
|
if (ClientManager::has_instance())
|
|
|
|
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
|
2013-04-10 20:54:01 +04:00
|
|
|
delete it->get();
|
2012-08-08 21:36:40 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-03-26 18:21:49 +04:00
|
|
|
}
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(false);
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 12:53:37 +04:00
|
|
|
void BufferManager::delete_buffer_if_exists(StringView name)
|
2013-04-10 20:54:01 +04:00
|
|
|
{
|
|
|
|
if (Buffer* buf = get_buffer_ifp(name))
|
|
|
|
delete_buffer(*buf);
|
|
|
|
}
|
|
|
|
|
2014-04-19 12:53:37 +04:00
|
|
|
Buffer* BufferManager::get_buffer_ifp(StringView name)
|
2011-09-08 04:11:48 +04:00
|
|
|
{
|
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
|
|
|
|
(buf->flags() & Buffer::Flags::File and
|
|
|
|
real_path(buf->name()) == real_path(parse_filename(name))))
|
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)
|
2014-04-19 12:53:37 +04:00
|
|
|
throw runtime_error("no such buffer '"_str + name + "'");
|
2013-03-21 22:09:31 +04:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
2012-09-28 16:14:49 +04:00
|
|
|
void BufferManager::set_last_used_buffer(Buffer& buffer)
|
|
|
|
{
|
|
|
|
auto it = m_buffers.begin();
|
|
|
|
while (*it != &buffer and it != m_buffers.end())
|
|
|
|
++it;
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(it != m_buffers.end());
|
2012-09-28 16:14:49 +04:00
|
|
|
m_buffers.erase(it);
|
|
|
|
m_buffers.emplace(m_buffers.begin(), &buffer);
|
|
|
|
}
|
|
|
|
|
2014-04-18 17:02:14 +04:00
|
|
|
CandidateList BufferManager::complete_buffer_name(StringView prefix,
|
2014-04-08 00:43:23 +04:00
|
|
|
ByteCount cursor_pos)
|
2011-11-12 18:06:49 +04:00
|
|
|
{
|
2014-04-18 17:02:14 +04:00
|
|
|
auto real_prefix = prefix.substr(0, cursor_pos);
|
2013-09-25 21:59:03 +04:00
|
|
|
const bool include_dirs = contains(real_prefix, '/');
|
2011-11-12 18:06:49 +04:00
|
|
|
CandidateList result;
|
2013-09-24 03:54:55 +04:00
|
|
|
CandidateList subsequence_result;
|
2011-11-12 18:06:49 +04:00
|
|
|
for (auto& buffer : m_buffers)
|
|
|
|
{
|
2013-03-25 22:58:23 +04:00
|
|
|
String name = buffer->display_name();
|
2014-08-01 01:43:36 +04:00
|
|
|
StringView match_name = name;
|
2013-09-25 21:59:03 +04:00
|
|
|
if (not include_dirs and buffer->flags() & Buffer::Flags::File)
|
|
|
|
{
|
|
|
|
ByteCount pos = name.find_last_of('/');
|
2013-11-11 02:51:52 +04:00
|
|
|
if (pos != (int)String::npos)
|
2014-08-03 13:00:34 +04:00
|
|
|
match_name = name.substr(pos+1);
|
2013-09-25 21:59:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix_match(match_name, real_prefix))
|
2012-08-29 23:52:17 +04:00
|
|
|
result.push_back(escape(name));
|
2014-08-01 01:43:36 +04:00
|
|
|
if (subsequence_match(name, real_prefix))
|
2013-09-24 03:54:55 +04:00
|
|
|
subsequence_result.push_back(escape(name));
|
2011-11-12 18:06:49 +04:00
|
|
|
}
|
2013-09-24 03:54:55 +04:00
|
|
|
return result.empty() ? subsequence_result : result;
|
2011-11-12 18:06:49 +04:00
|
|
|
}
|
2011-09-08 04:11:48 +04:00
|
|
|
|
|
|
|
}
|