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 1fce2e75 2021-08-14 op #include "session.h"
25 d1a0f2a3 2021-07-12 op #include "ui.h"
26 5caf7d67 2021-07-12 op #include "utf8.h"
28 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
29 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
30 84b88039 2021-07-12 op static void yornp_self_insert(void);
31 84b88039 2021-07-12 op static void yornp_abort(void);
32 84b88039 2021-07-12 op static void read_self_insert(void);
33 84b88039 2021-07-12 op static void read_abort(void);
34 84b88039 2021-07-12 op static void read_select(void);
35 4bc446b9 2021-07-21 op static void handle_clear_echoarea(int, short, void *);
37 4bc446b9 2021-07-21 op static struct event clechoev;
38 4bc446b9 2021-07-21 op static struct timeval clechoev_timer = { 5, 0 };
40 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
41 84b88039 2021-07-12 op static struct tab *yornp_data;
43 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
44 d1353324 2021-07-13 op static struct tab *read_data;
46 78894e73 2021-08-12 op /* XXX: don't forget to init these in minibuffer_init */
47 84b88039 2021-07-12 op struct histhead eecmd_history,
52 84b88039 2021-07-12 op struct ministate ministate;
54 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
56 54ee0a94 2021-07-21 op int in_minibuffer;
59 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
60 b1e1e41a 2021-07-14 op * ones already hidden.
63 b1e1e41a 2021-07-14 op recompute_completions(int add)
65 b1e1e41a 2021-07-14 op struct line *l;
66 e7b982f4 2021-07-14 op struct vline *vl;
67 e7b982f4 2021-07-14 op struct buffer *b;
69 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
72 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
73 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
74 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
75 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
77 b3be07ea 2021-07-18 op if (strcasestr(l->line, ministate.buf) != NULL ||
78 b3be07ea 2021-07-18 op (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
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;
83 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
84 5977965a 2021-07-15 op b->line_max--;
85 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
89 e7b982f4 2021-07-14 op if (b->current_line == NULL)
90 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
91 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
92 e7b982f4 2021-07-14 op vl = b->current_line;
93 e7b982f4 2021-07-14 op if (vl != NULL)
94 e7b982f4 2021-07-14 op vl->parent->type = LINE_COMPL_CURRENT;
98 16578ca5 2021-01-02 op minibuffer_insert_current_candidate(void)
100 16578ca5 2021-01-02 op struct vline *vl;
102 16578ca5 2021-01-02 op vl = ministate.compl.buffer.current_line;
103 16578ca5 2021-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
106 16578ca5 2021-01-02 op minibuffer_taint_hist();
107 16578ca5 2021-01-02 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
108 16578ca5 2021-01-02 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
113 d7ee7b5e 2021-07-15 op static void *
114 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
116 d7ee7b5e 2021-07-15 op struct vline *vl;
118 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
120 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
123 fbadd395 2021-07-16 op return vl->parent->data;
127 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
129 84b88039 2021-07-12 op struct hist *hist;
131 84b88039 2021-07-12 op if (ministate.history == NULL)
134 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
137 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
139 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
140 84b88039 2021-07-12 op ministate.history->len++;
144 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
145 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
146 84b88039 2021-07-12 op * navigation" thing.
149 84b88039 2021-07-12 op minibuffer_taint_hist(void)
151 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
154 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
155 84b88039 2021-07-12 op ministate.hist_cur = NULL;
156 19f7f20c 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
160 84b88039 2021-07-12 op minibuffer_self_insert(void)
162 84b88039 2021-07-12 op char *c, tmp[5] = {0};
165 84b88039 2021-07-12 op minibuffer_taint_hist();
167 84b88039 2021-07-12 op if (thiskey.cp == 0)
170 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
171 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
172 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
175 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
176 84b88039 2021-07-12 op memcpy(c, tmp, len);
177 84b88039 2021-07-12 op ministate.buffer.cpoff++;
179 b1e1e41a 2021-07-14 op recompute_completions(1);
183 40fbc354 2021-07-14 op sensible_self_insert(void)
185 e8c9de1e 2021-07-20 op if (thiskey.meta ||
186 85c820b8 2021-07-24 op (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
187 84b88039 2021-07-12 op global_key_unbound();
191 84b88039 2021-07-12 op minibuffer_self_insert();
195 84b88039 2021-07-12 op eecmd_select(void)
197 16578ca5 2021-01-02 op struct cmd *cmd;
198 16578ca5 2021-01-02 op struct vline *vl;
199 16578ca5 2021-01-02 op const char *t;
201 16578ca5 2021-01-02 op vl = ministate.compl.buffer.current_line;
202 16578ca5 2021-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
205 16578ca5 2021-01-02 op t = vl->parent->line;
206 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
207 16578ca5 2021-01-02 op if (!strcmp(cmd->cmd, t)) {
208 16578ca5 2021-01-02 op minibuffer_insert_current_candidate();
209 84b88039 2021-07-12 op exit_minibuffer();
210 84b88039 2021-07-12 op minibuffer_hist_save_entry();
211 84b88039 2021-07-12 op cmd->fn(current_buffer());
217 84b88039 2021-07-12 op message("No match");
221 9a28e7e5 2021-08-03 op ir_select_gemini(void)
223 84b88039 2021-07-12 op char buf[1025] = {0};
224 84b88039 2021-07-12 op struct phos_uri uri;
225 83dce83d 2021-07-17 op struct tab *tab = current_tab;
227 84b88039 2021-07-12 op exit_minibuffer();
228 84b88039 2021-07-12 op minibuffer_hist_save_entry();
230 84b88039 2021-07-12 op /* a bit ugly but... */
231 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
232 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
233 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
234 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
238 9a28e7e5 2021-08-03 op ir_select_gopher(void)
240 9a28e7e5 2021-08-03 op exit_minibuffer();
241 9a28e7e5 2021-08-03 op minibuffer_hist_save_entry();
243 9a28e7e5 2021-08-03 op gopher_send_search_req(current_tab, ministate.buf);
247 84b88039 2021-07-12 op lu_select(void)
249 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
251 84b88039 2021-07-12 op exit_minibuffer();
252 84b88039 2021-07-12 op minibuffer_hist_save_entry();
254 c6efff96 2021-08-16 op humanify_url(ministate.buf, url, sizeof(url));
255 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
259 84b88039 2021-07-12 op bp_select(void)
261 84b88039 2021-07-12 op exit_minibuffer();
262 84b88039 2021-07-12 op if (*ministate.buf != '\0')
263 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
265 84b88039 2021-07-12 op message("Abort.");
269 65601367 2021-07-14 op ts_select(void)
271 65601367 2021-07-14 op struct tab *tab;
273 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
274 65601367 2021-07-14 op message("No tab selected");
278 65601367 2021-07-14 op exit_minibuffer();
279 65601367 2021-07-14 op switch_to_tab(tab);
283 753c6ac7 2021-07-14 op ls_select(void)
285 753c6ac7 2021-07-14 op struct line *l;
287 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
288 753c6ac7 2021-07-14 op message("No link selected");
292 753c6ac7 2021-07-14 op exit_minibuffer();
293 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
296 d7ee7b5e 2021-07-15 op static inline void
297 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
299 753c6ac7 2021-07-14 op struct vline *vl;
300 5924d8d2 2021-07-21 op struct buffer *buffer;
302 5924d8d2 2021-07-21 op buffer = current_buffer();
304 5924d8d2 2021-07-21 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
305 753c6ac7 2021-07-14 op if (vl->parent == l)
309 753c6ac7 2021-07-14 op if (vl == NULL)
310 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
311 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
313 5924d8d2 2021-07-21 op buffer->top_line = vl;
314 5924d8d2 2021-07-21 op buffer->current_line = vl;
319 d7ee7b5e 2021-07-15 op swiper_select(void)
321 edd9a650 2021-07-15 op struct line *l;
323 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
324 edd9a650 2021-07-15 op message("No line selected");
328 edd9a650 2021-07-15 op exit_minibuffer();
329 edd9a650 2021-07-15 op jump_to_line(l);
333 edd9a650 2021-07-15 op toc_select(void)
335 d7ee7b5e 2021-07-15 op struct line *l;
337 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
338 d7ee7b5e 2021-07-15 op message("No line selected");
342 d7ee7b5e 2021-07-15 op exit_minibuffer();
343 d7ee7b5e 2021-07-15 op jump_to_line(l);
347 84b88039 2021-07-12 op yornp_self_insert(void)
349 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
350 84b88039 2021-07-12 op message("Please answer y or n");
354 84b88039 2021-07-12 op exit_minibuffer();
355 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
359 84b88039 2021-07-12 op yornp_abort(void)
361 84b88039 2021-07-12 op exit_minibuffer();
362 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
366 84b88039 2021-07-12 op read_self_insert(void)
368 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
369 84b88039 2021-07-12 op global_key_unbound();
373 84b88039 2021-07-12 op minibuffer_self_insert();
377 84b88039 2021-07-12 op read_abort(void)
379 84b88039 2021-07-12 op exit_minibuffer();
380 84b88039 2021-07-12 op read_cb(NULL, read_data);
384 84b88039 2021-07-12 op read_select(void)
386 95a8c791 2021-08-26 op exit_minibuffer();
387 84b88039 2021-07-12 op minibuffer_hist_save_entry();
388 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
392 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
394 b1e1e41a 2021-07-14 op static inline void
395 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
397 b3be07ea 2021-07-18 op const char *s, *descr;
398 b1e1e41a 2021-07-14 op struct line *l;
399 b1e1e41a 2021-07-14 op struct buffer *b;
400 b1e1e41a 2021-07-14 op struct parser *p;
401 0ce8aa3e 2021-07-18 op void *linedata;
403 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
404 b1e1e41a 2021-07-14 op p = &b->page;
406 0ce8aa3e 2021-07-18 op linedata = NULL;
407 0ce8aa3e 2021-07-18 op descr = NULL;
408 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
409 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
412 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
413 fbadd395 2021-07-16 op l->data = linedata;
414 b3be07ea 2021-07-18 op l->alt = (char*)descr;
415 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
418 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
420 65601367 2021-07-14 op linedata = NULL;
421 b3be07ea 2021-07-18 op descr = NULL;
424 e7b982f4 2021-07-14 op if ((l = TAILQ_FIRST(&p->head)) != NULL)
425 e7b982f4 2021-07-14 op l->type = LINE_COMPL_CURRENT;
429 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
430 e5a2797f 2021-07-13 op void (*abortfn)(void), struct histhead *hist,
431 e5a2797f 2021-07-13 op complfn *complfn, void *compldata)
433 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
434 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
435 b1e1e41a 2021-07-14 op ui_schedule_redraw();
437 b1e1e41a 2021-07-14 op ministate.compl.fn = complfn;
438 b1e1e41a 2021-07-14 op ministate.compl.data = compldata;
439 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn, compldata);
442 84b88039 2021-07-12 op base_map = &minibuffer_map;
443 84b88039 2021-07-12 op current_map = &minibuffer_map;
445 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
447 84b88039 2021-07-12 op ministate.donefn = donefn;
448 84b88039 2021-07-12 op ministate.abortfn = abortfn;
449 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
450 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
451 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
452 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
453 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
455 84b88039 2021-07-12 op ministate.history = hist;
456 84b88039 2021-07-12 op ministate.hist_cur = NULL;
457 84b88039 2021-07-12 op ministate.hist_off = 0;
461 84b88039 2021-07-12 op exit_minibuffer(void)
463 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
464 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
465 b1e1e41a 2021-07-14 op ui_schedule_redraw();
468 84b88039 2021-07-12 op in_minibuffer = 0;
469 84b88039 2021-07-12 op base_map = &global_map;
470 84b88039 2021-07-12 op current_map = &global_map;
474 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
475 84b88039 2021-07-12 op struct tab *data)
479 84b88039 2021-07-12 op if (in_minibuffer) {
484 84b88039 2021-07-12 op yornp_cb = fn;
485 84b88039 2021-07-12 op yornp_data = data;
486 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
487 e5a2797f 2021-07-13 op yornp_abort, NULL, NULL, NULL);
489 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
490 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
491 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
495 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
496 b1e1e41a 2021-07-14 op struct tab *data)
500 84b88039 2021-07-12 op if (in_minibuffer)
503 84b88039 2021-07-12 op read_cb = fn;
504 84b88039 2021-07-12 op read_data = data;
505 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
506 b1e1e41a 2021-07-14 op &read_history, NULL, NULL);
508 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
509 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
510 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);
514 4bc446b9 2021-07-21 op handle_clear_echoarea(int fd, short ev, void *d)
516 4bc446b9 2021-07-21 op free(ministate.curmesg);
517 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
519 4bc446b9 2021-07-21 op ui_after_message_hook();
523 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
525 4bc446b9 2021-07-21 op if (evtimer_pending(&clechoev, NULL))
526 4bc446b9 2021-07-21 op evtimer_del(&clechoev);
528 4bc446b9 2021-07-21 op free(ministate.curmesg);
529 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
531 4bc446b9 2021-07-21 op if (fmt != NULL) {
532 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);
533 4bc446b9 2021-07-21 op evtimer_add(&clechoev, &clechoev_timer);
535 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
536 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
537 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
540 4bc446b9 2021-07-21 op ui_after_message_hook();
544 4bc446b9 2021-07-21 op message(const char *fmt, ...)
548 4bc446b9 2021-07-21 op va_start(ap, fmt);
549 4bc446b9 2021-07-21 op vmessage(fmt, ap);
554 4bc446b9 2021-07-21 op minibuffer_init(void)
556 bccb5b0b 2021-07-21 op TAILQ_INIT(&eecmd_history.head);
557 bccb5b0b 2021-07-21 op TAILQ_INIT(&ir_history.head);
558 bccb5b0b 2021-07-21 op TAILQ_INIT(&lu_history.head);
559 78894e73 2021-08-12 op TAILQ_INIT(&read_history.head);
561 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
562 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.page.head);
564 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
565 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
566 ab0a4274 2021-07-25 op ministate.buffer.page.name = "*minibuffer*";
567 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;
569 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);