Blame


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