Annotation of src/usr.bin/tmux/cmd-set-window-option.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: cmd-set-window-option.c,v 1.14 2009/11/13 19:53:29 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Set a window option.
28: */
29:
30: int cmd_set_window_option_exec(struct cmd *, struct cmd_ctx *);
31:
32: const struct cmd_entry cmd_set_window_option_entry = {
33: "set-window-option", "setw",
1.11 nicm 34: "[-agu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.14 nicm 35: CMD_ARG12, "agu",
1.1 nicm 36: NULL,
1.11 nicm 37: cmd_target_parse,
1.1 nicm 38: cmd_set_window_option_exec,
1.11 nicm 39: cmd_target_free,
40: cmd_target_print
1.1 nicm 41: };
42:
43: const char *set_option_mode_keys_list[] = {
44: "emacs", "vi", NULL
45: };
46: const char *set_option_clock_mode_style_list[] = {
47: "12", "24", NULL
48: };
1.6 nicm 49: const struct set_option_entry set_window_option_table[] = {
1.1 nicm 50: { "aggressive-resize", SET_OPTION_FLAG, 0, 0, NULL },
51: { "automatic-rename", SET_OPTION_FLAG, 0, 0, NULL },
52: { "clock-mode-colour", SET_OPTION_COLOUR, 0, 0, NULL },
53: { "clock-mode-style",
54: SET_OPTION_CHOICE, 0, 0, set_option_clock_mode_style_list },
55: { "force-height", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
56: { "force-width", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.5 nicm 57: { "main-pane-height", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
1.1 nicm 58: { "main-pane-width", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
59: { "mode-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
60: { "mode-bg", SET_OPTION_COLOUR, 0, 0, NULL },
61: { "mode-fg", SET_OPTION_COLOUR, 0, 0, NULL },
62: { "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
1.9 nicm 63: { "mode-mouse", SET_OPTION_FLAG, 0, 0, NULL },
1.1 nicm 64: { "monitor-activity", SET_OPTION_FLAG, 0, 0, NULL },
65: { "monitor-content", SET_OPTION_STRING, 0, 0, NULL },
66: { "remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
1.13 nicm 67: { "synchronize-panes", SET_OPTION_FLAG, 0, 0, NULL },
1.1 nicm 68: { "utf8", SET_OPTION_FLAG, 0, 0, NULL },
69: { "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
70: { "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
1.7 nicm 71: { "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
72: { "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
73: { "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.15 ! nicm 74: { "window-status-current-format", SET_OPTION_STRING, 0, 0, NULL },
1.1 nicm 75: { "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.15 ! nicm 76: { "window-status-format", SET_OPTION_STRING, 0, 0, NULL },
1.1 nicm 77: { "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
1.6 nicm 78: { NULL, 0, 0, 0, NULL }
1.1 nicm 79: };
80:
81: int
82: cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
83: {
1.11 nicm 84: struct cmd_target_data *data = self->data;
1.1 nicm 85: struct winlink *wl;
86: struct client *c;
87: struct options *oo;
1.6 nicm 88: const struct set_option_entry *entry, *opt;
1.15 ! nicm 89: struct jobs *jobs;
! 90: struct job *job, *nextjob;
1.1 nicm 91: u_int i;
1.15 ! nicm 92: int try_again;
1.1 nicm 93:
1.14 nicm 94: if (cmd_check_flag(data->chflags, 'g'))
1.3 nicm 95: oo = &global_w_options;
1.1 nicm 96: else {
97: if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
98: return (-1);
99: oo = &wl->window->options;
100: }
101:
1.11 nicm 102: if (*data->arg == '\0') {
1.1 nicm 103: ctx->error(ctx, "invalid option");
104: return (-1);
105: }
106:
107: entry = NULL;
1.6 nicm 108: for (opt = set_window_option_table; opt->name != NULL; opt++) {
1.11 nicm 109: if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
1.1 nicm 110: continue;
111: if (entry != NULL) {
1.11 nicm 112: ctx->error(ctx, "ambiguous option: %s", data->arg);
1.1 nicm 113: return (-1);
114: }
1.6 nicm 115: entry = opt;
1.1 nicm 116:
117: /* Bail now if an exact match. */
1.11 nicm 118: if (strcmp(entry->name, data->arg) == 0)
1.1 nicm 119: break;
120: }
121: if (entry == NULL) {
1.11 nicm 122: ctx->error(ctx, "unknown option: %s", data->arg);
1.1 nicm 123: return (-1);
124: }
125:
1.14 nicm 126: if (cmd_check_flag(data->chflags, 'u')) {
127: if (cmd_check_flag(data->chflags, 'g')) {
1.1 nicm 128: ctx->error(ctx,
129: "can't unset global option: %s", entry->name);
130: return (-1);
131: }
1.11 nicm 132: if (data->arg2 != NULL) {
1.1 nicm 133: ctx->error(ctx,
134: "value passed to unset option: %s", entry->name);
135: return (-1);
136: }
137:
138: options_remove(oo, entry->name);
139: ctx->info(ctx, "unset option: %s", entry->name);
140: } else {
141: switch (entry->type) {
142: case SET_OPTION_STRING:
1.10 nicm 143: set_option_string(ctx, oo, entry,
1.14 nicm 144: data->arg2, cmd_check_flag(data->chflags, 'a'));
1.1 nicm 145: break;
146: case SET_OPTION_NUMBER:
1.11 nicm 147: set_option_number(ctx, oo, entry, data->arg2);
1.1 nicm 148: break;
1.12 nicm 149: case SET_OPTION_KEYS:
150: set_option_keys(ctx, oo, entry, data->arg2);
1.1 nicm 151: break;
152: case SET_OPTION_COLOUR:
1.11 nicm 153: set_option_colour(ctx, oo, entry, data->arg2);
1.1 nicm 154: break;
155: case SET_OPTION_ATTRIBUTES:
1.11 nicm 156: set_option_attributes(ctx, oo, entry, data->arg2);
1.1 nicm 157: break;
158: case SET_OPTION_FLAG:
1.11 nicm 159: set_option_flag(ctx, oo, entry, data->arg2);
1.1 nicm 160: break;
161: case SET_OPTION_CHOICE:
1.11 nicm 162: set_option_choice(ctx, oo, entry, data->arg2);
1.1 nicm 163: break;
164: }
165: }
166:
167: recalculate_sizes();
168: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
169: c = ARRAY_ITEM(&clients, i);
170: if (c != NULL && c->session != NULL)
171: server_redraw_client(c);
1.15 ! nicm 172: }
! 173:
! 174: /*
! 175: * Special-case: kill all persistent jobs if window-status-format has
! 176: * changed. Persistent jobs are only used by the status line at the
! 177: * moment so this works XXX.
! 178: */
! 179: if (strcmp(entry->name, "window-status-format") == 0) {
! 180: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 181: c = ARRAY_ITEM(&clients, i);
! 182: if (c == NULL || c->session == NULL)
! 183: continue;
! 184:
! 185: jobs = &c->status_jobs;
! 186: do {
! 187: try_again = 0;
! 188: job = RB_ROOT(jobs);
! 189: while (job != NULL) {
! 190: nextjob = RB_NEXT(jobs, jobs, job);
! 191: if (job->flags & JOB_PERSIST) {
! 192: job_remove(jobs, job);
! 193: try_again = 1;
! 194: break;
! 195: }
! 196: job = nextjob;
! 197: }
! 198: } while (try_again);
! 199: server_redraw_client(c);
! 200: }
1.1 nicm 201: }
202:
203: return (0);
204: }