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