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