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 int readkey(void);
121 static void dispatch_stdio(int, short, void*);
122 static void handle_clear_minibuf(int, short, void*);
123 static void handle_resize(int, short, void*);
124 static int wrap_page(struct tab*);
125 static void print_vline(struct vline*);
126 static void redraw_tabline(void);
127 static void redraw_body(struct tab*);
128 static void redraw_modeline(struct tab*);
129 static void redraw_minibuffer(void);
130 static void redraw_tab(struct tab*);
131 static void vmessage(const char*, va_list);
132 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
133 static void start_loading_anim(struct tab*);
134 static void update_loading_anim(int, short, void*);
135 static void stop_loading_anim(struct tab*);
136 static void load_url_in_tab(struct tab*, const char*);
137 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
138 static void exit_minibuffer(void);
139 static void switch_to_tab(struct tab*);
140 static struct tab *new_tab(const char*);
141 static void usage(void);
143 static struct { short meta; int key; uint32_t cp; } thiskey;
145 static WINDOW *tabline, *body, *modeline, *minibuf;
146 static int body_lines, body_cols;
148 static struct event clminibufev;
149 static struct timeval clminibufev_timer = { 5, 0 };
150 static struct timeval loadingev_timer = { 0, 250000 };
152 static uint32_t tab_counter;
154 static char keybuf[64];
156 struct kmap global_map,
161 static struct histhead eecmd_history,
165 static int in_minibuffer;
173 void (*donefn)(void);
174 void (*abortfn)(void);
176 struct histhead *history;
177 struct hist *hist_cur;
184 } line_prefixes[] = {
185 [LINE_TEXT] = { "", "" },
186 [LINE_LINK] = { "=> ", " " },
187 [LINE_TITLE_1] = { "# ", " " },
188 [LINE_TITLE_2] = { "## ", " " },
189 [LINE_TITLE_3] = { "### ", " " },
190 [LINE_ITEM] = { "* ", " " },
191 [LINE_QUOTE] = { "> ", " " },
192 [LINE_PRE_START] = { "```", " " },
193 [LINE_PRE_CONTENT] = { "", "" },
194 [LINE_PRE_END] = { "```", "```" },
197 static struct line_face {
201 [LINE_TEXT] = { 0, 0 },
202 [LINE_LINK] = { 0, A_UNDERLINE },
203 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
204 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
205 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
206 [LINE_ITEM] = { 0, 0 },
207 [LINE_QUOTE] = { 0, A_DIM },
208 [LINE_PRE_START] = { 0, 0 },
209 [LINE_PRE_CONTENT] = { 0, 0 },
210 [LINE_PRE_END] = { 0, 0 },
213 static struct tab_face {
214 int background, tab, current_tab;
216 A_REVERSE, A_REVERSE, A_NORMAL
220 empty_vlist(struct tab *tab)
222 struct vline *vl, *t;
224 tab->s.current_line = NULL;
227 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
228 TAILQ_REMOVE(&tab->s.head, vl, vlines);
235 global_set_key(const char *key, void (*fn)(struct tab*))
237 if (!kmap_define_key(&global_map, key, fn))
242 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
244 if (!kmap_define_key(&minibuffer_map, key, fn))
249 load_default_keys(void)
251 /* === global map === */
254 global_set_key("C-p", cmd_previous_line);
255 global_set_key("C-n", cmd_next_line);
256 global_set_key("C-f", cmd_forward_char);
257 global_set_key("C-b", cmd_backward_char);
258 global_set_key("M-{", cmd_backward_paragraph);
259 global_set_key("M-}", cmd_forward_paragraph);
260 global_set_key("C-a", cmd_move_beginning_of_line);
261 global_set_key("C-e", cmd_move_end_of_line);
263 global_set_key("M-v", cmd_scroll_up);
264 global_set_key("C-v", cmd_scroll_down);
265 global_set_key("M-space", cmd_scroll_up);
266 global_set_key("space", cmd_scroll_down);
268 global_set_key("C-x C-c", cmd_kill_telescope);
270 global_set_key("C-g", cmd_clear_minibuf);
272 global_set_key("M-x", cmd_execute_extended_command);
273 global_set_key("C-x C-f", cmd_load_url);
274 global_set_key("C-x M-f", cmd_load_current_url);
276 global_set_key("C-x t 0", cmd_tab_close);
277 global_set_key("C-x t 1", cmd_tab_close_other);
278 global_set_key("C-x t 2", cmd_tab_new);
279 global_set_key("C-x t o", cmd_tab_next);
280 global_set_key("C-x t O", cmd_tab_previous);
282 global_set_key("M-<", cmd_beginning_of_buffer);
283 global_set_key("M->", cmd_end_of_buffer);
285 global_set_key("C-M-b", cmd_previous_page);
286 global_set_key("C-M-f", cmd_next_page);
288 global_set_key("<f7> a", cmd_bookmark_page);
289 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
292 global_set_key("k", cmd_previous_line);
293 global_set_key("j", cmd_next_line);
294 global_set_key("l", cmd_forward_char);
295 global_set_key("h", cmd_backward_char);
296 global_set_key("{", cmd_backward_paragraph);
297 global_set_key("}", cmd_forward_paragraph);
298 global_set_key("^", cmd_move_beginning_of_line);
299 global_set_key("$", cmd_move_end_of_line);
301 global_set_key("K", cmd_scroll_line_up);
302 global_set_key("J", cmd_scroll_line_down);
304 global_set_key("g D", cmd_tab_close);
305 global_set_key("g N", cmd_tab_new);
306 global_set_key("g t", cmd_tab_next);
307 global_set_key("g T", cmd_tab_previous);
309 global_set_key("g g", cmd_beginning_of_buffer);
310 global_set_key("G", cmd_end_of_buffer);
312 global_set_key("H", cmd_previous_page);
313 global_set_key("L", cmd_next_page);
316 global_set_key("q", cmd_kill_telescope);
318 global_set_key("esc", cmd_clear_minibuf);
320 global_set_key(":", cmd_execute_extended_command);
323 global_set_key("<up>", cmd_previous_line);
324 global_set_key("<down>", cmd_next_line);
325 global_set_key("<right>", cmd_forward_char);
326 global_set_key("<left>", cmd_backward_char);
327 global_set_key("<prior>", cmd_scroll_up);
328 global_set_key("<next>", cmd_scroll_down);
330 global_set_key("M-<left>", cmd_previous_page);
331 global_set_key("M-<right>", cmd_next_page);
333 /* "ncurses standard" */
334 global_set_key("C-l", cmd_redraw);
337 global_set_key("C-m", cmd_push_button);
338 global_set_key("M-enter", cmd_push_button_new_tab);
339 global_set_key("M-tab", cmd_previous_button);
340 global_set_key("tab", cmd_next_button);
342 /* === minibuffer map === */
343 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
344 minibuffer_set_key("C-g", cmd_mini_abort);
345 minibuffer_set_key("esc", cmd_mini_abort);
346 minibuffer_set_key("C-d", cmd_mini_delete_char);
347 minibuffer_set_key("del", cmd_mini_delete_backward_char);
349 minibuffer_set_key("C-f", cmd_mini_forward_char);
350 minibuffer_set_key("C-b", cmd_mini_backward_char);
351 minibuffer_set_key("<right>", cmd_mini_forward_char);
352 minibuffer_set_key("<left>", cmd_mini_backward_char);
353 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
354 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
355 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
356 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
357 minibuffer_set_key("C-k", cmd_mini_kill_line);
359 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
360 minibuffer_set_key("M-n", cmd_mini_next_history_element);
361 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
362 minibuffer_set_key("<down>", cmd_mini_next_history_element);
366 restore_cursor(struct tab *tab)
371 vl =tab->s.current_line;
372 if (vl == NULL || vl->line == NULL)
373 tab->s.curs_x = tab->s.line_x = 0;
375 tab->s.curs_x = utf8_snwidth(vl->line, tab->s.line_x);
378 prfx = line_prefixes[vl->parent->type].prfx1;
379 tab->s.curs_x += utf8_swidth(prfx);
384 cmd_previous_line(struct tab *tab)
388 if (tab->s.current_line == NULL
389 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
392 if (--tab->s.curs_y < 0) {
394 cmd_scroll_line_up(tab);
398 tab->s.current_line = vl;
403 cmd_next_line(struct tab *tab)
407 if (tab->s.current_line == NULL
408 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
411 if (++tab->s.curs_y > body_lines-1) {
412 tab->s.curs_y = body_lines-1;
413 cmd_scroll_line_down(tab);
417 tab->s.current_line = vl;
422 cmd_backward_char(struct tab *tab)
424 if (tab->s.line_x != 0)
430 cmd_forward_char(struct tab *tab)
437 cmd_backward_paragraph(struct tab *tab)
440 if (tab->s.current_line == NULL ||
441 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
442 message("No previous paragraph");
445 cmd_previous_line(tab);
446 } while (tab->s.current_line->line != NULL ||
447 tab->s.current_line->parent->type != LINE_TEXT);
451 cmd_forward_paragraph(struct tab *tab)
454 if (tab->s.current_line == NULL ||
455 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
456 message("No next paragraph");
460 } while (tab->s.current_line->line != NULL ||
461 tab->s.current_line->parent->type != LINE_TEXT);
465 cmd_move_beginning_of_line(struct tab *tab)
472 cmd_move_end_of_line(struct tab *tab)
476 vl = tab->s.current_line;
477 if (vl->line == NULL)
479 tab->s.line_x = utf8_cplen(vl->line);
484 cmd_redraw(struct tab *tab)
486 handle_resize(0, 0, NULL);
490 cmd_scroll_line_up(struct tab *tab)
494 if (tab->s.line_off == 0)
497 vl = nth_line(tab, --tab->s.line_off);
502 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);
530 cmd_scroll_up(struct tab *tab)
536 for (; off > 0; --off)
537 cmd_scroll_line_up(tab);
541 cmd_scroll_down(struct tab *tab)
547 for (; off > 0; --off)
548 cmd_scroll_line_down(tab);
552 cmd_beginning_of_buffer(struct tab *tab)
554 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
563 cmd_end_of_buffer(struct tab *tab)
567 off = tab->s.line_max - body_lines;
570 tab->s.line_off = off;
571 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
573 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
574 tab->s.line_x = body_cols;
580 cmd_kill_telescope(struct tab *tab)
586 cmd_push_button(struct tab *tab)
591 nth = tab->s.line_off + tab->s.curs_y;
592 if (nth >= tab->s.line_max)
594 vl = nth_line(tab, nth);
595 if (vl->parent->type != LINE_LINK)
598 load_url_in_tab(tab, vl->parent->alt);
602 cmd_push_button_new_tab(struct tab *tab)
607 nth = tab->s.line_off + tab->s.curs_y;
608 if (nth > tab->s.line_max)
610 vl = nth_line(tab, nth);
611 if (vl->parent->type != LINE_LINK)
614 new_tab(vl->parent->alt);
618 cmd_previous_button(struct tab *tab)
621 if (tab->s.current_line == NULL ||
622 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
623 message("No previous link");
626 cmd_previous_line(tab);
627 } while (tab->s.current_line->parent->type != LINE_LINK);
631 cmd_next_button(struct tab *tab)
634 if (tab->s.current_line == NULL ||
635 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
636 message("No next link");
640 } while (tab->s.current_line->parent->type != LINE_LINK);
644 cmd_previous_page(struct tab *tab)
646 if (!load_previous_page(tab))
647 message("No previous page");
649 start_loading_anim(tab);
653 cmd_next_page(struct tab *tab)
655 if (!load_next_page(tab))
656 message("No next page");
658 start_loading_anim(tab);
662 cmd_clear_minibuf(struct tab *tab)
664 handle_clear_minibuf(0, 0, NULL);
668 cmd_execute_extended_command(struct tab *tab)
672 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
675 len = sizeof(ministate.prompt);
676 strlcpy(ministate.prompt, "", len);
679 strlcat(ministate.prompt, "M-", len);
681 strlcat(ministate.prompt, keyname(thiskey.key), len);
682 strlcat(ministate.prompt, " ", len);
686 cmd_tab_close(struct tab *tab)
690 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
691 TAILQ_NEXT(tab, tabs) == NULL) {
692 message("Can't close the only tab.");
696 if (evtimer_pending(&tab->s.loadingev, NULL))
697 evtimer_del(&tab->s.loadingev);
701 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
702 t = TAILQ_NEXT(tab, tabs);
703 TAILQ_REMOVE(&tabshead, tab, tabs);
710 cmd_tab_close_other(struct tab *tab)
714 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
715 if (t->flags & TAB_CURRENT)
719 TAILQ_REMOVE(&tabshead, t, tabs);
725 cmd_tab_new(struct tab *tab)
727 new_tab(NEW_TAB_URL);
731 cmd_tab_next(struct tab *tab)
735 tab->flags &= ~TAB_CURRENT;
737 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
738 t = TAILQ_FIRST(&tabshead);
739 t->flags |= TAB_CURRENT;
743 cmd_tab_previous(struct tab *tab)
747 tab->flags &= ~TAB_CURRENT;
749 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
750 t = TAILQ_LAST(&tabshead, tabshead);
751 t->flags |= TAB_CURRENT;
755 cmd_load_url(struct tab *tab)
757 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
759 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
763 cmd_load_current_url(struct tab *tab)
765 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
767 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
768 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
769 ministate.off = strlen(tab->hist_cur->h);
770 ministate.len = ministate.off;
774 cmd_bookmark_page(struct tab *tab)
776 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
777 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
778 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
779 ministate.off = strlen(tab->hist_cur->h);
780 ministate.len = ministate.off;
784 cmd_goto_bookmarks(struct tab *tab)
786 load_url_in_tab(tab, "about:bookmarks");
790 global_key_unbound(void)
792 message("%s is undefined", keybuf);
796 cmd_mini_delete_char(struct tab *tab)
798 minibuffer_taint_hist();
800 if (ministate.len == 0 || ministate.off == ministate.len)
803 memmove(&ministate.buf[ministate.off],
804 &ministate.buf[ministate.off+1],
805 ministate.len - ministate.off + 1);
810 cmd_mini_delete_backward_char(struct tab *tab)
812 minibuffer_taint_hist();
814 if (ministate.len == 0 || ministate.off == 0)
817 memmove(&ministate.buf[ministate.off-1],
818 &ministate.buf[ministate.off],
819 ministate.len - ministate.off + 1);
825 cmd_mini_forward_char(struct tab *tab)
827 if (ministate.off == ministate.len)
833 cmd_mini_backward_char(struct tab *tab)
835 if (ministate.off == 0)
841 cmd_mini_move_end_of_line(struct tab *tab)
843 ministate.off = ministate.len;
847 cmd_mini_move_beginning_of_line(struct tab *tab)
853 cmd_mini_kill_line(struct tab *tab)
855 minibuffer_taint_hist();
857 if (ministate.off == ministate.len)
859 ministate.buf[ministate.off] = '\0';
860 ministate.len -= ministate.off;
864 cmd_mini_abort(struct tab *tab)
870 cmd_mini_complete_and_exit(struct tab *tab)
872 minibuffer_taint_hist();
877 cmd_mini_previous_history_element(struct tab *tab)
879 if (ministate.history == NULL) {
880 message("No history");
884 if (ministate.hist_cur == NULL ||
885 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
886 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
887 ministate.hist_off = ministate.history->len - 1;
888 if (ministate.hist_cur == NULL)
889 message("No prev item");
891 ministate.hist_off--;
894 if (ministate.hist_cur != NULL) {
896 ministate.len = strlen(ministate.hist_cur->h);
901 cmd_mini_next_history_element(struct tab *tab)
903 if (ministate.history == NULL) {
904 message("No history");
908 if (ministate.hist_cur == NULL ||
909 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
910 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
911 ministate.hist_off = 0;
912 if (ministate.hist_cur == NULL)
913 message("No next item");
915 ministate.hist_off++;
918 if (ministate.hist_cur != NULL) {
920 ministate.len = strlen(ministate.hist_cur->h);
925 minibuffer_hist_save_entry(void)
929 if (ministate.history == NULL)
932 if ((hist = calloc(1, sizeof(*hist))) == NULL)
935 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
937 if (TAILQ_EMPTY(&ministate.history->head))
938 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
940 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
941 ministate.history->len++;
945 * taint the minibuffer cache: if we're currently showing a history
946 * element, copy that to the current buf and reset the "history
950 minibuffer_taint_hist(void)
952 if (ministate.hist_cur == NULL)
955 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
956 ministate.hist_cur = NULL;
960 minibuffer_self_insert(void)
962 minibuffer_taint_hist();
964 if (ministate.len == sizeof(ministate.buf) -1)
967 /* TODO: utf8 handling! */
969 memmove(&ministate.buf[ministate.off+1],
970 &ministate.buf[ministate.off],
971 ministate.len - ministate.off + 1);
972 ministate.buf[ministate.off] = thiskey.key;
978 eecmd_self_insert(void)
980 if (thiskey.meta || isspace(thiskey.key) ||
981 !isgraph(thiskey.key)) {
982 global_key_unbound();
986 minibuffer_self_insert();
993 minibuffer_hist_save_entry();
994 message("TODO: try to execute %s", ministate.buf);
1000 minibuffer_self_insert();
1006 char buf[1025] = {0};
1010 tab = current_tab();
1013 minibuffer_hist_save_entry();
1015 /* a bit ugly but... */
1016 memcpy(&url, &tab->url, sizeof(tab->url));
1017 url_set_query(&url, ministate.buf);
1018 url_unparse(&url, buf, sizeof(buf));
1019 load_url_in_tab(tab, buf);
1023 lu_self_insert(void)
1025 if (thiskey.meta || isspace(thiskey.key) ||
1026 !isgraph(thiskey.key)) {
1027 global_key_unbound();
1031 minibuffer_self_insert();
1038 minibuffer_hist_save_entry();
1039 load_url_in_tab(current_tab(), ministate.buf);
1046 if (*ministate.buf != '\0')
1047 add_to_bookmarks(ministate.buf);
1052 static struct vline *
1053 nth_line(struct tab *tab, size_t n)
1059 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1074 TAILQ_FOREACH(t, &tabshead, tabs) {
1075 if (t->flags & TAB_CURRENT)
1088 if ((thiskey.key = wgetch(body)) == ERR)
1091 thiskey.meta = thiskey.key == 27;
1093 thiskey.key = wgetch(body);
1094 if (thiskey.key == ERR || thiskey.key == 27) {
1101 if ((unsigned int)thiskey.key < UINT8_MAX) {
1103 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1105 if ((thiskey.key = wgetch(body)) == ERR) {
1106 message("Error decoding user input");
1116 dispatch_stdio(int fd, short ev, void *d)
1119 const char *keyname;
1125 if (keybuf[0] != '\0')
1126 strlcat(keybuf, " ", sizeof(keybuf));
1128 strlcat(keybuf, "M-", sizeof(keybuf));
1129 if (thiskey.cp != 0) {
1130 utf8_encode(thiskey.cp, tmp);
1131 strlcat(keybuf, tmp, sizeof(keybuf));
1133 if ((keyname = unkbd(thiskey.key)) != NULL)
1134 strlcat(keybuf, keyname, sizeof(keybuf));
1136 tmp[0] = thiskey.key;
1137 strlcat(keybuf, tmp, sizeof(keybuf));
1141 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1142 if (k->meta == thiskey.meta &&
1143 k->key == thiskey.key) {
1145 current_map = &k->map;
1147 current_map = base_map;
1148 strlcpy(keybuf, "", sizeof(keybuf));
1149 k->fn(current_tab());
1155 if (current_map->unhandled_input != NULL)
1156 current_map->unhandled_input();
1158 global_key_unbound();
1161 strlcpy(keybuf, "", sizeof(keybuf));
1162 current_map = base_map;
1165 redraw_tab(current_tab());
1169 handle_clear_minibuf(int fd, short ev, void *d)
1171 free(ministate.curmesg);
1172 ministate.curmesg = NULL;
1174 redraw_minibuffer();
1175 if (in_minibuffer) {
1185 handle_resize(int sig, short ev, void *d)
1193 /* move and resize the windows, in reverse order! */
1195 mvwin(minibuf, LINES-1, 0);
1196 wresize(minibuf, 1, COLS);
1198 mvwin(modeline, LINES-2, 0);
1199 wresize(modeline, 1, COLS);
1201 wresize(body, LINES-3, COLS);
1202 body_lines = LINES-3;
1205 wresize(tabline, 1, COLS);
1207 tab = current_tab();
1214 wrap_page(struct tab *tab)
1217 const struct line *orig;
1221 orig = tab->s.current_line == NULL
1223 : tab->s.current_line->parent;
1224 tab->s.current_line = NULL;
1227 tab->s.line_off = 0;
1231 TAILQ_FOREACH(l, &tab->page.head, lines) {
1232 prfx = line_prefixes[l->type].prfx1;
1241 case LINE_PRE_START:
1243 wrap_text(tab, prfx, l, body_cols);
1245 case LINE_PRE_CONTENT:
1246 hardwrap_text(tab, l, body_cols);
1250 if (orig == l && tab->s.current_line == NULL) {
1251 tab->s.line_off = tab->s.line_max-1;
1252 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1255 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1256 if (vl == NULL || vl->parent != orig)
1258 tab->s.current_line = vl;
1264 if (tab->s.current_line == NULL)
1265 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1271 print_vline(struct vline *vl)
1273 const char *text = vl->line;
1275 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1276 int text_face = line_faces[vl->parent->type].text_prop;
1279 prfx = line_prefixes[vl->parent->type].prfx1;
1281 prfx = line_prefixes[vl->parent->type].prfx2;
1286 wattron(body, prefix_face);
1287 wprintw(body, "%s", prfx);
1288 wattroff(body, prefix_face);
1290 wattron(body, text_face);
1291 wprintw(body, "%s", text);
1292 wattroff(body, text_face);
1296 redraw_tabline(void)
1300 int current, x, y, truncated;
1306 TAILQ_FOREACH(tab, &tabshead, tabs) {
1307 x += sizeof(buf) + 1;
1309 if (tab->flags & TAB_CURRENT)
1318 wattron(tabline, tab_face.background);
1319 wprintw(tabline, toskip == 0 ? " " : "<");
1320 wattroff(tabline, tab_face.background);
1323 TAILQ_FOREACH(tab, &tabshead, tabs) {
1331 getyx(tabline, y, x);
1332 if (x + sizeof(buf)+2 >= (size_t)COLS)
1335 current = tab->flags & TAB_CURRENT;
1337 if (*(title = tab->page.title) == '\0')
1338 title = tab->hist_cur->h;
1340 strlcpy(buf, " ", sizeof(buf));
1341 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1342 /* truncation happens */
1343 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1345 /* pad with spaces */
1346 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1351 wattron(tabline, tab_face.current_tab);
1353 wattron(tabline, tab_face.tab);
1355 wprintw(tabline, "%s", buf);
1356 if (TAILQ_NEXT(tab, tabs) != NULL)
1357 wprintw(tabline, " ");
1360 wattroff(tabline, tab_face.current_tab);
1362 wattroff(tabline, tab_face.tab);
1365 wattron(tabline, tab_face.background);
1366 for (; x < COLS; ++x)
1367 waddch(tabline, ' ');
1369 mvwprintw(tabline, 0, COLS-1, ">");
1373 trust_status_char(enum trust_state ts)
1376 case TS_UNKNOWN: return 'u';
1377 case TS_UNTRUSTED: return '!';
1378 case TS_TRUSTED: return 'v';
1379 case TS_VERIFIED: return 'V';
1384 redraw_modeline(struct tab *tab)
1387 int x, y, max_x, max_y;
1388 const char *mode = tab->page.name;
1389 const char *spin = "-\\|/";
1392 wattron(modeline, A_REVERSE);
1393 wmove(modeline, 0, 0);
1395 wprintw(modeline, "-%c%c %s ",
1396 spin[tab->s.loading_anim_step],
1397 trust_status_char(tab->trust),
1398 mode == NULL ? "(none)" : mode);
1400 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1402 if (tab->s.line_max <= (size_t)body_lines)
1403 wprintw(modeline, "All ");
1404 else if (tab->s.line_off == 0)
1405 wprintw(modeline, "Top ");
1406 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1407 wprintw(modeline, "Bottom ");
1409 wprintw(modeline, "%.0f%% ", pct);
1411 wprintw(modeline, "%d/%d %s ",
1412 tab->s.line_off + tab->s.curs_y,
1416 getyx(modeline, y, x);
1417 getmaxyx(modeline, max_y, max_x);
1422 for (; x < max_x; ++x)
1423 waddstr(modeline, "-");
1427 redraw_minibuffer(void)
1429 size_t skip = 0, off_x = 0, off_y = 0;
1433 if (in_minibuffer) {
1434 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1435 if (ministate.hist_cur != NULL)
1436 wprintw(minibuf, "(%zu/%zu) ",
1437 ministate.hist_off + 1,
1438 ministate.history->len);
1440 getyx(minibuf, off_y, off_x);
1442 while (ministate.off - skip > (size_t)COLS / 2) {
1443 skip += MIN(ministate.off/4, 1);
1446 if (ministate.hist_cur != NULL)
1447 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1449 wprintw(minibuf, "%s", ministate.buf + skip);
1452 if (ministate.curmesg != NULL) {
1454 wprintw(minibuf, " [%s]", ministate.curmesg);
1456 wprintw(minibuf, "%s", ministate.curmesg);
1459 if (!in_minibuffer && ministate.curmesg == NULL)
1460 wprintw(minibuf, "%s", keybuf);
1462 /* If nothing else, show the URL at point */
1463 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1464 tab = current_tab();
1465 if (tab->s.current_line != NULL &&
1466 tab->s.current_line->parent->type == LINE_LINK)
1467 wprintw(minibuf, "%s",
1468 tab->s.current_line->parent->alt);
1472 wmove(minibuf, 0, off_x + ministate.off - skip);
1476 redraw_tab(struct tab *tab)
1480 redraw_modeline(tab);
1481 redraw_minibuffer();
1486 if (in_minibuffer) {
1496 redraw_body(struct tab *tab)
1503 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1504 if (TAILQ_EMPTY(&tab->s.head))
1508 vl = nth_line(tab, tab->s.line_off);
1509 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1510 wmove(body, line, 0);
1513 if (line == body_lines)
1517 wmove(body, tab->s.curs_y, tab->s.curs_x);
1521 vmessage(const char *fmt, va_list ap)
1523 if (evtimer_pending(&clminibufev, NULL))
1524 evtimer_del(&clminibufev);
1525 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1526 evtimer_add(&clminibufev, &clminibufev_timer);
1528 free(ministate.curmesg);
1530 /* TODO: what to do if the allocation fails here? */
1531 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1532 ministate.curmesg = NULL;
1534 redraw_minibuffer();
1535 if (in_minibuffer) {
1545 message(const char *fmt, ...)
1555 start_loading_anim(struct tab *tab)
1557 if (tab->s.loading_anim)
1559 tab->s.loading_anim = 1;
1560 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1561 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1565 update_loading_anim(int fd, short ev, void *d)
1567 struct tab *tab = d;
1569 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1571 if (tab->flags & TAB_CURRENT) {
1572 redraw_modeline(tab);
1579 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1583 stop_loading_anim(struct tab *tab)
1585 if (!tab->s.loading_anim)
1587 evtimer_del(&tab->s.loadingev);
1588 tab->s.loading_anim = 0;
1589 tab->s.loading_anim_step = 0;
1591 if (!(tab->flags & TAB_CURRENT))
1594 redraw_modeline(tab);
1603 load_url_in_tab(struct tab *tab, const char *url)
1606 message("Loading %s...", url);
1607 start_loading_anim(tab);
1616 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1617 void (*abortfn)(void), struct histhead *hist)
1620 base_map = &minibuffer_map;
1621 current_map = &minibuffer_map;
1623 base_map->unhandled_input = self_insert_fn;
1625 ministate.donefn = donefn;
1626 ministate.abortfn = abortfn;
1627 memset(ministate.buf, 0, sizeof(ministate.buf));
1630 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1632 ministate.history = hist;
1633 ministate.hist_cur = NULL;
1634 ministate.hist_off = 0;
1638 exit_minibuffer(void)
1643 base_map = &global_map;
1644 current_map = &global_map;
1648 switch_to_tab(struct tab *tab)
1652 TAILQ_FOREACH(t, &tabshead, tabs) {
1653 t->flags &= ~TAB_CURRENT;
1656 tab->flags |= TAB_CURRENT;
1660 new_tab(const char *url)
1664 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1669 TAILQ_INIT(&tab->hist.head);
1671 TAILQ_INIT(&tab->s.head);
1673 tab->id = tab_counter++;
1676 if (TAILQ_EMPTY(&tabshead))
1677 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1679 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1681 load_url_in_tab(tab, url);
1688 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1692 ui_init(int argc, char * const *argv)
1694 const char *url = NEW_TAB_URL;
1697 while ((ch = getopt(argc, argv, "")) != -1) {
1710 setlocale(LC_ALL, "");
1712 TAILQ_INIT(&global_map.m);
1713 global_map.unhandled_input = global_key_unbound;
1715 TAILQ_INIT(&minibuffer_map.m);
1717 TAILQ_INIT(&eecmd_history.head);
1718 TAILQ_INIT(&ir_history.head);
1719 TAILQ_INIT(&lu_history.head);
1721 base_map = &global_map;
1722 current_map = &global_map;
1723 load_default_keys();
1730 intrflush(stdscr, FALSE);
1732 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1734 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1736 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1738 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1741 body_lines = LINES-3;
1745 scrollok(body, TRUE);
1747 /* non-blocking input */
1750 mvwprintw(body, 0, 0, "");
1752 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1753 event_add(&stdioev, NULL);
1755 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1756 signal_add(&winchev, NULL);
1764 ui_on_tab_loaded(struct tab *tab)
1766 stop_loading_anim(tab);
1767 message("Loaded %s", tab->hist_cur->h);
1778 ui_on_tab_refresh(struct tab *tab)
1781 if (tab->flags & TAB_CURRENT)
1786 ui_require_input(struct tab *tab, int hide)
1788 /* TODO: hard-switching to another tab is ugly */
1791 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1793 strlcpy(ministate.prompt, "Input required: ",
1794 sizeof(ministate.prompt));
1799 ui_notify(const char *fmt, ...)