@@ -324,24 +324,27 @@ _comp_expand_glob()
324324# state of `IFS` and the shell option `noglob`. A naive splitting by
325325# `arr=(...)` suffers from unexpected IFS and pathname expansions, so one
326326# should prefer this function to such naive splitting.
327- # @param $1 array_name The array name
328- # The array name should not start with an underscores "_", which is
329- # internally used. The array name should not be either "IFS" or
330- # "OPT{IND,ARG,ERR}".
331- # @param $2 text The string to split
332327# OPTIONS
333328# -a Append to the array
334329# -F sep Set a set of separator characters (used as IFS). The default
335330# separator is $' \t\n'
336331# -l The same as -F $'\n'
332+ # @param $1 array_name The array name
333+ # The array name should not start with an underscores "_", which is
334+ # internally used. The array name should not be either "IFS" or
335+ # "OPT{IND,ARG,ERR}".
336+ # @param $2 text The string to split
337+ # @return 2 when the usage is wrong, 0 when one or more completions are
338+ # generated, or 1 when the execution succeeds but no candidates are
339+ # generated.
337340_comp_split ()
338341{
339- local _assign= ' = ' IFS=$' \t\n '
342+ local _append=false IFS=$' \t\n '
340343
341344 local OPTIND=1 OPTARG=" " OPTERR=0 _opt
342345 while getopts ' :alF:' _opt " $@ " ; do
343346 case $_opt in
344- a) _assign= ' += ' ;;
347+ a) _append=true ;;
345348 l) IFS=$' \n ' ;;
346349 F) IFS=$OPTARG ;;
347350 * )
@@ -363,10 +366,19 @@ _comp_split()
363366 local _original_opts=$SHELLOPTS
364367 set -o noglob
365368
366- eval " $1 $_assign (\$ 2)"
369+ local _old_size _new_size
370+ if " $_append " ; then
371+ eval " $1 +=()" # in case $1 is unset
372+ eval " _old_size=\$ {#$1 [@]}"
373+ eval " $1 +=(\$ 2)"
374+ else
375+ _old_size=0
376+ eval " $1 =(\$ 2)"
377+ fi
378+ eval " _new_size=\$ {#$1 [@]}"
367379
368380 [[ :$_original_opts : == * :noglob:* ]] || set +o noglob
369- return 0
381+ (( _new_size > _old_size ))
370382}
371383
372384# Check if the argument looks like a path.
@@ -2507,8 +2519,8 @@ __load_completion()
25072519 # 1) From BASH_COMPLETION_USER_DIR (e.g. ~/.local/share/bash-completion):
25082520 # User installed completions.
25092521 if [[ ${BASH_COMPLETION_USER_DIR-} ]]; then
2510- _comp_split -F : paths " $BASH_COMPLETION_USER_DIR "
2511- dirs+=(" ${paths[@]/%// completions} " )
2522+ _comp_split -F : paths " $BASH_COMPLETION_USER_DIR " &&
2523+ dirs+=(" ${paths[@]/%// completions} " )
25122524 else
25132525 dirs=(" ${XDG_DATA_HOME:- $HOME / .local/ share} /bash-completion/completions" )
25142526 fi
@@ -2536,8 +2548,8 @@ __load_completion()
25362548
25372549 # 4) From XDG_DATA_DIRS or system dirs (e.g. /usr/share, /usr/local/share):
25382550 # Completions in the system data dirs.
2539- _comp_split -F : paths " ${XDG_DATA_DIRS:-/ usr/ local/ share:/ usr/ share} "
2540- dirs+=(" ${paths[@]/%// bash-completion/ completions} " )
2551+ _comp_split -F : paths " ${XDG_DATA_DIRS:-/ usr/ local/ share:/ usr/ share} " &&
2552+ dirs+=(" ${paths[@]/%// bash-completion/ completions} " )
25412553
25422554 local backslash=
25432555 if [[ $cmd == \\ * ]]; then
0 commit comments