# Sync panes Type once, into every terminal of one tab, like tmux's `synchronize-panes`. `ctrl-t e` starts it and, pressed again, stops it; a small pill sits in the tab bar while it's on. Your key bindings keep working meanwhile. It opens as a small window over your current layout, not a layout of its own. ![Sync panes](preview.png) This file is the brief: enough to build Sync panes from an empty folder. The files next to it are one build of it. If they don't work on your Herdr, or you want it different, build it again from here. ## What you see - A pill in the tab bar, about 24px high, at x 600, y 3 in the window. It has no Herdr frame: the pill draws its own rounded 12px border. - While it holds the keyboard: a 6px dot breathing in the theme's `accent` color (opacity 0.35 → 1 over 1200 ms), "Sync N · " (N = terminals it types into, then the tab's name) in `foreground`, and a 18px round ✕ on the right. Border in `accent`. - When the keyboard is elsewhere (you clicked a terminal): the dot and border turn `dim`, the text says "Paused" in `muted`, and the dot stops breathing. - When there's no terminal to type into (none in the tab, or a non-terminal pane is zoomed): "No terminals", in `dim`. - After a send fails: the dot and border turn `blocked`, the text says "Failed t3 · click to resume" (the terminals that failed), and a Herdr notification carries the error. - Text is 11.5px on the theme's `popover` background. Nothing else is added to the layout. ## How it behaves **What it shows is what it sends.** Keys are only sent while the pill says "Sync". Everything still queued is dropped when it pauses, stops, leaves its tab, fails, or when the panes it types into change. A key already handed to `send.py` can't be called back. - `ctrl-t e` opens the pill in the shown tab and gives it the keyboard. Every key you type goes to every terminal pane of **that tab**, in order. When a pane is zoomed, only it counts: a zoomed terminal gets keys alone; a zoomed Files or preview pane means none do. - Showing another tab (or space) closes the pill. - Panes added, closed, zoomed or unzoomed change who gets keys from the next key on; keys queued for the old set are dropped. - Letters, digits, symbols and other characters the keyboard types go as text. Enter, Tab, Backspace, Escape, Space, arrows, Home/End, Page Up/Down, Delete, Insert, F1–F12 and Ctrl/Alt combinations go as keys, encoded as each terminal's program asked for (application cursor keys, the kitty keyboard protocol). F13 and up aren't sent. - Option does what it does in Herdr's terminals (`option_as_alt`, macOS): it is Alt, or it types what the layout gives (Option+¥ is `\` on a JIS keyboard). Dead keys (Option+E, then E) and IME composition don't reach the pill; paste such text with Cmd-V. - Keys are typed straight into each terminal, with no clipboard and no focus change: `|` is as quick as `l`. - Cmd-V pastes the clipboard into every pane. Other Cmd shortcuts are ignored. - **Paused:** clicking a terminal gives it the keyboard; keys go only there, and nothing queued goes anywhere. Clicking the pill resumes. Cmd-V and moving between panes with a binding move focus for a moment but don't pause. - **If a send fails** for any terminal, typing is held until you click the pill. Key bindings still work meanwhile. - **Key bindings run while syncing.** With `ctrl-t n` bound to `tab.next`, `ctrl-t n` goes to the next tab (which ends syncing). A binding that moves to another pane of the tab (`pane.go`) keeps syncing; a binding that opens a dialog (`space.rename`) leaves the keyboard in the dialog. - Keys that start a multi-key binding but complete none type nothing: `ctrl-t` followed by an unbound key, or by Escape. `ctrl-t a` types a literal Ctrl+T into every terminal. - `ctrl-t e` again, or the ✕, stops it. - Drag the pill anywhere; Herdr remembers the spot. ## Built from - **One mod**, `mods/sync-panes/`, with `main.js`, `send.py` and `mod.json`. The manifest has `place: "none"` and opens as a frameless window overlay (`kind: "window"`, `key: "sync-panes"`, `frame: "none"`, `at: {x: 600, y: 3}`), so there is no `layouts/` folder or layout change. - **A `keys.json` snippet**, merged into the person's keys rather than replacing them: ```json { "global": { "ctrl-t e": "mod.open:sync-panes" }, "mod:sync-panes": { "ctrl-t a": "sync-panes.send:\u0014" } } ``` `mod.open` on an open mod closes it (Herdr 0.1.11), so the one binding starts and stops. `sync-panes.send:` reaches the mod through `nextCommand()` as `"send:"`. The mod also takes `sync-panes.stop` (`"stop"`) for a key that only stops; like any mod command, it exists only while the mod is loaded, so pressed while not syncing it fails with `unknown command`. - **A focusable view**: `cx.focus_handle()`, focused in `init`, and once more 300 ms later only if the pill hasn't had the keyboard yet (focusing during render isn't allowed). `track_focus` and `on_key_down` on the pill's root; `handle.is_focused()` decides Sync vs Paused. Tab and Shift-Tab are bound with `cx.bind_keys` (in `init`) to the pill's own actions `sync-panes-tab` / `sync-panes-shift-tab` in its key context `SyncPanes` (`key_context` on the root, `on_action` for each); the action handler sends them as the key, through the same path as `on_key_down`, so a multi-key binding ending in Tab (`ctrl-t tab`) still runs. A Tab that also arrives as a key event within 100 ms of its action is ignored. - **Taking the keyboard back** (`regain()`): Herdr applies `pane.focus` / `pane.go` after the mod's own `handle.focus()` in the same turn, so focusing right away loses. After a paste (if the pill had the keyboard), after a binding the pill ran that changed `focused().pane`, and when `watch()` sees a binding moved the keyboard to another pane, `cx.timer.after` tries again at 30, 120 and 300 ms, while the tab is the same and the pill doesn't have it, and calls `cx.notify()`: a focus handle changing doesn't redraw the pill by itself. The 300 ms watch timer also redraws whenever focus differs from what was last drawn, so Sync / Paused is never stale. - **One watch** (`watch(cx)`, on a 300 ms `cx.timer.every`, on every render, before every queued key and before every send): a different `focused().tab` stops; a different target set drops the queue. While the pill has the keyboard it notes `focused().pane` and the time. When it has lost it (outside the 500 ms after it moved focus itself): if `on_mouse_down_out` (a press anywhere outside the pill) came after that time, or `focused().pane` is the one noted, or not a target, it pauses and drops the queue; otherwise a binding moved panes, and it takes the keyboard back with `regain()`. Dropping bumps a generation number; queued items carry the generation they were made in and give up when it changed, when stopped, or when the tab changed. - **Targets:** `panes()` in that tab; if one is `zoomed`, just it when it has a `terminal`, else none; otherwise those with a `terminal`. Each queued item keeps the targets of the moment it was typed, and items only merge with the same targets. - **Keys while syncing.** The `keys()` sections that apply, most specific first: `mod:sync-panes`, `layout:`, `global`. - A key that starts a binding of two or more keys is held, and so is each next key while some binding still starts with the held ones. Escape, pause, resume, failure and stop drop them. Then the whole sequence is looked up: nothing or `terminal`, nothing happens; `sync-panes.stop` or `mod.open:sync-panes`, it stops; `send:`, typed as below; anything else, `run()` (a `name:value` action passes the value as the command's first argument from `commands()`, digits as numbers), then `regain()` if `focused().pane` changed. Herdr runs bound sequences itself (see Watch out), so this mostly sees unbound ones. - `sync-panes.send:` text: printable characters as text; `\r` and `\n` Enter, `\t` Tab, ESC Escape, Ctrl+A–Z as Ctrl keys. ESC and one printable character other than `[` and `O` (which start a key's own sequence) is Alt and that key, Shift too for a capital: the same bytes as the text in a legacy terminal, Alt+B rather than Esc then `b` in kitty mode. Any other control character (NUL, Ctrl+\ ] ^ _, DEL): nothing is sent, and a warning says so. - Option, on macOS: `init` and a 2 s `cx.timer.every` read `option_as_alt` from `${appHome()}/settings.json` (`fs/promises`; missing means `"auto"`). `"on"` is Alt, `"off"` isn't, and `"auto"` is Alt when `/usr/bin/defaults read com.apple.HIToolbox AppleCurrentKeyboardLayoutInputSourceID` is one of `com.apple.keylayout.US`, `USInternational-PC`, `ABC`, `ABC-India` (the layouts Herdr names for `"auto"`). Until the first read, and after a failed one, the last answer stays (at first, Alt). When it isn't Alt, a character key's `key_char` with Option (and no Ctrl) goes as text; named keys and Ctrl combinations keep Alt. - **Typing:** each queued item is a list of input, `{text}` or `{key, mods}` (`mods`: 1 Shift, 2 Alt, 4 Ctrl; key names `enter`, `esc`, `up`, `f5`, `a`, `;`, ...). Input typed while a send runs merges into the last queued item with the same targets (adjacent text joins), up to 256 entries. One item at a time is written as `{terminals, input}` JSON with `writeTextFile` into a folder made once with Python's `tempfile.mkdtemp` (private to the user, in its temporary folder) and handed over as `exec(python, [..., "-I", `${appHome()}/mods/sync-panes/send.py`, file])`. The Python is the first of `python3`, `python` that makes that folder (on Windows `py -3`, `python`, `python3`: there `python3` may be the Store's stand-in, which opens the Store); `-I` rules out Python 2. None: the send fails with "send.py needs Python 3". send.py deletes the file once read and prints `{terminal: null | error}`. `close()` removes the folder with the same Python (`shutil.rmtree`), so no `rm` is needed. Any error, a non-zero exit without that report, or a rejected `exec` fails: the failing terminals and the message are kept until the pill is clicked. - **send.py** (Python 3, standard library only) speaks herdr-core's protocol through `hc connect`: a `hello {framed: true, generation: 1}` line, then frames `[channel u32 LE][kind u8][length u32 LE][payload]` (DATA 0, CREDIT 1, CLOSE 2; channel 0 is JSON-RPC). For each terminal, in its own thread on the one connection: - `terminal.attach {terminal, cols: 0, rows: 0, history: 0}`. The core never applies a 0×0 size, so the terminal keeps the size Herdr gave it. Data that comes back is credited (CREDIT with its length). - The modes are read from the first paint, which ends with `ESC[?9001h` or `l`, and from live output after it: `ESC[?1h/l` (application cursor keys), `ESC[=flags;how u` (kitty flags: set, or, and-not), `ESC[>flags u` (push), `ESC[>4;n m` (modifyOtherKeys), `ESC c` (reset). A kitty pop (`ESC[" and typing `ls | grep "a"` + Enter runs the same command in both. - Arrows, Ctrl+C, Backspace and Tab reach both shells. - Typing `ls | wc -l` + Enter at a normal pace: every key, `|` and those after it included, reaches both shells within about 0.1 s; the clipboard isn't touched and the terminals keep their size. - With vim open in both panes, arrows move and Esc leaves insert mode. - Cmd-V pastes the clipboard into both. - Clicking a terminal shows "Paused" and typing goes only there; clicking the pill shows "Sync 2 · " again. - Typing `ls |` keeps "Sync 2 · ", and the next keys still reach both shells. - With `ctrl-t h` and `ctrl-t l` bound to `pane.go:left` and `pane.go:right`, moving between the two shells keeps "Sync 2 · ", and typing still reaches both. A binding that opens a dialog (`space.rename`) pauses and leaves the keyboard in the dialog. - With `ctrl-t n` bound to `tab.next`, `ctrl-t n` while syncing goes to the next tab and the pill closes; nothing typed before reaches the new tab. - Zooming one shell makes the pill say "Sync 1" and only that shell gets keys; zooming a Files or preview pane makes it say "No terminals". - Killing one of the shells from outside and typing shows "Failed …"; nothing more is typed anywhere until the pill is clicked. - `ctrl-t a` types a literal Ctrl+T into both (in `cat`, macOS shows a `load:` line in each); an unbound `ctrl-t ` types nothing. - A `"alt-left": "sync-panes.send:\u001bb"` binding in `mod:sync-panes` moves both shells back a word. - With `"option_as_alt": "off"` in settings.json, Option+key types the layout's character in both shells; with `"on"`, Option+B moves back a word in both. - With the bindings moved to `ctrl-b` in `keys.json` only, `ctrl-b e` starts and stops it; with `mod.open:sync-panes` on a single key (`cmd-shift-e`), that key does. - `ctrl-t e` (pressed again) and the ✕ both close the pill, and normal typing goes to one terminal again.