2 84b88039 2021-07-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 84b88039 2021-07-12 op #include <stdlib.h>
18 84b88039 2021-07-12 op #include <string.h>
20 450a89f7 2021-07-12 op #include "minibuffer.h"
21 d1a0f2a3 2021-07-12 op #include "ui.h"
22 5caf7d67 2021-07-12 op #include "utf8.h"
24 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
25 84b88039 2021-07-12 op static void yornp_self_insert(void);
26 84b88039 2021-07-12 op static void yornp_abort(void);
27 84b88039 2021-07-12 op static void read_self_insert(void);
28 84b88039 2021-07-12 op static void read_abort(void);
29 84b88039 2021-07-12 op static void read_select(void);
31 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
32 84b88039 2021-07-12 op static struct tab *yornp_data;
34 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
35 d1353324 2021-07-13 op static struct tab *read_data;
37 84b88039 2021-07-12 op struct histhead eecmd_history,
42 84b88039 2021-07-12 op struct ministate ministate;
44 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
47 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
48 b1e1e41a 2021-07-14 op * ones already hidden.
51 b1e1e41a 2021-07-14 op recompute_completions(int add)
53 b1e1e41a 2021-07-14 op struct line *l;
54 e7b982f4 2021-07-14 op struct vline *vl;
55 e7b982f4 2021-07-14 op struct buffer *b;
57 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
60 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
61 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
62 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
63 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
65 b1e1e41a 2021-07-14 op if (strstr(l->line, ministate.buf) != NULL)
66 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
68 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
71 e7b982f4 2021-07-14 op if (b->current_line == NULL)
72 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
73 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
74 e7b982f4 2021-07-14 op vl = b->current_line;
75 e7b982f4 2021-07-14 op if (vl != NULL)
76 e7b982f4 2021-07-14 op vl->parent->type = LINE_COMPL_CURRENT;
80 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
82 84b88039 2021-07-12 op struct hist *hist;
84 84b88039 2021-07-12 op if (ministate.history == NULL)
87 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
90 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
92 84b88039 2021-07-12 op if (TAILQ_EMPTY(&ministate.history->head))
93 84b88039 2021-07-12 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
95 84b88039 2021-07-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
96 84b88039 2021-07-12 op ministate.history->len++;
100 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
101 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
102 84b88039 2021-07-12 op * navigation" thing.
105 84b88039 2021-07-12 op minibuffer_taint_hist(void)
107 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
110 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
111 84b88039 2021-07-12 op ministate.hist_cur = NULL;
112 19f7f20c 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
116 84b88039 2021-07-12 op minibuffer_self_insert(void)
118 84b88039 2021-07-12 op char *c, tmp[5] = {0};
121 84b88039 2021-07-12 op minibuffer_taint_hist();
123 84b88039 2021-07-12 op if (thiskey.cp == 0)
126 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
127 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
128 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
131 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
132 84b88039 2021-07-12 op memcpy(c, tmp, len);
133 84b88039 2021-07-12 op ministate.buffer.cpoff++;
135 b1e1e41a 2021-07-14 op recompute_completions(1);
139 84b88039 2021-07-12 op eecmd_self_insert(void)
141 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
142 84b88039 2021-07-12 op !unicode_isgraph(thiskey.cp)) {
143 84b88039 2021-07-12 op global_key_unbound();
147 84b88039 2021-07-12 op minibuffer_self_insert();
151 84b88039 2021-07-12 op eecmd_select(void)
153 84b88039 2021-07-12 op struct cmd *cmd;
155 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
156 84b88039 2021-07-12 op if (!strcmp(cmd->cmd, ministate.buf)) {
157 84b88039 2021-07-12 op exit_minibuffer();
158 84b88039 2021-07-12 op minibuffer_hist_save_entry();
159 84b88039 2021-07-12 op cmd->fn(current_buffer());
164 84b88039 2021-07-12 op message("No match");
168 84b88039 2021-07-12 op ir_self_insert(void)
170 84b88039 2021-07-12 op minibuffer_self_insert();
174 84b88039 2021-07-12 op ir_select(void)
176 84b88039 2021-07-12 op char buf[1025] = {0};
177 84b88039 2021-07-12 op struct phos_uri uri;
178 84b88039 2021-07-12 op struct tab *tab;
180 84b88039 2021-07-12 op tab = current_tab();
182 84b88039 2021-07-12 op exit_minibuffer();
183 84b88039 2021-07-12 op minibuffer_hist_save_entry();
185 84b88039 2021-07-12 op /* a bit ugly but... */
186 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
187 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
188 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
189 84b88039 2021-07-12 op load_url_in_tab(tab, buf);
193 84b88039 2021-07-12 op lu_self_insert(void)
195 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
196 84b88039 2021-07-12 op !unicode_isgraph(thiskey.key)) {
197 84b88039 2021-07-12 op global_key_unbound();
201 84b88039 2021-07-12 op minibuffer_self_insert();
205 84b88039 2021-07-12 op lu_select(void)
207 84b88039 2021-07-12 op exit_minibuffer();
208 84b88039 2021-07-12 op minibuffer_hist_save_entry();
209 84b88039 2021-07-12 op load_url_in_tab(current_tab(), ministate.buf);
213 84b88039 2021-07-12 op bp_select(void)
215 84b88039 2021-07-12 op exit_minibuffer();
216 84b88039 2021-07-12 op if (*ministate.buf != '\0')
217 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
219 84b88039 2021-07-12 op message("Abort.");
223 65601367 2021-07-14 op ts_select(void)
225 65601367 2021-07-14 op struct vline *vl;
226 65601367 2021-07-14 op struct tab *tab;
228 65601367 2021-07-14 op vl = ministate.compl.buffer.current_line;
230 65601367 2021-07-14 op if (vl == NULL || vl->parent->flags & L_HIDDEN) {
231 65601367 2021-07-14 op message("No tab selected");
235 65601367 2021-07-14 op tab = vl->parent->meta.data;
236 65601367 2021-07-14 op exit_minibuffer();
237 65601367 2021-07-14 op switch_to_tab(tab);
241 84b88039 2021-07-12 op yornp_self_insert(void)
243 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
244 84b88039 2021-07-12 op message("Please answer y or n");
248 84b88039 2021-07-12 op exit_minibuffer();
249 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
253 84b88039 2021-07-12 op yornp_abort(void)
255 84b88039 2021-07-12 op exit_minibuffer();
256 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
260 84b88039 2021-07-12 op read_self_insert(void)
262 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
263 84b88039 2021-07-12 op global_key_unbound();
267 84b88039 2021-07-12 op minibuffer_self_insert();
271 84b88039 2021-07-12 op read_abort(void)
273 84b88039 2021-07-12 op exit_minibuffer();
274 84b88039 2021-07-12 op read_cb(NULL, read_data);
278 84b88039 2021-07-12 op read_select(void)
280 84b88039 2021-07-12 op exit_minibuffer();
281 84b88039 2021-07-12 op minibuffer_hist_save_entry();
282 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
286 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
288 b1e1e41a 2021-07-14 op static inline void
289 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
291 b1e1e41a 2021-07-14 op const char *s;
292 b1e1e41a 2021-07-14 op struct line *l;
293 b1e1e41a 2021-07-14 op struct buffer *b;
294 b1e1e41a 2021-07-14 op struct parser *p;
295 65601367 2021-07-14 op void *linedata = NULL;
297 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
298 b1e1e41a 2021-07-14 op p = &b->page;
300 65601367 2021-07-14 op while ((s = fn(&data, &linedata)) != NULL) {
301 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
304 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
305 65601367 2021-07-14 op l->meta.data = linedata;
306 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
309 b1e1e41a 2021-07-14 op if (TAILQ_EMPTY(&p->head))
310 b1e1e41a 2021-07-14 op TAILQ_INSERT_HEAD(&p->head, l, lines);
312 b1e1e41a 2021-07-14 op TAILQ_INSERT_TAIL(&p->head, l, lines);
314 65601367 2021-07-14 op linedata = NULL;
317 e7b982f4 2021-07-14 op if ((l = TAILQ_FIRST(&p->head)) != NULL)
318 e7b982f4 2021-07-14 op l->type = LINE_COMPL_CURRENT;
322 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
323 e5a2797f 2021-07-13 op void (*abortfn)(void), struct histhead *hist,
324 e5a2797f 2021-07-13 op complfn *complfn, void *compldata)
326 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
327 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
328 b1e1e41a 2021-07-14 op ui_schedule_redraw();
330 b1e1e41a 2021-07-14 op ministate.compl.fn = complfn;
331 b1e1e41a 2021-07-14 op ministate.compl.data = compldata;
332 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn, compldata);
335 84b88039 2021-07-12 op base_map = &minibuffer_map;
336 84b88039 2021-07-12 op current_map = &minibuffer_map;
338 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
340 84b88039 2021-07-12 op ministate.donefn = donefn;
341 84b88039 2021-07-12 op ministate.abortfn = abortfn;
342 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
343 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
344 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
345 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
346 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
348 84b88039 2021-07-12 op ministate.history = hist;
349 84b88039 2021-07-12 op ministate.hist_cur = NULL;
350 84b88039 2021-07-12 op ministate.hist_off = 0;
354 84b88039 2021-07-12 op exit_minibuffer(void)
356 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
357 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
358 b1e1e41a 2021-07-14 op ui_schedule_redraw();
361 84b88039 2021-07-12 op in_minibuffer = 0;
362 84b88039 2021-07-12 op base_map = &global_map;
363 84b88039 2021-07-12 op current_map = &global_map;
367 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
368 84b88039 2021-07-12 op struct tab *data)
372 84b88039 2021-07-12 op if (in_minibuffer) {
377 84b88039 2021-07-12 op yornp_cb = fn;
378 84b88039 2021-07-12 op yornp_data = data;
379 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
380 e5a2797f 2021-07-13 op yornp_abort, NULL, NULL, NULL);
382 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
383 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
384 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
388 84b88039 2021-07-12 op * Not yet "completing", but soon maybe...
391 d1353324 2021-07-13 op completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
392 b1e1e41a 2021-07-14 op struct tab *data)
396 84b88039 2021-07-12 op if (in_minibuffer)
399 84b88039 2021-07-12 op read_cb = fn;
400 84b88039 2021-07-12 op read_data = data;
401 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
402 b1e1e41a 2021-07-14 op &read_history, NULL, NULL);
404 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
405 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
406 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);