completion.fish
This file
Path
shell/completion.fish
167 lines · 6.1 KiB
Last changed by
fish: Optimize description alignment of completions (#4912)bitraidbefore adoptiona1e983b
History of this fileTake it
1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ completion.fish6#7# - $FZF_COMPLETION_OPTS8# - $FZF_EXPANSION_OPTS910# 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.13if string match -qr -- '^[12]\\.|^3\\.[0-3]' $version14 echo "fzf completion script requires fish version 3.4.0 or newer." >&215 return 116else if not command -q fzf17 echo "fzf was not found in path." >&218 return 119end2021function 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 end2728 # 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 -- $token3132 # Remove any line breaks from token33 set -- token (string replace -ra -- '\\\\\\n' '' $token | string collect)3435 # regex: Match token with unescaped/unquoted glob character36 set -l -- r_glob '^(?:[^\'"\\\\*]|\\\\[\\S\\s]|\'(?:\\\\[\\S\\s]|[^\'\\\\])*\'|"(?:\\\\[\\S\\s]|[^"\\\\])*")*\\*[\\S\\s]*$'3738 # regex: Match any unbalanced quote character39 set -l -- r_quote '^(?>(?:\\\\[\\s\\S]|"(?:[^"\\\\]|\\\\[\\s\\S])*"|\'(?:[^\'\\\\]|\\\\[\\s\\S])*\'|[^\'"\\\\]+)*)\\K[\'"]'4041 # 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)4344 set -l -- cl_tokenize_opt '--tokens-expanded'45 string match -q -- '3.*' $version46 and set -- cl_tokenize_opt '--tokenize'4748 # 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 ''5455 # Set fzf options56 test -z "$FZF_TMUX_HEIGHT"57 and set -l -- FZF_TMUX_HEIGHT 40%5859 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)\\ -c6667 set -lx FZF_DEFAULT_OPTS_FILE6869 set -l -- fzf_cmd fzf70 test "$FZF_TMUX" = 171 and set -- fzf_cmd fzf-tmux $FZF_TMUX_OPTS -d$FZF_TMUX_HEIGHT --7273 set -l result7475 # 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]_post8081 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 end8889 # 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'9697 set -- result (string escape -n -- ($fzf_cmd --read0 --print0 --scheme=path --no-multi-line | string split0))9899 # 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 end108109 # 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 end115116 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 end124125 # Determine the tabstop length for description alignment126 set -l -- max_columns (math $COLUMNS - 40)127 for len in (string match -r -- '^[^\\t]*(?=\\t)' $list[1..500] | string length -V)128 test "$len" -gt "$tabstop" -a "$len" -lt "$max_columns"129 and set -- tabstop $len130 end131 set -- tabstop (math $tabstop + 4)132133 set -- result (string collect -- $list | $fzf_cmd --delimiter="\t" --tabstop=$tabstop --wrap-sign=\t"↳ " --accept-nth=1)134 end135 end136137 # Update command line138 if test -n "$result"139 # No extra space after single selection that ends with path separator140 set -l -- tail ' '141 test (count $result) -eq 1142 and string match -q -- '*/' "$result"143 and set -- tail ''144145 commandline -rt -- (string join -- ' ' $result)$tail146 end147148 commandline -f repaint149end150151function _fzf_complete152 set -l fzf_args153 for i in $argv154 string match -q -- '--' $i; and break155 set -a -- fzf_args $i156 end157 fzf_complete $fzf_args158end159160# Bind to shift-tab161if string match -qr -- '^\\d\\d+|^[4-9]' $version162 bind shift-tab fzf_complete163 bind -M insert shift-tab fzf_complete164else165 bind -k btab fzf_complete166 bind -M insert -k btab fzf_complete167end