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 f63b8f73 2022-04-24 op #include <errno.h>
20 f63b8f73 2022-04-24 op #include <limits.h>
21 4bc446b9 2021-07-21 op #include <stdio.h>
22 84b88039 2021-07-12 op #include <stdlib.h>
23 84b88039 2021-07-12 op #include <string.h>
24 84b88039 2021-07-12 op
25 5a39f593 2024-02-05 op #include "certs.h"
26 cbe2da32 2024-02-06 op #include "cmd.h"
27 fd6c540b 2024-02-12 op #include "defaults.h"
28 f63b8f73 2022-04-24 op #include "fs.h"
29 65c49665 2024-01-23 op #include "hist.h"
30 36f94f06 2022-12-23 op #include "iri.h"
31 f7db6d13 2024-02-06 op #include "keymap.h"
32 450a89f7 2021-07-12 op #include "minibuffer.h"
33 1fce2e75 2021-08-14 op #include "session.h"
34 d1a0f2a3 2021-07-12 op #include "ui.h"
35 5caf7d67 2021-07-12 op #include "utf8.h"
36 9d65b1d9 2022-01-11 op #include "utils.h"
37 84b88039 2021-07-12 op
38 eefb3de5 2022-02-26 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
39 eefb3de5 2022-02-26 op
40 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
41 7bd3a14b 2022-04-13 op static const char *minibuffer_compl_text(void);
42 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
43 84b88039 2021-07-12 op static void yornp_self_insert(void);
44 84b88039 2021-07-12 op static void yornp_abort(void);
45 84b88039 2021-07-12 op static void read_self_insert(void);
46 84b88039 2021-07-12 op static void read_abort(void);
47 84b88039 2021-07-12 op static void read_select(void);
48 4bc446b9 2021-07-21 op static void handle_clear_echoarea(int, short, void *);
49 4bc446b9 2021-07-21 op
50 4bc446b9 2021-07-21 op static struct event clechoev;
51 4bc446b9 2021-07-21 op static struct timeval clechoev_timer = { 5, 0 };
52 84b88039 2021-07-12 op
53 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
54 84b88039 2021-07-12 op static struct tab *yornp_data;
55 84b88039 2021-07-12 op
56 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
57 d1353324 2021-07-13 op static struct tab *read_data;
58 84b88039 2021-07-12 op
59 65c49665 2024-01-23 op struct hist *eecmd_history;
60 65c49665 2024-01-23 op struct hist *ir_history;
61 65c49665 2024-01-23 op struct hist *lu_history;
62 65c49665 2024-01-23 op struct hist *read_history;
63 84b88039 2021-07-12 op
64 84b88039 2021-07-12 op struct ministate ministate;
65 84b88039 2021-07-12 op
66 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
67 54ee0a94 2021-07-21 op
68 54ee0a94 2021-07-21 op int in_minibuffer;
69 eefb3de5 2022-02-26 op
70 eefb3de5 2022-02-26 op static inline int
71 eefb3de5 2022-02-26 op matches(char **words, size_t len, struct line *l)
72 eefb3de5 2022-02-26 op {
73 eefb3de5 2022-02-26 op size_t i;
74 eefb3de5 2022-02-26 op int lm, am;
75 eefb3de5 2022-02-26 op
76 eefb3de5 2022-02-26 op for (i = 0; i < len; ++i) {
77 eefb3de5 2022-02-26 op lm = am = 0;
78 eefb3de5 2022-02-26 op
79 eefb3de5 2022-02-26 op if (strcasestr(l->line, words[i]) != NULL)
80 eefb3de5 2022-02-26 op lm = 1;
81 eefb3de5 2022-02-26 op if (l->alt != NULL &&
82 eefb3de5 2022-02-26 op strcasestr(l->alt, words[i]) != NULL)
83 eefb3de5 2022-02-26 op am = 1;
84 eefb3de5 2022-02-26 op
85 eefb3de5 2022-02-26 op if (!lm && !am)
86 eefb3de5 2022-02-26 op return 0;
87 eefb3de5 2022-02-26 op }
88 b1e1e41a 2021-07-14 op
89 eefb3de5 2022-02-26 op return 1;
90 eefb3de5 2022-02-26 op }
91 eefb3de5 2022-02-26 op
92 b1e1e41a 2021-07-14 op /*
93 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
94 b1e1e41a 2021-07-14 op * ones already hidden.
95 b1e1e41a 2021-07-14 op */
96 b1e1e41a 2021-07-14 op void
97 b1e1e41a 2021-07-14 op recompute_completions(int add)
98 b1e1e41a 2021-07-14 op {
99 eefb3de5 2022-02-26 op static char buf[GEMINI_URL_LEN];
100 1faa6821 2022-04-15 op const char *text;
101 eefb3de5 2022-02-26 op char *input, **ap, *words[10];
102 eefb3de5 2022-02-26 op size_t len = 0;
103 b1e1e41a 2021-07-14 op struct line *l;
104 7c1d55bf 2022-04-13 op struct vline *vl;
105 e7b982f4 2021-07-14 op struct buffer *b;
106 b1e1e41a 2021-07-14 op
107 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
108 b1e1e41a 2021-07-14 op return;
109 b1e1e41a 2021-07-14 op
110 65c49665 2024-01-23 op if (!ministate.editing)
111 65c49665 2024-01-23 op text = hist_cur(ministate.hist);
112 1faa6821 2022-04-15 op else
113 1faa6821 2022-04-15 op text = ministate.buf;
114 1faa6821 2022-04-15 op
115 1faa6821 2022-04-15 op strlcpy(buf, text, sizeof(buf));
116 eefb3de5 2022-02-26 op input = buf;
117 eefb3de5 2022-02-26 op
118 eefb3de5 2022-02-26 op /* tokenize the input */
119 eefb3de5 2022-02-26 op for (ap = words; ap < words + nitems(words) &&
120 eefb3de5 2022-02-26 op (*ap = strsep(&input, " ")) != NULL;) {
121 eefb3de5 2022-02-26 op if (**ap != '\0')
122 eefb3de5 2022-02-26 op ap++, len++;
123 eefb3de5 2022-02-26 op }
124 eefb3de5 2022-02-26 op
125 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
126 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
127 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
128 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
129 b1e1e41a 2021-07-14 op continue;
130 eefb3de5 2022-02-26 op if (matches(words, len, l)) {
131 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
132 5977965a 2021-07-15 op b->line_max++;
133 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
134 5977965a 2021-07-15 op } else {
135 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
136 5977965a 2021-07-15 op b->line_max--;
137 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
138 5977965a 2021-07-15 op }
139 b1e1e41a 2021-07-14 op }
140 e7b982f4 2021-07-14 op
141 e7b982f4 2021-07-14 op if (b->current_line == NULL)
142 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
143 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
144 7c1d55bf 2022-04-13 op vl = b->current_line;
145 4bb17137 2022-04-14 op if (ministate.compl.must_select && vl != NULL)
146 7c1d55bf 2022-04-13 op vl->parent->type = LINE_COMPL_CURRENT;
147 b1e1e41a 2021-07-14 op }
148 b1e1e41a 2021-07-14 op
149 16578ca5 2021-01-02 op int
150 16578ca5 2021-01-02 op minibuffer_insert_current_candidate(void)
151 16578ca5 2021-01-02 op {
152 16578ca5 2021-01-02 op struct vline *vl;
153 16578ca5 2021-01-02 op
154 16578ca5 2021-01-02 op vl = ministate.compl.buffer.current_line;
155 16578ca5 2021-01-02 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
156 16578ca5 2021-01-02 op return -1;
157 16578ca5 2021-01-02 op
158 16578ca5 2021-01-02 op minibuffer_taint_hist();
159 16578ca5 2021-01-02 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
160 16578ca5 2021-01-02 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
161 16578ca5 2021-01-02 op
162 16578ca5 2021-01-02 op return 0;
163 16578ca5 2021-01-02 op }
164 16578ca5 2021-01-02 op
165 d7ee7b5e 2021-07-15 op static void *
166 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
167 d7ee7b5e 2021-07-15 op {
168 d7ee7b5e 2021-07-15 op struct vline *vl;
169 d7ee7b5e 2021-07-15 op
170 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
171 d7ee7b5e 2021-07-15 op
172 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
173 d7ee7b5e 2021-07-15 op return NULL;
174 d7ee7b5e 2021-07-15 op
175 fbadd395 2021-07-16 op return vl->parent->data;
176 d7ee7b5e 2021-07-15 op }
177 d7ee7b5e 2021-07-15 op
178 7bd3a14b 2022-04-13 op static const char *
179 7bd3a14b 2022-04-13 op minibuffer_compl_text(void)
180 7bd3a14b 2022-04-13 op {
181 7bd3a14b 2022-04-13 op struct vline *vl;
182 7bd3a14b 2022-04-13 op
183 65c49665 2024-01-23 op if (!ministate.editing)
184 65c49665 2024-01-23 op return hist_cur(ministate.hist);
185 3f0b6911 2022-04-15 op
186 7bd3a14b 2022-04-13 op vl = ministate.compl.buffer.current_line;
187 7bd3a14b 2022-04-13 op if (vl == NULL || vl->parent->flags & L_HIDDEN ||
188 7bd3a14b 2022-04-13 op vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
189 7bd3a14b 2022-04-13 op return ministate.buf;
190 7bd3a14b 2022-04-13 op return vl->parent->line;
191 7bd3a14b 2022-04-13 op }
192 7bd3a14b 2022-04-13 op
193 84b88039 2021-07-12 op static void
194 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
195 84b88039 2021-07-12 op {
196 65c49665 2024-01-23 op if (ministate.hist == NULL)
197 84b88039 2021-07-12 op return;
198 84b88039 2021-07-12 op
199 65c49665 2024-01-23 op hist_append(ministate.hist, minibuffer_compl_text());
200 84b88039 2021-07-12 op }
201 84b88039 2021-07-12 op
202 84b88039 2021-07-12 op /*
203 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
204 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
205 84b88039 2021-07-12 op * navigation" thing.
206 84b88039 2021-07-12 op */
207 84b88039 2021-07-12 op void
208 84b88039 2021-07-12 op minibuffer_taint_hist(void)
209 84b88039 2021-07-12 op {
210 65c49665 2024-01-23 op if (ministate.editing)
211 84b88039 2021-07-12 op return;
212 84b88039 2021-07-12 op
213 65c49665 2024-01-23 op ministate.editing = 1;
214 65c49665 2024-01-23 op strlcpy(ministate.buf, hist_cur(ministate.hist),
215 65c49665 2024-01-23 op sizeof(ministate.buf));
216 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
217 84b88039 2021-07-12 op }
218 84b88039 2021-07-12 op
219 65601367 2021-07-14 op void
220 84b88039 2021-07-12 op minibuffer_self_insert(void)
221 84b88039 2021-07-12 op {
222 84b88039 2021-07-12 op char *c, tmp[5] = {0};
223 84b88039 2021-07-12 op size_t len;
224 84b88039 2021-07-12 op
225 84b88039 2021-07-12 op minibuffer_taint_hist();
226 84b88039 2021-07-12 op
227 84b88039 2021-07-12 op if (thiskey.cp == 0)
228 84b88039 2021-07-12 op return;
229 84b88039 2021-07-12 op
230 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
231 bd4a08a7 2022-11-07 op c = utf8_nth(ministate.buffer.current_line->parent->line,
232 bd4a08a7 2022-11-07 op ministate.buffer.cpoff);
233 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
234 84b88039 2021-07-12 op return;
235 84b88039 2021-07-12 op
236 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
237 84b88039 2021-07-12 op memcpy(c, tmp, len);
238 84b88039 2021-07-12 op ministate.buffer.cpoff++;
239 b1e1e41a 2021-07-14 op
240 b1e1e41a 2021-07-14 op recompute_completions(1);
241 40fbc354 2021-07-14 op }
242 40fbc354 2021-07-14 op
243 40fbc354 2021-07-14 op void
244 40fbc354 2021-07-14 op sensible_self_insert(void)
245 40fbc354 2021-07-14 op {
246 e8c9de1e 2021-07-20 op if (thiskey.meta ||
247 85c820b8 2021-07-24 op (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
248 84b88039 2021-07-12 op global_key_unbound();
249 84b88039 2021-07-12 op return;
250 84b88039 2021-07-12 op }
251 84b88039 2021-07-12 op
252 84b88039 2021-07-12 op minibuffer_self_insert();
253 84b88039 2021-07-12 op }
254 84b88039 2021-07-12 op
255 84b88039 2021-07-12 op void
256 84b88039 2021-07-12 op eecmd_select(void)
257 84b88039 2021-07-12 op {
258 16578ca5 2021-01-02 op struct cmd *cmd;
259 16578ca5 2021-01-02 op const char *t;
260 16578ca5 2021-01-02 op
261 7bd3a14b 2022-04-13 op t = minibuffer_compl_text();
262 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
263 16578ca5 2021-01-02 op if (!strcmp(cmd->cmd, t)) {
264 84b88039 2021-07-12 op minibuffer_hist_save_entry();
265 7bd3a14b 2022-04-13 op exit_minibuffer();
266 84b88039 2021-07-12 op cmd->fn(current_buffer());
267 84b88039 2021-07-12 op return;
268 84b88039 2021-07-12 op }
269 84b88039 2021-07-12 op }
270 84b88039 2021-07-12 op
271 84b88039 2021-07-12 op message("No match");
272 84b88039 2021-07-12 op }
273 84b88039 2021-07-12 op
274 84b88039 2021-07-12 op void
275 9a28e7e5 2021-08-03 op ir_select_gemini(void)
276 84b88039 2021-07-12 op {
277 36f94f06 2022-12-23 op static struct iri iri;
278 36f94f06 2022-12-23 op char buf[1025];
279 83dce83d 2021-07-17 op struct tab *tab = current_tab;
280 84b88039 2021-07-12 op
281 84b88039 2021-07-12 op minibuffer_hist_save_entry();
282 84b88039 2021-07-12 op
283 65c49665 2024-01-23 op if (iri_parse(NULL, hist_cur(tab->hist), &iri) == -1)
284 36f94f06 2022-12-23 op goto err;
285 36f94f06 2022-12-23 op if (iri_setquery(&iri, minibuffer_compl_text()) == -1)
286 36f94f06 2022-12-23 op goto err;
287 36f94f06 2022-12-23 op if (iri_unparse(&iri, buf, sizeof(buf)) == -1)
288 36f94f06 2022-12-23 op goto err;
289 7bd3a14b 2022-04-13 op
290 7bd3a14b 2022-04-13 op exit_minibuffer();
291 ed504b9e 2022-02-07 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
292 36f94f06 2022-12-23 op return;
293 36f94f06 2022-12-23 op
294 36f94f06 2022-12-23 op err:
295 36f94f06 2022-12-23 op message("Failed to select URL.");
296 ed504b9e 2022-02-07 op }
297 ed504b9e 2022-02-07 op
298 ed504b9e 2022-02-07 op void
299 ed504b9e 2022-02-07 op ir_select_reply(void)
300 ed504b9e 2022-02-07 op {
301 cc71f6cb 2024-01-16 op static struct iri iri;
302 ed504b9e 2022-02-07 op char buf[1025] = {0};
303 ed504b9e 2022-02-07 op struct tab *tab = current_tab;
304 ed504b9e 2022-02-07 op
305 ed504b9e 2022-02-07 op minibuffer_hist_save_entry();
306 ed504b9e 2022-02-07 op
307 ed504b9e 2022-02-07 op /* a bit ugly but... */
308 cc71f6cb 2024-01-16 op iri_parse(NULL, tab->last_input_url, &iri);
309 cc71f6cb 2024-01-16 op iri_setquery(&iri, minibuffer_compl_text());
310 cc71f6cb 2024-01-16 op iri_unparse(&iri, buf, sizeof(buf));
311 7bd3a14b 2022-04-13 op
312 7bd3a14b 2022-04-13 op exit_minibuffer();
313 ed21a9a1 2022-01-11 op load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
314 84b88039 2021-07-12 op }
315 84b88039 2021-07-12 op
316 84b88039 2021-07-12 op void
317 9a28e7e5 2021-08-03 op ir_select_gopher(void)
318 9a28e7e5 2021-08-03 op {
319 9a28e7e5 2021-08-03 op minibuffer_hist_save_entry();
320 7bd3a14b 2022-04-13 op gopher_send_search_req(current_tab, minibuffer_compl_text());
321 7bd3a14b 2022-04-13 op exit_minibuffer();
322 9a28e7e5 2021-08-03 op }
323 9a28e7e5 2021-08-03 op
324 9a28e7e5 2021-08-03 op void
325 84b88039 2021-07-12 op lu_select(void)
326 84b88039 2021-07-12 op {
327 c6efff96 2021-08-16 op char url[GEMINI_URL_LEN+1];
328 c6efff96 2021-08-16 op
329 84b88039 2021-07-12 op minibuffer_hist_save_entry();
330 65c49665 2024-01-23 op humanify_url(minibuffer_compl_text(), hist_cur(current_tab->hist),
331 449ea6fe 2024-01-16 op url, sizeof(url));
332 4bb6a4fa 2024-01-15 op
333 7bd3a14b 2022-04-13 op exit_minibuffer();
334 449ea6fe 2024-01-16 op load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
335 84b88039 2021-07-12 op }
336 84b88039 2021-07-12 op
337 84b88039 2021-07-12 op void
338 84b88039 2021-07-12 op bp_select(void)
339 84b88039 2021-07-12 op {
340 d2e2bebc 2024-01-23 op const char *url;
341 d2e2bebc 2024-01-23 op
342 d2e2bebc 2024-01-23 op url = minibuffer_compl_text();
343 d2e2bebc 2024-01-23 op if (*url != '\0') {
344 d2e2bebc 2024-01-23 op if (bookmark_page(url) == -1)
345 f63b8f73 2022-04-24 op message("failed to bookmark page: %s",
346 f63b8f73 2022-04-24 op strerror(errno));
347 d2e2bebc 2024-01-23 op else
348 d2e2bebc 2024-01-23 op message("Bookmarked");
349 f63b8f73 2022-04-24 op } else
350 84b88039 2021-07-12 op message("Abort.");
351 d2e2bebc 2024-01-23 op exit_minibuffer();
352 84b88039 2021-07-12 op }
353 84b88039 2021-07-12 op
354 65601367 2021-07-14 op void
355 65601367 2021-07-14 op ts_select(void)
356 65601367 2021-07-14 op {
357 65601367 2021-07-14 op struct tab *tab;
358 65601367 2021-07-14 op
359 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
360 65601367 2021-07-14 op message("No tab selected");
361 65601367 2021-07-14 op return;
362 65601367 2021-07-14 op }
363 65601367 2021-07-14 op
364 65601367 2021-07-14 op exit_minibuffer();
365 65601367 2021-07-14 op switch_to_tab(tab);
366 753c6ac7 2021-07-14 op }
367 753c6ac7 2021-07-14 op
368 753c6ac7 2021-07-14 op void
369 753c6ac7 2021-07-14 op ls_select(void)
370 753c6ac7 2021-07-14 op {
371 753c6ac7 2021-07-14 op struct line *l;
372 753c6ac7 2021-07-14 op
373 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
374 753c6ac7 2021-07-14 op message("No link selected");
375 753c6ac7 2021-07-14 op return;
376 753c6ac7 2021-07-14 op }
377 753c6ac7 2021-07-14 op
378 753c6ac7 2021-07-14 op exit_minibuffer();
379 ed21a9a1 2022-01-11 op load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
380 753c6ac7 2021-07-14 op }
381 753c6ac7 2021-07-14 op
382 d7ee7b5e 2021-07-15 op static inline void
383 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
384 753c6ac7 2021-07-14 op {
385 753c6ac7 2021-07-14 op struct vline *vl;
386 5924d8d2 2021-07-21 op struct buffer *buffer;
387 753c6ac7 2021-07-14 op
388 5924d8d2 2021-07-21 op buffer = current_buffer();
389 5924d8d2 2021-07-21 op
390 5924d8d2 2021-07-21 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
391 753c6ac7 2021-07-14 op if (vl->parent == l)
392 753c6ac7 2021-07-14 op break;
393 753c6ac7 2021-07-14 op }
394 753c6ac7 2021-07-14 op
395 753c6ac7 2021-07-14 op if (vl == NULL)
396 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
397 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
398 0b442ded 2021-07-16 op else {
399 5924d8d2 2021-07-21 op buffer->top_line = vl;
400 5924d8d2 2021-07-21 op buffer->current_line = vl;
401 0b442ded 2021-07-16 op }
402 65601367 2021-07-14 op }
403 65601367 2021-07-14 op
404 d7ee7b5e 2021-07-15 op void
405 d7ee7b5e 2021-07-15 op swiper_select(void)
406 edd9a650 2021-07-15 op {
407 edd9a650 2021-07-15 op struct line *l;
408 edd9a650 2021-07-15 op
409 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
410 edd9a650 2021-07-15 op message("No line selected");
411 edd9a650 2021-07-15 op return;
412 edd9a650 2021-07-15 op }
413 edd9a650 2021-07-15 op
414 edd9a650 2021-07-15 op exit_minibuffer();
415 edd9a650 2021-07-15 op jump_to_line(l);
416 edd9a650 2021-07-15 op }
417 edd9a650 2021-07-15 op
418 edd9a650 2021-07-15 op void
419 edd9a650 2021-07-15 op toc_select(void)
420 d7ee7b5e 2021-07-15 op {
421 d7ee7b5e 2021-07-15 op struct line *l;
422 d7ee7b5e 2021-07-15 op
423 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
424 d7ee7b5e 2021-07-15 op message("No line selected");
425 d7ee7b5e 2021-07-15 op return;
426 d7ee7b5e 2021-07-15 op }
427 d7ee7b5e 2021-07-15 op
428 d7ee7b5e 2021-07-15 op exit_minibuffer();
429 d7ee7b5e 2021-07-15 op jump_to_line(l);
430 d7ee7b5e 2021-07-15 op }
431 d7ee7b5e 2021-07-15 op
432 84b88039 2021-07-12 op static void
433 5a39f593 2024-02-05 op save_cert_for_site_cb(int r, struct tab *tab)
434 5a39f593 2024-02-05 op {
435 5a39f593 2024-02-05 op cert_save_for(tab->client_cert, &tab->iri, r);
436 5a39f593 2024-02-05 op }
437 5a39f593 2024-02-05 op
438 5a39f593 2024-02-05 op void
439 5a39f593 2024-02-05 op uc_select(void)
440 5a39f593 2024-02-05 op {
441 5a39f593 2024-02-05 op const char *name;
442 5a39f593 2024-02-05 op
443 5a39f593 2024-02-05 op name = minibuffer_compl_text();
444 5a39f593 2024-02-05 op if ((current_tab->client_cert = ccert(name)) == NULL) {
445 5a39f593 2024-02-05 op message("Certificate %s not found", name);
446 5a39f593 2024-02-05 op return;
447 5a39f593 2024-02-05 op }
448 5a39f593 2024-02-05 op
449 5a39f593 2024-02-05 op exit_minibuffer();
450 5a39f593 2024-02-05 op
451 c171304b 2024-02-05 op yornp("Remember for future sessions too?", save_cert_for_site_cb,
452 5a39f593 2024-02-05 op current_tab);
453 fd6c540b 2024-02-12 op }
454 fd6c540b 2024-02-12 op
455 fd6c540b 2024-02-12 op void
456 fd6c540b 2024-02-12 op search_select(void)
457 fd6c540b 2024-02-12 op {
458 fd6c540b 2024-02-12 op static struct iri iri;
459 fd6c540b 2024-02-12 op static char buf[1025];
460 fd6c540b 2024-02-12 op
461 fd6c540b 2024-02-12 op /* a bit ugly but... */
462 4e74baf8 2024-02-12 op if (iri_parse(NULL, default_search_engine, &iri) == -1) {
463 07a751ac 2024-02-12 op message("default-search-engine is a malformed IRI.");
464 4e74baf8 2024-02-12 op exit_minibuffer();
465 4e74baf8 2024-02-12 op return;
466 4e74baf8 2024-02-12 op }
467 fd6c540b 2024-02-12 op iri_setquery(&iri, minibuffer_compl_text());
468 fd6c540b 2024-02-12 op iri_unparse(&iri, buf, sizeof(buf));
469 fd6c540b 2024-02-12 op
470 fd6c540b 2024-02-12 op exit_minibuffer();
471 fd6c540b 2024-02-12 op load_url_in_tab(current_tab, buf, NULL, LU_MODE_NOCACHE);
472 5a39f593 2024-02-05 op }
473 5a39f593 2024-02-05 op
474 5a39f593 2024-02-05 op static void
475 84b88039 2021-07-12 op yornp_self_insert(void)
476 84b88039 2021-07-12 op {
477 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
478 84b88039 2021-07-12 op message("Please answer y or n");
479 84b88039 2021-07-12 op return;
480 84b88039 2021-07-12 op }
481 84b88039 2021-07-12 op
482 84b88039 2021-07-12 op exit_minibuffer();
483 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
484 84b88039 2021-07-12 op }
485 84b88039 2021-07-12 op
486 84b88039 2021-07-12 op static void
487 84b88039 2021-07-12 op yornp_abort(void)
488 84b88039 2021-07-12 op {
489 84b88039 2021-07-12 op exit_minibuffer();
490 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
491 84b88039 2021-07-12 op }
492 84b88039 2021-07-12 op
493 84b88039 2021-07-12 op static void
494 84b88039 2021-07-12 op read_self_insert(void)
495 84b88039 2021-07-12 op {
496 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
497 84b88039 2021-07-12 op global_key_unbound();
498 84b88039 2021-07-12 op return;
499 84b88039 2021-07-12 op }
500 84b88039 2021-07-12 op
501 84b88039 2021-07-12 op minibuffer_self_insert();
502 84b88039 2021-07-12 op }
503 84b88039 2021-07-12 op
504 84b88039 2021-07-12 op static void
505 84b88039 2021-07-12 op read_abort(void)
506 84b88039 2021-07-12 op {
507 84b88039 2021-07-12 op exit_minibuffer();
508 84b88039 2021-07-12 op read_cb(NULL, read_data);
509 84b88039 2021-07-12 op }
510 84b88039 2021-07-12 op
511 84b88039 2021-07-12 op static void
512 84b88039 2021-07-12 op read_select(void)
513 84b88039 2021-07-12 op {
514 95a8c791 2021-08-26 op exit_minibuffer();
515 84b88039 2021-07-12 op minibuffer_hist_save_entry();
516 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
517 b1e1e41a 2021-07-14 op }
518 b1e1e41a 2021-07-14 op
519 b1e1e41a 2021-07-14 op /*
520 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
521 b1e1e41a 2021-07-14 op */
522 b1e1e41a 2021-07-14 op static inline void
523 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
524 b1e1e41a 2021-07-14 op {
525 b3be07ea 2021-07-18 op const char *s, *descr;
526 b1e1e41a 2021-07-14 op struct line *l;
527 b1e1e41a 2021-07-14 op struct buffer *b;
528 b1e1e41a 2021-07-14 op struct parser *p;
529 0ce8aa3e 2021-07-18 op void *linedata;
530 b1e1e41a 2021-07-14 op
531 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
532 b1e1e41a 2021-07-14 op p = &b->page;
533 b1e1e41a 2021-07-14 op
534 0ce8aa3e 2021-07-18 op linedata = NULL;
535 0ce8aa3e 2021-07-18 op descr = NULL;
536 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
537 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
538 b1e1e41a 2021-07-14 op abort();
539 b1e1e41a 2021-07-14 op
540 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
541 fbadd395 2021-07-16 op l->data = linedata;
542 b3be07ea 2021-07-18 op l->alt = (char*)descr;
543 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
544 e7b982f4 2021-07-14 op abort();
545 b1e1e41a 2021-07-14 op
546 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
547 65601367 2021-07-14 op
548 65601367 2021-07-14 op linedata = NULL;
549 b3be07ea 2021-07-18 op descr = NULL;
550 b1e1e41a 2021-07-14 op }
551 7c1d55bf 2022-04-13 op
552 7c1d55bf 2022-04-13 op if ((l = TAILQ_FIRST(&p->head)) != NULL &&
553 7c1d55bf 2022-04-13 op ministate.compl.must_select)
554 7c1d55bf 2022-04-13 op l->type = LINE_COMPL_CURRENT;
555 84b88039 2021-07-12 op }
556 84b88039 2021-07-12 op
557 84b88039 2021-07-12 op void
558 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
559 65c49665 2024-01-23 op void (*abortfn)(void), struct hist *hist,
560 27dbcaab 2022-04-13 op complfn *complfn, void *compldata, int must_select)
561 84b88039 2021-07-12 op {
562 8115fd4a 2022-05-05 op ministate.compl.must_select = must_select;
563 8115fd4a 2022-05-05 op ministate.compl.fn = complfn;
564 8115fd4a 2022-05-05 op ministate.compl.data = compldata;
565 8115fd4a 2022-05-05 op
566 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
567 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
568 8115fd4a 2022-05-05 op populate_compl_buffer(complfn, compldata);
569 b1e1e41a 2021-07-14 op ui_schedule_redraw();
570 b1e1e41a 2021-07-14 op }
571 b1e1e41a 2021-07-14 op
572 84b88039 2021-07-12 op base_map = &minibuffer_map;
573 84b88039 2021-07-12 op current_map = &minibuffer_map;
574 84b88039 2021-07-12 op
575 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
576 84b88039 2021-07-12 op
577 84b88039 2021-07-12 op ministate.donefn = donefn;
578 84b88039 2021-07-12 op ministate.abortfn = abortfn;
579 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
580 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
581 bd4a08a7 2022-11-07 op ministate.buffer.current_line->parent->line = ministate.buf;
582 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
583 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
584 84b88039 2021-07-12 op
585 65c49665 2024-01-23 op ministate.editing = 1;
586 65c49665 2024-01-23 op ministate.hist = hist;
587 65c49665 2024-01-23 op if (ministate.hist)
588 65c49665 2024-01-23 op hist_seek_start(ministate.hist);
589 84b88039 2021-07-12 op }
590 84b88039 2021-07-12 op
591 84b88039 2021-07-12 op void
592 84b88039 2021-07-12 op exit_minibuffer(void)
593 84b88039 2021-07-12 op {
594 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
595 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
596 b1e1e41a 2021-07-14 op ui_schedule_redraw();
597 e7b982f4 2021-07-14 op }
598 b1e1e41a 2021-07-14 op
599 84b88039 2021-07-12 op in_minibuffer = 0;
600 84b88039 2021-07-12 op base_map = &global_map;
601 84b88039 2021-07-12 op current_map = &global_map;
602 84b88039 2021-07-12 op }
603 84b88039 2021-07-12 op
604 84b88039 2021-07-12 op void
605 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
606 84b88039 2021-07-12 op struct tab *data)
607 84b88039 2021-07-12 op {
608 84b88039 2021-07-12 op size_t len;
609 84b88039 2021-07-12 op
610 84b88039 2021-07-12 op if (in_minibuffer) {
611 84b88039 2021-07-12 op fn(0, data);
612 84b88039 2021-07-12 op return;
613 84b88039 2021-07-12 op }
614 84b88039 2021-07-12 op
615 84b88039 2021-07-12 op yornp_cb = fn;
616 84b88039 2021-07-12 op yornp_data = data;
617 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
618 27dbcaab 2022-04-13 op yornp_abort, NULL, NULL, NULL, 0);
619 84b88039 2021-07-12 op
620 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
621 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
622 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
623 84b88039 2021-07-12 op }
624 84b88039 2021-07-12 op
625 4bc446b9 2021-07-21 op void
626 4bc446b9 2021-07-21 op minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
627 b1e1e41a 2021-07-14 op struct tab *data)
628 84b88039 2021-07-12 op {
629 84b88039 2021-07-12 op size_t len;
630 84b88039 2021-07-12 op
631 84b88039 2021-07-12 op if (in_minibuffer)
632 84b88039 2021-07-12 op return;
633 84b88039 2021-07-12 op
634 84b88039 2021-07-12 op read_cb = fn;
635 84b88039 2021-07-12 op read_data = data;
636 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
637 65c49665 2024-01-23 op read_history, NULL, NULL, 0);
638 84b88039 2021-07-12 op
639 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
640 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
641 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);
642 4bc446b9 2021-07-21 op }
643 4bc446b9 2021-07-21 op
644 4bc446b9 2021-07-21 op static void
645 4bc446b9 2021-07-21 op handle_clear_echoarea(int fd, short ev, void *d)
646 4bc446b9 2021-07-21 op {
647 4bc446b9 2021-07-21 op free(ministate.curmesg);
648 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
649 4bc446b9 2021-07-21 op
650 4bc446b9 2021-07-21 op ui_after_message_hook();
651 4bc446b9 2021-07-21 op }
652 4bc446b9 2021-07-21 op
653 4bc446b9 2021-07-21 op void
654 4bc446b9 2021-07-21 op vmessage(const char *fmt, va_list ap)
655 4bc446b9 2021-07-21 op {
656 4bc446b9 2021-07-21 op if (evtimer_pending(&clechoev, NULL))
657 4bc446b9 2021-07-21 op evtimer_del(&clechoev);
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 if (fmt != NULL) {
663 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);
664 4bc446b9 2021-07-21 op evtimer_add(&clechoev, &clechoev_timer);
665 4bc446b9 2021-07-21 op
666 4bc446b9 2021-07-21 op /* TODO: what to do if the allocation fails here? */
667 4bc446b9 2021-07-21 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
668 4bc446b9 2021-07-21 op ministate.curmesg = NULL;
669 4bc446b9 2021-07-21 op }
670 4bc446b9 2021-07-21 op
671 4bc446b9 2021-07-21 op ui_after_message_hook();
672 84b88039 2021-07-12 op }
673 4bc446b9 2021-07-21 op
674 4bc446b9 2021-07-21 op void
675 4bc446b9 2021-07-21 op message(const char *fmt, ...)
676 4bc446b9 2021-07-21 op {
677 4bc446b9 2021-07-21 op va_list ap;
678 4bc446b9 2021-07-21 op
679 4bc446b9 2021-07-21 op va_start(ap, fmt);
680 4bc446b9 2021-07-21 op vmessage(fmt, ap);
681 4bc446b9 2021-07-21 op va_end(ap);
682 4bc446b9 2021-07-21 op }
683 4bc446b9 2021-07-21 op
684 4bc446b9 2021-07-21 op void
685 4bc446b9 2021-07-21 op minibuffer_init(void)
686 4bc446b9 2021-07-21 op {
687 65c49665 2024-01-23 op if ((eecmd_history = hist_new(HIST_WRAP)) == NULL ||
688 65c49665 2024-01-23 op (ir_history = hist_new(HIST_WRAP)) == NULL ||
689 65c49665 2024-01-23 op (lu_history = hist_new(HIST_WRAP)) == NULL ||
690 65c49665 2024-01-23 op (read_history = hist_new(HIST_WRAP)) == NULL)
691 65c49665 2024-01-23 op err(1, "hist_new");
692 bccb5b0b 2021-07-21 op
693 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.head);
694 78894e73 2021-08-12 op TAILQ_INIT(&ministate.compl.buffer.page.head);
695 78894e73 2021-08-12 op
696 bccb5b0b 2021-07-21 op ministate.line.type = LINE_TEXT;
697 bccb5b0b 2021-07-21 op ministate.vline.parent = &ministate.line;
698 ab0a4274 2021-07-25 op ministate.buffer.page.name = "*minibuffer*";
699 bccb5b0b 2021-07-21 op ministate.buffer.current_line = &ministate.vline;
700 bccb5b0b 2021-07-21 op
701 4bc446b9 2021-07-21 op evtimer_set(&clechoev, handle_clear_echoarea, NULL);
702 4bc446b9 2021-07-21 op }