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 786e6deb 2021-07-21 op #include "compat.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 786e6deb 2021-07-21 op #include "telescope.h"
24 786e6deb 2021-07-21 op
25 67c8ed7f 2021-03-13 op #define CTRL(n) ((n)&0x1F)
26 67c8ed7f 2021-03-13 op
27 0a5a10e8 2021-03-13 op static struct keytable {
28 754622a2 2021-03-15 op const char *p;
29 754622a2 2021-03-15 op int k;
30 67c8ed7f 2021-03-13 op } keytable[] = {
31 67c8ed7f 2021-03-13 op { "<up>", KEY_UP },
32 67c8ed7f 2021-03-13 op { "<down>", KEY_DOWN },
33 67c8ed7f 2021-03-13 op { "<left>", KEY_LEFT },
34 67c8ed7f 2021-03-13 op { "<right>", KEY_RIGHT },
35 67c8ed7f 2021-03-13 op { "<prior>", KEY_PPAGE },
36 67c8ed7f 2021-03-13 op { "<next>", KEY_NPAGE },
37 67c8ed7f 2021-03-13 op { "<home>", KEY_HOME },
38 67c8ed7f 2021-03-13 op { "<end>", KEY_END },
39 67c8ed7f 2021-03-13 op /* ... */
40 4c44f8c0 2021-03-15 op { "<f0>", KEY_F(0) },
41 4c44f8c0 2021-03-15 op { "<f1>", KEY_F(1) },
42 4c44f8c0 2021-03-15 op { "<f2>", KEY_F(2) },
43 4c44f8c0 2021-03-15 op { "<f3>", KEY_F(3) },
44 4c44f8c0 2021-03-15 op { "<f4>", KEY_F(4) },
45 4c44f8c0 2021-03-15 op { "<f5>", KEY_F(5) },
46 4c44f8c0 2021-03-15 op { "<f6>", KEY_F(6) },
47 4c44f8c0 2021-03-15 op { "<f7>", KEY_F(7) },
48 4c44f8c0 2021-03-15 op { "<f8>", KEY_F(8) },
49 4c44f8c0 2021-03-15 op { "<f9>", KEY_F(9) },
50 4c44f8c0 2021-03-15 op { "<f10>", KEY_F(10) },
51 4c44f8c0 2021-03-15 op { "<f11>", KEY_F(11) },
52 4c44f8c0 2021-03-15 op { "<f12>", KEY_F(12) },
53 4c44f8c0 2021-03-15 op { "<f13>", KEY_F(13) },
54 4c44f8c0 2021-03-15 op { "<f14>", KEY_F(14) },
55 4c44f8c0 2021-03-15 op { "<f15>", KEY_F(15) },
56 4c44f8c0 2021-03-15 op { "<f16>", KEY_F(16) },
57 4c44f8c0 2021-03-15 op { "<f17>", KEY_F(17) },
58 4c44f8c0 2021-03-15 op { "<f18>", KEY_F(18) },
59 4c44f8c0 2021-03-15 op { "<f19>", KEY_F(19) },
60 4c44f8c0 2021-03-15 op { "<f20>", KEY_F(20) },
61 4c44f8c0 2021-03-15 op { "<f21>", KEY_F(21) },
62 4c44f8c0 2021-03-15 op { "<f22>", KEY_F(22) },
63 4c44f8c0 2021-03-15 op { "<f23>", KEY_F(23) },
64 4c44f8c0 2021-03-15 op { "<f24>", KEY_F(24) },
65 4c44f8c0 2021-03-15 op { "<f25>", KEY_F(25) },
66 4c44f8c0 2021-03-15 op { "<f26>", KEY_F(26) },
67 4c44f8c0 2021-03-15 op { "<f27>", KEY_F(27) },
68 4c44f8c0 2021-03-15 op { "<f28>", KEY_F(28) },
69 4c44f8c0 2021-03-15 op { "<f29>", KEY_F(29) },
70 4c44f8c0 2021-03-15 op { "<f30>", KEY_F(30) },
71 4c44f8c0 2021-03-15 op { "<f31>", KEY_F(31) },
72 4c44f8c0 2021-03-15 op { "<f32>", KEY_F(32) },
73 4c44f8c0 2021-03-15 op { "<f33>", KEY_F(33) },
74 4c44f8c0 2021-03-15 op { "<f34>", KEY_F(34) },
75 4c44f8c0 2021-03-15 op { "<f35>", KEY_F(35) },
76 4c44f8c0 2021-03-15 op { "<f36>", KEY_F(36) },
77 4c44f8c0 2021-03-15 op { "<f37>", KEY_F(37) },
78 4c44f8c0 2021-03-15 op { "<f38>", KEY_F(38) },
79 4c44f8c0 2021-03-15 op { "<f39>", KEY_F(39) },
80 4c44f8c0 2021-03-15 op { "<f40>", KEY_F(40) },
81 4c44f8c0 2021-03-15 op { "<f41>", KEY_F(41) },
82 4c44f8c0 2021-03-15 op { "<f42>", KEY_F(42) },
83 4c44f8c0 2021-03-15 op { "<f43>", KEY_F(43) },
84 4c44f8c0 2021-03-15 op { "<f44>", KEY_F(44) },
85 4c44f8c0 2021-03-15 op { "<f45>", KEY_F(45) },
86 4c44f8c0 2021-03-15 op { "<f46>", KEY_F(46) },
87 4c44f8c0 2021-03-15 op { "<f47>", KEY_F(47) },
88 4c44f8c0 2021-03-15 op { "<f48>", KEY_F(48) },
89 4c44f8c0 2021-03-15 op { "<f49>", KEY_F(49) },
90 4c44f8c0 2021-03-15 op { "<f50>", KEY_F(50) },
91 4c44f8c0 2021-03-15 op { "<f51>", KEY_F(51) },
92 4c44f8c0 2021-03-15 op { "<f52>", KEY_F(52) },
93 4c44f8c0 2021-03-15 op { "<f53>", KEY_F(53) },
94 4c44f8c0 2021-03-15 op { "<f54>", KEY_F(54) },
95 4c44f8c0 2021-03-15 op { "<f55>", KEY_F(55) },
96 4c44f8c0 2021-03-15 op { "<f56>", KEY_F(56) },
97 4c44f8c0 2021-03-15 op { "<f57>", KEY_F(57) },
98 4c44f8c0 2021-03-15 op { "<f58>", KEY_F(58) },
99 4c44f8c0 2021-03-15 op { "<f59>", KEY_F(59) },
100 4c44f8c0 2021-03-15 op { "<f60>", KEY_F(60) },
101 4c44f8c0 2021-03-15 op { "<f61>", KEY_F(61) },
102 4c44f8c0 2021-03-15 op { "<f62>", KEY_F(62) },
103 4c44f8c0 2021-03-15 op { "<f63>", KEY_F(63) },
104 4c44f8c0 2021-03-15 op /* ... */
105 67c8ed7f 2021-03-13 op { "del", KEY_BACKSPACE },
106 3c066721 2021-03-26 op { "backspace", 127 },
107 67c8ed7f 2021-03-13 op { "esc", 27 },
108 67c8ed7f 2021-03-13 op { "space", ' ' },
109 67c8ed7f 2021-03-13 op { "spc", ' ' },
110 67c8ed7f 2021-03-13 op { "enter", CTRL('m') },
111 67c8ed7f 2021-03-13 op { "ret", CTRL('m' )},
112 67c8ed7f 2021-03-13 op { "tab", CTRL('i') },
113 8dc60352 2021-06-15 op { "backtab", KEY_BTAB },
114 67c8ed7f 2021-03-13 op /* ... */
115 67c8ed7f 2021-03-13 op { NULL, 0 },
116 67c8ed7f 2021-03-13 op };
117 67c8ed7f 2021-03-13 op
118 67c8ed7f 2021-03-13 op int
119 67c8ed7f 2021-03-13 op kbd(const char *key)
120 67c8ed7f 2021-03-13 op {
121 67c8ed7f 2021-03-13 op struct keytable *t;
122 67c8ed7f 2021-03-13 op
123 67c8ed7f 2021-03-13 op for (t = keytable; t->p != NULL; ++t) {
124 67c8ed7f 2021-03-13 op if (has_prefix(key, t->p))
125 67c8ed7f 2021-03-13 op return t->k;
126 67c8ed7f 2021-03-13 op }
127 67c8ed7f 2021-03-13 op
128 67c8ed7f 2021-03-13 op return *key;
129 67c8ed7f 2021-03-13 op }
130 67c8ed7f 2021-03-13 op
131 67c8ed7f 2021-03-13 op const char *
132 67c8ed7f 2021-03-13 op unkbd(int k)
133 67c8ed7f 2021-03-13 op {
134 67c8ed7f 2021-03-13 op struct keytable *t;
135 67c8ed7f 2021-03-13 op
136 67c8ed7f 2021-03-13 op for (t = keytable; t->p != NULL; ++t) {
137 67c8ed7f 2021-03-13 op if (k == t->k)
138 67c8ed7f 2021-03-13 op return t->p;
139 67c8ed7f 2021-03-13 op }
140 67c8ed7f 2021-03-13 op
141 67c8ed7f 2021-03-13 op return NULL;
142 67c8ed7f 2021-03-13 op }
143 67c8ed7f 2021-03-13 op
144 67c8ed7f 2021-03-13 op int
145 46f6e974 2021-05-17 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct buffer*))
146 67c8ed7f 2021-03-13 op {
147 67c8ed7f 2021-03-13 op int ctrl, meta, k;
148 67c8ed7f 2021-03-13 op struct keymap *entry;
149 67c8ed7f 2021-03-13 op
150 67c8ed7f 2021-03-13 op again:
151 67c8ed7f 2021-03-13 op if ((ctrl = has_prefix(key, "C-")))
152 67c8ed7f 2021-03-13 op key += 2;
153 67c8ed7f 2021-03-13 op if ((meta = has_prefix(key, "M-")))
154 67c8ed7f 2021-03-13 op key += 2;
155 67c8ed7f 2021-03-13 op if (*key == '\0')
156 67c8ed7f 2021-03-13 op return 0;
157 67c8ed7f 2021-03-13 op k = kbd(key);
158 67c8ed7f 2021-03-13 op
159 67c8ed7f 2021-03-13 op if (ctrl)
160 67c8ed7f 2021-03-13 op k = CTRL(k);
161 67c8ed7f 2021-03-13 op
162 67c8ed7f 2021-03-13 op /* skip key & spaces */
163 67c8ed7f 2021-03-13 op while (*key != '\0' && !isspace(*key))
164 67c8ed7f 2021-03-13 op ++key;
165 67c8ed7f 2021-03-13 op while (*key != '\0' && isspace(*key))
166 67c8ed7f 2021-03-13 op ++key;
167 67c8ed7f 2021-03-13 op
168 67c8ed7f 2021-03-13 op TAILQ_FOREACH(entry, &map->m, keymaps) {
169 67c8ed7f 2021-03-13 op if (entry->meta == meta && entry->key == k) {
170 67c8ed7f 2021-03-13 op if (*key == '\0') {
171 67c8ed7f 2021-03-13 op entry->fn = fn;
172 67c8ed7f 2021-03-13 op return 1;
173 67c8ed7f 2021-03-13 op }
174 67c8ed7f 2021-03-13 op map = &entry->map;
175 67c8ed7f 2021-03-13 op goto again;
176 67c8ed7f 2021-03-13 op }
177 67c8ed7f 2021-03-13 op }
178 67c8ed7f 2021-03-13 op
179 67c8ed7f 2021-03-13 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
180 67c8ed7f 2021-03-13 op return 0;
181 67c8ed7f 2021-03-13 op
182 67c8ed7f 2021-03-13 op entry->meta = meta;
183 67c8ed7f 2021-03-13 op entry->key = k;
184 67c8ed7f 2021-03-13 op TAILQ_INIT(&entry->map.m);
185 67c8ed7f 2021-03-13 op
186 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
187 67c8ed7f 2021-03-13 op
188 67c8ed7f 2021-03-13 op if (*key != '\0') {
189 67c8ed7f 2021-03-13 op map = &entry->map;
190 67c8ed7f 2021-03-13 op goto again;
191 67c8ed7f 2021-03-13 op }
192 67c8ed7f 2021-03-13 op
193 67c8ed7f 2021-03-13 op entry->fn = fn;
194 67c8ed7f 2021-03-13 op
195 67c8ed7f 2021-03-13 op return 1;
196 67c8ed7f 2021-03-13 op }
197 67c8ed7f 2021-03-13 op