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