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"
23 static void minibuffer_hist_save_entry(void);
24 static void minibuffer_self_insert(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*, unsigned int);
35 static unsigned int read_data;
37 struct histhead eecmd_history,
42 struct ministate ministate;
45 minibuffer_hist_save_entry(void)
49 if (ministate.history == NULL)
52 if ((hist = calloc(1, sizeof(*hist))) == NULL)
55 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
57 if (TAILQ_EMPTY(&ministate.history->head))
58 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
60 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
61 ministate.history->len++;
65 * taint the minibuffer cache: if we're currently showing a history
66 * element, copy that to the current buf and reset the "history
70 minibuffer_taint_hist(void)
72 if (ministate.hist_cur == NULL)
75 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
76 ministate.hist_cur = NULL;
77 ministate.buffer.current_line->line = ministate.buf;
81 minibuffer_self_insert(void)
83 char *c, tmp[5] = {0};
86 minibuffer_taint_hist();
91 len = utf8_encode(thiskey.cp, tmp);
92 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
93 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
96 memmove(c + len, c, strlen(c)+1);
98 ministate.buffer.cpoff++;
102 eecmd_self_insert(void)
104 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
105 !unicode_isgraph(thiskey.cp)) {
106 global_key_unbound();
110 minibuffer_self_insert();
118 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
119 if (!strcmp(cmd->cmd, ministate.buf)) {
121 minibuffer_hist_save_entry();
122 cmd->fn(current_buffer());
133 minibuffer_self_insert();
139 char buf[1025] = {0};
146 minibuffer_hist_save_entry();
148 /* a bit ugly but... */
149 memcpy(&uri, &tab->uri, sizeof(tab->uri));
150 phos_uri_set_query(&uri, ministate.buf);
151 phos_serialize_uri(&uri, buf, sizeof(buf));
152 load_url_in_tab(tab, buf);
158 if (thiskey.meta || unicode_isspace(thiskey.key) ||
159 !unicode_isgraph(thiskey.key)) {
160 global_key_unbound();
164 minibuffer_self_insert();
171 minibuffer_hist_save_entry();
172 load_url_in_tab(current_tab(), ministate.buf);
179 if (*ministate.buf != '\0')
180 add_to_bookmarks(ministate.buf);
186 yornp_self_insert(void)
188 if (thiskey.key != 'y' && thiskey.key != 'n') {
189 message("Please answer y or n");
194 yornp_cb(thiskey.key == 'y', yornp_data);
201 yornp_cb(0, yornp_data);
205 read_self_insert(void)
207 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
208 global_key_unbound();
212 minibuffer_self_insert();
219 read_cb(NULL, read_data);
226 minibuffer_hist_save_entry();
227 read_cb(ministate.buf, read_data);
231 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
232 void (*abortfn)(void), struct histhead *hist)
235 base_map = &minibuffer_map;
236 current_map = &minibuffer_map;
238 base_map->unhandled_input = self_insert_fn;
240 ministate.donefn = donefn;
241 ministate.abortfn = abortfn;
242 memset(ministate.buf, 0, sizeof(ministate.buf));
243 ministate.buffer.current_line = &ministate.vline;
244 ministate.buffer.current_line->line = ministate.buf;
245 ministate.buffer.cpoff = 0;
246 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
248 ministate.history = hist;
249 ministate.hist_cur = NULL;
250 ministate.hist_off = 0;
254 exit_minibuffer(void)
257 base_map = &global_map;
258 current_map = &global_map;
262 yornp(const char *prompt, void (*fn)(int, struct tab*),
274 enter_minibuffer(yornp_self_insert, yornp_self_insert,
277 len = sizeof(ministate.prompt);
278 strlcpy(ministate.prompt, prompt, len);
279 strlcat(ministate.prompt, " (y or n) ", len);
283 * Not yet "completing", but soon maybe...
286 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
296 enter_minibuffer(read_self_insert, read_select, read_abort,
299 len = sizeof(ministate.prompt);
300 strlcpy(ministate.prompt, prompt, len);
301 strlcat(ministate.prompt, ": ", len);