xref: /openbsd/usr.bin/tmux/cmd-select-layout.c (revision 2a71bce1)
1 /* $OpenBSD: cmd-select-layout.c,v 1.41 2022/05/30 12:52:02 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  * Switch window to selected layout.
27  */
28 
29 static enum cmd_retval	cmd_select_layout_exec(struct cmd *,
30 			    struct cmdq_item *);
31 
32 const struct cmd_entry cmd_select_layout_entry = {
33 	.name = "select-layout",
34 	.alias = "selectl",
35 
36 	.args = { "Enopt:", 0, 1, NULL },
37 	.usage = "[-Enop] " CMD_TARGET_PANE_USAGE " [layout-name]",
38 
39 	.target = { 't', CMD_FIND_PANE, 0 },
40 
41 	.flags = CMD_AFTERHOOK,
42 	.exec = cmd_select_layout_exec
43 };
44 
45 const struct cmd_entry cmd_next_layout_entry = {
46 	.name = "next-layout",
47 	.alias = "nextl",
48 
49 	.args = { "t:", 0, 0, NULL },
50 	.usage = CMD_TARGET_WINDOW_USAGE,
51 
52 	.target = { 't', CMD_FIND_WINDOW, 0 },
53 
54 	.flags = CMD_AFTERHOOK,
55 	.exec = cmd_select_layout_exec
56 };
57 
58 const struct cmd_entry cmd_previous_layout_entry = {
59 	.name = "previous-layout",
60 	.alias = "prevl",
61 
62 	.args = { "t:", 0, 0, NULL },
63 	.usage = CMD_TARGET_WINDOW_USAGE,
64 
65 	.target = { 't', CMD_FIND_WINDOW, 0 },
66 
67 	.flags = CMD_AFTERHOOK,
68 	.exec = cmd_select_layout_exec
69 };
70 
71 static enum cmd_retval
cmd_select_layout_exec(struct cmd * self,struct cmdq_item * item)72 cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item)
73 {
74 	struct args		*args = cmd_get_args(self);
75 	struct cmd_find_state	*target = cmdq_get_target(item);
76 	struct winlink		*wl = target->wl;
77 	struct window		*w = wl->window;
78 	struct window_pane	*wp = target->wp;
79 	const char		*layoutname;
80 	char			*oldlayout, *cause;
81 	int			 next, previous, layout;
82 
83 	server_unzoom_window(w);
84 
85 	next = (cmd_get_entry(self) == &cmd_next_layout_entry);
86 	if (args_has(args, 'n'))
87 		next = 1;
88 	previous = (cmd_get_entry(self) == &cmd_previous_layout_entry);
89 	if (args_has(args, 'p'))
90 		previous = 1;
91 
92 	oldlayout = w->old_layout;
93 	w->old_layout = layout_dump(w->layout_root);
94 
95 	if (next || previous) {
96 		if (next)
97 			layout_set_next(w);
98 		else
99 			layout_set_previous(w);
100 		goto changed;
101 	}
102 
103 	if (args_has(args, 'E')) {
104 		layout_spread_out(wp);
105 		goto changed;
106 	}
107 
108 	if (args_count(args) != 0)
109 		layoutname = args_string(args, 0);
110 	else if (args_has(args, 'o'))
111 		layoutname = oldlayout;
112 	else
113 		layoutname = NULL;
114 
115 	if (!args_has(args, 'o')) {
116 		if (layoutname == NULL)
117 			layout = w->lastlayout;
118 		else
119 			layout = layout_set_lookup(layoutname);
120 		if (layout != -1) {
121 			layout_set_select(w, layout);
122 			goto changed;
123 		}
124 	}
125 
126 	if (layoutname != NULL) {
127 		if (layout_parse(w, layoutname, &cause) == -1) {
128 			cmdq_error(item, "%s: %s", cause, layoutname);
129 			free(cause);
130 			goto error;
131 		}
132 		goto changed;
133 	}
134 
135 	free(oldlayout);
136 	return (CMD_RETURN_NORMAL);
137 
138 changed:
139 	free(oldlayout);
140 	recalculate_sizes();
141 	server_redraw_window(w);
142 	notify_window("window-layout-changed", w);
143 	return (CMD_RETURN_NORMAL);
144 
145 error:
146 	free(w->old_layout);
147 	w->old_layout = oldlayout;
148 	return (CMD_RETURN_ERROR);
149 }
150