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];
97 if (in_minibuffer != MB_COMPREAD)
100 strlcpy(buf, ministate.buf, sizeof(buf));
103 /* tokenize the input */
104 for (ap = words; ap < words + nitems(words) &&
105 (*ap = strsep(&input, " ")) != NULL;) {
110 b = &ministate.compl.buffer;
111 TAILQ_FOREACH(l, &b->page.head, lines) {
112 l->type = LINE_COMPL;
113 if (add && l->flags & L_HIDDEN)
115 if (matches(words, len, l)) {
116 if (l->flags & L_HIDDEN)
118 l->flags &= ~L_HIDDEN;
120 if (!(l->flags & L_HIDDEN))
122 l->flags |= L_HIDDEN;
126 if (b->current_line == NULL)
127 b->current_line = TAILQ_FIRST(&b->head);
128 b->current_line = adjust_line(b->current_line, b);
132 minibuffer_insert_current_candidate(void)
136 vl = ministate.compl.buffer.current_line;
137 if (vl == NULL || vl->parent->flags & L_HIDDEN)
140 minibuffer_taint_hist();
141 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
142 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
148 minibuffer_metadata(void)
152 vl = ministate.compl.buffer.current_line;
154 if (vl == NULL || vl->parent->flags & L_HIDDEN)
157 return vl->parent->data;
161 minibuffer_compl_text(void)
165 vl = ministate.compl.buffer.current_line;
166 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
167 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
168 return ministate.buf;
169 return vl->parent->line;
173 minibuffer_hist_save_entry(void)
178 if (ministate.history == NULL)
181 if ((hist = calloc(1, sizeof(*hist))) == NULL)
184 t = minibuffer_compl_text();
185 strlcpy(hist->h, t, sizeof(hist->h));
187 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
188 ministate.history->len++;
192 * taint the minibuffer cache: if we're currently showing a history
193 * element, copy that to the current buf and reset the "history
197 minibuffer_taint_hist(void)
199 if (ministate.hist_cur == NULL)
202 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
203 ministate.hist_cur = NULL;
204 ministate.buffer.current_line->line = ministate.buf;
208 minibuffer_self_insert(void)
210 char *c, tmp[5] = {0};
213 minibuffer_taint_hist();
218 len = utf8_encode(thiskey.cp, tmp);
219 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
220 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
223 memmove(c + len, c, strlen(c)+1);
225 ministate.buffer.cpoff++;
227 recompute_completions(1);
231 sensible_self_insert(void)
234 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
235 global_key_unbound();
239 minibuffer_self_insert();
248 t = minibuffer_compl_text();
249 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
250 if (!strcmp(cmd->cmd, t)) {
251 minibuffer_insert_current_candidate();
252 minibuffer_hist_save_entry();
254 cmd->fn(current_buffer());
263 ir_select_gemini(void)
265 char buf[1025] = {0};
267 struct tab *tab = current_tab;
269 minibuffer_hist_save_entry();
271 /* a bit ugly but... */
272 memcpy(&uri, &tab->uri, sizeof(tab->uri));
273 phos_uri_set_query(&uri, minibuffer_compl_text());
274 phos_serialize_uri(&uri, buf, sizeof(buf));
277 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
281 ir_select_reply(void)
283 char buf[1025] = {0};
285 struct tab *tab = current_tab;
287 minibuffer_hist_save_entry();
289 /* a bit ugly but... */
290 strlcpy(buf, tab->last_input_url, sizeof(buf));
291 phos_parse_absolute_uri(buf, &uri);
292 phos_uri_set_query(&uri, minibuffer_compl_text());
293 phos_serialize_uri(&uri, buf, sizeof(buf));
296 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
300 ir_select_gopher(void)
302 minibuffer_hist_save_entry();
303 gopher_send_search_req(current_tab, minibuffer_compl_text());
310 char url[GEMINI_URL_LEN+1];
312 minibuffer_hist_save_entry();
313 humanify_url(minibuffer_compl_text(), url, sizeof(url));
315 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
322 if (*ministate.buf != '\0')
323 add_to_bookmarks(ministate.buf);
333 if ((tab = minibuffer_metadata()) == NULL) {
334 message("No tab selected");
347 if ((l = minibuffer_metadata()) == NULL) {
348 message("No link selected");
353 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
357 jump_to_line(struct line *l)
360 struct buffer *buffer;
362 buffer = current_buffer();
364 TAILQ_FOREACH(vl, &buffer->head, vlines) {
370 message("Ops, %s error! Please report to %s",
371 __func__, PACKAGE_BUGREPORT);
373 buffer->top_line = vl;
374 buffer->current_line = vl;
383 if ((l = minibuffer_metadata()) == NULL) {
384 message("No line selected");
397 if ((l = minibuffer_metadata()) == NULL) {
398 message("No line selected");
407 yornp_self_insert(void)
409 if (thiskey.key != 'y' && thiskey.key != 'n') {
410 message("Please answer y or n");
415 yornp_cb(thiskey.key == 'y', yornp_data);
422 yornp_cb(0, yornp_data);
426 read_self_insert(void)
428 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
429 global_key_unbound();
433 minibuffer_self_insert();
440 read_cb(NULL, read_data);
447 minibuffer_hist_save_entry();
448 read_cb(ministate.buf, read_data);
452 * TODO: we should collect this asynchronously...
455 populate_compl_buffer(complfn *fn, void *data)
457 const char *s, *descr;
463 b = &ministate.compl.buffer;
468 while ((s = fn(&data, &linedata, &descr)) != NULL) {
469 if ((l = calloc(1, sizeof(*l))) == NULL)
472 l->type = LINE_COMPL;
474 l->alt = (char*)descr;
475 if ((l->line = strdup(s)) == NULL)
478 TAILQ_INSERT_TAIL(&p->head, l, lines);
486 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
487 void (*abortfn)(void), struct histhead *hist,
488 complfn *complfn, void *compldata)
490 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
491 if (in_minibuffer == MB_COMPREAD) {
492 ui_schedule_redraw();
494 ministate.compl.fn = complfn;
495 ministate.compl.data = compldata;
496 populate_compl_buffer(complfn, compldata);
499 base_map = &minibuffer_map;
500 current_map = &minibuffer_map;
502 base_map->unhandled_input = self_insert_fn;
504 ministate.donefn = donefn;
505 ministate.abortfn = abortfn;
506 memset(ministate.buf, 0, sizeof(ministate.buf));
507 ministate.buffer.current_line = &ministate.vline;
508 ministate.buffer.current_line->line = ministate.buf;
509 ministate.buffer.cpoff = 0;
510 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
512 ministate.history = hist;
513 ministate.hist_cur = NULL;
514 ministate.hist_off = 0;
518 exit_minibuffer(void)
520 if (in_minibuffer == MB_COMPREAD) {
521 erase_buffer(&ministate.compl.buffer);
522 ui_schedule_redraw();
526 base_map = &global_map;
527 current_map = &global_map;
531 yornp(const char *prompt, void (*fn)(int, struct tab*),
543 enter_minibuffer(yornp_self_insert, yornp_self_insert,
544 yornp_abort, NULL, NULL, NULL);
546 len = sizeof(ministate.prompt);
547 strlcpy(ministate.prompt, prompt, len);
548 strlcat(ministate.prompt, " (y or n) ", len);
552 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
562 enter_minibuffer(read_self_insert, read_select, read_abort,
563 &read_history, NULL, NULL);
565 len = sizeof(ministate.prompt);
566 strlcpy(ministate.prompt, prompt, len);
567 strlcat(ministate.prompt, ": ", len);
571 handle_clear_echoarea(int fd, short ev, void *d)
573 free(ministate.curmesg);
574 ministate.curmesg = NULL;
576 ui_after_message_hook();
580 vmessage(const char *fmt, va_list ap)
582 if (evtimer_pending(&clechoev, NULL))
583 evtimer_del(&clechoev);
585 free(ministate.curmesg);
586 ministate.curmesg = NULL;
589 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
590 evtimer_add(&clechoev, &clechoev_timer);
592 /* TODO: what to do if the allocation fails here? */
593 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
594 ministate.curmesg = NULL;
597 ui_after_message_hook();
601 message(const char *fmt, ...)
611 minibuffer_init(void)
613 TAILQ_INIT(&eecmd_history.head);
614 TAILQ_INIT(&ir_history.head);
615 TAILQ_INIT(&lu_history.head);
616 TAILQ_INIT(&read_history.head);
618 TAILQ_INIT(&ministate.compl.buffer.head);
619 TAILQ_INIT(&ministate.compl.buffer.page.head);
621 ministate.line.type = LINE_TEXT;
622 ministate.vline.parent = &ministate.line;
623 ministate.buffer.page.name = "*minibuffer*";
624 ministate.buffer.current_line = &ministate.vline;
626 evtimer_set(&clechoev, handle_clear_echoarea, NULL);