4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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>
30 int cmd_set_option_exec(struct cmd
*, struct cmd_ctx
*);
32 const char *cmd_set_option_print(
33 const struct set_option_entry
*, struct options_entry
*);
34 void cmd_set_option_string(struct cmd_ctx
*,
35 struct options
*, const struct set_option_entry
*, char *, int);
36 void cmd_set_option_number(struct cmd_ctx
*,
37 struct options
*, const struct set_option_entry
*, char *);
38 void cmd_set_option_keys(struct cmd_ctx
*,
39 struct options
*, const struct set_option_entry
*, char *);
40 void cmd_set_option_colour(struct cmd_ctx
*,
41 struct options
*, const struct set_option_entry
*, char *);
42 void cmd_set_option_attributes(struct cmd_ctx
*,
43 struct options
*, const struct set_option_entry
*, char *);
44 void cmd_set_option_flag(struct cmd_ctx
*,
45 struct options
*, const struct set_option_entry
*, char *);
46 void cmd_set_option_choice(struct cmd_ctx
*,
47 struct options
*, const struct set_option_entry
*, char *);
49 const struct cmd_entry cmd_set_option_entry
= {
51 "[-agsuw] [-t target-session|target-window] option [value]",
60 const char *set_option_mode_keys_list
[] = {
63 const char *set_option_clock_mode_style_list
[] = {
66 const char *set_option_status_keys_list
[] = {
69 const char *set_option_status_justify_list
[] = {
70 "left", "centre", "right", NULL
72 const char *set_option_bell_action_list
[] = {
73 "none", "any", "current", NULL
76 const struct set_option_entry set_option_table
[] = {
77 { "escape-time", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
78 { "exit-unattached", SET_OPTION_FLAG
, 0, 0, NULL
},
79 { "quiet", SET_OPTION_FLAG
, 0, 0, NULL
},
80 { NULL
, 0, 0, 0, NULL
}
83 const struct set_option_entry set_session_option_table
[] = {
84 { "base-index", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
85 { "bell-action", SET_OPTION_CHOICE
, 0, 0, set_option_bell_action_list
},
86 { "buffer-limit", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
87 { "default-command", SET_OPTION_STRING
, 0, 0, NULL
},
88 { "default-path", SET_OPTION_STRING
, 0, 0, NULL
},
89 { "default-shell", SET_OPTION_STRING
, 0, 0, NULL
},
90 { "default-terminal", SET_OPTION_STRING
, 0, 0, NULL
},
91 { "destroy-unattached", SET_OPTION_FLAG
, 0, 0, NULL
},
92 { "detach-on-destroy", SET_OPTION_FLAG
, 0, 0, NULL
},
93 { "display-panes-colour", SET_OPTION_COLOUR
, 0, 0, NULL
},
94 { "display-panes-active-colour", SET_OPTION_COLOUR
, 0, 0, NULL
},
95 { "display-panes-time", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
96 { "display-time", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
97 { "history-limit", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
98 { "lock-after-time", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
99 { "lock-command", SET_OPTION_STRING
, 0, 0, NULL
},
100 { "lock-server", SET_OPTION_FLAG
, 0, 0, NULL
},
101 { "message-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
102 { "message-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
103 { "message-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
104 { "message-limit", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
105 { "mouse-select-pane", SET_OPTION_FLAG
, 0, 0, NULL
},
106 { "pane-active-border-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
107 { "pane-active-border-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
108 { "pane-border-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
109 { "pane-border-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
110 { "prefix", SET_OPTION_KEYS
, 0, 0, NULL
},
111 { "repeat-time", SET_OPTION_NUMBER
, 0, SHRT_MAX
, NULL
},
112 { "set-remain-on-exit", SET_OPTION_FLAG
, 0, 0, NULL
},
113 { "set-titles", SET_OPTION_FLAG
, 0, 0, NULL
},
114 { "set-titles-string", SET_OPTION_STRING
, 0, 0, NULL
},
115 { "status", SET_OPTION_FLAG
, 0, 0, NULL
},
116 { "status-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
117 { "status-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
118 { "status-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
119 { "status-interval", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
121 SET_OPTION_CHOICE
, 0, 0, set_option_status_justify_list
},
122 { "status-keys", SET_OPTION_CHOICE
, 0, 0, set_option_status_keys_list
},
123 { "status-left", SET_OPTION_STRING
, 0, 0, NULL
},
124 { "status-left-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
125 { "status-left-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
126 { "status-left-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
127 { "status-left-length", SET_OPTION_NUMBER
, 0, SHRT_MAX
, NULL
},
128 { "status-right", SET_OPTION_STRING
, 0, 0, NULL
},
129 { "status-right-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
130 { "status-right-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
131 { "status-right-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
132 { "status-right-length", SET_OPTION_NUMBER
, 0, SHRT_MAX
, NULL
},
133 { "status-utf8", SET_OPTION_FLAG
, 0, 0, NULL
},
134 { "terminal-overrides", SET_OPTION_STRING
, 0, 0, NULL
},
135 { "update-environment", SET_OPTION_STRING
, 0, 0, NULL
},
136 { "visual-activity", SET_OPTION_FLAG
, 0, 0, NULL
},
137 { "visual-bell", SET_OPTION_FLAG
, 0, 0, NULL
},
138 { "visual-content", SET_OPTION_FLAG
, 0, 0, NULL
},
139 { NULL
, 0, 0, 0, NULL
}
142 const struct set_option_entry set_window_option_table
[] = {
143 { "aggressive-resize", SET_OPTION_FLAG
, 0, 0, NULL
},
144 { "alternate-screen", SET_OPTION_FLAG
, 0, 0, NULL
},
145 { "automatic-rename", SET_OPTION_FLAG
, 0, 0, NULL
},
146 { "clock-mode-colour", SET_OPTION_COLOUR
, 0, 0, NULL
},
147 { "clock-mode-style",
148 SET_OPTION_CHOICE
, 0, 0, set_option_clock_mode_style_list
},
149 { "force-height", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
150 { "force-width", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
151 { "main-pane-height", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
152 { "main-pane-width", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
153 { "mode-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
154 { "mode-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
155 { "mode-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
156 { "mode-keys", SET_OPTION_CHOICE
, 0, 0, set_option_mode_keys_list
},
157 { "mode-mouse", SET_OPTION_FLAG
, 0, 0, NULL
},
158 { "monitor-activity", SET_OPTION_FLAG
, 0, 0, NULL
},
159 { "monitor-content", SET_OPTION_STRING
, 0, 0, NULL
},
160 { "remain-on-exit", SET_OPTION_FLAG
, 0, 0, NULL
},
161 { "synchronize-panes", SET_OPTION_FLAG
, 0, 0, NULL
},
162 { "utf8", SET_OPTION_FLAG
, 0, 0, NULL
},
163 { "window-status-alert-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
164 { "window-status-alert-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
165 { "window-status-alert-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
166 { "window-status-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
167 { "window-status-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
168 { "window-status-current-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
169 { "window-status-current-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
170 { "window-status-current-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
171 { "window-status-current-format", SET_OPTION_STRING
, 0, 0, NULL
},
172 { "window-status-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
173 { "window-status-format", SET_OPTION_STRING
, 0, 0, NULL
},
174 { "word-separators", SET_OPTION_STRING
, 0, 0, NULL
},
175 { "xterm-keys", SET_OPTION_FLAG
, 0, 0, NULL
},
176 { NULL
, 0, 0, 0, NULL
}
180 cmd_set_option_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
182 struct cmd_target_data
*data
= self
->data
;
183 const struct set_option_entry
*table
;
188 const struct set_option_entry
*entry
, *opt
;
190 struct job
*job
, *nextjob
;
194 if (cmd_check_flag(data
->chflags
, 's')) {
195 oo
= &global_options
;
196 table
= set_option_table
;
197 } else if (cmd_check_flag(data
->chflags
, 'w')) {
198 table
= set_window_option_table
;
199 if (cmd_check_flag(data
->chflags
, 'g'))
200 oo
= &global_w_options
;
202 wl
= cmd_find_window(ctx
, data
->target
, NULL
);
205 oo
= &wl
->window
->options
;
208 table
= set_session_option_table
;
209 if (cmd_check_flag(data
->chflags
, 'g'))
210 oo
= &global_s_options
;
212 s
= cmd_find_session(ctx
, data
->target
);
219 if (*data
->arg
== '\0') {
220 ctx
->error(ctx
, "invalid option");
225 for (opt
= table
; opt
->name
!= NULL
; opt
++) {
226 if (strncmp(opt
->name
, data
->arg
, strlen(data
->arg
)) != 0)
229 ctx
->error(ctx
, "ambiguous option: %s", data
->arg
);
234 /* Bail now if an exact match. */
235 if (strcmp(entry
->name
, data
->arg
) == 0)
239 ctx
->error(ctx
, "unknown option: %s", data
->arg
);
243 if (cmd_check_flag(data
->chflags
, 'u')) {
244 if (cmd_check_flag(data
->chflags
, 'g')) {
246 "can't unset global option: %s", entry
->name
);
249 if (data
->arg2
!= NULL
) {
251 "value passed to unset option: %s", entry
->name
);
255 options_remove(oo
, entry
->name
);
256 ctx
->info(ctx
, "unset option: %s", entry
->name
);
258 switch (entry
->type
) {
259 case SET_OPTION_STRING
:
260 cmd_set_option_string(ctx
, oo
, entry
,
261 data
->arg2
, cmd_check_flag(data
->chflags
, 'a'));
263 case SET_OPTION_NUMBER
:
264 cmd_set_option_number(ctx
, oo
, entry
, data
->arg2
);
266 case SET_OPTION_KEYS
:
267 cmd_set_option_keys(ctx
, oo
, entry
, data
->arg2
);
269 case SET_OPTION_COLOUR
:
270 cmd_set_option_colour(ctx
, oo
, entry
, data
->arg2
);
272 case SET_OPTION_ATTRIBUTES
:
273 cmd_set_option_attributes(ctx
, oo
, entry
, data
->arg2
);
275 case SET_OPTION_FLAG
:
276 cmd_set_option_flag(ctx
, oo
, entry
, data
->arg2
);
278 case SET_OPTION_CHOICE
:
279 cmd_set_option_choice(ctx
, oo
, entry
, data
->arg2
);
285 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
286 c
= ARRAY_ITEM(&clients
, i
);
287 if (c
!= NULL
&& c
->session
!= NULL
)
288 server_redraw_client(c
);
292 * Special-case: kill all persistent jobs if status-left, status-right
293 * or set-titles-string have changed. Persistent jobs are only used by
294 * the status line at the moment so this works XXX.
296 if (strcmp(entry
->name
, "status-left") == 0 ||
297 strcmp(entry
->name
, "status-right") == 0 ||
298 strcmp(entry
->name
, "status") == 0 ||
299 strcmp(entry
->name
, "set-titles-string") == 0 ||
300 strcmp(entry
->name
, "window-status-format") == 0) {
301 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
302 c
= ARRAY_ITEM(&clients
, i
);
303 if (c
== NULL
|| c
->session
== NULL
)
306 jobs
= &c
->status_jobs
;
310 while (job
!= NULL
) {
311 nextjob
= RB_NEXT(jobs
, jobs
, job
);
312 if (job
->flags
& JOB_PERSIST
) {
313 job_remove(jobs
, job
);
320 server_redraw_client(c
);
328 cmd_set_option_print(
329 const struct set_option_entry
*entry
, struct options_entry
*o
)
331 static char out
[BUFSIZ
];
333 struct keylist
*keylist
;
337 switch (entry
->type
) {
338 case SET_OPTION_STRING
:
339 xsnprintf(out
, sizeof out
, "\"%s\"", o
->str
);
341 case SET_OPTION_NUMBER
:
342 xsnprintf(out
, sizeof out
, "%lld", o
->num
);
344 case SET_OPTION_KEYS
:
346 for (i
= 0; i
< ARRAY_LENGTH(keylist
); i
++) {
347 strlcat(out
, key_string_lookup_key(
348 ARRAY_ITEM(keylist
, i
)), sizeof out
);
349 if (i
!= ARRAY_LENGTH(keylist
) - 1)
350 strlcat(out
, ",", sizeof out
);
353 case SET_OPTION_COLOUR
:
354 s
= colour_tostring(o
->num
);
355 xsnprintf(out
, sizeof out
, "%s", s
);
357 case SET_OPTION_ATTRIBUTES
:
358 s
= attributes_tostring(o
->num
);
359 xsnprintf(out
, sizeof out
, "%s", s
);
361 case SET_OPTION_FLAG
:
363 strlcpy(out
, "on", sizeof out
);
365 strlcpy(out
, "off", sizeof out
);
367 case SET_OPTION_CHOICE
:
368 s
= entry
->choices
[o
->num
];
369 xsnprintf(out
, sizeof out
, "%s", s
);
376 cmd_set_option_string(struct cmd_ctx
*ctx
, struct options
*oo
,
377 const struct set_option_entry
*entry
, char *value
, int append
)
379 struct options_entry
*o
;
380 char *oldvalue
, *newvalue
;
383 ctx
->error(ctx
, "empty value");
388 oldvalue
= options_get_string(oo
, entry
->name
);
389 xasprintf(&newvalue
, "%s%s", oldvalue
, value
);
393 o
= options_set_string(oo
, entry
->name
, "%s", newvalue
);
395 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
397 if (newvalue
!= value
)
402 cmd_set_option_number(struct cmd_ctx
*ctx
, struct options
*oo
,
403 const struct set_option_entry
*entry
, char *value
)
405 struct options_entry
*o
;
410 ctx
->error(ctx
, "empty value");
414 number
= strtonum(value
, entry
->minimum
, entry
->maximum
, &errstr
);
415 if (errstr
!= NULL
) {
416 ctx
->error(ctx
, "value is %s: %s", errstr
, value
);
420 o
= options_set_number(oo
, entry
->name
, number
);
422 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
426 cmd_set_option_keys(struct cmd_ctx
*ctx
, struct options
*oo
,
427 const struct set_option_entry
*entry
, char *value
)
429 struct options_entry
*o
;
430 struct keylist
*keylist
;
431 char *copyvalue
, *ptr
, *str
;
435 ctx
->error(ctx
, "empty value");
439 keylist
= xmalloc(sizeof *keylist
);
442 ptr
= copyvalue
= xstrdup(value
);
443 while ((str
= strsep(&ptr
, ",")) != NULL
) {
444 if ((key
= key_string_lookup_string(str
)) == KEYC_NONE
) {
446 ctx
->error(ctx
, "unknown key: %s", str
);
450 ARRAY_ADD(keylist
, key
);
454 o
= options_set_data(oo
, entry
->name
, keylist
, xfree
);
456 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
460 cmd_set_option_colour(struct cmd_ctx
*ctx
, struct options
*oo
,
461 const struct set_option_entry
*entry
, char *value
)
463 struct options_entry
*o
;
467 ctx
->error(ctx
, "empty value");
471 if ((colour
= colour_fromstring(value
)) == -1) {
472 ctx
->error(ctx
, "bad colour: %s", value
);
476 o
= options_set_number(oo
, entry
->name
, colour
);
478 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
482 cmd_set_option_attributes(struct cmd_ctx
*ctx
, struct options
*oo
,
483 const struct set_option_entry
*entry
, char *value
)
485 struct options_entry
*o
;
489 ctx
->error(ctx
, "empty value");
493 if ((attr
= attributes_fromstring(value
)) == -1) {
494 ctx
->error(ctx
, "bad attributes: %s", value
);
498 o
= options_set_number(oo
, entry
->name
, attr
);
500 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
504 cmd_set_option_flag(struct cmd_ctx
*ctx
, struct options
*oo
,
505 const struct set_option_entry
*entry
, char *value
)
507 struct options_entry
*o
;
510 if (value
== NULL
|| *value
== '\0')
511 flag
= !options_get_number(oo
, entry
->name
);
513 if ((value
[0] == '1' && value
[1] == '\0') ||
514 strcasecmp(value
, "on") == 0 ||
515 strcasecmp(value
, "yes") == 0)
517 else if ((value
[0] == '0' && value
[1] == '\0') ||
518 strcasecmp(value
, "off") == 0 ||
519 strcasecmp(value
, "no") == 0)
522 ctx
->error(ctx
, "bad value: %s", value
);
527 o
= options_set_number(oo
, entry
->name
, flag
);
529 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
533 cmd_set_option_choice(struct cmd_ctx
*ctx
, struct options
*oo
,
534 const struct set_option_entry
*entry
, char *value
)
536 struct options_entry
*o
;
537 const char **choicep
;
541 ctx
->error(ctx
, "empty value");
546 for (choicep
= entry
->choices
; *choicep
!= NULL
; choicep
++) {
548 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
552 ctx
->error(ctx
, "ambiguous option value: %s", value
);
558 ctx
->error(ctx
, "unknown option value: %s", value
);
562 o
= options_set_number(oo
, entry
->name
, choice
);
564 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));