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