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_gopher(void)
241 exit_minibuffer();
242 minibuffer_hist_save_entry();
244 gopher_send_search_req(current_tab, ministate.buf);
247 void
248 lu_select(void)
250 char url[GEMINI_URL_LEN+1];
252 exit_minibuffer();
253 minibuffer_hist_save_entry();
255 humanify_url(ministate.buf, url, sizeof(url));
256 load_url_in_tab(current_tab, url, NULL, LU_MODE_NOCACHE);
259 void
260 bp_select(void)
262 exit_minibuffer();
263 if (*ministate.buf != '\0')
264 add_to_bookmarks(ministate.buf);
265 else
266 message("Abort.");
269 void
270 ts_select(void)
272 struct tab *tab;
274 if ((tab = minibuffer_metadata()) == NULL) {
275 message("No tab selected");
276 return;
279 exit_minibuffer();
280 switch_to_tab(tab);
283 void
284 ls_select(void)
286 struct line *l;
288 if ((l = minibuffer_metadata()) == NULL) {
289 message("No link selected");
290 return;
293 exit_minibuffer();
294 load_url_in_tab(current_tab, l->alt, NULL, LU_MODE_NOCACHE);
297 static inline void
298 jump_to_line(struct line *l)
300 struct vline *vl;
301 struct buffer *buffer;
303 buffer = current_buffer();
305 TAILQ_FOREACH(vl, &buffer->head, vlines) {
306 if (vl->parent == l)
307 break;
310 if (vl == NULL)
311 message("Ops, %s error! Please report to %s",
312 __func__, PACKAGE_BUGREPORT);
313 else {
314 buffer->top_line = vl;
315 buffer->current_line = vl;
319 void
320 swiper_select(void)
322 struct line *l;
324 if ((l = minibuffer_metadata()) == NULL) {
325 message("No line selected");
326 return;
329 exit_minibuffer();
330 jump_to_line(l);
333 void
334 toc_select(void)
336 struct line *l;
338 if ((l = minibuffer_metadata()) == NULL) {
339 message("No line selected");
340 return;
343 exit_minibuffer();
344 jump_to_line(l);
347 static void
348 yornp_self_insert(void)
350 if (thiskey.key != 'y' && thiskey.key != 'n') {
351 message("Please answer y or n");
352 return;
355 exit_minibuffer();
356 yornp_cb(thiskey.key == 'y', yornp_data);
359 static void
360 yornp_abort(void)
362 exit_minibuffer();
363 yornp_cb(0, yornp_data);
366 static void
367 read_self_insert(void)
369 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
370 global_key_unbound();
371 return;
374 minibuffer_self_insert();
377 static void
378 read_abort(void)
380 exit_minibuffer();
381 read_cb(NULL, read_data);
384 static void
385 read_select(void)
387 exit_minibuffer();
388 minibuffer_hist_save_entry();
389 read_cb(ministate.buf, read_data);
392 /*
393 * TODO: we should collect this asynchronously...
394 */
395 static inline void
396 populate_compl_buffer(complfn *fn, void *data)
398 const char *s, *descr;
399 struct line *l;
400 struct buffer *b;
401 struct parser *p;
402 void *linedata;
404 b = &ministate.compl.buffer;
405 p = &b->page;
407 linedata = NULL;
408 descr = NULL;
409 while ((s = fn(&data, &linedata, &descr)) != NULL) {
410 if ((l = calloc(1, sizeof(*l))) == NULL)
411 abort();
413 l->type = LINE_COMPL;
414 l->data = linedata;
415 l->alt = (char*)descr;
416 if ((l->line = strdup(s)) == NULL)
417 abort();
419 TAILQ_INSERT_TAIL(&p->head, l, lines);
421 linedata = NULL;
422 descr = NULL;
425 if ((l = TAILQ_FIRST(&p->head)) != NULL)
426 l->type = LINE_COMPL_CURRENT;
429 void
430 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
431 void (*abortfn)(void), struct histhead *hist,
432 complfn *complfn, void *compldata)
434 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
435 if (in_minibuffer == MB_COMPREAD) {
436 ui_schedule_redraw();
438 ministate.compl.fn = complfn;
439 ministate.compl.data = compldata;
440 populate_compl_buffer(complfn, compldata);
443 base_map = &minibuffer_map;
444 current_map = &minibuffer_map;
446 base_map->unhandled_input = self_insert_fn;
448 ministate.donefn = donefn;
449 ministate.abortfn = abortfn;
450 memset(ministate.buf, 0, sizeof(ministate.buf));
451 ministate.buffer.current_line = &ministate.vline;
452 ministate.buffer.current_line->line = ministate.buf;
453 ministate.buffer.cpoff = 0;
454 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
456 ministate.history = hist;
457 ministate.hist_cur = NULL;
458 ministate.hist_off = 0;
461 void
462 exit_minibuffer(void)
464 if (in_minibuffer == MB_COMPREAD) {
465 erase_buffer(&ministate.compl.buffer);
466 ui_schedule_redraw();
469 in_minibuffer = 0;
470 base_map = &global_map;
471 current_map = &global_map;
474 void
475 yornp(const char *prompt, void (*fn)(int, struct tab*),
476 struct tab *data)
478 size_t len;
480 if (in_minibuffer) {
481 fn(0, data);
482 return;
485 yornp_cb = fn;
486 yornp_data = data;
487 enter_minibuffer(yornp_self_insert, yornp_self_insert,
488 yornp_abort, NULL, NULL, NULL);
490 len = sizeof(ministate.prompt);
491 strlcpy(ministate.prompt, prompt, len);
492 strlcat(ministate.prompt, " (y or n) ", len);
495 void
496 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
497 struct tab *data)
499 size_t len;
501 if (in_minibuffer)
502 return;
504 read_cb = fn;
505 read_data = data;
506 enter_minibuffer(read_self_insert, read_select, read_abort,
507 &read_history, NULL, NULL);
509 len = sizeof(ministate.prompt);
510 strlcpy(ministate.prompt, prompt, len);
511 strlcat(ministate.prompt, ": ", len);
514 static void
515 handle_clear_echoarea(int fd, short ev, void *d)
517 free(ministate.curmesg);
518 ministate.curmesg = NULL;
520 ui_after_message_hook();
523 void
524 vmessage(const char *fmt, va_list ap)
526 if (evtimer_pending(&clechoev, NULL))
527 evtimer_del(&clechoev);
529 free(ministate.curmesg);
530 ministate.curmesg = NULL;
532 if (fmt != NULL) {
533 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
534 evtimer_add(&clechoev, &clechoev_timer);
536 /* TODO: what to do if the allocation fails here? */
537 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
538 ministate.curmesg = NULL;
541 ui_after_message_hook();
544 void
545 message(const char *fmt, ...)
547 va_list ap;
549 va_start(ap, fmt);
550 vmessage(fmt, ap);
551 va_end(ap);
554 void
555 minibuffer_init(void)
557 TAILQ_INIT(&eecmd_history.head);
558 TAILQ_INIT(&ir_history.head);
559 TAILQ_INIT(&lu_history.head);
560 TAILQ_INIT(&read_history.head);
562 TAILQ_INIT(&ministate.compl.buffer.head);
563 TAILQ_INIT(&ministate.compl.buffer.page.head);
565 ministate.line.type = LINE_TEXT;
566 ministate.vline.parent = &ministate.line;
567 ministate.buffer.page.name = "*minibuffer*";
568 ministate.buffer.current_line = &ministate.vline;
570 evtimer_set(&clechoev, handle_clear_echoarea, NULL);