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.line_x = MIN(tab->s.line_x, strlen(vl->line));
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 tab->s.curs_x = tab->s.line_x + strlen(prfx);
381 wmove(body, tab->s.curs_y, tab->s.curs_x);
385 cmd_previous_line(struct tab *tab)
389 if (tab->s.current_line == NULL
390 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
393 if (--tab->s.curs_y < 0) {
395 cmd_scroll_line_up(tab);
399 tab->s.current_line = vl;
404 cmd_next_line(struct tab *tab)
408 if (tab->s.current_line == NULL
409 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
412 if (++tab->s.curs_y > body_lines-1) {
413 tab->s.curs_y = body_lines-1;
414 cmd_scroll_line_down(tab);
418 tab->s.current_line = vl;
423 cmd_backward_char(struct tab *tab)
425 if (tab->s.line_x != 0)
431 cmd_forward_char(struct tab *tab)
438 cmd_backward_paragraph(struct tab *tab)
441 if (tab->s.current_line == NULL ||
442 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
443 message("No previous paragraph");
446 cmd_previous_line(tab);
447 } while (tab->s.current_line->line != NULL ||
448 tab->s.current_line->parent->type != LINE_TEXT);
452 cmd_forward_paragraph(struct tab *tab)
455 if (tab->s.current_line == NULL ||
456 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
457 message("No next paragraph");
461 } while (tab->s.current_line->line != NULL ||
462 tab->s.current_line->parent->type != LINE_TEXT);
466 cmd_move_beginning_of_line(struct tab *tab)
473 cmd_move_end_of_line(struct tab *tab)
477 vl = tab->s.current_line;
478 if (vl->line == NULL)
480 tab->s.line_x = body_cols;
485 cmd_redraw(struct tab *tab)
487 handle_resize(0, 0, NULL);
491 cmd_scroll_line_up(struct tab *tab)
495 if (tab->s.line_off == 0)
498 vl = nth_line(tab, --tab->s.line_off);
503 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
507 cmd_scroll_line_down(struct tab *tab)
511 vl = tab->s.current_line;
512 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
514 tab->s.current_line = vl;
519 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
522 vl = nth_line(tab, tab->s.line_off + body_lines-1);
523 wmove(body, body_lines-1, 0);
528 cmd_scroll_up(struct tab *tab)
534 for (; off > 0; --off)
535 cmd_scroll_line_up(tab);
539 cmd_scroll_down(struct tab *tab)
545 for (; off > 0; --off)
546 cmd_scroll_line_down(tab);
550 cmd_beginning_of_buffer(struct tab *tab)
552 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
561 cmd_end_of_buffer(struct tab *tab)
565 off = tab->s.line_max - body_lines;
568 tab->s.line_off = off;
569 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
571 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
572 tab->s.line_x = body_cols;
578 cmd_kill_telescope(struct tab *tab)
584 cmd_push_button(struct tab *tab)
589 nth = tab->s.line_off + tab->s.curs_y;
590 if (nth >= tab->s.line_max)
592 vl = nth_line(tab, nth);
593 if (vl->parent->type != LINE_LINK)
596 load_url_in_tab(tab, vl->parent->alt);
600 cmd_push_button_new_tab(struct tab *tab)
605 nth = tab->s.line_off + tab->s.curs_y;
606 if (nth > tab->s.line_max)
608 vl = nth_line(tab, nth);
609 if (vl->parent->type != LINE_LINK)
612 new_tab(vl->parent->alt);
616 cmd_previous_button(struct tab *tab)
619 if (tab->s.current_line == NULL ||
620 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
621 message("No previous link");
624 cmd_previous_line(tab);
625 } while (tab->s.current_line->parent->type != LINE_LINK);
629 cmd_next_button(struct tab *tab)
632 if (tab->s.current_line == NULL ||
633 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
634 message("No next link");
638 } while (tab->s.current_line->parent->type != LINE_LINK);
642 cmd_previous_page(struct tab *tab)
644 if (!load_previous_page(tab))
645 message("No previous page");
647 start_loading_anim(tab);
651 cmd_next_page(struct tab *tab)
653 if (!load_next_page(tab))
654 message("No next page");
656 start_loading_anim(tab);
660 cmd_clear_minibuf(struct tab *tab)
662 handle_clear_minibuf(0, 0, NULL);
666 cmd_execute_extended_command(struct tab *tab)
670 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
673 len = sizeof(ministate.prompt);
674 strlcpy(ministate.prompt, "", len);
677 strlcat(ministate.prompt, "M-", len);
679 strlcat(ministate.prompt, keyname(thiskey.key), len);
680 strlcat(ministate.prompt, " ", len);
684 cmd_tab_close(struct tab *tab)
688 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
689 TAILQ_NEXT(tab, tabs) == NULL) {
690 message("Can't close the only tab.");
694 if (evtimer_pending(&tab->s.loadingev, NULL))
695 evtimer_del(&tab->s.loadingev);
699 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
700 t = TAILQ_NEXT(tab, tabs);
701 TAILQ_REMOVE(&tabshead, tab, tabs);
708 cmd_tab_close_other(struct tab *tab)
712 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
713 if (t->flags & TAB_CURRENT)
717 TAILQ_REMOVE(&tabshead, t, tabs);
723 cmd_tab_new(struct tab *tab)
725 new_tab(NEW_TAB_URL);
729 cmd_tab_next(struct tab *tab)
733 tab->flags &= ~TAB_CURRENT;
735 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
736 t = TAILQ_FIRST(&tabshead);
737 t->flags |= TAB_CURRENT;
741 cmd_tab_previous(struct tab *tab)
745 tab->flags &= ~TAB_CURRENT;
747 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
748 t = TAILQ_LAST(&tabshead, tabshead);
749 t->flags |= TAB_CURRENT;
753 cmd_load_url(struct tab *tab)
755 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
757 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
761 cmd_load_current_url(struct tab *tab)
763 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
765 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
766 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
767 ministate.off = strlen(tab->hist_cur->h);
768 ministate.len = ministate.off;
772 cmd_bookmark_page(struct tab *tab)
774 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
775 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
776 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
777 ministate.off = strlen(tab->hist_cur->h);
778 ministate.len = ministate.off;
782 cmd_goto_bookmarks(struct tab *tab)
784 load_url_in_tab(tab, "about:bookmarks");
788 global_key_unbound(void)
790 message("%s is undefined", keybuf);
794 cmd_mini_delete_char(struct tab *tab)
796 minibuffer_taint_hist();
798 if (ministate.len == 0 || ministate.off == ministate.len)
801 memmove(&ministate.buf[ministate.off],
802 &ministate.buf[ministate.off+1],
803 ministate.len - ministate.off + 1);
808 cmd_mini_delete_backward_char(struct tab *tab)
810 minibuffer_taint_hist();
812 if (ministate.len == 0 || ministate.off == 0)
815 memmove(&ministate.buf[ministate.off-1],
816 &ministate.buf[ministate.off],
817 ministate.len - ministate.off + 1);
823 cmd_mini_forward_char(struct tab *tab)
825 if (ministate.off == ministate.len)
831 cmd_mini_backward_char(struct tab *tab)
833 if (ministate.off == 0)
839 cmd_mini_move_end_of_line(struct tab *tab)
841 ministate.off = ministate.len;
845 cmd_mini_move_beginning_of_line(struct tab *tab)
851 cmd_mini_kill_line(struct tab *tab)
853 minibuffer_taint_hist();
855 if (ministate.off == ministate.len)
857 ministate.buf[ministate.off] = '\0';
858 ministate.len -= ministate.off;
862 cmd_mini_abort(struct tab *tab)
868 cmd_mini_complete_and_exit(struct tab *tab)
870 minibuffer_taint_hist();
875 cmd_mini_previous_history_element(struct tab *tab)
877 if (ministate.history == NULL) {
878 message("No history");
882 if (ministate.hist_cur == NULL ||
883 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
884 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
885 ministate.hist_off = ministate.history->len - 1;
886 if (ministate.hist_cur == NULL)
887 message("No prev item");
889 ministate.hist_off--;
892 if (ministate.hist_cur != NULL) {
894 ministate.len = strlen(ministate.hist_cur->h);
899 cmd_mini_next_history_element(struct tab *tab)
901 if (ministate.history == NULL) {
902 message("No history");
906 if (ministate.hist_cur == NULL ||
907 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
908 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
909 ministate.hist_off = 0;
910 if (ministate.hist_cur == NULL)
911 message("No next item");
913 ministate.hist_off++;
916 if (ministate.hist_cur != NULL) {
918 ministate.len = strlen(ministate.hist_cur->h);
923 minibuffer_hist_save_entry(void)
927 if (ministate.history == NULL)
930 if ((hist = calloc(1, sizeof(*hist))) == NULL)
933 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
935 if (TAILQ_EMPTY(&ministate.history->head))
936 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
938 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
939 ministate.history->len++;
943 * taint the minibuffer cache: if we're currently showing a history
944 * element, copy that to the current buf and reset the "history
948 minibuffer_taint_hist(void)
950 if (ministate.hist_cur == NULL)
953 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
954 ministate.hist_cur = NULL;
958 minibuffer_self_insert(void)
960 minibuffer_taint_hist();
962 if (ministate.len == sizeof(ministate.buf) -1)
965 /* TODO: utf8 handling! */
967 memmove(&ministate.buf[ministate.off+1],
968 &ministate.buf[ministate.off],
969 ministate.len - ministate.off + 1);
970 ministate.buf[ministate.off] = thiskey.key;
976 eecmd_self_insert(void)
978 if (thiskey.meta || isspace(thiskey.key) ||
979 !isgraph(thiskey.key)) {
980 global_key_unbound();
984 minibuffer_self_insert();
991 minibuffer_hist_save_entry();
992 message("TODO: try to execute %s", ministate.buf);
998 minibuffer_self_insert();
1004 char buf[1025] = {0};
1008 tab = current_tab();
1011 minibuffer_hist_save_entry();
1013 /* a bit ugly but... */
1014 memcpy(&url, &tab->url, sizeof(tab->url));
1015 url_set_query(&url, ministate.buf);
1016 url_unparse(&url, buf, sizeof(buf));
1017 load_url_in_tab(tab, buf);
1021 lu_self_insert(void)
1023 if (thiskey.meta || isspace(thiskey.key) ||
1024 !isgraph(thiskey.key)) {
1025 global_key_unbound();
1029 minibuffer_self_insert();
1036 minibuffer_hist_save_entry();
1037 load_url_in_tab(current_tab(), ministate.buf);
1044 if (*ministate.buf != '\0')
1045 add_to_bookmarks(ministate.buf);
1050 static struct vline *
1051 nth_line(struct tab *tab, size_t n)
1057 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1072 TAILQ_FOREACH(t, &tabshead, tabs) {
1073 if (t->flags & TAB_CURRENT)
1082 dispatch_stdio(int fd, short ev, void *d)
1085 const char *keyname;
1088 thiskey.key = wgetch(body);
1089 if (thiskey.key == ERR)
1091 if (thiskey.key == 27) {
1092 /* TODO: make escape-time customizable */
1095 thiskey.key = wgetch(body);
1096 if (thiskey.key == ERR || thiskey.key == 27) {
1103 if (keybuf[0] != '\0')
1104 strlcat(keybuf, " ", sizeof(keybuf));
1106 strlcat(keybuf, "M-", sizeof(keybuf));
1107 if ((keyname = unkbd(thiskey.key)) != NULL)
1108 strlcat(keybuf, keyname, sizeof(keybuf));
1110 tmp[0] = thiskey.key;
1111 strlcat(keybuf, tmp, sizeof(keybuf));
1114 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1115 if (k->meta == thiskey.meta &&
1116 k->key == thiskey.key) {
1118 current_map = &k->map;
1120 current_map = base_map;
1121 strlcpy(keybuf, "", sizeof(keybuf));
1122 k->fn(current_tab());
1128 if (current_map->unhandled_input != NULL)
1129 current_map->unhandled_input();
1131 global_key_unbound();
1134 strlcpy(keybuf, "", sizeof(keybuf));
1135 current_map = base_map;
1138 redraw_tab(current_tab());
1142 handle_clear_minibuf(int fd, short ev, void *d)
1144 free(ministate.curmesg);
1145 ministate.curmesg = NULL;
1147 redraw_minibuffer();
1148 if (in_minibuffer) {
1158 handle_resize(int sig, short ev, void *d)
1166 /* move and resize the windows, in reverse order! */
1168 mvwin(minibuf, LINES-1, 0);
1169 wresize(minibuf, 1, COLS);
1171 mvwin(modeline, LINES-2, 0);
1172 wresize(modeline, 1, COLS);
1174 wresize(body, LINES-3, COLS);
1175 body_lines = LINES-3;
1178 wresize(tabline, 1, COLS);
1180 tab = current_tab();
1187 wrap_page(struct tab *tab)
1190 const struct line *orig;
1194 orig = tab->s.current_line == NULL
1196 : tab->s.current_line->parent;
1197 tab->s.current_line = NULL;
1200 tab->s.line_off = 0;
1204 TAILQ_FOREACH(l, &tab->page.head, lines) {
1205 prfx = line_prefixes[l->type].prfx1;
1214 case LINE_PRE_START:
1216 wrap_text(tab, prfx, l, body_cols);
1218 case LINE_PRE_CONTENT:
1219 hardwrap_text(tab, l, body_cols);
1223 if (orig == l && tab->s.current_line == NULL) {
1224 tab->s.line_off = tab->s.line_max-1;
1225 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1228 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1229 if (vl == NULL || vl->parent != orig)
1231 tab->s.current_line = vl;
1237 if (tab->s.current_line == NULL)
1238 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1244 print_vline(struct vline *vl)
1246 const char *text = vl->line;
1248 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1249 int text_face = line_faces[vl->parent->type].text_prop;
1252 prfx = line_prefixes[vl->parent->type].prfx1;
1254 prfx = line_prefixes[vl->parent->type].prfx2;
1259 wattron(body, prefix_face);
1260 wprintw(body, "%s", prfx);
1261 wattroff(body, prefix_face);
1263 wattron(body, text_face);
1264 wprintw(body, "%s", text);
1265 wattroff(body, text_face);
1269 redraw_tabline(void)
1273 int current, x, y, truncated;
1279 TAILQ_FOREACH(tab, &tabshead, tabs) {
1280 x += sizeof(buf) + 1;
1282 if (tab->flags & TAB_CURRENT)
1291 wattron(tabline, tab_face.background);
1292 wprintw(tabline, toskip == 0 ? " " : "<");
1293 wattroff(tabline, tab_face.background);
1296 TAILQ_FOREACH(tab, &tabshead, tabs) {
1304 getyx(tabline, y, x);
1305 if (x + sizeof(buf)+2 >= (size_t)COLS)
1308 current = tab->flags & TAB_CURRENT;
1310 if (*(title = tab->page.title) == '\0')
1311 title = tab->hist_cur->h;
1313 strlcpy(buf, " ", sizeof(buf));
1314 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1315 /* truncation happens */
1316 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1318 /* pad with spaces */
1319 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1324 wattron(tabline, tab_face.current_tab);
1326 wattron(tabline, tab_face.tab);
1328 wprintw(tabline, "%s", buf);
1329 if (TAILQ_NEXT(tab, tabs) != NULL)
1330 wprintw(tabline, " ");
1333 wattroff(tabline, tab_face.current_tab);
1335 wattroff(tabline, tab_face.tab);
1338 wattron(tabline, tab_face.background);
1339 for (; x < COLS; ++x)
1340 waddch(tabline, ' ');
1342 mvwprintw(tabline, 0, COLS-1, ">");
1346 trust_status_char(enum trust_state ts)
1349 case TS_UNKNOWN: return 'u';
1350 case TS_UNTRUSTED: return '!';
1351 case TS_TRUSTED: return 'v';
1352 case TS_VERIFIED: return 'V';
1357 redraw_modeline(struct tab *tab)
1360 int x, y, max_x, max_y;
1361 const char *mode = tab->page.name;
1362 const char *spin = "-\\|/";
1365 wattron(modeline, A_REVERSE);
1366 wmove(modeline, 0, 0);
1368 wprintw(modeline, "-%c%c %s ",
1369 spin[tab->s.loading_anim_step],
1370 trust_status_char(tab->trust),
1371 mode == NULL ? "(none)" : mode);
1373 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1375 if (tab->s.line_max <= (size_t)body_lines)
1376 wprintw(modeline, "All ");
1377 else if (tab->s.line_off == 0)
1378 wprintw(modeline, "Top ");
1379 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1380 wprintw(modeline, "Bottom ");
1382 wprintw(modeline, "%.0f%% ", pct);
1384 wprintw(modeline, "%d/%d %s ",
1385 tab->s.line_off + tab->s.curs_y,
1389 getyx(modeline, y, x);
1390 getmaxyx(modeline, max_y, max_x);
1395 for (; x < max_x; ++x)
1396 waddstr(modeline, "-");
1400 redraw_minibuffer(void)
1402 size_t skip = 0, off_x = 0, off_y = 0;
1406 if (in_minibuffer) {
1407 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1408 if (ministate.hist_cur != NULL)
1409 wprintw(minibuf, "(%zu/%zu) ",
1410 ministate.hist_off + 1,
1411 ministate.history->len);
1413 getyx(minibuf, off_y, off_x);
1415 while (ministate.off - skip > (size_t)COLS / 2) {
1416 skip += MIN(ministate.off/4, 1);
1419 if (ministate.hist_cur != NULL)
1420 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1422 wprintw(minibuf, "%s", ministate.buf + skip);
1425 if (ministate.curmesg != NULL) {
1427 wprintw(minibuf, " [%s]", ministate.curmesg);
1429 wprintw(minibuf, "%s", ministate.curmesg);
1432 if (!in_minibuffer && ministate.curmesg == NULL)
1433 wprintw(minibuf, "%s", keybuf);
1435 /* If nothing else, show the URL at point */
1436 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1437 tab = current_tab();
1438 if (tab->s.current_line != NULL &&
1439 tab->s.current_line->parent->type == LINE_LINK)
1440 wprintw(minibuf, "%s",
1441 tab->s.current_line->parent->alt);
1445 wmove(minibuf, 0, off_x + ministate.off - skip);
1449 redraw_tab(struct tab *tab)
1453 redraw_modeline(tab);
1454 redraw_minibuffer();
1456 restore_cursor(tab);
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)
1493 vmessage(const char *fmt, va_list ap)
1495 if (evtimer_pending(&clminibufev, NULL))
1496 evtimer_del(&clminibufev);
1497 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1498 evtimer_add(&clminibufev, &clminibufev_timer);
1500 free(ministate.curmesg);
1502 /* TODO: what to do if the allocation fails here? */
1503 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1504 ministate.curmesg = NULL;
1506 redraw_minibuffer();
1507 if (in_minibuffer) {
1517 message(const char *fmt, ...)
1527 start_loading_anim(struct tab *tab)
1529 if (tab->s.loading_anim)
1531 tab->s.loading_anim = 1;
1532 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1533 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1537 update_loading_anim(int fd, short ev, void *d)
1539 struct tab *tab = d;
1541 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1543 if (tab->flags & TAB_CURRENT) {
1544 redraw_modeline(tab);
1551 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1555 stop_loading_anim(struct tab *tab)
1557 if (!tab->s.loading_anim)
1559 evtimer_del(&tab->s.loadingev);
1560 tab->s.loading_anim = 0;
1561 tab->s.loading_anim_step = 0;
1563 if (!(tab->flags & TAB_CURRENT))
1566 redraw_modeline(tab);
1575 load_url_in_tab(struct tab *tab, const char *url)
1578 message("Loading %s...", url);
1579 start_loading_anim(tab);
1588 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1589 void (*abortfn)(void), struct histhead *hist)
1592 base_map = &minibuffer_map;
1593 current_map = &minibuffer_map;
1595 base_map->unhandled_input = self_insert_fn;
1597 ministate.donefn = donefn;
1598 ministate.abortfn = abortfn;
1599 memset(ministate.buf, 0, sizeof(ministate.buf));
1602 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1604 ministate.history = hist;
1605 ministate.hist_cur = NULL;
1606 ministate.hist_off = 0;
1610 exit_minibuffer(void)
1615 base_map = &global_map;
1616 current_map = &global_map;
1620 switch_to_tab(struct tab *tab)
1624 TAILQ_FOREACH(t, &tabshead, tabs) {
1625 t->flags &= ~TAB_CURRENT;
1628 tab->flags |= TAB_CURRENT;
1632 new_tab(const char *url)
1636 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1641 TAILQ_INIT(&tab->hist.head);
1643 TAILQ_INIT(&tab->s.head);
1645 tab->id = tab_counter++;
1648 if (TAILQ_EMPTY(&tabshead))
1649 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1651 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1653 load_url_in_tab(tab, url);
1660 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1664 ui_init(int argc, const char **argv)
1666 const char *url = NEW_TAB_URL;
1669 while ((ch = getopt(argc, argv, "")) != -1) {
1682 setlocale(LC_ALL, "");
1684 TAILQ_INIT(&global_map.m);
1685 global_map.unhandled_input = global_key_unbound;
1687 TAILQ_INIT(&minibuffer_map.m);
1689 TAILQ_INIT(&eecmd_history.head);
1690 TAILQ_INIT(&ir_history.head);
1691 TAILQ_INIT(&lu_history.head);
1693 base_map = &global_map;
1694 current_map = &global_map;
1695 load_default_keys();
1702 intrflush(stdscr, FALSE);
1704 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1706 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1708 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1710 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1713 body_lines = LINES-3;
1717 scrollok(body, TRUE);
1719 /* non-blocking input */
1722 mvwprintw(body, 0, 0, "");
1724 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1725 event_add(&stdioev, NULL);
1727 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1728 signal_add(&winchev, NULL);
1736 ui_on_tab_loaded(struct tab *tab)
1738 stop_loading_anim(tab);
1739 message("Loaded %s", tab->hist_cur->h);
1750 ui_on_tab_refresh(struct tab *tab)
1753 if (tab->flags & TAB_CURRENT)
1758 ui_require_input(struct tab *tab, int hide)
1760 /* TODO: hard-switching to another tab is ugly */
1763 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1765 strlcpy(ministate.prompt, "Input required: ",
1766 sizeof(ministate.prompt));
1771 ui_notify(const char *fmt, ...)