Skip to content

Commit 5b83ee5

Browse files
committed
refactor: use _comp_split [to be merged into failglob-1 after processing scop#870]
1 parent e6d0bb3 commit 5b83ee5

File tree

1 file changed

+37
-80
lines changed

1 file changed

+37
-80
lines changed

bash_completion

Lines changed: 37 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,13 @@ _comp_quote_compgen()
721721
#
722722
_filedir()
723723
{
724-
local IFS=$'\n'
725-
726724
_tilde "${cur-}" || return
727725

728726
local -a toks
729-
local reset arg=${1-}
727+
local arg=${1-}
730728

731729
if [[ $arg == -d ]]; then
732-
reset=$(shopt -po noglob)
733-
set -o noglob
734-
toks=($(compgen -d -- "${cur-}"))
735-
IFS=' '
736-
$reset
737-
IFS=$'\n'
730+
_comp_compgen toks -d -- "${cur-}"
738731
else
739732
local ret
740733
_comp_quote_compgen "${cur-}"
@@ -755,23 +748,12 @@ _filedir()
755748
! ${plusdirs-} ]] ||
756749
opts+=("${plusdirs[@]}")
757750

758-
reset=$(shopt -po noglob)
759-
set -o noglob
760-
toks+=($(compgen "${opts[@]}" -- "$quoted"))
761-
IFS=' '
762-
$reset
763-
IFS=$'\n'
751+
_comp_compgen toks "${opts[@]}" -- "$quoted"
764752

765753
# Try without filter if it failed to produce anything and configured to
766754
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} &&
767-
$arg && ${#toks[@]} -lt 1 ]] && {
768-
reset=$(shopt -po noglob)
769-
set -o noglob
770-
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- "$quoted"))
771-
IFS=' '
772-
$reset
773-
IFS=$'\n'
774-
}
755+
$arg && ${#toks[@]} -lt 1 ]] &&
756+
_comp_compgen -a toks -f ${plusdirs+"${plusdirs[@]}"} -- "$quoted"
775757
fi
776758

777759
if ((${#toks[@]} != 0)); then
@@ -826,12 +808,8 @@ _variables()
826808
return 0
827809
elif [[ $cur =~ ^(\$\{[#!]?)([A-Za-z0-9_]*)\[([^]]*)$ ]]; then
828810
# Complete ${array[i with ${array[idx]}
829-
local reset=$(shopt -po noglob) IFS=$'\n'
830-
set -o noglob
831-
COMPREPLY+=($(compgen -W '$(printf %s\\n "${!'"${BASH_REMATCH[2]}"'[@]}")' \
832-
-P "${BASH_REMATCH[1]}${BASH_REMATCH[2]}[" -S ']}' -- "${BASH_REMATCH[3]}"))
833-
IFS=$' \t\n'
834-
$reset
811+
_comp_compgen -a COMPREPLY -W '"${!'"${BASH_REMATCH[2]}"'[@]}"' \
812+
-P "${BASH_REMATCH[1]}${BASH_REMATCH[2]}[" -S ']}' -- "${BASH_REMATCH[3]}"
835813
# Complete ${arr[@ and ${arr[*
836814
if [[ ${BASH_REMATCH[3]} == [@*] ]]; then
837815
COMPREPLY+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}[${BASH_REMATCH[3]}]}")
@@ -1057,28 +1035,27 @@ _comp_initialize()
10571035
# @return True (0) if an option was found, False (> 0) otherwise
10581036
__parse_options()
10591037
{
1060-
local option option2 i IFS=$' \t\n,/|'
1038+
local option option2 i
10611039

10621040
# Take first found long option, or first one (short) if not found.
10631041
option=
1064-
local reset=$(shopt -po noglob)
1065-
set -o noglob
1066-
local -a array=($1)
1067-
$reset
1068-
for i in "${array[@]}"; do
1069-
case "$i" in
1070-
---*) break ;;
1071-
--?*)
1072-
option=$i
1073-
break
1074-
;;
1075-
-?*) [[ $option ]] || option=$i ;;
1076-
*) break ;;
1077-
esac
1078-
done
1042+
local -a array
1043+
if _comp_split -F $' \t\n,/|' array "$1"; then
1044+
for i in "${array[@]}"; do
1045+
case "$i" in
1046+
---*) break ;;
1047+
--?*)
1048+
option=$i
1049+
break
1050+
;;
1051+
-?*) [[ $option ]] || option=$i ;;
1052+
*) break ;;
1053+
esac
1054+
done
1055+
fi
10791056
[[ $option ]] || return 1
10801057

1081-
IFS=$' \t\n' # affects parsing of the regexps below...
1058+
local IFS=$' \t\n' # affects parsing of the regexps below...
10821059

10831060
# Expand --[no]foo to --foo and --nofoo etc
10841061
if [[ $option =~ (\[((no|dont)-?)\]). ]]; then
@@ -1335,12 +1312,13 @@ _tilde()
13351312
local result=0
13361313
if [[ ${1-} == \~* && $1 != */* ]]; then
13371314
# Try generate ~username completions
1338-
COMPREPLY=($(compgen -P '~' -u -- "${1#\~}"))
1339-
result=${#COMPREPLY[@]}
1340-
# 2>/dev/null for direct invocation, e.g. in the _tilde unit test
1341-
((result > 0)) && compopt -o filenames 2>/dev/null
1315+
if _comp_compgen COMPREPLY -P '~' -u -- "${1#\~}"; then
1316+
# 2>/dev/null for direct invocation, e.g. in the _tilde unit test
1317+
compopt -o filenames 2>/dev/null
1318+
return 1
1319+
fi
13421320
fi
1343-
return "$((result > 0 ? 1 : 0))"
1321+
return 0
13441322
}
13451323

13461324
# Expand variable starting with tilde (~)
@@ -1879,12 +1857,10 @@ _included_ssh_config_files()
18791857
local configfile i files f
18801858
configfile=$1
18811859

1882-
local IFS=$' \t\n' reset=$(shopt -po noglob)
1883-
set -o noglob
1884-
local included=($(command sed -ne 's/^[[:blank:]]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][[:blank:]]\(.*\)$/\1/p' "${configfile}"))
1885-
$reset
1860+
local -a included
1861+
_comp_split included "$(command sed -ne 's/^[[:blank:]]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][[:blank:]]\(.*\)$/\1/p' "${configfile}")"
1862+
((${#included[@]})) || return
18861863

1887-
[[ ${included-} ]] || return
18881864
for i in "${included[@]}"; do
18891865
# Check the origin of $configfile to complete relative included paths on included
18901866
# files according to ssh_config(5):
@@ -2419,16 +2395,9 @@ _filedir_xspec()
24192395
_comp_quote_compgen "$cur"
24202396
local quoted=$ret
24212397
2422-
local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
2398+
local xspec=${_xspecs[${1##*/}]} tmp
24232399
local -a toks
2424-
2425-
toks=($(
2426-
compgen -d -- "$quoted" | {
2427-
while read -r tmp; do
2428-
printf '%s\n' "$tmp"
2429-
done
2430-
}
2431-
))
2400+
_comp_compgen toks -d -- "$quoted"
24322401
24332402
# Munge xspec to contain uppercase version too
24342403
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
@@ -2441,24 +2410,12 @@ _filedir_xspec()
24412410
fi
24422411
xspec="$matchop($xspec|${xspec^^})"
24432412
2444-
toks+=($(
2445-
eval compgen -f -X "'!$xspec'" -- '$quoted' | {
2446-
while read -r tmp; do
2447-
[[ $tmp ]] && printf '%s\n' "$tmp"
2448-
done
2449-
}
2450-
))
2413+
_comp_compgen -a toks -f -X "@(|!($xspec))" -- "$quoted"
24512414
24522415
# Try without filter if it failed to produce anything and configured to
24532416
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} &&
2454-
${#toks[@]} -lt 1 ]] && {
2455-
local reset=$(shopt -po noglob)
2456-
set -o noglob
2457-
toks+=($(compgen -f -- "$quoted"))
2458-
IFS=' '
2459-
$reset
2460-
IFS=$'\n'
2461-
}
2417+
${#toks[@]} -lt 1 ]] &&
2418+
_comp_compgen -a toks -f -- "$quoted"
24622419
24632420
if ((${#toks[@]} != 0)); then
24642421
compopt -o filenames

0 commit comments

Comments
 (0)