From 5baebf81d3ede436d63ae38e464dacaa0ad4b486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 13 Oct 2011 21:15:05 +0300 Subject: [PATCH] Add _xfunc for loading and calling functions on demand, use it in apt-get, cvsps, rsync, and sshfs. --- bash_completion | 17 +++++++++++++++++ completions/apt-get | 2 +- completions/cvsps | 4 ++-- completions/rsync | 23 ++++++++++------------- completions/sshfs | 6 +++--- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/bash_completion b/bash_completion index 1a90e9b3..a8b8ff9a 100644 --- a/bash_completion +++ b/bash_completion @@ -1834,6 +1834,23 @@ _completion_loader() } && complete -D -F _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() +{ + set -- "$@" + local srcfile=$1 + shift + declare -F $1 &>/dev/null || { + local compdir=./completions + [[ $BASH_SOURCE == */* ]] && compdir="${BASH_SOURCE%/*}/completions" + . "$compdir/$srcfile" + } + "$@" +} + # source compat completion directory definitions if [[ -d $BASH_COMPLETION_COMPAT_DIR && -r $BASH_COMPLETION_COMPAT_DIR && \ -x $BASH_COMPLETION_COMPAT_DIR ]]; then diff --git a/completions/apt-get b/completions/apt-get index 1f6b726c..25e256a8 100644 --- a/completions/apt-get +++ b/completions/apt-get @@ -20,7 +20,7 @@ _apt_get() COMPREPLY=( $( _comp_dpkg_installed_packages $cur ) ) else # assume RPM based - _rpm_installed_packages + _xfunc rpm _rpm_installed_packages fi return 0 ;; diff --git a/completions/cvsps b/completions/cvsps index e74e7c68..1821a1a1 100644 --- a/completions/cvsps +++ b/completions/cvsps @@ -42,7 +42,7 @@ _cvsps() return 0 ;; --root) - declare -F _cvs_roots &>/dev/null && _cvs_roots + _xfunc cvs _cvs_roots return 0 ;; esac @@ -50,7 +50,7 @@ _cvsps() if [[ "$cur" == -* ]] ; then COMPREPLY=( $( compgen -W '$( _parse_help "$1" -h )' -- "$cur" ) ) else - declare -F _cvs_roots &>/dev/null && _cvs_roots + _xfunc cvs _cvs_roots fi } && complete -F _cvsps cvsps diff --git a/completions/rsync b/completions/rsync index 3befcb8a..aec6ea3c 100644 --- a/completions/rsync +++ b/completions/rsync @@ -66,22 +66,19 @@ _rsync() [[ $COMPREPLY == *= ]] || compopt +o nospace ;; *:*) - if declare -F _scp_remote_files &>/dev/null; then - # find which remote shell is used - local i shell=ssh - for (( i=1; i < cword; i++ )); do - if [[ "${words[i]}" == -@(e|-rsh) ]]; then - shell=${words[i+1]} - break - fi - done - [ "$shell" = ssh ] && _scp_remote_files - fi + # find which remote shell is used + local i shell=ssh + for (( i=1; i < cword; i++ )); do + if [[ "${words[i]}" == -@(e|-rsh) ]]; then + shell=${words[i+1]} + break + fi + done + [ "$shell" = ssh ] && _xfunc ssh _scp_remote_files ;; *) _known_hosts_real -c -a "$cur" - declare -F _scp_local_files &>/dev/null && \ - _scp_local_files || _filedir + _xfunc ssh _scp_local_files ;; esac diff --git a/completions/sshfs b/completions/sshfs index b557486e..180647ee 100644 --- a/completions/sshfs +++ b/completions/sshfs @@ -9,8 +9,8 @@ _sshfs() _expand || return 0 - if [[ "$cur" == *:* ]] && declare -F _scp_remote_files &>/dev/null ; then - _scp_remote_files -d + if [[ "$cur" == *:* ]]; then + _xfunc ssh _scp_remote_files -d # unlike scp and rsync, sshfs works with 1 backslash instead of 3 COMPREPLY=( "${COMPREPLY[@]//\\\\\\/\\}" ) return 0 @@ -18,7 +18,7 @@ _sshfs() [[ "$cur" == @(*/|[.~])* ]] || _known_hosts_real -c -a "$cur" - declare -F _scp_local_files &>/dev/null && _scp_local_files -d + _xfunc ssh _scp_local_files -d return 0 } &&