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"
21 #include "ui.h"
22 #include "utf8.h"
24 static void minibuffer_hist_save_entry(void);
25 static void minibuffer_self_insert(void);
26 static void yornp_self_insert(void);
27 static void yornp_abort(void);
28 static void read_self_insert(void);
29 static void read_abort(void);
30 static void read_select(void);
32 static void (*yornp_cb)(int, struct tab *);
33 static struct tab *yornp_data;
35 static void (*read_cb)(const char*, unsigned int);
36 static unsigned int read_data;
38 struct histhead eecmd_history,
39 ir_history,
40 lu_history,
41 read_history;
43 struct ministate ministate;
45 static void
46 minibuffer_hist_save_entry(void)
47 {
48 struct hist *hist;
50 if (ministate.history == NULL)
51 return;
53 if ((hist = calloc(1, sizeof(*hist))) == NULL)
54 abort();
56 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
58 if (TAILQ_EMPTY(&ministate.history->head))
59 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
60 else
61 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
62 ministate.history->len++;
63 }
65 /*
66 * taint the minibuffer cache: if we're currently showing a history
67 * element, copy that to the current buf and reset the "history
68 * navigation" thing.
69 */
70 void
71 minibuffer_taint_hist(void)
72 {
73 if (ministate.hist_cur == NULL)
74 return;
76 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
77 ministate.hist_cur = NULL;
78 ministate.buffer.current_line->line = ministate.buf;
79 }
81 static void
82 minibuffer_self_insert(void)
83 {
84 char *c, tmp[5] = {0};
85 size_t len;
87 minibuffer_taint_hist();
89 if (thiskey.cp == 0)
90 return;
92 len = utf8_encode(thiskey.cp, tmp);
93 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
94 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
95 return;
97 memmove(c + len, c, strlen(c)+1);
98 memcpy(c, tmp, len);
99 ministate.buffer.cpoff++;
102 void
103 eecmd_self_insert(void)
105 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
106 !unicode_isgraph(thiskey.cp)) {
107 global_key_unbound();
108 return;
111 minibuffer_self_insert();
114 void
115 eecmd_select(void)
117 struct cmd *cmd;
119 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
120 if (!strcmp(cmd->cmd, ministate.buf)) {
121 exit_minibuffer();
122 minibuffer_hist_save_entry();
123 cmd->fn(current_buffer());
124 return;
128 message("No match");
131 void
132 ir_self_insert(void)
134 minibuffer_self_insert();
137 void
138 ir_select(void)
140 char buf[1025] = {0};
141 struct phos_uri uri;
142 struct tab *tab;
144 tab = current_tab();
146 exit_minibuffer();
147 minibuffer_hist_save_entry();
149 /* a bit ugly but... */
150 memcpy(&uri, &tab->uri, sizeof(tab->uri));
151 phos_uri_set_query(&uri, ministate.buf);
152 phos_serialize_uri(&uri, buf, sizeof(buf));
153 load_url_in_tab(tab, buf);
156 void
157 lu_self_insert(void)
159 if (thiskey.meta || unicode_isspace(thiskey.key) ||
160 !unicode_isgraph(thiskey.key)) {
161 global_key_unbound();
162 return;
165 minibuffer_self_insert();
168 void
169 lu_select(void)
171 exit_minibuffer();
172 minibuffer_hist_save_entry();
173 load_url_in_tab(current_tab(), ministate.buf);
176 void
177 bp_select(void)
179 exit_minibuffer();
180 if (*ministate.buf != '\0')
181 add_to_bookmarks(ministate.buf);
182 else
183 message("Abort.");
186 static void
187 yornp_self_insert(void)
189 if (thiskey.key != 'y' && thiskey.key != 'n') {
190 message("Please answer y or n");
191 return;
194 exit_minibuffer();
195 yornp_cb(thiskey.key == 'y', yornp_data);
198 static void
199 yornp_abort(void)
201 exit_minibuffer();
202 yornp_cb(0, yornp_data);
205 static void
206 read_self_insert(void)
208 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
209 global_key_unbound();
210 return;
213 minibuffer_self_insert();
216 static void
217 read_abort(void)
219 exit_minibuffer();
220 read_cb(NULL, read_data);
223 static void
224 read_select(void)
226 exit_minibuffer();
227 minibuffer_hist_save_entry();
228 read_cb(ministate.buf, read_data);
231 void
232 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
233 void (*abortfn)(void), struct histhead *hist)
235 in_minibuffer = 1;
236 base_map = &minibuffer_map;
237 current_map = &minibuffer_map;
239 base_map->unhandled_input = self_insert_fn;
241 ministate.donefn = donefn;
242 ministate.abortfn = abortfn;
243 memset(ministate.buf, 0, sizeof(ministate.buf));
244 ministate.buffer.current_line = &ministate.vline;
245 ministate.buffer.current_line->line = ministate.buf;
246 ministate.buffer.cpoff = 0;
247 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
249 ministate.history = hist;
250 ministate.hist_cur = NULL;
251 ministate.hist_off = 0;
254 void
255 exit_minibuffer(void)
257 in_minibuffer = 0;
258 base_map = &global_map;
259 current_map = &global_map;
262 void
263 yornp(const char *prompt, void (*fn)(int, struct tab*),
264 struct tab *data)
266 size_t len;
268 if (in_minibuffer) {
269 fn(0, data);
270 return;
273 yornp_cb = fn;
274 yornp_data = data;
275 enter_minibuffer(yornp_self_insert, yornp_self_insert,
276 yornp_abort, NULL);
278 len = sizeof(ministate.prompt);
279 strlcpy(ministate.prompt, prompt, len);
280 strlcat(ministate.prompt, " (y or n) ", len);
283 /*
284 * Not yet "completing", but soon maybe...
285 */
286 void
287 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
288 unsigned int data)
290 size_t len;
292 if (in_minibuffer)
293 return;
295 read_cb = fn;
296 read_data = data;
297 enter_minibuffer(read_self_insert, read_select, read_abort,
298 &read_history);
300 len = sizeof(ministate.prompt);
301 strlcpy(ministate.prompt, prompt, len);
302 strlcat(ministate.prompt, ": ", len);