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