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