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->parent->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->parent->line,
236 ministate.buffer.cpoff);
237 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
238 return;
240 memmove(c + len, c, strlen(c)+1);
241 memcpy(c, tmp, len);
242 ministate.buffer.cpoff++;
244 recompute_completions(1);
247 void
248 sensible_self_insert(void)
250 if (thiskey.meta ||
251 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
252 global_key_unbound();
253 return;
256 minibuffer_self_insert();
259 void
260 eecmd_select(void)
262 struct cmd *cmd;
263 const char *t;
265 t = minibuffer_compl_text();
266 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
267 if (!strcmp(cmd->cmd, t)) {
268 minibuffer_hist_save_entry();
269 exit_minibuffer();
270 cmd->fn(current_buffer());
271 return;
275 message("No match");
278 void
279 ir_select_gemini(void)
281 char buf[1025] = {0};
282 struct phos_uri uri;
283 struct tab *tab = current_tab;
285 minibuffer_hist_save_entry();
287 /* a bit ugly but... */
288 memcpy(&uri, &tab->uri, sizeof(tab->uri));
289 phos_uri_set_query(&uri, minibuffer_compl_text());
290 phos_serialize_uri(&uri, buf, sizeof(buf));
292 exit_minibuffer();
293 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
296 void
297 ir_select_reply(void)
299 char buf[1025] = {0};
300 struct phos_uri uri;
301 struct tab *tab = current_tab;
303 minibuffer_hist_save_entry();
305 /* a bit ugly but... */
306 strlcpy(buf, tab->last_input_url, sizeof(buf));
307 phos_parse_absolute_uri(buf, &uri);
308 phos_uri_set_query(&uri, minibuffer_compl_text());
309 phos_serialize_uri(&uri, buf, sizeof(buf));
311 exit_minibuffer();
312 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
315 void
316 ir_select_gopher(void)
318 minibuffer_hist_save_entry();
319 gopher_send_search_req(current_tab, minibuffer_compl_text());
320 exit_minibuffer();
323 void
324 lu_select(void)
326 char url[GEMINI_URL_LEN+1];
328 minibuffer_hist_save_entry();
329 humanify_url(minibuffer_compl_text(), url, sizeof(url));
330 exit_minibuffer();
331 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
334 void
335 bp_select(void)
337 exit_minibuffer();
338 if (*ministate.buf != '\0') {
339 if (!bookmark_page(ministate.buf))
340 message("failed to bookmark page: %s",
341 strerror(errno));
342 } else
343 message("Abort.");
346 void
347 ts_select(void)
349 struct tab *tab;
351 if ((tab = minibuffer_metadata()) == NULL) {
352 message("No tab selected");
353 return;
356 exit_minibuffer();
357 switch_to_tab(tab);
360 void
361 ls_select(void)
363 struct line *l;
365 if ((l = minibuffer_metadata()) == NULL) {
366 message("No link selected");
367 return;
370 exit_minibuffer();
371 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
374 static inline void
375 jump_to_line(struct line *l)
377 struct vline *vl;
378 struct buffer *buffer;
380 buffer = current_buffer();
382 TAILQ_FOREACH(vl, &buffer->head, vlines) {
383 if (vl->parent == l)
384 break;
387 if (vl == NULL)
388 message("Ops, %s error! Please report to %s",
389 __func__, PACKAGE_BUGREPORT);
390 else {
391 buffer->top_line = vl;
392 buffer->current_line = vl;
396 void
397 swiper_select(void)
399 struct line *l;
401 if ((l = minibuffer_metadata()) == NULL) {
402 message("No line selected");
403 return;
406 exit_minibuffer();
407 jump_to_line(l);
410 void
411 toc_select(void)
413 struct line *l;
415 if ((l = minibuffer_metadata()) == NULL) {
416 message("No line selected");
417 return;
420 exit_minibuffer();
421 jump_to_line(l);
424 static void
425 yornp_self_insert(void)
427 if (thiskey.key != 'y' && thiskey.key != 'n') {
428 message("Please answer y or n");
429 return;
432 exit_minibuffer();
433 yornp_cb(thiskey.key == 'y', yornp_data);
436 static void
437 yornp_abort(void)
439 exit_minibuffer();
440 yornp_cb(0, yornp_data);
443 static void
444 read_self_insert(void)
446 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
447 global_key_unbound();
448 return;
451 minibuffer_self_insert();
454 static void
455 read_abort(void)
457 exit_minibuffer();
458 read_cb(NULL, read_data);
461 static void
462 read_select(void)
464 exit_minibuffer();
465 minibuffer_hist_save_entry();
466 read_cb(ministate.buf, read_data);
469 /*
470 * TODO: we should collect this asynchronously...
471 */
472 static inline void
473 populate_compl_buffer(complfn *fn, void *data)
475 const char *s, *descr;
476 struct line *l;
477 struct buffer *b;
478 struct parser *p;
479 void *linedata;
481 b = &ministate.compl.buffer;
482 p = &b->page;
484 linedata = NULL;
485 descr = NULL;
486 while ((s = fn(&data, &linedata, &descr)) != NULL) {
487 if ((l = calloc(1, sizeof(*l))) == NULL)
488 abort();
490 l->type = LINE_COMPL;
491 l->data = linedata;
492 l->alt = (char*)descr;
493 if ((l->line = strdup(s)) == NULL)
494 abort();
496 TAILQ_INSERT_TAIL(&p->head, l, lines);
498 linedata = NULL;
499 descr = NULL;
502 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
503 ministate.compl.must_select)
504 l->type = LINE_COMPL_CURRENT;
507 void
508 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
509 void (*abortfn)(void), struct histhead *hist,
510 complfn *complfn, void *compldata, int must_select)
512 ministate.compl.must_select = must_select;
513 ministate.compl.fn = complfn;
514 ministate.compl.data = compldata;
516 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
517 if (in_minibuffer == MB_COMPREAD) {
518 populate_compl_buffer(complfn, compldata);
519 ui_schedule_redraw();
522 base_map = &minibuffer_map;
523 current_map = &minibuffer_map;
525 base_map->unhandled_input = self_insert_fn;
527 ministate.donefn = donefn;
528 ministate.abortfn = abortfn;
529 memset(ministate.buf, 0, sizeof(ministate.buf));
530 ministate.buffer.current_line = &ministate.vline;
531 ministate.buffer.current_line->parent->line = ministate.buf;
532 ministate.buffer.cpoff = 0;
533 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
535 ministate.history = hist;
536 ministate.hist_cur = NULL;
537 ministate.hist_off = 0;
540 void
541 exit_minibuffer(void)
543 if (in_minibuffer == MB_COMPREAD) {
544 erase_buffer(&ministate.compl.buffer);
545 ui_schedule_redraw();
548 in_minibuffer = 0;
549 base_map = &global_map;
550 current_map = &global_map;
553 void
554 yornp(const char *prompt, void (*fn)(int, struct tab*),
555 struct tab *data)
557 size_t len;
559 if (in_minibuffer) {
560 fn(0, data);
561 return;
564 yornp_cb = fn;
565 yornp_data = data;
566 enter_minibuffer(yornp_self_insert, yornp_self_insert,
567 yornp_abort, NULL, NULL, NULL, 0);
569 len = sizeof(ministate.prompt);
570 strlcpy(ministate.prompt, prompt, len);
571 strlcat(ministate.prompt, " (y or n) ", len);
574 void
575 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
576 struct tab *data)
578 size_t len;
580 if (in_minibuffer)
581 return;
583 read_cb = fn;
584 read_data = data;
585 enter_minibuffer(read_self_insert, read_select, read_abort,
586 &read_history, NULL, NULL, 0);
588 len = sizeof(ministate.prompt);
589 strlcpy(ministate.prompt, prompt, len);
590 strlcat(ministate.prompt, ": ", len);
593 static void
594 handle_clear_echoarea(int fd, short ev, void *d)
596 free(ministate.curmesg);
597 ministate.curmesg = NULL;
599 ui_after_message_hook();
602 void
603 vmessage(const char *fmt, va_list ap)
605 if (evtimer_pending(&clechoev, NULL))
606 evtimer_del(&clechoev);
608 free(ministate.curmesg);
609 ministate.curmesg = NULL;
611 if (fmt != NULL) {
612 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
613 evtimer_add(&clechoev, &clechoev_timer);
615 /* TODO: what to do if the allocation fails here? */
616 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
617 ministate.curmesg = NULL;
620 ui_after_message_hook();
623 void
624 message(const char *fmt, ...)
626 va_list ap;
628 va_start(ap, fmt);
629 vmessage(fmt, ap);
630 va_end(ap);
633 void
634 minibuffer_init(void)
636 TAILQ_INIT(&eecmd_history.head);
637 TAILQ_INIT(&ir_history.head);
638 TAILQ_INIT(&lu_history.head);
639 TAILQ_INIT(&read_history.head);
641 TAILQ_INIT(&ministate.compl.buffer.head);
642 TAILQ_INIT(&ministate.compl.buffer.page.head);
644 ministate.line.type = LINE_TEXT;
645 ministate.vline.parent = &ministate.line;
646 ministate.buffer.page.name = "*minibuffer*";
647 ministate.buffer.current_line = &ministate.vline;
649 evtimer_set(&clechoev, handle_clear_echoarea, NULL);