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