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 d7ee7b5e 2021-07-15 op static void *minibuffer_metadata(void);
25 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(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 b3be07ea 2021-07-18 op if (strcasestr(l->line, ministate.buf) != NULL ||
67 b3be07ea 2021-07-18 op (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
68 5977965a 2021-07-15 op if (l->flags & L_HIDDEN)
69 5977965a 2021-07-15 op b->line_max++;
70 b1e1e41a 2021-07-14 op l->flags &= ~L_HIDDEN;
72 5977965a 2021-07-15 op if (!(l->flags & L_HIDDEN))
73 5977965a 2021-07-15 op b->line_max--;
74 b1e1e41a 2021-07-14 op l->flags |= L_HIDDEN;
78 e7b982f4 2021-07-14 op if (b->current_line == NULL)
79 e7b982f4 2021-07-14 op b->current_line = TAILQ_FIRST(&b->head);
80 e7b982f4 2021-07-14 op b->current_line = adjust_line(b->current_line, b);
81 e7b982f4 2021-07-14 op vl = b->current_line;
82 e7b982f4 2021-07-14 op if (vl != NULL)
83 e7b982f4 2021-07-14 op vl->parent->type = LINE_COMPL_CURRENT;
87 d7ee7b5e 2021-07-15 op minibuffer_metadata(void)
89 d7ee7b5e 2021-07-15 op struct vline *vl;
91 d7ee7b5e 2021-07-15 op vl = ministate.compl.buffer.current_line;
93 d7ee7b5e 2021-07-15 op if (vl == NULL || vl->parent->flags & L_HIDDEN)
96 fbadd395 2021-07-16 op return vl->parent->data;
100 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
102 84b88039 2021-07-12 op struct hist *hist;
104 84b88039 2021-07-12 op if (ministate.history == NULL)
107 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
110 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
112 84b88039 2021-07-12 op if (TAILQ_EMPTY(&ministate.history->head))
113 84b88039 2021-07-12 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
115 84b88039 2021-07-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
116 84b88039 2021-07-12 op ministate.history->len++;
120 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
121 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
122 84b88039 2021-07-12 op * navigation" thing.
125 84b88039 2021-07-12 op minibuffer_taint_hist(void)
127 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
130 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
131 84b88039 2021-07-12 op ministate.hist_cur = NULL;
132 19f7f20c 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
136 84b88039 2021-07-12 op minibuffer_self_insert(void)
138 84b88039 2021-07-12 op char *c, tmp[5] = {0};
141 84b88039 2021-07-12 op minibuffer_taint_hist();
143 84b88039 2021-07-12 op if (thiskey.cp == 0)
146 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
147 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
148 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
151 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
152 84b88039 2021-07-12 op memcpy(c, tmp, len);
153 84b88039 2021-07-12 op ministate.buffer.cpoff++;
155 b1e1e41a 2021-07-14 op recompute_completions(1);
159 40fbc354 2021-07-14 op sensible_self_insert(void)
161 40fbc354 2021-07-14 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
162 40fbc354 2021-07-14 op !unicode_isgraph(thiskey.key)) {
163 40fbc354 2021-07-14 op global_key_unbound();
167 40fbc354 2021-07-14 op minibuffer_self_insert();
171 84b88039 2021-07-12 op eecmd_self_insert(void)
173 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
174 84b88039 2021-07-12 op !unicode_isgraph(thiskey.cp)) {
175 84b88039 2021-07-12 op global_key_unbound();
179 84b88039 2021-07-12 op minibuffer_self_insert();
183 84b88039 2021-07-12 op eecmd_select(void)
185 84b88039 2021-07-12 op struct cmd *cmd;
187 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
188 84b88039 2021-07-12 op if (!strcmp(cmd->cmd, ministate.buf)) {
189 84b88039 2021-07-12 op exit_minibuffer();
190 84b88039 2021-07-12 op minibuffer_hist_save_entry();
191 84b88039 2021-07-12 op cmd->fn(current_buffer());
196 84b88039 2021-07-12 op message("No match");
200 84b88039 2021-07-12 op ir_self_insert(void)
202 84b88039 2021-07-12 op minibuffer_self_insert();
206 84b88039 2021-07-12 op ir_select(void)
208 84b88039 2021-07-12 op char buf[1025] = {0};
209 84b88039 2021-07-12 op struct phos_uri uri;
210 83dce83d 2021-07-17 op struct tab *tab = current_tab;
212 84b88039 2021-07-12 op exit_minibuffer();
213 84b88039 2021-07-12 op minibuffer_hist_save_entry();
215 84b88039 2021-07-12 op /* a bit ugly but... */
216 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
217 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
218 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
219 84b88039 2021-07-12 op load_url_in_tab(tab, buf);
223 84b88039 2021-07-12 op lu_select(void)
225 84b88039 2021-07-12 op exit_minibuffer();
226 84b88039 2021-07-12 op minibuffer_hist_save_entry();
227 83dce83d 2021-07-17 op load_url_in_tab(current_tab, ministate.buf);
231 84b88039 2021-07-12 op bp_select(void)
233 84b88039 2021-07-12 op exit_minibuffer();
234 84b88039 2021-07-12 op if (*ministate.buf != '\0')
235 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
237 84b88039 2021-07-12 op message("Abort.");
241 65601367 2021-07-14 op ts_select(void)
243 65601367 2021-07-14 op struct tab *tab;
245 d7ee7b5e 2021-07-15 op if ((tab = minibuffer_metadata()) == NULL) {
246 65601367 2021-07-14 op message("No tab selected");
250 65601367 2021-07-14 op exit_minibuffer();
251 65601367 2021-07-14 op switch_to_tab(tab);
255 753c6ac7 2021-07-14 op ls_select(void)
257 753c6ac7 2021-07-14 op struct line *l;
259 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
260 753c6ac7 2021-07-14 op message("No link selected");
264 753c6ac7 2021-07-14 op exit_minibuffer();
265 83dce83d 2021-07-17 op load_url_in_tab(current_tab, l->alt);
268 d7ee7b5e 2021-07-15 op static inline void
269 d7ee7b5e 2021-07-15 op jump_to_line(struct line *l)
271 753c6ac7 2021-07-14 op struct vline *vl;
272 83dce83d 2021-07-17 op struct tab *tab = current_tab;
274 753c6ac7 2021-07-14 op TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
275 753c6ac7 2021-07-14 op if (vl->parent == l)
279 753c6ac7 2021-07-14 op if (vl == NULL)
280 d7ee7b5e 2021-07-15 op message("Ops, %s error! Please report to %s",
281 d7ee7b5e 2021-07-15 op __func__, PACKAGE_BUGREPORT);
283 0b442ded 2021-07-16 op tab->buffer.top_line = vl;
284 753c6ac7 2021-07-14 op tab->buffer.current_line = vl;
289 d7ee7b5e 2021-07-15 op swiper_select(void)
291 edd9a650 2021-07-15 op struct line *l;
293 edd9a650 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
294 edd9a650 2021-07-15 op message("No line selected");
298 edd9a650 2021-07-15 op exit_minibuffer();
299 edd9a650 2021-07-15 op jump_to_line(l);
303 edd9a650 2021-07-15 op toc_select(void)
305 d7ee7b5e 2021-07-15 op struct line *l;
307 d7ee7b5e 2021-07-15 op if ((l = minibuffer_metadata()) == NULL) {
308 d7ee7b5e 2021-07-15 op message("No line selected");
312 d7ee7b5e 2021-07-15 op exit_minibuffer();
313 d7ee7b5e 2021-07-15 op jump_to_line(l);
317 84b88039 2021-07-12 op yornp_self_insert(void)
319 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
320 84b88039 2021-07-12 op message("Please answer y or n");
324 84b88039 2021-07-12 op exit_minibuffer();
325 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
329 84b88039 2021-07-12 op yornp_abort(void)
331 84b88039 2021-07-12 op exit_minibuffer();
332 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
336 84b88039 2021-07-12 op read_self_insert(void)
338 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
339 84b88039 2021-07-12 op global_key_unbound();
343 84b88039 2021-07-12 op minibuffer_self_insert();
347 84b88039 2021-07-12 op read_abort(void)
349 84b88039 2021-07-12 op exit_minibuffer();
350 84b88039 2021-07-12 op read_cb(NULL, read_data);
354 84b88039 2021-07-12 op read_select(void)
356 84b88039 2021-07-12 op exit_minibuffer();
357 84b88039 2021-07-12 op minibuffer_hist_save_entry();
358 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
362 b1e1e41a 2021-07-14 op * TODO: we should collect this asynchronously...
364 b1e1e41a 2021-07-14 op static inline void
365 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn *fn, void *data)
367 b3be07ea 2021-07-18 op const char *s, *descr;
368 b1e1e41a 2021-07-14 op struct line *l;
369 b1e1e41a 2021-07-14 op struct buffer *b;
370 b1e1e41a 2021-07-14 op struct parser *p;
371 0ce8aa3e 2021-07-18 op void *linedata;
373 b1e1e41a 2021-07-14 op b = &ministate.compl.buffer;
374 b1e1e41a 2021-07-14 op p = &b->page;
376 0ce8aa3e 2021-07-18 op linedata = NULL;
377 0ce8aa3e 2021-07-18 op descr = NULL;
378 b3be07ea 2021-07-18 op while ((s = fn(&data, &linedata, &descr)) != NULL) {
379 b1e1e41a 2021-07-14 op if ((l = calloc(1, sizeof(*l))) == NULL)
382 e7b982f4 2021-07-14 op l->type = LINE_COMPL;
383 fbadd395 2021-07-16 op l->data = linedata;
384 b3be07ea 2021-07-18 op l->alt = (char*)descr;
385 e7b982f4 2021-07-14 op if ((l->line = strdup(s)) == NULL)
388 b1e1e41a 2021-07-14 op if (TAILQ_EMPTY(&p->head))
389 b1e1e41a 2021-07-14 op TAILQ_INSERT_HEAD(&p->head, l, lines);
391 b1e1e41a 2021-07-14 op TAILQ_INSERT_TAIL(&p->head, l, lines);
393 65601367 2021-07-14 op linedata = NULL;
394 b3be07ea 2021-07-18 op descr = NULL;
397 e7b982f4 2021-07-14 op if ((l = TAILQ_FIRST(&p->head)) != NULL)
398 e7b982f4 2021-07-14 op l->type = LINE_COMPL_CURRENT;
402 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
403 e5a2797f 2021-07-13 op void (*abortfn)(void), struct histhead *hist,
404 e5a2797f 2021-07-13 op complfn *complfn, void *compldata)
406 b1e1e41a 2021-07-14 op in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
407 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
408 b1e1e41a 2021-07-14 op ui_schedule_redraw();
410 b1e1e41a 2021-07-14 op ministate.compl.fn = complfn;
411 b1e1e41a 2021-07-14 op ministate.compl.data = compldata;
412 b1e1e41a 2021-07-14 op populate_compl_buffer(complfn, compldata);
415 84b88039 2021-07-12 op base_map = &minibuffer_map;
416 84b88039 2021-07-12 op current_map = &minibuffer_map;
418 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
420 84b88039 2021-07-12 op ministate.donefn = donefn;
421 84b88039 2021-07-12 op ministate.abortfn = abortfn;
422 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
423 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
424 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
425 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
426 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
428 84b88039 2021-07-12 op ministate.history = hist;
429 84b88039 2021-07-12 op ministate.hist_cur = NULL;
430 84b88039 2021-07-12 op ministate.hist_off = 0;
434 84b88039 2021-07-12 op exit_minibuffer(void)
436 e7b982f4 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
437 e7b982f4 2021-07-14 op erase_buffer(&ministate.compl.buffer);
438 b1e1e41a 2021-07-14 op ui_schedule_redraw();
441 84b88039 2021-07-12 op in_minibuffer = 0;
442 84b88039 2021-07-12 op base_map = &global_map;
443 84b88039 2021-07-12 op current_map = &global_map;
447 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
448 84b88039 2021-07-12 op struct tab *data)
452 84b88039 2021-07-12 op if (in_minibuffer) {
457 84b88039 2021-07-12 op yornp_cb = fn;
458 84b88039 2021-07-12 op yornp_data = data;
459 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
460 e5a2797f 2021-07-13 op yornp_abort, NULL, NULL, NULL);
462 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
463 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
464 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
468 84b88039 2021-07-12 op * Not yet "completing", but soon maybe...
471 d1353324 2021-07-13 op completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
472 b1e1e41a 2021-07-14 op struct tab *data)
476 84b88039 2021-07-12 op if (in_minibuffer)
479 84b88039 2021-07-12 op read_cb = fn;
480 84b88039 2021-07-12 op read_data = data;
481 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
482 b1e1e41a 2021-07-14 op &read_history, NULL, NULL);
484 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
485 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
486 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);