cmd-list-keys.c revision 1.1.1.8 1 1.1.1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1.1.6 christos * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1.1.6 christos #include <stdlib.h>
22 1.1 jmmv #include <string.h>
23 1.1 jmmv
24 1.1 jmmv #include "tmux.h"
25 1.1 jmmv
26 1.1 jmmv /*
27 1.1 jmmv * List key bindings.
28 1.1 jmmv */
29 1.1 jmmv
30 1.1.1.7 christos static enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
31 1.1.1.5 christos
32 1.1.1.7 christos static enum cmd_retval cmd_list_keys_commands(struct cmd *,
33 1.1.1.7 christos struct cmdq_item *);
34 1.1 jmmv
35 1.1 jmmv const struct cmd_entry cmd_list_keys_entry = {
36 1.1.1.6 christos .name = "list-keys",
37 1.1.1.6 christos .alias = "lsk",
38 1.1.1.6 christos
39 1.1.1.7 christos .args = { "T:", 0, 0 },
40 1.1.1.7 christos .usage = "[-T key-table]",
41 1.1.1.6 christos
42 1.1.1.7 christos .flags = CMD_STARTSERVER|CMD_AFTERHOOK,
43 1.1.1.6 christos .exec = cmd_list_keys_exec
44 1.1.1.5 christos };
45 1.1.1.5 christos
46 1.1.1.5 christos const struct cmd_entry cmd_list_commands_entry = {
47 1.1.1.6 christos .name = "list-commands",
48 1.1.1.6 christos .alias = "lscm",
49 1.1.1.6 christos
50 1.1.1.7 christos .args = { "F:", 0, 0 },
51 1.1.1.7 christos .usage = "[-F format]",
52 1.1.1.6 christos
53 1.1.1.7 christos .flags = CMD_STARTSERVER|CMD_AFTERHOOK,
54 1.1.1.6 christos .exec = cmd_list_keys_exec
55 1.1 jmmv };
56 1.1 jmmv
57 1.1.1.7 christos static enum cmd_retval
58 1.1.1.7 christos cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
59 1.1 jmmv {
60 1.1.1.2 jmmv struct args *args = self->args;
61 1.1.1.5 christos struct key_table *table;
62 1.1 jmmv struct key_binding *bd;
63 1.1.1.5 christos const char *key, *tablename, *r;
64 1.1.1.6 christos char *cp, tmp[BUFSIZ];
65 1.1.1.5 christos int repeat, width, tablewidth, keywidth;
66 1.1.1.5 christos
67 1.1.1.5 christos if (self->entry == &cmd_list_commands_entry)
68 1.1.1.7 christos return (cmd_list_keys_commands(self, item));
69 1.1 jmmv
70 1.1.1.5 christos tablename = args_get(args, 'T');
71 1.1.1.5 christos if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
72 1.1.1.7 christos cmdq_error(item, "table %s doesn't exist", tablename);
73 1.1.1.5 christos return (CMD_RETURN_ERROR);
74 1.1.1.5 christos }
75 1.1.1.2 jmmv
76 1.1.1.5 christos repeat = 0;
77 1.1.1.5 christos tablewidth = keywidth = 0;
78 1.1.1.5 christos RB_FOREACH(table, key_tables, &key_tables) {
79 1.1.1.5 christos if (tablename != NULL && strcmp(table->name, tablename) != 0)
80 1.1 jmmv continue;
81 1.1.1.5 christos RB_FOREACH(bd, key_bindings, &table->key_bindings) {
82 1.1.1.5 christos key = key_string_lookup_key(bd->key);
83 1.1 jmmv
84 1.1.1.8 christos if (bd->flags & KEY_BINDING_REPEAT)
85 1.1.1.5 christos repeat = 1;
86 1.1.1.5 christos
87 1.1.1.6 christos width = utf8_cstrwidth(table->name);
88 1.1.1.5 christos if (width > tablewidth)
89 1.1.1.6 christos tablewidth = width;
90 1.1.1.6 christos width = utf8_cstrwidth(key);
91 1.1.1.5 christos if (width > keywidth)
92 1.1.1.5 christos keywidth = width;
93 1.1.1.5 christos }
94 1.1 jmmv }
95 1.1 jmmv
96 1.1.1.5 christos RB_FOREACH(table, key_tables, &key_tables) {
97 1.1.1.5 christos if (tablename != NULL && strcmp(table->name, tablename) != 0)
98 1.1 jmmv continue;
99 1.1.1.5 christos RB_FOREACH(bd, key_bindings, &table->key_bindings) {
100 1.1.1.5 christos key = key_string_lookup_key(bd->key);
101 1.1.1.5 christos
102 1.1.1.5 christos if (!repeat)
103 1.1.1.5 christos r = "";
104 1.1.1.8 christos else if (bd->flags & KEY_BINDING_REPEAT)
105 1.1.1.5 christos r = "-r ";
106 1.1.1.2 jmmv else
107 1.1.1.5 christos r = " ";
108 1.1.1.6 christos xsnprintf(tmp, sizeof tmp, "%s-T ", r);
109 1.1.1.6 christos
110 1.1.1.6 christos cp = utf8_padcstr(table->name, tablewidth);
111 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
112 1.1.1.6 christos strlcat(tmp, " ", sizeof tmp);
113 1.1.1.6 christos free(cp);
114 1.1.1.6 christos
115 1.1.1.6 christos cp = utf8_padcstr(key, keywidth);
116 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
117 1.1.1.6 christos strlcat(tmp, " ", sizeof tmp);
118 1.1.1.6 christos free(cp);
119 1.1.1.6 christos
120 1.1.1.6 christos cp = cmd_list_print(bd->cmdlist);
121 1.1.1.6 christos strlcat(tmp, cp, sizeof tmp);
122 1.1.1.6 christos free(cp);
123 1.1 jmmv
124 1.1.1.7 christos cmdq_print(item, "bind-key %s", tmp);
125 1.1.1.2 jmmv }
126 1.1 jmmv }
127 1.1 jmmv
128 1.1.1.3 christos return (CMD_RETURN_NORMAL);
129 1.1 jmmv }
130 1.1.1.5 christos
131 1.1.1.7 christos static enum cmd_retval
132 1.1.1.7 christos cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
133 1.1.1.5 christos {
134 1.1.1.7 christos struct args *args = self->args;
135 1.1.1.5 christos const struct cmd_entry **entryp;
136 1.1.1.5 christos const struct cmd_entry *entry;
137 1.1.1.7 christos struct format_tree *ft;
138 1.1.1.7 christos const char *template, *s;
139 1.1.1.7 christos char *line;
140 1.1.1.7 christos
141 1.1.1.7 christos if ((template = args_get(args, 'F')) == NULL) {
142 1.1.1.7 christos template = "#{command_list_name}"
143 1.1.1.7 christos "#{?command_list_alias, (#{command_list_alias}),} "
144 1.1.1.7 christos "#{command_list_usage}";
145 1.1.1.7 christos }
146 1.1.1.7 christos
147 1.1.1.8 christos ft = format_create(item->client, item, FORMAT_NONE, 0);
148 1.1.1.7 christos format_defaults(ft, NULL, NULL, NULL, NULL);
149 1.1.1.5 christos
150 1.1.1.5 christos for (entryp = cmd_table; *entryp != NULL; entryp++) {
151 1.1.1.5 christos entry = *entryp;
152 1.1.1.7 christos
153 1.1.1.7 christos format_add(ft, "command_list_name", "%s", entry->name);
154 1.1.1.7 christos if (entry->alias != NULL)
155 1.1.1.7 christos s = entry->alias;
156 1.1.1.7 christos else
157 1.1.1.7 christos s = "";
158 1.1.1.7 christos format_add(ft, "command_list_alias", "%s", s);
159 1.1.1.7 christos if (entry->usage != NULL)
160 1.1.1.7 christos s = entry->usage;
161 1.1.1.7 christos else
162 1.1.1.7 christos s = "";
163 1.1.1.7 christos format_add(ft, "command_list_usage", "%s", s);
164 1.1.1.7 christos
165 1.1.1.7 christos line = format_expand(ft, template);
166 1.1.1.7 christos if (*line != '\0')
167 1.1.1.7 christos cmdq_print(item, "%s", line);
168 1.1.1.7 christos free(line);
169 1.1.1.5 christos }
170 1.1.1.5 christos
171 1.1.1.7 christos format_free(ft);
172 1.1.1.5 christos return (CMD_RETURN_NORMAL);
173 1.1.1.5 christos }
174