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 int clminibufev_set;
144 static struct timeval clminibufev_timer = { 5, 0 };
145 static struct timeval loadingev_timer = { 0, 250000 };
147 static uint32_t tab_counter;
149 static char keybuf[64];
151 struct kmap global_map,
152 minibuffer_map,
153 *current_map,
154 *base_map;
156 static struct histhead eecmd_history,
157 ir_history,
158 lu_history;
160 static int in_minibuffer;
162 static struct {
163 char *curmesg;
165 char buf[1025];
166 size_t off, len;
167 char prompt[32];
168 void (*donefn)(void);
169 void (*abortfn)(void);
171 struct histhead *history;
172 struct hist *hist_cur;
173 size_t hist_off;
174 } ministate;
176 struct lineprefix {
177 const char *prfx1;
178 const char *prfx2;
179 } line_prefixes[] = {
180 [LINE_TEXT] = { "", "" },
181 [LINE_LINK] = { "=> ", " " },
182 [LINE_TITLE_1] = { "# ", " " },
183 [LINE_TITLE_2] = { "## ", " " },
184 [LINE_TITLE_3] = { "### ", " " },
185 [LINE_ITEM] = { "* ", " " },
186 [LINE_QUOTE] = { "> ", "> " },
187 [LINE_PRE_START] = { "```", "```" },
188 [LINE_PRE_CONTENT] = { "", "" },
189 [LINE_PRE_END] = { "```", "```" },
190 };
192 struct line_face {
193 int prop;
194 } line_faces[] = {
195 [LINE_TEXT] = { 0 },
196 [LINE_LINK] = { A_UNDERLINE },
197 [LINE_TITLE_1] = { A_BOLD },
198 [LINE_TITLE_2] = { A_BOLD },
199 [LINE_TITLE_3] = { A_BOLD },
200 [LINE_ITEM] = { 0 },
201 [LINE_QUOTE] = { A_DIM },
202 [LINE_PRE_START] = { 0 },
203 [LINE_PRE_CONTENT] = { 0 },
204 [LINE_PRE_END] = { 0 },
205 };
207 static void
208 empty_vlist(struct tab *tab)
210 struct vline *vl, *t;
212 tab->s.line_max = 0;
214 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
215 TAILQ_REMOVE(&tab->s.head, vl, vlines);
216 free(vl->line);
217 free(vl);
221 static inline void
222 global_set_key(const char *key, void (*fn)(struct tab*))
224 if (!kmap_define_key(&global_map, key, fn))
225 _exit(1);
228 static inline void
229 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
231 if (!kmap_define_key(&minibuffer_map, key, fn))
232 _exit(1);
235 static void
236 load_default_keys(void)
238 /* === global map === */
240 /* emacs */
241 global_set_key("C-p", cmd_previous_line);
242 global_set_key("C-n", cmd_next_line);
243 global_set_key("C-f", cmd_forward_char);
244 global_set_key("C-b", cmd_backward_char);
245 global_set_key("C-a", cmd_move_beginning_of_line);
246 global_set_key("C-e", cmd_move_end_of_line);
248 global_set_key("M-v", cmd_scroll_up);
249 global_set_key("C-v", cmd_scroll_down);
250 global_set_key("M-space", cmd_scroll_up);
251 global_set_key("space", cmd_scroll_down);
253 global_set_key("C-x C-c", cmd_kill_telescope);
255 global_set_key("C-g", cmd_clear_minibuf);
257 global_set_key("M-x", cmd_execute_extended_command);
258 global_set_key("C-x C-f", cmd_load_url);
259 global_set_key("C-x M-f", cmd_load_current_url);
261 global_set_key("C-x t 0", cmd_tab_close);
262 global_set_key("C-x t 1", cmd_tab_close_other);
263 global_set_key("C-x t 2", cmd_tab_new);
264 global_set_key("C-x t o", cmd_tab_next);
265 global_set_key("C-x t O", cmd_tab_previous);
267 global_set_key("M-<", cmd_beginning_of_buffer);
268 global_set_key("M->", cmd_end_of_buffer);
270 global_set_key("C-M-b", cmd_previous_page);
271 global_set_key("C-M-f", cmd_next_page);
273 global_set_key("<f7> a", cmd_bookmark_page);
274 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
276 /* vi/vi-like */
277 global_set_key("k", cmd_previous_line);
278 global_set_key("j", cmd_next_line);
279 global_set_key("l", cmd_forward_char);
280 global_set_key("h", cmd_backward_char);
281 global_set_key("^", cmd_move_beginning_of_line);
282 global_set_key("$", cmd_move_end_of_line);
284 global_set_key("K", cmd_scroll_line_up);
285 global_set_key("J", cmd_scroll_line_down);
287 global_set_key("g D", cmd_tab_close);
288 global_set_key("g N", cmd_tab_new);
289 global_set_key("g t", cmd_tab_next);
290 global_set_key("g T", cmd_tab_previous);
292 global_set_key("g g", cmd_beginning_of_buffer);
293 global_set_key("G", cmd_end_of_buffer);
295 global_set_key("H", cmd_previous_page);
296 global_set_key("L", cmd_next_page);
298 /* tmp */
299 global_set_key("q", cmd_kill_telescope);
301 global_set_key("esc", cmd_clear_minibuf);
303 global_set_key(":", cmd_execute_extended_command);
305 /* cua */
306 global_set_key("<up>", cmd_previous_line);
307 global_set_key("<down>", cmd_next_line);
308 global_set_key("<right>", cmd_forward_char);
309 global_set_key("<left>", cmd_backward_char);
310 global_set_key("<prior>", cmd_scroll_up);
311 global_set_key("<next>", cmd_scroll_down);
313 global_set_key("M-<left>", cmd_previous_page);
314 global_set_key("M-<right>", cmd_next_page);
316 /* "ncurses standard" */
317 global_set_key("C-l", cmd_redraw);
319 /* global */
320 global_set_key("C-m", cmd_push_button);
321 global_set_key("M-enter", cmd_push_button_new_tab);
323 /* === minibuffer map === */
324 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
325 minibuffer_set_key("C-g", cmd_mini_abort);
326 minibuffer_set_key("esc", cmd_mini_abort);
327 minibuffer_set_key("C-d", cmd_mini_delete_char);
328 minibuffer_set_key("del", cmd_mini_delete_backward_char);
330 minibuffer_set_key("C-f", cmd_mini_forward_char);
331 minibuffer_set_key("C-b", cmd_mini_backward_char);
332 minibuffer_set_key("<right>", cmd_mini_forward_char);
333 minibuffer_set_key("<left>", cmd_mini_backward_char);
334 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
335 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
336 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
337 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
338 minibuffer_set_key("C-k", cmd_mini_kill_line);
340 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
341 minibuffer_set_key("M-n", cmd_mini_next_history_element);
342 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
343 minibuffer_set_key("<down>", cmd_mini_next_history_element);
346 static void
347 restore_cursor(struct tab *tab)
349 wmove(body, tab->s.curs_y, tab->s.curs_x);
352 static void
353 cmd_previous_line(struct tab *tab)
355 if (--tab->s.curs_y < 0) {
356 tab->s.curs_y = 0;
357 cmd_scroll_line_up(tab);
360 restore_cursor(tab);
363 static void
364 cmd_next_line(struct tab *tab)
366 if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
367 return;
369 if (++tab->s.curs_y > body_lines-1) {
370 tab->s.curs_y = body_lines-1;
371 cmd_scroll_line_down(tab);
374 restore_cursor(tab);
377 static void
378 cmd_forward_char(struct tab *tab)
380 tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
381 restore_cursor(tab);
384 static void
385 cmd_backward_char(struct tab *tab)
387 tab->s.curs_x = MAX(0, tab->s.curs_x-1);
388 restore_cursor(tab);
391 static void
392 cmd_move_beginning_of_line(struct tab *tab)
394 tab->s.curs_x = 0;
395 restore_cursor(tab);
398 static void
399 cmd_move_end_of_line(struct tab *tab)
401 struct vline *vl;
402 size_t off;
403 const char *prfx;
405 off = tab->s.line_off + tab->s.curs_y;
406 if (off >= tab->s.line_max) {
407 tab->s.curs_x = 0;
408 goto end;
411 vl = nth_line(tab, off);
412 if (vl->line != NULL)
413 tab->s.curs_x = strlen(vl->line);
414 else
415 tab->s.curs_x = 0;
417 prfx = line_prefixes[vl->parent->type].prfx1;
418 tab->s.curs_x += strlen(prfx);
420 end:
421 restore_cursor(tab);
424 static void
425 cmd_redraw(struct tab *tab)
427 handle_resize(0, 0, NULL);
430 static void
431 cmd_scroll_line_up(struct tab *tab)
433 struct vline *vl;
435 if (tab->s.line_off == 0)
436 return;
438 vl = nth_line(tab, --tab->s.line_off);
439 wscrl(body, -1);
440 wmove(body, 0, 0);
441 print_vline(vl);
444 static void
445 cmd_scroll_line_down(struct tab *tab)
447 struct vline *vl;
449 if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
450 return;
452 tab->s.line_off++;
453 wscrl(body, 1);
455 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
456 return;
458 vl = nth_line(tab, tab->s.line_off + body_lines-1);
459 wmove(body, body_lines-1, 0);
460 print_vline(vl);
463 static void
464 cmd_scroll_up(struct tab *tab)
466 size_t off;
468 off = body_lines+1;
470 for (; off > 0; --off)
471 cmd_scroll_line_up(tab);
474 static void
475 cmd_scroll_down(struct tab *tab)
477 size_t off;
479 off = body_lines+1;
481 for (; off > 0; --off)
482 cmd_scroll_line_down(tab);
485 static void
486 cmd_beginning_of_buffer(struct tab *tab)
488 tab->s.line_off = 0;
489 tab->s.curs_y = 0;
490 redraw_body(tab);
493 static void
494 cmd_end_of_buffer(struct tab *tab)
496 ssize_t off;
498 off = tab->s.line_max - body_lines;
499 off = MAX(0, off);
501 tab->s.line_off = off;
502 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
504 redraw_body(tab);
507 static void
508 cmd_kill_telescope(struct tab *tab)
510 event_loopbreak();
513 static void
514 cmd_push_button(struct tab *tab)
516 struct vline *vl;
517 size_t nth;
519 nth = tab->s.line_off + tab->s.curs_y;
520 if (nth >= tab->s.line_max)
521 return;
522 vl = nth_line(tab, nth);
523 if (vl->parent->type != LINE_LINK)
524 return;
526 load_url_in_tab(tab, vl->parent->alt);
529 static void
530 cmd_push_button_new_tab(struct tab *tab)
532 struct vline *vl;
533 size_t nth;
535 nth = tab->s.line_off + tab->s.curs_y;
536 if (nth > tab->s.line_max)
537 return;
538 vl = nth_line(tab, nth);
539 if (vl->parent->type != LINE_LINK)
540 return;
542 new_tab(vl->parent->alt);
545 static void
546 cmd_previous_page(struct tab *tab)
548 if (!load_previous_page(tab))
549 message("No previous page");
550 else
551 start_loading_anim(tab);
554 static void
555 cmd_next_page(struct tab *tab)
557 if (!load_next_page(tab))
558 message("No next page");
559 else
560 start_loading_anim(tab);
563 static void
564 cmd_clear_minibuf(struct tab *tab)
566 handle_clear_minibuf(0, 0, NULL);
569 static void
570 cmd_execute_extended_command(struct tab *tab)
572 size_t len;
574 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
575 &eecmd_history);
577 len = sizeof(ministate.prompt);
578 strlcpy(ministate.prompt, "", len);
580 if (thiskey.meta)
581 strlcat(ministate.prompt, "M-", len);
583 strlcat(ministate.prompt, keyname(thiskey.key), len);
584 strlcat(ministate.prompt, " ", len);
587 static void
588 cmd_tab_close(struct tab *tab)
590 struct tab *t;
592 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
593 TAILQ_NEXT(tab, tabs) == NULL) {
594 message("Can't close the only tab.");
595 return;
598 stop_tab(tab);
600 t = TAILQ_PREV(tab, tabshead, tabs);
601 t->flags |= TAB_CURRENT;
603 TAILQ_REMOVE(&tabshead, tab, tabs);
605 free(tab);
608 static void
609 cmd_tab_close_other(struct tab *tab)
611 struct tab *t, *i;
613 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
614 if (t->flags & TAB_CURRENT)
615 continue;
617 stop_tab(t);
618 TAILQ_REMOVE(&tabshead, t, tabs);
619 free(t);
623 static void
624 cmd_tab_new(struct tab *tab)
626 new_tab(NEW_TAB_URL);
629 static void
630 cmd_tab_next(struct tab *tab)
632 struct tab *t;
634 tab->flags &= ~TAB_CURRENT;
636 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
637 t = TAILQ_FIRST(&tabshead);
638 t->flags |= TAB_CURRENT;
641 static void
642 cmd_tab_previous(struct tab *tab)
644 struct tab *t;
646 tab->flags &= ~TAB_CURRENT;
648 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
649 t = TAILQ_LAST(&tabshead, tabshead);
650 t->flags |= TAB_CURRENT;
653 static void
654 cmd_load_url(struct tab *tab)
656 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
657 &lu_history);
658 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
661 static void
662 cmd_load_current_url(struct tab *tab)
664 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
665 &lu_history);
666 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
667 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
668 ministate.off = strlen(tab->hist_cur->h);
669 ministate.len = ministate.off;
672 static void
673 cmd_bookmark_page(struct tab *tab)
675 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
676 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
677 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
678 ministate.off = strlen(tab->hist_cur->h);
679 ministate.len = ministate.off;
682 static void
683 cmd_goto_bookmarks(struct tab *tab)
685 load_url_in_tab(tab, "about:bookmarks");
688 static void
689 global_key_unbound(void)
691 message("%s is undefined", keybuf);
694 static void
695 cmd_mini_delete_char(struct tab *tab)
697 minibuffer_taint_hist();
699 if (ministate.len == 0 || ministate.off == ministate.len)
700 return;
702 memmove(&ministate.buf[ministate.off],
703 &ministate.buf[ministate.off+1],
704 ministate.len - ministate.off + 1);
705 ministate.len--;
708 static void
709 cmd_mini_delete_backward_char(struct tab *tab)
711 minibuffer_taint_hist();
713 if (ministate.len == 0 || ministate.off == 0)
714 return;
716 memmove(&ministate.buf[ministate.off-1],
717 &ministate.buf[ministate.off],
718 ministate.len - ministate.off + 1);
719 ministate.off--;
720 ministate.len--;
723 static void
724 cmd_mini_forward_char(struct tab *tab)
726 if (ministate.off == ministate.len)
727 return;
728 ministate.off++;
731 static void
732 cmd_mini_backward_char(struct tab *tab)
734 if (ministate.off == 0)
735 return;
736 ministate.off--;
739 static void
740 cmd_mini_move_end_of_line(struct tab *tab)
742 ministate.off = ministate.len;
745 static void
746 cmd_mini_move_beginning_of_line(struct tab *tab)
748 ministate.off = 0;
751 static void
752 cmd_mini_kill_line(struct tab *tab)
754 minibuffer_taint_hist();
756 if (ministate.off == ministate.len)
757 return;
758 ministate.buf[ministate.off] = '\0';
759 ministate.len -= ministate.off;
762 static void
763 cmd_mini_abort(struct tab *tab)
765 ministate.abortfn();
768 static void
769 cmd_mini_complete_and_exit(struct tab *tab)
771 minibuffer_taint_hist();
772 ministate.donefn();
775 static void
776 cmd_mini_previous_history_element(struct tab *tab)
778 if (ministate.history == NULL) {
779 message("No history");
780 return;
783 if (ministate.hist_cur == NULL ||
784 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
785 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
786 ministate.hist_off = ministate.history->len - 1;
787 if (ministate.hist_cur == NULL)
788 message("No prev item");
789 } else {
790 ministate.hist_off--;
793 if (ministate.hist_cur != NULL) {
794 ministate.off = 0;
795 ministate.len = strlen(ministate.hist_cur->h);
799 static void
800 cmd_mini_next_history_element(struct tab *tab)
802 if (ministate.history == NULL) {
803 message("No history");
804 return;
807 if (ministate.hist_cur == NULL ||
808 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
809 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
810 ministate.hist_off = 0;
811 if (ministate.hist_cur == NULL)
812 message("No next item");
813 } else {
814 ministate.hist_off++;
817 if (ministate.hist_cur != NULL) {
818 ministate.off = 0;
819 ministate.len = strlen(ministate.hist_cur->h);
823 static void
824 minibuffer_hist_save_entry(void)
826 struct hist *hist;
828 if (ministate.history == NULL)
829 return;
831 if ((hist = calloc(1, sizeof(*hist))) == NULL)
832 abort();
834 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
836 if (TAILQ_EMPTY(&ministate.history->head))
837 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
838 else
839 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
840 ministate.history->len++;
843 /*
844 * taint the minibuffer cache: if we're currently showing a history
845 * element, copy that to the current buf and reset the "history
846 * navigation" thing.
847 */
848 static void
849 minibuffer_taint_hist(void)
851 if (ministate.hist_cur == NULL)
852 return;
854 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
855 ministate.hist_cur = NULL;
858 static void
859 minibuffer_self_insert(void)
861 minibuffer_taint_hist();
863 if (ministate.len == sizeof(ministate.buf) -1)
864 return;
866 /* TODO: utf8 handling! */
868 memmove(&ministate.buf[ministate.off+1],
869 &ministate.buf[ministate.off],
870 ministate.len - ministate.off + 1);
871 ministate.buf[ministate.off] = thiskey.key;
872 ministate.off++;
873 ministate.len++;
876 static void
877 eecmd_self_insert(void)
879 if (thiskey.meta || isspace(thiskey.key) ||
880 !isgraph(thiskey.key)) {
881 global_key_unbound();
882 return;
885 minibuffer_self_insert();
888 static void
889 eecmd_select(void)
891 exit_minibuffer();
892 minibuffer_hist_save_entry();
893 message("TODO: try to execute %s", ministate.buf);
896 static void
897 ir_self_insert(void)
899 minibuffer_self_insert();
902 static void
903 ir_select(void)
905 char buf[1025] = {0};
906 struct url url;
907 struct tab *tab;
909 tab = current_tab();
911 exit_minibuffer();
912 minibuffer_hist_save_entry();
914 /* a bit ugly but... */
915 memcpy(&url, &tab->url, sizeof(tab->url));
916 url_set_query(&url, ministate.buf);
917 url_unparse(&url, buf, sizeof(buf));
918 load_url_in_tab(tab, buf);
921 static void
922 lu_self_insert(void)
924 if (thiskey.meta || isspace(thiskey.key) ||
925 !isgraph(thiskey.key)) {
926 global_key_unbound();
927 return;
930 minibuffer_self_insert();
933 static void
934 lu_select(void)
936 exit_minibuffer();
937 minibuffer_hist_save_entry();
938 load_url_in_tab(current_tab(), ministate.buf);
941 static void
942 bp_select(void)
944 exit_minibuffer();
945 if (*ministate.buf != '\0')
946 add_to_bookmarks(ministate.buf);
947 else
948 message("Abort.");
951 static struct vline *
952 nth_line(struct tab *tab, size_t n)
954 struct vline *vl;
955 size_t i;
957 i = 0;
958 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
959 if (i == n)
960 return vl;
961 i++;
964 /* unreachable */
965 abort();
968 static struct tab *
969 current_tab(void)
971 struct tab *t;
973 TAILQ_FOREACH(t, &tabshead, tabs) {
974 if (t->flags & TAB_CURRENT)
975 return t;
978 /* unreachable */
979 abort();
982 static void
983 dispatch_stdio(int fd, short ev, void *d)
985 struct keymap *k;
986 const char *keyname;
987 char tmp[2] = {0};
989 thiskey.key = wgetch(body);
990 if (thiskey.key == ERR)
991 return;
992 if (thiskey.key == 27) {
993 /* TODO: make escape-time customizable */
995 thiskey.meta = 1;
996 thiskey.key = wgetch(body);
997 if (thiskey.key == ERR || thiskey.key == 27) {
998 thiskey.meta = 0;
999 thiskey.key = 27;
1001 } else
1002 thiskey.meta = 0;
1004 if (keybuf[0] != '\0')
1005 strlcat(keybuf, " ", sizeof(keybuf));
1006 if (thiskey.meta)
1007 strlcat(keybuf, "M-", sizeof(keybuf));
1008 if ((keyname = unkbd(thiskey.key)) != NULL)
1009 strlcat(keybuf, keyname, sizeof(keybuf));
1010 else {
1011 tmp[0] = thiskey.key;
1012 strlcat(keybuf, tmp, sizeof(keybuf));
1015 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1016 if (k->meta == thiskey.meta &&
1017 k->key == thiskey.key) {
1018 if (k->fn == NULL)
1019 current_map = &k->map;
1020 else {
1021 current_map = base_map;
1022 strlcpy(keybuf, "", sizeof(keybuf));
1023 k->fn(current_tab());
1025 goto done;
1029 if (current_map->unhandled_input != NULL)
1030 current_map->unhandled_input();
1031 else {
1032 global_key_unbound();
1035 strlcpy(keybuf, "", sizeof(keybuf));
1036 current_map = base_map;
1038 done:
1039 redraw_tab(current_tab());
1042 static void
1043 handle_clear_minibuf(int fd, short ev, void *d)
1045 clminibufev_set = 0;
1047 free(ministate.curmesg);
1048 ministate.curmesg = NULL;
1050 redraw_minibuffer();
1051 if (in_minibuffer) {
1052 wrefresh(body);
1053 wrefresh(minibuf);
1054 } else {
1055 wrefresh(minibuf);
1056 wrefresh(body);
1060 static void
1061 handle_resize(int sig, short ev, void *d)
1063 struct tab *tab;
1065 endwin();
1066 refresh();
1067 clear();
1069 /* move and resize the windows, in reverse order! */
1071 mvwin(minibuf, LINES-1, 0);
1072 wresize(minibuf, 1, COLS);
1074 mvwin(modeline, LINES-2, 0);
1075 wresize(modeline, 1, COLS);
1077 wresize(body, LINES-3, COLS);
1078 body_lines = LINES-3;
1079 body_cols = COLS;
1081 wresize(tabline, 1, COLS);
1083 tab = current_tab();
1085 wrap_page(tab);
1086 redraw_tab(tab);
1089 static int
1090 wrap_page(struct tab *tab)
1092 struct line *l;
1093 const char *prfx;
1095 empty_vlist(tab);
1097 TAILQ_FOREACH(l, &tab->page.head, lines) {
1098 prfx = line_prefixes[l->type].prfx1;
1099 switch (l->type) {
1100 case LINE_TEXT:
1101 case LINE_LINK:
1102 case LINE_TITLE_1:
1103 case LINE_TITLE_2:
1104 case LINE_TITLE_3:
1105 case LINE_ITEM:
1106 case LINE_QUOTE:
1107 case LINE_PRE_START:
1108 case LINE_PRE_END:
1109 wrap_text(tab, prfx, l, body_cols);
1110 /* push_line(tab, l, NULL, 0, 0); */
1111 break;
1112 case LINE_PRE_CONTENT:
1113 hardwrap_text(tab, l, body_cols);
1114 break;
1117 return 1;
1120 static void
1121 print_vline(struct vline *vl)
1123 const char *text = vl->line;
1124 const char *prfx;
1125 int face = line_faces[vl->parent->type].prop;
1127 if (!vl->flags)
1128 prfx = line_prefixes[vl->parent->type].prfx1;
1129 else
1130 prfx = line_prefixes[vl->parent->type].prfx2;
1132 if (text == NULL)
1133 text = "";
1135 if (face != 0)
1136 wattron(body, face);
1137 wprintw(body, "%s%s", prfx, text);
1138 if (face != 0)
1139 wattroff(body, face);
1142 static void
1143 redraw_tabline(void)
1145 struct tab *tab;
1146 size_t toskip;
1147 int current, x, y, truncated;
1148 const char *title;
1149 char buf[25];
1151 toskip = 0;
1152 x = 1;
1153 TAILQ_FOREACH(tab, &tabshead, tabs) {
1154 x += sizeof(buf) + 1;
1155 toskip++;
1156 if (tab->flags & TAB_CURRENT)
1157 break;
1159 if (x < COLS-2)
1160 toskip = 0;
1161 else
1162 toskip--;
1164 werase(tabline);
1165 wattron(tabline, A_REVERSE);
1166 wprintw(tabline, toskip == 0 ? " " : "<");
1168 truncated = 0;
1169 TAILQ_FOREACH(tab, &tabshead, tabs) {
1170 if (truncated)
1171 break;
1172 if (toskip != 0) {
1173 toskip--;
1174 continue;
1177 getyx(tabline, y, x);
1178 if (x + sizeof(buf)+2 >= (size_t)COLS)
1179 truncated = 1;
1181 current = tab->flags & TAB_CURRENT;
1183 if (*(title = tab->page.title) == '\0')
1184 title = tab->hist_cur->h;
1186 strlcpy(buf, " ", sizeof(buf));
1187 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1188 /* truncation happens */
1189 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1190 } else {
1191 /* pad with spaces */
1192 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1193 /* nop */ ;
1196 if (current)
1197 wattroff(tabline, A_REVERSE);
1199 wprintw(tabline, "%s", buf);
1201 if (current)
1202 wattron(tabline, A_REVERSE);
1204 if (TAILQ_NEXT(tab, tabs) != NULL)
1205 wprintw(tabline, " ");
1208 for (; x < COLS; ++x)
1209 waddch(tabline, ' ');
1210 if (truncated)
1211 mvwprintw(tabline, 0, COLS-1, ">");
1214 static void
1215 redraw_modeline(struct tab *tab)
1217 double pct;
1218 int x, y, max_x, max_y;
1219 const char *mode = tab->page.name;
1220 const char *spin = "-\\|/";
1222 werase(modeline);
1223 wattron(modeline, A_REVERSE);
1224 wmove(modeline, 0, 0);
1226 wprintw(modeline, "-%c %s ",
1227 spin[tab->s.loading_anim_step], mode);
1229 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1231 if (tab->s.line_max <= (size_t)body_lines)
1232 wprintw(modeline, "All ");
1233 else if (tab->s.line_off == 0)
1234 wprintw(modeline, "Top ");
1235 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1236 wprintw(modeline, "Bottom ");
1237 else
1238 wprintw(modeline, "%.0f%% ", pct);
1240 wprintw(modeline, "%d/%d %s ",
1241 tab->s.line_off + tab->s.curs_y,
1242 tab->s.line_max,
1243 tab->hist_cur->h);
1245 getyx(modeline, y, x);
1246 getmaxyx(modeline, max_y, max_x);
1248 (void)y;
1249 (void)max_y;
1251 for (; x < max_x; ++x)
1252 waddstr(modeline, "-");
1255 static void
1256 redraw_minibuffer(void)
1258 size_t skip = 0, off_x = 0, off_y = 0;
1260 werase(minibuf);
1261 if (in_minibuffer) {
1262 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1263 if (ministate.hist_cur != NULL)
1264 wprintw(minibuf, "(%zu/%zu) ",
1265 ministate.hist_off + 1,
1266 ministate.history->len);
1268 getyx(minibuf, off_y, off_x);
1270 while (ministate.off - skip > (size_t)COLS / 2) {
1271 skip += MIN(ministate.off/4, 1);
1274 if (ministate.hist_cur != NULL)
1275 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1276 else
1277 wprintw(minibuf, "%s", ministate.buf + skip);
1280 if (ministate.curmesg != NULL) {
1281 if (in_minibuffer)
1282 wprintw(minibuf, " [%s]", ministate.curmesg);
1283 else
1284 wprintw(minibuf, "%s", ministate.curmesg);
1287 if (!in_minibuffer && ministate.curmesg == NULL)
1288 wprintw(minibuf, "%s", keybuf);
1290 if (in_minibuffer)
1291 wmove(minibuf, 0, off_x + ministate.off - skip);
1294 static void
1295 redraw_tab(struct tab *tab)
1297 redraw_tabline();
1298 redraw_body(tab);
1299 redraw_modeline(tab);
1300 redraw_minibuffer();
1302 restore_cursor(tab);
1303 wrefresh(tabline);
1304 wrefresh(modeline);
1306 if (in_minibuffer) {
1307 wrefresh(body);
1308 wrefresh(minibuf);
1309 } else {
1310 wrefresh(minibuf);
1311 wrefresh(body);
1315 static void
1316 redraw_body(struct tab *tab)
1318 struct vline *vl;
1319 int line;
1321 werase(body);
1323 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1324 if (TAILQ_EMPTY(&tab->s.head))
1325 return;
1327 line = 0;
1328 vl = nth_line(tab, tab->s.line_off);
1329 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1330 wmove(body, line, 0);
1331 print_vline(vl);
1332 line++;
1333 if (line == body_lines)
1334 break;
1338 static void
1339 vmessage(const char *fmt, va_list ap)
1341 if (clminibufev_set)
1342 evtimer_del(&clminibufev);
1343 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1344 evtimer_add(&clminibufev, &clminibufev_timer);
1345 clminibufev_set = 1;
1347 free(ministate.curmesg);
1349 /* TODO: what to do if the allocation fails here? */
1350 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1351 ministate.curmesg = NULL;
1353 redraw_minibuffer();
1354 if (in_minibuffer) {
1355 wrefresh(body);
1356 wrefresh(minibuf);
1357 } else {
1358 wrefresh(minibuf);
1359 wrefresh(body);
1363 static void
1364 message(const char *fmt, ...)
1366 va_list ap;
1368 va_start(ap, fmt);
1369 vmessage(fmt, ap);
1370 va_end(ap);
1373 static void
1374 start_loading_anim(struct tab *tab)
1376 if (tab->s.loading_anim)
1377 return;
1378 tab->s.loading_anim = 1;
1379 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1380 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1383 static void
1384 update_loading_anim(int fd, short ev, void *d)
1386 struct tab *tab = d;
1388 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1390 redraw_modeline(tab);
1391 wrefresh(modeline);
1393 wrefresh(body);
1394 if (in_minibuffer)
1395 wrefresh(minibuf);
1397 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1400 static void
1401 stop_loading_anim(struct tab *tab)
1403 if (!tab->s.loading_anim)
1404 return;
1405 evtimer_del(&tab->s.loadingev);
1406 tab->s.loading_anim = 0;
1407 tab->s.loading_anim_step = 0;
1409 redraw_modeline(tab);
1411 wrefresh(modeline);
1412 wrefresh(body);
1413 if (in_minibuffer)
1414 wrefresh(minibuf);
1417 static void
1418 load_url_in_tab(struct tab *tab, const char *url)
1420 empty_vlist(tab);
1421 message("Loading %s...", url);
1422 start_loading_anim(tab);
1423 load_url(tab, url);
1425 tab->s.curs_x = 0;
1426 tab->s.curs_y = 0;
1427 redraw_tab(tab);
1430 static void
1431 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1432 void (*abortfn)(void), struct histhead *hist)
1434 in_minibuffer = 1;
1435 base_map = &minibuffer_map;
1436 current_map = &minibuffer_map;
1438 base_map->unhandled_input = self_insert_fn;
1440 ministate.donefn = donefn;
1441 ministate.abortfn = abortfn;
1442 memset(ministate.buf, 0, sizeof(ministate.buf));
1443 ministate.off = 0;
1444 ministate.len = 0;
1445 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1447 ministate.history = hist;
1448 ministate.hist_cur = NULL;
1449 ministate.hist_off = 0;
1452 static void
1453 exit_minibuffer(void)
1455 werase(minibuf);
1457 in_minibuffer = 0;
1458 base_map = &global_map;
1459 current_map = &global_map;
1462 static void
1463 switch_to_tab(struct tab *tab)
1465 struct tab *t;
1467 TAILQ_FOREACH(t, &tabshead, tabs) {
1468 t->flags &= ~TAB_CURRENT;
1471 tab->flags |= TAB_CURRENT;
1474 static struct tab *
1475 new_tab(const char *url)
1477 struct tab *tab;
1479 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1480 goto err;
1482 TAILQ_INIT(&tab->hist.head);
1484 TAILQ_INIT(&tab->s.head);
1486 tab->id = tab_counter++;
1487 switch_to_tab(tab);
1489 if (TAILQ_EMPTY(&tabshead))
1490 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1491 else
1492 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1494 load_url_in_tab(tab, url);
1495 return tab;
1497 err:
1498 event_loopbreak();
1499 return NULL;
1502 int
1503 ui_init(void)
1505 setlocale(LC_ALL, "");
1507 TAILQ_INIT(&global_map.m);
1508 global_map.unhandled_input = global_key_unbound;
1510 TAILQ_INIT(&minibuffer_map.m);
1512 TAILQ_INIT(&eecmd_history.head);
1513 TAILQ_INIT(&ir_history.head);
1514 TAILQ_INIT(&lu_history.head);
1516 base_map = &global_map;
1517 current_map = &global_map;
1518 load_default_keys();
1520 initscr();
1521 raw();
1522 noecho();
1524 nonl();
1525 intrflush(stdscr, FALSE);
1527 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1528 return 0;
1529 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1530 return 0;
1531 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1532 return 0;
1533 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1534 return 0;
1536 body_lines = LINES-3;
1537 body_cols = COLS;
1539 keypad(body, TRUE);
1540 scrollok(body, TRUE);
1542 /* non-blocking input */
1543 wtimeout(body, 0);
1545 mvwprintw(body, 0, 0, "");
1547 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1548 event_add(&stdioev, NULL);
1550 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1551 signal_add(&winchev, NULL);
1553 new_tab(NEW_TAB_URL);
1555 return 1;
1558 void
1559 ui_on_tab_loaded(struct tab *tab)
1561 stop_loading_anim(tab);
1562 message("Loaded %s", tab->hist_cur->h);
1565 void
1566 ui_on_tab_refresh(struct tab *tab)
1568 if (!(tab->flags & TAB_CURRENT))
1569 return;
1571 wrap_page(tab);
1572 redraw_tab(tab);
1575 void
1576 ui_require_input(struct tab *tab, int hide)
1578 /* TODO: hard-switching to another tab is ugly */
1579 switch_to_tab(tab);
1581 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1582 &ir_history);
1583 strlcpy(ministate.prompt, "Input required: ",
1584 sizeof(ministate.prompt));
1585 redraw_tab(tab);
1588 void
1589 ui_notify(const char *fmt, ...)
1591 va_list ap;
1593 va_start(ap, fmt);
1594 vmessage(fmt, ap);
1595 va_end(ap);
1598 void
1599 ui_end(void)
1601 endwin();