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.
16 #include <lib-lib/macros.h>
17 #include <lib-lib/file.h>
18 #include <lib-lib/str.h>
21 #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) {
36 /* first look in the generic map for the function */
37 for (i = 0; OpGeneric[i].name; i++)
38 if (OpGeneric[i].op == op)
39 return (&OpGeneric[i]);
42 if ((map = km_get_table (menu))) {
43 for (i = 0; map[i].name; i++)
51 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
53 char buf[SHORT_STRING];
55 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
56 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
57 snprintf (d, dlen, "%s:%s", buf, txt);
62 char *mutt_compile_help (char *buf, size_t buflen, int menu,
63 struct mapping_t *items)
69 for (i = 0; items[i].name && buflen > 2; i++) {
75 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
83 static int print_macro (FILE * f, int maxwidth, const char **macro)
89 size_t len = m_strlen(*macro);
90 mbstate_t mbstate1, mbstate2;
92 p_clear(&mbstate1, 1);
93 p_clear(&mbstate2, 1);
94 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
95 *macro += k, len -= k) {
96 if (k == (size_t) (-1) || k == (size_t) (-2)) {
97 k = (k == (size_t) (-1)) ? 1 : len;
98 wc = replacement_char ();
100 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
101 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
106 char buf[MB_LEN_MAX * 2];
109 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t) (-1) &&
110 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t) (-1))
114 else if (wc < 0x20 || wc == 0x7f) {
127 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
136 return (maxwidth - n);
139 static int pad (FILE * f, int col, int i)
144 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
145 fprintf (f, fmt, "");
152 static void format_line (FILE * f, int ismacro,
153 const char *t1, const char *t2, const char *t3)
162 /* don't try to press string into one line with less than 40 characters.
163 The double paranthesis avoid a gcc warning, sigh ... */
164 if ((split = COLS < 40)) {
170 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
171 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
172 col = pad (f, m_strlen(t1), col_a);
176 if (!m_strcmp(Pager, "builtin"))
182 col += print_macro (f, col_b - col - 4, &t2);
183 if (m_strlen(t2) > col_b - col)
188 col += print_macro (f, col_b - col - 1, &t2);
192 col = pad (f, col, col_b);
195 print_macro (f, LONG_STRING, &t3);
205 /* FIXME: this is completely wrong */
206 if ((n = m_strlen(t3)) > COLS - col) {
208 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
214 print_macro (f, n, &t3);
217 if (m_strcmp(Pager, "builtin")) {
223 if (option (OPTMARKERS))
226 col = pad (f, n, col_b);
234 static void dump_menu (FILE * f, int menu)
236 struct keymap_t *map;
238 char buf[SHORT_STRING];
240 /* browse through the keymap table */
241 for (map = Keymaps[menu]; map; map = map->next) {
242 if (map->op != OP_NULL) {
243 km_expand_key (buf, sizeof (buf), map);
245 if (map->op == OP_MACRO) {
246 if (map->descr == NULL)
247 format_line (f, -1, buf, "macro", map->macro);
249 format_line (f, 1, buf, map->macro, map->descr);
252 b = help_lookupFunction (map->op, menu);
253 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
254 b ? _(HelpStrings[b->op]) :
255 _("ERROR: please report this bug"));
261 static int is_bound (struct keymap_t *map, int op)
263 for (; map; map = map->next)
269 static void dump_unbound (FILE * f,
270 struct binding_t *funcs,
271 struct keymap_t *map, struct keymap_t *aux)
275 for (i = 0; funcs[i].name; i++) {
276 if (!is_bound (map, funcs[i].op) &&
277 (!aux || !is_bound (aux, funcs[i].op)))
278 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
282 void mutt_help (int menu)
284 char t[_POSIX_PATH_MAX];
285 char buf[SHORT_STRING];
288 struct binding_t *funcs;
292 funcs = km_get_table (menu);
293 desc = mutt_getnamebyvalue (menu, Menus);
295 desc = _("<UNKNOWN>");
298 if ((f = safe_fopen (t, "w")) == NULL) {
304 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
305 fputs (_("\nGeneric bindings:\n\n"), f);
306 dump_menu (f, MENU_GENERIC);
309 fputs (_("\nUnbound functions:\n\n"), f);
311 dump_unbound (f, funcs, Keymaps[menu], NULL);
312 if (menu != MENU_PAGER)
313 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
317 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
320 (mutt_do_pager (buf, t,
321 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
322 == OP_REFORMAT_WINCH);