xref: /openbsd-src/usr.bin/tmux/cmd-split-window.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: cmd-split-window.c,v 1.95 2019/05/03 20:44:24 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 
36 static enum cmd_retval	cmd_split_window_exec(struct cmd *,
37 			    struct cmdq_item *);
38 
39 const 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] [-p percentage|-l size] " CMD_TARGET_PANE_USAGE
46 		 " [command]",
47 
48 	.target = { 't', CMD_FIND_PANE, 0 },
49 
50 	.flags = 0,
51 	.exec = cmd_split_window_exec
52 };
53 
54 static enum cmd_retval
55 cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
56 {
57 	struct args		*args = self->args;
58 	struct cmd_find_state	*current = &item->shared->current;
59 	struct spawn_context	 sc;
60 	struct client		*c = cmd_find_client(item, NULL, 1);
61 	struct session		*s = item->target.s;
62 	struct winlink		*wl = item->target.wl;
63 	struct window_pane	*wp = item->target.wp, *new_wp;
64 	enum layout_type	 type;
65 	struct layout_cell	*lc;
66 	struct cmd_find_state	 fs;
67 	int			 size, percentage, flags, input;
68 	const char		*template, *add;
69 	char			*cause, *cp;
70 	struct args_value	*value;
71 
72 	if (args_has(args, 'h'))
73 		type = LAYOUT_LEFTRIGHT;
74 	else
75 		type = LAYOUT_TOPBOTTOM;
76 	if (args_has(args, 'l')) {
77 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
78 		if (cause != NULL) {
79 			cmdq_error(item, "create pane failed: -l %s", cause);
80 			free(cause);
81 			return (CMD_RETURN_ERROR);
82 		}
83 	} else if (args_has(args, 'p')) {
84 		percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
85 		if (cause != NULL) {
86 			cmdq_error(item, "create pane failed: -p %s", cause);
87 			free(cause);
88 			return (CMD_RETURN_ERROR);
89 		}
90 		if (type == LAYOUT_TOPBOTTOM)
91 			size = (wp->sy * percentage) / 100;
92 		else
93 			size = (wp->sx * percentage) / 100;
94 	} else
95 		size = -1;
96 
97 	server_unzoom_window(wp->window);
98 	input = (args_has(args, 'I') && args->argc == 0);
99 
100 	flags = 0;
101 	if (args_has(args, 'b'))
102 		flags |= SPAWN_BEFORE;
103 	if (args_has(args, 'f'))
104 		flags |= SPAWN_FULLSIZE;
105 	if (input || (args->argc == 1 && *args->argv[0] == '\0'))
106 		flags |= SPAWN_EMPTY;
107 
108 	lc = layout_split_pane(wp, type, size, flags);
109 	if (lc == NULL) {
110 		cmdq_error(item, "no space for new pane");
111 		return (CMD_RETURN_ERROR);
112 	}
113 
114 	memset(&sc, 0, sizeof sc);
115 	sc.item = item;
116 	sc.s = s;
117 	sc.wl = wl;
118 
119 	sc.wp0 = wp;
120 	sc.lc = lc;
121 
122 	sc.name = NULL;
123 	sc.argc = args->argc;
124 	sc.argv = args->argv;
125 	sc.environ = environ_create();
126 
127 	add = args_first_value(args, 'e', &value);
128 	while (add != NULL) {
129 		environ_put(sc.environ, add);
130 		add = args_next_value(&value);
131 	}
132 
133 	sc.idx = -1;
134 	sc.cwd = args_get(args, 'c');
135 
136 	sc.flags = flags;
137 	if (args_has(args, 'd'))
138 		sc.flags |= SPAWN_DETACHED;
139 
140 	if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
141 		layout_close_pane(new_wp);
142 		cmdq_error(item, "create pane failed: %s", cause);
143 		free(cause);
144 		return (CMD_RETURN_ERROR);
145 	}
146 	if (input && window_pane_start_input(new_wp, item, &cause) != 0) {
147 		layout_close_pane(new_wp);
148 		window_remove_pane(wp->window, new_wp);
149 		cmdq_error(item, "%s", cause);
150 		free(cause);
151 		return (CMD_RETURN_ERROR);
152 	}
153 	if (!args_has(args, 'd'))
154 		cmd_find_from_winlink_pane(current, wl, new_wp, 0);
155 	server_redraw_window(wp->window);
156 	server_status_session(s);
157 
158 	if (args_has(args, 'P')) {
159 		if ((template = args_get(args, 'F')) == NULL)
160 			template = SPLIT_WINDOW_TEMPLATE;
161 		cp = format_single(item, template, c, s, wl, new_wp);
162 		cmdq_print(item, "%s", cp);
163 		free(cp);
164 	}
165 
166 	cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
167 	cmdq_insert_hook(s, item, &fs, "after-split-window");
168 
169 	environ_free(sc.environ);
170 	if (input)
171 		return (CMD_RETURN_WAIT);
172 	return (CMD_RETURN_NORMAL);
173 }
174