[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.39

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