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.
24 #include "mutt_menu.h"
25 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
34 #include "functions.h"
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 },
46 { "smime", MENU_SMIME },
49 { "key_select_pgp", MENU_KEY_SELECT_PGP },
50 { "key_select_smime", MENU_KEY_SELECT_SMIME },
59 { "query", MENU_QUERY },
60 { "generic", MENU_GENERIC },
64 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
66 static struct mapping_t KeyNames[] = {
67 { "<PageUp>", KEY_PPAGE },
68 { "<PageDown>", KEY_NPAGE },
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 },
79 { "<Enter>", KEY_ENTER },
81 { "<Return>", M_ENTER_C },
86 { "<BackTab>", KEY_BTAB },
91 /* contains the last key the user pressed */
94 struct keymap_t *Keymaps[MENU_MAX];
96 static struct keymap_t *allocKeys (int len, keycode_t *keys)
100 p = safe_calloc (1, sizeof (struct keymap_t));
102 p->keys = safe_malloc (len * sizeof (keycode_t));
103 memcpy (p->keys, keys, len * sizeof (keycode_t));
107 static int parse_fkey(char *s)
112 if(s[0] != '<' || ascii_tolower(s[1]) != 'f')
115 for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
128 * This function parses the string <NNN> and uses the octal value as the key
131 static int parse_keycode (const char *s)
133 if (isdigit ((unsigned char) s[1]) &&
134 isdigit ((unsigned char) s[2]) &&
135 isdigit ((unsigned char) s[3]) &&
138 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
143 static int parsekeys (char *str, keycode_t *d, int max)
146 char buff[SHORT_STRING];
150 strfcpy(buff, str, sizeof(buff));
156 if(*s == '<' && (t = strchr(s, '>')))
158 t++; c = *t; *t = '\0';
160 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
165 else if ((n = parse_fkey(s)) > 0)
170 else if ((n = parse_keycode(s)) > 0)
181 *d = (unsigned char)*s;
191 /* insert a key sequence into the specified map. the map is sorted by ASCII
192 * value (lowest to highest)
194 void km_bind (char *s, int menu, int op, char *macro, char *descr)
196 struct keymap_t *map, *tmp, *last = NULL, *next;
197 keycode_t buf[MAX_SEQ];
198 int len, pos = 0, lastpos = 0;
200 len = parsekeys (s, buf, MAX_SEQ);
202 map = allocKeys (len, buf);
204 map->macro = safe_strdup (macro);
205 map->descr = safe_strdup (descr);
211 if (pos >= len || pos >= tmp->len)
213 /* map and tmp match, but have different lengths, so overwrite */
224 while (tmp && len >= pos);
228 else if (buf[pos] == tmp->keys[pos])
230 else if (buf[pos] < tmp->keys[pos])
232 /* found location to insert between last and tmp */
236 else /* buf[pos] > tmp->keys[pos] */
256 void km_bindkey (char *s, int menu, int op)
258 km_bind (s, menu, op, NULL, NULL);
261 static int get_op (struct binding_t *bindings, const char *start, size_t len)
265 for (i = 0; bindings[i].name; i++)
267 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
268 mutt_strlen (bindings[i].name) == len)
269 return bindings[i].op;
275 static char *get_func (struct binding_t *bindings, int op)
279 for (i = 0; bindings[i].name; i++)
281 if (bindings[i].op == op) {
282 return bindings[i].name;
289 static void push_string (char *s)
291 char *pp, *p = s + mutt_strlen (s) - 1;
297 /* if we see something like "<PageUp>", look to see if it is a real
298 function name and return the corresponding value */
301 for (pp = p - 1; pp >= s && *pp != '<'; pp--)
305 if ((i = parse_fkey (pp)) > 0)
307 mutt_ungetch (KEY_F (i), 0);
313 for (i = 0; KeyNames[i].name; i++)
315 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
318 if (KeyNames[i].name)
321 mutt_ungetch (KeyNames[i].value, 0);
326 /* See if it is a valid command
327 * skip the '<' and the '>' when comparing */
328 for (i = 0; Menus[i].name; i++)
330 struct binding_t *binding = km_get_table (Menus[i].value);
333 op = get_op (binding, pp + 1, l - 2);
341 mutt_ungetch (0, op);
347 mutt_ungetch (*p--, 0);
351 static int retry_generic (int menu, keycode_t *keys, int keyslen, int lastkey)
353 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER)
356 mutt_ungetch (lastkey, 0);
357 for (; keyslen; keyslen--)
358 mutt_ungetch (keys[keyslen - 1], 0);
359 return (km_dokey (MENU_GENERIC));
361 if (menu != MENU_EDITOR)
363 /* probably a good idea to flush input here so we can abort macros */
370 * >0 function to execute
371 * OP_NULL no function bound to key sequence
372 * -1 error occured while reading input
374 int km_dokey (int menu)
377 struct keymap_t *map = Keymaps[menu];
384 return (retry_generic (menu, NULL, 0, 0));
388 /* ncurses doesn't return on resized screen when timeout is set to zero */
389 if (menu != MENU_EDITOR)
390 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
394 if (menu != MENU_EDITOR)
395 timeout (-1); /* restore blocking operation */
401 /* do we have an op already? */
405 struct binding_t *bindings;
407 /* is this a valid op for this menu? */
408 if ((bindings = km_get_table (menu)) &&
409 (func = get_func (bindings, tmp.op)))
412 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
415 if (menu != MENU_EDITOR && menu != MENU_PAGER)
417 /* check generic menu */
418 bindings = OpGeneric;
419 if ((func = get_func (bindings, tmp.op)))
423 /* Sigh. Valid function but not in this context.
424 * Find the literal string and push it back */
425 for (i = 0; Menus[i].name; i++)
427 bindings = km_get_table (Menus[i].value);
430 func = get_func (bindings, tmp.op);
433 /* careful not to feed the <..> as one token. otherwise
434 * push_string() will push the bogus op right back! */
435 mutt_ungetch ('>', 0);
437 mutt_ungetch ('<', 0);
442 /* continue to chew */
447 /* Nope. Business as usual */
448 while (LastKey > map->keys[pos])
450 if (pos > map->eq || !map->next)
451 return (retry_generic (menu, map->keys, pos, LastKey));
455 if (LastKey != map->keys[pos])
456 return (retry_generic (menu, map->keys, pos, LastKey));
458 if (++pos == map->len)
461 if (map->op != OP_MACRO)
467 mutt_error _("Macro loop detected.");
471 push_string (map->macro);
480 static void create_bindings (struct binding_t *map, int menu)
484 for (i = 0 ; map[i].name ; i++)
486 km_bindkey (map[i].seq, menu, map[i].op);
489 char *km_keyname (int c)
494 if ((p = mutt_getnamebyvalue (c, KeyNames)))
497 if (c < 256 && c > -128 && iscntrl ((unsigned char) c))
505 buf[1] = (c + '@') & 0x7f;
509 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
511 else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */
512 sprintf (buf, "<F%d>", c - KEY_F0);
513 else if (IsPrint (c))
514 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
516 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
520 int km_expand_key (char *s, size_t len, struct keymap_t *map)
530 strfcpy (s, km_keyname (map->keys[p]), len);
531 len -= (l = mutt_strlen (s));
533 if (++p >= map->len || !len)
542 struct keymap_t *km_find_func (int menu, int func)
544 struct keymap_t *map = Keymaps[menu];
546 for (; map; map = map->next)
554 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
556 create_bindings (OpAttach, MENU_ATTACH);
557 create_bindings (OpBrowser, MENU_FOLDER);
558 create_bindings (OpCompose, MENU_COMPOSE);
559 create_bindings (OpMain, MENU_MAIN);
560 create_bindings (OpPager, MENU_PAGER);
561 create_bindings (OpPost, MENU_POST);
562 create_bindings (OpQuery, MENU_QUERY);
563 create_bindings (OpAlias, MENU_ALIAS);
566 if ((WithCrypto & APPLICATION_PGP))
567 create_bindings (OpPgp, MENU_PGP);
569 if ((WithCrypto & APPLICATION_SMIME))
570 create_bindings (OpSmime, MENU_SMIME);
572 #ifdef CRYPT_BACKEND_GPGME
573 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
574 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
578 create_bindings (OpMix, MENU_MIX);
580 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
581 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
582 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
585 /* bindings for the line editor */
586 create_bindings (OpEditor, MENU_EDITOR);
588 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
589 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
590 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
591 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
592 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
593 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
594 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
595 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
596 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
598 /* generic menu keymap */
599 create_bindings (OpGeneric, MENU_GENERIC);
601 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
602 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
603 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
604 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
605 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
606 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
607 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
608 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
609 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
610 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
611 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
612 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
613 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
614 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
615 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
616 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
617 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
619 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
621 /* Miscellaneous extra bindings */
623 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
624 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
625 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
626 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
627 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
628 km_bindkey ("x", MENU_MAIN, OP_EXIT);
630 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
632 km_bindkey ("x", MENU_PAGER, OP_EXIT);
633 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
634 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
635 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
636 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
637 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
638 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
639 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
640 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
641 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
642 km_bindkey ("1", MENU_PAGER, OP_JUMP);
643 km_bindkey ("2", MENU_PAGER, OP_JUMP);
644 km_bindkey ("3", MENU_PAGER, OP_JUMP);
645 km_bindkey ("4", MENU_PAGER, OP_JUMP);
646 km_bindkey ("5", MENU_PAGER, OP_JUMP);
647 km_bindkey ("6", MENU_PAGER, OP_JUMP);
648 km_bindkey ("7", MENU_PAGER, OP_JUMP);
649 km_bindkey ("8", MENU_PAGER, OP_JUMP);
650 km_bindkey ("9", MENU_PAGER, OP_JUMP);
652 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
654 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
655 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
656 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
658 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
659 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
661 /* edit-to (default "t") hides generic tag-entry in Compose menu
662 This will bind tag-entry to "T" in the Compose menu */
663 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
666 void km_error_key (int menu)
668 char buf[SHORT_STRING];
669 struct keymap_t *key;
671 if(!(key = km_find_func(menu, OP_HELP)))
672 key = km_find_func(MENU_GENERIC, OP_HELP);
674 if(!(km_expand_key(buf, sizeof(buf), key)))
676 mutt_error _("Key is not bound.");
680 /* make sure the key is really the help key in this menu */
682 if (km_dokey (menu) != OP_HELP)
684 mutt_error _("Key is not bound.");
688 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
692 int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
696 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
699 strfcpy (err->data, _("push: too many arguments"), err->dsize);
703 push_string (buf->data);
707 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
708 static char *parse_keymap (int *menu, BUFFER *s, int maxmenus, int *nummenus, BUFFER *err)
714 memset (&buf, 0, sizeof (buf));
717 mutt_extract_token (&buf, s, 0);
727 if ((menu[i] = mutt_check_menu (p)) == -1)
729 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
740 mutt_extract_token (&buf, s, 0);
744 strfcpy (err->data, _("null key sequence"), err->dsize);
746 else if (MoreArgs (s))
751 strfcpy (err->data, _("too few arguments"), err->dsize);
759 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
763 for (i = 0; bindings[i].name; i++)
764 if (mutt_strcmp (func, bindings[i].name) == 0)
766 km_bindkey (key, menu, bindings[i].op);
772 struct binding_t *km_get_table (int menu)
798 return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;
800 #ifdef CRYPT_BACKEND_GPGME
801 case MENU_KEY_SELECT_PGP:
803 case MENU_KEY_SELECT_SMIME:
816 /* bind menu-name '<key_sequence>' function-name */
817 int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
819 struct binding_t *bindings = NULL;
821 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = 0, nummenus, i;
823 if ((key = parse_keymap (menu, s, sizeof (menu)/sizeof (menu[0]),
824 &nummenus, err)) == NULL)
827 /* function to execute */
828 mutt_extract_token (buf, s, 0);
831 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
834 else if (ascii_strcasecmp ("noop", buf->data) == 0)
836 for (i = 0; i < nummenus; ++i)
838 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
843 for (i = 0; i < nummenus; ++i)
845 /* First check the "generic" list of commands */
846 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
847 menu[i] == MENU_GENERIC ||
848 try_bind (key, menu[i], buf->data, OpGeneric) != 0)
850 /* Now check the menu-specific list of commands (if they exist) */
851 bindings = km_get_table (menu[i]);
852 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0)
854 snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data);
864 /* macro <menu> <key> <macro> <description> */
865 int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
867 int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = -1, nummenus, i;
871 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus, err)) == NULL)
874 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
875 /* make sure the macro sequence is not an empty string */
878 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
884 seq = safe_strdup (buf->data);
885 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
889 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
893 for (i = 0; i < nummenus; ++i)
895 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
904 for (i = 0; i < nummenus; ++i)
906 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
915 /* exec function-name */
916 int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
920 struct binding_t *bindings = NULL;
925 strfcpy (err->data, _("exec: no arguments"), err->dsize);
931 mutt_extract_token (buf, s, 0);
932 function = buf->data;
934 if ((bindings = km_get_table (CurrentMenu)) == NULL
935 && CurrentMenu != MENU_PAGER)
936 bindings = OpGeneric;
938 ops[nops] = get_op (bindings, function, mutt_strlen(function));
939 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
940 ops[nops] = get_op (OpGeneric, function, mutt_strlen(function));
942 if (ops[nops] == OP_NULL)
945 mutt_error (_("%s: no such function"), function);
950 while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0]));
953 mutt_ungetch(0, ops[--nops]);
959 * prompts the user to enter a keystroke, and displays the octal value back
962 void mutt_what_key (void)
966 mvprintw(LINES-1,0, _("Enter keys (^G to abort): "));
969 if (ch != ERR && ch != ctrl ('G'))
971 mutt_message(_("Char = %s, Octal = %o, Decimal = %d"),
972 km_keyname(ch), ch, ch);
975 while (ch != ERR && ch != ctrl ('G'));