Extend neww -S to work with -t as well as -t, GitHub issue 5571 from

Mazunki Hoksaas.
This commit is contained in:
nicm
2026-09-03 21:04:11 +00:00
committed by tmux update bot
parent 7b666e62be
commit a6f9a206b7
2 changed files with 49 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: cmd-new-window.c,v 1.103 2026/07/08 08:07:42 nicm Exp $ */ /* $OpenBSD: cmd-new-window.c,v 1.104 2026/09/03 21:04:11 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -60,7 +60,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
struct client *tc = cmdq_get_target_client(item); struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s; struct session *s = target->s;
struct winlink *wl = target->wl, *new_wl = NULL; struct winlink *wl = target->wl, *new_wl = NULL;
int idx = target->idx, before, count = args_count(args); int idx = target->idx, before;
int count = args_count(args);
char *cause = NULL, *cp, *expanded, *wname = NULL; char *cause = NULL, *cp, *expanded, *wname = NULL;
const char *template, *name; const char *template, *name;
struct cmd_find_state fs; struct cmd_find_state fs;
@@ -74,8 +75,10 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
} }
/* /*
* If -S and -n are given and -t is not and a single window with this * If -S is given, select an existing window instead of creating a
* name already exists, select it. * new one: preferring -t if it already points at a window, or
* otherwise -n if one exists with that name. If neither matches,
* fall through and create a window as normal.
*/ */
name = args_get(args, 'n'); name = args_get(args, 'n');
if (name != NULL) { if (name != NULL) {
@@ -88,32 +91,39 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
wname = clean_name(expanded, 0); wname = clean_name(expanded, 0);
free(expanded); free(expanded);
} }
if (args_has(args, 'S') && wname != NULL && target->idx == -1) { if (args_has(args, 'S')) {
expanded = format_single(item, wname, c, s, NULL, NULL); if (idx != -1)
RB_FOREACH(wl, winlinks, &s->windows) { new_wl = winlink_find_by_index(&s->windows, idx);
if (strcmp(wl->window->name, expanded) != 0) else if (wname != NULL) {
continue; expanded = format_single(item, wname, c, s, NULL, NULL);
if (new_wl == NULL) { RB_FOREACH(wl, winlinks, &s->windows) {
new_wl = wl; if (strcmp(wl->window->name, expanded) != 0)
continue; continue;
if (new_wl == NULL) {
new_wl = wl;
continue;
}
cmdq_error(item, "multiple windows named %s",
wname);
free(wname);
free(expanded);
return (CMD_RETURN_ERROR);
} }
cmdq_error(item, "multiple windows named %s", wname);
free(wname);
free(expanded); free(expanded);
return (CMD_RETURN_ERROR);
} }
free(expanded); }
if (new_wl != NULL) {
free(wname); /* Found an existing window to select instead of creating a new one. */
if (args_has(args, 'd')) if (new_wl != NULL) {
return (CMD_RETURN_NORMAL); free(wname);
if (session_set_current(s, new_wl) == 0) if (args_has(args, 'd'))
server_redraw_session(s);
if (c != NULL && c->session != NULL)
s->curw->window->latest = c;
recalculate_sizes();
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} if (session_set_current(s, new_wl) == 0)
server_redraw_session(s);
if (c != NULL && c->session != NULL)
s->curw->window->latest = c;
recalculate_sizes();
return (CMD_RETURN_NORMAL);
} }
before = args_has(args, 'b'); before = args_has(args, 'b');

19
tmux.1
View File

@@ -1,4 +1,4 @@
.\" $OpenBSD: tmux.1,v 1.1163 2026/09/01 12:49:49 nicm Exp $ .\" $OpenBSD: tmux.1,v 1.1164 2026/09/03 21:04:11 nicm Exp $
.\" .\"
.\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> .\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
.\" .\"
@@ -14,7 +14,7 @@
.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
.\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\" .\"
.Dd $Mdocdate: September 1 2026 $ .Dd $Mdocdate: September 3 2026 $
.Dt TMUX 1 .Dt TMUX 1
.Os .Os
.Sh NAME .Sh NAME
@@ -3588,13 +3588,20 @@ represents the window to be created; if the target already exists an error is
shown, unless the shown, unless the
.Fl k .Fl k
flag is used, in which case it is destroyed. flag is used, in which case it is destroyed.
.Ar window\-name
is the name given to the new window, if any.
.Pp
If If
.Fl S .Fl S
is given and a window named is given, an existing window is selected instead of creating a new one
.Ar window\-name (unless
already exists, it is selected (unless
.Fl d .Fl d
is also given in which case the command does nothing). is also given in which case the command does nothing):
.Ar target\-window
is preferred if it already identifies an existing window; otherwise a
window named
.Ar window\-name
is selected if one exists.
.Pp .Pp
.Ar shell\-command .Ar shell\-command
is the command to execute. is the command to execute.