summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-05-29 05:22:18 +0000
committerMaxime Coste <frrrwww@gmail.com>2012-05-29 05:22:18 +0000
commitd5995424734220e5717d3ffd25103b3befeeeccd (patch)
tree86c84f214c7839a43e060e57b9550d71527144cd /src
parent62202a46c113b06cccf9196804f88c923eae8cc3 (diff)
Support -shell-completion option in the def command
-shell-completion takes some shell code as parameter which should returns a newline separated list of completion candidates.
Diffstat (limited to 'src')
-rw-r--r--src/commands.cc31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/commands.cc b/src/commands.cc
index 75646c0b..9eb24749 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -13,6 +13,7 @@
#include "filter_registry.hh"
#include "register_manager.hh"
#include "completion.hh"
+#include "shell_manager.hh"
namespace Kakoune
@@ -441,7 +442,8 @@ void define_command(const CommandParameters& params, const Context& context)
{
ParametersParser parser(params,
{ { "env-params", false },
- { "append-params", false } });
+ { "append-params", false },
+ { "shell-completion", true } });
if (parser.positional_count() < 2)
throw wrong_argument_count();
@@ -482,7 +484,32 @@ void define_command(const CommandParameters& params, const Context& context)
CommandManager::instance().execute(cmd_params, context);
};
}
- CommandManager::instance().register_command(cmd_name, cmd);
+
+ if (parser.has_option("shell-completion"))
+ {
+ String shell_cmd = parser.option_value("shell-completion");
+ auto completer = [=](const CommandParameters& params,
+ size_t token_to_complete, size_t pos_in_token)
+ {
+ std::unordered_map<String, String> vars;
+ char param_name[] = "param0";
+ for (size_t i = 0; i < params.size(); ++i)
+ {
+ param_name[sizeof(param_name) - 2] = '0' + i;
+ vars[param_name] = params[i].c_str();
+ }
+ vars["token_to_complete"] = int_to_str(token_to_complete);
+ vars["pos_in_token"] = int_to_str(pos_in_token);
+
+ String output = ShellManager::instance().eval(shell_cmd, context, vars);
+ return split(output, '\n');
+ };
+ CommandManager::instance().register_command(cmd_name, cmd,
+ CommandManager::None,
+ completer);
+ }
+ else
+ CommandManager::instance().register_command(cmd_name, cmd);
}
void echo_message(const CommandParameters& params, const Context& context)