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"
27 static void *minibuffer_metadata(void);
28 static void minibuffer_hist_save_entry(void);
29 static void yornp_self_insert(void);
30 static void yornp_abort(void);
31 static void read_self_insert(void);
32 static void read_abort(void);
33 static void read_select(void);
34 static void handle_clear_echoarea(int, short, void *);
36 static struct event clechoev;
37 static struct timeval clechoev_timer = { 5, 0 };
39 static void (*yornp_cb)(int, struct tab *);
40 static struct tab *yornp_data;
42 static void (*read_cb)(const char*, struct tab *);
43 static struct tab *read_data;
45 struct histhead eecmd_history,
50 struct ministate ministate;
52 struct buffer minibufferwin;
57 * Recompute the visible completions. If add is 1, don't consider the
58 * ones already hidden.
61 recompute_completions(int add)
67 if (in_minibuffer != MB_COMPREAD)
70 b = &ministate.compl.buffer;
71 TAILQ_FOREACH(l, &b->page.head, lines) {
73 if (add && l->flags & L_HIDDEN)
75 if (strcasestr(l->line, ministate.buf) != NULL ||
76 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
77 if (l->flags & L_HIDDEN)
79 l->flags &= ~L_HIDDEN;
81 if (!(l->flags & L_HIDDEN))
87 if (b->current_line == NULL)
88 b->current_line = TAILQ_FIRST(&b->head);
89 b->current_line = adjust_line(b->current_line, b);
92 vl->parent->type = LINE_COMPL_CURRENT;
96 minibuffer_metadata(void)
100 vl = ministate.compl.buffer.current_line;
102 if (vl == NULL || vl->parent->flags & L_HIDDEN)
105 return vl->parent->data;
109 minibuffer_hist_save_entry(void)
113 if (ministate.history == NULL)
116 if ((hist = calloc(1, sizeof(*hist))) == NULL)
119 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
121 if (TAILQ_EMPTY(&ministate.history->head))
122 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
124 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
125 ministate.history->len++;
129 * taint the minibuffer cache: if we're currently showing a history
130 * element, copy that to the current buf and reset the "history
134 minibuffer_taint_hist(void)
136 if (ministate.hist_cur == NULL)
139 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
140 ministate.hist_cur = NULL;
141 ministate.buffer.current_line->line = ministate.buf;
145 minibuffer_self_insert(void)
147 char *c, tmp[5] = {0};
150 minibuffer_taint_hist();
155 len = utf8_encode(thiskey.cp, tmp);
156 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
157 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
160 memmove(c + len, c, strlen(c)+1);
162 ministate.buffer.cpoff++;
164 recompute_completions(1);
168 sensible_self_insert(void)
171 (unicode_isspace(thiskey.key) && thiskey.key != ' ')) {
172 global_key_unbound();
176 minibuffer_self_insert();
180 eecmd_self_insert(void)
182 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
183 !unicode_isgraph(thiskey.cp)) {
184 global_key_unbound();
188 minibuffer_self_insert();
196 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
197 if (!strcmp(cmd->cmd, ministate.buf)) {
199 minibuffer_hist_save_entry();
200 cmd->fn(current_buffer());
211 minibuffer_self_insert();
217 char buf[1025] = {0};
219 struct tab *tab = current_tab;
222 minibuffer_hist_save_entry();
224 /* a bit ugly but... */
225 memcpy(&uri, &tab->uri, sizeof(tab->uri));
226 phos_uri_set_query(&uri, ministate.buf);
227 phos_serialize_uri(&uri, buf, sizeof(buf));
228 load_url_in_tab(tab, buf, NULL);
235 minibuffer_hist_save_entry();
236 load_url_in_tab(current_tab, ministate.buf, NULL);
243 if (*ministate.buf != '\0')
244 add_to_bookmarks(ministate.buf);
254 if ((tab = minibuffer_metadata()) == NULL) {
255 message("No tab selected");
268 if ((l = minibuffer_metadata()) == NULL) {
269 message("No link selected");
274 load_url_in_tab(current_tab, l->alt, NULL);
278 jump_to_line(struct line *l)
281 struct buffer *buffer;
283 buffer = current_buffer();
285 TAILQ_FOREACH(vl, &buffer->head, vlines) {
291 message("Ops, %s error! Please report to %s",
292 __func__, PACKAGE_BUGREPORT);
294 buffer->top_line = vl;
295 buffer->current_line = vl;
304 if ((l = minibuffer_metadata()) == NULL) {
305 message("No line selected");
318 if ((l = minibuffer_metadata()) == NULL) {
319 message("No line selected");
328 yornp_self_insert(void)
330 if (thiskey.key != 'y' && thiskey.key != 'n') {
331 message("Please answer y or n");
336 yornp_cb(thiskey.key == 'y', yornp_data);
343 yornp_cb(0, yornp_data);
347 read_self_insert(void)
349 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
350 global_key_unbound();
354 minibuffer_self_insert();
361 read_cb(NULL, read_data);
368 minibuffer_hist_save_entry();
369 read_cb(ministate.buf, read_data);
373 * TODO: we should collect this asynchronously...
376 populate_compl_buffer(complfn *fn, void *data)
378 const char *s, *descr;
384 b = &ministate.compl.buffer;
389 while ((s = fn(&data, &linedata, &descr)) != NULL) {
390 if ((l = calloc(1, sizeof(*l))) == NULL)
393 l->type = LINE_COMPL;
395 l->alt = (char*)descr;
396 if ((l->line = strdup(s)) == NULL)
399 if (TAILQ_EMPTY(&p->head))
400 TAILQ_INSERT_HEAD(&p->head, l, lines);
402 TAILQ_INSERT_TAIL(&p->head, l, lines);
408 if ((l = TAILQ_FIRST(&p->head)) != NULL)
409 l->type = LINE_COMPL_CURRENT;
413 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
414 void (*abortfn)(void), struct histhead *hist,
415 complfn *complfn, void *compldata)
417 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
418 if (in_minibuffer == MB_COMPREAD) {
419 ui_schedule_redraw();
421 ministate.compl.fn = complfn;
422 ministate.compl.data = compldata;
423 populate_compl_buffer(complfn, compldata);
426 base_map = &minibuffer_map;
427 current_map = &minibuffer_map;
429 base_map->unhandled_input = self_insert_fn;
431 ministate.donefn = donefn;
432 ministate.abortfn = abortfn;
433 memset(ministate.buf, 0, sizeof(ministate.buf));
434 ministate.buffer.current_line = &ministate.vline;
435 ministate.buffer.current_line->line = ministate.buf;
436 ministate.buffer.cpoff = 0;
437 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
439 ministate.history = hist;
440 ministate.hist_cur = NULL;
441 ministate.hist_off = 0;
445 exit_minibuffer(void)
447 if (in_minibuffer == MB_COMPREAD) {
448 erase_buffer(&ministate.compl.buffer);
449 ui_schedule_redraw();
453 base_map = &global_map;
454 current_map = &global_map;
458 yornp(const char *prompt, void (*fn)(int, struct tab*),
470 enter_minibuffer(yornp_self_insert, yornp_self_insert,
471 yornp_abort, NULL, NULL, NULL);
473 len = sizeof(ministate.prompt);
474 strlcpy(ministate.prompt, prompt, len);
475 strlcat(ministate.prompt, " (y or n) ", len);
479 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
489 enter_minibuffer(read_self_insert, read_select, read_abort,
490 &read_history, NULL, NULL);
492 len = sizeof(ministate.prompt);
493 strlcpy(ministate.prompt, prompt, len);
494 strlcat(ministate.prompt, ": ", len);
498 handle_clear_echoarea(int fd, short ev, void *d)
500 free(ministate.curmesg);
501 ministate.curmesg = NULL;
503 ui_after_message_hook();
507 vmessage(const char *fmt, va_list ap)
509 if (evtimer_pending(&clechoev, NULL))
510 evtimer_del(&clechoev);
512 free(ministate.curmesg);
513 ministate.curmesg = NULL;
516 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
517 evtimer_add(&clechoev, &clechoev_timer);
519 /* TODO: what to do if the allocation fails here? */
520 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
521 ministate.curmesg = NULL;
524 ui_after_message_hook();
528 message(const char *fmt, ...)
538 minibuffer_init(void)
540 TAILQ_INIT(&eecmd_history.head);
541 TAILQ_INIT(&ir_history.head);
542 TAILQ_INIT(&lu_history.head);
544 ministate.line.type = LINE_TEXT;
545 ministate.vline.parent = &ministate.line;
546 ministate.buffer.current_line = &ministate.vline;
548 evtimer_set(&clechoev, handle_clear_echoarea, NULL);