Make struct action const
[elinks.git] / src / config / kbdbind.h
blob8facc19079c3743ec41e5cf52578ca8c1f589efe
1 #ifndef EL__CONFIG_KBDBIND_H
2 #define EL__CONFIG_KBDBIND_H
4 #include "config/options.h"
5 #include "main/event.h"
6 #include "main/object.h"
7 #include "terminal/terminal.h"
8 #include "util/string.h"
10 struct listbox_item;
11 struct module;
13 /* Used for holding enum <keymap>_action values. */
14 typedef long action_id_T;
16 enum keymap_id {
17 KEYMAP_INVALID = -1,
18 KEYMAP_MAIN,
19 KEYMAP_EDIT,
20 KEYMAP_MENU,
21 KEYMAP_MAX
24 struct action {
25 unsigned char *str;
26 action_id_T num;
27 enum keymap_id keymap_id;
28 unsigned char *desc;
29 unsigned int flags;
32 struct action_list {
33 const struct action *actions;
34 int num_actions;
36 struct keymap {
37 unsigned char *str;
38 enum keymap_id keymap_id;
39 unsigned char *desc;
42 enum action_flags {
43 ACTION_RESTRICT_ANONYMOUS = (1 << 16),
44 ACTION_REQUIRE_VIEW_STATE = (1 << 17),
45 ACTION_REQUIRE_LOCATION = (1 << 18),
46 ACTION_JUMP_TO_LINK = (1 << 19),
47 ACTION_REQUIRE_LINK = (1 << 20),
48 ACTION_REQUIRE_FORM = (1 << 21),
49 ACTION_FLAGS_MASK = (0xFF << 16),
52 /* Note: if you add anything here, please keep it in alphabetical order,
53 * and also update the table action_table[] in kbdbind.c. */
55 #define ACTION_(map, name, action, caption, flags) \
56 ACT_##map##_##action
58 enum main_action {
59 #include "config/actions-main.inc"
61 MAIN_ACTIONS,
64 enum edit_action {
65 #include "config/actions-edit.inc"
67 EDIT_ACTIONS
70 enum menu_action {
71 #include "config/actions-menu.inc"
73 MENU_ACTIONS
76 #undef ACTION_
78 enum kbdbind_flags {
79 KBDB_WATERMARK = 1,
80 KBDB_TOUCHED = 2,
82 /* Marks whether the binding has a key that is used
83 * by one of the default bindings. */
84 KBDB_DEFAULT_KEY = 4,
86 /* Marks whether the binding itself (the combination of key
87 * _and_ action) is default. */
88 KBDB_DEFAULT_BINDING = 8,
91 struct keybinding {
92 OBJECT_HEAD(struct keybinding);
94 enum keymap_id keymap_id;
95 action_id_T action_id;
96 struct term_event_keyboard kbd;
97 int event;
98 enum kbdbind_flags flags;
99 struct listbox_item *box_item;
103 struct keybinding *add_keybinding(enum keymap_id keymap_id, action_id_T action_id, struct term_event_keyboard *kbd, int event);
104 int keybinding_exists(enum keymap_id keymap_id, struct term_event_keyboard *kbd, action_id_T *action_id);
105 void free_keybinding(struct keybinding *);
107 const struct action *get_action(enum keymap_id keymap_id, action_id_T action_id);
108 unsigned char *get_action_name(enum keymap_id keymap_id, action_id_T action_id);
109 action_id_T get_action_from_string(enum keymap_id keymap_id, unsigned char *str);
110 unsigned char *get_action_name_from_keystroke(enum keymap_id keymap_id,
111 const unsigned char *keystroke_str);
113 static inline unsigned int
114 action_is_anonymous_safe(enum keymap_id keymap_id, action_id_T action_id)
116 const struct action *action = get_action(keymap_id, action_id);
118 return action && !(action->flags & ACTION_RESTRICT_ANONYMOUS);
121 static inline unsigned int
122 action_requires_view_state(enum keymap_id keymap_id, action_id_T action_id)
124 const struct action *action = get_action(keymap_id, action_id);
126 return action && (action->flags & ACTION_REQUIRE_VIEW_STATE);
129 static inline unsigned int
130 action_requires_location(enum keymap_id keymap_id, action_id_T action_id)
132 const struct action *action = get_action(keymap_id, action_id);
134 return action && (action->flags & ACTION_REQUIRE_LOCATION);
137 static inline unsigned int
138 action_prefix_is_link_number(enum keymap_id keymap_id, action_id_T action_id)
140 const struct action *action = get_action(keymap_id, action_id);
142 return action && (action->flags & ACTION_JUMP_TO_LINK);
145 static inline unsigned int
146 action_requires_link(enum keymap_id keymap_id, action_id_T action_id)
148 const struct action *action = get_action(keymap_id, action_id);
150 return action && (action->flags & ACTION_REQUIRE_LINK);
153 static inline unsigned int
154 action_requires_form(enum keymap_id keymap_id, action_id_T action_id)
156 const struct action *action = get_action(keymap_id, action_id);
158 return action && (action->flags & ACTION_REQUIRE_FORM);
161 term_event_key_T read_key(const unsigned char *);
162 unsigned char *get_keymap_name(enum keymap_id);
164 int parse_keystroke(const unsigned char *, struct term_event_keyboard *);
165 void add_keystroke_to_string(struct string *str, struct term_event_keyboard *kbd, int escape);
167 /* void add_accesskey_to_string(struct string *str, unicode_val_T accesskey); */
168 #define add_accesskey_to_string(str, accesskey) do { \
169 struct term_event_keyboard kbd; \
170 /* FIXME: #ifndef CONFIG_UTF8, kbd.key is encoded in \
171 * the charset of the terminal, so accesskey should be \
172 * converted from unicode_val_T to that. \
173 * #ifdef CONFIG_UTF8, the code is correct. */ \
174 kbd.key = accesskey; \
175 /* try_document_key() recognizes only Alt-accesskey \
176 * combos. */ \
177 kbd.modifier = KBD_MOD_ALT; \
178 add_keystroke_to_string(str, &kbd, 0); \
179 } while (0)
181 action_id_T kbd_action(enum keymap_id, struct term_event *, int *);
182 struct keybinding *kbd_ev_lookup(enum keymap_id, struct term_event_keyboard *kbd, int *);
183 struct keybinding *kbd_nm_lookup(enum keymap_id, unsigned char *);
185 int bind_do(unsigned char *, const unsigned char *, unsigned char *, int);
186 unsigned char *bind_act(unsigned char *, const unsigned char *);
187 void bind_config_string(struct string *);
189 #ifdef CONFIG_SCRIPTING
190 int bind_key_to_event_name(unsigned char *, const unsigned char *, unsigned char *,
191 unsigned char **);
192 #endif
194 void add_keystroke_action_to_string(struct string *string, action_id_T action_id, enum keymap_id keymap_id);
195 unsigned char *get_keystroke(action_id_T action_id, enum keymap_id keymap_id);
197 void add_actions_to_string(struct string *string, action_id_T actions[],
198 enum keymap_id keymap_id, struct terminal *term);
200 extern struct module kbdbind_module;
202 #endif