Skip to content

Commit 6693dc5

Browse files
committed
completion: make bash completion work in zsh
`compgen -W` does not behave the same way in zsh as it does in bash; it seems not to actually generate the completions we want. - [x] add a zsh equivalent and `_compgen_w` to abstract it away - [x] use `_compgen_w` instead of `compgen -W`
1 parent 396f219 commit 6693dc5

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

share/spack/bash/spack-completion.bash

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ if test -n "${ZSH_VERSION:-}" ; then
5252
fi
5353
fi
5454

55+
# compgen -W doesn't work in some versions of zsh, so use this instead.
56+
# see https://www.zsh.org/mla/workers/2011/msg00582.html
57+
_compgen_w() {
58+
if test -n "${ZSH_VERSION:-}" ; then
59+
typeset -a words
60+
words=( ${~=1} )
61+
local find="$2"
62+
results=(${(M)words[@]:#$find*})
63+
echo "${results[@]}"
64+
else
65+
compgen -W "$1" -- "$2"
66+
fi
67+
}
68+
5569
# Bash programmable completion for Spack
5670
_bash_completion_spack() {
5771
# In all following examples, let the cursor be denoted by brackets, i.e. []
@@ -137,7 +151,7 @@ _bash_completion_spack() {
137151
if [[ "$(LC_ALL=C type $subfunction 2>&1)" =~ $rgx ]]
138152
then
139153
$subfunction
140-
COMPREPLY=($(compgen -W "$SPACK_COMPREPLY" -- "$cur"))
154+
COMPREPLY=($(_compgen_w "$SPACK_COMPREPLY" "$cur"))
141155
fi
142156

143157
# if every completion is an alias for the same thing, just return that thing.
@@ -359,7 +373,7 @@ _spack_compress_aliases() {
359373
fi
360374

361375
# get the alias of the first thing in the list of completions
362-
_spack_get_alias "${COMPREPLY[0]}"
376+
_spack_get_alias "${COMPREPLY[@]:0:1}"
363377
local first_alias="$SPACK_ALIAS"
364378

365379
# if anything in the list would alias to something different, stop

share/spack/spack-completion.bash

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ if test -n "${ZSH_VERSION:-}" ; then
5252
fi
5353
fi
5454

55+
# compgen -W doesn't work in some versions of zsh, so use this instead.
56+
# see https://www.zsh.org/mla/workers/2011/msg00582.html
57+
_compgen_w() {
58+
if test -n "${ZSH_VERSION:-}" ; then
59+
typeset -a words
60+
words=( ${~=1} )
61+
local find="$2"
62+
results=(${(M)words[@]:#$find*})
63+
echo "${results[@]}"
64+
else
65+
compgen -W "$1" -- "$2"
66+
fi
67+
}
68+
5569
# Bash programmable completion for Spack
5670
_bash_completion_spack() {
5771
# In all following examples, let the cursor be denoted by brackets, i.e. []
@@ -137,7 +151,7 @@ _bash_completion_spack() {
137151
if [[ "$(LC_ALL=C type $subfunction 2>&1)" =~ $rgx ]]
138152
then
139153
$subfunction
140-
COMPREPLY=($(compgen -W "$SPACK_COMPREPLY" -- "$cur"))
154+
COMPREPLY=($(_compgen_w "$SPACK_COMPREPLY" "$cur"))
141155
fi
142156

143157
# if every completion is an alias for the same thing, just return that thing.
@@ -359,7 +373,7 @@ _spack_compress_aliases() {
359373
fi
360374

361375
# get the alias of the first thing in the list of completions
362-
_spack_get_alias "${COMPREPLY[0]}"
376+
_spack_get_alias "${COMPREPLY[@]:0:1}"
363377
local first_alias="$SPACK_ALIAS"
364378

365379
# if anything in the list would alias to something different, stop

0 commit comments

Comments
 (0)