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 "minibuffer.h"
27 #include "session.h"
28 #include "ui.h"
29 #include "utf8.h"
30 #include "utils.h"
32 #define nitems(x) (sizeof(x)/sizeof(x[0]))
34 static void *minibuffer_metadata(void);
35 static const char *minibuffer_compl_text(void);
36 static void minibuffer_hist_save_entry(void);
37 static void yornp_self_insert(void);
38 static void yornp_abort(void);
39 static void read_self_insert(void);
40 static void read_abort(void);
41 static void read_select(void);
42 static void handle_clear_echoarea(int, short, void *);
44 static struct event clechoev;
45 static struct timeval clechoev_timer = { 5, 0 };
47 static void (*yornp_cb)(int, struct tab *);
48 static struct tab *yornp_data;
50 static void (*read_cb)(const char*, struct tab *);
51 static struct tab *read_data;
53 /* XXX: don't forget to init these in minibuffer_init */
54 struct histhead eecmd_history,
55 ir_history,
56 lu_history,
57 read_history;
59 struct ministate ministate;
61 struct buffer minibufferwin;
63 int in_minibuffer;
65 static inline int
66 matches(char **words, size_t len, struct line *l)
67 {
68 size_t i;
69 int lm, am;
71 for (i = 0; i < len; ++i) {
72 lm = am = 0;
74 if (strcasestr(l->line, words[i]) != NULL)
75 lm = 1;
76 if (l->alt != NULL &&
77 strcasestr(l->alt, words[i]) != NULL)
78 am = 1;
80 if (!lm && !am)
81 return 0;
82 }
84 return 1;
85 }
87 /*
88 * Recompute the visible completions. If add is 1, don't consider the
89 * ones already hidden.
90 */
91 void
92 recompute_completions(int add)
93 {
94 static char buf[GEMINI_URL_LEN];
95 const char *text;
96 char *input, **ap, *words[10];
97 size_t len = 0;
98 struct line *l;
99 struct vline *vl;
100 struct buffer *b;
102 if (in_minibuffer != MB_COMPREAD)
103 return;
105 if (ministate.hist_cur != NULL)
106 text = ministate.hist_cur->h;
107 else
108 text = ministate.buf;
110 strlcpy(buf, text, sizeof(buf));
111 input = buf;
113 /* tokenize the input */
114 for (ap = words; ap < words + nitems(words) &&
115 (*ap = strsep(&input, " ")) != NULL;) {
116 if (**ap != '\0')
117 ap++, len++;
120 b = &ministate.compl.buffer;
121 TAILQ_FOREACH(l, &b->page.head, lines) {
122 l->type = LINE_COMPL;
123 if (add && l->flags & L_HIDDEN)
124 continue;
125 if (matches(words, len, l)) {
126 if (l->flags & L_HIDDEN)
127 b->line_max++;
128 l->flags &= ~L_HIDDEN;
129 } else {
130 if (!(l->flags & L_HIDDEN))
131 b->line_max--;
132 l->flags |= L_HIDDEN;
136 if (b->current_line == NULL)
137 b->current_line = TAILQ_FIRST(&b->head);
138 b->current_line = adjust_line(b->current_line, b);
139 vl = b->current_line;
140 if (ministate.compl.must_select && vl != NULL)
141 vl->parent->type = LINE_COMPL_CURRENT;
144 int
145 minibuffer_insert_current_candidate(void)
147 struct vline *vl;
149 vl = ministate.compl.buffer.current_line;
150 if (vl == NULL || vl->parent->flags & L_HIDDEN)
151 return -1;
153 minibuffer_taint_hist();
154 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
155 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
157 return 0;
160 static void *
161 minibuffer_metadata(void)
163 struct vline *vl;
165 vl = ministate.compl.buffer.current_line;
167 if (vl == NULL || vl->parent->flags & L_HIDDEN)
168 return NULL;
170 return vl->parent->data;
173 static const char *
174 minibuffer_compl_text(void)
176 struct vline *vl;
178 if (ministate.hist_cur != NULL)
179 return ministate.hist_cur->h;
181 vl = ministate.compl.buffer.current_line;
182 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
183 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
184 return ministate.buf;
185 return vl->parent->line;
188 static void
189 minibuffer_hist_save_entry(void)
191 struct hist *hist;
192 const char *t;
194 if (ministate.history == NULL)
195 return;
197 if ((hist = calloc(1, sizeof(*hist))) == NULL)
198 abort();
200 t = minibuffer_compl_text();
201 strlcpy(hist->h, t, sizeof(hist->h));
203 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
204 ministate.history->len++;
207 /*
208 * taint the minibuffer cache: if we're currently showing a history
209 * element, copy that to the current buf and reset the "history
210 * navigation" thing.
211 */
212 void
213 minibuffer_taint_hist(void)
215 if (ministate.hist_cur == NULL)
216 return;
218 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
219 ministate.hist_cur = NULL;
220 ministate.buffer.current_line->line = ministate.buf;
223 void
224 minibuffer_self_insert(void)
226 char *c, tmp[5] = {0};
227 size_t len;
229 minibuffer_taint_hist();
231 if (thiskey.cp == 0)
232 return;
234 len = utf8_encode(thiskey.cp, tmp);
235 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
236 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
237 return;
239 memmove(c + len, c, strlen(c)+1);
240 memcpy(c, tmp, len);
241 ministate.buffer.cpoff++;
243 recompute_completions(1);
246 void
247 sensible_self_insert(void)
249 if (thiskey.meta ||
250 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
251 global_key_unbound();
252 return;
255 minibuffer_self_insert();
258 void
259 eecmd_select(void)
261 struct cmd *cmd;
262 const char *t;
264 t = minibuffer_compl_text();
265 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
266 if (!strcmp(cmd->cmd, t)) {
267 minibuffer_hist_save_entry();
268 exit_minibuffer();
269 cmd->fn(current_buffer());
270 return;
274 message("No match");
277 void
278 ir_select_gemini(void)
280 char buf[1025] = {0};
281 struct phos_uri uri;
282 struct tab *tab = current_tab;
284 minibuffer_hist_save_entry();
286 /* a bit ugly but... */
287 memcpy(&uri, &tab->uri, sizeof(tab->uri));
288 phos_uri_set_query(&uri, minibuffer_compl_text());
289 phos_serialize_uri(&uri, buf, sizeof(buf));
291 exit_minibuffer();
292 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
295 void
296 ir_select_reply(void)
298 char buf[1025] = {0};
299 struct phos_uri uri;
300 struct tab *tab = current_tab;
302 minibuffer_hist_save_entry();
304 /* a bit ugly but... */
305 strlcpy(buf, tab->last_input_url, sizeof(buf));
306 phos_parse_absolute_uri(buf, &uri);
307 phos_uri_set_query(&uri, minibuffer_compl_text());
308 phos_serialize_uri(&uri, buf, sizeof(buf));
310 exit_minibuffer();
311 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
314 void
315 ir_select_gopher(void)
317 minibuffer_hist_save_entry();
318 gopher_send_search_req(current_tab, minibuffer_compl_text());
319 exit_minibuffer();
322 void
323 lu_select(void)
325 char url[GEMINI_URL_LEN+1];
327 minibuffer_hist_save_entry();
328 humanify_url(minibuffer_compl_text(), url, sizeof(url));
329 exit_minibuffer();
330 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
333 void
334 bp_select(void)
336 exit_minibuffer();
337 if (*ministate.buf != '\0') {
338 if (!bookmark_page(ministate.buf))
339 message("failed to bookmark page: %s",
340 strerror(errno));
341 } else
342 message("Abort.");
345 void
346 ts_select(void)
348 struct tab *tab;
350 if ((tab = minibuffer_metadata()) == NULL) {
351 message("No tab selected");
352 return;
355 exit_minibuffer();
356 switch_to_tab(tab);
359 void
360 ls_select(void)
362 struct line *l;
364 if ((l = minibuffer_metadata()) == NULL) {
365 message("No link selected");
366 return;
369 exit_minibuffer();
370 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
373 static inline void
374 jump_to_line(struct line *l)
376 struct vline *vl;
377 struct buffer *buffer;
379 buffer = current_buffer();
381 TAILQ_FOREACH(vl, &buffer->head, vlines) {
382 if (vl->parent == l)
383 break;
386 if (vl == NULL)
387 message("Ops, %s error! Please report to %s",
388 __func__, PACKAGE_BUGREPORT);
389 else {
390 buffer->top_line = vl;
391 buffer->current_line = vl;
395 void
396 swiper_select(void)
398 struct line *l;
400 if ((l = minibuffer_metadata()) == NULL) {
401 message("No line selected");
402 return;
405 exit_minibuffer();
406 jump_to_line(l);
409 void
410 toc_select(void)
412 struct line *l;
414 if ((l = minibuffer_metadata()) == NULL) {
415 message("No line selected");
416 return;
419 exit_minibuffer();
420 jump_to_line(l);
423 static void
424 yornp_self_insert(void)
426 if (thiskey.key != 'y' && thiskey.key != 'n') {
427 message("Please answer y or n");
428 return;
431 exit_minibuffer();
432 yornp_cb(thiskey.key == 'y', yornp_data);
435 static void
436 yornp_abort(void)
438 exit_minibuffer();
439 yornp_cb(0, yornp_data);
442 static void
443 read_self_insert(void)
445 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
446 global_key_unbound();
447 return;
450 minibuffer_self_insert();
453 static void
454 read_abort(void)
456 exit_minibuffer();
457 read_cb(NULL, read_data);
460 static void
461 read_select(void)
463 exit_minibuffer();
464 minibuffer_hist_save_entry();
465 read_cb(ministate.buf, read_data);
468 /*
469 * TODO: we should collect this asynchronously...
470 */
471 static inline void
472 populate_compl_buffer(complfn *fn, void *data)
474 const char *s, *descr;
475 struct line *l;
476 struct buffer *b;
477 struct parser *p;
478 void *linedata;
480 b = &ministate.compl.buffer;
481 p = &b->page;
483 linedata = NULL;
484 descr = NULL;
485 while ((s = fn(&data, &linedata, &descr)) != NULL) {
486 if ((l = calloc(1, sizeof(*l))) == NULL)
487 abort();
489 l->type = LINE_COMPL;
490 l->data = linedata;
491 l->alt = (char*)descr;
492 if ((l->line = strdup(s)) == NULL)
493 abort();
495 TAILQ_INSERT_TAIL(&p->head, l, lines);
497 linedata = NULL;
498 descr = NULL;
501 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
502 ministate.compl.must_select)
503 l->type = LINE_COMPL_CURRENT;
506 void
507 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
508 void (*abortfn)(void), struct histhead *hist,
509 complfn *complfn, void *compldata, int must_select)
511 ministate.compl.must_select = must_select;
512 ministate.compl.fn = complfn;
513 ministate.compl.data = compldata;
515 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
516 if (in_minibuffer == MB_COMPREAD) {
517 populate_compl_buffer(complfn, compldata);
518 ui_schedule_redraw();
521 base_map = &minibuffer_map;
522 current_map = &minibuffer_map;
524 base_map->unhandled_input = self_insert_fn;
526 ministate.donefn = donefn;
527 ministate.abortfn = abortfn;
528 memset(ministate.buf, 0, sizeof(ministate.buf));
529 ministate.buffer.current_line = &ministate.vline;
530 ministate.buffer.current_line->line = ministate.buf;
531 ministate.buffer.cpoff = 0;
532 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
534 ministate.history = hist;
535 ministate.hist_cur = NULL;
536 ministate.hist_off = 0;
539 void
540 exit_minibuffer(void)
542 if (in_minibuffer == MB_COMPREAD) {
543 erase_buffer(&ministate.compl.buffer);
544 ui_schedule_redraw();
547 in_minibuffer = 0;
548 base_map = &global_map;
549 current_map = &global_map;
552 void
553 yornp(const char *prompt, void (*fn)(int, struct tab*),
554 struct tab *data)
556 size_t len;
558 if (in_minibuffer) {
559 fn(0, data);
560 return;
563 yornp_cb = fn;
564 yornp_data = data;
565 enter_minibuffer(yornp_self_insert, yornp_self_insert,
566 yornp_abort, NULL, NULL, NULL, 0);
568 len = sizeof(ministate.prompt);
569 strlcpy(ministate.prompt, prompt, len);
570 strlcat(ministate.prompt, " (y or n) ", len);
573 void
574 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
575 struct tab *data)
577 size_t len;
579 if (in_minibuffer)
580 return;
582 read_cb = fn;
583 read_data = data;
584 enter_minibuffer(read_self_insert, read_select, read_abort,
585 &read_history, NULL, NULL, 0);
587 len = sizeof(ministate.prompt);
588 strlcpy(ministate.prompt, prompt, len);
589 strlcat(ministate.prompt, ": ", len);
592 static void
593 handle_clear_echoarea(int fd, short ev, void *d)
595 free(ministate.curmesg);
596 ministate.curmesg = NULL;
598 ui_after_message_hook();
601 void
602 vmessage(const char *fmt, va_list ap)
604 if (evtimer_pending(&clechoev, NULL))
605 evtimer_del(&clechoev);
607 free(ministate.curmesg);
608 ministate.curmesg = NULL;
610 if (fmt != NULL) {
611 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
612 evtimer_add(&clechoev, &clechoev_timer);
614 /* TODO: what to do if the allocation fails here? */
615 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
616 ministate.curmesg = NULL;
619 ui_after_message_hook();
622 void
623 message(const char *fmt, ...)
625 va_list ap;
627 va_start(ap, fmt);
628 vmessage(fmt, ap);
629 va_end(ap);
632 void
633 minibuffer_init(void)
635 TAILQ_INIT(&eecmd_history.head);
636 TAILQ_INIT(&ir_history.head);
637 TAILQ_INIT(&lu_history.head);
638 TAILQ_INIT(&read_history.head);
640 TAILQ_INIT(&ministate.compl.buffer.head);
641 TAILQ_INIT(&ministate.compl.buffer.page.head);
643 ministate.line.type = LINE_TEXT;
644 ministate.vline.parent = &ministate.line;
645 ministate.buffer.page.name = "*minibuffer*";
646 ministate.buffer.current_line = &ministate.vline;
648 evtimer_set(&clechoev, handle_clear_echoarea, NULL);