2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
21 #include <lib-lib/macros.h>
22 #include <lib-lib/file.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/mapping.h>
26 #include <lib-ui/curses.h>
32 static struct binding_t *help_lookupFunction (int op, int menu)
35 struct binding_t *map;
37 if (menu != MENU_PAGER) {
38 /* first look in the generic map for the function */
39 for (i = 0; OpGeneric[i].name; i++)
40 if (OpGeneric[i].op == op)
41 return (&OpGeneric[i]);
44 if ((map = km_get_table (menu))) {
45 for (i = 0; map[i].name; i++)
53 void mutt_make_help (char *d, ssize_t dlen, char *txt, int menu, int op)
55 char buf[SHORT_STRING];
57 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
58 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
59 snprintf (d, dlen, "%s:%s", buf, txt);
64 char *mutt_compile_help (char *buf, ssize_t buflen, int menu,
65 struct mapping_t *items)
71 for (i = 0; items[i].name && buflen > 2; i++) {
77 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
85 static int print_macro (FILE * f, int maxwidth, const char **macro)
91 ssize_t len = m_strlen(*macro);
92 mbstate_t mbstate1, mbstate2;
94 p_clear(&mbstate1, 1);
95 p_clear(&mbstate2, 1);
96 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
97 *macro += k, len -= k) {
98 if (k == -1 || k == -2) {
99 k = (k == -1) ? 1 : len;
100 wc = replacement_char ();
102 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
103 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
108 char buf[MB_LEN_MAX * 2];
111 if ((n1 = wcrtomb(buf, wc, &mbstate2)) != -1 &&
112 (n2 = wcrtomb(buf + n1, 0, &mbstate2)) != -1)
116 else if (wc < 0x20 || wc == 0x7f) {
129 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
138 return (maxwidth - n);
141 static int pad (FILE * f, int col, int i)
146 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
147 fprintf (f, fmt, "");
154 static void format_line (FILE * f, int ismacro,
155 const char *t1, const char *t2, const char *t3)
164 /* don't try to press string into one line with less than 40 characters.
165 The double paranthesis avoid a gcc warning, sigh ... */
166 if ((split = COLS < 40)) {
172 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
173 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
174 col = pad (f, m_strlen(t1), col_a);
178 if (!m_strcmp(Pager, "builtin"))
184 col += print_macro (f, col_b - col - 4, &t2);
185 if (m_strlen(t2) > col_b - col)
190 col += print_macro (f, col_b - col - 1, &t2);
194 col = pad (f, col, col_b);
197 print_macro (f, LONG_STRING, &t3);
205 t3 = vskipspaces(t3);
207 /* FIXME: this is completely wrong */
208 if ((n = m_strlen(t3)) > COLS - col) {
210 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
216 print_macro (f, n, &t3);
219 if (m_strcmp(Pager, "builtin")) {
225 if (option (OPTMARKERS))
228 col = pad (f, n, col_b);
236 static void dump_menu (FILE * f, int menu)
238 struct keymap_t *map;
240 char buf[SHORT_STRING];
242 /* browse through the keymap table */
243 for (map = Keymaps[menu]; map; map = map->next) {
244 if (map->op != OP_NULL) {
245 km_expand_key (buf, sizeof (buf), map);
247 if (map->op == OP_MACRO) {
248 if (map->descr == NULL)
249 format_line (f, -1, buf, "macro", map->macro);
251 format_line (f, 1, buf, map->macro, map->descr);
254 b = help_lookupFunction (map->op, menu);
255 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
256 b ? _(HelpStrings[b->op]) :
257 _("ERROR: please report this bug"));
263 static int is_bound (struct keymap_t *map, int op)
265 for (; map; map = map->next)
271 static void dump_unbound (FILE * f,
272 struct binding_t *funcs,
273 struct keymap_t *map, struct keymap_t *aux)
277 for (i = 0; funcs[i].name; i++) {
278 if (!is_bound (map, funcs[i].op) &&
279 (!aux || !is_bound (aux, funcs[i].op)))
280 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
284 void mutt_help (int menu)
286 char t[_POSIX_PATH_MAX];
287 char buf[SHORT_STRING];
290 struct binding_t *funcs;
294 funcs = km_get_table (menu);
295 desc = mutt_getnamebyvalue (menu, Menus);
297 desc = _("<UNKNOWN>");
300 if ((f = safe_fopen (t, "w")) == NULL) {
306 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
307 fputs (_("\nGeneric bindings:\n\n"), f);
308 dump_menu (f, MENU_GENERIC);
311 fputs (_("\nUnbound functions:\n\n"), f);
313 dump_unbound (f, funcs, Keymaps[menu], NULL);
314 if (menu != MENU_PAGER)
315 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
319 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
322 (mutt_do_pager (buf, t,
323 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
324 == OP_REFORMAT_WINCH);