5d91776f1fad2d3671ccb70af63e8ea30d1a5c39
[apps/madmutt.git] / keymap.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000,2002 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 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mutt_menu.h"
16 #include "mutt_curses.h"
17 #include "keymap.h"
18 #include "mapping.h"
19 #include "mutt_crypt.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "functions.h"
26
27 struct mapping_t Menus[] = {
28   {"alias", MENU_ALIAS},
29   {"attach", MENU_ATTACH},
30   {"browser", MENU_FOLDER},
31   {"compose", MENU_COMPOSE},
32   {"editor", MENU_EDITOR},
33   {"index", MENU_MAIN},
34   {"pager", MENU_PAGER},
35   {"postpone", MENU_POST},
36   {"pgp", MENU_PGP},
37   {"smime", MENU_SMIME},
38
39 #ifdef HAVE_GPGME
40   {"key_select_pgp", MENU_KEY_SELECT_PGP},
41   {"key_select_smime", MENU_KEY_SELECT_SMIME},
42 #endif
43
44
45 #ifdef MIXMASTER
46   {"mix", MENU_MIX},
47 #endif
48
49
50   {"query", MENU_QUERY},
51   {"generic", MENU_GENERIC},
52   {NULL, 0}
53 };
54
55 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
56
57 static struct mapping_t KeyNames[] = {
58   {"<PageUp>", KEY_PPAGE},
59   {"<PageDown>", KEY_NPAGE},
60   {"<Up>", KEY_UP},
61   {"<Down>", KEY_DOWN},
62   {"<Right>", KEY_RIGHT},
63   {"<Left>", KEY_LEFT},
64   {"<Delete>", KEY_DC},
65   {"<BackSpace>", KEY_BACKSPACE},
66   {"<Insert>", KEY_IC},
67   {"<Home>", KEY_HOME},
68   {"<End>", KEY_END},
69 #ifdef KEY_ENTER
70   {"<Enter>", KEY_ENTER},
71 #endif
72   {"<Return>", M_ENTER_C},
73   {"<Esc>", '\033'},
74   {"<Tab>", '\t'},
75   {"<Space>", ' '},
76 #ifdef KEY_BTAB
77   {"<BackTab>", KEY_BTAB},
78 #endif
79   {NULL, 0}
80 };
81
82 /* contains the last key the user pressed */
83 int LastKey;
84
85 struct keymap_t *Keymaps[MENU_MAX];
86
87 static struct keymap_t *allocKeys (int len, keycode_t * keys)
88 {
89   struct keymap_t *p;
90
91   p = safe_calloc (1, sizeof (struct keymap_t));
92   p->len = len;
93   p->keys = safe_malloc (len * sizeof (keycode_t));
94   memcpy (p->keys, keys, len * sizeof (keycode_t));
95   return (p);
96 }
97
98 static int parse_fkey (char *s)
99 {
100   char *t;
101   int n = 0;
102
103   if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
104     return -1;
105
106   for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
107     n *= 10;
108     n += *t - '0';
109   }
110
111   if (*t != '>')
112     return -1;
113   else
114     return n;
115 }
116
117 /*
118  * This function parses the string <NNN> and uses the octal value as the key
119  * to bind.
120  */
121 static int parse_keycode (const char *s)
122 {
123   if (isdigit ((unsigned char) s[1]) &&
124       isdigit ((unsigned char) s[2]) &&
125       isdigit ((unsigned char) s[3]) && s[4] == '>') {
126     return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
127   }
128   return -1;
129 }
130
131 static int parsekeys (char *str, keycode_t * d, int max)
132 {
133   int n, len = max;
134   char buff[SHORT_STRING];
135   char c;
136   char *s, *t;
137
138   strfcpy (buff, str, sizeof (buff));
139   s = buff;
140
141   while (*s && len) {
142     *d = '\0';
143     if (*s == '<' && (t = strchr (s, '>'))) {
144       t++;
145       c = *t;
146       *t = '\0';
147
148       if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
149         s = t;
150         *d = n;
151       }
152       else if ((n = parse_fkey (s)) > 0) {
153         s = t;
154         *d = KEY_F (n);
155       }
156       else if ((n = parse_keycode (s)) > 0) {
157         s = t;
158         *d = n;
159       }
160
161       *t = c;
162     }
163
164     if (!*d) {
165       *d = (unsigned char) *s;
166       s++;
167     }
168     d++;
169     len--;
170   }
171
172   return (max - len);
173 }
174
175 /* insert a key sequence into the specified map.  the map is sorted by ASCII
176  * value (lowest to highest)
177  */
178 void km_bind (char *s, int menu, int op, char *macro, char *descr)
179 {
180   struct keymap_t *map, *tmp, *last = NULL, *next;
181   keycode_t buf[MAX_SEQ];
182   int len, pos = 0, lastpos = 0;
183
184   len = parsekeys (s, buf, MAX_SEQ);
185
186   map = allocKeys (len, buf);
187   map->op = op;
188   map->macro = safe_strdup (macro);
189   map->descr = safe_strdup (descr);
190
191   tmp = Keymaps[menu];
192
193   while (tmp) {
194     if (pos >= len || pos >= tmp->len) {
195       /* map and tmp match, but have different lengths, so overwrite */
196       do {
197         len = tmp->eq;
198         next = tmp->next;
199         FREE (&tmp->macro);
200         FREE (&tmp->keys);
201         FREE (&tmp->descr);
202         FREE (&tmp);
203         tmp = next;
204       }
205       while (tmp && len >= pos);
206       map->eq = len;
207       break;
208     }
209     else if (buf[pos] == tmp->keys[pos])
210       pos++;
211     else if (buf[pos] < tmp->keys[pos]) {
212       /* found location to insert between last and tmp */
213       map->eq = pos;
214       break;
215     }
216     else {                      /* buf[pos] > tmp->keys[pos] */
217
218       last = tmp;
219       lastpos = pos;
220       if (pos > tmp->eq)
221         pos = tmp->eq;
222       tmp = tmp->next;
223     }
224   }
225
226   map->next = tmp;
227   if (last) {
228     last->next = map;
229     last->eq = lastpos;
230   }
231   else
232     Keymaps[menu] = map;
233 }
234
235 void km_bindkey (char *s, int menu, int op)
236 {
237   km_bind (s, menu, op, NULL, NULL);
238 }
239
240 static int get_op (struct binding_t *bindings, const char *start, size_t len)
241 {
242   int i;
243
244   for (i = 0; bindings[i].name; i++) {
245     if (!ascii_strncasecmp (start, bindings[i].name, len) &&
246         mutt_strlen (bindings[i].name) == len)
247       return bindings[i].op;
248   }
249
250   return OP_NULL;
251 }
252
253 static char *get_func (struct binding_t *bindings, int op)
254 {
255   int i;
256
257   for (i = 0; bindings[i].name; i++) {
258     if (bindings[i].op == op) {
259       return bindings[i].name;
260     }
261   }
262
263   return NULL;
264 }
265
266 static void push_string (char *s)
267 {
268   char *pp, *p = s + mutt_strlen (s) - 1;
269   size_t l;
270   int i, op = OP_NULL;
271
272   while (p >= s) {
273     /* if we see something like "<PageUp>", look to see if it is a real
274        function name and return the corresponding value */
275     if (*p == '>') {
276       for (pp = p - 1; pp >= s && *pp != '<'; pp--);
277       if (pp >= s) {
278         if ((i = parse_fkey (pp)) > 0) {
279           mutt_ungetch (KEY_F (i), 0);
280           p = pp - 1;
281           continue;
282         }
283
284         l = p - pp + 1;
285         for (i = 0; KeyNames[i].name; i++) {
286           if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
287             break;
288         }
289         if (KeyNames[i].name) {
290           /* found a match */
291           mutt_ungetch (KeyNames[i].value, 0);
292           p = pp - 1;
293           continue;
294         }
295
296         /* See if it is a valid command
297          * skip the '<' and the '>' when comparing */
298         for (i = 0; Menus[i].name; i++) {
299           struct binding_t *binding = km_get_table (Menus[i].value);
300
301           if (binding) {
302             op = get_op (binding, pp + 1, l - 2);
303             if (op != OP_NULL)
304               break;
305           }
306         }
307
308         if (op != OP_NULL) {
309           mutt_ungetch (0, op);
310           p = pp - 1;
311           continue;
312         }
313       }
314     }
315     mutt_ungetch (*p--, 0);
316   }
317 }
318
319 static int retry_generic (int menu, keycode_t * keys, int keyslen,
320                           int lastkey)
321 {
322   if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
323     if (lastkey)
324       mutt_ungetch (lastkey, 0);
325     for (; keyslen; keyslen--)
326       mutt_ungetch (keys[keyslen - 1], 0);
327     return (km_dokey (MENU_GENERIC));
328   }
329   if (menu != MENU_EDITOR) {
330     /* probably a good idea to flush input here so we can abort macros */
331     mutt_flushinp ();
332   }
333   return OP_NULL;
334 }
335
336 /* return values:
337  *        >0                function to execute
338  *        OP_NULL                no function bound to key sequence
339  *        -1                error occured while reading input
340  */
341 int km_dokey (int menu)
342 {
343   event_t tmp;
344   struct keymap_t *map = Keymaps[menu];
345   int pos = 0;
346   int n = 0;
347   int i;
348
349
350   if (!map)
351     return (retry_generic (menu, NULL, 0, 0));
352
353   FOREVER {
354     /* ncurses doesn't return on resized screen when timeout is set to zero */
355     if (menu != MENU_EDITOR)
356       timeout ((Timeout > 0 ? Timeout : 60) * 1000);
357
358     tmp = mutt_getch ();
359
360     if (menu != MENU_EDITOR)
361       timeout (-1);             /* restore blocking operation */
362
363     LastKey = tmp.ch;
364     if (LastKey == -1)
365       return -1;
366
367     /* do we have an op already? */
368     if (tmp.op) {
369       char *func = NULL;
370       struct binding_t *bindings;
371
372       /* is this a valid op for this menu? */
373       if ((bindings = km_get_table (menu)) &&
374           (func = get_func (bindings, tmp.op)))
375         return tmp.op;
376
377       if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
378         return tmp.op;
379
380       if (menu != MENU_EDITOR && menu != MENU_PAGER) {
381         /* check generic menu */
382         bindings = OpGeneric;
383         if ((func = get_func (bindings, tmp.op)))
384           return tmp.op;
385       }
386
387       /* Sigh. Valid function but not in this context.
388        * Find the literal string and push it back */
389       for (i = 0; Menus[i].name; i++) {
390         bindings = km_get_table (Menus[i].value);
391         if (bindings) {
392           func = get_func (bindings, tmp.op);
393           if (func) {
394             /* careful not to feed the <..> as one token. otherwise 
395              * push_string() will push the bogus op right back! */
396             mutt_ungetch ('>', 0);
397             push_string (func);
398             mutt_ungetch ('<', 0);
399             break;
400           }
401         }
402       }
403       /* continue to chew */
404       if (func)
405         continue;
406     }
407
408     /* Nope. Business as usual */
409     while (LastKey > map->keys[pos]) {
410       if (pos > map->eq || !map->next)
411         return (retry_generic (menu, map->keys, pos, LastKey));
412       map = map->next;
413     }
414
415     if (LastKey != map->keys[pos])
416       return (retry_generic (menu, map->keys, pos, LastKey));
417
418     if (++pos == map->len) {
419
420       if (map->op != OP_MACRO)
421         return map->op;
422
423       if (n++ == 10) {
424         mutt_flushinp ();
425         mutt_error _("Macro loop detected.");
426
427         return -1;
428       }
429
430       push_string (map->macro);
431       map = Keymaps[menu];
432       pos = 0;
433     }
434   }
435
436   /* not reached */
437 }
438
439 static void create_bindings (struct binding_t *map, int menu)
440 {
441   int i;
442
443   for (i = 0; map[i].name; i++)
444     if (map[i].seq)
445       km_bindkey (map[i].seq, menu, map[i].op);
446 }
447
448 char *km_keyname (int c)
449 {
450   static char buf[10];
451   char *p;
452
453   if ((p = mutt_getnamebyvalue (c, KeyNames)))
454     return p;
455
456   if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
457     if (c < 0)
458       c += 256;
459
460     if (c < 128) {
461       buf[0] = '^';
462       buf[1] = (c + '@') & 0x7f;
463       buf[2] = 0;
464     }
465     else
466       snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
467   }
468   else if (c >= KEY_F0 && c < KEY_F (256))      /* this maximum is just a guess */
469     sprintf (buf, "<F%d>", c - KEY_F0);
470   else if (IsPrint (c))
471     snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
472   else
473     snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
474   return (buf);
475 }
476
477 int km_expand_key (char *s, size_t len, struct keymap_t *map)
478 {
479   size_t l;
480   int p = 0;
481
482   if (!map)
483     return (0);
484
485   FOREVER {
486     strfcpy (s, km_keyname (map->keys[p]), len);
487     len -= (l = mutt_strlen (s));
488
489     if (++p >= map->len || !len)
490       return (1);
491
492     s += l;
493   }
494
495   /* not reached */
496 }
497
498 struct keymap_t *km_find_func (int menu, int func)
499 {
500   struct keymap_t *map = Keymaps[menu];
501
502   for (; map; map = map->next)
503     if (map->op == func)
504       break;
505   return (map);
506 }
507
508 void km_init (void)
509 {
510   memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
511
512   create_bindings (OpAttach, MENU_ATTACH);
513   create_bindings (OpBrowser, MENU_FOLDER);
514   create_bindings (OpCompose, MENU_COMPOSE);
515   create_bindings (OpMain, MENU_MAIN);
516   create_bindings (OpPager, MENU_PAGER);
517   create_bindings (OpPost, MENU_POST);
518   create_bindings (OpQuery, MENU_QUERY);
519   create_bindings (OpAlias, MENU_ALIAS);
520
521
522   if ((WithCrypto & APPLICATION_PGP))
523     create_bindings (OpPgp, MENU_PGP);
524
525   if ((WithCrypto & APPLICATION_SMIME))
526     create_bindings (OpSmime, MENU_SMIME);
527
528 #ifdef CRYPT_BACKEND_GPGME
529   create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
530   create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
531 #endif
532
533 #ifdef MIXMASTER
534   create_bindings (OpMix, MENU_MIX);
535
536   km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
537   km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
538   km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
539 #endif
540
541   /* bindings for the line editor */
542   create_bindings (OpEditor, MENU_EDITOR);
543
544   km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
545   km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
546   km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
547   km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
548   km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
549   km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
550   km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
551   km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
552   km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
553
554   /* generic menu keymap */
555   create_bindings (OpGeneric, MENU_GENERIC);
556
557   km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
558   km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
559   km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
560   km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
561   km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
562   km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
563   km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
564   km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
565   km_bindkey ("1", MENU_GENERIC, OP_JUMP);
566   km_bindkey ("2", MENU_GENERIC, OP_JUMP);
567   km_bindkey ("3", MENU_GENERIC, OP_JUMP);
568   km_bindkey ("4", MENU_GENERIC, OP_JUMP);
569   km_bindkey ("5", MENU_GENERIC, OP_JUMP);
570   km_bindkey ("6", MENU_GENERIC, OP_JUMP);
571   km_bindkey ("7", MENU_GENERIC, OP_JUMP);
572   km_bindkey ("8", MENU_GENERIC, OP_JUMP);
573   km_bindkey ("9", MENU_GENERIC, OP_JUMP);
574
575   km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
576
577   /* Miscellaneous extra bindings */
578
579   km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
580   km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
581   km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
582   km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
583   km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
584   km_bindkey ("x", MENU_MAIN, OP_EXIT);
585
586   km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
587
588   km_bindkey ("x", MENU_PAGER, OP_EXIT);
589   km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
590   km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
591   km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
592   km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
593   km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
594   km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
595   km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
596   km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
597   km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
598   km_bindkey ("1", MENU_PAGER, OP_JUMP);
599   km_bindkey ("2", MENU_PAGER, OP_JUMP);
600   km_bindkey ("3", MENU_PAGER, OP_JUMP);
601   km_bindkey ("4", MENU_PAGER, OP_JUMP);
602   km_bindkey ("5", MENU_PAGER, OP_JUMP);
603   km_bindkey ("6", MENU_PAGER, OP_JUMP);
604   km_bindkey ("7", MENU_PAGER, OP_JUMP);
605   km_bindkey ("8", MENU_PAGER, OP_JUMP);
606   km_bindkey ("9", MENU_PAGER, OP_JUMP);
607
608   km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
609
610   km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
611   km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
612   km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
613
614   km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
615   km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
616
617   /* edit-to (default "t") hides generic tag-entry in Compose menu
618      This will bind tag-entry to  "T" in the Compose menu */
619   km_bindkey ("T", MENU_COMPOSE, OP_TAG);
620 }
621
622 void km_error_key (int menu)
623 {
624   char buf[SHORT_STRING];
625   struct keymap_t *key;
626
627   if (!(key = km_find_func (menu, OP_HELP)))
628     key = km_find_func (MENU_GENERIC, OP_HELP);
629
630   if (!(km_expand_key (buf, sizeof (buf), key))) {
631     mutt_error _("Key is not bound.");
632
633     return;
634   }
635
636   /* make sure the key is really the help key in this menu */
637   push_string (buf);
638   if (km_dokey (menu) != OP_HELP) {
639     mutt_error _("Key is not bound.");
640
641     return;
642   }
643
644   mutt_error (_("Key is not bound.  Press '%s' for help."), buf);
645   return;
646 }
647
648 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
649                      BUFFER * err)
650 {
651   int r = 0;
652
653   mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
654   if (MoreArgs (s)) {
655     strfcpy (err->data, _("push: too many arguments"), err->dsize);
656     r = -1;
657   }
658   else
659     push_string (buf->data);
660   return (r);
661 }
662
663 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
664 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
665                            BUFFER * err)
666 {
667   BUFFER buf;
668   int i = 0;
669   char *p, *q;
670
671   memset (&buf, 0, sizeof (buf));
672
673   /* menu name */
674   mutt_extract_token (&buf, s, 0);
675   p = buf.data;
676   if (MoreArgs (s)) {
677     while (i < maxmenus) {
678       q = strchr (p, ',');
679       if (q)
680         *q = '\0';
681
682       if ((menu[i] = mutt_check_menu (p)) == -1) {
683         snprintf (err->data, err->dsize, _("%s: no such menu"), p);
684         goto error;
685       }
686       ++i;
687       if (q)
688         p = q + 1;
689       else
690         break;
691     }
692     *nummenus = i;
693     /* key sequence */
694     mutt_extract_token (&buf, s, 0);
695
696     if (!*buf.data) {
697       strfcpy (err->data, _("null key sequence"), err->dsize);
698     }
699     else if (MoreArgs (s))
700       return (buf.data);
701   }
702   else {
703     strfcpy (err->data, _("too few arguments"), err->dsize);
704   }
705 error:
706   FREE (&buf.data);
707   return (NULL);
708 }
709
710 static int
711 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
712 {
713   int i;
714
715   for (i = 0; bindings[i].name; i++)
716     if (mutt_strcmp (func, bindings[i].name) == 0) {
717       km_bindkey (key, menu, bindings[i].op);
718       return (0);
719     }
720   return (-1);
721 }
722
723 struct binding_t *km_get_table (int menu)
724 {
725   switch (menu) {
726   case MENU_MAIN:
727     return OpMain;
728   case MENU_GENERIC:
729     return OpGeneric;
730   case MENU_COMPOSE:
731     return OpCompose;
732   case MENU_PAGER:
733     return OpPager;
734   case MENU_POST:
735     return OpPost;
736   case MENU_FOLDER:
737     return OpBrowser;
738   case MENU_ALIAS:
739     return OpAlias;
740   case MENU_ATTACH:
741     return OpAttach;
742   case MENU_EDITOR:
743     return OpEditor;
744   case MENU_QUERY:
745     return OpQuery;
746
747   case MENU_PGP:
748     return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
749
750 #ifdef CRYPT_BACKEND_GPGME
751   case MENU_KEY_SELECT_PGP:
752     return OpPgp;
753   case MENU_KEY_SELECT_SMIME:
754     return OpSmime;
755 #endif
756
757 #ifdef MIXMASTER
758   case MENU_MIX:
759     return OpMix;
760 #endif
761
762   }
763   return NULL;
764 }
765
766 /* bind menu-name '<key_sequence>' function-name */
767 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
768                      BUFFER * err)
769 {
770   struct binding_t *bindings = NULL;
771   char *key;
772   int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
773     0, nummenus, i;
774
775   if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
776                            &nummenus, err)) == NULL)
777     return (-1);
778
779   /* function to execute */
780   mutt_extract_token (buf, s, 0);
781   if (MoreArgs (s)) {
782     strfcpy (err->data, _("bind: too many arguments"), err->dsize);
783     r = -1;
784   }
785   else if (ascii_strcasecmp ("noop", buf->data) == 0) {
786     for (i = 0; i < nummenus; ++i) {
787       km_bindkey (key, menu[i], OP_NULL);       /* the `unbind' command */
788     }
789   }
790   else {
791     for (i = 0; i < nummenus; ++i) {
792       /* First check the "generic" list of commands */
793       if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
794           menu[i] == MENU_GENERIC ||
795           try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
796         /* Now check the menu-specific list of commands (if they exist) */
797         bindings = km_get_table (menu[i]);
798         if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
799           snprintf (err->data, err->dsize, _("%s: no such function in map"),
800                     buf->data);
801           r = -1;
802         }
803       }
804     }
805   }
806   FREE (&key);
807   return (r);
808 }
809
810 /* macro <menu> <key> <macro> <description> */
811 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
812                       BUFFER * err)
813 {
814   int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
815     -1, nummenus, i;
816   char *seq = NULL;
817   char *key;
818
819   if ((key =
820        parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
821                      err)) == NULL)
822     return (-1);
823
824   mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
825   /* make sure the macro sequence is not an empty string */
826   if (!*buf->data) {
827     strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
828   }
829   else {
830     if (MoreArgs (s)) {
831       seq = safe_strdup (buf->data);
832       mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
833
834       if (MoreArgs (s)) {
835         strfcpy (err->data, _("macro: too many arguments"), err->dsize);
836       }
837       else {
838         for (i = 0; i < nummenus; ++i) {
839           km_bind (key, menu[i], OP_MACRO, seq, buf->data);
840           r = 0;
841         }
842       }
843
844       FREE (&seq);
845     }
846     else {
847       for (i = 0; i < nummenus; ++i) {
848         km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
849         r = 0;
850       }
851     }
852   }
853   FREE (&key);
854   return (r);
855 }
856
857 /* exec function-name */
858 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
859                      BUFFER * err)
860 {
861   int ops[128];
862   int nops = 0;
863   struct binding_t *bindings = NULL;
864   char *function;
865
866   if (!MoreArgs (s)) {
867     strfcpy (err->data, _("exec: no arguments"), err->dsize);
868     return (-1);
869   }
870
871   do {
872     mutt_extract_token (buf, s, 0);
873     function = buf->data;
874
875     if ((bindings = km_get_table (CurrentMenu)) == NULL
876         && CurrentMenu != MENU_PAGER)
877       bindings = OpGeneric;
878
879     ops[nops] = get_op (bindings, function, mutt_strlen (function));
880     if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
881       ops[nops] = get_op (OpGeneric, function, mutt_strlen (function));
882
883     if (ops[nops] == OP_NULL) {
884       mutt_flushinp ();
885       mutt_error (_("%s: no such function"), function);
886       return (-1);
887     }
888     nops++;
889   }
890   while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
891
892   while (nops)
893     mutt_ungetch (0, ops[--nops]);
894
895   return 0;
896 }
897
898 /*
899  * prompts the user to enter a keystroke, and displays the octal value back
900  * to the user.
901  */
902 void mutt_what_key (void)
903 {
904   int ch;
905
906   mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
907   do {
908     ch = getch ();
909     if (ch != ERR && ch != ctrl ('G')) {
910       mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
911                     km_keyname (ch), ch, ch);
912     }
913   }
914   while (ch != ERR && ch != ctrl ('G'));
915
916   mutt_flushinp ();
917 }