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.
22 #include "mutt_curses.h"
30 static struct binding_t *help_lookupFunction (int op, int menu)
33 struct binding_t *map;
35 if (menu != MENU_PAGER)
37 /* first look in the generic map for the function */
38 for (i = 0; OpGeneric[i].name; i++)
39 if (OpGeneric[i].op == op)
40 return (&OpGeneric[i]);
43 if ((map = km_get_table(menu)))
45 for (i = 0; map[i].name; i++)
53 void mutt_make_help (char *d, size_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);
65 mutt_compile_help (char *buf, size_t buflen, int menu, struct mapping_t *items)
71 for (i = 0; items[i].name && buflen > 2; i++)
79 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
80 len = mutt_strlen (pbuf);
87 static int print_macro (FILE *f, int maxwidth, const char **macro)
93 size_t len = mutt_strlen (*macro);
94 mbstate_t mbstate1, mbstate2;
96 memset (&mbstate1, 0, sizeof (mbstate1));
97 memset (&mbstate2, 0, sizeof (mbstate2));
98 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1)); *macro += k, len -= k)
100 if (k == (size_t)(-1) || k == (size_t)(-2))
102 k = (k == (size_t)(-1)) ? 1 : len;
103 wc = replacement_char ();
105 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
106 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0)
112 char buf[MB_LEN_MAX*2];
114 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t)(-1) &&
115 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t)(-1))
119 else if (wc < 0x20 || wc == 0x7f)
133 fprintf (f, "^%c", (char)((wc + '@') & 0x7f));
143 return (maxwidth - n);
146 static int pad (FILE *f, int col, int i)
152 snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
153 fprintf (f, fmt, "");
160 static void format_line (FILE *f, int ismacro,
161 const char *t1, const char *t2, const char *t3)
170 /* don't try to press string into one line with less than 40 characters.
171 The double paranthesis avoid a gcc warning, sigh ... */
172 if ((split = COLS < 40))
180 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
181 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
182 col = pad (f, mutt_strlen(t1), col_a);
187 if (!mutt_strcmp (Pager, "builtin"))
194 col += print_macro (f, col_b - col - 4, &t2);
195 if (mutt_strlen (t2) > col_b - col)
200 col += print_macro (f, col_b - col - 1, &t2);
204 col = pad (f, col, col_b);
208 print_macro (f, LONG_STRING, &t3);
221 /* FIXME: this is completely wrong */
222 if ((n = mutt_strlen (t3)) > COLS - col)
225 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--) ;
231 print_macro (f, n, &t3);
235 if (mutt_strcmp (Pager, "builtin"))
243 if (option (OPTMARKERS))
246 col = pad (f, n, col_b);
254 static void dump_menu (FILE *f, int menu)
256 struct keymap_t *map;
258 char buf[SHORT_STRING];
260 /* browse through the keymap table */
261 for (map = Keymaps[menu]; map; map = map->next)
263 if (map->op != OP_NULL)
265 km_expand_key (buf, sizeof (buf), map);
267 if (map->op == OP_MACRO)
269 if (map->descr == NULL)
270 format_line (f, -1, buf, "macro", map->macro);
272 format_line (f, 1, buf, map->macro, map->descr);
276 b = help_lookupFunction (map->op, menu);
277 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
278 b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
284 static int is_bound (struct keymap_t *map, int op)
286 for (; map; map = map->next)
292 static void dump_unbound (FILE *f,
293 struct binding_t *funcs,
294 struct keymap_t *map,
295 struct keymap_t *aux)
299 for (i = 0; funcs[i].name; i++)
301 if (! is_bound (map, funcs[i].op) &&
302 (!aux || ! is_bound (aux, funcs[i].op)))
303 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
307 void mutt_help (int menu)
309 char t[_POSIX_PATH_MAX];
310 char buf[SHORT_STRING];
313 struct binding_t *funcs;
317 funcs = km_get_table (menu);
318 desc = mutt_getnamebyvalue (menu, Menus);
320 desc = _("<UNKNOWN>");
323 if ((f = safe_fopen (t, "w")) == NULL)
330 if (menu != MENU_EDITOR && menu != MENU_PAGER)
332 fputs (_("\nGeneric bindings:\n\n"), f);
333 dump_menu (f, MENU_GENERIC);
336 fputs (_("\nUnbound functions:\n\n"), f);
338 dump_unbound (f, funcs, Keymaps[menu], NULL);
339 if (menu != MENU_PAGER)
340 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
344 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
347 (mutt_do_pager (buf, t,
348 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
350 == OP_REFORMAT_WINCH);