1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Runtime-only tmux key bindings installed for the lifetime of a tango process.
//!
//! Installs session-scoped Alt-Left detach bindings via `tmux bind-key -n` and
//! removes them on drop. The bindings are gated on a per-session user option
//! (`@tango_active`); only sessions tango has marked active will detach on press.
//! All other sessions pass the key through to the active pane unchanged.
//!
//! Two bindings are installed because macOS terminals (Terminal.app, iTerm2, Ghostty
//! with default Option behavior) rewrite the physical Option+Left key to `ESC b`
//! (`M-b`) — the readline backward-word sequence — instead of the CSI modified-arrow
//! sequence tmux's `M-Left` matches. Binding both forms covers either terminal mode.
//!
//! The gating uses a `tmux show-options` shell test rather than the `#{@user_option}`
//! format because the latter only expands inside `if-shell -F` on tmux >= 3.0. Older
//! distros (Ubuntu 18.04, RHEL 7/8) still ship tmux 1.8–2.6, where the format would
//! silently expand to an empty string and the binding would never fire.
use ;
const ACTIVE_FLAG: &str = "@tango_active";
const BINDING_COND: &str =
"[ \"$(tmux show-options -v -t '#{session_name}' @tango_active 2>/dev/null)\" = 1 ]";
const BINDING_THEN: &str = "detach-client";
/// Keys we bind, paired with their `send-keys` passthrough form for the else branch.
/// `M-Left` — terminals that send the CSI modified-arrow sequence for Alt+Left.
/// `M-b` — macOS terminals that rewrite Alt+Left to the readline backward-word
/// sequence (`ESC b`). In non-tango sessions this is forwarded so shells
/// still get word-back.
const BINDINGS: & = &;
/// Install the detach bindings on the running tmux server.
/// Idempotent and safe to call before any tmux server exists — the call simply
/// fails silently and can be retried after `attach-session` starts a server.
/// Remove tango's detach bindings. Best-effort: silently ignores errors and absent servers.
/// Mark `session` as a tango-managed attachment so the binding fires there.
/// Clear the tango-active flag on `session`. Called after the attach returns.
/// RAII guard: installs the binding on construction and unbinds it on drop.
/// Drop is best-effort — a panic or kill mid-attach will still run the unbind.
;