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