@@ -324,24 +324,27 @@ _comp_expand_glob()
324
324
# state of `IFS` and the shell option `noglob`. A naive splitting by
325
325
# `arr=(...)` suffers from unexpected IFS and pathname expansions, so one
326
326
# 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
332
327
# OPTIONS
333
328
# -a Append to the array
334
329
# -F sep Set a set of separator characters (used as IFS). The default
335
330
# separator is $' \t\n'
336
331
# -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.
337
340
_comp_split ()
338
341
{
339
- local _assign= ' = ' IFS=$' \t\n '
342
+ local _append=false IFS=$' \t\n '
340
343
341
344
local OPTIND=1 OPTARG=" " OPTERR=0 _opt
342
345
while getopts ' :alF:' _opt " $@ " ; do
343
346
case $_opt in
344
- a) _assign= ' += ' ;;
347
+ a) _append=true ;;
345
348
l) IFS=$' \n ' ;;
346
349
F) IFS=$OPTARG ;;
347
350
* )
@@ -363,10 +366,19 @@ _comp_split()
363
366
local _original_opts=$SHELLOPTS
364
367
set -o noglob
365
368
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 [@]}"
367
379
368
380
[[ :$_original_opts : == * :noglob:* ]] || set +o noglob
369
- return 0
381
+ (( _new_size > _old_size ))
370
382
}
371
383
372
384
# Check if the argument looks like a path.
@@ -2507,8 +2519,8 @@ __load_completion()
2507
2519
# 1) From BASH_COMPLETION_USER_DIR (e.g. ~/.local/share/bash-completion):
2508
2520
# User installed completions.
2509
2521
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} " )
2512
2524
else
2513
2525
dirs=(" ${XDG_DATA_HOME:- $HOME / .local/ share} /bash-completion/completions" )
2514
2526
fi
@@ -2536,8 +2548,8 @@ __load_completion()
2536
2548
2537
2549
# 4) From XDG_DATA_DIRS or system dirs (e.g. /usr/share, /usr/local/share):
2538
2550
# 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} " )
2541
2553
2542
2554
local backslash=
2543
2555
if [[ $cmd == \\ * ]]; then
0 commit comments