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.
27 #include "minibuffer.h"
33 #define nitems(x) (sizeof(x)/sizeof(x[0]))
35 static void *minibuffer_metadata(void);
36 static const char *minibuffer_compl_text(void);
37 static void minibuffer_hist_save_entry(void);
38 static void yornp_self_insert(void);
39 static void yornp_abort(void);
40 static void read_self_insert(void);
41 static void read_abort(void);
42 static void read_select(void);
43 static void handle_clear_echoarea(int, short, void *);
45 static struct event clechoev;
46 static struct timeval clechoev_timer = { 5, 0 };
48 static void (*yornp_cb)(int, struct tab *);
49 static struct tab *yornp_data;
51 static void (*read_cb)(const char*, struct tab *);
52 static struct tab *read_data;
54 /* XXX: don't forget to init these in minibuffer_init */
55 struct histhead eecmd_history,
60 struct ministate ministate;
62 struct buffer minibufferwin;
67 matches(char **words, size_t len, struct line *l)
72 for (i = 0; i < len; ++i) {
75 if (strcasestr(l->line, words[i]) != NULL)
78 strcasestr(l->alt, words[i]) != NULL)
89 * Recompute the visible completions. If add is 1, don't consider the
90 * ones already hidden.
93 recompute_completions(int add)
95 static char buf[GEMINI_URL_LEN];
97 char *input, **ap, *words[10];
103 if (in_minibuffer != MB_COMPREAD)
106 if (ministate.hist_cur != NULL)
107 text = ministate.hist_cur->h;
109 text = ministate.buf;
111 strlcpy(buf, text, sizeof(buf));
114 /* tokenize the input */
115 for (ap = words; ap < words + nitems(words) &&
116 (*ap = strsep(&input, " ")) != NULL;) {
121 b = &ministate.compl.buffer;
122 TAILQ_FOREACH(l, &b->page.head, lines) {
123 l->type = LINE_COMPL;
124 if (add && l->flags & L_HIDDEN)
126 if (matches(words, len, l)) {
127 if (l->flags & L_HIDDEN)
129 l->flags &= ~L_HIDDEN;
131 if (!(l->flags & L_HIDDEN))
133 l->flags |= L_HIDDEN;
137 if (b->current_line == NULL)
138 b->current_line = TAILQ_FIRST(&b->head);
139 b->current_line = adjust_line(b->current_line, b);
140 vl = b->current_line;
141 if (ministate.compl.must_select && vl != NULL)
142 vl->parent->type = LINE_COMPL_CURRENT;
146 minibuffer_insert_current_candidate(void)
150 vl = ministate.compl.buffer.current_line;
151 if (vl == NULL || vl->parent->flags & L_HIDDEN)
154 minibuffer_taint_hist();
155 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
156 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
162 minibuffer_metadata(void)
166 vl = ministate.compl.buffer.current_line;
168 if (vl == NULL || vl->parent->flags & L_HIDDEN)
171 return vl->parent->data;
175 minibuffer_compl_text(void)
179 if (ministate.hist_cur != NULL)
180 return ministate.hist_cur->h;
182 vl = ministate.compl.buffer.current_line;
183 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
184 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
185 return ministate.buf;
186 return vl->parent->line;
190 minibuffer_hist_save_entry(void)
195 if (ministate.history == NULL)
198 if ((hist = calloc(1, sizeof(*hist))) == NULL)
201 t = minibuffer_compl_text();
202 strlcpy(hist->h, t, sizeof(hist->h));
204 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
205 ministate.history->len++;
209 * taint the minibuffer cache: if we're currently showing a history
210 * element, copy that to the current buf and reset the "history
214 minibuffer_taint_hist(void)
216 if (ministate.hist_cur == NULL)
219 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
220 ministate.hist_cur = NULL;
221 ministate.buffer.current_line->parent->line = ministate.buf;
225 minibuffer_self_insert(void)
227 char *c, tmp[5] = {0};
230 minibuffer_taint_hist();
235 len = utf8_encode(thiskey.cp, tmp);
236 c = utf8_nth(ministate.buffer.current_line->parent->line,
237 ministate.buffer.cpoff);
238 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
241 memmove(c + len, c, strlen(c)+1);
243 ministate.buffer.cpoff++;
245 recompute_completions(1);
249 sensible_self_insert(void)
252 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
253 global_key_unbound();
257 minibuffer_self_insert();
266 t = minibuffer_compl_text();
267 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
268 if (!strcmp(cmd->cmd, t)) {
269 minibuffer_hist_save_entry();
271 cmd->fn(current_buffer());
280 ir_select_gemini(void)
282 static struct iri iri;
284 struct tab *tab = current_tab;
286 minibuffer_hist_save_entry();
288 if (iri_parse(NULL, tab->hist_cur->h, &iri) == -1)
290 if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
292 if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
296 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
300 message("Failed to select URL.");
304 ir_select_reply(void)
306 char buf[1025] = {0};
308 struct tab *tab = current_tab;
310 minibuffer_hist_save_entry();
312 /* a bit ugly but... */
313 strlcpy(buf, tab->last_input_url, sizeof(buf));
314 phos_parse_absolute_uri(buf, &uri);
315 phos_uri_set_query(&uri, minibuffer_compl_text());
316 phos_serialize_uri(&uri, buf, sizeof(buf));
319 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
323 ir_select_gopher(void)
325 minibuffer_hist_save_entry();
326 gopher_send_search_req(current_tab, minibuffer_compl_text());
333 char url[GEMINI_URL_LEN+1];
335 minibuffer_hist_save_entry();
336 humanify_url(minibuffer_compl_text(), url, sizeof(url));
338 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
345 if (*ministate.buf != '\0') {
346 if (!bookmark_page(ministate.buf))
347 message("failed to bookmark page: %s",
358 if ((tab = minibuffer_metadata()) == NULL) {
359 message("No tab selected");
372 if ((l = minibuffer_metadata()) == NULL) {
373 message("No link selected");
378 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
382 jump_to_line(struct line *l)
385 struct buffer *buffer;
387 buffer = current_buffer();
389 TAILQ_FOREACH(vl, &buffer->head, vlines) {
395 message("Ops, %s error! Please report to %s",
396 __func__, PACKAGE_BUGREPORT);
398 buffer->top_line = vl;
399 buffer->current_line = vl;
408 if ((l = minibuffer_metadata()) == NULL) {
409 message("No line selected");
422 if ((l = minibuffer_metadata()) == NULL) {
423 message("No line selected");
432 yornp_self_insert(void)
434 if (thiskey.key != 'y' && thiskey.key != 'n') {
435 message("Please answer y or n");
440 yornp_cb(thiskey.key == 'y', yornp_data);
447 yornp_cb(0, yornp_data);
451 read_self_insert(void)
453 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
454 global_key_unbound();
458 minibuffer_self_insert();
465 read_cb(NULL, read_data);
472 minibuffer_hist_save_entry();
473 read_cb(ministate.buf, read_data);
477 * TODO: we should collect this asynchronously...
480 populate_compl_buffer(complfn *fn, void *data)
482 const char *s, *descr;
488 b = &ministate.compl.buffer;
493 while ((s = fn(&data, &linedata, &descr)) != NULL) {
494 if ((l = calloc(1, sizeof(*l))) == NULL)
497 l->type = LINE_COMPL;
499 l->alt = (char*)descr;
500 if ((l->line = strdup(s)) == NULL)
503 TAILQ_INSERT_TAIL(&p->head, l, lines);
509 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
510 ministate.compl.must_select)
511 l->type = LINE_COMPL_CURRENT;
515 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
516 void (*abortfn)(void), struct histhead *hist,
517 complfn *complfn, void *compldata, int must_select)
519 ministate.compl.must_select = must_select;
520 ministate.compl.fn = complfn;
521 ministate.compl.data = compldata;
523 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
524 if (in_minibuffer == MB_COMPREAD) {
525 populate_compl_buffer(complfn, compldata);
526 ui_schedule_redraw();
529 base_map = &minibuffer_map;
530 current_map = &minibuffer_map;
532 base_map->unhandled_input = self_insert_fn;
534 ministate.donefn = donefn;
535 ministate.abortfn = abortfn;
536 memset(ministate.buf, 0, sizeof(ministate.buf));
537 ministate.buffer.current_line = &ministate.vline;
538 ministate.buffer.current_line->parent->line = ministate.buf;
539 ministate.buffer.cpoff = 0;
540 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
542 ministate.history = hist;
543 ministate.hist_cur = NULL;
544 ministate.hist_off = 0;
548 exit_minibuffer(void)
550 if (in_minibuffer == MB_COMPREAD) {
551 erase_buffer(&ministate.compl.buffer);
552 ui_schedule_redraw();
556 base_map = &global_map;
557 current_map = &global_map;
561 yornp(const char *prompt, void (*fn)(int, struct tab*),
573 enter_minibuffer(yornp_self_insert, yornp_self_insert,
574 yornp_abort, NULL, NULL, NULL, 0);
576 len = sizeof(ministate.prompt);
577 strlcpy(ministate.prompt, prompt, len);
578 strlcat(ministate.prompt, " (y or n) ", len);
582 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
592 enter_minibuffer(read_self_insert, read_select, read_abort,
593 &read_history, NULL, NULL, 0);
595 len = sizeof(ministate.prompt);
596 strlcpy(ministate.prompt, prompt, len);
597 strlcat(ministate.prompt, ": ", len);
601 handle_clear_echoarea(int fd, short ev, void *d)
603 free(ministate.curmesg);
604 ministate.curmesg = NULL;
606 ui_after_message_hook();
610 vmessage(const char *fmt, va_list ap)
612 if (evtimer_pending(&clechoev, NULL))
613 evtimer_del(&clechoev);
615 free(ministate.curmesg);
616 ministate.curmesg = NULL;
619 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
620 evtimer_add(&clechoev, &clechoev_timer);
622 /* TODO: what to do if the allocation fails here? */
623 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
624 ministate.curmesg = NULL;
627 ui_after_message_hook();
631 message(const char *fmt, ...)
641 minibuffer_init(void)
643 TAILQ_INIT(&eecmd_history.head);
644 TAILQ_INIT(&ir_history.head);
645 TAILQ_INIT(&lu_history.head);
646 TAILQ_INIT(&read_history.head);
648 TAILQ_INIT(&ministate.compl.buffer.head);
649 TAILQ_INIT(&ministate.compl.buffer.page.head);
651 ministate.line.type = LINE_TEXT;
652 ministate.vline.parent = &ministate.line;
653 ministate.buffer.page.name = "*minibuffer*";
654 ministate.buffer.current_line = &ministate.vline;
656 evtimer_set(&clechoev, handle_clear_echoarea, NULL);