1 |
|
|
/* $OpenBSD: cmd-select-layout.c,v 1.29 2016/01/19 15:59:12 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 |
|
|
enum cmd_retval cmd_select_layout_exec(struct cmd *, struct cmd_q *); |
30 |
|
|
|
31 |
|
|
const struct cmd_entry cmd_select_layout_entry = { |
32 |
|
|
.name = "select-layout", |
33 |
|
|
.alias = "selectl", |
34 |
|
|
|
35 |
|
|
.args = { "nopt:", 0, 1 }, |
36 |
|
|
.usage = "[-nop] " CMD_TARGET_WINDOW_USAGE " [layout-name]", |
37 |
|
|
|
38 |
|
|
.tflag = CMD_WINDOW, |
39 |
|
|
|
40 |
|
|
.flags = 0, |
41 |
|
|
.exec = cmd_select_layout_exec |
42 |
|
|
}; |
43 |
|
|
|
44 |
|
|
const struct cmd_entry cmd_next_layout_entry = { |
45 |
|
|
.name = "next-layout", |
46 |
|
|
.alias = "nextl", |
47 |
|
|
|
48 |
|
|
.args = { "t:", 0, 0 }, |
49 |
|
|
.usage = CMD_TARGET_WINDOW_USAGE, |
50 |
|
|
|
51 |
|
|
.tflag = CMD_WINDOW, |
52 |
|
|
|
53 |
|
|
.flags = 0, |
54 |
|
|
.exec = cmd_select_layout_exec |
55 |
|
|
}; |
56 |
|
|
|
57 |
|
|
const struct cmd_entry cmd_previous_layout_entry = { |
58 |
|
|
.name = "previous-layout", |
59 |
|
|
.alias = "prevl", |
60 |
|
|
|
61 |
|
|
.args = { "t:", 0, 0 }, |
62 |
|
|
.usage = CMD_TARGET_WINDOW_USAGE, |
63 |
|
|
|
64 |
|
|
.tflag = CMD_WINDOW, |
65 |
|
|
|
66 |
|
|
.flags = 0, |
67 |
|
|
.exec = cmd_select_layout_exec |
68 |
|
|
}; |
69 |
|
|
|
70 |
|
|
enum cmd_retval |
71 |
|
|
cmd_select_layout_exec(struct cmd *self, struct cmd_q *cmdq) |
72 |
|
|
{ |
73 |
|
|
struct args *args = self->args; |
74 |
|
|
struct winlink *wl = cmdq->state.tflag.wl; |
75 |
|
|
struct window *w; |
76 |
|
|
const char *layoutname; |
77 |
|
|
char *oldlayout; |
78 |
|
|
int next, previous, layout; |
79 |
|
|
|
80 |
|
|
w = wl->window; |
81 |
|
|
server_unzoom_window(w); |
82 |
|
|
|
83 |
|
|
next = self->entry == &cmd_next_layout_entry; |
84 |
|
|
if (args_has(args, 'n')) |
85 |
|
|
next = 1; |
86 |
|
|
previous = self->entry == &cmd_previous_layout_entry; |
87 |
|
|
if (args_has(args, 'p')) |
88 |
|
|
previous = 1; |
89 |
|
|
|
90 |
|
|
oldlayout = w->old_layout; |
91 |
|
|
w->old_layout = layout_dump(w->layout_root); |
92 |
|
|
|
93 |
|
|
if (next || previous) { |
94 |
|
|
if (next) |
95 |
|
|
layout_set_next(w); |
96 |
|
|
else |
97 |
|
|
layout_set_previous(w); |
98 |
|
|
goto changed; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
if (!args_has(args, 'o')) { |
102 |
|
|
if (args->argc == 0) |
103 |
|
|
layout = w->lastlayout; |
104 |
|
|
else |
105 |
|
|
layout = layout_set_lookup(args->argv[0]); |
106 |
|
|
if (layout != -1) { |
107 |
|
|
layout_set_select(w, layout); |
108 |
|
|
goto changed; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
if (args->argc != 0) |
113 |
|
|
layoutname = args->argv[0]; |
114 |
|
|
else if (args_has(args, 'o')) |
115 |
|
|
layoutname = oldlayout; |
116 |
|
|
else |
117 |
|
|
layoutname = NULL; |
118 |
|
|
|
119 |
|
|
if (layoutname != NULL) { |
120 |
|
|
if (layout_parse(w, layoutname) == -1) { |
121 |
|
|
cmdq_error(cmdq, "can't set layout: %s", layoutname); |
122 |
|
|
goto error; |
123 |
|
|
} |
124 |
|
|
goto changed; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
free(oldlayout); |
128 |
|
|
return (CMD_RETURN_NORMAL); |
129 |
|
|
|
130 |
|
|
changed: |
131 |
|
|
free(oldlayout); |
132 |
|
|
server_redraw_window(w); |
133 |
|
|
return (CMD_RETURN_NORMAL); |
134 |
|
|
|
135 |
|
|
error: |
136 |
|
|
free(w->old_layout); |
137 |
|
|
w->old_layout = oldlayout; |
138 |
|
|
return (CMD_RETURN_ERROR); |
139 |
|
|
} |