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>
19 #include <lib-lib/mapping.h>
21 #include <lib-ui/curses.h>
31 static struct binding_t *help_lookupFunction (int op, int menu)
34 struct binding_t *map;
36 if (menu != MENU_PAGER) {
37 /* first look in the generic map for the function */
38 for (i = 0; OpGeneric[i].name; i++)
39 if (OpGeneric[i].op == op)
40 return (&OpGeneric[i]);
43 if ((map = km_get_table (menu))) {
44 for (i = 0; map[i].name; i++)
52 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
54 char buf[SHORT_STRING];
56 if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
57 km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
58 snprintf (d, dlen, "%s:%s", buf, txt);
63 char *mutt_compile_help (char *buf, size_t buflen, int menu,
64 struct mapping_t *items)
70 for (i = 0; items[i].name && buflen > 2; i++) {
76 mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
84 static int print_macro (FILE * f, int maxwidth, const char **macro)
90 size_t len = m_strlen(*macro);
91 mbstate_t mbstate1, mbstate2;
93 p_clear(&mbstate1, 1);
94 p_clear(&mbstate2, 1);
95 for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
96 *macro += k, len -= k) {
97 if (k == (size_t) (-1) || k == (size_t) (-2)) {
98 k = (k == (size_t) (-1)) ? 1 : len;
99 wc = replacement_char ();
101 /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
102 if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
107 char buf[MB_LEN_MAX * 2];
110 if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t) (-1) &&
111 (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t) (-1))
115 else if (wc < 0x20 || wc == 0x7f) {
128 fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
137 return (maxwidth - n);
140 static int pad (FILE * f, int col, int i)
145 snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
146 fprintf (f, fmt, "");
153 static void format_line (FILE * f, int ismacro,
154 const char *t1, const char *t2, const char *t3)
163 /* don't try to press string into one line with less than 40 characters.
164 The double paranthesis avoid a gcc warning, sigh ... */
165 if ((split = COLS < 40)) {
171 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
172 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
173 col = pad (f, m_strlen(t1), col_a);
177 if (!m_strcmp(Pager, "builtin"))
183 col += print_macro (f, col_b - col - 4, &t2);
184 if (m_strlen(t2) > col_b - col)
189 col += print_macro (f, col_b - col - 1, &t2);
193 col = pad (f, col, col_b);
196 print_macro (f, LONG_STRING, &t3);
204 t3 = vskipspaces(t3);
206 /* FIXME: this is completely wrong */
207 if ((n = m_strlen(t3)) > COLS - col) {
209 for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
215 print_macro (f, n, &t3);
218 if (m_strcmp(Pager, "builtin")) {
224 if (option (OPTMARKERS))
227 col = pad (f, n, col_b);
235 static void dump_menu (FILE * f, int menu)
237 struct keymap_t *map;
239 char buf[SHORT_STRING];
241 /* browse through the keymap table */
242 for (map = Keymaps[menu]; map; map = map->next) {
243 if (map->op != OP_NULL) {
244 km_expand_key (buf, sizeof (buf), map);
246 if (map->op == OP_MACRO) {
247 if (map->descr == NULL)
248 format_line (f, -1, buf, "macro", map->macro);
250 format_line (f, 1, buf, map->macro, map->descr);
253 b = help_lookupFunction (map->op, menu);
254 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
255 b ? _(HelpStrings[b->op]) :
256 _("ERROR: please report this bug"));
262 static int is_bound (struct keymap_t *map, int op)
264 for (; map; map = map->next)
270 static void dump_unbound (FILE * f,
271 struct binding_t *funcs,
272 struct keymap_t *map, struct keymap_t *aux)
276 for (i = 0; funcs[i].name; i++) {
277 if (!is_bound (map, funcs[i].op) &&
278 (!aux || !is_bound (aux, funcs[i].op)))
279 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
283 void mutt_help (int menu)
285 char t[_POSIX_PATH_MAX];
286 char buf[SHORT_STRING];
289 struct binding_t *funcs;
293 funcs = km_get_table (menu);
294 desc = mutt_getnamebyvalue (menu, Menus);
296 desc = _("<UNKNOWN>");
299 if ((f = safe_fopen (t, "w")) == NULL) {
305 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
306 fputs (_("\nGeneric bindings:\n\n"), f);
307 dump_menu (f, MENU_GENERIC);
310 fputs (_("\nUnbound functions:\n\n"), f);
312 dump_unbound (f, funcs, Keymaps[menu], NULL);
313 if (menu != MENU_PAGER)
314 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
318 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
321 (mutt_do_pager (buf, t,
322 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
323 == OP_REFORMAT_WINCH);