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 <assert.h>
40 #include <curses.h>
41 #include <event.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #define TAB_CURRENT 0x1
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void 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_tab_move);
90 CMD(cmd_tab_move_to);
91 CMD(cmd_load_url);
92 CMD(cmd_load_current_url);
93 CMD(cmd_bookmark_page);
94 CMD(cmd_goto_bookmarks);
95 CMD(cmd_toggle_help);
97 CMD(cmd_mini_delete_char);
98 CMD(cmd_mini_delete_backward_char);
99 CMD(cmd_mini_kill_line);
100 CMD(cmd_mini_abort);
101 CMD(cmd_mini_complete_and_exit);
102 CMD(cmd_mini_previous_history_element);
103 CMD(cmd_mini_next_history_element);
105 #include "cmd.gen.h"
107 static void global_key_unbound(void);
108 static void minibuffer_hist_save_entry(void);
109 static void minibuffer_taint_hist(void);
110 static void minibuffer_self_insert(void);
111 static void eecmd_self_insert(void);
112 static void eecmd_select(void);
113 static void ir_self_insert(void);
114 static void ir_select(void);
115 static void lu_self_insert(void);
116 static void lu_select(void);
117 static void bp_select(void);
118 static void yornp_self_insert(void);
119 static void yornp_abort(void);
121 static struct vline *nth_line(struct window*, size_t);
122 static struct tab *current_tab(void);
123 static struct window *current_window(void);
124 static int readkey(void);
125 static void dispatch_stdio(int, short, void*);
126 static void handle_clear_minibuf(int, short, void*);
127 static void handle_resize(int, short, void*);
128 static int wrap_page(struct window*, int);
129 static void print_vline(WINDOW*, struct vline*);
130 static void redraw_tabline(void);
131 static void redraw_window(WINDOW*, int, struct window*);
132 static void redraw_help(void);
133 static void redraw_body(struct tab*);
134 static void redraw_modeline(struct tab*);
135 static void redraw_minibuffer(void);
136 static void redraw_tab(struct tab*);
137 static void emit_help_item(char*, void*);
138 static void rec_compute_help(struct kmap*, char*, size_t);
139 static void recompute_help(void);
140 static void vmessage(const char*, va_list);
141 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
142 static void start_loading_anim(struct tab*);
143 static void update_loading_anim(int, short, void*);
144 static void stop_loading_anim(struct tab*);
145 static void load_url_in_tab(struct tab*, const char*);
146 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
147 static void exit_minibuffer(void);
148 static void switch_to_tab(struct tab*);
149 static struct tab *new_tab(const char*);
150 static void usage(void);
152 static struct { short meta; int key; uint32_t cp; } thiskey;
154 static WINDOW *tabline, *body, *modeline, *minibuf;
155 static int body_lines, body_cols;
157 static WINDOW *help;
158 static struct window helpwin;
159 static int help_lines, help_cols;
161 static int side_window;
163 static struct event clminibufev;
164 static struct timeval clminibufev_timer = { 5, 0 };
165 static struct timeval loadingev_timer = { 0, 250000 };
167 static uint32_t tab_counter;
169 static char keybuf[64];
171 static void (*yornp_cb)(int, unsigned int);
173 struct kmap global_map,
174 minibuffer_map,
175 *current_map,
176 *base_map;
178 static struct histhead eecmd_history,
179 ir_history,
180 lu_history;
182 static int in_minibuffer;
184 static struct {
185 char *curmesg;
187 char prompt[64];
188 void (*donefn)(void);
189 void (*abortfn)(void);
191 char buf[1025];
192 struct line line;
193 struct vline vline;
194 struct window window;
196 struct histhead *history;
197 struct hist *hist_cur;
198 size_t hist_off;
199 } ministate;
201 struct lineprefix {
202 const char *prfx1;
203 const char *prfx2;
204 } line_prefixes[] = {
205 [LINE_TEXT] = { "", "" },
206 [LINE_LINK] = { "=> ", " " },
207 [LINE_TITLE_1] = { "# ", " " },
208 [LINE_TITLE_2] = { "## ", " " },
209 [LINE_TITLE_3] = { "### ", " " },
210 [LINE_ITEM] = { "* ", " " },
211 [LINE_QUOTE] = { "> ", " " },
212 [LINE_PRE_START] = { "```", " " },
213 [LINE_PRE_CONTENT] = { "", "" },
214 [LINE_PRE_END] = { "```", "```" },
215 };
217 static struct line_face {
218 int prefix_prop;
219 int text_prop;
220 } line_faces[] = {
221 [LINE_TEXT] = { 0, 0 },
222 [LINE_LINK] = { 0, A_UNDERLINE },
223 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
224 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
225 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
226 [LINE_ITEM] = { 0, 0 },
227 [LINE_QUOTE] = { 0, A_DIM },
228 [LINE_PRE_START] = { 0, 0 },
229 [LINE_PRE_CONTENT] = { 0, 0 },
230 [LINE_PRE_END] = { 0, 0 },
231 };
233 static struct tab_face {
234 int background, tab, current_tab;
235 } tab_face = {
236 A_REVERSE, A_REVERSE, A_NORMAL
237 };
239 static inline void
240 global_set_key(const char *key, void (*fn)(struct window*))
242 if (!kmap_define_key(&global_map, key, fn))
243 _exit(1);
246 static inline void
247 minibuffer_set_key(const char *key, void (*fn)(struct window*))
249 if (!kmap_define_key(&minibuffer_map, key, fn))
250 _exit(1);
253 static void
254 load_default_keys(void)
256 /* === global map === */
258 /* emacs */
259 global_set_key("C-p", cmd_previous_line);
260 global_set_key("C-n", cmd_next_line);
261 global_set_key("C-f", cmd_forward_char);
262 global_set_key("C-b", cmd_backward_char);
263 global_set_key("M-{", cmd_backward_paragraph);
264 global_set_key("M-}", cmd_forward_paragraph);
265 global_set_key("C-a", cmd_move_beginning_of_line);
266 global_set_key("C-e", cmd_move_end_of_line);
268 global_set_key("M-v", cmd_scroll_up);
269 global_set_key("C-v", cmd_scroll_down);
270 global_set_key("M-space", cmd_scroll_up);
271 global_set_key("space", cmd_scroll_down);
273 global_set_key("M-<", cmd_beginning_of_buffer);
274 global_set_key("M->", cmd_end_of_buffer);
276 global_set_key("C-x C-c", cmd_kill_telescope);
278 global_set_key("C-g", cmd_clear_minibuf);
280 global_set_key("M-x", cmd_execute_extended_command);
281 global_set_key("C-x C-f", cmd_load_url);
282 global_set_key("C-x M-f", cmd_load_current_url);
284 global_set_key("C-x t 0", cmd_tab_close);
285 global_set_key("C-x t 1", cmd_tab_close_other);
286 global_set_key("C-x t 2", cmd_tab_new);
287 global_set_key("C-x t o", cmd_tab_next);
288 global_set_key("C-x t O", cmd_tab_previous);
289 global_set_key("C-x t m", cmd_tab_move);
290 global_set_key("C-x t M", cmd_tab_move_to);
292 global_set_key("C-M-b", cmd_previous_page);
293 global_set_key("C-M-f", cmd_next_page);
295 global_set_key("<f7> a", cmd_bookmark_page);
296 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
298 /* vi/vi-like */
299 global_set_key("k", cmd_previous_line);
300 global_set_key("j", cmd_next_line);
301 global_set_key("l", cmd_forward_char);
302 global_set_key("h", cmd_backward_char);
303 global_set_key("{", cmd_backward_paragraph);
304 global_set_key("}", cmd_forward_paragraph);
305 global_set_key("^", cmd_move_beginning_of_line);
306 global_set_key("$", cmd_move_end_of_line);
308 global_set_key("K", cmd_scroll_line_up);
309 global_set_key("J", cmd_scroll_line_down);
311 global_set_key("g g", cmd_beginning_of_buffer);
312 global_set_key("G", cmd_end_of_buffer);
314 global_set_key("g D", cmd_tab_close);
315 global_set_key("g N", cmd_tab_new);
316 global_set_key("g t", cmd_tab_next);
317 global_set_key("g T", cmd_tab_previous);
318 global_set_key("g M-t", cmd_tab_move);
319 global_set_key("g M-T", cmd_tab_move_to);
321 global_set_key("H", cmd_previous_page);
322 global_set_key("L", cmd_next_page);
324 /* tmp */
325 global_set_key("q", cmd_kill_telescope);
327 global_set_key("esc", cmd_clear_minibuf);
329 global_set_key(":", cmd_execute_extended_command);
331 /* cua */
332 global_set_key("<up>", cmd_previous_line);
333 global_set_key("<down>", cmd_next_line);
334 global_set_key("<right>", cmd_forward_char);
335 global_set_key("<left>", cmd_backward_char);
336 global_set_key("<prior>", cmd_scroll_up);
337 global_set_key("<next>", cmd_scroll_down);
339 global_set_key("M-<left>", cmd_previous_page);
340 global_set_key("M-<right>", cmd_next_page);
342 /* "ncurses standard" */
343 global_set_key("C-l", cmd_redraw);
345 /* global */
346 global_set_key("<f1>", cmd_toggle_help);
347 global_set_key("C-m", cmd_push_button);
348 global_set_key("M-enter", cmd_push_button_new_tab);
349 global_set_key("M-tab", cmd_previous_button);
350 global_set_key("tab", cmd_next_button);
352 /* === minibuffer map === */
353 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
354 minibuffer_set_key("C-g", cmd_mini_abort);
355 minibuffer_set_key("esc", cmd_mini_abort);
356 minibuffer_set_key("C-d", cmd_mini_delete_char);
357 minibuffer_set_key("del", cmd_mini_delete_backward_char);
358 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
359 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
361 minibuffer_set_key("C-b", cmd_backward_char);
362 minibuffer_set_key("C-f", cmd_forward_char);
363 minibuffer_set_key("<left>", cmd_backward_char);
364 minibuffer_set_key("<right>", cmd_forward_char);
365 minibuffer_set_key("C-e", cmd_move_end_of_line);
366 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
367 minibuffer_set_key("<end>", cmd_move_end_of_line);
368 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
369 minibuffer_set_key("C-k", cmd_mini_kill_line);
371 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
372 minibuffer_set_key("M-n", cmd_mini_next_history_element);
373 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
374 minibuffer_set_key("<down>", cmd_mini_next_history_element);
377 static void
378 restore_cursor(struct window *window)
380 struct vline *vl;
381 const char *prfx;
383 vl = window->current_line;
384 if (vl == NULL || vl->line == NULL)
385 window->curs_x = window->cpoff = 0;
386 else
387 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
389 if (vl != NULL) {
390 prfx = line_prefixes[vl->parent->type].prfx1;
391 window->curs_x += utf8_swidth(prfx);
395 static void
396 cmd_previous_line(struct window *window)
398 struct vline *vl;
400 if (window->current_line == NULL
401 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
402 return;
404 if (--window->curs_y < 0) {
405 window->curs_y = 0;
406 cmd_scroll_line_up(window);
407 return;
410 window->current_line = vl;
411 restore_cursor(window);
414 static void
415 cmd_next_line(struct window *window)
417 struct vline *vl;
419 if (window->current_line == NULL
420 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
421 return;
423 if (++window->curs_y > body_lines-1) {
424 window->curs_y = body_lines-1;
425 cmd_scroll_line_down(window);
426 return;
429 window->current_line = vl;
430 restore_cursor(window);
433 static void
434 cmd_backward_char(struct window *window)
436 if (window->cpoff != 0)
437 window->cpoff--;
438 restore_cursor(window);
441 static void
442 cmd_forward_char(struct window *window)
444 window->cpoff++;
445 restore_cursor(window);
448 static void
449 cmd_backward_paragraph(struct window *window)
451 do {
452 if (window->current_line == NULL ||
453 window->current_line == TAILQ_FIRST(&window->head)) {
454 message("No previous paragraph");
455 return;
457 cmd_previous_line(window);
458 } while (window->current_line->line != NULL ||
459 window->current_line->parent->type != LINE_TEXT);
462 static void
463 cmd_forward_paragraph(struct window *window)
465 do {
466 if (window->current_line == NULL ||
467 window->current_line == TAILQ_LAST(&window->head, vhead)) {
468 message("No next paragraph");
469 return;
471 cmd_next_line(window);
472 } while (window->current_line->line != NULL ||
473 window->current_line->parent->type != LINE_TEXT);
476 static void
477 cmd_move_beginning_of_line(struct window *window)
479 window->cpoff = 0;
480 restore_cursor(window);
483 static void
484 cmd_move_end_of_line(struct window *window)
486 struct vline *vl;
488 vl = window->current_line;
489 if (vl->line == NULL)
490 return;
491 window->cpoff = utf8_cplen(vl->line);
492 restore_cursor(window);
495 static void
496 cmd_redraw(struct window *window)
498 handle_resize(0, 0, NULL);
501 static void
502 cmd_scroll_line_up(struct window *window)
504 struct vline *vl;
506 if (window->line_off == 0)
507 return;
509 vl = nth_line(window, --window->line_off);
510 wscrl(body, -1);
511 wmove(body, 0, 0);
512 print_vline(body, vl);
514 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
515 restore_cursor(window);
518 static void
519 cmd_scroll_line_down(struct window *window)
521 struct vline *vl;
523 vl = window->current_line;
524 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
525 return;
526 window->current_line = vl;
528 window->line_off++;
529 wscrl(body, 1);
531 if (window->line_max - window->line_off < (size_t)body_lines)
532 return;
534 vl = nth_line(window, window->line_off + body_lines-1);
535 wmove(body, body_lines-1, 0);
536 print_vline(body, vl);
538 restore_cursor(window);
541 static void
542 cmd_scroll_up(struct window *window)
544 size_t off;
546 off = body_lines+1;
548 for (; off > 0; --off)
549 cmd_scroll_line_up(window);
552 static void
553 cmd_scroll_down(struct window *window)
555 size_t off;
557 off = body_lines+1;
559 for (; off > 0; --off)
560 cmd_scroll_line_down(window);
563 static void
564 cmd_beginning_of_buffer(struct window *window)
566 window->current_line = TAILQ_FIRST(&window->head);
567 window->line_off = 0;
568 window->curs_y = 0;
569 window->cpoff = 0;
570 restore_cursor(window);
573 static void
574 cmd_end_of_buffer(struct window *window)
576 ssize_t off;
578 off = window->line_max - body_lines;
579 off = MAX(0, off);
581 window->line_off = off;
582 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
584 window->current_line = TAILQ_LAST(&window->head, vhead);
585 window->cpoff = body_cols;
586 restore_cursor(window);
589 static void
590 cmd_kill_telescope(struct window *window)
592 event_loopbreak();
595 static void
596 cmd_push_button(struct window *window)
598 struct vline *vl;
599 size_t nth;
601 nth = window->line_off + window->curs_y;
602 if (nth >= window->line_max)
603 return;
604 vl = nth_line(window, nth);
605 if (vl->parent->type != LINE_LINK)
606 return;
608 load_url_in_tab(current_tab(), vl->parent->alt);
611 static void
612 cmd_push_button_new_tab(struct window *window)
614 struct vline *vl;
615 size_t nth;
617 nth = window->line_off + window->curs_y;
618 if (nth > window->line_max)
619 return;
620 vl = nth_line(window, nth);
621 if (vl->parent->type != LINE_LINK)
622 return;
624 new_tab(vl->parent->alt);
627 static void
628 cmd_previous_button(struct window *window)
630 do {
631 if (window->current_line == NULL ||
632 window->current_line == TAILQ_FIRST(&window->head)) {
633 message("No previous link");
634 return;
636 cmd_previous_line(window);
637 } while (window->current_line->parent->type != LINE_LINK);
640 static void
641 cmd_next_button(struct window *window)
643 do {
644 if (window->current_line == NULL ||
645 window->current_line == TAILQ_LAST(&window->head, vhead)) {
646 message("No next link");
647 return;
649 cmd_next_line(window);
650 } while (window->current_line->parent->type != LINE_LINK);
653 static void
654 cmd_previous_page(struct window *window)
656 struct tab *tab = current_tab();
658 if (!load_previous_page(tab))
659 message("No previous page");
660 else
661 start_loading_anim(tab);
664 static void
665 cmd_next_page(struct window *window)
667 struct tab *tab = current_tab();
669 if (!load_next_page(tab))
670 message("No next page");
671 else
672 start_loading_anim(tab);
675 static void
676 cmd_clear_minibuf(struct window *window)
678 handle_clear_minibuf(0, 0, NULL);
681 static void
682 cmd_execute_extended_command(struct window *window)
684 size_t len;
686 if (in_minibuffer) {
687 message("We don't have enable-recursive-minibuffers");
688 return;
691 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
692 &eecmd_history);
694 len = sizeof(ministate.prompt);
695 strlcpy(ministate.prompt, "", len);
697 if (thiskey.meta)
698 strlcat(ministate.prompt, "M-", len);
700 strlcat(ministate.prompt, keyname(thiskey.key), len);
701 strlcat(ministate.prompt, " ", len);
704 static void
705 cmd_tab_close(struct window *window)
707 struct tab *tab, *t;
709 tab = current_tab();
710 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
711 TAILQ_NEXT(tab, tabs) == NULL) {
712 message("Can't close the only tab.");
713 return;
716 if (evtimer_pending(&tab->loadingev, NULL))
717 evtimer_del(&tab->loadingev);
719 stop_tab(tab);
721 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
722 t = TAILQ_NEXT(tab, tabs);
723 TAILQ_REMOVE(&tabshead, tab, tabs);
724 free(tab);
726 switch_to_tab(t);
729 static void
730 cmd_tab_close_other(struct window *window)
732 struct tab *t, *i;
734 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
735 if (t->flags & TAB_CURRENT)
736 continue;
738 stop_tab(t);
739 TAILQ_REMOVE(&tabshead, t, tabs);
740 free(t);
744 static void
745 cmd_tab_new(struct window *window)
747 new_tab(NEW_TAB_URL);
750 static void
751 cmd_tab_next(struct window *window)
753 struct tab *tab, *t;
755 tab = current_tab();
756 tab->flags &= ~TAB_CURRENT;
758 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
759 t = TAILQ_FIRST(&tabshead);
760 t->flags |= TAB_CURRENT;
763 static void
764 cmd_tab_previous(struct window *window)
766 struct tab *tab, *t;
768 tab = current_tab();
769 tab->flags &= ~TAB_CURRENT;
771 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
772 t = TAILQ_LAST(&tabshead, tabshead);
773 t->flags |= TAB_CURRENT;
776 static void
777 cmd_tab_move(struct window *window)
779 struct tab *tab, *t;
781 tab = current_tab();
782 t = TAILQ_NEXT(tab, tabs);
783 TAILQ_REMOVE(&tabshead, tab, tabs);
785 if (t == NULL)
786 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
787 else
788 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
791 static void
792 cmd_tab_move_to(struct window *window)
794 struct tab *tab, *t;
796 tab = current_tab();
797 t = TAILQ_PREV(tab, tabshead, tabs);
798 TAILQ_REMOVE(&tabshead, tab, tabs);
800 if (t == NULL) {
801 if (TAILQ_EMPTY(&tabshead))
802 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
803 else
804 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
805 } else
806 TAILQ_INSERT_BEFORE(t, tab, tabs);
809 static void
810 cmd_load_url(struct window *window)
812 if (in_minibuffer) {
813 message("We don't have enable-recursive-minibuffers");
814 return;
817 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
818 &lu_history);
819 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
822 static void
823 cmd_load_current_url(struct window *window)
825 struct tab *tab = current_tab();
827 if (in_minibuffer) {
828 message("We don't have enable-recursive-minibuffers");
829 return;
832 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
833 &lu_history);
834 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
835 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
836 ministate.window.cpoff = utf8_cplen(ministate.buf);
839 static void
840 cmd_bookmark_page(struct window *window)
842 struct tab *tab = current_tab();
844 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
845 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
846 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
847 ministate.window.cpoff = utf8_cplen(ministate.buf);
850 static void
851 cmd_goto_bookmarks(struct window *window)
853 load_url_in_tab(current_tab(), "about:bookmarks");
856 static void
857 cmd_toggle_help(struct window *window)
859 side_window = !side_window;
860 if (side_window)
861 recompute_help();
863 /* ugly hack, but otherwise the window doesn't get updated
864 * until I call handle_resize a second time (i.e. C-l). I
865 * will be happy to know why something like this is needed. */
866 handle_resize(0, 0, NULL);
867 handle_resize(0, 0, NULL);
870 static void
871 cmd_mini_delete_char(struct window *window)
873 char *c, *n;
875 if (!in_minibuffer) {
876 message("text is read-only");
877 return;
880 minibuffer_taint_hist();
882 c = utf8_nth(window->current_line->line, window->cpoff);
883 if (*c == '\0')
884 return;
885 n = utf8_next_cp(c);
887 memmove(c, n, strlen(n)+1);
890 static void
891 cmd_mini_delete_backward_char(struct window *window)
893 char *c, *p, *start;
895 if (!in_minibuffer) {
896 message("text is read-only");
897 return;
900 minibuffer_taint_hist();
902 c = utf8_nth(window->current_line->line, window->cpoff);
903 start = window->current_line->line;
904 if (c == start)
905 return;
906 p = utf8_prev_cp(c-1, start);
908 memmove(p, c, strlen(c)+1);
909 window->cpoff--;
912 static void
913 cmd_mini_kill_line(struct window *window)
915 char *c;
917 if (!in_minibuffer) {
918 message("text is read-only");
919 return;
922 minibuffer_taint_hist();
923 c = utf8_nth(window->current_line->line, window->cpoff);
924 *c = '\0';
927 static void
928 cmd_mini_abort(struct window *window)
930 if (!in_minibuffer)
931 return;
933 ministate.abortfn();
936 static void
937 cmd_mini_complete_and_exit(struct window *window)
939 if (!in_minibuffer)
940 return;
942 minibuffer_taint_hist();
943 ministate.donefn();
946 static void
947 cmd_mini_previous_history_element(struct window *window)
949 if (ministate.history == NULL) {
950 message("No history");
951 return;
954 if (ministate.hist_cur == NULL ||
955 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
956 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
957 ministate.hist_off = ministate.history->len - 1;
958 if (ministate.hist_cur == NULL)
959 message("No prev item");
960 } else {
961 ministate.hist_off--;
964 if (ministate.hist_cur != NULL)
965 window->current_line->line = ministate.hist_cur->h;
968 static void
969 cmd_mini_next_history_element(struct window *window)
971 if (ministate.history == NULL) {
972 message("No history");
973 return;
976 if (ministate.hist_cur == NULL ||
977 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
978 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
979 ministate.hist_off = 0;
980 if (ministate.hist_cur == NULL)
981 message("No next item");
982 } else {
983 ministate.hist_off++;
986 if (ministate.hist_cur != NULL)
987 window->current_line->line = ministate.hist_cur->h;
990 static void
991 global_key_unbound(void)
993 message("%s is undefined", keybuf);
996 static void
997 minibuffer_hist_save_entry(void)
999 struct hist *hist;
1001 if (ministate.history == NULL)
1002 return;
1004 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1005 abort();
1007 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1009 if (TAILQ_EMPTY(&ministate.history->head))
1010 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1011 else
1012 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1013 ministate.history->len++;
1017 * taint the minibuffer cache: if we're currently showing a history
1018 * element, copy that to the current buf and reset the "history
1019 * navigation" thing.
1021 static void
1022 minibuffer_taint_hist(void)
1024 if (ministate.hist_cur == NULL)
1025 return;
1027 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1028 ministate.hist_cur = NULL;
1031 static void
1032 minibuffer_self_insert(void)
1034 char *c, tmp[5] = {0};
1035 size_t len;
1037 minibuffer_taint_hist();
1039 if (thiskey.cp == 0)
1040 return;
1042 len = utf8_encode(thiskey.cp, tmp);
1043 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1044 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1045 return;
1047 memmove(c + len, c, strlen(c)+1);
1048 memcpy(c, tmp, len);
1049 ministate.window.cpoff++;
1052 static void
1053 eecmd_self_insert(void)
1055 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1056 !unicode_isgraph(thiskey.cp)) {
1057 global_key_unbound();
1058 return;
1061 minibuffer_self_insert();
1064 static void
1065 eecmd_select(void)
1067 struct cmds *cmd;
1069 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1070 if (!strcmp(cmd->cmd, ministate.buf)) {
1071 exit_minibuffer();
1072 minibuffer_hist_save_entry();
1073 cmd->fn(current_window());
1074 return;
1078 message("No match");
1081 static void
1082 ir_self_insert(void)
1084 minibuffer_self_insert();
1087 static void
1088 ir_select(void)
1090 char buf[1025] = {0};
1091 struct url url;
1092 struct tab *tab;
1094 tab = current_tab();
1096 exit_minibuffer();
1097 minibuffer_hist_save_entry();
1099 /* a bit ugly but... */
1100 memcpy(&url, &tab->url, sizeof(tab->url));
1101 url_set_query(&url, ministate.buf);
1102 url_unparse(&url, buf, sizeof(buf));
1103 load_url_in_tab(tab, buf);
1106 static void
1107 lu_self_insert(void)
1109 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1110 !unicode_isgraph(thiskey.key)) {
1111 global_key_unbound();
1112 return;
1115 minibuffer_self_insert();
1118 static void
1119 lu_select(void)
1121 exit_minibuffer();
1122 minibuffer_hist_save_entry();
1123 load_url_in_tab(current_tab(), ministate.buf);
1126 static void
1127 bp_select(void)
1129 exit_minibuffer();
1130 if (*ministate.buf != '\0')
1131 add_to_bookmarks(ministate.buf);
1132 else
1133 message("Abort.");
1136 static void
1137 yornp_self_insert(void)
1139 if (thiskey.key != 'y' && thiskey.key != 'n') {
1140 message("Please answer y or n");
1141 return;
1144 yornp_cb(thiskey.key == 'y', current_tab()->id);
1145 exit_minibuffer();
1148 static void
1149 yornp_abort(void)
1151 yornp_cb(0, current_tab()->id);
1152 exit_minibuffer();
1155 static struct vline *
1156 nth_line(struct window *window, size_t n)
1158 struct vline *vl;
1159 size_t i;
1161 i = 0;
1162 TAILQ_FOREACH(vl, &window->head, vlines) {
1163 if (i == n)
1164 return vl;
1165 i++;
1168 /* unreachable */
1169 abort();
1172 static struct tab *
1173 current_tab(void)
1175 struct tab *t;
1177 TAILQ_FOREACH(t, &tabshead, tabs) {
1178 if (t->flags & TAB_CURRENT)
1179 return t;
1182 /* unreachable */
1183 abort();
1186 static struct window *
1187 current_window(void)
1189 if (in_minibuffer)
1190 return &ministate.window;
1191 return &current_tab()->window;
1194 static int
1195 readkey(void)
1197 uint32_t state = 0;
1199 if ((thiskey.key = wgetch(body)) == ERR)
1200 return 0;
1202 thiskey.meta = thiskey.key == 27;
1203 if (thiskey.meta) {
1204 thiskey.key = wgetch(body);
1205 if (thiskey.key == ERR || thiskey.key == 27) {
1206 thiskey.meta = 0;
1207 thiskey.key = 27;
1211 thiskey.cp = 0;
1212 if ((unsigned int)thiskey.key < UINT8_MAX) {
1213 while (1) {
1214 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1215 break;
1216 if ((thiskey.key = wgetch(body)) == ERR) {
1217 message("Error decoding user input");
1218 return 0;
1223 return 1;
1226 static void
1227 dispatch_stdio(int fd, short ev, void *d)
1229 struct keymap *k;
1230 const char *keyname;
1231 char tmp[5] = {0};
1233 if (!readkey())
1234 return;
1236 if (keybuf[0] != '\0')
1237 strlcat(keybuf, " ", sizeof(keybuf));
1238 if (thiskey.meta)
1239 strlcat(keybuf, "M-", sizeof(keybuf));
1240 if (thiskey.cp != 0) {
1241 utf8_encode(thiskey.cp, tmp);
1242 strlcat(keybuf, tmp, sizeof(keybuf));
1243 } else {
1244 if ((keyname = unkbd(thiskey.key)) != NULL)
1245 strlcat(keybuf, keyname, sizeof(keybuf));
1246 else {
1247 tmp[0] = thiskey.key;
1248 strlcat(keybuf, tmp, sizeof(keybuf));
1252 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1253 if (k->meta == thiskey.meta &&
1254 k->key == thiskey.key) {
1255 if (k->fn == NULL)
1256 current_map = &k->map;
1257 else {
1258 current_map = base_map;
1259 strlcpy(keybuf, "", sizeof(keybuf));
1260 k->fn(current_window());
1262 goto done;
1266 if (current_map->unhandled_input != NULL)
1267 current_map->unhandled_input();
1268 else {
1269 global_key_unbound();
1272 strlcpy(keybuf, "", sizeof(keybuf));
1273 current_map = base_map;
1275 done:
1276 if (side_window)
1277 recompute_help();
1279 redraw_tab(current_tab());
1282 static void
1283 handle_clear_minibuf(int fd, short ev, void *d)
1285 free(ministate.curmesg);
1286 ministate.curmesg = NULL;
1288 redraw_minibuffer();
1289 if (in_minibuffer) {
1290 wrefresh(body);
1291 wrefresh(minibuf);
1292 } else {
1293 wrefresh(minibuf);
1294 wrefresh(body);
1298 static void
1299 handle_resize(int sig, short ev, void *d)
1301 struct tab *tab;
1303 endwin();
1304 refresh();
1305 clear();
1307 /* move and resize the windows, in reverse order! */
1309 mvwin(minibuf, LINES-1, 0);
1310 wresize(minibuf, 1, COLS);
1312 mvwin(modeline, LINES-2, 0);
1313 wresize(modeline, 1, COLS);
1315 body_lines = LINES-3;
1316 body_cols = COLS;
1318 if (side_window) {
1319 help_cols = 0.3 * COLS;
1320 help_lines = LINES-3;
1321 mvwin(help, 1, 0);
1322 wresize(help, help_lines, help_cols);
1324 wrap_page(&helpwin, help_cols);
1326 body_cols = COLS - help_cols - 1;
1327 mvwin(body, 1, help_cols);
1328 } else
1329 mvwin(body, 1, 0);
1331 wresize(body, body_lines, body_cols);
1333 wresize(tabline, 1, COLS);
1335 tab = current_tab();
1337 wrap_page(&tab->window, body_cols);
1338 redraw_tab(tab);
1341 static int
1342 wrap_page(struct window *window, int width)
1344 struct line *l;
1345 const struct line *orig;
1346 struct vline *vl;
1347 const char *prfx;
1349 orig = window->current_line == NULL
1350 ? NULL
1351 : window->current_line->parent;
1352 window->current_line = NULL;
1354 window->curs_y = 0;
1355 window->line_off = 0;
1357 empty_vlist(window);
1359 TAILQ_FOREACH(l, &window->page.head, lines) {
1360 prfx = line_prefixes[l->type].prfx1;
1361 switch (l->type) {
1362 case LINE_TEXT:
1363 case LINE_LINK:
1364 case LINE_TITLE_1:
1365 case LINE_TITLE_2:
1366 case LINE_TITLE_3:
1367 case LINE_ITEM:
1368 case LINE_QUOTE:
1369 case LINE_PRE_START:
1370 case LINE_PRE_END:
1371 wrap_text(window, prfx, l, width);
1372 break;
1373 case LINE_PRE_CONTENT:
1374 hardwrap_text(window, l, width);
1375 break;
1378 if (orig == l && window->current_line == NULL) {
1379 window->line_off = window->line_max-1;
1380 window->current_line = TAILQ_LAST(&window->head, vhead);
1382 while (1) {
1383 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1384 if (vl == NULL || vl->parent != orig)
1385 break;
1386 window->current_line = vl;
1387 window->line_off--;
1392 if (window->current_line == NULL)
1393 window->current_line = TAILQ_FIRST(&window->head);
1395 return 1;
1398 static void
1399 print_vline(WINDOW *window, struct vline *vl)
1401 const char *text = vl->line;
1402 const char *prfx;
1403 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1404 int text_face = line_faces[vl->parent->type].text_prop;
1406 if (!vl->flags)
1407 prfx = line_prefixes[vl->parent->type].prfx1;
1408 else
1409 prfx = line_prefixes[vl->parent->type].prfx2;
1411 if (text == NULL)
1412 text = "";
1414 wattron(window, prefix_face);
1415 wprintw(window, "%s", prfx);
1416 wattroff(window, prefix_face);
1418 wattron(window, text_face);
1419 wprintw(window, "%s", text);
1420 wattroff(window, text_face);
1423 static void
1424 redraw_tabline(void)
1426 struct tab *tab;
1427 size_t toskip;
1428 int current, x, y, truncated;
1429 const char *title;
1430 char buf[25];
1432 toskip = 0;
1433 x = 1;
1434 TAILQ_FOREACH(tab, &tabshead, tabs) {
1435 x += sizeof(buf) + 1;
1436 toskip++;
1437 if (tab->flags & TAB_CURRENT)
1438 break;
1440 if (x < COLS-2)
1441 toskip = 0;
1442 else
1443 toskip--;
1445 werase(tabline);
1446 wattron(tabline, tab_face.background);
1447 wprintw(tabline, toskip == 0 ? " " : "<");
1448 wattroff(tabline, tab_face.background);
1450 truncated = 0;
1451 TAILQ_FOREACH(tab, &tabshead, tabs) {
1452 if (truncated)
1453 break;
1454 if (toskip != 0) {
1455 toskip--;
1456 continue;
1459 getyx(tabline, y, x);
1460 if (x + sizeof(buf)+2 >= (size_t)COLS)
1461 truncated = 1;
1463 current = tab->flags & TAB_CURRENT;
1465 if (*(title = tab->window.page.title) == '\0')
1466 title = tab->hist_cur->h;
1468 strlcpy(buf, " ", sizeof(buf));
1469 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1470 /* truncation happens */
1471 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1472 } else {
1473 /* pad with spaces */
1474 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1475 /* nop */ ;
1478 if (current)
1479 wattron(tabline, tab_face.current_tab);
1480 else
1481 wattron(tabline, tab_face.tab);
1483 wprintw(tabline, "%s", buf);
1484 if (TAILQ_NEXT(tab, tabs) != NULL)
1485 wprintw(tabline, " ");
1487 if (current)
1488 wattroff(tabline, tab_face.current_tab);
1489 else
1490 wattroff(tabline, tab_face.tab);
1493 wattron(tabline, tab_face.background);
1494 for (; x < COLS; ++x)
1495 waddch(tabline, ' ');
1496 if (truncated)
1497 mvwprintw(tabline, 0, COLS-1, ">");
1500 static void
1501 redraw_window(WINDOW *win, int height, struct window *window)
1503 struct vline *vl;
1504 int l;
1506 werase(win);
1508 window->line_off = MIN(window->line_max-1, window->line_off);
1509 if (TAILQ_EMPTY(&window->head))
1510 return;
1512 l = 0;
1513 vl = nth_line(window, window->line_off);
1514 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1515 wmove(win, l, 0);
1516 print_vline(win, vl);
1517 l++;
1518 if (l == height)
1519 break;
1522 wmove(win, window->curs_y, window->curs_x);
1525 static void
1526 redraw_help(void)
1528 redraw_window(help, help_lines, &helpwin);
1531 static void
1532 redraw_body(struct tab *tab)
1534 redraw_window(body, body_lines, &tab->window);
1537 static inline char
1538 trust_status_char(enum trust_state ts)
1540 switch (ts) {
1541 case TS_UNKNOWN: return 'u';
1542 case TS_UNTRUSTED: return '!';
1543 case TS_TRUSTED: return 'v';
1544 case TS_VERIFIED: return 'V';
1548 static void
1549 redraw_modeline(struct tab *tab)
1551 double pct;
1552 int x, y, max_x, max_y;
1553 const char *mode = tab->window.page.name;
1554 const char *spin = "-\\|/";
1556 werase(modeline);
1557 wattron(modeline, A_REVERSE);
1558 wmove(modeline, 0, 0);
1560 wprintw(modeline, "-%c%c %s ",
1561 spin[tab->loading_anim_step],
1562 trust_status_char(tab->trust),
1563 mode == NULL ? "(none)" : mode);
1565 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1567 if (tab->window.line_max <= (size_t)body_lines)
1568 wprintw(modeline, "All ");
1569 else if (tab->window.line_off == 0)
1570 wprintw(modeline, "Top ");
1571 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1572 wprintw(modeline, "Bottom ");
1573 else
1574 wprintw(modeline, "%.0f%% ", pct);
1576 wprintw(modeline, "%d/%d %s ",
1577 tab->window.line_off + tab->window.curs_y,
1578 tab->window.line_max,
1579 tab->hist_cur->h);
1581 getyx(modeline, y, x);
1582 getmaxyx(modeline, max_y, max_x);
1584 (void)y;
1585 (void)max_y;
1587 for (; x < max_x; ++x)
1588 waddstr(modeline, "-");
1591 static void
1592 redraw_minibuffer(void)
1594 struct tab *tab;
1595 size_t off_y, off_x = 0;
1596 char *start, *c;
1598 werase(minibuf);
1600 if (in_minibuffer) {
1601 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1602 if (ministate.hist_cur != NULL)
1603 wprintw(minibuf, "(%zu/%zu) ",
1604 ministate.hist_off + 1,
1605 ministate.history->len);
1607 getyx(minibuf, off_y, off_x);
1609 start = ministate.hist_cur != NULL
1610 ? ministate.hist_cur->h
1611 : ministate.buf;
1612 c = utf8_nth(ministate.window.current_line->line,
1613 ministate.window.cpoff);
1614 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1615 start = utf8_next_cp(start);
1618 waddstr(minibuf, start);
1621 if (ministate.curmesg != NULL)
1622 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1623 ministate.curmesg);
1625 if (!in_minibuffer && ministate.curmesg == NULL)
1626 waddstr(minibuf, keybuf);
1628 /* If nothing else, show the URL at point */
1629 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1630 tab = current_tab();
1631 if (tab->window.current_line != NULL &&
1632 tab->window.current_line->parent->type == LINE_LINK)
1633 waddstr(minibuf, tab->window.current_line->parent->alt);
1636 if (in_minibuffer)
1637 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1640 static void
1641 redraw_tab(struct tab *tab)
1643 if (side_window) {
1644 redraw_help();
1645 wnoutrefresh(help);
1648 redraw_tabline();
1649 redraw_body(tab);
1650 redraw_modeline(tab);
1651 redraw_minibuffer();
1653 wnoutrefresh(tabline);
1654 wnoutrefresh(modeline);
1656 if (in_minibuffer) {
1657 wnoutrefresh(body);
1658 wnoutrefresh(minibuf);
1659 } else {
1660 wnoutrefresh(minibuf);
1661 wnoutrefresh(body);
1664 doupdate();
1667 static void
1668 emit_help_item(char *prfx, void *fn)
1670 struct line *l;
1671 struct cmds *cmd;
1673 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1674 if (fn == cmd->fn)
1675 break;
1677 assert(cmd != NULL);
1679 if ((l = calloc(1, sizeof(*l))) == NULL)
1680 abort();
1682 l->type = LINE_TEXT;
1683 l->alt = NULL;
1685 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1687 if (TAILQ_EMPTY(&helpwin.page.head))
1688 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1689 else
1690 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1693 static void
1694 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1696 struct keymap *k;
1697 char p[32];
1698 const char *kn;
1700 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1701 strlcpy(p, prfx, sizeof(p));
1702 if (*p != '\0')
1703 strlcat(p, " ", sizeof(p));
1704 if (k->meta)
1705 strlcat(p, "M-", sizeof(p));
1706 if ((kn = unkbd(k->key)) != NULL)
1707 strlcat(p, kn, sizeof(p));
1708 else
1709 strlcat(p, keyname(k->key), sizeof(p));
1711 if (k->fn == NULL)
1712 rec_compute_help(&k->map, p, sizeof(p));
1713 else
1714 emit_help_item(p, k->fn);
1718 static void
1719 recompute_help(void)
1721 char p[32] = { 0 };
1723 empty_vlist(&helpwin);
1724 empty_linelist(&helpwin);
1725 rec_compute_help(current_map, p, sizeof(p));
1726 wrap_page(&helpwin, help_cols);
1729 static void
1730 vmessage(const char *fmt, va_list ap)
1732 if (evtimer_pending(&clminibufev, NULL))
1733 evtimer_del(&clminibufev);
1734 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1735 evtimer_add(&clminibufev, &clminibufev_timer);
1737 free(ministate.curmesg);
1739 /* TODO: what to do if the allocation fails here? */
1740 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1741 ministate.curmesg = NULL;
1743 redraw_minibuffer();
1744 if (in_minibuffer) {
1745 wrefresh(body);
1746 wrefresh(minibuf);
1747 } else {
1748 wrefresh(minibuf);
1749 wrefresh(body);
1753 static void
1754 message(const char *fmt, ...)
1756 va_list ap;
1758 va_start(ap, fmt);
1759 vmessage(fmt, ap);
1760 va_end(ap);
1763 static void
1764 start_loading_anim(struct tab *tab)
1766 if (tab->loading_anim)
1767 return;
1768 tab->loading_anim = 1;
1769 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1770 evtimer_add(&tab->loadingev, &loadingev_timer);
1773 static void
1774 update_loading_anim(int fd, short ev, void *d)
1776 struct tab *tab = d;
1778 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1780 if (tab->flags & TAB_CURRENT) {
1781 redraw_modeline(tab);
1782 wrefresh(modeline);
1783 wrefresh(body);
1784 if (in_minibuffer)
1785 wrefresh(minibuf);
1788 evtimer_add(&tab->loadingev, &loadingev_timer);
1791 static void
1792 stop_loading_anim(struct tab *tab)
1794 if (!tab->loading_anim)
1795 return;
1796 evtimer_del(&tab->loadingev);
1797 tab->loading_anim = 0;
1798 tab->loading_anim_step = 0;
1800 if (!(tab->flags & TAB_CURRENT))
1801 return;
1803 redraw_modeline(tab);
1805 wrefresh(modeline);
1806 wrefresh(body);
1807 if (in_minibuffer)
1808 wrefresh(minibuf);
1811 static void
1812 load_url_in_tab(struct tab *tab, const char *url)
1814 empty_vlist(&tab->window);
1815 message("Loading %s...", url);
1816 start_loading_anim(tab);
1817 load_url(tab, url);
1819 tab->window.curs_x = 0;
1820 tab->window.curs_y = 0;
1821 redraw_tab(tab);
1824 static void
1825 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1826 void (*abortfn)(void), struct histhead *hist)
1828 in_minibuffer = 1;
1829 base_map = &minibuffer_map;
1830 current_map = &minibuffer_map;
1832 base_map->unhandled_input = self_insert_fn;
1834 ministate.donefn = donefn;
1835 ministate.abortfn = abortfn;
1836 memset(ministate.buf, 0, sizeof(ministate.buf));
1837 ministate.window.current_line = &ministate.vline;
1838 ministate.window.current_line->line = ministate.buf;
1839 ministate.window.cpoff = 0;
1840 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1842 ministate.history = hist;
1843 ministate.hist_cur = NULL;
1844 ministate.hist_off = 0;
1847 static void
1848 exit_minibuffer(void)
1850 werase(minibuf);
1852 in_minibuffer = 0;
1853 base_map = &global_map;
1854 current_map = &global_map;
1857 static void
1858 switch_to_tab(struct tab *tab)
1860 struct tab *t;
1862 TAILQ_FOREACH(t, &tabshead, tabs) {
1863 t->flags &= ~TAB_CURRENT;
1866 tab->flags |= TAB_CURRENT;
1869 static struct tab *
1870 new_tab(const char *url)
1872 struct tab *tab;
1874 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1875 event_loopbreak();
1876 return NULL;
1879 TAILQ_INIT(&tab->hist.head);
1881 TAILQ_INIT(&tab->window.head);
1883 tab->id = tab_counter++;
1884 switch_to_tab(tab);
1886 if (TAILQ_EMPTY(&tabshead))
1887 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1888 else
1889 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1891 load_url_in_tab(tab, url);
1892 return tab;
1895 static void
1896 usage(void)
1898 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1901 int
1902 ui_init(int argc, char * const *argv)
1904 const char *url = NEW_TAB_URL;
1905 int ch;
1907 while ((ch = getopt(argc, argv, "")) != -1) {
1908 switch (ch) {
1909 default:
1910 usage();
1911 return 0;
1914 argc -= optind;
1915 argv += optind;
1917 if (argc != 0)
1918 url = argv[0];
1920 setlocale(LC_ALL, "");
1922 TAILQ_INIT(&global_map.m);
1923 global_map.unhandled_input = global_key_unbound;
1925 TAILQ_INIT(&minibuffer_map.m);
1927 TAILQ_INIT(&eecmd_history.head);
1928 TAILQ_INIT(&ir_history.head);
1929 TAILQ_INIT(&lu_history.head);
1931 ministate.line.type = LINE_TEXT;
1932 ministate.vline.parent = &ministate.line;
1933 ministate.window.current_line = &ministate.vline;
1935 /* initialize help window */
1936 TAILQ_INIT(&helpwin.head);
1938 base_map = &global_map;
1939 current_map = &global_map;
1940 load_default_keys();
1942 initscr();
1943 raw();
1944 noecho();
1946 nonl();
1947 intrflush(stdscr, FALSE);
1949 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1950 return 0;
1951 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1952 return 0;
1953 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1954 return 0;
1955 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1956 return 0;
1957 if ((help = newwin(1, 1, 1, 0)) == NULL)
1958 return 0;
1960 body_lines = LINES-3;
1961 body_cols = COLS;
1963 keypad(body, TRUE);
1964 scrollok(body, TRUE);
1966 /* non-blocking input */
1967 wtimeout(body, 0);
1969 mvwprintw(body, 0, 0, "");
1971 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1972 event_add(&stdioev, NULL);
1974 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1975 signal_add(&winchev, NULL);
1977 new_tab(url);
1979 return 1;
1982 void
1983 ui_on_tab_loaded(struct tab *tab)
1985 stop_loading_anim(tab);
1986 message("Loaded %s", tab->hist_cur->h);
1988 redraw_tabline();
1989 wrefresh(tabline);
1990 if (in_minibuffer)
1991 wrefresh(minibuf);
1992 else
1993 wrefresh(body);
1996 void
1997 ui_on_tab_refresh(struct tab *tab)
1999 wrap_page(&tab->window, body_cols);
2000 if (tab->flags & TAB_CURRENT) {
2001 restore_cursor(&tab->window);
2002 redraw_tab(tab);
2006 void
2007 ui_require_input(struct tab *tab, int hide)
2009 /* TODO: hard-switching to another tab is ugly */
2010 switch_to_tab(tab);
2012 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2013 &ir_history);
2014 strlcpy(ministate.prompt, "Input required: ",
2015 sizeof(ministate.prompt));
2016 redraw_tab(tab);
2019 void
2020 ui_yornp(const char *prompt, void (*fn)(int, unsigned int))
2022 size_t len;
2024 yornp_cb = fn;
2025 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2026 yornp_abort, NULL);
2028 len = sizeof(ministate.prompt);
2029 strlcpy(ministate.prompt, prompt, len);
2030 strlcat(ministate.prompt, " (y or n) ", len);
2031 redraw_tab(current_tab());
2034 void
2035 ui_notify(const char *fmt, ...)
2037 va_list ap;
2039 va_start(ap, fmt);
2040 vmessage(fmt, ap);
2041 va_end(ap);
2044 void
2045 ui_end(void)
2047 endwin();