home/src/buffer_manager.cc

95 lines
2.2 KiB
C++
Raw Normal View History

#include "buffer_manager.hh"
2011-09-09 21:24:18 +02:00
#include "assert.hh"
#include "buffer.hh"
2011-09-09 20:40:59 +02:00
#include "exception.hh"
#include "string.hh"
namespace Kakoune
{
2011-09-09 20:40:59 +02:00
struct name_not_unique : logic_error {};
BufferManager::~BufferManager()
{
// delete remaining buffers
while (not m_buffers.empty())
2012-08-08 19:36:40 +02:00
delete m_buffers.front().get();
}
2012-08-08 19:36:40 +02:00
void BufferManager::register_buffer(Buffer& buffer)
{
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();
}
2012-08-08 19:36:40 +02:00
m_buffers.push_back(safe_ptr<Buffer>(&buffer));
}
2012-08-08 19:36:40 +02:00
void BufferManager::unregister_buffer(Buffer& buffer)
{
2012-08-08 19:36:40 +02:00
for (auto it = m_buffers.begin(); it != m_buffers.end(); ++it)
{
2012-08-08 19:36:40 +02:00
if (*it == &buffer)
{
m_buffers.erase(it);
return;
}
}
2012-08-08 19:36:40 +02:00
assert(false);
}
Buffer* BufferManager::get_buffer(const String& name)
{
2012-08-08 19:36:40 +02:00
for (auto& buf : m_buffers)
{
if (buf->name() == name)
return buf.get();
}
return nullptr;
}
void BufferManager::set_last_used_buffer(Buffer& buffer)
{
auto it = m_buffers.begin();
while (*it != &buffer and it != m_buffers.end())
++it;
assert(it != m_buffers.end());
m_buffers.erase(it);
m_buffers.emplace(m_buffers.begin(), &buffer);
}
CandidateList BufferManager::complete_buffername(const String& prefix,
CharCount cursor_pos)
{
String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
for (auto& buffer : m_buffers)
{
2012-08-08 19:36:40 +02:00
const String& name = buffer->name();
if (name.substr(0, real_prefix.length()) == real_prefix)
result.push_back(escape(name));
}
// no prefix completion found, check regex matching
if (result.empty())
{
try
{
Regex ex(real_prefix.begin(), real_prefix.end());
for (auto& buffer : m_buffers)
{
const String& name = buffer->name();
if (boost::regex_search(name.begin(), name.end(), ex))
result.push_back(escape(name));
}
}
catch (boost::regex_error& err) {}
}
return result;
}
}