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 (strstr(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 eecmd_self_insert(void)
141 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
142 !unicode_isgraph(thiskey.cp)) {
143 global_key_unbound();
147 minibuffer_self_insert();
155 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
156 if (!strcmp(cmd->cmd, ministate.buf)) {
158 minibuffer_hist_save_entry();
159 cmd->fn(current_buffer());
170 minibuffer_self_insert();
176 char buf[1025] = {0};
183 minibuffer_hist_save_entry();
185 /* a bit ugly but... */
186 memcpy(&uri, &tab->uri, sizeof(tab->uri));
187 phos_uri_set_query(&uri, ministate.buf);
188 phos_serialize_uri(&uri, buf, sizeof(buf));
189 load_url_in_tab(tab, buf);
195 if (thiskey.meta || unicode_isspace(thiskey.key) ||
196 !unicode_isgraph(thiskey.key)) {
197 global_key_unbound();
201 minibuffer_self_insert();
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;
241 yornp_self_insert(void)
243 if (thiskey.key != 'y' && thiskey.key != 'n') {
244 message("Please answer y or n");
249 yornp_cb(thiskey.key == 'y', yornp_data);
256 yornp_cb(0, yornp_data);
260 read_self_insert(void)
262 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
263 global_key_unbound();
267 minibuffer_self_insert();
274 read_cb(NULL, read_data);
281 minibuffer_hist_save_entry();
282 read_cb(ministate.buf, read_data);
286 * TODO: we should collect this asynchronously...
289 populate_compl_buffer(complfn *fn, void *data)
295 void *linedata = NULL;
297 b = &ministate.compl.buffer;
300 while ((s = fn(&data, &linedata)) != NULL) {
301 if ((l = calloc(1, sizeof(*l))) == NULL)
304 l->type = LINE_COMPL;
305 l->meta.data = linedata;
306 if ((l->line = strdup(s)) == NULL)
309 if (TAILQ_EMPTY(&p->head))
310 TAILQ_INSERT_HEAD(&p->head, l, lines);
312 TAILQ_INSERT_TAIL(&p->head, l, lines);
317 if ((l = TAILQ_FIRST(&p->head)) != NULL)
318 l->type = LINE_COMPL_CURRENT;
322 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
323 void (*abortfn)(void), struct histhead *hist,
324 complfn *complfn, void *compldata)
326 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
327 if (in_minibuffer == MB_COMPREAD) {
328 ui_schedule_redraw();
330 ministate.compl.fn = complfn;
331 ministate.compl.data = compldata;
332 populate_compl_buffer(complfn, compldata);
335 base_map = &minibuffer_map;
336 current_map = &minibuffer_map;
338 base_map->unhandled_input = self_insert_fn;
340 ministate.donefn = donefn;
341 ministate.abortfn = abortfn;
342 memset(ministate.buf, 0, sizeof(ministate.buf));
343 ministate.buffer.current_line = &ministate.vline;
344 ministate.buffer.current_line->line = ministate.buf;
345 ministate.buffer.cpoff = 0;
346 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
348 ministate.history = hist;
349 ministate.hist_cur = NULL;
350 ministate.hist_off = 0;
354 exit_minibuffer(void)
356 if (in_minibuffer == MB_COMPREAD) {
357 erase_buffer(&ministate.compl.buffer);
358 ui_schedule_redraw();
362 base_map = &global_map;
363 current_map = &global_map;
367 yornp(const char *prompt, void (*fn)(int, struct tab*),
379 enter_minibuffer(yornp_self_insert, yornp_self_insert,
380 yornp_abort, NULL, NULL, NULL);
382 len = sizeof(ministate.prompt);
383 strlcpy(ministate.prompt, prompt, len);
384 strlcat(ministate.prompt, " (y or n) ", len);
388 * Not yet "completing", but soon maybe...
391 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
401 enter_minibuffer(read_self_insert, read_select, read_abort,
402 &read_history, NULL, NULL);
404 len = sizeof(ministate.prompt);
405 strlcpy(ministate.prompt, prompt, len);
406 strlcat(ministate.prompt, ": ", len);