[BACK]Return to cmd-list-keys.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-list-keys.c, Revision 1.30

1.30    ! nicm        1: /* $OpenBSD: cmd-list-keys.c,v 1.29 2015/11/12 12:43:36 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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.29      nicm       21: #include <stdlib.h>
1.3       nicm       22: #include <string.h>
                     23:
1.1       nicm       24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * List key bindings.
                     28:  */
                     29:
1.21      nicm       30: enum cmd_retval         cmd_list_keys_exec(struct cmd *, struct cmd_q *);
1.24      nicm       31:
1.21      nicm       32: enum cmd_retval         cmd_list_keys_table(struct cmd *, struct cmd_q *);
1.30    ! nicm       33: enum cmd_retval         cmd_list_keys_commands(struct cmd_q *);
1.6       nicm       34:
1.1       nicm       35: const struct cmd_entry cmd_list_keys_entry = {
                     36:        "list-keys", "lsk",
1.26      nicm       37:        "t:T:", 0, 0,
                     38:        "[-t mode-table] [-T key-table]",
1.13      nicm       39:        0,
                     40:        cmd_list_keys_exec
1.1       nicm       41: };
                     42:
1.24      nicm       43: const struct cmd_entry cmd_list_commands_entry = {
                     44:        "list-commands", "lscm",
                     45:        "", 0, 0,
                     46:        "",
                     47:        0,
                     48:        cmd_list_keys_exec
                     49: };
                     50:
1.18      nicm       51: enum cmd_retval
1.21      nicm       52: cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       53: {
1.13      nicm       54:        struct args             *args = self->args;
1.26      nicm       55:        struct key_table        *table;
1.1       nicm       56:        struct key_binding      *bd;
1.26      nicm       57:        const char              *key, *tablename, *r;
1.29      nicm       58:        char                    *cp, tmp[BUFSIZ];
1.8       nicm       59:        size_t                   used;
1.26      nicm       60:        int                      repeat, width, tablewidth, keywidth;
1.3       nicm       61:
1.24      nicm       62:        if (self->entry == &cmd_list_commands_entry)
1.30    ! nicm       63:                return (cmd_list_keys_commands(cmdq));
1.24      nicm       64:
1.13      nicm       65:        if (args_has(args, 't'))
1.21      nicm       66:                return (cmd_list_keys_table(self, cmdq));
1.6       nicm       67:
1.26      nicm       68:        tablename = args_get(args, 'T');
                     69:        if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
                     70:                cmdq_error(cmdq, "table %s doesn't exist", tablename);
                     71:                return (CMD_RETURN_ERROR);
                     72:        }
1.15      nicm       73:
1.26      nicm       74:        repeat = 0;
                     75:        tablewidth = keywidth = 0;
                     76:        RB_FOREACH(table, key_tables, &key_tables) {
                     77:                if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.3       nicm       78:                        continue;
1.26      nicm       79:                RB_FOREACH(bd, key_bindings, &table->key_bindings) {
                     80:                        key = key_string_lookup_key(bd->key);
1.3       nicm       81:
1.14      nicm       82:                        if (bd->can_repeat)
1.26      nicm       83:                                repeat = 1;
                     84:
1.29      nicm       85:                        width = utf8_cstrwidth(table->name);
1.26      nicm       86:                        if (width > tablewidth)
1.28      nicm       87:                                tablewidth = width;
                     88:                        width = utf8_cstrwidth(key);
1.26      nicm       89:                        if (width > keywidth)
                     90:                                keywidth = width;
                     91:                }
1.3       nicm       92:        }
                     93:
1.26      nicm       94:        RB_FOREACH(table, key_tables, &key_tables) {
                     95:                if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.1       nicm       96:                        continue;
1.26      nicm       97:                RB_FOREACH(bd, key_bindings, &table->key_bindings) {
                     98:                        key = key_string_lookup_key(bd->key);
                     99:
                    100:                        if (!repeat)
                    101:                                r = "";
                    102:                        else if (bd->can_repeat)
                    103:                                r = "-r ";
1.14      nicm      104:                        else
1.26      nicm      105:                                r = "   ";
1.29      nicm      106:                        xsnprintf(tmp, sizeof tmp, "%s-T ", r);
                    107:
                    108:                        cp = utf8_padcstr(table->name, tablewidth);
                    109:                        strlcat(tmp, cp, sizeof tmp);
                    110:                        strlcat(tmp, " ", sizeof tmp);
                    111:                        free(cp);
                    112:
                    113:                        cp = utf8_padcstr(key, keywidth);
                    114:                        strlcat(tmp, cp, sizeof tmp);
                    115:                        strlcat(tmp, " ", sizeof tmp);
                    116:                        free(cp);
                    117:
                    118:                        used = strlen(tmp);
                    119:                        if (used < (sizeof tmp) - 1) {
1.26      nicm      120:                                cmd_list_print(bd->cmdlist, tmp + used,
                    121:                                    (sizeof tmp) - used);
                    122:                        }
1.1       nicm      123:
1.26      nicm      124:                        cmdq_print(cmdq, "bind-key %s", tmp);
                    125:                }
1.6       nicm      126:        }
                    127:
1.18      nicm      128:        return (CMD_RETURN_NORMAL);
1.6       nicm      129: }
                    130:
1.19      nicm      131: enum cmd_retval
1.21      nicm      132: cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq)
1.6       nicm      133: {
1.13      nicm      134:        struct args                     *args = self->args;
                    135:        const char                      *tablename;
1.6       nicm      136:        const struct mode_key_table     *mtab;
                    137:        struct mode_key_binding         *mbind;
                    138:        const char                      *key, *cmdstr, *mode;
1.14      nicm      139:        int                              width, keywidth, any_mode;
1.6       nicm      140:
1.13      nicm      141:        tablename = args_get(args, 't');
                    142:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.21      nicm      143:                cmdq_error(cmdq, "unknown key table: %s", tablename);
1.18      nicm      144:                return (CMD_RETURN_ERROR);
1.6       nicm      145:        }
                    146:
                    147:        width = 0;
1.14      nicm      148:        any_mode = 0;
1.17      nicm      149:        RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.6       nicm      150:                key = key_string_lookup_key(mbind->key);
                    151:
1.14      nicm      152:                if (mbind->mode != 0)
                    153:                        any_mode = 1;
                    154:
                    155:                keywidth = strlen(key);
1.6       nicm      156:                if (keywidth > width)
                    157:                        width = keywidth;
                    158:        }
                    159:
1.17      nicm      160:        RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.6       nicm      161:                key = key_string_lookup_key(mbind->key);
                    162:
                    163:                mode = "";
                    164:                if (mbind->mode != 0)
1.14      nicm      165:                        mode = "c";
1.6       nicm      166:                cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
1.14      nicm      167:                if (cmdstr != NULL) {
1.21      nicm      168:                        cmdq_print(cmdq, "bind-key -%st %s%s %*s %s%s%s%s",
1.14      nicm      169:                            mode, any_mode && *mode == '\0' ? " " : "",
1.20      nicm      170:                            mtab->name, (int) width, key, cmdstr,
                    171:                            mbind->arg != NULL ? " \"" : "",
                    172:                            mbind->arg != NULL ? mbind->arg : "",
                    173:                            mbind->arg != NULL ? "\"": "");
1.14      nicm      174:                }
1.24      nicm      175:        }
                    176:
                    177:        return (CMD_RETURN_NORMAL);
                    178: }
                    179:
                    180: enum cmd_retval
1.30    ! nicm      181: cmd_list_keys_commands(struct cmd_q *cmdq)
1.24      nicm      182: {
                    183:        const struct cmd_entry  **entryp;
1.25      nicm      184:        const struct cmd_entry   *entry;
1.24      nicm      185:
                    186:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    187:                entry = *entryp;
                    188:                if (entry->alias == NULL) {
                    189:                        cmdq_print(cmdq, "%s %s", entry->name, entry->usage);
                    190:                        continue;
                    191:                }
                    192:                cmdq_print(cmdq, "%s (%s) %s", entry->name, entry->alias,
                    193:                    entry->usage);
1.1       nicm      194:        }
                    195:
1.18      nicm      196:        return (CMD_RETURN_NORMAL);
1.1       nicm      197: }