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

1.40    ! nicm        1: /* $OpenBSD: cmd-list-keys.c,v 1.39 2016/10/14 22:14:22 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.34      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:
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.40    ! nicm       30: static enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
1.24      nicm       31:
1.40    ! nicm       32: static enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmdq_item *);
        !            33: static enum cmd_retval cmd_list_keys_commands(struct cmd *,
        !            34:                            struct cmdq_item *);
1.6       nicm       35:
1.1       nicm       36: const struct cmd_entry cmd_list_keys_entry = {
1.32      nicm       37:        .name = "list-keys",
                     38:        .alias = "lsk",
                     39:
                     40:        .args = { "t:T:", 0, 0 },
                     41:        .usage = "[-t mode-table] [-T key-table]",
                     42:
1.39      nicm       43:        .flags = CMD_STARTSERVER|CMD_AFTERHOOK,
1.32      nicm       44:        .exec = cmd_list_keys_exec
1.1       nicm       45: };
                     46:
1.24      nicm       47: const struct cmd_entry cmd_list_commands_entry = {
1.32      nicm       48:        .name = "list-commands",
                     49:        .alias = "lscm",
                     50:
1.35      nicm       51:        .args = { "F:", 0, 0 },
                     52:        .usage = "[-F format]",
1.32      nicm       53:
1.39      nicm       54:        .flags = CMD_STARTSERVER|CMD_AFTERHOOK,
1.32      nicm       55:        .exec = cmd_list_keys_exec
1.24      nicm       56: };
                     57:
1.35      nicm       58: static enum cmd_retval
1.40    ! nicm       59: cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       60: {
1.13      nicm       61:        struct args             *args = self->args;
1.26      nicm       62:        struct key_table        *table;
1.1       nicm       63:        struct key_binding      *bd;
1.26      nicm       64:        const char              *key, *tablename, *r;
1.29      nicm       65:        char                    *cp, tmp[BUFSIZ];
1.26      nicm       66:        int                      repeat, width, tablewidth, keywidth;
1.3       nicm       67:
1.24      nicm       68:        if (self->entry == &cmd_list_commands_entry)
1.40    ! nicm       69:                return (cmd_list_keys_commands(self, item));
1.24      nicm       70:
1.13      nicm       71:        if (args_has(args, 't'))
1.40    ! nicm       72:                return (cmd_list_keys_table(self, item));
1.6       nicm       73:
1.26      nicm       74:        tablename = args_get(args, 'T');
                     75:        if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
1.40    ! nicm       76:                cmdq_error(item, "table %s doesn't exist", tablename);
1.26      nicm       77:                return (CMD_RETURN_ERROR);
                     78:        }
1.15      nicm       79:
1.26      nicm       80:        repeat = 0;
                     81:        tablewidth = keywidth = 0;
                     82:        RB_FOREACH(table, key_tables, &key_tables) {
                     83:                if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.3       nicm       84:                        continue;
1.26      nicm       85:                RB_FOREACH(bd, key_bindings, &table->key_bindings) {
                     86:                        key = key_string_lookup_key(bd->key);
1.3       nicm       87:
1.14      nicm       88:                        if (bd->can_repeat)
1.26      nicm       89:                                repeat = 1;
                     90:
1.29      nicm       91:                        width = utf8_cstrwidth(table->name);
1.26      nicm       92:                        if (width > tablewidth)
1.28      nicm       93:                                tablewidth = width;
                     94:                        width = utf8_cstrwidth(key);
1.26      nicm       95:                        if (width > keywidth)
                     96:                                keywidth = width;
                     97:                }
1.3       nicm       98:        }
                     99:
1.26      nicm      100:        RB_FOREACH(table, key_tables, &key_tables) {
                    101:                if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.1       nicm      102:                        continue;
1.26      nicm      103:                RB_FOREACH(bd, key_bindings, &table->key_bindings) {
                    104:                        key = key_string_lookup_key(bd->key);
                    105:
                    106:                        if (!repeat)
                    107:                                r = "";
                    108:                        else if (bd->can_repeat)
                    109:                                r = "-r ";
1.14      nicm      110:                        else
1.26      nicm      111:                                r = "   ";
1.29      nicm      112:                        xsnprintf(tmp, sizeof tmp, "%s-T ", r);
                    113:
                    114:                        cp = utf8_padcstr(table->name, tablewidth);
                    115:                        strlcat(tmp, cp, sizeof tmp);
                    116:                        strlcat(tmp, " ", sizeof tmp);
                    117:                        free(cp);
                    118:
                    119:                        cp = utf8_padcstr(key, keywidth);
                    120:                        strlcat(tmp, cp, sizeof tmp);
                    121:                        strlcat(tmp, " ", sizeof tmp);
                    122:                        free(cp);
                    123:
1.31      nicm      124:                        cp = cmd_list_print(bd->cmdlist);
                    125:                        strlcat(tmp, cp, sizeof tmp);
                    126:                        free(cp);
1.1       nicm      127:
1.40    ! nicm      128:                        cmdq_print(item, "bind-key %s", tmp);
1.26      nicm      129:                }
1.6       nicm      130:        }
                    131:
1.18      nicm      132:        return (CMD_RETURN_NORMAL);
1.6       nicm      133: }
                    134:
1.35      nicm      135: static enum cmd_retval
1.40    ! nicm      136: cmd_list_keys_table(struct cmd *self, struct cmdq_item *item)
1.6       nicm      137: {
1.13      nicm      138:        struct args                     *args = self->args;
1.38      nicm      139:        const char                      *tablename, *cmdstr;
1.6       nicm      140:        const struct mode_key_table     *mtab;
                    141:        struct mode_key_binding         *mbind;
1.38      nicm      142:        int                              width, keywidth;
1.6       nicm      143:
1.13      nicm      144:        tablename = args_get(args, 't');
                    145:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.40    ! nicm      146:                cmdq_error(item, "unknown key table: %s", tablename);
1.18      nicm      147:                return (CMD_RETURN_ERROR);
1.6       nicm      148:        }
                    149:
1.37      nicm      150:        keywidth = 0;
1.17      nicm      151:        RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.38      nicm      152:                width = strlen(key_string_lookup_key(mbind->key));
1.36      nicm      153:                if (width > keywidth)
                    154:                        keywidth = width;
1.6       nicm      155:        }
                    156:
1.17      nicm      157:        RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.6       nicm      158:                cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
1.14      nicm      159:                if (cmdstr != NULL) {
1.40    ! nicm      160:                        cmdq_print(item, "bind-key -t %s %*s %s",
1.38      nicm      161:                            mtab->name, (int)keywidth,
                    162:                            key_string_lookup_key(mbind->key), cmdstr);
1.14      nicm      163:                }
1.24      nicm      164:        }
                    165:
                    166:        return (CMD_RETURN_NORMAL);
                    167: }
                    168:
1.35      nicm      169: static enum cmd_retval
1.40    ! nicm      170: cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
1.24      nicm      171: {
1.35      nicm      172:        struct args             *args = self->args;
1.24      nicm      173:        const struct cmd_entry  **entryp;
1.25      nicm      174:        const struct cmd_entry   *entry;
1.35      nicm      175:        struct format_tree       *ft;
1.40    ! nicm      176:        const char               *template, *s;
1.35      nicm      177:        char                     *line;
                    178:
                    179:        if ((template = args_get(args, 'F')) == NULL) {
                    180:                template = "#{command_list_name}"
                    181:                    "#{?command_list_alias, (#{command_list_alias}),} "
                    182:                    "#{command_list_usage}";
                    183:        }
                    184:
1.40    ! nicm      185:        ft = format_create(item, 0);
1.35      nicm      186:        format_defaults(ft, NULL, NULL, NULL, NULL);
1.24      nicm      187:
                    188:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    189:                entry = *entryp;
1.35      nicm      190:
                    191:                format_add(ft, "command_list_name", "%s", entry->name);
1.40    ! nicm      192:                if (entry->alias != NULL)
        !           193:                        s = entry->alias;
        !           194:                else
        !           195:                        s = "";
        !           196:                format_add(ft, "command_list_alias", "%s", s);
        !           197:                if (entry->usage != NULL)
        !           198:                        s = entry->usage;
        !           199:                else
        !           200:                        s = "";
        !           201:                format_add(ft, "command_list_usage", "%s", s);
1.35      nicm      202:
                    203:                line = format_expand(ft, template);
                    204:                if (*line != '\0')
1.40    ! nicm      205:                        cmdq_print(item, "%s", line);
1.35      nicm      206:                free(line);
1.1       nicm      207:        }
                    208:
1.35      nicm      209:        format_free(ft);
1.18      nicm      210:        return (CMD_RETURN_NORMAL);
1.1       nicm      211: }