grafthistory: comment about downloading the pack index
[elinks/elinks-j605.git] / src / bfu / hotkey.c
blob42222c449583e12cbd17675e14d0ae0ae5f1cac8
1 /* Hotkeys handling. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/hotkey.h"
12 #include "bfu/menu.h"
13 #include "config/kbdbind.h"
14 #include "intl/gettext/libintl.h"
15 #include "terminal/draw.h"
16 #include "terminal/terminal.h"
17 #include "terminal/window.h"
18 #include "util/conv.h"
19 #include "util/memory.h"
22 /* Return position (starting at 1) of the first tilde in text,
23 * or 0 if not found. */
24 static inline int
25 find_hotkey_pos(unsigned char *text)
27 if (text && *text) {
28 unsigned char *p = strchr(text, '~');
30 if (p) return (int) (p - text) + 1;
33 return 0;
36 void
37 init_hotkeys(struct terminal *term, struct menu *menu)
39 struct menu_item *mi;
41 #ifdef CONFIG_DEBUG
42 /* hotkey debugging */
43 if (menu->hotkeys) {
44 struct menu_item *used_hotkeys[255];
46 memset(used_hotkeys, 0, sizeof(used_hotkeys));
48 foreach_menu_item(mi, menu->items) {
49 unsigned char *text = mi->text;
51 if (!mi_has_left_text(mi)) continue;
52 if (mi_text_translate(mi)) text = _(text, term);
53 if (!*text) continue;
55 if (mi->hotkey_state != HKS_CACHED && !mi->hotkey_pos)
56 mi->hotkey_pos = find_hotkey_pos(text);
58 /* Negative value for hotkey_pos means the key is already
59 * used by another entry. We mark it to be able to highlight
60 * this hotkey in menus. --Zas */
61 if (mi->hotkey_pos) {
62 struct menu_item **used = &used_hotkeys[toupper(text[mi->hotkey_pos])];
64 if (*used) {
65 mi->hotkey_pos = -mi->hotkey_pos;
66 if ((*used)->hotkey_pos > 0)
67 (*used)->hotkey_pos = -(*used)->hotkey_pos;
70 *used = mi;
71 mi->hotkey_state = HKS_CACHED;
75 #endif
77 foreach_menu_item(mi, menu->items) {
78 if (!menu->hotkeys) {
79 mi->hotkey_pos = 0;
80 mi->hotkey_state = HKS_IGNORE;
81 } else if (mi->hotkey_state != HKS_CACHED
82 && !mi->hotkey_pos) {
83 unsigned char *text = mi->text;
85 if (!mi_has_left_text(mi)) continue;
86 if (mi_text_translate(mi)) text = _(text, term);
87 if (!*text) continue;
89 mi->hotkey_pos = find_hotkey_pos(text);
91 if (mi->hotkey_pos)
92 mi->hotkey_state = HKS_CACHED;
97 #ifdef CONFIG_NLS
98 void
99 clear_hotkeys_cache(struct menu *menu)
101 struct menu_item *item;
103 foreach_menu_item(item, menu->items) {
104 item->hotkey_state = menu->hotkeys ? HKS_SHOW : HKS_IGNORE;
105 item->hotkey_pos = 0;
108 #endif
110 void
111 refresh_hotkeys(struct terminal *term, struct menu *menu)
113 #ifdef CONFIG_NLS
114 if (current_language != menu->lang) {
115 clear_hotkeys_cache(menu);
116 init_hotkeys(term, menu);
117 menu->lang = current_language;
119 #else
120 init_hotkeys(term, menu);
121 #endif
124 /* Returns true if key (upcased) matches one of the hotkeys in menu */
125 static int
126 is_hotkey(struct menu_item *item, unsigned char key, struct terminal *term)
128 unsigned char *text;
129 int key_pos;
131 assert(item);
132 if_assert_failed return 0;
134 if (!mi_has_left_text(item)) return 0;
136 text = item->text;
137 if (mi_text_translate(item)) text = _(text, term);
138 if (!text || !*text) return 0;
140 key_pos = item->hotkey_pos;
142 #ifdef CONFIG_DEBUG
143 if (key_pos < 0) key_pos = -key_pos;
144 #endif
146 return (key_pos && (toupper(text[key_pos]) == key));
149 /* Returns true if key (upcased) matches first letter of menu item left text. */
150 static int
151 is_not_so_hotkey(struct menu_item *item, unsigned char key, struct terminal *term)
153 unsigned char *text;
155 assert(item);
156 if_assert_failed return 0;
158 if (!mi_has_left_text(item)) return 0;
160 text = item->text;
161 if (mi_text_translate(item)) text = _(text, term);
162 if (!text || !*text) return 0;
164 return (toupper(*text) == key);
167 static int
168 check_hotkeys_common(struct menu *menu, unsigned char hotkey, struct terminal *term,
169 int (*func)(struct menu_item *, unsigned char, struct terminal *))
171 unsigned char key = toupper(hotkey);
172 int i = menu->selected;
173 int start;
175 if (menu->size < 1) return 0;
177 i %= menu->size;
178 if (i < 0) i += menu->size;
180 start = i;
181 do {
182 if (++i == menu->size) i = 0;
184 if (func(&menu->items[i], key, term)) {
185 menu->selected = i;
186 return 1;
189 } while (i != start);
191 return 0;
194 /* Returns true if a hotkey was found in the menu, and set menu->selected. */
196 check_hotkeys(struct menu *menu, unsigned char key, struct terminal *term)
198 return check_hotkeys_common(menu, key, term, is_hotkey);
201 /* Search if first letter of an entry in menu matches the key (caseless comp.).
202 * It searchs in all entries, from selected entry to bottom and then from top
203 * to selected entry.
204 * It returns 1 if found and set menu->selected. */
206 check_not_so_hot_keys(struct menu *menu, unsigned char key, struct terminal *term)
208 return check_hotkeys_common(menu, key, term, is_not_so_hotkey);