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 buffer *b;
97 if (in_minibuffer != MB_COMPREAD)
98 return;
100 strlcpy(buf, ministate.buf, sizeof(buf));
101 input = buf;
103 /* tokenize the input */
104 for (ap = words; ap < words + nitems(words) &&
105 (*ap = strsep(&input, " ")) != NULL;) {
106 if (**ap != '\0')
107 ap++, len++;
110 b = &ministate.compl.buffer;
111 TAILQ_FOREACH(l, &b->page.head, lines) {
112 l->type = LINE_COMPL;
113 if (add && l->flags & L_HIDDEN)
114 continue;
115 if (matches(words, len, l)) {
116 if (l->flags & L_HIDDEN)
117 b->line_max++;
118 l->flags &= ~L_HIDDEN;
119 } else {
120 if (!(l->flags & L_HIDDEN))
121 b->line_max--;
122 l->flags |= L_HIDDEN;
126 if (b->current_line == NULL)
127 b->current_line = TAILQ_FIRST(&b->head);
128 b->current_line = adjust_line(b->current_line, b);
131 int
132 minibuffer_insert_current_candidate(void)
134 struct vline *vl;
136 vl = ministate.compl.buffer.current_line;
137 if (vl == NULL || vl->parent->flags & L_HIDDEN)
138 return -1;
140 minibuffer_taint_hist();
141 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
142 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
144 return 0;
147 static void *
148 minibuffer_metadata(void)
150 struct vline *vl;
152 vl = ministate.compl.buffer.current_line;
154 if (vl == NULL || vl->parent->flags & L_HIDDEN)
155 return NULL;
157 return vl->parent->data;
160 static const char *
161 minibuffer_compl_text(void)
163 struct vline *vl;
165 vl = ministate.compl.buffer.current_line;
166 if (vl == NULL || vl->parent->flags & L_HIDDEN ||
167 vl->parent->type == LINE_COMPL || vl->parent->line == NULL)
168 return ministate.buf;
169 return vl->parent->line;
172 static void
173 minibuffer_hist_save_entry(void)
175 struct hist *hist;
176 const char *t;
178 if (ministate.history == NULL)
179 return;
181 if ((hist = calloc(1, sizeof(*hist))) == NULL)
182 abort();
184 t = minibuffer_compl_text();
185 strlcpy(hist->h, t, sizeof(hist->h));
187 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
188 ministate.history->len++;
191 /*
192 * taint the minibuffer cache: if we're currently showing a history
193 * element, copy that to the current buf and reset the "history
194 * navigation" thing.
195 */
196 void
197 minibuffer_taint_hist(void)
199 if (ministate.hist_cur == NULL)
200 return;
202 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
203 ministate.hist_cur = NULL;
204 ministate.buffer.current_line->line = ministate.buf;
207 void
208 minibuffer_self_insert(void)
210 char *c, tmp[5] = {0};
211 size_t len;
213 minibuffer_taint_hist();
215 if (thiskey.cp == 0)
216 return;
218 len = utf8_encode(thiskey.cp, tmp);
219 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
220 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
221 return;
223 memmove(c + len, c, strlen(c)+1);
224 memcpy(c, tmp, len);
225 ministate.buffer.cpoff++;
227 recompute_completions(1);
230 void
231 sensible_self_insert(void)
233 if (thiskey.meta ||
234 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
235 global_key_unbound();
236 return;
239 minibuffer_self_insert();
242 void
243 eecmd_select(void)
245 struct cmd *cmd;
246 const char *t;
248 t = minibuffer_compl_text();
249 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
250 if (!strcmp(cmd->cmd, t)) {
251 minibuffer_insert_current_candidate();
252 minibuffer_hist_save_entry();
253 exit_minibuffer();
254 cmd->fn(current_buffer());
255 return;
259 message("No match");
262 void
263 ir_select_gemini(void)
265 char buf[1025] = {0};
266 struct phos_uri uri;
267 struct tab *tab = current_tab;
269 minibuffer_hist_save_entry();
271 /* a bit ugly but... */
272 memcpy(&uri, &tab->uri, sizeof(tab->uri));
273 phos_uri_set_query(&uri, minibuffer_compl_text());
274 phos_serialize_uri(&uri, buf, sizeof(buf));
276 exit_minibuffer();
277 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
280 void
281 ir_select_reply(void)
283 char buf[1025] = {0};
284 struct phos_uri uri;
285 struct tab *tab = current_tab;
287 minibuffer_hist_save_entry();
289 /* a bit ugly but... */
290 strlcpy(buf, tab->last_input_url, sizeof(buf));
291 phos_parse_absolute_uri(buf, &uri);
292 phos_uri_set_query(&uri, minibuffer_compl_text());
293 phos_serialize_uri(&uri, buf, sizeof(buf));
295 exit_minibuffer();
296 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
299 void
300 ir_select_gopher(void)
302 minibuffer_hist_save_entry();
303 gopher_send_search_req(current_tab, minibuffer_compl_text());
304 exit_minibuffer();
307 void
308 lu_select(void)
310 char url[GEMINI_URL_LEN+1];
312 minibuffer_hist_save_entry();
313 humanify_url(minibuffer_compl_text(), url, sizeof(url));
314 exit_minibuffer();
315 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
318 void
319 bp_select(void)
321 exit_minibuffer();
322 if (*ministate.buf != '\0')
323 add_to_bookmarks(ministate.buf);
324 else
325 message("Abort.");
328 void
329 ts_select(void)
331 struct tab *tab;
333 if ((tab = minibuffer_metadata()) == NULL) {
334 message("No tab selected");
335 return;
338 exit_minibuffer();
339 switch_to_tab(tab);
342 void
343 ls_select(void)
345 struct line *l;
347 if ((l = minibuffer_metadata()) == NULL) {
348 message("No link selected");
349 return;
352 exit_minibuffer();
353 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
356 static inline void
357 jump_to_line(struct line *l)
359 struct vline *vl;
360 struct buffer *buffer;
362 buffer = current_buffer();
364 TAILQ_FOREACH(vl, &buffer->head, vlines) {
365 if (vl->parent == l)
366 break;
369 if (vl == NULL)
370 message("Ops, %s error! Please report to %s",
371 __func__, PACKAGE_BUGREPORT);
372 else {
373 buffer->top_line = vl;
374 buffer->current_line = vl;
378 void
379 swiper_select(void)
381 struct line *l;
383 if ((l = minibuffer_metadata()) == NULL) {
384 message("No line selected");
385 return;
388 exit_minibuffer();
389 jump_to_line(l);
392 void
393 toc_select(void)
395 struct line *l;
397 if ((l = minibuffer_metadata()) == NULL) {
398 message("No line selected");
399 return;
402 exit_minibuffer();
403 jump_to_line(l);
406 static void
407 yornp_self_insert(void)
409 if (thiskey.key != 'y' && thiskey.key != 'n') {
410 message("Please answer y or n");
411 return;
414 exit_minibuffer();
415 yornp_cb(thiskey.key == 'y', yornp_data);
418 static void
419 yornp_abort(void)
421 exit_minibuffer();
422 yornp_cb(0, yornp_data);
425 static void
426 read_self_insert(void)
428 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
429 global_key_unbound();
430 return;
433 minibuffer_self_insert();
436 static void
437 read_abort(void)
439 exit_minibuffer();
440 read_cb(NULL, read_data);
443 static void
444 read_select(void)
446 exit_minibuffer();
447 minibuffer_hist_save_entry();
448 read_cb(ministate.buf, read_data);
451 /*
452 * TODO: we should collect this asynchronously...
453 */
454 static inline void
455 populate_compl_buffer(complfn *fn, void *data)
457 const char *s, *descr;
458 struct line *l;
459 struct buffer *b;
460 struct parser *p;
461 void *linedata;
463 b = &ministate.compl.buffer;
464 p = &b->page;
466 linedata = NULL;
467 descr = NULL;
468 while ((s = fn(&data, &linedata, &descr)) != NULL) {
469 if ((l = calloc(1, sizeof(*l))) == NULL)
470 abort();
472 l->type = LINE_COMPL;
473 l->data = linedata;
474 l->alt = (char*)descr;
475 if ((l->line = strdup(s)) == NULL)
476 abort();
478 TAILQ_INSERT_TAIL(&p->head, l, lines);
480 linedata = NULL;
481 descr = NULL;
485 void
486 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
487 void (*abortfn)(void), struct histhead *hist,
488 complfn *complfn, void *compldata)
490 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
491 if (in_minibuffer == MB_COMPREAD) {
492 ui_schedule_redraw();
494 ministate.compl.fn = complfn;
495 ministate.compl.data = compldata;
496 populate_compl_buffer(complfn, compldata);
499 base_map = &minibuffer_map;
500 current_map = &minibuffer_map;
502 base_map->unhandled_input = self_insert_fn;
504 ministate.donefn = donefn;
505 ministate.abortfn = abortfn;
506 memset(ministate.buf, 0, sizeof(ministate.buf));
507 ministate.buffer.current_line = &ministate.vline;
508 ministate.buffer.current_line->line = ministate.buf;
509 ministate.buffer.cpoff = 0;
510 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
512 ministate.history = hist;
513 ministate.hist_cur = NULL;
514 ministate.hist_off = 0;
517 void
518 exit_minibuffer(void)
520 if (in_minibuffer == MB_COMPREAD) {
521 erase_buffer(&ministate.compl.buffer);
522 ui_schedule_redraw();
525 in_minibuffer = 0;
526 base_map = &global_map;
527 current_map = &global_map;
530 void
531 yornp(const char *prompt, void (*fn)(int, struct tab*),
532 struct tab *data)
534 size_t len;
536 if (in_minibuffer) {
537 fn(0, data);
538 return;
541 yornp_cb = fn;
542 yornp_data = data;
543 enter_minibuffer(yornp_self_insert, yornp_self_insert,
544 yornp_abort, NULL, NULL, NULL);
546 len = sizeof(ministate.prompt);
547 strlcpy(ministate.prompt, prompt, len);
548 strlcat(ministate.prompt, " (y or n) ", len);
551 void
552 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
553 struct tab *data)
555 size_t len;
557 if (in_minibuffer)
558 return;
560 read_cb = fn;
561 read_data = data;
562 enter_minibuffer(read_self_insert, read_select, read_abort,
563 &read_history, NULL, NULL);
565 len = sizeof(ministate.prompt);
566 strlcpy(ministate.prompt, prompt, len);
567 strlcat(ministate.prompt, ": ", len);
570 static void
571 handle_clear_echoarea(int fd, short ev, void *d)
573 free(ministate.curmesg);
574 ministate.curmesg = NULL;
576 ui_after_message_hook();
579 void
580 vmessage(const char *fmt, va_list ap)
582 if (evtimer_pending(&clechoev, NULL))
583 evtimer_del(&clechoev);
585 free(ministate.curmesg);
586 ministate.curmesg = NULL;
588 if (fmt != NULL) {
589 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
590 evtimer_add(&clechoev, &clechoev_timer);
592 /* TODO: what to do if the allocation fails here? */
593 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
594 ministate.curmesg = NULL;
597 ui_after_message_hook();
600 void
601 message(const char *fmt, ...)
603 va_list ap;
605 va_start(ap, fmt);
606 vmessage(fmt, ap);
607 va_end(ap);
610 void
611 minibuffer_init(void)
613 TAILQ_INIT(&eecmd_history.head);
614 TAILQ_INIT(&ir_history.head);
615 TAILQ_INIT(&lu_history.head);
616 TAILQ_INIT(&read_history.head);
618 TAILQ_INIT(&ministate.compl.buffer.head);
619 TAILQ_INIT(&ministate.compl.buffer.page.head);
621 ministate.line.type = LINE_TEXT;
622 ministate.vline.parent = &ministate.line;
623 ministate.buffer.page.name = "*minibuffer*";
624 ministate.buffer.current_line = &ministate.vline;
626 evtimer_set(&clechoev, handle_clear_echoarea, NULL);