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