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
48 #define TAB_URGENT 0x2
50 #define NEW_TAB_URL "about:new"
52 static struct event stdioev, winchev;
54 static void load_default_keys(void);
55 static void restore_cursor(struct window*);
57 #define CMD(fnname) static void fnname(struct window *)
58 #define DEFALIAS(s, d) /* nothing */
60 CMD(cmd_previous_line);
62 CMD(cmd_backward_char);
63 CMD(cmd_forward_char);
64 CMD(cmd_backward_paragraph);
65 CMD(cmd_forward_paragraph);
66 CMD(cmd_move_beginning_of_line);
67 CMD(cmd_move_end_of_line);
69 CMD(cmd_scroll_line_down);
70 CMD(cmd_scroll_line_up);
73 CMD(cmd_beginning_of_buffer);
74 CMD(cmd_end_of_buffer);
76 CMD(cmd_kill_telescope);
77 DEFALIAS(q, cmd_kill_telescope)
78 DEFALIAS(wq, cmd_kill_telescope)
81 CMD(cmd_push_button_new_tab);
82 CMD(cmd_previous_button);
84 CMD(cmd_previous_page);
86 CMD(cmd_clear_minibuf);
87 CMD(cmd_execute_extended_command);
89 CMD(cmd_tab_close_other);
92 DEFALIAS(tabnew, cmd_tab_new)
95 DEFALIAS(tabn, cmd_tab_next)
97 CMD(cmd_tab_previous);
98 DEFALIAS(tabp, cmd_tab_previous)
101 CMD(cmd_tab_move_to);
103 CMD(cmd_load_current_url);
104 CMD(cmd_bookmark_page);
105 CMD(cmd_list_bookmarks);
106 CMD(cmd_toggle_help);
108 CMD(cmd_mini_delete_char);
109 CMD(cmd_mini_delete_backward_char);
110 CMD(cmd_mini_kill_line);
112 CMD(cmd_mini_complete_and_exit);
113 CMD(cmd_mini_previous_history_element);
114 CMD(cmd_mini_next_history_element);
118 static void global_key_unbound(void);
119 static void minibuffer_hist_save_entry(void);
120 static void minibuffer_taint_hist(void);
121 static void minibuffer_self_insert(void);
122 static void eecmd_self_insert(void);
123 static void eecmd_select(void);
124 static void ir_self_insert(void);
125 static void ir_select(void);
126 static void lu_self_insert(void);
127 static void lu_select(void);
128 static void bp_select(void);
129 static void yornp_self_insert(void);
130 static void yornp_abort(void);
132 static struct vline *nth_line(struct window*, size_t);
133 static struct tab *current_tab(void);
134 static struct window *current_window(void);
135 static int readkey(void);
136 static void dispatch_stdio(int, short, void*);
137 static void handle_clear_minibuf(int, short, void*);
138 static void handle_resize(int, short, void*);
139 static void handle_resize_timeout(int, short, void*);
140 static int wrap_page(struct window*, int);
141 static void print_vline(WINDOW*, struct vline*);
142 static void redraw_tabline(void);
143 static void redraw_window(WINDOW*, int, struct window*);
144 static void redraw_help(void);
145 static void redraw_body(struct tab*);
146 static void redraw_modeline(struct tab*);
147 static void redraw_minibuffer(void);
148 static void redraw_tab(struct tab*);
149 static void emit_help_item(char*, void*);
150 static void rec_compute_help(struct kmap*, char*, size_t);
151 static void recompute_help(void);
152 static void vmessage(const char*, va_list);
153 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
154 static void start_loading_anim(struct tab*);
155 static void update_loading_anim(int, short, void*);
156 static void stop_loading_anim(struct tab*);
157 static void load_url_in_tab(struct tab*, const char*);
158 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
159 static void exit_minibuffer(void);
160 static void switch_to_tab(struct tab*);
161 static struct tab *new_tab(const char*);
162 static void session_new_tab_cb(const char*);
163 static void usage(void);
165 static struct { short meta; int key; uint32_t cp; } thiskey;
167 static struct event resizeev;
168 static struct timeval resize_timer = { 0, 250000 };
170 static WINDOW *tabline, *body, *modeline, *minibuf;
171 static int body_lines, body_cols;
174 static struct window helpwin;
175 static int help_lines, help_cols;
177 static int side_window;
179 static struct event clminibufev;
180 static struct timeval clminibufev_timer = { 5, 0 };
181 static struct timeval loadingev_timer = { 0, 250000 };
183 static uint32_t tab_counter;
185 static char keybuf[64];
187 static void (*yornp_cb)(int, unsigned int);
188 static unsigned int yornp_data;
190 struct kmap global_map,
195 static struct histhead eecmd_history,
199 static int in_minibuffer;
205 void (*donefn)(void);
206 void (*abortfn)(void);
211 struct window window;
213 struct histhead *history;
214 struct hist *hist_cur;
221 } line_prefixes[] = {
222 [LINE_TEXT] = { "", "" },
223 [LINE_LINK] = { "=> ", " " },
224 [LINE_TITLE_1] = { "# ", " " },
225 [LINE_TITLE_2] = { "## ", " " },
226 [LINE_TITLE_3] = { "### ", " " },
227 [LINE_ITEM] = { "* ", " " },
228 [LINE_QUOTE] = { "> ", " " },
229 [LINE_PRE_START] = { "```", " " },
230 [LINE_PRE_CONTENT] = { "", "" },
231 [LINE_PRE_END] = { "```", "```" },
234 static struct line_face {
238 [LINE_TEXT] = { 0, 0 },
239 [LINE_LINK] = { 0, A_UNDERLINE },
240 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
241 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
242 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
243 [LINE_ITEM] = { 0, 0 },
244 [LINE_QUOTE] = { 0, A_DIM },
245 [LINE_PRE_START] = { 0, 0 },
246 [LINE_PRE_CONTENT] = { 0, 0 },
247 [LINE_PRE_END] = { 0, 0 },
250 static struct tab_face {
251 int background, tab, current_tab;
253 A_REVERSE, A_REVERSE, A_NORMAL
257 global_set_key(const char *key, void (*fn)(struct window*))
259 if (!kmap_define_key(&global_map, key, fn))
264 minibuffer_set_key(const char *key, void (*fn)(struct window*))
266 if (!kmap_define_key(&minibuffer_map, key, fn))
271 load_default_keys(void)
273 /* === global map === */
276 global_set_key("C-p", cmd_previous_line);
277 global_set_key("C-n", cmd_next_line);
278 global_set_key("C-f", cmd_forward_char);
279 global_set_key("C-b", cmd_backward_char);
280 global_set_key("M-{", cmd_backward_paragraph);
281 global_set_key("M-}", cmd_forward_paragraph);
282 global_set_key("C-a", cmd_move_beginning_of_line);
283 global_set_key("C-e", cmd_move_end_of_line);
285 global_set_key("M-v", cmd_scroll_up);
286 global_set_key("C-v", cmd_scroll_down);
287 global_set_key("M-space", cmd_scroll_up);
288 global_set_key("space", cmd_scroll_down);
290 global_set_key("M-<", cmd_beginning_of_buffer);
291 global_set_key("M->", cmd_end_of_buffer);
293 global_set_key("C-x C-c", cmd_kill_telescope);
295 global_set_key("C-g", cmd_clear_minibuf);
297 global_set_key("M-x", cmd_execute_extended_command);
298 global_set_key("C-x C-f", cmd_load_url);
299 global_set_key("C-x M-f", cmd_load_current_url);
301 global_set_key("C-x t 0", cmd_tab_close);
302 global_set_key("C-x t 1", cmd_tab_close_other);
303 global_set_key("C-x t 2", cmd_tab_new);
304 global_set_key("C-x t o", cmd_tab_next);
305 global_set_key("C-x t O", cmd_tab_previous);
306 global_set_key("C-x t m", cmd_tab_move);
307 global_set_key("C-x t M", cmd_tab_move_to);
309 global_set_key("C-M-b", cmd_previous_page);
310 global_set_key("C-M-f", cmd_next_page);
312 global_set_key("<f7> a", cmd_bookmark_page);
313 global_set_key("<f7> <f7>", cmd_list_bookmarks);
316 global_set_key("k", cmd_previous_line);
317 global_set_key("j", cmd_next_line);
318 global_set_key("l", cmd_forward_char);
319 global_set_key("h", cmd_backward_char);
320 global_set_key("{", cmd_backward_paragraph);
321 global_set_key("}", cmd_forward_paragraph);
322 global_set_key("^", cmd_move_beginning_of_line);
323 global_set_key("$", cmd_move_end_of_line);
325 global_set_key("K", cmd_scroll_line_up);
326 global_set_key("J", cmd_scroll_line_down);
328 global_set_key("g g", cmd_beginning_of_buffer);
329 global_set_key("G", cmd_end_of_buffer);
331 global_set_key("g D", cmd_tab_close);
332 global_set_key("g N", cmd_tab_new);
333 global_set_key("g t", cmd_tab_next);
334 global_set_key("g T", cmd_tab_previous);
335 global_set_key("g M-t", cmd_tab_move);
336 global_set_key("g M-T", cmd_tab_move_to);
338 global_set_key("H", cmd_previous_page);
339 global_set_key("L", cmd_next_page);
342 global_set_key("q", cmd_kill_telescope);
344 global_set_key("esc", cmd_clear_minibuf);
346 global_set_key(":", cmd_execute_extended_command);
349 global_set_key("<up>", cmd_previous_line);
350 global_set_key("<down>", cmd_next_line);
351 global_set_key("<right>", cmd_forward_char);
352 global_set_key("<left>", cmd_backward_char);
353 global_set_key("<prior>", cmd_scroll_up);
354 global_set_key("<next>", cmd_scroll_down);
356 global_set_key("M-<left>", cmd_previous_page);
357 global_set_key("M-<right>", cmd_next_page);
359 /* "ncurses standard" */
360 global_set_key("C-l", cmd_redraw);
363 global_set_key("<f1>", cmd_toggle_help);
364 global_set_key("C-m", cmd_push_button);
365 global_set_key("M-enter", cmd_push_button_new_tab);
366 global_set_key("M-tab", cmd_previous_button);
367 global_set_key("tab", cmd_next_button);
369 /* === minibuffer map === */
370 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
371 minibuffer_set_key("C-g", cmd_mini_abort);
372 minibuffer_set_key("esc", cmd_mini_abort);
373 minibuffer_set_key("C-d", cmd_mini_delete_char);
374 minibuffer_set_key("del", cmd_mini_delete_backward_char);
375 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
376 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
378 minibuffer_set_key("C-b", cmd_backward_char);
379 minibuffer_set_key("C-f", cmd_forward_char);
380 minibuffer_set_key("<left>", cmd_backward_char);
381 minibuffer_set_key("<right>", cmd_forward_char);
382 minibuffer_set_key("C-e", cmd_move_end_of_line);
383 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
384 minibuffer_set_key("<end>", cmd_move_end_of_line);
385 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
386 minibuffer_set_key("C-k", cmd_mini_kill_line);
388 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
389 minibuffer_set_key("M-n", cmd_mini_next_history_element);
390 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
391 minibuffer_set_key("<down>", cmd_mini_next_history_element);
395 restore_cursor(struct window *window)
400 vl = window->current_line;
401 if (vl == NULL || vl->line == NULL)
402 window->curs_x = window->cpoff = 0;
404 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
407 prfx = line_prefixes[vl->parent->type].prfx1;
408 window->curs_x += utf8_swidth(prfx);
413 cmd_previous_line(struct window *window)
417 if (window->current_line == NULL
418 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
421 if (--window->curs_y < 0) {
423 cmd_scroll_line_up(window);
427 window->current_line = vl;
428 restore_cursor(window);
432 cmd_next_line(struct window *window)
436 if (window->current_line == NULL
437 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
440 if (++window->curs_y > body_lines-1) {
441 window->curs_y = body_lines-1;
442 cmd_scroll_line_down(window);
446 window->current_line = vl;
447 restore_cursor(window);
451 cmd_backward_char(struct window *window)
453 if (window->cpoff != 0)
455 restore_cursor(window);
459 cmd_forward_char(struct window *window)
463 if (window->current_line->line != NULL)
464 len = utf8_cplen(window->current_line->line);
465 if (++window->cpoff > len)
467 restore_cursor(window);
471 cmd_backward_paragraph(struct window *window)
474 if (window->current_line == NULL ||
475 window->current_line == TAILQ_FIRST(&window->head)) {
476 message("No previous paragraph");
479 cmd_previous_line(window);
480 } while (window->current_line->line != NULL ||
481 window->current_line->parent->type != LINE_TEXT);
485 cmd_forward_paragraph(struct window *window)
488 if (window->current_line == NULL ||
489 window->current_line == TAILQ_LAST(&window->head, vhead)) {
490 message("No next paragraph");
493 cmd_next_line(window);
494 } while (window->current_line->line != NULL ||
495 window->current_line->parent->type != LINE_TEXT);
499 cmd_move_beginning_of_line(struct window *window)
502 restore_cursor(window);
506 cmd_move_end_of_line(struct window *window)
510 vl = window->current_line;
511 if (vl->line == NULL)
513 window->cpoff = utf8_cplen(vl->line);
514 restore_cursor(window);
518 cmd_redraw(struct window *window)
520 handle_resize(0, 0, NULL);
524 cmd_scroll_line_up(struct window *window)
528 if (window->line_off == 0)
531 vl = nth_line(window, --window->line_off);
534 print_vline(body, vl);
536 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
537 restore_cursor(window);
541 cmd_scroll_line_down(struct window *window)
545 vl = window->current_line;
546 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
548 window->current_line = vl;
553 if (window->line_max - window->line_off < (size_t)body_lines)
556 vl = nth_line(window, window->line_off + body_lines-1);
557 wmove(body, body_lines-1, 0);
558 print_vline(body, vl);
560 restore_cursor(window);
564 cmd_scroll_up(struct window *window)
570 for (; off > 0; --off)
571 cmd_scroll_line_up(window);
575 cmd_scroll_down(struct window *window)
581 for (; off > 0; --off)
582 cmd_scroll_line_down(window);
586 cmd_beginning_of_buffer(struct window *window)
588 window->current_line = TAILQ_FIRST(&window->head);
589 window->line_off = 0;
592 restore_cursor(window);
596 cmd_end_of_buffer(struct window *window)
600 off = window->line_max - body_lines;
603 window->line_off = off;
604 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
606 window->current_line = TAILQ_LAST(&window->head, vhead);
607 window->cpoff = body_cols;
608 restore_cursor(window);
612 cmd_kill_telescope(struct window *window)
619 cmd_push_button(struct window *window)
624 nth = window->line_off + window->curs_y;
625 if (nth >= window->line_max)
627 vl = nth_line(window, nth);
628 if (vl->parent->type != LINE_LINK)
631 load_url_in_tab(current_tab(), vl->parent->alt);
635 cmd_push_button_new_tab(struct window *window)
640 nth = window->line_off + window->curs_y;
641 if (nth > window->line_max)
643 vl = nth_line(window, nth);
644 if (vl->parent->type != LINE_LINK)
647 new_tab(vl->parent->alt);
651 cmd_previous_button(struct window *window)
654 if (window->current_line == NULL ||
655 window->current_line == TAILQ_FIRST(&window->head)) {
656 message("No previous link");
659 cmd_previous_line(window);
660 } while (window->current_line->parent->type != LINE_LINK);
664 cmd_next_button(struct window *window)
667 if (window->current_line == NULL ||
668 window->current_line == TAILQ_LAST(&window->head, vhead)) {
669 message("No next link");
672 cmd_next_line(window);
673 } while (window->current_line->parent->type != LINE_LINK);
677 cmd_previous_page(struct window *window)
679 struct tab *tab = current_tab();
681 if (!load_previous_page(tab))
682 message("No previous page");
684 start_loading_anim(tab);
688 cmd_next_page(struct window *window)
690 struct tab *tab = current_tab();
692 if (!load_next_page(tab))
693 message("No next page");
695 start_loading_anim(tab);
699 cmd_clear_minibuf(struct window *window)
701 handle_clear_minibuf(0, 0, NULL);
705 cmd_execute_extended_command(struct window *window)
710 message("We don't have enable-recursive-minibuffers");
714 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
717 len = sizeof(ministate.prompt);
718 strlcpy(ministate.prompt, "", len);
721 strlcat(ministate.prompt, "M-", len);
723 strlcat(ministate.prompt, keyname(thiskey.key), len);
726 strlcat(ministate.prompt, " ", len);
730 cmd_tab_close(struct window *window)
735 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
736 TAILQ_NEXT(tab, tabs) == NULL) {
737 message("Can't close the only tab.");
741 if (evtimer_pending(&tab->loadingev, NULL))
742 evtimer_del(&tab->loadingev);
746 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
747 t = TAILQ_NEXT(tab, tabs);
748 TAILQ_REMOVE(&tabshead, tab, tabs);
755 cmd_tab_close_other(struct window *window)
759 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
760 if (t->flags & TAB_CURRENT)
764 TAILQ_REMOVE(&tabshead, t, tabs);
770 cmd_tab_new(struct window *window)
772 new_tab(NEW_TAB_URL);
776 cmd_tab_next(struct window *window)
781 tab->flags &= ~TAB_CURRENT;
783 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
784 t = TAILQ_FIRST(&tabshead);
785 t->flags |= TAB_CURRENT;
786 t->flags &= ~TAB_URGENT;
790 cmd_tab_previous(struct window *window)
795 tab->flags &= ~TAB_CURRENT;
797 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
798 t = TAILQ_LAST(&tabshead, tabshead);
799 t->flags |= TAB_CURRENT;
800 t->flags &= ~TAB_URGENT;
804 cmd_tab_move(struct window *window)
809 t = TAILQ_NEXT(tab, tabs);
810 TAILQ_REMOVE(&tabshead, tab, tabs);
813 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
815 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
819 cmd_tab_move_to(struct window *window)
824 t = TAILQ_PREV(tab, tabshead, tabs);
825 TAILQ_REMOVE(&tabshead, tab, tabs);
828 if (TAILQ_EMPTY(&tabshead))
829 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
831 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
833 TAILQ_INSERT_BEFORE(t, tab, tabs);
837 cmd_load_url(struct window *window)
840 message("We don't have enable-recursive-minibuffers");
844 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
846 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
847 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
848 cmd_move_end_of_line(&ministate.window);
852 cmd_load_current_url(struct window *window)
854 struct tab *tab = current_tab();
857 message("We don't have enable-recursive-minibuffers");
861 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
863 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
864 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
865 ministate.window.cpoff = utf8_cplen(ministate.buf);
869 cmd_bookmark_page(struct window *window)
871 struct tab *tab = current_tab();
873 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
874 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
875 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
876 ministate.window.cpoff = utf8_cplen(ministate.buf);
880 cmd_list_bookmarks(struct window *window)
882 load_url_in_tab(current_tab(), "about:bookmarks");
886 cmd_toggle_help(struct window *window)
888 side_window = !side_window;
893 * ugly hack, but otherwise the window doesn't get updated
894 * until I call handle_resize a second time (i.e. C-l). I
895 * will be happy to know why something like this is needed.
897 handle_resize(0, 0, NULL);
898 handle_resize(0, 0, NULL);
902 cmd_mini_delete_char(struct window *window)
906 if (!in_minibuffer) {
907 message("text is read-only");
911 minibuffer_taint_hist();
913 c = utf8_nth(window->current_line->line, window->cpoff);
918 memmove(c, n, strlen(n)+1);
922 cmd_mini_delete_backward_char(struct window *window)
926 if (!in_minibuffer) {
927 message("text is read-only");
931 minibuffer_taint_hist();
933 c = utf8_nth(window->current_line->line, window->cpoff);
934 start = window->current_line->line;
937 p = utf8_prev_cp(c-1, start);
939 memmove(p, c, strlen(c)+1);
944 cmd_mini_kill_line(struct window *window)
948 if (!in_minibuffer) {
949 message("text is read-only");
953 minibuffer_taint_hist();
954 c = utf8_nth(window->current_line->line, window->cpoff);
959 cmd_mini_abort(struct window *window)
968 cmd_mini_complete_and_exit(struct window *window)
973 minibuffer_taint_hist();
978 cmd_mini_previous_history_element(struct window *window)
980 if (ministate.history == NULL) {
981 message("No history");
985 if (ministate.hist_cur == NULL ||
986 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
987 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
988 ministate.hist_off = ministate.history->len - 1;
989 if (ministate.hist_cur == NULL)
990 message("No prev item");
992 ministate.hist_off--;
995 if (ministate.hist_cur != NULL)
996 window->current_line->line = ministate.hist_cur->h;
1000 cmd_mini_next_history_element(struct window *window)
1002 if (ministate.history == NULL) {
1003 message("No history");
1007 if (ministate.hist_cur == NULL ||
1008 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
1009 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
1010 ministate.hist_off = 0;
1011 if (ministate.hist_cur == NULL)
1012 message("No next item");
1014 ministate.hist_off++;
1017 if (ministate.hist_cur != NULL)
1018 window->current_line->line = ministate.hist_cur->h;
1022 global_key_unbound(void)
1024 message("%s is undefined", keybuf);
1028 minibuffer_hist_save_entry(void)
1032 if (ministate.history == NULL)
1035 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1038 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1040 if (TAILQ_EMPTY(&ministate.history->head))
1041 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1043 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1044 ministate.history->len++;
1048 * taint the minibuffer cache: if we're currently showing a history
1049 * element, copy that to the current buf and reset the "history
1050 * navigation" thing.
1053 minibuffer_taint_hist(void)
1055 if (ministate.hist_cur == NULL)
1058 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1059 ministate.hist_cur = NULL;
1063 minibuffer_self_insert(void)
1065 char *c, tmp[5] = {0};
1068 minibuffer_taint_hist();
1070 if (thiskey.cp == 0)
1073 len = utf8_encode(thiskey.cp, tmp);
1074 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1075 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1078 memmove(c + len, c, strlen(c)+1);
1079 memcpy(c, tmp, len);
1080 ministate.window.cpoff++;
1084 eecmd_self_insert(void)
1086 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1087 !unicode_isgraph(thiskey.cp)) {
1088 global_key_unbound();
1092 minibuffer_self_insert();
1100 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1101 if (!strcmp(cmd->cmd, ministate.buf)) {
1103 minibuffer_hist_save_entry();
1104 cmd->fn(current_window());
1109 message("No match");
1113 ir_self_insert(void)
1115 minibuffer_self_insert();
1121 char buf[1025] = {0};
1122 struct phos_uri uri;
1125 tab = current_tab();
1128 minibuffer_hist_save_entry();
1130 /* a bit ugly but... */
1131 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1132 phos_uri_set_query(&uri, ministate.buf);
1133 phos_serialize_uri(&uri, buf, sizeof(buf));
1134 load_url_in_tab(tab, buf);
1138 lu_self_insert(void)
1140 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1141 !unicode_isgraph(thiskey.key)) {
1142 global_key_unbound();
1146 minibuffer_self_insert();
1153 minibuffer_hist_save_entry();
1154 load_url_in_tab(current_tab(), ministate.buf);
1161 if (*ministate.buf != '\0')
1162 add_to_bookmarks(ministate.buf);
1168 yornp_self_insert(void)
1170 if (thiskey.key != 'y' && thiskey.key != 'n') {
1171 message("Please answer y or n");
1176 yornp_cb(thiskey.key == 'y', yornp_data);
1183 yornp_cb(0, yornp_data);
1186 static struct vline *
1187 nth_line(struct window *window, size_t n)
1193 TAILQ_FOREACH(vl, &window->head, vlines) {
1208 TAILQ_FOREACH(t, &tabshead, tabs) {
1209 if (t->flags & TAB_CURRENT)
1217 static struct window *
1218 current_window(void)
1221 return &ministate.window;
1222 return ¤t_tab()->window;
1230 if ((thiskey.key = wgetch(body)) == ERR)
1233 thiskey.meta = thiskey.key == 27;
1235 thiskey.key = wgetch(body);
1236 if (thiskey.key == ERR || thiskey.key == 27) {
1243 if ((unsigned int)thiskey.key < UINT8_MAX) {
1245 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1247 if ((thiskey.key = wgetch(body)) == ERR) {
1248 message("Error decoding user input");
1258 dispatch_stdio(int fd, short ev, void *d)
1261 const char *keyname;
1267 if (keybuf[0] != '\0')
1268 strlcat(keybuf, " ", sizeof(keybuf));
1270 strlcat(keybuf, "M-", sizeof(keybuf));
1271 if (thiskey.cp != 0) {
1272 utf8_encode(thiskey.cp, tmp);
1273 strlcat(keybuf, tmp, sizeof(keybuf));
1275 if ((keyname = unkbd(thiskey.key)) != NULL)
1276 strlcat(keybuf, keyname, sizeof(keybuf));
1278 tmp[0] = thiskey.key;
1279 strlcat(keybuf, tmp, sizeof(keybuf));
1283 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1284 if (k->meta == thiskey.meta &&
1285 k->key == thiskey.key) {
1287 current_map = &k->map;
1289 current_map = base_map;
1290 strlcpy(keybuf, "", sizeof(keybuf));
1291 k->fn(current_window());
1297 if (current_map->unhandled_input != NULL)
1298 current_map->unhandled_input();
1300 global_key_unbound();
1303 strlcpy(keybuf, "", sizeof(keybuf));
1304 current_map = base_map;
1310 redraw_tab(current_tab());
1314 handle_clear_minibuf(int fd, short ev, void *d)
1316 free(ministate.curmesg);
1317 ministate.curmesg = NULL;
1319 redraw_minibuffer();
1320 if (in_minibuffer) {
1330 handle_resize(int sig, short ev, void *d)
1332 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1333 event_del(&resizeev);
1335 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1336 evtimer_add(&resizeev, &resize_timer);
1340 handle_resize_timeout(int s, short ev, void *d)
1348 /* move and resize the windows, in reverse order! */
1350 mvwin(minibuf, LINES-1, 0);
1351 wresize(minibuf, 1, COLS);
1353 mvwin(modeline, LINES-2, 0);
1354 wresize(modeline, 1, COLS);
1356 body_lines = LINES-3;
1360 help_cols = 0.3 * COLS;
1361 help_lines = LINES-3;
1363 wresize(help, help_lines, help_cols);
1365 wrap_page(&helpwin, help_cols);
1367 body_cols = COLS - help_cols - 1;
1368 mvwin(body, 1, help_cols);
1372 wresize(body, body_lines, body_cols);
1374 wresize(tabline, 1, COLS);
1376 tab = current_tab();
1378 wrap_page(&tab->window, body_cols);
1383 wrap_page(struct window *window, int width)
1386 const struct line *orig;
1390 orig = window->current_line == NULL
1392 : window->current_line->parent;
1393 window->current_line = NULL;
1396 window->line_off = 0;
1398 empty_vlist(window);
1400 TAILQ_FOREACH(l, &window->page.head, lines) {
1401 prfx = line_prefixes[l->type].prfx1;
1410 case LINE_PRE_START:
1412 wrap_text(window, prfx, l, width);
1414 case LINE_PRE_CONTENT:
1415 hardwrap_text(window, l, width);
1419 if (orig == l && window->current_line == NULL) {
1420 window->line_off = window->line_max-1;
1421 window->current_line = TAILQ_LAST(&window->head, vhead);
1424 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1425 if (vl == NULL || vl->parent != orig)
1427 window->current_line = vl;
1433 if (window->current_line == NULL)
1434 window->current_line = TAILQ_FIRST(&window->head);
1440 print_vline(WINDOW *window, struct vline *vl)
1442 const char *text = vl->line;
1444 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1445 int text_face = line_faces[vl->parent->type].text_prop;
1448 prfx = line_prefixes[vl->parent->type].prfx1;
1450 prfx = line_prefixes[vl->parent->type].prfx2;
1455 wattron(window, prefix_face);
1456 wprintw(window, "%s", prfx);
1457 wattroff(window, prefix_face);
1459 wattron(window, text_face);
1460 wprintw(window, "%s", text);
1461 wattroff(window, text_face);
1465 redraw_tabline(void)
1468 size_t toskip, ots, tabwidth, space, x;
1469 int current, y, truncated;
1473 tabwidth = sizeof(buf)+1;
1477 TAILQ_FOREACH(tab, &tabshead, tabs) {
1479 if (tab->flags & TAB_CURRENT)
1483 if (toskip * tabwidth < space)
1488 while (toskip != 0 &&
1489 (ots - toskip+1) * tabwidth < space)
1494 wattron(tabline, tab_face.background);
1495 wprintw(tabline, toskip == 0 ? " " : "<");
1496 wattroff(tabline, tab_face.background);
1499 TAILQ_FOREACH(tab, &tabshead, tabs) {
1507 getyx(tabline, y, x);
1508 if (x + sizeof(buf)+2 >= (size_t)COLS)
1511 current = tab->flags & TAB_CURRENT;
1513 if (*(title = tab->window.page.title) == '\0')
1514 title = tab->hist_cur->h;
1516 if (tab->flags & TAB_URGENT)
1517 strlcpy(buf, "!", sizeof(buf));
1519 strlcpy(buf, " ", sizeof(buf));
1521 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1522 /* truncation happens */
1523 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1525 /* pad with spaces */
1526 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1531 wattron(tabline, tab_face.current_tab);
1533 wattron(tabline, tab_face.tab);
1535 wprintw(tabline, "%s", buf);
1536 if (TAILQ_NEXT(tab, tabs) != NULL)
1537 wprintw(tabline, " ");
1540 wattroff(tabline, tab_face.current_tab);
1542 wattroff(tabline, tab_face.tab);
1545 wattron(tabline, tab_face.background);
1546 for (; x < (size_t)COLS; ++x)
1547 waddch(tabline, ' ');
1549 mvwprintw(tabline, 0, COLS-1, ">");
1553 redraw_window(WINDOW *win, int height, struct window *window)
1560 window->line_off = MIN(window->line_max-1, window->line_off);
1561 if (TAILQ_EMPTY(&window->head))
1565 vl = nth_line(window, window->line_off);
1566 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1568 print_vline(win, vl);
1574 wmove(win, window->curs_y, window->curs_x);
1580 redraw_window(help, help_lines, &helpwin);
1584 redraw_body(struct tab *tab)
1586 redraw_window(body, body_lines, &tab->window);
1590 trust_status_char(enum trust_state ts)
1593 case TS_UNKNOWN: return 'u';
1594 case TS_UNTRUSTED: return '!';
1595 case TS_TRUSTED: return 'v';
1596 case TS_VERIFIED: return 'V';
1601 redraw_modeline(struct tab *tab)
1604 int x, y, max_x, max_y;
1605 const char *mode = tab->window.page.name;
1606 const char *spin = "-\\|/";
1609 wattron(modeline, A_REVERSE);
1610 wmove(modeline, 0, 0);
1612 wprintw(modeline, "-%c%c %s ",
1613 spin[tab->loading_anim_step],
1614 trust_status_char(tab->trust),
1615 mode == NULL ? "(none)" : mode);
1617 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1619 if (tab->window.line_max <= (size_t)body_lines)
1620 wprintw(modeline, "All ");
1621 else if (tab->window.line_off == 0)
1622 wprintw(modeline, "Top ");
1623 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1624 wprintw(modeline, "Bottom ");
1626 wprintw(modeline, "%.0f%% ", pct);
1628 wprintw(modeline, "%d/%d %s ",
1629 tab->window.line_off + tab->window.curs_y,
1630 tab->window.line_max,
1633 getyx(modeline, y, x);
1634 getmaxyx(modeline, max_y, max_x);
1639 for (; x < max_x; ++x)
1640 waddstr(modeline, "-");
1644 redraw_minibuffer(void)
1647 size_t off_y, off_x = 0;
1652 if (in_minibuffer) {
1653 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1654 if (ministate.hist_cur != NULL)
1655 wprintw(minibuf, "(%zu/%zu) ",
1656 ministate.hist_off + 1,
1657 ministate.history->len);
1659 getyx(minibuf, off_y, off_x);
1661 start = ministate.hist_cur != NULL
1662 ? ministate.hist_cur->h
1664 c = utf8_nth(ministate.window.current_line->line,
1665 ministate.window.cpoff);
1666 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1667 start = utf8_next_cp(start);
1670 waddstr(minibuf, start);
1673 if (ministate.curmesg != NULL)
1674 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1677 if (!in_minibuffer && ministate.curmesg == NULL)
1678 waddstr(minibuf, keybuf);
1680 /* If nothing else, show the URL at point */
1681 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1682 tab = current_tab();
1683 if (tab->window.current_line != NULL &&
1684 tab->window.current_line->parent->type == LINE_LINK)
1685 waddstr(minibuf, tab->window.current_line->parent->alt);
1689 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1693 redraw_tab(struct tab *tab)
1702 redraw_modeline(tab);
1703 redraw_minibuffer();
1705 wnoutrefresh(tabline);
1706 wnoutrefresh(modeline);
1708 if (in_minibuffer) {
1710 wnoutrefresh(minibuf);
1712 wnoutrefresh(minibuf);
1720 emit_help_item(char *prfx, void *fn)
1725 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1729 assert(cmd != NULL);
1731 if ((l = calloc(1, sizeof(*l))) == NULL)
1734 l->type = LINE_TEXT;
1737 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1739 if (TAILQ_EMPTY(&helpwin.page.head))
1740 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1742 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1746 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1752 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1753 strlcpy(p, prfx, sizeof(p));
1755 strlcat(p, " ", sizeof(p));
1757 strlcat(p, "M-", sizeof(p));
1758 if ((kn = unkbd(k->key)) != NULL)
1759 strlcat(p, kn, sizeof(p));
1761 strlcat(p, keyname(k->key), sizeof(p));
1764 rec_compute_help(&k->map, p, sizeof(p));
1766 emit_help_item(p, k->fn);
1771 recompute_help(void)
1775 empty_vlist(&helpwin);
1776 empty_linelist(&helpwin);
1777 rec_compute_help(current_map, p, sizeof(p));
1778 wrap_page(&helpwin, help_cols);
1782 vmessage(const char *fmt, va_list ap)
1784 if (evtimer_pending(&clminibufev, NULL))
1785 evtimer_del(&clminibufev);
1786 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1787 evtimer_add(&clminibufev, &clminibufev_timer);
1789 free(ministate.curmesg);
1791 /* TODO: what to do if the allocation fails here? */
1792 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1793 ministate.curmesg = NULL;
1795 redraw_minibuffer();
1796 if (in_minibuffer) {
1806 message(const char *fmt, ...)
1816 start_loading_anim(struct tab *tab)
1818 if (tab->loading_anim)
1820 tab->loading_anim = 1;
1821 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1822 evtimer_add(&tab->loadingev, &loadingev_timer);
1826 update_loading_anim(int fd, short ev, void *d)
1828 struct tab *tab = d;
1830 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1832 if (tab->flags & TAB_CURRENT) {
1833 redraw_modeline(tab);
1840 evtimer_add(&tab->loadingev, &loadingev_timer);
1844 stop_loading_anim(struct tab *tab)
1846 if (!tab->loading_anim)
1848 evtimer_del(&tab->loadingev);
1849 tab->loading_anim = 0;
1850 tab->loading_anim_step = 0;
1852 if (!(tab->flags & TAB_CURRENT))
1855 redraw_modeline(tab);
1864 load_url_in_tab(struct tab *tab, const char *url)
1866 message("Loading %s...", url);
1867 start_loading_anim(tab);
1870 tab->window.curs_x = 0;
1871 tab->window.curs_y = 0;
1876 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1877 void (*abortfn)(void), struct histhead *hist)
1880 base_map = &minibuffer_map;
1881 current_map = &minibuffer_map;
1883 base_map->unhandled_input = self_insert_fn;
1885 ministate.donefn = donefn;
1886 ministate.abortfn = abortfn;
1887 memset(ministate.buf, 0, sizeof(ministate.buf));
1888 ministate.window.current_line = &ministate.vline;
1889 ministate.window.current_line->line = ministate.buf;
1890 ministate.window.cpoff = 0;
1891 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1893 ministate.history = hist;
1894 ministate.hist_cur = NULL;
1895 ministate.hist_off = 0;
1899 exit_minibuffer(void)
1904 base_map = &global_map;
1905 current_map = &global_map;
1909 switch_to_tab(struct tab *tab)
1913 TAILQ_FOREACH(t, &tabshead, tabs) {
1914 t->flags &= ~TAB_CURRENT;
1917 tab->flags |= TAB_CURRENT;
1923 return tab_counter++;
1927 new_tab(const char *url)
1931 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1936 TAILQ_INIT(&tab->hist.head);
1938 TAILQ_INIT(&tab->window.head);
1940 tab->id = tab_new_id();
1943 if (TAILQ_EMPTY(&tabshead))
1944 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1946 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1948 load_url_in_tab(tab, url);
1953 session_new_tab_cb(const char *url)
1961 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1965 ui_init(int argc, char * const *argv)
1967 const char *url = NEW_TAB_URL;
1970 while ((ch = getopt(argc, argv, "")) != -1) {
1983 setlocale(LC_ALL, "");
1985 TAILQ_INIT(&global_map.m);
1986 global_map.unhandled_input = global_key_unbound;
1988 TAILQ_INIT(&minibuffer_map.m);
1990 TAILQ_INIT(&eecmd_history.head);
1991 TAILQ_INIT(&ir_history.head);
1992 TAILQ_INIT(&lu_history.head);
1994 ministate.line.type = LINE_TEXT;
1995 ministate.vline.parent = &ministate.line;
1996 ministate.window.current_line = &ministate.vline;
1998 /* initialize help window */
1999 TAILQ_INIT(&helpwin.head);
2001 base_map = &global_map;
2002 current_map = &global_map;
2003 load_default_keys();
2010 intrflush(stdscr, FALSE);
2012 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2014 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2016 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2018 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2020 if ((help = newwin(1, 1, 1, 0)) == NULL)
2023 body_lines = LINES-3;
2027 scrollok(body, TRUE);
2029 /* non-blocking input */
2032 mvwprintw(body, 0, 0, "");
2034 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2035 event_add(&stdioev, NULL);
2037 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2038 signal_add(&winchev, NULL);
2040 load_last_session(session_new_tab_cb);
2041 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2048 ui_on_tab_loaded(struct tab *tab)
2050 stop_loading_anim(tab);
2051 message("Loaded %s", tab->hist_cur->h);
2062 ui_on_tab_refresh(struct tab *tab)
2064 wrap_page(&tab->window, body_cols);
2065 if (tab->flags & TAB_CURRENT) {
2066 restore_cursor(&tab->window);
2069 tab->flags |= TAB_URGENT;
2073 ui_require_input(struct tab *tab, int hide)
2075 /* TODO: hard-switching to another tab is ugly */
2078 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2080 strlcpy(ministate.prompt, "Input required: ",
2081 sizeof(ministate.prompt));
2086 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2091 if (in_minibuffer) {
2098 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2101 len = sizeof(ministate.prompt);
2102 strlcpy(ministate.prompt, prompt, len);
2103 strlcat(ministate.prompt, " (y or n) ", len);
2104 redraw_tab(current_tab());
2108 ui_notify(const char *fmt, ...)