xref: /openbsd-src/usr.bin/tmux/cmd-break-pane.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-break-pane.c,v 1.50 2019/11/28 09:45:15 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 
23 #include "tmux.h"
24 
25 /*
26  * Break pane off into a window.
27  */
28 
29 #define BREAK_PANE_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
30 
31 static enum cmd_retval	cmd_break_pane_exec(struct cmd *, struct cmdq_item *);
32 
33 const struct cmd_entry cmd_break_pane_entry = {
34 	.name = "break-pane",
35 	.alias = "breakp",
36 
37 	.args = { "dPF:n:s:t:", 0, 0 },
38 	.usage = "[-dP] [-F format] [-n window-name] [-s src-pane] "
39 		 "[-t dst-window]",
40 
41 	.source = { 's', CMD_FIND_PANE, 0 },
42 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },
43 
44 	.flags = 0,
45 	.exec = cmd_break_pane_exec
46 };
47 
48 static enum cmd_retval
49 cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
50 {
51 	struct args		*args = self->args;
52 	struct cmd_find_state	*current = &item->shared->current;
53 	struct client		*c = cmd_find_client(item, NULL, 1);
54 	struct winlink		*wl = item->source.wl;
55 	struct session		*src_s = item->source.s;
56 	struct session		*dst_s = item->target.s;
57 	struct window_pane	*wp = item->source.wp;
58 	struct window		*w = wl->window;
59 	char			*name, *cause;
60 	int			 idx = item->target.idx;
61 	const char		*template;
62 	char			*cp;
63 
64 	if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) {
65 		cmdq_error(item, "index %d already in use", idx);
66 		return (CMD_RETURN_ERROR);
67 	}
68 
69 	if (window_count_panes(w) == 1) {
70 		cmdq_error(item, "can't break with only one pane");
71 		return (CMD_RETURN_ERROR);
72 	}
73 	server_unzoom_window(w);
74 
75 	TAILQ_REMOVE(&w->panes, wp, entry);
76 	window_lost_pane(w, wp);
77 	layout_close_pane(wp);
78 
79 	w = wp->window = window_create(w->sx, w->sy, w->xpixel, w->ypixel);
80 	options_set_parent(wp->options, w->options);
81 	wp->flags |= PANE_STYLECHANGED;
82 	TAILQ_INSERT_HEAD(&w->panes, wp, entry);
83 	w->active = wp;
84 	w->latest = c;
85 
86 	if (!args_has(args, 'n')) {
87 		name = default_window_name(w);
88 		window_set_name(w, name);
89 		free(name);
90 	} else {
91 		window_set_name(w, args_get(args, 'n'));
92 		options_set_number(w->options, "automatic-rename", 0);
93 	}
94 
95 	layout_init(w, wp);
96 	wp->flags |= PANE_CHANGED;
97 
98 	if (idx == -1)
99 		idx = -1 - options_get_number(dst_s->options, "base-index");
100 	wl = session_attach(dst_s, w, idx, &cause); /* can't fail */
101 	if (!args_has(self->args, 'd')) {
102 		session_select(dst_s, wl->idx);
103 		cmd_find_from_session(current, dst_s, 0);
104 	}
105 
106 	server_redraw_session(src_s);
107 	if (src_s != dst_s)
108 		server_redraw_session(dst_s);
109 	server_status_session_group(src_s);
110 	if (src_s != dst_s)
111 		server_status_session_group(dst_s);
112 
113 	if (args_has(args, 'P')) {
114 		if ((template = args_get(args, 'F')) == NULL)
115 			template = BREAK_PANE_TEMPLATE;
116 		cp = format_single(item, template, c, dst_s, wl, wp);
117 		cmdq_print(item, "%s", cp);
118 		free(cp);
119 	}
120 	return (CMD_RETURN_NORMAL);
121 }
122