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

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