[BACK]Return to mode-key.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/mode-key.c, Revision 1.36

1.36    ! nicm        1: /* $OpenBSD: mode-key.c,v 1.35 2010/03/22 19:10:42 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      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.
                      9:  *
                     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.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
1.11      nicm       21: #include <string.h>
                     22:
1.1       nicm       23: #include "tmux.h"
                     24:
1.10      nicm       25: /*
                     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).
                     31:  *
                     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.
                     35:  *
                     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.
                     40:  */
                     41:
                     42: /* Edit keys command strings. */
1.36    ! nicm       43: const struct mode_key_cmdstr mode_key_cmdstr_edit[] = {
1.10      nicm       44:        { MODEKEYEDIT_BACKSPACE, "backspace" },
1.25      nicm       45:        { MODEKEYEDIT_CANCEL, "cancel" },
1.10      nicm       46:        { MODEKEYEDIT_COMPLETE, "complete" },
                     47:        { MODEKEYEDIT_CURSORLEFT, "cursor-left" },
                     48:        { MODEKEYEDIT_CURSORRIGHT, "cursor-right" },
                     49:        { MODEKEYEDIT_DELETE, "delete" },
1.16      nicm       50:        { MODEKEYEDIT_DELETELINE, "delete-line" },
1.10      nicm       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" },
1.17      nicm       60:        { MODEKEYEDIT_TRANSPOSECHARS, "transpose-chars" },
1.11      nicm       61:
                     62:        { 0, NULL }
1.10      nicm       63: };
1.25      nicm       64:
1.10      nicm       65: /* Choice keys command strings. */
1.36    ! nicm       66: const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
1.10      nicm       67:        { MODEKEYCHOICE_CANCEL, "cancel" },
                     68:        { MODEKEYCHOICE_CHOOSE, "choose" },
                     69:        { MODEKEYCHOICE_DOWN, "down" },
                     70:        { MODEKEYCHOICE_PAGEDOWN, "page-down" },
                     71:        { MODEKEYCHOICE_PAGEUP, "page-up" },
1.30      nicm       72:        { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
                     73:        { MODEKEYCHOICE_SCROLLUP, "scroll-up" },
1.10      nicm       74:        { MODEKEYCHOICE_UP, "up" },
1.11      nicm       75:
                     76:        { 0, NULL }
1.10      nicm       77: };
                     78:
                     79: /* Copy keys command strings. */
1.36    ! nicm       80: const struct mode_key_cmdstr mode_key_cmdstr_copy[] = {
1.15      nicm       81:        { MODEKEYCOPY_BACKTOINDENTATION, "back-to-indentation" },
1.23      nicm       82:        { MODEKEYCOPY_BOTTOMLINE, "bottom-line" },
1.10      nicm       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" },
1.15      nicm       88:        { MODEKEYCOPY_GOTOLINE, "goto-line" },
1.27      nicm       89:        { MODEKEYCOPY_HISTORYBOTTOM, "history-bottom" },
                     90:        { MODEKEYCOPY_HISTORYTOP, "history-top" },
1.35      nicm       91:        { MODEKEYCOPY_JUMP, "jump-forward" },
                     92:        { MODEKEYCOPY_JUMPAGAIN, "jump-again" },
                     93:        { MODEKEYCOPY_JUMPREVERSE, "jump-reverse" },
                     94:        { MODEKEYCOPY_JUMPBACK, "jump-backward" },
1.10      nicm       95:        { MODEKEYCOPY_LEFT, "cursor-left" },
1.32      nicm       96:        { MODEKEYCOPY_RECTANGLETOGGLE, "rectangle-toggle" },
1.23      nicm       97:        { MODEKEYCOPY_MIDDLELINE, "middle-line" },
1.10      nicm       98:        { MODEKEYCOPY_NEXTPAGE, "page-down" },
1.31      nicm       99:        { MODEKEYCOPY_NEXTSPACE, "next-space" },
                    100:        { MODEKEYCOPY_NEXTSPACEEND, "next-space-end" },
1.10      nicm      101:        { MODEKEYCOPY_NEXTWORD, "next-word" },
1.28      nicm      102:        { MODEKEYCOPY_NEXTWORDEND, "next-word-end" },
1.10      nicm      103:        { MODEKEYCOPY_PREVIOUSPAGE, "page-up" },
1.31      nicm      104:        { MODEKEYCOPY_PREVIOUSSPACE, "previous-space" },
1.10      nicm      105:        { MODEKEYCOPY_PREVIOUSWORD, "previous-word" },
                    106:        { MODEKEYCOPY_RIGHT, "cursor-right" },
1.21      nicm      107:        { MODEKEYCOPY_SCROLLDOWN, "scroll-down" },
                    108:        { MODEKEYCOPY_SCROLLUP, "scroll-up" },
1.15      nicm      109:        { MODEKEYCOPY_SEARCHAGAIN, "search-again" },
                    110:        { MODEKEYCOPY_SEARCHDOWN, "search-forward" },
1.33      nicm      111:        { MODEKEYCOPY_SEARCHREVERSE, "search-reverse" },
1.15      nicm      112:        { MODEKEYCOPY_SEARCHUP, "search-backward" },
1.34      nicm      113:        { MODEKEYCOPY_STARTNUMBERPREFIX, "start-number-prefix" },
1.10      nicm      114:        { MODEKEYCOPY_STARTOFLINE, "start-of-line" },
                    115:        { MODEKEYCOPY_STARTSELECTION, "begin-selection" },
1.23      nicm      116:        { MODEKEYCOPY_TOPLINE, "top-line" },
1.10      nicm      117:        { MODEKEYCOPY_UP, "cursor-up" },
1.11      nicm      118:
                    119:        { 0, NULL }
1.10      nicm      120: };
                    121:
1.8       nicm      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:        { '\011' /* Tab */,     0, MODEKEYEDIT_COMPLETE },
                    127:        { '\033' /* Escape */,  0, MODEKEYEDIT_SWITCHMODE },
                    128:        { '\r',                 0, MODEKEYEDIT_ENTER },
                    129:        { KEYC_BSPACE,          0, MODEKEYEDIT_BACKSPACE },
                    130:        { KEYC_DC,              0, MODEKEYEDIT_DELETE },
                    131:
                    132:        { '$',                  1, MODEKEYEDIT_ENDOFLINE },
                    133:        { '0',                  1, MODEKEYEDIT_STARTOFLINE },
                    134:        { 'D',                  1, MODEKEYEDIT_DELETETOENDOFLINE },
                    135:        { '\003' /* C-c */,     1, MODEKEYEDIT_CANCEL },
                    136:        { '\010' /* C-h */,     1, MODEKEYEDIT_BACKSPACE },
                    137:        { '\r',                 1, MODEKEYEDIT_ENTER },
                    138:        { '^',                  1, MODEKEYEDIT_STARTOFLINE },
                    139:        { 'a',                  1, MODEKEYEDIT_SWITCHMODEAPPEND },
1.27      nicm      140:        { 'd',                  1, MODEKEYEDIT_DELETELINE },
1.8       nicm      141:        { 'h',                  1, MODEKEYEDIT_CURSORLEFT },
                    142:        { 'i',                  1, MODEKEYEDIT_SWITCHMODE },
                    143:        { 'j',                  1, MODEKEYEDIT_HISTORYDOWN },
                    144:        { 'k',                  1, MODEKEYEDIT_HISTORYUP },
                    145:        { 'l',                  1, MODEKEYEDIT_CURSORRIGHT },
                    146:        { 'p',                  1, MODEKEYEDIT_PASTE },
                    147:        { KEYC_BSPACE,          1, MODEKEYEDIT_BACKSPACE },
                    148:        { KEYC_DC,              1, MODEKEYEDIT_DELETE },
                    149:        { KEYC_DOWN,            1, MODEKEYEDIT_HISTORYDOWN },
                    150:        { KEYC_LEFT,            1, MODEKEYEDIT_CURSORLEFT },
                    151:        { KEYC_RIGHT,           1, MODEKEYEDIT_CURSORRIGHT },
                    152:        { KEYC_UP,              1, MODEKEYEDIT_HISTORYUP },
                    153:
                    154:        { 0,                   -1, 0 }
                    155: };
1.10      nicm      156: struct mode_key_tree mode_key_tree_vi_edit;
1.8       nicm      157:
                    158: /* vi choice selection keys. */
                    159: const struct mode_key_entry mode_key_vi_choice[] = {
1.30      nicm      160:        { '\002' /* C-b */,     0, MODEKEYCHOICE_PAGEUP },
1.8       nicm      161:        { '\003' /* C-c */,     0, MODEKEYCHOICE_CANCEL },
1.30      nicm      162:        { '\005' /* C-e */,     0, MODEKEYCHOICE_SCROLLDOWN },
                    163:        { '\006' /* C-f */,     0, MODEKEYCHOICE_PAGEDOWN },
                    164:        { '\031' /* C-y */,     0, MODEKEYCHOICE_SCROLLUP },
1.8       nicm      165:        { '\r',                 0, MODEKEYCHOICE_CHOOSE },
                    166:        { 'j',                  0, MODEKEYCHOICE_DOWN },
                    167:        { 'k',                  0, MODEKEYCHOICE_UP },
                    168:        { 'q',                  0, MODEKEYCHOICE_CANCEL },
1.30      nicm      169:        { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
1.8       nicm      170:        { KEYC_DOWN,            0, MODEKEYCHOICE_DOWN },
                    171:        { KEYC_NPAGE,           0, MODEKEYCHOICE_PAGEDOWN },
                    172:        { KEYC_PPAGE,           0, MODEKEYCHOICE_PAGEUP },
1.30      nicm      173:        { KEYC_UP | KEYC_CTRL,  0, MODEKEYCHOICE_SCROLLUP },
1.8       nicm      174:        { KEYC_UP,              0, MODEKEYCHOICE_UP },
                    175:
                    176:        { 0,                    -1, 0 }
                    177: };
1.10      nicm      178: struct mode_key_tree mode_key_tree_vi_choice;
1.8       nicm      179:
                    180: /* vi copy mode keys. */
                    181: const struct mode_key_entry mode_key_vi_copy[] = {
                    182:        { ' ',                  0, MODEKEYCOPY_STARTSELECTION },
                    183:        { '$',                  0, MODEKEYCOPY_ENDOFLINE },
1.35      nicm      184:        { ',',                  0, MODEKEYCOPY_JUMPREVERSE },
                    185:        { ';',                  0, MODEKEYCOPY_JUMPAGAIN },
1.20      nicm      186:        { '/',                  0, MODEKEYCOPY_SEARCHDOWN },
1.8       nicm      187:        { '0',                  0, MODEKEYCOPY_STARTOFLINE },
1.34      nicm      188:        { '1',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    189:        { '2',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    190:        { '3',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    191:        { '4',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    192:        { '5',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    193:        { '6',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    194:        { '7',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    195:        { '8',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    196:        { '9',                  0, MODEKEYCOPY_STARTNUMBERPREFIX },
1.20      nicm      197:        { ':',                  0, MODEKEYCOPY_GOTOLINE },
                    198:        { '?',                  0, MODEKEYCOPY_SEARCHUP },
1.31      nicm      199:        { 'B',                  0, MODEKEYCOPY_PREVIOUSSPACE },
                    200:        { 'E',                  0, MODEKEYCOPY_NEXTSPACEEND },
1.35      nicm      201:        { 'F',                  0, MODEKEYCOPY_JUMPBACK },
1.27      nicm      202:        { 'G',                  0, MODEKEYCOPY_HISTORYBOTTOM },
1.23      nicm      203:        { 'H',                  0, MODEKEYCOPY_TOPLINE },
1.22      nicm      204:        { 'J',                  0, MODEKEYCOPY_SCROLLDOWN },
                    205:        { 'K',                  0, MODEKEYCOPY_SCROLLUP },
1.23      nicm      206:        { 'L',                  0, MODEKEYCOPY_BOTTOMLINE },
                    207:        { 'M',                  0, MODEKEYCOPY_MIDDLELINE },
1.33      nicm      208:        { 'N',                  0, MODEKEYCOPY_SEARCHREVERSE },
1.31      nicm      209:        { 'W',                  0, MODEKEYCOPY_NEXTSPACE },
1.13      nicm      210:        { '\002' /* C-b */,     0, MODEKEYCOPY_PREVIOUSPAGE },
1.9       nicm      211:        { '\003' /* C-c */,     0, MODEKEYCOPY_CANCEL },
1.13      nicm      212:        { '\004' /* C-d */,     0, MODEKEYCOPY_HALFPAGEDOWN },
1.26      nicm      213:        { '\005' /* C-e */,     0, MODEKEYCOPY_SCROLLDOWN },
1.8       nicm      214:        { '\006' /* C-f */,     0, MODEKEYCOPY_NEXTPAGE },
                    215:        { '\010' /* C-h */,     0, MODEKEYCOPY_LEFT },
1.13      nicm      216:        { '\025' /* C-u */,     0, MODEKEYCOPY_HALFPAGEUP },
1.26      nicm      217:        { '\031' /* C-y */,     0, MODEKEYCOPY_SCROLLUP },
1.8       nicm      218:        { '\033' /* Escape */,  0, MODEKEYCOPY_CLEARSELECTION },
                    219:        { '\r',                 0, MODEKEYCOPY_COPYSELECTION },
                    220:        { '^',                  0, MODEKEYCOPY_BACKTOINDENTATION },
                    221:        { 'b',                  0, MODEKEYCOPY_PREVIOUSWORD },
1.29      nicm      222:        { 'e',                  0, MODEKEYCOPY_NEXTWORDEND },
1.35      nicm      223:        { 'f',                  0, MODEKEYCOPY_JUMP },
1.27      nicm      224:        { 'g',                  0, MODEKEYCOPY_HISTORYTOP },
1.8       nicm      225:        { 'h',                  0, MODEKEYCOPY_LEFT },
                    226:        { 'j',                  0, MODEKEYCOPY_DOWN },
                    227:        { 'k',                  0, MODEKEYCOPY_UP },
                    228:        { 'l',                  0, MODEKEYCOPY_RIGHT },
1.15      nicm      229:        { 'n',                  0, MODEKEYCOPY_SEARCHAGAIN },
1.9       nicm      230:        { 'q',                  0, MODEKEYCOPY_CANCEL },
1.32      nicm      231:        { 'v',                  0, MODEKEYCOPY_RECTANGLETOGGLE },
1.8       nicm      232:        { 'w',                  0, MODEKEYCOPY_NEXTWORD },
                    233:        { KEYC_BSPACE,          0, MODEKEYCOPY_LEFT },
1.21      nicm      234:        { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
1.8       nicm      235:        { KEYC_DOWN,            0, MODEKEYCOPY_DOWN },
                    236:        { KEYC_LEFT,            0, MODEKEYCOPY_LEFT },
                    237:        { KEYC_NPAGE,           0, MODEKEYCOPY_NEXTPAGE },
                    238:        { KEYC_PPAGE,           0, MODEKEYCOPY_PREVIOUSPAGE },
                    239:        { KEYC_RIGHT,           0, MODEKEYCOPY_RIGHT },
1.21      nicm      240:        { KEYC_UP | KEYC_CTRL,  0, MODEKEYCOPY_SCROLLUP },
1.8       nicm      241:        { KEYC_UP,              0, MODEKEYCOPY_UP },
1.25      nicm      242:
1.8       nicm      243:        { 0,                    -1, 0 }
                    244: };
1.10      nicm      245: struct mode_key_tree mode_key_tree_vi_copy;
1.8       nicm      246:
                    247: /* emacs editing keys. */
                    248: const struct mode_key_entry mode_key_emacs_edit[] = {
1.25      nicm      249:        { '\001' /* C-a */,     0, MODEKEYEDIT_STARTOFLINE },
1.18      nicm      250:        { '\002' /* C-b */,     0, MODEKEYEDIT_CURSORLEFT },
1.14      nicm      251:        { '\003' /* C-c */,     0, MODEKEYEDIT_CANCEL },
1.8       nicm      252:        { '\004' /* C-d */,     0, MODEKEYEDIT_DELETE },
                    253:        { '\005' /* C-e */,     0, MODEKEYEDIT_ENDOFLINE },
                    254:        { '\006' /* C-f */,     0, MODEKEYEDIT_CURSORRIGHT },
                    255:        { '\010' /* C-H */,     0, MODEKEYEDIT_BACKSPACE },
1.16      nicm      256:        { '\011' /* Tab */,     0, MODEKEYEDIT_COMPLETE },
1.8       nicm      257:        { '\013' /* C-k */,     0, MODEKEYEDIT_DELETETOENDOFLINE },
                    258:        { '\016' /* C-n */,     0, MODEKEYEDIT_HISTORYDOWN },
                    259:        { '\020' /* C-p */,     0, MODEKEYEDIT_HISTORYUP },
1.17      nicm      260:        { '\024' /* C-t */,     0, MODEKEYEDIT_TRANSPOSECHARS },
1.16      nicm      261:        { '\025' /* C-u */,     0, MODEKEYEDIT_DELETELINE },
1.8       nicm      262:        { '\031' /* C-y */,     0, MODEKEYEDIT_PASTE },
1.14      nicm      263:        { '\033' /* Escape */,  0, MODEKEYEDIT_CANCEL },
1.8       nicm      264:        { '\r',                 0, MODEKEYEDIT_ENTER },
1.25      nicm      265:        { 'm' | KEYC_ESCAPE,    0, MODEKEYEDIT_STARTOFLINE },
1.8       nicm      266:        { KEYC_BSPACE,          0, MODEKEYEDIT_BACKSPACE },
                    267:        { KEYC_DC,              0, MODEKEYEDIT_DELETE },
                    268:        { KEYC_DOWN,            0, MODEKEYEDIT_HISTORYDOWN },
                    269:        { KEYC_LEFT,            0, MODEKEYEDIT_CURSORLEFT },
                    270:        { KEYC_RIGHT,           0, MODEKEYEDIT_CURSORRIGHT },
                    271:        { KEYC_UP,              0, MODEKEYEDIT_HISTORYUP },
                    272:
                    273:        { 0,                   -1, 0 }
                    274: };
1.10      nicm      275: struct mode_key_tree mode_key_tree_emacs_edit;
1.8       nicm      276:
                    277: /* emacs choice selection keys. */
                    278: const struct mode_key_entry mode_key_emacs_choice[] = {
                    279:        { '\003' /* C-c */,     0, MODEKEYCHOICE_CANCEL },
1.18      nicm      280:        { '\016' /* C-n */,     0, MODEKEYCHOICE_DOWN },
                    281:        { '\020' /* C-p */,     0, MODEKEYCHOICE_UP },
1.19      nicm      282:        { '\026' /* C-v */,     0, MODEKEYCHOICE_PAGEDOWN },
1.8       nicm      283:        { '\033' /* Escape */,  0, MODEKEYCHOICE_CANCEL },
                    284:        { '\r',                 0, MODEKEYCHOICE_CHOOSE },
                    285:        { 'q',                  0, MODEKEYCHOICE_CANCEL },
1.19      nicm      286:        { 'v' | KEYC_ESCAPE,    0, MODEKEYCHOICE_PAGEUP },
1.30      nicm      287:        { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
1.8       nicm      288:        { KEYC_DOWN,            0, MODEKEYCHOICE_DOWN },
                    289:        { KEYC_NPAGE,           0, MODEKEYCHOICE_PAGEDOWN },
                    290:        { KEYC_PPAGE,           0, MODEKEYCHOICE_PAGEUP },
1.30      nicm      291:        { KEYC_UP | KEYC_CTRL,  0, MODEKEYCHOICE_SCROLLUP },
1.8       nicm      292:        { KEYC_UP,              0, MODEKEYCHOICE_UP },
                    293:
                    294:        { 0,                    -1, 0 }
                    295: };
1.10      nicm      296: struct mode_key_tree mode_key_tree_emacs_choice;
1.8       nicm      297:
                    298: /* emacs copy mode keys. */
                    299: const struct mode_key_entry mode_key_emacs_copy[] = {
                    300:        { ' ',                  0, MODEKEYCOPY_NEXTPAGE },
1.35      nicm      301:        { ',',                  0, MODEKEYCOPY_JUMPREVERSE },
                    302:        { ';',                  0, MODEKEYCOPY_JUMPAGAIN },
1.34      nicm      303:        { '1' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    304:        { '2' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    305:        { '3' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    306:        { '4' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    307:        { '5' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    308:        { '6' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    309:        { '7' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    310:        { '8' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
                    311:        { '9' | KEYC_ESCAPE,    0, MODEKEYCOPY_STARTNUMBERPREFIX },
1.32      nicm      312:        { '<' | KEYC_ESCAPE,    0, MODEKEYCOPY_HISTORYTOP },
                    313:        { '>' | KEYC_ESCAPE,    0, MODEKEYCOPY_HISTORYBOTTOM },
1.35      nicm      314:        { 'F',                  0, MODEKEYCOPY_JUMPBACK },
                    315:        { 'N',                  0, MODEKEYCOPY_SEARCHREVERSE },
1.27      nicm      316:        { 'R' | KEYC_ESCAPE,    0, MODEKEYCOPY_TOPLINE },
1.32      nicm      317:        { 'R',                  0, MODEKEYCOPY_RECTANGLETOGGLE },
1.8       nicm      318:        { '\000' /* C-Space */, 0, MODEKEYCOPY_STARTSELECTION },
                    319:        { '\001' /* C-a */,     0, MODEKEYCOPY_STARTOFLINE },
                    320:        { '\002' /* C-b */,     0, MODEKEYCOPY_LEFT },
1.9       nicm      321:        { '\003' /* C-c */,     0, MODEKEYCOPY_CANCEL },
1.8       nicm      322:        { '\005' /* C-e */,     0, MODEKEYCOPY_ENDOFLINE },
                    323:        { '\006' /* C-f */,     0, MODEKEYCOPY_RIGHT },
                    324:        { '\007' /* C-g */,     0, MODEKEYCOPY_CLEARSELECTION },
                    325:        { '\016' /* C-n */,     0, MODEKEYCOPY_DOWN },
                    326:        { '\020' /* C-p */,     0, MODEKEYCOPY_UP },
1.15      nicm      327:        { '\022' /* C-r */,     0, MODEKEYCOPY_SEARCHUP },
                    328:        { '\023' /* C-s */,     0, MODEKEYCOPY_SEARCHDOWN },
1.8       nicm      329:        { '\026' /* C-v */,     0, MODEKEYCOPY_NEXTPAGE },
                    330:        { '\027' /* C-w */,     0, MODEKEYCOPY_COPYSELECTION },
1.9       nicm      331:        { '\033' /* Escape */,  0, MODEKEYCOPY_CANCEL },
1.33      nicm      332:        { 'N',                  0, MODEKEYCOPY_SEARCHREVERSE },
1.8       nicm      333:        { 'b' | KEYC_ESCAPE,    0, MODEKEYCOPY_PREVIOUSWORD },
1.35      nicm      334:        { 'f',                  0, MODEKEYCOPY_JUMP },
1.28      nicm      335:        { 'f' | KEYC_ESCAPE,    0, MODEKEYCOPY_NEXTWORDEND },
1.15      nicm      336:        { 'g',                  0, MODEKEYCOPY_GOTOLINE },
1.8       nicm      337:        { 'm' | KEYC_ESCAPE,    0, MODEKEYCOPY_BACKTOINDENTATION },
1.15      nicm      338:        { 'n',                  0, MODEKEYCOPY_SEARCHAGAIN },
1.9       nicm      339:        { 'q',                  0, MODEKEYCOPY_CANCEL },
1.23      nicm      340:        { 'r' | KEYC_ESCAPE,    0, MODEKEYCOPY_MIDDLELINE },
1.8       nicm      341:        { 'v' | KEYC_ESCAPE,    0, MODEKEYCOPY_PREVIOUSPAGE },
                    342:        { 'w' | KEYC_ESCAPE,    0, MODEKEYCOPY_COPYSELECTION },
1.21      nicm      343:        { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCOPY_SCROLLDOWN },
1.13      nicm      344:        { KEYC_DOWN | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEDOWN },
1.8       nicm      345:        { KEYC_DOWN,            0, MODEKEYCOPY_DOWN },
                    346:        { KEYC_LEFT,            0, MODEKEYCOPY_LEFT },
                    347:        { KEYC_NPAGE,           0, MODEKEYCOPY_NEXTPAGE },
                    348:        { KEYC_PPAGE,           0, MODEKEYCOPY_PREVIOUSPAGE },
                    349:        { KEYC_RIGHT,           0, MODEKEYCOPY_RIGHT },
1.21      nicm      350:        { KEYC_UP | KEYC_CTRL,  0, MODEKEYCOPY_SCROLLUP },
1.13      nicm      351:        { KEYC_UP | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEUP },
1.8       nicm      352:        { KEYC_UP,              0, MODEKEYCOPY_UP },
                    353:
1.25      nicm      354:        { 0,                    -1, 0 }
1.8       nicm      355: };
1.10      nicm      356: struct mode_key_tree mode_key_tree_emacs_copy;
                    357:
                    358: /* Table mapping key table names to default settings and trees. */
                    359: const struct mode_key_table mode_key_tables[] = {
                    360:        { "vi-edit", mode_key_cmdstr_edit,
                    361:          &mode_key_tree_vi_edit, mode_key_vi_edit },
                    362:        { "vi-choice", mode_key_cmdstr_choice,
                    363:          &mode_key_tree_vi_choice, mode_key_vi_choice },
                    364:        { "vi-copy", mode_key_cmdstr_copy,
                    365:          &mode_key_tree_vi_copy, mode_key_vi_copy },
                    366:        { "emacs-edit", mode_key_cmdstr_edit,
                    367:          &mode_key_tree_emacs_edit, mode_key_emacs_edit },
                    368:        { "emacs-choice", mode_key_cmdstr_choice,
                    369:          &mode_key_tree_emacs_choice, mode_key_emacs_choice },
                    370:        { "emacs-copy", mode_key_cmdstr_copy,
                    371:          &mode_key_tree_emacs_copy, mode_key_emacs_copy },
                    372:
                    373:        { NULL, NULL, NULL, NULL }
                    374: };
                    375:
                    376: SPLAY_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
                    377:
                    378: int
                    379: mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
                    380: {
                    381:        if (mbind1->mode != mbind2->mode)
                    382:                return (mbind1->mode - mbind2->mode);
                    383:        return (mbind1->key - mbind2->key);
                    384: }
                    385:
                    386: const char *
1.36    ! nicm      387: mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
1.10      nicm      388: {
                    389:        for (; cmdstr->name != NULL; cmdstr++) {
                    390:                if (cmdstr->cmd == cmd)
                    391:                        return (cmdstr->name);
1.11      nicm      392:        }
                    393:        return (NULL);
                    394: }
                    395:
                    396: enum mode_key_cmd
1.36    ! nicm      397: mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
1.11      nicm      398: {
                    399:        for (; cmdstr->name != NULL; cmdstr++) {
                    400:                if (strcasecmp(cmdstr->name, name) == 0)
                    401:                        return (cmdstr->cmd);
                    402:        }
                    403:        return (MODEKEY_NONE);
                    404: }
                    405:
                    406: const struct mode_key_table *
                    407: mode_key_findtable(const char *name)
                    408: {
                    409:        const struct mode_key_table     *mtab;
1.25      nicm      410:
1.11      nicm      411:        for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
                    412:                if (strcasecmp(name, mtab->name) == 0)
                    413:                        return (mtab);
1.10      nicm      414:        }
                    415:        return (NULL);
                    416: }
1.1       nicm      417:
                    418: void
1.10      nicm      419: mode_key_init_trees(void)
1.1       nicm      420: {
1.10      nicm      421:        const struct mode_key_table     *mtab;
                    422:        const struct mode_key_entry     *ment;
                    423:        struct mode_key_binding         *mbind;
                    424:
                    425:        for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
                    426:                SPLAY_INIT(mtab->tree);
                    427:                for (ment = mtab->table; ment->mode != -1; ment++) {
                    428:                        mbind = xmalloc(sizeof *mbind);
                    429:                        mbind->key = ment->key;
                    430:                        mbind->mode = ment->mode;
                    431:                        mbind->cmd = ment->cmd;
                    432:                        SPLAY_INSERT(mode_key_tree, mtab->tree, mbind);
                    433:                }
                    434:        }
                    435: }
                    436:
                    437: void
                    438: mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
                    439: {
                    440:        mdata->tree = mtree;
1.8       nicm      441:        mdata->mode = 0;
1.1       nicm      442: }
                    443:
                    444: enum mode_key_cmd
                    445: mode_key_lookup(struct mode_key_data *mdata, int key)
                    446: {
1.10      nicm      447:        struct mode_key_binding *mbind, mtmp;
                    448:
                    449:        mtmp.key = key;
                    450:        mtmp.mode = mdata->mode;
                    451:        if ((mbind = SPLAY_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) {
                    452:                if (mdata->mode != 0)
                    453:                        return (MODEKEY_NONE);
                    454:                return (MODEKEY_OTHER);
                    455:        }
1.1       nicm      456:
1.10      nicm      457:        switch (mbind->cmd) {
                    458:        case MODEKEYEDIT_SWITCHMODE:
                    459:        case MODEKEYEDIT_SWITCHMODEAPPEND:
                    460:                mdata->mode = 1 - mdata->mode;
                    461:                /* FALLTHROUGH */
                    462:        default:
                    463:                return (mbind->cmd);
1.1       nicm      464:        }
                    465: }