xref: /netbsd-src/external/bsd/tmux/dist/cmd-split-window.c (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1 /* $OpenBSD$ */
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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Split a window (add a new pane).
31  */
32 
33 #define SPLIT_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
34 
35 static enum cmd_retval	cmd_split_window_exec(struct cmd *,
36 			    struct cmdq_item *);
37 
38 const struct cmd_entry cmd_split_window_entry = {
39 	.name = "split-window",
40 	.alias = "splitw",
41 
42 	.args = { "bc:de:fF:hIl:p:Pt:vZ", 0, -1, NULL },
43 	.usage = "[-bdefhIPvZ] [-c start-directory] [-e environment] "
44 		 "[-F format] [-l size] " CMD_TARGET_PANE_USAGE
45 		 "[shell-command]",
46 
47 	.target = { 't', CMD_FIND_PANE, 0 },
48 
49 	.flags = 0,
50 	.exec = cmd_split_window_exec
51 };
52 
53 static enum cmd_retval
54 cmd_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 = cmdq_get_current(item);
58 	struct cmd_find_state	*target = cmdq_get_target(item);
59 	struct spawn_context	 sc = { 0 };
60 	struct client		*tc = cmdq_get_target_client(item);
61 	struct session		*s = target->s;
62 	struct winlink		*wl = target->wl;
63 	struct window		*w = wl->window;
64 	struct window_pane	*wp = target->wp, *new_wp;
65 	enum layout_type	 type;
66 	struct layout_cell	*lc;
67 	struct cmd_find_state	 fs;
68 	int			 size, percentage, flags, input;
69 	const char		*template, *errstr, *p;
70 	char			*cause, *cp, *copy;
71 	size_t			 plen;
72 	struct args_value	*av;
73 	u_int			 count = args_count(args);
74 
75 	if (args_has(args, 'h'))
76 		type = LAYOUT_LEFTRIGHT;
77 	else
78 		type = LAYOUT_TOPBOTTOM;
79 	if ((p = args_get(args, 'l')) != NULL) {
80 		plen = strlen(p);
81 		if (p[plen - 1] == '%') {
82 			copy = xstrdup(p);
83 			copy[plen - 1] = '\0';
84 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
85 			free(copy);
86 			if (errstr != NULL) {
87 				cmdq_error(item, "percentage %s", errstr);
88 				return (CMD_RETURN_ERROR);
89 			}
90 			if (args_has(args, 'f')) {
91 				if (type == LAYOUT_TOPBOTTOM)
92 					size = (w->sy * percentage) / 100;
93 				else
94 					size = (w->sx * percentage) / 100;
95 			} else {
96 				if (type == LAYOUT_TOPBOTTOM)
97 					size = (wp->sy * percentage) / 100;
98 				else
99 					size = (wp->sx * percentage) / 100;
100 			}
101 		} else {
102 			size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
103 			if (cause != NULL) {
104 				cmdq_error(item, "lines %s", cause);
105 				free(cause);
106 				return (CMD_RETURN_ERROR);
107 			}
108 		}
109 	} else if (args_has(args, 'p')) {
110 		percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
111 		if (cause != NULL) {
112 			cmdq_error(item, "create pane failed: -p %s", cause);
113 			free(cause);
114 			return (CMD_RETURN_ERROR);
115 		}
116 		if (args_has(args, 'f')) {
117 			if (type == LAYOUT_TOPBOTTOM)
118 				size = (w->sy * percentage) / 100;
119 			else
120 				size = (w->sx * percentage) / 100;
121 		} else {
122 			if (type == LAYOUT_TOPBOTTOM)
123 				size = (wp->sy * percentage) / 100;
124 			else
125 				size = (wp->sx * percentage) / 100;
126 		}
127 	} else
128 		size = -1;
129 
130 	window_push_zoom(wp->window, 1, args_has(args, 'Z'));
131 	input = (args_has(args, 'I') && count == 0);
132 
133 	flags = 0;
134 	if (args_has(args, 'b'))
135 		flags |= SPAWN_BEFORE;
136 	if (args_has(args, 'f'))
137 		flags |= SPAWN_FULLSIZE;
138 	if (input || (count == 1 && *args_string(args, 0) == '\0'))
139 		flags |= SPAWN_EMPTY;
140 
141 	lc = layout_split_pane(wp, type, size, flags);
142 	if (lc == NULL) {
143 		cmdq_error(item, "no space for new pane");
144 		return (CMD_RETURN_ERROR);
145 	}
146 
147 	sc.item = item;
148 	sc.s = s;
149 	sc.wl = wl;
150 
151 	sc.wp0 = wp;
152 	sc.lc = lc;
153 
154 	args_to_vector(args, &sc.argc, &sc.argv);
155 	sc.environ = environ_create();
156 
157 	av = args_first_value(args, 'e');
158 	while (av != NULL) {
159 		environ_put(sc.environ, av->string, 0);
160 		av = args_next_value(av);
161 	}
162 
163 	sc.idx = -1;
164 	sc.cwd = args_get(args, 'c');
165 
166 	sc.flags = flags;
167 	if (args_has(args, 'd'))
168 		sc.flags |= SPAWN_DETACHED;
169 	if (args_has(args, 'Z'))
170 		sc.flags |= SPAWN_ZOOM;
171 
172 	if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
173 		cmdq_error(item, "create pane failed: %s", cause);
174 		free(cause);
175 		if (sc.argv != NULL)
176 			cmd_free_argv(sc.argc, sc.argv);
177 		environ_free(sc.environ);
178 		return (CMD_RETURN_ERROR);
179 	}
180 	if (input) {
181 		switch (window_pane_start_input(new_wp, item, &cause)) {
182 		case -1:
183 			server_client_remove_pane(new_wp);
184 			layout_close_pane(new_wp);
185 			window_remove_pane(wp->window, new_wp);
186 			cmdq_error(item, "%s", cause);
187 			free(cause);
188 			if (sc.argv != NULL)
189 				cmd_free_argv(sc.argc, sc.argv);
190 			environ_free(sc.environ);
191 			return (CMD_RETURN_ERROR);
192 		case 1:
193 			input = 0;
194 			break;
195 		}
196 	}
197 	if (!args_has(args, 'd'))
198 		cmd_find_from_winlink_pane(current, wl, new_wp, 0);
199 	window_pop_zoom(wp->window);
200 	server_redraw_window(wp->window);
201 	server_status_session(s);
202 
203 	if (args_has(args, 'P')) {
204 		if ((template = args_get(args, 'F')) == NULL)
205 			template = SPLIT_WINDOW_TEMPLATE;
206 		cp = format_single(item, template, tc, s, wl, new_wp);
207 		cmdq_print(item, "%s", cp);
208 		free(cp);
209 	}
210 
211 	cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
212 	cmdq_insert_hook(s, item, &fs, "after-split-window");
213 
214 	if (sc.argv != NULL)
215 		cmd_free_argv(sc.argc, sc.argv);
216 	environ_free(sc.environ);
217 	if (input)
218 		return (CMD_RETURN_WAIT);
219 	return (CMD_RETURN_NORMAL);
220 }
221