@@ -437,6 +437,11 @@ _comp_split()
437437# [-alR|-v arr|-c cur] to the child calls of `_comp_compgen` in
438438# `_comp_compgen_NAME`.
439439#
440+ # @remarks When no options are supplied to _comp_compgen, `_comp_compgen NAME
441+ # args` is equivalent to the direct call `_comp_compgen_NAME args`. As the
442+ # direct call is slightly more efficient, the direct call is preferred over
443+ # calling it through `_comp_compgen`.
444+ #
440445# @remarks Design `_comp_compgen_NAME`: a function that produce completions can
441446# be defined with the name _comp_compgen_NAME. The function is supposed to
442447# generate completions by calling `_comp_compgen`. To reflect the options
@@ -853,47 +858,52 @@ _comp_compgen_filedir()
853858 _comp_compgen_tilde && return
854859
855860 local -a toks
856- local arg =${1-}
861+ local _arg =${1-}
857862
858- if [[ $arg == -d ]]; then
863+ if [[ $_arg == -d ]]; then
859864 _comp_compgen -v toks -- -d
860865 else
861866 local ret
862867 _comp_quote_compgen " ${cur-} "
863- local quoted=$ret
868+ local _quoted=$ret
869+ _comp_unlocal ret
864870
865871 # Munge xspec to contain uppercase version too
866872 # https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
867873 # news://news.gmane.io/4C940E1C.1010304@case.edu
868- local xspec =${arg : +" !*.@($arg |${arg ^^} )" } plusdirs =()
874+ local _xspec =${_arg : +" !*.@($_arg |${_arg ^^} )" } _plusdirs =()
869875
870876 # Use plusdirs to get dir completions if we have a xspec; if we don't,
871877 # there's no need, dirs come along with other completions. Don't use
872878 # plusdirs quite yet if fallback is in use though, in order to not ruin
873879 # the fallback condition with the "plus" dirs.
874- local opts =(-f -X " $xspec " )
875- [[ $xspec ]] && plusdirs =(-o plusdirs)
880+ local _opts =(-f -X " $_xspec " )
881+ [[ $_xspec ]] && _plusdirs =(-o plusdirs)
876882 [[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-} } ||
877- ! ${plusdirs -} ]] ||
878- opts +=(" ${plusdirs [@]} " )
883+ ! ${_plusdirs -} ]] ||
884+ _opts +=(" ${_plusdirs [@]} " )
879885
880- _comp_compgen -v toks -c " $quoted " -- " ${opts [@]} "
886+ _comp_compgen -v toks -c " $_quoted " -- " ${_opts [@]} "
881887
882888 # Try without filter if it failed to produce anything and configured to
883889 [[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-} } &&
884- $arg && ${# toks[@]} -lt 1 ]] &&
885- _comp_compgen -av toks -c " $quoted " -- -f ${plusdirs+" ${plusdirs[@]} " }
890+ $_arg && ${# toks[@]} -lt 1 ]] &&
891+ _comp_compgen -av toks -c " $_quoted " -- \
892+ -f ${_plusdirs+" ${_plusdirs[@]} " }
886893 fi
887894
888895 if (( ${# toks[@]} != 0 )) ; then
889- # 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir unit test
896+ # 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
897+ # unit test
890898 compopt -o filenames 2> /dev/null
891899 fi
892900
893901 # Note: bash < 4.4 has a bug that all the elements are connected with
894902 # ${v-"${a[@]}"} when IFS does not contain whitespace.
895903 local IFS=$' \t\n '
896- _comp_compgen_set ${toks[@]+" ${toks[@]} " }
904+ local -a _tmp=(${toks[@]+" ${toks[@]} " } )
905+ _comp_unlocal toks
906+ _comp_compgen_set ${_tmp[@]+" ${_tmp[@]} " }
897907} # _comp_compgen_filedir()
898908
899909# This function splits $cur=--foo=bar into $prev=--foo, $cur=bar, making it
@@ -1154,7 +1164,7 @@ _comp_initialize()
11541164 ;;
11551165 esac
11561166 cur=${cur## " $redir " }
1157- _comp_compgen filedir " $xspec "
1167+ _comp_compgen_filedir " $xspec "
11581168 return 1
11591169 fi
11601170
0 commit comments