-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp.c
93 lines (78 loc) · 2.23 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
/*
* Copyright (c) 2021 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "compat.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmd.h"
#include "keymap.h"
#include "telescope.h"
#include "ui.h"
#include "xwrapper.h"
static void emit_help_item(char *, interactivefn *);
static void rec_compute_help(struct kmap *, char *, size_t);
static void
emit_help_item(char *prfx, interactivefn *fn)
{
struct line *l;
struct cmd *cmd;
for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
if (fn == cmd->fn)
break;
}
assert(cmd != NULL);
l = xcalloc(1, sizeof(*l));
l->type = LINE_HELP;
l->line = xstrdup(prfx);
l->alt = (char*)cmd->cmd;
TAILQ_INSERT_TAIL(&helpwin.head, l, lines);
}
static void
rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
{
struct keymap *k;
char p[32];
const char *kn;
TAILQ_FOREACH(k, &keymap->m, keymaps) {
strlcpy(p, prfx, sizeof(p));
if (*p != '\0')
strlcat(p, " ", sizeof(p));
if (k->meta)
strlcat(p, "M-", sizeof(p));
if ((kn = unkbd(k->key)) != NULL)
strlcat(p, kn, sizeof(p));
else
strlcat(p, ui_keyname(k->key), sizeof(p));
if (k->fn == NULL)
rec_compute_help(&k->map, p, sizeof(p));
else
emit_help_item(p, k->fn);
}
}
void
recompute_help(void)
{
static struct kmap *last_active_map = NULL;
char p[32] = { 0 };
if (last_active_map != current_map) {
last_active_map = current_map;
helpwin.mode = "*Help*";
erase_buffer(&helpwin);
rec_compute_help(current_map, p, sizeof(p));
wrap_page(&helpwin, help_cols, 0);
}
}