aboutsummaryrefslogtreecommitdiff
path: root/tmuxrdr/plugin/bin/track-focus
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-10-05 11:27:00 +0300
committerIgor Soarez <igor@soarez.org>2026-10-05 11:27:00 +0300
commit986e760687a2d34051934d410bec71fa2efc349b (patch)
treef57dcc4ba0d7690fdf90eeb2f0970a8ebbefc45a /tmuxrdr/plugin/bin/track-focus
parent7b116c05bfda646af39d2524c54e70c751f57ee8 (diff)
tmuxrdr: tmux-mode plugin
The tmux default prefix bindings herdr has no built-in action for, as a herdr plugin built on its CLI and socket: break-pane, swap-pane and rotate-window by pane order (focus behaviour measured on tmux 3.7c), mark/clear-mark, last-window and switch-client -l (from focus events, per session), select-window and move-window by index, paste-buffer -p, display-message in tmux's default format, select-layout -E, kill-pane and kill-window behind tmux's y/n prompts, a command prompt taking tmux's commands, clock-mode, show-messages, and automatic-rename. Also a .gitignore for secrets scoped to tmuxrdr/, leaving herdr's own untouched.
Diffstat (limited to 'tmuxrdr/plugin/bin/track-focus')
-rwxr-xr-xtmuxrdr/plugin/bin/track-focus36
1 files changed, 36 insertions, 0 deletions
diff --git a/tmuxrdr/plugin/bin/track-focus b/tmuxrdr/plugin/bin/track-focus
new file mode 100755
index 00000000..4132891f
--- /dev/null
+++ b/tmuxrdr/plugin/bin/track-focus
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Event hook for tab.focused and workspace.focused: keeps the current and
+# previous tab of each workspace, and the current and previous workspace, so
+# `last window` and `last session` know where to go. Hooks run concurrently,
+# so updates take a mkdir lock.
+. "$(dirname "$0")/../lib.sh"
+
+dir="$(state_dir)"
+ev="${HERDR_PLUGIN_EVENT_JSON:-}"
+[ -n "$ev" ] || exit 0
+
+field() {
+ printf '%s' "$ev" | jq -r --arg k "$1" '[.. | objects | .[$k]? | strings][0] // empty'
+}
+
+# record NAME VALUE: VALUE becomes NAME.current, the old one NAME.previous.
+record() {
+ [ -n "$2" ] || return 0
+ cur="$(cat "$dir/$1.current" 2>/dev/null || true)"
+ [ "$cur" = "$2" ] && return 0
+ [ -n "$cur" ] && printf '%s\n' "$cur" >"$dir/$1.previous"
+ printf '%s\n' "$2" >"$dir/$1.current"
+}
+
+tries=0
+until mkdir "$dir/.lock" 2>/dev/null; do
+ tries=$((tries + 1))
+ [ "$tries" -gt 40 ] && break
+ sleep 0.05
+done
+trap 'rmdir "$dir/.lock" 2>/dev/null' EXIT
+
+case "${HERDR_PLUGIN_EVENT:-}" in
+tab.focused) record "tab.$(key_of "$(field workspace_id)")" "$(field tab_id)" ;;
+workspace.focused) record workspace "$(field workspace_id)" ;;
+esac