tmux: restore the window and pane title on exit

The chat named the window and pane "S: <dir>" at startup but left it there
when it exited, so the shell prompt inherited the label of a program that was
gone. tmux_title now captures the previous window name, pane title and
automatic-rename value before it renames anything, and tmux_title_reset puts
them back:

- registered with atexit for the normal quit and the double-Ctrl-C exit,
- called from a SIGTERM/SIGHUP/SIGQUIT handler after endwin(), so a killed
  chat does not leave the terminal or the title behind (SIGINT stays the
  chat's own cancel key).

The commands still go through execvp, never a shell; the captured names are
argv entries. Manual check: "S: work" -> Ctrl-D -> the previous "OLD"/
"OLD_PANE" are back. README says so.

make check: all tests passed.
This commit is contained in:
lotherk committed 2026-11-18 07:15:36 +00:00
1 parent 4be6e26f78
commit 00aba079c5
2 files changed
+200 -29

No files matched your search

+4 -2
View File
@@ -340,8 +340,10 @@ When the chat starts under tmux, it names the window and the pane after the
directory it was started in, with `S: ` in front - `S: kidstube` in one window,
`S: splinter` in the next. tmux is asked directly (not through a shell, and the
directory is one argument), and `automatic-rename` is turned off for that
window so the foreground process does not replace the name. Outside tmux
nothing is emitted into the terminal.
window so the foreground process does not replace the name. On exit the names
tmux had before are put back: normal quit, a signal (SIGTERM/SIGHUP/SIGQUIT) and
the double-Ctrl-C exit all restore them, so the shell prompt does not inherit
`S:`. Outside tmux nothing is emitted into the terminal.
## Colour, measured
+196 -27
View File
@@ -37,12 +37,14 @@
#include <curses.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <iconv.h>
#include "decomp.h"
#include <locale.h>
#include <sys/wait.h>
#include <wchar.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
@@ -167,6 +169,7 @@ static void draw_history(struct ui_state *);
static void draw_sidebar(struct ui_state *, const char *, const char *,
struct sp_host *);
static void draw_statusbar(struct ui_state *);
static void tmux_title_reset(void);
static size_t add_entry(struct ui_state *, const char *, const char *);
static void drain_idle(struct ui_state *);
@@ -2896,6 +2899,7 @@ ui_turn(struct ui_state *s, const char *text)
c2 = wgetch(ui_input);
if (c2 == 3 || c2 == 4) {
endwin();
tmux_title_reset();
_exit(1);
}
if (c2 == ERR)
@@ -3005,6 +3009,152 @@ ui_start(struct ui_state *s, const struct ui_backend *be,
return 0;
}
/*
* tmux titles. The window and pane say "S: <dir>" while the chat runs; on
* exit the previous names go back, so a shell prompt does not inherit the
* label of a program that is gone. Captured before the first rename, restored
* from atexit and from the signal handler.
*/
static char tmux_pane[64];
static char tmux_prev_window[256];
static char tmux_prev_pane[512];
static char tmux_prev_auto[16] = "on";
static int tmux_titled;
static int tmux_restored;
/* run one tmux argument vector with no output; the screen belongs to curses */
static void
tmux_argv(char *const argv[])
{
pid_t p;
p = fork();
if (p == 0) {
int dn = open("/dev/null", O_WRONLY);
if (dn >= 0) {
(void)dup2(dn, STDOUT_FILENO);
(void)dup2(dn, STDERR_FILENO);
if (dn != STDERR_FILENO && dn != STDOUT_FILENO)
close(dn);
}
execvp("tmux", argv);
_exit(127);
}
if (p > 0)
while (waitpid(p, NULL, 0) < 0 && errno == EINTR)
;
}
/* read one tmux value: display-message, or show-options when what is NULL */
static int
tmux_read(const char *what, char *buf, size_t cap)
{
int fds[2], st = 0;
pid_t p;
ssize_t n;
size_t used = 0;
buf[0] = '\0';
if (pipe(fds) != 0)
return -1;
p = fork();
if (p == 0) {
int dn = open("/dev/null", O_WRONLY);
(void)dup2(fds[1], STDOUT_FILENO);
if (dn >= 0) {
(void)dup2(dn, STDERR_FILENO);
if (dn != STDERR_FILENO)
close(dn);
}
close(fds[0]);
close(fds[1]);
if (what != NULL)
execlp("tmux", "tmux", "display-message", "-p", "-t",
tmux_pane, what, (char *)NULL);
else
execlp("tmux", "tmux", "show-options", "-w", "-t",
tmux_pane, "-v", "automatic-rename",
(char *)NULL);
_exit(127);
}
close(fds[1]);
if (p < 0) {
close(fds[0]);
return -1;
}
while ((n = read(fds[0], buf + used, cap - 1 - used)) > 0) {
used += (size_t)n;
if (used >= cap - 1)
break;
}
close(fds[0]);
while (waitpid(p, &st, 0) < 0 && errno == EINTR)
;
buf[used] = '\0';
while (used > 0 && (buf[used - 1] == '\n' || buf[used - 1] == '\r'))
buf[--used] = '\0';
return (WIFEXITED(st) && WEXITSTATUS(st) == 0) ? 0 : -1;
}
/* put back what tmux had before the chat */
static void
tmux_title_reset(void)
{
char *a_auto[9];
if (!tmux_titled || tmux_restored)
return;
tmux_restored = 1;
a_auto[0] = "tmux";
a_auto[1] = "set-option";
a_auto[2] = "-w";
a_auto[3] = "-t";
a_auto[4] = tmux_pane;
a_auto[5] = "automatic-rename";
a_auto[6] = tmux_prev_auto[0] != '\0' ? tmux_prev_auto : "on";
a_auto[7] = NULL;
tmux_argv(a_auto);
if (tmux_prev_window[0] != '\0') {
char *a_win[7];
a_win[0] = "tmux";
a_win[1] = "rename-window";
a_win[2] = "-t";
a_win[3] = tmux_pane;
a_win[4] = tmux_prev_window;
a_win[5] = NULL;
tmux_argv(a_win);
}
{
char *a_pane[7];
a_pane[0] = "tmux";
a_pane[1] = "select-pane";
a_pane[2] = "-t";
a_pane[3] = tmux_pane;
a_pane[4] = "-T";
a_pane[5] = tmux_prev_pane;
a_pane[6] = NULL;
tmux_argv(a_pane);
}
}
/*
* A signal is an exit too: put the terminal and the title back before the
* default handler takes over. SIGINT is not ours - raw() turns Ctrl-C into a
* key, and the chat cancels a turn instead of dying.
*/
static void
tmux_signal(int sig)
{
endwin();
tmux_title_reset();
signal(sig, SIG_DFL);
raise(sig);
}
/*
* In tmux, say which tree this window is working in. The command is not run
* through a shell: the directory name is one argv, so a quote or a semicolon
@@ -3017,12 +3167,17 @@ tmux_title(void)
const char *tmux = getenv("TMUX");
const char *pane = getenv("TMUX_PANE");
char cwd[700], title[760], *base;
pid_t p;
if (tmux == NULL || *tmux == '\0' || pane == NULL || *pane == '\0')
return;
if (getcwd(cwd, sizeof(cwd)) == NULL || cwd[0] == '\0')
return;
snprintf(tmux_pane, sizeof(tmux_pane), "%s", pane);
(void)tmux_read("#{window_name}", tmux_prev_window,
sizeof(tmux_prev_window));
(void)tmux_read("#{pane_title}", tmux_prev_pane,
sizeof(tmux_prev_pane));
(void)tmux_read(NULL, tmux_prev_auto, sizeof(tmux_prev_auto));
base = strrchr(cwd, '/');
base = base != NULL ? base + 1 : cwd;
if (*base == '\0')
@@ -3030,36 +3185,50 @@ tmux_title(void)
snprintf(title, sizeof(title), "S: %s", base);
/*
* Keep tmux from replacing the name with the foreground process at the
* next refresh; then name the window and the pane. Three small forks at
* startup, no output into the curses screen.
* next refresh; then name the window and the pane. No output into the
* curses screen.
*/
p = fork();
if (p == 0) {
execlp("tmux", "tmux", "set-option", "-w", "-t", pane,
"automatic-rename", "off", (char *)NULL);
_exit(127);
{
char *a[9];
a[0] = "tmux";
a[1] = "set-option";
a[2] = "-w";
a[3] = "-t";
a[4] = tmux_pane;
a[5] = "automatic-rename";
a[6] = "off";
a[7] = NULL;
tmux_argv(a);
}
if (p > 0)
while (waitpid(p, NULL, 0) < 0 && errno == EINTR)
;
p = fork();
if (p == 0) {
execlp("tmux", "tmux", "rename-window", "-t", pane, title,
(char *)NULL);
_exit(127);
{
char *a[7];
a[0] = "tmux";
a[1] = "rename-window";
a[2] = "-t";
a[3] = tmux_pane;
a[4] = title;
a[5] = NULL;
tmux_argv(a);
}
if (p > 0)
while (waitpid(p, NULL, 0) < 0 && errno == EINTR)
;
p = fork();
if (p == 0) {
execlp("tmux", "tmux", "select-pane", "-t", pane, "-T", title,
(char *)NULL);
_exit(127);
{
char *a[8];
a[0] = "tmux";
a[1] = "select-pane";
a[2] = "-t";
a[3] = tmux_pane;
a[4] = "-T";
a[5] = title;
a[6] = NULL;
tmux_argv(a);
}
if (p > 0)
while (waitpid(p, NULL, 0) < 0 && errno == EINTR)
;
tmux_titled = 1;
(void)atexit(tmux_title_reset);
(void)signal(SIGTERM, tmux_signal);
(void)signal(SIGHUP, tmux_signal);
(void)signal(SIGQUIT, tmux_signal);
}
int