2 84b88039 2021-07-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 84b88039 2021-07-12 op * Permission to use, copy, modify, and distribute this software for any
5 84b88039 2021-07-12 op * purpose with or without fee is hereby granted, provided that the above
6 84b88039 2021-07-12 op * copyright notice and this permission notice appear in all copies.
8 84b88039 2021-07-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 84b88039 2021-07-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 84b88039 2021-07-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 84b88039 2021-07-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 84b88039 2021-07-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 84b88039 2021-07-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 84b88039 2021-07-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4bc446b9 2021-07-21 op #include "compat.h"
19 4bc446b9 2021-07-21 op #include <stdio.h>
20 84b88039 2021-07-12 op #include <stdlib.h>
21 84b88039 2021-07-12 op #include <string.h>
23 450a89f7 2021-07-12 op #include "minibuffer.h"
24 d1a0f2a3 2021-07-12 op #include "ui.h"
25 5caf7d67 2021-07-12 op #include "utf8.h"
27 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
28 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
29 84b88039 2021-07-12 op static void yornp_self_insert(void);
30 84b88039 2021-07-12 op static void yornp_abort(void);
31 84b88039 2021-07-12 op static void read_self_insert(void);
32 84b88039 2021-07-12 op static void read_abort(void);
33 84b88039 2021-07-12 op static void read_select(void);
34 4bc446b9 2021-07-21 op static void handle_clear_echoarea(int, short, void *);
36 4bc446b9 2021-07-21 op static struct event clechoev;
37 4bc446b9 2021-07-21 op static struct timeval clechoev_timer = { 5, 0 };
39 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
40 84b88039 2021-07-12 op static struct tab *yornp_data;
42 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
43 d1353324 2021-07-13 op static struct tab *read_data;
45 84b88039 2021-07-12 op struct histhead eecmd_history,
50 84b88039 2021-07-12 op struct ministate ministate;
52 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
55 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
56 b1e1e41a 2021-07-14 op * ones already hidden.
59 b1e1e41a 2021-07-14 op recompute_completions(int add)
61 b1e1e41a 2021-07-14 op struct line *l;
62 e7b982f4 2021-07-14 op struct vline *vl;
63 e7b982f4 2021-07-14 op struct buffer *b;
65 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
68 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
69 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
70 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
71 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
73 b3be07ea 2021-07-18 op if (strcasestr(l->line, ministate.buf) != NULL ||
74 b3be07ea 2021-07-18 op (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
75 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
76 5977965a 2021-07-15 op b->line_max++;
77 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
79 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
80 5977965a 2021-07-15 op b->line_max--;
81 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
85 e7b982f4 2021-07-14 op if (b->current_line == NULL)
86 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
87 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
88 e7b982f4 2021-07-14 op vl = b->current_line;
89 e7b982f4 2021-07-14 op if (vl != NULL)
90 e7b982f4 2021-07-14 op vl->parent->type = LINE_COMPL_CURRENT;
94 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
96 d7ee7b5e 2021-07-15 op struct vline *vl;
98 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
100 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
103 fbadd395 2021-07-16 op return vl->parent->data;
107 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
109 84b88039 2021-07-12 op struct hist *hist;
111 84b88039 2021-07-12 op if (ministate.history == NULL)
114 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
117 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
119 84b88039 2021-07-12 op if (TAILQ_EMPTY(&ministate.history->head))
120 84b88039 2021-07-12 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
122 84b88039 2021-07-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
123 84b88039 2021-07-12 op ministate.history->len++;
127 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
128 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
129 84b88039 2021-07-12 op * navigation" thing.
132 84b88039 2021-07-12 op minibuffer_taint_hist(void)
134 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
137 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
138 84b88039 2021-07-12 op ministate.hist_cur = NULL;
139 19f7f20c 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
143 84b88039 2021-07-12 op minibuffer_self_insert(void)
145 84b88039 2021-07-12 op char *c, tmp[5] = {0};
148 84b88039 2021-07-12 op minibuffer_taint_hist();
150 84b88039 2021-07-12 op if (thiskey.cp == 0)
153 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
154 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
155 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
158 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
159 84b88039 2021-07-12 op memcpy(c, tmp, len);
160 84b88039 2021-07-12 op ministate.buffer.cpoff++;
162 b1e1e41a 2021-07-14 op recompute_completions(1);
166 40fbc354 2021-07-14 op sensible_self_insert(void)
168 e8c9de1e 2021-07-20 op if (thiskey.meta ||
169 e8c9de1e 2021-07-20 op (unicode_isspace(thiskey.key) && thiskey.key != ' ')) {
170 40fbc354 2021-07-14 op global_key_unbound();
174 40fbc354 2021-07-14 op minibuffer_self_insert();
178 84b88039 2021-07-12 op eecmd_self_insert(void)
180 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
181 84b88039 2021-07-12 op !unicode_isgraph(thiskey.cp)) {
182 84b88039 2021-07-12 op global_key_unbound();
186 84b88039 2021-07-12 op minibuffer_self_insert();
190 84b88039 2021-07-12 op eecmd_select(void)
192 84b88039 2021-07-12 op struct cmd *cmd;
194 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
195 84b88039 2021-07-12 op if (!strcmp(cmd->cmd, ministate.buf)) {
196 84b88039 2021-07-12 op exit_minibuffer();
197 84b88039 2021-07-12 op minibuffer_hist_save_entry();
198 84b88039 2021-07-12 op cmd->fn(current_buffer());
203 84b88039 2021-07-12 op message("No match");
207 84b88039 2021-07-12 op ir_self_insert(void)
209 84b88039 2021-07-12 op minibuffer_self_insert();
213 84b88039 2021-07-12 op ir_select(void)
215 84b88039 2021-07-12 op char buf[1025] = {0};
216 84b88039 2021-07-12 op struct phos_uri uri;
217 83dce83d 2021-07-17 op struct tab *tab = current_tab;
219 84b88039 2021-07-12 op exit_minibuffer();
220 84b88039 2021-07-12 op minibuffer_hist_save_entry();
222 84b88039 2021-07-12 op /* a bit ugly but... */
223 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
224 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
225 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
226 bd3a3a95 2021-07-20 op load_url_in_tab(tab, buf, NULL);
230 84b88039 2021-07-12 op lu_select(void)
232 84b88039 2021-07-12 op exit_minibuffer();
233 84b88039 2021-07-12 op minibuffer_hist_save_entry();
234 bd3a3a95 2021-07-20 op load_url_in_tab(current_tab, ministate.buf, NULL);
238 84b88039 2021-07-12 op bp_select(void)
240 84b88039 2021-07-12 op exit_minibuffer();
241 84b88039 2021-07-12 op if (*ministate.buf != '\0')
242 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
244 84b88039 2021-07-12 op message("Abort.");
248 65601367 2021-07-14 op ts_select(void)
250 65601367 2021-07-14 op struct tab *tab;
252 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
253 65601367 2021-07-14 op message("No tab selected");
257 65601367 2021-07-14 op exit_minibuffer();
258 65601367 2021-07-14 op switch_to_tab(tab);
262 753c6ac7 2021-07-14 op ls_select(void)
264 753c6ac7 2021-07-14 op struct line *l;
266 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
267 753c6ac7 2021-07-14 op message("No link selected");
271 753c6ac7 2021-07-14 op exit_minibuffer();
272 bd3a3a95 2021-07-20 op load_url_in_tab(current_tab, l->alt, NULL);
275 d7ee7b5e 2021-07-15 op static inline void
276 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
278 753c6ac7 2021-07-14 op struct vline *vl;
279 83dce83d 2021-07-17 op struct tab *tab = current_tab;
281 753c6ac7 2021-07-14 op TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
282 753c6ac7 2021-07-14 op if (vl->parent == l)
286 753c6ac7 2021-07-14 op if (vl == NULL)
287 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
288 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
290 0b442ded 2021-07-16 op tab->buffer.top_line = vl;
291 753c6ac7 2021-07-14 op tab->buffer.current_line = vl;
296 d7ee7b5e 2021-07-15 op swiper_select(void)
298 edd9a650 2021-07-15 op struct line *l;
300 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
301 edd9a650 2021-07-15 op message("No line selected");
305 edd9a650 2021-07-15 op exit_minibuffer();
306 edd9a650 2021-07-15 op jump_to_line(l);
310 edd9a650 2021-07-15 op toc_select(void)
312 d7ee7b5e 2021-07-15 op struct line *l;
314 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
315 d7ee7b5e 2021-07-15 op message("No line selected");
319 d7ee7b5e 2021-07-15 op exit_minibuffer();
320 d7ee7b5e 2021-07-15 op jump_to_line(l);
324 84b88039 2021-07-12 op yornp_self_insert(void)
326 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
327 84b88039 2021-07-12 op message("Please answer y or n");
331 84b88039 2021-07-12 op exit_minibuffer();
332 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
336 84b88039 2021-07-12 op yornp_abort(void)
338 84b88039 2021-07-12 op exit_minibuffer();
339 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
343 84b88039 2021-07-12 op read_self_insert(void)
345 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
346 84b88039 2021-07-12 op global_key_unbound();
350 84b88039 2021-07-12 op minibuffer_self_insert();
354 84b88039 2021-07-12 op read_abort(void)
356 84b88039 2021-07-12 op exit_minibuffer();
357 84b88039 2021-07-12 op read_cb(NULL, read_data);
361 84b88039 2021-07-12 op read_select(void)
363 84b88039 2021-07-12 op exit_minibuffer();
364 84b88039 2021-07-12 op minibuffer_hist_save_entry();
365 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
369 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
371 b1e1e41a 2021-07-14 op static inline void
372 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
374 b3be07ea 2021-07-18 op const char *s, *descr;
375 b1e1e41a 2021-07-14 op struct line *l;
376 b1e1e41a 2021-07-14 op struct buffer *b;
377 b1e1e41a 2021-07-14 op struct parser *p;
378 0ce8aa3e 2021-07-18 op void *linedata;
380 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
381 b1e1e41a 2021-07-14 op p = &b->page;
383 0ce8aa3e 2021-07-18 op linedata = NULL;
384 0ce8aa3e 2021-07-18 op descr = NULL;
385 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
386 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
389 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
390 fbadd395 2021-07-16 op l->data = linedata;
391 b3be07ea 2021-07-18 op l->alt = (char*)descr;
392 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
395 b1e1e41a 2021-07-14 op if (TAILQ_EMPTY(&p->head))
396 b1e1e41a 2021-07-14 op TAILQ_INSERT_HEAD(&p->head, l, lines);
398 b1e1e41a 2021-07-14 op TAILQ_INSERT_TAIL(&p->head, l, lines);
400 65601367 2021-07-14 op linedata = NULL;
401 b3be07ea 2021-07-18 op descr = NULL;
404 e7b982f4 2021-07-14 op if ((l = TAILQ_FIRST(&p->head)) != NULL)
405 e7b982f4 2021-07-14 op l->type = LINE_COMPL_CURRENT;
409 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
410 e5a2797f 2021-07-13 op void (*abortfn)(void), struct histhead *hist,
411 e5a2797f 2021-07-13 op complfn *complfn, void *compldata)
413 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
414 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
415 b1e1e41a 2021-07-14 op ui_schedule_redraw();
417 b1e1e41a 2021-07-14 op ministate.compl.fn = complfn;
418 b1e1e41a 2021-07-14 op ministate.compl.data = compldata;
419 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn, compldata);
422 84b88039 2021-07-12 op base_map = &minibuffer_map;
423 84b88039 2021-07-12 op current_map = &minibuffer_map;
425 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
427 84b88039 2021-07-12 op ministate.donefn = donefn;
428 84b88039 2021-07-12 op ministate.abortfn = abortfn;
429 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
430 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
431 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
432 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
433 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
435 84b88039 2021-07-12 op ministate.history = hist;
436 84b88039 2021-07-12 op ministate.hist_cur = NULL;
437 84b88039 2021-07-12 op ministate.hist_off = 0;
441 84b88039 2021-07-12 op exit_minibuffer(void)
443 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
444 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
445 b1e1e41a 2021-07-14 op ui_schedule_redraw();
448 84b88039 2021-07-12 op in_minibuffer = 0;
449 84b88039 2021-07-12 op base_map = &global_map;
450 84b88039 2021-07-12 op current_map = &global_map;
454 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
455 84b88039 2021-07-12 op struct tab *data)
459 84b88039 2021-07-12 op if (in_minibuffer) {
464 84b88039 2021-07-12 op yornp_cb = fn;
465 84b88039 2021-07-12 op yornp_data = data;
466 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
467 e5a2797f 2021-07-13 op yornp_abort, NULL, NULL, NULL);
469 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
470 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
471 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
475 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
476 b1e1e41a 2021-07-14 op struct tab *data)
480 84b88039 2021-07-12 op if (in_minibuffer)
483 84b88039 2021-07-12 op read_cb = fn;
484 84b88039 2021-07-12 op read_data = data;
485 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
486 b1e1e41a 2021-07-14 op &read_history, NULL, NULL);
488 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
489 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
490 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);
494 4bc446b9 2021-07-21 op handle_clear_echoarea(int fd, short ev, void *d)
496 4bc446b9 2021-07-21 op free(ministate.curmesg);
497 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
499 4bc446b9 2021-07-21 op ui_after_message_hook();
503 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
505 4bc446b9 2021-07-21 op if (evtimer_pending(&clechoev, NULL))
506 4bc446b9 2021-07-21 op evtimer_del(&clechoev);
508 4bc446b9 2021-07-21 op free(ministate.curmesg);
509 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
511 4bc446b9 2021-07-21 op if (fmt != NULL) {
512 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);
513 4bc446b9 2021-07-21 op evtimer_add(&clechoev, &clechoev_timer);
515 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
516 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
517 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
520 4bc446b9 2021-07-21 op ui_after_message_hook();
524 4bc446b9 2021-07-21 op message(const char *fmt, ...)
528 4bc446b9 2021-07-21 op va_start(ap, fmt);
529 4bc446b9 2021-07-21 op vmessage(fmt, ap);
534 4bc446b9 2021-07-21 op minibuffer_init(void)
536 bccb5b0b 2021-07-21 op TAILQ_INIT(&eecmd_history.head);
537 bccb5b0b 2021-07-21 op TAILQ_INIT(&ir_history.head);
538 bccb5b0b 2021-07-21 op TAILQ_INIT(&lu_history.head);
540 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
541 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
542 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;
544 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);