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},
43 {"query", MENU_QUERY},
44 {"generic", MENU_GENERIC},
48 static struct mapping_t KeyNames[] = {
49 {"<PageUp>", KEY_PPAGE},
50 {"<PageDown>", KEY_NPAGE},
53 {"<Right>", KEY_RIGHT},
56 {"<BackSpace>", KEY_BACKSPACE},
61 {"<Enter>", KEY_ENTER},
63 {"<Return>", M_ENTER_C},
68 {"<BackTab>", KEY_BTAB},
76 /* contains the last key the user pressed */
79 struct keymap_t *Keymaps[MENU_MAX];
81 static struct keymap_t *allocKeys(int len, keycode_t *keys)
85 p = p_new(struct keymap_t, 1);
87 p->keys = p_dup(keys, len);
91 static int parse_fkey (const char *s)
95 if (s[0] != '<' || ascii_tolower(s[1]) != 'f')
98 n = strtol(s + 2, (char **)&s, 10);
99 return *s == '>' ? n : -1;
103 * This function parses the string <NNN> and uses the octal value as the key
106 static int parse_keycode (const char *s)
112 n = strtol(s + 1, (char **)&s, 8);
113 return *s == '>' ? n : -1;
116 static int parsekeys (const char *str, keycode_t * d, int max)
119 char buff[SHORT_STRING];
123 m_strcpy(buff, sizeof(buff), str);
128 if (*s == '<' && (t = strchr (s, '>'))) {
133 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
137 else if ((n = parse_fkey (s)) > 0) {
141 else if ((n = parse_keycode (s)) > 0) {
150 *d = (unsigned char) *s;
160 /* insert a key sequence into the specified map. the map is sorted by ASCII
161 * value (lowest to highest)
163 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
165 struct keymap_t *map, *tmp, *last = NULL, *next;
166 keycode_t buf[MAX_SEQ];
167 int len, pos = 0, lastpos = 0;
169 len = parsekeys (s, buf, MAX_SEQ);
171 map = allocKeys (len, buf);
173 map->macro = m_strdup(macro);
174 map->descr = m_strdup(descr);
179 if (pos >= len || pos >= tmp->len) {
180 /* map and tmp match, but have different lengths, so overwrite */
184 p_delete(&tmp->macro);
185 p_delete(&tmp->keys);
186 p_delete(&tmp->descr);
190 while (tmp && len >= pos);
194 else if (buf[pos] == tmp->keys[pos])
196 else if (buf[pos] < tmp->keys[pos]) {
197 /* found location to insert between last and tmp */
201 else { /* buf[pos] > tmp->keys[pos] */
220 void km_bindkey (const char *s, int menu, int op)
222 km_bind (s, menu, op, NULL, NULL);
225 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
229 for (i = 0; bindings[i].name; i++) {
230 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
231 m_strlen(bindings[i].name) == len)
232 return bindings[i].op;
238 static const char *get_func (struct binding_t *bindings, int op)
242 for (i = 0; bindings[i].name; i++) {
243 if (bindings[i].op == op) {
244 return bindings[i].name;
251 static void push_string(const char *s)
253 const char *pp, *p = s + m_strlen(s) - 1;
258 /* if we see something like "<PageUp>", look to see if it is a real
259 function name and return the corresponding value */
261 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
263 if ((i = parse_fkey (pp)) > 0) {
264 mutt_ungetch (KEY_F (i), 0);
270 for (i = 0; KeyNames[i].name; i++) {
271 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
274 if (KeyNames[i].name) {
276 mutt_ungetch (KeyNames[i].value, 0);
281 /* See if it is a valid command
282 * skip the '<' and the '>' when comparing */
283 for (i = 0; Menus[i].name; i++) {
284 struct binding_t *binding = km_get_table (Menus[i].value);
287 op = get_op (binding, pp + 1, l - 2);
294 mutt_ungetch (0, op);
300 mutt_ungetch ((unsigned char) *p--, 0);
304 static int retry_generic (int menu, keycode_t * keys, int keyslen,
307 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
309 mutt_ungetch (lastkey, 0);
310 for (; keyslen; keyslen--)
311 mutt_ungetch (keys[keyslen - 1], 0);
312 return (km_dokey (MENU_GENERIC));
314 if (menu != MENU_EDITOR) {
315 /* probably a good idea to flush input here so we can abort macros */
322 * >0 function to execute
323 * OP_NULL no function bound to key sequence
324 * -1 error occured while reading input
326 int km_dokey (int menu)
329 struct keymap_t *map = Keymaps[menu];
336 return (retry_generic (menu, NULL, 0, 0));
339 /* ncurses doesn't return on resized screen when timeout is set to zero */
340 if (menu != MENU_EDITOR)
341 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
345 if (menu != MENU_EDITOR)
346 timeout (-1); /* restore blocking operation */
352 /* do we have an op already? */
354 const char *func = NULL;
355 struct binding_t *bindings;
357 /* is this a valid op for this menu? */
358 if ((bindings = km_get_table (menu)) &&
359 (func = get_func (bindings, tmp.op)))
362 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
365 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
366 /* check generic menu */
367 bindings = OpGeneric;
368 if ((func = get_func (bindings, tmp.op)))
372 /* Sigh. Valid function but not in this context.
373 * Find the literal string and push it back */
374 for (i = 0; Menus[i].name; i++) {
375 bindings = km_get_table (Menus[i].value);
377 func = get_func (bindings, tmp.op);
379 /* careful not to feed the <..> as one token. otherwise
380 * push_string() will push the bogus op right back! */
381 mutt_ungetch ('>', 0);
383 mutt_ungetch ('<', 0);
388 /* continue to chew */
393 /* Nope. Business as usual */
394 while (LastKey > map->keys[pos]) {
395 if (pos > map->eq || !map->next)
396 return (retry_generic (menu, map->keys, pos, LastKey));
400 if (LastKey != map->keys[pos])
401 return (retry_generic (menu, map->keys, pos, LastKey));
403 if (++pos == map->len) {
405 if (map->op != OP_MACRO)
410 mutt_error _("Macro loop detected.");
415 push_string (map->macro);
424 static void create_bindings (struct binding_t *map, int menu)
428 for (i = 0; map[i].name; i++)
430 km_bindkey (map[i].seq, menu, map[i].op);
433 const char *km_keyname(int c)
438 if ((p = mutt_getnamebyvalue (c, KeyNames)))
441 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
447 buf[1] = (c + '@') & 0x7f;
451 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
453 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
454 sprintf (buf, "<F%d>", c - KEY_F0);
455 else if (isprint((unsigned char)c))
456 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
458 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
462 int km_expand_key (char *s, size_t len, struct keymap_t *map)
471 m_strcpy(s, len, km_keyname(map->keys[p]));
472 len -= (l = m_strlen(s));
474 if (++p >= map->len || !len)
483 struct keymap_t *km_find_func (int menu, int func)
485 struct keymap_t *map = Keymaps[menu];
487 for (; map; map = map->next)
495 p_clear(Keymaps, MENU_MAX);
497 create_bindings (OpAttach, MENU_ATTACH);
498 create_bindings (OpBrowser, MENU_FOLDER);
499 create_bindings (OpCompose, MENU_COMPOSE);
500 create_bindings (OpMain, MENU_MAIN);
501 create_bindings (OpPager, MENU_PAGER);
502 create_bindings (OpPost, MENU_POST);
503 create_bindings (OpQuery, MENU_QUERY);
504 create_bindings (OpAlias, MENU_ALIAS);
505 create_bindings (OpPgp, MENU_PGP);
506 create_bindings (OpSmime, MENU_SMIME);
508 #ifdef CRYPT_BACKEND_GPGME
509 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
510 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
514 create_bindings (OpMix, MENU_MIX);
516 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
517 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
518 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
521 /* bindings for the line editor */
522 create_bindings (OpEditor, MENU_EDITOR);
524 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
525 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
526 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
527 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
528 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
529 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
530 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
531 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
532 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
534 /* generic menu keymap */
535 create_bindings (OpGeneric, MENU_GENERIC);
537 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
538 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
539 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
540 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
541 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
542 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
543 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
544 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
545 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
546 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
547 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
548 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
549 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
550 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
551 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
552 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
553 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
555 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
557 /* Miscellaneous extra bindings */
559 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
560 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
561 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
562 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
563 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
564 km_bindkey ("x", MENU_MAIN, OP_EXIT);
566 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
568 km_bindkey ("x", MENU_PAGER, OP_EXIT);
569 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
570 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
571 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
572 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
573 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
574 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
575 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
576 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
577 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
578 km_bindkey ("1", MENU_PAGER, OP_JUMP);
579 km_bindkey ("2", MENU_PAGER, OP_JUMP);
580 km_bindkey ("3", MENU_PAGER, OP_JUMP);
581 km_bindkey ("4", MENU_PAGER, OP_JUMP);
582 km_bindkey ("5", MENU_PAGER, OP_JUMP);
583 km_bindkey ("6", MENU_PAGER, OP_JUMP);
584 km_bindkey ("7", MENU_PAGER, OP_JUMP);
585 km_bindkey ("8", MENU_PAGER, OP_JUMP);
586 km_bindkey ("9", MENU_PAGER, OP_JUMP);
588 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
590 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
591 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
592 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
594 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
595 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
597 /* edit-to (default "t") hides generic tag-entry in Compose menu
598 This will bind tag-entry to "T" in the Compose menu */
599 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
602 void km_error_key (int menu)
604 char buf[SHORT_STRING];
605 struct keymap_t *key;
607 if (!(key = km_find_func (menu, OP_HELP)))
608 key = km_find_func (MENU_GENERIC, OP_HELP);
610 if (!(km_expand_key (buf, sizeof (buf), key))) {
611 mutt_error _("Key is not bound.");
616 /* make sure the key is really the help key in this menu */
618 if (km_dokey (menu) != OP_HELP) {
619 mutt_error _("Key is not bound.");
624 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
628 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
633 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
635 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
639 push_string (buf->data);
643 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
644 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
654 mutt_extract_token (&buf, s, 0);
657 while (i < maxmenus) {
662 if ((menu[i] = mutt_getvaluebyname(p, Menus)) == -1) {
663 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
674 mutt_extract_token (&buf, s, 0);
677 m_strcpy(err->data, err->dsize, _("null key sequence"));
679 else if (MoreArgs (s))
683 m_strcpy(err->data, err->dsize, _("too few arguments"));
691 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
695 for (i = 0; bindings[i].name; i++)
696 if (m_strcmp(func, bindings[i].name) == 0) {
697 km_bindkey (key, menu, bindings[i].op);
703 struct binding_t *km_get_table (int menu)
729 #ifdef CRYPT_BACKEND_GPGME
730 case MENU_KEY_SELECT_PGP:
732 case MENU_KEY_SELECT_SMIME:
745 /* bind menu-name '<key_sequence>' function-name */
746 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
749 struct binding_t *bindings = NULL;
751 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
754 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
755 &nummenus, err)) == NULL)
758 /* function to execute */
759 mutt_extract_token (buf, s, 0);
761 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
764 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
765 for (i = 0; i < nummenus; ++i) {
766 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
770 for (i = 0; i < nummenus; ++i) {
771 /* First check the "generic" list of commands */
772 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
773 menu[i] == MENU_GENERIC ||
774 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
775 /* Now check the menu-specific list of commands (if they exist) */
776 bindings = km_get_table (menu[i]);
777 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
778 snprintf (err->data, err->dsize, _("%s: no such function in map"),
789 /* macro <menu> <key> <macro> <description> */
790 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
793 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
799 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
803 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
804 /* make sure the macro sequence is not an empty string */
806 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
810 seq = m_strdup(buf->data);
811 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
814 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
817 for (i = 0; i < nummenus; ++i) {
818 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
826 for (i = 0; i < nummenus; ++i) {
827 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
836 /* exec function-name */
837 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
842 struct binding_t *bindings = NULL;
846 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
851 mutt_extract_token (buf, s, 0);
852 function = buf->data;
854 if ((bindings = km_get_table (CurrentMenu)) == NULL
855 && CurrentMenu != MENU_PAGER)
856 bindings = OpGeneric;
858 ops[nops] = get_op (bindings, function, m_strlen(function));
859 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
860 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
862 if (ops[nops] == OP_NULL) {
864 mutt_error (_("%s: no such function"), function);
869 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
872 mutt_ungetch (0, ops[--nops]);
878 * prompts the user to enter a keystroke, and displays the octal value back
881 void mutt_what_key (void)
885 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
888 if (ch != ERR && ch != ctrl ('G')) {
889 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
890 km_keyname (ch), ch, ch);
893 while (ch != ERR && ch != ctrl ('G'));