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