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