Skip to content

style(_comp_compgen): call _comp_compgen_filedir directly #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
38 changes: 24 additions & 14 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ _comp_split()
# [-alR|-v arr|-c cur] to the child calls of `_comp_compgen` in
# `_comp_compgen_NAME`.
#
# @remarks When no options are supplied to _comp_compgen, `_comp_compgen NAME
# args` is equivalent to the direct call `_comp_compgen_NAME args`. As the
# direct call is slightly more efficient, the direct call is preferred over
# calling it through `_comp_compgen`.
#
# @remarks Design `_comp_compgen_NAME`: a function that produce completions can
# be defined with the name _comp_compgen_NAME. The function is supposed to
# generate completions by calling `_comp_compgen`. To reflect the options
Expand Down Expand Up @@ -853,47 +858,52 @@ _comp_compgen_filedir()
_comp_compgen_tilde && return

local -a toks
local arg=${1-}
local _arg=${1-}

if [[ $arg == -d ]]; then
if [[ $_arg == -d ]]; then
_comp_compgen -v toks -- -d
else
local ret
_comp_quote_compgen "${cur-}"
local quoted=$ret
local _quoted=$ret
_comp_unlocal ret

# Munge xspec to contain uppercase version too
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
# news://news.gmane.io/[email protected]
local xspec=${arg:+"!*.@($arg|${arg^^})"} plusdirs=()
local _xspec=${_arg:+"!*.@($_arg|${_arg^^})"} _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)
local _opts=(-f -X "$_xspec")
[[ $_xspec ]] && _plusdirs=(-o plusdirs)
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} ||
! ${plusdirs-} ]] ||
opts+=("${plusdirs[@]}")
! ${_plusdirs-} ]] ||
_opts+=("${_plusdirs[@]}")

_comp_compgen -v toks -c "$quoted" -- "${opts[@]}"
_comp_compgen -v toks -c "$_quoted" -- "${_opts[@]}"

# Try without filter if it failed to produce anything and configured to
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-}} &&
$arg && ${#toks[@]} -lt 1 ]] &&
_comp_compgen -av toks -c "$quoted" -- -f ${plusdirs+"${plusdirs[@]}"}
$_arg && ${#toks[@]} -lt 1 ]] &&
_comp_compgen -av toks -c "$_quoted" -- \
-f ${_plusdirs+"${_plusdirs[@]}"}
fi

if ((${#toks[@]} != 0)); then
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir unit test
# 2>/dev/null for direct invocation, e.g. in the _comp_compgen_filedir
# unit test
compopt -o filenames 2>/dev/null
fi

# Note: bash < 4.4 has a bug that all the elements are connected with
# ${v-"${a[@]}"} when IFS does not contain whitespace.
local IFS=$' \t\n'
_comp_compgen_set ${toks[@]+"${toks[@]}"}
local -a _tmp=(${toks[@]+"${toks[@]}"})
_comp_unlocal toks
_comp_compgen_set ${_tmp[@]+"${_tmp[@]}"}
} # _comp_compgen_filedir()

# This function splits $cur=--foo=bar into $prev=--foo, $cur=bar, making it
Expand Down Expand Up @@ -1154,7 +1164,7 @@ _comp_initialize()
;;
esac
cur=${cur##"$redir"}
_comp_compgen filedir "$xspec"
_comp_compgen_filedir "$xspec"
return 1
fi

Expand Down
4 changes: 2 additions & 2 deletions completions/2to3
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _comp_cmd_2to3()
return
;;
-o | --output-dir)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
esac
Expand All @@ -32,7 +32,7 @@ _comp_cmd_2to3()
return
fi

_comp_compgen filedir py
_comp_compgen_filedir py
} &&
complete -F _comp_cmd_2to3 2to3

Expand Down
2 changes: 1 addition & 1 deletion completions/7z
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ _comp_cmd_7z()
2>/dev/null | tail -n+2)")" -- "$cur"))
compopt -o filenames
else
_comp_compgen filedir
_comp_compgen_filedir
fi
fi
} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/_adb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _comp_cmd_adb()
return
;;
-f)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/_chsh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ _comp_cmd_chsh()
return
;;
-R | --root)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
-s | --shell)
Expand Down
4 changes: 2 additions & 2 deletions completions/_hexdump
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _comp_cmd_hexdump()
return
;;
-f)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
esac
Expand All @@ -25,7 +25,7 @@ _comp_cmd_hexdump()
return
fi

_comp_compgen filedir
_comp_compgen_filedir
} &&
complete -F _comp_cmd_hexdump hexdump hd

Expand Down
2 changes: 1 addition & 1 deletion completions/_hwclock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _comp_cmd_hwclock()
return
;;
-f | --rtc | --adjfile)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
esac
Expand Down
6 changes: 3 additions & 3 deletions completions/_mock
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ _comp_cmd_mock()
return
;;
--configdir | --resultdir)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
--spec)
_comp_compgen filedir spec
_comp_compgen_filedir spec
return
;;
--target)
Expand All @@ -61,7 +61,7 @@ _comp_cmd_mock()
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
_comp_compgen filedir '@(?(no)src.r|s)pm'
_comp_compgen_filedir '@(?(no)src.r|s)pm'
fi
} &&
complete -F _comp_cmd_mock mock
Expand Down
2 changes: 1 addition & 1 deletion completions/_mount.linux
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _comp_cmd_mount()
return
;;
--bind | -B | --rbind | -R)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
-p | --pass-fd)
Expand Down
2 changes: 1 addition & 1 deletion completions/_repomanage
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _comp_cmd_repomanage()
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
_comp_compgen filedir -d
_comp_compgen_filedir -d
fi
} &&
complete -F _comp_cmd_repomanage repomanage
Expand Down
4 changes: 2 additions & 2 deletions completions/_slackpkg
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _comp_cmd_slackpkg()
return
;;
remove)
_comp_compgen filedir
_comp_compgen_filedir
COMPREPLY+=($(compgen -W 'a ap d e f k kde kdei l n t tcl x
xap xfce y' -- "$cur"))
COMPREPLY+=($(
Expand All @@ -84,7 +84,7 @@ _comp_cmd_slackpkg()
return
;;
install | reinstall | upgrade | blacklist | download)
_comp_compgen filedir
_comp_compgen_filedir
COMPREPLY+=($(compgen -W 'a ap d e f k kde kdei l n t tcl x
xap xfce y' -- "$cur"))
COMPREPLY+=($(cut -f 6 -d\ "${WORKDIR}/pkglist" 2>/dev/null |
Expand Down
6 changes: 3 additions & 3 deletions completions/_svn
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ _comp_cmd_svn()

case $prev in
--config-dir)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
-F | --file | --targets)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
--encoding)
Expand Down Expand Up @@ -199,7 +199,7 @@ _comp_cmd_svn()
if [[ $command == @(help|[h?]) ]]; then
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
else
_comp_compgen filedir
_comp_compgen_filedir
fi
fi
fi
Expand Down
4 changes: 2 additions & 2 deletions completions/_svnadmin
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _comp_cmd_svnadmin()
else
case $prev in
--config-dir)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
--fs-type)
Expand Down Expand Up @@ -67,7 +67,7 @@ _comp_cmd_svnadmin()
if [[ $command == @(help|[h?]) ]]; then
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
else
_comp_compgen filedir
_comp_compgen_filedir
fi
fi
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/_svnlook
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _comp_cmd_svnlook()
if [[ $command == @(help|[h?]) ]]; then
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
else
_comp_compgen filedir
_comp_compgen_filedir
fi
fi
fi
Expand Down
2 changes: 1 addition & 1 deletion completions/_udevadm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _comp_cmd_udevadm()
return
;;
--device-id-of-file | --exit-if-exists)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
--action)
Expand Down
10 changes: 5 additions & 5 deletions completions/_xm
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ _comp_cmd_xm()
esac
;;
restore | dry-run | vnet-create)
_comp_compgen filedir
_comp_compgen_filedir
;;
save)
_count_args
Expand All @@ -107,7 +107,7 @@ _comp_cmd_xm()
_comp_cmd_xm__domain_names
;;
3)
_comp_compgen filedir
_comp_compgen_filedir
;;
esac
;;
Expand Down Expand Up @@ -186,19 +186,19 @@ _comp_cmd_xm()
esac
;;
create)
_comp_compgen filedir
_comp_compgen_filedir
COMPREPLY+=(
$(compgen -W '$(command ls /etc/xen 2>/dev/null)' \
-- "$cur"))
;;
new)
case $prev in
-f | -F | --defconfig | --config)
_comp_compgen filedir
_comp_compgen_filedir
return
;;
--path)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
esac
Expand Down
8 changes: 4 additions & 4 deletions completions/_yum
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _comp_cmd_yum()
# TODO: install|update|upgrade should not match *src.rpm
if [[ $cur == @(*/|[.~])* &&
$special == @(deplist|install|update|upgrade) ]]; then
_comp_compgen filedir rpm
_comp_compgen_filedir rpm
return
fi
case $special in
Expand Down Expand Up @@ -94,16 +94,16 @@ _comp_cmd_yum()
;;
localinstall | localupdate)
# TODO: should not match *src.rpm
_comp_compgen filedir rpm
_comp_compgen_filedir rpm
;;
-d | -e)
COMPREPLY=($(compgen -W '{0..10}' -- "$cur"))
;;
-c)
_comp_compgen filedir
_comp_compgen_filedir
;;
--installroot)
_comp_compgen filedir -d
_comp_compgen_filedir -d
;;
--enablerepo)
COMPREPLY=($(compgen -W '$(_yum_repolist disabled)' -- "$cur"))
Expand Down
6 changes: 3 additions & 3 deletions completions/a2x
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ _comp_cmd_a2x()
return
;;
--destination-dir | --icons-dir | -${noargopts}D)
_comp_compgen filedir -d
_comp_compgen_filedir -d
return
;;
--doctype | -${noargopts}d)
_comp_xfunc asciidoc doctype
return
;;
--stylesheet)
_comp_compgen filedir css
_comp_compgen_filedir css
return
;;
esac
Expand All @@ -34,7 +34,7 @@ _comp_cmd_a2x()
return
fi

_comp_compgen filedir
_comp_compgen_filedir
} &&
complete -F _comp_cmd_a2x a2x

Expand Down
6 changes: 3 additions & 3 deletions completions/abook
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ _comp_cmd_abook()
;;
--infile)
COMPREPLY=($(compgen -W stdin -- "$cur"))
_comp_compgen filedir
_comp_compgen_filedir
;;
--outfile)
COMPREPLY=($(compgen -W stdout -- "$cur"))
_comp_compgen filedir
_comp_compgen_filedir
;;
--config | --datafile)
_comp_compgen filedir
_comp_compgen_filedir
;;
esac
} &&
Expand Down
Loading