/src/tmux/cmd-join-pane.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2011 George Nachman <tmux@georgester.com> |
5 | | * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> |
6 | | * |
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. |
10 | | * |
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. |
18 | | */ |
19 | | |
20 | | #include <sys/types.h> |
21 | | |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <unistd.h> |
25 | | |
26 | | #include "tmux.h" |
27 | | |
28 | | /* |
29 | | * Join or move a pane into another (like split/swap/kill). |
30 | | */ |
31 | | |
32 | | static enum cmd_retval cmd_join_pane_exec(struct cmd *, struct cmdq_item *); |
33 | | |
34 | | const struct cmd_entry cmd_join_pane_entry = { |
35 | | .name = "join-pane", |
36 | | .alias = "joinp", |
37 | | |
38 | | .args = { "bdfhvp:l:s:t:", 0, 0, NULL }, |
39 | | .usage = "[-bdfhv] [-l size] " CMD_SRCDST_PANE_USAGE, |
40 | | |
41 | | .source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED }, |
42 | | .target = { 't', CMD_FIND_PANE, 0 }, |
43 | | |
44 | | .flags = 0, |
45 | | .exec = cmd_join_pane_exec |
46 | | }; |
47 | | |
48 | | const struct cmd_entry cmd_move_pane_entry = { |
49 | | .name = "move-pane", |
50 | | .alias = "movep", |
51 | | |
52 | | .args = { "bdfhvp:l:s:t:", 0, 0, NULL }, |
53 | | .usage = "[-bdfhv] [-l size] " CMD_SRCDST_PANE_USAGE, |
54 | | |
55 | | .source = { 's', CMD_FIND_PANE, CMD_FIND_DEFAULT_MARKED }, |
56 | | .target = { 't', CMD_FIND_PANE, 0 }, |
57 | | |
58 | | .flags = 0, |
59 | | .exec = cmd_join_pane_exec |
60 | | }; |
61 | | |
62 | | static enum cmd_retval |
63 | | cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item) |
64 | 0 | { |
65 | 0 | struct args *args = cmd_get_args(self); |
66 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
67 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
68 | 0 | struct cmd_find_state *source = cmdq_get_source(item); |
69 | 0 | struct session *dst_s; |
70 | 0 | struct winlink *src_wl, *dst_wl; |
71 | 0 | struct window *src_w, *dst_w; |
72 | 0 | struct window_pane *src_wp, *dst_wp; |
73 | 0 | char *cause = NULL; |
74 | 0 | int size, dst_idx; |
75 | 0 | int flags; |
76 | 0 | enum layout_type type; |
77 | 0 | struct layout_cell *lc; |
78 | 0 | u_int curval = 0; |
79 | |
|
80 | 0 | dst_s = target->s; |
81 | 0 | dst_wl = target->wl; |
82 | 0 | dst_wp = target->wp; |
83 | 0 | dst_w = dst_wl->window; |
84 | 0 | dst_idx = dst_wl->idx; |
85 | 0 | server_unzoom_window(dst_w); |
86 | |
|
87 | 0 | src_wl = source->wl; |
88 | 0 | src_wp = source->wp; |
89 | 0 | src_w = src_wl->window; |
90 | 0 | server_unzoom_window(src_w); |
91 | |
|
92 | 0 | if (src_wp == dst_wp) { |
93 | 0 | cmdq_error(item, "source and target panes must be different"); |
94 | 0 | return (CMD_RETURN_ERROR); |
95 | 0 | } |
96 | | |
97 | 0 | type = LAYOUT_TOPBOTTOM; |
98 | 0 | if (args_has(args, 'h')) |
99 | 0 | type = LAYOUT_LEFTRIGHT; |
100 | | |
101 | | /* If the 'p' flag is dropped then this bit can be moved into 'l'. */ |
102 | 0 | if (args_has(args, 'l') || args_has(args, 'p')) { |
103 | 0 | if (args_has(args, 'f')) { |
104 | 0 | if (type == LAYOUT_TOPBOTTOM) |
105 | 0 | curval = dst_w->sy; |
106 | 0 | else |
107 | 0 | curval = dst_w->sx; |
108 | 0 | } else { |
109 | 0 | if (type == LAYOUT_TOPBOTTOM) |
110 | 0 | curval = dst_wp->sy; |
111 | 0 | else |
112 | 0 | curval = dst_wp->sx; |
113 | 0 | } |
114 | 0 | } |
115 | |
|
116 | 0 | size = -1; |
117 | 0 | if (args_has(args, 'l')) { |
118 | 0 | size = args_percentage_and_expand(args, 'l', 0, INT_MAX, curval, |
119 | 0 | item, &cause); |
120 | 0 | } else if (args_has(args, 'p')) { |
121 | 0 | size = args_strtonum_and_expand(args, 'l', 0, 100, item, |
122 | 0 | &cause); |
123 | 0 | if (cause == NULL) |
124 | 0 | size = curval * size / 100; |
125 | 0 | } |
126 | 0 | if (cause != NULL) { |
127 | 0 | cmdq_error(item, "size %s", cause); |
128 | 0 | free(cause); |
129 | 0 | return (CMD_RETURN_ERROR); |
130 | 0 | } |
131 | | |
132 | 0 | flags = 0; |
133 | 0 | if (args_has(args, 'b')) |
134 | 0 | flags |= SPAWN_BEFORE; |
135 | 0 | if (args_has(args, 'f')) |
136 | 0 | flags |= SPAWN_FULLSIZE; |
137 | |
|
138 | 0 | lc = layout_split_pane(dst_wp, type, size, flags); |
139 | 0 | if (lc == NULL) { |
140 | 0 | cmdq_error(item, "create pane failed: pane too small"); |
141 | 0 | return (CMD_RETURN_ERROR); |
142 | 0 | } |
143 | | |
144 | 0 | layout_close_pane(src_wp); |
145 | |
|
146 | 0 | server_client_remove_pane(src_wp); |
147 | 0 | window_lost_pane(src_w, src_wp); |
148 | 0 | TAILQ_REMOVE(&src_w->panes, src_wp, entry); |
149 | |
|
150 | 0 | src_wp->window = dst_w; |
151 | 0 | options_set_parent(src_wp->options, dst_w->options); |
152 | 0 | src_wp->flags |= (PANE_STYLECHANGED|PANE_THEMECHANGED); |
153 | 0 | if (flags & SPAWN_BEFORE) |
154 | 0 | TAILQ_INSERT_BEFORE(dst_wp, src_wp, entry); |
155 | 0 | else |
156 | 0 | TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); |
157 | 0 | layout_assign_pane(lc, src_wp, 0); |
158 | 0 | colour_palette_from_option(&src_wp->palette, src_wp->options); |
159 | |
|
160 | 0 | recalculate_sizes(); |
161 | |
|
162 | 0 | server_redraw_window(src_w); |
163 | 0 | server_redraw_window(dst_w); |
164 | |
|
165 | 0 | if (!args_has(args, 'd')) { |
166 | 0 | window_set_active_pane(dst_w, src_wp, 1); |
167 | 0 | session_select(dst_s, dst_idx); |
168 | 0 | cmd_find_from_session(current, dst_s, 0); |
169 | 0 | server_redraw_session(dst_s); |
170 | 0 | } else |
171 | 0 | server_status_session(dst_s); |
172 | |
|
173 | 0 | if (window_count_panes(src_w) == 0) |
174 | 0 | server_kill_window(src_w, 1); |
175 | 0 | else |
176 | 0 | notify_window("window-layout-changed", src_w); |
177 | 0 | notify_window("window-layout-changed", dst_w); |
178 | |
|
179 | 0 | return (CMD_RETURN_NORMAL); |
180 | 0 | } |