mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-25 22:27:41 +00:00
[completion] Revamp completion API
* _fzf_complete is the helper function for custom completion * _fzf_complete FZF_OPTS ARGS * Reads the output of the source command instead of the command string * In zsh, you can use pipe to feed the data into the function, but it's not possible in bash as by doing so COMPREPLY is set from the subshell and thus nullified * Change the naming convention for consistency: * _fzf_complete_COMMAND e.g. # pass completion suggested by @d4ndo (#362) _fzf_complete_pass() { _fzf_complete '+m' "$@" < <( local pwdir=${PASSWORD_STORE_DIR-~/.password-store/} local stringsize="${#pwdir}" find "$pwdir" -name "*.gpg" -print | cut -c "$((stringsize + 1))"- | sed -e 's/\(.*\)\.gpg/\1/' ) } # Only in bash complete -F _fzf_complete_pass -o default -o bashdefault pass
This commit is contained in:
parent
659f49a09a
commit
eee45a9578
@ -94,7 +94,7 @@ _fzf_handle_dynamic_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local cur base dir leftover matches trigger cmd fzf
|
local cur base dir leftover matches trigger cmd fzf
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
@ -135,20 +135,29 @@ _fzf_path_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_list_completion() {
|
_fzf_feed_fifo() (
|
||||||
local cur selected trigger cmd src fzf
|
rm -f "$fifo"
|
||||||
|
mkfifo "$fifo"
|
||||||
|
cat <&0 > "$fifo" &
|
||||||
|
)
|
||||||
|
|
||||||
|
_fzf_complete() {
|
||||||
|
local fifo cur selected trigger cmd fzf
|
||||||
|
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
read -r src
|
|
||||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||||
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
trigger=${FZF_COMPLETION_TRIGGER-'**'}
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
if [[ ${cur} == *"$trigger" ]]; then
|
if [[ ${cur} == *"$trigger" ]]; then
|
||||||
cur=${cur:0:${#cur}-${#trigger}}
|
cur=${cur:0:${#cur}-${#trigger}}
|
||||||
|
|
||||||
|
_fzf_feed_fifo "$fifo"
|
||||||
tput sc
|
tput sc
|
||||||
selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
selected=$(eval "cat '$fifo' | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
||||||
selected=${selected% }
|
selected=${selected% } # Strip trailing space not to repeat "-o nospace"
|
||||||
tput rc
|
tput rc
|
||||||
|
rm -f "$fifo"
|
||||||
|
|
||||||
if [ -n "$selected" ]; then
|
if [ -n "$selected" ]; then
|
||||||
COMPREPLY=("$selected")
|
COMPREPLY=("$selected")
|
||||||
@ -160,25 +169,25 @@ _fzf_list_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_path_completion() {
|
||||||
_fzf_path_completion \
|
__fzf_generic_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||||
"-m" "" "$@"
|
"-m" "" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_file_completion() {
|
_fzf_file_completion() {
|
||||||
_fzf_path_completion \
|
__fzf_generic_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
||||||
"-m" "" "$@"
|
"-m" "" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
_fzf_path_completion \
|
__fzf_generic_path_completion \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||||
"" "/" "$@"
|
"" "/" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_kill_completion() {
|
_fzf_complete_kill() {
|
||||||
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||||
|
|
||||||
local selected fzf
|
local selected fzf
|
||||||
@ -193,28 +202,37 @@ _fzf_kill_completion() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
||||||
EOF
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
||||||
EOF
|
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
||||||
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_env_var_completion() {
|
_fzf_complete_unset() {
|
||||||
_fzf_list_completion '-m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_alias_completion() {
|
_fzf_complete_export() {
|
||||||
_fzf_list_completion '-m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
_fzf_complete_unalias() {
|
||||||
|
_fzf_complete '-m' "$@" < <(
|
||||||
alias | sed 's/=.*//' | sed 's/.* //'
|
alias | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
# fzf options
|
# fzf options
|
||||||
@ -257,19 +275,19 @@ done
|
|||||||
|
|
||||||
# Anything
|
# Anything
|
||||||
for cmd in $a_cmds; do
|
for cmd in $a_cmds; do
|
||||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
complete -F _fzf_path_completion -o default -o bashdefault $cmd
|
||||||
done
|
done
|
||||||
|
|
||||||
# Kill completion
|
# Kill completion
|
||||||
complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill
|
||||||
|
|
||||||
# Host completion
|
# Host completion
|
||||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
complete -F _fzf_complete_ssh -o default -o bashdefault ssh
|
||||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
complete -F _fzf_complete_telnet -o default -o bashdefault telnet
|
||||||
|
|
||||||
# Environment variables / Aliases
|
# Environment variables / Aliases
|
||||||
complete -F _fzf_env_var_completion -o default -o bashdefault unset
|
complete -F _fzf_complete_unset -o default -o bashdefault unset
|
||||||
complete -F _fzf_env_var_completion -o default -o bashdefault export
|
complete -F _fzf_complete_export -o default -o bashdefault export
|
||||||
complete -F _fzf_alias_completion -o default -o bashdefault unalias
|
complete -F _fzf_complete_unalias -o default -o bashdefault unalias
|
||||||
|
|
||||||
unset cmd d_cmds f_cmds a_cmds x_cmds
|
unset cmd d_cmds f_cmds a_cmds x_cmds
|
||||||
|
@ -10,8 +10,9 @@
|
|||||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||||
|
|
||||||
_fzf_path_completion() {
|
__fzf_generic_path_completion() {
|
||||||
local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm
|
local base lbuf find_opts fzf_opts suffix tail fzf dir leftover matches nnm
|
||||||
|
# (Q) flag removes a quoting level: "foo\ bar" => "foo bar"
|
||||||
base=${(Q)1}
|
base=${(Q)1}
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
find_opts=$3
|
find_opts=$3
|
||||||
@ -47,60 +48,71 @@ _fzf_path_completion() {
|
|||||||
[ -n "$nnm" ] && unsetopt nonomatch
|
[ -n "$nnm" ] && unsetopt nonomatch
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_all_completion() {
|
_fzf_path_completion() {
|
||||||
_fzf_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||||
"-m" "" " "
|
"-m" "" " "
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_dir_completion() {
|
_fzf_dir_completion() {
|
||||||
_fzf_path_completion "$1" "$2" \
|
__fzf_generic_path_completion "$1" "$2" \
|
||||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||||
"" "/" ""
|
"" "/" ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_list_completion() {
|
_fzf_feed_fifo() (
|
||||||
local fzf_opts lbuf src fzf matches
|
rm -f "$fifo"
|
||||||
|
mkfifo "$fifo"
|
||||||
|
cat <&0 > "$fifo" &
|
||||||
|
)
|
||||||
|
|
||||||
|
_fzf_complete() {
|
||||||
|
local fifo fzf_opts lbuf fzf matches
|
||||||
|
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
|
||||||
fzf_opts=$1
|
fzf_opts=$1
|
||||||
lbuf=$2
|
lbuf=$2
|
||||||
read -r src
|
|
||||||
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
|
||||||
|
|
||||||
matches=$(eval "$src" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "$prefix")
|
_fzf_feed_fifo "$fifo"
|
||||||
|
matches=$(cat "$fifo" | ${=fzf} ${=FZF_COMPLETION_OPTS} ${=fzf_opts} -q "${(Q)prefix}" | tr '\n' ' ')
|
||||||
if [ -n "$matches" ]; then
|
if [ -n "$matches" ]; then
|
||||||
LBUFFER="$lbuf$matches "
|
LBUFFER="$lbuf$matches"
|
||||||
fi
|
fi
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
rm -f "$fifo"
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_telnet_completion() {
|
_fzf_complete_telnet() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
|
||||||
EOF
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_ssh_completion() {
|
_fzf_complete_ssh() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
|
||||||
EOF
|
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
|
||||||
|
awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_export_completion() {
|
_fzf_complete_export() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_unset_completion() {
|
_fzf_complete_unset() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '-m' "$@" < <(
|
||||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
_fzf_unalias_completion() {
|
_fzf_complete_unalias() {
|
||||||
_fzf_list_completion '+m' "$@" << "EOF"
|
_fzf_complete '+m' "$@" < <(
|
||||||
alias | sed 's/=.*//'
|
alias | sed 's/=.*//'
|
||||||
EOF
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fzf-completion() {
|
fzf-completion() {
|
||||||
@ -140,12 +152,12 @@ fzf-completion() {
|
|||||||
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
|
||||||
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
[ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
|
||||||
|
|
||||||
if eval "type _fzf_${cmd}_completion > /dev/null"; then
|
if eval "type _fzf_complete_${cmd} > /dev/null"; then
|
||||||
eval "prefix=\"$prefix\" _fzf_${cmd}_completion \"$lbuf\""
|
eval "prefix=\"$prefix\" _fzf_complete_${cmd} \"$lbuf\""
|
||||||
elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
|
elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
|
||||||
_fzf_dir_completion "$prefix" "$lbuf"
|
_fzf_dir_completion "$prefix" "$lbuf"
|
||||||
else
|
else
|
||||||
_fzf_all_completion "$prefix" "$lbuf"
|
_fzf_path_completion "$prefix" "$lbuf"
|
||||||
fi
|
fi
|
||||||
# Fall back to default completion
|
# Fall back to default completion
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user