xref: /openbsd-src/usr.bin/tmux/cmd-join-pane.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /* $OpenBSD: cmd-join-pane.c,v 1.19 2015/06/18 23:56:01 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2011 George Nachman <tmux@georgester.com>
5  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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 	"join-pane", "joinp",
38 	"bdhvp:l:s:t:", 0, 0,
39 	"[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
40 	0,
41 	cmd_join_pane_exec
42 };
43 
44 const struct cmd_entry cmd_move_pane_entry = {
45 	"move-pane", "movep",
46 	"bdhvp:l:s:t:", 0, 0,
47 	"[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE,
48 	0,
49 	cmd_join_pane_exec
50 };
51 
52 enum cmd_retval
53 cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq)
54 {
55 	return (join_pane(self, cmdq, self->entry == &cmd_join_pane_entry));
56 }
57 
58 enum cmd_retval
59 join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window)
60 {
61 	struct args		*args = self->args;
62 	struct session		*dst_s;
63 	struct winlink		*src_wl, *dst_wl;
64 	struct window		*src_w, *dst_w;
65 	struct window_pane	*src_wp, *dst_wp;
66 	char			*cause;
67 	int			 size, percentage, dst_idx;
68 	enum layout_type	 type;
69 	struct layout_cell	*lc;
70 
71 	dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp);
72 	if (dst_wl == NULL)
73 		return (CMD_RETURN_ERROR);
74 	dst_w = dst_wl->window;
75 	dst_idx = dst_wl->idx;
76 	server_unzoom_window(dst_w);
77 
78 	src_wl = cmd_find_pane_marked(cmdq, args_get(args, 's'), NULL, &src_wp);
79 	if (src_wl == NULL)
80 		return (CMD_RETURN_ERROR);
81 	src_w = src_wl->window;
82 	server_unzoom_window(src_w);
83 
84 	if (not_same_window && src_w == dst_w) {
85 		cmdq_error(cmdq, "can't join a pane to its own window");
86 		return (CMD_RETURN_ERROR);
87 	}
88 	if (!not_same_window && src_wp == dst_wp) {
89 		cmdq_error(cmdq, "source and target panes must be different");
90 		return (CMD_RETURN_ERROR);
91 	}
92 
93 	type = LAYOUT_TOPBOTTOM;
94 	if (args_has(args, 'h'))
95 		type = LAYOUT_LEFTRIGHT;
96 
97 	size = -1;
98 	if (args_has(args, 'l')) {
99 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
100 		if (cause != NULL) {
101 			cmdq_error(cmdq, "size %s", cause);
102 			free(cause);
103 			return (CMD_RETURN_ERROR);
104 		}
105 	} else if (args_has(args, 'p')) {
106 		percentage = args_strtonum(args, 'p', 0, 100, &cause);
107 		if (cause != NULL) {
108 			cmdq_error(cmdq, "percentage %s", cause);
109 			free(cause);
110 			return (CMD_RETURN_ERROR);
111 		}
112 		if (type == LAYOUT_TOPBOTTOM)
113 			size = (dst_wp->sy * percentage) / 100;
114 		else
115 			size = (dst_wp->sx * percentage) / 100;
116 	}
117 	lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b'));
118 	if (lc == NULL) {
119 		cmdq_error(cmdq, "create pane failed: pane too small");
120 		return (CMD_RETURN_ERROR);
121 	}
122 
123 	layout_close_pane(src_wp);
124 
125 	window_lost_pane(src_w, src_wp);
126 	TAILQ_REMOVE(&src_w->panes, src_wp, entry);
127 
128 	if (window_count_panes(src_w) == 0)
129 		server_kill_window(src_w);
130 	else
131 		notify_window_layout_changed(src_w);
132 
133 	src_wp->window = dst_w;
134 	TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
135 	layout_assign_pane(lc, src_wp);
136 
137 	recalculate_sizes();
138 
139 	server_redraw_window(src_w);
140 	server_redraw_window(dst_w);
141 
142 	if (!args_has(args, 'd')) {
143 		window_set_active_pane(dst_w, src_wp);
144 		session_select(dst_s, dst_idx);
145 		server_redraw_session(dst_s);
146 	} else
147 		server_status_session(dst_s);
148 
149 	notify_window_layout_changed(dst_w);
150 	return (CMD_RETURN_NORMAL);
151 }
152