From 75120b11738ecdf15de1da0bff10d871a8dc7e12 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Tue, 30 Apr 2024 20:55:39 +0100 Subject: [PATCH 1/2] make it possible to `source` the Zsh autocomplete script --- autocomplete/zsh_autocomplete | 41 +++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/autocomplete/zsh_autocomplete b/autocomplete/zsh_autocomplete index 622143dc2b..b2ee002657 100644 --- a/autocomplete/zsh_autocomplete +++ b/autocomplete/zsh_autocomplete @@ -1,20 +1,29 @@ -#compdef $PROG +#compdef program +compdef _program program -_cli_zsh_autocomplete() { - local -a opts - local cur - cur=${words[-1]} - if [[ "$cur" == "-"* ]]; then - opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}") - else - opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}") - fi +# Replace "program" with the actual name of your CLI program. Let's say your CLI +# program is called "acme", then replace like so: +# * program => acme +# * _program => _acme - if [[ "${opts[1]}" != "" ]]; then - _describe 'values' opts - else - _files - fi +_program() { + local -a opts + local cur + cur=${words[-1]} + if [[ "$cur" == "-"* ]]; then + opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}") + else + opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}") + fi + + if [[ "${opts[1]}" != "" ]]; then + _describe 'values' opts + else + _files + fi } -compdef _cli_zsh_autocomplete $PROG +# don't run the completion function when being source-ed or eval-ed +if [ "$funcstack[1]" = "_program" ]; then + _program +fi From 0b61790ec75ea43b2266ea22f643f89731165b1f Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Wed, 1 May 2024 01:00:19 +0100 Subject: [PATCH 2/2] update comment at the top of the file to be more descriptive --- autocomplete/zsh_autocomplete | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autocomplete/zsh_autocomplete b/autocomplete/zsh_autocomplete index b2ee002657..109d382d34 100644 --- a/autocomplete/zsh_autocomplete +++ b/autocomplete/zsh_autocomplete @@ -1,8 +1,9 @@ #compdef program compdef _program program -# Replace "program" with the actual name of your CLI program. Let's say your CLI -# program is called "acme", then replace like so: +# Replace all occurences of "program" in this file with the actual name of your +# CLI program. We recommend using Find+Replace feature of your editor. Let's say +# your CLI program is called "acme", then replace like so: # * program => acme # * _program => _acme