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"
|
2011-09-09 20:40:59 +02:00
|
|
|
#include "exception.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-06-28 13:45:42 +02:00
|
|
|
delete m_buffers.begin()->second.get();
|
2012-06-14 15:15:30 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 02:11:48 +02:00
|
|
|
void BufferManager::register_buffer(Buffer* buffer)
|
|
|
|
{
|
|
|
|
assert(buffer);
|
2012-04-14 03:17:09 +02:00
|
|
|
const String& name = buffer->name();
|
2011-09-08 02:11:48 +02:00
|
|
|
if (m_buffers.find(name) != m_buffers.end())
|
|
|
|
throw name_not_unique();
|
|
|
|
|
2012-06-28 13:45:42 +02:00
|
|
|
m_buffers[name] = safe_ptr<Buffer>(buffer);
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2012-03-26 16:21:49 +02:00
|
|
|
void BufferManager::unregister_buffer(Buffer* buffer)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
|
|
|
assert(buffer);
|
|
|
|
auto buffer_it = m_buffers.find(buffer->name());
|
2012-03-26 16:21:49 +02:00
|
|
|
if (buffer_it != m_buffers.end())
|
|
|
|
{
|
|
|
|
assert(buffer_it->second == buffer);
|
|
|
|
m_buffers.erase(buffer_it);
|
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
Buffer* BufferManager::get_buffer(const String& name)
|
2011-09-08 02:11:48 +02:00
|
|
|
{
|
|
|
|
if (m_buffers.find(name) == m_buffers.end())
|
|
|
|
return nullptr;
|
|
|
|
|
2012-06-28 13:45:42 +02:00
|
|
|
return m_buffers[name].get();
|
2011-09-08 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList BufferManager::complete_buffername(const String& prefix,
|
2011-11-12 15:06:49 +01:00
|
|
|
size_t cursor_pos)
|
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
if (buffer.first.substr(0, real_prefix.length()) == real_prefix)
|
|
|
|
result.push_back(buffer.first);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2011-09-08 02:11:48 +02:00
|
|
|
|
|
|
|
}
|