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/str.h>
20 #include "mutt_curses.h"
29 static struct binding_t *help_lookupFunction (int op, int menu)
32 struct binding_t *map;
34 if (menu != MENU_PAGER) {
35 /* first look in the generic map for the function */
36 for (i = 0; OpGeneric[i].name; i++)
37 if (OpGeneric[i].op == op)
38 return (&OpGeneric[i]);
41 if ((map = km_get_table (menu))) {
42 for (i = 0; map[i].name; i++)
50 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
52 char buf[SHORT_STRING];
54 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
55 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
56 snprintf (d, dlen, "%s:%s", buf, txt);
61 char *mutt_compile_help (char *buf, size_t buflen, int menu,
62 struct mapping_t *items)
68 for (i = 0; items[i].name && buflen > 2; i++) {
74 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
82 static int print_macro (FILE * f, int maxwidth, const char **macro)
88 size_t len = m_strlen(*macro);
89 mbstate_t mbstate1, mbstate2;
91 p_clear(&mbstate1, 1);
92 p_clear(&mbstate2, 1);
93 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
94 *macro += k, len -= k) {
95 if (k == (size_t) (-1) || k == (size_t) (-2)) {
96 k = (k == (size_t) (-1)) ? 1 : len;
97 wc = replacement_char ();
99 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
100 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
105 char buf[MB_LEN_MAX * 2];
108 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t) (-1) &&
109 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t) (-1))
113 else if (wc < 0x20 || wc == 0x7f) {
126 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
135 return (maxwidth - n);
138 static int pad (FILE * f, int col, int i)
143 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
144 fprintf (f, fmt, "");
151 static void format_line (FILE * f, int ismacro,
152 const char *t1, const char *t2, const char *t3)
161 /* don't try to press string into one line with less than 40 characters.
162 The double paranthesis avoid a gcc warning, sigh ... */
163 if ((split = COLS < 40)) {
169 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
170 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
171 col = pad (f, m_strlen(t1), col_a);
175 if (!m_strcmp(Pager, "builtin"))
181 col += print_macro (f, col_b - col - 4, &t2);
182 if (m_strlen(t2) > col_b - col)
187 col += print_macro (f, col_b - col - 1, &t2);
191 col = pad (f, col, col_b);
194 print_macro (f, LONG_STRING, &t3);
204 /* FIXME: this is completely wrong */
205 if ((n = m_strlen(t3)) > COLS - col) {
207 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
213 print_macro (f, n, &t3);
216 if (m_strcmp(Pager, "builtin")) {
222 if (option (OPTMARKERS))
225 col = pad (f, n, col_b);
233 static void dump_menu (FILE * f, int menu)
235 struct keymap_t *map;
237 char buf[SHORT_STRING];
239 /* browse through the keymap table */
240 for (map = Keymaps[menu]; map; map = map->next) {
241 if (map->op != OP_NULL) {
242 km_expand_key (buf, sizeof (buf), map);
244 if (map->op == OP_MACRO) {
245 if (map->descr == NULL)
246 format_line (f, -1, buf, "macro", map->macro);
248 format_line (f, 1, buf, map->macro, map->descr);
251 b = help_lookupFunction (map->op, menu);
252 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
253 b ? _(HelpStrings[b->op]) :
254 _("ERROR: please report this bug"));
260 static int is_bound (struct keymap_t *map, int op)
262 for (; map; map = map->next)
268 static void dump_unbound (FILE * f,
269 struct binding_t *funcs,
270 struct keymap_t *map, struct keymap_t *aux)
274 for (i = 0; funcs[i].name; i++) {
275 if (!is_bound (map, funcs[i].op) &&
276 (!aux || !is_bound (aux, funcs[i].op)))
277 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
281 void mutt_help (int menu)
283 char t[_POSIX_PATH_MAX];
284 char buf[SHORT_STRING];
287 struct binding_t *funcs;
291 funcs = km_get_table (menu);
292 desc = mutt_getnamebyvalue (menu, Menus);
294 desc = _("<UNKNOWN>");
297 if ((f = safe_fopen (t, "w")) == NULL) {
303 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
304 fputs (_("\nGeneric bindings:\n\n"), f);
305 dump_menu (f, MENU_GENERIC);
308 fputs (_("\nUnbound functions:\n\n"), f);
310 dump_unbound (f, funcs, Keymaps[menu], NULL);
311 if (menu != MENU_PAGER)
312 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
316 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
319 (mutt_do_pager (buf, t,
320 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
321 == OP_REFORMAT_WINCH);