Skip to content

Commit ed7d46e

Browse files
scopakinomyoga
andcommitted
fix(_filedir): apply trailing slashes with -f
If we are completing entries somewhere else besides the current dir, `compopt -o filenames` won't do its usual thing; append slashes ourselves. Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
1 parent 1f240a6 commit ed7d46e

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

bash_completion

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,15 @@ _comp_quote_compgen()
11661166

11671167
# This function performs file and directory completion. It's better than
11681168
# simply using 'compgen -f', because it honours spaces in filenames.
1169-
# @param $1 If `-d', complete only on directories. Otherwise filter/pick only
1170-
# completions with `.$1' and the uppercase version of it as file
1171-
# extension.
1169+
# @param $1 Complete filenames matching `.$1' and the uppercase version of it.
1170+
# Ignored with `-d`.
1171+
# OPTIONS
1172+
# -d Complete only on directories
1173+
# -f Perform `compopt -f filenames` modifications manually. This
1174+
# suffixes a slash to a directory name. This can be combined with
1175+
# the `-C dir` option to `_comp_compgen`, where the generated
1176+
# filenames do not exist in the current working directory and Bash
1177+
# fails to properly detect the filenames.
11721178
# @return 0 if at least one completion is generated, or 1 otherwise.
11731179
#
11741180
# @since 2.12
@@ -1177,9 +1183,22 @@ _comp_compgen_filedir()
11771183
_comp_compgen_tilde && return
11781184

11791185
local -a toks
1186+
local _dir="" _filenames=""
1187+
local OPTIND=1 OPTARG="" OPTERR=0 _opt
1188+
while getopts ":df" _opt "$@"; do
1189+
case $_opt in
1190+
d) _dir=set ;;
1191+
f) _filenames=set ;;
1192+
*)
1193+
printf "bash_completion: %s: usage error\n" "$FUNCNAME" >&2
1194+
return 2
1195+
;;
1196+
esac
1197+
done
1198+
shift "$((OPTIND - 1))"
11801199
local _arg=${1-}
11811200

1182-
if [[ $_arg == -d ]]; then
1201+
if [[ $_dir ]]; then
11831202
_comp_compgen -v toks -- -d
11841203
else
11851204
local REPLY
@@ -1223,9 +1242,19 @@ _comp_compgen_filedir()
12231242
fi
12241243

12251244
if ((${#toks[@]} != 0)); then
1226-
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
1227-
# unit test
1228-
compopt -o filenames 2>/dev/null
1245+
# compopt 2>/dev/null for direct invocation, e.g. in
1246+
# _comp_compgen_filedir unit test
1247+
if [[ $_filenames ]]; then
1248+
local i
1249+
for i in "${!toks[@]}"; do
1250+
if [[ -d ${toks[i]} ]]; then
1251+
toks[i]+=/
1252+
compopt -o nospace 2>/dev/null
1253+
fi
1254+
done
1255+
else
1256+
compopt -o filenames 2>/dev/null
1257+
fi
12291258
fi
12301259

12311260
# Note: bash < 4.4 has a bug that all the elements are connected with

completions/_mount.linux

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ _comp_cmd_mount()
3838
return
3939
;;
4040
-L)
41-
_comp_compgen -C "/dev/disk/by-label/" -- -f
41+
_comp_compgen -C "/dev/disk/by-label/" filedir -f
4242
return
4343
;;
4444
-U)
45-
_comp_compgen -C "/dev/disk/by-uuid/" -- -f
45+
_comp_compgen -C "/dev/disk/by-uuid/" filedir -f
4646
return
4747
;;
4848
-O | --test-opts)

completions/_slackpkg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ _comp_cmd_slackpkg()
6565
;;
6666
install-template | remove-template)
6767
if [[ -e $confdir/templates ]]; then
68-
_comp_compgen -C "$confdir/templates" -- -f -X \
69-
"!?*.template" && COMPREPLY=("${COMPREPLY[@]%.template}")
68+
_comp_compgen -C "$confdir/templates" filedir -f template &&
69+
COMPREPLY=("${COMPREPLY[@]%.template}")
7070
fi
7171
return
7272
;;
7373
remove)
7474
_comp_compgen_filedir
7575
_comp_compgen -a -- -W 'a ap d e f k kde kdei l n t tcl x xap xfce
7676
y'
77-
_comp_compgen -aC /var/log/packages -- -f
77+
_comp_compgen -aC /var/log/packages filedir -f
7878
return
7979
;;
8080
install | reinstall | upgrade | blacklist | download)

completions/removepkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _comp_cmd_removepkg()
1515
fi
1616

1717
local root=${ROOT:-/}
18-
_comp_compgen -C "$root/var/log/packages" -- -f
18+
_comp_compgen -C "$root/var/log/packages" filedir -f
1919
} &&
2020
complete -F _comp_cmd_removepkg removepkg
2121

completions/sbopkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ _comp_cmd_sbopkg()
6363

6464
_comp_compgen_split -l -- "$(command sed -ne "s/^SLACKBUILD NAME: //p" \
6565
"$file")"
66-
_comp_compgen -aC "$QUEUEDIR" -- -f -X "!*.sqf"
66+
_comp_compgen -aC "$QUEUEDIR" filedir -f sqf
6767
} &&
6868
complete -F _comp_cmd_sbopkg sbopkg
6969

completions/slapt-get

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ _comp_cmd_slapt_get()
7474
return
7575
;;
7676
ins) # --remove|--filelist
77-
_comp_compgen -C /var/log/packages -- -f
77+
_comp_compgen -C /var/log/packages filedir -f
7878
return
7979
;;
8080
set) # --install-set

0 commit comments

Comments
 (0)