[fish] Accept starting dir for <M-c> key binding (#944)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This also modifies <C-t> behaviour. The longest file path in the input is used as root directory for `find` command. The remainder of the input is passed to fzf's --query as a initial search parameters.
authored by John Nguyen · committed by Junegunn Choi · parent dbcaec5 · Browse files · 1 file·+62 added−13 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+62 added−13 removed
- other:+62 added−13 removed
Provenance
- Commit
70cfa6a- Tree
e98dad6- Author
John Nguyen <ipwnponies@users.noreply.github.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
dbcaec5
1 file changed+62 added−13 removed
modified:shell/key-bindings.fish+62 added-13 removed
2 unmodified lines3 function fzf_key_bindings4 5 # Store current token in $dir as root for the 'find' command6 function fzf-file-widget -d "List files and folders"7- set -l dir (commandline -t)8- # The commandline token might be escaped, we need to unescape it.9- set dir (eval "printf '%s' $dir")10- if [ ! -d "$dir" ]11- set dir .12- end13- # Some 'find' versions print undesired duplicated slashes if the path ends with slashes.14- set dir (string replace --regex '(.)/+$' '$1' "$dir")7+ set -l commandline (__fzf_parse_commandline)8+ set -l dir $commandline[1]9+ set -l fzf_query $commandline[2]10 11 # "-path \$dir'*/\\.*'" matches hidden files/folders inside $dir but not12 # $dir itself, even if hidden.13 set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "4 unmodified lines18 19 set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%20 begin21 set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS"27- eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m" | while read -l r; set result $result $r; end22+ eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)' -m --query "'$fzf_query'"' | while read -l r; set result $result $r; end23 end24 if [ -z "$result" ]25 commandline -f repaint26 return18 unmodified lines45 commandline -f repaint46 end47 48 function fzf-cd-widget -d "Change directory"49+ set -l commandline (__fzf_parse_commandline)50+ set -l dir $commandline[1]51+ set -l fzf_query $commandline[2]52+53 set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "55- command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \56- -o -type d -print 2> /dev/null | cut -b3-"54+ command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \55+ -o -type d -print 2> /dev/null | sed 's@^\./@@'"56 set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%57 begin58 set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS"60- eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m" | read -l result61- [ "$result" ]; and cd $result59+ eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)' +m --query "'$fzf_query'"' | read -l result60+61+ if [ -n "$result" ]62+ cd $result63+64+ # Remove last token from commandline.65+ commandline -t ""66+ end67 end68+69 commandline -f repaint70 end71 72 function __fzfcmd14 unmodified lines87 bind -M insert \ct fzf-file-widget88 bind -M insert \cr fzf-history-widget89 bind -M insert \ec fzf-cd-widget90 end91+92+ function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath and rest of token'93+ # eval is used to do shell expansion on paths94+ set -l commandline (eval "printf '%s' "(commandline -t))95+96+ if [ -z $commandline ]97+ # Default to current directory with no --query98+ set dir '.'99+ set fzf_query ''100+ else101+ set dir (__fzf_get_dir $commandline)102+103+ if [ "$dir" = "." -a (string sub -l 1 $commandline) != '.' ]104+ # if $dir is "." but commandline is not a relative path, this means no file path found105+ set fzf_query $commandline106+ else107+ # Also remove trailing slash after dir, to "split" input properly108+ set fzf_query (string replace -r "^$dir/?" '' "$commandline")109+ end110+ end111+112+ echo $dir113+ echo $fzf_query114+ end115+116+ function __fzf_get_dir -d 'Find the longest existing filepath from input string'117+ set dir $argv118+119+ # Strip all trailing slashes. Ignore if $dir is root dir (/)120+ if [ (string length $dir) -gt 1 ]121+ set dir (string replace -r '/*$' '' $dir)122+ end123+124+ # Iteratively check if dir exists and strip tail end of path125+ while [ ! -d "$dir" ]126+ # If path is absolute, this can keep going until ends up at /127+ # If path is relative, this can keep going until entire input is consumed, dirname returns "."128+ set dir (dirname "$dir")129+ end130+131+ echo $dir132+ end133+134 end