When only two panes in a window, only draw half the separating line as
[tmux-openbsd.git] / cmd-join-pane.c
blobcf17e7d9eae58eb7aa9f0d017678e1b506cc464d
1 /* $OpenBSD$ */
3 /*
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>
22 #include <paths.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include "tmux.h"
29 * Join or move a pane into another (like split/swap/kill).
32 void cmd_join_pane_key_binding(struct cmd *, int);
33 enum cmd_retval cmd_join_pane_exec(struct cmd *, struct cmd_q *);
35 enum cmd_retval join_pane(struct cmd *, struct cmd_q *, int);
37 const struct cmd_entry cmd_join_pane_entry = {
38 "join-pane", "joinp",
39 "bdhvp:l:s:t:", 0, 0,
40 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
42 cmd_join_pane_key_binding,
43 NULL,
44 cmd_join_pane_exec
47 const struct cmd_entry cmd_move_pane_entry = {
48 "move-pane", "movep",
49 "bdhvp:l:s:t:", 0, 0,
50 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
52 NULL,
53 NULL,
54 cmd_join_pane_exec
57 void
58 cmd_join_pane_key_binding(struct cmd *self, int key)
60 switch (key) {
61 case '%':
62 self->args = args_create(0);
63 args_set(self->args, 'h', NULL);
64 break;
65 default:
66 self->args = args_create(0);
67 break;
71 enum cmd_retval
72 cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq)
74 return (join_pane(self, cmdq, self->entry == &cmd_join_pane_entry));
77 enum cmd_retval
78 join_pane(struct cmd *self, struct cmd_q *cmdq, 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;
85 char *cause;
86 int size, percentage, dst_idx;
87 enum layout_type type;
88 struct layout_cell *lc;
90 dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp);
91 if (dst_wl == NULL)
92 return (CMD_RETURN_ERROR);
93 dst_w = dst_wl->window;
94 dst_idx = dst_wl->idx;
95 server_unzoom_window(dst_w);
97 src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp);
98 if (src_wl == NULL)
99 return (CMD_RETURN_ERROR);
100 src_w = src_wl->window;
101 server_unzoom_window(src_w);
103 if (not_same_window && src_w == dst_w) {
104 cmdq_error(cmdq, "can't join a pane to its own window");
105 return (CMD_RETURN_ERROR);
107 if (!not_same_window && src_wp == dst_wp) {
108 cmdq_error(cmdq, "source and target panes must be different");
109 return (CMD_RETURN_ERROR);
112 type = LAYOUT_TOPBOTTOM;
113 if (args_has(args, 'h'))
114 type = LAYOUT_LEFTRIGHT;
116 size = -1;
117 if (args_has(args, 'l')) {
118 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
119 if (cause != NULL) {
120 cmdq_error(cmdq, "size %s", cause);
121 free(cause);
122 return (CMD_RETURN_ERROR);
124 } else if (args_has(args, 'p')) {
125 percentage = args_strtonum(args, 'p', 0, 100, &cause);
126 if (cause != NULL) {
127 cmdq_error(cmdq, "percentage %s", cause);
128 free(cause);
129 return (CMD_RETURN_ERROR);
131 if (type == LAYOUT_TOPBOTTOM)
132 size = (dst_wp->sy * percentage) / 100;
133 else
134 size = (dst_wp->sx * percentage) / 100;
136 lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b'));
137 if (lc == NULL) {
138 cmdq_error(cmdq, "create pane failed: pane too small");
139 return (CMD_RETURN_ERROR);
142 layout_close_pane(src_wp);
144 if (src_w->active == src_wp) {
145 src_w->active = TAILQ_PREV(src_wp, window_panes, entry);
146 if (src_w->active == NULL)
147 src_w->active = TAILQ_NEXT(src_wp, entry);
149 TAILQ_REMOVE(&src_w->panes, src_wp, entry);
151 if (window_count_panes(src_w) == 0)
152 server_kill_window(src_w);
153 else
154 notify_window_layout_changed(src_w);
156 src_wp->window = dst_w;
157 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
158 layout_assign_pane(lc, src_wp);
160 recalculate_sizes();
162 server_redraw_window(src_w);
163 server_redraw_window(dst_w);
165 if (!args_has(args, 'd')) {
166 window_set_active_pane(dst_w, src_wp);
167 session_select(dst_s, dst_idx);
168 server_redraw_session(dst_s);
169 } else
170 server_status_session(dst_s);
172 notify_window_layout_changed(dst_w);
173 return (CMD_RETURN_NORMAL);