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 { "visual-silence", SET_OPTION_FLAG
, 0, 0, NULL
},
140 { NULL
, 0, 0, 0, NULL
}
143 const struct set_option_entry set_window_option_table
[] = {
144 { "aggressive-resize", SET_OPTION_FLAG
, 0, 0, NULL
},
145 { "alternate-screen", SET_OPTION_FLAG
, 0, 0, NULL
},
146 { "automatic-rename", SET_OPTION_FLAG
, 0, 0, NULL
},
147 { "clock-mode-colour", SET_OPTION_COLOUR
, 0, 0, NULL
},
148 { "clock-mode-style",
149 SET_OPTION_CHOICE
, 0, 0, set_option_clock_mode_style_list
},
150 { "force-height", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
151 { "force-width", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
152 { "main-pane-height", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
153 { "main-pane-width", SET_OPTION_NUMBER
, 1, INT_MAX
, NULL
},
154 { "mode-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
155 { "mode-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
156 { "mode-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
157 { "mode-keys", SET_OPTION_CHOICE
, 0, 0, set_option_mode_keys_list
},
158 { "mode-mouse", SET_OPTION_FLAG
, 0, 0, NULL
},
159 { "monitor-activity", SET_OPTION_FLAG
, 0, 0, NULL
},
160 { "monitor-content", SET_OPTION_STRING
, 0, 0, NULL
},
161 { "monitor-silence",SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
162 { "other-pane-height", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
163 { "other-pane-width", SET_OPTION_NUMBER
, 0, INT_MAX
, NULL
},
164 { "remain-on-exit", SET_OPTION_FLAG
, 0, 0, NULL
},
165 { "synchronize-panes", SET_OPTION_FLAG
, 0, 0, NULL
},
166 { "utf8", SET_OPTION_FLAG
, 0, 0, NULL
},
167 { "window-status-alert-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
168 { "window-status-alert-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
169 { "window-status-alert-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
170 { "window-status-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
171 { "window-status-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
172 { "window-status-current-attr", SET_OPTION_ATTRIBUTES
, 0, 0, NULL
},
173 { "window-status-current-bg", SET_OPTION_COLOUR
, 0, 0, NULL
},
174 { "window-status-current-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
175 { "window-status-current-format", SET_OPTION_STRING
, 0, 0, NULL
},
176 { "window-status-fg", SET_OPTION_COLOUR
, 0, 0, NULL
},
177 { "window-status-format", SET_OPTION_STRING
, 0, 0, NULL
},
178 { "word-separators", SET_OPTION_STRING
, 0, 0, NULL
},
179 { "xterm-keys", SET_OPTION_FLAG
, 0, 0, NULL
},
180 { NULL
, 0, 0, 0, NULL
}
184 cmd_set_option_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
186 struct cmd_target_data
*data
= self
->data
;
187 const struct set_option_entry
*table
;
192 const struct set_option_entry
*entry
, *opt
;
194 struct job
*job
, *nextjob
;
198 if (cmd_check_flag(data
->chflags
, 's')) {
199 oo
= &global_options
;
200 table
= set_option_table
;
201 } else if (cmd_check_flag(data
->chflags
, 'w')) {
202 table
= set_window_option_table
;
203 if (cmd_check_flag(data
->chflags
, 'g'))
204 oo
= &global_w_options
;
206 wl
= cmd_find_window(ctx
, data
->target
, NULL
);
209 oo
= &wl
->window
->options
;
212 table
= set_session_option_table
;
213 if (cmd_check_flag(data
->chflags
, 'g'))
214 oo
= &global_s_options
;
216 s
= cmd_find_session(ctx
, data
->target
);
223 if (*data
->arg
== '\0') {
224 ctx
->error(ctx
, "invalid option");
229 for (opt
= table
; opt
->name
!= NULL
; opt
++) {
230 if (strncmp(opt
->name
, data
->arg
, strlen(data
->arg
)) != 0)
233 ctx
->error(ctx
, "ambiguous option: %s", data
->arg
);
238 /* Bail now if an exact match. */
239 if (strcmp(entry
->name
, data
->arg
) == 0)
243 ctx
->error(ctx
, "unknown option: %s", data
->arg
);
247 if (cmd_check_flag(data
->chflags
, 'u')) {
248 if (cmd_check_flag(data
->chflags
, 'g')) {
250 "can't unset global option: %s", entry
->name
);
253 if (data
->arg2
!= NULL
) {
255 "value passed to unset option: %s", entry
->name
);
259 options_remove(oo
, entry
->name
);
260 ctx
->info(ctx
, "unset option: %s", entry
->name
);
262 switch (entry
->type
) {
263 case SET_OPTION_STRING
:
264 cmd_set_option_string(ctx
, oo
, entry
,
265 data
->arg2
, cmd_check_flag(data
->chflags
, 'a'));
267 case SET_OPTION_NUMBER
:
268 cmd_set_option_number(ctx
, oo
, entry
, data
->arg2
);
270 case SET_OPTION_KEYS
:
271 cmd_set_option_keys(ctx
, oo
, entry
, data
->arg2
);
273 case SET_OPTION_COLOUR
:
274 cmd_set_option_colour(ctx
, oo
, entry
, data
->arg2
);
276 case SET_OPTION_ATTRIBUTES
:
277 cmd_set_option_attributes(ctx
, oo
, entry
, data
->arg2
);
279 case SET_OPTION_FLAG
:
280 cmd_set_option_flag(ctx
, oo
, entry
, data
->arg2
);
282 case SET_OPTION_CHOICE
:
283 cmd_set_option_choice(ctx
, oo
, entry
, data
->arg2
);
289 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
290 c
= ARRAY_ITEM(&clients
, i
);
291 if (c
!= NULL
&& c
->session
!= NULL
)
292 server_redraw_client(c
);
296 * Special-case: kill all persistent jobs if status-left, status-right
297 * or set-titles-string have changed. Persistent jobs are only used by
298 * the status line at the moment so this works XXX.
300 if (strcmp(entry
->name
, "status-left") == 0 ||
301 strcmp(entry
->name
, "status-right") == 0 ||
302 strcmp(entry
->name
, "status") == 0 ||
303 strcmp(entry
->name
, "set-titles-string") == 0 ||
304 strcmp(entry
->name
, "window-status-format") == 0) {
305 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
306 c
= ARRAY_ITEM(&clients
, i
);
307 if (c
== NULL
|| c
->session
== NULL
)
310 jobs
= &c
->status_jobs
;
314 while (job
!= NULL
) {
315 nextjob
= RB_NEXT(jobs
, jobs
, job
);
316 if (job
->flags
& JOB_PERSIST
) {
317 job_remove(jobs
, job
);
324 server_redraw_client(c
);
332 cmd_set_option_print(
333 const struct set_option_entry
*entry
, struct options_entry
*o
)
335 static char out
[BUFSIZ
];
337 struct keylist
*keylist
;
341 switch (entry
->type
) {
342 case SET_OPTION_STRING
:
343 xsnprintf(out
, sizeof out
, "\"%s\"", o
->str
);
345 case SET_OPTION_NUMBER
:
346 xsnprintf(out
, sizeof out
, "%lld", o
->num
);
348 case SET_OPTION_KEYS
:
350 for (i
= 0; i
< ARRAY_LENGTH(keylist
); i
++) {
351 strlcat(out
, key_string_lookup_key(
352 ARRAY_ITEM(keylist
, i
)), sizeof out
);
353 if (i
!= ARRAY_LENGTH(keylist
) - 1)
354 strlcat(out
, ",", sizeof out
);
357 case SET_OPTION_COLOUR
:
358 s
= colour_tostring(o
->num
);
359 xsnprintf(out
, sizeof out
, "%s", s
);
361 case SET_OPTION_ATTRIBUTES
:
362 s
= attributes_tostring(o
->num
);
363 xsnprintf(out
, sizeof out
, "%s", s
);
365 case SET_OPTION_FLAG
:
367 strlcpy(out
, "on", sizeof out
);
369 strlcpy(out
, "off", sizeof out
);
371 case SET_OPTION_CHOICE
:
372 s
= entry
->choices
[o
->num
];
373 xsnprintf(out
, sizeof out
, "%s", s
);
380 cmd_set_option_string(struct cmd_ctx
*ctx
, struct options
*oo
,
381 const struct set_option_entry
*entry
, char *value
, int append
)
383 struct options_entry
*o
;
384 char *oldvalue
, *newvalue
;
387 ctx
->error(ctx
, "empty value");
392 oldvalue
= options_get_string(oo
, entry
->name
);
393 xasprintf(&newvalue
, "%s%s", oldvalue
, value
);
397 o
= options_set_string(oo
, entry
->name
, "%s", newvalue
);
399 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
401 if (newvalue
!= value
)
406 cmd_set_option_number(struct cmd_ctx
*ctx
, struct options
*oo
,
407 const struct set_option_entry
*entry
, char *value
)
409 struct options_entry
*o
;
414 ctx
->error(ctx
, "empty value");
418 number
= strtonum(value
, entry
->minimum
, entry
->maximum
, &errstr
);
419 if (errstr
!= NULL
) {
420 ctx
->error(ctx
, "value is %s: %s", errstr
, value
);
424 o
= options_set_number(oo
, entry
->name
, number
);
426 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
430 cmd_set_option_keys(struct cmd_ctx
*ctx
, struct options
*oo
,
431 const struct set_option_entry
*entry
, char *value
)
433 struct options_entry
*o
;
434 struct keylist
*keylist
;
435 char *copyvalue
, *ptr
, *str
;
439 ctx
->error(ctx
, "empty value");
443 keylist
= xmalloc(sizeof *keylist
);
446 ptr
= copyvalue
= xstrdup(value
);
447 while ((str
= strsep(&ptr
, ",")) != NULL
) {
448 if ((key
= key_string_lookup_string(str
)) == KEYC_NONE
) {
450 ctx
->error(ctx
, "unknown key: %s", str
);
454 ARRAY_ADD(keylist
, key
);
458 o
= options_set_data(oo
, entry
->name
, keylist
, xfree
);
460 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
464 cmd_set_option_colour(struct cmd_ctx
*ctx
, struct options
*oo
,
465 const struct set_option_entry
*entry
, char *value
)
467 struct options_entry
*o
;
471 ctx
->error(ctx
, "empty value");
475 if ((colour
= colour_fromstring(value
)) == -1) {
476 ctx
->error(ctx
, "bad colour: %s", value
);
480 o
= options_set_number(oo
, entry
->name
, colour
);
482 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
486 cmd_set_option_attributes(struct cmd_ctx
*ctx
, struct options
*oo
,
487 const struct set_option_entry
*entry
, char *value
)
489 struct options_entry
*o
;
493 ctx
->error(ctx
, "empty value");
497 if ((attr
= attributes_fromstring(value
)) == -1) {
498 ctx
->error(ctx
, "bad attributes: %s", value
);
502 o
= options_set_number(oo
, entry
->name
, attr
);
504 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
508 cmd_set_option_flag(struct cmd_ctx
*ctx
, struct options
*oo
,
509 const struct set_option_entry
*entry
, char *value
)
511 struct options_entry
*o
;
514 if (value
== NULL
|| *value
== '\0')
515 flag
= !options_get_number(oo
, entry
->name
);
517 if ((value
[0] == '1' && value
[1] == '\0') ||
518 strcasecmp(value
, "on") == 0 ||
519 strcasecmp(value
, "yes") == 0)
521 else if ((value
[0] == '0' && value
[1] == '\0') ||
522 strcasecmp(value
, "off") == 0 ||
523 strcasecmp(value
, "no") == 0)
526 ctx
->error(ctx
, "bad value: %s", value
);
531 o
= options_set_number(oo
, entry
->name
, flag
);
533 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));
537 cmd_set_option_choice(struct cmd_ctx
*ctx
, struct options
*oo
,
538 const struct set_option_entry
*entry
, char *value
)
540 struct options_entry
*o
;
541 const char **choicep
;
545 ctx
->error(ctx
, "empty value");
550 for (choicep
= entry
->choices
; *choicep
!= NULL
; choicep
++) {
552 if (strncmp(*choicep
, value
, strlen(value
)) != 0)
556 ctx
->error(ctx
, "ambiguous option value: %s", value
);
562 ctx
->error(ctx
, "unknown option value: %s", value
);
566 o
= options_set_number(oo
, entry
->name
, choice
);
568 "set option: %s -> %s", o
->name
, cmd_set_option_print(entry
, o
));