Annotation of src/usr.bin/tmux/key-bindings.c, Revision 1.52
1.52 ! nicm 1: /* $OpenBSD: key-bindings.c,v 1.51 2015/10/21 11:13:47 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 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 <ctype.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
1.29 nicm 27: RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
1.45 nicm 28: RB_GENERATE(key_tables, key_table, entry, key_table_cmp);
29: struct key_tables key_tables = RB_INITIALIZER(&key_tables);
1.1 nicm 30:
1.45 nicm 31: int
32: key_table_cmp(struct key_table *e1, struct key_table *e2)
33: {
34: return (strcmp(e1->name, e2->name));
35: }
1.1 nicm 36:
37: int
38: key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
39: {
1.45 nicm 40: return (bd1->key - bd2->key);
41: }
42:
43: struct key_table *
44: key_bindings_get_table(const char *name, int create)
45: {
46: struct key_table table_find, *table;
47:
48: table_find.name = name;
49: table = RB_FIND(key_tables, &key_tables, &table_find);
50: if (table != NULL || !create)
51: return (table);
1.8 nicm 52:
1.45 nicm 53: table = xmalloc(sizeof *table);
54: table->name = xstrdup(name);
55: RB_INIT(&table->key_bindings);
1.8 nicm 56:
1.45 nicm 57: table->references = 1; /* one reference in key_tables */
58: RB_INSERT(key_tables, &key_tables, table);
59:
60: return (table);
1.1 nicm 61: }
62:
1.45 nicm 63: void
64: key_bindings_unref_table(struct key_table *table)
1.1 nicm 65: {
1.45 nicm 66: struct key_binding *bd;
67:
68: if (--table->references != 0)
69: return;
70:
71: while (!RB_EMPTY(&table->key_bindings)) {
72: bd = RB_ROOT(&table->key_bindings);
73: RB_REMOVE(key_bindings, &table->key_bindings, bd);
74: cmd_list_free(bd->cmdlist);
75: free(bd);
76: }
1.1 nicm 77:
1.45 nicm 78: free((void *)table->name);
79: free(table);
1.1 nicm 80: }
81:
82: void
1.45 nicm 83: key_bindings_add(const char *name, int key, int can_repeat,
84: struct cmd_list *cmdlist)
1.1 nicm 85: {
1.45 nicm 86: struct key_table *table;
87: struct key_binding bd_find, *bd;
88:
89: table = key_bindings_get_table(name, 1);
1.1 nicm 90:
1.45 nicm 91: bd_find.key = key;
92: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
93: if (bd != NULL) {
94: RB_REMOVE(key_bindings, &table->key_bindings, bd);
95: cmd_list_free(bd->cmdlist);
96: free(bd);
97: }
1.15 nicm 98:
1.2 nicm 99: bd = xmalloc(sizeof *bd);
100: bd->key = key;
1.45 nicm 101: RB_INSERT(key_bindings, &table->key_bindings, bd);
1.15 nicm 102:
1.1 nicm 103: bd->can_repeat = can_repeat;
104: bd->cmdlist = cmdlist;
105: }
106:
107: void
1.45 nicm 108: key_bindings_remove(const char *name, int key)
1.1 nicm 109: {
1.45 nicm 110: struct key_table *table;
111: struct key_binding bd_find, *bd;
112:
113: table = key_bindings_get_table(name, 0);
114: if (table == NULL)
115: return;
1.1 nicm 116:
1.45 nicm 117: bd_find.key = key;
118: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
119: if (bd == NULL)
1.1 nicm 120: return;
1.45 nicm 121:
122: RB_REMOVE(key_bindings, &table->key_bindings, bd);
1.41 nicm 123: cmd_list_free(bd->cmdlist);
124: free(bd);
1.45 nicm 125:
126: if (RB_EMPTY(&table->key_bindings)) {
127: RB_REMOVE(key_tables, &key_tables, table);
128: key_bindings_unref_table(table);
129: }
130: }
131:
132: void
133: key_bindings_remove_table(const char *name)
134: {
135: struct key_table *table;
136:
137: table = key_bindings_get_table(name, 0);
138: if (table != NULL) {
139: RB_REMOVE(key_tables, &key_tables, table);
140: key_bindings_unref_table(table);
141: }
1.1 nicm 142: }
143:
144: void
145: key_bindings_init(void)
146: {
1.47 nicm 147: static const char *defaults[] = {
1.42 nicm 148: "bind C-b send-prefix",
149: "bind C-o rotate-window",
150: "bind C-z suspend-client",
151: "bind Space next-layout",
152: "bind ! break-pane",
153: "bind '\"' split-window",
154: "bind '#' list-buffers",
155: "bind '$' command-prompt -I'#S' \"rename-session '%%'\"",
156: "bind % split-window -h",
157: "bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
158: "bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"",
159: "bind ( switch-client -p",
160: "bind ) switch-client -n",
161: "bind , command-prompt -I'#W' \"rename-window '%%'\"",
162: "bind - delete-buffer",
163: "bind . command-prompt \"move-window -t '%%'\"",
1.50 nicm 164: "bind 0 select-window -t:=0",
165: "bind 1 select-window -t:=1",
166: "bind 2 select-window -t:=2",
167: "bind 3 select-window -t:=3",
168: "bind 4 select-window -t:=4",
169: "bind 5 select-window -t:=5",
170: "bind 6 select-window -t:=6",
171: "bind 7 select-window -t:=7",
172: "bind 8 select-window -t:=8",
173: "bind 9 select-window -t:=9",
1.42 nicm 174: "bind : command-prompt",
175: "bind \\; last-pane",
176: "bind = choose-buffer",
177: "bind ? list-keys",
178: "bind D choose-client",
179: "bind L switch-client -l",
1.49 nicm 180: "bind M select-pane -M",
1.42 nicm 181: "bind [ copy-mode",
182: "bind ] paste-buffer",
183: "bind c new-window",
184: "bind d detach-client",
185: "bind f command-prompt \"find-window '%%'\"",
186: "bind i display-message",
187: "bind l last-window",
1.49 nicm 188: "bind m select-pane -m",
1.42 nicm 189: "bind n next-window",
190: "bind o select-pane -t:.+",
191: "bind p previous-window",
192: "bind q display-panes",
193: "bind r refresh-client",
194: "bind s choose-tree",
195: "bind t clock-mode",
196: "bind w choose-window",
197: "bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
198: "bind z resize-pane -Z",
199: "bind { swap-pane -U",
200: "bind } swap-pane -D",
201: "bind '~' show-messages",
202: "bind PPage copy-mode -u",
203: "bind -r Up select-pane -U",
204: "bind -r Down select-pane -D",
205: "bind -r Left select-pane -L",
206: "bind -r Right select-pane -R",
207: "bind M-1 select-layout even-horizontal",
208: "bind M-2 select-layout even-vertical",
209: "bind M-3 select-layout main-horizontal",
210: "bind M-4 select-layout main-vertical",
211: "bind M-5 select-layout tiled",
212: "bind M-n next-window -a",
213: "bind M-o rotate-window -D",
214: "bind M-p previous-window -a",
215: "bind -r M-Up resize-pane -U 5",
216: "bind -r M-Down resize-pane -D 5",
217: "bind -r M-Left resize-pane -L 5",
218: "bind -r M-Right resize-pane -R 5",
219: "bind -r C-Up resize-pane -U",
220: "bind -r C-Down resize-pane -D",
221: "bind -r C-Left resize-pane -L",
222: "bind -r C-Right resize-pane -R",
1.44 nicm 223: "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M",
224: "bind -n MouseDrag1Border resize-pane -M",
225: "bind -n MouseDown1Status select-window -t=",
1.52 ! nicm 226: "bind -n WheelDownStatus next-window",
! 227: "bind -n WheelUpStatus previous-window",
1.46 nicm 228: "bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'",
1.49 nicm 229: "bind -n MouseDown3Pane select-pane -mt=",
1.51 nicm 230: "bind -n WheelUpPane if-shell -Ft= '#{mouse_any_flag}' 'send-keys -M' 'if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -e\"'",
1.1 nicm 231: };
232: u_int i;
233: struct cmd_list *cmdlist;
1.47 nicm 234: char *cause;
1.42 nicm 235: int error;
236: struct cmd_q *cmdq;
1.1 nicm 237:
1.43 nicm 238: cmdq = cmdq_new(NULL);
1.42 nicm 239: for (i = 0; i < nitems(defaults); i++) {
240: error = cmd_string_parse(defaults[i], &cmdlist,
241: "<default-keys>", i, &cause);
242: if (error != 0)
243: fatalx("bad default key");
1.44 nicm 244: cmdq_run(cmdq, cmdlist, NULL);
1.48 nicm 245: cmd_list_free(cmdlist);
1.1 nicm 246: }
1.43 nicm 247: cmdq_free(cmdq);
1.1 nicm 248: }
249:
250: void
1.44 nicm 251: key_bindings_dispatch(struct key_binding *bd, struct client *c,
252: struct mouse_event *m)
1.1 nicm 253: {
1.17 nicm 254: struct cmd *cmd;
255: int readonly;
1.1 nicm 256:
1.17 nicm 257: readonly = 1;
1.23 nicm 258: TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
1.17 nicm 259: if (!(cmd->entry->flags & CMD_READONLY))
260: readonly = 0;
261: }
1.38 nicm 262: if (!readonly && (c->flags & CLIENT_READONLY)) {
1.40 nicm 263: cmdq_error(c->cmdq, "client is read-only");
1.17 nicm 264: return;
265: }
1.1 nicm 266:
1.44 nicm 267: cmdq_run(c->cmdq, bd->cmdlist, m);
1.1 nicm 268: }