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