2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Ncurses UI for telescope.
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
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
33 * TODO: make the text formatting on-demand.
37 #include <telescope.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_backward_char(struct tab*);
62 static void cmd_forward_char(struct tab*);
63 static void cmd_backward_paragraph(struct tab*);
64 static void cmd_forward_paragraph(struct tab*);
65 static void cmd_move_beginning_of_line(struct tab*);
66 static void cmd_move_end_of_line(struct tab*);
67 static void cmd_redraw(struct tab*);
68 static void cmd_scroll_line_down(struct tab*);
69 static void cmd_scroll_line_up(struct tab*);
70 static void cmd_scroll_up(struct tab*);
71 static void cmd_scroll_down(struct tab*);
72 static void cmd_beginning_of_buffer(struct tab*);
73 static void cmd_end_of_buffer(struct tab*);
74 static void cmd_kill_telescope(struct tab*);
75 static void cmd_push_button(struct tab*);
76 static void cmd_push_button_new_tab(struct tab*);
77 static void cmd_previous_button(struct tab*);
78 static void cmd_next_button(struct tab*);
79 static void cmd_previous_page(struct tab*);
80 static void cmd_next_page(struct tab*);
81 static void cmd_clear_minibuf(struct tab*);
82 static void cmd_execute_extended_command(struct tab*);
83 static void cmd_tab_close(struct tab*);
84 static void cmd_tab_close_other(struct tab*);
85 static void cmd_tab_new(struct tab*);
86 static void cmd_tab_next(struct tab*);
87 static void cmd_tab_previous(struct tab*);
88 static void cmd_load_url(struct tab*);
89 static void cmd_load_current_url(struct tab*);
90 static void cmd_bookmark_page(struct tab*);
91 static void cmd_goto_bookmarks(struct tab*);
93 static void global_key_unbound(void);
95 static void cmd_mini_delete_char(struct tab*);
96 static void cmd_mini_delete_backward_char(struct tab*);
97 static void cmd_mini_forward_char(struct tab*);
98 static void cmd_mini_backward_char(struct tab*);
99 static void cmd_mini_move_end_of_line(struct tab*);
100 static void cmd_mini_move_beginning_of_line(struct tab*);
101 static void cmd_mini_kill_line(struct tab*);
102 static void cmd_mini_abort(struct tab*);
103 static void cmd_mini_complete_and_exit(struct tab*);
104 static void cmd_mini_previous_history_element(struct tab*);
105 static void cmd_mini_next_history_element(struct tab*);
107 static void minibuffer_hist_save_entry(void);
108 static void minibuffer_taint_hist(void);
109 static void minibuffer_self_insert(void);
110 static void eecmd_self_insert(void);
111 static void eecmd_select(void);
112 static void ir_self_insert(void);
113 static void ir_select(void);
114 static void lu_self_insert(void);
115 static void lu_select(void);
116 static void bp_select(void);
118 static struct vline *nth_line(struct tab*, size_t);
119 static struct tab *current_tab(void);
120 static void dispatch_stdio(int, short, void*);
121 static void handle_clear_minibuf(int, short, void*);
122 static void handle_resize(int, short, void*);
123 static int wrap_page(struct tab*);
124 static void print_vline(struct vline*);
125 static void redraw_tabline(void);
126 static void redraw_body(struct tab*);
127 static void redraw_modeline(struct tab*);
128 static void redraw_minibuffer(void);
129 static void redraw_tab(struct tab*);
130 static void vmessage(const char*, va_list);
131 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
132 static void start_loading_anim(struct tab*);
133 static void update_loading_anim(int, short, void*);
134 static void stop_loading_anim(struct tab*);
135 static void load_url_in_tab(struct tab*, const char*);
136 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
137 static void exit_minibuffer(void);
138 static void switch_to_tab(struct tab*);
139 static struct tab *new_tab(const char*);
140 static void usage(void);
142 static struct { int meta, key; } thiskey;
144 static WINDOW *tabline, *body, *modeline, *minibuf;
145 static int body_lines, body_cols;
147 static struct event clminibufev;
148 static struct timeval clminibufev_timer = { 5, 0 };
149 static struct timeval loadingev_timer = { 0, 250000 };
151 static uint32_t tab_counter;
153 static char keybuf[64];
155 struct kmap global_map,
160 static struct histhead eecmd_history,
164 static int in_minibuffer;
172 void (*donefn)(void);
173 void (*abortfn)(void);
175 struct histhead *history;
176 struct hist *hist_cur;
183 } line_prefixes[] = {
184 [LINE_TEXT] = { "", "" },
185 [LINE_LINK] = { "=> ", " " },
186 [LINE_TITLE_1] = { "# ", " " },
187 [LINE_TITLE_2] = { "## ", " " },
188 [LINE_TITLE_3] = { "### ", " " },
189 [LINE_ITEM] = { "* ", " " },
190 [LINE_QUOTE] = { "> ", " " },
191 [LINE_PRE_START] = { "```", " " },
192 [LINE_PRE_CONTENT] = { "", "" },
193 [LINE_PRE_END] = { "```", "```" },
196 static struct line_face {
200 [LINE_TEXT] = { 0, 0 },
201 [LINE_LINK] = { 0, A_UNDERLINE },
202 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
203 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
204 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
205 [LINE_ITEM] = { 0, 0 },
206 [LINE_QUOTE] = { 0, A_DIM },
207 [LINE_PRE_START] = { 0, 0 },
208 [LINE_PRE_CONTENT] = { 0, 0 },
209 [LINE_PRE_END] = { 0, 0 },
212 static struct tab_face {
213 int background, tab, current_tab;
215 A_REVERSE, A_REVERSE, A_NORMAL
219 empty_vlist(struct tab *tab)
221 struct vline *vl, *t;
223 tab->s.current_line = NULL;
226 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
227 TAILQ_REMOVE(&tab->s.head, vl, vlines);
234 global_set_key(const char *key, void (*fn)(struct tab*))
236 if (!kmap_define_key(&global_map, key, fn))
241 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
243 if (!kmap_define_key(&minibuffer_map, key, fn))
248 load_default_keys(void)
250 /* === global map === */
253 global_set_key("C-p", cmd_previous_line);
254 global_set_key("C-n", cmd_next_line);
255 global_set_key("C-f", cmd_forward_char);
256 global_set_key("C-b", cmd_backward_char);
257 global_set_key("M-{", cmd_backward_paragraph);
258 global_set_key("M-}", cmd_forward_paragraph);
259 global_set_key("C-a", cmd_move_beginning_of_line);
260 global_set_key("C-e", cmd_move_end_of_line);
262 global_set_key("M-v", cmd_scroll_up);
263 global_set_key("C-v", cmd_scroll_down);
264 global_set_key("M-space", cmd_scroll_up);
265 global_set_key("space", cmd_scroll_down);
267 global_set_key("C-x C-c", cmd_kill_telescope);
269 global_set_key("C-g", cmd_clear_minibuf);
271 global_set_key("M-x", cmd_execute_extended_command);
272 global_set_key("C-x C-f", cmd_load_url);
273 global_set_key("C-x M-f", cmd_load_current_url);
275 global_set_key("C-x t 0", cmd_tab_close);
276 global_set_key("C-x t 1", cmd_tab_close_other);
277 global_set_key("C-x t 2", cmd_tab_new);
278 global_set_key("C-x t o", cmd_tab_next);
279 global_set_key("C-x t O", cmd_tab_previous);
281 global_set_key("M-<", cmd_beginning_of_buffer);
282 global_set_key("M->", cmd_end_of_buffer);
284 global_set_key("C-M-b", cmd_previous_page);
285 global_set_key("C-M-f", cmd_next_page);
287 global_set_key("<f7> a", cmd_bookmark_page);
288 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
291 global_set_key("k", cmd_previous_line);
292 global_set_key("j", cmd_next_line);
293 global_set_key("l", cmd_forward_char);
294 global_set_key("h", cmd_backward_char);
295 global_set_key("{", cmd_backward_paragraph);
296 global_set_key("}", cmd_forward_paragraph);
297 global_set_key("^", cmd_move_beginning_of_line);
298 global_set_key("$", cmd_move_end_of_line);
300 global_set_key("K", cmd_scroll_line_up);
301 global_set_key("J", cmd_scroll_line_down);
303 global_set_key("g D", cmd_tab_close);
304 global_set_key("g N", cmd_tab_new);
305 global_set_key("g t", cmd_tab_next);
306 global_set_key("g T", cmd_tab_previous);
308 global_set_key("g g", cmd_beginning_of_buffer);
309 global_set_key("G", cmd_end_of_buffer);
311 global_set_key("H", cmd_previous_page);
312 global_set_key("L", cmd_next_page);
315 global_set_key("q", cmd_kill_telescope);
317 global_set_key("esc", cmd_clear_minibuf);
319 global_set_key(":", cmd_execute_extended_command);
322 global_set_key("<up>", cmd_previous_line);
323 global_set_key("<down>", cmd_next_line);
324 global_set_key("<right>", cmd_forward_char);
325 global_set_key("<left>", cmd_backward_char);
326 global_set_key("<prior>", cmd_scroll_up);
327 global_set_key("<next>", cmd_scroll_down);
329 global_set_key("M-<left>", cmd_previous_page);
330 global_set_key("M-<right>", cmd_next_page);
332 /* "ncurses standard" */
333 global_set_key("C-l", cmd_redraw);
336 global_set_key("C-m", cmd_push_button);
337 global_set_key("M-enter", cmd_push_button_new_tab);
338 global_set_key("M-tab", cmd_previous_button);
339 global_set_key("tab", cmd_next_button);
341 /* === minibuffer map === */
342 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
343 minibuffer_set_key("C-g", cmd_mini_abort);
344 minibuffer_set_key("esc", cmd_mini_abort);
345 minibuffer_set_key("C-d", cmd_mini_delete_char);
346 minibuffer_set_key("del", cmd_mini_delete_backward_char);
348 minibuffer_set_key("C-f", cmd_mini_forward_char);
349 minibuffer_set_key("C-b", cmd_mini_backward_char);
350 minibuffer_set_key("<right>", cmd_mini_forward_char);
351 minibuffer_set_key("<left>", cmd_mini_backward_char);
352 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
353 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
354 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
355 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
356 minibuffer_set_key("C-k", cmd_mini_kill_line);
358 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
359 minibuffer_set_key("M-n", cmd_mini_next_history_element);
360 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
361 minibuffer_set_key("<down>", cmd_mini_next_history_element);
365 restore_cursor(struct tab *tab)
370 vl =tab->s.current_line;
371 if (vl == NULL || vl->line == NULL)
372 tab->s.curs_x = tab->s.line_x = 0;
374 tab->s.curs_x = utf8_snwidth(vl->line, tab->s.line_x);
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 tab->s.curs_x += utf8_swidth(prfx);
383 cmd_previous_line(struct tab *tab)
387 if (tab->s.current_line == NULL
388 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
391 if (--tab->s.curs_y < 0) {
393 cmd_scroll_line_up(tab);
397 tab->s.current_line = vl;
402 cmd_next_line(struct tab *tab)
406 if (tab->s.current_line == NULL
407 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
410 if (++tab->s.curs_y > body_lines-1) {
411 tab->s.curs_y = body_lines-1;
412 cmd_scroll_line_down(tab);
416 tab->s.current_line = vl;
421 cmd_backward_char(struct tab *tab)
423 if (tab->s.line_x != 0)
429 cmd_forward_char(struct tab *tab)
436 cmd_backward_paragraph(struct tab *tab)
439 if (tab->s.current_line == NULL ||
440 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
441 message("No previous paragraph");
444 cmd_previous_line(tab);
445 } while (tab->s.current_line->line != NULL ||
446 tab->s.current_line->parent->type != LINE_TEXT);
450 cmd_forward_paragraph(struct tab *tab)
453 if (tab->s.current_line == NULL ||
454 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
455 message("No next paragraph");
459 } while (tab->s.current_line->line != NULL ||
460 tab->s.current_line->parent->type != LINE_TEXT);
464 cmd_move_beginning_of_line(struct tab *tab)
471 cmd_move_end_of_line(struct tab *tab)
475 vl = tab->s.current_line;
476 if (vl->line == NULL)
478 tab->s.line_x = utf8_cplen(vl->line);
483 cmd_redraw(struct tab *tab)
485 handle_resize(0, 0, NULL);
489 cmd_scroll_line_up(struct tab *tab)
493 if (tab->s.line_off == 0)
496 vl = nth_line(tab, --tab->s.line_off);
501 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
506 cmd_scroll_line_down(struct tab *tab)
510 vl = tab->s.current_line;
511 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
513 tab->s.current_line = vl;
518 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
521 vl = nth_line(tab, tab->s.line_off + body_lines-1);
522 wmove(body, body_lines-1, 0);
529 cmd_scroll_up(struct tab *tab)
535 for (; off > 0; --off)
536 cmd_scroll_line_up(tab);
540 cmd_scroll_down(struct tab *tab)
546 for (; off > 0; --off)
547 cmd_scroll_line_down(tab);
551 cmd_beginning_of_buffer(struct tab *tab)
553 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
562 cmd_end_of_buffer(struct tab *tab)
566 off = tab->s.line_max - body_lines;
569 tab->s.line_off = off;
570 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
572 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
573 tab->s.line_x = body_cols;
579 cmd_kill_telescope(struct tab *tab)
585 cmd_push_button(struct tab *tab)
590 nth = tab->s.line_off + tab->s.curs_y;
591 if (nth >= tab->s.line_max)
593 vl = nth_line(tab, nth);
594 if (vl->parent->type != LINE_LINK)
597 load_url_in_tab(tab, vl->parent->alt);
601 cmd_push_button_new_tab(struct tab *tab)
606 nth = tab->s.line_off + tab->s.curs_y;
607 if (nth > tab->s.line_max)
609 vl = nth_line(tab, nth);
610 if (vl->parent->type != LINE_LINK)
613 new_tab(vl->parent->alt);
617 cmd_previous_button(struct tab *tab)
620 if (tab->s.current_line == NULL ||
621 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
622 message("No previous link");
625 cmd_previous_line(tab);
626 } while (tab->s.current_line->parent->type != LINE_LINK);
630 cmd_next_button(struct tab *tab)
633 if (tab->s.current_line == NULL ||
634 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
635 message("No next link");
639 } while (tab->s.current_line->parent->type != LINE_LINK);
643 cmd_previous_page(struct tab *tab)
645 if (!load_previous_page(tab))
646 message("No previous page");
648 start_loading_anim(tab);
652 cmd_next_page(struct tab *tab)
654 if (!load_next_page(tab))
655 message("No next page");
657 start_loading_anim(tab);
661 cmd_clear_minibuf(struct tab *tab)
663 handle_clear_minibuf(0, 0, NULL);
667 cmd_execute_extended_command(struct tab *tab)
671 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
674 len = sizeof(ministate.prompt);
675 strlcpy(ministate.prompt, "", len);
678 strlcat(ministate.prompt, "M-", len);
680 strlcat(ministate.prompt, keyname(thiskey.key), len);
681 strlcat(ministate.prompt, " ", len);
685 cmd_tab_close(struct tab *tab)
689 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
690 TAILQ_NEXT(tab, tabs) == NULL) {
691 message("Can't close the only tab.");
695 if (evtimer_pending(&tab->s.loadingev, NULL))
696 evtimer_del(&tab->s.loadingev);
700 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
701 t = TAILQ_NEXT(tab, tabs);
702 TAILQ_REMOVE(&tabshead, tab, tabs);
709 cmd_tab_close_other(struct tab *tab)
713 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
714 if (t->flags & TAB_CURRENT)
718 TAILQ_REMOVE(&tabshead, t, tabs);
724 cmd_tab_new(struct tab *tab)
726 new_tab(NEW_TAB_URL);
730 cmd_tab_next(struct tab *tab)
734 tab->flags &= ~TAB_CURRENT;
736 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
737 t = TAILQ_FIRST(&tabshead);
738 t->flags |= TAB_CURRENT;
742 cmd_tab_previous(struct tab *tab)
746 tab->flags &= ~TAB_CURRENT;
748 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
749 t = TAILQ_LAST(&tabshead, tabshead);
750 t->flags |= TAB_CURRENT;
754 cmd_load_url(struct tab *tab)
756 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
758 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
762 cmd_load_current_url(struct tab *tab)
764 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
766 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
767 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
768 ministate.off = strlen(tab->hist_cur->h);
769 ministate.len = ministate.off;
773 cmd_bookmark_page(struct tab *tab)
775 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
776 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
777 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
778 ministate.off = strlen(tab->hist_cur->h);
779 ministate.len = ministate.off;
783 cmd_goto_bookmarks(struct tab *tab)
785 load_url_in_tab(tab, "about:bookmarks");
789 global_key_unbound(void)
791 message("%s is undefined", keybuf);
795 cmd_mini_delete_char(struct tab *tab)
797 minibuffer_taint_hist();
799 if (ministate.len == 0 || ministate.off == ministate.len)
802 memmove(&ministate.buf[ministate.off],
803 &ministate.buf[ministate.off+1],
804 ministate.len - ministate.off + 1);
809 cmd_mini_delete_backward_char(struct tab *tab)
811 minibuffer_taint_hist();
813 if (ministate.len == 0 || ministate.off == 0)
816 memmove(&ministate.buf[ministate.off-1],
817 &ministate.buf[ministate.off],
818 ministate.len - ministate.off + 1);
824 cmd_mini_forward_char(struct tab *tab)
826 if (ministate.off == ministate.len)
832 cmd_mini_backward_char(struct tab *tab)
834 if (ministate.off == 0)
840 cmd_mini_move_end_of_line(struct tab *tab)
842 ministate.off = ministate.len;
846 cmd_mini_move_beginning_of_line(struct tab *tab)
852 cmd_mini_kill_line(struct tab *tab)
854 minibuffer_taint_hist();
856 if (ministate.off == ministate.len)
858 ministate.buf[ministate.off] = '\0';
859 ministate.len -= ministate.off;
863 cmd_mini_abort(struct tab *tab)
869 cmd_mini_complete_and_exit(struct tab *tab)
871 minibuffer_taint_hist();
876 cmd_mini_previous_history_element(struct tab *tab)
878 if (ministate.history == NULL) {
879 message("No history");
883 if (ministate.hist_cur == NULL ||
884 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
885 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
886 ministate.hist_off = ministate.history->len - 1;
887 if (ministate.hist_cur == NULL)
888 message("No prev item");
890 ministate.hist_off--;
893 if (ministate.hist_cur != NULL) {
895 ministate.len = strlen(ministate.hist_cur->h);
900 cmd_mini_next_history_element(struct tab *tab)
902 if (ministate.history == NULL) {
903 message("No history");
907 if (ministate.hist_cur == NULL ||
908 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
909 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
910 ministate.hist_off = 0;
911 if (ministate.hist_cur == NULL)
912 message("No next item");
914 ministate.hist_off++;
917 if (ministate.hist_cur != NULL) {
919 ministate.len = strlen(ministate.hist_cur->h);
924 minibuffer_hist_save_entry(void)
928 if (ministate.history == NULL)
931 if ((hist = calloc(1, sizeof(*hist))) == NULL)
934 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
936 if (TAILQ_EMPTY(&ministate.history->head))
937 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
939 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
940 ministate.history->len++;
944 * taint the minibuffer cache: if we're currently showing a history
945 * element, copy that to the current buf and reset the "history
949 minibuffer_taint_hist(void)
951 if (ministate.hist_cur == NULL)
954 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
955 ministate.hist_cur = NULL;
959 minibuffer_self_insert(void)
961 minibuffer_taint_hist();
963 if (ministate.len == sizeof(ministate.buf) -1)
966 /* TODO: utf8 handling! */
968 memmove(&ministate.buf[ministate.off+1],
969 &ministate.buf[ministate.off],
970 ministate.len - ministate.off + 1);
971 ministate.buf[ministate.off] = thiskey.key;
977 eecmd_self_insert(void)
979 if (thiskey.meta || isspace(thiskey.key) ||
980 !isgraph(thiskey.key)) {
981 global_key_unbound();
985 minibuffer_self_insert();
992 minibuffer_hist_save_entry();
993 message("TODO: try to execute %s", ministate.buf);
999 minibuffer_self_insert();
1005 char buf[1025] = {0};
1009 tab = current_tab();
1012 minibuffer_hist_save_entry();
1014 /* a bit ugly but... */
1015 memcpy(&url, &tab->url, sizeof(tab->url));
1016 url_set_query(&url, ministate.buf);
1017 url_unparse(&url, buf, sizeof(buf));
1018 load_url_in_tab(tab, buf);
1022 lu_self_insert(void)
1024 if (thiskey.meta || isspace(thiskey.key) ||
1025 !isgraph(thiskey.key)) {
1026 global_key_unbound();
1030 minibuffer_self_insert();
1037 minibuffer_hist_save_entry();
1038 load_url_in_tab(current_tab(), ministate.buf);
1045 if (*ministate.buf != '\0')
1046 add_to_bookmarks(ministate.buf);
1051 static struct vline *
1052 nth_line(struct tab *tab, size_t n)
1058 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1073 TAILQ_FOREACH(t, &tabshead, tabs) {
1074 if (t->flags & TAB_CURRENT)
1083 dispatch_stdio(int fd, short ev, void *d)
1086 const char *keyname;
1089 thiskey.key = wgetch(body);
1090 if (thiskey.key == ERR)
1092 if (thiskey.key == 27) {
1093 /* TODO: make escape-time customizable */
1096 thiskey.key = wgetch(body);
1097 if (thiskey.key == ERR || thiskey.key == 27) {
1104 if (keybuf[0] != '\0')
1105 strlcat(keybuf, " ", sizeof(keybuf));
1107 strlcat(keybuf, "M-", sizeof(keybuf));
1108 if ((keyname = unkbd(thiskey.key)) != NULL)
1109 strlcat(keybuf, keyname, sizeof(keybuf));
1111 tmp[0] = thiskey.key;
1112 strlcat(keybuf, tmp, sizeof(keybuf));
1115 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1116 if (k->meta == thiskey.meta &&
1117 k->key == thiskey.key) {
1119 current_map = &k->map;
1121 current_map = base_map;
1122 strlcpy(keybuf, "", sizeof(keybuf));
1123 k->fn(current_tab());
1129 if (current_map->unhandled_input != NULL)
1130 current_map->unhandled_input();
1132 global_key_unbound();
1135 strlcpy(keybuf, "", sizeof(keybuf));
1136 current_map = base_map;
1139 redraw_tab(current_tab());
1143 handle_clear_minibuf(int fd, short ev, void *d)
1145 free(ministate.curmesg);
1146 ministate.curmesg = NULL;
1148 redraw_minibuffer();
1149 if (in_minibuffer) {
1159 handle_resize(int sig, short ev, void *d)
1167 /* move and resize the windows, in reverse order! */
1169 mvwin(minibuf, LINES-1, 0);
1170 wresize(minibuf, 1, COLS);
1172 mvwin(modeline, LINES-2, 0);
1173 wresize(modeline, 1, COLS);
1175 wresize(body, LINES-3, COLS);
1176 body_lines = LINES-3;
1179 wresize(tabline, 1, COLS);
1181 tab = current_tab();
1188 wrap_page(struct tab *tab)
1191 const struct line *orig;
1195 orig = tab->s.current_line == NULL
1197 : tab->s.current_line->parent;
1198 tab->s.current_line = NULL;
1201 tab->s.line_off = 0;
1205 TAILQ_FOREACH(l, &tab->page.head, lines) {
1206 prfx = line_prefixes[l->type].prfx1;
1215 case LINE_PRE_START:
1217 wrap_text(tab, prfx, l, body_cols);
1219 case LINE_PRE_CONTENT:
1220 hardwrap_text(tab, l, body_cols);
1224 if (orig == l && tab->s.current_line == NULL) {
1225 tab->s.line_off = tab->s.line_max-1;
1226 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1229 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1230 if (vl == NULL || vl->parent != orig)
1232 tab->s.current_line = vl;
1238 if (tab->s.current_line == NULL)
1239 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1245 print_vline(struct vline *vl)
1247 const char *text = vl->line;
1249 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1250 int text_face = line_faces[vl->parent->type].text_prop;
1253 prfx = line_prefixes[vl->parent->type].prfx1;
1255 prfx = line_prefixes[vl->parent->type].prfx2;
1260 wattron(body, prefix_face);
1261 wprintw(body, "%s", prfx);
1262 wattroff(body, prefix_face);
1264 wattron(body, text_face);
1265 wprintw(body, "%s", text);
1266 wattroff(body, text_face);
1270 redraw_tabline(void)
1274 int current, x, y, truncated;
1280 TAILQ_FOREACH(tab, &tabshead, tabs) {
1281 x += sizeof(buf) + 1;
1283 if (tab->flags & TAB_CURRENT)
1292 wattron(tabline, tab_face.background);
1293 wprintw(tabline, toskip == 0 ? " " : "<");
1294 wattroff(tabline, tab_face.background);
1297 TAILQ_FOREACH(tab, &tabshead, tabs) {
1305 getyx(tabline, y, x);
1306 if (x + sizeof(buf)+2 >= (size_t)COLS)
1309 current = tab->flags & TAB_CURRENT;
1311 if (*(title = tab->page.title) == '\0')
1312 title = tab->hist_cur->h;
1314 strlcpy(buf, " ", sizeof(buf));
1315 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1316 /* truncation happens */
1317 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1319 /* pad with spaces */
1320 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1325 wattron(tabline, tab_face.current_tab);
1327 wattron(tabline, tab_face.tab);
1329 wprintw(tabline, "%s", buf);
1330 if (TAILQ_NEXT(tab, tabs) != NULL)
1331 wprintw(tabline, " ");
1334 wattroff(tabline, tab_face.current_tab);
1336 wattroff(tabline, tab_face.tab);
1339 wattron(tabline, tab_face.background);
1340 for (; x < COLS; ++x)
1341 waddch(tabline, ' ');
1343 mvwprintw(tabline, 0, COLS-1, ">");
1347 trust_status_char(enum trust_state ts)
1350 case TS_UNKNOWN: return 'u';
1351 case TS_UNTRUSTED: return '!';
1352 case TS_TRUSTED: return 'v';
1353 case TS_VERIFIED: return 'V';
1358 redraw_modeline(struct tab *tab)
1361 int x, y, max_x, max_y;
1362 const char *mode = tab->page.name;
1363 const char *spin = "-\\|/";
1366 wattron(modeline, A_REVERSE);
1367 wmove(modeline, 0, 0);
1369 wprintw(modeline, "-%c%c %s ",
1370 spin[tab->s.loading_anim_step],
1371 trust_status_char(tab->trust),
1372 mode == NULL ? "(none)" : mode);
1374 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1376 if (tab->s.line_max <= (size_t)body_lines)
1377 wprintw(modeline, "All ");
1378 else if (tab->s.line_off == 0)
1379 wprintw(modeline, "Top ");
1380 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1381 wprintw(modeline, "Bottom ");
1383 wprintw(modeline, "%.0f%% ", pct);
1385 wprintw(modeline, "%d/%d %s ",
1386 tab->s.line_off + tab->s.curs_y,
1390 getyx(modeline, y, x);
1391 getmaxyx(modeline, max_y, max_x);
1396 for (; x < max_x; ++x)
1397 waddstr(modeline, "-");
1401 redraw_minibuffer(void)
1403 size_t skip = 0, off_x = 0, off_y = 0;
1407 if (in_minibuffer) {
1408 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1409 if (ministate.hist_cur != NULL)
1410 wprintw(minibuf, "(%zu/%zu) ",
1411 ministate.hist_off + 1,
1412 ministate.history->len);
1414 getyx(minibuf, off_y, off_x);
1416 while (ministate.off - skip > (size_t)COLS / 2) {
1417 skip += MIN(ministate.off/4, 1);
1420 if (ministate.hist_cur != NULL)
1421 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1423 wprintw(minibuf, "%s", ministate.buf + skip);
1426 if (ministate.curmesg != NULL) {
1428 wprintw(minibuf, " [%s]", ministate.curmesg);
1430 wprintw(minibuf, "%s", ministate.curmesg);
1433 if (!in_minibuffer && ministate.curmesg == NULL)
1434 wprintw(minibuf, "%s", keybuf);
1436 /* If nothing else, show the URL at point */
1437 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1438 tab = current_tab();
1439 if (tab->s.current_line != NULL &&
1440 tab->s.current_line->parent->type == LINE_LINK)
1441 wprintw(minibuf, "%s",
1442 tab->s.current_line->parent->alt);
1446 wmove(minibuf, 0, off_x + ministate.off - skip);
1450 redraw_tab(struct tab *tab)
1454 redraw_modeline(tab);
1455 redraw_minibuffer();
1460 if (in_minibuffer) {
1470 redraw_body(struct tab *tab)
1477 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1478 if (TAILQ_EMPTY(&tab->s.head))
1482 vl = nth_line(tab, tab->s.line_off);
1483 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1484 wmove(body, line, 0);
1487 if (line == body_lines)
1491 wmove(body, tab->s.curs_y, tab->s.curs_x);
1495 vmessage(const char *fmt, va_list ap)
1497 if (evtimer_pending(&clminibufev, NULL))
1498 evtimer_del(&clminibufev);
1499 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1500 evtimer_add(&clminibufev, &clminibufev_timer);
1502 free(ministate.curmesg);
1504 /* TODO: what to do if the allocation fails here? */
1505 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1506 ministate.curmesg = NULL;
1508 redraw_minibuffer();
1509 if (in_minibuffer) {
1519 message(const char *fmt, ...)
1529 start_loading_anim(struct tab *tab)
1531 if (tab->s.loading_anim)
1533 tab->s.loading_anim = 1;
1534 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1535 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1539 update_loading_anim(int fd, short ev, void *d)
1541 struct tab *tab = d;
1543 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1545 if (tab->flags & TAB_CURRENT) {
1546 redraw_modeline(tab);
1553 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1557 stop_loading_anim(struct tab *tab)
1559 if (!tab->s.loading_anim)
1561 evtimer_del(&tab->s.loadingev);
1562 tab->s.loading_anim = 0;
1563 tab->s.loading_anim_step = 0;
1565 if (!(tab->flags & TAB_CURRENT))
1568 redraw_modeline(tab);
1577 load_url_in_tab(struct tab *tab, const char *url)
1580 message("Loading %s...", url);
1581 start_loading_anim(tab);
1590 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1591 void (*abortfn)(void), struct histhead *hist)
1594 base_map = &minibuffer_map;
1595 current_map = &minibuffer_map;
1597 base_map->unhandled_input = self_insert_fn;
1599 ministate.donefn = donefn;
1600 ministate.abortfn = abortfn;
1601 memset(ministate.buf, 0, sizeof(ministate.buf));
1604 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1606 ministate.history = hist;
1607 ministate.hist_cur = NULL;
1608 ministate.hist_off = 0;
1612 exit_minibuffer(void)
1617 base_map = &global_map;
1618 current_map = &global_map;
1622 switch_to_tab(struct tab *tab)
1626 TAILQ_FOREACH(t, &tabshead, tabs) {
1627 t->flags &= ~TAB_CURRENT;
1630 tab->flags |= TAB_CURRENT;
1634 new_tab(const char *url)
1638 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1643 TAILQ_INIT(&tab->hist.head);
1645 TAILQ_INIT(&tab->s.head);
1647 tab->id = tab_counter++;
1650 if (TAILQ_EMPTY(&tabshead))
1651 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1653 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1655 load_url_in_tab(tab, url);
1662 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1666 ui_init(int argc, char * const *argv)
1668 const char *url = NEW_TAB_URL;
1671 while ((ch = getopt(argc, argv, "")) != -1) {
1684 setlocale(LC_ALL, "");
1686 TAILQ_INIT(&global_map.m);
1687 global_map.unhandled_input = global_key_unbound;
1689 TAILQ_INIT(&minibuffer_map.m);
1691 TAILQ_INIT(&eecmd_history.head);
1692 TAILQ_INIT(&ir_history.head);
1693 TAILQ_INIT(&lu_history.head);
1695 base_map = &global_map;
1696 current_map = &global_map;
1697 load_default_keys();
1704 intrflush(stdscr, FALSE);
1706 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1708 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1710 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1712 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1715 body_lines = LINES-3;
1719 scrollok(body, TRUE);
1721 /* non-blocking input */
1724 mvwprintw(body, 0, 0, "");
1726 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1727 event_add(&stdioev, NULL);
1729 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1730 signal_add(&winchev, NULL);
1738 ui_on_tab_loaded(struct tab *tab)
1740 stop_loading_anim(tab);
1741 message("Loaded %s", tab->hist_cur->h);
1752 ui_on_tab_refresh(struct tab *tab)
1755 if (tab->flags & TAB_CURRENT)
1760 ui_require_input(struct tab *tab, int hide)
1762 /* TODO: hard-switching to another tab is ugly */
1765 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1767 strlcpy(ministate.prompt, "Input required: ",
1768 sizeof(ministate.prompt));
1773 ui_notify(const char *fmt, ...)