Annotation of src/usr.bin/tmux/cmd-set-window-option.c, Revision 1.12
1.12 ! nicm 1: /* $OpenBSD: cmd-set-window-option.c,v 1.11 2009/08/11 12:53:37 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]",
35: CMD_ARG12, CMD_CHFLAG('a')|CMD_CHFLAG('g')|CMD_CHFLAG('u'),
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 },
67: { "utf8", SET_OPTION_FLAG, 0, 0, NULL },
68: { "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
69: { "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
1.7 nicm 70: { "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
71: { "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
72: { "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.1 nicm 73: { "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
74: { "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
1.6 nicm 75: { NULL, 0, 0, 0, NULL }
1.1 nicm 76: };
77:
78: int
79: cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
80: {
1.11 nicm 81: struct cmd_target_data *data = self->data;
1.1 nicm 82: struct winlink *wl;
83: struct client *c;
84: struct options *oo;
1.6 nicm 85: const struct set_option_entry *entry, *opt;
1.1 nicm 86: u_int i;
87:
1.4 nicm 88: if (data->chflags & CMD_CHFLAG('g'))
1.3 nicm 89: oo = &global_w_options;
1.1 nicm 90: else {
91: if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
92: return (-1);
93: oo = &wl->window->options;
94: }
95:
1.11 nicm 96: if (*data->arg == '\0') {
1.1 nicm 97: ctx->error(ctx, "invalid option");
98: return (-1);
99: }
100:
101: entry = NULL;
1.6 nicm 102: for (opt = set_window_option_table; opt->name != NULL; opt++) {
1.11 nicm 103: if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
1.1 nicm 104: continue;
105: if (entry != NULL) {
1.11 nicm 106: ctx->error(ctx, "ambiguous option: %s", data->arg);
1.1 nicm 107: return (-1);
108: }
1.6 nicm 109: entry = opt;
1.1 nicm 110:
111: /* Bail now if an exact match. */
1.11 nicm 112: if (strcmp(entry->name, data->arg) == 0)
1.1 nicm 113: break;
114: }
115: if (entry == NULL) {
1.11 nicm 116: ctx->error(ctx, "unknown option: %s", data->arg);
1.1 nicm 117: return (-1);
118: }
119:
1.4 nicm 120: if (data->chflags & CMD_CHFLAG('u')) {
121: if (data->chflags & CMD_CHFLAG('g')) {
1.1 nicm 122: ctx->error(ctx,
123: "can't unset global option: %s", entry->name);
124: return (-1);
125: }
1.11 nicm 126: if (data->arg2 != NULL) {
1.1 nicm 127: ctx->error(ctx,
128: "value passed to unset option: %s", entry->name);
129: return (-1);
130: }
131:
132: options_remove(oo, entry->name);
133: ctx->info(ctx, "unset option: %s", entry->name);
134: } else {
135: switch (entry->type) {
136: case SET_OPTION_STRING:
1.10 nicm 137: set_option_string(ctx, oo, entry,
1.11 nicm 138: data->arg2, data->chflags & CMD_CHFLAG('a'));
1.1 nicm 139: break;
140: case SET_OPTION_NUMBER:
1.11 nicm 141: set_option_number(ctx, oo, entry, data->arg2);
1.1 nicm 142: break;
1.12 ! nicm 143: case SET_OPTION_KEYS:
! 144: set_option_keys(ctx, oo, entry, data->arg2);
1.1 nicm 145: break;
146: case SET_OPTION_COLOUR:
1.11 nicm 147: set_option_colour(ctx, oo, entry, data->arg2);
1.1 nicm 148: break;
149: case SET_OPTION_ATTRIBUTES:
1.11 nicm 150: set_option_attributes(ctx, oo, entry, data->arg2);
1.1 nicm 151: break;
152: case SET_OPTION_FLAG:
1.11 nicm 153: set_option_flag(ctx, oo, entry, data->arg2);
1.1 nicm 154: break;
155: case SET_OPTION_CHOICE:
1.11 nicm 156: set_option_choice(ctx, oo, entry, data->arg2);
1.1 nicm 157: break;
158: }
159: }
160:
161: recalculate_sizes();
162: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
163: c = ARRAY_ITEM(&clients, i);
164: if (c != NULL && c->session != NULL)
165: server_redraw_client(c);
166: }
167:
168: return (0);
169: }