Blame


1 67c8ed7f 2021-03-13 op /*
2 67c8ed7f 2021-03-13 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 67c8ed7f 2021-03-13 op *
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.
7 67c8ed7f 2021-03-13 op *
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.
15 67c8ed7f 2021-03-13 op */
16 67c8ed7f 2021-03-13 op
17 67c8ed7f 2021-03-13 op #include "telescope.h"
18 67c8ed7f 2021-03-13 op
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>
22 67c8ed7f 2021-03-13 op
23 67c8ed7f 2021-03-13 op #define CTRL(n) ((n)&0x1F)
24 67c8ed7f 2021-03-13 op
25 0a5a10e8 2021-03-13 op static struct keytable {
26 67c8ed7f 2021-03-13 op char *p;
27 67c8ed7f 2021-03-13 op int k;
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 },
37 67c8ed7f 2021-03-13 op /* ... */
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') },
45 67c8ed7f 2021-03-13 op /* ... */
46 67c8ed7f 2021-03-13 op { NULL, 0 },
47 67c8ed7f 2021-03-13 op };
48 67c8ed7f 2021-03-13 op
49 67c8ed7f 2021-03-13 op int
50 67c8ed7f 2021-03-13 op kbd(const char *key)
51 67c8ed7f 2021-03-13 op {
52 67c8ed7f 2021-03-13 op struct keytable *t;
53 67c8ed7f 2021-03-13 op
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))
56 67c8ed7f 2021-03-13 op return t->k;
57 67c8ed7f 2021-03-13 op }
58 67c8ed7f 2021-03-13 op
59 67c8ed7f 2021-03-13 op return *key;
60 67c8ed7f 2021-03-13 op }
61 67c8ed7f 2021-03-13 op
62 67c8ed7f 2021-03-13 op const char *
63 67c8ed7f 2021-03-13 op unkbd(int k)
64 67c8ed7f 2021-03-13 op {
65 67c8ed7f 2021-03-13 op struct keytable *t;
66 67c8ed7f 2021-03-13 op
67 67c8ed7f 2021-03-13 op for (t = keytable; t->p != NULL; ++t) {
68 67c8ed7f 2021-03-13 op if (k == t->k)
69 67c8ed7f 2021-03-13 op return t->p;
70 67c8ed7f 2021-03-13 op }
71 67c8ed7f 2021-03-13 op
72 67c8ed7f 2021-03-13 op return NULL;
73 67c8ed7f 2021-03-13 op }
74 67c8ed7f 2021-03-13 op
75 67c8ed7f 2021-03-13 op int
76 67c8ed7f 2021-03-13 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
77 67c8ed7f 2021-03-13 op {
78 67c8ed7f 2021-03-13 op int ctrl, meta, k;
79 67c8ed7f 2021-03-13 op struct keymap *entry;
80 67c8ed7f 2021-03-13 op
81 67c8ed7f 2021-03-13 op again:
82 67c8ed7f 2021-03-13 op if ((ctrl = has_prefix(key, "C-")))
83 67c8ed7f 2021-03-13 op key += 2;
84 67c8ed7f 2021-03-13 op if ((meta = has_prefix(key, "M-")))
85 67c8ed7f 2021-03-13 op key += 2;
86 67c8ed7f 2021-03-13 op if (*key == '\0')
87 67c8ed7f 2021-03-13 op return 0;
88 67c8ed7f 2021-03-13 op k = kbd(key);
89 67c8ed7f 2021-03-13 op
90 67c8ed7f 2021-03-13 op if (ctrl)
91 67c8ed7f 2021-03-13 op k = CTRL(k);
92 67c8ed7f 2021-03-13 op
93 67c8ed7f 2021-03-13 op /* skip key & spaces */
94 67c8ed7f 2021-03-13 op while (*key != '\0' && !isspace(*key))
95 67c8ed7f 2021-03-13 op ++key;
96 67c8ed7f 2021-03-13 op while (*key != '\0' && isspace(*key))
97 67c8ed7f 2021-03-13 op ++key;
98 67c8ed7f 2021-03-13 op
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;
103 67c8ed7f 2021-03-13 op return 1;
104 67c8ed7f 2021-03-13 op }
105 67c8ed7f 2021-03-13 op map = &entry->map;
106 67c8ed7f 2021-03-13 op goto again;
107 67c8ed7f 2021-03-13 op }
108 67c8ed7f 2021-03-13 op }
109 67c8ed7f 2021-03-13 op
110 67c8ed7f 2021-03-13 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
111 67c8ed7f 2021-03-13 op return 0;
112 67c8ed7f 2021-03-13 op
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);
116 67c8ed7f 2021-03-13 op
117 67c8ed7f 2021-03-13 op if (TAILQ_EMPTY(&map->m))
118 67c8ed7f 2021-03-13 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
119 67c8ed7f 2021-03-13 op else
120 67c8ed7f 2021-03-13 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
121 67c8ed7f 2021-03-13 op
122 67c8ed7f 2021-03-13 op if (*key != '\0') {
123 67c8ed7f 2021-03-13 op map = &entry->map;
124 67c8ed7f 2021-03-13 op goto again;
125 67c8ed7f 2021-03-13 op }
126 67c8ed7f 2021-03-13 op
127 67c8ed7f 2021-03-13 op entry->fn = fn;
128 67c8ed7f 2021-03-13 op
129 67c8ed7f 2021-03-13 op return 1;
130 67c8ed7f 2021-03-13 op }
131 67c8ed7f 2021-03-13 op