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