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