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) {
40 /* first look in the generic map for the function */
41 for (i = 0; OpGeneric[i].name; i++)
42 if (OpGeneric[i].op == op)
43 return (&OpGeneric[i]);
46 if ((map = km_get_table (menu))) {
47 for (i = 0; map[i].name; i++)
55 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
57 char buf[SHORT_STRING];
59 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
60 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
61 snprintf (d, dlen, "%s:%s", buf, txt);
66 char *mutt_compile_help (char *buf, size_t buflen, int menu,
67 struct mapping_t *items)
73 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));
99 *macro += k, len -= k) {
100 if (k == (size_t) (-1) || k == (size_t) (-2)) {
101 k = (k == (size_t) (-1)) ? 1 : len;
102 wc = replacement_char ();
104 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
105 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
110 char buf[MB_LEN_MAX * 2];
113 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t) (-1) &&
114 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t) (-1))
118 else if (wc < 0x20 || wc == 0x7f) {
131 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
140 return (maxwidth - n);
143 static int pad (FILE * f, int col, int i)
148 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
149 fprintf (f, fmt, "");
156 static void format_line (FILE * f, int ismacro,
157 const char *t1, const char *t2, const char *t3)
166 /* don't try to press string into one line with less than 40 characters.
167 The double paranthesis avoid a gcc warning, sigh ... */
168 if ((split = COLS < 40)) {
174 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
175 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
176 col = pad (f, mutt_strlen (t1), col_a);
180 if (!mutt_strcmp (Pager, "builtin"))
186 col += print_macro (f, col_b - col - 4, &t2);
187 if (mutt_strlen (t2) > col_b - col)
192 col += print_macro (f, col_b - col - 1, &t2);
196 col = pad (f, col, col_b);
199 print_macro (f, LONG_STRING, &t3);
209 /* FIXME: this is completely wrong */
210 if ((n = mutt_strlen (t3)) > COLS - col) {
212 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
218 print_macro (f, n, &t3);
221 if (mutt_strcmp (Pager, "builtin")) {
227 if (option (OPTMARKERS))
230 col = pad (f, n, col_b);
238 static void dump_menu (FILE * f, int menu)
240 struct keymap_t *map;
242 char buf[SHORT_STRING];
244 /* browse through the keymap table */
245 for (map = Keymaps[menu]; map; map = map->next) {
246 if (map->op != OP_NULL) {
247 km_expand_key (buf, sizeof (buf), map);
249 if (map->op == OP_MACRO) {
250 if (map->descr == NULL)
251 format_line (f, -1, buf, "macro", map->macro);
253 format_line (f, 1, buf, map->macro, map->descr);
256 b = help_lookupFunction (map->op, menu);
257 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
258 b ? _(HelpStrings[b->op]) :
259 _("ERROR: please report this bug"));
265 static int is_bound (struct keymap_t *map, int op)
267 for (; map; map = map->next)
273 static void dump_unbound (FILE * f,
274 struct binding_t *funcs,
275 struct keymap_t *map, struct keymap_t *aux)
279 for (i = 0; funcs[i].name; i++) {
280 if (!is_bound (map, funcs[i].op) &&
281 (!aux || !is_bound (aux, funcs[i].op)))
282 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
286 void mutt_help (int menu)
288 char t[_POSIX_PATH_MAX];
289 char buf[SHORT_STRING];
292 struct binding_t *funcs;
296 funcs = km_get_table (menu);
297 desc = mutt_getnamebyvalue (menu, Menus);
299 desc = _("<UNKNOWN>");
302 if ((f = safe_fopen (t, "w")) == NULL) {
308 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
309 fputs (_("\nGeneric bindings:\n\n"), f);
310 dump_menu (f, MENU_GENERIC);
313 fputs (_("\nUnbound functions:\n\n"), f);
315 dump_unbound (f, funcs, Keymaps[menu], NULL);
316 if (menu != MENU_PAGER)
317 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
321 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
324 (mutt_do_pager (buf, t,
325 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
326 == OP_REFORMAT_WINCH);