xref: /netbsd-src/external/bsd/tmux/dist/cmd-new-window.c (revision f844e94ef29eebc7999c12636b87f541bb86868b)
15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv 
3698d5317Sjmmv /*
4f26e8bc9Schristos  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv  *
6698d5317Sjmmv  * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv  * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv  * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv  *
10698d5317Sjmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv  */
18698d5317Sjmmv 
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv 
21928fc495Schristos #include <errno.h>
22928fc495Schristos #include <fcntl.h>
23698d5317Sjmmv #include <stdlib.h>
24928fc495Schristos #include <string.h>
25928fc495Schristos #include <unistd.h>
26698d5317Sjmmv 
27698d5317Sjmmv #include "tmux.h"
28698d5317Sjmmv 
29698d5317Sjmmv /*
30698d5317Sjmmv  * Create a new window.
31698d5317Sjmmv  */
32698d5317Sjmmv 
335494e770Schristos #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
345494e770Schristos 
35e9a2d6faSchristos static enum cmd_retval	cmd_new_window_exec(struct cmd *, struct cmdq_item *);
36698d5317Sjmmv 
37698d5317Sjmmv const struct cmd_entry cmd_new_window_entry = {
38f26e8bc9Schristos 	.name = "new-window",
39f26e8bc9Schristos 	.alias = "neww",
40f26e8bc9Schristos 
4146548964Swiz 	.args = { "abc:de:F:kn:PSt:", 0, -1, NULL },
42e271dbb8Schristos 	.usage = "[-abdkPS] [-c start-directory] [-e environment] [-F format] "
4346548964Swiz 		 "[-n window-name] " CMD_TARGET_WINDOW_USAGE " [shell-command]",
44f26e8bc9Schristos 
45fe99a117Schristos 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },
46f26e8bc9Schristos 
47f26e8bc9Schristos 	.flags = 0,
48f26e8bc9Schristos 	.exec = cmd_new_window_exec
49698d5317Sjmmv };
50698d5317Sjmmv 
51e9a2d6faSchristos static enum cmd_retval
cmd_new_window_exec(struct cmd * self,struct cmdq_item * item)52e9a2d6faSchristos cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
53698d5317Sjmmv {
54e271dbb8Schristos 	struct args		*args = cmd_get_args(self);
55e271dbb8Schristos 	struct client		*c = cmdq_get_client(item);
56e271dbb8Schristos 	struct cmd_find_state	*current = cmdq_get_current(item);
57e271dbb8Schristos 	struct cmd_find_state	*target = cmdq_get_target(item);
5846548964Swiz 	struct spawn_context	 sc = { 0 };
59e271dbb8Schristos 	struct client		*tc = cmdq_get_target_client(item);
60e271dbb8Schristos 	struct session		*s = target->s;
6146548964Swiz 	struct winlink		*wl = target->wl, *new_wl = NULL;
62e271dbb8Schristos 	int			 idx = target->idx, before;
63*f844e94eSwiz 	char			*cause = NULL, *cp, *expanded;
6446548964Swiz 	const char		*template, *name;
65e9a2d6faSchristos 	struct cmd_find_state	 fs;
6646548964Swiz 	struct args_value	*av;
67698d5317Sjmmv 
68e271dbb8Schristos 	/*
69e271dbb8Schristos 	 * If -S and -n are given and -t is not and a single window with this
70e271dbb8Schristos 	 * name already exists, select it.
71e271dbb8Schristos 	 */
72e271dbb8Schristos 	name = args_get(args, 'n');
73e271dbb8Schristos 	if (args_has(args, 'S') && name != NULL && target->idx == -1) {
74*f844e94eSwiz 		expanded = format_single(item, name, c, s, NULL, NULL);
75e271dbb8Schristos 		RB_FOREACH(wl, winlinks, &s->windows) {
76*f844e94eSwiz 			if (strcmp(wl->window->name, expanded) != 0)
77e271dbb8Schristos 				continue;
78e271dbb8Schristos 			if (new_wl == NULL) {
79e271dbb8Schristos 				new_wl = wl;
80e271dbb8Schristos 				continue;
81e271dbb8Schristos 			}
82e271dbb8Schristos 			cmdq_error(item, "multiple windows named %s", name);
83*f844e94eSwiz 			free(expanded);
84928fc495Schristos 			return (CMD_RETURN_ERROR);
85698d5317Sjmmv 		}
86*f844e94eSwiz 		free(expanded);
87e271dbb8Schristos 		if (new_wl != NULL) {
88e271dbb8Schristos 			if (args_has(args, 'd'))
89e271dbb8Schristos 				return (CMD_RETURN_NORMAL);
90e271dbb8Schristos 			if (session_set_current(s, new_wl) == 0)
91e271dbb8Schristos 				server_redraw_session(s);
92e271dbb8Schristos 			if (c != NULL && c->session != NULL)
93e271dbb8Schristos 				s->curw->window->latest = c;
94e271dbb8Schristos 			recalculate_sizes();
95e271dbb8Schristos 			return (CMD_RETURN_NORMAL);
96e271dbb8Schristos 		}
97e271dbb8Schristos 	}
98e271dbb8Schristos 
99e271dbb8Schristos 	before = args_has(args, 'b');
100e271dbb8Schristos 	if (args_has(args, 'a') || before) {
101e271dbb8Schristos 		idx = winlink_shuffle_up(s, wl, before);
102e271dbb8Schristos 		if (idx == -1)
103e271dbb8Schristos 			idx = target->idx;
104e271dbb8Schristos 	}
105698d5317Sjmmv 
10630744affSchristos 	sc.item = item;
10730744affSchristos 	sc.s = s;
108e271dbb8Schristos 	sc.tc = tc;
10930744affSchristos 
11030744affSchristos 	sc.name = args_get(args, 'n');
11146548964Swiz 	args_to_vector(args, &sc.argc, &sc.argv);
11230744affSchristos 	sc.environ = environ_create();
11330744affSchristos 
11446548964Swiz 	av = args_first_value(args, 'e');
11546548964Swiz 	while (av != NULL) {
11646548964Swiz 		environ_put(sc.environ, av->string, 0);
11746548964Swiz 		av = args_next_value(av);
1185494e770Schristos 	}
1195494e770Schristos 
12030744affSchristos 	sc.idx = idx;
12130744affSchristos 	sc.cwd = args_get(args, 'c');
122928fc495Schristos 
12330744affSchristos 	sc.flags = 0;
12430744affSchristos 	if (args_has(args, 'd'))
12530744affSchristos 		sc.flags |= SPAWN_DETACHED;
12630744affSchristos 	if (args_has(args, 'k'))
12730744affSchristos 		sc.flags |= SPAWN_KILL;
128928fc495Schristos 
12930744affSchristos 	if ((new_wl = spawn_window(&sc, &cause)) == NULL) {
130e9a2d6faSchristos 		cmdq_error(item, "create window failed: %s", cause);
131928fc495Schristos 		free(cause);
13246548964Swiz 		if (sc.argv != NULL)
13346548964Swiz 			cmd_free_argv(sc.argc, sc.argv);
13446548964Swiz 		environ_free(sc.environ);
13530744affSchristos 		return (CMD_RETURN_ERROR);
136698d5317Sjmmv 	}
13730744affSchristos 	if (!args_has(args, 'd') || new_wl == s->curw) {
13830744affSchristos 		cmd_find_from_winlink(current, new_wl, 0);
139698d5317Sjmmv 		server_redraw_session_group(s);
140698d5317Sjmmv 	} else
141698d5317Sjmmv 		server_status_session_group(s);
142698d5317Sjmmv 
143928fc495Schristos 	if (args_has(args, 'P')) {
144928fc495Schristos 		if ((template = args_get(args, 'F')) == NULL)
145928fc495Schristos 			template = NEW_WINDOW_TEMPLATE;
146e271dbb8Schristos 		cp = format_single(item, template, tc, s, new_wl,
14768e6ba84Schristos 			new_wl->window->active);
148e9a2d6faSchristos 		cmdq_print(item, "%s", cp);
149928fc495Schristos 		free(cp);
150928fc495Schristos 	}
151928fc495Schristos 
15230744affSchristos 	cmd_find_from_winlink(&fs, new_wl, 0);
15330744affSchristos 	cmdq_insert_hook(s, item, &fs, "after-new-window");
154e9a2d6faSchristos 
15546548964Swiz 	if (sc.argv != NULL)
15646548964Swiz 		cmd_free_argv(sc.argc, sc.argv);
15730744affSchristos 	environ_free(sc.environ);
158928fc495Schristos 	return (CMD_RETURN_NORMAL);
159698d5317Sjmmv }
160