xref: /openbsd-src/usr.bin/tmux/cmd-list-keys.c (revision c020cf82e0cc147236f01a8dca7052034cf9d30d)
1 /* $OpenBSD: cmd-list-keys.c,v 1.59 2020/05/16 16:35: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 = { "1aNP:T:", 0, 1 },
40 	.usage = "[-1aN] [-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, 1 },
51 	.usage = "[-F format] [command]",
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->note == '\0') {
73 			bd = key_bindings_next(table, bd);
74 			continue;
75 		}
76 		width = utf8_cstrwidth(key_string_lookup_key(bd->key, 0));
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 {
89 	struct client		*tc = cmdq_get_target_client(item);
90 	struct key_table	*table;
91 	struct key_binding	*bd;
92 	const char		*key;
93 	char			*tmp, *note;
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) ||
103 		    ((bd->note == NULL || *bd->note == '\0') &&
104 		    !args_has(args, 'a'))) {
105 			bd = key_bindings_next(table, bd);
106 			continue;
107 		}
108 		found = 1;
109 		key = key_string_lookup_key(bd->key, 0);
110 
111 		if (bd->note == NULL || *bd->note == '\0')
112 			note = cmd_list_print(bd->cmdlist, 1);
113 		else
114 			note = xstrdup(bd->note);
115 		tmp = utf8_padcstr(key, keywidth + 1);
116 		if (args_has(args, '1') && tc != NULL)
117 			status_message_set(tc, 1, "%s%s%s", prefix, tmp, note);
118 		else
119 			cmdq_print(item, "%s%s%s", prefix, tmp, note);
120 		free(tmp);
121 		free(note);
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)
138 			xasprintf(&s, "%s ", key_string_lookup_key(*prefix, 0));
139 		else
140 			s = xstrdup("");
141 	} else
142 		s = xstrdup(args_get(args, 'P'));
143 	return (s);
144 }
145 
146 static enum cmd_retval
147 cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
148 {
149 	struct args		*args = cmd_get_args(self);
150 	struct key_table	*table;
151 	struct key_binding	*bd;
152 	const char		*tablename, *r;
153 	char			*key, *cp, *tmp, *start, *empty;
154 	key_code		 prefix, only = KEYC_UNKNOWN;
155 	int			 repeat, width, tablewidth, keywidth, found = 0;
156 	size_t			 tmpsize, tmpused, cplen;
157 
158 	if (cmd_get_entry(self) == &cmd_list_commands_entry)
159 		return (cmd_list_keys_commands(self, item));
160 
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 		}
167 	}
168 
169 	tablename = args_get(args, 'T');
170 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
171 		cmdq_error(item, "table %s doesn't exist", tablename);
172 		return (CMD_RETURN_ERROR);
173 	}
174 
175 	if (args_has(args, 'N')) {
176 		if (tablename == NULL) {
177 			start = cmd_list_keys_get_prefix(args, &prefix);
178 			keywidth = cmd_list_keys_get_width("root", only);
179 			if (prefix != KEYC_NONE) {
180 				width = cmd_list_keys_get_width("prefix", only);
181 				if (width == 0)
182 					prefix = KEYC_NONE;
183 				else if (width > keywidth)
184 					keywidth = width;
185 			}
186 			empty = utf8_padcstr("", utf8_cstrwidth(start));
187 
188 			found = cmd_list_keys_print_notes(item, args, "root",
189 			    keywidth, only, empty);
190 			if (prefix != KEYC_NONE) {
191 				if (cmd_list_keys_print_notes(item, args,
192 				    "prefix", keywidth, only, start))
193 					found = 1;
194 			}
195 			free(empty);
196 		} else {
197 			if (args_has(args, 'P'))
198 				start = xstrdup(args_get(args, 'P'));
199 			else
200 				start = xstrdup("");
201 			keywidth = cmd_list_keys_get_width(tablename, only);
202 			found = cmd_list_keys_print_notes(item, args, tablename,
203 			    keywidth, only, start);
204 
205 		}
206 		free(start);
207 		goto out;
208 	}
209 
210 	repeat = 0;
211 	tablewidth = keywidth = 0;
212 	table = key_bindings_first_table ();
213 	while (table != NULL) {
214 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
215 			table = key_bindings_next_table(table);
216 			continue;
217 		}
218 		bd = key_bindings_first(table);
219 		while (bd != NULL) {
220 			if (only != KEYC_UNKNOWN && bd->key != only) {
221 				bd = key_bindings_next(table, bd);
222 				continue;
223 			}
224 			key = args_escape(key_string_lookup_key(bd->key, 0));
225 
226 			if (bd->flags & KEY_BINDING_REPEAT)
227 				repeat = 1;
228 
229 			width = utf8_cstrwidth(table->name);
230 			if (width > tablewidth)
231 				tablewidth = width;
232 			width = utf8_cstrwidth(key);
233 			if (width > keywidth)
234 				keywidth = width;
235 
236 			free(key);
237 			bd = key_bindings_next(table, bd);
238 		}
239 		table = key_bindings_next_table(table);
240 	}
241 
242 	tmpsize = 256;
243 	tmp = xmalloc(tmpsize);
244 
245 	table = key_bindings_first_table ();
246 	while (table != NULL) {
247 		if (tablename != NULL && strcmp(table->name, tablename) != 0) {
248 			table = key_bindings_next_table(table);
249 			continue;
250 		}
251 		bd = key_bindings_first(table);
252 		while (bd != NULL) {
253 			if (only != KEYC_UNKNOWN && bd->key != only) {
254 				bd = key_bindings_next(table, bd);
255 				continue;
256 			}
257 			found = 1;
258 			key = args_escape(key_string_lookup_key(bd->key, 0));
259 
260 			if (!repeat)
261 				r = "";
262 			else if (bd->flags & KEY_BINDING_REPEAT)
263 				r = "-r ";
264 			else
265 				r = "   ";
266 			tmpused = xsnprintf(tmp, tmpsize, "%s-T ", r);
267 
268 			cp = utf8_padcstr(table->name, tablewidth);
269 			cplen = strlen(cp) + 1;
270 			while (tmpused + cplen + 1 >= tmpsize) {
271 				tmpsize *= 2;
272 				tmp = xrealloc(tmp, tmpsize);
273 			}
274 			strlcat(tmp, cp, tmpsize);
275 			tmpused = strlcat(tmp, " ", tmpsize);
276 			free(cp);
277 
278 			cp = utf8_padcstr(key, keywidth);
279 			cplen = strlen(cp) + 1;
280 			while (tmpused + cplen + 1 >= tmpsize) {
281 				tmpsize *= 2;
282 				tmp = xrealloc(tmp, tmpsize);
283 			}
284 			strlcat(tmp, cp, tmpsize);
285 			tmpused = strlcat(tmp, " ", tmpsize);
286 			free(cp);
287 
288 			cp = cmd_list_print(bd->cmdlist, 1);
289 			cplen = strlen(cp);
290 			while (tmpused + cplen + 1 >= tmpsize) {
291 				tmpsize *= 2;
292 				tmp = xrealloc(tmp, tmpsize);
293 			}
294 			strlcat(tmp, cp, tmpsize);
295 			free(cp);
296 
297 			cmdq_print(item, "bind-key %s", tmp);
298 
299 			free(key);
300 			bd = key_bindings_next(table, bd);
301 		}
302 		table = key_bindings_next_table(table);
303 	}
304 
305 	free(tmp);
306 
307 out:
308 	if (only != KEYC_UNKNOWN && !found) {
309 		cmdq_error(item, "unknown key: %s", args->argv[0]);
310 		return (CMD_RETURN_ERROR);
311 	}
312 	return (CMD_RETURN_NORMAL);
313 }
314 
315 static enum cmd_retval
316 cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
317 {
318 	struct args		 *args = cmd_get_args(self);
319 	const struct cmd_entry	**entryp;
320 	const struct cmd_entry	 *entry;
321 	struct format_tree	 *ft;
322 	const char		 *template, *s, *command = NULL;
323 	char			 *line;
324 
325 	if (args->argc != 0)
326 		command = args->argv[0];
327 
328 	if ((template = args_get(args, 'F')) == NULL) {
329 		template = "#{command_list_name}"
330 		    "#{?command_list_alias, (#{command_list_alias}),} "
331 		    "#{command_list_usage}";
332 	}
333 
334 	ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
335 	format_defaults(ft, NULL, NULL, NULL, NULL);
336 
337 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
338 		entry = *entryp;
339 		if (command != NULL &&
340 		    (strcmp(entry->name, command) != 0 &&
341 		    (entry->alias == NULL ||
342 		    strcmp(entry->alias, command) != 0)))
343 		    continue;
344 
345 		format_add(ft, "command_list_name", "%s", entry->name);
346 		if (entry->alias != NULL)
347 			s = entry->alias;
348 		else
349 			s = "";
350 		format_add(ft, "command_list_alias", "%s", s);
351 		if (entry->usage != NULL)
352 			s = entry->usage;
353 		else
354 			s = "";
355 		format_add(ft, "command_list_usage", "%s", s);
356 
357 		line = format_expand(ft, template);
358 		if (*line != '\0')
359 			cmdq_print(item, "%s", line);
360 		free(line);
361 	}
362 
363 	format_free(ft);
364 	return (CMD_RETURN_NORMAL);
365 }
366