1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::compat::queue::tailq_foreach;
use crate::*;
use crate::options_::*;
pub static CMD_SET_OPTION_ENTRY: cmd_entry = cmd_entry {
name: "set-option",
alias: Some("set"),
args: args_parse::new("aFgopqst:uUw", 1, 2, Some(cmd_set_option_args_parse)),
usage: "[-aFgopqsuUw] [-t target-pane] option [value]",
target: cmd_entry_flag::new(
b't',
cmd_find_type::CMD_FIND_PANE,
cmd_find_flags::CMD_FIND_CANFAIL,
),
flags: cmd_flag::CMD_AFTERHOOK,
exec: cmd_set_option_exec,
source: cmd_entry_flag::zeroed(),
};
pub static CMD_SET_WINDOW_OPTION_ENTRY: cmd_entry = cmd_entry {
name: "set-window-option",
alias: Some("setw"),
args: args_parse::new("aFgoqt:u", 1, 2, Some(cmd_set_option_args_parse)),
usage: "[-aFgoqu] [-t target-window] option [value]",
target: cmd_entry_flag::new(
b't',
cmd_find_type::CMD_FIND_WINDOW,
cmd_find_flags::CMD_FIND_CANFAIL,
),
flags: cmd_flag::CMD_AFTERHOOK,
exec: cmd_set_option_exec,
source: cmd_entry_flag::zeroed(),
};
pub static CMD_SET_HOOK_ENTRY: cmd_entry = cmd_entry {
name: "set-hook",
alias: None,
args: args_parse::new("agpRt:uw", 1, 2, Some(cmd_set_option_args_parse)),
usage: "[-agpRuw] [-t target-pane] hook [command]",
target: cmd_entry_flag::new(
b't',
cmd_find_type::CMD_FIND_PANE,
cmd_find_flags::CMD_FIND_CANFAIL,
),
flags: cmd_flag::CMD_AFTERHOOK,
exec: cmd_set_option_exec,
source: cmd_entry_flag::zeroed(),
};
/// C `vendor/tmux/cmd-set-option.c:75`: `static enum args_parse_type cmd_set_option_args_parse(__unused struct args *args, u_int idx, __unused char **cause)`
pub fn cmd_set_option_args_parse(
_args: *mut args,
idx: u32,
_cause: *mut *mut u8,
) -> args_parse_type {
match idx {
1 => args_parse_type::ARGS_PARSE_COMMANDS_OR_STRING,
_ => args_parse_type::ARGS_PARSE_STRING,
}
}
/// C `vendor/tmux/cmd-set-option.c:84`: `static enum cmd_retval cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)`
pub unsafe fn cmd_set_option_exec(self_: *mut cmd, item: *mut cmdq_item) -> cmd_retval {
unsafe {
let args = cmd_get_args(self_);
let append = args_has(args, 'a');
let target = cmdq_get_target(item);
let mut oo: *mut options = null_mut();
let parent: *mut options_entry;
let mut o: *mut options_entry;
let argument: *mut u8;
let mut expanded: *mut u8 = null_mut();
let mut value: *const u8;
let mut idx: i32 = 0;
let already: i32;
let mut ambiguous: i32 = 0;
let scope: i32;
'fail: {
'out: {
let window =
std::ptr::eq(cmd_get_entry(self_), &CMD_SET_WINDOW_OPTION_ENTRY) as i32;
// Expand argument.
argument = format_single_from_target(item, args_string(args, 0));
// If set-hook -R, fire the hook straight away.
if std::ptr::eq(cmd_get_entry(self_), &CMD_SET_HOOK_ENTRY) && args_has(args, 'R') {
notify_hook(item, argument);
free_(argument);
return cmd_retval::CMD_RETURN_NORMAL;
}
// Parse option name and index.
let Some(name) = options_match(cstr_to_str(argument), &raw mut idx, &raw mut ambiguous) else {
if args_has(args, 'q') {
break 'out;
}
if ambiguous != 0 {
cmdq_error!(item, "ambiguous option: {}", _s(argument));
} else {
cmdq_error!(item, "invalid option: {}", _s(argument));
}
break 'fail;
};
if args_count(args) < 2 {
value = null_mut();
} else {
value = args_string(args, 1);
}
if !value.is_null() && args_has(args, 'F') {
expanded = format_single_from_target(item, value);
value = expanded;
}
let mut cause = null_mut();
// Get the scope and table for the option .
scope = options_scope_from_name(
args,
window,
&name,
target,
&raw mut oo,
&raw mut cause,
);
if scope == OPTIONS_TABLE_NONE {
if args_has(args, 'q') {
break 'out;
}
cmdq_error!(item, "{}", _s(cause));
free_(cause);
break 'fail;
}
o = options_get_only(oo, &name);
parent = options_get(&mut *oo, &name);
// Check that array options and indexes match up.
if idx != -1 && (name.starts_with('@') || !options_is_array(parent)) {
cmdq_error!(item, "not an array: {}", _s(argument));
break 'fail;
}
// With -o, check this option is not already set.
if !args_has(args, 'u') && args_has(args, 'o') {
if idx == -1 {
already = !o.is_null() as i32;
} else if o.is_null() {
already = 0;
} else {
already = (!options_array_get(o, idx as u32).is_null()) as i32;
}
if already != 0 {
if args_has(args, 'q') {
break 'out;
}
cmdq_error!(item, "already set: {}", _s(argument));
break 'fail;
}
}
// Change the option.
if args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW {
for loop_ in tailq_foreach::<_, discr_entry>(&raw mut (*(*target).w).panes)
.map(NonNull::as_ptr)
{
let po = options_get_only((*loop_).options, &name);
if po.is_null() {
continue;
}
if let Err(cause) = options_remove_or_default(po, idx) {
cmdq_error!(item, "{}", cause.to_str().unwrap());
break 'fail;
}
}
}
if args_has(args, 'u') || args_has(args, 'U') {
if o.is_null() {
break 'out;
}
if let Err(cause) = options_remove_or_default(o, idx) {
cmdq_error!(item, "{}", cause.to_str().unwrap());
break 'fail;
}
} else if name.starts_with('@') {
if value.is_null() {
cmdq_error!(item, "empty value");
break 'fail;
}
options_set_string!(oo, &name, append, "{}", _s(value));
} else if idx == -1 && !options_is_array(parent) {
if let Err(cause) = options_from_string(
oo,
options_table_entry(parent),
(*options_table_entry(parent)).name,
value,
args_has(args, 'a'),
) {
cmdq_error!(item, "{}", cause.to_str().unwrap());
break 'fail;
}
} else {
if value.is_null() {
cmdq_error!(item, "empty value");
break 'fail;
}
if o.is_null() {
o = options_empty(oo, options_table_entry(parent));
}
if idx == -1 {
if !append {
options_array_clear(o);
}
if let Err(cause) = options_array_assign(o, cstr_to_str(value)) {
cmdq_error!(item, "{}", cause.to_str().unwrap());
break 'fail;
}
} else if let Err(cause) =
options_array_set(o, idx as u32, Some(cstr_to_str(value)), append)
{
cmdq_error!(item, "{}", cause.to_str().unwrap());
break 'fail;
}
}
options_push_changes(&name);
// ztmux: toggling zellij mode changes pane sizes (the frame
// inset), so relayout every window immediately - the window size
// is unchanged, so recalculate_sizes() alone would not re-fix it.
// @ztmux-ratatui gates the whole ratatui renderer (and, via the
// inset, pane sizes too), so it relayouts on the same path.
if name == "@ztmux-zellij-mode"
|| name == "@ztmux-pane-names"
|| name == "@ztmux-ratatui"
{
for w in rb_foreach(&raw mut WINDOWS).map(NonNull::as_ptr) {
layout_fix_panes(w, null_mut());
server_redraw_window(w);
}
}
}
// out:
free_(argument);
free_(expanded);
// free_(name);
return cmd_retval::CMD_RETURN_NORMAL;
}
// fail:
free_(argument);
free_(expanded);
// free_(name);
cmd_retval::CMD_RETURN_ERROR
}
}