xref: /openbsd-src/usr.bin/tmux/cmd-join-pane.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-join-pane.c,v 1.38 2020/01/02 13:44:17 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2011 George Nachman <tmux@georgester.com>
5  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <paths.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Join or move a pane into another (like split/swap/kill).
31  */
32 
33 static enum cmd_retval	cmd_join_pane_exec(struct cmd *, struct cmdq_item *);
34 
35 const struct cmd_entry cmd_join_pane_entry = {
36 	.name = "join-pane",
37 	.alias = "joinp",
38 
39 	.args = { "bdfhvp:l:s:t:", 0, 0 },
40 	.usage = "[-bdfhv] [-l size] " CMD_SRCDST_PANE_USAGE,
41 
42 	.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
43 	.target = { 't', CMD_FIND_PANE, 0 },
44 
45 	.flags = 0,
46 	.exec = cmd_join_pane_exec
47 };
48 
49 const struct cmd_entry cmd_move_pane_entry = {
50 	.name = "move-pane",
51 	.alias = "movep",
52 
53 	.args = { "bdhvp:l:s:t:", 0, 0 },
54 	.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
55 
56 	.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
57 	.target = { 't', CMD_FIND_PANE, 0 },
58 
59 	.flags = 0,
60 	.exec = cmd_join_pane_exec
61 };
62 
63 static enum cmd_retval
64 cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
65 {
66 	struct args		*args = self->args;
67 	struct cmd_find_state	*current = &item->shared->current;
68 	struct session		*dst_s;
69 	struct winlink		*src_wl, *dst_wl;
70 	struct window		*src_w, *dst_w;
71 	struct window_pane	*src_wp, *dst_wp;
72 	char			*cause, *copy;
73 	const char		*errstr, *p;
74 	size_t			 plen;
75 	int			 size, percentage, dst_idx, not_same_window;
76 	int			 flags;
77 	enum layout_type	 type;
78 	struct layout_cell	*lc;
79 
80 	if (self->entry == &cmd_join_pane_entry)
81 		not_same_window = 1;
82 	else
83 		not_same_window = 0;
84 
85 	dst_s = item->target.s;
86 	dst_wl = item->target.wl;
87 	dst_wp = item->target.wp;
88 	dst_w = dst_wl->window;
89 	dst_idx = dst_wl->idx;
90 	server_unzoom_window(dst_w);
91 
92 	src_wl = item->source.wl;
93 	src_wp = item->source.wp;
94 	src_w = src_wl->window;
95 	server_unzoom_window(src_w);
96 
97 	if (not_same_window && src_w == dst_w) {
98 		cmdq_error(item, "can't join a pane to its own window");
99 		return (CMD_RETURN_ERROR);
100 	}
101 	if (!not_same_window && src_wp == dst_wp) {
102 		cmdq_error(item, "source and target panes must be different");
103 		return (CMD_RETURN_ERROR);
104 	}
105 
106 	type = LAYOUT_TOPBOTTOM;
107 	if (args_has(args, 'h'))
108 		type = LAYOUT_LEFTRIGHT;
109 
110 	size = -1;
111 	if ((p = args_get(args, 'l')) != NULL) {
112 		plen = strlen(p);
113 		if (p[plen - 1] == '%') {
114 			copy = xstrdup(p);
115 			copy[plen - 1] = '\0';
116 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
117 			free(copy);
118 			if (errstr != NULL) {
119 				cmdq_error(item, "percentage %s", errstr);
120 				return (CMD_RETURN_ERROR);
121 			}
122 			if (type == LAYOUT_TOPBOTTOM)
123 				size = (dst_wp->sy * percentage) / 100;
124 			else
125 				size = (dst_wp->sx * percentage) / 100;
126 		} else {
127 			size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
128 			if (cause != NULL) {
129 				cmdq_error(item, "size %s", cause);
130 				free(cause);
131 				return (CMD_RETURN_ERROR);
132 			}
133 		}
134 	} else if (args_has(args, 'p')) {
135 		percentage = args_strtonum(args, 'p', 0, 100, &cause);
136 		if (cause != NULL) {
137 			cmdq_error(item, "percentage %s", cause);
138 			free(cause);
139 			return (CMD_RETURN_ERROR);
140 		}
141 		if (type == LAYOUT_TOPBOTTOM)
142 			size = (dst_wp->sy * percentage) / 100;
143 		else
144 			size = (dst_wp->sx * percentage) / 100;
145 	}
146 
147 	flags = 0;
148 	if (args_has(args, 'b'))
149 		flags |= SPAWN_BEFORE;
150 	if (args_has(args, 'f'))
151 		flags |= SPAWN_FULLSIZE;
152 
153 	lc = layout_split_pane(dst_wp, type, size, flags);
154 	if (lc == NULL) {
155 		cmdq_error(item, "create pane failed: pane too small");
156 		return (CMD_RETURN_ERROR);
157 	}
158 
159 	layout_close_pane(src_wp);
160 
161 	window_lost_pane(src_w, src_wp);
162 	TAILQ_REMOVE(&src_w->panes, src_wp, entry);
163 
164 	src_wp->window = dst_w;
165 	options_set_parent(src_wp->options, dst_w->options);
166 	src_wp->flags |= PANE_STYLECHANGED;
167 	TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
168 	layout_assign_pane(lc, src_wp);
169 
170 	recalculate_sizes();
171 
172 	server_redraw_window(src_w);
173 	server_redraw_window(dst_w);
174 
175 	if (!args_has(args, 'd')) {
176 		window_set_active_pane(dst_w, src_wp, 1);
177 		session_select(dst_s, dst_idx);
178 		cmd_find_from_session(current, dst_s, 0);
179 		server_redraw_session(dst_s);
180 	} else
181 		server_status_session(dst_s);
182 
183 	if (window_count_panes(src_w) == 0)
184 		server_kill_window(src_w);
185 	else
186 		notify_window("window-layout-changed", src_w);
187 	notify_window("window-layout-changed", dst_w);
188 
189 	return (CMD_RETURN_NORMAL);
190 }
191