xref: /openbsd-src/usr.bin/tmux/cmd-join-pane.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: cmd-join-pane.c,v 1.23 2016/01/19 15:59:12 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 <unistd.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Join or move a pane into another (like split/swap/kill).
30  */
31 
32 enum cmd_retval	 cmd_join_pane_exec(struct cmd *, struct cmd_q *);
33 
34 enum cmd_retval	 join_pane(struct cmd *, struct cmd_q *, int);
35 
36 const struct cmd_entry cmd_join_pane_entry = {
37 	.name = "join-pane",
38 	.alias = "joinp",
39 
40 	.args = { "bdhvp:l:s:t:", 0, 0 },
41 	.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
42 
43 	.sflag = CMD_PANE_MARKED,
44 	.tflag = CMD_PANE,
45 
46 	.flags = 0,
47 	.exec = cmd_join_pane_exec
48 };
49 
50 const struct cmd_entry cmd_move_pane_entry = {
51 	.name = "move-pane",
52 	.alias = "movep",
53 
54 	.args = { "bdhvp:l:s:t:", 0, 0 },
55 	.usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
56 
57 	.sflag = CMD_PANE,
58 	.tflag = CMD_PANE,
59 
60 	.flags = 0,
61 	.exec = cmd_join_pane_exec
62 };
63 
64 enum cmd_retval
65 cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq)
66 {
67 	return (join_pane(self, cmdq, self->entry == &cmd_join_pane_entry));
68 }
69 
70 enum cmd_retval
71 join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window)
72 {
73 	struct args		*args = self->args;
74 	struct session		*dst_s;
75 	struct winlink		*src_wl, *dst_wl;
76 	struct window		*src_w, *dst_w;
77 	struct window_pane	*src_wp, *dst_wp;
78 	char			*cause;
79 	int			 size, percentage, dst_idx;
80 	enum layout_type	 type;
81 	struct layout_cell	*lc;
82 
83 	dst_s = cmdq->state.tflag.s;
84 	dst_wl = cmdq->state.tflag.wl;
85 	dst_wp = cmdq->state.tflag.wp;
86 	dst_w = dst_wl->window;
87 	dst_idx = dst_wl->idx;
88 	server_unzoom_window(dst_w);
89 
90 	src_wl = cmdq->state.sflag.wl;
91 	src_wp = cmdq->state.sflag.wp;
92 	src_w = src_wl->window;
93 	server_unzoom_window(src_w);
94 
95 	if (not_same_window && src_w == dst_w) {
96 		cmdq_error(cmdq, "can't join a pane to its own window");
97 		return (CMD_RETURN_ERROR);
98 	}
99 	if (!not_same_window && src_wp == dst_wp) {
100 		cmdq_error(cmdq, "source and target panes must be different");
101 		return (CMD_RETURN_ERROR);
102 	}
103 
104 	type = LAYOUT_TOPBOTTOM;
105 	if (args_has(args, 'h'))
106 		type = LAYOUT_LEFTRIGHT;
107 
108 	size = -1;
109 	if (args_has(args, 'l')) {
110 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
111 		if (cause != NULL) {
112 			cmdq_error(cmdq, "size %s", cause);
113 			free(cause);
114 			return (CMD_RETURN_ERROR);
115 		}
116 	} else if (args_has(args, 'p')) {
117 		percentage = args_strtonum(args, 'p', 0, 100, &cause);
118 		if (cause != NULL) {
119 			cmdq_error(cmdq, "percentage %s", cause);
120 			free(cause);
121 			return (CMD_RETURN_ERROR);
122 		}
123 		if (type == LAYOUT_TOPBOTTOM)
124 			size = (dst_wp->sy * percentage) / 100;
125 		else
126 			size = (dst_wp->sx * percentage) / 100;
127 	}
128 	lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b'));
129 	if (lc == NULL) {
130 		cmdq_error(cmdq, "create pane failed: pane too small");
131 		return (CMD_RETURN_ERROR);
132 	}
133 
134 	layout_close_pane(src_wp);
135 
136 	window_lost_pane(src_w, src_wp);
137 	TAILQ_REMOVE(&src_w->panes, src_wp, entry);
138 
139 	if (window_count_panes(src_w) == 0)
140 		server_kill_window(src_w);
141 	else
142 		notify_window_layout_changed(src_w);
143 
144 	src_wp->window = dst_w;
145 	TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
146 	layout_assign_pane(lc, src_wp);
147 
148 	recalculate_sizes();
149 
150 	server_redraw_window(src_w);
151 	server_redraw_window(dst_w);
152 
153 	if (!args_has(args, 'd')) {
154 		window_set_active_pane(dst_w, src_wp);
155 		session_select(dst_s, dst_idx);
156 		server_redraw_session(dst_s);
157 	} else
158 		server_status_session(dst_s);
159 
160 	notify_window_layout_changed(dst_w);
161 	return (CMD_RETURN_NORMAL);
162 }
163