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 *)
57 #define DEFALIAS(s, d) /* nothing */
59 CMD(cmd_previous_line);
61 CMD(cmd_backward_char);
62 CMD(cmd_forward_char);
63 CMD(cmd_backward_paragraph);
64 CMD(cmd_forward_paragraph);
65 CMD(cmd_move_beginning_of_line);
66 CMD(cmd_move_end_of_line);
68 CMD(cmd_scroll_line_down);
69 CMD(cmd_scroll_line_up);
72 CMD(cmd_beginning_of_buffer);
73 CMD(cmd_end_of_buffer);
75 CMD(cmd_kill_telescope);
76 DEFALIAS(q, cmd_kill_telescope)
77 DEFALIAS(wq, cmd_kill_telescope)
80 CMD(cmd_push_button_new_tab);
81 CMD(cmd_previous_button);
83 CMD(cmd_previous_page);
85 CMD(cmd_clear_minibuf);
86 CMD(cmd_execute_extended_command);
88 CMD(cmd_tab_close_other);
91 CMD(cmd_tab_previous);
95 CMD(cmd_load_current_url);
96 CMD(cmd_bookmark_page);
97 CMD(cmd_list_bookmarks);
100 CMD(cmd_mini_delete_char);
101 CMD(cmd_mini_delete_backward_char);
102 CMD(cmd_mini_kill_line);
104 CMD(cmd_mini_complete_and_exit);
105 CMD(cmd_mini_previous_history_element);
106 CMD(cmd_mini_next_history_element);
110 static void global_key_unbound(void);
111 static void minibuffer_hist_save_entry(void);
112 static void minibuffer_taint_hist(void);
113 static void minibuffer_self_insert(void);
114 static void eecmd_self_insert(void);
115 static void eecmd_select(void);
116 static void ir_self_insert(void);
117 static void ir_select(void);
118 static void lu_self_insert(void);
119 static void lu_select(void);
120 static void bp_select(void);
121 static void yornp_self_insert(void);
122 static void yornp_abort(void);
124 static struct vline *nth_line(struct window*, size_t);
125 static struct tab *current_tab(void);
126 static struct window *current_window(void);
127 static int readkey(void);
128 static void dispatch_stdio(int, short, void*);
129 static void handle_clear_minibuf(int, short, void*);
130 static void handle_resize(int, short, void*);
131 static int wrap_page(struct window*, int);
132 static void print_vline(WINDOW*, struct vline*);
133 static void redraw_tabline(void);
134 static void redraw_window(WINDOW*, int, struct window*);
135 static void redraw_help(void);
136 static void redraw_body(struct tab*);
137 static void redraw_modeline(struct tab*);
138 static void redraw_minibuffer(void);
139 static void redraw_tab(struct tab*);
140 static void emit_help_item(char*, void*);
141 static void rec_compute_help(struct kmap*, char*, size_t);
142 static void recompute_help(void);
143 static void vmessage(const char*, va_list);
144 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
145 static void start_loading_anim(struct tab*);
146 static void update_loading_anim(int, short, void*);
147 static void stop_loading_anim(struct tab*);
148 static void load_url_in_tab(struct tab*, const char*);
149 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
150 static void exit_minibuffer(void);
151 static void switch_to_tab(struct tab*);
152 static struct tab *new_tab(const char*);
153 static void session_new_tab_cb(const char*);
154 static void usage(void);
156 static struct { short meta; int key; uint32_t cp; } thiskey;
158 static WINDOW *tabline, *body, *modeline, *minibuf;
159 static int body_lines, body_cols;
162 static struct window helpwin;
163 static int help_lines, help_cols;
165 static int side_window;
167 static struct event clminibufev;
168 static struct timeval clminibufev_timer = { 5, 0 };
169 static struct timeval loadingev_timer = { 0, 250000 };
171 static uint32_t tab_counter;
173 static char keybuf[64];
175 static void (*yornp_cb)(int, unsigned int);
176 static unsigned int yornp_data;
178 struct kmap global_map,
183 static struct histhead eecmd_history,
187 static int in_minibuffer;
193 void (*donefn)(void);
194 void (*abortfn)(void);
199 struct window window;
201 struct histhead *history;
202 struct hist *hist_cur;
209 } line_prefixes[] = {
210 [LINE_TEXT] = { "", "" },
211 [LINE_LINK] = { "=> ", " " },
212 [LINE_TITLE_1] = { "# ", " " },
213 [LINE_TITLE_2] = { "## ", " " },
214 [LINE_TITLE_3] = { "### ", " " },
215 [LINE_ITEM] = { "* ", " " },
216 [LINE_QUOTE] = { "> ", " " },
217 [LINE_PRE_START] = { "```", " " },
218 [LINE_PRE_CONTENT] = { "", "" },
219 [LINE_PRE_END] = { "```", "```" },
222 static struct line_face {
226 [LINE_TEXT] = { 0, 0 },
227 [LINE_LINK] = { 0, A_UNDERLINE },
228 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
229 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
230 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
231 [LINE_ITEM] = { 0, 0 },
232 [LINE_QUOTE] = { 0, A_DIM },
233 [LINE_PRE_START] = { 0, 0 },
234 [LINE_PRE_CONTENT] = { 0, 0 },
235 [LINE_PRE_END] = { 0, 0 },
238 static struct tab_face {
239 int background, tab, current_tab;
241 A_REVERSE, A_REVERSE, A_NORMAL
245 global_set_key(const char *key, void (*fn)(struct window*))
247 if (!kmap_define_key(&global_map, key, fn))
252 minibuffer_set_key(const char *key, void (*fn)(struct window*))
254 if (!kmap_define_key(&minibuffer_map, key, fn))
259 load_default_keys(void)
261 /* === global map === */
264 global_set_key("C-p", cmd_previous_line);
265 global_set_key("C-n", cmd_next_line);
266 global_set_key("C-f", cmd_forward_char);
267 global_set_key("C-b", cmd_backward_char);
268 global_set_key("M-{", cmd_backward_paragraph);
269 global_set_key("M-}", cmd_forward_paragraph);
270 global_set_key("C-a", cmd_move_beginning_of_line);
271 global_set_key("C-e", cmd_move_end_of_line);
273 global_set_key("M-v", cmd_scroll_up);
274 global_set_key("C-v", cmd_scroll_down);
275 global_set_key("M-space", cmd_scroll_up);
276 global_set_key("space", cmd_scroll_down);
278 global_set_key("M-<", cmd_beginning_of_buffer);
279 global_set_key("M->", cmd_end_of_buffer);
281 global_set_key("C-x C-c", cmd_kill_telescope);
283 global_set_key("C-g", cmd_clear_minibuf);
285 global_set_key("M-x", cmd_execute_extended_command);
286 global_set_key("C-x C-f", cmd_load_url);
287 global_set_key("C-x M-f", cmd_load_current_url);
289 global_set_key("C-x t 0", cmd_tab_close);
290 global_set_key("C-x t 1", cmd_tab_close_other);
291 global_set_key("C-x t 2", cmd_tab_new);
292 global_set_key("C-x t o", cmd_tab_next);
293 global_set_key("C-x t O", cmd_tab_previous);
294 global_set_key("C-x t m", cmd_tab_move);
295 global_set_key("C-x t M", cmd_tab_move_to);
297 global_set_key("C-M-b", cmd_previous_page);
298 global_set_key("C-M-f", cmd_next_page);
300 global_set_key("<f7> a", cmd_bookmark_page);
301 global_set_key("<f7> <f7>", cmd_list_bookmarks);
304 global_set_key("k", cmd_previous_line);
305 global_set_key("j", cmd_next_line);
306 global_set_key("l", cmd_forward_char);
307 global_set_key("h", cmd_backward_char);
308 global_set_key("{", cmd_backward_paragraph);
309 global_set_key("}", cmd_forward_paragraph);
310 global_set_key("^", cmd_move_beginning_of_line);
311 global_set_key("$", cmd_move_end_of_line);
313 global_set_key("K", cmd_scroll_line_up);
314 global_set_key("J", cmd_scroll_line_down);
316 global_set_key("g g", cmd_beginning_of_buffer);
317 global_set_key("G", cmd_end_of_buffer);
319 global_set_key("g D", cmd_tab_close);
320 global_set_key("g N", cmd_tab_new);
321 global_set_key("g t", cmd_tab_next);
322 global_set_key("g T", cmd_tab_previous);
323 global_set_key("g M-t", cmd_tab_move);
324 global_set_key("g M-T", cmd_tab_move_to);
326 global_set_key("H", cmd_previous_page);
327 global_set_key("L", cmd_next_page);
330 global_set_key("q", cmd_kill_telescope);
332 global_set_key("esc", cmd_clear_minibuf);
334 global_set_key(":", cmd_execute_extended_command);
337 global_set_key("<up>", cmd_previous_line);
338 global_set_key("<down>", cmd_next_line);
339 global_set_key("<right>", cmd_forward_char);
340 global_set_key("<left>", cmd_backward_char);
341 global_set_key("<prior>", cmd_scroll_up);
342 global_set_key("<next>", cmd_scroll_down);
344 global_set_key("M-<left>", cmd_previous_page);
345 global_set_key("M-<right>", cmd_next_page);
347 /* "ncurses standard" */
348 global_set_key("C-l", cmd_redraw);
351 global_set_key("<f1>", cmd_toggle_help);
352 global_set_key("C-m", cmd_push_button);
353 global_set_key("M-enter", cmd_push_button_new_tab);
354 global_set_key("M-tab", cmd_previous_button);
355 global_set_key("tab", cmd_next_button);
357 /* === minibuffer map === */
358 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
359 minibuffer_set_key("C-g", cmd_mini_abort);
360 minibuffer_set_key("esc", cmd_mini_abort);
361 minibuffer_set_key("C-d", cmd_mini_delete_char);
362 minibuffer_set_key("del", cmd_mini_delete_backward_char);
363 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
364 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
366 minibuffer_set_key("C-b", cmd_backward_char);
367 minibuffer_set_key("C-f", cmd_forward_char);
368 minibuffer_set_key("<left>", cmd_backward_char);
369 minibuffer_set_key("<right>", cmd_forward_char);
370 minibuffer_set_key("C-e", cmd_move_end_of_line);
371 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
372 minibuffer_set_key("<end>", cmd_move_end_of_line);
373 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
374 minibuffer_set_key("C-k", cmd_mini_kill_line);
376 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
377 minibuffer_set_key("M-n", cmd_mini_next_history_element);
378 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
379 minibuffer_set_key("<down>", cmd_mini_next_history_element);
383 restore_cursor(struct window *window)
388 vl = window->current_line;
389 if (vl == NULL || vl->line == NULL)
390 window->curs_x = window->cpoff = 0;
392 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
395 prfx = line_prefixes[vl->parent->type].prfx1;
396 window->curs_x += utf8_swidth(prfx);
401 cmd_previous_line(struct window *window)
405 if (window->current_line == NULL
406 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
409 if (--window->curs_y < 0) {
411 cmd_scroll_line_up(window);
415 window->current_line = vl;
416 restore_cursor(window);
420 cmd_next_line(struct window *window)
424 if (window->current_line == NULL
425 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
428 if (++window->curs_y > body_lines-1) {
429 window->curs_y = body_lines-1;
430 cmd_scroll_line_down(window);
434 window->current_line = vl;
435 restore_cursor(window);
439 cmd_backward_char(struct window *window)
441 if (window->cpoff != 0)
443 restore_cursor(window);
447 cmd_forward_char(struct window *window)
451 len = utf8_cplen(ministate.buf);
452 if (++window->cpoff > len)
454 restore_cursor(window);
458 cmd_backward_paragraph(struct window *window)
461 if (window->current_line == NULL ||
462 window->current_line == TAILQ_FIRST(&window->head)) {
463 message("No previous paragraph");
466 cmd_previous_line(window);
467 } while (window->current_line->line != NULL ||
468 window->current_line->parent->type != LINE_TEXT);
472 cmd_forward_paragraph(struct window *window)
475 if (window->current_line == NULL ||
476 window->current_line == TAILQ_LAST(&window->head, vhead)) {
477 message("No next paragraph");
480 cmd_next_line(window);
481 } while (window->current_line->line != NULL ||
482 window->current_line->parent->type != LINE_TEXT);
486 cmd_move_beginning_of_line(struct window *window)
489 restore_cursor(window);
493 cmd_move_end_of_line(struct window *window)
497 vl = window->current_line;
498 if (vl->line == NULL)
500 window->cpoff = utf8_cplen(vl->line);
501 restore_cursor(window);
505 cmd_redraw(struct window *window)
507 handle_resize(0, 0, NULL);
511 cmd_scroll_line_up(struct window *window)
515 if (window->line_off == 0)
518 vl = nth_line(window, --window->line_off);
521 print_vline(body, vl);
523 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
524 restore_cursor(window);
528 cmd_scroll_line_down(struct window *window)
532 vl = window->current_line;
533 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
535 window->current_line = vl;
540 if (window->line_max - window->line_off < (size_t)body_lines)
543 vl = nth_line(window, window->line_off + body_lines-1);
544 wmove(body, body_lines-1, 0);
545 print_vline(body, vl);
547 restore_cursor(window);
551 cmd_scroll_up(struct window *window)
557 for (; off > 0; --off)
558 cmd_scroll_line_up(window);
562 cmd_scroll_down(struct window *window)
568 for (; off > 0; --off)
569 cmd_scroll_line_down(window);
573 cmd_beginning_of_buffer(struct window *window)
575 window->current_line = TAILQ_FIRST(&window->head);
576 window->line_off = 0;
579 restore_cursor(window);
583 cmd_end_of_buffer(struct window *window)
587 off = window->line_max - body_lines;
590 window->line_off = off;
591 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
593 window->current_line = TAILQ_LAST(&window->head, vhead);
594 window->cpoff = body_cols;
595 restore_cursor(window);
599 cmd_kill_telescope(struct window *window)
606 cmd_push_button(struct window *window)
611 nth = window->line_off + window->curs_y;
612 if (nth >= window->line_max)
614 vl = nth_line(window, nth);
615 if (vl->parent->type != LINE_LINK)
618 load_url_in_tab(current_tab(), vl->parent->alt);
622 cmd_push_button_new_tab(struct window *window)
627 nth = window->line_off + window->curs_y;
628 if (nth > window->line_max)
630 vl = nth_line(window, nth);
631 if (vl->parent->type != LINE_LINK)
634 new_tab(vl->parent->alt);
638 cmd_previous_button(struct window *window)
641 if (window->current_line == NULL ||
642 window->current_line == TAILQ_FIRST(&window->head)) {
643 message("No previous link");
646 cmd_previous_line(window);
647 } while (window->current_line->parent->type != LINE_LINK);
651 cmd_next_button(struct window *window)
654 if (window->current_line == NULL ||
655 window->current_line == TAILQ_LAST(&window->head, vhead)) {
656 message("No next link");
659 cmd_next_line(window);
660 } while (window->current_line->parent->type != LINE_LINK);
664 cmd_previous_page(struct window *window)
666 struct tab *tab = current_tab();
668 if (!load_previous_page(tab))
669 message("No previous page");
671 start_loading_anim(tab);
675 cmd_next_page(struct window *window)
677 struct tab *tab = current_tab();
679 if (!load_next_page(tab))
680 message("No next page");
682 start_loading_anim(tab);
686 cmd_clear_minibuf(struct window *window)
688 handle_clear_minibuf(0, 0, NULL);
692 cmd_execute_extended_command(struct window *window)
697 message("We don't have enable-recursive-minibuffers");
701 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
704 len = sizeof(ministate.prompt);
705 strlcpy(ministate.prompt, "", len);
708 strlcat(ministate.prompt, "M-", len);
710 strlcat(ministate.prompt, keyname(thiskey.key), len);
711 strlcat(ministate.prompt, " ", len);
715 cmd_tab_close(struct window *window)
720 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
721 TAILQ_NEXT(tab, tabs) == NULL) {
722 message("Can't close the only tab.");
726 if (evtimer_pending(&tab->loadingev, NULL))
727 evtimer_del(&tab->loadingev);
731 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
732 t = TAILQ_NEXT(tab, tabs);
733 TAILQ_REMOVE(&tabshead, tab, tabs);
740 cmd_tab_close_other(struct window *window)
744 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
745 if (t->flags & TAB_CURRENT)
749 TAILQ_REMOVE(&tabshead, t, tabs);
755 cmd_tab_new(struct window *window)
757 new_tab(NEW_TAB_URL);
761 cmd_tab_next(struct window *window)
766 tab->flags &= ~TAB_CURRENT;
768 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
769 t = TAILQ_FIRST(&tabshead);
770 t->flags |= TAB_CURRENT;
774 cmd_tab_previous(struct window *window)
779 tab->flags &= ~TAB_CURRENT;
781 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
782 t = TAILQ_LAST(&tabshead, tabshead);
783 t->flags |= TAB_CURRENT;
787 cmd_tab_move(struct window *window)
792 t = TAILQ_NEXT(tab, tabs);
793 TAILQ_REMOVE(&tabshead, tab, tabs);
796 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
798 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
802 cmd_tab_move_to(struct window *window)
807 t = TAILQ_PREV(tab, tabshead, tabs);
808 TAILQ_REMOVE(&tabshead, tab, tabs);
811 if (TAILQ_EMPTY(&tabshead))
812 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
814 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
816 TAILQ_INSERT_BEFORE(t, tab, tabs);
820 cmd_load_url(struct window *window)
823 message("We don't have enable-recursive-minibuffers");
827 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
829 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
830 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
831 cmd_move_end_of_line(&ministate.window);
835 cmd_load_current_url(struct window *window)
837 struct tab *tab = current_tab();
840 message("We don't have enable-recursive-minibuffers");
844 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
846 strlcpy(ministate.prompt, "Load 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_bookmark_page(struct window *window)
854 struct tab *tab = current_tab();
856 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
857 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
858 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
859 ministate.window.cpoff = utf8_cplen(ministate.buf);
863 cmd_list_bookmarks(struct window *window)
865 load_url_in_tab(current_tab(), "about:bookmarks");
869 cmd_toggle_help(struct window *window)
871 side_window = !side_window;
875 /* ugly hack, but otherwise the window doesn't get updated
876 * until I call handle_resize a second time (i.e. C-l). I
877 * will be happy to know why something like this is needed. */
878 handle_resize(0, 0, NULL);
879 handle_resize(0, 0, NULL);
883 cmd_mini_delete_char(struct window *window)
887 if (!in_minibuffer) {
888 message("text is read-only");
892 minibuffer_taint_hist();
894 c = utf8_nth(window->current_line->line, window->cpoff);
899 memmove(c, n, strlen(n)+1);
903 cmd_mini_delete_backward_char(struct window *window)
907 if (!in_minibuffer) {
908 message("text is read-only");
912 minibuffer_taint_hist();
914 c = utf8_nth(window->current_line->line, window->cpoff);
915 start = window->current_line->line;
918 p = utf8_prev_cp(c-1, start);
920 memmove(p, c, strlen(c)+1);
925 cmd_mini_kill_line(struct window *window)
929 if (!in_minibuffer) {
930 message("text is read-only");
934 minibuffer_taint_hist();
935 c = utf8_nth(window->current_line->line, window->cpoff);
940 cmd_mini_abort(struct window *window)
949 cmd_mini_complete_and_exit(struct window *window)
954 minibuffer_taint_hist();
959 cmd_mini_previous_history_element(struct window *window)
961 if (ministate.history == NULL) {
962 message("No history");
966 if (ministate.hist_cur == NULL ||
967 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
968 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
969 ministate.hist_off = ministate.history->len - 1;
970 if (ministate.hist_cur == NULL)
971 message("No prev item");
973 ministate.hist_off--;
976 if (ministate.hist_cur != NULL)
977 window->current_line->line = ministate.hist_cur->h;
981 cmd_mini_next_history_element(struct window *window)
983 if (ministate.history == NULL) {
984 message("No history");
988 if (ministate.hist_cur == NULL ||
989 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
990 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
991 ministate.hist_off = 0;
992 if (ministate.hist_cur == NULL)
993 message("No next item");
995 ministate.hist_off++;
998 if (ministate.hist_cur != NULL)
999 window->current_line->line = ministate.hist_cur->h;
1003 global_key_unbound(void)
1005 message("%s is undefined", keybuf);
1009 minibuffer_hist_save_entry(void)
1013 if (ministate.history == NULL)
1016 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1019 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1021 if (TAILQ_EMPTY(&ministate.history->head))
1022 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1024 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1025 ministate.history->len++;
1029 * taint the minibuffer cache: if we're currently showing a history
1030 * element, copy that to the current buf and reset the "history
1031 * navigation" thing.
1034 minibuffer_taint_hist(void)
1036 if (ministate.hist_cur == NULL)
1039 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1040 ministate.hist_cur = NULL;
1044 minibuffer_self_insert(void)
1046 char *c, tmp[5] = {0};
1049 minibuffer_taint_hist();
1051 if (thiskey.cp == 0)
1054 len = utf8_encode(thiskey.cp, tmp);
1055 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1056 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1059 memmove(c + len, c, strlen(c)+1);
1060 memcpy(c, tmp, len);
1061 ministate.window.cpoff++;
1065 eecmd_self_insert(void)
1067 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1068 !unicode_isgraph(thiskey.cp)) {
1069 global_key_unbound();
1073 minibuffer_self_insert();
1081 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1082 if (!strcmp(cmd->cmd, ministate.buf)) {
1084 minibuffer_hist_save_entry();
1085 cmd->fn(current_window());
1090 message("No match");
1094 ir_self_insert(void)
1096 minibuffer_self_insert();
1102 char buf[1025] = {0};
1103 struct phos_uri uri;
1106 tab = current_tab();
1109 minibuffer_hist_save_entry();
1111 /* a bit ugly but... */
1112 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1113 phos_uri_set_query(&uri, ministate.buf);
1114 phos_serialize_uri(&uri, buf, sizeof(buf));
1115 load_url_in_tab(tab, buf);
1119 lu_self_insert(void)
1121 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1122 !unicode_isgraph(thiskey.key)) {
1123 global_key_unbound();
1127 minibuffer_self_insert();
1134 minibuffer_hist_save_entry();
1135 load_url_in_tab(current_tab(), ministate.buf);
1142 if (*ministate.buf != '\0')
1143 add_to_bookmarks(ministate.buf);
1149 yornp_self_insert(void)
1151 if (thiskey.key != 'y' && thiskey.key != 'n') {
1152 message("Please answer y or n");
1157 yornp_cb(thiskey.key == 'y', yornp_data);
1164 yornp_cb(0, yornp_data);
1167 static struct vline *
1168 nth_line(struct window *window, size_t n)
1174 TAILQ_FOREACH(vl, &window->head, vlines) {
1189 TAILQ_FOREACH(t, &tabshead, tabs) {
1190 if (t->flags & TAB_CURRENT)
1198 static struct window *
1199 current_window(void)
1202 return &ministate.window;
1203 return ¤t_tab()->window;
1211 if ((thiskey.key = wgetch(body)) == ERR)
1214 thiskey.meta = thiskey.key == 27;
1216 thiskey.key = wgetch(body);
1217 if (thiskey.key == ERR || thiskey.key == 27) {
1224 if ((unsigned int)thiskey.key < UINT8_MAX) {
1226 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1228 if ((thiskey.key = wgetch(body)) == ERR) {
1229 message("Error decoding user input");
1239 dispatch_stdio(int fd, short ev, void *d)
1242 const char *keyname;
1248 if (keybuf[0] != '\0')
1249 strlcat(keybuf, " ", sizeof(keybuf));
1251 strlcat(keybuf, "M-", sizeof(keybuf));
1252 if (thiskey.cp != 0) {
1253 utf8_encode(thiskey.cp, tmp);
1254 strlcat(keybuf, tmp, sizeof(keybuf));
1256 if ((keyname = unkbd(thiskey.key)) != NULL)
1257 strlcat(keybuf, keyname, sizeof(keybuf));
1259 tmp[0] = thiskey.key;
1260 strlcat(keybuf, tmp, sizeof(keybuf));
1264 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1265 if (k->meta == thiskey.meta &&
1266 k->key == thiskey.key) {
1268 current_map = &k->map;
1270 current_map = base_map;
1271 strlcpy(keybuf, "", sizeof(keybuf));
1272 k->fn(current_window());
1278 if (current_map->unhandled_input != NULL)
1279 current_map->unhandled_input();
1281 global_key_unbound();
1284 strlcpy(keybuf, "", sizeof(keybuf));
1285 current_map = base_map;
1291 redraw_tab(current_tab());
1295 handle_clear_minibuf(int fd, short ev, void *d)
1297 free(ministate.curmesg);
1298 ministate.curmesg = NULL;
1300 redraw_minibuffer();
1301 if (in_minibuffer) {
1311 handle_resize(int sig, short ev, void *d)
1319 /* move and resize the windows, in reverse order! */
1321 mvwin(minibuf, LINES-1, 0);
1322 wresize(minibuf, 1, COLS);
1324 mvwin(modeline, LINES-2, 0);
1325 wresize(modeline, 1, COLS);
1327 body_lines = LINES-3;
1331 help_cols = 0.3 * COLS;
1332 help_lines = LINES-3;
1334 wresize(help, help_lines, help_cols);
1336 wrap_page(&helpwin, help_cols);
1338 body_cols = COLS - help_cols - 1;
1339 mvwin(body, 1, help_cols);
1343 wresize(body, body_lines, body_cols);
1345 wresize(tabline, 1, COLS);
1347 tab = current_tab();
1349 wrap_page(&tab->window, body_cols);
1354 wrap_page(struct window *window, int width)
1357 const struct line *orig;
1361 orig = window->current_line == NULL
1363 : window->current_line->parent;
1364 window->current_line = NULL;
1367 window->line_off = 0;
1369 empty_vlist(window);
1371 TAILQ_FOREACH(l, &window->page.head, lines) {
1372 prfx = line_prefixes[l->type].prfx1;
1381 case LINE_PRE_START:
1383 wrap_text(window, prfx, l, width);
1385 case LINE_PRE_CONTENT:
1386 hardwrap_text(window, l, width);
1390 if (orig == l && window->current_line == NULL) {
1391 window->line_off = window->line_max-1;
1392 window->current_line = TAILQ_LAST(&window->head, vhead);
1395 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1396 if (vl == NULL || vl->parent != orig)
1398 window->current_line = vl;
1404 if (window->current_line == NULL)
1405 window->current_line = TAILQ_FIRST(&window->head);
1411 print_vline(WINDOW *window, struct vline *vl)
1413 const char *text = vl->line;
1415 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1416 int text_face = line_faces[vl->parent->type].text_prop;
1419 prfx = line_prefixes[vl->parent->type].prfx1;
1421 prfx = line_prefixes[vl->parent->type].prfx2;
1426 wattron(window, prefix_face);
1427 wprintw(window, "%s", prfx);
1428 wattroff(window, prefix_face);
1430 wattron(window, text_face);
1431 wprintw(window, "%s", text);
1432 wattroff(window, text_face);
1436 redraw_tabline(void)
1439 size_t toskip, ots, tabwidth, space, x;
1440 int current, y, truncated;
1444 tabwidth = sizeof(buf)+1;
1448 TAILQ_FOREACH(tab, &tabshead, tabs) {
1450 if (tab->flags & TAB_CURRENT)
1454 if (toskip * tabwidth < space)
1459 while (toskip != 0 &&
1460 (ots - toskip+1) * tabwidth < space)
1465 wattron(tabline, tab_face.background);
1466 wprintw(tabline, toskip == 0 ? " " : "<");
1467 wattroff(tabline, tab_face.background);
1470 TAILQ_FOREACH(tab, &tabshead, tabs) {
1478 getyx(tabline, y, x);
1479 if (x + sizeof(buf)+2 >= (size_t)COLS)
1482 current = tab->flags & TAB_CURRENT;
1484 if (*(title = tab->window.page.title) == '\0')
1485 title = tab->hist_cur->h;
1487 strlcpy(buf, " ", sizeof(buf));
1488 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1489 /* truncation happens */
1490 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1492 /* pad with spaces */
1493 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1498 wattron(tabline, tab_face.current_tab);
1500 wattron(tabline, tab_face.tab);
1502 wprintw(tabline, "%s", buf);
1503 if (TAILQ_NEXT(tab, tabs) != NULL)
1504 wprintw(tabline, " ");
1507 wattroff(tabline, tab_face.current_tab);
1509 wattroff(tabline, tab_face.tab);
1512 wattron(tabline, tab_face.background);
1513 for (; x < (size_t)COLS; ++x)
1514 waddch(tabline, ' ');
1516 mvwprintw(tabline, 0, COLS-1, ">");
1520 redraw_window(WINDOW *win, int height, struct window *window)
1527 window->line_off = MIN(window->line_max-1, window->line_off);
1528 if (TAILQ_EMPTY(&window->head))
1532 vl = nth_line(window, window->line_off);
1533 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1535 print_vline(win, vl);
1541 wmove(win, window->curs_y, window->curs_x);
1547 redraw_window(help, help_lines, &helpwin);
1551 redraw_body(struct tab *tab)
1553 redraw_window(body, body_lines, &tab->window);
1557 trust_status_char(enum trust_state ts)
1560 case TS_UNKNOWN: return 'u';
1561 case TS_UNTRUSTED: return '!';
1562 case TS_TRUSTED: return 'v';
1563 case TS_VERIFIED: return 'V';
1568 redraw_modeline(struct tab *tab)
1571 int x, y, max_x, max_y;
1572 const char *mode = tab->window.page.name;
1573 const char *spin = "-\\|/";
1576 wattron(modeline, A_REVERSE);
1577 wmove(modeline, 0, 0);
1579 wprintw(modeline, "-%c%c %s ",
1580 spin[tab->loading_anim_step],
1581 trust_status_char(tab->trust),
1582 mode == NULL ? "(none)" : mode);
1584 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1586 if (tab->window.line_max <= (size_t)body_lines)
1587 wprintw(modeline, "All ");
1588 else if (tab->window.line_off == 0)
1589 wprintw(modeline, "Top ");
1590 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1591 wprintw(modeline, "Bottom ");
1593 wprintw(modeline, "%.0f%% ", pct);
1595 wprintw(modeline, "%d/%d %s ",
1596 tab->window.line_off + tab->window.curs_y,
1597 tab->window.line_max,
1600 getyx(modeline, y, x);
1601 getmaxyx(modeline, max_y, max_x);
1606 for (; x < max_x; ++x)
1607 waddstr(modeline, "-");
1611 redraw_minibuffer(void)
1614 size_t off_y, off_x = 0;
1619 if (in_minibuffer) {
1620 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1621 if (ministate.hist_cur != NULL)
1622 wprintw(minibuf, "(%zu/%zu) ",
1623 ministate.hist_off + 1,
1624 ministate.history->len);
1626 getyx(minibuf, off_y, off_x);
1628 start = ministate.hist_cur != NULL
1629 ? ministate.hist_cur->h
1631 c = utf8_nth(ministate.window.current_line->line,
1632 ministate.window.cpoff);
1633 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1634 start = utf8_next_cp(start);
1637 waddstr(minibuf, start);
1640 if (ministate.curmesg != NULL)
1641 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1644 if (!in_minibuffer && ministate.curmesg == NULL)
1645 waddstr(minibuf, keybuf);
1647 /* If nothing else, show the URL at point */
1648 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1649 tab = current_tab();
1650 if (tab->window.current_line != NULL &&
1651 tab->window.current_line->parent->type == LINE_LINK)
1652 waddstr(minibuf, tab->window.current_line->parent->alt);
1656 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1660 redraw_tab(struct tab *tab)
1669 redraw_modeline(tab);
1670 redraw_minibuffer();
1672 wnoutrefresh(tabline);
1673 wnoutrefresh(modeline);
1675 if (in_minibuffer) {
1677 wnoutrefresh(minibuf);
1679 wnoutrefresh(minibuf);
1687 emit_help_item(char *prfx, void *fn)
1692 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1696 assert(cmd != NULL);
1698 if ((l = calloc(1, sizeof(*l))) == NULL)
1701 l->type = LINE_TEXT;
1704 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1706 if (TAILQ_EMPTY(&helpwin.page.head))
1707 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1709 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1713 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1719 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1720 strlcpy(p, prfx, sizeof(p));
1722 strlcat(p, " ", sizeof(p));
1724 strlcat(p, "M-", sizeof(p));
1725 if ((kn = unkbd(k->key)) != NULL)
1726 strlcat(p, kn, sizeof(p));
1728 strlcat(p, keyname(k->key), sizeof(p));
1731 rec_compute_help(&k->map, p, sizeof(p));
1733 emit_help_item(p, k->fn);
1738 recompute_help(void)
1742 empty_vlist(&helpwin);
1743 empty_linelist(&helpwin);
1744 rec_compute_help(current_map, p, sizeof(p));
1745 wrap_page(&helpwin, help_cols);
1749 vmessage(const char *fmt, va_list ap)
1751 if (evtimer_pending(&clminibufev, NULL))
1752 evtimer_del(&clminibufev);
1753 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1754 evtimer_add(&clminibufev, &clminibufev_timer);
1756 free(ministate.curmesg);
1758 /* TODO: what to do if the allocation fails here? */
1759 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1760 ministate.curmesg = NULL;
1762 redraw_minibuffer();
1763 if (in_minibuffer) {
1773 message(const char *fmt, ...)
1783 start_loading_anim(struct tab *tab)
1785 if (tab->loading_anim)
1787 tab->loading_anim = 1;
1788 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1789 evtimer_add(&tab->loadingev, &loadingev_timer);
1793 update_loading_anim(int fd, short ev, void *d)
1795 struct tab *tab = d;
1797 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1799 if (tab->flags & TAB_CURRENT) {
1800 redraw_modeline(tab);
1807 evtimer_add(&tab->loadingev, &loadingev_timer);
1811 stop_loading_anim(struct tab *tab)
1813 if (!tab->loading_anim)
1815 evtimer_del(&tab->loadingev);
1816 tab->loading_anim = 0;
1817 tab->loading_anim_step = 0;
1819 if (!(tab->flags & TAB_CURRENT))
1822 redraw_modeline(tab);
1831 load_url_in_tab(struct tab *tab, const char *url)
1833 message("Loading %s...", url);
1834 start_loading_anim(tab);
1837 tab->window.curs_x = 0;
1838 tab->window.curs_y = 0;
1843 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1844 void (*abortfn)(void), struct histhead *hist)
1847 base_map = &minibuffer_map;
1848 current_map = &minibuffer_map;
1850 base_map->unhandled_input = self_insert_fn;
1852 ministate.donefn = donefn;
1853 ministate.abortfn = abortfn;
1854 memset(ministate.buf, 0, sizeof(ministate.buf));
1855 ministate.window.current_line = &ministate.vline;
1856 ministate.window.current_line->line = ministate.buf;
1857 ministate.window.cpoff = 0;
1858 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1860 ministate.history = hist;
1861 ministate.hist_cur = NULL;
1862 ministate.hist_off = 0;
1866 exit_minibuffer(void)
1871 base_map = &global_map;
1872 current_map = &global_map;
1876 switch_to_tab(struct tab *tab)
1880 TAILQ_FOREACH(t, &tabshead, tabs) {
1881 t->flags &= ~TAB_CURRENT;
1884 tab->flags |= TAB_CURRENT;
1890 return tab_counter++;
1894 new_tab(const char *url)
1898 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1903 TAILQ_INIT(&tab->hist.head);
1905 TAILQ_INIT(&tab->window.head);
1907 tab->id = tab_new_id();
1910 if (TAILQ_EMPTY(&tabshead))
1911 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1913 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1915 load_url_in_tab(tab, url);
1920 session_new_tab_cb(const char *url)
1928 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1932 ui_init(int argc, char * const *argv)
1934 const char *url = NEW_TAB_URL;
1937 while ((ch = getopt(argc, argv, "")) != -1) {
1950 setlocale(LC_ALL, "");
1952 TAILQ_INIT(&global_map.m);
1953 global_map.unhandled_input = global_key_unbound;
1955 TAILQ_INIT(&minibuffer_map.m);
1957 TAILQ_INIT(&eecmd_history.head);
1958 TAILQ_INIT(&ir_history.head);
1959 TAILQ_INIT(&lu_history.head);
1961 ministate.line.type = LINE_TEXT;
1962 ministate.vline.parent = &ministate.line;
1963 ministate.window.current_line = &ministate.vline;
1965 /* initialize help window */
1966 TAILQ_INIT(&helpwin.head);
1968 base_map = &global_map;
1969 current_map = &global_map;
1970 load_default_keys();
1977 intrflush(stdscr, FALSE);
1979 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1981 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1983 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1985 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1987 if ((help = newwin(1, 1, 1, 0)) == NULL)
1990 body_lines = LINES-3;
1994 scrollok(body, TRUE);
1996 /* non-blocking input */
1999 mvwprintw(body, 0, 0, "");
2001 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2002 event_add(&stdioev, NULL);
2004 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2005 signal_add(&winchev, NULL);
2007 load_last_session(session_new_tab_cb);
2008 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2015 ui_on_tab_loaded(struct tab *tab)
2017 stop_loading_anim(tab);
2018 message("Loaded %s", tab->hist_cur->h);
2029 ui_on_tab_refresh(struct tab *tab)
2031 wrap_page(&tab->window, body_cols);
2032 if (tab->flags & TAB_CURRENT) {
2033 restore_cursor(&tab->window);
2039 ui_require_input(struct tab *tab, int hide)
2041 /* TODO: hard-switching to another tab is ugly */
2044 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2046 strlcpy(ministate.prompt, "Input required: ",
2047 sizeof(ministate.prompt));
2052 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2057 if (in_minibuffer) {
2064 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2067 len = sizeof(ministate.prompt);
2068 strlcpy(ministate.prompt, prompt, len);
2069 strlcat(ministate.prompt, " (y or n) ", len);
2070 redraw_tab(current_tab());
2074 ui_notify(const char *fmt, ...)