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