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"
22 static void minibuffer_hist_save_entry(void);
23 static void minibuffer_self_insert(void);
24 static void yornp_self_insert(void);
25 static void yornp_abort(void);
26 static void read_self_insert(void);
27 static void read_abort(void);
28 static void read_select(void);
30 static void (*yornp_cb)(int, struct tab *);
31 static struct tab *yornp_data;
33 static void (*read_cb)(const char*, unsigned int);
34 static unsigned int read_data;
36 struct histhead eecmd_history,
41 struct ministate ministate;
44 minibuffer_hist_save_entry(void)
48 if (ministate.history == NULL)
51 if ((hist = calloc(1, sizeof(*hist))) == NULL)
54 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
56 if (TAILQ_EMPTY(&ministate.history->head))
57 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
59 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
60 ministate.history->len++;
64 * taint the minibuffer cache: if we're currently showing a history
65 * element, copy that to the current buf and reset the "history
69 minibuffer_taint_hist(void)
71 if (ministate.hist_cur == NULL)
74 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
75 ministate.hist_cur = NULL;
79 minibuffer_self_insert(void)
81 char *c, tmp[5] = {0};
84 minibuffer_taint_hist();
89 len = utf8_encode(thiskey.cp, tmp);
90 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
91 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
94 memmove(c + len, c, strlen(c)+1);
96 ministate.buffer.cpoff++;
100 eecmd_self_insert(void)
102 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
103 !unicode_isgraph(thiskey.cp)) {
104 global_key_unbound();
108 minibuffer_self_insert();
116 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
117 if (!strcmp(cmd->cmd, ministate.buf)) {
119 minibuffer_hist_save_entry();
120 cmd->fn(current_buffer());
131 minibuffer_self_insert();
137 char buf[1025] = {0};
144 minibuffer_hist_save_entry();
146 /* a bit ugly but... */
147 memcpy(&uri, &tab->uri, sizeof(tab->uri));
148 phos_uri_set_query(&uri, ministate.buf);
149 phos_serialize_uri(&uri, buf, sizeof(buf));
150 load_url_in_tab(tab, buf);
156 if (thiskey.meta || unicode_isspace(thiskey.key) ||
157 !unicode_isgraph(thiskey.key)) {
158 global_key_unbound();
162 minibuffer_self_insert();
169 minibuffer_hist_save_entry();
170 load_url_in_tab(current_tab(), ministate.buf);
177 if (*ministate.buf != '\0')
178 add_to_bookmarks(ministate.buf);
184 yornp_self_insert(void)
186 if (thiskey.key != 'y' && thiskey.key != 'n') {
187 message("Please answer y or n");
192 yornp_cb(thiskey.key == 'y', yornp_data);
199 yornp_cb(0, yornp_data);
203 read_self_insert(void)
205 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
206 global_key_unbound();
210 minibuffer_self_insert();
217 read_cb(NULL, read_data);
224 minibuffer_hist_save_entry();
225 read_cb(ministate.buf, read_data);
229 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
230 void (*abortfn)(void), struct histhead *hist)
233 base_map = &minibuffer_map;
234 current_map = &minibuffer_map;
236 base_map->unhandled_input = self_insert_fn;
238 ministate.donefn = donefn;
239 ministate.abortfn = abortfn;
240 memset(ministate.buf, 0, sizeof(ministate.buf));
241 ministate.buffer.current_line = &ministate.vline;
242 ministate.buffer.current_line->line = ministate.buf;
243 ministate.buffer.cpoff = 0;
244 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
246 ministate.history = hist;
247 ministate.hist_cur = NULL;
248 ministate.hist_off = 0;
252 exit_minibuffer(void)
255 base_map = &global_map;
256 current_map = &global_map;
260 yornp(const char *prompt, void (*fn)(int, struct tab*),
272 enter_minibuffer(yornp_self_insert, yornp_self_insert,
275 len = sizeof(ministate.prompt);
276 strlcpy(ministate.prompt, prompt, len);
277 strlcat(ministate.prompt, " (y or n) ", len);
281 * Not yet "completing", but soon maybe...
284 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
294 enter_minibuffer(read_self_insert, read_select, read_abort,
297 len = sizeof(ministate.prompt);
298 strlcpy(ministate.prompt, prompt, len);
299 strlcat(ministate.prompt, ": ", len);