1 /* $Id: cmd-join-pane.c,v 1.1.1.1 2011/03/10 09:15:37 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 int cmd_join_pane_parse(struct cmd *, int, char **, char **); 31 int cmd_join_pane_exec(struct cmd *, struct cmd_ctx *); 32 void cmd_join_pane_free(struct cmd *); 33 void cmd_join_pane_init(struct cmd *, int); 34 size_t cmd_join_pane_print(struct cmd *, char *, size_t); 35 36 struct cmd_join_pane_data { 37 char *src; 38 char *dst; 39 int flag_detached; 40 int flag_horizontal; 41 int percentage; 42 int size; 43 }; 44 45 const struct cmd_entry cmd_join_pane_entry = { 46 "join-pane", "joinp", 47 "[-dhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]", 48 0, "", 49 cmd_join_pane_init, 50 cmd_join_pane_parse, 51 cmd_join_pane_exec, 52 cmd_join_pane_free, 53 cmd_join_pane_print 54 }; 55 56 void 57 cmd_join_pane_init(struct cmd *self, int key) 58 { 59 struct cmd_join_pane_data *data; 60 61 self->data = data = xmalloc(sizeof *data); 62 data->src = NULL; 63 data->dst = NULL; 64 data->flag_detached = 0; 65 data->flag_horizontal = 0; 66 data->percentage = -1; 67 data->size = -1; 68 69 switch (key) { 70 case '%': 71 data->flag_horizontal = 1; 72 break; 73 case '"': 74 data->flag_horizontal = 0; 75 break; 76 } 77 } 78 79 int 80 cmd_join_pane_parse(struct cmd *self, int argc, char **argv, char **cause) 81 { 82 struct cmd_join_pane_data *data; 83 int opt; 84 const char *errstr; 85 86 self->entry->init(self, KEYC_NONE); 87 data = self->data; 88 89 while ((opt = getopt(argc, argv, "dhl:p:s:t:v")) != -1) { 90 switch (opt) { 91 case 'd': 92 data->flag_detached = 1; 93 break; 94 case 'h': 95 data->flag_horizontal = 1; 96 break; 97 case 's': 98 if (data->src == NULL) 99 data->src = xstrdup(optarg); 100 break; 101 case 't': 102 if (data->dst == NULL) 103 data->dst = xstrdup(optarg); 104 break; 105 case 'l': 106 if (data->percentage != -1 || data->size != -1) 107 break; 108 data->size = strtonum(optarg, 1, INT_MAX, &errstr); 109 if (errstr != NULL) { 110 xasprintf(cause, "size %s", errstr); 111 goto error; 112 } 113 break; 114 case 'p': 115 if (data->size != -1 || data->percentage != -1) 116 break; 117 data->percentage = strtonum(optarg, 1, 100, &errstr); 118 if (errstr != NULL) { 119 xasprintf(cause, "percentage %s", errstr); 120 goto error; 121 } 122 break; 123 case 'v': 124 data->flag_horizontal = 0; 125 break; 126 default: 127 goto usage; 128 } 129 } 130 argc -= optind; 131 argv += optind; 132 if (argc != 0) 133 goto usage; 134 135 return (0); 136 137 usage: 138 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); 139 140 error: 141 self->entry->free(self); 142 return (-1); 143 } 144 145 int 146 cmd_join_pane_exec(struct cmd *self, struct cmd_ctx *ctx) 147 { 148 struct cmd_join_pane_data *data = self->data; 149 struct session *dst_s; 150 struct winlink *src_wl, *dst_wl; 151 struct window *src_w, *dst_w; 152 struct window_pane *src_wp, *dst_wp; 153 int size, dst_idx; 154 enum layout_type type; 155 struct layout_cell *lc; 156 157 if ((dst_wl = cmd_find_pane(ctx, data->dst, &dst_s, &dst_wp)) == NULL) 158 return (-1); 159 dst_w = dst_wl->window; 160 dst_idx = dst_wl->idx; 161 162 if ((src_wl = cmd_find_pane(ctx, data->src, NULL, &src_wp)) == NULL) 163 return (-1); 164 src_w = src_wl->window; 165 166 if (src_w == dst_w) { 167 ctx->error(ctx, "can't join a pane to its own window"); 168 return (-1); 169 } 170 171 type = LAYOUT_TOPBOTTOM; 172 if (data->flag_horizontal) 173 type = LAYOUT_LEFTRIGHT; 174 175 size = -1; 176 if (data->size != -1) 177 size = data->size; 178 else if (data->percentage != -1) { 179 if (type == LAYOUT_TOPBOTTOM) 180 size = (dst_wp->sy * data->percentage) / 100; 181 else 182 size = (dst_wp->sx * data->percentage) / 100; 183 } 184 185 if ((lc = layout_split_pane(dst_wp, type, size)) == NULL) { 186 ctx->error(ctx, "create pane failed: pane too small"); 187 return (-1); 188 } 189 190 layout_close_pane(src_wp); 191 192 if (src_w->active == src_wp) { 193 src_w->active = TAILQ_PREV(src_wp, window_panes, entry); 194 if (src_w->active == NULL) 195 src_w->active = TAILQ_NEXT(src_wp, entry); 196 } 197 TAILQ_REMOVE(&src_w->panes, src_wp, entry); 198 199 if (window_count_panes(src_w) == 0) 200 server_kill_window(src_w); 201 202 src_wp->window = dst_w; 203 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); 204 layout_assign_pane(lc, src_wp); 205 206 recalculate_sizes(); 207 208 server_redraw_window(src_w); 209 server_redraw_window(dst_w); 210 211 if (!data->flag_detached) { 212 window_set_active_pane(dst_w, src_wp); 213 session_select(dst_s, dst_idx); 214 server_redraw_session(dst_s); 215 } else 216 server_status_session(dst_s); 217 218 return (0); 219 } 220 221 void 222 cmd_join_pane_free(struct cmd *self) 223 { 224 struct cmd_join_pane_data *data = self->data; 225 226 if (data->src != NULL) 227 xfree(data->src); 228 if (data->dst != NULL) 229 xfree(data->dst); 230 xfree(data); 231 } 232 233 size_t 234 cmd_join_pane_print(struct cmd *self, char *buf, size_t len) 235 { 236 struct cmd_join_pane_data *data = self->data; 237 size_t off = 0; 238 239 off += xsnprintf(buf, len, "%s", self->entry->name); 240 if (data == NULL) 241 return (off); 242 if (off < len && data->flag_detached) 243 off += xsnprintf(buf + off, len - off, " -d"); 244 if (off < len && data->flag_horizontal) 245 off += xsnprintf(buf + off, len - off, " -h"); 246 if (off < len && data->size > 0) 247 off += xsnprintf(buf + off, len - off, " -l %d", data->size); 248 if (off < len && data->percentage > 0) { 249 off += xsnprintf( 250 buf + off, len - off, " -p %d", data->percentage); 251 } 252 if (off < len && data->src != NULL) 253 off += cmd_prarg(buf + off, len - off, " -s ", data->src); 254 if (off < len && data->dst != NULL) 255 off += cmd_prarg(buf + off, len - off, " -t ", data->dst); 256 return (off); 257 } 258