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