fish: Completion script rewrite (SHIFT-TAB)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by bitraid · committed by Junegunn Choi · parent 9f8294d · Browse files · 6 files·+215 added−439 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+215 added−439 removed
- tests:+1 added−2 removed
- other:+214 added−437 removed
Provenance
- Commit
23164c2- Tree
763dfee- Author
bitraid <bitraid@protonmail.ch>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9f8294d
6 files changed+215 added−439 removed
deleted:shell/common.fish-141 removed
1- function __fzf_defaults2- # $argv[1]: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS3- # $argv[2..]: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS4- test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%5- string join ' ' -- \6- "--height $FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" $argv[1] \7- (test -r "$FZF_DEFAULT_OPTS_FILE"; and string join -- ' ' <$FZF_DEFAULT_OPTS_FILE) \8- $FZF_DEFAULT_OPTS $argv[2..-1]9- end10-11- function __fzfcmd12- test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%13- if test -n "$FZF_TMUX_OPTS"14- echo "fzf-tmux $FZF_TMUX_OPTS -- "15- else if test "$FZF_TMUX" = "1"16- echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "17- else18- echo "fzf"19- end20- end21-22- function __fzf_cmd_tokens -d 'Return command line tokens, skipping leading env assignments and command prefixes'23- # Get tokens - use version-appropriate flags24- set -l tokens25- if test (string match -r -- '^\d+' $version) -ge 426- set -- tokens (commandline -xpc)27- else28- set -- tokens (commandline -opc)29- end30-31- # Filter out leading environment variable assignments32- set -l -- var_count 033- for i in $tokens34- if string match -qr -- '^[\w]+=' $i35- set var_count (math $var_count + 1)36- else37- break38- end39- end40- set -e -- tokens[0..$var_count]41-42- # Skip command prefixes so callers see the actual command name,43- # e.g. "builtin cd" → "cd", "env VAR=1 command cd" → "cd"44- while true45- switch "$tokens[1]"46- case builtin command47- set -e -- tokens[1]48- test "$tokens[1]" = "--"; and set -e -- tokens[1]49- case env50- set -e -- tokens[1]51- test "$tokens[1]" = "--"; and set -e -- tokens[1]52- while string match -qr -- '^[\w]+=' "$tokens[1]"53- set -e -- tokens[1]54- end55- case '*'56- break57- end58- end59-60- string escape -n -- $tokens61- end62-63- function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'64- set -l fzf_query ''65- set -l prefix ''66- set -l dir '.'67-68- # Set variables containing the major and minor fish version numbers, using69- # a method compatible with all supported fish versions.70- set -l -- fish_major (string match -r -- '^\d+' $version)71- set -l -- fish_minor (string match -r -- '^\d+\.(\d+)' $version)[2]72-73- # fish v3.3.0 and newer: Don't use option prefix if " -- " is preceded.74- set -l -- match_regex '(?<fzf_query>[\s\S]*?(?=\n?$)$)'75- set -l -- prefix_regex '^-[^\s=]+=|^-(?!-)\S'76- if test "$fish_major" -eq 3 -a "$fish_minor" -lt 377- or string match -q -v -- '* -- *' (string sub -l (commandline -Cp) -- (commandline -p))78- set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"79- end80-81- # Set $prefix and expanded $fzf_query with preserved trailing newlines.82- if test "$fish_major" -ge 483- # fish v4.0.0 and newer84- string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N)85- else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 286- # fish v3.2.0 - v3.7.1 (last v3)87- string match -q -r -- $match_regex (commandline --current-token --tokenize | string collect -N)88- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)' '')89- else90- # fish older than v3.2.0 (v3.1b1 - v3.1.2)91- set -l -- cl_token (commandline --current-token --tokenize | string collect -N)92- set -- prefix (string match -r -- $prefix_regex $cl_token)93- set -- fzf_query (string replace -- "$prefix" '' $cl_token | string collect -N)94- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)|\\\n\\\n$' '')95- end96-97- if test -n "$fzf_query"98- # Normalize path in $fzf_query, set $dir to the longest existing directory.99- if test \( "$fish_major" -ge 4 \) -o \( "$fish_major" -eq 3 -a "$fish_minor" -ge 5 \)100- # fish v3.5.0 and newer101- set -- fzf_query (path normalize -- $fzf_query)102- set -- dir $fzf_query103- while not path is -d $dir104- set -- dir (path dirname $dir)105- end106- else107- # fish older than v3.5.0 (v3.1b1 - v3.4.1)108- if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2109- # fish v3.2.0 - v3.4.1110- string match -q -r -- '(?<fzf_query>^[\s\S]*?(?=\n?$)$)' \111- (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)112- else113- # fish v3.1b1 - v3.1.2114- set -- fzf_query (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)115- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r '\\\n$' '')116- end117- set -- dir $fzf_query118- while not test -d "$dir"119- set -- dir (dirname -z -- "$dir" | string split0)120- end121- end122-123- if not string match -q -- '.' $dir; or string match -q -r -- '^\./|^\.$' $fzf_query124- # Strip $dir from $fzf_query - preserve trailing newlines.125- if test "$fish_major" -ge 4126- # fish v4.0.0 and newer127- string match -q -r -- '^'(string escape --style=regex -- $dir)'/?(?<fzf_query>[\s\S]*)' $fzf_query128- else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2129- # fish v3.2.0 - v3.7.1 (last v3)130- string match -q -r -- '^/?(?<fzf_query>[\s\S]*?(?=\n?$)$)' \131- (string replace -- "$dir" '' $fzf_query | string collect -N)132- else133- # fish older than v3.2.0 (v3.1b1 - v3.1.2)134- set -- fzf_query (string replace -- "$dir" '' $fzf_query | string collect -N)135- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^/?|\\\n$' '')136- end137- end138- end139-140- string escape -n -- "$dir" "$fzf_query" "$prefix"141- endmodified:shell/completion.fish+129 added-201 removed
2 unmodified lines3 # / /_/_ / / /_4 # / __/ / /_/ __/5 # /_/ /___/_/ completion.fish6 #7-# - $FZF_COMPLETION_OPTS (default: empty)7+# - $FZF_COMPLETION_OPTS8+# - $FZF_EXPANSION_OPTS9 9-function fzf_completion_setup10+# The oldest supported fish version is 3.4.0. For this message being able to be11+# displayed on older versions, the command substitution syntax $() should not12+# be used anywhere in the script, otherwise the source command will fail.13+if string match -qr -- '^[12]\\.|^3\\.[0-3]' $version14+ echo "fzf completion script requires fish version 3.4.0 or newer." >&215+ return 116+else if not command -q fzf17+ echo "fzf was not found in path." >&218+ return 119+end20 11-#----BEGIN INCLUDE common.fish12-# NOTE: Do not directly edit this section, which is copied from "common.fish".13-# To modify it, one can edit "common.fish" and run "./update.sh" to apply14-# the changes. See code comments in "common.fish" for the implementation details.15-16- function __fzf_defaults17- test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%18- string join ' ' -- \19- "--height $FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" $argv[1] \20- (test -r "$FZF_DEFAULT_OPTS_FILE"; and string join -- ' ' <$FZF_DEFAULT_OPTS_FILE) \21- $FZF_DEFAULT_OPTS $argv[2..-1]21+function fzf_complete -w fzf -d 'fzf command completion and wildcard expansion search'22+ # Restore the default shift-tab behavior on tab completions23+ if commandline --paging-mode24+ commandline -f complete-and-search25+ return26 end27 24- function __fzfcmd25- test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%26- if test -n "$FZF_TMUX_OPTS"27- echo "fzf-tmux $FZF_TMUX_OPTS -- "28- else if test "$FZF_TMUX" = "1"29- echo "fzf-tmux -d$FZF_TMUX_HEIGHT -- "30- else31- echo "fzf"32- end33- end28+ # Remove any trailing unescaped backslash from token and update command line29+ set -l -- token (string replace -r -- '(?<!\\\\)(?:\\\\\\\\)*\\K\\\\$' '' (commandline -t | string collect) | string collect)30+ commandline -rt -- $token31 35- function __fzf_cmd_tokens -d 'Return command line tokens, skipping leading env assignments and command prefixes'36- set -l tokens37- if test (string match -r -- '^\d+' $version) -ge 438- set -- tokens (commandline -xpc)39- else40- set -- tokens (commandline -opc)41- end32+ # Remove any line breaks from token33+ set -- token (string replace -ra -- '\\\\\\n' '' $token | string collect)34 43- set -l -- var_count 044- for i in $tokens45- if string match -qr -- '^[\w]+=' $i46- set var_count (math $var_count + 1)47- else48- break49- end50- end51- set -e -- tokens[0..$var_count]35+ # regex: Match token with unescaped/unquoted glob character36+ set -l -- r_glob '^(?:[^\'"\\\\*]|\\\\[\\S\\s]|\'(?:\\\\[\\S\\s]|[^\'\\\\])*\'|"(?:\\\\[\\S\\s]|[^"\\\\])*")*\\*[\\S\\s]*$'37 53- while true54- switch "$tokens[1]"55- case builtin command56- set -e -- tokens[1]57- test "$tokens[1]" = "--"; and set -e -- tokens[1]58- case env59- set -e -- tokens[1]60- test "$tokens[1]" = "--"; and set -e -- tokens[1]61- while string match -qr -- '^[\w]+=' "$tokens[1]"62- set -e -- tokens[1]63- end64- case '*'65- break66- end67- end38+ # regex: Match any unbalanced quote character39+ set -l -- r_quote '^(?>(?:\\\\[\\s\\S]|"(?:[^"\\\\]|\\\\[\\s\\S])*"|\'(?:[^\'\\\\]|\\\\[\\s\\S])*\'|[^\'"\\\\]+)*)\\K[\'"]'40 69- string escape -n -- $tokens70- end41+ # The expansion pattern is the token with any open quote closed, or is empty.42+ set -l -- glob_pattern (string match -r -- $r_glob $token | string collect)(string match -r -- $r_quote $token | string collect -a)43 72- function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'73- set -l fzf_query ''74- set -l prefix ''75- set -l dir '.'44+ set -l -- cl_tokenize_opt '--tokens-expanded'45+ string match -q -- '3.*' $version46+ and set -- cl_tokenize_opt '--tokenize'47 77- set -l -- fish_major (string match -r -- '^\d+' $version)78- set -l -- fish_minor (string match -r -- '^\d+\.(\d+)' $version)[2]48+ # Set command line tokens without any leading variable definitions or launcher49+ # commands (including their options, but not any option arguments).50+ set -l -- r_cmd '^(?:(?:builtin|command|doas|env|sudo|\\w+=\\S*|-\\S+)\\s+)*\\K[\\s\\S]+'51+ set -l -- cmd (commandline $cl_tokenize_opt --input=(commandline -pc | string match -r $r_cmd))52+ test -z "$token"53+ and set -a -- cmd ''54 80- set -l -- match_regex '(?<fzf_query>[\s\S]*?(?=\n?$)$)'81- set -l -- prefix_regex '^-[^\s=]+=|^-(?!-)\S'82- if test "$fish_major" -eq 3 -a "$fish_minor" -lt 383- or string match -q -v -- '* -- *' (string sub -l (commandline -Cp) -- (commandline -p))84- set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"85- end55+ # Set fzf options56+ test -z "$FZF_TMUX_HEIGHT"57+ and set -l -- FZF_TMUX_HEIGHT 40%58 87- if test "$fish_major" -ge 488- string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N)89- else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 290- string match -q -r -- $match_regex (commandline --current-token --tokenize | string collect -N)91- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)' '')92- else93- set -l -- cl_token (commandline --current-token --tokenize | string collect -N)94- set -- prefix (string match -r -- $prefix_regex $cl_token)95- set -- fzf_query (string replace -- "$prefix" '' $cl_token | string collect -N)96- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)|\\\n\\\n$' '')97- end59+ set -lax -- FZF_DEFAULT_OPTS \60+ "--height=$FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" \61+ (test -r "$FZF_DEFAULT_OPTS_FILE"; and string join -- ' ' <$FZF_DEFAULT_OPTS_FILE) \62+ $FZF_DEFAULT_OPTS '--bind=alt-r:toggle-raw --multi --wrap=word --reverse' \63+ (if test -n "$glob_pattern"; string collect -- $FZF_EXPANSION_OPTS; else;64+ string collect -- $FZF_COMPLETION_OPTS; end; string escape -n -- $argv) \65+ --with-shell=(status fish-path)\\ -c66 99- if test -n "$fzf_query"100- if test \( "$fish_major" -ge 4 \) -o \( "$fish_major" -eq 3 -a "$fish_minor" -ge 5 \)101- set -- fzf_query (path normalize -- $fzf_query)102- set -- dir $fzf_query103- while not path is -d $dir104- set -- dir (path dirname $dir)105- end106- else107- if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2108- string match -q -r -- '(?<fzf_query>^[\s\S]*?(?=\n?$)$)' \109- (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)110- else111- set -- fzf_query (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)112- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r '\\\n$' '')113- end114- set -- dir $fzf_query115- while not test -d "$dir"116- set -- dir (dirname -z -- "$dir" | string split0)117- end118- end67+ set -lx FZF_DEFAULT_OPTS_FILE68 120- if not string match -q -- '.' $dir; or string match -q -r -- '^\./|^\.$' $fzf_query121- if test "$fish_major" -ge 4122- string match -q -r -- '^'(string escape --style=regex -- $dir)'/?(?<fzf_query>[\s\S]*)' $fzf_query123- else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2124- string match -q -r -- '^/?(?<fzf_query>[\s\S]*?(?=\n?$)$)' \125- (string replace -- "$dir" '' $fzf_query | string collect -N)126- else127- set -- fzf_query (string replace -- "$dir" '' $fzf_query | string collect -N)128- eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^/?|\\\n$' '')129- end130- end131- end69+ set -l -- fzf_cmd fzf70+ test "$FZF_TMUX" = 171+ and set -- fzf_cmd fzf-tmux $FZF_TMUX_OPTS -d$FZF_TMUX_HEIGHT --72 133- string escape -n -- "$dir" "$fzf_query" "$prefix"134- end135-#----END INCLUDE73+ set -l result74 137- # Use complete builtin for specific commands138- function __fzf_complete_native139- set -l -- token (commandline -t)140- set -l -- completions (eval complete -C \"$argv[1]\")141- test -n "$completions"; or begin commandline -f repaint; return; end75+ # Get the completion list from stdin when it's not a tty76+ if not isatty stdin77+ set -l -- custom_post_func _fzf_post_complete_$cmd[1]78+ functions -q $custom_post_func79+ or set -- custom_post_func _fzf_complete_$cmd[1]_post80 143- # Calculate tabstop based on longest completion item (sample first 500 for performance)144- set -l -- tabstop 20145- set -l -- sample_size (math "min(500, "(count $completions)")")146- for c in $completions[1..$sample_size]147- set -l -- len (string length -V -- (string split -- \t $c))148- test -n "$len[2]" -a "$len[1]" -gt "$tabstop"149- and set -- tabstop $len[1]81+ if functions -q $custom_post_func82+ $fzf_cmd | $custom_post_func $cmd | while read -l r; set -a -- result $r; end83+ else if string match -q -- '*--print0*' "$FZF_DEFAULT_OPTS"84+ $fzf_cmd | while read -lz r; set -a -- result $r; end85+ else86+ $fzf_cmd | while read -l r; set -a -- result $r; end87 end151- # limit to 120 to prevent long lines152- set -- tabstop (math "min($tabstop + 4, 120)")88 154- set -l result155- set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults \156- "--reverse --delimiter=\\t --nth=1 --tabstop=$tabstop --color=fg:dim,nth:regular" \157- $FZF_COMPLETION_OPTS $argv[2..-1] --accept-nth=1 --read0 --print0)158- set -- result (string join0 -- $completions | eval (__fzfcmd) | string split0)159- and begin160- set -l -- tail ' '161- # Append / to bare ~username results (fish omits it unlike other shells)162- set -- result (string replace -r -- '^(~\w+)\s?$' '$1/' $result)163- # Don't add trailing space if single result is a directory164- test (count $result) -eq 1165- and string match -q -- '*/' "$result"; and set -- tail ''89+ # Wildcard expansion90+ else if test -n "$glob_pattern"91+ # Set the command to be run by fzf, so there is a visual indicator and an92+ # easy way to abort on long recursive searches.93+ set -lx -- FZF_DEFAULT_COMMAND "for i in $glob_pattern;" \94+ 'test -d "$i"; and string match -qv -- "*/" $i; and set -- i $i/;' \95+ 'string join0 -- $i; end'96 167- set -l -- result (string escape -n -- $result)97+ set -- result (string escape -n -- ($fzf_cmd --read0 --print0 --scheme=path --no-multi-line | string split0))98 169- string match -q -- '~*' "$token"170- and set result (string replace -r -- '^\\\\~' '~' $result)171-172- string match -q -- '$*' "$token"173- and set result (string replace -r -- '^\\\\\$' '\$' $result)174-175- commandline -rt -- (string join ' ' -- $result)$tail99+ # Command completion100+ else101+ # Call custom function if defined102+ set -l -- custom_func _fzf_complete_$cmd[1]103+ if functions -q $custom_func; and not set -q __fzf_no_custom_complete104+ set -lx __fzf_no_custom_complete105+ $custom_func $cmd106+ return107 end177- commandline -f repaint178- end108 180- function _fzf_complete181- set -l -- args (string escape -- $argv | string join ' ' | string split -- ' -- ')182- set -l -- post_func (status function)_(string split -- ' ' $args[2])[1]_post183- set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults --reverse $FZF_COMPLETION_OPTS $args[1])184- set -lx FZF_DEFAULT_OPTS_FILE185- set -lx FZF_DEFAULT_COMMAND186- set -l -- fzf_query (commandline -t | string escape)187- set -l result188- eval (__fzfcmd) --query=$fzf_query | while read -l r; set -a -- result $r; end189- and if functions -q $post_func190- commandline -rt -- (string collect -- $result | eval $post_func $args[2] | string join ' ')' '191- else192- commandline -rt -- (string join -- ' ' (string escape -- $result))' '109+ # Workaround for complete not having newlines in results110+ if string match -qr -- '\\n' $token111+ set -- token (string replace -ra -- '(?<!\\\\)(?:\\\\\\\\)*\\K\\\\\$' '\\\\\\\\\$' $token | string collect)112+ set -- token (string unescape -- $token | string collect)113+ set -- token (string replace -ra -- '\\n' '\\\\n' $token | string collect)114 end194- commandline -f repaint195- end115 197- # Kill completion (process selection)198- function _fzf_complete_kill199- set -l -- fzf_query (commandline -t | string escape)200- set -lx -- FZF_DEFAULT_OPTS (__fzf_defaults --reverse $FZF_COMPLETION_OPTS \201- --accept-nth=2 -m --header-lines=1 --no-preview --wrap)202- set -lx FZF_DEFAULT_OPTS_FILE203- if type -q ps204- set -l -- ps_cmd 'begin command ps -eo user,pid,ppid,start,time,command 2>/dev/null;' \205- 'or command ps -eo user,pid,ppid,time,args 2>/dev/null;' \206- 'or command ps --everyone --full --windows 2>/dev/null; end'207- set -l -- result (eval $ps_cmd \| (__fzfcmd) --query=$fzf_query)208- and commandline -rt -- (string join ' ' -- $result)" "209- else210- __fzf_complete_native "kill " --multi --query=$fzf_query116+ set -- list (complete -C --escape -- (string join -- ' ' (commandline -pc $cl_tokenize_opt) $token | string collect))117+ if test -n "$list"118+ # Get the initial tabstop value119+ if set -l -- tabstop (string match -rga -- '--tabstop[= ](?:0*)([1-9]\\d+|[4-9])' "$FZF_DEFAULT_OPTS")[-1]120+ set -- tabstop (math $tabstop - 4)121+ else122+ set -- tabstop 4123+ end124+125+ # Determine the tabstop length for description alignment126+ set -l -- max_columns (math $COLUMNS - 40)127+ for i in $list[1..500]128+ set -l -- item (string split -f 1 -- \t $i)129+ and set -l -- len (string length -V -- $item)130+ and test "$len" -gt "$tabstop" -a "$len" -lt "$max_columns"131+ and set -- tabstop $len132+ end133+ set -- tabstop (math $tabstop + 4)134+135+ set -- result (string collect -- $list | $fzf_cmd --delimiter="\t" --tabstop=$tabstop --wrap-sign=\t"↳ " --accept-nth=1)136 end212- commandline -f repaint137 end138 215- # Main completion function216- function fzf-completion217- set -l -- tokens (__fzf_cmd_tokens)218- set -l -- current_token (commandline -t)219- set -l -- cmd_name $tokens[1]139+ # Update command line140+ if test -n "$result"141+ # No extra space after single selection that ends with path separator142+ set -l -- tail ' '143+ test (count $result) -eq 1144+ and string match -q -- '*/' "$result"145+ and set -- tail ''146 221- # Route to appropriate completion function222- if test -n "$tokens"; and functions -q _fzf_complete_$cmd_name223- _fzf_complete_$cmd_name $tokens224- else225- set -l -- fzf_opt --query=$current_token --multi226- __fzf_complete_native "$tokens $current_token" $fzf_opt227- end147+ commandline -rt -- (string join -- ' ' $result)$tail148 end149 230- # Bind Shift-Tab to fzf-completion (Tab retains native Fish behavior)231- if test (string match -r -- '^\d+' $version) -ge 4232- bind shift-tab fzf-completion233- bind -M insert shift-tab fzf-completion234- else235- bind -k btab fzf-completion236- bind -M insert -k btab fzf-completion150+ commandline -f repaint151+end152+153+function _fzf_complete154+ set -l fzf_args155+ for i in $argv156+ string match -q -- '--' $i; and break157+ set -a -- fzf_args $i158 end159+ fzf_complete $fzf_args160 end161 240-# Run setup241-fzf_completion_setup162+# Bind to shift-tab163+if string match -qr -- '^\\d\\d+|^[4-9]' $version164+ bind shift-tab fzf_complete165+ bind -M insert shift-tab fzf_complete166+else167+ bind -k btab fzf_complete168+ bind -M insert -k btab fzf_complete169+endmodified:shell/key-bindings.fish+19 added-45 removed
9 unmodified lines10 # - $FZF_CTRL_R_OPTS11 # - $FZF_ALT_C_COMMAND12 # - $FZF_ALT_C_OPTS13 14-14 # Key bindings15 # ------------16 # The oldest supported fish version is 3.1b1. To maintain compatibility, the17 # command substitution syntax $(cmd) should never be used, even behind a version9 unmodified lines27 echo "fzf was not found in path." >&228 return 129 end30 32-#----BEGIN INCLUDE common.fish33-# NOTE: Do not directly edit this section, which is copied from "common.fish".34-# To modify it, one can edit "common.fish" and run "./update.sh" to apply35-# the changes. See code comments in "common.fish" for the implementation details.36-31 function __fzf_defaults32+ # $argv[1]: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS33+ # $argv[2..]: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS34 test -n "$FZF_TMUX_HEIGHT"; or set -l FZF_TMUX_HEIGHT 40%35 string join ' ' -- \36 "--height $FZF_TMUX_HEIGHT --min-height=20+ --bind=ctrl-z:ignore" $argv[1] \37 (test -r "$FZF_DEFAULT_OPTS_FILE"; and string join -- ' ' <$FZF_DEFAULT_OPTS_FILE) \10 unmodified lines48 echo "fzf"49 end50 end51 56- function __fzf_cmd_tokens -d 'Return command line tokens, skipping leading env assignments and command prefixes'57- set -l tokens58- if test (string match -r -- '^\d+' $version) -ge 459- set -- tokens (commandline -xpc)60- else61- set -- tokens (commandline -opc)62- end63-64- set -l -- var_count 065- for i in $tokens66- if string match -qr -- '^[\w]+=' $i67- set var_count (math $var_count + 1)68- else69- break70- end71- end72- set -e -- tokens[0..$var_count]73-74- while true75- switch "$tokens[1]"76- case builtin command77- set -e -- tokens[1]78- test "$tokens[1]" = "--"; and set -e -- tokens[1]79- case env80- set -e -- tokens[1]81- test "$tokens[1]" = "--"; and set -e -- tokens[1]82- while string match -qr -- '^[\w]+=' "$tokens[1]"83- set -e -- tokens[1]84- end85- case '*'86- break87- end88- end89-90- string escape -n -- $tokens91- end92-52 function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath, fzf query, and optional -option= prefix'53 set -l fzf_query ''54 set -l prefix ''55 set -l dir '.'56 57+ # Set variables containing the major and minor fish version numbers, using58+ # a method compatible with all supported fish versions.59 set -l -- fish_major (string match -r -- '^\d+' $version)60 set -l -- fish_minor (string match -r -- '^\d+\.(\d+)' $version)[2]61 62+ # fish v3.3.0 and newer: Don't use option prefix if " -- " is preceded.63 set -l -- match_regex '(?<fzf_query>[\s\S]*?(?=\n?$)$)'64 set -l -- prefix_regex '^-[^\s=]+=|^-(?!-)\S'65 if test "$fish_major" -eq 3 -a "$fish_minor" -lt 366 or string match -q -v -- '* -- *' (string sub -l (commandline -Cp) -- (commandline -p))67 set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"68 end69 70+ # Set $prefix and expanded $fzf_query with preserved trailing newlines.71 if test "$fish_major" -ge 472+ # fish v4.0.0 and newer73 string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N)74 else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 275+ # fish v3.2.0 - v3.7.1 (last v3)76 string match -q -r -- $match_regex (commandline --current-token --tokenize | string collect -N)77 eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)' '')78 else79+ # fish older than v3.2.0 (v3.1b1 - v3.1.2)80 set -l -- cl_token (commandline --current-token --tokenize | string collect -N)81 set -- prefix (string match -r -- $prefix_regex $cl_token)82 set -- fzf_query (string replace -- "$prefix" '' $cl_token | string collect -N)83 eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)|\\\n\\\n$' '')84 end85 86 if test -n "$fzf_query"87+ # Normalize path in $fzf_query, set $dir to the longest existing directory.88 if test \( "$fish_major" -ge 4 \) -o \( "$fish_major" -eq 3 -a "$fish_minor" -ge 5 \)89+ # fish v3.5.0 and newer90 set -- fzf_query (path normalize -- $fzf_query)91 set -- dir $fzf_query92 while not path is -d $dir93 set -- dir (path dirname $dir)94 end95 else96+ # fish older than v3.5.0 (v3.1b1 - v3.4.1)97 if test "$fish_major" -eq 3 -a "$fish_minor" -ge 298+ # fish v3.2.0 - v3.4.199 string match -q -r -- '(?<fzf_query>^[\s\S]*?(?=\n?$)$)' \100 (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)101 else102+ # fish v3.1b1 - v3.1.2103 set -- fzf_query (string replace -r -a -- '(?<=/)/|(?<!^)/+(?!\n)$' '' $fzf_query | string collect -N)104 eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r '\\\n$' '')105 end106 set -- dir $fzf_query2 unmodified lines109 end110 end111 112 if not string match -q -- '.' $dir; or string match -q -r -- '^\./|^\.$' $fzf_query113+ # Strip $dir from $fzf_query - preserve trailing newlines.114 if test "$fish_major" -ge 4115+ # fish v4.0.0 and newer116 string match -q -r -- '^'(string escape --style=regex -- $dir)'/?(?<fzf_query>[\s\S]*)' $fzf_query117 else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2118+ # fish v3.2.0 - v3.7.1 (last v3)119 string match -q -r -- '^/?(?<fzf_query>[\s\S]*?(?=\n?$)$)' \120 (string replace -- "$dir" '' $fzf_query | string collect -N)121 else122+ # fish older than v3.2.0 (v3.1b1 - v3.1.2)123 set -- fzf_query (string replace -- "$dir" '' $fzf_query | string collect -N)124 eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^/?|\\\n$' '')125 end126 end127 end128 129 string escape -n -- "$dir" "$fzf_query" "$prefix"130 end156-#----END INCLUDE131 132 # Store current token in $dir as root for the 'find' command133 function fzf-file-widget -d "List files and folders"134 set -l commandline (__fzf_parse_commandline)8 unmodified lines143 set -lx FZF_DEFAULT_COMMAND "$FZF_CTRL_T_COMMAND"144 set -lx FZF_DEFAULT_OPTS_FILE145 146 set -l result (eval (__fzfcmd) --walker-root=$dir --query=$fzf_query | string split0)173- and commandline -rt -- (string join -- ' ' $prefix(string escape --no-quoted -- $result))' '147+ and commandline -rt -- (string join -- ' ' $prefix(string escape -n -- $result))' '148 149 commandline -f repaint150 end151 100 unmodified linesmodified:shell/update.sh+12 added-14 removed
6 unmodified lines7891011111213141513141516171819201819202122222324252627282930252627282930313236 unmodified lines6 unmodified linesdir=${0%"${0##*/}"}
update() { { sed -n "1,/^#----BEGIN INCLUDE $1/p" "$2" sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1" cat << EOF# NOTE: Do not directly edit this section, which is copied from "$1".# To modify it, one can edit "$1" and run "./update.sh" to apply# the changes. See code comments in "$1" for the implementation details.# NOTE: Do not directly edit this section, which is copied from "common.sh".# To modify it, one can edit "common.sh" and run "./update.sh" to apply# the changes. See code comments in "common.sh" for the implementation details.EOF echo grep -v '^[[:blank:]]*#' "$dir/$1" # remove code comments from the common file sed -n '/^#----END INCLUDE/,$p' "$2" } > "$2.part" grep -v '^[[:blank:]]*#' "$dir/common.sh" # remove code comments in common.sh sed -n '/^#----END INCLUDE/,$p' "$1" } > "$1.part"
mv -f "$2.part" "$2" mv -f "$1.part" "$1"}
update "common.sh" "$dir/completion.bash"update "common.sh" "$dir/completion.zsh"update "common.sh" "$dir/key-bindings.bash"update "common.sh" "$dir/key-bindings.zsh"update "common.fish" "$dir/completion.fish"update "common.fish" "$dir/key-bindings.fish"update "$dir/completion.bash"update "$dir/completion.zsh"update "$dir/key-bindings.bash"update "$dir/key-bindings.zsh"
# Check if --check is in ARGVcheck=0rest=()36 unmodified linesmodified:test/lib/common.fish+1 added-2 removed
1 unmodified line2 set -e FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS FZF_DEFAULT_OPTS_FILE FZF_TMUX FZF_TMUX_OPTS3 set -e FZF_CTRL_T_COMMAND FZF_CTRL_T_OPTS FZF_ALT_C_COMMAND FZF_ALT_C_OPTS FZF_CTRL_R_OPTS4 set -e FZF_API_KEY5 # Unset completion-specific variables6-set -e FZF_COMPLETION_TRIGGER FZF_COMPLETION_OPTS6+set -e FZF_COMPLETION_OPTS FZF_EXPANSION_OPTS7 8 set -gx FZF_DEFAULT_OPTS "--no-scrollbar --pointer '>' --marker '>'"9-set -gx FZF_COMPLETION_TRIGGER '++'9 set -gx fish_history fzf_test10 11 # Add fzf to PATH12 fish_add_path <%= BASE %>/bin4 unmodified linesmodified:README.md+54 added-36 removed
745 unmodified lines746 ```747 748 ### Fuzzy completion for fish749 750-(Available in 0.68.0 or later)751-750 Fuzzy completion for fish differs from bash and zsh in that:751 752 - It doesn't require a trigger sequence like `**`. Instead, if activates755- on `Shift-TAB`, while `TAB` preserves fish's native completion behavior.753+ on `Shift-TAB` (replacing the native pager search mode), while `TAB` preserves754+ fish's native completion behavior.755 - It relies on fish's native completion system to populate the candidate list,756 rather than performing a recursive file system traversal. For recursive757 searching, use the `CTRL-T` binding instead.758+- If the current command line token is a wildcard pattern, it performs search on759+ the wildcard expansion path list (instead of the native behavior of inserting760+ all the results in command line). Because the shell is used for the expansion,761+ there is a limit in the number of results.759-- The only supported configuration variable is `FZF_COMPLETION_OPTS`.762+- The only supported configuration variables are `FZF_COMPLETION_OPTS` and763+ `FZF_EXPANSION_OPS`.764+- The function that is used by custom completion functions is named765+ `fzf_complete`, which only accepts fzf options as arguments, and can be also766+ called without any redirected input, to just modify fzf options while767+ presenting the native completion results. For compatibility with other shells,768+ a function named `_fzf_complete` is provided, that can accept ` -- $argv` in769+ its command line arguments, after fzf options.770 761-That said, just like in bash and zsh, you can implement custom completion for762-a specific command by defining an `_fzf_complete_COMMAND` function. For example:771+For commands that are not covered by fish completions, it is better to create772+regular fish completion functions (which will work for both `TAB` and773+`Shift-TAB`), and create fzf completion functions only when needing to modify774+fzf options for a specific command or want different results for `Shift-TAB`:775 776 ```fish765-function _fzf_complete_foo766- function _fzf_complete_foo_post767- awk '{print $NF}'777+# Customize git completion778+function _fzf_complete_git779+ # Show header text with active branch for all git completions780+ set -lx -- FZF_COMPLETION_OPTS --header="'"(git branch --show-current 2>/dev/null)"'"781+782+ # No other changes when less than 3 arguments, or when completing options783+ if not set -q argv[3]; or string match -q -- '-*' $argv[-1]784+ fzf_complete785+ return786 end769- _fzf_complete --multi --reverse --header-lines=3 -- $argv < (ls -al | psub)787 771- functions -e _fzf_complete_foo_post788+ # Check subcommand789+ switch $argv[2]790+ case checkout diff log show791+ # Set preview and display all branches and commits for subcommands: checkout, diff, log, show792+ begin793+ git branch --all --format='%(refname:short)'794+ git log --all --oneline --color=always795+ end | fzf_complete --no-multi --ansi --accept-nth=1 --query=$argv[-1] --preview='git show --color=always {1}'796+797+ case add rm mv798+ # Only set preview for subcommands: add, rm, mv799+ # Special characters in fish completion lists are escaped, so the r flag must be used.800+ fzf_complete --preview="git diff --color=always {r1}"801+802+ case '*'803+ # No changes for other subcommands804+ fzf_complete805+ end806 end807 ```808 775-And here's a more complex example for customizing `git`809+Similar to bash and zsh, the output of fzf can be processed before inserted in810+command line, by defining a function named `_fzf_complete_COMMAND_post` or811+`_fzf_post_complete_COMMAND`:812 813 ```fish778-function _fzf_complete_git779- switch $argv[2]780- case checkout switch781- _fzf_complete --reverse --no-preview -- $argv < (git branch --all --format='%(refname:short)' | psub)814+function _fzf_complete_foo815+ ls -sh --zero --color=always | fzf_complete --read0 --print0 --ansi --no-multi-line --header-lines=1816+end817 783- case add784- function _fzf_complete_git_post785- awk '{print $NF}'786- end787- _fzf_complete --multi --reverse -- $argv < (git status --short | psub)788-789- case show log diff790- function _fzf_complete_git_post791- awk '{print $1}'792- end793- _fzf_complete --reverse --no-sort --preview='git show --color=always {1}' -- $argv < (git log --oneline | psub)794-795- case ''796- __fzf_complete_native "$argv[1] " --query=(commandline -t | string escape)797-798- case '*'799- set -l -- current_token (commandline -t)800- __fzf_complete_native "$argv $current_token" --query=(string escape -- $current_token) --multi818+function _fzf_post_complete_foo819+ while read -lz result820+ string escape -n -- $result | string trim -l -c '\ ' | string split -m 1 -f 2 ' '821 end802-803- functions -e _fzf_complete_git_post822 end823 ```824 825 Vim plugin286 unmodified lines