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