Add a flag to cmd_find_session so that attach-session can prefer
[tmux-openbsd.git] / mode-key.c
blob823c44292496c29d6070ae3a9e7a3292cb64153a
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 and MODEKEYEDIT_SWITCHMODEAPPEND) are special-cased
39 * to do this.
42 /* Edit keys command strings. */
43 const struct mode_key_cmdstr mode_key_cmdstr_edit[] = {
44 { MODEKEYEDIT_BACKSPACE, "backspace" },
45 { MODEKEYEDIT_CANCEL, "cancel" },
46 { MODEKEYEDIT_COMPLETE, "complete" },
47 { MODEKEYEDIT_CURSORLEFT, "cursor-left" },
48 { MODEKEYEDIT_CURSORRIGHT, "cursor-right" },
49 { MODEKEYEDIT_DELETE, "delete" },
50 { MODEKEYEDIT_DELETELINE, "delete-line" },
51 { MODEKEYEDIT_DELETETOENDOFLINE, "delete-end-of-line" },
52 { MODEKEYEDIT_ENDOFLINE, "end-of-line" },
53 { MODEKEYEDIT_ENTER, "enter" },
54 { MODEKEYEDIT_HISTORYDOWN, "history-down" },
55 { MODEKEYEDIT_HISTORYUP, "history-up" },
56 { MODEKEYEDIT_PASTE, "paste" },
57 { MODEKEYEDIT_STARTOFLINE, "start-of-line" },
58 { MODEKEYEDIT_SWITCHMODE, "switch-mode" },
59 { MODEKEYEDIT_SWITCHMODEAPPEND, "switch-mode-append" },
60 { MODEKEYEDIT_TRANSPOSECHARS, "transpose-chars" },
62 { 0, NULL }
65 /* Choice keys command strings. */
66 const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
67 { MODEKEYCHOICE_CANCEL, "cancel" },
68 { MODEKEYCHOICE_CHOOSE, "choose" },
69 { MODEKEYCHOICE_DOWN, "down" },
70 { MODEKEYCHOICE_PAGEDOWN, "page-down" },
71 { MODEKEYCHOICE_PAGEUP, "page-up" },
72 { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
73 { MODEKEYCHOICE_SCROLLUP, "scroll-up" },
74 { MODEKEYCHOICE_UP, "up" },
76 { 0, NULL }
79 /* Copy keys command strings. */
80 const struct mode_key_cmdstr mode_key_cmdstr_copy[] = {
81 { MODEKEYCOPY_BACKTOINDENTATION, "back-to-indentation" },
82 { MODEKEYCOPY_BOTTOMLINE, "bottom-line" },
83 { MODEKEYCOPY_CANCEL, "cancel" },
84 { MODEKEYCOPY_CLEARSELECTION, "clear-selection" },
85 { MODEKEYCOPY_COPYSELECTION, "copy-selection" },
86 { MODEKEYCOPY_DOWN, "cursor-down" },
87 { MODEKEYCOPY_ENDOFLINE, "end-of-line" },
88 { MODEKEYCOPY_GOTOLINE, "goto-line" },
89 { MODEKEYCOPY_HISTORYBOTTOM, "history-bottom" },
90 { MODEKEYCOPY_HISTORYTOP, "history-top" },
91 { MODEKEYCOPY_JUMP, "jump-forward" },
92 { MODEKEYCOPY_JUMPAGAIN, "jump-again" },
93 { MODEKEYCOPY_JUMPREVERSE, "jump-reverse" },
94 { MODEKEYCOPY_JUMPBACK, "jump-backward" },
95 { MODEKEYCOPY_LEFT, "cursor-left" },
96 { MODEKEYCOPY_RECTANGLETOGGLE, "rectangle-toggle" },
97 { MODEKEYCOPY_MIDDLELINE, "middle-line" },
98 { MODEKEYCOPY_NEXTPAGE, "page-down" },
99 { MODEKEYCOPY_NEXTSPACE, "next-space" },
100 { MODEKEYCOPY_NEXTSPACEEND, "next-space-end" },
101 { MODEKEYCOPY_NEXTWORD, "next-word" },
102 { MODEKEYCOPY_NEXTWORDEND, "next-word-end" },
103 { MODEKEYCOPY_PREVIOUSPAGE, "page-up" },
104 { MODEKEYCOPY_PREVIOUSSPACE, "previous-space" },
105 { MODEKEYCOPY_PREVIOUSWORD, "previous-word" },
106 { MODEKEYCOPY_RIGHT, "cursor-right" },
107 { MODEKEYCOPY_SCROLLDOWN, "scroll-down" },
108 { MODEKEYCOPY_SCROLLUP, "scroll-up" },
109 { MODEKEYCOPY_SEARCHAGAIN, "search-again" },
110 { MODEKEYCOPY_SEARCHDOWN, "search-forward" },
111 { MODEKEYCOPY_SEARCHREVERSE, "search-reverse" },
112 { MODEKEYCOPY_SEARCHUP, "search-backward" },
113 { MODEKEYCOPY_STARTNUMBERPREFIX, "start-number-prefix" },
114 { MODEKEYCOPY_STARTOFLINE, "start-of-line" },
115 { MODEKEYCOPY_STARTSELECTION, "begin-selection" },
116 { MODEKEYCOPY_TOPLINE, "top-line" },
117 { MODEKEYCOPY_UP, "cursor-up" },
119 { 0, NULL }
122 /* vi editing keys. */
123 const struct mode_key_entry mode_key_vi_edit[] = {
124 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
125 { '\010' /* C-h */, 0, MODEKEYEDIT_BACKSPACE },
126 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
127 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
128 { '\033' /* Escape */, 0, MODEKEYEDIT_SWITCHMODE },
129 { '\r', 0, MODEKEYEDIT_ENTER },
130 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
131 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
133 { '$', 1, MODEKEYEDIT_ENDOFLINE },
134 { '0', 1, MODEKEYEDIT_STARTOFLINE },
135 { 'D', 1, MODEKEYEDIT_DELETETOENDOFLINE },
136 { '\003' /* C-c */, 1, MODEKEYEDIT_CANCEL },
137 { '\010' /* C-h */, 1, MODEKEYEDIT_BACKSPACE },
138 { '\r', 1, MODEKEYEDIT_ENTER },
139 { '^', 1, MODEKEYEDIT_STARTOFLINE },
140 { 'a', 1, MODEKEYEDIT_SWITCHMODEAPPEND },
141 { 'd', 1, MODEKEYEDIT_DELETELINE },
142 { 'h', 1, MODEKEYEDIT_CURSORLEFT },
143 { 'i', 1, MODEKEYEDIT_SWITCHMODE },
144 { 'j', 1, MODEKEYEDIT_HISTORYDOWN },
145 { 'k', 1, MODEKEYEDIT_HISTORYUP },
146 { 'l', 1, MODEKEYEDIT_CURSORRIGHT },
147 { 'p', 1, MODEKEYEDIT_PASTE },
148 { KEYC_BSPACE, 1, MODEKEYEDIT_BACKSPACE },
149 { KEYC_DC, 1, MODEKEYEDIT_DELETE },
150 { KEYC_DOWN, 1, MODEKEYEDIT_HISTORYDOWN },
151 { KEYC_LEFT, 1, MODEKEYEDIT_CURSORLEFT },
152 { KEYC_RIGHT, 1, MODEKEYEDIT_CURSORRIGHT },
153 { KEYC_UP, 1, MODEKEYEDIT_HISTORYUP },
155 { 0, -1, 0 }
157 struct mode_key_tree mode_key_tree_vi_edit;
159 /* vi choice selection keys. */
160 const struct mode_key_entry mode_key_vi_choice[] = {
161 { '\002' /* C-b */, 0, MODEKEYCHOICE_PAGEUP },
162 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
163 { '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN },
164 { '\006' /* C-f */, 0, MODEKEYCHOICE_PAGEDOWN },
165 { '\031' /* C-y */, 0, MODEKEYCHOICE_SCROLLUP },
166 { '\r', 0, MODEKEYCHOICE_CHOOSE },
167 { 'j', 0, MODEKEYCHOICE_DOWN },
168 { 'k', 0, MODEKEYCHOICE_UP },
169 { 'q', 0, MODEKEYCHOICE_CANCEL },
170 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
171 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
172 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
173 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
174 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
175 { KEYC_UP, 0, MODEKEYCHOICE_UP },
177 { 0, -1, 0 }
179 struct mode_key_tree mode_key_tree_vi_choice;
181 /* vi copy mode keys. */
182 const struct mode_key_entry mode_key_vi_copy[] = {
183 { ' ', 0, MODEKEYCOPY_STARTSELECTION },
184 { '$', 0, MODEKEYCOPY_ENDOFLINE },
185 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
186 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
187 { '/', 0, MODEKEYCOPY_SEARCHDOWN },
188 { '0', 0, MODEKEYCOPY_STARTOFLINE },
189 { '1', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
190 { '2', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
191 { '3', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
192 { '4', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
193 { '5', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
194 { '6', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
195 { '7', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
196 { '8', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
197 { '9', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
198 { ':', 0, MODEKEYCOPY_GOTOLINE },
199 { '?', 0, MODEKEYCOPY_SEARCHUP },
200 { 'B', 0, MODEKEYCOPY_PREVIOUSSPACE },
201 { 'E', 0, MODEKEYCOPY_NEXTSPACEEND },
202 { 'F', 0, MODEKEYCOPY_JUMPBACK },
203 { 'G', 0, MODEKEYCOPY_HISTORYBOTTOM },
204 { 'H', 0, MODEKEYCOPY_TOPLINE },
205 { 'J', 0, MODEKEYCOPY_SCROLLDOWN },
206 { 'K', 0, MODEKEYCOPY_SCROLLUP },
207 { 'L', 0, MODEKEYCOPY_BOTTOMLINE },
208 { 'M', 0, MODEKEYCOPY_MIDDLELINE },
209 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
210 { 'W', 0, MODEKEYCOPY_NEXTSPACE },
211 { '\002' /* C-b */, 0, MODEKEYCOPY_PREVIOUSPAGE },
212 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
213 { '\004' /* C-d */, 0, MODEKEYCOPY_HALFPAGEDOWN },
214 { '\005' /* C-e */, 0, MODEKEYCOPY_SCROLLDOWN },
215 { '\006' /* C-f */, 0, MODEKEYCOPY_NEXTPAGE },
216 { '\010' /* C-h */, 0, MODEKEYCOPY_LEFT },
217 { '\025' /* C-u */, 0, MODEKEYCOPY_HALFPAGEUP },
218 { '\031' /* C-y */, 0, MODEKEYCOPY_SCROLLUP },
219 { '\033' /* Escape */, 0, MODEKEYCOPY_CLEARSELECTION },
220 { '\r', 0, MODEKEYCOPY_COPYSELECTION },
221 { '^', 0, MODEKEYCOPY_BACKTOINDENTATION },
222 { 'b', 0, MODEKEYCOPY_PREVIOUSWORD },
223 { 'e', 0, MODEKEYCOPY_NEXTWORDEND },
224 { 'f', 0, MODEKEYCOPY_JUMP },
225 { 'g', 0, MODEKEYCOPY_HISTORYTOP },
226 { 'h', 0, MODEKEYCOPY_LEFT },
227 { 'j', 0, MODEKEYCOPY_DOWN },
228 { 'k', 0, MODEKEYCOPY_UP },
229 { 'l', 0, MODEKEYCOPY_RIGHT },
230 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
231 { 'q', 0, MODEKEYCOPY_CANCEL },
232 { 'v', 0, MODEKEYCOPY_RECTANGLETOGGLE },
233 { 'w', 0, MODEKEYCOPY_NEXTWORD },
234 { KEYC_BSPACE, 0, MODEKEYCOPY_LEFT },
235 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
236 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
237 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
238 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
239 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
240 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
241 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
242 { KEYC_UP, 0, MODEKEYCOPY_UP },
244 { 0, -1, 0 }
246 struct mode_key_tree mode_key_tree_vi_copy;
248 /* emacs editing keys. */
249 const struct mode_key_entry mode_key_emacs_edit[] = {
250 { '\001' /* C-a */, 0, MODEKEYEDIT_STARTOFLINE },
251 { '\002' /* C-b */, 0, MODEKEYEDIT_CURSORLEFT },
252 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
253 { '\004' /* C-d */, 0, MODEKEYEDIT_DELETE },
254 { '\005' /* C-e */, 0, MODEKEYEDIT_ENDOFLINE },
255 { '\006' /* C-f */, 0, MODEKEYEDIT_CURSORRIGHT },
256 { '\010' /* C-H */, 0, MODEKEYEDIT_BACKSPACE },
257 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
258 { '\013' /* C-k */, 0, MODEKEYEDIT_DELETETOENDOFLINE },
259 { '\016' /* C-n */, 0, MODEKEYEDIT_HISTORYDOWN },
260 { '\020' /* C-p */, 0, MODEKEYEDIT_HISTORYUP },
261 { '\024' /* C-t */, 0, MODEKEYEDIT_TRANSPOSECHARS },
262 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
263 { '\031' /* C-y */, 0, MODEKEYEDIT_PASTE },
264 { '\033' /* Escape */, 0, MODEKEYEDIT_CANCEL },
265 { '\r', 0, MODEKEYEDIT_ENTER },
266 { 'm' | KEYC_ESCAPE, 0, MODEKEYEDIT_STARTOFLINE },
267 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
268 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
269 { KEYC_DOWN, 0, MODEKEYEDIT_HISTORYDOWN },
270 { KEYC_LEFT, 0, MODEKEYEDIT_CURSORLEFT },
271 { KEYC_RIGHT, 0, MODEKEYEDIT_CURSORRIGHT },
272 { KEYC_UP, 0, MODEKEYEDIT_HISTORYUP },
274 { 0, -1, 0 }
276 struct mode_key_tree mode_key_tree_emacs_edit;
278 /* emacs choice selection keys. */
279 const struct mode_key_entry mode_key_emacs_choice[] = {
280 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
281 { '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN },
282 { '\020' /* C-p */, 0, MODEKEYCHOICE_UP },
283 { '\026' /* C-v */, 0, MODEKEYCHOICE_PAGEDOWN },
284 { '\033' /* Escape */, 0, MODEKEYCHOICE_CANCEL },
285 { '\r', 0, MODEKEYCHOICE_CHOOSE },
286 { 'q', 0, MODEKEYCHOICE_CANCEL },
287 { 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP },
288 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
289 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
290 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
291 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
292 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
293 { KEYC_UP, 0, MODEKEYCHOICE_UP },
295 { 0, -1, 0 }
297 struct mode_key_tree mode_key_tree_emacs_choice;
299 /* emacs copy mode keys. */
300 const struct mode_key_entry mode_key_emacs_copy[] = {
301 { ' ', 0, MODEKEYCOPY_NEXTPAGE },
302 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
303 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
304 { '1' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
305 { '2' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
306 { '3' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
307 { '4' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
308 { '5' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
309 { '6' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
310 { '7' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
311 { '8' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
312 { '9' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
313 { '<' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYTOP },
314 { '>' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYBOTTOM },
315 { 'F', 0, MODEKEYCOPY_JUMPBACK },
316 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
317 { 'R' | KEYC_ESCAPE, 0, MODEKEYCOPY_TOPLINE },
318 { 'R', 0, MODEKEYCOPY_RECTANGLETOGGLE },
319 { '\000' /* C-Space */, 0, MODEKEYCOPY_STARTSELECTION },
320 { '\001' /* C-a */, 0, MODEKEYCOPY_STARTOFLINE },
321 { '\002' /* C-b */, 0, MODEKEYCOPY_LEFT },
322 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
323 { '\005' /* C-e */, 0, MODEKEYCOPY_ENDOFLINE },
324 { '\006' /* C-f */, 0, MODEKEYCOPY_RIGHT },
325 { '\007' /* C-g */, 0, MODEKEYCOPY_CLEARSELECTION },
326 { '\016' /* C-n */, 0, MODEKEYCOPY_DOWN },
327 { '\020' /* C-p */, 0, MODEKEYCOPY_UP },
328 { '\022' /* C-r */, 0, MODEKEYCOPY_SEARCHUP },
329 { '\023' /* C-s */, 0, MODEKEYCOPY_SEARCHDOWN },
330 { '\026' /* C-v */, 0, MODEKEYCOPY_NEXTPAGE },
331 { '\027' /* C-w */, 0, MODEKEYCOPY_COPYSELECTION },
332 { '\033' /* Escape */, 0, MODEKEYCOPY_CANCEL },
333 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
334 { 'b' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSWORD },
335 { 'f', 0, MODEKEYCOPY_JUMP },
336 { 'f' | KEYC_ESCAPE, 0, MODEKEYCOPY_NEXTWORDEND },
337 { 'g', 0, MODEKEYCOPY_GOTOLINE },
338 { 'm' | KEYC_ESCAPE, 0, MODEKEYCOPY_BACKTOINDENTATION },
339 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
340 { 'q', 0, MODEKEYCOPY_CANCEL },
341 { 'r' | KEYC_ESCAPE, 0, MODEKEYCOPY_MIDDLELINE },
342 { 'v' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSPAGE },
343 { 'w' | KEYC_ESCAPE, 0, MODEKEYCOPY_COPYSELECTION },
344 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
345 { KEYC_DOWN | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEDOWN },
346 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
347 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
348 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
349 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
350 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
351 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
352 { KEYC_UP | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEUP },
353 { KEYC_UP, 0, MODEKEYCOPY_UP },
355 { 0, -1, 0 }
357 struct mode_key_tree mode_key_tree_emacs_copy;
359 /* Table mapping key table names to default settings and trees. */
360 const struct mode_key_table mode_key_tables[] = {
361 { "vi-edit", mode_key_cmdstr_edit,
362 &mode_key_tree_vi_edit, mode_key_vi_edit },
363 { "vi-choice", mode_key_cmdstr_choice,
364 &mode_key_tree_vi_choice, mode_key_vi_choice },
365 { "vi-copy", mode_key_cmdstr_copy,
366 &mode_key_tree_vi_copy, mode_key_vi_copy },
367 { "emacs-edit", mode_key_cmdstr_edit,
368 &mode_key_tree_emacs_edit, mode_key_emacs_edit },
369 { "emacs-choice", mode_key_cmdstr_choice,
370 &mode_key_tree_emacs_choice, mode_key_emacs_choice },
371 { "emacs-copy", mode_key_cmdstr_copy,
372 &mode_key_tree_emacs_copy, mode_key_emacs_copy },
374 { NULL, NULL, NULL, NULL }
377 SPLAY_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
380 mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
382 if (mbind1->mode != mbind2->mode)
383 return (mbind1->mode - mbind2->mode);
384 return (mbind1->key - mbind2->key);
387 const char *
388 mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
390 for (; cmdstr->name != NULL; cmdstr++) {
391 if (cmdstr->cmd == cmd)
392 return (cmdstr->name);
394 return (NULL);
397 enum mode_key_cmd
398 mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
400 for (; cmdstr->name != NULL; cmdstr++) {
401 if (strcasecmp(cmdstr->name, name) == 0)
402 return (cmdstr->cmd);
404 return (MODEKEY_NONE);
407 const struct mode_key_table *
408 mode_key_findtable(const char *name)
410 const struct mode_key_table *mtab;
412 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
413 if (strcasecmp(name, mtab->name) == 0)
414 return (mtab);
416 return (NULL);
419 void
420 mode_key_init_trees(void)
422 const struct mode_key_table *mtab;
423 const struct mode_key_entry *ment;
424 struct mode_key_binding *mbind;
426 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
427 SPLAY_INIT(mtab->tree);
428 for (ment = mtab->table; ment->mode != -1; ment++) {
429 mbind = xmalloc(sizeof *mbind);
430 mbind->key = ment->key;
431 mbind->mode = ment->mode;
432 mbind->cmd = ment->cmd;
433 SPLAY_INSERT(mode_key_tree, mtab->tree, mbind);
438 void
439 mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
441 mdata->tree = mtree;
442 mdata->mode = 0;
445 enum mode_key_cmd
446 mode_key_lookup(struct mode_key_data *mdata, int key)
448 struct mode_key_binding *mbind, mtmp;
450 mtmp.key = key;
451 mtmp.mode = mdata->mode;
452 if ((mbind = SPLAY_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) {
453 if (mdata->mode != 0)
454 return (MODEKEY_NONE);
455 return (MODEKEY_OTHER);
458 switch (mbind->cmd) {
459 case MODEKEYEDIT_SWITCHMODE:
460 case MODEKEYEDIT_SWITCHMODEAPPEND:
461 mdata->mode = 1 - mdata->mode;
462 /* FALLTHROUGH */
463 default:
464 return (mbind->cmd);