2012-06-29 18:37:17 +02:00
|
|
|
#include "register_manager.hh"
|
|
|
|
|
|
|
|
#include "assert.hh"
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "exception.hh"
|
2014-12-23 14:54:09 +01:00
|
|
|
#include "id_map.hh"
|
2012-06-29 18:37:17 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
// static value register, which can be modified
|
|
|
|
// using operator=, so should be user modifiable
|
|
|
|
class StaticRegister : public Register
|
|
|
|
{
|
|
|
|
public:
|
2015-03-09 14:48:41 +01:00
|
|
|
Register& operator=(ConstArrayView<String> values) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2015-01-14 20:16:32 +01:00
|
|
|
m_content = Vector<String, MemoryDomain::Registers>(values.begin(), values.end());
|
2012-06-29 18:37:17 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
ConstArrayView<String> values(const Context&) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2012-08-05 20:13:41 +02:00
|
|
|
if (m_content.empty())
|
2015-03-09 14:48:41 +01:00
|
|
|
return ConstArrayView<String>(ms_empty);
|
2012-06-29 18:37:17 +02:00
|
|
|
else
|
2015-03-09 14:48:41 +01:00
|
|
|
return ConstArrayView<String>(m_content);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
protected:
|
2015-01-14 20:16:32 +01:00
|
|
|
Vector<String, MemoryDomain::Registers> m_content;
|
2012-06-29 18:37:17 +02:00
|
|
|
|
|
|
|
static const String ms_empty;
|
|
|
|
};
|
|
|
|
|
|
|
|
const String StaticRegister::ms_empty;
|
|
|
|
|
|
|
|
// Dynamic value register, use it's RegisterRetriever
|
|
|
|
// to get it's value when needed.
|
|
|
|
class DynamicRegister : public StaticRegister
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DynamicRegister(RegisterRetriever function)
|
|
|
|
: m_function(std::move(function)) {}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
Register& operator=(ConstArrayView<String> values) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
|
|
|
throw runtime_error("this register is not assignable");
|
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
ConstArrayView<String> values(const Context& context) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2012-08-05 20:13:41 +02:00
|
|
|
m_content = m_function(context);
|
|
|
|
return StaticRegister::values(context);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-14 20:16:32 +01:00
|
|
|
RegisterRetriever m_function;
|
2012-06-29 18:37:17 +02:00
|
|
|
};
|
|
|
|
|
2014-06-06 01:48:18 +02:00
|
|
|
Register& RegisterManager::operator[](StringView reg)
|
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
|
|
|
|
2014-12-23 23:51:00 +01:00
|
|
|
static const IdMap<Codepoint> reg_names = {
|
2014-06-06 01:48:18 +02:00
|
|
|
{ "slash", '/' },
|
|
|
|
{ "dquote", '"' },
|
2015-08-19 22:16:53 +02:00
|
|
|
{ "pipe", '|' },
|
|
|
|
{ "caret", '^' },
|
|
|
|
{ "arobase", '@' }
|
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));
|
2014-06-06 01:48:18 +02:00
|
|
|
return (*this)[it->second];
|
|
|
|
}
|
|
|
|
|
|
|
|
Register& RegisterManager::operator[](Codepoint c)
|
|
|
|
{
|
2015-07-14 14:48:39 +02:00
|
|
|
c = tolower(c);
|
|
|
|
if (c < 32 or c > 127)
|
|
|
|
throw runtime_error(format("invalid register name: '{}'", c));
|
|
|
|
|
2014-06-06 01:48:18 +02:00
|
|
|
auto& reg_ptr = m_registers[c];
|
2012-06-29 18:37:17 +02:00
|
|
|
if (not reg_ptr)
|
|
|
|
reg_ptr.reset(new StaticRegister());
|
|
|
|
return *reg_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterManager::register_dynamic_register(char reg, RegisterRetriever function)
|
|
|
|
{
|
|
|
|
auto& reg_ptr = m_registers[reg];
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not reg_ptr);
|
2012-06-29 18:37:17 +02:00
|
|
|
reg_ptr.reset(new DynamicRegister(std::move(function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|