there is no real need for a lib-hash anymore. fold it.
[apps/madmutt.git] / help.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #define HELP_C
11
12 #include <lib-lib/lib-lib.h>
13
14 #include <lib-ui/curses.h>
15
16 #include "mutt.h"
17 #include "charset.h"
18 #include "keymap.h"
19 #include "pager.h"
20
21 static struct binding_t *help_lookupFunction (int op, int menu)
22 {
23   int i;
24   struct binding_t *map;
25
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]);
31   }
32
33   if ((map = km_get_table (menu))) {
34     for (i = 0; map[i].name; i++)
35       if (map[i].op == op)
36         return (&map[i]);
37   }
38
39   return (NULL);
40 }
41
42 void mutt_make_help (char *d, ssize_t dlen, char *txt, int menu, int op)
43 {
44   char buf[STRING];
45
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);
49   else
50     d[0] = 0;
51 }
52
53 char *mutt_compile_help (char *buf, ssize_t buflen, int menu,
54                          struct mapping_t *items)
55 {
56   int i;
57   ssize_t len;
58   char *pbuf = buf;
59
60   for (i = 0; items[i].name && buflen > 2; i++) {
61     if (i) {
62       *pbuf++ = ' ';
63       *pbuf++ = ' ';
64       buflen -= 2;
65     }
66     mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
67     len = m_strlen(pbuf);
68     pbuf += len;
69     buflen -= len;
70   }
71   return buf;
72 }
73
74 static int print_macro (FILE * f, int maxwidth, const char **macro)
75 {
76   int n = maxwidth;
77   wchar_t wc;
78   int w;
79   ssize_t k;
80   ssize_t len = m_strlen(*macro);
81   mbstate_t mbstate1, mbstate2;
82
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;
90     }
91     /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
92     if (iswprint(wc) && (w = wcwidth (wc)) >= 0) {
93       if (w > n)
94         break;
95       n -= w;
96       {
97         char buf[MB_LEN_MAX * 2];
98         ssize_t n1, n2;
99
100         if ((n1 = wcrtomb(buf, wc, &mbstate2)) != -1 &&
101             (n2 = wcrtomb(buf + n1, 0, &mbstate2)) != -1)
102           fputs (buf, f);
103       }
104     }
105     else if (wc < 0x20 || wc == 0x7f) {
106       if (2 > n)
107         break;
108       n -= 2;
109       if (wc == '\033')
110         fprintf (f, "\\e");
111       else if (wc == '\n')
112         fprintf (f, "\\n");
113       else if (wc == '\r')
114         fprintf (f, "\\r");
115       else if (wc == '\t')
116         fprintf (f, "\\t");
117       else
118         fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
119     }
120     else {
121       if (1 > n)
122         break;
123       n -= 1;
124       fprintf (f, "?");
125     }
126   }
127   return (maxwidth - n);
128 }
129
130 static int pad (FILE * f, int col, int i)
131 {
132   char fmt[8];
133
134   if (col < i) {
135     snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
136     fprintf (f, fmt, "");
137     return (i);
138   }
139   fputc (' ', f);
140   return (col + 1);
141 }
142
143 static void format_line (FILE * f, int ismacro,
144                          const char *t1, const char *t2, const char *t3)
145 {
146   int col;
147   int col_a, col_b;
148   int split;
149   int n;
150
151   fputs (t1, f);
152
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)) {
156     col_a = col = 0;
157     col_b = LONG_STRING;
158     fputc ('\n', f);
159   }
160   else {
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);
164   }
165
166   if (ismacro > 0) {
167     fputs ("_\010M ", f);
168     col += 2;
169
170     if (!split) {
171       col += print_macro (f, col_b - col - 4, &t2);
172       if (m_strlen(t2) > col_b - col)
173         t2 = "...";
174     }
175   }
176
177   col += print_macro (f, col_b - col - 1, &t2);
178   if (split)
179     fputc ('\n', f);
180   else
181     col = pad (f, col, col_b);
182
183   if (split) {
184     print_macro (f, LONG_STRING, &t3);
185     fputc ('\n', f);
186   }
187   else {
188     while (*t3) {
189       n = COLS - col;
190
191       if (ismacro >= 0) {
192         t3 = vskipspaces(t3);
193
194         /* FIXME: this is completely wrong */
195         if ((n = m_strlen(t3)) > COLS - col) {
196           n = COLS - col;
197           for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
198           if (col_a)
199             n = col_a;
200         }
201       }
202
203       print_macro (f, n, &t3);
204
205       if (*t3) {
206         n += col - COLS;
207         if (option (OPTMARKERS))
208           ++n;
209         col = pad (f, n, col_b);
210       }
211     }
212   }
213
214   fputc ('\n', f);
215 }
216
217 static void dump_menu (FILE * f, int menu)
218 {
219   struct keymap_t *map;
220   struct binding_t *b;
221   char buf[STRING];
222
223   /* browse through the keymap table */
224   for (map = Keymaps[menu]; map; map = map->next) {
225     if (map->op != OP_NULL) {
226       km_expand_key (buf, sizeof (buf), map);
227
228       if (map->op == OP_MACRO) {
229         if (map->descr == NULL)
230           format_line (f, -1, buf, "macro", map->macro);
231         else
232           format_line (f, 1, buf, map->macro, map->descr);
233       }
234       else {
235         b = help_lookupFunction (map->op, menu);
236         format_line (f, 0, buf, b ? b->name : "UNKNOWN",
237                      b ? _(HelpStrings[b->op]) :
238                      _("ERROR: please report this bug"));
239       }
240     }
241   }
242 }
243
244 static int is_bound (struct keymap_t *map, int op)
245 {
246   for (; map; map = map->next)
247     if (map->op == op)
248       return 1;
249   return 0;
250 }
251
252 static void dump_unbound (FILE * f,
253                           struct binding_t *funcs,
254                           struct keymap_t *map, struct keymap_t *aux)
255 {
256   int i;
257
258   for (i = 0; funcs[i].name; i++) {
259     if (!is_bound (map, funcs[i].op) &&
260         (!aux || !is_bound (aux, funcs[i].op)))
261       format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
262   }
263 }
264
265 void mutt_help (int menu)
266 {
267     char tmp[_POSIX_PATH_MAX];
268     char buf[STRING];
269     const char *desc;
270     FILE *f;
271     struct binding_t *funcs;
272
273     funcs = km_get_table (menu);
274     desc = mutt_getnamebyvalue (menu, Menus);
275     if (!desc)
276         desc = _("<UNKNOWN>");
277
278     do {
279         f = m_tempfile(tmp, sizeof(tmp), NONULL(MCore.tmpdir), NULL);
280         if (!f) {
281             mutt_perror(tmp);
282             return;
283         }
284
285         dump_menu(f, menu);
286         if (menu != MENU_EDITOR && menu != MENU_PAGER) {
287             fputs(_("\nGeneric bindings:\n\n"), f);
288             dump_menu(f, MENU_GENERIC);
289         }
290
291         fputs(_("\nUnbound functions:\n\n"), f);
292         if (funcs)
293             dump_unbound(f, funcs, Keymaps[menu], NULL);
294
295         if (menu != MENU_PAGER)
296             dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
297
298         m_fclose(&f);
299
300         snprintf(buf, sizeof (buf), _("Help for %s"), desc);
301     } while (mutt_pager(buf, tmp,
302                         M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
303                         NULL) == OP_REFORMAT_WINCH);
304 }