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