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