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;
76 ministate.buffer.current_line->line = ministate.buf;
80 minibuffer_self_insert(void)
82 char *c, tmp[5] = {0};
85 minibuffer_taint_hist();
90 len = utf8_encode(thiskey.cp, tmp);
91 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
92 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
95 memmove(c + len, c, strlen(c)+1);
97 ministate.buffer.cpoff++;
101 eecmd_self_insert(void)
103 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
104 !unicode_isgraph(thiskey.cp)) {
105 global_key_unbound();
109 minibuffer_self_insert();
117 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
118 if (!strcmp(cmd->cmd, ministate.buf)) {
120 minibuffer_hist_save_entry();
121 cmd->fn(current_buffer());
132 minibuffer_self_insert();
138 char buf[1025] = {0};
145 minibuffer_hist_save_entry();
147 /* a bit ugly but... */
148 memcpy(&uri, &tab->uri, sizeof(tab->uri));
149 phos_uri_set_query(&uri, ministate.buf);
150 phos_serialize_uri(&uri, buf, sizeof(buf));
151 load_url_in_tab(tab, buf);
157 if (thiskey.meta || unicode_isspace(thiskey.key) ||
158 !unicode_isgraph(thiskey.key)) {
159 global_key_unbound();
163 minibuffer_self_insert();
170 minibuffer_hist_save_entry();
171 load_url_in_tab(current_tab(), ministate.buf);
178 if (*ministate.buf != '\0')
179 add_to_bookmarks(ministate.buf);
185 yornp_self_insert(void)
187 if (thiskey.key != 'y' && thiskey.key != 'n') {
188 message("Please answer y or n");
193 yornp_cb(thiskey.key == 'y', yornp_data);
200 yornp_cb(0, yornp_data);
204 read_self_insert(void)
206 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
207 global_key_unbound();
211 minibuffer_self_insert();
218 read_cb(NULL, read_data);
225 minibuffer_hist_save_entry();
226 read_cb(ministate.buf, read_data);
230 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
231 void (*abortfn)(void), struct histhead *hist)
234 base_map = &minibuffer_map;
235 current_map = &minibuffer_map;
237 base_map->unhandled_input = self_insert_fn;
239 ministate.donefn = donefn;
240 ministate.abortfn = abortfn;
241 memset(ministate.buf, 0, sizeof(ministate.buf));
242 ministate.buffer.current_line = &ministate.vline;
243 ministate.buffer.current_line->line = ministate.buf;
244 ministate.buffer.cpoff = 0;
245 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
247 ministate.history = hist;
248 ministate.hist_cur = NULL;
249 ministate.hist_off = 0;
253 exit_minibuffer(void)
256 base_map = &global_map;
257 current_map = &global_map;
261 yornp(const char *prompt, void (*fn)(int, struct tab*),
273 enter_minibuffer(yornp_self_insert, yornp_self_insert,
276 len = sizeof(ministate.prompt);
277 strlcpy(ministate.prompt, prompt, len);
278 strlcat(ministate.prompt, " (y or n) ", len);
282 * Not yet "completing", but soon maybe...
285 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
295 enter_minibuffer(read_self_insert, read_select, read_abort,
298 len = sizeof(ministate.prompt);
299 strlcpy(ministate.prompt, prompt, len);
300 strlcat(ministate.prompt, ": ", len);