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 "compat.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "minibuffer.h"
24 #include "ui.h"
25 #include "utf8.h"
27 static void *minibuffer_metadata(void);
28 static void minibuffer_hist_save_entry(void);
29 static void yornp_self_insert(void);
30 static void yornp_abort(void);
31 static void read_self_insert(void);
32 static void read_abort(void);
33 static void read_select(void);
34 static void handle_clear_echoarea(int, short, void *);
36 static struct event clechoev;
37 static struct timeval clechoev_timer = { 5, 0 };
39 static void (*yornp_cb)(int, struct tab *);
40 static struct tab *yornp_data;
42 static void (*read_cb)(const char*, struct tab *);
43 static struct tab *read_data;
45 struct histhead eecmd_history,
46 ir_history,
47 lu_history,
48 read_history;
50 struct ministate ministate;
52 struct buffer minibufferwin;
54 /*
55 * Recompute the visible completions. If add is 1, don't consider the
56 * ones already hidden.
57 */
58 void
59 recompute_completions(int add)
60 {
61 struct line *l;
62 struct vline *vl;
63 struct buffer *b;
65 if (in_minibuffer != MB_COMPREAD)
66 return;
68 b = &ministate.compl.buffer;
69 TAILQ_FOREACH(l, &b->page.head, lines) {
70 l->type = LINE_COMPL;
71 if (add && l->flags & L_HIDDEN)
72 continue;
73 if (strcasestr(l->line, ministate.buf) != NULL ||
74 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
75 if (l->flags & L_HIDDEN)
76 b->line_max++;
77 l->flags &= ~L_HIDDEN;
78 } else {
79 if (!(l->flags & L_HIDDEN))
80 b->line_max--;
81 l->flags |= L_HIDDEN;
82 }
83 }
85 if (b->current_line == NULL)
86 b->current_line = TAILQ_FIRST(&b->head);
87 b->current_line = adjust_line(b->current_line, b);
88 vl = b->current_line;
89 if (vl != NULL)
90 vl->parent->type = LINE_COMPL_CURRENT;
91 }
93 static void *
94 minibuffer_metadata(void)
95 {
96 struct vline *vl;
98 vl = ministate.compl.buffer.current_line;
100 if (vl == NULL || vl->parent->flags & L_HIDDEN)
101 return NULL;
103 return vl->parent->data;
106 static void
107 minibuffer_hist_save_entry(void)
109 struct hist *hist;
111 if (ministate.history == NULL)
112 return;
114 if ((hist = calloc(1, sizeof(*hist))) == NULL)
115 abort();
117 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
119 if (TAILQ_EMPTY(&ministate.history->head))
120 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
121 else
122 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
123 ministate.history->len++;
126 /*
127 * taint the minibuffer cache: if we're currently showing a history
128 * element, copy that to the current buf and reset the "history
129 * navigation" thing.
130 */
131 void
132 minibuffer_taint_hist(void)
134 if (ministate.hist_cur == NULL)
135 return;
137 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
138 ministate.hist_cur = NULL;
139 ministate.buffer.current_line->line = ministate.buf;
142 void
143 minibuffer_self_insert(void)
145 char *c, tmp[5] = {0};
146 size_t len;
148 minibuffer_taint_hist();
150 if (thiskey.cp == 0)
151 return;
153 len = utf8_encode(thiskey.cp, tmp);
154 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
155 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
156 return;
158 memmove(c + len, c, strlen(c)+1);
159 memcpy(c, tmp, len);
160 ministate.buffer.cpoff++;
162 recompute_completions(1);
165 void
166 sensible_self_insert(void)
168 if (thiskey.meta ||
169 (unicode_isspace(thiskey.key) && thiskey.key != ' ')) {
170 global_key_unbound();
171 return;
174 minibuffer_self_insert();
177 void
178 eecmd_self_insert(void)
180 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
181 !unicode_isgraph(thiskey.cp)) {
182 global_key_unbound();
183 return;
186 minibuffer_self_insert();
189 void
190 eecmd_select(void)
192 struct cmd *cmd;
194 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
195 if (!strcmp(cmd->cmd, ministate.buf)) {
196 exit_minibuffer();
197 minibuffer_hist_save_entry();
198 cmd->fn(current_buffer());
199 return;
203 message("No match");
206 void
207 ir_self_insert(void)
209 minibuffer_self_insert();
212 void
213 ir_select(void)
215 char buf[1025] = {0};
216 struct phos_uri uri;
217 struct tab *tab = current_tab;
219 exit_minibuffer();
220 minibuffer_hist_save_entry();
222 /* a bit ugly but... */
223 memcpy(&uri, &tab->uri, sizeof(tab->uri));
224 phos_uri_set_query(&uri, ministate.buf);
225 phos_serialize_uri(&uri, buf, sizeof(buf));
226 load_url_in_tab(tab, buf, NULL);
229 void
230 lu_select(void)
232 exit_minibuffer();
233 minibuffer_hist_save_entry();
234 load_url_in_tab(current_tab, ministate.buf, NULL);
237 void
238 bp_select(void)
240 exit_minibuffer();
241 if (*ministate.buf != '\0')
242 add_to_bookmarks(ministate.buf);
243 else
244 message("Abort.");
247 void
248 ts_select(void)
250 struct tab *tab;
252 if ((tab = minibuffer_metadata()) == NULL) {
253 message("No tab selected");
254 return;
257 exit_minibuffer();
258 switch_to_tab(tab);
261 void
262 ls_select(void)
264 struct line *l;
266 if ((l = minibuffer_metadata()) == NULL) {
267 message("No link selected");
268 return;
271 exit_minibuffer();
272 load_url_in_tab(current_tab, l->alt, NULL);
275 static inline void
276 jump_to_line(struct line *l)
278 struct vline *vl;
279 struct tab *tab = current_tab;
281 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
282 if (vl->parent == l)
283 break;
286 if (vl == NULL)
287 message("Ops, %s error! Please report to %s",
288 __func__, PACKAGE_BUGREPORT);
289 else {
290 tab->buffer.top_line = vl;
291 tab->buffer.current_line = vl;
295 void
296 swiper_select(void)
298 struct line *l;
300 if ((l = minibuffer_metadata()) == NULL) {
301 message("No line selected");
302 return;
305 exit_minibuffer();
306 jump_to_line(l);
309 void
310 toc_select(void)
312 struct line *l;
314 if ((l = minibuffer_metadata()) == NULL) {
315 message("No line selected");
316 return;
319 exit_minibuffer();
320 jump_to_line(l);
323 static void
324 yornp_self_insert(void)
326 if (thiskey.key != 'y' && thiskey.key != 'n') {
327 message("Please answer y or n");
328 return;
331 exit_minibuffer();
332 yornp_cb(thiskey.key == 'y', yornp_data);
335 static void
336 yornp_abort(void)
338 exit_minibuffer();
339 yornp_cb(0, yornp_data);
342 static void
343 read_self_insert(void)
345 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
346 global_key_unbound();
347 return;
350 minibuffer_self_insert();
353 static void
354 read_abort(void)
356 exit_minibuffer();
357 read_cb(NULL, read_data);
360 static void
361 read_select(void)
363 exit_minibuffer();
364 minibuffer_hist_save_entry();
365 read_cb(ministate.buf, read_data);
368 /*
369 * TODO: we should collect this asynchronously...
370 */
371 static inline void
372 populate_compl_buffer(complfn *fn, void *data)
374 const char *s, *descr;
375 struct line *l;
376 struct buffer *b;
377 struct parser *p;
378 void *linedata;
380 b = &ministate.compl.buffer;
381 p = &b->page;
383 linedata = NULL;
384 descr = NULL;
385 while ((s = fn(&data, &linedata, &descr)) != NULL) {
386 if ((l = calloc(1, sizeof(*l))) == NULL)
387 abort();
389 l->type = LINE_COMPL;
390 l->data = linedata;
391 l->alt = (char*)descr;
392 if ((l->line = strdup(s)) == NULL)
393 abort();
395 if (TAILQ_EMPTY(&p->head))
396 TAILQ_INSERT_HEAD(&p->head, l, lines);
397 else
398 TAILQ_INSERT_TAIL(&p->head, l, lines);
400 linedata = NULL;
401 descr = NULL;
404 if ((l = TAILQ_FIRST(&p->head)) != NULL)
405 l->type = LINE_COMPL_CURRENT;
408 void
409 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
410 void (*abortfn)(void), struct histhead *hist,
411 complfn *complfn, void *compldata)
413 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
414 if (in_minibuffer == MB_COMPREAD) {
415 ui_schedule_redraw();
417 ministate.compl.fn = complfn;
418 ministate.compl.data = compldata;
419 populate_compl_buffer(complfn, compldata);
422 base_map = &minibuffer_map;
423 current_map = &minibuffer_map;
425 base_map->unhandled_input = self_insert_fn;
427 ministate.donefn = donefn;
428 ministate.abortfn = abortfn;
429 memset(ministate.buf, 0, sizeof(ministate.buf));
430 ministate.buffer.current_line = &ministate.vline;
431 ministate.buffer.current_line->line = ministate.buf;
432 ministate.buffer.cpoff = 0;
433 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
435 ministate.history = hist;
436 ministate.hist_cur = NULL;
437 ministate.hist_off = 0;
440 void
441 exit_minibuffer(void)
443 if (in_minibuffer == MB_COMPREAD) {
444 erase_buffer(&ministate.compl.buffer);
445 ui_schedule_redraw();
448 in_minibuffer = 0;
449 base_map = &global_map;
450 current_map = &global_map;
453 void
454 yornp(const char *prompt, void (*fn)(int, struct tab*),
455 struct tab *data)
457 size_t len;
459 if (in_minibuffer) {
460 fn(0, data);
461 return;
464 yornp_cb = fn;
465 yornp_data = data;
466 enter_minibuffer(yornp_self_insert, yornp_self_insert,
467 yornp_abort, NULL, NULL, NULL);
469 len = sizeof(ministate.prompt);
470 strlcpy(ministate.prompt, prompt, len);
471 strlcat(ministate.prompt, " (y or n) ", len);
474 void
475 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
476 struct tab *data)
478 size_t len;
480 if (in_minibuffer)
481 return;
483 read_cb = fn;
484 read_data = data;
485 enter_minibuffer(read_self_insert, read_select, read_abort,
486 &read_history, NULL, NULL);
488 len = sizeof(ministate.prompt);
489 strlcpy(ministate.prompt, prompt, len);
490 strlcat(ministate.prompt, ": ", len);
493 static void
494 handle_clear_echoarea(int fd, short ev, void *d)
496 free(ministate.curmesg);
497 ministate.curmesg = NULL;
499 ui_after_message_hook();
502 void
503 vmessage(const char *fmt, va_list ap)
505 if (evtimer_pending(&clechoev, NULL))
506 evtimer_del(&clechoev);
508 free(ministate.curmesg);
509 ministate.curmesg = NULL;
511 if (fmt != NULL) {
512 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
513 evtimer_add(&clechoev, &clechoev_timer);
515 /* TODO: what to do if the allocation fails here? */
516 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
517 ministate.curmesg = NULL;
520 ui_after_message_hook();
523 void
524 message(const char *fmt, ...)
526 va_list ap;
528 va_start(ap, fmt);
529 vmessage(fmt, ap);
530 va_end(ap);
533 void
534 minibuffer_init(void)
536 TAILQ_INIT(&eecmd_history.head);
537 TAILQ_INIT(&ir_history.head);
538 TAILQ_INIT(&lu_history.head);
540 ministate.line.type = LINE_TEXT;
541 ministate.vline.parent = &ministate.line;
542 ministate.buffer.current_line = &ministate.vline;
544 evtimer_set(&clechoev, handle_clear_echoarea, NULL);