Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
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
27 * first/last line!
28 *
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''.
32 *
33 * TODO: make the text formatting on-demand.
34 *
35 */
37 #include <telescope.h>
39 #include <ctype.h>
40 #include <curses.h>
41 #include <event.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #define TAB_CURRENT 0x1
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void empty_vlist(struct tab*);
57 static void restore_cursor(struct tab *);
59 static void cmd_previous_line(struct tab*);
60 static void cmd_next_line(struct tab*);
61 static void cmd_backward_char(struct tab*);
62 static void cmd_forward_char(struct tab*);
63 static void cmd_backward_paragraph(struct tab*);
64 static void cmd_forward_paragraph(struct tab*);
65 static void cmd_move_beginning_of_line(struct tab*);
66 static void cmd_move_end_of_line(struct tab*);
67 static void cmd_redraw(struct tab*);
68 static void cmd_scroll_line_down(struct tab*);
69 static void cmd_scroll_line_up(struct tab*);
70 static void cmd_scroll_up(struct tab*);
71 static void cmd_scroll_down(struct tab*);
72 static void cmd_beginning_of_buffer(struct tab*);
73 static void cmd_end_of_buffer(struct tab*);
74 static void cmd_kill_telescope(struct tab*);
75 static void cmd_push_button(struct tab*);
76 static void cmd_push_button_new_tab(struct tab*);
77 static void cmd_previous_button(struct tab*);
78 static void cmd_next_button(struct tab*);
79 static void cmd_previous_page(struct tab*);
80 static void cmd_next_page(struct tab*);
81 static void cmd_clear_minibuf(struct tab*);
82 static void cmd_execute_extended_command(struct tab*);
83 static void cmd_tab_close(struct tab*);
84 static void cmd_tab_close_other(struct tab*);
85 static void cmd_tab_new(struct tab*);
86 static void cmd_tab_next(struct tab*);
87 static void cmd_tab_previous(struct tab*);
88 static void cmd_load_url(struct tab*);
89 static void cmd_load_current_url(struct tab*);
90 static void cmd_bookmark_page(struct tab*);
91 static void cmd_goto_bookmarks(struct tab*);
93 static void global_key_unbound(void);
95 static void cmd_mini_delete_char(struct tab*);
96 static void cmd_mini_delete_backward_char(struct tab*);
97 static void cmd_mini_forward_char(struct tab*);
98 static void cmd_mini_backward_char(struct tab*);
99 static void cmd_mini_move_end_of_line(struct tab*);
100 static void cmd_mini_move_beginning_of_line(struct tab*);
101 static void cmd_mini_kill_line(struct tab*);
102 static void cmd_mini_abort(struct tab*);
103 static void cmd_mini_complete_and_exit(struct tab*);
104 static void cmd_mini_previous_history_element(struct tab*);
105 static void cmd_mini_next_history_element(struct tab*);
107 static void minibuffer_hist_save_entry(void);
108 static void minibuffer_taint_hist(void);
109 static void minibuffer_self_insert(void);
110 static void eecmd_self_insert(void);
111 static void eecmd_select(void);
112 static void ir_self_insert(void);
113 static void ir_select(void);
114 static void lu_self_insert(void);
115 static void lu_select(void);
116 static void bp_select(void);
118 static struct vline *nth_line(struct tab*, size_t);
119 static struct tab *current_tab(void);
120 static void dispatch_stdio(int, short, void*);
121 static void handle_clear_minibuf(int, short, void*);
122 static void handle_resize(int, short, void*);
123 static int wrap_page(struct tab*);
124 static void print_vline(struct vline*);
125 static void redraw_tabline(void);
126 static void redraw_body(struct tab*);
127 static void redraw_modeline(struct tab*);
128 static void redraw_minibuffer(void);
129 static void redraw_tab(struct tab*);
130 static void vmessage(const char*, va_list);
131 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
132 static void start_loading_anim(struct tab*);
133 static void update_loading_anim(int, short, void*);
134 static void stop_loading_anim(struct tab*);
135 static void load_url_in_tab(struct tab*, const char*);
136 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
137 static void exit_minibuffer(void);
138 static void switch_to_tab(struct tab*);
139 static struct tab *new_tab(const char*);
140 static void usage(void);
142 static struct { int meta, key; } thiskey;
144 static WINDOW *tabline, *body, *modeline, *minibuf;
145 static int body_lines, body_cols;
147 static struct event clminibufev;
148 static struct timeval clminibufev_timer = { 5, 0 };
149 static struct timeval loadingev_timer = { 0, 250000 };
151 static uint32_t tab_counter;
153 static char keybuf[64];
155 struct kmap global_map,
156 minibuffer_map,
157 *current_map,
158 *base_map;
160 static struct histhead eecmd_history,
161 ir_history,
162 lu_history;
164 static int in_minibuffer;
166 static struct {
167 char *curmesg;
169 char buf[1025];
170 size_t off, len;
171 char prompt[32];
172 void (*donefn)(void);
173 void (*abortfn)(void);
175 struct histhead *history;
176 struct hist *hist_cur;
177 size_t hist_off;
178 } ministate;
180 struct lineprefix {
181 const char *prfx1;
182 const char *prfx2;
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] = { "```", "```" },
194 };
196 static struct line_face {
197 int prefix_prop;
198 int text_prop;
199 } line_faces[] = {
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 },
210 };
212 static struct tab_face {
213 int background, tab, current_tab;
214 } tab_face = {
215 A_REVERSE, A_REVERSE, A_NORMAL
216 };
218 static void
219 empty_vlist(struct tab *tab)
221 struct vline *vl, *t;
223 tab->s.current_line = NULL;
224 tab->s.line_max = 0;
226 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
227 TAILQ_REMOVE(&tab->s.head, vl, vlines);
228 free(vl->line);
229 free(vl);
233 static inline void
234 global_set_key(const char *key, void (*fn)(struct tab*))
236 if (!kmap_define_key(&global_map, key, fn))
237 _exit(1);
240 static inline void
241 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
243 if (!kmap_define_key(&minibuffer_map, key, fn))
244 _exit(1);
247 static void
248 load_default_keys(void)
250 /* === global map === */
252 /* emacs */
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);
290 /* vi/vi-like */
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);
314 /* tmp */
315 global_set_key("q", cmd_kill_telescope);
317 global_set_key("esc", cmd_clear_minibuf);
319 global_set_key(":", cmd_execute_extended_command);
321 /* cua */
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);
335 /* global */
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-f", cmd_mini_forward_char);
349 minibuffer_set_key("C-b", cmd_mini_backward_char);
350 minibuffer_set_key("<right>", cmd_mini_forward_char);
351 minibuffer_set_key("<left>", cmd_mini_backward_char);
352 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
353 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
354 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
355 minibuffer_set_key("<home>", cmd_mini_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);
364 static void
365 restore_cursor(struct tab *tab)
367 struct vline *vl;
368 const char *prfx;
370 vl =tab->s.current_line;
371 if (vl == NULL || vl->line == NULL)
372 tab->s.curs_x = tab->s.line_x = 0;
373 else
374 tab->s.line_x = MIN(tab->s.line_x, strlen(vl->line));
376 if (vl != NULL) {
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 tab->s.curs_x = tab->s.line_x + strlen(prfx);
381 wmove(body, tab->s.curs_y, tab->s.curs_x);
384 static void
385 cmd_previous_line(struct tab *tab)
387 struct vline *vl;
389 if (tab->s.current_line == NULL
390 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
391 return;
393 if (--tab->s.curs_y < 0) {
394 tab->s.curs_y = 0;
395 cmd_scroll_line_up(tab);
396 return;
399 tab->s.current_line = vl;
400 restore_cursor(tab);
403 static void
404 cmd_next_line(struct tab *tab)
406 struct vline *vl;
408 if (tab->s.current_line == NULL
409 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
410 return;
412 if (++tab->s.curs_y > body_lines-1) {
413 tab->s.curs_y = body_lines-1;
414 cmd_scroll_line_down(tab);
415 return;
418 tab->s.current_line = vl;
419 restore_cursor(tab);
422 static void
423 cmd_backward_char(struct tab *tab)
425 if (tab->s.line_x != 0)
426 tab->s.line_x--;
427 restore_cursor(tab);
430 static void
431 cmd_forward_char(struct tab *tab)
433 tab->s.line_x++;
434 restore_cursor(tab);
437 static void
438 cmd_backward_paragraph(struct tab *tab)
440 do {
441 if (tab->s.current_line == NULL ||
442 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
443 message("No previous paragraph");
444 return;
446 cmd_previous_line(tab);
447 } while (tab->s.current_line->line != NULL ||
448 tab->s.current_line->parent->type != LINE_TEXT);
451 static void
452 cmd_forward_paragraph(struct tab *tab)
454 do {
455 if (tab->s.current_line == NULL ||
456 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
457 message("No next paragraph");
458 return;
460 cmd_next_line(tab);
461 } while (tab->s.current_line->line != NULL ||
462 tab->s.current_line->parent->type != LINE_TEXT);
465 static void
466 cmd_move_beginning_of_line(struct tab *tab)
468 tab->s.line_x = 0;
469 restore_cursor(tab);
472 static void
473 cmd_move_end_of_line(struct tab *tab)
475 struct vline *vl;
477 vl = tab->s.current_line;
478 if (vl->line == NULL)
479 return;
480 tab->s.line_x = body_cols;
481 restore_cursor(tab);
484 static void
485 cmd_redraw(struct tab *tab)
487 handle_resize(0, 0, NULL);
490 static void
491 cmd_scroll_line_up(struct tab *tab)
493 struct vline *vl;
495 if (tab->s.line_off == 0)
496 return;
498 vl = nth_line(tab, --tab->s.line_off);
499 wscrl(body, -1);
500 wmove(body, 0, 0);
501 print_vline(vl);
503 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
506 static void
507 cmd_scroll_line_down(struct tab *tab)
509 struct vline *vl;
511 vl = tab->s.current_line;
512 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
513 return;
514 tab->s.current_line = vl;
516 tab->s.line_off++;
517 wscrl(body, 1);
519 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
520 return;
522 vl = nth_line(tab, tab->s.line_off + body_lines-1);
523 wmove(body, body_lines-1, 0);
524 print_vline(vl);
527 static void
528 cmd_scroll_up(struct tab *tab)
530 size_t off;
532 off = body_lines+1;
534 for (; off > 0; --off)
535 cmd_scroll_line_up(tab);
538 static void
539 cmd_scroll_down(struct tab *tab)
541 size_t off;
543 off = body_lines+1;
545 for (; off > 0; --off)
546 cmd_scroll_line_down(tab);
549 static void
550 cmd_beginning_of_buffer(struct tab *tab)
552 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
553 tab->s.line_off = 0;
554 tab->s.curs_y = 0;
555 tab->s.line_x = 0;
556 restore_cursor(tab);
557 redraw_body(tab);
560 static void
561 cmd_end_of_buffer(struct tab *tab)
563 ssize_t off;
565 off = tab->s.line_max - body_lines;
566 off = MAX(0, off);
568 tab->s.line_off = off;
569 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
571 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
572 tab->s.line_x = body_cols;
573 restore_cursor(tab);
574 redraw_body(tab);
577 static void
578 cmd_kill_telescope(struct tab *tab)
580 event_loopbreak();
583 static void
584 cmd_push_button(struct tab *tab)
586 struct vline *vl;
587 size_t nth;
589 nth = tab->s.line_off + tab->s.curs_y;
590 if (nth >= tab->s.line_max)
591 return;
592 vl = nth_line(tab, nth);
593 if (vl->parent->type != LINE_LINK)
594 return;
596 load_url_in_tab(tab, vl->parent->alt);
599 static void
600 cmd_push_button_new_tab(struct tab *tab)
602 struct vline *vl;
603 size_t nth;
605 nth = tab->s.line_off + tab->s.curs_y;
606 if (nth > tab->s.line_max)
607 return;
608 vl = nth_line(tab, nth);
609 if (vl->parent->type != LINE_LINK)
610 return;
612 new_tab(vl->parent->alt);
615 static void
616 cmd_previous_button(struct tab *tab)
618 do {
619 if (tab->s.current_line == NULL ||
620 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
621 message("No previous link");
622 return;
624 cmd_previous_line(tab);
625 } while (tab->s.current_line->parent->type != LINE_LINK);
628 static void
629 cmd_next_button(struct tab *tab)
631 do {
632 if (tab->s.current_line == NULL ||
633 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
634 message("No next link");
635 return;
637 cmd_next_line(tab);
638 } while (tab->s.current_line->parent->type != LINE_LINK);
641 static void
642 cmd_previous_page(struct tab *tab)
644 if (!load_previous_page(tab))
645 message("No previous page");
646 else
647 start_loading_anim(tab);
650 static void
651 cmd_next_page(struct tab *tab)
653 if (!load_next_page(tab))
654 message("No next page");
655 else
656 start_loading_anim(tab);
659 static void
660 cmd_clear_minibuf(struct tab *tab)
662 handle_clear_minibuf(0, 0, NULL);
665 static void
666 cmd_execute_extended_command(struct tab *tab)
668 size_t len;
670 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
671 &eecmd_history);
673 len = sizeof(ministate.prompt);
674 strlcpy(ministate.prompt, "", len);
676 if (thiskey.meta)
677 strlcat(ministate.prompt, "M-", len);
679 strlcat(ministate.prompt, keyname(thiskey.key), len);
680 strlcat(ministate.prompt, " ", len);
683 static void
684 cmd_tab_close(struct tab *tab)
686 struct tab *t;
688 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
689 TAILQ_NEXT(tab, tabs) == NULL) {
690 message("Can't close the only tab.");
691 return;
694 if (evtimer_pending(&tab->s.loadingev, NULL))
695 evtimer_del(&tab->s.loadingev);
697 stop_tab(tab);
699 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
700 t = TAILQ_NEXT(tab, tabs);
701 TAILQ_REMOVE(&tabshead, tab, tabs);
702 free(tab);
704 switch_to_tab(t);
707 static void
708 cmd_tab_close_other(struct tab *tab)
710 struct tab *t, *i;
712 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
713 if (t->flags & TAB_CURRENT)
714 continue;
716 stop_tab(t);
717 TAILQ_REMOVE(&tabshead, t, tabs);
718 free(t);
722 static void
723 cmd_tab_new(struct tab *tab)
725 new_tab(NEW_TAB_URL);
728 static void
729 cmd_tab_next(struct tab *tab)
731 struct tab *t;
733 tab->flags &= ~TAB_CURRENT;
735 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
736 t = TAILQ_FIRST(&tabshead);
737 t->flags |= TAB_CURRENT;
740 static void
741 cmd_tab_previous(struct tab *tab)
743 struct tab *t;
745 tab->flags &= ~TAB_CURRENT;
747 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
748 t = TAILQ_LAST(&tabshead, tabshead);
749 t->flags |= TAB_CURRENT;
752 static void
753 cmd_load_url(struct tab *tab)
755 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
756 &lu_history);
757 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
760 static void
761 cmd_load_current_url(struct tab *tab)
763 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
764 &lu_history);
765 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
766 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
767 ministate.off = strlen(tab->hist_cur->h);
768 ministate.len = ministate.off;
771 static void
772 cmd_bookmark_page(struct tab *tab)
774 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
775 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
776 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
777 ministate.off = strlen(tab->hist_cur->h);
778 ministate.len = ministate.off;
781 static void
782 cmd_goto_bookmarks(struct tab *tab)
784 load_url_in_tab(tab, "about:bookmarks");
787 static void
788 global_key_unbound(void)
790 message("%s is undefined", keybuf);
793 static void
794 cmd_mini_delete_char(struct tab *tab)
796 minibuffer_taint_hist();
798 if (ministate.len == 0 || ministate.off == ministate.len)
799 return;
801 memmove(&ministate.buf[ministate.off],
802 &ministate.buf[ministate.off+1],
803 ministate.len - ministate.off + 1);
804 ministate.len--;
807 static void
808 cmd_mini_delete_backward_char(struct tab *tab)
810 minibuffer_taint_hist();
812 if (ministate.len == 0 || ministate.off == 0)
813 return;
815 memmove(&ministate.buf[ministate.off-1],
816 &ministate.buf[ministate.off],
817 ministate.len - ministate.off + 1);
818 ministate.off--;
819 ministate.len--;
822 static void
823 cmd_mini_forward_char(struct tab *tab)
825 if (ministate.off == ministate.len)
826 return;
827 ministate.off++;
830 static void
831 cmd_mini_backward_char(struct tab *tab)
833 if (ministate.off == 0)
834 return;
835 ministate.off--;
838 static void
839 cmd_mini_move_end_of_line(struct tab *tab)
841 ministate.off = ministate.len;
844 static void
845 cmd_mini_move_beginning_of_line(struct tab *tab)
847 ministate.off = 0;
850 static void
851 cmd_mini_kill_line(struct tab *tab)
853 minibuffer_taint_hist();
855 if (ministate.off == ministate.len)
856 return;
857 ministate.buf[ministate.off] = '\0';
858 ministate.len -= ministate.off;
861 static void
862 cmd_mini_abort(struct tab *tab)
864 ministate.abortfn();
867 static void
868 cmd_mini_complete_and_exit(struct tab *tab)
870 minibuffer_taint_hist();
871 ministate.donefn();
874 static void
875 cmd_mini_previous_history_element(struct tab *tab)
877 if (ministate.history == NULL) {
878 message("No history");
879 return;
882 if (ministate.hist_cur == NULL ||
883 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
884 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
885 ministate.hist_off = ministate.history->len - 1;
886 if (ministate.hist_cur == NULL)
887 message("No prev item");
888 } else {
889 ministate.hist_off--;
892 if (ministate.hist_cur != NULL) {
893 ministate.off = 0;
894 ministate.len = strlen(ministate.hist_cur->h);
898 static void
899 cmd_mini_next_history_element(struct tab *tab)
901 if (ministate.history == NULL) {
902 message("No history");
903 return;
906 if (ministate.hist_cur == NULL ||
907 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
908 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
909 ministate.hist_off = 0;
910 if (ministate.hist_cur == NULL)
911 message("No next item");
912 } else {
913 ministate.hist_off++;
916 if (ministate.hist_cur != NULL) {
917 ministate.off = 0;
918 ministate.len = strlen(ministate.hist_cur->h);
922 static void
923 minibuffer_hist_save_entry(void)
925 struct hist *hist;
927 if (ministate.history == NULL)
928 return;
930 if ((hist = calloc(1, sizeof(*hist))) == NULL)
931 abort();
933 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
935 if (TAILQ_EMPTY(&ministate.history->head))
936 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
937 else
938 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
939 ministate.history->len++;
942 /*
943 * taint the minibuffer cache: if we're currently showing a history
944 * element, copy that to the current buf and reset the "history
945 * navigation" thing.
946 */
947 static void
948 minibuffer_taint_hist(void)
950 if (ministate.hist_cur == NULL)
951 return;
953 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
954 ministate.hist_cur = NULL;
957 static void
958 minibuffer_self_insert(void)
960 minibuffer_taint_hist();
962 if (ministate.len == sizeof(ministate.buf) -1)
963 return;
965 /* TODO: utf8 handling! */
967 memmove(&ministate.buf[ministate.off+1],
968 &ministate.buf[ministate.off],
969 ministate.len - ministate.off + 1);
970 ministate.buf[ministate.off] = thiskey.key;
971 ministate.off++;
972 ministate.len++;
975 static void
976 eecmd_self_insert(void)
978 if (thiskey.meta || isspace(thiskey.key) ||
979 !isgraph(thiskey.key)) {
980 global_key_unbound();
981 return;
984 minibuffer_self_insert();
987 static void
988 eecmd_select(void)
990 exit_minibuffer();
991 minibuffer_hist_save_entry();
992 message("TODO: try to execute %s", ministate.buf);
995 static void
996 ir_self_insert(void)
998 minibuffer_self_insert();
1001 static void
1002 ir_select(void)
1004 char buf[1025] = {0};
1005 struct url url;
1006 struct tab *tab;
1008 tab = current_tab();
1010 exit_minibuffer();
1011 minibuffer_hist_save_entry();
1013 /* a bit ugly but... */
1014 memcpy(&url, &tab->url, sizeof(tab->url));
1015 url_set_query(&url, ministate.buf);
1016 url_unparse(&url, buf, sizeof(buf));
1017 load_url_in_tab(tab, buf);
1020 static void
1021 lu_self_insert(void)
1023 if (thiskey.meta || isspace(thiskey.key) ||
1024 !isgraph(thiskey.key)) {
1025 global_key_unbound();
1026 return;
1029 minibuffer_self_insert();
1032 static void
1033 lu_select(void)
1035 exit_minibuffer();
1036 minibuffer_hist_save_entry();
1037 load_url_in_tab(current_tab(), ministate.buf);
1040 static void
1041 bp_select(void)
1043 exit_minibuffer();
1044 if (*ministate.buf != '\0')
1045 add_to_bookmarks(ministate.buf);
1046 else
1047 message("Abort.");
1050 static struct vline *
1051 nth_line(struct tab *tab, size_t n)
1053 struct vline *vl;
1054 size_t i;
1056 i = 0;
1057 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1058 if (i == n)
1059 return vl;
1060 i++;
1063 /* unreachable */
1064 abort();
1067 static struct tab *
1068 current_tab(void)
1070 struct tab *t;
1072 TAILQ_FOREACH(t, &tabshead, tabs) {
1073 if (t->flags & TAB_CURRENT)
1074 return t;
1077 /* unreachable */
1078 abort();
1081 static void
1082 dispatch_stdio(int fd, short ev, void *d)
1084 struct keymap *k;
1085 const char *keyname;
1086 char tmp[2] = {0};
1088 thiskey.key = wgetch(body);
1089 if (thiskey.key == ERR)
1090 return;
1091 if (thiskey.key == 27) {
1092 /* TODO: make escape-time customizable */
1094 thiskey.meta = 1;
1095 thiskey.key = wgetch(body);
1096 if (thiskey.key == ERR || thiskey.key == 27) {
1097 thiskey.meta = 0;
1098 thiskey.key = 27;
1100 } else
1101 thiskey.meta = 0;
1103 if (keybuf[0] != '\0')
1104 strlcat(keybuf, " ", sizeof(keybuf));
1105 if (thiskey.meta)
1106 strlcat(keybuf, "M-", sizeof(keybuf));
1107 if ((keyname = unkbd(thiskey.key)) != NULL)
1108 strlcat(keybuf, keyname, sizeof(keybuf));
1109 else {
1110 tmp[0] = thiskey.key;
1111 strlcat(keybuf, tmp, sizeof(keybuf));
1114 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1115 if (k->meta == thiskey.meta &&
1116 k->key == thiskey.key) {
1117 if (k->fn == NULL)
1118 current_map = &k->map;
1119 else {
1120 current_map = base_map;
1121 strlcpy(keybuf, "", sizeof(keybuf));
1122 k->fn(current_tab());
1124 goto done;
1128 if (current_map->unhandled_input != NULL)
1129 current_map->unhandled_input();
1130 else {
1131 global_key_unbound();
1134 strlcpy(keybuf, "", sizeof(keybuf));
1135 current_map = base_map;
1137 done:
1138 redraw_tab(current_tab());
1141 static void
1142 handle_clear_minibuf(int fd, short ev, void *d)
1144 free(ministate.curmesg);
1145 ministate.curmesg = NULL;
1147 redraw_minibuffer();
1148 if (in_minibuffer) {
1149 wrefresh(body);
1150 wrefresh(minibuf);
1151 } else {
1152 wrefresh(minibuf);
1153 wrefresh(body);
1157 static void
1158 handle_resize(int sig, short ev, void *d)
1160 struct tab *tab;
1162 endwin();
1163 refresh();
1164 clear();
1166 /* move and resize the windows, in reverse order! */
1168 mvwin(minibuf, LINES-1, 0);
1169 wresize(minibuf, 1, COLS);
1171 mvwin(modeline, LINES-2, 0);
1172 wresize(modeline, 1, COLS);
1174 wresize(body, LINES-3, COLS);
1175 body_lines = LINES-3;
1176 body_cols = COLS;
1178 wresize(tabline, 1, COLS);
1180 tab = current_tab();
1182 wrap_page(tab);
1183 redraw_tab(tab);
1186 static int
1187 wrap_page(struct tab *tab)
1189 struct line *l;
1190 const struct line *orig;
1191 struct vline *vl;
1192 const char *prfx;
1194 orig = tab->s.current_line == NULL
1195 ? NULL
1196 : tab->s.current_line->parent;
1197 tab->s.current_line = NULL;
1199 tab->s.curs_y = 0;
1200 tab->s.line_off = 0;
1202 empty_vlist(tab);
1204 TAILQ_FOREACH(l, &tab->page.head, lines) {
1205 prfx = line_prefixes[l->type].prfx1;
1206 switch (l->type) {
1207 case LINE_TEXT:
1208 case LINE_LINK:
1209 case LINE_TITLE_1:
1210 case LINE_TITLE_2:
1211 case LINE_TITLE_3:
1212 case LINE_ITEM:
1213 case LINE_QUOTE:
1214 case LINE_PRE_START:
1215 case LINE_PRE_END:
1216 wrap_text(tab, prfx, l, body_cols);
1217 break;
1218 case LINE_PRE_CONTENT:
1219 hardwrap_text(tab, l, body_cols);
1220 break;
1223 if (orig == l && tab->s.current_line == NULL) {
1224 tab->s.line_off = tab->s.line_max-1;
1225 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1227 while (1) {
1228 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1229 if (vl == NULL || vl->parent != orig)
1230 break;
1231 tab->s.current_line = vl;
1232 tab->s.line_off--;
1237 if (tab->s.current_line == NULL)
1238 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1240 return 1;
1243 static void
1244 print_vline(struct vline *vl)
1246 const char *text = vl->line;
1247 const char *prfx;
1248 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1249 int text_face = line_faces[vl->parent->type].text_prop;
1251 if (!vl->flags)
1252 prfx = line_prefixes[vl->parent->type].prfx1;
1253 else
1254 prfx = line_prefixes[vl->parent->type].prfx2;
1256 if (text == NULL)
1257 text = "";
1259 wattron(body, prefix_face);
1260 wprintw(body, "%s", prfx);
1261 wattroff(body, prefix_face);
1263 wattron(body, text_face);
1264 wprintw(body, "%s", text);
1265 wattroff(body, text_face);
1268 static void
1269 redraw_tabline(void)
1271 struct tab *tab;
1272 size_t toskip;
1273 int current, x, y, truncated;
1274 const char *title;
1275 char buf[25];
1277 toskip = 0;
1278 x = 1;
1279 TAILQ_FOREACH(tab, &tabshead, tabs) {
1280 x += sizeof(buf) + 1;
1281 toskip++;
1282 if (tab->flags & TAB_CURRENT)
1283 break;
1285 if (x < COLS-2)
1286 toskip = 0;
1287 else
1288 toskip--;
1290 werase(tabline);
1291 wattron(tabline, tab_face.background);
1292 wprintw(tabline, toskip == 0 ? " " : "<");
1293 wattroff(tabline, tab_face.background);
1295 truncated = 0;
1296 TAILQ_FOREACH(tab, &tabshead, tabs) {
1297 if (truncated)
1298 break;
1299 if (toskip != 0) {
1300 toskip--;
1301 continue;
1304 getyx(tabline, y, x);
1305 if (x + sizeof(buf)+2 >= (size_t)COLS)
1306 truncated = 1;
1308 current = tab->flags & TAB_CURRENT;
1310 if (*(title = tab->page.title) == '\0')
1311 title = tab->hist_cur->h;
1313 strlcpy(buf, " ", sizeof(buf));
1314 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1315 /* truncation happens */
1316 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1317 } else {
1318 /* pad with spaces */
1319 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1320 /* nop */ ;
1323 if (current)
1324 wattron(tabline, tab_face.current_tab);
1325 else
1326 wattron(tabline, tab_face.tab);
1328 wprintw(tabline, "%s", buf);
1329 if (TAILQ_NEXT(tab, tabs) != NULL)
1330 wprintw(tabline, " ");
1332 if (current)
1333 wattroff(tabline, tab_face.current_tab);
1334 else
1335 wattroff(tabline, tab_face.tab);
1338 wattron(tabline, tab_face.background);
1339 for (; x < COLS; ++x)
1340 waddch(tabline, ' ');
1341 if (truncated)
1342 mvwprintw(tabline, 0, COLS-1, ">");
1345 static inline char
1346 trust_status_char(enum trust_state ts)
1348 switch (ts) {
1349 case TS_UNKNOWN: return 'u';
1350 case TS_UNTRUSTED: return '!';
1351 case TS_TRUSTED: return 'v';
1352 case TS_VERIFIED: return 'V';
1356 static void
1357 redraw_modeline(struct tab *tab)
1359 double pct;
1360 int x, y, max_x, max_y;
1361 const char *mode = tab->page.name;
1362 const char *spin = "-\\|/";
1364 werase(modeline);
1365 wattron(modeline, A_REVERSE);
1366 wmove(modeline, 0, 0);
1368 wprintw(modeline, "-%c%c %s ",
1369 spin[tab->s.loading_anim_step],
1370 trust_status_char(tab->trust),
1371 mode == NULL ? "(none)" : mode);
1373 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1375 if (tab->s.line_max <= (size_t)body_lines)
1376 wprintw(modeline, "All ");
1377 else if (tab->s.line_off == 0)
1378 wprintw(modeline, "Top ");
1379 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1380 wprintw(modeline, "Bottom ");
1381 else
1382 wprintw(modeline, "%.0f%% ", pct);
1384 wprintw(modeline, "%d/%d %s ",
1385 tab->s.line_off + tab->s.curs_y,
1386 tab->s.line_max,
1387 tab->hist_cur->h);
1389 getyx(modeline, y, x);
1390 getmaxyx(modeline, max_y, max_x);
1392 (void)y;
1393 (void)max_y;
1395 for (; x < max_x; ++x)
1396 waddstr(modeline, "-");
1399 static void
1400 redraw_minibuffer(void)
1402 size_t skip = 0, off_x = 0, off_y = 0;
1403 struct tab *tab;
1405 werase(minibuf);
1406 if (in_minibuffer) {
1407 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1408 if (ministate.hist_cur != NULL)
1409 wprintw(minibuf, "(%zu/%zu) ",
1410 ministate.hist_off + 1,
1411 ministate.history->len);
1413 getyx(minibuf, off_y, off_x);
1415 while (ministate.off - skip > (size_t)COLS / 2) {
1416 skip += MIN(ministate.off/4, 1);
1419 if (ministate.hist_cur != NULL)
1420 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1421 else
1422 wprintw(minibuf, "%s", ministate.buf + skip);
1425 if (ministate.curmesg != NULL) {
1426 if (in_minibuffer)
1427 wprintw(minibuf, " [%s]", ministate.curmesg);
1428 else
1429 wprintw(minibuf, "%s", ministate.curmesg);
1432 if (!in_minibuffer && ministate.curmesg == NULL)
1433 wprintw(minibuf, "%s", keybuf);
1435 /* If nothing else, show the URL at point */
1436 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1437 tab = current_tab();
1438 if (tab->s.current_line != NULL &&
1439 tab->s.current_line->parent->type == LINE_LINK)
1440 wprintw(minibuf, "%s",
1441 tab->s.current_line->parent->alt);
1444 if (in_minibuffer)
1445 wmove(minibuf, 0, off_x + ministate.off - skip);
1448 static void
1449 redraw_tab(struct tab *tab)
1451 redraw_tabline();
1452 redraw_body(tab);
1453 redraw_modeline(tab);
1454 redraw_minibuffer();
1456 restore_cursor(tab);
1457 wrefresh(tabline);
1458 wrefresh(modeline);
1460 if (in_minibuffer) {
1461 wrefresh(body);
1462 wrefresh(minibuf);
1463 } else {
1464 wrefresh(minibuf);
1465 wrefresh(body);
1469 static void
1470 redraw_body(struct tab *tab)
1472 struct vline *vl;
1473 int line;
1475 werase(body);
1477 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1478 if (TAILQ_EMPTY(&tab->s.head))
1479 return;
1481 line = 0;
1482 vl = nth_line(tab, tab->s.line_off);
1483 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1484 wmove(body, line, 0);
1485 print_vline(vl);
1486 line++;
1487 if (line == body_lines)
1488 break;
1492 static void
1493 vmessage(const char *fmt, va_list ap)
1495 if (evtimer_pending(&clminibufev, NULL))
1496 evtimer_del(&clminibufev);
1497 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1498 evtimer_add(&clminibufev, &clminibufev_timer);
1500 free(ministate.curmesg);
1502 /* TODO: what to do if the allocation fails here? */
1503 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1504 ministate.curmesg = NULL;
1506 redraw_minibuffer();
1507 if (in_minibuffer) {
1508 wrefresh(body);
1509 wrefresh(minibuf);
1510 } else {
1511 wrefresh(minibuf);
1512 wrefresh(body);
1516 static void
1517 message(const char *fmt, ...)
1519 va_list ap;
1521 va_start(ap, fmt);
1522 vmessage(fmt, ap);
1523 va_end(ap);
1526 static void
1527 start_loading_anim(struct tab *tab)
1529 if (tab->s.loading_anim)
1530 return;
1531 tab->s.loading_anim = 1;
1532 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1533 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1536 static void
1537 update_loading_anim(int fd, short ev, void *d)
1539 struct tab *tab = d;
1541 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1543 if (tab->flags & TAB_CURRENT) {
1544 redraw_modeline(tab);
1545 wrefresh(modeline);
1546 wrefresh(body);
1547 if (in_minibuffer)
1548 wrefresh(minibuf);
1551 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1554 static void
1555 stop_loading_anim(struct tab *tab)
1557 if (!tab->s.loading_anim)
1558 return;
1559 evtimer_del(&tab->s.loadingev);
1560 tab->s.loading_anim = 0;
1561 tab->s.loading_anim_step = 0;
1563 if (!(tab->flags & TAB_CURRENT))
1564 return;
1566 redraw_modeline(tab);
1568 wrefresh(modeline);
1569 wrefresh(body);
1570 if (in_minibuffer)
1571 wrefresh(minibuf);
1574 static void
1575 load_url_in_tab(struct tab *tab, const char *url)
1577 empty_vlist(tab);
1578 message("Loading %s...", url);
1579 start_loading_anim(tab);
1580 load_url(tab, url);
1582 tab->s.curs_x = 0;
1583 tab->s.curs_y = 0;
1584 redraw_tab(tab);
1587 static void
1588 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1589 void (*abortfn)(void), struct histhead *hist)
1591 in_minibuffer = 1;
1592 base_map = &minibuffer_map;
1593 current_map = &minibuffer_map;
1595 base_map->unhandled_input = self_insert_fn;
1597 ministate.donefn = donefn;
1598 ministate.abortfn = abortfn;
1599 memset(ministate.buf, 0, sizeof(ministate.buf));
1600 ministate.off = 0;
1601 ministate.len = 0;
1602 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1604 ministate.history = hist;
1605 ministate.hist_cur = NULL;
1606 ministate.hist_off = 0;
1609 static void
1610 exit_minibuffer(void)
1612 werase(minibuf);
1614 in_minibuffer = 0;
1615 base_map = &global_map;
1616 current_map = &global_map;
1619 static void
1620 switch_to_tab(struct tab *tab)
1622 struct tab *t;
1624 TAILQ_FOREACH(t, &tabshead, tabs) {
1625 t->flags &= ~TAB_CURRENT;
1628 tab->flags |= TAB_CURRENT;
1631 static struct tab *
1632 new_tab(const char *url)
1634 struct tab *tab;
1636 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1637 event_loopbreak();
1638 return NULL;
1641 TAILQ_INIT(&tab->hist.head);
1643 TAILQ_INIT(&tab->s.head);
1645 tab->id = tab_counter++;
1646 switch_to_tab(tab);
1648 if (TAILQ_EMPTY(&tabshead))
1649 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1650 else
1651 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1653 load_url_in_tab(tab, url);
1654 return tab;
1657 static void
1658 usage(void)
1660 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1663 int
1664 ui_init(int argc, char * const *argv)
1666 const char *url = NEW_TAB_URL;
1667 int ch;
1669 while ((ch = getopt(argc, argv, "")) != -1) {
1670 switch (ch) {
1671 default:
1672 usage();
1673 return 0;
1676 argc -= optind;
1677 argv += optind;
1679 if (argc != 0)
1680 url = argv[0];
1682 setlocale(LC_ALL, "");
1684 TAILQ_INIT(&global_map.m);
1685 global_map.unhandled_input = global_key_unbound;
1687 TAILQ_INIT(&minibuffer_map.m);
1689 TAILQ_INIT(&eecmd_history.head);
1690 TAILQ_INIT(&ir_history.head);
1691 TAILQ_INIT(&lu_history.head);
1693 base_map = &global_map;
1694 current_map = &global_map;
1695 load_default_keys();
1697 initscr();
1698 raw();
1699 noecho();
1701 nonl();
1702 intrflush(stdscr, FALSE);
1704 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1705 return 0;
1706 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1707 return 0;
1708 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1709 return 0;
1710 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1711 return 0;
1713 body_lines = LINES-3;
1714 body_cols = COLS;
1716 keypad(body, TRUE);
1717 scrollok(body, TRUE);
1719 /* non-blocking input */
1720 wtimeout(body, 0);
1722 mvwprintw(body, 0, 0, "");
1724 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1725 event_add(&stdioev, NULL);
1727 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1728 signal_add(&winchev, NULL);
1730 new_tab(url);
1732 return 1;
1735 void
1736 ui_on_tab_loaded(struct tab *tab)
1738 stop_loading_anim(tab);
1739 message("Loaded %s", tab->hist_cur->h);
1741 redraw_tabline();
1742 wrefresh(tabline);
1743 if (in_minibuffer)
1744 wrefresh(minibuf);
1745 else
1746 wrefresh(body);
1749 void
1750 ui_on_tab_refresh(struct tab *tab)
1752 wrap_page(tab);
1753 if (tab->flags & TAB_CURRENT)
1754 redraw_tab(tab);
1757 void
1758 ui_require_input(struct tab *tab, int hide)
1760 /* TODO: hard-switching to another tab is ugly */
1761 switch_to_tab(tab);
1763 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1764 &ir_history);
1765 strlcpy(ministate.prompt, "Input required: ",
1766 sizeof(ministate.prompt));
1767 redraw_tab(tab);
1770 void
1771 ui_notify(const char *fmt, ...)
1773 va_list ap;
1775 va_start(ap, fmt);
1776 vmessage(fmt, ap);
1777 va_end(ap);
1780 void
1781 ui_end(void)
1783 endwin();