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_hist_save_entry(void);
25 static void yornp_self_insert(void);
26 static void yornp_abort(void);
27 static void read_self_insert(void);
28 static void read_abort(void);
29 static void read_select(void);
31 static void (*yornp_cb)(int, struct tab *);
32 static struct tab *yornp_data;
34 static void (*read_cb)(const char*, struct tab *);
35 static struct tab *read_data;
37 struct histhead eecmd_history,
42 struct ministate ministate;
44 struct buffer minibufferwin;
47 * Recompute the visible completions. If add is 1, don't consider the
48 * ones already hidden.
51 recompute_completions(int add)
57 if (in_minibuffer != MB_COMPREAD)
60 b = &ministate.compl.buffer;
61 TAILQ_FOREACH(l, &b->page.head, lines) {
63 if (add && l->flags & L_HIDDEN)
65 if (strcasestr(l->line, ministate.buf) != NULL)
66 l->flags &= ~L_HIDDEN;
71 if (b->current_line == NULL)
72 b->current_line = TAILQ_FIRST(&b->head);
73 b->current_line = adjust_line(b->current_line, b);
76 vl->parent->type = LINE_COMPL_CURRENT;
80 minibuffer_hist_save_entry(void)
84 if (ministate.history == NULL)
87 if ((hist = calloc(1, sizeof(*hist))) == NULL)
90 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
92 if (TAILQ_EMPTY(&ministate.history->head))
93 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
95 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
96 ministate.history->len++;
100 * taint the minibuffer cache: if we're currently showing a history
101 * element, copy that to the current buf and reset the "history
105 minibuffer_taint_hist(void)
107 if (ministate.hist_cur == NULL)
110 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
111 ministate.hist_cur = NULL;
112 ministate.buffer.current_line->line = ministate.buf;
116 minibuffer_self_insert(void)
118 char *c, tmp[5] = {0};
121 minibuffer_taint_hist();
126 len = utf8_encode(thiskey.cp, tmp);
127 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
128 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
131 memmove(c + len, c, strlen(c)+1);
133 ministate.buffer.cpoff++;
135 recompute_completions(1);
139 sensible_self_insert(void)
141 if (thiskey.meta || unicode_isspace(thiskey.key) ||
142 !unicode_isgraph(thiskey.key)) {
143 global_key_unbound();
147 minibuffer_self_insert();
151 eecmd_self_insert(void)
153 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
154 !unicode_isgraph(thiskey.cp)) {
155 global_key_unbound();
159 minibuffer_self_insert();
167 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
168 if (!strcmp(cmd->cmd, ministate.buf)) {
170 minibuffer_hist_save_entry();
171 cmd->fn(current_buffer());
182 minibuffer_self_insert();
188 char buf[1025] = {0};
195 minibuffer_hist_save_entry();
197 /* a bit ugly but... */
198 memcpy(&uri, &tab->uri, sizeof(tab->uri));
199 phos_uri_set_query(&uri, ministate.buf);
200 phos_serialize_uri(&uri, buf, sizeof(buf));
201 load_url_in_tab(tab, buf);
208 minibuffer_hist_save_entry();
209 load_url_in_tab(current_tab(), ministate.buf);
216 if (*ministate.buf != '\0')
217 add_to_bookmarks(ministate.buf);
228 vl = ministate.compl.buffer.current_line;
230 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
231 message("No tab selected");
235 tab = vl->parent->meta.data;
246 vl = ministate.compl.buffer.current_line;
248 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
249 message("No link selected");
253 l = vl->parent->meta.data;
256 load_url_in_tab(current_tab(), l->meta.alt);
266 vl = ministate.compl.buffer.current_line;
268 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
269 message("No line selected");
273 l = vl->parent->meta.data;
278 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
284 message("Ops, swiper error! Please report to %s",
287 tab->buffer.current_line = vl;
291 yornp_self_insert(void)
293 if (thiskey.key != 'y' && thiskey.key != 'n') {
294 message("Please answer y or n");
299 yornp_cb(thiskey.key == 'y', yornp_data);
306 yornp_cb(0, yornp_data);
310 read_self_insert(void)
312 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
313 global_key_unbound();
317 minibuffer_self_insert();
324 read_cb(NULL, read_data);
331 minibuffer_hist_save_entry();
332 read_cb(ministate.buf, read_data);
336 * TODO: we should collect this asynchronously...
339 populate_compl_buffer(complfn *fn, void *data)
345 void *linedata = NULL;
347 b = &ministate.compl.buffer;
350 while ((s = fn(&data, &linedata)) != NULL) {
351 if ((l = calloc(1, sizeof(*l))) == NULL)
354 l->type = LINE_COMPL;
355 l->meta.data = linedata;
356 if ((l->line = strdup(s)) == NULL)
359 if (TAILQ_EMPTY(&p->head))
360 TAILQ_INSERT_HEAD(&p->head, l, lines);
362 TAILQ_INSERT_TAIL(&p->head, l, lines);
367 if ((l = TAILQ_FIRST(&p->head)) != NULL)
368 l->type = LINE_COMPL_CURRENT;
372 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
373 void (*abortfn)(void), struct histhead *hist,
374 complfn *complfn, void *compldata)
376 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
377 if (in_minibuffer == MB_COMPREAD) {
378 ui_schedule_redraw();
380 ministate.compl.fn = complfn;
381 ministate.compl.data = compldata;
382 populate_compl_buffer(complfn, compldata);
385 base_map = &minibuffer_map;
386 current_map = &minibuffer_map;
388 base_map->unhandled_input = self_insert_fn;
390 ministate.donefn = donefn;
391 ministate.abortfn = abortfn;
392 memset(ministate.buf, 0, sizeof(ministate.buf));
393 ministate.buffer.current_line = &ministate.vline;
394 ministate.buffer.current_line->line = ministate.buf;
395 ministate.buffer.cpoff = 0;
396 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
398 ministate.history = hist;
399 ministate.hist_cur = NULL;
400 ministate.hist_off = 0;
404 exit_minibuffer(void)
406 if (in_minibuffer == MB_COMPREAD) {
407 erase_buffer(&ministate.compl.buffer);
408 ui_schedule_redraw();
412 base_map = &global_map;
413 current_map = &global_map;
417 yornp(const char *prompt, void (*fn)(int, struct tab*),
429 enter_minibuffer(yornp_self_insert, yornp_self_insert,
430 yornp_abort, NULL, NULL, NULL);
432 len = sizeof(ministate.prompt);
433 strlcpy(ministate.prompt, prompt, len);
434 strlcat(ministate.prompt, " (y or n) ", len);
438 * Not yet "completing", but soon maybe...
441 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
451 enter_minibuffer(read_self_insert, read_select, read_abort,
452 &read_history, NULL, NULL);
454 len = sizeof(ministate.prompt);
455 strlcpy(ministate.prompt, prompt, len);
456 strlcat(ministate.prompt, ": ", len);