Blame


1 84b88039 2021-07-12 op /*
2 4bb6a4fa 2024-01-15 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 84b88039 2021-07-12 op *
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.
7 84b88039 2021-07-12 op *
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.
15 84b88039 2021-07-12 op */
16 84b88039 2021-07-12 op
17 4bc446b9 2021-07-21 op #include "compat.h"
18 4bc446b9 2021-07-21 op
19 98d3e6c1 2024-02-18 op #include <sys/time.h>
20 98d3e6c1 2024-02-18 op
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>
28 84b88039 2021-07-12 op
29 6d24bfb3 2024-10-19 op #include <grapheme.h>
30 6d24bfb3 2024-10-19 op
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"
44 84b88039 2021-07-12 op
45 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
46 eefb3de5 2022-02-26 op
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 *);
56 4bc446b9 2021-07-21 op
57 98d3e6c1 2024-02-18 op static unsigned long clechotimer;
58 98d3e6c1 2024-02-18 op static struct timeval clechotv = { 5, 0 };
59 84b88039 2021-07-12 op
60 057e7eb6 2024-06-22 op static void (*yornp_cb)(int, void *);
61 057e7eb6 2024-06-22 op static void *yornp_data;
62 84b88039 2021-07-12 op
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;
65 84b88039 2021-07-12 op
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;
70 84b88039 2021-07-12 op
71 84b88039 2021-07-12 op struct ministate ministate;
72 84b88039 2021-07-12 op
73 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
74 54ee0a94 2021-07-21 op
75 54ee0a94 2021-07-21 op int in_minibuffer;
76 eefb3de5 2022-02-26 op
77 a3054746 2024-02-22 op static int
78 a3054746 2024-02-22 op codepoint_isgraph(uint32_t cp)
79 a3054746 2024-02-22 op {
80 a3054746 2024-02-22 op if (cp < INT8_MAX)
81 a3054746 2024-02-22 op return isgraph((unsigned char)cp);
82 a3054746 2024-02-22 op return 1;
83 a3054746 2024-02-22 op }
84 a3054746 2024-02-22 op
85 eefb3de5 2022-02-26 op static inline int
86 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
87 eefb3de5 2022-02-26 op {
88 eefb3de5 2022-02-26 op size_t i;
89 eefb3de5 2022-02-26 op int lm, am;
90 eefb3de5 2022-02-26 op
91 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
92 eefb3de5 2022-02-26 op lm = am = 0;
93 eefb3de5 2022-02-26 op
94 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
95 eefb3de5 2022-02-26 op lm = 1;
96 eefb3de5 2022-02-26 op if (l->alt != NULL &&
97 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
98 eefb3de5 2022-02-26 op am = 1;
99 eefb3de5 2022-02-26 op
100 eefb3de5 2022-02-26 op if (!lm && !am)
101 eefb3de5 2022-02-26 op return 0;
102 eefb3de5 2022-02-26 op }
103 b1e1e41a 2021-07-14 op
104 eefb3de5 2022-02-26 op return 1;
105 eefb3de5 2022-02-26 op }
106 eefb3de5 2022-02-26 op
107 b1e1e41a 2021-07-14 op /*
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.
110 b1e1e41a 2021-07-14 op */
111 b1e1e41a 2021-07-14 op void
112 b1e1e41a 2021-07-14 op recompute_completions(int add)
113 b1e1e41a 2021-07-14 op {
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;
121 b1e1e41a 2021-07-14 op
122 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
123 b1e1e41a 2021-07-14 op return;
124 b1e1e41a 2021-07-14 op
125 65c49665 2024-01-23 op if (!ministate.editing)
126 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
127 1faa6821 2022-04-15 op else
128 1faa6821 2022-04-15 op text = ministate.buf;
129 1faa6821 2022-04-15 op
130 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
131 eefb3de5 2022-02-26 op input = buf;
132 eefb3de5 2022-02-26 op
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')
137 eefb3de5 2022-02-26 op ap++, len++;
138 eefb3de5 2022-02-26 op }
139 eefb3de5 2022-02-26 op
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)
144 b1e1e41a 2021-07-14 op continue;
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;
149 5977965a 2021-07-15 op } else {
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;
153 5977965a 2021-07-15 op }
154 b1e1e41a 2021-07-14 op }
155 e7b982f4 2021-07-14 op
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;
162 b1e1e41a 2021-07-14 op }
163 b1e1e41a 2021-07-14 op
164 16578ca5 2022-01-02 op int
165 16578ca5 2022-01-02 op minibuffer_insert_current_candidate(void)
166 16578ca5 2022-01-02 op {
167 16578ca5 2022-01-02 op struct vline *vl;
168 16578ca5 2022-01-02 op
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)
171 16578ca5 2022-01-02 op return -1;
172 16578ca5 2022-01-02 op
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);
177 16578ca5 2022-01-02 op
178 16578ca5 2022-01-02 op return 0;
179 16578ca5 2022-01-02 op }
180 16578ca5 2022-01-02 op
181 d7ee7b5e 2021-07-15 op static void *
182 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
183 d7ee7b5e 2021-07-15 op {
184 d7ee7b5e 2021-07-15 op struct vline *vl;
185 d7ee7b5e 2021-07-15 op
186 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
187 d7ee7b5e 2021-07-15 op
188 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
189 d7ee7b5e 2021-07-15 op return NULL;
190 d7ee7b5e 2021-07-15 op
191 fbadd395 2021-07-16 op return vl->parent->data;
192 d7ee7b5e 2021-07-15 op }
193 d7ee7b5e 2021-07-15 op
194 7bd3a14b 2022-04-13 op static const char *
195 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
196 7bd3a14b 2022-04-13 op {
197 7bd3a14b 2022-04-13 op struct vline *vl;
198 7bd3a14b 2022-04-13 op
199 65c49665 2024-01-23 op if (!ministate.editing)
200 65c49665 2024-01-23 op return hist_cur(ministate.hist);
201 3f0b6911 2022-04-15 op
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;
207 7bd3a14b 2022-04-13 op }
208 7bd3a14b 2022-04-13 op
209 84b88039 2021-07-12 op static void
210 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
211 84b88039 2021-07-12 op {
212 65c49665 2024-01-23 op if (ministate.hist == NULL)
213 84b88039 2021-07-12 op return;
214 84b88039 2021-07-12 op
215 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
216 84b88039 2021-07-12 op }
217 84b88039 2021-07-12 op
218 84b88039 2021-07-12 op /*
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.
222 84b88039 2021-07-12 op */
223 84b88039 2021-07-12 op void
224 84b88039 2021-07-12 op minibuffer_taint_hist(void)
225 84b88039 2021-07-12 op {
226 65c49665 2024-01-23 op if (ministate.editing)
227 84b88039 2021-07-12 op return;
228 84b88039 2021-07-12 op
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;
234 0fceedb5 2024-06-07 op }
235 0fceedb5 2024-06-07 op
236 0fceedb5 2024-06-07 op void
237 0fceedb5 2024-06-07 op minibuffer_confirm(void)
238 0fceedb5 2024-06-07 op {
239 0fceedb5 2024-06-07 op if (!in_minibuffer || ministate.donefn == NULL)
240 0fceedb5 2024-06-07 op return;
241 0fceedb5 2024-06-07 op
242 0fceedb5 2024-06-07 op minibuffer_taint_hist();
243 0fceedb5 2024-06-07 op ministate.donefn(minibuffer_compl_text());
244 84b88039 2021-07-12 op }
245 84b88039 2021-07-12 op
246 65601367 2021-07-14 op void
247 84b88039 2021-07-12 op minibuffer_self_insert(void)
248 84b88039 2021-07-12 op {
249 84b88039 2021-07-12 op char *c, tmp[5] = {0};
250 84b88039 2021-07-12 op size_t len;
251 84b88039 2021-07-12 op
252 84b88039 2021-07-12 op minibuffer_taint_hist();
253 84b88039 2021-07-12 op
254 84b88039 2021-07-12 op if (thiskey.cp == 0)
255 84b88039 2021-07-12 op return;
256 84b88039 2021-07-12 op
257 6d24bfb3 2024-10-19 op len = grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
258 6d24bfb3 2024-10-19 op
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)
263 84b88039 2021-07-12 op return;
264 84b88039 2021-07-12 op
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;
269 b1e1e41a 2021-07-14 op
270 b1e1e41a 2021-07-14 op recompute_completions(1);
271 40fbc354 2021-07-14 op }
272 40fbc354 2021-07-14 op
273 40fbc354 2021-07-14 op void
274 40fbc354 2021-07-14 op sensible_self_insert(void)
275 40fbc354 2021-07-14 op {
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();
279 84b88039 2021-07-12 op return;
280 84b88039 2021-07-12 op }
281 84b88039 2021-07-12 op
282 84b88039 2021-07-12 op minibuffer_self_insert();
283 84b88039 2021-07-12 op }
284 84b88039 2021-07-12 op
285 84b88039 2021-07-12 op void
286 0fceedb5 2024-06-07 op eecmd_select(const char *t)
287 84b88039 2021-07-12 op {
288 16578ca5 2022-01-02 op struct cmd *cmd;
289 16578ca5 2022-01-02 op
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());
295 84b88039 2021-07-12 op return;
296 84b88039 2021-07-12 op }
297 84b88039 2021-07-12 op }
298 84b88039 2021-07-12 op
299 84b88039 2021-07-12 op message("No match");
300 84b88039 2021-07-12 op }
301 84b88039 2021-07-12 op
302 84b88039 2021-07-12 op void
303 0fceedb5 2024-06-07 op ir_select_gemini(const char *text)
304 84b88039 2021-07-12 op {
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;
308 84b88039 2021-07-12 op
309 84b88039 2021-07-12 op minibuffer_hist_save_entry();
310 84b88039 2021-07-12 op
311 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
312 36f94f06 2022-12-23 op goto err;
313 0fceedb5 2024-06-07 op if (iri_setquery(&iri, text) == -1)
314 36f94f06 2022-12-23 op goto err;
315 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
316 36f94f06 2022-12-23 op goto err;
317 7bd3a14b 2022-04-13 op
318 7bd3a14b 2022-04-13 op exit_minibuffer();
319 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
320 36f94f06 2022-12-23 op return;
321 36f94f06 2022-12-23 op
322 36f94f06 2022-12-23 op err:
323 36f94f06 2022-12-23 op message("Failed to select URL.");
324 ed504b9e 2022-02-07 op }
325 ed504b9e 2022-02-07 op
326 ed504b9e 2022-02-07 op void
327 0fceedb5 2024-06-07 op ir_select_reply(const char *text)
328 ed504b9e 2022-02-07 op {
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;
332 ed504b9e 2022-02-07 op
333 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
334 ed504b9e 2022-02-07 op
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));
339 7bd3a14b 2022-04-13 op
340 7bd3a14b 2022-04-13 op exit_minibuffer();
341 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
342 84b88039 2021-07-12 op }
343 84b88039 2021-07-12 op
344 84b88039 2021-07-12 op void
345 0fceedb5 2024-06-07 op ir_select_gopher(const char *text)
346 9a28e7e5 2021-08-03 op {
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();
350 9a28e7e5 2021-08-03 op }
351 9a28e7e5 2021-08-03 op
352 9a28e7e5 2021-08-03 op void
353 0fceedb5 2024-06-07 op lu_select(const char *text)
354 84b88039 2021-07-12 op {
355 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
356 c6efff96 2021-08-16 op
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));
359 4bb6a4fa 2024-01-15 op
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);
362 84b88039 2021-07-12 op }
363 84b88039 2021-07-12 op
364 84b88039 2021-07-12 op void
365 0fceedb5 2024-06-07 op bp_select(const char *url)
366 84b88039 2021-07-12 op {
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));
371 d2e2bebc 2024-01-23 op else
372 d2e2bebc 2024-01-23 op message("Bookmarked");
373 f63b8f73 2022-04-24 op } else
374 84b88039 2021-07-12 op message("Abort.");
375 d2e2bebc 2024-01-23 op exit_minibuffer();
376 84b88039 2021-07-12 op }
377 84b88039 2021-07-12 op
378 65601367 2021-07-14 op void
379 0fceedb5 2024-06-07 op ts_select(const char *text)
380 65601367 2021-07-14 op {
381 65601367 2021-07-14 op struct tab *tab;
382 65601367 2021-07-14 op
383 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
384 65601367 2021-07-14 op message("No tab selected");
385 65601367 2021-07-14 op return;
386 65601367 2021-07-14 op }
387 65601367 2021-07-14 op
388 65601367 2021-07-14 op exit_minibuffer();
389 65601367 2021-07-14 op switch_to_tab(tab);
390 753c6ac7 2021-07-14 op }
391 753c6ac7 2021-07-14 op
392 753c6ac7 2021-07-14 op void
393 0fceedb5 2024-06-07 op ls_select(const char *text)
394 753c6ac7 2021-07-14 op {
395 753c6ac7 2021-07-14 op struct line *l;
396 753c6ac7 2021-07-14 op
397 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
398 753c6ac7 2021-07-14 op message("No link selected");
399 753c6ac7 2021-07-14 op return;
400 753c6ac7 2021-07-14 op }
401 753c6ac7 2021-07-14 op
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);
404 753c6ac7 2021-07-14 op }
405 753c6ac7 2021-07-14 op
406 d7ee7b5e 2021-07-15 op static inline void
407 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
408 753c6ac7 2021-07-14 op {
409 753c6ac7 2021-07-14 op struct vline *vl;
410 5924d8d2 2021-07-21 op struct buffer *buffer;
411 753c6ac7 2021-07-14 op
412 5924d8d2 2021-07-21 op buffer = current_buffer();
413 5924d8d2 2021-07-21 op
414 c1d27b0e 2024-06-14 op TAILQ_FOREACH(vl, &buffer->vhead, vlines) {
415 753c6ac7 2021-07-14 op if (vl->parent == l)
416 753c6ac7 2021-07-14 op break;
417 753c6ac7 2021-07-14 op }
418 753c6ac7 2021-07-14 op
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);
422 0b442ded 2021-07-16 op else {
423 5924d8d2 2021-07-21 op buffer->top_line = vl;
424 5924d8d2 2021-07-21 op buffer->current_line = vl;
425 0b442ded 2021-07-16 op }
426 65601367 2021-07-14 op }
427 65601367 2021-07-14 op
428 d7ee7b5e 2021-07-15 op void
429 0fceedb5 2024-06-07 op swiper_select(const char *text)
430 edd9a650 2021-07-15 op {
431 edd9a650 2021-07-15 op struct line *l;
432 edd9a650 2021-07-15 op
433 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
434 edd9a650 2021-07-15 op message("No line selected");
435 edd9a650 2021-07-15 op return;
436 edd9a650 2021-07-15 op }
437 edd9a650 2021-07-15 op
438 edd9a650 2021-07-15 op exit_minibuffer();
439 edd9a650 2021-07-15 op jump_to_line(l);
440 edd9a650 2021-07-15 op }
441 edd9a650 2021-07-15 op
442 edd9a650 2021-07-15 op void
443 0fceedb5 2024-06-07 op toc_select(const char *text)
444 d7ee7b5e 2021-07-15 op {
445 d7ee7b5e 2021-07-15 op struct line *l;
446 d7ee7b5e 2021-07-15 op
447 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
448 d7ee7b5e 2021-07-15 op message("No line selected");
449 d7ee7b5e 2021-07-15 op return;
450 d7ee7b5e 2021-07-15 op }
451 d7ee7b5e 2021-07-15 op
452 d7ee7b5e 2021-07-15 op exit_minibuffer();
453 d7ee7b5e 2021-07-15 op jump_to_line(l);
454 d7ee7b5e 2021-07-15 op }
455 d7ee7b5e 2021-07-15 op
456 84b88039 2021-07-12 op static void
457 057e7eb6 2024-06-22 op save_cert_for_site_cb(int r, void *data)
458 5a39f593 2024-02-05 op {
459 057e7eb6 2024-06-22 op struct tab *tab = data;
460 057e7eb6 2024-06-22 op
461 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
462 5a39f593 2024-02-05 op }
463 5a39f593 2024-02-05 op
464 5a39f593 2024-02-05 op void
465 0fceedb5 2024-06-07 op uc_select(const char *name)
466 5a39f593 2024-02-05 op {
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);
469 5a39f593 2024-02-05 op return;
470 5a39f593 2024-02-05 op }
471 5a39f593 2024-02-05 op
472 5a39f593 2024-02-05 op exit_minibuffer();
473 5a39f593 2024-02-05 op
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);
476 fd6c540b 2024-02-12 op }
477 fd6c540b 2024-02-12 op
478 fd6c540b 2024-02-12 op void
479 0fceedb5 2024-06-07 op search_select(const char *text)
480 fd6c540b 2024-02-12 op {
481 fd6c540b 2024-02-12 op static struct iri iri;
482 fd6c540b 2024-02-12 op static char buf[1025];
483 fd6c540b 2024-02-12 op
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();
488 4e74baf8 2024-02-12 op return;
489 4e74baf8 2024-02-12 op }
490 0fceedb5 2024-06-07 op iri_setquery(&iri, text);
491 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
492 fd6c540b 2024-02-12 op
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);
495 5a39f593 2024-02-05 op }
496 5a39f593 2024-02-05 op
497 5a39f593 2024-02-05 op static void
498 84b88039 2021-07-12 op yornp_self_insert(void)
499 84b88039 2021-07-12 op {
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");
502 84b88039 2021-07-12 op return;
503 84b88039 2021-07-12 op }
504 84b88039 2021-07-12 op
505 84b88039 2021-07-12 op exit_minibuffer();
506 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
507 84b88039 2021-07-12 op }
508 84b88039 2021-07-12 op
509 84b88039 2021-07-12 op static void
510 84b88039 2021-07-12 op yornp_abort(void)
511 84b88039 2021-07-12 op {
512 84b88039 2021-07-12 op exit_minibuffer();
513 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
514 84b88039 2021-07-12 op }
515 84b88039 2021-07-12 op
516 84b88039 2021-07-12 op static void
517 84b88039 2021-07-12 op read_self_insert(void)
518 84b88039 2021-07-12 op {
519 a3054746 2024-02-22 op if (thiskey.meta || !codepoint_isgraph(thiskey.cp)) {
520 84b88039 2021-07-12 op global_key_unbound();
521 84b88039 2021-07-12 op return;
522 84b88039 2021-07-12 op }
523 84b88039 2021-07-12 op
524 84b88039 2021-07-12 op minibuffer_self_insert();
525 84b88039 2021-07-12 op }
526 84b88039 2021-07-12 op
527 84b88039 2021-07-12 op static void
528 84b88039 2021-07-12 op read_abort(void)
529 84b88039 2021-07-12 op {
530 84b88039 2021-07-12 op exit_minibuffer();
531 84b88039 2021-07-12 op read_cb(NULL, read_data);
532 84b88039 2021-07-12 op }
533 84b88039 2021-07-12 op
534 84b88039 2021-07-12 op static void
535 0fceedb5 2024-06-07 op read_select(const char *text)
536 84b88039 2021-07-12 op {
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);
540 b1e1e41a 2021-07-14 op }
541 b1e1e41a 2021-07-14 op
542 b1e1e41a 2021-07-14 op /*
543 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
544 b1e1e41a 2021-07-14 op */
545 b1e1e41a 2021-07-14 op static inline void
546 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
547 b1e1e41a 2021-07-14 op {
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;
552 b1e1e41a 2021-07-14 op
553 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
554 b1e1e41a 2021-07-14 op
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));
559 b1e1e41a 2021-07-14 op
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);
564 b1e1e41a 2021-07-14 op
565 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
566 65601367 2021-07-14 op
567 65601367 2021-07-14 op linedata = NULL;
568 b3be07ea 2021-07-18 op descr = NULL;
569 b1e1e41a 2021-07-14 op }
570 7c1d55bf 2022-04-13 op
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;
574 84b88039 2021-07-12 op }
575 84b88039 2021-07-12 op
576 84b88039 2021-07-12 op void
577 dfd1efcc 2024-06-23 op enter_minibuffer(struct minibuffer *minibuffer, const char *fmt, ...)
578 84b88039 2021-07-12 op {
579 dfd1efcc 2024-06-23 op va_list ap;
580 8115fd4a 2022-05-05 op
581 dfd1efcc 2024-06-23 op va_start(ap, fmt);
582 dfd1efcc 2024-06-23 op vsnprintf(ministate.prompt, sizeof(ministate.prompt), fmt, ap);
583 dfd1efcc 2024-06-23 op va_end(ap);
584 dfd1efcc 2024-06-23 op
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;
588 dfd1efcc 2024-06-23 op
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();
594 b1e1e41a 2021-07-14 op }
595 b1e1e41a 2021-07-14 op
596 84b88039 2021-07-12 op base_map = &minibuffer_map;
597 84b88039 2021-07-12 op current_map = &minibuffer_map;
598 84b88039 2021-07-12 op
599 dfd1efcc 2024-06-23 op base_map->unhandled_input = minibuffer->self_insert;
600 84b88039 2021-07-12 op
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;
605 dfd1efcc 2024-06-23 op
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);
611 dfd1efcc 2024-06-23 op } else {
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;
615 dfd1efcc 2024-06-23 op }
616 dfd1efcc 2024-06-23 op
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;
619 84b88039 2021-07-12 op
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);
624 84b88039 2021-07-12 op }
625 84b88039 2021-07-12 op
626 84b88039 2021-07-12 op void
627 84b88039 2021-07-12 op exit_minibuffer(void)
628 84b88039 2021-07-12 op {
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();
632 e7b982f4 2021-07-14 op }
633 b1e1e41a 2021-07-14 op
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;
637 84b88039 2021-07-12 op }
638 84b88039 2021-07-12 op
639 84b88039 2021-07-12 op void
640 057e7eb6 2024-06-22 op yornp(const char *prompt, void (*fn)(int, void*), void *data)
641 84b88039 2021-07-12 op {
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,
645 dfd1efcc 2024-06-23 op };
646 84b88039 2021-07-12 op
647 84b88039 2021-07-12 op if (in_minibuffer) {
648 84b88039 2021-07-12 op fn(0, data);
649 84b88039 2021-07-12 op return;
650 84b88039 2021-07-12 op }
651 84b88039 2021-07-12 op
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);
655 84b88039 2021-07-12 op }
656 84b88039 2021-07-12 op
657 4bc446b9 2021-07-21 op void
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)
660 84b88039 2021-07-12 op {
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,
667 dfd1efcc 2024-06-23 op };
668 dfd1efcc 2024-06-23 op
669 84b88039 2021-07-12 op if (in_minibuffer)
670 84b88039 2021-07-12 op return;
671 84b88039 2021-07-12 op
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);
675 4bc446b9 2021-07-21 op }
676 4bc446b9 2021-07-21 op
677 4bc446b9 2021-07-21 op static void
678 98d3e6c1 2024-02-18 op handle_clear_echoarea(int fd, int ev, void *d)
679 4bc446b9 2021-07-21 op {
680 4bc446b9 2021-07-21 op free(ministate.curmesg);
681 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
682 4bc446b9 2021-07-21 op
683 4bc446b9 2021-07-21 op ui_after_message_hook();
684 4bc446b9 2021-07-21 op }
685 4bc446b9 2021-07-21 op
686 4bc446b9 2021-07-21 op void
687 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
688 4bc446b9 2021-07-21 op {
689 98d3e6c1 2024-02-18 op ev_timer_cancel(clechotimer);
690 a74e6b4d 2024-02-19 op clechotimer = 0;
691 4bc446b9 2021-07-21 op
692 4bc446b9 2021-07-21 op free(ministate.curmesg);
693 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
694 4bc446b9 2021-07-21 op
695 4bc446b9 2021-07-21 op if (fmt != NULL) {
696 98d3e6c1 2024-02-18 op clechotimer = ev_timer(&clechotv, handle_clear_echoarea,
697 98d3e6c1 2024-02-18 op NULL);
698 4bc446b9 2021-07-21 op
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;
702 4bc446b9 2021-07-21 op }
703 4bc446b9 2021-07-21 op
704 4bc446b9 2021-07-21 op ui_after_message_hook();
705 84b88039 2021-07-12 op }
706 4bc446b9 2021-07-21 op
707 4bc446b9 2021-07-21 op void
708 4bc446b9 2021-07-21 op message(const char *fmt, ...)
709 4bc446b9 2021-07-21 op {
710 4bc446b9 2021-07-21 op va_list ap;
711 4bc446b9 2021-07-21 op
712 4bc446b9 2021-07-21 op va_start(ap, fmt);
713 4bc446b9 2021-07-21 op vmessage(fmt, ap);
714 4bc446b9 2021-07-21 op va_end(ap);
715 4bc446b9 2021-07-21 op }
716 4bc446b9 2021-07-21 op
717 4bc446b9 2021-07-21 op void
718 4bc446b9 2021-07-21 op minibuffer_init(void)
719 4bc446b9 2021-07-21 op {
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");
725 bccb5b0b 2021-07-21 op
726 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
727 c1d27b0e 2024-06-14 op TAILQ_INIT(&ministate.compl.buffer.vhead);
728 78894e73 2021-08-12 op
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;
733 4bc446b9 2021-07-21 op }