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>
28 * Join or move a pane into another (like split/swap/kill).
31 void cmd_join_pane_key_binding(struct cmd
*, int);
32 enum cmd_retval
cmd_join_pane_exec(struct cmd
*, struct cmd_q
*);
34 enum cmd_retval
join_pane(struct cmd
*, struct cmd_q
*, int);
36 const struct cmd_entry cmd_join_pane_entry
= {
39 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
41 cmd_join_pane_key_binding
,
45 const struct cmd_entry cmd_move_pane_entry
= {
48 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
55 cmd_join_pane_key_binding(struct cmd
*self
, int key
)
59 self
->args
= args_create(0);
60 args_set(self
->args
, 'h', NULL
);
63 self
->args
= args_create(0);
69 cmd_join_pane_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
71 return (join_pane(self
, cmdq
, self
->entry
== &cmd_join_pane_entry
));
75 join_pane(struct cmd
*self
, struct cmd_q
*cmdq
, int not_same_window
)
77 struct args
*args
= self
->args
;
78 struct session
*dst_s
;
79 struct winlink
*src_wl
, *dst_wl
;
80 struct window
*src_w
, *dst_w
;
81 struct window_pane
*src_wp
, *dst_wp
;
83 int size
, percentage
, dst_idx
;
84 enum layout_type type
;
85 struct layout_cell
*lc
;
87 dst_wl
= cmd_find_pane(cmdq
, args_get(args
, 't'), &dst_s
, &dst_wp
);
89 return (CMD_RETURN_ERROR
);
90 dst_w
= dst_wl
->window
;
91 dst_idx
= dst_wl
->idx
;
92 server_unzoom_window(dst_w
);
94 src_wl
= cmd_find_pane(cmdq
, args_get(args
, 's'), NULL
, &src_wp
);
96 return (CMD_RETURN_ERROR
);
97 src_w
= src_wl
->window
;
98 server_unzoom_window(src_w
);
100 if (not_same_window
&& src_w
== dst_w
) {
101 cmdq_error(cmdq
, "can't join a pane to its own window");
102 return (CMD_RETURN_ERROR
);
104 if (!not_same_window
&& src_wp
== dst_wp
) {
105 cmdq_error(cmdq
, "source and target panes must be different");
106 return (CMD_RETURN_ERROR
);
109 type
= LAYOUT_TOPBOTTOM
;
110 if (args_has(args
, 'h'))
111 type
= LAYOUT_LEFTRIGHT
;
114 if (args_has(args
, 'l')) {
115 size
= args_strtonum(args
, 'l', 0, INT_MAX
, &cause
);
117 cmdq_error(cmdq
, "size %s", cause
);
119 return (CMD_RETURN_ERROR
);
121 } else if (args_has(args
, 'p')) {
122 percentage
= args_strtonum(args
, 'p', 0, 100, &cause
);
124 cmdq_error(cmdq
, "percentage %s", cause
);
126 return (CMD_RETURN_ERROR
);
128 if (type
== LAYOUT_TOPBOTTOM
)
129 size
= (dst_wp
->sy
* percentage
) / 100;
131 size
= (dst_wp
->sx
* percentage
) / 100;
133 lc
= layout_split_pane(dst_wp
, type
, size
, args_has(args
, 'b'));
135 cmdq_error(cmdq
, "create pane failed: pane too small");
136 return (CMD_RETURN_ERROR
);
139 layout_close_pane(src_wp
);
141 if (src_w
->active
== src_wp
) {
142 src_w
->active
= TAILQ_PREV(src_wp
, window_panes
, entry
);
143 if (src_w
->active
== NULL
)
144 src_w
->active
= TAILQ_NEXT(src_wp
, entry
);
146 TAILQ_REMOVE(&src_w
->panes
, src_wp
, entry
);
148 if (window_count_panes(src_w
) == 0)
149 server_kill_window(src_w
);
151 notify_window_layout_changed(src_w
);
153 src_wp
->window
= dst_w
;
154 TAILQ_INSERT_AFTER(&dst_w
->panes
, dst_wp
, src_wp
, entry
);
155 layout_assign_pane(lc
, src_wp
);
159 server_redraw_window(src_w
);
160 server_redraw_window(dst_w
);
162 if (!args_has(args
, 'd')) {
163 window_set_active_pane(dst_w
, src_wp
);
164 session_select(dst_s
, dst_idx
);
165 server_redraw_session(dst_s
);
167 server_status_session(dst_s
);
169 notify_window_layout_changed(dst_w
);
170 return (CMD_RETURN_NORMAL
);