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