2 * Copyright Neil Brown ©2015-2020 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
7 * A keymap maps a key to a command.
8 * Keys are ordered for fast binary-search lookup.
9 * A "key" is an arbitrary string which typically contains
10 * some 'mode' prefix and some specific tail.
11 * e.g emacs:A:C-X is Alt-Control-X in emacs mode.
12 * As far as the map is concerned, it is just a lexically order string.
14 * A 'command' is a struct provided by any of various
17 * A range can be stored by setting the lsb of the command pointer at
18 * the start of the range.
19 * When searching for a key we find the first entry that is not less
20 * than the target. If it is an exact match, use it. If previous entry
21 * exists and has the lsb set, then use that command.
23 * So to add a range, the start is entered with lsb set, and the end it
24 * entered with lsb clear.
26 * If a key is registered a second time, the new over-rides the old.
27 * This is particularly useful for registering a range, and then some
29 * To delete a key from a range we need to make two ranges, one that ends
30 * just before the new key, one that starts just after.
31 * The 'ends just before' is easy - we just add the new key or range.
32 * The 'starts just after' is managed by entering the same key twice.
33 * The first instance of the key has a 'lsb clear' command and is used for
34 * exact matches. The second instance has 'lsb set' and is used for everything
37 * A 'prefix' can be registered which creates a command which temporarily
38 * enabled the given prefix. It is applied to the next command, but is
39 * discarded after that. This is just a convenience function.
43 #define _GNU_SOURCE /* for asprintf */
52 inline static int qhash(char key, unsigned int start)
54 return (start ^ key) * 0x61C88647U;
58 unsigned long bloom[256 / (sizeof(unsigned long)*8) ];
62 char * safe *keys safe;
63 struct command * safe *comms safe;
66 static inline struct command * safe GETCOMM(struct command *c safe)
68 return (struct command * safe)(((unsigned long)c) & ~1UL);
71 static inline int IS_RANGE(struct command *c)
73 return ((unsigned long)c) & 1;
76 static inline struct command *SET_RANGE(struct command *c)
78 return (struct command *)(((unsigned long)c) | 1UL);
84 int len = ci->str ? strlen(ci->str) : 0;
87 if (ci->comm == &keymap_list)
88 /* should be impossible */
90 /* ci->comm MUST be the keymap */
91 m = (struct map* safe)ci->comm;
93 for (i = 0; i < m->size; i++)
94 if (!len || strncmp(ci->str, m->keys[i], len) == 0)
95 if (comm_call(ci->comm2, "cb", ci->focus,
96 IS_RANGE(m->comms[i]), NULL, m->keys[i]) <= 0)
101 static int size2alloc(int size)
103 /* Alway multiple of 8. */
104 return ((size-1) | 7) + 1;
107 struct map *safe key_alloc(void)
109 struct map *m = calloc(1, sizeof(*m));
111 key_add(m, "keymap:list", &keymap_list);
115 void key_free(struct map *m safe)
118 for (i = 0; i < m->size; i++) {
120 command_put(GETCOMM(m->comms[i]));
127 static void hash_str(const char *key safe, int len, unsigned int *hashes safe)
132 for (i = 0; (len < 0 || i < len) && key[i]; i++) {
133 h = qhash(key[i], h);
134 if (key[i] == '-' || key[i] == ':')
138 for (; (len < 0 || i < len) && key[i]; i++)
139 h = qhash(key[i], h);
143 inline static void set_bit(unsigned long *set safe, int bit)
145 set[bit/(sizeof(unsigned long)*8)] |=
146 1UL << (bit % (sizeof(unsigned long)*8));
149 inline static int test_bit(unsigned long *set safe, int bit)
151 return !! (set[bit/(sizeof(unsigned long)*8)] &
152 (1UL << (bit % (sizeof(unsigned long)*8))));
155 static bool key_present(struct map *map safe, unsigned int *hashes safe)
159 for (i = 0; i < map->size; i++) {
161 hash_str(map->keys[i], -1, h);
162 if (IS_RANGE(map->comms[i])) {
163 set_bit(map->bloom, h[1]&0xff);
164 set_bit(map->bloom, (h[1]>>8)&0xff);
165 set_bit(map->bloom, (h[1]>>16)&0xff);
167 set_bit(map->bloom, h[0]&0xff);
168 set_bit(map->bloom, (h[0]>>8)&0xff);
169 set_bit(map->bloom, (h[0]>>16)&0xff);
175 if (test_bit(map->bloom, hashes[0]&0xff) &&
176 test_bit(map->bloom, (hashes[0]>>8)&0xff) &&
177 test_bit(map->bloom, (hashes[0]>>16)&0xff))
179 if (test_bit(map->bloom, hashes[1]&0xff) &&
180 test_bit(map->bloom, (hashes[1]>>8)&0xff) &&
181 test_bit(map->bloom, (hashes[1]>>16)&0xff))
186 /* Find first entry >= k */
187 static int key_find(struct map *map safe, const char *k safe)
192 /* all entries before 'lo' are < k.
193 * all entries at 'hi' or later are >= k.
194 * So when lo==hi, hi is the answer.
197 int mid = (hi + lo)/ 2;
198 if (strcmp(map->keys[mid], k) < 0)
206 void key_add(struct map *map safe, const char *k safe, struct command *comm)
210 struct command *comm2 = NULL;
216 pos = key_find(map, k);
218 * 1/ match start of range: insert before
219 * 2/ match non-range start: replace
220 * 3/ not in range: insert before like 1
221 * 4/ in a range: insert match and range start.
223 if (pos >= map->size) {
224 /* Insert k,comm - default action */
225 } else if (strcmp(k, map->keys[pos]) == 0) {
226 /* match: need to check if range-start */
227 if (IS_RANGE(map->comms[pos])) {
228 /* Changing the start of a range,
229 * insert an exact match */
231 /* replace a non-range */
232 command_put(map->comms[pos]);
233 map->comms[pos] = command_get(comm);
236 } else if (pos > 0 && IS_RANGE(map->comms[pos-1])) {
237 /* insert within a range.
238 * Add given command as non-range match, and old command
241 comm2 = map->comms[pos-1];
243 /* Not in a range, simple insert */
246 ins_cnt = comm2 ? 2 : 1;
247 size = size2alloc(map->size + ins_cnt);
249 if (size2alloc(map->size) != size) {
250 map->keys = realloc(map->keys, size * sizeof(map->keys[0]));
251 map->comms = realloc(map->comms,
252 size * sizeof(struct command *));
255 memmove(map->keys+pos+ins_cnt, map->keys+pos,
256 (map->size - pos) * sizeof(map->keys[0]));
257 memmove(map->comms+pos+ins_cnt, map->comms+pos,
258 (map->size - pos) * sizeof(struct command *));
259 map->keys[pos] = strdup(k);
260 map->comms[pos] = command_get(comm);
262 map->keys[pos+1] = strdup(k);
263 map->comms[pos+1] = SET_RANGE(command_get(GETCOMM(comm2)));
265 map->size += ins_cnt;
269 void key_add_range(struct map *map safe,
270 const char *first safe, const char *last safe,
271 struct command *comm)
277 if (!comm || strcmp(first, last) >= 0)
280 /* Add the first entry using key_add */
281 key_add(map, first, comm);
282 pos = key_find(map, first);
283 pos2 = key_find(map, last);
285 /* Now 'pos' is a stand-alone entry for 'first'.
286 * If the entry before pos2 is a range start, update to start at 'last',
287 * else discard it, and discard everything else between pos and pos2.
288 * Then insert a stand-alone for 'last' and update 'pos' to be a
291 if (pos2 - 1 > pos && IS_RANGE(map->comms[pos2-1])) {
292 free(map->keys[pos2-1]);
293 map->keys[pos2-1] = strdup(last);
296 /* Need to insert 'last', and remove extras. so +1 and -(pos2-pos-1); */
297 move_size = 1 - (pos2 - pos - 1);
298 size = size2alloc(map->size + move_size);
299 if (size2alloc(map->size) < size) {
300 map->keys = realloc(map->keys, size * sizeof(map->keys[0]));
301 map->comms = realloc(map->comms,
302 size * sizeof(struct command *));
304 for (i = pos + 1; i < pos2; i++) {
306 command_put(GETCOMM(map->comms[i]));
308 memmove(map->keys+pos2 + move_size, map->keys+pos2,
309 (map->size - pos2) * sizeof(map->keys[0]));
310 memmove(map->comms+pos2+ move_size, map->comms+pos2,
311 (map->size - pos2) * sizeof(struct command *));
313 map->comms[pos] = SET_RANGE(comm);
314 map->keys[pos+1] = strdup(last);
315 map->comms[pos+1] = command_get(comm);
316 map->size += move_size;
320 void key_add_chain(struct map *map safe, struct map *chain)
329 void key_del(struct map *map, wint_t k)
333 pos = key_find(map, k, -1);
334 if (pos >= map->size || strcmp(map->keys[pos], k) == 0)
337 memmove(map->keys+pos, map->keys+pos+1,
338 (map->size-pos-1) * sizeof(map->keys[0]));
339 memmove(map->comms+pos, map->comms+pos+1,
340 (map->size-pos-1) * sizeof(struct command *));
346 int key_pfx_func(const struct cmd_info *ci safe)
348 struct pfx_cmd *m = container_of(ci->comm, struct pfx_cmd, c);
350 call("Mode:set-all", ci->focus, ci->num, NULL, m->pfx, ci->num2);
354 struct command *key_lookup_cmd(struct map *m safe, const char *c safe)
356 int pos = key_find(m, c);
360 else if (strcmp(m->keys[pos], c) == 0)
361 /* Exact match - use this entry */
362 return GETCOMM(m->comms[pos]);
363 else if (pos > 0 && IS_RANGE(m->comms[pos-1]))
364 /* In a range, use previous */
365 return GETCOMM(m->comms[pos-1]);
370 /* FIXME this makes lots of things non re-entrant */
371 static struct backtrace {
372 struct command *comm safe;
373 const struct cmd_info *ci safe;
374 struct backtrace *prev;
377 static char *mark_info(struct mark *m)
382 asprintf(&ret, "M-");
385 if (!mark_valid(m)) {
386 asprintf(&ret, "M-FREED");
389 ret = pane_call_ret(str, m->owner, "doc:debug:mark",
394 asprintf(&ret, "M:%x<%p>", m->seq, m);
400 struct backtrace *bt;
401 LOG("Start Backtrace:");
402 for (bt = backtrace; bt; bt = bt->prev) {
403 const struct cmd_info *ci = bt->ci;
404 struct command *h = ci->home->handle;
405 struct command *f = ci->focus->handle;
406 char *m1 = mark_info(ci->mark);
407 char *m2 = mark_info(ci->mark2);
409 LOG(" H:%s \"%s\" F:%s: %d %s \"%s\" %d %s \"%s\" (%d,%d) %s",
413 ci->num, m1, ci->str,
414 ci->num2, m2, ci->str2,
416 ci->comm2 ? ci->comm2->name : "");
420 LOG("End Backtrace");
423 static int do_comm_call(struct command *comm safe,
424 const struct cmd_info *ci safe)
433 ret = comm->func(ci);
438 int key_lookup(struct map *m safe, const struct cmd_info *ci safe)
440 struct command *comm;
442 if (ci->hash && !key_present(m, ci->hash)) {
443 stat_count("bloom-miss");
447 comm = key_lookup_cmd(m, ci->key);
449 stat_count("bloom-hit-bad");
452 stat_count("bloom-hit-good");
453 ((struct cmd_info*)ci)->comm = comm;
455 if (comm->func == keymap_list_func)
456 ((struct cmd_info*)ci)->comm = (struct command *safe)m;
458 return do_comm_call(comm, ci);
462 int key_lookup_prefix(struct map *m safe, const struct cmd_info *ci safe)
464 int pos = key_find(m, ci->key);
465 struct command *comm, *prev = NULL;
466 int len = strlen(ci->key);
467 const char *k = ci->key;
470 while (ret == 0 && pos < m->size &&
471 strncmp(m->keys[pos], k, len) == 0) {
472 comm = GETCOMM(m->comms[pos]);
473 if (comm && comm != prev) {
474 ((struct cmd_info*)ci)->comm = comm;
475 ((struct cmd_info*)ci)->key = m->keys[pos];
476 ret = do_comm_call(comm, ci);
477 ASSERT(ret >= 0 || ret < Eunused);
482 ((struct cmd_info*)ci)->key = k;
486 int key_lookup_cmd_func(const struct cmd_info *ci safe)
488 struct lookup_cmd *l = container_of(ci->comm, struct lookup_cmd, c);
489 struct map *m = safe_cast *l->m;
490 int ret = key_lookup(m, ci);
492 while (!ret && m->chain) {
494 ret = key_lookup(m, ci);
499 /* key_handle. Search towards root for the pane which handles the command.
501 int key_handle(const struct cmd_info *ci safe)
503 struct cmd_info *vci = (struct cmd_info*)ci;
505 unsigned int hash[2];
507 time_start_key(ci->key);
508 if ((void*) ci->comm) {
509 int ret = do_comm_call(ci->comm, ci);
510 time_stop_key(ci->key);
514 hash_str(ci->key, -1, hash);
517 /* If 'home' is set, search from there, else search
525 int ret = Efallthrough;
526 if (p->handle && !(p->damaged & DAMAGED_DEAD)) {
528 vci->comm = p->handle;
529 /* Don't add this to the call stack as it
530 * should simple call the desired function and
531 * that will appear on the call stack.
533 ret = p->handle->func(ci);
535 if (ret != Efallthrough) {
536 time_stop_key(ci->key);
537 /* 'p' might have been destroyed */
545 time_stop_key(ci->key);