cmd-join-pane.c revision 1.1.1.11
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 <string.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
32static enum cmd_retval	cmd_join_pane_exec(struct cmd *, struct cmdq_item *);
33
34const struct cmd_entry cmd_join_pane_entry = {
35	.name = "join-pane",
36	.alias = "joinp",
37
38	.args = { "bdfhvp:l:s:t:", 0, 0 },
39	.usage = "[-bdfhv] [-l size] " CMD_SRCDST_PANE_USAGE,
40
41	.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
42	.target = { 't', CMD_FIND_PANE, 0 },
43
44	.flags = 0,
45	.exec = cmd_join_pane_exec
46};
47
48const struct cmd_entry cmd_move_pane_entry = {
49	.name = "move-pane",
50	.alias = "movep",
51
52	.args = { "bdfhvp:l:s:t:", 0, 0 },
53	.usage = "[-bdfhv] [-l size] " CMD_SRCDST_PANE_USAGE,
54
55	.source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED },
56	.target = { 't', CMD_FIND_PANE, 0 },
57
58	.flags = 0,
59	.exec = cmd_join_pane_exec
60};
61
62static enum cmd_retval
63cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
64{
65	struct args		*args = cmd_get_args(self);
66	struct cmd_find_state	*current = cmdq_get_current(item);
67	struct cmd_find_state	*target = cmdq_get_target(item);
68	struct cmd_find_state	*source = cmdq_get_source(item);
69	struct session		*dst_s;
70	struct winlink		*src_wl, *dst_wl;
71	struct window		*src_w, *dst_w;
72	struct window_pane	*src_wp, *dst_wp;
73	char			*cause = NULL;
74	int			 size, percentage, dst_idx;
75	int			 flags;
76	enum layout_type	 type;
77	struct layout_cell	*lc;
78
79	dst_s = target->s;
80	dst_wl = target->wl;
81	dst_wp = target->wp;
82	dst_w = dst_wl->window;
83	dst_idx = dst_wl->idx;
84	server_unzoom_window(dst_w);
85
86	src_wl = source->wl;
87	src_wp = source->wp;
88	src_w = src_wl->window;
89	server_unzoom_window(src_w);
90
91	if (src_wp == dst_wp) {
92		cmdq_error(item, "source and target panes must be different");
93		return (CMD_RETURN_ERROR);
94	}
95
96	type = LAYOUT_TOPBOTTOM;
97	if (args_has(args, 'h'))
98		type = LAYOUT_LEFTRIGHT;
99
100	size = -1;
101	if (args_has(args, 'l')) {
102		if (type == LAYOUT_TOPBOTTOM) {
103			size = args_percentage(args, 'l', 0, INT_MAX,
104			    dst_wp->sy, &cause);
105		} else {
106			size = args_percentage(args, 'l', 0, INT_MAX,
107			    dst_wp->sx, &cause);
108		}
109	} else if (args_has(args, 'p')) {
110		percentage = args_strtonum(args, 'p', 0, 100, &cause);
111		if (cause == NULL) {
112			if (type == LAYOUT_TOPBOTTOM)
113				size = (dst_wp->sy * percentage) / 100;
114			else
115				size = (dst_wp->sx * percentage) / 100;
116		}
117	}
118	if (cause != NULL) {
119		cmdq_error(item, "size %s", cause);
120		free(cause);
121		return (CMD_RETURN_ERROR);
122	}
123
124	flags = 0;
125	if (args_has(args, 'b'))
126		flags |= SPAWN_BEFORE;
127	if (args_has(args, 'f'))
128		flags |= SPAWN_FULLSIZE;
129
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	server_client_remove_pane(src_wp);
139	window_lost_pane(src_w, src_wp);
140	TAILQ_REMOVE(&src_w->panes, src_wp, entry);
141
142	src_wp->window = dst_w;
143	options_set_parent(src_wp->options, dst_w->options);
144	src_wp->flags |= PANE_STYLECHANGED;
145	if (flags & SPAWN_BEFORE)
146		TAILQ_INSERT_BEFORE(dst_wp, src_wp, entry);
147	else
148		TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
149	layout_assign_pane(lc, src_wp, 0);
150
151	recalculate_sizes();
152
153	server_redraw_window(src_w);
154	server_redraw_window(dst_w);
155
156	if (!args_has(args, 'd')) {
157		window_set_active_pane(dst_w, src_wp, 1);
158		session_select(dst_s, dst_idx);
159		cmd_find_from_session(current, dst_s, 0);
160		server_redraw_session(dst_s);
161	} else
162		server_status_session(dst_s);
163
164	if (window_count_panes(src_w) == 0)
165		server_kill_window(src_w, 1);
166	else
167		notify_window("window-layout-changed", src_w);
168	notify_window("window-layout-changed", dst_w);
169
170	return (CMD_RETURN_NORMAL);
171}
172