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