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