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 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*, struct tab *);
35 static struct tab *read_data;
37 struct histhead eecmd_history,
38 ir_history,
39 lu_history,
40 read_history;
42 struct ministate ministate;
44 struct buffer minibufferwin;
46 /*
47 * Recompute the visible completions. If add is 1, don't consider the
48 * ones already hidden.
49 */
50 void
51 recompute_completions(int add)
52 {
53 struct line *l;
54 struct vline *vl;
55 struct buffer *b;
57 if (in_minibuffer != MB_COMPREAD)
58 return;
60 b = &ministate.compl.buffer;
61 TAILQ_FOREACH(l, &b->page.head, lines) {
62 l->type = LINE_COMPL;
63 if (add && l->flags & L_HIDDEN)
64 continue;
65 if (strstr(l->line, ministate.buf) != NULL)
66 l->flags &= ~L_HIDDEN;
67 else
68 l->flags |= L_HIDDEN;
69 }
71 if (b->current_line == NULL)
72 b->current_line = TAILQ_FIRST(&b->head);
73 b->current_line = adjust_line(b->current_line, b);
74 vl = b->current_line;
75 if (vl != NULL)
76 vl->parent->type = LINE_COMPL_CURRENT;
77 }
79 static void
80 minibuffer_hist_save_entry(void)
81 {
82 struct hist *hist;
84 if (ministate.history == NULL)
85 return;
87 if ((hist = calloc(1, sizeof(*hist))) == NULL)
88 abort();
90 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
92 if (TAILQ_EMPTY(&ministate.history->head))
93 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
94 else
95 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
96 ministate.history->len++;
97 }
99 /*
100 * taint the minibuffer cache: if we're currently showing a history
101 * element, copy that to the current buf and reset the "history
102 * navigation" thing.
103 */
104 void
105 minibuffer_taint_hist(void)
107 if (ministate.hist_cur == NULL)
108 return;
110 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
111 ministate.hist_cur = NULL;
112 ministate.buffer.current_line->line = ministate.buf;
115 void
116 minibuffer_self_insert(void)
118 char *c, tmp[5] = {0};
119 size_t len;
121 minibuffer_taint_hist();
123 if (thiskey.cp == 0)
124 return;
126 len = utf8_encode(thiskey.cp, tmp);
127 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
128 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
129 return;
131 memmove(c + len, c, strlen(c)+1);
132 memcpy(c, tmp, len);
133 ministate.buffer.cpoff++;
135 recompute_completions(1);
138 void
139 eecmd_self_insert(void)
141 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
142 !unicode_isgraph(thiskey.cp)) {
143 global_key_unbound();
144 return;
147 minibuffer_self_insert();
150 void
151 eecmd_select(void)
153 struct cmd *cmd;
155 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
156 if (!strcmp(cmd->cmd, ministate.buf)) {
157 exit_minibuffer();
158 minibuffer_hist_save_entry();
159 cmd->fn(current_buffer());
160 return;
164 message("No match");
167 void
168 ir_self_insert(void)
170 minibuffer_self_insert();
173 void
174 ir_select(void)
176 char buf[1025] = {0};
177 struct phos_uri uri;
178 struct tab *tab;
180 tab = current_tab();
182 exit_minibuffer();
183 minibuffer_hist_save_entry();
185 /* a bit ugly but... */
186 memcpy(&uri, &tab->uri, sizeof(tab->uri));
187 phos_uri_set_query(&uri, ministate.buf);
188 phos_serialize_uri(&uri, buf, sizeof(buf));
189 load_url_in_tab(tab, buf);
192 void
193 lu_self_insert(void)
195 if (thiskey.meta || unicode_isspace(thiskey.key) ||
196 !unicode_isgraph(thiskey.key)) {
197 global_key_unbound();
198 return;
201 minibuffer_self_insert();
204 void
205 lu_select(void)
207 exit_minibuffer();
208 minibuffer_hist_save_entry();
209 load_url_in_tab(current_tab(), ministate.buf);
212 void
213 bp_select(void)
215 exit_minibuffer();
216 if (*ministate.buf != '\0')
217 add_to_bookmarks(ministate.buf);
218 else
219 message("Abort.");
222 void
223 ts_select(void)
225 struct vline *vl;
226 struct tab *tab;
228 vl = ministate.compl.buffer.current_line;
230 if (vl == NULL || vl->parent->flags & L_HIDDEN) {
231 message("No tab selected");
232 return;
235 tab = vl->parent->meta.data;
236 exit_minibuffer();
237 switch_to_tab(tab);
240 static void
241 yornp_self_insert(void)
243 if (thiskey.key != 'y' && thiskey.key != 'n') {
244 message("Please answer y or n");
245 return;
248 exit_minibuffer();
249 yornp_cb(thiskey.key == 'y', yornp_data);
252 static void
253 yornp_abort(void)
255 exit_minibuffer();
256 yornp_cb(0, yornp_data);
259 static void
260 read_self_insert(void)
262 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
263 global_key_unbound();
264 return;
267 minibuffer_self_insert();
270 static void
271 read_abort(void)
273 exit_minibuffer();
274 read_cb(NULL, read_data);
277 static void
278 read_select(void)
280 exit_minibuffer();
281 minibuffer_hist_save_entry();
282 read_cb(ministate.buf, read_data);
285 /*
286 * TODO: we should collect this asynchronously...
287 */
288 static inline void
289 populate_compl_buffer(complfn *fn, void *data)
291 const char *s;
292 struct line *l;
293 struct buffer *b;
294 struct parser *p;
295 void *linedata = NULL;
297 b = &ministate.compl.buffer;
298 p = &b->page;
300 while ((s = fn(&data, &linedata)) != NULL) {
301 if ((l = calloc(1, sizeof(*l))) == NULL)
302 abort();
304 l->type = LINE_COMPL;
305 l->meta.data = linedata;
306 if ((l->line = strdup(s)) == NULL)
307 abort();
309 if (TAILQ_EMPTY(&p->head))
310 TAILQ_INSERT_HEAD(&p->head, l, lines);
311 else
312 TAILQ_INSERT_TAIL(&p->head, l, lines);
314 linedata = NULL;
317 if ((l = TAILQ_FIRST(&p->head)) != NULL)
318 l->type = LINE_COMPL_CURRENT;
321 void
322 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
323 void (*abortfn)(void), struct histhead *hist,
324 complfn *complfn, void *compldata)
326 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
327 if (in_minibuffer == MB_COMPREAD) {
328 ui_schedule_redraw();
330 ministate.compl.fn = complfn;
331 ministate.compl.data = compldata;
332 populate_compl_buffer(complfn, compldata);
335 base_map = &minibuffer_map;
336 current_map = &minibuffer_map;
338 base_map->unhandled_input = self_insert_fn;
340 ministate.donefn = donefn;
341 ministate.abortfn = abortfn;
342 memset(ministate.buf, 0, sizeof(ministate.buf));
343 ministate.buffer.current_line = &ministate.vline;
344 ministate.buffer.current_line->line = ministate.buf;
345 ministate.buffer.cpoff = 0;
346 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
348 ministate.history = hist;
349 ministate.hist_cur = NULL;
350 ministate.hist_off = 0;
353 void
354 exit_minibuffer(void)
356 if (in_minibuffer == MB_COMPREAD) {
357 erase_buffer(&ministate.compl.buffer);
358 ui_schedule_redraw();
361 in_minibuffer = 0;
362 base_map = &global_map;
363 current_map = &global_map;
366 void
367 yornp(const char *prompt, void (*fn)(int, struct tab*),
368 struct tab *data)
370 size_t len;
372 if (in_minibuffer) {
373 fn(0, data);
374 return;
377 yornp_cb = fn;
378 yornp_data = data;
379 enter_minibuffer(yornp_self_insert, yornp_self_insert,
380 yornp_abort, NULL, NULL, NULL);
382 len = sizeof(ministate.prompt);
383 strlcpy(ministate.prompt, prompt, len);
384 strlcat(ministate.prompt, " (y or n) ", len);
387 /*
388 * Not yet "completing", but soon maybe...
389 */
390 void
391 completing_read(const char *prompt, void (*fn)(const char *, struct tab *),
392 struct tab *data)
394 size_t len;
396 if (in_minibuffer)
397 return;
399 read_cb = fn;
400 read_data = data;
401 enter_minibuffer(read_self_insert, read_select, read_abort,
402 &read_history, NULL, NULL);
404 len = sizeof(ministate.prompt);
405 strlcpy(ministate.prompt, prompt, len);
406 strlcat(ministate.prompt, ": ", len);