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

1.61    ! nicm        1: /* $OpenBSD: cmd-list-keys.c,v 1.60 2020/07/06 07:27:39 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.51      nicm       39:        .args = { "1aNP:T:", 0, 1 },
                     40:        .usage = "[-1aN] [-P prefix-string] [-T key-table] [key]",
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.52      nicm       50:        .args = { "F:", 0, 1 },
                     51:        .usage = "[-F format] [command]",
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.50      nicm       57: static u_int
                     58: cmd_list_keys_get_width(const char *tablename, key_code only)
                     59: {
                     60:        struct key_table        *table;
                     61:        struct key_binding      *bd;
                     62:        u_int                    width, keywidth = 0;
                     63:
                     64:        table = key_bindings_get_table(tablename, 0);
                     65:        if (table == NULL)
                     66:                return (0);
                     67:        bd = key_bindings_first(table);
                     68:        while (bd != NULL) {
                     69:                if ((only != KEYC_UNKNOWN && bd->key != only) ||
                     70:                    KEYC_IS_MOUSE(bd->key) ||
1.58      nicm       71:                    bd->note == NULL ||
                     72:                    *bd->note == '\0') {
1.50      nicm       73:                        bd = key_bindings_next(table, bd);
                     74:                        continue;
                     75:                }
1.59      nicm       76:                width = utf8_cstrwidth(key_string_lookup_key(bd->key, 0));
1.50      nicm       77:                if (width > keywidth)
                     78:                        keywidth = width;
                     79:
                     80:                bd = key_bindings_next(table, bd);
                     81:        }
                     82:        return (keywidth);
                     83: }
                     84:
                     85: static int
                     86: cmd_list_keys_print_notes(struct cmdq_item *item, struct args *args,
                     87:     const char *tablename, u_int keywidth, key_code only, const char *prefix)
                     88: {
1.56      nicm       89:        struct client           *tc = cmdq_get_target_client(item);
1.50      nicm       90:        struct key_table        *table;
                     91:        struct key_binding      *bd;
                     92:        const char              *key;
1.51      nicm       93:        char                    *tmp, *note;
1.50      nicm       94:        int                      found = 0;
                     95:
                     96:        table = key_bindings_get_table(tablename, 0);
                     97:        if (table == NULL)
                     98:                return (0);
                     99:        bd = key_bindings_first(table);
                    100:        while (bd != NULL) {
                    101:                if ((only != KEYC_UNKNOWN && bd->key != only) ||
                    102:                    KEYC_IS_MOUSE(bd->key) ||
1.58      nicm      103:                    ((bd->note == NULL || *bd->note == '\0') &&
                    104:                    !args_has(args, 'a'))) {
1.50      nicm      105:                        bd = key_bindings_next(table, bd);
                    106:                        continue;
                    107:                }
                    108:                found = 1;
1.59      nicm      109:                key = key_string_lookup_key(bd->key, 0);
1.50      nicm      110:
1.58      nicm      111:                if (bd->note == NULL || *bd->note == '\0')
1.51      nicm      112:                        note = cmd_list_print(bd->cmdlist, 1);
                    113:                else
                    114:                        note = xstrdup(bd->note);
1.50      nicm      115:                tmp = utf8_padcstr(key, keywidth + 1);
1.56      nicm      116:                if (args_has(args, '1') && tc != NULL)
1.61    ! nicm      117:                        status_message_set(tc, -1, 1, "%s%s%s", prefix, tmp, note);
1.50      nicm      118:                else
1.51      nicm      119:                        cmdq_print(item, "%s%s%s", prefix, tmp, note);
1.50      nicm      120:                free(tmp);
1.51      nicm      121:                free(note);
1.50      nicm      122:
                    123:                if (args_has(args, '1'))
                    124:                        break;
                    125:                bd = key_bindings_next(table, bd);
                    126:        }
                    127:        return (found);
                    128: }
                    129:
                    130: static char *
                    131: cmd_list_keys_get_prefix(struct args *args, key_code *prefix)
                    132: {
                    133:        char    *s;
                    134:
                    135:        *prefix = options_get_number(global_s_options, "prefix");
                    136:        if (!args_has(args, 'P')) {
                    137:                if (*prefix != KEYC_NONE)
1.59      nicm      138:                        xasprintf(&s, "%s ", key_string_lookup_key(*prefix, 0));
1.50      nicm      139:                else
                    140:                        s = xstrdup("");
                    141:        } else
                    142:                s = xstrdup(args_get(args, 'P'));
                    143:        return (s);
                    144: }
                    145:
1.35      nicm      146: static enum cmd_retval
1.40      nicm      147: cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm      148: {
1.54      nicm      149:        struct args             *args = cmd_get_args(self);
1.26      nicm      150:        struct key_table        *table;
1.1       nicm      151:        struct key_binding      *bd;
1.46      nicm      152:        const char              *tablename, *r;
1.50      nicm      153:        char                    *key, *cp, *tmp, *start, *empty;
                    154:        key_code                 prefix, only = KEYC_UNKNOWN;
                    155:        int                      repeat, width, tablewidth, keywidth, found = 0;
1.48      nicm      156:        size_t                   tmpsize, tmpused, cplen;
1.3       nicm      157:
1.54      nicm      158:        if (cmd_get_entry(self) == &cmd_list_commands_entry)
1.40      nicm      159:                return (cmd_list_keys_commands(self, item));
1.24      nicm      160:
1.50      nicm      161:        if (args->argc != 0) {
                    162:                only = key_string_lookup_string(args->argv[0]);
                    163:                if (only == KEYC_UNKNOWN) {
                    164:                        cmdq_error(item, "invalid key: %s", args->argv[0]);
                    165:                        return (CMD_RETURN_ERROR);
                    166:                }
1.60      nicm      167:                only &= KEYC_MASK_KEY;
1.50      nicm      168:        }
                    169:
1.26      nicm      170:        tablename = args_get(args, 'T');
                    171:        if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
1.40      nicm      172:                cmdq_error(item, "table %s doesn't exist", tablename);
1.26      nicm      173:                return (CMD_RETURN_ERROR);
                    174:        }
1.15      nicm      175:
1.50      nicm      176:        if (args_has(args, 'N')) {
                    177:                if (tablename == NULL) {
                    178:                        start = cmd_list_keys_get_prefix(args, &prefix);
                    179:                        keywidth = cmd_list_keys_get_width("root", only);
                    180:                        if (prefix != KEYC_NONE) {
                    181:                                width = cmd_list_keys_get_width("prefix", only);
                    182:                                if (width == 0)
                    183:                                        prefix = KEYC_NONE;
                    184:                                else if (width > keywidth)
                    185:                                        keywidth = width;
                    186:                        }
                    187:                        empty = utf8_padcstr("", utf8_cstrwidth(start));
                    188:
                    189:                        found = cmd_list_keys_print_notes(item, args, "root",
                    190:                            keywidth, only, empty);
                    191:                        if (prefix != KEYC_NONE) {
                    192:                                if (cmd_list_keys_print_notes(item, args,
                    193:                                    "prefix", keywidth, only, start))
                    194:                                        found = 1;
                    195:                        }
                    196:                        free(empty);
                    197:                } else {
                    198:                        if (args_has(args, 'P'))
                    199:                                start = xstrdup(args_get(args, 'P'));
                    200:                        else
                    201:                                start = xstrdup("");
                    202:                        keywidth = cmd_list_keys_get_width(tablename, only);
                    203:                        found = cmd_list_keys_print_notes(item, args, tablename,
                    204:                            keywidth, only, start);
                    205:
                    206:                }
                    207:                free(start);
                    208:                goto out;
                    209:        }
                    210:
1.26      nicm      211:        repeat = 0;
                    212:        tablewidth = keywidth = 0;
1.45      nicm      213:        table = key_bindings_first_table ();
                    214:        while (table != NULL) {
                    215:                if (tablename != NULL && strcmp(table->name, tablename) != 0) {
                    216:                        table = key_bindings_next_table(table);
1.3       nicm      217:                        continue;
1.45      nicm      218:                }
                    219:                bd = key_bindings_first(table);
                    220:                while (bd != NULL) {
1.50      nicm      221:                        if (only != KEYC_UNKNOWN && bd->key != only) {
                    222:                                bd = key_bindings_next(table, bd);
                    223:                                continue;
                    224:                        }
1.59      nicm      225:                        key = args_escape(key_string_lookup_key(bd->key, 0));
1.3       nicm      226:
1.43      nicm      227:                        if (bd->flags & KEY_BINDING_REPEAT)
1.26      nicm      228:                                repeat = 1;
                    229:
1.29      nicm      230:                        width = utf8_cstrwidth(table->name);
1.26      nicm      231:                        if (width > tablewidth)
1.28      nicm      232:                                tablewidth = width;
                    233:                        width = utf8_cstrwidth(key);
1.26      nicm      234:                        if (width > keywidth)
                    235:                                keywidth = width;
1.45      nicm      236:
1.46      nicm      237:                        free(key);
1.45      nicm      238:                        bd = key_bindings_next(table, bd);
1.26      nicm      239:                }
1.45      nicm      240:                table = key_bindings_next_table(table);
1.3       nicm      241:        }
                    242:
1.48      nicm      243:        tmpsize = 256;
                    244:        tmp = xmalloc(tmpsize);
                    245:
1.45      nicm      246:        table = key_bindings_first_table ();
                    247:        while (table != NULL) {
                    248:                if (tablename != NULL && strcmp(table->name, tablename) != 0) {
                    249:                        table = key_bindings_next_table(table);
1.1       nicm      250:                        continue;
1.45      nicm      251:                }
                    252:                bd = key_bindings_first(table);
                    253:                while (bd != NULL) {
1.50      nicm      254:                        if (only != KEYC_UNKNOWN && bd->key != only) {
                    255:                                bd = key_bindings_next(table, bd);
                    256:                                continue;
                    257:                        }
                    258:                        found = 1;
1.59      nicm      259:                        key = args_escape(key_string_lookup_key(bd->key, 0));
1.26      nicm      260:
                    261:                        if (!repeat)
                    262:                                r = "";
1.43      nicm      263:                        else if (bd->flags & KEY_BINDING_REPEAT)
1.26      nicm      264:                                r = "-r ";
1.14      nicm      265:                        else
1.26      nicm      266:                                r = "   ";
1.48      nicm      267:                        tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
1.29      nicm      268:
                    269:                        cp = utf8_padcstr(table->name, tablewidth);
1.48      nicm      270:                        cplen = strlen(cp) + 1;
1.49      nicm      271:                        while (tmpused + cplen + 1 >= tmpsize) {
1.48      nicm      272:                                tmpsize *= 2;
                    273:                                tmp = xrealloc(tmp, tmpsize);
                    274:                        }
1.53      nicm      275:                        strlcat(tmp, cp, tmpsize);
1.48      nicm      276:                        tmpused = strlcat(tmp, " ", tmpsize);
1.29      nicm      277:                        free(cp);
                    278:
                    279:                        cp = utf8_padcstr(key, keywidth);
1.48      nicm      280:                        cplen = strlen(cp) + 1;
                    281:                        while (tmpused + cplen + 1 >= tmpsize) {
                    282:                                tmpsize *= 2;
                    283:                                tmp = xrealloc(tmp, tmpsize);
                    284:                        }
1.53      nicm      285:                        strlcat(tmp, cp, tmpsize);
1.48      nicm      286:                        tmpused = strlcat(tmp, " ", tmpsize);
1.29      nicm      287:                        free(cp);
                    288:
1.46      nicm      289:                        cp = cmd_list_print(bd->cmdlist, 1);
1.48      nicm      290:                        cplen = strlen(cp);
                    291:                        while (tmpused + cplen + 1 >= tmpsize) {
                    292:                                tmpsize *= 2;
                    293:                                tmp = xrealloc(tmp, tmpsize);
                    294:                        }
                    295:                        strlcat(tmp, cp, tmpsize);
1.31      nicm      296:                        free(cp);
1.1       nicm      297:
1.40      nicm      298:                        cmdq_print(item, "bind-key %s", tmp);
1.46      nicm      299:
                    300:                        free(key);
1.45      nicm      301:                        bd = key_bindings_next(table, bd);
1.14      nicm      302:                }
1.45      nicm      303:                table = key_bindings_next_table(table);
1.24      nicm      304:        }
1.48      nicm      305:
                    306:        free(tmp);
1.24      nicm      307:
1.50      nicm      308: out:
                    309:        if (only != KEYC_UNKNOWN && !found) {
                    310:                cmdq_error(item, "unknown key: %s", args->argv[0]);
                    311:                return (CMD_RETURN_ERROR);
                    312:        }
1.24      nicm      313:        return (CMD_RETURN_NORMAL);
                    314: }
                    315:
1.35      nicm      316: static enum cmd_retval
1.40      nicm      317: cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
1.24      nicm      318: {
1.54      nicm      319:        struct args              *args = cmd_get_args(self);
1.24      nicm      320:        const struct cmd_entry  **entryp;
1.25      nicm      321:        const struct cmd_entry   *entry;
1.35      nicm      322:        struct format_tree       *ft;
1.52      nicm      323:        const char               *template, *s, *command = NULL;
1.35      nicm      324:        char                     *line;
                    325:
1.52      nicm      326:        if (args->argc != 0)
                    327:                command = args->argv[0];
                    328:
1.35      nicm      329:        if ((template = args_get(args, 'F')) == NULL) {
                    330:                template = "#{command_list_name}"
                    331:                    "#{?command_list_alias, (#{command_list_alias}),} "
                    332:                    "#{command_list_usage}";
                    333:        }
                    334:
1.55      nicm      335:        ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
1.35      nicm      336:        format_defaults(ft, NULL, NULL, NULL, NULL);
1.24      nicm      337:
                    338:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    339:                entry = *entryp;
1.52      nicm      340:                if (command != NULL &&
                    341:                    (strcmp(entry->name, command) != 0 &&
                    342:                    (entry->alias == NULL ||
                    343:                    strcmp(entry->alias, command) != 0)))
                    344:                    continue;
1.35      nicm      345:
                    346:                format_add(ft, "command_list_name", "%s", entry->name);
1.40      nicm      347:                if (entry->alias != NULL)
                    348:                        s = entry->alias;
                    349:                else
                    350:                        s = "";
                    351:                format_add(ft, "command_list_alias", "%s", s);
                    352:                if (entry->usage != NULL)
                    353:                        s = entry->usage;
                    354:                else
                    355:                        s = "";
                    356:                format_add(ft, "command_list_usage", "%s", s);
1.35      nicm      357:
                    358:                line = format_expand(ft, template);
                    359:                if (*line != '\0')
1.40      nicm      360:                        cmdq_print(item, "%s", line);
1.35      nicm      361:                free(line);
1.1       nicm      362:        }
                    363:
1.35      nicm      364:        format_free(ft);
1.18      nicm      365:        return (CMD_RETURN_NORMAL);
1.1       nicm      366: }