/src/tmux/cmd-new-window.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-new-window.c,v 1.104 2026/09/03 21:04:11 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | |
21 | | #include <errno.h> |
22 | | #include <fcntl.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | #include "tmux.h" |
28 | | |
29 | | /* |
30 | | * Create a new window. |
31 | | */ |
32 | | |
33 | 0 | #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}" |
34 | | |
35 | | static enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmdq_item *); |
36 | | |
37 | | const struct cmd_entry cmd_new_window_entry = { |
38 | | .name = "new-window", |
39 | | .alias = "neww", |
40 | | |
41 | | .args = { "abc:de:EF:kn:PSt:", 0, -1, NULL }, |
42 | | .usage = "[-abdEkPS] [-c start-directory] [-e environment] [-F format] " |
43 | | "[-n window-name] " CMD_TARGET_WINDOW_USAGE |
44 | | " [shell-command [argument ...]]", |
45 | | |
46 | | .target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX }, |
47 | | |
48 | | .flags = 0, |
49 | | .exec = cmd_new_window_exec |
50 | | }; |
51 | | |
52 | | static enum cmd_retval |
53 | | cmd_new_window_exec(struct cmd *self, struct cmdq_item *item) |
54 | 0 | { |
55 | 0 | struct args *args = cmd_get_args(self); |
56 | 0 | struct client *c = cmdq_get_client(item); |
57 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
58 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
59 | 0 | struct spawn_context sc = { 0 }; |
60 | 0 | struct client *tc = cmdq_get_target_client(item); |
61 | 0 | struct session *s = target->s; |
62 | 0 | struct winlink *wl = target->wl, *new_wl = NULL; |
63 | 0 | int idx = target->idx, before; |
64 | 0 | int count = args_count(args); |
65 | 0 | char *cause = NULL, *cp, *expanded, *wname = NULL; |
66 | 0 | const char *template, *name; |
67 | 0 | struct cmd_find_state fs; |
68 | 0 | struct args_value *av; |
69 | |
|
70 | 0 | if (args_has(args, 'E') && |
71 | 0 | count != 0 && |
72 | 0 | (count != 1 || *args_string(args, 0) != '\0')) { |
73 | 0 | cmdq_error(item, "command cannot be given for empty pane"); |
74 | 0 | return (CMD_RETURN_ERROR); |
75 | 0 | } |
76 | | |
77 | | /* |
78 | | * If -S is given, select an existing window instead of creating a |
79 | | * new one: preferring -t if it already points at a window, or |
80 | | * otherwise -n if one exists with that name. If neither matches, |
81 | | * fall through and create a window as normal. |
82 | | */ |
83 | 0 | name = args_get(args, 'n'); |
84 | 0 | if (name != NULL) { |
85 | 0 | expanded = format_single(item, name, c, s, NULL, NULL); |
86 | 0 | if (!check_name(expanded)) { |
87 | 0 | cmdq_error(item, "invalid window name: %s", expanded); |
88 | 0 | free(expanded); |
89 | 0 | return (CMD_RETURN_ERROR); |
90 | 0 | } |
91 | 0 | wname = clean_name(expanded, 0); |
92 | 0 | free(expanded); |
93 | 0 | } |
94 | 0 | if (args_has(args, 'S')) { |
95 | 0 | if (idx != -1) |
96 | 0 | new_wl = winlink_find_by_index(&s->windows, idx); |
97 | 0 | else if (wname != NULL) { |
98 | 0 | expanded = format_single(item, wname, c, s, NULL, NULL); |
99 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
100 | 0 | if (strcmp(wl->window->name, expanded) != 0) |
101 | 0 | continue; |
102 | 0 | if (new_wl == NULL) { |
103 | 0 | new_wl = wl; |
104 | 0 | continue; |
105 | 0 | } |
106 | 0 | cmdq_error(item, "multiple windows named %s", |
107 | 0 | wname); |
108 | 0 | free(wname); |
109 | 0 | free(expanded); |
110 | 0 | return (CMD_RETURN_ERROR); |
111 | 0 | } |
112 | 0 | free(expanded); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | /* Found an existing window to select instead of creating a new one. */ |
117 | 0 | if (new_wl != NULL) { |
118 | 0 | free(wname); |
119 | 0 | if (args_has(args, 'd')) |
120 | 0 | return (CMD_RETURN_NORMAL); |
121 | 0 | if (session_set_current(s, new_wl) == 0) |
122 | 0 | server_redraw_session(s); |
123 | 0 | if (c != NULL && c->session != NULL) |
124 | 0 | s->curw->window->latest = c; |
125 | 0 | recalculate_sizes(); |
126 | 0 | return (CMD_RETURN_NORMAL); |
127 | 0 | } |
128 | | |
129 | 0 | before = args_has(args, 'b'); |
130 | 0 | if (args_has(args, 'a') || before) { |
131 | 0 | idx = winlink_shuffle_up(s, wl, before); |
132 | 0 | if (idx == -1) |
133 | 0 | idx = target->idx; |
134 | 0 | } |
135 | |
|
136 | 0 | sc.item = item; |
137 | 0 | sc.s = s; |
138 | 0 | sc.tc = tc; |
139 | |
|
140 | 0 | sc.name = wname; |
141 | 0 | args_to_vector(args, &sc.argc, &sc.argv); |
142 | 0 | sc.environ = environ_create(); |
143 | |
|
144 | 0 | av = args_first_value(args, 'e'); |
145 | 0 | while (av != NULL) { |
146 | 0 | environ_put(sc.environ, av->string, 0); |
147 | 0 | av = args_next_value(av); |
148 | 0 | } |
149 | |
|
150 | 0 | sc.idx = idx; |
151 | 0 | sc.cwd = args_get(args, 'c'); |
152 | |
|
153 | 0 | sc.flags = 0; |
154 | 0 | if (args_has(args, 'E') || (count == 1 && *args_string(args, 0) == '\0')) |
155 | 0 | sc.flags |= SPAWN_EMPTY; |
156 | 0 | if (args_has(args, 'd')) |
157 | 0 | sc.flags |= SPAWN_DETACHED; |
158 | 0 | if (args_has(args, 'k')) |
159 | 0 | sc.flags |= SPAWN_KILL; |
160 | |
|
161 | 0 | if ((new_wl = spawn_window(&sc, &cause)) == NULL) { |
162 | 0 | cmdq_error(item, "create window failed: %s", cause); |
163 | 0 | free(cause); |
164 | 0 | goto fail; |
165 | 0 | } |
166 | 0 | if (!args_has(args, 'd') || new_wl == s->curw) { |
167 | 0 | cmd_find_from_winlink(current, new_wl, 0); |
168 | 0 | server_redraw_session_group(s); |
169 | 0 | } else |
170 | 0 | server_status_session_group(s); |
171 | |
|
172 | 0 | if (args_has(args, 'P')) { |
173 | 0 | if ((template = args_get(args, 'F')) == NULL) |
174 | 0 | template = NEW_WINDOW_TEMPLATE; |
175 | 0 | cp = format_single(item, template, tc, s, new_wl, |
176 | 0 | new_wl->window->active); |
177 | 0 | cmdq_print(item, "%s", cp); |
178 | 0 | free(cp); |
179 | 0 | } |
180 | |
|
181 | 0 | cmd_find_from_winlink(&fs, new_wl, 0); |
182 | 0 | cmdq_insert_hook(s, item, &fs, "after-new-window"); |
183 | |
|
184 | 0 | if (sc.argv != NULL) |
185 | 0 | cmd_free_argv(sc.argc, sc.argv); |
186 | 0 | environ_free(sc.environ); |
187 | 0 | free(wname); |
188 | 0 | return (CMD_RETURN_NORMAL); |
189 | | |
190 | 0 | fail: |
191 | 0 | if (sc.argv != NULL) |
192 | 0 | cmd_free_argv(sc.argc, sc.argv); |
193 | 0 | environ_free(sc.environ); |
194 | 0 | free(wname); |
195 | 0 | return (CMD_RETURN_ERROR); |
196 | 0 | } |