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