2015-03-13 07:08:42 +03:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
|
|
|
function fzf_key_bindings
|
|
|
|
|
2017-02-16 11:18:01 +03:00
|
|
|
# Store current token in $dir as root for the 'find' command
|
2016-12-14 09:37:27 +03:00
|
|
|
function fzf-file-widget -d "List files and folders"
|
2017-06-25 15:16:15 +03:00
|
|
|
set -l commandline (__fzf_parse_commandline)
|
|
|
|
set -l dir $commandline[1]
|
|
|
|
set -l fzf_query $commandline[2]
|
2015-03-13 07:08:42 +03:00
|
|
|
|
2016-12-14 09:37:27 +03:00
|
|
|
# "-path \$dir'*/\\.*'" matches hidden files/folders inside $dir but not
|
|
|
|
# $dir itself, even if hidden.
|
2015-06-25 19:00:58 +03:00
|
|
|
set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND "
|
2017-02-18 19:33:13 +03:00
|
|
|
command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \
|
2016-12-14 09:37:27 +03:00
|
|
|
-o -type f -print \
|
|
|
|
-o -type d -print \
|
2017-06-10 07:34:49 +03:00
|
|
|
-o -type l -print 2> /dev/null | sed 's@^\./@@'"
|
2016-12-14 09:37:27 +03:00
|
|
|
|
2017-01-15 10:15:51 +03:00
|
|
|
set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%
|
|
|
|
begin
|
|
|
|
set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS"
|
2017-06-25 15:16:15 +03:00
|
|
|
eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)' -m --query "'$fzf_query'"' | while read -l r; set result $result $r; end
|
2017-01-15 10:15:51 +03:00
|
|
|
end
|
2016-12-14 09:37:27 +03:00
|
|
|
if [ -z "$result" ]
|
|
|
|
commandline -f repaint
|
|
|
|
return
|
2017-06-12 12:24:45 +03:00
|
|
|
else
|
2016-12-14 09:37:27 +03:00
|
|
|
# Remove last token from commandline.
|
|
|
|
commandline -t ""
|
|
|
|
end
|
|
|
|
for i in $result
|
|
|
|
commandline -it -- (string escape $i)
|
|
|
|
commandline -it -- ' '
|
|
|
|
end
|
2015-04-21 18:30:09 +03:00
|
|
|
commandline -f repaint
|
2015-03-13 07:08:42 +03:00
|
|
|
end
|
|
|
|
|
2016-12-14 09:37:27 +03:00
|
|
|
function fzf-history-widget -d "Show command history"
|
2017-01-15 10:15:51 +03:00
|
|
|
set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%
|
|
|
|
begin
|
2017-03-03 06:20:01 +03:00
|
|
|
set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m"
|
2017-07-15 12:50:23 +03:00
|
|
|
|
|
|
|
set -l FISH_MAJOR (echo $FISH_VERSION | cut -f1 -d.)
|
|
|
|
set -l FISH_MINOR (echo $FISH_VERSION | cut -f2 -d.)
|
|
|
|
|
|
|
|
# history's -z flag is needed for multi-line support.
|
|
|
|
# history's -z flag was added in fish 2.4.0, so don't use it for versions
|
|
|
|
# before 2.4.0.
|
|
|
|
if [ "$FISH_MAJOR" -gt 2 -o \( "$FISH_MAJOR" -eq 2 -a "$FISH_MINOR" -ge 4 \) ];
|
|
|
|
history -z | eval (__fzfcmd) --read0 -q '(commandline)' | perl -pe 'chomp if eof' | read -lz result
|
|
|
|
and commandline -- $result
|
|
|
|
else
|
|
|
|
history | eval (__fzfcmd) -q '(commandline)' | read -l result
|
|
|
|
and commandline -- $result
|
|
|
|
end
|
2017-01-15 10:15:51 +03:00
|
|
|
end
|
2015-03-13 07:08:42 +03:00
|
|
|
commandline -f repaint
|
|
|
|
end
|
|
|
|
|
2016-12-14 09:37:27 +03:00
|
|
|
function fzf-cd-widget -d "Change directory"
|
2017-06-25 15:16:15 +03:00
|
|
|
set -l commandline (__fzf_parse_commandline)
|
|
|
|
set -l dir $commandline[1]
|
|
|
|
set -l fzf_query $commandline[2]
|
|
|
|
|
2015-11-07 18:11:46 +03:00
|
|
|
set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "
|
2017-06-25 15:16:15 +03:00
|
|
|
command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \
|
|
|
|
-o -type d -print 2> /dev/null | sed 's@^\./@@'"
|
2017-01-15 10:15:51 +03:00
|
|
|
set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%
|
|
|
|
begin
|
|
|
|
set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS"
|
2017-06-25 15:16:15 +03:00
|
|
|
eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)' +m --query "'$fzf_query'"' | read -l result
|
|
|
|
|
|
|
|
if [ -n "$result" ]
|
|
|
|
cd $result
|
|
|
|
|
|
|
|
# Remove last token from commandline.
|
|
|
|
commandline -t ""
|
|
|
|
end
|
2017-01-15 10:15:51 +03:00
|
|
|
end
|
2017-06-25 15:16:15 +03:00
|
|
|
|
2015-03-13 07:08:42 +03:00
|
|
|
commandline -f repaint
|
|
|
|
end
|
|
|
|
|
2015-04-21 18:55:39 +03:00
|
|
|
function __fzfcmd
|
2017-01-07 19:30:31 +03:00
|
|
|
set -q FZF_TMUX; or set FZF_TMUX 0
|
|
|
|
set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40%
|
2015-04-21 18:55:39 +03:00
|
|
|
if [ $FZF_TMUX -eq 1 ]
|
2017-01-07 19:30:31 +03:00
|
|
|
echo "fzf-tmux -d$FZF_TMUX_HEIGHT"
|
2015-03-13 07:08:42 +03:00
|
|
|
else
|
2017-01-15 10:15:51 +03:00
|
|
|
echo "fzf"
|
2015-03-13 07:08:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-24 07:55:48 +03:00
|
|
|
bind \ct fzf-file-widget
|
|
|
|
bind \cr fzf-history-widget
|
|
|
|
bind \ec fzf-cd-widget
|
2015-03-31 00:10:47 +03:00
|
|
|
|
|
|
|
if bind -M insert > /dev/null 2>&1
|
2016-04-24 07:55:48 +03:00
|
|
|
bind -M insert \ct fzf-file-widget
|
|
|
|
bind -M insert \cr fzf-history-widget
|
|
|
|
bind -M insert \ec fzf-cd-widget
|
2015-03-31 00:10:47 +03:00
|
|
|
end
|
2017-06-25 15:16:15 +03:00
|
|
|
|
|
|
|
function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath and rest of token'
|
|
|
|
# eval is used to do shell expansion on paths
|
|
|
|
set -l commandline (eval "printf '%s' "(commandline -t))
|
|
|
|
|
|
|
|
if [ -z $commandline ]
|
|
|
|
# Default to current directory with no --query
|
|
|
|
set dir '.'
|
|
|
|
set fzf_query ''
|
|
|
|
else
|
|
|
|
set dir (__fzf_get_dir $commandline)
|
|
|
|
|
|
|
|
if [ "$dir" = "." -a (string sub -l 1 $commandline) != '.' ]
|
|
|
|
# if $dir is "." but commandline is not a relative path, this means no file path found
|
|
|
|
set fzf_query $commandline
|
|
|
|
else
|
|
|
|
# Also remove trailing slash after dir, to "split" input properly
|
|
|
|
set fzf_query (string replace -r "^$dir/?" '' "$commandline")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
echo $dir
|
|
|
|
echo $fzf_query
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fzf_get_dir -d 'Find the longest existing filepath from input string'
|
|
|
|
set dir $argv
|
|
|
|
|
|
|
|
# Strip all trailing slashes. Ignore if $dir is root dir (/)
|
|
|
|
if [ (string length $dir) -gt 1 ]
|
|
|
|
set dir (string replace -r '/*$' '' $dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Iteratively check if dir exists and strip tail end of path
|
|
|
|
while [ ! -d "$dir" ]
|
|
|
|
# If path is absolute, this can keep going until ends up at /
|
|
|
|
# If path is relative, this can keep going until entire input is consumed, dirname returns "."
|
|
|
|
set dir (dirname "$dir")
|
|
|
|
end
|
|
|
|
|
|
|
|
echo $dir
|
|
|
|
end
|
|
|
|
|
2015-03-13 07:08:42 +03:00
|
|
|
end
|