Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stdlib.h>
18 #include <string.h>
20 #include "minibuffer.h"
22 static void minibuffer_hist_save_entry(void);
23 static void minibuffer_self_insert(void);
24 static void yornp_self_insert(void);
25 static void yornp_abort(void);
26 static void read_self_insert(void);
27 static void read_abort(void);
28 static void read_select(void);
30 static void (*yornp_cb)(int, struct tab *);
31 static struct tab *yornp_data;
33 static void (*read_cb)(const char*, unsigned int);
34 static unsigned int read_data;
36 struct histhead eecmd_history,
37 ir_history,
38 lu_history,
39 read_history;
41 struct ministate ministate;
43 static void
44 minibuffer_hist_save_entry(void)
45 {
46 struct hist *hist;
48 if (ministate.history == NULL)
49 return;
51 if ((hist = calloc(1, sizeof(*hist))) == NULL)
52 abort();
54 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
56 if (TAILQ_EMPTY(&ministate.history->head))
57 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
58 else
59 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
60 ministate.history->len++;
61 }
63 /*
64 * taint the minibuffer cache: if we're currently showing a history
65 * element, copy that to the current buf and reset the "history
66 * navigation" thing.
67 */
68 void
69 minibuffer_taint_hist(void)
70 {
71 if (ministate.hist_cur == NULL)
72 return;
74 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
75 ministate.hist_cur = NULL;
76 }
78 static void
79 minibuffer_self_insert(void)
80 {
81 char *c, tmp[5] = {0};
82 size_t len;
84 minibuffer_taint_hist();
86 if (thiskey.cp == 0)
87 return;
89 len = utf8_encode(thiskey.cp, tmp);
90 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
91 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
92 return;
94 memmove(c + len, c, strlen(c)+1);
95 memcpy(c, tmp, len);
96 ministate.buffer.cpoff++;
97 }
99 void
100 eecmd_self_insert(void)
102 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
103 !unicode_isgraph(thiskey.cp)) {
104 global_key_unbound();
105 return;
108 minibuffer_self_insert();
111 void
112 eecmd_select(void)
114 struct cmd *cmd;
116 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
117 if (!strcmp(cmd->cmd, ministate.buf)) {
118 exit_minibuffer();
119 minibuffer_hist_save_entry();
120 cmd->fn(current_buffer());
121 return;
125 message("No match");
128 void
129 ir_self_insert(void)
131 minibuffer_self_insert();
134 void
135 ir_select(void)
137 char buf[1025] = {0};
138 struct phos_uri uri;
139 struct tab *tab;
141 tab = current_tab();
143 exit_minibuffer();
144 minibuffer_hist_save_entry();
146 /* a bit ugly but... */
147 memcpy(&uri, &tab->uri, sizeof(tab->uri));
148 phos_uri_set_query(&uri, ministate.buf);
149 phos_serialize_uri(&uri, buf, sizeof(buf));
150 load_url_in_tab(tab, buf);
153 void
154 lu_self_insert(void)
156 if (thiskey.meta || unicode_isspace(thiskey.key) ||
157 !unicode_isgraph(thiskey.key)) {
158 global_key_unbound();
159 return;
162 minibuffer_self_insert();
165 void
166 lu_select(void)
168 exit_minibuffer();
169 minibuffer_hist_save_entry();
170 load_url_in_tab(current_tab(), ministate.buf);
173 void
174 bp_select(void)
176 exit_minibuffer();
177 if (*ministate.buf != '\0')
178 add_to_bookmarks(ministate.buf);
179 else
180 message("Abort.");
183 static void
184 yornp_self_insert(void)
186 if (thiskey.key != 'y' && thiskey.key != 'n') {
187 message("Please answer y or n");
188 return;
191 exit_minibuffer();
192 yornp_cb(thiskey.key == 'y', yornp_data);
195 static void
196 yornp_abort(void)
198 exit_minibuffer();
199 yornp_cb(0, yornp_data);
202 static void
203 read_self_insert(void)
205 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
206 global_key_unbound();
207 return;
210 minibuffer_self_insert();
213 static void
214 read_abort(void)
216 exit_minibuffer();
217 read_cb(NULL, read_data);
220 static void
221 read_select(void)
223 exit_minibuffer();
224 minibuffer_hist_save_entry();
225 read_cb(ministate.buf, read_data);
228 void
229 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
230 void (*abortfn)(void), struct histhead *hist)
232 in_minibuffer = 1;
233 base_map = &minibuffer_map;
234 current_map = &minibuffer_map;
236 base_map->unhandled_input = self_insert_fn;
238 ministate.donefn = donefn;
239 ministate.abortfn = abortfn;
240 memset(ministate.buf, 0, sizeof(ministate.buf));
241 ministate.buffer.current_line = &ministate.vline;
242 ministate.buffer.current_line->line = ministate.buf;
243 ministate.buffer.cpoff = 0;
244 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
246 ministate.history = hist;
247 ministate.hist_cur = NULL;
248 ministate.hist_off = 0;
251 void
252 exit_minibuffer(void)
254 in_minibuffer = 0;
255 base_map = &global_map;
256 current_map = &global_map;
259 void
260 yornp(const char *prompt, void (*fn)(int, struct tab*),
261 struct tab *data)
263 size_t len;
265 if (in_minibuffer) {
266 fn(0, data);
267 return;
270 yornp_cb = fn;
271 yornp_data = data;
272 enter_minibuffer(yornp_self_insert, yornp_self_insert,
273 yornp_abort, NULL);
275 len = sizeof(ministate.prompt);
276 strlcpy(ministate.prompt, prompt, len);
277 strlcat(ministate.prompt, " (y or n) ", len);
280 /*
281 * Not yet "completing", but soon maybe...
282 */
283 void
284 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
285 unsigned int data)
287 size_t len;
289 if (in_minibuffer)
290 return;
292 read_cb = fn;
293 read_data = data;
294 enter_minibuffer(read_self_insert, read_select, read_abort,
295 &read_history);
297 len = sizeof(ministate.prompt);
298 strlcpy(ministate.prompt, prompt, len);
299 strlcat(ministate.prompt, ": ", len);