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 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 .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
49 const 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
63 enum cmd_retval
cmd_join_pane_exec(struct cmd * self,struct cmd_q * cmdq)64 cmd_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
69 enum cmd_retval
join_pane(struct cmd * self,struct cmd_q * cmdq,int not_same_window)70 join_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'), 0);
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 src_wp->window = dst_w;
139 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
140 layout_assign_pane(lc, src_wp);
141
142 recalculate_sizes();
143
144 server_redraw_window(src_w);
145 server_redraw_window(dst_w);
146
147 if (!args_has(args, 'd')) {
148 window_set_active_pane(dst_w, src_wp);
149 session_select(dst_s, dst_idx);
150 server_redraw_session(dst_s);
151 } else
152 server_status_session(dst_s);
153
154 if (window_count_panes(src_w) == 0)
155 server_kill_window(src_w);
156 else
157 notify_window_layout_changed(src_w);
158 notify_window_layout_changed(dst_w);
159
160 return (CMD_RETURN_NORMAL);
161 }
162