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