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.
23 #include "minibuffer.h"
29 #define nitems(x) (sizeof(x)/sizeof(x[0]))
31 static void *minibuffer_metadata(void);
32 static const char *minibuffer_compl_text(void);
33 static void minibuffer_hist_save_entry(void);
34 static void yornp_self_insert(void);
35 static void yornp_abort(void);
36 static void read_self_insert(void);
37 static void read_abort(void);
38 static void read_select(void);
39 static void handle_clear_echoarea(int, short, void *);
41 static struct event clechoev;
42 static struct timeval clechoev_timer = { 5, 0 };
44 static void (*yornp_cb)(int, struct tab *);
45 static struct tab *yornp_data;
47 static void (*read_cb)(const char*, struct tab *);
48 static struct tab *read_data;
50 /* XXX: don't forget to init these in minibuffer_init */
51 struct histhead eecmd_history,
56 struct ministate ministate;
58 struct buffer minibufferwin;
63 matches(char **words, size_t len, struct line *l)
68 for (i = 0; i < len; ++i) {
71 if (strcasestr(l->line, words[i]) != NULL)
74 strcasestr(l->alt, words[i]) != NULL)
85 * Recompute the visible completions. If add is 1, don't consider the
86 * ones already hidden.
89 recompute_completions(int add)
91 static char buf[GEMINI_URL_LEN];
92 char *input, **ap, *words[10];
98 if (in_minibuffer != MB_COMPREAD)
101 strlcpy(buf, ministate.buf, sizeof(buf));
104 /* tokenize the input */
105 for (ap = words; ap < words + nitems(words) &&
106 (*ap = strsep(&input, " ")) != NULL;) {
111 b = &ministate.compl.buffer;
112 TAILQ_FOREACH(l, &b->page.head, lines) {
113 l->type = LINE_COMPL;
114 if (add && l->flags & L_HIDDEN)
116 if (matches(words, len, l)) {
117 if (l->flags & L_HIDDEN)
119 l->flags &= ~L_HIDDEN;
121 if (!(l->flags & L_HIDDEN))
123 l->flags |= L_HIDDEN;
127 if (b->current_line == NULL)
128 b->current_line = TAILQ_FIRST(&b->head);
129 b->current_line = adjust_line(b->current_line, b);
130 vl = b->current_line;
131 if (ministate.compl.must_select && vl != NULL)
132 vl->parent->type = LINE_COMPL_CURRENT;
136 minibuffer_insert_current_candidate(void)
140 vl = ministate.compl.buffer.current_line;
141 if (vl == NULL || vl->parent->flags & L_HIDDEN)
144 minibuffer_taint_hist();
145 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
146 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
152 minibuffer_metadata(void)
156 vl = ministate.compl.buffer.current_line;
158 if (vl == NULL || vl->parent->flags & L_HIDDEN)
161 return vl->parent->data;
165 minibuffer_compl_text(void)
169 vl = ministate.compl.buffer.current_line;
170 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
171 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
172 return ministate.buf;
173 return vl->parent->line;
177 minibuffer_hist_save_entry(void)
182 if (ministate.history == NULL)
185 if ((hist = calloc(1, sizeof(*hist))) == NULL)
188 t = minibuffer_compl_text();
189 strlcpy(hist->h, t, sizeof(hist->h));
191 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
192 ministate.history->len++;
196 * taint the minibuffer cache: if we're currently showing a history
197 * element, copy that to the current buf and reset the "history
201 minibuffer_taint_hist(void)
203 if (ministate.hist_cur == NULL)
206 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
207 ministate.hist_cur = NULL;
208 ministate.buffer.current_line->line = ministate.buf;
212 minibuffer_self_insert(void)
214 char *c, tmp[5] = {0};
217 minibuffer_taint_hist();
222 len = utf8_encode(thiskey.cp, tmp);
223 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
224 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
227 memmove(c + len, c, strlen(c)+1);
229 ministate.buffer.cpoff++;
231 recompute_completions(1);
235 sensible_self_insert(void)
238 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
239 global_key_unbound();
243 minibuffer_self_insert();
252 t = minibuffer_compl_text();
253 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
254 if (!strcmp(cmd->cmd, t)) {
255 minibuffer_insert_current_candidate();
256 minibuffer_hist_save_entry();
258 cmd->fn(current_buffer());
267 ir_select_gemini(void)
269 char buf[1025] = {0};
271 struct tab *tab = current_tab;
273 minibuffer_hist_save_entry();
275 /* a bit ugly but... */
276 memcpy(&uri, &tab->uri, sizeof(tab->uri));
277 phos_uri_set_query(&uri, minibuffer_compl_text());
278 phos_serialize_uri(&uri, buf, sizeof(buf));
281 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
285 ir_select_reply(void)
287 char buf[1025] = {0};
289 struct tab *tab = current_tab;
291 minibuffer_hist_save_entry();
293 /* a bit ugly but... */
294 strlcpy(buf, tab->last_input_url, sizeof(buf));
295 phos_parse_absolute_uri(buf, &uri);
296 phos_uri_set_query(&uri, minibuffer_compl_text());
297 phos_serialize_uri(&uri, buf, sizeof(buf));
300 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
304 ir_select_gopher(void)
306 minibuffer_hist_save_entry();
307 gopher_send_search_req(current_tab, minibuffer_compl_text());
314 char url[GEMINI_URL_LEN+1];
316 minibuffer_hist_save_entry();
317 humanify_url(minibuffer_compl_text(), url, sizeof(url));
319 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
326 if (*ministate.buf != '\0')
327 add_to_bookmarks(ministate.buf);
337 if ((tab = minibuffer_metadata()) == NULL) {
338 message("No tab selected");
351 if ((l = minibuffer_metadata()) == NULL) {
352 message("No link selected");
357 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
361 jump_to_line(struct line *l)
364 struct buffer *buffer;
366 buffer = current_buffer();
368 TAILQ_FOREACH(vl, &buffer->head, vlines) {
374 message("Ops, %s error! Please report to %s",
375 __func__, PACKAGE_BUGREPORT);
377 buffer->top_line = vl;
378 buffer->current_line = vl;
387 if ((l = minibuffer_metadata()) == NULL) {
388 message("No line selected");
401 if ((l = minibuffer_metadata()) == NULL) {
402 message("No line selected");
411 yornp_self_insert(void)
413 if (thiskey.key != 'y' && thiskey.key != 'n') {
414 message("Please answer y or n");
419 yornp_cb(thiskey.key == 'y', yornp_data);
426 yornp_cb(0, yornp_data);
430 read_self_insert(void)
432 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
433 global_key_unbound();
437 minibuffer_self_insert();
444 read_cb(NULL, read_data);
451 minibuffer_hist_save_entry();
452 read_cb(ministate.buf, read_data);
456 * TODO: we should collect this asynchronously...
459 populate_compl_buffer(complfn *fn, void *data)
461 const char *s, *descr;
467 b = &ministate.compl.buffer;
472 while ((s = fn(&data, &linedata, &descr)) != NULL) {
473 if ((l = calloc(1, sizeof(*l))) == NULL)
476 l->type = LINE_COMPL;
478 l->alt = (char*)descr;
479 if ((l->line = strdup(s)) == NULL)
482 TAILQ_INSERT_TAIL(&p->head, l, lines);
488 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
489 ministate.compl.must_select)
490 l->type = LINE_COMPL_CURRENT;
494 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
495 void (*abortfn)(void), struct histhead *hist,
496 complfn *complfn, void *compldata, int must_select)
498 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
499 if (in_minibuffer == MB_COMPREAD) {
500 ui_schedule_redraw();
502 ministate.compl.fn = complfn;
503 ministate.compl.data = compldata;
504 ministate.compl.must_select = must_select;
505 populate_compl_buffer(complfn, compldata);
508 base_map = &minibuffer_map;
509 current_map = &minibuffer_map;
511 base_map->unhandled_input = self_insert_fn;
513 ministate.donefn = donefn;
514 ministate.abortfn = abortfn;
515 memset(ministate.buf, 0, sizeof(ministate.buf));
516 ministate.buffer.current_line = &ministate.vline;
517 ministate.buffer.current_line->line = ministate.buf;
518 ministate.buffer.cpoff = 0;
519 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
521 ministate.history = hist;
522 ministate.hist_cur = NULL;
523 ministate.hist_off = 0;
527 exit_minibuffer(void)
529 if (in_minibuffer == MB_COMPREAD) {
530 erase_buffer(&ministate.compl.buffer);
531 ui_schedule_redraw();
535 base_map = &global_map;
536 current_map = &global_map;
540 yornp(const char *prompt, void (*fn)(int, struct tab*),
552 enter_minibuffer(yornp_self_insert, yornp_self_insert,
553 yornp_abort, NULL, NULL, NULL, 0);
555 len = sizeof(ministate.prompt);
556 strlcpy(ministate.prompt, prompt, len);
557 strlcat(ministate.prompt, " (y or n) ", len);
561 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
571 enter_minibuffer(read_self_insert, read_select, read_abort,
572 &read_history, NULL, NULL, 0);
574 len = sizeof(ministate.prompt);
575 strlcpy(ministate.prompt, prompt, len);
576 strlcat(ministate.prompt, ": ", len);
580 handle_clear_echoarea(int fd, short ev, void *d)
582 free(ministate.curmesg);
583 ministate.curmesg = NULL;
585 ui_after_message_hook();
589 vmessage(const char *fmt, va_list ap)
591 if (evtimer_pending(&clechoev, NULL))
592 evtimer_del(&clechoev);
594 free(ministate.curmesg);
595 ministate.curmesg = NULL;
598 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
599 evtimer_add(&clechoev, &clechoev_timer);
601 /* TODO: what to do if the allocation fails here? */
602 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
603 ministate.curmesg = NULL;
606 ui_after_message_hook();
610 message(const char *fmt, ...)
620 minibuffer_init(void)
622 TAILQ_INIT(&eecmd_history.head);
623 TAILQ_INIT(&ir_history.head);
624 TAILQ_INIT(&lu_history.head);
625 TAILQ_INIT(&read_history.head);
627 TAILQ_INIT(&ministate.compl.buffer.head);
628 TAILQ_INIT(&ministate.compl.buffer.page.head);
630 ministate.line.type = LINE_TEXT;
631 ministate.vline.parent = &ministate.line;
632 ministate.buffer.page.name = "*minibuffer*";
633 ministate.buffer.current_line = &ministate.vline;
635 evtimer_set(&clechoev, handle_clear_echoarea, NULL);