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 98d3e6c1 2024-02-18 op #include <sys/time.h>
21 a3054746 2024-02-22 op #include <ctype.h>
22 f63b8f73 2022-04-24 op #include <errno.h>
23 f63b8f73 2022-04-24 op #include <limits.h>
24 4bc446b9 2021-07-21 op #include <stdio.h>
25 84b88039 2021-07-12 op #include <stdlib.h>
26 84b88039 2021-07-12 op #include <string.h>
28 5a39f593 2024-02-05 op #include "certs.h"
29 cbe2da32 2024-02-06 op #include "cmd.h"
30 fd6c540b 2024-02-12 op #include "defaults.h"
31 98d3e6c1 2024-02-18 op #include "ev.h"
32 f63b8f73 2022-04-24 op #include "fs.h"
33 65c49665 2024-01-23 op #include "hist.h"
34 36f94f06 2022-12-23 op #include "iri.h"
35 f7db6d13 2024-02-06 op #include "keymap.h"
36 450a89f7 2021-07-12 op #include "minibuffer.h"
37 1fce2e75 2021-08-14 op #include "session.h"
38 d1a0f2a3 2021-07-12 op #include "ui.h"
39 5caf7d67 2021-07-12 op #include "utf8.h"
40 9d65b1d9 2022-01-11 op #include "utils.h"
42 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
44 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
45 7bd3a14b 2022-04-13 op static const char *minibuffer_compl_text(void);
46 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
47 84b88039 2021-07-12 op static void yornp_self_insert(void);
48 84b88039 2021-07-12 op static void yornp_abort(void);
49 84b88039 2021-07-12 op static void read_self_insert(void);
50 84b88039 2021-07-12 op static void read_abort(void);
51 84b88039 2021-07-12 op static void read_select(void);
52 98d3e6c1 2024-02-18 op static void handle_clear_echoarea(int, int, void *);
54 98d3e6c1 2024-02-18 op static unsigned long clechotimer;
55 98d3e6c1 2024-02-18 op static struct timeval clechotv = { 5, 0 };
57 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
58 84b88039 2021-07-12 op static struct tab *yornp_data;
60 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
61 d1353324 2021-07-13 op static struct tab *read_data;
63 65c49665 2024-01-23 op struct hist *eecmd_history;
64 65c49665 2024-01-23 op struct hist *ir_history;
65 65c49665 2024-01-23 op struct hist *lu_history;
66 65c49665 2024-01-23 op struct hist *read_history;
68 84b88039 2021-07-12 op struct ministate ministate;
70 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
72 54ee0a94 2021-07-21 op int in_minibuffer;
75 a3054746 2024-02-22 op codepoint_isgraph(uint32_t cp)
77 a3054746 2024-02-22 op if (cp < INT8_MAX)
78 a3054746 2024-02-22 op return isgraph((unsigned char)cp);
82 eefb3de5 2022-02-26 op static inline int
83 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
88 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
91 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
93 eefb3de5 2022-02-26 op if (l->alt != NULL &&
94 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
97 eefb3de5 2022-02-26 op if (!lm && !am)
105 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
106 b1e1e41a 2021-07-14 op * ones already hidden.
109 b1e1e41a 2021-07-14 op recompute_completions(int add)
111 eefb3de5 2022-02-26 op static char buf[GEMINI_URL_LEN];
112 1faa6821 2022-04-15 op const char *text;
113 eefb3de5 2022-02-26 op char *input, **ap, *words[10];
114 eefb3de5 2022-02-26 op size_t len = 0;
115 b1e1e41a 2021-07-14 op struct line *l;
116 7c1d55bf 2022-04-13 op struct vline *vl;
117 e7b982f4 2021-07-14 op struct buffer *b;
119 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
122 65c49665 2024-01-23 op if (!ministate.editing)
123 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
125 1faa6821 2022-04-15 op text = ministate.buf;
127 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
130 eefb3de5 2022-02-26 op /* tokenize the input */
131 eefb3de5 2022-02-26 op for (ap = words; ap < words + nitems(words) &&
132 eefb3de5 2022-02-26 op (*ap = strsep(&input, " ")) != NULL;) {
133 eefb3de5 2022-02-26 op if (**ap != '\0')
137 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
138 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
139 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
140 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
142 eefb3de5 2022-02-26 op if (matches(words, len, l)) {
143 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
144 5977965a 2021-07-15 op b->line_max++;
145 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
147 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
148 5977965a 2021-07-15 op b->line_max--;
149 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
153 e7b982f4 2021-07-14 op if (b->current_line == NULL)
154 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
155 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
156 7c1d55bf 2022-04-13 op vl = b->current_line;
157 4bb17137 2022-04-14 op if (ministate.compl.must_select && vl != NULL)
158 7c1d55bf 2022-04-13 op vl->parent->type = LINE_COMPL_CURRENT;
162 16578ca5 2021-01-02 op minibuffer_insert_current_candidate(void)
164 16578ca5 2021-01-02 op struct vline *vl;
166 16578ca5 2021-01-02 op vl = ministate.compl.buffer.current_line;
167 16578ca5 2021-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
170 16578ca5 2021-01-02 op minibuffer_taint_hist();
171 16578ca5 2021-01-02 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
172 16578ca5 2021-01-02 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
177 d7ee7b5e 2021-07-15 op static void *
178 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
180 d7ee7b5e 2021-07-15 op struct vline *vl;
182 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
184 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
187 fbadd395 2021-07-16 op return vl->parent->data;
190 7bd3a14b 2022-04-13 op static const char *
191 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
193 7bd3a14b 2022-04-13 op struct vline *vl;
195 65c49665 2024-01-23 op if (!ministate.editing)
196 65c49665 2024-01-23 op return hist_cur(ministate.hist);
198 7bd3a14b 2022-04-13 op vl = ministate.compl.buffer.current_line;
199 7bd3a14b 2022-04-13 op if (vl == NULL || vl->parent->flags & L_HIDDEN ||
200 7bd3a14b 2022-04-13 op vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
201 7bd3a14b 2022-04-13 op return ministate.buf;
202 7bd3a14b 2022-04-13 op return vl->parent->line;
206 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
208 65c49665 2024-01-23 op if (ministate.hist == NULL)
211 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
215 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
216 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
217 84b88039 2021-07-12 op * navigation" thing.
220 84b88039 2021-07-12 op minibuffer_taint_hist(void)
222 65c49665 2024-01-23 op if (ministate.editing)
225 65c49665 2024-01-23 op ministate.editing = 1;
226 65c49665 2024-01-23 op strlcpy(ministate.buf, hist_cur(ministate.hist),
227 65c49665 2024-01-23 op sizeof(ministate.buf));
228 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
232 84b88039 2021-07-12 op minibuffer_self_insert(void)
234 84b88039 2021-07-12 op char *c, tmp[5] = {0};
237 84b88039 2021-07-12 op minibuffer_taint_hist();
239 84b88039 2021-07-12 op if (thiskey.cp == 0)
242 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
243 bd4a08a7 2022-11-07 op c = utf8_nth(ministate.buffer.current_line->parent->line,
244 bd4a08a7 2022-11-07 op ministate.buffer.cpoff);
245 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
248 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
249 84b88039 2021-07-12 op memcpy(c, tmp, len);
250 84b88039 2021-07-12 op ministate.buffer.cpoff++;
252 b1e1e41a 2021-07-14 op recompute_completions(1);
256 40fbc354 2021-07-14 op sensible_self_insert(void)
258 e8c9de1e 2021-07-20 op if (thiskey.meta ||
259 a3054746 2024-02-22 op (!codepoint_isgraph(thiskey.key) && thiskey.key != ' ')) {
260 84b88039 2021-07-12 op global_key_unbound();
264 84b88039 2021-07-12 op minibuffer_self_insert();
268 84b88039 2021-07-12 op eecmd_select(void)
270 16578ca5 2021-01-02 op struct cmd *cmd;
271 16578ca5 2021-01-02 op const char *t;
273 7bd3a14b 2022-04-13 op t = minibuffer_compl_text();
274 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
275 16578ca5 2021-01-02 op if (!strcmp(cmd->cmd, t)) {
276 84b88039 2021-07-12 op minibuffer_hist_save_entry();
277 7bd3a14b 2022-04-13 op exit_minibuffer();
278 84b88039 2021-07-12 op cmd->fn(current_buffer());
283 84b88039 2021-07-12 op message("No match");
287 9a28e7e5 2021-08-03 op ir_select_gemini(void)
289 36f94f06 2022-12-23 op static struct iri iri;
290 36f94f06 2022-12-23 op char buf[1025];
291 83dce83d 2021-07-17 op struct tab *tab = current_tab;
293 84b88039 2021-07-12 op minibuffer_hist_save_entry();
295 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
297 36f94f06 2022-12-23 op if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
299 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
302 7bd3a14b 2022-04-13 op exit_minibuffer();
303 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
307 36f94f06 2022-12-23 op message("Failed to select URL.");
311 ed504b9e 2022-02-07 op ir_select_reply(void)
313 cc71f6cb 2024-01-16 op static struct iri iri;
314 ed504b9e 2022-02-07 op char buf[1025] = {0};
315 ed504b9e 2022-02-07 op struct tab *tab = current_tab;
317 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
319 ed504b9e 2022-02-07 op /* a bit ugly but... */
320 cc71f6cb 2024-01-16 op iri_parse(NULL, tab->last_input_url, &iri);
321 cc71f6cb 2024-01-16 op iri_setquery(&iri, minibuffer_compl_text());
322 cc71f6cb 2024-01-16 op iri_unparse(&iri, buf, sizeof(buf));
324 7bd3a14b 2022-04-13 op exit_minibuffer();
325 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
329 9a28e7e5 2021-08-03 op ir_select_gopher(void)
331 9a28e7e5 2021-08-03 op minibuffer_hist_save_entry();
332 7bd3a14b 2022-04-13 op gopher_send_search_req(current_tab, minibuffer_compl_text());
333 7bd3a14b 2022-04-13 op exit_minibuffer();
337 84b88039 2021-07-12 op lu_select(void)
339 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
341 84b88039 2021-07-12 op minibuffer_hist_save_entry();
342 65c49665 2024-01-23 op humanify_url(minibuffer_compl_text(), hist_cur(current_tab->hist),
343 449ea6fe 2024-01-16 op url, sizeof(url));
345 7bd3a14b 2022-04-13 op exit_minibuffer();
346 449ea6fe 2024-01-16 op load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
350 84b88039 2021-07-12 op bp_select(void)
352 d2e2bebc 2024-01-23 op const char *url;
354 d2e2bebc 2024-01-23 op url = minibuffer_compl_text();
355 d2e2bebc 2024-01-23 op if (*url != '\0') {
356 d2e2bebc 2024-01-23 op if (bookmark_page(url) == -1)
357 f63b8f73 2022-04-24 op message("failed to bookmark page: %s",
358 f63b8f73 2022-04-24 op strerror(errno));
360 d2e2bebc 2024-01-23 op message("Bookmarked");
362 84b88039 2021-07-12 op message("Abort.");
363 d2e2bebc 2024-01-23 op exit_minibuffer();
367 65601367 2021-07-14 op ts_select(void)
369 65601367 2021-07-14 op struct tab *tab;
371 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
372 65601367 2021-07-14 op message("No tab selected");
376 65601367 2021-07-14 op exit_minibuffer();
377 65601367 2021-07-14 op switch_to_tab(tab);
381 753c6ac7 2021-07-14 op ls_select(void)
383 753c6ac7 2021-07-14 op struct line *l;
385 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
386 753c6ac7 2021-07-14 op message("No link selected");
390 753c6ac7 2021-07-14 op exit_minibuffer();
391 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
394 d7ee7b5e 2021-07-15 op static inline void
395 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
397 753c6ac7 2021-07-14 op struct vline *vl;
398 5924d8d2 2021-07-21 op struct buffer *buffer;
400 5924d8d2 2021-07-21 op buffer = current_buffer();
402 5924d8d2 2021-07-21 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
403 753c6ac7 2021-07-14 op if (vl->parent == l)
407 753c6ac7 2021-07-14 op if (vl == NULL)
408 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
409 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
411 5924d8d2 2021-07-21 op buffer->top_line = vl;
412 5924d8d2 2021-07-21 op buffer->current_line = vl;
417 d7ee7b5e 2021-07-15 op swiper_select(void)
419 edd9a650 2021-07-15 op struct line *l;
421 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
422 edd9a650 2021-07-15 op message("No line selected");
426 edd9a650 2021-07-15 op exit_minibuffer();
427 edd9a650 2021-07-15 op jump_to_line(l);
431 edd9a650 2021-07-15 op toc_select(void)
433 d7ee7b5e 2021-07-15 op struct line *l;
435 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
436 d7ee7b5e 2021-07-15 op message("No line selected");
440 d7ee7b5e 2021-07-15 op exit_minibuffer();
441 d7ee7b5e 2021-07-15 op jump_to_line(l);
445 5a39f593 2024-02-05 op save_cert_for_site_cb(int r, struct tab *tab)
447 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
451 5a39f593 2024-02-05 op uc_select(void)
453 5a39f593 2024-02-05 op const char *name;
455 5a39f593 2024-02-05 op name = minibuffer_compl_text();
456 5a39f593 2024-02-05 op if ((current_tab->client_cert = ccert(name)) == NULL) {
457 5a39f593 2024-02-05 op message("Certificate %s not found", name);
461 5a39f593 2024-02-05 op exit_minibuffer();
463 c171304b 2024-02-05 op yornp("Remember for future sessions too?", save_cert_for_site_cb,
464 5a39f593 2024-02-05 op current_tab);
468 fd6c540b 2024-02-12 op search_select(void)
470 fd6c540b 2024-02-12 op static struct iri iri;
471 fd6c540b 2024-02-12 op static char buf[1025];
473 fd6c540b 2024-02-12 op /* a bit ugly but... */
474 4e74baf8 2024-02-12 op if (iri_parse(NULL, default_search_engine, &iri) == -1) {
475 07a751ac 2024-02-12 op message("default-search-engine is a malformed IRI.");
476 4e74baf8 2024-02-12 op exit_minibuffer();
479 fd6c540b 2024-02-12 op iri_setquery(&iri, minibuffer_compl_text());
480 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
482 fd6c540b 2024-02-12 op exit_minibuffer();
483 fd6c540b 2024-02-12 op load_url_in_tab(current_tab, buf, NULL, LU_MODE_NOCACHE);
487 84b88039 2021-07-12 op yornp_self_insert(void)
489 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
490 84b88039 2021-07-12 op message("Please answer y or n");
494 84b88039 2021-07-12 op exit_minibuffer();
495 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
499 84b88039 2021-07-12 op yornp_abort(void)
501 84b88039 2021-07-12 op exit_minibuffer();
502 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
506 84b88039 2021-07-12 op read_self_insert(void)
508 a3054746 2024-02-22 op if (thiskey.meta || !codepoint_isgraph(thiskey.cp)) {
509 84b88039 2021-07-12 op global_key_unbound();
513 84b88039 2021-07-12 op minibuffer_self_insert();
517 84b88039 2021-07-12 op read_abort(void)
519 84b88039 2021-07-12 op exit_minibuffer();
520 84b88039 2021-07-12 op read_cb(NULL, read_data);
524 84b88039 2021-07-12 op read_select(void)
526 95a8c791 2021-08-26 op exit_minibuffer();
527 84b88039 2021-07-12 op minibuffer_hist_save_entry();
528 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
532 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
534 b1e1e41a 2021-07-14 op static inline void
535 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
537 b3be07ea 2021-07-18 op const char *s, *descr;
538 b1e1e41a 2021-07-14 op struct line *l;
539 b1e1e41a 2021-07-14 op struct buffer *b;
540 b1e1e41a 2021-07-14 op struct parser *p;
541 0ce8aa3e 2021-07-18 op void *linedata;
543 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
544 b1e1e41a 2021-07-14 op p = &b->page;
546 0ce8aa3e 2021-07-18 op linedata = NULL;
547 0ce8aa3e 2021-07-18 op descr = NULL;
548 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
549 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
552 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
553 fbadd395 2021-07-16 op l->data = linedata;
554 b3be07ea 2021-07-18 op l->alt = (char*)descr;
555 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
558 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
560 65601367 2021-07-14 op linedata = NULL;
561 b3be07ea 2021-07-18 op descr = NULL;
564 7c1d55bf 2022-04-13 op if ((l = TAILQ_FIRST(&p->head)) != NULL &&
565 7c1d55bf 2022-04-13 op ministate.compl.must_select)
566 7c1d55bf 2022-04-13 op l->type = LINE_COMPL_CURRENT;
570 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
571 65c49665 2024-01-23 op void (*abortfn)(void), struct hist *hist,
572 27dbcaab 2022-04-13 op complfn *complfn, void *compldata, int must_select)
574 8115fd4a 2022-05-05 op ministate.compl.must_select = must_select;
575 8115fd4a 2022-05-05 op ministate.compl.fn = complfn;
576 8115fd4a 2022-05-05 op ministate.compl.data = compldata;
578 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
579 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
580 8115fd4a 2022-05-05 op populate_compl_buffer(complfn, compldata);
581 b1e1e41a 2021-07-14 op ui_schedule_redraw();
584 84b88039 2021-07-12 op base_map = &minibuffer_map;
585 84b88039 2021-07-12 op current_map = &minibuffer_map;
587 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
589 84b88039 2021-07-12 op ministate.donefn = donefn;
590 84b88039 2021-07-12 op ministate.abortfn = abortfn;
591 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
592 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
593 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
594 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
595 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
597 65c49665 2024-01-23 op ministate.editing = 1;
598 65c49665 2024-01-23 op ministate.hist = hist;
599 65c49665 2024-01-23 op if (ministate.hist)
600 65c49665 2024-01-23 op hist_seek_start(ministate.hist);
604 84b88039 2021-07-12 op exit_minibuffer(void)
606 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
607 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
608 b1e1e41a 2021-07-14 op ui_schedule_redraw();
611 84b88039 2021-07-12 op in_minibuffer = 0;
612 84b88039 2021-07-12 op base_map = &global_map;
613 84b88039 2021-07-12 op current_map = &global_map;
617 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
618 84b88039 2021-07-12 op struct tab *data)
622 84b88039 2021-07-12 op if (in_minibuffer) {
627 84b88039 2021-07-12 op yornp_cb = fn;
628 84b88039 2021-07-12 op yornp_data = data;
629 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
630 27dbcaab 2022-04-13 op yornp_abort, NULL, NULL, NULL, 0);
632 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
633 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
634 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
638 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
639 b1e1e41a 2021-07-14 op struct tab *data)
643 84b88039 2021-07-12 op if (in_minibuffer)
646 84b88039 2021-07-12 op read_cb = fn;
647 84b88039 2021-07-12 op read_data = data;
648 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
649 65c49665 2024-01-23 op read_history, NULL, NULL, 0);
651 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
652 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
653 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);
657 98d3e6c1 2024-02-18 op handle_clear_echoarea(int fd, int ev, void *d)
659 4bc446b9 2021-07-21 op free(ministate.curmesg);
660 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
662 4bc446b9 2021-07-21 op ui_after_message_hook();
666 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
668 98d3e6c1 2024-02-18 op ev_timer_cancel(clechotimer);
669 a74e6b4d 2024-02-19 op clechotimer = 0;
671 4bc446b9 2021-07-21 op free(ministate.curmesg);
672 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
674 4bc446b9 2021-07-21 op if (fmt != NULL) {
675 98d3e6c1 2024-02-18 op clechotimer = ev_timer(&clechotv, handle_clear_echoarea,
678 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
679 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
680 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
683 4bc446b9 2021-07-21 op ui_after_message_hook();
687 4bc446b9 2021-07-21 op message(const char *fmt, ...)
691 4bc446b9 2021-07-21 op va_start(ap, fmt);
692 4bc446b9 2021-07-21 op vmessage(fmt, ap);
697 4bc446b9 2021-07-21 op minibuffer_init(void)
699 65c49665 2024-01-23 op if ((eecmd_history = hist_new(HIST_WRAP)) == NULL ||
700 65c49665 2024-01-23 op (ir_history = hist_new(HIST_WRAP)) == NULL ||
701 65c49665 2024-01-23 op (lu_history = hist_new(HIST_WRAP)) == NULL ||
702 65c49665 2024-01-23 op (read_history = hist_new(HIST_WRAP)) == NULL)
703 65c49665 2024-01-23 op err(1, "hist_new");
705 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
706 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.page.head);
708 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
709 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
710 ab0a4274 2021-07-25 op ministate.buffer.page.name = "*minibuffer*";
711 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;