Skip to content
Myr

[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

authored by Junegunn Choi · committed by Junegunn Choi · parent 659f49a · Browse files · 2 files+91 added−61 removed
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

2 files+91 added−61 removed

modified:shell/completion.bash+50 added-32 removed
92 unmodified lines
93 return $ret94 fi95}9697_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 lines
134 _fzf_handle_dynamic_completion "$cmd" "$@"135 fi136}137138_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)143144_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 src148149 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}}154155 _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"161162 if [ -n "$selected" ]; then163 COMPREPLY=("$selected")164 return 0
3 unmodified lines
168 _fzf_handle_dynamic_completion "$cmd" "$@"169 fi170}171163_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}177178_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}183184_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}189181_fzf_kill_completion() {190_fzf_complete_kill() {191 [ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1192193 local selected fzf194 [ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
6 unmodified lines
201 return 0202 fi203}204196_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 -u199EOF205_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}211202_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 -u205EOF212_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}219208_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/.* //'211EOF223 )224}225214_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}231232_fzf_complete_unalias() {233 _fzf_complete '-m' "$@" < <(234 alias | sed 's/=.*//' | sed 's/.* //'217EOF235 )236}237238# fzf options239complete -o default -F _fzf_opts_completion fzf
34 unmodified lines
274done275276# Anything277for cmd in $a_cmds; do260 complete -F _fzf_all_completion -o default -o bashdefault $cmd278 complete -F _fzf_path_completion -o default -o bashdefault $cmd279done280281# Kill completion264complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill282complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill283284# Host completion267complete -F _fzf_ssh_completion -o default -o bashdefault ssh268complete -F _fzf_telnet_completion -o default -o bashdefault telnet285complete -F _fzf_complete_ssh -o default -o bashdefault ssh286complete -F _fzf_complete_telnet -o default -o bashdefault telnet287288# Environment variables / Aliases271complete -F _fzf_env_var_completion -o default -o bashdefault unset272complete -F _fzf_env_var_completion -o default -o bashdefault export273complete -F _fzf_alias_completion -o default -o bashdefault unalias289complete -F _fzf_complete_unset -o default -o bashdefault unset290complete -F _fzf_complete_export -o default -o bashdefault export291complete -F _fzf_complete_unalias -o default -o bashdefault unalias292293unset cmd d_cmds f_cmds a_cmds x_cmds
modified:shell/completion.zsh+41 added-29 removed