Skip to content

Commit f9c0471

Browse files
committed
feat(_comp_compgen_set): add a function to just store values
1 parent 7054a3c commit f9c0471

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

bash_completion

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ _comp_split()
449449
#
450450
# _comp_compgen -R -- -W '"${words[@]}"'
451451
#
452+
# or use the utility `_comp_compgen_set`:
453+
#
454+
# _comp_compgen_set "${words[@]}"
455+
#
452456
# Other nested calls of _comp_compgen can also be used. The function is
453457
# supposed to replace the existing content of the array by default to allow the
454458
# caller control whether to replace or append by the option `-a`.
@@ -540,6 +544,22 @@ _comp_compgen()
540544
_comp_split -l ${_append:+-a} "$_var" "$_result"
541545
}
542546

547+
# usage: _comp_compgen_set [words...]
548+
# Reset COMPREPLY with the specified WORDS. If no arguments are specified, the
549+
# array is cleared.
550+
#
551+
# When an array name is specified by `-v VAR` in a caller _comp_compgen, the
552+
# array is reset instead of COMPREPLY. When the `-a` flag is specified in a
553+
# caller _comp_compgen, the words are appended to the existing elements of the
554+
# array instead of replacing the existing elements. This function ignores
555+
# ${cur-} or the prefix specified by `-v CUR`.
556+
_comp_compgen_set()
557+
{
558+
local _append=${_comp_compgen__append-}
559+
local _var=${_comp_compgen__var-COMPREPLY}
560+
eval -- "$_var${_append:++}=(\"\$@\")"
561+
}
562+
543563
# Check if the argument looks like a path.
544564
# @param $1 thing to check
545565
# @return True (0) if it does, False (> 0) otherwise
@@ -868,8 +888,12 @@ _comp_compgen_filedir()
868888
if ((${#toks[@]} != 0)); then
869889
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir unit test
870890
compopt -o filenames 2>/dev/null
871-
_comp_compgen -R -- -W '"${toks[@]}"'
872891
fi
892+
893+
# Note: bash < 4.4 has a bug that all the elements are connected with
894+
# ${v-"${a[@]}"} when IFS does not contain whitespace.
895+
local IFS=$' \t\n'
896+
_comp_compgen_set ${toks[@]+"${toks[@]}"}
873897
} # _comp_compgen_filedir()
874898

875899
# This function splits $cur=--foo=bar into $prev=--foo, $cur=bar, making it

0 commit comments

Comments
 (0)