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, void*);
173 static void *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 url url;
1095 struct tab *tab;
1097 tab = current_tab();
1099 exit_minibuffer();
1100 minibuffer_hist_save_entry();
1102 /* a bit ugly but... */
1103 memcpy(&url, &tab->url, sizeof(tab->url));
1104 url_set_query(&url, ministate.buf);
1105 url_unparse(&url, buf, sizeof(buf));
1106 load_url_in_tab(tab, buf);
1109 static void
1110 lu_self_insert(void)
1112 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1113 !unicode_isgraph(thiskey.key)) {
1114 global_key_unbound();
1115 return;
1118 minibuffer_self_insert();
1121 static void
1122 lu_select(void)
1124 exit_minibuffer();
1125 minibuffer_hist_save_entry();
1126 load_url_in_tab(current_tab(), ministate.buf);
1129 static void
1130 bp_select(void)
1132 exit_minibuffer();
1133 if (*ministate.buf != '\0')
1134 add_to_bookmarks(ministate.buf);
1135 else
1136 message("Abort.");
1139 static void
1140 yornp_self_insert(void)
1142 if (thiskey.key != 'y' && thiskey.key != 'n') {
1143 message("Please answer y or n");
1144 return;
1147 yornp_cb(thiskey.key == 'y', yornp_data);
1148 yornp_data = NULL;
1149 exit_minibuffer();
1152 static void
1153 yornp_abort(void)
1155 yornp_cb(0, yornp_data);
1156 yornp_data = NULL;
1157 exit_minibuffer();
1160 static struct vline *
1161 nth_line(struct window *window, size_t n)
1163 struct vline *vl;
1164 size_t i;
1166 i = 0;
1167 TAILQ_FOREACH(vl, &window->head, vlines) {
1168 if (i == n)
1169 return vl;
1170 i++;
1173 /* unreachable */
1174 abort();
1177 static struct tab *
1178 current_tab(void)
1180 struct tab *t;
1182 TAILQ_FOREACH(t, &tabshead, tabs) {
1183 if (t->flags & TAB_CURRENT)
1184 return t;
1187 /* unreachable */
1188 abort();
1191 static struct window *
1192 current_window(void)
1194 if (in_minibuffer)
1195 return &ministate.window;
1196 return &current_tab()->window;
1199 static int
1200 readkey(void)
1202 uint32_t state = 0;
1204 if ((thiskey.key = wgetch(body)) == ERR)
1205 return 0;
1207 thiskey.meta = thiskey.key == 27;
1208 if (thiskey.meta) {
1209 thiskey.key = wgetch(body);
1210 if (thiskey.key == ERR || thiskey.key == 27) {
1211 thiskey.meta = 0;
1212 thiskey.key = 27;
1216 thiskey.cp = 0;
1217 if ((unsigned int)thiskey.key < UINT8_MAX) {
1218 while (1) {
1219 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1220 break;
1221 if ((thiskey.key = wgetch(body)) == ERR) {
1222 message("Error decoding user input");
1223 return 0;
1228 return 1;
1231 static void
1232 dispatch_stdio(int fd, short ev, void *d)
1234 struct keymap *k;
1235 const char *keyname;
1236 char tmp[5] = {0};
1238 if (!readkey())
1239 return;
1241 if (keybuf[0] != '\0')
1242 strlcat(keybuf, " ", sizeof(keybuf));
1243 if (thiskey.meta)
1244 strlcat(keybuf, "M-", sizeof(keybuf));
1245 if (thiskey.cp != 0) {
1246 utf8_encode(thiskey.cp, tmp);
1247 strlcat(keybuf, tmp, sizeof(keybuf));
1248 } else {
1249 if ((keyname = unkbd(thiskey.key)) != NULL)
1250 strlcat(keybuf, keyname, sizeof(keybuf));
1251 else {
1252 tmp[0] = thiskey.key;
1253 strlcat(keybuf, tmp, sizeof(keybuf));
1257 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1258 if (k->meta == thiskey.meta &&
1259 k->key == thiskey.key) {
1260 if (k->fn == NULL)
1261 current_map = &k->map;
1262 else {
1263 current_map = base_map;
1264 strlcpy(keybuf, "", sizeof(keybuf));
1265 k->fn(current_window());
1267 goto done;
1271 if (current_map->unhandled_input != NULL)
1272 current_map->unhandled_input();
1273 else {
1274 global_key_unbound();
1277 strlcpy(keybuf, "", sizeof(keybuf));
1278 current_map = base_map;
1280 done:
1281 if (side_window)
1282 recompute_help();
1284 redraw_tab(current_tab());
1287 static void
1288 handle_clear_minibuf(int fd, short ev, void *d)
1290 free(ministate.curmesg);
1291 ministate.curmesg = NULL;
1293 redraw_minibuffer();
1294 if (in_minibuffer) {
1295 wrefresh(body);
1296 wrefresh(minibuf);
1297 } else {
1298 wrefresh(minibuf);
1299 wrefresh(body);
1303 static void
1304 handle_resize(int sig, short ev, void *d)
1306 struct tab *tab;
1308 endwin();
1309 refresh();
1310 clear();
1312 /* move and resize the windows, in reverse order! */
1314 mvwin(minibuf, LINES-1, 0);
1315 wresize(minibuf, 1, COLS);
1317 mvwin(modeline, LINES-2, 0);
1318 wresize(modeline, 1, COLS);
1320 body_lines = LINES-3;
1321 body_cols = COLS;
1323 if (side_window) {
1324 help_cols = 0.3 * COLS;
1325 help_lines = LINES-3;
1326 mvwin(help, 1, 0);
1327 wresize(help, help_lines, help_cols);
1329 wrap_page(&helpwin, help_cols);
1331 body_cols = COLS - help_cols - 1;
1332 mvwin(body, 1, help_cols);
1333 } else
1334 mvwin(body, 1, 0);
1336 wresize(body, body_lines, body_cols);
1338 wresize(tabline, 1, COLS);
1340 tab = current_tab();
1342 wrap_page(&tab->window, body_cols);
1343 redraw_tab(tab);
1346 static int
1347 wrap_page(struct window *window, int width)
1349 struct line *l;
1350 const struct line *orig;
1351 struct vline *vl;
1352 const char *prfx;
1354 orig = window->current_line == NULL
1355 ? NULL
1356 : window->current_line->parent;
1357 window->current_line = NULL;
1359 window->curs_y = 0;
1360 window->line_off = 0;
1362 empty_vlist(window);
1364 TAILQ_FOREACH(l, &window->page.head, lines) {
1365 prfx = line_prefixes[l->type].prfx1;
1366 switch (l->type) {
1367 case LINE_TEXT:
1368 case LINE_LINK:
1369 case LINE_TITLE_1:
1370 case LINE_TITLE_2:
1371 case LINE_TITLE_3:
1372 case LINE_ITEM:
1373 case LINE_QUOTE:
1374 case LINE_PRE_START:
1375 case LINE_PRE_END:
1376 wrap_text(window, prfx, l, width);
1377 break;
1378 case LINE_PRE_CONTENT:
1379 hardwrap_text(window, l, width);
1380 break;
1383 if (orig == l && window->current_line == NULL) {
1384 window->line_off = window->line_max-1;
1385 window->current_line = TAILQ_LAST(&window->head, vhead);
1387 while (1) {
1388 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1389 if (vl == NULL || vl->parent != orig)
1390 break;
1391 window->current_line = vl;
1392 window->line_off--;
1397 if (window->current_line == NULL)
1398 window->current_line = TAILQ_FIRST(&window->head);
1400 return 1;
1403 static void
1404 print_vline(WINDOW *window, struct vline *vl)
1406 const char *text = vl->line;
1407 const char *prfx;
1408 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1409 int text_face = line_faces[vl->parent->type].text_prop;
1411 if (!vl->flags)
1412 prfx = line_prefixes[vl->parent->type].prfx1;
1413 else
1414 prfx = line_prefixes[vl->parent->type].prfx2;
1416 if (text == NULL)
1417 text = "";
1419 wattron(window, prefix_face);
1420 wprintw(window, "%s", prfx);
1421 wattroff(window, prefix_face);
1423 wattron(window, text_face);
1424 wprintw(window, "%s", text);
1425 wattroff(window, text_face);
1428 static void
1429 redraw_tabline(void)
1431 struct tab *tab;
1432 size_t toskip;
1433 int current, x, y, truncated;
1434 const char *title;
1435 char buf[25];
1437 toskip = 0;
1438 x = 1;
1439 TAILQ_FOREACH(tab, &tabshead, tabs) {
1440 x += sizeof(buf) + 1;
1441 toskip++;
1442 if (tab->flags & TAB_CURRENT)
1443 break;
1445 if (x < COLS-2)
1446 toskip = 0;
1447 else
1448 toskip--;
1450 werase(tabline);
1451 wattron(tabline, tab_face.background);
1452 wprintw(tabline, toskip == 0 ? " " : "<");
1453 wattroff(tabline, tab_face.background);
1455 truncated = 0;
1456 TAILQ_FOREACH(tab, &tabshead, tabs) {
1457 if (truncated)
1458 break;
1459 if (toskip != 0) {
1460 toskip--;
1461 continue;
1464 getyx(tabline, y, x);
1465 if (x + sizeof(buf)+2 >= (size_t)COLS)
1466 truncated = 1;
1468 current = tab->flags & TAB_CURRENT;
1470 if (*(title = tab->window.page.title) == '\0')
1471 title = tab->hist_cur->h;
1473 strlcpy(buf, " ", sizeof(buf));
1474 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1475 /* truncation happens */
1476 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1477 } else {
1478 /* pad with spaces */
1479 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1480 /* nop */ ;
1483 if (current)
1484 wattron(tabline, tab_face.current_tab);
1485 else
1486 wattron(tabline, tab_face.tab);
1488 wprintw(tabline, "%s", buf);
1489 if (TAILQ_NEXT(tab, tabs) != NULL)
1490 wprintw(tabline, " ");
1492 if (current)
1493 wattroff(tabline, tab_face.current_tab);
1494 else
1495 wattroff(tabline, tab_face.tab);
1498 wattron(tabline, tab_face.background);
1499 for (; x < COLS; ++x)
1500 waddch(tabline, ' ');
1501 if (truncated)
1502 mvwprintw(tabline, 0, COLS-1, ">");
1505 static void
1506 redraw_window(WINDOW *win, int height, struct window *window)
1508 struct vline *vl;
1509 int l;
1511 werase(win);
1513 window->line_off = MIN(window->line_max-1, window->line_off);
1514 if (TAILQ_EMPTY(&window->head))
1515 return;
1517 l = 0;
1518 vl = nth_line(window, window->line_off);
1519 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1520 wmove(win, l, 0);
1521 print_vline(win, vl);
1522 l++;
1523 if (l == height)
1524 break;
1527 wmove(win, window->curs_y, window->curs_x);
1530 static void
1531 redraw_help(void)
1533 redraw_window(help, help_lines, &helpwin);
1536 static void
1537 redraw_body(struct tab *tab)
1539 redraw_window(body, body_lines, &tab->window);
1542 static inline char
1543 trust_status_char(enum trust_state ts)
1545 switch (ts) {
1546 case TS_UNKNOWN: return 'u';
1547 case TS_UNTRUSTED: return '!';
1548 case TS_TRUSTED: return 'v';
1549 case TS_VERIFIED: return 'V';
1553 static void
1554 redraw_modeline(struct tab *tab)
1556 double pct;
1557 int x, y, max_x, max_y;
1558 const char *mode = tab->window.page.name;
1559 const char *spin = "-\\|/";
1561 werase(modeline);
1562 wattron(modeline, A_REVERSE);
1563 wmove(modeline, 0, 0);
1565 wprintw(modeline, "-%c%c %s ",
1566 spin[tab->loading_anim_step],
1567 trust_status_char(tab->trust),
1568 mode == NULL ? "(none)" : mode);
1570 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1572 if (tab->window.line_max <= (size_t)body_lines)
1573 wprintw(modeline, "All ");
1574 else if (tab->window.line_off == 0)
1575 wprintw(modeline, "Top ");
1576 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1577 wprintw(modeline, "Bottom ");
1578 else
1579 wprintw(modeline, "%.0f%% ", pct);
1581 wprintw(modeline, "%d/%d %s ",
1582 tab->window.line_off + tab->window.curs_y,
1583 tab->window.line_max,
1584 tab->hist_cur->h);
1586 getyx(modeline, y, x);
1587 getmaxyx(modeline, max_y, max_x);
1589 (void)y;
1590 (void)max_y;
1592 for (; x < max_x; ++x)
1593 waddstr(modeline, "-");
1596 static void
1597 redraw_minibuffer(void)
1599 struct tab *tab;
1600 size_t off_y, off_x = 0;
1601 char *start, *c;
1603 werase(minibuf);
1605 if (in_minibuffer) {
1606 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1607 if (ministate.hist_cur != NULL)
1608 wprintw(minibuf, "(%zu/%zu) ",
1609 ministate.hist_off + 1,
1610 ministate.history->len);
1612 getyx(minibuf, off_y, off_x);
1614 start = ministate.hist_cur != NULL
1615 ? ministate.hist_cur->h
1616 : ministate.buf;
1617 c = utf8_nth(ministate.window.current_line->line,
1618 ministate.window.cpoff);
1619 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1620 start = utf8_next_cp(start);
1623 waddstr(minibuf, start);
1626 if (ministate.curmesg != NULL)
1627 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1628 ministate.curmesg);
1630 if (!in_minibuffer && ministate.curmesg == NULL)
1631 waddstr(minibuf, keybuf);
1633 /* If nothing else, show the URL at point */
1634 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1635 tab = current_tab();
1636 if (tab->window.current_line != NULL &&
1637 tab->window.current_line->parent->type == LINE_LINK)
1638 waddstr(minibuf, tab->window.current_line->parent->alt);
1641 if (in_minibuffer)
1642 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1645 static void
1646 redraw_tab(struct tab *tab)
1648 if (side_window) {
1649 redraw_help();
1650 wnoutrefresh(help);
1653 redraw_tabline();
1654 redraw_body(tab);
1655 redraw_modeline(tab);
1656 redraw_minibuffer();
1658 wnoutrefresh(tabline);
1659 wnoutrefresh(modeline);
1661 if (in_minibuffer) {
1662 wnoutrefresh(body);
1663 wnoutrefresh(minibuf);
1664 } else {
1665 wnoutrefresh(minibuf);
1666 wnoutrefresh(body);
1669 doupdate();
1672 static void
1673 emit_help_item(char *prfx, void *fn)
1675 struct line *l;
1676 struct cmds *cmd;
1678 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1679 if (fn == cmd->fn)
1680 break;
1682 assert(cmd != NULL);
1684 if ((l = calloc(1, sizeof(*l))) == NULL)
1685 abort();
1687 l->type = LINE_TEXT;
1688 l->alt = NULL;
1690 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1692 if (TAILQ_EMPTY(&helpwin.page.head))
1693 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1694 else
1695 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1698 static void
1699 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1701 struct keymap *k;
1702 char p[32];
1703 const char *kn;
1705 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1706 strlcpy(p, prfx, sizeof(p));
1707 if (*p != '\0')
1708 strlcat(p, " ", sizeof(p));
1709 if (k->meta)
1710 strlcat(p, "M-", sizeof(p));
1711 if ((kn = unkbd(k->key)) != NULL)
1712 strlcat(p, kn, sizeof(p));
1713 else
1714 strlcat(p, keyname(k->key), sizeof(p));
1716 if (k->fn == NULL)
1717 rec_compute_help(&k->map, p, sizeof(p));
1718 else
1719 emit_help_item(p, k->fn);
1723 static void
1724 recompute_help(void)
1726 char p[32] = { 0 };
1728 empty_vlist(&helpwin);
1729 empty_linelist(&helpwin);
1730 rec_compute_help(current_map, p, sizeof(p));
1731 wrap_page(&helpwin, help_cols);
1734 static void
1735 vmessage(const char *fmt, va_list ap)
1737 if (evtimer_pending(&clminibufev, NULL))
1738 evtimer_del(&clminibufev);
1739 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1740 evtimer_add(&clminibufev, &clminibufev_timer);
1742 free(ministate.curmesg);
1744 /* TODO: what to do if the allocation fails here? */
1745 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1746 ministate.curmesg = NULL;
1748 redraw_minibuffer();
1749 if (in_minibuffer) {
1750 wrefresh(body);
1751 wrefresh(minibuf);
1752 } else {
1753 wrefresh(minibuf);
1754 wrefresh(body);
1758 static void
1759 message(const char *fmt, ...)
1761 va_list ap;
1763 va_start(ap, fmt);
1764 vmessage(fmt, ap);
1765 va_end(ap);
1768 static void
1769 start_loading_anim(struct tab *tab)
1771 if (tab->loading_anim)
1772 return;
1773 tab->loading_anim = 1;
1774 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1775 evtimer_add(&tab->loadingev, &loadingev_timer);
1778 static void
1779 update_loading_anim(int fd, short ev, void *d)
1781 struct tab *tab = d;
1783 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1785 if (tab->flags & TAB_CURRENT) {
1786 redraw_modeline(tab);
1787 wrefresh(modeline);
1788 wrefresh(body);
1789 if (in_minibuffer)
1790 wrefresh(minibuf);
1793 evtimer_add(&tab->loadingev, &loadingev_timer);
1796 static void
1797 stop_loading_anim(struct tab *tab)
1799 if (!tab->loading_anim)
1800 return;
1801 evtimer_del(&tab->loadingev);
1802 tab->loading_anim = 0;
1803 tab->loading_anim_step = 0;
1805 if (!(tab->flags & TAB_CURRENT))
1806 return;
1808 redraw_modeline(tab);
1810 wrefresh(modeline);
1811 wrefresh(body);
1812 if (in_minibuffer)
1813 wrefresh(minibuf);
1816 static void
1817 load_url_in_tab(struct tab *tab, const char *url)
1819 message("Loading %s...", url);
1820 start_loading_anim(tab);
1821 load_url(tab, url);
1823 tab->window.curs_x = 0;
1824 tab->window.curs_y = 0;
1825 redraw_tab(tab);
1828 static void
1829 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1830 void (*abortfn)(void), struct histhead *hist)
1832 in_minibuffer = 1;
1833 base_map = &minibuffer_map;
1834 current_map = &minibuffer_map;
1836 base_map->unhandled_input = self_insert_fn;
1838 ministate.donefn = donefn;
1839 ministate.abortfn = abortfn;
1840 memset(ministate.buf, 0, sizeof(ministate.buf));
1841 ministate.window.current_line = &ministate.vline;
1842 ministate.window.current_line->line = ministate.buf;
1843 ministate.window.cpoff = 0;
1844 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1846 ministate.history = hist;
1847 ministate.hist_cur = NULL;
1848 ministate.hist_off = 0;
1851 static void
1852 exit_minibuffer(void)
1854 werase(minibuf);
1856 in_minibuffer = 0;
1857 base_map = &global_map;
1858 current_map = &global_map;
1861 static void
1862 switch_to_tab(struct tab *tab)
1864 struct tab *t;
1866 TAILQ_FOREACH(t, &tabshead, tabs) {
1867 t->flags &= ~TAB_CURRENT;
1870 tab->flags |= TAB_CURRENT;
1873 static struct tab *
1874 new_tab(const char *url)
1876 struct tab *tab;
1878 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1879 event_loopbreak();
1880 return NULL;
1883 TAILQ_INIT(&tab->hist.head);
1885 TAILQ_INIT(&tab->window.head);
1887 tab->id = tab_counter++;
1888 switch_to_tab(tab);
1890 if (TAILQ_EMPTY(&tabshead))
1891 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1892 else
1893 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1895 load_url_in_tab(tab, url);
1896 return tab;
1899 static void
1900 session_new_tab_cb(const char *url)
1902 new_tab(url);
1905 static void
1906 usage(void)
1908 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1911 int
1912 ui_init(int argc, char * const *argv)
1914 const char *url = NEW_TAB_URL;
1915 int ch;
1917 while ((ch = getopt(argc, argv, "")) != -1) {
1918 switch (ch) {
1919 default:
1920 usage();
1921 return 0;
1924 argc -= optind;
1925 argv += optind;
1927 if (argc != 0)
1928 url = argv[0];
1930 setlocale(LC_ALL, "");
1932 TAILQ_INIT(&global_map.m);
1933 global_map.unhandled_input = global_key_unbound;
1935 TAILQ_INIT(&minibuffer_map.m);
1937 TAILQ_INIT(&eecmd_history.head);
1938 TAILQ_INIT(&ir_history.head);
1939 TAILQ_INIT(&lu_history.head);
1941 ministate.line.type = LINE_TEXT;
1942 ministate.vline.parent = &ministate.line;
1943 ministate.window.current_line = &ministate.vline;
1945 /* initialize help window */
1946 TAILQ_INIT(&helpwin.head);
1948 base_map = &global_map;
1949 current_map = &global_map;
1950 load_default_keys();
1952 initscr();
1953 raw();
1954 noecho();
1956 nonl();
1957 intrflush(stdscr, FALSE);
1959 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1960 return 0;
1961 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1962 return 0;
1963 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1964 return 0;
1965 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1966 return 0;
1967 if ((help = newwin(1, 1, 1, 0)) == NULL)
1968 return 0;
1970 body_lines = LINES-3;
1971 body_cols = COLS;
1973 keypad(body, TRUE);
1974 scrollok(body, TRUE);
1976 /* non-blocking input */
1977 wtimeout(body, 0);
1979 mvwprintw(body, 0, 0, "");
1981 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1982 event_add(&stdioev, NULL);
1984 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1985 signal_add(&winchev, NULL);
1987 load_last_session(session_new_tab_cb);
1988 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1989 new_tab(url);
1991 return 1;
1994 void
1995 ui_on_tab_loaded(struct tab *tab)
1997 stop_loading_anim(tab);
1998 message("Loaded %s", tab->hist_cur->h);
2000 redraw_tabline();
2001 wrefresh(tabline);
2002 if (in_minibuffer)
2003 wrefresh(minibuf);
2004 else
2005 wrefresh(body);
2008 void
2009 ui_on_tab_refresh(struct tab *tab)
2011 wrap_page(&tab->window, body_cols);
2012 if (tab->flags & TAB_CURRENT) {
2013 restore_cursor(&tab->window);
2014 redraw_tab(tab);
2018 void
2019 ui_require_input(struct tab *tab, int hide)
2021 /* TODO: hard-switching to another tab is ugly */
2022 switch_to_tab(tab);
2024 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2025 &ir_history);
2026 strlcpy(ministate.prompt, "Input required: ",
2027 sizeof(ministate.prompt));
2028 redraw_tab(tab);
2031 void
2032 ui_yornp(const char *prompt, void (*fn)(int, void*), void *data)
2034 size_t len;
2036 yornp_cb = fn;
2037 yornp_data = data;
2038 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2039 yornp_abort, NULL);
2041 len = sizeof(ministate.prompt);
2042 strlcpy(ministate.prompt, prompt, len);
2043 strlcat(ministate.prompt, " (y or n) ", len);
2044 redraw_tab(current_tab());
2047 void
2048 ui_notify(const char *fmt, ...)
2050 va_list ap;
2052 va_start(ap, fmt);
2053 vmessage(fmt, ap);
2054 va_end(ap);
2057 void
2058 ui_end(void)
2060 endwin();