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_insert_current_candidate();
256 minibuffer_hist_save_entry();
257 exit_minibuffer();
258 cmd->fn(current_buffer());
259 return;
263 message("No match");
266 void
267 ir_select_gemini(void)
269 char buf[1025] = {0};
270 struct phos_uri uri;
271 struct tab *tab = current_tab;
273 minibuffer_hist_save_entry();
275 /* a bit ugly but... */
276 memcpy(&uri, &tab->uri, sizeof(tab->uri));
277 phos_uri_set_query(&uri, minibuffer_compl_text());
278 phos_serialize_uri(&uri, buf, sizeof(buf));
280 exit_minibuffer();
281 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
284 void
285 ir_select_reply(void)
287 char buf[1025] = {0};
288 struct phos_uri uri;
289 struct tab *tab = current_tab;
291 minibuffer_hist_save_entry();
293 /* a bit ugly but... */
294 strlcpy(buf, tab->last_input_url, sizeof(buf));
295 phos_parse_absolute_uri(buf, &uri);
296 phos_uri_set_query(&uri, minibuffer_compl_text());
297 phos_serialize_uri(&uri, buf, sizeof(buf));
299 exit_minibuffer();
300 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
303 void
304 ir_select_gopher(void)
306 minibuffer_hist_save_entry();
307 gopher_send_search_req(current_tab, minibuffer_compl_text());
308 exit_minibuffer();
311 void
312 lu_select(void)
314 char url[GEMINI_URL_LEN+1];
316 minibuffer_hist_save_entry();
317 humanify_url(minibuffer_compl_text(), url, sizeof(url));
318 exit_minibuffer();
319 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
322 void
323 bp_select(void)
325 exit_minibuffer();
326 if (*ministate.buf != '\0')
327 add_to_bookmarks(ministate.buf);
328 else
329 message("Abort.");
332 void
333 ts_select(void)
335 struct tab *tab;
337 if ((tab = minibuffer_metadata()) == NULL) {
338 message("No tab selected");
339 return;
342 exit_minibuffer();
343 switch_to_tab(tab);
346 void
347 ls_select(void)
349 struct line *l;
351 if ((l = minibuffer_metadata()) == NULL) {
352 message("No link selected");
353 return;
356 exit_minibuffer();
357 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
360 static inline void
361 jump_to_line(struct line *l)
363 struct vline *vl;
364 struct buffer *buffer;
366 buffer = current_buffer();
368 TAILQ_FOREACH(vl, &buffer->head, vlines) {
369 if (vl->parent == l)
370 break;
373 if (vl == NULL)
374 message("Ops, %s error! Please report to %s",
375 __func__, PACKAGE_BUGREPORT);
376 else {
377 buffer->top_line = vl;
378 buffer->current_line = vl;
382 void
383 swiper_select(void)
385 struct line *l;
387 if ((l = minibuffer_metadata()) == NULL) {
388 message("No line selected");
389 return;
392 exit_minibuffer();
393 jump_to_line(l);
396 void
397 toc_select(void)
399 struct line *l;
401 if ((l = minibuffer_metadata()) == NULL) {
402 message("No line selected");
403 return;
406 exit_minibuffer();
407 jump_to_line(l);
410 static void
411 yornp_self_insert(void)
413 if (thiskey.key != 'y' && thiskey.key != 'n') {
414 message("Please answer y or n");
415 return;
418 exit_minibuffer();
419 yornp_cb(thiskey.key == 'y', yornp_data);
422 static void
423 yornp_abort(void)
425 exit_minibuffer();
426 yornp_cb(0, yornp_data);
429 static void
430 read_self_insert(void)
432 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
433 global_key_unbound();
434 return;
437 minibuffer_self_insert();
440 static void
441 read_abort(void)
443 exit_minibuffer();
444 read_cb(NULL, read_data);
447 static void
448 read_select(void)
450 exit_minibuffer();
451 minibuffer_hist_save_entry();
452 read_cb(ministate.buf, read_data);
455 /*
456 * TODO: we should collect this asynchronously...
457 */
458 static inline void
459 populate_compl_buffer(complfn *fn, void *data)
461 const char *s, *descr;
462 struct line *l;
463 struct buffer *b;
464 struct parser *p;
465 void *linedata;
467 b = &ministate.compl.buffer;
468 p = &b->page;
470 linedata = NULL;
471 descr = NULL;
472 while ((s = fn(&data, &linedata, &descr)) != NULL) {
473 if ((l = calloc(1, sizeof(*l))) == NULL)
474 abort();
476 l->type = LINE_COMPL;
477 l->data = linedata;
478 l->alt = (char*)descr;
479 if ((l->line = strdup(s)) == NULL)
480 abort();
482 TAILQ_INSERT_TAIL(&p->head, l, lines);
484 linedata = NULL;
485 descr = NULL;
488 if ((l = TAILQ_FIRST(&p->head)) != NULL &&
489 ministate.compl.must_select)
490 l->type = LINE_COMPL_CURRENT;
493 void
494 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
495 void (*abortfn)(void), struct histhead *hist,
496 complfn *complfn, void *compldata, int must_select)
498 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
499 if (in_minibuffer == MB_COMPREAD) {
500 ui_schedule_redraw();
502 ministate.compl.fn = complfn;
503 ministate.compl.data = compldata;
504 ministate.compl.must_select = must_select;
505 populate_compl_buffer(complfn, compldata);
508 base_map = &minibuffer_map;
509 current_map = &minibuffer_map;
511 base_map->unhandled_input = self_insert_fn;
513 ministate.donefn = donefn;
514 ministate.abortfn = abortfn;
515 memset(ministate.buf, 0, sizeof(ministate.buf));
516 ministate.buffer.current_line = &ministate.vline;
517 ministate.buffer.current_line->line = ministate.buf;
518 ministate.buffer.cpoff = 0;
519 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
521 ministate.history = hist;
522 ministate.hist_cur = NULL;
523 ministate.hist_off = 0;
526 void
527 exit_minibuffer(void)
529 if (in_minibuffer == MB_COMPREAD) {
530 erase_buffer(&ministate.compl.buffer);
531 ui_schedule_redraw();
534 in_minibuffer = 0;
535 base_map = &global_map;
536 current_map = &global_map;
539 void
540 yornp(const char *prompt, void (*fn)(int, struct tab*),
541 struct tab *data)
543 size_t len;
545 if (in_minibuffer) {
546 fn(0, data);
547 return;
550 yornp_cb = fn;
551 yornp_data = data;
552 enter_minibuffer(yornp_self_insert, yornp_self_insert,
553 yornp_abort, NULL, NULL, NULL, 0);
555 len = sizeof(ministate.prompt);
556 strlcpy(ministate.prompt, prompt, len);
557 strlcat(ministate.prompt, " (y or n) ", len);
560 void
561 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
562 struct tab *data)
564 size_t len;
566 if (in_minibuffer)
567 return;
569 read_cb = fn;
570 read_data = data;
571 enter_minibuffer(read_self_insert, read_select, read_abort,
572 &read_history, NULL, NULL, 0);
574 len = sizeof(ministate.prompt);
575 strlcpy(ministate.prompt, prompt, len);
576 strlcat(ministate.prompt, ": ", len);
579 static void
580 handle_clear_echoarea(int fd, short ev, void *d)
582 free(ministate.curmesg);
583 ministate.curmesg = NULL;
585 ui_after_message_hook();
588 void
589 vmessage(const char *fmt, va_list ap)
591 if (evtimer_pending(&clechoev, NULL))
592 evtimer_del(&clechoev);
594 free(ministate.curmesg);
595 ministate.curmesg = NULL;
597 if (fmt != NULL) {
598 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
599 evtimer_add(&clechoev, &clechoev_timer);
601 /* TODO: what to do if the allocation fails here? */
602 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
603 ministate.curmesg = NULL;
606 ui_after_message_hook();
609 void
610 message(const char *fmt, ...)
612 va_list ap;
614 va_start(ap, fmt);
615 vmessage(fmt, ap);
616 va_end(ap);
619 void
620 minibuffer_init(void)
622 TAILQ_INIT(&eecmd_history.head);
623 TAILQ_INIT(&ir_history.head);
624 TAILQ_INIT(&lu_history.head);
625 TAILQ_INIT(&read_history.head);
627 TAILQ_INIT(&ministate.compl.buffer.head);
628 TAILQ_INIT(&ministate.compl.buffer.page.head);
630 ministate.line.type = LINE_TEXT;
631 ministate.vline.parent = &ministate.line;
632 ministate.buffer.page.name = "*minibuffer*";
633 ministate.buffer.current_line = &ministate.vline;
635 evtimer_set(&clechoev, handle_clear_echoarea, NULL);