xref: /openbsd-src/usr.bin/tmux/key-bindings.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: key-bindings.c,v 1.89 2019/04/29 06:55:21 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 static int key_bindings_cmp(struct key_binding *, struct key_binding *);
28 RB_GENERATE_STATIC(key_bindings, key_binding, entry, key_bindings_cmp);
29 static int key_table_cmp(struct key_table *, struct key_table *);
30 RB_GENERATE_STATIC(key_tables, key_table, entry, key_table_cmp);
31 static struct key_tables key_tables = RB_INITIALIZER(&key_tables);
32 
33 static int
34 key_table_cmp(struct key_table *table1, struct key_table *table2)
35 {
36 	return (strcmp(table1->name, table2->name));
37 }
38 
39 static int
40 key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
41 {
42 	if (bd1->key < bd2->key)
43 		return (-1);
44 	if (bd1->key > bd2->key)
45 		return (1);
46 	return (0);
47 }
48 
49 struct key_table *
50 key_bindings_get_table(const char *name, int create)
51 {
52 	struct key_table	table_find, *table;
53 
54 	table_find.name = name;
55 	table = RB_FIND(key_tables, &key_tables, &table_find);
56 	if (table != NULL || !create)
57 		return (table);
58 
59 	table = xmalloc(sizeof *table);
60 	table->name = xstrdup(name);
61 	RB_INIT(&table->key_bindings);
62 
63 	table->references = 1; /* one reference in key_tables */
64 	RB_INSERT(key_tables, &key_tables, table);
65 
66 	return (table);
67 }
68 
69 struct key_table *
70 key_bindings_first_table(void)
71 {
72 	return (RB_MIN(key_tables, &key_tables));
73 }
74 
75 struct key_table *
76 key_bindings_next_table(struct key_table *table)
77 {
78 	return (RB_NEXT(key_tables, &key_tables, table));
79 }
80 
81 void
82 key_bindings_unref_table(struct key_table *table)
83 {
84 	struct key_binding	*bd;
85 	struct key_binding	*bd1;
86 
87 	if (--table->references != 0)
88 		return;
89 
90 	RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) {
91 		RB_REMOVE(key_bindings, &table->key_bindings, bd);
92 		cmd_list_free(bd->cmdlist);
93 		free(bd);
94 	}
95 
96 	free((void *)table->name);
97 	free(table);
98 }
99 
100 struct key_binding *
101 key_bindings_get(struct key_table *table, key_code key)
102 {
103 	struct key_binding	bd;
104 
105 	bd.key = key;
106 	return (RB_FIND(key_bindings, &table->key_bindings, &bd));
107 }
108 
109 struct key_binding *
110 key_bindings_first(struct key_table *table)
111 {
112 	return (RB_MIN(key_bindings, &table->key_bindings));
113 }
114 
115 struct key_binding *
116 key_bindings_next(__unused struct key_table *table, struct key_binding *bd)
117 {
118 	return (RB_NEXT(key_bindings, &table->key_bindings, bd));
119 }
120 
121 void
122 key_bindings_add(const char *name, key_code key, int repeat,
123     struct cmd_list *cmdlist)
124 {
125 	struct key_table	*table;
126 	struct key_binding	 bd_find, *bd;
127 
128 	table = key_bindings_get_table(name, 1);
129 
130 	bd_find.key = (key & ~KEYC_XTERM);
131 	bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
132 	if (bd != NULL) {
133 		RB_REMOVE(key_bindings, &table->key_bindings, bd);
134 		cmd_list_free(bd->cmdlist);
135 		free(bd);
136 	}
137 
138 	bd = xcalloc(1, sizeof *bd);
139 	bd->key = key;
140 	RB_INSERT(key_bindings, &table->key_bindings, bd);
141 
142 	if (repeat)
143 		bd->flags |= KEY_BINDING_REPEAT;
144 	bd->cmdlist = cmdlist;
145 }
146 
147 void
148 key_bindings_remove(const char *name, key_code key)
149 {
150 	struct key_table	*table;
151 	struct key_binding	 bd_find, *bd;
152 
153 	table = key_bindings_get_table(name, 0);
154 	if (table == NULL)
155 		return;
156 
157 	bd_find.key = (key & ~KEYC_XTERM);
158 	bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
159 	if (bd == NULL)
160 		return;
161 
162 	RB_REMOVE(key_bindings, &table->key_bindings, bd);
163 	cmd_list_free(bd->cmdlist);
164 	free(bd);
165 
166 	if (RB_EMPTY(&table->key_bindings)) {
167 		RB_REMOVE(key_tables, &key_tables, table);
168 		key_bindings_unref_table(table);
169 	}
170 }
171 
172 void
173 key_bindings_remove_table(const char *name)
174 {
175 	struct key_table	*table;
176 	struct client		*c;
177 
178 	table = key_bindings_get_table(name, 0);
179 	if (table != NULL) {
180 		RB_REMOVE(key_tables, &key_tables, table);
181 		key_bindings_unref_table(table);
182 	}
183 	TAILQ_FOREACH(c, &clients, entry) {
184 		if (c->keytable == table)
185 			server_client_set_key_table(c, NULL);
186 	}
187 }
188 
189 void
190 key_bindings_init(void)
191 {
192 	static const char *defaults[] = {
193 		"bind C-b send-prefix",
194 		"bind C-o rotate-window",
195 		"bind C-z suspend-client",
196 		"bind Space next-layout",
197 		"bind ! break-pane",
198 		"bind '\"' split-window",
199 		"bind '#' list-buffers",
200 		"bind '$' command-prompt -I'#S' \"rename-session -- '%%'\"",
201 		"bind % split-window -h",
202 		"bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
203 		"bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"",
204 		"bind ( switch-client -p",
205 		"bind ) switch-client -n",
206 		"bind , command-prompt -I'#W' \"rename-window -- '%%'\"",
207 		"bind - delete-buffer",
208 		"bind . command-prompt \"move-window -t '%%'\"",
209 		"bind 0 select-window -t:=0",
210 		"bind 1 select-window -t:=1",
211 		"bind 2 select-window -t:=2",
212 		"bind 3 select-window -t:=3",
213 		"bind 4 select-window -t:=4",
214 		"bind 5 select-window -t:=5",
215 		"bind 6 select-window -t:=6",
216 		"bind 7 select-window -t:=7",
217 		"bind 8 select-window -t:=8",
218 		"bind 9 select-window -t:=9",
219 		"bind : command-prompt",
220 		"bind \\; last-pane",
221 		"bind = choose-buffer -Z",
222 		"bind ? list-keys",
223 		"bind D choose-client -Z",
224 		"bind E select-layout -E",
225 		"bind L switch-client -l",
226 		"bind M select-pane -M",
227 		"bind [ copy-mode",
228 		"bind ] paste-buffer",
229 		"bind c new-window",
230 		"bind d detach-client",
231 		"bind f command-prompt \"find-window -Z -- '%%'\"",
232 		"bind i display-message",
233 		"bind l last-window",
234 		"bind m select-pane -m",
235 		"bind n next-window",
236 		"bind o select-pane -t:.+",
237 		"bind p previous-window",
238 		"bind q display-panes",
239 		"bind r refresh-client",
240 		"bind s choose-tree -Zs",
241 		"bind t clock-mode",
242 		"bind w choose-tree -Zw",
243 		"bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
244 		"bind z resize-pane -Z",
245 		"bind { swap-pane -U",
246 		"bind } swap-pane -D",
247 		"bind '~' show-messages",
248 		"bind PPage copy-mode -u",
249 		"bind -r Up select-pane -U",
250 		"bind -r Down select-pane -D",
251 		"bind -r Left select-pane -L",
252 		"bind -r Right select-pane -R",
253 		"bind M-1 select-layout even-horizontal",
254 		"bind M-2 select-layout even-vertical",
255 		"bind M-3 select-layout main-horizontal",
256 		"bind M-4 select-layout main-vertical",
257 		"bind M-5 select-layout tiled",
258 		"bind M-n next-window -a",
259 		"bind M-o rotate-window -D",
260 		"bind M-p previous-window -a",
261 		"bind -r S-Up refresh-client -U 10",
262 		"bind -r S-Down refresh-client -D 10",
263 		"bind -r S-Left refresh-client -L 10",
264 		"bind -r S-Right refresh-client -R 10",
265 		"bind -r DC refresh-client -c",
266 		"bind -r M-Up resize-pane -U 5",
267 		"bind -r M-Down resize-pane -D 5",
268 		"bind -r M-Left resize-pane -L 5",
269 		"bind -r M-Right resize-pane -R 5",
270 		"bind -r C-Up resize-pane -U",
271 		"bind -r C-Down resize-pane -D",
272 		"bind -r C-Left resize-pane -L",
273 		"bind -r C-Right resize-pane -R",
274 		"bind -n MouseDown1Pane select-pane -t=\\; send-keys -M",
275 		"bind -n MouseDrag1Border resize-pane -M",
276 		"bind -n MouseDown1Status select-window -t=",
277 		"bind -n WheelDownStatus next-window",
278 		"bind -n WheelUpStatus previous-window",
279 		"bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'",
280 		"bind -n MouseDown3Pane if-shell -Ft= '#{mouse_any_flag}' 'select-pane -t=; send-keys -M' 'select-pane -mt='",
281 		"bind -n WheelUpPane if-shell -Ft= '#{mouse_any_flag}' 'send-keys -M' 'if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -et=\"'",
282 
283 		"bind -Tcopy-mode C-Space send -X begin-selection",
284 		"bind -Tcopy-mode C-a send -X start-of-line",
285 		"bind -Tcopy-mode C-c send -X cancel",
286 		"bind -Tcopy-mode C-e send -X end-of-line",
287 		"bind -Tcopy-mode C-f send -X cursor-right",
288 		"bind -Tcopy-mode C-b send -X cursor-left",
289 		"bind -Tcopy-mode C-g send -X clear-selection",
290 		"bind -Tcopy-mode C-k send -X copy-end-of-line",
291 		"bind -Tcopy-mode C-n send -X cursor-down",
292 		"bind -Tcopy-mode C-p send -X cursor-up",
293 		"bind -Tcopy-mode C-r command-prompt -ip'(search up)' -I'#{pane_search_string}' 'send -X search-backward-incremental \"%%%\"'",
294 		"bind -Tcopy-mode C-s command-prompt -ip'(search down)' -I'#{pane_search_string}' 'send -X search-forward-incremental \"%%%\"'",
295 		"bind -Tcopy-mode C-v send -X page-down",
296 		"bind -Tcopy-mode C-w send -X copy-selection-and-cancel",
297 		"bind -Tcopy-mode Escape send -X cancel",
298 		"bind -Tcopy-mode Space send -X page-down",
299 		"bind -Tcopy-mode , send -X jump-reverse",
300 		"bind -Tcopy-mode \\; send -X jump-again",
301 		"bind -Tcopy-mode F command-prompt -1p'(jump backward)' 'send -X jump-backward \"%%%\"'",
302 		"bind -Tcopy-mode N send -X search-reverse",
303 		"bind -Tcopy-mode R send -X rectangle-toggle",
304 		"bind -Tcopy-mode T command-prompt -1p'(jump to backward)' 'send -X jump-to-backward \"%%%\"'",
305 		"bind -Tcopy-mode f command-prompt -1p'(jump forward)' 'send -X jump-forward \"%%%\"'",
306 		"bind -Tcopy-mode g command-prompt -p'(goto line)' 'send -X goto-line \"%%%\"'",
307 		"bind -Tcopy-mode n send -X search-again",
308 		"bind -Tcopy-mode q send -X cancel",
309 		"bind -Tcopy-mode t command-prompt -1p'(jump to forward)' 'send -X jump-to-forward \"%%%\"'",
310 		"bind -Tcopy-mode Home send -X start-of-line",
311 		"bind -Tcopy-mode End send -X end-of-line",
312 		"bind -Tcopy-mode MouseDown1Pane select-pane",
313 		"bind -Tcopy-mode MouseDrag1Pane select-pane\\; send -X begin-selection",
314 		"bind -Tcopy-mode MouseDragEnd1Pane send -X copy-selection-and-cancel",
315 		"bind -Tcopy-mode WheelUpPane select-pane\\; send -N5 -X scroll-up",
316 		"bind -Tcopy-mode WheelDownPane select-pane\\; send -N5 -X scroll-down",
317 		"bind -Tcopy-mode DoubleClick1Pane select-pane\\; send -X select-word",
318 		"bind -Tcopy-mode TripleClick1Pane select-pane\\; send -X select-line",
319 		"bind -Tcopy-mode NPage send -X page-down",
320 		"bind -Tcopy-mode PPage send -X page-up",
321 		"bind -Tcopy-mode Up send -X cursor-up",
322 		"bind -Tcopy-mode Down send -X cursor-down",
323 		"bind -Tcopy-mode Left send -X cursor-left",
324 		"bind -Tcopy-mode Right send -X cursor-right",
325 		"bind -Tcopy-mode M-1 command-prompt -Np'(repeat)' -I1 'send -N \"%%%\"'",
326 		"bind -Tcopy-mode M-2 command-prompt -Np'(repeat)' -I2 'send -N \"%%%\"'",
327 		"bind -Tcopy-mode M-3 command-prompt -Np'(repeat)' -I3 'send -N \"%%%\"'",
328 		"bind -Tcopy-mode M-4 command-prompt -Np'(repeat)' -I4 'send -N \"%%%\"'",
329 		"bind -Tcopy-mode M-5 command-prompt -Np'(repeat)' -I5 'send -N \"%%%\"'",
330 		"bind -Tcopy-mode M-6 command-prompt -Np'(repeat)' -I6 'send -N \"%%%\"'",
331 		"bind -Tcopy-mode M-7 command-prompt -Np'(repeat)' -I7 'send -N \"%%%\"'",
332 		"bind -Tcopy-mode M-8 command-prompt -Np'(repeat)' -I8 'send -N \"%%%\"'",
333 		"bind -Tcopy-mode M-9 command-prompt -Np'(repeat)' -I9 'send -N \"%%%\"'",
334 		"bind -Tcopy-mode M-< send -X history-top",
335 		"bind -Tcopy-mode M-> send -X history-bottom",
336 		"bind -Tcopy-mode M-R send -X top-line",
337 		"bind -Tcopy-mode M-b send -X previous-word",
338 		"bind -Tcopy-mode C-M-b send -X previous-matching-bracket",
339 		"bind -Tcopy-mode M-f send -X next-word-end",
340 		"bind -Tcopy-mode C-M-f send -X next-matching-bracket",
341 		"bind -Tcopy-mode M-m send -X back-to-indentation",
342 		"bind -Tcopy-mode M-r send -X middle-line",
343 		"bind -Tcopy-mode M-v send -X page-up",
344 		"bind -Tcopy-mode M-w send -X copy-selection-and-cancel",
345 		"bind -Tcopy-mode M-{ send -X previous-paragraph",
346 		"bind -Tcopy-mode M-} send -X next-paragraph",
347 		"bind -Tcopy-mode M-Up send -X halfpage-up",
348 		"bind -Tcopy-mode M-Down send -X halfpage-down",
349 		"bind -Tcopy-mode C-Up send -X scroll-up",
350 		"bind -Tcopy-mode C-Down send -X scroll-down",
351 
352 		"bind -Tcopy-mode-vi C-c send -X cancel",
353 		"bind -Tcopy-mode-vi C-d send -X halfpage-down",
354 		"bind -Tcopy-mode-vi C-e send -X scroll-down",
355 		"bind -Tcopy-mode-vi C-b send -X page-up",
356 		"bind -Tcopy-mode-vi C-f send -X page-down",
357 		"bind -Tcopy-mode-vi C-h send -X cursor-left",
358 		"bind -Tcopy-mode-vi C-j send -X copy-selection-and-cancel",
359 		"bind -Tcopy-mode-vi Enter send -X copy-selection-and-cancel",
360 		"bind -Tcopy-mode-vi C-u send -X halfpage-up",
361 		"bind -Tcopy-mode-vi C-v send -X rectangle-toggle",
362 		"bind -Tcopy-mode-vi C-y send -X scroll-up",
363 		"bind -Tcopy-mode-vi Escape send -X clear-selection",
364 		"bind -Tcopy-mode-vi Space send -X begin-selection",
365 		"bind -Tcopy-mode-vi '$' send -X end-of-line",
366 		"bind -Tcopy-mode-vi , send -X jump-reverse",
367 		"bind -Tcopy-mode-vi / command-prompt -p'(search down)' 'send -X search-forward \"%%%\"'",
368 		"bind -Tcopy-mode-vi 0 send -X start-of-line",
369 		"bind -Tcopy-mode-vi 1 command-prompt -Np'(repeat)' -I1 'send -N \"%%%\"'",
370 		"bind -Tcopy-mode-vi 2 command-prompt -Np'(repeat)' -I2 'send -N \"%%%\"'",
371 		"bind -Tcopy-mode-vi 3 command-prompt -Np'(repeat)' -I3 'send -N \"%%%\"'",
372 		"bind -Tcopy-mode-vi 4 command-prompt -Np'(repeat)' -I4 'send -N \"%%%\"'",
373 		"bind -Tcopy-mode-vi 5 command-prompt -Np'(repeat)' -I5 'send -N \"%%%\"'",
374 		"bind -Tcopy-mode-vi 6 command-prompt -Np'(repeat)' -I6 'send -N \"%%%\"'",
375 		"bind -Tcopy-mode-vi 7 command-prompt -Np'(repeat)' -I7 'send -N \"%%%\"'",
376 		"bind -Tcopy-mode-vi 8 command-prompt -Np'(repeat)' -I8 'send -N \"%%%\"'",
377 		"bind -Tcopy-mode-vi 9 command-prompt -Np'(repeat)' -I9 'send -N \"%%%\"'",
378 		"bind -Tcopy-mode-vi : command-prompt -p'(goto line)' 'send -X goto-line \"%%%\"'",
379 		"bind -Tcopy-mode-vi \\; send -X jump-again",
380 		"bind -Tcopy-mode-vi ? command-prompt -p'(search up)' 'send -X search-backward \"%%%\"'",
381 		"bind -Tcopy-mode-vi A send -X append-selection-and-cancel",
382 		"bind -Tcopy-mode-vi B send -X previous-space",
383 		"bind -Tcopy-mode-vi D send -X copy-end-of-line",
384 		"bind -Tcopy-mode-vi E send -X next-space-end",
385 		"bind -Tcopy-mode-vi F command-prompt -1p'(jump backward)' 'send -X jump-backward \"%%%\"'",
386 		"bind -Tcopy-mode-vi G send -X history-bottom",
387 		"bind -Tcopy-mode-vi H send -X top-line",
388 		"bind -Tcopy-mode-vi J send -X scroll-down",
389 		"bind -Tcopy-mode-vi K send -X scroll-up",
390 		"bind -Tcopy-mode-vi L send -X bottom-line",
391 		"bind -Tcopy-mode-vi M send -X middle-line",
392 		"bind -Tcopy-mode-vi N send -X search-reverse",
393 		"bind -Tcopy-mode-vi T command-prompt -1p'(jump to backward)' 'send -X jump-to-backward \"%%%\"'",
394 		"bind -Tcopy-mode-vi V send -X select-line",
395 		"bind -Tcopy-mode-vi W send -X next-space",
396 		"bind -Tcopy-mode-vi ^ send -X back-to-indentation",
397 		"bind -Tcopy-mode-vi b send -X previous-word",
398 		"bind -Tcopy-mode-vi e send -X next-word-end",
399 		"bind -Tcopy-mode-vi f command-prompt -1p'(jump forward)' 'send -X jump-forward \"%%%\"'",
400 		"bind -Tcopy-mode-vi g send -X history-top",
401 		"bind -Tcopy-mode-vi h send -X cursor-left",
402 		"bind -Tcopy-mode-vi j send -X cursor-down",
403 		"bind -Tcopy-mode-vi k send -X cursor-up",
404 		"bind -Tcopy-mode-vi l send -X cursor-right",
405 		"bind -Tcopy-mode-vi n send -X search-again",
406 		"bind -Tcopy-mode-vi o send -X other-end",
407 		"bind -Tcopy-mode-vi q send -X cancel",
408 		"bind -Tcopy-mode-vi t command-prompt -1p'(jump to forward)' 'send -X jump-to-forward \"%%%\"'",
409 		"bind -Tcopy-mode-vi v send -X rectangle-toggle",
410 		"bind -Tcopy-mode-vi w send -X next-word",
411 		"bind -Tcopy-mode-vi { send -X previous-paragraph",
412 		"bind -Tcopy-mode-vi } send -X next-paragraph",
413 		"bind -Tcopy-mode-vi % send -X next-matching-bracket",
414 		"bind -Tcopy-mode-vi MouseDown1Pane select-pane",
415 		"bind -Tcopy-mode-vi MouseDrag1Pane select-pane\\; send -X begin-selection",
416 		"bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-selection-and-cancel",
417 		"bind -Tcopy-mode-vi WheelUpPane select-pane\\; send -N5 -X scroll-up",
418 		"bind -Tcopy-mode-vi WheelDownPane select-pane\\; send -N5 -X scroll-down",
419 		"bind -Tcopy-mode-vi DoubleClick1Pane select-pane\\; send -X select-word",
420 		"bind -Tcopy-mode-vi TripleClick1Pane select-pane\\; send -X select-line",
421 		"bind -Tcopy-mode-vi BSpace send -X cursor-left",
422 		"bind -Tcopy-mode-vi NPage send -X page-down",
423 		"bind -Tcopy-mode-vi PPage send -X page-up",
424 		"bind -Tcopy-mode-vi Up send -X cursor-up",
425 		"bind -Tcopy-mode-vi Down send -X cursor-down",
426 		"bind -Tcopy-mode-vi Left send -X cursor-left",
427 		"bind -Tcopy-mode-vi Right send -X cursor-right",
428 		"bind -Tcopy-mode-vi C-Up send -X scroll-up",
429 		"bind -Tcopy-mode-vi C-Down send -X scroll-down",
430 	};
431 	u_int		 i;
432 	struct cmd_list	*cmdlist;
433 	char		*cause;
434 
435 	for (i = 0; i < nitems(defaults); i++) {
436 		cmdlist = cmd_string_parse(defaults[i], "<default>", i, &cause);
437 		if (cmdlist == NULL)
438 			fatalx("bad default key: %s", defaults[i]);
439 		cmdq_append(NULL, cmdq_get_command(cmdlist, NULL, NULL, 0));
440 		cmd_list_free(cmdlist);
441 	}
442 }
443 
444 static enum cmd_retval
445 key_bindings_read_only(struct cmdq_item *item, __unused void *data)
446 {
447 	cmdq_error(item, "client is read-only");
448 	return (CMD_RETURN_ERROR);
449 }
450 
451 void
452 key_bindings_dispatch(struct key_binding *bd, struct cmdq_item *item,
453     struct client *c, struct mouse_event *m, struct cmd_find_state *fs)
454 {
455 	struct cmd		*cmd;
456 	struct cmdq_item	*new_item;
457 	int			 readonly;
458 
459 	readonly = 1;
460 	TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
461 		if (!(cmd->entry->flags & CMD_READONLY))
462 			readonly = 0;
463 	}
464 	if (!readonly && (c->flags & CLIENT_READONLY))
465 		new_item = cmdq_get_callback(key_bindings_read_only, NULL);
466 	else {
467 		new_item = cmdq_get_command(bd->cmdlist, fs, m, 0);
468 		if (bd->flags & KEY_BINDING_REPEAT)
469 			new_item->shared->flags |= CMDQ_SHARED_REPEAT;
470 	}
471 	if (item != NULL)
472 		cmdq_insert_after(item, new_item);
473 	else
474 		cmdq_append(c, new_item);
475 }
476