Skip to content

Commit eb94fb1

Browse files
authored
Merge pull request #734 from akinomyoga/_comp_xfunc
feat: add `_comp_xfunc`
2 parents a2db7ad + 7978941 commit eb94fb1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+297
-132
lines changed

bash_completion

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ _comp__init_blacklist_glob='@(acroread.sh)'
4444
# Turn on extended globbing and programmable completion
4545
shopt -s extglob progcomp
4646

47+
# Declare a compatibility function name
48+
# @param $1 Old function name
49+
# @param $2 New function name
50+
_comp_deprecate_func()
51+
{
52+
if [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
53+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$1: invalid function name '$1'" >&2
54+
return 2
55+
elif [[ $2 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
56+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "\$2: invalid function name '$2'" >&2
57+
return 2
58+
fi
59+
eval -- "$1() { $2 \"\$@\"; }"
60+
}
61+
4762
# A lot of the following one-liners were taken directly from the
4863
# completion examples provided with the bash 2.04 source distribution
4964

@@ -2429,16 +2444,21 @@ _completion_loader()
24292444
# Function for loading and calling functions from dynamically loaded
24302445
# completion files that may not have been sourced yet.
24312446
# @param $1 completion file to load function from in case it is missing
2432-
# @param $2... function and its arguments
2433-
_xfunc()
2447+
# @param $2 the xfunc name. When it does not start with `_',
2448+
# `_comp_xfunc_${1//[^a-zA-Z0-9_]/_}_$2' is used for the actual name of the
2449+
# shell function.
2450+
# @param $3... if any, specifies the arguments that are passed to the xfunc.
2451+
_comp_xfunc()
24342452
{
2435-
set -- "$@"
2436-
local srcfile=$1
2437-
shift
2438-
declare -F $1 &>/dev/null || __load_completion "$srcfile"
2439-
"$@"
2453+
local xfunc_name=$2
2454+
[[ $xfunc_name == _* ]] ||
2455+
xfunc_name=_comp_xfunc_${1//[^a-zA-Z0-9_]/_}_$xfunc_name
2456+
declare -F "$xfunc_name" &>/dev/null || __load_completion "$1"
2457+
"$xfunc_name" "${@:3}"
24402458
}
24412459
2460+
_comp_deprecate_func _xfunc _comp_xfunc
2461+
24422462
# source compat completion directory definitions
24432463
_comp__init_compat_dir=${BASH_COMPLETION_COMPAT_DIR:-/etc/bash_completion.d}
24442464
if [[ -d $_comp__init_compat_dir && -r $_comp__init_compat_dir && -x $_comp__init_compat_dir ]]; then

completions/_svn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _svn()
3434
return
3535
;;
3636
--encoding)
37-
_xfunc iconv _iconv_charsets
37+
_comp_xfunc iconv charsets
3838
return
3939
;;
4040
--editor-cmd | --diff-cmd | --diff3-cmd)

completions/_yum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ _yum()
6868
return
6969
;;
7070
remove | erase)
71-
# _rpm_installed_packages is not arch-qualified
71+
# _comp_xfunc_rpm_installed_packages is not arch-qualified
7272
_yum_list installed
7373
return
7474
;;

completions/a2x

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _a2x()
1515
return
1616
;;
1717
--doctype | -!(-*)d)
18-
_xfunc asciidoc _asciidoc_doctype
18+
_comp_xfunc asciidoc doctype
1919
return
2020
;;
2121
--stylesheet)

completions/add_members

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ _add_members()
2222
COMPREPLY=($(compgen -W '--regular-members-file --digest-members-file
2323
--welcome-msg --admin-notify --help' -- "$cur"))
2424
else
25-
_xfunc list_lists _mailman_lists
25+
_comp_xfunc list_lists mailman_lists
2626
fi
2727

2828
} &&

completions/apt-build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ _apt_build()
1616
if [[ -v special ]]; then
1717
case $special in
1818
install | source | info)
19-
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages))
19+
COMPREPLY=($(_comp_xfunc apt-cache packages))
2020
;;
2121
remove)
2222
COMPREPLY=(
23-
$(_xfunc dpkg _comp_dpkg_installed_packages "$cur"))
23+
$(_comp_xfunc dpkg installed_packages "$cur"))
2424
;;
2525
esac
2626
return

completions/apt-cache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
# Debian apt-cache(8) completion -*- shell-script -*-
22

33
# List APT binary packages
4-
_apt_cache_packages()
4+
_comp_xfunc_apt_cache_packages()
55
{
66
apt-cache --no-generate pkgnames "$cur" 2>/dev/null || :
77
}
88

99
# List APT source packages
10-
_apt_cache_sources()
10+
_comp_xfunc_apt_cache_sources()
1111
{
1212
compgen -W "$(apt-cache dumpavail |
1313
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$1"
1414
}
1515

1616
# List APT source packages
17-
_apt_cache_src_packages()
17+
_comp_xfunc_apt_cache_src_packages()
1818
{
19-
compgen -W '$(_apt_cache_sources "$cur")' -- "$cur"
19+
compgen -W '$(_comp_xfunc_apt_cache_sources "$cur")' -- "$cur"
2020
}
2121

22+
_comp_deprecate_func _apt_cache_packages _comp_xfunc_apt_cache_packages
23+
_comp_deprecate_func _apt_cache_sources _comp_xfunc_apt_cache_sources
24+
_comp_deprecate_func _apt_cache_src_packages _comp_xfunc_apt_cache_src_packages
25+
2226
_apt_cache()
2327
{
2428
local cur prev words cword
@@ -41,11 +45,11 @@ _apt_cache()
4145
;;
4246

4347
showsrc)
44-
COMPREPLY=($(_apt_cache_sources "$cur"))
48+
COMPREPLY=($(_comp_xfunc_apt_cache_sources "$cur"))
4549
;;
4650

4751
*)
48-
COMPREPLY=($(_apt_cache_packages))
52+
COMPREPLY=($(_comp_xfunc_apt_cache_packages))
4953
;;
5054

5155
esac

completions/apt-get

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# Debian apt-get(8) completion -*- shell-script -*-
22

3-
_comp_cmd_apt_get_installed_packages()
3+
_comp_xfunc_apt_get_installed_packages()
44
{
55
if [[ -f /etc/debian_version ]]; then
66
# Debian system
77
COMPREPLY=($(
8-
_xfunc dpkg _comp_dpkg_installed_packages $cur
8+
_comp_xfunc dpkg installed_packages $cur
99
))
1010
else
1111
# assume RPM based
12-
_xfunc rpm _rpm_installed_packages
12+
_comp_xfunc rpm installed_packages
1313
fi
1414
}
1515

16+
_comp_deprecate_func _comp_cmd_apt_get_installed_packages _comp_xfunc_apt_get_installed_packages
17+
1618
_apt_get()
1719
{
1820
local cur prev words cword package
@@ -29,10 +31,10 @@ _apt_get()
2931
if [[ -v special ]]; then
3032
case $special in
3133
remove | auto?(-)remove | purge)
32-
_comp_cmd_apt_get_installed_packages
34+
_comp_xfunc_apt_get_installed_packages
3335
;;
3436
source)
35-
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages)
37+
COMPREPLY=($(_comp_xfunc apt-cache packages)
3638
$(compgen -W "$(apt-cache dumpavail |
3739
awk '$1 == "Source:" { print $2 }' | sort -u)" -- "$cur"))
3840
;;
@@ -59,7 +61,7 @@ _apt_get()
5961
[[ $cur != */* ]] || return
6062
;;&
6163
*)
62-
COMPREPLY+=($(_xfunc apt-cache _apt_cache_packages))
64+
COMPREPLY+=($(_comp_xfunc apt-cache packages))
6365
;;
6466
esac
6567
return

completions/apt-mark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _comp_cmd_apt_mark()
2727
return
2828
;;
2929
*)
30-
_xfunc apt-get _comp_cmd_apt_get_installed_packages
30+
_comp_xfunc apt-get installed_packages
3131
;;
3232
esac
3333
return

completions/aptitude

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ _aptitude()
3131
install | hold | markauto | unmarkauto | dist-upgrade | full-upgrade | \
3232
safe-upgrade | download | show | changelog | why | why-not | build-dep | \
3333
add-user-tag | remove-user-tag | versions)
34-
COMPREPLY=($(_xfunc apt-cache _apt_cache_packages))
34+
COMPREPLY=($(_comp_xfunc apt-cache packages))
3535
return
3636
;;
3737
purge | remove | reinstall | forbid-version)
3838
COMPREPLY=(
39-
$(_xfunc dpkg _comp_dpkg_installed_packages "$cur"))
39+
$(_comp_xfunc dpkg installed_packages "$cur"))
4040
return
4141
;;
4242
unhold)

0 commit comments

Comments
 (0)