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
31 * ``on-demand'', but it's still missing.
35 #include <telescope.h>
47 #define TAB_CURRENT 0x1
49 #define NEW_TAB_URL "about:new"
51 static struct event stdioev, winchev;
53 static void load_default_keys(void);
54 static void restore_cursor(struct window*);
56 #define CMD(fnname) static void fnname(struct window *)
58 CMD(cmd_previous_line);
60 CMD(cmd_backward_char);
61 CMD(cmd_forward_char);
62 CMD(cmd_backward_paragraph);
63 CMD(cmd_forward_paragraph);
64 CMD(cmd_move_beginning_of_line);
65 CMD(cmd_move_end_of_line);
67 CMD(cmd_scroll_line_down);
68 CMD(cmd_scroll_line_up);
71 CMD(cmd_beginning_of_buffer);
72 CMD(cmd_end_of_buffer);
73 CMD(cmd_kill_telescope);
75 CMD(cmd_push_button_new_tab);
76 CMD(cmd_previous_button);
78 CMD(cmd_previous_page);
80 CMD(cmd_clear_minibuf);
81 CMD(cmd_execute_extended_command);
83 CMD(cmd_tab_close_other);
86 CMD(cmd_tab_previous);
90 CMD(cmd_load_current_url);
91 CMD(cmd_bookmark_page);
92 CMD(cmd_list_bookmarks);
95 CMD(cmd_mini_delete_char);
96 CMD(cmd_mini_delete_backward_char);
97 CMD(cmd_mini_kill_line);
99 CMD(cmd_mini_complete_and_exit);
100 CMD(cmd_mini_previous_history_element);
101 CMD(cmd_mini_next_history_element);
105 static void global_key_unbound(void);
106 static void minibuffer_hist_save_entry(void);
107 static void minibuffer_taint_hist(void);
108 static void minibuffer_self_insert(void);
109 static void eecmd_self_insert(void);
110 static void eecmd_select(void);
111 static void ir_self_insert(void);
112 static void ir_select(void);
113 static void lu_self_insert(void);
114 static void lu_select(void);
115 static void bp_select(void);
116 static void yornp_self_insert(void);
117 static void yornp_abort(void);
119 static struct vline *nth_line(struct window*, size_t);
120 static struct tab *current_tab(void);
121 static struct window *current_window(void);
122 static int readkey(void);
123 static void dispatch_stdio(int, short, void*);
124 static void handle_clear_minibuf(int, short, void*);
125 static void handle_resize(int, short, void*);
126 static int wrap_page(struct window*, int);
127 static void print_vline(WINDOW*, struct vline*);
128 static void redraw_tabline(void);
129 static void redraw_window(WINDOW*, int, struct window*);
130 static void redraw_help(void);
131 static void redraw_body(struct tab*);
132 static void redraw_modeline(struct tab*);
133 static void redraw_minibuffer(void);
134 static void redraw_tab(struct tab*);
135 static void emit_help_item(char*, void*);
136 static void rec_compute_help(struct kmap*, char*, size_t);
137 static void recompute_help(void);
138 static void vmessage(const char*, va_list);
139 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
140 static void start_loading_anim(struct tab*);
141 static void update_loading_anim(int, short, void*);
142 static void stop_loading_anim(struct tab*);
143 static void load_url_in_tab(struct tab*, const char*);
144 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
145 static void exit_minibuffer(void);
146 static void switch_to_tab(struct tab*);
147 static struct tab *new_tab(const char*);
148 static void session_new_tab_cb(const char*);
149 static void usage(void);
151 static struct { short meta; int key; uint32_t cp; } thiskey;
153 static WINDOW *tabline, *body, *modeline, *minibuf;
154 static int body_lines, body_cols;
157 static struct window helpwin;
158 static int help_lines, help_cols;
160 static int side_window;
162 static struct event clminibufev;
163 static struct timeval clminibufev_timer = { 5, 0 };
164 static struct timeval loadingev_timer = { 0, 250000 };
166 static uint32_t tab_counter;
168 static char keybuf[64];
170 static void (*yornp_cb)(int, unsigned int);
171 static unsigned int yornp_data;
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_list_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)
597 cmd_push_button(struct window *window)
602 nth = window->line_off + window->curs_y;
603 if (nth >= window->line_max)
605 vl = nth_line(window, nth);
606 if (vl->parent->type != LINE_LINK)
609 load_url_in_tab(current_tab(), vl->parent->alt);
613 cmd_push_button_new_tab(struct window *window)
618 nth = window->line_off + window->curs_y;
619 if (nth > window->line_max)
621 vl = nth_line(window, nth);
622 if (vl->parent->type != LINE_LINK)
625 new_tab(vl->parent->alt);
629 cmd_previous_button(struct window *window)
632 if (window->current_line == NULL ||
633 window->current_line == TAILQ_FIRST(&window->head)) {
634 message("No previous link");
637 cmd_previous_line(window);
638 } while (window->current_line->parent->type != LINE_LINK);
642 cmd_next_button(struct window *window)
645 if (window->current_line == NULL ||
646 window->current_line == TAILQ_LAST(&window->head, vhead)) {
647 message("No next link");
650 cmd_next_line(window);
651 } while (window->current_line->parent->type != LINE_LINK);
655 cmd_previous_page(struct window *window)
657 struct tab *tab = current_tab();
659 if (!load_previous_page(tab))
660 message("No previous page");
662 start_loading_anim(tab);
666 cmd_next_page(struct window *window)
668 struct tab *tab = current_tab();
670 if (!load_next_page(tab))
671 message("No next page");
673 start_loading_anim(tab);
677 cmd_clear_minibuf(struct window *window)
679 handle_clear_minibuf(0, 0, NULL);
683 cmd_execute_extended_command(struct window *window)
688 message("We don't have enable-recursive-minibuffers");
692 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
695 len = sizeof(ministate.prompt);
696 strlcpy(ministate.prompt, "", len);
699 strlcat(ministate.prompt, "M-", len);
701 strlcat(ministate.prompt, keyname(thiskey.key), len);
702 strlcat(ministate.prompt, " ", len);
706 cmd_tab_close(struct window *window)
711 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
712 TAILQ_NEXT(tab, tabs) == NULL) {
713 message("Can't close the only tab.");
717 if (evtimer_pending(&tab->loadingev, NULL))
718 evtimer_del(&tab->loadingev);
722 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
723 t = TAILQ_NEXT(tab, tabs);
724 TAILQ_REMOVE(&tabshead, tab, tabs);
731 cmd_tab_close_other(struct window *window)
735 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
736 if (t->flags & TAB_CURRENT)
740 TAILQ_REMOVE(&tabshead, t, tabs);
746 cmd_tab_new(struct window *window)
748 new_tab(NEW_TAB_URL);
752 cmd_tab_next(struct window *window)
757 tab->flags &= ~TAB_CURRENT;
759 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
760 t = TAILQ_FIRST(&tabshead);
761 t->flags |= TAB_CURRENT;
765 cmd_tab_previous(struct window *window)
770 tab->flags &= ~TAB_CURRENT;
772 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
773 t = TAILQ_LAST(&tabshead, tabshead);
774 t->flags |= TAB_CURRENT;
778 cmd_tab_move(struct window *window)
783 t = TAILQ_NEXT(tab, tabs);
784 TAILQ_REMOVE(&tabshead, tab, tabs);
787 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
789 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
793 cmd_tab_move_to(struct window *window)
798 t = TAILQ_PREV(tab, tabshead, tabs);
799 TAILQ_REMOVE(&tabshead, tab, tabs);
802 if (TAILQ_EMPTY(&tabshead))
803 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
805 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
807 TAILQ_INSERT_BEFORE(t, tab, tabs);
811 cmd_load_url(struct window *window)
814 message("We don't have enable-recursive-minibuffers");
818 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
820 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
824 cmd_load_current_url(struct window *window)
826 struct tab *tab = current_tab();
829 message("We don't have enable-recursive-minibuffers");
833 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
835 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
836 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
837 ministate.window.cpoff = utf8_cplen(ministate.buf);
841 cmd_bookmark_page(struct window *window)
843 struct tab *tab = current_tab();
845 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
846 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
847 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
848 ministate.window.cpoff = utf8_cplen(ministate.buf);
852 cmd_list_bookmarks(struct window *window)
854 load_url_in_tab(current_tab(), "about:bookmarks");
858 cmd_toggle_help(struct window *window)
860 side_window = !side_window;
864 /* ugly hack, but otherwise the window doesn't get updated
865 * until I call handle_resize a second time (i.e. C-l). I
866 * will be happy to know why something like this is needed. */
867 handle_resize(0, 0, NULL);
868 handle_resize(0, 0, NULL);
872 cmd_mini_delete_char(struct window *window)
876 if (!in_minibuffer) {
877 message("text is read-only");
881 minibuffer_taint_hist();
883 c = utf8_nth(window->current_line->line, window->cpoff);
888 memmove(c, n, strlen(n)+1);
892 cmd_mini_delete_backward_char(struct window *window)
896 if (!in_minibuffer) {
897 message("text is read-only");
901 minibuffer_taint_hist();
903 c = utf8_nth(window->current_line->line, window->cpoff);
904 start = window->current_line->line;
907 p = utf8_prev_cp(c-1, start);
909 memmove(p, c, strlen(c)+1);
914 cmd_mini_kill_line(struct window *window)
918 if (!in_minibuffer) {
919 message("text is read-only");
923 minibuffer_taint_hist();
924 c = utf8_nth(window->current_line->line, window->cpoff);
929 cmd_mini_abort(struct window *window)
938 cmd_mini_complete_and_exit(struct window *window)
943 minibuffer_taint_hist();
948 cmd_mini_previous_history_element(struct window *window)
950 if (ministate.history == NULL) {
951 message("No history");
955 if (ministate.hist_cur == NULL ||
956 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
957 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
958 ministate.hist_off = ministate.history->len - 1;
959 if (ministate.hist_cur == NULL)
960 message("No prev item");
962 ministate.hist_off--;
965 if (ministate.hist_cur != NULL)
966 window->current_line->line = ministate.hist_cur->h;
970 cmd_mini_next_history_element(struct window *window)
972 if (ministate.history == NULL) {
973 message("No history");
977 if (ministate.hist_cur == NULL ||
978 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
979 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
980 ministate.hist_off = 0;
981 if (ministate.hist_cur == NULL)
982 message("No next item");
984 ministate.hist_off++;
987 if (ministate.hist_cur != NULL)
988 window->current_line->line = ministate.hist_cur->h;
992 global_key_unbound(void)
994 message("%s is undefined", keybuf);
998 minibuffer_hist_save_entry(void)
1002 if (ministate.history == NULL)
1005 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1008 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1010 if (TAILQ_EMPTY(&ministate.history->head))
1011 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1013 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1014 ministate.history->len++;
1018 * taint the minibuffer cache: if we're currently showing a history
1019 * element, copy that to the current buf and reset the "history
1020 * navigation" thing.
1023 minibuffer_taint_hist(void)
1025 if (ministate.hist_cur == NULL)
1028 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1029 ministate.hist_cur = NULL;
1033 minibuffer_self_insert(void)
1035 char *c, tmp[5] = {0};
1038 minibuffer_taint_hist();
1040 if (thiskey.cp == 0)
1043 len = utf8_encode(thiskey.cp, tmp);
1044 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1045 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1048 memmove(c + len, c, strlen(c)+1);
1049 memcpy(c, tmp, len);
1050 ministate.window.cpoff++;
1054 eecmd_self_insert(void)
1056 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1057 !unicode_isgraph(thiskey.cp)) {
1058 global_key_unbound();
1062 minibuffer_self_insert();
1070 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1071 if (!strcmp(cmd->cmd, ministate.buf)) {
1073 minibuffer_hist_save_entry();
1074 cmd->fn(current_window());
1079 message("No match");
1083 ir_self_insert(void)
1085 minibuffer_self_insert();
1091 char buf[1025] = {0};
1092 struct phos_uri uri;
1095 tab = current_tab();
1098 minibuffer_hist_save_entry();
1100 /* a bit ugly but... */
1101 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1102 phos_uri_set_query(&uri, ministate.buf);
1103 phos_serialize_uri(&uri, buf, sizeof(buf));
1104 load_url_in_tab(tab, buf);
1108 lu_self_insert(void)
1110 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1111 !unicode_isgraph(thiskey.key)) {
1112 global_key_unbound();
1116 minibuffer_self_insert();
1123 minibuffer_hist_save_entry();
1124 load_url_in_tab(current_tab(), ministate.buf);
1131 if (*ministate.buf != '\0')
1132 add_to_bookmarks(ministate.buf);
1138 yornp_self_insert(void)
1140 if (thiskey.key != 'y' && thiskey.key != 'n') {
1141 message("Please answer y or n");
1146 yornp_cb(thiskey.key == 'y', yornp_data);
1153 yornp_cb(0, yornp_data);
1156 static struct vline *
1157 nth_line(struct window *window, size_t n)
1163 TAILQ_FOREACH(vl, &window->head, vlines) {
1178 TAILQ_FOREACH(t, &tabshead, tabs) {
1179 if (t->flags & TAB_CURRENT)
1187 static struct window *
1188 current_window(void)
1191 return &ministate.window;
1192 return ¤t_tab()->window;
1200 if ((thiskey.key = wgetch(body)) == ERR)
1203 thiskey.meta = thiskey.key == 27;
1205 thiskey.key = wgetch(body);
1206 if (thiskey.key == ERR || thiskey.key == 27) {
1213 if ((unsigned int)thiskey.key < UINT8_MAX) {
1215 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1217 if ((thiskey.key = wgetch(body)) == ERR) {
1218 message("Error decoding user input");
1228 dispatch_stdio(int fd, short ev, void *d)
1231 const char *keyname;
1237 if (keybuf[0] != '\0')
1238 strlcat(keybuf, " ", sizeof(keybuf));
1240 strlcat(keybuf, "M-", sizeof(keybuf));
1241 if (thiskey.cp != 0) {
1242 utf8_encode(thiskey.cp, tmp);
1243 strlcat(keybuf, tmp, sizeof(keybuf));
1245 if ((keyname = unkbd(thiskey.key)) != NULL)
1246 strlcat(keybuf, keyname, sizeof(keybuf));
1248 tmp[0] = thiskey.key;
1249 strlcat(keybuf, tmp, sizeof(keybuf));
1253 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1254 if (k->meta == thiskey.meta &&
1255 k->key == thiskey.key) {
1257 current_map = &k->map;
1259 current_map = base_map;
1260 strlcpy(keybuf, "", sizeof(keybuf));
1261 k->fn(current_window());
1267 if (current_map->unhandled_input != NULL)
1268 current_map->unhandled_input();
1270 global_key_unbound();
1273 strlcpy(keybuf, "", sizeof(keybuf));
1274 current_map = base_map;
1280 redraw_tab(current_tab());
1284 handle_clear_minibuf(int fd, short ev, void *d)
1286 free(ministate.curmesg);
1287 ministate.curmesg = NULL;
1289 redraw_minibuffer();
1290 if (in_minibuffer) {
1300 handle_resize(int sig, short ev, void *d)
1308 /* move and resize the windows, in reverse order! */
1310 mvwin(minibuf, LINES-1, 0);
1311 wresize(minibuf, 1, COLS);
1313 mvwin(modeline, LINES-2, 0);
1314 wresize(modeline, 1, COLS);
1316 body_lines = LINES-3;
1320 help_cols = 0.3 * COLS;
1321 help_lines = LINES-3;
1323 wresize(help, help_lines, help_cols);
1325 wrap_page(&helpwin, help_cols);
1327 body_cols = COLS - help_cols - 1;
1328 mvwin(body, 1, help_cols);
1332 wresize(body, body_lines, body_cols);
1334 wresize(tabline, 1, COLS);
1336 tab = current_tab();
1338 wrap_page(&tab->window, body_cols);
1343 wrap_page(struct window *window, int width)
1346 const struct line *orig;
1350 orig = window->current_line == NULL
1352 : window->current_line->parent;
1353 window->current_line = NULL;
1356 window->line_off = 0;
1358 empty_vlist(window);
1360 TAILQ_FOREACH(l, &window->page.head, lines) {
1361 prfx = line_prefixes[l->type].prfx1;
1370 case LINE_PRE_START:
1372 wrap_text(window, prfx, l, width);
1374 case LINE_PRE_CONTENT:
1375 hardwrap_text(window, l, width);
1379 if (orig == l && window->current_line == NULL) {
1380 window->line_off = window->line_max-1;
1381 window->current_line = TAILQ_LAST(&window->head, vhead);
1384 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1385 if (vl == NULL || vl->parent != orig)
1387 window->current_line = vl;
1393 if (window->current_line == NULL)
1394 window->current_line = TAILQ_FIRST(&window->head);
1400 print_vline(WINDOW *window, struct vline *vl)
1402 const char *text = vl->line;
1404 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1405 int text_face = line_faces[vl->parent->type].text_prop;
1408 prfx = line_prefixes[vl->parent->type].prfx1;
1410 prfx = line_prefixes[vl->parent->type].prfx2;
1415 wattron(window, prefix_face);
1416 wprintw(window, "%s", prfx);
1417 wattroff(window, prefix_face);
1419 wattron(window, text_face);
1420 wprintw(window, "%s", text);
1421 wattroff(window, text_face);
1425 redraw_tabline(void)
1428 size_t toskip, ots, tabwidth, space, x;
1429 int current, y, truncated;
1433 tabwidth = sizeof(buf)+1;
1437 TAILQ_FOREACH(tab, &tabshead, tabs) {
1439 if (tab->flags & TAB_CURRENT)
1443 if (toskip * tabwidth < space)
1448 while (toskip != 0 &&
1449 (ots - toskip+1) * tabwidth < space)
1454 wattron(tabline, tab_face.background);
1455 wprintw(tabline, toskip == 0 ? " " : "<");
1456 wattroff(tabline, tab_face.background);
1459 TAILQ_FOREACH(tab, &tabshead, tabs) {
1467 getyx(tabline, y, x);
1468 if (x + sizeof(buf)+2 >= (size_t)COLS)
1471 current = tab->flags & TAB_CURRENT;
1473 if (*(title = tab->window.page.title) == '\0')
1474 title = tab->hist_cur->h;
1476 strlcpy(buf, " ", sizeof(buf));
1477 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1478 /* truncation happens */
1479 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1481 /* pad with spaces */
1482 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1487 wattron(tabline, tab_face.current_tab);
1489 wattron(tabline, tab_face.tab);
1491 wprintw(tabline, "%s", buf);
1492 if (TAILQ_NEXT(tab, tabs) != NULL)
1493 wprintw(tabline, " ");
1496 wattroff(tabline, tab_face.current_tab);
1498 wattroff(tabline, tab_face.tab);
1501 wattron(tabline, tab_face.background);
1502 for (; x < (size_t)COLS; ++x)
1503 waddch(tabline, ' ');
1505 mvwprintw(tabline, 0, COLS-1, ">");
1509 redraw_window(WINDOW *win, int height, struct window *window)
1516 window->line_off = MIN(window->line_max-1, window->line_off);
1517 if (TAILQ_EMPTY(&window->head))
1521 vl = nth_line(window, window->line_off);
1522 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1524 print_vline(win, vl);
1530 wmove(win, window->curs_y, window->curs_x);
1536 redraw_window(help, help_lines, &helpwin);
1540 redraw_body(struct tab *tab)
1542 redraw_window(body, body_lines, &tab->window);
1546 trust_status_char(enum trust_state ts)
1549 case TS_UNKNOWN: return 'u';
1550 case TS_UNTRUSTED: return '!';
1551 case TS_TRUSTED: return 'v';
1552 case TS_VERIFIED: return 'V';
1557 redraw_modeline(struct tab *tab)
1560 int x, y, max_x, max_y;
1561 const char *mode = tab->window.page.name;
1562 const char *spin = "-\\|/";
1565 wattron(modeline, A_REVERSE);
1566 wmove(modeline, 0, 0);
1568 wprintw(modeline, "-%c%c %s ",
1569 spin[tab->loading_anim_step],
1570 trust_status_char(tab->trust),
1571 mode == NULL ? "(none)" : mode);
1573 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1575 if (tab->window.line_max <= (size_t)body_lines)
1576 wprintw(modeline, "All ");
1577 else if (tab->window.line_off == 0)
1578 wprintw(modeline, "Top ");
1579 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1580 wprintw(modeline, "Bottom ");
1582 wprintw(modeline, "%.0f%% ", pct);
1584 wprintw(modeline, "%d/%d %s ",
1585 tab->window.line_off + tab->window.curs_y,
1586 tab->window.line_max,
1589 getyx(modeline, y, x);
1590 getmaxyx(modeline, max_y, max_x);
1595 for (; x < max_x; ++x)
1596 waddstr(modeline, "-");
1600 redraw_minibuffer(void)
1603 size_t off_y, off_x = 0;
1608 if (in_minibuffer) {
1609 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1610 if (ministate.hist_cur != NULL)
1611 wprintw(minibuf, "(%zu/%zu) ",
1612 ministate.hist_off + 1,
1613 ministate.history->len);
1615 getyx(minibuf, off_y, off_x);
1617 start = ministate.hist_cur != NULL
1618 ? ministate.hist_cur->h
1620 c = utf8_nth(ministate.window.current_line->line,
1621 ministate.window.cpoff);
1622 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1623 start = utf8_next_cp(start);
1626 waddstr(minibuf, start);
1629 if (ministate.curmesg != NULL)
1630 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1633 if (!in_minibuffer && ministate.curmesg == NULL)
1634 waddstr(minibuf, keybuf);
1636 /* If nothing else, show the URL at point */
1637 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1638 tab = current_tab();
1639 if (tab->window.current_line != NULL &&
1640 tab->window.current_line->parent->type == LINE_LINK)
1641 waddstr(minibuf, tab->window.current_line->parent->alt);
1645 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1649 redraw_tab(struct tab *tab)
1658 redraw_modeline(tab);
1659 redraw_minibuffer();
1661 wnoutrefresh(tabline);
1662 wnoutrefresh(modeline);
1664 if (in_minibuffer) {
1666 wnoutrefresh(minibuf);
1668 wnoutrefresh(minibuf);
1676 emit_help_item(char *prfx, void *fn)
1681 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1685 assert(cmd != NULL);
1687 if ((l = calloc(1, sizeof(*l))) == NULL)
1690 l->type = LINE_TEXT;
1693 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1695 if (TAILQ_EMPTY(&helpwin.page.head))
1696 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1698 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1702 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1708 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1709 strlcpy(p, prfx, sizeof(p));
1711 strlcat(p, " ", sizeof(p));
1713 strlcat(p, "M-", sizeof(p));
1714 if ((kn = unkbd(k->key)) != NULL)
1715 strlcat(p, kn, sizeof(p));
1717 strlcat(p, keyname(k->key), sizeof(p));
1720 rec_compute_help(&k->map, p, sizeof(p));
1722 emit_help_item(p, k->fn);
1727 recompute_help(void)
1731 empty_vlist(&helpwin);
1732 empty_linelist(&helpwin);
1733 rec_compute_help(current_map, p, sizeof(p));
1734 wrap_page(&helpwin, help_cols);
1738 vmessage(const char *fmt, va_list ap)
1740 if (evtimer_pending(&clminibufev, NULL))
1741 evtimer_del(&clminibufev);
1742 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1743 evtimer_add(&clminibufev, &clminibufev_timer);
1745 free(ministate.curmesg);
1747 /* TODO: what to do if the allocation fails here? */
1748 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1749 ministate.curmesg = NULL;
1751 redraw_minibuffer();
1752 if (in_minibuffer) {
1762 message(const char *fmt, ...)
1772 start_loading_anim(struct tab *tab)
1774 if (tab->loading_anim)
1776 tab->loading_anim = 1;
1777 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1778 evtimer_add(&tab->loadingev, &loadingev_timer);
1782 update_loading_anim(int fd, short ev, void *d)
1784 struct tab *tab = d;
1786 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1788 if (tab->flags & TAB_CURRENT) {
1789 redraw_modeline(tab);
1796 evtimer_add(&tab->loadingev, &loadingev_timer);
1800 stop_loading_anim(struct tab *tab)
1802 if (!tab->loading_anim)
1804 evtimer_del(&tab->loadingev);
1805 tab->loading_anim = 0;
1806 tab->loading_anim_step = 0;
1808 if (!(tab->flags & TAB_CURRENT))
1811 redraw_modeline(tab);
1820 load_url_in_tab(struct tab *tab, const char *url)
1822 message("Loading %s...", url);
1823 start_loading_anim(tab);
1826 tab->window.curs_x = 0;
1827 tab->window.curs_y = 0;
1832 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1833 void (*abortfn)(void), struct histhead *hist)
1836 base_map = &minibuffer_map;
1837 current_map = &minibuffer_map;
1839 base_map->unhandled_input = self_insert_fn;
1841 ministate.donefn = donefn;
1842 ministate.abortfn = abortfn;
1843 memset(ministate.buf, 0, sizeof(ministate.buf));
1844 ministate.window.current_line = &ministate.vline;
1845 ministate.window.current_line->line = ministate.buf;
1846 ministate.window.cpoff = 0;
1847 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1849 ministate.history = hist;
1850 ministate.hist_cur = NULL;
1851 ministate.hist_off = 0;
1855 exit_minibuffer(void)
1860 base_map = &global_map;
1861 current_map = &global_map;
1865 switch_to_tab(struct tab *tab)
1869 TAILQ_FOREACH(t, &tabshead, tabs) {
1870 t->flags &= ~TAB_CURRENT;
1873 tab->flags |= TAB_CURRENT;
1879 return tab_counter++;
1883 new_tab(const char *url)
1887 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1892 TAILQ_INIT(&tab->hist.head);
1894 TAILQ_INIT(&tab->window.head);
1896 tab->id = tab_new_id();
1899 if (TAILQ_EMPTY(&tabshead))
1900 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1902 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1904 load_url_in_tab(tab, url);
1909 session_new_tab_cb(const char *url)
1917 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1921 ui_init(int argc, char * const *argv)
1923 const char *url = NEW_TAB_URL;
1926 while ((ch = getopt(argc, argv, "")) != -1) {
1939 setlocale(LC_ALL, "");
1941 TAILQ_INIT(&global_map.m);
1942 global_map.unhandled_input = global_key_unbound;
1944 TAILQ_INIT(&minibuffer_map.m);
1946 TAILQ_INIT(&eecmd_history.head);
1947 TAILQ_INIT(&ir_history.head);
1948 TAILQ_INIT(&lu_history.head);
1950 ministate.line.type = LINE_TEXT;
1951 ministate.vline.parent = &ministate.line;
1952 ministate.window.current_line = &ministate.vline;
1954 /* initialize help window */
1955 TAILQ_INIT(&helpwin.head);
1957 base_map = &global_map;
1958 current_map = &global_map;
1959 load_default_keys();
1966 intrflush(stdscr, FALSE);
1968 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1970 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1972 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1974 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1976 if ((help = newwin(1, 1, 1, 0)) == NULL)
1979 body_lines = LINES-3;
1983 scrollok(body, TRUE);
1985 /* non-blocking input */
1988 mvwprintw(body, 0, 0, "");
1990 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1991 event_add(&stdioev, NULL);
1993 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1994 signal_add(&winchev, NULL);
1996 load_last_session(session_new_tab_cb);
1997 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2004 ui_on_tab_loaded(struct tab *tab)
2006 stop_loading_anim(tab);
2007 message("Loaded %s", tab->hist_cur->h);
2018 ui_on_tab_refresh(struct tab *tab)
2020 wrap_page(&tab->window, body_cols);
2021 if (tab->flags & TAB_CURRENT) {
2022 restore_cursor(&tab->window);
2028 ui_require_input(struct tab *tab, int hide)
2030 /* TODO: hard-switching to another tab is ugly */
2033 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2035 strlcpy(ministate.prompt, "Input required: ",
2036 sizeof(ministate.prompt));
2041 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2046 if (in_minibuffer) {
2053 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2056 len = sizeof(ministate.prompt);
2057 strlcpy(ministate.prompt, prompt, len);
2058 strlcat(ministate.prompt, " (y or n) ", len);
2059 redraw_tab(current_tab());
2063 ui_notify(const char *fmt, ...)