Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / tmux / dist / cmd-join-pane.c
blob0e8f031fc8a154e962b67b22493cc9699d98f1c3
1 /* Id */
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 <stdlib.h>
23 #include <unistd.h>
25 #include "tmux.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 = {
37 "join-pane", "joinp",
38 "bdhvp:l:s:t:", 0, 0,
39 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
41 cmd_join_pane_key_binding,
42 cmd_join_pane_exec
45 const struct cmd_entry cmd_move_pane_entry = {
46 "move-pane", "movep",
47 "bdhvp:l:s:t:", 0, 0,
48 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
50 NULL,
51 cmd_join_pane_exec
54 void
55 cmd_join_pane_key_binding(struct cmd *self, int key)
57 switch (key) {
58 case '%':
59 self->args = args_create(0);
60 args_set(self->args, 'h', NULL);
61 break;
62 default:
63 self->args = args_create(0);
64 break;
68 enum cmd_retval
69 cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq)
71 return (join_pane(self, cmdq, self->entry == &cmd_join_pane_entry));
74 enum cmd_retval
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;
82 char *cause;
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);
88 if (dst_wl == NULL)
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);
95 if (src_wl == NULL)
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;
113 size = -1;
114 if (args_has(args, 'l')) {
115 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
116 if (cause != NULL) {
117 cmdq_error(cmdq, "size %s", cause);
118 free(cause);
119 return (CMD_RETURN_ERROR);
121 } else if (args_has(args, 'p')) {
122 percentage = args_strtonum(args, 'p', 0, 100, &cause);
123 if (cause != NULL) {
124 cmdq_error(cmdq, "percentage %s", cause);
125 free(cause);
126 return (CMD_RETURN_ERROR);
128 if (type == LAYOUT_TOPBOTTOM)
129 size = (dst_wp->sy * percentage) / 100;
130 else
131 size = (dst_wp->sx * percentage) / 100;
133 lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b'));
134 if (lc == NULL) {
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);
150 else
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);
157 recalculate_sizes();
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);
166 } else
167 server_status_session(dst_s);
169 notify_window_layout_changed(dst_w);
170 return (CMD_RETURN_NORMAL);