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"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2011-09-08 02:11:48 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2011-09-09 20:40:59 +02:00
|
|
|
struct name_not_unique : logic_error {};
|
2011-09-08 02:11:48 +02:00
|
|
|
|
2012-06-14 15:15:30 +02:00
|
|
|
BufferManager::~BufferManager()
|
|
|
|
{
|
|
|
|
// delete remaining buffers
|
|
|
|
while (not m_buffers.empty())
|
2012-08-08 19:36:40 +02:00
|
|
|
delete m_buffers.front().get();
|
2012-06-14 15:15:30 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 19:36:40 +02:00
|
|
|
void BufferManager::register_buffer(Buffer& buffer)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
2012-08-08 19:36:40 +02:00
|
|
|
const String& name = buffer.name();
|
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
|
|
|
if (buf->name() == name)
|
|
|
|
throw name_not_unique();
|
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
|
2012-12-28 13:50:02 +01:00
|
|
|
m_buffers.emplace(m_buffers.begin(), &buffer);
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 19:36:40 +02:00
|
|
|
void BufferManager::unregister_buffer(Buffer& buffer)
|
2013-04-10 18:54:01 +02: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 02:11:48 +02:00
|
|
|
{
|
2012-08-08 19:36:40 +02:00
|
|
|
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
|
2012-03-26 16:21:49 +02:00
|
|
|
{
|
2012-08-08 19:36:40 +02:00
|
|
|
if (*it == &buffer)
|
|
|
|
{
|
2013-03-22 14:26:44 +01:00
|
|
|
if (ClientManager::has_instance())
|
|
|
|
ClientManager::instance().ensure_no_client_uses_buffer(buffer);
|
2013-04-10 18:54:01 +02:00
|
|
|
delete it->get();
|
2012-08-08 19:36:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-03-26 16:21:49 +02:00
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(false);
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2013-04-10 18:54:01 +02:00
|
|
|
void BufferManager::delete_buffer_if_exists(const String& name)
|
|
|
|
{
|
|
|
|
if (Buffer* buf = get_buffer_ifp(name))
|
|
|
|
delete_buffer(*buf);
|
|
|
|
}
|
|
|
|
|
2013-03-21 19:09:31 +01:00
|
|
|
Buffer* BufferManager::get_buffer_ifp(const String& name)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
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
|
|
|
|
(buf->flags() & Buffer::Flags::File and
|
|
|
|
real_path(buf->name()) == real_path(parse_filename(name))))
|
2012-08-08 19:36:40 +02:00
|
|
|
return buf.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2013-03-21 19:09:31 +01:00
|
|
|
Buffer& BufferManager::get_buffer(const String& name)
|
|
|
|
{
|
|
|
|
Buffer* res = get_buffer_ifp(name);
|
|
|
|
if (not res)
|
|
|
|
throw runtime_error("no such buffer '" + name + "'");
|
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:14:49 +02: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 20:04:11 +02:00
|
|
|
kak_assert(it != m_buffers.end());
|
2012-09-28 14:14:49 +02:00
|
|
|
m_buffers.erase(it);
|
|
|
|
m_buffers.emplace(m_buffers.begin(), &buffer);
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList BufferManager::complete_buffername(const String& prefix,
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount cursor_pos)
|
2011-11-12 15:06:49 +01:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
2011-11-12 15:06:49 +01:00
|
|
|
CandidateList result;
|
|
|
|
for (auto& buffer : m_buffers)
|
|
|
|
{
|
2013-03-25 19:58:23 +01:00
|
|
|
String name = buffer->display_name();
|
2013-09-23 21:16:25 +02:00
|
|
|
if (prefix_match(name, real_prefix))
|
2012-08-29 21:52:17 +02:00
|
|
|
result.push_back(escape(name));
|
2011-11-12 15:06:49 +01:00
|
|
|
}
|
2012-08-14 14:26:36 +02:00
|
|
|
// no prefix completion found, check regex matching
|
|
|
|
if (result.empty())
|
|
|
|
{
|
2012-08-28 21:46:49 +02:00
|
|
|
try
|
2012-08-14 14:26:36 +02:00
|
|
|
{
|
2012-08-28 21:46:49 +02:00
|
|
|
Regex ex(real_prefix.begin(), real_prefix.end());
|
|
|
|
for (auto& buffer : m_buffers)
|
|
|
|
{
|
2013-03-25 19:58:23 +01:00
|
|
|
String name = buffer->display_name();
|
2012-08-28 21:46:49 +02:00
|
|
|
if (boost::regex_search(name.begin(), name.end(), ex))
|
2012-08-29 21:52:17 +02:00
|
|
|
result.push_back(escape(name));
|
2012-08-28 21:46:49 +02:00
|
|
|
}
|
2012-08-14 14:26:36 +02:00
|
|
|
}
|
2012-08-28 21:46:49 +02:00
|
|
|
catch (boost::regex_error& err) {}
|
2012-08-14 14:26:36 +02:00
|
|
|
}
|
2011-11-12 15:06:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
|
|
|
|
}
|