summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/mode-key.c
blob: 0153864d60bb35924a719e7c8acbdaf4debc02f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* $OpenBSD: mode-key.c,v 1.64 2015/11/12 11:05:34 nicm Exp $ */

/*
 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <string.h>

#include "tmux.h"

/*
 * Mode keys. These are the key bindings used when editing (status prompt), and
 * in the modes. They are split into two sets of three tables, one set of three
 * for vi and the other for emacs key bindings. The three tables are for
 * editing, for menu-like modes (choice, more), and for copy modes (copy,
 * scroll).
 *
 * The fixed tables of struct mode_key_entry below are the defaults: they are
 * built into a tree of struct mode_key_binding by mode_key_init_trees, which
 * can then be modified.
 *
 * vi command mode is handled by having a mode flag in the struct which allows
 * two sets of bindings to be swapped between. A couple of editing commands
 * (any matching MODEKEYEDIT_SWITCHMODE*) are special-cased to do this.
 */

/* Entry in the default mode key tables. */
struct mode_key_entry {
	key_code		key;

	/*
	 * Editing mode for vi: 0 is edit mode, keys not in the table are
	 * returned as MODEKEY_OTHER; 1 is command mode, keys not in the table
	 * are returned as MODEKEY_NONE. This is also matched on, allowing some
	 * keys to be bound in edit mode.
	 */
	int			mode;
	enum mode_key_cmd	cmd;
};

/* Edit keys command strings. */
const struct mode_key_cmdstr mode_key_cmdstr_edit[] = {
	{ MODEKEYEDIT_BACKSPACE, "backspace" },
	{ MODEKEYEDIT_CANCEL, "cancel" },
	{ MODEKEYEDIT_COMPLETE, "complete" },
	{ MODEKEYEDIT_CURSORLEFT, "cursor-left" },
	{ MODEKEYEDIT_CURSORRIGHT, "cursor-right" },
	{ MODEKEYEDIT_DELETE, "delete" },
	{ MODEKEYEDIT_DELETELINE, "delete-line" },
	{ MODEKEYEDIT_DELETETOENDOFLINE, "delete-end-of-line" },
	{ MODEKEYEDIT_DELETEWORD, "delete-word" },
	{ MODEKEYEDIT_ENDOFLINE, "end-of-line" },
	{ MODEKEYEDIT_ENTER, "enter" },
	{ MODEKEYEDIT_HISTORYDOWN, "history-down" },
	{ MODEKEYEDIT_HISTORYUP, "history-up" },
	{ MODEKEYEDIT_NEXTSPACE, "next-space" },
	{ MODEKEYEDIT_NEXTSPACEEND, "next-space-end" },
	{ MODEKEYEDIT_NEXTWORD, "next-word" },
	{ MODEKEYEDIT_NEXTWORDEND, "next-word-end" },
	{ MODEKEYEDIT_PASTE, "paste" },
	{ MODEKEYEDIT_PREVIOUSSPACE, "previous-space" },
	{ MODEKEYEDIT_PREVIOUSWORD, "previous-word" },
	{ MODEKEYEDIT_STARTOFLINE, "start-of-line" },
	{ MODEKEYEDIT_SWITCHMODE, "switch-mode" },
	{ MODEKEYEDIT_SWITCHMODEAPPEND, "switch-mode-append" },
	{ MODEKEYEDIT_SWITCHMODEAPPENDLINE, "switch-mode-append-line" },
	{ MODEKEYEDIT_SWITCHMODEBEGINLINE, "switch-mode-begin-line" },
	{ MODEKEYEDIT_SWITCHMODECHANGELINE, "switch-mode-change-line" },
	{ MODEKEYEDIT_SWITCHMODESUBSTITUTE, "switch-mode-substitute" },
	{ MODEKEYEDIT_SWITCHMODESUBSTITUTELINE, "switch-mode-substitute-line" },
	{ MODEKEYEDIT_TRANSPOSECHARS, "transpose-chars" },

	{ 0, NULL }
};

/* Choice keys command strings. */
const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
	{ MODEKEYCHOICE_BACKSPACE, "backspace" },
	{ MODEKEYCHOICE_BOTTOMLINE, "bottom-line"},
	{ MODEKEYCHOICE_CANCEL, "cancel" },
	{ MODEKEYCHOICE_CHOOSE, "choose" },
	{ MODEKEYCHOICE_DOWN, "down" },
	{ MODEKEYCHOICE_ENDOFLIST, "end-of-list"},
	{ MODEKEYCHOICE_PAGEDOWN, "page-down" },
	{ MODEKEYCHOICE_PAGEUP, "page-up" },
	{ MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
	{ MODEKEYCHOICE_SCROLLUP, "scroll-up" },
	{ MODEKEYCHOICE_STARTNUMBERPREFIX, "start-number-prefix" },
	{ MODEKEYCHOICE_STARTOFLIST, "start-of-list"},
	{ MODEKEYCHOICE_TOPLINE, "top-line"},
	{ MODEKEYCHOICE_TREE_COLLAPSE, "tree-collapse" },
	{ MODEKEYCHOICE_TREE_COLLAPSE_ALL, "tree-collapse-all" },
	{ MODEKEYCHOICE_TREE_EXPAND, "tree-expand" },
	{ MODEKEYCHOICE_TREE_EXPAND_ALL, "tree-expand-all" },
	{ MODEKEYCHOICE_TREE_TOGGLE, "tree-toggle" },
	{ MODEKEYCHOICE_UP, "up" },

	{ 0, NULL }
};

/* Copy keys command strings. */
const struct mode_key_cmdstr mode_key_cmdstr_copy[] = {
	{ MODEKEYCOPY_APPENDSELECTION, "append-selection" },
	{ MODEKEYCOPY_BACKTOINDENTATION, "back-to-indentation" },
	{ MODEKEYCOPY_BOTTOMLINE, "bottom-line" },
	{ MODEKEYCOPY_CANCEL, "cancel" },
	{ MODEKEYCOPY_CLEARSELECTION, "clear-selection" },
	{ MODEKEYCOPY_COPYPIPE, "copy-pipe" },
	{ MODEKEYCOPY_COPYLINE, "copy-line" },
	{ MODEKEYCOPY_COPYENDOFLINE, "copy-end-of-line" },
	{ MODEKEYCOPY_COPYSELECTION, "copy-selection" },
	{ MODEKEYCOPY_DOWN, "cursor-down" },
	{ MODEKEYCOPY_ENDOFLINE, "end-of-line" },
	{ MODEKEYCOPY_GOTOLINE, "goto-line" },
	{ MODEKEYCOPY_HALFPAGEDOWN, "halfpage-down" },
	{ MODEKEYCOPY_HALFPAGEUP, "halfpage-up" },
	{ MODEKEYCOPY_HISTORYBOTTOM, "history-bottom" },
	{ MODEKEYCOPY_HISTORYTOP, "history-top" },
	{ MODEKEYCOPY_JUMP, "jump-forward" },
	{ MODEKEYCOPY_JUMPAGAIN, "jump-again" },
	{ MODEKEYCOPY_JUMPREVERSE, "jump-reverse" },
	{ MODEKEYCOPY_JUMPBACK, "jump-backward" },
	{ MODEKEYCOPY_JUMPTO, "jump-to-forward" },
	{ MODEKEYCOPY_JUMPTOBACK, "jump-to-backward" },
	{ MODEKEYCOPY_LEFT, "cursor-left" },
	{ MODEKEYCOPY_RECTANGLETOGGLE, "rectangle-toggle" },
	{ MODEKEYCOPY_MIDDLELINE, "middle-line" },
	{ MODEKEYCOPY_NEXTPAGE, "page-down" },
	{ MODEKEYCOPY_NEXTSPACE, "next-space" },
	{ MODEKEYCOPY_NEXTSPACEEND, "next-space-end" },
	{ MODEKEYCOPY_NEXTWORD, "next-word" },
	{ MODEKEYCOPY_NEXTWORDEND, "next-word-end" },
	{ MODEKEYCOPY_OTHEREND, "other-end" },
	{ MODEKEYCOPY_PREVIOUSPAGE, "page-up" },
	{ MODEKEYCOPY_PREVIOUSSPACE, "previous-space" },
	{ MODEKEYCOPY_PREVIOUSWORD, "previous-word" },
	{ MODEKEYCOPY_RIGHT, "cursor-right" },
	{ MODEKEYCOPY_SCROLLDOWN, "scroll-down" },
	{ MODEKEYCOPY_SCROLLUP, "scroll-up" },
	{ MODEKEYCOPY_SEARCHAGAIN, "search-again" },
	{ MODEKEYCOPY_SEARCHDOWN, "search-forward" },
	{ MODEKEYCOPY_SEARCHREVERSE, "search-reverse" },
	{ MODEKEYCOPY_SEARCHUP, "search-backward" },
	{ MODEKEYCOPY_SELECTLINE, "select-line" },
	{ MODEKEYCOPY_STARTNAMEDBUFFER, "start-named-buffer" },
	{ MODEKEYCOPY_STARTNUMBERPREFIX, "start-number-prefix" },
	{ MODEKEYCOPY_STARTOFLINE, "start-of-line" },
	{ MODEKEYCOPY_STARTSELECTION, "begin-selection" },
	{ MODEKEYCOPY_TOPLINE, "top-line" },
	{ MODEKEYCOPY_UP, "cursor-up" },

	{ 0, NULL }
};

/* vi editing keys. */
const struct mode_key_entry mode_key_vi_edit[] = {
	{ '\003' /* C-c */,	    0, MODEKEYEDIT_CANCEL },
	{ '\010' /* C-h */,	    0, MODEKEYEDIT_BACKSPACE },
	{ '\011' /* Tab */,	    0, MODEKEYEDIT_COMPLETE },
	{ '\025' /* C-u */,	    0, MODEKEYEDIT_DELETELINE },
	{ '\027' /* C-w */,	    0, MODEKEYEDIT_DELETEWORD },
	{ '\033' /* Escape */,	    0, MODEKEYEDIT_SWITCHMODE },
	{ '\n',			    0, MODEKEYEDIT_ENTER },
	{ '\r',			    0, MODEKEYEDIT_ENTER },
	{ KEYC_BSPACE,		    0, MODEKEYEDIT_BACKSPACE },
	{ KEYC_DC,		    0, MODEKEYEDIT_DELETE },
	{ KEYC_DOWN,		    0, MODEKEYEDIT_HISTORYDOWN },
	{ KEYC_LEFT,		    0, MODEKEYEDIT_CURSORLEFT },
	{ KEYC_RIGHT,		    0, MODEKEYEDIT_CURSORRIGHT },
	{ KEYC_UP,		    0, MODEKEYEDIT_HISTORYUP },
	{ KEYC_HOME,		    0, MODEKEYEDIT_STARTOFLINE },
	{ KEYC_END,		    0, MODEKEYEDIT_ENDOFLINE },

	{ '$',			    1, MODEKEYEDIT_ENDOFLINE },
	{ '0',			    1, MODEKEYEDIT_STARTOFLINE },
	{ 'A',			    1, MODEKEYEDIT_SWITCHMODEAPPENDLINE },
	{ 'B',			    1, MODEKEYEDIT_PREVIOUSSPACE },
	{ 'C',			    1, MODEKEYEDIT_SWITCHMODECHANGELINE },
	{ 'D',			    1, MODEKEYEDIT_DELETETOENDOFLINE },
	{ 'E',			    1, MODEKEYEDIT_NEXTSPACEEND },
	{ 'I',			    1, MODEKEYEDIT_SWITCHMODEBEGINLINE },
	{ 'S',			    1, MODEKEYEDIT_SWITCHMODESUBSTITUTELINE },
	{ 'W',			    1, MODEKEYEDIT_NEXTSPACE },
	{ 'X',			    1, MODEKEYEDIT_BACKSPACE },
	{ '\003' /* C-c */,	    1, MODEKEYEDIT_CANCEL },
	{ '\010' /* C-h */,	    1, MODEKEYEDIT_BACKSPACE },
	{ '\n',			    1, MODEKEYEDIT_ENTER },
	{ '\r',			    1, MODEKEYEDIT_ENTER },
	{ '^',			    1, MODEKEYEDIT_STARTOFLINE },
	{ 'a',			    1, MODEKEYEDIT_SWITCHMODEAPPEND },
	{ 'b',			    1, MODEKEYEDIT_PREVIOUSWORD },
	{ 'd',			    1, MODEKEYEDIT_DELETELINE },
	{ 'e',			    1, MODEKEYEDIT_NEXTWORDEND },
	{ 'h',			    1, MODEKEYEDIT_CURSORLEFT },
	{ 'i',			    1, MODEKEYEDIT_SWITCHMODE },
	{ 'j',			    1, MODEKEYEDIT_HISTORYDOWN },
	{ 'k',			    1, MODEKEYEDIT_HISTORYUP },
	{ 'l',			    1, MODEKEYEDIT_CURSORRIGHT },
	{ 'p',			    1, MODEKEYEDIT_PASTE },
	{ 's',			    1, MODEKEYEDIT_SWITCHMODESUBSTITUTE },
	{ 'w',			    1, MODEKEYEDIT_NEXTWORD },
	{ 'x',			    1, MODEKEYEDIT_DELETE },
	{ KEYC_BSPACE,		    1, MODEKEYEDIT_BACKSPACE },
	{ KEYC_DC,		    1, MODEKEYEDIT_DELETE },
	{ KEYC_DOWN,		    1, MODEKEYEDIT_HISTORYDOWN },
	{ KEYC_LEFT,		    1, MODEKEYEDIT_CURSORLEFT },
	{ KEYC_RIGHT,		    1, MODEKEYEDIT_CURSORRIGHT },
	{ KEYC_UP,		    1, MODEKEYEDIT_HISTORYUP },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_vi_edit;

/* vi choice selection keys. */
const struct mode_key_entry mode_key_vi_choice[] = {
	{ '0' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '1' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '2' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '3' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '4' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '5' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '6' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '7' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '8' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '9' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '\002' /* C-b */,	    0, MODEKEYCHOICE_PAGEUP },
	{ '\003' /* C-c */,	    0, MODEKEYCHOICE_CANCEL },
	{ '\005' /* C-e */,	    0, MODEKEYCHOICE_SCROLLDOWN },
	{ '\006' /* C-f */,	    0, MODEKEYCHOICE_PAGEDOWN },
	{ '\031' /* C-y */,	    0, MODEKEYCHOICE_SCROLLUP },
	{ '\n',			    0, MODEKEYCHOICE_CHOOSE },
	{ '\r',			    0, MODEKEYCHOICE_CHOOSE },
	{ 'j',			    0, MODEKEYCHOICE_DOWN },
	{ 'k',			    0, MODEKEYCHOICE_UP },
	{ 'q',			    0, MODEKEYCHOICE_CANCEL },
	{ KEYC_HOME,                0, MODEKEYCHOICE_STARTOFLIST },
	{ 'g',                      0, MODEKEYCHOICE_STARTOFLIST },
	{ 'H',                      0, MODEKEYCHOICE_TOPLINE },
	{ 'L',                      0, MODEKEYCHOICE_BOTTOMLINE },
	{ 'G',                      0, MODEKEYCHOICE_ENDOFLIST },
	{ KEYC_END,                 0, MODEKEYCHOICE_ENDOFLIST },
	{ KEYC_BSPACE,		    0, MODEKEYCHOICE_BACKSPACE },
	{ KEYC_DOWN | KEYC_CTRL,    0, MODEKEYCHOICE_SCROLLDOWN },
	{ KEYC_DOWN,		    0, MODEKEYCHOICE_DOWN },
	{ KEYC_NPAGE,		    0, MODEKEYCHOICE_PAGEDOWN },
	{ KEYC_PPAGE,		    0, MODEKEYCHOICE_PAGEUP },
	{ KEYC_UP | KEYC_CTRL,	    0, MODEKEYCHOICE_SCROLLUP },
	{ KEYC_UP,		    0, MODEKEYCHOICE_UP },
	{ ' ',			    0, MODEKEYCHOICE_TREE_TOGGLE },
	{ KEYC_LEFT,		    0, MODEKEYCHOICE_TREE_COLLAPSE },
	{ KEYC_RIGHT,		    0, MODEKEYCHOICE_TREE_EXPAND },
	{ KEYC_LEFT | KEYC_CTRL,    0, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
	{ KEYC_RIGHT | KEYC_CTRL,   0, MODEKEYCHOICE_TREE_EXPAND_ALL },
	{ KEYC_MOUSEDOWN1_PANE,     0, MODEKEYCHOICE_CHOOSE },
	{ KEYC_MOUSEDOWN3_PANE,     0, MODEKEYCHOICE_TREE_TOGGLE },
	{ KEYC_WHEELUP_PANE,        0, MODEKEYCHOICE_UP },
	{ KEYC_WHEELDOWN_PANE,      0, MODEKEYCHOICE_DOWN },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_vi_choice;

/* vi copy mode keys. */
const struct mode_key_entry mode_key_vi_copy[] = {
	{ ' ',			    0, MODEKEYCOPY_STARTSELECTION },
	{ '"',			    0, MODEKEYCOPY_STARTNAMEDBUFFER },
	{ '$',			    0, MODEKEYCOPY_ENDOFLINE },
	{ ',',			    0, MODEKEYCOPY_JUMPREVERSE },
	{ ';',			    0, MODEKEYCOPY_JUMPAGAIN },
	{ '/',			    0, MODEKEYCOPY_SEARCHDOWN },
	{ '0',			    0, MODEKEYCOPY_STARTOFLINE },
	{ '1',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '2',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '3',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '4',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '5',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '6',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '7',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '8',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '9',			    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ ':',			    0, MODEKEYCOPY_GOTOLINE },
	{ '?',			    0, MODEKEYCOPY_SEARCHUP },
	{ 'A',			    0, MODEKEYCOPY_APPENDSELECTION },
	{ 'B',			    0, MODEKEYCOPY_PREVIOUSSPACE },
	{ 'D',			    0, MODEKEYCOPY_COPYENDOFLINE },
	{ 'E',			    0, MODEKEYCOPY_NEXTSPACEEND },
	{ 'F',			    0, MODEKEYCOPY_JUMPBACK },
	{ 'G',			    0, MODEKEYCOPY_HISTORYBOTTOM },
	{ 'H',			    0, MODEKEYCOPY_TOPLINE },
	{ 'J',			    0, MODEKEYCOPY_SCROLLDOWN },
	{ 'K',			    0, MODEKEYCOPY_SCROLLUP },
	{ 'L',			    0, MODEKEYCOPY_BOTTOMLINE },
	{ 'M',			    0, MODEKEYCOPY_MIDDLELINE },
	{ 'N',			    0, MODEKEYCOPY_SEARCHREVERSE },
	{ 'T',			    0, MODEKEYCOPY_JUMPTOBACK },
	{ 'V',			    0, MODEKEYCOPY_SELECTLINE },
	{ 'W',			    0, MODEKEYCOPY_NEXTSPACE },
	{ '\002' /* C-b */,	    0, MODEKEYCOPY_PREVIOUSPAGE },
	{ '\003' /* C-c */,	    0, MODEKEYCOPY_CANCEL },
	{ '\004' /* C-d */,	    0, MODEKEYCOPY_HALFPAGEDOWN },
	{ '\005' /* C-e */,	    0, MODEKEYCOPY_SCROLLDOWN },
	{ '\006' /* C-f */,	    0, MODEKEYCOPY_NEXTPAGE },
	{ '\010' /* C-h */,	    0, MODEKEYCOPY_LEFT },
	{ '\025' /* C-u */,	    0, MODEKEYCOPY_HALFPAGEUP },
	{ '\031' /* C-y */,	    0, MODEKEYCOPY_SCROLLUP },
	{ '\033' /* Escape */,	    0, MODEKEYCOPY_CLEARSELECTION },
	{ '\n',			    0, MODEKEYCOPY_COPYSELECTION },
	{ '\r',			    0, MODEKEYCOPY_COPYSELECTION },
	{ '^',			    0, MODEKEYCOPY_BACKTOINDENTATION },
	{ 'b',			    0, MODEKEYCOPY_PREVIOUSWORD },
	{ 'e',			    0, MODEKEYCOPY_NEXTWORDEND },
	{ 'f',			    0, MODEKEYCOPY_JUMP },
	{ 'g',			    0, MODEKEYCOPY_HISTORYTOP },
	{ 'h',			    0, MODEKEYCOPY_LEFT },
	{ 'j',			    0, MODEKEYCOPY_DOWN },
	{ 'k',			    0, MODEKEYCOPY_UP },
	{ 'l',			    0, MODEKEYCOPY_RIGHT },
	{ 'n',			    0, MODEKEYCOPY_SEARCHAGAIN },
	{ 'o',			    0, MODEKEYCOPY_OTHEREND },
	{ 't',			    0, MODEKEYCOPY_JUMPTO },
	{ 'q',			    0, MODEKEYCOPY_CANCEL },
	{ 'v',			    0, MODEKEYCOPY_RECTANGLETOGGLE },
	{ 'w',			    0, MODEKEYCOPY_NEXTWORD },
	{ KEYC_BSPACE,		    0, MODEKEYCOPY_LEFT },
	{ KEYC_DOWN | KEYC_CTRL,    0, MODEKEYCOPY_SCROLLDOWN },
	{ KEYC_DOWN,		    0, MODEKEYCOPY_DOWN },
	{ KEYC_LEFT,		    0, MODEKEYCOPY_LEFT },
	{ KEYC_NPAGE,		    0, MODEKEYCOPY_NEXTPAGE },
	{ KEYC_PPAGE,		    0, MODEKEYCOPY_PREVIOUSPAGE },
	{ KEYC_RIGHT,		    0, MODEKEYCOPY_RIGHT },
	{ KEYC_UP | KEYC_CTRL,	    0, MODEKEYCOPY_SCROLLUP },
	{ KEYC_UP,		    0, MODEKEYCOPY_UP },
	{ KEYC_WHEELUP_PANE,        0, MODEKEYCOPY_SCROLLUP },
	{ KEYC_WHEELDOWN_PANE,      0, MODEKEYCOPY_SCROLLDOWN },
	{ KEYC_MOUSEDRAG1_PANE,     0, MODEKEYCOPY_STARTSELECTION },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_vi_copy;

/* emacs editing keys. */
const struct mode_key_entry mode_key_emacs_edit[] = {
	{ '\001' /* C-a */,	    0, MODEKEYEDIT_STARTOFLINE },
	{ '\002' /* C-b */,	    0, MODEKEYEDIT_CURSORLEFT },
	{ '\003' /* C-c */,	    0, MODEKEYEDIT_CANCEL },
	{ '\004' /* C-d */,	    0, MODEKEYEDIT_DELETE },
	{ '\005' /* C-e */,	    0, MODEKEYEDIT_ENDOFLINE },
	{ '\006' /* C-f */,	    0, MODEKEYEDIT_CURSORRIGHT },
	{ '\010' /* C-H */,	    0, MODEKEYEDIT_BACKSPACE },
	{ '\011' /* Tab */,	    0, MODEKEYEDIT_COMPLETE },
	{ '\013' /* C-k */,	    0, MODEKEYEDIT_DELETETOENDOFLINE },
	{ '\016' /* C-n */,	    0, MODEKEYEDIT_HISTORYDOWN },
	{ '\020' /* C-p */,	    0, MODEKEYEDIT_HISTORYUP },
	{ '\024' /* C-t */,	    0, MODEKEYEDIT_TRANSPOSECHARS },
	{ '\025' /* C-u */,	    0, MODEKEYEDIT_DELETELINE },
	{ '\027' /* C-w */,	    0, MODEKEYEDIT_DELETEWORD },
	{ '\031' /* C-y */,	    0, MODEKEYEDIT_PASTE },
	{ '\033' /* Escape */,	    0, MODEKEYEDIT_CANCEL },
	{ '\n',			    0, MODEKEYEDIT_ENTER },
	{ '\r',			    0, MODEKEYEDIT_ENTER },
	{ 'b' | KEYC_ESCAPE,	    0, MODEKEYEDIT_PREVIOUSWORD },
	{ 'f' | KEYC_ESCAPE,	    0, MODEKEYEDIT_NEXTWORDEND },
	{ 'm' | KEYC_ESCAPE,	    0, MODEKEYEDIT_STARTOFLINE },
	{ KEYC_BSPACE,		    0, MODEKEYEDIT_BACKSPACE },
	{ KEYC_DC,		    0, MODEKEYEDIT_DELETE },
	{ KEYC_DOWN,		    0, MODEKEYEDIT_HISTORYDOWN },
	{ KEYC_LEFT,		    0, MODEKEYEDIT_CURSORLEFT },
	{ KEYC_RIGHT,		    0, MODEKEYEDIT_CURSORRIGHT },
	{ KEYC_UP,		    0, MODEKEYEDIT_HISTORYUP },
	{ KEYC_HOME,		    0, MODEKEYEDIT_STARTOFLINE },
	{ KEYC_END,		    0, MODEKEYEDIT_ENDOFLINE },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_emacs_edit;

/* emacs choice selection keys. */
const struct mode_key_entry mode_key_emacs_choice[] = {
	{ '0' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '1' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '2' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '3' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '4' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '5' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '6' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '7' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '8' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '9' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTNUMBERPREFIX },
	{ '\003' /* C-c */,	    0, MODEKEYCHOICE_CANCEL },
	{ '\016' /* C-n */,	    0, MODEKEYCHOICE_DOWN },
	{ '\020' /* C-p */,	    0, MODEKEYCHOICE_UP },
	{ '\026' /* C-v */,	    0, MODEKEYCHOICE_PAGEDOWN },
	{ '\033' /* Escape */,	    0, MODEKEYCHOICE_CANCEL },
	{ '\n',			    0, MODEKEYCHOICE_CHOOSE },
	{ '\r',			    0, MODEKEYCHOICE_CHOOSE },
	{ 'q',			    0, MODEKEYCHOICE_CANCEL },
	{ 'v' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_PAGEUP },
	{ KEYC_HOME,                0, MODEKEYCHOICE_STARTOFLIST },
	{ '<' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_STARTOFLIST },
	{ 'R' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_TOPLINE },
	{ '>' | KEYC_ESCAPE,	    0, MODEKEYCHOICE_ENDOFLIST },
	{ KEYC_END,                 0, MODEKEYCHOICE_ENDOFLIST },
	{ KEYC_BSPACE,		    0, MODEKEYCHOICE_BACKSPACE },
	{ KEYC_DOWN | KEYC_CTRL,    0, MODEKEYCHOICE_SCROLLDOWN },
	{ KEYC_DOWN,		    0, MODEKEYCHOICE_DOWN },
	{ KEYC_NPAGE,		    0, MODEKEYCHOICE_PAGEDOWN },
	{ KEYC_PPAGE,		    0, MODEKEYCHOICE_PAGEUP },
	{ KEYC_UP | KEYC_CTRL,	    0, MODEKEYCHOICE_SCROLLUP },
	{ KEYC_UP,		    0, MODEKEYCHOICE_UP },
	{ ' ',			    0, MODEKEYCHOICE_TREE_TOGGLE },
	{ KEYC_LEFT,		    0, MODEKEYCHOICE_TREE_COLLAPSE },
	{ KEYC_RIGHT,		    0, MODEKEYCHOICE_TREE_EXPAND },
	{ KEYC_LEFT | KEYC_CTRL,    0, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
	{ KEYC_RIGHT | KEYC_CTRL,   0, MODEKEYCHOICE_TREE_EXPAND_ALL },
	{ KEYC_MOUSEDOWN1_PANE,     0, MODEKEYCHOICE_CHOOSE },
	{ KEYC_MOUSEDOWN3_PANE,     0, MODEKEYCHOICE_TREE_TOGGLE },
	{ KEYC_WHEELUP_PANE,        0, MODEKEYCHOICE_UP },
	{ KEYC_WHEELDOWN_PANE,      0, MODEKEYCHOICE_DOWN },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_emacs_choice;

/* emacs copy mode keys. */
const struct mode_key_entry mode_key_emacs_copy[] = {
	{ ' ',			    0, MODEKEYCOPY_NEXTPAGE },
	{ ',',			    0, MODEKEYCOPY_JUMPREVERSE },
	{ ';',			    0, MODEKEYCOPY_JUMPAGAIN },
	{ '1' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '2' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '3' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '4' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '5' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '6' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '7' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '8' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '9' | KEYC_ESCAPE,	    0, MODEKEYCOPY_STARTNUMBERPREFIX },
	{ '<' | KEYC_ESCAPE,	    0, MODEKEYCOPY_HISTORYTOP },
	{ '>' | KEYC_ESCAPE,	    0, MODEKEYCOPY_HISTORYBOTTOM },
	{ 'F',			    0, MODEKEYCOPY_JUMPBACK },
	{ 'N',			    0, MODEKEYCOPY_SEARCHREVERSE },
	{ 'R' | KEYC_ESCAPE,	    0, MODEKEYCOPY_TOPLINE },
	{ 'R',			    0, MODEKEYCOPY_RECTANGLETOGGLE },
	{ 'T',			    0, MODEKEYCOPY_JUMPTOBACK },
	{ '\000' /* C-Space */,	    0, MODEKEYCOPY_STARTSELECTION },
	{ '\001' /* C-a */,	    0, MODEKEYCOPY_STARTOFLINE },
	{ '\002' /* C-b */,	    0, MODEKEYCOPY_LEFT },
	{ '\003' /* C-c */,	    0, MODEKEYCOPY_CANCEL },
	{ '\005' /* C-e */,	    0, MODEKEYCOPY_ENDOFLINE },
	{ '\006' /* C-f */,	    0, MODEKEYCOPY_RIGHT },
	{ '\007' /* C-g */,	    0, MODEKEYCOPY_CLEARSELECTION },
	{ '\013' /* C-k */,	    0, MODEKEYCOPY_COPYENDOFLINE },
	{ '\016' /* C-n */,	    0, MODEKEYCOPY_DOWN },
	{ '\020' /* C-p */,	    0, MODEKEYCOPY_UP },
	{ '\022' /* C-r */,	    0, MODEKEYCOPY_SEARCHUP },
	{ '\023' /* C-s */,	    0, MODEKEYCOPY_SEARCHDOWN },
	{ '\026' /* C-v */,	    0, MODEKEYCOPY_NEXTPAGE },
	{ '\027' /* C-w */,	    0, MODEKEYCOPY_COPYSELECTION },
	{ '\033' /* Escape */,	    0, MODEKEYCOPY_CANCEL },
	{ 'b' | KEYC_ESCAPE,	    0, MODEKEYCOPY_PREVIOUSWORD },
	{ 'f',			    0, MODEKEYCOPY_JUMP },
	{ 'f' | KEYC_ESCAPE,	    0, MODEKEYCOPY_NEXTWORDEND },
	{ 'g',			    0, MODEKEYCOPY_GOTOLINE },
	{ 'm' | KEYC_ESCAPE,	    0, MODEKEYCOPY_BACKTOINDENTATION },
	{ 'n',			    0, MODEKEYCOPY_SEARCHAGAIN },
	{ 'q',			    0, MODEKEYCOPY_CANCEL },
	{ 'r' | KEYC_ESCAPE,	    0, MODEKEYCOPY_MIDDLELINE },
	{ 't',			    0, MODEKEYCOPY_JUMPTO },
	{ 'v' | KEYC_ESCAPE,	    0, MODEKEYCOPY_PREVIOUSPAGE },
	{ 'w' | KEYC_ESCAPE,	    0, MODEKEYCOPY_COPYSELECTION },
	{ KEYC_DOWN | KEYC_CTRL,    0, MODEKEYCOPY_SCROLLDOWN },
	{ KEYC_DOWN | KEYC_ESCAPE,  0, MODEKEYCOPY_HALFPAGEDOWN },
	{ KEYC_DOWN,		    0, MODEKEYCOPY_DOWN },
	{ KEYC_LEFT,		    0, MODEKEYCOPY_LEFT },
	{ KEYC_NPAGE,		    0, MODEKEYCOPY_NEXTPAGE },
	{ KEYC_PPAGE,		    0, MODEKEYCOPY_PREVIOUSPAGE },
	{ KEYC_RIGHT,		    0, MODEKEYCOPY_RIGHT },
	{ KEYC_UP | KEYC_CTRL,	    0, MODEKEYCOPY_SCROLLUP },
	{ KEYC_UP | KEYC_ESCAPE,    0, MODEKEYCOPY_HALFPAGEUP },
	{ KEYC_UP,		    0, MODEKEYCOPY_UP },
	{ KEYC_WHEELUP_PANE,        0, MODEKEYCOPY_SCROLLUP },
	{ KEYC_WHEELDOWN_PANE,      0, MODEKEYCOPY_SCROLLDOWN },
	{ KEYC_MOUSEDRAG1_PANE,     0, MODEKEYCOPY_STARTSELECTION },

	{ 0,			   -1, 0 }
};
struct mode_key_tree mode_key_tree_emacs_copy;

/* Table mapping key table names to default settings and trees. */
const struct mode_key_table mode_key_tables[] = {
	{ "vi-edit", mode_key_cmdstr_edit,
	  &mode_key_tree_vi_edit, mode_key_vi_edit },
	{ "vi-choice", mode_key_cmdstr_choice,
	  &mode_key_tree_vi_choice, mode_key_vi_choice },
	{ "vi-copy", mode_key_cmdstr_copy,
	  &mode_key_tree_vi_copy, mode_key_vi_copy },
	{ "emacs-edit", mode_key_cmdstr_edit,
	  &mode_key_tree_emacs_edit, mode_key_emacs_edit },
	{ "emacs-choice", mode_key_cmdstr_choice,
	  &mode_key_tree_emacs_choice, mode_key_emacs_choice },
	{ "emacs-copy", mode_key_cmdstr_copy,
	  &mode_key_tree_emacs_copy, mode_key_emacs_copy },

	{ NULL, NULL, NULL, NULL }
};

RB_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);

int
mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
{
	if (mbind1->mode < mbind2->mode)
		return (-1);
	if (mbind1->mode > mbind2->mode)
		return (1);
	if (mbind1->key < mbind2->key)
		return (-1);
	if (mbind1->key > mbind2->key)
		return (1);
	return (0);
}

const char *
mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
{
	for (; cmdstr->name != NULL; cmdstr++) {
		if (cmdstr->cmd == cmd)
			return (cmdstr->name);
	}
	return (NULL);
}

enum mode_key_cmd
mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
{
	for (; cmdstr->name != NULL; cmdstr++) {
		if (strcasecmp(cmdstr->name, name) == 0)
			return (cmdstr->cmd);
	}
	return (MODEKEY_NONE);
}

const struct mode_key_table *
mode_key_findtable(const char *name)
{
	const struct mode_key_table	*mtab;

	for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
		if (strcasecmp(name, mtab->name) == 0)
			return (mtab);
	}
	return (NULL);
}

void
mode_key_init_trees(void)
{
	const struct mode_key_table	*mtab;
	const struct mode_key_entry	*ment;
	struct mode_key_binding		*mbind;

	for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
		RB_INIT(mtab->tree);
		for (ment = mtab->table; ment->mode != -1; ment++) {
			mbind = xmalloc(sizeof *mbind);
			mbind->key = ment->key;
			mbind->mode = ment->mode;
			mbind->cmd = ment->cmd;
			mbind->arg = NULL;
			RB_INSERT(mode_key_tree, mtab->tree, mbind);
		}
	}
}

void
mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
{
	mdata->tree = mtree;
	mdata->mode = 0;
}

enum mode_key_cmd
mode_key_lookup(struct mode_key_data *mdata, key_code key, const char **arg)
{
	struct mode_key_binding	*mbind, mtmp;

	mtmp.key = key;
	mtmp.mode = mdata->mode;
	if ((mbind = RB_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) {
		if (mdata->mode != 0)
			return (MODEKEY_NONE);
		return (MODEKEY_OTHER);
	}

	switch (mbind->cmd) {
	case MODEKEYEDIT_SWITCHMODE:
	case MODEKEYEDIT_SWITCHMODEAPPEND:
	case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
	case MODEKEYEDIT_SWITCHMODEBEGINLINE:
	case MODEKEYEDIT_SWITCHMODECHANGELINE:
	case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
	case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
		mdata->mode = 1 - mdata->mode;
		/* FALLTHROUGH */
	default:
		if (arg != NULL)
			*arg = mbind->arg;
		return (mbind->cmd);
	}
}