cmd-split-window.c revision 1.99
1/* $OpenBSD: cmd-split-window.c,v 1.99 2020/04/13 08:26:27 nicm Exp $ */
2
3/*
4 * Copyright (c) 2009 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 <paths.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "tmux.h"
29
30/*
31 * Split a window (add a new pane).
32 */
33
34#define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
35
36static enum cmd_retval	cmd_split_window_exec(struct cmd *,
37			    struct cmdq_item *);
38
39const struct cmd_entry cmd_split_window_entry = {
40	.name = "split-window",
41	.alias = "splitw",
42
43	.args = { "bc:de:fF:hIl:p:Pt:v", 0, -1 },
44	.usage = "[-bdefhIPv] [-c start-directory] [-e environment] "
45		 "[-F format] [-l size] " CMD_TARGET_PANE_USAGE " [command]",
46
47	.target = { 't', CMD_FIND_PANE, 0 },
48
49	.flags = 0,
50	.exec = cmd_split_window_exec
51};
52
53static enum cmd_retval
54cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
55{
56	struct args		*args = cmd_get_args(self);
57	struct cmd_find_state	*current = &item->shared->current;
58	struct spawn_context	 sc;
59	struct client		*c = cmd_find_client(item, NULL, 1);
60	struct session		*s = item->target.s;
61	struct winlink		*wl = item->target.wl;
62	struct window_pane	*wp = item->target.wp, *new_wp;
63	enum layout_type	 type;
64	struct layout_cell	*lc;
65	struct cmd_find_state	 fs;
66	int			 size, percentage, flags, input;
67	const char		*template, *add, *errstr, *p;
68	char			*cause, *cp, *copy;
69	size_t			 plen;
70	struct args_value	*value;
71
72	if (args_has(args, 'h'))
73		type = LAYOUT_LEFTRIGHT;
74	else
75		type = LAYOUT_TOPBOTTOM;
76	if ((p = args_get(args, 'l')) != NULL) {
77		plen = strlen(p);
78		if (p[plen - 1] == '%') {
79			copy = xstrdup(p);
80			copy[plen - 1] = '\0';
81			percentage = strtonum(copy, 0, INT_MAX, &errstr);
82			free(copy);
83			if (errstr != NULL) {
84				cmdq_error(item, "percentage %s", errstr);
85				return (CMD_RETURN_ERROR);
86			}
87			if (type == LAYOUT_TOPBOTTOM)
88				size = (wp->sy * percentage) / 100;
89			else
90				size = (wp->sx * percentage) / 100;
91		} else {
92			size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
93			if (cause != NULL) {
94				cmdq_error(item, "lines %s", cause);
95				free(cause);
96				return (CMD_RETURN_ERROR);
97			}
98		}
99	} else if (args_has(args, 'p')) {
100		percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
101		if (cause != NULL) {
102			cmdq_error(item, "create pane failed: -p %s", cause);
103			free(cause);
104			return (CMD_RETURN_ERROR);
105		}
106		if (type == LAYOUT_TOPBOTTOM)
107			size = (wp->sy * percentage) / 100;
108		else
109			size = (wp->sx * percentage) / 100;
110	} else
111		size = -1;
112
113	server_unzoom_window(wp->window);
114	input = (args_has(args, 'I') && args->argc == 0);
115
116	flags = 0;
117	if (args_has(args, 'b'))
118		flags |= SPAWN_BEFORE;
119	if (args_has(args, 'f'))
120		flags |= SPAWN_FULLSIZE;
121	if (input || (args->argc == 1 && *args->argv[0] == '\0'))
122		flags |= SPAWN_EMPTY;
123
124	lc = layout_split_pane(wp, type, size, flags);
125	if (lc == NULL) {
126		cmdq_error(item, "no space for new pane");
127		return (CMD_RETURN_ERROR);
128	}
129
130	memset(&sc, 0, sizeof sc);
131	sc.item = item;
132	sc.s = s;
133	sc.wl = wl;
134
135	sc.wp0 = wp;
136	sc.lc = lc;
137
138	sc.name = NULL;
139	sc.argc = args->argc;
140	sc.argv = args->argv;
141	sc.environ = environ_create();
142
143	add = args_first_value(args, 'e', &value);
144	while (add != NULL) {
145		environ_put(sc.environ, add, 0);
146		add = args_next_value(&value);
147	}
148
149	sc.idx = -1;
150	sc.cwd = args_get(args, 'c');
151
152	sc.flags = flags;
153	if (args_has(args, 'd'))
154		sc.flags |= SPAWN_DETACHED;
155
156	if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
157		cmdq_error(item, "create pane failed: %s", cause);
158		free(cause);
159		return (CMD_RETURN_ERROR);
160	}
161	if (input && window_pane_start_input(new_wp, item, &cause) != 0) {
162		layout_close_pane(new_wp);
163		window_remove_pane(wp->window, new_wp);
164		cmdq_error(item, "%s", cause);
165		free(cause);
166		return (CMD_RETURN_ERROR);
167	}
168	if (!args_has(args, 'd'))
169		cmd_find_from_winlink_pane(current, wl, new_wp, 0);
170	server_redraw_window(wp->window);
171	server_status_session(s);
172
173	if (args_has(args, 'P')) {
174		if ((template = args_get(args, 'F')) == NULL)
175			template = SPLIT_WINDOW_TEMPLATE;
176		cp = format_single(item, template, c, s, wl, new_wp);
177		cmdq_print(item, "%s", cp);
178		free(cp);
179	}
180
181	cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
182	cmdq_insert_hook(s, item, &fs, "after-split-window");
183
184	environ_free(sc.environ);
185	if (input)
186		return (CMD_RETURN_WAIT);
187	return (CMD_RETURN_NORMAL);
188}
189