1/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2011 George Nachman <[email protected]>
5 * Copyright (c) 2009 Nicholas Marriott <[email protected]>
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 = { "bdhvp:l:s:t:", 0, 0 },
53 .usage = "[-bdhv] [-p percentage|-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 = self->args;
66 struct cmd_find_state *current = &item->shared->current;
67 struct session *dst_s;
68 struct winlink *src_wl, *dst_wl;
69 struct window *src_w, *dst_w;
70 struct window_pane *src_wp, *dst_wp;
71 char *cause, *copy;
72 const char *errstr, *p;
73 size_t plen;
74 int size, percentage, dst_idx, not_same_window;
75 int flags;
76 enum layout_type type;
77 struct layout_cell *lc;
78
79 if (self->entry == &cmd_join_pane_entry)
80 not_same_window = 1;
81 else
82 not_same_window = 0;
83
84 dst_s = item->target.s;
85 dst_wl = item->target.wl;
86 dst_wp = item->target.wp;
87 dst_w = dst_wl->window;
88 dst_idx = dst_wl->idx;
89 server_unzoom_window(dst_w);
90
91 src_wl = item->source.wl;
92 src_wp = item->source.wp;
93 src_w = src_wl->window;
94 server_unzoom_window(src_w);
95
96 if (not_same_window && src_w == dst_w) {
97 cmdq_error(item, "can't join a pane to its own window");
98 return (CMD_RETURN_ERROR);
99 }
100 if (!not_same_window && src_wp == dst_wp) {
101 cmdq_error(item, "source and target panes must be different");
102 return (CMD_RETURN_ERROR);
103 }
104
105 type = LAYOUT_TOPBOTTOM;
106 if (args_has(args, 'h'))
107 type = LAYOUT_LEFTRIGHT;
108
109 size = -1;
110 if ((p = args_get(args, 'l')) != NULL) {
111 plen = strlen(p);
112 if (p[plen - 1] == '%') {
113 copy = xstrdup(p);
114 copy[plen - 1] = '\0';
115 percentage = strtonum(copy, 0, INT_MAX, &errstr);
116 free(copy);
117 if (errstr != NULL) {
118 cmdq_error(item, "percentage %s", errstr);
119 return (CMD_RETURN_ERROR);
120 }
121 if (type == LAYOUT_TOPBOTTOM)
122 size = (dst_wp->sy * percentage) / 100;
123 else
124 size = (dst_wp->sx * percentage) / 100;
125 } else {
126 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
127 if (cause != NULL) {
128 cmdq_error(item, "size %s", cause);
129 free(cause);
130 return (CMD_RETURN_ERROR);
131 }
132 }
133 } else if (args_has(args, 'p')) {
134 percentage = args_strtonum(args, 'p', 0, 100, &cause);
135 if (cause != NULL) {
136 cmdq_error(item, "percentage %s", cause);
137 free(cause);
138 return (CMD_RETURN_ERROR);
139 }
140 if (type == LAYOUT_TOPBOTTOM)
141 size = (dst_wp->sy * percentage) / 100;
142 else
143 size = (dst_wp->sx * percentage) / 100;
144 }
145
146 flags = 0;
147 if (args_has(args, 'b'))
148 flags |= SPAWN_BEFORE;
149 if (args_has(args, 'f'))
150 flags |= SPAWN_FULLSIZE;
151
152 lc = layout_split_pane(dst_wp, type, size, flags);
153 if (lc == NULL) {
154 cmdq_error(item, "create pane failed: pane too small");
155 return (CMD_RETURN_ERROR);
156 }
157
158 layout_close_pane(src_wp);
159
160 window_lost_pane(src_w, src_wp);
161 TAILQ_REMOVE(&src_w->panes, src_wp, entry);
162
163 src_wp->window = dst_w;
164 options_set_parent(src_wp->options, dst_w->options);
165 src_wp->flags |= PANE_STYLECHANGED;
166 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
167 layout_assign_pane(lc, src_wp);
168
169 recalculate_sizes();
170
171 server_redraw_window(src_w);
172 server_redraw_window(dst_w);
173
174 if (!args_has(args, 'd')) {
175 window_set_active_pane(dst_w, src_wp, 1);
176 session_select(dst_s, dst_idx);
177 cmd_find_from_session(current, dst_s, 0);
178 server_redraw_session(dst_s);
179 } else
180 server_status_session(dst_s);
181
182 if (window_count_panes(src_w) == 0)
183 server_kill_window(src_w);
184 else
185 notify_window("window-layout-changed", src_w);
186 notify_window("window-layout-changed", dst_w);
187
188 return (CMD_RETURN_NORMAL);
189}
190