Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / mode-key.c
blob43d07c0f254906863cf90e3b89bf82289a1c5200
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_DELETEWORD, "delete-word" },
53 { MODEKEYEDIT_ENDOFLINE, "end-of-line" },
54 { MODEKEYEDIT_ENTER, "enter" },
55 { MODEKEYEDIT_HISTORYDOWN, "history-down" },
56 { MODEKEYEDIT_HISTORYUP, "history-up" },
57 { MODEKEYEDIT_NEXTSPACE, "next-space" },
58 { MODEKEYEDIT_NEXTSPACEEND, "next-space-end" },
59 { MODEKEYEDIT_NEXTWORD, "next-word" },
60 { MODEKEYEDIT_NEXTWORDEND, "next-word-end" },
61 { MODEKEYEDIT_PASTE, "paste" },
62 { MODEKEYEDIT_PREVIOUSSPACE, "previous-space" },
63 { MODEKEYEDIT_PREVIOUSWORD, "previous-word" },
64 { MODEKEYEDIT_STARTOFLINE, "start-of-line" },
65 { MODEKEYEDIT_SWITCHMODE, "switch-mode" },
66 { MODEKEYEDIT_SWITCHMODEAPPEND, "switch-mode-append" },
67 { MODEKEYEDIT_TRANSPOSECHARS, "transpose-chars" },
69 { 0, NULL }
72 /* Choice keys command strings. */
73 const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
74 { MODEKEYCHOICE_CANCEL, "cancel" },
75 { MODEKEYCHOICE_CHOOSE, "choose" },
76 { MODEKEYCHOICE_DOWN, "down" },
77 { MODEKEYCHOICE_PAGEDOWN, "page-down" },
78 { MODEKEYCHOICE_PAGEUP, "page-up" },
79 { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
80 { MODEKEYCHOICE_SCROLLUP, "scroll-up" },
81 { MODEKEYCHOICE_UP, "up" },
83 { 0, NULL }
86 /* Copy keys command strings. */
87 const struct mode_key_cmdstr mode_key_cmdstr_copy[] = {
88 { MODEKEYCOPY_BACKTOINDENTATION, "back-to-indentation" },
89 { MODEKEYCOPY_BOTTOMLINE, "bottom-line" },
90 { MODEKEYCOPY_CANCEL, "cancel" },
91 { MODEKEYCOPY_CLEARSELECTION, "clear-selection" },
92 { MODEKEYCOPY_COPYLINE, "copy-line" },
93 { MODEKEYCOPY_COPYENDOFLINE, "copy-end-of-line" },
94 { MODEKEYCOPY_COPYSELECTION, "copy-selection" },
95 { MODEKEYCOPY_DOWN, "cursor-down" },
96 { MODEKEYCOPY_ENDOFLINE, "end-of-line" },
97 { MODEKEYCOPY_GOTOLINE, "goto-line" },
98 { MODEKEYCOPY_HISTORYBOTTOM, "history-bottom" },
99 { MODEKEYCOPY_HISTORYTOP, "history-top" },
100 { MODEKEYCOPY_JUMP, "jump-forward" },
101 { MODEKEYCOPY_JUMPAGAIN, "jump-again" },
102 { MODEKEYCOPY_JUMPREVERSE, "jump-reverse" },
103 { MODEKEYCOPY_JUMPBACK, "jump-backward" },
104 { MODEKEYCOPY_JUMPTO, "jump-to-forward" },
105 { MODEKEYCOPY_JUMPTOBACK, "jump-to-backward" },
106 { MODEKEYCOPY_LEFT, "cursor-left" },
107 { MODEKEYCOPY_RECTANGLETOGGLE, "rectangle-toggle" },
108 { MODEKEYCOPY_MIDDLELINE, "middle-line" },
109 { MODEKEYCOPY_NEXTPAGE, "page-down" },
110 { MODEKEYCOPY_NEXTSPACE, "next-space" },
111 { MODEKEYCOPY_NEXTSPACEEND, "next-space-end" },
112 { MODEKEYCOPY_NEXTWORD, "next-word" },
113 { MODEKEYCOPY_NEXTWORDEND, "next-word-end" },
114 { MODEKEYCOPY_PREVIOUSPAGE, "page-up" },
115 { MODEKEYCOPY_PREVIOUSSPACE, "previous-space" },
116 { MODEKEYCOPY_PREVIOUSWORD, "previous-word" },
117 { MODEKEYCOPY_RIGHT, "cursor-right" },
118 { MODEKEYCOPY_SCROLLDOWN, "scroll-down" },
119 { MODEKEYCOPY_SCROLLUP, "scroll-up" },
120 { MODEKEYCOPY_SEARCHAGAIN, "search-again" },
121 { MODEKEYCOPY_SEARCHDOWN, "search-forward" },
122 { MODEKEYCOPY_SEARCHREVERSE, "search-reverse" },
123 { MODEKEYCOPY_SEARCHUP, "search-backward" },
124 { MODEKEYCOPY_SELECTLINE, "select-line" },
125 { MODEKEYCOPY_STARTNUMBERPREFIX, "start-number-prefix" },
126 { MODEKEYCOPY_STARTOFLINE, "start-of-line" },
127 { MODEKEYCOPY_STARTSELECTION, "begin-selection" },
128 { MODEKEYCOPY_TOPLINE, "top-line" },
129 { MODEKEYCOPY_UP, "cursor-up" },
131 { 0, NULL }
134 /* vi editing keys. */
135 const struct mode_key_entry mode_key_vi_edit[] = {
136 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
137 { '\010' /* C-h */, 0, MODEKEYEDIT_BACKSPACE },
138 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
139 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
140 { '\027' /* C-w */, 0, MODEKEYEDIT_DELETEWORD },
141 { '\033' /* Escape */, 0, MODEKEYEDIT_SWITCHMODE },
142 { '\r', 0, MODEKEYEDIT_ENTER },
143 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
144 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
145 { KEYC_DOWN, 0, MODEKEYEDIT_HISTORYDOWN },
146 { KEYC_LEFT, 0, MODEKEYEDIT_CURSORLEFT },
147 { KEYC_RIGHT, 0, MODEKEYEDIT_CURSORRIGHT },
148 { KEYC_UP, 0, MODEKEYEDIT_HISTORYUP },
149 { KEYC_HOME, 0, MODEKEYEDIT_STARTOFLINE },
150 { KEYC_END, 0, MODEKEYEDIT_ENDOFLINE },
152 { '$', 1, MODEKEYEDIT_ENDOFLINE },
153 { '0', 1, MODEKEYEDIT_STARTOFLINE },
154 { 'B', 1, MODEKEYEDIT_PREVIOUSSPACE },
155 { 'D', 1, MODEKEYEDIT_DELETETOENDOFLINE },
156 { 'E', 1, MODEKEYEDIT_NEXTSPACEEND },
157 { 'W', 1, MODEKEYEDIT_NEXTSPACE },
158 { 'X', 1, MODEKEYEDIT_BACKSPACE },
159 { '\003' /* C-c */, 1, MODEKEYEDIT_CANCEL },
160 { '\010' /* C-h */, 1, MODEKEYEDIT_BACKSPACE },
161 { '\r', 1, MODEKEYEDIT_ENTER },
162 { '^', 1, MODEKEYEDIT_STARTOFLINE },
163 { 'a', 1, MODEKEYEDIT_SWITCHMODEAPPEND },
164 { 'b', 1, MODEKEYEDIT_PREVIOUSWORD },
165 { 'd', 1, MODEKEYEDIT_DELETELINE },
166 { 'e', 1, MODEKEYEDIT_NEXTWORDEND },
167 { 'h', 1, MODEKEYEDIT_CURSORLEFT },
168 { 'i', 1, MODEKEYEDIT_SWITCHMODE },
169 { 'j', 1, MODEKEYEDIT_HISTORYDOWN },
170 { 'k', 1, MODEKEYEDIT_HISTORYUP },
171 { 'l', 1, MODEKEYEDIT_CURSORRIGHT },
172 { 'p', 1, MODEKEYEDIT_PASTE },
173 { 'w', 1, MODEKEYEDIT_NEXTWORD },
174 { 'x', 1, MODEKEYEDIT_DELETE },
175 { KEYC_BSPACE, 1, MODEKEYEDIT_BACKSPACE },
176 { KEYC_DC, 1, MODEKEYEDIT_DELETE },
177 { KEYC_DOWN, 1, MODEKEYEDIT_HISTORYDOWN },
178 { KEYC_LEFT, 1, MODEKEYEDIT_CURSORLEFT },
179 { KEYC_RIGHT, 1, MODEKEYEDIT_CURSORRIGHT },
180 { KEYC_UP, 1, MODEKEYEDIT_HISTORYUP },
182 { 0, -1, 0 }
184 struct mode_key_tree mode_key_tree_vi_edit;
186 /* vi choice selection keys. */
187 const struct mode_key_entry mode_key_vi_choice[] = {
188 { '\002' /* C-b */, 0, MODEKEYCHOICE_PAGEUP },
189 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
190 { '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN },
191 { '\006' /* C-f */, 0, MODEKEYCHOICE_PAGEDOWN },
192 { '\031' /* C-y */, 0, MODEKEYCHOICE_SCROLLUP },
193 { '\r', 0, MODEKEYCHOICE_CHOOSE },
194 { 'j', 0, MODEKEYCHOICE_DOWN },
195 { 'k', 0, MODEKEYCHOICE_UP },
196 { 'q', 0, MODEKEYCHOICE_CANCEL },
197 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
198 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
199 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
200 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
201 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
202 { KEYC_UP, 0, MODEKEYCHOICE_UP },
204 { 0, -1, 0 }
206 struct mode_key_tree mode_key_tree_vi_choice;
208 /* vi copy mode keys. */
209 const struct mode_key_entry mode_key_vi_copy[] = {
210 { ' ', 0, MODEKEYCOPY_STARTSELECTION },
211 { '$', 0, MODEKEYCOPY_ENDOFLINE },
212 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
213 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
214 { '/', 0, MODEKEYCOPY_SEARCHDOWN },
215 { '0', 0, MODEKEYCOPY_STARTOFLINE },
216 { '1', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
217 { '2', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
218 { '3', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
219 { '4', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
220 { '5', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
221 { '6', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
222 { '7', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
223 { '8', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
224 { '9', 0, MODEKEYCOPY_STARTNUMBERPREFIX },
225 { ':', 0, MODEKEYCOPY_GOTOLINE },
226 { '?', 0, MODEKEYCOPY_SEARCHUP },
227 { 'B', 0, MODEKEYCOPY_PREVIOUSSPACE },
228 { 'D', 0, MODEKEYCOPY_COPYENDOFLINE },
229 { 'E', 0, MODEKEYCOPY_NEXTSPACEEND },
230 { 'F', 0, MODEKEYCOPY_JUMPBACK },
231 { 'G', 0, MODEKEYCOPY_HISTORYBOTTOM },
232 { 'H', 0, MODEKEYCOPY_TOPLINE },
233 { 'J', 0, MODEKEYCOPY_SCROLLDOWN },
234 { 'K', 0, MODEKEYCOPY_SCROLLUP },
235 { 'L', 0, MODEKEYCOPY_BOTTOMLINE },
236 { 'M', 0, MODEKEYCOPY_MIDDLELINE },
237 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
238 { 'T', 0, MODEKEYCOPY_JUMPTOBACK },
239 { 'W', 0, MODEKEYCOPY_NEXTSPACE },
240 { '\002' /* C-b */, 0, MODEKEYCOPY_PREVIOUSPAGE },
241 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
242 { '\004' /* C-d */, 0, MODEKEYCOPY_HALFPAGEDOWN },
243 { '\005' /* C-e */, 0, MODEKEYCOPY_SCROLLDOWN },
244 { '\006' /* C-f */, 0, MODEKEYCOPY_NEXTPAGE },
245 { '\010' /* C-h */, 0, MODEKEYCOPY_LEFT },
246 { '\025' /* C-u */, 0, MODEKEYCOPY_HALFPAGEUP },
247 { '\031' /* C-y */, 0, MODEKEYCOPY_SCROLLUP },
248 { '\033' /* Escape */, 0, MODEKEYCOPY_CLEARSELECTION },
249 { '\r', 0, MODEKEYCOPY_COPYSELECTION },
250 { '^', 0, MODEKEYCOPY_BACKTOINDENTATION },
251 { 'b', 0, MODEKEYCOPY_PREVIOUSWORD },
252 { 'e', 0, MODEKEYCOPY_NEXTWORDEND },
253 { 'f', 0, MODEKEYCOPY_JUMP },
254 { 'g', 0, MODEKEYCOPY_HISTORYTOP },
255 { 'h', 0, MODEKEYCOPY_LEFT },
256 { 'j', 0, MODEKEYCOPY_DOWN },
257 { 'k', 0, MODEKEYCOPY_UP },
258 { 'l', 0, MODEKEYCOPY_RIGHT },
259 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
260 { 't', 0, MODEKEYCOPY_JUMPTO },
261 { 'q', 0, MODEKEYCOPY_CANCEL },
262 { 'v', 0, MODEKEYCOPY_RECTANGLETOGGLE },
263 { 'w', 0, MODEKEYCOPY_NEXTWORD },
264 { KEYC_BSPACE, 0, MODEKEYCOPY_LEFT },
265 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
266 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
267 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
268 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
269 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
270 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
271 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
272 { KEYC_UP, 0, MODEKEYCOPY_UP },
274 { 0, -1, 0 }
276 struct mode_key_tree mode_key_tree_vi_copy;
278 /* emacs editing keys. */
279 const struct mode_key_entry mode_key_emacs_edit[] = {
280 { '\001' /* C-a */, 0, MODEKEYEDIT_STARTOFLINE },
281 { '\002' /* C-b */, 0, MODEKEYEDIT_CURSORLEFT },
282 { '\003' /* C-c */, 0, MODEKEYEDIT_CANCEL },
283 { '\004' /* C-d */, 0, MODEKEYEDIT_DELETE },
284 { '\005' /* C-e */, 0, MODEKEYEDIT_ENDOFLINE },
285 { '\006' /* C-f */, 0, MODEKEYEDIT_CURSORRIGHT },
286 { '\010' /* C-H */, 0, MODEKEYEDIT_BACKSPACE },
287 { '\011' /* Tab */, 0, MODEKEYEDIT_COMPLETE },
288 { '\013' /* C-k */, 0, MODEKEYEDIT_DELETETOENDOFLINE },
289 { '\016' /* C-n */, 0, MODEKEYEDIT_HISTORYDOWN },
290 { '\020' /* C-p */, 0, MODEKEYEDIT_HISTORYUP },
291 { '\024' /* C-t */, 0, MODEKEYEDIT_TRANSPOSECHARS },
292 { '\025' /* C-u */, 0, MODEKEYEDIT_DELETELINE },
293 { '\027' /* C-w */, 0, MODEKEYEDIT_DELETEWORD },
294 { '\031' /* C-y */, 0, MODEKEYEDIT_PASTE },
295 { '\033' /* Escape */, 0, MODEKEYEDIT_CANCEL },
296 { '\r', 0, MODEKEYEDIT_ENTER },
297 { 'b' | KEYC_ESCAPE, 0, MODEKEYEDIT_PREVIOUSWORD },
298 { 'f' | KEYC_ESCAPE, 0, MODEKEYEDIT_NEXTWORDEND },
299 { 'm' | KEYC_ESCAPE, 0, MODEKEYEDIT_STARTOFLINE },
300 { KEYC_BSPACE, 0, MODEKEYEDIT_BACKSPACE },
301 { KEYC_DC, 0, MODEKEYEDIT_DELETE },
302 { KEYC_DOWN, 0, MODEKEYEDIT_HISTORYDOWN },
303 { KEYC_LEFT, 0, MODEKEYEDIT_CURSORLEFT },
304 { KEYC_RIGHT, 0, MODEKEYEDIT_CURSORRIGHT },
305 { KEYC_UP, 0, MODEKEYEDIT_HISTORYUP },
306 { KEYC_HOME, 0, MODEKEYEDIT_STARTOFLINE },
307 { KEYC_END, 0, MODEKEYEDIT_ENDOFLINE },
309 { 0, -1, 0 }
311 struct mode_key_tree mode_key_tree_emacs_edit;
313 /* emacs choice selection keys. */
314 const struct mode_key_entry mode_key_emacs_choice[] = {
315 { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
316 { '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN },
317 { '\020' /* C-p */, 0, MODEKEYCHOICE_UP },
318 { '\026' /* C-v */, 0, MODEKEYCHOICE_PAGEDOWN },
319 { '\033' /* Escape */, 0, MODEKEYCHOICE_CANCEL },
320 { '\r', 0, MODEKEYCHOICE_CHOOSE },
321 { 'q', 0, MODEKEYCHOICE_CANCEL },
322 { 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP },
323 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
324 { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
325 { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
326 { KEYC_PPAGE, 0, MODEKEYCHOICE_PAGEUP },
327 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCHOICE_SCROLLUP },
328 { KEYC_UP, 0, MODEKEYCHOICE_UP },
330 { 0, -1, 0 }
332 struct mode_key_tree mode_key_tree_emacs_choice;
334 /* emacs copy mode keys. */
335 const struct mode_key_entry mode_key_emacs_copy[] = {
336 { ' ', 0, MODEKEYCOPY_NEXTPAGE },
337 { ',', 0, MODEKEYCOPY_JUMPREVERSE },
338 { ';', 0, MODEKEYCOPY_JUMPAGAIN },
339 { '1' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
340 { '2' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
341 { '3' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
342 { '4' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
343 { '5' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
344 { '6' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
345 { '7' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
346 { '8' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
347 { '9' | KEYC_ESCAPE, 0, MODEKEYCOPY_STARTNUMBERPREFIX },
348 { '<' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYTOP },
349 { '>' | KEYC_ESCAPE, 0, MODEKEYCOPY_HISTORYBOTTOM },
350 { 'F', 0, MODEKEYCOPY_JUMPBACK },
351 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
352 { 'R' | KEYC_ESCAPE, 0, MODEKEYCOPY_TOPLINE },
353 { 'R', 0, MODEKEYCOPY_RECTANGLETOGGLE },
354 { 'T', 0, MODEKEYCOPY_JUMPTOBACK },
355 { '\000' /* C-Space */, 0, MODEKEYCOPY_STARTSELECTION },
356 { '\001' /* C-a */, 0, MODEKEYCOPY_STARTOFLINE },
357 { '\002' /* C-b */, 0, MODEKEYCOPY_LEFT },
358 { '\003' /* C-c */, 0, MODEKEYCOPY_CANCEL },
359 { '\005' /* C-e */, 0, MODEKEYCOPY_ENDOFLINE },
360 { '\006' /* C-f */, 0, MODEKEYCOPY_RIGHT },
361 { '\007' /* C-g */, 0, MODEKEYCOPY_CLEARSELECTION },
362 { '\013' /* C-k */, 0, MODEKEYCOPY_COPYENDOFLINE },
363 { '\016' /* C-n */, 0, MODEKEYCOPY_DOWN },
364 { '\020' /* C-p */, 0, MODEKEYCOPY_UP },
365 { '\022' /* C-r */, 0, MODEKEYCOPY_SEARCHUP },
366 { '\023' /* C-s */, 0, MODEKEYCOPY_SEARCHDOWN },
367 { '\026' /* C-v */, 0, MODEKEYCOPY_NEXTPAGE },
368 { '\027' /* C-w */, 0, MODEKEYCOPY_COPYSELECTION },
369 { '\033' /* Escape */, 0, MODEKEYCOPY_CANCEL },
370 { 'N', 0, MODEKEYCOPY_SEARCHREVERSE },
371 { 'b' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSWORD },
372 { 'f', 0, MODEKEYCOPY_JUMP },
373 { 'f' | KEYC_ESCAPE, 0, MODEKEYCOPY_NEXTWORDEND },
374 { 'g', 0, MODEKEYCOPY_GOTOLINE },
375 { 'm' | KEYC_ESCAPE, 0, MODEKEYCOPY_BACKTOINDENTATION },
376 { 'n', 0, MODEKEYCOPY_SEARCHAGAIN },
377 { 'q', 0, MODEKEYCOPY_CANCEL },
378 { 'r' | KEYC_ESCAPE, 0, MODEKEYCOPY_MIDDLELINE },
379 { 't', 0, MODEKEYCOPY_JUMPTO },
380 { 'v' | KEYC_ESCAPE, 0, MODEKEYCOPY_PREVIOUSPAGE },
381 { 'w' | KEYC_ESCAPE, 0, MODEKEYCOPY_COPYSELECTION },
382 { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
383 { KEYC_DOWN | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEDOWN },
384 { KEYC_DOWN, 0, MODEKEYCOPY_DOWN },
385 { KEYC_LEFT, 0, MODEKEYCOPY_LEFT },
386 { KEYC_NPAGE, 0, MODEKEYCOPY_NEXTPAGE },
387 { KEYC_PPAGE, 0, MODEKEYCOPY_PREVIOUSPAGE },
388 { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT },
389 { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP },
390 { KEYC_UP | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEUP },
391 { KEYC_UP, 0, MODEKEYCOPY_UP },
393 { 0, -1, 0 }
395 struct mode_key_tree mode_key_tree_emacs_copy;
397 /* Table mapping key table names to default settings and trees. */
398 const struct mode_key_table mode_key_tables[] = {
399 { "vi-edit", mode_key_cmdstr_edit,
400 &mode_key_tree_vi_edit, mode_key_vi_edit },
401 { "vi-choice", mode_key_cmdstr_choice,
402 &mode_key_tree_vi_choice, mode_key_vi_choice },
403 { "vi-copy", mode_key_cmdstr_copy,
404 &mode_key_tree_vi_copy, mode_key_vi_copy },
405 { "emacs-edit", mode_key_cmdstr_edit,
406 &mode_key_tree_emacs_edit, mode_key_emacs_edit },
407 { "emacs-choice", mode_key_cmdstr_choice,
408 &mode_key_tree_emacs_choice, mode_key_emacs_choice },
409 { "emacs-copy", mode_key_cmdstr_copy,
410 &mode_key_tree_emacs_copy, mode_key_emacs_copy },
412 { NULL, NULL, NULL, NULL }
415 RB_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
418 mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
420 if (mbind1->mode != mbind2->mode)
421 return (mbind1->mode - mbind2->mode);
422 return (mbind1->key - mbind2->key);
425 const char *
426 mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
428 for (; cmdstr->name != NULL; cmdstr++) {
429 if (cmdstr->cmd == cmd)
430 return (cmdstr->name);
432 return (NULL);
435 enum mode_key_cmd
436 mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
438 for (; cmdstr->name != NULL; cmdstr++) {
439 if (strcasecmp(cmdstr->name, name) == 0)
440 return (cmdstr->cmd);
442 return (MODEKEY_NONE);
445 const struct mode_key_table *
446 mode_key_findtable(const char *name)
448 const struct mode_key_table *mtab;
450 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
451 if (strcasecmp(name, mtab->name) == 0)
452 return (mtab);
454 return (NULL);
457 void
458 mode_key_init_trees(void)
460 const struct mode_key_table *mtab;
461 const struct mode_key_entry *ment;
462 struct mode_key_binding *mbind;
464 for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
465 RB_INIT(mtab->tree);
466 for (ment = mtab->table; ment->mode != -1; ment++) {
467 mbind = xmalloc(sizeof *mbind);
468 mbind->key = ment->key;
469 mbind->mode = ment->mode;
470 mbind->cmd = ment->cmd;
471 RB_INSERT(mode_key_tree, mtab->tree, mbind);
476 void
477 mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
479 mdata->tree = mtree;
480 mdata->mode = 0;
483 enum mode_key_cmd
484 mode_key_lookup(struct mode_key_data *mdata, int key)
486 struct mode_key_binding *mbind, mtmp;
488 mtmp.key = key;
489 mtmp.mode = mdata->mode;
490 if ((mbind = RB_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) {
491 if (mdata->mode != 0)
492 return (MODEKEY_NONE);
493 return (MODEKEY_OTHER);
496 switch (mbind->cmd) {
497 case MODEKEYEDIT_SWITCHMODE:
498 case MODEKEYEDIT_SWITCHMODEAPPEND:
499 mdata->mode = 1 - mdata->mode;
500 /* FALLTHROUGH */
501 default:
502 return (mbind->cmd);