2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "minibuffer.h"
24 static void *minibuffer_metadata(void);
25 static void minibuffer_hist_save_entry(void);
26 static void yornp_self_insert(void);
27 static void yornp_abort(void);
28 static void read_self_insert(void);
29 static void read_abort(void);
30 static void read_select(void);
32 static void (*yornp_cb)(int, struct tab *);
33 static struct tab *yornp_data;
35 static void (*read_cb)(const char*, struct tab *);
36 static struct tab *read_data;
38 struct histhead eecmd_history,
43 struct ministate ministate;
45 struct buffer minibufferwin;
48 * Recompute the visible completions. If add is 1, don't consider the
49 * ones already hidden.
52 recompute_completions(int add)
58 if (in_minibuffer != MB_COMPREAD)
61 b = &ministate.compl.buffer;
62 TAILQ_FOREACH(l, &b->page.head, lines) {
64 if (add && l->flags & L_HIDDEN)
66 if (strcasestr(l->line, ministate.buf) != NULL) {
67 if (l->flags & L_HIDDEN)
69 l->flags &= ~L_HIDDEN;
71 if (!(l->flags & L_HIDDEN))
77 if (b->current_line == NULL)
78 b->current_line = TAILQ_FIRST(&b->head);
79 b->current_line = adjust_line(b->current_line, b);
82 vl->parent->type = LINE_COMPL_CURRENT;
86 minibuffer_metadata(void)
90 vl = ministate.compl.buffer.current_line;
92 if (vl == NULL || vl->parent->flags & L_HIDDEN)
95 return vl->parent->data;
99 minibuffer_hist_save_entry(void)
103 if (ministate.history == NULL)
106 if ((hist = calloc(1, sizeof(*hist))) == NULL)
109 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
111 if (TAILQ_EMPTY(&ministate.history->head))
112 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
114 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
115 ministate.history->len++;
119 * taint the minibuffer cache: if we're currently showing a history
120 * element, copy that to the current buf and reset the "history
124 minibuffer_taint_hist(void)
126 if (ministate.hist_cur == NULL)
129 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
130 ministate.hist_cur = NULL;
131 ministate.buffer.current_line->line = ministate.buf;
135 minibuffer_self_insert(void)
137 char *c, tmp[5] = {0};
140 minibuffer_taint_hist();
145 len = utf8_encode(thiskey.cp, tmp);
146 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
147 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
150 memmove(c + len, c, strlen(c)+1);
152 ministate.buffer.cpoff++;
154 recompute_completions(1);
158 sensible_self_insert(void)
160 if (thiskey.meta || unicode_isspace(thiskey.key) ||
161 !unicode_isgraph(thiskey.key)) {
162 global_key_unbound();
166 minibuffer_self_insert();
170 eecmd_self_insert(void)
172 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
173 !unicode_isgraph(thiskey.cp)) {
174 global_key_unbound();
178 minibuffer_self_insert();
186 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
187 if (!strcmp(cmd->cmd, ministate.buf)) {
189 minibuffer_hist_save_entry();
190 cmd->fn(current_buffer());
201 minibuffer_self_insert();
207 char buf[1025] = {0};
209 struct tab *tab = current_tab;
212 minibuffer_hist_save_entry();
214 /* a bit ugly but... */
215 memcpy(&uri, &tab->uri, sizeof(tab->uri));
216 phos_uri_set_query(&uri, ministate.buf);
217 phos_serialize_uri(&uri, buf, sizeof(buf));
218 load_url_in_tab(tab, buf);
225 minibuffer_hist_save_entry();
226 load_url_in_tab(current_tab, ministate.buf);
233 if (*ministate.buf != '\0')
234 add_to_bookmarks(ministate.buf);
244 if ((tab = minibuffer_metadata()) == NULL) {
245 message("No tab selected");
258 if ((l = minibuffer_metadata()) == NULL) {
259 message("No link selected");
264 load_url_in_tab(current_tab, l->alt);
268 jump_to_line(struct line *l)
271 struct tab *tab = current_tab;
273 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
279 message("Ops, %s error! Please report to %s",
280 __func__, PACKAGE_BUGREPORT);
282 tab->buffer.top_line = vl;
283 tab->buffer.current_line = vl;
292 if ((l = minibuffer_metadata()) == NULL) {
293 message("No line selected");
306 if ((l = minibuffer_metadata()) == NULL) {
307 message("No line selected");
316 yornp_self_insert(void)
318 if (thiskey.key != 'y' && thiskey.key != 'n') {
319 message("Please answer y or n");
324 yornp_cb(thiskey.key == 'y', yornp_data);
331 yornp_cb(0, yornp_data);
335 read_self_insert(void)
337 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
338 global_key_unbound();
342 minibuffer_self_insert();
349 read_cb(NULL, read_data);
356 minibuffer_hist_save_entry();
357 read_cb(ministate.buf, read_data);
361 * TODO: we should collect this asynchronously...
364 populate_compl_buffer(complfn *fn, void *data)
370 void *linedata = NULL;
372 b = &ministate.compl.buffer;
375 while ((s = fn(&data, &linedata)) != NULL) {
376 if ((l = calloc(1, sizeof(*l))) == NULL)
379 l->type = LINE_COMPL;
381 if ((l->line = strdup(s)) == NULL)
384 if (TAILQ_EMPTY(&p->head))
385 TAILQ_INSERT_HEAD(&p->head, l, lines);
387 TAILQ_INSERT_TAIL(&p->head, l, lines);
392 if ((l = TAILQ_FIRST(&p->head)) != NULL)
393 l->type = LINE_COMPL_CURRENT;
397 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
398 void (*abortfn)(void), struct histhead *hist,
399 complfn *complfn, void *compldata)
401 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
402 if (in_minibuffer == MB_COMPREAD) {
403 ui_schedule_redraw();
405 ministate.compl.fn = complfn;
406 ministate.compl.data = compldata;
407 populate_compl_buffer(complfn, compldata);
410 base_map = &minibuffer_map;
411 current_map = &minibuffer_map;
413 base_map->unhandled_input = self_insert_fn;
415 ministate.donefn = donefn;
416 ministate.abortfn = abortfn;
417 memset(ministate.buf, 0, sizeof(ministate.buf));
418 ministate.buffer.current_line = &ministate.vline;
419 ministate.buffer.current_line->line = ministate.buf;
420 ministate.buffer.cpoff = 0;
421 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
423 ministate.history = hist;
424 ministate.hist_cur = NULL;
425 ministate.hist_off = 0;
429 exit_minibuffer(void)
431 if (in_minibuffer == MB_COMPREAD) {
432 erase_buffer(&ministate.compl.buffer);
433 ui_schedule_redraw();
437 base_map = &global_map;
438 current_map = &global_map;
442 yornp(const char *prompt, void (*fn)(int, struct tab*),
454 enter_minibuffer(yornp_self_insert, yornp_self_insert,
455 yornp_abort, NULL, NULL, NULL);
457 len = sizeof(ministate.prompt);
458 strlcpy(ministate.prompt, prompt, len);
459 strlcat(ministate.prompt, " (y or n) ", len);
463 * Not yet "completing", but soon maybe...
466 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
476 enter_minibuffer(read_self_insert, read_select, read_abort,
477 &read_history, NULL, NULL);
479 len = sizeof(ministate.prompt);
480 strlcpy(ministate.prompt, prompt, len);
481 strlcat(ministate.prompt, ": ", len);