2015-03-06 18:51:50 +09:00
#!/usr/bin/env bash
2015-03-07 09:08:41 +09:00
# fzf-tmux: starts fzf in a tmux pane
2020-03-31 22:18:09 +09:00
# usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]
2015-03-06 18:51:50 +09:00
2016-07-10 14:28:58 +09:00
fail() {
>& 2 echo " $1 "
exit 2
}
fzf = " $( command -v fzf 2> /dev/null) " || fzf = " $( dirname " $0 " ) /fzf"
[[ -x " $fzf " ]] || fail 'fzf executable not found'
2015-03-06 18:51:50 +09:00
args =()
opt = ""
2015-03-08 16:36:37 +09:00
skip = ""
2015-03-09 02:50:43 +09:00
swap = ""
close = ""
2015-03-10 01:41:35 +09:00
term = ""
2017-08-31 22:16:00 -04:00
[[ -n " $LINES " ]] && lines = $LINES || lines = $( tput lines) || lines = $( tmux display-message -p "#{pane_height}" )
[[ -n " $COLUMNS " ]] && columns = $COLUMNS || columns = $( tput cols) || columns = $( tmux display-message -p "#{pane_width}" )
2016-07-10 14:41:06 +09:00
help() {
2020-03-31 22:18:09 +09:00
>& 2 echo 'usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]
2016-07-10 14:41:06 +09:00
2020-03-31 22:18:09 +09:00
LAYOUT OPTIONS:
(default layout: -d 50%)
2016-07-10 14:41:06 +09:00
2020-03-31 22:18:09 +09:00
Popup window (requires tmux 3.2 or above):
-p [WIDTH[%][,HEIGHT[%]]] (default: 50%)
-w WIDTH[%]
-h HEIGHT[%]
-x COL
-y ROW
Split pane:
-u [HEIGHT[%]] Split above (up)
-d [HEIGHT[%]] Split below (down)
-l [WIDTH[%]] Split left
-r [WIDTH[%]] Split right
2016-07-10 14:41:06 +09:00
'
exit
}
while [[ $# -gt 0 ]] ; do
2015-03-06 18:51:50 +09:00
arg = " $1 "
2016-07-10 14:41:06 +09:00
shift
[[ -z " $skip " ]] && case " $arg " in
2015-03-10 01:41:35 +09:00
-)
term = 1
;;
2016-07-10 14:41:06 +09:00
--help)
help
;;
--version)
echo "fzf-tmux (with fzf $( " $fzf " --version) )"
exit
;;
2020-03-31 22:18:09 +09:00
-p*| -w*| -h*| -x*| -y*| -d*| -u*| -r*| -l*)
if [[ " $arg " = ~ ^-[ pwhxy] ]] ; then
2022-08-06 19:01:08 +09:00
[[ " $opt " = ~ "-E" ]] || opt = "-E"
2020-03-31 22:18:09 +09:00
elif [[ " $arg " = ~ ^.[ lr] ]] ; then
2015-03-09 02:50:43 +09:00
opt = "-h"
if [[ " $arg " = ~ ^.l ]] ; then
opt = " $opt -d"
swap = "; swap-pane -D ; select-pane -L"
close = "; tmux swap-pane -D"
fi
else
opt = ""
if [[ " $arg " = ~ ^.u ]] ; then
opt = " $opt -d"
swap = "; swap-pane -D ; select-pane -U"
close = "; tmux swap-pane -D"
fi
fi
2016-07-10 14:41:06 +09:00
if [[ ${# arg } -gt 2 ]] ; then
2015-03-06 18:51:50 +09:00
size = " ${ arg : 2 } "
else
2020-04-07 09:55:48 +09:00
if [[ " $1 " = ~ ^[ 0-9%,] +$ ]] || [[ " $1 " = ~ ^[ A-Z] $ ]] ; then
2015-03-08 15:08:27 +09:00
size = " $1 "
2015-03-08 16:39:30 +09:00
shift
2016-07-10 14:41:06 +09:00
else
2015-03-08 15:08:27 +09:00
continue
fi
2015-03-06 18:51:50 +09:00
fi
2015-03-09 02:50:43 +09:00
2020-03-31 22:18:09 +09:00
if [[ " $arg " = ~ ^-p ]] ; then
if [[ -n " $size " ]] ; then
w = ${ size %%,* }
h = ${ size ##*, }
opt = " $opt -w $w -h $h "
fi
elif [[ " $arg " = ~ ^-[ whxy] ]] ; then
opt = " $opt ${ arg : 0 : 2 } $size "
elif [[ " $size " = ~ %$ ]] ; then
2015-03-09 02:50:43 +09:00
size = ${ size : 0 :(( ${# size } -1)) }
2016-07-10 14:41:06 +09:00
if [[ -n " $swap " ]] ; then
2015-03-09 02:50:43 +09:00
opt = " $opt -p $(( 100 - size )) "
else
opt = " $opt -p $size "
fi
else
2016-07-10 14:41:06 +09:00
if [[ -n " $swap " ]] ; then
2015-03-10 01:41:35 +09:00
if [[ " $arg " = ~ ^.l ]] ; then
2016-10-12 23:09:21 +09:00
max = $columns
2015-03-10 01:41:35 +09:00
else
2016-04-24 13:32:33 +09:00
max = $lines
2015-03-10 01:41:35 +09:00
fi
2015-03-09 02:50:43 +09:00
size = $(( max - size ))
2016-07-10 14:41:06 +09:00
[[ $size -lt 0 ]] && size = 0
2015-03-09 02:50:43 +09:00
opt = " $opt -l $size "
else
opt = " $opt -l $size "
fi
fi
2015-03-06 18:51:50 +09:00
;;
--)
# "--" can be used to separate fzf-tmux options from fzf options to
# avoid conflicts
2015-03-08 16:36:37 +09:00
skip = 1
2016-07-10 14:41:06 +09:00
continue
2015-03-06 18:51:50 +09:00
;;
*)
2016-07-10 14:41:06 +09:00
args +=( " $arg " )
2015-03-06 18:51:50 +09:00
;;
esac
2016-07-10 14:41:06 +09:00
[[ -n " $skip " ]] && args +=( " $arg " )
2015-03-06 18:51:50 +09:00
done
2020-04-01 23:43:35 +09:00
if [[ -z " $TMUX " ]] ; then
2016-07-10 14:34:29 +09:00
" $fzf " " ${ args [@] } "
2015-03-06 18:51:50 +09:00
exit $?
fi
2021-01-01 22:43:36 +09:00
# --height option is not allowed. CTRL-Z is also disabled.
args =( " ${ args [@] } " "--no-height" "--bind=ctrl-z:ignore" )
2017-01-08 01:30:31 +09:00
2020-05-23 20:47:43 +09:00
# Handle zoomed tmux pane without popup options by moving it to a temp window
2022-08-06 19:01:08 +09:00
if [[ ! " $opt " = ~ "-E" ]] && tmux list-panes -F '#F' | grep -q Z; then
2020-05-23 20:47:43 +09:00
zoomed_without_popup = 1
2015-12-07 17:45:22 -08:00
original_window = $( tmux display-message -p "#{window_id}" )
2017-04-01 11:09:46 +09:00
tmp_window = $( tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - '\\;' do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'" )
2015-12-07 17:45:22 -08:00
tmux swap-pane -t $tmp_window \; select -window -t $tmp_window
fi
2015-03-06 18:51:50 +09:00
set -e
# Clean up named pipes on exit
id = $RANDOM
2015-10-05 13:01:09 +09:00
argsf = " ${ TMPDIR :- /tmp } /fzf-args- $id "
fifo1 = " ${ TMPDIR :- /tmp } /fzf-fifo1- $id "
fifo2 = " ${ TMPDIR :- /tmp } /fzf-fifo2- $id "
fifo3 = " ${ TMPDIR :- /tmp } /fzf-fifo3- $id "
2023-08-24 19:31:38 +12:00
if tmux_win_opts = $( tmux show-options -p remain-on-exit \; show-options -p synchronize-panes 2> /dev/null) ; then
tmux_win_opts =( $( sed '/ off/d; s/synchronize-panes/set-option -p synchronize-panes/; s/remain-on-exit/set-option -p remain-on-exit/; s/$/ \\;/' <<< " $tmux_win_opts " ) )
tmux_off_opts = '; set-option -p synchronize-panes off ; set-option -p remain-on-exit off'
else
tmux_win_opts =( $( tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/' ) )
tmux_off_opts = '; set-window-option synchronize-panes off ; set-window-option remain-on-exit off'
fi
2015-03-06 18:51:50 +09:00
cleanup() {
2017-06-04 23:24:57 +09:00
\r m -f $argsf $fifo1 $fifo2 $fifo3
2015-12-07 17:45:22 -08:00
2018-05-03 20:25:15 +01:00
# Restore tmux window options
2023-10-13 20:13:28 +09:00
if [[ " ${# tmux_win_opts [@] } " -gt 1 ]] ; then
2020-10-09 22:03:35 +09:00
eval "tmux ${ tmux_win_opts [*] } "
2018-05-03 20:25:15 +01:00
fi
2020-05-23 20:47:43 +09:00
# Remove temp window if we were zoomed without popup options
if [[ -n " $zoomed_without_popup " ]] ; then
2017-02-08 14:55:17 +09:00
tmux display-message -p "#{window_id}" > /dev/null
2015-12-07 17:45:22 -08:00
tmux swap-pane -t $original_window \; \
select -window -t $original_window \; \
kill-window -t $tmp_window \; \
resize-pane -Z
fi
2017-04-03 11:49:54 +09:00
2021-08-14 11:33:21 +00:00
if [[ $# -gt 0 ]] ; then
2017-08-26 02:51:19 +09:00
trap - EXIT
2017-04-03 11:49:54 +09:00
exit 130
fi
2015-03-06 18:51:50 +09:00
}
2017-04-03 11:49:54 +09:00
trap 'cleanup 1' SIGUSR1
2017-06-04 23:24:57 +09:00
trap 'cleanup' EXIT
2015-03-06 18:51:50 +09:00
2020-10-09 22:03:35 +09:00
envs = "export TERM= $TERM "
2022-08-06 18:28:45 -07:00
if [[ " $opt " = ~ "-E" ]] ; then
2023-04-02 00:16:00 +09:00
tmux_version = $( tmux -V | sed 's/[^0-9.]//g' )
2023-04-11 16:29:29 +09:00
if [[ $( awk '{print ($1 > 3.2)}' <<< " $tmux_version " 2> /dev/null || bc -l <<< " $tmux_version > 3.2" ) = 1 ]] ; then
2022-08-06 18:28:45 -07:00
FZF_DEFAULT_OPTS = "--border $FZF_DEFAULT_OPTS "
opt = "-B $opt "
2023-04-02 00:16:00 +09:00
elif [[ $tmux_version = 3.2 ]] ; then
FZF_DEFAULT_OPTS = "--margin 0,1 $FZF_DEFAULT_OPTS "
else
echo "fzf-tmux: tmux 3.2 or above is required for popup mode" >& 2
exit 2
2022-08-06 18:28:45 -07:00
fi
fi
2016-07-10 14:34:29 +09:00
[[ -n " $FZF_DEFAULT_OPTS " ]] && envs = " $envs FZF_DEFAULT_OPTS= $( printf %q " $FZF_DEFAULT_OPTS " ) "
[[ -n " $FZF_DEFAULT_COMMAND " ]] && envs = " $envs FZF_DEFAULT_COMMAND= $( printf %q " $FZF_DEFAULT_COMMAND " ) "
2023-08-03 16:47:19 +09:00
[[ -n " $RUNEWIDTH_EASTASIAN " ]] && envs = " $envs RUNEWIDTH_EASTASIAN= $( printf %q " $RUNEWIDTH_EASTASIAN " ) "
2023-05-31 20:12:51 +09:00
[[ -n " $BAT_THEME " ]] && envs = " $envs BAT_THEME= $( printf %q " $BAT_THEME " ) "
2020-10-09 22:03:35 +09:00
echo " $envs ;" > " $argsf "
2015-04-20 22:42:02 +09:00
# Build arguments to fzf
2020-10-09 22:03:35 +09:00
opts = $( printf "%q " " ${ args [@] } " )
2015-04-20 22:42:02 +09:00
2017-04-03 11:49:54 +09:00
pppid = $$
2020-10-09 22:03:35 +09:00
echo -n "trap 'kill -SIGUSR1 - $pppid ' EXIT SIGINT SIGTERM;" >> $argsf
2017-08-26 02:51:19 +09:00
close = "; trap - EXIT SIGINT SIGTERM $close "
2017-04-03 11:49:54 +09:00
2020-10-09 22:03:35 +09:00
export TMUX = $( cut -d , -f 1,2 <<< " $TMUX " )
mkfifo -m o+w $fifo2
2022-08-06 19:01:08 +09:00
if [[ " $opt " = ~ "-E" ]] ; then
2020-03-31 22:18:09 +09:00
cat $fifo2 &
if [[ -n " $term " ]] || [[ -t 0 ]] ; then
cat <<< "\" $fzf \" $opts > $fifo2 ; out=\$? $close ; exit \$out" >> $argsf
else
mkfifo $fifo1
cat <<< "\" $fzf \" $opts < $fifo1 > $fifo2 ; out=\$? $close ; exit \$out" >> $argsf
cat <& 0 > $fifo1 &
fi
2021-03-08 12:44:36 +09:00
2022-08-08 13:39:37 +09:00
tmux popup -d " $PWD " $opt "bash $argsf " > /dev/null 2>& 1
2020-03-31 22:18:09 +09:00
exit $?
fi
2020-10-09 22:03:35 +09:00
mkfifo -m o+w $fifo3
2016-07-10 14:34:29 +09:00
if [[ -n " $term " ]] || [[ -t 0 ]] ; then
2017-08-26 02:51:19 +09:00
cat <<< "\" $fzf \" $opts > $fifo2 ; echo \$? > $fifo3 $close " >> $argsf
2015-03-06 18:51:50 +09:00
else
mkfifo $fifo1
2017-08-26 02:51:19 +09:00
cat <<< "\" $fzf \" $opts < $fifo1 > $fifo2 ; echo \$? > $fifo3 $close " >> $argsf
2015-03-06 18:51:50 +09:00
cat <& 0 > $fifo1 &
fi
2023-08-24 19:31:38 +12:00
tmux \
2022-08-08 13:39:37 +09:00
split-window -c " $PWD " $opt "bash -c 'exec -a fzf bash $argsf '" $swap \
2023-08-24 19:31:38 +12:00
$tmux_off_opts \
2020-10-09 22:03:35 +09:00
> /dev/null 2>& 1 || { " $fzf " " ${ args [@] } " ; exit $? ; }
2015-03-06 18:51:50 +09:00
cat $fifo2
2015-09-18 10:26:04 +09:00
exit " $( cat $fifo3 ) "