From 550a95a3d7c74a7090000d07a01eea0974454a86 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 1 Dec 2015 14:00:55 +0000 Subject: [PATCH] Change def -shell-params to def -params and take the counts as parameters --- rc/clang.kak | 2 +- rc/ctags.kak | 2 +- rc/git-tools.kak | 2 +- rc/grep.kak | 2 +- rc/make.kak | 2 +- rc/man.kak | 4 ++-- rc/tmux.kak | 10 +++++----- rc/x11.kak | 4 ++-- share/kak/kakrc | 4 ++-- src/commands.cc | 35 +++++++++++++++++++++++++---------- 10 files changed, 41 insertions(+), 26 deletions(-) diff --git a/rc/clang.kak b/rc/clang.kak index de677dbb..ca9a90bc 100644 --- a/rc/clang.kak +++ b/rc/clang.kak @@ -5,7 +5,7 @@ decl -hidden str-list clang_completions decl -hidden line-flag-list clang_flags decl -hidden str clang_errors -def -shell-params clang-parse -docstring "Parse the contents of the current buffer with clang" %{ +def clang-parse -docstring "Parse the contents of the current buffer with clang" %{ %sh{ dir=$(mktemp -d -t kak-clang.XXXXXXXX) mkfifo ${dir}/fifo diff --git a/rc/ctags.kak b/rc/ctags.kak index db2de2e9..66617750 100644 --- a/rc/ctags.kak +++ b/rc/ctags.kak @@ -5,7 +5,7 @@ decl str-list ctagsfiles 'tags' -def -shell-params \ +def -params 0..1 \ -shell-completion ' ( for tags in $(echo "${kak_opt_ctagsfiles}" | tr \':\' \'\n\'); do readtags -t "${tags}" -p "$1" diff --git a/rc/git-tools.kak b/rc/git-tools.kak index 02f85ab2..f81339b6 100644 --- a/rc/git-tools.kak +++ b/rc/git-tools.kak @@ -25,7 +25,7 @@ decl line-flag-list git_diff_flags face GitBlame default,magenta -def -shell-params \ +def -params 1.. \ -docstring %sh{printf "%%{Git wrapping helper\navailable commands:\n add\n rm\n blame\n commit\n checkout\n diff\n hide-blame\n log\n show\n show-diff\n status\n update-diff}"} \ -shell-completion %{ shift $(expr ${kak_token_to_complete}) diff --git a/rc/grep.kak b/rc/grep.kak index 36a497e3..1bc5ef95 100644 --- a/rc/grep.kak +++ b/rc/grep.kak @@ -2,7 +2,7 @@ decl str grepcmd 'grep -RHn' decl str toolsclient decl -hidden int _grep_current_line 0 -def -shell-params -file-completion \ +def -params .. -file-completion \ grep -docstring "Grep utility wrapper" %{ %sh{ output=$(mktemp -d -t kak-grep.XXXXXXXX)/fifo mkfifo ${output} diff --git a/rc/make.kak b/rc/make.kak index c4122326..8f3a8fb5 100644 --- a/rc/make.kak +++ b/rc/make.kak @@ -2,7 +2,7 @@ decl str makecmd make decl str toolsclient decl -hidden int _make_current_error_line -def -shell-params make -docstring "Make utility wrapper" %{ %sh{ +def -params .. make -docstring "Make utility wrapper" %{ %sh{ output=$(mktemp -d -t kak-make.XXXXXXXX)/fifo mkfifo ${output} ( eval ${kak_opt_makecmd} "$@" > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null & diff --git a/rc/man.kak b/rc/man.kak index 03c80070..c1c401ff 100644 --- a/rc/man.kak +++ b/rc/man.kak @@ -14,7 +14,7 @@ hook global WinSetOption filetype=(?!man).* %{ rmhooks window man-hooks } -def -hidden -shell-params _man %{ %sh{ +def -hidden -params .. _man %{ %sh{ manout=$(mktemp /tmp/kak-man-XXXXXX) colout=$(mktemp /tmp/kak-man-XXXXXX) MANWIDTH=${kak_window_width} man "$@" > $manout @@ -33,7 +33,7 @@ def -hidden -shell-params _man %{ %sh{ fi } } -def -shell-params \ +def -params .. \ -shell-completion %{ prefix=${1:0:${kak_pos_in_token}} for page in /usr/share/man/*/${prefix}*.[1-8]*; do diff --git a/rc/tmux.kak b/rc/tmux.kak index b153ba15..171c8404 100644 --- a/rc/tmux.kak +++ b/rc/tmux.kak @@ -12,7 +12,7 @@ hook global KakBegin .* %{ } ## Temporarily override the default client creation command -def -hidden -shell-params tmux-new-impl %{ +def -hidden -params 1.. tmux-new-impl %{ %sh{ if [ -z "$TMUX" ]; then echo "echo -color Error This command is only available in a tmux session" @@ -25,20 +25,20 @@ def -hidden -shell-params tmux-new-impl %{ } } -def tmux-new-vertical -shell-params -command-completion -docstring "Create a new vertical pane in tmux" %{ +def tmux-new-vertical -params .. -command-completion -docstring "Create a new vertical pane in tmux" %{ %sh{ echo "tmux-new-impl 'split-window -v' $@" } } -def tmux-new-horizontal -shell-params -command-completion -docstring "Create a new horizontal pane in tmux" %{ +def tmux-new-horizontal -params .. -command-completion -docstring "Create a new horizontal pane in tmux" %{ %sh{ echo "tmux-new-impl 'split-window -h' $@" } } -def tmux-new-window -shell-params -command-completion -docstring "Create a new horizontal pane in tmux" %{ +def tmux-new-window -params .. -command-completion -docstring "Create a new horizontal pane in tmux" %{ %sh{ echo "tmux-new-impl 'new-window' $@" } } def -docstring "focus given client" \ - -shell-params -client-completion \ + -params 0..1 -client-completion \ tmux-focus %{ %sh{ if [ $# -gt 1 ]; then echo "echo -color Error 'too many arguments, use focus [client]'" diff --git a/rc/x11.kak b/rc/x11.kak index cd4d4465..dc4595e4 100644 --- a/rc/x11.kak +++ b/rc/x11.kak @@ -19,7 +19,7 @@ decl str termcmd %sh{ } def -docstring 'create a new kak client for current session' \ - -shell-params \ + -params .. \ -command-completion \ x11-new %{ %sh{ if [ -z "${kak_opt_termcmd}" ]; then @@ -31,7 +31,7 @@ def -docstring 'create a new kak client for current session' \ }} def -docstring 'focus given client\'s window' \ - -shell-params -client-completion \ + -params 0..1 -client-completion \ x11-focus %{ %sh{ if [ $# -gt 1 ]; then echo "echo -color Error 'too many arguments, use focus [client]'" diff --git a/share/kak/kakrc b/share/kak/kakrc index f7edadf5..3c49bd93 100644 --- a/share/kak/kakrc +++ b/share/kak/kakrc @@ -1,11 +1,11 @@ -def -shell-params runtime %{ %sh{ +def -params 1.. runtime %{ %sh{ while [ $# -gt 0 ]; do echo "source '${kak_runtime}/$1'" shift done }} -def -shell-params -docstring "colorscheme : enable named colorscheme" \ +def -params 1 -docstring "colorscheme : enable named colorscheme" \ -shell-completion %{ prefix=${1:0:${kak_pos_in_token}} ( diff --git a/src/commands.cc b/src/commands.cc index a7d180dc..4f85e641 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -721,9 +721,23 @@ void define_command(const ParametersParser& parser, Context& context, const Shel const String& commands = parser[1]; Command cmd; ParameterDesc desc; - if (parser.get_switch("shell-params")) + if (auto params = parser.get_switch("params")) { - desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional }; + size_t min = 0, max = -1; + StringView counts = *params; + static const Regex re{R"((\d+)?..(\d+)?)"}; + MatchResults res; + if (regex_match(counts.begin(), counts.end(), res, re)) + { + if (res[1].matched) + min = (size_t)str_to_int({res[1].first, res[1].second}); + if (res[2].matched) + max = (size_t)str_to_int({res[2].first, res[2].second}); + } + else + min = max = (size_t)str_to_int(counts); + + desc = ParameterDesc{ {}, ParameterDesc::Flags::SwitchesAsPositional, min, max }; cmd = [=](const ParametersParser& parser, Context& context, const ShellContext&) { CommandManager::instance().execute(commands, context, { params_to_shell(parser) }); }; @@ -815,15 +829,16 @@ const CommandDesc define_command_cmd = { nullptr, "def : define a command executing ", ParameterDesc{ - { { "shell-params", { false, "pass parameters to each shell escape as $0..$N" } }, - { "allow-override", { false, "allow overriding an existing command" } }, - { "hidden", { false, "do not display the command in completion candidates" } }, - { "docstring", { true, "define the documentation string for command" } }, - { "file-completion", { false, "complete parameters using filename completion" } }, - { "client-completion", { false, "complete parameters using client name completion" } }, - { "buffer-completion", { false, "complete parameters using buffer name completion" } }, + { { "params", { true, "take parameters, accessible to each shell escape as $0..$N\n" + "parameter should take the form or .. (both omittable)" } }, + { "allow-override", { false, "allow overriding an existing command" } }, + { "hidden", { false, "do not display the command in completion candidates" } }, + { "docstring", { true, "define the documentation string for command" } }, + { "file-completion", { false, "complete parameters using filename completion" } }, + { "client-completion", { false, "complete parameters using client name completion" } }, + { "buffer-completion", { false, "complete parameters using buffer name completion" } }, { "command-completion", { false, "complete parameters using kakoune command completion" } }, - { "shell-completion", { true, "complete the parameters using the given shell-script" } } }, + { "shell-completion", { true, "complete the parameters using the given shell-script" } } }, ParameterDesc::Flags::None, 2, 2 },