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"
48 #define TAB_CURRENT 0x1
49 #define TAB_URGENT 0x2
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void restore_cursor(struct window*);
58 static void global_key_unbound(void);
59 static void minibuffer_hist_save_entry(void);
60 static void minibuffer_taint_hist(void);
61 static void minibuffer_self_insert(void);
62 static void eecmd_self_insert(void);
63 static void eecmd_select(void);
64 static void ir_self_insert(void);
65 static void ir_select(void);
66 static void lu_self_insert(void);
67 static void lu_select(void);
68 static void bp_select(void);
69 static void yornp_self_insert(void);
70 static void yornp_abort(void);
72 static struct vline *nth_line(struct window*, size_t);
73 static struct tab *current_tab(void);
74 static struct window *current_window(void);
75 static int readkey(void);
76 static void dispatch_stdio(int, short, void*);
77 static void handle_clear_minibuf(int, short, void*);
78 static void handle_resize(int, short, void*);
79 static void handle_resize_timeout(int, short, void*);
80 static int wrap_page(struct window*, int);
81 static void print_vline(WINDOW*, struct vline*);
82 static void redraw_tabline(void);
83 static void redraw_window(WINDOW*, int, struct window*);
84 static void redraw_help(void);
85 static void redraw_body(struct tab*);
86 static void redraw_modeline(struct tab*);
87 static void redraw_minibuffer(void);
88 static void redraw_tab(struct tab*);
89 static void emit_help_item(char*, void*);
90 static void rec_compute_help(struct kmap*, char*, size_t);
91 static void recompute_help(void);
92 static void vmessage(const char*, va_list);
93 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
94 static void start_loading_anim(struct tab*);
95 static void update_loading_anim(int, short, void*);
96 static void stop_loading_anim(struct tab*);
97 static void load_url_in_tab(struct tab*, const char*);
98 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
99 static void exit_minibuffer(void);
100 static void switch_to_tab(struct tab*);
101 static struct tab *new_tab(const char*);
102 static void session_new_tab_cb(const char*);
103 static void usage(void);
105 static struct { short meta; int key; uint32_t cp; } thiskey;
107 static struct event resizeev;
108 static struct timeval resize_timer = { 0, 250000 };
110 static WINDOW *tabline, *body, *modeline, *minibuf;
111 static int body_lines, body_cols;
114 static struct window helpwin;
115 static int help_lines, help_cols;
117 static int side_window;
119 static struct event clminibufev;
120 static struct timeval clminibufev_timer = { 5, 0 };
121 static struct timeval loadingev_timer = { 0, 250000 };
123 static uint32_t tab_counter;
125 static char keybuf[64];
127 static void (*yornp_cb)(int, unsigned int);
128 static unsigned int yornp_data;
130 struct kmap global_map,
135 static struct histhead eecmd_history,
139 static int in_minibuffer;
145 void (*donefn)(void);
146 void (*abortfn)(void);
151 struct window window;
153 struct histhead *history;
154 struct hist *hist_cur;
161 } line_prefixes[] = {
162 [LINE_TEXT] = { "", "" },
163 [LINE_LINK] = { "=> ", " " },
164 [LINE_TITLE_1] = { "# ", " " },
165 [LINE_TITLE_2] = { "## ", " " },
166 [LINE_TITLE_3] = { "### ", " " },
167 [LINE_ITEM] = { "* ", " " },
168 [LINE_QUOTE] = { "> ", " " },
169 [LINE_PRE_START] = { "```", " " },
170 [LINE_PRE_CONTENT] = { "", "" },
171 [LINE_PRE_END] = { "```", "```" },
174 static struct line_face {
178 [LINE_TEXT] = { 0, 0 },
179 [LINE_LINK] = { 0, A_UNDERLINE },
180 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
181 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
182 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
183 [LINE_ITEM] = { 0, 0 },
184 [LINE_QUOTE] = { 0, A_DIM },
185 [LINE_PRE_START] = { 0, 0 },
186 [LINE_PRE_CONTENT] = { 0, 0 },
187 [LINE_PRE_END] = { 0, 0 },
190 static struct tab_face {
191 int background, tab, current_tab;
193 A_REVERSE, A_REVERSE, A_NORMAL
197 global_set_key(const char *key, void (*fn)(struct window*))
199 if (!kmap_define_key(&global_map, key, fn))
204 minibuffer_set_key(const char *key, void (*fn)(struct window*))
206 if (!kmap_define_key(&minibuffer_map, key, fn))
211 load_default_keys(void)
213 /* === global map === */
216 global_set_key("C-p", cmd_previous_line);
217 global_set_key("C-n", cmd_next_line);
218 global_set_key("C-f", cmd_forward_char);
219 global_set_key("C-b", cmd_backward_char);
220 global_set_key("M-{", cmd_backward_paragraph);
221 global_set_key("M-}", cmd_forward_paragraph);
222 global_set_key("C-a", cmd_move_beginning_of_line);
223 global_set_key("C-e", cmd_move_end_of_line);
225 global_set_key("M-v", cmd_scroll_up);
226 global_set_key("C-v", cmd_scroll_down);
227 global_set_key("M-space", cmd_scroll_up);
228 global_set_key("space", cmd_scroll_down);
230 global_set_key("M-<", cmd_beginning_of_buffer);
231 global_set_key("M->", cmd_end_of_buffer);
233 global_set_key("C-x C-c", cmd_kill_telescope);
235 global_set_key("C-g", cmd_clear_minibuf);
237 global_set_key("M-x", cmd_execute_extended_command);
238 global_set_key("C-x C-f", cmd_load_url);
239 global_set_key("C-x M-f", cmd_load_current_url);
241 global_set_key("C-x t 0", cmd_tab_close);
242 global_set_key("C-x t 1", cmd_tab_close_other);
243 global_set_key("C-x t 2", cmd_tab_new);
244 global_set_key("C-x t o", cmd_tab_next);
245 global_set_key("C-x t O", cmd_tab_previous);
246 global_set_key("C-x t m", cmd_tab_move);
247 global_set_key("C-x t M", cmd_tab_move_to);
249 global_set_key("C-M-b", cmd_previous_page);
250 global_set_key("C-M-f", cmd_next_page);
252 global_set_key("<f7> a", cmd_bookmark_page);
253 global_set_key("<f7> <f7>", cmd_list_bookmarks);
256 global_set_key("k", cmd_previous_line);
257 global_set_key("j", cmd_next_line);
258 global_set_key("l", cmd_forward_char);
259 global_set_key("h", cmd_backward_char);
260 global_set_key("{", cmd_backward_paragraph);
261 global_set_key("}", cmd_forward_paragraph);
262 global_set_key("^", cmd_move_beginning_of_line);
263 global_set_key("$", cmd_move_end_of_line);
265 global_set_key("K", cmd_scroll_line_up);
266 global_set_key("J", cmd_scroll_line_down);
268 global_set_key("g g", cmd_beginning_of_buffer);
269 global_set_key("G", cmd_end_of_buffer);
271 global_set_key("g D", cmd_tab_close);
272 global_set_key("g N", cmd_tab_new);
273 global_set_key("g t", cmd_tab_next);
274 global_set_key("g T", cmd_tab_previous);
275 global_set_key("g M-t", cmd_tab_move);
276 global_set_key("g M-T", cmd_tab_move_to);
278 global_set_key("H", cmd_previous_page);
279 global_set_key("L", cmd_next_page);
282 global_set_key("q", cmd_kill_telescope);
284 global_set_key("esc", cmd_clear_minibuf);
286 global_set_key(":", cmd_execute_extended_command);
289 global_set_key("<up>", cmd_previous_line);
290 global_set_key("<down>", cmd_next_line);
291 global_set_key("<right>", cmd_forward_char);
292 global_set_key("<left>", cmd_backward_char);
293 global_set_key("<prior>", cmd_scroll_up);
294 global_set_key("<next>", cmd_scroll_down);
296 global_set_key("M-<left>", cmd_previous_page);
297 global_set_key("M-<right>", cmd_next_page);
299 /* "ncurses standard" */
300 global_set_key("C-l", cmd_redraw);
303 global_set_key("<f1>", cmd_toggle_help);
304 global_set_key("C-m", cmd_push_button);
305 global_set_key("M-enter", cmd_push_button_new_tab);
306 global_set_key("M-tab", cmd_previous_button);
307 global_set_key("tab", cmd_next_button);
309 /* === minibuffer map === */
310 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
311 minibuffer_set_key("C-g", cmd_mini_abort);
312 minibuffer_set_key("esc", cmd_mini_abort);
313 minibuffer_set_key("C-d", cmd_mini_delete_char);
314 minibuffer_set_key("del", cmd_mini_delete_backward_char);
315 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
316 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
318 minibuffer_set_key("C-b", cmd_backward_char);
319 minibuffer_set_key("C-f", cmd_forward_char);
320 minibuffer_set_key("<left>", cmd_backward_char);
321 minibuffer_set_key("<right>", cmd_forward_char);
322 minibuffer_set_key("C-e", cmd_move_end_of_line);
323 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
324 minibuffer_set_key("<end>", cmd_move_end_of_line);
325 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
326 minibuffer_set_key("C-k", cmd_mini_kill_line);
328 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
329 minibuffer_set_key("M-n", cmd_mini_next_history_element);
330 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
331 minibuffer_set_key("<down>", cmd_mini_next_history_element);
335 restore_cursor(struct window *window)
340 vl = window->current_line;
341 if (vl == NULL || vl->line == NULL)
342 window->curs_x = window->cpoff = 0;
344 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
347 prfx = line_prefixes[vl->parent->type].prfx1;
348 window->curs_x += utf8_swidth(prfx);
353 cmd_previous_line(struct window *window)
357 if (window->current_line == NULL
358 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
361 if (--window->curs_y < 0) {
363 cmd_scroll_line_up(window);
367 window->current_line = vl;
368 restore_cursor(window);
372 cmd_next_line(struct window *window)
376 if (window->current_line == NULL
377 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
380 if (++window->curs_y > body_lines-1) {
381 window->curs_y = body_lines-1;
382 cmd_scroll_line_down(window);
386 window->current_line = vl;
387 restore_cursor(window);
391 cmd_backward_char(struct window *window)
393 if (window->cpoff != 0)
395 restore_cursor(window);
399 cmd_forward_char(struct window *window)
403 if (window->current_line->line != NULL)
404 len = utf8_cplen(window->current_line->line);
405 if (++window->cpoff > len)
407 restore_cursor(window);
411 cmd_backward_paragraph(struct window *window)
414 if (window->current_line == NULL ||
415 window->current_line == TAILQ_FIRST(&window->head)) {
416 message("No previous paragraph");
419 cmd_previous_line(window);
420 } while (window->current_line->line != NULL ||
421 window->current_line->parent->type != LINE_TEXT);
425 cmd_forward_paragraph(struct window *window)
428 if (window->current_line == NULL ||
429 window->current_line == TAILQ_LAST(&window->head, vhead)) {
430 message("No next paragraph");
433 cmd_next_line(window);
434 } while (window->current_line->line != NULL ||
435 window->current_line->parent->type != LINE_TEXT);
439 cmd_move_beginning_of_line(struct window *window)
442 restore_cursor(window);
446 cmd_move_end_of_line(struct window *window)
450 vl = window->current_line;
451 if (vl->line == NULL)
453 window->cpoff = utf8_cplen(vl->line);
454 restore_cursor(window);
458 cmd_redraw(struct window *window)
460 handle_resize(0, 0, NULL);
464 cmd_scroll_line_up(struct window *window)
468 if (window->line_off == 0)
471 vl = nth_line(window, --window->line_off);
474 print_vline(body, vl);
476 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
477 restore_cursor(window);
481 cmd_scroll_line_down(struct window *window)
485 vl = window->current_line;
486 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
488 window->current_line = vl;
493 if (window->line_max - window->line_off < (size_t)body_lines)
496 vl = nth_line(window, window->line_off + body_lines-1);
497 wmove(body, body_lines-1, 0);
498 print_vline(body, vl);
500 restore_cursor(window);
504 cmd_scroll_up(struct window *window)
510 for (; off > 0; --off)
511 cmd_scroll_line_up(window);
515 cmd_scroll_down(struct window *window)
521 for (; off > 0; --off)
522 cmd_scroll_line_down(window);
526 cmd_beginning_of_buffer(struct window *window)
528 window->current_line = TAILQ_FIRST(&window->head);
529 window->line_off = 0;
532 restore_cursor(window);
536 cmd_end_of_buffer(struct window *window)
540 off = window->line_max - body_lines;
543 window->line_off = off;
544 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
546 window->current_line = TAILQ_LAST(&window->head, vhead);
547 window->cpoff = body_cols;
548 restore_cursor(window);
552 cmd_kill_telescope(struct window *window)
559 cmd_push_button(struct window *window)
564 nth = window->line_off + window->curs_y;
565 if (nth >= window->line_max)
567 vl = nth_line(window, nth);
568 if (vl->parent->type != LINE_LINK)
571 load_url_in_tab(current_tab(), vl->parent->alt);
575 cmd_push_button_new_tab(struct window *window)
580 nth = window->line_off + window->curs_y;
581 if (nth > window->line_max)
583 vl = nth_line(window, nth);
584 if (vl->parent->type != LINE_LINK)
587 new_tab(vl->parent->alt);
591 cmd_previous_button(struct window *window)
594 if (window->current_line == NULL ||
595 window->current_line == TAILQ_FIRST(&window->head)) {
596 message("No previous link");
599 cmd_previous_line(window);
600 } while (window->current_line->parent->type != LINE_LINK);
604 cmd_next_button(struct window *window)
607 if (window->current_line == NULL ||
608 window->current_line == TAILQ_LAST(&window->head, vhead)) {
609 message("No next link");
612 cmd_next_line(window);
613 } while (window->current_line->parent->type != LINE_LINK);
617 cmd_previous_page(struct window *window)
619 struct tab *tab = current_tab();
621 if (!load_previous_page(tab))
622 message("No previous page");
624 start_loading_anim(tab);
628 cmd_next_page(struct window *window)
630 struct tab *tab = current_tab();
632 if (!load_next_page(tab))
633 message("No next page");
635 start_loading_anim(tab);
639 cmd_clear_minibuf(struct window *window)
641 handle_clear_minibuf(0, 0, NULL);
645 cmd_execute_extended_command(struct window *window)
650 message("We don't have enable-recursive-minibuffers");
654 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
657 len = sizeof(ministate.prompt);
658 strlcpy(ministate.prompt, "", len);
661 strlcat(ministate.prompt, "M-", len);
663 strlcat(ministate.prompt, keyname(thiskey.key), len);
666 strlcat(ministate.prompt, " ", len);
670 cmd_tab_close(struct window *window)
675 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
676 TAILQ_NEXT(tab, tabs) == NULL) {
677 message("Can't close the only tab.");
681 if (evtimer_pending(&tab->loadingev, NULL))
682 evtimer_del(&tab->loadingev);
686 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
687 t = TAILQ_NEXT(tab, tabs);
688 TAILQ_REMOVE(&tabshead, tab, tabs);
695 cmd_tab_close_other(struct window *window)
699 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
700 if (t->flags & TAB_CURRENT)
704 TAILQ_REMOVE(&tabshead, t, tabs);
710 cmd_tab_new(struct window *window)
712 new_tab(NEW_TAB_URL);
716 cmd_tab_next(struct window *window)
721 tab->flags &= ~TAB_CURRENT;
723 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
724 t = TAILQ_FIRST(&tabshead);
725 t->flags |= TAB_CURRENT;
726 t->flags &= ~TAB_URGENT;
730 cmd_tab_previous(struct window *window)
735 tab->flags &= ~TAB_CURRENT;
737 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
738 t = TAILQ_LAST(&tabshead, tabshead);
739 t->flags |= TAB_CURRENT;
740 t->flags &= ~TAB_URGENT;
744 cmd_tab_move(struct window *window)
749 t = TAILQ_NEXT(tab, tabs);
750 TAILQ_REMOVE(&tabshead, tab, tabs);
753 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
755 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
759 cmd_tab_move_to(struct window *window)
764 t = TAILQ_PREV(tab, tabshead, tabs);
765 TAILQ_REMOVE(&tabshead, tab, tabs);
768 if (TAILQ_EMPTY(&tabshead))
769 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
771 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
773 TAILQ_INSERT_BEFORE(t, tab, tabs);
777 cmd_load_url(struct window *window)
780 message("We don't have enable-recursive-minibuffers");
784 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
786 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
787 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
788 cmd_move_end_of_line(&ministate.window);
792 cmd_load_current_url(struct window *window)
794 struct tab *tab = current_tab();
797 message("We don't have enable-recursive-minibuffers");
801 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
803 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
804 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
805 ministate.window.cpoff = utf8_cplen(ministate.buf);
809 cmd_bookmark_page(struct window *window)
811 struct tab *tab = current_tab();
813 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
814 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
815 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
816 ministate.window.cpoff = utf8_cplen(ministate.buf);
820 cmd_list_bookmarks(struct window *window)
822 load_url_in_tab(current_tab(), "about:bookmarks");
826 cmd_toggle_help(struct window *window)
828 side_window = !side_window;
833 * ugly hack, but otherwise the window doesn't get updated
834 * until I call handle_resize a second time (i.e. C-l). I
835 * will be happy to know why something like this is needed.
837 handle_resize(0, 0, NULL);
838 handle_resize(0, 0, NULL);
842 cmd_mini_delete_char(struct window *window)
846 if (!in_minibuffer) {
847 message("text is read-only");
851 minibuffer_taint_hist();
853 c = utf8_nth(window->current_line->line, window->cpoff);
858 memmove(c, n, strlen(n)+1);
862 cmd_mini_delete_backward_char(struct window *window)
866 if (!in_minibuffer) {
867 message("text is read-only");
871 minibuffer_taint_hist();
873 c = utf8_nth(window->current_line->line, window->cpoff);
874 start = window->current_line->line;
877 p = utf8_prev_cp(c-1, start);
879 memmove(p, c, strlen(c)+1);
884 cmd_mini_kill_line(struct window *window)
888 if (!in_minibuffer) {
889 message("text is read-only");
893 minibuffer_taint_hist();
894 c = utf8_nth(window->current_line->line, window->cpoff);
899 cmd_mini_abort(struct window *window)
908 cmd_mini_complete_and_exit(struct window *window)
913 minibuffer_taint_hist();
918 cmd_mini_previous_history_element(struct window *window)
920 if (ministate.history == NULL) {
921 message("No history");
925 if (ministate.hist_cur == NULL ||
926 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
927 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
928 ministate.hist_off = ministate.history->len - 1;
929 if (ministate.hist_cur == NULL)
930 message("No prev item");
932 ministate.hist_off--;
935 if (ministate.hist_cur != NULL)
936 window->current_line->line = ministate.hist_cur->h;
940 cmd_mini_next_history_element(struct window *window)
942 if (ministate.history == NULL) {
943 message("No history");
947 if (ministate.hist_cur == NULL ||
948 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
949 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
950 ministate.hist_off = 0;
951 if (ministate.hist_cur == NULL)
952 message("No next item");
954 ministate.hist_off++;
957 if (ministate.hist_cur != NULL)
958 window->current_line->line = ministate.hist_cur->h;
962 global_key_unbound(void)
964 message("%s is undefined", keybuf);
968 minibuffer_hist_save_entry(void)
972 if (ministate.history == NULL)
975 if ((hist = calloc(1, sizeof(*hist))) == NULL)
978 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
980 if (TAILQ_EMPTY(&ministate.history->head))
981 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
983 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
984 ministate.history->len++;
988 * taint the minibuffer cache: if we're currently showing a history
989 * element, copy that to the current buf and reset the "history
993 minibuffer_taint_hist(void)
995 if (ministate.hist_cur == NULL)
998 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
999 ministate.hist_cur = NULL;
1003 minibuffer_self_insert(void)
1005 char *c, tmp[5] = {0};
1008 minibuffer_taint_hist();
1010 if (thiskey.cp == 0)
1013 len = utf8_encode(thiskey.cp, tmp);
1014 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1015 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1018 memmove(c + len, c, strlen(c)+1);
1019 memcpy(c, tmp, len);
1020 ministate.window.cpoff++;
1024 eecmd_self_insert(void)
1026 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1027 !unicode_isgraph(thiskey.cp)) {
1028 global_key_unbound();
1032 minibuffer_self_insert();
1040 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1041 if (!strcmp(cmd->cmd, ministate.buf)) {
1043 minibuffer_hist_save_entry();
1044 cmd->fn(current_window());
1049 message("No match");
1053 ir_self_insert(void)
1055 minibuffer_self_insert();
1061 char buf[1025] = {0};
1062 struct phos_uri uri;
1065 tab = current_tab();
1068 minibuffer_hist_save_entry();
1070 /* a bit ugly but... */
1071 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1072 phos_uri_set_query(&uri, ministate.buf);
1073 phos_serialize_uri(&uri, buf, sizeof(buf));
1074 load_url_in_tab(tab, buf);
1078 lu_self_insert(void)
1080 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1081 !unicode_isgraph(thiskey.key)) {
1082 global_key_unbound();
1086 minibuffer_self_insert();
1093 minibuffer_hist_save_entry();
1094 load_url_in_tab(current_tab(), ministate.buf);
1101 if (*ministate.buf != '\0')
1102 add_to_bookmarks(ministate.buf);
1108 yornp_self_insert(void)
1110 if (thiskey.key != 'y' && thiskey.key != 'n') {
1111 message("Please answer y or n");
1116 yornp_cb(thiskey.key == 'y', yornp_data);
1123 yornp_cb(0, yornp_data);
1126 static struct vline *
1127 nth_line(struct window *window, size_t n)
1133 TAILQ_FOREACH(vl, &window->head, vlines) {
1148 TAILQ_FOREACH(t, &tabshead, tabs) {
1149 if (t->flags & TAB_CURRENT)
1157 static struct window *
1158 current_window(void)
1161 return &ministate.window;
1162 return ¤t_tab()->window;
1170 if ((thiskey.key = wgetch(body)) == ERR)
1173 thiskey.meta = thiskey.key == 27;
1175 thiskey.key = wgetch(body);
1176 if (thiskey.key == ERR || thiskey.key == 27) {
1183 if ((unsigned int)thiskey.key < UINT8_MAX) {
1185 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1187 if ((thiskey.key = wgetch(body)) == ERR) {
1188 message("Error decoding user input");
1198 dispatch_stdio(int fd, short ev, void *d)
1201 const char *keyname;
1207 if (keybuf[0] != '\0')
1208 strlcat(keybuf, " ", sizeof(keybuf));
1210 strlcat(keybuf, "M-", sizeof(keybuf));
1211 if (thiskey.cp != 0) {
1212 utf8_encode(thiskey.cp, tmp);
1213 strlcat(keybuf, tmp, sizeof(keybuf));
1215 if ((keyname = unkbd(thiskey.key)) != NULL)
1216 strlcat(keybuf, keyname, sizeof(keybuf));
1218 tmp[0] = thiskey.key;
1219 strlcat(keybuf, tmp, sizeof(keybuf));
1223 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1224 if (k->meta == thiskey.meta &&
1225 k->key == thiskey.key) {
1227 current_map = &k->map;
1229 current_map = base_map;
1230 strlcpy(keybuf, "", sizeof(keybuf));
1231 k->fn(current_window());
1237 if (current_map->unhandled_input != NULL)
1238 current_map->unhandled_input();
1240 global_key_unbound();
1243 strlcpy(keybuf, "", sizeof(keybuf));
1244 current_map = base_map;
1250 redraw_tab(current_tab());
1254 handle_clear_minibuf(int fd, short ev, void *d)
1256 free(ministate.curmesg);
1257 ministate.curmesg = NULL;
1259 redraw_minibuffer();
1260 if (in_minibuffer) {
1270 handle_resize(int sig, short ev, void *d)
1272 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1273 event_del(&resizeev);
1275 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1276 evtimer_add(&resizeev, &resize_timer);
1280 handle_resize_timeout(int s, short ev, void *d)
1288 /* move and resize the windows, in reverse order! */
1290 mvwin(minibuf, LINES-1, 0);
1291 wresize(minibuf, 1, COLS);
1293 mvwin(modeline, LINES-2, 0);
1294 wresize(modeline, 1, COLS);
1296 body_lines = LINES-3;
1300 help_cols = 0.3 * COLS;
1301 help_lines = LINES-3;
1303 wresize(help, help_lines, help_cols);
1305 wrap_page(&helpwin, help_cols);
1307 body_cols = COLS - help_cols - 1;
1308 mvwin(body, 1, help_cols);
1312 wresize(body, body_lines, body_cols);
1314 wresize(tabline, 1, COLS);
1316 tab = current_tab();
1318 wrap_page(&tab->window, body_cols);
1323 wrap_page(struct window *window, int width)
1326 const struct line *orig;
1330 orig = window->current_line == NULL
1332 : window->current_line->parent;
1333 window->current_line = NULL;
1336 window->line_off = 0;
1338 empty_vlist(window);
1340 TAILQ_FOREACH(l, &window->page.head, lines) {
1341 prfx = line_prefixes[l->type].prfx1;
1350 case LINE_PRE_START:
1352 wrap_text(window, prfx, l, width);
1354 case LINE_PRE_CONTENT:
1355 hardwrap_text(window, l, width);
1359 if (orig == l && window->current_line == NULL) {
1360 window->line_off = window->line_max-1;
1361 window->current_line = TAILQ_LAST(&window->head, vhead);
1364 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1365 if (vl == NULL || vl->parent != orig)
1367 window->current_line = vl;
1373 if (window->current_line == NULL)
1374 window->current_line = TAILQ_FIRST(&window->head);
1380 print_vline(WINDOW *window, struct vline *vl)
1382 const char *text = vl->line;
1384 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1385 int text_face = line_faces[vl->parent->type].text_prop;
1388 prfx = line_prefixes[vl->parent->type].prfx1;
1390 prfx = line_prefixes[vl->parent->type].prfx2;
1395 wattron(window, prefix_face);
1396 wprintw(window, "%s", prfx);
1397 wattroff(window, prefix_face);
1399 wattron(window, text_face);
1400 wprintw(window, "%s", text);
1401 wattroff(window, text_face);
1405 redraw_tabline(void)
1408 size_t toskip, ots, tabwidth, space, x;
1409 int current, y, truncated;
1413 tabwidth = sizeof(buf)+1;
1417 TAILQ_FOREACH(tab, &tabshead, tabs) {
1419 if (tab->flags & TAB_CURRENT)
1423 if (toskip * tabwidth < space)
1428 while (toskip != 0 &&
1429 (ots - toskip+1) * tabwidth < space)
1434 wattron(tabline, tab_face.background);
1435 wprintw(tabline, toskip == 0 ? " " : "<");
1436 wattroff(tabline, tab_face.background);
1439 TAILQ_FOREACH(tab, &tabshead, tabs) {
1447 getyx(tabline, y, x);
1448 if (x + sizeof(buf)+2 >= (size_t)COLS)
1451 current = tab->flags & TAB_CURRENT;
1453 if (*(title = tab->window.page.title) == '\0')
1454 title = tab->hist_cur->h;
1456 if (tab->flags & TAB_URGENT)
1457 strlcpy(buf, "!", sizeof(buf));
1459 strlcpy(buf, " ", sizeof(buf));
1461 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1462 /* truncation happens */
1463 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1465 /* pad with spaces */
1466 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1471 wattron(tabline, tab_face.current_tab);
1473 wattron(tabline, tab_face.tab);
1475 wprintw(tabline, "%s", buf);
1476 if (TAILQ_NEXT(tab, tabs) != NULL)
1477 wprintw(tabline, " ");
1480 wattroff(tabline, tab_face.current_tab);
1482 wattroff(tabline, tab_face.tab);
1485 wattron(tabline, tab_face.background);
1486 for (; x < (size_t)COLS; ++x)
1487 waddch(tabline, ' ');
1489 mvwprintw(tabline, 0, COLS-1, ">");
1493 redraw_window(WINDOW *win, int height, struct window *window)
1500 window->line_off = MIN(window->line_max-1, window->line_off);
1501 if (TAILQ_EMPTY(&window->head))
1505 vl = nth_line(window, window->line_off);
1506 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1508 print_vline(win, vl);
1514 wmove(win, window->curs_y, window->curs_x);
1520 redraw_window(help, help_lines, &helpwin);
1524 redraw_body(struct tab *tab)
1526 redraw_window(body, body_lines, &tab->window);
1530 trust_status_char(enum trust_state ts)
1533 case TS_UNKNOWN: return 'u';
1534 case TS_UNTRUSTED: return '!';
1535 case TS_TRUSTED: return 'v';
1536 case TS_VERIFIED: return 'V';
1541 redraw_modeline(struct tab *tab)
1544 int x, y, max_x, max_y;
1545 const char *mode = tab->window.page.name;
1546 const char *spin = "-\\|/";
1549 wattron(modeline, A_REVERSE);
1550 wmove(modeline, 0, 0);
1552 wprintw(modeline, "-%c%c %s ",
1553 spin[tab->loading_anim_step],
1554 trust_status_char(tab->trust),
1555 mode == NULL ? "(none)" : mode);
1557 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1559 if (tab->window.line_max <= (size_t)body_lines)
1560 wprintw(modeline, "All ");
1561 else if (tab->window.line_off == 0)
1562 wprintw(modeline, "Top ");
1563 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1564 wprintw(modeline, "Bottom ");
1566 wprintw(modeline, "%.0f%% ", pct);
1568 wprintw(modeline, "%d/%d %s ",
1569 tab->window.line_off + tab->window.curs_y,
1570 tab->window.line_max,
1573 getyx(modeline, y, x);
1574 getmaxyx(modeline, max_y, max_x);
1579 for (; x < max_x; ++x)
1580 waddstr(modeline, "-");
1584 redraw_minibuffer(void)
1587 size_t off_y, off_x = 0;
1592 if (in_minibuffer) {
1593 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1594 if (ministate.hist_cur != NULL)
1595 wprintw(minibuf, "(%zu/%zu) ",
1596 ministate.hist_off + 1,
1597 ministate.history->len);
1599 getyx(minibuf, off_y, off_x);
1601 start = ministate.hist_cur != NULL
1602 ? ministate.hist_cur->h
1604 c = utf8_nth(ministate.window.current_line->line,
1605 ministate.window.cpoff);
1606 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1607 start = utf8_next_cp(start);
1610 waddstr(minibuf, start);
1613 if (ministate.curmesg != NULL)
1614 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1617 if (!in_minibuffer && ministate.curmesg == NULL)
1618 waddstr(minibuf, keybuf);
1620 /* If nothing else, show the URL at point */
1621 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1622 tab = current_tab();
1623 if (tab->window.current_line != NULL &&
1624 tab->window.current_line->parent->type == LINE_LINK)
1625 waddstr(minibuf, tab->window.current_line->parent->alt);
1629 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1633 redraw_tab(struct tab *tab)
1642 redraw_modeline(tab);
1643 redraw_minibuffer();
1645 wnoutrefresh(tabline);
1646 wnoutrefresh(modeline);
1648 if (in_minibuffer) {
1650 wnoutrefresh(minibuf);
1652 wnoutrefresh(minibuf);
1660 emit_help_item(char *prfx, void *fn)
1665 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1669 assert(cmd != NULL);
1671 if ((l = calloc(1, sizeof(*l))) == NULL)
1674 l->type = LINE_TEXT;
1677 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1679 if (TAILQ_EMPTY(&helpwin.page.head))
1680 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1682 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1686 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1692 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1693 strlcpy(p, prfx, sizeof(p));
1695 strlcat(p, " ", sizeof(p));
1697 strlcat(p, "M-", sizeof(p));
1698 if ((kn = unkbd(k->key)) != NULL)
1699 strlcat(p, kn, sizeof(p));
1701 strlcat(p, keyname(k->key), sizeof(p));
1704 rec_compute_help(&k->map, p, sizeof(p));
1706 emit_help_item(p, k->fn);
1711 recompute_help(void)
1715 empty_vlist(&helpwin);
1716 empty_linelist(&helpwin);
1717 rec_compute_help(current_map, p, sizeof(p));
1718 wrap_page(&helpwin, help_cols);
1722 vmessage(const char *fmt, va_list ap)
1724 if (evtimer_pending(&clminibufev, NULL))
1725 evtimer_del(&clminibufev);
1726 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1727 evtimer_add(&clminibufev, &clminibufev_timer);
1729 free(ministate.curmesg);
1731 /* TODO: what to do if the allocation fails here? */
1732 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1733 ministate.curmesg = NULL;
1735 redraw_minibuffer();
1736 if (in_minibuffer) {
1746 message(const char *fmt, ...)
1756 start_loading_anim(struct tab *tab)
1758 if (tab->loading_anim)
1760 tab->loading_anim = 1;
1761 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1762 evtimer_add(&tab->loadingev, &loadingev_timer);
1766 update_loading_anim(int fd, short ev, void *d)
1768 struct tab *tab = d;
1770 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1772 if (tab->flags & TAB_CURRENT) {
1773 redraw_modeline(tab);
1780 evtimer_add(&tab->loadingev, &loadingev_timer);
1784 stop_loading_anim(struct tab *tab)
1786 if (!tab->loading_anim)
1788 evtimer_del(&tab->loadingev);
1789 tab->loading_anim = 0;
1790 tab->loading_anim_step = 0;
1792 if (!(tab->flags & TAB_CURRENT))
1795 redraw_modeline(tab);
1804 load_url_in_tab(struct tab *tab, const char *url)
1806 message("Loading %s...", url);
1807 start_loading_anim(tab);
1810 tab->window.curs_x = 0;
1811 tab->window.curs_y = 0;
1816 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1817 void (*abortfn)(void), struct histhead *hist)
1820 base_map = &minibuffer_map;
1821 current_map = &minibuffer_map;
1823 base_map->unhandled_input = self_insert_fn;
1825 ministate.donefn = donefn;
1826 ministate.abortfn = abortfn;
1827 memset(ministate.buf, 0, sizeof(ministate.buf));
1828 ministate.window.current_line = &ministate.vline;
1829 ministate.window.current_line->line = ministate.buf;
1830 ministate.window.cpoff = 0;
1831 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1833 ministate.history = hist;
1834 ministate.hist_cur = NULL;
1835 ministate.hist_off = 0;
1839 exit_minibuffer(void)
1844 base_map = &global_map;
1845 current_map = &global_map;
1849 switch_to_tab(struct tab *tab)
1853 TAILQ_FOREACH(t, &tabshead, tabs) {
1854 t->flags &= ~TAB_CURRENT;
1857 tab->flags |= TAB_CURRENT;
1863 return tab_counter++;
1867 new_tab(const char *url)
1871 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1876 TAILQ_INIT(&tab->hist.head);
1878 TAILQ_INIT(&tab->window.head);
1880 tab->id = tab_new_id();
1883 if (TAILQ_EMPTY(&tabshead))
1884 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1886 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1888 load_url_in_tab(tab, url);
1893 session_new_tab_cb(const char *url)
1901 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1905 ui_init(int argc, char * const *argv)
1907 const char *url = NEW_TAB_URL;
1910 while ((ch = getopt(argc, argv, "")) != -1) {
1923 setlocale(LC_ALL, "");
1925 TAILQ_INIT(&global_map.m);
1926 global_map.unhandled_input = global_key_unbound;
1928 TAILQ_INIT(&minibuffer_map.m);
1930 TAILQ_INIT(&eecmd_history.head);
1931 TAILQ_INIT(&ir_history.head);
1932 TAILQ_INIT(&lu_history.head);
1934 ministate.line.type = LINE_TEXT;
1935 ministate.vline.parent = &ministate.line;
1936 ministate.window.current_line = &ministate.vline;
1938 /* initialize help window */
1939 TAILQ_INIT(&helpwin.head);
1941 base_map = &global_map;
1942 current_map = &global_map;
1943 load_default_keys();
1950 intrflush(stdscr, FALSE);
1952 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1954 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1956 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1958 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1960 if ((help = newwin(1, 1, 1, 0)) == NULL)
1963 body_lines = LINES-3;
1967 scrollok(body, TRUE);
1969 /* non-blocking input */
1972 mvwprintw(body, 0, 0, "");
1974 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1975 event_add(&stdioev, NULL);
1977 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1978 signal_add(&winchev, NULL);
1980 load_last_session(session_new_tab_cb);
1981 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1988 ui_on_tab_loaded(struct tab *tab)
1990 stop_loading_anim(tab);
1991 message("Loaded %s", tab->hist_cur->h);
2002 ui_on_tab_refresh(struct tab *tab)
2004 wrap_page(&tab->window, body_cols);
2005 if (tab->flags & TAB_CURRENT) {
2006 restore_cursor(&tab->window);
2009 tab->flags |= TAB_URGENT;
2013 ui_require_input(struct tab *tab, int hide)
2015 /* TODO: hard-switching to another tab is ugly */
2018 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2020 strlcpy(ministate.prompt, "Input required: ",
2021 sizeof(ministate.prompt));
2026 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2031 if (in_minibuffer) {
2038 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2041 len = sizeof(ministate.prompt);
2042 strlcpy(ministate.prompt, prompt, len);
2043 strlcat(ministate.prompt, " (y or n) ", len);
2044 redraw_tab(current_tab());
2048 ui_notify(const char *fmt, ...)