xref: /openbsd/usr.bin/tmux/key-bindings.c (revision 3cab2bb3)
1 /* $OpenBSD: key-bindings.c,v 1.130 2020/06/16 08:18:34 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_SESSION_MENU \
28 	" 'Next' 'n' {switch-client -n}" \
29 	" 'Previous' 'p' {switch-client -p}" \
30 	" ''" \
31 	" 'Renumber' 'N' {move-window -r}" \
32 	" 'Rename' 'n' {command-prompt -I \"#S\" \"rename-session -- '%%'\"}" \
33 	" ''" \
34 	" 'New Session' 's' {new-session}" \
35 	" 'New Window' 'w' {new-window}"
36 #define DEFAULT_WINDOW_MENU \
37 	" '#{?#{>:#{session_windows},1},,-}Swap Left' 'l' {swap-window -t:-1}" \
38 	" '#{?#{>:#{session_windows},1},,-}Swap Right' 'r' {swap-window -t:+1}" \
39 	" '#{?pane_marked_set,,-}Swap Marked' 's' {swap-window}" \
40 	" ''" \
41 	" 'Kill' 'X' {kill-window}" \
42 	" 'Respawn' 'R' {respawn-window -k}" \
43 	" '#{?pane_marked,Unmark,Mark}' 'm' {select-pane -m}" \
44 	" 'Rename' 'n' {command-prompt -I \"#W\" \"rename-window -- '%%'\"}" \
45 	" ''" \
46 	" 'New After' 'w' {new-window -a}" \
47 	" 'New At End' 'W' {new-window}"
48 #define DEFAULT_PANE_MENU \
49 	" '#{?#{m/r:(copy|view)-mode,#{pane_mode}},Go To Top,}' '<' {send -X history-top}" \
50 	" '#{?#{m/r:(copy|view)-mode,#{pane_mode}},Go To Bottom,}' '>' {send -X history-bottom}" \
51 	" ''" \
52 	" '#{?mouse_word,Search For #[underscore]#{=/9/...:mouse_word},}' 'C-r' {if -F '#{?#{m/r:(copy|view)-mode,#{pane_mode}},0,1}' 'copy-mode -t='; send -Xt= search-backward \"#{q:mouse_word}\"}" \
53 	" '#{?mouse_word,Type #[underscore]#{=/9/...:mouse_word},}' 'C-y' {copy-mode -q; send-keys -l -- \"#{q:mouse_word}\"}" \
54 	" '#{?mouse_word,Copy #[underscore]#{=/9/...:mouse_word},}' 'c' {copy-mode -q; set-buffer -- \"#{q:mouse_word}\"}" \
55 	" '#{?mouse_line,Copy Line,}' 'l' {copy-mode -q; set-buffer -- \"#{q:mouse_line}\"}" \
56 	" ''" \
57 	" 'Horizontal Split' 'h' {split-window -h}" \
58 	" 'Vertical Split' 'v' {split-window -v}" \
59 	" ''" \
60 	" '#{?#{>:#{window_panes},1},,-}Swap Up' 'u' {swap-pane -U}" \
61 	" '#{?#{>:#{window_panes},1},,-}Swap Down' 'd' {swap-pane -D}" \
62 	" '#{?pane_marked_set,,-}Swap Marked' 's' {swap-pane}" \
63 	" ''" \
64 	" 'Kill' 'X' {kill-pane}" \
65 	" 'Respawn' 'R' {respawn-pane -k}" \
66 	" '#{?pane_marked,Unmark,Mark}' 'm' {select-pane -m}" \
67 	" '#{?#{>:#{window_panes},1},,-}#{?window_zoomed_flag,Unzoom,Zoom}' 'z' {resize-pane -Z}"
68 
69 static int key_bindings_cmp(struct key_binding *, struct key_binding *);
70 RB_GENERATE_STATIC(key_bindings, key_binding, entry, key_bindings_cmp);
71 static int key_table_cmp(struct key_table *, struct key_table *);
72 RB_GENERATE_STATIC(key_tables, key_table, entry, key_table_cmp);
73 static struct key_tables key_tables = RB_INITIALIZER(&key_tables);
74 
75 static int
76 key_table_cmp(struct key_table *table1, struct key_table *table2)
77 {
78 	return (strcmp(table1->name, table2->name));
79 }
80 
81 static int
82 key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
83 {
84 	if (bd1->key < bd2->key)
85 		return (-1);
86 	if (bd1->key > bd2->key)
87 		return (1);
88 	return (0);
89 }
90 
91 static void
92 key_bindings_free(struct key_binding *bd)
93 {
94 	cmd_list_free(bd->cmdlist);
95 	free((void *)bd->note);
96 	free(bd);
97 }
98 
99 struct key_table *
100 key_bindings_get_table(const char *name, int create)
101 {
102 	struct key_table	table_find, *table;
103 
104 	table_find.name = name;
105 	table = RB_FIND(key_tables, &key_tables, &table_find);
106 	if (table != NULL || !create)
107 		return (table);
108 
109 	table = xmalloc(sizeof *table);
110 	table->name = xstrdup(name);
111 	RB_INIT(&table->key_bindings);
112 	RB_INIT(&table->default_key_bindings);
113 
114 	table->references = 1; /* one reference in key_tables */
115 	RB_INSERT(key_tables, &key_tables, table);
116 
117 	return (table);
118 }
119 
120 struct key_table *
121 key_bindings_first_table(void)
122 {
123 	return (RB_MIN(key_tables, &key_tables));
124 }
125 
126 struct key_table *
127 key_bindings_next_table(struct key_table *table)
128 {
129 	return (RB_NEXT(key_tables, &key_tables, table));
130 }
131 
132 void
133 key_bindings_unref_table(struct key_table *table)
134 {
135 	struct key_binding	*bd;
136 	struct key_binding	*bd1;
137 
138 	if (--table->references != 0)
139 		return;
140 
141 	RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) {
142 		RB_REMOVE(key_bindings, &table->key_bindings, bd);
143 		key_bindings_free(bd);
144 	}
145 	RB_FOREACH_SAFE(bd, key_bindings, &table->default_key_bindings, bd1) {
146 		RB_REMOVE(key_bindings, &table->default_key_bindings, bd);
147 		key_bindings_free(bd);
148 	}
149 
150 	free((void *)table->name);
151 	free(table);
152 }
153 
154 struct key_binding *
155 key_bindings_get(struct key_table *table, key_code key)
156 {
157 	struct key_binding	bd;
158 
159 	bd.key = key;
160 	return (RB_FIND(key_bindings, &table->key_bindings, &bd));
161 }
162 
163 struct key_binding *
164 key_bindings_get_default(struct key_table *table, key_code key)
165 {
166 	struct key_binding	bd;
167 
168 	bd.key = key;
169 	return (RB_FIND(key_bindings, &table->default_key_bindings, &bd));
170 }
171 
172 struct key_binding *
173 key_bindings_first(struct key_table *table)
174 {
175 	return (RB_MIN(key_bindings, &table->key_bindings));
176 }
177 
178 struct key_binding *
179 key_bindings_next(__unused struct key_table *table, struct key_binding *bd)
180 {
181 	return (RB_NEXT(key_bindings, &table->key_bindings, bd));
182 }
183 
184 void
185 key_bindings_add(const char *name, key_code key, const char *note, int repeat,
186     struct cmd_list *cmdlist)
187 {
188 	struct key_table	*table;
189 	struct key_binding	*bd;
190 
191 	table = key_bindings_get_table(name, 1);
192 
193 	bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
194 	if (bd != NULL) {
195 		RB_REMOVE(key_bindings, &table->key_bindings, bd);
196 		key_bindings_free(bd);
197 	}
198 
199 	bd = xcalloc(1, sizeof *bd);
200 	bd->key = (key & ~KEYC_MASK_FLAGS);
201 	if (note != NULL)
202 		bd->note = xstrdup(note);
203 	RB_INSERT(key_bindings, &table->key_bindings, bd);
204 
205 	if (repeat)
206 		bd->flags |= KEY_BINDING_REPEAT;
207 	bd->cmdlist = cmdlist;
208 }
209 
210 void
211 key_bindings_remove(const char *name, key_code key)
212 {
213 	struct key_table	*table;
214 	struct key_binding	*bd;
215 
216 	table = key_bindings_get_table(name, 0);
217 	if (table == NULL)
218 		return;
219 
220 	bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
221 	if (bd == NULL)
222 		return;
223 
224 	RB_REMOVE(key_bindings, &table->key_bindings, bd);
225 	key_bindings_free(bd);
226 
227 	if (RB_EMPTY(&table->key_bindings) &&
228 	    RB_EMPTY(&table->default_key_bindings)) {
229 		RB_REMOVE(key_tables, &key_tables, table);
230 		key_bindings_unref_table(table);
231 	}
232 }
233 
234 void
235 key_bindings_reset(const char *name, key_code key)
236 {
237 	struct key_table	*table;
238 	struct key_binding	*bd, *dd;
239 
240 	table = key_bindings_get_table(name, 0);
241 	if (table == NULL)
242 		return;
243 
244 	bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
245 	if (bd == NULL)
246 		return;
247 
248 	dd = key_bindings_get_default(table, bd->key);
249 	if (dd == NULL) {
250 		key_bindings_remove(name, bd->key);
251 		return;
252 	}
253 
254 	cmd_list_free(bd->cmdlist);
255 	bd->cmdlist = dd->cmdlist;
256 	bd->cmdlist->references++;
257 
258 	free((void *)bd->note);
259 	if (dd->note != NULL)
260 		bd->note = xstrdup(dd->note);
261 	else
262 		bd->note = NULL;
263 	bd->flags = dd->flags;
264 }
265 
266 void
267 key_bindings_remove_table(const char *name)
268 {
269 	struct key_table	*table;
270 	struct client		*c;
271 
272 	table = key_bindings_get_table(name, 0);
273 	if (table != NULL) {
274 		RB_REMOVE(key_tables, &key_tables, table);
275 		key_bindings_unref_table(table);
276 	}
277 	TAILQ_FOREACH(c, &clients, entry) {
278 		if (c->keytable == table)
279 			server_client_set_key_table(c, NULL);
280 	}
281 }
282 
283 void
284 key_bindings_reset_table(const char *name)
285 {
286 	struct key_table	*table;
287 	struct key_binding	*bd, *bd1;
288 
289 	table = key_bindings_get_table(name, 0);
290 	if (table == NULL)
291 		return;
292 	if (RB_EMPTY(&table->default_key_bindings)) {
293 		key_bindings_remove_table(name);
294 		return;
295 	}
296 	RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1)
297 		key_bindings_reset(name, bd->key);
298 }
299 
300 static enum cmd_retval
301 key_bindings_init_done(__unused struct cmdq_item *item, __unused void *data)
302 {
303 	struct key_table	*table;
304 	struct key_binding	*bd, *new_bd;
305 
306 	RB_FOREACH(table, key_tables, &key_tables) {
307 		RB_FOREACH(bd, key_bindings, &table->key_bindings) {
308 			new_bd = xcalloc(1, sizeof *bd);
309 			new_bd->key = bd->key;
310 			if (bd->note != NULL)
311 				new_bd->note = xstrdup(bd->note);
312 			new_bd->flags = bd->flags;
313 			new_bd->cmdlist = bd->cmdlist;
314 			new_bd->cmdlist->references++;
315 			RB_INSERT(key_bindings, &table->default_key_bindings,
316 			    new_bd);
317 		}
318 	}
319 	return (CMD_RETURN_NORMAL);
320 }
321 
322 void
323 key_bindings_init(void)
324 {
325 	static const char *defaults[] = {
326 		/* Prefix keys. */
327 		"bind -N 'Send the prefix key' C-b send-prefix",
328 		"bind -N 'Rotate through the panes' C-o rotate-window",
329 		"bind -N 'Suspend the current client' C-z suspend-client",
330 		"bind -N 'Select next layout' Space next-layout",
331 		"bind -N 'Break pane to a new window' ! break-pane",
332 		"bind -N 'Split window vertically' '\"' split-window",
333 		"bind -N 'List all paste buffers' '#' list-buffers",
334 		"bind -N 'Rename current session' '$' command-prompt -I'#S' \"rename-session -- '%%'\"",
335 		"bind -N 'Split window horizontally' % split-window -h",
336 		"bind -N 'Kill current window' & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
337 		"bind -N 'Prompt for window index to select' \"'\" command-prompt -Wpindex \"select-window -t ':%%'\"",
338 		"bind -N 'Switch to previous client' ( switch-client -p",
339 		"bind -N 'Switch to next client' ) switch-client -n",
340 		"bind -N 'Rename current window' , command-prompt -I'#W' \"rename-window -- '%%'\"",
341 		"bind -N 'Delete the most recent paste buffer' - delete-buffer",
342 		"bind -N 'Move the current window' . command-prompt -T \"move-window -t '%%'\"",
343 		"bind -N 'Describe key binding' '/' command-prompt -kpkey 'list-keys -1N \"%%%\"'",
344 		"bind -N 'Select window 0' 0 select-window -t:=0",
345 		"bind -N 'Select window 1' 1 select-window -t:=1",
346 		"bind -N 'Select window 2' 2 select-window -t:=2",
347 		"bind -N 'Select window 3' 3 select-window -t:=3",
348 		"bind -N 'Select window 4' 4 select-window -t:=4",
349 		"bind -N 'Select window 5' 5 select-window -t:=5",
350 		"bind -N 'Select window 6' 6 select-window -t:=6",
351 		"bind -N 'Select window 7' 7 select-window -t:=7",
352 		"bind -N 'Select window 8' 8 select-window -t:=8",
353 		"bind -N 'Select window 9' 9 select-window -t:=9",
354 		"bind -N 'Prompt for a command' : command-prompt",
355 		"bind -N 'Move to the previously active pane' \\; last-pane",
356 		"bind -N 'Choose a paste buffer from a list' = choose-buffer -Z",
357 		"bind -N 'List key bindings' ? list-keys -N",
358 		"bind -N 'Choose a client from a list' D choose-client -Z",
359 		"bind -N 'Spread panes out evenly' E select-layout -E",
360 		"bind -N 'Switch to the last client' L switch-client -l",
361 		"bind -N 'Clear the marked pane' M select-pane -M",
362 		"bind -N 'Enter copy mode' [ copy-mode",
363 		"bind -N 'Paste the most recent paste buffer' ] paste-buffer -p",
364 		"bind -N 'Create a new window' c new-window",
365 		"bind -N 'Detach the current client' d detach-client",
366 		"bind -N 'Search for a pane' f command-prompt \"find-window -Z -- '%%'\"",
367 		"bind -N 'Display window information' i display-message",
368 		"bind -N 'Select the previously current window' l last-window",
369 		"bind -N 'Toggle the marked pane' m select-pane -m",
370 		"bind -N 'Select the next window' n next-window",
371 		"bind -N 'Select the next pane' o select-pane -t:.+",
372 		"bind -N 'Customize options' C customize-mode -Z",
373 		"bind -N 'Select the previous pane' p previous-window",
374 		"bind -N 'Display pane numbers' q display-panes",
375 		"bind -N 'Redraw the current client' r refresh-client",
376 		"bind -N 'Choose a session from a list' s choose-tree -Zs",
377 		"bind -N 'Show a clock' t clock-mode",
378 		"bind -N 'Choose a window from a list' w choose-tree -Zw",
379 		"bind -N 'Kill the active pane' x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
380 		"bind -N 'Zoom the active pane' z resize-pane -Z",
381 		"bind -N 'Swap the active pane with the pane above' '{' swap-pane -U",
382 		"bind -N 'Swap the active pane with the pane below' '}' swap-pane -D",
383 		"bind -N 'Show messages' '~' show-messages",
384 		"bind -N 'Enter copy mode and scroll up' PPage copy-mode -u",
385 		"bind -N 'Select the pane above the active pane' -r Up select-pane -U",
386 		"bind -N 'Select the pane below the active pane' -r Down select-pane -D",
387 		"bind -N 'Select the pane to the left of the active pane' -r Left select-pane -L",
388 		"bind -N 'Select the pane to the right of the active pane' -r Right select-pane -R",
389 		"bind -N 'Set the even-horizontal layout' M-1 select-layout even-horizontal",
390 		"bind -N 'Set the even-vertical layout' M-2 select-layout even-vertical",
391 		"bind -N 'Set the main-horizontal layout' M-3 select-layout main-horizontal",
392 		"bind -N 'Set the main-vertical layout' M-4 select-layout main-vertical",
393 		"bind -N 'Select the tiled layout' M-5 select-layout tiled",
394 		"bind -N 'Select the next window with an alert' M-n next-window -a",
395 		"bind -N 'Rotate through the panes in reverse' M-o rotate-window -D",
396 		"bind -N 'Select the previous window with an alert' M-p previous-window -a",
397 		"bind -N 'Move the visible part of the window up' -r S-Up refresh-client -U 10",
398 		"bind -N 'Move the visible part of the window down' -r S-Down refresh-client -D 10",
399 		"bind -N 'Move the visible part of the window left' -r S-Left refresh-client -L 10",
400 		"bind -N 'Move the visible part of the window right' -r S-Right refresh-client -R 10",
401 		"bind -N 'Reset so the visible part of the window follows the cursor' -r DC refresh-client -c",
402 		"bind -N 'Resize the pane up by 5' -r M-Up resize-pane -U 5",
403 		"bind -N 'Resize the pane down by 5' -r M-Down resize-pane -D 5",
404 		"bind -N 'Resize the pane left by 5' -r M-Left resize-pane -L 5",
405 		"bind -N 'Resize the pane right by 5' -r M-Right resize-pane -R 5",
406 		"bind -N 'Resize the pane up' -r C-Up resize-pane -U",
407 		"bind -N 'Resize the pane down' -r C-Down resize-pane -D",
408 		"bind -N 'Resize the pane left' -r C-Left resize-pane -L",
409 		"bind -N 'Resize the pane right' -r C-Right resize-pane -R",
410 
411 		/* Menu keys */
412 		"bind < display-menu -xW -yW -T '#[align=centre]#{window_index}:#{window_name}' " DEFAULT_WINDOW_MENU,
413 		"bind > display-menu -xP -yP -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU,
414 
415 		/* Mouse button 1 down on pane. */
416 		"bind -n MouseDown1Pane select-pane -t=\\; send -M",
417 
418 		/* Mouse button 1 drag on pane. */
419 		"bind -n MouseDrag1Pane if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -M }",
420 
421 		/* Mouse wheel up on pane. */
422 		"bind -n WheelUpPane if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -e }",
423 
424 		/* Mouse button 2 down on pane. */
425 		"bind -n MouseDown2Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { paste -p }",
426 
427 		/* Mouse button 1 double click on pane. */
428 		"bind -n DoubleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-word; run -d0.3; send -X copy-pipe-and-cancel }",
429 
430 		/* Mouse button 1 triple click on pane. */
431 		"bind -n TripleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-line; run -d0.3; send -X copy-pipe-and-cancel }",
432 
433 		/* Mouse button 1 drag on border. */
434 		"bind -n MouseDrag1Border resize-pane -M",
435 
436 		/* Mouse button 1 down on status line. */
437 		"bind -n MouseDown1Status select-window -t=",
438 
439 		/* Mouse wheel down on status line. */
440 		"bind -n WheelDownStatus next-window",
441 
442 		/* Mouse wheel up on status line. */
443 		"bind -n WheelUpStatus previous-window",
444 
445 		/* Mouse button 3 down on status left. */
446 		"bind -n MouseDown3StatusLeft display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU,
447 
448 		/* Mouse button 3 down on status line. */
449 		"bind -n MouseDown3Status display-menu -t= -xW -yW -T '#[align=centre]#{window_index}:#{window_name}' " DEFAULT_WINDOW_MENU,
450 
451 		/* Mouse button 3 down on pane. */
452 		"bind -n MouseDown3Pane if -Ft= '#{||:#{mouse_any_flag},#{&&:#{pane_in_mode},#{?#{m/r:(copy|view)-mode,#{pane_mode}},0,1}}}' { select-pane -t=; send -M } { display-menu -t= -xM -yM -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU " }",
453 		"bind -n M-MouseDown3Pane display-menu -t= -xM -yM -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU,
454 
455 		/* Copy mode (emacs) keys. */
456 		"bind -Tcopy-mode C-Space send -X begin-selection",
457 		"bind -Tcopy-mode C-a send -X start-of-line",
458 		"bind -Tcopy-mode C-c send -X cancel",
459 		"bind -Tcopy-mode C-e send -X end-of-line",
460 		"bind -Tcopy-mode C-f send -X cursor-right",
461 		"bind -Tcopy-mode C-b send -X cursor-left",
462 		"bind -Tcopy-mode C-g send -X clear-selection",
463 		"bind -Tcopy-mode C-k send -X copy-end-of-line",
464 		"bind -Tcopy-mode C-n send -X cursor-down",
465 		"bind -Tcopy-mode C-p send -X cursor-up",
466 		"bind -Tcopy-mode C-r command-prompt -ip'(search up)' -I'#{pane_search_string}' 'send -X search-backward-incremental \"%%%\"'",
467 		"bind -Tcopy-mode C-s command-prompt -ip'(search down)' -I'#{pane_search_string}' 'send -X search-forward-incremental \"%%%\"'",
468 		"bind -Tcopy-mode C-v send -X page-down",
469 		"bind -Tcopy-mode C-w send -X copy-pipe-and-cancel",
470 		"bind -Tcopy-mode Escape send -X cancel",
471 		"bind -Tcopy-mode Space send -X page-down",
472 		"bind -Tcopy-mode , send -X jump-reverse",
473 		"bind -Tcopy-mode \\; send -X jump-again",
474 		"bind -Tcopy-mode F command-prompt -1p'(jump backward)' 'send -X jump-backward \"%%%\"'",
475 		"bind -Tcopy-mode N send -X search-reverse",
476 		"bind -Tcopy-mode R send -X rectangle-toggle",
477 		"bind -Tcopy-mode T command-prompt -1p'(jump to backward)' 'send -X jump-to-backward \"%%%\"'",
478 		"bind -Tcopy-mode X send -X set-mark",
479 		"bind -Tcopy-mode f command-prompt -1p'(jump forward)' 'send -X jump-forward \"%%%\"'",
480 		"bind -Tcopy-mode g command-prompt -p'(goto line)' 'send -X goto-line \"%%%\"'",
481 		"bind -Tcopy-mode n send -X search-again",
482 		"bind -Tcopy-mode q send -X cancel",
483 		"bind -Tcopy-mode r send -X refresh-from-pane",
484 		"bind -Tcopy-mode t command-prompt -1p'(jump to forward)' 'send -X jump-to-forward \"%%%\"'",
485 		"bind -Tcopy-mode Home send -X start-of-line",
486 		"bind -Tcopy-mode End send -X end-of-line",
487 		"bind -Tcopy-mode MouseDown1Pane select-pane",
488 		"bind -Tcopy-mode MouseDrag1Pane select-pane\\; send -X begin-selection",
489 		"bind -Tcopy-mode MouseDragEnd1Pane send -X copy-pipe-and-cancel",
490 		"bind -Tcopy-mode WheelUpPane select-pane\\; send -N5 -X scroll-up",
491 		"bind -Tcopy-mode WheelDownPane select-pane\\; send -N5 -X scroll-down",
492 		"bind -Tcopy-mode DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-pipe-and-cancel",
493 		"bind -Tcopy-mode TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-pipe-and-cancel",
494 		"bind -Tcopy-mode NPage send -X page-down",
495 		"bind -Tcopy-mode PPage send -X page-up",
496 		"bind -Tcopy-mode Up send -X cursor-up",
497 		"bind -Tcopy-mode Down send -X cursor-down",
498 		"bind -Tcopy-mode Left send -X cursor-left",
499 		"bind -Tcopy-mode Right send -X cursor-right",
500 		"bind -Tcopy-mode M-1 command-prompt -Np'(repeat)' -I1 'send -N \"%%%\"'",
501 		"bind -Tcopy-mode M-2 command-prompt -Np'(repeat)' -I2 'send -N \"%%%\"'",
502 		"bind -Tcopy-mode M-3 command-prompt -Np'(repeat)' -I3 'send -N \"%%%\"'",
503 		"bind -Tcopy-mode M-4 command-prompt -Np'(repeat)' -I4 'send -N \"%%%\"'",
504 		"bind -Tcopy-mode M-5 command-prompt -Np'(repeat)' -I5 'send -N \"%%%\"'",
505 		"bind -Tcopy-mode M-6 command-prompt -Np'(repeat)' -I6 'send -N \"%%%\"'",
506 		"bind -Tcopy-mode M-7 command-prompt -Np'(repeat)' -I7 'send -N \"%%%\"'",
507 		"bind -Tcopy-mode M-8 command-prompt -Np'(repeat)' -I8 'send -N \"%%%\"'",
508 		"bind -Tcopy-mode M-9 command-prompt -Np'(repeat)' -I9 'send -N \"%%%\"'",
509 		"bind -Tcopy-mode M-< send -X history-top",
510 		"bind -Tcopy-mode M-> send -X history-bottom",
511 		"bind -Tcopy-mode M-R send -X top-line",
512 		"bind -Tcopy-mode M-b send -X previous-word",
513 		"bind -Tcopy-mode C-M-b send -X previous-matching-bracket",
514 		"bind -Tcopy-mode M-f send -X next-word-end",
515 		"bind -Tcopy-mode C-M-f send -X next-matching-bracket",
516 		"bind -Tcopy-mode M-m send -X back-to-indentation",
517 		"bind -Tcopy-mode M-r send -X middle-line",
518 		"bind -Tcopy-mode M-v send -X page-up",
519 		"bind -Tcopy-mode M-w send -X copy-pipe-and-cancel",
520 		"bind -Tcopy-mode M-x send -X jump-to-mark",
521 		"bind -Tcopy-mode 'M-{' send -X previous-paragraph",
522 		"bind -Tcopy-mode 'M-}' send -X next-paragraph",
523 		"bind -Tcopy-mode M-Up send -X halfpage-up",
524 		"bind -Tcopy-mode M-Down send -X halfpage-down",
525 		"bind -Tcopy-mode C-Up send -X scroll-up",
526 		"bind -Tcopy-mode C-Down send -X scroll-down",
527 
528 		/* Copy mode (vi) keys. */
529 		"bind -Tcopy-mode-vi '#' send -FX search-backward '#{copy_cursor_word}'",
530 		"bind -Tcopy-mode-vi * send -FX search-forward '#{copy_cursor_word}'",
531 		"bind -Tcopy-mode-vi C-c send -X cancel",
532 		"bind -Tcopy-mode-vi C-d send -X halfpage-down",
533 		"bind -Tcopy-mode-vi C-e send -X scroll-down",
534 		"bind -Tcopy-mode-vi C-b send -X page-up",
535 		"bind -Tcopy-mode-vi C-f send -X page-down",
536 		"bind -Tcopy-mode-vi C-h send -X cursor-left",
537 		"bind -Tcopy-mode-vi C-j send -X copy-pipe-and-cancel",
538 		"bind -Tcopy-mode-vi Enter send -X copy-pipe-and-cancel",
539 		"bind -Tcopy-mode-vi C-u send -X halfpage-up",
540 		"bind -Tcopy-mode-vi C-v send -X rectangle-toggle",
541 		"bind -Tcopy-mode-vi C-y send -X scroll-up",
542 		"bind -Tcopy-mode-vi Escape send -X clear-selection",
543 		"bind -Tcopy-mode-vi Space send -X begin-selection",
544 		"bind -Tcopy-mode-vi '$' send -X end-of-line",
545 		"bind -Tcopy-mode-vi , send -X jump-reverse",
546 		"bind -Tcopy-mode-vi / command-prompt -p'(search down)' 'send -X search-forward \"%%%\"'",
547 		"bind -Tcopy-mode-vi 0 send -X start-of-line",
548 		"bind -Tcopy-mode-vi 1 command-prompt -Np'(repeat)' -I1 'send -N \"%%%\"'",
549 		"bind -Tcopy-mode-vi 2 command-prompt -Np'(repeat)' -I2 'send -N \"%%%\"'",
550 		"bind -Tcopy-mode-vi 3 command-prompt -Np'(repeat)' -I3 'send -N \"%%%\"'",
551 		"bind -Tcopy-mode-vi 4 command-prompt -Np'(repeat)' -I4 'send -N \"%%%\"'",
552 		"bind -Tcopy-mode-vi 5 command-prompt -Np'(repeat)' -I5 'send -N \"%%%\"'",
553 		"bind -Tcopy-mode-vi 6 command-prompt -Np'(repeat)' -I6 'send -N \"%%%\"'",
554 		"bind -Tcopy-mode-vi 7 command-prompt -Np'(repeat)' -I7 'send -N \"%%%\"'",
555 		"bind -Tcopy-mode-vi 8 command-prompt -Np'(repeat)' -I8 'send -N \"%%%\"'",
556 		"bind -Tcopy-mode-vi 9 command-prompt -Np'(repeat)' -I9 'send -N \"%%%\"'",
557 		"bind -Tcopy-mode-vi : command-prompt -p'(goto line)' 'send -X goto-line \"%%%\"'",
558 		"bind -Tcopy-mode-vi \\; send -X jump-again",
559 		"bind -Tcopy-mode-vi ? command-prompt -p'(search up)' 'send -X search-backward \"%%%\"'",
560 		"bind -Tcopy-mode-vi A send -X append-selection-and-cancel",
561 		"bind -Tcopy-mode-vi B send -X previous-space",
562 		"bind -Tcopy-mode-vi D send -X copy-end-of-line",
563 		"bind -Tcopy-mode-vi E send -X next-space-end",
564 		"bind -Tcopy-mode-vi F command-prompt -1p'(jump backward)' 'send -X jump-backward \"%%%\"'",
565 		"bind -Tcopy-mode-vi G send -X history-bottom",
566 		"bind -Tcopy-mode-vi H send -X top-line",
567 		"bind -Tcopy-mode-vi J send -X scroll-down",
568 		"bind -Tcopy-mode-vi K send -X scroll-up",
569 		"bind -Tcopy-mode-vi L send -X bottom-line",
570 		"bind -Tcopy-mode-vi M send -X middle-line",
571 		"bind -Tcopy-mode-vi N send -X search-reverse",
572 		"bind -Tcopy-mode-vi T command-prompt -1p'(jump to backward)' 'send -X jump-to-backward \"%%%\"'",
573 		"bind -Tcopy-mode-vi V send -X select-line",
574 		"bind -Tcopy-mode-vi W send -X next-space",
575 		"bind -Tcopy-mode-vi X send -X set-mark",
576 		"bind -Tcopy-mode-vi ^ send -X back-to-indentation",
577 		"bind -Tcopy-mode-vi b send -X previous-word",
578 		"bind -Tcopy-mode-vi e send -X next-word-end",
579 		"bind -Tcopy-mode-vi f command-prompt -1p'(jump forward)' 'send -X jump-forward \"%%%\"'",
580 		"bind -Tcopy-mode-vi g send -X history-top",
581 		"bind -Tcopy-mode-vi h send -X cursor-left",
582 		"bind -Tcopy-mode-vi j send -X cursor-down",
583 		"bind -Tcopy-mode-vi k send -X cursor-up",
584 		"bind -Tcopy-mode-vi l send -X cursor-right",
585 		"bind -Tcopy-mode-vi n send -X search-again",
586 		"bind -Tcopy-mode-vi o send -X other-end",
587 		"bind -Tcopy-mode-vi q send -X cancel",
588 		"bind -Tcopy-mode-vi r send -X refresh-from-pane",
589 		"bind -Tcopy-mode-vi t command-prompt -1p'(jump to forward)' 'send -X jump-to-forward \"%%%\"'",
590 		"bind -Tcopy-mode-vi v send -X rectangle-toggle",
591 		"bind -Tcopy-mode-vi w send -X next-word",
592 		"bind -Tcopy-mode-vi '{' send -X previous-paragraph",
593 		"bind -Tcopy-mode-vi '}' send -X next-paragraph",
594 		"bind -Tcopy-mode-vi % send -X next-matching-bracket",
595 		"bind -Tcopy-mode-vi MouseDown1Pane select-pane",
596 		"bind -Tcopy-mode-vi MouseDrag1Pane select-pane\\; send -X begin-selection",
597 		"bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel",
598 		"bind -Tcopy-mode-vi WheelUpPane select-pane\\; send -N5 -X scroll-up",
599 		"bind -Tcopy-mode-vi WheelDownPane select-pane\\; send -N5 -X scroll-down",
600 		"bind -Tcopy-mode-vi DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-pipe-and-cancel",
601 		"bind -Tcopy-mode-vi TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-pipe-and-cancel",
602 		"bind -Tcopy-mode-vi BSpace send -X cursor-left",
603 		"bind -Tcopy-mode-vi NPage send -X page-down",
604 		"bind -Tcopy-mode-vi PPage send -X page-up",
605 		"bind -Tcopy-mode-vi Up send -X cursor-up",
606 		"bind -Tcopy-mode-vi Down send -X cursor-down",
607 		"bind -Tcopy-mode-vi Left send -X cursor-left",
608 		"bind -Tcopy-mode-vi Right send -X cursor-right",
609 		"bind -Tcopy-mode-vi M-x send -X jump-to-mark",
610 		"bind -Tcopy-mode-vi C-Up send -X scroll-up",
611 		"bind -Tcopy-mode-vi C-Down send -X scroll-down",
612 	};
613 	u_int			 i;
614 	struct cmd_parse_result	*pr;
615 
616 	for (i = 0; i < nitems(defaults); i++) {
617 		pr = cmd_parse_from_string(defaults[i], NULL);
618 		if (pr->status != CMD_PARSE_SUCCESS)
619 			fatalx("bad default key: %s", defaults[i]);
620 		cmdq_append(NULL, cmdq_get_command(pr->cmdlist, NULL));
621 		cmd_list_free(pr->cmdlist);
622 	}
623 	cmdq_append(NULL, cmdq_get_callback(key_bindings_init_done, NULL));
624 }
625 
626 static enum cmd_retval
627 key_bindings_read_only(struct cmdq_item *item, __unused void *data)
628 {
629 	cmdq_error(item, "client is read-only");
630 	return (CMD_RETURN_ERROR);
631 }
632 
633 struct cmdq_item *
634 key_bindings_dispatch(struct key_binding *bd, struct cmdq_item *item,
635     struct client *c, struct key_event *event, struct cmd_find_state *fs)
636 {
637 	struct cmdq_item	*new_item;
638 	struct cmdq_state	*new_state;
639 	int			 readonly, flags = 0;
640 
641 	if (c == NULL || (~c->flags & CLIENT_READONLY))
642 		readonly = 1;
643 	else
644 		readonly = cmd_list_all_have(bd->cmdlist, CMD_READONLY);
645 	if (!readonly)
646 		new_item = cmdq_get_callback(key_bindings_read_only, NULL);
647 	else {
648 		if (bd->flags & KEY_BINDING_REPEAT)
649 			flags |= CMDQ_STATE_REPEAT;
650 		new_state = cmdq_new_state(fs, event, flags);
651 		new_item = cmdq_get_command(bd->cmdlist, new_state);
652 		cmdq_free_state(new_state);
653 	}
654 	if (item != NULL)
655 		new_item = cmdq_insert_after(item, new_item);
656 	else
657 		new_item = cmdq_append(c, new_item);
658 	return (new_item);
659 }
660