[completion] Revamp completion API
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* _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
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+91 added−61 removed
- other:+91 added−61 removed
Provenance
- Commit
eee45a9- Tree
21bac55- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
659f49a
2 files changed+91 added−61 removed
modified:shell/completion.bash+50 added-32 removed
92 unmodified lines93 return $ret94 fi95 }96 97-_fzf_path_completion() {97+__fzf_generic_path_completion() {98 local cur base dir leftover matches trigger cmd fzf99 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"100 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')101 COMPREPLY=()32 unmodified lines134 _fzf_handle_dynamic_completion "$cmd" "$@"135 fi136 }137 138-_fzf_list_completion() {139- local cur selected trigger cmd src fzf138+_fzf_feed_fifo() (139+ rm -f "$fifo"140+ mkfifo "$fifo"141+ cat <&0 > "$fifo" &142+)143+144+_fzf_complete() {145+ local fifo cur selected trigger cmd fzf146+ fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"147 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"141- read -r src148+149 cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')150 trigger=${FZF_COMPLETION_TRIGGER-'**'}151 cur="${COMP_WORDS[COMP_CWORD]}"152 if [[ ${cur} == *"$trigger" ]]; then153 cur=${cur:0:${#cur}-${#trigger}}154 155+ _fzf_feed_fifo "$fifo"156 tput sc149- selected=$(eval "$src | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')150- selected=${selected% }157+ selected=$(eval "cat '$fifo' | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')158+ selected=${selected% } # Strip trailing space not to repeat "-o nospace"159 tput rc160+ rm -f "$fifo"161 162 if [ -n "$selected" ]; then163 COMPREPLY=("$selected")164 return 03 unmodified lines168 _fzf_handle_dynamic_completion "$cmd" "$@"169 fi170 }171 163-_fzf_all_completion() {164- _fzf_path_completion \172+_fzf_path_completion() {173+ __fzf_generic_path_completion \174 "-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \175 "-m" "" "$@"176 }177 178 _fzf_file_completion() {170- _fzf_path_completion \179+ __fzf_generic_path_completion \180 "-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \181 "-m" "" "$@"182 }183 184 _fzf_dir_completion() {176- _fzf_path_completion \185+ __fzf_generic_path_completion \186 "-name .git -prune -o -name .svn -prune -o -type d -print" \187 "" "/" "$@"188 }189 181-_fzf_kill_completion() {190+_fzf_complete_kill() {191 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1192 193 local selected fzf194 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"6 unmodified lines201 return 0202 fi203 }204 196-_fzf_telnet_completion() {197- _fzf_list_completion '+m' "$@" << "EOF"198- \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u199-EOF205+_fzf_complete_telnet() {206+ _fzf_complete '+m' "$@" < <(207+ \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |208+ awk '{if (length($2) > 0) {print $2}}' | sort -u209+ )210 }211 202-_fzf_ssh_completion() {203- _fzf_list_completion '+m' "$@" << "EOF"204- 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 -u205-EOF212+_fzf_complete_ssh() {213+ _fzf_complete '+m' "$@" < <(214+ cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \215+ <(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |216+ awk '{if (length($2) > 0) {print $2}}' | sort -u217+ )218 }219 208-_fzf_env_var_completion() {209- _fzf_list_completion '-m' "$@" << "EOF"220+_fzf_complete_unset() {221+ _fzf_complete '-m' "$@" < <(222 declare -xp | sed 's/=.*//' | sed 's/.* //'211-EOF223+ )224 }225 214-_fzf_alias_completion() {215- _fzf_list_completion '-m' "$@" << "EOF"226+_fzf_complete_export() {227+ _fzf_complete '-m' "$@" < <(228+ declare -xp | sed 's/=.*//' | sed 's/.* //'229+ )230+}231+232+_fzf_complete_unalias() {233+ _fzf_complete '-m' "$@" < <(234 alias | sed 's/=.*//' | sed 's/.* //'217-EOF235+ )236 }237 238 # fzf options239 complete -o default -F _fzf_opts_completion fzf34 unmodified lines274 done275 276 # Anything277 for cmd in $a_cmds; do260- complete -F _fzf_all_completion -o default -o bashdefault $cmd278+ complete -F _fzf_path_completion -o default -o bashdefault $cmd279 done280 281 # Kill completion264-complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill282+complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill283 284 # Host completion267-complete -F _fzf_ssh_completion -o default -o bashdefault ssh268-complete -F _fzf_telnet_completion -o default -o bashdefault telnet285+complete -F _fzf_complete_ssh -o default -o bashdefault ssh286+complete -F _fzf_complete_telnet -o default -o bashdefault telnet287 288 # Environment variables / Aliases271-complete -F _fzf_env_var_completion -o default -o bashdefault unset272-complete -F _fzf_env_var_completion -o default -o bashdefault export273-complete -F _fzf_alias_completion -o default -o bashdefault unalias289+complete -F _fzf_complete_unset -o default -o bashdefault unset290+complete -F _fzf_complete_export -o default -o bashdefault export291+complete -F _fzf_complete_unalias -o default -o bashdefault unalias292 293 unset cmd d_cmds f_cmds a_cmds x_cmdsmodified:shell/completion.zsh+41 added-29 removed
8 unmodified lines9101112131314151617181927 unmodified lines47484950505151525354555657575859606162626363646566676869707172736674756976777871798081828384767778798586878889909182838485929394959697989988891001011029110310410594951061071089710911011110010111211311410311511611711811931 unmodified lines15115215315414314415515615715815914816016116216316410 unmodified lines8 unmodified lines# - $FZF_TMUX_HEIGHT (default: '40%')# - $FZF_COMPLETION_TRIGGER (default: '**')# - $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 # (Q) flag removes a quoting level: "foo\ bar" => "foo bar" base=${(Q)1} lbuf=$2 find_opts=$3 fzf_opts=$427 unmodified lines done [ -n "$nnm" ] && unsetopt nonomatch}
_fzf_all_completion() { _fzf_path_completion "$1" "$2" \_fzf_path_completion() { __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" \ "-m" "" " "}
_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" \ "" "/" ""}
_fzf_list_completion() { local fzf_opts lbuf src fzf matches_fzf_feed_fifo() ( 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 lbuf=$2 read -r src [ ${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 LBUFFER="$lbuf$matches " LBUFFER="$lbuf$matches" fi zle redisplay rm -f "$fifo"}
_fzf_telnet_completion() { _fzf_list_completion '+m' "$@" << "EOF" \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -uEOF_fzf_complete_telnet() { _fzf_complete '+m' "$@" < <( \grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' | awk '{if (length($2) > 0) {print $2}}' | sort -u )}
_fzf_ssh_completion() { _fzf_list_completion '+m' "$@" << "EOF" 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 -uEOF_fzf_complete_ssh() { _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 )}
_fzf_export_completion() { _fzf_list_completion '+m' "$@" << "EOF"_fzf_complete_export() { _fzf_complete '-m' "$@" < <( declare -xp | sed 's/=.*//' | sed 's/.* //'EOF )}
_fzf_unset_completion() { _fzf_list_completion '+m' "$@" << "EOF"_fzf_complete_unset() { _fzf_complete '-m' "$@" < <( declare -xp | sed 's/=.*//' | sed 's/.* //'EOF )}
_fzf_unalias_completion() { _fzf_list_completion '+m' "$@" << "EOF"_fzf_complete_unalias() { _fzf_complete '+m' "$@" < <( alias | sed 's/=.*//'EOF )}
fzf-completion() { local tokens cmd prefix trigger tail fzf matches lbuf d_cmds sws31 unmodified lines
[ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}} [ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}}
if eval "type _fzf_${cmd}_completion > /dev/null"; then eval "prefix=\"$prefix\" _fzf_${cmd}_completion \"$lbuf\"" if eval "type _fzf_complete_${cmd} > /dev/null"; then eval "prefix=\"$prefix\" _fzf_complete_${cmd} \"$lbuf\"" elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then _fzf_dir_completion "$prefix" "$lbuf" else _fzf_all_completion "$prefix" "$lbuf" _fzf_path_completion "$prefix" "$lbuf" fi # Fall back to default completion else eval "zle ${fzf_default_completion:-expand-or-complete}"10 unmodified lines