Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "cmd.h"
25 #include "keymap.h"
26 #include "telescope.h"
27 #include "ui.h"
29 static void emit_help_item(char *, interactivefn *);
30 static void rec_compute_help(struct kmap *, char *, size_t);
32 static void
33 emit_help_item(char *prfx, interactivefn *fn)
34 {
35 struct line *l;
36 struct cmd *cmd;
38 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
39 if (fn == cmd->fn)
40 break;
41 }
42 assert(cmd != NULL);
44 if ((l = calloc(1, sizeof(*l))) == NULL)
45 abort();
47 l->type = LINE_HELP;
48 l->line = strdup(prfx);
49 l->alt = (char*)cmd->cmd;
51 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
52 }
54 static void
55 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
56 {
57 struct keymap *k;
58 char p[32];
59 const char *kn;
61 TAILQ_FOREACH(k, &keymap->m, keymaps) {
62 strlcpy(p, prfx, sizeof(p));
63 if (*p != '\0')
64 strlcat(p, " ", sizeof(p));
65 if (k->meta)
66 strlcat(p, "M-", sizeof(p));
67 if ((kn = unkbd(k->key)) != NULL)
68 strlcat(p, kn, sizeof(p));
69 else
70 strlcat(p, ui_keyname(k->key), sizeof(p));
72 if (k->fn == NULL)
73 rec_compute_help(&k->map, p, sizeof(p));
74 else
75 emit_help_item(p, k->fn);
76 }
77 }
79 void
80 recompute_help(void)
81 {
82 static struct kmap *last_active_map = NULL;
83 char p[32] = { 0 };
85 if (last_active_map != current_map) {
86 last_active_map = current_map;
88 helpwin.page.name = "*Help*";
89 erase_buffer(&helpwin);
90 rec_compute_help(current_map, p, sizeof(p));
91 wrap_page(&helpwin, help_cols);
92 }
93 }