CommandManager: add flags support and IgnoreSemiColons flag

commands are now registred with flags, and the IgnoreSemiColons
flag permit to specify a command which needs all the parameters
on the line, bypassing the command sequence operator ';'.

the hook command is tagged as such.
This commit is contained in:
Maxime Coste 2012-01-15 03:02:08 +00:00
parent be5cf92367
commit f259af9326
3 changed files with 27 additions and 5 deletions

View File

@ -9,16 +9,18 @@ namespace Kakoune
{ {
void CommandManager::register_command(const std::string& command_name, Command command, void CommandManager::register_command(const std::string& command_name, Command command,
unsigned flags,
const CommandCompleter& completer) const CommandCompleter& completer)
{ {
m_commands[command_name] = CommandAndCompleter { command, completer }; m_commands[command_name] = CommandDescriptor { command, flags, completer };
} }
void CommandManager::register_command(const std::vector<std::string>& command_names, Command command, void CommandManager::register_command(const std::vector<std::string>& command_names, Command command,
unsigned flags,
const CommandCompleter& completer) const CommandCompleter& completer)
{ {
for (auto command_name : command_names) for (auto command_name : command_names)
register_command(command_name, command, completer); register_command(command_name, command, flags, completer);
} }
typedef std::vector<std::pair<size_t, size_t>> TokenList; typedef std::vector<std::pair<size_t, size_t>> TokenList;
@ -97,6 +99,9 @@ void CommandManager::execute(const CommandParameters& params,
if (command_it == m_commands.end()) if (command_it == m_commands.end())
throw command_not_found(*begin); throw command_not_found(*begin);
if (command_it->second.flags & IgnoreSemiColons)
end = params.end();
command_it->second.command(CommandParameters(begin + 1, end), context); command_it->second.command(CommandParameters(begin + 1, end), context);
} }

View File

@ -53,6 +53,12 @@ private:
class CommandManager : public Singleton<CommandManager> class CommandManager : public Singleton<CommandManager>
{ {
public: public:
enum Flags
{
None = 0,
IgnoreSemiColons = 1,
};
void execute(const std::string& command_line, const Context& context); void execute(const std::string& command_line, const Context& context);
void execute(const CommandParameters& params, const Context& context); void execute(const CommandParameters& params, const Context& context);
@ -60,19 +66,22 @@ public:
void register_command(const std::string& command_name, void register_command(const std::string& command_name,
Command command, Command command,
unsigned flags = None,
const CommandCompleter& completer = CommandCompleter()); const CommandCompleter& completer = CommandCompleter());
void register_command(const std::vector<std::string>& command_names, void register_command(const std::vector<std::string>& command_names,
Command command, Command command,
unsigned flags = None,
const CommandCompleter& completer = CommandCompleter()); const CommandCompleter& completer = CommandCompleter());
private: private:
struct CommandAndCompleter struct CommandDescriptor
{ {
Command command; Command command;
unsigned flags;
CommandCompleter completer; CommandCompleter completer;
}; };
std::unordered_map<std::string, CommandAndCompleter> m_commands; std::unordered_map<std::string, CommandDescriptor> m_commands;
}; };
} }

View File

@ -895,38 +895,46 @@ int main(int argc, char* argv[])
HooksManager hooks_manager; HooksManager hooks_manager;
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit, command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit,
CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
command_manager.register_command(std::vector<std::string>{ "q", "quit" }, quit<false>); command_manager.register_command(std::vector<std::string>{ "q", "quit" }, quit<false>);
command_manager.register_command(std::vector<std::string>{ "q!", "quit!" }, quit<true>); command_manager.register_command(std::vector<std::string>{ "q!", "quit!" }, quit<true>);
command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer, command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer,
CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
command_manager.register_command(std::vector<std::string>{ "wq" }, write_and_quit<false>); command_manager.register_command(std::vector<std::string>{ "wq" }, write_and_quit<false>);
command_manager.register_command(std::vector<std::string>{ "wq!" }, write_and_quit<true>); command_manager.register_command(std::vector<std::string>{ "wq!" }, write_and_quit<true>);
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer, command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer,
CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2)
}); });
command_manager.register_command(std::vector<std::string>{ "ah", "addhl" }, add_highlighter, command_manager.register_command(std::vector<std::string>{ "ah", "addhl" }, add_highlighter,
CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2)
}); });
command_manager.register_command(std::vector<std::string>{ "rh", "rmhl" }, rm_highlighter, command_manager.register_command(std::vector<std::string>{ "rh", "rmhl" }, rm_highlighter,
CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window->complete_highlighterid(prefix, cursor_pos); } { return main_context.window->complete_highlighterid(prefix, cursor_pos); }
}); });
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter, command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter,
CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2)
}); });
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter, command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter,
CommandManager::None,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window->complete_filterid(prefix, cursor_pos); } { return main_context.window->complete_filterid(prefix, cursor_pos); }
}); });
command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook); command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook, CommandManager::IgnoreSemiColons);
command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file, command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file,
CommandManager::None,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
command_manager.register_command(std::vector<std::string>{ "exec" }, exec_string); command_manager.register_command(std::vector<std::string>{ "exec" }, exec_string);