Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <stdlib.h>
18 #include <string.h>
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,
37 ir_history,
38 lu_history,
39 read_history;
41 struct ministate ministate;
43 static void
44 minibuffer_hist_save_entry(void)
45 {
46 struct hist *hist;
48 if (ministate.history == NULL)
49 return;
51 if ((hist = calloc(1, sizeof(*hist))) == NULL)
52 abort();
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);
58 else
59 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
60 ministate.history->len++;
61 }
63 /*
64 * taint the minibuffer cache: if we're currently showing a history
65 * element, copy that to the current buf and reset the "history
66 * navigation" thing.
67 */
68 void
69 minibuffer_taint_hist(void)
70 {
71 if (ministate.hist_cur == NULL)
72 return;
74 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
75 ministate.hist_cur = NULL;
76 ministate.buffer.current_line->line = ministate.buf;
77 }
79 static void
80 minibuffer_self_insert(void)
81 {
82 char *c, tmp[5] = {0};
83 size_t len;
85 minibuffer_taint_hist();
87 if (thiskey.cp == 0)
88 return;
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)
93 return;
95 memmove(c + len, c, strlen(c)+1);
96 memcpy(c, tmp, len);
97 ministate.buffer.cpoff++;
98 }
100 void
101 eecmd_self_insert(void)
103 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
104 !unicode_isgraph(thiskey.cp)) {
105 global_key_unbound();
106 return;
109 minibuffer_self_insert();
112 void
113 eecmd_select(void)
115 struct cmd *cmd;
117 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
118 if (!strcmp(cmd->cmd, ministate.buf)) {
119 exit_minibuffer();
120 minibuffer_hist_save_entry();
121 cmd->fn(current_buffer());
122 return;
126 message("No match");
129 void
130 ir_self_insert(void)
132 minibuffer_self_insert();
135 void
136 ir_select(void)
138 char buf[1025] = {0};
139 struct phos_uri uri;
140 struct tab *tab;
142 tab = current_tab();
144 exit_minibuffer();
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);
154 void
155 lu_self_insert(void)
157 if (thiskey.meta || unicode_isspace(thiskey.key) ||
158 !unicode_isgraph(thiskey.key)) {
159 global_key_unbound();
160 return;
163 minibuffer_self_insert();
166 void
167 lu_select(void)
169 exit_minibuffer();
170 minibuffer_hist_save_entry();
171 load_url_in_tab(current_tab(), ministate.buf);
174 void
175 bp_select(void)
177 exit_minibuffer();
178 if (*ministate.buf != '\0')
179 add_to_bookmarks(ministate.buf);
180 else
181 message("Abort.");
184 static void
185 yornp_self_insert(void)
187 if (thiskey.key != 'y' && thiskey.key != 'n') {
188 message("Please answer y or n");
189 return;
192 exit_minibuffer();
193 yornp_cb(thiskey.key == 'y', yornp_data);
196 static void
197 yornp_abort(void)
199 exit_minibuffer();
200 yornp_cb(0, yornp_data);
203 static void
204 read_self_insert(void)
206 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
207 global_key_unbound();
208 return;
211 minibuffer_self_insert();
214 static void
215 read_abort(void)
217 exit_minibuffer();
218 read_cb(NULL, read_data);
221 static void
222 read_select(void)
224 exit_minibuffer();
225 minibuffer_hist_save_entry();
226 read_cb(ministate.buf, read_data);
229 void
230 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
231 void (*abortfn)(void), struct histhead *hist)
233 in_minibuffer = 1;
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;
252 void
253 exit_minibuffer(void)
255 in_minibuffer = 0;
256 base_map = &global_map;
257 current_map = &global_map;
260 void
261 yornp(const char *prompt, void (*fn)(int, struct tab*),
262 struct tab *data)
264 size_t len;
266 if (in_minibuffer) {
267 fn(0, data);
268 return;
271 yornp_cb = fn;
272 yornp_data = data;
273 enter_minibuffer(yornp_self_insert, yornp_self_insert,
274 yornp_abort, NULL);
276 len = sizeof(ministate.prompt);
277 strlcpy(ministate.prompt, prompt, len);
278 strlcat(ministate.prompt, " (y or n) ", len);
281 /*
282 * Not yet "completing", but soon maybe...
283 */
284 void
285 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
286 unsigned int data)
288 size_t len;
290 if (in_minibuffer)
291 return;
293 read_cb = fn;
294 read_data = data;
295 enter_minibuffer(read_self_insert, read_select, read_abort,
296 &read_history);
298 len = sizeof(ministate.prompt);
299 strlcpy(ministate.prompt, prompt, len);
300 strlcat(ministate.prompt, ": ", len);