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.
12 #include <lib-lib/lib-lib.h>
14 #include <lib-ui/curses.h>
21 static struct binding_t *help_lookupFunction (int op, int menu)
24 struct binding_t *map;
26 if (menu != MENU_PAGER) {
27 /* first look in the generic map for the function */
28 for (i = 0; OpGeneric[i].name; i++)
29 if (OpGeneric[i].op == op)
30 return (&OpGeneric[i]);
33 if ((map = km_get_table (menu))) {
34 for (i = 0; map[i].name; i++)
42 void mutt_make_help (char *d, ssize_t dlen, char *txt, int menu, int op)
46 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
47 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
48 snprintf (d, dlen, "%s:%s", buf, txt);
53 char *mutt_compile_help (char *buf, ssize_t buflen, int menu,
54 struct mapping_t *items)
60 for (i = 0; items[i].name && buflen > 2; i++) {
66 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
74 static int print_macro (FILE * f, int maxwidth, const char **macro)
80 ssize_t len = m_strlen(*macro);
81 mbstate_t mbstate1, mbstate2;
83 p_clear(&mbstate1, 1);
84 p_clear(&mbstate2, 1);
85 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
86 *macro += k, len -= k) {
87 if (k == -1 || k == -2) {
88 k = (k == -1) ? 1 : len;
89 wc = CharsetReplacement;
91 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
92 if (iswprint(wc) && (w = wcwidth (wc)) >= 0) {
97 char buf[MB_LEN_MAX * 2];
100 if ((n1 = wcrtomb(buf, wc, &mbstate2)) != -1 &&
101 (n2 = wcrtomb(buf + n1, 0, &mbstate2)) != -1)
105 else if (wc < 0x20 || wc == 0x7f) {
118 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
127 return (maxwidth - n);
130 static int pad (FILE * f, int col, int i)
135 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
136 fprintf (f, fmt, "");
143 static void format_line (FILE * f, int ismacro,
144 const char *t1, const char *t2, const char *t3)
153 /* don't try to press string into one line with less than 40 characters.
154 The double paranthesis avoid a gcc warning, sigh ... */
155 if ((split = COLS < 40)) {
161 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
162 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
163 col = pad (f, m_strlen(t1), col_a);
167 if (!m_strcmp(Pager, "builtin"))
173 col += print_macro (f, col_b - col - 4, &t2);
174 if (m_strlen(t2) > col_b - col)
179 col += print_macro (f, col_b - col - 1, &t2);
183 col = pad (f, col, col_b);
186 print_macro (f, LONG_STRING, &t3);
194 t3 = vskipspaces(t3);
196 /* FIXME: this is completely wrong */
197 if ((n = m_strlen(t3)) > COLS - col) {
199 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
205 print_macro (f, n, &t3);
208 if (m_strcmp(Pager, "builtin")) {
214 if (option (OPTMARKERS))
217 col = pad (f, n, col_b);
225 static void dump_menu (FILE * f, int menu)
227 struct keymap_t *map;
231 /* browse through the keymap table */
232 for (map = Keymaps[menu]; map; map = map->next) {
233 if (map->op != OP_NULL) {
234 km_expand_key (buf, sizeof (buf), map);
236 if (map->op == OP_MACRO) {
237 if (map->descr == NULL)
238 format_line (f, -1, buf, "macro", map->macro);
240 format_line (f, 1, buf, map->macro, map->descr);
243 b = help_lookupFunction (map->op, menu);
244 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
245 b ? _(HelpStrings[b->op]) :
246 _("ERROR: please report this bug"));
252 static int is_bound (struct keymap_t *map, int op)
254 for (; map; map = map->next)
260 static void dump_unbound (FILE * f,
261 struct binding_t *funcs,
262 struct keymap_t *map, struct keymap_t *aux)
266 for (i = 0; funcs[i].name; i++) {
267 if (!is_bound (map, funcs[i].op) &&
268 (!aux || !is_bound (aux, funcs[i].op)))
269 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
273 void mutt_help (int menu)
275 char tmp[_POSIX_PATH_MAX];
279 struct binding_t *funcs;
281 funcs = km_get_table (menu);
282 desc = mutt_getnamebyvalue (menu, Menus);
284 desc = _("<UNKNOWN>");
287 f = m_tempfile(tmp, sizeof(tmp), NONULL(MCore.tmpdir), NULL);
294 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
295 fputs(_("\nGeneric bindings:\n\n"), f);
296 dump_menu(f, MENU_GENERIC);
299 fputs(_("\nUnbound functions:\n\n"), f);
301 dump_unbound(f, funcs, Keymaps[menu], NULL);
303 if (menu != MENU_PAGER)
304 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
308 snprintf(buf, sizeof (buf), _("Help for %s"), desc);
309 } while (mutt_do_pager(buf, tmp,
310 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
311 NULL) == OP_REFORMAT_WINCH);