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 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void empty_vlist(struct tab*);
57 static void restore_cursor(struct tab *);
59 static void cmd_previous_line(struct tab*);
60 static void cmd_next_line(struct tab*);
61 static void cmd_forward_char(struct tab*);
62 static void cmd_backward_char(struct tab*);
63 static void cmd_move_beginning_of_line(struct tab*);
64 static void cmd_move_end_of_line(struct tab*);
65 static void cmd_redraw(struct tab*);
66 static void cmd_scroll_line_down(struct tab*);
67 static void cmd_scroll_line_up(struct tab*);
68 static void cmd_scroll_up(struct tab*);
69 static void cmd_scroll_down(struct tab*);
70 static void cmd_beginning_of_buffer(struct tab*);
71 static void cmd_end_of_buffer(struct tab*);
72 static void cmd_kill_telescope(struct tab*);
73 static void cmd_push_button(struct tab*);
74 static void cmd_push_button_new_tab(struct tab*);
75 static void cmd_previous_page(struct tab*);
76 static void cmd_next_page(struct tab*);
77 static void cmd_clear_minibuf(struct tab*);
78 static void cmd_execute_extended_command(struct tab*);
79 static void cmd_tab_close(struct tab*);
80 static void cmd_tab_close_other(struct tab*);
81 static void cmd_tab_new(struct tab*);
82 static void cmd_tab_next(struct tab*);
83 static void cmd_tab_previous(struct tab*);
84 static void cmd_load_url(struct tab*);
85 static void cmd_load_current_url(struct tab*);
86 static void cmd_bookmark_page(struct tab*);
87 static void cmd_goto_bookmarks(struct tab*);
89 static void global_key_unbound(void);
91 static void cmd_mini_delete_char(struct tab*);
92 static void cmd_mini_delete_backward_char(struct tab*);
93 static void cmd_mini_forward_char(struct tab*);
94 static void cmd_mini_backward_char(struct tab*);
95 static void cmd_mini_move_end_of_line(struct tab*);
96 static void cmd_mini_move_beginning_of_line(struct tab*);
97 static void cmd_mini_kill_line(struct tab*);
98 static void cmd_mini_abort(struct tab*);
99 static void cmd_mini_complete_and_exit(struct tab*);
100 static void cmd_mini_previous_history_element(struct tab*);
101 static void cmd_mini_next_history_element(struct tab*);
103 static void minibuffer_hist_save_entry(void);
104 static void minibuffer_taint_hist(void);
105 static void minibuffer_self_insert(void);
106 static void eecmd_self_insert(void);
107 static void eecmd_select(void);
108 static void ir_self_insert(void);
109 static void ir_select(void);
110 static void lu_self_insert(void);
111 static void lu_select(void);
112 static void bp_select(void);
114 static struct vline *nth_line(struct tab*, size_t);
115 static struct tab *current_tab(void);
116 static void dispatch_stdio(int, short, void*);
117 static void handle_clear_minibuf(int, short, void*);
118 static void handle_resize(int, short, void*);
119 static int wrap_page(struct tab*);
120 static void print_vline(struct vline*);
121 static void redraw_tabline(void);
122 static void redraw_body(struct tab*);
123 static void redraw_modeline(struct tab*);
124 static void redraw_minibuffer(void);
125 static void redraw_tab(struct tab*);
126 static void vmessage(const char*, va_list);
127 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
128 static void start_loading_anim(struct tab*);
129 static void update_loading_anim(int, short, void*);
130 static void stop_loading_anim(struct tab*);
131 static void load_url_in_tab(struct tab*, const char*);
132 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
133 static void exit_minibuffer(void);
134 static void switch_to_tab(struct tab*);
135 static struct tab *new_tab(const char*);
137 static struct { int meta, key; } thiskey;
139 static WINDOW *tabline, *body, *modeline, *minibuf;
140 static int body_lines, body_cols;
142 static struct event clminibufev;
143 static struct timeval clminibufev_timer = { 5, 0 };
144 static struct timeval loadingev_timer = { 0, 250000 };
146 static uint32_t tab_counter;
148 static char keybuf[64];
150 struct kmap global_map,
151 minibuffer_map,
152 *current_map,
153 *base_map;
155 static struct histhead eecmd_history,
156 ir_history,
157 lu_history;
159 static int in_minibuffer;
161 static struct {
162 char *curmesg;
164 char buf[1025];
165 size_t off, len;
166 char prompt[32];
167 void (*donefn)(void);
168 void (*abortfn)(void);
170 struct histhead *history;
171 struct hist *hist_cur;
172 size_t hist_off;
173 } ministate;
175 struct lineprefix {
176 const char *prfx1;
177 const char *prfx2;
178 } line_prefixes[] = {
179 [LINE_TEXT] = { "", "" },
180 [LINE_LINK] = { "=> ", " " },
181 [LINE_TITLE_1] = { "# ", " " },
182 [LINE_TITLE_2] = { "## ", " " },
183 [LINE_TITLE_3] = { "### ", " " },
184 [LINE_ITEM] = { "* ", " " },
185 [LINE_QUOTE] = { "> ", "> " },
186 [LINE_PRE_START] = { "```", "```" },
187 [LINE_PRE_CONTENT] = { "", "" },
188 [LINE_PRE_END] = { "```", "```" },
189 };
191 static struct line_face {
192 int prop;
193 } line_faces[] = {
194 [LINE_TEXT] = { 0 },
195 [LINE_LINK] = { A_UNDERLINE },
196 [LINE_TITLE_1] = { A_BOLD },
197 [LINE_TITLE_2] = { A_BOLD },
198 [LINE_TITLE_3] = { A_BOLD },
199 [LINE_ITEM] = { 0 },
200 [LINE_QUOTE] = { A_DIM },
201 [LINE_PRE_START] = { 0 },
202 [LINE_PRE_CONTENT] = { 0 },
203 [LINE_PRE_END] = { 0 },
204 };
206 static struct tab_face {
207 int background, tab, current_tab;
208 } tab_face = {
209 A_REVERSE, A_REVERSE, A_NORMAL
210 };
212 static void
213 empty_vlist(struct tab *tab)
215 struct vline *vl, *t;
217 tab->s.line_max = 0;
219 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
220 TAILQ_REMOVE(&tab->s.head, vl, vlines);
221 free(vl->line);
222 free(vl);
226 static inline void
227 global_set_key(const char *key, void (*fn)(struct tab*))
229 if (!kmap_define_key(&global_map, key, fn))
230 _exit(1);
233 static inline void
234 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
236 if (!kmap_define_key(&minibuffer_map, key, fn))
237 _exit(1);
240 static void
241 load_default_keys(void)
243 /* === global map === */
245 /* emacs */
246 global_set_key("C-p", cmd_previous_line);
247 global_set_key("C-n", cmd_next_line);
248 global_set_key("C-f", cmd_forward_char);
249 global_set_key("C-b", cmd_backward_char);
250 global_set_key("C-a", cmd_move_beginning_of_line);
251 global_set_key("C-e", cmd_move_end_of_line);
253 global_set_key("M-v", cmd_scroll_up);
254 global_set_key("C-v", cmd_scroll_down);
255 global_set_key("M-space", cmd_scroll_up);
256 global_set_key("space", cmd_scroll_down);
258 global_set_key("C-x C-c", cmd_kill_telescope);
260 global_set_key("C-g", cmd_clear_minibuf);
262 global_set_key("M-x", cmd_execute_extended_command);
263 global_set_key("C-x C-f", cmd_load_url);
264 global_set_key("C-x M-f", cmd_load_current_url);
266 global_set_key("C-x t 0", cmd_tab_close);
267 global_set_key("C-x t 1", cmd_tab_close_other);
268 global_set_key("C-x t 2", cmd_tab_new);
269 global_set_key("C-x t o", cmd_tab_next);
270 global_set_key("C-x t O", cmd_tab_previous);
272 global_set_key("M-<", cmd_beginning_of_buffer);
273 global_set_key("M->", cmd_end_of_buffer);
275 global_set_key("C-M-b", cmd_previous_page);
276 global_set_key("C-M-f", cmd_next_page);
278 global_set_key("<f7> a", cmd_bookmark_page);
279 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
281 /* vi/vi-like */
282 global_set_key("k", cmd_previous_line);
283 global_set_key("j", cmd_next_line);
284 global_set_key("l", cmd_forward_char);
285 global_set_key("h", cmd_backward_char);
286 global_set_key("^", cmd_move_beginning_of_line);
287 global_set_key("$", cmd_move_end_of_line);
289 global_set_key("K", cmd_scroll_line_up);
290 global_set_key("J", cmd_scroll_line_down);
292 global_set_key("g D", cmd_tab_close);
293 global_set_key("g N", cmd_tab_new);
294 global_set_key("g t", cmd_tab_next);
295 global_set_key("g T", cmd_tab_previous);
297 global_set_key("g g", cmd_beginning_of_buffer);
298 global_set_key("G", cmd_end_of_buffer);
300 global_set_key("H", cmd_previous_page);
301 global_set_key("L", cmd_next_page);
303 /* tmp */
304 global_set_key("q", cmd_kill_telescope);
306 global_set_key("esc", cmd_clear_minibuf);
308 global_set_key(":", cmd_execute_extended_command);
310 /* cua */
311 global_set_key("<up>", cmd_previous_line);
312 global_set_key("<down>", cmd_next_line);
313 global_set_key("<right>", cmd_forward_char);
314 global_set_key("<left>", cmd_backward_char);
315 global_set_key("<prior>", cmd_scroll_up);
316 global_set_key("<next>", cmd_scroll_down);
318 global_set_key("M-<left>", cmd_previous_page);
319 global_set_key("M-<right>", cmd_next_page);
321 /* "ncurses standard" */
322 global_set_key("C-l", cmd_redraw);
324 /* global */
325 global_set_key("C-m", cmd_push_button);
326 global_set_key("M-enter", cmd_push_button_new_tab);
328 /* === minibuffer map === */
329 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
330 minibuffer_set_key("C-g", cmd_mini_abort);
331 minibuffer_set_key("esc", cmd_mini_abort);
332 minibuffer_set_key("C-d", cmd_mini_delete_char);
333 minibuffer_set_key("del", cmd_mini_delete_backward_char);
335 minibuffer_set_key("C-f", cmd_mini_forward_char);
336 minibuffer_set_key("C-b", cmd_mini_backward_char);
337 minibuffer_set_key("<right>", cmd_mini_forward_char);
338 minibuffer_set_key("<left>", cmd_mini_backward_char);
339 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
340 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
341 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
342 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
343 minibuffer_set_key("C-k", cmd_mini_kill_line);
345 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
346 minibuffer_set_key("M-n", cmd_mini_next_history_element);
347 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
348 minibuffer_set_key("<down>", cmd_mini_next_history_element);
351 static void
352 restore_cursor(struct tab *tab)
354 wmove(body, tab->s.curs_y, tab->s.curs_x);
357 static void
358 cmd_previous_line(struct tab *tab)
360 if (--tab->s.curs_y < 0) {
361 tab->s.curs_y = 0;
362 cmd_scroll_line_up(tab);
365 restore_cursor(tab);
368 static void
369 cmd_next_line(struct tab *tab)
371 if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
372 return;
374 if (++tab->s.curs_y > body_lines-1) {
375 tab->s.curs_y = body_lines-1;
376 cmd_scroll_line_down(tab);
379 restore_cursor(tab);
382 static void
383 cmd_forward_char(struct tab *tab)
385 tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
386 restore_cursor(tab);
389 static void
390 cmd_backward_char(struct tab *tab)
392 tab->s.curs_x = MAX(0, tab->s.curs_x-1);
393 restore_cursor(tab);
396 static void
397 cmd_move_beginning_of_line(struct tab *tab)
399 tab->s.curs_x = 0;
400 restore_cursor(tab);
403 static void
404 cmd_move_end_of_line(struct tab *tab)
406 struct vline *vl;
407 size_t off;
408 const char *prfx;
410 off = tab->s.line_off + tab->s.curs_y;
411 if (off >= tab->s.line_max) {
412 tab->s.curs_x = 0;
413 goto end;
416 vl = nth_line(tab, off);
417 if (vl->line != NULL)
418 tab->s.curs_x = strlen(vl->line);
419 else
420 tab->s.curs_x = 0;
422 prfx = line_prefixes[vl->parent->type].prfx1;
423 tab->s.curs_x += strlen(prfx);
425 end:
426 restore_cursor(tab);
429 static void
430 cmd_redraw(struct tab *tab)
432 handle_resize(0, 0, NULL);
435 static void
436 cmd_scroll_line_up(struct tab *tab)
438 struct vline *vl;
440 if (tab->s.line_off == 0)
441 return;
443 vl = nth_line(tab, --tab->s.line_off);
444 wscrl(body, -1);
445 wmove(body, 0, 0);
446 print_vline(vl);
449 static void
450 cmd_scroll_line_down(struct tab *tab)
452 struct vline *vl;
454 if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
455 return;
457 tab->s.line_off++;
458 wscrl(body, 1);
460 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
461 return;
463 vl = nth_line(tab, tab->s.line_off + body_lines-1);
464 wmove(body, body_lines-1, 0);
465 print_vline(vl);
468 static void
469 cmd_scroll_up(struct tab *tab)
471 size_t off;
473 off = body_lines+1;
475 for (; off > 0; --off)
476 cmd_scroll_line_up(tab);
479 static void
480 cmd_scroll_down(struct tab *tab)
482 size_t off;
484 off = body_lines+1;
486 for (; off > 0; --off)
487 cmd_scroll_line_down(tab);
490 static void
491 cmd_beginning_of_buffer(struct tab *tab)
493 tab->s.line_off = 0;
494 tab->s.curs_y = 0;
495 redraw_body(tab);
498 static void
499 cmd_end_of_buffer(struct tab *tab)
501 ssize_t off;
503 off = tab->s.line_max - body_lines;
504 off = MAX(0, off);
506 tab->s.line_off = off;
507 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
509 redraw_body(tab);
512 static void
513 cmd_kill_telescope(struct tab *tab)
515 event_loopbreak();
518 static void
519 cmd_push_button(struct tab *tab)
521 struct vline *vl;
522 size_t nth;
524 nth = tab->s.line_off + tab->s.curs_y;
525 if (nth >= tab->s.line_max)
526 return;
527 vl = nth_line(tab, nth);
528 if (vl->parent->type != LINE_LINK)
529 return;
531 load_url_in_tab(tab, vl->parent->alt);
534 static void
535 cmd_push_button_new_tab(struct tab *tab)
537 struct vline *vl;
538 size_t nth;
540 nth = tab->s.line_off + tab->s.curs_y;
541 if (nth > tab->s.line_max)
542 return;
543 vl = nth_line(tab, nth);
544 if (vl->parent->type != LINE_LINK)
545 return;
547 new_tab(vl->parent->alt);
550 static void
551 cmd_previous_page(struct tab *tab)
553 if (!load_previous_page(tab))
554 message("No previous page");
555 else
556 start_loading_anim(tab);
559 static void
560 cmd_next_page(struct tab *tab)
562 if (!load_next_page(tab))
563 message("No next page");
564 else
565 start_loading_anim(tab);
568 static void
569 cmd_clear_minibuf(struct tab *tab)
571 handle_clear_minibuf(0, 0, NULL);
574 static void
575 cmd_execute_extended_command(struct tab *tab)
577 size_t len;
579 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
580 &eecmd_history);
582 len = sizeof(ministate.prompt);
583 strlcpy(ministate.prompt, "", len);
585 if (thiskey.meta)
586 strlcat(ministate.prompt, "M-", len);
588 strlcat(ministate.prompt, keyname(thiskey.key), len);
589 strlcat(ministate.prompt, " ", len);
592 static void
593 cmd_tab_close(struct tab *tab)
595 struct tab *t;
597 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
598 TAILQ_NEXT(tab, tabs) == NULL) {
599 message("Can't close the only tab.");
600 return;
603 stop_tab(tab);
605 t = TAILQ_PREV(tab, tabshead, tabs);
606 t->flags |= TAB_CURRENT;
608 TAILQ_REMOVE(&tabshead, tab, tabs);
610 free(tab);
613 static void
614 cmd_tab_close_other(struct tab *tab)
616 struct tab *t, *i;
618 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
619 if (t->flags & TAB_CURRENT)
620 continue;
622 stop_tab(t);
623 TAILQ_REMOVE(&tabshead, t, tabs);
624 free(t);
628 static void
629 cmd_tab_new(struct tab *tab)
631 new_tab(NEW_TAB_URL);
634 static void
635 cmd_tab_next(struct tab *tab)
637 struct tab *t;
639 tab->flags &= ~TAB_CURRENT;
641 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
642 t = TAILQ_FIRST(&tabshead);
643 t->flags |= TAB_CURRENT;
646 static void
647 cmd_tab_previous(struct tab *tab)
649 struct tab *t;
651 tab->flags &= ~TAB_CURRENT;
653 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
654 t = TAILQ_LAST(&tabshead, tabshead);
655 t->flags |= TAB_CURRENT;
658 static void
659 cmd_load_url(struct tab *tab)
661 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
662 &lu_history);
663 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
666 static void
667 cmd_load_current_url(struct tab *tab)
669 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
670 &lu_history);
671 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
672 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
673 ministate.off = strlen(tab->hist_cur->h);
674 ministate.len = ministate.off;
677 static void
678 cmd_bookmark_page(struct tab *tab)
680 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
681 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
682 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
683 ministate.off = strlen(tab->hist_cur->h);
684 ministate.len = ministate.off;
687 static void
688 cmd_goto_bookmarks(struct tab *tab)
690 load_url_in_tab(tab, "about:bookmarks");
693 static void
694 global_key_unbound(void)
696 message("%s is undefined", keybuf);
699 static void
700 cmd_mini_delete_char(struct tab *tab)
702 minibuffer_taint_hist();
704 if (ministate.len == 0 || ministate.off == ministate.len)
705 return;
707 memmove(&ministate.buf[ministate.off],
708 &ministate.buf[ministate.off+1],
709 ministate.len - ministate.off + 1);
710 ministate.len--;
713 static void
714 cmd_mini_delete_backward_char(struct tab *tab)
716 minibuffer_taint_hist();
718 if (ministate.len == 0 || ministate.off == 0)
719 return;
721 memmove(&ministate.buf[ministate.off-1],
722 &ministate.buf[ministate.off],
723 ministate.len - ministate.off + 1);
724 ministate.off--;
725 ministate.len--;
728 static void
729 cmd_mini_forward_char(struct tab *tab)
731 if (ministate.off == ministate.len)
732 return;
733 ministate.off++;
736 static void
737 cmd_mini_backward_char(struct tab *tab)
739 if (ministate.off == 0)
740 return;
741 ministate.off--;
744 static void
745 cmd_mini_move_end_of_line(struct tab *tab)
747 ministate.off = ministate.len;
750 static void
751 cmd_mini_move_beginning_of_line(struct tab *tab)
753 ministate.off = 0;
756 static void
757 cmd_mini_kill_line(struct tab *tab)
759 minibuffer_taint_hist();
761 if (ministate.off == ministate.len)
762 return;
763 ministate.buf[ministate.off] = '\0';
764 ministate.len -= ministate.off;
767 static void
768 cmd_mini_abort(struct tab *tab)
770 ministate.abortfn();
773 static void
774 cmd_mini_complete_and_exit(struct tab *tab)
776 minibuffer_taint_hist();
777 ministate.donefn();
780 static void
781 cmd_mini_previous_history_element(struct tab *tab)
783 if (ministate.history == NULL) {
784 message("No history");
785 return;
788 if (ministate.hist_cur == NULL ||
789 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
790 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
791 ministate.hist_off = ministate.history->len - 1;
792 if (ministate.hist_cur == NULL)
793 message("No prev item");
794 } else {
795 ministate.hist_off--;
798 if (ministate.hist_cur != NULL) {
799 ministate.off = 0;
800 ministate.len = strlen(ministate.hist_cur->h);
804 static void
805 cmd_mini_next_history_element(struct tab *tab)
807 if (ministate.history == NULL) {
808 message("No history");
809 return;
812 if (ministate.hist_cur == NULL ||
813 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
814 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
815 ministate.hist_off = 0;
816 if (ministate.hist_cur == NULL)
817 message("No next item");
818 } else {
819 ministate.hist_off++;
822 if (ministate.hist_cur != NULL) {
823 ministate.off = 0;
824 ministate.len = strlen(ministate.hist_cur->h);
828 static void
829 minibuffer_hist_save_entry(void)
831 struct hist *hist;
833 if (ministate.history == NULL)
834 return;
836 if ((hist = calloc(1, sizeof(*hist))) == NULL)
837 abort();
839 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
841 if (TAILQ_EMPTY(&ministate.history->head))
842 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
843 else
844 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
845 ministate.history->len++;
848 /*
849 * taint the minibuffer cache: if we're currently showing a history
850 * element, copy that to the current buf and reset the "history
851 * navigation" thing.
852 */
853 static void
854 minibuffer_taint_hist(void)
856 if (ministate.hist_cur == NULL)
857 return;
859 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
860 ministate.hist_cur = NULL;
863 static void
864 minibuffer_self_insert(void)
866 minibuffer_taint_hist();
868 if (ministate.len == sizeof(ministate.buf) -1)
869 return;
871 /* TODO: utf8 handling! */
873 memmove(&ministate.buf[ministate.off+1],
874 &ministate.buf[ministate.off],
875 ministate.len - ministate.off + 1);
876 ministate.buf[ministate.off] = thiskey.key;
877 ministate.off++;
878 ministate.len++;
881 static void
882 eecmd_self_insert(void)
884 if (thiskey.meta || isspace(thiskey.key) ||
885 !isgraph(thiskey.key)) {
886 global_key_unbound();
887 return;
890 minibuffer_self_insert();
893 static void
894 eecmd_select(void)
896 exit_minibuffer();
897 minibuffer_hist_save_entry();
898 message("TODO: try to execute %s", ministate.buf);
901 static void
902 ir_self_insert(void)
904 minibuffer_self_insert();
907 static void
908 ir_select(void)
910 char buf[1025] = {0};
911 struct url url;
912 struct tab *tab;
914 tab = current_tab();
916 exit_minibuffer();
917 minibuffer_hist_save_entry();
919 /* a bit ugly but... */
920 memcpy(&url, &tab->url, sizeof(tab->url));
921 url_set_query(&url, ministate.buf);
922 url_unparse(&url, buf, sizeof(buf));
923 load_url_in_tab(tab, buf);
926 static void
927 lu_self_insert(void)
929 if (thiskey.meta || isspace(thiskey.key) ||
930 !isgraph(thiskey.key)) {
931 global_key_unbound();
932 return;
935 minibuffer_self_insert();
938 static void
939 lu_select(void)
941 exit_minibuffer();
942 minibuffer_hist_save_entry();
943 load_url_in_tab(current_tab(), ministate.buf);
946 static void
947 bp_select(void)
949 exit_minibuffer();
950 if (*ministate.buf != '\0')
951 add_to_bookmarks(ministate.buf);
952 else
953 message("Abort.");
956 static struct vline *
957 nth_line(struct tab *tab, size_t n)
959 struct vline *vl;
960 size_t i;
962 i = 0;
963 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
964 if (i == n)
965 return vl;
966 i++;
969 /* unreachable */
970 abort();
973 static struct tab *
974 current_tab(void)
976 struct tab *t;
978 TAILQ_FOREACH(t, &tabshead, tabs) {
979 if (t->flags & TAB_CURRENT)
980 return t;
983 /* unreachable */
984 abort();
987 static void
988 dispatch_stdio(int fd, short ev, void *d)
990 struct keymap *k;
991 const char *keyname;
992 char tmp[2] = {0};
994 thiskey.key = wgetch(body);
995 if (thiskey.key == ERR)
996 return;
997 if (thiskey.key == 27) {
998 /* TODO: make escape-time customizable */
1000 thiskey.meta = 1;
1001 thiskey.key = wgetch(body);
1002 if (thiskey.key == ERR || thiskey.key == 27) {
1003 thiskey.meta = 0;
1004 thiskey.key = 27;
1006 } else
1007 thiskey.meta = 0;
1009 if (keybuf[0] != '\0')
1010 strlcat(keybuf, " ", sizeof(keybuf));
1011 if (thiskey.meta)
1012 strlcat(keybuf, "M-", sizeof(keybuf));
1013 if ((keyname = unkbd(thiskey.key)) != NULL)
1014 strlcat(keybuf, keyname, sizeof(keybuf));
1015 else {
1016 tmp[0] = thiskey.key;
1017 strlcat(keybuf, tmp, sizeof(keybuf));
1020 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1021 if (k->meta == thiskey.meta &&
1022 k->key == thiskey.key) {
1023 if (k->fn == NULL)
1024 current_map = &k->map;
1025 else {
1026 current_map = base_map;
1027 strlcpy(keybuf, "", sizeof(keybuf));
1028 k->fn(current_tab());
1030 goto done;
1034 if (current_map->unhandled_input != NULL)
1035 current_map->unhandled_input();
1036 else {
1037 global_key_unbound();
1040 strlcpy(keybuf, "", sizeof(keybuf));
1041 current_map = base_map;
1043 done:
1044 redraw_tab(current_tab());
1047 static void
1048 handle_clear_minibuf(int fd, short ev, void *d)
1050 free(ministate.curmesg);
1051 ministate.curmesg = NULL;
1053 redraw_minibuffer();
1054 if (in_minibuffer) {
1055 wrefresh(body);
1056 wrefresh(minibuf);
1057 } else {
1058 wrefresh(minibuf);
1059 wrefresh(body);
1063 static void
1064 handle_resize(int sig, short ev, void *d)
1066 struct tab *tab;
1068 endwin();
1069 refresh();
1070 clear();
1072 /* move and resize the windows, in reverse order! */
1074 mvwin(minibuf, LINES-1, 0);
1075 wresize(minibuf, 1, COLS);
1077 mvwin(modeline, LINES-2, 0);
1078 wresize(modeline, 1, COLS);
1080 wresize(body, LINES-3, COLS);
1081 body_lines = LINES-3;
1082 body_cols = COLS;
1084 wresize(tabline, 1, COLS);
1086 tab = current_tab();
1088 wrap_page(tab);
1089 redraw_tab(tab);
1092 static int
1093 wrap_page(struct tab *tab)
1095 struct line *l;
1096 const char *prfx;
1098 empty_vlist(tab);
1100 TAILQ_FOREACH(l, &tab->page.head, lines) {
1101 prfx = line_prefixes[l->type].prfx1;
1102 switch (l->type) {
1103 case LINE_TEXT:
1104 case LINE_LINK:
1105 case LINE_TITLE_1:
1106 case LINE_TITLE_2:
1107 case LINE_TITLE_3:
1108 case LINE_ITEM:
1109 case LINE_QUOTE:
1110 case LINE_PRE_START:
1111 case LINE_PRE_END:
1112 wrap_text(tab, prfx, l, body_cols);
1113 /* push_line(tab, l, NULL, 0, 0); */
1114 break;
1115 case LINE_PRE_CONTENT:
1116 hardwrap_text(tab, l, body_cols);
1117 break;
1120 return 1;
1123 static void
1124 print_vline(struct vline *vl)
1126 const char *text = vl->line;
1127 const char *prfx;
1128 int face = line_faces[vl->parent->type].prop;
1130 if (!vl->flags)
1131 prfx = line_prefixes[vl->parent->type].prfx1;
1132 else
1133 prfx = line_prefixes[vl->parent->type].prfx2;
1135 if (text == NULL)
1136 text = "";
1138 if (face != 0)
1139 wattron(body, face);
1140 wprintw(body, "%s%s", prfx, text);
1141 if (face != 0)
1142 wattroff(body, face);
1145 static void
1146 redraw_tabline(void)
1148 struct tab *tab;
1149 size_t toskip;
1150 int current, x, y, truncated;
1151 const char *title;
1152 char buf[25];
1154 toskip = 0;
1155 x = 1;
1156 TAILQ_FOREACH(tab, &tabshead, tabs) {
1157 x += sizeof(buf) + 1;
1158 toskip++;
1159 if (tab->flags & TAB_CURRENT)
1160 break;
1162 if (x < COLS-2)
1163 toskip = 0;
1164 else
1165 toskip--;
1167 werase(tabline);
1168 wattron(tabline, tab_face.background);
1169 wprintw(tabline, toskip == 0 ? " " : "<");
1170 wattroff(tabline, tab_face.background);
1172 truncated = 0;
1173 TAILQ_FOREACH(tab, &tabshead, tabs) {
1174 if (truncated)
1175 break;
1176 if (toskip != 0) {
1177 toskip--;
1178 continue;
1181 getyx(tabline, y, x);
1182 if (x + sizeof(buf)+2 >= (size_t)COLS)
1183 truncated = 1;
1185 current = tab->flags & TAB_CURRENT;
1187 if (*(title = tab->page.title) == '\0')
1188 title = tab->hist_cur->h;
1190 strlcpy(buf, " ", sizeof(buf));
1191 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1192 /* truncation happens */
1193 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1194 } else {
1195 /* pad with spaces */
1196 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1197 /* nop */ ;
1200 if (current)
1201 wattron(tabline, tab_face.current_tab);
1202 else
1203 wattron(tabline, tab_face.tab);
1205 wprintw(tabline, "%s", buf);
1206 if (TAILQ_NEXT(tab, tabs) != NULL)
1207 wprintw(tabline, " ");
1209 if (current)
1210 wattroff(tabline, tab_face.current_tab);
1211 else
1212 wattroff(tabline, tab_face.tab);
1215 wattron(tabline, tab_face.background);
1216 for (; x < COLS; ++x)
1217 waddch(tabline, ' ');
1218 if (truncated)
1219 mvwprintw(tabline, 0, COLS-1, ">");
1222 static void
1223 redraw_modeline(struct tab *tab)
1225 double pct;
1226 int x, y, max_x, max_y;
1227 const char *mode = tab->page.name;
1228 const char *spin = "-\\|/";
1230 werase(modeline);
1231 wattron(modeline, A_REVERSE);
1232 wmove(modeline, 0, 0);
1234 wprintw(modeline, "-%c %s ",
1235 spin[tab->s.loading_anim_step],
1236 mode == NULL ? "(none)" : mode);
1238 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1240 if (tab->s.line_max <= (size_t)body_lines)
1241 wprintw(modeline, "All ");
1242 else if (tab->s.line_off == 0)
1243 wprintw(modeline, "Top ");
1244 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1245 wprintw(modeline, "Bottom ");
1246 else
1247 wprintw(modeline, "%.0f%% ", pct);
1249 wprintw(modeline, "%d/%d %s ",
1250 tab->s.line_off + tab->s.curs_y,
1251 tab->s.line_max,
1252 tab->hist_cur->h);
1254 getyx(modeline, y, x);
1255 getmaxyx(modeline, max_y, max_x);
1257 (void)y;
1258 (void)max_y;
1260 for (; x < max_x; ++x)
1261 waddstr(modeline, "-");
1264 static void
1265 redraw_minibuffer(void)
1267 size_t skip = 0, off_x = 0, off_y = 0;
1269 werase(minibuf);
1270 if (in_minibuffer) {
1271 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1272 if (ministate.hist_cur != NULL)
1273 wprintw(minibuf, "(%zu/%zu) ",
1274 ministate.hist_off + 1,
1275 ministate.history->len);
1277 getyx(minibuf, off_y, off_x);
1279 while (ministate.off - skip > (size_t)COLS / 2) {
1280 skip += MIN(ministate.off/4, 1);
1283 if (ministate.hist_cur != NULL)
1284 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1285 else
1286 wprintw(minibuf, "%s", ministate.buf + skip);
1289 if (ministate.curmesg != NULL) {
1290 if (in_minibuffer)
1291 wprintw(minibuf, " [%s]", ministate.curmesg);
1292 else
1293 wprintw(minibuf, "%s", ministate.curmesg);
1296 if (!in_minibuffer && ministate.curmesg == NULL)
1297 wprintw(minibuf, "%s", keybuf);
1299 if (in_minibuffer)
1300 wmove(minibuf, 0, off_x + ministate.off - skip);
1303 static void
1304 redraw_tab(struct tab *tab)
1306 redraw_tabline();
1307 redraw_body(tab);
1308 redraw_modeline(tab);
1309 redraw_minibuffer();
1311 restore_cursor(tab);
1312 wrefresh(tabline);
1313 wrefresh(modeline);
1315 if (in_minibuffer) {
1316 wrefresh(body);
1317 wrefresh(minibuf);
1318 } else {
1319 wrefresh(minibuf);
1320 wrefresh(body);
1324 static void
1325 redraw_body(struct tab *tab)
1327 struct vline *vl;
1328 int line;
1330 werase(body);
1332 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1333 if (TAILQ_EMPTY(&tab->s.head))
1334 return;
1336 line = 0;
1337 vl = nth_line(tab, tab->s.line_off);
1338 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1339 wmove(body, line, 0);
1340 print_vline(vl);
1341 line++;
1342 if (line == body_lines)
1343 break;
1347 static void
1348 vmessage(const char *fmt, va_list ap)
1350 if (evtimer_pending(&clminibufev, NULL))
1351 evtimer_del(&clminibufev);
1352 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1353 evtimer_add(&clminibufev, &clminibufev_timer);
1355 free(ministate.curmesg);
1357 /* TODO: what to do if the allocation fails here? */
1358 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1359 ministate.curmesg = NULL;
1361 redraw_minibuffer();
1362 if (in_minibuffer) {
1363 wrefresh(body);
1364 wrefresh(minibuf);
1365 } else {
1366 wrefresh(minibuf);
1367 wrefresh(body);
1371 static void
1372 message(const char *fmt, ...)
1374 va_list ap;
1376 va_start(ap, fmt);
1377 vmessage(fmt, ap);
1378 va_end(ap);
1381 static void
1382 start_loading_anim(struct tab *tab)
1384 if (tab->s.loading_anim)
1385 return;
1386 tab->s.loading_anim = 1;
1387 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1388 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1391 static void
1392 update_loading_anim(int fd, short ev, void *d)
1394 struct tab *tab = d;
1396 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1398 if (tab->flags & TAB_CURRENT) {
1399 redraw_modeline(tab);
1400 wrefresh(modeline);
1401 wrefresh(body);
1402 if (in_minibuffer)
1403 wrefresh(minibuf);
1406 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1409 static void
1410 stop_loading_anim(struct tab *tab)
1412 if (!tab->s.loading_anim)
1413 return;
1414 evtimer_del(&tab->s.loadingev);
1415 tab->s.loading_anim = 0;
1416 tab->s.loading_anim_step = 0;
1418 redraw_modeline(tab);
1420 wrefresh(modeline);
1421 wrefresh(body);
1422 if (in_minibuffer)
1423 wrefresh(minibuf);
1426 static void
1427 load_url_in_tab(struct tab *tab, const char *url)
1429 empty_vlist(tab);
1430 message("Loading %s...", url);
1431 start_loading_anim(tab);
1432 load_url(tab, url);
1434 tab->s.curs_x = 0;
1435 tab->s.curs_y = 0;
1436 redraw_tab(tab);
1439 static void
1440 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1441 void (*abortfn)(void), struct histhead *hist)
1443 in_minibuffer = 1;
1444 base_map = &minibuffer_map;
1445 current_map = &minibuffer_map;
1447 base_map->unhandled_input = self_insert_fn;
1449 ministate.donefn = donefn;
1450 ministate.abortfn = abortfn;
1451 memset(ministate.buf, 0, sizeof(ministate.buf));
1452 ministate.off = 0;
1453 ministate.len = 0;
1454 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1456 ministate.history = hist;
1457 ministate.hist_cur = NULL;
1458 ministate.hist_off = 0;
1461 static void
1462 exit_minibuffer(void)
1464 werase(minibuf);
1466 in_minibuffer = 0;
1467 base_map = &global_map;
1468 current_map = &global_map;
1471 static void
1472 switch_to_tab(struct tab *tab)
1474 struct tab *t;
1476 TAILQ_FOREACH(t, &tabshead, tabs) {
1477 t->flags &= ~TAB_CURRENT;
1480 tab->flags |= TAB_CURRENT;
1483 static struct tab *
1484 new_tab(const char *url)
1486 struct tab *tab;
1488 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1489 goto err;
1491 TAILQ_INIT(&tab->hist.head);
1493 TAILQ_INIT(&tab->s.head);
1495 tab->id = tab_counter++;
1496 switch_to_tab(tab);
1498 if (TAILQ_EMPTY(&tabshead))
1499 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1500 else
1501 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1503 load_url_in_tab(tab, url);
1504 return tab;
1506 err:
1507 event_loopbreak();
1508 return NULL;
1511 int
1512 ui_init(void)
1514 setlocale(LC_ALL, "");
1516 TAILQ_INIT(&global_map.m);
1517 global_map.unhandled_input = global_key_unbound;
1519 TAILQ_INIT(&minibuffer_map.m);
1521 TAILQ_INIT(&eecmd_history.head);
1522 TAILQ_INIT(&ir_history.head);
1523 TAILQ_INIT(&lu_history.head);
1525 base_map = &global_map;
1526 current_map = &global_map;
1527 load_default_keys();
1529 initscr();
1530 raw();
1531 noecho();
1533 nonl();
1534 intrflush(stdscr, FALSE);
1536 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1537 return 0;
1538 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1539 return 0;
1540 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1541 return 0;
1542 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1543 return 0;
1545 body_lines = LINES-3;
1546 body_cols = COLS;
1548 keypad(body, TRUE);
1549 scrollok(body, TRUE);
1551 /* non-blocking input */
1552 wtimeout(body, 0);
1554 mvwprintw(body, 0, 0, "");
1556 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1557 event_add(&stdioev, NULL);
1559 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1560 signal_add(&winchev, NULL);
1562 new_tab(NEW_TAB_URL);
1564 return 1;
1567 void
1568 ui_on_tab_loaded(struct tab *tab)
1570 stop_loading_anim(tab);
1571 message("Loaded %s", tab->hist_cur->h);
1574 void
1575 ui_on_tab_refresh(struct tab *tab)
1577 if (!(tab->flags & TAB_CURRENT))
1578 return;
1580 wrap_page(tab);
1581 redraw_tab(tab);
1584 void
1585 ui_require_input(struct tab *tab, int hide)
1587 /* TODO: hard-switching to another tab is ugly */
1588 switch_to_tab(tab);
1590 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1591 &ir_history);
1592 strlcpy(ministate.prompt, "Input required: ",
1593 sizeof(ministate.prompt));
1594 redraw_tab(tab);
1597 void
1598 ui_notify(const char *fmt, ...)
1600 va_list ap;
1602 va_start(ap, fmt);
1603 vmessage(fmt, ap);
1604 va_end(ap);
1607 void
1608 ui_end(void)
1610 endwin();