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.curs_x = utf8_snwidth(vl->line, tab->s.line_x);
376 if (vl != NULL) {
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 tab->s.curs_x += utf8_swidth(prfx);
382 static void
383 cmd_previous_line(struct tab *tab)
385 struct vline *vl;
387 if (tab->s.current_line == NULL
388 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
389 return;
391 if (--tab->s.curs_y < 0) {
392 tab->s.curs_y = 0;
393 cmd_scroll_line_up(tab);
394 return;
397 tab->s.current_line = vl;
398 restore_cursor(tab);
401 static void
402 cmd_next_line(struct tab *tab)
404 struct vline *vl;
406 if (tab->s.current_line == NULL
407 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
408 return;
410 if (++tab->s.curs_y > body_lines-1) {
411 tab->s.curs_y = body_lines-1;
412 cmd_scroll_line_down(tab);
413 return;
416 tab->s.current_line = vl;
417 restore_cursor(tab);
420 static void
421 cmd_backward_char(struct tab *tab)
423 if (tab->s.line_x != 0)
424 tab->s.line_x--;
425 restore_cursor(tab);
428 static void
429 cmd_forward_char(struct tab *tab)
431 tab->s.line_x++;
432 restore_cursor(tab);
435 static void
436 cmd_backward_paragraph(struct tab *tab)
438 do {
439 if (tab->s.current_line == NULL ||
440 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
441 message("No previous paragraph");
442 return;
444 cmd_previous_line(tab);
445 } while (tab->s.current_line->line != NULL ||
446 tab->s.current_line->parent->type != LINE_TEXT);
449 static void
450 cmd_forward_paragraph(struct tab *tab)
452 do {
453 if (tab->s.current_line == NULL ||
454 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
455 message("No next paragraph");
456 return;
458 cmd_next_line(tab);
459 } while (tab->s.current_line->line != NULL ||
460 tab->s.current_line->parent->type != LINE_TEXT);
463 static void
464 cmd_move_beginning_of_line(struct tab *tab)
466 tab->s.line_x = 0;
467 restore_cursor(tab);
470 static void
471 cmd_move_end_of_line(struct tab *tab)
473 struct vline *vl;
475 vl = tab->s.current_line;
476 if (vl->line == NULL)
477 return;
478 tab->s.line_x = utf8_cplen(vl->line);
479 restore_cursor(tab);
482 static void
483 cmd_redraw(struct tab *tab)
485 handle_resize(0, 0, NULL);
488 static void
489 cmd_scroll_line_up(struct tab *tab)
491 struct vline *vl;
493 if (tab->s.line_off == 0)
494 return;
496 vl = nth_line(tab, --tab->s.line_off);
497 wscrl(body, -1);
498 wmove(body, 0, 0);
499 print_vline(vl);
501 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
502 restore_cursor(tab);
505 static void
506 cmd_scroll_line_down(struct tab *tab)
508 struct vline *vl;
510 vl = tab->s.current_line;
511 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
512 return;
513 tab->s.current_line = vl;
515 tab->s.line_off++;
516 wscrl(body, 1);
518 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
519 return;
521 vl = nth_line(tab, tab->s.line_off + body_lines-1);
522 wmove(body, body_lines-1, 0);
523 print_vline(vl);
525 restore_cursor(tab);
528 static void
529 cmd_scroll_up(struct tab *tab)
531 size_t off;
533 off = body_lines+1;
535 for (; off > 0; --off)
536 cmd_scroll_line_up(tab);
539 static void
540 cmd_scroll_down(struct tab *tab)
542 size_t off;
544 off = body_lines+1;
546 for (; off > 0; --off)
547 cmd_scroll_line_down(tab);
550 static void
551 cmd_beginning_of_buffer(struct tab *tab)
553 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
554 tab->s.line_off = 0;
555 tab->s.curs_y = 0;
556 tab->s.line_x = 0;
557 restore_cursor(tab);
558 redraw_body(tab);
561 static void
562 cmd_end_of_buffer(struct tab *tab)
564 ssize_t off;
566 off = tab->s.line_max - body_lines;
567 off = MAX(0, off);
569 tab->s.line_off = off;
570 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
572 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
573 tab->s.line_x = body_cols;
574 restore_cursor(tab);
575 redraw_body(tab);
578 static void
579 cmd_kill_telescope(struct tab *tab)
581 event_loopbreak();
584 static void
585 cmd_push_button(struct tab *tab)
587 struct vline *vl;
588 size_t nth;
590 nth = tab->s.line_off + tab->s.curs_y;
591 if (nth >= tab->s.line_max)
592 return;
593 vl = nth_line(tab, nth);
594 if (vl->parent->type != LINE_LINK)
595 return;
597 load_url_in_tab(tab, vl->parent->alt);
600 static void
601 cmd_push_button_new_tab(struct tab *tab)
603 struct vline *vl;
604 size_t nth;
606 nth = tab->s.line_off + tab->s.curs_y;
607 if (nth > tab->s.line_max)
608 return;
609 vl = nth_line(tab, nth);
610 if (vl->parent->type != LINE_LINK)
611 return;
613 new_tab(vl->parent->alt);
616 static void
617 cmd_previous_button(struct tab *tab)
619 do {
620 if (tab->s.current_line == NULL ||
621 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
622 message("No previous link");
623 return;
625 cmd_previous_line(tab);
626 } while (tab->s.current_line->parent->type != LINE_LINK);
629 static void
630 cmd_next_button(struct tab *tab)
632 do {
633 if (tab->s.current_line == NULL ||
634 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
635 message("No next link");
636 return;
638 cmd_next_line(tab);
639 } while (tab->s.current_line->parent->type != LINE_LINK);
642 static void
643 cmd_previous_page(struct tab *tab)
645 if (!load_previous_page(tab))
646 message("No previous page");
647 else
648 start_loading_anim(tab);
651 static void
652 cmd_next_page(struct tab *tab)
654 if (!load_next_page(tab))
655 message("No next page");
656 else
657 start_loading_anim(tab);
660 static void
661 cmd_clear_minibuf(struct tab *tab)
663 handle_clear_minibuf(0, 0, NULL);
666 static void
667 cmd_execute_extended_command(struct tab *tab)
669 size_t len;
671 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
672 &eecmd_history);
674 len = sizeof(ministate.prompt);
675 strlcpy(ministate.prompt, "", len);
677 if (thiskey.meta)
678 strlcat(ministate.prompt, "M-", len);
680 strlcat(ministate.prompt, keyname(thiskey.key), len);
681 strlcat(ministate.prompt, " ", len);
684 static void
685 cmd_tab_close(struct tab *tab)
687 struct tab *t;
689 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
690 TAILQ_NEXT(tab, tabs) == NULL) {
691 message("Can't close the only tab.");
692 return;
695 if (evtimer_pending(&tab->s.loadingev, NULL))
696 evtimer_del(&tab->s.loadingev);
698 stop_tab(tab);
700 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
701 t = TAILQ_NEXT(tab, tabs);
702 TAILQ_REMOVE(&tabshead, tab, tabs);
703 free(tab);
705 switch_to_tab(t);
708 static void
709 cmd_tab_close_other(struct tab *tab)
711 struct tab *t, *i;
713 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
714 if (t->flags & TAB_CURRENT)
715 continue;
717 stop_tab(t);
718 TAILQ_REMOVE(&tabshead, t, tabs);
719 free(t);
723 static void
724 cmd_tab_new(struct tab *tab)
726 new_tab(NEW_TAB_URL);
729 static void
730 cmd_tab_next(struct tab *tab)
732 struct tab *t;
734 tab->flags &= ~TAB_CURRENT;
736 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
737 t = TAILQ_FIRST(&tabshead);
738 t->flags |= TAB_CURRENT;
741 static void
742 cmd_tab_previous(struct tab *tab)
744 struct tab *t;
746 tab->flags &= ~TAB_CURRENT;
748 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
749 t = TAILQ_LAST(&tabshead, tabshead);
750 t->flags |= TAB_CURRENT;
753 static void
754 cmd_load_url(struct tab *tab)
756 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
757 &lu_history);
758 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
761 static void
762 cmd_load_current_url(struct tab *tab)
764 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
765 &lu_history);
766 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
767 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
768 ministate.off = strlen(tab->hist_cur->h);
769 ministate.len = ministate.off;
772 static void
773 cmd_bookmark_page(struct tab *tab)
775 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
776 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
777 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
778 ministate.off = strlen(tab->hist_cur->h);
779 ministate.len = ministate.off;
782 static void
783 cmd_goto_bookmarks(struct tab *tab)
785 load_url_in_tab(tab, "about:bookmarks");
788 static void
789 global_key_unbound(void)
791 message("%s is undefined", keybuf);
794 static void
795 cmd_mini_delete_char(struct tab *tab)
797 minibuffer_taint_hist();
799 if (ministate.len == 0 || ministate.off == ministate.len)
800 return;
802 memmove(&ministate.buf[ministate.off],
803 &ministate.buf[ministate.off+1],
804 ministate.len - ministate.off + 1);
805 ministate.len--;
808 static void
809 cmd_mini_delete_backward_char(struct tab *tab)
811 minibuffer_taint_hist();
813 if (ministate.len == 0 || ministate.off == 0)
814 return;
816 memmove(&ministate.buf[ministate.off-1],
817 &ministate.buf[ministate.off],
818 ministate.len - ministate.off + 1);
819 ministate.off--;
820 ministate.len--;
823 static void
824 cmd_mini_forward_char(struct tab *tab)
826 if (ministate.off == ministate.len)
827 return;
828 ministate.off++;
831 static void
832 cmd_mini_backward_char(struct tab *tab)
834 if (ministate.off == 0)
835 return;
836 ministate.off--;
839 static void
840 cmd_mini_move_end_of_line(struct tab *tab)
842 ministate.off = ministate.len;
845 static void
846 cmd_mini_move_beginning_of_line(struct tab *tab)
848 ministate.off = 0;
851 static void
852 cmd_mini_kill_line(struct tab *tab)
854 minibuffer_taint_hist();
856 if (ministate.off == ministate.len)
857 return;
858 ministate.buf[ministate.off] = '\0';
859 ministate.len -= ministate.off;
862 static void
863 cmd_mini_abort(struct tab *tab)
865 ministate.abortfn();
868 static void
869 cmd_mini_complete_and_exit(struct tab *tab)
871 minibuffer_taint_hist();
872 ministate.donefn();
875 static void
876 cmd_mini_previous_history_element(struct tab *tab)
878 if (ministate.history == NULL) {
879 message("No history");
880 return;
883 if (ministate.hist_cur == NULL ||
884 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
885 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
886 ministate.hist_off = ministate.history->len - 1;
887 if (ministate.hist_cur == NULL)
888 message("No prev item");
889 } else {
890 ministate.hist_off--;
893 if (ministate.hist_cur != NULL) {
894 ministate.off = 0;
895 ministate.len = strlen(ministate.hist_cur->h);
899 static void
900 cmd_mini_next_history_element(struct tab *tab)
902 if (ministate.history == NULL) {
903 message("No history");
904 return;
907 if (ministate.hist_cur == NULL ||
908 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
909 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
910 ministate.hist_off = 0;
911 if (ministate.hist_cur == NULL)
912 message("No next item");
913 } else {
914 ministate.hist_off++;
917 if (ministate.hist_cur != NULL) {
918 ministate.off = 0;
919 ministate.len = strlen(ministate.hist_cur->h);
923 static void
924 minibuffer_hist_save_entry(void)
926 struct hist *hist;
928 if (ministate.history == NULL)
929 return;
931 if ((hist = calloc(1, sizeof(*hist))) == NULL)
932 abort();
934 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
936 if (TAILQ_EMPTY(&ministate.history->head))
937 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
938 else
939 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
940 ministate.history->len++;
943 /*
944 * taint the minibuffer cache: if we're currently showing a history
945 * element, copy that to the current buf and reset the "history
946 * navigation" thing.
947 */
948 static void
949 minibuffer_taint_hist(void)
951 if (ministate.hist_cur == NULL)
952 return;
954 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
955 ministate.hist_cur = NULL;
958 static void
959 minibuffer_self_insert(void)
961 minibuffer_taint_hist();
963 if (ministate.len == sizeof(ministate.buf) -1)
964 return;
966 /* TODO: utf8 handling! */
968 memmove(&ministate.buf[ministate.off+1],
969 &ministate.buf[ministate.off],
970 ministate.len - ministate.off + 1);
971 ministate.buf[ministate.off] = thiskey.key;
972 ministate.off++;
973 ministate.len++;
976 static void
977 eecmd_self_insert(void)
979 if (thiskey.meta || isspace(thiskey.key) ||
980 !isgraph(thiskey.key)) {
981 global_key_unbound();
982 return;
985 minibuffer_self_insert();
988 static void
989 eecmd_select(void)
991 exit_minibuffer();
992 minibuffer_hist_save_entry();
993 message("TODO: try to execute %s", ministate.buf);
996 static void
997 ir_self_insert(void)
999 minibuffer_self_insert();
1002 static void
1003 ir_select(void)
1005 char buf[1025] = {0};
1006 struct url url;
1007 struct tab *tab;
1009 tab = current_tab();
1011 exit_minibuffer();
1012 minibuffer_hist_save_entry();
1014 /* a bit ugly but... */
1015 memcpy(&url, &tab->url, sizeof(tab->url));
1016 url_set_query(&url, ministate.buf);
1017 url_unparse(&url, buf, sizeof(buf));
1018 load_url_in_tab(tab, buf);
1021 static void
1022 lu_self_insert(void)
1024 if (thiskey.meta || isspace(thiskey.key) ||
1025 !isgraph(thiskey.key)) {
1026 global_key_unbound();
1027 return;
1030 minibuffer_self_insert();
1033 static void
1034 lu_select(void)
1036 exit_minibuffer();
1037 minibuffer_hist_save_entry();
1038 load_url_in_tab(current_tab(), ministate.buf);
1041 static void
1042 bp_select(void)
1044 exit_minibuffer();
1045 if (*ministate.buf != '\0')
1046 add_to_bookmarks(ministate.buf);
1047 else
1048 message("Abort.");
1051 static struct vline *
1052 nth_line(struct tab *tab, size_t n)
1054 struct vline *vl;
1055 size_t i;
1057 i = 0;
1058 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1059 if (i == n)
1060 return vl;
1061 i++;
1064 /* unreachable */
1065 abort();
1068 static struct tab *
1069 current_tab(void)
1071 struct tab *t;
1073 TAILQ_FOREACH(t, &tabshead, tabs) {
1074 if (t->flags & TAB_CURRENT)
1075 return t;
1078 /* unreachable */
1079 abort();
1082 static void
1083 dispatch_stdio(int fd, short ev, void *d)
1085 struct keymap *k;
1086 const char *keyname;
1087 char tmp[2] = {0};
1089 thiskey.key = wgetch(body);
1090 if (thiskey.key == ERR)
1091 return;
1092 if (thiskey.key == 27) {
1093 /* TODO: make escape-time customizable */
1095 thiskey.meta = 1;
1096 thiskey.key = wgetch(body);
1097 if (thiskey.key == ERR || thiskey.key == 27) {
1098 thiskey.meta = 0;
1099 thiskey.key = 27;
1101 } else
1102 thiskey.meta = 0;
1104 if (keybuf[0] != '\0')
1105 strlcat(keybuf, " ", sizeof(keybuf));
1106 if (thiskey.meta)
1107 strlcat(keybuf, "M-", sizeof(keybuf));
1108 if ((keyname = unkbd(thiskey.key)) != NULL)
1109 strlcat(keybuf, keyname, sizeof(keybuf));
1110 else {
1111 tmp[0] = thiskey.key;
1112 strlcat(keybuf, tmp, sizeof(keybuf));
1115 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1116 if (k->meta == thiskey.meta &&
1117 k->key == thiskey.key) {
1118 if (k->fn == NULL)
1119 current_map = &k->map;
1120 else {
1121 current_map = base_map;
1122 strlcpy(keybuf, "", sizeof(keybuf));
1123 k->fn(current_tab());
1125 goto done;
1129 if (current_map->unhandled_input != NULL)
1130 current_map->unhandled_input();
1131 else {
1132 global_key_unbound();
1135 strlcpy(keybuf, "", sizeof(keybuf));
1136 current_map = base_map;
1138 done:
1139 redraw_tab(current_tab());
1142 static void
1143 handle_clear_minibuf(int fd, short ev, void *d)
1145 free(ministate.curmesg);
1146 ministate.curmesg = NULL;
1148 redraw_minibuffer();
1149 if (in_minibuffer) {
1150 wrefresh(body);
1151 wrefresh(minibuf);
1152 } else {
1153 wrefresh(minibuf);
1154 wrefresh(body);
1158 static void
1159 handle_resize(int sig, short ev, void *d)
1161 struct tab *tab;
1163 endwin();
1164 refresh();
1165 clear();
1167 /* move and resize the windows, in reverse order! */
1169 mvwin(minibuf, LINES-1, 0);
1170 wresize(minibuf, 1, COLS);
1172 mvwin(modeline, LINES-2, 0);
1173 wresize(modeline, 1, COLS);
1175 wresize(body, LINES-3, COLS);
1176 body_lines = LINES-3;
1177 body_cols = COLS;
1179 wresize(tabline, 1, COLS);
1181 tab = current_tab();
1183 wrap_page(tab);
1184 redraw_tab(tab);
1187 static int
1188 wrap_page(struct tab *tab)
1190 struct line *l;
1191 const struct line *orig;
1192 struct vline *vl;
1193 const char *prfx;
1195 orig = tab->s.current_line == NULL
1196 ? NULL
1197 : tab->s.current_line->parent;
1198 tab->s.current_line = NULL;
1200 tab->s.curs_y = 0;
1201 tab->s.line_off = 0;
1203 empty_vlist(tab);
1205 TAILQ_FOREACH(l, &tab->page.head, lines) {
1206 prfx = line_prefixes[l->type].prfx1;
1207 switch (l->type) {
1208 case LINE_TEXT:
1209 case LINE_LINK:
1210 case LINE_TITLE_1:
1211 case LINE_TITLE_2:
1212 case LINE_TITLE_3:
1213 case LINE_ITEM:
1214 case LINE_QUOTE:
1215 case LINE_PRE_START:
1216 case LINE_PRE_END:
1217 wrap_text(tab, prfx, l, body_cols);
1218 break;
1219 case LINE_PRE_CONTENT:
1220 hardwrap_text(tab, l, body_cols);
1221 break;
1224 if (orig == l && tab->s.current_line == NULL) {
1225 tab->s.line_off = tab->s.line_max-1;
1226 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1228 while (1) {
1229 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1230 if (vl == NULL || vl->parent != orig)
1231 break;
1232 tab->s.current_line = vl;
1233 tab->s.line_off--;
1238 if (tab->s.current_line == NULL)
1239 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1241 return 1;
1244 static void
1245 print_vline(struct vline *vl)
1247 const char *text = vl->line;
1248 const char *prfx;
1249 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1250 int text_face = line_faces[vl->parent->type].text_prop;
1252 if (!vl->flags)
1253 prfx = line_prefixes[vl->parent->type].prfx1;
1254 else
1255 prfx = line_prefixes[vl->parent->type].prfx2;
1257 if (text == NULL)
1258 text = "";
1260 wattron(body, prefix_face);
1261 wprintw(body, "%s", prfx);
1262 wattroff(body, prefix_face);
1264 wattron(body, text_face);
1265 wprintw(body, "%s", text);
1266 wattroff(body, text_face);
1269 static void
1270 redraw_tabline(void)
1272 struct tab *tab;
1273 size_t toskip;
1274 int current, x, y, truncated;
1275 const char *title;
1276 char buf[25];
1278 toskip = 0;
1279 x = 1;
1280 TAILQ_FOREACH(tab, &tabshead, tabs) {
1281 x += sizeof(buf) + 1;
1282 toskip++;
1283 if (tab->flags & TAB_CURRENT)
1284 break;
1286 if (x < COLS-2)
1287 toskip = 0;
1288 else
1289 toskip--;
1291 werase(tabline);
1292 wattron(tabline, tab_face.background);
1293 wprintw(tabline, toskip == 0 ? " " : "<");
1294 wattroff(tabline, tab_face.background);
1296 truncated = 0;
1297 TAILQ_FOREACH(tab, &tabshead, tabs) {
1298 if (truncated)
1299 break;
1300 if (toskip != 0) {
1301 toskip--;
1302 continue;
1305 getyx(tabline, y, x);
1306 if (x + sizeof(buf)+2 >= (size_t)COLS)
1307 truncated = 1;
1309 current = tab->flags & TAB_CURRENT;
1311 if (*(title = tab->page.title) == '\0')
1312 title = tab->hist_cur->h;
1314 strlcpy(buf, " ", sizeof(buf));
1315 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1316 /* truncation happens */
1317 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1318 } else {
1319 /* pad with spaces */
1320 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1321 /* nop */ ;
1324 if (current)
1325 wattron(tabline, tab_face.current_tab);
1326 else
1327 wattron(tabline, tab_face.tab);
1329 wprintw(tabline, "%s", buf);
1330 if (TAILQ_NEXT(tab, tabs) != NULL)
1331 wprintw(tabline, " ");
1333 if (current)
1334 wattroff(tabline, tab_face.current_tab);
1335 else
1336 wattroff(tabline, tab_face.tab);
1339 wattron(tabline, tab_face.background);
1340 for (; x < COLS; ++x)
1341 waddch(tabline, ' ');
1342 if (truncated)
1343 mvwprintw(tabline, 0, COLS-1, ">");
1346 static inline char
1347 trust_status_char(enum trust_state ts)
1349 switch (ts) {
1350 case TS_UNKNOWN: return 'u';
1351 case TS_UNTRUSTED: return '!';
1352 case TS_TRUSTED: return 'v';
1353 case TS_VERIFIED: return 'V';
1357 static void
1358 redraw_modeline(struct tab *tab)
1360 double pct;
1361 int x, y, max_x, max_y;
1362 const char *mode = tab->page.name;
1363 const char *spin = "-\\|/";
1365 werase(modeline);
1366 wattron(modeline, A_REVERSE);
1367 wmove(modeline, 0, 0);
1369 wprintw(modeline, "-%c%c %s ",
1370 spin[tab->s.loading_anim_step],
1371 trust_status_char(tab->trust),
1372 mode == NULL ? "(none)" : mode);
1374 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1376 if (tab->s.line_max <= (size_t)body_lines)
1377 wprintw(modeline, "All ");
1378 else if (tab->s.line_off == 0)
1379 wprintw(modeline, "Top ");
1380 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1381 wprintw(modeline, "Bottom ");
1382 else
1383 wprintw(modeline, "%.0f%% ", pct);
1385 wprintw(modeline, "%d/%d %s ",
1386 tab->s.line_off + tab->s.curs_y,
1387 tab->s.line_max,
1388 tab->hist_cur->h);
1390 getyx(modeline, y, x);
1391 getmaxyx(modeline, max_y, max_x);
1393 (void)y;
1394 (void)max_y;
1396 for (; x < max_x; ++x)
1397 waddstr(modeline, "-");
1400 static void
1401 redraw_minibuffer(void)
1403 size_t skip = 0, off_x = 0, off_y = 0;
1404 struct tab *tab;
1406 werase(minibuf);
1407 if (in_minibuffer) {
1408 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1409 if (ministate.hist_cur != NULL)
1410 wprintw(minibuf, "(%zu/%zu) ",
1411 ministate.hist_off + 1,
1412 ministate.history->len);
1414 getyx(minibuf, off_y, off_x);
1416 while (ministate.off - skip > (size_t)COLS / 2) {
1417 skip += MIN(ministate.off/4, 1);
1420 if (ministate.hist_cur != NULL)
1421 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1422 else
1423 wprintw(minibuf, "%s", ministate.buf + skip);
1426 if (ministate.curmesg != NULL) {
1427 if (in_minibuffer)
1428 wprintw(minibuf, " [%s]", ministate.curmesg);
1429 else
1430 wprintw(minibuf, "%s", ministate.curmesg);
1433 if (!in_minibuffer && ministate.curmesg == NULL)
1434 wprintw(minibuf, "%s", keybuf);
1436 /* If nothing else, show the URL at point */
1437 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1438 tab = current_tab();
1439 if (tab->s.current_line != NULL &&
1440 tab->s.current_line->parent->type == LINE_LINK)
1441 wprintw(minibuf, "%s",
1442 tab->s.current_line->parent->alt);
1445 if (in_minibuffer)
1446 wmove(minibuf, 0, off_x + ministate.off - skip);
1449 static void
1450 redraw_tab(struct tab *tab)
1452 redraw_tabline();
1453 redraw_body(tab);
1454 redraw_modeline(tab);
1455 redraw_minibuffer();
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;
1491 wmove(body, tab->s.curs_y, tab->s.curs_x);
1494 static void
1495 vmessage(const char *fmt, va_list ap)
1497 if (evtimer_pending(&clminibufev, NULL))
1498 evtimer_del(&clminibufev);
1499 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1500 evtimer_add(&clminibufev, &clminibufev_timer);
1502 free(ministate.curmesg);
1504 /* TODO: what to do if the allocation fails here? */
1505 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1506 ministate.curmesg = NULL;
1508 redraw_minibuffer();
1509 if (in_minibuffer) {
1510 wrefresh(body);
1511 wrefresh(minibuf);
1512 } else {
1513 wrefresh(minibuf);
1514 wrefresh(body);
1518 static void
1519 message(const char *fmt, ...)
1521 va_list ap;
1523 va_start(ap, fmt);
1524 vmessage(fmt, ap);
1525 va_end(ap);
1528 static void
1529 start_loading_anim(struct tab *tab)
1531 if (tab->s.loading_anim)
1532 return;
1533 tab->s.loading_anim = 1;
1534 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1535 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1538 static void
1539 update_loading_anim(int fd, short ev, void *d)
1541 struct tab *tab = d;
1543 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1545 if (tab->flags & TAB_CURRENT) {
1546 redraw_modeline(tab);
1547 wrefresh(modeline);
1548 wrefresh(body);
1549 if (in_minibuffer)
1550 wrefresh(minibuf);
1553 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1556 static void
1557 stop_loading_anim(struct tab *tab)
1559 if (!tab->s.loading_anim)
1560 return;
1561 evtimer_del(&tab->s.loadingev);
1562 tab->s.loading_anim = 0;
1563 tab->s.loading_anim_step = 0;
1565 if (!(tab->flags & TAB_CURRENT))
1566 return;
1568 redraw_modeline(tab);
1570 wrefresh(modeline);
1571 wrefresh(body);
1572 if (in_minibuffer)
1573 wrefresh(minibuf);
1576 static void
1577 load_url_in_tab(struct tab *tab, const char *url)
1579 empty_vlist(tab);
1580 message("Loading %s...", url);
1581 start_loading_anim(tab);
1582 load_url(tab, url);
1584 tab->s.curs_x = 0;
1585 tab->s.curs_y = 0;
1586 redraw_tab(tab);
1589 static void
1590 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1591 void (*abortfn)(void), struct histhead *hist)
1593 in_minibuffer = 1;
1594 base_map = &minibuffer_map;
1595 current_map = &minibuffer_map;
1597 base_map->unhandled_input = self_insert_fn;
1599 ministate.donefn = donefn;
1600 ministate.abortfn = abortfn;
1601 memset(ministate.buf, 0, sizeof(ministate.buf));
1602 ministate.off = 0;
1603 ministate.len = 0;
1604 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1606 ministate.history = hist;
1607 ministate.hist_cur = NULL;
1608 ministate.hist_off = 0;
1611 static void
1612 exit_minibuffer(void)
1614 werase(minibuf);
1616 in_minibuffer = 0;
1617 base_map = &global_map;
1618 current_map = &global_map;
1621 static void
1622 switch_to_tab(struct tab *tab)
1624 struct tab *t;
1626 TAILQ_FOREACH(t, &tabshead, tabs) {
1627 t->flags &= ~TAB_CURRENT;
1630 tab->flags |= TAB_CURRENT;
1633 static struct tab *
1634 new_tab(const char *url)
1636 struct tab *tab;
1638 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1639 event_loopbreak();
1640 return NULL;
1643 TAILQ_INIT(&tab->hist.head);
1645 TAILQ_INIT(&tab->s.head);
1647 tab->id = tab_counter++;
1648 switch_to_tab(tab);
1650 if (TAILQ_EMPTY(&tabshead))
1651 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1652 else
1653 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1655 load_url_in_tab(tab, url);
1656 return tab;
1659 static void
1660 usage(void)
1662 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1665 int
1666 ui_init(int argc, char * const *argv)
1668 const char *url = NEW_TAB_URL;
1669 int ch;
1671 while ((ch = getopt(argc, argv, "")) != -1) {
1672 switch (ch) {
1673 default:
1674 usage();
1675 return 0;
1678 argc -= optind;
1679 argv += optind;
1681 if (argc != 0)
1682 url = argv[0];
1684 setlocale(LC_ALL, "");
1686 TAILQ_INIT(&global_map.m);
1687 global_map.unhandled_input = global_key_unbound;
1689 TAILQ_INIT(&minibuffer_map.m);
1691 TAILQ_INIT(&eecmd_history.head);
1692 TAILQ_INIT(&ir_history.head);
1693 TAILQ_INIT(&lu_history.head);
1695 base_map = &global_map;
1696 current_map = &global_map;
1697 load_default_keys();
1699 initscr();
1700 raw();
1701 noecho();
1703 nonl();
1704 intrflush(stdscr, FALSE);
1706 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1707 return 0;
1708 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1709 return 0;
1710 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1711 return 0;
1712 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1713 return 0;
1715 body_lines = LINES-3;
1716 body_cols = COLS;
1718 keypad(body, TRUE);
1719 scrollok(body, TRUE);
1721 /* non-blocking input */
1722 wtimeout(body, 0);
1724 mvwprintw(body, 0, 0, "");
1726 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1727 event_add(&stdioev, NULL);
1729 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1730 signal_add(&winchev, NULL);
1732 new_tab(url);
1734 return 1;
1737 void
1738 ui_on_tab_loaded(struct tab *tab)
1740 stop_loading_anim(tab);
1741 message("Loaded %s", tab->hist_cur->h);
1743 redraw_tabline();
1744 wrefresh(tabline);
1745 if (in_minibuffer)
1746 wrefresh(minibuf);
1747 else
1748 wrefresh(body);
1751 void
1752 ui_on_tab_refresh(struct tab *tab)
1754 wrap_page(tab);
1755 if (tab->flags & TAB_CURRENT)
1756 redraw_tab(tab);
1759 void
1760 ui_require_input(struct tab *tab, int hide)
1762 /* TODO: hard-switching to another tab is ugly */
1763 switch_to_tab(tab);
1765 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1766 &ir_history);
1767 strlcpy(ministate.prompt, "Input required: ",
1768 sizeof(ministate.prompt));
1769 redraw_tab(tab);
1772 void
1773 ui_notify(const char *fmt, ...)
1775 va_list ap;
1777 va_start(ap, fmt);
1778 vmessage(fmt, ap);
1779 va_end(ap);
1782 void
1783 ui_end(void)
1785 endwin();