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