1 /*
2 * Copyright (C) 1996-2000,2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
help_lookupFunction(int op,int menu)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
mutt_make_help(char * d,size_t dlen,char * txt,int menu,int op)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 *
mutt_compile_help(char * buf,size_t buflen,int menu,struct mapping_t * items)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
print_macro(FILE * f,int maxwidth,const char ** macro)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
get_wrapped_width(const char * t,size_t wid)150 static int get_wrapped_width (const char *t, size_t wid)
151 {
152 wchar_t wc;
153 size_t k;
154 size_t m, n;
155 size_t len = mutt_strlen (t);
156 const char *s = t;
157 mbstate_t mbstate;
158
159 memset (&mbstate, 0, sizeof (mbstate));
160 for (m = wid, n = 0;
161 len && (k = mbrtowc (&wc, s, len, &mbstate)) && (n <= wid);
162 s += k, len -= k)
163 {
164 if (*s == ' ')
165 m = n;
166 if (k == (size_t)(-1) || k == (size_t)(-2))
167 {
168 k = (k == (size_t)(-1)) ? 1 : len;
169 wc = replacement_char ();
170 }
171 if (!IsWPrint (wc))
172 wc = '?';
173 n += wcwidth (wc);
174 }
175 if (n > wid)
176 n = m;
177 else
178 n = wid;
179 return n;
180 }
181
pad(FILE * f,int col,int i)182 static int pad (FILE *f, int col, int i)
183 {
184 char fmt[8];
185
186 if (col < i)
187 {
188 snprintf (fmt, sizeof(fmt), "%%-%ds", i - col);
189 fprintf (f, fmt, "");
190 return (i);
191 }
192 fputc (' ', f);
193 return (col + 1);
194 }
195
format_line(FILE * f,int ismacro,const char * t1,const char * t2,const char * t3)196 static void format_line (FILE *f, int ismacro,
197 const char *t1, const char *t2, const char *t3)
198 {
199 int col;
200 int col_a, col_b;
201 int split;
202 int n;
203
204 fputs (t1, f);
205
206 /* don't try to press string into one line with less than 40 characters.
207 The double paranthesis avoid a gcc warning, sigh ... */
208 if ((split = COLS < 40))
209 {
210 col_a = col = 0;
211 col_b = LONG_STRING;
212 fputc ('\n', f);
213 }
214 else
215 {
216 col_a = COLS > 83 ? (COLS - 32) >> 2 : 12;
217 col_b = COLS > 49 ? (COLS - 10) >> 1 : 19;
218 col = pad (f, mutt_strwidth(t1), col_a);
219 }
220
221 if (ismacro > 0)
222 {
223 if (!mutt_strcmp (Pager, "builtin"))
224 fputs ("_\010", f);
225 fputs ("M ", f);
226 col += 2;
227
228 if (!split)
229 {
230 col += print_macro (f, col_b - col - 4, &t2);
231 if (mutt_strwidth (t2) > col_b - col)
232 t2 = "...";
233 }
234 }
235
236 col += print_macro (f, col_b - col - 1, &t2);
237 if (split)
238 fputc ('\n', f);
239 else
240 col = pad (f, col, col_b);
241
242 if (split)
243 {
244 print_macro (f, LONG_STRING, &t3);
245 fputc ('\n', f);
246 }
247 else
248 {
249 while (*t3)
250 {
251 n = COLS - col;
252
253 if (ismacro >= 0)
254 {
255 SKIPWS(t3);
256 n = get_wrapped_width (t3, n);
257 }
258
259 n = print_macro (f, n, &t3);
260
261 if (*t3)
262 {
263 if (mutt_strcmp (Pager, "builtin"))
264 {
265 fputc ('\n', f);
266 n = 0;
267 }
268 else
269 {
270 n += col - COLS;
271 if (option (OPTMARKERS))
272 ++n;
273 }
274 col = pad (f, n, col_b);
275 }
276 }
277 }
278
279 fputc ('\n', f);
280 }
281
dump_menu(FILE * f,int menu)282 static void dump_menu (FILE *f, int menu)
283 {
284 struct keymap_t *map;
285 struct binding_t *b;
286 char buf[SHORT_STRING];
287
288 /* browse through the keymap table */
289 for (map = Keymaps[menu]; map; map = map->next)
290 {
291 if (map->op != OP_NULL)
292 {
293 km_expand_key (buf, sizeof (buf), map);
294
295 if (map->op == OP_MACRO)
296 {
297 if (map->descr == NULL)
298 format_line (f, -1, buf, "macro", map->macro);
299 else
300 format_line (f, 1, buf, map->macro, map->descr);
301 }
302 else
303 {
304 b = help_lookupFunction (map->op, menu);
305 format_line (f, 0, buf, b ? b->name : "UNKNOWN",
306 b ? _(HelpStrings[b->op]) : _("ERROR: please report this bug"));
307 }
308 }
309 }
310 }
311
is_bound(struct keymap_t * map,int op)312 static int is_bound (struct keymap_t *map, int op)
313 {
314 for (; map; map = map->next)
315 if (map->op == op)
316 return 1;
317 return 0;
318 }
319
dump_unbound(FILE * f,struct binding_t * funcs,struct keymap_t * map,struct keymap_t * aux)320 static void dump_unbound (FILE *f,
321 struct binding_t *funcs,
322 struct keymap_t *map,
323 struct keymap_t *aux)
324 {
325 int i;
326
327 for (i = 0; funcs[i].name; i++)
328 {
329 if (! is_bound (map, funcs[i].op) &&
330 (!aux || ! is_bound (aux, funcs[i].op)))
331 format_line (f, 0, funcs[i].name, "", _(HelpStrings[funcs[i].op]));
332 }
333 }
334
mutt_help(int menu)335 void mutt_help (int menu)
336 {
337 char t[_POSIX_PATH_MAX];
338 char buf[SHORT_STRING];
339 char *desc;
340 FILE *f;
341 struct binding_t *funcs;
342
343 mutt_mktemp (t, sizeof (t));
344
345 funcs = km_get_table (menu);
346 desc = mutt_getnamebyvalue (menu, Menus);
347 if (!desc)
348 desc = _("<UNKNOWN>");
349
350 do {
351 if ((f = safe_fopen (t, "w")) == NULL)
352 {
353 mutt_perror (t);
354 return;
355 }
356
357 dump_menu (f, menu);
358 if (menu != MENU_EDITOR && menu != MENU_PAGER)
359 {
360 fputs (_("\nGeneric bindings:\n\n"), f);
361 dump_menu (f, MENU_GENERIC);
362 }
363
364 fputs (_("\nUnbound functions:\n\n"), f);
365 if (funcs)
366 dump_unbound (f, funcs, Keymaps[menu], NULL);
367 if (menu != MENU_PAGER)
368 dump_unbound (f, OpGeneric, Keymaps[MENU_GENERIC], Keymaps[menu]);
369
370 safe_fclose (&f);
371
372 snprintf (buf, sizeof (buf), _("Help for %s"), desc);
373 }
374 while
375 (mutt_do_pager (buf, t,
376 M_PAGER_RETWINCH | M_PAGER_MARKER | M_PAGER_NSKIP | M_PAGER_NOWRAP,
377 NULL)
378 == OP_REFORMAT_WINCH);
379 }
380