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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-ui/curses.h>
13 #include <lib-ui/menu.h>
17 #include <lib-crypt/crypt.h>
19 #define MUTT_FUNCTIONS_VALUES
20 #include "functions.def"
22 struct mapping_t Menus[] = {
23 {"alias", MENU_ALIAS},
24 {"attach", MENU_ATTACH},
25 {"browser", MENU_FOLDER},
26 {"compose", MENU_COMPOSE},
27 {"editor", MENU_EDITOR},
29 {"pager", MENU_PAGER},
30 {"postpone", MENU_POST},
32 {"smime", MENU_SMIME},
35 {"key_select_pgp", MENU_KEY_SELECT_PGP},
36 {"key_select_smime", MENU_KEY_SELECT_SMIME},
45 {"query", MENU_QUERY},
46 {"generic", MENU_GENERIC},
50 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
52 static struct mapping_t KeyNames[] = {
53 {"<PageUp>", KEY_PPAGE},
54 {"<PageDown>", KEY_NPAGE},
57 {"<Right>", KEY_RIGHT},
60 {"<BackSpace>", KEY_BACKSPACE},
65 {"<Enter>", KEY_ENTER},
67 {"<Return>", M_ENTER_C},
72 {"<BackTab>", KEY_BTAB},
80 /* contains the last key the user pressed */
83 struct keymap_t *Keymaps[MENU_MAX];
85 static struct keymap_t *allocKeys(int len, keycode_t *keys)
89 p = p_new(struct keymap_t, 1);
91 p->keys = p_dup(keys, len);
95 static int parse_fkey (const char *s)
100 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
103 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
115 * This function parses the string <NNN> and uses the octal value as the key
118 static int parse_keycode (const char *s)
120 if (isdigit ((unsigned char) s[1]) &&
121 isdigit ((unsigned char) s[2]) &&
122 isdigit ((unsigned char) s[3]) && s[4] == '>') {
123 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
128 static int parsekeys (const char *str, keycode_t * d, int max)
131 char buff[SHORT_STRING];
135 m_strcpy(buff, sizeof(buff), str);
140 if (*s == '<' && (t = strchr (s, '>'))) {
145 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
149 else if ((n = parse_fkey (s)) > 0) {
153 else if ((n = parse_keycode (s)) > 0) {
162 *d = (unsigned char) *s;
172 /* insert a key sequence into the specified map. the map is sorted by ASCII
173 * value (lowest to highest)
175 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
177 struct keymap_t *map, *tmp, *last = NULL, *next;
178 keycode_t buf[MAX_SEQ];
179 int len, pos = 0, lastpos = 0;
181 len = parsekeys (s, buf, MAX_SEQ);
183 map = allocKeys (len, buf);
185 map->macro = m_strdup(macro);
186 map->descr = m_strdup(descr);
191 if (pos >= len || pos >= tmp->len) {
192 /* map and tmp match, but have different lengths, so overwrite */
196 p_delete(&tmp->macro);
197 p_delete(&tmp->keys);
198 p_delete(&tmp->descr);
202 while (tmp && len >= pos);
206 else if (buf[pos] == tmp->keys[pos])
208 else if (buf[pos] < tmp->keys[pos]) {
209 /* found location to insert between last and tmp */
213 else { /* buf[pos] > tmp->keys[pos] */
232 void km_bindkey (const char *s, int menu, int op)
234 km_bind (s, menu, op, NULL, NULL);
237 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
241 for (i = 0; bindings[i].name; i++) {
242 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
243 m_strlen(bindings[i].name) == len)
244 return bindings[i].op;
250 static const char *get_func (struct binding_t *bindings, int op)
254 for (i = 0; bindings[i].name; i++) {
255 if (bindings[i].op == op) {
256 return bindings[i].name;
263 static void push_string(const char *s)
265 const char *pp, *p = s + m_strlen(s) - 1;
270 /* if we see something like "<PageUp>", look to see if it is a real
271 function name and return the corresponding value */
273 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
275 if ((i = parse_fkey (pp)) > 0) {
276 mutt_ungetch (KEY_F (i), 0);
282 for (i = 0; KeyNames[i].name; i++) {
283 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
286 if (KeyNames[i].name) {
288 mutt_ungetch (KeyNames[i].value, 0);
293 /* See if it is a valid command
294 * skip the '<' and the '>' when comparing */
295 for (i = 0; Menus[i].name; i++) {
296 struct binding_t *binding = km_get_table (Menus[i].value);
299 op = get_op (binding, pp + 1, l - 2);
306 mutt_ungetch (0, op);
312 mutt_ungetch ((unsigned char) *p--, 0);
316 static int retry_generic (int menu, keycode_t * keys, int keyslen,
319 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
321 mutt_ungetch (lastkey, 0);
322 for (; keyslen; keyslen--)
323 mutt_ungetch (keys[keyslen - 1], 0);
324 return (km_dokey (MENU_GENERIC));
326 if (menu != MENU_EDITOR) {
327 /* probably a good idea to flush input here so we can abort macros */
334 * >0 function to execute
335 * OP_NULL no function bound to key sequence
336 * -1 error occured while reading input
338 int km_dokey (int menu)
341 struct keymap_t *map = Keymaps[menu];
348 return (retry_generic (menu, NULL, 0, 0));
351 /* ncurses doesn't return on resized screen when timeout is set to zero */
352 if (menu != MENU_EDITOR)
353 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
357 if (menu != MENU_EDITOR)
358 timeout (-1); /* restore blocking operation */
364 /* do we have an op already? */
366 const char *func = NULL;
367 struct binding_t *bindings;
369 /* is this a valid op for this menu? */
370 if ((bindings = km_get_table (menu)) &&
371 (func = get_func (bindings, tmp.op)))
374 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
377 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
378 /* check generic menu */
379 bindings = OpGeneric;
380 if ((func = get_func (bindings, tmp.op)))
384 /* Sigh. Valid function but not in this context.
385 * Find the literal string and push it back */
386 for (i = 0; Menus[i].name; i++) {
387 bindings = km_get_table (Menus[i].value);
389 func = get_func (bindings, tmp.op);
391 /* careful not to feed the <..> as one token. otherwise
392 * push_string() will push the bogus op right back! */
393 mutt_ungetch ('>', 0);
395 mutt_ungetch ('<', 0);
400 /* continue to chew */
405 /* Nope. Business as usual */
406 while (LastKey > map->keys[pos]) {
407 if (pos > map->eq || !map->next)
408 return (retry_generic (menu, map->keys, pos, LastKey));
412 if (LastKey != map->keys[pos])
413 return (retry_generic (menu, map->keys, pos, LastKey));
415 if (++pos == map->len) {
417 if (map->op != OP_MACRO)
422 mutt_error _("Macro loop detected.");
427 push_string (map->macro);
436 static void create_bindings (struct binding_t *map, int menu)
440 for (i = 0; map[i].name; i++)
442 km_bindkey (map[i].seq, menu, map[i].op);
445 const char *km_keyname(int c)
450 if ((p = mutt_getnamebyvalue (c, KeyNames)))
453 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
459 buf[1] = (c + '@') & 0x7f;
463 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
465 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
466 sprintf (buf, "<F%d>", c - KEY_F0);
467 else if (isprint((unsigned char)c))
468 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
470 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
474 int km_expand_key (char *s, size_t len, struct keymap_t *map)
483 m_strcpy(s, len, km_keyname(map->keys[p]));
484 len -= (l = m_strlen(s));
486 if (++p >= map->len || !len)
495 struct keymap_t *km_find_func (int menu, int func)
497 struct keymap_t *map = Keymaps[menu];
499 for (; map; map = map->next)
507 p_clear(Keymaps, MENU_MAX);
509 create_bindings (OpAttach, MENU_ATTACH);
510 create_bindings (OpBrowser, MENU_FOLDER);
511 create_bindings (OpCompose, MENU_COMPOSE);
512 create_bindings (OpMain, MENU_MAIN);
513 create_bindings (OpPager, MENU_PAGER);
514 create_bindings (OpPost, MENU_POST);
515 create_bindings (OpQuery, MENU_QUERY);
516 create_bindings (OpAlias, MENU_ALIAS);
519 create_bindings (OpPgp, MENU_PGP);
520 create_bindings (OpSmime, MENU_SMIME);
522 #ifdef CRYPT_BACKEND_GPGME
523 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
524 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
528 create_bindings (OpMix, MENU_MIX);
530 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
531 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
532 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
535 /* bindings for the line editor */
536 create_bindings (OpEditor, MENU_EDITOR);
538 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
539 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
540 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
541 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
542 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
543 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
544 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
545 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
546 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
548 /* generic menu keymap */
549 create_bindings (OpGeneric, MENU_GENERIC);
551 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
552 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
553 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
554 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
555 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
556 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
557 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
558 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
559 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
560 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
561 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
562 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
563 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
564 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
565 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
566 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
567 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
569 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
571 /* Miscellaneous extra bindings */
573 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
574 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
575 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
576 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
577 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
578 km_bindkey ("x", MENU_MAIN, OP_EXIT);
580 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
582 km_bindkey ("x", MENU_PAGER, OP_EXIT);
583 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
584 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
585 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
586 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
587 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
588 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
589 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
590 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
591 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
592 km_bindkey ("1", MENU_PAGER, OP_JUMP);
593 km_bindkey ("2", MENU_PAGER, OP_JUMP);
594 km_bindkey ("3", MENU_PAGER, OP_JUMP);
595 km_bindkey ("4", MENU_PAGER, OP_JUMP);
596 km_bindkey ("5", MENU_PAGER, OP_JUMP);
597 km_bindkey ("6", MENU_PAGER, OP_JUMP);
598 km_bindkey ("7", MENU_PAGER, OP_JUMP);
599 km_bindkey ("8", MENU_PAGER, OP_JUMP);
600 km_bindkey ("9", MENU_PAGER, OP_JUMP);
602 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
604 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
605 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
606 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
608 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
609 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
611 /* edit-to (default "t") hides generic tag-entry in Compose menu
612 This will bind tag-entry to "T" in the Compose menu */
613 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
616 void km_error_key (int menu)
618 char buf[SHORT_STRING];
619 struct keymap_t *key;
621 if (!(key = km_find_func (menu, OP_HELP)))
622 key = km_find_func (MENU_GENERIC, OP_HELP);
624 if (!(km_expand_key (buf, sizeof (buf), key))) {
625 mutt_error _("Key is not bound.");
630 /* make sure the key is really the help key in this menu */
632 if (km_dokey (menu) != OP_HELP) {
633 mutt_error _("Key is not bound.");
638 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
642 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
647 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
649 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
653 push_string (buf->data);
657 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
658 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
668 mutt_extract_token (&buf, s, 0);
671 while (i < maxmenus) {
676 if ((menu[i] = mutt_check_menu (p)) == -1) {
677 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
688 mutt_extract_token (&buf, s, 0);
691 m_strcpy(err->data, err->dsize, _("null key sequence"));
693 else if (MoreArgs (s))
697 m_strcpy(err->data, err->dsize, _("too few arguments"));
705 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
709 for (i = 0; bindings[i].name; i++)
710 if (m_strcmp(func, bindings[i].name) == 0) {
711 km_bindkey (key, menu, bindings[i].op);
717 struct binding_t *km_get_table (int menu)
744 #ifdef CRYPT_BACKEND_GPGME
745 case MENU_KEY_SELECT_PGP:
747 case MENU_KEY_SELECT_SMIME:
760 /* bind menu-name '<key_sequence>' function-name */
761 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
764 struct binding_t *bindings = NULL;
766 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
769 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
770 &nummenus, err)) == NULL)
773 /* function to execute */
774 mutt_extract_token (buf, s, 0);
776 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
779 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
780 for (i = 0; i < nummenus; ++i) {
781 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
785 for (i = 0; i < nummenus; ++i) {
786 /* First check the "generic" list of commands */
787 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
788 menu[i] == MENU_GENERIC ||
789 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
790 /* Now check the menu-specific list of commands (if they exist) */
791 bindings = km_get_table (menu[i]);
792 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
793 snprintf (err->data, err->dsize, _("%s: no such function in map"),
804 /* macro <menu> <key> <macro> <description> */
805 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
808 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
814 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
818 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
819 /* make sure the macro sequence is not an empty string */
821 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
825 seq = m_strdup(buf->data);
826 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
829 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
832 for (i = 0; i < nummenus; ++i) {
833 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
841 for (i = 0; i < nummenus; ++i) {
842 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
851 /* exec function-name */
852 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
857 struct binding_t *bindings = NULL;
861 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
866 mutt_extract_token (buf, s, 0);
867 function = buf->data;
869 if ((bindings = km_get_table (CurrentMenu)) == NULL
870 && CurrentMenu != MENU_PAGER)
871 bindings = OpGeneric;
873 ops[nops] = get_op (bindings, function, m_strlen(function));
874 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
875 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
877 if (ops[nops] == OP_NULL) {
879 mutt_error (_("%s: no such function"), function);
884 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
887 mutt_ungetch (0, ops[--nops]);
893 * prompts the user to enter a keystroke, and displays the octal value back
896 void mutt_what_key (void)
900 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
903 if (ch != ERR && ch != ctrl ('G')) {
904 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
905 km_keyname (ch), ch, ch);
908 while (ch != ERR && ch != ctrl ('G'));