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 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>
27 84b88039 2021-07-12 op
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"
41 84b88039 2021-07-12 op
42 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
43 eefb3de5 2022-02-26 op
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 *);
53 4bc446b9 2021-07-21 op
54 98d3e6c1 2024-02-18 op static unsigned long clechotimer;
55 98d3e6c1 2024-02-18 op static struct timeval clechotv = { 5, 0 };
56 84b88039 2021-07-12 op
57 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
58 84b88039 2021-07-12 op static struct tab *yornp_data;
59 84b88039 2021-07-12 op
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;
62 84b88039 2021-07-12 op
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;
67 84b88039 2021-07-12 op
68 84b88039 2021-07-12 op struct ministate ministate;
69 84b88039 2021-07-12 op
70 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
71 54ee0a94 2021-07-21 op
72 54ee0a94 2021-07-21 op int in_minibuffer;
73 eefb3de5 2022-02-26 op
74 a3054746 2024-02-22 op static int
75 a3054746 2024-02-22 op codepoint_isgraph(uint32_t cp)
76 a3054746 2024-02-22 op {
77 a3054746 2024-02-22 op if (cp < INT8_MAX)
78 a3054746 2024-02-22 op return isgraph((unsigned char)cp);
79 a3054746 2024-02-22 op return 1;
80 a3054746 2024-02-22 op }
81 a3054746 2024-02-22 op
82 eefb3de5 2022-02-26 op static inline int
83 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
84 eefb3de5 2022-02-26 op {
85 eefb3de5 2022-02-26 op size_t i;
86 eefb3de5 2022-02-26 op int lm, am;
87 eefb3de5 2022-02-26 op
88 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
89 eefb3de5 2022-02-26 op lm = am = 0;
90 eefb3de5 2022-02-26 op
91 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
92 eefb3de5 2022-02-26 op lm = 1;
93 eefb3de5 2022-02-26 op if (l->alt != NULL &&
94 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
95 eefb3de5 2022-02-26 op am = 1;
96 eefb3de5 2022-02-26 op
97 eefb3de5 2022-02-26 op if (!lm && !am)
98 eefb3de5 2022-02-26 op return 0;
99 eefb3de5 2022-02-26 op }
100 b1e1e41a 2021-07-14 op
101 eefb3de5 2022-02-26 op return 1;
102 eefb3de5 2022-02-26 op }
103 eefb3de5 2022-02-26 op
104 b1e1e41a 2021-07-14 op /*
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.
107 b1e1e41a 2021-07-14 op */
108 b1e1e41a 2021-07-14 op void
109 b1e1e41a 2021-07-14 op recompute_completions(int add)
110 b1e1e41a 2021-07-14 op {
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;
118 b1e1e41a 2021-07-14 op
119 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
120 b1e1e41a 2021-07-14 op return;
121 b1e1e41a 2021-07-14 op
122 65c49665 2024-01-23 op if (!ministate.editing)
123 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
124 1faa6821 2022-04-15 op else
125 1faa6821 2022-04-15 op text = ministate.buf;
126 1faa6821 2022-04-15 op
127 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
128 eefb3de5 2022-02-26 op input = buf;
129 eefb3de5 2022-02-26 op
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')
134 eefb3de5 2022-02-26 op ap++, len++;
135 eefb3de5 2022-02-26 op }
136 eefb3de5 2022-02-26 op
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)
141 b1e1e41a 2021-07-14 op continue;
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;
146 5977965a 2021-07-15 op } else {
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;
150 5977965a 2021-07-15 op }
151 b1e1e41a 2021-07-14 op }
152 e7b982f4 2021-07-14 op
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;
159 b1e1e41a 2021-07-14 op }
160 b1e1e41a 2021-07-14 op
161 16578ca5 2021-01-02 op int
162 16578ca5 2021-01-02 op minibuffer_insert_current_candidate(void)
163 16578ca5 2021-01-02 op {
164 16578ca5 2021-01-02 op struct vline *vl;
165 16578ca5 2021-01-02 op
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)
168 16578ca5 2021-01-02 op return -1;
169 16578ca5 2021-01-02 op
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);
173 16578ca5 2021-01-02 op
174 16578ca5 2021-01-02 op return 0;
175 16578ca5 2021-01-02 op }
176 16578ca5 2021-01-02 op
177 d7ee7b5e 2021-07-15 op static void *
178 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
179 d7ee7b5e 2021-07-15 op {
180 d7ee7b5e 2021-07-15 op struct vline *vl;
181 d7ee7b5e 2021-07-15 op
182 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
183 d7ee7b5e 2021-07-15 op
184 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
185 d7ee7b5e 2021-07-15 op return NULL;
186 d7ee7b5e 2021-07-15 op
187 fbadd395 2021-07-16 op return vl->parent->data;
188 d7ee7b5e 2021-07-15 op }
189 d7ee7b5e 2021-07-15 op
190 7bd3a14b 2022-04-13 op static const char *
191 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
192 7bd3a14b 2022-04-13 op {
193 7bd3a14b 2022-04-13 op struct vline *vl;
194 7bd3a14b 2022-04-13 op
195 65c49665 2024-01-23 op if (!ministate.editing)
196 65c49665 2024-01-23 op return hist_cur(ministate.hist);
197 3f0b6911 2022-04-15 op
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;
203 7bd3a14b 2022-04-13 op }
204 7bd3a14b 2022-04-13 op
205 84b88039 2021-07-12 op static void
206 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
207 84b88039 2021-07-12 op {
208 65c49665 2024-01-23 op if (ministate.hist == NULL)
209 84b88039 2021-07-12 op return;
210 84b88039 2021-07-12 op
211 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
212 84b88039 2021-07-12 op }
213 84b88039 2021-07-12 op
214 84b88039 2021-07-12 op /*
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.
218 84b88039 2021-07-12 op */
219 84b88039 2021-07-12 op void
220 84b88039 2021-07-12 op minibuffer_taint_hist(void)
221 84b88039 2021-07-12 op {
222 65c49665 2024-01-23 op if (ministate.editing)
223 84b88039 2021-07-12 op return;
224 84b88039 2021-07-12 op
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;
229 84b88039 2021-07-12 op }
230 84b88039 2021-07-12 op
231 65601367 2021-07-14 op void
232 84b88039 2021-07-12 op minibuffer_self_insert(void)
233 84b88039 2021-07-12 op {
234 84b88039 2021-07-12 op char *c, tmp[5] = {0};
235 84b88039 2021-07-12 op size_t len;
236 84b88039 2021-07-12 op
237 84b88039 2021-07-12 op minibuffer_taint_hist();
238 84b88039 2021-07-12 op
239 84b88039 2021-07-12 op if (thiskey.cp == 0)
240 84b88039 2021-07-12 op return;
241 84b88039 2021-07-12 op
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)
246 84b88039 2021-07-12 op return;
247 84b88039 2021-07-12 op
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++;
251 b1e1e41a 2021-07-14 op
252 b1e1e41a 2021-07-14 op recompute_completions(1);
253 40fbc354 2021-07-14 op }
254 40fbc354 2021-07-14 op
255 40fbc354 2021-07-14 op void
256 40fbc354 2021-07-14 op sensible_self_insert(void)
257 40fbc354 2021-07-14 op {
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();
261 84b88039 2021-07-12 op return;
262 84b88039 2021-07-12 op }
263 84b88039 2021-07-12 op
264 84b88039 2021-07-12 op minibuffer_self_insert();
265 84b88039 2021-07-12 op }
266 84b88039 2021-07-12 op
267 84b88039 2021-07-12 op void
268 84b88039 2021-07-12 op eecmd_select(void)
269 84b88039 2021-07-12 op {
270 16578ca5 2021-01-02 op struct cmd *cmd;
271 16578ca5 2021-01-02 op const char *t;
272 16578ca5 2021-01-02 op
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());
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
283 84b88039 2021-07-12 op message("No match");
284 84b88039 2021-07-12 op }
285 84b88039 2021-07-12 op
286 84b88039 2021-07-12 op void
287 9a28e7e5 2021-08-03 op ir_select_gemini(void)
288 84b88039 2021-07-12 op {
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;
292 84b88039 2021-07-12 op
293 84b88039 2021-07-12 op minibuffer_hist_save_entry();
294 84b88039 2021-07-12 op
295 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
296 36f94f06 2022-12-23 op goto err;
297 36f94f06 2022-12-23 op if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
298 36f94f06 2022-12-23 op goto err;
299 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
300 36f94f06 2022-12-23 op goto err;
301 7bd3a14b 2022-04-13 op
302 7bd3a14b 2022-04-13 op exit_minibuffer();
303 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
304 36f94f06 2022-12-23 op return;
305 36f94f06 2022-12-23 op
306 36f94f06 2022-12-23 op err:
307 36f94f06 2022-12-23 op message("Failed to select URL.");
308 ed504b9e 2022-02-07 op }
309 ed504b9e 2022-02-07 op
310 ed504b9e 2022-02-07 op void
311 ed504b9e 2022-02-07 op ir_select_reply(void)
312 ed504b9e 2022-02-07 op {
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;
316 ed504b9e 2022-02-07 op
317 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
318 ed504b9e 2022-02-07 op
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));
323 7bd3a14b 2022-04-13 op
324 7bd3a14b 2022-04-13 op exit_minibuffer();
325 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
326 84b88039 2021-07-12 op }
327 84b88039 2021-07-12 op
328 84b88039 2021-07-12 op void
329 9a28e7e5 2021-08-03 op ir_select_gopher(void)
330 9a28e7e5 2021-08-03 op {
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();
334 9a28e7e5 2021-08-03 op }
335 9a28e7e5 2021-08-03 op
336 9a28e7e5 2021-08-03 op void
337 84b88039 2021-07-12 op lu_select(void)
338 84b88039 2021-07-12 op {
339 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
340 c6efff96 2021-08-16 op
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));
344 4bb6a4fa 2024-01-15 op
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);
347 84b88039 2021-07-12 op }
348 84b88039 2021-07-12 op
349 84b88039 2021-07-12 op void
350 84b88039 2021-07-12 op bp_select(void)
351 84b88039 2021-07-12 op {
352 d2e2bebc 2024-01-23 op const char *url;
353 d2e2bebc 2024-01-23 op
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));
359 d2e2bebc 2024-01-23 op else
360 d2e2bebc 2024-01-23 op message("Bookmarked");
361 f63b8f73 2022-04-24 op } else
362 84b88039 2021-07-12 op message("Abort.");
363 d2e2bebc 2024-01-23 op exit_minibuffer();
364 84b88039 2021-07-12 op }
365 84b88039 2021-07-12 op
366 65601367 2021-07-14 op void
367 65601367 2021-07-14 op ts_select(void)
368 65601367 2021-07-14 op {
369 65601367 2021-07-14 op struct tab *tab;
370 65601367 2021-07-14 op
371 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
372 65601367 2021-07-14 op message("No tab selected");
373 65601367 2021-07-14 op return;
374 65601367 2021-07-14 op }
375 65601367 2021-07-14 op
376 65601367 2021-07-14 op exit_minibuffer();
377 65601367 2021-07-14 op switch_to_tab(tab);
378 753c6ac7 2021-07-14 op }
379 753c6ac7 2021-07-14 op
380 753c6ac7 2021-07-14 op void
381 753c6ac7 2021-07-14 op ls_select(void)
382 753c6ac7 2021-07-14 op {
383 753c6ac7 2021-07-14 op struct line *l;
384 753c6ac7 2021-07-14 op
385 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
386 753c6ac7 2021-07-14 op message("No link selected");
387 753c6ac7 2021-07-14 op return;
388 753c6ac7 2021-07-14 op }
389 753c6ac7 2021-07-14 op
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);
392 753c6ac7 2021-07-14 op }
393 753c6ac7 2021-07-14 op
394 d7ee7b5e 2021-07-15 op static inline void
395 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
396 753c6ac7 2021-07-14 op {
397 753c6ac7 2021-07-14 op struct vline *vl;
398 5924d8d2 2021-07-21 op struct buffer *buffer;
399 753c6ac7 2021-07-14 op
400 5924d8d2 2021-07-21 op buffer = current_buffer();
401 5924d8d2 2021-07-21 op
402 5924d8d2 2021-07-21 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
403 753c6ac7 2021-07-14 op if (vl->parent == l)
404 753c6ac7 2021-07-14 op break;
405 753c6ac7 2021-07-14 op }
406 753c6ac7 2021-07-14 op
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);
410 0b442ded 2021-07-16 op else {
411 5924d8d2 2021-07-21 op buffer->top_line = vl;
412 5924d8d2 2021-07-21 op buffer->current_line = vl;
413 0b442ded 2021-07-16 op }
414 65601367 2021-07-14 op }
415 65601367 2021-07-14 op
416 d7ee7b5e 2021-07-15 op void
417 d7ee7b5e 2021-07-15 op swiper_select(void)
418 edd9a650 2021-07-15 op {
419 edd9a650 2021-07-15 op struct line *l;
420 edd9a650 2021-07-15 op
421 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
422 edd9a650 2021-07-15 op message("No line selected");
423 edd9a650 2021-07-15 op return;
424 edd9a650 2021-07-15 op }
425 edd9a650 2021-07-15 op
426 edd9a650 2021-07-15 op exit_minibuffer();
427 edd9a650 2021-07-15 op jump_to_line(l);
428 edd9a650 2021-07-15 op }
429 edd9a650 2021-07-15 op
430 edd9a650 2021-07-15 op void
431 edd9a650 2021-07-15 op toc_select(void)
432 d7ee7b5e 2021-07-15 op {
433 d7ee7b5e 2021-07-15 op struct line *l;
434 d7ee7b5e 2021-07-15 op
435 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
436 d7ee7b5e 2021-07-15 op message("No line selected");
437 d7ee7b5e 2021-07-15 op return;
438 d7ee7b5e 2021-07-15 op }
439 d7ee7b5e 2021-07-15 op
440 d7ee7b5e 2021-07-15 op exit_minibuffer();
441 d7ee7b5e 2021-07-15 op jump_to_line(l);
442 d7ee7b5e 2021-07-15 op }
443 d7ee7b5e 2021-07-15 op
444 84b88039 2021-07-12 op static void
445 5a39f593 2024-02-05 op save_cert_for_site_cb(int r, struct tab *tab)
446 5a39f593 2024-02-05 op {
447 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
448 5a39f593 2024-02-05 op }
449 5a39f593 2024-02-05 op
450 5a39f593 2024-02-05 op void
451 5a39f593 2024-02-05 op uc_select(void)
452 5a39f593 2024-02-05 op {
453 5a39f593 2024-02-05 op const char *name;
454 5a39f593 2024-02-05 op
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);
458 5a39f593 2024-02-05 op return;
459 5a39f593 2024-02-05 op }
460 5a39f593 2024-02-05 op
461 5a39f593 2024-02-05 op exit_minibuffer();
462 5a39f593 2024-02-05 op
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);
465 fd6c540b 2024-02-12 op }
466 fd6c540b 2024-02-12 op
467 fd6c540b 2024-02-12 op void
468 fd6c540b 2024-02-12 op search_select(void)
469 fd6c540b 2024-02-12 op {
470 fd6c540b 2024-02-12 op static struct iri iri;
471 fd6c540b 2024-02-12 op static char buf[1025];
472 fd6c540b 2024-02-12 op
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();
477 4e74baf8 2024-02-12 op return;
478 4e74baf8 2024-02-12 op }
479 fd6c540b 2024-02-12 op iri_setquery(&iri, minibuffer_compl_text());
480 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
481 fd6c540b 2024-02-12 op
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);
484 5a39f593 2024-02-05 op }
485 5a39f593 2024-02-05 op
486 5a39f593 2024-02-05 op static void
487 84b88039 2021-07-12 op yornp_self_insert(void)
488 84b88039 2021-07-12 op {
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");
491 84b88039 2021-07-12 op return;
492 84b88039 2021-07-12 op }
493 84b88039 2021-07-12 op
494 84b88039 2021-07-12 op exit_minibuffer();
495 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
496 84b88039 2021-07-12 op }
497 84b88039 2021-07-12 op
498 84b88039 2021-07-12 op static void
499 84b88039 2021-07-12 op yornp_abort(void)
500 84b88039 2021-07-12 op {
501 84b88039 2021-07-12 op exit_minibuffer();
502 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
503 84b88039 2021-07-12 op }
504 84b88039 2021-07-12 op
505 84b88039 2021-07-12 op static void
506 84b88039 2021-07-12 op read_self_insert(void)
507 84b88039 2021-07-12 op {
508 a3054746 2024-02-22 op if (thiskey.meta || !codepoint_isgraph(thiskey.cp)) {
509 84b88039 2021-07-12 op global_key_unbound();
510 84b88039 2021-07-12 op return;
511 84b88039 2021-07-12 op }
512 84b88039 2021-07-12 op
513 84b88039 2021-07-12 op minibuffer_self_insert();
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_abort(void)
518 84b88039 2021-07-12 op {
519 84b88039 2021-07-12 op exit_minibuffer();
520 84b88039 2021-07-12 op read_cb(NULL, read_data);
521 84b88039 2021-07-12 op }
522 84b88039 2021-07-12 op
523 84b88039 2021-07-12 op static void
524 84b88039 2021-07-12 op read_select(void)
525 84b88039 2021-07-12 op {
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);
529 b1e1e41a 2021-07-14 op }
530 b1e1e41a 2021-07-14 op
531 b1e1e41a 2021-07-14 op /*
532 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
533 b1e1e41a 2021-07-14 op */
534 b1e1e41a 2021-07-14 op static inline void
535 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
536 b1e1e41a 2021-07-14 op {
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;
542 b1e1e41a 2021-07-14 op
543 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
544 b1e1e41a 2021-07-14 op p = &b->page;
545 b1e1e41a 2021-07-14 op
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)
550 b1e1e41a 2021-07-14 op abort();
551 b1e1e41a 2021-07-14 op
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)
556 e7b982f4 2021-07-14 op abort();
557 b1e1e41a 2021-07-14 op
558 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
559 65601367 2021-07-14 op
560 65601367 2021-07-14 op linedata = NULL;
561 b3be07ea 2021-07-18 op descr = NULL;
562 b1e1e41a 2021-07-14 op }
563 7c1d55bf 2022-04-13 op
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;
567 84b88039 2021-07-12 op }
568 84b88039 2021-07-12 op
569 84b88039 2021-07-12 op void
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)
573 84b88039 2021-07-12 op {
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;
577 8115fd4a 2022-05-05 op
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();
582 b1e1e41a 2021-07-14 op }
583 b1e1e41a 2021-07-14 op
584 84b88039 2021-07-12 op base_map = &minibuffer_map;
585 84b88039 2021-07-12 op current_map = &minibuffer_map;
586 84b88039 2021-07-12 op
587 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
588 84b88039 2021-07-12 op
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));
596 84b88039 2021-07-12 op
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);
601 84b88039 2021-07-12 op }
602 84b88039 2021-07-12 op
603 84b88039 2021-07-12 op void
604 84b88039 2021-07-12 op exit_minibuffer(void)
605 84b88039 2021-07-12 op {
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();
609 e7b982f4 2021-07-14 op }
610 b1e1e41a 2021-07-14 op
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;
614 84b88039 2021-07-12 op }
615 84b88039 2021-07-12 op
616 84b88039 2021-07-12 op void
617 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
618 84b88039 2021-07-12 op struct tab *data)
619 84b88039 2021-07-12 op {
620 84b88039 2021-07-12 op size_t len;
621 84b88039 2021-07-12 op
622 84b88039 2021-07-12 op if (in_minibuffer) {
623 84b88039 2021-07-12 op fn(0, data);
624 84b88039 2021-07-12 op return;
625 84b88039 2021-07-12 op }
626 84b88039 2021-07-12 op
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);
631 84b88039 2021-07-12 op
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);
635 84b88039 2021-07-12 op }
636 84b88039 2021-07-12 op
637 4bc446b9 2021-07-21 op void
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)
640 84b88039 2021-07-12 op {
641 84b88039 2021-07-12 op size_t len;
642 84b88039 2021-07-12 op
643 84b88039 2021-07-12 op if (in_minibuffer)
644 84b88039 2021-07-12 op return;
645 84b88039 2021-07-12 op
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);
650 84b88039 2021-07-12 op
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);
654 4bc446b9 2021-07-21 op }
655 4bc446b9 2021-07-21 op
656 4bc446b9 2021-07-21 op static void
657 98d3e6c1 2024-02-18 op handle_clear_echoarea(int fd, int ev, void *d)
658 4bc446b9 2021-07-21 op {
659 4bc446b9 2021-07-21 op free(ministate.curmesg);
660 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
661 4bc446b9 2021-07-21 op
662 4bc446b9 2021-07-21 op ui_after_message_hook();
663 4bc446b9 2021-07-21 op }
664 4bc446b9 2021-07-21 op
665 4bc446b9 2021-07-21 op void
666 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
667 4bc446b9 2021-07-21 op {
668 98d3e6c1 2024-02-18 op ev_timer_cancel(clechotimer);
669 a74e6b4d 2024-02-19 op clechotimer = 0;
670 4bc446b9 2021-07-21 op
671 4bc446b9 2021-07-21 op free(ministate.curmesg);
672 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
673 4bc446b9 2021-07-21 op
674 4bc446b9 2021-07-21 op if (fmt != NULL) {
675 98d3e6c1 2024-02-18 op clechotimer = ev_timer(&clechotv, handle_clear_echoarea,
676 98d3e6c1 2024-02-18 op NULL);
677 4bc446b9 2021-07-21 op
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;
681 4bc446b9 2021-07-21 op }
682 4bc446b9 2021-07-21 op
683 4bc446b9 2021-07-21 op ui_after_message_hook();
684 84b88039 2021-07-12 op }
685 4bc446b9 2021-07-21 op
686 4bc446b9 2021-07-21 op void
687 4bc446b9 2021-07-21 op message(const char *fmt, ...)
688 4bc446b9 2021-07-21 op {
689 4bc446b9 2021-07-21 op va_list ap;
690 4bc446b9 2021-07-21 op
691 4bc446b9 2021-07-21 op va_start(ap, fmt);
692 4bc446b9 2021-07-21 op vmessage(fmt, ap);
693 4bc446b9 2021-07-21 op va_end(ap);
694 4bc446b9 2021-07-21 op }
695 4bc446b9 2021-07-21 op
696 4bc446b9 2021-07-21 op void
697 4bc446b9 2021-07-21 op minibuffer_init(void)
698 4bc446b9 2021-07-21 op {
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");
704 bccb5b0b 2021-07-21 op
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);
707 78894e73 2021-08-12 op
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;
712 4bc446b9 2021-07-21 op }