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