2 67c8ed7f 2021-03-13 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 67c8ed7f 2021-03-13 op * Permission to use, copy, modify, and distribute this software for any
5 67c8ed7f 2021-03-13 op * purpose with or without fee is hereby granted, provided that the above
6 67c8ed7f 2021-03-13 op * copyright notice and this permission notice appear in all copies.
8 67c8ed7f 2021-03-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 67c8ed7f 2021-03-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 67c8ed7f 2021-03-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 67c8ed7f 2021-03-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 67c8ed7f 2021-03-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 67c8ed7f 2021-03-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 67c8ed7f 2021-03-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 67c8ed7f 2021-03-13 op #include "telescope.h"
19 67c8ed7f 2021-03-13 op #include <ctype.h>
20 67c8ed7f 2021-03-13 op #include <curses.h>
21 67c8ed7f 2021-03-13 op #include <stdlib.h>
23 67c8ed7f 2021-03-13 op #define CTRL(n) ((n)&0x1F)
25 0a5a10e8 2021-03-13 op static struct keytable {
28 67c8ed7f 2021-03-13 op } keytable[] = {
29 67c8ed7f 2021-03-13 op { "<up>", KEY_UP },
30 67c8ed7f 2021-03-13 op { "<down>", KEY_DOWN },
31 67c8ed7f 2021-03-13 op { "<left>", KEY_LEFT },
32 67c8ed7f 2021-03-13 op { "<right>", KEY_RIGHT },
33 67c8ed7f 2021-03-13 op { "<prior>", KEY_PPAGE },
34 67c8ed7f 2021-03-13 op { "<next>", KEY_NPAGE },
35 67c8ed7f 2021-03-13 op { "<home>", KEY_HOME },
36 67c8ed7f 2021-03-13 op { "<end>", KEY_END },
38 67c8ed7f 2021-03-13 op { "del", KEY_BACKSPACE },
39 67c8ed7f 2021-03-13 op { "esc", 27 },
40 67c8ed7f 2021-03-13 op { "space", ' ' },
41 67c8ed7f 2021-03-13 op { "spc", ' ' },
42 67c8ed7f 2021-03-13 op { "enter", CTRL('m') },
43 67c8ed7f 2021-03-13 op { "ret", CTRL('m' )},
44 67c8ed7f 2021-03-13 op { "tab", CTRL('i') },
50 67c8ed7f 2021-03-13 op kbd(const char *key)
52 67c8ed7f 2021-03-13 op struct keytable *t;
54 67c8ed7f 2021-03-13 op for (t = keytable; t->p != NULL; ++t) {
55 67c8ed7f 2021-03-13 op if (has_prefix(key, t->p))
65 67c8ed7f 2021-03-13 op struct keytable *t;
67 67c8ed7f 2021-03-13 op for (t = keytable; t->p != NULL; ++t) {
68 67c8ed7f 2021-03-13 op if (k == t->k)
76 67c8ed7f 2021-03-13 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
78 67c8ed7f 2021-03-13 op int ctrl, meta, k;
79 67c8ed7f 2021-03-13 op struct keymap *entry;
82 67c8ed7f 2021-03-13 op if ((ctrl = has_prefix(key, "C-")))
84 67c8ed7f 2021-03-13 op if ((meta = has_prefix(key, "M-")))
86 67c8ed7f 2021-03-13 op if (*key == '\0')
93 67c8ed7f 2021-03-13 op /* skip key & spaces */
94 67c8ed7f 2021-03-13 op while (*key != '\0' && !isspace(*key))
96 67c8ed7f 2021-03-13 op while (*key != '\0' && isspace(*key))
99 67c8ed7f 2021-03-13 op TAILQ_FOREACH(entry, &map->m, keymaps) {
100 67c8ed7f 2021-03-13 op if (entry->meta == meta && entry->key == k) {
101 67c8ed7f 2021-03-13 op if (*key == '\0') {
102 67c8ed7f 2021-03-13 op entry->fn = fn;
105 67c8ed7f 2021-03-13 op map = &entry->map;
110 67c8ed7f 2021-03-13 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
113 67c8ed7f 2021-03-13 op entry->meta = meta;
114 67c8ed7f 2021-03-13 op entry->key = k;
115 67c8ed7f 2021-03-13 op TAILQ_INIT(&entry->map.m);
117 67c8ed7f 2021-03-13 op if (TAILQ_EMPTY(&map->m))
118 67c8ed7f 2021-03-13 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
120 67c8ed7f 2021-03-13 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
122 67c8ed7f 2021-03-13 op if (*key != '\0') {
123 67c8ed7f 2021-03-13 op map = &entry->map;
127 67c8ed7f 2021-03-13 op entry->fn = fn;