From d89c9e94bac3f7a493b18b96a2f2aa530fa5f116 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 5 Dec 2014 00:15:14 +0900 Subject: [PATCH] Handle dynamically loaded completion functions (#107 / #79) --- fzf-completion.bash | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/fzf-completion.bash b/fzf-completion.bash index ebee4d2..61797b1 100644 --- a/fzf-completion.bash +++ b/fzf-completion.bash @@ -53,8 +53,25 @@ _fzf_opts_completion() { return 0 } +_fzf_handle_dynamic_completion() { + local cmd orig ret + cmd="$1" + shift + + orig=$(eval "echo \$_fzf_orig_completion_$cmd") + if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then + $orig "$@" + elif [ -n "$_fzf_completion_loader" ]; then + _completion_loader "$@" + ret=$? + eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter) + source $BASH_SOURCE + return $ret + fi +} + _fzf_path_completion() { - local cur base dir leftover matches trigger cmd orig + local cur base dir leftover matches trigger cmd cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g') COMPREPLY=() trigger=${FZF_COMPLETION_TRIGGER:-**} @@ -88,13 +105,12 @@ _fzf_path_completion() { else shift shift - orig=$(eval "echo \$_fzf_orig_completion_$cmd") - [ -n "$orig" ] && type "$orig" > /dev/null 2>&1 && $orig "$@" + _fzf_handle_dynamic_completion "$cmd" "$@" fi } _fzf_list_completion() { - local cur selected trigger cmd src ret + local cur selected trigger cmd src read -r src cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g') trigger=${FZF_COMPLETION_TRIGGER:-**} @@ -113,16 +129,7 @@ _fzf_list_completion() { fi else shift - orig=$(eval "echo \$_fzf_orig_completion_$cmd") - if [ -n "$orig" ] && type "$orig" > /dev/null; then - $orig "$@" - elif [ -n "$_fzf_completion_loader" ]; then - _completion_loader "$@" - ret=$? - eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter) - source $BASH_SOURCE - return $ret - fi + _fzf_handle_dynamic_completion "$cmd" "$@" fi }