98fae82da42d038b9ed0c87b557524fd03345191 tmux support diff --git a/config.def.h b/config.def.h index b41747f..ed4a329 100644 --- a/config.def.h +++ b/config.def.h @@ -20,6 +20,14 @@ static char shell[] = "/bin/sh"; static char *utmp = NULL; static char stty_args[] = "stty raw pass8 nl -echo -iexten -cstopb 38400"; +/* Multiplexer wrapper */ +static int use_mux = 0; +static char *mux_new[] = {"/usr/bin/tmux", "new-session", "-s", NULL, "--"}; +static char mux_post[] = + "tmux list-clients -t \"$MUX_NM\" 2>/dev/null" + "| { read -r _ && ! read -r _; }" + "&& tmux kill-session -t \"$MUX_NM\""; + /* identification sequence returned in DA and DECID */ static char vtiden[] = "\033[?6c"; diff --git a/st.c b/st.c index 2594c65..d41b805 100644 --- a/st.c +++ b/st.c @@ -560,6 +560,9 @@ typedef struct { static Fontcache frc[16]; static int frclen = 0; +/* Multiplexer session name */ +static char mux_nm[64] = {0}; + ssize_t xwrite(int fd, const char *s, size_t len) { @@ -1366,6 +1369,16 @@ execsh(void) signal(SIGTERM, SIG_DFL); signal(SIGALRM, SIG_DFL); + if (use_mux) { + int i = sizeof(mux_new)/sizeof(mux_new[0]), j = 0; + while(args[j++]); + char *margs[i + j]; + while(j--) { margs[i + j] = args[j]; } + while(i--) { margs[i] = mux_new[i] ? mux_new[i] : mux_nm; } + execvp(mux_new[0], margs); + _exit(1); + } + execvp(prog, args); _exit(1); } @@ -4177,6 +4190,11 @@ cmessage(XEvent *e) xw.state &= ~WIN_FOCUSED; } } else if (e->xclient.data.l[0] == xw.wmdeletewin) { + if (use_mux) { + setenv("MUX_NM", mux_nm, 1); + if (system(mux_post) != 0) + fprintf(stderr, "mux_post execution error\n"); + } /* Send SIGHUP to shell */ kill(pid, SIGHUP); exit(0); @@ -4325,7 +4343,7 @@ usage(void) die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" " [-n name] [-o file]\n" " [-T title] [-t title] [-w windowid]" - " [[-e] command [args ...]]\n" + " [-m|-M] [[-e] command [args ...]]\n" " %s [-aiv] [-c class] [-f font] [-g geometry]" " [-n name] [-o file]\n" " [-T title] [-t title] [-w windowid] -l line" @@ -4368,6 +4386,12 @@ main(int argc, char *argv[]) case 'l': opt_line = EARGF(usage()); break; + case 'm': + use_mux = 1; + break; + case 'M': + use_mux = 0; + break; case 'n': opt_name = EARGF(usage()); break; @@ -4392,6 +4416,9 @@ run: if (!opt_title && !opt_line) opt_title = basename(xstrdup(argv[0])); } + if (use_mux) { + snprintf(mux_nm, sizeof(mux_nm), "%.43s-%d", (opt_name ? opt_name : "st"), getpid()); + } setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); tnew(MAX(cols, 1), MAX(rows, 1));