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_list_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 session_new_tab_cb(const char*);
151 static void usage(void);
153 static struct { short meta; int key; uint32_t cp; } thiskey;
155 static WINDOW *tabline, *body, *modeline, *minibuf;
156 static int body_lines, body_cols;
158 static WINDOW *help;
159 static struct window helpwin;
160 static int help_lines, help_cols;
162 static int side_window;
164 static struct event clminibufev;
165 static struct timeval clminibufev_timer = { 5, 0 };
166 static struct timeval loadingev_timer = { 0, 250000 };
168 static uint32_t tab_counter;
170 static char keybuf[64];
172 static void (*yornp_cb)(int, unsigned int);
173 static unsigned int yornp_data;
175 struct kmap global_map,
176 minibuffer_map,
177 *current_map,
178 *base_map;
180 static struct histhead eecmd_history,
181 ir_history,
182 lu_history;
184 static int in_minibuffer;
186 static struct {
187 char *curmesg;
189 char prompt[64];
190 void (*donefn)(void);
191 void (*abortfn)(void);
193 char buf[1025];
194 struct line line;
195 struct vline vline;
196 struct window window;
198 struct histhead *history;
199 struct hist *hist_cur;
200 size_t hist_off;
201 } ministate;
203 struct lineprefix {
204 const char *prfx1;
205 const char *prfx2;
206 } line_prefixes[] = {
207 [LINE_TEXT] = { "", "" },
208 [LINE_LINK] = { "=> ", " " },
209 [LINE_TITLE_1] = { "# ", " " },
210 [LINE_TITLE_2] = { "## ", " " },
211 [LINE_TITLE_3] = { "### ", " " },
212 [LINE_ITEM] = { "* ", " " },
213 [LINE_QUOTE] = { "> ", " " },
214 [LINE_PRE_START] = { "```", " " },
215 [LINE_PRE_CONTENT] = { "", "" },
216 [LINE_PRE_END] = { "```", "```" },
217 };
219 static struct line_face {
220 int prefix_prop;
221 int text_prop;
222 } line_faces[] = {
223 [LINE_TEXT] = { 0, 0 },
224 [LINE_LINK] = { 0, A_UNDERLINE },
225 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
226 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
227 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
228 [LINE_ITEM] = { 0, 0 },
229 [LINE_QUOTE] = { 0, A_DIM },
230 [LINE_PRE_START] = { 0, 0 },
231 [LINE_PRE_CONTENT] = { 0, 0 },
232 [LINE_PRE_END] = { 0, 0 },
233 };
235 static struct tab_face {
236 int background, tab, current_tab;
237 } tab_face = {
238 A_REVERSE, A_REVERSE, A_NORMAL
239 };
241 static inline void
242 global_set_key(const char *key, void (*fn)(struct window*))
244 if (!kmap_define_key(&global_map, key, fn))
245 _exit(1);
248 static inline void
249 minibuffer_set_key(const char *key, void (*fn)(struct window*))
251 if (!kmap_define_key(&minibuffer_map, key, fn))
252 _exit(1);
255 static void
256 load_default_keys(void)
258 /* === global map === */
260 /* emacs */
261 global_set_key("C-p", cmd_previous_line);
262 global_set_key("C-n", cmd_next_line);
263 global_set_key("C-f", cmd_forward_char);
264 global_set_key("C-b", cmd_backward_char);
265 global_set_key("M-{", cmd_backward_paragraph);
266 global_set_key("M-}", cmd_forward_paragraph);
267 global_set_key("C-a", cmd_move_beginning_of_line);
268 global_set_key("C-e", cmd_move_end_of_line);
270 global_set_key("M-v", cmd_scroll_up);
271 global_set_key("C-v", cmd_scroll_down);
272 global_set_key("M-space", cmd_scroll_up);
273 global_set_key("space", cmd_scroll_down);
275 global_set_key("M-<", cmd_beginning_of_buffer);
276 global_set_key("M->", cmd_end_of_buffer);
278 global_set_key("C-x C-c", cmd_kill_telescope);
280 global_set_key("C-g", cmd_clear_minibuf);
282 global_set_key("M-x", cmd_execute_extended_command);
283 global_set_key("C-x C-f", cmd_load_url);
284 global_set_key("C-x M-f", cmd_load_current_url);
286 global_set_key("C-x t 0", cmd_tab_close);
287 global_set_key("C-x t 1", cmd_tab_close_other);
288 global_set_key("C-x t 2", cmd_tab_new);
289 global_set_key("C-x t o", cmd_tab_next);
290 global_set_key("C-x t O", cmd_tab_previous);
291 global_set_key("C-x t m", cmd_tab_move);
292 global_set_key("C-x t M", cmd_tab_move_to);
294 global_set_key("C-M-b", cmd_previous_page);
295 global_set_key("C-M-f", cmd_next_page);
297 global_set_key("<f7> a", cmd_bookmark_page);
298 global_set_key("<f7> <f7>", cmd_list_bookmarks);
300 /* vi/vi-like */
301 global_set_key("k", cmd_previous_line);
302 global_set_key("j", cmd_next_line);
303 global_set_key("l", cmd_forward_char);
304 global_set_key("h", cmd_backward_char);
305 global_set_key("{", cmd_backward_paragraph);
306 global_set_key("}", cmd_forward_paragraph);
307 global_set_key("^", cmd_move_beginning_of_line);
308 global_set_key("$", cmd_move_end_of_line);
310 global_set_key("K", cmd_scroll_line_up);
311 global_set_key("J", cmd_scroll_line_down);
313 global_set_key("g g", cmd_beginning_of_buffer);
314 global_set_key("G", cmd_end_of_buffer);
316 global_set_key("g D", cmd_tab_close);
317 global_set_key("g N", cmd_tab_new);
318 global_set_key("g t", cmd_tab_next);
319 global_set_key("g T", cmd_tab_previous);
320 global_set_key("g M-t", cmd_tab_move);
321 global_set_key("g M-T", cmd_tab_move_to);
323 global_set_key("H", cmd_previous_page);
324 global_set_key("L", cmd_next_page);
326 /* tmp */
327 global_set_key("q", cmd_kill_telescope);
329 global_set_key("esc", cmd_clear_minibuf);
331 global_set_key(":", cmd_execute_extended_command);
333 /* cua */
334 global_set_key("<up>", cmd_previous_line);
335 global_set_key("<down>", cmd_next_line);
336 global_set_key("<right>", cmd_forward_char);
337 global_set_key("<left>", cmd_backward_char);
338 global_set_key("<prior>", cmd_scroll_up);
339 global_set_key("<next>", cmd_scroll_down);
341 global_set_key("M-<left>", cmd_previous_page);
342 global_set_key("M-<right>", cmd_next_page);
344 /* "ncurses standard" */
345 global_set_key("C-l", cmd_redraw);
347 /* global */
348 global_set_key("<f1>", cmd_toggle_help);
349 global_set_key("C-m", cmd_push_button);
350 global_set_key("M-enter", cmd_push_button_new_tab);
351 global_set_key("M-tab", cmd_previous_button);
352 global_set_key("tab", cmd_next_button);
354 /* === minibuffer map === */
355 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
356 minibuffer_set_key("C-g", cmd_mini_abort);
357 minibuffer_set_key("esc", cmd_mini_abort);
358 minibuffer_set_key("C-d", cmd_mini_delete_char);
359 minibuffer_set_key("del", cmd_mini_delete_backward_char);
360 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
361 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
363 minibuffer_set_key("C-b", cmd_backward_char);
364 minibuffer_set_key("C-f", cmd_forward_char);
365 minibuffer_set_key("<left>", cmd_backward_char);
366 minibuffer_set_key("<right>", cmd_forward_char);
367 minibuffer_set_key("C-e", cmd_move_end_of_line);
368 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
369 minibuffer_set_key("<end>", cmd_move_end_of_line);
370 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
371 minibuffer_set_key("C-k", cmd_mini_kill_line);
373 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
374 minibuffer_set_key("M-n", cmd_mini_next_history_element);
375 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
376 minibuffer_set_key("<down>", cmd_mini_next_history_element);
379 static void
380 restore_cursor(struct window *window)
382 struct vline *vl;
383 const char *prfx;
385 vl = window->current_line;
386 if (vl == NULL || vl->line == NULL)
387 window->curs_x = window->cpoff = 0;
388 else
389 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
391 if (vl != NULL) {
392 prfx = line_prefixes[vl->parent->type].prfx1;
393 window->curs_x += utf8_swidth(prfx);
397 static void
398 cmd_previous_line(struct window *window)
400 struct vline *vl;
402 if (window->current_line == NULL
403 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
404 return;
406 if (--window->curs_y < 0) {
407 window->curs_y = 0;
408 cmd_scroll_line_up(window);
409 return;
412 window->current_line = vl;
413 restore_cursor(window);
416 static void
417 cmd_next_line(struct window *window)
419 struct vline *vl;
421 if (window->current_line == NULL
422 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
423 return;
425 if (++window->curs_y > body_lines-1) {
426 window->curs_y = body_lines-1;
427 cmd_scroll_line_down(window);
428 return;
431 window->current_line = vl;
432 restore_cursor(window);
435 static void
436 cmd_backward_char(struct window *window)
438 if (window->cpoff != 0)
439 window->cpoff--;
440 restore_cursor(window);
443 static void
444 cmd_forward_char(struct window *window)
446 window->cpoff++;
447 restore_cursor(window);
450 static void
451 cmd_backward_paragraph(struct window *window)
453 do {
454 if (window->current_line == NULL ||
455 window->current_line == TAILQ_FIRST(&window->head)) {
456 message("No previous paragraph");
457 return;
459 cmd_previous_line(window);
460 } while (window->current_line->line != NULL ||
461 window->current_line->parent->type != LINE_TEXT);
464 static void
465 cmd_forward_paragraph(struct window *window)
467 do {
468 if (window->current_line == NULL ||
469 window->current_line == TAILQ_LAST(&window->head, vhead)) {
470 message("No next paragraph");
471 return;
473 cmd_next_line(window);
474 } while (window->current_line->line != NULL ||
475 window->current_line->parent->type != LINE_TEXT);
478 static void
479 cmd_move_beginning_of_line(struct window *window)
481 window->cpoff = 0;
482 restore_cursor(window);
485 static void
486 cmd_move_end_of_line(struct window *window)
488 struct vline *vl;
490 vl = window->current_line;
491 if (vl->line == NULL)
492 return;
493 window->cpoff = utf8_cplen(vl->line);
494 restore_cursor(window);
497 static void
498 cmd_redraw(struct window *window)
500 handle_resize(0, 0, NULL);
503 static void
504 cmd_scroll_line_up(struct window *window)
506 struct vline *vl;
508 if (window->line_off == 0)
509 return;
511 vl = nth_line(window, --window->line_off);
512 wscrl(body, -1);
513 wmove(body, 0, 0);
514 print_vline(body, vl);
516 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
517 restore_cursor(window);
520 static void
521 cmd_scroll_line_down(struct window *window)
523 struct vline *vl;
525 vl = window->current_line;
526 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
527 return;
528 window->current_line = vl;
530 window->line_off++;
531 wscrl(body, 1);
533 if (window->line_max - window->line_off < (size_t)body_lines)
534 return;
536 vl = nth_line(window, window->line_off + body_lines-1);
537 wmove(body, body_lines-1, 0);
538 print_vline(body, vl);
540 restore_cursor(window);
543 static void
544 cmd_scroll_up(struct window *window)
546 size_t off;
548 off = body_lines+1;
550 for (; off > 0; --off)
551 cmd_scroll_line_up(window);
554 static void
555 cmd_scroll_down(struct window *window)
557 size_t off;
559 off = body_lines+1;
561 for (; off > 0; --off)
562 cmd_scroll_line_down(window);
565 static void
566 cmd_beginning_of_buffer(struct window *window)
568 window->current_line = TAILQ_FIRST(&window->head);
569 window->line_off = 0;
570 window->curs_y = 0;
571 window->cpoff = 0;
572 restore_cursor(window);
575 static void
576 cmd_end_of_buffer(struct window *window)
578 ssize_t off;
580 off = window->line_max - body_lines;
581 off = MAX(0, off);
583 window->line_off = off;
584 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
586 window->current_line = TAILQ_LAST(&window->head, vhead);
587 window->cpoff = body_cols;
588 restore_cursor(window);
591 static void
592 cmd_kill_telescope(struct window *window)
594 save_session();
595 event_loopbreak();
598 static void
599 cmd_push_button(struct window *window)
601 struct vline *vl;
602 size_t nth;
604 nth = window->line_off + window->curs_y;
605 if (nth >= window->line_max)
606 return;
607 vl = nth_line(window, nth);
608 if (vl->parent->type != LINE_LINK)
609 return;
611 load_url_in_tab(current_tab(), vl->parent->alt);
614 static void
615 cmd_push_button_new_tab(struct window *window)
617 struct vline *vl;
618 size_t nth;
620 nth = window->line_off + window->curs_y;
621 if (nth > window->line_max)
622 return;
623 vl = nth_line(window, nth);
624 if (vl->parent->type != LINE_LINK)
625 return;
627 new_tab(vl->parent->alt);
630 static void
631 cmd_previous_button(struct window *window)
633 do {
634 if (window->current_line == NULL ||
635 window->current_line == TAILQ_FIRST(&window->head)) {
636 message("No previous link");
637 return;
639 cmd_previous_line(window);
640 } while (window->current_line->parent->type != LINE_LINK);
643 static void
644 cmd_next_button(struct window *window)
646 do {
647 if (window->current_line == NULL ||
648 window->current_line == TAILQ_LAST(&window->head, vhead)) {
649 message("No next link");
650 return;
652 cmd_next_line(window);
653 } while (window->current_line->parent->type != LINE_LINK);
656 static void
657 cmd_previous_page(struct window *window)
659 struct tab *tab = current_tab();
661 if (!load_previous_page(tab))
662 message("No previous page");
663 else
664 start_loading_anim(tab);
667 static void
668 cmd_next_page(struct window *window)
670 struct tab *tab = current_tab();
672 if (!load_next_page(tab))
673 message("No next page");
674 else
675 start_loading_anim(tab);
678 static void
679 cmd_clear_minibuf(struct window *window)
681 handle_clear_minibuf(0, 0, NULL);
684 static void
685 cmd_execute_extended_command(struct window *window)
687 size_t len;
689 if (in_minibuffer) {
690 message("We don't have enable-recursive-minibuffers");
691 return;
694 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
695 &eecmd_history);
697 len = sizeof(ministate.prompt);
698 strlcpy(ministate.prompt, "", len);
700 if (thiskey.meta)
701 strlcat(ministate.prompt, "M-", len);
703 strlcat(ministate.prompt, keyname(thiskey.key), len);
704 strlcat(ministate.prompt, " ", len);
707 static void
708 cmd_tab_close(struct window *window)
710 struct tab *tab, *t;
712 tab = current_tab();
713 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
714 TAILQ_NEXT(tab, tabs) == NULL) {
715 message("Can't close the only tab.");
716 return;
719 if (evtimer_pending(&tab->loadingev, NULL))
720 evtimer_del(&tab->loadingev);
722 stop_tab(tab);
724 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
725 t = TAILQ_NEXT(tab, tabs);
726 TAILQ_REMOVE(&tabshead, tab, tabs);
727 free(tab);
729 switch_to_tab(t);
732 static void
733 cmd_tab_close_other(struct window *window)
735 struct tab *t, *i;
737 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
738 if (t->flags & TAB_CURRENT)
739 continue;
741 stop_tab(t);
742 TAILQ_REMOVE(&tabshead, t, tabs);
743 free(t);
747 static void
748 cmd_tab_new(struct window *window)
750 new_tab(NEW_TAB_URL);
753 static void
754 cmd_tab_next(struct window *window)
756 struct tab *tab, *t;
758 tab = current_tab();
759 tab->flags &= ~TAB_CURRENT;
761 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
762 t = TAILQ_FIRST(&tabshead);
763 t->flags |= TAB_CURRENT;
766 static void
767 cmd_tab_previous(struct window *window)
769 struct tab *tab, *t;
771 tab = current_tab();
772 tab->flags &= ~TAB_CURRENT;
774 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
775 t = TAILQ_LAST(&tabshead, tabshead);
776 t->flags |= TAB_CURRENT;
779 static void
780 cmd_tab_move(struct window *window)
782 struct tab *tab, *t;
784 tab = current_tab();
785 t = TAILQ_NEXT(tab, tabs);
786 TAILQ_REMOVE(&tabshead, tab, tabs);
788 if (t == NULL)
789 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
790 else
791 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
794 static void
795 cmd_tab_move_to(struct window *window)
797 struct tab *tab, *t;
799 tab = current_tab();
800 t = TAILQ_PREV(tab, tabshead, tabs);
801 TAILQ_REMOVE(&tabshead, tab, tabs);
803 if (t == NULL) {
804 if (TAILQ_EMPTY(&tabshead))
805 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
806 else
807 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
808 } else
809 TAILQ_INSERT_BEFORE(t, tab, tabs);
812 static void
813 cmd_load_url(struct window *window)
815 if (in_minibuffer) {
816 message("We don't have enable-recursive-minibuffers");
817 return;
820 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
821 &lu_history);
822 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
825 static void
826 cmd_load_current_url(struct window *window)
828 struct tab *tab = current_tab();
830 if (in_minibuffer) {
831 message("We don't have enable-recursive-minibuffers");
832 return;
835 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
836 &lu_history);
837 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
838 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
839 ministate.window.cpoff = utf8_cplen(ministate.buf);
842 static void
843 cmd_bookmark_page(struct window *window)
845 struct tab *tab = current_tab();
847 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
848 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
849 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
850 ministate.window.cpoff = utf8_cplen(ministate.buf);
853 static void
854 cmd_list_bookmarks(struct window *window)
856 load_url_in_tab(current_tab(), "about:bookmarks");
859 static void
860 cmd_toggle_help(struct window *window)
862 side_window = !side_window;
863 if (side_window)
864 recompute_help();
866 /* ugly hack, but otherwise the window doesn't get updated
867 * until I call handle_resize a second time (i.e. C-l). I
868 * will be happy to know why something like this is needed. */
869 handle_resize(0, 0, NULL);
870 handle_resize(0, 0, NULL);
873 static void
874 cmd_mini_delete_char(struct window *window)
876 char *c, *n;
878 if (!in_minibuffer) {
879 message("text is read-only");
880 return;
883 minibuffer_taint_hist();
885 c = utf8_nth(window->current_line->line, window->cpoff);
886 if (*c == '\0')
887 return;
888 n = utf8_next_cp(c);
890 memmove(c, n, strlen(n)+1);
893 static void
894 cmd_mini_delete_backward_char(struct window *window)
896 char *c, *p, *start;
898 if (!in_minibuffer) {
899 message("text is read-only");
900 return;
903 minibuffer_taint_hist();
905 c = utf8_nth(window->current_line->line, window->cpoff);
906 start = window->current_line->line;
907 if (c == start)
908 return;
909 p = utf8_prev_cp(c-1, start);
911 memmove(p, c, strlen(c)+1);
912 window->cpoff--;
915 static void
916 cmd_mini_kill_line(struct window *window)
918 char *c;
920 if (!in_minibuffer) {
921 message("text is read-only");
922 return;
925 minibuffer_taint_hist();
926 c = utf8_nth(window->current_line->line, window->cpoff);
927 *c = '\0';
930 static void
931 cmd_mini_abort(struct window *window)
933 if (!in_minibuffer)
934 return;
936 ministate.abortfn();
939 static void
940 cmd_mini_complete_and_exit(struct window *window)
942 if (!in_minibuffer)
943 return;
945 minibuffer_taint_hist();
946 ministate.donefn();
949 static void
950 cmd_mini_previous_history_element(struct window *window)
952 if (ministate.history == NULL) {
953 message("No history");
954 return;
957 if (ministate.hist_cur == NULL ||
958 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
959 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
960 ministate.hist_off = ministate.history->len - 1;
961 if (ministate.hist_cur == NULL)
962 message("No prev item");
963 } else {
964 ministate.hist_off--;
967 if (ministate.hist_cur != NULL)
968 window->current_line->line = ministate.hist_cur->h;
971 static void
972 cmd_mini_next_history_element(struct window *window)
974 if (ministate.history == NULL) {
975 message("No history");
976 return;
979 if (ministate.hist_cur == NULL ||
980 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
981 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
982 ministate.hist_off = 0;
983 if (ministate.hist_cur == NULL)
984 message("No next item");
985 } else {
986 ministate.hist_off++;
989 if (ministate.hist_cur != NULL)
990 window->current_line->line = ministate.hist_cur->h;
993 static void
994 global_key_unbound(void)
996 message("%s is undefined", keybuf);
999 static void
1000 minibuffer_hist_save_entry(void)
1002 struct hist *hist;
1004 if (ministate.history == NULL)
1005 return;
1007 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1008 abort();
1010 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1012 if (TAILQ_EMPTY(&ministate.history->head))
1013 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1014 else
1015 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1016 ministate.history->len++;
1020 * taint the minibuffer cache: if we're currently showing a history
1021 * element, copy that to the current buf and reset the "history
1022 * navigation" thing.
1024 static void
1025 minibuffer_taint_hist(void)
1027 if (ministate.hist_cur == NULL)
1028 return;
1030 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1031 ministate.hist_cur = NULL;
1034 static void
1035 minibuffer_self_insert(void)
1037 char *c, tmp[5] = {0};
1038 size_t len;
1040 minibuffer_taint_hist();
1042 if (thiskey.cp == 0)
1043 return;
1045 len = utf8_encode(thiskey.cp, tmp);
1046 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1047 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1048 return;
1050 memmove(c + len, c, strlen(c)+1);
1051 memcpy(c, tmp, len);
1052 ministate.window.cpoff++;
1055 static void
1056 eecmd_self_insert(void)
1058 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1059 !unicode_isgraph(thiskey.cp)) {
1060 global_key_unbound();
1061 return;
1064 minibuffer_self_insert();
1067 static void
1068 eecmd_select(void)
1070 struct cmds *cmd;
1072 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1073 if (!strcmp(cmd->cmd, ministate.buf)) {
1074 exit_minibuffer();
1075 minibuffer_hist_save_entry();
1076 cmd->fn(current_window());
1077 return;
1081 message("No match");
1084 static void
1085 ir_self_insert(void)
1087 minibuffer_self_insert();
1090 static void
1091 ir_select(void)
1093 char buf[1025] = {0};
1094 struct phos_uri uri;
1095 struct tab *tab;
1097 tab = current_tab();
1099 exit_minibuffer();
1100 minibuffer_hist_save_entry();
1102 /* a bit ugly but... */
1103 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1105 /* XXX: ptc encode! */
1106 memcpy(&uri.query, ministate.buf, strlen(ministate.buf)+1);
1108 phos_serialize_uri(&uri, buf, sizeof(buf));
1109 load_url_in_tab(tab, buf);
1112 static void
1113 lu_self_insert(void)
1115 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1116 !unicode_isgraph(thiskey.key)) {
1117 global_key_unbound();
1118 return;
1121 minibuffer_self_insert();
1124 static void
1125 lu_select(void)
1127 exit_minibuffer();
1128 minibuffer_hist_save_entry();
1129 load_url_in_tab(current_tab(), ministate.buf);
1132 static void
1133 bp_select(void)
1135 exit_minibuffer();
1136 if (*ministate.buf != '\0')
1137 add_to_bookmarks(ministate.buf);
1138 else
1139 message("Abort.");
1142 static void
1143 yornp_self_insert(void)
1145 if (thiskey.key != 'y' && thiskey.key != 'n') {
1146 message("Please answer y or n");
1147 return;
1150 yornp_cb(thiskey.key == 'y', yornp_data);
1151 exit_minibuffer();
1154 static void
1155 yornp_abort(void)
1157 yornp_cb(0, yornp_data);
1158 exit_minibuffer();
1161 static struct vline *
1162 nth_line(struct window *window, size_t n)
1164 struct vline *vl;
1165 size_t i;
1167 i = 0;
1168 TAILQ_FOREACH(vl, &window->head, vlines) {
1169 if (i == n)
1170 return vl;
1171 i++;
1174 /* unreachable */
1175 abort();
1178 static struct tab *
1179 current_tab(void)
1181 struct tab *t;
1183 TAILQ_FOREACH(t, &tabshead, tabs) {
1184 if (t->flags & TAB_CURRENT)
1185 return t;
1188 /* unreachable */
1189 abort();
1192 static struct window *
1193 current_window(void)
1195 if (in_minibuffer)
1196 return &ministate.window;
1197 return &current_tab()->window;
1200 static int
1201 readkey(void)
1203 uint32_t state = 0;
1205 if ((thiskey.key = wgetch(body)) == ERR)
1206 return 0;
1208 thiskey.meta = thiskey.key == 27;
1209 if (thiskey.meta) {
1210 thiskey.key = wgetch(body);
1211 if (thiskey.key == ERR || thiskey.key == 27) {
1212 thiskey.meta = 0;
1213 thiskey.key = 27;
1217 thiskey.cp = 0;
1218 if ((unsigned int)thiskey.key < UINT8_MAX) {
1219 while (1) {
1220 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1221 break;
1222 if ((thiskey.key = wgetch(body)) == ERR) {
1223 message("Error decoding user input");
1224 return 0;
1229 return 1;
1232 static void
1233 dispatch_stdio(int fd, short ev, void *d)
1235 struct keymap *k;
1236 const char *keyname;
1237 char tmp[5] = {0};
1239 if (!readkey())
1240 return;
1242 if (keybuf[0] != '\0')
1243 strlcat(keybuf, " ", sizeof(keybuf));
1244 if (thiskey.meta)
1245 strlcat(keybuf, "M-", sizeof(keybuf));
1246 if (thiskey.cp != 0) {
1247 utf8_encode(thiskey.cp, tmp);
1248 strlcat(keybuf, tmp, sizeof(keybuf));
1249 } else {
1250 if ((keyname = unkbd(thiskey.key)) != NULL)
1251 strlcat(keybuf, keyname, sizeof(keybuf));
1252 else {
1253 tmp[0] = thiskey.key;
1254 strlcat(keybuf, tmp, sizeof(keybuf));
1258 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1259 if (k->meta == thiskey.meta &&
1260 k->key == thiskey.key) {
1261 if (k->fn == NULL)
1262 current_map = &k->map;
1263 else {
1264 current_map = base_map;
1265 strlcpy(keybuf, "", sizeof(keybuf));
1266 k->fn(current_window());
1268 goto done;
1272 if (current_map->unhandled_input != NULL)
1273 current_map->unhandled_input();
1274 else {
1275 global_key_unbound();
1278 strlcpy(keybuf, "", sizeof(keybuf));
1279 current_map = base_map;
1281 done:
1282 if (side_window)
1283 recompute_help();
1285 redraw_tab(current_tab());
1288 static void
1289 handle_clear_minibuf(int fd, short ev, void *d)
1291 free(ministate.curmesg);
1292 ministate.curmesg = NULL;
1294 redraw_minibuffer();
1295 if (in_minibuffer) {
1296 wrefresh(body);
1297 wrefresh(minibuf);
1298 } else {
1299 wrefresh(minibuf);
1300 wrefresh(body);
1304 static void
1305 handle_resize(int sig, short ev, void *d)
1307 struct tab *tab;
1309 endwin();
1310 refresh();
1311 clear();
1313 /* move and resize the windows, in reverse order! */
1315 mvwin(minibuf, LINES-1, 0);
1316 wresize(minibuf, 1, COLS);
1318 mvwin(modeline, LINES-2, 0);
1319 wresize(modeline, 1, COLS);
1321 body_lines = LINES-3;
1322 body_cols = COLS;
1324 if (side_window) {
1325 help_cols = 0.3 * COLS;
1326 help_lines = LINES-3;
1327 mvwin(help, 1, 0);
1328 wresize(help, help_lines, help_cols);
1330 wrap_page(&helpwin, help_cols);
1332 body_cols = COLS - help_cols - 1;
1333 mvwin(body, 1, help_cols);
1334 } else
1335 mvwin(body, 1, 0);
1337 wresize(body, body_lines, body_cols);
1339 wresize(tabline, 1, COLS);
1341 tab = current_tab();
1343 wrap_page(&tab->window, body_cols);
1344 redraw_tab(tab);
1347 static int
1348 wrap_page(struct window *window, int width)
1350 struct line *l;
1351 const struct line *orig;
1352 struct vline *vl;
1353 const char *prfx;
1355 orig = window->current_line == NULL
1356 ? NULL
1357 : window->current_line->parent;
1358 window->current_line = NULL;
1360 window->curs_y = 0;
1361 window->line_off = 0;
1363 empty_vlist(window);
1365 TAILQ_FOREACH(l, &window->page.head, lines) {
1366 prfx = line_prefixes[l->type].prfx1;
1367 switch (l->type) {
1368 case LINE_TEXT:
1369 case LINE_LINK:
1370 case LINE_TITLE_1:
1371 case LINE_TITLE_2:
1372 case LINE_TITLE_3:
1373 case LINE_ITEM:
1374 case LINE_QUOTE:
1375 case LINE_PRE_START:
1376 case LINE_PRE_END:
1377 wrap_text(window, prfx, l, width);
1378 break;
1379 case LINE_PRE_CONTENT:
1380 hardwrap_text(window, l, width);
1381 break;
1384 if (orig == l && window->current_line == NULL) {
1385 window->line_off = window->line_max-1;
1386 window->current_line = TAILQ_LAST(&window->head, vhead);
1388 while (1) {
1389 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1390 if (vl == NULL || vl->parent != orig)
1391 break;
1392 window->current_line = vl;
1393 window->line_off--;
1398 if (window->current_line == NULL)
1399 window->current_line = TAILQ_FIRST(&window->head);
1401 return 1;
1404 static void
1405 print_vline(WINDOW *window, struct vline *vl)
1407 const char *text = vl->line;
1408 const char *prfx;
1409 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1410 int text_face = line_faces[vl->parent->type].text_prop;
1412 if (!vl->flags)
1413 prfx = line_prefixes[vl->parent->type].prfx1;
1414 else
1415 prfx = line_prefixes[vl->parent->type].prfx2;
1417 if (text == NULL)
1418 text = "";
1420 wattron(window, prefix_face);
1421 wprintw(window, "%s", prfx);
1422 wattroff(window, prefix_face);
1424 wattron(window, text_face);
1425 wprintw(window, "%s", text);
1426 wattroff(window, text_face);
1429 static void
1430 redraw_tabline(void)
1432 struct tab *tab;
1433 size_t toskip;
1434 int current, x, y, truncated;
1435 const char *title;
1436 char buf[25];
1438 toskip = 0;
1439 x = 1;
1440 TAILQ_FOREACH(tab, &tabshead, tabs) {
1441 x += sizeof(buf) + 1;
1442 toskip++;
1443 if (tab->flags & TAB_CURRENT)
1444 break;
1446 if (x < COLS-2)
1447 toskip = 0;
1448 else
1449 toskip--;
1451 werase(tabline);
1452 wattron(tabline, tab_face.background);
1453 wprintw(tabline, toskip == 0 ? " " : "<");
1454 wattroff(tabline, tab_face.background);
1456 truncated = 0;
1457 TAILQ_FOREACH(tab, &tabshead, tabs) {
1458 if (truncated)
1459 break;
1460 if (toskip != 0) {
1461 toskip--;
1462 continue;
1465 getyx(tabline, y, x);
1466 if (x + sizeof(buf)+2 >= (size_t)COLS)
1467 truncated = 1;
1469 current = tab->flags & TAB_CURRENT;
1471 if (*(title = tab->window.page.title) == '\0')
1472 title = tab->hist_cur->h;
1474 strlcpy(buf, " ", sizeof(buf));
1475 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1476 /* truncation happens */
1477 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1478 } else {
1479 /* pad with spaces */
1480 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1481 /* nop */ ;
1484 if (current)
1485 wattron(tabline, tab_face.current_tab);
1486 else
1487 wattron(tabline, tab_face.tab);
1489 wprintw(tabline, "%s", buf);
1490 if (TAILQ_NEXT(tab, tabs) != NULL)
1491 wprintw(tabline, " ");
1493 if (current)
1494 wattroff(tabline, tab_face.current_tab);
1495 else
1496 wattroff(tabline, tab_face.tab);
1499 wattron(tabline, tab_face.background);
1500 for (; x < COLS; ++x)
1501 waddch(tabline, ' ');
1502 if (truncated)
1503 mvwprintw(tabline, 0, COLS-1, ">");
1506 static void
1507 redraw_window(WINDOW *win, int height, struct window *window)
1509 struct vline *vl;
1510 int l;
1512 werase(win);
1514 window->line_off = MIN(window->line_max-1, window->line_off);
1515 if (TAILQ_EMPTY(&window->head))
1516 return;
1518 l = 0;
1519 vl = nth_line(window, window->line_off);
1520 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1521 wmove(win, l, 0);
1522 print_vline(win, vl);
1523 l++;
1524 if (l == height)
1525 break;
1528 wmove(win, window->curs_y, window->curs_x);
1531 static void
1532 redraw_help(void)
1534 redraw_window(help, help_lines, &helpwin);
1537 static void
1538 redraw_body(struct tab *tab)
1540 redraw_window(body, body_lines, &tab->window);
1543 static inline char
1544 trust_status_char(enum trust_state ts)
1546 switch (ts) {
1547 case TS_UNKNOWN: return 'u';
1548 case TS_UNTRUSTED: return '!';
1549 case TS_TRUSTED: return 'v';
1550 case TS_VERIFIED: return 'V';
1554 static void
1555 redraw_modeline(struct tab *tab)
1557 double pct;
1558 int x, y, max_x, max_y;
1559 const char *mode = tab->window.page.name;
1560 const char *spin = "-\\|/";
1562 werase(modeline);
1563 wattron(modeline, A_REVERSE);
1564 wmove(modeline, 0, 0);
1566 wprintw(modeline, "-%c%c %s ",
1567 spin[tab->loading_anim_step],
1568 trust_status_char(tab->trust),
1569 mode == NULL ? "(none)" : mode);
1571 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1573 if (tab->window.line_max <= (size_t)body_lines)
1574 wprintw(modeline, "All ");
1575 else if (tab->window.line_off == 0)
1576 wprintw(modeline, "Top ");
1577 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1578 wprintw(modeline, "Bottom ");
1579 else
1580 wprintw(modeline, "%.0f%% ", pct);
1582 wprintw(modeline, "%d/%d %s ",
1583 tab->window.line_off + tab->window.curs_y,
1584 tab->window.line_max,
1585 tab->hist_cur->h);
1587 getyx(modeline, y, x);
1588 getmaxyx(modeline, max_y, max_x);
1590 (void)y;
1591 (void)max_y;
1593 for (; x < max_x; ++x)
1594 waddstr(modeline, "-");
1597 static void
1598 redraw_minibuffer(void)
1600 struct tab *tab;
1601 size_t off_y, off_x = 0;
1602 char *start, *c;
1604 werase(minibuf);
1606 if (in_minibuffer) {
1607 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1608 if (ministate.hist_cur != NULL)
1609 wprintw(minibuf, "(%zu/%zu) ",
1610 ministate.hist_off + 1,
1611 ministate.history->len);
1613 getyx(minibuf, off_y, off_x);
1615 start = ministate.hist_cur != NULL
1616 ? ministate.hist_cur->h
1617 : ministate.buf;
1618 c = utf8_nth(ministate.window.current_line->line,
1619 ministate.window.cpoff);
1620 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1621 start = utf8_next_cp(start);
1624 waddstr(minibuf, start);
1627 if (ministate.curmesg != NULL)
1628 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1629 ministate.curmesg);
1631 if (!in_minibuffer && ministate.curmesg == NULL)
1632 waddstr(minibuf, keybuf);
1634 /* If nothing else, show the URL at point */
1635 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1636 tab = current_tab();
1637 if (tab->window.current_line != NULL &&
1638 tab->window.current_line->parent->type == LINE_LINK)
1639 waddstr(minibuf, tab->window.current_line->parent->alt);
1642 if (in_minibuffer)
1643 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1646 static void
1647 redraw_tab(struct tab *tab)
1649 if (side_window) {
1650 redraw_help();
1651 wnoutrefresh(help);
1654 redraw_tabline();
1655 redraw_body(tab);
1656 redraw_modeline(tab);
1657 redraw_minibuffer();
1659 wnoutrefresh(tabline);
1660 wnoutrefresh(modeline);
1662 if (in_minibuffer) {
1663 wnoutrefresh(body);
1664 wnoutrefresh(minibuf);
1665 } else {
1666 wnoutrefresh(minibuf);
1667 wnoutrefresh(body);
1670 doupdate();
1673 static void
1674 emit_help_item(char *prfx, void *fn)
1676 struct line *l;
1677 struct cmds *cmd;
1679 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1680 if (fn == cmd->fn)
1681 break;
1683 assert(cmd != NULL);
1685 if ((l = calloc(1, sizeof(*l))) == NULL)
1686 abort();
1688 l->type = LINE_TEXT;
1689 l->alt = NULL;
1691 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1693 if (TAILQ_EMPTY(&helpwin.page.head))
1694 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1695 else
1696 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1699 static void
1700 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1702 struct keymap *k;
1703 char p[32];
1704 const char *kn;
1706 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1707 strlcpy(p, prfx, sizeof(p));
1708 if (*p != '\0')
1709 strlcat(p, " ", sizeof(p));
1710 if (k->meta)
1711 strlcat(p, "M-", sizeof(p));
1712 if ((kn = unkbd(k->key)) != NULL)
1713 strlcat(p, kn, sizeof(p));
1714 else
1715 strlcat(p, keyname(k->key), sizeof(p));
1717 if (k->fn == NULL)
1718 rec_compute_help(&k->map, p, sizeof(p));
1719 else
1720 emit_help_item(p, k->fn);
1724 static void
1725 recompute_help(void)
1727 char p[32] = { 0 };
1729 empty_vlist(&helpwin);
1730 empty_linelist(&helpwin);
1731 rec_compute_help(current_map, p, sizeof(p));
1732 wrap_page(&helpwin, help_cols);
1735 static void
1736 vmessage(const char *fmt, va_list ap)
1738 if (evtimer_pending(&clminibufev, NULL))
1739 evtimer_del(&clminibufev);
1740 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1741 evtimer_add(&clminibufev, &clminibufev_timer);
1743 free(ministate.curmesg);
1745 /* TODO: what to do if the allocation fails here? */
1746 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1747 ministate.curmesg = NULL;
1749 redraw_minibuffer();
1750 if (in_minibuffer) {
1751 wrefresh(body);
1752 wrefresh(minibuf);
1753 } else {
1754 wrefresh(minibuf);
1755 wrefresh(body);
1759 static void
1760 message(const char *fmt, ...)
1762 va_list ap;
1764 va_start(ap, fmt);
1765 vmessage(fmt, ap);
1766 va_end(ap);
1769 static void
1770 start_loading_anim(struct tab *tab)
1772 if (tab->loading_anim)
1773 return;
1774 tab->loading_anim = 1;
1775 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1776 evtimer_add(&tab->loadingev, &loadingev_timer);
1779 static void
1780 update_loading_anim(int fd, short ev, void *d)
1782 struct tab *tab = d;
1784 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1786 if (tab->flags & TAB_CURRENT) {
1787 redraw_modeline(tab);
1788 wrefresh(modeline);
1789 wrefresh(body);
1790 if (in_minibuffer)
1791 wrefresh(minibuf);
1794 evtimer_add(&tab->loadingev, &loadingev_timer);
1797 static void
1798 stop_loading_anim(struct tab *tab)
1800 if (!tab->loading_anim)
1801 return;
1802 evtimer_del(&tab->loadingev);
1803 tab->loading_anim = 0;
1804 tab->loading_anim_step = 0;
1806 if (!(tab->flags & TAB_CURRENT))
1807 return;
1809 redraw_modeline(tab);
1811 wrefresh(modeline);
1812 wrefresh(body);
1813 if (in_minibuffer)
1814 wrefresh(minibuf);
1817 static void
1818 load_url_in_tab(struct tab *tab, const char *url)
1820 message("Loading %s...", url);
1821 start_loading_anim(tab);
1822 load_url(tab, url);
1824 tab->window.curs_x = 0;
1825 tab->window.curs_y = 0;
1826 redraw_tab(tab);
1829 static void
1830 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1831 void (*abortfn)(void), struct histhead *hist)
1833 in_minibuffer = 1;
1834 base_map = &minibuffer_map;
1835 current_map = &minibuffer_map;
1837 base_map->unhandled_input = self_insert_fn;
1839 ministate.donefn = donefn;
1840 ministate.abortfn = abortfn;
1841 memset(ministate.buf, 0, sizeof(ministate.buf));
1842 ministate.window.current_line = &ministate.vline;
1843 ministate.window.current_line->line = ministate.buf;
1844 ministate.window.cpoff = 0;
1845 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1847 ministate.history = hist;
1848 ministate.hist_cur = NULL;
1849 ministate.hist_off = 0;
1852 static void
1853 exit_minibuffer(void)
1855 werase(minibuf);
1857 in_minibuffer = 0;
1858 base_map = &global_map;
1859 current_map = &global_map;
1862 static void
1863 switch_to_tab(struct tab *tab)
1865 struct tab *t;
1867 TAILQ_FOREACH(t, &tabshead, tabs) {
1868 t->flags &= ~TAB_CURRENT;
1871 tab->flags |= TAB_CURRENT;
1874 static struct tab *
1875 new_tab(const char *url)
1877 struct tab *tab;
1879 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1880 event_loopbreak();
1881 return NULL;
1884 TAILQ_INIT(&tab->hist.head);
1886 TAILQ_INIT(&tab->window.head);
1888 tab->id = tab_counter++;
1889 switch_to_tab(tab);
1891 if (TAILQ_EMPTY(&tabshead))
1892 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1893 else
1894 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1896 load_url_in_tab(tab, url);
1897 return tab;
1900 static void
1901 session_new_tab_cb(const char *url)
1903 new_tab(url);
1906 static void
1907 usage(void)
1909 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1912 int
1913 ui_init(int argc, char * const *argv)
1915 const char *url = NEW_TAB_URL;
1916 int ch;
1918 while ((ch = getopt(argc, argv, "")) != -1) {
1919 switch (ch) {
1920 default:
1921 usage();
1922 return 0;
1925 argc -= optind;
1926 argv += optind;
1928 if (argc != 0)
1929 url = argv[0];
1931 setlocale(LC_ALL, "");
1933 TAILQ_INIT(&global_map.m);
1934 global_map.unhandled_input = global_key_unbound;
1936 TAILQ_INIT(&minibuffer_map.m);
1938 TAILQ_INIT(&eecmd_history.head);
1939 TAILQ_INIT(&ir_history.head);
1940 TAILQ_INIT(&lu_history.head);
1942 ministate.line.type = LINE_TEXT;
1943 ministate.vline.parent = &ministate.line;
1944 ministate.window.current_line = &ministate.vline;
1946 /* initialize help window */
1947 TAILQ_INIT(&helpwin.head);
1949 base_map = &global_map;
1950 current_map = &global_map;
1951 load_default_keys();
1953 initscr();
1954 raw();
1955 noecho();
1957 nonl();
1958 intrflush(stdscr, FALSE);
1960 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1961 return 0;
1962 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1963 return 0;
1964 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1965 return 0;
1966 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1967 return 0;
1968 if ((help = newwin(1, 1, 1, 0)) == NULL)
1969 return 0;
1971 body_lines = LINES-3;
1972 body_cols = COLS;
1974 keypad(body, TRUE);
1975 scrollok(body, TRUE);
1977 /* non-blocking input */
1978 wtimeout(body, 0);
1980 mvwprintw(body, 0, 0, "");
1982 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1983 event_add(&stdioev, NULL);
1985 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1986 signal_add(&winchev, NULL);
1988 load_last_session(session_new_tab_cb);
1989 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1990 new_tab(url);
1992 return 1;
1995 void
1996 ui_on_tab_loaded(struct tab *tab)
1998 stop_loading_anim(tab);
1999 message("Loaded %s", tab->hist_cur->h);
2001 redraw_tabline();
2002 wrefresh(tabline);
2003 if (in_minibuffer)
2004 wrefresh(minibuf);
2005 else
2006 wrefresh(body);
2009 void
2010 ui_on_tab_refresh(struct tab *tab)
2012 wrap_page(&tab->window, body_cols);
2013 if (tab->flags & TAB_CURRENT) {
2014 restore_cursor(&tab->window);
2015 redraw_tab(tab);
2019 void
2020 ui_require_input(struct tab *tab, int hide)
2022 /* TODO: hard-switching to another tab is ugly */
2023 switch_to_tab(tab);
2025 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2026 &ir_history);
2027 strlcpy(ministate.prompt, "Input required: ",
2028 sizeof(ministate.prompt));
2029 redraw_tab(tab);
2032 void
2033 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2034 unsigned int data)
2036 size_t len;
2038 yornp_cb = fn;
2039 yornp_data = data;
2040 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2041 yornp_abort, NULL);
2043 len = sizeof(ministate.prompt);
2044 strlcpy(ministate.prompt, prompt, len);
2045 strlcat(ministate.prompt, " (y or n) ", len);
2046 redraw_tab(current_tab());
2049 void
2050 ui_notify(const char *fmt, ...)
2052 va_list ap;
2054 va_start(ap, fmt);
2055 vmessage(fmt, ap);
2056 va_end(ap);
2059 void
2060 ui_end(void)
2062 endwin();