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 tab*);
56 static void restore_cursor(struct tab *);
58 static void cmd_previous_line(struct tab*);
59 static void cmd_next_line(struct tab*);
60 static void cmd_backward_char(struct tab*);
61 static void cmd_forward_char(struct tab*);
62 static void cmd_backward_paragraph(struct tab*);
63 static void cmd_forward_paragraph(struct tab*);
64 static void cmd_move_beginning_of_line(struct tab*);
65 static void cmd_move_end_of_line(struct tab*);
66 static void cmd_redraw(struct tab*);
67 static void cmd_scroll_line_down(struct tab*);
68 static void cmd_scroll_line_up(struct tab*);
69 static void cmd_scroll_up(struct tab*);
70 static void cmd_scroll_down(struct tab*);
71 static void cmd_beginning_of_buffer(struct tab*);
72 static void cmd_end_of_buffer(struct tab*);
73 static void cmd_kill_telescope(struct tab*);
74 static void cmd_push_button(struct tab*);
75 static void cmd_push_button_new_tab(struct tab*);
76 static void cmd_previous_button(struct tab*);
77 static void cmd_next_button(struct tab*);
78 static void cmd_previous_page(struct tab*);
79 static void cmd_next_page(struct tab*);
80 static void cmd_clear_minibuf(struct tab*);
81 static void cmd_execute_extended_command(struct tab*);
82 static void cmd_tab_close(struct tab*);
83 static void cmd_tab_close_other(struct tab*);
84 static void cmd_tab_new(struct tab*);
85 static void cmd_tab_next(struct tab*);
86 static void cmd_tab_previous(struct tab*);
87 static void cmd_load_url(struct tab*);
88 static void cmd_load_current_url(struct tab*);
89 static void cmd_bookmark_page(struct tab*);
90 static void cmd_goto_bookmarks(struct tab*);
92 static void global_key_unbound(void);
94 static void cmd_mini_delete_char(struct tab*);
95 static void cmd_mini_delete_backward_char(struct tab*);
96 static void cmd_mini_forward_char(struct tab*);
97 static void cmd_mini_backward_char(struct tab*);
98 static void cmd_mini_move_end_of_line(struct tab*);
99 static void cmd_mini_move_beginning_of_line(struct tab*);
100 static void cmd_mini_kill_line(struct tab*);
101 static void cmd_mini_abort(struct tab*);
102 static void cmd_mini_complete_and_exit(struct tab*);
103 static void cmd_mini_previous_history_element(struct tab*);
104 static void cmd_mini_next_history_element(struct tab*);
106 static void minibuffer_hist_save_entry(void);
107 static void minibuffer_taint_hist(void);
108 static void minibuffer_self_insert(void);
109 static void eecmd_self_insert(void);
110 static void eecmd_select(void);
111 static void ir_self_insert(void);
112 static void ir_select(void);
113 static void lu_self_insert(void);
114 static void lu_select(void);
115 static void bp_select(void);
117 static struct vline *nth_line(struct tab*, size_t);
118 static struct tab *current_tab(void);
119 static int readkey(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 { short meta; int key; uint32_t cp; } 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 char *curs;
171 size_t cpoff;
172 char prompt[32];
173 void (*donefn)(void);
174 void (*abortfn)(void);
176 struct histhead *history;
177 struct hist *hist_cur;
178 size_t hist_off;
179 } ministate;
181 struct lineprefix {
182 const char *prfx1;
183 const char *prfx2;
184 } line_prefixes[] = {
185 [LINE_TEXT] = { "", "" },
186 [LINE_LINK] = { "=> ", " " },
187 [LINE_TITLE_1] = { "# ", " " },
188 [LINE_TITLE_2] = { "## ", " " },
189 [LINE_TITLE_3] = { "### ", " " },
190 [LINE_ITEM] = { "* ", " " },
191 [LINE_QUOTE] = { "> ", " " },
192 [LINE_PRE_START] = { "```", " " },
193 [LINE_PRE_CONTENT] = { "", "" },
194 [LINE_PRE_END] = { "```", "```" },
195 };
197 static struct line_face {
198 int prefix_prop;
199 int text_prop;
200 } line_faces[] = {
201 [LINE_TEXT] = { 0, 0 },
202 [LINE_LINK] = { 0, A_UNDERLINE },
203 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
204 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
205 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
206 [LINE_ITEM] = { 0, 0 },
207 [LINE_QUOTE] = { 0, A_DIM },
208 [LINE_PRE_START] = { 0, 0 },
209 [LINE_PRE_CONTENT] = { 0, 0 },
210 [LINE_PRE_END] = { 0, 0 },
211 };
213 static struct tab_face {
214 int background, tab, current_tab;
215 } tab_face = {
216 A_REVERSE, A_REVERSE, A_NORMAL
217 };
219 static void
220 empty_vlist(struct tab *tab)
222 struct vline *vl, *t;
224 tab->s.current_line = NULL;
225 tab->s.line_max = 0;
227 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
228 TAILQ_REMOVE(&tab->s.head, vl, vlines);
229 free(vl->line);
230 free(vl);
234 static inline void
235 global_set_key(const char *key, void (*fn)(struct tab*))
237 if (!kmap_define_key(&global_map, key, fn))
238 _exit(1);
241 static inline void
242 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
244 if (!kmap_define_key(&minibuffer_map, key, fn))
245 _exit(1);
248 static void
249 load_default_keys(void)
251 /* === global map === */
253 /* emacs */
254 global_set_key("C-p", cmd_previous_line);
255 global_set_key("C-n", cmd_next_line);
256 global_set_key("C-f", cmd_forward_char);
257 global_set_key("C-b", cmd_backward_char);
258 global_set_key("M-{", cmd_backward_paragraph);
259 global_set_key("M-}", cmd_forward_paragraph);
260 global_set_key("C-a", cmd_move_beginning_of_line);
261 global_set_key("C-e", cmd_move_end_of_line);
263 global_set_key("M-v", cmd_scroll_up);
264 global_set_key("C-v", cmd_scroll_down);
265 global_set_key("M-space", cmd_scroll_up);
266 global_set_key("space", cmd_scroll_down);
268 global_set_key("C-x C-c", cmd_kill_telescope);
270 global_set_key("C-g", cmd_clear_minibuf);
272 global_set_key("M-x", cmd_execute_extended_command);
273 global_set_key("C-x C-f", cmd_load_url);
274 global_set_key("C-x M-f", cmd_load_current_url);
276 global_set_key("C-x t 0", cmd_tab_close);
277 global_set_key("C-x t 1", cmd_tab_close_other);
278 global_set_key("C-x t 2", cmd_tab_new);
279 global_set_key("C-x t o", cmd_tab_next);
280 global_set_key("C-x t O", cmd_tab_previous);
282 global_set_key("M-<", cmd_beginning_of_buffer);
283 global_set_key("M->", cmd_end_of_buffer);
285 global_set_key("C-M-b", cmd_previous_page);
286 global_set_key("C-M-f", cmd_next_page);
288 global_set_key("<f7> a", cmd_bookmark_page);
289 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
291 /* vi/vi-like */
292 global_set_key("k", cmd_previous_line);
293 global_set_key("j", cmd_next_line);
294 global_set_key("l", cmd_forward_char);
295 global_set_key("h", cmd_backward_char);
296 global_set_key("{", cmd_backward_paragraph);
297 global_set_key("}", cmd_forward_paragraph);
298 global_set_key("^", cmd_move_beginning_of_line);
299 global_set_key("$", cmd_move_end_of_line);
301 global_set_key("K", cmd_scroll_line_up);
302 global_set_key("J", cmd_scroll_line_down);
304 global_set_key("g D", cmd_tab_close);
305 global_set_key("g N", cmd_tab_new);
306 global_set_key("g t", cmd_tab_next);
307 global_set_key("g T", cmd_tab_previous);
309 global_set_key("g g", cmd_beginning_of_buffer);
310 global_set_key("G", cmd_end_of_buffer);
312 global_set_key("H", cmd_previous_page);
313 global_set_key("L", cmd_next_page);
315 /* tmp */
316 global_set_key("q", cmd_kill_telescope);
318 global_set_key("esc", cmd_clear_minibuf);
320 global_set_key(":", cmd_execute_extended_command);
322 /* cua */
323 global_set_key("<up>", cmd_previous_line);
324 global_set_key("<down>", cmd_next_line);
325 global_set_key("<right>", cmd_forward_char);
326 global_set_key("<left>", cmd_backward_char);
327 global_set_key("<prior>", cmd_scroll_up);
328 global_set_key("<next>", cmd_scroll_down);
330 global_set_key("M-<left>", cmd_previous_page);
331 global_set_key("M-<right>", cmd_next_page);
333 /* "ncurses standard" */
334 global_set_key("C-l", cmd_redraw);
336 /* global */
337 global_set_key("C-m", cmd_push_button);
338 global_set_key("M-enter", cmd_push_button_new_tab);
339 global_set_key("M-tab", cmd_previous_button);
340 global_set_key("tab", cmd_next_button);
342 /* === minibuffer map === */
343 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
344 minibuffer_set_key("C-g", cmd_mini_abort);
345 minibuffer_set_key("esc", cmd_mini_abort);
346 minibuffer_set_key("C-d", cmd_mini_delete_char);
347 minibuffer_set_key("del", cmd_mini_delete_backward_char);
349 minibuffer_set_key("C-f", cmd_mini_forward_char);
350 minibuffer_set_key("C-b", cmd_mini_backward_char);
351 minibuffer_set_key("<right>", cmd_mini_forward_char);
352 minibuffer_set_key("<left>", cmd_mini_backward_char);
353 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
354 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
355 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
356 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
357 minibuffer_set_key("C-k", cmd_mini_kill_line);
359 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
360 minibuffer_set_key("M-n", cmd_mini_next_history_element);
361 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
362 minibuffer_set_key("<down>", cmd_mini_next_history_element);
365 static void
366 restore_cursor(struct tab *tab)
368 struct vline *vl;
369 const char *prfx;
371 vl =tab->s.current_line;
372 if (vl == NULL || vl->line == NULL)
373 tab->s.curs_x = tab->s.line_x = 0;
374 else
375 tab->s.curs_x = utf8_snwidth(vl->line, tab->s.line_x);
377 if (vl != NULL) {
378 prfx = line_prefixes[vl->parent->type].prfx1;
379 tab->s.curs_x += utf8_swidth(prfx);
383 static void
384 cmd_previous_line(struct tab *tab)
386 struct vline *vl;
388 if (tab->s.current_line == NULL
389 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
390 return;
392 if (--tab->s.curs_y < 0) {
393 tab->s.curs_y = 0;
394 cmd_scroll_line_up(tab);
395 return;
398 tab->s.current_line = vl;
399 restore_cursor(tab);
402 static void
403 cmd_next_line(struct tab *tab)
405 struct vline *vl;
407 if (tab->s.current_line == NULL
408 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
409 return;
411 if (++tab->s.curs_y > body_lines-1) {
412 tab->s.curs_y = body_lines-1;
413 cmd_scroll_line_down(tab);
414 return;
417 tab->s.current_line = vl;
418 restore_cursor(tab);
421 static void
422 cmd_backward_char(struct tab *tab)
424 if (tab->s.line_x != 0)
425 tab->s.line_x--;
426 restore_cursor(tab);
429 static void
430 cmd_forward_char(struct tab *tab)
432 tab->s.line_x++;
433 restore_cursor(tab);
436 static void
437 cmd_backward_paragraph(struct tab *tab)
439 do {
440 if (tab->s.current_line == NULL ||
441 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
442 message("No previous paragraph");
443 return;
445 cmd_previous_line(tab);
446 } while (tab->s.current_line->line != NULL ||
447 tab->s.current_line->parent->type != LINE_TEXT);
450 static void
451 cmd_forward_paragraph(struct tab *tab)
453 do {
454 if (tab->s.current_line == NULL ||
455 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
456 message("No next paragraph");
457 return;
459 cmd_next_line(tab);
460 } while (tab->s.current_line->line != NULL ||
461 tab->s.current_line->parent->type != LINE_TEXT);
464 static void
465 cmd_move_beginning_of_line(struct tab *tab)
467 tab->s.line_x = 0;
468 restore_cursor(tab);
471 static void
472 cmd_move_end_of_line(struct tab *tab)
474 struct vline *vl;
476 vl = tab->s.current_line;
477 if (vl->line == NULL)
478 return;
479 tab->s.line_x = utf8_cplen(vl->line);
480 restore_cursor(tab);
483 static void
484 cmd_redraw(struct tab *tab)
486 handle_resize(0, 0, NULL);
489 static void
490 cmd_scroll_line_up(struct tab *tab)
492 struct vline *vl;
494 if (tab->s.line_off == 0)
495 return;
497 vl = nth_line(tab, --tab->s.line_off);
498 wscrl(body, -1);
499 wmove(body, 0, 0);
500 print_vline(vl);
502 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
503 restore_cursor(tab);
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);
526 restore_cursor(tab);
529 static void
530 cmd_scroll_up(struct tab *tab)
532 size_t off;
534 off = body_lines+1;
536 for (; off > 0; --off)
537 cmd_scroll_line_up(tab);
540 static void
541 cmd_scroll_down(struct tab *tab)
543 size_t off;
545 off = body_lines+1;
547 for (; off > 0; --off)
548 cmd_scroll_line_down(tab);
551 static void
552 cmd_beginning_of_buffer(struct tab *tab)
554 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
555 tab->s.line_off = 0;
556 tab->s.curs_y = 0;
557 tab->s.line_x = 0;
558 restore_cursor(tab);
559 redraw_body(tab);
562 static void
563 cmd_end_of_buffer(struct tab *tab)
565 ssize_t off;
567 off = tab->s.line_max - body_lines;
568 off = MAX(0, off);
570 tab->s.line_off = off;
571 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
573 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
574 tab->s.line_x = body_cols;
575 restore_cursor(tab);
576 redraw_body(tab);
579 static void
580 cmd_kill_telescope(struct tab *tab)
582 event_loopbreak();
585 static void
586 cmd_push_button(struct tab *tab)
588 struct vline *vl;
589 size_t nth;
591 nth = tab->s.line_off + tab->s.curs_y;
592 if (nth >= tab->s.line_max)
593 return;
594 vl = nth_line(tab, nth);
595 if (vl->parent->type != LINE_LINK)
596 return;
598 load_url_in_tab(tab, vl->parent->alt);
601 static void
602 cmd_push_button_new_tab(struct tab *tab)
604 struct vline *vl;
605 size_t nth;
607 nth = tab->s.line_off + tab->s.curs_y;
608 if (nth > tab->s.line_max)
609 return;
610 vl = nth_line(tab, nth);
611 if (vl->parent->type != LINE_LINK)
612 return;
614 new_tab(vl->parent->alt);
617 static void
618 cmd_previous_button(struct tab *tab)
620 do {
621 if (tab->s.current_line == NULL ||
622 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
623 message("No previous link");
624 return;
626 cmd_previous_line(tab);
627 } while (tab->s.current_line->parent->type != LINE_LINK);
630 static void
631 cmd_next_button(struct tab *tab)
633 do {
634 if (tab->s.current_line == NULL ||
635 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
636 message("No next link");
637 return;
639 cmd_next_line(tab);
640 } while (tab->s.current_line->parent->type != LINE_LINK);
643 static void
644 cmd_previous_page(struct tab *tab)
646 if (!load_previous_page(tab))
647 message("No previous page");
648 else
649 start_loading_anim(tab);
652 static void
653 cmd_next_page(struct tab *tab)
655 if (!load_next_page(tab))
656 message("No next page");
657 else
658 start_loading_anim(tab);
661 static void
662 cmd_clear_minibuf(struct tab *tab)
664 handle_clear_minibuf(0, 0, NULL);
667 static void
668 cmd_execute_extended_command(struct tab *tab)
670 size_t len;
672 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
673 &eecmd_history);
675 len = sizeof(ministate.prompt);
676 strlcpy(ministate.prompt, "", len);
678 if (thiskey.meta)
679 strlcat(ministate.prompt, "M-", len);
681 strlcat(ministate.prompt, keyname(thiskey.key), len);
682 strlcat(ministate.prompt, " ", len);
685 static void
686 cmd_tab_close(struct tab *tab)
688 struct tab *t;
690 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
691 TAILQ_NEXT(tab, tabs) == NULL) {
692 message("Can't close the only tab.");
693 return;
696 if (evtimer_pending(&tab->s.loadingev, NULL))
697 evtimer_del(&tab->s.loadingev);
699 stop_tab(tab);
701 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
702 t = TAILQ_NEXT(tab, tabs);
703 TAILQ_REMOVE(&tabshead, tab, tabs);
704 free(tab);
706 switch_to_tab(t);
709 static void
710 cmd_tab_close_other(struct tab *tab)
712 struct tab *t, *i;
714 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
715 if (t->flags & TAB_CURRENT)
716 continue;
718 stop_tab(t);
719 TAILQ_REMOVE(&tabshead, t, tabs);
720 free(t);
724 static void
725 cmd_tab_new(struct tab *tab)
727 new_tab(NEW_TAB_URL);
730 static void
731 cmd_tab_next(struct tab *tab)
733 struct tab *t;
735 tab->flags &= ~TAB_CURRENT;
737 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
738 t = TAILQ_FIRST(&tabshead);
739 t->flags |= TAB_CURRENT;
742 static void
743 cmd_tab_previous(struct tab *tab)
745 struct tab *t;
747 tab->flags &= ~TAB_CURRENT;
749 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
750 t = TAILQ_LAST(&tabshead, tabshead);
751 t->flags |= TAB_CURRENT;
754 static void
755 cmd_load_url(struct tab *tab)
757 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
758 &lu_history);
759 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
762 static void
763 cmd_load_current_url(struct tab *tab)
765 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
766 &lu_history);
767 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
768 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
769 ministate.curs = strchr(ministate.buf, '\0');
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.curs = strchr(ministate.buf, '\0');
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 char *n;
798 minibuffer_taint_hist();
800 if (*(n = utf8_next_cp(ministate.curs)) == '\0')
801 return;
803 memmove(ministate.curs, n, strlen(n)+1);
806 static void
807 cmd_mini_delete_backward_char(struct tab *tab)
809 char *p;
811 minibuffer_taint_hist();
813 if ((p = utf8_prev_cp(ministate.curs, ministate.buf)) == ministate.buf)
814 return;
816 memmove(p, ministate.curs, strlen(ministate.curs)+1);
819 static void
820 cmd_mini_forward_char(struct tab *tab)
822 if (*ministate.curs == '\0')
823 return;
824 ministate.curs = utf8_next_cp(ministate.curs);
825 ministate.cpoff++;
828 static void
829 cmd_mini_backward_char(struct tab *tab)
831 if (ministate.cpoff == 0)
832 return;
833 ministate.cpoff--;
834 ministate.curs = utf8_prev_cp(ministate.curs-1, ministate.buf);
837 static void
838 cmd_mini_move_end_of_line(struct tab *tab)
840 ministate.curs = strchr(ministate.buf, '\0');
841 ministate.cpoff = utf8_cplen(ministate.buf);
844 static void
845 cmd_mini_move_beginning_of_line(struct tab *tab)
847 ministate.curs = ministate.buf;
848 ministate.cpoff = 0;
851 static void
852 cmd_mini_kill_line(struct tab *tab)
854 minibuffer_taint_hist();
855 *ministate.curs = '\0';
858 static void
859 cmd_mini_abort(struct tab *tab)
861 ministate.abortfn();
864 static void
865 cmd_mini_complete_and_exit(struct tab *tab)
867 minibuffer_taint_hist();
868 ministate.donefn();
871 static void
872 cmd_mini_previous_history_element(struct tab *tab)
874 if (ministate.history == NULL) {
875 message("No history");
876 return;
879 if (ministate.hist_cur == NULL ||
880 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
881 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
882 ministate.hist_off = ministate.history->len - 1;
883 if (ministate.hist_cur == NULL)
884 message("No prev item");
885 } else {
886 ministate.hist_off--;
889 if (ministate.hist_cur != NULL)
890 ministate.curs = ministate.hist_cur->h;
893 static void
894 cmd_mini_next_history_element(struct tab *tab)
896 if (ministate.history == NULL) {
897 message("No history");
898 return;
901 if (ministate.hist_cur == NULL ||
902 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
903 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
904 ministate.hist_off = 0;
905 if (ministate.hist_cur == NULL)
906 message("No next item");
907 } else {
908 ministate.hist_off++;
911 if (ministate.hist_cur != NULL)
912 ministate.curs = ministate.hist_cur->h;
915 static void
916 minibuffer_hist_save_entry(void)
918 struct hist *hist;
920 if (ministate.history == NULL)
921 return;
923 if ((hist = calloc(1, sizeof(*hist))) == NULL)
924 abort();
926 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
928 if (TAILQ_EMPTY(&ministate.history->head))
929 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
930 else
931 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
932 ministate.history->len++;
935 /*
936 * taint the minibuffer cache: if we're currently showing a history
937 * element, copy that to the current buf and reset the "history
938 * navigation" thing.
939 */
940 static void
941 minibuffer_taint_hist(void)
943 if (ministate.hist_cur == NULL)
944 return;
946 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
947 ministate.hist_cur = NULL;
950 static void
951 minibuffer_self_insert(void)
953 char tmp[5] = {0};
954 size_t len;
956 minibuffer_taint_hist();
958 if (thiskey.cp == 0)
959 return;
961 len = utf8_encode(thiskey.cp, tmp);
962 if (ministate.curs + len > ministate.buf + sizeof(ministate.buf) - 1)
963 return;
965 memmove(ministate.curs + len, ministate.curs, strlen(ministate.curs)+1);
966 memcpy(ministate.curs, tmp, len);
967 ministate.curs = utf8_next_cp(ministate.curs);
968 ministate.cpoff++;
971 static void
972 eecmd_self_insert(void)
974 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
975 !unicode_isgraph(thiskey.cp)) {
976 global_key_unbound();
977 return;
980 minibuffer_self_insert();
983 static void
984 eecmd_select(void)
986 exit_minibuffer();
987 minibuffer_hist_save_entry();
988 message("TODO: try to execute %s", ministate.buf);
991 static void
992 ir_self_insert(void)
994 minibuffer_self_insert();
997 static void
998 ir_select(void)
1000 char buf[1025] = {0};
1001 struct url url;
1002 struct tab *tab;
1004 tab = current_tab();
1006 exit_minibuffer();
1007 minibuffer_hist_save_entry();
1009 /* a bit ugly but... */
1010 memcpy(&url, &tab->url, sizeof(tab->url));
1011 url_set_query(&url, ministate.buf);
1012 url_unparse(&url, buf, sizeof(buf));
1013 load_url_in_tab(tab, buf);
1016 static void
1017 lu_self_insert(void)
1019 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1020 !unicode_isgraph(thiskey.key)) {
1021 global_key_unbound();
1022 return;
1025 minibuffer_self_insert();
1028 static void
1029 lu_select(void)
1031 exit_minibuffer();
1032 minibuffer_hist_save_entry();
1033 load_url_in_tab(current_tab(), ministate.buf);
1036 static void
1037 bp_select(void)
1039 exit_minibuffer();
1040 if (*ministate.buf != '\0')
1041 add_to_bookmarks(ministate.buf);
1042 else
1043 message("Abort.");
1046 static struct vline *
1047 nth_line(struct tab *tab, size_t n)
1049 struct vline *vl;
1050 size_t i;
1052 i = 0;
1053 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1054 if (i == n)
1055 return vl;
1056 i++;
1059 /* unreachable */
1060 abort();
1063 static struct tab *
1064 current_tab(void)
1066 struct tab *t;
1068 TAILQ_FOREACH(t, &tabshead, tabs) {
1069 if (t->flags & TAB_CURRENT)
1070 return t;
1073 /* unreachable */
1074 abort();
1077 static int
1078 readkey(void)
1080 uint32_t state = 0;
1082 if ((thiskey.key = wgetch(body)) == ERR)
1083 return 0;
1085 thiskey.meta = thiskey.key == 27;
1086 if (thiskey.meta) {
1087 thiskey.key = wgetch(body);
1088 if (thiskey.key == ERR || thiskey.key == 27) {
1089 thiskey.meta = 0;
1090 thiskey.key = 27;
1094 thiskey.cp = 0;
1095 if ((unsigned int)thiskey.key < UINT8_MAX) {
1096 while (1) {
1097 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1098 break;
1099 if ((thiskey.key = wgetch(body)) == ERR) {
1100 message("Error decoding user input");
1101 return 0;
1106 return 1;
1109 static void
1110 dispatch_stdio(int fd, short ev, void *d)
1112 struct keymap *k;
1113 const char *keyname;
1114 char tmp[5] = {0};
1116 if (!readkey())
1117 return;
1119 if (keybuf[0] != '\0')
1120 strlcat(keybuf, " ", sizeof(keybuf));
1121 if (thiskey.meta)
1122 strlcat(keybuf, "M-", sizeof(keybuf));
1123 if (thiskey.cp != 0) {
1124 utf8_encode(thiskey.cp, tmp);
1125 strlcat(keybuf, tmp, sizeof(keybuf));
1126 } else {
1127 if ((keyname = unkbd(thiskey.key)) != NULL)
1128 strlcat(keybuf, keyname, sizeof(keybuf));
1129 else {
1130 tmp[0] = thiskey.key;
1131 strlcat(keybuf, tmp, sizeof(keybuf));
1135 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1136 if (k->meta == thiskey.meta &&
1137 k->key == thiskey.key) {
1138 if (k->fn == NULL)
1139 current_map = &k->map;
1140 else {
1141 current_map = base_map;
1142 strlcpy(keybuf, "", sizeof(keybuf));
1143 k->fn(current_tab());
1145 goto done;
1149 if (current_map->unhandled_input != NULL)
1150 current_map->unhandled_input();
1151 else {
1152 global_key_unbound();
1155 strlcpy(keybuf, "", sizeof(keybuf));
1156 current_map = base_map;
1158 done:
1159 redraw_tab(current_tab());
1162 static void
1163 handle_clear_minibuf(int fd, short ev, void *d)
1165 free(ministate.curmesg);
1166 ministate.curmesg = NULL;
1168 redraw_minibuffer();
1169 if (in_minibuffer) {
1170 wrefresh(body);
1171 wrefresh(minibuf);
1172 } else {
1173 wrefresh(minibuf);
1174 wrefresh(body);
1178 static void
1179 handle_resize(int sig, short ev, void *d)
1181 struct tab *tab;
1183 endwin();
1184 refresh();
1185 clear();
1187 /* move and resize the windows, in reverse order! */
1189 mvwin(minibuf, LINES-1, 0);
1190 wresize(minibuf, 1, COLS);
1192 mvwin(modeline, LINES-2, 0);
1193 wresize(modeline, 1, COLS);
1195 wresize(body, LINES-3, COLS);
1196 body_lines = LINES-3;
1197 body_cols = COLS;
1199 wresize(tabline, 1, COLS);
1201 tab = current_tab();
1203 wrap_page(tab);
1204 redraw_tab(tab);
1207 static int
1208 wrap_page(struct tab *tab)
1210 struct line *l;
1211 const struct line *orig;
1212 struct vline *vl;
1213 const char *prfx;
1215 orig = tab->s.current_line == NULL
1216 ? NULL
1217 : tab->s.current_line->parent;
1218 tab->s.current_line = NULL;
1220 tab->s.curs_y = 0;
1221 tab->s.line_off = 0;
1223 empty_vlist(tab);
1225 TAILQ_FOREACH(l, &tab->page.head, lines) {
1226 prfx = line_prefixes[l->type].prfx1;
1227 switch (l->type) {
1228 case LINE_TEXT:
1229 case LINE_LINK:
1230 case LINE_TITLE_1:
1231 case LINE_TITLE_2:
1232 case LINE_TITLE_3:
1233 case LINE_ITEM:
1234 case LINE_QUOTE:
1235 case LINE_PRE_START:
1236 case LINE_PRE_END:
1237 wrap_text(tab, prfx, l, body_cols);
1238 break;
1239 case LINE_PRE_CONTENT:
1240 hardwrap_text(tab, l, body_cols);
1241 break;
1244 if (orig == l && tab->s.current_line == NULL) {
1245 tab->s.line_off = tab->s.line_max-1;
1246 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1248 while (1) {
1249 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1250 if (vl == NULL || vl->parent != orig)
1251 break;
1252 tab->s.current_line = vl;
1253 tab->s.line_off--;
1258 if (tab->s.current_line == NULL)
1259 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1261 return 1;
1264 static void
1265 print_vline(struct vline *vl)
1267 const char *text = vl->line;
1268 const char *prfx;
1269 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1270 int text_face = line_faces[vl->parent->type].text_prop;
1272 if (!vl->flags)
1273 prfx = line_prefixes[vl->parent->type].prfx1;
1274 else
1275 prfx = line_prefixes[vl->parent->type].prfx2;
1277 if (text == NULL)
1278 text = "";
1280 wattron(body, prefix_face);
1281 wprintw(body, "%s", prfx);
1282 wattroff(body, prefix_face);
1284 wattron(body, text_face);
1285 wprintw(body, "%s", text);
1286 wattroff(body, text_face);
1289 static void
1290 redraw_tabline(void)
1292 struct tab *tab;
1293 size_t toskip;
1294 int current, x, y, truncated;
1295 const char *title;
1296 char buf[25];
1298 toskip = 0;
1299 x = 1;
1300 TAILQ_FOREACH(tab, &tabshead, tabs) {
1301 x += sizeof(buf) + 1;
1302 toskip++;
1303 if (tab->flags & TAB_CURRENT)
1304 break;
1306 if (x < COLS-2)
1307 toskip = 0;
1308 else
1309 toskip--;
1311 werase(tabline);
1312 wattron(tabline, tab_face.background);
1313 wprintw(tabline, toskip == 0 ? " " : "<");
1314 wattroff(tabline, tab_face.background);
1316 truncated = 0;
1317 TAILQ_FOREACH(tab, &tabshead, tabs) {
1318 if (truncated)
1319 break;
1320 if (toskip != 0) {
1321 toskip--;
1322 continue;
1325 getyx(tabline, y, x);
1326 if (x + sizeof(buf)+2 >= (size_t)COLS)
1327 truncated = 1;
1329 current = tab->flags & TAB_CURRENT;
1331 if (*(title = tab->page.title) == '\0')
1332 title = tab->hist_cur->h;
1334 strlcpy(buf, " ", sizeof(buf));
1335 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1336 /* truncation happens */
1337 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1338 } else {
1339 /* pad with spaces */
1340 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1341 /* nop */ ;
1344 if (current)
1345 wattron(tabline, tab_face.current_tab);
1346 else
1347 wattron(tabline, tab_face.tab);
1349 wprintw(tabline, "%s", buf);
1350 if (TAILQ_NEXT(tab, tabs) != NULL)
1351 wprintw(tabline, " ");
1353 if (current)
1354 wattroff(tabline, tab_face.current_tab);
1355 else
1356 wattroff(tabline, tab_face.tab);
1359 wattron(tabline, tab_face.background);
1360 for (; x < COLS; ++x)
1361 waddch(tabline, ' ');
1362 if (truncated)
1363 mvwprintw(tabline, 0, COLS-1, ">");
1366 static inline char
1367 trust_status_char(enum trust_state ts)
1369 switch (ts) {
1370 case TS_UNKNOWN: return 'u';
1371 case TS_UNTRUSTED: return '!';
1372 case TS_TRUSTED: return 'v';
1373 case TS_VERIFIED: return 'V';
1377 static void
1378 redraw_modeline(struct tab *tab)
1380 double pct;
1381 int x, y, max_x, max_y;
1382 const char *mode = tab->page.name;
1383 const char *spin = "-\\|/";
1385 werase(modeline);
1386 wattron(modeline, A_REVERSE);
1387 wmove(modeline, 0, 0);
1389 wprintw(modeline, "-%c%c %s ",
1390 spin[tab->s.loading_anim_step],
1391 trust_status_char(tab->trust),
1392 mode == NULL ? "(none)" : mode);
1394 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1396 if (tab->s.line_max <= (size_t)body_lines)
1397 wprintw(modeline, "All ");
1398 else if (tab->s.line_off == 0)
1399 wprintw(modeline, "Top ");
1400 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1401 wprintw(modeline, "Bottom ");
1402 else
1403 wprintw(modeline, "%.0f%% ", pct);
1405 wprintw(modeline, "%d/%d %s ",
1406 tab->s.line_off + tab->s.curs_y,
1407 tab->s.line_max,
1408 tab->hist_cur->h);
1410 getyx(modeline, y, x);
1411 getmaxyx(modeline, max_y, max_x);
1413 (void)y;
1414 (void)max_y;
1416 for (; x < max_x; ++x)
1417 waddstr(modeline, "-");
1420 static void
1421 redraw_minibuffer(void)
1423 struct tab *tab;
1424 size_t off_y, off_x = 0;
1425 char *start;
1427 werase(minibuf);
1429 if (in_minibuffer) {
1430 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1431 if (ministate.hist_cur != NULL)
1432 wprintw(minibuf, "(%zu/%zu) ",
1433 ministate.hist_off + 1,
1434 ministate.history->len);
1436 getyx(minibuf, off_y, off_x);
1438 start = ministate.hist_cur != NULL
1439 ? ministate.hist_cur->h
1440 : ministate.buf;
1441 while (utf8_swidth_between(start, ministate.curs) > (size_t)COLS/2) {
1442 start = utf8_next_cp(start);
1445 waddstr(minibuf, start);
1448 if (ministate.curmesg != NULL)
1449 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1450 ministate.curmesg);
1452 if (!in_minibuffer && ministate.curmesg == NULL)
1453 waddstr(minibuf, keybuf);
1455 /* If nothing else, show the URL at point */
1456 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1457 tab = current_tab();
1458 if (tab->s.current_line != NULL &&
1459 tab->s.current_line->parent->type == LINE_LINK)
1460 waddstr(minibuf, tab->s.current_line->parent->alt);
1463 if (in_minibuffer)
1464 wmove(minibuf, 0, off_x + utf8_swidth_between(start, ministate.curs));
1467 static void
1468 redraw_tab(struct tab *tab)
1470 redraw_tabline();
1471 redraw_body(tab);
1472 redraw_modeline(tab);
1473 redraw_minibuffer();
1475 wrefresh(tabline);
1476 wrefresh(modeline);
1478 if (in_minibuffer) {
1479 wrefresh(body);
1480 wrefresh(minibuf);
1481 } else {
1482 wrefresh(minibuf);
1483 wrefresh(body);
1487 static void
1488 redraw_body(struct tab *tab)
1490 struct vline *vl;
1491 int line;
1493 werase(body);
1495 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1496 if (TAILQ_EMPTY(&tab->s.head))
1497 return;
1499 line = 0;
1500 vl = nth_line(tab, tab->s.line_off);
1501 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1502 wmove(body, line, 0);
1503 print_vline(vl);
1504 line++;
1505 if (line == body_lines)
1506 break;
1509 wmove(body, tab->s.curs_y, tab->s.curs_x);
1512 static void
1513 vmessage(const char *fmt, va_list ap)
1515 if (evtimer_pending(&clminibufev, NULL))
1516 evtimer_del(&clminibufev);
1517 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1518 evtimer_add(&clminibufev, &clminibufev_timer);
1520 free(ministate.curmesg);
1522 /* TODO: what to do if the allocation fails here? */
1523 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1524 ministate.curmesg = NULL;
1526 redraw_minibuffer();
1527 if (in_minibuffer) {
1528 wrefresh(body);
1529 wrefresh(minibuf);
1530 } else {
1531 wrefresh(minibuf);
1532 wrefresh(body);
1536 static void
1537 message(const char *fmt, ...)
1539 va_list ap;
1541 va_start(ap, fmt);
1542 vmessage(fmt, ap);
1543 va_end(ap);
1546 static void
1547 start_loading_anim(struct tab *tab)
1549 if (tab->s.loading_anim)
1550 return;
1551 tab->s.loading_anim = 1;
1552 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1553 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1556 static void
1557 update_loading_anim(int fd, short ev, void *d)
1559 struct tab *tab = d;
1561 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1563 if (tab->flags & TAB_CURRENT) {
1564 redraw_modeline(tab);
1565 wrefresh(modeline);
1566 wrefresh(body);
1567 if (in_minibuffer)
1568 wrefresh(minibuf);
1571 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1574 static void
1575 stop_loading_anim(struct tab *tab)
1577 if (!tab->s.loading_anim)
1578 return;
1579 evtimer_del(&tab->s.loadingev);
1580 tab->s.loading_anim = 0;
1581 tab->s.loading_anim_step = 0;
1583 if (!(tab->flags & TAB_CURRENT))
1584 return;
1586 redraw_modeline(tab);
1588 wrefresh(modeline);
1589 wrefresh(body);
1590 if (in_minibuffer)
1591 wrefresh(minibuf);
1594 static void
1595 load_url_in_tab(struct tab *tab, const char *url)
1597 empty_vlist(tab);
1598 message("Loading %s...", url);
1599 start_loading_anim(tab);
1600 load_url(tab, url);
1602 tab->s.curs_x = 0;
1603 tab->s.curs_y = 0;
1604 redraw_tab(tab);
1607 static void
1608 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1609 void (*abortfn)(void), struct histhead *hist)
1611 in_minibuffer = 1;
1612 base_map = &minibuffer_map;
1613 current_map = &minibuffer_map;
1615 base_map->unhandled_input = self_insert_fn;
1617 ministate.donefn = donefn;
1618 ministate.abortfn = abortfn;
1619 memset(ministate.buf, 0, sizeof(ministate.buf));
1620 ministate.curs = ministate.buf;
1621 ministate.cpoff = 0;
1622 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1624 ministate.history = hist;
1625 ministate.hist_cur = NULL;
1626 ministate.hist_off = 0;
1629 static void
1630 exit_minibuffer(void)
1632 werase(minibuf);
1634 in_minibuffer = 0;
1635 base_map = &global_map;
1636 current_map = &global_map;
1639 static void
1640 switch_to_tab(struct tab *tab)
1642 struct tab *t;
1644 TAILQ_FOREACH(t, &tabshead, tabs) {
1645 t->flags &= ~TAB_CURRENT;
1648 tab->flags |= TAB_CURRENT;
1651 static struct tab *
1652 new_tab(const char *url)
1654 struct tab *tab;
1656 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1657 event_loopbreak();
1658 return NULL;
1661 TAILQ_INIT(&tab->hist.head);
1663 TAILQ_INIT(&tab->s.head);
1665 tab->id = tab_counter++;
1666 switch_to_tab(tab);
1668 if (TAILQ_EMPTY(&tabshead))
1669 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1670 else
1671 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1673 load_url_in_tab(tab, url);
1674 return tab;
1677 static void
1678 usage(void)
1680 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1683 int
1684 ui_init(int argc, char * const *argv)
1686 const char *url = NEW_TAB_URL;
1687 int ch;
1689 while ((ch = getopt(argc, argv, "")) != -1) {
1690 switch (ch) {
1691 default:
1692 usage();
1693 return 0;
1696 argc -= optind;
1697 argv += optind;
1699 if (argc != 0)
1700 url = argv[0];
1702 setlocale(LC_ALL, "");
1704 TAILQ_INIT(&global_map.m);
1705 global_map.unhandled_input = global_key_unbound;
1707 TAILQ_INIT(&minibuffer_map.m);
1709 TAILQ_INIT(&eecmd_history.head);
1710 TAILQ_INIT(&ir_history.head);
1711 TAILQ_INIT(&lu_history.head);
1713 base_map = &global_map;
1714 current_map = &global_map;
1715 load_default_keys();
1717 initscr();
1718 raw();
1719 noecho();
1721 nonl();
1722 intrflush(stdscr, FALSE);
1724 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1725 return 0;
1726 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1727 return 0;
1728 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1729 return 0;
1730 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1731 return 0;
1733 body_lines = LINES-3;
1734 body_cols = COLS;
1736 keypad(body, TRUE);
1737 scrollok(body, TRUE);
1739 /* non-blocking input */
1740 wtimeout(body, 0);
1742 mvwprintw(body, 0, 0, "");
1744 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1745 event_add(&stdioev, NULL);
1747 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1748 signal_add(&winchev, NULL);
1750 new_tab(url);
1752 return 1;
1755 void
1756 ui_on_tab_loaded(struct tab *tab)
1758 stop_loading_anim(tab);
1759 message("Loaded %s", tab->hist_cur->h);
1761 redraw_tabline();
1762 wrefresh(tabline);
1763 if (in_minibuffer)
1764 wrefresh(minibuf);
1765 else
1766 wrefresh(body);
1769 void
1770 ui_on_tab_refresh(struct tab *tab)
1772 wrap_page(tab);
1773 if (tab->flags & TAB_CURRENT)
1774 redraw_tab(tab);
1777 void
1778 ui_require_input(struct tab *tab, int hide)
1780 /* TODO: hard-switching to another tab is ugly */
1781 switch_to_tab(tab);
1783 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1784 &ir_history);
1785 strlcpy(ministate.prompt, "Input required: ",
1786 sizeof(ministate.prompt));
1787 redraw_tab(tab);
1790 void
1791 ui_notify(const char *fmt, ...)
1793 va_list ap;
1795 va_start(ap, fmt);
1796 vmessage(fmt, ap);
1797 va_end(ap);
1800 void
1801 ui_end(void)
1803 endwin();