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