configure fixes.
[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 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <wctype.h>
17 #include <wchar.h>
18 #include <ctype.h>
19 #include <string.h>
20
21 #include <lib-lib/macros.h>
22 #include <lib-lib/file.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/mapping.h>
25
26 #include <lib-ui/curses.h>
27
28 #include "mutt.h"
29 #include "charset.h"
30 #include "keymap.h"
31 #include "pager.h"
32
33 static struct binding_t *help_lookupFunction (int op, int menu)
34 {
35   int i;
36   struct binding_t *map;
37
38   if (menu != MENU_PAGER) {
39     /* first look in the generic map for the function */
40     for (i = 0; OpGeneric[i].name; i++)
41       if (OpGeneric[i].op == op)
42         return (&OpGeneric[i]);
43   }
44
45   if ((map = km_get_table (menu))) {
46     for (i = 0; map[i].name; i++)
47       if (map[i].op == op)
48         return (&map[i]);
49   }
50
51   return (NULL);
52 }
53
54 void mutt_make_help (char *d, ssize_t dlen, char *txt, int menu, int op)
55 {
56   char buf[SHORT_STRING];
57
58   if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
59       km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
60     snprintf (d, dlen, "%s:%s", buf, txt);
61   else
62     d[0] = 0;
63 }
64
65 char *mutt_compile_help (char *buf, ssize_t buflen, int menu,
66                          struct mapping_t *items)
67 {
68   int i;
69   ssize_t len;
70   char *pbuf = buf;
71
72   for (i = 0; items[i].name && buflen > 2; i++) {
73     if (i) {
74       *pbuf++ = ' ';
75       *pbuf++ = ' ';
76       buflen -= 2;
77     }
78     mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
79     len = m_strlen(pbuf);
80     pbuf += len;
81     buflen -= len;
82   }
83   return buf;
84 }
85
86 static int print_macro (FILE * f, int maxwidth, const char **macro)
87 {
88   int n = maxwidth;
89   wchar_t wc;
90   int w;
91   ssize_t k;
92   ssize_t len = m_strlen(*macro);
93   mbstate_t mbstate1, mbstate2;
94
95   p_clear(&mbstate1, 1);
96   p_clear(&mbstate2, 1);
97   for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1));
98        *macro += k, len -= k) {
99     if (k == -1 || k == -2) {
100       k = (k == -1) ? 1 : len;
101       wc = CharsetReplacement;
102     }
103     /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
104     if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0) {
105       if (w > n)
106         break;
107       n -= w;
108       {
109         char buf[MB_LEN_MAX * 2];
110         ssize_t n1, n2;
111
112         if ((n1 = wcrtomb(buf, wc, &mbstate2)) != -1 &&
113             (n2 = wcrtomb(buf + n1, 0, &mbstate2)) != -1)
114           fputs (buf, f);
115       }
116     }
117     else if (wc < 0x20 || wc == 0x7f) {
118       if (2 > n)
119         break;
120       n -= 2;
121       if (wc == '\033')
122         fprintf (f, "\\e");
123       else if (wc == '\n')
124         fprintf (f, "\\n");
125       else if (wc == '\r')
126         fprintf (f, "\\r");
127       else if (wc == '\t')
128         fprintf (f, "\\t");
129       else
130         fprintf (f, "^%c", (char) ((wc + '@') & 0x7f));
131     }
132     else {
133       if (1 > n)
134         break;
135       n -= 1;
136       fprintf (f, "?");
137     }
138   }
139   return (maxwidth - n);
140 }
141
142 static int pad (FILE * f, int col, int i)
143 {
144   char fmt[8];
145
146   if (col < i) {
147     snprintf (fmt, sizeof (fmt), "%%-%ds", i - col);
148     fprintf (f, fmt, "");
149     return (i);
150   }
151   fputc (' ', f);
152   return (col + 1);
153 }
154
155 static void format_line (FILE * f, int ismacro,
156                          const char *t1, const char *t2, const char *t3)
157 {
158   int col;
159   int col_a, col_b;
160   int split;
161   int n;
162
163   fputs (t1, f);
164
165   /* don't try to press string into one line with less than 40 characters.
166      The double paranthesis avoid a gcc warning, sigh ... */
167   if ((split = COLS < 40)) {
168     col_a = col = 0;
169     col_b = LONG_STRING;
170     fputc ('\n', f);
171   }
172   else {
173     col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
174     col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
175     col = pad (f, m_strlen(t1), col_a);
176   }
177
178   if (ismacro > 0) {
179     if (!m_strcmp(Pager, "builtin"))
180       fputs ("_\010", f);
181     fputs ("M ", f);
182     col += 2;
183
184     if (!split) {
185       col += print_macro (f, col_b - col - 4, &t2);
186       if (m_strlen(t2) > col_b - col)
187         t2 = "...";
188     }
189   }
190
191   col += print_macro (f, col_b - col - 1, &t2);
192   if (split)
193     fputc ('\n', f);
194   else
195     col = pad (f, col, col_b);
196
197   if (split) {
198     print_macro (f, LONG_STRING, &t3);
199     fputc ('\n', f);
200   }
201   else {
202     while (*t3) {
203       n = COLS - col;
204
205       if (ismacro >= 0) {
206         t3 = vskipspaces(t3);
207
208         /* FIXME: this is completely wrong */
209         if ((n = m_strlen(t3)) > COLS - col) {
210           n = COLS - col;
211           for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--);
212           if (col_a)
213             n = col_a;
214         }
215       }
216
217       print_macro (f, n, &t3);
218
219       if (*t3) {
220         if (m_strcmp(Pager, "builtin")) {
221           fputc ('\n', f);
222           n = 0;
223         }
224         else {
225           n += col - COLS;
226           if (option (OPTMARKERS))
227             ++n;
228         }
229         col = pad (f, n, col_b);
230       }
231     }
232   }
233
234   fputc ('\n', f);
235 }
236
237 static void dump_menu (FILE * f, int menu)
238 {
239   struct keymap_t *map;
240   struct binding_t *b;
241   char buf[SHORT_STRING];
242
243   /* browse through the keymap table */
244   for (map = Keymaps[menu]; map; map = map->next) {
245     if (map->op != OP_NULL) {
246       km_expand_key (buf, sizeof (buf), map);
247
248       if (map->op == OP_MACRO) {
249         if (map->descr == NULL)
250           format_line (f, -1, buf, "macro", map->macro);
251         else
252           format_line (f, 1, buf, map->macro, map->descr);
253       }
254       else {
255         b = help_lookupFunction (map->op, menu);
256         format_line (f, 0, buf, b ? b->name : "UNKNOWN",
257                      b ? _(HelpStrings[b->op]) :
258                      _("ERROR: please report this bug"));
259       }
260     }
261   }
262 }
263
264 static int is_bound (struct keymap_t *map, int op)
265 {
266   for (; map; map = map->next)
267     if (map->op == op)
268       return 1;
269   return 0;
270 }
271
272 static void dump_unbound (FILE * f,
273                           struct binding_t *funcs,
274                           struct keymap_t *map, struct keymap_t *aux)
275 {
276   int i;
277
278   for (i = 0; funcs[i].name; i++) {
279     if (!is_bound (map, funcs[i].op) &&
280         (!aux || !is_bound (aux, funcs[i].op)))
281       format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
282   }
283 }
284
285 void mutt_help (int menu)
286 {
287   char t[_POSIX_PATH_MAX];
288   char buf[SHORT_STRING];
289   const char *desc;
290   FILE *f;
291   struct binding_t *funcs;
292
293   mutt_mktemp (t);
294
295   funcs = km_get_table (menu);
296   desc = mutt_getnamebyvalue (menu, Menus);
297   if (!desc)
298     desc = _("<UNKNOWN>");
299
300   do {
301     if ((f = safe_fopen (t, "w")) == NULL) {
302       mutt_perror (t);
303       return;
304     }
305
306     dump_menu (f, menu);
307     if (menu != MENU_EDITOR && menu != MENU_PAGER) {
308       fputs (_("\nGeneric bindings:\n\n"), f);
309       dump_menu (f, MENU_GENERIC);
310     }
311
312     fputs (_("\nUnbound functions:\n\n"), f);
313     if (funcs)
314       dump_unbound (f, funcs, Keymaps[menu], NULL);
315     if (menu != MENU_PAGER)
316       dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
317
318     fclose (f);
319
320     snprintf (buf, sizeof (buf), _("Help for %s"), desc);
321   }
322   while
323     (mutt_do_pager (buf, t,
324                     M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP, NULL)
325      == OP_REFORMAT_WINCH);
326 }