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"
28 static void *minibuffer_metadata(void);
29 static void minibuffer_hist_save_entry(void);
30 static void yornp_self_insert(void);
31 static void yornp_abort(void);
32 static void read_self_insert(void);
33 static void read_abort(void);
34 static void read_select(void);
35 static void handle_clear_echoarea(int, short, void *);
37 static struct event clechoev;
38 static struct timeval clechoev_timer = { 5, 0 };
40 static void (*yornp_cb)(int, struct tab *);
41 static struct tab *yornp_data;
43 static void (*read_cb)(const char*, struct tab *);
44 static struct tab *read_data;
46 /* XXX: don't forget to init these in minibuffer_init */
47 struct histhead eecmd_history,
48 ir_history,
49 lu_history,
50 read_history;
52 struct ministate ministate;
54 struct buffer minibufferwin;
56 int in_minibuffer;
58 /*
59 * Recompute the visible completions. If add is 1, don't consider the
60 * ones already hidden.
61 */
62 void
63 recompute_completions(int add)
64 {
65 struct line *l;
66 struct vline *vl;
67 struct buffer *b;
69 if (in_minibuffer != MB_COMPREAD)
70 return;
72 b = &ministate.compl.buffer;
73 TAILQ_FOREACH(l, &b->page.head, lines) {
74 l->type = LINE_COMPL;
75 if (add && l->flags & L_HIDDEN)
76 continue;
77 if (strcasestr(l->line, ministate.buf) != NULL ||
78 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
79 if (l->flags & L_HIDDEN)
80 b->line_max++;
81 l->flags &= ~L_HIDDEN;
82 } else {
83 if (!(l->flags & L_HIDDEN))
84 b->line_max--;
85 l->flags |= L_HIDDEN;
86 }
87 }
89 if (b->current_line == NULL)
90 b->current_line = TAILQ_FIRST(&b->head);
91 b->current_line = adjust_line(b->current_line, b);
92 vl = b->current_line;
93 if (vl != NULL)
94 vl->parent->type = LINE_COMPL_CURRENT;
95 }
97 int
98 minibuffer_insert_current_candidate(void)
99 {
100 struct vline *vl;
102 vl = ministate.compl.buffer.current_line;
103 if (vl == NULL || vl->parent->flags & L_HIDDEN)
104 return -1;
106 minibuffer_taint_hist();
107 strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
108 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
110 return 0;
113 static void *
114 minibuffer_metadata(void)
116 struct vline *vl;
118 vl = ministate.compl.buffer.current_line;
120 if (vl == NULL || vl->parent->flags & L_HIDDEN)
121 return NULL;
123 return vl->parent->data;
126 static void
127 minibuffer_hist_save_entry(void)
129 struct hist *hist;
131 if (ministate.history == NULL)
132 return;
134 if ((hist = calloc(1, sizeof(*hist))) == NULL)
135 abort();
137 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
139 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
140 ministate.history->len++;
143 /*
144 * taint the minibuffer cache: if we're currently showing a history
145 * element, copy that to the current buf and reset the "history
146 * navigation" thing.
147 */
148 void
149 minibuffer_taint_hist(void)
151 if (ministate.hist_cur == NULL)
152 return;
154 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
155 ministate.hist_cur = NULL;
156 ministate.buffer.current_line->line = ministate.buf;
159 void
160 minibuffer_self_insert(void)
162 char *c, tmp[5] = {0};
163 size_t len;
165 minibuffer_taint_hist();
167 if (thiskey.cp == 0)
168 return;
170 len = utf8_encode(thiskey.cp, tmp);
171 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
172 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
173 return;
175 memmove(c + len, c, strlen(c)+1);
176 memcpy(c, tmp, len);
177 ministate.buffer.cpoff++;
179 recompute_completions(1);
182 void
183 sensible_self_insert(void)
185 if (thiskey.meta ||
186 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
187 global_key_unbound();
188 return;
191 minibuffer_self_insert();
194 void
195 eecmd_select(void)
197 struct cmd *cmd;
198 struct vline *vl;
199 const char *t;
201 vl = ministate.compl.buffer.current_line;
202 if (vl == NULL || vl->parent->flags & L_HIDDEN)
203 goto end;
205 t = vl->parent->line;
206 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
207 if (!strcmp(cmd->cmd, t)) {
208 minibuffer_insert_current_candidate();
209 exit_minibuffer();
210 minibuffer_hist_save_entry();
211 cmd->fn(current_buffer());
212 return;
216 end:
217 message("No match");
220 void
221 ir_select_gemini(void)
223 char buf[1025] = {0};
224 struct phos_uri uri;
225 struct tab *tab = current_tab;
227 exit_minibuffer();
228 minibuffer_hist_save_entry();
230 /* a bit ugly but... */
231 memcpy(&uri, &tab->uri, sizeof(tab->uri));
232 phos_uri_set_query(&uri, ministate.buf);
233 phos_serialize_uri(&uri, buf, sizeof(buf));
234 load_url_in_tab(tab, buf, NULL, 0);
237 void
238 ir_select_gopher(void)
240 exit_minibuffer();
241 minibuffer_hist_save_entry();
243 gopher_send_search_req(current_tab, ministate.buf);
246 void
247 lu_select(void)
249 char url[GEMINI_URL_LEN+1];
251 exit_minibuffer();
252 minibuffer_hist_save_entry();
254 humanify_url(ministate.buf, url, sizeof(url));
255 load_url_in_tab(current_tab, url, NULL, 0);
258 void
259 bp_select(void)
261 exit_minibuffer();
262 if (*ministate.buf != '\0')
263 add_to_bookmarks(ministate.buf);
264 else
265 message("Abort.");
268 void
269 ts_select(void)
271 struct tab *tab;
273 if ((tab = minibuffer_metadata()) == NULL) {
274 message("No tab selected");
275 return;
278 exit_minibuffer();
279 switch_to_tab(tab);
282 void
283 ls_select(void)
285 struct line *l;
287 if ((l = minibuffer_metadata()) == NULL) {
288 message("No link selected");
289 return;
292 exit_minibuffer();
293 load_url_in_tab(current_tab, l->alt, NULL, 0);
296 static inline void
297 jump_to_line(struct line *l)
299 struct vline *vl;
300 struct buffer *buffer;
302 buffer = current_buffer();
304 TAILQ_FOREACH(vl, &buffer->head, vlines) {
305 if (vl->parent == l)
306 break;
309 if (vl == NULL)
310 message("Ops, %s error! Please report to %s",
311 __func__, PACKAGE_BUGREPORT);
312 else {
313 buffer->top_line = vl;
314 buffer->current_line = vl;
318 void
319 swiper_select(void)
321 struct line *l;
323 if ((l = minibuffer_metadata()) == NULL) {
324 message("No line selected");
325 return;
328 exit_minibuffer();
329 jump_to_line(l);
332 void
333 toc_select(void)
335 struct line *l;
337 if ((l = minibuffer_metadata()) == NULL) {
338 message("No line selected");
339 return;
342 exit_minibuffer();
343 jump_to_line(l);
346 static void
347 yornp_self_insert(void)
349 if (thiskey.key != 'y' && thiskey.key != 'n') {
350 message("Please answer y or n");
351 return;
354 exit_minibuffer();
355 yornp_cb(thiskey.key == 'y', yornp_data);
358 static void
359 yornp_abort(void)
361 exit_minibuffer();
362 yornp_cb(0, yornp_data);
365 static void
366 read_self_insert(void)
368 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
369 global_key_unbound();
370 return;
373 minibuffer_self_insert();
376 static void
377 read_abort(void)
379 exit_minibuffer();
380 read_cb(NULL, read_data);
383 static void
384 read_select(void)
386 exit_minibuffer();
387 minibuffer_hist_save_entry();
388 read_cb(ministate.buf, read_data);
391 /*
392 * TODO: we should collect this asynchronously...
393 */
394 static inline void
395 populate_compl_buffer(complfn *fn, void *data)
397 const char *s, *descr;
398 struct line *l;
399 struct buffer *b;
400 struct parser *p;
401 void *linedata;
403 b = &ministate.compl.buffer;
404 p = &b->page;
406 linedata = NULL;
407 descr = NULL;
408 while ((s = fn(&data, &linedata, &descr)) != NULL) {
409 if ((l = calloc(1, sizeof(*l))) == NULL)
410 abort();
412 l->type = LINE_COMPL;
413 l->data = linedata;
414 l->alt = (char*)descr;
415 if ((l->line = strdup(s)) == NULL)
416 abort();
418 TAILQ_INSERT_TAIL(&p->head, l, lines);
420 linedata = NULL;
421 descr = NULL;
424 if ((l = TAILQ_FIRST(&p->head)) != NULL)
425 l->type = LINE_COMPL_CURRENT;
428 void
429 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
430 void (*abortfn)(void), struct histhead *hist,
431 complfn *complfn, void *compldata)
433 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
434 if (in_minibuffer == MB_COMPREAD) {
435 ui_schedule_redraw();
437 ministate.compl.fn = complfn;
438 ministate.compl.data = compldata;
439 populate_compl_buffer(complfn, compldata);
442 base_map = &minibuffer_map;
443 current_map = &minibuffer_map;
445 base_map->unhandled_input = self_insert_fn;
447 ministate.donefn = donefn;
448 ministate.abortfn = abortfn;
449 memset(ministate.buf, 0, sizeof(ministate.buf));
450 ministate.buffer.current_line = &ministate.vline;
451 ministate.buffer.current_line->line = ministate.buf;
452 ministate.buffer.cpoff = 0;
453 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
455 ministate.history = hist;
456 ministate.hist_cur = NULL;
457 ministate.hist_off = 0;
460 void
461 exit_minibuffer(void)
463 if (in_minibuffer == MB_COMPREAD) {
464 erase_buffer(&ministate.compl.buffer);
465 ui_schedule_redraw();
468 in_minibuffer = 0;
469 base_map = &global_map;
470 current_map = &global_map;
473 void
474 yornp(const char *prompt, void (*fn)(int, struct tab*),
475 struct tab *data)
477 size_t len;
479 if (in_minibuffer) {
480 fn(0, data);
481 return;
484 yornp_cb = fn;
485 yornp_data = data;
486 enter_minibuffer(yornp_self_insert, yornp_self_insert,
487 yornp_abort, NULL, NULL, NULL);
489 len = sizeof(ministate.prompt);
490 strlcpy(ministate.prompt, prompt, len);
491 strlcat(ministate.prompt, " (y or n) ", len);
494 void
495 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
496 struct tab *data)
498 size_t len;
500 if (in_minibuffer)
501 return;
503 read_cb = fn;
504 read_data = data;
505 enter_minibuffer(read_self_insert, read_select, read_abort,
506 &read_history, NULL, NULL);
508 len = sizeof(ministate.prompt);
509 strlcpy(ministate.prompt, prompt, len);
510 strlcat(ministate.prompt, ": ", len);
513 static void
514 handle_clear_echoarea(int fd, short ev, void *d)
516 free(ministate.curmesg);
517 ministate.curmesg = NULL;
519 ui_after_message_hook();
522 void
523 vmessage(const char *fmt, va_list ap)
525 if (evtimer_pending(&clechoev, NULL))
526 evtimer_del(&clechoev);
528 free(ministate.curmesg);
529 ministate.curmesg = NULL;
531 if (fmt != NULL) {
532 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
533 evtimer_add(&clechoev, &clechoev_timer);
535 /* TODO: what to do if the allocation fails here? */
536 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
537 ministate.curmesg = NULL;
540 ui_after_message_hook();
543 void
544 message(const char *fmt, ...)
546 va_list ap;
548 va_start(ap, fmt);
549 vmessage(fmt, ap);
550 va_end(ap);
553 void
554 minibuffer_init(void)
556 TAILQ_INIT(&eecmd_history.head);
557 TAILQ_INIT(&ir_history.head);
558 TAILQ_INIT(&lu_history.head);
559 TAILQ_INIT(&read_history.head);
561 TAILQ_INIT(&ministate.compl.buffer.head);
562 TAILQ_INIT(&ministate.compl.buffer.page.head);
564 ministate.line.type = LINE_TEXT;
565 ministate.vline.parent = &ministate.line;
566 ministate.buffer.page.name = "*minibuffer*";
567 ministate.buffer.current_line = &ministate.vline;
569 evtimer_set(&clechoev, handle_clear_echoarea, NULL);