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 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,
55 struct ministate ministate;
57 struct buffer minibufferwin;
62 matches(char **words, size_t len, struct line *l)
67 for (i = 0; i < len; ++i) {
70 if (strcasestr(l->line, words[i]) != NULL)
73 strcasestr(l->alt, words[i]) != NULL)
84 * Recompute the visible completions. If add is 1, don't consider the
85 * ones already hidden.
88 recompute_completions(int add)
90 static char buf[GEMINI_URL_LEN];
91 char *input, **ap, *words[10];
96 if (in_minibuffer != MB_COMPREAD)
99 strlcpy(buf, ministate.buf, sizeof(buf));
102 /* tokenize the input */
103 for (ap = words; ap < words + nitems(words) &&
104 (*ap = strsep(&input, " ")) != NULL;) {
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)
114 if (matches(words, len, l)) {
115 if (l->flags & L_HIDDEN)
117 l->flags &= ~L_HIDDEN;
119 if (!(l->flags & L_HIDDEN))
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);
131 minibuffer_insert_current_candidate(void)
135 vl = ministate.compl.buffer.current_line;
136 if (vl == NULL || vl->parent->flags & L_HIDDEN)
139 minibuffer_taint_hist();
140 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
141 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
147 minibuffer_metadata(void)
151 vl = ministate.compl.buffer.current_line;
153 if (vl == NULL || vl->parent->flags & L_HIDDEN)
156 return vl->parent->data;
160 minibuffer_hist_save_entry(void)
164 if (ministate.history == NULL)
167 if ((hist = calloc(1, sizeof(*hist))) == NULL)
170 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
172 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
173 ministate.history->len++;
177 * taint the minibuffer cache: if we're currently showing a history
178 * element, copy that to the current buf and reset the "history
182 minibuffer_taint_hist(void)
184 if (ministate.hist_cur == NULL)
187 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
188 ministate.hist_cur = NULL;
189 ministate.buffer.current_line->line = ministate.buf;
193 minibuffer_self_insert(void)
195 char *c, tmp[5] = {0};
198 minibuffer_taint_hist();
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)
208 memmove(c + len, c, strlen(c)+1);
210 ministate.buffer.cpoff++;
212 recompute_completions(1);
216 sensible_self_insert(void)
219 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
220 global_key_unbound();
224 minibuffer_self_insert();
234 vl = ministate.compl.buffer.current_line;
235 if (vl == NULL || vl->parent->flags & L_HIDDEN)
238 t = vl->parent->line;
239 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
240 if (!strcmp(cmd->cmd, t)) {
241 minibuffer_insert_current_candidate();
243 minibuffer_hist_save_entry();
244 cmd->fn(current_buffer());
254 ir_select_gemini(void)
256 char buf[1025] = {0};
258 struct tab *tab = current_tab;
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);
271 ir_select_reply(void)
273 char buf[1025] = {0};
275 struct tab *tab = current_tab;
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);
289 ir_select_gopher(void)
292 minibuffer_hist_save_entry();
294 gopher_send_search_req(current_tab, ministate.buf);
300 char url[GEMINI_URL_LEN+1];
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);
313 if (*ministate.buf != '\0')
314 add_to_bookmarks(ministate.buf);
324 if ((tab = minibuffer_metadata()) == NULL) {
325 message("No tab selected");
338 if ((l = minibuffer_metadata()) == NULL) {
339 message("No link selected");
344 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
348 jump_to_line(struct line *l)
351 struct buffer *buffer;
353 buffer = current_buffer();
355 TAILQ_FOREACH(vl, &buffer->head, vlines) {
361 message("Ops, %s error! Please report to %s",
362 __func__, PACKAGE_BUGREPORT);
364 buffer->top_line = vl;
365 buffer->current_line = vl;
374 if ((l = minibuffer_metadata()) == NULL) {
375 message("No line selected");
388 if ((l = minibuffer_metadata()) == NULL) {
389 message("No line selected");
398 yornp_self_insert(void)
400 if (thiskey.key != 'y' && thiskey.key != 'n') {
401 message("Please answer y or n");
406 yornp_cb(thiskey.key == 'y', yornp_data);
413 yornp_cb(0, yornp_data);
417 read_self_insert(void)
419 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
420 global_key_unbound();
424 minibuffer_self_insert();
431 read_cb(NULL, read_data);
438 minibuffer_hist_save_entry();
439 read_cb(ministate.buf, read_data);
443 * TODO: we should collect this asynchronously...
446 populate_compl_buffer(complfn *fn, void *data)
448 const char *s, *descr;
454 b = &ministate.compl.buffer;
459 while ((s = fn(&data, &linedata, &descr)) != NULL) {
460 if ((l = calloc(1, sizeof(*l))) == NULL)
463 l->type = LINE_COMPL;
465 l->alt = (char*)descr;
466 if ((l->line = strdup(s)) == NULL)
469 TAILQ_INSERT_TAIL(&p->head, l, lines);
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;
509 exit_minibuffer(void)
511 if (in_minibuffer == MB_COMPREAD) {
512 erase_buffer(&ministate.compl.buffer);
513 ui_schedule_redraw();
517 base_map = &global_map;
518 current_map = &global_map;
522 yornp(const char *prompt, void (*fn)(int, struct tab*),
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);
543 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
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);
562 handle_clear_echoarea(int fd, short ev, void *d)
564 free(ministate.curmesg);
565 ministate.curmesg = NULL;
567 ui_after_message_hook();
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;
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();
592 message(const char *fmt, ...)
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);