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"
|
2011-09-09 22:40:59 +04:00
|
|
|
#include "exception.hh"
|
2012-08-29 23:49:36 +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
|
|
|
{
|
2012-08-08 21:36:40 +04:00
|
|
|
const String& name = buffer.name();
|
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
|
|
|
if (buf->name() == name)
|
|
|
|
throw name_not_unique();
|
|
|
|
}
|
2011-09-08 04:11:48 +04:00
|
|
|
|
2012-08-08 21:36:40 +04:00
|
|
|
m_buffers.push_back(safe_ptr<Buffer>(&buffer));
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:36:40 +04:00
|
|
|
void BufferManager::unregister_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)
|
|
|
|
{
|
|
|
|
m_buffers.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-26 18:21:49 +04:00
|
|
|
}
|
2012-08-08 21:36:40 +04:00
|
|
|
assert(false);
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
Buffer* BufferManager::get_buffer(const String& name)
|
2011-09-08 04:11:48 +04:00
|
|
|
{
|
2012-08-08 21:36:40 +04:00
|
|
|
for (auto& buf : m_buffers)
|
|
|
|
{
|
|
|
|
if (buf->name() == name)
|
|
|
|
return buf.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
assert(it != m_buffers.end());
|
|
|
|
m_buffers.erase(it);
|
|
|
|
m_buffers.emplace(m_buffers.begin(), &buffer);
|
|
|
|
}
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
CandidateList BufferManager::complete_buffername(const String& prefix,
|
2012-08-24 01:56:35 +04:00
|
|
|
CharCount cursor_pos)
|
2011-11-12 18:06:49 +04:00
|
|
|
{
|
2012-04-14 05:17:09 +04:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
2011-11-12 18:06:49 +04:00
|
|
|
CandidateList result;
|
|
|
|
for (auto& buffer : m_buffers)
|
|
|
|
{
|
2012-08-08 21:36:40 +04:00
|
|
|
const String& name = buffer->name();
|
|
|
|
if (name.substr(0, real_prefix.length()) == real_prefix)
|
2012-08-29 23:52:17 +04:00
|
|
|
result.push_back(escape(name));
|
2011-11-12 18:06:49 +04:00
|
|
|
}
|
2012-08-14 16:26:36 +04:00
|
|
|
// no prefix completion found, check regex matching
|
|
|
|
if (result.empty())
|
|
|
|
{
|
2012-08-28 23:46:49 +04:00
|
|
|
try
|
2012-08-14 16:26:36 +04:00
|
|
|
{
|
2012-08-28 23:46:49 +04:00
|
|
|
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))
|
2012-08-29 23:52:17 +04:00
|
|
|
result.push_back(escape(name));
|
2012-08-28 23:46:49 +04:00
|
|
|
}
|
2012-08-14 16:26:36 +04:00
|
|
|
}
|
2012-08-28 23:46:49 +04:00
|
|
|
catch (boost::regex_error& err) {}
|
2012-08-14 16:26:36 +04:00
|
|
|
}
|
2011-11-12 18:06:49 +04:00
|
|
|
return result;
|
|
|
|
}
|
2011-09-08 04:11:48 +04:00
|
|
|
|
|
|
|
}
|