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 "buffer.h"
16 #include "ascii.h"
17 #include "mutt_menu.h"
18 #include "mutt_curses.h"
19 #include "keymap.h"
20 #include "mapping.h"
21 #include "mutt_crypt.h"
22
23 #include "lib/mem.h"
24 #include "lib/intl.h"
25 #include "lib/str.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "functions.h"
32
33 struct mapping_t Menus[] = {
34   {"alias", MENU_ALIAS},
35   {"attach", MENU_ATTACH},
36   {"browser", MENU_FOLDER},
37   {"compose", MENU_COMPOSE},
38   {"editor", MENU_EDITOR},
39   {"index", MENU_MAIN},
40   {"pager", MENU_PAGER},
41   {"postpone", MENU_POST},
42   {"pgp", MENU_PGP},
43   {"smime", MENU_SMIME},
44
45 #ifdef HAVE_GPGME
46   {"key_select_pgp", MENU_KEY_SELECT_PGP},
47   {"key_select_smime", MENU_KEY_SELECT_SMIME},
48 #endif
49
50
51 #ifdef MIXMASTER
52   {"mix", MENU_MIX},
53 #endif
54
55
56   {"query", MENU_QUERY},
57   {"generic", MENU_GENERIC},
58   {NULL, 0}
59 };
60
61 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
62
63 static struct mapping_t KeyNames[] = {
64   {"<PageUp>", KEY_PPAGE},
65   {"<PageDown>", KEY_NPAGE},
66   {"<Up>", KEY_UP},
67   {"<Down>", KEY_DOWN},
68   {"<Right>", KEY_RIGHT},
69   {"<Left>", KEY_LEFT},
70   {"<Delete>", KEY_DC},
71   {"<BackSpace>", KEY_BACKSPACE},
72   {"<Insert>", KEY_IC},
73   {"<Home>", KEY_HOME},
74   {"<End>", KEY_END},
75 #ifdef KEY_ENTER
76   {"<Enter>", KEY_ENTER},
77 #endif
78   {"<Return>", M_ENTER_C},
79   {"<Esc>", '\033'},
80   {"<Tab>", '\t'},
81   {"<Space>", ' '},
82 #ifdef KEY_BTAB
83   {"<BackTab>", KEY_BTAB},
84 #endif
85   {NULL, 0}
86 };
87
88 /* contains the last key the user pressed */
89 int LastKey;
90
91 struct keymap_t *Keymaps[MENU_MAX];
92
93 static struct keymap_t *allocKeys (int len, keycode_t * keys)
94 {
95   struct keymap_t *p;
96
97   p = mem_calloc (1, sizeof (struct keymap_t));
98   p->len = len;
99   p->keys = mem_malloc (len * sizeof (keycode_t));
100   memcpy (p->keys, keys, len * sizeof (keycode_t));
101   return (p);
102 }
103
104 static int parse_fkey (char *s)
105 {
106   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 (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   strfcpy (buff, str, sizeof (buff));
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 (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 = str_dup (macro);
195   map->descr = str_dup (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         mem_free (&tmp->macro);
206         mem_free (&tmp->keys);
207         mem_free (&tmp->descr);
208         mem_free (&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 (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, size_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         str_len (bindings[i].name) == len)
253       return bindings[i].op;
254   }
255
256   return OP_NULL;
257 }
258
259 static 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 (char *s)
273 {
274   char *pp, *p = s + str_len (s) - 1;
275   size_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 (*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   FOREVER {
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       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 char *km_keyname (int c)
455 {
456   static char buf[10];
457   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 (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   FOREVER {
492     strfcpy (s, km_keyname (map->keys[p]), len);
493     len -= (l = str_len (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   memset (Keymaps, 0, sizeof (struct keymap_t *) * 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   if ((WithCrypto & APPLICATION_PGP))
529     create_bindings (OpPgp, MENU_PGP);
530
531   if ((WithCrypto & APPLICATION_SMIME))
532     create_bindings (OpSmime, MENU_SMIME);
533
534 #ifdef CRYPT_BACKEND_GPGME
535   create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
536   create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
537 #endif
538
539 #ifdef MIXMASTER
540   create_bindings (OpMix, MENU_MIX);
541
542   km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
543   km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
544   km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
545 #endif
546
547   /* bindings for the line editor */
548   create_bindings (OpEditor, MENU_EDITOR);
549
550   km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
551   km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
552   km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
553   km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
554   km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
555   km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
556   km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
557   km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
558   km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
559
560   /* generic menu keymap */
561   create_bindings (OpGeneric, MENU_GENERIC);
562
563   km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
564   km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
565   km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
566   km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
567   km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
568   km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
569   km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
570   km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
571   km_bindkey ("1", MENU_GENERIC, OP_JUMP);
572   km_bindkey ("2", MENU_GENERIC, OP_JUMP);
573   km_bindkey ("3", MENU_GENERIC, OP_JUMP);
574   km_bindkey ("4", MENU_GENERIC, OP_JUMP);
575   km_bindkey ("5", MENU_GENERIC, OP_JUMP);
576   km_bindkey ("6", MENU_GENERIC, OP_JUMP);
577   km_bindkey ("7", MENU_GENERIC, OP_JUMP);
578   km_bindkey ("8", MENU_GENERIC, OP_JUMP);
579   km_bindkey ("9", MENU_GENERIC, OP_JUMP);
580
581   km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
582
583   /* Miscellaneous extra bindings */
584
585   km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
586   km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
587   km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
588   km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
589   km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
590   km_bindkey ("x", MENU_MAIN, OP_EXIT);
591
592   km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
593
594   km_bindkey ("x", MENU_PAGER, OP_EXIT);
595   km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
596   km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
597   km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
598   km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
599   km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
600   km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
601   km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
602   km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
603   km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
604   km_bindkey ("1", MENU_PAGER, OP_JUMP);
605   km_bindkey ("2", MENU_PAGER, OP_JUMP);
606   km_bindkey ("3", MENU_PAGER, OP_JUMP);
607   km_bindkey ("4", MENU_PAGER, OP_JUMP);
608   km_bindkey ("5", MENU_PAGER, OP_JUMP);
609   km_bindkey ("6", MENU_PAGER, OP_JUMP);
610   km_bindkey ("7", MENU_PAGER, OP_JUMP);
611   km_bindkey ("8", MENU_PAGER, OP_JUMP);
612   km_bindkey ("9", MENU_PAGER, OP_JUMP);
613
614   km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
615
616   km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
617   km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
618   km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
619
620   km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
621   km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
622
623   /* edit-to (default "t") hides generic tag-entry in Compose menu
624      This will bind tag-entry to  "T" in the Compose menu */
625   km_bindkey ("T", MENU_COMPOSE, OP_TAG);
626 }
627
628 void km_error_key (int menu)
629 {
630   char buf[SHORT_STRING];
631   struct keymap_t *key;
632
633   if (!(key = km_find_func (menu, OP_HELP)))
634     key = km_find_func (MENU_GENERIC, OP_HELP);
635
636   if (!(km_expand_key (buf, sizeof (buf), key))) {
637     mutt_error _("Key is not bound.");
638
639     return;
640   }
641
642   /* make sure the key is really the help key in this menu */
643   push_string (buf);
644   if (km_dokey (menu) != OP_HELP) {
645     mutt_error _("Key is not bound.");
646
647     return;
648   }
649
650   mutt_error (_("Key is not bound.  Press '%s' for help."), buf);
651   return;
652 }
653
654 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
655                      BUFFER * err)
656 {
657   int r = 0;
658
659   mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
660   if (MoreArgs (s)) {
661     strfcpy (err->data, _("push: too many arguments"), err->dsize);
662     r = -1;
663   }
664   else
665     push_string (buf->data);
666   return (r);
667 }
668
669 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
670 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
671                            BUFFER * err)
672 {
673   BUFFER buf;
674   int i = 0;
675   char *p, *q;
676
677   memset (&buf, 0, sizeof (buf));
678
679   /* menu name */
680   mutt_extract_token (&buf, s, 0);
681   p = buf.data;
682   if (MoreArgs (s)) {
683     while (i < maxmenus) {
684       q = strchr (p, ',');
685       if (q)
686         *q = '\0';
687
688       if ((menu[i] = mutt_check_menu (p)) == -1) {
689         snprintf (err->data, err->dsize, _("%s: no such menu"), p);
690         goto error;
691       }
692       ++i;
693       if (q)
694         p = q + 1;
695       else
696         break;
697     }
698     *nummenus = i;
699     /* key sequence */
700     mutt_extract_token (&buf, s, 0);
701
702     if (!*buf.data) {
703       strfcpy (err->data, _("null key sequence"), err->dsize);
704     }
705     else if (MoreArgs (s))
706       return (buf.data);
707   }
708   else {
709     strfcpy (err->data, _("too few arguments"), err->dsize);
710   }
711 error:
712   mem_free (&buf.data);
713   return (NULL);
714 }
715
716 static int
717 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
718 {
719   int i;
720
721   for (i = 0; bindings[i].name; i++)
722     if (str_cmp (func, bindings[i].name) == 0) {
723       km_bindkey (key, menu, bindings[i].op);
724       return (0);
725     }
726   return (-1);
727 }
728
729 struct binding_t *km_get_table (int menu)
730 {
731   switch (menu) {
732   case MENU_MAIN:
733     return OpMain;
734   case MENU_GENERIC:
735     return OpGeneric;
736   case MENU_COMPOSE:
737     return OpCompose;
738   case MENU_PAGER:
739     return OpPager;
740   case MENU_POST:
741     return OpPost;
742   case MENU_FOLDER:
743     return OpBrowser;
744   case MENU_ALIAS:
745     return OpAlias;
746   case MENU_ATTACH:
747     return OpAttach;
748   case MENU_EDITOR:
749     return OpEditor;
750   case MENU_QUERY:
751     return OpQuery;
752
753   case MENU_PGP:
754     return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
755
756 #ifdef CRYPT_BACKEND_GPGME
757   case MENU_KEY_SELECT_PGP:
758     return OpPgp;
759   case MENU_KEY_SELECT_SMIME:
760     return OpSmime;
761 #endif
762
763 #ifdef MIXMASTER
764   case MENU_MIX:
765     return OpMix;
766 #endif
767
768   }
769   return NULL;
770 }
771
772 /* bind menu-name '<key_sequence>' function-name */
773 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
774                      BUFFER * err)
775 {
776   struct binding_t *bindings = NULL;
777   char *key;
778   int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
779     0, nummenus, i;
780
781   if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
782                            &nummenus, err)) == NULL)
783     return (-1);
784
785   /* function to execute */
786   mutt_extract_token (buf, s, 0);
787   if (MoreArgs (s)) {
788     strfcpy (err->data, _("bind: too many arguments"), err->dsize);
789     r = -1;
790   }
791   else if (ascii_strcasecmp ("noop", buf->data) == 0) {
792     for (i = 0; i < nummenus; ++i) {
793       km_bindkey (key, menu[i], OP_NULL);       /* the `unbind' command */
794     }
795   }
796   else {
797     for (i = 0; i < nummenus; ++i) {
798       /* First check the "generic" list of commands */
799       if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
800           menu[i] == MENU_GENERIC ||
801           try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
802         /* Now check the menu-specific list of commands (if they exist) */
803         bindings = km_get_table (menu[i]);
804         if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
805           snprintf (err->data, err->dsize, _("%s: no such function in map"),
806                     buf->data);
807           r = -1;
808         }
809       }
810     }
811   }
812   mem_free (&key);
813   return (r);
814 }
815
816 /* macro <menu> <key> <macro> <description> */
817 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
818                       BUFFER * err)
819 {
820   int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
821     -1, nummenus, i;
822   char *seq = NULL;
823   char *key;
824
825   if ((key =
826        parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
827                      err)) == NULL)
828     return (-1);
829
830   mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
831   /* make sure the macro sequence is not an empty string */
832   if (!*buf->data) {
833     strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
834   }
835   else {
836     if (MoreArgs (s)) {
837       seq = str_dup (buf->data);
838       mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
839
840       if (MoreArgs (s)) {
841         strfcpy (err->data, _("macro: too many arguments"), err->dsize);
842       }
843       else {
844         for (i = 0; i < nummenus; ++i) {
845           km_bind (key, menu[i], OP_MACRO, seq, buf->data);
846           r = 0;
847         }
848       }
849
850       mem_free (&seq);
851     }
852     else {
853       for (i = 0; i < nummenus; ++i) {
854         km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
855         r = 0;
856       }
857     }
858   }
859   mem_free (&key);
860   return (r);
861 }
862
863 /* exec function-name */
864 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
865                      BUFFER * err)
866 {
867   int ops[128];
868   int nops = 0;
869   struct binding_t *bindings = NULL;
870   char *function;
871
872   if (!MoreArgs (s)) {
873     strfcpy (err->data, _("exec: no arguments"), err->dsize);
874     return (-1);
875   }
876
877   do {
878     mutt_extract_token (buf, s, 0);
879     function = buf->data;
880
881     if ((bindings = km_get_table (CurrentMenu)) == NULL
882         && CurrentMenu != MENU_PAGER)
883       bindings = OpGeneric;
884
885     ops[nops] = get_op (bindings, function, str_len (function));
886     if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
887       ops[nops] = get_op (OpGeneric, function, str_len (function));
888
889     if (ops[nops] == OP_NULL) {
890       mutt_flushinp ();
891       mutt_error (_("%s: no such function"), function);
892       return (-1);
893     }
894     nops++;
895   }
896   while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
897
898   while (nops)
899     mutt_ungetch (0, ops[--nops]);
900
901   return 0;
902 }
903
904 /*
905  * prompts the user to enter a keystroke, and displays the octal value back
906  * to the user.
907  */
908 void mutt_what_key (void)
909 {
910   int ch;
911
912   mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
913   do {
914     ch = getch ();
915     if (ch != ERR && ch != ctrl ('G')) {
916       mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
917                     km_keyname (ch), ch, ch);
918     }
919   }
920   while (ch != ERR && ch != ctrl ('G'));
921
922   mutt_flushinp ();
923 }