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 static void *minibuffer_metadata(void);
30 static void minibuffer_hist_save_entry(void);
31 static void yornp_self_insert(void);
32 static void yornp_abort(void);
33 static void read_self_insert(void);
34 static void read_abort(void);
35 static void read_select(void);
36 static void handle_clear_echoarea(int, short, void *);
38 static struct event clechoev;
39 static struct timeval clechoev_timer = { 5, 0 };
41 static void (*yornp_cb)(int, struct tab *);
42 static struct tab *yornp_data;
44 static void (*read_cb)(const char*, struct tab *);
45 static struct tab *read_data;
47 /* XXX: don't forget to init these in minibuffer_init */
48 struct histhead eecmd_history,
49 ir_history,
50 lu_history,
51 read_history;
53 struct ministate ministate;
55 struct buffer minibufferwin;
57 int in_minibuffer;
59 /*
60 * Recompute the visible completions. If add is 1, don't consider the
61 * ones already hidden.
62 */
63 void
64 recompute_completions(int add)
65 {
66 struct line *l;
67 struct vline *vl;
68 struct buffer *b;
70 if (in_minibuffer != MB_COMPREAD)
71 return;
73 b = &ministate.compl.buffer;
74 TAILQ_FOREACH(l, &b->page.head, lines) {
75 l->type = LINE_COMPL;
76 if (add && l->flags & L_HIDDEN)
77 continue;
78 if (strcasestr(l->line, ministate.buf) != NULL ||
79 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
80 if (l->flags & L_HIDDEN)
81 b->line_max++;
82 l->flags &= ~L_HIDDEN;
83 } else {
84 if (!(l->flags & L_HIDDEN))
85 b->line_max--;
86 l->flags |= L_HIDDEN;
87 }
88 }
90 if (b->current_line == NULL)
91 b->current_line = TAILQ_FIRST(&b->head);
92 b->current_line = adjust_line(b->current_line, b);
93 vl = b->current_line;
94 if (vl != NULL)
95 vl->parent->type = LINE_COMPL_CURRENT;
96 }
98 int
99 minibuffer_insert_current_candidate(void)
101 struct vline *vl;
103 vl = ministate.compl.buffer.current_line;
104 if (vl == NULL || vl->parent->flags & L_HIDDEN)
105 return -1;
107 minibuffer_taint_hist();
108 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
109 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
111 return 0;
114 static void *
115 minibuffer_metadata(void)
117 struct vline *vl;
119 vl = ministate.compl.buffer.current_line;
121 if (vl == NULL || vl->parent->flags & L_HIDDEN)
122 return NULL;
124 return vl->parent->data;
127 static void
128 minibuffer_hist_save_entry(void)
130 struct hist *hist;
132 if (ministate.history == NULL)
133 return;
135 if ((hist = calloc(1, sizeof(*hist))) == NULL)
136 abort();
138 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
140 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
141 ministate.history->len++;
144 /*
145 * taint the minibuffer cache: if we're currently showing a history
146 * element, copy that to the current buf and reset the "history
147 * navigation" thing.
148 */
149 void
150 minibuffer_taint_hist(void)
152 if (ministate.hist_cur == NULL)
153 return;
155 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
156 ministate.hist_cur = NULL;
157 ministate.buffer.current_line->line = ministate.buf;
160 void
161 minibuffer_self_insert(void)
163 char *c, tmp[5] = {0};
164 size_t len;
166 minibuffer_taint_hist();
168 if (thiskey.cp == 0)
169 return;
171 len = utf8_encode(thiskey.cp, tmp);
172 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
173 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
174 return;
176 memmove(c + len, c, strlen(c)+1);
177 memcpy(c, tmp, len);
178 ministate.buffer.cpoff++;
180 recompute_completions(1);
183 void
184 sensible_self_insert(void)
186 if (thiskey.meta ||
187 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
188 global_key_unbound();
189 return;
192 minibuffer_self_insert();
195 void
196 eecmd_select(void)
198 struct cmd *cmd;
199 struct vline *vl;
200 const char *t;
202 vl = ministate.compl.buffer.current_line;
203 if (vl == NULL || vl->parent->flags & L_HIDDEN)
204 goto end;
206 t = vl->parent->line;
207 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
208 if (!strcmp(cmd->cmd, t)) {
209 minibuffer_insert_current_candidate();
210 exit_minibuffer();
211 minibuffer_hist_save_entry();
212 cmd->fn(current_buffer());
213 return;
217 end:
218 message("No match");
221 void
222 ir_select_gemini(void)
224 char buf[1025] = {0};
225 struct phos_uri uri;
226 struct tab *tab = current_tab;
228 exit_minibuffer();
229 minibuffer_hist_save_entry();
231 /* a bit ugly but... */
232 memcpy(&uri, &tab->uri, sizeof(tab->uri));
233 phos_uri_set_query(&uri, ministate.buf);
234 phos_serialize_uri(&uri, buf, sizeof(buf));
235 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
238 void
239 ir_select_reply(void)
241 char buf[1025] = {0};
242 struct phos_uri uri;
243 struct tab *tab = current_tab;
245 exit_minibuffer();
246 minibuffer_hist_save_entry();
248 /* a bit ugly but... */
249 strlcpy(buf, tab->last_input_url, sizeof(buf));
250 phos_parse_absolute_uri(buf, &uri);
251 phos_uri_set_query(&uri, ministate.buf);
252 phos_serialize_uri(&uri, buf, sizeof(buf));
253 load_url_in_tab(tab, buf, NULL, LU_MODE_NOCACHE);
256 void
257 ir_select_gopher(void)
259 exit_minibuffer();
260 minibuffer_hist_save_entry();
262 gopher_send_search_req(current_tab, ministate.buf);
265 void
266 lu_select(void)
268 char url[GEMINI_URL_LEN+1];
270 exit_minibuffer();
271 minibuffer_hist_save_entry();
273 humanify_url(ministate.buf, url, sizeof(url));
274 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
277 void
278 bp_select(void)
280 exit_minibuffer();
281 if (*ministate.buf != '\0')
282 add_to_bookmarks(ministate.buf);
283 else
284 message("Abort.");
287 void
288 ts_select(void)
290 struct tab *tab;
292 if ((tab = minibuffer_metadata()) == NULL) {
293 message("No tab selected");
294 return;
297 exit_minibuffer();
298 switch_to_tab(tab);
301 void
302 ls_select(void)
304 struct line *l;
306 if ((l = minibuffer_metadata()) == NULL) {
307 message("No link selected");
308 return;
311 exit_minibuffer();
312 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
315 static inline void
316 jump_to_line(struct line *l)
318 struct vline *vl;
319 struct buffer *buffer;
321 buffer = current_buffer();
323 TAILQ_FOREACH(vl, &buffer->head, vlines) {
324 if (vl->parent == l)
325 break;
328 if (vl == NULL)
329 message("Ops, %s error! Please report to %s",
330 __func__, PACKAGE_BUGREPORT);
331 else {
332 buffer->top_line = vl;
333 buffer->current_line = vl;
337 void
338 swiper_select(void)
340 struct line *l;
342 if ((l = minibuffer_metadata()) == NULL) {
343 message("No line selected");
344 return;
347 exit_minibuffer();
348 jump_to_line(l);
351 void
352 toc_select(void)
354 struct line *l;
356 if ((l = minibuffer_metadata()) == NULL) {
357 message("No line selected");
358 return;
361 exit_minibuffer();
362 jump_to_line(l);
365 static void
366 yornp_self_insert(void)
368 if (thiskey.key != 'y' && thiskey.key != 'n') {
369 message("Please answer y or n");
370 return;
373 exit_minibuffer();
374 yornp_cb(thiskey.key == 'y', yornp_data);
377 static void
378 yornp_abort(void)
380 exit_minibuffer();
381 yornp_cb(0, yornp_data);
384 static void
385 read_self_insert(void)
387 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
388 global_key_unbound();
389 return;
392 minibuffer_self_insert();
395 static void
396 read_abort(void)
398 exit_minibuffer();
399 read_cb(NULL, read_data);
402 static void
403 read_select(void)
405 exit_minibuffer();
406 minibuffer_hist_save_entry();
407 read_cb(ministate.buf, read_data);
410 /*
411 * TODO: we should collect this asynchronously...
412 */
413 static inline void
414 populate_compl_buffer(complfn *fn, void *data)
416 const char *s, *descr;
417 struct line *l;
418 struct buffer *b;
419 struct parser *p;
420 void *linedata;
422 b = &ministate.compl.buffer;
423 p = &b->page;
425 linedata = NULL;
426 descr = NULL;
427 while ((s = fn(&data, &linedata, &descr)) != NULL) {
428 if ((l = calloc(1, sizeof(*l))) == NULL)
429 abort();
431 l->type = LINE_COMPL;
432 l->data = linedata;
433 l->alt = (char*)descr;
434 if ((l->line = strdup(s)) == NULL)
435 abort();
437 TAILQ_INSERT_TAIL(&p->head, l, lines);
439 linedata = NULL;
440 descr = NULL;
443 if ((l = TAILQ_FIRST(&p->head)) != NULL)
444 l->type = LINE_COMPL_CURRENT;
447 void
448 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
449 void (*abortfn)(void), struct histhead *hist,
450 complfn *complfn, void *compldata)
452 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
453 if (in_minibuffer == MB_COMPREAD) {
454 ui_schedule_redraw();
456 ministate.compl.fn = complfn;
457 ministate.compl.data = compldata;
458 populate_compl_buffer(complfn, compldata);
461 base_map = &minibuffer_map;
462 current_map = &minibuffer_map;
464 base_map->unhandled_input = self_insert_fn;
466 ministate.donefn = donefn;
467 ministate.abortfn = abortfn;
468 memset(ministate.buf, 0, sizeof(ministate.buf));
469 ministate.buffer.current_line = &ministate.vline;
470 ministate.buffer.current_line->line = ministate.buf;
471 ministate.buffer.cpoff = 0;
472 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
474 ministate.history = hist;
475 ministate.hist_cur = NULL;
476 ministate.hist_off = 0;
479 void
480 exit_minibuffer(void)
482 if (in_minibuffer == MB_COMPREAD) {
483 erase_buffer(&ministate.compl.buffer);
484 ui_schedule_redraw();
487 in_minibuffer = 0;
488 base_map = &global_map;
489 current_map = &global_map;
492 void
493 yornp(const char *prompt, void (*fn)(int, struct tab*),
494 struct tab *data)
496 size_t len;
498 if (in_minibuffer) {
499 fn(0, data);
500 return;
503 yornp_cb = fn;
504 yornp_data = data;
505 enter_minibuffer(yornp_self_insert, yornp_self_insert,
506 yornp_abort, NULL, NULL, NULL);
508 len = sizeof(ministate.prompt);
509 strlcpy(ministate.prompt, prompt, len);
510 strlcat(ministate.prompt, " (y or n) ", len);
513 void
514 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
515 struct tab *data)
517 size_t len;
519 if (in_minibuffer)
520 return;
522 read_cb = fn;
523 read_data = data;
524 enter_minibuffer(read_self_insert, read_select, read_abort,
525 &read_history, NULL, NULL);
527 len = sizeof(ministate.prompt);
528 strlcpy(ministate.prompt, prompt, len);
529 strlcat(ministate.prompt, ": ", len);
532 static void
533 handle_clear_echoarea(int fd, short ev, void *d)
535 free(ministate.curmesg);
536 ministate.curmesg = NULL;
538 ui_after_message_hook();
541 void
542 vmessage(const char *fmt, va_list ap)
544 if (evtimer_pending(&clechoev, NULL))
545 evtimer_del(&clechoev);
547 free(ministate.curmesg);
548 ministate.curmesg = NULL;
550 if (fmt != NULL) {
551 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
552 evtimer_add(&clechoev, &clechoev_timer);
554 /* TODO: what to do if the allocation fails here? */
555 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
556 ministate.curmesg = NULL;
559 ui_after_message_hook();
562 void
563 message(const char *fmt, ...)
565 va_list ap;
567 va_start(ap, fmt);
568 vmessage(fmt, ap);
569 va_end(ap);
572 void
573 minibuffer_init(void)
575 TAILQ_INIT(&eecmd_history.head);
576 TAILQ_INIT(&ir_history.head);
577 TAILQ_INIT(&lu_history.head);
578 TAILQ_INIT(&read_history.head);
580 TAILQ_INIT(&ministate.compl.buffer.head);
581 TAILQ_INIT(&ministate.compl.buffer.page.head);
583 ministate.line.type = LINE_TEXT;
584 ministate.vline.parent = &ministate.line;
585 ministate.buffer.page.name = "*minibuffer*";
586 ministate.buffer.current_line = &ministate.vline;
588 evtimer_set(&clechoev, handle_clear_echoarea, NULL);