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*, unsigned int);
36 static unsigned int read_data;
38 struct histhead eecmd_history,
43 struct ministate ministate;
46 minibuffer_hist_save_entry(void)
50 if (ministate.history == NULL)
53 if ((hist = calloc(1, sizeof(*hist))) == NULL)
56 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
58 if (TAILQ_EMPTY(&ministate.history->head))
59 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
61 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
62 ministate.history->len++;
66 * taint the minibuffer cache: if we're currently showing a history
67 * element, copy that to the current buf and reset the "history
71 minibuffer_taint_hist(void)
73 if (ministate.hist_cur == NULL)
76 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
77 ministate.hist_cur = NULL;
78 ministate.buffer.current_line->line = ministate.buf;
82 minibuffer_self_insert(void)
84 char *c, tmp[5] = {0};
87 minibuffer_taint_hist();
92 len = utf8_encode(thiskey.cp, tmp);
93 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
94 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
97 memmove(c + len, c, strlen(c)+1);
99 ministate.buffer.cpoff++;
103 eecmd_self_insert(void)
105 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
106 !unicode_isgraph(thiskey.cp)) {
107 global_key_unbound();
111 minibuffer_self_insert();
119 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
120 if (!strcmp(cmd->cmd, ministate.buf)) {
122 minibuffer_hist_save_entry();
123 cmd->fn(current_buffer());
134 minibuffer_self_insert();
140 char buf[1025] = {0};
147 minibuffer_hist_save_entry();
149 /* a bit ugly but... */
150 memcpy(&uri, &tab->uri, sizeof(tab->uri));
151 phos_uri_set_query(&uri, ministate.buf);
152 phos_serialize_uri(&uri, buf, sizeof(buf));
153 load_url_in_tab(tab, buf);
159 if (thiskey.meta || unicode_isspace(thiskey.key) ||
160 !unicode_isgraph(thiskey.key)) {
161 global_key_unbound();
165 minibuffer_self_insert();
172 minibuffer_hist_save_entry();
173 load_url_in_tab(current_tab(), ministate.buf);
180 if (*ministate.buf != '\0')
181 add_to_bookmarks(ministate.buf);
187 yornp_self_insert(void)
189 if (thiskey.key != 'y' && thiskey.key != 'n') {
190 message("Please answer y or n");
195 yornp_cb(thiskey.key == 'y', yornp_data);
202 yornp_cb(0, yornp_data);
206 read_self_insert(void)
208 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
209 global_key_unbound();
213 minibuffer_self_insert();
220 read_cb(NULL, read_data);
227 minibuffer_hist_save_entry();
228 read_cb(ministate.buf, read_data);
232 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
233 void (*abortfn)(void), struct histhead *hist)
236 base_map = &minibuffer_map;
237 current_map = &minibuffer_map;
239 base_map->unhandled_input = self_insert_fn;
241 ministate.donefn = donefn;
242 ministate.abortfn = abortfn;
243 memset(ministate.buf, 0, sizeof(ministate.buf));
244 ministate.buffer.current_line = &ministate.vline;
245 ministate.buffer.current_line->line = ministate.buf;
246 ministate.buffer.cpoff = 0;
247 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
249 ministate.history = hist;
250 ministate.hist_cur = NULL;
251 ministate.hist_off = 0;
255 exit_minibuffer(void)
258 base_map = &global_map;
259 current_map = &global_map;
263 yornp(const char *prompt, void (*fn)(int, struct tab*),
275 enter_minibuffer(yornp_self_insert, yornp_self_insert,
278 len = sizeof(ministate.prompt);
279 strlcpy(ministate.prompt, prompt, len);
280 strlcat(ministate.prompt, " (y or n) ", len);
284 * Not yet "completing", but soon maybe...
287 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
297 enter_minibuffer(read_self_insert, read_select, read_abort,
300 len = sizeof(ministate.prompt);
301 strlcpy(ministate.prompt, prompt, len);
302 strlcat(ministate.prompt, ": ", len);