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 minibuffer_self_insert(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)
56 if (in_minibuffer != MB_COMPREAD)
59 TAILQ_FOREACH(l, &ministate.compl.buffer.page.head, lines) {
60 if (add && l->flags & L_HIDDEN)
62 if (strstr(l->line, ministate.buf) != NULL)
63 l->flags &= ~L_HIDDEN;
70 minibuffer_hist_save_entry(void)
74 if (ministate.history == NULL)
77 if ((hist = calloc(1, sizeof(*hist))) == NULL)
80 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
82 if (TAILQ_EMPTY(&ministate.history->head))
83 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
85 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
86 ministate.history->len++;
90 * taint the minibuffer cache: if we're currently showing a history
91 * element, copy that to the current buf and reset the "history
95 minibuffer_taint_hist(void)
97 if (ministate.hist_cur == NULL)
100 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
101 ministate.hist_cur = NULL;
102 ministate.buffer.current_line->line = ministate.buf;
106 minibuffer_self_insert(void)
108 char *c, tmp[5] = {0};
111 minibuffer_taint_hist();
116 len = utf8_encode(thiskey.cp, tmp);
117 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
118 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
121 memmove(c + len, c, strlen(c)+1);
123 ministate.buffer.cpoff++;
125 recompute_completions(1);
129 eecmd_self_insert(void)
131 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
132 !unicode_isgraph(thiskey.cp)) {
133 global_key_unbound();
137 minibuffer_self_insert();
145 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
146 if (!strcmp(cmd->cmd, ministate.buf)) {
148 minibuffer_hist_save_entry();
149 cmd->fn(current_buffer());
160 minibuffer_self_insert();
166 char buf[1025] = {0};
173 minibuffer_hist_save_entry();
175 /* a bit ugly but... */
176 memcpy(&uri, &tab->uri, sizeof(tab->uri));
177 phos_uri_set_query(&uri, ministate.buf);
178 phos_serialize_uri(&uri, buf, sizeof(buf));
179 load_url_in_tab(tab, buf);
185 if (thiskey.meta || unicode_isspace(thiskey.key) ||
186 !unicode_isgraph(thiskey.key)) {
187 global_key_unbound();
191 minibuffer_self_insert();
198 minibuffer_hist_save_entry();
199 load_url_in_tab(current_tab(), ministate.buf);
206 if (*ministate.buf != '\0')
207 add_to_bookmarks(ministate.buf);
213 yornp_self_insert(void)
215 if (thiskey.key != 'y' && thiskey.key != 'n') {
216 message("Please answer y or n");
221 yornp_cb(thiskey.key == 'y', yornp_data);
228 yornp_cb(0, yornp_data);
232 read_self_insert(void)
234 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
235 global_key_unbound();
239 minibuffer_self_insert();
246 read_cb(NULL, read_data);
253 minibuffer_hist_save_entry();
254 read_cb(ministate.buf, read_data);
258 * Just like erase_buffer, but don't call free(l->line);
261 erase_compl_buffer(struct buffer *buffer)
267 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
268 TAILQ_REMOVE(&buffer->page.head, l, lines);
269 /* don't free l->line! */
275 * TODO: we should collect this asynchronously...
278 populate_compl_buffer(complfn *fn, void *data)
285 b = &ministate.compl.buffer;
288 while ((s = fn(&data)) != NULL) {
289 if ((l = calloc(1, sizeof(*l))) == NULL)
295 if (TAILQ_EMPTY(&p->head))
296 TAILQ_INSERT_HEAD(&p->head, l, lines);
298 TAILQ_INSERT_TAIL(&p->head, l, lines);
303 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
304 void (*abortfn)(void), struct histhead *hist,
305 complfn *complfn, void *compldata)
307 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
308 if (in_minibuffer == MB_COMPREAD) {
309 ui_schedule_redraw();
311 erase_compl_buffer(&ministate.compl.buffer);
313 ministate.compl.fn = complfn;
314 ministate.compl.data = compldata;
315 populate_compl_buffer(complfn, compldata);
318 base_map = &minibuffer_map;
319 current_map = &minibuffer_map;
321 base_map->unhandled_input = self_insert_fn;
323 ministate.donefn = donefn;
324 ministate.abortfn = abortfn;
325 memset(ministate.buf, 0, sizeof(ministate.buf));
326 ministate.buffer.current_line = &ministate.vline;
327 ministate.buffer.current_line->line = ministate.buf;
328 ministate.buffer.cpoff = 0;
329 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
331 ministate.history = hist;
332 ministate.hist_cur = NULL;
333 ministate.hist_off = 0;
337 exit_minibuffer(void)
339 if (in_minibuffer == MB_COMPREAD)
340 ui_schedule_redraw();
343 base_map = &global_map;
344 current_map = &global_map;
348 yornp(const char *prompt, void (*fn)(int, struct tab*),
360 enter_minibuffer(yornp_self_insert, yornp_self_insert,
361 yornp_abort, NULL, NULL, NULL);
363 len = sizeof(ministate.prompt);
364 strlcpy(ministate.prompt, prompt, len);
365 strlcat(ministate.prompt, " (y or n) ", len);
369 * Not yet "completing", but soon maybe...
372 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
382 enter_minibuffer(read_self_insert, read_select, read_abort,
383 &read_history, NULL, NULL);
385 len = sizeof(ministate.prompt);
386 strlcpy(ministate.prompt, prompt, len);
387 strlcat(ministate.prompt, ": ", len);