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