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 "session.h"
25 #include "ui.h"
26 #include "utf8.h"
27 #include "utils.h"
29 #define nitems(x) (sizeof(x)/sizeof(x[0]))
31 static void *minibuffer_metadata(void);
32 static const char *minibuffer_compl_text(void);
33 static void minibuffer_hist_save_entry(void);
34 static void yornp_self_insert(void);
35 static void yornp_abort(void);
36 static void read_self_insert(void);
37 static void read_abort(void);
38 static void read_select(void);
39 static void handle_clear_echoarea(int, short, void *);
41 static struct event clechoev;
42 static struct timeval clechoev_timer = { 5, 0 };
44 static void (*yornp_cb)(int, struct tab *);
45 static struct tab *yornp_data;
47 static void (*read_cb)(const char*, struct tab *);
48 static struct tab *read_data;
50 /* XXX: don't forget to init these in minibuffer_init */
51 struct histhead eecmd_history,
52 ir_history,
53 lu_history,
54 read_history;
56 struct ministate ministate;
58 struct buffer minibufferwin;
60 int in_minibuffer;
62 static inline int
63 matches(char **words, size_t len, struct line *l)
64 {
65 size_t i;
66 int lm, am;
68 for (i = 0; i < len; ++i) {
69 lm = am = 0;
71 if (strcasestr(l->line, words[i]) != NULL)
72 lm = 1;
73 if (l->alt != NULL &&
74 strcasestr(l->alt, words[i]) != NULL)
75 am = 1;
77 if (!lm && !am)
78 return 0;
79 }
81 return 1;
82 }
84 /*
85 * Recompute the visible completions. If add is 1, don't consider the
86 * ones already hidden.
87 */
88 void
89 recompute_completions(int add)
90 {
91 static char buf[GEMINI_URL_LEN];
92 const char *text;
93 char *input, **ap, *words[10];
94 size_t len = 0;
95 struct line *l;
96 struct vline *vl;
97 struct buffer *b;
99 if (in_minibuffer != MB_COMPREAD)
100 return;
102 if (ministate.hist_cur != NULL)
103 text = ministate.hist_cur->h;
104 else
105 text = ministate.buf;
107 strlcpy(buf, text, sizeof(buf));
108 input = buf;
110 /* tokenize the input */
111 for (ap = words; ap < words + nitems(words) &&
112 (*ap = strsep(&input, " ")) != NULL;) {
113 if (**ap != '\0')
114 ap++, len++;
117 b = &ministate.compl.buffer;
118 TAILQ_FOREACH(l, &b->page.head, lines) {
119 l->type = LINE_COMPL;
120 if (add && l->flags & L_HIDDEN)
121 continue;
122 if (matches(words, len, l)) {
123 if (l->flags & L_HIDDEN)
124 b->line_max++;
125 l->flags &= ~L_HIDDEN;
126 } else {
127 if (!(l->flags & L_HIDDEN))
128 b->line_max--;
129 l->flags |= L_HIDDEN;
133 if (b->current_line == NULL)
134 b->current_line = TAILQ_FIRST(&b->head);
135 b->current_line = adjust_line(b->current_line, b);
136 vl = b->current_line;
137 if (ministate.compl.must_select && vl != NULL)
138 vl->parent->type = LINE_COMPL_CURRENT;
141 int
142 minibuffer_insert_current_candidate(void)
144 struct vline *vl;
146 vl = ministate.compl.buffer.current_line;
147 if (vl == NULL || vl->parent->flags & L_HIDDEN)
148 return -1;
150 minibuffer_taint_hist();
151 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
152 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
154 return 0;
157 static void *
158 minibuffer_metadata(void)
160 struct vline *vl;
162 vl = ministate.compl.buffer.current_line;
164 if (vl == NULL || vl->parent->flags & L_HIDDEN)
165 return NULL;
167 return vl->parent->data;
170 static const char *
171 minibuffer_compl_text(void)
173 struct vline *vl;
175 if (ministate.hist_cur != NULL)
176 return ministate.hist_cur->h;
178 vl = ministate.compl.buffer.current_line;
179 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
180 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
181 return ministate.buf;
182 return vl->parent->line;
185 static void
186 minibuffer_hist_save_entry(void)
188 struct hist *hist;
189 const char *t;
191 if (ministate.history == NULL)
192 return;
194 if ((hist = calloc(1, sizeof(*hist))) == NULL)
195 abort();
197 t = minibuffer_compl_text();
198 strlcpy(hist->h, t, sizeof(hist->h));
200 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
201 ministate.history->len++;
204 /*
205 * taint the minibuffer cache: if we're currently showing a history
206 * element, copy that to the current buf and reset the "history
207 * navigation" thing.
208 */
209 void
210 minibuffer_taint_hist(void)
212 if (ministate.hist_cur == NULL)
213 return;
215 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
216 ministate.hist_cur = NULL;
217 ministate.buffer.current_line->line = ministate.buf;
220 void
221 minibuffer_self_insert(void)
223 char *c, tmp[5] = {0};
224 size_t len;
226 minibuffer_taint_hist();
228 if (thiskey.cp == 0)
229 return;
231 len = utf8_encode(thiskey.cp, tmp);
232 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
233 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
234 return;
236 memmove(c + len, c, strlen(c)+1);
237 memcpy(c, tmp, len);
238 ministate.buffer.cpoff++;
240 recompute_completions(1);
243 void
244 sensible_self_insert(void)
246 if (thiskey.meta ||
247 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
248 global_key_unbound();
249 return;
252 minibuffer_self_insert();
255 void
256 eecmd_select(void)
258 struct cmd *cmd;
259 const char *t;
261 t = minibuffer_compl_text();
262 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
263 if (!strcmp(cmd->cmd, t)) {
264 minibuffer_hist_save_entry();
265 exit_minibuffer();
266 cmd->fn(current_buffer());
267 return;
271 message("No match");
274 void
275 ir_select_gemini(void)
277 char buf[1025] = {0};
278 struct phos_uri uri;
279 struct tab *tab = current_tab;
281 minibuffer_hist_save_entry();
283 /* a bit ugly but... */
284 memcpy(&uri, &tab->uri, sizeof(tab->uri));
285 phos_uri_set_query(&uri, minibuffer_compl_text());
286 phos_serialize_uri(&uri, buf, sizeof(buf));
288 exit_minibuffer();
289 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
292 void
293 ir_select_reply(void)
295 char buf[1025] = {0};
296 struct phos_uri uri;
297 struct tab *tab = current_tab;
299 minibuffer_hist_save_entry();
301 /* a bit ugly but... */
302 strlcpy(buf, tab->last_input_url, sizeof(buf));
303 phos_parse_absolute_uri(buf, &uri);
304 phos_uri_set_query(&uri, minibuffer_compl_text());
305 phos_serialize_uri(&uri, buf, sizeof(buf));
307 exit_minibuffer();
308 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
311 void
312 ir_select_gopher(void)
314 minibuffer_hist_save_entry();
315 gopher_send_search_req(current_tab, minibuffer_compl_text());
316 exit_minibuffer();
319 void
320 lu_select(void)
322 char url[GEMINI_URL_LEN+1];
324 minibuffer_hist_save_entry();
325 humanify_url(minibuffer_compl_text(), url, sizeof(url));
326 exit_minibuffer();
327 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
330 void
331 bp_select(void)
333 exit_minibuffer();
334 if (*ministate.buf != '\0')
335 add_to_bookmarks(ministate.buf);
336 else
337 message("Abort.");
340 void
341 ts_select(void)
343 struct tab *tab;
345 if ((tab = minibuffer_metadata()) == NULL) {
346 message("No tab selected");
347 return;
350 exit_minibuffer();
351 switch_to_tab(tab);
354 void
355 ls_select(void)
357 struct line *l;
359 if ((l = minibuffer_metadata()) == NULL) {
360 message("No link selected");
361 return;
364 exit_minibuffer();
365 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
368 static inline void
369 jump_to_line(struct line *l)
371 struct vline *vl;
372 struct buffer *buffer;
374 buffer = current_buffer();
376 TAILQ_FOREACH(vl, &buffer->head, vlines) {
377 if (vl->parent == l)
378 break;
381 if (vl == NULL)
382 message("Ops, %s error! Please report to %s",
383 __func__, PACKAGE_BUGREPORT);
384 else {
385 buffer->top_line = vl;
386 buffer->current_line = vl;
390 void
391 swiper_select(void)
393 struct line *l;
395 if ((l = minibuffer_metadata()) == NULL) {
396 message("No line selected");
397 return;
400 exit_minibuffer();
401 jump_to_line(l);
404 void
405 toc_select(void)
407 struct line *l;
409 if ((l = minibuffer_metadata()) == NULL) {
410 message("No line selected");
411 return;
414 exit_minibuffer();
415 jump_to_line(l);
418 static void
419 yornp_self_insert(void)
421 if (thiskey.key != 'y' && thiskey.key != 'n') {
422 message("Please answer y or n");
423 return;
426 exit_minibuffer();
427 yornp_cb(thiskey.key == 'y', yornp_data);
430 static void
431 yornp_abort(void)
433 exit_minibuffer();
434 yornp_cb(0, yornp_data);
437 static void
438 read_self_insert(void)
440 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
441 global_key_unbound();
442 return;
445 minibuffer_self_insert();
448 static void
449 read_abort(void)
451 exit_minibuffer();
452 read_cb(NULL, read_data);
455 static void
456 read_select(void)
458 exit_minibuffer();
459 minibuffer_hist_save_entry();
460 read_cb(ministate.buf, read_data);
463 /*
464 * TODO: we should collect this asynchronously...
465 */
466 static inline void
467 populate_compl_buffer(complfn *fn, void *data)
469 const char *s, *descr;
470 struct line *l;
471 struct buffer *b;
472 struct parser *p;
473 void *linedata;
475 b = &ministate.compl.buffer;
476 p = &b->page;
478 linedata = NULL;
479 descr = NULL;
480 while ((s = fn(&data, &linedata, &descr)) != NULL) {
481 if ((l = calloc(1, sizeof(*l))) == NULL)
482 abort();
484 l->type = LINE_COMPL;
485 l->data = linedata;
486 l->alt = (char*)descr;
487 if ((l->line = strdup(s)) == NULL)
488 abort();
490 TAILQ_INSERT_TAIL(&p->head, l, lines);
492 linedata = NULL;
493 descr = NULL;
496 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
497 ministate.compl.must_select)
498 l->type = LINE_COMPL_CURRENT;
501 void
502 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
503 void (*abortfn)(void), struct histhead *hist,
504 complfn *complfn, void *compldata, int must_select)
506 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
507 if (in_minibuffer == MB_COMPREAD) {
508 ui_schedule_redraw();
510 ministate.compl.fn = complfn;
511 ministate.compl.data = compldata;
512 ministate.compl.must_select = must_select;
513 populate_compl_buffer(complfn, compldata);
516 base_map = &minibuffer_map;
517 current_map = &minibuffer_map;
519 base_map->unhandled_input = self_insert_fn;
521 ministate.donefn = donefn;
522 ministate.abortfn = abortfn;
523 memset(ministate.buf, 0, sizeof(ministate.buf));
524 ministate.buffer.current_line = &ministate.vline;
525 ministate.buffer.current_line->line = ministate.buf;
526 ministate.buffer.cpoff = 0;
527 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
529 ministate.history = hist;
530 ministate.hist_cur = NULL;
531 ministate.hist_off = 0;
534 void
535 exit_minibuffer(void)
537 if (in_minibuffer == MB_COMPREAD) {
538 erase_buffer(&ministate.compl.buffer);
539 ui_schedule_redraw();
542 in_minibuffer = 0;
543 base_map = &global_map;
544 current_map = &global_map;
547 void
548 yornp(const char *prompt, void (*fn)(int, struct tab*),
549 struct tab *data)
551 size_t len;
553 if (in_minibuffer) {
554 fn(0, data);
555 return;
558 yornp_cb = fn;
559 yornp_data = data;
560 enter_minibuffer(yornp_self_insert, yornp_self_insert,
561 yornp_abort, NULL, NULL, NULL, 0);
563 len = sizeof(ministate.prompt);
564 strlcpy(ministate.prompt, prompt, len);
565 strlcat(ministate.prompt, " (y or n) ", len);
568 void
569 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
570 struct tab *data)
572 size_t len;
574 if (in_minibuffer)
575 return;
577 read_cb = fn;
578 read_data = data;
579 enter_minibuffer(read_self_insert, read_select, read_abort,
580 &read_history, NULL, NULL, 0);
582 len = sizeof(ministate.prompt);
583 strlcpy(ministate.prompt, prompt, len);
584 strlcat(ministate.prompt, ": ", len);
587 static void
588 handle_clear_echoarea(int fd, short ev, void *d)
590 free(ministate.curmesg);
591 ministate.curmesg = NULL;
593 ui_after_message_hook();
596 void
597 vmessage(const char *fmt, va_list ap)
599 if (evtimer_pending(&clechoev, NULL))
600 evtimer_del(&clechoev);
602 free(ministate.curmesg);
603 ministate.curmesg = NULL;
605 if (fmt != NULL) {
606 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
607 evtimer_add(&clechoev, &clechoev_timer);
609 /* TODO: what to do if the allocation fails here? */
610 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
611 ministate.curmesg = NULL;
614 ui_after_message_hook();
617 void
618 message(const char *fmt, ...)
620 va_list ap;
622 va_start(ap, fmt);
623 vmessage(fmt, ap);
624 va_end(ap);
627 void
628 minibuffer_init(void)
630 TAILQ_INIT(&eecmd_history.head);
631 TAILQ_INIT(&ir_history.head);
632 TAILQ_INIT(&lu_history.head);
633 TAILQ_INIT(&read_history.head);
635 TAILQ_INIT(&ministate.compl.buffer.head);
636 TAILQ_INIT(&ministate.compl.buffer.page.head);
638 ministate.line.type = LINE_TEXT;
639 ministate.vline.parent = &ministate.line;
640 ministate.buffer.page.name = "*minibuffer*";
641 ministate.buffer.current_line = &ministate.vline;
643 evtimer_set(&clechoev, handle_clear_echoarea, NULL);