2 84b88039 2021-07-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 84b88039 2021-07-12 op * Permission to use, copy, modify, and distribute this software for any
5 84b88039 2021-07-12 op * purpose with or without fee is hereby granted, provided that the above
6 84b88039 2021-07-12 op * copyright notice and this permission notice appear in all copies.
8 84b88039 2021-07-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 84b88039 2021-07-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 84b88039 2021-07-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 84b88039 2021-07-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 84b88039 2021-07-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 84b88039 2021-07-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 84b88039 2021-07-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 84b88039 2021-07-12 op #include <stdlib.h>
18 84b88039 2021-07-12 op #include <string.h>
20 450a89f7 2021-07-12 op #include "minibuffer.h"
22 84b88039 2021-07-12 op static void minibuffer_hist_save_entry(void);
23 84b88039 2021-07-12 op static void minibuffer_self_insert(void);
24 84b88039 2021-07-12 op static void yornp_self_insert(void);
25 84b88039 2021-07-12 op static void yornp_abort(void);
26 84b88039 2021-07-12 op static void read_self_insert(void);
27 84b88039 2021-07-12 op static void read_abort(void);
28 84b88039 2021-07-12 op static void read_select(void);
30 84b88039 2021-07-12 op static void (*yornp_cb)(int, struct tab *);
31 84b88039 2021-07-12 op static struct tab *yornp_data;
33 84b88039 2021-07-12 op static void (*read_cb)(const char*, unsigned int);
34 84b88039 2021-07-12 op static unsigned int read_data;
36 84b88039 2021-07-12 op struct histhead eecmd_history,
41 84b88039 2021-07-12 op struct ministate ministate;
44 84b88039 2021-07-12 op minibuffer_hist_save_entry(void)
46 84b88039 2021-07-12 op struct hist *hist;
48 84b88039 2021-07-12 op if (ministate.history == NULL)
51 84b88039 2021-07-12 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
54 84b88039 2021-07-12 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
56 84b88039 2021-07-12 op if (TAILQ_EMPTY(&ministate.history->head))
57 84b88039 2021-07-12 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
59 84b88039 2021-07-12 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
60 84b88039 2021-07-12 op ministate.history->len++;
64 84b88039 2021-07-12 op * taint the minibuffer cache: if we're currently showing a history
65 84b88039 2021-07-12 op * element, copy that to the current buf and reset the "history
66 84b88039 2021-07-12 op * navigation" thing.
69 84b88039 2021-07-12 op minibuffer_taint_hist(void)
71 84b88039 2021-07-12 op if (ministate.hist_cur == NULL)
74 84b88039 2021-07-12 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
75 84b88039 2021-07-12 op ministate.hist_cur = NULL;
79 84b88039 2021-07-12 op minibuffer_self_insert(void)
81 84b88039 2021-07-12 op char *c, tmp[5] = {0};
84 84b88039 2021-07-12 op minibuffer_taint_hist();
86 84b88039 2021-07-12 op if (thiskey.cp == 0)
89 84b88039 2021-07-12 op len = utf8_encode(thiskey.cp, tmp);
90 84b88039 2021-07-12 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
91 84b88039 2021-07-12 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
94 84b88039 2021-07-12 op memmove(c + len, c, strlen(c)+1);
95 84b88039 2021-07-12 op memcpy(c, tmp, len);
96 84b88039 2021-07-12 op ministate.buffer.cpoff++;
100 84b88039 2021-07-12 op eecmd_self_insert(void)
102 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
103 84b88039 2021-07-12 op !unicode_isgraph(thiskey.cp)) {
104 84b88039 2021-07-12 op global_key_unbound();
108 84b88039 2021-07-12 op minibuffer_self_insert();
112 84b88039 2021-07-12 op eecmd_select(void)
114 84b88039 2021-07-12 op struct cmd *cmd;
116 84b88039 2021-07-12 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
117 84b88039 2021-07-12 op if (!strcmp(cmd->cmd, ministate.buf)) {
118 84b88039 2021-07-12 op exit_minibuffer();
119 84b88039 2021-07-12 op minibuffer_hist_save_entry();
120 84b88039 2021-07-12 op cmd->fn(current_buffer());
125 84b88039 2021-07-12 op message("No match");
129 84b88039 2021-07-12 op ir_self_insert(void)
131 84b88039 2021-07-12 op minibuffer_self_insert();
135 84b88039 2021-07-12 op ir_select(void)
137 84b88039 2021-07-12 op char buf[1025] = {0};
138 84b88039 2021-07-12 op struct phos_uri uri;
139 84b88039 2021-07-12 op struct tab *tab;
141 84b88039 2021-07-12 op tab = current_tab();
143 84b88039 2021-07-12 op exit_minibuffer();
144 84b88039 2021-07-12 op minibuffer_hist_save_entry();
146 84b88039 2021-07-12 op /* a bit ugly but... */
147 84b88039 2021-07-12 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
148 84b88039 2021-07-12 op phos_uri_set_query(&uri, ministate.buf);
149 84b88039 2021-07-12 op phos_serialize_uri(&uri, buf, sizeof(buf));
150 84b88039 2021-07-12 op load_url_in_tab(tab, buf);
154 84b88039 2021-07-12 op lu_self_insert(void)
156 84b88039 2021-07-12 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
157 84b88039 2021-07-12 op !unicode_isgraph(thiskey.key)) {
158 84b88039 2021-07-12 op global_key_unbound();
162 84b88039 2021-07-12 op minibuffer_self_insert();
166 84b88039 2021-07-12 op lu_select(void)
168 84b88039 2021-07-12 op exit_minibuffer();
169 84b88039 2021-07-12 op minibuffer_hist_save_entry();
170 84b88039 2021-07-12 op load_url_in_tab(current_tab(), ministate.buf);
174 84b88039 2021-07-12 op bp_select(void)
176 84b88039 2021-07-12 op exit_minibuffer();
177 84b88039 2021-07-12 op if (*ministate.buf != '\0')
178 84b88039 2021-07-12 op add_to_bookmarks(ministate.buf);
180 84b88039 2021-07-12 op message("Abort.");
184 84b88039 2021-07-12 op yornp_self_insert(void)
186 84b88039 2021-07-12 op if (thiskey.key != 'y' && thiskey.key != 'n') {
187 84b88039 2021-07-12 op message("Please answer y or n");
191 84b88039 2021-07-12 op exit_minibuffer();
192 84b88039 2021-07-12 op yornp_cb(thiskey.key == 'y', yornp_data);
196 84b88039 2021-07-12 op yornp_abort(void)
198 84b88039 2021-07-12 op exit_minibuffer();
199 84b88039 2021-07-12 op yornp_cb(0, yornp_data);
203 84b88039 2021-07-12 op read_self_insert(void)
205 84b88039 2021-07-12 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
206 84b88039 2021-07-12 op global_key_unbound();
210 84b88039 2021-07-12 op minibuffer_self_insert();
214 84b88039 2021-07-12 op read_abort(void)
216 84b88039 2021-07-12 op exit_minibuffer();
217 84b88039 2021-07-12 op read_cb(NULL, read_data);
221 84b88039 2021-07-12 op read_select(void)
223 84b88039 2021-07-12 op exit_minibuffer();
224 84b88039 2021-07-12 op minibuffer_hist_save_entry();
225 84b88039 2021-07-12 op read_cb(ministate.buf, read_data);
229 84b88039 2021-07-12 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
230 84b88039 2021-07-12 op void (*abortfn)(void), struct histhead *hist)
232 84b88039 2021-07-12 op in_minibuffer = 1;
233 84b88039 2021-07-12 op base_map = &minibuffer_map;
234 84b88039 2021-07-12 op current_map = &minibuffer_map;
236 84b88039 2021-07-12 op base_map->unhandled_input = self_insert_fn;
238 84b88039 2021-07-12 op ministate.donefn = donefn;
239 84b88039 2021-07-12 op ministate.abortfn = abortfn;
240 84b88039 2021-07-12 op memset(ministate.buf, 0, sizeof(ministate.buf));
241 84b88039 2021-07-12 op ministate.buffer.current_line = &ministate.vline;
242 84b88039 2021-07-12 op ministate.buffer.current_line->line = ministate.buf;
243 84b88039 2021-07-12 op ministate.buffer.cpoff = 0;
244 84b88039 2021-07-12 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
246 84b88039 2021-07-12 op ministate.history = hist;
247 84b88039 2021-07-12 op ministate.hist_cur = NULL;
248 84b88039 2021-07-12 op ministate.hist_off = 0;
252 84b88039 2021-07-12 op exit_minibuffer(void)
254 84b88039 2021-07-12 op in_minibuffer = 0;
255 84b88039 2021-07-12 op base_map = &global_map;
256 84b88039 2021-07-12 op current_map = &global_map;
260 84b88039 2021-07-12 op yornp(const char *prompt, void (*fn)(int, struct tab*),
261 84b88039 2021-07-12 op struct tab *data)
265 84b88039 2021-07-12 op if (in_minibuffer) {
270 84b88039 2021-07-12 op yornp_cb = fn;
271 84b88039 2021-07-12 op yornp_data = data;
272 84b88039 2021-07-12 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
273 84b88039 2021-07-12 op yornp_abort, NULL);
275 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
276 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
277 84b88039 2021-07-12 op strlcat(ministate.prompt, " (y or n) ", len);
281 84b88039 2021-07-12 op * Not yet "completing", but soon maybe...
284 84b88039 2021-07-12 op completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
285 84b88039 2021-07-12 op unsigned int data)
289 84b88039 2021-07-12 op if (in_minibuffer)
292 84b88039 2021-07-12 op read_cb = fn;
293 84b88039 2021-07-12 op read_data = data;
294 84b88039 2021-07-12 op enter_minibuffer(read_self_insert, read_select, read_abort,
295 84b88039 2021-07-12 op &read_history);
297 84b88039 2021-07-12 op len = sizeof(ministate.prompt);
298 84b88039 2021-07-12 op strlcpy(ministate.prompt, prompt, len);
299 84b88039 2021-07-12 op strlcat(ministate.prompt, ": ", len);