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 <curses.h>
40 #include <event.h>
41 #include <locale.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #define TAB_CURRENT 0x1
50 #define NEW_TAB_URL "about:new"
52 static struct event stdioev, winchev;
54 static void load_default_keys(void);
55 static void empty_vlist(struct window*);
56 static void restore_cursor(struct window*);
58 static void cmd_previous_line(struct window*);
59 static void cmd_next_line(struct window*);
60 static void cmd_backward_char(struct window*);
61 static void cmd_forward_char(struct window*);
62 static void cmd_backward_paragraph(struct window*);
63 static void cmd_forward_paragraph(struct window*);
64 static void cmd_move_beginning_of_line(struct window*);
65 static void cmd_move_end_of_line(struct window*);
66 static void cmd_redraw(struct window*);
67 static void cmd_scroll_line_down(struct window*);
68 static void cmd_scroll_line_up(struct window*);
69 static void cmd_scroll_up(struct window*);
70 static void cmd_scroll_down(struct window*);
71 static void cmd_beginning_of_buffer(struct window*);
72 static void cmd_end_of_buffer(struct window*);
73 static void cmd_kill_telescope(struct window*);
74 static void cmd_push_button(struct window*);
75 static void cmd_push_button_new_tab(struct window*);
76 static void cmd_previous_button(struct window*);
77 static void cmd_next_button(struct window*);
78 static void cmd_previous_page(struct window*);
79 static void cmd_next_page(struct window*);
80 static void cmd_clear_minibuf(struct window*);
81 static void cmd_execute_extended_command(struct window*);
82 static void cmd_tab_close(struct window*);
83 static void cmd_tab_close_other(struct window*);
84 static void cmd_tab_new(struct window*);
85 static void cmd_tab_next(struct window*);
86 static void cmd_tab_previous(struct window*);
87 static void cmd_load_url(struct window*);
88 static void cmd_load_current_url(struct window*);
89 static void cmd_bookmark_page(struct window*);
90 static void cmd_goto_bookmarks(struct window*);
92 static void global_key_unbound(void);
94 static void cmd_mini_delete_char(struct window*);
95 static void cmd_mini_delete_backward_char(struct window*);
96 static void cmd_mini_kill_line(struct window*);
97 static void cmd_mini_abort(struct window*);
98 static void cmd_mini_complete_and_exit(struct window*);
99 static void cmd_mini_previous_history_element(struct window*);
100 static void cmd_mini_next_history_element(struct window*);
102 static void minibuffer_hist_save_entry(void);
103 static void minibuffer_taint_hist(void);
104 static void minibuffer_self_insert(void);
105 static void eecmd_self_insert(void);
106 static void eecmd_select(void);
107 static void ir_self_insert(void);
108 static void ir_select(void);
109 static void lu_self_insert(void);
110 static void lu_select(void);
111 static void bp_select(void);
113 static struct vline *nth_line(struct window*, size_t);
114 static struct tab *current_tab(void);
115 static struct window *current_window(void);
116 static int readkey(void);
117 static void dispatch_stdio(int, short, void*);
118 static void handle_clear_minibuf(int, short, void*);
119 static void handle_resize(int, short, void*);
120 static int wrap_page(struct window*);
121 static void print_vline(struct vline*);
122 static void redraw_tabline(void);
123 static void redraw_body(struct tab*);
124 static void redraw_modeline(struct tab*);
125 static void redraw_minibuffer(void);
126 static void redraw_tab(struct tab*);
127 static void vmessage(const char*, va_list);
128 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
129 static void start_loading_anim(struct tab*);
130 static void update_loading_anim(int, short, void*);
131 static void stop_loading_anim(struct tab*);
132 static void load_url_in_tab(struct tab*, const char*);
133 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
134 static void exit_minibuffer(void);
135 static void switch_to_tab(struct tab*);
136 static struct tab *new_tab(const char*);
137 static void usage(void);
139 static struct { short meta; int key; uint32_t cp; } thiskey;
141 static WINDOW *tabline, *body, *modeline, *minibuf;
142 static int body_lines, body_cols;
144 static struct event clminibufev;
145 static struct timeval clminibufev_timer = { 5, 0 };
146 static struct timeval loadingev_timer = { 0, 250000 };
148 static uint32_t tab_counter;
150 static char keybuf[64];
152 struct kmap global_map,
153 minibuffer_map,
154 *current_map,
155 *base_map;
157 static struct histhead eecmd_history,
158 ir_history,
159 lu_history;
161 static int in_minibuffer;
163 static struct {
164 char *curmesg;
166 char prompt[32];
167 void (*donefn)(void);
168 void (*abortfn)(void);
170 char buf[1025];
171 struct line line;
172 struct vline vline;
173 struct window window;
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 window *window)
221 struct vline *vl, *t;
223 window->current_line = NULL;
224 window->line_max = 0;
226 TAILQ_FOREACH_SAFE(vl, &window->head, vlines, t) {
227 TAILQ_REMOVE(&window->head, vl, vlines);
228 free(vl->line);
229 free(vl);
233 static inline void
234 global_set_key(const char *key, void (*fn)(struct window*))
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 window*))
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-b", cmd_backward_char);
349 minibuffer_set_key("C-f", cmd_forward_char);
350 minibuffer_set_key("<left>", cmd_backward_char);
351 minibuffer_set_key("<right>", cmd_forward_char);
352 minibuffer_set_key("C-e", cmd_move_end_of_line);
353 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
354 minibuffer_set_key("<end>", cmd_move_end_of_line);
355 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
356 minibuffer_set_key("C-k", cmd_mini_kill_line);
358 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
359 minibuffer_set_key("M-n", cmd_mini_next_history_element);
360 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
361 minibuffer_set_key("<down>", cmd_mini_next_history_element);
364 static void
365 restore_cursor(struct window *window)
367 struct vline *vl;
368 const char *prfx;
370 vl = window->current_line;
371 if (vl == NULL || vl->line == NULL)
372 window->curs_x = window->cpoff = 0;
373 else
374 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
376 if (vl != NULL) {
377 prfx = line_prefixes[vl->parent->type].prfx1;
378 window->curs_x += utf8_swidth(prfx);
382 static void
383 cmd_previous_line(struct window *window)
385 struct vline *vl;
387 if (window->current_line == NULL
388 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
389 return;
391 if (--window->curs_y < 0) {
392 window->curs_y = 0;
393 cmd_scroll_line_up(window);
394 return;
397 window->current_line = vl;
398 restore_cursor(window);
401 static void
402 cmd_next_line(struct window *window)
404 struct vline *vl;
406 if (window->current_line == NULL
407 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
408 return;
410 if (++window->curs_y > body_lines-1) {
411 window->curs_y = body_lines-1;
412 cmd_scroll_line_down(window);
413 return;
416 window->current_line = vl;
417 restore_cursor(window);
420 static void
421 cmd_backward_char(struct window *window)
423 if (window->cpoff != 0)
424 window->cpoff--;
425 restore_cursor(window);
428 static void
429 cmd_forward_char(struct window *window)
431 window->cpoff++;
432 restore_cursor(window);
435 static void
436 cmd_backward_paragraph(struct window *window)
438 do {
439 if (window->current_line == NULL ||
440 window->current_line == TAILQ_FIRST(&window->head)) {
441 message("No previous paragraph");
442 return;
444 cmd_previous_line(window);
445 } while (window->current_line->line != NULL ||
446 window->current_line->parent->type != LINE_TEXT);
449 static void
450 cmd_forward_paragraph(struct window *window)
452 do {
453 if (window->current_line == NULL ||
454 window->current_line == TAILQ_LAST(&window->head, vhead)) {
455 message("No next paragraph");
456 return;
458 cmd_next_line(window);
459 } while (window->current_line->line != NULL ||
460 window->current_line->parent->type != LINE_TEXT);
463 static void
464 cmd_move_beginning_of_line(struct window *window)
466 window->cpoff = 0;
467 restore_cursor(window);
470 static void
471 cmd_move_end_of_line(struct window *window)
473 struct vline *vl;
475 vl = window->current_line;
476 if (vl->line == NULL)
477 return;
478 window->cpoff = utf8_cplen(vl->line);
479 restore_cursor(window);
482 static void
483 cmd_redraw(struct window *window)
485 handle_resize(0, 0, NULL);
488 static void
489 cmd_scroll_line_up(struct window *window)
491 struct vline *vl;
493 if (window->line_off == 0)
494 return;
496 vl = nth_line(window, --window->line_off);
497 wscrl(body, -1);
498 wmove(body, 0, 0);
499 print_vline(vl);
501 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
502 restore_cursor(window);
505 static void
506 cmd_scroll_line_down(struct window *window)
508 struct vline *vl;
510 vl = window->current_line;
511 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
512 return;
513 window->current_line = vl;
515 window->line_off++;
516 wscrl(body, 1);
518 if (window->line_max - window->line_off < (size_t)body_lines)
519 return;
521 vl = nth_line(window, window->line_off + body_lines-1);
522 wmove(body, body_lines-1, 0);
523 print_vline(vl);
525 restore_cursor(window);
528 static void
529 cmd_scroll_up(struct window *window)
531 size_t off;
533 off = body_lines+1;
535 for (; off > 0; --off)
536 cmd_scroll_line_up(window);
539 static void
540 cmd_scroll_down(struct window *window)
542 size_t off;
544 off = body_lines+1;
546 for (; off > 0; --off)
547 cmd_scroll_line_down(window);
550 static void
551 cmd_beginning_of_buffer(struct window *window)
553 window->current_line = TAILQ_FIRST(&window->head);
554 window->line_off = 0;
555 window->curs_y = 0;
556 window->cpoff = 0;
557 restore_cursor(window);
560 static void
561 cmd_end_of_buffer(struct window *window)
563 ssize_t off;
565 off = window->line_max - body_lines;
566 off = MAX(0, off);
568 window->line_off = off;
569 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
571 window->current_line = TAILQ_LAST(&window->head, vhead);
572 window->cpoff = body_cols;
573 restore_cursor(window);
576 static void
577 cmd_kill_telescope(struct window *window)
579 event_loopbreak();
582 static void
583 cmd_push_button(struct window *window)
585 struct vline *vl;
586 size_t nth;
588 nth = window->line_off + window->curs_y;
589 if (nth >= window->line_max)
590 return;
591 vl = nth_line(window, nth);
592 if (vl->parent->type != LINE_LINK)
593 return;
595 load_url_in_tab(current_tab(), vl->parent->alt);
598 static void
599 cmd_push_button_new_tab(struct window *window)
601 struct vline *vl;
602 size_t nth;
604 nth = window->line_off + window->curs_y;
605 if (nth > window->line_max)
606 return;
607 vl = nth_line(window, nth);
608 if (vl->parent->type != LINE_LINK)
609 return;
611 new_tab(vl->parent->alt);
614 static void
615 cmd_previous_button(struct window *window)
617 do {
618 if (window->current_line == NULL ||
619 window->current_line == TAILQ_FIRST(&window->head)) {
620 message("No previous link");
621 return;
623 cmd_previous_line(window);
624 } while (window->current_line->parent->type != LINE_LINK);
627 static void
628 cmd_next_button(struct window *window)
630 do {
631 if (window->current_line == NULL ||
632 window->current_line == TAILQ_LAST(&window->head, vhead)) {
633 message("No next link");
634 return;
636 cmd_next_line(window);
637 } while (window->current_line->parent->type != LINE_LINK);
640 static void
641 cmd_previous_page(struct window *window)
643 struct tab *tab = current_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 window *window)
654 struct tab *tab = current_tab();
656 if (!load_next_page(tab))
657 message("No next page");
658 else
659 start_loading_anim(tab);
662 static void
663 cmd_clear_minibuf(struct window *window)
665 handle_clear_minibuf(0, 0, NULL);
668 static void
669 cmd_execute_extended_command(struct window *window)
671 size_t len;
673 if (in_minibuffer) {
674 message("We don't have enable-recursive-minibuffers");
675 return;
678 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
679 &eecmd_history);
681 len = sizeof(ministate.prompt);
682 strlcpy(ministate.prompt, "", len);
684 if (thiskey.meta)
685 strlcat(ministate.prompt, "M-", len);
687 strlcat(ministate.prompt, keyname(thiskey.key), len);
688 strlcat(ministate.prompt, " ", len);
691 static void
692 cmd_tab_close(struct window *window)
694 struct tab *tab, *t;
696 tab = current_tab();
697 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
698 TAILQ_NEXT(tab, tabs) == NULL) {
699 message("Can't close the only tab.");
700 return;
703 if (evtimer_pending(&tab->loadingev, NULL))
704 evtimer_del(&tab->loadingev);
706 stop_tab(tab);
708 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
709 t = TAILQ_NEXT(tab, tabs);
710 TAILQ_REMOVE(&tabshead, tab, tabs);
711 free(tab);
713 switch_to_tab(t);
716 static void
717 cmd_tab_close_other(struct window *window)
719 struct tab *tab, *t, *i;
721 tab = current_tab();
722 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
723 if (t->flags & TAB_CURRENT)
724 continue;
726 stop_tab(t);
727 TAILQ_REMOVE(&tabshead, t, tabs);
728 free(t);
732 static void
733 cmd_tab_new(struct window *window)
735 new_tab(NEW_TAB_URL);
738 static void
739 cmd_tab_next(struct window *window)
741 struct tab *tab, *t;
743 tab = current_tab();
744 tab->flags &= ~TAB_CURRENT;
746 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
747 t = TAILQ_FIRST(&tabshead);
748 t->flags |= TAB_CURRENT;
751 static void
752 cmd_tab_previous(struct window *window)
754 struct tab *tab, *t;
756 tab = current_tab();
757 tab->flags &= ~TAB_CURRENT;
759 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
760 t = TAILQ_LAST(&tabshead, tabshead);
761 t->flags |= TAB_CURRENT;
764 static void
765 cmd_load_url(struct window *window)
767 if (in_minibuffer) {
768 message("We don't have enable-recursive-minibuffers");
769 return;
772 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
773 &lu_history);
774 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
777 static void
778 cmd_load_current_url(struct window *window)
780 struct tab *tab = current_tab();
782 if (in_minibuffer) {
783 message("We don't have enable-recursive-minibuffers");
784 return;
787 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
788 &lu_history);
789 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
790 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
791 ministate.window.cpoff = utf8_cplen(ministate.buf);
794 static void
795 cmd_bookmark_page(struct window *window)
797 struct tab *tab = current_tab();
799 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
800 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
801 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
802 ministate.window.cpoff = utf8_cplen(ministate.buf);
805 static void
806 cmd_goto_bookmarks(struct window *window)
808 load_url_in_tab(current_tab(), "about:bookmarks");
811 static void
812 global_key_unbound(void)
814 message("%s is undefined", keybuf);
817 static void
818 cmd_mini_delete_char(struct window *window)
820 char *c, *n;
822 if (!in_minibuffer) {
823 message("text is read-only");
824 return;
827 minibuffer_taint_hist();
829 c = utf8_nth(window->current_line->line, window->cpoff);
830 if (*c == '\0')
831 return;
832 n = utf8_next_cp(c);
834 memmove(c, n, strlen(n)+1);
837 static void
838 cmd_mini_delete_backward_char(struct window *window)
840 char *c, *p, *start;
842 if (!in_minibuffer) {
843 message("text is read-only");
844 return;
847 minibuffer_taint_hist();
849 c = utf8_nth(window->current_line->line, window->cpoff);
850 start = window->current_line->line;
851 if (c == start)
852 return;
853 p = utf8_prev_cp(c-1, start);
855 memmove(p, c, strlen(c)+1);
856 window->cpoff--;
859 static void
860 cmd_mini_kill_line(struct window *window)
862 char *c;
864 if (!in_minibuffer) {
865 message("text is read-only");
866 return;
869 minibuffer_taint_hist();
870 c = utf8_nth(window->current_line->line, window->cpoff);
871 *c = '\0';
874 static void
875 cmd_mini_abort(struct window *window)
877 if (!in_minibuffer)
878 return;
880 ministate.abortfn();
883 static void
884 cmd_mini_complete_and_exit(struct window *window)
886 if (!in_minibuffer)
887 return;
889 minibuffer_taint_hist();
890 ministate.donefn();
893 static void
894 cmd_mini_previous_history_element(struct window *window)
896 if (ministate.history == NULL) {
897 message("No history");
898 return;
901 if (ministate.hist_cur == NULL ||
902 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
903 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
904 ministate.hist_off = ministate.history->len - 1;
905 if (ministate.hist_cur == NULL)
906 message("No prev item");
907 } else {
908 ministate.hist_off--;
911 if (ministate.hist_cur != NULL)
912 window->current_line->line = ministate.hist_cur->h;
915 static void
916 cmd_mini_next_history_element(struct window *window)
918 if (ministate.history == NULL) {
919 message("No history");
920 return;
923 if (ministate.hist_cur == NULL ||
924 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
925 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
926 ministate.hist_off = 0;
927 if (ministate.hist_cur == NULL)
928 message("No next item");
929 } else {
930 ministate.hist_off++;
933 if (ministate.hist_cur != NULL)
934 window->current_line->line = ministate.hist_cur->h;
937 static void
938 minibuffer_hist_save_entry(void)
940 struct hist *hist;
942 if (ministate.history == NULL)
943 return;
945 if ((hist = calloc(1, sizeof(*hist))) == NULL)
946 abort();
948 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
950 if (TAILQ_EMPTY(&ministate.history->head))
951 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
952 else
953 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
954 ministate.history->len++;
957 /*
958 * taint the minibuffer cache: if we're currently showing a history
959 * element, copy that to the current buf and reset the "history
960 * navigation" thing.
961 */
962 static void
963 minibuffer_taint_hist(void)
965 if (ministate.hist_cur == NULL)
966 return;
968 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
969 ministate.hist_cur = NULL;
972 static void
973 minibuffer_self_insert(void)
975 char *c, tmp[5] = {0};
976 size_t len;
978 minibuffer_taint_hist();
980 if (thiskey.cp == 0)
981 return;
983 len = utf8_encode(thiskey.cp, tmp);
984 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
985 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
986 return;
988 memmove(c + len, c, strlen(c)+1);
989 memcpy(c, tmp, len);
990 ministate.window.cpoff++;
993 static void
994 eecmd_self_insert(void)
996 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
997 !unicode_isgraph(thiskey.cp)) {
998 global_key_unbound();
999 return;
1002 minibuffer_self_insert();
1005 static void
1006 eecmd_select(void)
1008 exit_minibuffer();
1009 minibuffer_hist_save_entry();
1010 message("TODO: try to execute %s", ministate.buf);
1013 static void
1014 ir_self_insert(void)
1016 minibuffer_self_insert();
1019 static void
1020 ir_select(void)
1022 char buf[1025] = {0};
1023 struct url url;
1024 struct tab *tab;
1026 tab = current_tab();
1028 exit_minibuffer();
1029 minibuffer_hist_save_entry();
1031 /* a bit ugly but... */
1032 memcpy(&url, &tab->url, sizeof(tab->url));
1033 url_set_query(&url, ministate.buf);
1034 url_unparse(&url, buf, sizeof(buf));
1035 load_url_in_tab(tab, buf);
1038 static void
1039 lu_self_insert(void)
1041 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1042 !unicode_isgraph(thiskey.key)) {
1043 global_key_unbound();
1044 return;
1047 minibuffer_self_insert();
1050 static void
1051 lu_select(void)
1053 exit_minibuffer();
1054 minibuffer_hist_save_entry();
1055 load_url_in_tab(current_tab(), ministate.buf);
1058 static void
1059 bp_select(void)
1061 exit_minibuffer();
1062 if (*ministate.buf != '\0')
1063 add_to_bookmarks(ministate.buf);
1064 else
1065 message("Abort.");
1068 static struct vline *
1069 nth_line(struct window *window, size_t n)
1071 struct vline *vl;
1072 size_t i;
1074 i = 0;
1075 TAILQ_FOREACH(vl, &window->head, vlines) {
1076 if (i == n)
1077 return vl;
1078 i++;
1081 /* unreachable */
1082 abort();
1085 static struct tab *
1086 current_tab(void)
1088 struct tab *t;
1090 TAILQ_FOREACH(t, &tabshead, tabs) {
1091 if (t->flags & TAB_CURRENT)
1092 return t;
1095 /* unreachable */
1096 abort();
1099 static struct window *
1100 current_window(void)
1102 if (in_minibuffer)
1103 return &ministate.window;
1104 return &current_tab()->window;
1107 static int
1108 readkey(void)
1110 uint32_t state = 0;
1112 if ((thiskey.key = wgetch(body)) == ERR)
1113 return 0;
1115 thiskey.meta = thiskey.key == 27;
1116 if (thiskey.meta) {
1117 thiskey.key = wgetch(body);
1118 if (thiskey.key == ERR || thiskey.key == 27) {
1119 thiskey.meta = 0;
1120 thiskey.key = 27;
1124 thiskey.cp = 0;
1125 if ((unsigned int)thiskey.key < UINT8_MAX) {
1126 while (1) {
1127 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1128 break;
1129 if ((thiskey.key = wgetch(body)) == ERR) {
1130 message("Error decoding user input");
1131 return 0;
1136 return 1;
1139 static void
1140 dispatch_stdio(int fd, short ev, void *d)
1142 struct keymap *k;
1143 const char *keyname;
1144 char tmp[5] = {0};
1146 if (!readkey())
1147 return;
1149 if (keybuf[0] != '\0')
1150 strlcat(keybuf, " ", sizeof(keybuf));
1151 if (thiskey.meta)
1152 strlcat(keybuf, "M-", sizeof(keybuf));
1153 if (thiskey.cp != 0) {
1154 utf8_encode(thiskey.cp, tmp);
1155 strlcat(keybuf, tmp, sizeof(keybuf));
1156 } else {
1157 if ((keyname = unkbd(thiskey.key)) != NULL)
1158 strlcat(keybuf, keyname, sizeof(keybuf));
1159 else {
1160 tmp[0] = thiskey.key;
1161 strlcat(keybuf, tmp, sizeof(keybuf));
1165 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1166 if (k->meta == thiskey.meta &&
1167 k->key == thiskey.key) {
1168 if (k->fn == NULL)
1169 current_map = &k->map;
1170 else {
1171 current_map = base_map;
1172 strlcpy(keybuf, "", sizeof(keybuf));
1173 k->fn(current_window());
1175 goto done;
1179 if (current_map->unhandled_input != NULL)
1180 current_map->unhandled_input();
1181 else {
1182 global_key_unbound();
1185 strlcpy(keybuf, "", sizeof(keybuf));
1186 current_map = base_map;
1188 done:
1189 redraw_tab(current_tab());
1192 static void
1193 handle_clear_minibuf(int fd, short ev, void *d)
1195 free(ministate.curmesg);
1196 ministate.curmesg = NULL;
1198 redraw_minibuffer();
1199 if (in_minibuffer) {
1200 wrefresh(body);
1201 wrefresh(minibuf);
1202 } else {
1203 wrefresh(minibuf);
1204 wrefresh(body);
1208 static void
1209 handle_resize(int sig, short ev, void *d)
1211 struct tab *tab;
1213 endwin();
1214 refresh();
1215 clear();
1217 /* move and resize the windows, in reverse order! */
1219 mvwin(minibuf, LINES-1, 0);
1220 wresize(minibuf, 1, COLS);
1222 mvwin(modeline, LINES-2, 0);
1223 wresize(modeline, 1, COLS);
1225 wresize(body, LINES-3, COLS);
1226 body_lines = LINES-3;
1227 body_cols = COLS;
1229 wresize(tabline, 1, COLS);
1231 tab = current_tab();
1233 wrap_page(&tab->window);
1234 redraw_tab(tab);
1237 static int
1238 wrap_page(struct window *window)
1240 struct line *l;
1241 const struct line *orig;
1242 struct vline *vl;
1243 const char *prfx;
1245 orig = window->current_line == NULL
1246 ? NULL
1247 : window->current_line->parent;
1248 window->current_line = NULL;
1250 window->curs_y = 0;
1251 window->line_off = 0;
1253 empty_vlist(window);
1255 TAILQ_FOREACH(l, &window->page.head, lines) {
1256 prfx = line_prefixes[l->type].prfx1;
1257 switch (l->type) {
1258 case LINE_TEXT:
1259 case LINE_LINK:
1260 case LINE_TITLE_1:
1261 case LINE_TITLE_2:
1262 case LINE_TITLE_3:
1263 case LINE_ITEM:
1264 case LINE_QUOTE:
1265 case LINE_PRE_START:
1266 case LINE_PRE_END:
1267 wrap_text(window, prfx, l, body_cols);
1268 break;
1269 case LINE_PRE_CONTENT:
1270 hardwrap_text(window, l, body_cols);
1271 break;
1274 if (orig == l && window->current_line == NULL) {
1275 window->line_off = window->line_max-1;
1276 window->current_line = TAILQ_LAST(&window->head, vhead);
1278 while (1) {
1279 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1280 if (vl == NULL || vl->parent != orig)
1281 break;
1282 window->current_line = vl;
1283 window->line_off--;
1288 if (window->current_line == NULL)
1289 window->current_line = TAILQ_FIRST(&window->head);
1291 return 1;
1294 static void
1295 print_vline(struct vline *vl)
1297 const char *text = vl->line;
1298 const char *prfx;
1299 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1300 int text_face = line_faces[vl->parent->type].text_prop;
1302 if (!vl->flags)
1303 prfx = line_prefixes[vl->parent->type].prfx1;
1304 else
1305 prfx = line_prefixes[vl->parent->type].prfx2;
1307 if (text == NULL)
1308 text = "";
1310 wattron(body, prefix_face);
1311 wprintw(body, "%s", prfx);
1312 wattroff(body, prefix_face);
1314 wattron(body, text_face);
1315 wprintw(body, "%s", text);
1316 wattroff(body, text_face);
1319 static void
1320 redraw_tabline(void)
1322 struct tab *tab;
1323 size_t toskip;
1324 int current, x, y, truncated;
1325 const char *title;
1326 char buf[25];
1328 toskip = 0;
1329 x = 1;
1330 TAILQ_FOREACH(tab, &tabshead, tabs) {
1331 x += sizeof(buf) + 1;
1332 toskip++;
1333 if (tab->flags & TAB_CURRENT)
1334 break;
1336 if (x < COLS-2)
1337 toskip = 0;
1338 else
1339 toskip--;
1341 werase(tabline);
1342 wattron(tabline, tab_face.background);
1343 wprintw(tabline, toskip == 0 ? " " : "<");
1344 wattroff(tabline, tab_face.background);
1346 truncated = 0;
1347 TAILQ_FOREACH(tab, &tabshead, tabs) {
1348 if (truncated)
1349 break;
1350 if (toskip != 0) {
1351 toskip--;
1352 continue;
1355 getyx(tabline, y, x);
1356 if (x + sizeof(buf)+2 >= (size_t)COLS)
1357 truncated = 1;
1359 current = tab->flags & TAB_CURRENT;
1361 if (*(title = tab->window.page.title) == '\0')
1362 title = tab->hist_cur->h;
1364 strlcpy(buf, " ", sizeof(buf));
1365 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1366 /* truncation happens */
1367 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1368 } else {
1369 /* pad with spaces */
1370 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1371 /* nop */ ;
1374 if (current)
1375 wattron(tabline, tab_face.current_tab);
1376 else
1377 wattron(tabline, tab_face.tab);
1379 wprintw(tabline, "%s", buf);
1380 if (TAILQ_NEXT(tab, tabs) != NULL)
1381 wprintw(tabline, " ");
1383 if (current)
1384 wattroff(tabline, tab_face.current_tab);
1385 else
1386 wattroff(tabline, tab_face.tab);
1389 wattron(tabline, tab_face.background);
1390 for (; x < COLS; ++x)
1391 waddch(tabline, ' ');
1392 if (truncated)
1393 mvwprintw(tabline, 0, COLS-1, ">");
1396 static inline char
1397 trust_status_char(enum trust_state ts)
1399 switch (ts) {
1400 case TS_UNKNOWN: return 'u';
1401 case TS_UNTRUSTED: return '!';
1402 case TS_TRUSTED: return 'v';
1403 case TS_VERIFIED: return 'V';
1407 static void
1408 redraw_modeline(struct tab *tab)
1410 double pct;
1411 int x, y, max_x, max_y;
1412 const char *mode = tab->window.page.name;
1413 const char *spin = "-\\|/";
1415 werase(modeline);
1416 wattron(modeline, A_REVERSE);
1417 wmove(modeline, 0, 0);
1419 wprintw(modeline, "-%c%c %s ",
1420 spin[tab->loading_anim_step],
1421 trust_status_char(tab->trust),
1422 mode == NULL ? "(none)" : mode);
1424 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1426 if (tab->window.line_max <= (size_t)body_lines)
1427 wprintw(modeline, "All ");
1428 else if (tab->window.line_off == 0)
1429 wprintw(modeline, "Top ");
1430 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1431 wprintw(modeline, "Bottom ");
1432 else
1433 wprintw(modeline, "%.0f%% ", pct);
1435 wprintw(modeline, "%d/%d %s ",
1436 tab->window.line_off + tab->window.curs_y,
1437 tab->window.line_max,
1438 tab->hist_cur->h);
1440 getyx(modeline, y, x);
1441 getmaxyx(modeline, max_y, max_x);
1443 (void)y;
1444 (void)max_y;
1446 for (; x < max_x; ++x)
1447 waddstr(modeline, "-");
1450 static void
1451 redraw_minibuffer(void)
1453 struct tab *tab;
1454 size_t off_y, off_x = 0;
1455 char *start, *c;
1457 werase(minibuf);
1459 if (in_minibuffer) {
1460 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1461 if (ministate.hist_cur != NULL)
1462 wprintw(minibuf, "(%zu/%zu) ",
1463 ministate.hist_off + 1,
1464 ministate.history->len);
1466 getyx(minibuf, off_y, off_x);
1468 start = ministate.hist_cur != NULL
1469 ? ministate.hist_cur->h
1470 : ministate.buf;
1471 c = utf8_nth(ministate.window.current_line->line,
1472 ministate.window.cpoff);
1473 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1474 start = utf8_next_cp(start);
1477 waddstr(minibuf, start);
1480 if (ministate.curmesg != NULL)
1481 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1482 ministate.curmesg);
1484 if (!in_minibuffer && ministate.curmesg == NULL)
1485 waddstr(minibuf, keybuf);
1487 /* If nothing else, show the URL at point */
1488 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1489 tab = current_tab();
1490 if (tab->window.current_line != NULL &&
1491 tab->window.current_line->parent->type == LINE_LINK)
1492 waddstr(minibuf, tab->window.current_line->parent->alt);
1495 if (in_minibuffer)
1496 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1499 static void
1500 redraw_tab(struct tab *tab)
1502 redraw_tabline();
1503 redraw_body(tab);
1504 redraw_modeline(tab);
1505 redraw_minibuffer();
1507 wrefresh(tabline);
1508 wrefresh(modeline);
1510 if (in_minibuffer) {
1511 wrefresh(body);
1512 wrefresh(minibuf);
1513 } else {
1514 wrefresh(minibuf);
1515 wrefresh(body);
1519 static void
1520 redraw_body(struct tab *tab)
1522 struct vline *vl;
1523 int line;
1525 werase(body);
1527 tab->window.line_off = MIN(tab->window.line_max-1, tab->window.line_off);
1528 if (TAILQ_EMPTY(&tab->window.head))
1529 return;
1531 line = 0;
1532 vl = nth_line(&tab->window, tab->window.line_off);
1533 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1534 wmove(body, line, 0);
1535 print_vline(vl);
1536 line++;
1537 if (line == body_lines)
1538 break;
1541 wmove(body, tab->window.curs_y, tab->window.curs_x);
1544 static void
1545 vmessage(const char *fmt, va_list ap)
1547 if (evtimer_pending(&clminibufev, NULL))
1548 evtimer_del(&clminibufev);
1549 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1550 evtimer_add(&clminibufev, &clminibufev_timer);
1552 free(ministate.curmesg);
1554 /* TODO: what to do if the allocation fails here? */
1555 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1556 ministate.curmesg = NULL;
1558 redraw_minibuffer();
1559 if (in_minibuffer) {
1560 wrefresh(body);
1561 wrefresh(minibuf);
1562 } else {
1563 wrefresh(minibuf);
1564 wrefresh(body);
1568 static void
1569 message(const char *fmt, ...)
1571 va_list ap;
1573 va_start(ap, fmt);
1574 vmessage(fmt, ap);
1575 va_end(ap);
1578 static void
1579 start_loading_anim(struct tab *tab)
1581 if (tab->loading_anim)
1582 return;
1583 tab->loading_anim = 1;
1584 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1585 evtimer_add(&tab->loadingev, &loadingev_timer);
1588 static void
1589 update_loading_anim(int fd, short ev, void *d)
1591 struct tab *tab = d;
1593 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1595 if (tab->flags & TAB_CURRENT) {
1596 redraw_modeline(tab);
1597 wrefresh(modeline);
1598 wrefresh(body);
1599 if (in_minibuffer)
1600 wrefresh(minibuf);
1603 evtimer_add(&tab->loadingev, &loadingev_timer);
1606 static void
1607 stop_loading_anim(struct tab *tab)
1609 if (!tab->loading_anim)
1610 return;
1611 evtimer_del(&tab->loadingev);
1612 tab->loading_anim = 0;
1613 tab->loading_anim_step = 0;
1615 if (!(tab->flags & TAB_CURRENT))
1616 return;
1618 redraw_modeline(tab);
1620 wrefresh(modeline);
1621 wrefresh(body);
1622 if (in_minibuffer)
1623 wrefresh(minibuf);
1626 static void
1627 load_url_in_tab(struct tab *tab, const char *url)
1629 empty_vlist(&tab->window);
1630 message("Loading %s...", url);
1631 start_loading_anim(tab);
1632 load_url(tab, url);
1634 tab->window.curs_x = 0;
1635 tab->window.curs_y = 0;
1636 redraw_tab(tab);
1639 static void
1640 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1641 void (*abortfn)(void), struct histhead *hist)
1643 in_minibuffer = 1;
1644 base_map = &minibuffer_map;
1645 current_map = &minibuffer_map;
1647 base_map->unhandled_input = self_insert_fn;
1649 ministate.donefn = donefn;
1650 ministate.abortfn = abortfn;
1651 memset(ministate.buf, 0, sizeof(ministate.buf));
1652 ministate.window.current_line = &ministate.vline;
1653 ministate.window.current_line->line = ministate.buf;
1654 ministate.window.cpoff = 0;
1655 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1657 ministate.history = hist;
1658 ministate.hist_cur = NULL;
1659 ministate.hist_off = 0;
1662 static void
1663 exit_minibuffer(void)
1665 werase(minibuf);
1667 in_minibuffer = 0;
1668 base_map = &global_map;
1669 current_map = &global_map;
1672 static void
1673 switch_to_tab(struct tab *tab)
1675 struct tab *t;
1677 TAILQ_FOREACH(t, &tabshead, tabs) {
1678 t->flags &= ~TAB_CURRENT;
1681 tab->flags |= TAB_CURRENT;
1684 static struct tab *
1685 new_tab(const char *url)
1687 struct tab *tab;
1689 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1690 event_loopbreak();
1691 return NULL;
1694 TAILQ_INIT(&tab->hist.head);
1696 TAILQ_INIT(&tab->window.head);
1698 tab->id = tab_counter++;
1699 switch_to_tab(tab);
1701 if (TAILQ_EMPTY(&tabshead))
1702 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1703 else
1704 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1706 load_url_in_tab(tab, url);
1707 return tab;
1710 static void
1711 usage(void)
1713 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1716 int
1717 ui_init(int argc, char * const *argv)
1719 const char *url = NEW_TAB_URL;
1720 int ch;
1722 while ((ch = getopt(argc, argv, "")) != -1) {
1723 switch (ch) {
1724 default:
1725 usage();
1726 return 0;
1729 argc -= optind;
1730 argv += optind;
1732 if (argc != 0)
1733 url = argv[0];
1735 setlocale(LC_ALL, "");
1737 TAILQ_INIT(&global_map.m);
1738 global_map.unhandled_input = global_key_unbound;
1740 TAILQ_INIT(&minibuffer_map.m);
1742 TAILQ_INIT(&eecmd_history.head);
1743 TAILQ_INIT(&ir_history.head);
1744 TAILQ_INIT(&lu_history.head);
1746 ministate.line.type = LINE_TEXT;
1747 ministate.vline.parent = &ministate.line;
1748 ministate.window.current_line = &ministate.vline;
1750 base_map = &global_map;
1751 current_map = &global_map;
1752 load_default_keys();
1754 initscr();
1755 raw();
1756 noecho();
1758 nonl();
1759 intrflush(stdscr, FALSE);
1761 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1762 return 0;
1763 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1764 return 0;
1765 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1766 return 0;
1767 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1768 return 0;
1770 body_lines = LINES-3;
1771 body_cols = COLS;
1773 keypad(body, TRUE);
1774 scrollok(body, TRUE);
1776 /* non-blocking input */
1777 wtimeout(body, 0);
1779 mvwprintw(body, 0, 0, "");
1781 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1782 event_add(&stdioev, NULL);
1784 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1785 signal_add(&winchev, NULL);
1787 new_tab(url);
1789 return 1;
1792 void
1793 ui_on_tab_loaded(struct tab *tab)
1795 stop_loading_anim(tab);
1796 message("Loaded %s", tab->hist_cur->h);
1798 redraw_tabline();
1799 wrefresh(tabline);
1800 if (in_minibuffer)
1801 wrefresh(minibuf);
1802 else
1803 wrefresh(body);
1806 void
1807 ui_on_tab_refresh(struct tab *tab)
1809 wrap_page(&tab->window);
1810 if (tab->flags & TAB_CURRENT) {
1811 restore_cursor(&tab->window);
1812 redraw_tab(tab);
1816 void
1817 ui_require_input(struct tab *tab, int hide)
1819 /* TODO: hard-switching to another tab is ugly */
1820 switch_to_tab(tab);
1822 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1823 &ir_history);
1824 strlcpy(ministate.prompt, "Input required: ",
1825 sizeof(ministate.prompt));
1826 redraw_tab(tab);
1829 void
1830 ui_notify(const char *fmt, ...)
1832 va_list ap;
1834 va_start(ap, fmt);
1835 vmessage(fmt, ap);
1836 va_end(ap);
1839 void
1840 ui_end(void)
1842 endwin();