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