2 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_menu.h"
25 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
30 #include "imap/imap.h"
37 #include "functions.h"
39 struct mapping_t Menus[] = {
40 { "alias", MENU_ALIAS },
41 { "attach", MENU_ATTACH },
42 { "browser", MENU_FOLDER },
43 { "compose", MENU_COMPOSE },
44 { "editor", MENU_EDITOR },
45 { "index", MENU_MAIN },
46 { "pager", MENU_PAGER },
47 { "postpone", MENU_POST },
49 { "smime", MENU_SMIME },
51 { "key_select_pgp", MENU_KEY_SELECT_PGP },
52 { "key_select_smime", MENU_KEY_SELECT_SMIME },
60 { "query", MENU_QUERY },
61 { "generic", MENU_GENERIC },
65 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
67 static struct mapping_t KeyNames[] = {
68 { "<PageUp>", KEY_PPAGE },
69 { "<PageDown>", KEY_NPAGE },
71 { "<Down>", KEY_DOWN },
72 { "<Right>", KEY_RIGHT },
73 { "<Left>", KEY_LEFT },
74 { "<Delete>", KEY_DC },
75 { "<BackSpace>",KEY_BACKSPACE },
76 { "<Insert>", KEY_IC },
77 { "<Home>", KEY_HOME },
80 { "<Enter>", KEY_ENTER },
82 { "<Return>", M_ENTER_C },
87 { "<BackTab>", KEY_BTAB },
90 { "<Next>", KEY_NEXT },
95 /* contains the last key the user pressed */
98 struct keymap_t *Keymaps[MENU_MAX];
100 static struct keymap_t *allocKeys (int len, keycode_t *keys)
104 p = safe_calloc (1, sizeof (struct keymap_t));
106 p->keys = safe_malloc (len * sizeof (keycode_t));
107 memcpy (p->keys, keys, len * sizeof (keycode_t));
111 static int parse_fkey(char *s)
116 if(s[0] != '<' || ascii_tolower(s[1]) != 'f')
119 for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
132 * This function parses the string <NNN> and uses the octal value as the key
135 static int parse_keycode (const char *s)
137 if (isdigit ((unsigned char) s[1]) &&
138 isdigit ((unsigned char) s[2]) &&
139 isdigit ((unsigned char) s[3]) &&
142 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
147 static int parsekeys (char *str, keycode_t *d, int max)
150 char buff[SHORT_STRING];
154 strfcpy(buff, str, sizeof(buff));
160 if(*s == '<' && (t = strchr(s, '>')))
162 t++; c = *t; *t = '\0';
164 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
169 else if ((n = parse_fkey(s)) > 0)
174 else if ((n = parse_keycode(s)) > 0)
185 *d = (unsigned char)*s;
195 /* insert a key sequence into the specified map. the map is sorted by ASCII
196 * value (lowest to highest)
198 void km_bind (char *s, int menu, int op, char *macro, char *descr)
200 struct keymap_t *map, *tmp, *last = NULL, *next;
201 keycode_t buf[MAX_SEQ];
202 int len, pos = 0, lastpos = 0;
204 len = parsekeys (s, buf, MAX_SEQ);
206 map = allocKeys (len, buf);
208 map->macro = safe_strdup (macro);
209 map->descr = safe_strdup (descr);
215 if (pos >= len || pos >= tmp->len)
217 /* map and tmp match, but have different lengths, so overwrite */
228 while (tmp && len >= pos);
232 else if (buf[pos] == tmp->keys[pos])
234 else if (buf[pos] < tmp->keys[pos])
236 /* found location to insert between last and tmp */
240 else /* buf[pos] > tmp->keys[pos] */
260 void km_bindkey (char *s, int menu, int op)
262 km_bind (s, menu, op, NULL, NULL);
265 static int get_op (struct binding_t *bindings, const char *start, size_t len)
269 for (i = 0; bindings[i].name; i++)
271 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
272 mutt_strlen (bindings[i].name) == len)
273 return bindings[i].op;
279 static char *get_func (struct binding_t *bindings, int op)
283 for (i = 0; bindings[i].name; i++)
285 if (bindings[i].op == op)
286 return bindings[i].name;
292 static void push_string (char *s)
294 char *pp, *p = s + mutt_strlen (s) - 1;
300 /* if we see something like "<PageUp>", look to see if it is a real
301 function name and return the corresponding value */
304 for (pp = p - 1; pp >= s && *pp != '<'; pp--)
308 if ((i = parse_fkey (pp)) > 0)
310 mutt_ungetch (KEY_F (i), 0);
316 for (i = 0; KeyNames[i].name; i++)
318 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
321 if (KeyNames[i].name)
324 mutt_ungetch (KeyNames[i].value, 0);
329 /* See if it is a valid command
330 * skip the '<' and the '>' when comparing */
331 for (i = 0; Menus[i].name; i++)
333 struct binding_t *binding = km_get_table (Menus[i].value);
336 op = get_op (binding, pp + 1, l - 2);
344 mutt_ungetch (0, op);
350 mutt_ungetch ((unsigned char)*p--, 0); /* independent 8 bits chars */
354 static int retry_generic (int menu, keycode_t *keys, int keyslen, int lastkey)
356 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER)
359 mutt_ungetch (lastkey, 0);
360 for (; keyslen; keyslen--)
361 mutt_ungetch (keys[keyslen - 1], 0);
362 return (km_dokey (MENU_GENERIC));
364 if (menu != MENU_EDITOR)
366 /* probably a good idea to flush input here so we can abort macros */
373 * >0 function to execute
374 * OP_NULL no function bound to key sequence
375 * -1 error occured while reading input
377 int km_dokey (int menu)
380 struct keymap_t *map = Keymaps[menu];
386 return (retry_generic (menu, NULL, 0, 0));
390 i = Timeout > 0 ? Timeout : 60;
392 /* keepalive may need to run more frequently than Timeout allows */
395 if (ImapKeepalive >= i)
398 while (ImapKeepalive && ImapKeepalive < i)
400 timeout (ImapKeepalive * 1000);
404 /* something other than timeout */
416 /* hide timeouts from line editor */
417 if (menu == MENU_EDITOR && tmp.ch == -2)
425 /* do we have an op already? */
429 struct binding_t *bindings;
431 /* is this a valid op for this menu? */
432 if ((bindings = km_get_table (menu)) &&
433 (func = get_func (bindings, tmp.op)))
436 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
439 if (menu != MENU_EDITOR && menu != MENU_PAGER)
441 /* check generic menu */
442 bindings = OpGeneric;
443 if ((func = get_func (bindings, tmp.op)))
447 /* Sigh. Valid function but not in this context.
448 * Find the literal string and push it back */
449 for (i = 0; Menus[i].name; i++)
451 bindings = km_get_table (Menus[i].value);
454 func = get_func (bindings, tmp.op);
457 /* careful not to feed the <..> as one token. otherwise
458 * push_string() will push the bogus op right back! */
459 mutt_ungetch ('>', 0);
461 mutt_ungetch ('<', 0);
466 /* continue to chew */
471 /* Nope. Business as usual */
472 while (LastKey > map->keys[pos])
474 if (pos > map->eq || !map->next)
475 return (retry_generic (menu, map->keys, pos, LastKey));
479 if (LastKey != map->keys[pos])
480 return (retry_generic (menu, map->keys, pos, LastKey));
482 if (++pos == map->len)
485 if (map->op != OP_MACRO)
491 mutt_error _("Macro loop detected.");
495 push_string (map->macro);
504 static void create_bindings (struct binding_t *map, int menu)
508 for (i = 0 ; map[i].name ; i++)
510 km_bindkey (map[i].seq, menu, map[i].op);
513 char *km_keyname (int c)
518 if ((p = mutt_getnamebyvalue (c, KeyNames)))
521 if (c < 256 && c > -128 && iscntrl ((unsigned char) c))
529 buf[1] = (c + '@') & 0x7f;
533 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
535 else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */
536 sprintf (buf, "<F%d>", c - KEY_F0);
537 else if (IsPrint (c))
538 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
540 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
544 int km_expand_key (char *s, size_t len, struct keymap_t *map)
554 strfcpy (s, km_keyname (map->keys[p]), len);
555 len -= (l = mutt_strlen (s));
557 if (++p >= map->len || !len)
566 struct keymap_t *km_find_func (int menu, int func)
568 struct keymap_t *map = Keymaps[menu];
570 for (; map; map = map->next)
578 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
580 create_bindings (OpAttach, MENU_ATTACH);
581 create_bindings (OpBrowser, MENU_FOLDER);
582 create_bindings (OpCompose, MENU_COMPOSE);
583 create_bindings (OpMain, MENU_MAIN);
584 create_bindings (OpPager, MENU_PAGER);
585 create_bindings (OpPost, MENU_POST);
586 create_bindings (OpQuery, MENU_QUERY);
587 create_bindings (OpAlias, MENU_ALIAS);
590 if ((WithCrypto & APPLICATION_PGP))
591 create_bindings (OpPgp, MENU_PGP);
593 if ((WithCrypto & APPLICATION_SMIME))
594 create_bindings (OpSmime, MENU_SMIME);
596 #ifdef CRYPT_BACKEND_GPGME
597 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
598 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
602 create_bindings (OpMix, MENU_MIX);
604 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
605 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
606 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
609 /* bindings for the line editor */
610 create_bindings (OpEditor, MENU_EDITOR);
612 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
613 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
614 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
615 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
616 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
617 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
618 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
619 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
620 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
622 /* generic menu keymap */
623 create_bindings (OpGeneric, MENU_GENERIC);
625 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
626 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
627 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
628 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
629 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
630 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
631 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
632 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
633 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
634 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
635 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
636 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
637 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
638 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
639 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
640 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
641 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
643 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
645 /* Miscellaneous extra bindings */
647 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
648 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
649 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
650 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
651 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
652 km_bindkey ("x", MENU_MAIN, OP_EXIT);
654 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
656 km_bindkey ("x", MENU_PAGER, OP_EXIT);
657 km_bindkey ("i", MENU_PAGER, OP_EXIT);
658 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
659 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
660 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
661 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
662 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
663 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
664 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
665 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
666 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
667 km_bindkey ("1", MENU_PAGER, OP_JUMP);
668 km_bindkey ("2", MENU_PAGER, OP_JUMP);
669 km_bindkey ("3", MENU_PAGER, OP_JUMP);
670 km_bindkey ("4", MENU_PAGER, OP_JUMP);
671 km_bindkey ("5", MENU_PAGER, OP_JUMP);
672 km_bindkey ("6", MENU_PAGER, OP_JUMP);
673 km_bindkey ("7", MENU_PAGER, OP_JUMP);
674 km_bindkey ("8", MENU_PAGER, OP_JUMP);
675 km_bindkey ("9", MENU_PAGER, OP_JUMP);
677 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
679 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
680 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
681 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
683 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
684 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
686 /* edit-to (default "t") hides generic tag-entry in Compose menu
687 This will bind tag-entry to "T" in the Compose menu */
688 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
691 void km_error_key (int menu)
693 char buf[SHORT_STRING];
694 struct keymap_t *key;
696 if(!(key = km_find_func(menu, OP_HELP)))
697 key = km_find_func(MENU_GENERIC, OP_HELP);
699 if(!(km_expand_key(buf, sizeof(buf), key)))
701 mutt_error _("Key is not bound.");
705 /* make sure the key is really the help key in this menu */
707 if (km_dokey (menu) != OP_HELP)
709 mutt_error _("Key is not bound.");
713 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
717 int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
721 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
724 strfcpy (err->data, _("push: too many arguments"), err->dsize);
728 push_string (buf->data);
732 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
733 static char *parse_keymap (int *menu, BUFFER *s, int maxmenus, int *nummenus, BUFFER *err)
739 memset (&buf, 0, sizeof (buf));
742 mutt_extract_token (&buf, s, 0);
752 if ((menu[i] = mutt_check_menu (p)) == -1)
754 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
765 mutt_extract_token (&buf, s, 0);
769 strfcpy (err->data, _("null key sequence"), err->dsize);
771 else if (MoreArgs (s))
776 strfcpy (err->data, _("too few arguments"), err->dsize);
784 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
788 for (i = 0; bindings[i].name; i++)
789 if (mutt_strcmp (func, bindings[i].name) == 0)
791 km_bindkey (key, menu, bindings[i].op);
797 struct binding_t *km_get_table (int menu)
823 return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;
825 #ifdef CRYPT_BACKEND_GPGME
826 case MENU_KEY_SELECT_PGP:
828 case MENU_KEY_SELECT_SMIME:
841 /* bind menu-name '<key_sequence>' function-name */
842 int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
844 struct binding_t *bindings = NULL;
846 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = 0, nummenus, i;
848 if ((key = parse_keymap (menu, s, sizeof (menu)/sizeof (menu[0]),
849 &nummenus, err)) == NULL)
852 /* function to execute */
853 mutt_extract_token (buf, s, 0);
856 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
859 else if (ascii_strcasecmp ("noop", buf->data) == 0)
861 for (i = 0; i < nummenus; ++i)
863 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
868 for (i = 0; i < nummenus; ++i)
870 /* First check the "generic" list of commands */
871 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
872 menu[i] == MENU_GENERIC ||
873 try_bind (key, menu[i], buf->data, OpGeneric) != 0)
875 /* Now check the menu-specific list of commands (if they exist) */
876 bindings = km_get_table (menu[i]);
877 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0)
879 snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data);
889 /* macro <menu> <key> <macro> <description> */
890 int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
892 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = -1, nummenus, i;
896 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus, err)) == NULL)
899 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
900 /* make sure the macro sequence is not an empty string */
903 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
909 seq = safe_strdup (buf->data);
910 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
914 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
918 for (i = 0; i < nummenus; ++i)
920 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
929 for (i = 0; i < nummenus; ++i)
931 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
940 /* exec function-name */
941 int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
945 struct binding_t *bindings = NULL;
950 strfcpy (err->data, _("exec: no arguments"), err->dsize);
956 mutt_extract_token (buf, s, 0);
957 function = buf->data;
959 if ((bindings = km_get_table (CurrentMenu)) == NULL
960 && CurrentMenu != MENU_PAGER)
961 bindings = OpGeneric;
963 ops[nops] = get_op (bindings, function, mutt_strlen(function));
964 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
965 ops[nops] = get_op (OpGeneric, function, mutt_strlen(function));
967 if (ops[nops] == OP_NULL)
970 mutt_error (_("%s: no such function"), function);
975 while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0]));
978 mutt_ungetch(0, ops[--nops]);
984 * prompts the user to enter a keystroke, and displays the octal value back
987 void mutt_what_key (void)
991 mvprintw(LINES-1,0, _("Enter keys (^G to abort): "));
994 if (ch != ERR && ch != ctrl ('G'))
996 mutt_message(_("Char = %s, Octal = %o, Decimal = %d"),
997 km_keyname(ch), ch, ch);
1000 while (ch != ERR && ch != ctrl ('G'));