2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
26 #include "minibuffer.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,
59 struct ministate ministate;
61 struct buffer minibufferwin;
66 matches(char **words, size_t len, struct line *l)
71 for (i = 0; i < len; ++i) {
74 if (strcasestr(l->line, words[i]) != NULL)
77 strcasestr(l->alt, words[i]) != NULL)
88 * Recompute the visible completions. If add is 1, don't consider the
89 * ones already hidden.
92 recompute_completions(int add)
94 static char buf[GEMINI_URL_LEN];
96 char *input, **ap, *words[10];
102 if (in_minibuffer != MB_COMPREAD)
105 if (ministate.hist_cur != NULL)
106 text = ministate.hist_cur->h;
108 text = ministate.buf;
110 strlcpy(buf, text, sizeof(buf));
113 /* tokenize the input */
114 for (ap = words; ap < words + nitems(words) &&
115 (*ap = strsep(&input, " ")) != NULL;) {
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)
125 if (matches(words, len, l)) {
126 if (l->flags & L_HIDDEN)
128 l->flags &= ~L_HIDDEN;
130 if (!(l->flags & L_HIDDEN))
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;
145 minibuffer_insert_current_candidate(void)
149 vl = ministate.compl.buffer.current_line;
150 if (vl == NULL || vl->parent->flags & L_HIDDEN)
153 minibuffer_taint_hist();
154 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
155 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
161 minibuffer_metadata(void)
165 vl = ministate.compl.buffer.current_line;
167 if (vl == NULL || vl->parent->flags & L_HIDDEN)
170 return vl->parent->data;
174 minibuffer_compl_text(void)
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;
189 minibuffer_hist_save_entry(void)
194 if (ministate.history == NULL)
197 if ((hist = calloc(1, sizeof(*hist))) == NULL)
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++;
208 * taint the minibuffer cache: if we're currently showing a history
209 * element, copy that to the current buf and reset the "history
213 minibuffer_taint_hist(void)
215 if (ministate.hist_cur == NULL)
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;
224 minibuffer_self_insert(void)
226 char *c, tmp[5] = {0};
229 minibuffer_taint_hist();
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)
240 memmove(c + len, c, strlen(c)+1);
242 ministate.buffer.cpoff++;
244 recompute_completions(1);
248 sensible_self_insert(void)
251 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
252 global_key_unbound();
256 minibuffer_self_insert();
265 t = minibuffer_compl_text();
266 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
267 if (!strcmp(cmd->cmd, t)) {
268 minibuffer_hist_save_entry();
270 cmd->fn(current_buffer());
279 ir_select_gemini(void)
281 char buf[1025] = {0};
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));
293 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
297 ir_select_reply(void)
299 char buf[1025] = {0};
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));
312 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
316 ir_select_gopher(void)
318 minibuffer_hist_save_entry();
319 gopher_send_search_req(current_tab, minibuffer_compl_text());
326 char url[GEMINI_URL_LEN+1];
328 minibuffer_hist_save_entry();
329 humanify_url(minibuffer_compl_text(), url, sizeof(url));
331 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
338 if (*ministate.buf != '\0') {
339 if (!bookmark_page(ministate.buf))
340 message("failed to bookmark page: %s",
351 if ((tab = minibuffer_metadata()) == NULL) {
352 message("No tab selected");
365 if ((l = minibuffer_metadata()) == NULL) {
366 message("No link selected");
371 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
375 jump_to_line(struct line *l)
378 struct buffer *buffer;
380 buffer = current_buffer();
382 TAILQ_FOREACH(vl, &buffer->head, vlines) {
388 message("Ops, %s error! Please report to %s",
389 __func__, PACKAGE_BUGREPORT);
391 buffer->top_line = vl;
392 buffer->current_line = vl;
401 if ((l = minibuffer_metadata()) == NULL) {
402 message("No line selected");
415 if ((l = minibuffer_metadata()) == NULL) {
416 message("No line selected");
425 yornp_self_insert(void)
427 if (thiskey.key != 'y' && thiskey.key != 'n') {
428 message("Please answer y or n");
433 yornp_cb(thiskey.key == 'y', yornp_data);
440 yornp_cb(0, yornp_data);
444 read_self_insert(void)
446 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
447 global_key_unbound();
451 minibuffer_self_insert();
458 read_cb(NULL, read_data);
465 minibuffer_hist_save_entry();
466 read_cb(ministate.buf, read_data);
470 * TODO: we should collect this asynchronously...
473 populate_compl_buffer(complfn *fn, void *data)
475 const char *s, *descr;
481 b = &ministate.compl.buffer;
486 while ((s = fn(&data, &linedata, &descr)) != NULL) {
487 if ((l = calloc(1, sizeof(*l))) == NULL)
490 l->type = LINE_COMPL;
492 l->alt = (char*)descr;
493 if ((l->line = strdup(s)) == NULL)
496 TAILQ_INSERT_TAIL(&p->head, l, lines);
502 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
503 ministate.compl.must_select)
504 l->type = LINE_COMPL_CURRENT;
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;
541 exit_minibuffer(void)
543 if (in_minibuffer == MB_COMPREAD) {
544 erase_buffer(&ministate.compl.buffer);
545 ui_schedule_redraw();
549 base_map = &global_map;
550 current_map = &global_map;
554 yornp(const char *prompt, void (*fn)(int, struct tab*),
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);
575 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
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);
594 handle_clear_echoarea(int fd, short ev, void *d)
596 free(ministate.curmesg);
597 ministate.curmesg = NULL;
599 ui_after_message_hook();
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;
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();
624 message(const char *fmt, ...)
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);