4 * Copyright (c) 2011 George Nachman <tmux@georgester.com>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
20 #include <sys/types.h>
29 * Join or move a pane into another (like split/swap/kill).
32 void cmd_join_pane_key_binding(struct cmd
*, int);
33 int cmd_join_pane_exec(struct cmd
*, struct cmd_ctx
*);
35 int join_pane(struct cmd
*, struct cmd_ctx
*, int);
37 const struct cmd_entry cmd_join_pane_entry
= {
40 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
42 cmd_join_pane_key_binding
,
47 const struct cmd_entry cmd_move_pane_entry
= {
50 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
58 cmd_join_pane_key_binding(struct cmd
*self
, int key
)
62 self
->args
= args_create(0);
63 args_set(self
->args
, 'h', NULL
);
66 self
->args
= args_create(0);
72 cmd_join_pane_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
74 return (join_pane(self
, ctx
, self
->entry
== &cmd_join_pane_entry
));
78 join_pane(struct cmd
*self
, struct cmd_ctx
*ctx
, int not_same_window
)
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
;
86 int size
, percentage
, dst_idx
;
87 enum layout_type type
;
88 struct layout_cell
*lc
;
90 dst_wl
= cmd_find_pane(ctx
, args_get(args
, 't'), &dst_s
, &dst_wp
);
93 dst_w
= dst_wl
->window
;
94 dst_idx
= dst_wl
->idx
;
96 src_wl
= cmd_find_pane(ctx
, args_get(args
, 's'), NULL
, &src_wp
);
99 src_w
= src_wl
->window
;
101 if (not_same_window
&& src_w
== dst_w
) {
102 ctx
->error(ctx
, "can't join a pane to its own window");
105 if (!not_same_window
&& src_wp
== dst_wp
) {
106 ctx
->error(ctx
, "source and target panes must be different");
110 type
= LAYOUT_TOPBOTTOM
;
111 if (args_has(args
, 'h'))
112 type
= LAYOUT_LEFTRIGHT
;
115 if (args_has(args
, 'l')) {
116 size
= args_strtonum(args
, 'l', 0, INT_MAX
, &cause
);
118 ctx
->error(ctx
, "size %s", cause
);
122 } else if (args_has(args
, 'p')) {
123 percentage
= args_strtonum(args
, 'p', 0, 100, &cause
);
125 ctx
->error(ctx
, "percentage %s", cause
);
129 if (type
== LAYOUT_TOPBOTTOM
)
130 size
= (dst_wp
->sy
* percentage
) / 100;
132 size
= (dst_wp
->sx
* percentage
) / 100;
134 lc
= layout_split_pane(dst_wp
, type
, size
, args_has(args
, 'b'));
136 ctx
->error(ctx
, "create pane failed: pane too small");
140 layout_close_pane(src_wp
);
142 if (src_w
->active
== src_wp
) {
143 src_w
->active
= TAILQ_PREV(src_wp
, window_panes
, entry
);
144 if (src_w
->active
== NULL
)
145 src_w
->active
= TAILQ_NEXT(src_wp
, entry
);
147 TAILQ_REMOVE(&src_w
->panes
, src_wp
, entry
);
149 if (window_count_panes(src_w
) == 0)
150 server_kill_window(src_w
);
152 notify_window_layout_changed(src_w
);
154 src_wp
->window
= dst_w
;
155 TAILQ_INSERT_AFTER(&dst_w
->panes
, dst_wp
, src_wp
, entry
);
156 layout_assign_pane(lc
, src_wp
);
160 server_redraw_window(src_w
);
161 server_redraw_window(dst_w
);
163 if (!args_has(args
, 'd')) {
164 window_set_active_pane(dst_w
, src_wp
);
165 session_select(dst_s
, dst_idx
);
166 server_redraw_session(dst_s
);
168 server_status_session(dst_s
);
170 notify_window_layout_changed(dst_w
);