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