2 * Copyright (C) 1996-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 pad (FILE *f, int col, int i)
156 snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
157 fprintf (f, fmt, "");
164 static void format_line (FILE *f, int ismacro,
165 const char *t1, const char *t2, const char *t3)
174 /* don't try to press string into one line with less than 40 characters.
175 The double paranthesis avoid a gcc warning, sigh ... */
176 if ((split = COLS < 40))
184 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
185 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
186 col = pad (f, mutt_strlen(t1), col_a);
191 if (!mutt_strcmp (Pager, "builtin"))
198 col += print_macro (f, col_b - col - 4, &t2);
199 if (mutt_strlen (t2) > col_b - col)
204 col += print_macro (f, col_b - col - 1, &t2);
208 col = pad (f, col, col_b);
212 print_macro (f, LONG_STRING, &t3);
225 /* FIXME: this is completely wrong */
226 if ((n = mutt_strlen (t3)) > COLS - col)
229 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--) ;
235 print_macro (f, n, &t3);
239 if (mutt_strcmp (Pager, "builtin"))
247 if (option (OPTMARKERS))
250 col = pad (f, n, col_b);
258 static void dump_menu (FILE *f, int menu)
260 struct keymap_t *map;
262 char buf[SHORT_STRING];
264 /* browse through the keymap table */
265 for (map = Keymaps[menu]; map; map = map->next)
267 if (map->op != OP_NULL)
269 km_expand_key (buf, sizeof (buf), map);
271 if (map->op == OP_MACRO)
273 if (map->descr == NULL)
274 format_line (f, -1, buf, "macro", map->macro);
276 format_line (f, 1, buf, map->macro, map->descr);
280 b = help_lookupFunction (map->op, menu);
281 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
282 b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
288 static int is_bound (struct keymap_t *map, int op)
290 for (; map; map = map->next)
296 static void dump_unbound (FILE *f,
297 struct binding_t *funcs,
298 struct keymap_t *map,
299 struct keymap_t *aux)
303 for (i = 0; funcs[i].name; i++)
305 if (! is_bound (map, funcs[i].op) &&
306 (!aux || ! is_bound (aux, funcs[i].op)))
307 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
311 void mutt_help (int menu)
313 char t[_POSIX_PATH_MAX];
314 char buf[SHORT_STRING];
317 struct binding_t *funcs;
321 funcs = km_get_table (menu);
322 desc = mutt_getnamebyvalue (menu, Menus);
324 desc = _("<UNKNOWN>");
327 if ((f = safe_fopen (t, "w")) == NULL)
334 if (menu != MENU_EDITOR && menu != MENU_PAGER)
336 fputs (_("\nGeneric bindings:\n\n"), f);
337 dump_menu (f, MENU_GENERIC);
340 fputs (_("\nUnbound functions:\n\n"), f);
342 dump_unbound (f, funcs, Keymaps[menu], NULL);
343 if (menu != MENU_PAGER)
344 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
348 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
351 (mutt_do_pager (buf, t,
352 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
354 == OP_REFORMAT_WINCH);