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 restore_cursor(struct window*);
58 #define CMD(fnname) static void fnname(struct window *)
60 CMD(cmd_previous_line);
62 CMD(cmd_backward_char);
63 CMD(cmd_forward_char);
64 CMD(cmd_backward_paragraph);
65 CMD(cmd_forward_paragraph);
66 CMD(cmd_move_beginning_of_line);
67 CMD(cmd_move_end_of_line);
69 CMD(cmd_scroll_line_down);
70 CMD(cmd_scroll_line_up);
73 CMD(cmd_beginning_of_buffer);
74 CMD(cmd_end_of_buffer);
75 CMD(cmd_kill_telescope);
77 CMD(cmd_push_button_new_tab);
78 CMD(cmd_previous_button);
80 CMD(cmd_previous_page);
82 CMD(cmd_clear_minibuf);
83 CMD(cmd_execute_extended_command);
85 CMD(cmd_tab_close_other);
88 CMD(cmd_tab_previous);
92 CMD(cmd_load_current_url);
93 CMD(cmd_bookmark_page);
94 CMD(cmd_goto_bookmarks);
97 CMD(cmd_mini_delete_char);
98 CMD(cmd_mini_delete_backward_char);
99 CMD(cmd_mini_kill_line);
101 CMD(cmd_mini_complete_and_exit);
102 CMD(cmd_mini_previous_history_element);
103 CMD(cmd_mini_next_history_element);
107 static void global_key_unbound(void);
108 static void minibuffer_hist_save_entry(void);
109 static void minibuffer_taint_hist(void);
110 static void minibuffer_self_insert(void);
111 static void eecmd_self_insert(void);
112 static void eecmd_select(void);
113 static void ir_self_insert(void);
114 static void ir_select(void);
115 static void lu_self_insert(void);
116 static void lu_select(void);
117 static void bp_select(void);
118 static void yornp_self_insert(void);
119 static void yornp_abort(void);
121 static struct vline *nth_line(struct window*, size_t);
122 static struct tab *current_tab(void);
123 static struct window *current_window(void);
124 static int readkey(void);
125 static void dispatch_stdio(int, short, void*);
126 static void handle_clear_minibuf(int, short, void*);
127 static void handle_resize(int, short, void*);
128 static int wrap_page(struct window*, int);
129 static void print_vline(WINDOW*, struct vline*);
130 static void redraw_tabline(void);
131 static void redraw_window(WINDOW*, int, struct window*);
132 static void redraw_help(void);
133 static void redraw_body(struct tab*);
134 static void redraw_modeline(struct tab*);
135 static void redraw_minibuffer(void);
136 static void redraw_tab(struct tab*);
137 static void emit_help_item(char*, void*);
138 static void rec_compute_help(struct kmap*, char*, size_t);
139 static void recompute_help(void);
140 static void vmessage(const char*, va_list);
141 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
142 static void start_loading_anim(struct tab*);
143 static void update_loading_anim(int, short, void*);
144 static void stop_loading_anim(struct tab*);
145 static void load_url_in_tab(struct tab*, const char*);
146 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
147 static void exit_minibuffer(void);
148 static void switch_to_tab(struct tab*);
149 static struct tab *new_tab(const char*);
150 static void usage(void);
152 static struct { short meta; int key; uint32_t cp; } thiskey;
154 static WINDOW *tabline, *body, *modeline, *minibuf;
155 static int body_lines, body_cols;
158 static struct window helpwin;
159 static int help_lines, help_cols;
161 static int side_window;
163 static struct event clminibufev;
164 static struct timeval clminibufev_timer = { 5, 0 };
165 static struct timeval loadingev_timer = { 0, 250000 };
167 static uint32_t tab_counter;
169 static char keybuf[64];
171 static void (*yornp_cb)(int, unsigned int);
173 struct kmap global_map,
178 static struct histhead eecmd_history,
182 static int in_minibuffer;
188 void (*donefn)(void);
189 void (*abortfn)(void);
194 struct window window;
196 struct histhead *history;
197 struct hist *hist_cur;
204 } line_prefixes[] = {
205 [LINE_TEXT] = { "", "" },
206 [LINE_LINK] = { "=> ", " " },
207 [LINE_TITLE_1] = { "# ", " " },
208 [LINE_TITLE_2] = { "## ", " " },
209 [LINE_TITLE_3] = { "### ", " " },
210 [LINE_ITEM] = { "* ", " " },
211 [LINE_QUOTE] = { "> ", " " },
212 [LINE_PRE_START] = { "```", " " },
213 [LINE_PRE_CONTENT] = { "", "" },
214 [LINE_PRE_END] = { "```", "```" },
217 static struct line_face {
221 [LINE_TEXT] = { 0, 0 },
222 [LINE_LINK] = { 0, A_UNDERLINE },
223 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
224 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
225 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
226 [LINE_ITEM] = { 0, 0 },
227 [LINE_QUOTE] = { 0, A_DIM },
228 [LINE_PRE_START] = { 0, 0 },
229 [LINE_PRE_CONTENT] = { 0, 0 },
230 [LINE_PRE_END] = { 0, 0 },
233 static struct tab_face {
234 int background, tab, current_tab;
236 A_REVERSE, A_REVERSE, A_NORMAL
240 global_set_key(const char *key, void (*fn)(struct window*))
242 if (!kmap_define_key(&global_map, key, fn))
247 minibuffer_set_key(const char *key, void (*fn)(struct window*))
249 if (!kmap_define_key(&minibuffer_map, key, fn))
254 load_default_keys(void)
256 /* === global map === */
259 global_set_key("C-p", cmd_previous_line);
260 global_set_key("C-n", cmd_next_line);
261 global_set_key("C-f", cmd_forward_char);
262 global_set_key("C-b", cmd_backward_char);
263 global_set_key("M-{", cmd_backward_paragraph);
264 global_set_key("M-}", cmd_forward_paragraph);
265 global_set_key("C-a", cmd_move_beginning_of_line);
266 global_set_key("C-e", cmd_move_end_of_line);
268 global_set_key("M-v", cmd_scroll_up);
269 global_set_key("C-v", cmd_scroll_down);
270 global_set_key("M-space", cmd_scroll_up);
271 global_set_key("space", cmd_scroll_down);
273 global_set_key("M-<", cmd_beginning_of_buffer);
274 global_set_key("M->", cmd_end_of_buffer);
276 global_set_key("C-x C-c", cmd_kill_telescope);
278 global_set_key("C-g", cmd_clear_minibuf);
280 global_set_key("M-x", cmd_execute_extended_command);
281 global_set_key("C-x C-f", cmd_load_url);
282 global_set_key("C-x M-f", cmd_load_current_url);
284 global_set_key("C-x t 0", cmd_tab_close);
285 global_set_key("C-x t 1", cmd_tab_close_other);
286 global_set_key("C-x t 2", cmd_tab_new);
287 global_set_key("C-x t o", cmd_tab_next);
288 global_set_key("C-x t O", cmd_tab_previous);
289 global_set_key("C-x t m", cmd_tab_move);
290 global_set_key("C-x t M", cmd_tab_move_to);
292 global_set_key("C-M-b", cmd_previous_page);
293 global_set_key("C-M-f", cmd_next_page);
295 global_set_key("<f7> a", cmd_bookmark_page);
296 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
299 global_set_key("k", cmd_previous_line);
300 global_set_key("j", cmd_next_line);
301 global_set_key("l", cmd_forward_char);
302 global_set_key("h", cmd_backward_char);
303 global_set_key("{", cmd_backward_paragraph);
304 global_set_key("}", cmd_forward_paragraph);
305 global_set_key("^", cmd_move_beginning_of_line);
306 global_set_key("$", cmd_move_end_of_line);
308 global_set_key("K", cmd_scroll_line_up);
309 global_set_key("J", cmd_scroll_line_down);
311 global_set_key("g g", cmd_beginning_of_buffer);
312 global_set_key("G", cmd_end_of_buffer);
314 global_set_key("g D", cmd_tab_close);
315 global_set_key("g N", cmd_tab_new);
316 global_set_key("g t", cmd_tab_next);
317 global_set_key("g T", cmd_tab_previous);
318 global_set_key("g M-t", cmd_tab_move);
319 global_set_key("g M-T", cmd_tab_move_to);
321 global_set_key("H", cmd_previous_page);
322 global_set_key("L", cmd_next_page);
325 global_set_key("q", cmd_kill_telescope);
327 global_set_key("esc", cmd_clear_minibuf);
329 global_set_key(":", cmd_execute_extended_command);
332 global_set_key("<up>", cmd_previous_line);
333 global_set_key("<down>", cmd_next_line);
334 global_set_key("<right>", cmd_forward_char);
335 global_set_key("<left>", cmd_backward_char);
336 global_set_key("<prior>", cmd_scroll_up);
337 global_set_key("<next>", cmd_scroll_down);
339 global_set_key("M-<left>", cmd_previous_page);
340 global_set_key("M-<right>", cmd_next_page);
342 /* "ncurses standard" */
343 global_set_key("C-l", cmd_redraw);
346 global_set_key("<f1>", cmd_toggle_help);
347 global_set_key("C-m", cmd_push_button);
348 global_set_key("M-enter", cmd_push_button_new_tab);
349 global_set_key("M-tab", cmd_previous_button);
350 global_set_key("tab", cmd_next_button);
352 /* === minibuffer map === */
353 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
354 minibuffer_set_key("C-g", cmd_mini_abort);
355 minibuffer_set_key("esc", cmd_mini_abort);
356 minibuffer_set_key("C-d", cmd_mini_delete_char);
357 minibuffer_set_key("del", cmd_mini_delete_backward_char);
358 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
359 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
361 minibuffer_set_key("C-b", cmd_backward_char);
362 minibuffer_set_key("C-f", cmd_forward_char);
363 minibuffer_set_key("<left>", cmd_backward_char);
364 minibuffer_set_key("<right>", cmd_forward_char);
365 minibuffer_set_key("C-e", cmd_move_end_of_line);
366 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
367 minibuffer_set_key("<end>", cmd_move_end_of_line);
368 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
369 minibuffer_set_key("C-k", cmd_mini_kill_line);
371 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
372 minibuffer_set_key("M-n", cmd_mini_next_history_element);
373 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
374 minibuffer_set_key("<down>", cmd_mini_next_history_element);
378 restore_cursor(struct window *window)
383 vl = window->current_line;
384 if (vl == NULL || vl->line == NULL)
385 window->curs_x = window->cpoff = 0;
387 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
390 prfx = line_prefixes[vl->parent->type].prfx1;
391 window->curs_x += utf8_swidth(prfx);
396 cmd_previous_line(struct window *window)
400 if (window->current_line == NULL
401 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
404 if (--window->curs_y < 0) {
406 cmd_scroll_line_up(window);
410 window->current_line = vl;
411 restore_cursor(window);
415 cmd_next_line(struct window *window)
419 if (window->current_line == NULL
420 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
423 if (++window->curs_y > body_lines-1) {
424 window->curs_y = body_lines-1;
425 cmd_scroll_line_down(window);
429 window->current_line = vl;
430 restore_cursor(window);
434 cmd_backward_char(struct window *window)
436 if (window->cpoff != 0)
438 restore_cursor(window);
442 cmd_forward_char(struct window *window)
445 restore_cursor(window);
449 cmd_backward_paragraph(struct window *window)
452 if (window->current_line == NULL ||
453 window->current_line == TAILQ_FIRST(&window->head)) {
454 message("No previous paragraph");
457 cmd_previous_line(window);
458 } while (window->current_line->line != NULL ||
459 window->current_line->parent->type != LINE_TEXT);
463 cmd_forward_paragraph(struct window *window)
466 if (window->current_line == NULL ||
467 window->current_line == TAILQ_LAST(&window->head, vhead)) {
468 message("No next paragraph");
471 cmd_next_line(window);
472 } while (window->current_line->line != NULL ||
473 window->current_line->parent->type != LINE_TEXT);
477 cmd_move_beginning_of_line(struct window *window)
480 restore_cursor(window);
484 cmd_move_end_of_line(struct window *window)
488 vl = window->current_line;
489 if (vl->line == NULL)
491 window->cpoff = utf8_cplen(vl->line);
492 restore_cursor(window);
496 cmd_redraw(struct window *window)
498 handle_resize(0, 0, NULL);
502 cmd_scroll_line_up(struct window *window)
506 if (window->line_off == 0)
509 vl = nth_line(window, --window->line_off);
512 print_vline(body, vl);
514 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
515 restore_cursor(window);
519 cmd_scroll_line_down(struct window *window)
523 vl = window->current_line;
524 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
526 window->current_line = vl;
531 if (window->line_max - window->line_off < (size_t)body_lines)
534 vl = nth_line(window, window->line_off + body_lines-1);
535 wmove(body, body_lines-1, 0);
536 print_vline(body, vl);
538 restore_cursor(window);
542 cmd_scroll_up(struct window *window)
548 for (; off > 0; --off)
549 cmd_scroll_line_up(window);
553 cmd_scroll_down(struct window *window)
559 for (; off > 0; --off)
560 cmd_scroll_line_down(window);
564 cmd_beginning_of_buffer(struct window *window)
566 window->current_line = TAILQ_FIRST(&window->head);
567 window->line_off = 0;
570 restore_cursor(window);
574 cmd_end_of_buffer(struct window *window)
578 off = window->line_max - body_lines;
581 window->line_off = off;
582 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
584 window->current_line = TAILQ_LAST(&window->head, vhead);
585 window->cpoff = body_cols;
586 restore_cursor(window);
590 cmd_kill_telescope(struct window *window)
596 cmd_push_button(struct window *window)
601 nth = window->line_off + window->curs_y;
602 if (nth >= window->line_max)
604 vl = nth_line(window, nth);
605 if (vl->parent->type != LINE_LINK)
608 load_url_in_tab(current_tab(), vl->parent->alt);
612 cmd_push_button_new_tab(struct window *window)
617 nth = window->line_off + window->curs_y;
618 if (nth > window->line_max)
620 vl = nth_line(window, nth);
621 if (vl->parent->type != LINE_LINK)
624 new_tab(vl->parent->alt);
628 cmd_previous_button(struct window *window)
631 if (window->current_line == NULL ||
632 window->current_line == TAILQ_FIRST(&window->head)) {
633 message("No previous link");
636 cmd_previous_line(window);
637 } while (window->current_line->parent->type != LINE_LINK);
641 cmd_next_button(struct window *window)
644 if (window->current_line == NULL ||
645 window->current_line == TAILQ_LAST(&window->head, vhead)) {
646 message("No next link");
649 cmd_next_line(window);
650 } while (window->current_line->parent->type != LINE_LINK);
654 cmd_previous_page(struct window *window)
656 struct tab *tab = current_tab();
658 if (!load_previous_page(tab))
659 message("No previous page");
661 start_loading_anim(tab);
665 cmd_next_page(struct window *window)
667 struct tab *tab = current_tab();
669 if (!load_next_page(tab))
670 message("No next page");
672 start_loading_anim(tab);
676 cmd_clear_minibuf(struct window *window)
678 handle_clear_minibuf(0, 0, NULL);
682 cmd_execute_extended_command(struct window *window)
687 message("We don't have enable-recursive-minibuffers");
691 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
694 len = sizeof(ministate.prompt);
695 strlcpy(ministate.prompt, "", len);
698 strlcat(ministate.prompt, "M-", len);
700 strlcat(ministate.prompt, keyname(thiskey.key), len);
701 strlcat(ministate.prompt, " ", len);
705 cmd_tab_close(struct window *window)
710 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
711 TAILQ_NEXT(tab, tabs) == NULL) {
712 message("Can't close the only tab.");
716 if (evtimer_pending(&tab->loadingev, NULL))
717 evtimer_del(&tab->loadingev);
721 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
722 t = TAILQ_NEXT(tab, tabs);
723 TAILQ_REMOVE(&tabshead, tab, tabs);
730 cmd_tab_close_other(struct window *window)
734 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
735 if (t->flags & TAB_CURRENT)
739 TAILQ_REMOVE(&tabshead, t, tabs);
745 cmd_tab_new(struct window *window)
747 new_tab(NEW_TAB_URL);
751 cmd_tab_next(struct window *window)
756 tab->flags &= ~TAB_CURRENT;
758 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
759 t = TAILQ_FIRST(&tabshead);
760 t->flags |= TAB_CURRENT;
764 cmd_tab_previous(struct window *window)
769 tab->flags &= ~TAB_CURRENT;
771 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
772 t = TAILQ_LAST(&tabshead, tabshead);
773 t->flags |= TAB_CURRENT;
777 cmd_tab_move(struct window *window)
782 t = TAILQ_NEXT(tab, tabs);
783 TAILQ_REMOVE(&tabshead, tab, tabs);
786 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
788 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
792 cmd_tab_move_to(struct window *window)
797 t = TAILQ_PREV(tab, tabshead, tabs);
798 TAILQ_REMOVE(&tabshead, tab, tabs);
801 if (TAILQ_EMPTY(&tabshead))
802 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
804 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
806 TAILQ_INSERT_BEFORE(t, tab, tabs);
810 cmd_load_url(struct window *window)
813 message("We don't have enable-recursive-minibuffers");
817 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
819 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
823 cmd_load_current_url(struct window *window)
825 struct tab *tab = current_tab();
828 message("We don't have enable-recursive-minibuffers");
832 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
834 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
835 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
836 ministate.window.cpoff = utf8_cplen(ministate.buf);
840 cmd_bookmark_page(struct window *window)
842 struct tab *tab = current_tab();
844 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
845 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
846 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
847 ministate.window.cpoff = utf8_cplen(ministate.buf);
851 cmd_goto_bookmarks(struct window *window)
853 load_url_in_tab(current_tab(), "about:bookmarks");
857 cmd_toggle_help(struct window *window)
859 side_window = !side_window;
863 /* ugly hack, but otherwise the window doesn't get updated
864 * until I call handle_resize a second time (i.e. C-l). I
865 * will be happy to know why something like this is needed. */
866 handle_resize(0, 0, NULL);
867 handle_resize(0, 0, NULL);
871 cmd_mini_delete_char(struct window *window)
875 if (!in_minibuffer) {
876 message("text is read-only");
880 minibuffer_taint_hist();
882 c = utf8_nth(window->current_line->line, window->cpoff);
887 memmove(c, n, strlen(n)+1);
891 cmd_mini_delete_backward_char(struct window *window)
895 if (!in_minibuffer) {
896 message("text is read-only");
900 minibuffer_taint_hist();
902 c = utf8_nth(window->current_line->line, window->cpoff);
903 start = window->current_line->line;
906 p = utf8_prev_cp(c-1, start);
908 memmove(p, c, strlen(c)+1);
913 cmd_mini_kill_line(struct window *window)
917 if (!in_minibuffer) {
918 message("text is read-only");
922 minibuffer_taint_hist();
923 c = utf8_nth(window->current_line->line, window->cpoff);
928 cmd_mini_abort(struct window *window)
937 cmd_mini_complete_and_exit(struct window *window)
942 minibuffer_taint_hist();
947 cmd_mini_previous_history_element(struct window *window)
949 if (ministate.history == NULL) {
950 message("No history");
954 if (ministate.hist_cur == NULL ||
955 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
956 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
957 ministate.hist_off = ministate.history->len - 1;
958 if (ministate.hist_cur == NULL)
959 message("No prev item");
961 ministate.hist_off--;
964 if (ministate.hist_cur != NULL)
965 window->current_line->line = ministate.hist_cur->h;
969 cmd_mini_next_history_element(struct window *window)
971 if (ministate.history == NULL) {
972 message("No history");
976 if (ministate.hist_cur == NULL ||
977 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
978 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
979 ministate.hist_off = 0;
980 if (ministate.hist_cur == NULL)
981 message("No next item");
983 ministate.hist_off++;
986 if (ministate.hist_cur != NULL)
987 window->current_line->line = ministate.hist_cur->h;
991 global_key_unbound(void)
993 message("%s is undefined", keybuf);
997 minibuffer_hist_save_entry(void)
1001 if (ministate.history == NULL)
1004 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1007 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1009 if (TAILQ_EMPTY(&ministate.history->head))
1010 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1012 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1013 ministate.history->len++;
1017 * taint the minibuffer cache: if we're currently showing a history
1018 * element, copy that to the current buf and reset the "history
1019 * navigation" thing.
1022 minibuffer_taint_hist(void)
1024 if (ministate.hist_cur == NULL)
1027 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1028 ministate.hist_cur = NULL;
1032 minibuffer_self_insert(void)
1034 char *c, tmp[5] = {0};
1037 minibuffer_taint_hist();
1039 if (thiskey.cp == 0)
1042 len = utf8_encode(thiskey.cp, tmp);
1043 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1044 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1047 memmove(c + len, c, strlen(c)+1);
1048 memcpy(c, tmp, len);
1049 ministate.window.cpoff++;
1053 eecmd_self_insert(void)
1055 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1056 !unicode_isgraph(thiskey.cp)) {
1057 global_key_unbound();
1061 minibuffer_self_insert();
1069 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1070 if (!strcmp(cmd->cmd, ministate.buf)) {
1072 minibuffer_hist_save_entry();
1073 cmd->fn(current_window());
1078 message("No match");
1082 ir_self_insert(void)
1084 minibuffer_self_insert();
1090 char buf[1025] = {0};
1094 tab = current_tab();
1097 minibuffer_hist_save_entry();
1099 /* a bit ugly but... */
1100 memcpy(&url, &tab->url, sizeof(tab->url));
1101 url_set_query(&url, ministate.buf);
1102 url_unparse(&url, buf, sizeof(buf));
1103 load_url_in_tab(tab, buf);
1107 lu_self_insert(void)
1109 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1110 !unicode_isgraph(thiskey.key)) {
1111 global_key_unbound();
1115 minibuffer_self_insert();
1122 minibuffer_hist_save_entry();
1123 load_url_in_tab(current_tab(), ministate.buf);
1130 if (*ministate.buf != '\0')
1131 add_to_bookmarks(ministate.buf);
1137 yornp_self_insert(void)
1139 if (thiskey.key != 'y' && thiskey.key != 'n') {
1140 message("Please answer y or n");
1144 yornp_cb(thiskey.key == 'y', current_tab()->id);
1151 yornp_cb(0, current_tab()->id);
1155 static struct vline *
1156 nth_line(struct window *window, size_t n)
1162 TAILQ_FOREACH(vl, &window->head, vlines) {
1177 TAILQ_FOREACH(t, &tabshead, tabs) {
1178 if (t->flags & TAB_CURRENT)
1186 static struct window *
1187 current_window(void)
1190 return &ministate.window;
1191 return ¤t_tab()->window;
1199 if ((thiskey.key = wgetch(body)) == ERR)
1202 thiskey.meta = thiskey.key == 27;
1204 thiskey.key = wgetch(body);
1205 if (thiskey.key == ERR || thiskey.key == 27) {
1212 if ((unsigned int)thiskey.key < UINT8_MAX) {
1214 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1216 if ((thiskey.key = wgetch(body)) == ERR) {
1217 message("Error decoding user input");
1227 dispatch_stdio(int fd, short ev, void *d)
1230 const char *keyname;
1236 if (keybuf[0] != '\0')
1237 strlcat(keybuf, " ", sizeof(keybuf));
1239 strlcat(keybuf, "M-", sizeof(keybuf));
1240 if (thiskey.cp != 0) {
1241 utf8_encode(thiskey.cp, tmp);
1242 strlcat(keybuf, tmp, sizeof(keybuf));
1244 if ((keyname = unkbd(thiskey.key)) != NULL)
1245 strlcat(keybuf, keyname, sizeof(keybuf));
1247 tmp[0] = thiskey.key;
1248 strlcat(keybuf, tmp, sizeof(keybuf));
1252 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1253 if (k->meta == thiskey.meta &&
1254 k->key == thiskey.key) {
1256 current_map = &k->map;
1258 current_map = base_map;
1259 strlcpy(keybuf, "", sizeof(keybuf));
1260 k->fn(current_window());
1266 if (current_map->unhandled_input != NULL)
1267 current_map->unhandled_input();
1269 global_key_unbound();
1272 strlcpy(keybuf, "", sizeof(keybuf));
1273 current_map = base_map;
1279 redraw_tab(current_tab());
1283 handle_clear_minibuf(int fd, short ev, void *d)
1285 free(ministate.curmesg);
1286 ministate.curmesg = NULL;
1288 redraw_minibuffer();
1289 if (in_minibuffer) {
1299 handle_resize(int sig, short ev, void *d)
1307 /* move and resize the windows, in reverse order! */
1309 mvwin(minibuf, LINES-1, 0);
1310 wresize(minibuf, 1, COLS);
1312 mvwin(modeline, LINES-2, 0);
1313 wresize(modeline, 1, COLS);
1315 body_lines = LINES-3;
1319 help_cols = 0.3 * COLS;
1320 help_lines = LINES-3;
1322 wresize(help, help_lines, help_cols);
1324 wrap_page(&helpwin, help_cols);
1326 body_cols = COLS - help_cols - 1;
1327 mvwin(body, 1, help_cols);
1331 wresize(body, body_lines, body_cols);
1333 wresize(tabline, 1, COLS);
1335 tab = current_tab();
1337 wrap_page(&tab->window, body_cols);
1342 wrap_page(struct window *window, int width)
1345 const struct line *orig;
1349 orig = window->current_line == NULL
1351 : window->current_line->parent;
1352 window->current_line = NULL;
1355 window->line_off = 0;
1357 empty_vlist(window);
1359 TAILQ_FOREACH(l, &window->page.head, lines) {
1360 prfx = line_prefixes[l->type].prfx1;
1369 case LINE_PRE_START:
1371 wrap_text(window, prfx, l, width);
1373 case LINE_PRE_CONTENT:
1374 hardwrap_text(window, l, width);
1378 if (orig == l && window->current_line == NULL) {
1379 window->line_off = window->line_max-1;
1380 window->current_line = TAILQ_LAST(&window->head, vhead);
1383 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1384 if (vl == NULL || vl->parent != orig)
1386 window->current_line = vl;
1392 if (window->current_line == NULL)
1393 window->current_line = TAILQ_FIRST(&window->head);
1399 print_vline(WINDOW *window, struct vline *vl)
1401 const char *text = vl->line;
1403 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1404 int text_face = line_faces[vl->parent->type].text_prop;
1407 prfx = line_prefixes[vl->parent->type].prfx1;
1409 prfx = line_prefixes[vl->parent->type].prfx2;
1414 wattron(window, prefix_face);
1415 wprintw(window, "%s", prfx);
1416 wattroff(window, prefix_face);
1418 wattron(window, text_face);
1419 wprintw(window, "%s", text);
1420 wattroff(window, text_face);
1424 redraw_tabline(void)
1428 int current, x, y, truncated;
1434 TAILQ_FOREACH(tab, &tabshead, tabs) {
1435 x += sizeof(buf) + 1;
1437 if (tab->flags & TAB_CURRENT)
1446 wattron(tabline, tab_face.background);
1447 wprintw(tabline, toskip == 0 ? " " : "<");
1448 wattroff(tabline, tab_face.background);
1451 TAILQ_FOREACH(tab, &tabshead, tabs) {
1459 getyx(tabline, y, x);
1460 if (x + sizeof(buf)+2 >= (size_t)COLS)
1463 current = tab->flags & TAB_CURRENT;
1465 if (*(title = tab->window.page.title) == '\0')
1466 title = tab->hist_cur->h;
1468 strlcpy(buf, " ", sizeof(buf));
1469 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1470 /* truncation happens */
1471 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1473 /* pad with spaces */
1474 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1479 wattron(tabline, tab_face.current_tab);
1481 wattron(tabline, tab_face.tab);
1483 wprintw(tabline, "%s", buf);
1484 if (TAILQ_NEXT(tab, tabs) != NULL)
1485 wprintw(tabline, " ");
1488 wattroff(tabline, tab_face.current_tab);
1490 wattroff(tabline, tab_face.tab);
1493 wattron(tabline, tab_face.background);
1494 for (; x < COLS; ++x)
1495 waddch(tabline, ' ');
1497 mvwprintw(tabline, 0, COLS-1, ">");
1501 redraw_window(WINDOW *win, int height, struct window *window)
1508 window->line_off = MIN(window->line_max-1, window->line_off);
1509 if (TAILQ_EMPTY(&window->head))
1513 vl = nth_line(window, window->line_off);
1514 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1516 print_vline(win, vl);
1522 wmove(win, window->curs_y, window->curs_x);
1528 redraw_window(help, help_lines, &helpwin);
1532 redraw_body(struct tab *tab)
1534 redraw_window(body, body_lines, &tab->window);
1538 trust_status_char(enum trust_state ts)
1541 case TS_UNKNOWN: return 'u';
1542 case TS_UNTRUSTED: return '!';
1543 case TS_TRUSTED: return 'v';
1544 case TS_VERIFIED: return 'V';
1549 redraw_modeline(struct tab *tab)
1552 int x, y, max_x, max_y;
1553 const char *mode = tab->window.page.name;
1554 const char *spin = "-\\|/";
1557 wattron(modeline, A_REVERSE);
1558 wmove(modeline, 0, 0);
1560 wprintw(modeline, "-%c%c %s ",
1561 spin[tab->loading_anim_step],
1562 trust_status_char(tab->trust),
1563 mode == NULL ? "(none)" : mode);
1565 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1567 if (tab->window.line_max <= (size_t)body_lines)
1568 wprintw(modeline, "All ");
1569 else if (tab->window.line_off == 0)
1570 wprintw(modeline, "Top ");
1571 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1572 wprintw(modeline, "Bottom ");
1574 wprintw(modeline, "%.0f%% ", pct);
1576 wprintw(modeline, "%d/%d %s ",
1577 tab->window.line_off + tab->window.curs_y,
1578 tab->window.line_max,
1581 getyx(modeline, y, x);
1582 getmaxyx(modeline, max_y, max_x);
1587 for (; x < max_x; ++x)
1588 waddstr(modeline, "-");
1592 redraw_minibuffer(void)
1595 size_t off_y, off_x = 0;
1600 if (in_minibuffer) {
1601 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1602 if (ministate.hist_cur != NULL)
1603 wprintw(minibuf, "(%zu/%zu) ",
1604 ministate.hist_off + 1,
1605 ministate.history->len);
1607 getyx(minibuf, off_y, off_x);
1609 start = ministate.hist_cur != NULL
1610 ? ministate.hist_cur->h
1612 c = utf8_nth(ministate.window.current_line->line,
1613 ministate.window.cpoff);
1614 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1615 start = utf8_next_cp(start);
1618 waddstr(minibuf, start);
1621 if (ministate.curmesg != NULL)
1622 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1625 if (!in_minibuffer && ministate.curmesg == NULL)
1626 waddstr(minibuf, keybuf);
1628 /* If nothing else, show the URL at point */
1629 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1630 tab = current_tab();
1631 if (tab->window.current_line != NULL &&
1632 tab->window.current_line->parent->type == LINE_LINK)
1633 waddstr(minibuf, tab->window.current_line->parent->alt);
1637 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1641 redraw_tab(struct tab *tab)
1650 redraw_modeline(tab);
1651 redraw_minibuffer();
1653 wnoutrefresh(tabline);
1654 wnoutrefresh(modeline);
1656 if (in_minibuffer) {
1658 wnoutrefresh(minibuf);
1660 wnoutrefresh(minibuf);
1668 emit_help_item(char *prfx, void *fn)
1673 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1677 assert(cmd != NULL);
1679 if ((l = calloc(1, sizeof(*l))) == NULL)
1682 l->type = LINE_TEXT;
1685 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1687 if (TAILQ_EMPTY(&helpwin.page.head))
1688 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1690 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1694 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1700 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1701 strlcpy(p, prfx, sizeof(p));
1703 strlcat(p, " ", sizeof(p));
1705 strlcat(p, "M-", sizeof(p));
1706 if ((kn = unkbd(k->key)) != NULL)
1707 strlcat(p, kn, sizeof(p));
1709 strlcat(p, keyname(k->key), sizeof(p));
1712 rec_compute_help(&k->map, p, sizeof(p));
1714 emit_help_item(p, k->fn);
1719 recompute_help(void)
1723 empty_vlist(&helpwin);
1724 empty_linelist(&helpwin);
1725 rec_compute_help(current_map, p, sizeof(p));
1726 wrap_page(&helpwin, help_cols);
1730 vmessage(const char *fmt, va_list ap)
1732 if (evtimer_pending(&clminibufev, NULL))
1733 evtimer_del(&clminibufev);
1734 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1735 evtimer_add(&clminibufev, &clminibufev_timer);
1737 free(ministate.curmesg);
1739 /* TODO: what to do if the allocation fails here? */
1740 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1741 ministate.curmesg = NULL;
1743 redraw_minibuffer();
1744 if (in_minibuffer) {
1754 message(const char *fmt, ...)
1764 start_loading_anim(struct tab *tab)
1766 if (tab->loading_anim)
1768 tab->loading_anim = 1;
1769 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1770 evtimer_add(&tab->loadingev, &loadingev_timer);
1774 update_loading_anim(int fd, short ev, void *d)
1776 struct tab *tab = d;
1778 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1780 if (tab->flags & TAB_CURRENT) {
1781 redraw_modeline(tab);
1788 evtimer_add(&tab->loadingev, &loadingev_timer);
1792 stop_loading_anim(struct tab *tab)
1794 if (!tab->loading_anim)
1796 evtimer_del(&tab->loadingev);
1797 tab->loading_anim = 0;
1798 tab->loading_anim_step = 0;
1800 if (!(tab->flags & TAB_CURRENT))
1803 redraw_modeline(tab);
1812 load_url_in_tab(struct tab *tab, const char *url)
1814 empty_vlist(&tab->window);
1815 message("Loading %s...", url);
1816 start_loading_anim(tab);
1819 tab->window.curs_x = 0;
1820 tab->window.curs_y = 0;
1825 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1826 void (*abortfn)(void), struct histhead *hist)
1829 base_map = &minibuffer_map;
1830 current_map = &minibuffer_map;
1832 base_map->unhandled_input = self_insert_fn;
1834 ministate.donefn = donefn;
1835 ministate.abortfn = abortfn;
1836 memset(ministate.buf, 0, sizeof(ministate.buf));
1837 ministate.window.current_line = &ministate.vline;
1838 ministate.window.current_line->line = ministate.buf;
1839 ministate.window.cpoff = 0;
1840 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1842 ministate.history = hist;
1843 ministate.hist_cur = NULL;
1844 ministate.hist_off = 0;
1848 exit_minibuffer(void)
1853 base_map = &global_map;
1854 current_map = &global_map;
1858 switch_to_tab(struct tab *tab)
1862 TAILQ_FOREACH(t, &tabshead, tabs) {
1863 t->flags &= ~TAB_CURRENT;
1866 tab->flags |= TAB_CURRENT;
1870 new_tab(const char *url)
1874 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1879 TAILQ_INIT(&tab->hist.head);
1881 TAILQ_INIT(&tab->window.head);
1883 tab->id = tab_counter++;
1886 if (TAILQ_EMPTY(&tabshead))
1887 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1889 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1891 load_url_in_tab(tab, url);
1898 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1902 ui_init(int argc, char * const *argv)
1904 const char *url = NEW_TAB_URL;
1907 while ((ch = getopt(argc, argv, "")) != -1) {
1920 setlocale(LC_ALL, "");
1922 TAILQ_INIT(&global_map.m);
1923 global_map.unhandled_input = global_key_unbound;
1925 TAILQ_INIT(&minibuffer_map.m);
1927 TAILQ_INIT(&eecmd_history.head);
1928 TAILQ_INIT(&ir_history.head);
1929 TAILQ_INIT(&lu_history.head);
1931 ministate.line.type = LINE_TEXT;
1932 ministate.vline.parent = &ministate.line;
1933 ministate.window.current_line = &ministate.vline;
1935 /* initialize help window */
1936 TAILQ_INIT(&helpwin.head);
1938 base_map = &global_map;
1939 current_map = &global_map;
1940 load_default_keys();
1947 intrflush(stdscr, FALSE);
1949 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1951 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1953 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1955 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1957 if ((help = newwin(1, 1, 1, 0)) == NULL)
1960 body_lines = LINES-3;
1964 scrollok(body, TRUE);
1966 /* non-blocking input */
1969 mvwprintw(body, 0, 0, "");
1971 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1972 event_add(&stdioev, NULL);
1974 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1975 signal_add(&winchev, NULL);
1983 ui_on_tab_loaded(struct tab *tab)
1985 stop_loading_anim(tab);
1986 message("Loaded %s", tab->hist_cur->h);
1997 ui_on_tab_refresh(struct tab *tab)
1999 wrap_page(&tab->window, body_cols);
2000 if (tab->flags & TAB_CURRENT) {
2001 restore_cursor(&tab->window);
2007 ui_require_input(struct tab *tab, int hide)
2009 /* TODO: hard-switching to another tab is ugly */
2012 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2014 strlcpy(ministate.prompt, "Input required: ",
2015 sizeof(ministate.prompt));
2020 ui_yornp(const char *prompt, void (*fn)(int, unsigned int))
2025 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2028 len = sizeof(ministate.prompt);
2029 strlcpy(ministate.prompt, prompt, len);
2030 strlcat(ministate.prompt, " (y or n) ", len);
2031 redraw_tab(current_tab());
2035 ui_notify(const char *fmt, ...)