Added shell command completion support to define-command and prompt

This commit also introduces a regression in that I decided that the best way to
avoid overly long and confusing names was to rename the current shell-*
switches to script-*, and have the shell command completion be
shell-completion.

renamed script-{completion,candidates} to shell-script-*

Updated docs with new switch names

Added -shell-completion switch to x11-repl and kitty-repl
This commit is contained in:
Justin Frank 2018-09-26 14:42:01 -07:00
parent 551021d6e5
commit ab6bc41358
11 changed files with 43 additions and 29 deletions

View File

@ -291,6 +291,9 @@ New commands can be defined using the *define-command* command:
try command completion on any parameter passed to this command try command completion on any parameter passed to this command
*-shell-completion*::: *-shell-completion*:::
try shell command completion on any parameter passed to this command
*-shell-script-completion*:::
following string is a shell command which takes parameters as following string is a shell command which takes parameters as
positional params and output one completion candidate per line. positional params and output one completion candidate per line.
The provided shell command will run after each keypress The provided shell command will run after each keypress
@ -305,7 +308,7 @@ New commands can be defined using the *define-command* command:
Position of the cursor inside the token being completed, in bytes Position of the cursor inside the token being completed, in bytes
from token start. from token start.
*-shell-candidates*::: *-shell-script-candidates*:::
following string is a shell command which takes parameters as following string is a shell command which takes parameters as
positional params and output one completion candidate per line. positional params and output one completion candidate per line.
The provided shell command will run once at the beginning of each The provided shell command will run once at the beginning of each

View File

@ -7,7 +7,7 @@ declare-option -docstring "list of paths to tag files to parse when looking up a
str-list ctagsfiles 'tags' str-list ctagsfiles 'tags'
define-command -params ..1 \ define-command -params ..1 \
-shell-candidates %{ -shell-script-candidates %{
realpath() { ( path=$(readlink "$1"); cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) } realpath() { ( path=$(readlink "$1"); cd "$(dirname "$1")"; printf "%s/%s\n" "$(pwd -P)" "$(basename "$1")" ) }
eval "set -- $kak_opt_ctagsfiles" eval "set -- $kak_opt_ctagsfiles"
for candidate in "$@"; do for candidate in "$@"; do

View File

@ -131,7 +131,7 @@ define-command -params 1 -hidden doc-render %{
} }
define-command -params 1..2 \ define-command -params 1..2 \
-shell-candidates %{ -shell-script-candidates %{
if [ "$kak_token_to_complete" -eq 0 ]; then if [ "$kak_token_to_complete" -eq 0 ]; then
find "${kak_runtime}/doc/" -type f -name "*.asciidoc" | sed 's,.*/,,; s/\.[^/]*$//' find "${kak_runtime}/doc/" -type f -name "*.asciidoc" | sed 's,.*/,,; s/\.[^/]*$//'
elif [ "$kak_token_to_complete" -eq 1 ]; then elif [ "$kak_token_to_complete" -eq 1 ]; then

View File

@ -20,10 +20,10 @@ add-highlighter shared/kakrc/shell1 region -recurse '\{' '(^|\h)\K%?%sh\{' '\}'
add-highlighter shared/kakrc/shell2 region -recurse '\(' '(^|\h)\K%?%sh\(' '\)' ref sh add-highlighter shared/kakrc/shell2 region -recurse '\(' '(^|\h)\K%?%sh\(' '\)' ref sh
add-highlighter shared/kakrc/shell3 region -recurse '\[' '(^|\h)\K%?%sh\[' '\]' ref sh add-highlighter shared/kakrc/shell3 region -recurse '\[' '(^|\h)\K%?%sh\[' '\]' ref sh
add-highlighter shared/kakrc/shell4 region -recurse '<' '(^|\h)\K%?%sh<' '>' ref sh add-highlighter shared/kakrc/shell4 region -recurse '<' '(^|\h)\K%?%sh<' '>' ref sh
add-highlighter shared/kakrc/shell5 region -recurse '\{' '(^|\h)\K-shell-(completion|candidates)\h+%\{' '\}' ref sh add-highlighter shared/kakrc/shell5 region -recurse '\{' '(^|\h)\K-script-(completion|candidates)\h+%\{' '\}' ref sh
add-highlighter shared/kakrc/shell6 region -recurse '\(' '(^|\h)\K-shell-(completion|candidates)\h+%\(' '\)' ref sh add-highlighter shared/kakrc/shell6 region -recurse '\(' '(^|\h)\K-script-(completion|candidates)\h+%\(' '\)' ref sh
add-highlighter shared/kakrc/shell7 region -recurse '\[' '(^|\h)\K-shell-(completion|candidates)\h+%\[' '\]' ref sh add-highlighter shared/kakrc/shell7 region -recurse '\[' '(^|\h)\K-script-(completion|candidates)\h+%\[' '\]' ref sh
add-highlighter shared/kakrc/shell8 region -recurse '<' '(^|\h)\K-shell-(completion|candidates)\h+%<' '>' ref sh add-highlighter shared/kakrc/shell8 region -recurse '<' '(^|\h)\K-script-(completion|candidates)\h+%<' '>' ref sh
evaluate-commands %sh{ evaluate-commands %sh{
# Grammar # Grammar

View File

@ -51,8 +51,8 @@ define-command -hidden -params 2..3 man-impl %{ evaluate-commands %sh{
} } } }
define-command -params ..1 \ define-command -params ..1 \
-shell-candidates %{ -shell-script-candidates %{
find /usr/share/man/ -name '*.[1-8]*' | sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),' find /usr/share/man/ -name '*.[1-8]*' | sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),'
} \ } \
-docstring %{man [<page>]: manpage viewer wrapper -docstring %{man [<page>]: manpage viewer wrapper
If no argument is passed to the command, the selection will be used as page If no argument is passed to the command, the selection will be used as page

View File

@ -27,7 +27,7 @@ define-command -params 1.. \
-docstring %sh{printf 'git [<arguments>]: git wrapping helper -docstring %sh{printf 'git [<arguments>]: git wrapping helper
All the optional arguments are forwarded to the git utility All the optional arguments are forwarded to the git utility
Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-blame\n hide-diff\n log\n show\n show-diff\n status\n update-diff'} \ Available commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-blame\n hide-diff\n log\n show\n show-diff\n status\n update-diff'} \
-shell-candidates %{ -shell-script-candidates %{
if [ $kak_token_to_complete -eq 0 ]; then if [ $kak_token_to_complete -eq 0 ]; then
printf "add\nrm\nblame\ncommit\ncheckout\ndiff\nhide-blame\nhide-diff\nlog\nshow\nshow-diff\nstatus\nupdate-diff\n" printf "add\nrm\nblame\ncommit\ncheckout\ndiff\nhide-blame\nhide-diff\nlog\nshow\nshow-diff\nstatus\nupdate-diff\n"
else else

View File

@ -43,9 +43,7 @@ If no client is passed then the current one is used} \
define-command -docstring %{kitty-repl [<arguments>]: create a new window for repl interaction define-command -docstring %{kitty-repl [<arguments>]: create a new window for repl interaction
All optional parameters are forwarded to the new window} \ All optional parameters are forwarded to the new window} \
-params .. \ -params .. \
-shell-candidates %{ -shell-completion \
find $(echo $PATH | tr ':' ' ') -mindepth 1 -maxdepth 1 -executable -printf "%f\n"
} \
kitty-repl %{ evaluate-commands %sh{ kitty-repl %{ evaluate-commands %sh{
if [ $# -eq 0 ]; then cmd="${SHELL:-/bin/sh}"; else cmd="$*"; fi if [ $# -eq 0 ]; then cmd="${SHELL:-/bin/sh}"; else cmd="$*"; fi
kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type --title kak_repl_window $cmd < /dev/null > /dev/null 2>&1 & kitty @ new-window --no-response --window-type $kak_opt_kitty_window_type --title kak_repl_window $cmd < /dev/null > /dev/null 2>&1 &

View File

@ -2,7 +2,7 @@
define-command -docstring %{x11-repl [<arguments>]: create a new window for repl interaction define-command -docstring %{x11-repl [<arguments>]: create a new window for repl interaction
All optional parameters are forwarded to the new window} \ All optional parameters are forwarded to the new window} \
-params .. \ -params .. \
-command-completion \ -shell-completion \
x11-repl %{ evaluate-commands %sh{ x11-repl %{ evaluate-commands %sh{
if [ -z "${kak_opt_termcmd}" ]; then if [ -z "${kak_opt_termcmd}" ]; then
echo "echo -markup '{Error}termcmd option is not set'" echo "echo -markup '{Error}termcmd option is not set'"

View File

@ -1,5 +1,5 @@
def -params 1 -docstring "colorscheme <name>: enable named colorscheme" \ def -params 1 -docstring "colorscheme <name>: enable named colorscheme" \
-shell-candidates %{ -shell-script-candidates %{
find -L "${kak_runtime}/colors" "${kak_config}/colors" -type f -name '*\.kak' \ find -L "${kak_runtime}/colors" "${kak_config}/colors" -type f -name '*\.kak' \
| while read -r filename; do | while read -r filename; do
basename="${filename##*/}" basename="${filename##*/}"

View File

@ -997,7 +997,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel
return complete_buffer_name(context, flags, params[token_to_complete], pos_in_token); return complete_buffer_name(context, flags, params[token_to_complete], pos_in_token);
}; };
} }
else if (auto shell_cmd_opt = parser.get_switch("shell-completion")) else if (auto shell_cmd_opt = parser.get_switch("shell-script-completion"))
{ {
String shell_cmd = shell_cmd_opt->str(); String shell_cmd = shell_cmd_opt->str();
completer = [=](const Context& context, CompletionFlags flags, completer = [=](const Context& context, CompletionFlags flags,
@ -1022,7 +1022,7 @@ void define_command(const ParametersParser& parser, Context& context, const Shel
return Completions{ 0_byte, pos_in_token, std::move(candidates) }; return Completions{ 0_byte, pos_in_token, std::move(candidates) };
}; };
} }
else if (auto shell_cmd_opt = parser.get_switch("shell-candidates")) else if (auto shell_cmd_opt = parser.get_switch("shell-script-candidates"))
{ {
String shell_cmd = shell_cmd_opt->str(); String shell_cmd = shell_cmd_opt->str();
Vector<std::pair<String, UsedLetters>, MemoryDomain::Completion> candidates; Vector<std::pair<String, UsedLetters>, MemoryDomain::Completion> candidates;
@ -1082,6 +1082,15 @@ void define_command(const ParametersParser& parser, Context& context, const Shel
context, flags, params, token_to_complete, pos_in_token); context, flags, params, token_to_complete, pos_in_token);
}; };
} }
else if (parser.get_switch("shell-completion"))
{
completer = [](const Context& context, CompletionFlags flags,
CommandParameters params,
size_t token_to_complete, ByteCount pos_in_token)
{
return shell_complete(context, flags, params[token_to_complete], pos_in_token);
};
}
auto docstring = trim_whitespaces(parser.get_switch("docstring").value_or(StringView{})); auto docstring = trim_whitespaces(parser.get_switch("docstring").value_or(StringView{}));
@ -1093,17 +1102,18 @@ const CommandDesc define_command_cmd = {
"def", "def",
"define-command [<switches>] <name> <cmds>: define a command <name> executing <cmds>", "define-command [<switches>] <name> <cmds>: define a command <name> executing <cmds>",
ParameterDesc{ ParameterDesc{
{ { "params", { true, "take parameters, accessible to each shell escape as $0..$N\n" { { "params", { true, "take parameters, accessible to each shell escape as $0..$N\n"
"parameter should take the form <count> or <min>..<max> (both omittable)" } }, "parameter should take the form <count> or <min>..<max> (both omittable)" } },
{ "override", { false, "allow overriding an existing command" } }, { "override", { false, "allow overriding an existing command" } },
{ "hidden", { false, "do not display the command in completion candidates" } }, { "hidden", { false, "do not display the command in completion candidates" } },
{ "docstring", { true, "define the documentation string for command" } }, { "docstring", { true, "define the documentation string for command" } },
{ "file-completion", { false, "complete parameters using filename completion" } }, { "file-completion", { false, "complete parameters using filename completion" } },
{ "client-completion", { false, "complete parameters using client name completion" } }, { "client-completion", { false, "complete parameters using client name completion" } },
{ "buffer-completion", { false, "complete parameters using buffer name completion" } }, { "buffer-completion", { false, "complete parameters using buffer name completion" } },
{ "command-completion", { false, "complete parameters using kakoune command completion" } }, { "command-completion", { false, "complete parameters using kakoune command completion" } },
{ "shell-completion", { true, "complete parameters using the given shell-script" } }, { "shell-completion", { false, "complete parameters using shell command completion" } },
{ "shell-candidates", { true, "get the parameter candidates using the given shell-script" } } }, { "shell-script-completion", { true, "complete parameters using the given shell-script" } },
{ "shell-script-candidates", { true, "get the parameter candidates using the given shell-script" } } },
ParameterDesc::Flags::None, ParameterDesc::Flags::None,
2, 2 2, 2
}, },
@ -1828,6 +1838,7 @@ const CommandDesc prompt_cmd = {
{ "client-completion", { false, "use client completion for prompt" } }, { "client-completion", { false, "use client completion for prompt" } },
{ "buffer-completion", { false, "use buffer completion for prompt" } }, { "buffer-completion", { false, "use buffer completion for prompt" } },
{ "command-completion", { false, "use command completion for prompt" } }, { "command-completion", { false, "use command completion for prompt" } },
{ "shell-completion", { false, "use shell command completion for prompt" } },
{ "on-change", { true, "command to execute whenever the prompt changes" } }, { "on-change", { true, "command to execute whenever the prompt changes" } },
{ "on-abort", { true, "command to execute whenever the prompt is canceled" } } }, { "on-abort", { true, "command to execute whenever the prompt is canceled" } } },
ParameterDesc::Flags::None, 2, 2 ParameterDesc::Flags::None, 2, 2
@ -1863,6 +1874,8 @@ const CommandDesc prompt_cmd = {
return CommandManager::instance().complete( return CommandManager::instance().complete(
context, flags, prefix, cursor_pos); context, flags, prefix, cursor_pos);
}; };
else if (parser.get_switch("shell-completion"))
completer = shell_complete;
const auto flags = parser.get_switch("password") ? const auto flags = parser.get_switch("password") ?
PromptFlags::Password : PromptFlags::None; PromptFlags::Password : PromptFlags::None;

View File

@ -1 +1 @@
def my-command -params 0..1 -shell-candidates %{ printf "aaa\nbbb\nccc" } %{ exec i %arg{1} <esc> } def my-command -params 0..1 -shell-script-candidates %{ printf "aaa\nbbb\nccc" } %{ exec i %arg{1} <esc> }