xref: /openbsd-src/usr.bin/tmux/cmd-list-keys.c (revision c88774048f0a7e9ace7afc00ed0f4ed84ad7cf1b)
1 /* $OpenBSD: cmd-list-keys.c,v 1.50 2020/01/27 08:53:13 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * List key bindings.
28  */
29 
30 static enum cmd_retval	cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
31 
32 static enum cmd_retval	cmd_list_keys_commands(struct cmd *,
33 			    struct cmdq_item *);
34 
35 const struct cmd_entry cmd_list_keys_entry = {
36 	.name = "list-keys",
37 	.alias = "lsk",
38 
39 	.args = { "1NP:T:", 0, 1 },
40 	.usage = "[-1N] [-P prefix-string] [-T key-table] [key]",
41 
42 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
43 	.exec = cmd_list_keys_exec
44 };
45 
46 const struct cmd_entry cmd_list_commands_entry = {
47 	.name = "list-commands",
48 	.alias = "lscm",
49 
50 	.args = { "F:", 0, 0 },
51 	.usage = "[-F format]",
52 
53 	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
54 	.exec = cmd_list_keys_exec
55 };
56 
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) ||
71 		    bd->note == NULL) {
72 			bd = key_bindings_next(table, bd);
73 			continue;
74 		}
75 		width = utf8_cstrwidth(key_string_lookup_key(bd->key));
76 		if (width > keywidth)
77 			keywidth = width;
78 
79 		bd = key_bindings_next(table, bd);
80 	}
81 	return (keywidth);
82 }
83 
84 static int
85 cmd_list_keys_print_notes(struct cmdq_item *item, struct args *args,
86     const char *tablename, u_int keywidth, key_code only, const char *prefix)
87 {
88 	struct client		*c = cmd_find_client(item, NULL, 1);
89 	struct key_table	*table;
90 	struct key_binding	*bd;
91 	const char		*key;
92 	char			*tmp;
93 	int	                 found = 0;
94 
95 	table = key_bindings_get_table(tablename, 0);
96 	if (table == NULL)
97 		return (0);
98 	bd = key_bindings_first(table);
99 	while (bd != NULL) {
100 		if ((only != KEYC_UNKNOWN && bd->key != only) ||
101 		    KEYC_IS_MOUSE(bd->key) ||
102 		    bd->note == NULL) {
103 			bd = key_bindings_next(table, bd);
104 			continue;
105 		}
106 		found = 1;
107 		key = key_string_lookup_key(bd->key);
108 
109 		tmp = utf8_padcstr(key, keywidth + 1);
110 		if (args_has(args, '1') && c != NULL)
111 			status_message_set(c, "%s%s%s", prefix, tmp, bd->note);
112 		else
113 			cmdq_print(item, "%s%s%s", prefix, tmp, bd->note);
114 		free(tmp);
115 
116 		if (args_has(args, '1'))
117 			break;
118 		bd = key_bindings_next(table, bd);
119 	}
120 	return (found);
121 }
122 
123 static char *
124 cmd_list_keys_get_prefix(struct args *args, key_code *prefix)
125 {
126 	char	*s;
127 
128 	*prefix = options_get_number(global_s_options, "prefix");
129 	if (!args_has(args, 'P')) {
130 		if (*prefix != KEYC_NONE)
131 			xasprintf(&s, "%s ", key_string_lookup_key(*prefix));
132 		else
133 			s = xstrdup("");
134 	} else
135 		s = xstrdup(args_get(args, 'P'));
136 	return (s);
137 }
138 
139 static enum cmd_retval
140 cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
141 {
142 	struct args		*args = self->args;
143 	struct key_table	*table;
144 	struct key_binding	*bd;
145 	const char		*tablename, *r;
146 	char			*key, *cp, *tmp, *start, *empty;
147 	key_code		 prefix, only = KEYC_UNKNOWN;
148 	int			 repeat, width, tablewidth, keywidth, found = 0;
149 	size_t			 tmpsize, tmpused, cplen;
150 
151 	if (self->entry == &cmd_list_commands_entry)
152 		return (cmd_list_keys_commands(self, item));
153 
154 	if (args->argc != 0) {
155 		only = key_string_lookup_string(args->argv[0]);
156 		if (only == KEYC_UNKNOWN) {
157 			cmdq_error(item, "invalid key: %s", args->argv[0]);
158 			return (CMD_RETURN_ERROR);
159 		}
160 	}
161 
162 	tablename = args_get(args, 'T');
163 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
164 		cmdq_error(item, "table %s doesn't exist", tablename);
165 		return (CMD_RETURN_ERROR);
166 	}
167 
168 	if (args_has(args, 'N')) {
169 		if (tablename == NULL) {
170 			start = cmd_list_keys_get_prefix(args, &prefix);
171 			keywidth = cmd_list_keys_get_width("root", only);
172 			if (prefix != KEYC_NONE) {
173 				width = cmd_list_keys_get_width("prefix", only);
174 				if (width == 0)
175 					prefix = KEYC_NONE;
176 				else if (width > keywidth)
177 					keywidth = width;
178 			}
179 			empty = utf8_padcstr("", utf8_cstrwidth(start));
180 
181 			found = cmd_list_keys_print_notes(item, args, "root",
182 			    keywidth, only, empty);
183 			if (prefix != KEYC_NONE) {
184 				if (cmd_list_keys_print_notes(item, args,
185 				    "prefix", keywidth, only, start))
186 					found = 1;
187 			}
188 			free(empty);
189 		} else {
190 			if (args_has(args, 'P'))
191 				start = xstrdup(args_get(args, 'P'));
192 			else
193 				start = xstrdup("");
194 			keywidth = cmd_list_keys_get_width(tablename, only);
195 			found = cmd_list_keys_print_notes(item, args, tablename,
196 			    keywidth, only, start);
197 
198 		}
199 		free(start);
200 		goto out;
201 	}
202 
203 	repeat = 0;
204 	tablewidth = keywidth = 0;
205 	table = key_bindings_first_table ();
206 	while (table != NULL) {
207 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
208 			table = key_bindings_next_table(table);
209 			continue;
210 		}
211 		bd = key_bindings_first(table);
212 		while (bd != NULL) {
213 			if (only != KEYC_UNKNOWN && bd->key != only) {
214 				bd = key_bindings_next(table, bd);
215 				continue;
216 			}
217 			key = args_escape(key_string_lookup_key(bd->key));
218 
219 			if (bd->flags & KEY_BINDING_REPEAT)
220 				repeat = 1;
221 
222 			width = utf8_cstrwidth(table->name);
223 			if (width > tablewidth)
224 				tablewidth = width;
225 			width = utf8_cstrwidth(key);
226 			if (width > keywidth)
227 				keywidth = width;
228 
229 			free(key);
230 			bd = key_bindings_next(table, bd);
231 		}
232 		table = key_bindings_next_table(table);
233 	}
234 
235 	tmpsize = 256;
236 	tmp = xmalloc(tmpsize);
237 
238 	table = key_bindings_first_table ();
239 	while (table != NULL) {
240 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
241 			table = key_bindings_next_table(table);
242 			continue;
243 		}
244 		bd = key_bindings_first(table);
245 		while (bd != NULL) {
246 			if (only != KEYC_UNKNOWN && bd->key != only) {
247 				bd = key_bindings_next(table, bd);
248 				continue;
249 			}
250 			found = 1;
251 			key = args_escape(key_string_lookup_key(bd->key));
252 
253 			if (!repeat)
254 				r = "";
255 			else if (bd->flags & KEY_BINDING_REPEAT)
256 				r = "-r ";
257 			else
258 				r = "   ";
259 			tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
260 
261 			cp = utf8_padcstr(table->name, tablewidth);
262 			cplen = strlen(cp) + 1;
263 			while (tmpused + cplen + 1 >= tmpsize) {
264 				tmpsize *= 2;
265 				tmp = xrealloc(tmp, tmpsize);
266 			}
267 			tmpused = strlcat(tmp, cp, tmpsize);
268 			tmpused = strlcat(tmp, " ", tmpsize);
269 			free(cp);
270 
271 			cp = utf8_padcstr(key, keywidth);
272 			cplen = strlen(cp) + 1;
273 			while (tmpused + cplen + 1 >= tmpsize) {
274 				tmpsize *= 2;
275 				tmp = xrealloc(tmp, tmpsize);
276 			}
277 			tmpused = strlcat(tmp, cp, tmpsize);
278 			tmpused = strlcat(tmp, " ", tmpsize);
279 			free(cp);
280 
281 			cp = cmd_list_print(bd->cmdlist, 1);
282 			cplen = strlen(cp);
283 			while (tmpused + cplen + 1 >= tmpsize) {
284 				tmpsize *= 2;
285 				tmp = xrealloc(tmp, tmpsize);
286 			}
287 			strlcat(tmp, cp, tmpsize);
288 			free(cp);
289 
290 			cmdq_print(item, "bind-key %s", tmp);
291 
292 			free(key);
293 			bd = key_bindings_next(table, bd);
294 		}
295 		table = key_bindings_next_table(table);
296 	}
297 
298 	free(tmp);
299 
300 out:
301 	if (only != KEYC_UNKNOWN && !found) {
302 		cmdq_error(item, "unknown key: %s", args->argv[0]);
303 		return (CMD_RETURN_ERROR);
304 	}
305 	return (CMD_RETURN_NORMAL);
306 }
307 
308 static enum cmd_retval
309 cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
310 {
311 	struct args		 *args = self->args;
312 	const struct cmd_entry	**entryp;
313 	const struct cmd_entry	 *entry;
314 	struct format_tree	 *ft;
315 	const char		 *template, *s;
316 	char			 *line;
317 
318 	if ((template = args_get(args, 'F')) == NULL) {
319 		template = "#{command_list_name}"
320 		    "#{?command_list_alias, (#{command_list_alias}),} "
321 		    "#{command_list_usage}";
322 	}
323 
324 	ft = format_create(item->client, item, FORMAT_NONE, 0);
325 	format_defaults(ft, NULL, NULL, NULL, NULL);
326 
327 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
328 		entry = *entryp;
329 
330 		format_add(ft, "command_list_name", "%s", entry->name);
331 		if (entry->alias != NULL)
332 			s = entry->alias;
333 		else
334 			s = "";
335 		format_add(ft, "command_list_alias", "%s", s);
336 		if (entry->usage != NULL)
337 			s = entry->usage;
338 		else
339 			s = "";
340 		format_add(ft, "command_list_usage", "%s", s);
341 
342 		line = format_expand(ft, template);
343 		if (*line != '\0')
344 			cmdq_print(item, "%s", line);
345 		free(line);
346 	}
347 
348 	format_free(ft);
349 	return (CMD_RETURN_NORMAL);
350 }
351