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 c58259f8 2024-06-24 op #include <stdarg.h>
25 4bc446b9 2021-07-21 op #include <stdio.h>
26 84b88039 2021-07-12 op #include <stdlib.h>
27 84b88039 2021-07-12 op #include <string.h>
29 6d24bfb3 2024-10-19 op #include <grapheme.h>
31 5a39f593 2024-02-05 op #include "certs.h"
32 cbe2da32 2024-02-06 op #include "cmd.h"
33 fd6c540b 2024-02-12 op #include "defaults.h"
34 98d3e6c1 2024-02-18 op #include "ev.h"
35 f63b8f73 2022-04-24 op #include "fs.h"
36 65c49665 2024-01-23 op #include "hist.h"
37 36f94f06 2022-12-23 op #include "iri.h"
38 f7db6d13 2024-02-06 op #include "keymap.h"
39 450a89f7 2021-07-12 op #include "minibuffer.h"
40 1fce2e75 2021-08-14 op #include "session.h"
41 d1a0f2a3 2021-07-12 op #include "ui.h"
42 9d65b1d9 2022-01-11 op #include "utils.h"
43 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
45 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
47 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
48 7bd3a14b 2022-04-13 op static const char *minibuffer_compl_text(void);
49 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
50 84b88039 2021-07-12 op static void yornp_self_insert(void);
51 84b88039 2021-07-12 op static void yornp_abort(void);
52 84b88039 2021-07-12 op static void read_self_insert(void);
53 84b88039 2021-07-12 op static void read_abort(void);
54 0fceedb5 2024-06-07 op static void read_select(const char *);
55 98d3e6c1 2024-02-18 op static void handle_clear_echoarea(int, int, void *);
57 98d3e6c1 2024-02-18 op static unsigned long clechotimer;
58 98d3e6c1 2024-02-18 op static struct timeval clechotv = { 5, 0 };
60 057e7eb6 2024-06-22 op static void (*yornp_cb)(int, void *);
61 057e7eb6 2024-06-22 op static void *yornp_data;
63 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
64 d1353324 2021-07-13 op static struct tab *read_data;
66 65c49665 2024-01-23 op struct hist *eecmd_history;
67 65c49665 2024-01-23 op struct hist *ir_history;
68 65c49665 2024-01-23 op struct hist *lu_history;
69 65c49665 2024-01-23 op struct hist *read_history;
71 84b88039 2021-07-12 op struct ministate ministate;
73 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
75 54ee0a94 2021-07-21 op int in_minibuffer;
78 a3054746 2024-02-22 op codepoint_isgraph(uint32_t cp)
80 a3054746 2024-02-22 op if (cp < INT8_MAX)
81 a3054746 2024-02-22 op return isgraph((unsigned char)cp);
85 eefb3de5 2022-02-26 op static inline int
86 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
91 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
94 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
96 eefb3de5 2022-02-26 op if (l->alt != NULL &&
97 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
100 eefb3de5 2022-02-26 op if (!lm && !am)
108 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
109 b1e1e41a 2021-07-14 op * ones already hidden.
112 b1e1e41a 2021-07-14 op recompute_completions(int add)
114 eefb3de5 2022-02-26 op static char buf[GEMINI_URL_LEN];
115 1faa6821 2022-04-15 op const char *text;
116 eefb3de5 2022-02-26 op char *input, **ap, *words[10];
117 eefb3de5 2022-02-26 op size_t len = 0;
118 b1e1e41a 2021-07-14 op struct line *l;
119 7c1d55bf 2022-04-13 op struct vline *vl;
120 e7b982f4 2021-07-14 op struct buffer *b;
122 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
125 65c49665 2024-01-23 op if (!ministate.editing)
126 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
128 1faa6821 2022-04-15 op text = ministate.buf;
130 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
133 eefb3de5 2022-02-26 op /* tokenize the input */
134 eefb3de5 2022-02-26 op for (ap = words; ap < words + nitems(words) &&
135 eefb3de5 2022-02-26 op (*ap = strsep(&input, " ")) != NULL;) {
136 eefb3de5 2022-02-26 op if (**ap != '\0')
140 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
141 c1d27b0e 2024-06-14 op TAILQ_FOREACH(l, &b->head, lines) {
142 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
143 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
145 eefb3de5 2022-02-26 op if (matches(words, len, l)) {
146 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
147 5977965a 2021-07-15 op b->line_max++;
148 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
150 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
151 5977965a 2021-07-15 op b->line_max--;
152 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
156 e7b982f4 2021-07-14 op if (b->current_line == NULL)
157 c1d27b0e 2024-06-14 op b->current_line = TAILQ_FIRST(&b->vhead);
158 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
159 7c1d55bf 2022-04-13 op vl = b->current_line;
160 4bb17137 2022-04-14 op if (ministate.compl.must_select && vl != NULL)
161 7c1d55bf 2022-04-13 op vl->parent->type = LINE_COMPL_CURRENT;
165 16578ca5 2022-01-02 op minibuffer_insert_current_candidate(void)
167 16578ca5 2022-01-02 op struct vline *vl;
169 16578ca5 2022-01-02 op vl = ministate.compl.buffer.current_line;
170 16578ca5 2022-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
173 16578ca5 2022-01-02 op minibuffer_taint_hist();
174 16578ca5 2022-01-02 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
175 6d24bfb3 2024-10-19 op ministate.buffer.point_offset = strlen(ministate.buf);
176 6d24bfb3 2024-10-19 op ministate.vline.len = strlen(ministate.buf);
181 d7ee7b5e 2021-07-15 op static void *
182 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
184 d7ee7b5e 2021-07-15 op struct vline *vl;
186 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
188 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
191 fbadd395 2021-07-16 op return vl->parent->data;
194 7bd3a14b 2022-04-13 op static const char *
195 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
197 7bd3a14b 2022-04-13 op struct vline *vl;
199 65c49665 2024-01-23 op if (!ministate.editing)
200 65c49665 2024-01-23 op return hist_cur(ministate.hist);
202 7bd3a14b 2022-04-13 op vl = ministate.compl.buffer.current_line;
203 7bd3a14b 2022-04-13 op if (vl == NULL || vl->parent->flags & L_HIDDEN ||
204 7bd3a14b 2022-04-13 op vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
205 7bd3a14b 2022-04-13 op return ministate.buf;
206 7bd3a14b 2022-04-13 op return vl->parent->line;
210 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
212 65c49665 2024-01-23 op if (ministate.hist == NULL)
215 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
219 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
220 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
221 84b88039 2021-07-12 op * navigation" thing.
224 84b88039 2021-07-12 op minibuffer_taint_hist(void)
226 65c49665 2024-01-23 op if (ministate.editing)
229 65c49665 2024-01-23 op ministate.editing = 1;
230 65c49665 2024-01-23 op strlcpy(ministate.buf, hist_cur(ministate.hist),
231 65c49665 2024-01-23 op sizeof(ministate.buf));
232 6d24bfb3 2024-10-19 op ministate.buffer.point_offset = 0;
233 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
237 0fceedb5 2024-06-07 op minibuffer_confirm(void)
239 0fceedb5 2024-06-07 op if (!in_minibuffer || ministate.donefn == NULL)
242 0fceedb5 2024-06-07 op minibuffer_taint_hist();
243 0fceedb5 2024-06-07 op ministate.donefn(minibuffer_compl_text());
247 84b88039 2021-07-12 op minibuffer_self_insert(void)
249 84b88039 2021-07-12 op char *c, tmp[5] = {0};
252 84b88039 2021-07-12 op minibuffer_taint_hist();
254 84b88039 2021-07-12 op if (thiskey.cp == 0)
257 6d24bfb3 2024-10-19 op len = grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
259 6d24bfb3 2024-10-19 op c = ministate.buffer.current_line->parent->line
260 6d24bfb3 2024-10-19 op + ministate.buffer.current_line->from
261 6d24bfb3 2024-10-19 op + ministate.buffer.point_offset;
262 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
265 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
266 84b88039 2021-07-12 op memcpy(c, tmp, len);
267 6d24bfb3 2024-10-19 op ministate.buffer.point_offset += len;
268 307e9acc 2024-10-20 op ministate.vline.len += len;
270 b1e1e41a 2021-07-14 op recompute_completions(1);
274 40fbc354 2021-07-14 op sensible_self_insert(void)
276 e8c9de1e 2021-07-20 op if (thiskey.meta ||
277 a3054746 2024-02-22 op (!codepoint_isgraph(thiskey.key) && thiskey.key != ' ')) {
278 84b88039 2021-07-12 op global_key_unbound();
282 84b88039 2021-07-12 op minibuffer_self_insert();
286 0fceedb5 2024-06-07 op eecmd_select(const char *t)
288 16578ca5 2022-01-02 op struct cmd *cmd;
290 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
291 16578ca5 2022-01-02 op if (!strcmp(cmd->cmd, t)) {
292 84b88039 2021-07-12 op minibuffer_hist_save_entry();
293 7bd3a14b 2022-04-13 op exit_minibuffer();
294 84b88039 2021-07-12 op cmd->fn(current_buffer());
299 84b88039 2021-07-12 op message("No match");
303 0fceedb5 2024-06-07 op ir_select_gemini(const char *text)
305 36f94f06 2022-12-23 op static struct iri iri;
306 36f94f06 2022-12-23 op char buf[1025];
307 83dce83d 2021-07-17 op struct tab *tab = current_tab;
309 84b88039 2021-07-12 op minibuffer_hist_save_entry();
311 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
313 0fceedb5 2024-06-07 op if (iri_setquery(&iri, text) == -1)
315 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
318 7bd3a14b 2022-04-13 op exit_minibuffer();
319 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
323 36f94f06 2022-12-23 op message("Failed to select URL.");
327 0fceedb5 2024-06-07 op ir_select_reply(const char *text)
329 cc71f6cb 2024-01-16 op static struct iri iri;
330 ed504b9e 2022-02-07 op char buf[1025] = {0};
331 ed504b9e 2022-02-07 op struct tab *tab = current_tab;
333 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
335 ed504b9e 2022-02-07 op /* a bit ugly but... */
336 cc71f6cb 2024-01-16 op iri_parse(NULL, tab->last_input_url, &iri);
337 0fceedb5 2024-06-07 op iri_setquery(&iri, text);
338 cc71f6cb 2024-01-16 op iri_unparse(&iri, buf, sizeof(buf));
340 7bd3a14b 2022-04-13 op exit_minibuffer();
341 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
345 0fceedb5 2024-06-07 op ir_select_gopher(const char *text)
347 9a28e7e5 2021-08-03 op minibuffer_hist_save_entry();
348 0fceedb5 2024-06-07 op gopher_send_search_req(current_tab, text);
349 7bd3a14b 2022-04-13 op exit_minibuffer();
353 0fceedb5 2024-06-07 op lu_select(const char *text)
355 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
357 84b88039 2021-07-12 op minibuffer_hist_save_entry();
358 0fceedb5 2024-06-07 op humanify_url(text, hist_cur(current_tab->hist), url, sizeof(url));
360 7bd3a14b 2022-04-13 op exit_minibuffer();
361 449ea6fe 2024-01-16 op load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
365 0fceedb5 2024-06-07 op bp_select(const char *url)
367 d2e2bebc 2024-01-23 op if (*url != '\0') {
368 d2e2bebc 2024-01-23 op if (bookmark_page(url) == -1)
369 f63b8f73 2022-04-24 op message("failed to bookmark page: %s",
370 f63b8f73 2022-04-24 op strerror(errno));
372 d2e2bebc 2024-01-23 op message("Bookmarked");
374 84b88039 2021-07-12 op message("Abort.");
375 d2e2bebc 2024-01-23 op exit_minibuffer();
379 0fceedb5 2024-06-07 op ts_select(const char *text)
381 65601367 2021-07-14 op struct tab *tab;
383 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
384 65601367 2021-07-14 op message("No tab selected");
388 65601367 2021-07-14 op exit_minibuffer();
389 65601367 2021-07-14 op switch_to_tab(tab);
393 0fceedb5 2024-06-07 op ls_select(const char *text)
395 753c6ac7 2021-07-14 op struct line *l;
397 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
398 753c6ac7 2021-07-14 op message("No link selected");
402 753c6ac7 2021-07-14 op exit_minibuffer();
403 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
406 d7ee7b5e 2021-07-15 op static inline void
407 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
409 753c6ac7 2021-07-14 op struct vline *vl;
410 5924d8d2 2021-07-21 op struct buffer *buffer;
412 5924d8d2 2021-07-21 op buffer = current_buffer();
414 c1d27b0e 2024-06-14 op TAILQ_FOREACH(vl, &buffer->vhead, vlines) {
415 753c6ac7 2021-07-14 op if (vl->parent == l)
419 753c6ac7 2021-07-14 op if (vl == NULL)
420 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
421 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
423 5924d8d2 2021-07-21 op buffer->top_line = vl;
424 5924d8d2 2021-07-21 op buffer->current_line = vl;
429 0fceedb5 2024-06-07 op swiper_select(const char *text)
431 edd9a650 2021-07-15 op struct line *l;
433 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
434 edd9a650 2021-07-15 op message("No line selected");
438 edd9a650 2021-07-15 op exit_minibuffer();
439 edd9a650 2021-07-15 op jump_to_line(l);
443 0fceedb5 2024-06-07 op toc_select(const char *text)
445 d7ee7b5e 2021-07-15 op struct line *l;
447 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
448 d7ee7b5e 2021-07-15 op message("No line selected");
452 d7ee7b5e 2021-07-15 op exit_minibuffer();
453 d7ee7b5e 2021-07-15 op jump_to_line(l);
457 057e7eb6 2024-06-22 op save_cert_for_site_cb(int r, void *data)
459 057e7eb6 2024-06-22 op struct tab *tab = data;
461 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
465 0fceedb5 2024-06-07 op uc_select(const char *name)
467 5a39f593 2024-02-05 op if ((current_tab->client_cert = ccert(name)) == NULL) {
468 5a39f593 2024-02-05 op message("Certificate %s not found", name);
472 5a39f593 2024-02-05 op exit_minibuffer();
474 c171304b 2024-02-05 op yornp("Remember for future sessions too?", save_cert_for_site_cb,
475 5a39f593 2024-02-05 op current_tab);
479 0fceedb5 2024-06-07 op search_select(const char *text)
481 fd6c540b 2024-02-12 op static struct iri iri;
482 fd6c540b 2024-02-12 op static char buf[1025];
484 fd6c540b 2024-02-12 op /* a bit ugly but... */
485 4e74baf8 2024-02-12 op if (iri_parse(NULL, default_search_engine, &iri) == -1) {
486 07a751ac 2024-02-12 op message("default-search-engine is a malformed IRI.");
487 4e74baf8 2024-02-12 op exit_minibuffer();
490 0fceedb5 2024-06-07 op iri_setquery(&iri, text);
491 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
493 fd6c540b 2024-02-12 op exit_minibuffer();
494 fd6c540b 2024-02-12 op load_url_in_tab(current_tab, buf, NULL, LU_MODE_NOCACHE);
498 84b88039 2021-07-12 op yornp_self_insert(void)
500 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
501 84b88039 2021-07-12 op message("Please answer y or n");
505 84b88039 2021-07-12 op exit_minibuffer();
506 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
510 84b88039 2021-07-12 op yornp_abort(void)
512 84b88039 2021-07-12 op exit_minibuffer();
513 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
517 84b88039 2021-07-12 op read_self_insert(void)
519 a3054746 2024-02-22 op if (thiskey.meta || !codepoint_isgraph(thiskey.cp)) {
520 84b88039 2021-07-12 op global_key_unbound();
524 84b88039 2021-07-12 op minibuffer_self_insert();
528 84b88039 2021-07-12 op read_abort(void)
530 84b88039 2021-07-12 op exit_minibuffer();
531 84b88039 2021-07-12 op read_cb(NULL, read_data);
535 0fceedb5 2024-06-07 op read_select(const char *text)
537 95a8c791 2021-08-26 op exit_minibuffer();
538 84b88039 2021-07-12 op minibuffer_hist_save_entry();
539 0fceedb5 2024-06-07 op read_cb(text, read_data);
543 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
545 b1e1e41a 2021-07-14 op static inline void
546 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
548 b3be07ea 2021-07-18 op const char *s, *descr;
549 b1e1e41a 2021-07-14 op struct line *l;
550 b1e1e41a 2021-07-14 op struct buffer *b;
551 0ce8aa3e 2021-07-18 op void *linedata;
553 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
555 0ce8aa3e 2021-07-18 op linedata = NULL;
556 0ce8aa3e 2021-07-18 op descr = NULL;
557 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
558 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
560 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
561 fbadd395 2021-07-16 op l->data = linedata;
562 b3be07ea 2021-07-18 op l->alt = (char*)descr;
563 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(s);
565 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
567 65601367 2021-07-14 op linedata = NULL;
568 b3be07ea 2021-07-18 op descr = NULL;
571 c1d27b0e 2024-06-14 op if ((l = TAILQ_FIRST(&b->head)) != NULL &&
572 7c1d55bf 2022-04-13 op ministate.compl.must_select)
573 7c1d55bf 2022-04-13 op l->type = LINE_COMPL_CURRENT;
577 dfd1efcc 2024-06-23 op enter_minibuffer(struct minibuffer *minibuffer, const char *fmt, ...)
581 dfd1efcc 2024-06-23 op va_start(ap, fmt);
582 dfd1efcc 2024-06-23 op vsnprintf(ministate.prompt, sizeof(ministate.prompt), fmt, ap);
585 dfd1efcc 2024-06-23 op ministate.compl.must_select = minibuffer->must_select;
586 dfd1efcc 2024-06-23 op ministate.compl.fn = minibuffer->complfn;
587 dfd1efcc 2024-06-23 op ministate.compl.data = minibuffer->compldata;
589 dfd1efcc 2024-06-23 op in_minibuffer = minibuffer->complfn == NULL ? MB_READ : MB_COMPREAD;
590 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
591 dfd1efcc 2024-06-23 op populate_compl_buffer(minibuffer->complfn,
592 dfd1efcc 2024-06-23 op minibuffer->compldata);
593 b1e1e41a 2021-07-14 op ui_schedule_redraw();
596 84b88039 2021-07-12 op base_map = &minibuffer_map;
597 84b88039 2021-07-12 op current_map = &minibuffer_map;
599 dfd1efcc 2024-06-23 op base_map->unhandled_input = minibuffer->self_insert;
601 dfd1efcc 2024-06-23 op ministate.donefn = minibuffer->done;
602 dfd1efcc 2024-06-23 op ministate.abortfn = minibuffer->abort;
603 7e435604 2024-06-24 op if (ministate.abortfn == NULL)
604 7e435604 2024-06-24 op ministate.abortfn = exit_minibuffer;
606 dfd1efcc 2024-06-23 op if (minibuffer->input) {
607 dfd1efcc 2024-06-23 op strlcpy(ministate.buf, minibuffer->input,
608 dfd1efcc 2024-06-23 op sizeof(ministate.buf));
609 6d24bfb3 2024-10-19 op ministate.buffer.point_offset = strlen(ministate.buf);
610 6d24bfb3 2024-10-19 op ministate.vline.len = strlen(ministate.buf);
612 dfd1efcc 2024-06-23 op ministate.buf[0] = '\0';
613 6d24bfb3 2024-10-19 op ministate.buffer.point_offset = 0;
614 6d24bfb3 2024-10-19 op ministate.vline.len = 0;
617 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
618 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
620 65c49665 2024-01-23 op ministate.editing = 1;
621 dfd1efcc 2024-06-23 op ministate.hist = minibuffer->history;
622 65c49665 2024-01-23 op if (ministate.hist)
623 65c49665 2024-01-23 op hist_seek_start(ministate.hist);
627 84b88039 2021-07-12 op exit_minibuffer(void)
629 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
630 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
631 b1e1e41a 2021-07-14 op ui_schedule_redraw();
634 84b88039 2021-07-12 op in_minibuffer = 0;
635 84b88039 2021-07-12 op base_map = &global_map;
636 84b88039 2021-07-12 op current_map = &global_map;
640 057e7eb6 2024-06-22 op yornp(const char *prompt, void (*fn)(int, void*), void *data)
642 dfd1efcc 2024-06-23 op struct minibuffer m = {
643 dfd1efcc 2024-06-23 op .self_insert = yornp_self_insert,
644 dfd1efcc 2024-06-23 op .abort = yornp_abort,
647 84b88039 2021-07-12 op if (in_minibuffer) {
652 84b88039 2021-07-12 op yornp_cb = fn;
653 84b88039 2021-07-12 op yornp_data = data;
654 dfd1efcc 2024-06-23 op enter_minibuffer(&m, "%s (y or n)", prompt);
658 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
659 479b240a 2024-06-25 op struct tab *data, const char *input)
661 dfd1efcc 2024-06-23 op struct minibuffer m = {
662 dfd1efcc 2024-06-23 op .self_insert = read_self_insert,
663 dfd1efcc 2024-06-23 op .done = read_select,
664 dfd1efcc 2024-06-23 op .abort = read_abort,
665 dfd1efcc 2024-06-23 op .history = read_history,
666 479b240a 2024-06-25 op .input = input,
669 84b88039 2021-07-12 op if (in_minibuffer)
672 84b88039 2021-07-12 op read_cb = fn;
673 84b88039 2021-07-12 op read_data = data;
674 dfd1efcc 2024-06-23 op enter_minibuffer(&m, "%s: ", prompt);
678 98d3e6c1 2024-02-18 op handle_clear_echoarea(int fd, int ev, void *d)
680 4bc446b9 2021-07-21 op free(ministate.curmesg);
681 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
683 4bc446b9 2021-07-21 op ui_after_message_hook();
687 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
689 98d3e6c1 2024-02-18 op ev_timer_cancel(clechotimer);
690 a74e6b4d 2024-02-19 op clechotimer = 0;
692 4bc446b9 2021-07-21 op free(ministate.curmesg);
693 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
695 4bc446b9 2021-07-21 op if (fmt != NULL) {
696 98d3e6c1 2024-02-18 op clechotimer = ev_timer(&clechotv, handle_clear_echoarea,
699 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
700 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
701 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
704 4bc446b9 2021-07-21 op ui_after_message_hook();
708 4bc446b9 2021-07-21 op message(const char *fmt, ...)
712 4bc446b9 2021-07-21 op va_start(ap, fmt);
713 4bc446b9 2021-07-21 op vmessage(fmt, ap);
718 4bc446b9 2021-07-21 op minibuffer_init(void)
720 65c49665 2024-01-23 op if ((eecmd_history = hist_new(HIST_WRAP)) == NULL ||
721 65c49665 2024-01-23 op (ir_history = hist_new(HIST_WRAP)) == NULL ||
722 65c49665 2024-01-23 op (lu_history = hist_new(HIST_WRAP)) == NULL ||
723 65c49665 2024-01-23 op (read_history = hist_new(HIST_WRAP)) == NULL)
724 65c49665 2024-01-23 op err(1, "hist_new");
726 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
727 c1d27b0e 2024-06-14 op TAILQ_INIT(&ministate.compl.buffer.vhead);
729 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
730 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
731 c1d27b0e 2024-06-14 op ministate.buffer.mode = "*minibuffer*";
732 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;