| 1 |
# ____ ____ |
| 2 |
# / __/___ / __/ |
| 3 |
# / /_/_ / / /_ |
| 4 |
# / __/ / /_/ __/ |
| 5 |
# /_/ /___/_/ completion.fish |
| 6 |
# |
| 7 |
# - $FZF_COMPLETION_OPTS |
| 8 |
# - $FZF_EXPANSION_OPTS |
| 9 |
|
| 10 |
# The oldest supported fish version is 3.4.0. For this message being able to be |
| 11 |
# displayed on older versions, the command substitution syntax $() should not |
| 12 |
# be used anywhere in the script, otherwise the source command will fail. |
| 13 |
if string match -qr -- '^[12]\\.|^3\\.[0-3]' $version |
| 14 |
echo "fzf completion script requires fish version 3.4.0 or newer." >&2 |
| 15 |
return 1 |
| 16 |
else if not command -q fzf |
| 17 |
echo "fzf was not found in path." >&2 |
| 18 |
return 1 |
| 19 |
end |
| 20 |
|
| 21 |
function fzf_complete -w fzf -d 'fzf command completion and wildcard expansion search' |
| 22 |
# Restore the default shift-tab behavior on tab completions |
| 23 |
if commandline --paging-mode |
| 24 |
commandline -f complete-and-search |
| 25 |
return |
| 26 |
end |
| 27 |
|
| 28 |
# Remove any trailing unescaped backslash from token and update command line |
| 29 |
set -l -- token (string replace -r -- '(?<!\\\\)(?:\\\\\\\\)*\\K\\\\$' '' (commandline -t | string collect) | string collect) |
| 30 |
commandline -rt -- $token |
| 31 |
|
| 32 |
# Remove any line breaks from token |
| 33 |
set -- token (string replace -ra -- '\\\\\\n' '' $token | string collect) |
| 34 |
|
| 35 |
# regex: Match token with unescaped/unquoted glob character |
| 36 |
set -l -- r_glob '^(?:[^\'"\\\\*]|\\\\[\\S\\s]|\'(?:\\\\[\\S\\s]|[^\'\\\\])*\'|"(?:\\\\[\\S\\s]|[^"\\\\])*")*\\*[\\S\\s]*$' |
| 37 |
|
| 38 |
# regex: Match any unbalanced quote character |
| 39 |
set -l -- r_quote '^(?>(?:\\\\[\\s\\S]|"(?:[^"\\\\]|\\\\[\\s\\S])*"|\'(?:[^\'\\\\]|\\\\[\\s\\S])*\'|[^\'"\\\\]+)*)\\K[\'"]' |
| 40 |
|
| 41 |
# 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 |
|
| 44 |
set -l -- cl_tokenize_opt '--tokens-expanded' |
| 45 |
string match -q -- '3.*' $version |
| 46 |
and set -- cl_tokenize_opt '--tokenize' |
| 47 |
|
| 48 |
# Set command line tokens without any leading variable definitions or launcher |
| 49 |
# 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 |
|
| 55 |
# Set fzf options |
| 56 |
test -z "$FZF_TMUX_HEIGHT" |
| 57 |
and set -l -- FZF_TMUX_HEIGHT 40% |
| 58 |
|
| 59 |
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)\\ -c |
| 66 |
|
| 67 |
set -lx FZF_DEFAULT_OPTS_FILE |
| 68 |
|
| 69 |
set -l -- fzf_cmd fzf |
| 70 |
test "$FZF_TMUX" = 1 |
| 71 |
and set -- fzf_cmd fzf-tmux $FZF_TMUX_OPTS -d$FZF_TMUX_HEIGHT -- |
| 72 |
|
| 73 |
set -l result |
| 74 |
|
| 75 |
# Get the completion list from stdin when it's not a tty |
| 76 |
if not isatty stdin |
| 77 |
set -l -- custom_post_func _fzf_post_complete_$cmd[1] |
| 78 |
functions -q $custom_post_func |
| 79 |
or set -- custom_post_func _fzf_complete_$cmd[1]_post |
| 80 |
|
| 81 |
if functions -q $custom_post_func |
| 82 |
$fzf_cmd | $custom_post_func $cmd | while read -l r; set -a -- result $r; end |
| 83 |
else if string match -q -- '*--print0*' "$FZF_DEFAULT_OPTS" |
| 84 |
$fzf_cmd | while read -lz r; set -a -- result $r; end |
| 85 |
else |
| 86 |
$fzf_cmd | while read -l r; set -a -- result $r; end |
| 87 |
end |
| 88 |
|
| 89 |
# Wildcard expansion |
| 90 |
else if test -n "$glob_pattern" |
| 91 |
# Set the command to be run by fzf, so there is a visual indicator and an |
| 92 |
# 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 |
|
| 97 |
set -- result (string escape -n -- ($fzf_cmd --read0 --print0 --scheme=path --no-multi-line | string split0)) |
| 98 |
|
| 99 |
# Command completion |
| 100 |
else |
| 101 |
# Call custom function if defined |
| 102 |
set -l -- custom_func _fzf_complete_$cmd[1] |
| 103 |
if functions -q $custom_func; and not set -q __fzf_no_custom_complete |
| 104 |
set -lx __fzf_no_custom_complete |
| 105 |
$custom_func $cmd |
| 106 |
return |
| 107 |
end |
| 108 |
|
| 109 |
# Workaround for complete not having newlines in results |
| 110 |
if string match -qr -- '\\n' $token |
| 111 |
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 |
end |
| 115 |
|
| 116 |
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 value |
| 119 |
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 |
else |
| 122 |
set -- tabstop 4 |
| 123 |
end |
| 124 |
|
| 125 |
# Determine the tabstop length for description alignment |
| 126 |
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 $len |
| 130 |
end |
| 131 |
set -- tabstop (math $tabstop + 4) |
| 132 |
|
| 133 |
set -- result (string collect -- $list | $fzf_cmd --delimiter="\t" --tabstop=$tabstop --wrap-sign=\t"↳ " --accept-nth=1) |
| 134 |
end |
| 135 |
end |
| 136 |
|
| 137 |
# Update command line |
| 138 |
if test -n "$result" |
| 139 |
# No extra space after single selection that ends with path separator |
| 140 |
set -l -- tail ' ' |
| 141 |
test (count $result) -eq 1 |
| 142 |
and string match -q -- '*/' "$result" |
| 143 |
and set -- tail '' |
| 144 |
|
| 145 |
commandline -rt -- (string join -- ' ' $result)$tail |
| 146 |
end |
| 147 |
|
| 148 |
commandline -f repaint |
| 149 |
end |
| 150 |
|
| 151 |
function _fzf_complete |
| 152 |
set -l fzf_args |
| 153 |
for i in $argv |
| 154 |
string match -q -- '--' $i; and break |
| 155 |
set -a -- fzf_args $i |
| 156 |
end |
| 157 |
fzf_complete $fzf_args |
| 158 |
end |
| 159 |
|
| 160 |
# Bind to shift-tab |
| 161 |
if string match -qr -- '^\\d\\d+|^[4-9]' $version |
| 162 |
bind shift-tab fzf_complete |
| 163 |
bind -M insert shift-tab fzf_complete |
| 164 |
else |
| 165 |
bind -k btab fzf_complete |
| 166 |
bind -M insert -k btab fzf_complete |
| 167 |
end |