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 de278567 2021-07-21 op #include "telescope.h"
25 de278567 2021-07-21 op #include "ui.h"
28 de278567 2021-07-21 op emit_help_item(char *prfx, void *fn)
30 de278567 2021-07-21 op struct line *l;
31 de278567 2021-07-21 op struct cmd *cmd;
33 de278567 2021-07-21 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
34 de278567 2021-07-21 op if (fn == cmd->fn)
37 de278567 2021-07-21 op assert(cmd != NULL);
39 de278567 2021-07-21 op if ((l = calloc(1, sizeof(*l))) == NULL)
42 de278567 2021-07-21 op l->type = LINE_TEXT;
43 de278567 2021-07-21 op l->alt = NULL;
45 de278567 2021-07-21 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
47 de278567 2021-07-21 op if (TAILQ_EMPTY(&helpwin.page.head))
48 de278567 2021-07-21 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
50 de278567 2021-07-21 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
54 de278567 2021-07-21 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
56 de278567 2021-07-21 op struct keymap *k;
58 de278567 2021-07-21 op const char *kn;
60 de278567 2021-07-21 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
61 de278567 2021-07-21 op strlcpy(p, prfx, sizeof(p));
62 de278567 2021-07-21 op if (*p != '\0')
63 de278567 2021-07-21 op strlcat(p, " ", sizeof(p));
65 de278567 2021-07-21 op strlcat(p, "M-", sizeof(p));
66 de278567 2021-07-21 op if ((kn = unkbd(k->key)) != NULL)
67 de278567 2021-07-21 op strlcat(p, kn, sizeof(p));
69 de278567 2021-07-21 op strlcat(p, ui_keyname(k->key), sizeof(p));
71 de278567 2021-07-21 op if (k->fn == NULL)
72 de278567 2021-07-21 op rec_compute_help(&k->map, p, sizeof(p));
74 de278567 2021-07-21 op emit_help_item(p, k->fn);
79 de278567 2021-07-21 op recompute_help(void)
81 7102f5d9 2021-07-21 op static struct kmap *last_active_map = NULL;
82 de278567 2021-07-21 op char p[32] = { 0 };
84 7102f5d9 2021-07-21 op if (last_active_map != current_map) {
85 7102f5d9 2021-07-21 op last_active_map = current_map;
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);