2 * Copyright Neil Brown ©2015-2019 <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' pr efix and some specific tail.
11 * e.g emacs-M-C-Chr-x is Meta-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 than the target.
20 * If it is an exact match, use it.
21 * If previous entry 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 entered with
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 exception.
28 * To delete a key from a range we need to make two ranges, one that ends
29 * just before the new key, one that starts just after.
30 * The 'ends just before' is easy - we just add the new key or range.
31 * The 'starts just after' is managed by entering the same key twice.
32 * The first instance of the key has a 'lsb clear' command and is used for
33 * exact matches. The second instance has 'lsb set' and is used for everything
36 * A 'prefix' can be registered which creates a command which temporarily
37 * enabled the given prefix. It is applied to the next command, but is
38 * discarded after that. This is just a convenience function.
49 inline static int qhash(char key, unsigned int start)
51 return (start ^ key) * 0x61C88647U;
55 unsigned long bloom[256 / (sizeof(long)*8) ];
60 char * safe *keys safe;
61 struct command * safe *comms safe;
64 static inline struct command * safe GETCOMM(struct command *c safe)
66 return (struct command * safe)(((unsigned long)c) & ~1UL);
69 static inline int IS_RANGE(struct command *c)
71 return ((unsigned long)c) & 1;
74 static inline struct command *SET_RANGE(struct command *c)
76 return (struct command *)(((unsigned long)c) | 1UL);
79 static int size2alloc(int size)
81 /* Alway multiple of 8. */
82 return ((size-1) | 7) + 1;
85 struct map *safe key_alloc(void)
87 struct map *m = calloc(1, sizeof(*m));
93 void key_free(struct map *m safe)
96 for (i = 0; i < m->size; i++) {
98 command_put(GETCOMM(m->comms[i]));
105 static int hash_str(char *key safe, int len)
109 for (i = 0; (len < 0 || i < len) && key[i]; i++)
110 h = qhash(key[i], h);
114 inline static void set_bit(unsigned long *set safe, int bit)
116 set[bit/(sizeof(unsigned long)*8)] |= 1UL << (bit % (sizeof(unsigned long)*8));
119 inline static int test_bit(unsigned long *set safe, int bit)
121 return !! (set[bit/(sizeof(unsigned long)*8)] & (1UL << (bit % (sizeof(unsigned long)*8))));
125 static int key_present(struct map *map safe, char *key, int klen, unsigned int *hashp safe)
131 for (i = 0; i < map->size; i++) {
132 hash = hash_str(map->keys[i], map->prefix_len);
133 set_bit(map->bloom, hash&0xff);
134 set_bit(map->bloom, (hash>>8)&0xff);
135 set_bit(map->bloom, (hash>>16)&0xff);
139 if (map->prefix_len < 0 || klen <= map->prefix_len)
142 hash = hashp[map->prefix_len];
143 return (test_bit(map->bloom, hash&0xff) &&
144 test_bit(map->bloom, (hash>>8)&0xff) &&
145 test_bit(map->bloom, (hash>>16)&0xff));
148 /* Find first entry >= k */
149 static int key_find_len(struct map *map safe, char *k safe, int len)
154 /* all entries before 'lo' are < k.
155 * all entries at 'hi' or later are >= k.
156 * So when lo==hi, hi is the answer.
159 int mid = (hi + lo)/ 2;
160 int cmp = strncmp(map->keys[mid], k, len);
169 static int key_find(struct map *map safe, char *k safe)
171 return key_find_len(map, k, strlen(k));
174 void key_add(struct map *map safe, char *k safe, struct command *comm)
178 struct command *comm2 = NULL;
184 pos = key_find(map, k);
186 * 1/ match start of range: insert before
187 * 2/ match non-range start: replace
188 * 3/ not in range: insert before like 1
189 * 4/ in a range: insert match and range start.
191 if (pos >= map->size) {
192 /* Insert k,comm - default action */
193 } else if (strcmp(k, map->keys[pos]) == 0) {
194 /* match: need to check if range-start */
195 if (IS_RANGE(map->comms[pos])) {
196 /* Changing the start of a range, insert an exact match */
198 /* replace a non-range */
199 command_put(map->comms[pos]);
200 map->comms[pos] = command_get(comm);
203 } else if (pos > 0 && IS_RANGE(map->comms[pos-1])) {
204 /* insert within a range.
205 * Add given command as non-range match, and old command
208 comm2 = map->comms[pos-1];
210 /* Not in a range, simple insert */
213 ins_cnt = comm2 ? 2 : 1;
214 size = size2alloc(map->size + ins_cnt);
216 if (size2alloc(map->size) != size) {
217 map->keys = realloc(map->keys, size * sizeof(map->keys[0]));
218 map->comms = realloc(map->comms,
219 size * sizeof(struct command *));
222 memmove(map->keys+pos+ins_cnt, map->keys+pos,
223 (map->size - pos) * sizeof(map->keys[0]));
224 memmove(map->comms+pos+ins_cnt, map->comms+pos,
225 (map->size - pos) * sizeof(struct command *));
226 map->keys[pos] = strdup(k);
227 map->comms[pos] = command_get(comm);
229 map->keys[pos+1] = strdup(k);
230 map->comms[pos+1] = SET_RANGE(command_get(GETCOMM(comm2)));
232 map->size += ins_cnt;
236 void key_add_range(struct map *map safe, char *first safe, char *last safe,
237 struct command *comm)
244 if (!comm || strcmp(first, last) >= 0)
247 /* Add the first entry using key_add */
248 key_add(map, first, comm);
249 pos = key_find(map, first);
250 pos2 = key_find(map, last);
252 /* Now 'pos' is a stand-alone entry for 'first'.
253 * If the entry before pos2 is a range start, update to start at 'last',
254 * else discard it, and discard everything else between pos and pos2.
255 * Then insert a stand-alone for 'last' and update 'pos' to be a range-start.
257 if (pos2 - 1 > pos && IS_RANGE(map->comms[pos2-1])) {
258 map->keys[pos2-1] = last;
261 /* Need to insert 'last', and remove extras. so +1 and -(pos2-pos-1); */
262 move_size = 1 - (pos2 - pos - 1);
263 size = size2alloc(map->size + move_size);
264 if (size2alloc(map->size) < size) {
265 map->keys = realloc(map->keys, size * sizeof(map->keys[0]));
266 map->comms = realloc(map->comms,
267 size * sizeof(struct command *));
269 for (i = pos + 1; i < pos2; i++) {
271 command_put(GETCOMM(map->comms[i]));
273 memmove(map->keys+pos2 + move_size, map->keys+pos2,
274 (map->size - pos2) * sizeof(map->keys[0]));
275 memmove(map->comms+pos2+ move_size, map->comms+pos2,
276 (map->size - pos2) * sizeof(struct command *));
278 map->comms[pos] = SET_RANGE(comm);
279 map->keys[pos+1] = strdup(last);
280 map->comms[pos+1] = command_get(comm);
281 map->size += move_size;
284 first[prefix] && first[prefix+1] == last[prefix+1];
287 if (map->prefix_len < 0 || map->prefix_len > prefix)
288 map->prefix_len = prefix;
291 void key_add_chain(struct map *map safe, struct map *chain)
300 void key_del(struct map *map, wint_t k)
304 pos = key_find(map, k, -1);
305 if (pos >= map->size || strcmp(map->keys[pos], k) == 0)
308 memmove(map->keys+pos, map->keys+pos+1,
309 (map->size-pos-1) * sizeof(map->keys[0]));
310 memmove(map->comms+pos, map->comms+pos+1,
311 (map->size-pos-1) * sizeof(struct command *));
322 static int key_prefix(const struct cmd_info *ci safe)
324 struct modmap *m = container_of(ci->comm, struct modmap, comm);
326 call("Mode:set-mode", ci->focus, 0, NULL, m->name);
327 call("Mode:set-num", ci->focus, ci->num);
328 call("Mode:set-num2", ci->focus, ci->num2);
332 struct command *key_register_prefix(char *name safe)
334 struct modmap *mm = malloc(sizeof(*mm));
336 /* FIXME refcount these */
337 mm->name = strdup(name);
338 mm->comm.func = key_prefix;
340 mm->comm.free = NULL;
344 struct command *key_lookup_cmd(struct map *m safe, char *c safe,
345 char **cret, int *lenret)
347 /* If 'k' contains an ASCII US (Unit Separator, 0o37 0x1f 31),
348 * it represents multiple keys.
349 * Call key_find() on each of them until success.
352 char *end = strchr(c, '\037');
357 pos = key_find_len(m, c, end - c);
361 else if (strncmp(m->keys[pos], c, end - c) == 0 &&
362 m->keys[pos][end - c] == '\0') {
363 /* Exact match - use this entry */
368 return GETCOMM(m->comms[pos]);
369 } else if (pos > 0 && IS_RANGE(m->comms[pos-1])) {
370 /* In a range, use previous */
375 return GETCOMM(m->comms[pos-1]);
384 int key_lookup(struct map *m safe, const struct cmd_info *ci safe)
386 struct command *comm;
390 if (ci->hash && !key_present(m, ci->key, strlen(ci->key), ci->hash))
393 comm = key_lookup_cmd(m, ci->key, &key, &len);
397 /* This is message, but when there are multiple
398 * keys, we need to pass down the one that was matched.
401 char *oldkey = ci->key;
402 char tail = key[len];
405 ((struct cmd_info*)ci)->comm = comm;
406 ((struct cmd_info*)ci)->key = key;
407 ret = comm->func(ci);
408 ((struct cmd_info*)ci)->key = oldkey;
415 int key_lookup_prefix(struct map *m safe, const struct cmd_info *ci safe)
417 int pos = key_find(m, ci->key);
418 struct command *comm, *prev = NULL;
419 int len = strlen(ci->key);
422 while (pos < m->size && strncmp(m->keys[pos], k, len) == 0) {
423 comm = GETCOMM(m->comms[pos]);
424 if (comm && comm != prev) {
426 ((struct cmd_info*)ci)->comm = comm;
427 ((struct cmd_info*)ci)->key = m->keys[pos];
428 ret = comm->func(ci);
429 ASSERT(ret >= 0 || ret < Eunused);
436 ((struct cmd_info*)ci)->key = k;
440 int key_lookup_cmd_func(const struct cmd_info *ci safe)
442 struct lookup_cmd *l = container_of(ci->comm, struct lookup_cmd, c);
443 struct map *m = safe_cast *l->m;
444 int ret = key_lookup(m, ci);
446 while (!ret && m->chain) {
448 ret = key_lookup(m, ci);
453 /* key_handle. Search towards root for the pane which handles the command.
455 int key_handle(const struct cmd_info *ci safe)
457 struct cmd_info *vci = (struct cmd_info*)ci;
459 unsigned int hash[30];
463 time_start_key(ci->key);
464 if ((void*) ci->comm) {
465 int ret = ci->comm->func(ci);
466 if (ret == Enotarget)
468 time_stop_key(ci->key);
472 for (i = 0; i < 30 && ci->key[i]; i++) {
473 h = qhash(ci->key[i], h);
476 if (ci->key[i] == '\037')
477 // Disable hash for multi-keys
484 /* If 'home' is set, search from there, else search
495 vci->comm = p->handle;
496 ret = p->handle->func(ci);
499 time_stop_key(ci->key);
500 /* 'p' might have been destroyed */
501 return ret == Enotarget ? Efallthrough : ret;
505 time_stop_key(ci->key);