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);
715 strlcat(ministate.prompt, " ", len);
719 cmd_tab_close(struct window *window)
724 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
725 TAILQ_NEXT(tab, tabs) == NULL) {
726 message("Can't close the only tab.");
730 if (evtimer_pending(&tab->loadingev, NULL))
731 evtimer_del(&tab->loadingev);
735 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
736 t = TAILQ_NEXT(tab, tabs);
737 TAILQ_REMOVE(&tabshead, tab, tabs);
744 cmd_tab_close_other(struct window *window)
748 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
749 if (t->flags & TAB_CURRENT)
753 TAILQ_REMOVE(&tabshead, t, tabs);
759 cmd_tab_new(struct window *window)
761 new_tab(NEW_TAB_URL);
765 cmd_tab_next(struct window *window)
770 tab->flags &= ~TAB_CURRENT;
772 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
773 t = TAILQ_FIRST(&tabshead);
774 t->flags |= TAB_CURRENT;
778 cmd_tab_previous(struct window *window)
783 tab->flags &= ~TAB_CURRENT;
785 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
786 t = TAILQ_LAST(&tabshead, tabshead);
787 t->flags |= TAB_CURRENT;
791 cmd_tab_move(struct window *window)
796 t = TAILQ_NEXT(tab, tabs);
797 TAILQ_REMOVE(&tabshead, tab, tabs);
800 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
802 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
806 cmd_tab_move_to(struct window *window)
811 t = TAILQ_PREV(tab, tabshead, tabs);
812 TAILQ_REMOVE(&tabshead, tab, tabs);
815 if (TAILQ_EMPTY(&tabshead))
816 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
818 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
820 TAILQ_INSERT_BEFORE(t, tab, tabs);
824 cmd_load_url(struct window *window)
827 message("We don't have enable-recursive-minibuffers");
831 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
833 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
834 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
835 cmd_move_end_of_line(&ministate.window);
839 cmd_load_current_url(struct window *window)
841 struct tab *tab = current_tab();
844 message("We don't have enable-recursive-minibuffers");
848 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
850 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
851 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
852 ministate.window.cpoff = utf8_cplen(ministate.buf);
856 cmd_bookmark_page(struct window *window)
858 struct tab *tab = current_tab();
860 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
861 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
862 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
863 ministate.window.cpoff = utf8_cplen(ministate.buf);
867 cmd_list_bookmarks(struct window *window)
869 load_url_in_tab(current_tab(), "about:bookmarks");
873 cmd_toggle_help(struct window *window)
875 side_window = !side_window;
879 /* ugly hack, but otherwise the window doesn't get updated
880 * until I call handle_resize a second time (i.e. C-l). I
881 * will be happy to know why something like this is needed. */
882 handle_resize(0, 0, NULL);
883 handle_resize(0, 0, NULL);
887 cmd_mini_delete_char(struct window *window)
891 if (!in_minibuffer) {
892 message("text is read-only");
896 minibuffer_taint_hist();
898 c = utf8_nth(window->current_line->line, window->cpoff);
903 memmove(c, n, strlen(n)+1);
907 cmd_mini_delete_backward_char(struct window *window)
911 if (!in_minibuffer) {
912 message("text is read-only");
916 minibuffer_taint_hist();
918 c = utf8_nth(window->current_line->line, window->cpoff);
919 start = window->current_line->line;
922 p = utf8_prev_cp(c-1, start);
924 memmove(p, c, strlen(c)+1);
929 cmd_mini_kill_line(struct window *window)
933 if (!in_minibuffer) {
934 message("text is read-only");
938 minibuffer_taint_hist();
939 c = utf8_nth(window->current_line->line, window->cpoff);
944 cmd_mini_abort(struct window *window)
953 cmd_mini_complete_and_exit(struct window *window)
958 minibuffer_taint_hist();
963 cmd_mini_previous_history_element(struct window *window)
965 if (ministate.history == NULL) {
966 message("No history");
970 if (ministate.hist_cur == NULL ||
971 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
972 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
973 ministate.hist_off = ministate.history->len - 1;
974 if (ministate.hist_cur == NULL)
975 message("No prev item");
977 ministate.hist_off--;
980 if (ministate.hist_cur != NULL)
981 window->current_line->line = ministate.hist_cur->h;
985 cmd_mini_next_history_element(struct window *window)
987 if (ministate.history == NULL) {
988 message("No history");
992 if (ministate.hist_cur == NULL ||
993 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
994 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
995 ministate.hist_off = 0;
996 if (ministate.hist_cur == NULL)
997 message("No next item");
999 ministate.hist_off++;
1002 if (ministate.hist_cur != NULL)
1003 window->current_line->line = ministate.hist_cur->h;
1007 global_key_unbound(void)
1009 message("%s is undefined", keybuf);
1013 minibuffer_hist_save_entry(void)
1017 if (ministate.history == NULL)
1020 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1023 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1025 if (TAILQ_EMPTY(&ministate.history->head))
1026 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1028 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1029 ministate.history->len++;
1033 * taint the minibuffer cache: if we're currently showing a history
1034 * element, copy that to the current buf and reset the "history
1035 * navigation" thing.
1038 minibuffer_taint_hist(void)
1040 if (ministate.hist_cur == NULL)
1043 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1044 ministate.hist_cur = NULL;
1048 minibuffer_self_insert(void)
1050 char *c, tmp[5] = {0};
1053 minibuffer_taint_hist();
1055 if (thiskey.cp == 0)
1058 len = utf8_encode(thiskey.cp, tmp);
1059 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1060 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1063 memmove(c + len, c, strlen(c)+1);
1064 memcpy(c, tmp, len);
1065 ministate.window.cpoff++;
1069 eecmd_self_insert(void)
1071 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1072 !unicode_isgraph(thiskey.cp)) {
1073 global_key_unbound();
1077 minibuffer_self_insert();
1085 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1086 if (!strcmp(cmd->cmd, ministate.buf)) {
1088 minibuffer_hist_save_entry();
1089 cmd->fn(current_window());
1094 message("No match");
1098 ir_self_insert(void)
1100 minibuffer_self_insert();
1106 char buf[1025] = {0};
1107 struct phos_uri uri;
1110 tab = current_tab();
1113 minibuffer_hist_save_entry();
1115 /* a bit ugly but... */
1116 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1117 phos_uri_set_query(&uri, ministate.buf);
1118 phos_serialize_uri(&uri, buf, sizeof(buf));
1119 load_url_in_tab(tab, buf);
1123 lu_self_insert(void)
1125 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1126 !unicode_isgraph(thiskey.key)) {
1127 global_key_unbound();
1131 minibuffer_self_insert();
1138 minibuffer_hist_save_entry();
1139 load_url_in_tab(current_tab(), ministate.buf);
1146 if (*ministate.buf != '\0')
1147 add_to_bookmarks(ministate.buf);
1153 yornp_self_insert(void)
1155 if (thiskey.key != 'y' && thiskey.key != 'n') {
1156 message("Please answer y or n");
1161 yornp_cb(thiskey.key == 'y', yornp_data);
1168 yornp_cb(0, yornp_data);
1171 static struct vline *
1172 nth_line(struct window *window, size_t n)
1178 TAILQ_FOREACH(vl, &window->head, vlines) {
1193 TAILQ_FOREACH(t, &tabshead, tabs) {
1194 if (t->flags & TAB_CURRENT)
1202 static struct window *
1203 current_window(void)
1206 return &ministate.window;
1207 return ¤t_tab()->window;
1215 if ((thiskey.key = wgetch(body)) == ERR)
1218 thiskey.meta = thiskey.key == 27;
1220 thiskey.key = wgetch(body);
1221 if (thiskey.key == ERR || thiskey.key == 27) {
1228 if ((unsigned int)thiskey.key < UINT8_MAX) {
1230 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1232 if ((thiskey.key = wgetch(body)) == ERR) {
1233 message("Error decoding user input");
1243 dispatch_stdio(int fd, short ev, void *d)
1246 const char *keyname;
1252 if (keybuf[0] != '\0')
1253 strlcat(keybuf, " ", sizeof(keybuf));
1255 strlcat(keybuf, "M-", sizeof(keybuf));
1256 if (thiskey.cp != 0) {
1257 utf8_encode(thiskey.cp, tmp);
1258 strlcat(keybuf, tmp, sizeof(keybuf));
1260 if ((keyname = unkbd(thiskey.key)) != NULL)
1261 strlcat(keybuf, keyname, sizeof(keybuf));
1263 tmp[0] = thiskey.key;
1264 strlcat(keybuf, tmp, sizeof(keybuf));
1268 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1269 if (k->meta == thiskey.meta &&
1270 k->key == thiskey.key) {
1272 current_map = &k->map;
1274 current_map = base_map;
1275 strlcpy(keybuf, "", sizeof(keybuf));
1276 k->fn(current_window());
1282 if (current_map->unhandled_input != NULL)
1283 current_map->unhandled_input();
1285 global_key_unbound();
1288 strlcpy(keybuf, "", sizeof(keybuf));
1289 current_map = base_map;
1295 redraw_tab(current_tab());
1299 handle_clear_minibuf(int fd, short ev, void *d)
1301 free(ministate.curmesg);
1302 ministate.curmesg = NULL;
1304 redraw_minibuffer();
1305 if (in_minibuffer) {
1315 handle_resize(int sig, short ev, void *d)
1317 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1318 event_del(&resizeev);
1320 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1321 evtimer_add(&resizeev, &resize_timer);
1325 handle_resize_timeout(int s, short ev, void *d)
1333 /* move and resize the windows, in reverse order! */
1335 mvwin(minibuf, LINES-1, 0);
1336 wresize(minibuf, 1, COLS);
1338 mvwin(modeline, LINES-2, 0);
1339 wresize(modeline, 1, COLS);
1341 body_lines = LINES-3;
1345 help_cols = 0.3 * COLS;
1346 help_lines = LINES-3;
1348 wresize(help, help_lines, help_cols);
1350 wrap_page(&helpwin, help_cols);
1352 body_cols = COLS - help_cols - 1;
1353 mvwin(body, 1, help_cols);
1357 wresize(body, body_lines, body_cols);
1359 wresize(tabline, 1, COLS);
1361 tab = current_tab();
1363 wrap_page(&tab->window, body_cols);
1368 wrap_page(struct window *window, int width)
1371 const struct line *orig;
1375 orig = window->current_line == NULL
1377 : window->current_line->parent;
1378 window->current_line = NULL;
1381 window->line_off = 0;
1383 empty_vlist(window);
1385 TAILQ_FOREACH(l, &window->page.head, lines) {
1386 prfx = line_prefixes[l->type].prfx1;
1395 case LINE_PRE_START:
1397 wrap_text(window, prfx, l, width);
1399 case LINE_PRE_CONTENT:
1400 hardwrap_text(window, l, width);
1404 if (orig == l && window->current_line == NULL) {
1405 window->line_off = window->line_max-1;
1406 window->current_line = TAILQ_LAST(&window->head, vhead);
1409 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1410 if (vl == NULL || vl->parent != orig)
1412 window->current_line = vl;
1418 if (window->current_line == NULL)
1419 window->current_line = TAILQ_FIRST(&window->head);
1425 print_vline(WINDOW *window, struct vline *vl)
1427 const char *text = vl->line;
1429 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1430 int text_face = line_faces[vl->parent->type].text_prop;
1433 prfx = line_prefixes[vl->parent->type].prfx1;
1435 prfx = line_prefixes[vl->parent->type].prfx2;
1440 wattron(window, prefix_face);
1441 wprintw(window, "%s", prfx);
1442 wattroff(window, prefix_face);
1444 wattron(window, text_face);
1445 wprintw(window, "%s", text);
1446 wattroff(window, text_face);
1450 redraw_tabline(void)
1453 size_t toskip, ots, tabwidth, space, x;
1454 int current, y, truncated;
1458 tabwidth = sizeof(buf)+1;
1462 TAILQ_FOREACH(tab, &tabshead, tabs) {
1464 if (tab->flags & TAB_CURRENT)
1468 if (toskip * tabwidth < space)
1473 while (toskip != 0 &&
1474 (ots - toskip+1) * tabwidth < space)
1479 wattron(tabline, tab_face.background);
1480 wprintw(tabline, toskip == 0 ? " " : "<");
1481 wattroff(tabline, tab_face.background);
1484 TAILQ_FOREACH(tab, &tabshead, tabs) {
1492 getyx(tabline, y, x);
1493 if (x + sizeof(buf)+2 >= (size_t)COLS)
1496 current = tab->flags & TAB_CURRENT;
1498 if (*(title = tab->window.page.title) == '\0')
1499 title = tab->hist_cur->h;
1501 strlcpy(buf, " ", sizeof(buf));
1502 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1503 /* truncation happens */
1504 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1506 /* pad with spaces */
1507 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1512 wattron(tabline, tab_face.current_tab);
1514 wattron(tabline, tab_face.tab);
1516 wprintw(tabline, "%s", buf);
1517 if (TAILQ_NEXT(tab, tabs) != NULL)
1518 wprintw(tabline, " ");
1521 wattroff(tabline, tab_face.current_tab);
1523 wattroff(tabline, tab_face.tab);
1526 wattron(tabline, tab_face.background);
1527 for (; x < (size_t)COLS; ++x)
1528 waddch(tabline, ' ');
1530 mvwprintw(tabline, 0, COLS-1, ">");
1534 redraw_window(WINDOW *win, int height, struct window *window)
1541 window->line_off = MIN(window->line_max-1, window->line_off);
1542 if (TAILQ_EMPTY(&window->head))
1546 vl = nth_line(window, window->line_off);
1547 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1549 print_vline(win, vl);
1555 wmove(win, window->curs_y, window->curs_x);
1561 redraw_window(help, help_lines, &helpwin);
1565 redraw_body(struct tab *tab)
1567 redraw_window(body, body_lines, &tab->window);
1571 trust_status_char(enum trust_state ts)
1574 case TS_UNKNOWN: return 'u';
1575 case TS_UNTRUSTED: return '!';
1576 case TS_TRUSTED: return 'v';
1577 case TS_VERIFIED: return 'V';
1582 redraw_modeline(struct tab *tab)
1585 int x, y, max_x, max_y;
1586 const char *mode = tab->window.page.name;
1587 const char *spin = "-\\|/";
1590 wattron(modeline, A_REVERSE);
1591 wmove(modeline, 0, 0);
1593 wprintw(modeline, "-%c%c %s ",
1594 spin[tab->loading_anim_step],
1595 trust_status_char(tab->trust),
1596 mode == NULL ? "(none)" : mode);
1598 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1600 if (tab->window.line_max <= (size_t)body_lines)
1601 wprintw(modeline, "All ");
1602 else if (tab->window.line_off == 0)
1603 wprintw(modeline, "Top ");
1604 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1605 wprintw(modeline, "Bottom ");
1607 wprintw(modeline, "%.0f%% ", pct);
1609 wprintw(modeline, "%d/%d %s ",
1610 tab->window.line_off + tab->window.curs_y,
1611 tab->window.line_max,
1614 getyx(modeline, y, x);
1615 getmaxyx(modeline, max_y, max_x);
1620 for (; x < max_x; ++x)
1621 waddstr(modeline, "-");
1625 redraw_minibuffer(void)
1628 size_t off_y, off_x = 0;
1633 if (in_minibuffer) {
1634 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1635 if (ministate.hist_cur != NULL)
1636 wprintw(minibuf, "(%zu/%zu) ",
1637 ministate.hist_off + 1,
1638 ministate.history->len);
1640 getyx(minibuf, off_y, off_x);
1642 start = ministate.hist_cur != NULL
1643 ? ministate.hist_cur->h
1645 c = utf8_nth(ministate.window.current_line->line,
1646 ministate.window.cpoff);
1647 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1648 start = utf8_next_cp(start);
1651 waddstr(minibuf, start);
1654 if (ministate.curmesg != NULL)
1655 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1658 if (!in_minibuffer && ministate.curmesg == NULL)
1659 waddstr(minibuf, keybuf);
1661 /* If nothing else, show the URL at point */
1662 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1663 tab = current_tab();
1664 if (tab->window.current_line != NULL &&
1665 tab->window.current_line->parent->type == LINE_LINK)
1666 waddstr(minibuf, tab->window.current_line->parent->alt);
1670 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1674 redraw_tab(struct tab *tab)
1683 redraw_modeline(tab);
1684 redraw_minibuffer();
1686 wnoutrefresh(tabline);
1687 wnoutrefresh(modeline);
1689 if (in_minibuffer) {
1691 wnoutrefresh(minibuf);
1693 wnoutrefresh(minibuf);
1701 emit_help_item(char *prfx, void *fn)
1706 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1710 assert(cmd != NULL);
1712 if ((l = calloc(1, sizeof(*l))) == NULL)
1715 l->type = LINE_TEXT;
1718 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1720 if (TAILQ_EMPTY(&helpwin.page.head))
1721 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1723 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1727 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1733 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1734 strlcpy(p, prfx, sizeof(p));
1736 strlcat(p, " ", sizeof(p));
1738 strlcat(p, "M-", sizeof(p));
1739 if ((kn = unkbd(k->key)) != NULL)
1740 strlcat(p, kn, sizeof(p));
1742 strlcat(p, keyname(k->key), sizeof(p));
1745 rec_compute_help(&k->map, p, sizeof(p));
1747 emit_help_item(p, k->fn);
1752 recompute_help(void)
1756 empty_vlist(&helpwin);
1757 empty_linelist(&helpwin);
1758 rec_compute_help(current_map, p, sizeof(p));
1759 wrap_page(&helpwin, help_cols);
1763 vmessage(const char *fmt, va_list ap)
1765 if (evtimer_pending(&clminibufev, NULL))
1766 evtimer_del(&clminibufev);
1767 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1768 evtimer_add(&clminibufev, &clminibufev_timer);
1770 free(ministate.curmesg);
1772 /* TODO: what to do if the allocation fails here? */
1773 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1774 ministate.curmesg = NULL;
1776 redraw_minibuffer();
1777 if (in_minibuffer) {
1787 message(const char *fmt, ...)
1797 start_loading_anim(struct tab *tab)
1799 if (tab->loading_anim)
1801 tab->loading_anim = 1;
1802 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1803 evtimer_add(&tab->loadingev, &loadingev_timer);
1807 update_loading_anim(int fd, short ev, void *d)
1809 struct tab *tab = d;
1811 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1813 if (tab->flags & TAB_CURRENT) {
1814 redraw_modeline(tab);
1821 evtimer_add(&tab->loadingev, &loadingev_timer);
1825 stop_loading_anim(struct tab *tab)
1827 if (!tab->loading_anim)
1829 evtimer_del(&tab->loadingev);
1830 tab->loading_anim = 0;
1831 tab->loading_anim_step = 0;
1833 if (!(tab->flags & TAB_CURRENT))
1836 redraw_modeline(tab);
1845 load_url_in_tab(struct tab *tab, const char *url)
1847 message("Loading %s...", url);
1848 start_loading_anim(tab);
1851 tab->window.curs_x = 0;
1852 tab->window.curs_y = 0;
1857 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1858 void (*abortfn)(void), struct histhead *hist)
1861 base_map = &minibuffer_map;
1862 current_map = &minibuffer_map;
1864 base_map->unhandled_input = self_insert_fn;
1866 ministate.donefn = donefn;
1867 ministate.abortfn = abortfn;
1868 memset(ministate.buf, 0, sizeof(ministate.buf));
1869 ministate.window.current_line = &ministate.vline;
1870 ministate.window.current_line->line = ministate.buf;
1871 ministate.window.cpoff = 0;
1872 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1874 ministate.history = hist;
1875 ministate.hist_cur = NULL;
1876 ministate.hist_off = 0;
1880 exit_minibuffer(void)
1885 base_map = &global_map;
1886 current_map = &global_map;
1890 switch_to_tab(struct tab *tab)
1894 TAILQ_FOREACH(t, &tabshead, tabs) {
1895 t->flags &= ~TAB_CURRENT;
1898 tab->flags |= TAB_CURRENT;
1904 return tab_counter++;
1908 new_tab(const char *url)
1912 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1917 TAILQ_INIT(&tab->hist.head);
1919 TAILQ_INIT(&tab->window.head);
1921 tab->id = tab_new_id();
1924 if (TAILQ_EMPTY(&tabshead))
1925 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1927 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1929 load_url_in_tab(tab, url);
1934 session_new_tab_cb(const char *url)
1942 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1946 ui_init(int argc, char * const *argv)
1948 const char *url = NEW_TAB_URL;
1951 while ((ch = getopt(argc, argv, "")) != -1) {
1964 setlocale(LC_ALL, "");
1966 TAILQ_INIT(&global_map.m);
1967 global_map.unhandled_input = global_key_unbound;
1969 TAILQ_INIT(&minibuffer_map.m);
1971 TAILQ_INIT(&eecmd_history.head);
1972 TAILQ_INIT(&ir_history.head);
1973 TAILQ_INIT(&lu_history.head);
1975 ministate.line.type = LINE_TEXT;
1976 ministate.vline.parent = &ministate.line;
1977 ministate.window.current_line = &ministate.vline;
1979 /* initialize help window */
1980 TAILQ_INIT(&helpwin.head);
1982 base_map = &global_map;
1983 current_map = &global_map;
1984 load_default_keys();
1991 intrflush(stdscr, FALSE);
1993 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1995 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1997 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1999 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2001 if ((help = newwin(1, 1, 1, 0)) == NULL)
2004 body_lines = LINES-3;
2008 scrollok(body, TRUE);
2010 /* non-blocking input */
2013 mvwprintw(body, 0, 0, "");
2015 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2016 event_add(&stdioev, NULL);
2018 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2019 signal_add(&winchev, NULL);
2021 load_last_session(session_new_tab_cb);
2022 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2029 ui_on_tab_loaded(struct tab *tab)
2031 stop_loading_anim(tab);
2032 message("Loaded %s", tab->hist_cur->h);
2043 ui_on_tab_refresh(struct tab *tab)
2045 wrap_page(&tab->window, body_cols);
2046 if (tab->flags & TAB_CURRENT) {
2047 restore_cursor(&tab->window);
2053 ui_require_input(struct tab *tab, int hide)
2055 /* TODO: hard-switching to another tab is ugly */
2058 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2060 strlcpy(ministate.prompt, "Input required: ",
2061 sizeof(ministate.prompt));
2066 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2071 if (in_minibuffer) {
2078 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2081 len = sizeof(ministate.prompt);
2082 strlcpy(ministate.prompt, prompt, len);
2083 strlcat(ministate.prompt, " (y or n) ", len);
2084 redraw_tab(current_tab());
2088 ui_notify(const char *fmt, ...)