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_hist_save_entry();
257 cmd->fn(current_buffer());
266 ir_select_gemini(void)
268 char buf[1025] = {0};
270 struct tab *tab = current_tab;
272 minibuffer_hist_save_entry();
274 /* a bit ugly but... */
275 memcpy(&uri, &tab->uri, sizeof(tab->uri));
276 phos_uri_set_query(&uri, minibuffer_compl_text());
277 phos_serialize_uri(&uri, buf, sizeof(buf));
280 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
284 ir_select_reply(void)
286 char buf[1025] = {0};
288 struct tab *tab = current_tab;
290 minibuffer_hist_save_entry();
292 /* a bit ugly but... */
293 strlcpy(buf, tab->last_input_url, sizeof(buf));
294 phos_parse_absolute_uri(buf, &uri);
295 phos_uri_set_query(&uri, minibuffer_compl_text());
296 phos_serialize_uri(&uri, buf, sizeof(buf));
299 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
303 ir_select_gopher(void)
305 minibuffer_hist_save_entry();
306 gopher_send_search_req(current_tab, minibuffer_compl_text());
313 char url[GEMINI_URL_LEN+1];
315 minibuffer_hist_save_entry();
316 humanify_url(minibuffer_compl_text(), url, sizeof(url));
318 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
325 if (*ministate.buf != '\0')
326 add_to_bookmarks(ministate.buf);
336 if ((tab = minibuffer_metadata()) == NULL) {
337 message("No tab selected");
350 if ((l = minibuffer_metadata()) == NULL) {
351 message("No link selected");
356 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
360 jump_to_line(struct line *l)
363 struct buffer *buffer;
365 buffer = current_buffer();
367 TAILQ_FOREACH(vl, &buffer->head, vlines) {
373 message("Ops, %s error! Please report to %s",
374 __func__, PACKAGE_BUGREPORT);
376 buffer->top_line = vl;
377 buffer->current_line = vl;
386 if ((l = minibuffer_metadata()) == NULL) {
387 message("No line selected");
400 if ((l = minibuffer_metadata()) == NULL) {
401 message("No line selected");
410 yornp_self_insert(void)
412 if (thiskey.key != 'y' && thiskey.key != 'n') {
413 message("Please answer y or n");
418 yornp_cb(thiskey.key == 'y', yornp_data);
425 yornp_cb(0, yornp_data);
429 read_self_insert(void)
431 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
432 global_key_unbound();
436 minibuffer_self_insert();
443 read_cb(NULL, read_data);
450 minibuffer_hist_save_entry();
451 read_cb(ministate.buf, read_data);
455 * TODO: we should collect this asynchronously...
458 populate_compl_buffer(complfn *fn, void *data)
460 const char *s, *descr;
466 b = &ministate.compl.buffer;
471 while ((s = fn(&data, &linedata, &descr)) != NULL) {
472 if ((l = calloc(1, sizeof(*l))) == NULL)
475 l->type = LINE_COMPL;
477 l->alt = (char*)descr;
478 if ((l->line = strdup(s)) == NULL)
481 TAILQ_INSERT_TAIL(&p->head, l, lines);
487 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
488 ministate.compl.must_select)
489 l->type = LINE_COMPL_CURRENT;
493 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
494 void (*abortfn)(void), struct histhead *hist,
495 complfn *complfn, void *compldata, int must_select)
497 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
498 if (in_minibuffer == MB_COMPREAD) {
499 ui_schedule_redraw();
501 ministate.compl.fn = complfn;
502 ministate.compl.data = compldata;
503 ministate.compl.must_select = must_select;
504 populate_compl_buffer(complfn, compldata);
507 base_map = &minibuffer_map;
508 current_map = &minibuffer_map;
510 base_map->unhandled_input = self_insert_fn;
512 ministate.donefn = donefn;
513 ministate.abortfn = abortfn;
514 memset(ministate.buf, 0, sizeof(ministate.buf));
515 ministate.buffer.current_line = &ministate.vline;
516 ministate.buffer.current_line->line = ministate.buf;
517 ministate.buffer.cpoff = 0;
518 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
520 ministate.history = hist;
521 ministate.hist_cur = NULL;
522 ministate.hist_off = 0;
526 exit_minibuffer(void)
528 if (in_minibuffer == MB_COMPREAD) {
529 erase_buffer(&ministate.compl.buffer);
530 ui_schedule_redraw();
534 base_map = &global_map;
535 current_map = &global_map;
539 yornp(const char *prompt, void (*fn)(int, struct tab*),
551 enter_minibuffer(yornp_self_insert, yornp_self_insert,
552 yornp_abort, NULL, NULL, NULL, 0);
554 len = sizeof(ministate.prompt);
555 strlcpy(ministate.prompt, prompt, len);
556 strlcat(ministate.prompt, " (y or n) ", len);
560 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
570 enter_minibuffer(read_self_insert, read_select, read_abort,
571 &read_history, NULL, NULL, 0);
573 len = sizeof(ministate.prompt);
574 strlcpy(ministate.prompt, prompt, len);
575 strlcat(ministate.prompt, ": ", len);
579 handle_clear_echoarea(int fd, short ev, void *d)
581 free(ministate.curmesg);
582 ministate.curmesg = NULL;
584 ui_after_message_hook();
588 vmessage(const char *fmt, va_list ap)
590 if (evtimer_pending(&clechoev, NULL))
591 evtimer_del(&clechoev);
593 free(ministate.curmesg);
594 ministate.curmesg = NULL;
597 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
598 evtimer_add(&clechoev, &clechoev_timer);
600 /* TODO: what to do if the allocation fails here? */
601 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
602 ministate.curmesg = NULL;
605 ui_after_message_hook();
609 message(const char *fmt, ...)
619 minibuffer_init(void)
621 TAILQ_INIT(&eecmd_history.head);
622 TAILQ_INIT(&ir_history.head);
623 TAILQ_INIT(&lu_history.head);
624 TAILQ_INIT(&read_history.head);
626 TAILQ_INIT(&ministate.compl.buffer.head);
627 TAILQ_INIT(&ministate.compl.buffer.page.head);
629 ministate.line.type = LINE_TEXT;
630 ministate.vline.parent = &ministate.line;
631 ministate.buffer.page.name = "*minibuffer*";
632 ministate.buffer.current_line = &ministate.vline;
634 evtimer_set(&clechoev, handle_clear_echoarea, NULL);