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'', but it's still missing.
32 *
33 */
35 #include <telescope.h>
37 #include <assert.h>
38 #include <curses.h>
39 #include <event.h>
40 #include <locale.h>
41 #include <signal.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #define TAB_CURRENT 0x1
49 #define NEW_TAB_URL "about:new"
51 static struct event stdioev, winchev;
53 static void load_default_keys(void);
54 static void restore_cursor(struct window*);
56 #define CMD(fnname) static void fnname(struct window *)
58 CMD(cmd_previous_line);
59 CMD(cmd_next_line);
60 CMD(cmd_backward_char);
61 CMD(cmd_forward_char);
62 CMD(cmd_backward_paragraph);
63 CMD(cmd_forward_paragraph);
64 CMD(cmd_move_beginning_of_line);
65 CMD(cmd_move_end_of_line);
66 CMD(cmd_redraw);
67 CMD(cmd_scroll_line_down);
68 CMD(cmd_scroll_line_up);
69 CMD(cmd_scroll_up);
70 CMD(cmd_scroll_down);
71 CMD(cmd_beginning_of_buffer);
72 CMD(cmd_end_of_buffer);
73 CMD(cmd_kill_telescope);
74 CMD(cmd_push_button);
75 CMD(cmd_push_button_new_tab);
76 CMD(cmd_previous_button);
77 CMD(cmd_next_button);
78 CMD(cmd_previous_page);
79 CMD(cmd_next_page);
80 CMD(cmd_clear_minibuf);
81 CMD(cmd_execute_extended_command);
82 CMD(cmd_tab_close);
83 CMD(cmd_tab_close_other);
84 CMD(cmd_tab_new);
85 CMD(cmd_tab_next);
86 CMD(cmd_tab_previous);
87 CMD(cmd_tab_move);
88 CMD(cmd_tab_move_to);
89 CMD(cmd_load_url);
90 CMD(cmd_load_current_url);
91 CMD(cmd_bookmark_page);
92 CMD(cmd_list_bookmarks);
93 CMD(cmd_toggle_help);
95 CMD(cmd_mini_delete_char);
96 CMD(cmd_mini_delete_backward_char);
97 CMD(cmd_mini_kill_line);
98 CMD(cmd_mini_abort);
99 CMD(cmd_mini_complete_and_exit);
100 CMD(cmd_mini_previous_history_element);
101 CMD(cmd_mini_next_history_element);
103 #include "cmd.gen.h"
105 static void global_key_unbound(void);
106 static void minibuffer_hist_save_entry(void);
107 static void minibuffer_taint_hist(void);
108 static void minibuffer_self_insert(void);
109 static void eecmd_self_insert(void);
110 static void eecmd_select(void);
111 static void ir_self_insert(void);
112 static void ir_select(void);
113 static void lu_self_insert(void);
114 static void lu_select(void);
115 static void bp_select(void);
116 static void yornp_self_insert(void);
117 static void yornp_abort(void);
119 static struct vline *nth_line(struct window*, size_t);
120 static struct tab *current_tab(void);
121 static struct window *current_window(void);
122 static int readkey(void);
123 static void dispatch_stdio(int, short, void*);
124 static void handle_clear_minibuf(int, short, void*);
125 static void handle_resize(int, short, void*);
126 static int wrap_page(struct window*, int);
127 static void print_vline(WINDOW*, struct vline*);
128 static void redraw_tabline(void);
129 static void redraw_window(WINDOW*, int, struct window*);
130 static void redraw_help(void);
131 static void redraw_body(struct tab*);
132 static void redraw_modeline(struct tab*);
133 static void redraw_minibuffer(void);
134 static void redraw_tab(struct tab*);
135 static void emit_help_item(char*, void*);
136 static void rec_compute_help(struct kmap*, char*, size_t);
137 static void recompute_help(void);
138 static void vmessage(const char*, va_list);
139 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
140 static void start_loading_anim(struct tab*);
141 static void update_loading_anim(int, short, void*);
142 static void stop_loading_anim(struct tab*);
143 static void load_url_in_tab(struct tab*, const char*);
144 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
145 static void exit_minibuffer(void);
146 static void switch_to_tab(struct tab*);
147 static struct tab *new_tab(const char*);
148 static void session_new_tab_cb(const char*);
149 static void usage(void);
151 static struct { short meta; int key; uint32_t cp; } thiskey;
153 static WINDOW *tabline, *body, *modeline, *minibuf;
154 static int body_lines, body_cols;
156 static WINDOW *help;
157 static struct window helpwin;
158 static int help_lines, help_cols;
160 static int side_window;
162 static struct event clminibufev;
163 static struct timeval clminibufev_timer = { 5, 0 };
164 static struct timeval loadingev_timer = { 0, 250000 };
166 static uint32_t tab_counter;
168 static char keybuf[64];
170 static void (*yornp_cb)(int, unsigned int);
171 static unsigned int yornp_data;
173 struct kmap global_map,
174 minibuffer_map,
175 *current_map,
176 *base_map;
178 static struct histhead eecmd_history,
179 ir_history,
180 lu_history;
182 static int in_minibuffer;
184 static struct {
185 char *curmesg;
187 char prompt[64];
188 void (*donefn)(void);
189 void (*abortfn)(void);
191 char buf[1025];
192 struct line line;
193 struct vline vline;
194 struct window window;
196 struct histhead *history;
197 struct hist *hist_cur;
198 size_t hist_off;
199 } ministate;
201 struct lineprefix {
202 const char *prfx1;
203 const char *prfx2;
204 } line_prefixes[] = {
205 [LINE_TEXT] = { "", "" },
206 [LINE_LINK] = { "=> ", " " },
207 [LINE_TITLE_1] = { "# ", " " },
208 [LINE_TITLE_2] = { "## ", " " },
209 [LINE_TITLE_3] = { "### ", " " },
210 [LINE_ITEM] = { "* ", " " },
211 [LINE_QUOTE] = { "> ", " " },
212 [LINE_PRE_START] = { "```", " " },
213 [LINE_PRE_CONTENT] = { "", "" },
214 [LINE_PRE_END] = { "```", "```" },
215 };
217 static struct line_face {
218 int prefix_prop;
219 int text_prop;
220 } line_faces[] = {
221 [LINE_TEXT] = { 0, 0 },
222 [LINE_LINK] = { 0, A_UNDERLINE },
223 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
224 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
225 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
226 [LINE_ITEM] = { 0, 0 },
227 [LINE_QUOTE] = { 0, A_DIM },
228 [LINE_PRE_START] = { 0, 0 },
229 [LINE_PRE_CONTENT] = { 0, 0 },
230 [LINE_PRE_END] = { 0, 0 },
231 };
233 static struct tab_face {
234 int background, tab, current_tab;
235 } tab_face = {
236 A_REVERSE, A_REVERSE, A_NORMAL
237 };
239 static inline void
240 global_set_key(const char *key, void (*fn)(struct window*))
242 if (!kmap_define_key(&global_map, key, fn))
243 _exit(1);
246 static inline void
247 minibuffer_set_key(const char *key, void (*fn)(struct window*))
249 if (!kmap_define_key(&minibuffer_map, key, fn))
250 _exit(1);
253 static void
254 load_default_keys(void)
256 /* === global map === */
258 /* emacs */
259 global_set_key("C-p", cmd_previous_line);
260 global_set_key("C-n", cmd_next_line);
261 global_set_key("C-f", cmd_forward_char);
262 global_set_key("C-b", cmd_backward_char);
263 global_set_key("M-{", cmd_backward_paragraph);
264 global_set_key("M-}", cmd_forward_paragraph);
265 global_set_key("C-a", cmd_move_beginning_of_line);
266 global_set_key("C-e", cmd_move_end_of_line);
268 global_set_key("M-v", cmd_scroll_up);
269 global_set_key("C-v", cmd_scroll_down);
270 global_set_key("M-space", cmd_scroll_up);
271 global_set_key("space", cmd_scroll_down);
273 global_set_key("M-<", cmd_beginning_of_buffer);
274 global_set_key("M->", cmd_end_of_buffer);
276 global_set_key("C-x C-c", cmd_kill_telescope);
278 global_set_key("C-g", cmd_clear_minibuf);
280 global_set_key("M-x", cmd_execute_extended_command);
281 global_set_key("C-x C-f", cmd_load_url);
282 global_set_key("C-x M-f", cmd_load_current_url);
284 global_set_key("C-x t 0", cmd_tab_close);
285 global_set_key("C-x t 1", cmd_tab_close_other);
286 global_set_key("C-x t 2", cmd_tab_new);
287 global_set_key("C-x t o", cmd_tab_next);
288 global_set_key("C-x t O", cmd_tab_previous);
289 global_set_key("C-x t m", cmd_tab_move);
290 global_set_key("C-x t M", cmd_tab_move_to);
292 global_set_key("C-M-b", cmd_previous_page);
293 global_set_key("C-M-f", cmd_next_page);
295 global_set_key("<f7> a", cmd_bookmark_page);
296 global_set_key("<f7> <f7>", cmd_list_bookmarks);
298 /* vi/vi-like */
299 global_set_key("k", cmd_previous_line);
300 global_set_key("j", cmd_next_line);
301 global_set_key("l", cmd_forward_char);
302 global_set_key("h", cmd_backward_char);
303 global_set_key("{", cmd_backward_paragraph);
304 global_set_key("}", cmd_forward_paragraph);
305 global_set_key("^", cmd_move_beginning_of_line);
306 global_set_key("$", cmd_move_end_of_line);
308 global_set_key("K", cmd_scroll_line_up);
309 global_set_key("J", cmd_scroll_line_down);
311 global_set_key("g g", cmd_beginning_of_buffer);
312 global_set_key("G", cmd_end_of_buffer);
314 global_set_key("g D", cmd_tab_close);
315 global_set_key("g N", cmd_tab_new);
316 global_set_key("g t", cmd_tab_next);
317 global_set_key("g T", cmd_tab_previous);
318 global_set_key("g M-t", cmd_tab_move);
319 global_set_key("g M-T", cmd_tab_move_to);
321 global_set_key("H", cmd_previous_page);
322 global_set_key("L", cmd_next_page);
324 /* tmp */
325 global_set_key("q", cmd_kill_telescope);
327 global_set_key("esc", cmd_clear_minibuf);
329 global_set_key(":", cmd_execute_extended_command);
331 /* cua */
332 global_set_key("<up>", cmd_previous_line);
333 global_set_key("<down>", cmd_next_line);
334 global_set_key("<right>", cmd_forward_char);
335 global_set_key("<left>", cmd_backward_char);
336 global_set_key("<prior>", cmd_scroll_up);
337 global_set_key("<next>", cmd_scroll_down);
339 global_set_key("M-<left>", cmd_previous_page);
340 global_set_key("M-<right>", cmd_next_page);
342 /* "ncurses standard" */
343 global_set_key("C-l", cmd_redraw);
345 /* global */
346 global_set_key("<f1>", cmd_toggle_help);
347 global_set_key("C-m", cmd_push_button);
348 global_set_key("M-enter", cmd_push_button_new_tab);
349 global_set_key("M-tab", cmd_previous_button);
350 global_set_key("tab", cmd_next_button);
352 /* === minibuffer map === */
353 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
354 minibuffer_set_key("C-g", cmd_mini_abort);
355 minibuffer_set_key("esc", cmd_mini_abort);
356 minibuffer_set_key("C-d", cmd_mini_delete_char);
357 minibuffer_set_key("del", cmd_mini_delete_backward_char);
358 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
359 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
361 minibuffer_set_key("C-b", cmd_backward_char);
362 minibuffer_set_key("C-f", cmd_forward_char);
363 minibuffer_set_key("<left>", cmd_backward_char);
364 minibuffer_set_key("<right>", cmd_forward_char);
365 minibuffer_set_key("C-e", cmd_move_end_of_line);
366 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
367 minibuffer_set_key("<end>", cmd_move_end_of_line);
368 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
369 minibuffer_set_key("C-k", cmd_mini_kill_line);
371 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
372 minibuffer_set_key("M-n", cmd_mini_next_history_element);
373 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
374 minibuffer_set_key("<down>", cmd_mini_next_history_element);
377 static void
378 restore_cursor(struct window *window)
380 struct vline *vl;
381 const char *prfx;
383 vl = window->current_line;
384 if (vl == NULL || vl->line == NULL)
385 window->curs_x = window->cpoff = 0;
386 else
387 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
389 if (vl != NULL) {
390 prfx = line_prefixes[vl->parent->type].prfx1;
391 window->curs_x += utf8_swidth(prfx);
395 static void
396 cmd_previous_line(struct window *window)
398 struct vline *vl;
400 if (window->current_line == NULL
401 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
402 return;
404 if (--window->curs_y < 0) {
405 window->curs_y = 0;
406 cmd_scroll_line_up(window);
407 return;
410 window->current_line = vl;
411 restore_cursor(window);
414 static void
415 cmd_next_line(struct window *window)
417 struct vline *vl;
419 if (window->current_line == NULL
420 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
421 return;
423 if (++window->curs_y > body_lines-1) {
424 window->curs_y = body_lines-1;
425 cmd_scroll_line_down(window);
426 return;
429 window->current_line = vl;
430 restore_cursor(window);
433 static void
434 cmd_backward_char(struct window *window)
436 if (window->cpoff != 0)
437 window->cpoff--;
438 restore_cursor(window);
441 static void
442 cmd_forward_char(struct window *window)
444 window->cpoff++;
445 restore_cursor(window);
448 static void
449 cmd_backward_paragraph(struct window *window)
451 do {
452 if (window->current_line == NULL ||
453 window->current_line == TAILQ_FIRST(&window->head)) {
454 message("No previous paragraph");
455 return;
457 cmd_previous_line(window);
458 } while (window->current_line->line != NULL ||
459 window->current_line->parent->type != LINE_TEXT);
462 static void
463 cmd_forward_paragraph(struct window *window)
465 do {
466 if (window->current_line == NULL ||
467 window->current_line == TAILQ_LAST(&window->head, vhead)) {
468 message("No next paragraph");
469 return;
471 cmd_next_line(window);
472 } while (window->current_line->line != NULL ||
473 window->current_line->parent->type != LINE_TEXT);
476 static void
477 cmd_move_beginning_of_line(struct window *window)
479 window->cpoff = 0;
480 restore_cursor(window);
483 static void
484 cmd_move_end_of_line(struct window *window)
486 struct vline *vl;
488 vl = window->current_line;
489 if (vl->line == NULL)
490 return;
491 window->cpoff = utf8_cplen(vl->line);
492 restore_cursor(window);
495 static void
496 cmd_redraw(struct window *window)
498 handle_resize(0, 0, NULL);
501 static void
502 cmd_scroll_line_up(struct window *window)
504 struct vline *vl;
506 if (window->line_off == 0)
507 return;
509 vl = nth_line(window, --window->line_off);
510 wscrl(body, -1);
511 wmove(body, 0, 0);
512 print_vline(body, vl);
514 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
515 restore_cursor(window);
518 static void
519 cmd_scroll_line_down(struct window *window)
521 struct vline *vl;
523 vl = window->current_line;
524 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
525 return;
526 window->current_line = vl;
528 window->line_off++;
529 wscrl(body, 1);
531 if (window->line_max - window->line_off < (size_t)body_lines)
532 return;
534 vl = nth_line(window, window->line_off + body_lines-1);
535 wmove(body, body_lines-1, 0);
536 print_vline(body, vl);
538 restore_cursor(window);
541 static void
542 cmd_scroll_up(struct window *window)
544 size_t off;
546 off = body_lines+1;
548 for (; off > 0; --off)
549 cmd_scroll_line_up(window);
552 static void
553 cmd_scroll_down(struct window *window)
555 size_t off;
557 off = body_lines+1;
559 for (; off > 0; --off)
560 cmd_scroll_line_down(window);
563 static void
564 cmd_beginning_of_buffer(struct window *window)
566 window->current_line = TAILQ_FIRST(&window->head);
567 window->line_off = 0;
568 window->curs_y = 0;
569 window->cpoff = 0;
570 restore_cursor(window);
573 static void
574 cmd_end_of_buffer(struct window *window)
576 ssize_t off;
578 off = window->line_max - body_lines;
579 off = MAX(0, off);
581 window->line_off = off;
582 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
584 window->current_line = TAILQ_LAST(&window->head, vhead);
585 window->cpoff = body_cols;
586 restore_cursor(window);
589 static void
590 cmd_kill_telescope(struct window *window)
592 save_session();
593 event_loopbreak();
596 static void
597 cmd_push_button(struct window *window)
599 struct vline *vl;
600 size_t nth;
602 nth = window->line_off + window->curs_y;
603 if (nth >= window->line_max)
604 return;
605 vl = nth_line(window, nth);
606 if (vl->parent->type != LINE_LINK)
607 return;
609 load_url_in_tab(current_tab(), vl->parent->alt);
612 static void
613 cmd_push_button_new_tab(struct window *window)
615 struct vline *vl;
616 size_t nth;
618 nth = window->line_off + window->curs_y;
619 if (nth > window->line_max)
620 return;
621 vl = nth_line(window, nth);
622 if (vl->parent->type != LINE_LINK)
623 return;
625 new_tab(vl->parent->alt);
628 static void
629 cmd_previous_button(struct window *window)
631 do {
632 if (window->current_line == NULL ||
633 window->current_line == TAILQ_FIRST(&window->head)) {
634 message("No previous link");
635 return;
637 cmd_previous_line(window);
638 } while (window->current_line->parent->type != LINE_LINK);
641 static void
642 cmd_next_button(struct window *window)
644 do {
645 if (window->current_line == NULL ||
646 window->current_line == TAILQ_LAST(&window->head, vhead)) {
647 message("No next link");
648 return;
650 cmd_next_line(window);
651 } while (window->current_line->parent->type != LINE_LINK);
654 static void
655 cmd_previous_page(struct window *window)
657 struct tab *tab = current_tab();
659 if (!load_previous_page(tab))
660 message("No previous page");
661 else
662 start_loading_anim(tab);
665 static void
666 cmd_next_page(struct window *window)
668 struct tab *tab = current_tab();
670 if (!load_next_page(tab))
671 message("No next page");
672 else
673 start_loading_anim(tab);
676 static void
677 cmd_clear_minibuf(struct window *window)
679 handle_clear_minibuf(0, 0, NULL);
682 static void
683 cmd_execute_extended_command(struct window *window)
685 size_t len;
687 if (in_minibuffer) {
688 message("We don't have enable-recursive-minibuffers");
689 return;
692 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
693 &eecmd_history);
695 len = sizeof(ministate.prompt);
696 strlcpy(ministate.prompt, "", len);
698 if (thiskey.meta)
699 strlcat(ministate.prompt, "M-", len);
701 strlcat(ministate.prompt, keyname(thiskey.key), len);
702 strlcat(ministate.prompt, " ", len);
705 static void
706 cmd_tab_close(struct window *window)
708 struct tab *tab, *t;
710 tab = current_tab();
711 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
712 TAILQ_NEXT(tab, tabs) == NULL) {
713 message("Can't close the only tab.");
714 return;
717 if (evtimer_pending(&tab->loadingev, NULL))
718 evtimer_del(&tab->loadingev);
720 stop_tab(tab);
722 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
723 t = TAILQ_NEXT(tab, tabs);
724 TAILQ_REMOVE(&tabshead, tab, tabs);
725 free(tab);
727 switch_to_tab(t);
730 static void
731 cmd_tab_close_other(struct window *window)
733 struct tab *t, *i;
735 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
736 if (t->flags & TAB_CURRENT)
737 continue;
739 stop_tab(t);
740 TAILQ_REMOVE(&tabshead, t, tabs);
741 free(t);
745 static void
746 cmd_tab_new(struct window *window)
748 new_tab(NEW_TAB_URL);
751 static void
752 cmd_tab_next(struct window *window)
754 struct tab *tab, *t;
756 tab = current_tab();
757 tab->flags &= ~TAB_CURRENT;
759 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
760 t = TAILQ_FIRST(&tabshead);
761 t->flags |= TAB_CURRENT;
764 static void
765 cmd_tab_previous(struct window *window)
767 struct tab *tab, *t;
769 tab = current_tab();
770 tab->flags &= ~TAB_CURRENT;
772 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
773 t = TAILQ_LAST(&tabshead, tabshead);
774 t->flags |= TAB_CURRENT;
777 static void
778 cmd_tab_move(struct window *window)
780 struct tab *tab, *t;
782 tab = current_tab();
783 t = TAILQ_NEXT(tab, tabs);
784 TAILQ_REMOVE(&tabshead, tab, tabs);
786 if (t == NULL)
787 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
788 else
789 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
792 static void
793 cmd_tab_move_to(struct window *window)
795 struct tab *tab, *t;
797 tab = current_tab();
798 t = TAILQ_PREV(tab, tabshead, tabs);
799 TAILQ_REMOVE(&tabshead, tab, tabs);
801 if (t == NULL) {
802 if (TAILQ_EMPTY(&tabshead))
803 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
804 else
805 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
806 } else
807 TAILQ_INSERT_BEFORE(t, tab, tabs);
810 static void
811 cmd_load_url(struct window *window)
813 if (in_minibuffer) {
814 message("We don't have enable-recursive-minibuffers");
815 return;
818 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
819 &lu_history);
820 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
823 static void
824 cmd_load_current_url(struct window *window)
826 struct tab *tab = current_tab();
828 if (in_minibuffer) {
829 message("We don't have enable-recursive-minibuffers");
830 return;
833 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
834 &lu_history);
835 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
836 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
837 ministate.window.cpoff = utf8_cplen(ministate.buf);
840 static void
841 cmd_bookmark_page(struct window *window)
843 struct tab *tab = current_tab();
845 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
846 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
847 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
848 ministate.window.cpoff = utf8_cplen(ministate.buf);
851 static void
852 cmd_list_bookmarks(struct window *window)
854 load_url_in_tab(current_tab(), "about:bookmarks");
857 static void
858 cmd_toggle_help(struct window *window)
860 side_window = !side_window;
861 if (side_window)
862 recompute_help();
864 /* ugly hack, but otherwise the window doesn't get updated
865 * until I call handle_resize a second time (i.e. C-l). I
866 * will be happy to know why something like this is needed. */
867 handle_resize(0, 0, NULL);
868 handle_resize(0, 0, NULL);
871 static void
872 cmd_mini_delete_char(struct window *window)
874 char *c, *n;
876 if (!in_minibuffer) {
877 message("text is read-only");
878 return;
881 minibuffer_taint_hist();
883 c = utf8_nth(window->current_line->line, window->cpoff);
884 if (*c == '\0')
885 return;
886 n = utf8_next_cp(c);
888 memmove(c, n, strlen(n)+1);
891 static void
892 cmd_mini_delete_backward_char(struct window *window)
894 char *c, *p, *start;
896 if (!in_minibuffer) {
897 message("text is read-only");
898 return;
901 minibuffer_taint_hist();
903 c = utf8_nth(window->current_line->line, window->cpoff);
904 start = window->current_line->line;
905 if (c == start)
906 return;
907 p = utf8_prev_cp(c-1, start);
909 memmove(p, c, strlen(c)+1);
910 window->cpoff--;
913 static void
914 cmd_mini_kill_line(struct window *window)
916 char *c;
918 if (!in_minibuffer) {
919 message("text is read-only");
920 return;
923 minibuffer_taint_hist();
924 c = utf8_nth(window->current_line->line, window->cpoff);
925 *c = '\0';
928 static void
929 cmd_mini_abort(struct window *window)
931 if (!in_minibuffer)
932 return;
934 ministate.abortfn();
937 static void
938 cmd_mini_complete_and_exit(struct window *window)
940 if (!in_minibuffer)
941 return;
943 minibuffer_taint_hist();
944 ministate.donefn();
947 static void
948 cmd_mini_previous_history_element(struct window *window)
950 if (ministate.history == NULL) {
951 message("No history");
952 return;
955 if (ministate.hist_cur == NULL ||
956 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
957 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
958 ministate.hist_off = ministate.history->len - 1;
959 if (ministate.hist_cur == NULL)
960 message("No prev item");
961 } else {
962 ministate.hist_off--;
965 if (ministate.hist_cur != NULL)
966 window->current_line->line = ministate.hist_cur->h;
969 static void
970 cmd_mini_next_history_element(struct window *window)
972 if (ministate.history == NULL) {
973 message("No history");
974 return;
977 if (ministate.hist_cur == NULL ||
978 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
979 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
980 ministate.hist_off = 0;
981 if (ministate.hist_cur == NULL)
982 message("No next item");
983 } else {
984 ministate.hist_off++;
987 if (ministate.hist_cur != NULL)
988 window->current_line->line = ministate.hist_cur->h;
991 static void
992 global_key_unbound(void)
994 message("%s is undefined", keybuf);
997 static void
998 minibuffer_hist_save_entry(void)
1000 struct hist *hist;
1002 if (ministate.history == NULL)
1003 return;
1005 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1006 abort();
1008 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1010 if (TAILQ_EMPTY(&ministate.history->head))
1011 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1012 else
1013 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1014 ministate.history->len++;
1018 * taint the minibuffer cache: if we're currently showing a history
1019 * element, copy that to the current buf and reset the "history
1020 * navigation" thing.
1022 static void
1023 minibuffer_taint_hist(void)
1025 if (ministate.hist_cur == NULL)
1026 return;
1028 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1029 ministate.hist_cur = NULL;
1032 static void
1033 minibuffer_self_insert(void)
1035 char *c, tmp[5] = {0};
1036 size_t len;
1038 minibuffer_taint_hist();
1040 if (thiskey.cp == 0)
1041 return;
1043 len = utf8_encode(thiskey.cp, tmp);
1044 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1045 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1046 return;
1048 memmove(c + len, c, strlen(c)+1);
1049 memcpy(c, tmp, len);
1050 ministate.window.cpoff++;
1053 static void
1054 eecmd_self_insert(void)
1056 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1057 !unicode_isgraph(thiskey.cp)) {
1058 global_key_unbound();
1059 return;
1062 minibuffer_self_insert();
1065 static void
1066 eecmd_select(void)
1068 struct cmds *cmd;
1070 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1071 if (!strcmp(cmd->cmd, ministate.buf)) {
1072 exit_minibuffer();
1073 minibuffer_hist_save_entry();
1074 cmd->fn(current_window());
1075 return;
1079 message("No match");
1082 static void
1083 ir_self_insert(void)
1085 minibuffer_self_insert();
1088 static void
1089 ir_select(void)
1091 char buf[1025] = {0};
1092 struct phos_uri uri;
1093 struct tab *tab;
1095 tab = current_tab();
1097 exit_minibuffer();
1098 minibuffer_hist_save_entry();
1100 /* a bit ugly but... */
1101 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1103 /* XXX: ptc encode! */
1104 memcpy(&uri.query, ministate.buf, strlen(ministate.buf)+1);
1106 phos_serialize_uri(&uri, buf, sizeof(buf));
1107 load_url_in_tab(tab, buf);
1110 static void
1111 lu_self_insert(void)
1113 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1114 !unicode_isgraph(thiskey.key)) {
1115 global_key_unbound();
1116 return;
1119 minibuffer_self_insert();
1122 static void
1123 lu_select(void)
1125 exit_minibuffer();
1126 minibuffer_hist_save_entry();
1127 load_url_in_tab(current_tab(), ministate.buf);
1130 static void
1131 bp_select(void)
1133 exit_minibuffer();
1134 if (*ministate.buf != '\0')
1135 add_to_bookmarks(ministate.buf);
1136 else
1137 message("Abort.");
1140 static void
1141 yornp_self_insert(void)
1143 if (thiskey.key != 'y' && thiskey.key != 'n') {
1144 message("Please answer y or n");
1145 return;
1148 yornp_cb(thiskey.key == 'y', yornp_data);
1149 exit_minibuffer();
1152 static void
1153 yornp_abort(void)
1155 yornp_cb(0, yornp_data);
1156 exit_minibuffer();
1159 static struct vline *
1160 nth_line(struct window *window, size_t n)
1162 struct vline *vl;
1163 size_t i;
1165 i = 0;
1166 TAILQ_FOREACH(vl, &window->head, vlines) {
1167 if (i == n)
1168 return vl;
1169 i++;
1172 /* unreachable */
1173 abort();
1176 static struct tab *
1177 current_tab(void)
1179 struct tab *t;
1181 TAILQ_FOREACH(t, &tabshead, tabs) {
1182 if (t->flags & TAB_CURRENT)
1183 return t;
1186 /* unreachable */
1187 abort();
1190 static struct window *
1191 current_window(void)
1193 if (in_minibuffer)
1194 return &ministate.window;
1195 return &current_tab()->window;
1198 static int
1199 readkey(void)
1201 uint32_t state = 0;
1203 if ((thiskey.key = wgetch(body)) == ERR)
1204 return 0;
1206 thiskey.meta = thiskey.key == 27;
1207 if (thiskey.meta) {
1208 thiskey.key = wgetch(body);
1209 if (thiskey.key == ERR || thiskey.key == 27) {
1210 thiskey.meta = 0;
1211 thiskey.key = 27;
1215 thiskey.cp = 0;
1216 if ((unsigned int)thiskey.key < UINT8_MAX) {
1217 while (1) {
1218 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1219 break;
1220 if ((thiskey.key = wgetch(body)) == ERR) {
1221 message("Error decoding user input");
1222 return 0;
1227 return 1;
1230 static void
1231 dispatch_stdio(int fd, short ev, void *d)
1233 struct keymap *k;
1234 const char *keyname;
1235 char tmp[5] = {0};
1237 if (!readkey())
1238 return;
1240 if (keybuf[0] != '\0')
1241 strlcat(keybuf, " ", sizeof(keybuf));
1242 if (thiskey.meta)
1243 strlcat(keybuf, "M-", sizeof(keybuf));
1244 if (thiskey.cp != 0) {
1245 utf8_encode(thiskey.cp, tmp);
1246 strlcat(keybuf, tmp, sizeof(keybuf));
1247 } else {
1248 if ((keyname = unkbd(thiskey.key)) != NULL)
1249 strlcat(keybuf, keyname, sizeof(keybuf));
1250 else {
1251 tmp[0] = thiskey.key;
1252 strlcat(keybuf, tmp, sizeof(keybuf));
1256 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1257 if (k->meta == thiskey.meta &&
1258 k->key == thiskey.key) {
1259 if (k->fn == NULL)
1260 current_map = &k->map;
1261 else {
1262 current_map = base_map;
1263 strlcpy(keybuf, "", sizeof(keybuf));
1264 k->fn(current_window());
1266 goto done;
1270 if (current_map->unhandled_input != NULL)
1271 current_map->unhandled_input();
1272 else {
1273 global_key_unbound();
1276 strlcpy(keybuf, "", sizeof(keybuf));
1277 current_map = base_map;
1279 done:
1280 if (side_window)
1281 recompute_help();
1283 redraw_tab(current_tab());
1286 static void
1287 handle_clear_minibuf(int fd, short ev, void *d)
1289 free(ministate.curmesg);
1290 ministate.curmesg = NULL;
1292 redraw_minibuffer();
1293 if (in_minibuffer) {
1294 wrefresh(body);
1295 wrefresh(minibuf);
1296 } else {
1297 wrefresh(minibuf);
1298 wrefresh(body);
1302 static void
1303 handle_resize(int sig, short ev, void *d)
1305 struct tab *tab;
1307 endwin();
1308 refresh();
1309 clear();
1311 /* move and resize the windows, in reverse order! */
1313 mvwin(minibuf, LINES-1, 0);
1314 wresize(minibuf, 1, COLS);
1316 mvwin(modeline, LINES-2, 0);
1317 wresize(modeline, 1, COLS);
1319 body_lines = LINES-3;
1320 body_cols = COLS;
1322 if (side_window) {
1323 help_cols = 0.3 * COLS;
1324 help_lines = LINES-3;
1325 mvwin(help, 1, 0);
1326 wresize(help, help_lines, help_cols);
1328 wrap_page(&helpwin, help_cols);
1330 body_cols = COLS - help_cols - 1;
1331 mvwin(body, 1, help_cols);
1332 } else
1333 mvwin(body, 1, 0);
1335 wresize(body, body_lines, body_cols);
1337 wresize(tabline, 1, COLS);
1339 tab = current_tab();
1341 wrap_page(&tab->window, body_cols);
1342 redraw_tab(tab);
1345 static int
1346 wrap_page(struct window *window, int width)
1348 struct line *l;
1349 const struct line *orig;
1350 struct vline *vl;
1351 const char *prfx;
1353 orig = window->current_line == NULL
1354 ? NULL
1355 : window->current_line->parent;
1356 window->current_line = NULL;
1358 window->curs_y = 0;
1359 window->line_off = 0;
1361 empty_vlist(window);
1363 TAILQ_FOREACH(l, &window->page.head, lines) {
1364 prfx = line_prefixes[l->type].prfx1;
1365 switch (l->type) {
1366 case LINE_TEXT:
1367 case LINE_LINK:
1368 case LINE_TITLE_1:
1369 case LINE_TITLE_2:
1370 case LINE_TITLE_3:
1371 case LINE_ITEM:
1372 case LINE_QUOTE:
1373 case LINE_PRE_START:
1374 case LINE_PRE_END:
1375 wrap_text(window, prfx, l, width);
1376 break;
1377 case LINE_PRE_CONTENT:
1378 hardwrap_text(window, l, width);
1379 break;
1382 if (orig == l && window->current_line == NULL) {
1383 window->line_off = window->line_max-1;
1384 window->current_line = TAILQ_LAST(&window->head, vhead);
1386 while (1) {
1387 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1388 if (vl == NULL || vl->parent != orig)
1389 break;
1390 window->current_line = vl;
1391 window->line_off--;
1396 if (window->current_line == NULL)
1397 window->current_line = TAILQ_FIRST(&window->head);
1399 return 1;
1402 static void
1403 print_vline(WINDOW *window, struct vline *vl)
1405 const char *text = vl->line;
1406 const char *prfx;
1407 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1408 int text_face = line_faces[vl->parent->type].text_prop;
1410 if (!vl->flags)
1411 prfx = line_prefixes[vl->parent->type].prfx1;
1412 else
1413 prfx = line_prefixes[vl->parent->type].prfx2;
1415 if (text == NULL)
1416 text = "";
1418 wattron(window, prefix_face);
1419 wprintw(window, "%s", prfx);
1420 wattroff(window, prefix_face);
1422 wattron(window, text_face);
1423 wprintw(window, "%s", text);
1424 wattroff(window, text_face);
1427 static void
1428 redraw_tabline(void)
1430 struct tab *tab;
1431 size_t toskip, ots, tabwidth, space, x;
1432 int current, y, truncated;
1433 const char *title;
1434 char buf[25];
1436 tabwidth = sizeof(buf)+1;
1437 space = COLS-2;
1439 toskip = 0;
1440 TAILQ_FOREACH(tab, &tabshead, tabs) {
1441 toskip++;
1442 if (tab->flags & TAB_CURRENT)
1443 break;
1446 if (toskip * tabwidth < space)
1447 toskip = 0;
1448 else {
1449 ots = toskip;
1450 toskip--;
1451 while (toskip != 0 &&
1452 (ots - toskip+1) * tabwidth < space)
1453 toskip--;
1456 werase(tabline);
1457 wattron(tabline, tab_face.background);
1458 wprintw(tabline, toskip == 0 ? " " : "<");
1459 wattroff(tabline, tab_face.background);
1461 truncated = 0;
1462 TAILQ_FOREACH(tab, &tabshead, tabs) {
1463 if (truncated)
1464 break;
1465 if (toskip != 0) {
1466 toskip--;
1467 continue;
1470 getyx(tabline, y, x);
1471 if (x + sizeof(buf)+2 >= (size_t)COLS)
1472 truncated = 1;
1474 current = tab->flags & TAB_CURRENT;
1476 if (*(title = tab->window.page.title) == '\0')
1477 title = tab->hist_cur->h;
1479 strlcpy(buf, " ", sizeof(buf));
1480 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1481 /* truncation happens */
1482 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1483 } else {
1484 /* pad with spaces */
1485 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1486 /* nop */ ;
1489 if (current)
1490 wattron(tabline, tab_face.current_tab);
1491 else
1492 wattron(tabline, tab_face.tab);
1494 wprintw(tabline, "%s", buf);
1495 if (TAILQ_NEXT(tab, tabs) != NULL)
1496 wprintw(tabline, " ");
1498 if (current)
1499 wattroff(tabline, tab_face.current_tab);
1500 else
1501 wattroff(tabline, tab_face.tab);
1504 wattron(tabline, tab_face.background);
1505 for (; x < (size_t)COLS; ++x)
1506 waddch(tabline, ' ');
1507 if (truncated)
1508 mvwprintw(tabline, 0, COLS-1, ">");
1511 static void
1512 redraw_window(WINDOW *win, int height, struct window *window)
1514 struct vline *vl;
1515 int l;
1517 werase(win);
1519 window->line_off = MIN(window->line_max-1, window->line_off);
1520 if (TAILQ_EMPTY(&window->head))
1521 return;
1523 l = 0;
1524 vl = nth_line(window, window->line_off);
1525 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1526 wmove(win, l, 0);
1527 print_vline(win, vl);
1528 l++;
1529 if (l == height)
1530 break;
1533 wmove(win, window->curs_y, window->curs_x);
1536 static void
1537 redraw_help(void)
1539 redraw_window(help, help_lines, &helpwin);
1542 static void
1543 redraw_body(struct tab *tab)
1545 redraw_window(body, body_lines, &tab->window);
1548 static inline char
1549 trust_status_char(enum trust_state ts)
1551 switch (ts) {
1552 case TS_UNKNOWN: return 'u';
1553 case TS_UNTRUSTED: return '!';
1554 case TS_TRUSTED: return 'v';
1555 case TS_VERIFIED: return 'V';
1559 static void
1560 redraw_modeline(struct tab *tab)
1562 double pct;
1563 int x, y, max_x, max_y;
1564 const char *mode = tab->window.page.name;
1565 const char *spin = "-\\|/";
1567 werase(modeline);
1568 wattron(modeline, A_REVERSE);
1569 wmove(modeline, 0, 0);
1571 wprintw(modeline, "-%c%c %s ",
1572 spin[tab->loading_anim_step],
1573 trust_status_char(tab->trust),
1574 mode == NULL ? "(none)" : mode);
1576 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1578 if (tab->window.line_max <= (size_t)body_lines)
1579 wprintw(modeline, "All ");
1580 else if (tab->window.line_off == 0)
1581 wprintw(modeline, "Top ");
1582 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1583 wprintw(modeline, "Bottom ");
1584 else
1585 wprintw(modeline, "%.0f%% ", pct);
1587 wprintw(modeline, "%d/%d %s ",
1588 tab->window.line_off + tab->window.curs_y,
1589 tab->window.line_max,
1590 tab->hist_cur->h);
1592 getyx(modeline, y, x);
1593 getmaxyx(modeline, max_y, max_x);
1595 (void)y;
1596 (void)max_y;
1598 for (; x < max_x; ++x)
1599 waddstr(modeline, "-");
1602 static void
1603 redraw_minibuffer(void)
1605 struct tab *tab;
1606 size_t off_y, off_x = 0;
1607 char *start, *c;
1609 werase(minibuf);
1611 if (in_minibuffer) {
1612 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1613 if (ministate.hist_cur != NULL)
1614 wprintw(minibuf, "(%zu/%zu) ",
1615 ministate.hist_off + 1,
1616 ministate.history->len);
1618 getyx(minibuf, off_y, off_x);
1620 start = ministate.hist_cur != NULL
1621 ? ministate.hist_cur->h
1622 : ministate.buf;
1623 c = utf8_nth(ministate.window.current_line->line,
1624 ministate.window.cpoff);
1625 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1626 start = utf8_next_cp(start);
1629 waddstr(minibuf, start);
1632 if (ministate.curmesg != NULL)
1633 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1634 ministate.curmesg);
1636 if (!in_minibuffer && ministate.curmesg == NULL)
1637 waddstr(minibuf, keybuf);
1639 /* If nothing else, show the URL at point */
1640 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1641 tab = current_tab();
1642 if (tab->window.current_line != NULL &&
1643 tab->window.current_line->parent->type == LINE_LINK)
1644 waddstr(minibuf, tab->window.current_line->parent->alt);
1647 if (in_minibuffer)
1648 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1651 static void
1652 redraw_tab(struct tab *tab)
1654 if (side_window) {
1655 redraw_help();
1656 wnoutrefresh(help);
1659 redraw_tabline();
1660 redraw_body(tab);
1661 redraw_modeline(tab);
1662 redraw_minibuffer();
1664 wnoutrefresh(tabline);
1665 wnoutrefresh(modeline);
1667 if (in_minibuffer) {
1668 wnoutrefresh(body);
1669 wnoutrefresh(minibuf);
1670 } else {
1671 wnoutrefresh(minibuf);
1672 wnoutrefresh(body);
1675 doupdate();
1678 static void
1679 emit_help_item(char *prfx, void *fn)
1681 struct line *l;
1682 struct cmds *cmd;
1684 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1685 if (fn == cmd->fn)
1686 break;
1688 assert(cmd != NULL);
1690 if ((l = calloc(1, sizeof(*l))) == NULL)
1691 abort();
1693 l->type = LINE_TEXT;
1694 l->alt = NULL;
1696 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1698 if (TAILQ_EMPTY(&helpwin.page.head))
1699 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1700 else
1701 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1704 static void
1705 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1707 struct keymap *k;
1708 char p[32];
1709 const char *kn;
1711 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1712 strlcpy(p, prfx, sizeof(p));
1713 if (*p != '\0')
1714 strlcat(p, " ", sizeof(p));
1715 if (k->meta)
1716 strlcat(p, "M-", sizeof(p));
1717 if ((kn = unkbd(k->key)) != NULL)
1718 strlcat(p, kn, sizeof(p));
1719 else
1720 strlcat(p, keyname(k->key), sizeof(p));
1722 if (k->fn == NULL)
1723 rec_compute_help(&k->map, p, sizeof(p));
1724 else
1725 emit_help_item(p, k->fn);
1729 static void
1730 recompute_help(void)
1732 char p[32] = { 0 };
1734 empty_vlist(&helpwin);
1735 empty_linelist(&helpwin);
1736 rec_compute_help(current_map, p, sizeof(p));
1737 wrap_page(&helpwin, help_cols);
1740 static void
1741 vmessage(const char *fmt, va_list ap)
1743 if (evtimer_pending(&clminibufev, NULL))
1744 evtimer_del(&clminibufev);
1745 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1746 evtimer_add(&clminibufev, &clminibufev_timer);
1748 free(ministate.curmesg);
1750 /* TODO: what to do if the allocation fails here? */
1751 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1752 ministate.curmesg = NULL;
1754 redraw_minibuffer();
1755 if (in_minibuffer) {
1756 wrefresh(body);
1757 wrefresh(minibuf);
1758 } else {
1759 wrefresh(minibuf);
1760 wrefresh(body);
1764 static void
1765 message(const char *fmt, ...)
1767 va_list ap;
1769 va_start(ap, fmt);
1770 vmessage(fmt, ap);
1771 va_end(ap);
1774 static void
1775 start_loading_anim(struct tab *tab)
1777 if (tab->loading_anim)
1778 return;
1779 tab->loading_anim = 1;
1780 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1781 evtimer_add(&tab->loadingev, &loadingev_timer);
1784 static void
1785 update_loading_anim(int fd, short ev, void *d)
1787 struct tab *tab = d;
1789 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1791 if (tab->flags & TAB_CURRENT) {
1792 redraw_modeline(tab);
1793 wrefresh(modeline);
1794 wrefresh(body);
1795 if (in_minibuffer)
1796 wrefresh(minibuf);
1799 evtimer_add(&tab->loadingev, &loadingev_timer);
1802 static void
1803 stop_loading_anim(struct tab *tab)
1805 if (!tab->loading_anim)
1806 return;
1807 evtimer_del(&tab->loadingev);
1808 tab->loading_anim = 0;
1809 tab->loading_anim_step = 0;
1811 if (!(tab->flags & TAB_CURRENT))
1812 return;
1814 redraw_modeline(tab);
1816 wrefresh(modeline);
1817 wrefresh(body);
1818 if (in_minibuffer)
1819 wrefresh(minibuf);
1822 static void
1823 load_url_in_tab(struct tab *tab, const char *url)
1825 message("Loading %s...", url);
1826 start_loading_anim(tab);
1827 load_url(tab, url);
1829 tab->window.curs_x = 0;
1830 tab->window.curs_y = 0;
1831 redraw_tab(tab);
1834 static void
1835 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1836 void (*abortfn)(void), struct histhead *hist)
1838 in_minibuffer = 1;
1839 base_map = &minibuffer_map;
1840 current_map = &minibuffer_map;
1842 base_map->unhandled_input = self_insert_fn;
1844 ministate.donefn = donefn;
1845 ministate.abortfn = abortfn;
1846 memset(ministate.buf, 0, sizeof(ministate.buf));
1847 ministate.window.current_line = &ministate.vline;
1848 ministate.window.current_line->line = ministate.buf;
1849 ministate.window.cpoff = 0;
1850 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1852 ministate.history = hist;
1853 ministate.hist_cur = NULL;
1854 ministate.hist_off = 0;
1857 static void
1858 exit_minibuffer(void)
1860 werase(minibuf);
1862 in_minibuffer = 0;
1863 base_map = &global_map;
1864 current_map = &global_map;
1867 static void
1868 switch_to_tab(struct tab *tab)
1870 struct tab *t;
1872 TAILQ_FOREACH(t, &tabshead, tabs) {
1873 t->flags &= ~TAB_CURRENT;
1876 tab->flags |= TAB_CURRENT;
1879 unsigned int
1880 tab_new_id(void)
1882 return tab_counter++;
1885 static struct tab *
1886 new_tab(const char *url)
1888 struct tab *tab;
1890 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1891 event_loopbreak();
1892 return NULL;
1895 TAILQ_INIT(&tab->hist.head);
1897 TAILQ_INIT(&tab->window.head);
1899 tab->id = tab_new_id();
1900 switch_to_tab(tab);
1902 if (TAILQ_EMPTY(&tabshead))
1903 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1904 else
1905 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1907 load_url_in_tab(tab, url);
1908 return tab;
1911 static void
1912 session_new_tab_cb(const char *url)
1914 new_tab(url);
1917 static void
1918 usage(void)
1920 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1923 int
1924 ui_init(int argc, char * const *argv)
1926 const char *url = NEW_TAB_URL;
1927 int ch;
1929 while ((ch = getopt(argc, argv, "")) != -1) {
1930 switch (ch) {
1931 default:
1932 usage();
1933 return 0;
1936 argc -= optind;
1937 argv += optind;
1939 if (argc != 0)
1940 url = argv[0];
1942 setlocale(LC_ALL, "");
1944 TAILQ_INIT(&global_map.m);
1945 global_map.unhandled_input = global_key_unbound;
1947 TAILQ_INIT(&minibuffer_map.m);
1949 TAILQ_INIT(&eecmd_history.head);
1950 TAILQ_INIT(&ir_history.head);
1951 TAILQ_INIT(&lu_history.head);
1953 ministate.line.type = LINE_TEXT;
1954 ministate.vline.parent = &ministate.line;
1955 ministate.window.current_line = &ministate.vline;
1957 /* initialize help window */
1958 TAILQ_INIT(&helpwin.head);
1960 base_map = &global_map;
1961 current_map = &global_map;
1962 load_default_keys();
1964 initscr();
1965 raw();
1966 noecho();
1968 nonl();
1969 intrflush(stdscr, FALSE);
1971 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1972 return 0;
1973 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1974 return 0;
1975 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1976 return 0;
1977 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1978 return 0;
1979 if ((help = newwin(1, 1, 1, 0)) == NULL)
1980 return 0;
1982 body_lines = LINES-3;
1983 body_cols = COLS;
1985 keypad(body, TRUE);
1986 scrollok(body, TRUE);
1988 /* non-blocking input */
1989 wtimeout(body, 0);
1991 mvwprintw(body, 0, 0, "");
1993 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1994 event_add(&stdioev, NULL);
1996 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1997 signal_add(&winchev, NULL);
1999 load_last_session(session_new_tab_cb);
2000 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2001 new_tab(url);
2003 return 1;
2006 void
2007 ui_on_tab_loaded(struct tab *tab)
2009 stop_loading_anim(tab);
2010 message("Loaded %s", tab->hist_cur->h);
2012 redraw_tabline();
2013 wrefresh(tabline);
2014 if (in_minibuffer)
2015 wrefresh(minibuf);
2016 else
2017 wrefresh(body);
2020 void
2021 ui_on_tab_refresh(struct tab *tab)
2023 wrap_page(&tab->window, body_cols);
2024 if (tab->flags & TAB_CURRENT) {
2025 restore_cursor(&tab->window);
2026 redraw_tab(tab);
2030 void
2031 ui_require_input(struct tab *tab, int hide)
2033 /* TODO: hard-switching to another tab is ugly */
2034 switch_to_tab(tab);
2036 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2037 &ir_history);
2038 strlcpy(ministate.prompt, "Input required: ",
2039 sizeof(ministate.prompt));
2040 redraw_tab(tab);
2043 void
2044 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2045 unsigned int data)
2047 size_t len;
2049 yornp_cb = fn;
2050 yornp_data = data;
2051 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2052 yornp_abort, NULL);
2054 len = sizeof(ministate.prompt);
2055 strlcpy(ministate.prompt, prompt, len);
2056 strlcat(ministate.prompt, " (y or n) ", len);
2057 redraw_tab(current_tab());
2060 void
2061 ui_notify(const char *fmt, ...)
2063 va_list ap;
2065 va_start(ap, fmt);
2066 vmessage(fmt, ap);
2067 va_end(ap);
2070 void
2071 ui_end(void)
2073 endwin();