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_list_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 session_new_tab_cb(const char*);
151 static void usage(void);
153 static struct { short meta; int key; uint32_t cp; } thiskey;
155 static WINDOW *tabline, *body, *modeline, *minibuf;
156 static int body_lines, body_cols;
159 static struct window helpwin;
160 static int help_lines, help_cols;
162 static int side_window;
164 static struct event clminibufev;
165 static struct timeval clminibufev_timer = { 5, 0 };
166 static struct timeval loadingev_timer = { 0, 250000 };
168 static uint32_t tab_counter;
170 static char keybuf[64];
172 static void (*yornp_cb)(int, unsigned int);
173 static unsigned int yornp_data;
175 struct kmap global_map,
180 static struct histhead eecmd_history,
184 static int in_minibuffer;
190 void (*donefn)(void);
191 void (*abortfn)(void);
196 struct window window;
198 struct histhead *history;
199 struct hist *hist_cur;
206 } line_prefixes[] = {
207 [LINE_TEXT] = { "", "" },
208 [LINE_LINK] = { "=> ", " " },
209 [LINE_TITLE_1] = { "# ", " " },
210 [LINE_TITLE_2] = { "## ", " " },
211 [LINE_TITLE_3] = { "### ", " " },
212 [LINE_ITEM] = { "* ", " " },
213 [LINE_QUOTE] = { "> ", " " },
214 [LINE_PRE_START] = { "```", " " },
215 [LINE_PRE_CONTENT] = { "", "" },
216 [LINE_PRE_END] = { "```", "```" },
219 static struct line_face {
223 [LINE_TEXT] = { 0, 0 },
224 [LINE_LINK] = { 0, A_UNDERLINE },
225 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
226 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
227 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
228 [LINE_ITEM] = { 0, 0 },
229 [LINE_QUOTE] = { 0, A_DIM },
230 [LINE_PRE_START] = { 0, 0 },
231 [LINE_PRE_CONTENT] = { 0, 0 },
232 [LINE_PRE_END] = { 0, 0 },
235 static struct tab_face {
236 int background, tab, current_tab;
238 A_REVERSE, A_REVERSE, A_NORMAL
242 global_set_key(const char *key, void (*fn)(struct window*))
244 if (!kmap_define_key(&global_map, key, fn))
249 minibuffer_set_key(const char *key, void (*fn)(struct window*))
251 if (!kmap_define_key(&minibuffer_map, key, fn))
256 load_default_keys(void)
258 /* === global map === */
261 global_set_key("C-p", cmd_previous_line);
262 global_set_key("C-n", cmd_next_line);
263 global_set_key("C-f", cmd_forward_char);
264 global_set_key("C-b", cmd_backward_char);
265 global_set_key("M-{", cmd_backward_paragraph);
266 global_set_key("M-}", cmd_forward_paragraph);
267 global_set_key("C-a", cmd_move_beginning_of_line);
268 global_set_key("C-e", cmd_move_end_of_line);
270 global_set_key("M-v", cmd_scroll_up);
271 global_set_key("C-v", cmd_scroll_down);
272 global_set_key("M-space", cmd_scroll_up);
273 global_set_key("space", cmd_scroll_down);
275 global_set_key("M-<", cmd_beginning_of_buffer);
276 global_set_key("M->", cmd_end_of_buffer);
278 global_set_key("C-x C-c", cmd_kill_telescope);
280 global_set_key("C-g", cmd_clear_minibuf);
282 global_set_key("M-x", cmd_execute_extended_command);
283 global_set_key("C-x C-f", cmd_load_url);
284 global_set_key("C-x M-f", cmd_load_current_url);
286 global_set_key("C-x t 0", cmd_tab_close);
287 global_set_key("C-x t 1", cmd_tab_close_other);
288 global_set_key("C-x t 2", cmd_tab_new);
289 global_set_key("C-x t o", cmd_tab_next);
290 global_set_key("C-x t O", cmd_tab_previous);
291 global_set_key("C-x t m", cmd_tab_move);
292 global_set_key("C-x t M", cmd_tab_move_to);
294 global_set_key("C-M-b", cmd_previous_page);
295 global_set_key("C-M-f", cmd_next_page);
297 global_set_key("<f7> a", cmd_bookmark_page);
298 global_set_key("<f7> <f7>", cmd_list_bookmarks);
301 global_set_key("k", cmd_previous_line);
302 global_set_key("j", cmd_next_line);
303 global_set_key("l", cmd_forward_char);
304 global_set_key("h", cmd_backward_char);
305 global_set_key("{", cmd_backward_paragraph);
306 global_set_key("}", cmd_forward_paragraph);
307 global_set_key("^", cmd_move_beginning_of_line);
308 global_set_key("$", cmd_move_end_of_line);
310 global_set_key("K", cmd_scroll_line_up);
311 global_set_key("J", cmd_scroll_line_down);
313 global_set_key("g g", cmd_beginning_of_buffer);
314 global_set_key("G", cmd_end_of_buffer);
316 global_set_key("g D", cmd_tab_close);
317 global_set_key("g N", cmd_tab_new);
318 global_set_key("g t", cmd_tab_next);
319 global_set_key("g T", cmd_tab_previous);
320 global_set_key("g M-t", cmd_tab_move);
321 global_set_key("g M-T", cmd_tab_move_to);
323 global_set_key("H", cmd_previous_page);
324 global_set_key("L", cmd_next_page);
327 global_set_key("q", cmd_kill_telescope);
329 global_set_key("esc", cmd_clear_minibuf);
331 global_set_key(":", cmd_execute_extended_command);
334 global_set_key("<up>", cmd_previous_line);
335 global_set_key("<down>", cmd_next_line);
336 global_set_key("<right>", cmd_forward_char);
337 global_set_key("<left>", cmd_backward_char);
338 global_set_key("<prior>", cmd_scroll_up);
339 global_set_key("<next>", cmd_scroll_down);
341 global_set_key("M-<left>", cmd_previous_page);
342 global_set_key("M-<right>", cmd_next_page);
344 /* "ncurses standard" */
345 global_set_key("C-l", cmd_redraw);
348 global_set_key("<f1>", cmd_toggle_help);
349 global_set_key("C-m", cmd_push_button);
350 global_set_key("M-enter", cmd_push_button_new_tab);
351 global_set_key("M-tab", cmd_previous_button);
352 global_set_key("tab", cmd_next_button);
354 /* === minibuffer map === */
355 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
356 minibuffer_set_key("C-g", cmd_mini_abort);
357 minibuffer_set_key("esc", cmd_mini_abort);
358 minibuffer_set_key("C-d", cmd_mini_delete_char);
359 minibuffer_set_key("del", cmd_mini_delete_backward_char);
360 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
361 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
363 minibuffer_set_key("C-b", cmd_backward_char);
364 minibuffer_set_key("C-f", cmd_forward_char);
365 minibuffer_set_key("<left>", cmd_backward_char);
366 minibuffer_set_key("<right>", cmd_forward_char);
367 minibuffer_set_key("C-e", cmd_move_end_of_line);
368 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
369 minibuffer_set_key("<end>", cmd_move_end_of_line);
370 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
371 minibuffer_set_key("C-k", cmd_mini_kill_line);
373 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
374 minibuffer_set_key("M-n", cmd_mini_next_history_element);
375 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
376 minibuffer_set_key("<down>", cmd_mini_next_history_element);
380 restore_cursor(struct window *window)
385 vl = window->current_line;
386 if (vl == NULL || vl->line == NULL)
387 window->curs_x = window->cpoff = 0;
389 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
392 prfx = line_prefixes[vl->parent->type].prfx1;
393 window->curs_x += utf8_swidth(prfx);
398 cmd_previous_line(struct window *window)
402 if (window->current_line == NULL
403 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
406 if (--window->curs_y < 0) {
408 cmd_scroll_line_up(window);
412 window->current_line = vl;
413 restore_cursor(window);
417 cmd_next_line(struct window *window)
421 if (window->current_line == NULL
422 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
425 if (++window->curs_y > body_lines-1) {
426 window->curs_y = body_lines-1;
427 cmd_scroll_line_down(window);
431 window->current_line = vl;
432 restore_cursor(window);
436 cmd_backward_char(struct window *window)
438 if (window->cpoff != 0)
440 restore_cursor(window);
444 cmd_forward_char(struct window *window)
447 restore_cursor(window);
451 cmd_backward_paragraph(struct window *window)
454 if (window->current_line == NULL ||
455 window->current_line == TAILQ_FIRST(&window->head)) {
456 message("No previous paragraph");
459 cmd_previous_line(window);
460 } while (window->current_line->line != NULL ||
461 window->current_line->parent->type != LINE_TEXT);
465 cmd_forward_paragraph(struct window *window)
468 if (window->current_line == NULL ||
469 window->current_line == TAILQ_LAST(&window->head, vhead)) {
470 message("No next paragraph");
473 cmd_next_line(window);
474 } while (window->current_line->line != NULL ||
475 window->current_line->parent->type != LINE_TEXT);
479 cmd_move_beginning_of_line(struct window *window)
482 restore_cursor(window);
486 cmd_move_end_of_line(struct window *window)
490 vl = window->current_line;
491 if (vl->line == NULL)
493 window->cpoff = utf8_cplen(vl->line);
494 restore_cursor(window);
498 cmd_redraw(struct window *window)
500 handle_resize(0, 0, NULL);
504 cmd_scroll_line_up(struct window *window)
508 if (window->line_off == 0)
511 vl = nth_line(window, --window->line_off);
514 print_vline(body, vl);
516 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
517 restore_cursor(window);
521 cmd_scroll_line_down(struct window *window)
525 vl = window->current_line;
526 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
528 window->current_line = vl;
533 if (window->line_max - window->line_off < (size_t)body_lines)
536 vl = nth_line(window, window->line_off + body_lines-1);
537 wmove(body, body_lines-1, 0);
538 print_vline(body, vl);
540 restore_cursor(window);
544 cmd_scroll_up(struct window *window)
550 for (; off > 0; --off)
551 cmd_scroll_line_up(window);
555 cmd_scroll_down(struct window *window)
561 for (; off > 0; --off)
562 cmd_scroll_line_down(window);
566 cmd_beginning_of_buffer(struct window *window)
568 window->current_line = TAILQ_FIRST(&window->head);
569 window->line_off = 0;
572 restore_cursor(window);
576 cmd_end_of_buffer(struct window *window)
580 off = window->line_max - body_lines;
583 window->line_off = off;
584 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
586 window->current_line = TAILQ_LAST(&window->head, vhead);
587 window->cpoff = body_cols;
588 restore_cursor(window);
592 cmd_kill_telescope(struct window *window)
599 cmd_push_button(struct window *window)
604 nth = window->line_off + window->curs_y;
605 if (nth >= window->line_max)
607 vl = nth_line(window, nth);
608 if (vl->parent->type != LINE_LINK)
611 load_url_in_tab(current_tab(), vl->parent->alt);
615 cmd_push_button_new_tab(struct window *window)
620 nth = window->line_off + window->curs_y;
621 if (nth > window->line_max)
623 vl = nth_line(window, nth);
624 if (vl->parent->type != LINE_LINK)
627 new_tab(vl->parent->alt);
631 cmd_previous_button(struct window *window)
634 if (window->current_line == NULL ||
635 window->current_line == TAILQ_FIRST(&window->head)) {
636 message("No previous link");
639 cmd_previous_line(window);
640 } while (window->current_line->parent->type != LINE_LINK);
644 cmd_next_button(struct window *window)
647 if (window->current_line == NULL ||
648 window->current_line == TAILQ_LAST(&window->head, vhead)) {
649 message("No next link");
652 cmd_next_line(window);
653 } while (window->current_line->parent->type != LINE_LINK);
657 cmd_previous_page(struct window *window)
659 struct tab *tab = current_tab();
661 if (!load_previous_page(tab))
662 message("No previous page");
664 start_loading_anim(tab);
668 cmd_next_page(struct window *window)
670 struct tab *tab = current_tab();
672 if (!load_next_page(tab))
673 message("No next page");
675 start_loading_anim(tab);
679 cmd_clear_minibuf(struct window *window)
681 handle_clear_minibuf(0, 0, NULL);
685 cmd_execute_extended_command(struct window *window)
690 message("We don't have enable-recursive-minibuffers");
694 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
697 len = sizeof(ministate.prompt);
698 strlcpy(ministate.prompt, "", len);
701 strlcat(ministate.prompt, "M-", len);
703 strlcat(ministate.prompt, keyname(thiskey.key), len);
704 strlcat(ministate.prompt, " ", len);
708 cmd_tab_close(struct window *window)
713 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
714 TAILQ_NEXT(tab, tabs) == NULL) {
715 message("Can't close the only tab.");
719 if (evtimer_pending(&tab->loadingev, NULL))
720 evtimer_del(&tab->loadingev);
724 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
725 t = TAILQ_NEXT(tab, tabs);
726 TAILQ_REMOVE(&tabshead, tab, tabs);
733 cmd_tab_close_other(struct window *window)
737 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
738 if (t->flags & TAB_CURRENT)
742 TAILQ_REMOVE(&tabshead, t, tabs);
748 cmd_tab_new(struct window *window)
750 new_tab(NEW_TAB_URL);
754 cmd_tab_next(struct window *window)
759 tab->flags &= ~TAB_CURRENT;
761 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
762 t = TAILQ_FIRST(&tabshead);
763 t->flags |= TAB_CURRENT;
767 cmd_tab_previous(struct window *window)
772 tab->flags &= ~TAB_CURRENT;
774 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
775 t = TAILQ_LAST(&tabshead, tabshead);
776 t->flags |= TAB_CURRENT;
780 cmd_tab_move(struct window *window)
785 t = TAILQ_NEXT(tab, tabs);
786 TAILQ_REMOVE(&tabshead, tab, tabs);
789 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
791 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
795 cmd_tab_move_to(struct window *window)
800 t = TAILQ_PREV(tab, tabshead, tabs);
801 TAILQ_REMOVE(&tabshead, tab, tabs);
804 if (TAILQ_EMPTY(&tabshead))
805 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
807 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
809 TAILQ_INSERT_BEFORE(t, tab, tabs);
813 cmd_load_url(struct window *window)
816 message("We don't have enable-recursive-minibuffers");
820 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
822 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
826 cmd_load_current_url(struct window *window)
828 struct tab *tab = current_tab();
831 message("We don't have enable-recursive-minibuffers");
835 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
837 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
838 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
839 ministate.window.cpoff = utf8_cplen(ministate.buf);
843 cmd_bookmark_page(struct window *window)
845 struct tab *tab = current_tab();
847 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
848 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
849 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
850 ministate.window.cpoff = utf8_cplen(ministate.buf);
854 cmd_list_bookmarks(struct window *window)
856 load_url_in_tab(current_tab(), "about:bookmarks");
860 cmd_toggle_help(struct window *window)
862 side_window = !side_window;
866 /* ugly hack, but otherwise the window doesn't get updated
867 * until I call handle_resize a second time (i.e. C-l). I
868 * will be happy to know why something like this is needed. */
869 handle_resize(0, 0, NULL);
870 handle_resize(0, 0, NULL);
874 cmd_mini_delete_char(struct window *window)
878 if (!in_minibuffer) {
879 message("text is read-only");
883 minibuffer_taint_hist();
885 c = utf8_nth(window->current_line->line, window->cpoff);
890 memmove(c, n, strlen(n)+1);
894 cmd_mini_delete_backward_char(struct window *window)
898 if (!in_minibuffer) {
899 message("text is read-only");
903 minibuffer_taint_hist();
905 c = utf8_nth(window->current_line->line, window->cpoff);
906 start = window->current_line->line;
909 p = utf8_prev_cp(c-1, start);
911 memmove(p, c, strlen(c)+1);
916 cmd_mini_kill_line(struct window *window)
920 if (!in_minibuffer) {
921 message("text is read-only");
925 minibuffer_taint_hist();
926 c = utf8_nth(window->current_line->line, window->cpoff);
931 cmd_mini_abort(struct window *window)
940 cmd_mini_complete_and_exit(struct window *window)
945 minibuffer_taint_hist();
950 cmd_mini_previous_history_element(struct window *window)
952 if (ministate.history == NULL) {
953 message("No history");
957 if (ministate.hist_cur == NULL ||
958 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
959 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
960 ministate.hist_off = ministate.history->len - 1;
961 if (ministate.hist_cur == NULL)
962 message("No prev item");
964 ministate.hist_off--;
967 if (ministate.hist_cur != NULL)
968 window->current_line->line = ministate.hist_cur->h;
972 cmd_mini_next_history_element(struct window *window)
974 if (ministate.history == NULL) {
975 message("No history");
979 if (ministate.hist_cur == NULL ||
980 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
981 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
982 ministate.hist_off = 0;
983 if (ministate.hist_cur == NULL)
984 message("No next item");
986 ministate.hist_off++;
989 if (ministate.hist_cur != NULL)
990 window->current_line->line = ministate.hist_cur->h;
994 global_key_unbound(void)
996 message("%s is undefined", keybuf);
1000 minibuffer_hist_save_entry(void)
1004 if (ministate.history == NULL)
1007 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1010 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1012 if (TAILQ_EMPTY(&ministate.history->head))
1013 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1015 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1016 ministate.history->len++;
1020 * taint the minibuffer cache: if we're currently showing a history
1021 * element, copy that to the current buf and reset the "history
1022 * navigation" thing.
1025 minibuffer_taint_hist(void)
1027 if (ministate.hist_cur == NULL)
1030 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1031 ministate.hist_cur = NULL;
1035 minibuffer_self_insert(void)
1037 char *c, tmp[5] = {0};
1040 minibuffer_taint_hist();
1042 if (thiskey.cp == 0)
1045 len = utf8_encode(thiskey.cp, tmp);
1046 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1047 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1050 memmove(c + len, c, strlen(c)+1);
1051 memcpy(c, tmp, len);
1052 ministate.window.cpoff++;
1056 eecmd_self_insert(void)
1058 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1059 !unicode_isgraph(thiskey.cp)) {
1060 global_key_unbound();
1064 minibuffer_self_insert();
1072 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1073 if (!strcmp(cmd->cmd, ministate.buf)) {
1075 minibuffer_hist_save_entry();
1076 cmd->fn(current_window());
1081 message("No match");
1085 ir_self_insert(void)
1087 minibuffer_self_insert();
1093 char buf[1025] = {0};
1094 struct phos_uri uri;
1097 tab = current_tab();
1100 minibuffer_hist_save_entry();
1102 /* a bit ugly but... */
1103 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1105 /* XXX: ptc encode! */
1106 memcpy(&uri.query, ministate.buf, strlen(ministate.buf)+1);
1108 phos_serialize_uri(&uri, buf, sizeof(buf));
1109 load_url_in_tab(tab, buf);
1113 lu_self_insert(void)
1115 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1116 !unicode_isgraph(thiskey.key)) {
1117 global_key_unbound();
1121 minibuffer_self_insert();
1128 minibuffer_hist_save_entry();
1129 load_url_in_tab(current_tab(), ministate.buf);
1136 if (*ministate.buf != '\0')
1137 add_to_bookmarks(ministate.buf);
1143 yornp_self_insert(void)
1145 if (thiskey.key != 'y' && thiskey.key != 'n') {
1146 message("Please answer y or n");
1150 yornp_cb(thiskey.key == 'y', yornp_data);
1157 yornp_cb(0, yornp_data);
1161 static struct vline *
1162 nth_line(struct window *window, size_t n)
1168 TAILQ_FOREACH(vl, &window->head, vlines) {
1183 TAILQ_FOREACH(t, &tabshead, tabs) {
1184 if (t->flags & TAB_CURRENT)
1192 static struct window *
1193 current_window(void)
1196 return &ministate.window;
1197 return ¤t_tab()->window;
1205 if ((thiskey.key = wgetch(body)) == ERR)
1208 thiskey.meta = thiskey.key == 27;
1210 thiskey.key = wgetch(body);
1211 if (thiskey.key == ERR || thiskey.key == 27) {
1218 if ((unsigned int)thiskey.key < UINT8_MAX) {
1220 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1222 if ((thiskey.key = wgetch(body)) == ERR) {
1223 message("Error decoding user input");
1233 dispatch_stdio(int fd, short ev, void *d)
1236 const char *keyname;
1242 if (keybuf[0] != '\0')
1243 strlcat(keybuf, " ", sizeof(keybuf));
1245 strlcat(keybuf, "M-", sizeof(keybuf));
1246 if (thiskey.cp != 0) {
1247 utf8_encode(thiskey.cp, tmp);
1248 strlcat(keybuf, tmp, sizeof(keybuf));
1250 if ((keyname = unkbd(thiskey.key)) != NULL)
1251 strlcat(keybuf, keyname, sizeof(keybuf));
1253 tmp[0] = thiskey.key;
1254 strlcat(keybuf, tmp, sizeof(keybuf));
1258 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1259 if (k->meta == thiskey.meta &&
1260 k->key == thiskey.key) {
1262 current_map = &k->map;
1264 current_map = base_map;
1265 strlcpy(keybuf, "", sizeof(keybuf));
1266 k->fn(current_window());
1272 if (current_map->unhandled_input != NULL)
1273 current_map->unhandled_input();
1275 global_key_unbound();
1278 strlcpy(keybuf, "", sizeof(keybuf));
1279 current_map = base_map;
1285 redraw_tab(current_tab());
1289 handle_clear_minibuf(int fd, short ev, void *d)
1291 free(ministate.curmesg);
1292 ministate.curmesg = NULL;
1294 redraw_minibuffer();
1295 if (in_minibuffer) {
1305 handle_resize(int sig, short ev, void *d)
1313 /* move and resize the windows, in reverse order! */
1315 mvwin(minibuf, LINES-1, 0);
1316 wresize(minibuf, 1, COLS);
1318 mvwin(modeline, LINES-2, 0);
1319 wresize(modeline, 1, COLS);
1321 body_lines = LINES-3;
1325 help_cols = 0.3 * COLS;
1326 help_lines = LINES-3;
1328 wresize(help, help_lines, help_cols);
1330 wrap_page(&helpwin, help_cols);
1332 body_cols = COLS - help_cols - 1;
1333 mvwin(body, 1, help_cols);
1337 wresize(body, body_lines, body_cols);
1339 wresize(tabline, 1, COLS);
1341 tab = current_tab();
1343 wrap_page(&tab->window, body_cols);
1348 wrap_page(struct window *window, int width)
1351 const struct line *orig;
1355 orig = window->current_line == NULL
1357 : window->current_line->parent;
1358 window->current_line = NULL;
1361 window->line_off = 0;
1363 empty_vlist(window);
1365 TAILQ_FOREACH(l, &window->page.head, lines) {
1366 prfx = line_prefixes[l->type].prfx1;
1375 case LINE_PRE_START:
1377 wrap_text(window, prfx, l, width);
1379 case LINE_PRE_CONTENT:
1380 hardwrap_text(window, l, width);
1384 if (orig == l && window->current_line == NULL) {
1385 window->line_off = window->line_max-1;
1386 window->current_line = TAILQ_LAST(&window->head, vhead);
1389 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1390 if (vl == NULL || vl->parent != orig)
1392 window->current_line = vl;
1398 if (window->current_line == NULL)
1399 window->current_line = TAILQ_FIRST(&window->head);
1405 print_vline(WINDOW *window, struct vline *vl)
1407 const char *text = vl->line;
1409 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1410 int text_face = line_faces[vl->parent->type].text_prop;
1413 prfx = line_prefixes[vl->parent->type].prfx1;
1415 prfx = line_prefixes[vl->parent->type].prfx2;
1420 wattron(window, prefix_face);
1421 wprintw(window, "%s", prfx);
1422 wattroff(window, prefix_face);
1424 wattron(window, text_face);
1425 wprintw(window, "%s", text);
1426 wattroff(window, text_face);
1430 redraw_tabline(void)
1434 int current, x, y, truncated;
1440 TAILQ_FOREACH(tab, &tabshead, tabs) {
1441 x += sizeof(buf) + 1;
1443 if (tab->flags & TAB_CURRENT)
1452 wattron(tabline, tab_face.background);
1453 wprintw(tabline, toskip == 0 ? " " : "<");
1454 wattroff(tabline, tab_face.background);
1457 TAILQ_FOREACH(tab, &tabshead, tabs) {
1465 getyx(tabline, y, x);
1466 if (x + sizeof(buf)+2 >= (size_t)COLS)
1469 current = tab->flags & TAB_CURRENT;
1471 if (*(title = tab->window.page.title) == '\0')
1472 title = tab->hist_cur->h;
1474 strlcpy(buf, " ", sizeof(buf));
1475 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1476 /* truncation happens */
1477 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1479 /* pad with spaces */
1480 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1485 wattron(tabline, tab_face.current_tab);
1487 wattron(tabline, tab_face.tab);
1489 wprintw(tabline, "%s", buf);
1490 if (TAILQ_NEXT(tab, tabs) != NULL)
1491 wprintw(tabline, " ");
1494 wattroff(tabline, tab_face.current_tab);
1496 wattroff(tabline, tab_face.tab);
1499 wattron(tabline, tab_face.background);
1500 for (; x < COLS; ++x)
1501 waddch(tabline, ' ');
1503 mvwprintw(tabline, 0, COLS-1, ">");
1507 redraw_window(WINDOW *win, int height, struct window *window)
1514 window->line_off = MIN(window->line_max-1, window->line_off);
1515 if (TAILQ_EMPTY(&window->head))
1519 vl = nth_line(window, window->line_off);
1520 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1522 print_vline(win, vl);
1528 wmove(win, window->curs_y, window->curs_x);
1534 redraw_window(help, help_lines, &helpwin);
1538 redraw_body(struct tab *tab)
1540 redraw_window(body, body_lines, &tab->window);
1544 trust_status_char(enum trust_state ts)
1547 case TS_UNKNOWN: return 'u';
1548 case TS_UNTRUSTED: return '!';
1549 case TS_TRUSTED: return 'v';
1550 case TS_VERIFIED: return 'V';
1555 redraw_modeline(struct tab *tab)
1558 int x, y, max_x, max_y;
1559 const char *mode = tab->window.page.name;
1560 const char *spin = "-\\|/";
1563 wattron(modeline, A_REVERSE);
1564 wmove(modeline, 0, 0);
1566 wprintw(modeline, "-%c%c %s ",
1567 spin[tab->loading_anim_step],
1568 trust_status_char(tab->trust),
1569 mode == NULL ? "(none)" : mode);
1571 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1573 if (tab->window.line_max <= (size_t)body_lines)
1574 wprintw(modeline, "All ");
1575 else if (tab->window.line_off == 0)
1576 wprintw(modeline, "Top ");
1577 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1578 wprintw(modeline, "Bottom ");
1580 wprintw(modeline, "%.0f%% ", pct);
1582 wprintw(modeline, "%d/%d %s ",
1583 tab->window.line_off + tab->window.curs_y,
1584 tab->window.line_max,
1587 getyx(modeline, y, x);
1588 getmaxyx(modeline, max_y, max_x);
1593 for (; x < max_x; ++x)
1594 waddstr(modeline, "-");
1598 redraw_minibuffer(void)
1601 size_t off_y, off_x = 0;
1606 if (in_minibuffer) {
1607 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1608 if (ministate.hist_cur != NULL)
1609 wprintw(minibuf, "(%zu/%zu) ",
1610 ministate.hist_off + 1,
1611 ministate.history->len);
1613 getyx(minibuf, off_y, off_x);
1615 start = ministate.hist_cur != NULL
1616 ? ministate.hist_cur->h
1618 c = utf8_nth(ministate.window.current_line->line,
1619 ministate.window.cpoff);
1620 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1621 start = utf8_next_cp(start);
1624 waddstr(minibuf, start);
1627 if (ministate.curmesg != NULL)
1628 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1631 if (!in_minibuffer && ministate.curmesg == NULL)
1632 waddstr(minibuf, keybuf);
1634 /* If nothing else, show the URL at point */
1635 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1636 tab = current_tab();
1637 if (tab->window.current_line != NULL &&
1638 tab->window.current_line->parent->type == LINE_LINK)
1639 waddstr(minibuf, tab->window.current_line->parent->alt);
1643 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1647 redraw_tab(struct tab *tab)
1656 redraw_modeline(tab);
1657 redraw_minibuffer();
1659 wnoutrefresh(tabline);
1660 wnoutrefresh(modeline);
1662 if (in_minibuffer) {
1664 wnoutrefresh(minibuf);
1666 wnoutrefresh(minibuf);
1674 emit_help_item(char *prfx, void *fn)
1679 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1683 assert(cmd != NULL);
1685 if ((l = calloc(1, sizeof(*l))) == NULL)
1688 l->type = LINE_TEXT;
1691 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1693 if (TAILQ_EMPTY(&helpwin.page.head))
1694 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1696 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1700 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1706 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1707 strlcpy(p, prfx, sizeof(p));
1709 strlcat(p, " ", sizeof(p));
1711 strlcat(p, "M-", sizeof(p));
1712 if ((kn = unkbd(k->key)) != NULL)
1713 strlcat(p, kn, sizeof(p));
1715 strlcat(p, keyname(k->key), sizeof(p));
1718 rec_compute_help(&k->map, p, sizeof(p));
1720 emit_help_item(p, k->fn);
1725 recompute_help(void)
1729 empty_vlist(&helpwin);
1730 empty_linelist(&helpwin);
1731 rec_compute_help(current_map, p, sizeof(p));
1732 wrap_page(&helpwin, help_cols);
1736 vmessage(const char *fmt, va_list ap)
1738 if (evtimer_pending(&clminibufev, NULL))
1739 evtimer_del(&clminibufev);
1740 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1741 evtimer_add(&clminibufev, &clminibufev_timer);
1743 free(ministate.curmesg);
1745 /* TODO: what to do if the allocation fails here? */
1746 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1747 ministate.curmesg = NULL;
1749 redraw_minibuffer();
1750 if (in_minibuffer) {
1760 message(const char *fmt, ...)
1770 start_loading_anim(struct tab *tab)
1772 if (tab->loading_anim)
1774 tab->loading_anim = 1;
1775 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1776 evtimer_add(&tab->loadingev, &loadingev_timer);
1780 update_loading_anim(int fd, short ev, void *d)
1782 struct tab *tab = d;
1784 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1786 if (tab->flags & TAB_CURRENT) {
1787 redraw_modeline(tab);
1794 evtimer_add(&tab->loadingev, &loadingev_timer);
1798 stop_loading_anim(struct tab *tab)
1800 if (!tab->loading_anim)
1802 evtimer_del(&tab->loadingev);
1803 tab->loading_anim = 0;
1804 tab->loading_anim_step = 0;
1806 if (!(tab->flags & TAB_CURRENT))
1809 redraw_modeline(tab);
1818 load_url_in_tab(struct tab *tab, const char *url)
1820 message("Loading %s...", url);
1821 start_loading_anim(tab);
1824 tab->window.curs_x = 0;
1825 tab->window.curs_y = 0;
1830 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1831 void (*abortfn)(void), struct histhead *hist)
1834 base_map = &minibuffer_map;
1835 current_map = &minibuffer_map;
1837 base_map->unhandled_input = self_insert_fn;
1839 ministate.donefn = donefn;
1840 ministate.abortfn = abortfn;
1841 memset(ministate.buf, 0, sizeof(ministate.buf));
1842 ministate.window.current_line = &ministate.vline;
1843 ministate.window.current_line->line = ministate.buf;
1844 ministate.window.cpoff = 0;
1845 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1847 ministate.history = hist;
1848 ministate.hist_cur = NULL;
1849 ministate.hist_off = 0;
1853 exit_minibuffer(void)
1858 base_map = &global_map;
1859 current_map = &global_map;
1863 switch_to_tab(struct tab *tab)
1867 TAILQ_FOREACH(t, &tabshead, tabs) {
1868 t->flags &= ~TAB_CURRENT;
1871 tab->flags |= TAB_CURRENT;
1875 new_tab(const char *url)
1879 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1884 TAILQ_INIT(&tab->hist.head);
1886 TAILQ_INIT(&tab->window.head);
1888 tab->id = tab_counter++;
1891 if (TAILQ_EMPTY(&tabshead))
1892 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1894 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1896 load_url_in_tab(tab, url);
1901 session_new_tab_cb(const char *url)
1909 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1913 ui_init(int argc, char * const *argv)
1915 const char *url = NEW_TAB_URL;
1918 while ((ch = getopt(argc, argv, "")) != -1) {
1931 setlocale(LC_ALL, "");
1933 TAILQ_INIT(&global_map.m);
1934 global_map.unhandled_input = global_key_unbound;
1936 TAILQ_INIT(&minibuffer_map.m);
1938 TAILQ_INIT(&eecmd_history.head);
1939 TAILQ_INIT(&ir_history.head);
1940 TAILQ_INIT(&lu_history.head);
1942 ministate.line.type = LINE_TEXT;
1943 ministate.vline.parent = &ministate.line;
1944 ministate.window.current_line = &ministate.vline;
1946 /* initialize help window */
1947 TAILQ_INIT(&helpwin.head);
1949 base_map = &global_map;
1950 current_map = &global_map;
1951 load_default_keys();
1958 intrflush(stdscr, FALSE);
1960 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1962 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1964 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1966 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1968 if ((help = newwin(1, 1, 1, 0)) == NULL)
1971 body_lines = LINES-3;
1975 scrollok(body, TRUE);
1977 /* non-blocking input */
1980 mvwprintw(body, 0, 0, "");
1982 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1983 event_add(&stdioev, NULL);
1985 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1986 signal_add(&winchev, NULL);
1988 load_last_session(session_new_tab_cb);
1989 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1996 ui_on_tab_loaded(struct tab *tab)
1998 stop_loading_anim(tab);
1999 message("Loaded %s", tab->hist_cur->h);
2010 ui_on_tab_refresh(struct tab *tab)
2012 wrap_page(&tab->window, body_cols);
2013 if (tab->flags & TAB_CURRENT) {
2014 restore_cursor(&tab->window);
2020 ui_require_input(struct tab *tab, int hide)
2022 /* TODO: hard-switching to another tab is ugly */
2025 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2027 strlcpy(ministate.prompt, "Input required: ",
2028 sizeof(ministate.prompt));
2033 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2040 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2043 len = sizeof(ministate.prompt);
2044 strlcpy(ministate.prompt, prompt, len);
2045 strlcat(ministate.prompt, " (y or n) ", len);
2046 redraw_tab(current_tab());
2050 ui_notify(const char *fmt, ...)