@@ -1232,9 +1232,15 @@ _comp_quote_compgen()
12321232
12331233# This function performs file and directory completion. It's better than
12341234# simply using 'compgen -f', because it honours spaces in filenames.
1235- # @param $1 If `-d', complete only on directories. Otherwise filter/pick only
1236- # completions with `.$1' and the uppercase version of it as file
1237- # extension.
1235+ # @param $1 Complete filenames matching `.$1' and the uppercase version of it.
1236+ # Ignored with `-d`.
1237+ # OPTIONS
1238+ # -d Complete only on directories
1239+ # -f Perform `compopt -f filenames` modifications manually. This
1240+ # suffixes a slash to a directory name. This can be combined with
1241+ # the `-C dir` option to `_comp_compgen`, where the generated
1242+ # filenames do not exist in the current working directory and Bash
1243+ # fails to properly detect the filenames.
12381244# @return 0 if at least one completion is generated, or 1 otherwise.
12391245#
12401246# @since 2.12
@@ -1243,9 +1249,22 @@ _comp_compgen_filedir()
12431249 _comp_compgen_tilde && return
12441250
12451251 local -a toks
1252+ local _dir=" " _filenames=" "
1253+ local OPTIND=1 OPTARG=" " OPTERR=0 _opt
1254+ while getopts " :df" _opt " $@ " ; do
1255+ case $_opt in
1256+ d) _dir=set ;;
1257+ f) _filenames=set ;;
1258+ * )
1259+ printf " bash_completion: %s: usage error\n" " $FUNCNAME " >&2
1260+ return 2
1261+ ;;
1262+ esac
1263+ done
1264+ shift " $(( OPTIND - 1 )) "
12461265 local _arg=${1-}
12471266
1248- if [[ $_arg == -d ]]; then
1267+ if [[ $_dir ]]; then
12491268 _comp_compgen -v toks -- -d
12501269 else
12511270 local REPLY
@@ -1289,9 +1308,19 @@ _comp_compgen_filedir()
12891308 fi
12901309
12911310 if (( ${# toks[@]} != 0 )) ; then
1292- # 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
1293- # unit test
1294- compopt -o filenames 2> /dev/null
1311+ # compopt 2>/dev/null for direct invocation, e.g. in
1312+ # _comp_compgen_filedir unit test
1313+ if [[ $_filenames ]]; then
1314+ local i
1315+ for i in " ${! toks[@]} " ; do
1316+ if [[ -d ${toks[i]} ]]; then
1317+ toks[i]+=/
1318+ compopt -o nospace 2> /dev/null
1319+ fi
1320+ done
1321+ else
1322+ compopt -o filenames 2> /dev/null
1323+ fi
12951324 fi
12961325
12971326 # Note: bash < 4.4 has a bug that all the elements are connected with
0 commit comments