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.
26 #include "minibuffer.h"
32 #define nitems(x) (sizeof(x)/sizeof(x[0]))
34 static void *minibuffer_metadata(void);
35 static const char *minibuffer_compl_text(void);
36 static void minibuffer_hist_save_entry(void);
37 static void yornp_self_insert(void);
38 static void yornp_abort(void);
39 static void read_self_insert(void);
40 static void read_abort(void);
41 static void read_select(void);
42 static void handle_clear_echoarea(int, short, void *);
44 static struct event clechoev;
45 static struct timeval clechoev_timer = { 5, 0 };
47 static void (*yornp_cb)(int, struct tab *);
48 static struct tab *yornp_data;
50 static void (*read_cb)(const char*, struct tab *);
51 static struct tab *read_data;
53 /* XXX: don't forget to init these in minibuffer_init */
54 struct histhead eecmd_history,
59 struct ministate ministate;
61 struct buffer minibufferwin;
66 matches(char **words, size_t len, struct line *l)
71 for (i = 0; i < len; ++i) {
74 if (strcasestr(l->line, words[i]) != NULL)
77 strcasestr(l->alt, words[i]) != NULL)
88 * Recompute the visible completions. If add is 1, don't consider the
89 * ones already hidden.
92 recompute_completions(int add)
94 static char buf[GEMINI_URL_LEN];
96 char *input, **ap, *words[10];
102 if (in_minibuffer != MB_COMPREAD)
105 if (ministate.hist_cur != NULL)
106 text = ministate.hist_cur->h;
108 text = ministate.buf;
110 strlcpy(buf, text, sizeof(buf));
113 /* tokenize the input */
114 for (ap = words; ap < words + nitems(words) &&
115 (*ap = strsep(&input, " ")) != NULL;) {
120 b = &ministate.compl.buffer;
121 TAILQ_FOREACH(l, &b->page.head, lines) {
122 l->type = LINE_COMPL;
123 if (add && l->flags & L_HIDDEN)
125 if (matches(words, len, l)) {
126 if (l->flags & L_HIDDEN)
128 l->flags &= ~L_HIDDEN;
130 if (!(l->flags & L_HIDDEN))
132 l->flags |= L_HIDDEN;
136 if (b->current_line == NULL)
137 b->current_line = TAILQ_FIRST(&b->head);
138 b->current_line = adjust_line(b->current_line, b);
139 vl = b->current_line;
140 if (ministate.compl.must_select && vl != NULL)
141 vl->parent->type = LINE_COMPL_CURRENT;
145 minibuffer_insert_current_candidate(void)
149 vl = ministate.compl.buffer.current_line;
150 if (vl == NULL || vl->parent->flags & L_HIDDEN)
153 minibuffer_taint_hist();
154 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
155 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
161 minibuffer_metadata(void)
165 vl = ministate.compl.buffer.current_line;
167 if (vl == NULL || vl->parent->flags & L_HIDDEN)
170 return vl->parent->data;
174 minibuffer_compl_text(void)
178 if (ministate.hist_cur != NULL)
179 return ministate.hist_cur->h;
181 vl = ministate.compl.buffer.current_line;
182 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
183 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
184 return ministate.buf;
185 return vl->parent->line;
189 minibuffer_hist_save_entry(void)
194 if (ministate.history == NULL)
197 if ((hist = calloc(1, sizeof(*hist))) == NULL)
200 t = minibuffer_compl_text();
201 strlcpy(hist->h, t, sizeof(hist->h));
203 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
204 ministate.history->len++;
208 * taint the minibuffer cache: if we're currently showing a history
209 * element, copy that to the current buf and reset the "history
213 minibuffer_taint_hist(void)
215 if (ministate.hist_cur == NULL)
218 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
219 ministate.hist_cur = NULL;
220 ministate.buffer.current_line->line = ministate.buf;
224 minibuffer_self_insert(void)
226 char *c, tmp[5] = {0};
229 minibuffer_taint_hist();
234 len = utf8_encode(thiskey.cp, tmp);
235 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
236 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
239 memmove(c + len, c, strlen(c)+1);
241 ministate.buffer.cpoff++;
243 recompute_completions(1);
247 sensible_self_insert(void)
250 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
251 global_key_unbound();
255 minibuffer_self_insert();
264 t = minibuffer_compl_text();
265 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
266 if (!strcmp(cmd->cmd, t)) {
267 minibuffer_hist_save_entry();
269 cmd->fn(current_buffer());
278 ir_select_gemini(void)
280 char buf[1025] = {0};
282 struct tab *tab = current_tab;
284 minibuffer_hist_save_entry();
286 /* a bit ugly but... */
287 memcpy(&uri, &tab->uri, sizeof(tab->uri));
288 phos_uri_set_query(&uri, minibuffer_compl_text());
289 phos_serialize_uri(&uri, buf, sizeof(buf));
292 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
296 ir_select_reply(void)
298 char buf[1025] = {0};
300 struct tab *tab = current_tab;
302 minibuffer_hist_save_entry();
304 /* a bit ugly but... */
305 strlcpy(buf, tab->last_input_url, sizeof(buf));
306 phos_parse_absolute_uri(buf, &uri);
307 phos_uri_set_query(&uri, minibuffer_compl_text());
308 phos_serialize_uri(&uri, buf, sizeof(buf));
311 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
315 ir_select_gopher(void)
317 minibuffer_hist_save_entry();
318 gopher_send_search_req(current_tab, minibuffer_compl_text());
325 char url[GEMINI_URL_LEN+1];
327 minibuffer_hist_save_entry();
328 humanify_url(minibuffer_compl_text(), url, sizeof(url));
330 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
337 if (*ministate.buf != '\0') {
338 if (!bookmark_page(ministate.buf))
339 message("failed to bookmark page: %s",
350 if ((tab = minibuffer_metadata()) == NULL) {
351 message("No tab selected");
364 if ((l = minibuffer_metadata()) == NULL) {
365 message("No link selected");
370 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
374 jump_to_line(struct line *l)
377 struct buffer *buffer;
379 buffer = current_buffer();
381 TAILQ_FOREACH(vl, &buffer->head, vlines) {
387 message("Ops, %s error! Please report to %s",
388 __func__, PACKAGE_BUGREPORT);
390 buffer->top_line = vl;
391 buffer->current_line = vl;
400 if ((l = minibuffer_metadata()) == NULL) {
401 message("No line selected");
414 if ((l = minibuffer_metadata()) == NULL) {
415 message("No line selected");
424 yornp_self_insert(void)
426 if (thiskey.key != 'y' && thiskey.key != 'n') {
427 message("Please answer y or n");
432 yornp_cb(thiskey.key == 'y', yornp_data);
439 yornp_cb(0, yornp_data);
443 read_self_insert(void)
445 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
446 global_key_unbound();
450 minibuffer_self_insert();
457 read_cb(NULL, read_data);
464 minibuffer_hist_save_entry();
465 read_cb(ministate.buf, read_data);
469 * TODO: we should collect this asynchronously...
472 populate_compl_buffer(complfn *fn, void *data)
474 const char *s, *descr;
480 b = &ministate.compl.buffer;
485 while ((s = fn(&data, &linedata, &descr)) != NULL) {
486 if ((l = calloc(1, sizeof(*l))) == NULL)
489 l->type = LINE_COMPL;
491 l->alt = (char*)descr;
492 if ((l->line = strdup(s)) == NULL)
495 TAILQ_INSERT_TAIL(&p->head, l, lines);
501 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
502 ministate.compl.must_select)
503 l->type = LINE_COMPL_CURRENT;
507 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
508 void (*abortfn)(void), struct histhead *hist,
509 complfn *complfn, void *compldata, int must_select)
511 ministate.compl.must_select = must_select;
512 ministate.compl.fn = complfn;
513 ministate.compl.data = compldata;
515 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
516 if (in_minibuffer == MB_COMPREAD) {
517 populate_compl_buffer(complfn, compldata);
518 ui_schedule_redraw();
521 base_map = &minibuffer_map;
522 current_map = &minibuffer_map;
524 base_map->unhandled_input = self_insert_fn;
526 ministate.donefn = donefn;
527 ministate.abortfn = abortfn;
528 memset(ministate.buf, 0, sizeof(ministate.buf));
529 ministate.buffer.current_line = &ministate.vline;
530 ministate.buffer.current_line->line = ministate.buf;
531 ministate.buffer.cpoff = 0;
532 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
534 ministate.history = hist;
535 ministate.hist_cur = NULL;
536 ministate.hist_off = 0;
540 exit_minibuffer(void)
542 if (in_minibuffer == MB_COMPREAD) {
543 erase_buffer(&ministate.compl.buffer);
544 ui_schedule_redraw();
548 base_map = &global_map;
549 current_map = &global_map;
553 yornp(const char *prompt, void (*fn)(int, struct tab*),
565 enter_minibuffer(yornp_self_insert, yornp_self_insert,
566 yornp_abort, NULL, NULL, NULL, 0);
568 len = sizeof(ministate.prompt);
569 strlcpy(ministate.prompt, prompt, len);
570 strlcat(ministate.prompt, " (y or n) ", len);
574 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
584 enter_minibuffer(read_self_insert, read_select, read_abort,
585 &read_history, NULL, NULL, 0);
587 len = sizeof(ministate.prompt);
588 strlcpy(ministate.prompt, prompt, len);
589 strlcat(ministate.prompt, ": ", len);
593 handle_clear_echoarea(int fd, short ev, void *d)
595 free(ministate.curmesg);
596 ministate.curmesg = NULL;
598 ui_after_message_hook();
602 vmessage(const char *fmt, va_list ap)
604 if (evtimer_pending(&clechoev, NULL))
605 evtimer_del(&clechoev);
607 free(ministate.curmesg);
608 ministate.curmesg = NULL;
611 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
612 evtimer_add(&clechoev, &clechoev_timer);
614 /* TODO: what to do if the allocation fails here? */
615 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
616 ministate.curmesg = NULL;
619 ui_after_message_hook();
623 message(const char *fmt, ...)
633 minibuffer_init(void)
635 TAILQ_INIT(&eecmd_history.head);
636 TAILQ_INIT(&ir_history.head);
637 TAILQ_INIT(&lu_history.head);
638 TAILQ_INIT(&read_history.head);
640 TAILQ_INIT(&ministate.compl.buffer.head);
641 TAILQ_INIT(&ministate.compl.buffer.page.head);
643 ministate.line.type = LINE_TEXT;
644 ministate.vline.parent = &ministate.line;
645 ministate.buffer.page.name = "*minibuffer*";
646 ministate.buffer.current_line = &ministate.vline;
648 evtimer_set(&clechoev, handle_clear_echoarea, NULL);