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 "ui.h"
25 #include "utf8.h"
27 static void *minibuffer_metadata(void);
28 static void minibuffer_hist_save_entry(void);
29 static void yornp_self_insert(void);
30 static void yornp_abort(void);
31 static void read_self_insert(void);
32 static void read_abort(void);
33 static void read_select(void);
34 static void handle_clear_echoarea(int, short, void *);
36 static struct event clechoev;
37 static struct timeval clechoev_timer = { 5, 0 };
39 static void (*yornp_cb)(int, struct tab *);
40 static struct tab *yornp_data;
42 static void (*read_cb)(const char*, struct tab *);
43 static struct tab *read_data;
45 /* XXX: don't forget to init these in minibuffer_init */
46 struct histhead eecmd_history,
47 ir_history,
48 lu_history,
49 read_history;
51 struct ministate ministate;
53 struct buffer minibufferwin;
55 int in_minibuffer;
57 /*
58 * Recompute the visible completions. If add is 1, don't consider the
59 * ones already hidden.
60 */
61 void
62 recompute_completions(int add)
63 {
64 struct line *l;
65 struct vline *vl;
66 struct buffer *b;
68 if (in_minibuffer != MB_COMPREAD)
69 return;
71 b = &ministate.compl.buffer;
72 TAILQ_FOREACH(l, &b->page.head, lines) {
73 l->type = LINE_COMPL;
74 if (add && l->flags & L_HIDDEN)
75 continue;
76 if (strcasestr(l->line, ministate.buf) != NULL ||
77 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
78 if (l->flags & L_HIDDEN)
79 b->line_max++;
80 l->flags &= ~L_HIDDEN;
81 } else {
82 if (!(l->flags & L_HIDDEN))
83 b->line_max--;
84 l->flags |= L_HIDDEN;
85 }
86 }
88 if (b->current_line == NULL)
89 b->current_line = TAILQ_FIRST(&b->head);
90 b->current_line = adjust_line(b->current_line, b);
91 vl = b->current_line;
92 if (vl != NULL)
93 vl->parent->type = LINE_COMPL_CURRENT;
94 }
96 static void *
97 minibuffer_metadata(void)
98 {
99 struct vline *vl;
101 vl = ministate.compl.buffer.current_line;
103 if (vl == NULL || vl->parent->flags & L_HIDDEN)
104 return NULL;
106 return vl->parent->data;
109 static void
110 minibuffer_hist_save_entry(void)
112 struct hist *hist;
114 if (ministate.history == NULL)
115 return;
117 if ((hist = calloc(1, sizeof(*hist))) == NULL)
118 abort();
120 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
122 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
123 ministate.history->len++;
126 /*
127 * taint the minibuffer cache: if we're currently showing a history
128 * element, copy that to the current buf and reset the "history
129 * navigation" thing.
130 */
131 void
132 minibuffer_taint_hist(void)
134 if (ministate.hist_cur == NULL)
135 return;
137 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
138 ministate.hist_cur = NULL;
139 ministate.buffer.current_line->line = ministate.buf;
142 void
143 minibuffer_self_insert(void)
145 char *c, tmp[5] = {0};
146 size_t len;
148 minibuffer_taint_hist();
150 if (thiskey.cp == 0)
151 return;
153 len = utf8_encode(thiskey.cp, tmp);
154 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
155 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
156 return;
158 memmove(c + len, c, strlen(c)+1);
159 memcpy(c, tmp, len);
160 ministate.buffer.cpoff++;
162 recompute_completions(1);
165 void
166 sensible_self_insert(void)
168 if (thiskey.meta ||
169 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
170 global_key_unbound();
171 return;
174 minibuffer_self_insert();
177 void
178 eecmd_select(void)
180 struct cmd *cmd;
182 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
183 if (!strcmp(cmd->cmd, ministate.buf)) {
184 exit_minibuffer();
185 minibuffer_hist_save_entry();
186 cmd->fn(current_buffer());
187 return;
191 message("No match");
194 void
195 ir_select_gemini(void)
197 char buf[1025] = {0};
198 struct phos_uri uri;
199 struct tab *tab = current_tab;
201 exit_minibuffer();
202 minibuffer_hist_save_entry();
204 /* a bit ugly but... */
205 memcpy(&uri, &tab->uri, sizeof(tab->uri));
206 phos_uri_set_query(&uri, ministate.buf);
207 phos_serialize_uri(&uri, buf, sizeof(buf));
208 load_url_in_tab(tab, buf, NULL, 0);
211 void
212 ir_select_gopher(void)
214 exit_minibuffer();
215 minibuffer_hist_save_entry();
217 gopher_send_search_req(current_tab, ministate.buf);
220 void
221 lu_select(void)
223 exit_minibuffer();
224 minibuffer_hist_save_entry();
225 load_url_in_tab(current_tab, ministate.buf, NULL, 0);
228 void
229 bp_select(void)
231 exit_minibuffer();
232 if (*ministate.buf != '\0')
233 add_to_bookmarks(ministate.buf);
234 else
235 message("Abort.");
238 void
239 ts_select(void)
241 struct tab *tab;
243 if ((tab = minibuffer_metadata()) == NULL) {
244 message("No tab selected");
245 return;
248 exit_minibuffer();
249 switch_to_tab(tab);
252 void
253 ls_select(void)
255 struct line *l;
257 if ((l = minibuffer_metadata()) == NULL) {
258 message("No link selected");
259 return;
262 exit_minibuffer();
263 load_url_in_tab(current_tab, l->alt, NULL, 0);
266 static inline void
267 jump_to_line(struct line *l)
269 struct vline *vl;
270 struct buffer *buffer;
272 buffer = current_buffer();
274 TAILQ_FOREACH(vl, &buffer->head, vlines) {
275 if (vl->parent == l)
276 break;
279 if (vl == NULL)
280 message("Ops, %s error! Please report to %s",
281 __func__, PACKAGE_BUGREPORT);
282 else {
283 buffer->top_line = vl;
284 buffer->current_line = vl;
288 void
289 swiper_select(void)
291 struct line *l;
293 if ((l = minibuffer_metadata()) == NULL) {
294 message("No line selected");
295 return;
298 exit_minibuffer();
299 jump_to_line(l);
302 void
303 toc_select(void)
305 struct line *l;
307 if ((l = minibuffer_metadata()) == NULL) {
308 message("No line selected");
309 return;
312 exit_minibuffer();
313 jump_to_line(l);
316 static void
317 yornp_self_insert(void)
319 if (thiskey.key != 'y' && thiskey.key != 'n') {
320 message("Please answer y or n");
321 return;
324 exit_minibuffer();
325 yornp_cb(thiskey.key == 'y', yornp_data);
328 static void
329 yornp_abort(void)
331 exit_minibuffer();
332 yornp_cb(0, yornp_data);
335 static void
336 read_self_insert(void)
338 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
339 global_key_unbound();
340 return;
343 minibuffer_self_insert();
346 static void
347 read_abort(void)
349 exit_minibuffer();
350 read_cb(NULL, read_data);
353 static void
354 read_select(void)
356 exit_minibuffer();
357 minibuffer_hist_save_entry();
358 read_cb(ministate.buf, read_data);
361 /*
362 * TODO: we should collect this asynchronously...
363 */
364 static inline void
365 populate_compl_buffer(complfn *fn, void *data)
367 const char *s, *descr;
368 struct line *l;
369 struct buffer *b;
370 struct parser *p;
371 void *linedata;
373 b = &ministate.compl.buffer;
374 p = &b->page;
376 linedata = NULL;
377 descr = NULL;
378 while ((s = fn(&data, &linedata, &descr)) != NULL) {
379 if ((l = calloc(1, sizeof(*l))) == NULL)
380 abort();
382 l->type = LINE_COMPL;
383 l->data = linedata;
384 l->alt = (char*)descr;
385 if ((l->line = strdup(s)) == NULL)
386 abort();
388 TAILQ_INSERT_TAIL(&p->head, l, lines);
390 linedata = NULL;
391 descr = NULL;
394 if ((l = TAILQ_FIRST(&p->head)) != NULL)
395 l->type = LINE_COMPL_CURRENT;
398 void
399 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
400 void (*abortfn)(void), struct histhead *hist,
401 complfn *complfn, void *compldata)
403 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
404 if (in_minibuffer == MB_COMPREAD) {
405 ui_schedule_redraw();
407 ministate.compl.fn = complfn;
408 ministate.compl.data = compldata;
409 populate_compl_buffer(complfn, compldata);
412 base_map = &minibuffer_map;
413 current_map = &minibuffer_map;
415 base_map->unhandled_input = self_insert_fn;
417 ministate.donefn = donefn;
418 ministate.abortfn = abortfn;
419 memset(ministate.buf, 0, sizeof(ministate.buf));
420 ministate.buffer.current_line = &ministate.vline;
421 ministate.buffer.current_line->line = ministate.buf;
422 ministate.buffer.cpoff = 0;
423 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
425 ministate.history = hist;
426 ministate.hist_cur = NULL;
427 ministate.hist_off = 0;
430 void
431 exit_minibuffer(void)
433 if (in_minibuffer == MB_COMPREAD) {
434 erase_buffer(&ministate.compl.buffer);
435 ui_schedule_redraw();
438 in_minibuffer = 0;
439 base_map = &global_map;
440 current_map = &global_map;
443 void
444 yornp(const char *prompt, void (*fn)(int, struct tab*),
445 struct tab *data)
447 size_t len;
449 if (in_minibuffer) {
450 fn(0, data);
451 return;
454 yornp_cb = fn;
455 yornp_data = data;
456 enter_minibuffer(yornp_self_insert, yornp_self_insert,
457 yornp_abort, NULL, NULL, NULL);
459 len = sizeof(ministate.prompt);
460 strlcpy(ministate.prompt, prompt, len);
461 strlcat(ministate.prompt, " (y or n) ", len);
464 void
465 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
466 struct tab *data)
468 size_t len;
470 if (in_minibuffer)
471 return;
473 read_cb = fn;
474 read_data = data;
475 enter_minibuffer(read_self_insert, read_select, read_abort,
476 &read_history, NULL, NULL);
478 len = sizeof(ministate.prompt);
479 strlcpy(ministate.prompt, prompt, len);
480 strlcat(ministate.prompt, ": ", len);
483 static void
484 handle_clear_echoarea(int fd, short ev, void *d)
486 free(ministate.curmesg);
487 ministate.curmesg = NULL;
489 ui_after_message_hook();
492 void
493 vmessage(const char *fmt, va_list ap)
495 if (evtimer_pending(&clechoev, NULL))
496 evtimer_del(&clechoev);
498 free(ministate.curmesg);
499 ministate.curmesg = NULL;
501 if (fmt != NULL) {
502 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
503 evtimer_add(&clechoev, &clechoev_timer);
505 /* TODO: what to do if the allocation fails here? */
506 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
507 ministate.curmesg = NULL;
510 ui_after_message_hook();
513 void
514 message(const char *fmt, ...)
516 va_list ap;
518 va_start(ap, fmt);
519 vmessage(fmt, ap);
520 va_end(ap);
523 void
524 minibuffer_init(void)
526 TAILQ_INIT(&eecmd_history.head);
527 TAILQ_INIT(&ir_history.head);
528 TAILQ_INIT(&lu_history.head);
529 TAILQ_INIT(&read_history.head);
531 TAILQ_INIT(&ministate.compl.buffer.head);
532 TAILQ_INIT(&ministate.compl.buffer.page.head);
534 ministate.line.type = LINE_TEXT;
535 ministate.vline.parent = &ministate.line;
536 ministate.buffer.page.name = "*minibuffer*";
537 ministate.buffer.current_line = &ministate.vline;
539 evtimer_set(&clechoev, handle_clear_echoarea, NULL);