From 0244112af42f5a4e027705dd68b662fb2c795cce Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 12 Nov 2013 19:38:19 +0000 Subject: [PATCH] Add support for hidden commands, that will not show on completion --- README.asciidoc | 1 + src/command_manager.cc | 10 +++++++--- src/command_manager.hh | 22 ++++++++++++++++++---- src/commands.cc | 29 +++++++++++++++++------------ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index c1a3e47b..ada43caa 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -668,6 +668,7 @@ def can also takes some flags: completion candidate per line. * +-allow-override+: allow the new command to replace an exisiting one with the same name. + * +-hidden+: do not show the command in command name completions Using shell expansion permits to define complex commands or to access kakoune state: diff --git a/src/command_manager.cc b/src/command_manager.cc index 165a58f3..74468c06 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -18,17 +18,19 @@ bool CommandManager::command_defined(const String& command_name) const void CommandManager::register_command(String command_name, Command command, + CommandFlags flags, CommandCompleter completer) { - m_commands[command_name] = { std::move(command), std::move(completer) }; + m_commands[command_name] = { std::move(command), flags, std::move(completer) }; } void CommandManager::register_commands(memoryview command_names, Command command, + CommandFlags flags, CommandCompleter completer) { for (auto command_name : command_names) - m_commands[command_name] = { command, completer }; + m_commands[command_name] = { command, flags, completer }; } struct parse_error : runtime_error @@ -337,7 +339,9 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla for (auto& command : m_commands) { - if (prefix_match(command.first, prefix)) + if (command.second.flags & CommandFlags::Hidden) + continue; + if ( prefix_match(command.first, prefix)) result.candidates.push_back(command.first); } std::sort(result.candidates.begin(), result.candidates.end()); diff --git a/src/command_manager.hh b/src/command_manager.hh index 849ea3a7..92bbd7f2 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -21,6 +21,19 @@ using CommandCompleter = std::function; +enum class CommandFlags +{ + None = 0, + Hidden = 1, +}; +constexpr CommandFlags operator|(CommandFlags lhs, CommandFlags rhs) +{ + return (CommandFlags)((int)lhs | (int)rhs); +} +constexpr bool operator&(CommandFlags lhs, CommandFlags rhs) +{ + return (bool)((int)lhs & (int)rhs); +} class PerArgumentCommandCompleter { @@ -55,12 +68,12 @@ public: bool command_defined(const String& command_name) const; - void register_command(String command_name, - Command command, + void register_command(String command_name, Command command, + CommandFlags flags = CommandFlags::None, CommandCompleter completer = CommandCompleter()); - void register_commands(memoryview command_names, - Command command, + void register_commands(memoryview command_names, Command command, + CommandFlags flags = CommandFlags::None, CommandCompleter completer = CommandCompleter()); private: @@ -69,6 +82,7 @@ private: struct CommandDescriptor { Command command; + CommandFlags flags; CommandCompleter completer; }; std::unordered_map m_commands; diff --git a/src/commands.cc b/src/commands.cc index 4911337c..fb49fbf8 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -333,6 +333,7 @@ void define_command(CommandParameters params, Context& context) { "shell-params", false }, { "allow-override", false }, { "file-completion", false }, + { "hidden", false }, { "shell-completion", true } }, ParametersParser::Flags::None, 2, 2); @@ -344,6 +345,10 @@ void define_command(CommandParameters params, Context& context) not parser.has_option("allow-override")) throw runtime_error("command '" + cmd_name + "' already defined"); + CommandFlags flags = CommandFlags::None; + if (parser.has_option("hidden")) + flags = CommandFlags::Hidden; + String commands = parser[1]; Command cmd; if (parser.has_option("env-params")) @@ -397,7 +402,7 @@ void define_command(CommandParameters params, Context& context) return split(output, '\n'); }; } - CommandManager::instance().register_command(cmd_name, cmd, completer); + CommandManager::instance().register_command(cmd_name, cmd, flags, completer); } void echo_message(CommandParameters params, Context& context) @@ -809,9 +814,9 @@ void register_commands() [](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) { return complete_filename(prefix, context.options()["ignored_files"].get(), cursor_pos); } }); - cm.register_commands({ "e", "edit" }, edit, filename_completer); - cm.register_commands({ "e!", "edit!" }, edit, filename_completer); - cm.register_commands({ "w", "write" }, write_buffer, filename_completer); + cm.register_commands({ "e", "edit" }, edit, CommandFlags::None, filename_completer); + cm.register_commands({ "e!", "edit!" }, edit, CommandFlags::None, filename_completer); + cm.register_commands({ "w", "write" }, write_buffer, CommandFlags::None, filename_completer); cm.register_commands({ "wa", "writeall" }, write_all_buffers); cm.register_commands({ "q", "quit" }, quit); cm.register_commands({ "q!", "quit!" }, quit); @@ -822,19 +827,19 @@ void register_commands() [](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) { return BufferManager::instance().complete_buffername(prefix, cursor_pos); } }); - cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer); - cm.register_commands({ "db", "delbuf" }, delete_buffer, buffer_completer); - cm.register_commands({ "db!", "delbuf!" }, delete_buffer, buffer_completer); + cm.register_commands({ "b", "buffer" }, show_buffer, CommandFlags::None, buffer_completer); + cm.register_commands({ "db", "delbuf" }, delete_buffer, CommandFlags::None, buffer_completer); + cm.register_commands({ "db!", "delbuf!" }, delete_buffer, CommandFlags::None, buffer_completer); cm.register_commands({"nb", "namebuf"}, set_buffer_name); auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); }; - cm.register_commands({ "ah", "addhl" }, add_highlighter, group_add_completer(get_highlighters)); - cm.register_commands({ "rh", "rmhl" }, rm_highlighter, group_rm_completer(get_highlighters)); + cm.register_commands({ "ah", "addhl" }, add_highlighter, CommandFlags::None, group_add_completer(get_highlighters)); + cm.register_commands({ "rh", "rmhl" }, rm_highlighter, CommandFlags::None, group_rm_completer(get_highlighters)); cm.register_command("hook", add_hook); cm.register_command("rmhooks", rm_hooks); - cm.register_command("source", exec_commands_in_file, filename_completer); + cm.register_command("source", exec_commands_in_file, CommandFlags::None, filename_completer); cm.register_command("exec", exec_string); cm.register_command("eval", eval_string); @@ -849,7 +854,7 @@ void register_commands() cm.register_command("echo", echo_message); cm.register_command("debug", write_debug_message); - cm.register_command("set", set_option, + cm.register_command("set", set_option, CommandFlags::None, [](const Context& context, CompletionFlags, CommandParameters params, size_t token_to_complete, ByteCount pos_in_token) { if (token_to_complete == 0) @@ -873,7 +878,7 @@ void register_commands() cm.register_commands({"ca", "colalias"}, define_color_alias); cm.register_commands({"nc", "nameclient"}, set_client_name); - cm.register_command("cd", change_working_directory, filename_completer); + cm.register_command("cd", change_working_directory, CommandFlags::None, filename_completer); cm.register_command("map", map_key); } }