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"
29 #include "functions.h"
31 struct mapping_t Menus[] = {
32 {"alias", MENU_ALIAS},
33 {"attach", MENU_ATTACH},
34 {"browser", MENU_FOLDER},
35 {"compose", MENU_COMPOSE},
36 {"editor", MENU_EDITOR},
38 {"pager", MENU_PAGER},
39 {"postpone", MENU_POST},
41 {"smime", MENU_SMIME},
44 {"key_select_pgp", MENU_KEY_SELECT_PGP},
45 {"key_select_smime", MENU_KEY_SELECT_SMIME},
54 {"query", MENU_QUERY},
55 {"generic", MENU_GENERIC},
59 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
61 static struct mapping_t KeyNames[] = {
62 {"<PageUp>", KEY_PPAGE},
63 {"<PageDown>", KEY_NPAGE},
66 {"<Right>", KEY_RIGHT},
69 {"<BackSpace>", KEY_BACKSPACE},
74 {"<Enter>", KEY_ENTER},
76 {"<Return>", M_ENTER_C},
81 {"<BackTab>", KEY_BTAB},
86 /* contains the last key the user pressed */
89 struct keymap_t *Keymaps[MENU_MAX];
91 static struct keymap_t *allocKeys (int len, keycode_t * keys)
95 p = safe_calloc (1, sizeof (struct keymap_t));
97 p->keys = safe_malloc (len * sizeof (keycode_t));
98 memcpy (p->keys, keys, len * sizeof (keycode_t));
102 static int parse_fkey (char *s)
107 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
110 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
122 * This function parses the string <NNN> and uses the octal value as the key
125 static int parse_keycode (const char *s)
127 if (isdigit ((unsigned char) s[1]) &&
128 isdigit ((unsigned char) s[2]) &&
129 isdigit ((unsigned char) s[3]) && s[4] == '>') {
130 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
135 static int parsekeys (char *str, keycode_t * d, int max)
138 char buff[SHORT_STRING];
142 strfcpy (buff, str, sizeof (buff));
147 if (*s == '<' && (t = strchr (s, '>'))) {
152 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
156 else if ((n = parse_fkey (s)) > 0) {
160 else if ((n = parse_keycode (s)) > 0) {
169 *d = (unsigned char) *s;
179 /* insert a key sequence into the specified map. the map is sorted by ASCII
180 * value (lowest to highest)
182 void km_bind (char *s, int menu, int op, char *macro, char *descr)
184 struct keymap_t *map, *tmp, *last = NULL, *next;
185 keycode_t buf[MAX_SEQ];
186 int len, pos = 0, lastpos = 0;
188 len = parsekeys (s, buf, MAX_SEQ);
190 map = allocKeys (len, buf);
192 map->macro = safe_strdup (macro);
193 map->descr = safe_strdup (descr);
198 if (pos >= len || pos >= tmp->len) {
199 /* map and tmp match, but have different lengths, so overwrite */
209 while (tmp && len >= pos);
213 else if (buf[pos] == tmp->keys[pos])
215 else if (buf[pos] < tmp->keys[pos]) {
216 /* found location to insert between last and tmp */
220 else { /* buf[pos] > tmp->keys[pos] */
239 void km_bindkey (char *s, int menu, int op)
241 km_bind (s, menu, op, NULL, NULL);
244 static int get_op (struct binding_t *bindings, const char *start, size_t len)
248 for (i = 0; bindings[i].name; i++) {
249 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
250 mutt_strlen (bindings[i].name) == len)
251 return bindings[i].op;
257 static char *get_func (struct binding_t *bindings, int op)
261 for (i = 0; bindings[i].name; i++) {
262 if (bindings[i].op == op) {
263 return bindings[i].name;
270 static void push_string (char *s)
272 char *pp, *p = s + mutt_strlen (s) - 1;
277 /* if we see something like "<PageUp>", look to see if it is a real
278 function name and return the corresponding value */
280 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
282 if ((i = parse_fkey (pp)) > 0) {
283 mutt_ungetch (KEY_F (i), 0);
289 for (i = 0; KeyNames[i].name; i++) {
290 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
293 if (KeyNames[i].name) {
295 mutt_ungetch (KeyNames[i].value, 0);
300 /* See if it is a valid command
301 * skip the '<' and the '>' when comparing */
302 for (i = 0; Menus[i].name; i++) {
303 struct binding_t *binding = km_get_table (Menus[i].value);
306 op = get_op (binding, pp + 1, l - 2);
313 mutt_ungetch (0, op);
319 mutt_ungetch (*p--, 0);
323 static int retry_generic (int menu, keycode_t * keys, int keyslen,
326 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
328 mutt_ungetch (lastkey, 0);
329 for (; keyslen; keyslen--)
330 mutt_ungetch (keys[keyslen - 1], 0);
331 return (km_dokey (MENU_GENERIC));
333 if (menu != MENU_EDITOR) {
334 /* probably a good idea to flush input here so we can abort macros */
341 * >0 function to execute
342 * OP_NULL no function bound to key sequence
343 * -1 error occured while reading input
345 int km_dokey (int menu)
348 struct keymap_t *map = Keymaps[menu];
355 return (retry_generic (menu, NULL, 0, 0));
358 /* ncurses doesn't return on resized screen when timeout is set to zero */
359 if (menu != MENU_EDITOR)
360 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
364 if (menu != MENU_EDITOR)
365 timeout (-1); /* restore blocking operation */
371 /* do we have an op already? */
374 struct binding_t *bindings;
376 /* is this a valid op for this menu? */
377 if ((bindings = km_get_table (menu)) &&
378 (func = get_func (bindings, tmp.op)))
381 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
384 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
385 /* check generic menu */
386 bindings = OpGeneric;
387 if ((func = get_func (bindings, tmp.op)))
391 /* Sigh. Valid function but not in this context.
392 * Find the literal string and push it back */
393 for (i = 0; Menus[i].name; i++) {
394 bindings = km_get_table (Menus[i].value);
396 func = get_func (bindings, tmp.op);
398 /* careful not to feed the <..> as one token. otherwise
399 * push_string() will push the bogus op right back! */
400 mutt_ungetch ('>', 0);
402 mutt_ungetch ('<', 0);
407 /* continue to chew */
412 /* Nope. Business as usual */
413 while (LastKey > map->keys[pos]) {
414 if (pos > map->eq || !map->next)
415 return (retry_generic (menu, map->keys, pos, LastKey));
419 if (LastKey != map->keys[pos])
420 return (retry_generic (menu, map->keys, pos, LastKey));
422 if (++pos == map->len) {
424 if (map->op != OP_MACRO)
429 mutt_error _("Macro loop detected.");
434 push_string (map->macro);
443 static void create_bindings (struct binding_t *map, int menu)
447 for (i = 0; map[i].name; i++)
449 km_bindkey (map[i].seq, menu, map[i].op);
452 char *km_keyname (int c)
457 if ((p = mutt_getnamebyvalue (c, KeyNames)))
460 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
466 buf[1] = (c + '@') & 0x7f;
470 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
472 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
473 sprintf (buf, "<F%d>", c - KEY_F0);
474 else if (IsPrint (c))
475 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
477 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
481 int km_expand_key (char *s, size_t len, struct keymap_t *map)
490 strfcpy (s, km_keyname (map->keys[p]), len);
491 len -= (l = mutt_strlen (s));
493 if (++p >= map->len || !len)
502 struct keymap_t *km_find_func (int menu, int func)
504 struct keymap_t *map = Keymaps[menu];
506 for (; map; map = map->next)
514 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
516 create_bindings (OpAttach, MENU_ATTACH);
517 create_bindings (OpBrowser, MENU_FOLDER);
518 create_bindings (OpCompose, MENU_COMPOSE);
519 create_bindings (OpMain, MENU_MAIN);
520 create_bindings (OpPager, MENU_PAGER);
521 create_bindings (OpPost, MENU_POST);
522 create_bindings (OpQuery, MENU_QUERY);
523 create_bindings (OpAlias, MENU_ALIAS);
526 if ((WithCrypto & APPLICATION_PGP))
527 create_bindings (OpPgp, MENU_PGP);
529 if ((WithCrypto & APPLICATION_SMIME))
530 create_bindings (OpSmime, MENU_SMIME);
532 #ifdef CRYPT_BACKEND_GPGME
533 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
534 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
538 create_bindings (OpMix, MENU_MIX);
540 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
541 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
542 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
545 /* bindings for the line editor */
546 create_bindings (OpEditor, MENU_EDITOR);
548 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
549 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
550 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
551 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
552 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
553 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
554 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
555 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
556 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
558 /* generic menu keymap */
559 create_bindings (OpGeneric, MENU_GENERIC);
561 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
562 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
563 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
564 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
565 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
566 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
567 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
568 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
569 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
570 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
571 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
572 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
573 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
574 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
575 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
576 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
577 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
579 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
581 /* Miscellaneous extra bindings */
583 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
584 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
585 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
586 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
587 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
588 km_bindkey ("x", MENU_MAIN, OP_EXIT);
590 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
592 km_bindkey ("x", MENU_PAGER, OP_EXIT);
593 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
594 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
595 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
596 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
597 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
598 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
599 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
600 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
601 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
602 km_bindkey ("1", MENU_PAGER, OP_JUMP);
603 km_bindkey ("2", MENU_PAGER, OP_JUMP);
604 km_bindkey ("3", MENU_PAGER, OP_JUMP);
605 km_bindkey ("4", MENU_PAGER, OP_JUMP);
606 km_bindkey ("5", MENU_PAGER, OP_JUMP);
607 km_bindkey ("6", MENU_PAGER, OP_JUMP);
608 km_bindkey ("7", MENU_PAGER, OP_JUMP);
609 km_bindkey ("8", MENU_PAGER, OP_JUMP);
610 km_bindkey ("9", MENU_PAGER, OP_JUMP);
612 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
614 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
615 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
616 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
618 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
619 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
621 /* edit-to (default "t") hides generic tag-entry in Compose menu
622 This will bind tag-entry to "T" in the Compose menu */
623 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
626 void km_error_key (int menu)
628 char buf[SHORT_STRING];
629 struct keymap_t *key;
631 if (!(key = km_find_func (menu, OP_HELP)))
632 key = km_find_func (MENU_GENERIC, OP_HELP);
634 if (!(km_expand_key (buf, sizeof (buf), key))) {
635 mutt_error _("Key is not bound.");
640 /* make sure the key is really the help key in this menu */
642 if (km_dokey (menu) != OP_HELP) {
643 mutt_error _("Key is not bound.");
648 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
652 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
657 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
659 strfcpy (err->data, _("push: too many arguments"), err->dsize);
663 push_string (buf->data);
667 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
668 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
675 memset (&buf, 0, sizeof (buf));
678 mutt_extract_token (&buf, s, 0);
681 while (i < maxmenus) {
686 if ((menu[i] = mutt_check_menu (p)) == -1) {
687 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
698 mutt_extract_token (&buf, s, 0);
701 strfcpy (err->data, _("null key sequence"), err->dsize);
703 else if (MoreArgs (s))
707 strfcpy (err->data, _("too few arguments"), err->dsize);
715 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
719 for (i = 0; bindings[i].name; i++)
720 if (mutt_strcmp (func, bindings[i].name) == 0) {
721 km_bindkey (key, menu, bindings[i].op);
727 struct binding_t *km_get_table (int menu)
752 return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
754 #ifdef CRYPT_BACKEND_GPGME
755 case MENU_KEY_SELECT_PGP:
757 case MENU_KEY_SELECT_SMIME:
770 /* bind menu-name '<key_sequence>' function-name */
771 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
774 struct binding_t *bindings = NULL;
776 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
779 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
780 &nummenus, err)) == NULL)
783 /* function to execute */
784 mutt_extract_token (buf, s, 0);
786 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
789 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
790 for (i = 0; i < nummenus; ++i) {
791 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
795 for (i = 0; i < nummenus; ++i) {
796 /* First check the "generic" list of commands */
797 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
798 menu[i] == MENU_GENERIC ||
799 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
800 /* Now check the menu-specific list of commands (if they exist) */
801 bindings = km_get_table (menu[i]);
802 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
803 snprintf (err->data, err->dsize, _("%s: no such function in map"),
814 /* macro <menu> <key> <macro> <description> */
815 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
818 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
824 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
828 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
829 /* make sure the macro sequence is not an empty string */
831 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
835 seq = safe_strdup (buf->data);
836 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
839 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
842 for (i = 0; i < nummenus; ++i) {
843 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
851 for (i = 0; i < nummenus; ++i) {
852 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
861 /* exec function-name */
862 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
867 struct binding_t *bindings = NULL;
871 strfcpy (err->data, _("exec: no arguments"), err->dsize);
876 mutt_extract_token (buf, s, 0);
877 function = buf->data;
879 if ((bindings = km_get_table (CurrentMenu)) == NULL
880 && CurrentMenu != MENU_PAGER)
881 bindings = OpGeneric;
883 ops[nops] = get_op (bindings, function, mutt_strlen (function));
884 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
885 ops[nops] = get_op (OpGeneric, function, mutt_strlen (function));
887 if (ops[nops] == OP_NULL) {
889 mutt_error (_("%s: no such function"), function);
894 while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
897 mutt_ungetch (0, ops[--nops]);
903 * prompts the user to enter a keystroke, and displays the octal value back
906 void mutt_what_key (void)
910 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
913 if (ch != ERR && ch != ctrl ('G')) {
914 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
915 km_keyname (ch), ch, ch);
918 while (ch != ERR && ch != ctrl ('G'));