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