add -file-completion option to def command and document in README

This commit is contained in:
Maxime Coste 2012-09-12 14:21:42 +02:00
parent f89636a182
commit 7c039585a4
2 changed files with 18 additions and 6 deletions

View File

@ -291,6 +291,8 @@ def can also takes some flags:
kak_paramN with N the parameter number kak_paramN with N the parameter number
* *-shell-params*: pass parameters given to commands as positional parameters * *-shell-params*: pass parameters given to commands as positional parameters
to any shell expansions used in the command. to any shell expansions used in the command.
* *-file-completion*: try file completion on any parameter passed
to this command
* *-shell-completion*: following string is a shell command which takes * *-shell-completion*: following string is a shell command which takes
parameters as positional params and output one parameters as positional params and output one
completion candidate per line. completion candidate per line.

View File

@ -534,6 +534,7 @@ void define_command(const CommandParameters& params, Context& context)
{ { "env-params", false }, { { "env-params", false },
{ "shell-params", false }, { "shell-params", false },
{ "allow-override", false }, { "allow-override", false },
{ "file-completion", false },
{ "shell-completion", true } }); { "shell-completion", true } });
if (parser.positional_count() != 2) if (parser.positional_count() != 2)
@ -570,11 +571,22 @@ void define_command(const CommandParameters& params, Context& context)
}; };
} }
if (parser.has_option("shell-completion")) CommandCompleter completer;
if (parser.has_option("file-completion"))
{
completer = [](const Context& context, const CommandParameters& params,
size_t token_to_complete, CharCount pos_in_token)
{
const String& prefix = token_to_complete < params.size() ?
params[token_to_complete] : String();
return complete_filename(context, prefix, pos_in_token);
};
}
else if (parser.has_option("shell-completion"))
{ {
String shell_cmd = parser.option_value("shell-completion"); String shell_cmd = parser.option_value("shell-completion");
auto completer = [=](const Context& context, const CommandParameters& params, completer = [=](const Context& context, const CommandParameters& params,
size_t token_to_complete, CharCount pos_in_token) size_t token_to_complete, CharCount pos_in_token)
{ {
EnvVarMap vars = { EnvVarMap vars = {
{"token_to_complete", int_to_str(token_to_complete) }, {"token_to_complete", int_to_str(token_to_complete) },
@ -583,10 +595,8 @@ void define_command(const CommandParameters& params, Context& context)
String output = ShellManager::instance().eval(shell_cmd, context, params, vars); String output = ShellManager::instance().eval(shell_cmd, context, params, vars);
return split(output, '\n'); return split(output, '\n');
}; };
CommandManager::instance().register_command(cmd_name, cmd, completer);
} }
else CommandManager::instance().register_command(cmd_name, cmd, completer);
CommandManager::instance().register_command(cmd_name, cmd);
} }
void echo_message(const CommandParameters& params, Context& context) void echo_message(const CommandParameters& params, Context& context)