xref: /netbsd-src/external/bsd/tmux/dist/cmd-select-layout.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /* $OpenBSD$ */
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 = { "nopt:", 0, 1 },
37 	.usage = "[-nop] " CMD_TARGET_WINDOW_USAGE " [layout-name]",
38 
39 	.target = { 't', CMD_FIND_WINDOW, 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 },
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 },
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
72 cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item)
73 {
74 	struct args	*args = self->args;
75 	struct winlink	*wl = item->target.wl;
76 	struct window	*w;
77 	const char	*layoutname;
78 	char		*oldlayout;
79 	int		 next, previous, layout;
80 
81 	w = wl->window;
82 	server_unzoom_window(w);
83 
84 	next = self->entry == &cmd_next_layout_entry;
85 	if (args_has(args, 'n'))
86 		next = 1;
87 	previous = self->entry == &cmd_previous_layout_entry;
88 	if (args_has(args, 'p'))
89 		previous = 1;
90 
91 	oldlayout = w->old_layout;
92 	w->old_layout = layout_dump(w->layout_root);
93 
94 	if (next || previous) {
95 		if (next)
96 			layout_set_next(w);
97 		else
98 			layout_set_previous(w);
99 		goto changed;
100 	}
101 
102 	if (!args_has(args, 'o')) {
103 		if (args->argc == 0)
104 			layout = w->lastlayout;
105 		else
106 			layout = layout_set_lookup(args->argv[0]);
107 		if (layout != -1) {
108 			layout_set_select(w, layout);
109 			goto changed;
110 		}
111 	}
112 
113 	if (args->argc != 0)
114 		layoutname = args->argv[0];
115 	else if (args_has(args, 'o'))
116 		layoutname = oldlayout;
117 	else
118 		layoutname = NULL;
119 
120 	if (layoutname != NULL) {
121 		if (layout_parse(w, layoutname) == -1) {
122 			cmdq_error(item, "can't set layout: %s", layoutname);
123 			goto error;
124 		}
125 		goto changed;
126 	}
127 
128 	free(oldlayout);
129 	return (CMD_RETURN_NORMAL);
130 
131 changed:
132 	free(oldlayout);
133 	server_redraw_window(w);
134 	return (CMD_RETURN_NORMAL);
135 
136 error:
137 	free(w->old_layout);
138 	w->old_layout = oldlayout;
139 	return (CMD_RETURN_ERROR);
140 }
141