xref: /minix/external/bsd/tmux/dist/cmd-join-pane.c (revision 9f988b79)
1 /* $Id: cmd-join-pane.c,v 1.1.1.2 2011/08/17 18:40:04 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <unistd.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Join a pane into another (like split/swap/kill).
28  */
29 
30 void	cmd_join_pane_key_binding(struct cmd *, int);
31 int	cmd_join_pane_exec(struct cmd *, struct cmd_ctx *);
32 
33 const struct cmd_entry cmd_join_pane_entry = {
34 	"join-pane", "joinp",
35 	"dhvp:l:s:t:", 0, 0,
36 	"[-dhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
37 	0,
38 	cmd_join_pane_key_binding,
39 	NULL,
40 	cmd_join_pane_exec
41 };
42 
43 void
44 cmd_join_pane_key_binding(struct cmd *self, int key)
45 {
46 	switch (key) {
47 	case '%':
48 		self->args = args_create(0);
49 		args_set(self->args, 'h', NULL);
50 		break;
51 	default:
52 		self->args = args_create(0);
53 		break;
54 	}
55 }
56 
57 int
58 cmd_join_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
59 {
60 	struct args		*args = self->args;
61 	struct session		*dst_s;
62 	struct winlink		*src_wl, *dst_wl;
63 	struct window		*src_w, *dst_w;
64 	struct window_pane	*src_wp, *dst_wp;
65 	char			*cause;
66 	int			 size, percentage, dst_idx;
67 	enum layout_type	 type;
68 	struct layout_cell	*lc;
69 
70 	dst_wl = cmd_find_pane(ctx, args_get(args, 't'), &dst_s, &dst_wp);
71 	if (dst_wl == NULL)
72 		return (-1);
73 	dst_w = dst_wl->window;
74 	dst_idx = dst_wl->idx;
75 
76 	src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp);
77 	if (src_wl == NULL)
78 		return (-1);
79 	src_w = src_wl->window;
80 
81 	if (src_w == dst_w) {
82 		ctx->error(ctx, "can't join a pane to its own window");
83 		return (-1);
84 	}
85 
86 	type = LAYOUT_TOPBOTTOM;
87 	if (args_has(args, 'h'))
88 		type = LAYOUT_LEFTRIGHT;
89 
90 	size = -1;
91 	if (args_has(args, 'l')) {
92 		size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
93 		if (cause != NULL) {
94 			ctx->error(ctx, "size %s", cause);
95 			xfree(cause);
96 			return (-1);
97 		}
98 	} else if (args_has(args, 'p')) {
99 		percentage = args_strtonum(args, 'p', 0, 100, &cause);
100 		if (cause != NULL) {
101 			ctx->error(ctx, "percentage %s", cause);
102 			xfree(cause);
103 			return (-1);
104 		}
105 		if (type == LAYOUT_TOPBOTTOM)
106 			size = (dst_wp->sy * percentage) / 100;
107 		else
108 			size = (dst_wp->sx * percentage) / 100;
109 	}
110 
111 	if ((lc = layout_split_pane(dst_wp, type, size)) == NULL) {
112 		ctx->error(ctx, "create pane failed: pane too small");
113 		return (-1);
114 	}
115 
116 	layout_close_pane(src_wp);
117 
118 	if (src_w->active == src_wp) {
119 		src_w->active = TAILQ_PREV(src_wp, window_panes, entry);
120 		if (src_w->active == NULL)
121 			src_w->active = TAILQ_NEXT(src_wp, entry);
122 	}
123 	TAILQ_REMOVE(&src_w->panes, src_wp, entry);
124 
125 	if (window_count_panes(src_w) == 0)
126 		server_kill_window(src_w);
127 
128 	src_wp->window = dst_w;
129 	TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
130 	layout_assign_pane(lc, src_wp);
131 
132 	recalculate_sizes();
133 
134 	server_redraw_window(src_w);
135 	server_redraw_window(dst_w);
136 
137 	if (!args_has(args, 'd')) {
138 		window_set_active_pane(dst_w, src_wp);
139 		session_select(dst_s, dst_idx);
140 		server_redraw_session(dst_s);
141 	} else
142 		server_status_session(dst_s);
143 
144 	return (0);
145 }
146