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