2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Ncurses UI for telescope.
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
26 * This way is easy to scroll: just call wscrl and then render the
29 * This means that on every resize we have to clear our list of lines
30 * and re-render everything. A clever approach would be to do this
33 * TODO: make the text formatting on-demand.
37 #include <telescope.h>
48 #define TAB_CURRENT 0x1
50 #define NEW_TAB_URL "about:new"
52 static struct event stdioev, winchev;
54 static void load_default_keys(void);
55 static void empty_vlist(struct window*);
56 static void restore_cursor(struct window*);
58 static void cmd_previous_line(struct window*);
59 static void cmd_next_line(struct window*);
60 static void cmd_backward_char(struct window*);
61 static void cmd_forward_char(struct window*);
62 static void cmd_backward_paragraph(struct window*);
63 static void cmd_forward_paragraph(struct window*);
64 static void cmd_move_beginning_of_line(struct window*);
65 static void cmd_move_end_of_line(struct window*);
66 static void cmd_redraw(struct window*);
67 static void cmd_scroll_line_down(struct window*);
68 static void cmd_scroll_line_up(struct window*);
69 static void cmd_scroll_up(struct window*);
70 static void cmd_scroll_down(struct window*);
71 static void cmd_beginning_of_buffer(struct window*);
72 static void cmd_end_of_buffer(struct window*);
73 static void cmd_kill_telescope(struct window*);
74 static void cmd_push_button(struct window*);
75 static void cmd_push_button_new_tab(struct window*);
76 static void cmd_previous_button(struct window*);
77 static void cmd_next_button(struct window*);
78 static void cmd_previous_page(struct window*);
79 static void cmd_next_page(struct window*);
80 static void cmd_clear_minibuf(struct window*);
81 static void cmd_execute_extended_command(struct window*);
82 static void cmd_tab_close(struct window*);
83 static void cmd_tab_close_other(struct window*);
84 static void cmd_tab_new(struct window*);
85 static void cmd_tab_next(struct window*);
86 static void cmd_tab_previous(struct window*);
87 static void cmd_load_url(struct window*);
88 static void cmd_load_current_url(struct window*);
89 static void cmd_bookmark_page(struct window*);
90 static void cmd_goto_bookmarks(struct window*);
92 static void global_key_unbound(void);
94 static void cmd_mini_delete_char(struct window*);
95 static void cmd_mini_delete_backward_char(struct window*);
96 static void cmd_mini_kill_line(struct window*);
97 static void cmd_mini_abort(struct window*);
98 static void cmd_mini_complete_and_exit(struct window*);
99 static void cmd_mini_previous_history_element(struct window*);
100 static void cmd_mini_next_history_element(struct window*);
102 static void minibuffer_hist_save_entry(void);
103 static void minibuffer_taint_hist(void);
104 static void minibuffer_self_insert(void);
105 static void eecmd_self_insert(void);
106 static void eecmd_select(void);
107 static void ir_self_insert(void);
108 static void ir_select(void);
109 static void lu_self_insert(void);
110 static void lu_select(void);
111 static void bp_select(void);
113 static struct vline *nth_line(struct window*, size_t);
114 static struct tab *current_tab(void);
115 static struct window *current_window(void);
116 static int readkey(void);
117 static void dispatch_stdio(int, short, void*);
118 static void handle_clear_minibuf(int, short, void*);
119 static void handle_resize(int, short, void*);
120 static int wrap_page(struct window*);
121 static void print_vline(struct vline*);
122 static void redraw_tabline(void);
123 static void redraw_body(struct tab*);
124 static void redraw_modeline(struct tab*);
125 static void redraw_minibuffer(void);
126 static void redraw_tab(struct tab*);
127 static void vmessage(const char*, va_list);
128 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
129 static void start_loading_anim(struct tab*);
130 static void update_loading_anim(int, short, void*);
131 static void stop_loading_anim(struct tab*);
132 static void load_url_in_tab(struct tab*, const char*);
133 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
134 static void exit_minibuffer(void);
135 static void switch_to_tab(struct tab*);
136 static struct tab *new_tab(const char*);
137 static void usage(void);
139 static struct { short meta; int key; uint32_t cp; } thiskey;
141 static WINDOW *tabline, *body, *modeline, *minibuf;
142 static int body_lines, body_cols;
144 static struct event clminibufev;
145 static struct timeval clminibufev_timer = { 5, 0 };
146 static struct timeval loadingev_timer = { 0, 250000 };
148 static uint32_t tab_counter;
150 static char keybuf[64];
152 struct kmap global_map,
157 static struct histhead eecmd_history,
161 static int in_minibuffer;
167 void (*donefn)(void);
168 void (*abortfn)(void);
173 struct window window;
175 struct histhead *history;
176 struct hist *hist_cur;
183 } line_prefixes[] = {
184 [LINE_TEXT] = { "", "" },
185 [LINE_LINK] = { "=> ", " " },
186 [LINE_TITLE_1] = { "# ", " " },
187 [LINE_TITLE_2] = { "## ", " " },
188 [LINE_TITLE_3] = { "### ", " " },
189 [LINE_ITEM] = { "* ", " " },
190 [LINE_QUOTE] = { "> ", " " },
191 [LINE_PRE_START] = { "```", " " },
192 [LINE_PRE_CONTENT] = { "", "" },
193 [LINE_PRE_END] = { "```", "```" },
196 static struct line_face {
200 [LINE_TEXT] = { 0, 0 },
201 [LINE_LINK] = { 0, A_UNDERLINE },
202 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
203 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
204 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
205 [LINE_ITEM] = { 0, 0 },
206 [LINE_QUOTE] = { 0, A_DIM },
207 [LINE_PRE_START] = { 0, 0 },
208 [LINE_PRE_CONTENT] = { 0, 0 },
209 [LINE_PRE_END] = { 0, 0 },
212 static struct tab_face {
213 int background, tab, current_tab;
215 A_REVERSE, A_REVERSE, A_NORMAL
219 empty_vlist(struct window *window)
221 struct vline *vl, *t;
223 window->current_line = NULL;
224 window->line_max = 0;
226 TAILQ_FOREACH_SAFE(vl, &window->head, vlines, t) {
227 TAILQ_REMOVE(&window->head, vl, vlines);
234 global_set_key(const char *key, void (*fn)(struct window*))
236 if (!kmap_define_key(&global_map, key, fn))
241 minibuffer_set_key(const char *key, void (*fn)(struct window*))
243 if (!kmap_define_key(&minibuffer_map, key, fn))
248 load_default_keys(void)
250 /* === global map === */
253 global_set_key("C-p", cmd_previous_line);
254 global_set_key("C-n", cmd_next_line);
255 global_set_key("C-f", cmd_forward_char);
256 global_set_key("C-b", cmd_backward_char);
257 global_set_key("M-{", cmd_backward_paragraph);
258 global_set_key("M-}", cmd_forward_paragraph);
259 global_set_key("C-a", cmd_move_beginning_of_line);
260 global_set_key("C-e", cmd_move_end_of_line);
262 global_set_key("M-v", cmd_scroll_up);
263 global_set_key("C-v", cmd_scroll_down);
264 global_set_key("M-space", cmd_scroll_up);
265 global_set_key("space", cmd_scroll_down);
267 global_set_key("C-x C-c", cmd_kill_telescope);
269 global_set_key("C-g", cmd_clear_minibuf);
271 global_set_key("M-x", cmd_execute_extended_command);
272 global_set_key("C-x C-f", cmd_load_url);
273 global_set_key("C-x M-f", cmd_load_current_url);
275 global_set_key("C-x t 0", cmd_tab_close);
276 global_set_key("C-x t 1", cmd_tab_close_other);
277 global_set_key("C-x t 2", cmd_tab_new);
278 global_set_key("C-x t o", cmd_tab_next);
279 global_set_key("C-x t O", cmd_tab_previous);
281 global_set_key("M-<", cmd_beginning_of_buffer);
282 global_set_key("M->", cmd_end_of_buffer);
284 global_set_key("C-M-b", cmd_previous_page);
285 global_set_key("C-M-f", cmd_next_page);
287 global_set_key("<f7> a", cmd_bookmark_page);
288 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
291 global_set_key("k", cmd_previous_line);
292 global_set_key("j", cmd_next_line);
293 global_set_key("l", cmd_forward_char);
294 global_set_key("h", cmd_backward_char);
295 global_set_key("{", cmd_backward_paragraph);
296 global_set_key("}", cmd_forward_paragraph);
297 global_set_key("^", cmd_move_beginning_of_line);
298 global_set_key("$", cmd_move_end_of_line);
300 global_set_key("K", cmd_scroll_line_up);
301 global_set_key("J", cmd_scroll_line_down);
303 global_set_key("g D", cmd_tab_close);
304 global_set_key("g N", cmd_tab_new);
305 global_set_key("g t", cmd_tab_next);
306 global_set_key("g T", cmd_tab_previous);
308 global_set_key("g g", cmd_beginning_of_buffer);
309 global_set_key("G", cmd_end_of_buffer);
311 global_set_key("H", cmd_previous_page);
312 global_set_key("L", cmd_next_page);
315 global_set_key("q", cmd_kill_telescope);
317 global_set_key("esc", cmd_clear_minibuf);
319 global_set_key(":", cmd_execute_extended_command);
322 global_set_key("<up>", cmd_previous_line);
323 global_set_key("<down>", cmd_next_line);
324 global_set_key("<right>", cmd_forward_char);
325 global_set_key("<left>", cmd_backward_char);
326 global_set_key("<prior>", cmd_scroll_up);
327 global_set_key("<next>", cmd_scroll_down);
329 global_set_key("M-<left>", cmd_previous_page);
330 global_set_key("M-<right>", cmd_next_page);
332 /* "ncurses standard" */
333 global_set_key("C-l", cmd_redraw);
336 global_set_key("C-m", cmd_push_button);
337 global_set_key("M-enter", cmd_push_button_new_tab);
338 global_set_key("M-tab", cmd_previous_button);
339 global_set_key("tab", cmd_next_button);
341 /* === minibuffer map === */
342 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
343 minibuffer_set_key("C-g", cmd_mini_abort);
344 minibuffer_set_key("esc", cmd_mini_abort);
345 minibuffer_set_key("C-d", cmd_mini_delete_char);
346 minibuffer_set_key("del", cmd_mini_delete_backward_char);
348 minibuffer_set_key("C-b", cmd_backward_char);
349 minibuffer_set_key("C-f", cmd_forward_char);
350 minibuffer_set_key("<left>", cmd_backward_char);
351 minibuffer_set_key("<right>", cmd_forward_char);
352 minibuffer_set_key("C-e", cmd_move_end_of_line);
353 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
354 minibuffer_set_key("<end>", cmd_move_end_of_line);
355 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
356 minibuffer_set_key("C-k", cmd_mini_kill_line);
358 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
359 minibuffer_set_key("M-n", cmd_mini_next_history_element);
360 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
361 minibuffer_set_key("<down>", cmd_mini_next_history_element);
365 restore_cursor(struct window *window)
370 vl = window->current_line;
371 if (vl == NULL || vl->line == NULL)
372 window->curs_x = window->cpoff = 0;
374 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 window->curs_x += utf8_swidth(prfx);
383 cmd_previous_line(struct window *window)
387 if (window->current_line == NULL
388 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
391 if (--window->curs_y < 0) {
393 cmd_scroll_line_up(window);
397 window->current_line = vl;
398 restore_cursor(window);
402 cmd_next_line(struct window *window)
406 if (window->current_line == NULL
407 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
410 if (++window->curs_y > body_lines-1) {
411 window->curs_y = body_lines-1;
412 cmd_scroll_line_down(window);
416 window->current_line = vl;
417 restore_cursor(window);
421 cmd_backward_char(struct window *window)
423 if (window->cpoff != 0)
425 restore_cursor(window);
429 cmd_forward_char(struct window *window)
432 restore_cursor(window);
436 cmd_backward_paragraph(struct window *window)
439 if (window->current_line == NULL ||
440 window->current_line == TAILQ_FIRST(&window->head)) {
441 message("No previous paragraph");
444 cmd_previous_line(window);
445 } while (window->current_line->line != NULL ||
446 window->current_line->parent->type != LINE_TEXT);
450 cmd_forward_paragraph(struct window *window)
453 if (window->current_line == NULL ||
454 window->current_line == TAILQ_LAST(&window->head, vhead)) {
455 message("No next paragraph");
458 cmd_next_line(window);
459 } while (window->current_line->line != NULL ||
460 window->current_line->parent->type != LINE_TEXT);
464 cmd_move_beginning_of_line(struct window *window)
467 restore_cursor(window);
471 cmd_move_end_of_line(struct window *window)
475 vl = window->current_line;
476 if (vl->line == NULL)
478 window->cpoff = utf8_cplen(vl->line);
479 restore_cursor(window);
483 cmd_redraw(struct window *window)
485 handle_resize(0, 0, NULL);
489 cmd_scroll_line_up(struct window *window)
493 if (window->line_off == 0)
496 vl = nth_line(window, --window->line_off);
501 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
502 restore_cursor(window);
506 cmd_scroll_line_down(struct window *window)
510 vl = window->current_line;
511 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
513 window->current_line = vl;
518 if (window->line_max - window->line_off < (size_t)body_lines)
521 vl = nth_line(window, window->line_off + body_lines-1);
522 wmove(body, body_lines-1, 0);
525 restore_cursor(window);
529 cmd_scroll_up(struct window *window)
535 for (; off > 0; --off)
536 cmd_scroll_line_up(window);
540 cmd_scroll_down(struct window *window)
546 for (; off > 0; --off)
547 cmd_scroll_line_down(window);
551 cmd_beginning_of_buffer(struct window *window)
553 window->current_line = TAILQ_FIRST(&window->head);
554 window->line_off = 0;
557 restore_cursor(window);
561 cmd_end_of_buffer(struct window *window)
565 off = window->line_max - body_lines;
568 window->line_off = off;
569 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
571 window->current_line = TAILQ_LAST(&window->head, vhead);
572 window->cpoff = body_cols;
573 restore_cursor(window);
577 cmd_kill_telescope(struct window *window)
583 cmd_push_button(struct window *window)
588 nth = window->line_off + window->curs_y;
589 if (nth >= window->line_max)
591 vl = nth_line(window, nth);
592 if (vl->parent->type != LINE_LINK)
595 load_url_in_tab(current_tab(), vl->parent->alt);
599 cmd_push_button_new_tab(struct window *window)
604 nth = window->line_off + window->curs_y;
605 if (nth > window->line_max)
607 vl = nth_line(window, nth);
608 if (vl->parent->type != LINE_LINK)
611 new_tab(vl->parent->alt);
615 cmd_previous_button(struct window *window)
618 if (window->current_line == NULL ||
619 window->current_line == TAILQ_FIRST(&window->head)) {
620 message("No previous link");
623 cmd_previous_line(window);
624 } while (window->current_line->parent->type != LINE_LINK);
628 cmd_next_button(struct window *window)
631 if (window->current_line == NULL ||
632 window->current_line == TAILQ_LAST(&window->head, vhead)) {
633 message("No next link");
636 cmd_next_line(window);
637 } while (window->current_line->parent->type != LINE_LINK);
641 cmd_previous_page(struct window *window)
643 struct tab *tab = current_tab();
645 if (!load_previous_page(tab))
646 message("No previous page");
648 start_loading_anim(tab);
652 cmd_next_page(struct window *window)
654 struct tab *tab = current_tab();
656 if (!load_next_page(tab))
657 message("No next page");
659 start_loading_anim(tab);
663 cmd_clear_minibuf(struct window *window)
665 handle_clear_minibuf(0, 0, NULL);
669 cmd_execute_extended_command(struct window *window)
674 message("We don't have enable-recursive-minibuffers");
678 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
681 len = sizeof(ministate.prompt);
682 strlcpy(ministate.prompt, "", len);
685 strlcat(ministate.prompt, "M-", len);
687 strlcat(ministate.prompt, keyname(thiskey.key), len);
688 strlcat(ministate.prompt, " ", len);
692 cmd_tab_close(struct window *window)
697 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
698 TAILQ_NEXT(tab, tabs) == NULL) {
699 message("Can't close the only tab.");
703 if (evtimer_pending(&tab->loadingev, NULL))
704 evtimer_del(&tab->loadingev);
708 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
709 t = TAILQ_NEXT(tab, tabs);
710 TAILQ_REMOVE(&tabshead, tab, tabs);
717 cmd_tab_close_other(struct window *window)
719 struct tab *tab, *t, *i;
722 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
723 if (t->flags & TAB_CURRENT)
727 TAILQ_REMOVE(&tabshead, t, tabs);
733 cmd_tab_new(struct window *window)
735 new_tab(NEW_TAB_URL);
739 cmd_tab_next(struct window *window)
744 tab->flags &= ~TAB_CURRENT;
746 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
747 t = TAILQ_FIRST(&tabshead);
748 t->flags |= TAB_CURRENT;
752 cmd_tab_previous(struct window *window)
757 tab->flags &= ~TAB_CURRENT;
759 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
760 t = TAILQ_LAST(&tabshead, tabshead);
761 t->flags |= TAB_CURRENT;
765 cmd_load_url(struct window *window)
768 message("We don't have enable-recursive-minibuffers");
772 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
774 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
778 cmd_load_current_url(struct window *window)
780 struct tab *tab = current_tab();
783 message("We don't have enable-recursive-minibuffers");
787 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
789 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
790 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
791 ministate.window.cpoff = utf8_cplen(ministate.buf);
795 cmd_bookmark_page(struct window *window)
797 struct tab *tab = current_tab();
799 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
800 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
801 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
802 ministate.window.cpoff = utf8_cplen(ministate.buf);
806 cmd_goto_bookmarks(struct window *window)
808 load_url_in_tab(current_tab(), "about:bookmarks");
812 global_key_unbound(void)
814 message("%s is undefined", keybuf);
818 cmd_mini_delete_char(struct window *window)
822 if (!in_minibuffer) {
823 message("text is read-only");
827 minibuffer_taint_hist();
829 c = utf8_nth(window->current_line->line, window->cpoff);
834 memmove(c, n, strlen(n)+1);
838 cmd_mini_delete_backward_char(struct window *window)
842 if (!in_minibuffer) {
843 message("text is read-only");
847 minibuffer_taint_hist();
849 c = utf8_nth(window->current_line->line, window->cpoff);
850 start = window->current_line->line;
853 p = utf8_prev_cp(c-1, start);
855 memmove(p, c, strlen(c)+1);
860 cmd_mini_kill_line(struct window *window)
864 if (!in_minibuffer) {
865 message("text is read-only");
869 minibuffer_taint_hist();
870 c = utf8_nth(window->current_line->line, window->cpoff);
875 cmd_mini_abort(struct window *window)
884 cmd_mini_complete_and_exit(struct window *window)
889 minibuffer_taint_hist();
894 cmd_mini_previous_history_element(struct window *window)
896 if (ministate.history == NULL) {
897 message("No history");
901 if (ministate.hist_cur == NULL ||
902 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
903 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
904 ministate.hist_off = ministate.history->len - 1;
905 if (ministate.hist_cur == NULL)
906 message("No prev item");
908 ministate.hist_off--;
911 if (ministate.hist_cur != NULL)
912 window->current_line->line = ministate.hist_cur->h;
916 cmd_mini_next_history_element(struct window *window)
918 if (ministate.history == NULL) {
919 message("No history");
923 if (ministate.hist_cur == NULL ||
924 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
925 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
926 ministate.hist_off = 0;
927 if (ministate.hist_cur == NULL)
928 message("No next item");
930 ministate.hist_off++;
933 if (ministate.hist_cur != NULL)
934 window->current_line->line = ministate.hist_cur->h;
938 minibuffer_hist_save_entry(void)
942 if (ministate.history == NULL)
945 if ((hist = calloc(1, sizeof(*hist))) == NULL)
948 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
950 if (TAILQ_EMPTY(&ministate.history->head))
951 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
953 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
954 ministate.history->len++;
958 * taint the minibuffer cache: if we're currently showing a history
959 * element, copy that to the current buf and reset the "history
963 minibuffer_taint_hist(void)
965 if (ministate.hist_cur == NULL)
968 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
969 ministate.hist_cur = NULL;
973 minibuffer_self_insert(void)
975 char *c, tmp[5] = {0};
978 minibuffer_taint_hist();
983 len = utf8_encode(thiskey.cp, tmp);
984 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
985 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
988 memmove(c + len, c, strlen(c)+1);
990 ministate.window.cpoff++;
994 eecmd_self_insert(void)
996 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
997 !unicode_isgraph(thiskey.cp)) {
998 global_key_unbound();
1002 minibuffer_self_insert();
1009 minibuffer_hist_save_entry();
1010 message("TODO: try to execute %s", ministate.buf);
1014 ir_self_insert(void)
1016 minibuffer_self_insert();
1022 char buf[1025] = {0};
1026 tab = current_tab();
1029 minibuffer_hist_save_entry();
1031 /* a bit ugly but... */
1032 memcpy(&url, &tab->url, sizeof(tab->url));
1033 url_set_query(&url, ministate.buf);
1034 url_unparse(&url, buf, sizeof(buf));
1035 load_url_in_tab(tab, buf);
1039 lu_self_insert(void)
1041 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1042 !unicode_isgraph(thiskey.key)) {
1043 global_key_unbound();
1047 minibuffer_self_insert();
1054 minibuffer_hist_save_entry();
1055 load_url_in_tab(current_tab(), ministate.buf);
1062 if (*ministate.buf != '\0')
1063 add_to_bookmarks(ministate.buf);
1068 static struct vline *
1069 nth_line(struct window *window, size_t n)
1075 TAILQ_FOREACH(vl, &window->head, vlines) {
1090 TAILQ_FOREACH(t, &tabshead, tabs) {
1091 if (t->flags & TAB_CURRENT)
1099 static struct window *
1100 current_window(void)
1103 return &ministate.window;
1104 return ¤t_tab()->window;
1112 if ((thiskey.key = wgetch(body)) == ERR)
1115 thiskey.meta = thiskey.key == 27;
1117 thiskey.key = wgetch(body);
1118 if (thiskey.key == ERR || thiskey.key == 27) {
1125 if ((unsigned int)thiskey.key < UINT8_MAX) {
1127 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1129 if ((thiskey.key = wgetch(body)) == ERR) {
1130 message("Error decoding user input");
1140 dispatch_stdio(int fd, short ev, void *d)
1143 const char *keyname;
1149 if (keybuf[0] != '\0')
1150 strlcat(keybuf, " ", sizeof(keybuf));
1152 strlcat(keybuf, "M-", sizeof(keybuf));
1153 if (thiskey.cp != 0) {
1154 utf8_encode(thiskey.cp, tmp);
1155 strlcat(keybuf, tmp, sizeof(keybuf));
1157 if ((keyname = unkbd(thiskey.key)) != NULL)
1158 strlcat(keybuf, keyname, sizeof(keybuf));
1160 tmp[0] = thiskey.key;
1161 strlcat(keybuf, tmp, sizeof(keybuf));
1165 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1166 if (k->meta == thiskey.meta &&
1167 k->key == thiskey.key) {
1169 current_map = &k->map;
1171 current_map = base_map;
1172 strlcpy(keybuf, "", sizeof(keybuf));
1173 k->fn(current_window());
1179 if (current_map->unhandled_input != NULL)
1180 current_map->unhandled_input();
1182 global_key_unbound();
1185 strlcpy(keybuf, "", sizeof(keybuf));
1186 current_map = base_map;
1189 redraw_tab(current_tab());
1193 handle_clear_minibuf(int fd, short ev, void *d)
1195 free(ministate.curmesg);
1196 ministate.curmesg = NULL;
1198 redraw_minibuffer();
1199 if (in_minibuffer) {
1209 handle_resize(int sig, short ev, void *d)
1217 /* move and resize the windows, in reverse order! */
1219 mvwin(minibuf, LINES-1, 0);
1220 wresize(minibuf, 1, COLS);
1222 mvwin(modeline, LINES-2, 0);
1223 wresize(modeline, 1, COLS);
1225 wresize(body, LINES-3, COLS);
1226 body_lines = LINES-3;
1229 wresize(tabline, 1, COLS);
1231 tab = current_tab();
1233 wrap_page(&tab->window);
1238 wrap_page(struct window *window)
1241 const struct line *orig;
1245 orig = window->current_line == NULL
1247 : window->current_line->parent;
1248 window->current_line = NULL;
1251 window->line_off = 0;
1253 empty_vlist(window);
1255 TAILQ_FOREACH(l, &window->page.head, lines) {
1256 prfx = line_prefixes[l->type].prfx1;
1265 case LINE_PRE_START:
1267 wrap_text(window, prfx, l, body_cols);
1269 case LINE_PRE_CONTENT:
1270 hardwrap_text(window, l, body_cols);
1274 if (orig == l && window->current_line == NULL) {
1275 window->line_off = window->line_max-1;
1276 window->current_line = TAILQ_LAST(&window->head, vhead);
1279 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1280 if (vl == NULL || vl->parent != orig)
1282 window->current_line = vl;
1288 if (window->current_line == NULL)
1289 window->current_line = TAILQ_FIRST(&window->head);
1295 print_vline(struct vline *vl)
1297 const char *text = vl->line;
1299 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1300 int text_face = line_faces[vl->parent->type].text_prop;
1303 prfx = line_prefixes[vl->parent->type].prfx1;
1305 prfx = line_prefixes[vl->parent->type].prfx2;
1310 wattron(body, prefix_face);
1311 wprintw(body, "%s", prfx);
1312 wattroff(body, prefix_face);
1314 wattron(body, text_face);
1315 wprintw(body, "%s", text);
1316 wattroff(body, text_face);
1320 redraw_tabline(void)
1324 int current, x, y, truncated;
1330 TAILQ_FOREACH(tab, &tabshead, tabs) {
1331 x += sizeof(buf) + 1;
1333 if (tab->flags & TAB_CURRENT)
1342 wattron(tabline, tab_face.background);
1343 wprintw(tabline, toskip == 0 ? " " : "<");
1344 wattroff(tabline, tab_face.background);
1347 TAILQ_FOREACH(tab, &tabshead, tabs) {
1355 getyx(tabline, y, x);
1356 if (x + sizeof(buf)+2 >= (size_t)COLS)
1359 current = tab->flags & TAB_CURRENT;
1361 if (*(title = tab->window.page.title) == '\0')
1362 title = tab->hist_cur->h;
1364 strlcpy(buf, " ", sizeof(buf));
1365 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1366 /* truncation happens */
1367 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1369 /* pad with spaces */
1370 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1375 wattron(tabline, tab_face.current_tab);
1377 wattron(tabline, tab_face.tab);
1379 wprintw(tabline, "%s", buf);
1380 if (TAILQ_NEXT(tab, tabs) != NULL)
1381 wprintw(tabline, " ");
1384 wattroff(tabline, tab_face.current_tab);
1386 wattroff(tabline, tab_face.tab);
1389 wattron(tabline, tab_face.background);
1390 for (; x < COLS; ++x)
1391 waddch(tabline, ' ');
1393 mvwprintw(tabline, 0, COLS-1, ">");
1397 trust_status_char(enum trust_state ts)
1400 case TS_UNKNOWN: return 'u';
1401 case TS_UNTRUSTED: return '!';
1402 case TS_TRUSTED: return 'v';
1403 case TS_VERIFIED: return 'V';
1408 redraw_modeline(struct tab *tab)
1411 int x, y, max_x, max_y;
1412 const char *mode = tab->window.page.name;
1413 const char *spin = "-\\|/";
1416 wattron(modeline, A_REVERSE);
1417 wmove(modeline, 0, 0);
1419 wprintw(modeline, "-%c%c %s ",
1420 spin[tab->loading_anim_step],
1421 trust_status_char(tab->trust),
1422 mode == NULL ? "(none)" : mode);
1424 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1426 if (tab->window.line_max <= (size_t)body_lines)
1427 wprintw(modeline, "All ");
1428 else if (tab->window.line_off == 0)
1429 wprintw(modeline, "Top ");
1430 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1431 wprintw(modeline, "Bottom ");
1433 wprintw(modeline, "%.0f%% ", pct);
1435 wprintw(modeline, "%d/%d %s ",
1436 tab->window.line_off + tab->window.curs_y,
1437 tab->window.line_max,
1440 getyx(modeline, y, x);
1441 getmaxyx(modeline, max_y, max_x);
1446 for (; x < max_x; ++x)
1447 waddstr(modeline, "-");
1451 redraw_minibuffer(void)
1454 size_t off_y, off_x = 0;
1459 if (in_minibuffer) {
1460 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1461 if (ministate.hist_cur != NULL)
1462 wprintw(minibuf, "(%zu/%zu) ",
1463 ministate.hist_off + 1,
1464 ministate.history->len);
1466 getyx(minibuf, off_y, off_x);
1468 start = ministate.hist_cur != NULL
1469 ? ministate.hist_cur->h
1471 c = utf8_nth(ministate.window.current_line->line,
1472 ministate.window.cpoff);
1473 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1474 start = utf8_next_cp(start);
1477 waddstr(minibuf, start);
1480 if (ministate.curmesg != NULL)
1481 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1484 if (!in_minibuffer && ministate.curmesg == NULL)
1485 waddstr(minibuf, keybuf);
1487 /* If nothing else, show the URL at point */
1488 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1489 tab = current_tab();
1490 if (tab->window.current_line != NULL &&
1491 tab->window.current_line->parent->type == LINE_LINK)
1492 waddstr(minibuf, tab->window.current_line->parent->alt);
1496 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1500 redraw_tab(struct tab *tab)
1504 redraw_modeline(tab);
1505 redraw_minibuffer();
1510 if (in_minibuffer) {
1520 redraw_body(struct tab *tab)
1527 tab->window.line_off = MIN(tab->window.line_max-1, tab->window.line_off);
1528 if (TAILQ_EMPTY(&tab->window.head))
1532 vl = nth_line(&tab->window, tab->window.line_off);
1533 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1534 wmove(body, line, 0);
1537 if (line == body_lines)
1541 wmove(body, tab->window.curs_y, tab->window.curs_x);
1545 vmessage(const char *fmt, va_list ap)
1547 if (evtimer_pending(&clminibufev, NULL))
1548 evtimer_del(&clminibufev);
1549 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1550 evtimer_add(&clminibufev, &clminibufev_timer);
1552 free(ministate.curmesg);
1554 /* TODO: what to do if the allocation fails here? */
1555 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1556 ministate.curmesg = NULL;
1558 redraw_minibuffer();
1559 if (in_minibuffer) {
1569 message(const char *fmt, ...)
1579 start_loading_anim(struct tab *tab)
1581 if (tab->loading_anim)
1583 tab->loading_anim = 1;
1584 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1585 evtimer_add(&tab->loadingev, &loadingev_timer);
1589 update_loading_anim(int fd, short ev, void *d)
1591 struct tab *tab = d;
1593 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1595 if (tab->flags & TAB_CURRENT) {
1596 redraw_modeline(tab);
1603 evtimer_add(&tab->loadingev, &loadingev_timer);
1607 stop_loading_anim(struct tab *tab)
1609 if (!tab->loading_anim)
1611 evtimer_del(&tab->loadingev);
1612 tab->loading_anim = 0;
1613 tab->loading_anim_step = 0;
1615 if (!(tab->flags & TAB_CURRENT))
1618 redraw_modeline(tab);
1627 load_url_in_tab(struct tab *tab, const char *url)
1629 empty_vlist(&tab->window);
1630 message("Loading %s...", url);
1631 start_loading_anim(tab);
1634 tab->window.curs_x = 0;
1635 tab->window.curs_y = 0;
1640 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1641 void (*abortfn)(void), struct histhead *hist)
1644 base_map = &minibuffer_map;
1645 current_map = &minibuffer_map;
1647 base_map->unhandled_input = self_insert_fn;
1649 ministate.donefn = donefn;
1650 ministate.abortfn = abortfn;
1651 memset(ministate.buf, 0, sizeof(ministate.buf));
1652 ministate.window.current_line = &ministate.vline;
1653 ministate.window.current_line->line = ministate.buf;
1654 ministate.window.cpoff = 0;
1655 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1657 ministate.history = hist;
1658 ministate.hist_cur = NULL;
1659 ministate.hist_off = 0;
1663 exit_minibuffer(void)
1668 base_map = &global_map;
1669 current_map = &global_map;
1673 switch_to_tab(struct tab *tab)
1677 TAILQ_FOREACH(t, &tabshead, tabs) {
1678 t->flags &= ~TAB_CURRENT;
1681 tab->flags |= TAB_CURRENT;
1685 new_tab(const char *url)
1689 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1694 TAILQ_INIT(&tab->hist.head);
1696 TAILQ_INIT(&tab->window.head);
1698 tab->id = tab_counter++;
1701 if (TAILQ_EMPTY(&tabshead))
1702 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1704 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1706 load_url_in_tab(tab, url);
1713 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1717 ui_init(int argc, char * const *argv)
1719 const char *url = NEW_TAB_URL;
1722 while ((ch = getopt(argc, argv, "")) != -1) {
1735 setlocale(LC_ALL, "");
1737 TAILQ_INIT(&global_map.m);
1738 global_map.unhandled_input = global_key_unbound;
1740 TAILQ_INIT(&minibuffer_map.m);
1742 TAILQ_INIT(&eecmd_history.head);
1743 TAILQ_INIT(&ir_history.head);
1744 TAILQ_INIT(&lu_history.head);
1746 ministate.line.type = LINE_TEXT;
1747 ministate.vline.parent = &ministate.line;
1748 ministate.window.current_line = &ministate.vline;
1750 base_map = &global_map;
1751 current_map = &global_map;
1752 load_default_keys();
1759 intrflush(stdscr, FALSE);
1761 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1763 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1765 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1767 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1770 body_lines = LINES-3;
1774 scrollok(body, TRUE);
1776 /* non-blocking input */
1779 mvwprintw(body, 0, 0, "");
1781 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1782 event_add(&stdioev, NULL);
1784 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1785 signal_add(&winchev, NULL);
1793 ui_on_tab_loaded(struct tab *tab)
1795 stop_loading_anim(tab);
1796 message("Loaded %s", tab->hist_cur->h);
1807 ui_on_tab_refresh(struct tab *tab)
1809 wrap_page(&tab->window);
1810 if (tab->flags & TAB_CURRENT) {
1811 restore_cursor(&tab->window);
1817 ui_require_input(struct tab *tab, int hide)
1819 /* TODO: hard-switching to another tab is ugly */
1822 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1824 strlcpy(ministate.prompt, "Input required: ",
1825 sizeof(ministate.prompt));
1830 ui_notify(const char *fmt, ...)