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