Skip to content

Commit

Permalink
Merge pull request #734 from akinomyoga/_comp_xfunc
Browse files Browse the repository at this point in the history
feat: add `_comp_xfunc`
  • Loading branch information
scop authored Apr 14, 2022
2 parents a2db7ad + 7978941 commit eb94fb1
Show file tree
Hide file tree
Showing 61 changed files with 297 additions and 132 deletions.
34 changes: 27 additions & 7 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ _comp__init_blacklist_glob='@(acroread.sh)'
# Turn on extended globbing and programmable completion
shopt -s extglob progcomp

# Declare a compatibility function name
# @param $1 Old function name
# @param $2 New function name
_comp_deprecate_func()
{
if [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$1: invalid function name '$1'" >&2
return 2
elif [[ $2 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$2: invalid function name '$2'" >&2
return 2
fi
eval -- "$1() { $2 \"\$@\"; }"
}

# A lot of the following one-liners were taken directly from the
# completion examples provided with the bash 2.04 source distribution

Expand Down Expand Up @@ -2429,16 +2444,21 @@ _completion_loader()
# Function for loading and calling functions from dynamically loaded
# completion files that may not have been sourced yet.
# @param $1 completion file to load function from in case it is missing
# @param $2... function and its arguments
_xfunc()
# @param $2 the xfunc name. When it does not start with `_',
# `_comp_xfunc_${1//[^a-zA-Z0-9_]/_}_$2' is used for the actual name of the
# shell function.
# @param $3... if any, specifies the arguments that are passed to the xfunc.
_comp_xfunc()
{
set -- "$@"
local srcfile=$1
shift
declare -F $1 &>/dev/null || __load_completion "$srcfile"
"$@"
local xfunc_name=$2
[[ $xfunc_name == _* ]] ||
xfunc_name=_comp_xfunc_${1//[^a-zA-Z0-9_]/_}_$xfunc_name
declare -F "$xfunc_name" &>/dev/null || __load_completion "$1"
"$xfunc_name" "${@:3}"
}
_comp_deprecate_func _xfunc _comp_xfunc
# source compat completion directory definitions
_comp__init_compat_dir=${BASH_COMPLETION_COMPAT_DIR:-/etc/bash_completion.d}
if [[ -d $_comp__init_compat_dir && -r $_comp__init_compat_dir && -x $_comp__init_compat_dir ]]; then
Expand Down
2 changes: 1 addition & 1 deletion completions/_svn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _svn()
return
;;
--encoding)
_xfunc iconv _iconv_charsets
_comp_xfunc iconv charsets
return
;;
--editor-cmd | --diff-cmd | --diff3-cmd)
Expand Down
2 changes: 1 addition & 1 deletion completions/_yum
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ _yum()
return
;;
remove | erase)
# _rpm_installed_packages is not arch-qualified
# _comp_xfunc_rpm_installed_packages is not arch-qualified
_yum_list installed
return
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/a2x
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _a2x()
return
;;
--doctype | -!(-*)d)
_xfunc asciidoc _asciidoc_doctype
_comp_xfunc asciidoc doctype
return
;;
--stylesheet)
Expand Down
2 changes: 1 addition & 1 deletion completions/add_members
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ _add_members()
COMPREPLY=($(compgen -W '--regular-members-file --digest-members-file
--welcome-msg --admin-notify --help' -- "$cur"))
else
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
fi

} &&
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-build
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ _apt_build()
if [[ -v special ]]; then
case $special in
install | source | info)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages))
COMPREPLY=($(_comp_xfunc apt-cache packages))
;;
remove)
COMPREPLY=(
$(_xfunc dpkg _comp_dpkg_installed_packages "$cur"))
$(_comp_xfunc dpkg installed_packages "$cur"))
;;
esac
return
Expand Down
16 changes: 10 additions & 6 deletions completions/apt-cache
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
# Debian apt-cache(8) completion -*- shell-script -*-

# List APT binary packages
_apt_cache_packages()
_comp_xfunc_apt_cache_packages()
{
apt-cache --no-generate pkgnames "$cur" 2>/dev/null || :
}

# List APT source packages
_apt_cache_sources()
_comp_xfunc_apt_cache_sources()
{
compgen -W "$(apt-cache dumpavail |
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$1"
}

# List APT source packages
_apt_cache_src_packages()
_comp_xfunc_apt_cache_src_packages()
{
compgen -W '$(_apt_cache_sources "$cur")' -- "$cur"
compgen -W '$(_comp_xfunc_apt_cache_sources "$cur")' -- "$cur"
}

_comp_deprecate_func _apt_cache_packages _comp_xfunc_apt_cache_packages
_comp_deprecate_func _apt_cache_sources _comp_xfunc_apt_cache_sources
_comp_deprecate_func _apt_cache_src_packages _comp_xfunc_apt_cache_src_packages

_apt_cache()
{
local cur prev words cword
Expand All @@ -41,11 +45,11 @@ _apt_cache()
;;

showsrc)
COMPREPLY=($(_apt_cache_sources "$cur"))
COMPREPLY=($(_comp_xfunc_apt_cache_sources "$cur"))
;;

*)
COMPREPLY=($(_apt_cache_packages))
COMPREPLY=($(_comp_xfunc_apt_cache_packages))
;;

esac
Expand Down
14 changes: 8 additions & 6 deletions completions/apt-get
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Debian apt-get(8) completion -*- shell-script -*-

_comp_cmd_apt_get_installed_packages()
_comp_xfunc_apt_get_installed_packages()
{
if [[ -f /etc/debian_version ]]; then
# Debian system
COMPREPLY=($(
_xfunc dpkg _comp_dpkg_installed_packages $cur
_comp_xfunc dpkg installed_packages $cur
))
else
# assume RPM based
_xfunc rpm _rpm_installed_packages
_comp_xfunc rpm installed_packages
fi
}

_comp_deprecate_func _comp_cmd_apt_get_installed_packages _comp_xfunc_apt_get_installed_packages

_apt_get()
{
local cur prev words cword package
Expand All @@ -29,10 +31,10 @@ _apt_get()
if [[ -v special ]]; then
case $special in
remove | auto?(-)remove | purge)
_comp_cmd_apt_get_installed_packages
_comp_xfunc_apt_get_installed_packages
;;
source)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages)
COMPREPLY=($(_comp_xfunc apt-cache packages)
$(compgen -W "$(apt-cache dumpavail |
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$cur"))
;;
Expand All @@ -59,7 +61,7 @@ _apt_get()
[[ $cur != */* ]] || return
;;&
*)
COMPREPLY+=($(_xfunc apt-cache _apt_cache_packages))
COMPREPLY+=($(_comp_xfunc apt-cache packages))
;;
esac
return
Expand Down
2 changes: 1 addition & 1 deletion completions/apt-mark
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _comp_cmd_apt_mark()
return
;;
*)
_xfunc apt-get _comp_cmd_apt_get_installed_packages
_comp_xfunc apt-get installed_packages
;;
esac
return
Expand Down
4 changes: 2 additions & 2 deletions completions/aptitude
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ _aptitude()
install | hold | markauto | unmarkauto | dist-upgrade | full-upgrade | \
safe-upgrade | download | show | changelog | why | why-not | build-dep | \
add-user-tag | remove-user-tag | versions)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages))
COMPREPLY=($(_comp_xfunc apt-cache packages))
return
;;
purge | remove | reinstall | forbid-version)
COMPREPLY=(
$(_xfunc dpkg _comp_dpkg_installed_packages "$cur"))
$(_comp_xfunc dpkg installed_packages "$cur"))
return
;;
unhold)
Expand Down
2 changes: 1 addition & 1 deletion completions/arch
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _have mailmanctl &&
done
case $args in
1)
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
;;
2)
_filedir
Expand Down
6 changes: 4 additions & 2 deletions completions/asciidoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# asciidoc(1) completion -*- shell-script -*-

_asciidoc_doctype()
_comp_xfunc_asciidoc_doctype()
{
COMPREPLY+=($(compgen -W 'article book manpage' -- "$cur"))
}

_comp_deprecate_func _asciidoc_doctype _comp_xfunc_asciidoc_doctype

_asciidoc()
{
local cur prev words cword split
Expand All @@ -23,7 +25,7 @@ _asciidoc()
return
;;
--doctype | -!(-*)d)
_asciidoc_doctype
_comp_xfunc_asciidoc_doctype
return
;;
--help | -!(-*)h)
Expand Down
10 changes: 5 additions & 5 deletions completions/bts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _cached_bugs()
_src_packages_with_prefix()
{
ppn=${cur:4} # partial package name, after stripping "src:"
compgen -P "src:" -W '$(_xfunc apt-cache _apt_cache_sources "$ppn")' \
compgen -P "src:" -W '$(_comp_xfunc apt-cache sources "$ppn")' \
-- "$ppn"
}

Expand Down Expand Up @@ -58,17 +58,17 @@ _bts()
return
;;
package)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages))
COMPREPLY=($(_comp_xfunc apt-cache packages))
return
;;
cache)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages)
COMPREPLY=($(_comp_xfunc apt-cache packages)
$(_src_packages_with_prefix)
$(compgen -W 'from: release-critical RC' -- "$cur"))
return
;;
cleancache)
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages)
COMPREPLY=($(_comp_xfunc apt-cache packages)
$(_src_packages_with_prefix)
$(compgen -W 'from: tag: usertag: ALL' -- "$cur"))
return
Expand All @@ -83,7 +83,7 @@ _bts()
# COMP_WORDS would be: "bts cleancache src : <partial_pkg_name>"
pos=$((COMP_CWORD - 2))
if [[ $pos -gt 0 && ${COMP_WORDS[pos]} == "src" ]]; then
COMPREPLY=($(_xfunc apt-cache _apt_cache_src_packages))
COMPREPLY=($(_comp_xfunc apt-cache src_packages))
return
fi
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/change_pw
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _change_pw()

case $prev in
-l | --listname)
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/check_db
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _check_db()
if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '--all --verbose --help' -- "$cur"))
else
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
fi

} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/clone_member
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _clone_member()

case $prev in
-l | --listname)
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/config_list
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _config_list()
COMPREPLY=($(compgen -W '--inputfile --outputfile --checkonly
--verbose --help' -- "$cur"))
else
_xfunc list_lists _mailman_lists
_comp_xfunc list_lists mailman_lists
fi

} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/curl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ _curl()
return
;;
--pubkey)
_xfunc ssh _ssh_identityfile pub
_comp_xfunc ssh identityfile pub
return
;;
--request | -!(-*)X)
Expand Down
6 changes: 4 additions & 2 deletions completions/cvs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _cvs_kflags()
COMPREPLY=($(compgen -W 'kv kvl k o b v' -- "$cur"))
}

_cvs_roots()
_comp_xfunc_cvs_roots()
{
local -a cvsroots
[[ -v CVSROOT ]] && cvsroots=("$CVSROOT")
Expand All @@ -45,6 +45,8 @@ _cvs_roots()
__ltrim_colon_completions "$cur"
}

_comp_deprecate_func _cvs_roots _comp_xfunc_cvs_roots

_cvs()
{
local cur prev words cword
Expand Down Expand Up @@ -273,7 +275,7 @@ _cvs()
fi
;;
cvsroot)
_cvs_roots
_comp_xfunc_cvs_roots
;;
diff | log | status)
if [[ $cur == -* ]]; then
Expand Down
4 changes: 2 additions & 2 deletions completions/cvsps
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ _cvsps()
return
;;
--root)
_xfunc cvs _cvs_roots
_comp_xfunc cvs roots
return
;;
esac

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help "$1" -h)' -- "$cur"))
else
_xfunc cvs _cvs_roots
_comp_xfunc cvs roots
fi
} &&
complete -F _cvsps cvsps
Expand Down
Loading

0 comments on commit eb94fb1

Please sign in to comment.