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);
355 minibuffer_set_key("C-b", cmd_backward_char);
356 minibuffer_set_key("C-f", cmd_forward_char);
357 minibuffer_set_key("<left>", cmd_backward_char);
358 minibuffer_set_key("<right>", cmd_forward_char);
359 minibuffer_set_key("C-e", cmd_move_end_of_line);
360 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
361 minibuffer_set_key("<end>", cmd_move_end_of_line);
362 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
363 minibuffer_set_key("C-k", cmd_mini_kill_line);
365 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
366 minibuffer_set_key("M-n", cmd_mini_next_history_element);
367 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
368 minibuffer_set_key("<down>", cmd_mini_next_history_element);
371 static void
372 restore_cursor(struct window *window)
374 struct vline *vl;
375 const char *prfx;
377 vl = window->current_line;
378 if (vl == NULL || vl->line == NULL)
379 window->curs_x = window->cpoff = 0;
380 else
381 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
383 if (vl != NULL) {
384 prfx = line_prefixes[vl->parent->type].prfx1;
385 window->curs_x += utf8_swidth(prfx);
389 static void
390 cmd_previous_line(struct window *window)
392 struct vline *vl;
394 if (window->current_line == NULL
395 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
396 return;
398 if (--window->curs_y < 0) {
399 window->curs_y = 0;
400 cmd_scroll_line_up(window);
401 return;
404 window->current_line = vl;
405 restore_cursor(window);
408 static void
409 cmd_next_line(struct window *window)
411 struct vline *vl;
413 if (window->current_line == NULL
414 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
415 return;
417 if (++window->curs_y > body_lines-1) {
418 window->curs_y = body_lines-1;
419 cmd_scroll_line_down(window);
420 return;
423 window->current_line = vl;
424 restore_cursor(window);
427 static void
428 cmd_backward_char(struct window *window)
430 if (window->cpoff != 0)
431 window->cpoff--;
432 restore_cursor(window);
435 static void
436 cmd_forward_char(struct window *window)
438 window->cpoff++;
439 restore_cursor(window);
442 static void
443 cmd_backward_paragraph(struct window *window)
445 do {
446 if (window->current_line == NULL ||
447 window->current_line == TAILQ_FIRST(&window->head)) {
448 message("No previous paragraph");
449 return;
451 cmd_previous_line(window);
452 } while (window->current_line->line != NULL ||
453 window->current_line->parent->type != LINE_TEXT);
456 static void
457 cmd_forward_paragraph(struct window *window)
459 do {
460 if (window->current_line == NULL ||
461 window->current_line == TAILQ_LAST(&window->head, vhead)) {
462 message("No next paragraph");
463 return;
465 cmd_next_line(window);
466 } while (window->current_line->line != NULL ||
467 window->current_line->parent->type != LINE_TEXT);
470 static void
471 cmd_move_beginning_of_line(struct window *window)
473 window->cpoff = 0;
474 restore_cursor(window);
477 static void
478 cmd_move_end_of_line(struct window *window)
480 struct vline *vl;
482 vl = window->current_line;
483 if (vl->line == NULL)
484 return;
485 window->cpoff = utf8_cplen(vl->line);
486 restore_cursor(window);
489 static void
490 cmd_redraw(struct window *window)
492 handle_resize(0, 0, NULL);
495 static void
496 cmd_scroll_line_up(struct window *window)
498 struct vline *vl;
500 if (window->line_off == 0)
501 return;
503 vl = nth_line(window, --window->line_off);
504 wscrl(body, -1);
505 wmove(body, 0, 0);
506 print_vline(vl);
508 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
509 restore_cursor(window);
512 static void
513 cmd_scroll_line_down(struct window *window)
515 struct vline *vl;
517 vl = window->current_line;
518 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
519 return;
520 window->current_line = vl;
522 window->line_off++;
523 wscrl(body, 1);
525 if (window->line_max - window->line_off < (size_t)body_lines)
526 return;
528 vl = nth_line(window, window->line_off + body_lines-1);
529 wmove(body, body_lines-1, 0);
530 print_vline(vl);
532 restore_cursor(window);
535 static void
536 cmd_scroll_up(struct window *window)
538 size_t off;
540 off = body_lines+1;
542 for (; off > 0; --off)
543 cmd_scroll_line_up(window);
546 static void
547 cmd_scroll_down(struct window *window)
549 size_t off;
551 off = body_lines+1;
553 for (; off > 0; --off)
554 cmd_scroll_line_down(window);
557 static void
558 cmd_beginning_of_buffer(struct window *window)
560 window->current_line = TAILQ_FIRST(&window->head);
561 window->line_off = 0;
562 window->curs_y = 0;
563 window->cpoff = 0;
564 restore_cursor(window);
567 static void
568 cmd_end_of_buffer(struct window *window)
570 ssize_t off;
572 off = window->line_max - body_lines;
573 off = MAX(0, off);
575 window->line_off = off;
576 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
578 window->current_line = TAILQ_LAST(&window->head, vhead);
579 window->cpoff = body_cols;
580 restore_cursor(window);
583 static void
584 cmd_kill_telescope(struct window *window)
586 event_loopbreak();
589 static void
590 cmd_push_button(struct window *window)
592 struct vline *vl;
593 size_t nth;
595 nth = window->line_off + window->curs_y;
596 if (nth >= window->line_max)
597 return;
598 vl = nth_line(window, nth);
599 if (vl->parent->type != LINE_LINK)
600 return;
602 load_url_in_tab(current_tab(), vl->parent->alt);
605 static void
606 cmd_push_button_new_tab(struct window *window)
608 struct vline *vl;
609 size_t nth;
611 nth = window->line_off + window->curs_y;
612 if (nth > window->line_max)
613 return;
614 vl = nth_line(window, nth);
615 if (vl->parent->type != LINE_LINK)
616 return;
618 new_tab(vl->parent->alt);
621 static void
622 cmd_previous_button(struct window *window)
624 do {
625 if (window->current_line == NULL ||
626 window->current_line == TAILQ_FIRST(&window->head)) {
627 message("No previous link");
628 return;
630 cmd_previous_line(window);
631 } while (window->current_line->parent->type != LINE_LINK);
634 static void
635 cmd_next_button(struct window *window)
637 do {
638 if (window->current_line == NULL ||
639 window->current_line == TAILQ_LAST(&window->head, vhead)) {
640 message("No next link");
641 return;
643 cmd_next_line(window);
644 } while (window->current_line->parent->type != LINE_LINK);
647 static void
648 cmd_previous_page(struct window *window)
650 struct tab *tab = current_tab();
652 if (!load_previous_page(tab))
653 message("No previous page");
654 else
655 start_loading_anim(tab);
658 static void
659 cmd_next_page(struct window *window)
661 struct tab *tab = current_tab();
663 if (!load_next_page(tab))
664 message("No next page");
665 else
666 start_loading_anim(tab);
669 static void
670 cmd_clear_minibuf(struct window *window)
672 handle_clear_minibuf(0, 0, NULL);
675 static void
676 cmd_execute_extended_command(struct window *window)
678 size_t len;
680 if (in_minibuffer) {
681 message("We don't have enable-recursive-minibuffers");
682 return;
685 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
686 &eecmd_history);
688 len = sizeof(ministate.prompt);
689 strlcpy(ministate.prompt, "", len);
691 if (thiskey.meta)
692 strlcat(ministate.prompt, "M-", len);
694 strlcat(ministate.prompt, keyname(thiskey.key), len);
695 strlcat(ministate.prompt, " ", len);
698 static void
699 cmd_tab_close(struct window *window)
701 struct tab *tab, *t;
703 tab = current_tab();
704 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
705 TAILQ_NEXT(tab, tabs) == NULL) {
706 message("Can't close the only tab.");
707 return;
710 if (evtimer_pending(&tab->loadingev, NULL))
711 evtimer_del(&tab->loadingev);
713 stop_tab(tab);
715 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
716 t = TAILQ_NEXT(tab, tabs);
717 TAILQ_REMOVE(&tabshead, tab, tabs);
718 free(tab);
720 switch_to_tab(t);
723 static void
724 cmd_tab_close_other(struct window *window)
726 struct tab *tab, *t, *i;
728 tab = current_tab();
729 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
730 if (t->flags & TAB_CURRENT)
731 continue;
733 stop_tab(t);
734 TAILQ_REMOVE(&tabshead, t, tabs);
735 free(t);
739 static void
740 cmd_tab_new(struct window *window)
742 new_tab(NEW_TAB_URL);
745 static void
746 cmd_tab_next(struct window *window)
748 struct tab *tab, *t;
750 tab = current_tab();
751 tab->flags &= ~TAB_CURRENT;
753 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
754 t = TAILQ_FIRST(&tabshead);
755 t->flags |= TAB_CURRENT;
758 static void
759 cmd_tab_previous(struct window *window)
761 struct tab *tab, *t;
763 tab = current_tab();
764 tab->flags &= ~TAB_CURRENT;
766 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
767 t = TAILQ_LAST(&tabshead, tabshead);
768 t->flags |= TAB_CURRENT;
771 static void
772 cmd_load_url(struct window *window)
774 if (in_minibuffer) {
775 message("We don't have enable-recursive-minibuffers");
776 return;
779 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
780 &lu_history);
781 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
784 static void
785 cmd_load_current_url(struct window *window)
787 struct tab *tab = current_tab();
789 if (in_minibuffer) {
790 message("We don't have enable-recursive-minibuffers");
791 return;
794 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
795 &lu_history);
796 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
797 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
798 ministate.window.cpoff = utf8_cplen(ministate.buf);
801 static void
802 cmd_bookmark_page(struct window *window)
804 struct tab *tab = current_tab();
806 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
807 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
808 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
809 ministate.window.cpoff = utf8_cplen(ministate.buf);
812 static void
813 cmd_goto_bookmarks(struct window *window)
815 load_url_in_tab(current_tab(), "about:bookmarks");
818 static void
819 cmd_mini_delete_char(struct window *window)
821 char *c, *n;
823 if (!in_minibuffer) {
824 message("text is read-only");
825 return;
828 minibuffer_taint_hist();
830 c = utf8_nth(window->current_line->line, window->cpoff);
831 if (*c == '\0')
832 return;
833 n = utf8_next_cp(c);
835 memmove(c, n, strlen(n)+1);
838 static void
839 cmd_mini_delete_backward_char(struct window *window)
841 char *c, *p, *start;
843 if (!in_minibuffer) {
844 message("text is read-only");
845 return;
848 minibuffer_taint_hist();
850 c = utf8_nth(window->current_line->line, window->cpoff);
851 start = window->current_line->line;
852 if (c == start)
853 return;
854 p = utf8_prev_cp(c-1, start);
856 memmove(p, c, strlen(c)+1);
857 window->cpoff--;
860 static void
861 cmd_mini_kill_line(struct window *window)
863 char *c;
865 if (!in_minibuffer) {
866 message("text is read-only");
867 return;
870 minibuffer_taint_hist();
871 c = utf8_nth(window->current_line->line, window->cpoff);
872 *c = '\0';
875 static void
876 cmd_mini_abort(struct window *window)
878 if (!in_minibuffer)
879 return;
881 ministate.abortfn();
884 static void
885 cmd_mini_complete_and_exit(struct window *window)
887 if (!in_minibuffer)
888 return;
890 minibuffer_taint_hist();
891 ministate.donefn();
894 static void
895 cmd_mini_previous_history_element(struct window *window)
897 if (ministate.history == NULL) {
898 message("No history");
899 return;
902 if (ministate.hist_cur == NULL ||
903 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
904 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
905 ministate.hist_off = ministate.history->len - 1;
906 if (ministate.hist_cur == NULL)
907 message("No prev item");
908 } else {
909 ministate.hist_off--;
912 if (ministate.hist_cur != NULL)
913 window->current_line->line = ministate.hist_cur->h;
916 static void
917 cmd_mini_next_history_element(struct window *window)
919 if (ministate.history == NULL) {
920 message("No history");
921 return;
924 if (ministate.hist_cur == NULL ||
925 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
926 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
927 ministate.hist_off = 0;
928 if (ministate.hist_cur == NULL)
929 message("No next item");
930 } else {
931 ministate.hist_off++;
934 if (ministate.hist_cur != NULL)
935 window->current_line->line = ministate.hist_cur->h;
938 static void
939 global_key_unbound(void)
941 message("%s is undefined", keybuf);
944 static void
945 minibuffer_hist_save_entry(void)
947 struct hist *hist;
949 if (ministate.history == NULL)
950 return;
952 if ((hist = calloc(1, sizeof(*hist))) == NULL)
953 abort();
955 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
957 if (TAILQ_EMPTY(&ministate.history->head))
958 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
959 else
960 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
961 ministate.history->len++;
964 /*
965 * taint the minibuffer cache: if we're currently showing a history
966 * element, copy that to the current buf and reset the "history
967 * navigation" thing.
968 */
969 static void
970 minibuffer_taint_hist(void)
972 if (ministate.hist_cur == NULL)
973 return;
975 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
976 ministate.hist_cur = NULL;
979 static void
980 minibuffer_self_insert(void)
982 char *c, tmp[5] = {0};
983 size_t len;
985 minibuffer_taint_hist();
987 if (thiskey.cp == 0)
988 return;
990 len = utf8_encode(thiskey.cp, tmp);
991 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
992 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
993 return;
995 memmove(c + len, c, strlen(c)+1);
996 memcpy(c, tmp, len);
997 ministate.window.cpoff++;
1000 static void
1001 eecmd_self_insert(void)
1003 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1004 !unicode_isgraph(thiskey.cp)) {
1005 global_key_unbound();
1006 return;
1009 minibuffer_self_insert();
1012 static void
1013 eecmd_select(void)
1015 struct cmds *cmd;
1017 exit_minibuffer();
1018 minibuffer_hist_save_entry();
1020 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1021 if (!strcmp(cmd->cmd, ministate.buf)) {
1022 cmd->fn(current_window());
1023 return;
1027 message("Unknown command: %s", ministate.buf);
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();