This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit b93bb6d749b936f32beb5bcf50f9b0e4dbe4c413
Author: [email protected] <[email protected]>
AuthorDate: Thu Sep 24 23:40:42 2026 -0600

    tymux: attach natively when running inside Terminology
    
    When TERMINOLOGY is set, and TMUX and STY are not (a multiplexer in
    between would receive the command instead of Terminology), tymux attach
    and tymux new print the switch command and exit instead of proxying,
    so the session gets Terminology's own rendering and input handling.
    TYMUX_NO_NATIVE=1 keeps the inline proxy, for instance when TERMINOLOGY
    is forwarded over ssh and the command would reach the local terminal.
    
    The integration test no longer inherits TERMINOLOGY, TMUX or STY from
    the environment it runs in: inside Terminology, every attach in it
    would otherwise turn into a hand-off.
---
 man/tymux.1     | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 src/bin/tymux.c | 57 ++++++++++++++++++++++++++++++++++
 tests/tymux.py  | 49 ++++++++++++++++++++++++++++--
 3 files changed, 188 insertions(+), 12 deletions(-)

diff --git a/man/tymux.1 b/man/tymux.1
index 03924271..86386c01 100644
--- a/man/tymux.1
+++ b/man/tymux.1
@@ -32,6 +32,18 @@ control shows
 until the new one detaches.
 .PP
 The size of the terminal in control is the size of the shell's screen.
+.PP
+When
+.B new
+or
+.B attach
+run inside a Terminology terminal that is not itself nested in a
+multiplexer, they hand the session over to Terminology natively instead of
+proxying it: Terminology replaces the terminal's shell with a direct view
+of the session, with full input forwarding, and
+.B tymux
+exits immediately rather than staying attached. It still has no scrollback
+of its own (see \fBBUGS\fP). See \fBNATIVE HAND\-OFF\fP.
 .SH COMMANDS
 .TP
 .BR new " [" \fINAME\fP ]
@@ -87,16 +99,80 @@ Set in the shells started by
 to their name.
 .B tymux
 warns when attaching from inside one of them.
+.TP
+.B TERMINOLOGY, TMUX, STY, TYMUX_NO_NATIVE
+Used to decide on native hand-off; see \fBNATIVE HAND\-OFF\fP.
+.SH NATIVE HAND-OFF
+Instead of proxying the session through the inline terminal emulation
+described above,
+.B new
+and
+.B attach
+detect when they are run inside a Terminology terminal and ask it to take
+the session over directly.
+.PP
+Detection requires \fBTERMINOLOGY\fP to be set, which Terminology sets in
+the environment of the shells it starts, and neither \fBTMUX\fP nor
+\fBSTY\fP to be set, since either would mean the visible terminal is a
+tmux or screen pane multiplexed behind the one Terminology drew, and the
+hand-off escape would not necessarily reach it.
+.PP
+.B TYMUX_NO_NATIVE
+is the escape hatch: when set, hand-off detection is skipped and the
+inline proxy is used unconditionally, even under Terminology.
+.PP
+A hand-off replaces this terminal's shell outright: the shell that ran
+.B tymux
+receives \fBSIGHUP\fP, exactly as if the terminal's process had exited.
+.PP
+.B tymux
+cannot tell whether the terminal it sent the escape to actually acted on
+it: Terminology only does so when its own extended escapes are enabled
+(see
+.BR terminology (1)),
+and any other terminal ignores it outright. Either way
+.B tymux
+still exits as soon as the escape is written, so if the recipient did not
+take the hand-off the session is left unattached. Set \fBTYMUX_NO_NATIVE\fP
+if this is a concern, e.g. when scripting
+.B tymux attach
+from a context where that cannot be guaranteed.
+.PP
+Since a hand-off targets whatever terminal reads standard output, running
+.B tymux attach
+or
+.B tymux new
+inside Terminology over an SSH connection that forwards \fBTERMINOLOGY\fP
+(via \fBSendEnv\fP or \fBAcceptEnv\fP) makes the escape reach the
+.I local
+Terminology window, which then switches to a
+.I local
+tymux session of the same name \(en not the remote one the command was
+meant to operate on. \fBTYMUX_NO_NATIVE\fP avoids this.
 .SH BUGS
-The attached terminal is redrawn from the shell's screen: it has no
-scrollback of its own, and mouse reporting, bracketed paste, application
-cursor keys and focus reporting are not forwarded to it.
-Terminology's own escape sequences (media popups and the other commands
-introduced by ESC }) and color scheme changes (OSC 4, 10, 11 and 12) are
-ignored in a session, desktop notifications (OSC 99 and 777) are not
-forwarded, and hyperlinks (OSC 8) are drawn as plain text.
-The size of a session is capped at 1920 columns by 1280 rows: a larger
-terminal leaves the excess blank.
+The attached terminal has no scrollback of its own, in either mode: the
+shadow pty a native hand-off (see \fBNATIVE HAND\-OFF\fP) installs keeps
+no backlog either, and neither does the inline proxy. A shared backlog
+fed from the daemon is planned but not implemented yet.
+.PP
+When proxied (not natively handed off), the attached terminal is also
+only redrawn from the shell's screen, and mouse reporting, bracketed
+paste, application cursor keys and focus reporting are not forwarded to
+it; a native hand-off does not have this limitation, since it runs the
+session through Terminology's normal terminal emulation and input
+encoding.
+.PP
+In both modes, Terminology's own escape sequences (media popups and the
+other commands introduced by ESC }) and color scheme changes (OSC 4, 10,
+11 and 12) are ignored in a session, and desktop notifications (OSC 99
+and 777) are not forwarded, because the daemon that emulates the session
+drops them.
+Hyperlinks (OSC 8) have no destination in either mode: the inline proxy
+draws them as plain text, and a native hand-off receives only their
+index in the daemon, not their target, so they cannot be followed.
+.PP
+The size of a session is capped at 1920 columns by 1280 rows in both
+modes: a larger terminal leaves the excess blank.
 .SH SEE ALSO
 .BR terminology (1)
 .SH AUTHOR
diff --git a/src/bin/tymux.c b/src/bin/tymux.c
index 1adff2be..4f40b1e8 100644
--- a/src/bin/tymux.c
+++ b/src/bin/tymux.c
@@ -651,6 +651,56 @@ _proxy_run(int sock)
    return proxy.exit_code;
 }
 
+/* Whether to ask a Terminology window that is running us to take this
+ * session over natively (see _native_handoff_try()) instead of running
+ * the inline proxy below.
+ *
+ * TERMINOLOGY is set by Terminology in the environment of the shells it
+ * spawns, so its presence means our stdout is very likely a Terminology
+ * terminal. $TMUX and $STY rule out the cases where that terminal is
+ * multiplexed behind tmux or screen: the escape would only reach
+ * whichever pane is currently attached to *that* multiplexer, not
+ * necessarily us, and demoting the wrong pane's shell would be a
+ * surprising thing to do. TYMUX_NO_NATIVE is the escape hatch for anyone
+ * who wants the inline proxy regardless (e.g. scripting the CLI attach
+ * from within Terminology itself). */
+static bool
+_native_handoff_allowed(void)
+{
+   if (getenv("TYMUX_NO_NATIVE")) return false;
+   if (!getenv("TERMINOLOGY")) return false;
+   if (getenv("TMUX") || getenv("STY")) return false;
+   return true;
+}
+
+/* Emits Terminology's private native hand-off escape and returns true if
+ * it was sent: the caller must exit without running the inline proxy, as
+ * Terminology receiving it replaces this process's controlling terminal
+ * with a shadow pty and closing our pty master will end us via SIGHUP. */
+static bool
+_native_handoff_try(const char *name, TermSrvAttachMode mode)
+{
+   char esc[128];
+   const char *variant;
+   int n;
+
+   if (!_native_handoff_allowed()) return false;
+
+   switch (mode)
+     {
+      case TSRV_ATTACH_SHARED:   variant = "ats;"; break;
+      case TSRV_ATTACH_READONLY: variant = "atr;"; break;
+      default:                   variant = "at;"; break;
+     }
+   n = snprintf(esc, sizeof(esc), "\033}%s%s", variant, name);
+   if (n <= 0 || (size_t)n >= sizeof(esc)) return false;
+
+   /* +1 to also send the NUL that terminates a Terminology escape. */
+   if (fwrite(esc, 1, (size_t)n + 1, stdout) != (size_t)n + 1) return false;
+   if (fflush(stdout) != 0) return false;
+   return true;
+}
+
 static int
 _attach(const char *name, TermSrvAttachMode mode)
 {
@@ -666,6 +716,13 @@ _attach(const char *name, TermSrvAttachMode mode)
         fprintf(stderr, "tymux: no tymux named '%s'\n", name);
         return 1;
      }
+   /* Checked liveness above so we never hand off a session that does not
+    * exist; Terminology has no way to report that failure back to us. */
+   if (_native_handoff_try(name, mode))
+     {
+        close(sock);
+        return 0;
+     }
    if (termsrv_msg_send(sock, TSRV_MSG_ATTACH, &mode_byte, 1) < 0)
      {
         perror("tymux: send");
diff --git a/tests/tymux.py b/tests/tymux.py
index 382a670a..44779b3f 100644
--- a/tests/tymux.py
+++ b/tests/tymux.py
@@ -31,7 +31,8 @@ SHELL_RC = os.path.join(XDG, "shellrc")
 with open(SHELL_RC, "w") as f:
     f.write("set +o emacs 2>/dev/null; set +o vi 2>/dev/null\n")
 ENV = {k: v for k, v in os.environ.items()
-       if k not in ("DISPLAY", "WAYLAND_DISPLAY", "TYMUX_NAME")}
+       if k not in ("DISPLAY", "WAYLAND_DISPLAY", "TYMUX_NAME",
+                    "TERMINOLOGY", "TMUX", "STY", "TYMUX_NO_NATIVE")}
 ENV.update(XDG_RUNTIME_DIR=XDG, SHELL="/bin/sh", PS1="$ ", ENV=SHELL_RC,
            TERM="xterm-256color")
 failures = []
@@ -112,10 +113,10 @@ def check(cond, what):
 class Client:
     """tymux running in a pseudo terminal of rows x cols"""
 
-    def __init__(self, args, rows=24, cols=80, env=ENV):
+    def __init__(self, args, rows=24, cols=80, env=None):
         self.pid, self.fd = pty.fork()
         if self.pid == 0:
-            os.execve(TYMUX, [TYMUX] + args, env)
+            os.execve(TYMUX, [TYMUX] + args, env if env is not None else ENV)
         self.resize(rows, cols)
         self.out = b""
         self.status = None
@@ -382,6 +383,48 @@ def main():
           "attach to a missing tymux fails")
     check(tymux("new", "a/b").returncode == 1, "invalid names are refused")
 
+    # Native hand-off: when run as if from inside Terminology (TERMINOLOGY
+    # set, no multiplexer in the way), 'tymux attach'/'tymux new' print the
+    # private escape and exit instead of proxying.
+    NAME2 = "h%d" % os.getpid()
+    b = Client(["new", NAME2])
+    check(b.expect("$ "), "hand-off: second session shell prompt shown")
+
+    term_env = dict(ENV, TERMINOLOGY="1")
+    result = subprocess.run([TYMUX, "attach", NAME2], env=term_env,
+                            capture_output=True, timeout=10)
+    check(result.returncode == 0, "hand-off: attach exits 0")
+    check(result.stdout == b"\033}at;" + NAME2.encode() + b"\0",
+          "hand-off: attach emits the plain (hijack) escape")
+
+    result = subprocess.run([TYMUX, "attach", "--readonly", NAME2],
+                            env=term_env, capture_output=True, timeout=10)
+    check(result.stdout == b"\033}atr;" + NAME2.encode() + b"\0",
+          "hand-off: --readonly emits the atr; variant")
+
+    result = subprocess.run([TYMUX, "attach", "--shared", NAME2],
+                            env=term_env, capture_output=True, timeout=10)
+    check(result.stdout == b"\033}ats;" + NAME2.encode() + b"\0",
+          "hand-off: --shared emits the ats; variant")
+
+    NAME3 = "j%d" % os.getpid()
+    result = subprocess.run([TYMUX, "new", NAME3], env=term_env,
+                            capture_output=True, timeout=10)
+    check(result.returncode == 0 and
+          result.stdout == b"\033}at;" + NAME3.encode() + b"\0",
+          "hand-off: 'tymux new' also hands off the freshly created session")
+
+    # TYMUX_NO_NATIVE is the escape hatch: even with TERMINOLOGY set, the
+    # inline proxy runs as if nothing was set.
+    noenv = dict(term_env, TYMUX_NO_NATIVE="1")
+    forced = Client(["attach", NAME2], env=noenv)
+    check(forced.expect("$ "), "hand-off: TYMUX_NO_NATIVE forces the proxy")
+    forced.send("\x02d")
+    check(forced.wait() == 0, "hand-off: forced proxy detaches normally")
+
+    tymux("kill", NAME2)
+    tymux("kill", NAME3)
+
 
 try:
     main()

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to