2 * Copyright (c) 2021, 2024 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.
36 #include "minibuffer.h"
42 #define nitems(x) (sizeof(x)/sizeof(x[0]))
44 static void *minibuffer_metadata(void);
45 static const char *minibuffer_compl_text(void);
46 static void minibuffer_hist_save_entry(void);
47 static void yornp_self_insert(void);
48 static void yornp_abort(void);
49 static void read_self_insert(void);
50 static void read_abort(void);
51 static void read_select(void);
52 static void handle_clear_echoarea(int, int, void *);
54 static unsigned long clechotimer;
55 static struct timeval clechotv = { 5, 0 };
57 static void (*yornp_cb)(int, struct tab *);
58 static struct tab *yornp_data;
60 static void (*read_cb)(const char*, struct tab *);
61 static struct tab *read_data;
63 struct hist *eecmd_history;
64 struct hist *ir_history;
65 struct hist *lu_history;
66 struct hist *read_history;
68 struct ministate ministate;
70 struct buffer minibufferwin;
75 codepoint_isgraph(uint32_t cp)
78 return isgraph((unsigned char)cp);
83 matches(char **words, size_t len, struct line *l)
88 for (i = 0; i < len; ++i) {
91 if (strcasestr(l->line, words[i]) != NULL)
94 strcasestr(l->alt, words[i]) != NULL)
105 * Recompute the visible completions. If add is 1, don't consider the
106 * ones already hidden.
109 recompute_completions(int add)
111 static char buf[GEMINI_URL_LEN];
113 char *input, **ap, *words[10];
119 if (in_minibuffer != MB_COMPREAD)
122 if (!ministate.editing)
123 text = hist_cur(ministate.hist);
125 text = ministate.buf;
127 strlcpy(buf, text, sizeof(buf));
130 /* tokenize the input */
131 for (ap = words; ap < words + nitems(words) &&
132 (*ap = strsep(&input, " ")) != NULL;) {
137 b = &ministate.compl.buffer;
138 TAILQ_FOREACH(l, &b->page.head, lines) {
139 l->type = LINE_COMPL;
140 if (add && l->flags & L_HIDDEN)
142 if (matches(words, len, l)) {
143 if (l->flags & L_HIDDEN)
145 l->flags &= ~L_HIDDEN;
147 if (!(l->flags & L_HIDDEN))
149 l->flags |= L_HIDDEN;
153 if (b->current_line == NULL)
154 b->current_line = TAILQ_FIRST(&b->head);
155 b->current_line = adjust_line(b->current_line, b);
156 vl = b->current_line;
157 if (ministate.compl.must_select && vl != NULL)
158 vl->parent->type = LINE_COMPL_CURRENT;
162 minibuffer_insert_current_candidate(void)
166 vl = ministate.compl.buffer.current_line;
167 if (vl == NULL || vl->parent->flags & L_HIDDEN)
170 minibuffer_taint_hist();
171 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
172 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
178 minibuffer_metadata(void)
182 vl = ministate.compl.buffer.current_line;
184 if (vl == NULL || vl->parent->flags & L_HIDDEN)
187 return vl->parent->data;
191 minibuffer_compl_text(void)
195 if (!ministate.editing)
196 return hist_cur(ministate.hist);
198 vl = ministate.compl.buffer.current_line;
199 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
200 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
201 return ministate.buf;
202 return vl->parent->line;
206 minibuffer_hist_save_entry(void)
208 if (ministate.hist == NULL)
211 hist_append(ministate.hist, minibuffer_compl_text());
215 * taint the minibuffer cache: if we're currently showing a history
216 * element, copy that to the current buf and reset the "history
220 minibuffer_taint_hist(void)
222 if (ministate.editing)
225 ministate.editing = 1;
226 strlcpy(ministate.buf, hist_cur(ministate.hist),
227 sizeof(ministate.buf));
228 ministate.buffer.current_line->parent->line = ministate.buf;
232 minibuffer_self_insert(void)
234 char *c, tmp[5] = {0};
237 minibuffer_taint_hist();
242 len = utf8_encode(thiskey.cp, tmp);
243 c = utf8_nth(ministate.buffer.current_line->parent->line,
244 ministate.buffer.cpoff);
245 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
248 memmove(c + len, c, strlen(c)+1);
250 ministate.buffer.cpoff++;
252 recompute_completions(1);
256 sensible_self_insert(void)
259 (!codepoint_isgraph(thiskey.key) && thiskey.key != ' ')) {
260 global_key_unbound();
264 minibuffer_self_insert();
273 t = minibuffer_compl_text();
274 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
275 if (!strcmp(cmd->cmd, t)) {
276 minibuffer_hist_save_entry();
278 cmd->fn(current_buffer());
287 ir_select_gemini(void)
289 static struct iri iri;
291 struct tab *tab = current_tab;
293 minibuffer_hist_save_entry();
295 if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
297 if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
299 if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
303 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
307 message("Failed to select URL.");
311 ir_select_reply(void)
313 static struct iri iri;
314 char buf[1025] = {0};
315 struct tab *tab = current_tab;
317 minibuffer_hist_save_entry();
319 /* a bit ugly but... */
320 iri_parse(NULL, tab->last_input_url, &iri);
321 iri_setquery(&iri, minibuffer_compl_text());
322 iri_unparse(&iri, buf, sizeof(buf));
325 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
329 ir_select_gopher(void)
331 minibuffer_hist_save_entry();
332 gopher_send_search_req(current_tab, minibuffer_compl_text());
339 char url[GEMINI_URL_LEN+1];
341 minibuffer_hist_save_entry();
342 humanify_url(minibuffer_compl_text(), hist_cur(current_tab->hist),
346 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
354 url = minibuffer_compl_text();
356 if (bookmark_page(url) == -1)
357 message("failed to bookmark page: %s",
360 message("Bookmarked");
371 if ((tab = minibuffer_metadata()) == NULL) {
372 message("No tab selected");
385 if ((l = minibuffer_metadata()) == NULL) {
386 message("No link selected");
391 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
395 jump_to_line(struct line *l)
398 struct buffer *buffer;
400 buffer = current_buffer();
402 TAILQ_FOREACH(vl, &buffer->head, vlines) {
408 message("Ops, %s error! Please report to %s",
409 __func__, PACKAGE_BUGREPORT);
411 buffer->top_line = vl;
412 buffer->current_line = vl;
421 if ((l = minibuffer_metadata()) == NULL) {
422 message("No line selected");
435 if ((l = minibuffer_metadata()) == NULL) {
436 message("No line selected");
445 save_cert_for_site_cb(int r, struct tab *tab)
447 cert_save_for(tab->client_cert, &tab->iri, r);
455 name = minibuffer_compl_text();
456 if ((current_tab->client_cert = ccert(name)) == NULL) {
457 message("Certificate %s not found", name);
463 yornp("Remember for future sessions too?", save_cert_for_site_cb,
470 static struct iri iri;
471 static char buf[1025];
473 /* a bit ugly but... */
474 if (iri_parse(NULL, default_search_engine, &iri) == -1) {
475 message("default-search-engine is a malformed IRI.");
479 iri_setquery(&iri, minibuffer_compl_text());
480 iri_unparse(&iri, buf, sizeof(buf));
483 load_url_in_tab(current_tab, buf, NULL, LU_MODE_NOCACHE);
487 yornp_self_insert(void)
489 if (thiskey.key != 'y' && thiskey.key != 'n') {
490 message("Please answer y or n");
495 yornp_cb(thiskey.key == 'y', yornp_data);
502 yornp_cb(0, yornp_data);
506 read_self_insert(void)
508 if (thiskey.meta || !codepoint_isgraph(thiskey.cp)) {
509 global_key_unbound();
513 minibuffer_self_insert();
520 read_cb(NULL, read_data);
527 minibuffer_hist_save_entry();
528 read_cb(ministate.buf, read_data);
532 * TODO: we should collect this asynchronously...
535 populate_compl_buffer(complfn *fn, void *data)
537 const char *s, *descr;
543 b = &ministate.compl.buffer;
548 while ((s = fn(&data, &linedata, &descr)) != NULL) {
549 if ((l = calloc(1, sizeof(*l))) == NULL)
552 l->type = LINE_COMPL;
554 l->alt = (char*)descr;
555 if ((l->line = strdup(s)) == NULL)
558 TAILQ_INSERT_TAIL(&p->head, l, lines);
564 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
565 ministate.compl.must_select)
566 l->type = LINE_COMPL_CURRENT;
570 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
571 void (*abortfn)(void), struct hist *hist,
572 complfn *complfn, void *compldata, int must_select)
574 ministate.compl.must_select = must_select;
575 ministate.compl.fn = complfn;
576 ministate.compl.data = compldata;
578 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
579 if (in_minibuffer == MB_COMPREAD) {
580 populate_compl_buffer(complfn, compldata);
581 ui_schedule_redraw();
584 base_map = &minibuffer_map;
585 current_map = &minibuffer_map;
587 base_map->unhandled_input = self_insert_fn;
589 ministate.donefn = donefn;
590 ministate.abortfn = abortfn;
591 memset(ministate.buf, 0, sizeof(ministate.buf));
592 ministate.buffer.current_line = &ministate.vline;
593 ministate.buffer.current_line->parent->line = ministate.buf;
594 ministate.buffer.cpoff = 0;
595 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
597 ministate.editing = 1;
598 ministate.hist = hist;
600 hist_seek_start(ministate.hist);
604 exit_minibuffer(void)
606 if (in_minibuffer == MB_COMPREAD) {
607 erase_buffer(&ministate.compl.buffer);
608 ui_schedule_redraw();
612 base_map = &global_map;
613 current_map = &global_map;
617 yornp(const char *prompt, void (*fn)(int, struct tab*),
629 enter_minibuffer(yornp_self_insert, yornp_self_insert,
630 yornp_abort, NULL, NULL, NULL, 0);
632 len = sizeof(ministate.prompt);
633 strlcpy(ministate.prompt, prompt, len);
634 strlcat(ministate.prompt, " (y or n) ", len);
638 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
648 enter_minibuffer(read_self_insert, read_select, read_abort,
649 read_history, NULL, NULL, 0);
651 len = sizeof(ministate.prompt);
652 strlcpy(ministate.prompt, prompt, len);
653 strlcat(ministate.prompt, ": ", len);
657 handle_clear_echoarea(int fd, int ev, void *d)
659 free(ministate.curmesg);
660 ministate.curmesg = NULL;
662 ui_after_message_hook();
666 vmessage(const char *fmt, va_list ap)
668 ev_timer_cancel(clechotimer);
671 free(ministate.curmesg);
672 ministate.curmesg = NULL;
675 clechotimer = ev_timer(&clechotv, handle_clear_echoarea,
678 /* TODO: what to do if the allocation fails here? */
679 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
680 ministate.curmesg = NULL;
683 ui_after_message_hook();
687 message(const char *fmt, ...)
697 minibuffer_init(void)
699 if ((eecmd_history = hist_new(HIST_WRAP)) == NULL ||
700 (ir_history = hist_new(HIST_WRAP)) == NULL ||
701 (lu_history = hist_new(HIST_WRAP)) == NULL ||
702 (read_history = hist_new(HIST_WRAP)) == NULL)
705 TAILQ_INIT(&ministate.compl.buffer.head);
706 TAILQ_INIT(&ministate.compl.buffer.page.head);
708 ministate.line.type = LINE_TEXT;
709 ministate.vline.parent = &ministate.line;
710 ministate.buffer.page.name = "*minibuffer*";
711 ministate.buffer.current_line = &ministate.vline;