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 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
26 * This way is easy to scroll: just call wscrl and then render the
27 * first/last line!
28 *
29 * This means that on every resize we have to clear our list of lines
30 * and re-render everything. A clever approach would be to do this
31 * ``on-demand''.
32 *
33 * TODO: make the text formatting on-demand.
34 *
35 */
37 #include <telescope.h>
39 #include <ctype.h>
40 #include <curses.h>
41 #include <event.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #define TAB_CURRENT 0x1
51 static struct event stdioev, winchev;
53 static void load_default_keys(void);
54 static void empty_vlist(struct tab*);
55 static void restore_cursor(struct tab *);
57 static void cmd_previous_line(struct tab*);
58 static void cmd_next_line(struct tab*);
59 static void cmd_forward_char(struct tab*);
60 static void cmd_backward_char(struct tab*);
61 static void cmd_move_beginning_of_line(struct tab*);
62 static void cmd_move_end_of_line(struct tab*);
63 static void cmd_redraw(struct tab*);
64 static void cmd_scroll_line_down(struct tab*);
65 static void cmd_scroll_line_up(struct tab*);
66 static void cmd_scroll_up(struct tab*);
67 static void cmd_scroll_down(struct tab*);
68 static void cmd_beginning_of_buffer(struct tab*);
69 static void cmd_end_of_buffer(struct tab*);
70 static void cmd_kill_telescope(struct tab*);
71 static void cmd_push_button(struct tab*);
72 static void cmd_push_button_new_tab(struct tab*);
73 static void cmd_previous_page(struct tab*);
74 static void cmd_next_page(struct tab*);
75 static void cmd_clear_minibuf(struct tab*);
76 static void cmd_execute_extended_command(struct tab*);
77 static void cmd_tab_close(struct tab*);
78 static void cmd_tab_new(struct tab*);
79 static void cmd_tab_next(struct tab*);
80 static void cmd_tab_previous(struct tab*);
81 static void cmd_load_url(struct tab*);
82 static void cmd_load_current_url(struct tab*);
83 static void cmd_bookmark_page(struct tab*);
85 static void global_key_unbound(void);
87 static void cmd_mini_delete_char(struct tab*);
88 static void cmd_mini_delete_backward_char(struct tab*);
89 static void cmd_mini_forward_char(struct tab*);
90 static void cmd_mini_backward_char(struct tab*);
91 static void cmd_mini_move_end_of_line(struct tab*);
92 static void cmd_mini_move_beginning_of_line(struct tab*);
93 static void cmd_mini_kill_line(struct tab*);
94 static void cmd_mini_abort(struct tab*);
95 static void cmd_mini_complete_and_exit(struct tab*);
96 static void cmd_mini_previous_history_element(struct tab*);
97 static void cmd_mini_next_history_element(struct tab*);
99 static void minibuffer_hist_save_entry(void);
100 static void minibuffer_taint_hist(void);
101 static void minibuffer_self_insert(void);
102 static void eecmd_self_insert(void);
103 static void eecmd_select(void);
104 static void ir_self_insert(void);
105 static void ir_select(void);
106 static void lu_self_insert(void);
107 static void lu_select(void);
108 static void bp_select(void);
110 static struct vline *nth_line(struct tab*, size_t);
111 static struct tab *current_tab(void);
112 static void dispatch_stdio(int, short, void*);
113 static void handle_clear_minibuf(int, short, void*);
114 static void handle_resize(int, short, void*);
115 static int wrap_page(struct tab*);
116 static void print_vline(struct vline*);
117 static void redraw_tabline(void);
118 static void redraw_body(struct tab*);
119 static void redraw_modeline(struct tab*);
120 static void redraw_minibuffer(void);
121 static void redraw_tab(struct tab*);
122 static void vmessage(const char*, va_list);
123 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
124 static void start_loading_anim(struct tab*);
125 static void update_loading_anim(int, short, void*);
126 static void stop_loading_anim(struct tab*);
127 static void load_url_in_tab(struct tab*, const char*);
128 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
129 static void exit_minibuffer(void);
130 static void switch_to_tab(struct tab*);
131 static struct tab *new_tab(void);
133 static struct { int meta, key; } thiskey;
135 static WINDOW *tabline, *body, *modeline, *minibuf;
136 static int body_lines, body_cols;
138 static struct event clminibufev;
139 static int clminibufev_set;
140 static struct timeval clminibufev_timer = { 5, 0 };
141 static struct timeval loadingev_timer = { 0, 250000 };
143 static uint32_t tab_counter;
145 static char keybuf[64];
147 struct kmap global_map,
148 minibuffer_map,
149 *current_map,
150 *base_map;
152 static struct histhead eecmd_history,
153 ir_history,
154 lu_history;
156 static int in_minibuffer;
158 static struct {
159 char *curmesg;
161 char buf[1025];
162 size_t off, len;
163 char prompt[32];
164 void (*donefn)(void);
165 void (*abortfn)(void);
167 struct histhead *history;
168 struct hist *hist_cur;
169 size_t hist_off;
170 } ministate;
172 struct lineprefix {
173 const char *prfx1;
174 const char *prfx2;
175 } line_prefixes[] = {
176 [LINE_TEXT] = { "", "" },
177 [LINE_LINK] = { "=> ", " " },
178 [LINE_TITLE_1] = { "# ", " " },
179 [LINE_TITLE_2] = { "## ", " " },
180 [LINE_TITLE_3] = { "### ", " " },
181 [LINE_ITEM] = { "* ", " " },
182 [LINE_QUOTE] = { "> ", "> " },
183 [LINE_PRE_START] = { "```", "```" },
184 [LINE_PRE_CONTENT] = { "", "" },
185 [LINE_PRE_END] = { "```", "```" },
186 };
188 struct line_face {
189 int prop;
190 } line_faces[] = {
191 [LINE_TEXT] = { 0 },
192 [LINE_LINK] = { A_UNDERLINE },
193 [LINE_TITLE_1] = { A_BOLD },
194 [LINE_TITLE_2] = { A_BOLD },
195 [LINE_TITLE_3] = { A_BOLD },
196 [LINE_ITEM] = { 0 },
197 [LINE_QUOTE] = { A_DIM },
198 [LINE_PRE_START] = { 0 },
199 [LINE_PRE_CONTENT] = { 0 },
200 [LINE_PRE_END] = { 0 },
201 };
203 static void
204 empty_vlist(struct tab *tab)
206 struct vline *vl, *t;
208 tab->s.line_max = 0;
210 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
211 TAILQ_REMOVE(&tab->s.head, vl, vlines);
212 free(vl->line);
213 free(vl);
217 static inline void
218 global_set_key(const char *key, void (*fn)(struct tab*))
220 if (!kmap_define_key(&global_map, key, fn))
221 _exit(1);
224 static inline void
225 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
227 if (!kmap_define_key(&minibuffer_map, key, fn))
228 _exit(1);
231 static void
232 load_default_keys(void)
234 /* === global map === */
236 /* emacs */
237 global_set_key("C-p", cmd_previous_line);
238 global_set_key("C-n", cmd_next_line);
239 global_set_key("C-f", cmd_forward_char);
240 global_set_key("C-b", cmd_backward_char);
241 global_set_key("C-a", cmd_move_beginning_of_line);
242 global_set_key("C-e", cmd_move_end_of_line);
244 global_set_key("M-v", cmd_scroll_up);
245 global_set_key("C-v", cmd_scroll_down);
246 global_set_key("M-space", cmd_scroll_up);
247 global_set_key("space", cmd_scroll_down);
249 global_set_key("C-x C-c", cmd_kill_telescope);
251 global_set_key("C-g", cmd_clear_minibuf);
253 global_set_key("M-x", cmd_execute_extended_command);
254 global_set_key("C-x C-f", cmd_load_url);
255 global_set_key("C-x M-f", cmd_load_current_url);
257 global_set_key("C-x t 0", cmd_tab_close);
258 global_set_key("C-x t 2", cmd_tab_new);
259 global_set_key("C-x t o", cmd_tab_next);
260 global_set_key("C-x t O", cmd_tab_previous);
262 global_set_key("M-<", cmd_beginning_of_buffer);
263 global_set_key("M->", cmd_end_of_buffer);
265 global_set_key("C-M-b", cmd_previous_page);
266 global_set_key("C-M-f", cmd_next_page);
268 global_set_key("<f7> a", cmd_bookmark_page);
270 /* vi/vi-like */
271 global_set_key("k", cmd_previous_line);
272 global_set_key("j", cmd_next_line);
273 global_set_key("l", cmd_forward_char);
274 global_set_key("h", cmd_backward_char);
275 global_set_key("^", cmd_move_beginning_of_line);
276 global_set_key("$", cmd_move_end_of_line);
278 global_set_key("K", cmd_scroll_line_up);
279 global_set_key("J", cmd_scroll_line_down);
281 global_set_key("g g", cmd_beginning_of_buffer);
282 global_set_key("G", cmd_end_of_buffer);
284 global_set_key("H", cmd_previous_page);
285 global_set_key("L", cmd_next_page);
287 /* tmp */
288 global_set_key("q", cmd_kill_telescope);
290 global_set_key("esc", cmd_clear_minibuf);
292 global_set_key(":", cmd_execute_extended_command);
294 /* cua */
295 global_set_key("<up>", cmd_previous_line);
296 global_set_key("<down>", cmd_next_line);
297 global_set_key("<right>", cmd_forward_char);
298 global_set_key("<left>", cmd_backward_char);
299 global_set_key("<prior>", cmd_scroll_up);
300 global_set_key("<next>", cmd_scroll_down);
302 global_set_key("M-<left>", cmd_previous_page);
303 global_set_key("M-<right>", cmd_next_page);
305 /* "ncurses standard" */
306 global_set_key("C-l", cmd_redraw);
308 /* global */
309 global_set_key("C-m", cmd_push_button);
310 global_set_key("M-enter", cmd_push_button_new_tab);
312 /* === minibuffer map === */
313 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
314 minibuffer_set_key("C-g", cmd_mini_abort);
315 minibuffer_set_key("esc", cmd_mini_abort);
316 minibuffer_set_key("C-d", cmd_mini_delete_char);
317 minibuffer_set_key("del", cmd_mini_delete_backward_char);
319 minibuffer_set_key("C-f", cmd_mini_forward_char);
320 minibuffer_set_key("C-b", cmd_mini_backward_char);
321 minibuffer_set_key("<right>", cmd_mini_forward_char);
322 minibuffer_set_key("<left>", cmd_mini_backward_char);
323 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
324 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
325 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
326 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
327 minibuffer_set_key("C-k", cmd_mini_kill_line);
329 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
330 minibuffer_set_key("M-n", cmd_mini_next_history_element);
331 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
332 minibuffer_set_key("<down>", cmd_mini_next_history_element);
335 static void
336 restore_cursor(struct tab *tab)
338 wmove(body, tab->s.curs_y, tab->s.curs_x);
341 static void
342 cmd_previous_line(struct tab *tab)
344 if (--tab->s.curs_y < 0) {
345 tab->s.curs_y = 0;
346 cmd_scroll_line_up(tab);
349 restore_cursor(tab);
352 static void
353 cmd_next_line(struct tab *tab)
355 if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
356 return;
358 if (++tab->s.curs_y > body_lines-1) {
359 tab->s.curs_y = body_lines-1;
360 cmd_scroll_line_down(tab);
363 restore_cursor(tab);
366 static void
367 cmd_forward_char(struct tab *tab)
369 tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
370 restore_cursor(tab);
373 static void
374 cmd_backward_char(struct tab *tab)
376 tab->s.curs_x = MAX(0, tab->s.curs_x-1);
377 restore_cursor(tab);
380 static void
381 cmd_move_beginning_of_line(struct tab *tab)
383 tab->s.curs_x = 0;
384 restore_cursor(tab);
387 static void
388 cmd_move_end_of_line(struct tab *tab)
390 struct vline *vl;
391 size_t off;
392 const char *prfx;
394 off = tab->s.line_off + tab->s.curs_y;
395 if (off >= tab->s.line_max) {
396 tab->s.curs_x = 0;
397 goto end;
400 vl = nth_line(tab, off);
401 if (vl->line != NULL)
402 tab->s.curs_x = strlen(vl->line);
403 else
404 tab->s.curs_x = 0;
406 prfx = line_prefixes[vl->parent->type].prfx1;
407 tab->s.curs_x += strlen(prfx);
409 end:
410 restore_cursor(tab);
413 static void
414 cmd_redraw(struct tab *tab)
416 handle_resize(0, 0, NULL);
419 static void
420 cmd_scroll_line_up(struct tab *tab)
422 struct vline *vl;
424 if (tab->s.line_off == 0)
425 return;
427 vl = nth_line(tab, --tab->s.line_off);
428 wscrl(body, -1);
429 wmove(body, 0, 0);
430 print_vline(vl);
433 static void
434 cmd_scroll_line_down(struct tab *tab)
436 struct vline *vl;
437 size_t n;
439 if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
440 return;
442 tab->s.line_off++;
443 wscrl(body, 1);
445 if (tab->s.line_max - tab->s.line_off < body_lines)
446 return;
448 vl = nth_line(tab, tab->s.line_off + body_lines-1);
449 wmove(body, body_lines-1, 0);
450 print_vline(vl);
453 static void
454 cmd_scroll_up(struct tab *tab)
456 size_t off;
458 off = body_lines+1;
460 for (; off > 0; --off)
461 cmd_scroll_line_up(tab);
464 static void
465 cmd_scroll_down(struct tab *tab)
467 size_t off;
469 off = body_lines+1;
471 for (; off > 0; --off)
472 cmd_scroll_line_down(tab);
475 static void
476 cmd_beginning_of_buffer(struct tab *tab)
478 tab->s.line_off = 0;
479 tab->s.curs_y = 0;
480 redraw_body(tab);
483 static void
484 cmd_end_of_buffer(struct tab *tab)
486 ssize_t off;
488 off = tab->s.line_max - body_lines;
489 off = MAX(0, off);
491 tab->s.line_off = off;
492 tab->s.curs_y = MIN(body_lines, tab->s.line_max);
494 redraw_body(tab);
497 static void
498 cmd_kill_telescope(struct tab *tab)
500 event_loopbreak();
503 static void
504 cmd_push_button(struct tab *tab)
506 struct vline *vl;
507 size_t nth;
509 nth = tab->s.line_off + tab->s.curs_y;
510 if (nth >= tab->s.line_max)
511 return;
512 vl = nth_line(tab, nth);
513 if (vl->parent->type != LINE_LINK)
514 return;
516 load_url_in_tab(tab, vl->parent->alt);
519 static void
520 cmd_push_button_new_tab(struct tab *tab)
522 struct tab *t;
523 struct vline *vl;
524 size_t nth;
526 nth = tab->s.line_off + tab->s.curs_y;
527 if (nth > tab->s.line_max)
528 return;
529 vl = nth_line(tab, nth);
530 if (vl->parent->type != LINE_LINK)
531 return;
533 t = new_tab();
534 memcpy(&t->url, &tab->url, sizeof(tab->url));
535 load_url_in_tab(t, vl->parent->alt);
538 static void
539 cmd_previous_page(struct tab *tab)
541 if (!load_previous_page(tab))
542 message("No previous page");
543 else
544 start_loading_anim(tab);
547 static void
548 cmd_next_page(struct tab *tab)
550 if (!load_next_page(tab))
551 message("No next page");
552 else
553 start_loading_anim(tab);
556 static void
557 cmd_clear_minibuf(struct tab *tab)
559 handle_clear_minibuf(0, 0, NULL);
562 static void
563 cmd_execute_extended_command(struct tab *tab)
565 size_t len;
567 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
568 &eecmd_history);
570 len = sizeof(ministate.prompt);
571 strlcpy(ministate.prompt, "", len);
573 if (thiskey.meta)
574 strlcat(ministate.prompt, "M-", len);
576 strlcat(ministate.prompt, keyname(thiskey.key), len);
577 strlcat(ministate.prompt, " ", len);
580 static void
581 cmd_tab_close(struct tab *tab)
583 struct tab *t;
585 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
586 TAILQ_NEXT(tab, tabs) == NULL) {
587 message("Can't close the only tab.");
588 return;
591 stop_tab(tab);
593 t = TAILQ_PREV(tab, tabshead, tabs);
594 t->flags |= TAB_CURRENT;
596 TAILQ_REMOVE(&tabshead, tab, tabs);
598 free(tab);
601 static void
602 cmd_tab_new(struct tab *tab)
604 new_tab();
607 static void
608 cmd_tab_next(struct tab *tab)
610 struct tab *t;
612 tab->flags &= ~TAB_CURRENT;
614 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
615 t = TAILQ_FIRST(&tabshead);
616 t->flags |= TAB_CURRENT;
619 static void
620 cmd_tab_previous(struct tab *tab)
622 struct tab *t;
624 tab->flags &= ~TAB_CURRENT;
626 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
627 t = TAILQ_LAST(&tabshead, tabshead);
628 t->flags |= TAB_CURRENT;
631 static void
632 cmd_load_url(struct tab *tab)
634 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
635 &lu_history);
636 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
639 static void
640 cmd_load_current_url(struct tab *tab)
642 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
643 &lu_history);
644 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
645 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
646 ministate.off = strlen(tab->hist_cur->h);
647 ministate.len = ministate.off;
650 static void
651 cmd_bookmark_page(struct tab *tab)
653 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
654 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
655 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
656 ministate.off = strlen(tab->hist_cur->h);
657 ministate.len = ministate.off;
660 static void
661 global_key_unbound(void)
663 message("%s is undefined", keybuf);
666 static void
667 cmd_mini_delete_char(struct tab *tab)
669 minibuffer_taint_hist();
671 if (ministate.len == 0 || ministate.off == ministate.len)
672 return;
674 memmove(&ministate.buf[ministate.off],
675 &ministate.buf[ministate.off+1],
676 ministate.len - ministate.off + 1);
677 ministate.len--;
680 static void
681 cmd_mini_delete_backward_char(struct tab *tab)
683 minibuffer_taint_hist();
685 if (ministate.len == 0 || ministate.off == 0)
686 return;
688 memmove(&ministate.buf[ministate.off-1],
689 &ministate.buf[ministate.off],
690 ministate.len - ministate.off + 1);
691 ministate.off--;
692 ministate.len--;
695 static void
696 cmd_mini_forward_char(struct tab *tab)
698 if (ministate.off == ministate.len)
699 return;
700 ministate.off++;
703 static void
704 cmd_mini_backward_char(struct tab *tab)
706 if (ministate.off == 0)
707 return;
708 ministate.off--;
711 static void
712 cmd_mini_move_end_of_line(struct tab *tab)
714 ministate.off = ministate.len;
717 static void
718 cmd_mini_move_beginning_of_line(struct tab *tab)
720 ministate.off = 0;
723 static void
724 cmd_mini_kill_line(struct tab *tab)
726 minibuffer_taint_hist();
728 if (ministate.off == ministate.len)
729 return;
730 ministate.buf[ministate.off] = '\0';
731 ministate.len -= ministate.off;
734 static void
735 cmd_mini_abort(struct tab *tab)
737 ministate.abortfn();
740 static void
741 cmd_mini_complete_and_exit(struct tab *tab)
743 minibuffer_taint_hist();
744 ministate.donefn();
747 static void
748 cmd_mini_previous_history_element(struct tab *tab)
750 if (ministate.history == NULL) {
751 message("No history");
752 return;
755 if (ministate.hist_cur == NULL ||
756 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
757 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
758 ministate.hist_off = ministate.history->len - 1;
759 if (ministate.hist_cur == NULL)
760 message("No prev item");
761 } else {
762 ministate.hist_off--;
765 if (ministate.hist_cur != NULL) {
766 ministate.off = 0;
767 ministate.len = strlen(ministate.hist_cur->h);
771 static void
772 cmd_mini_next_history_element(struct tab *tab)
774 if (ministate.history == NULL) {
775 message("No history");
776 return;
779 if (ministate.hist_cur == NULL ||
780 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
781 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
782 ministate.hist_off = 0;
783 if (ministate.hist_cur == NULL)
784 message("No next item");
785 } else {
786 ministate.hist_off++;
789 if (ministate.hist_cur != NULL) {
790 ministate.off = 0;
791 ministate.len = strlen(ministate.hist_cur->h);
795 static void
796 minibuffer_hist_save_entry(void)
798 struct hist *hist;
800 if (ministate.history == NULL)
801 return;
803 if ((hist = calloc(1, sizeof(*hist))) == NULL)
804 abort();
806 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
808 if (TAILQ_EMPTY(&ministate.history->head))
809 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
810 else
811 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
812 ministate.history->len++;
815 /*
816 * taint the minibuffer cache: if we're currently showing a history
817 * element, copy that to the current buf and reset the "history
818 * navigation" thing.
819 */
820 static void
821 minibuffer_taint_hist(void)
823 if (ministate.hist_cur == NULL)
824 return;
826 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
827 ministate.hist_cur = NULL;
830 static void
831 minibuffer_self_insert(void)
833 minibuffer_taint_hist();
835 if (ministate.len == sizeof(ministate.buf) -1)
836 return;
838 /* TODO: utf8 handling! */
840 memmove(&ministate.buf[ministate.off+1],
841 &ministate.buf[ministate.off],
842 ministate.len - ministate.off + 1);
843 ministate.buf[ministate.off] = thiskey.key;
844 ministate.off++;
845 ministate.len++;
848 static void
849 eecmd_self_insert(void)
851 if (thiskey.meta || isspace(thiskey.key) ||
852 !isgraph(thiskey.key)) {
853 global_key_unbound();
854 return;
857 minibuffer_self_insert();
860 static void
861 eecmd_select(void)
863 exit_minibuffer();
864 minibuffer_hist_save_entry();
865 message("TODO: try to execute %s", ministate.buf);
868 static void
869 ir_self_insert(void)
871 minibuffer_self_insert();
874 static void
875 ir_select(void)
877 char buf[1025] = {0};
878 struct url url;
879 struct tab *tab;
881 tab = current_tab();
883 exit_minibuffer();
884 minibuffer_hist_save_entry();
886 /* a bit ugly but... */
887 memcpy(&url, &tab->url, sizeof(tab->url));
888 url_set_query(&url, ministate.buf);
889 url_unparse(&url, buf, sizeof(buf));
890 load_url_in_tab(tab, buf);
893 static void
894 lu_self_insert(void)
896 if (thiskey.meta || isspace(thiskey.key) ||
897 !isgraph(thiskey.key)) {
898 global_key_unbound();
899 return;
902 minibuffer_self_insert();
905 static void
906 lu_select(void)
908 exit_minibuffer();
909 minibuffer_hist_save_entry();
910 load_url_in_tab(current_tab(), ministate.buf);
913 static void
914 bp_select(void)
916 exit_minibuffer();
917 if (*ministate.buf != '\0')
918 add_to_bookmarks(ministate.buf);
919 else
920 message("Abort.");
923 static struct vline *
924 nth_line(struct tab *tab, size_t n)
926 struct vline *vl;
927 size_t i;
929 i = 0;
930 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
931 if (i == n)
932 return vl;
933 i++;
936 /* unreachable */
937 abort();
940 static struct tab *
941 current_tab(void)
943 struct tab *t;
945 TAILQ_FOREACH(t, &tabshead, tabs) {
946 if (t->flags & TAB_CURRENT)
947 return t;
950 /* unreachable */
951 abort();
954 static void
955 dispatch_stdio(int fd, short ev, void *d)
957 struct tab *tab;
958 struct keymap *k;
959 const char *keyname;
960 char tmp[2] = {0};
962 thiskey.key = wgetch(body);
963 if (thiskey.key == ERR)
964 return;
965 if (thiskey.key == 27) {
966 /* TODO: make escape-time customizable */
968 thiskey.meta = 1;
969 thiskey.key = wgetch(body);
970 if (thiskey.key == ERR || thiskey.key == 27) {
971 thiskey.meta = 0;
972 thiskey.key = 27;
974 } else
975 thiskey.meta = 0;
977 if (keybuf[0] != '\0')
978 strlcat(keybuf, " ", sizeof(keybuf));
979 if (thiskey.meta)
980 strlcat(keybuf, "M-", sizeof(keybuf));
981 if ((keyname = unkbd(thiskey.key)) != NULL)
982 strlcat(keybuf, keyname, sizeof(keybuf));
983 else {
984 tmp[0] = thiskey.key;
985 strlcat(keybuf, tmp, sizeof(keybuf));
988 TAILQ_FOREACH(k, &current_map->m, keymaps) {
989 if (k->meta == thiskey.meta &&
990 k->key == thiskey.key) {
991 if (k->fn == NULL)
992 current_map = &k->map;
993 else {
994 current_map = base_map;
995 strlcpy(keybuf, "", sizeof(keybuf));
996 k->fn(current_tab());
998 goto done;
1002 if (current_map->unhandled_input != NULL)
1003 current_map->unhandled_input();
1004 else {
1005 global_key_unbound();
1008 strlcpy(keybuf, "", sizeof(keybuf));
1009 current_map = base_map;
1011 done:
1012 redraw_tab(current_tab());
1015 static void
1016 handle_clear_minibuf(int fd, short ev, void *d)
1018 clminibufev_set = 0;
1020 free(ministate.curmesg);
1021 ministate.curmesg = NULL;
1023 redraw_minibuffer();
1024 if (in_minibuffer) {
1025 wrefresh(body);
1026 wrefresh(minibuf);
1027 } else {
1028 wrefresh(minibuf);
1029 wrefresh(body);
1033 static void
1034 handle_resize(int sig, short ev, void *d)
1036 struct tab *tab;
1038 endwin();
1039 refresh();
1040 clear();
1042 /* move and resize the windows, in reverse order! */
1044 mvwin(minibuf, LINES-1, 0);
1045 wresize(minibuf, 1, COLS);
1047 mvwin(modeline, LINES-2, 0);
1048 wresize(modeline, 1, COLS);
1050 wresize(body, LINES-3, COLS);
1051 body_lines = LINES-3;
1052 body_cols = COLS;
1054 wresize(tabline, 1, COLS);
1056 tab = current_tab();
1058 wrap_page(tab);
1059 redraw_tab(tab);
1062 static int
1063 wrap_page(struct tab *tab)
1065 struct line *l;
1066 const char *prfx;
1068 empty_vlist(tab);
1070 TAILQ_FOREACH(l, &tab->page.head, lines) {
1071 prfx = line_prefixes[l->type].prfx1;
1072 switch (l->type) {
1073 case LINE_TEXT:
1074 case LINE_LINK:
1075 case LINE_TITLE_1:
1076 case LINE_TITLE_2:
1077 case LINE_TITLE_3:
1078 case LINE_ITEM:
1079 case LINE_QUOTE:
1080 case LINE_PRE_START:
1081 case LINE_PRE_END:
1082 wrap_text(tab, prfx, l, body_cols);
1083 /* push_line(tab, l, NULL, 0, 0); */
1084 break;
1085 case LINE_PRE_CONTENT:
1086 hardwrap_text(tab, l, body_cols);
1087 break;
1090 return 1;
1093 static inline void
1094 print_vline(struct vline *vl)
1096 const char *text = vl->line;
1097 const char *prfx;
1098 int face = line_faces[vl->parent->type].prop;
1100 if (!vl->flags)
1101 prfx = line_prefixes[vl->parent->type].prfx1;
1102 else
1103 prfx = line_prefixes[vl->parent->type].prfx2;
1105 if (text == NULL)
1106 text = "";
1108 if (face != 0)
1109 wattron(body, face);
1110 wprintw(body, "%s%s", prfx, text);
1111 if (face != 0)
1112 wattroff(body, face);
1115 static void
1116 redraw_tabline(void)
1118 struct tab *tab;
1119 int current;
1120 const char *title;
1122 werase(tabline);
1123 wbkgd(tabline, A_REVERSE);
1125 wprintw(tabline, " ");
1126 TAILQ_FOREACH(tab, &tabshead, tabs) {
1127 current = tab->flags & TAB_CURRENT;
1129 if (*(title = tab->page.title) == '\0')
1130 title = tab->hist_cur->h;
1132 if (current)
1133 wattron(tabline, A_UNDERLINE);
1135 wprintw(tabline, "%s%d: %s",
1136 current ? "*" : " ", tab->id, title);
1138 if (current)
1139 wattroff(tabline, A_UNDERLINE);
1143 static void
1144 redraw_modeline(struct tab *tab)
1146 double pct;
1147 int x, y, max_x, max_y;
1148 const char *mode = tab->page.name;
1149 const char *spin = "-\\|/";
1151 werase(modeline);
1152 wattron(modeline, A_REVERSE);
1153 wmove(modeline, 0, 0);
1155 wprintw(modeline, "-%c %s ",
1156 spin[tab->s.loading_anim_step], mode);
1158 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1160 if (tab->s.line_max <= body_lines)
1161 wprintw(modeline, "All ");
1162 else if (tab->s.line_off == 0)
1163 wprintw(modeline, "Top ");
1164 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1165 wprintw(modeline, "Bottom ");
1166 else
1167 wprintw(modeline, "%.0f%% ", pct);
1169 wprintw(modeline, "%d/%d %s ",
1170 tab->s.line_off + tab->s.curs_y,
1171 tab->s.line_max,
1172 tab->hist_cur->h);
1174 getyx(modeline, y, x);
1175 getmaxyx(modeline, max_y, max_x);
1177 (void)y;
1178 (void)max_y;
1180 for (; x < max_x; ++x)
1181 waddstr(modeline, "-");
1184 static void
1185 redraw_minibuffer(void)
1187 size_t skip = 0, off_x = 0, off_y = 0;
1189 werase(minibuf);
1190 if (in_minibuffer) {
1191 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1192 if (ministate.hist_cur != NULL)
1193 wprintw(minibuf, "(%zu/%zu) ",
1194 ministate.hist_off + 1,
1195 ministate.history->len);
1197 getyx(minibuf, off_y, off_x);
1199 while (ministate.off - skip > COLS / 2) {
1200 skip += MIN(ministate.off/4, 1);
1203 if (ministate.hist_cur != NULL)
1204 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1205 else
1206 wprintw(minibuf, "%s", ministate.buf + skip);
1209 if (ministate.curmesg != NULL) {
1210 if (in_minibuffer)
1211 wprintw(minibuf, " [%s]", ministate.curmesg);
1212 else
1213 wprintw(minibuf, "%s", ministate.curmesg);
1216 if (!in_minibuffer && ministate.curmesg == NULL)
1217 wprintw(minibuf, "%s", keybuf);
1219 if (in_minibuffer)
1220 wmove(minibuf, 0, off_x + ministate.off - skip);
1223 static void
1224 redraw_tab(struct tab *tab)
1226 redraw_tabline();
1227 redraw_body(tab);
1228 redraw_modeline(tab);
1229 redraw_minibuffer();
1231 restore_cursor(tab);
1232 wrefresh(tabline);
1233 wrefresh(modeline);
1235 if (in_minibuffer) {
1236 wrefresh(body);
1237 wrefresh(minibuf);
1238 } else {
1239 wrefresh(minibuf);
1240 wrefresh(body);
1244 static void
1245 redraw_body(struct tab *tab)
1247 struct vline *vl;
1248 int line;
1250 werase(body);
1252 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1253 if (TAILQ_EMPTY(&tab->s.head))
1254 return;
1256 line = 0;
1257 vl = nth_line(tab, tab->s.line_off);
1258 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1259 wmove(body, line, 0);
1260 print_vline(vl);
1261 line++;
1262 if (line == body_lines)
1263 break;
1267 static void
1268 vmessage(const char *fmt, va_list ap)
1270 if (clminibufev_set)
1271 evtimer_del(&clminibufev);
1272 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1273 evtimer_add(&clminibufev, &clminibufev_timer);
1274 clminibufev_set = 1;
1276 free(ministate.curmesg);
1278 /* TODO: what to do if the allocation fails here? */
1279 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1280 ministate.curmesg = NULL;
1282 redraw_minibuffer();
1283 if (in_minibuffer) {
1284 wrefresh(body);
1285 wrefresh(minibuf);
1286 } else {
1287 wrefresh(minibuf);
1288 wrefresh(body);
1292 static void
1293 message(const char *fmt, ...)
1295 va_list ap;
1297 va_start(ap, fmt);
1298 vmessage(fmt, ap);
1299 va_end(ap);
1302 static void
1303 start_loading_anim(struct tab *tab)
1305 if (tab->s.loading_anim)
1306 return;
1307 tab->s.loading_anim = 1;
1308 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1309 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1312 static void
1313 update_loading_anim(int fd, short ev, void *d)
1315 struct tab *tab = d;
1317 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1319 redraw_modeline(tab);
1320 wrefresh(modeline);
1322 wrefresh(body);
1323 if (in_minibuffer)
1324 wrefresh(minibuf);
1326 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1329 static void
1330 stop_loading_anim(struct tab *tab)
1332 if (!tab->s.loading_anim)
1333 return;
1334 evtimer_del(&tab->s.loadingev);
1335 tab->s.loading_anim = 0;
1336 tab->s.loading_anim_step = 0;
1338 redraw_modeline(tab);
1340 wrefresh(modeline);
1341 wrefresh(body);
1342 if (in_minibuffer)
1343 wrefresh(minibuf);
1346 static void
1347 load_url_in_tab(struct tab *tab, const char *url)
1349 empty_vlist(tab);
1350 message("Loading %s...", url);
1351 start_loading_anim(tab);
1352 load_url(tab, url);
1354 tab->s.curs_x = 0;
1355 tab->s.curs_y = 0;
1356 redraw_tab(tab);
1359 static void
1360 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1361 void (*abortfn)(void), struct histhead *hist)
1363 in_minibuffer = 1;
1364 base_map = &minibuffer_map;
1365 current_map = &minibuffer_map;
1367 base_map->unhandled_input = self_insert_fn;
1369 ministate.donefn = donefn;
1370 ministate.abortfn = abortfn;
1371 memset(ministate.buf, 0, sizeof(ministate.buf));
1372 ministate.off = 0;
1373 ministate.len = 0;
1374 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1376 ministate.history = hist;
1377 ministate.hist_cur = NULL;
1378 ministate.hist_off = 0;
1381 static void
1382 exit_minibuffer(void)
1384 werase(minibuf);
1386 in_minibuffer = 0;
1387 base_map = &global_map;
1388 current_map = &global_map;
1391 static void
1392 switch_to_tab(struct tab *tab)
1394 struct tab *t;
1396 TAILQ_FOREACH(t, &tabshead, tabs) {
1397 t->flags &= ~TAB_CURRENT;
1400 tab->flags |= TAB_CURRENT;
1403 static struct tab *
1404 new_tab(void)
1406 struct tab *tab, *t;
1407 const char *url = "about:new";
1409 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1410 goto err;
1412 TAILQ_INIT(&tab->hist.head);
1414 TAILQ_INIT(&tab->s.head);
1416 tab->id = tab_counter++;
1417 switch_to_tab(tab);
1419 if (TAILQ_EMPTY(&tabshead))
1420 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1421 else
1422 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1424 load_url_in_tab(tab, url);
1425 return tab;
1427 err:
1428 event_loopbreak();
1429 return NULL;
1432 int
1433 ui_init(void)
1435 setlocale(LC_ALL, "");
1437 TAILQ_INIT(&global_map.m);
1438 global_map.unhandled_input = global_key_unbound;
1440 TAILQ_INIT(&minibuffer_map.m);
1442 TAILQ_INIT(&eecmd_history.head);
1443 TAILQ_INIT(&ir_history.head);
1444 TAILQ_INIT(&lu_history.head);
1446 base_map = &global_map;
1447 current_map = &global_map;
1448 load_default_keys();
1450 initscr();
1451 raw();
1452 noecho();
1454 nonl();
1455 intrflush(stdscr, FALSE);
1457 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1458 return 0;
1459 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1460 return 0;
1461 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1462 return 0;
1463 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1464 return 0;
1466 body_lines = LINES-3;
1467 body_cols = COLS;
1469 keypad(body, TRUE);
1470 scrollok(body, TRUE);
1472 /* non-blocking input */
1473 wtimeout(body, 0);
1475 mvwprintw(body, 0, 0, "");
1477 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1478 event_add(&stdioev, NULL);
1480 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1481 signal_add(&winchev, NULL);
1483 new_tab();
1485 return 1;
1488 void
1489 ui_on_tab_loaded(struct tab *tab)
1491 stop_loading_anim(tab);
1492 message("Loaded %s", tab->hist_cur->h);
1495 void
1496 ui_on_tab_refresh(struct tab *tab)
1498 if (!(tab->flags & TAB_CURRENT))
1499 return;
1501 wrap_page(tab);
1502 redraw_tab(tab);
1505 void
1506 ui_require_input(struct tab *tab, int hide)
1508 /* TODO: hard-switching to another tab is ugly */
1509 switch_to_tab(tab);
1511 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1512 &ir_history);
1513 strlcpy(ministate.prompt, "Input required: ",
1514 sizeof(ministate.prompt));
1515 redraw_tab(tab);
1518 void
1519 ui_notify(const char *fmt, ...)
1521 va_list ap;
1523 va_start(ap, fmt);
1524 vmessage(fmt, ap);
1525 va_end(ap);
1528 void
1529 ui_end(void)
1531 endwin();