Rocco Rutte:
[apps/madmutt.git] / help.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */ 
18
19 #define HELP_C
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "mutt.h"
26 #include "mutt_curses.h"
27 #include "keymap.h"
28 #include "pager.h"
29 #include "mapping.h"
30
31 #include <ctype.h>
32 #include <string.h>
33
34 static struct binding_t *help_lookupFunction (int op, int menu)
35 {
36   int i;
37   struct binding_t *map;
38
39   if (menu != MENU_PAGER)
40   {
41     /* first look in the generic map for the function */
42     for (i = 0; OpGeneric[i].name; i++)
43       if (OpGeneric[i].op == op)
44         return (&OpGeneric[i]);    
45   }
46
47   if ((map = km_get_table(menu)))
48   {
49     for (i = 0; map[i].name; i++)
50       if (map[i].op == op)
51         return (&map[i]);
52   }
53   
54   return (NULL);
55 }
56
57 void mutt_make_help (char *d, size_t dlen, char *txt, int menu, int op)
58 {
59   char buf[SHORT_STRING];
60
61   if (km_expand_key (buf, sizeof (buf), km_find_func (menu, op)) ||
62       km_expand_key (buf, sizeof (buf), km_find_func (MENU_GENERIC, op)))
63     snprintf (d, dlen, "%s:%s", buf, txt);
64   else
65     d[0] = 0;
66 }
67
68 char *
69 mutt_compile_help (char *buf, size_t buflen, int menu, struct mapping_t *items)
70 {
71   int i;
72   size_t len;
73   char *pbuf = buf;
74   
75   for (i = 0; items[i].name && buflen > 2; i++)
76   {
77     if (i)
78     {
79       *pbuf++ = ' ';
80       *pbuf++ = ' ';
81       buflen -= 2;
82     }
83     mutt_make_help (pbuf, buflen, _(items[i].name), menu, items[i].value);
84     len = mutt_strlen (pbuf);
85     pbuf += len;
86     buflen -= len;
87   }
88   return buf;
89 }
90
91 static int print_macro (FILE *f, int maxwidth, const char **macro)
92 {
93   int n = maxwidth;
94   wchar_t wc;
95   int w;
96   size_t k;
97   size_t len = mutt_strlen (*macro);
98   mbstate_t mbstate1, mbstate2;
99
100   memset (&mbstate1, 0, sizeof (mbstate1));
101   memset (&mbstate2, 0, sizeof (mbstate2));
102   for (; len && (k = mbrtowc (&wc, *macro, len, &mbstate1)); *macro += k, len -= k)
103   {
104     if (k == (size_t)(-1) || k == (size_t)(-2))
105     {
106       k = (k == (size_t)(-1)) ? 1 : len;
107       wc = replacement_char ();
108     }
109     /* glibc-2.1.3's wcwidth() returns 1 for unprintable chars! */
110     if (IsWPrint (wc) && (w = wcwidth (wc)) >= 0)
111     {
112       if (w > n)
113         break;
114       n -= w;
115       {
116         char buf[MB_LEN_MAX*2];
117         size_t n1, n2;
118         if ((n1 = wcrtomb (buf, wc, &mbstate2)) != (size_t)(-1) &&
119             (n2 = wcrtomb (buf + n1, 0, &mbstate2)) != (size_t)(-1))
120           fputs (buf, f);
121       }
122     }
123     else if (wc < 0x20 || wc == 0x7f)
124     {
125       if (2 > n)
126         break;
127       n -= 2;
128       if (wc == '\033')
129         fprintf (f, "\\e");
130       else if (wc == '\n')
131         fprintf (f, "\\n");
132       else if (wc == '\r')
133         fprintf (f, "\\r");
134       else if (wc == '\t')
135         fprintf (f, "\\t");
136       else
137         fprintf (f, "^%c", (char)((wc + '@') & 0x7f));
138     }
139     else
140     {
141       if (1 > n)
142         break;
143       n -= 1;
144       fprintf (f, "?");
145     }
146   }
147   return (maxwidth - n);
148 }
149
150 static int pad (FILE *f, int col, int i)
151 {
152   char fmt[8];
153
154   if (col < i)
155   {
156     snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
157     fprintf (f, fmt, "");
158     return (i);
159   }
160   fputc (' ', f);
161   return (col + 1);
162 }
163
164 static void format_line (FILE *f, int ismacro,
165                          const char *t1, const char *t2, const char *t3)
166 {
167   int col;
168   int col_a, col_b;
169   int split;
170   int n;
171
172   fputs (t1, f);
173
174   /* don't try to press string into one line with less than 40 characters.
175      The double paranthesis avoid a gcc warning, sigh ... */
176   if ((split = COLS < 40))
177   {
178     col_a = col = 0;
179     col_b = LONG_STRING;
180     fputc ('\n', f);
181   }
182   else
183   {
184     col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
185     col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
186     col = pad (f, mutt_strlen(t1), col_a);
187   }
188
189   if (ismacro > 0)
190   {
191     if (!mutt_strcmp (Pager, "builtin"))
192       fputs ("_\010", f);
193     fputs ("M ", f);
194     col += 2;
195
196     if (!split)
197     {
198       col += print_macro (f, col_b - col - 4, &t2);
199       if (mutt_strlen (t2) > col_b - col)
200         t2 = "...";
201     }
202   }
203
204   col += print_macro (f, col_b - col - 1, &t2);
205   if (split)
206     fputc ('\n', f);
207   else
208     col = pad (f, col, col_b);
209
210   if (split)
211   {
212     print_macro (f, LONG_STRING, &t3);
213     fputc ('\n', f);
214   }
215   else
216   {
217     while (*t3)
218     {
219       n = COLS - col;
220
221       if (ismacro >= 0)
222       {
223         SKIPWS(t3);
224
225         /* FIXME: this is completely wrong */
226         if ((n = mutt_strlen (t3)) > COLS - col)
227         {
228           n = COLS - col;
229           for (col_a = n; col_a > 0 && t3[col_a] != ' '; col_a--) ;
230           if (col_a)
231             n = col_a;
232         }
233       }
234
235       print_macro (f, n, &t3);
236
237       if (*t3)
238       {
239         if (mutt_strcmp (Pager, "builtin"))
240         {
241           fputc ('\n', f);
242           n = 0;
243         }
244         else
245         {
246           n += col - COLS;
247           if (option (OPTMARKERS))
248             ++n;
249         }
250         col = pad (f, n, col_b);
251       }
252     }
253   }
254
255   fputc ('\n', f);
256 }
257
258 static void dump_menu (FILE *f, int menu)
259 {
260   struct keymap_t *map;
261   struct binding_t *b;
262   char buf[SHORT_STRING];
263
264   /* browse through the keymap table */
265   for (map = Keymaps[menu]; map; map = map->next)
266   {
267     if (map->op != OP_NULL)
268     {
269       km_expand_key (buf, sizeof (buf), map);
270
271       if (map->op == OP_MACRO)
272       {
273         if (map->descr == NULL)
274           format_line (f, -1, buf, "macro", map->macro);
275         else
276           format_line (f, 1, buf, map->macro, map->descr);
277       }
278       else
279       {
280         b = help_lookupFunction (map->op, menu);
281         format_line (f, 0, buf, b ? b->name : "UNKNOWN",
282               b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
283       }
284     }
285   }
286 }
287
288 static int is_bound (struct keymap_t *map, int op)
289 {
290   for (; map; map = map->next)
291     if (map->op == op)
292       return 1;
293   return 0;
294 }
295
296 static void dump_unbound (FILE *f,
297                           struct binding_t *funcs,
298                           struct keymap_t *map,
299                           struct keymap_t *aux)
300 {
301   int i;
302
303   for (i = 0; funcs[i].name; i++)
304   {
305     if (! is_bound (map, funcs[i].op) &&
306         (!aux || ! is_bound (aux, funcs[i].op)))
307       format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
308   }
309 }
310
311 void mutt_help (int menu)
312 {
313   char t[_POSIX_PATH_MAX];
314   char buf[SHORT_STRING];
315   char *desc;
316   FILE *f;
317   struct binding_t *funcs;
318
319   mutt_mktemp (t);
320
321   funcs = km_get_table (menu);
322   desc = mutt_getnamebyvalue (menu, Menus);
323   if (!desc)
324     desc = _("<UNKNOWN>");
325   
326   do {
327     if ((f = safe_fopen (t, "w")) == NULL)
328     {
329       mutt_perror (t);
330       return;
331     }
332   
333     dump_menu (f, menu);
334     if (menu != MENU_EDITOR && menu != MENU_PAGER)
335     {
336       fputs (_("\nGeneric bindings:\n\n"), f);
337       dump_menu (f, MENU_GENERIC);
338     }
339   
340     fputs (_("\nUnbound functions:\n\n"), f);
341     if (funcs)
342       dump_unbound (f, funcs, Keymaps[menu], NULL);
343     if (menu != MENU_PAGER)
344       dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
345   
346     fclose (f);
347   
348     snprintf (buf, sizeof (buf), _("Help for %s"), desc);
349   }
350   while
351     (mutt_do_pager (buf, t,
352                     M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP,
353                     NULL)
354      == OP_REFORMAT_WINCH);
355 }