Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / cmd-join-pane.c
blob0bfefd2f9035de2d2228a40fa7be1ec54f6f9fbe
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <paths.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "tmux.h"
28 * Join a pane into another (like split/swap/kill).
31 void cmd_join_pane_key_binding(struct cmd *, int);
32 int cmd_join_pane_exec(struct cmd *, struct cmd_ctx *);
34 const struct cmd_entry cmd_join_pane_entry = {
35 "join-pane", "joinp",
36 "dhvp:l:s:t:", 0, 0,
37 "[-dhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]",
39 cmd_join_pane_key_binding,
40 NULL,
41 cmd_join_pane_exec
44 void
45 cmd_join_pane_key_binding(struct cmd *self, int key)
47 switch (key) {
48 case '%':
49 self->args = args_create(0);
50 args_set(self->args, 'h', NULL);
51 break;
52 default:
53 self->args = args_create(0);
54 break;
58 int
59 cmd_join_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
61 struct args *args = self->args;
62 struct session *dst_s;
63 struct winlink *src_wl, *dst_wl;
64 struct window *src_w, *dst_w;
65 struct window_pane *src_wp, *dst_wp;
66 char *cause;
67 int size, percentage, dst_idx;
68 enum layout_type type;
69 struct layout_cell *lc;
71 dst_wl = cmd_find_pane(ctx, args_get(args, 't'), &dst_s, &dst_wp);
72 if (dst_wl == NULL)
73 return (-1);
74 dst_w = dst_wl->window;
75 dst_idx = dst_wl->idx;
77 src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp);
78 if (src_wl == NULL)
79 return (-1);
80 src_w = src_wl->window;
82 if (src_w == dst_w) {
83 ctx->error(ctx, "can't join a pane to its own window");
84 return (-1);
87 type = LAYOUT_TOPBOTTOM;
88 if (args_has(args, 'h'))
89 type = LAYOUT_LEFTRIGHT;
91 size = -1;
92 if (args_has(args, 'l')) {
93 size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
94 if (cause != NULL) {
95 ctx->error(ctx, "size %s", cause);
96 xfree(cause);
97 return (-1);
99 } else if (args_has(args, 'p')) {
100 percentage = args_strtonum(args, 'p', 0, 100, &cause);
101 if (cause != NULL) {
102 ctx->error(ctx, "percentage %s", cause);
103 xfree(cause);
104 return (-1);
106 if (type == LAYOUT_TOPBOTTOM)
107 size = (dst_wp->sy * percentage) / 100;
108 else
109 size = (dst_wp->sx * percentage) / 100;
112 if ((lc = layout_split_pane(dst_wp, type, size)) == NULL) {
113 ctx->error(ctx, "create pane failed: pane too small");
114 return (-1);
117 layout_close_pane(src_wp);
119 if (src_w->active == src_wp) {
120 src_w->active = TAILQ_PREV(src_wp, window_panes, entry);
121 if (src_w->active == NULL)
122 src_w->active = TAILQ_NEXT(src_wp, entry);
124 TAILQ_REMOVE(&src_w->panes, src_wp, entry);
126 if (window_count_panes(src_w) == 0)
127 server_kill_window(src_w);
129 src_wp->window = dst_w;
130 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry);
131 layout_assign_pane(lc, src_wp);
133 recalculate_sizes();
135 server_redraw_window(src_w);
136 server_redraw_window(dst_w);
138 if (!args_has(args, 'd')) {
139 window_set_active_pane(dst_w, src_wp);
140 session_select(dst_s, dst_idx);
141 server_redraw_session(dst_s);
142 } else
143 server_status_session(dst_s);
145 return (0);