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.
20 #include "mutt_curses.h"
28 static struct binding_t *help_lookupFunction (int op, int menu)
31 struct binding_t *map;
33 if (menu != MENU_PAGER) {
34 /* first look in the generic map for the function */
35 for (i = 0; OpGeneric[i].name; i++)
36 if (OpGeneric[i].op == op)
37 return (&OpGeneric[i]);
40 if ((map = km_get_table (menu))) {
41 for (i = 0; map[i].name; i++)
49 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
51 char buf[SHORT_STRING];
53 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
54 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
55 snprintf (d, dlen, "%s:%s", buf, txt);
60 char *mutt_compile_help (char *buf, size_t buflen, int menu,
61 struct mapping_t *items)
67 for (i = 0; items[i].name && buflen > 2; i++) {
73 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
81 static int print_macro (FILE * f, int maxwidth, const char **macro)
87 size_t len = str_len (*macro);
88 mbstate_t mbstate1, mbstate2;
90 memset (&mbstate1, 0, sizeof (mbstate1));
91 memset (&mbstate2, 0, sizeof (mbstate2));
92 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
93 *macro += k, len -= k) {
94 if (k == (size_t) (-1) || k == (size_t) (-2)) {
95 k = (k == (size_t) (-1)) ? 1 : len;
96 wc = replacement_char ();
98 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
99 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
104 char buf[MB_LEN_MAX * 2];
107 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t) (-1) &&
108 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t) (-1))
112 else if (wc < 0x20 || wc == 0x7f) {
125 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
134 return (maxwidth - n);
137 static int pad (FILE * f, int col, int i)
142 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
143 fprintf (f, fmt, "");
150 static void format_line (FILE * f, int ismacro,
151 const char *t1, const char *t2, const char *t3)
160 /* don't try to press string into one line with less than 40 characters.
161 The double paranthesis avoid a gcc warning, sigh ... */
162 if ((split = COLS < 40)) {
168 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
169 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
170 col = pad (f, str_len (t1), col_a);
174 if (!str_cmp (Pager, "builtin"))
180 col += print_macro (f, col_b - col - 4, &t2);
181 if (str_len (t2) > col_b - col)
186 col += print_macro (f, col_b - col - 1, &t2);
190 col = pad (f, col, col_b);
193 print_macro (f, LONG_STRING, &t3);
203 /* FIXME: this is completely wrong */
204 if ((n = str_len (t3)) > COLS - col) {
206 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
212 print_macro (f, n, &t3);
215 if (str_cmp (Pager, "builtin")) {
221 if (option (OPTMARKERS))
224 col = pad (f, n, col_b);
232 static void dump_menu (FILE * f, int menu)
234 struct keymap_t *map;
236 char buf[SHORT_STRING];
238 /* browse through the keymap table */
239 for (map = Keymaps[menu]; map; map = map->next) {
240 if (map->op != OP_NULL) {
241 km_expand_key (buf, sizeof (buf), map);
243 if (map->op == OP_MACRO) {
244 if (map->descr == NULL)
245 format_line (f, -1, buf, "macro", map->macro);
247 format_line (f, 1, buf, map->macro, map->descr);
250 b = help_lookupFunction (map->op, menu);
251 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
252 b ? _(HelpStrings[b->op]) :
253 _("ERROR: please report this bug"));
259 static int is_bound (struct keymap_t *map, int op)
261 for (; map; map = map->next)
267 static void dump_unbound (FILE * f,
268 struct binding_t *funcs,
269 struct keymap_t *map, struct keymap_t *aux)
273 for (i = 0; funcs[i].name; i++) {
274 if (!is_bound (map, funcs[i].op) &&
275 (!aux || !is_bound (aux, funcs[i].op)))
276 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
280 void mutt_help (int menu)
282 char t[_POSIX_PATH_MAX];
283 char buf[SHORT_STRING];
286 struct binding_t *funcs;
290 funcs = km_get_table (menu);
291 desc = mutt_getnamebyvalue (menu, Menus);
293 desc = _("<UNKNOWN>");
296 if ((f = safe_fopen (t, "w")) == NULL) {
302 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
303 fputs (_("\nGeneric bindings:\n\n"), f);
304 dump_menu (f, MENU_GENERIC);
307 fputs (_("\nUnbound functions:\n\n"), f);
309 dump_unbound (f, funcs, Keymaps[menu], NULL);
310 if (menu != MENU_PAGER)
311 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
315 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
318 (mutt_do_pager (buf, t,
319 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
320 == OP_REFORMAT_WINCH);