Skip to content

Commit b879044

Browse files
committed
[fish] Improve option prefix processing
- Support single-letter options without = such as -fFILEPATH - fish v3.3.0 and newer: Disable option prefix if -- is preceded
1 parent 84aff1b commit b879044

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

shell/key-bindings.fish

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,30 @@ function fzf_key_bindings
5151
set -l -- fish_major (string match -r -- '^\d+' $version)
5252
set -l -- fish_minor (string match -r -- '^\d+\.(\d+)' $version)[2]
5353

54+
set -l -- match_regex '(?<fzf_query>[\s\S]*?(?=\n?$)$)'
55+
set -l -- prefix_regex '^-[^\s=]+=|^-[^\s]'
56+
57+
# fish v3.3.0 and newer: Don't use option prefix if " -- " is preceded.
58+
if test \( "$fish_major" -ge 4 \) -o \( "$fish_major" -eq 3 -a "$fish_minor" -ge 3 \)
59+
if string match -q -v -- '* -- *' (string sub -l (commandline -Cp) -- (commandline -p))
60+
set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"
61+
end
62+
else
63+
set -- match_regex "(?<prefix>$prefix_regex)?$match_regex"
64+
end
65+
5466
# Set $prefix and expanded $fzf_query with preserved trailing newlines.
5567
if test "$fish_major" -ge 4
5668
# fish v4.0.0 and newer
57-
string match -q -r -- '(?<prefix>^-[^\s=]+=)?(?<fzf_query>[\s\S]*?(?=\n?$)$)' \
58-
(commandline --current-token --tokens-expanded | string collect -N)
69+
string match -q -r -- $match_regex (commandline --current-token --tokens-expanded | string collect -N)
5970
else if test "$fish_major" -eq 3 -a "$fish_minor" -ge 2
6071
# fish v3.2.0 - v3.7.1 (last v3)
61-
string match -q -r -- '(?<prefix>^-[^\s=]+=)?(?<fzf_query>[\s\S]*?(?=\n?$)$)' \
62-
(commandline --current-token --tokenize | string collect -N)
72+
string match -q -r -- $match_regex (commandline --current-token --tokenize | string collect -N)
6373
eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)' '')
6474
else
6575
# fish older than v3.2.0 (v3.1b1 - v3.1.2)
6676
set -l -- cl_token (commandline --current-token --tokenize | string collect -N)
67-
set -- prefix (string match -r -- '^-[^\s=]+=' $cl_token)
77+
set -- prefix (string match -r -- $prefix_regex $cl_token)
6878
set -- fzf_query (string replace -- "$prefix" '' $cl_token | string collect -N)
6979
eval set -- fzf_query (string escape -n -- $fzf_query | string replace -r -a '^\\\(?=~)|\\\(?=\$\w)|\\\n\\\n$' '')
7080
end

0 commit comments

Comments
 (0)