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 exit_minibuffer();
225 minibuffer_hist_save_entry();
226 load_url_in_tab(current_tab, ministate.buf, NULL, 0);
229 void
230 bp_select(void)
232 exit_minibuffer();
233 if (*ministate.buf != '\0')
234 add_to_bookmarks(ministate.buf);
235 else
236 message("Abort.");
239 void
240 ts_select(void)
242 struct tab *tab;
244 if ((tab = minibuffer_metadata()) == NULL) {
245 message("No tab selected");
246 return;
249 exit_minibuffer();
250 switch_to_tab(tab);
253 void
254 ls_select(void)
256 struct line *l;
258 if ((l = minibuffer_metadata()) == NULL) {
259 message("No link selected");
260 return;
263 exit_minibuffer();
264 load_url_in_tab(current_tab, l->alt, NULL, 0);
267 static inline void
268 jump_to_line(struct line *l)
270 struct vline *vl;
271 struct buffer *buffer;
273 buffer = current_buffer();
275 TAILQ_FOREACH(vl, &buffer->head, vlines) {
276 if (vl->parent == l)
277 break;
280 if (vl == NULL)
281 message("Ops, %s error! Please report to %s",
282 __func__, PACKAGE_BUGREPORT);
283 else {
284 buffer->top_line = vl;
285 buffer->current_line = vl;
289 void
290 swiper_select(void)
292 struct line *l;
294 if ((l = minibuffer_metadata()) == NULL) {
295 message("No line selected");
296 return;
299 exit_minibuffer();
300 jump_to_line(l);
303 void
304 toc_select(void)
306 struct line *l;
308 if ((l = minibuffer_metadata()) == NULL) {
309 message("No line selected");
310 return;
313 exit_minibuffer();
314 jump_to_line(l);
317 static void
318 yornp_self_insert(void)
320 if (thiskey.key != 'y' && thiskey.key != 'n') {
321 message("Please answer y or n");
322 return;
325 exit_minibuffer();
326 yornp_cb(thiskey.key == 'y', yornp_data);
329 static void
330 yornp_abort(void)
332 exit_minibuffer();
333 yornp_cb(0, yornp_data);
336 static void
337 read_self_insert(void)
339 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
340 global_key_unbound();
341 return;
344 minibuffer_self_insert();
347 static void
348 read_abort(void)
350 exit_minibuffer();
351 read_cb(NULL, read_data);
354 static void
355 read_select(void)
357 exit_minibuffer();
358 minibuffer_hist_save_entry();
359 read_cb(ministate.buf, read_data);
362 /*
363 * TODO: we should collect this asynchronously...
364 */
365 static inline void
366 populate_compl_buffer(complfn *fn, void *data)
368 const char *s, *descr;
369 struct line *l;
370 struct buffer *b;
371 struct parser *p;
372 void *linedata;
374 b = &ministate.compl.buffer;
375 p = &b->page;
377 linedata = NULL;
378 descr = NULL;
379 while ((s = fn(&data, &linedata, &descr)) != NULL) {
380 if ((l = calloc(1, sizeof(*l))) == NULL)
381 abort();
383 l->type = LINE_COMPL;
384 l->data = linedata;
385 l->alt = (char*)descr;
386 if ((l->line = strdup(s)) == NULL)
387 abort();
389 TAILQ_INSERT_TAIL(&p->head, l, lines);
391 linedata = NULL;
392 descr = NULL;
395 if ((l = TAILQ_FIRST(&p->head)) != NULL)
396 l->type = LINE_COMPL_CURRENT;
399 void
400 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
401 void (*abortfn)(void), struct histhead *hist,
402 complfn *complfn, void *compldata)
404 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
405 if (in_minibuffer == MB_COMPREAD) {
406 ui_schedule_redraw();
408 ministate.compl.fn = complfn;
409 ministate.compl.data = compldata;
410 populate_compl_buffer(complfn, compldata);
413 base_map = &minibuffer_map;
414 current_map = &minibuffer_map;
416 base_map->unhandled_input = self_insert_fn;
418 ministate.donefn = donefn;
419 ministate.abortfn = abortfn;
420 memset(ministate.buf, 0, sizeof(ministate.buf));
421 ministate.buffer.current_line = &ministate.vline;
422 ministate.buffer.current_line->line = ministate.buf;
423 ministate.buffer.cpoff = 0;
424 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
426 ministate.history = hist;
427 ministate.hist_cur = NULL;
428 ministate.hist_off = 0;
431 void
432 exit_minibuffer(void)
434 if (in_minibuffer == MB_COMPREAD) {
435 erase_buffer(&ministate.compl.buffer);
436 ui_schedule_redraw();
439 in_minibuffer = 0;
440 base_map = &global_map;
441 current_map = &global_map;
444 void
445 yornp(const char *prompt, void (*fn)(int, struct tab*),
446 struct tab *data)
448 size_t len;
450 if (in_minibuffer) {
451 fn(0, data);
452 return;
455 yornp_cb = fn;
456 yornp_data = data;
457 enter_minibuffer(yornp_self_insert, yornp_self_insert,
458 yornp_abort, NULL, NULL, NULL);
460 len = sizeof(ministate.prompt);
461 strlcpy(ministate.prompt, prompt, len);
462 strlcat(ministate.prompt, " (y or n) ", len);
465 void
466 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
467 struct tab *data)
469 size_t len;
471 if (in_minibuffer)
472 return;
474 read_cb = fn;
475 read_data = data;
476 enter_minibuffer(read_self_insert, read_select, read_abort,
477 &read_history, NULL, NULL);
479 len = sizeof(ministate.prompt);
480 strlcpy(ministate.prompt, prompt, len);
481 strlcat(ministate.prompt, ": ", len);
484 static void
485 handle_clear_echoarea(int fd, short ev, void *d)
487 free(ministate.curmesg);
488 ministate.curmesg = NULL;
490 ui_after_message_hook();
493 void
494 vmessage(const char *fmt, va_list ap)
496 if (evtimer_pending(&clechoev, NULL))
497 evtimer_del(&clechoev);
499 free(ministate.curmesg);
500 ministate.curmesg = NULL;
502 if (fmt != NULL) {
503 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
504 evtimer_add(&clechoev, &clechoev_timer);
506 /* TODO: what to do if the allocation fails here? */
507 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
508 ministate.curmesg = NULL;
511 ui_after_message_hook();
514 void
515 message(const char *fmt, ...)
517 va_list ap;
519 va_start(ap, fmt);
520 vmessage(fmt, ap);
521 va_end(ap);
524 void
525 minibuffer_init(void)
527 TAILQ_INIT(&eecmd_history.head);
528 TAILQ_INIT(&ir_history.head);
529 TAILQ_INIT(&lu_history.head);
530 TAILQ_INIT(&read_history.head);
532 TAILQ_INIT(&ministate.compl.buffer.head);
533 TAILQ_INIT(&ministate.compl.buffer.page.head);
535 ministate.line.type = LINE_TEXT;
536 ministate.vline.parent = &ministate.line;
537 ministate.buffer.page.name = "*minibuffer*";
538 ministate.buffer.current_line = &ministate.vline;
540 evtimer_set(&clechoev, handle_clear_echoarea, NULL);