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
Provenance
- Commit
57c08d9- Tree
4c524c7- Author
LangLangBart <92653266+LangLangBart@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
51623a5
2 files changed+30 added−25 removed
modified:shell/completion.zsh+29 added-24 removed
118 unmodified lines119 fzf "$@"120 fi121 }122 123-# 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 }131 132 __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=$614 unmodified lines154 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 done48 unmodified lines220 shift221 rest=("$@")222 fi223 230- 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=cat229 230 _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 lines302 awk '{print $2}'303 }304 305 fzf-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 noposixbuiltins309 310+ # 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 fi323 324- cmd=$(__fzf_extract_command "$LBUFFER")325-324 # Explicitly allow for empty trigger.325 trigger=${FZF_COMPLETION_TRIGGER-'**'}326 [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")327 9 unmodified lines337 # Trigger sequence given338 if [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then339 d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir})340 341+ # Make the 'cmd_word' global342+ zle __fzf_extract_command || :343+ [[ -z "$cmd_word" ]] && return344+345 [ -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]}}350 349- 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 fi8 unmodified lines367 [[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2]368 unset binding369 }370 371+# Completion widget to gain access to the 'words' array (man zshcompwid)372+zle -C __fzf_extract_command .complete-word __fzf_extract_command373+# Normal widget374 zle -N fzf-completion375 bindkey '^I' fzf-completion376 fi377 5 unmodified linesmodified:shell/key-bindings.zsh+1 added-1 removed
109 unmodified linesExpand all11011111211311411411511611711832 unmodified lines109 unmodified linesExpand all setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null # Ensure the module is loaded if not already, and the required features, such # as the associative 'history' array, which maps event numbers to full history # lines, are set. Also, make sure Perl is installed for multi-line output. if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( ${#commands[perl]} )); then if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( $+commands[perl] )); then # Import commands from other shells if SHARE_HISTORY is enabled, as the # 'history' array only updates after executing a non-empty command. selected="$( if [[ -o sharehistory ]]; then32 unmodified lines