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"
21 #include "ui.h"
22 #include "utf8.h"
24 static void minibuffer_hist_save_entry(void);
25 static void minibuffer_self_insert(void);
26 static void yornp_self_insert(void);
27 static void yornp_abort(void);
28 static void read_self_insert(void);
29 static void read_abort(void);
30 static void read_select(void);
32 static void (*yornp_cb)(int, struct tab *);
33 static struct tab *yornp_data;
35 static void (*read_cb)(const char*, struct tab *);
36 static struct tab *read_data;
38 struct histhead eecmd_history,
39 ir_history,
40 lu_history,
41 read_history;
43 struct ministate ministate;
45 struct buffer minibufferwin;
47 /*
48 * Recompute the visible completions. If add is 1, don't consider the
49 * ones already hidden.
50 */
51 void
52 recompute_completions(int add)
53 {
54 struct line *l;
55 struct vline *vl;
56 struct buffer *b;
58 if (in_minibuffer != MB_COMPREAD)
59 return;
61 b = &ministate.compl.buffer;
62 TAILQ_FOREACH(l, &b->page.head, lines) {
63 l->type = LINE_COMPL;
64 if (add && l->flags & L_HIDDEN)
65 continue;
66 if (strstr(l->line, ministate.buf) != NULL)
67 l->flags &= ~L_HIDDEN;
68 else
69 l->flags |= L_HIDDEN;
70 }
72 if (b->current_line == NULL)
73 b->current_line = TAILQ_FIRST(&b->head);
74 b->current_line = adjust_line(b->current_line, b);
75 vl = b->current_line;
76 if (vl != NULL)
77 vl->parent->type = LINE_COMPL_CURRENT;
78 }
80 static void
81 minibuffer_hist_save_entry(void)
82 {
83 struct hist *hist;
85 if (ministate.history == NULL)
86 return;
88 if ((hist = calloc(1, sizeof(*hist))) == NULL)
89 abort();
91 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
93 if (TAILQ_EMPTY(&ministate.history->head))
94 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
95 else
96 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
97 ministate.history->len++;
98 }
100 /*
101 * taint the minibuffer cache: if we're currently showing a history
102 * element, copy that to the current buf and reset the "history
103 * navigation" thing.
104 */
105 void
106 minibuffer_taint_hist(void)
108 if (ministate.hist_cur == NULL)
109 return;
111 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
112 ministate.hist_cur = NULL;
113 ministate.buffer.current_line->line = ministate.buf;
116 static void
117 minibuffer_self_insert(void)
119 char *c, tmp[5] = {0};
120 size_t len;
122 minibuffer_taint_hist();
124 if (thiskey.cp == 0)
125 return;
127 len = utf8_encode(thiskey.cp, tmp);
128 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
129 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
130 return;
132 memmove(c + len, c, strlen(c)+1);
133 memcpy(c, tmp, len);
134 ministate.buffer.cpoff++;
136 recompute_completions(1);
139 void
140 eecmd_self_insert(void)
142 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
143 !unicode_isgraph(thiskey.cp)) {
144 global_key_unbound();
145 return;
148 minibuffer_self_insert();
151 void
152 eecmd_select(void)
154 struct cmd *cmd;
156 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
157 if (!strcmp(cmd->cmd, ministate.buf)) {
158 exit_minibuffer();
159 minibuffer_hist_save_entry();
160 cmd->fn(current_buffer());
161 return;
165 message("No match");
168 void
169 ir_self_insert(void)
171 minibuffer_self_insert();
174 void
175 ir_select(void)
177 char buf[1025] = {0};
178 struct phos_uri uri;
179 struct tab *tab;
181 tab = current_tab();
183 exit_minibuffer();
184 minibuffer_hist_save_entry();
186 /* a bit ugly but... */
187 memcpy(&uri, &tab->uri, sizeof(tab->uri));
188 phos_uri_set_query(&uri, ministate.buf);
189 phos_serialize_uri(&uri, buf, sizeof(buf));
190 load_url_in_tab(tab, buf);
193 void
194 lu_self_insert(void)
196 if (thiskey.meta || unicode_isspace(thiskey.key) ||
197 !unicode_isgraph(thiskey.key)) {
198 global_key_unbound();
199 return;
202 minibuffer_self_insert();
205 void
206 lu_select(void)
208 exit_minibuffer();
209 minibuffer_hist_save_entry();
210 load_url_in_tab(current_tab(), ministate.buf);
213 void
214 bp_select(void)
216 exit_minibuffer();
217 if (*ministate.buf != '\0')
218 add_to_bookmarks(ministate.buf);
219 else
220 message("Abort.");
223 static void
224 yornp_self_insert(void)
226 if (thiskey.key != 'y' && thiskey.key != 'n') {
227 message("Please answer y or n");
228 return;
231 exit_minibuffer();
232 yornp_cb(thiskey.key == 'y', yornp_data);
235 static void
236 yornp_abort(void)
238 exit_minibuffer();
239 yornp_cb(0, yornp_data);
242 static void
243 read_self_insert(void)
245 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
246 global_key_unbound();
247 return;
250 minibuffer_self_insert();
253 static void
254 read_abort(void)
256 exit_minibuffer();
257 read_cb(NULL, read_data);
260 static void
261 read_select(void)
263 exit_minibuffer();
264 minibuffer_hist_save_entry();
265 read_cb(ministate.buf, read_data);
268 /*
269 * TODO: we should collect this asynchronously...
270 */
271 static inline void
272 populate_compl_buffer(complfn *fn, void *data)
274 const char *s;
275 struct line *l;
276 struct buffer *b;
277 struct parser *p;
279 b = &ministate.compl.buffer;
280 p = &b->page;
282 while ((s = fn(&data)) != NULL) {
283 if ((l = calloc(1, sizeof(*l))) == NULL)
284 abort();
286 l->type = LINE_COMPL;
287 if ((l->line = strdup(s)) == NULL)
288 abort();
290 if (TAILQ_EMPTY(&p->head))
291 TAILQ_INSERT_HEAD(&p->head, l, lines);
292 else
293 TAILQ_INSERT_TAIL(&p->head, l, lines);
296 if ((l = TAILQ_FIRST(&p->head)) != NULL)
297 l->type = LINE_COMPL_CURRENT;
300 void
301 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
302 void (*abortfn)(void), struct histhead *hist,
303 complfn *complfn, void *compldata)
305 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
306 if (in_minibuffer == MB_COMPREAD) {
307 ui_schedule_redraw();
309 ministate.compl.fn = complfn;
310 ministate.compl.data = compldata;
311 populate_compl_buffer(complfn, compldata);
314 base_map = &minibuffer_map;
315 current_map = &minibuffer_map;
317 base_map->unhandled_input = self_insert_fn;
319 ministate.donefn = donefn;
320 ministate.abortfn = abortfn;
321 memset(ministate.buf, 0, sizeof(ministate.buf));
322 ministate.buffer.current_line = &ministate.vline;
323 ministate.buffer.current_line->line = ministate.buf;
324 ministate.buffer.cpoff = 0;
325 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
327 ministate.history = hist;
328 ministate.hist_cur = NULL;
329 ministate.hist_off = 0;
332 void
333 exit_minibuffer(void)
335 if (in_minibuffer == MB_COMPREAD) {
336 erase_buffer(&ministate.compl.buffer);
337 ui_schedule_redraw();
340 in_minibuffer = 0;
341 base_map = &global_map;
342 current_map = &global_map;
345 void
346 yornp(const char *prompt, void (*fn)(int, struct tab*),
347 struct tab *data)
349 size_t len;
351 if (in_minibuffer) {
352 fn(0, data);
353 return;
356 yornp_cb = fn;
357 yornp_data = data;
358 enter_minibuffer(yornp_self_insert, yornp_self_insert,
359 yornp_abort, NULL, NULL, NULL);
361 len = sizeof(ministate.prompt);
362 strlcpy(ministate.prompt, prompt, len);
363 strlcat(ministate.prompt, " (y or n) ", len);
366 /*
367 * Not yet "completing", but soon maybe...
368 */
369 void
370 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
371 struct tab *data)
373 size_t len;
375 if (in_minibuffer)
376 return;
378 read_cb = fn;
379 read_data = data;
380 enter_minibuffer(read_self_insert, read_select, read_abort,
381 &read_history, NULL, NULL);
383 len = sizeof(ministate.prompt);
384 strlcpy(ministate.prompt, prompt, len);
385 strlcat(ministate.prompt, ": ", len);