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};
214 minibuffer_hist_save_entry();
216 /* a bit ugly but... */
217 memcpy(&uri, &tab->uri, sizeof(tab->uri));
218 phos_uri_set_query(&uri, ministate.buf);
219 phos_serialize_uri(&uri, buf, sizeof(buf));
220 load_url_in_tab(tab, buf);
227 minibuffer_hist_save_entry();
228 load_url_in_tab(current_tab(), ministate.buf);
235 if (*ministate.buf != '\0')
236 add_to_bookmarks(ministate.buf);
246 if ((tab = minibuffer_metadata()) == NULL) {
247 message("No tab selected");
260 if ((l = minibuffer_metadata()) == NULL) {
261 message("No link selected");
266 load_url_in_tab(current_tab(), l->alt);
270 jump_to_line(struct line *l)
277 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
283 message("Ops, %s error! Please report to %s",
284 __func__, PACKAGE_BUGREPORT);
286 tab->buffer.top_line = vl;
287 tab->buffer.current_line = vl;
296 if ((l = minibuffer_metadata()) == NULL) {
297 message("No line selected");
310 if ((l = minibuffer_metadata()) == NULL) {
311 message("No line selected");
320 yornp_self_insert(void)
322 if (thiskey.key != 'y' && thiskey.key != 'n') {
323 message("Please answer y or n");
328 yornp_cb(thiskey.key == 'y', yornp_data);
335 yornp_cb(0, yornp_data);
339 read_self_insert(void)
341 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
342 global_key_unbound();
346 minibuffer_self_insert();
353 read_cb(NULL, read_data);
360 minibuffer_hist_save_entry();
361 read_cb(ministate.buf, read_data);
365 * TODO: we should collect this asynchronously...
368 populate_compl_buffer(complfn *fn, void *data)
374 void *linedata = NULL;
376 b = &ministate.compl.buffer;
379 while ((s = fn(&data, &linedata)) != NULL) {
380 if ((l = calloc(1, sizeof(*l))) == NULL)
383 l->type = LINE_COMPL;
385 if ((l->line = strdup(s)) == NULL)
388 if (TAILQ_EMPTY(&p->head))
389 TAILQ_INSERT_HEAD(&p->head, l, lines);
391 TAILQ_INSERT_TAIL(&p->head, l, lines);
396 if ((l = TAILQ_FIRST(&p->head)) != NULL)
397 l->type = LINE_COMPL_CURRENT;
401 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
402 void (*abortfn)(void), struct histhead *hist,
403 complfn *complfn, void *compldata)
405 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
406 if (in_minibuffer == MB_COMPREAD) {
407 ui_schedule_redraw();
409 ministate.compl.fn = complfn;
410 ministate.compl.data = compldata;
411 populate_compl_buffer(complfn, compldata);
414 base_map = &minibuffer_map;
415 current_map = &minibuffer_map;
417 base_map->unhandled_input = self_insert_fn;
419 ministate.donefn = donefn;
420 ministate.abortfn = abortfn;
421 memset(ministate.buf, 0, sizeof(ministate.buf));
422 ministate.buffer.current_line = &ministate.vline;
423 ministate.buffer.current_line->line = ministate.buf;
424 ministate.buffer.cpoff = 0;
425 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
427 ministate.history = hist;
428 ministate.hist_cur = NULL;
429 ministate.hist_off = 0;
433 exit_minibuffer(void)
435 if (in_minibuffer == MB_COMPREAD) {
436 erase_buffer(&ministate.compl.buffer);
437 ui_schedule_redraw();
441 base_map = &global_map;
442 current_map = &global_map;
446 yornp(const char *prompt, void (*fn)(int, struct tab*),
458 enter_minibuffer(yornp_self_insert, yornp_self_insert,
459 yornp_abort, NULL, NULL, NULL);
461 len = sizeof(ministate.prompt);
462 strlcpy(ministate.prompt, prompt, len);
463 strlcat(ministate.prompt, " (y or n) ", len);
467 * Not yet "completing", but soon maybe...
470 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
480 enter_minibuffer(read_self_insert, read_select, read_abort,
481 &read_history, NULL, NULL);
483 len = sizeof(ministate.prompt);
484 strlcpy(ministate.prompt, prompt, len);
485 strlcat(ministate.prompt, ": ", len);