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

Annotation of src/usr.bin/tmux/key-bindings.c, Revision 1.144

1.144   ! nicm        1: /* $OpenBSD: key-bindings.c,v 1.143 2022/07/06 07:36:36 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.56      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
                     21: #include <ctype.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
1.97      nicm       27: #define DEFAULT_SESSION_MENU \
                     28:        " 'Next' 'n' {switch-client -n}" \
                     29:        " 'Previous' 'p' {switch-client -p}" \
                     30:        " ''" \
                     31:        " 'Renumber' 'N' {move-window -r}" \
1.141     nicm       32:        " 'Rename' 'n' {command-prompt -I \"#S\" {rename-session -- '%%'}}" \
1.97      nicm       33:        " ''" \
                     34:        " 'New Session' 's' {new-session}" \
                     35:        " 'New Window' 'w' {new-window}"
                     36: #define DEFAULT_WINDOW_MENU \
1.112     nicm       37:        " '#{?#{>:#{session_windows},1},,-}Swap Left' 'l' {swap-window -t:-1}" \
                     38:        " '#{?#{>:#{session_windows},1},,-}Swap Right' 'r' {swap-window -t:+1}" \
1.97      nicm       39:        " '#{?pane_marked_set,,-}Swap Marked' 's' {swap-window}" \
                     40:        " ''" \
                     41:        " 'Kill' 'X' {kill-window}" \
                     42:        " 'Respawn' 'R' {respawn-window -k}" \
                     43:        " '#{?pane_marked,Unmark,Mark}' 'm' {select-pane -m}" \
1.141     nicm       44:        " 'Rename' 'n' {command-prompt -FI \"#W\" {rename-window -t '#{window_id}' -- '%%'}}" \
1.97      nicm       45:        " ''" \
                     46:        " 'New After' 'w' {new-window -a}" \
                     47:        " 'New At End' 'W' {new-window}"
                     48: #define DEFAULT_PANE_MENU \
1.115     nicm       49:        " '#{?#{m/r:(copy|view)-mode,#{pane_mode}},Go To Top,}' '<' {send -X history-top}" \
                     50:        " '#{?#{m/r:(copy|view)-mode,#{pane_mode}},Go To Bottom,}' '>' {send -X history-bottom}" \
1.109     nicm       51:        " ''" \
1.115     nicm       52:        " '#{?mouse_word,Search For #[underscore]#{=/9/...:mouse_word},}' 'C-r' {if -F '#{?#{m/r:(copy|view)-mode,#{pane_mode}},0,1}' 'copy-mode -t='; send -Xt= search-backward \"#{q:mouse_word}\"}" \
1.109     nicm       53:        " '#{?mouse_word,Type #[underscore]#{=/9/...:mouse_word},}' 'C-y' {copy-mode -q; send-keys -l -- \"#{q:mouse_word}\"}" \
                     54:        " '#{?mouse_word,Copy #[underscore]#{=/9/...:mouse_word},}' 'c' {copy-mode -q; set-buffer -- \"#{q:mouse_word}\"}" \
                     55:        " '#{?mouse_line,Copy Line,}' 'l' {copy-mode -q; set-buffer -- \"#{q:mouse_line}\"}" \
1.143     nicm       56:        " ''" \
                     57:        " '#{?mouse_hyperlink,Type #[underscore]#{=/9/...:mouse_hyperlink},}' 'C-h' {copy-mode -q; send-keys -l -- \"#{q:mouse_hyperlink}\"}" \
                     58:        " '#{?mouse_hyperlink,Copy #[underscore]#{=/9/...:mouse_hyperlink},}' 'h' {copy-mode -q; set-buffer -- \"#{q:mouse_hyperlink}\"}" \
1.97      nicm       59:        " ''" \
                     60:        " 'Horizontal Split' 'h' {split-window -h}" \
                     61:        " 'Vertical Split' 'v' {split-window -v}" \
                     62:        " ''" \
1.112     nicm       63:        " '#{?#{>:#{window_panes},1},,-}Swap Up' 'u' {swap-pane -U}" \
1.113     nicm       64:        " '#{?#{>:#{window_panes},1},,-}Swap Down' 'd' {swap-pane -D}" \
1.97      nicm       65:        " '#{?pane_marked_set,,-}Swap Marked' 's' {swap-pane}" \
                     66:        " ''" \
                     67:        " 'Kill' 'X' {kill-pane}" \
                     68:        " 'Respawn' 'R' {respawn-pane -k}" \
                     69:        " '#{?pane_marked,Unmark,Mark}' 'm' {select-pane -m}" \
1.114     nicm       70:        " '#{?#{>:#{window_panes},1},,-}#{?window_zoomed_flag,Unzoom,Zoom}' 'z' {resize-pane -Z}"
1.97      nicm       71:
1.86      nicm       72: static int key_bindings_cmp(struct key_binding *, struct key_binding *);
                     73: RB_GENERATE_STATIC(key_bindings, key_binding, entry, key_bindings_cmp);
                     74: static int key_table_cmp(struct key_table *, struct key_table *);
                     75: RB_GENERATE_STATIC(key_tables, key_table, entry, key_table_cmp);
                     76: static struct key_tables key_tables = RB_INITIALIZER(&key_tables);
1.1       nicm       77:
1.86      nicm       78: static int
                     79: key_table_cmp(struct key_table *table1, struct key_table *table2)
1.45      nicm       80: {
1.86      nicm       81:        return (strcmp(table1->name, table2->name));
1.45      nicm       82: }
1.1       nicm       83:
1.86      nicm       84: static int
1.1       nicm       85: key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
                     86: {
1.55      nicm       87:        if (bd1->key < bd2->key)
                     88:                return (-1);
                     89:        if (bd1->key > bd2->key)
                     90:                return (1);
                     91:        return (0);
1.45      nicm       92: }
                     93:
1.106     nicm       94: static void
1.125     nicm       95: key_bindings_free(struct key_binding *bd)
1.106     nicm       96: {
                     97:        cmd_list_free(bd->cmdlist);
1.107     nicm       98:        free((void *)bd->note);
1.106     nicm       99:        free(bd);
                    100: }
                    101:
1.45      nicm      102: struct key_table *
                    103: key_bindings_get_table(const char *name, int create)
                    104: {
                    105:        struct key_table        table_find, *table;
                    106:
                    107:        table_find.name = name;
                    108:        table = RB_FIND(key_tables, &key_tables, &table_find);
                    109:        if (table != NULL || !create)
                    110:                return (table);
1.8       nicm      111:
1.45      nicm      112:        table = xmalloc(sizeof *table);
                    113:        table->name = xstrdup(name);
                    114:        RB_INIT(&table->key_bindings);
1.125     nicm      115:        RB_INIT(&table->default_key_bindings);
1.8       nicm      116:
1.45      nicm      117:        table->references = 1; /* one reference in key_tables */
                    118:        RB_INSERT(key_tables, &key_tables, table);
                    119:
                    120:        return (table);
1.1       nicm      121: }
                    122:
1.86      nicm      123: struct key_table *
                    124: key_bindings_first_table(void)
                    125: {
                    126:        return (RB_MIN(key_tables, &key_tables));
                    127: }
                    128:
                    129: struct key_table *
                    130: key_bindings_next_table(struct key_table *table)
                    131: {
                    132:        return (RB_NEXT(key_tables, &key_tables, table));
                    133: }
                    134:
1.45      nicm      135: void
                    136: key_bindings_unref_table(struct key_table *table)
1.1       nicm      137: {
1.45      nicm      138:        struct key_binding      *bd;
1.57      nicm      139:        struct key_binding      *bd1;
1.45      nicm      140:
                    141:        if (--table->references != 0)
                    142:                return;
                    143:
1.125     nicm      144:        RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) {
                    145:                RB_REMOVE(key_bindings, &table->key_bindings, bd);
                    146:                key_bindings_free(bd);
                    147:        }
                    148:        RB_FOREACH_SAFE(bd, key_bindings, &table->default_key_bindings, bd1) {
                    149:                RB_REMOVE(key_bindings, &table->default_key_bindings, bd);
                    150:                key_bindings_free(bd);
                    151:        }
1.1       nicm      152:
1.45      nicm      153:        free((void *)table->name);
                    154:        free(table);
1.86      nicm      155: }
                    156:
                    157: struct key_binding *
                    158: key_bindings_get(struct key_table *table, key_code key)
                    159: {
                    160:        struct key_binding      bd;
                    161:
                    162:        bd.key = key;
                    163:        return (RB_FIND(key_bindings, &table->key_bindings, &bd));
                    164: }
                    165:
                    166: struct key_binding *
1.125     nicm      167: key_bindings_get_default(struct key_table *table, key_code key)
                    168: {
                    169:        struct key_binding      bd;
                    170:
                    171:        bd.key = key;
                    172:        return (RB_FIND(key_bindings, &table->default_key_bindings, &bd));
                    173: }
                    174:
                    175: struct key_binding *
1.86      nicm      176: key_bindings_first(struct key_table *table)
                    177: {
                    178:        return (RB_MIN(key_bindings, &table->key_bindings));
                    179: }
                    180:
                    181: struct key_binding *
                    182: key_bindings_next(__unused struct key_table *table, struct key_binding *bd)
                    183: {
                    184:        return (RB_NEXT(key_bindings, &table->key_bindings, bd));
1.1       nicm      185: }
                    186:
                    187: void
1.107     nicm      188: key_bindings_add(const char *name, key_code key, const char *note, int repeat,
1.45      nicm      189:     struct cmd_list *cmdlist)
1.1       nicm      190: {
1.45      nicm      191:        struct key_table        *table;
1.106     nicm      192:        struct key_binding      *bd;
1.139     nicm      193:        char                    *s;
1.45      nicm      194:
                    195:        table = key_bindings_get_table(name, 1);
1.1       nicm      196:
1.127     nicm      197:        bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
1.131     nicm      198:        if (cmdlist == NULL) {
                    199:                if (bd != NULL) {
                    200:                        free((void *)bd->note);
                    201:                        if (note != NULL)
                    202:                                bd->note = xstrdup(note);
                    203:                        else
                    204:                                bd->note = NULL;
                    205:                }
                    206:                return;
                    207:        }
1.125     nicm      208:        if (bd != NULL) {
                    209:                RB_REMOVE(key_bindings, &table->key_bindings, bd);
                    210:                key_bindings_free(bd);
                    211:        }
1.15      nicm      212:
1.76      nicm      213:        bd = xcalloc(1, sizeof *bd);
1.128     nicm      214:        bd->key = (key & ~KEYC_MASK_FLAGS);
1.107     nicm      215:        if (note != NULL)
                    216:                bd->note = xstrdup(note);
1.45      nicm      217:        RB_INSERT(key_bindings, &table->key_bindings, bd);
1.15      nicm      218:
1.75      nicm      219:        if (repeat)
                    220:                bd->flags |= KEY_BINDING_REPEAT;
1.1       nicm      221:        bd->cmdlist = cmdlist;
1.133     nicm      222:
1.139     nicm      223:        s = cmd_list_print(bd->cmdlist, 0);
1.133     nicm      224:        log_debug("%s: %#llx %s = %s", __func__, bd->key,
1.139     nicm      225:            key_string_lookup_key(bd->key, 1), s);
                    226:        free(s);
1.1       nicm      227: }
                    228:
                    229: void
1.55      nicm      230: key_bindings_remove(const char *name, key_code key)
1.1       nicm      231: {
1.45      nicm      232:        struct key_table        *table;
1.106     nicm      233:        struct key_binding      *bd;
1.45      nicm      234:
                    235:        table = key_bindings_get_table(name, 0);
                    236:        if (table == NULL)
                    237:                return;
1.1       nicm      238:
1.127     nicm      239:        bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
1.45      nicm      240:        if (bd == NULL)
1.1       nicm      241:                return;
1.133     nicm      242:
                    243:        log_debug("%s: %#llx %s", __func__, bd->key,
                    244:            key_string_lookup_key(bd->key, 1));
1.45      nicm      245:
1.125     nicm      246:        RB_REMOVE(key_bindings, &table->key_bindings, bd);
                    247:        key_bindings_free(bd);
                    248:
                    249:        if (RB_EMPTY(&table->key_bindings) &&
                    250:            RB_EMPTY(&table->default_key_bindings)) {
1.45      nicm      251:                RB_REMOVE(key_tables, &key_tables, table);
                    252:                key_bindings_unref_table(table);
                    253:        }
                    254: }
                    255:
                    256: void
1.130     nicm      257: key_bindings_reset(const char *name, key_code key)
                    258: {
                    259:        struct key_table        *table;
                    260:        struct key_binding      *bd, *dd;
                    261:
                    262:        table = key_bindings_get_table(name, 0);
                    263:        if (table == NULL)
                    264:                return;
                    265:
                    266:        bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
                    267:        if (bd == NULL)
                    268:                return;
                    269:
                    270:        dd = key_bindings_get_default(table, bd->key);
                    271:        if (dd == NULL) {
                    272:                key_bindings_remove(name, bd->key);
                    273:                return;
                    274:        }
                    275:
                    276:        cmd_list_free(bd->cmdlist);
                    277:        bd->cmdlist = dd->cmdlist;
                    278:        bd->cmdlist->references++;
                    279:
                    280:        free((void *)bd->note);
                    281:        if (dd->note != NULL)
                    282:                bd->note = xstrdup(dd->note);
                    283:        else
                    284:                bd->note = NULL;
                    285:        bd->flags = dd->flags;
                    286: }
                    287:
                    288: void
1.45      nicm      289: key_bindings_remove_table(const char *name)
                    290: {
                    291:        struct key_table        *table;
1.82      nicm      292:        struct client           *c;
1.45      nicm      293:
                    294:        table = key_bindings_get_table(name, 0);
                    295:        if (table != NULL) {
                    296:                RB_REMOVE(key_tables, &key_tables, table);
                    297:                key_bindings_unref_table(table);
1.82      nicm      298:        }
                    299:        TAILQ_FOREACH(c, &clients, entry) {
                    300:                if (c->keytable == table)
                    301:                        server_client_set_key_table(c, NULL);
1.45      nicm      302:        }
1.130     nicm      303: }
                    304:
                    305: void
                    306: key_bindings_reset_table(const char *name)
                    307: {
                    308:        struct key_table        *table;
                    309:        struct key_binding      *bd, *bd1;
                    310:
                    311:        table = key_bindings_get_table(name, 0);
                    312:        if (table == NULL)
                    313:                return;
                    314:        if (RB_EMPTY(&table->default_key_bindings)) {
                    315:                key_bindings_remove_table(name);
                    316:                return;
                    317:        }
                    318:        RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1)
                    319:                key_bindings_reset(name, bd->key);
1.1       nicm      320: }
                    321:
1.125     nicm      322: static enum cmd_retval
                    323: key_bindings_init_done(__unused struct cmdq_item *item, __unused void *data)
                    324: {
                    325:        struct key_table        *table;
                    326:        struct key_binding      *bd, *new_bd;
                    327:
                    328:        RB_FOREACH(table, key_tables, &key_tables) {
                    329:                RB_FOREACH(bd, key_bindings, &table->key_bindings) {
                    330:                        new_bd = xcalloc(1, sizeof *bd);
                    331:                        new_bd->key = bd->key;
                    332:                        if (bd->note != NULL)
                    333:                                new_bd->note = xstrdup(bd->note);
                    334:                        new_bd->flags = bd->flags;
                    335:                        new_bd->cmdlist = bd->cmdlist;
                    336:                        new_bd->cmdlist->references++;
                    337:                        RB_INSERT(key_bindings, &table->default_key_bindings,
                    338:                            new_bd);
                    339:                }
                    340:        }
                    341:        return (CMD_RETURN_NORMAL);
                    342: }
                    343:
1.1       nicm      344: void
                    345: key_bindings_init(void)
                    346: {
1.47      nicm      347:        static const char *defaults[] = {
1.108     nicm      348:                /* Prefix keys. */
1.138     nicm      349:                "bind -N 'Send the prefix key' C-b { send-prefix }",
                    350:                "bind -N 'Rotate through the panes' C-o { rotate-window }",
                    351:                "bind -N 'Suspend the current client' C-z { suspend-client }",
                    352:                "bind -N 'Select next layout' Space { next-layout }",
                    353:                "bind -N 'Break pane to a new window' ! { break-pane }",
                    354:                "bind -N 'Split window vertically' '\"' { split-window }",
                    355:                "bind -N 'List all paste buffers' '#' { list-buffers }",
1.141     nicm      356:                "bind -N 'Rename current session' '$' { command-prompt -I'#S' { rename-session -- '%%' } }",
1.138     nicm      357:                "bind -N 'Split window horizontally' % { split-window -h }",
                    358:                "bind -N 'Kill current window' & { confirm-before -p\"kill-window #W? (y/n)\" kill-window }",
1.141     nicm      359:                "bind -N 'Prompt for window index to select' \"'\" { command-prompt -T window-target -pindex { select-window -t ':%%' } }",
1.138     nicm      360:                "bind -N 'Switch to previous client' ( { switch-client -p }",
                    361:                "bind -N 'Switch to next client' ) { switch-client -n }",
1.141     nicm      362:                "bind -N 'Rename current window' , { command-prompt -I'#W' { rename-window -- '%%' } }",
1.138     nicm      363:                "bind -N 'Delete the most recent paste buffer' - { delete-buffer }",
1.141     nicm      364:                "bind -N 'Move the current window' . { command-prompt -T target { move-window -t '%%' } }",
                    365:                "bind -N 'Describe key binding' '/' { command-prompt -kpkey  { list-keys -1N '%%' } }",
1.138     nicm      366:                "bind -N 'Select window 0' 0 { select-window -t:=0 }",
                    367:                "bind -N 'Select window 1' 1 { select-window -t:=1 }",
                    368:                "bind -N 'Select window 2' 2 { select-window -t:=2 }",
                    369:                "bind -N 'Select window 3' 3 { select-window -t:=3 }",
                    370:                "bind -N 'Select window 4' 4 { select-window -t:=4 }",
                    371:                "bind -N 'Select window 5' 5 { select-window -t:=5 }",
                    372:                "bind -N 'Select window 6' 6 { select-window -t:=6 }",
                    373:                "bind -N 'Select window 7' 7 { select-window -t:=7 }",
                    374:                "bind -N 'Select window 8' 8 { select-window -t:=8 }",
                    375:                "bind -N 'Select window 9' 9 { select-window -t:=9 }",
                    376:                "bind -N 'Prompt for a command' : { command-prompt }",
                    377:                "bind -N 'Move to the previously active pane' \\; { last-pane }",
                    378:                "bind -N 'Choose a paste buffer from a list' = { choose-buffer -Z }",
                    379:                "bind -N 'List key bindings' ? { list-keys -N }",
                    380:                "bind -N 'Choose a client from a list' D { choose-client -Z }",
                    381:                "bind -N 'Spread panes out evenly' E { select-layout -E }",
                    382:                "bind -N 'Switch to the last client' L { switch-client -l }",
                    383:                "bind -N 'Clear the marked pane' M { select-pane -M }",
                    384:                "bind -N 'Enter copy mode' [ { copy-mode }",
                    385:                "bind -N 'Paste the most recent paste buffer' ] { paste-buffer -p }",
                    386:                "bind -N 'Create a new window' c { new-window }",
                    387:                "bind -N 'Detach the current client' d { detach-client }",
1.141     nicm      388:                "bind -N 'Search for a pane' f { command-prompt { find-window -Z -- '%%' } }",
1.138     nicm      389:                "bind -N 'Display window information' i { display-message }",
                    390:                "bind -N 'Select the previously current window' l { last-window }",
                    391:                "bind -N 'Toggle the marked pane' m { select-pane -m }",
                    392:                "bind -N 'Select the next window' n { next-window }",
                    393:                "bind -N 'Select the next pane' o { select-pane -t:.+ }",
                    394:                "bind -N 'Customize options' C { customize-mode -Z }",
                    395:                "bind -N 'Select the previous window' p { previous-window }",
                    396:                "bind -N 'Display pane numbers' q { display-panes }",
                    397:                "bind -N 'Redraw the current client' r { refresh-client }",
                    398:                "bind -N 'Choose a session from a list' s { choose-tree -Zs }",
                    399:                "bind -N 'Show a clock' t { clock-mode }",
                    400:                "bind -N 'Choose a window from a list' w { choose-tree -Zw }",
                    401:                "bind -N 'Kill the active pane' x { confirm-before -p\"kill-pane #P? (y/n)\" kill-pane }",
                    402:                "bind -N 'Zoom the active pane' z { resize-pane -Z }",
                    403:                "bind -N 'Swap the active pane with the pane above' '{' { swap-pane -U }",
                    404:                "bind -N 'Swap the active pane with the pane below' '}' { swap-pane -D }",
                    405:                "bind -N 'Show messages' '~' { show-messages }",
                    406:                "bind -N 'Enter copy mode and scroll up' PPage { copy-mode -u }",
                    407:                "bind -N 'Select the pane above the active pane' -r Up { select-pane -U }",
                    408:                "bind -N 'Select the pane below the active pane' -r Down { select-pane -D }",
                    409:                "bind -N 'Select the pane to the left of the active pane' -r Left { select-pane -L }",
                    410:                "bind -N 'Select the pane to the right of the active pane' -r Right { select-pane -R }",
                    411:                "bind -N 'Set the even-horizontal layout' M-1 { select-layout even-horizontal }",
                    412:                "bind -N 'Set the even-vertical layout' M-2 { select-layout even-vertical }",
                    413:                "bind -N 'Set the main-horizontal layout' M-3 { select-layout main-horizontal }",
                    414:                "bind -N 'Set the main-vertical layout' M-4 { select-layout main-vertical }",
                    415:                "bind -N 'Select the tiled layout' M-5 { select-layout tiled }",
                    416:                "bind -N 'Select the next window with an alert' M-n { next-window -a }",
                    417:                "bind -N 'Rotate through the panes in reverse' M-o { rotate-window -D }",
                    418:                "bind -N 'Select the previous window with an alert' M-p { previous-window -a }",
                    419:                "bind -N 'Move the visible part of the window up' -r S-Up { refresh-client -U 10 }",
                    420:                "bind -N 'Move the visible part of the window down' -r S-Down { refresh-client -D 10 }",
                    421:                "bind -N 'Move the visible part of the window left' -r S-Left { refresh-client -L 10 }",
                    422:                "bind -N 'Move the visible part of the window right' -r S-Right { refresh-client -R 10 }",
                    423:                "bind -N 'Reset so the visible part of the window follows the cursor' -r DC { refresh-client -c }",
                    424:                "bind -N 'Resize the pane up by 5' -r M-Up { resize-pane -U 5 }",
                    425:                "bind -N 'Resize the pane down by 5' -r M-Down { resize-pane -D 5 }",
                    426:                "bind -N 'Resize the pane left by 5' -r M-Left { resize-pane -L 5 }",
                    427:                "bind -N 'Resize the pane right by 5' -r M-Right { resize-pane -R 5 }",
                    428:                "bind -N 'Resize the pane up' -r C-Up { resize-pane -U }",
                    429:                "bind -N 'Resize the pane down' -r C-Down { resize-pane -D }",
                    430:                "bind -N 'Resize the pane left' -r C-Left { resize-pane -L }",
                    431:                "bind -N 'Resize the pane right' -r C-Right { resize-pane -R }",
1.91      nicm      432:
1.108     nicm      433:                /* Menu keys */
1.138     nicm      434:                "bind < { display-menu -xW -yW -T '#[align=centre]#{window_index}:#{window_name}' " DEFAULT_WINDOW_MENU " }",
                    435:                "bind > { display-menu -xP -yP -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU " }",
1.108     nicm      436:
                    437:                /* Mouse button 1 down on pane. */
1.138     nicm      438:                "bind -n MouseDown1Pane { select-pane -t=; send -M }",
1.108     nicm      439:
                    440:                /* Mouse button 1 drag on pane. */
1.138     nicm      441:                "bind -n MouseDrag1Pane { if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -M } }",
1.108     nicm      442:
                    443:                /* Mouse wheel up on pane. */
1.138     nicm      444:                "bind -n WheelUpPane { if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -e } }",
1.108     nicm      445:
                    446:                /* Mouse button 2 down on pane. */
1.138     nicm      447:                "bind -n MouseDown2Pane { select-pane -t=; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { paste -p } }",
1.108     nicm      448:
                    449:                /* Mouse button 1 double click on pane. */
1.138     nicm      450:                "bind -n DoubleClick1Pane { select-pane -t=; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-word; run -d0.3; send -X copy-pipe-and-cancel } }",
1.108     nicm      451:
                    452:                /* Mouse button 1 triple click on pane. */
1.138     nicm      453:                "bind -n TripleClick1Pane { select-pane -t=; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-line; run -d0.3; send -X copy-pipe-and-cancel } }",
1.108     nicm      454:
                    455:                /* Mouse button 1 drag on border. */
1.138     nicm      456:                "bind -n MouseDrag1Border { resize-pane -M }",
1.108     nicm      457:
                    458:                /* Mouse button 1 down on status line. */
1.138     nicm      459:                "bind -n MouseDown1Status { select-window -t= }",
1.108     nicm      460:
                    461:                /* Mouse wheel down on status line. */
1.138     nicm      462:                "bind -n WheelDownStatus { next-window }",
1.108     nicm      463:
                    464:                /* Mouse wheel up on status line. */
1.138     nicm      465:                "bind -n WheelUpStatus { previous-window }",
1.95      nicm      466:
1.108     nicm      467:                /* Mouse button 3 down on status left. */
1.138     nicm      468:                "bind -n MouseDown3StatusLeft { display-menu -t= -xM -yW -T '#[align=centre]#{session_name}' " DEFAULT_SESSION_MENU " }",
1.108     nicm      469:
                    470:                /* Mouse button 3 down on status line. */
1.138     nicm      471:                "bind -n MouseDown3Status { display-menu -t= -xW -yW -T '#[align=centre]#{window_index}:#{window_name}' " DEFAULT_WINDOW_MENU "}",
1.108     nicm      472:
                    473:                /* Mouse button 3 down on pane. */
1.138     nicm      474:                "bind -n MouseDown3Pane { if -Ft= '#{||:#{mouse_any_flag},#{&&:#{pane_in_mode},#{?#{m/r:(copy|view)-mode,#{pane_mode}},0,1}}}' { select-pane -t=; send -M } { display-menu -t= -xM -yM -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU " } }",
                    475:                "bind -n M-MouseDown3Pane { display-menu -t= -xM -yM -T '#[align=centre]#{pane_index} (#{pane_id})' " DEFAULT_PANE_MENU " }",
1.58      nicm      476:
1.108     nicm      477:                /* Copy mode (emacs) keys. */
1.138     nicm      478:                "bind -Tcopy-mode C-Space { send -X begin-selection }",
                    479:                "bind -Tcopy-mode C-a { send -X start-of-line }",
                    480:                "bind -Tcopy-mode C-c { send -X cancel }",
                    481:                "bind -Tcopy-mode C-e { send -X end-of-line }",
                    482:                "bind -Tcopy-mode C-f { send -X cursor-right }",
                    483:                "bind -Tcopy-mode C-b { send -X cursor-left }",
                    484:                "bind -Tcopy-mode C-g { send -X clear-selection }",
                    485:                "bind -Tcopy-mode C-k { send -X copy-pipe-end-of-line-and-cancel }",
                    486:                "bind -Tcopy-mode C-n { send -X cursor-down }",
                    487:                "bind -Tcopy-mode C-p { send -X cursor-up }",
1.141     nicm      488:                "bind -Tcopy-mode C-r { command-prompt -T search -ip'(search up)' -I'#{pane_search_string}' { send -X search-backward-incremental '%%' } }",
                    489:                "bind -Tcopy-mode C-s { command-prompt -T search -ip'(search down)' -I'#{pane_search_string}' { send -X search-forward-incremental '%%' } }",
1.138     nicm      490:                "bind -Tcopy-mode C-v { send -X page-down }",
                    491:                "bind -Tcopy-mode C-w { send -X copy-pipe-and-cancel }",
                    492:                "bind -Tcopy-mode Escape { send -X cancel }",
                    493:                "bind -Tcopy-mode Space { send -X page-down }",
                    494:                "bind -Tcopy-mode , { send -X jump-reverse }",
                    495:                "bind -Tcopy-mode \\; { send -X jump-again }",
1.141     nicm      496:                "bind -Tcopy-mode F { command-prompt -1p'(jump backward)' { send -X jump-backward '%%' } }",
1.138     nicm      497:                "bind -Tcopy-mode N { send -X search-reverse }",
1.142     nicm      498:                "bind -Tcopy-mode P { send -X toggle-position }",
1.138     nicm      499:                "bind -Tcopy-mode R { send -X rectangle-toggle }",
1.141     nicm      500:                "bind -Tcopy-mode T { command-prompt -1p'(jump to backward)' { send -X jump-to-backward '%%' } }",
1.138     nicm      501:                "bind -Tcopy-mode X { send -X set-mark }",
1.141     nicm      502:                "bind -Tcopy-mode f { command-prompt -1p'(jump forward)' { send -X jump-forward '%%' } }",
                    503:                "bind -Tcopy-mode g { command-prompt -p'(goto line)' { send -X goto-line '%%' } }",
1.138     nicm      504:                "bind -Tcopy-mode n { send -X search-again }",
                    505:                "bind -Tcopy-mode q { send -X cancel }",
                    506:                "bind -Tcopy-mode r { send -X refresh-from-pane }",
1.141     nicm      507:                "bind -Tcopy-mode t { command-prompt -1p'(jump to forward)' { send -X jump-to-forward '%%' } }",
1.138     nicm      508:                "bind -Tcopy-mode Home { send -X start-of-line }",
                    509:                "bind -Tcopy-mode End { send -X end-of-line }",
1.66      nicm      510:                "bind -Tcopy-mode MouseDown1Pane select-pane",
1.138     nicm      511:                "bind -Tcopy-mode MouseDrag1Pane { select-pane; send -X begin-selection }",
                    512:                "bind -Tcopy-mode MouseDragEnd1Pane { send -X copy-pipe-and-cancel }",
                    513:                "bind -Tcopy-mode WheelUpPane { select-pane; send -N5 -X scroll-up }",
                    514:                "bind -Tcopy-mode WheelDownPane { select-pane; send -N5 -X scroll-down }",
                    515:                "bind -Tcopy-mode DoubleClick1Pane { select-pane; send -X select-word; run -d0.3; send -X copy-pipe-and-cancel }",
                    516:                "bind -Tcopy-mode TripleClick1Pane { select-pane; send -X select-line; run -d0.3; send -X copy-pipe-and-cancel }",
                    517:                "bind -Tcopy-mode NPage { send -X page-down }",
                    518:                "bind -Tcopy-mode PPage { send -X page-up }",
                    519:                "bind -Tcopy-mode Up { send -X cursor-up }",
                    520:                "bind -Tcopy-mode Down { send -X cursor-down }",
                    521:                "bind -Tcopy-mode Left { send -X cursor-left }",
                    522:                "bind -Tcopy-mode Right { send -X cursor-right }",
1.141     nicm      523:                "bind -Tcopy-mode M-1 { command-prompt -Np'(repeat)' -I1 { send -N '%%' } }",
                    524:                "bind -Tcopy-mode M-2 { command-prompt -Np'(repeat)' -I2 { send -N '%%' } }",
                    525:                "bind -Tcopy-mode M-3 { command-prompt -Np'(repeat)' -I3 { send -N '%%' } }",
                    526:                "bind -Tcopy-mode M-4 { command-prompt -Np'(repeat)' -I4 { send -N '%%' } }",
                    527:                "bind -Tcopy-mode M-5 { command-prompt -Np'(repeat)' -I5 { send -N '%%' } }",
                    528:                "bind -Tcopy-mode M-6 { command-prompt -Np'(repeat)' -I6 { send -N '%%' } }",
                    529:                "bind -Tcopy-mode M-7 { command-prompt -Np'(repeat)' -I7 { send -N '%%' } }",
                    530:                "bind -Tcopy-mode M-8 { command-prompt -Np'(repeat)' -I8 { send -N '%%' } }",
                    531:                "bind -Tcopy-mode M-9 { command-prompt -Np'(repeat)' -I9 { send -N '%%' } }",
1.138     nicm      532:                "bind -Tcopy-mode M-< { send -X history-top }",
                    533:                "bind -Tcopy-mode M-> { send -X history-bottom }",
                    534:                "bind -Tcopy-mode M-R { send -X top-line }",
                    535:                "bind -Tcopy-mode M-b { send -X previous-word }",
                    536:                "bind -Tcopy-mode C-M-b { send -X previous-matching-bracket }",
                    537:                "bind -Tcopy-mode M-f { send -X next-word-end }",
                    538:                "bind -Tcopy-mode C-M-f { send -X next-matching-bracket }",
                    539:                "bind -Tcopy-mode M-m { send -X back-to-indentation }",
                    540:                "bind -Tcopy-mode M-r { send -X middle-line }",
                    541:                "bind -Tcopy-mode M-v { send -X page-up }",
                    542:                "bind -Tcopy-mode M-w { send -X copy-pipe-and-cancel }",
                    543:                "bind -Tcopy-mode M-x { send -X jump-to-mark }",
                    544:                "bind -Tcopy-mode 'M-{' { send -X previous-paragraph }",
                    545:                "bind -Tcopy-mode 'M-}' { send -X next-paragraph }",
                    546:                "bind -Tcopy-mode M-Up { send -X halfpage-up }",
                    547:                "bind -Tcopy-mode M-Down { send -X halfpage-down }",
                    548:                "bind -Tcopy-mode C-Up { send -X scroll-up }",
                    549:                "bind -Tcopy-mode C-Down { send -X scroll-down }",
1.58      nicm      550:
1.108     nicm      551:                /* Copy mode (vi) keys. */
1.138     nicm      552:                "bind -Tcopy-mode-vi '#' { send -FX search-backward '#{copy_cursor_word}' }",
                    553:                "bind -Tcopy-mode-vi * { send -FX search-forward '#{copy_cursor_word}' }",
                    554:                "bind -Tcopy-mode-vi C-c { send -X cancel }",
                    555:                "bind -Tcopy-mode-vi C-d { send -X halfpage-down }",
                    556:                "bind -Tcopy-mode-vi C-e { send -X scroll-down }",
                    557:                "bind -Tcopy-mode-vi C-b { send -X page-up }",
                    558:                "bind -Tcopy-mode-vi C-f { send -X page-down }",
                    559:                "bind -Tcopy-mode-vi C-h { send -X cursor-left }",
                    560:                "bind -Tcopy-mode-vi C-j { send -X copy-pipe-and-cancel }",
                    561:                "bind -Tcopy-mode-vi Enter { send -X copy-pipe-and-cancel }",
                    562:                "bind -Tcopy-mode-vi C-u { send -X halfpage-up }",
                    563:                "bind -Tcopy-mode-vi C-v { send -X rectangle-toggle }",
                    564:                "bind -Tcopy-mode-vi C-y { send -X scroll-up }",
                    565:                "bind -Tcopy-mode-vi Escape { send -X clear-selection }",
                    566:                "bind -Tcopy-mode-vi Space { send -X begin-selection }",
                    567:                "bind -Tcopy-mode-vi '$' { send -X end-of-line }",
                    568:                "bind -Tcopy-mode-vi , { send -X jump-reverse }",
1.141     nicm      569:                "bind -Tcopy-mode-vi / { command-prompt -T search -p'(search down)' { send -X search-forward '%%' } }",
1.138     nicm      570:                "bind -Tcopy-mode-vi 0 { send -X start-of-line }",
1.141     nicm      571:                "bind -Tcopy-mode-vi 1 { command-prompt -Np'(repeat)' -I1 { send -N '%%' } }",
                    572:                "bind -Tcopy-mode-vi 2 { command-prompt -Np'(repeat)' -I2 { send -N '%%' } }",
                    573:                "bind -Tcopy-mode-vi 3 { command-prompt -Np'(repeat)' -I3 { send -N '%%' } }",
                    574:                "bind -Tcopy-mode-vi 4 { command-prompt -Np'(repeat)' -I4 { send -N '%%' } }",
                    575:                "bind -Tcopy-mode-vi 5 { command-prompt -Np'(repeat)' -I5 { send -N '%%' } }",
                    576:                "bind -Tcopy-mode-vi 6 { command-prompt -Np'(repeat)' -I6 { send -N '%%' } }",
                    577:                "bind -Tcopy-mode-vi 7 { command-prompt -Np'(repeat)' -I7 { send -N '%%' } }",
                    578:                "bind -Tcopy-mode-vi 8 { command-prompt -Np'(repeat)' -I8 { send -N '%%' } }",
                    579:                "bind -Tcopy-mode-vi 9 { command-prompt -Np'(repeat)' -I9 { send -N '%%' } }",
                    580:                "bind -Tcopy-mode-vi : { command-prompt -p'(goto line)' { send -X goto-line '%%' } }",
1.138     nicm      581:                "bind -Tcopy-mode-vi \\; { send -X jump-again }",
1.141     nicm      582:                "bind -Tcopy-mode-vi ? { command-prompt -T search -p'(search up)' { send -X search-backward '%%' } }",
1.138     nicm      583:                "bind -Tcopy-mode-vi A { send -X append-selection-and-cancel }",
                    584:                "bind -Tcopy-mode-vi B { send -X previous-space }",
                    585:                "bind -Tcopy-mode-vi D { send -X copy-pipe-end-of-line-and-cancel }",
                    586:                "bind -Tcopy-mode-vi E { send -X next-space-end }",
1.141     nicm      587:                "bind -Tcopy-mode-vi F { command-prompt -1p'(jump backward)' { send -X jump-backward '%%' } }",
1.138     nicm      588:                "bind -Tcopy-mode-vi G { send -X history-bottom }",
                    589:                "bind -Tcopy-mode-vi H { send -X top-line }",
                    590:                "bind -Tcopy-mode-vi J { send -X scroll-down }",
                    591:                "bind -Tcopy-mode-vi K { send -X scroll-up }",
                    592:                "bind -Tcopy-mode-vi L { send -X bottom-line }",
                    593:                "bind -Tcopy-mode-vi M { send -X middle-line }",
                    594:                "bind -Tcopy-mode-vi N { send -X search-reverse }",
1.142     nicm      595:                "bind -Tcopy-mode-vi P { send -X toggle-position }",
1.141     nicm      596:                "bind -Tcopy-mode-vi T { command-prompt -1p'(jump to backward)' { send -X jump-to-backward '%%' } }",
1.138     nicm      597:                "bind -Tcopy-mode-vi V { send -X select-line }",
                    598:                "bind -Tcopy-mode-vi W { send -X next-space }",
                    599:                "bind -Tcopy-mode-vi X { send -X set-mark }",
                    600:                "bind -Tcopy-mode-vi ^ { send -X back-to-indentation }",
                    601:                "bind -Tcopy-mode-vi b { send -X previous-word }",
                    602:                "bind -Tcopy-mode-vi e { send -X next-word-end }",
1.141     nicm      603:                "bind -Tcopy-mode-vi f { command-prompt -1p'(jump forward)' { send -X jump-forward '%%' } }",
1.138     nicm      604:                "bind -Tcopy-mode-vi g { send -X history-top }",
                    605:                "bind -Tcopy-mode-vi h { send -X cursor-left }",
                    606:                "bind -Tcopy-mode-vi j { send -X cursor-down }",
                    607:                "bind -Tcopy-mode-vi k { send -X cursor-up }",
                    608:                "bind -Tcopy-mode-vi l { send -X cursor-right }",
                    609:                "bind -Tcopy-mode-vi n { send -X search-again }",
                    610:                "bind -Tcopy-mode-vi o { send -X other-end }",
                    611:                "bind -Tcopy-mode-vi q { send -X cancel }",
                    612:                "bind -Tcopy-mode-vi r { send -X refresh-from-pane }",
1.141     nicm      613:                "bind -Tcopy-mode-vi t { command-prompt -1p'(jump to forward)' { send -X jump-to-forward '%%' } }",
1.138     nicm      614:                "bind -Tcopy-mode-vi v { send -X rectangle-toggle }",
                    615:                "bind -Tcopy-mode-vi w { send -X next-word }",
                    616:                "bind -Tcopy-mode-vi '{' { send -X previous-paragraph }",
                    617:                "bind -Tcopy-mode-vi '}' { send -X next-paragraph }",
                    618:                "bind -Tcopy-mode-vi % { send -X next-matching-bracket }",
1.144   ! nicm      619:                "bind -Tcopy-mode-vi Home { send -X start-of-line }",
        !           620:                "bind -Tcopy-mode-vi End { send -X end-of-line }",
1.138     nicm      621:                "bind -Tcopy-mode-vi MouseDown1Pane { select-pane }",
                    622:                "bind -Tcopy-mode-vi MouseDrag1Pane { select-pane; send -X begin-selection }",
                    623:                "bind -Tcopy-mode-vi MouseDragEnd1Pane { send -X copy-pipe-and-cancel }",
                    624:                "bind -Tcopy-mode-vi WheelUpPane { select-pane; send -N5 -X scroll-up }",
                    625:                "bind -Tcopy-mode-vi WheelDownPane { select-pane; send -N5 -X scroll-down }",
                    626:                "bind -Tcopy-mode-vi DoubleClick1Pane { select-pane; send -X select-word; run -d0.3; send -X copy-pipe-and-cancel }",
                    627:                "bind -Tcopy-mode-vi TripleClick1Pane { select-pane; send -X select-line; run -d0.3; send -X copy-pipe-and-cancel }",
                    628:                "bind -Tcopy-mode-vi BSpace { send -X cursor-left }",
                    629:                "bind -Tcopy-mode-vi NPage { send -X page-down }",
                    630:                "bind -Tcopy-mode-vi PPage { send -X page-up }",
                    631:                "bind -Tcopy-mode-vi Up { send -X cursor-up }",
                    632:                "bind -Tcopy-mode-vi Down { send -X cursor-down }",
                    633:                "bind -Tcopy-mode-vi Left { send -X cursor-left }",
                    634:                "bind -Tcopy-mode-vi Right { send -X cursor-right }",
                    635:                "bind -Tcopy-mode-vi M-x { send -X jump-to-mark }",
                    636:                "bind -Tcopy-mode-vi C-Up { send -X scroll-up }",
                    637:                "bind -Tcopy-mode-vi C-Down { send -X scroll-down }",
1.1       nicm      638:        };
1.93      nicm      639:        u_int                    i;
                    640:        struct cmd_parse_result *pr;
1.1       nicm      641:
1.42      nicm      642:        for (i = 0; i < nitems(defaults); i++) {
1.93      nicm      643:                pr = cmd_parse_from_string(defaults[i], NULL);
1.140     nicm      644:                if (pr->status != CMD_PARSE_SUCCESS) {
                    645:                        log_debug("%s", pr->error);
1.72      nicm      646:                        fatalx("bad default key: %s", defaults[i]);
1.140     nicm      647:                }
1.122     nicm      648:                cmdq_append(NULL, cmdq_get_command(pr->cmdlist, NULL));
1.93      nicm      649:                cmd_list_free(pr->cmdlist);
1.1       nicm      650:        }
1.125     nicm      651:        cmdq_append(NULL, cmdq_get_callback(key_bindings_init_done, NULL));
1.61      nicm      652: }
                    653:
                    654: static enum cmd_retval
1.62      nicm      655: key_bindings_read_only(struct cmdq_item *item, __unused void *data)
1.61      nicm      656: {
1.62      nicm      657:        cmdq_error(item, "client is read-only");
1.61      nicm      658:        return (CMD_RETURN_ERROR);
1.1       nicm      659: }
                    660:
1.90      nicm      661: struct cmdq_item *
1.78      nicm      662: key_bindings_dispatch(struct key_binding *bd, struct cmdq_item *item,
1.121     nicm      663:     struct client *c, struct key_event *event, struct cmd_find_state *fs)
1.1       nicm      664: {
1.78      nicm      665:        struct cmdq_item        *new_item;
1.122     nicm      666:        struct cmdq_state       *new_state;
1.121     nicm      667:        int                      readonly, flags = 0;
1.1       nicm      668:
1.102     nicm      669:        if (c == NULL || (~c->flags & CLIENT_READONLY))
                    670:                readonly = 1;
1.118     nicm      671:        else
                    672:                readonly = cmd_list_all_have(bd->cmdlist, CMD_READONLY);
1.102     nicm      673:        if (!readonly)
1.78      nicm      674:                new_item = cmdq_get_callback(key_bindings_read_only, NULL);
1.73      nicm      675:        else {
1.75      nicm      676:                if (bd->flags & KEY_BINDING_REPEAT)
1.121     nicm      677:                        flags |= CMDQ_STATE_REPEAT;
1.122     nicm      678:                new_state = cmdq_new_state(fs, event, flags);
                    679:                new_item = cmdq_get_command(bd->cmdlist, new_state);
                    680:                cmdq_free_state(new_state);
1.73      nicm      681:        }
1.78      nicm      682:        if (item != NULL)
1.105     nicm      683:                new_item = cmdq_insert_after(item, new_item);
1.78      nicm      684:        else
1.105     nicm      685:                new_item = cmdq_append(c, new_item);
1.90      nicm      686:        return (new_item);
1.1       nicm      687: }