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];
93 char *input, **ap, *words[10];
99 if (in_minibuffer != MB_COMPREAD)
102 if (ministate.hist_cur != NULL)
103 text = ministate.hist_cur->h;
105 text = ministate.buf;
107 strlcpy(buf, text, sizeof(buf));
110 /* tokenize the input */
111 for (ap = words; ap < words + nitems(words) &&
112 (*ap = strsep(&input, " ")) != NULL;) {
117 b = &ministate.compl.buffer;
118 TAILQ_FOREACH(l, &b->page.head, lines) {
119 l->type = LINE_COMPL;
120 if (add && l->flags & L_HIDDEN)
122 if (matches(words, len, l)) {
123 if (l->flags & L_HIDDEN)
125 l->flags &= ~L_HIDDEN;
127 if (!(l->flags & L_HIDDEN))
129 l->flags |= L_HIDDEN;
133 if (b->current_line == NULL)
134 b->current_line = TAILQ_FIRST(&b->head);
135 b->current_line = adjust_line(b->current_line, b);
136 vl = b->current_line;
137 if (ministate.compl.must_select && vl != NULL)
138 vl->parent->type = LINE_COMPL_CURRENT;
142 minibuffer_insert_current_candidate(void)
146 vl = ministate.compl.buffer.current_line;
147 if (vl == NULL || vl->parent->flags & L_HIDDEN)
150 minibuffer_taint_hist();
151 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
152 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
158 minibuffer_metadata(void)
162 vl = ministate.compl.buffer.current_line;
164 if (vl == NULL || vl->parent->flags & L_HIDDEN)
167 return vl->parent->data;
171 minibuffer_compl_text(void)
175 if (ministate.hist_cur != NULL)
176 return ministate.hist_cur->h;
178 vl = ministate.compl.buffer.current_line;
179 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
180 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
181 return ministate.buf;
182 return vl->parent->line;
186 minibuffer_hist_save_entry(void)
191 if (ministate.history == NULL)
194 if ((hist = calloc(1, sizeof(*hist))) == NULL)
197 t = minibuffer_compl_text();
198 strlcpy(hist->h, t, sizeof(hist->h));
200 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
201 ministate.history->len++;
205 * taint the minibuffer cache: if we're currently showing a history
206 * element, copy that to the current buf and reset the "history
210 minibuffer_taint_hist(void)
212 if (ministate.hist_cur == NULL)
215 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
216 ministate.hist_cur = NULL;
217 ministate.buffer.current_line->line = ministate.buf;
221 minibuffer_self_insert(void)
223 char *c, tmp[5] = {0};
226 minibuffer_taint_hist();
231 len = utf8_encode(thiskey.cp, tmp);
232 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
233 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
236 memmove(c + len, c, strlen(c)+1);
238 ministate.buffer.cpoff++;
240 recompute_completions(1);
244 sensible_self_insert(void)
247 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
248 global_key_unbound();
252 minibuffer_self_insert();
261 t = minibuffer_compl_text();
262 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
263 if (!strcmp(cmd->cmd, t)) {
264 minibuffer_hist_save_entry();
266 cmd->fn(current_buffer());
275 ir_select_gemini(void)
277 char buf[1025] = {0};
279 struct tab *tab = current_tab;
281 minibuffer_hist_save_entry();
283 /* a bit ugly but... */
284 memcpy(&uri, &tab->uri, sizeof(tab->uri));
285 phos_uri_set_query(&uri, minibuffer_compl_text());
286 phos_serialize_uri(&uri, buf, sizeof(buf));
289 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
293 ir_select_reply(void)
295 char buf[1025] = {0};
297 struct tab *tab = current_tab;
299 minibuffer_hist_save_entry();
301 /* a bit ugly but... */
302 strlcpy(buf, tab->last_input_url, sizeof(buf));
303 phos_parse_absolute_uri(buf, &uri);
304 phos_uri_set_query(&uri, minibuffer_compl_text());
305 phos_serialize_uri(&uri, buf, sizeof(buf));
308 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
312 ir_select_gopher(void)
314 minibuffer_hist_save_entry();
315 gopher_send_search_req(current_tab, minibuffer_compl_text());
322 char url[GEMINI_URL_LEN+1];
324 minibuffer_hist_save_entry();
325 humanify_url(minibuffer_compl_text(), url, sizeof(url));
327 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
334 if (*ministate.buf != '\0')
335 add_to_bookmarks(ministate.buf);
345 if ((tab = minibuffer_metadata()) == NULL) {
346 message("No tab selected");
359 if ((l = minibuffer_metadata()) == NULL) {
360 message("No link selected");
365 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
369 jump_to_line(struct line *l)
372 struct buffer *buffer;
374 buffer = current_buffer();
376 TAILQ_FOREACH(vl, &buffer->head, vlines) {
382 message("Ops, %s error! Please report to %s",
383 __func__, PACKAGE_BUGREPORT);
385 buffer->top_line = vl;
386 buffer->current_line = vl;
395 if ((l = minibuffer_metadata()) == NULL) {
396 message("No line selected");
409 if ((l = minibuffer_metadata()) == NULL) {
410 message("No line selected");
419 yornp_self_insert(void)
421 if (thiskey.key != 'y' && thiskey.key != 'n') {
422 message("Please answer y or n");
427 yornp_cb(thiskey.key == 'y', yornp_data);
434 yornp_cb(0, yornp_data);
438 read_self_insert(void)
440 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
441 global_key_unbound();
445 minibuffer_self_insert();
452 read_cb(NULL, read_data);
459 minibuffer_hist_save_entry();
460 read_cb(ministate.buf, read_data);
464 * TODO: we should collect this asynchronously...
467 populate_compl_buffer(complfn *fn, void *data)
469 const char *s, *descr;
475 b = &ministate.compl.buffer;
480 while ((s = fn(&data, &linedata, &descr)) != NULL) {
481 if ((l = calloc(1, sizeof(*l))) == NULL)
484 l->type = LINE_COMPL;
486 l->alt = (char*)descr;
487 if ((l->line = strdup(s)) == NULL)
490 TAILQ_INSERT_TAIL(&p->head, l, lines);
496 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
497 ministate.compl.must_select)
498 l->type = LINE_COMPL_CURRENT;
502 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
503 void (*abortfn)(void), struct histhead *hist,
504 complfn *complfn, void *compldata, int must_select)
506 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
507 if (in_minibuffer == MB_COMPREAD) {
508 ui_schedule_redraw();
510 ministate.compl.fn = complfn;
511 ministate.compl.data = compldata;
512 ministate.compl.must_select = must_select;
513 populate_compl_buffer(complfn, compldata);
516 base_map = &minibuffer_map;
517 current_map = &minibuffer_map;
519 base_map->unhandled_input = self_insert_fn;
521 ministate.donefn = donefn;
522 ministate.abortfn = abortfn;
523 memset(ministate.buf, 0, sizeof(ministate.buf));
524 ministate.buffer.current_line = &ministate.vline;
525 ministate.buffer.current_line->line = ministate.buf;
526 ministate.buffer.cpoff = 0;
527 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
529 ministate.history = hist;
530 ministate.hist_cur = NULL;
531 ministate.hist_off = 0;
535 exit_minibuffer(void)
537 if (in_minibuffer == MB_COMPREAD) {
538 erase_buffer(&ministate.compl.buffer);
539 ui_schedule_redraw();
543 base_map = &global_map;
544 current_map = &global_map;
548 yornp(const char *prompt, void (*fn)(int, struct tab*),
560 enter_minibuffer(yornp_self_insert, yornp_self_insert,
561 yornp_abort, NULL, NULL, NULL, 0);
563 len = sizeof(ministate.prompt);
564 strlcpy(ministate.prompt, prompt, len);
565 strlcat(ministate.prompt, " (y or n) ", len);
569 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
579 enter_minibuffer(read_self_insert, read_select, read_abort,
580 &read_history, NULL, NULL, 0);
582 len = sizeof(ministate.prompt);
583 strlcpy(ministate.prompt, prompt, len);
584 strlcat(ministate.prompt, ": ", len);
588 handle_clear_echoarea(int fd, short ev, void *d)
590 free(ministate.curmesg);
591 ministate.curmesg = NULL;
593 ui_after_message_hook();
597 vmessage(const char *fmt, va_list ap)
599 if (evtimer_pending(&clechoev, NULL))
600 evtimer_del(&clechoev);
602 free(ministate.curmesg);
603 ministate.curmesg = NULL;
606 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
607 evtimer_add(&clechoev, &clechoev_timer);
609 /* TODO: what to do if the allocation fails here? */
610 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
611 ministate.curmesg = NULL;
614 ui_after_message_hook();
618 message(const char *fmt, ...)
628 minibuffer_init(void)
630 TAILQ_INIT(&eecmd_history.head);
631 TAILQ_INIT(&ir_history.head);
632 TAILQ_INIT(&lu_history.head);
633 TAILQ_INIT(&read_history.head);
635 TAILQ_INIT(&ministate.compl.buffer.head);
636 TAILQ_INIT(&ministate.compl.buffer.page.head);
638 ministate.line.type = LINE_TEXT;
639 ministate.vline.parent = &ministate.line;
640 ministate.buffer.page.name = "*minibuffer*";
641 ministate.buffer.current_line = &ministate.vline;
643 evtimer_set(&clechoev, handle_clear_echoarea, NULL);