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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
24 #include "mutt_crypt.h"
30 #include "functions.h"
32 struct mapping_t Menus[] = {
33 { "alias", MENU_ALIAS },
34 { "attach", MENU_ATTACH },
35 { "browser", MENU_FOLDER },
36 { "compose", MENU_COMPOSE },
37 { "editor", MENU_EDITOR },
38 { "index", MENU_MAIN },
39 { "pager", MENU_PAGER },
40 { "postpone", MENU_POST },
42 { "smime", MENU_SMIME },
45 { "key_select_pgp", MENU_KEY_SELECT_PGP },
46 { "key_select_smime", MENU_KEY_SELECT_SMIME },
55 { "query", MENU_QUERY },
56 { "generic", MENU_GENERIC },
60 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
62 static struct mapping_t KeyNames[] = {
63 { "<PageUp>", KEY_PPAGE },
64 { "<PageDown>", KEY_NPAGE },
66 { "<Down>", KEY_DOWN },
67 { "<Right>", KEY_RIGHT },
68 { "<Left>", KEY_LEFT },
69 { "<Delete>", KEY_DC },
70 { "<BackSpace>",KEY_BACKSPACE },
71 { "<Insert>", KEY_IC },
72 { "<Home>", KEY_HOME },
75 { "<Enter>", KEY_ENTER },
77 { "<Return>", M_ENTER_C },
82 { "<BackTab>", KEY_BTAB },
87 /* contains the last key the user pressed */
90 struct keymap_t *Keymaps[MENU_MAX];
92 static struct keymap_t *allocKeys (int len, keycode_t *keys)
96 p = safe_calloc (1, sizeof (struct keymap_t));
98 p->keys = safe_malloc (len * sizeof (keycode_t));
99 memcpy (p->keys, keys, len * sizeof (keycode_t));
103 static int parse_fkey(char *s)
108 if(s[0] != '<' || ascii_tolower(s[1]) != 'f')
111 for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
124 * This function parses the string <NNN> and uses the octal value as the key
127 static int parse_keycode (const char *s)
129 if (isdigit ((unsigned char) s[1]) &&
130 isdigit ((unsigned char) s[2]) &&
131 isdigit ((unsigned char) s[3]) &&
134 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
139 static int parsekeys (char *str, keycode_t *d, int max)
142 char buff[SHORT_STRING];
146 strfcpy(buff, str, sizeof(buff));
152 if(*s == '<' && (t = strchr(s, '>')))
154 t++; c = *t; *t = '\0';
156 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
161 else if ((n = parse_fkey(s)) > 0)
166 else if ((n = parse_keycode(s)) > 0)
177 *d = (unsigned char)*s;
187 /* insert a key sequence into the specified map. the map is sorted by ASCII
188 * value (lowest to highest)
190 void km_bind (char *s, int menu, int op, char *macro, char *descr)
192 struct keymap_t *map, *tmp, *last = NULL, *next;
193 keycode_t buf[MAX_SEQ];
194 int len, pos = 0, lastpos = 0;
196 len = parsekeys (s, buf, MAX_SEQ);
198 map = allocKeys (len, buf);
200 map->macro = safe_strdup (macro);
201 map->descr = safe_strdup (descr);
207 if (pos >= len || pos >= tmp->len)
209 /* map and tmp match, but have different lengths, so overwrite */
220 while (tmp && len >= pos);
224 else if (buf[pos] == tmp->keys[pos])
226 else if (buf[pos] < tmp->keys[pos])
228 /* found location to insert between last and tmp */
232 else /* buf[pos] > tmp->keys[pos] */
252 void km_bindkey (char *s, int menu, int op)
254 km_bind (s, menu, op, NULL, NULL);
257 static int get_op (struct binding_t *bindings, const char *start, size_t len)
261 for (i = 0; bindings[i].name; i++)
263 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
264 mutt_strlen (bindings[i].name) == len)
265 return bindings[i].op;
271 static char *get_func (struct binding_t *bindings, int op)
275 for (i = 0; bindings[i].name; i++)
277 if (bindings[i].op == op) {
278 return bindings[i].name;
285 static void push_string (char *s)
287 char *pp, *p = s + mutt_strlen (s) - 1;
293 /* if we see something like "<PageUp>", look to see if it is a real
294 function name and return the corresponding value */
297 for (pp = p - 1; pp >= s && *pp != '<'; pp--)
301 if ((i = parse_fkey (pp)) > 0)
303 mutt_ungetch (KEY_F (i), 0);
309 for (i = 0; KeyNames[i].name; i++)
311 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
314 if (KeyNames[i].name)
317 mutt_ungetch (KeyNames[i].value, 0);
322 /* See if it is a valid command
323 * skip the '<' and the '>' when comparing */
324 for (i = 0; Menus[i].name; i++)
326 struct binding_t *binding = km_get_table (Menus[i].value);
329 op = get_op (binding, pp + 1, l - 2);
337 mutt_ungetch (0, op);
343 mutt_ungetch (*p--, 0);
347 static int retry_generic (int menu, keycode_t *keys, int keyslen, int lastkey)
349 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER)
352 mutt_ungetch (lastkey, 0);
353 for (; keyslen; keyslen--)
354 mutt_ungetch (keys[keyslen - 1], 0);
355 return (km_dokey (MENU_GENERIC));
357 if (menu != MENU_EDITOR)
359 /* probably a good idea to flush input here so we can abort macros */
366 * >0 function to execute
367 * OP_NULL no function bound to key sequence
368 * -1 error occured while reading input
370 int km_dokey (int menu)
373 struct keymap_t *map = Keymaps[menu];
380 return (retry_generic (menu, NULL, 0, 0));
384 /* ncurses doesn't return on resized screen when timeout is set to zero */
385 if (menu != MENU_EDITOR)
386 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
390 if (menu != MENU_EDITOR)
391 timeout (-1); /* restore blocking operation */
397 /* do we have an op already? */
401 struct binding_t *bindings;
403 /* is this a valid op for this menu? */
404 if ((bindings = km_get_table (menu)) &&
405 (func = get_func (bindings, tmp.op)))
408 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
411 if (menu != MENU_EDITOR && menu != MENU_PAGER)
413 /* check generic menu */
414 bindings = OpGeneric;
415 if ((func = get_func (bindings, tmp.op)))
419 /* Sigh. Valid function but not in this context.
420 * Find the literal string and push it back */
421 for (i = 0; Menus[i].name; i++)
423 bindings = km_get_table (Menus[i].value);
426 func = get_func (bindings, tmp.op);
429 /* careful not to feed the <..> as one token. otherwise
430 * push_string() will push the bogus op right back! */
431 mutt_ungetch ('>', 0);
433 mutt_ungetch ('<', 0);
438 /* continue to chew */
443 /* Nope. Business as usual */
444 while (LastKey > map->keys[pos])
446 if (pos > map->eq || !map->next)
447 return (retry_generic (menu, map->keys, pos, LastKey));
451 if (LastKey != map->keys[pos])
452 return (retry_generic (menu, map->keys, pos, LastKey));
454 if (++pos == map->len)
457 if (map->op != OP_MACRO)
463 mutt_error _("Macro loop detected.");
467 push_string (map->macro);
476 static void create_bindings (struct binding_t *map, int menu)
480 for (i = 0 ; map[i].name ; i++)
482 km_bindkey (map[i].seq, menu, map[i].op);
485 char *km_keyname (int c)
490 if ((p = mutt_getnamebyvalue (c, KeyNames)))
493 if (c < 256 && c > -128 && iscntrl ((unsigned char) c))
501 buf[1] = (c + '@') & 0x7f;
505 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
507 else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */
508 sprintf (buf, "<F%d>", c - KEY_F0);
509 else if (IsPrint (c))
510 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
512 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
516 int km_expand_key (char *s, size_t len, struct keymap_t *map)
526 strfcpy (s, km_keyname (map->keys[p]), len);
527 len -= (l = mutt_strlen (s));
529 if (++p >= map->len || !len)
538 struct keymap_t *km_find_func (int menu, int func)
540 struct keymap_t *map = Keymaps[menu];
542 for (; map; map = map->next)
550 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
552 create_bindings (OpAttach, MENU_ATTACH);
553 create_bindings (OpBrowser, MENU_FOLDER);
554 create_bindings (OpCompose, MENU_COMPOSE);
555 create_bindings (OpMain, MENU_MAIN);
556 create_bindings (OpPager, MENU_PAGER);
557 create_bindings (OpPost, MENU_POST);
558 create_bindings (OpQuery, MENU_QUERY);
559 create_bindings (OpAlias, MENU_ALIAS);
562 if ((WithCrypto & APPLICATION_PGP))
563 create_bindings (OpPgp, MENU_PGP);
565 if ((WithCrypto & APPLICATION_SMIME))
566 create_bindings (OpSmime, MENU_SMIME);
568 #ifdef CRYPT_BACKEND_GPGME
569 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
570 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
574 create_bindings (OpMix, MENU_MIX);
576 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
577 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
578 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
581 /* bindings for the line editor */
582 create_bindings (OpEditor, MENU_EDITOR);
584 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
585 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
586 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
587 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
588 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
589 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
590 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
591 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
592 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
594 /* generic menu keymap */
595 create_bindings (OpGeneric, MENU_GENERIC);
597 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
598 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
599 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
600 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
601 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
602 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
603 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
604 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
605 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
606 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
607 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
608 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
609 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
610 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
611 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
612 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
613 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
615 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
617 /* Miscellaneous extra bindings */
619 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
620 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
621 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
622 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
623 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
624 km_bindkey ("x", MENU_MAIN, OP_EXIT);
626 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
628 km_bindkey ("x", MENU_PAGER, OP_EXIT);
629 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
630 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
631 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
632 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
633 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
634 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
635 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
636 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
637 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
638 km_bindkey ("1", MENU_PAGER, OP_JUMP);
639 km_bindkey ("2", MENU_PAGER, OP_JUMP);
640 km_bindkey ("3", MENU_PAGER, OP_JUMP);
641 km_bindkey ("4", MENU_PAGER, OP_JUMP);
642 km_bindkey ("5", MENU_PAGER, OP_JUMP);
643 km_bindkey ("6", MENU_PAGER, OP_JUMP);
644 km_bindkey ("7", MENU_PAGER, OP_JUMP);
645 km_bindkey ("8", MENU_PAGER, OP_JUMP);
646 km_bindkey ("9", MENU_PAGER, OP_JUMP);
648 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
650 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
651 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
652 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
654 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
655 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
657 /* edit-to (default "t") hides generic tag-entry in Compose menu
658 This will bind tag-entry to "T" in the Compose menu */
659 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
662 void km_error_key (int menu)
664 char buf[SHORT_STRING];
665 struct keymap_t *key;
667 if(!(key = km_find_func(menu, OP_HELP)))
668 key = km_find_func(MENU_GENERIC, OP_HELP);
670 if(!(km_expand_key(buf, sizeof(buf), key)))
672 mutt_error _("Key is not bound.");
676 /* make sure the key is really the help key in this menu */
678 if (km_dokey (menu) != OP_HELP)
680 mutt_error _("Key is not bound.");
684 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
688 int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
692 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
695 strfcpy (err->data, _("push: too many arguments"), err->dsize);
699 push_string (buf->data);
703 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
704 static char *parse_keymap (int *menu, BUFFER *s, int maxmenus, int *nummenus, BUFFER *err)
710 memset (&buf, 0, sizeof (buf));
713 mutt_extract_token (&buf, s, 0);
723 if ((menu[i] = mutt_check_menu (p)) == -1)
725 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
736 mutt_extract_token (&buf, s, 0);
740 strfcpy (err->data, _("null key sequence"), err->dsize);
742 else if (MoreArgs (s))
747 strfcpy (err->data, _("too few arguments"), err->dsize);
755 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
759 for (i = 0; bindings[i].name; i++)
760 if (mutt_strcmp (func, bindings[i].name) == 0)
762 km_bindkey (key, menu, bindings[i].op);
768 struct binding_t *km_get_table (int menu)
794 return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;
796 #ifdef CRYPT_BACKEND_GPGME
797 case MENU_KEY_SELECT_PGP:
799 case MENU_KEY_SELECT_SMIME:
812 /* bind menu-name '<key_sequence>' function-name */
813 int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
815 struct binding_t *bindings = NULL;
817 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = 0, nummenus, i;
819 if ((key = parse_keymap (menu, s, sizeof (menu)/sizeof (menu[0]),
820 &nummenus, err)) == NULL)
823 /* function to execute */
824 mutt_extract_token (buf, s, 0);
827 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
830 else if (ascii_strcasecmp ("noop", buf->data) == 0)
832 for (i = 0; i < nummenus; ++i)
834 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
839 for (i = 0; i < nummenus; ++i)
841 /* First check the "generic" list of commands */
842 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
843 menu[i] == MENU_GENERIC ||
844 try_bind (key, menu[i], buf->data, OpGeneric) != 0)
846 /* Now check the menu-specific list of commands (if they exist) */
847 bindings = km_get_table (menu[i]);
848 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0)
850 snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data);
860 /* macro <menu> <key> <macro> <description> */
861 int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
863 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = -1, nummenus, i;
867 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus, err)) == NULL)
870 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
871 /* make sure the macro sequence is not an empty string */
874 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
880 seq = safe_strdup (buf->data);
881 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
885 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
889 for (i = 0; i < nummenus; ++i)
891 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
900 for (i = 0; i < nummenus; ++i)
902 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
911 /* exec function-name */
912 int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
916 struct binding_t *bindings = NULL;
921 strfcpy (err->data, _("exec: no arguments"), err->dsize);
927 mutt_extract_token (buf, s, 0);
928 function = buf->data;
930 if ((bindings = km_get_table (CurrentMenu)) == NULL
931 && CurrentMenu != MENU_PAGER)
932 bindings = OpGeneric;
934 ops[nops] = get_op (bindings, function, mutt_strlen(function));
935 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
936 ops[nops] = get_op (OpGeneric, function, mutt_strlen(function));
938 if (ops[nops] == OP_NULL)
941 mutt_error (_("%s: no such function"), function);
946 while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0]));
949 mutt_ungetch(0, ops[--nops]);
955 * prompts the user to enter a keystroke, and displays the octal value back
958 void mutt_what_key (void)
962 mvprintw(LINES-1,0, _("Enter keys (^G to abort): "));
965 if (ch != ERR && ch != ctrl ('G'))
967 mutt_message(_("Char = %s, Octal = %o, Decimal = %d"),
968 km_keyname(ch), ch, ch);
971 while (ch != ERR && ch != ctrl ('G'));