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"
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,
38 ir_history,
39 lu_history,
40 read_history;
42 struct ministate ministate;
44 static void
45 minibuffer_hist_save_entry(void)
46 {
47 struct hist *hist;
49 if (ministate.history == NULL)
50 return;
52 if ((hist = calloc(1, sizeof(*hist))) == NULL)
53 abort();
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);
59 else
60 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
61 ministate.history->len++;
62 }
64 /*
65 * taint the minibuffer cache: if we're currently showing a history
66 * element, copy that to the current buf and reset the "history
67 * navigation" thing.
68 */
69 void
70 minibuffer_taint_hist(void)
71 {
72 if (ministate.hist_cur == NULL)
73 return;
75 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
76 ministate.hist_cur = NULL;
77 ministate.buffer.current_line->line = ministate.buf;
78 }
80 static void
81 minibuffer_self_insert(void)
82 {
83 char *c, tmp[5] = {0};
84 size_t len;
86 minibuffer_taint_hist();
88 if (thiskey.cp == 0)
89 return;
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)
94 return;
96 memmove(c + len, c, strlen(c)+1);
97 memcpy(c, tmp, len);
98 ministate.buffer.cpoff++;
99 }
101 void
102 eecmd_self_insert(void)
104 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
105 !unicode_isgraph(thiskey.cp)) {
106 global_key_unbound();
107 return;
110 minibuffer_self_insert();
113 void
114 eecmd_select(void)
116 struct cmd *cmd;
118 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
119 if (!strcmp(cmd->cmd, ministate.buf)) {
120 exit_minibuffer();
121 minibuffer_hist_save_entry();
122 cmd->fn(current_buffer());
123 return;
127 message("No match");
130 void
131 ir_self_insert(void)
133 minibuffer_self_insert();
136 void
137 ir_select(void)
139 char buf[1025] = {0};
140 struct phos_uri uri;
141 struct tab *tab;
143 tab = current_tab();
145 exit_minibuffer();
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);
155 void
156 lu_self_insert(void)
158 if (thiskey.meta || unicode_isspace(thiskey.key) ||
159 !unicode_isgraph(thiskey.key)) {
160 global_key_unbound();
161 return;
164 minibuffer_self_insert();
167 void
168 lu_select(void)
170 exit_minibuffer();
171 minibuffer_hist_save_entry();
172 load_url_in_tab(current_tab(), ministate.buf);
175 void
176 bp_select(void)
178 exit_minibuffer();
179 if (*ministate.buf != '\0')
180 add_to_bookmarks(ministate.buf);
181 else
182 message("Abort.");
185 static void
186 yornp_self_insert(void)
188 if (thiskey.key != 'y' && thiskey.key != 'n') {
189 message("Please answer y or n");
190 return;
193 exit_minibuffer();
194 yornp_cb(thiskey.key == 'y', yornp_data);
197 static void
198 yornp_abort(void)
200 exit_minibuffer();
201 yornp_cb(0, yornp_data);
204 static void
205 read_self_insert(void)
207 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
208 global_key_unbound();
209 return;
212 minibuffer_self_insert();
215 static void
216 read_abort(void)
218 exit_minibuffer();
219 read_cb(NULL, read_data);
222 static void
223 read_select(void)
225 exit_minibuffer();
226 minibuffer_hist_save_entry();
227 read_cb(ministate.buf, read_data);
230 void
231 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
232 void (*abortfn)(void), struct histhead *hist)
234 in_minibuffer = 1;
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;
253 void
254 exit_minibuffer(void)
256 in_minibuffer = 0;
257 base_map = &global_map;
258 current_map = &global_map;
261 void
262 yornp(const char *prompt, void (*fn)(int, struct tab*),
263 struct tab *data)
265 size_t len;
267 if (in_minibuffer) {
268 fn(0, data);
269 return;
272 yornp_cb = fn;
273 yornp_data = data;
274 enter_minibuffer(yornp_self_insert, yornp_self_insert,
275 yornp_abort, NULL);
277 len = sizeof(ministate.prompt);
278 strlcpy(ministate.prompt, prompt, len);
279 strlcat(ministate.prompt, " (y or n) ", len);
282 /*
283 * Not yet "completing", but soon maybe...
284 */
285 void
286 completing_read(const char *prompt, void (*fn)(const char *, unsigned int),
287 unsigned int data)
289 size_t len;
291 if (in_minibuffer)
292 return;
294 read_cb = fn;
295 read_data = data;
296 enter_minibuffer(read_self_insert, read_select, read_abort,
297 &read_history);
299 len = sizeof(ministate.prompt);
300 strlcpy(ministate.prompt, prompt, len);
301 strlcat(ministate.prompt, ": ", len);