Add support for long names for registers

We can now access register / with the name slash, * with star,
and | with pipe

Fixes #23
This commit is contained in:
Maxime Coste 2014-06-06 00:48:18 +01:00
parent 2d5c730441
commit d33554a1cc
5 changed files with 24 additions and 10 deletions

View File

@ -313,9 +313,7 @@ String eval_token(const Token& token, Context& context,
return ShellManager::instance().eval(content, context, shell_params, return ShellManager::instance().eval(content, context, shell_params,
env_vars); env_vars);
case Token::Type::RegisterExpand: case Token::Type::RegisterExpand:
if (content.length() != 1) return RegisterManager::instance()[content].values(context)[0];
throw runtime_error("wrong register name: " + content);
return RegisterManager::instance()[content[0]].values(context)[0];
case Token::Type::OptionExpand: case Token::Type::OptionExpand:
return context.options()[content].get_as_string(); return context.options()[content].get_as_string();
case Token::Type::RawEval: case Token::Type::RawEval:

View File

@ -1269,9 +1269,7 @@ const CommandDesc set_register_cmd = {
CommandCompleter{}, CommandCompleter{},
[](const ParametersParser& parser, Context& context) [](const ParametersParser& parser, Context& context)
{ {
if (parser[0].length() != 1) RegisterManager::instance()[parser[0]] = memoryview<String>(parser[1]);
throw runtime_error("register names are single character");
RegisterManager::instance()[parser[0][0]] = memoryview<String>(parser[1]);
} }
}; };

View File

@ -106,7 +106,7 @@ void register_env_vars()
}, { }, {
"reg_.+", "reg_.+",
[](StringView name, const Context& context) -> String [](StringView name, const Context& context) -> String
{ return RegisterManager::instance()[name[4]].values(context)[0]; } { return RegisterManager::instance()[name.substr(4_byte)].values(context)[0]; }
}, { }, {
"client_env_.+", "client_env_.+",
[](StringView name, const Context& context) -> String [](StringView name, const Context& context) -> String

View File

@ -1,6 +1,7 @@
#include "register_manager.hh" #include "register_manager.hh"
#include "assert.hh" #include "assert.hh"
#include "id_map.hh"
#include "utils.hh" #include "utils.hh"
namespace Kakoune namespace Kakoune
@ -55,9 +56,25 @@ private:
RegisterRetriever m_function; RegisterRetriever m_function;
}; };
Register& RegisterManager::operator[](char reg) Register& RegisterManager::operator[](StringView reg)
{ {
auto& reg_ptr = m_registers[reg]; if (reg.length() == 1)
return (*this)[reg[0]];
static const id_map<Codepoint> reg_names = {
{ "slash", '/' },
{ "dquote", '"' },
{ "pipe", '|' }
};
auto it = reg_names.find(reg);
if (it == reg_names.end())
throw runtime_error("no such register: " + reg);
return (*this)[it->second];
}
Register& RegisterManager::operator[](Codepoint c)
{
auto& reg_ptr = m_registers[c];
if (not reg_ptr) if (not reg_ptr)
reg_ptr.reset(new StaticRegister()); reg_ptr.reset(new StaticRegister());
return *reg_ptr; return *reg_ptr;

View File

@ -16,7 +16,8 @@ using RegisterRetriever = std::function<std::vector<String> (const Context&)>;
class RegisterManager : public Singleton<RegisterManager> class RegisterManager : public Singleton<RegisterManager>
{ {
public: public:
Register& operator[](char reg); Register& operator[](StringView reg);
Register& operator[](Codepoint c);
void register_dynamic_register(char reg, RegisterRetriever function); void register_dynamic_register(char reg, RegisterRetriever function);
protected: protected: