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