xref: /openbsd-src/usr.bin/tmux/cmd-list-keys.c (revision aa60fd48f9c224fe43fa9d6fd7d5e3ce80f807b4)
1 /* $OpenBSD: cmd-list-keys.c,v 1.27 2015/10/26 22:03:04 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * List key bindings.
27  */
28 
29 enum cmd_retval	 cmd_list_keys_exec(struct cmd *, struct cmd_q *);
30 
31 enum cmd_retval	 cmd_list_keys_table(struct cmd *, struct cmd_q *);
32 enum cmd_retval	 cmd_list_keys_commands(struct cmd *, struct cmd_q *);
33 
34 const struct cmd_entry cmd_list_keys_entry = {
35 	"list-keys", "lsk",
36 	"t:T:", 0, 0,
37 	"[-t mode-table] [-T key-table]",
38 	0,
39 	cmd_list_keys_exec
40 };
41 
42 const struct cmd_entry cmd_list_commands_entry = {
43 	"list-commands", "lscm",
44 	"", 0, 0,
45 	"",
46 	0,
47 	cmd_list_keys_exec
48 };
49 
50 enum cmd_retval
51 cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq)
52 {
53 	struct args		*args = self->args;
54 	struct key_table	*table;
55 	struct key_binding	*bd;
56 	const char		*key, *tablename, *r;
57 	char			 tmp[BUFSIZ];
58 	size_t			 used;
59 	int			 repeat, width, tablewidth, keywidth;
60 
61 	if (self->entry == &cmd_list_commands_entry)
62 		return (cmd_list_keys_commands(self, cmdq));
63 
64 	if (args_has(args, 't'))
65 		return (cmd_list_keys_table(self, cmdq));
66 
67 	tablename = args_get(args, 'T');
68 	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
69 		cmdq_error(cmdq, "table %s doesn't exist", tablename);
70 		return (CMD_RETURN_ERROR);
71 	}
72 
73 	repeat = 0;
74 	tablewidth = keywidth = 0;
75 	RB_FOREACH(table, key_tables, &key_tables) {
76 		if (tablename != NULL && strcmp(table->name, tablename) != 0)
77 			continue;
78 		RB_FOREACH(bd, key_bindings, &table->key_bindings) {
79 			key = key_string_lookup_key(bd->key);
80 
81 			if (bd->can_repeat)
82 				repeat = 1;
83 
84 			width = strlen(table->name);
85 			if (width > tablewidth)
86 				tablewidth =width;
87 			width = strlen(key);
88 			if (width > keywidth)
89 				keywidth = width;
90 		}
91 	}
92 
93 	RB_FOREACH(table, key_tables, &key_tables) {
94 		if (tablename != NULL && strcmp(table->name, tablename) != 0)
95 			continue;
96 		RB_FOREACH(bd, key_bindings, &table->key_bindings) {
97 			key = key_string_lookup_key(bd->key);
98 
99 			if (!repeat)
100 				r = "";
101 			else if (bd->can_repeat)
102 				r = "-r ";
103 			else
104 				r = "   ";
105 			used = xsnprintf(tmp, sizeof tmp, "%s-T %-*s %-*s ", r,
106 			    (int)tablewidth, table->name, (int)keywidth, key);
107 			if (used < sizeof tmp) {
108 				cmd_list_print(bd->cmdlist, tmp + used,
109 				    (sizeof tmp) - used);
110 			}
111 
112 			cmdq_print(cmdq, "bind-key %s", tmp);
113 		}
114 	}
115 
116 	return (CMD_RETURN_NORMAL);
117 }
118 
119 enum cmd_retval
120 cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq)
121 {
122 	struct args			*args = self->args;
123 	const char			*tablename;
124 	const struct mode_key_table	*mtab;
125 	struct mode_key_binding		*mbind;
126 	const char			*key, *cmdstr, *mode;
127 	int			 	 width, keywidth, any_mode;
128 
129 	tablename = args_get(args, 't');
130 	if ((mtab = mode_key_findtable(tablename)) == NULL) {
131 		cmdq_error(cmdq, "unknown key table: %s", tablename);
132 		return (CMD_RETURN_ERROR);
133 	}
134 
135 	width = 0;
136 	any_mode = 0;
137 	RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
138 		key = key_string_lookup_key(mbind->key);
139 
140 		if (mbind->mode != 0)
141 			any_mode = 1;
142 
143 		keywidth = strlen(key);
144 		if (keywidth > width)
145 			width = keywidth;
146 	}
147 
148 	RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
149 		key = key_string_lookup_key(mbind->key);
150 
151 		mode = "";
152 		if (mbind->mode != 0)
153 			mode = "c";
154 		cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
155 		if (cmdstr != NULL) {
156 			cmdq_print(cmdq, "bind-key -%st %s%s %*s %s%s%s%s",
157 			    mode, any_mode && *mode == '\0' ? " " : "",
158 			    mtab->name, (int) width, key, cmdstr,
159 			    mbind->arg != NULL ? " \"" : "",
160 			    mbind->arg != NULL ? mbind->arg : "",
161 			    mbind->arg != NULL ? "\"": "");
162 		}
163 	}
164 
165 	return (CMD_RETURN_NORMAL);
166 }
167 
168 enum cmd_retval
169 cmd_list_keys_commands(unused struct cmd *self, struct cmd_q *cmdq)
170 {
171 	const struct cmd_entry	**entryp;
172 	const struct cmd_entry	 *entry;
173 
174 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
175 		entry = *entryp;
176 		if (entry->alias == NULL) {
177 			cmdq_print(cmdq, "%s %s", entry->name, entry->usage);
178 			continue;
179 		}
180 		cmdq_print(cmdq, "%s (%s) %s", entry->name, entry->alias,
181 		    entry->usage);
182 	}
183 
184 	return (CMD_RETURN_NORMAL);
185 }
186