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_reply(void)
241 char buf[1025] = {0};
243 struct tab *tab = current_tab;
246 minibuffer_hist_save_entry();
248 /* a bit ugly but... */
249 strlcpy(buf, tab->last_input_url, sizeof(buf));
250 phos_parse_absolute_uri(buf, &uri);
251 phos_uri_set_query(&uri, ministate.buf);
252 phos_serialize_uri(&uri, buf, sizeof(buf));
253 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
257 ir_select_gopher(void)
260 minibuffer_hist_save_entry();
262 gopher_send_search_req(current_tab, ministate.buf);
268 char url[GEMINI_URL_LEN+1];
271 minibuffer_hist_save_entry();
273 humanify_url(ministate.buf, url, sizeof(url));
274 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
281 if (*ministate.buf != '\0')
282 add_to_bookmarks(ministate.buf);
292 if ((tab = minibuffer_metadata()) == NULL) {
293 message("No tab selected");
306 if ((l = minibuffer_metadata()) == NULL) {
307 message("No link selected");
312 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
316 jump_to_line(struct line *l)
319 struct buffer *buffer;
321 buffer = current_buffer();
323 TAILQ_FOREACH(vl, &buffer->head, vlines) {
329 message("Ops, %s error! Please report to %s",
330 __func__, PACKAGE_BUGREPORT);
332 buffer->top_line = vl;
333 buffer->current_line = vl;
342 if ((l = minibuffer_metadata()) == NULL) {
343 message("No line selected");
356 if ((l = minibuffer_metadata()) == NULL) {
357 message("No line selected");
366 yornp_self_insert(void)
368 if (thiskey.key != 'y' && thiskey.key != 'n') {
369 message("Please answer y or n");
374 yornp_cb(thiskey.key == 'y', yornp_data);
381 yornp_cb(0, yornp_data);
385 read_self_insert(void)
387 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
388 global_key_unbound();
392 minibuffer_self_insert();
399 read_cb(NULL, read_data);
406 minibuffer_hist_save_entry();
407 read_cb(ministate.buf, read_data);
411 * TODO: we should collect this asynchronously...
414 populate_compl_buffer(complfn *fn, void *data)
416 const char *s, *descr;
422 b = &ministate.compl.buffer;
427 while ((s = fn(&data, &linedata, &descr)) != NULL) {
428 if ((l = calloc(1, sizeof(*l))) == NULL)
431 l->type = LINE_COMPL;
433 l->alt = (char*)descr;
434 if ((l->line = strdup(s)) == NULL)
437 TAILQ_INSERT_TAIL(&p->head, l, lines);
443 if ((l = TAILQ_FIRST(&p->head)) != NULL)
444 l->type = LINE_COMPL_CURRENT;
448 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
449 void (*abortfn)(void), struct histhead *hist,
450 complfn *complfn, void *compldata)
452 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
453 if (in_minibuffer == MB_COMPREAD) {
454 ui_schedule_redraw();
456 ministate.compl.fn = complfn;
457 ministate.compl.data = compldata;
458 populate_compl_buffer(complfn, compldata);
461 base_map = &minibuffer_map;
462 current_map = &minibuffer_map;
464 base_map->unhandled_input = self_insert_fn;
466 ministate.donefn = donefn;
467 ministate.abortfn = abortfn;
468 memset(ministate.buf, 0, sizeof(ministate.buf));
469 ministate.buffer.current_line = &ministate.vline;
470 ministate.buffer.current_line->line = ministate.buf;
471 ministate.buffer.cpoff = 0;
472 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
474 ministate.history = hist;
475 ministate.hist_cur = NULL;
476 ministate.hist_off = 0;
480 exit_minibuffer(void)
482 if (in_minibuffer == MB_COMPREAD) {
483 erase_buffer(&ministate.compl.buffer);
484 ui_schedule_redraw();
488 base_map = &global_map;
489 current_map = &global_map;
493 yornp(const char *prompt, void (*fn)(int, struct tab*),
505 enter_minibuffer(yornp_self_insert, yornp_self_insert,
506 yornp_abort, NULL, NULL, NULL);
508 len = sizeof(ministate.prompt);
509 strlcpy(ministate.prompt, prompt, len);
510 strlcat(ministate.prompt, " (y or n) ", len);
514 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
524 enter_minibuffer(read_self_insert, read_select, read_abort,
525 &read_history, NULL, NULL);
527 len = sizeof(ministate.prompt);
528 strlcpy(ministate.prompt, prompt, len);
529 strlcat(ministate.prompt, ": ", len);
533 handle_clear_echoarea(int fd, short ev, void *d)
535 free(ministate.curmesg);
536 ministate.curmesg = NULL;
538 ui_after_message_hook();
542 vmessage(const char *fmt, va_list ap)
544 if (evtimer_pending(&clechoev, NULL))
545 evtimer_del(&clechoev);
547 free(ministate.curmesg);
548 ministate.curmesg = NULL;
551 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
552 evtimer_add(&clechoev, &clechoev_timer);
554 /* TODO: what to do if the allocation fails here? */
555 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
556 ministate.curmesg = NULL;
559 ui_after_message_hook();
563 message(const char *fmt, ...)
573 minibuffer_init(void)
575 TAILQ_INIT(&eecmd_history.head);
576 TAILQ_INIT(&ir_history.head);
577 TAILQ_INIT(&lu_history.head);
578 TAILQ_INIT(&read_history.head);
580 TAILQ_INIT(&ministate.compl.buffer.head);
581 TAILQ_INIT(&ministate.compl.buffer.page.head);
583 ministate.line.type = LINE_TEXT;
584 ministate.vline.parent = &ministate.line;
585 ministate.buffer.page.name = "*minibuffer*";
586 ministate.buffer.current_line = &ministate.vline;
588 evtimer_set(&clechoev, handle_clear_echoarea, NULL);