Skip to content
Myr

Simplify fzf-tmux script

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

# Should properly escape arguments FZF_DEFAULT_OPTS='--prompt "\$a`b\"c"' fzf-tmux --header $'$a\nb"c`d'

authored by Junegunn Choi · committed by Junegunn Choi · parent 8651448 · Browse files · 1 file+15 added−27 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+15 added−27 removed

  • other:+15 added−27 removed

1 file+15 added−27 removed

modified:bin/fzf-tmux+15 added-27 removed
134 unmodified lines
135 exit $?136fi137138# --height option is not allowed139args=("--no-height" "${args[@]}")139args=("${args[@]}" "--no-height")140141# Handle zoomed tmux pane without popup options by moving it to a temp window142if [[ ! "$opt" =~ "-K -E" ]] && tmux list-panes -F '#F' | grep -q Z; then143 zoomed_without_popup=1
9 unmodified lines
153argsf="${TMPDIR:-/tmp}/fzf-args-$id"154fifo1="${TMPDIR:-/tmp}/fzf-fifo1-$id"155fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id"156fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"157tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') )158cleanup() {159 \rm -f $argsf $fifo1 $fifo2 $fifo3160161 # Restore tmux window options162 if [[ "${#tmux_win_opts[@]}" -gt 0 ]]; then162 eval "tmux ${tmux_win_opts[@]}"163 eval "tmux ${tmux_win_opts[*]}"164 fi165166 # Remove temp window if we were zoomed without popup options167 if [[ -n "$zoomed_without_popup" ]]; then
11 unmodified lines
179}180trap 'cleanup 1' SIGUSR1181trap 'cleanup' EXIT182182envs="env TERM=$TERM "183envs="export TERM=$TERM "184[[ "$opt" =~ "-K -E" ]] && FZF_DEFAULT_OPTS="--margin 0,1 $FZF_DEFAULT_OPTS"185[[ -n "$FZF_DEFAULT_OPTS" ]] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"186[[ -n "$FZF_DEFAULT_COMMAND" ]] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"187echo "$envs;" > "$argsf"188187mkfifo -m o+w $fifo2188mkfifo -m o+w $fifo3189189# Build arguments to fzf191opts=""192for arg in "${args[@]}"; do193 arg="${arg//\\/\\\\}"194 arg="${arg//\"/\\\"}"195 arg="${arg//\`/\\\`}"196 arg="${arg//$/\\$}"197 opts="$opts \"$arg\""198done190opts=$(printf "%q " "${args[@]}")191192pppid=$$201echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" > $argsf193echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" >> $argsf194close="; trap - EXIT SIGINT SIGTERM $close"195204tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') )205196export TMUX=$(cut -d , -f 1,2 <<< "$TMUX")197mkfifo -m o+w $fifo2198if [[ "$opt" =~ "-K -E" ]]; then199 cat $fifo2 &200 if [[ -n "$term" ]] || [[ -t 0 ]]; then201 cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> $argsf210 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux popup -d "$PWD" "${tmux_args[@]}" $opt -R "$envs bash $argsf" > /dev/null 2>&1202 else203 mkfifo $fifo1204 cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> $argsf205 cat <&0 > $fifo1 &215 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux popup -d "$PWD" "${tmux_args[@]}" $opt -R "$envs bash $argsf" > /dev/null 2>&1206 fi207 tmux popup -d "$PWD" "${tmux_args[@]}" $opt -R "bash $argsf" > /dev/null 2>&1208 exit $?209fi210211mkfifo -m o+w $fifo3212if [[ -n "$term" ]] || [[ -t 0 ]]; then213 cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf222 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\223 set-window-option remain-on-exit off \;\224 split-window $opt "${tmux_args[@]}" "$envs bash -c 'cd $(printf %q "$PWD"); exec -a fzf bash $argsf'" $swap \225 > /dev/null 2>&1 || { "$fzf" "${args[@]}"; exit $?; }214else215 mkfifo $fifo1216 cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf229 TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\230 set-window-option remain-on-exit off \;\231 split-window $opt "${tmux_args[@]}" "$envs bash -c 'exec -a fzf bash $argsf'" $swap \232 > /dev/null 2>&1 || { "$fzf" "${args[@]}"; exit $?; }217 cat <&0 > $fifo1 &218fi219tmux set-window-option synchronize-panes off \;\220 set-window-option remain-on-exit off \;\221 split-window -c "$PWD" $opt "${tmux_args[@]}" "bash -c 'exec -a fzf bash $argsf'" $swap \222 > /dev/null 2>&1 || { "$fzf" "${args[@]}"; exit $?; }223cat $fifo2224exit "$(cat $fifo3)"