Skip to content

Commit 5a07301

Browse files
committed
fix(_filedir): apply trailing slashes with -C ourselves
If we are completing entries somewhere else besides the current dir, `compopt -o filenames` won't do its usual thing; append slashes ourselves.
1 parent 59a294c commit 5a07301

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

bash_completion

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ _get_cword()
716716
_get_pword()
717717
{
718718
if ((COMP_CWORD >= 1)); then
719-
_get_cword "${@:-}" 1
719+
_get_cword "${@-}" 1
720720
fi
721721
}
722722

@@ -792,7 +792,7 @@ _quote_readline_by_ref()
792792
# @param $1 Complete filenames matching `.$1' and the uppercase version of it.
793793
# Ignored with `-d`.
794794
# OPTIONS
795-
# -C dir Change to dir before generating completions
795+
# -C dir Complete entries in specified directory
796796
# -d Complete only on directories
797797
_filedir()
798798
{
@@ -801,12 +801,12 @@ _filedir()
801801
_tilde "${cur-}" || return
802802

803803
local -a toks
804-
local reset arg chdir=. dir=false
804+
local reset arg chdir="" dir=false
805805

806806
local OPTIND=1 OPTARG="" OPTERR=0
807807
while getopts ":C:d" arg "$@"; do
808808
case $arg in
809-
C) chdir=$OPTARG ;;
809+
C) chdir="$OPTARG/" ;;
810810
d) dir=true ;;
811811
*)
812812
echo "bash_completion: $FUNCNAME: usage error" >&2
@@ -820,13 +820,13 @@ _filedir()
820820
if $dir; then
821821
reset=$(shopt -po noglob)
822822
set -o noglob
823-
toks=($(command cd -- "$chdir" 2>/dev/null && compgen -d -- "${cur-}"))
823+
toks=($(compgen -d -- "$chdir${cur-}"))
824824
IFS=' '
825825
$reset
826826
IFS=$'\n'
827827
else
828828
local quoted
829-
_quote_readline_by_ref "${cur-}" quoted
829+
_quote_readline_by_ref "$chdir${cur-}" quoted
830830

831831
# Munge xspec to contain uppercase version too
832832
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
@@ -845,10 +845,7 @@ _filedir()
845845

846846
reset=$(shopt -po noglob)
847847
set -o noglob
848-
toks+=($(
849-
command cd -- "$chdir" 2>/dev/null &&
850-
compgen "${opts[@]}" -- $quoted
851-
))
848+
toks+=($(compgen "${opts[@]}" -- $quoted))
852849
IFS=' '
853850
$reset
854851
IFS=$'\n'
@@ -858,20 +855,27 @@ _filedir()
858855
$arg && ${#toks[@]} -lt 1 ]] && {
859856
reset=$(shopt -po noglob)
860857
set -o noglob
861-
toks+=($(
862-
command cd -- "$chdir" 2>/dev/null &&
863-
compgen -f ${plusdirs+"${plusdirs[@]}"} -- $quoted
864-
))
858+
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- $quoted))
865859
IFS=' '
866860
$reset
867861
IFS=$'\n'
868862
}
869863
fi
870864

871865
if ((${#toks[@]} != 0)); then
872-
# 2>/dev/null for direct invocation, e.g. in the _filedir unit test
873-
compopt -o filenames 2>/dev/null
874-
COMPREPLY+=("${toks[@]}")
866+
# compopt 2>/dev/null for direct invocation, e.g. in _filedir unit test
867+
if [[ -n $chdir ]]; then
868+
local i
869+
for i in ${!toks[*]}; do
870+
if [[ -d ${toks[i]} ]]; then
871+
toks[i]+=/
872+
compopt -o nospace 2>/dev/null
873+
fi
874+
done
875+
else
876+
compopt -o filenames 2>/dev/null
877+
fi
878+
COMPREPLY+=("${toks[@]#$chdir}")
875879
fi
876880
} # _filedir()
877881

@@ -1392,9 +1396,9 @@ _available_interfaces()
13921396
local PATH=$PATH:/sbin
13931397

13941398
COMPREPLY=($({
1395-
if [[ ${1:-} == -w ]]; then
1399+
if [[ ${1-} == -w ]]; then
13961400
iwconfig
1397-
elif [[ ${1:-} == -a ]]; then
1401+
elif [[ ${1-} == -a ]]; then
13981402
ifconfig || ip -c=never link show up || ip link show up
13991403
else
14001404
ifconfig -a || ip -c=never link show || ip link show
@@ -2247,7 +2251,7 @@ _cd()
22472251

22482252
# Use standard dir completion if no CDPATH or parameter starts with /,
22492253
# ./ or ../
2250-
if [[ ! ${CDPATH:-} || $cur == ?(.)?(.)/* ]]; then
2254+
if [[ ! ${CDPATH-} || $cur == ?(.)?(.)/* ]]; then
22512255
_filedir -d
22522256
return
22532257
fi
@@ -2435,7 +2439,7 @@ complete -F _comp_root_command fakeroot gksu gksudo kdesudo really
24352439
# Return true if the completion should be treated as running as root
24362440
_complete_as_root()
24372441
{
2438-
[[ $EUID -eq 0 || ${root_command:-} ]]
2442+
[[ $EUID -eq 0 || ${root_command-} ]]
24392443
}
24402444

24412445
_longopt()

0 commit comments

Comments
 (0)