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>
28 * Join a pane into another (like split/swap/kill).
31 int cmd_join_pane_parse(struct cmd
*, int, char **, char **);
32 int cmd_join_pane_exec(struct cmd
*, struct cmd_ctx
*);
33 void cmd_join_pane_free(struct cmd
*);
34 void cmd_join_pane_init(struct cmd
*, int);
35 size_t cmd_join_pane_print(struct cmd
*, char *, size_t);
37 struct cmd_join_pane_data
{
46 const struct cmd_entry cmd_join_pane_entry
= {
48 "[-dhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane] [command]",
58 cmd_join_pane_init(struct cmd
*self
, int key
)
60 struct cmd_join_pane_data
*data
;
62 self
->data
= data
= xmalloc(sizeof *data
);
65 data
->flag_detached
= 0;
66 data
->flag_horizontal
= 0;
67 data
->percentage
= -1;
72 data
->flag_horizontal
= 1;
75 data
->flag_horizontal
= 0;
81 cmd_join_pane_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
83 struct cmd_join_pane_data
*data
;
87 self
->entry
->init(self
, KEYC_NONE
);
90 while ((opt
= getopt(argc
, argv
, "dhl:p:s:t:v")) != -1) {
93 data
->flag_detached
= 1;
96 data
->flag_horizontal
= 1;
99 if (data
->src
== NULL
)
100 data
->src
= xstrdup(optarg
);
103 if (data
->dst
== NULL
)
104 data
->dst
= xstrdup(optarg
);
107 if (data
->percentage
!= -1 || data
->size
!= -1)
109 data
->size
= strtonum(optarg
, 1, INT_MAX
, &errstr
);
110 if (errstr
!= NULL
) {
111 xasprintf(cause
, "size %s", errstr
);
116 if (data
->size
!= -1 || data
->percentage
!= -1)
118 data
->percentage
= strtonum(optarg
, 1, 100, &errstr
);
119 if (errstr
!= NULL
) {
120 xasprintf(cause
, "percentage %s", errstr
);
125 data
->flag_horizontal
= 0;
139 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
142 self
->entry
->free(self
);
147 cmd_join_pane_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
149 struct cmd_join_pane_data
*data
= self
->data
;
150 struct session
*dst_s
;
151 struct winlink
*src_wl
, *dst_wl
;
152 struct window
*src_w
, *dst_w
;
153 struct window_pane
*src_wp
, *dst_wp
;
155 enum layout_type type
;
156 struct layout_cell
*lc
;
158 if ((dst_wl
= cmd_find_pane(ctx
, data
->dst
, &dst_s
, &dst_wp
)) == NULL
)
160 dst_w
= dst_wl
->window
;
161 dst_idx
= dst_wl
->idx
;
163 if ((src_wl
= cmd_find_pane(ctx
, data
->src
, NULL
, &src_wp
)) == NULL
)
165 src_w
= src_wl
->window
;
167 if (src_w
== dst_w
) {
168 ctx
->error(ctx
, "can't join a pane to its own window");
172 type
= LAYOUT_TOPBOTTOM
;
173 if (data
->flag_horizontal
)
174 type
= LAYOUT_LEFTRIGHT
;
177 if (data
->size
!= -1)
179 else if (data
->percentage
!= -1) {
180 if (type
== LAYOUT_TOPBOTTOM
)
181 size
= (dst_wp
->sy
* data
->percentage
) / 100;
183 size
= (dst_wp
->sx
* data
->percentage
) / 100;
186 if ((lc
= layout_split_pane(dst_wp
, type
, size
)) == NULL
) {
187 ctx
->error(ctx
, "create pane failed: pane too small");
191 layout_close_pane(src_wp
);
193 if (src_w
->active
== src_wp
) {
194 src_w
->active
= TAILQ_PREV(src_wp
, window_panes
, entry
);
195 if (src_w
->active
== NULL
)
196 src_w
->active
= TAILQ_NEXT(src_wp
, entry
);
198 TAILQ_REMOVE(&src_w
->panes
, src_wp
, entry
);
200 if (window_count_panes(src_w
) == 0)
201 server_kill_window(src_w
);
203 src_wp
->window
= dst_w
;
204 TAILQ_INSERT_AFTER(&dst_w
->panes
, dst_wp
, src_wp
, entry
);
205 layout_assign_pane(lc
, src_wp
);
209 server_redraw_window(src_w
);
210 server_redraw_window(dst_w
);
212 if (!data
->flag_detached
) {
213 window_set_active_pane(dst_w
, src_wp
);
214 session_select(dst_s
, dst_idx
);
215 server_redraw_session(dst_s
);
217 server_status_session(dst_s
);
223 cmd_join_pane_free(struct cmd
*self
)
225 struct cmd_join_pane_data
*data
= self
->data
;
227 if (data
->src
!= NULL
)
229 if (data
->dst
!= NULL
)
235 cmd_join_pane_print(struct cmd
*self
, char *buf
, size_t len
)
237 struct cmd_join_pane_data
*data
= self
->data
;
240 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
243 if (off
< len
&& data
->flag_detached
)
244 off
+= xsnprintf(buf
+ off
, len
- off
, " -d");
245 if (off
< len
&& data
->flag_horizontal
)
246 off
+= xsnprintf(buf
+ off
, len
- off
, " -h");
247 if (off
< len
&& data
->size
> 0)
248 off
+= xsnprintf(buf
+ off
, len
- off
, " -l %d", data
->size
);
249 if (off
< len
&& data
->percentage
> 0) {
251 buf
+ off
, len
- off
, " -p %d", data
->percentage
);
253 if (off
< len
&& data
->src
!= NULL
)
254 off
+= cmd_prarg(buf
+ off
, len
- off
, " -s ", data
->src
);
255 if (off
< len
&& data
->dst
!= NULL
)
256 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->dst
);