diff --git a/server-client.c b/server-client.c index 570c1ed9..c5c702b3 100644 --- a/server-client.c +++ b/server-client.c @@ -3683,6 +3683,8 @@ server_client_set_flags(struct client *c, const char *flags) flag = CLIENT_IGNORESIZE; else if (strcmp(next, "active-pane") == 0) flag = CLIENT_ACTIVEPANE; + else if (strcmp(next, "no-detach-on-destroy") == 0) + flag = CLIENT_NO_DETACH_ON_DESTROY; if (flag == 0) continue; @@ -3716,6 +3718,8 @@ server_client_get_flags(struct client *c) strlcat(s, "control-mode,", sizeof s); if (c->flags & CLIENT_IGNORESIZE) strlcat(s, "ignore-size,", sizeof s); + if (c->flags & CLIENT_NO_DETACH_ON_DESTROY) + strlcat(s, "no-detach-on-destroy,", sizeof s); if (c->flags & CLIENT_CONTROL_NOOUTPUT) strlcat(s, "no-output,", sizeof s); if (c->flags & CLIENT_CONTROL_WAITEXIT) diff --git a/server-fn.c b/server-fn.c index 3435ad85..9518c43a 100644 --- a/server-fn.c +++ b/server-fn.c @@ -425,7 +425,7 @@ void server_destroy_session(struct session *s) { struct client *c; - struct session *s_new = NULL; + struct session *s_new = NULL, *cs_new, *use_s; int detach_on_destroy; detach_on_destroy = options_get_number(s->options, "detach-on-destroy"); @@ -437,15 +437,26 @@ server_destroy_session(struct session *s) s_new = session_previous_session(s); else if (detach_on_destroy == 4) s_new = session_next_session(s); - if (s_new == s) - s_new = NULL; + + /* + * If no suitable new session was found above, then look for any + * session as an alternative in case a client needs it. + */ + if (s_new == NULL && + (detach_on_destroy == 1 || detach_on_destroy == 2)) + cs_new = server_find_session(s, server_newer_session); + TAILQ_FOREACH(c, &clients, entry) { if (c->session != s) continue; + use_s = s_new; + if (use_s == NULL && (c->flags & CLIENT_NO_DETACH_ON_DESTROY)) + use_s = cs_new; + c->session = NULL; c->last_session = NULL; - server_client_set_session(c, s_new); - if (s_new == NULL) + server_client_set_session(c, use_s); + if (use_s == NULL) c->flags |= CLIENT_EXIT; } recalculate_sizes(); diff --git a/tmux.1 b/tmux.1 index 41c6e6d3..388e9a7f 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1066,6 +1066,9 @@ The flags are: the client has an independent active pane .It ignore-size the client does not affect the size of other clients +.It no-detach-on-destroy +do not detach the client when the session it is attached to is destroyed if +there are any other sessions .It no-output the client does not receive pane output in control mode .It pause-after=seconds diff --git a/tmux.h b/tmux.h index 9ef733c3..ae25c5a9 100644 --- a/tmux.h +++ b/tmux.h @@ -1924,6 +1924,7 @@ struct client { #define CLIENT_BRACKETPASTING 0x1000000000ULL #define CLIENT_ASSUMEPASTING 0x2000000000ULL #define CLIENT_REDRAWSCROLLBARS 0x4000000000ULL +#define CLIENT_NO_DETACH_ON_DESTROY 0x8000000000ULL #define CLIENT_ALLREDRAWFLAGS \ (CLIENT_REDRAWWINDOW| \ CLIENT_REDRAWSTATUS| \