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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "minibuffer.h"
24 #include "session.h"
25 #include "ui.h"
26 #include "utf8.h"
27 #include "utils.h"
29 #define nitems(x) (sizeof(x)/sizeof(x[0]))
31 static void *minibuffer_metadata(void);
32 static void minibuffer_hist_save_entry(void);
33 static void yornp_self_insert(void);
34 static void yornp_abort(void);
35 static void read_self_insert(void);
36 static void read_abort(void);
37 static void read_select(void);
38 static void handle_clear_echoarea(int, short, void *);
40 static struct event clechoev;
41 static struct timeval clechoev_timer = { 5, 0 };
43 static void (*yornp_cb)(int, struct tab *);
44 static struct tab *yornp_data;
46 static void (*read_cb)(const char*, struct tab *);
47 static struct tab *read_data;
49 /* XXX: don't forget to init these in minibuffer_init */
50 struct histhead eecmd_history,
51 ir_history,
52 lu_history,
53 read_history;
55 struct ministate ministate;
57 struct buffer minibufferwin;
59 int in_minibuffer;
61 static inline int
62 matches(char **words, size_t len, struct line *l)
63 {
64 size_t i;
65 int lm, am;
67 for (i = 0; i < len; ++i) {
68 lm = am = 0;
70 if (strcasestr(l->line, words[i]) != NULL)
71 lm = 1;
72 if (l->alt != NULL &&
73 strcasestr(l->alt, words[i]) != NULL)
74 am = 1;
76 if (!lm && !am)
77 return 0;
78 }
80 return 1;
81 }
83 /*
84 * Recompute the visible completions. If add is 1, don't consider the
85 * ones already hidden.
86 */
87 void
88 recompute_completions(int add)
89 {
90 static char buf[GEMINI_URL_LEN];
91 char *input, **ap, *words[10];
92 size_t len = 0;
93 struct line *l;
94 struct vline *vl;
95 struct buffer *b;
97 if (in_minibuffer != MB_COMPREAD)
98 return;
100 strlcpy(buf, ministate.buf, sizeof(buf));
101 input = buf;
103 /* tokenize the input */
104 for (ap = words; ap < words + nitems(words) &&
105 (*ap = strsep(&input, " ")) != NULL;) {
106 if (**ap != '\0')
107 ap++, len++;
110 b = &ministate.compl.buffer;
111 TAILQ_FOREACH(l, &b->page.head, lines) {
112 l->type = LINE_COMPL;
113 if (add && l->flags & L_HIDDEN)
114 continue;
115 if (matches(words, len, l)) {
116 if (l->flags & L_HIDDEN)
117 b->line_max++;
118 l->flags &= ~L_HIDDEN;
119 } else {
120 if (!(l->flags & L_HIDDEN))
121 b->line_max--;
122 l->flags |= L_HIDDEN;
126 if (b->current_line == NULL)
127 b->current_line = TAILQ_FIRST(&b->head);
128 b->current_line = adjust_line(b->current_line, b);
129 vl = b->current_line;
130 if (vl != NULL)
131 vl->parent->type = LINE_COMPL_CURRENT;
134 int
135 minibuffer_insert_current_candidate(void)
137 struct vline *vl;
139 vl = ministate.compl.buffer.current_line;
140 if (vl == NULL || vl->parent->flags & L_HIDDEN)
141 return -1;
143 minibuffer_taint_hist();
144 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
145 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
147 return 0;
150 static void *
151 minibuffer_metadata(void)
153 struct vline *vl;
155 vl = ministate.compl.buffer.current_line;
157 if (vl == NULL || vl->parent->flags & L_HIDDEN)
158 return NULL;
160 return vl->parent->data;
163 static void
164 minibuffer_hist_save_entry(void)
166 struct hist *hist;
168 if (ministate.history == NULL)
169 return;
171 if ((hist = calloc(1, sizeof(*hist))) == NULL)
172 abort();
174 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
176 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
177 ministate.history->len++;
180 /*
181 * taint the minibuffer cache: if we're currently showing a history
182 * element, copy that to the current buf and reset the "history
183 * navigation" thing.
184 */
185 void
186 minibuffer_taint_hist(void)
188 if (ministate.hist_cur == NULL)
189 return;
191 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
192 ministate.hist_cur = NULL;
193 ministate.buffer.current_line->line = ministate.buf;
196 void
197 minibuffer_self_insert(void)
199 char *c, tmp[5] = {0};
200 size_t len;
202 minibuffer_taint_hist();
204 if (thiskey.cp == 0)
205 return;
207 len = utf8_encode(thiskey.cp, tmp);
208 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
209 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
210 return;
212 memmove(c + len, c, strlen(c)+1);
213 memcpy(c, tmp, len);
214 ministate.buffer.cpoff++;
216 recompute_completions(1);
219 void
220 sensible_self_insert(void)
222 if (thiskey.meta ||
223 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
224 global_key_unbound();
225 return;
228 minibuffer_self_insert();
231 void
232 eecmd_select(void)
234 struct cmd *cmd;
235 struct vline *vl;
236 const char *t;
238 vl = ministate.compl.buffer.current_line;
239 if (vl == NULL || vl->parent->flags & L_HIDDEN)
240 goto end;
242 t = vl->parent->line;
243 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
244 if (!strcmp(cmd->cmd, t)) {
245 minibuffer_insert_current_candidate();
246 exit_minibuffer();
247 minibuffer_hist_save_entry();
248 cmd->fn(current_buffer());
249 return;
253 end:
254 message("No match");
257 void
258 ir_select_gemini(void)
260 char buf[1025] = {0};
261 struct phos_uri uri;
262 struct tab *tab = current_tab;
264 exit_minibuffer();
265 minibuffer_hist_save_entry();
267 /* a bit ugly but... */
268 memcpy(&uri, &tab->uri, sizeof(tab->uri));
269 phos_uri_set_query(&uri, ministate.buf);
270 phos_serialize_uri(&uri, buf, sizeof(buf));
271 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
274 void
275 ir_select_reply(void)
277 char buf[1025] = {0};
278 struct phos_uri uri;
279 struct tab *tab = current_tab;
281 exit_minibuffer();
282 minibuffer_hist_save_entry();
284 /* a bit ugly but... */
285 strlcpy(buf, tab->last_input_url, sizeof(buf));
286 phos_parse_absolute_uri(buf, &uri);
287 phos_uri_set_query(&uri, ministate.buf);
288 phos_serialize_uri(&uri, buf, sizeof(buf));
289 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
292 void
293 ir_select_gopher(void)
295 exit_minibuffer();
296 minibuffer_hist_save_entry();
298 gopher_send_search_req(current_tab, ministate.buf);
301 void
302 lu_select(void)
304 char url[GEMINI_URL_LEN+1];
306 exit_minibuffer();
307 minibuffer_hist_save_entry();
309 humanify_url(ministate.buf, url, sizeof(url));
310 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
313 void
314 bp_select(void)
316 exit_minibuffer();
317 if (*ministate.buf != '\0')
318 add_to_bookmarks(ministate.buf);
319 else
320 message("Abort.");
323 void
324 ts_select(void)
326 struct tab *tab;
328 if ((tab = minibuffer_metadata()) == NULL) {
329 message("No tab selected");
330 return;
333 exit_minibuffer();
334 switch_to_tab(tab);
337 void
338 ls_select(void)
340 struct line *l;
342 if ((l = minibuffer_metadata()) == NULL) {
343 message("No link selected");
344 return;
347 exit_minibuffer();
348 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
351 static inline void
352 jump_to_line(struct line *l)
354 struct vline *vl;
355 struct buffer *buffer;
357 buffer = current_buffer();
359 TAILQ_FOREACH(vl, &buffer->head, vlines) {
360 if (vl->parent == l)
361 break;
364 if (vl == NULL)
365 message("Ops, %s error! Please report to %s",
366 __func__, PACKAGE_BUGREPORT);
367 else {
368 buffer->top_line = vl;
369 buffer->current_line = vl;
373 void
374 swiper_select(void)
376 struct line *l;
378 if ((l = minibuffer_metadata()) == NULL) {
379 message("No line selected");
380 return;
383 exit_minibuffer();
384 jump_to_line(l);
387 void
388 toc_select(void)
390 struct line *l;
392 if ((l = minibuffer_metadata()) == NULL) {
393 message("No line selected");
394 return;
397 exit_minibuffer();
398 jump_to_line(l);
401 static void
402 yornp_self_insert(void)
404 if (thiskey.key != 'y' && thiskey.key != 'n') {
405 message("Please answer y or n");
406 return;
409 exit_minibuffer();
410 yornp_cb(thiskey.key == 'y', yornp_data);
413 static void
414 yornp_abort(void)
416 exit_minibuffer();
417 yornp_cb(0, yornp_data);
420 static void
421 read_self_insert(void)
423 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
424 global_key_unbound();
425 return;
428 minibuffer_self_insert();
431 static void
432 read_abort(void)
434 exit_minibuffer();
435 read_cb(NULL, read_data);
438 static void
439 read_select(void)
441 exit_minibuffer();
442 minibuffer_hist_save_entry();
443 read_cb(ministate.buf, read_data);
446 /*
447 * TODO: we should collect this asynchronously...
448 */
449 static inline void
450 populate_compl_buffer(complfn *fn, void *data)
452 const char *s, *descr;
453 struct line *l;
454 struct buffer *b;
455 struct parser *p;
456 void *linedata;
458 b = &ministate.compl.buffer;
459 p = &b->page;
461 linedata = NULL;
462 descr = NULL;
463 while ((s = fn(&data, &linedata, &descr)) != NULL) {
464 if ((l = calloc(1, sizeof(*l))) == NULL)
465 abort();
467 l->type = LINE_COMPL;
468 l->data = linedata;
469 l->alt = (char*)descr;
470 if ((l->line = strdup(s)) == NULL)
471 abort();
473 TAILQ_INSERT_TAIL(&p->head, l, lines);
475 linedata = NULL;
476 descr = NULL;
479 if ((l = TAILQ_FIRST(&p->head)) != NULL)
480 l->type = LINE_COMPL_CURRENT;
483 void
484 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
485 void (*abortfn)(void), struct histhead *hist,
486 complfn *complfn, void *compldata)
488 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
489 if (in_minibuffer == MB_COMPREAD) {
490 ui_schedule_redraw();
492 ministate.compl.fn = complfn;
493 ministate.compl.data = compldata;
494 populate_compl_buffer(complfn, compldata);
497 base_map = &minibuffer_map;
498 current_map = &minibuffer_map;
500 base_map->unhandled_input = self_insert_fn;
502 ministate.donefn = donefn;
503 ministate.abortfn = abortfn;
504 memset(ministate.buf, 0, sizeof(ministate.buf));
505 ministate.buffer.current_line = &ministate.vline;
506 ministate.buffer.current_line->line = ministate.buf;
507 ministate.buffer.cpoff = 0;
508 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
510 ministate.history = hist;
511 ministate.hist_cur = NULL;
512 ministate.hist_off = 0;
515 void
516 exit_minibuffer(void)
518 if (in_minibuffer == MB_COMPREAD) {
519 erase_buffer(&ministate.compl.buffer);
520 ui_schedule_redraw();
523 in_minibuffer = 0;
524 base_map = &global_map;
525 current_map = &global_map;
528 void
529 yornp(const char *prompt, void (*fn)(int, struct tab*),
530 struct tab *data)
532 size_t len;
534 if (in_minibuffer) {
535 fn(0, data);
536 return;
539 yornp_cb = fn;
540 yornp_data = data;
541 enter_minibuffer(yornp_self_insert, yornp_self_insert,
542 yornp_abort, NULL, NULL, NULL);
544 len = sizeof(ministate.prompt);
545 strlcpy(ministate.prompt, prompt, len);
546 strlcat(ministate.prompt, " (y or n) ", len);
549 void
550 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
551 struct tab *data)
553 size_t len;
555 if (in_minibuffer)
556 return;
558 read_cb = fn;
559 read_data = data;
560 enter_minibuffer(read_self_insert, read_select, read_abort,
561 &read_history, NULL, NULL);
563 len = sizeof(ministate.prompt);
564 strlcpy(ministate.prompt, prompt, len);
565 strlcat(ministate.prompt, ": ", len);
568 static void
569 handle_clear_echoarea(int fd, short ev, void *d)
571 free(ministate.curmesg);
572 ministate.curmesg = NULL;
574 ui_after_message_hook();
577 void
578 vmessage(const char *fmt, va_list ap)
580 if (evtimer_pending(&clechoev, NULL))
581 evtimer_del(&clechoev);
583 free(ministate.curmesg);
584 ministate.curmesg = NULL;
586 if (fmt != NULL) {
587 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
588 evtimer_add(&clechoev, &clechoev_timer);
590 /* TODO: what to do if the allocation fails here? */
591 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
592 ministate.curmesg = NULL;
595 ui_after_message_hook();
598 void
599 message(const char *fmt, ...)
601 va_list ap;
603 va_start(ap, fmt);
604 vmessage(fmt, ap);
605 va_end(ap);
608 void
609 minibuffer_init(void)
611 TAILQ_INIT(&eecmd_history.head);
612 TAILQ_INIT(&ir_history.head);
613 TAILQ_INIT(&lu_history.head);
614 TAILQ_INIT(&read_history.head);
616 TAILQ_INIT(&ministate.compl.buffer.head);
617 TAILQ_INIT(&ministate.compl.buffer.page.head);
619 ministate.line.type = LINE_TEXT;
620 ministate.vline.parent = &ministate.line;
621 ministate.buffer.page.name = "*minibuffer*";
622 ministate.buffer.current_line = &ministate.vline;
624 evtimer_set(&clechoev, handle_clear_echoarea, NULL);