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.
15 #include "mutt_menu.h"
16 #include "mutt_curses.h"
19 #include "mutt_crypt.h"
25 #include "functions.h"
27 struct mapping_t Menus[] = {
28 {"alias", MENU_ALIAS},
29 {"attach", MENU_ATTACH},
30 {"browser", MENU_FOLDER},
31 {"compose", MENU_COMPOSE},
32 {"editor", MENU_EDITOR},
34 {"pager", MENU_PAGER},
35 {"postpone", MENU_POST},
37 {"smime", MENU_SMIME},
40 {"key_select_pgp", MENU_KEY_SELECT_PGP},
41 {"key_select_smime", MENU_KEY_SELECT_SMIME},
50 {"query", MENU_QUERY},
51 {"generic", MENU_GENERIC},
55 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
57 static struct mapping_t KeyNames[] = {
58 {"<PageUp>", KEY_PPAGE},
59 {"<PageDown>", KEY_NPAGE},
62 {"<Right>", KEY_RIGHT},
65 {"<BackSpace>", KEY_BACKSPACE},
70 {"<Enter>", KEY_ENTER},
72 {"<Return>", M_ENTER_C},
77 {"<BackTab>", KEY_BTAB},
82 /* contains the last key the user pressed */
85 struct keymap_t *Keymaps[MENU_MAX];
87 static struct keymap_t *allocKeys (int len, keycode_t * keys)
91 p = safe_calloc (1, sizeof (struct keymap_t));
93 p->keys = safe_malloc (len * sizeof (keycode_t));
94 memcpy (p->keys, keys, len * sizeof (keycode_t));
98 static int parse_fkey (char *s)
103 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
106 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]) && s[4] == '>') {
126 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
131 static int parsekeys (char *str, keycode_t * d, int max)
134 char buff[SHORT_STRING];
138 strfcpy (buff, str, sizeof (buff));
143 if (*s == '<' && (t = strchr (s, '>'))) {
148 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
152 else if ((n = parse_fkey (s)) > 0) {
156 else if ((n = parse_keycode (s)) > 0) {
165 *d = (unsigned char) *s;
175 /* insert a key sequence into the specified map. the map is sorted by ASCII
176 * value (lowest to highest)
178 void km_bind (char *s, int menu, int op, char *macro, char *descr)
180 struct keymap_t *map, *tmp, *last = NULL, *next;
181 keycode_t buf[MAX_SEQ];
182 int len, pos = 0, lastpos = 0;
184 len = parsekeys (s, buf, MAX_SEQ);
186 map = allocKeys (len, buf);
188 map->macro = safe_strdup (macro);
189 map->descr = safe_strdup (descr);
194 if (pos >= len || pos >= tmp->len) {
195 /* map and tmp match, but have different lengths, so overwrite */
205 while (tmp && len >= pos);
209 else if (buf[pos] == tmp->keys[pos])
211 else if (buf[pos] < tmp->keys[pos]) {
212 /* found location to insert between last and tmp */
216 else { /* buf[pos] > tmp->keys[pos] */
235 void km_bindkey (char *s, int menu, int op)
237 km_bind (s, menu, op, NULL, NULL);
240 static int get_op (struct binding_t *bindings, const char *start, size_t len)
244 for (i = 0; bindings[i].name; i++) {
245 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
246 mutt_strlen (bindings[i].name) == len)
247 return bindings[i].op;
253 static char *get_func (struct binding_t *bindings, int op)
257 for (i = 0; bindings[i].name; i++) {
258 if (bindings[i].op == op) {
259 return bindings[i].name;
266 static void push_string (char *s)
268 char *pp, *p = s + mutt_strlen (s) - 1;
273 /* if we see something like "<PageUp>", look to see if it is a real
274 function name and return the corresponding value */
276 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
278 if ((i = parse_fkey (pp)) > 0) {
279 mutt_ungetch (KEY_F (i), 0);
285 for (i = 0; KeyNames[i].name; i++) {
286 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
289 if (KeyNames[i].name) {
291 mutt_ungetch (KeyNames[i].value, 0);
296 /* See if it is a valid command
297 * skip the '<' and the '>' when comparing */
298 for (i = 0; Menus[i].name; i++) {
299 struct binding_t *binding = km_get_table (Menus[i].value);
302 op = get_op (binding, pp + 1, l - 2);
309 mutt_ungetch (0, op);
315 mutt_ungetch (*p--, 0);
319 static int retry_generic (int menu, keycode_t * keys, int keyslen,
322 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
324 mutt_ungetch (lastkey, 0);
325 for (; keyslen; keyslen--)
326 mutt_ungetch (keys[keyslen - 1], 0);
327 return (km_dokey (MENU_GENERIC));
329 if (menu != MENU_EDITOR) {
330 /* probably a good idea to flush input here so we can abort macros */
337 * >0 function to execute
338 * OP_NULL no function bound to key sequence
339 * -1 error occured while reading input
341 int km_dokey (int menu)
344 struct keymap_t *map = Keymaps[menu];
351 return (retry_generic (menu, NULL, 0, 0));
354 /* ncurses doesn't return on resized screen when timeout is set to zero */
355 if (menu != MENU_EDITOR)
356 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
360 if (menu != MENU_EDITOR)
361 timeout (-1); /* restore blocking operation */
367 /* do we have an op already? */
370 struct binding_t *bindings;
372 /* is this a valid op for this menu? */
373 if ((bindings = km_get_table (menu)) &&
374 (func = get_func (bindings, tmp.op)))
377 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
380 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
381 /* check generic menu */
382 bindings = OpGeneric;
383 if ((func = get_func (bindings, tmp.op)))
387 /* Sigh. Valid function but not in this context.
388 * Find the literal string and push it back */
389 for (i = 0; Menus[i].name; i++) {
390 bindings = km_get_table (Menus[i].value);
392 func = get_func (bindings, tmp.op);
394 /* careful not to feed the <..> as one token. otherwise
395 * push_string() will push the bogus op right back! */
396 mutt_ungetch ('>', 0);
398 mutt_ungetch ('<', 0);
403 /* continue to chew */
408 /* Nope. Business as usual */
409 while (LastKey > map->keys[pos]) {
410 if (pos > map->eq || !map->next)
411 return (retry_generic (menu, map->keys, pos, LastKey));
415 if (LastKey != map->keys[pos])
416 return (retry_generic (menu, map->keys, pos, LastKey));
418 if (++pos == map->len) {
420 if (map->op != OP_MACRO)
425 mutt_error _("Macro loop detected.");
430 push_string (map->macro);
439 static void create_bindings (struct binding_t *map, int menu)
443 for (i = 0; map[i].name; i++)
445 km_bindkey (map[i].seq, menu, map[i].op);
448 char *km_keyname (int c)
453 if ((p = mutt_getnamebyvalue (c, KeyNames)))
456 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
462 buf[1] = (c + '@') & 0x7f;
466 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
468 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
469 sprintf (buf, "<F%d>", c - KEY_F0);
470 else if (IsPrint (c))
471 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
473 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
477 int km_expand_key (char *s, size_t len, struct keymap_t *map)
486 strfcpy (s, km_keyname (map->keys[p]), len);
487 len -= (l = mutt_strlen (s));
489 if (++p >= map->len || !len)
498 struct keymap_t *km_find_func (int menu, int func)
500 struct keymap_t *map = Keymaps[menu];
502 for (; map; map = map->next)
510 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
512 create_bindings (OpAttach, MENU_ATTACH);
513 create_bindings (OpBrowser, MENU_FOLDER);
514 create_bindings (OpCompose, MENU_COMPOSE);
515 create_bindings (OpMain, MENU_MAIN);
516 create_bindings (OpPager, MENU_PAGER);
517 create_bindings (OpPost, MENU_POST);
518 create_bindings (OpQuery, MENU_QUERY);
519 create_bindings (OpAlias, MENU_ALIAS);
522 if ((WithCrypto & APPLICATION_PGP))
523 create_bindings (OpPgp, MENU_PGP);
525 if ((WithCrypto & APPLICATION_SMIME))
526 create_bindings (OpSmime, MENU_SMIME);
528 #ifdef CRYPT_BACKEND_GPGME
529 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
530 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
534 create_bindings (OpMix, MENU_MIX);
536 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
537 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
538 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
541 /* bindings for the line editor */
542 create_bindings (OpEditor, MENU_EDITOR);
544 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
545 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
546 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
547 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
548 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
549 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
550 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
551 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
552 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
554 /* generic menu keymap */
555 create_bindings (OpGeneric, MENU_GENERIC);
557 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
558 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
559 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
560 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
561 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
562 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
563 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
564 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
565 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
566 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
567 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
568 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
569 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
570 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
571 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
572 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
573 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
575 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
577 /* Miscellaneous extra bindings */
579 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
580 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
581 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
582 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
583 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
584 km_bindkey ("x", MENU_MAIN, OP_EXIT);
586 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
588 km_bindkey ("x", MENU_PAGER, OP_EXIT);
589 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
590 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
591 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
592 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
593 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
594 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
595 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
596 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
597 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
598 km_bindkey ("1", MENU_PAGER, OP_JUMP);
599 km_bindkey ("2", MENU_PAGER, OP_JUMP);
600 km_bindkey ("3", MENU_PAGER, OP_JUMP);
601 km_bindkey ("4", MENU_PAGER, OP_JUMP);
602 km_bindkey ("5", MENU_PAGER, OP_JUMP);
603 km_bindkey ("6", MENU_PAGER, OP_JUMP);
604 km_bindkey ("7", MENU_PAGER, OP_JUMP);
605 km_bindkey ("8", MENU_PAGER, OP_JUMP);
606 km_bindkey ("9", MENU_PAGER, OP_JUMP);
608 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
610 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
611 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
612 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
614 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
615 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
617 /* edit-to (default "t") hides generic tag-entry in Compose menu
618 This will bind tag-entry to "T" in the Compose menu */
619 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
622 void km_error_key (int menu)
624 char buf[SHORT_STRING];
625 struct keymap_t *key;
627 if (!(key = km_find_func (menu, OP_HELP)))
628 key = km_find_func (MENU_GENERIC, OP_HELP);
630 if (!(km_expand_key (buf, sizeof (buf), key))) {
631 mutt_error _("Key is not bound.");
636 /* make sure the key is really the help key in this menu */
638 if (km_dokey (menu) != OP_HELP) {
639 mutt_error _("Key is not bound.");
644 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
648 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
653 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
655 strfcpy (err->data, _("push: too many arguments"), err->dsize);
659 push_string (buf->data);
663 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
664 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
671 memset (&buf, 0, sizeof (buf));
674 mutt_extract_token (&buf, s, 0);
677 while (i < maxmenus) {
682 if ((menu[i] = mutt_check_menu (p)) == -1) {
683 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
694 mutt_extract_token (&buf, s, 0);
697 strfcpy (err->data, _("null key sequence"), err->dsize);
699 else if (MoreArgs (s))
703 strfcpy (err->data, _("too few arguments"), err->dsize);
711 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
715 for (i = 0; bindings[i].name; i++)
716 if (mutt_strcmp (func, bindings[i].name) == 0) {
717 km_bindkey (key, menu, bindings[i].op);
723 struct binding_t *km_get_table (int menu)
748 return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
750 #ifdef CRYPT_BACKEND_GPGME
751 case MENU_KEY_SELECT_PGP:
753 case MENU_KEY_SELECT_SMIME:
766 /* bind menu-name '<key_sequence>' function-name */
767 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
770 struct binding_t *bindings = NULL;
772 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
775 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
776 &nummenus, err)) == NULL)
779 /* function to execute */
780 mutt_extract_token (buf, s, 0);
782 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
785 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
786 for (i = 0; i < nummenus; ++i) {
787 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
791 for (i = 0; i < nummenus; ++i) {
792 /* First check the "generic" list of commands */
793 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
794 menu[i] == MENU_GENERIC ||
795 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
796 /* Now check the menu-specific list of commands (if they exist) */
797 bindings = km_get_table (menu[i]);
798 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
799 snprintf (err->data, err->dsize, _("%s: no such function in map"),
810 /* macro <menu> <key> <macro> <description> */
811 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
814 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
820 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
824 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
825 /* make sure the macro sequence is not an empty string */
827 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
831 seq = safe_strdup (buf->data);
832 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
835 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
838 for (i = 0; i < nummenus; ++i) {
839 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
847 for (i = 0; i < nummenus; ++i) {
848 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
857 /* exec function-name */
858 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
863 struct binding_t *bindings = NULL;
867 strfcpy (err->data, _("exec: no arguments"), err->dsize);
872 mutt_extract_token (buf, s, 0);
873 function = buf->data;
875 if ((bindings = km_get_table (CurrentMenu)) == NULL
876 && CurrentMenu != MENU_PAGER)
877 bindings = OpGeneric;
879 ops[nops] = get_op (bindings, function, mutt_strlen (function));
880 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
881 ops[nops] = get_op (OpGeneric, function, mutt_strlen (function));
883 if (ops[nops] == OP_NULL) {
885 mutt_error (_("%s: no such function"), function);
890 while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
893 mutt_ungetch (0, ops[--nops]);
899 * prompts the user to enter a keystroke, and displays the octal value back
902 void mutt_what_key (void)
906 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
909 if (ch != ERR && ch != ctrl ('G')) {
910 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
911 km_keyname (ch), ch, ch);
914 while (ch != ERR && ch != ctrl ('G'));