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 #define CMD(fnname) static void fnname(struct window *)
60 CMD(cmd_previous_line);
61 CMD(cmd_next_line);
62 CMD(cmd_backward_char);
63 CMD(cmd_forward_char);
64 CMD(cmd_backward_paragraph);
65 CMD(cmd_forward_paragraph);
66 CMD(cmd_move_beginning_of_line);
67 CMD(cmd_move_end_of_line);
68 CMD(cmd_redraw);
69 CMD(cmd_scroll_line_down);
70 CMD(cmd_scroll_line_up);
71 CMD(cmd_scroll_up);
72 CMD(cmd_scroll_down);
73 CMD(cmd_beginning_of_buffer);
74 CMD(cmd_end_of_buffer);
75 CMD(cmd_kill_telescope);
76 CMD(cmd_push_button);
77 CMD(cmd_push_button_new_tab);
78 CMD(cmd_previous_button);
79 CMD(cmd_next_button);
80 CMD(cmd_previous_page);
81 CMD(cmd_next_page);
82 CMD(cmd_clear_minibuf);
83 CMD(cmd_execute_extended_command);
84 CMD(cmd_tab_close);
85 CMD(cmd_tab_close_other);
86 CMD(cmd_tab_new);
87 CMD(cmd_tab_next);
88 CMD(cmd_tab_previous);
89 CMD(cmd_load_url);
90 CMD(cmd_load_current_url);
91 CMD(cmd_bookmark_page);
92 CMD(cmd_goto_bookmarks);
94 CMD(cmd_mini_delete_char);
95 CMD(cmd_mini_delete_backward_char);
96 CMD(cmd_mini_kill_line);
97 CMD(cmd_mini_abort);
98 CMD(cmd_mini_complete_and_exit);
99 CMD(cmd_mini_previous_history_element);
100 CMD(cmd_mini_next_history_element);
102 #include "cmd.gen.h"
104 static void global_key_unbound(void);
105 static void minibuffer_hist_save_entry(void);
106 static void minibuffer_taint_hist(void);
107 static void minibuffer_self_insert(void);
108 static void eecmd_self_insert(void);
109 static void eecmd_select(void);
110 static void ir_self_insert(void);
111 static void ir_select(void);
112 static void lu_self_insert(void);
113 static void lu_select(void);
114 static void bp_select(void);
115 static void yornp_self_insert(void);
116 static void yornp_abort(void);
118 static struct vline *nth_line(struct window*, size_t);
119 static struct tab *current_tab(void);
120 static struct window *current_window(void);
121 static int readkey(void);
122 static void dispatch_stdio(int, short, void*);
123 static void handle_clear_minibuf(int, short, void*);
124 static void handle_resize(int, short, void*);
125 static int wrap_page(struct window*);
126 static void print_vline(struct vline*);
127 static void redraw_tabline(void);
128 static void redraw_body(struct tab*);
129 static void redraw_modeline(struct tab*);
130 static void redraw_minibuffer(void);
131 static void redraw_tab(struct tab*);
132 static void vmessage(const char*, va_list);
133 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
134 static void start_loading_anim(struct tab*);
135 static void update_loading_anim(int, short, void*);
136 static void stop_loading_anim(struct tab*);
137 static void load_url_in_tab(struct tab*, const char*);
138 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
139 static void exit_minibuffer(void);
140 static void switch_to_tab(struct tab*);
141 static struct tab *new_tab(const char*);
142 static void usage(void);
144 static struct { short meta; int key; uint32_t cp; } thiskey;
146 static WINDOW *tabline, *body, *modeline, *minibuf;
147 static int body_lines, body_cols;
149 static struct event clminibufev;
150 static struct timeval clminibufev_timer = { 5, 0 };
151 static struct timeval loadingev_timer = { 0, 250000 };
153 static uint32_t tab_counter;
155 static char keybuf[64];
157 static void (*yornp_cb)(int, unsigned int);
159 struct kmap global_map,
160 minibuffer_map,
161 *current_map,
162 *base_map;
164 static struct histhead eecmd_history,
165 ir_history,
166 lu_history;
168 static int in_minibuffer;
170 static struct {
171 char *curmesg;
173 char prompt[64];
174 void (*donefn)(void);
175 void (*abortfn)(void);
177 char buf[1025];
178 struct line line;
179 struct vline vline;
180 struct window window;
182 struct histhead *history;
183 struct hist *hist_cur;
184 size_t hist_off;
185 } ministate;
187 struct lineprefix {
188 const char *prfx1;
189 const char *prfx2;
190 } line_prefixes[] = {
191 [LINE_TEXT] = { "", "" },
192 [LINE_LINK] = { "=> ", " " },
193 [LINE_TITLE_1] = { "# ", " " },
194 [LINE_TITLE_2] = { "## ", " " },
195 [LINE_TITLE_3] = { "### ", " " },
196 [LINE_ITEM] = { "* ", " " },
197 [LINE_QUOTE] = { "> ", " " },
198 [LINE_PRE_START] = { "```", " " },
199 [LINE_PRE_CONTENT] = { "", "" },
200 [LINE_PRE_END] = { "```", "```" },
201 };
203 static struct line_face {
204 int prefix_prop;
205 int text_prop;
206 } line_faces[] = {
207 [LINE_TEXT] = { 0, 0 },
208 [LINE_LINK] = { 0, A_UNDERLINE },
209 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
210 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
211 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
212 [LINE_ITEM] = { 0, 0 },
213 [LINE_QUOTE] = { 0, A_DIM },
214 [LINE_PRE_START] = { 0, 0 },
215 [LINE_PRE_CONTENT] = { 0, 0 },
216 [LINE_PRE_END] = { 0, 0 },
217 };
219 static struct tab_face {
220 int background, tab, current_tab;
221 } tab_face = {
222 A_REVERSE, A_REVERSE, A_NORMAL
223 };
225 static void
226 empty_vlist(struct window *window)
228 struct vline *vl, *t;
230 window->current_line = NULL;
231 window->line_max = 0;
233 TAILQ_FOREACH_SAFE(vl, &window->head, vlines, t) {
234 TAILQ_REMOVE(&window->head, vl, vlines);
235 free(vl->line);
236 free(vl);
240 static inline void
241 global_set_key(const char *key, void (*fn)(struct window*))
243 if (!kmap_define_key(&global_map, key, fn))
244 _exit(1);
247 static inline void
248 minibuffer_set_key(const char *key, void (*fn)(struct window*))
250 if (!kmap_define_key(&minibuffer_map, key, fn))
251 _exit(1);
254 static void
255 load_default_keys(void)
257 /* === global map === */
259 /* emacs */
260 global_set_key("C-p", cmd_previous_line);
261 global_set_key("C-n", cmd_next_line);
262 global_set_key("C-f", cmd_forward_char);
263 global_set_key("C-b", cmd_backward_char);
264 global_set_key("M-{", cmd_backward_paragraph);
265 global_set_key("M-}", cmd_forward_paragraph);
266 global_set_key("C-a", cmd_move_beginning_of_line);
267 global_set_key("C-e", cmd_move_end_of_line);
269 global_set_key("M-v", cmd_scroll_up);
270 global_set_key("C-v", cmd_scroll_down);
271 global_set_key("M-space", cmd_scroll_up);
272 global_set_key("space", cmd_scroll_down);
274 global_set_key("C-x C-c", cmd_kill_telescope);
276 global_set_key("C-g", cmd_clear_minibuf);
278 global_set_key("M-x", cmd_execute_extended_command);
279 global_set_key("C-x C-f", cmd_load_url);
280 global_set_key("C-x M-f", cmd_load_current_url);
282 global_set_key("C-x t 0", cmd_tab_close);
283 global_set_key("C-x t 1", cmd_tab_close_other);
284 global_set_key("C-x t 2", cmd_tab_new);
285 global_set_key("C-x t o", cmd_tab_next);
286 global_set_key("C-x t O", cmd_tab_previous);
288 global_set_key("M-<", cmd_beginning_of_buffer);
289 global_set_key("M->", cmd_end_of_buffer);
291 global_set_key("C-M-b", cmd_previous_page);
292 global_set_key("C-M-f", cmd_next_page);
294 global_set_key("<f7> a", cmd_bookmark_page);
295 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
297 /* vi/vi-like */
298 global_set_key("k", cmd_previous_line);
299 global_set_key("j", cmd_next_line);
300 global_set_key("l", cmd_forward_char);
301 global_set_key("h", cmd_backward_char);
302 global_set_key("{", cmd_backward_paragraph);
303 global_set_key("}", cmd_forward_paragraph);
304 global_set_key("^", cmd_move_beginning_of_line);
305 global_set_key("$", cmd_move_end_of_line);
307 global_set_key("K", cmd_scroll_line_up);
308 global_set_key("J", cmd_scroll_line_down);
310 global_set_key("g D", cmd_tab_close);
311 global_set_key("g N", cmd_tab_new);
312 global_set_key("g t", cmd_tab_next);
313 global_set_key("g T", cmd_tab_previous);
315 global_set_key("g g", cmd_beginning_of_buffer);
316 global_set_key("G", cmd_end_of_buffer);
318 global_set_key("H", cmd_previous_page);
319 global_set_key("L", cmd_next_page);
321 /* tmp */
322 global_set_key("q", cmd_kill_telescope);
324 global_set_key("esc", cmd_clear_minibuf);
326 global_set_key(":", cmd_execute_extended_command);
328 /* cua */
329 global_set_key("<up>", cmd_previous_line);
330 global_set_key("<down>", cmd_next_line);
331 global_set_key("<right>", cmd_forward_char);
332 global_set_key("<left>", cmd_backward_char);
333 global_set_key("<prior>", cmd_scroll_up);
334 global_set_key("<next>", cmd_scroll_down);
336 global_set_key("M-<left>", cmd_previous_page);
337 global_set_key("M-<right>", cmd_next_page);
339 /* "ncurses standard" */
340 global_set_key("C-l", cmd_redraw);
342 /* global */
343 global_set_key("C-m", cmd_push_button);
344 global_set_key("M-enter", cmd_push_button_new_tab);
345 global_set_key("M-tab", cmd_previous_button);
346 global_set_key("tab", cmd_next_button);
348 /* === minibuffer map === */
349 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
350 minibuffer_set_key("C-g", cmd_mini_abort);
351 minibuffer_set_key("esc", cmd_mini_abort);
352 minibuffer_set_key("C-d", cmd_mini_delete_char);
353 minibuffer_set_key("del", cmd_mini_delete_backward_char);
354 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
355 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
357 minibuffer_set_key("C-b", cmd_backward_char);
358 minibuffer_set_key("C-f", cmd_forward_char);
359 minibuffer_set_key("<left>", cmd_backward_char);
360 minibuffer_set_key("<right>", cmd_forward_char);
361 minibuffer_set_key("C-e", cmd_move_end_of_line);
362 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
363 minibuffer_set_key("<end>", cmd_move_end_of_line);
364 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
365 minibuffer_set_key("C-k", cmd_mini_kill_line);
367 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
368 minibuffer_set_key("M-n", cmd_mini_next_history_element);
369 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
370 minibuffer_set_key("<down>", cmd_mini_next_history_element);
373 static void
374 restore_cursor(struct window *window)
376 struct vline *vl;
377 const char *prfx;
379 vl = window->current_line;
380 if (vl == NULL || vl->line == NULL)
381 window->curs_x = window->cpoff = 0;
382 else
383 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
385 if (vl != NULL) {
386 prfx = line_prefixes[vl->parent->type].prfx1;
387 window->curs_x += utf8_swidth(prfx);
391 static void
392 cmd_previous_line(struct window *window)
394 struct vline *vl;
396 if (window->current_line == NULL
397 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
398 return;
400 if (--window->curs_y < 0) {
401 window->curs_y = 0;
402 cmd_scroll_line_up(window);
403 return;
406 window->current_line = vl;
407 restore_cursor(window);
410 static void
411 cmd_next_line(struct window *window)
413 struct vline *vl;
415 if (window->current_line == NULL
416 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
417 return;
419 if (++window->curs_y > body_lines-1) {
420 window->curs_y = body_lines-1;
421 cmd_scroll_line_down(window);
422 return;
425 window->current_line = vl;
426 restore_cursor(window);
429 static void
430 cmd_backward_char(struct window *window)
432 if (window->cpoff != 0)
433 window->cpoff--;
434 restore_cursor(window);
437 static void
438 cmd_forward_char(struct window *window)
440 window->cpoff++;
441 restore_cursor(window);
444 static void
445 cmd_backward_paragraph(struct window *window)
447 do {
448 if (window->current_line == NULL ||
449 window->current_line == TAILQ_FIRST(&window->head)) {
450 message("No previous paragraph");
451 return;
453 cmd_previous_line(window);
454 } while (window->current_line->line != NULL ||
455 window->current_line->parent->type != LINE_TEXT);
458 static void
459 cmd_forward_paragraph(struct window *window)
461 do {
462 if (window->current_line == NULL ||
463 window->current_line == TAILQ_LAST(&window->head, vhead)) {
464 message("No next paragraph");
465 return;
467 cmd_next_line(window);
468 } while (window->current_line->line != NULL ||
469 window->current_line->parent->type != LINE_TEXT);
472 static void
473 cmd_move_beginning_of_line(struct window *window)
475 window->cpoff = 0;
476 restore_cursor(window);
479 static void
480 cmd_move_end_of_line(struct window *window)
482 struct vline *vl;
484 vl = window->current_line;
485 if (vl->line == NULL)
486 return;
487 window->cpoff = utf8_cplen(vl->line);
488 restore_cursor(window);
491 static void
492 cmd_redraw(struct window *window)
494 handle_resize(0, 0, NULL);
497 static void
498 cmd_scroll_line_up(struct window *window)
500 struct vline *vl;
502 if (window->line_off == 0)
503 return;
505 vl = nth_line(window, --window->line_off);
506 wscrl(body, -1);
507 wmove(body, 0, 0);
508 print_vline(vl);
510 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
511 restore_cursor(window);
514 static void
515 cmd_scroll_line_down(struct window *window)
517 struct vline *vl;
519 vl = window->current_line;
520 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
521 return;
522 window->current_line = vl;
524 window->line_off++;
525 wscrl(body, 1);
527 if (window->line_max - window->line_off < (size_t)body_lines)
528 return;
530 vl = nth_line(window, window->line_off + body_lines-1);
531 wmove(body, body_lines-1, 0);
532 print_vline(vl);
534 restore_cursor(window);
537 static void
538 cmd_scroll_up(struct window *window)
540 size_t off;
542 off = body_lines+1;
544 for (; off > 0; --off)
545 cmd_scroll_line_up(window);
548 static void
549 cmd_scroll_down(struct window *window)
551 size_t off;
553 off = body_lines+1;
555 for (; off > 0; --off)
556 cmd_scroll_line_down(window);
559 static void
560 cmd_beginning_of_buffer(struct window *window)
562 window->current_line = TAILQ_FIRST(&window->head);
563 window->line_off = 0;
564 window->curs_y = 0;
565 window->cpoff = 0;
566 restore_cursor(window);
569 static void
570 cmd_end_of_buffer(struct window *window)
572 ssize_t off;
574 off = window->line_max - body_lines;
575 off = MAX(0, off);
577 window->line_off = off;
578 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
580 window->current_line = TAILQ_LAST(&window->head, vhead);
581 window->cpoff = body_cols;
582 restore_cursor(window);
585 static void
586 cmd_kill_telescope(struct window *window)
588 event_loopbreak();
591 static void
592 cmd_push_button(struct window *window)
594 struct vline *vl;
595 size_t nth;
597 nth = window->line_off + window->curs_y;
598 if (nth >= window->line_max)
599 return;
600 vl = nth_line(window, nth);
601 if (vl->parent->type != LINE_LINK)
602 return;
604 load_url_in_tab(current_tab(), vl->parent->alt);
607 static void
608 cmd_push_button_new_tab(struct window *window)
610 struct vline *vl;
611 size_t nth;
613 nth = window->line_off + window->curs_y;
614 if (nth > window->line_max)
615 return;
616 vl = nth_line(window, nth);
617 if (vl->parent->type != LINE_LINK)
618 return;
620 new_tab(vl->parent->alt);
623 static void
624 cmd_previous_button(struct window *window)
626 do {
627 if (window->current_line == NULL ||
628 window->current_line == TAILQ_FIRST(&window->head)) {
629 message("No previous link");
630 return;
632 cmd_previous_line(window);
633 } while (window->current_line->parent->type != LINE_LINK);
636 static void
637 cmd_next_button(struct window *window)
639 do {
640 if (window->current_line == NULL ||
641 window->current_line == TAILQ_LAST(&window->head, vhead)) {
642 message("No next link");
643 return;
645 cmd_next_line(window);
646 } while (window->current_line->parent->type != LINE_LINK);
649 static void
650 cmd_previous_page(struct window *window)
652 struct tab *tab = current_tab();
654 if (!load_previous_page(tab))
655 message("No previous page");
656 else
657 start_loading_anim(tab);
660 static void
661 cmd_next_page(struct window *window)
663 struct tab *tab = current_tab();
665 if (!load_next_page(tab))
666 message("No next page");
667 else
668 start_loading_anim(tab);
671 static void
672 cmd_clear_minibuf(struct window *window)
674 handle_clear_minibuf(0, 0, NULL);
677 static void
678 cmd_execute_extended_command(struct window *window)
680 size_t len;
682 if (in_minibuffer) {
683 message("We don't have enable-recursive-minibuffers");
684 return;
687 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
688 &eecmd_history);
690 len = sizeof(ministate.prompt);
691 strlcpy(ministate.prompt, "", len);
693 if (thiskey.meta)
694 strlcat(ministate.prompt, "M-", len);
696 strlcat(ministate.prompt, keyname(thiskey.key), len);
697 strlcat(ministate.prompt, " ", len);
700 static void
701 cmd_tab_close(struct window *window)
703 struct tab *tab, *t;
705 tab = current_tab();
706 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
707 TAILQ_NEXT(tab, tabs) == NULL) {
708 message("Can't close the only tab.");
709 return;
712 if (evtimer_pending(&tab->loadingev, NULL))
713 evtimer_del(&tab->loadingev);
715 stop_tab(tab);
717 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
718 t = TAILQ_NEXT(tab, tabs);
719 TAILQ_REMOVE(&tabshead, tab, tabs);
720 free(tab);
722 switch_to_tab(t);
725 static void
726 cmd_tab_close_other(struct window *window)
728 struct tab *t, *i;
730 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
731 if (t->flags & TAB_CURRENT)
732 continue;
734 stop_tab(t);
735 TAILQ_REMOVE(&tabshead, t, tabs);
736 free(t);
740 static void
741 cmd_tab_new(struct window *window)
743 new_tab(NEW_TAB_URL);
746 static void
747 cmd_tab_next(struct window *window)
749 struct tab *tab, *t;
751 tab = current_tab();
752 tab->flags &= ~TAB_CURRENT;
754 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
755 t = TAILQ_FIRST(&tabshead);
756 t->flags |= TAB_CURRENT;
759 static void
760 cmd_tab_previous(struct window *window)
762 struct tab *tab, *t;
764 tab = current_tab();
765 tab->flags &= ~TAB_CURRENT;
767 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
768 t = TAILQ_LAST(&tabshead, tabshead);
769 t->flags |= TAB_CURRENT;
772 static void
773 cmd_load_url(struct window *window)
775 if (in_minibuffer) {
776 message("We don't have enable-recursive-minibuffers");
777 return;
780 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
781 &lu_history);
782 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
785 static void
786 cmd_load_current_url(struct window *window)
788 struct tab *tab = current_tab();
790 if (in_minibuffer) {
791 message("We don't have enable-recursive-minibuffers");
792 return;
795 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
796 &lu_history);
797 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
798 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
799 ministate.window.cpoff = utf8_cplen(ministate.buf);
802 static void
803 cmd_bookmark_page(struct window *window)
805 struct tab *tab = current_tab();
807 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
808 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
809 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
810 ministate.window.cpoff = utf8_cplen(ministate.buf);
813 static void
814 cmd_goto_bookmarks(struct window *window)
816 load_url_in_tab(current_tab(), "about:bookmarks");
819 static void
820 cmd_mini_delete_char(struct window *window)
822 char *c, *n;
824 if (!in_minibuffer) {
825 message("text is read-only");
826 return;
829 minibuffer_taint_hist();
831 c = utf8_nth(window->current_line->line, window->cpoff);
832 if (*c == '\0')
833 return;
834 n = utf8_next_cp(c);
836 memmove(c, n, strlen(n)+1);
839 static void
840 cmd_mini_delete_backward_char(struct window *window)
842 char *c, *p, *start;
844 if (!in_minibuffer) {
845 message("text is read-only");
846 return;
849 minibuffer_taint_hist();
851 c = utf8_nth(window->current_line->line, window->cpoff);
852 start = window->current_line->line;
853 if (c == start)
854 return;
855 p = utf8_prev_cp(c-1, start);
857 memmove(p, c, strlen(c)+1);
858 window->cpoff--;
861 static void
862 cmd_mini_kill_line(struct window *window)
864 char *c;
866 if (!in_minibuffer) {
867 message("text is read-only");
868 return;
871 minibuffer_taint_hist();
872 c = utf8_nth(window->current_line->line, window->cpoff);
873 *c = '\0';
876 static void
877 cmd_mini_abort(struct window *window)
879 if (!in_minibuffer)
880 return;
882 ministate.abortfn();
885 static void
886 cmd_mini_complete_and_exit(struct window *window)
888 if (!in_minibuffer)
889 return;
891 minibuffer_taint_hist();
892 ministate.donefn();
895 static void
896 cmd_mini_previous_history_element(struct window *window)
898 if (ministate.history == NULL) {
899 message("No history");
900 return;
903 if (ministate.hist_cur == NULL ||
904 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
905 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
906 ministate.hist_off = ministate.history->len - 1;
907 if (ministate.hist_cur == NULL)
908 message("No prev item");
909 } else {
910 ministate.hist_off--;
913 if (ministate.hist_cur != NULL)
914 window->current_line->line = ministate.hist_cur->h;
917 static void
918 cmd_mini_next_history_element(struct window *window)
920 if (ministate.history == NULL) {
921 message("No history");
922 return;
925 if (ministate.hist_cur == NULL ||
926 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
927 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
928 ministate.hist_off = 0;
929 if (ministate.hist_cur == NULL)
930 message("No next item");
931 } else {
932 ministate.hist_off++;
935 if (ministate.hist_cur != NULL)
936 window->current_line->line = ministate.hist_cur->h;
939 static void
940 global_key_unbound(void)
942 message("%s is undefined", keybuf);
945 static void
946 minibuffer_hist_save_entry(void)
948 struct hist *hist;
950 if (ministate.history == NULL)
951 return;
953 if ((hist = calloc(1, sizeof(*hist))) == NULL)
954 abort();
956 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
958 if (TAILQ_EMPTY(&ministate.history->head))
959 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
960 else
961 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
962 ministate.history->len++;
965 /*
966 * taint the minibuffer cache: if we're currently showing a history
967 * element, copy that to the current buf and reset the "history
968 * navigation" thing.
969 */
970 static void
971 minibuffer_taint_hist(void)
973 if (ministate.hist_cur == NULL)
974 return;
976 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
977 ministate.hist_cur = NULL;
980 static void
981 minibuffer_self_insert(void)
983 char *c, tmp[5] = {0};
984 size_t len;
986 minibuffer_taint_hist();
988 if (thiskey.cp == 0)
989 return;
991 len = utf8_encode(thiskey.cp, tmp);
992 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
993 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
994 return;
996 memmove(c + len, c, strlen(c)+1);
997 memcpy(c, tmp, len);
998 ministate.window.cpoff++;
1001 static void
1002 eecmd_self_insert(void)
1004 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1005 !unicode_isgraph(thiskey.cp)) {
1006 global_key_unbound();
1007 return;
1010 minibuffer_self_insert();
1013 static void
1014 eecmd_select(void)
1016 struct cmds *cmd;
1018 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1019 if (!strcmp(cmd->cmd, ministate.buf)) {
1020 exit_minibuffer();
1021 minibuffer_hist_save_entry();
1022 cmd->fn(current_window());
1023 return;
1027 message("No match");
1030 static void
1031 ir_self_insert(void)
1033 minibuffer_self_insert();
1036 static void
1037 ir_select(void)
1039 char buf[1025] = {0};
1040 struct url url;
1041 struct tab *tab;
1043 tab = current_tab();
1045 exit_minibuffer();
1046 minibuffer_hist_save_entry();
1048 /* a bit ugly but... */
1049 memcpy(&url, &tab->url, sizeof(tab->url));
1050 url_set_query(&url, ministate.buf);
1051 url_unparse(&url, buf, sizeof(buf));
1052 load_url_in_tab(tab, buf);
1055 static void
1056 lu_self_insert(void)
1058 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1059 !unicode_isgraph(thiskey.key)) {
1060 global_key_unbound();
1061 return;
1064 minibuffer_self_insert();
1067 static void
1068 lu_select(void)
1070 exit_minibuffer();
1071 minibuffer_hist_save_entry();
1072 load_url_in_tab(current_tab(), ministate.buf);
1075 static void
1076 bp_select(void)
1078 exit_minibuffer();
1079 if (*ministate.buf != '\0')
1080 add_to_bookmarks(ministate.buf);
1081 else
1082 message("Abort.");
1085 static void
1086 yornp_self_insert(void)
1088 if (thiskey.key != 'y' && thiskey.key != 'n') {
1089 message("Please answer y or n");
1090 return;
1093 yornp_cb(thiskey.key == 'y', current_tab()->id);
1094 exit_minibuffer();
1097 static void
1098 yornp_abort(void)
1100 yornp_cb(0, current_tab()->id);
1101 exit_minibuffer();
1104 static struct vline *
1105 nth_line(struct window *window, size_t n)
1107 struct vline *vl;
1108 size_t i;
1110 i = 0;
1111 TAILQ_FOREACH(vl, &window->head, vlines) {
1112 if (i == n)
1113 return vl;
1114 i++;
1117 /* unreachable */
1118 abort();
1121 static struct tab *
1122 current_tab(void)
1124 struct tab *t;
1126 TAILQ_FOREACH(t, &tabshead, tabs) {
1127 if (t->flags & TAB_CURRENT)
1128 return t;
1131 /* unreachable */
1132 abort();
1135 static struct window *
1136 current_window(void)
1138 if (in_minibuffer)
1139 return &ministate.window;
1140 return &current_tab()->window;
1143 static int
1144 readkey(void)
1146 uint32_t state = 0;
1148 if ((thiskey.key = wgetch(body)) == ERR)
1149 return 0;
1151 thiskey.meta = thiskey.key == 27;
1152 if (thiskey.meta) {
1153 thiskey.key = wgetch(body);
1154 if (thiskey.key == ERR || thiskey.key == 27) {
1155 thiskey.meta = 0;
1156 thiskey.key = 27;
1160 thiskey.cp = 0;
1161 if ((unsigned int)thiskey.key < UINT8_MAX) {
1162 while (1) {
1163 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1164 break;
1165 if ((thiskey.key = wgetch(body)) == ERR) {
1166 message("Error decoding user input");
1167 return 0;
1172 return 1;
1175 static void
1176 dispatch_stdio(int fd, short ev, void *d)
1178 struct keymap *k;
1179 const char *keyname;
1180 char tmp[5] = {0};
1182 if (!readkey())
1183 return;
1185 if (keybuf[0] != '\0')
1186 strlcat(keybuf, " ", sizeof(keybuf));
1187 if (thiskey.meta)
1188 strlcat(keybuf, "M-", sizeof(keybuf));
1189 if (thiskey.cp != 0) {
1190 utf8_encode(thiskey.cp, tmp);
1191 strlcat(keybuf, tmp, sizeof(keybuf));
1192 } else {
1193 if ((keyname = unkbd(thiskey.key)) != NULL)
1194 strlcat(keybuf, keyname, sizeof(keybuf));
1195 else {
1196 tmp[0] = thiskey.key;
1197 strlcat(keybuf, tmp, sizeof(keybuf));
1201 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1202 if (k->meta == thiskey.meta &&
1203 k->key == thiskey.key) {
1204 if (k->fn == NULL)
1205 current_map = &k->map;
1206 else {
1207 current_map = base_map;
1208 strlcpy(keybuf, "", sizeof(keybuf));
1209 k->fn(current_window());
1211 goto done;
1215 if (current_map->unhandled_input != NULL)
1216 current_map->unhandled_input();
1217 else {
1218 global_key_unbound();
1221 strlcpy(keybuf, "", sizeof(keybuf));
1222 current_map = base_map;
1224 done:
1225 redraw_tab(current_tab());
1228 static void
1229 handle_clear_minibuf(int fd, short ev, void *d)
1231 free(ministate.curmesg);
1232 ministate.curmesg = NULL;
1234 redraw_minibuffer();
1235 if (in_minibuffer) {
1236 wrefresh(body);
1237 wrefresh(minibuf);
1238 } else {
1239 wrefresh(minibuf);
1240 wrefresh(body);
1244 static void
1245 handle_resize(int sig, short ev, void *d)
1247 struct tab *tab;
1249 endwin();
1250 refresh();
1251 clear();
1253 /* move and resize the windows, in reverse order! */
1255 mvwin(minibuf, LINES-1, 0);
1256 wresize(minibuf, 1, COLS);
1258 mvwin(modeline, LINES-2, 0);
1259 wresize(modeline, 1, COLS);
1261 wresize(body, LINES-3, COLS);
1262 body_lines = LINES-3;
1263 body_cols = COLS;
1265 wresize(tabline, 1, COLS);
1267 tab = current_tab();
1269 wrap_page(&tab->window);
1270 redraw_tab(tab);
1273 static int
1274 wrap_page(struct window *window)
1276 struct line *l;
1277 const struct line *orig;
1278 struct vline *vl;
1279 const char *prfx;
1281 orig = window->current_line == NULL
1282 ? NULL
1283 : window->current_line->parent;
1284 window->current_line = NULL;
1286 window->curs_y = 0;
1287 window->line_off = 0;
1289 empty_vlist(window);
1291 TAILQ_FOREACH(l, &window->page.head, lines) {
1292 prfx = line_prefixes[l->type].prfx1;
1293 switch (l->type) {
1294 case LINE_TEXT:
1295 case LINE_LINK:
1296 case LINE_TITLE_1:
1297 case LINE_TITLE_2:
1298 case LINE_TITLE_3:
1299 case LINE_ITEM:
1300 case LINE_QUOTE:
1301 case LINE_PRE_START:
1302 case LINE_PRE_END:
1303 wrap_text(window, prfx, l, body_cols);
1304 break;
1305 case LINE_PRE_CONTENT:
1306 hardwrap_text(window, l, body_cols);
1307 break;
1310 if (orig == l && window->current_line == NULL) {
1311 window->line_off = window->line_max-1;
1312 window->current_line = TAILQ_LAST(&window->head, vhead);
1314 while (1) {
1315 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1316 if (vl == NULL || vl->parent != orig)
1317 break;
1318 window->current_line = vl;
1319 window->line_off--;
1324 if (window->current_line == NULL)
1325 window->current_line = TAILQ_FIRST(&window->head);
1327 return 1;
1330 static void
1331 print_vline(struct vline *vl)
1333 const char *text = vl->line;
1334 const char *prfx;
1335 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1336 int text_face = line_faces[vl->parent->type].text_prop;
1338 if (!vl->flags)
1339 prfx = line_prefixes[vl->parent->type].prfx1;
1340 else
1341 prfx = line_prefixes[vl->parent->type].prfx2;
1343 if (text == NULL)
1344 text = "";
1346 wattron(body, prefix_face);
1347 wprintw(body, "%s", prfx);
1348 wattroff(body, prefix_face);
1350 wattron(body, text_face);
1351 wprintw(body, "%s", text);
1352 wattroff(body, text_face);
1355 static void
1356 redraw_tabline(void)
1358 struct tab *tab;
1359 size_t toskip;
1360 int current, x, y, truncated;
1361 const char *title;
1362 char buf[25];
1364 toskip = 0;
1365 x = 1;
1366 TAILQ_FOREACH(tab, &tabshead, tabs) {
1367 x += sizeof(buf) + 1;
1368 toskip++;
1369 if (tab->flags & TAB_CURRENT)
1370 break;
1372 if (x < COLS-2)
1373 toskip = 0;
1374 else
1375 toskip--;
1377 werase(tabline);
1378 wattron(tabline, tab_face.background);
1379 wprintw(tabline, toskip == 0 ? " " : "<");
1380 wattroff(tabline, tab_face.background);
1382 truncated = 0;
1383 TAILQ_FOREACH(tab, &tabshead, tabs) {
1384 if (truncated)
1385 break;
1386 if (toskip != 0) {
1387 toskip--;
1388 continue;
1391 getyx(tabline, y, x);
1392 if (x + sizeof(buf)+2 >= (size_t)COLS)
1393 truncated = 1;
1395 current = tab->flags & TAB_CURRENT;
1397 if (*(title = tab->window.page.title) == '\0')
1398 title = tab->hist_cur->h;
1400 strlcpy(buf, " ", sizeof(buf));
1401 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1402 /* truncation happens */
1403 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1404 } else {
1405 /* pad with spaces */
1406 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1407 /* nop */ ;
1410 if (current)
1411 wattron(tabline, tab_face.current_tab);
1412 else
1413 wattron(tabline, tab_face.tab);
1415 wprintw(tabline, "%s", buf);
1416 if (TAILQ_NEXT(tab, tabs) != NULL)
1417 wprintw(tabline, " ");
1419 if (current)
1420 wattroff(tabline, tab_face.current_tab);
1421 else
1422 wattroff(tabline, tab_face.tab);
1425 wattron(tabline, tab_face.background);
1426 for (; x < COLS; ++x)
1427 waddch(tabline, ' ');
1428 if (truncated)
1429 mvwprintw(tabline, 0, COLS-1, ">");
1432 static inline char
1433 trust_status_char(enum trust_state ts)
1435 switch (ts) {
1436 case TS_UNKNOWN: return 'u';
1437 case TS_UNTRUSTED: return '!';
1438 case TS_TRUSTED: return 'v';
1439 case TS_VERIFIED: return 'V';
1443 static void
1444 redraw_modeline(struct tab *tab)
1446 double pct;
1447 int x, y, max_x, max_y;
1448 const char *mode = tab->window.page.name;
1449 const char *spin = "-\\|/";
1451 werase(modeline);
1452 wattron(modeline, A_REVERSE);
1453 wmove(modeline, 0, 0);
1455 wprintw(modeline, "-%c%c %s ",
1456 spin[tab->loading_anim_step],
1457 trust_status_char(tab->trust),
1458 mode == NULL ? "(none)" : mode);
1460 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1462 if (tab->window.line_max <= (size_t)body_lines)
1463 wprintw(modeline, "All ");
1464 else if (tab->window.line_off == 0)
1465 wprintw(modeline, "Top ");
1466 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1467 wprintw(modeline, "Bottom ");
1468 else
1469 wprintw(modeline, "%.0f%% ", pct);
1471 wprintw(modeline, "%d/%d %s ",
1472 tab->window.line_off + tab->window.curs_y,
1473 tab->window.line_max,
1474 tab->hist_cur->h);
1476 getyx(modeline, y, x);
1477 getmaxyx(modeline, max_y, max_x);
1479 (void)y;
1480 (void)max_y;
1482 for (; x < max_x; ++x)
1483 waddstr(modeline, "-");
1486 static void
1487 redraw_minibuffer(void)
1489 struct tab *tab;
1490 size_t off_y, off_x = 0;
1491 char *start, *c;
1493 werase(minibuf);
1495 if (in_minibuffer) {
1496 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1497 if (ministate.hist_cur != NULL)
1498 wprintw(minibuf, "(%zu/%zu) ",
1499 ministate.hist_off + 1,
1500 ministate.history->len);
1502 getyx(minibuf, off_y, off_x);
1504 start = ministate.hist_cur != NULL
1505 ? ministate.hist_cur->h
1506 : ministate.buf;
1507 c = utf8_nth(ministate.window.current_line->line,
1508 ministate.window.cpoff);
1509 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1510 start = utf8_next_cp(start);
1513 waddstr(minibuf, start);
1516 if (ministate.curmesg != NULL)
1517 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1518 ministate.curmesg);
1520 if (!in_minibuffer && ministate.curmesg == NULL)
1521 waddstr(minibuf, keybuf);
1523 /* If nothing else, show the URL at point */
1524 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1525 tab = current_tab();
1526 if (tab->window.current_line != NULL &&
1527 tab->window.current_line->parent->type == LINE_LINK)
1528 waddstr(minibuf, tab->window.current_line->parent->alt);
1531 if (in_minibuffer)
1532 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1535 static void
1536 redraw_tab(struct tab *tab)
1538 redraw_tabline();
1539 redraw_body(tab);
1540 redraw_modeline(tab);
1541 redraw_minibuffer();
1543 wrefresh(tabline);
1544 wrefresh(modeline);
1546 if (in_minibuffer) {
1547 wrefresh(body);
1548 wrefresh(minibuf);
1549 } else {
1550 wrefresh(minibuf);
1551 wrefresh(body);
1555 static void
1556 redraw_body(struct tab *tab)
1558 struct vline *vl;
1559 int line;
1561 werase(body);
1563 tab->window.line_off = MIN(tab->window.line_max-1, tab->window.line_off);
1564 if (TAILQ_EMPTY(&tab->window.head))
1565 return;
1567 line = 0;
1568 vl = nth_line(&tab->window, tab->window.line_off);
1569 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1570 wmove(body, line, 0);
1571 print_vline(vl);
1572 line++;
1573 if (line == body_lines)
1574 break;
1577 wmove(body, tab->window.curs_y, tab->window.curs_x);
1580 static void
1581 vmessage(const char *fmt, va_list ap)
1583 if (evtimer_pending(&clminibufev, NULL))
1584 evtimer_del(&clminibufev);
1585 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1586 evtimer_add(&clminibufev, &clminibufev_timer);
1588 free(ministate.curmesg);
1590 /* TODO: what to do if the allocation fails here? */
1591 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1592 ministate.curmesg = NULL;
1594 redraw_minibuffer();
1595 if (in_minibuffer) {
1596 wrefresh(body);
1597 wrefresh(minibuf);
1598 } else {
1599 wrefresh(minibuf);
1600 wrefresh(body);
1604 static void
1605 message(const char *fmt, ...)
1607 va_list ap;
1609 va_start(ap, fmt);
1610 vmessage(fmt, ap);
1611 va_end(ap);
1614 static void
1615 start_loading_anim(struct tab *tab)
1617 if (tab->loading_anim)
1618 return;
1619 tab->loading_anim = 1;
1620 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1621 evtimer_add(&tab->loadingev, &loadingev_timer);
1624 static void
1625 update_loading_anim(int fd, short ev, void *d)
1627 struct tab *tab = d;
1629 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1631 if (tab->flags & TAB_CURRENT) {
1632 redraw_modeline(tab);
1633 wrefresh(modeline);
1634 wrefresh(body);
1635 if (in_minibuffer)
1636 wrefresh(minibuf);
1639 evtimer_add(&tab->loadingev, &loadingev_timer);
1642 static void
1643 stop_loading_anim(struct tab *tab)
1645 if (!tab->loading_anim)
1646 return;
1647 evtimer_del(&tab->loadingev);
1648 tab->loading_anim = 0;
1649 tab->loading_anim_step = 0;
1651 if (!(tab->flags & TAB_CURRENT))
1652 return;
1654 redraw_modeline(tab);
1656 wrefresh(modeline);
1657 wrefresh(body);
1658 if (in_minibuffer)
1659 wrefresh(minibuf);
1662 static void
1663 load_url_in_tab(struct tab *tab, const char *url)
1665 empty_vlist(&tab->window);
1666 message("Loading %s...", url);
1667 start_loading_anim(tab);
1668 load_url(tab, url);
1670 tab->window.curs_x = 0;
1671 tab->window.curs_y = 0;
1672 redraw_tab(tab);
1675 static void
1676 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1677 void (*abortfn)(void), struct histhead *hist)
1679 in_minibuffer = 1;
1680 base_map = &minibuffer_map;
1681 current_map = &minibuffer_map;
1683 base_map->unhandled_input = self_insert_fn;
1685 ministate.donefn = donefn;
1686 ministate.abortfn = abortfn;
1687 memset(ministate.buf, 0, sizeof(ministate.buf));
1688 ministate.window.current_line = &ministate.vline;
1689 ministate.window.current_line->line = ministate.buf;
1690 ministate.window.cpoff = 0;
1691 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1693 ministate.history = hist;
1694 ministate.hist_cur = NULL;
1695 ministate.hist_off = 0;
1698 static void
1699 exit_minibuffer(void)
1701 werase(minibuf);
1703 in_minibuffer = 0;
1704 base_map = &global_map;
1705 current_map = &global_map;
1708 static void
1709 switch_to_tab(struct tab *tab)
1711 struct tab *t;
1713 TAILQ_FOREACH(t, &tabshead, tabs) {
1714 t->flags &= ~TAB_CURRENT;
1717 tab->flags |= TAB_CURRENT;
1720 static struct tab *
1721 new_tab(const char *url)
1723 struct tab *tab;
1725 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1726 event_loopbreak();
1727 return NULL;
1730 TAILQ_INIT(&tab->hist.head);
1732 TAILQ_INIT(&tab->window.head);
1734 tab->id = tab_counter++;
1735 switch_to_tab(tab);
1737 if (TAILQ_EMPTY(&tabshead))
1738 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1739 else
1740 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1742 load_url_in_tab(tab, url);
1743 return tab;
1746 static void
1747 usage(void)
1749 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1752 int
1753 ui_init(int argc, char * const *argv)
1755 const char *url = NEW_TAB_URL;
1756 int ch;
1758 while ((ch = getopt(argc, argv, "")) != -1) {
1759 switch (ch) {
1760 default:
1761 usage();
1762 return 0;
1765 argc -= optind;
1766 argv += optind;
1768 if (argc != 0)
1769 url = argv[0];
1771 setlocale(LC_ALL, "");
1773 TAILQ_INIT(&global_map.m);
1774 global_map.unhandled_input = global_key_unbound;
1776 TAILQ_INIT(&minibuffer_map.m);
1778 TAILQ_INIT(&eecmd_history.head);
1779 TAILQ_INIT(&ir_history.head);
1780 TAILQ_INIT(&lu_history.head);
1782 ministate.line.type = LINE_TEXT;
1783 ministate.vline.parent = &ministate.line;
1784 ministate.window.current_line = &ministate.vline;
1786 base_map = &global_map;
1787 current_map = &global_map;
1788 load_default_keys();
1790 initscr();
1791 raw();
1792 noecho();
1794 nonl();
1795 intrflush(stdscr, FALSE);
1797 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1798 return 0;
1799 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1800 return 0;
1801 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1802 return 0;
1803 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1804 return 0;
1806 body_lines = LINES-3;
1807 body_cols = COLS;
1809 keypad(body, TRUE);
1810 scrollok(body, TRUE);
1812 /* non-blocking input */
1813 wtimeout(body, 0);
1815 mvwprintw(body, 0, 0, "");
1817 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1818 event_add(&stdioev, NULL);
1820 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1821 signal_add(&winchev, NULL);
1823 new_tab(url);
1825 return 1;
1828 void
1829 ui_on_tab_loaded(struct tab *tab)
1831 stop_loading_anim(tab);
1832 message("Loaded %s", tab->hist_cur->h);
1834 redraw_tabline();
1835 wrefresh(tabline);
1836 if (in_minibuffer)
1837 wrefresh(minibuf);
1838 else
1839 wrefresh(body);
1842 void
1843 ui_on_tab_refresh(struct tab *tab)
1845 wrap_page(&tab->window);
1846 if (tab->flags & TAB_CURRENT) {
1847 restore_cursor(&tab->window);
1848 redraw_tab(tab);
1852 void
1853 ui_require_input(struct tab *tab, int hide)
1855 /* TODO: hard-switching to another tab is ugly */
1856 switch_to_tab(tab);
1858 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1859 &ir_history);
1860 strlcpy(ministate.prompt, "Input required: ",
1861 sizeof(ministate.prompt));
1862 redraw_tab(tab);
1865 void
1866 ui_yornp(const char *prompt, void (*fn)(int, unsigned int))
1868 size_t len;
1870 yornp_cb = fn;
1871 enter_minibuffer(yornp_self_insert, yornp_self_insert,
1872 yornp_abort, NULL);
1874 len = sizeof(ministate.prompt);
1875 strlcpy(ministate.prompt, prompt, len);
1876 strlcat(ministate.prompt, " (y or n) ", len);
1877 redraw_tab(current_tab());
1880 void
1881 ui_notify(const char *fmt, ...)
1883 va_list ap;
1885 va_start(ap, fmt);
1886 vmessage(fmt, ap);
1887 va_end(ap);
1890 void
1891 ui_end(void)
1893 endwin();