If -s to swap-pane is not given, use the current pane.
[tmux-openbsd.git] / mode-key.c
blob54abcf32d6371419f723955713308df99c10a8fe
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <string.h>
23 #include "tmux.h"
26 * Mode keys. These are the key bindings used when editing (status prompt), and
27 * in the modes. They are split into two sets of three tables, one set of three
28 * for vi and the other for emacs key bindings. The three tables are for
29 * editing, for menu-like modes (choice, more), and for copy modes (copy,
30 * scroll).
32 * The fixed tables of struct mode_key_entry below are the defaults: they are
33 * built into a tree of struct mode_key_binding by mode_key_init_trees, which
34 * can then be modified.
36 * vi command mode is handled by having a mode flag in the struct which allows
37 * two sets of bindings to be swapped between. A couple of editing commands
38 * (MODEKEYEDIT_SWITCHMODE, MODEKEYEDIT_SWITCHMODEAPPEND,
39 * MODEKEYEDIT_SWITCHMODEAPPENDLINE, and MODEKEYEDIT_SWITCHMODEBEGINLINE)
40 * are special-cased to do this.
43 /* Edit keys command strings. */
44 const struct mode_key_cmdstr mode_key_cmdstr_edit[] = {
45 { MODEKEYEDIT_BACKSPACE, "backspace" },
46 { MODEKEYEDIT_CANCEL, "cancel" },
47 { MODEKEYEDIT_COMPLETE, "complete" },
48 { MODEKEYEDIT_CURSORLEFT, "cursor-left" },
49 { MODEKEYEDIT_CURSORRIGHT, "cursor-right" },
50 { MODEKEYEDIT_DELETE, "delete" },
51 { MODEKEYEDIT_DELETELINE, "delete-line" },
52 { MODEKEYEDIT_DELETETOENDOFLINE, "delete-end-of-line" },
53 { MODEKEYEDIT_DELETEWORD, "delete-word" },
54 { MODEKEYEDIT_ENDOFLINE, "end-of-line" },
55 { MODEKEYEDIT_ENTER, "enter" },
56 { MODEKEYEDIT_HISTORYDOWN, "history-down" },
57 { MODEKEYEDIT_HISTORYUP, "history-up" },
58 { MODEKEYEDIT_NEXTSPACE, "next-space" },
59 { MODEKEYEDIT_NEXTSPACEEND, "next-space-end" },
60 { MODEKEYEDIT_NEXTWORD, "next-word" },
61 { MODEKEYEDIT_NEXTWORDEND, "next-word-end" },
62 { MODEKEYEDIT_PASTE, "paste" },
63 { MODEKEYEDIT_PREVIOUSSPACE, "previous-space" },
64 { MODEKEYEDIT_PREVIOUSWORD, "previous-word" },
65 { MODEKEYEDIT_STARTOFLINE, "start-of-line" },
66 { MODEKEYEDIT_SWITCHMODE, "switch-mode" },
67 { MODEKEYEDIT_SWITCHMODEAPPEND, "switch-mode-append" },
68 { MODEKEYEDIT_SWITCHMODEAPPENDLINE, "switch-mode-append-line" },
69 { MODEKEYEDIT_SWITCHMODEBEGINLINE, "switch-mode-begin-line" },
70 { MODEKEYEDIT_TRANSPOSECHARS, "transpose-chars" },
72 { 0, NULL }
75 /* Choice keys command strings. */
76 const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
77 { MODEKEYCHOICE_BACKSPACE, "backspace" },
78 { MODEKEYCHOICE_CANCEL, "cancel" },
79 { MODEKEYCHOICE_CHOOSE, "choose" },
80 { MODEKEYCHOICE_DOWN, "down" },
81 { MODEKEYCHOICE_PAGEDOWN, "page-down" },
82 { MODEKEYCHOICE_PAGEUP, "page-up" },
83 { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
84 { MODEKEYCHOICE_SCROLLUP, "scroll-up" },
85 { MODEKEYCHOICE_STARTNUMBERPREFIX, "start-number-prefix" },
86 { MODEKEYCHOICE_TREE_COLLAPSE, "tree-collapse" },
87 { MODEKEYCHOICE_TREE_COLLAPSE_ALL, "tree-collapse-all" },
88 { MODEKEYCHOICE_TREE_EXPAND, "tree-expand" },
89 { MODEKEYCHOICE_TREE_EXPAND_ALL, "tree-expand-all" },
90 { MODEKEYCHOICE_TREE_TOGGLE, "tree-toggle" },
91 { MODEKEYCHOICE_UP, "up" },
93 { 0, NULL }
96 /* Copy keys command strings. */
97 const struct mode_key_cmdstr mode_key_cmdstr_copy[] = {
98 { MODEKEYCOPY_BACKTOINDENTATION, "back-to-indentation" },
99 { MODEKEYCOPY_BOTTOMLINE, "bottom-line" },
100 { MODEKEYCOPY_CANCEL, "cancel" },
101 { MODEKEYCOPY_CLEARSELECTION, "clear-selection" },
102 { MODEKEYCOPY_COPYPIPE, "copy-pipe" },
103 { MODEKEYCOPY_COPYLINE, "copy-line" },
104 { MODEKEYCOPY_COPYENDOFLINE, "copy-end-of-line" },
105 { MODEKEYCOPY_COPYSELECTION, "copy-selection" },
106 { MODEKEYCOPY_DOWN, "cursor-down" },
107 { MODEKEYCOPY_ENDOFLINE, "end-of-line" },
108 { MODEKEYCOPY_GOTOLINE, "goto-line" },
109 { MODEKEYCOPY_HALFPAGEDOWN, "halfpage-down" },
110 { MODEKEYCOPY_HALFPAGEUP, "halfpage-up" },
111 { MODEKEYCOPY_HISTORYBOTTOM, "history-bottom" },
112 { MODEKEYCOPY_HISTORYTOP, "history-top" },
113 { MODEKEYCOPY_JUMP, "jump-forward" },
114 { MODEKEYCOPY_JUMPAGAIN, "jump-again" },
115 { MODEKEYCOPY_JUMPREVERSE, "jump-reverse" },
116 { MODEKEYCOPY_JUMPBACK, "jump-backward" },
117 { MODEKEYCOPY_JUMPTO, "jump-to-forward" },
118 { MODEKEYCOPY_JUMPTOBACK, "jump-to-backward" },
119 { MODEKEYCOPY_LEFT, "cursor-left" },
120 { MODEKEYCOPY_RECTANGLETOGGLE, "rectangle-toggle" },
121 { MODEKEYCOPY_MIDDLELINE, "middle-line" },
122 { MODEKEYCOPY_NEXTPAGE, "page-down" },
123 { MODEKEYCOPY_NEXTSPACE, "next-space" },
124 { MODEKEYCOPY_NEXTSPACEEND, "next-space-end" },
125 { MODEKEYCOPY_NEXTWORD, "next-word" },
126 { MODEKEYCOPY_NEXTWORDEND, "next-word-end" },
127 { MODEKEYCOPY_PREVIOUSPAGE, "page-up" },
128 { MODEKEYCOPY_PREVIOUSSPACE, "previous-space" },
129 { MODEKEYCOPY_PREVIOUSWORD, "previous-word" },
130 { MODEKEYCOPY_RIGHT, "cursor-right" },
131 { MODEKEYCOPY_SCROLLDOWN, "scroll-down" },
132 { MODEKEYCOPY_SCROLLUP, "scroll-up" },
133 { MODEKEYCOPY_SEARCHAGAIN, "search-again" },
134 { MODEKEYCOPY_SEARCHDOWN, "search-forward" },
135 { MODEKEYCOPY_SEARCHREVERSE, "search-reverse" },
136 { MODEKEYCOPY_SEARCHUP, "search-backward" },
137 { MODEKEYCOPY_SELECTLINE, "select-line" },
138 { MODEKEYCOPY_STARTNUMBERPREFIX, "start-number-prefix" },
139 { MODEKEYCOPY_STARTOFLINE, "start-of-line" },
140 { MODEKEYCOPY_STARTSELECTION, "begin-selection" },
141 { MODEKEYCOPY_TOPLINE, "top-line" },
142 { MODEKEYCOPY_UP, "cursor-up" },
144 { 0, NULL }
147 /* vi editing keys. */
148 const struct mode_key_entry mode_key_vi_edit[] = {
149 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
150 { '\010' /* C-h */, 0, MODEKEYEDIT_BACKSPACE },
151 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
152 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
153 { '\027' /* C-w */, 0, MODEKEYEDIT_DELETEWORD },
154 { '\033' /* Escape */, 0, MODEKEYEDIT_SWITCHMODE },
155 { '\r', 0, MODEKEYEDIT_ENTER },
156 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
157 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
158 { KEYC_DOWN, 0, MODEKEYEDIT_HISTORYDOWN },
159 { KEYC_LEFT, 0, MODEKEYEDIT_CURSORLEFT },
160 { KEYC_RIGHT, 0, MODEKEYEDIT_CURSORRIGHT },
161 { KEYC_UP, 0, MODEKEYEDIT_HISTORYUP },
162 { KEYC_HOME, 0, MODEKEYEDIT_STARTOFLINE },
163 { KEYC_END, 0, MODEKEYEDIT_ENDOFLINE },
165 { '$', 1, MODEKEYEDIT_ENDOFLINE },
166 { '0', 1, MODEKEYEDIT_STARTOFLINE },
167 { 'A', 1, MODEKEYEDIT_SWITCHMODEAPPENDLINE },
168 { 'B', 1, MODEKEYEDIT_PREVIOUSSPACE },
169 { 'D', 1, MODEKEYEDIT_DELETETOENDOFLINE },
170 { 'E', 1, MODEKEYEDIT_NEXTSPACEEND },
171 { 'I', 1, MODEKEYEDIT_SWITCHMODEBEGINLINE },
172 { 'W', 1, MODEKEYEDIT_NEXTSPACE },
173 { 'X', 1, MODEKEYEDIT_BACKSPACE },
174 { '\003' /* C-c */, 1, MODEKEYEDIT_CANCEL },
175 { '\010' /* C-h */, 1, MODEKEYEDIT_BACKSPACE },
176 { '\r', 1, MODEKEYEDIT_ENTER },
177 { '^', 1, MODEKEYEDIT_STARTOFLINE },
178 { 'a', 1, MODEKEYEDIT_SWITCHMODEAPPEND },
179 { 'b', 1, MODEKEYEDIT_PREVIOUSWORD },
180 { 'd', 1, MODEKEYEDIT_DELETELINE },
181 { 'e', 1, MODEKEYEDIT_NEXTWORDEND },
182 { 'h', 1, MODEKEYEDIT_CURSORLEFT },
183 { 'i', 1, MODEKEYEDIT_SWITCHMODE },
184 { 'j', 1, MODEKEYEDIT_HISTORYDOWN },
185 { 'k', 1, MODEKEYEDIT_HISTORYUP },
186 { 'l', 1, MODEKEYEDIT_CURSORRIGHT },
187 { 'p', 1, MODEKEYEDIT_PASTE },
188 { 'w', 1, MODEKEYEDIT_NEXTWORD },
189 { 'x', 1, MODEKEYEDIT_DELETE },
190 { KEYC_BSPACE, 1, MODEKEYEDIT_BACKSPACE },
191 { KEYC_DC, 1, MODEKEYEDIT_DELETE },
192 { KEYC_DOWN, 1, MODEKEYEDIT_HISTORYDOWN },
193 { KEYC_LEFT, 1, MODEKEYEDIT_CURSORLEFT },
194 { KEYC_RIGHT, 1, MODEKEYEDIT_CURSORRIGHT },
195 { KEYC_UP, 1, MODEKEYEDIT_HISTORYUP },
197 { 0, -1, 0 }
199 struct mode_key_tree mode_key_tree_vi_edit;
201 /* vi choice selection keys. */
202 const struct mode_key_entry mode_key_vi_choice[] = {
203 { '0' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
204 { '1' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
205 { '2' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
206 { '3' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
207 { '4' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
208 { '5' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
209 { '6' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
210 { '7' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
211 { '8' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
212 { '9' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
213 { '\002' /* C-b */, 0, MODEKEYCHOICE_PAGEUP },
214 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
215 { '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN },
216 { '\006' /* C-f */, 0, MODEKEYCHOICE_PAGEDOWN },
217 { '\031' /* C-y */, 0, MODEKEYCHOICE_SCROLLUP },
218 { '\r', 0, MODEKEYCHOICE_CHOOSE },
219 { 'j', 0, MODEKEYCHOICE_DOWN },
220 { 'k', 0, MODEKEYCHOICE_UP },
221 { 'q', 0, MODEKEYCHOICE_CANCEL },
222 { KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE },
223 { KEYC_DOWN | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLDOWN },
224 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
225 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
226 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
227 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
228 { KEYC_UP, 0, MODEKEYCHOICE_UP },
229 { ' ', 0, MODEKEYCHOICE_TREE_TOGGLE },
230 { KEYC_LEFT, 0, MODEKEYCHOICE_TREE_COLLAPSE },
231 { KEYC_RIGHT, 0, MODEKEYCHOICE_TREE_EXPAND },
232 { KEYC_LEFT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
233 { KEYC_RIGHT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_EXPAND_ALL },
235 { 0, -1, 0 }
237 struct mode_key_tree mode_key_tree_vi_choice;
239 /* vi copy mode keys. */
240 const struct mode_key_entry mode_key_vi_copy[] = {
241 { ' ', 0, MODEKEYCOPY_STARTSELECTION },
242 { '$', 0, MODEKEYCOPY_ENDOFLINE },
243 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
244 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
245 { '/', 0, MODEKEYCOPY_SEARCHDOWN },
246 { '0', 0, MODEKEYCOPY_STARTOFLINE },
247 { '1', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
248 { '2', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
249 { '3', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
250 { '4', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
251 { '5', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
252 { '6', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
253 { '7', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
254 { '8', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
255 { '9', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
256 { ':', 0, MODEKEYCOPY_GOTOLINE },
257 { '?', 0, MODEKEYCOPY_SEARCHUP },
258 { 'B', 0, MODEKEYCOPY_PREVIOUSSPACE },
259 { 'D', 0, MODEKEYCOPY_COPYENDOFLINE },
260 { 'E', 0, MODEKEYCOPY_NEXTSPACEEND },
261 { 'F', 0, MODEKEYCOPY_JUMPBACK },
262 { 'G', 0, MODEKEYCOPY_HISTORYBOTTOM },
263 { 'H', 0, MODEKEYCOPY_TOPLINE },
264 { 'J', 0, MODEKEYCOPY_SCROLLDOWN },
265 { 'K', 0, MODEKEYCOPY_SCROLLUP },
266 { 'L', 0, MODEKEYCOPY_BOTTOMLINE },
267 { 'M', 0, MODEKEYCOPY_MIDDLELINE },
268 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
269 { 'T', 0, MODEKEYCOPY_JUMPTOBACK },
270 { 'W', 0, MODEKEYCOPY_NEXTSPACE },
271 { '\002' /* C-b */, 0, MODEKEYCOPY_PREVIOUSPAGE },
272 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
273 { '\004' /* C-d */, 0, MODEKEYCOPY_HALFPAGEDOWN },
274 { '\005' /* C-e */, 0, MODEKEYCOPY_SCROLLDOWN },
275 { '\006' /* C-f */, 0, MODEKEYCOPY_NEXTPAGE },
276 { '\010' /* C-h */, 0, MODEKEYCOPY_LEFT },
277 { '\025' /* C-u */, 0, MODEKEYCOPY_HALFPAGEUP },
278 { '\031' /* C-y */, 0, MODEKEYCOPY_SCROLLUP },
279 { '\033' /* Escape */, 0, MODEKEYCOPY_CLEARSELECTION },
280 { '\r', 0, MODEKEYCOPY_COPYSELECTION },
281 { '^', 0, MODEKEYCOPY_BACKTOINDENTATION },
282 { 'b', 0, MODEKEYCOPY_PREVIOUSWORD },
283 { 'e', 0, MODEKEYCOPY_NEXTWORDEND },
284 { 'f', 0, MODEKEYCOPY_JUMP },
285 { 'g', 0, MODEKEYCOPY_HISTORYTOP },
286 { 'h', 0, MODEKEYCOPY_LEFT },
287 { 'j', 0, MODEKEYCOPY_DOWN },
288 { 'k', 0, MODEKEYCOPY_UP },
289 { 'l', 0, MODEKEYCOPY_RIGHT },
290 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
291 { 't', 0, MODEKEYCOPY_JUMPTO },
292 { 'q', 0, MODEKEYCOPY_CANCEL },
293 { 'v', 0, MODEKEYCOPY_RECTANGLETOGGLE },
294 { 'w', 0, MODEKEYCOPY_NEXTWORD },
295 { KEYC_BSPACE, 0, MODEKEYCOPY_LEFT },
296 { KEYC_DOWN | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLDOWN },
297 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
298 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
299 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
300 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
301 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
302 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
303 { KEYC_UP, 0, MODEKEYCOPY_UP },
305 { 0, -1, 0 }
307 struct mode_key_tree mode_key_tree_vi_copy;
309 /* emacs editing keys. */
310 const struct mode_key_entry mode_key_emacs_edit[] = {
311 { '\001' /* C-a */, 0, MODEKEYEDIT_STARTOFLINE },
312 { '\002' /* C-b */, 0, MODEKEYEDIT_CURSORLEFT },
313 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
314 { '\004' /* C-d */, 0, MODEKEYEDIT_DELETE },
315 { '\005' /* C-e */, 0, MODEKEYEDIT_ENDOFLINE },
316 { '\006' /* C-f */, 0, MODEKEYEDIT_CURSORRIGHT },
317 { '\010' /* C-H */, 0, MODEKEYEDIT_BACKSPACE },
318 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
319 { '\013' /* C-k */, 0, MODEKEYEDIT_DELETETOENDOFLINE },
320 { '\016' /* C-n */, 0, MODEKEYEDIT_HISTORYDOWN },
321 { '\020' /* C-p */, 0, MODEKEYEDIT_HISTORYUP },
322 { '\024' /* C-t */, 0, MODEKEYEDIT_TRANSPOSECHARS },
323 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
324 { '\027' /* C-w */, 0, MODEKEYEDIT_DELETEWORD },
325 { '\031' /* C-y */, 0, MODEKEYEDIT_PASTE },
326 { '\033' /* Escape */, 0, MODEKEYEDIT_CANCEL },
327 { '\r', 0, MODEKEYEDIT_ENTER },
328 { 'b' | KEYC_ESCAPE, 0, MODEKEYEDIT_PREVIOUSWORD },
329 { 'f' | KEYC_ESCAPE, 0, MODEKEYEDIT_NEXTWORDEND },
330 { 'm' | KEYC_ESCAPE, 0, MODEKEYEDIT_STARTOFLINE },
331 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
332 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
333 { KEYC_DOWN, 0, MODEKEYEDIT_HISTORYDOWN },
334 { KEYC_LEFT, 0, MODEKEYEDIT_CURSORLEFT },
335 { KEYC_RIGHT, 0, MODEKEYEDIT_CURSORRIGHT },
336 { KEYC_UP, 0, MODEKEYEDIT_HISTORYUP },
337 { KEYC_HOME, 0, MODEKEYEDIT_STARTOFLINE },
338 { KEYC_END, 0, MODEKEYEDIT_ENDOFLINE },
340 { 0, -1, 0 }
342 struct mode_key_tree mode_key_tree_emacs_edit;
344 /* emacs choice selection keys. */
345 const struct mode_key_entry mode_key_emacs_choice[] = {
346 { '0' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
347 { '1' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
348 { '2' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
349 { '3' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
350 { '4' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
351 { '5' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
352 { '6' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
353 { '7' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
354 { '8' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
355 { '9' | KEYC_ESCAPE, 0, MODEKEYCHOICE_STARTNUMBERPREFIX },
356 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
357 { '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN },
358 { '\020' /* C-p */, 0, MODEKEYCHOICE_UP },
359 { '\026' /* C-v */, 0, MODEKEYCHOICE_PAGEDOWN },
360 { '\033' /* Escape */, 0, MODEKEYCHOICE_CANCEL },
361 { '\r', 0, MODEKEYCHOICE_CHOOSE },
362 { 'q', 0, MODEKEYCHOICE_CANCEL },
363 { 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP },
364 { KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE },
365 { KEYC_DOWN | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLDOWN },
366 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
367 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
368 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
369 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
370 { KEYC_UP, 0, MODEKEYCHOICE_UP },
371 { ' ', 0, MODEKEYCHOICE_TREE_TOGGLE },
372 { KEYC_LEFT, 0, MODEKEYCHOICE_TREE_COLLAPSE },
373 { KEYC_RIGHT, 0, MODEKEYCHOICE_TREE_EXPAND },
374 { KEYC_LEFT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
375 { KEYC_RIGHT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_EXPAND_ALL },
377 { 0, -1, 0 }
379 struct mode_key_tree mode_key_tree_emacs_choice;
381 /* emacs copy mode keys. */
382 const struct mode_key_entry mode_key_emacs_copy[] = {
383 { ' ', 0, MODEKEYCOPY_NEXTPAGE },
384 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
385 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
386 { '1' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
387 { '2' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
388 { '3' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
389 { '4' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
390 { '5' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
391 { '6' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
392 { '7' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
393 { '8' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
394 { '9' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
395 { '<' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYTOP },
396 { '>' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYBOTTOM },
397 { 'F', 0, MODEKEYCOPY_JUMPBACK },
398 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
399 { 'R' | KEYC_ESCAPE, 0, MODEKEYCOPY_TOPLINE },
400 { 'R', 0, MODEKEYCOPY_RECTANGLETOGGLE },
401 { 'T', 0, MODEKEYCOPY_JUMPTOBACK },
402 { '\000' /* C-Space */, 0, MODEKEYCOPY_STARTSELECTION },
403 { '\001' /* C-a */, 0, MODEKEYCOPY_STARTOFLINE },
404 { '\002' /* C-b */, 0, MODEKEYCOPY_LEFT },
405 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
406 { '\005' /* C-e */, 0, MODEKEYCOPY_ENDOFLINE },
407 { '\006' /* C-f */, 0, MODEKEYCOPY_RIGHT },
408 { '\007' /* C-g */, 0, MODEKEYCOPY_CLEARSELECTION },
409 { '\013' /* C-k */, 0, MODEKEYCOPY_COPYENDOFLINE },
410 { '\016' /* C-n */, 0, MODEKEYCOPY_DOWN },
411 { '\020' /* C-p */, 0, MODEKEYCOPY_UP },
412 { '\022' /* C-r */, 0, MODEKEYCOPY_SEARCHUP },
413 { '\023' /* C-s */, 0, MODEKEYCOPY_SEARCHDOWN },
414 { '\026' /* C-v */, 0, MODEKEYCOPY_NEXTPAGE },
415 { '\027' /* C-w */, 0, MODEKEYCOPY_COPYSELECTION },
416 { '\033' /* Escape */, 0, MODEKEYCOPY_CANCEL },
417 { 'b' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSWORD },
418 { 'f', 0, MODEKEYCOPY_JUMP },
419 { 'f' | KEYC_ESCAPE, 0, MODEKEYCOPY_NEXTWORDEND },
420 { 'g', 0, MODEKEYCOPY_GOTOLINE },
421 { 'm' | KEYC_ESCAPE, 0, MODEKEYCOPY_BACKTOINDENTATION },
422 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
423 { 'q', 0, MODEKEYCOPY_CANCEL },
424 { 'r' | KEYC_ESCAPE, 0, MODEKEYCOPY_MIDDLELINE },
425 { 't', 0, MODEKEYCOPY_JUMPTO },
426 { 'v' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSPAGE },
427 { 'w' | KEYC_ESCAPE, 0, MODEKEYCOPY_COPYSELECTION },
428 { KEYC_DOWN | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLDOWN },
429 { KEYC_DOWN | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEDOWN },
430 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
431 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
432 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
433 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
434 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
435 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
436 { KEYC_UP | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEUP },
437 { KEYC_UP, 0, MODEKEYCOPY_UP },
439 { 0, -1, 0 }
441 struct mode_key_tree mode_key_tree_emacs_copy;
443 /* Table mapping key table names to default settings and trees. */
444 const struct mode_key_table mode_key_tables[] = {
445 { "vi-edit", mode_key_cmdstr_edit,
446 &mode_key_tree_vi_edit, mode_key_vi_edit },
447 { "vi-choice", mode_key_cmdstr_choice,
448 &mode_key_tree_vi_choice, mode_key_vi_choice },
449 { "vi-copy", mode_key_cmdstr_copy,
450 &mode_key_tree_vi_copy, mode_key_vi_copy },
451 { "emacs-edit", mode_key_cmdstr_edit,
452 &mode_key_tree_emacs_edit, mode_key_emacs_edit },
453 { "emacs-choice", mode_key_cmdstr_choice,
454 &mode_key_tree_emacs_choice, mode_key_emacs_choice },
455 { "emacs-copy", mode_key_cmdstr_copy,
456 &mode_key_tree_emacs_copy, mode_key_emacs_copy },
458 { NULL, NULL, NULL, NULL }
461 RB_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
464 mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
466 if (mbind1->mode != mbind2->mode)
467 return (mbind1->mode - mbind2->mode);
468 return (mbind1->key - mbind2->key);
471 const char *
472 mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
474 for (; cmdstr->name != NULL; cmdstr++) {
475 if (cmdstr->cmd == cmd)
476 return (cmdstr->name);
478 return (NULL);
481 enum mode_key_cmd
482 mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
484 for (; cmdstr->name != NULL; cmdstr++) {
485 if (strcasecmp(cmdstr->name, name) == 0)
486 return (cmdstr->cmd);
488 return (MODEKEY_NONE);
491 const struct mode_key_table *
492 mode_key_findtable(const char *name)
494 const struct mode_key_table *mtab;
496 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
497 if (strcasecmp(name, mtab->name) == 0)
498 return (mtab);
500 return (NULL);
503 void
504 mode_key_init_trees(void)
506 const struct mode_key_table *mtab;
507 const struct mode_key_entry *ment;
508 struct mode_key_binding *mbind;
510 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
511 RB_INIT(mtab->tree);
512 for (ment = mtab->table; ment->mode != -1; ment++) {
513 mbind = xmalloc(sizeof *mbind);
514 mbind->key = ment->key;
515 mbind->mode = ment->mode;
516 mbind->cmd = ment->cmd;
517 mbind->arg = NULL;
518 RB_INSERT(mode_key_tree, mtab->tree, mbind);
523 void
524 mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
526 mdata->tree = mtree;
527 mdata->mode = 0;
530 enum mode_key_cmd
531 mode_key_lookup(struct mode_key_data *mdata, int key, const char **arg)
533 struct mode_key_binding *mbind, mtmp;
535 mtmp.key = key;
536 mtmp.mode = mdata->mode;
537 if ((mbind = RB_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) {
538 if (mdata->mode != 0)
539 return (MODEKEY_NONE);
540 return (MODEKEY_OTHER);
543 switch (mbind->cmd) {
544 case MODEKEYEDIT_SWITCHMODE:
545 case MODEKEYEDIT_SWITCHMODEAPPEND:
546 case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
547 case MODEKEYEDIT_SWITCHMODEBEGINLINE:
548 mdata->mode = 1 - mdata->mode;
549 /* FALLTHROUGH */
550 default:
551 if (arg != NULL)
552 *arg = mbind->arg;
553 return (mbind->cmd);