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