Skip to content
Myr

[completion] Allow users to customize fzf options via _fzf_comprun

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Related #1809 #1850

authored by Junegunn Choi · committed by Junegunn Choi · parent 54891d1 · Browse files · 3 files+63 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+63 added−26 removed

  • other:+63 added−26 removed

3 files+63 added−26 removed

modified:shell/completion.bash+16 added-12 removed
32 unmodified lines
3334# To redraw line after fzf closes (printf '\e[5n')35bind '"\e[0n": redraw-current-line'3637__fzfcmd_complete() {38 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&39 echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"37__fzf_comprun() {38 if [ "$(type -t _fzf_comprun 2>&1)" = function ]; then39 _fzf_comprun "$@"40 elif [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]; then41 shift42 fzf-tmux -d "${FZF_TMUX_HEIGHT:-40%}" "$@"43 else44 shift45 fzf "$@"46 fi47}4849__fzf_orig_completion_filter() {50 sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2"; [[ "\1" = *" -o nospace "* ]] \&\& [[ ! "$__fzf_nospace_commands" = *" \3 "* ]] \&\& __fzf_nospace_commands="$__fzf_nospace_commands \3 ";/' |
97 unmodified lines
148 fi149}150151__fzf_generic_path_completion() {145 local cur base dir leftover matches trigger cmd fzf146 fzf="$(__fzfcmd_complete)"152 local cur base dir leftover matches trigger cmd153 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"154 COMPREPLY=()155 trigger=${FZF_COMPLETION_TRIGGER-'**'}156 cur="${COMP_WORDS[COMP_CWORD]}"
7 unmodified lines
164 leftover=${base/#"$dir"}165 leftover=${leftover/#\/}166 [ -z "$dir" ] && dir='.'167 [ "$dir" != "/" ] && dir="${dir/%\//}"162 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $2 -q "$leftover" | while read -r item; do168 matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $2" __fzf_comprun "$4" -q "$leftover" | while read -r item; do169 printf "%q$3 " "$item"170 done)171 matches=${matches% }172 [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "
16 unmodified lines
189 fi190}191192_fzf_complete() {187 local cur selected trigger cmd fzf post193 local cur selected trigger cmd post194 post="$(caller 0 | awk '{print $2}')_post"195 type -t "$post" > /dev/null 2>&1 || post=cat190 fzf="$(__fzfcmd_complete)"196197 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"198 trigger=${FZF_COMPLETION_TRIGGER-'**'}199 cur="${COMP_WORDS[COMP_CWORD]}"200 if [[ "$cur" == *"$trigger" ]]; then201 cur=${cur:0:${#cur}-${#trigger}}202198 selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ')203 selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $1" __fzf_comprun "$2" -q "$cur" | $post | tr '\n' ' ')204 selected=${selected% } # Strip trailing space not to repeat "-o nospace"205 if [ -n "$selected" ]; then206 COMPREPLY=("$selected")207 else
22 unmodified lines
230231_fzf_complete_kill() {232 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1233229 local selected fzf230 fzf="$(__fzfcmd_complete)"231 selected=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap $FZF_COMPLETION_OPTS" $fzf -m | awk '{print $2}' | tr '\n' ' ')234 local selected235 selected=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS --preview 'echo {}' --preview-window down:3:wrap" __fzf_comprun "kill" -m | awk '{print $2}' | tr '\n' ' ')236 printf '\e[5n'237238 if [ -n "$selected" ]; then239 COMPREPLY=( "$selected" )
105 unmodified lines
modified:shell/completion.zsh+32 added-14 removed
29 unmodified lines
30fi3132###########################################################3334__fzfcmd_complete() {35 [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&36 echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"34__fzf_comprun() {35 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then36 _fzf_comprun "$@"37 elif [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]; then38 shift39 fzf-tmux -d "${FZF_TMUX_HEIGHT:-40%}" "$@"40 else41 shift42 fzf "$@"43 fi44}4546# Extract the name of the command. e.g. foo=1 bar baz**<tab>47__fzf_extract_command() {48 local token tokens49 tokens=(${(z)1})50 for token in $tokens; do51 if [[ "$token" =~ [[:alnum:]] && ! "$token" =~ "=" ]]; then52 echo "$token"53 return54 fi55 done56 echo "${tokens[1]}"57}5859__fzf_generic_path_completion() {40 local base lbuf compgen fzf_opts suffix tail fzf dir leftover matches60 local base lbuf cmd compgen fzf_opts suffix tail dir leftover matches61 base=$162 lbuf=$263 cmd=$(__fzf_extract_command "$lbuf")64 compgen=$365 fzf_opts=$466 suffix=$567 tail=$647 fzf="$(__fzfcmd_complete)"6869 setopt localoptions nonomatch70 eval "base=$base"71 [[ $base = *"/"* ]] && dir="$base"
2 unmodified lines
74 leftover=${base/#"$dir"}75 leftover=${leftover/#\/}76 [ -z "$dir" ] && dir='.'77 [ "$dir" != "/" ] && dir="${dir/%\//}"58 matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${(Q)${(Z+n+)fzf}} ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" | while read item; do78 matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" | while read item; do79 echo -n "${(q)item}$suffix "80 done)81 matches=${matches% }82 if [ -n "$matches" ]; then
23 unmodified lines
106 cat <&0 > "$1" &107)108109_fzf_complete() {90 local fifo fzf_opts lbuf fzf matches post110 local fifo fzf_opts lbuf cmd matches post111 fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"112 fzf_opts=$1113 lbuf=$2114 cmd=$(__fzf_extract_command "$lbuf")115 post="${funcstack[2]}_post"116 type $post > /dev/null 2>&1 || post=cat11797 fzf="$(__fzfcmd_complete)"98118 _fzf_feed_fifo "$fifo"100 matches=$(cat "$fifo" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${(Q)${(Z+n+)fzf}} ${(Q)${(Z+n+)fzf_opts}} -q "${(Q)prefix}" | $post | tr '\n' ' ')119 matches=$(cat "$fifo" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "${(Q)prefix}" | $post | tr '\n' ' ')120 if [ -n "$matches" ]; then121 LBUFFER="$lbuf$matches"122 fi123 zle reset-prompt
35 unmodified lines
159 )160}161162fzf-completion() {144 local tokens cmd prefix trigger tail fzf matches lbuf d_cmds163 local tokens cmd prefix trigger tail matches lbuf d_cmds164 setopt localoptions noshwordsplit noksh_arrays noposixbuiltins165166 # http://zsh.sourceforge.net/FAQ/zshfaq03.html167 # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
2 unmodified lines
170 zle ${fzf_default_completion:-expand-or-complete}171 return172 fi173155 cmd=${tokens[1]}174 cmd=$(__fzf_extract_command "$LBUFFER")175176 # Explicitly allow for empty trigger.177 trigger=${FZF_COMPLETION_TRIGGER-'**'}178 [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")
6 unmodified lines
185186 tail=${LBUFFER:$(( ${#LBUFFER} - ${#trigger} ))}187 # Kill completion (do not require trigger sequence)188 if [ $cmd = kill -a ${LBUFFER[-1]} = ' ' ]; then170 fzf="$(__fzfcmd_complete)"171 matches=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap $FZF_COMPLETION_OPTS" ${(Q)${(Z+n+)fzf}} -m | awk '{print $2}' | tr '\n' ' ')189 matches=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS --preview 'echo {}' --preview-window down:3:wrap" __fzf_comprun "$cmd" -m | awk '{print $2}' | tr '\n' ' ')190 if [ -n "$matches" ]; then191 LBUFFER="$LBUFFER$matches"192 fi193 zle reset-prompt
30 unmodified lines
modified:README.md+15 added
401 unmodified lines
402# Use fd to generate the list for directory completion403_fzf_compgen_dir() {404 fd --type d --hidden --follow --exclude ".git" . "$1"405}406407# (EXPERIMENTAL) Advanced customization of fzf options via _fzf_comprun function408# - The first argument to the function is the name of the command.409# - You should make sure to pass the rest of the arguments to fzf.410_fzf_comprun() {411 local command=$1412 shift413414 case "$command" in415 cd) fzf "$@" --preview 'tree -C {} | head -200' ;;416 export|unset) fzf "$@" --preview "eval 'echo \$'{}" "$@" ;;417 ssh) fzf "$@" --preview 'dig {}' ;;418 *) fzf "$@" ;;419 esac420}421```422423#### Supported commands424
176 unmodified lines