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 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
68 if (l->flags & L_HIDDEN)
70 l->flags &= ~L_HIDDEN;
72 if (!(l->flags & L_HIDDEN))
78 if (b->current_line == NULL)
79 b->current_line = TAILQ_FIRST(&b->head);
80 b->current_line = adjust_line(b->current_line, b);
83 vl->parent->type = LINE_COMPL_CURRENT;
87 minibuffer_metadata(void)
91 vl = ministate.compl.buffer.current_line;
93 if (vl == NULL || vl->parent->flags & L_HIDDEN)
96 return vl->parent->data;
100 minibuffer_hist_save_entry(void)
104 if (ministate.history == NULL)
107 if ((hist = calloc(1, sizeof(*hist))) == NULL)
110 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
112 if (TAILQ_EMPTY(&ministate.history->head))
113 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
115 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
116 ministate.history->len++;
120 * taint the minibuffer cache: if we're currently showing a history
121 * element, copy that to the current buf and reset the "history
125 minibuffer_taint_hist(void)
127 if (ministate.hist_cur == NULL)
130 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
131 ministate.hist_cur = NULL;
132 ministate.buffer.current_line->line = ministate.buf;
136 minibuffer_self_insert(void)
138 char *c, tmp[5] = {0};
141 minibuffer_taint_hist();
146 len = utf8_encode(thiskey.cp, tmp);
147 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
148 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
151 memmove(c + len, c, strlen(c)+1);
153 ministate.buffer.cpoff++;
155 recompute_completions(1);
159 sensible_self_insert(void)
161 if (thiskey.meta || unicode_isspace(thiskey.key) ||
162 !unicode_isgraph(thiskey.key)) {
163 global_key_unbound();
167 minibuffer_self_insert();
171 eecmd_self_insert(void)
173 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
174 !unicode_isgraph(thiskey.cp)) {
175 global_key_unbound();
179 minibuffer_self_insert();
187 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
188 if (!strcmp(cmd->cmd, ministate.buf)) {
190 minibuffer_hist_save_entry();
191 cmd->fn(current_buffer());
202 minibuffer_self_insert();
208 char buf[1025] = {0};
210 struct tab *tab = current_tab;
213 minibuffer_hist_save_entry();
215 /* a bit ugly but... */
216 memcpy(&uri, &tab->uri, sizeof(tab->uri));
217 phos_uri_set_query(&uri, ministate.buf);
218 phos_serialize_uri(&uri, buf, sizeof(buf));
219 load_url_in_tab(tab, buf);
226 minibuffer_hist_save_entry();
227 load_url_in_tab(current_tab, ministate.buf);
234 if (*ministate.buf != '\0')
235 add_to_bookmarks(ministate.buf);
245 if ((tab = minibuffer_metadata()) == NULL) {
246 message("No tab selected");
259 if ((l = minibuffer_metadata()) == NULL) {
260 message("No link selected");
265 load_url_in_tab(current_tab, l->alt);
269 jump_to_line(struct line *l)
272 struct tab *tab = current_tab;
274 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
280 message("Ops, %s error! Please report to %s",
281 __func__, PACKAGE_BUGREPORT);
283 tab->buffer.top_line = vl;
284 tab->buffer.current_line = vl;
293 if ((l = minibuffer_metadata()) == NULL) {
294 message("No line selected");
307 if ((l = minibuffer_metadata()) == NULL) {
308 message("No line selected");
317 yornp_self_insert(void)
319 if (thiskey.key != 'y' && thiskey.key != 'n') {
320 message("Please answer y or n");
325 yornp_cb(thiskey.key == 'y', yornp_data);
332 yornp_cb(0, yornp_data);
336 read_self_insert(void)
338 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
339 global_key_unbound();
343 minibuffer_self_insert();
350 read_cb(NULL, read_data);
357 minibuffer_hist_save_entry();
358 read_cb(ministate.buf, read_data);
362 * TODO: we should collect this asynchronously...
365 populate_compl_buffer(complfn *fn, void *data)
367 const char *s, *descr;
373 b = &ministate.compl.buffer;
378 while ((s = fn(&data, &linedata, &descr)) != NULL) {
379 if ((l = calloc(1, sizeof(*l))) == NULL)
382 l->type = LINE_COMPL;
384 l->alt = (char*)descr;
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);
397 if ((l = TAILQ_FIRST(&p->head)) != NULL)
398 l->type = LINE_COMPL_CURRENT;
402 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
403 void (*abortfn)(void), struct histhead *hist,
404 complfn *complfn, void *compldata)
406 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
407 if (in_minibuffer == MB_COMPREAD) {
408 ui_schedule_redraw();
410 ministate.compl.fn = complfn;
411 ministate.compl.data = compldata;
412 populate_compl_buffer(complfn, compldata);
415 base_map = &minibuffer_map;
416 current_map = &minibuffer_map;
418 base_map->unhandled_input = self_insert_fn;
420 ministate.donefn = donefn;
421 ministate.abortfn = abortfn;
422 memset(ministate.buf, 0, sizeof(ministate.buf));
423 ministate.buffer.current_line = &ministate.vline;
424 ministate.buffer.current_line->line = ministate.buf;
425 ministate.buffer.cpoff = 0;
426 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
428 ministate.history = hist;
429 ministate.hist_cur = NULL;
430 ministate.hist_off = 0;
434 exit_minibuffer(void)
436 if (in_minibuffer == MB_COMPREAD) {
437 erase_buffer(&ministate.compl.buffer);
438 ui_schedule_redraw();
442 base_map = &global_map;
443 current_map = &global_map;
447 yornp(const char *prompt, void (*fn)(int, struct tab*),
459 enter_minibuffer(yornp_self_insert, yornp_self_insert,
460 yornp_abort, NULL, NULL, NULL);
462 len = sizeof(ministate.prompt);
463 strlcpy(ministate.prompt, prompt, len);
464 strlcat(ministate.prompt, " (y or n) ", len);
468 * Not yet "completing", but soon maybe...
471 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
481 enter_minibuffer(read_self_insert, read_select, read_abort,
482 &read_history, NULL, NULL);
484 len = sizeof(ministate.prompt);
485 strlcpy(ministate.prompt, prompt, len);
486 strlcat(ministate.prompt, ": ", len);