2012-06-29 18:37:17 +02:00
|
|
|
#include "register_manager.hh"
|
|
|
|
|
|
|
|
#include "assert.hh"
|
2020-07-19 04:56:55 +02:00
|
|
|
#include "context.hh"
|
2017-03-07 02:12:37 +01:00
|
|
|
#include "hash_map.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
#include "string_utils.hh"
|
2012-06-29 18:37:17 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2020-07-19 04:56:55 +02:00
|
|
|
void StaticRegister::set(Context& context, ConstArrayView<String> values, bool)
|
|
|
|
{
|
|
|
|
m_content.assign(values.begin(), values.end());
|
2020-07-21 12:27:42 +02:00
|
|
|
if (not m_disable_modified_hook)
|
|
|
|
context.hooks().run_hook(Hook::RegisterModified, m_name, context);
|
2020-07-19 04:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstArrayView<String> StaticRegister::get(const Context&)
|
|
|
|
{
|
|
|
|
if (m_content.empty())
|
|
|
|
return ConstArrayView<String>(String::ms_empty);
|
|
|
|
else
|
|
|
|
return ConstArrayView<String>(m_content);
|
|
|
|
}
|
|
|
|
|
|
|
|
const String& StaticRegister::get_main(const Context& context, size_t main_index)
|
|
|
|
{
|
|
|
|
auto content = get(context);
|
|
|
|
return content[std::min(main_index, content.size() - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryRegister::set(Context& context, ConstArrayView<String> values, bool restoring)
|
|
|
|
{
|
|
|
|
constexpr size_t size_limit = 100;
|
|
|
|
|
|
|
|
if (restoring)
|
|
|
|
return StaticRegister::set(context, values, true);
|
|
|
|
|
|
|
|
for (auto& entry : values)
|
|
|
|
{
|
|
|
|
m_content.erase(std::remove(m_content.begin(), m_content.end(), entry),
|
|
|
|
m_content.end());
|
|
|
|
m_content.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t current_size = m_content.size();
|
|
|
|
if (current_size > size_limit)
|
|
|
|
m_content.erase(m_content.begin(), m_content.begin() + (current_size - size_limit));
|
|
|
|
|
2020-07-21 12:27:42 +02:00
|
|
|
if (not m_disable_modified_hook)
|
|
|
|
context.hooks().run_hook(Hook::RegisterModified, m_name, context);
|
2020-07-19 04:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const String& HistoryRegister::get_main(const Context&, size_t)
|
|
|
|
{
|
|
|
|
return m_content.empty() ? String::ms_empty : m_content.back();
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:47:14 +01:00
|
|
|
static const HashMap<String, Codepoint> reg_names = {
|
|
|
|
{ "slash", '/' },
|
|
|
|
{ "dquote", '"' },
|
|
|
|
{ "pipe", '|' },
|
|
|
|
{ "caret", '^' },
|
|
|
|
{ "arobase", '@' },
|
|
|
|
{ "percent", '%' },
|
|
|
|
{ "dot", '.' },
|
|
|
|
{ "hash", '#' },
|
|
|
|
{ "underscore", '_' },
|
|
|
|
{ "colon", ':' }
|
|
|
|
};
|
|
|
|
|
2015-11-25 22:08:33 +01:00
|
|
|
Register& RegisterManager::operator[](StringView reg) const
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2014-06-06 01:48:18 +02:00
|
|
|
if (reg.length() == 1)
|
2015-03-10 20:33:46 +01:00
|
|
|
return (*this)[reg[0_byte]];
|
2014-06-06 01:48:18 +02:00
|
|
|
|
|
|
|
auto it = reg_names.find(reg);
|
|
|
|
if (it == reg_names.end())
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("no such register: '{}'", reg));
|
2015-09-16 20:57:57 +02:00
|
|
|
return (*this)[it->value];
|
2014-06-06 01:48:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:08:33 +01:00
|
|
|
Register& RegisterManager::operator[](Codepoint c) const
|
2014-06-06 01:48:18 +02:00
|
|
|
{
|
2015-11-11 01:21:20 +01:00
|
|
|
c = to_lower(c);
|
2015-11-25 22:08:33 +01:00
|
|
|
auto it = m_registers.find(c);
|
|
|
|
if (it == m_registers.end())
|
|
|
|
throw runtime_error(format("no such register: '{}'", c));
|
2015-07-14 14:48:39 +02:00
|
|
|
|
2017-03-07 01:30:54 +01:00
|
|
|
return *(it->value);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
|
2017-03-07 01:30:54 +01:00
|
|
|
void RegisterManager::add_register(Codepoint c, std::unique_ptr<Register> reg)
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2015-11-25 22:08:33 +01:00
|
|
|
auto& reg_ptr = m_registers[c];
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not reg_ptr);
|
2015-11-25 22:08:33 +01:00
|
|
|
reg_ptr = std::move(reg);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
|
2019-11-23 08:47:14 +01:00
|
|
|
CandidateList RegisterManager::complete_register_name(StringView prefix, ByteCount cursor_pos) const
|
|
|
|
{
|
|
|
|
return complete(prefix, cursor_pos, reg_names | transform([](auto& i) { return i.key; }) | gather<Vector<String>>());
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|