-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.c
327 lines (285 loc) · 7.41 KB
/
help.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2008-2013 Various Authors
* Copyright 2006 <[email protected]>
*
* heavily based on filters.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "help.h"
#include "window.h"
#include "search.h"
#include "misc.h"
#include "xmalloc.h"
#include "keys.h"
#include "command_mode.h"
#include "ui_curses.h"
#include "options.h"
#include "cmdline.h"
#include <stdio.h>
struct window *help_win;
struct searchable *help_searchable;
static LIST_HEAD(help_head);
static struct list_head *bound_head;
static struct list_head *bound_tail;
static struct list_head *unbound_head;
static struct list_head *unbound_tail;
static inline void help_entry_to_iter(struct help_entry *e, struct iter *iter)
{
iter->data0 = &help_head;
iter->data1 = e;
iter->data2 = NULL;
}
static GENERIC_ITER_PREV(help_get_prev, struct help_entry, node)
static GENERIC_ITER_NEXT(help_get_next, struct help_entry, node)
static int help_search_get_current(void *data, struct iter *iter)
{
return window_get_sel(help_win, iter);
}
static int help_search_matches(void *data, struct iter *iter, const char *text)
{
int matched = 0;
char **words = get_words(text);
if (words[0] != NULL) {
struct help_entry *ent;
int i;
ent = iter_to_help_entry(iter);
for (i = 0; ; i++) {
if (words[i] == NULL) {
window_set_sel(help_win, iter);
matched = 1;
break;
}
if (ent->type == HE_TEXT) {
if (!u_strcasestr(ent->text, words[i]))
break;
} else if (ent->type == HE_BOUND) {
if (!u_strcasestr(ent->binding->cmd, words[i]) &&
!u_strcasestr(ent->binding->key->name, words[i]))
break;
} else if (ent->type == HE_UNBOUND) {
if (!u_strcasestr(ent->command->name, words[i]))
break;
} else if (ent->type == HE_OPTION) {
if (!u_strcasestr(ent->option->name, words[i]))
break;
}
}
}
free_str_array(words);
return matched;
}
static const struct searchable_ops help_search_ops = {
.get_prev = help_get_prev,
.get_next = help_get_next,
.get_current = help_search_get_current,
.matches = help_search_matches
};
static void help_add_text(const char *s)
{
struct help_entry *ent;
ent = xnew(struct help_entry, 1);
ent->type = HE_TEXT;
ent->text = s;
list_add_tail(&ent->node, &help_head);
}
static void help_add_defaults(void)
{
struct cmus_opt *opt;
help_add_text("Keybindings");
help_add_text("-----------");
bound_head = help_head.prev;
help_add_text("");
help_add_text("Unbound Commands");
help_add_text("----------------");
unbound_head = help_head.prev;
help_add_text("");
help_add_text("Options");
help_add_text("-------");
list_for_each_entry(opt, &option_head, node) {
struct help_entry *ent = xnew(struct help_entry, 1);
ent->type = HE_OPTION;
ent->option = opt;
list_add_tail(&ent->node, &help_head);
}
bound_tail = bound_head->next;
unbound_tail = unbound_head->next;
}
void help_remove_unbound(struct command *cmd)
{
struct help_entry *ent;
struct iter i;
list_for_each_entry(ent, &help_head, node) {
if (ent->type != HE_UNBOUND)
continue;
if (ent->command == cmd) {
help_entry_to_iter(ent, &i);
window_row_vanishes(help_win, &i);
list_del(&ent->node);
free(ent);
return;
}
}
}
static void list_add_sorted(struct list_head *new, struct list_head *head,
struct list_head *tail,
int (*cmp)(struct list_head *, struct list_head *))
{
struct list_head *item = tail->prev;
while (item != head) {
if (cmp(new, item) >= 0)
break;
item = item->prev;
}
/* add after item */
list_add(new, item);
}
static int bound_cmp(struct list_head *ai, struct list_head *bi)
{
struct help_entry *a = container_of(ai, struct help_entry, node);
struct help_entry *b = container_of(bi, struct help_entry, node);
int ret = a->binding->ctx - b->binding->ctx;
if (!ret)
ret = strcmp(a->binding->key->name, b->binding->key->name);
return ret;
}
static int unbound_cmp(struct list_head *ai, struct list_head *bi)
{
struct help_entry *a = container_of(ai, struct help_entry, node);
struct help_entry *b = container_of(bi, struct help_entry, node);
return strcmp(a->command->name, b->command->name);
}
void help_add_unbound(struct command *cmd)
{
struct help_entry *ent;
ent = xnew(struct help_entry, 1);
ent->type = HE_UNBOUND;
ent->command = cmd;
list_add_sorted(&ent->node, unbound_head, unbound_tail, unbound_cmp);
}
void help_add_all_unbound(void)
{
int i;
for (i = 0; commands[i].name; ++i)
if (!commands[i].bc)
help_add_unbound(&commands[i]);
}
void help_select(void)
{
struct iter sel;
struct help_entry *ent;
char buf[OPTION_MAX_SIZE];
if (!window_get_sel(help_win, &sel))
return;
ent = iter_to_help_entry(&sel);
switch (ent->type) {
case HE_BOUND:
snprintf(buf, sizeof(buf), "bind -f %s %s %s",
key_context_names[ent->binding->ctx],
ent->binding->key->name,
ent->binding->cmd);
cmdline_set_text(buf);
enter_command_mode();
break;
case HE_UNBOUND:
snprintf(buf, sizeof(buf), "bind common <key> %s",
ent->command->name);
cmdline_set_text(buf);
enter_command_mode();
break;
case HE_OPTION:
snprintf(buf, sizeof(buf), "set %s=", ent->option->name);
size_t len = strlen(buf);
ent->option->get(ent->option->data, buf + len, sizeof(buf) - len);
cmdline_set_text(buf);
enter_command_mode();
break;
default:
break;
}
}
void help_toggle(void)
{
struct iter sel;
struct help_entry *ent;
if (!window_get_sel(help_win, &sel))
return;
ent = iter_to_help_entry(&sel);
switch (ent->type) {
case HE_OPTION:
if (ent->option->toggle) {
ent->option->toggle(ent->option->data);
help_win->changed = 1;
}
break;
default:
break;
}
}
void help_remove(void)
{
struct iter sel;
struct help_entry *ent;
if (!window_get_sel(help_win, &sel))
return;
ent = iter_to_help_entry(&sel);
switch (ent->type) {
case HE_BOUND:
if (yes_no_query("Remove selected binding? [y/N]") == UI_QUERY_ANSWER_YES)
key_unbind(key_context_names[ent->binding->ctx],
ent->binding->key->name, 0);
break;
default:
break;
}
}
void help_add_bound(const struct binding *bind)
{
struct help_entry *ent;
ent = xnew(struct help_entry, 1);
ent->type = HE_BOUND;
ent->binding = bind;
list_add_sorted(&ent->node, bound_head, bound_tail, bound_cmp);
}
void help_remove_bound(const struct binding *bind)
{
struct help_entry *ent;
struct iter i;
list_for_each_entry(ent, &help_head, node) {
if (ent->binding == bind) {
help_entry_to_iter(ent, &i);
window_row_vanishes(help_win, &i);
list_del(&ent->node);
free(ent);
return;
}
}
}
void help_init(void)
{
struct iter iter;
help_win = window_new(help_get_prev, help_get_next);
window_set_contents(help_win, &help_head);
window_changed(help_win);
help_add_defaults();
iter.data0 = &help_head;
iter.data1 = NULL;
iter.data2 = NULL;
help_searchable = searchable_new(NULL, &iter, &help_search_ops);
}
void help_exit(void)
{
searchable_free(help_searchable);
window_free(help_win);
}