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 struct histhead eecmd_history,
46 ir_history,
47 lu_history,
48 read_history;
50 struct ministate ministate;
52 struct buffer minibufferwin;
54 int in_minibuffer;
56 /*
57 * Recompute the visible completions. If add is 1, don't consider the
58 * ones already hidden.
59 */
60 void
61 recompute_completions(int add)
62 {
63 struct line *l;
64 struct vline *vl;
65 struct buffer *b;
67 if (in_minibuffer != MB_COMPREAD)
68 return;
70 b = &ministate.compl.buffer;
71 TAILQ_FOREACH(l, &b->page.head, lines) {
72 l->type = LINE_COMPL;
73 if (add && l->flags & L_HIDDEN)
74 continue;
75 if (strcasestr(l->line, ministate.buf) != NULL ||
76 (l->alt != NULL && strcasestr(l->alt, ministate.buf) != NULL)) {
77 if (l->flags & L_HIDDEN)
78 b->line_max++;
79 l->flags &= ~L_HIDDEN;
80 } else {
81 if (!(l->flags & L_HIDDEN))
82 b->line_max--;
83 l->flags |= L_HIDDEN;
84 }
85 }
87 if (b->current_line == NULL)
88 b->current_line = TAILQ_FIRST(&b->head);
89 b->current_line = adjust_line(b->current_line, b);
90 vl = b->current_line;
91 if (vl != NULL)
92 vl->parent->type = LINE_COMPL_CURRENT;
93 }
95 static void *
96 minibuffer_metadata(void)
97 {
98 struct vline *vl;
100 vl = ministate.compl.buffer.current_line;
102 if (vl == NULL || vl->parent->flags & L_HIDDEN)
103 return NULL;
105 return vl->parent->data;
108 static void
109 minibuffer_hist_save_entry(void)
111 struct hist *hist;
113 if (ministate.history == NULL)
114 return;
116 if ((hist = calloc(1, sizeof(*hist))) == NULL)
117 abort();
119 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
121 if (TAILQ_EMPTY(&ministate.history->head))
122 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
123 else
124 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
125 ministate.history->len++;
128 /*
129 * taint the minibuffer cache: if we're currently showing a history
130 * element, copy that to the current buf and reset the "history
131 * navigation" thing.
132 */
133 void
134 minibuffer_taint_hist(void)
136 if (ministate.hist_cur == NULL)
137 return;
139 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
140 ministate.hist_cur = NULL;
141 ministate.buffer.current_line->line = ministate.buf;
144 void
145 minibuffer_self_insert(void)
147 char *c, tmp[5] = {0};
148 size_t len;
150 minibuffer_taint_hist();
152 if (thiskey.cp == 0)
153 return;
155 len = utf8_encode(thiskey.cp, tmp);
156 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
157 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
158 return;
160 memmove(c + len, c, strlen(c)+1);
161 memcpy(c, tmp, len);
162 ministate.buffer.cpoff++;
164 recompute_completions(1);
167 void
168 sensible_self_insert(void)
170 if (thiskey.meta ||
171 (unicode_isspace(thiskey.key) && thiskey.key != ' ')) {
172 global_key_unbound();
173 return;
176 minibuffer_self_insert();
179 void
180 eecmd_self_insert(void)
182 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
183 !unicode_isgraph(thiskey.cp)) {
184 global_key_unbound();
185 return;
188 minibuffer_self_insert();
191 void
192 eecmd_select(void)
194 struct cmd *cmd;
196 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
197 if (!strcmp(cmd->cmd, ministate.buf)) {
198 exit_minibuffer();
199 minibuffer_hist_save_entry();
200 cmd->fn(current_buffer());
201 return;
205 message("No match");
208 void
209 ir_self_insert(void)
211 minibuffer_self_insert();
214 void
215 ir_select(void)
217 char buf[1025] = {0};
218 struct phos_uri uri;
219 struct tab *tab = current_tab;
221 exit_minibuffer();
222 minibuffer_hist_save_entry();
224 /* a bit ugly but... */
225 memcpy(&uri, &tab->uri, sizeof(tab->uri));
226 phos_uri_set_query(&uri, ministate.buf);
227 phos_serialize_uri(&uri, buf, sizeof(buf));
228 load_url_in_tab(tab, buf, NULL);
231 void
232 lu_select(void)
234 exit_minibuffer();
235 minibuffer_hist_save_entry();
236 load_url_in_tab(current_tab, ministate.buf, NULL);
239 void
240 bp_select(void)
242 exit_minibuffer();
243 if (*ministate.buf != '\0')
244 add_to_bookmarks(ministate.buf);
245 else
246 message("Abort.");
249 void
250 ts_select(void)
252 struct tab *tab;
254 if ((tab = minibuffer_metadata()) == NULL) {
255 message("No tab selected");
256 return;
259 exit_minibuffer();
260 switch_to_tab(tab);
263 void
264 ls_select(void)
266 struct line *l;
268 if ((l = minibuffer_metadata()) == NULL) {
269 message("No link selected");
270 return;
273 exit_minibuffer();
274 load_url_in_tab(current_tab, l->alt, NULL);
277 static inline void
278 jump_to_line(struct line *l)
280 struct vline *vl;
281 struct tab *tab = current_tab;
283 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
284 if (vl->parent == l)
285 break;
288 if (vl == NULL)
289 message("Ops, %s error! Please report to %s",
290 __func__, PACKAGE_BUGREPORT);
291 else {
292 tab->buffer.top_line = vl;
293 tab->buffer.current_line = vl;
297 void
298 swiper_select(void)
300 struct line *l;
302 if ((l = minibuffer_metadata()) == NULL) {
303 message("No line selected");
304 return;
307 exit_minibuffer();
308 jump_to_line(l);
311 void
312 toc_select(void)
314 struct line *l;
316 if ((l = minibuffer_metadata()) == NULL) {
317 message("No line selected");
318 return;
321 exit_minibuffer();
322 jump_to_line(l);
325 static void
326 yornp_self_insert(void)
328 if (thiskey.key != 'y' && thiskey.key != 'n') {
329 message("Please answer y or n");
330 return;
333 exit_minibuffer();
334 yornp_cb(thiskey.key == 'y', yornp_data);
337 static void
338 yornp_abort(void)
340 exit_minibuffer();
341 yornp_cb(0, yornp_data);
344 static void
345 read_self_insert(void)
347 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
348 global_key_unbound();
349 return;
352 minibuffer_self_insert();
355 static void
356 read_abort(void)
358 exit_minibuffer();
359 read_cb(NULL, read_data);
362 static void
363 read_select(void)
365 exit_minibuffer();
366 minibuffer_hist_save_entry();
367 read_cb(ministate.buf, read_data);
370 /*
371 * TODO: we should collect this asynchronously...
372 */
373 static inline void
374 populate_compl_buffer(complfn *fn, void *data)
376 const char *s, *descr;
377 struct line *l;
378 struct buffer *b;
379 struct parser *p;
380 void *linedata;
382 b = &ministate.compl.buffer;
383 p = &b->page;
385 linedata = NULL;
386 descr = NULL;
387 while ((s = fn(&data, &linedata, &descr)) != NULL) {
388 if ((l = calloc(1, sizeof(*l))) == NULL)
389 abort();
391 l->type = LINE_COMPL;
392 l->data = linedata;
393 l->alt = (char*)descr;
394 if ((l->line = strdup(s)) == NULL)
395 abort();
397 if (TAILQ_EMPTY(&p->head))
398 TAILQ_INSERT_HEAD(&p->head, l, lines);
399 else
400 TAILQ_INSERT_TAIL(&p->head, l, lines);
402 linedata = NULL;
403 descr = NULL;
406 if ((l = TAILQ_FIRST(&p->head)) != NULL)
407 l->type = LINE_COMPL_CURRENT;
410 void
411 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
412 void (*abortfn)(void), struct histhead *hist,
413 complfn *complfn, void *compldata)
415 in_minibuffer = complfn == NULL ? MB_READ : MB_COMPREAD;
416 if (in_minibuffer == MB_COMPREAD) {
417 ui_schedule_redraw();
419 ministate.compl.fn = complfn;
420 ministate.compl.data = compldata;
421 populate_compl_buffer(complfn, compldata);
424 base_map = &minibuffer_map;
425 current_map = &minibuffer_map;
427 base_map->unhandled_input = self_insert_fn;
429 ministate.donefn = donefn;
430 ministate.abortfn = abortfn;
431 memset(ministate.buf, 0, sizeof(ministate.buf));
432 ministate.buffer.current_line = &ministate.vline;
433 ministate.buffer.current_line->line = ministate.buf;
434 ministate.buffer.cpoff = 0;
435 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
437 ministate.history = hist;
438 ministate.hist_cur = NULL;
439 ministate.hist_off = 0;
442 void
443 exit_minibuffer(void)
445 if (in_minibuffer == MB_COMPREAD) {
446 erase_buffer(&ministate.compl.buffer);
447 ui_schedule_redraw();
450 in_minibuffer = 0;
451 base_map = &global_map;
452 current_map = &global_map;
455 void
456 yornp(const char *prompt, void (*fn)(int, struct tab*),
457 struct tab *data)
459 size_t len;
461 if (in_minibuffer) {
462 fn(0, data);
463 return;
466 yornp_cb = fn;
467 yornp_data = data;
468 enter_minibuffer(yornp_self_insert, yornp_self_insert,
469 yornp_abort, NULL, NULL, NULL);
471 len = sizeof(ministate.prompt);
472 strlcpy(ministate.prompt, prompt, len);
473 strlcat(ministate.prompt, " (y or n) ", len);
476 void
477 minibuffer_read(const char *prompt, void (*fn)(const char *, struct tab *),
478 struct tab *data)
480 size_t len;
482 if (in_minibuffer)
483 return;
485 read_cb = fn;
486 read_data = data;
487 enter_minibuffer(read_self_insert, read_select, read_abort,
488 &read_history, NULL, NULL);
490 len = sizeof(ministate.prompt);
491 strlcpy(ministate.prompt, prompt, len);
492 strlcat(ministate.prompt, ": ", len);
495 static void
496 handle_clear_echoarea(int fd, short ev, void *d)
498 free(ministate.curmesg);
499 ministate.curmesg = NULL;
501 ui_after_message_hook();
504 void
505 vmessage(const char *fmt, va_list ap)
507 if (evtimer_pending(&clechoev, NULL))
508 evtimer_del(&clechoev);
510 free(ministate.curmesg);
511 ministate.curmesg = NULL;
513 if (fmt != NULL) {
514 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
515 evtimer_add(&clechoev, &clechoev_timer);
517 /* TODO: what to do if the allocation fails here? */
518 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
519 ministate.curmesg = NULL;
522 ui_after_message_hook();
525 void
526 message(const char *fmt, ...)
528 va_list ap;
530 va_start(ap, fmt);
531 vmessage(fmt, ap);
532 va_end(ap);
535 void
536 minibuffer_init(void)
538 TAILQ_INIT(&eecmd_history.head);
539 TAILQ_INIT(&ir_history.head);
540 TAILQ_INIT(&lu_history.head);
542 ministate.line.type = LINE_TEXT;
543 ministate.vline.parent = &ministate.line;
544 ministate.buffer.current_line = &ministate.vline;
546 evtimer_set(&clechoev, handle_clear_echoarea, NULL);