Merge branch 'obsd-master'
[tmux.git] / cmd-select-layout.c
blob6dfe2b6a80819b08f1ec95c10ffc4a1b851820b9
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
23 #include "tmux.h"
26 * Switch window to selected layout.
29 static enum cmd_retval cmd_select_layout_exec(struct cmd *,
30 struct cmdq_item *);
32 const struct cmd_entry cmd_select_layout_entry = {
33 .name = "select-layout",
34 .alias = "selectl",
36 .args = { "Enopt:", 0, 1, NULL },
37 .usage = "[-Enop] " CMD_TARGET_PANE_USAGE " [layout-name]",
39 .target = { 't', CMD_FIND_PANE, 0 },
41 .flags = CMD_AFTERHOOK,
42 .exec = cmd_select_layout_exec
45 const struct cmd_entry cmd_next_layout_entry = {
46 .name = "next-layout",
47 .alias = "nextl",
49 .args = { "t:", 0, 0, NULL },
50 .usage = CMD_TARGET_WINDOW_USAGE,
52 .target = { 't', CMD_FIND_WINDOW, 0 },
54 .flags = CMD_AFTERHOOK,
55 .exec = cmd_select_layout_exec
58 const struct cmd_entry cmd_previous_layout_entry = {
59 .name = "previous-layout",
60 .alias = "prevl",
62 .args = { "t:", 0, 0, NULL },
63 .usage = CMD_TARGET_WINDOW_USAGE,
65 .target = { 't', CMD_FIND_WINDOW, 0 },
67 .flags = CMD_AFTERHOOK,
68 .exec = cmd_select_layout_exec
71 static enum cmd_retval
72 cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item)
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;
83 server_unzoom_window(w);
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;
92 oldlayout = w->old_layout;
93 w->old_layout = layout_dump(w->layout_root);
95 if (next || previous) {
96 if (next)
97 layout_set_next(w);
98 else
99 layout_set_previous(w);
100 goto changed;
103 if (args_has(args, 'E')) {
104 layout_spread_out(wp);
105 goto changed;
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;
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;
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;
132 goto changed;
135 free(oldlayout);
136 return (CMD_RETURN_NORMAL);
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);
145 error:
146 free(w->old_layout);
147 w->old_layout = oldlayout;
148 return (CMD_RETURN_ERROR);