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/lib-ui.h>
13 #include <lib-ui/menu.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},
39 {"query", MENU_QUERY},
40 {"generic", MENU_GENERIC},
44 static struct mapping_t KeyNames[] = {
45 {"<PageUp>", KEY_PPAGE},
46 {"<PageDown>", KEY_NPAGE},
49 {"<Right>", KEY_RIGHT},
52 {"<BackSpace>", KEY_BACKSPACE},
57 {"<Enter>", KEY_ENTER},
64 {"<BackTab>", KEY_BTAB},
72 /* contains the last key the user pressed */
75 struct keymap_t *Keymaps[MENU_MAX];
77 static struct keymap_t *allocKeys(int len, keycode_t *keys)
81 p = p_new(struct keymap_t, 1);
83 p->keys = p_dup(keys, len);
87 static int parse_fkey (const char *s)
91 if (s[0] != '<' || ascii_tolower(s[1]) != 'f')
94 n = strtol(s + 2, (char **)&s, 10);
95 return *s == '>' ? n : -1;
99 * This function parses the string <NNN> and uses the octal value as the key
102 static int parse_keycode (const char *s)
108 n = strtol(s + 1, (char **)&s, 8);
109 return *s == '>' ? n : -1;
112 static int parsekeys (const char *str, keycode_t * d, int max)
119 m_strcpy(buff, sizeof(buff), str);
124 if (*s == '<' && (t = strchr (s, '>'))) {
129 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
133 else if ((n = parse_fkey (s)) > 0) {
137 else if ((n = parse_keycode (s)) > 0) {
146 *d = (unsigned char) *s;
156 /* insert a key sequence into the specified map. the map is sorted by ASCII
157 * value (lowest to highest)
159 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
161 struct keymap_t *map, *tmp, *last = NULL, *next;
162 keycode_t buf[MAX_SEQ];
163 int len, pos = 0, lastpos = 0;
165 len = parsekeys (s, buf, MAX_SEQ);
167 map = allocKeys (len, buf);
169 map->macro = m_strdup(macro);
170 map->descr = m_strdup(descr);
175 if (pos >= len || pos >= tmp->len) {
176 /* map and tmp match, but have different lengths, so overwrite */
180 p_delete(&tmp->macro);
181 p_delete(&tmp->keys);
182 p_delete(&tmp->descr);
186 while (tmp && len >= pos);
190 else if (buf[pos] == tmp->keys[pos])
192 else if (buf[pos] < tmp->keys[pos]) {
193 /* found location to insert between last and tmp */
197 else { /* buf[pos] > tmp->keys[pos] */
216 void km_bindkey (const char *s, int menu, int op)
218 km_bind (s, menu, op, NULL, NULL);
221 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
225 for (i = 0; bindings[i].name; i++) {
226 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
227 m_strlen(bindings[i].name) == len)
228 return bindings[i].op;
234 static const char *get_func (struct binding_t *bindings, int op)
238 for (i = 0; bindings[i].name; i++) {
239 if (bindings[i].op == op) {
240 return bindings[i].name;
247 static void push_string(const char *s)
249 const char *pp, *p = s + m_strlen(s) - 1;
254 /* if we see something like "<PageUp>", look to see if it is a real
255 function name and return the corresponding value */
257 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
259 if ((i = parse_fkey (pp)) > 0) {
260 mutt_ungetch (KEY_F (i), 0);
266 for (i = 0; KeyNames[i].name; i++) {
267 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
270 if (KeyNames[i].name) {
272 mutt_ungetch (KeyNames[i].value, 0);
277 /* See if it is a valid command
278 * skip the '<' and the '>' when comparing */
279 for (i = 0; Menus[i].name; i++) {
280 struct binding_t *binding = km_get_table (Menus[i].value);
283 op = get_op (binding, pp + 1, l - 2);
290 mutt_ungetch (0, op);
296 mutt_ungetch ((unsigned char) *p--, 0);
300 static int retry_generic (int menu, keycode_t * keys, int keyslen,
303 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
305 mutt_ungetch (lastkey, 0);
306 for (; keyslen; keyslen--)
307 mutt_ungetch (keys[keyslen - 1], 0);
308 return (km_dokey (MENU_GENERIC));
310 if (menu != MENU_EDITOR) {
311 /* probably a good idea to flush input here so we can abort macros */
318 * >0 function to execute
319 * OP_NULL no function bound to key sequence
320 * -1 error occured while reading input
322 int km_dokey (int menu)
325 struct keymap_t *map = Keymaps[menu];
332 return (retry_generic (menu, NULL, 0, 0));
335 /* ncurses doesn't return on resized screen when timeout is set to zero */
336 if (menu != MENU_EDITOR)
337 wtimeout (main_w, (Timeout > 0 ? Timeout : 60) * 1000);
341 if (menu != MENU_EDITOR)
342 wtimeout (main_w, -1); /* restore blocking operation */
348 /* do we have an op already? */
350 const char *func = NULL;
351 struct binding_t *bindings;
353 /* is this a valid op for this menu? */
354 if ((bindings = km_get_table (menu)) &&
355 (func = get_func (bindings, tmp.op)))
358 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
361 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
362 /* check generic menu */
363 bindings = OpGeneric;
364 if ((func = get_func (bindings, tmp.op)))
368 /* Sigh. Valid function but not in this context.
369 * Find the literal string and push it back */
370 for (i = 0; Menus[i].name; i++) {
371 bindings = km_get_table (Menus[i].value);
373 func = get_func (bindings, tmp.op);
375 /* careful not to feed the <..> as one token. otherwise
376 * push_string() will push the bogus op right back! */
377 mutt_ungetch ('>', 0);
379 mutt_ungetch ('<', 0);
384 /* continue to chew */
389 /* Nope. Business as usual */
390 while (LastKey > map->keys[pos]) {
391 if (pos > map->eq || !map->next)
392 return (retry_generic (menu, map->keys, pos, LastKey));
396 if (LastKey != map->keys[pos])
397 return (retry_generic (menu, map->keys, pos, LastKey));
399 if (++pos == map->len) {
401 if (map->op != OP_MACRO)
406 mutt_error _("Macro loop detected.");
411 push_string (map->macro);
420 static void create_bindings (struct binding_t *map, int menu)
424 for (i = 0; map[i].name; i++)
426 km_bindkey (map[i].seq, menu, map[i].op);
429 const char *km_keyname(int c)
434 if ((p = mutt_getnamebyvalue (c, KeyNames)))
437 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
443 buf[1] = (c + '@') & 0x7f;
447 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
449 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
450 sprintf (buf, "<F%d>", c - KEY_F0);
451 else if (isprint((unsigned char)c))
452 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
454 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
458 int km_expand_key (char *s, size_t len, struct keymap_t *map)
467 m_strcpy(s, len, km_keyname(map->keys[p]));
468 len -= (l = m_strlen(s));
470 if (++p >= map->len || !len)
479 struct keymap_t *km_find_func (int menu, int func)
481 struct keymap_t *map = Keymaps[menu];
483 for (; map; map = map->next)
491 p_clear(Keymaps, MENU_MAX);
493 create_bindings (OpAttach, MENU_ATTACH);
494 create_bindings (OpBrowser, MENU_FOLDER);
495 create_bindings (OpCompose, MENU_COMPOSE);
496 create_bindings (OpMain, MENU_MAIN);
497 create_bindings (OpPager, MENU_PAGER);
498 create_bindings (OpPost, MENU_POST);
499 create_bindings (OpQuery, MENU_QUERY);
500 create_bindings (OpAlias, MENU_ALIAS);
501 create_bindings (OpPgp, MENU_PGP);
502 create_bindings (OpSmime, MENU_SMIME);
503 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
504 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
506 /* bindings for the line editor */
507 create_bindings (OpEditor, MENU_EDITOR);
509 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
510 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
511 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
512 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
513 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
514 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
515 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
516 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
517 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
519 /* generic menu keymap */
520 create_bindings (OpGeneric, MENU_GENERIC);
522 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
523 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
524 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
525 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
526 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
527 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
528 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
529 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
530 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
531 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
532 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
533 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
534 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
535 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
536 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
537 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
538 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
540 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
542 /* Miscellaneous extra bindings */
544 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
545 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
546 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
547 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
548 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
549 km_bindkey ("x", MENU_MAIN, OP_EXIT);
551 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
553 km_bindkey ("x", MENU_PAGER, OP_EXIT);
554 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
555 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
556 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
557 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
558 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
559 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
560 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
561 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
562 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
563 km_bindkey ("1", MENU_PAGER, OP_JUMP);
564 km_bindkey ("2", MENU_PAGER, OP_JUMP);
565 km_bindkey ("3", MENU_PAGER, OP_JUMP);
566 km_bindkey ("4", MENU_PAGER, OP_JUMP);
567 km_bindkey ("5", MENU_PAGER, OP_JUMP);
568 km_bindkey ("6", MENU_PAGER, OP_JUMP);
569 km_bindkey ("7", MENU_PAGER, OP_JUMP);
570 km_bindkey ("8", MENU_PAGER, OP_JUMP);
571 km_bindkey ("9", MENU_PAGER, OP_JUMP);
573 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
575 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
576 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
577 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
579 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
580 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
582 /* edit-to (default "t") hides generic tag-entry in Compose menu
583 This will bind tag-entry to "T" in the Compose menu */
584 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
587 void km_error_key (int menu)
590 struct keymap_t *key;
592 if (!(key = km_find_func (menu, OP_HELP)))
593 key = km_find_func (MENU_GENERIC, OP_HELP);
595 if (!(km_expand_key (buf, sizeof (buf), key))) {
596 mutt_error _("Key is not bound.");
601 /* make sure the key is really the help key in this menu */
603 if (km_dokey (menu) != OP_HELP) {
604 mutt_error _("Key is not bound.");
609 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
613 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
618 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
620 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
624 push_string (buf->data);
628 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
629 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
639 mutt_extract_token (&buf, s, 0);
642 while (i < maxmenus) {
647 if ((menu[i] = mutt_getvaluebyname(p, Menus)) == -1) {
648 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
659 mutt_extract_token (&buf, s, 0);
662 m_strcpy(err->data, err->dsize, _("null key sequence"));
664 else if (MoreArgs (s))
668 m_strcpy(err->data, err->dsize, _("too few arguments"));
676 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
680 for (i = 0; bindings[i].name; i++)
681 if (m_strcmp(func, bindings[i].name) == 0) {
682 km_bindkey (key, menu, bindings[i].op);
688 struct binding_t *km_get_table (int menu)
713 case MENU_KEY_SELECT_PGP:
715 case MENU_KEY_SELECT_SMIME:
721 /* bind menu-name '<key_sequence>' function-name */
722 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
725 struct binding_t *bindings = NULL;
727 int menu[countof(Menus) - 1], r =
730 if (!(key = parse_keymap(menu, s, countof(menu), &nummenus, err)))
733 /* function to execute */
734 mutt_extract_token (buf, s, 0);
736 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
739 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
740 for (i = 0; i < nummenus; ++i) {
741 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
745 for (i = 0; i < nummenus; ++i) {
746 /* First check the "generic" list of commands */
747 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
748 menu[i] == MENU_GENERIC ||
749 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
750 /* Now check the menu-specific list of commands (if they exist) */
751 bindings = km_get_table (menu[i]);
752 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
753 snprintf (err->data, err->dsize, _("%s: no such function in map"),
764 /* macro <menu> <key> <macro> <description> */
765 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
768 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
774 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
778 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
779 /* make sure the macro sequence is not an empty string */
781 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
785 seq = m_strdup(buf->data);
786 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
789 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
792 for (i = 0; i < nummenus; ++i) {
793 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
801 for (i = 0; i < nummenus; ++i) {
802 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
811 /* exec function-name */
812 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
817 struct binding_t *bindings = NULL;
821 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
826 mutt_extract_token (buf, s, 0);
827 function = buf->data;
829 if ((bindings = km_get_table (CurrentMenu)) == NULL
830 && CurrentMenu != MENU_PAGER)
831 bindings = OpGeneric;
833 ops[nops] = get_op (bindings, function, m_strlen(function));
834 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
835 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
837 if (ops[nops] == OP_NULL) {
839 mutt_error (_("%s: no such function"), function);
844 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
847 mutt_ungetch (0, ops[--nops]);