Blame


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