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));
1102 phos_uri_set_query(&uri, ministate.buf);
1103 phos_serialize_uri(&uri, buf, sizeof(buf));
1104 load_url_in_tab(tab, buf);
1107 static void
1108 lu_self_insert(void)
1110 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1111 !unicode_isgraph(thiskey.key)) {
1112 global_key_unbound();
1113 return;
1116 minibuffer_self_insert();
1119 static void
1120 lu_select(void)
1122 exit_minibuffer();
1123 minibuffer_hist_save_entry();
1124 load_url_in_tab(current_tab(), ministate.buf);
1127 static void
1128 bp_select(void)
1130 exit_minibuffer();
1131 if (*ministate.buf != '\0')
1132 add_to_bookmarks(ministate.buf);
1133 else
1134 message("Abort.");
1137 static void
1138 yornp_self_insert(void)
1140 if (thiskey.key != 'y' && thiskey.key != 'n') {
1141 message("Please answer y or n");
1142 return;
1145 exit_minibuffer();
1146 yornp_cb(thiskey.key == 'y', yornp_data);
1149 static void
1150 yornp_abort(void)
1152 exit_minibuffer();
1153 yornp_cb(0, yornp_data);
1156 static struct vline *
1157 nth_line(struct window *window, size_t n)
1159 struct vline *vl;
1160 size_t i;
1162 i = 0;
1163 TAILQ_FOREACH(vl, &window->head, vlines) {
1164 if (i == n)
1165 return vl;
1166 i++;
1169 /* unreachable */
1170 abort();
1173 static struct tab *
1174 current_tab(void)
1176 struct tab *t;
1178 TAILQ_FOREACH(t, &tabshead, tabs) {
1179 if (t->flags & TAB_CURRENT)
1180 return t;
1183 /* unreachable */
1184 abort();
1187 static struct window *
1188 current_window(void)
1190 if (in_minibuffer)
1191 return &ministate.window;
1192 return &current_tab()->window;
1195 static int
1196 readkey(void)
1198 uint32_t state = 0;
1200 if ((thiskey.key = wgetch(body)) == ERR)
1201 return 0;
1203 thiskey.meta = thiskey.key == 27;
1204 if (thiskey.meta) {
1205 thiskey.key = wgetch(body);
1206 if (thiskey.key == ERR || thiskey.key == 27) {
1207 thiskey.meta = 0;
1208 thiskey.key = 27;
1212 thiskey.cp = 0;
1213 if ((unsigned int)thiskey.key < UINT8_MAX) {
1214 while (1) {
1215 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1216 break;
1217 if ((thiskey.key = wgetch(body)) == ERR) {
1218 message("Error decoding user input");
1219 return 0;
1224 return 1;
1227 static void
1228 dispatch_stdio(int fd, short ev, void *d)
1230 struct keymap *k;
1231 const char *keyname;
1232 char tmp[5] = {0};
1234 if (!readkey())
1235 return;
1237 if (keybuf[0] != '\0')
1238 strlcat(keybuf, " ", sizeof(keybuf));
1239 if (thiskey.meta)
1240 strlcat(keybuf, "M-", sizeof(keybuf));
1241 if (thiskey.cp != 0) {
1242 utf8_encode(thiskey.cp, tmp);
1243 strlcat(keybuf, tmp, sizeof(keybuf));
1244 } else {
1245 if ((keyname = unkbd(thiskey.key)) != NULL)
1246 strlcat(keybuf, keyname, sizeof(keybuf));
1247 else {
1248 tmp[0] = thiskey.key;
1249 strlcat(keybuf, tmp, sizeof(keybuf));
1253 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1254 if (k->meta == thiskey.meta &&
1255 k->key == thiskey.key) {
1256 if (k->fn == NULL)
1257 current_map = &k->map;
1258 else {
1259 current_map = base_map;
1260 strlcpy(keybuf, "", sizeof(keybuf));
1261 k->fn(current_window());
1263 goto done;
1267 if (current_map->unhandled_input != NULL)
1268 current_map->unhandled_input();
1269 else {
1270 global_key_unbound();
1273 strlcpy(keybuf, "", sizeof(keybuf));
1274 current_map = base_map;
1276 done:
1277 if (side_window)
1278 recompute_help();
1280 redraw_tab(current_tab());
1283 static void
1284 handle_clear_minibuf(int fd, short ev, void *d)
1286 free(ministate.curmesg);
1287 ministate.curmesg = NULL;
1289 redraw_minibuffer();
1290 if (in_minibuffer) {
1291 wrefresh(body);
1292 wrefresh(minibuf);
1293 } else {
1294 wrefresh(minibuf);
1295 wrefresh(body);
1299 static void
1300 handle_resize(int sig, short ev, void *d)
1302 struct tab *tab;
1304 endwin();
1305 refresh();
1306 clear();
1308 /* move and resize the windows, in reverse order! */
1310 mvwin(minibuf, LINES-1, 0);
1311 wresize(minibuf, 1, COLS);
1313 mvwin(modeline, LINES-2, 0);
1314 wresize(modeline, 1, COLS);
1316 body_lines = LINES-3;
1317 body_cols = COLS;
1319 if (side_window) {
1320 help_cols = 0.3 * COLS;
1321 help_lines = LINES-3;
1322 mvwin(help, 1, 0);
1323 wresize(help, help_lines, help_cols);
1325 wrap_page(&helpwin, help_cols);
1327 body_cols = COLS - help_cols - 1;
1328 mvwin(body, 1, help_cols);
1329 } else
1330 mvwin(body, 1, 0);
1332 wresize(body, body_lines, body_cols);
1334 wresize(tabline, 1, COLS);
1336 tab = current_tab();
1338 wrap_page(&tab->window, body_cols);
1339 redraw_tab(tab);
1342 static int
1343 wrap_page(struct window *window, int width)
1345 struct line *l;
1346 const struct line *orig;
1347 struct vline *vl;
1348 const char *prfx;
1350 orig = window->current_line == NULL
1351 ? NULL
1352 : window->current_line->parent;
1353 window->current_line = NULL;
1355 window->curs_y = 0;
1356 window->line_off = 0;
1358 empty_vlist(window);
1360 TAILQ_FOREACH(l, &window->page.head, lines) {
1361 prfx = line_prefixes[l->type].prfx1;
1362 switch (l->type) {
1363 case LINE_TEXT:
1364 case LINE_LINK:
1365 case LINE_TITLE_1:
1366 case LINE_TITLE_2:
1367 case LINE_TITLE_3:
1368 case LINE_ITEM:
1369 case LINE_QUOTE:
1370 case LINE_PRE_START:
1371 case LINE_PRE_END:
1372 wrap_text(window, prfx, l, width);
1373 break;
1374 case LINE_PRE_CONTENT:
1375 hardwrap_text(window, l, width);
1376 break;
1379 if (orig == l && window->current_line == NULL) {
1380 window->line_off = window->line_max-1;
1381 window->current_line = TAILQ_LAST(&window->head, vhead);
1383 while (1) {
1384 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1385 if (vl == NULL || vl->parent != orig)
1386 break;
1387 window->current_line = vl;
1388 window->line_off--;
1393 if (window->current_line == NULL)
1394 window->current_line = TAILQ_FIRST(&window->head);
1396 return 1;
1399 static void
1400 print_vline(WINDOW *window, struct vline *vl)
1402 const char *text = vl->line;
1403 const char *prfx;
1404 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1405 int text_face = line_faces[vl->parent->type].text_prop;
1407 if (!vl->flags)
1408 prfx = line_prefixes[vl->parent->type].prfx1;
1409 else
1410 prfx = line_prefixes[vl->parent->type].prfx2;
1412 if (text == NULL)
1413 text = "";
1415 wattron(window, prefix_face);
1416 wprintw(window, "%s", prfx);
1417 wattroff(window, prefix_face);
1419 wattron(window, text_face);
1420 wprintw(window, "%s", text);
1421 wattroff(window, text_face);
1424 static void
1425 redraw_tabline(void)
1427 struct tab *tab;
1428 size_t toskip, ots, tabwidth, space, x;
1429 int current, y, truncated;
1430 const char *title;
1431 char buf[25];
1433 tabwidth = sizeof(buf)+1;
1434 space = COLS-2;
1436 toskip = 0;
1437 TAILQ_FOREACH(tab, &tabshead, tabs) {
1438 toskip++;
1439 if (tab->flags & TAB_CURRENT)
1440 break;
1443 if (toskip * tabwidth < space)
1444 toskip = 0;
1445 else {
1446 ots = toskip;
1447 toskip--;
1448 while (toskip != 0 &&
1449 (ots - toskip+1) * tabwidth < space)
1450 toskip--;
1453 werase(tabline);
1454 wattron(tabline, tab_face.background);
1455 wprintw(tabline, toskip == 0 ? " " : "<");
1456 wattroff(tabline, tab_face.background);
1458 truncated = 0;
1459 TAILQ_FOREACH(tab, &tabshead, tabs) {
1460 if (truncated)
1461 break;
1462 if (toskip != 0) {
1463 toskip--;
1464 continue;
1467 getyx(tabline, y, x);
1468 if (x + sizeof(buf)+2 >= (size_t)COLS)
1469 truncated = 1;
1471 current = tab->flags & TAB_CURRENT;
1473 if (*(title = tab->window.page.title) == '\0')
1474 title = tab->hist_cur->h;
1476 strlcpy(buf, " ", sizeof(buf));
1477 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1478 /* truncation happens */
1479 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1480 } else {
1481 /* pad with spaces */
1482 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1483 /* nop */ ;
1486 if (current)
1487 wattron(tabline, tab_face.current_tab);
1488 else
1489 wattron(tabline, tab_face.tab);
1491 wprintw(tabline, "%s", buf);
1492 if (TAILQ_NEXT(tab, tabs) != NULL)
1493 wprintw(tabline, " ");
1495 if (current)
1496 wattroff(tabline, tab_face.current_tab);
1497 else
1498 wattroff(tabline, tab_face.tab);
1501 wattron(tabline, tab_face.background);
1502 for (; x < (size_t)COLS; ++x)
1503 waddch(tabline, ' ');
1504 if (truncated)
1505 mvwprintw(tabline, 0, COLS-1, ">");
1508 static void
1509 redraw_window(WINDOW *win, int height, struct window *window)
1511 struct vline *vl;
1512 int l;
1514 werase(win);
1516 window->line_off = MIN(window->line_max-1, window->line_off);
1517 if (TAILQ_EMPTY(&window->head))
1518 return;
1520 l = 0;
1521 vl = nth_line(window, window->line_off);
1522 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1523 wmove(win, l, 0);
1524 print_vline(win, vl);
1525 l++;
1526 if (l == height)
1527 break;
1530 wmove(win, window->curs_y, window->curs_x);
1533 static void
1534 redraw_help(void)
1536 redraw_window(help, help_lines, &helpwin);
1539 static void
1540 redraw_body(struct tab *tab)
1542 redraw_window(body, body_lines, &tab->window);
1545 static inline char
1546 trust_status_char(enum trust_state ts)
1548 switch (ts) {
1549 case TS_UNKNOWN: return 'u';
1550 case TS_UNTRUSTED: return '!';
1551 case TS_TRUSTED: return 'v';
1552 case TS_VERIFIED: return 'V';
1556 static void
1557 redraw_modeline(struct tab *tab)
1559 double pct;
1560 int x, y, max_x, max_y;
1561 const char *mode = tab->window.page.name;
1562 const char *spin = "-\\|/";
1564 werase(modeline);
1565 wattron(modeline, A_REVERSE);
1566 wmove(modeline, 0, 0);
1568 wprintw(modeline, "-%c%c %s ",
1569 spin[tab->loading_anim_step],
1570 trust_status_char(tab->trust),
1571 mode == NULL ? "(none)" : mode);
1573 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1575 if (tab->window.line_max <= (size_t)body_lines)
1576 wprintw(modeline, "All ");
1577 else if (tab->window.line_off == 0)
1578 wprintw(modeline, "Top ");
1579 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1580 wprintw(modeline, "Bottom ");
1581 else
1582 wprintw(modeline, "%.0f%% ", pct);
1584 wprintw(modeline, "%d/%d %s ",
1585 tab->window.line_off + tab->window.curs_y,
1586 tab->window.line_max,
1587 tab->hist_cur->h);
1589 getyx(modeline, y, x);
1590 getmaxyx(modeline, max_y, max_x);
1592 (void)y;
1593 (void)max_y;
1595 for (; x < max_x; ++x)
1596 waddstr(modeline, "-");
1599 static void
1600 redraw_minibuffer(void)
1602 struct tab *tab;
1603 size_t off_y, off_x = 0;
1604 char *start, *c;
1606 werase(minibuf);
1608 if (in_minibuffer) {
1609 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1610 if (ministate.hist_cur != NULL)
1611 wprintw(minibuf, "(%zu/%zu) ",
1612 ministate.hist_off + 1,
1613 ministate.history->len);
1615 getyx(minibuf, off_y, off_x);
1617 start = ministate.hist_cur != NULL
1618 ? ministate.hist_cur->h
1619 : ministate.buf;
1620 c = utf8_nth(ministate.window.current_line->line,
1621 ministate.window.cpoff);
1622 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1623 start = utf8_next_cp(start);
1626 waddstr(minibuf, start);
1629 if (ministate.curmesg != NULL)
1630 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1631 ministate.curmesg);
1633 if (!in_minibuffer && ministate.curmesg == NULL)
1634 waddstr(minibuf, keybuf);
1636 /* If nothing else, show the URL at point */
1637 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1638 tab = current_tab();
1639 if (tab->window.current_line != NULL &&
1640 tab->window.current_line->parent->type == LINE_LINK)
1641 waddstr(minibuf, tab->window.current_line->parent->alt);
1644 if (in_minibuffer)
1645 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1648 static void
1649 redraw_tab(struct tab *tab)
1651 if (side_window) {
1652 redraw_help();
1653 wnoutrefresh(help);
1656 redraw_tabline();
1657 redraw_body(tab);
1658 redraw_modeline(tab);
1659 redraw_minibuffer();
1661 wnoutrefresh(tabline);
1662 wnoutrefresh(modeline);
1664 if (in_minibuffer) {
1665 wnoutrefresh(body);
1666 wnoutrefresh(minibuf);
1667 } else {
1668 wnoutrefresh(minibuf);
1669 wnoutrefresh(body);
1672 doupdate();
1675 static void
1676 emit_help_item(char *prfx, void *fn)
1678 struct line *l;
1679 struct cmds *cmd;
1681 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1682 if (fn == cmd->fn)
1683 break;
1685 assert(cmd != NULL);
1687 if ((l = calloc(1, sizeof(*l))) == NULL)
1688 abort();
1690 l->type = LINE_TEXT;
1691 l->alt = NULL;
1693 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1695 if (TAILQ_EMPTY(&helpwin.page.head))
1696 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1697 else
1698 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1701 static void
1702 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1704 struct keymap *k;
1705 char p[32];
1706 const char *kn;
1708 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1709 strlcpy(p, prfx, sizeof(p));
1710 if (*p != '\0')
1711 strlcat(p, " ", sizeof(p));
1712 if (k->meta)
1713 strlcat(p, "M-", sizeof(p));
1714 if ((kn = unkbd(k->key)) != NULL)
1715 strlcat(p, kn, sizeof(p));
1716 else
1717 strlcat(p, keyname(k->key), sizeof(p));
1719 if (k->fn == NULL)
1720 rec_compute_help(&k->map, p, sizeof(p));
1721 else
1722 emit_help_item(p, k->fn);
1726 static void
1727 recompute_help(void)
1729 char p[32] = { 0 };
1731 empty_vlist(&helpwin);
1732 empty_linelist(&helpwin);
1733 rec_compute_help(current_map, p, sizeof(p));
1734 wrap_page(&helpwin, help_cols);
1737 static void
1738 vmessage(const char *fmt, va_list ap)
1740 if (evtimer_pending(&clminibufev, NULL))
1741 evtimer_del(&clminibufev);
1742 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1743 evtimer_add(&clminibufev, &clminibufev_timer);
1745 free(ministate.curmesg);
1747 /* TODO: what to do if the allocation fails here? */
1748 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1749 ministate.curmesg = NULL;
1751 redraw_minibuffer();
1752 if (in_minibuffer) {
1753 wrefresh(body);
1754 wrefresh(minibuf);
1755 } else {
1756 wrefresh(minibuf);
1757 wrefresh(body);
1761 static void
1762 message(const char *fmt, ...)
1764 va_list ap;
1766 va_start(ap, fmt);
1767 vmessage(fmt, ap);
1768 va_end(ap);
1771 static void
1772 start_loading_anim(struct tab *tab)
1774 if (tab->loading_anim)
1775 return;
1776 tab->loading_anim = 1;
1777 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1778 evtimer_add(&tab->loadingev, &loadingev_timer);
1781 static void
1782 update_loading_anim(int fd, short ev, void *d)
1784 struct tab *tab = d;
1786 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1788 if (tab->flags & TAB_CURRENT) {
1789 redraw_modeline(tab);
1790 wrefresh(modeline);
1791 wrefresh(body);
1792 if (in_minibuffer)
1793 wrefresh(minibuf);
1796 evtimer_add(&tab->loadingev, &loadingev_timer);
1799 static void
1800 stop_loading_anim(struct tab *tab)
1802 if (!tab->loading_anim)
1803 return;
1804 evtimer_del(&tab->loadingev);
1805 tab->loading_anim = 0;
1806 tab->loading_anim_step = 0;
1808 if (!(tab->flags & TAB_CURRENT))
1809 return;
1811 redraw_modeline(tab);
1813 wrefresh(modeline);
1814 wrefresh(body);
1815 if (in_minibuffer)
1816 wrefresh(minibuf);
1819 static void
1820 load_url_in_tab(struct tab *tab, const char *url)
1822 message("Loading %s...", url);
1823 start_loading_anim(tab);
1824 load_url(tab, url);
1826 tab->window.curs_x = 0;
1827 tab->window.curs_y = 0;
1828 redraw_tab(tab);
1831 static void
1832 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1833 void (*abortfn)(void), struct histhead *hist)
1835 in_minibuffer = 1;
1836 base_map = &minibuffer_map;
1837 current_map = &minibuffer_map;
1839 base_map->unhandled_input = self_insert_fn;
1841 ministate.donefn = donefn;
1842 ministate.abortfn = abortfn;
1843 memset(ministate.buf, 0, sizeof(ministate.buf));
1844 ministate.window.current_line = &ministate.vline;
1845 ministate.window.current_line->line = ministate.buf;
1846 ministate.window.cpoff = 0;
1847 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1849 ministate.history = hist;
1850 ministate.hist_cur = NULL;
1851 ministate.hist_off = 0;
1854 static void
1855 exit_minibuffer(void)
1857 werase(minibuf);
1859 in_minibuffer = 0;
1860 base_map = &global_map;
1861 current_map = &global_map;
1864 static void
1865 switch_to_tab(struct tab *tab)
1867 struct tab *t;
1869 TAILQ_FOREACH(t, &tabshead, tabs) {
1870 t->flags &= ~TAB_CURRENT;
1873 tab->flags |= TAB_CURRENT;
1876 unsigned int
1877 tab_new_id(void)
1879 return tab_counter++;
1882 static struct tab *
1883 new_tab(const char *url)
1885 struct tab *tab;
1887 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1888 event_loopbreak();
1889 return NULL;
1892 TAILQ_INIT(&tab->hist.head);
1894 TAILQ_INIT(&tab->window.head);
1896 tab->id = tab_new_id();
1897 switch_to_tab(tab);
1899 if (TAILQ_EMPTY(&tabshead))
1900 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1901 else
1902 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1904 load_url_in_tab(tab, url);
1905 return tab;
1908 static void
1909 session_new_tab_cb(const char *url)
1911 new_tab(url);
1914 static void
1915 usage(void)
1917 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1920 int
1921 ui_init(int argc, char * const *argv)
1923 const char *url = NEW_TAB_URL;
1924 int ch;
1926 while ((ch = getopt(argc, argv, "")) != -1) {
1927 switch (ch) {
1928 default:
1929 usage();
1930 return 0;
1933 argc -= optind;
1934 argv += optind;
1936 if (argc != 0)
1937 url = argv[0];
1939 setlocale(LC_ALL, "");
1941 TAILQ_INIT(&global_map.m);
1942 global_map.unhandled_input = global_key_unbound;
1944 TAILQ_INIT(&minibuffer_map.m);
1946 TAILQ_INIT(&eecmd_history.head);
1947 TAILQ_INIT(&ir_history.head);
1948 TAILQ_INIT(&lu_history.head);
1950 ministate.line.type = LINE_TEXT;
1951 ministate.vline.parent = &ministate.line;
1952 ministate.window.current_line = &ministate.vline;
1954 /* initialize help window */
1955 TAILQ_INIT(&helpwin.head);
1957 base_map = &global_map;
1958 current_map = &global_map;
1959 load_default_keys();
1961 initscr();
1962 raw();
1963 noecho();
1965 nonl();
1966 intrflush(stdscr, FALSE);
1968 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1969 return 0;
1970 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1971 return 0;
1972 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1973 return 0;
1974 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1975 return 0;
1976 if ((help = newwin(1, 1, 1, 0)) == NULL)
1977 return 0;
1979 body_lines = LINES-3;
1980 body_cols = COLS;
1982 keypad(body, TRUE);
1983 scrollok(body, TRUE);
1985 /* non-blocking input */
1986 wtimeout(body, 0);
1988 mvwprintw(body, 0, 0, "");
1990 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1991 event_add(&stdioev, NULL);
1993 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1994 signal_add(&winchev, NULL);
1996 load_last_session(session_new_tab_cb);
1997 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1998 new_tab(url);
2000 return 1;
2003 void
2004 ui_on_tab_loaded(struct tab *tab)
2006 stop_loading_anim(tab);
2007 message("Loaded %s", tab->hist_cur->h);
2009 redraw_tabline();
2010 wrefresh(tabline);
2011 if (in_minibuffer)
2012 wrefresh(minibuf);
2013 else
2014 wrefresh(body);
2017 void
2018 ui_on_tab_refresh(struct tab *tab)
2020 wrap_page(&tab->window, body_cols);
2021 if (tab->flags & TAB_CURRENT) {
2022 restore_cursor(&tab->window);
2023 redraw_tab(tab);
2027 void
2028 ui_require_input(struct tab *tab, int hide)
2030 /* TODO: hard-switching to another tab is ugly */
2031 switch_to_tab(tab);
2033 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2034 &ir_history);
2035 strlcpy(ministate.prompt, "Input required: ",
2036 sizeof(ministate.prompt));
2037 redraw_tab(tab);
2040 void
2041 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2042 unsigned int data)
2044 size_t len;
2046 if (in_minibuffer) {
2047 fn(0, data);
2048 return;
2051 yornp_cb = fn;
2052 yornp_data = data;
2053 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2054 yornp_abort, NULL);
2056 len = sizeof(ministate.prompt);
2057 strlcpy(ministate.prompt, prompt, len);
2058 strlcat(ministate.prompt, " (y or n) ", len);
2059 redraw_tab(current_tab());
2062 void
2063 ui_notify(const char *fmt, ...)
2065 va_list ap;
2067 va_start(ap, fmt);
2068 vmessage(fmt, ap);
2069 va_end(ap);
2072 void
2073 ui_end(void)
2075 endwin();