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 if (ministate.hist_cur != NULL)
170 return ministate.hist_cur->h;
172 vl = ministate.compl.buffer.current_line;
173 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
174 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
175 return ministate.buf;
176 return vl->parent->line;
179 static void
180 minibuffer_hist_save_entry(void)
182 struct hist *hist;
183 const char *t;
185 if (ministate.history == NULL)
186 return;
188 if ((hist = calloc(1, sizeof(*hist))) == NULL)
189 abort();
191 t = minibuffer_compl_text();
192 strlcpy(hist->h, t, sizeof(hist->h));
194 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
195 ministate.history->len++;
198 /*
199 * taint the minibuffer cache: if we're currently showing a history
200 * element, copy that to the current buf and reset the "history
201 * navigation" thing.
202 */
203 void
204 minibuffer_taint_hist(void)
206 if (ministate.hist_cur == NULL)
207 return;
209 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
210 ministate.hist_cur = NULL;
211 ministate.buffer.current_line->line = ministate.buf;
214 void
215 minibuffer_self_insert(void)
217 char *c, tmp[5] = {0};
218 size_t len;
220 minibuffer_taint_hist();
222 if (thiskey.cp == 0)
223 return;
225 len = utf8_encode(thiskey.cp, tmp);
226 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
227 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
228 return;
230 memmove(c + len, c, strlen(c)+1);
231 memcpy(c, tmp, len);
232 ministate.buffer.cpoff++;
234 recompute_completions(1);
237 void
238 sensible_self_insert(void)
240 if (thiskey.meta ||
241 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
242 global_key_unbound();
243 return;
246 minibuffer_self_insert();
249 void
250 eecmd_select(void)
252 struct cmd *cmd;
253 const char *t;
255 t = minibuffer_compl_text();
256 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
257 if (!strcmp(cmd->cmd, t)) {
258 minibuffer_hist_save_entry();
259 exit_minibuffer();
260 cmd->fn(current_buffer());
261 return;
265 message("No match");
268 void
269 ir_select_gemini(void)
271 char buf[1025] = {0};
272 struct phos_uri uri;
273 struct tab *tab = current_tab;
275 minibuffer_hist_save_entry();
277 /* a bit ugly but... */
278 memcpy(&uri, &tab->uri, sizeof(tab->uri));
279 phos_uri_set_query(&uri, minibuffer_compl_text());
280 phos_serialize_uri(&uri, buf, sizeof(buf));
282 exit_minibuffer();
283 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
286 void
287 ir_select_reply(void)
289 char buf[1025] = {0};
290 struct phos_uri uri;
291 struct tab *tab = current_tab;
293 minibuffer_hist_save_entry();
295 /* a bit ugly but... */
296 strlcpy(buf, tab->last_input_url, sizeof(buf));
297 phos_parse_absolute_uri(buf, &uri);
298 phos_uri_set_query(&uri, minibuffer_compl_text());
299 phos_serialize_uri(&uri, buf, sizeof(buf));
301 exit_minibuffer();
302 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
305 void
306 ir_select_gopher(void)
308 minibuffer_hist_save_entry();
309 gopher_send_search_req(current_tab, minibuffer_compl_text());
310 exit_minibuffer();
313 void
314 lu_select(void)
316 char url[GEMINI_URL_LEN+1];
318 minibuffer_hist_save_entry();
319 humanify_url(minibuffer_compl_text(), url, sizeof(url));
320 exit_minibuffer();
321 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
324 void
325 bp_select(void)
327 exit_minibuffer();
328 if (*ministate.buf != '\0')
329 add_to_bookmarks(ministate.buf);
330 else
331 message("Abort.");
334 void
335 ts_select(void)
337 struct tab *tab;
339 if ((tab = minibuffer_metadata()) == NULL) {
340 message("No tab selected");
341 return;
344 exit_minibuffer();
345 switch_to_tab(tab);
348 void
349 ls_select(void)
351 struct line *l;
353 if ((l = minibuffer_metadata()) == NULL) {
354 message("No link selected");
355 return;
358 exit_minibuffer();
359 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
362 static inline void
363 jump_to_line(struct line *l)
365 struct vline *vl;
366 struct buffer *buffer;
368 buffer = current_buffer();
370 TAILQ_FOREACH(vl, &buffer->head, vlines) {
371 if (vl->parent == l)
372 break;
375 if (vl == NULL)
376 message("Ops, %s error! Please report to %s",
377 __func__, PACKAGE_BUGREPORT);
378 else {
379 buffer->top_line = vl;
380 buffer->current_line = vl;
384 void
385 swiper_select(void)
387 struct line *l;
389 if ((l = minibuffer_metadata()) == NULL) {
390 message("No line selected");
391 return;
394 exit_minibuffer();
395 jump_to_line(l);
398 void
399 toc_select(void)
401 struct line *l;
403 if ((l = minibuffer_metadata()) == NULL) {
404 message("No line selected");
405 return;
408 exit_minibuffer();
409 jump_to_line(l);
412 static void
413 yornp_self_insert(void)
415 if (thiskey.key != 'y' && thiskey.key != 'n') {
416 message("Please answer y or n");
417 return;
420 exit_minibuffer();
421 yornp_cb(thiskey.key == 'y', yornp_data);
424 static void
425 yornp_abort(void)
427 exit_minibuffer();
428 yornp_cb(0, yornp_data);
431 static void
432 read_self_insert(void)
434 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
435 global_key_unbound();
436 return;
439 minibuffer_self_insert();
442 static void
443 read_abort(void)
445 exit_minibuffer();
446 read_cb(NULL, read_data);
449 static void
450 read_select(void)
452 exit_minibuffer();
453 minibuffer_hist_save_entry();
454 read_cb(ministate.buf, read_data);
457 /*
458 * TODO: we should collect this asynchronously...
459 */
460 static inline void
461 populate_compl_buffer(complfn *fn, void *data)
463 const char *s, *descr;
464 struct line *l;
465 struct buffer *b;
466 struct parser *p;
467 void *linedata;
469 b = &ministate.compl.buffer;
470 p = &b->page;
472 linedata = NULL;
473 descr = NULL;
474 while ((s = fn(&data, &linedata, &descr)) != NULL) {
475 if ((l = calloc(1, sizeof(*l))) == NULL)
476 abort();
478 l->type = LINE_COMPL;
479 l->data = linedata;
480 l->alt = (char*)descr;
481 if ((l->line = strdup(s)) == NULL)
482 abort();
484 TAILQ_INSERT_TAIL(&p->head, l, lines);
486 linedata = NULL;
487 descr = NULL;
490 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
491 ministate.compl.must_select)
492 l->type = LINE_COMPL_CURRENT;
495 void
496 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
497 void (*abortfn)(void), struct histhead *hist,
498 complfn *complfn, void *compldata, int must_select)
500 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
501 if (in_minibuffer == MB_COMPREAD) {
502 ui_schedule_redraw();
504 ministate.compl.fn = complfn;
505 ministate.compl.data = compldata;
506 ministate.compl.must_select = must_select;
507 populate_compl_buffer(complfn, compldata);
510 base_map = &minibuffer_map;
511 current_map = &minibuffer_map;
513 base_map->unhandled_input = self_insert_fn;
515 ministate.donefn = donefn;
516 ministate.abortfn = abortfn;
517 memset(ministate.buf, 0, sizeof(ministate.buf));
518 ministate.buffer.current_line = &ministate.vline;
519 ministate.buffer.current_line->line = ministate.buf;
520 ministate.buffer.cpoff = 0;
521 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
523 ministate.history = hist;
524 ministate.hist_cur = NULL;
525 ministate.hist_off = 0;
528 void
529 exit_minibuffer(void)
531 if (in_minibuffer == MB_COMPREAD) {
532 erase_buffer(&ministate.compl.buffer);
533 ui_schedule_redraw();
536 in_minibuffer = 0;
537 base_map = &global_map;
538 current_map = &global_map;
541 void
542 yornp(const char *prompt, void (*fn)(int, struct tab*),
543 struct tab *data)
545 size_t len;
547 if (in_minibuffer) {
548 fn(0, data);
549 return;
552 yornp_cb = fn;
553 yornp_data = data;
554 enter_minibuffer(yornp_self_insert, yornp_self_insert,
555 yornp_abort, NULL, NULL, NULL, 0);
557 len = sizeof(ministate.prompt);
558 strlcpy(ministate.prompt, prompt, len);
559 strlcat(ministate.prompt, " (y or n) ", len);
562 void
563 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
564 struct tab *data)
566 size_t len;
568 if (in_minibuffer)
569 return;
571 read_cb = fn;
572 read_data = data;
573 enter_minibuffer(read_self_insert, read_select, read_abort,
574 &read_history, NULL, NULL, 0);
576 len = sizeof(ministate.prompt);
577 strlcpy(ministate.prompt, prompt, len);
578 strlcat(ministate.prompt, ": ", len);
581 static void
582 handle_clear_echoarea(int fd, short ev, void *d)
584 free(ministate.curmesg);
585 ministate.curmesg = NULL;
587 ui_after_message_hook();
590 void
591 vmessage(const char *fmt, va_list ap)
593 if (evtimer_pending(&clechoev, NULL))
594 evtimer_del(&clechoev);
596 free(ministate.curmesg);
597 ministate.curmesg = NULL;
599 if (fmt != NULL) {
600 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
601 evtimer_add(&clechoev, &clechoev_timer);
603 /* TODO: what to do if the allocation fails here? */
604 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
605 ministate.curmesg = NULL;
608 ui_after_message_hook();
611 void
612 message(const char *fmt, ...)
614 va_list ap;
616 va_start(ap, fmt);
617 vmessage(fmt, ap);
618 va_end(ap);
621 void
622 minibuffer_init(void)
624 TAILQ_INIT(&eecmd_history.head);
625 TAILQ_INIT(&ir_history.head);
626 TAILQ_INIT(&lu_history.head);
627 TAILQ_INIT(&read_history.head);
629 TAILQ_INIT(&ministate.compl.buffer.head);
630 TAILQ_INIT(&ministate.compl.buffer.page.head);
632 ministate.line.type = LINE_TEXT;
633 ministate.vline.parent = &ministate.line;
634 ministate.buffer.page.name = "*minibuffer*";
635 ministate.buffer.current_line = &ministate.vline;
637 evtimer_set(&clechoev, handle_clear_echoarea, NULL);