2 4bb6a4fa 2024-01-15 op * Copyright (c) 2021, 2024 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 f63b8f73 2022-04-24 op #include <errno.h>
20 f63b8f73 2022-04-24 op #include <limits.h>
21 4bc446b9 2021-07-21 op #include <stdio.h>
22 84b88039 2021-07-12 op #include <stdlib.h>
23 84b88039 2021-07-12 op #include <string.h>
25 5a39f593 2024-02-05 op #include "certs.h"
26 cbe2da32 2024-02-06 op #include "cmd.h"
27 fd6c540b 2024-02-12 op #include "defaults.h"
28 f63b8f73 2022-04-24 op #include "fs.h"
29 65c49665 2024-01-23 op #include "hist.h"
30 36f94f06 2022-12-23 op #include "iri.h"
31 f7db6d13 2024-02-06 op #include "keymap.h"
32 450a89f7 2021-07-12 op #include "minibuffer.h"
33 1fce2e75 2021-08-14 op #include "session.h"
34 d1a0f2a3 2021-07-12 op #include "ui.h"
35 5caf7d67 2021-07-12 op #include "utf8.h"
36 9d65b1d9 2022-01-11 op #include "utils.h"
38 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
40 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
41 7bd3a14b 2022-04-13 op static const char *minibuffer_compl_text(void);
42 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
43 84b88039 2021-07-12 op static void yornp_self_insert(void);
44 84b88039 2021-07-12 op static void yornp_abort(void);
45 84b88039 2021-07-12 op static void read_self_insert(void);
46 84b88039 2021-07-12 op static void read_abort(void);
47 84b88039 2021-07-12 op static void read_select(void);
48 4bc446b9 2021-07-21 op static void handle_clear_echoarea(int, short, void *);
50 4bc446b9 2021-07-21 op static struct event clechoev;
51 4bc446b9 2021-07-21 op static struct timeval clechoev_timer = { 5, 0 };
53 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
54 84b88039 2021-07-12 op static struct tab *yornp_data;
56 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
57 d1353324 2021-07-13 op static struct tab *read_data;
59 65c49665 2024-01-23 op struct hist *eecmd_history;
60 65c49665 2024-01-23 op struct hist *ir_history;
61 65c49665 2024-01-23 op struct hist *lu_history;
62 65c49665 2024-01-23 op struct hist *read_history;
64 84b88039 2021-07-12 op struct ministate ministate;
66 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
68 54ee0a94 2021-07-21 op int in_minibuffer;
70 eefb3de5 2022-02-26 op static inline int
71 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
76 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
79 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
81 eefb3de5 2022-02-26 op if (l->alt != NULL &&
82 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
85 eefb3de5 2022-02-26 op if (!lm && !am)
93 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
94 b1e1e41a 2021-07-14 op * ones already hidden.
97 b1e1e41a 2021-07-14 op recompute_completions(int add)
99 eefb3de5 2022-02-26 op static char buf[GEMINI_URL_LEN];
100 1faa6821 2022-04-15 op const char *text;
101 eefb3de5 2022-02-26 op char *input, **ap, *words[10];
102 eefb3de5 2022-02-26 op size_t len = 0;
103 b1e1e41a 2021-07-14 op struct line *l;
104 7c1d55bf 2022-04-13 op struct vline *vl;
105 e7b982f4 2021-07-14 op struct buffer *b;
107 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
110 65c49665 2024-01-23 op if (!ministate.editing)
111 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
113 1faa6821 2022-04-15 op text = ministate.buf;
115 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
118 eefb3de5 2022-02-26 op /* tokenize the input */
119 eefb3de5 2022-02-26 op for (ap = words; ap < words + nitems(words) &&
120 eefb3de5 2022-02-26 op (*ap = strsep(&input, " ")) != NULL;) {
121 eefb3de5 2022-02-26 op if (**ap != '\0')
125 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
126 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
127 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
128 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
130 eefb3de5 2022-02-26 op if (matches(words, len, l)) {
131 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
132 5977965a 2021-07-15 op b->line_max++;
133 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
135 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
136 5977965a 2021-07-15 op b->line_max--;
137 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
141 e7b982f4 2021-07-14 op if (b->current_line == NULL)
142 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
143 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
144 7c1d55bf 2022-04-13 op vl = b->current_line;
145 4bb17137 2022-04-14 op if (ministate.compl.must_select && vl != NULL)
146 7c1d55bf 2022-04-13 op vl->parent->type = LINE_COMPL_CURRENT;
150 16578ca5 2021-01-02 op minibuffer_insert_current_candidate(void)
152 16578ca5 2021-01-02 op struct vline *vl;
154 16578ca5 2021-01-02 op vl = ministate.compl.buffer.current_line;
155 16578ca5 2021-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
158 16578ca5 2021-01-02 op minibuffer_taint_hist();
159 16578ca5 2021-01-02 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
160 16578ca5 2021-01-02 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
165 d7ee7b5e 2021-07-15 op static void *
166 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
168 d7ee7b5e 2021-07-15 op struct vline *vl;
170 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
172 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
175 fbadd395 2021-07-16 op return vl->parent->data;
178 7bd3a14b 2022-04-13 op static const char *
179 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
181 7bd3a14b 2022-04-13 op struct vline *vl;
183 65c49665 2024-01-23 op if (!ministate.editing)
184 65c49665 2024-01-23 op return hist_cur(ministate.hist);
186 7bd3a14b 2022-04-13 op vl = ministate.compl.buffer.current_line;
187 7bd3a14b 2022-04-13 op if (vl == NULL || vl->parent->flags & L_HIDDEN ||
188 7bd3a14b 2022-04-13 op vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
189 7bd3a14b 2022-04-13 op return ministate.buf;
190 7bd3a14b 2022-04-13 op return vl->parent->line;
194 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
196 65c49665 2024-01-23 op if (ministate.hist == NULL)
199 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
203 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
204 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
205 84b88039 2021-07-12 op * navigation" thing.
208 84b88039 2021-07-12 op minibuffer_taint_hist(void)
210 65c49665 2024-01-23 op if (ministate.editing)
213 65c49665 2024-01-23 op ministate.editing = 1;
214 65c49665 2024-01-23 op strlcpy(ministate.buf, hist_cur(ministate.hist),
215 65c49665 2024-01-23 op sizeof(ministate.buf));
216 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
220 84b88039 2021-07-12 op minibuffer_self_insert(void)
222 84b88039 2021-07-12 op char *c, tmp[5] = {0};
225 84b88039 2021-07-12 op minibuffer_taint_hist();
227 84b88039 2021-07-12 op if (thiskey.cp == 0)
230 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
231 bd4a08a7 2022-11-07 op c = utf8_nth(ministate.buffer.current_line->parent->line,
232 bd4a08a7 2022-11-07 op ministate.buffer.cpoff);
233 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
236 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
237 84b88039 2021-07-12 op memcpy(c, tmp, len);
238 84b88039 2021-07-12 op ministate.buffer.cpoff++;
240 b1e1e41a 2021-07-14 op recompute_completions(1);
244 40fbc354 2021-07-14 op sensible_self_insert(void)
246 e8c9de1e 2021-07-20 op if (thiskey.meta ||
247 85c820b8 2021-07-24 op (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
248 84b88039 2021-07-12 op global_key_unbound();
252 84b88039 2021-07-12 op minibuffer_self_insert();
256 84b88039 2021-07-12 op eecmd_select(void)
258 16578ca5 2021-01-02 op struct cmd *cmd;
259 16578ca5 2021-01-02 op const char *t;
261 7bd3a14b 2022-04-13 op t = minibuffer_compl_text();
262 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
263 16578ca5 2021-01-02 op if (!strcmp(cmd->cmd, t)) {
264 84b88039 2021-07-12 op minibuffer_hist_save_entry();
265 7bd3a14b 2022-04-13 op exit_minibuffer();
266 84b88039 2021-07-12 op cmd->fn(current_buffer());
271 84b88039 2021-07-12 op message("No match");
275 9a28e7e5 2021-08-03 op ir_select_gemini(void)
277 36f94f06 2022-12-23 op static struct iri iri;
278 36f94f06 2022-12-23 op char buf[1025];
279 83dce83d 2021-07-17 op struct tab *tab = current_tab;
281 84b88039 2021-07-12 op minibuffer_hist_save_entry();
283 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
285 36f94f06 2022-12-23 op if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
287 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
290 7bd3a14b 2022-04-13 op exit_minibuffer();
291 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
295 36f94f06 2022-12-23 op message("Failed to select URL.");
299 ed504b9e 2022-02-07 op ir_select_reply(void)
301 cc71f6cb 2024-01-16 op static struct iri iri;
302 ed504b9e 2022-02-07 op char buf[1025] = {0};
303 ed504b9e 2022-02-07 op struct tab *tab = current_tab;
305 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
307 ed504b9e 2022-02-07 op /* a bit ugly but... */
308 cc71f6cb 2024-01-16 op iri_parse(NULL, tab->last_input_url, &iri);
309 cc71f6cb 2024-01-16 op iri_setquery(&iri, minibuffer_compl_text());
310 cc71f6cb 2024-01-16 op iri_unparse(&iri, buf, sizeof(buf));
312 7bd3a14b 2022-04-13 op exit_minibuffer();
313 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
317 9a28e7e5 2021-08-03 op ir_select_gopher(void)
319 9a28e7e5 2021-08-03 op minibuffer_hist_save_entry();
320 7bd3a14b 2022-04-13 op gopher_send_search_req(current_tab, minibuffer_compl_text());
321 7bd3a14b 2022-04-13 op exit_minibuffer();
325 84b88039 2021-07-12 op lu_select(void)
327 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
329 84b88039 2021-07-12 op minibuffer_hist_save_entry();
330 65c49665 2024-01-23 op humanify_url(minibuffer_compl_text(), hist_cur(current_tab->hist),
331 449ea6fe 2024-01-16 op url, sizeof(url));
333 7bd3a14b 2022-04-13 op exit_minibuffer();
334 449ea6fe 2024-01-16 op load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
338 84b88039 2021-07-12 op bp_select(void)
340 d2e2bebc 2024-01-23 op const char *url;
342 d2e2bebc 2024-01-23 op url = minibuffer_compl_text();
343 d2e2bebc 2024-01-23 op if (*url != '\0') {
344 d2e2bebc 2024-01-23 op if (bookmark_page(url) == -1)
345 f63b8f73 2022-04-24 op message("failed to bookmark page: %s",
346 f63b8f73 2022-04-24 op strerror(errno));
348 d2e2bebc 2024-01-23 op message("Bookmarked");
350 84b88039 2021-07-12 op message("Abort.");
351 d2e2bebc 2024-01-23 op exit_minibuffer();
355 65601367 2021-07-14 op ts_select(void)
357 65601367 2021-07-14 op struct tab *tab;
359 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
360 65601367 2021-07-14 op message("No tab selected");
364 65601367 2021-07-14 op exit_minibuffer();
365 65601367 2021-07-14 op switch_to_tab(tab);
369 753c6ac7 2021-07-14 op ls_select(void)
371 753c6ac7 2021-07-14 op struct line *l;
373 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
374 753c6ac7 2021-07-14 op message("No link selected");
378 753c6ac7 2021-07-14 op exit_minibuffer();
379 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
382 d7ee7b5e 2021-07-15 op static inline void
383 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
385 753c6ac7 2021-07-14 op struct vline *vl;
386 5924d8d2 2021-07-21 op struct buffer *buffer;
388 5924d8d2 2021-07-21 op buffer = current_buffer();
390 5924d8d2 2021-07-21 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
391 753c6ac7 2021-07-14 op if (vl->parent == l)
395 753c6ac7 2021-07-14 op if (vl == NULL)
396 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
397 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
399 5924d8d2 2021-07-21 op buffer->top_line = vl;
400 5924d8d2 2021-07-21 op buffer->current_line = vl;
405 d7ee7b5e 2021-07-15 op swiper_select(void)
407 edd9a650 2021-07-15 op struct line *l;
409 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
410 edd9a650 2021-07-15 op message("No line selected");
414 edd9a650 2021-07-15 op exit_minibuffer();
415 edd9a650 2021-07-15 op jump_to_line(l);
419 edd9a650 2021-07-15 op toc_select(void)
421 d7ee7b5e 2021-07-15 op struct line *l;
423 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
424 d7ee7b5e 2021-07-15 op message("No line selected");
428 d7ee7b5e 2021-07-15 op exit_minibuffer();
429 d7ee7b5e 2021-07-15 op jump_to_line(l);
433 5a39f593 2024-02-05 op save_cert_for_site_cb(int r, struct tab *tab)
435 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
439 5a39f593 2024-02-05 op uc_select(void)
441 5a39f593 2024-02-05 op const char *name;
443 5a39f593 2024-02-05 op name = minibuffer_compl_text();
444 5a39f593 2024-02-05 op if ((current_tab->client_cert = ccert(name)) == NULL) {
445 5a39f593 2024-02-05 op message("Certificate %s not found", name);
449 5a39f593 2024-02-05 op exit_minibuffer();
451 c171304b 2024-02-05 op yornp("Remember for future sessions too?", save_cert_for_site_cb,
452 5a39f593 2024-02-05 op current_tab);
456 fd6c540b 2024-02-12 op search_select(void)
458 fd6c540b 2024-02-12 op static struct iri iri;
459 fd6c540b 2024-02-12 op static char buf[1025];
461 fd6c540b 2024-02-12 op /* a bit ugly but... */
462 4e74baf8 2024-02-12 op if (iri_parse(NULL, default_search_engine, &iri) == -1) {
463 07a751ac 2024-02-12 op message("default-search-engine is a malformed IRI.");
464 4e74baf8 2024-02-12 op exit_minibuffer();
467 fd6c540b 2024-02-12 op iri_setquery(&iri, minibuffer_compl_text());
468 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
470 fd6c540b 2024-02-12 op exit_minibuffer();
471 fd6c540b 2024-02-12 op load_url_in_tab(current_tab, buf, NULL, LU_MODE_NOCACHE);
475 84b88039 2021-07-12 op yornp_self_insert(void)
477 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
478 84b88039 2021-07-12 op message("Please answer y or n");
482 84b88039 2021-07-12 op exit_minibuffer();
483 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
487 84b88039 2021-07-12 op yornp_abort(void)
489 84b88039 2021-07-12 op exit_minibuffer();
490 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
494 84b88039 2021-07-12 op read_self_insert(void)
496 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
497 84b88039 2021-07-12 op global_key_unbound();
501 84b88039 2021-07-12 op minibuffer_self_insert();
505 84b88039 2021-07-12 op read_abort(void)
507 84b88039 2021-07-12 op exit_minibuffer();
508 84b88039 2021-07-12 op read_cb(NULL, read_data);
512 84b88039 2021-07-12 op read_select(void)
514 95a8c791 2021-08-26 op exit_minibuffer();
515 84b88039 2021-07-12 op minibuffer_hist_save_entry();
516 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
520 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
522 b1e1e41a 2021-07-14 op static inline void
523 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
525 b3be07ea 2021-07-18 op const char *s, *descr;
526 b1e1e41a 2021-07-14 op struct line *l;
527 b1e1e41a 2021-07-14 op struct buffer *b;
528 b1e1e41a 2021-07-14 op struct parser *p;
529 0ce8aa3e 2021-07-18 op void *linedata;
531 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
532 b1e1e41a 2021-07-14 op p = &b->page;
534 0ce8aa3e 2021-07-18 op linedata = NULL;
535 0ce8aa3e 2021-07-18 op descr = NULL;
536 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
537 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
540 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
541 fbadd395 2021-07-16 op l->data = linedata;
542 b3be07ea 2021-07-18 op l->alt = (char*)descr;
543 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
546 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
548 65601367 2021-07-14 op linedata = NULL;
549 b3be07ea 2021-07-18 op descr = NULL;
552 7c1d55bf 2022-04-13 op if ((l = TAILQ_FIRST(&p->head)) != NULL &&
553 7c1d55bf 2022-04-13 op ministate.compl.must_select)
554 7c1d55bf 2022-04-13 op l->type = LINE_COMPL_CURRENT;
558 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
559 65c49665 2024-01-23 op void (*abortfn)(void), struct hist *hist,
560 27dbcaab 2022-04-13 op complfn *complfn, void *compldata, int must_select)
562 8115fd4a 2022-05-05 op ministate.compl.must_select = must_select;
563 8115fd4a 2022-05-05 op ministate.compl.fn = complfn;
564 8115fd4a 2022-05-05 op ministate.compl.data = compldata;
566 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
567 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
568 8115fd4a 2022-05-05 op populate_compl_buffer(complfn, compldata);
569 b1e1e41a 2021-07-14 op ui_schedule_redraw();
572 84b88039 2021-07-12 op base_map = &minibuffer_map;
573 84b88039 2021-07-12 op current_map = &minibuffer_map;
575 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
577 84b88039 2021-07-12 op ministate.donefn = donefn;
578 84b88039 2021-07-12 op ministate.abortfn = abortfn;
579 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
580 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
581 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
582 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
583 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
585 65c49665 2024-01-23 op ministate.editing = 1;
586 65c49665 2024-01-23 op ministate.hist = hist;
587 65c49665 2024-01-23 op if (ministate.hist)
588 65c49665 2024-01-23 op hist_seek_start(ministate.hist);
592 84b88039 2021-07-12 op exit_minibuffer(void)
594 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
595 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
596 b1e1e41a 2021-07-14 op ui_schedule_redraw();
599 84b88039 2021-07-12 op in_minibuffer = 0;
600 84b88039 2021-07-12 op base_map = &global_map;
601 84b88039 2021-07-12 op current_map = &global_map;
605 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
606 84b88039 2021-07-12 op struct tab *data)
610 84b88039 2021-07-12 op if (in_minibuffer) {
615 84b88039 2021-07-12 op yornp_cb = fn;
616 84b88039 2021-07-12 op yornp_data = data;
617 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
618 27dbcaab 2022-04-13 op yornp_abort, NULL, NULL, NULL, 0);
620 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
621 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
622 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
626 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
627 b1e1e41a 2021-07-14 op struct tab *data)
631 84b88039 2021-07-12 op if (in_minibuffer)
634 84b88039 2021-07-12 op read_cb = fn;
635 84b88039 2021-07-12 op read_data = data;
636 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
637 65c49665 2024-01-23 op read_history, NULL, NULL, 0);
639 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
640 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
641 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);
645 4bc446b9 2021-07-21 op handle_clear_echoarea(int fd, short ev, void *d)
647 4bc446b9 2021-07-21 op free(ministate.curmesg);
648 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
650 4bc446b9 2021-07-21 op ui_after_message_hook();
654 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
656 4bc446b9 2021-07-21 op if (evtimer_pending(&clechoev, NULL))
657 4bc446b9 2021-07-21 op evtimer_del(&clechoev);
659 4bc446b9 2021-07-21 op free(ministate.curmesg);
660 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
662 4bc446b9 2021-07-21 op if (fmt != NULL) {
663 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);
664 4bc446b9 2021-07-21 op evtimer_add(&clechoev, &clechoev_timer);
666 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
667 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
668 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
671 4bc446b9 2021-07-21 op ui_after_message_hook();
675 4bc446b9 2021-07-21 op message(const char *fmt, ...)
679 4bc446b9 2021-07-21 op va_start(ap, fmt);
680 4bc446b9 2021-07-21 op vmessage(fmt, ap);
685 4bc446b9 2021-07-21 op minibuffer_init(void)
687 65c49665 2024-01-23 op if ((eecmd_history = hist_new(HIST_WRAP)) == NULL ||
688 65c49665 2024-01-23 op (ir_history = hist_new(HIST_WRAP)) == NULL ||
689 65c49665 2024-01-23 op (lu_history = hist_new(HIST_WRAP)) == NULL ||
690 65c49665 2024-01-23 op (read_history = hist_new(HIST_WRAP)) == NULL)
691 65c49665 2024-01-23 op err(1, "hist_new");
693 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
694 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.page.head);
696 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
697 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
698 ab0a4274 2021-07-25 op ministate.buffer.page.name = "*minibuffer*";
699 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;
701 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);