Annotation of src/usr.bin/tmux/key-bindings.c, Revision 1.44
1.44 ! nicm 1: /* $OpenBSD: key-bindings.c,v 1.43 2014/10/22 23:18:53 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.1 nicm 28:
29: struct key_bindings key_bindings;
30:
31: int
32: key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
33: {
1.8 nicm 34: int key1, key2;
35:
36: key1 = bd1->key & ~KEYC_PREFIX;
37: key2 = bd2->key & ~KEYC_PREFIX;
38: if (key1 != key2)
39: return (key1 - key2);
40:
41: if (bd1->key & KEYC_PREFIX && !(bd2->key & KEYC_PREFIX))
42: return (-1);
43: if (bd2->key & KEYC_PREFIX && !(bd1->key & KEYC_PREFIX))
44: return (1);
45: return (0);
1.1 nicm 46: }
47:
48: struct key_binding *
49: key_bindings_lookup(int key)
50: {
51: struct key_binding bd;
52:
53: bd.key = key;
1.29 nicm 54: return (RB_FIND(key_bindings, &key_bindings, &bd));
1.1 nicm 55: }
56:
57: void
58: key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist)
59: {
60: struct key_binding *bd;
61:
1.2 nicm 62: key_bindings_remove(key);
1.15 nicm 63:
1.2 nicm 64: bd = xmalloc(sizeof *bd);
65: bd->key = key;
1.29 nicm 66: RB_INSERT(key_bindings, &key_bindings, bd);
1.15 nicm 67:
1.1 nicm 68: bd->can_repeat = can_repeat;
69: bd->cmdlist = cmdlist;
70: }
71:
72: void
73: key_bindings_remove(int key)
74: {
75: struct key_binding *bd;
76:
77: if ((bd = key_bindings_lookup(key)) == NULL)
78: return;
1.29 nicm 79: RB_REMOVE(key_bindings, &key_bindings, bd);
1.41 nicm 80: cmd_list_free(bd->cmdlist);
81: free(bd);
1.1 nicm 82: }
83:
84: void
85: key_bindings_init(void)
86: {
1.42 nicm 87: static const char* defaults[] = {
88: "bind C-b send-prefix",
89: "bind C-o rotate-window",
90: "bind C-z suspend-client",
91: "bind Space next-layout",
92: "bind ! break-pane",
93: "bind '\"' split-window",
94: "bind '#' list-buffers",
95: "bind '$' command-prompt -I'#S' \"rename-session '%%'\"",
96: "bind % split-window -h",
97: "bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
98: "bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"",
99: "bind ( switch-client -p",
100: "bind ) switch-client -n",
101: "bind , command-prompt -I'#W' \"rename-window '%%'\"",
102: "bind - delete-buffer",
103: "bind . command-prompt \"move-window -t '%%'\"",
104: "bind 0 select-window -t:0",
105: "bind 1 select-window -t:1",
106: "bind 2 select-window -t:2",
107: "bind 3 select-window -t:3",
108: "bind 4 select-window -t:4",
109: "bind 5 select-window -t:5",
110: "bind 6 select-window -t:6",
111: "bind 7 select-window -t:7",
112: "bind 8 select-window -t:8",
113: "bind 9 select-window -t:9",
114: "bind : command-prompt",
115: "bind \\; last-pane",
116: "bind = choose-buffer",
117: "bind ? list-keys",
118: "bind D choose-client",
119: "bind L switch-client -l",
120: "bind [ copy-mode",
121: "bind ] paste-buffer",
122: "bind c new-window",
123: "bind d detach-client",
124: "bind f command-prompt \"find-window '%%'\"",
125: "bind i display-message",
126: "bind l last-window",
127: "bind n next-window",
128: "bind o select-pane -t:.+",
129: "bind p previous-window",
130: "bind q display-panes",
131: "bind r refresh-client",
132: "bind s choose-tree",
133: "bind t clock-mode",
134: "bind w choose-window",
135: "bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
136: "bind z resize-pane -Z",
137: "bind { swap-pane -U",
138: "bind } swap-pane -D",
139: "bind '~' show-messages",
140: "bind PPage copy-mode -u",
141: "bind -r Up select-pane -U",
142: "bind -r Down select-pane -D",
143: "bind -r Left select-pane -L",
144: "bind -r Right select-pane -R",
145: "bind M-1 select-layout even-horizontal",
146: "bind M-2 select-layout even-vertical",
147: "bind M-3 select-layout main-horizontal",
148: "bind M-4 select-layout main-vertical",
149: "bind M-5 select-layout tiled",
150: "bind M-n next-window -a",
151: "bind M-o rotate-window -D",
152: "bind M-p previous-window -a",
153: "bind -r M-Up resize-pane -U 5",
154: "bind -r M-Down resize-pane -D 5",
155: "bind -r M-Left resize-pane -L 5",
156: "bind -r M-Right resize-pane -R 5",
157: "bind -r C-Up resize-pane -U",
158: "bind -r C-Down resize-pane -D",
159: "bind -r C-Left resize-pane -L",
160: "bind -r C-Right resize-pane -R",
1.44 ! nicm 161: "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M",
! 162: "bind -n MouseDrag1Border resize-pane -M",
! 163: "bind -n MouseDown1Status select-window -t=",
! 164: "bind -n MouseDrag1Pane copy-mode -M",
1.1 nicm 165: };
166: u_int i;
167: struct cmd_list *cmdlist;
1.42 nicm 168: char* cause;
169: int error;
170: struct cmd_q *cmdq;
1.1 nicm 171:
1.29 nicm 172: RB_INIT(&key_bindings);
1.1 nicm 173:
1.43 nicm 174: cmdq = cmdq_new(NULL);
1.42 nicm 175: for (i = 0; i < nitems(defaults); i++) {
176: error = cmd_string_parse(defaults[i], &cmdlist,
177: "<default-keys>", i, &cause);
178: if (error != 0)
179: fatalx("bad default key");
1.44 ! nicm 180: cmdq_run(cmdq, cmdlist, NULL);
! 181: cmd_list_free (cmdlist);
1.1 nicm 182: }
1.43 nicm 183: cmdq_free(cmdq);
1.1 nicm 184: }
185:
186: void
1.44 ! nicm 187: key_bindings_dispatch(struct key_binding *bd, struct client *c,
! 188: struct mouse_event *m)
1.1 nicm 189: {
1.17 nicm 190: struct cmd *cmd;
191: int readonly;
1.1 nicm 192:
1.17 nicm 193: readonly = 1;
1.23 nicm 194: TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
1.17 nicm 195: if (!(cmd->entry->flags & CMD_READONLY))
196: readonly = 0;
197: }
1.38 nicm 198: if (!readonly && (c->flags & CLIENT_READONLY)) {
1.40 nicm 199: cmdq_error(c->cmdq, "client is read-only");
1.17 nicm 200: return;
201: }
1.1 nicm 202:
1.44 ! nicm 203: cmdq_run(c->cmdq, bd->cmdlist, m);
1.1 nicm 204: }