Skip to content
Myr

[fish] Improve commandline parsing

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Enable using unescaped quotes for exact-match, exact-boundary-match. - Enable suffix-exact-match. - Enable inverse-exact-match, inverse-prefix/suffix-exact-match. - Allow searching for double quotes and backslashes. - Combine multiple consecutive slashes into one. - Workaround for test command bug, allowing $dir or $commandline be a single `!`.

authored by bitraid · committed by Junegunn Choi · parent 961ae15 · Browse files · 1 file+22 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+22 added−12 removed

  • other:+22 added−12 removed

1 file+22 added−12 removed

modified:shell/key-bindings.fish+22 added-12 removed
34 unmodified lines
35 set -l prefix $commandline[3]3637 test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%38 begin39 set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path --walker-root='$dir'" "$FZF_CTRL_T_OPTS")39 set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path --walker-root=$dir" "$FZF_CTRL_T_OPTS")40 set -lx FZF_DEFAULT_COMMAND "$FZF_CTRL_T_COMMAND"41 set -lx FZF_DEFAULT_OPTS_FILE ''42 eval (__fzfcmd)' -m --query "'$fzf_query'"' | while read -l r; set result $result $r; end42 eval (__fzfcmd) -m --query=$fzf_query | while read -l r; set -a result $r; end43 end44 if [ -z "$result" ]45 commandline -f repaint46 return
50 unmodified lines
97 set -l prefix $commandline[3]9899 test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%100 begin101 set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path --walker-root='$dir'" "$FZF_ALT_C_OPTS")101 set -lx FZF_DEFAULT_OPTS (__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path --walker-root=$dir" "$FZF_ALT_C_OPTS")102 set -lx FZF_DEFAULT_OPTS_FILE ''103 set -lx FZF_DEFAULT_COMMAND "$FZF_ALT_C_COMMAND"104 eval (__fzfcmd)' +m --query "'$fzf_query'"' | read -l result104 eval (__fzfcmd) +m --query=$fzf_query | read -l result105106 if [ -n "$result" ]107 cd -- $result108
42 unmodified lines
151 # strip -option= from token if present152 set -l prefix (string match -r -- '^-[^\s=]+=' $commandline)153 set commandline (string replace -- "$prefix" '' $commandline)154155 # escape special characters, except for the $ sign of valid variable names,156 # so that after eval, the original string is returned, but with the157 # variable names replaced by their values.158 set commandline (string escape -n -- $commandline)159 set commandline (string replace -r -a '\x5c\$(?=[\w])' '\$' -- $commandline)160161 # eval is used to do shell expansion on paths162 eval set commandline $commandline163164 # Combine multiple consecutive slashes into one165 set commandline (string replace -r -a '/+' '/' -- $commandline)166167 if [ -z $commandline ]168 # Default to current directory with no --query169 set dir '.'170 set fzf_query ''171 else172 set dir (__fzf_get_dir $commandline)173165 if [ "$dir" = "." -a (string sub -l 1 -- $commandline) != '.' ]174 # BUG: on combined expressions, if a left argument is a single `!`, the175 # builtin test command of fish will treat it as the ! operator. To176 # overcome this, have the variable parts on the right.177 if test "." = "$dir" -a "." != (string sub -l 1 -- $commandline)178 # if $dir is "." but commandline is not a relative path, this means no file path found179 set fzf_query $commandline180 else181 # Also remove trailing slash after dir, to "split" input properly170 set fzf_query (string replace -r "^$dir/?" -- '' "$commandline")182 set fzf_query (string replace -r "^$dir/?" '' -- $commandline)183 end184 end185174 echo $dir175 echo $fzf_query186 echo (string escape -- $dir)187 echo (string escape -- $fzf_query)188 echo $prefix189 end190191 function __fzf_get_dir -d 'Find the longest existing filepath from input string'192 set dir $argv193182 # Strip all trailing slashes. Ignore if $dir is root dir (/)183 if [ (string length -- $dir) -gt 1 ]184 set dir (string replace -r '/*$' -- '' $dir)185 end194 # Strip trailing slash, unless $dir is root dir (/)195 set dir (string replace -r '(?<!^)/$' '' -- $dir)196197 # Iteratively check if dir exists and strip tail end of path198 while [ ! -d "$dir" ]199 # If path is absolute, this can keep going until ends up at /
8 unmodified lines