1 /**
2  * @file
3  * Manage keymappings
4  *
5  * @authors
6  * Copyright (C) 1996-2000,2002,2010 Michael R. Elkins <me@mutt.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef MUTT_KEYMAP_H
24 #define MUTT_KEYMAP_H
25 
26 #include "config.h"
27 #include <stddef.h>
28 #include <stdint.h>
29 #include "mutt/lib.h"
30 #include "core/lib.h"
31 #include "menu/lib.h"
32 
33 #define MUTT_UNBIND  1<<0
34 #define MUTT_UNMACRO 1<<1
35 /* maximal length of a key binding sequence used for buffer in km_bindkey */
36 #define MAX_SEQ 8
37 
38 /// Type for key storage, the rest of neomutt works fine with int type
39 typedef short keycode_t;
40 
41 void init_extended_keys(void);
42 
43 /**
44  * struct Keymap - A keyboard mapping
45  *
46  * entry in the keymap tree
47  */
48 struct Keymap
49 {
50   char *macro;                  ///< macro expansion (op == OP_MACRO)
51   char *desc;                   ///< description of a macro for the help menu
52   short op;                     ///< operation to perform
53   short eq;                     ///< number of leading keys equal to next entry
54   short len;                    ///< length of key sequence (unit: sizeof (keycode_t))
55   keycode_t *keys;              ///< key sequence
56   STAILQ_ENTRY(Keymap) entries; ///< next key in map
57 };
58 
59 STAILQ_HEAD(KeymapList, Keymap);
60 
61 /**
62  * struct KeyEvent - An event such as a keypress
63  */
64 struct KeyEvent
65 {
66   int ch; ///< raw key pressed
67   int op; ///< function op
68 };
69 
70 int km_expand_key(char *s, size_t len, struct Keymap *map);
71 struct Keymap *km_find_func(enum MenuType menu, int func);
72 void km_init(void);
73 void km_error_key(enum MenuType menu);
74 void mutt_what_key(void);
75 void mutt_init_abort_key(void);
76 int main_config_observer(struct NotifyCallback *nc);
77 
78 enum CommandResult km_bind(char *s, enum MenuType menu, int op, char *macro, char *desc);
79 int km_dokey(enum MenuType menu);
80 
81 extern struct KeymapList Keymaps[]; ///< Array of Keymap keybindings, one for each Menu
82 
83 extern int LastKey; ///< Last real key pressed, recorded by dokey()
84 extern keycode_t AbortKey; ///< key to abort edits etc, normally Ctrl-G
85 
86 extern const struct Mapping Menus[];
87 
88 /**
89  * struct Binding - Mapping between a user key and a function
90  */
91 struct Binding
92 {
93   const char *name; ///< name of the function
94   int op;           ///< function id number
95   const char *seq;  ///< default key binding
96 };
97 
98 /**
99  * struct EventBinding - A key binding Event
100  */
101 struct EventBinding
102 {
103   enum MenuType menu; ///< Menu, e.g. #MENU_PAGER
104   const char *key;    ///< Key string being bound (for new bind/macro)
105   int op;             ///< Operation the key's bound to (for bind), e.g. OP_DELETE
106 };
107 
108 /**
109  * enum NotifyBinding - Key Binding notification types
110  *
111  * Observers of #NT_BINDING will be passed an #EventBinding.
112  *
113  * @note Notifications are sent **after** the event.
114  */
115 enum NotifyBinding
116 {
117   NT_BINDING_ADD = 1,    ///< Key binding has been added
118   NT_BINDING_DELETE,     ///< Key binding has been deleted
119   NT_BINDING_DELETE_ALL, ///< All key bindings have been deleted
120 
121   NT_MACRO_ADD,          ///< Key macro has been added
122   NT_MACRO_DELETE,       ///< Key macro has been deleted
123   NT_MACRO_DELETE_ALL,   ///< All key macros have been deleted
124 };
125 
126 const struct Binding *km_get_table(enum MenuType menu);
127 const char *mutt_get_func(const struct Binding *bindings, int op);
128 
129 void mutt_keys_free(void);
130 
131 enum CommandResult mutt_parse_bind   (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
132 enum CommandResult mutt_parse_exec   (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
133 enum CommandResult mutt_parse_macro  (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
134 enum CommandResult mutt_parse_push   (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
135 enum CommandResult mutt_parse_unbind (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
136 enum CommandResult mutt_parse_unmacro(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err);
137 
138 #endif /* MUTT_KEYMAP_H */
139