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 "compat.h"
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "fs.h"
26 #include "iri.h"
27 #include "minibuffer.h"
28 #include "session.h"
29 #include "ui.h"
30 #include "utf8.h"
31 #include "utils.h"
33 #define nitems(x) (sizeof(x)/sizeof(x[0]))
35 static void *minibuffer_metadata(void);
36 static const char *minibuffer_compl_text(void);
37 static void minibuffer_hist_save_entry(void);
38 static void yornp_self_insert(void);
39 static void yornp_abort(void);
40 static void read_self_insert(void);
41 static void read_abort(void);
42 static void read_select(void);
43 static void handle_clear_echoarea(int, short, void *);
45 static struct event clechoev;
46 static struct timeval clechoev_timer = { 5, 0 };
48 static void (*yornp_cb)(int, struct tab *);
49 static struct tab *yornp_data;
51 static void (*read_cb)(const char*, struct tab *);
52 static struct tab *read_data;
54 /* XXX: don't forget to init these in minibuffer_init */
55 struct histhead eecmd_history,
56 ir_history,
57 lu_history,
58 read_history;
60 struct ministate ministate;
62 struct buffer minibufferwin;
64 int in_minibuffer;
66 static inline int
67 matches(char **words, size_t len, struct line *l)
68 {
69 size_t i;
70 int lm, am;
72 for (i = 0; i < len; ++i) {
73 lm = am = 0;
75 if (strcasestr(l->line, words[i]) != NULL)
76 lm = 1;
77 if (l->alt != NULL &&
78 strcasestr(l->alt, words[i]) != NULL)
79 am = 1;
81 if (!lm && !am)
82 return 0;
83 }
85 return 1;
86 }
88 /*
89 * Recompute the visible completions. If add is 1, don't consider the
90 * ones already hidden.
91 */
92 void
93 recompute_completions(int add)
94 {
95 static char buf[GEMINI_URL_LEN];
96 const char *text;
97 char *input, **ap, *words[10];
98 size_t len = 0;
99 struct line *l;
100 struct vline *vl;
101 struct buffer *b;
103 if (in_minibuffer != MB_COMPREAD)
104 return;
106 if (ministate.hist_cur != NULL)
107 text = ministate.hist_cur->h;
108 else
109 text = ministate.buf;
111 strlcpy(buf, text, sizeof(buf));
112 input = buf;
114 /* tokenize the input */
115 for (ap = words; ap < words + nitems(words) &&
116 (*ap = strsep(&input, " ")) != NULL;) {
117 if (**ap != '\0')
118 ap++, len++;
121 b = &ministate.compl.buffer;
122 TAILQ_FOREACH(l, &b->page.head, lines) {
123 l->type = LINE_COMPL;
124 if (add && l->flags & L_HIDDEN)
125 continue;
126 if (matches(words, len, l)) {
127 if (l->flags & L_HIDDEN)
128 b->line_max++;
129 l->flags &= ~L_HIDDEN;
130 } else {
131 if (!(l->flags & L_HIDDEN))
132 b->line_max--;
133 l->flags |= L_HIDDEN;
137 if (b->current_line == NULL)
138 b->current_line = TAILQ_FIRST(&b->head);
139 b->current_line = adjust_line(b->current_line, b);
140 vl = b->current_line;
141 if (ministate.compl.must_select && vl != NULL)
142 vl->parent->type = LINE_COMPL_CURRENT;
145 int
146 minibuffer_insert_current_candidate(void)
148 struct vline *vl;
150 vl = ministate.compl.buffer.current_line;
151 if (vl == NULL || vl->parent->flags & L_HIDDEN)
152 return -1;
154 minibuffer_taint_hist();
155 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
156 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
158 return 0;
161 static void *
162 minibuffer_metadata(void)
164 struct vline *vl;
166 vl = ministate.compl.buffer.current_line;
168 if (vl == NULL || vl->parent->flags & L_HIDDEN)
169 return NULL;
171 return vl->parent->data;
174 static const char *
175 minibuffer_compl_text(void)
177 struct vline *vl;
179 if (ministate.hist_cur != NULL)
180 return ministate.hist_cur->h;
182 vl = ministate.compl.buffer.current_line;
183 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
184 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
185 return ministate.buf;
186 return vl->parent->line;
189 static void
190 minibuffer_hist_save_entry(void)
192 struct hist *hist;
193 const char *t;
195 if (ministate.history == NULL)
196 return;
198 if ((hist = calloc(1, sizeof(*hist))) == NULL)
199 abort();
201 t = minibuffer_compl_text();
202 strlcpy(hist->h, t, sizeof(hist->h));
204 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
205 ministate.history->len++;
208 /*
209 * taint the minibuffer cache: if we're currently showing a history
210 * element, copy that to the current buf and reset the "history
211 * navigation" thing.
212 */
213 void
214 minibuffer_taint_hist(void)
216 if (ministate.hist_cur == NULL)
217 return;
219 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
220 ministate.hist_cur = NULL;
221 ministate.buffer.current_line->parent->line = ministate.buf;
224 void
225 minibuffer_self_insert(void)
227 char *c, tmp[5] = {0};
228 size_t len;
230 minibuffer_taint_hist();
232 if (thiskey.cp == 0)
233 return;
235 len = utf8_encode(thiskey.cp, tmp);
236 c = utf8_nth(ministate.buffer.current_line->parent->line,
237 ministate.buffer.cpoff);
238 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
239 return;
241 memmove(c + len, c, strlen(c)+1);
242 memcpy(c, tmp, len);
243 ministate.buffer.cpoff++;
245 recompute_completions(1);
248 void
249 sensible_self_insert(void)
251 if (thiskey.meta ||
252 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
253 global_key_unbound();
254 return;
257 minibuffer_self_insert();
260 void
261 eecmd_select(void)
263 struct cmd *cmd;
264 const char *t;
266 t = minibuffer_compl_text();
267 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
268 if (!strcmp(cmd->cmd, t)) {
269 minibuffer_hist_save_entry();
270 exit_minibuffer();
271 cmd->fn(current_buffer());
272 return;
276 message("No match");
279 void
280 ir_select_gemini(void)
282 static struct iri iri;
283 char buf[1025];
284 struct tab *tab = current_tab;
286 minibuffer_hist_save_entry();
288 if (iri_parse(NULL, tab->hist_cur->h, &iri) == -1)
289 goto err;
290 if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
291 goto err;
292 if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
293 goto err;
295 exit_minibuffer();
296 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
297 return;
299 err:
300 message("Failed to select URL.");
303 void
304 ir_select_reply(void)
306 char buf[1025] = {0};
307 struct phos_uri uri;
308 struct tab *tab = current_tab;
310 minibuffer_hist_save_entry();
312 /* a bit ugly but... */
313 strlcpy(buf, tab->last_input_url, sizeof(buf));
314 phos_parse_absolute_uri(buf, &uri);
315 phos_uri_set_query(&uri, minibuffer_compl_text());
316 phos_serialize_uri(&uri, buf, sizeof(buf));
318 exit_minibuffer();
319 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
322 void
323 ir_select_gopher(void)
325 minibuffer_hist_save_entry();
326 gopher_send_search_req(current_tab, minibuffer_compl_text());
327 exit_minibuffer();
330 void
331 lu_select(void)
333 char url[GEMINI_URL_LEN+1];
335 minibuffer_hist_save_entry();
336 humanify_url(minibuffer_compl_text(), url, sizeof(url));
337 exit_minibuffer();
338 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
341 void
342 bp_select(void)
344 exit_minibuffer();
345 if (*ministate.buf != '\0') {
346 if (!bookmark_page(ministate.buf))
347 message("failed to bookmark page: %s",
348 strerror(errno));
349 } else
350 message("Abort.");
353 void
354 ts_select(void)
356 struct tab *tab;
358 if ((tab = minibuffer_metadata()) == NULL) {
359 message("No tab selected");
360 return;
363 exit_minibuffer();
364 switch_to_tab(tab);
367 void
368 ls_select(void)
370 struct line *l;
372 if ((l = minibuffer_metadata()) == NULL) {
373 message("No link selected");
374 return;
377 exit_minibuffer();
378 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
381 static inline void
382 jump_to_line(struct line *l)
384 struct vline *vl;
385 struct buffer *buffer;
387 buffer = current_buffer();
389 TAILQ_FOREACH(vl, &buffer->head, vlines) {
390 if (vl->parent == l)
391 break;
394 if (vl == NULL)
395 message("Ops, %s error! Please report to %s",
396 __func__, PACKAGE_BUGREPORT);
397 else {
398 buffer->top_line = vl;
399 buffer->current_line = vl;
403 void
404 swiper_select(void)
406 struct line *l;
408 if ((l = minibuffer_metadata()) == NULL) {
409 message("No line selected");
410 return;
413 exit_minibuffer();
414 jump_to_line(l);
417 void
418 toc_select(void)
420 struct line *l;
422 if ((l = minibuffer_metadata()) == NULL) {
423 message("No line selected");
424 return;
427 exit_minibuffer();
428 jump_to_line(l);
431 static void
432 yornp_self_insert(void)
434 if (thiskey.key != 'y' && thiskey.key != 'n') {
435 message("Please answer y or n");
436 return;
439 exit_minibuffer();
440 yornp_cb(thiskey.key == 'y', yornp_data);
443 static void
444 yornp_abort(void)
446 exit_minibuffer();
447 yornp_cb(0, yornp_data);
450 static void
451 read_self_insert(void)
453 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
454 global_key_unbound();
455 return;
458 minibuffer_self_insert();
461 static void
462 read_abort(void)
464 exit_minibuffer();
465 read_cb(NULL, read_data);
468 static void
469 read_select(void)
471 exit_minibuffer();
472 minibuffer_hist_save_entry();
473 read_cb(ministate.buf, read_data);
476 /*
477 * TODO: we should collect this asynchronously...
478 */
479 static inline void
480 populate_compl_buffer(complfn *fn, void *data)
482 const char *s, *descr;
483 struct line *l;
484 struct buffer *b;
485 struct parser *p;
486 void *linedata;
488 b = &ministate.compl.buffer;
489 p = &b->page;
491 linedata = NULL;
492 descr = NULL;
493 while ((s = fn(&data, &linedata, &descr)) != NULL) {
494 if ((l = calloc(1, sizeof(*l))) == NULL)
495 abort();
497 l->type = LINE_COMPL;
498 l->data = linedata;
499 l->alt = (char*)descr;
500 if ((l->line = strdup(s)) == NULL)
501 abort();
503 TAILQ_INSERT_TAIL(&p->head, l, lines);
505 linedata = NULL;
506 descr = NULL;
509 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
510 ministate.compl.must_select)
511 l->type = LINE_COMPL_CURRENT;
514 void
515 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
516 void (*abortfn)(void), struct histhead *hist,
517 complfn *complfn, void *compldata, int must_select)
519 ministate.compl.must_select = must_select;
520 ministate.compl.fn = complfn;
521 ministate.compl.data = compldata;
523 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
524 if (in_minibuffer == MB_COMPREAD) {
525 populate_compl_buffer(complfn, compldata);
526 ui_schedule_redraw();
529 base_map = &minibuffer_map;
530 current_map = &minibuffer_map;
532 base_map->unhandled_input = self_insert_fn;
534 ministate.donefn = donefn;
535 ministate.abortfn = abortfn;
536 memset(ministate.buf, 0, sizeof(ministate.buf));
537 ministate.buffer.current_line = &ministate.vline;
538 ministate.buffer.current_line->parent->line = ministate.buf;
539 ministate.buffer.cpoff = 0;
540 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
542 ministate.history = hist;
543 ministate.hist_cur = NULL;
544 ministate.hist_off = 0;
547 void
548 exit_minibuffer(void)
550 if (in_minibuffer == MB_COMPREAD) {
551 erase_buffer(&ministate.compl.buffer);
552 ui_schedule_redraw();
555 in_minibuffer = 0;
556 base_map = &global_map;
557 current_map = &global_map;
560 void
561 yornp(const char *prompt, void (*fn)(int, struct tab*),
562 struct tab *data)
564 size_t len;
566 if (in_minibuffer) {
567 fn(0, data);
568 return;
571 yornp_cb = fn;
572 yornp_data = data;
573 enter_minibuffer(yornp_self_insert, yornp_self_insert,
574 yornp_abort, NULL, NULL, NULL, 0);
576 len = sizeof(ministate.prompt);
577 strlcpy(ministate.prompt, prompt, len);
578 strlcat(ministate.prompt, " (y or n) ", len);
581 void
582 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
583 struct tab *data)
585 size_t len;
587 if (in_minibuffer)
588 return;
590 read_cb = fn;
591 read_data = data;
592 enter_minibuffer(read_self_insert, read_select, read_abort,
593 &read_history, NULL, NULL, 0);
595 len = sizeof(ministate.prompt);
596 strlcpy(ministate.prompt, prompt, len);
597 strlcat(ministate.prompt, ": ", len);
600 static void
601 handle_clear_echoarea(int fd, short ev, void *d)
603 free(ministate.curmesg);
604 ministate.curmesg = NULL;
606 ui_after_message_hook();
609 void
610 vmessage(const char *fmt, va_list ap)
612 if (evtimer_pending(&clechoev, NULL))
613 evtimer_del(&clechoev);
615 free(ministate.curmesg);
616 ministate.curmesg = NULL;
618 if (fmt != NULL) {
619 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
620 evtimer_add(&clechoev, &clechoev_timer);
622 /* TODO: what to do if the allocation fails here? */
623 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
624 ministate.curmesg = NULL;
627 ui_after_message_hook();
630 void
631 message(const char *fmt, ...)
633 va_list ap;
635 va_start(ap, fmt);
636 vmessage(fmt, ap);
637 va_end(ap);
640 void
641 minibuffer_init(void)
643 TAILQ_INIT(&eecmd_history.head);
644 TAILQ_INIT(&ir_history.head);
645 TAILQ_INIT(&lu_history.head);
646 TAILQ_INIT(&read_history.head);
648 TAILQ_INIT(&ministate.compl.buffer.head);
649 TAILQ_INIT(&ministate.compl.buffer.page.head);
651 ministate.line.type = LINE_TEXT;
652 ministate.vline.parent = &ministate.line;
653 ministate.buffer.page.name = "*minibuffer*";
654 ministate.buffer.current_line = &ministate.vline;
656 evtimer_set(&clechoev, handle_clear_echoarea, NULL);