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 },
49 { "query", MENU_QUERY },
50 { "generic", MENU_GENERIC },
54 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
56 static struct mapping_t KeyNames[] = {
57 { "<PageUp>", KEY_PPAGE },
58 { "<PageDown>", KEY_NPAGE },
60 { "<Down>", KEY_DOWN },
61 { "<Right>", KEY_RIGHT },
62 { "<Left>", KEY_LEFT },
63 { "<Delete>", KEY_DC },
64 { "<BackSpace>",KEY_BACKSPACE },
65 { "<Insert>", KEY_IC },
66 { "<Home>", KEY_HOME },
69 { "<Enter>", KEY_ENTER },
71 { "<Return>", M_ENTER_C },
76 { "<BackTab>", KEY_BTAB },
81 /* contains the last key the user pressed */
84 struct keymap_t *Keymaps[MENU_MAX];
86 static struct keymap_t *allocKeys (int len, keycode_t *keys)
90 p = safe_calloc (1, sizeof (struct keymap_t));
92 p->keys = safe_malloc (len * sizeof (keycode_t));
93 memcpy (p->keys, keys, len * sizeof (keycode_t));
97 static int parse_fkey(char *s)
102 if(s[0] != '<' || ascii_tolower(s[1]) != 'f')
105 for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
118 * This function parses the string <NNN> and uses the octal value as the key
121 static int parse_keycode (const char *s)
123 if (isdigit ((unsigned char) s[1]) &&
124 isdigit ((unsigned char) s[2]) &&
125 isdigit ((unsigned char) s[3]) &&
128 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
133 static int parsekeys (char *str, keycode_t *d, int max)
136 char buff[SHORT_STRING];
140 strfcpy(buff, str, sizeof(buff));
146 if(*s == '<' && (t = strchr(s, '>')))
148 t++; c = *t; *t = '\0';
150 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
155 else if ((n = parse_fkey(s)) > 0)
160 else if ((n = parse_keycode(s)) > 0)
171 *d = (unsigned char)*s;
181 /* insert a key sequence into the specified map. the map is sorted by ASCII
182 * value (lowest to highest)
184 void km_bind (char *s, int menu, int op, char *macro, char *descr)
186 struct keymap_t *map, *tmp, *last = NULL, *next;
187 keycode_t buf[MAX_SEQ];
188 int len, pos = 0, lastpos = 0;
190 len = parsekeys (s, buf, MAX_SEQ);
192 map = allocKeys (len, buf);
194 map->macro = safe_strdup (macro);
195 map->descr = safe_strdup (descr);
201 if (pos >= len || pos >= tmp->len)
203 /* map and tmp match, but have different lengths, so overwrite */
214 while (tmp && len >= pos);
218 else if (buf[pos] == tmp->keys[pos])
220 else if (buf[pos] < tmp->keys[pos])
222 /* found location to insert between last and tmp */
226 else /* buf[pos] > tmp->keys[pos] */
246 void km_bindkey (char *s, int menu, int op)
248 km_bind (s, menu, op, NULL, NULL);
251 static int get_op (struct binding_t *bindings, const char *start, size_t len)
255 for (i = 0; bindings[i].name; i++)
257 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
258 mutt_strlen (bindings[i].name) == len)
259 return bindings[i].op;
265 static char *get_func (struct binding_t *bindings, int op)
269 for (i = 0; bindings[i].name; i++)
271 if (bindings[i].op == op) {
272 return bindings[i].name;
279 static void push_string (char *s)
281 char *pp, *p = s + mutt_strlen (s) - 1;
287 /* if we see something like "<PageUp>", look to see if it is a real
288 function name and return the corresponding value */
291 for (pp = p - 1; pp >= s && *pp != '<'; pp--)
295 if ((i = parse_fkey (pp)) > 0)
297 mutt_ungetch (KEY_F (i), 0);
303 for (i = 0; KeyNames[i].name; i++)
305 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
308 if (KeyNames[i].name)
311 mutt_ungetch (KeyNames[i].value, 0);
316 /* See if it is a valid command
317 * skip the '<' and the '>' when comparing */
318 for (i = 0; Menus[i].name; i++)
320 struct binding_t *binding = km_get_table (Menus[i].value);
323 op = get_op (binding, pp + 1, l - 2);
331 mutt_ungetch (0, op);
337 mutt_ungetch (*p--, 0);
341 static int retry_generic (int menu, keycode_t *keys, int keyslen, int lastkey)
343 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER)
346 mutt_ungetch (lastkey, 0);
347 for (; keyslen; keyslen--)
348 mutt_ungetch (keys[keyslen - 1], 0);
349 return (km_dokey (MENU_GENERIC));
351 if (menu != MENU_EDITOR)
353 /* probably a good idea to flush input here so we can abort macros */
360 * >0 function to execute
361 * OP_NULL no function bound to key sequence
362 * -1 error occured while reading input
364 int km_dokey (int menu)
367 struct keymap_t *map = Keymaps[menu];
374 return (retry_generic (menu, NULL, 0, 0));
378 /* ncurses doesn't return on resized screen when timeout is set to zero */
379 if (menu != MENU_EDITOR)
380 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
384 if (menu != MENU_EDITOR)
385 timeout (-1); /* restore blocking operation */
391 /* do we have an op already? */
395 struct binding_t *bindings;
397 /* is this a valid op for this menu? */
398 if ((bindings = km_get_table (menu)) &&
399 (func = get_func (bindings, tmp.op)))
402 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
405 if (menu != MENU_EDITOR && menu != MENU_PAGER)
407 /* check generic menu */
408 bindings = OpGeneric;
409 if ((func = get_func (bindings, tmp.op)))
413 /* Sigh. Valid function but not in this context.
414 * Find the literal string and push it back */
415 for (i = 0; Menus[i].name; i++)
417 bindings = km_get_table (Menus[i].value);
420 func = get_func (bindings, tmp.op);
423 /* careful not to feed the <..> as one token. otherwise
424 * push_string() will push the bogus op right back! */
425 mutt_ungetch ('>', 0);
427 mutt_ungetch ('<', 0);
432 /* continue to chew */
437 /* Nope. Business as usual */
438 while (LastKey > map->keys[pos])
440 if (pos > map->eq || !map->next)
441 return (retry_generic (menu, map->keys, pos, LastKey));
445 if (LastKey != map->keys[pos])
446 return (retry_generic (menu, map->keys, pos, LastKey));
448 if (++pos == map->len)
451 if (map->op != OP_MACRO)
457 mutt_error _("Macro loop detected.");
461 push_string (map->macro);
470 static void create_bindings (struct binding_t *map, int menu)
474 for (i = 0 ; map[i].name ; i++)
476 km_bindkey (map[i].seq, menu, map[i].op);
479 char *km_keyname (int c)
484 if ((p = mutt_getnamebyvalue (c, KeyNames)))
487 if (c < 256 && c > -128 && iscntrl ((unsigned char) c))
495 buf[1] = (c + '@') & 0x7f;
499 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
501 else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */
502 sprintf (buf, "<F%d>", c - KEY_F0);
503 else if (IsPrint (c))
504 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
506 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
510 int km_expand_key (char *s, size_t len, struct keymap_t *map)
520 strfcpy (s, km_keyname (map->keys[p]), len);
521 len -= (l = mutt_strlen (s));
523 if (++p >= map->len || !len)
532 struct keymap_t *km_find_func (int menu, int func)
534 struct keymap_t *map = Keymaps[menu];
536 for (; map; map = map->next)
544 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
546 create_bindings (OpAttach, MENU_ATTACH);
547 create_bindings (OpBrowser, MENU_FOLDER);
548 create_bindings (OpCompose, MENU_COMPOSE);
549 create_bindings (OpMain, MENU_MAIN);
550 create_bindings (OpPager, MENU_PAGER);
551 create_bindings (OpPost, MENU_POST);
552 create_bindings (OpQuery, MENU_QUERY);
553 create_bindings (OpAlias, MENU_ALIAS);
556 if ((WithCrypto & APPLICATION_PGP))
557 create_bindings (OpPgp, MENU_PGP);
559 if ((WithCrypto & APPLICATION_SMIME))
560 create_bindings (OpSmime, MENU_SMIME);
563 create_bindings (OpMix, MENU_MIX);
565 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
566 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
567 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
570 /* bindings for the line editor */
571 create_bindings (OpEditor, MENU_EDITOR);
573 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
574 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
575 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
576 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
577 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
578 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
579 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
580 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
581 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
583 /* generic menu keymap */
584 create_bindings (OpGeneric, MENU_GENERIC);
586 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
587 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
588 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
589 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
590 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
591 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
592 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
593 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
594 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
595 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
596 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
597 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
598 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
599 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
600 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
601 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
602 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
604 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
606 /* Miscellaneous extra bindings */
608 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
609 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
610 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
611 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
612 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
613 km_bindkey ("x", MENU_MAIN, OP_EXIT);
615 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
617 km_bindkey ("x", MENU_PAGER, OP_EXIT);
618 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
619 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
620 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
621 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
622 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
623 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
624 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
625 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
626 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
627 km_bindkey ("1", MENU_PAGER, OP_JUMP);
628 km_bindkey ("2", MENU_PAGER, OP_JUMP);
629 km_bindkey ("3", MENU_PAGER, OP_JUMP);
630 km_bindkey ("4", MENU_PAGER, OP_JUMP);
631 km_bindkey ("5", MENU_PAGER, OP_JUMP);
632 km_bindkey ("6", MENU_PAGER, OP_JUMP);
633 km_bindkey ("7", MENU_PAGER, OP_JUMP);
634 km_bindkey ("8", MENU_PAGER, OP_JUMP);
635 km_bindkey ("9", MENU_PAGER, OP_JUMP);
637 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
639 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
640 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
641 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
643 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
644 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
646 /* edit-to (default "t") hides generic tag-entry in Compose menu
647 This will bind tag-entry to "T" in the Compose menu */
648 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
651 void km_error_key (int menu)
653 char buf[SHORT_STRING];
654 struct keymap_t *key;
656 if(!(key = km_find_func(menu, OP_HELP)))
657 key = km_find_func(MENU_GENERIC, OP_HELP);
659 if(!(km_expand_key(buf, sizeof(buf), key)))
661 mutt_error _("Key is not bound.");
665 /* make sure the key is really the help key in this menu */
667 if (km_dokey (menu) != OP_HELP)
669 mutt_error _("Key is not bound.");
673 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
677 int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
681 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
684 strfcpy (err->data, _("push: too many arguments"), err->dsize);
688 push_string (buf->data);
692 /* expects to see: <menu-string> <key-string> */
693 char *parse_keymap (int *menu, BUFFER *s, BUFFER *err)
697 memset (&buf, 0, sizeof (buf));
700 mutt_extract_token (&buf, s, 0);
703 if ((*menu = mutt_check_menu (buf.data)) == -1)
705 snprintf (err->data, err->dsize, _("%s: no such menu"), buf.data);
710 mutt_extract_token (&buf, s, 0);
714 strfcpy (err->data, _("null key sequence"), err->dsize);
716 else if (MoreArgs (s))
722 strfcpy (err->data, _("too few arguments"), err->dsize);
729 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
733 for (i = 0; bindings[i].name; i++)
734 if (mutt_strcmp (func, bindings[i].name) == 0)
736 km_bindkey (key, menu, bindings[i].op);
742 struct binding_t *km_get_table (int menu)
768 return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;
779 /* bind menu-name '<key_sequence>' function-name */
780 int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
782 struct binding_t *bindings = NULL;
786 if ((key = parse_keymap (&menu, s, err)) == NULL)
789 /* function to execute */
790 mutt_extract_token (buf, s, 0);
793 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
796 else if (ascii_strcasecmp ("noop", buf->data) == 0)
797 km_bindkey (key, menu, OP_NULL); /* the `unbind' command */
800 /* First check the "generic" list of commands */
801 if (menu == MENU_PAGER || menu == MENU_EDITOR || menu == MENU_GENERIC ||
802 try_bind (key, menu, buf->data, OpGeneric) != 0)
804 /* Now check the menu-specific list of commands (if they exist) */
805 bindings = km_get_table (menu);
806 if (bindings && try_bind (key, menu, buf->data, bindings) != 0)
808 snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data);
817 /* macro <menu> <key> <macro> <description> */
818 int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
824 if ((key = parse_keymap (&menu, s, err)) == NULL)
827 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
828 /* make sure the macro sequence is not an empty string */
831 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
837 seq = safe_strdup (buf->data);
838 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
842 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
846 km_bind (key, menu, OP_MACRO, seq, buf->data);
854 km_bind (key, menu, OP_MACRO, buf->data, NULL);
862 /* exec function-name */
863 int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
867 struct binding_t *bindings = NULL;
872 strfcpy (err->data, _("exec: no arguments"), err->dsize);
878 mutt_extract_token (buf, s, 0);
879 function = buf->data;
881 if ((bindings = km_get_table (CurrentMenu)) == NULL
882 && CurrentMenu != MENU_PAGER)
883 bindings = OpGeneric;
885 ops[nops] = get_op (bindings, function, mutt_strlen(function));
886 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
887 ops[nops] = get_op (OpGeneric, function, mutt_strlen(function));
889 if (ops[nops] == OP_NULL)
892 mutt_error (_("%s: no such function"), function);
897 while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0]));
900 mutt_ungetch(0, ops[--nops]);
906 * prompts the user to enter a keystroke, and displays the octal value back
909 void mutt_what_key (void)
913 mvprintw(LINES-1,0, _("Enter keys (^G to abort): "));
916 if (ch != ERR && ch != ctrl ('G'))
918 mutt_message(_("Char = %s, Octal = %o, Decimal = %d"),
919 km_keyname(ch), ch, ch);
922 while (ch != ERR && ch != ctrl ('G'));