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"
|
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
|
|
|
|
|
|
|
void BufferManager::register_buffer(Buffer* buffer)
|
|
|
|
{
|
|
|
|
assert(buffer);
|
|
|
|
const std::string& name = buffer->name();
|
|
|
|
if (m_buffers.find(name) != m_buffers.end())
|
|
|
|
throw name_not_unique();
|
|
|
|
|
2012-03-26 18:21:49 +04:00
|
|
|
m_buffers[name] = buffer;
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 18:21:49 +04:00
|
|
|
void BufferManager::unregister_buffer(Buffer* buffer)
|
2011-09-08 04:11:48 +04:00
|
|
|
{
|
|
|
|
assert(buffer);
|
|
|
|
auto buffer_it = m_buffers.find(buffer->name());
|
2012-03-26 18:21:49 +04:00
|
|
|
if (buffer_it != m_buffers.end())
|
|
|
|
{
|
|
|
|
assert(buffer_it->second == buffer);
|
|
|
|
m_buffers.erase(buffer_it);
|
|
|
|
}
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer* BufferManager::get_buffer(const std::string& name)
|
|
|
|
{
|
|
|
|
if (m_buffers.find(name) == m_buffers.end())
|
|
|
|
return nullptr;
|
|
|
|
|
2012-03-26 18:21:49 +04:00
|
|
|
return m_buffers[name];
|
2011-09-08 04:11:48 +04:00
|
|
|
}
|
|
|
|
|
2011-11-12 18:06:49 +04:00
|
|
|
CandidateList BufferManager::complete_buffername(const std::string& prefix,
|
|
|
|
size_t cursor_pos)
|
|
|
|
{
|
|
|
|
std::string real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
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 04:11:48 +04:00
|
|
|
|
|
|
|
}
|