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 buffer *b;
96 if (in_minibuffer != MB_COMPREAD)
97 return;
99 strlcpy(buf, ministate.buf, sizeof(buf));
100 input = buf;
102 /* tokenize the input */
103 for (ap = words; ap < words + nitems(words) &&
104 (*ap = strsep(&input, " ")) != NULL;) {
105 if (**ap != '\0')
106 ap++, len++;
109 b = &ministate.compl.buffer;
110 TAILQ_FOREACH(l, &b->page.head, lines) {
111 l->type = LINE_COMPL;
112 if (add && l->flags & L_HIDDEN)
113 continue;
114 if (matches(words, len, l)) {
115 if (l->flags & L_HIDDEN)
116 b->line_max++;
117 l->flags &= ~L_HIDDEN;
118 } else {
119 if (!(l->flags & L_HIDDEN))
120 b->line_max--;
121 l->flags |= L_HIDDEN;
125 if (b->current_line == NULL)
126 b->current_line = TAILQ_FIRST(&b->head);
127 b->current_line = adjust_line(b->current_line, b);
130 int
131 minibuffer_insert_current_candidate(void)
133 struct vline *vl;
135 vl = ministate.compl.buffer.current_line;
136 if (vl == NULL || vl->parent->flags & L_HIDDEN)
137 return -1;
139 minibuffer_taint_hist();
140 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
141 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
143 return 0;
146 static void *
147 minibuffer_metadata(void)
149 struct vline *vl;
151 vl = ministate.compl.buffer.current_line;
153 if (vl == NULL || vl->parent->flags & L_HIDDEN)
154 return NULL;
156 return vl->parent->data;
159 static void
160 minibuffer_hist_save_entry(void)
162 struct hist *hist;
164 if (ministate.history == NULL)
165 return;
167 if ((hist = calloc(1, sizeof(*hist))) == NULL)
168 abort();
170 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
172 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
173 ministate.history->len++;
176 /*
177 * taint the minibuffer cache: if we're currently showing a history
178 * element, copy that to the current buf and reset the "history
179 * navigation" thing.
180 */
181 void
182 minibuffer_taint_hist(void)
184 if (ministate.hist_cur == NULL)
185 return;
187 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
188 ministate.hist_cur = NULL;
189 ministate.buffer.current_line->line = ministate.buf;
192 void
193 minibuffer_self_insert(void)
195 char *c, tmp[5] = {0};
196 size_t len;
198 minibuffer_taint_hist();
200 if (thiskey.cp == 0)
201 return;
203 len = utf8_encode(thiskey.cp, tmp);
204 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
205 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
206 return;
208 memmove(c + len, c, strlen(c)+1);
209 memcpy(c, tmp, len);
210 ministate.buffer.cpoff++;
212 recompute_completions(1);
215 void
216 sensible_self_insert(void)
218 if (thiskey.meta ||
219 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
220 global_key_unbound();
221 return;
224 minibuffer_self_insert();
227 void
228 eecmd_select(void)
230 struct cmd *cmd;
231 struct vline *vl;
232 const char *t;
234 vl = ministate.compl.buffer.current_line;
235 if (vl == NULL || vl->parent->flags & L_HIDDEN)
236 goto end;
238 t = vl->parent->line;
239 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
240 if (!strcmp(cmd->cmd, t)) {
241 minibuffer_insert_current_candidate();
242 exit_minibuffer();
243 minibuffer_hist_save_entry();
244 cmd->fn(current_buffer());
245 return;
249 end:
250 message("No match");
253 void
254 ir_select_gemini(void)
256 char buf[1025] = {0};
257 struct phos_uri uri;
258 struct tab *tab = current_tab;
260 exit_minibuffer();
261 minibuffer_hist_save_entry();
263 /* a bit ugly but... */
264 memcpy(&uri, &tab->uri, sizeof(tab->uri));
265 phos_uri_set_query(&uri, ministate.buf);
266 phos_serialize_uri(&uri, buf, sizeof(buf));
267 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
270 void
271 ir_select_reply(void)
273 char buf[1025] = {0};
274 struct phos_uri uri;
275 struct tab *tab = current_tab;
277 exit_minibuffer();
278 minibuffer_hist_save_entry();
280 /* a bit ugly but... */
281 strlcpy(buf, tab->last_input_url, sizeof(buf));
282 phos_parse_absolute_uri(buf, &uri);
283 phos_uri_set_query(&uri, ministate.buf);
284 phos_serialize_uri(&uri, buf, sizeof(buf));
285 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
288 void
289 ir_select_gopher(void)
291 exit_minibuffer();
292 minibuffer_hist_save_entry();
294 gopher_send_search_req(current_tab, ministate.buf);
297 void
298 lu_select(void)
300 char url[GEMINI_URL_LEN+1];
302 exit_minibuffer();
303 minibuffer_hist_save_entry();
305 humanify_url(ministate.buf, url, sizeof(url));
306 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
309 void
310 bp_select(void)
312 exit_minibuffer();
313 if (*ministate.buf != '\0')
314 add_to_bookmarks(ministate.buf);
315 else
316 message("Abort.");
319 void
320 ts_select(void)
322 struct tab *tab;
324 if ((tab = minibuffer_metadata()) == NULL) {
325 message("No tab selected");
326 return;
329 exit_minibuffer();
330 switch_to_tab(tab);
333 void
334 ls_select(void)
336 struct line *l;
338 if ((l = minibuffer_metadata()) == NULL) {
339 message("No link selected");
340 return;
343 exit_minibuffer();
344 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
347 static inline void
348 jump_to_line(struct line *l)
350 struct vline *vl;
351 struct buffer *buffer;
353 buffer = current_buffer();
355 TAILQ_FOREACH(vl, &buffer->head, vlines) {
356 if (vl->parent == l)
357 break;
360 if (vl == NULL)
361 message("Ops, %s error! Please report to %s",
362 __func__, PACKAGE_BUGREPORT);
363 else {
364 buffer->top_line = vl;
365 buffer->current_line = vl;
369 void
370 swiper_select(void)
372 struct line *l;
374 if ((l = minibuffer_metadata()) == NULL) {
375 message("No line selected");
376 return;
379 exit_minibuffer();
380 jump_to_line(l);
383 void
384 toc_select(void)
386 struct line *l;
388 if ((l = minibuffer_metadata()) == NULL) {
389 message("No line selected");
390 return;
393 exit_minibuffer();
394 jump_to_line(l);
397 static void
398 yornp_self_insert(void)
400 if (thiskey.key != 'y' && thiskey.key != 'n') {
401 message("Please answer y or n");
402 return;
405 exit_minibuffer();
406 yornp_cb(thiskey.key == 'y', yornp_data);
409 static void
410 yornp_abort(void)
412 exit_minibuffer();
413 yornp_cb(0, yornp_data);
416 static void
417 read_self_insert(void)
419 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
420 global_key_unbound();
421 return;
424 minibuffer_self_insert();
427 static void
428 read_abort(void)
430 exit_minibuffer();
431 read_cb(NULL, read_data);
434 static void
435 read_select(void)
437 exit_minibuffer();
438 minibuffer_hist_save_entry();
439 read_cb(ministate.buf, read_data);
442 /*
443 * TODO: we should collect this asynchronously...
444 */
445 static inline void
446 populate_compl_buffer(complfn *fn, void *data)
448 const char *s, *descr;
449 struct line *l;
450 struct buffer *b;
451 struct parser *p;
452 void *linedata;
454 b = &ministate.compl.buffer;
455 p = &b->page;
457 linedata = NULL;
458 descr = NULL;
459 while ((s = fn(&data, &linedata, &descr)) != NULL) {
460 if ((l = calloc(1, sizeof(*l))) == NULL)
461 abort();
463 l->type = LINE_COMPL;
464 l->data = linedata;
465 l->alt = (char*)descr;
466 if ((l->line = strdup(s)) == NULL)
467 abort();
469 TAILQ_INSERT_TAIL(&p->head, l, lines);
471 linedata = NULL;
472 descr = NULL;
476 void
477 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
478 void (*abortfn)(void), struct histhead *hist,
479 complfn *complfn, void *compldata)
481 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
482 if (in_minibuffer == MB_COMPREAD) {
483 ui_schedule_redraw();
485 ministate.compl.fn = complfn;
486 ministate.compl.data = compldata;
487 populate_compl_buffer(complfn, compldata);
490 base_map = &minibuffer_map;
491 current_map = &minibuffer_map;
493 base_map->unhandled_input = self_insert_fn;
495 ministate.donefn = donefn;
496 ministate.abortfn = abortfn;
497 memset(ministate.buf, 0, sizeof(ministate.buf));
498 ministate.buffer.current_line = &ministate.vline;
499 ministate.buffer.current_line->line = ministate.buf;
500 ministate.buffer.cpoff = 0;
501 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
503 ministate.history = hist;
504 ministate.hist_cur = NULL;
505 ministate.hist_off = 0;
508 void
509 exit_minibuffer(void)
511 if (in_minibuffer == MB_COMPREAD) {
512 erase_buffer(&ministate.compl.buffer);
513 ui_schedule_redraw();
516 in_minibuffer = 0;
517 base_map = &global_map;
518 current_map = &global_map;
521 void
522 yornp(const char *prompt, void (*fn)(int, struct tab*),
523 struct tab *data)
525 size_t len;
527 if (in_minibuffer) {
528 fn(0, data);
529 return;
532 yornp_cb = fn;
533 yornp_data = data;
534 enter_minibuffer(yornp_self_insert, yornp_self_insert,
535 yornp_abort, NULL, NULL, NULL);
537 len = sizeof(ministate.prompt);
538 strlcpy(ministate.prompt, prompt, len);
539 strlcat(ministate.prompt, " (y or n) ", len);
542 void
543 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
544 struct tab *data)
546 size_t len;
548 if (in_minibuffer)
549 return;
551 read_cb = fn;
552 read_data = data;
553 enter_minibuffer(read_self_insert, read_select, read_abort,
554 &read_history, NULL, NULL);
556 len = sizeof(ministate.prompt);
557 strlcpy(ministate.prompt, prompt, len);
558 strlcat(ministate.prompt, ": ", len);
561 static void
562 handle_clear_echoarea(int fd, short ev, void *d)
564 free(ministate.curmesg);
565 ministate.curmesg = NULL;
567 ui_after_message_hook();
570 void
571 vmessage(const char *fmt, va_list ap)
573 if (evtimer_pending(&clechoev, NULL))
574 evtimer_del(&clechoev);
576 free(ministate.curmesg);
577 ministate.curmesg = NULL;
579 if (fmt != NULL) {
580 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
581 evtimer_add(&clechoev, &clechoev_timer);
583 /* TODO: what to do if the allocation fails here? */
584 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
585 ministate.curmesg = NULL;
588 ui_after_message_hook();
591 void
592 message(const char *fmt, ...)
594 va_list ap;
596 va_start(ap, fmt);
597 vmessage(fmt, ap);
598 va_end(ap);
601 void
602 minibuffer_init(void)
604 TAILQ_INIT(&eecmd_history.head);
605 TAILQ_INIT(&ir_history.head);
606 TAILQ_INIT(&lu_history.head);
607 TAILQ_INIT(&read_history.head);
609 TAILQ_INIT(&ministate.compl.buffer.head);
610 TAILQ_INIT(&ministate.compl.buffer.page.head);
612 ministate.line.type = LINE_TEXT;
613 ministate.vline.parent = &ministate.line;
614 ministate.buffer.page.name = "*minibuffer*";
615 ministate.buffer.current_line = &ministate.vline;
617 evtimer_set(&clechoev, handle_clear_echoarea, NULL);