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 static void *minibuffer_metadata(void);
30 static void minibuffer_hist_save_entry(void);
31 static void yornp_self_insert(void);
32 static void yornp_abort(void);
33 static void read_self_insert(void);
34 static void read_abort(void);
35 static void read_select(void);
36 static void handle_clear_echoarea(int, short, void *);
38 static struct event clechoev;
39 static struct timeval clechoev_timer = { 5, 0 };
41 static void (*yornp_cb)(int, struct tab *);
42 static struct tab *yornp_data;
44 static void (*read_cb)(const char*, struct tab *);
45 static struct tab *read_data;
47 /* XXX: don't forget to init these in minibuffer_init */
48 struct histhead eecmd_history,
53 struct ministate ministate;
55 struct buffer minibufferwin;
60 * Recompute the visible completions. If add is 1, don't consider the
61 * ones already hidden.
64 recompute_completions(int add)
70 if (in_minibuffer != MB_COMPREAD)
73 b = &ministate.compl.buffer;
74 TAILQ_FOREACH(l, &b->page.head, lines) {
76 if (add && l->flags & L_HIDDEN)
78 if (strcasestr(l->line, ministate.buf) != NULL ||
79 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
80 if (l->flags & L_HIDDEN)
82 l->flags &= ~L_HIDDEN;
84 if (!(l->flags & L_HIDDEN))
90 if (b->current_line == NULL)
91 b->current_line = TAILQ_FIRST(&b->head);
92 b->current_line = adjust_line(b->current_line, b);
95 vl->parent->type = LINE_COMPL_CURRENT;
99 minibuffer_insert_current_candidate(void)
103 vl = ministate.compl.buffer.current_line;
104 if (vl == NULL || vl->parent->flags & L_HIDDEN)
107 minibuffer_taint_hist();
108 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
109 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
115 minibuffer_metadata(void)
119 vl = ministate.compl.buffer.current_line;
121 if (vl == NULL || vl->parent->flags & L_HIDDEN)
124 return vl->parent->data;
128 minibuffer_hist_save_entry(void)
132 if (ministate.history == NULL)
135 if ((hist = calloc(1, sizeof(*hist))) == NULL)
138 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
140 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
141 ministate.history->len++;
145 * taint the minibuffer cache: if we're currently showing a history
146 * element, copy that to the current buf and reset the "history
150 minibuffer_taint_hist(void)
152 if (ministate.hist_cur == NULL)
155 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
156 ministate.hist_cur = NULL;
157 ministate.buffer.current_line->line = ministate.buf;
161 minibuffer_self_insert(void)
163 char *c, tmp[5] = {0};
166 minibuffer_taint_hist();
171 len = utf8_encode(thiskey.cp, tmp);
172 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
173 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
176 memmove(c + len, c, strlen(c)+1);
178 ministate.buffer.cpoff++;
180 recompute_completions(1);
184 sensible_self_insert(void)
187 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
188 global_key_unbound();
192 minibuffer_self_insert();
202 vl = ministate.compl.buffer.current_line;
203 if (vl == NULL || vl->parent->flags & L_HIDDEN)
206 t = vl->parent->line;
207 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
208 if (!strcmp(cmd->cmd, t)) {
209 minibuffer_insert_current_candidate();
211 minibuffer_hist_save_entry();
212 cmd->fn(current_buffer());
222 ir_select_gemini(void)
224 char buf[1025] = {0};
226 struct tab *tab = current_tab;
229 minibuffer_hist_save_entry();
231 /* a bit ugly but... */
232 memcpy(&uri, &tab->uri, sizeof(tab->uri));
233 phos_uri_set_query(&uri, ministate.buf);
234 phos_serialize_uri(&uri, buf, sizeof(buf));
235 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
239 ir_select_gopher(void)
242 minibuffer_hist_save_entry();
244 gopher_send_search_req(current_tab, ministate.buf);
250 char url[GEMINI_URL_LEN+1];
253 minibuffer_hist_save_entry();
255 humanify_url(ministate.buf, url, sizeof(url));
256 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
263 if (*ministate.buf != '\0')
264 add_to_bookmarks(ministate.buf);
274 if ((tab = minibuffer_metadata()) == NULL) {
275 message("No tab selected");
288 if ((l = minibuffer_metadata()) == NULL) {
289 message("No link selected");
294 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
298 jump_to_line(struct line *l)
301 struct buffer *buffer;
303 buffer = current_buffer();
305 TAILQ_FOREACH(vl, &buffer->head, vlines) {
311 message("Ops, %s error! Please report to %s",
312 __func__, PACKAGE_BUGREPORT);
314 buffer->top_line = vl;
315 buffer->current_line = vl;
324 if ((l = minibuffer_metadata()) == NULL) {
325 message("No line selected");
338 if ((l = minibuffer_metadata()) == NULL) {
339 message("No line selected");
348 yornp_self_insert(void)
350 if (thiskey.key != 'y' && thiskey.key != 'n') {
351 message("Please answer y or n");
356 yornp_cb(thiskey.key == 'y', yornp_data);
363 yornp_cb(0, yornp_data);
367 read_self_insert(void)
369 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
370 global_key_unbound();
374 minibuffer_self_insert();
381 read_cb(NULL, read_data);
388 minibuffer_hist_save_entry();
389 read_cb(ministate.buf, read_data);
393 * TODO: we should collect this asynchronously...
396 populate_compl_buffer(complfn *fn, void *data)
398 const char *s, *descr;
404 b = &ministate.compl.buffer;
409 while ((s = fn(&data, &linedata, &descr)) != NULL) {
410 if ((l = calloc(1, sizeof(*l))) == NULL)
413 l->type = LINE_COMPL;
415 l->alt = (char*)descr;
416 if ((l->line = strdup(s)) == NULL)
419 TAILQ_INSERT_TAIL(&p->head, l, lines);
425 if ((l = TAILQ_FIRST(&p->head)) != NULL)
426 l->type = LINE_COMPL_CURRENT;
430 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
431 void (*abortfn)(void), struct histhead *hist,
432 complfn *complfn, void *compldata)
434 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
435 if (in_minibuffer == MB_COMPREAD) {
436 ui_schedule_redraw();
438 ministate.compl.fn = complfn;
439 ministate.compl.data = compldata;
440 populate_compl_buffer(complfn, compldata);
443 base_map = &minibuffer_map;
444 current_map = &minibuffer_map;
446 base_map->unhandled_input = self_insert_fn;
448 ministate.donefn = donefn;
449 ministate.abortfn = abortfn;
450 memset(ministate.buf, 0, sizeof(ministate.buf));
451 ministate.buffer.current_line = &ministate.vline;
452 ministate.buffer.current_line->line = ministate.buf;
453 ministate.buffer.cpoff = 0;
454 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
456 ministate.history = hist;
457 ministate.hist_cur = NULL;
458 ministate.hist_off = 0;
462 exit_minibuffer(void)
464 if (in_minibuffer == MB_COMPREAD) {
465 erase_buffer(&ministate.compl.buffer);
466 ui_schedule_redraw();
470 base_map = &global_map;
471 current_map = &global_map;
475 yornp(const char *prompt, void (*fn)(int, struct tab*),
487 enter_minibuffer(yornp_self_insert, yornp_self_insert,
488 yornp_abort, NULL, NULL, NULL);
490 len = sizeof(ministate.prompt);
491 strlcpy(ministate.prompt, prompt, len);
492 strlcat(ministate.prompt, " (y or n) ", len);
496 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
506 enter_minibuffer(read_self_insert, read_select, read_abort,
507 &read_history, NULL, NULL);
509 len = sizeof(ministate.prompt);
510 strlcpy(ministate.prompt, prompt, len);
511 strlcat(ministate.prompt, ": ", len);
515 handle_clear_echoarea(int fd, short ev, void *d)
517 free(ministate.curmesg);
518 ministate.curmesg = NULL;
520 ui_after_message_hook();
524 vmessage(const char *fmt, va_list ap)
526 if (evtimer_pending(&clechoev, NULL))
527 evtimer_del(&clechoev);
529 free(ministate.curmesg);
530 ministate.curmesg = NULL;
533 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
534 evtimer_add(&clechoev, &clechoev_timer);
536 /* TODO: what to do if the allocation fails here? */
537 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
538 ministate.curmesg = NULL;
541 ui_after_message_hook();
545 message(const char *fmt, ...)
555 minibuffer_init(void)
557 TAILQ_INIT(&eecmd_history.head);
558 TAILQ_INIT(&ir_history.head);
559 TAILQ_INIT(&lu_history.head);
560 TAILQ_INIT(&read_history.head);
562 TAILQ_INIT(&ministate.compl.buffer.head);
563 TAILQ_INIT(&ministate.compl.buffer.page.head);
565 ministate.line.type = LINE_TEXT;
566 ministate.vline.parent = &ministate.line;
567 ministate.buffer.page.name = "*minibuffer*";
568 ministate.buffer.current_line = &ministate.vline;
570 evtimer_set(&clechoev, handle_clear_echoarea, NULL);