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 yornp_self_insert(void);
26 static void yornp_abort(void);
27 static void read_self_insert(void);
28 static void read_abort(void);
29 static void read_select(void);
31 static void (*yornp_cb)(int, struct tab *);
32 static struct tab *yornp_data;
34 static void (*read_cb)(const char*, struct tab *);
35 static struct tab *read_data;
37 struct histhead eecmd_history,
38 ir_history,
39 lu_history,
40 read_history;
42 struct ministate ministate;
44 struct buffer minibufferwin;
46 /*
47 * Recompute the visible completions. If add is 1, don't consider the
48 * ones already hidden.
49 */
50 void
51 recompute_completions(int add)
52 {
53 struct line *l;
54 struct vline *vl;
55 struct buffer *b;
57 if (in_minibuffer != MB_COMPREAD)
58 return;
60 b = &ministate.compl.buffer;
61 TAILQ_FOREACH(l, &b->page.head, lines) {
62 l->type = LINE_COMPL;
63 if (add && l->flags & L_HIDDEN)
64 continue;
65 if (strcasestr(l->line, ministate.buf) != NULL)
66 l->flags &= ~L_HIDDEN;
67 else
68 l->flags |= L_HIDDEN;
69 }
71 if (b->current_line == NULL)
72 b->current_line = TAILQ_FIRST(&b->head);
73 b->current_line = adjust_line(b->current_line, b);
74 vl = b->current_line;
75 if (vl != NULL)
76 vl->parent->type = LINE_COMPL_CURRENT;
77 }
79 static void
80 minibuffer_hist_save_entry(void)
81 {
82 struct hist *hist;
84 if (ministate.history == NULL)
85 return;
87 if ((hist = calloc(1, sizeof(*hist))) == NULL)
88 abort();
90 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
92 if (TAILQ_EMPTY(&ministate.history->head))
93 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
94 else
95 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
96 ministate.history->len++;
97 }
99 /*
100 * taint the minibuffer cache: if we're currently showing a history
101 * element, copy that to the current buf and reset the "history
102 * navigation" thing.
103 */
104 void
105 minibuffer_taint_hist(void)
107 if (ministate.hist_cur == NULL)
108 return;
110 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
111 ministate.hist_cur = NULL;
112 ministate.buffer.current_line->line = ministate.buf;
115 void
116 minibuffer_self_insert(void)
118 char *c, tmp[5] = {0};
119 size_t len;
121 minibuffer_taint_hist();
123 if (thiskey.cp == 0)
124 return;
126 len = utf8_encode(thiskey.cp, tmp);
127 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
128 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
129 return;
131 memmove(c + len, c, strlen(c)+1);
132 memcpy(c, tmp, len);
133 ministate.buffer.cpoff++;
135 recompute_completions(1);
138 void
139 sensible_self_insert(void)
141 if (thiskey.meta || unicode_isspace(thiskey.key) ||
142 !unicode_isgraph(thiskey.key)) {
143 global_key_unbound();
144 return;
147 minibuffer_self_insert();
150 void
151 eecmd_self_insert(void)
153 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
154 !unicode_isgraph(thiskey.cp)) {
155 global_key_unbound();
156 return;
159 minibuffer_self_insert();
162 void
163 eecmd_select(void)
165 struct cmd *cmd;
167 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
168 if (!strcmp(cmd->cmd, ministate.buf)) {
169 exit_minibuffer();
170 minibuffer_hist_save_entry();
171 cmd->fn(current_buffer());
172 return;
176 message("No match");
179 void
180 ir_self_insert(void)
182 minibuffer_self_insert();
185 void
186 ir_select(void)
188 char buf[1025] = {0};
189 struct phos_uri uri;
190 struct tab *tab;
192 tab = current_tab();
194 exit_minibuffer();
195 minibuffer_hist_save_entry();
197 /* a bit ugly but... */
198 memcpy(&uri, &tab->uri, sizeof(tab->uri));
199 phos_uri_set_query(&uri, ministate.buf);
200 phos_serialize_uri(&uri, buf, sizeof(buf));
201 load_url_in_tab(tab, buf);
204 void
205 lu_select(void)
207 exit_minibuffer();
208 minibuffer_hist_save_entry();
209 load_url_in_tab(current_tab(), ministate.buf);
212 void
213 bp_select(void)
215 exit_minibuffer();
216 if (*ministate.buf != '\0')
217 add_to_bookmarks(ministate.buf);
218 else
219 message("Abort.");
222 void
223 ts_select(void)
225 struct vline *vl;
226 struct tab *tab;
228 vl = ministate.compl.buffer.current_line;
230 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
231 message("No tab selected");
232 return;
235 tab = vl->parent->meta.data;
236 exit_minibuffer();
237 switch_to_tab(tab);
240 void
241 ls_select(void)
243 struct line *l;
244 struct vline *vl;
246 vl = ministate.compl.buffer.current_line;
248 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
249 message("No link selected");
250 return;
253 l = vl->parent->meta.data;
254 exit_minibuffer();
256 load_url_in_tab(current_tab(), l->meta.alt);
259 void
260 swiper_select(void)
262 struct line *l;
263 struct vline *vl;
264 struct tab *tab;
266 vl = ministate.compl.buffer.current_line;
268 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
269 message("No line selected");
270 return;
273 l = vl->parent->meta.data;
274 exit_minibuffer();
276 tab = current_tab();
278 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
279 if (vl->parent == l)
280 break;
283 if (vl == NULL)
284 message("Ops, swiper error! Please report to %s",
285 PACKAGE_BUGREPORT);
286 else
287 tab->buffer.current_line = vl;
290 static void
291 yornp_self_insert(void)
293 if (thiskey.key != 'y' && thiskey.key != 'n') {
294 message("Please answer y or n");
295 return;
298 exit_minibuffer();
299 yornp_cb(thiskey.key == 'y', yornp_data);
302 static void
303 yornp_abort(void)
305 exit_minibuffer();
306 yornp_cb(0, yornp_data);
309 static void
310 read_self_insert(void)
312 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
313 global_key_unbound();
314 return;
317 minibuffer_self_insert();
320 static void
321 read_abort(void)
323 exit_minibuffer();
324 read_cb(NULL, read_data);
327 static void
328 read_select(void)
330 exit_minibuffer();
331 minibuffer_hist_save_entry();
332 read_cb(ministate.buf, read_data);
335 /*
336 * TODO: we should collect this asynchronously...
337 */
338 static inline void
339 populate_compl_buffer(complfn *fn, void *data)
341 const char *s;
342 struct line *l;
343 struct buffer *b;
344 struct parser *p;
345 void *linedata = NULL;
347 b = &ministate.compl.buffer;
348 p = &b->page;
350 while ((s = fn(&data, &linedata)) != NULL) {
351 if ((l = calloc(1, sizeof(*l))) == NULL)
352 abort();
354 l->type = LINE_COMPL;
355 l->meta.data = linedata;
356 if ((l->line = strdup(s)) == NULL)
357 abort();
359 if (TAILQ_EMPTY(&p->head))
360 TAILQ_INSERT_HEAD(&p->head, l, lines);
361 else
362 TAILQ_INSERT_TAIL(&p->head, l, lines);
364 linedata = NULL;
367 if ((l = TAILQ_FIRST(&p->head)) != NULL)
368 l->type = LINE_COMPL_CURRENT;
371 void
372 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
373 void (*abortfn)(void), struct histhead *hist,
374 complfn *complfn, void *compldata)
376 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
377 if (in_minibuffer == MB_COMPREAD) {
378 ui_schedule_redraw();
380 ministate.compl.fn = complfn;
381 ministate.compl.data = compldata;
382 populate_compl_buffer(complfn, compldata);
385 base_map = &minibuffer_map;
386 current_map = &minibuffer_map;
388 base_map->unhandled_input = self_insert_fn;
390 ministate.donefn = donefn;
391 ministate.abortfn = abortfn;
392 memset(ministate.buf, 0, sizeof(ministate.buf));
393 ministate.buffer.current_line = &ministate.vline;
394 ministate.buffer.current_line->line = ministate.buf;
395 ministate.buffer.cpoff = 0;
396 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
398 ministate.history = hist;
399 ministate.hist_cur = NULL;
400 ministate.hist_off = 0;
403 void
404 exit_minibuffer(void)
406 if (in_minibuffer == MB_COMPREAD) {
407 erase_buffer(&ministate.compl.buffer);
408 ui_schedule_redraw();
411 in_minibuffer = 0;
412 base_map = &global_map;
413 current_map = &global_map;
416 void
417 yornp(const char *prompt, void (*fn)(int, struct tab*),
418 struct tab *data)
420 size_t len;
422 if (in_minibuffer) {
423 fn(0, data);
424 return;
427 yornp_cb = fn;
428 yornp_data = data;
429 enter_minibuffer(yornp_self_insert, yornp_self_insert,
430 yornp_abort, NULL, NULL, NULL);
432 len = sizeof(ministate.prompt);
433 strlcpy(ministate.prompt, prompt, len);
434 strlcat(ministate.prompt, " (y or n) ", len);
437 /*
438 * Not yet "completing", but soon maybe...
439 */
440 void
441 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
442 struct tab *data)
444 size_t len;
446 if (in_minibuffer)
447 return;
449 read_cb = fn;
450 read_data = data;
451 enter_minibuffer(read_self_insert, read_select, read_abort,
452 &read_history, NULL, NULL);
454 len = sizeof(ministate.prompt);
455 strlcpy(ministate.prompt, prompt, len);
456 strlcat(ministate.prompt, ": ", len);