2 * Copyright (C) 1996-2000,2009 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "mutt_curses.h"
34 static struct binding_t *help_lookupFunction (int op, int menu)
37 struct binding_t *map;
39 if (menu != MENU_PAGER)
41 /* first look in the generic map for the function */
42 for (i = 0; OpGeneric[i].name; i++)
43 if (OpGeneric[i].op == op)
44 return (&OpGeneric[i]);
47 if ((map = km_get_table(menu)))
49 for (i = 0; map[i].name; i++)
57 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
59 char buf[SHORT_STRING];
61 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
62 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
63 snprintf (d, dlen, "%s:%s", buf, txt);
69 mutt_compile_help (char *buf, size_t buflen, int menu, struct mapping_t *items)
75 for (i = 0; items[i].name && buflen > 2; i++)
83 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
84 len = mutt_strlen (pbuf);
91 static int print_macro (FILE *f, int maxwidth, const char **macro)
97 size_t len = mutt_strlen (*macro);
98 mbstate_t mbstate1, mbstate2;
100 memset (&mbstate1, 0, sizeof (mbstate1));
101 memset (&mbstate2, 0, sizeof (mbstate2));
102 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1)); *macro += k, len -= k)
104 if (k == (size_t)(-1) || k == (size_t)(-2))
106 k = (k == (size_t)(-1)) ? 1 : len;
107 wc = replacement_char ();
109 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
110 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0)
116 char buf[MB_LEN_MAX*2];
118 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t)(-1) &&
119 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t)(-1))
123 else if (wc < 0x20 || wc == 0x7f)
137 fprintf (f, "^%c", (char)((wc + '@') & 0x7f));
147 return (maxwidth - n);
150 static int get_wrapped_width (const char *t, size_t wid)
155 size_t len = mutt_strlen (t);
159 memset (&mbstate, 0, sizeof (mbstate));
161 len && (k = mbrtowc (&wc, s, len, &mbstate)) && (n <= wid);
166 if (k == (size_t)(-1) || k == (size_t)(-2))
168 k = (k == (size_t)(-1)) ? 1 : len;
169 wc = replacement_char ();
182 static int pad (FILE *f, int col, int i)
188 snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
189 fprintf (f, fmt, "");
196 static void format_line (FILE *f, int ismacro,
197 const char *t1, const char *t2, const char *t3)
206 /* don't try to press string into one line with less than 40 characters.
207 The double paranthesis avoid a gcc warning, sigh ... */
208 if ((split = COLS < 40))
216 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
217 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
218 col = pad (f, mutt_strwidth(t1), col_a);
223 if (!mutt_strcmp (Pager, "builtin"))
230 col += print_macro (f, col_b - col - 4, &t2);
231 if (mutt_strwidth (t2) > col_b - col)
236 col += print_macro (f, col_b - col - 1, &t2);
240 col = pad (f, col, col_b);
244 print_macro (f, LONG_STRING, &t3);
256 n = get_wrapped_width (t3, n);
259 n = print_macro (f, n, &t3);
263 if (mutt_strcmp (Pager, "builtin"))
271 if (option (OPTMARKERS))
274 col = pad (f, n, col_b);
282 static void dump_menu (FILE *f, int menu)
284 struct keymap_t *map;
286 char buf[SHORT_STRING];
288 /* browse through the keymap table */
289 for (map = Keymaps[menu]; map; map = map->next)
291 if (map->op != OP_NULL)
293 km_expand_key (buf, sizeof (buf), map);
295 if (map->op == OP_MACRO)
297 if (map->descr == NULL)
298 format_line (f, -1, buf, "macro", map->macro);
300 format_line (f, 1, buf, map->macro, map->descr);
304 b = help_lookupFunction (map->op, menu);
305 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
306 b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
312 static int is_bound (struct keymap_t *map, int op)
314 for (; map; map = map->next)
320 static void dump_unbound (FILE *f,
321 struct binding_t *funcs,
322 struct keymap_t *map,
323 struct keymap_t *aux)
327 for (i = 0; funcs[i].name; i++)
329 if (! is_bound (map, funcs[i].op) &&
330 (!aux || ! is_bound (aux, funcs[i].op)))
331 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
335 void mutt_help (int menu)
337 char t[_POSIX_PATH_MAX];
338 char buf[SHORT_STRING];
341 struct binding_t *funcs;
343 mutt_mktemp (t, sizeof (t));
345 funcs = km_get_table (menu);
346 desc = mutt_getnamebyvalue (menu, Menus);
348 desc = _("<UNKNOWN>");
351 if ((f = safe_fopen (t, "w")) == NULL)
358 if (menu != MENU_EDITOR && menu != MENU_PAGER)
360 fputs (_("\nGeneric bindings:\n\n"), f);
361 dump_menu (f, MENU_GENERIC);
364 fputs (_("\nUnbound functions:\n\n"), f);
366 dump_unbound (f, funcs, Keymaps[menu], NULL);
367 if (menu != MENU_PAGER)
368 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
372 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
375 (mutt_do_pager (buf, t,
376 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP | M_PAGER_NOWRAP,
378 == OP_REFORMAT_WINCH);