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 minibuffer_self_insert(void);
26 84b88039 2021-07-12 op static void yornp_self_insert(void);
27 84b88039 2021-07-12 op static void yornp_abort(void);
28 84b88039 2021-07-12 op static void read_self_insert(void);
29 84b88039 2021-07-12 op static void read_abort(void);
30 84b88039 2021-07-12 op static void read_select(void);
32 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
33 84b88039 2021-07-12 op static struct tab *yornp_data;
35 d1353324 2021-07-13 op static void (*read_cb)(const char*, struct tab *);
36 d1353324 2021-07-13 op static struct tab *read_data;
38 84b88039 2021-07-12 op struct histhead eecmd_history,
43 84b88039 2021-07-12 op struct ministate ministate;
45 b1e1e41a 2021-07-14 op struct buffer minibufferwin;
48 b1e1e41a 2021-07-14 op * Recompute the visible completions. If add is 1, don't consider the
49 b1e1e41a 2021-07-14 op * ones already hidden.
52 b1e1e41a 2021-07-14 op recompute_completions(int add)
54 b1e1e41a 2021-07-14 op struct line *l;
55 e7b982f4 2021-07-14 op struct vline *vl;
56 e7b982f4 2021-07-14 op struct buffer *b;
58 b1e1e41a 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
61 e7b982f4 2021-07-14 op b = &ministate.compl.buffer;
62 e7b982f4 2021-07-14 op TAILQ_FOREACH(l, &b->page.head, lines) {
63 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
64 b1e1e41a 2021-07-14 op if (add && l->flags & L_HIDDEN)
66 b1e1e41a 2021-07-14 op if (strstr(l->line, ministate.buf) != NULL)
67 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
69 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
72 e7b982f4 2021-07-14 op if (b->current_line == NULL)
73 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
74 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
75 e7b982f4 2021-07-14 op vl = b->current_line;
76 e7b982f4 2021-07-14 op if (vl != NULL)
77 e7b982f4 2021-07-14 op vl->parent->type = LINE_COMPL_CURRENT;
81 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
83 84b88039 2021-07-12 op struct hist *hist;
85 84b88039 2021-07-12 op if (ministate.history == NULL)
88 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
91 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
93 84b88039 2021-07-12 op if (TAILQ_EMPTY(&ministate.history->head))
94 84b88039 2021-07-12 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
96 84b88039 2021-07-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
97 84b88039 2021-07-12 op ministate.history->len++;
101 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
102 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
103 84b88039 2021-07-12 op * navigation" thing.
106 84b88039 2021-07-12 op minibuffer_taint_hist(void)
108 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
111 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
112 84b88039 2021-07-12 op ministate.hist_cur = NULL;
113 19f7f20c 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
117 84b88039 2021-07-12 op minibuffer_self_insert(void)
119 84b88039 2021-07-12 op char *c, tmp[5] = {0};
122 84b88039 2021-07-12 op minibuffer_taint_hist();
124 84b88039 2021-07-12 op if (thiskey.cp == 0)
127 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
128 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
129 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
132 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
133 84b88039 2021-07-12 op memcpy(c, tmp, len);
134 84b88039 2021-07-12 op ministate.buffer.cpoff++;
136 b1e1e41a 2021-07-14 op recompute_completions(1);
140 84b88039 2021-07-12 op eecmd_self_insert(void)
142 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
143 84b88039 2021-07-12 op !unicode_isgraph(thiskey.cp)) {
144 84b88039 2021-07-12 op global_key_unbound();
148 84b88039 2021-07-12 op minibuffer_self_insert();
152 84b88039 2021-07-12 op eecmd_select(void)
154 84b88039 2021-07-12 op struct cmd *cmd;
156 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
157 84b88039 2021-07-12 op if (!strcmp(cmd->cmd, ministate.buf)) {
158 84b88039 2021-07-12 op exit_minibuffer();
159 84b88039 2021-07-12 op minibuffer_hist_save_entry();
160 84b88039 2021-07-12 op cmd->fn(current_buffer());
165 84b88039 2021-07-12 op message("No match");
169 84b88039 2021-07-12 op ir_self_insert(void)
171 84b88039 2021-07-12 op minibuffer_self_insert();
175 84b88039 2021-07-12 op ir_select(void)
177 84b88039 2021-07-12 op char buf[1025] = {0};
178 84b88039 2021-07-12 op struct phos_uri uri;
179 84b88039 2021-07-12 op struct tab *tab;
181 84b88039 2021-07-12 op tab = current_tab();
183 84b88039 2021-07-12 op exit_minibuffer();
184 84b88039 2021-07-12 op minibuffer_hist_save_entry();
186 84b88039 2021-07-12 op /* a bit ugly but... */
187 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
188 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
189 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
190 84b88039 2021-07-12 op load_url_in_tab(tab, buf);
194 84b88039 2021-07-12 op lu_self_insert(void)
196 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
197 84b88039 2021-07-12 op !unicode_isgraph(thiskey.key)) {
198 84b88039 2021-07-12 op global_key_unbound();
202 84b88039 2021-07-12 op minibuffer_self_insert();
206 84b88039 2021-07-12 op lu_select(void)
208 84b88039 2021-07-12 op exit_minibuffer();
209 84b88039 2021-07-12 op minibuffer_hist_save_entry();
210 84b88039 2021-07-12 op load_url_in_tab(current_tab(), ministate.buf);
214 84b88039 2021-07-12 op bp_select(void)
216 84b88039 2021-07-12 op exit_minibuffer();
217 84b88039 2021-07-12 op if (*ministate.buf != '\0')
218 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
220 84b88039 2021-07-12 op message("Abort.");
224 84b88039 2021-07-12 op yornp_self_insert(void)
226 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
227 84b88039 2021-07-12 op message("Please answer y or n");
231 84b88039 2021-07-12 op exit_minibuffer();
232 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
236 84b88039 2021-07-12 op yornp_abort(void)
238 84b88039 2021-07-12 op exit_minibuffer();
239 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
243 84b88039 2021-07-12 op read_self_insert(void)
245 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
246 84b88039 2021-07-12 op global_key_unbound();
250 84b88039 2021-07-12 op minibuffer_self_insert();
254 84b88039 2021-07-12 op read_abort(void)
256 84b88039 2021-07-12 op exit_minibuffer();
257 84b88039 2021-07-12 op read_cb(NULL, read_data);
261 84b88039 2021-07-12 op read_select(void)
263 84b88039 2021-07-12 op exit_minibuffer();
264 84b88039 2021-07-12 op minibuffer_hist_save_entry();
265 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
269 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
271 b1e1e41a 2021-07-14 op static inline void
272 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
274 b1e1e41a 2021-07-14 op const char *s;
275 b1e1e41a 2021-07-14 op struct line *l;
276 b1e1e41a 2021-07-14 op struct buffer *b;
277 b1e1e41a 2021-07-14 op struct parser *p;
279 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
280 b1e1e41a 2021-07-14 op p = &b->page;
282 b1e1e41a 2021-07-14 op while ((s = fn(&data)) != NULL) {
283 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
286 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
287 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
290 b1e1e41a 2021-07-14 op if (TAILQ_EMPTY(&p->head))
291 b1e1e41a 2021-07-14 op TAILQ_INSERT_HEAD(&p->head, l, lines);
293 b1e1e41a 2021-07-14 op TAILQ_INSERT_TAIL(&p->head, l, lines);
296 e7b982f4 2021-07-14 op if ((l = TAILQ_FIRST(&p->head)) != NULL)
297 e7b982f4 2021-07-14 op l->type = LINE_COMPL_CURRENT;
301 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
302 e5a2797f 2021-07-13 op void (*abortfn)(void), struct histhead *hist,
303 e5a2797f 2021-07-13 op complfn *complfn, void *compldata)
305 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
306 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
307 b1e1e41a 2021-07-14 op ui_schedule_redraw();
309 b1e1e41a 2021-07-14 op ministate.compl.fn = complfn;
310 b1e1e41a 2021-07-14 op ministate.compl.data = compldata;
311 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn, compldata);
314 84b88039 2021-07-12 op base_map = &minibuffer_map;
315 84b88039 2021-07-12 op current_map = &minibuffer_map;
317 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
319 84b88039 2021-07-12 op ministate.donefn = donefn;
320 84b88039 2021-07-12 op ministate.abortfn = abortfn;
321 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
322 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
323 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
324 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
325 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
327 84b88039 2021-07-12 op ministate.history = hist;
328 84b88039 2021-07-12 op ministate.hist_cur = NULL;
329 84b88039 2021-07-12 op ministate.hist_off = 0;
333 84b88039 2021-07-12 op exit_minibuffer(void)
335 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
336 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
337 b1e1e41a 2021-07-14 op ui_schedule_redraw();
340 84b88039 2021-07-12 op in_minibuffer = 0;
341 84b88039 2021-07-12 op base_map = &global_map;
342 84b88039 2021-07-12 op current_map = &global_map;
346 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
347 84b88039 2021-07-12 op struct tab *data)
351 84b88039 2021-07-12 op if (in_minibuffer) {
356 84b88039 2021-07-12 op yornp_cb = fn;
357 84b88039 2021-07-12 op yornp_data = data;
358 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
359 e5a2797f 2021-07-13 op yornp_abort, NULL, NULL, NULL);
361 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
362 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
363 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
367 84b88039 2021-07-12 op * Not yet "completing", but soon maybe...
370 d1353324 2021-07-13 op completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
371 b1e1e41a 2021-07-14 op struct tab *data)
375 84b88039 2021-07-12 op if (in_minibuffer)
378 84b88039 2021-07-12 op read_cb = fn;
379 84b88039 2021-07-12 op read_data = data;
380 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
381 b1e1e41a 2021-07-14 op &read_history, NULL, NULL);
383 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
384 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
385 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);