2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
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.
17 #include "mutt_menu.h"
18 #include "mutt_curses.h"
21 #include "mutt_crypt.h"
31 #include "functions.h"
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},
40 {"pager", MENU_PAGER},
41 {"postpone", MENU_POST},
43 {"smime", MENU_SMIME},
46 {"key_select_pgp", MENU_KEY_SELECT_PGP},
47 {"key_select_smime", MENU_KEY_SELECT_SMIME},
56 {"query", MENU_QUERY},
57 {"generic", MENU_GENERIC},
61 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
63 static struct mapping_t KeyNames[] = {
64 {"<PageUp>", KEY_PPAGE},
65 {"<PageDown>", KEY_NPAGE},
68 {"<Right>", KEY_RIGHT},
71 {"<BackSpace>", KEY_BACKSPACE},
76 {"<Enter>", KEY_ENTER},
78 {"<Return>", M_ENTER_C},
83 {"<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 = mem_calloc (1, sizeof (struct keymap_t));
102 p->keys = mem_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++) {
127 * This function parses the string <NNN> and uses the octal value as the key
130 static int parse_keycode (const char *s)
132 if (isdigit ((unsigned char) s[1]) &&
133 isdigit ((unsigned char) s[2]) &&
134 isdigit ((unsigned char) s[3]) && s[4] == '>') {
135 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
140 static int parsekeys (char *str, keycode_t * d, int max)
143 char buff[SHORT_STRING];
147 strfcpy (buff, str, sizeof (buff));
152 if (*s == '<' && (t = strchr (s, '>'))) {
157 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
161 else if ((n = parse_fkey (s)) > 0) {
165 else if ((n = parse_keycode (s)) > 0) {
174 *d = (unsigned char) *s;
184 /* insert a key sequence into the specified map. the map is sorted by ASCII
185 * value (lowest to highest)
187 void km_bind (char *s, int menu, int op, char *macro, char *descr)
189 struct keymap_t *map, *tmp, *last = NULL, *next;
190 keycode_t buf[MAX_SEQ];
191 int len, pos = 0, lastpos = 0;
193 len = parsekeys (s, buf, MAX_SEQ);
195 map = allocKeys (len, buf);
197 map->macro = str_dup (macro);
198 map->descr = str_dup (descr);
203 if (pos >= len || pos >= tmp->len) {
204 /* map and tmp match, but have different lengths, so overwrite */
208 mem_free (&tmp->macro);
209 mem_free (&tmp->keys);
210 mem_free (&tmp->descr);
214 while (tmp && len >= pos);
218 else if (buf[pos] == tmp->keys[pos])
220 else if (buf[pos] < tmp->keys[pos]) {
221 /* found location to insert between last and tmp */
225 else { /* buf[pos] > tmp->keys[pos] */
244 void km_bindkey (char *s, int menu, int op)
246 km_bind (s, menu, op, NULL, NULL);
249 static int get_op (struct binding_t *bindings, const char *start, size_t len)
253 for (i = 0; bindings[i].name; i++) {
254 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
255 str_len (bindings[i].name) == len)
256 return bindings[i].op;
262 static char *get_func (struct binding_t *bindings, int op)
266 for (i = 0; bindings[i].name; i++) {
267 if (bindings[i].op == op) {
268 return bindings[i].name;
275 static void push_string (char *s)
277 char *pp, *p = s + str_len (s) - 1;
282 /* if we see something like "<PageUp>", look to see if it is a real
283 function name and return the corresponding value */
285 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
287 if ((i = parse_fkey (pp)) > 0) {
288 mutt_ungetch (KEY_F (i), 0);
294 for (i = 0; KeyNames[i].name; i++) {
295 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
298 if (KeyNames[i].name) {
300 mutt_ungetch (KeyNames[i].value, 0);
305 /* See if it is a valid command
306 * skip the '<' and the '>' when comparing */
307 for (i = 0; Menus[i].name; i++) {
308 struct binding_t *binding = km_get_table (Menus[i].value);
311 op = get_op (binding, pp + 1, l - 2);
318 mutt_ungetch (0, op);
324 mutt_ungetch ((unsigned char) *p--, 0);
328 static int retry_generic (int menu, keycode_t * keys, int keyslen,
331 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
333 mutt_ungetch (lastkey, 0);
334 for (; keyslen; keyslen--)
335 mutt_ungetch (keys[keyslen - 1], 0);
336 return (km_dokey (MENU_GENERIC));
338 if (menu != MENU_EDITOR) {
339 /* probably a good idea to flush input here so we can abort macros */
346 * >0 function to execute
347 * OP_NULL no function bound to key sequence
348 * -1 error occured while reading input
350 int km_dokey (int menu)
353 struct keymap_t *map = Keymaps[menu];
360 return (retry_generic (menu, NULL, 0, 0));
363 /* ncurses doesn't return on resized screen when timeout is set to zero */
364 if (menu != MENU_EDITOR)
365 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
369 if (menu != MENU_EDITOR)
370 timeout (-1); /* restore blocking operation */
376 /* do we have an op already? */
379 struct binding_t *bindings;
381 /* is this a valid op for this menu? */
382 if ((bindings = km_get_table (menu)) &&
383 (func = get_func (bindings, tmp.op)))
386 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
389 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
390 /* check generic menu */
391 bindings = OpGeneric;
392 if ((func = get_func (bindings, tmp.op)))
396 /* Sigh. Valid function but not in this context.
397 * Find the literal string and push it back */
398 for (i = 0; Menus[i].name; i++) {
399 bindings = km_get_table (Menus[i].value);
401 func = get_func (bindings, tmp.op);
403 /* careful not to feed the <..> as one token. otherwise
404 * push_string() will push the bogus op right back! */
405 mutt_ungetch ('>', 0);
407 mutt_ungetch ('<', 0);
412 /* continue to chew */
417 /* Nope. Business as usual */
418 while (LastKey > map->keys[pos]) {
419 if (pos > map->eq || !map->next)
420 return (retry_generic (menu, map->keys, pos, LastKey));
424 if (LastKey != map->keys[pos])
425 return (retry_generic (menu, map->keys, pos, LastKey));
427 if (++pos == map->len) {
429 if (map->op != OP_MACRO)
434 mutt_error _("Macro loop detected.");
439 push_string (map->macro);
448 static void create_bindings (struct binding_t *map, int menu)
452 for (i = 0; map[i].name; i++)
454 km_bindkey (map[i].seq, menu, map[i].op);
457 char *km_keyname (int c)
462 if ((p = mutt_getnamebyvalue (c, KeyNames)))
465 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
471 buf[1] = (c + '@') & 0x7f;
475 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
477 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
478 sprintf (buf, "<F%d>", c - KEY_F0);
479 else if (IsPrint (c))
480 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
482 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
486 int km_expand_key (char *s, size_t len, struct keymap_t *map)
495 strfcpy (s, km_keyname (map->keys[p]), len);
496 len -= (l = str_len (s));
498 if (++p >= map->len || !len)
507 struct keymap_t *km_find_func (int menu, int func)
509 struct keymap_t *map = Keymaps[menu];
511 for (; map; map = map->next)
519 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
521 create_bindings (OpAttach, MENU_ATTACH);
522 create_bindings (OpBrowser, MENU_FOLDER);
523 create_bindings (OpCompose, MENU_COMPOSE);
524 create_bindings (OpMain, MENU_MAIN);
525 create_bindings (OpPager, MENU_PAGER);
526 create_bindings (OpPost, MENU_POST);
527 create_bindings (OpQuery, MENU_QUERY);
528 create_bindings (OpAlias, MENU_ALIAS);
531 if ((WithCrypto & APPLICATION_PGP))
532 create_bindings (OpPgp, MENU_PGP);
534 if ((WithCrypto & APPLICATION_SMIME))
535 create_bindings (OpSmime, MENU_SMIME);
537 #ifdef CRYPT_BACKEND_GPGME
538 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
539 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
543 create_bindings (OpMix, MENU_MIX);
545 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
546 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
547 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
550 /* bindings for the line editor */
551 create_bindings (OpEditor, MENU_EDITOR);
553 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
554 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
555 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
556 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
557 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
558 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
559 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
560 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
561 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
563 /* generic menu keymap */
564 create_bindings (OpGeneric, MENU_GENERIC);
566 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
567 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
568 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
569 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
570 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
571 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
572 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
573 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
574 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
575 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
576 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
577 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
578 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
579 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
580 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
581 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
582 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
584 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
586 /* Miscellaneous extra bindings */
588 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
589 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
590 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
591 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
592 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
593 km_bindkey ("x", MENU_MAIN, OP_EXIT);
595 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
597 km_bindkey ("x", MENU_PAGER, OP_EXIT);
598 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
599 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
600 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
601 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
602 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
603 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
604 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
605 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
606 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
607 km_bindkey ("1", MENU_PAGER, OP_JUMP);
608 km_bindkey ("2", MENU_PAGER, OP_JUMP);
609 km_bindkey ("3", MENU_PAGER, OP_JUMP);
610 km_bindkey ("4", MENU_PAGER, OP_JUMP);
611 km_bindkey ("5", MENU_PAGER, OP_JUMP);
612 km_bindkey ("6", MENU_PAGER, OP_JUMP);
613 km_bindkey ("7", MENU_PAGER, OP_JUMP);
614 km_bindkey ("8", MENU_PAGER, OP_JUMP);
615 km_bindkey ("9", MENU_PAGER, OP_JUMP);
617 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
619 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
620 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
621 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
623 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
624 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
626 /* edit-to (default "t") hides generic tag-entry in Compose menu
627 This will bind tag-entry to "T" in the Compose menu */
628 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
631 void km_error_key (int menu)
633 char buf[SHORT_STRING];
634 struct keymap_t *key;
636 if (!(key = km_find_func (menu, OP_HELP)))
637 key = km_find_func (MENU_GENERIC, OP_HELP);
639 if (!(km_expand_key (buf, sizeof (buf), key))) {
640 mutt_error _("Key is not bound.");
645 /* make sure the key is really the help key in this menu */
647 if (km_dokey (menu) != OP_HELP) {
648 mutt_error _("Key is not bound.");
653 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
657 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
662 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
664 strfcpy (err->data, _("push: too many arguments"), err->dsize);
668 push_string (buf->data);
672 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
673 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
680 memset (&buf, 0, sizeof (buf));
683 mutt_extract_token (&buf, s, 0);
686 while (i < maxmenus) {
691 if ((menu[i] = mutt_check_menu (p)) == -1) {
692 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
703 mutt_extract_token (&buf, s, 0);
706 strfcpy (err->data, _("null key sequence"), err->dsize);
708 else if (MoreArgs (s))
712 strfcpy (err->data, _("too few arguments"), err->dsize);
715 mem_free (&buf.data);
720 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
724 for (i = 0; bindings[i].name; i++)
725 if (str_cmp (func, bindings[i].name) == 0) {
726 km_bindkey (key, menu, bindings[i].op);
732 struct binding_t *km_get_table (int menu)
757 return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
759 #ifdef CRYPT_BACKEND_GPGME
760 case MENU_KEY_SELECT_PGP:
762 case MENU_KEY_SELECT_SMIME:
775 /* bind menu-name '<key_sequence>' function-name */
776 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
779 struct binding_t *bindings = NULL;
781 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
784 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
785 &nummenus, err)) == NULL)
788 /* function to execute */
789 mutt_extract_token (buf, s, 0);
791 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
794 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
795 for (i = 0; i < nummenus; ++i) {
796 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
800 for (i = 0; i < nummenus; ++i) {
801 /* First check the "generic" list of commands */
802 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
803 menu[i] == MENU_GENERIC ||
804 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
805 /* Now check the menu-specific list of commands (if they exist) */
806 bindings = km_get_table (menu[i]);
807 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
808 snprintf (err->data, err->dsize, _("%s: no such function in map"),
819 /* macro <menu> <key> <macro> <description> */
820 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
823 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
829 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
833 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
834 /* make sure the macro sequence is not an empty string */
836 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
840 seq = str_dup (buf->data);
841 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
844 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
847 for (i = 0; i < nummenus; ++i) {
848 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
856 for (i = 0; i < nummenus; ++i) {
857 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
866 /* exec function-name */
867 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
872 struct binding_t *bindings = NULL;
876 strfcpy (err->data, _("exec: no arguments"), err->dsize);
881 mutt_extract_token (buf, s, 0);
882 function = buf->data;
884 if ((bindings = km_get_table (CurrentMenu)) == NULL
885 && CurrentMenu != MENU_PAGER)
886 bindings = OpGeneric;
888 ops[nops] = get_op (bindings, function, str_len (function));
889 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
890 ops[nops] = get_op (OpGeneric, function, str_len (function));
892 if (ops[nops] == OP_NULL) {
894 mutt_error (_("%s: no such function"), function);
899 while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
902 mutt_ungetch (0, ops[--nops]);
908 * prompts the user to enter a keystroke, and displays the octal value back
911 void mutt_what_key (void)
915 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
918 if (ch != ERR && ch != ctrl ('G')) {
919 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
920 km_keyname (ch), ch, ch);
923 while (ch != ERR && ch != ctrl ('G'));