Skip to content
Myr

Enhance command extraction in zsh completion (#4082)

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

Fix #1992

authored by LangLangBart · committed by GitHub · parent 51623a5 · Browse files · 2 files+30 added−25 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+30 added−25 removed

  • other:+30 added−25 removed

2 files+30 added−25 removed

modified:shell/completion.zsh+29 added-24 removed
118 unmodified lines
119 fzf "$@"120 fi121}122123# Extract the name of the command. e.g. foo=1 bar baz**<tab>123# Extract the name of the command. e.g. ls; foo=1 ssh **<tab>124__fzf_extract_command() {125 local token tokens126 tokens=(${(z)1})127 for token in $tokens; do128 token=${(Q)token}129 if [[ "$token" =~ [[:alnum:]] && ! "$token" =~ "=" ]]; then130 echo "$token"131 return132 fi133 done134 echo "${tokens[1]}"125 setopt localoptions noksh_arrays126 # Control completion with the "compstate" parameter, insert and list noting127 compstate[insert]=128 compstate[list]=129 cmd_word="${words[1]}"130}131132__fzf_generic_path_completion() {138 local base lbuf cmd compgen fzf_opts suffix tail dir leftover matches133 local base lbuf compgen fzf_opts suffix tail dir leftover matches134 base=$1135 lbuf=$2141 cmd=$(__fzf_extract_command "$lbuf")136 compgen=$3137 fzf_opts=$4138 suffix=$5139 tail=$6
14 unmodified lines
154 export FZF_DEFAULT_OPTS155 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --scheme=path" "${FZF_COMPLETION_OPTS-}")156 unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS_FILE157 if declare -f "$compgen" > /dev/null; then164 eval "$compgen $(printf %q "$dir")" | __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover"158 eval "$compgen $(printf %q "$dir")" | __fzf_comprun "$cmd_word" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover"159 else160 if [[ $compgen =~ dir ]]; then161 walker=dir,follow162 rest=${FZF_COMPLETION_DIR_OPTS-}163 else164 walker=file,dir,follow,hidden165 rest=${FZF_COMPLETION_PATH_OPTS-}166 fi173 __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" --walker "$walker" --walker-root="$dir" ${(Q)${(Z+n+)rest}} < /dev/tty167 __fzf_comprun "$cmd_word" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" --walker "$walker" --walker-root="$dir" ${(Q)${(Z+n+)rest}} < /dev/tty168 fi | while read -r item; do169 item="${item%$suffix}$suffix"170 echo -n -E "${(q)item} "171 done
48 unmodified lines
220 shift221 rest=("$@")222 fi223230 local fifo lbuf cmd matches post224 local fifo lbuf matches post225 fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"226 lbuf=${rest[0]}233 cmd=$(__fzf_extract_command "$lbuf")227 post="${funcstack[1]}_post"228 type $post > /dev/null 2>&1 || post=cat229230 _fzf_feed_fifo "$fifo"231 matches=$(232 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse" "${FZF_COMPLETION_OPTS-} $str_arg") \233 FZF_DEFAULT_OPTS_FILE='' \241 __fzf_comprun "$cmd" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')234 __fzf_comprun "$cmd_word" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')235 if [ -n "$matches" ]; then236 LBUFFER="$lbuf$matches"237 fi238 command rm -f "$fifo"
63 unmodified lines
302 awk '{print $2}'303}304305fzf-completion() {306 trap 'unset cmd_word' EXIT313 local tokens cmd prefix trigger tail matches lbuf d_cmds307 local tokens prefix trigger tail matches lbuf d_cmds308 setopt localoptions noshwordsplit noksh_arrays noposixbuiltins309310 # Check if at least one completion system (old or new) is active311 if ! zmodload -F zsh/parameter p:functions 2>/dev/null || ! (( $+functions[compdef] )); then312 if ! zmodload -e zsh/compctl; then313 zmodload -i zsh/compctl314 fi315 fi316 # http://zsh.sourceforge.net/FAQ/zshfaq03.html317 # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags318 tokens=(${(z)LBUFFER})319 if [ ${#tokens} -lt 1 ]; then320 zle ${fzf_default_completion:-expand-or-complete}321 return322 fi323324 cmd=$(__fzf_extract_command "$LBUFFER")325324 # Explicitly allow for empty trigger.325 trigger=${FZF_COMPLETION_TRIGGER-'**'}326 [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")327
9 unmodified lines
337 # Trigger sequence given338 if [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then339 d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir})340341 # Make the 'cmd_word' global342 zle __fzf_extract_command || :343 [[ -z "$cmd_word" ]] && return344345 [ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}346 if [[ $prefix = *'$('* ]] || [[ $prefix = *'<('* ]] || [[ $prefix = *'>('* ]] || [[ $prefix = *':='* ]] || [[ $prefix = *'`'* ]]; then347 return348 fi349 [ -n "${tokens[-1]}" ] && lbuf=${lbuf:0:-${#tokens[-1]}}350349 if eval "type _fzf_complete_${cmd} > /dev/null"; then350 prefix="$prefix" eval _fzf_complete_${cmd} ${(q)lbuf}351 if eval "type _fzf_complete_${cmd_word} > /dev/null"; then352 prefix="$prefix" eval _fzf_complete_${cmd_word} ${(q)lbuf}353 zle reset-prompt352 elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then354 elif [ ${d_cmds[(i)$cmd_word]} -le ${#d_cmds} ]; then355 _fzf_dir_completion "$prefix" "$lbuf"356 else357 _fzf_path_completion "$prefix" "$lbuf"358 fi
8 unmodified lines
367 [[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2]368 unset binding369}370371# Completion widget to gain access to the 'words' array (man zshcompwid)372zle -C __fzf_extract_command .complete-word __fzf_extract_command373# Normal widget374zle -N fzf-completion375bindkey '^I' fzf-completion376fi377
5 unmodified lines
modified:shell/key-bindings.zsh+1 added-1 removed