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