This commit was manufactured by cvs2svn to create tag 'TMUX_1_2'.
[tmux.git] / cmd-set-option.c
blob54216951962d033988ba590ccabf8fd9eae88997
1 /* $Id: cmd-set-option.c,v 1.96 2010-02-26 13:31:39 tcunha Exp $ */
3 /*
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>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Set an option.
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 = {
50 "set-option", "set",
51 "[-agsuw] [-t target-session|target-window] option [value]",
52 CMD_ARG12, "agsuw",
53 NULL,
54 cmd_target_parse,
55 cmd_set_option_exec,
56 cmd_target_free,
57 cmd_target_print
60 const char *set_option_mode_keys_list[] = {
61 "emacs", "vi", NULL
63 const char *set_option_clock_mode_style_list[] = {
64 "12", "24", NULL
66 const char *set_option_status_keys_list[] = {
67 "emacs", "vi", NULL
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 { "quiet", SET_OPTION_FLAG, 0, 0, NULL },
79 { NULL, 0, 0, 0, NULL }
82 const struct set_option_entry set_session_option_table[] = {
83 { "base-index", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
84 { "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list },
85 { "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
86 { "default-command", SET_OPTION_STRING, 0, 0, NULL },
87 { "default-path", SET_OPTION_STRING, 0, 0, NULL },
88 { "default-shell", SET_OPTION_STRING, 0, 0, NULL },
89 { "default-terminal", SET_OPTION_STRING, 0, 0, NULL },
90 { "display-panes-colour", SET_OPTION_COLOUR, 0, 0, NULL },
91 { "display-panes-active-colour", SET_OPTION_COLOUR, 0, 0, NULL },
92 { "display-panes-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
93 { "display-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
94 { "history-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
95 { "lock-after-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
96 { "lock-command", SET_OPTION_STRING, 0, 0, NULL },
97 { "lock-server", SET_OPTION_FLAG, 0, 0, NULL },
98 { "message-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
99 { "message-bg", SET_OPTION_COLOUR, 0, 0, NULL },
100 { "message-fg", SET_OPTION_COLOUR, 0, 0, NULL },
101 { "message-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
102 { "mouse-select-pane", SET_OPTION_FLAG, 0, 0, NULL },
103 { "pane-active-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
104 { "pane-active-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
105 { "pane-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
106 { "pane-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
107 { "prefix", SET_OPTION_KEYS, 0, 0, NULL },
108 { "repeat-time", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
109 { "set-remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
110 { "set-titles", SET_OPTION_FLAG, 0, 0, NULL },
111 { "set-titles-string", SET_OPTION_STRING, 0, 0, NULL },
112 { "status", SET_OPTION_FLAG, 0, 0, NULL },
113 { "status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
114 { "status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
115 { "status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
116 { "status-interval", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
117 { "status-justify",
118 SET_OPTION_CHOICE, 0, 0, set_option_status_justify_list },
119 { "status-keys", SET_OPTION_CHOICE, 0, 0, set_option_status_keys_list },
120 { "status-left", SET_OPTION_STRING, 0, 0, NULL },
121 { "status-left-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
122 { "status-left-bg", SET_OPTION_COLOUR, 0, 0, NULL },
123 { "status-left-fg", SET_OPTION_COLOUR, 0, 0, NULL },
124 { "status-left-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
125 { "status-right", SET_OPTION_STRING, 0, 0, NULL },
126 { "status-right-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
127 { "status-right-bg", SET_OPTION_COLOUR, 0, 0, NULL },
128 { "status-right-fg", SET_OPTION_COLOUR, 0, 0, NULL },
129 { "status-right-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
130 { "status-utf8", SET_OPTION_FLAG, 0, 0, NULL },
131 { "terminal-overrides", SET_OPTION_STRING, 0, 0, NULL },
132 { "update-environment", SET_OPTION_STRING, 0, 0, NULL },
133 { "visual-activity", SET_OPTION_FLAG, 0, 0, NULL },
134 { "visual-bell", SET_OPTION_FLAG, 0, 0, NULL },
135 { "visual-content", SET_OPTION_FLAG, 0, 0, NULL },
136 { NULL, 0, 0, 0, NULL }
139 const struct set_option_entry set_window_option_table[] = {
140 { "aggressive-resize", SET_OPTION_FLAG, 0, 0, NULL },
141 { "alternate-screen", SET_OPTION_FLAG, 0, 0, NULL },
142 { "automatic-rename", SET_OPTION_FLAG, 0, 0, NULL },
143 { "clock-mode-colour", SET_OPTION_COLOUR, 0, 0, NULL },
144 { "clock-mode-style",
145 SET_OPTION_CHOICE, 0, 0, set_option_clock_mode_style_list },
146 { "force-height", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
147 { "force-width", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
148 { "main-pane-height", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
149 { "main-pane-width", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
150 { "mode-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
151 { "mode-bg", SET_OPTION_COLOUR, 0, 0, NULL },
152 { "mode-fg", SET_OPTION_COLOUR, 0, 0, NULL },
153 { "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
154 { "mode-mouse", SET_OPTION_FLAG, 0, 0, NULL },
155 { "monitor-activity", SET_OPTION_FLAG, 0, 0, NULL },
156 { "monitor-content", SET_OPTION_STRING, 0, 0, NULL },
157 { "remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
158 { "synchronize-panes", SET_OPTION_FLAG, 0, 0, NULL },
159 { "utf8", SET_OPTION_FLAG, 0, 0, NULL },
160 { "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
161 { "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
162 { "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
163 { "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
164 { "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
165 { "window-status-current-format", SET_OPTION_STRING, 0, 0, NULL },
166 { "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
167 { "window-status-format", SET_OPTION_STRING, 0, 0, NULL },
168 { "word-separators", SET_OPTION_STRING, 0, 0, NULL },
169 { "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
170 { NULL, 0, 0, 0, NULL }
174 cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
176 struct cmd_target_data *data = self->data;
177 const struct set_option_entry *table;
178 struct session *s;
179 struct winlink *wl;
180 struct client *c;
181 struct options *oo;
182 const struct set_option_entry *entry, *opt;
183 struct jobs *jobs;
184 struct job *job, *nextjob;
185 u_int i;
186 int try_again;
188 if (cmd_check_flag(data->chflags, 's')) {
189 oo = &global_options;
190 table = set_option_table;
191 } else if (cmd_check_flag(data->chflags, 'w')) {
192 table = set_window_option_table;
193 if (cmd_check_flag(data->chflags, 'g'))
194 oo = &global_w_options;
195 else {
196 wl = cmd_find_window(ctx, data->target, NULL);
197 if (wl == NULL)
198 return (-1);
199 oo = &wl->window->options;
201 } else {
202 table = set_session_option_table;
203 if (cmd_check_flag(data->chflags, 'g'))
204 oo = &global_s_options;
205 else {
206 s = cmd_find_session(ctx, data->target);
207 if (s == NULL)
208 return (-1);
209 oo = &s->options;
213 if (*data->arg == '\0') {
214 ctx->error(ctx, "invalid option");
215 return (-1);
218 entry = NULL;
219 for (opt = table; opt->name != NULL; opt++) {
220 if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
221 continue;
222 if (entry != NULL) {
223 ctx->error(ctx, "ambiguous option: %s", data->arg);
224 return (-1);
226 entry = opt;
228 /* Bail now if an exact match. */
229 if (strcmp(entry->name, data->arg) == 0)
230 break;
232 if (entry == NULL) {
233 ctx->error(ctx, "unknown option: %s", data->arg);
234 return (-1);
237 if (cmd_check_flag(data->chflags, 'u')) {
238 if (cmd_check_flag(data->chflags, 'g')) {
239 ctx->error(ctx,
240 "can't unset global option: %s", entry->name);
241 return (-1);
243 if (data->arg2 != NULL) {
244 ctx->error(ctx,
245 "value passed to unset option: %s", entry->name);
246 return (-1);
249 options_remove(oo, entry->name);
250 ctx->info(ctx, "unset option: %s", entry->name);
251 } else {
252 switch (entry->type) {
253 case SET_OPTION_STRING:
254 cmd_set_option_string(ctx, oo, entry,
255 data->arg2, cmd_check_flag(data->chflags, 'a'));
256 break;
257 case SET_OPTION_NUMBER:
258 cmd_set_option_number(ctx, oo, entry, data->arg2);
259 break;
260 case SET_OPTION_KEYS:
261 cmd_set_option_keys(ctx, oo, entry, data->arg2);
262 break;
263 case SET_OPTION_COLOUR:
264 cmd_set_option_colour(ctx, oo, entry, data->arg2);
265 break;
266 case SET_OPTION_ATTRIBUTES:
267 cmd_set_option_attributes(ctx, oo, entry, data->arg2);
268 break;
269 case SET_OPTION_FLAG:
270 cmd_set_option_flag(ctx, oo, entry, data->arg2);
271 break;
272 case SET_OPTION_CHOICE:
273 cmd_set_option_choice(ctx, oo, entry, data->arg2);
274 break;
278 recalculate_sizes();
279 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
280 c = ARRAY_ITEM(&clients, i);
281 if (c != NULL && c->session != NULL)
282 server_redraw_client(c);
286 * Special-case: kill all persistent jobs if status-left, status-right
287 * or set-titles-string have changed. Persistent jobs are only used by
288 * the status line at the moment so this works XXX.
290 if (strcmp(entry->name, "status-left") == 0 ||
291 strcmp(entry->name, "status-right") == 0 ||
292 strcmp(entry->name, "set-titles-string") == 0 ||
293 strcmp(entry->name, "window-status-format") == 0) {
294 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
295 c = ARRAY_ITEM(&clients, i);
296 if (c == NULL || c->session == NULL)
297 continue;
299 jobs = &c->status_jobs;
300 do {
301 try_again = 0;
302 job = RB_ROOT(jobs);
303 while (job != NULL) {
304 nextjob = RB_NEXT(jobs, jobs, job);
305 if (job->flags & JOB_PERSIST) {
306 job_remove(jobs, job);
307 try_again = 1;
308 break;
310 job = nextjob;
312 } while (try_again);
313 server_redraw_client(c);
317 return (0);
320 const char *
321 cmd_set_option_print(
322 const struct set_option_entry *entry, struct options_entry *o)
324 static char out[BUFSIZ];
325 const char *s;
326 struct keylist *keylist;
327 u_int i;
329 *out = '\0';
330 switch (entry->type) {
331 case SET_OPTION_STRING:
332 xsnprintf(out, sizeof out, "\"%s\"", o->str);
333 break;
334 case SET_OPTION_NUMBER:
335 xsnprintf(out, sizeof out, "%lld", o->num);
336 break;
337 case SET_OPTION_KEYS:
338 keylist = o->data;
339 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
340 strlcat(out, key_string_lookup_key(
341 ARRAY_ITEM(keylist, i)), sizeof out);
342 if (i != ARRAY_LENGTH(keylist) - 1)
343 strlcat(out, ",", sizeof out);
345 break;
346 case SET_OPTION_COLOUR:
347 s = colour_tostring(o->num);
348 xsnprintf(out, sizeof out, "%s", s);
349 break;
350 case SET_OPTION_ATTRIBUTES:
351 s = attributes_tostring(o->num);
352 xsnprintf(out, sizeof out, "%s", s);
353 break;
354 case SET_OPTION_FLAG:
355 if (o->num)
356 strlcpy(out, "on", sizeof out);
357 else
358 strlcpy(out, "off", sizeof out);
359 break;
360 case SET_OPTION_CHOICE:
361 s = entry->choices[o->num];
362 xsnprintf(out, sizeof out, "%s", s);
363 break;
365 return (out);
368 void
369 cmd_set_option_string(struct cmd_ctx *ctx, struct options *oo,
370 const struct set_option_entry *entry, char *value, int append)
372 struct options_entry *o;
373 char *oldvalue, *newvalue;
375 if (value == NULL) {
376 ctx->error(ctx, "empty value");
377 return;
380 if (append) {
381 oldvalue = options_get_string(oo, entry->name);
382 xasprintf(&newvalue, "%s%s", oldvalue, value);
383 } else
384 newvalue = value;
386 o = options_set_string(oo, entry->name, "%s", newvalue);
387 ctx->info(ctx,
388 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
390 if (newvalue != value)
391 xfree(newvalue);
394 void
395 cmd_set_option_number(struct cmd_ctx *ctx, struct options *oo,
396 const struct set_option_entry *entry, char *value)
398 struct options_entry *o;
399 long long number;
400 const char *errstr;
402 if (value == NULL) {
403 ctx->error(ctx, "empty value");
404 return;
407 number = strtonum(value, entry->minimum, entry->maximum, &errstr);
408 if (errstr != NULL) {
409 ctx->error(ctx, "value is %s: %s", errstr, value);
410 return;
413 o = options_set_number(oo, entry->name, number);
414 ctx->info(ctx,
415 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
418 void
419 cmd_set_option_keys(struct cmd_ctx *ctx, struct options *oo,
420 const struct set_option_entry *entry, char *value)
422 struct options_entry *o;
423 struct keylist *keylist;
424 char *copyvalue, *ptr, *str;
425 int key;
427 if (value == NULL) {
428 ctx->error(ctx, "empty value");
429 return;
432 keylist = xmalloc(sizeof *keylist);
433 ARRAY_INIT(keylist);
435 ptr = copyvalue = xstrdup(value);
436 while ((str = strsep(&ptr, ",")) != NULL) {
437 if ((key = key_string_lookup_string(str)) == KEYC_NONE) {
438 xfree(keylist);
439 ctx->error(ctx, "unknown key: %s", str);
440 xfree(copyvalue);
441 return;
443 ARRAY_ADD(keylist, key);
445 xfree(copyvalue);
447 o = options_set_data(oo, entry->name, keylist, xfree);
448 ctx->info(ctx,
449 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
452 void
453 cmd_set_option_colour(struct cmd_ctx *ctx, struct options *oo,
454 const struct set_option_entry *entry, char *value)
456 struct options_entry *o;
457 int colour;
459 if (value == NULL) {
460 ctx->error(ctx, "empty value");
461 return;
464 if ((colour = colour_fromstring(value)) == -1) {
465 ctx->error(ctx, "bad colour: %s", value);
466 return;
469 o = options_set_number(oo, entry->name, colour);
470 ctx->info(ctx,
471 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
474 void
475 cmd_set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
476 const struct set_option_entry *entry, char *value)
478 struct options_entry *o;
479 int attr;
481 if (value == NULL) {
482 ctx->error(ctx, "empty value");
483 return;
486 if ((attr = attributes_fromstring(value)) == -1) {
487 ctx->error(ctx, "bad attributes: %s", value);
488 return;
491 o = options_set_number(oo, entry->name, attr);
492 ctx->info(ctx,
493 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
496 void
497 cmd_set_option_flag(struct cmd_ctx *ctx, struct options *oo,
498 const struct set_option_entry *entry, char *value)
500 struct options_entry *o;
501 int flag;
503 if (value == NULL || *value == '\0')
504 flag = !options_get_number(oo, entry->name);
505 else {
506 if ((value[0] == '1' && value[1] == '\0') ||
507 strcasecmp(value, "on") == 0 ||
508 strcasecmp(value, "yes") == 0)
509 flag = 1;
510 else if ((value[0] == '0' && value[1] == '\0') ||
511 strcasecmp(value, "off") == 0 ||
512 strcasecmp(value, "no") == 0)
513 flag = 0;
514 else {
515 ctx->error(ctx, "bad value: %s", value);
516 return;
520 o = options_set_number(oo, entry->name, flag);
521 ctx->info(ctx,
522 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
525 void
526 cmd_set_option_choice(struct cmd_ctx *ctx, struct options *oo,
527 const struct set_option_entry *entry, char *value)
529 struct options_entry *o;
530 const char **choicep;
531 int n, choice = -1;
533 if (value == NULL) {
534 ctx->error(ctx, "empty value");
535 return;
538 n = 0;
539 for (choicep = entry->choices; *choicep != NULL; choicep++) {
540 n++;
541 if (strncmp(*choicep, value, strlen(value)) != 0)
542 continue;
544 if (choice != -1) {
545 ctx->error(ctx, "ambiguous option value: %s", value);
546 return;
548 choice = n - 1;
550 if (choice == -1) {
551 ctx->error(ctx, "unknown option value: %s", value);
552 return;
555 o = options_set_number(oo, entry->name, choice);
556 ctx->info(ctx,
557 "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));