Skip to content
Myr

tmux.go

This file

Path

src/tmux.go

275 lines · 10.9 KiB · Go

Last changed by

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

History of this file

Take it

The walk stopped after 200 commits: lines older than 76efddd point at it.

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 1–3why, one step back
1package fzf
23import (

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 4–5why, one step back
4 "fmt"
5 "os"

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

line 6why, one step back
6 "os/exec"

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 7why, one step back
7 "path/filepath"

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 8why, one step back
8 "regexp"

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 9why, one step back
9 "strings"

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

lines 10–11why, one step back
10
11 "github.com/junegunn/fzf/src/tui"

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 12–13why, one step back
12)
13

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

lines 14–16why, one step back
14// The flag group of new-pane usage includes -A (tmux 3.8 or above)
15var tmuxOverZoomFlag = regexp.MustCompile(`new-pane.*\[-[[:alpha:]]*A`)16

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 17why, one step back
17// Returns the size of the current window if the tmux server supports

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

lines 18–20why, one step back
18// floating panes (tmux 3.7 or above), and whether a floating pane can stay
19// above a zoomed pane20func tmuxFloatingPaneInfo() (int, int, bool, bool) {

Comment why floating pane requires TMUX_PANEJunegunn Choibefore adoption4dcedc0

lines 21–30why, one step back
21 // TMUX_PANE is not set when fzf is not started from within a pane
22 // (e.g. 'bind-key 0 run-shell "fzf --popup"'). Do not use a floating23 // pane there; a blocking run-shell suspends key processing for the24 // client until the command exits, so fzf in a floating pane would25 // never receive a key and the client would appear frozen until the26 // process is killed externally. A popup is unaffected; its input is27 // handled at the client overlay level, bypassing the suspended path.28 // The right approach for such bindings is 'run-shell -b', which does29 // not suspend key processing, as discussed in:30 // https://github.com/tmux/tmux/issues/5384

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 31–32why, one step back
31 target := os.Getenv("TMUX_PANE")
32 if target == "" {

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 33why, one step back
33 return 0, 0, false, false

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 34–40why, one step back
34 }
35 // A single invocation for both checks. Cannot rely on the exit status;36 // tmux versions before 3.7 exit normally with empty output for an37 // unknown command name, so check the output instead.38 out, err := exec.Command("tmux", "display-message", "-p", "-t", target,39 "#{window_width} #{window_height}", ";", "list-commands", "new-pane").Output()40 if err != nil || !strings.Contains(string(out), "new-pane") {

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 41why, one step back
41 return 0, 0, false, false

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 42–44why, one step back
42 }
43 var width, height int44 if _, err := fmt.Sscanf(string(out), "%d %d", &width, &height); err != nil {

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 45why, one step back
45 return 0, 0, false, false

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 46–48why, one step back
46 }
47 // The window is too small to fit a floating pane of the minimum size48 if width < 3 || height < 3 {

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 49why, one step back
49 return 0, 0, false, false

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 50why, one step back
50 }

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 51why, one step back
51 return width, height, tmuxOverZoomFlag.Match(out), true

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 52–53why, one step back
52}
53

Escape trailing semicolon in tmux command argumentJunegunn Choibefore adoption5763168

lines 54–55why, one step back
54// tmux ends a command at an argument ending in ';', so a trailing one is
55// escaped. Elsewhere it is not special

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 56why, one step back
56func escapeTmuxSeparator(str string) string {

Escape trailing semicolon in tmux command argumentJunegunn Choibefore adoption5763168

lines 57–58why, one step back
57 if strings.HasSuffix(str, ";") {
58 return str[:len(str)-1] + `\;`

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

lines 59–62why, one step back
59 }
60 return str61}62

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 63–65why, one step back
63// '#[...]' is still processed when a substituted value is drawn, so '#' is
64// doubled. '#{...}' is already literal65func escapeTmuxFormat(str string) string {

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

lines 66–68why, one step back
66 return escapeTmuxSeparator(strings.ReplaceAll(str, "#", "##"))
67}68

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 69–82why, one step back
69// The option pane-border-format reads back, and the pane it is set on
70const (71 tmuxBorderLabelOption = "@fzf-border-label"72 tmuxBorderLabelEnv = internalEnvPrefix + "TMUX_LABEL_PANE"73)7475// Errors are ignored. The pane may be gone76func setTmuxBorderLabel(pane string, label string) {77 // Strip ANSI sequences. tmux would draw the escape's remainder as text78 label, _, _ = extractColor(label, nil, nil)79 exec.Command("tmux", "set-option", "-p", "-t", pane,80 tmuxBorderLabelOption, escapeTmuxFormat(label)).Run()81}82

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 83–92why, one step back
83// Convert sizeSpec to the number of cells, clamped between the minimum
84// footprint of 3, including the border, and the window size85func tmuxDim(spec sizeSpec, window int) int {86 dim := int(spec.size)87 if spec.percent {88 dim = window * dim / 10089 }90 return max(3, min(dim, window))91}92

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 93why, one step back
93func runTmuxFloatingPane(argStr string, dir string, windowWidth int, windowHeight int, overZoom bool, opts *Options) (int, error) {

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 94–138why, one step back
94 // Unlike display-popup, the size of a floating pane does not account for
95 // the border around it, and the position is that of the content area. To96 // stay consistent with popups, treat the requested size as the total97 // footprint including the border.98 width := tmuxDim(opts.Tmux.width, windowWidth)99 height := tmuxDim(opts.Tmux.height, windowHeight)100 x := (windowWidth-width)/2 + 1101 y := (windowHeight-height)/2 + 1102 switch opts.Tmux.position {103 case posUp:104 y = 1105 case posDown:106 y = windowHeight - height + 1107 case posLeft:108 x = 1109 case posRight:110 x = windowWidth - width + 1111 }112113 return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {114 sh, err := sh(needBash)115 if err != nil {116 return nil, err117 }118 // Unlike display-popup, new-pane does not block until the command119 // finishes, and it does not propagate the exit status. So we block on120 // a wait-for channel that the pane signals on completion, and pass121 // the exit status through a temporary file. A watchdog process122 // signals the same channel if the pane is closed abnormally123 // (e.g. kill-pane), in which case the file is not written.124 //125 // has-session is the liveness check because it fails when the target126 // pane is gone, while display-message succeeds even for a dead pane.127 signal := escapeSingleQuote("fzf-" + filepath.Base(temp))128129 // Pre-create the exit status file so that another user on a shared130 // TMPDIR cannot plant a file or a symbolic link at the predictable131 // path while the pane is running132 codeFile := temp + ".code"133 f, err := os.OpenFile(codeFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)134 if err != nil {135 return nil, err136 }137 f.Close()138 code := escapeSingleQuote(codeFile)

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

lines 139–149why, one step back
139
140 // Pane options are set by the pane itself before running fzf, so141 // that they are in place no matter how quickly the command exits.142 // The target must be explicit; without it, the commands would143 // resolve to the active pane of the session's current window,144 // which is not necessarily the pane running them.145 //146 // The pane should always close on exit like a popup, even when147 // remain-on-exit is on.148 setup := `tmux set-option -p -t "$TMUX_PANE" remain-on-exit off 2> /dev/null; `149

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 150–155why, one step back
150 // --border-label lives in a pane-scoped user option that
151 // pane-border-format reads back, so a program in the pane cannot152 // replace it by setting the title. Set even when empty, so153 // change-border-label can fill it in and the default pane status is154 // not shown. pane-border-status is a window option until the next155 // tmux release, so it is left alone.

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 156why, one step back
156 // https://github.com/tmux/tmux/commit/7a18fa281db3

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 157–161why, one step back
157 // --border-label-pos is ignored. With an explicit --border fzf draws
158 // the label itself, but the native border cannot be removed.159 ownsLabel := noBorderSpecified(opts)160 label := ""161 if ownsLabel {

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 162why, one step back
162 // Strip ANSI sequences fzf would otherwise render itself

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

line 163why, one step back
163 label, _, _ = extractColor(opts.BorderLabel.label, nil, nil)

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 164why, one step back
164 }

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 165–166why, one step back
165 setup += fmt.Sprintf(`tmux set-option -p -t "$TMUX_PANE" %s %s 2> /dev/null; `,
166 tmuxBorderLabelOption, escapeSingleQuote(escapeTmuxFormat(label)))

Clear border text of the floating pane when no label is givenJunegunn Choibefore adoptioneee92b1

line 167why, one step back
167 setup += fmt.Sprintf(`tmux set-option -p -t "$TMUX_PANE" pane-border-format %s 2> /dev/null; `,

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 168–171why, one step back
168 escapeSingleQuote("#{"+tmuxBorderLabelOption+"}"))
169 if ownsLabel {170 setup += fmt.Sprintf(`export %s="$TMUX_PANE"; `, tmuxBorderLabelEnv)171 }

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

lines 172–173why, one step back
172 paneCmd := fmt.Sprintf("%s%s %s; echo $? > %s; tmux wait-for -S %s",
173 setup, escapeSingleQuote(sh), escapeSingleQuote(temp), code, signal)

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 174why, one step back
174 target := os.Getenv("TMUX_PANE")

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

lines 175–182why, one step back
175 newPane := "tmux new-pane -A"
176 if !overZoom {177 // Unzoom the window first; creating a floating pane over a178 // zoomed window crashes the tmux server on 3.7b179 newPane = fmt.Sprintf("tmux if -F -t %s '#{window_zoomed_flag}' %s ';' new-pane",180 escapeSingleQuote(target), escapeSingleQuote("resize-pane -Z -t "+target))181 }182 newPane += fmt.Sprintf(" -P -F '#{pane_id}' -t %s -c %s -x %d -y %d -X %d -Y %d %s -c %s",

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 183–184why, one step back
183 escapeSingleQuote(target), escapeSingleQuote(dir), width-2, height-2, x, y,
184 escapeSingleQuote(sh), escapeSingleQuote(paneCmd))

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 185why, one step back
185

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 186–209why, one step back
186 // The pane is killed when the proxy process is interrupted or hung up,
187 // like a popup dying with its client. wait-for runs in the background188 // and is awaited with the interruptible wait builtin so that the trap189 // can fire while blocked. The trap is installed before creating the190 // pane; a signal received during creation is deferred until the191 // command substitution completes, and the pane is killed right after.192 // An interrupted wait does not reap the waiter, so it is killed193 // along with the watchdog.194 script := fmt.Sprintf(`trap '[ -n "$id" ] && tmux kill-pane -t "$id" 2> /dev/null' INT TERM HUP195id=$(%s) || { status=$?; rm -f %s; exit "$status"; }196{ while tmux has-session -t "$id" 2> /dev/null; do sleep 1; done; tmux wait-for -S %s; } &197watchdog=$!198tmux wait-for %s &199waiter=$!200wait "$waiter"201kill "$watchdog" "$waiter" 2> /dev/null202wait 2> /dev/null203if [ -s %s ]; then code=$(cat %s); else code=130; fi204rm -f %s205exit "$code"`, newPane, code, signal, signal, code, code, code)206 return exec.Command(sh, "-c", script), nil207 }, opts, true)208}209

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

lines 210–216why, one step back
210// Whether no box border was asked for. 'none' and 'line' count as none.
211// fzf draws no box for either212func noBorderSpecified(opts *Options) bool {213 return opts.BorderShape == tui.BorderUndefined ||214 opts.BorderShape == tui.BorderLine || opts.BorderShape == tui.BorderNone215}216

Default to native border for Zellij floating pane (#4854)Junegunn Choibefore adoptione54f11c

lines 217–220why, one step back
217// Whether to use the multiplexer's native border for the floating pane. Its
218// native border is the handle that makes the pane movable and resizable with219// the mouse, so it is the default; 'border-native' forces it. It is not used220// when a border style is explicitly specified with --border, so that the

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

line 221why, one step back
221// fzf-drawn border is the only one shown.

Default to native border for Zellij floating pane (#4854)Junegunn Choibefore adoptione54f11c

line 222why, one step back
222func nativeBorder(opts *Options) bool {

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

line 223why, one step back
223 return opts.Tmux.border || noBorderSpecified(opts)

Default to native border for Zellij floating pane (#4854)Junegunn Choibefore adoptione54f11c

lines 224–225why, one step back
224}
225

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

line 226why, one step back
226func runTmux(args []string, opts *Options) (int, error) {

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 227why, one step back
227 // On tmux 3.7 or above, fzf runs in a floating pane instead of a popup.

Default to native border for Zellij floating pane (#4854)Junegunn Choibefore adoptione54f11c

line 228why, one step back
228 // When the native border is not used (an explicit --border style), a

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 229why, one step back
229 // popup is used instead so that the fzf-drawn border is the only border

Default to native border for Zellij floating pane (#4854)Junegunn Choibefore adoptione54f11c

lines 230–231why, one step back
230 // shown; the native border of a tmux floating pane cannot be removed.
231 if nativeBorder(opts) {

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 232why, one step back
232 if windowWidth, windowHeight, overZoom, ok := tmuxFloatingPaneInfo(); ok {

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 233why, one step back
233 opts.Tmux.border = true

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

line 234why, one step back
234 argStr, dir := popupArgStr(args, opts)

Preserve tmux window zoom when opening a floating paneJunegunn Choibefore adoptiond8aeec3

line 235why, one step back
235 return runTmuxFloatingPane(argStr, dir, windowWidth, windowHeight, overZoom, opts)

Use floating pane instead of popup on tmux 3.7 or above (#4850) (#4852)Junegunn Choibefore adoption1e31e5d

lines 236–238why, one step back
236 }
237 }238

Support zellij floating pane via --popup (new name for --tmux) (#4145)Junegunn Choibefore adoption6f17d49

line 239why, one step back
239 argStr, dir := popupArgStr(args, opts)

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 240–248why, one step back
240
241 // Set tmux options for popup placement242 // C Both The centre of the terminal243 // R -x The right side of the terminal244 // P Both The bottom left of the pane245 // M Both The mouse position246 // W Both The window position on the status line247 // S -y The line above or below the status line248 tmuxArgs := []string{"display-popup", "-E", "-d", dir}

Set --border-label as the title of the tmux floating pane (#4853)Junegunn Choibefore adoption77e6394

line 249why, one step back
249 if !opts.Tmux.border {

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 250–275why, one step back
250 tmuxArgs = append(tmuxArgs, "-B")
251 }252 switch opts.Tmux.position {253 case posUp:254 tmuxArgs = append(tmuxArgs, "-xC", "-y0")255 case posDown:256 tmuxArgs = append(tmuxArgs, "-xC", "-y9999")257 case posLeft:258 tmuxArgs = append(tmuxArgs, "-x0", "-yC")259 case posRight:260 tmuxArgs = append(tmuxArgs, "-xR", "-yC")261 case posCenter:262 tmuxArgs = append(tmuxArgs, "-xC", "-yC")263 }264 tmuxArgs = append(tmuxArgs, "-w"+opts.Tmux.width.String())265 tmuxArgs = append(tmuxArgs, "-h"+opts.Tmux.height.String())266267 return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {268 sh, err := sh(needBash)269 if err != nil {270 return nil, err271 }272 tmuxArgs = append(tmuxArgs, sh, temp)273 return exec.Command("tmux", tmuxArgs...), nil274 }, opts, true)275}