2 de278567 2021-07-21 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 de278567 2021-07-21 op #include "compat.h"
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>
24 cbe2da32 2024-02-06 op #include "cmd.h"
25 f7db6d13 2024-02-06 op #include "keymap.h"
26 de278567 2021-07-21 op #include "telescope.h"
27 de278567 2021-07-21 op #include "ui.h"
28 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
30 408b5181 2021-07-21 op static void emit_help_item(char *, interactivefn *);
31 408b5181 2021-07-21 op static void rec_compute_help(struct kmap *, char *, size_t);
34 408b5181 2021-07-21 op emit_help_item(char *prfx, interactivefn *fn)
36 de278567 2021-07-21 op struct line *l;
37 de278567 2021-07-21 op struct cmd *cmd;
39 de278567 2021-07-21 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
40 de278567 2021-07-21 op if (fn == cmd->fn)
43 de278567 2021-07-21 op assert(cmd != NULL);
45 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
47 c6be26e4 2021-07-21 op l->type = LINE_HELP;
48 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(prfx);
49 c6be26e4 2021-07-21 op l->alt = (char*)cmd->cmd;
51 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&helpwin.head, l, lines);
55 de278567 2021-07-21 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
57 de278567 2021-07-21 op struct keymap *k;
59 de278567 2021-07-21 op const char *kn;
61 de278567 2021-07-21 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
62 de278567 2021-07-21 op strlcpy(p, prfx, sizeof(p));
63 de278567 2021-07-21 op if (*p != '\0')
64 de278567 2021-07-21 op strlcat(p, " ", sizeof(p));
66 de278567 2021-07-21 op strlcat(p, "M-", sizeof(p));
67 de278567 2021-07-21 op if ((kn = unkbd(k->key)) != NULL)
68 de278567 2021-07-21 op strlcat(p, kn, sizeof(p));
70 de278567 2021-07-21 op strlcat(p, ui_keyname(k->key), sizeof(p));
72 de278567 2021-07-21 op if (k->fn == NULL)
73 de278567 2021-07-21 op rec_compute_help(&k->map, p, sizeof(p));
75 de278567 2021-07-21 op emit_help_item(p, k->fn);
80 de278567 2021-07-21 op recompute_help(void)
82 7102f5d9 2021-07-21 op static struct kmap *last_active_map = NULL;
83 de278567 2021-07-21 op char p[32] = { 0 };
85 7102f5d9 2021-07-21 op if (last_active_map != current_map) {
86 7102f5d9 2021-07-21 op last_active_map = current_map;
88 c1d27b0e 2024-06-14 op helpwin.mode = "*Help*";
89 7102f5d9 2021-07-21 op erase_buffer(&helpwin);
90 7102f5d9 2021-07-21 op rec_compute_help(current_map, p, sizeof(p));
91 f853ec6f 2024-10-22 op wrap_page(&helpwin, help_cols, 0);