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 char *input, **ap, *words[10];
93 size_t len = 0;
94 struct line *l;
95 struct vline *vl;
96 struct buffer *b;
98 if (in_minibuffer != MB_COMPREAD)
99 return;
101 strlcpy(buf, ministate.buf, sizeof(buf));
102 input = buf;
104 /* tokenize the input */
105 for (ap = words; ap < words + nitems(words) &&
106 (*ap = strsep(&input, " ")) != NULL;) {
107 if (**ap != '\0')
108 ap++, len++;
111 b = &ministate.compl.buffer;
112 TAILQ_FOREACH(l, &b->page.head, lines) {
113 l->type = LINE_COMPL;
114 if (add && l->flags & L_HIDDEN)
115 continue;
116 if (matches(words, len, l)) {
117 if (l->flags & L_HIDDEN)
118 b->line_max++;
119 l->flags &= ~L_HIDDEN;
120 } else {
121 if (!(l->flags & L_HIDDEN))
122 b->line_max--;
123 l->flags |= L_HIDDEN;
127 if (b->current_line == NULL)
128 b->current_line = TAILQ_FIRST(&b->head);
129 b->current_line = adjust_line(b->current_line, b);
130 vl = b->current_line;
131 if (ministate.compl.must_select && vl != NULL)
132 vl->parent->type = LINE_COMPL_CURRENT;
135 int
136 minibuffer_insert_current_candidate(void)
138 struct vline *vl;
140 vl = ministate.compl.buffer.current_line;
141 if (vl == NULL || vl->parent->flags & L_HIDDEN)
142 return -1;
144 minibuffer_taint_hist();
145 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
146 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
148 return 0;
151 static void *
152 minibuffer_metadata(void)
154 struct vline *vl;
156 vl = ministate.compl.buffer.current_line;
158 if (vl == NULL || vl->parent->flags & L_HIDDEN)
159 return NULL;
161 return vl->parent->data;
164 static const char *
165 minibuffer_compl_text(void)
167 struct vline *vl;
169 vl = ministate.compl.buffer.current_line;
170 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
171 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
172 return ministate.buf;
173 return vl->parent->line;
176 static void
177 minibuffer_hist_save_entry(void)
179 struct hist *hist;
180 const char *t;
182 if (ministate.history == NULL)
183 return;
185 if ((hist = calloc(1, sizeof(*hist))) == NULL)
186 abort();
188 t = minibuffer_compl_text();
189 strlcpy(hist->h, t, sizeof(hist->h));
191 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
192 ministate.history->len++;
195 /*
196 * taint the minibuffer cache: if we're currently showing a history
197 * element, copy that to the current buf and reset the "history
198 * navigation" thing.
199 */
200 void
201 minibuffer_taint_hist(void)
203 if (ministate.hist_cur == NULL)
204 return;
206 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
207 ministate.hist_cur = NULL;
208 ministate.buffer.current_line->line = ministate.buf;
211 void
212 minibuffer_self_insert(void)
214 char *c, tmp[5] = {0};
215 size_t len;
217 minibuffer_taint_hist();
219 if (thiskey.cp == 0)
220 return;
222 len = utf8_encode(thiskey.cp, tmp);
223 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
224 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
225 return;
227 memmove(c + len, c, strlen(c)+1);
228 memcpy(c, tmp, len);
229 ministate.buffer.cpoff++;
231 recompute_completions(1);
234 void
235 sensible_self_insert(void)
237 if (thiskey.meta ||
238 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
239 global_key_unbound();
240 return;
243 minibuffer_self_insert();
246 void
247 eecmd_select(void)
249 struct cmd *cmd;
250 const char *t;
252 t = minibuffer_compl_text();
253 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
254 if (!strcmp(cmd->cmd, t)) {
255 minibuffer_hist_save_entry();
256 exit_minibuffer();
257 cmd->fn(current_buffer());
258 return;
262 message("No match");
265 void
266 ir_select_gemini(void)
268 char buf[1025] = {0};
269 struct phos_uri uri;
270 struct tab *tab = current_tab;
272 minibuffer_hist_save_entry();
274 /* a bit ugly but... */
275 memcpy(&uri, &tab->uri, sizeof(tab->uri));
276 phos_uri_set_query(&uri, minibuffer_compl_text());
277 phos_serialize_uri(&uri, buf, sizeof(buf));
279 exit_minibuffer();
280 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
283 void
284 ir_select_reply(void)
286 char buf[1025] = {0};
287 struct phos_uri uri;
288 struct tab *tab = current_tab;
290 minibuffer_hist_save_entry();
292 /* a bit ugly but... */
293 strlcpy(buf, tab->last_input_url, sizeof(buf));
294 phos_parse_absolute_uri(buf, &uri);
295 phos_uri_set_query(&uri, minibuffer_compl_text());
296 phos_serialize_uri(&uri, buf, sizeof(buf));
298 exit_minibuffer();
299 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
302 void
303 ir_select_gopher(void)
305 minibuffer_hist_save_entry();
306 gopher_send_search_req(current_tab, minibuffer_compl_text());
307 exit_minibuffer();
310 void
311 lu_select(void)
313 char url[GEMINI_URL_LEN+1];
315 minibuffer_hist_save_entry();
316 humanify_url(minibuffer_compl_text(), url, sizeof(url));
317 exit_minibuffer();
318 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
321 void
322 bp_select(void)
324 exit_minibuffer();
325 if (*ministate.buf != '\0')
326 add_to_bookmarks(ministate.buf);
327 else
328 message("Abort.");
331 void
332 ts_select(void)
334 struct tab *tab;
336 if ((tab = minibuffer_metadata()) == NULL) {
337 message("No tab selected");
338 return;
341 exit_minibuffer();
342 switch_to_tab(tab);
345 void
346 ls_select(void)
348 struct line *l;
350 if ((l = minibuffer_metadata()) == NULL) {
351 message("No link selected");
352 return;
355 exit_minibuffer();
356 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
359 static inline void
360 jump_to_line(struct line *l)
362 struct vline *vl;
363 struct buffer *buffer;
365 buffer = current_buffer();
367 TAILQ_FOREACH(vl, &buffer->head, vlines) {
368 if (vl->parent == l)
369 break;
372 if (vl == NULL)
373 message("Ops, %s error! Please report to %s",
374 __func__, PACKAGE_BUGREPORT);
375 else {
376 buffer->top_line = vl;
377 buffer->current_line = vl;
381 void
382 swiper_select(void)
384 struct line *l;
386 if ((l = minibuffer_metadata()) == NULL) {
387 message("No line selected");
388 return;
391 exit_minibuffer();
392 jump_to_line(l);
395 void
396 toc_select(void)
398 struct line *l;
400 if ((l = minibuffer_metadata()) == NULL) {
401 message("No line selected");
402 return;
405 exit_minibuffer();
406 jump_to_line(l);
409 static void
410 yornp_self_insert(void)
412 if (thiskey.key != 'y' && thiskey.key != 'n') {
413 message("Please answer y or n");
414 return;
417 exit_minibuffer();
418 yornp_cb(thiskey.key == 'y', yornp_data);
421 static void
422 yornp_abort(void)
424 exit_minibuffer();
425 yornp_cb(0, yornp_data);
428 static void
429 read_self_insert(void)
431 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
432 global_key_unbound();
433 return;
436 minibuffer_self_insert();
439 static void
440 read_abort(void)
442 exit_minibuffer();
443 read_cb(NULL, read_data);
446 static void
447 read_select(void)
449 exit_minibuffer();
450 minibuffer_hist_save_entry();
451 read_cb(ministate.buf, read_data);
454 /*
455 * TODO: we should collect this asynchronously...
456 */
457 static inline void
458 populate_compl_buffer(complfn *fn, void *data)
460 const char *s, *descr;
461 struct line *l;
462 struct buffer *b;
463 struct parser *p;
464 void *linedata;
466 b = &ministate.compl.buffer;
467 p = &b->page;
469 linedata = NULL;
470 descr = NULL;
471 while ((s = fn(&data, &linedata, &descr)) != NULL) {
472 if ((l = calloc(1, sizeof(*l))) == NULL)
473 abort();
475 l->type = LINE_COMPL;
476 l->data = linedata;
477 l->alt = (char*)descr;
478 if ((l->line = strdup(s)) == NULL)
479 abort();
481 TAILQ_INSERT_TAIL(&p->head, l, lines);
483 linedata = NULL;
484 descr = NULL;
487 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
488 ministate.compl.must_select)
489 l->type = LINE_COMPL_CURRENT;
492 void
493 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
494 void (*abortfn)(void), struct histhead *hist,
495 complfn *complfn, void *compldata, int must_select)
497 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
498 if (in_minibuffer == MB_COMPREAD) {
499 ui_schedule_redraw();
501 ministate.compl.fn = complfn;
502 ministate.compl.data = compldata;
503 ministate.compl.must_select = must_select;
504 populate_compl_buffer(complfn, compldata);
507 base_map = &minibuffer_map;
508 current_map = &minibuffer_map;
510 base_map->unhandled_input = self_insert_fn;
512 ministate.donefn = donefn;
513 ministate.abortfn = abortfn;
514 memset(ministate.buf, 0, sizeof(ministate.buf));
515 ministate.buffer.current_line = &ministate.vline;
516 ministate.buffer.current_line->line = ministate.buf;
517 ministate.buffer.cpoff = 0;
518 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
520 ministate.history = hist;
521 ministate.hist_cur = NULL;
522 ministate.hist_off = 0;
525 void
526 exit_minibuffer(void)
528 if (in_minibuffer == MB_COMPREAD) {
529 erase_buffer(&ministate.compl.buffer);
530 ui_schedule_redraw();
533 in_minibuffer = 0;
534 base_map = &global_map;
535 current_map = &global_map;
538 void
539 yornp(const char *prompt, void (*fn)(int, struct tab*),
540 struct tab *data)
542 size_t len;
544 if (in_minibuffer) {
545 fn(0, data);
546 return;
549 yornp_cb = fn;
550 yornp_data = data;
551 enter_minibuffer(yornp_self_insert, yornp_self_insert,
552 yornp_abort, NULL, NULL, NULL, 0);
554 len = sizeof(ministate.prompt);
555 strlcpy(ministate.prompt, prompt, len);
556 strlcat(ministate.prompt, " (y or n) ", len);
559 void
560 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
561 struct tab *data)
563 size_t len;
565 if (in_minibuffer)
566 return;
568 read_cb = fn;
569 read_data = data;
570 enter_minibuffer(read_self_insert, read_select, read_abort,
571 &read_history, NULL, NULL, 0);
573 len = sizeof(ministate.prompt);
574 strlcpy(ministate.prompt, prompt, len);
575 strlcat(ministate.prompt, ": ", len);
578 static void
579 handle_clear_echoarea(int fd, short ev, void *d)
581 free(ministate.curmesg);
582 ministate.curmesg = NULL;
584 ui_after_message_hook();
587 void
588 vmessage(const char *fmt, va_list ap)
590 if (evtimer_pending(&clechoev, NULL))
591 evtimer_del(&clechoev);
593 free(ministate.curmesg);
594 ministate.curmesg = NULL;
596 if (fmt != NULL) {
597 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
598 evtimer_add(&clechoev, &clechoev_timer);
600 /* TODO: what to do if the allocation fails here? */
601 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
602 ministate.curmesg = NULL;
605 ui_after_message_hook();
608 void
609 message(const char *fmt, ...)
611 va_list ap;
613 va_start(ap, fmt);
614 vmessage(fmt, ap);
615 va_end(ap);
618 void
619 minibuffer_init(void)
621 TAILQ_INIT(&eecmd_history.head);
622 TAILQ_INIT(&ir_history.head);
623 TAILQ_INIT(&lu_history.head);
624 TAILQ_INIT(&read_history.head);
626 TAILQ_INIT(&ministate.compl.buffer.head);
627 TAILQ_INIT(&ministate.compl.buffer.page.head);
629 ministate.line.type = LINE_TEXT;
630 ministate.vline.parent = &ministate.line;
631 ministate.buffer.page.name = "*minibuffer*";
632 ministate.buffer.current_line = &ministate.vline;
634 evtimer_set(&clechoev, handle_clear_echoarea, NULL);