1 /* Id */ 2 3 /* 4 * Copyright (c) 2011 George Nachman <tmux@georgester.com> 5 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> 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 <unistd.h> 24 25 #include "tmux.h" 26 27 /* 28 * Join or move a pane into another (like split/swap/kill). 29 */ 30 31 void cmd_join_pane_key_binding(struct cmd *, int); 32 enum cmd_retval cmd_join_pane_exec(struct cmd *, struct cmd_q *); 33 34 enum cmd_retval join_pane(struct cmd *, struct cmd_q *, int); 35 36 const struct cmd_entry cmd_join_pane_entry = { 37 "join-pane", "joinp", 38 "bdhvp:l:s:t:", 0, 0, 39 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]", 40 0, 41 cmd_join_pane_key_binding, 42 cmd_join_pane_exec 43 }; 44 45 const struct cmd_entry cmd_move_pane_entry = { 46 "move-pane", "movep", 47 "bdhvp:l:s:t:", 0, 0, 48 "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]", 49 0, 50 NULL, 51 cmd_join_pane_exec 52 }; 53 54 void 55 cmd_join_pane_key_binding(struct cmd *self, int key) 56 { 57 switch (key) { 58 case '%': 59 self->args = args_create(0); 60 args_set(self->args, 'h', NULL); 61 break; 62 default: 63 self->args = args_create(0); 64 break; 65 } 66 } 67 68 enum cmd_retval 69 cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq) 70 { 71 return (join_pane(self, cmdq, self->entry == &cmd_join_pane_entry)); 72 } 73 74 enum cmd_retval 75 join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) 76 { 77 struct args *args = self->args; 78 struct session *dst_s; 79 struct winlink *src_wl, *dst_wl; 80 struct window *src_w, *dst_w; 81 struct window_pane *src_wp, *dst_wp; 82 char *cause; 83 int size, percentage, dst_idx; 84 enum layout_type type; 85 struct layout_cell *lc; 86 87 dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp); 88 if (dst_wl == NULL) 89 return (CMD_RETURN_ERROR); 90 dst_w = dst_wl->window; 91 dst_idx = dst_wl->idx; 92 server_unzoom_window(dst_w); 93 94 src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp); 95 if (src_wl == NULL) 96 return (CMD_RETURN_ERROR); 97 src_w = src_wl->window; 98 server_unzoom_window(src_w); 99 100 if (not_same_window && src_w == dst_w) { 101 cmdq_error(cmdq, "can't join a pane to its own window"); 102 return (CMD_RETURN_ERROR); 103 } 104 if (!not_same_window && src_wp == dst_wp) { 105 cmdq_error(cmdq, "source and target panes must be different"); 106 return (CMD_RETURN_ERROR); 107 } 108 109 type = LAYOUT_TOPBOTTOM; 110 if (args_has(args, 'h')) 111 type = LAYOUT_LEFTRIGHT; 112 113 size = -1; 114 if (args_has(args, 'l')) { 115 size = args_strtonum(args, 'l', 0, INT_MAX, &cause); 116 if (cause != NULL) { 117 cmdq_error(cmdq, "size %s", cause); 118 free(cause); 119 return (CMD_RETURN_ERROR); 120 } 121 } else if (args_has(args, 'p')) { 122 percentage = args_strtonum(args, 'p', 0, 100, &cause); 123 if (cause != NULL) { 124 cmdq_error(cmdq, "percentage %s", cause); 125 free(cause); 126 return (CMD_RETURN_ERROR); 127 } 128 if (type == LAYOUT_TOPBOTTOM) 129 size = (dst_wp->sy * percentage) / 100; 130 else 131 size = (dst_wp->sx * percentage) / 100; 132 } 133 lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b')); 134 if (lc == NULL) { 135 cmdq_error(cmdq, "create pane failed: pane too small"); 136 return (CMD_RETURN_ERROR); 137 } 138 139 layout_close_pane(src_wp); 140 141 if (src_w->active == src_wp) { 142 src_w->active = TAILQ_PREV(src_wp, window_panes, entry); 143 if (src_w->active == NULL) 144 src_w->active = TAILQ_NEXT(src_wp, entry); 145 } 146 TAILQ_REMOVE(&src_w->panes, src_wp, entry); 147 148 if (window_count_panes(src_w) == 0) 149 server_kill_window(src_w); 150 else 151 notify_window_layout_changed(src_w); 152 153 src_wp->window = dst_w; 154 TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); 155 layout_assign_pane(lc, src_wp); 156 157 recalculate_sizes(); 158 159 server_redraw_window(src_w); 160 server_redraw_window(dst_w); 161 162 if (!args_has(args, 'd')) { 163 window_set_active_pane(dst_w, src_wp); 164 session_select(dst_s, dst_idx); 165 server_redraw_session(dst_s); 166 } else 167 server_status_session(dst_s); 168 169 notify_window_layout_changed(dst_w); 170 return (CMD_RETURN_NORMAL); 171 } 172