From 2d5c7304411cbac923f132dfee34cf7ffc155d5c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 6 Jun 2014 00:17:15 +0100 Subject: [PATCH] Replace CommandManager::register_command*s* with register_alias --- src/command_manager.cc | 18 ++++-------------- src/command_manager.hh | 7 +------ src/commands.cc | 25 +++++++++++++++---------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 82fcea46..9ae3cbc5 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -30,21 +30,11 @@ void CommandManager::register_command(String command_name, std::move(completer) }; } -void CommandManager::register_commands(memoryview command_names, - Command command, - String docstring, - ParameterDesc param_desc, - CommandFlags flags, - CommandCompleter completer) +void CommandManager::register_alias(String alias, String command) { - kak_assert(not command_names.empty()); - m_commands[command_names[0]] = { std::move(command), - std::move(docstring), - std::move(param_desc), - flags, - completer }; - for (size_t i = 1; i < command_names.size(); ++i) - m_aliases[command_names[i]] = command_names[0]; + kak_assert(not alias.empty()); + kak_assert(command_defined(command)); + m_aliases[alias] = std::move(command); } struct parse_error : runtime_error diff --git a/src/command_manager.hh b/src/command_manager.hh index ea577d00..8d0cc957 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -79,12 +79,7 @@ public: ParameterDesc param_desc, CommandFlags flags = CommandFlags::None, CommandCompleter completer = CommandCompleter()); - - void register_commands(memoryview command_names, Command command, - String docstring, - ParameterDesc param_desc, - CommandFlags flags = CommandFlags::None, - CommandCompleter completer = CommandCompleter()); + void register_alias(String alias, String command); private: void execute_single_command(CommandParameters params, diff --git a/src/commands.cc b/src/commands.cc index 25c206f0..988fbeb1 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -567,6 +567,14 @@ void define_command(const ParametersParser& parser, Context& context) if (parser.has_option("docstring")) docstring = parser.option_value("docstring"); + String alias; + if (parser.has_option("alias")) + { + alias = parser.option_value("alias"); + if (alias.empty()) + throw runtime_error("alias should not be an empty string"); + } + String commands = parser[1]; Command cmd; ParameterDesc desc; @@ -648,13 +656,11 @@ void define_command(const ParametersParser& parser, Context& context) return Completions{ 0_byte, params[token_to_complete].length(), split(output, '\n') }; }; } - if (parser.has_option("alias")) - CommandManager::instance().register_commands( - { cmd_name, parser.option_value("alias") }, - cmd, std::move(docstring), desc, flags, completer); - else - CommandManager::instance().register_command( - cmd_name, cmd, std::move(docstring), desc, flags, completer); + + auto& cm = CommandManager::instance(); + cm.register_command(cmd_name, cmd, std::move(docstring), desc, flags, completer); + if (not alias.empty()) + cm.register_alias(std::move(alias), cmd_name); } const CommandDesc define_command_cmd = { @@ -1316,10 +1322,9 @@ void exec_keys(const KeyList& keys, Context& context) static void register_command(CommandManager& cm, const CommandDesc& c) { + cm.register_command(c.name, c.func, c.docstring, c.params, c.flags, c.completer); if (c.alias) - cm.register_commands({ c.name, c.alias }, c.func, c.docstring, c.params, c.flags, c.completer); - else - cm.register_command(c.name, c.func, c.docstring, c.params, c.flags, c.completer); + cm.register_alias(c.alias, c.name); } void register_commands()