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

1.47    ! nicm        1: /* $OpenBSD: cmd-list-keys.c,v 1.46 2019/05/23 14:03:44 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.47    ! nicm       64:        char                    *key, *cp, tmp[8192];
1.26      nicm       65:        int                      repeat, width, tablewidth, keywidth;
1.3       nicm       66:
1.24      nicm       67:        if (self->entry == &cmd_list_commands_entry)
1.40      nicm       68:                return (cmd_list_keys_commands(self, item));
1.24      nicm       69:
1.26      nicm       70:        tablename = args_get(args, 'T');
                     71:        if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
1.40      nicm       72:                cmdq_error(item, "table %s doesn't exist", tablename);
1.26      nicm       73:                return (CMD_RETURN_ERROR);
                     74:        }
1.15      nicm       75:
1.26      nicm       76:        repeat = 0;
                     77:        tablewidth = keywidth = 0;
1.45      nicm       78:        table = key_bindings_first_table ();
                     79:        while (table != NULL) {
                     80:                if (tablename != NULL && strcmp(table->name, tablename) != 0) {
                     81:                        table = key_bindings_next_table(table);
1.3       nicm       82:                        continue;
1.45      nicm       83:                }
                     84:                bd = key_bindings_first(table);
                     85:                while (bd != NULL) {
1.46      nicm       86:                        key = args_escape(key_string_lookup_key(bd->key));
1.3       nicm       87:
1.43      nicm       88:                        if (bd->flags & KEY_BINDING_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;
1.45      nicm       97:
1.46      nicm       98:                        free(key);
1.45      nicm       99:                        bd = key_bindings_next(table, bd);
1.26      nicm      100:                }
1.45      nicm      101:                table = key_bindings_next_table(table);
1.3       nicm      102:        }
                    103:
1.45      nicm      104:        table = key_bindings_first_table ();
                    105:        while (table != NULL) {
                    106:                if (tablename != NULL && strcmp(table->name, tablename) != 0) {
                    107:                        table = key_bindings_next_table(table);
1.1       nicm      108:                        continue;
1.45      nicm      109:                }
                    110:                bd = key_bindings_first(table);
                    111:                while (bd != NULL) {
1.46      nicm      112:                        key = args_escape(key_string_lookup_key(bd->key));
1.26      nicm      113:
                    114:                        if (!repeat)
                    115:                                r = "";
1.43      nicm      116:                        else if (bd->flags & KEY_BINDING_REPEAT)
1.26      nicm      117:                                r = "-r ";
1.14      nicm      118:                        else
1.26      nicm      119:                                r = "   ";
1.29      nicm      120:                        xsnprintf(tmp, sizeof tmp, "%s-T ", r);
                    121:
                    122:                        cp = utf8_padcstr(table->name, tablewidth);
                    123:                        strlcat(tmp, cp, sizeof tmp);
                    124:                        strlcat(tmp, " ", sizeof tmp);
                    125:                        free(cp);
                    126:
                    127:                        cp = utf8_padcstr(key, keywidth);
                    128:                        strlcat(tmp, cp, sizeof tmp);
                    129:                        strlcat(tmp, " ", sizeof tmp);
                    130:                        free(cp);
                    131:
1.46      nicm      132:                        cp = cmd_list_print(bd->cmdlist, 1);
1.31      nicm      133:                        strlcat(tmp, cp, sizeof tmp);
                    134:                        free(cp);
1.1       nicm      135:
1.40      nicm      136:                        cmdq_print(item, "bind-key %s", tmp);
1.46      nicm      137:
                    138:                        free(key);
1.45      nicm      139:                        bd = key_bindings_next(table, bd);
1.14      nicm      140:                }
1.45      nicm      141:                table = key_bindings_next_table(table);
1.24      nicm      142:        }
                    143:
                    144:        return (CMD_RETURN_NORMAL);
                    145: }
                    146:
1.35      nicm      147: static enum cmd_retval
1.40      nicm      148: cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
1.24      nicm      149: {
1.35      nicm      150:        struct args             *args = self->args;
1.24      nicm      151:        const struct cmd_entry  **entryp;
1.25      nicm      152:        const struct cmd_entry   *entry;
1.35      nicm      153:        struct format_tree       *ft;
1.40      nicm      154:        const char               *template, *s;
1.35      nicm      155:        char                     *line;
                    156:
                    157:        if ((template = args_get(args, 'F')) == NULL) {
                    158:                template = "#{command_list_name}"
                    159:                    "#{?command_list_alias, (#{command_list_alias}),} "
                    160:                    "#{command_list_usage}";
                    161:        }
                    162:
1.44      nicm      163:        ft = format_create(item->client, item, FORMAT_NONE, 0);
1.35      nicm      164:        format_defaults(ft, NULL, NULL, NULL, NULL);
1.24      nicm      165:
                    166:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    167:                entry = *entryp;
1.35      nicm      168:
                    169:                format_add(ft, "command_list_name", "%s", entry->name);
1.40      nicm      170:                if (entry->alias != NULL)
                    171:                        s = entry->alias;
                    172:                else
                    173:                        s = "";
                    174:                format_add(ft, "command_list_alias", "%s", s);
                    175:                if (entry->usage != NULL)
                    176:                        s = entry->usage;
                    177:                else
                    178:                        s = "";
                    179:                format_add(ft, "command_list_usage", "%s", s);
1.35      nicm      180:
                    181:                line = format_expand(ft, template);
                    182:                if (*line != '\0')
1.40      nicm      183:                        cmdq_print(item, "%s", line);
1.35      nicm      184:                free(line);
1.1       nicm      185:        }
                    186:
1.35      nicm      187:        format_free(ft);
1.18      nicm      188:        return (CMD_RETURN_NORMAL);
1.1       nicm      189: }