Skip to content

Commit

Permalink
_filedir: avoid duplicate dirs internally, and a compgen -d call for …
Browse files Browse the repository at this point in the history
…files

Not having duplicates in completions here is useful so we can sometimes
check if a _filedir resulted in only one completion. The previous
implementation resulted in duplicate dirs internally.
  • Loading branch information
scop committed Oct 20, 2019
1 parent 2701887 commit da99bc5
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -559,25 +559,34 @@ _filedir()
local -a toks
local reset

reset=$(shopt -po noglob); set -o noglob
toks=( $(compgen -d -- "$cur") )
IFS=' '; $reset; IFS=$'\n'

if [[ "$1" != -d ]]; then
if [[ "$1" == -d ]]; then
reset=$(shopt -po noglob); set -o noglob
toks=( $(compgen -d -- "$cur") )
IFS=' '; $reset; IFS=$'\n'
else
local quoted
_quote_readline_by_ref "$cur" quoted

# Munge xspec to contain uppercase version too
# http://thread.gmane.org/gmane.comp.shells.bash.bugs/15294/focus=15306
local xspec=${1:+"!*.@($1|${1^^})"}
local xspec=${1:+"!*.@($1|${1^^})"} plusdirs=()

# Use plusdirs to get dir completions if we have a xspec; if we don't,
# there's no need, dirs come along with other completions. Don't use
# plusdirs quite yet if fallback is in use though, in order to not ruin
# the fallback condition with the "plus" dirs.
local opts=( -f -X "$xspec" )
[[ $xspec ]] && plusdirs=(-o plusdirs)
[[ ${COMP_FILEDIR_FALLBACK-} ]] || opts+=( "${plusdirs[@]}" )

reset=$(shopt -po noglob); set -o noglob
toks+=( $(compgen -f -X "$xspec" -- $quoted) )
toks+=( $(compgen "${opts[@]}" -- $quoted) )
IFS=' '; $reset; IFS=$'\n'

# Try without filter if it failed to produce anything and configured to
[[ -n ${COMP_FILEDIR_FALLBACK:-} && -n "$1" && ${#toks[@]} -lt 1 ]] && {
reset=$(shopt -po noglob); set -o noglob
toks+=( $(compgen -f -- $quoted) )
toks+=( $(compgen -f "${plusdirs[@]}" -- $quoted) )
IFS=' '; $reset; IFS=$'\n'
}
fi
Expand Down

0 comments on commit da99bc5

Please sign in to comment.