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 void handle_resize_timeout(int, short, void*);
132 static int wrap_page(struct window*, int);
133 static void print_vline(WINDOW*, struct vline*);
134 static void redraw_tabline(void);
135 static void redraw_window(WINDOW*, int, struct window*);
136 static void redraw_help(void);
137 static void redraw_body(struct tab*);
138 static void redraw_modeline(struct tab*);
139 static void redraw_minibuffer(void);
140 static void redraw_tab(struct tab*);
141 static void emit_help_item(char*, void*);
142 static void rec_compute_help(struct kmap*, char*, size_t);
143 static void recompute_help(void);
144 static void vmessage(const char*, va_list);
145 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
146 static void start_loading_anim(struct tab*);
147 static void update_loading_anim(int, short, void*);
148 static void stop_loading_anim(struct tab*);
149 static void load_url_in_tab(struct tab*, const char*);
150 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
151 static void exit_minibuffer(void);
152 static void switch_to_tab(struct tab*);
153 static struct tab *new_tab(const char*);
154 static void session_new_tab_cb(const char*);
155 static void usage(void);
157 static struct { short meta; int key; uint32_t cp; } thiskey;
159 static struct event resizeev;
160 static struct timeval resize_timer = { 0, 250000 };
162 static WINDOW *tabline, *body, *modeline, *minibuf;
163 static int body_lines, body_cols;
166 static struct window helpwin;
167 static int help_lines, help_cols;
169 static int side_window;
171 static struct event clminibufev;
172 static struct timeval clminibufev_timer = { 5, 0 };
173 static struct timeval loadingev_timer = { 0, 250000 };
175 static uint32_t tab_counter;
177 static char keybuf[64];
179 static void (*yornp_cb)(int, unsigned int);
180 static unsigned int yornp_data;
182 struct kmap global_map,
187 static struct histhead eecmd_history,
191 static int in_minibuffer;
197 void (*donefn)(void);
198 void (*abortfn)(void);
203 struct window window;
205 struct histhead *history;
206 struct hist *hist_cur;
213 } line_prefixes[] = {
214 [LINE_TEXT] = { "", "" },
215 [LINE_LINK] = { "=> ", " " },
216 [LINE_TITLE_1] = { "# ", " " },
217 [LINE_TITLE_2] = { "## ", " " },
218 [LINE_TITLE_3] = { "### ", " " },
219 [LINE_ITEM] = { "* ", " " },
220 [LINE_QUOTE] = { "> ", " " },
221 [LINE_PRE_START] = { "```", " " },
222 [LINE_PRE_CONTENT] = { "", "" },
223 [LINE_PRE_END] = { "```", "```" },
226 static struct line_face {
230 [LINE_TEXT] = { 0, 0 },
231 [LINE_LINK] = { 0, A_UNDERLINE },
232 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
233 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
234 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
235 [LINE_ITEM] = { 0, 0 },
236 [LINE_QUOTE] = { 0, A_DIM },
237 [LINE_PRE_START] = { 0, 0 },
238 [LINE_PRE_CONTENT] = { 0, 0 },
239 [LINE_PRE_END] = { 0, 0 },
242 static struct tab_face {
243 int background, tab, current_tab;
245 A_REVERSE, A_REVERSE, A_NORMAL
249 global_set_key(const char *key, void (*fn)(struct window*))
251 if (!kmap_define_key(&global_map, key, fn))
256 minibuffer_set_key(const char *key, void (*fn)(struct window*))
258 if (!kmap_define_key(&minibuffer_map, key, fn))
263 load_default_keys(void)
265 /* === global map === */
268 global_set_key("C-p", cmd_previous_line);
269 global_set_key("C-n", cmd_next_line);
270 global_set_key("C-f", cmd_forward_char);
271 global_set_key("C-b", cmd_backward_char);
272 global_set_key("M-{", cmd_backward_paragraph);
273 global_set_key("M-}", cmd_forward_paragraph);
274 global_set_key("C-a", cmd_move_beginning_of_line);
275 global_set_key("C-e", cmd_move_end_of_line);
277 global_set_key("M-v", cmd_scroll_up);
278 global_set_key("C-v", cmd_scroll_down);
279 global_set_key("M-space", cmd_scroll_up);
280 global_set_key("space", cmd_scroll_down);
282 global_set_key("M-<", cmd_beginning_of_buffer);
283 global_set_key("M->", cmd_end_of_buffer);
285 global_set_key("C-x C-c", cmd_kill_telescope);
287 global_set_key("C-g", cmd_clear_minibuf);
289 global_set_key("M-x", cmd_execute_extended_command);
290 global_set_key("C-x C-f", cmd_load_url);
291 global_set_key("C-x M-f", cmd_load_current_url);
293 global_set_key("C-x t 0", cmd_tab_close);
294 global_set_key("C-x t 1", cmd_tab_close_other);
295 global_set_key("C-x t 2", cmd_tab_new);
296 global_set_key("C-x t o", cmd_tab_next);
297 global_set_key("C-x t O", cmd_tab_previous);
298 global_set_key("C-x t m", cmd_tab_move);
299 global_set_key("C-x t M", cmd_tab_move_to);
301 global_set_key("C-M-b", cmd_previous_page);
302 global_set_key("C-M-f", cmd_next_page);
304 global_set_key("<f7> a", cmd_bookmark_page);
305 global_set_key("<f7> <f7>", cmd_list_bookmarks);
308 global_set_key("k", cmd_previous_line);
309 global_set_key("j", cmd_next_line);
310 global_set_key("l", cmd_forward_char);
311 global_set_key("h", cmd_backward_char);
312 global_set_key("{", cmd_backward_paragraph);
313 global_set_key("}", cmd_forward_paragraph);
314 global_set_key("^", cmd_move_beginning_of_line);
315 global_set_key("$", cmd_move_end_of_line);
317 global_set_key("K", cmd_scroll_line_up);
318 global_set_key("J", cmd_scroll_line_down);
320 global_set_key("g g", cmd_beginning_of_buffer);
321 global_set_key("G", cmd_end_of_buffer);
323 global_set_key("g D", cmd_tab_close);
324 global_set_key("g N", cmd_tab_new);
325 global_set_key("g t", cmd_tab_next);
326 global_set_key("g T", cmd_tab_previous);
327 global_set_key("g M-t", cmd_tab_move);
328 global_set_key("g M-T", cmd_tab_move_to);
330 global_set_key("H", cmd_previous_page);
331 global_set_key("L", cmd_next_page);
334 global_set_key("q", cmd_kill_telescope);
336 global_set_key("esc", cmd_clear_minibuf);
338 global_set_key(":", cmd_execute_extended_command);
341 global_set_key("<up>", cmd_previous_line);
342 global_set_key("<down>", cmd_next_line);
343 global_set_key("<right>", cmd_forward_char);
344 global_set_key("<left>", cmd_backward_char);
345 global_set_key("<prior>", cmd_scroll_up);
346 global_set_key("<next>", cmd_scroll_down);
348 global_set_key("M-<left>", cmd_previous_page);
349 global_set_key("M-<right>", cmd_next_page);
351 /* "ncurses standard" */
352 global_set_key("C-l", cmd_redraw);
355 global_set_key("<f1>", cmd_toggle_help);
356 global_set_key("C-m", cmd_push_button);
357 global_set_key("M-enter", cmd_push_button_new_tab);
358 global_set_key("M-tab", cmd_previous_button);
359 global_set_key("tab", cmd_next_button);
361 /* === minibuffer map === */
362 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
363 minibuffer_set_key("C-g", cmd_mini_abort);
364 minibuffer_set_key("esc", cmd_mini_abort);
365 minibuffer_set_key("C-d", cmd_mini_delete_char);
366 minibuffer_set_key("del", cmd_mini_delete_backward_char);
367 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
368 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
370 minibuffer_set_key("C-b", cmd_backward_char);
371 minibuffer_set_key("C-f", cmd_forward_char);
372 minibuffer_set_key("<left>", cmd_backward_char);
373 minibuffer_set_key("<right>", cmd_forward_char);
374 minibuffer_set_key("C-e", cmd_move_end_of_line);
375 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
376 minibuffer_set_key("<end>", cmd_move_end_of_line);
377 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
378 minibuffer_set_key("C-k", cmd_mini_kill_line);
380 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
381 minibuffer_set_key("M-n", cmd_mini_next_history_element);
382 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
383 minibuffer_set_key("<down>", cmd_mini_next_history_element);
387 restore_cursor(struct window *window)
392 vl = window->current_line;
393 if (vl == NULL || vl->line == NULL)
394 window->curs_x = window->cpoff = 0;
396 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
399 prfx = line_prefixes[vl->parent->type].prfx1;
400 window->curs_x += utf8_swidth(prfx);
405 cmd_previous_line(struct window *window)
409 if (window->current_line == NULL
410 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
413 if (--window->curs_y < 0) {
415 cmd_scroll_line_up(window);
419 window->current_line = vl;
420 restore_cursor(window);
424 cmd_next_line(struct window *window)
428 if (window->current_line == NULL
429 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
432 if (++window->curs_y > body_lines-1) {
433 window->curs_y = body_lines-1;
434 cmd_scroll_line_down(window);
438 window->current_line = vl;
439 restore_cursor(window);
443 cmd_backward_char(struct window *window)
445 if (window->cpoff != 0)
447 restore_cursor(window);
451 cmd_forward_char(struct window *window)
455 len = utf8_cplen(ministate.buf);
456 if (++window->cpoff > len)
458 restore_cursor(window);
462 cmd_backward_paragraph(struct window *window)
465 if (window->current_line == NULL ||
466 window->current_line == TAILQ_FIRST(&window->head)) {
467 message("No previous paragraph");
470 cmd_previous_line(window);
471 } while (window->current_line->line != NULL ||
472 window->current_line->parent->type != LINE_TEXT);
476 cmd_forward_paragraph(struct window *window)
479 if (window->current_line == NULL ||
480 window->current_line == TAILQ_LAST(&window->head, vhead)) {
481 message("No next paragraph");
484 cmd_next_line(window);
485 } while (window->current_line->line != NULL ||
486 window->current_line->parent->type != LINE_TEXT);
490 cmd_move_beginning_of_line(struct window *window)
493 restore_cursor(window);
497 cmd_move_end_of_line(struct window *window)
501 vl = window->current_line;
502 if (vl->line == NULL)
504 window->cpoff = utf8_cplen(vl->line);
505 restore_cursor(window);
509 cmd_redraw(struct window *window)
511 handle_resize(0, 0, NULL);
515 cmd_scroll_line_up(struct window *window)
519 if (window->line_off == 0)
522 vl = nth_line(window, --window->line_off);
525 print_vline(body, vl);
527 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
528 restore_cursor(window);
532 cmd_scroll_line_down(struct window *window)
536 vl = window->current_line;
537 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
539 window->current_line = vl;
544 if (window->line_max - window->line_off < (size_t)body_lines)
547 vl = nth_line(window, window->line_off + body_lines-1);
548 wmove(body, body_lines-1, 0);
549 print_vline(body, vl);
551 restore_cursor(window);
555 cmd_scroll_up(struct window *window)
561 for (; off > 0; --off)
562 cmd_scroll_line_up(window);
566 cmd_scroll_down(struct window *window)
572 for (; off > 0; --off)
573 cmd_scroll_line_down(window);
577 cmd_beginning_of_buffer(struct window *window)
579 window->current_line = TAILQ_FIRST(&window->head);
580 window->line_off = 0;
583 restore_cursor(window);
587 cmd_end_of_buffer(struct window *window)
591 off = window->line_max - body_lines;
594 window->line_off = off;
595 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
597 window->current_line = TAILQ_LAST(&window->head, vhead);
598 window->cpoff = body_cols;
599 restore_cursor(window);
603 cmd_kill_telescope(struct window *window)
610 cmd_push_button(struct window *window)
615 nth = window->line_off + window->curs_y;
616 if (nth >= window->line_max)
618 vl = nth_line(window, nth);
619 if (vl->parent->type != LINE_LINK)
622 load_url_in_tab(current_tab(), vl->parent->alt);
626 cmd_push_button_new_tab(struct window *window)
631 nth = window->line_off + window->curs_y;
632 if (nth > window->line_max)
634 vl = nth_line(window, nth);
635 if (vl->parent->type != LINE_LINK)
638 new_tab(vl->parent->alt);
642 cmd_previous_button(struct window *window)
645 if (window->current_line == NULL ||
646 window->current_line == TAILQ_FIRST(&window->head)) {
647 message("No previous link");
650 cmd_previous_line(window);
651 } while (window->current_line->parent->type != LINE_LINK);
655 cmd_next_button(struct window *window)
658 if (window->current_line == NULL ||
659 window->current_line == TAILQ_LAST(&window->head, vhead)) {
660 message("No next link");
663 cmd_next_line(window);
664 } while (window->current_line->parent->type != LINE_LINK);
668 cmd_previous_page(struct window *window)
670 struct tab *tab = current_tab();
672 if (!load_previous_page(tab))
673 message("No previous page");
675 start_loading_anim(tab);
679 cmd_next_page(struct window *window)
681 struct tab *tab = current_tab();
683 if (!load_next_page(tab))
684 message("No next page");
686 start_loading_anim(tab);
690 cmd_clear_minibuf(struct window *window)
692 handle_clear_minibuf(0, 0, NULL);
696 cmd_execute_extended_command(struct window *window)
701 message("We don't have enable-recursive-minibuffers");
705 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
708 len = sizeof(ministate.prompt);
709 strlcpy(ministate.prompt, "", len);
712 strlcat(ministate.prompt, "M-", len);
714 strlcat(ministate.prompt, keyname(thiskey.key), len);
717 strlcat(ministate.prompt, " ", len);
721 cmd_tab_close(struct window *window)
726 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
727 TAILQ_NEXT(tab, tabs) == NULL) {
728 message("Can't close the only tab.");
732 if (evtimer_pending(&tab->loadingev, NULL))
733 evtimer_del(&tab->loadingev);
737 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
738 t = TAILQ_NEXT(tab, tabs);
739 TAILQ_REMOVE(&tabshead, tab, tabs);
746 cmd_tab_close_other(struct window *window)
750 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
751 if (t->flags & TAB_CURRENT)
755 TAILQ_REMOVE(&tabshead, t, tabs);
761 cmd_tab_new(struct window *window)
763 new_tab(NEW_TAB_URL);
767 cmd_tab_next(struct window *window)
772 tab->flags &= ~TAB_CURRENT;
774 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
775 t = TAILQ_FIRST(&tabshead);
776 t->flags |= TAB_CURRENT;
780 cmd_tab_previous(struct window *window)
785 tab->flags &= ~TAB_CURRENT;
787 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
788 t = TAILQ_LAST(&tabshead, tabshead);
789 t->flags |= TAB_CURRENT;
793 cmd_tab_move(struct window *window)
798 t = TAILQ_NEXT(tab, tabs);
799 TAILQ_REMOVE(&tabshead, tab, tabs);
802 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
804 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
808 cmd_tab_move_to(struct window *window)
813 t = TAILQ_PREV(tab, tabshead, tabs);
814 TAILQ_REMOVE(&tabshead, tab, tabs);
817 if (TAILQ_EMPTY(&tabshead))
818 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
820 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
822 TAILQ_INSERT_BEFORE(t, tab, tabs);
826 cmd_load_url(struct window *window)
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, "gemini://", sizeof(ministate.buf));
837 cmd_move_end_of_line(&ministate.window);
841 cmd_load_current_url(struct window *window)
843 struct tab *tab = current_tab();
846 message("We don't have enable-recursive-minibuffers");
850 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
852 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
853 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
854 ministate.window.cpoff = utf8_cplen(ministate.buf);
858 cmd_bookmark_page(struct window *window)
860 struct tab *tab = current_tab();
862 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
863 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
864 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
865 ministate.window.cpoff = utf8_cplen(ministate.buf);
869 cmd_list_bookmarks(struct window *window)
871 load_url_in_tab(current_tab(), "about:bookmarks");
875 cmd_toggle_help(struct window *window)
877 side_window = !side_window;
881 /* ugly hack, but otherwise the window doesn't get updated
882 * until I call handle_resize a second time (i.e. C-l). I
883 * will be happy to know why something like this is needed. */
884 handle_resize(0, 0, NULL);
885 handle_resize(0, 0, NULL);
889 cmd_mini_delete_char(struct window *window)
893 if (!in_minibuffer) {
894 message("text is read-only");
898 minibuffer_taint_hist();
900 c = utf8_nth(window->current_line->line, window->cpoff);
905 memmove(c, n, strlen(n)+1);
909 cmd_mini_delete_backward_char(struct window *window)
913 if (!in_minibuffer) {
914 message("text is read-only");
918 minibuffer_taint_hist();
920 c = utf8_nth(window->current_line->line, window->cpoff);
921 start = window->current_line->line;
924 p = utf8_prev_cp(c-1, start);
926 memmove(p, c, strlen(c)+1);
931 cmd_mini_kill_line(struct window *window)
935 if (!in_minibuffer) {
936 message("text is read-only");
940 minibuffer_taint_hist();
941 c = utf8_nth(window->current_line->line, window->cpoff);
946 cmd_mini_abort(struct window *window)
955 cmd_mini_complete_and_exit(struct window *window)
960 minibuffer_taint_hist();
965 cmd_mini_previous_history_element(struct window *window)
967 if (ministate.history == NULL) {
968 message("No history");
972 if (ministate.hist_cur == NULL ||
973 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
974 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
975 ministate.hist_off = ministate.history->len - 1;
976 if (ministate.hist_cur == NULL)
977 message("No prev item");
979 ministate.hist_off--;
982 if (ministate.hist_cur != NULL)
983 window->current_line->line = ministate.hist_cur->h;
987 cmd_mini_next_history_element(struct window *window)
989 if (ministate.history == NULL) {
990 message("No history");
994 if (ministate.hist_cur == NULL ||
995 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
996 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
997 ministate.hist_off = 0;
998 if (ministate.hist_cur == NULL)
999 message("No next item");
1001 ministate.hist_off++;
1004 if (ministate.hist_cur != NULL)
1005 window->current_line->line = ministate.hist_cur->h;
1009 global_key_unbound(void)
1011 message("%s is undefined", keybuf);
1015 minibuffer_hist_save_entry(void)
1019 if (ministate.history == NULL)
1022 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1025 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1027 if (TAILQ_EMPTY(&ministate.history->head))
1028 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1030 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1031 ministate.history->len++;
1035 * taint the minibuffer cache: if we're currently showing a history
1036 * element, copy that to the current buf and reset the "history
1037 * navigation" thing.
1040 minibuffer_taint_hist(void)
1042 if (ministate.hist_cur == NULL)
1045 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1046 ministate.hist_cur = NULL;
1050 minibuffer_self_insert(void)
1052 char *c, tmp[5] = {0};
1055 minibuffer_taint_hist();
1057 if (thiskey.cp == 0)
1060 len = utf8_encode(thiskey.cp, tmp);
1061 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1062 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1065 memmove(c + len, c, strlen(c)+1);
1066 memcpy(c, tmp, len);
1067 ministate.window.cpoff++;
1071 eecmd_self_insert(void)
1073 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1074 !unicode_isgraph(thiskey.cp)) {
1075 global_key_unbound();
1079 minibuffer_self_insert();
1087 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1088 if (!strcmp(cmd->cmd, ministate.buf)) {
1090 minibuffer_hist_save_entry();
1091 cmd->fn(current_window());
1096 message("No match");
1100 ir_self_insert(void)
1102 minibuffer_self_insert();
1108 char buf[1025] = {0};
1109 struct phos_uri uri;
1112 tab = current_tab();
1115 minibuffer_hist_save_entry();
1117 /* a bit ugly but... */
1118 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1119 phos_uri_set_query(&uri, ministate.buf);
1120 phos_serialize_uri(&uri, buf, sizeof(buf));
1121 load_url_in_tab(tab, buf);
1125 lu_self_insert(void)
1127 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1128 !unicode_isgraph(thiskey.key)) {
1129 global_key_unbound();
1133 minibuffer_self_insert();
1140 minibuffer_hist_save_entry();
1141 load_url_in_tab(current_tab(), ministate.buf);
1148 if (*ministate.buf != '\0')
1149 add_to_bookmarks(ministate.buf);
1155 yornp_self_insert(void)
1157 if (thiskey.key != 'y' && thiskey.key != 'n') {
1158 message("Please answer y or n");
1163 yornp_cb(thiskey.key == 'y', yornp_data);
1170 yornp_cb(0, yornp_data);
1173 static struct vline *
1174 nth_line(struct window *window, size_t n)
1180 TAILQ_FOREACH(vl, &window->head, vlines) {
1195 TAILQ_FOREACH(t, &tabshead, tabs) {
1196 if (t->flags & TAB_CURRENT)
1204 static struct window *
1205 current_window(void)
1208 return &ministate.window;
1209 return ¤t_tab()->window;
1217 if ((thiskey.key = wgetch(body)) == ERR)
1220 thiskey.meta = thiskey.key == 27;
1222 thiskey.key = wgetch(body);
1223 if (thiskey.key == ERR || thiskey.key == 27) {
1230 if ((unsigned int)thiskey.key < UINT8_MAX) {
1232 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1234 if ((thiskey.key = wgetch(body)) == ERR) {
1235 message("Error decoding user input");
1245 dispatch_stdio(int fd, short ev, void *d)
1248 const char *keyname;
1254 if (keybuf[0] != '\0')
1255 strlcat(keybuf, " ", sizeof(keybuf));
1257 strlcat(keybuf, "M-", sizeof(keybuf));
1258 if (thiskey.cp != 0) {
1259 utf8_encode(thiskey.cp, tmp);
1260 strlcat(keybuf, tmp, sizeof(keybuf));
1262 if ((keyname = unkbd(thiskey.key)) != NULL)
1263 strlcat(keybuf, keyname, sizeof(keybuf));
1265 tmp[0] = thiskey.key;
1266 strlcat(keybuf, tmp, sizeof(keybuf));
1270 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1271 if (k->meta == thiskey.meta &&
1272 k->key == thiskey.key) {
1274 current_map = &k->map;
1276 current_map = base_map;
1277 strlcpy(keybuf, "", sizeof(keybuf));
1278 k->fn(current_window());
1284 if (current_map->unhandled_input != NULL)
1285 current_map->unhandled_input();
1287 global_key_unbound();
1290 strlcpy(keybuf, "", sizeof(keybuf));
1291 current_map = base_map;
1297 redraw_tab(current_tab());
1301 handle_clear_minibuf(int fd, short ev, void *d)
1303 free(ministate.curmesg);
1304 ministate.curmesg = NULL;
1306 redraw_minibuffer();
1307 if (in_minibuffer) {
1317 handle_resize(int sig, short ev, void *d)
1319 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1320 event_del(&resizeev);
1322 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1323 evtimer_add(&resizeev, &resize_timer);
1327 handle_resize_timeout(int s, short ev, void *d)
1335 /* move and resize the windows, in reverse order! */
1337 mvwin(minibuf, LINES-1, 0);
1338 wresize(minibuf, 1, COLS);
1340 mvwin(modeline, LINES-2, 0);
1341 wresize(modeline, 1, COLS);
1343 body_lines = LINES-3;
1347 help_cols = 0.3 * COLS;
1348 help_lines = LINES-3;
1350 wresize(help, help_lines, help_cols);
1352 wrap_page(&helpwin, help_cols);
1354 body_cols = COLS - help_cols - 1;
1355 mvwin(body, 1, help_cols);
1359 wresize(body, body_lines, body_cols);
1361 wresize(tabline, 1, COLS);
1363 tab = current_tab();
1365 wrap_page(&tab->window, body_cols);
1370 wrap_page(struct window *window, int width)
1373 const struct line *orig;
1377 orig = window->current_line == NULL
1379 : window->current_line->parent;
1380 window->current_line = NULL;
1383 window->line_off = 0;
1385 empty_vlist(window);
1387 TAILQ_FOREACH(l, &window->page.head, lines) {
1388 prfx = line_prefixes[l->type].prfx1;
1397 case LINE_PRE_START:
1399 wrap_text(window, prfx, l, width);
1401 case LINE_PRE_CONTENT:
1402 hardwrap_text(window, l, width);
1406 if (orig == l && window->current_line == NULL) {
1407 window->line_off = window->line_max-1;
1408 window->current_line = TAILQ_LAST(&window->head, vhead);
1411 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1412 if (vl == NULL || vl->parent != orig)
1414 window->current_line = vl;
1420 if (window->current_line == NULL)
1421 window->current_line = TAILQ_FIRST(&window->head);
1427 print_vline(WINDOW *window, struct vline *vl)
1429 const char *text = vl->line;
1431 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1432 int text_face = line_faces[vl->parent->type].text_prop;
1435 prfx = line_prefixes[vl->parent->type].prfx1;
1437 prfx = line_prefixes[vl->parent->type].prfx2;
1442 wattron(window, prefix_face);
1443 wprintw(window, "%s", prfx);
1444 wattroff(window, prefix_face);
1446 wattron(window, text_face);
1447 wprintw(window, "%s", text);
1448 wattroff(window, text_face);
1452 redraw_tabline(void)
1455 size_t toskip, ots, tabwidth, space, x;
1456 int current, y, truncated;
1460 tabwidth = sizeof(buf)+1;
1464 TAILQ_FOREACH(tab, &tabshead, tabs) {
1466 if (tab->flags & TAB_CURRENT)
1470 if (toskip * tabwidth < space)
1475 while (toskip != 0 &&
1476 (ots - toskip+1) * tabwidth < space)
1481 wattron(tabline, tab_face.background);
1482 wprintw(tabline, toskip == 0 ? " " : "<");
1483 wattroff(tabline, tab_face.background);
1486 TAILQ_FOREACH(tab, &tabshead, tabs) {
1494 getyx(tabline, y, x);
1495 if (x + sizeof(buf)+2 >= (size_t)COLS)
1498 current = tab->flags & TAB_CURRENT;
1500 if (*(title = tab->window.page.title) == '\0')
1501 title = tab->hist_cur->h;
1503 strlcpy(buf, " ", sizeof(buf));
1504 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1505 /* truncation happens */
1506 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1508 /* pad with spaces */
1509 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1514 wattron(tabline, tab_face.current_tab);
1516 wattron(tabline, tab_face.tab);
1518 wprintw(tabline, "%s", buf);
1519 if (TAILQ_NEXT(tab, tabs) != NULL)
1520 wprintw(tabline, " ");
1523 wattroff(tabline, tab_face.current_tab);
1525 wattroff(tabline, tab_face.tab);
1528 wattron(tabline, tab_face.background);
1529 for (; x < (size_t)COLS; ++x)
1530 waddch(tabline, ' ');
1532 mvwprintw(tabline, 0, COLS-1, ">");
1536 redraw_window(WINDOW *win, int height, struct window *window)
1543 window->line_off = MIN(window->line_max-1, window->line_off);
1544 if (TAILQ_EMPTY(&window->head))
1548 vl = nth_line(window, window->line_off);
1549 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1551 print_vline(win, vl);
1557 wmove(win, window->curs_y, window->curs_x);
1563 redraw_window(help, help_lines, &helpwin);
1567 redraw_body(struct tab *tab)
1569 redraw_window(body, body_lines, &tab->window);
1573 trust_status_char(enum trust_state ts)
1576 case TS_UNKNOWN: return 'u';
1577 case TS_UNTRUSTED: return '!';
1578 case TS_TRUSTED: return 'v';
1579 case TS_VERIFIED: return 'V';
1584 redraw_modeline(struct tab *tab)
1587 int x, y, max_x, max_y;
1588 const char *mode = tab->window.page.name;
1589 const char *spin = "-\\|/";
1592 wattron(modeline, A_REVERSE);
1593 wmove(modeline, 0, 0);
1595 wprintw(modeline, "-%c%c %s ",
1596 spin[tab->loading_anim_step],
1597 trust_status_char(tab->trust),
1598 mode == NULL ? "(none)" : mode);
1600 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1602 if (tab->window.line_max <= (size_t)body_lines)
1603 wprintw(modeline, "All ");
1604 else if (tab->window.line_off == 0)
1605 wprintw(modeline, "Top ");
1606 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1607 wprintw(modeline, "Bottom ");
1609 wprintw(modeline, "%.0f%% ", pct);
1611 wprintw(modeline, "%d/%d %s ",
1612 tab->window.line_off + tab->window.curs_y,
1613 tab->window.line_max,
1616 getyx(modeline, y, x);
1617 getmaxyx(modeline, max_y, max_x);
1622 for (; x < max_x; ++x)
1623 waddstr(modeline, "-");
1627 redraw_minibuffer(void)
1630 size_t off_y, off_x = 0;
1635 if (in_minibuffer) {
1636 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1637 if (ministate.hist_cur != NULL)
1638 wprintw(minibuf, "(%zu/%zu) ",
1639 ministate.hist_off + 1,
1640 ministate.history->len);
1642 getyx(minibuf, off_y, off_x);
1644 start = ministate.hist_cur != NULL
1645 ? ministate.hist_cur->h
1647 c = utf8_nth(ministate.window.current_line->line,
1648 ministate.window.cpoff);
1649 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1650 start = utf8_next_cp(start);
1653 waddstr(minibuf, start);
1656 if (ministate.curmesg != NULL)
1657 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1660 if (!in_minibuffer && ministate.curmesg == NULL)
1661 waddstr(minibuf, keybuf);
1663 /* If nothing else, show the URL at point */
1664 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1665 tab = current_tab();
1666 if (tab->window.current_line != NULL &&
1667 tab->window.current_line->parent->type == LINE_LINK)
1668 waddstr(minibuf, tab->window.current_line->parent->alt);
1672 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1676 redraw_tab(struct tab *tab)
1685 redraw_modeline(tab);
1686 redraw_minibuffer();
1688 wnoutrefresh(tabline);
1689 wnoutrefresh(modeline);
1691 if (in_minibuffer) {
1693 wnoutrefresh(minibuf);
1695 wnoutrefresh(minibuf);
1703 emit_help_item(char *prfx, void *fn)
1708 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1712 assert(cmd != NULL);
1714 if ((l = calloc(1, sizeof(*l))) == NULL)
1717 l->type = LINE_TEXT;
1720 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1722 if (TAILQ_EMPTY(&helpwin.page.head))
1723 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1725 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1729 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1735 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1736 strlcpy(p, prfx, sizeof(p));
1738 strlcat(p, " ", sizeof(p));
1740 strlcat(p, "M-", sizeof(p));
1741 if ((kn = unkbd(k->key)) != NULL)
1742 strlcat(p, kn, sizeof(p));
1744 strlcat(p, keyname(k->key), sizeof(p));
1747 rec_compute_help(&k->map, p, sizeof(p));
1749 emit_help_item(p, k->fn);
1754 recompute_help(void)
1758 empty_vlist(&helpwin);
1759 empty_linelist(&helpwin);
1760 rec_compute_help(current_map, p, sizeof(p));
1761 wrap_page(&helpwin, help_cols);
1765 vmessage(const char *fmt, va_list ap)
1767 if (evtimer_pending(&clminibufev, NULL))
1768 evtimer_del(&clminibufev);
1769 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1770 evtimer_add(&clminibufev, &clminibufev_timer);
1772 free(ministate.curmesg);
1774 /* TODO: what to do if the allocation fails here? */
1775 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1776 ministate.curmesg = NULL;
1778 redraw_minibuffer();
1779 if (in_minibuffer) {
1789 message(const char *fmt, ...)
1799 start_loading_anim(struct tab *tab)
1801 if (tab->loading_anim)
1803 tab->loading_anim = 1;
1804 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1805 evtimer_add(&tab->loadingev, &loadingev_timer);
1809 update_loading_anim(int fd, short ev, void *d)
1811 struct tab *tab = d;
1813 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1815 if (tab->flags & TAB_CURRENT) {
1816 redraw_modeline(tab);
1823 evtimer_add(&tab->loadingev, &loadingev_timer);
1827 stop_loading_anim(struct tab *tab)
1829 if (!tab->loading_anim)
1831 evtimer_del(&tab->loadingev);
1832 tab->loading_anim = 0;
1833 tab->loading_anim_step = 0;
1835 if (!(tab->flags & TAB_CURRENT))
1838 redraw_modeline(tab);
1847 load_url_in_tab(struct tab *tab, const char *url)
1849 message("Loading %s...", url);
1850 start_loading_anim(tab);
1853 tab->window.curs_x = 0;
1854 tab->window.curs_y = 0;
1859 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1860 void (*abortfn)(void), struct histhead *hist)
1863 base_map = &minibuffer_map;
1864 current_map = &minibuffer_map;
1866 base_map->unhandled_input = self_insert_fn;
1868 ministate.donefn = donefn;
1869 ministate.abortfn = abortfn;
1870 memset(ministate.buf, 0, sizeof(ministate.buf));
1871 ministate.window.current_line = &ministate.vline;
1872 ministate.window.current_line->line = ministate.buf;
1873 ministate.window.cpoff = 0;
1874 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1876 ministate.history = hist;
1877 ministate.hist_cur = NULL;
1878 ministate.hist_off = 0;
1882 exit_minibuffer(void)
1887 base_map = &global_map;
1888 current_map = &global_map;
1892 switch_to_tab(struct tab *tab)
1896 TAILQ_FOREACH(t, &tabshead, tabs) {
1897 t->flags &= ~TAB_CURRENT;
1900 tab->flags |= TAB_CURRENT;
1906 return tab_counter++;
1910 new_tab(const char *url)
1914 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1919 TAILQ_INIT(&tab->hist.head);
1921 TAILQ_INIT(&tab->window.head);
1923 tab->id = tab_new_id();
1926 if (TAILQ_EMPTY(&tabshead))
1927 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1929 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1931 load_url_in_tab(tab, url);
1936 session_new_tab_cb(const char *url)
1944 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1948 ui_init(int argc, char * const *argv)
1950 const char *url = NEW_TAB_URL;
1953 while ((ch = getopt(argc, argv, "")) != -1) {
1966 setlocale(LC_ALL, "");
1968 TAILQ_INIT(&global_map.m);
1969 global_map.unhandled_input = global_key_unbound;
1971 TAILQ_INIT(&minibuffer_map.m);
1973 TAILQ_INIT(&eecmd_history.head);
1974 TAILQ_INIT(&ir_history.head);
1975 TAILQ_INIT(&lu_history.head);
1977 ministate.line.type = LINE_TEXT;
1978 ministate.vline.parent = &ministate.line;
1979 ministate.window.current_line = &ministate.vline;
1981 /* initialize help window */
1982 TAILQ_INIT(&helpwin.head);
1984 base_map = &global_map;
1985 current_map = &global_map;
1986 load_default_keys();
1993 intrflush(stdscr, FALSE);
1995 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1997 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1999 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2001 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2003 if ((help = newwin(1, 1, 1, 0)) == NULL)
2006 body_lines = LINES-3;
2010 scrollok(body, TRUE);
2012 /* non-blocking input */
2015 mvwprintw(body, 0, 0, "");
2017 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2018 event_add(&stdioev, NULL);
2020 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2021 signal_add(&winchev, NULL);
2023 load_last_session(session_new_tab_cb);
2024 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2031 ui_on_tab_loaded(struct tab *tab)
2033 stop_loading_anim(tab);
2034 message("Loaded %s", tab->hist_cur->h);
2045 ui_on_tab_refresh(struct tab *tab)
2047 wrap_page(&tab->window, body_cols);
2048 if (tab->flags & TAB_CURRENT) {
2049 restore_cursor(&tab->window);
2055 ui_require_input(struct tab *tab, int hide)
2057 /* TODO: hard-switching to another tab is ugly */
2060 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2062 strlcpy(ministate.prompt, "Input required: ",
2063 sizeof(ministate.prompt));
2068 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2073 if (in_minibuffer) {
2080 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2083 len = sizeof(ministate.prompt);
2084 strlcpy(ministate.prompt, prompt, len);
2085 strlcat(ministate.prompt, " (y or n) ", len);
2086 redraw_tab(current_tab());
2090 ui_notify(const char *fmt, ...)