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