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.current_line = vl;
294 if ((l = minibuffer_metadata()) == NULL) {
295 message("No line selected");
308 if ((l = minibuffer_metadata()) == NULL) {
309 message("No line selected");
318 yornp_self_insert(void)
320 if (thiskey.key != 'y' && thiskey.key != 'n') {
321 message("Please answer y or n");
326 yornp_cb(thiskey.key == 'y', yornp_data);
333 yornp_cb(0, yornp_data);
337 read_self_insert(void)
339 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
340 global_key_unbound();
344 minibuffer_self_insert();
351 read_cb(NULL, read_data);
358 minibuffer_hist_save_entry();
359 read_cb(ministate.buf, read_data);
363 * TODO: we should collect this asynchronously...
366 populate_compl_buffer(complfn *fn, void *data)
372 void *linedata = NULL;
374 b = &ministate.compl.buffer;
377 while ((s = fn(&data, &linedata)) != NULL) {
378 if ((l = calloc(1, sizeof(*l))) == NULL)
381 l->type = LINE_COMPL;
383 if ((l->line = strdup(s)) == NULL)
386 if (TAILQ_EMPTY(&p->head))
387 TAILQ_INSERT_HEAD(&p->head, l, lines);
389 TAILQ_INSERT_TAIL(&p->head, l, lines);
394 if ((l = TAILQ_FIRST(&p->head)) != NULL)
395 l->type = LINE_COMPL_CURRENT;
399 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
400 void (*abortfn)(void), struct histhead *hist,
401 complfn *complfn, void *compldata)
403 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
404 if (in_minibuffer == MB_COMPREAD) {
405 ui_schedule_redraw();
407 ministate.compl.fn = complfn;
408 ministate.compl.data = compldata;
409 populate_compl_buffer(complfn, compldata);
412 base_map = &minibuffer_map;
413 current_map = &minibuffer_map;
415 base_map->unhandled_input = self_insert_fn;
417 ministate.donefn = donefn;
418 ministate.abortfn = abortfn;
419 memset(ministate.buf, 0, sizeof(ministate.buf));
420 ministate.buffer.current_line = &ministate.vline;
421 ministate.buffer.current_line->line = ministate.buf;
422 ministate.buffer.cpoff = 0;
423 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
425 ministate.history = hist;
426 ministate.hist_cur = NULL;
427 ministate.hist_off = 0;
431 exit_minibuffer(void)
433 if (in_minibuffer == MB_COMPREAD) {
434 erase_buffer(&ministate.compl.buffer);
435 ui_schedule_redraw();
439 base_map = &global_map;
440 current_map = &global_map;
444 yornp(const char *prompt, void (*fn)(int, struct tab*),
456 enter_minibuffer(yornp_self_insert, yornp_self_insert,
457 yornp_abort, NULL, NULL, NULL);
459 len = sizeof(ministate.prompt);
460 strlcpy(ministate.prompt, prompt, len);
461 strlcat(ministate.prompt, " (y or n) ", len);
465 * Not yet "completing", but soon maybe...
468 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
478 enter_minibuffer(read_self_insert, read_select, read_abort,
479 &read_history, NULL, NULL);
481 len = sizeof(ministate.prompt);
482 strlcpy(ministate.prompt, prompt, len);
483 strlcat(ministate.prompt, ": ", len);