Annotation of src/usr.bin/tmux/cmd-list-keys.c, Revision 1.36
1.36 ! nicm 1: /* $OpenBSD: cmd-list-keys.c,v 1.35 2016/06/15 08:54:11 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.35 nicm 30: static enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmd_q *);
1.24 nicm 31:
1.35 nicm 32: static enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmd_q *);
33: static enum cmd_retval cmd_list_keys_commands(struct cmd *, struct cmd_q *);
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:
39: .args = { "t:T:", 0, 0 },
40: .usage = "[-t mode-table] [-T key-table]",
41:
1.33 nicm 42: .flags = CMD_STARTSERVER,
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.33 nicm 53: .flags = CMD_STARTSERVER,
1.32 nicm 54: .exec = cmd_list_keys_exec
1.24 nicm 55: };
56:
1.35 nicm 57: static enum cmd_retval
1.21 nicm 58: cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
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.26 nicm 63: const char *key, *tablename, *r;
1.29 nicm 64: char *cp, tmp[BUFSIZ];
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.35 nicm 68: return (cmd_list_keys_commands(self, cmdq));
1.24 nicm 69:
1.13 nicm 70: if (args_has(args, 't'))
1.21 nicm 71: return (cmd_list_keys_table(self, cmdq));
1.6 nicm 72:
1.26 nicm 73: tablename = args_get(args, 'T');
74: if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
75: cmdq_error(cmdq, "table %s doesn't exist", tablename);
76: return (CMD_RETURN_ERROR);
77: }
1.15 nicm 78:
1.26 nicm 79: repeat = 0;
80: tablewidth = keywidth = 0;
81: RB_FOREACH(table, key_tables, &key_tables) {
82: if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.3 nicm 83: continue;
1.26 nicm 84: RB_FOREACH(bd, key_bindings, &table->key_bindings) {
85: key = key_string_lookup_key(bd->key);
1.3 nicm 86:
1.14 nicm 87: if (bd->can_repeat)
1.26 nicm 88: repeat = 1;
89:
1.29 nicm 90: width = utf8_cstrwidth(table->name);
1.26 nicm 91: if (width > tablewidth)
1.28 nicm 92: tablewidth = width;
93: width = utf8_cstrwidth(key);
1.26 nicm 94: if (width > keywidth)
95: keywidth = width;
96: }
1.3 nicm 97: }
98:
1.26 nicm 99: RB_FOREACH(table, key_tables, &key_tables) {
100: if (tablename != NULL && strcmp(table->name, tablename) != 0)
1.1 nicm 101: continue;
1.26 nicm 102: RB_FOREACH(bd, key_bindings, &table->key_bindings) {
103: key = key_string_lookup_key(bd->key);
104:
105: if (!repeat)
106: r = "";
107: else if (bd->can_repeat)
108: r = "-r ";
1.14 nicm 109: else
1.26 nicm 110: r = " ";
1.29 nicm 111: xsnprintf(tmp, sizeof tmp, "%s-T ", r);
112:
113: cp = utf8_padcstr(table->name, tablewidth);
114: strlcat(tmp, cp, sizeof tmp);
115: strlcat(tmp, " ", sizeof tmp);
116: free(cp);
117:
118: cp = utf8_padcstr(key, keywidth);
119: strlcat(tmp, cp, sizeof tmp);
120: strlcat(tmp, " ", sizeof tmp);
121: free(cp);
122:
1.31 nicm 123: cp = cmd_list_print(bd->cmdlist);
124: strlcat(tmp, cp, sizeof tmp);
125: free(cp);
1.1 nicm 126:
1.26 nicm 127: cmdq_print(cmdq, "bind-key %s", tmp);
128: }
1.6 nicm 129: }
130:
1.18 nicm 131: return (CMD_RETURN_NORMAL);
1.6 nicm 132: }
133:
1.35 nicm 134: static enum cmd_retval
1.21 nicm 135: cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq)
1.6 nicm 136: {
1.13 nicm 137: struct args *args = self->args;
1.36 ! nicm 138: const char *tablename, *key, *cmdstr, *mode;
1.6 nicm 139: const struct mode_key_table *mtab;
140: struct mode_key_binding *mbind;
1.36 ! nicm 141: char repeat[16];
! 142: int width, keywidth, repeatwidth, any_mode;
1.6 nicm 143:
1.13 nicm 144: tablename = args_get(args, 't');
145: if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.21 nicm 146: cmdq_error(cmdq, "unknown key table: %s", tablename);
1.18 nicm 147: return (CMD_RETURN_ERROR);
1.6 nicm 148: }
149:
1.36 ! nicm 150: keywidth = repeatwidth = 0;
1.14 nicm 151: any_mode = 0;
1.17 nicm 152: RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.6 nicm 153: key = key_string_lookup_key(mbind->key);
154:
1.14 nicm 155: if (mbind->mode != 0)
156: any_mode = 1;
157:
1.36 ! nicm 158: width = strlen(key);
! 159: if (width > keywidth)
! 160: keywidth = width;
! 161:
! 162: if (mbind->repeat != 1) {
! 163: snprintf(repeat, sizeof repeat, "%u", mbind->repeat);
! 164: width = strlen(repeat);
! 165: if (width > repeatwidth)
! 166: repeatwidth = width;
! 167: }
1.6 nicm 168: }
169:
1.17 nicm 170: RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
1.6 nicm 171: key = key_string_lookup_key(mbind->key);
172:
173: mode = "";
174: if (mbind->mode != 0)
1.14 nicm 175: mode = "c";
1.36 ! nicm 176: snprintf(repeat, sizeof repeat, "%u", mbind->repeat);
1.6 nicm 177: cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
1.14 nicm 178: if (cmdstr != NULL) {
1.36 ! nicm 179: cmdq_print(cmdq,
! 180: "bind-key -%st %s%s%s%*s %*s %s%s%s%s",
1.14 nicm 181: mode, any_mode && *mode == '\0' ? " " : "",
1.36 ! nicm 182: mtab->name,
! 183: mbind->repeat != 1 ? " -R " :
! 184: (repeatwidth == 0 ? "" : " "),
! 185: repeatwidth, mbind->repeat != 1 ? repeat : "",
! 186: (int)keywidth, key, cmdstr,
1.20 nicm 187: mbind->arg != NULL ? " \"" : "",
188: mbind->arg != NULL ? mbind->arg : "",
189: mbind->arg != NULL ? "\"": "");
1.14 nicm 190: }
1.24 nicm 191: }
192:
193: return (CMD_RETURN_NORMAL);
194: }
195:
1.35 nicm 196: static enum cmd_retval
197: cmd_list_keys_commands(struct cmd *self, struct cmd_q *cmdq)
1.24 nicm 198: {
1.35 nicm 199: struct args *args = self->args;
1.24 nicm 200: const struct cmd_entry **entryp;
1.25 nicm 201: const struct cmd_entry *entry;
1.35 nicm 202: struct format_tree *ft;
203: const char *template;
204: char *line;
205:
206: if ((template = args_get(args, 'F')) == NULL) {
207: template = "#{command_list_name}"
208: "#{?command_list_alias, (#{command_list_alias}),} "
209: "#{command_list_usage}";
210: }
211:
212: ft = format_create(cmdq, 0);
213: format_defaults(ft, NULL, NULL, NULL, NULL);
1.24 nicm 214:
215: for (entryp = cmd_table; *entryp != NULL; entryp++) {
216: entry = *entryp;
1.35 nicm 217:
218: format_add(ft, "command_list_name", "%s", entry->name);
219: if (entry->alias != NULL) {
220: format_add(ft, "command_list_alias", "%s",
221: entry->alias);
222: }
223: if (entry->alias != NULL) {
224: format_add(ft, "command_list_usage", "%s",
225: entry->usage);
1.24 nicm 226: }
1.35 nicm 227:
228: line = format_expand(ft, template);
229: if (*line != '\0')
230: cmdq_print(cmdq, "%s", line);
231: free(line);
1.1 nicm 232: }
233:
1.35 nicm 234: format_free(ft);
1.18 nicm 235: return (CMD_RETURN_NORMAL);
1.1 nicm 236: }