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 static void *
98 minibuffer_metadata(void)
99 {
100 struct vline *vl;
102 vl = ministate.compl.buffer.current_line;
104 if (vl == NULL || vl->parent->flags & L_HIDDEN)
105 return NULL;
107 return vl->parent->data;
110 static void
111 minibuffer_hist_save_entry(void)
113 struct hist *hist;
115 if (ministate.history == NULL)
116 return;
118 if ((hist = calloc(1, sizeof(*hist))) == NULL)
119 abort();
121 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
123 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
124 ministate.history->len++;
127 /*
128 * taint the minibuffer cache: if we're currently showing a history
129 * element, copy that to the current buf and reset the "history
130 * navigation" thing.
131 */
132 void
133 minibuffer_taint_hist(void)
135 if (ministate.hist_cur == NULL)
136 return;
138 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
139 ministate.hist_cur = NULL;
140 ministate.buffer.current_line->line = ministate.buf;
143 void
144 minibuffer_self_insert(void)
146 char *c, tmp[5] = {0};
147 size_t len;
149 minibuffer_taint_hist();
151 if (thiskey.cp == 0)
152 return;
154 len = utf8_encode(thiskey.cp, tmp);
155 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
156 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
157 return;
159 memmove(c + len, c, strlen(c)+1);
160 memcpy(c, tmp, len);
161 ministate.buffer.cpoff++;
163 recompute_completions(1);
166 void
167 sensible_self_insert(void)
169 if (thiskey.meta ||
170 (!unicode_isgraph(thiskey.key) && thiskey.key != ' ')) {
171 global_key_unbound();
172 return;
175 minibuffer_self_insert();
178 void
179 eecmd_select(void)
181 struct cmd *cmd;
183 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
184 if (!strcmp(cmd->cmd, ministate.buf)) {
185 exit_minibuffer();
186 minibuffer_hist_save_entry();
187 cmd->fn(current_buffer());
188 return;
192 message("No match");
195 void
196 ir_select_gemini(void)
198 char buf[1025] = {0};
199 struct phos_uri uri;
200 struct tab *tab = current_tab;
202 exit_minibuffer();
203 minibuffer_hist_save_entry();
205 /* a bit ugly but... */
206 memcpy(&uri, &tab->uri, sizeof(tab->uri));
207 phos_uri_set_query(&uri, ministate.buf);
208 phos_serialize_uri(&uri, buf, sizeof(buf));
209 load_url_in_tab(tab, buf, NULL, 0);
212 void
213 ir_select_gopher(void)
215 exit_minibuffer();
216 minibuffer_hist_save_entry();
218 gopher_send_search_req(current_tab, ministate.buf);
221 void
222 lu_select(void)
224 char url[GEMINI_URL_LEN+1];
226 exit_minibuffer();
227 minibuffer_hist_save_entry();
229 humanify_url(ministate.buf, url, sizeof(url));
230 load_url_in_tab(current_tab, url, NULL, 0);
233 void
234 bp_select(void)
236 exit_minibuffer();
237 if (*ministate.buf != '\0')
238 add_to_bookmarks(ministate.buf);
239 else
240 message("Abort.");
243 void
244 ts_select(void)
246 struct tab *tab;
248 if ((tab = minibuffer_metadata()) == NULL) {
249 message("No tab selected");
250 return;
253 exit_minibuffer();
254 switch_to_tab(tab);
257 void
258 ls_select(void)
260 struct line *l;
262 if ((l = minibuffer_metadata()) == NULL) {
263 message("No link selected");
264 return;
267 exit_minibuffer();
268 load_url_in_tab(current_tab, l->alt, NULL, 0);
271 static inline void
272 jump_to_line(struct line *l)
274 struct vline *vl;
275 struct buffer *buffer;
277 buffer = current_buffer();
279 TAILQ_FOREACH(vl, &buffer->head, vlines) {
280 if (vl->parent == l)
281 break;
284 if (vl == NULL)
285 message("Ops, %s error! Please report to %s",
286 __func__, PACKAGE_BUGREPORT);
287 else {
288 buffer->top_line = vl;
289 buffer->current_line = vl;
293 void
294 swiper_select(void)
296 struct line *l;
298 if ((l = minibuffer_metadata()) == NULL) {
299 message("No line selected");
300 return;
303 exit_minibuffer();
304 jump_to_line(l);
307 void
308 toc_select(void)
310 struct line *l;
312 if ((l = minibuffer_metadata()) == NULL) {
313 message("No line selected");
314 return;
317 exit_minibuffer();
318 jump_to_line(l);
321 static void
322 yornp_self_insert(void)
324 if (thiskey.key != 'y' && thiskey.key != 'n') {
325 message("Please answer y or n");
326 return;
329 exit_minibuffer();
330 yornp_cb(thiskey.key == 'y', yornp_data);
333 static void
334 yornp_abort(void)
336 exit_minibuffer();
337 yornp_cb(0, yornp_data);
340 static void
341 read_self_insert(void)
343 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
344 global_key_unbound();
345 return;
348 minibuffer_self_insert();
351 static void
352 read_abort(void)
354 exit_minibuffer();
355 read_cb(NULL, read_data);
358 static void
359 read_select(void)
361 exit_minibuffer();
362 minibuffer_hist_save_entry();
363 read_cb(ministate.buf, read_data);
366 /*
367 * TODO: we should collect this asynchronously...
368 */
369 static inline void
370 populate_compl_buffer(complfn *fn, void *data)
372 const char *s, *descr;
373 struct line *l;
374 struct buffer *b;
375 struct parser *p;
376 void *linedata;
378 b = &ministate.compl.buffer;
379 p = &b->page;
381 linedata = NULL;
382 descr = NULL;
383 while ((s = fn(&data, &linedata, &descr)) != NULL) {
384 if ((l = calloc(1, sizeof(*l))) == NULL)
385 abort();
387 l->type = LINE_COMPL;
388 l->data = linedata;
389 l->alt = (char*)descr;
390 if ((l->line = strdup(s)) == NULL)
391 abort();
393 TAILQ_INSERT_TAIL(&p->head, l, lines);
395 linedata = NULL;
396 descr = NULL;
399 if ((l = TAILQ_FIRST(&p->head)) != NULL)
400 l->type = LINE_COMPL_CURRENT;
403 void
404 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
405 void (*abortfn)(void), struct histhead *hist,
406 complfn *complfn, void *compldata)
408 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
409 if (in_minibuffer == MB_COMPREAD) {
410 ui_schedule_redraw();
412 ministate.compl.fn = complfn;
413 ministate.compl.data = compldata;
414 populate_compl_buffer(complfn, compldata);
417 base_map = &minibuffer_map;
418 current_map = &minibuffer_map;
420 base_map->unhandled_input = self_insert_fn;
422 ministate.donefn = donefn;
423 ministate.abortfn = abortfn;
424 memset(ministate.buf, 0, sizeof(ministate.buf));
425 ministate.buffer.current_line = &ministate.vline;
426 ministate.buffer.current_line->line = ministate.buf;
427 ministate.buffer.cpoff = 0;
428 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
430 ministate.history = hist;
431 ministate.hist_cur = NULL;
432 ministate.hist_off = 0;
435 void
436 exit_minibuffer(void)
438 if (in_minibuffer == MB_COMPREAD) {
439 erase_buffer(&ministate.compl.buffer);
440 ui_schedule_redraw();
443 in_minibuffer = 0;
444 base_map = &global_map;
445 current_map = &global_map;
448 void
449 yornp(const char *prompt, void (*fn)(int, struct tab*),
450 struct tab *data)
452 size_t len;
454 if (in_minibuffer) {
455 fn(0, data);
456 return;
459 yornp_cb = fn;
460 yornp_data = data;
461 enter_minibuffer(yornp_self_insert, yornp_self_insert,
462 yornp_abort, NULL, NULL, NULL);
464 len = sizeof(ministate.prompt);
465 strlcpy(ministate.prompt, prompt, len);
466 strlcat(ministate.prompt, " (y or n) ", len);
469 void
470 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
471 struct tab *data)
473 size_t len;
475 if (in_minibuffer)
476 return;
478 read_cb = fn;
479 read_data = data;
480 enter_minibuffer(read_self_insert, read_select, read_abort,
481 &read_history, NULL, NULL);
483 len = sizeof(ministate.prompt);
484 strlcpy(ministate.prompt, prompt, len);
485 strlcat(ministate.prompt, ": ", len);
488 static void
489 handle_clear_echoarea(int fd, short ev, void *d)
491 free(ministate.curmesg);
492 ministate.curmesg = NULL;
494 ui_after_message_hook();
497 void
498 vmessage(const char *fmt, va_list ap)
500 if (evtimer_pending(&clechoev, NULL))
501 evtimer_del(&clechoev);
503 free(ministate.curmesg);
504 ministate.curmesg = NULL;
506 if (fmt != NULL) {
507 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
508 evtimer_add(&clechoev, &clechoev_timer);
510 /* TODO: what to do if the allocation fails here? */
511 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
512 ministate.curmesg = NULL;
515 ui_after_message_hook();
518 void
519 message(const char *fmt, ...)
521 va_list ap;
523 va_start(ap, fmt);
524 vmessage(fmt, ap);
525 va_end(ap);
528 void
529 minibuffer_init(void)
531 TAILQ_INIT(&eecmd_history.head);
532 TAILQ_INIT(&ir_history.head);
533 TAILQ_INIT(&lu_history.head);
534 TAILQ_INIT(&read_history.head);
536 TAILQ_INIT(&ministate.compl.buffer.head);
537 TAILQ_INIT(&ministate.compl.buffer.page.head);
539 ministate.line.type = LINE_TEXT;
540 ministate.vline.parent = &ministate.line;
541 ministate.buffer.page.name = "*minibuffer*";
542 ministate.buffer.current_line = &ministate.vline;
544 evtimer_set(&clechoev, handle_clear_echoarea, NULL);