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 *)
57 #define DEFALIAS(s, d) /* nothing */
59 CMD(cmd_previous_line);
60 CMD(cmd_next_line);
61 CMD(cmd_backward_char);
62 CMD(cmd_forward_char);
63 CMD(cmd_backward_paragraph);
64 CMD(cmd_forward_paragraph);
65 CMD(cmd_move_beginning_of_line);
66 CMD(cmd_move_end_of_line);
67 CMD(cmd_redraw);
68 CMD(cmd_scroll_line_down);
69 CMD(cmd_scroll_line_up);
70 CMD(cmd_scroll_up);
71 CMD(cmd_scroll_down);
72 CMD(cmd_beginning_of_buffer);
73 CMD(cmd_end_of_buffer);
75 CMD(cmd_kill_telescope);
76 DEFALIAS(q, cmd_kill_telescope)
77 DEFALIAS(wq, cmd_kill_telescope)
79 CMD(cmd_push_button);
80 CMD(cmd_push_button_new_tab);
81 CMD(cmd_previous_button);
82 CMD(cmd_next_button);
83 CMD(cmd_previous_page);
84 CMD(cmd_next_page);
85 CMD(cmd_clear_minibuf);
86 CMD(cmd_execute_extended_command);
87 CMD(cmd_tab_close);
88 CMD(cmd_tab_close_other);
89 CMD(cmd_tab_new);
90 CMD(cmd_tab_next);
91 CMD(cmd_tab_previous);
92 CMD(cmd_tab_move);
93 CMD(cmd_tab_move_to);
94 CMD(cmd_load_url);
95 CMD(cmd_load_current_url);
96 CMD(cmd_bookmark_page);
97 CMD(cmd_list_bookmarks);
98 CMD(cmd_toggle_help);
100 CMD(cmd_mini_delete_char);
101 CMD(cmd_mini_delete_backward_char);
102 CMD(cmd_mini_kill_line);
103 CMD(cmd_mini_abort);
104 CMD(cmd_mini_complete_and_exit);
105 CMD(cmd_mini_previous_history_element);
106 CMD(cmd_mini_next_history_element);
108 #include "cmd.gen.h"
110 static void global_key_unbound(void);
111 static void minibuffer_hist_save_entry(void);
112 static void minibuffer_taint_hist(void);
113 static void minibuffer_self_insert(void);
114 static void eecmd_self_insert(void);
115 static void eecmd_select(void);
116 static void ir_self_insert(void);
117 static void ir_select(void);
118 static void lu_self_insert(void);
119 static void lu_select(void);
120 static void bp_select(void);
121 static void yornp_self_insert(void);
122 static void yornp_abort(void);
124 static struct vline *nth_line(struct window*, size_t);
125 static struct tab *current_tab(void);
126 static struct window *current_window(void);
127 static int readkey(void);
128 static void dispatch_stdio(int, short, void*);
129 static void handle_clear_minibuf(int, short, void*);
130 static void handle_resize(int, short, void*);
131 static int wrap_page(struct window*, int);
132 static void print_vline(WINDOW*, struct vline*);
133 static void redraw_tabline(void);
134 static void redraw_window(WINDOW*, int, struct window*);
135 static void redraw_help(void);
136 static void redraw_body(struct tab*);
137 static void redraw_modeline(struct tab*);
138 static void redraw_minibuffer(void);
139 static void redraw_tab(struct tab*);
140 static void emit_help_item(char*, void*);
141 static void rec_compute_help(struct kmap*, char*, size_t);
142 static void recompute_help(void);
143 static void vmessage(const char*, va_list);
144 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
145 static void start_loading_anim(struct tab*);
146 static void update_loading_anim(int, short, void*);
147 static void stop_loading_anim(struct tab*);
148 static void load_url_in_tab(struct tab*, const char*);
149 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
150 static void exit_minibuffer(void);
151 static void switch_to_tab(struct tab*);
152 static struct tab *new_tab(const char*);
153 static void session_new_tab_cb(const char*);
154 static void usage(void);
156 static struct { short meta; int key; uint32_t cp; } thiskey;
158 static WINDOW *tabline, *body, *modeline, *minibuf;
159 static int body_lines, body_cols;
161 static WINDOW *help;
162 static struct window helpwin;
163 static int help_lines, help_cols;
165 static int side_window;
167 static struct event clminibufev;
168 static struct timeval clminibufev_timer = { 5, 0 };
169 static struct timeval loadingev_timer = { 0, 250000 };
171 static uint32_t tab_counter;
173 static char keybuf[64];
175 static void (*yornp_cb)(int, unsigned int);
176 static unsigned int yornp_data;
178 struct kmap global_map,
179 minibuffer_map,
180 *current_map,
181 *base_map;
183 static struct histhead eecmd_history,
184 ir_history,
185 lu_history;
187 static int in_minibuffer;
189 static struct {
190 char *curmesg;
192 char prompt[64];
193 void (*donefn)(void);
194 void (*abortfn)(void);
196 char buf[1025];
197 struct line line;
198 struct vline vline;
199 struct window window;
201 struct histhead *history;
202 struct hist *hist_cur;
203 size_t hist_off;
204 } ministate;
206 struct lineprefix {
207 const char *prfx1;
208 const char *prfx2;
209 } line_prefixes[] = {
210 [LINE_TEXT] = { "", "" },
211 [LINE_LINK] = { "=> ", " " },
212 [LINE_TITLE_1] = { "# ", " " },
213 [LINE_TITLE_2] = { "## ", " " },
214 [LINE_TITLE_3] = { "### ", " " },
215 [LINE_ITEM] = { "* ", " " },
216 [LINE_QUOTE] = { "> ", " " },
217 [LINE_PRE_START] = { "```", " " },
218 [LINE_PRE_CONTENT] = { "", "" },
219 [LINE_PRE_END] = { "```", "```" },
220 };
222 static struct line_face {
223 int prefix_prop;
224 int text_prop;
225 } line_faces[] = {
226 [LINE_TEXT] = { 0, 0 },
227 [LINE_LINK] = { 0, A_UNDERLINE },
228 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
229 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
230 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
231 [LINE_ITEM] = { 0, 0 },
232 [LINE_QUOTE] = { 0, A_DIM },
233 [LINE_PRE_START] = { 0, 0 },
234 [LINE_PRE_CONTENT] = { 0, 0 },
235 [LINE_PRE_END] = { 0, 0 },
236 };
238 static struct tab_face {
239 int background, tab, current_tab;
240 } tab_face = {
241 A_REVERSE, A_REVERSE, A_NORMAL
242 };
244 static inline void
245 global_set_key(const char *key, void (*fn)(struct window*))
247 if (!kmap_define_key(&global_map, key, fn))
248 _exit(1);
251 static inline void
252 minibuffer_set_key(const char *key, void (*fn)(struct window*))
254 if (!kmap_define_key(&minibuffer_map, key, fn))
255 _exit(1);
258 static void
259 load_default_keys(void)
261 /* === global map === */
263 /* emacs */
264 global_set_key("C-p", cmd_previous_line);
265 global_set_key("C-n", cmd_next_line);
266 global_set_key("C-f", cmd_forward_char);
267 global_set_key("C-b", cmd_backward_char);
268 global_set_key("M-{", cmd_backward_paragraph);
269 global_set_key("M-}", cmd_forward_paragraph);
270 global_set_key("C-a", cmd_move_beginning_of_line);
271 global_set_key("C-e", cmd_move_end_of_line);
273 global_set_key("M-v", cmd_scroll_up);
274 global_set_key("C-v", cmd_scroll_down);
275 global_set_key("M-space", cmd_scroll_up);
276 global_set_key("space", cmd_scroll_down);
278 global_set_key("M-<", cmd_beginning_of_buffer);
279 global_set_key("M->", cmd_end_of_buffer);
281 global_set_key("C-x C-c", cmd_kill_telescope);
283 global_set_key("C-g", cmd_clear_minibuf);
285 global_set_key("M-x", cmd_execute_extended_command);
286 global_set_key("C-x C-f", cmd_load_url);
287 global_set_key("C-x M-f", cmd_load_current_url);
289 global_set_key("C-x t 0", cmd_tab_close);
290 global_set_key("C-x t 1", cmd_tab_close_other);
291 global_set_key("C-x t 2", cmd_tab_new);
292 global_set_key("C-x t o", cmd_tab_next);
293 global_set_key("C-x t O", cmd_tab_previous);
294 global_set_key("C-x t m", cmd_tab_move);
295 global_set_key("C-x t M", cmd_tab_move_to);
297 global_set_key("C-M-b", cmd_previous_page);
298 global_set_key("C-M-f", cmd_next_page);
300 global_set_key("<f7> a", cmd_bookmark_page);
301 global_set_key("<f7> <f7>", cmd_list_bookmarks);
303 /* vi/vi-like */
304 global_set_key("k", cmd_previous_line);
305 global_set_key("j", cmd_next_line);
306 global_set_key("l", cmd_forward_char);
307 global_set_key("h", cmd_backward_char);
308 global_set_key("{", cmd_backward_paragraph);
309 global_set_key("}", cmd_forward_paragraph);
310 global_set_key("^", cmd_move_beginning_of_line);
311 global_set_key("$", cmd_move_end_of_line);
313 global_set_key("K", cmd_scroll_line_up);
314 global_set_key("J", cmd_scroll_line_down);
316 global_set_key("g g", cmd_beginning_of_buffer);
317 global_set_key("G", cmd_end_of_buffer);
319 global_set_key("g D", cmd_tab_close);
320 global_set_key("g N", cmd_tab_new);
321 global_set_key("g t", cmd_tab_next);
322 global_set_key("g T", cmd_tab_previous);
323 global_set_key("g M-t", cmd_tab_move);
324 global_set_key("g M-T", cmd_tab_move_to);
326 global_set_key("H", cmd_previous_page);
327 global_set_key("L", cmd_next_page);
329 /* tmp */
330 global_set_key("q", cmd_kill_telescope);
332 global_set_key("esc", cmd_clear_minibuf);
334 global_set_key(":", cmd_execute_extended_command);
336 /* cua */
337 global_set_key("<up>", cmd_previous_line);
338 global_set_key("<down>", cmd_next_line);
339 global_set_key("<right>", cmd_forward_char);
340 global_set_key("<left>", cmd_backward_char);
341 global_set_key("<prior>", cmd_scroll_up);
342 global_set_key("<next>", cmd_scroll_down);
344 global_set_key("M-<left>", cmd_previous_page);
345 global_set_key("M-<right>", cmd_next_page);
347 /* "ncurses standard" */
348 global_set_key("C-l", cmd_redraw);
350 /* global */
351 global_set_key("<f1>", cmd_toggle_help);
352 global_set_key("C-m", cmd_push_button);
353 global_set_key("M-enter", cmd_push_button_new_tab);
354 global_set_key("M-tab", cmd_previous_button);
355 global_set_key("tab", cmd_next_button);
357 /* === minibuffer map === */
358 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
359 minibuffer_set_key("C-g", cmd_mini_abort);
360 minibuffer_set_key("esc", cmd_mini_abort);
361 minibuffer_set_key("C-d", cmd_mini_delete_char);
362 minibuffer_set_key("del", cmd_mini_delete_backward_char);
363 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
364 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
366 minibuffer_set_key("C-b", cmd_backward_char);
367 minibuffer_set_key("C-f", cmd_forward_char);
368 minibuffer_set_key("<left>", cmd_backward_char);
369 minibuffer_set_key("<right>", cmd_forward_char);
370 minibuffer_set_key("C-e", cmd_move_end_of_line);
371 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
372 minibuffer_set_key("<end>", cmd_move_end_of_line);
373 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
374 minibuffer_set_key("C-k", cmd_mini_kill_line);
376 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
377 minibuffer_set_key("M-n", cmd_mini_next_history_element);
378 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
379 minibuffer_set_key("<down>", cmd_mini_next_history_element);
382 static void
383 restore_cursor(struct window *window)
385 struct vline *vl;
386 const char *prfx;
388 vl = window->current_line;
389 if (vl == NULL || vl->line == NULL)
390 window->curs_x = window->cpoff = 0;
391 else
392 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
394 if (vl != NULL) {
395 prfx = line_prefixes[vl->parent->type].prfx1;
396 window->curs_x += utf8_swidth(prfx);
400 static void
401 cmd_previous_line(struct window *window)
403 struct vline *vl;
405 if (window->current_line == NULL
406 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
407 return;
409 if (--window->curs_y < 0) {
410 window->curs_y = 0;
411 cmd_scroll_line_up(window);
412 return;
415 window->current_line = vl;
416 restore_cursor(window);
419 static void
420 cmd_next_line(struct window *window)
422 struct vline *vl;
424 if (window->current_line == NULL
425 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
426 return;
428 if (++window->curs_y > body_lines-1) {
429 window->curs_y = body_lines-1;
430 cmd_scroll_line_down(window);
431 return;
434 window->current_line = vl;
435 restore_cursor(window);
438 static void
439 cmd_backward_char(struct window *window)
441 if (window->cpoff != 0)
442 window->cpoff--;
443 restore_cursor(window);
446 static void
447 cmd_forward_char(struct window *window)
449 window->cpoff++;
450 restore_cursor(window);
453 static void
454 cmd_backward_paragraph(struct window *window)
456 do {
457 if (window->current_line == NULL ||
458 window->current_line == TAILQ_FIRST(&window->head)) {
459 message("No previous paragraph");
460 return;
462 cmd_previous_line(window);
463 } while (window->current_line->line != NULL ||
464 window->current_line->parent->type != LINE_TEXT);
467 static void
468 cmd_forward_paragraph(struct window *window)
470 do {
471 if (window->current_line == NULL ||
472 window->current_line == TAILQ_LAST(&window->head, vhead)) {
473 message("No next paragraph");
474 return;
476 cmd_next_line(window);
477 } while (window->current_line->line != NULL ||
478 window->current_line->parent->type != LINE_TEXT);
481 static void
482 cmd_move_beginning_of_line(struct window *window)
484 window->cpoff = 0;
485 restore_cursor(window);
488 static void
489 cmd_move_end_of_line(struct window *window)
491 struct vline *vl;
493 vl = window->current_line;
494 if (vl->line == NULL)
495 return;
496 window->cpoff = utf8_cplen(vl->line);
497 restore_cursor(window);
500 static void
501 cmd_redraw(struct window *window)
503 handle_resize(0, 0, NULL);
506 static void
507 cmd_scroll_line_up(struct window *window)
509 struct vline *vl;
511 if (window->line_off == 0)
512 return;
514 vl = nth_line(window, --window->line_off);
515 wscrl(body, -1);
516 wmove(body, 0, 0);
517 print_vline(body, vl);
519 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
520 restore_cursor(window);
523 static void
524 cmd_scroll_line_down(struct window *window)
526 struct vline *vl;
528 vl = window->current_line;
529 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
530 return;
531 window->current_line = vl;
533 window->line_off++;
534 wscrl(body, 1);
536 if (window->line_max - window->line_off < (size_t)body_lines)
537 return;
539 vl = nth_line(window, window->line_off + body_lines-1);
540 wmove(body, body_lines-1, 0);
541 print_vline(body, vl);
543 restore_cursor(window);
546 static void
547 cmd_scroll_up(struct window *window)
549 size_t off;
551 off = body_lines-1;
553 for (; off > 0; --off)
554 cmd_scroll_line_up(window);
557 static void
558 cmd_scroll_down(struct window *window)
560 size_t off;
562 off = body_lines-1;
564 for (; off > 0; --off)
565 cmd_scroll_line_down(window);
568 static void
569 cmd_beginning_of_buffer(struct window *window)
571 window->current_line = TAILQ_FIRST(&window->head);
572 window->line_off = 0;
573 window->curs_y = 0;
574 window->cpoff = 0;
575 restore_cursor(window);
578 static void
579 cmd_end_of_buffer(struct window *window)
581 ssize_t off;
583 off = window->line_max - body_lines;
584 off = MAX(0, off);
586 window->line_off = off;
587 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
589 window->current_line = TAILQ_LAST(&window->head, vhead);
590 window->cpoff = body_cols;
591 restore_cursor(window);
594 static void
595 cmd_kill_telescope(struct window *window)
597 save_session();
598 event_loopbreak();
601 static void
602 cmd_push_button(struct window *window)
604 struct vline *vl;
605 size_t nth;
607 nth = window->line_off + window->curs_y;
608 if (nth >= window->line_max)
609 return;
610 vl = nth_line(window, nth);
611 if (vl->parent->type != LINE_LINK)
612 return;
614 load_url_in_tab(current_tab(), vl->parent->alt);
617 static void
618 cmd_push_button_new_tab(struct window *window)
620 struct vline *vl;
621 size_t nth;
623 nth = window->line_off + window->curs_y;
624 if (nth > window->line_max)
625 return;
626 vl = nth_line(window, nth);
627 if (vl->parent->type != LINE_LINK)
628 return;
630 new_tab(vl->parent->alt);
633 static void
634 cmd_previous_button(struct window *window)
636 do {
637 if (window->current_line == NULL ||
638 window->current_line == TAILQ_FIRST(&window->head)) {
639 message("No previous link");
640 return;
642 cmd_previous_line(window);
643 } while (window->current_line->parent->type != LINE_LINK);
646 static void
647 cmd_next_button(struct window *window)
649 do {
650 if (window->current_line == NULL ||
651 window->current_line == TAILQ_LAST(&window->head, vhead)) {
652 message("No next link");
653 return;
655 cmd_next_line(window);
656 } while (window->current_line->parent->type != LINE_LINK);
659 static void
660 cmd_previous_page(struct window *window)
662 struct tab *tab = current_tab();
664 if (!load_previous_page(tab))
665 message("No previous page");
666 else
667 start_loading_anim(tab);
670 static void
671 cmd_next_page(struct window *window)
673 struct tab *tab = current_tab();
675 if (!load_next_page(tab))
676 message("No next page");
677 else
678 start_loading_anim(tab);
681 static void
682 cmd_clear_minibuf(struct window *window)
684 handle_clear_minibuf(0, 0, NULL);
687 static void
688 cmd_execute_extended_command(struct window *window)
690 size_t len;
692 if (in_minibuffer) {
693 message("We don't have enable-recursive-minibuffers");
694 return;
697 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
698 &eecmd_history);
700 len = sizeof(ministate.prompt);
701 strlcpy(ministate.prompt, "", len);
703 if (thiskey.meta)
704 strlcat(ministate.prompt, "M-", len);
706 strlcat(ministate.prompt, keyname(thiskey.key), len);
707 strlcat(ministate.prompt, " ", len);
710 static void
711 cmd_tab_close(struct window *window)
713 struct tab *tab, *t;
715 tab = current_tab();
716 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
717 TAILQ_NEXT(tab, tabs) == NULL) {
718 message("Can't close the only tab.");
719 return;
722 if (evtimer_pending(&tab->loadingev, NULL))
723 evtimer_del(&tab->loadingev);
725 stop_tab(tab);
727 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
728 t = TAILQ_NEXT(tab, tabs);
729 TAILQ_REMOVE(&tabshead, tab, tabs);
730 free(tab);
732 switch_to_tab(t);
735 static void
736 cmd_tab_close_other(struct window *window)
738 struct tab *t, *i;
740 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
741 if (t->flags & TAB_CURRENT)
742 continue;
744 stop_tab(t);
745 TAILQ_REMOVE(&tabshead, t, tabs);
746 free(t);
750 static void
751 cmd_tab_new(struct window *window)
753 new_tab(NEW_TAB_URL);
756 static void
757 cmd_tab_next(struct window *window)
759 struct tab *tab, *t;
761 tab = current_tab();
762 tab->flags &= ~TAB_CURRENT;
764 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
765 t = TAILQ_FIRST(&tabshead);
766 t->flags |= TAB_CURRENT;
769 static void
770 cmd_tab_previous(struct window *window)
772 struct tab *tab, *t;
774 tab = current_tab();
775 tab->flags &= ~TAB_CURRENT;
777 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
778 t = TAILQ_LAST(&tabshead, tabshead);
779 t->flags |= TAB_CURRENT;
782 static void
783 cmd_tab_move(struct window *window)
785 struct tab *tab, *t;
787 tab = current_tab();
788 t = TAILQ_NEXT(tab, tabs);
789 TAILQ_REMOVE(&tabshead, tab, tabs);
791 if (t == NULL)
792 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
793 else
794 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
797 static void
798 cmd_tab_move_to(struct window *window)
800 struct tab *tab, *t;
802 tab = current_tab();
803 t = TAILQ_PREV(tab, tabshead, tabs);
804 TAILQ_REMOVE(&tabshead, tab, tabs);
806 if (t == NULL) {
807 if (TAILQ_EMPTY(&tabshead))
808 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
809 else
810 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
811 } else
812 TAILQ_INSERT_BEFORE(t, tab, tabs);
815 static void
816 cmd_load_url(struct window *window)
818 if (in_minibuffer) {
819 message("We don't have enable-recursive-minibuffers");
820 return;
823 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
824 &lu_history);
825 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
828 static void
829 cmd_load_current_url(struct window *window)
831 struct tab *tab = current_tab();
833 if (in_minibuffer) {
834 message("We don't have enable-recursive-minibuffers");
835 return;
838 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
839 &lu_history);
840 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
841 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
842 ministate.window.cpoff = utf8_cplen(ministate.buf);
845 static void
846 cmd_bookmark_page(struct window *window)
848 struct tab *tab = current_tab();
850 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
851 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
852 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
853 ministate.window.cpoff = utf8_cplen(ministate.buf);
856 static void
857 cmd_list_bookmarks(struct window *window)
859 load_url_in_tab(current_tab(), "about:bookmarks");
862 static void
863 cmd_toggle_help(struct window *window)
865 side_window = !side_window;
866 if (side_window)
867 recompute_help();
869 /* ugly hack, but otherwise the window doesn't get updated
870 * until I call handle_resize a second time (i.e. C-l). I
871 * will be happy to know why something like this is needed. */
872 handle_resize(0, 0, NULL);
873 handle_resize(0, 0, NULL);
876 static void
877 cmd_mini_delete_char(struct window *window)
879 char *c, *n;
881 if (!in_minibuffer) {
882 message("text is read-only");
883 return;
886 minibuffer_taint_hist();
888 c = utf8_nth(window->current_line->line, window->cpoff);
889 if (*c == '\0')
890 return;
891 n = utf8_next_cp(c);
893 memmove(c, n, strlen(n)+1);
896 static void
897 cmd_mini_delete_backward_char(struct window *window)
899 char *c, *p, *start;
901 if (!in_minibuffer) {
902 message("text is read-only");
903 return;
906 minibuffer_taint_hist();
908 c = utf8_nth(window->current_line->line, window->cpoff);
909 start = window->current_line->line;
910 if (c == start)
911 return;
912 p = utf8_prev_cp(c-1, start);
914 memmove(p, c, strlen(c)+1);
915 window->cpoff--;
918 static void
919 cmd_mini_kill_line(struct window *window)
921 char *c;
923 if (!in_minibuffer) {
924 message("text is read-only");
925 return;
928 minibuffer_taint_hist();
929 c = utf8_nth(window->current_line->line, window->cpoff);
930 *c = '\0';
933 static void
934 cmd_mini_abort(struct window *window)
936 if (!in_minibuffer)
937 return;
939 ministate.abortfn();
942 static void
943 cmd_mini_complete_and_exit(struct window *window)
945 if (!in_minibuffer)
946 return;
948 minibuffer_taint_hist();
949 ministate.donefn();
952 static void
953 cmd_mini_previous_history_element(struct window *window)
955 if (ministate.history == NULL) {
956 message("No history");
957 return;
960 if (ministate.hist_cur == NULL ||
961 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
962 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
963 ministate.hist_off = ministate.history->len - 1;
964 if (ministate.hist_cur == NULL)
965 message("No prev item");
966 } else {
967 ministate.hist_off--;
970 if (ministate.hist_cur != NULL)
971 window->current_line->line = ministate.hist_cur->h;
974 static void
975 cmd_mini_next_history_element(struct window *window)
977 if (ministate.history == NULL) {
978 message("No history");
979 return;
982 if (ministate.hist_cur == NULL ||
983 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
984 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
985 ministate.hist_off = 0;
986 if (ministate.hist_cur == NULL)
987 message("No next item");
988 } else {
989 ministate.hist_off++;
992 if (ministate.hist_cur != NULL)
993 window->current_line->line = ministate.hist_cur->h;
996 static void
997 global_key_unbound(void)
999 message("%s is undefined", keybuf);
1002 static void
1003 minibuffer_hist_save_entry(void)
1005 struct hist *hist;
1007 if (ministate.history == NULL)
1008 return;
1010 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1011 abort();
1013 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1015 if (TAILQ_EMPTY(&ministate.history->head))
1016 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1017 else
1018 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1019 ministate.history->len++;
1023 * taint the minibuffer cache: if we're currently showing a history
1024 * element, copy that to the current buf and reset the "history
1025 * navigation" thing.
1027 static void
1028 minibuffer_taint_hist(void)
1030 if (ministate.hist_cur == NULL)
1031 return;
1033 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1034 ministate.hist_cur = NULL;
1037 static void
1038 minibuffer_self_insert(void)
1040 char *c, tmp[5] = {0};
1041 size_t len;
1043 minibuffer_taint_hist();
1045 if (thiskey.cp == 0)
1046 return;
1048 len = utf8_encode(thiskey.cp, tmp);
1049 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1050 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1051 return;
1053 memmove(c + len, c, strlen(c)+1);
1054 memcpy(c, tmp, len);
1055 ministate.window.cpoff++;
1058 static void
1059 eecmd_self_insert(void)
1061 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1062 !unicode_isgraph(thiskey.cp)) {
1063 global_key_unbound();
1064 return;
1067 minibuffer_self_insert();
1070 static void
1071 eecmd_select(void)
1073 struct cmds *cmd;
1075 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1076 if (!strcmp(cmd->cmd, ministate.buf)) {
1077 exit_minibuffer();
1078 minibuffer_hist_save_entry();
1079 cmd->fn(current_window());
1080 return;
1084 message("No match");
1087 static void
1088 ir_self_insert(void)
1090 minibuffer_self_insert();
1093 static void
1094 ir_select(void)
1096 char buf[1025] = {0};
1097 struct phos_uri uri;
1098 struct tab *tab;
1100 tab = current_tab();
1102 exit_minibuffer();
1103 minibuffer_hist_save_entry();
1105 /* a bit ugly but... */
1106 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1107 phos_uri_set_query(&uri, ministate.buf);
1108 phos_serialize_uri(&uri, buf, sizeof(buf));
1109 load_url_in_tab(tab, buf);
1112 static void
1113 lu_self_insert(void)
1115 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1116 !unicode_isgraph(thiskey.key)) {
1117 global_key_unbound();
1118 return;
1121 minibuffer_self_insert();
1124 static void
1125 lu_select(void)
1127 exit_minibuffer();
1128 minibuffer_hist_save_entry();
1129 load_url_in_tab(current_tab(), ministate.buf);
1132 static void
1133 bp_select(void)
1135 exit_minibuffer();
1136 if (*ministate.buf != '\0')
1137 add_to_bookmarks(ministate.buf);
1138 else
1139 message("Abort.");
1142 static void
1143 yornp_self_insert(void)
1145 if (thiskey.key != 'y' && thiskey.key != 'n') {
1146 message("Please answer y or n");
1147 return;
1150 exit_minibuffer();
1151 yornp_cb(thiskey.key == 'y', yornp_data);
1154 static void
1155 yornp_abort(void)
1157 exit_minibuffer();
1158 yornp_cb(0, yornp_data);
1161 static struct vline *
1162 nth_line(struct window *window, size_t n)
1164 struct vline *vl;
1165 size_t i;
1167 i = 0;
1168 TAILQ_FOREACH(vl, &window->head, vlines) {
1169 if (i == n)
1170 return vl;
1171 i++;
1174 /* unreachable */
1175 abort();
1178 static struct tab *
1179 current_tab(void)
1181 struct tab *t;
1183 TAILQ_FOREACH(t, &tabshead, tabs) {
1184 if (t->flags & TAB_CURRENT)
1185 return t;
1188 /* unreachable */
1189 abort();
1192 static struct window *
1193 current_window(void)
1195 if (in_minibuffer)
1196 return &ministate.window;
1197 return &current_tab()->window;
1200 static int
1201 readkey(void)
1203 uint32_t state = 0;
1205 if ((thiskey.key = wgetch(body)) == ERR)
1206 return 0;
1208 thiskey.meta = thiskey.key == 27;
1209 if (thiskey.meta) {
1210 thiskey.key = wgetch(body);
1211 if (thiskey.key == ERR || thiskey.key == 27) {
1212 thiskey.meta = 0;
1213 thiskey.key = 27;
1217 thiskey.cp = 0;
1218 if ((unsigned int)thiskey.key < UINT8_MAX) {
1219 while (1) {
1220 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1221 break;
1222 if ((thiskey.key = wgetch(body)) == ERR) {
1223 message("Error decoding user input");
1224 return 0;
1229 return 1;
1232 static void
1233 dispatch_stdio(int fd, short ev, void *d)
1235 struct keymap *k;
1236 const char *keyname;
1237 char tmp[5] = {0};
1239 if (!readkey())
1240 return;
1242 if (keybuf[0] != '\0')
1243 strlcat(keybuf, " ", sizeof(keybuf));
1244 if (thiskey.meta)
1245 strlcat(keybuf, "M-", sizeof(keybuf));
1246 if (thiskey.cp != 0) {
1247 utf8_encode(thiskey.cp, tmp);
1248 strlcat(keybuf, tmp, sizeof(keybuf));
1249 } else {
1250 if ((keyname = unkbd(thiskey.key)) != NULL)
1251 strlcat(keybuf, keyname, sizeof(keybuf));
1252 else {
1253 tmp[0] = thiskey.key;
1254 strlcat(keybuf, tmp, sizeof(keybuf));
1258 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1259 if (k->meta == thiskey.meta &&
1260 k->key == thiskey.key) {
1261 if (k->fn == NULL)
1262 current_map = &k->map;
1263 else {
1264 current_map = base_map;
1265 strlcpy(keybuf, "", sizeof(keybuf));
1266 k->fn(current_window());
1268 goto done;
1272 if (current_map->unhandled_input != NULL)
1273 current_map->unhandled_input();
1274 else {
1275 global_key_unbound();
1278 strlcpy(keybuf, "", sizeof(keybuf));
1279 current_map = base_map;
1281 done:
1282 if (side_window)
1283 recompute_help();
1285 redraw_tab(current_tab());
1288 static void
1289 handle_clear_minibuf(int fd, short ev, void *d)
1291 free(ministate.curmesg);
1292 ministate.curmesg = NULL;
1294 redraw_minibuffer();
1295 if (in_minibuffer) {
1296 wrefresh(body);
1297 wrefresh(minibuf);
1298 } else {
1299 wrefresh(minibuf);
1300 wrefresh(body);
1304 static void
1305 handle_resize(int sig, short ev, void *d)
1307 struct tab *tab;
1309 endwin();
1310 refresh();
1311 clear();
1313 /* move and resize the windows, in reverse order! */
1315 mvwin(minibuf, LINES-1, 0);
1316 wresize(minibuf, 1, COLS);
1318 mvwin(modeline, LINES-2, 0);
1319 wresize(modeline, 1, COLS);
1321 body_lines = LINES-3;
1322 body_cols = COLS;
1324 if (side_window) {
1325 help_cols = 0.3 * COLS;
1326 help_lines = LINES-3;
1327 mvwin(help, 1, 0);
1328 wresize(help, help_lines, help_cols);
1330 wrap_page(&helpwin, help_cols);
1332 body_cols = COLS - help_cols - 1;
1333 mvwin(body, 1, help_cols);
1334 } else
1335 mvwin(body, 1, 0);
1337 wresize(body, body_lines, body_cols);
1339 wresize(tabline, 1, COLS);
1341 tab = current_tab();
1343 wrap_page(&tab->window, body_cols);
1344 redraw_tab(tab);
1347 static int
1348 wrap_page(struct window *window, int width)
1350 struct line *l;
1351 const struct line *orig;
1352 struct vline *vl;
1353 const char *prfx;
1355 orig = window->current_line == NULL
1356 ? NULL
1357 : window->current_line->parent;
1358 window->current_line = NULL;
1360 window->curs_y = 0;
1361 window->line_off = 0;
1363 empty_vlist(window);
1365 TAILQ_FOREACH(l, &window->page.head, lines) {
1366 prfx = line_prefixes[l->type].prfx1;
1367 switch (l->type) {
1368 case LINE_TEXT:
1369 case LINE_LINK:
1370 case LINE_TITLE_1:
1371 case LINE_TITLE_2:
1372 case LINE_TITLE_3:
1373 case LINE_ITEM:
1374 case LINE_QUOTE:
1375 case LINE_PRE_START:
1376 case LINE_PRE_END:
1377 wrap_text(window, prfx, l, width);
1378 break;
1379 case LINE_PRE_CONTENT:
1380 hardwrap_text(window, l, width);
1381 break;
1384 if (orig == l && window->current_line == NULL) {
1385 window->line_off = window->line_max-1;
1386 window->current_line = TAILQ_LAST(&window->head, vhead);
1388 while (1) {
1389 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1390 if (vl == NULL || vl->parent != orig)
1391 break;
1392 window->current_line = vl;
1393 window->line_off--;
1398 if (window->current_line == NULL)
1399 window->current_line = TAILQ_FIRST(&window->head);
1401 return 1;
1404 static void
1405 print_vline(WINDOW *window, struct vline *vl)
1407 const char *text = vl->line;
1408 const char *prfx;
1409 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1410 int text_face = line_faces[vl->parent->type].text_prop;
1412 if (!vl->flags)
1413 prfx = line_prefixes[vl->parent->type].prfx1;
1414 else
1415 prfx = line_prefixes[vl->parent->type].prfx2;
1417 if (text == NULL)
1418 text = "";
1420 wattron(window, prefix_face);
1421 wprintw(window, "%s", prfx);
1422 wattroff(window, prefix_face);
1424 wattron(window, text_face);
1425 wprintw(window, "%s", text);
1426 wattroff(window, text_face);
1429 static void
1430 redraw_tabline(void)
1432 struct tab *tab;
1433 size_t toskip, ots, tabwidth, space, x;
1434 int current, y, truncated;
1435 const char *title;
1436 char buf[25];
1438 tabwidth = sizeof(buf)+1;
1439 space = COLS-2;
1441 toskip = 0;
1442 TAILQ_FOREACH(tab, &tabshead, tabs) {
1443 toskip++;
1444 if (tab->flags & TAB_CURRENT)
1445 break;
1448 if (toskip * tabwidth < space)
1449 toskip = 0;
1450 else {
1451 ots = toskip;
1452 toskip--;
1453 while (toskip != 0 &&
1454 (ots - toskip+1) * tabwidth < space)
1455 toskip--;
1458 werase(tabline);
1459 wattron(tabline, tab_face.background);
1460 wprintw(tabline, toskip == 0 ? " " : "<");
1461 wattroff(tabline, tab_face.background);
1463 truncated = 0;
1464 TAILQ_FOREACH(tab, &tabshead, tabs) {
1465 if (truncated)
1466 break;
1467 if (toskip != 0) {
1468 toskip--;
1469 continue;
1472 getyx(tabline, y, x);
1473 if (x + sizeof(buf)+2 >= (size_t)COLS)
1474 truncated = 1;
1476 current = tab->flags & TAB_CURRENT;
1478 if (*(title = tab->window.page.title) == '\0')
1479 title = tab->hist_cur->h;
1481 strlcpy(buf, " ", sizeof(buf));
1482 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1483 /* truncation happens */
1484 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1485 } else {
1486 /* pad with spaces */
1487 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1488 /* nop */ ;
1491 if (current)
1492 wattron(tabline, tab_face.current_tab);
1493 else
1494 wattron(tabline, tab_face.tab);
1496 wprintw(tabline, "%s", buf);
1497 if (TAILQ_NEXT(tab, tabs) != NULL)
1498 wprintw(tabline, " ");
1500 if (current)
1501 wattroff(tabline, tab_face.current_tab);
1502 else
1503 wattroff(tabline, tab_face.tab);
1506 wattron(tabline, tab_face.background);
1507 for (; x < (size_t)COLS; ++x)
1508 waddch(tabline, ' ');
1509 if (truncated)
1510 mvwprintw(tabline, 0, COLS-1, ">");
1513 static void
1514 redraw_window(WINDOW *win, int height, struct window *window)
1516 struct vline *vl;
1517 int l;
1519 werase(win);
1521 window->line_off = MIN(window->line_max-1, window->line_off);
1522 if (TAILQ_EMPTY(&window->head))
1523 return;
1525 l = 0;
1526 vl = nth_line(window, window->line_off);
1527 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1528 wmove(win, l, 0);
1529 print_vline(win, vl);
1530 l++;
1531 if (l == height)
1532 break;
1535 wmove(win, window->curs_y, window->curs_x);
1538 static void
1539 redraw_help(void)
1541 redraw_window(help, help_lines, &helpwin);
1544 static void
1545 redraw_body(struct tab *tab)
1547 redraw_window(body, body_lines, &tab->window);
1550 static inline char
1551 trust_status_char(enum trust_state ts)
1553 switch (ts) {
1554 case TS_UNKNOWN: return 'u';
1555 case TS_UNTRUSTED: return '!';
1556 case TS_TRUSTED: return 'v';
1557 case TS_VERIFIED: return 'V';
1561 static void
1562 redraw_modeline(struct tab *tab)
1564 double pct;
1565 int x, y, max_x, max_y;
1566 const char *mode = tab->window.page.name;
1567 const char *spin = "-\\|/";
1569 werase(modeline);
1570 wattron(modeline, A_REVERSE);
1571 wmove(modeline, 0, 0);
1573 wprintw(modeline, "-%c%c %s ",
1574 spin[tab->loading_anim_step],
1575 trust_status_char(tab->trust),
1576 mode == NULL ? "(none)" : mode);
1578 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1580 if (tab->window.line_max <= (size_t)body_lines)
1581 wprintw(modeline, "All ");
1582 else if (tab->window.line_off == 0)
1583 wprintw(modeline, "Top ");
1584 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1585 wprintw(modeline, "Bottom ");
1586 else
1587 wprintw(modeline, "%.0f%% ", pct);
1589 wprintw(modeline, "%d/%d %s ",
1590 tab->window.line_off + tab->window.curs_y,
1591 tab->window.line_max,
1592 tab->hist_cur->h);
1594 getyx(modeline, y, x);
1595 getmaxyx(modeline, max_y, max_x);
1597 (void)y;
1598 (void)max_y;
1600 for (; x < max_x; ++x)
1601 waddstr(modeline, "-");
1604 static void
1605 redraw_minibuffer(void)
1607 struct tab *tab;
1608 size_t off_y, off_x = 0;
1609 char *start, *c;
1611 werase(minibuf);
1613 if (in_minibuffer) {
1614 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1615 if (ministate.hist_cur != NULL)
1616 wprintw(minibuf, "(%zu/%zu) ",
1617 ministate.hist_off + 1,
1618 ministate.history->len);
1620 getyx(minibuf, off_y, off_x);
1622 start = ministate.hist_cur != NULL
1623 ? ministate.hist_cur->h
1624 : ministate.buf;
1625 c = utf8_nth(ministate.window.current_line->line,
1626 ministate.window.cpoff);
1627 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1628 start = utf8_next_cp(start);
1631 waddstr(minibuf, start);
1634 if (ministate.curmesg != NULL)
1635 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1636 ministate.curmesg);
1638 if (!in_minibuffer && ministate.curmesg == NULL)
1639 waddstr(minibuf, keybuf);
1641 /* If nothing else, show the URL at point */
1642 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1643 tab = current_tab();
1644 if (tab->window.current_line != NULL &&
1645 tab->window.current_line->parent->type == LINE_LINK)
1646 waddstr(minibuf, tab->window.current_line->parent->alt);
1649 if (in_minibuffer)
1650 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1653 static void
1654 redraw_tab(struct tab *tab)
1656 if (side_window) {
1657 redraw_help();
1658 wnoutrefresh(help);
1661 redraw_tabline();
1662 redraw_body(tab);
1663 redraw_modeline(tab);
1664 redraw_minibuffer();
1666 wnoutrefresh(tabline);
1667 wnoutrefresh(modeline);
1669 if (in_minibuffer) {
1670 wnoutrefresh(body);
1671 wnoutrefresh(minibuf);
1672 } else {
1673 wnoutrefresh(minibuf);
1674 wnoutrefresh(body);
1677 doupdate();
1680 static void
1681 emit_help_item(char *prfx, void *fn)
1683 struct line *l;
1684 struct cmds *cmd;
1686 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1687 if (fn == cmd->fn)
1688 break;
1690 assert(cmd != NULL);
1692 if ((l = calloc(1, sizeof(*l))) == NULL)
1693 abort();
1695 l->type = LINE_TEXT;
1696 l->alt = NULL;
1698 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1700 if (TAILQ_EMPTY(&helpwin.page.head))
1701 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1702 else
1703 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1706 static void
1707 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1709 struct keymap *k;
1710 char p[32];
1711 const char *kn;
1713 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1714 strlcpy(p, prfx, sizeof(p));
1715 if (*p != '\0')
1716 strlcat(p, " ", sizeof(p));
1717 if (k->meta)
1718 strlcat(p, "M-", sizeof(p));
1719 if ((kn = unkbd(k->key)) != NULL)
1720 strlcat(p, kn, sizeof(p));
1721 else
1722 strlcat(p, keyname(k->key), sizeof(p));
1724 if (k->fn == NULL)
1725 rec_compute_help(&k->map, p, sizeof(p));
1726 else
1727 emit_help_item(p, k->fn);
1731 static void
1732 recompute_help(void)
1734 char p[32] = { 0 };
1736 empty_vlist(&helpwin);
1737 empty_linelist(&helpwin);
1738 rec_compute_help(current_map, p, sizeof(p));
1739 wrap_page(&helpwin, help_cols);
1742 static void
1743 vmessage(const char *fmt, va_list ap)
1745 if (evtimer_pending(&clminibufev, NULL))
1746 evtimer_del(&clminibufev);
1747 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1748 evtimer_add(&clminibufev, &clminibufev_timer);
1750 free(ministate.curmesg);
1752 /* TODO: what to do if the allocation fails here? */
1753 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1754 ministate.curmesg = NULL;
1756 redraw_minibuffer();
1757 if (in_minibuffer) {
1758 wrefresh(body);
1759 wrefresh(minibuf);
1760 } else {
1761 wrefresh(minibuf);
1762 wrefresh(body);
1766 static void
1767 message(const char *fmt, ...)
1769 va_list ap;
1771 va_start(ap, fmt);
1772 vmessage(fmt, ap);
1773 va_end(ap);
1776 static void
1777 start_loading_anim(struct tab *tab)
1779 if (tab->loading_anim)
1780 return;
1781 tab->loading_anim = 1;
1782 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1783 evtimer_add(&tab->loadingev, &loadingev_timer);
1786 static void
1787 update_loading_anim(int fd, short ev, void *d)
1789 struct tab *tab = d;
1791 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1793 if (tab->flags & TAB_CURRENT) {
1794 redraw_modeline(tab);
1795 wrefresh(modeline);
1796 wrefresh(body);
1797 if (in_minibuffer)
1798 wrefresh(minibuf);
1801 evtimer_add(&tab->loadingev, &loadingev_timer);
1804 static void
1805 stop_loading_anim(struct tab *tab)
1807 if (!tab->loading_anim)
1808 return;
1809 evtimer_del(&tab->loadingev);
1810 tab->loading_anim = 0;
1811 tab->loading_anim_step = 0;
1813 if (!(tab->flags & TAB_CURRENT))
1814 return;
1816 redraw_modeline(tab);
1818 wrefresh(modeline);
1819 wrefresh(body);
1820 if (in_minibuffer)
1821 wrefresh(minibuf);
1824 static void
1825 load_url_in_tab(struct tab *tab, const char *url)
1827 message("Loading %s...", url);
1828 start_loading_anim(tab);
1829 load_url(tab, url);
1831 tab->window.curs_x = 0;
1832 tab->window.curs_y = 0;
1833 redraw_tab(tab);
1836 static void
1837 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1838 void (*abortfn)(void), struct histhead *hist)
1840 in_minibuffer = 1;
1841 base_map = &minibuffer_map;
1842 current_map = &minibuffer_map;
1844 base_map->unhandled_input = self_insert_fn;
1846 ministate.donefn = donefn;
1847 ministate.abortfn = abortfn;
1848 memset(ministate.buf, 0, sizeof(ministate.buf));
1849 ministate.window.current_line = &ministate.vline;
1850 ministate.window.current_line->line = ministate.buf;
1851 ministate.window.cpoff = 0;
1852 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1854 ministate.history = hist;
1855 ministate.hist_cur = NULL;
1856 ministate.hist_off = 0;
1859 static void
1860 exit_minibuffer(void)
1862 werase(minibuf);
1864 in_minibuffer = 0;
1865 base_map = &global_map;
1866 current_map = &global_map;
1869 static void
1870 switch_to_tab(struct tab *tab)
1872 struct tab *t;
1874 TAILQ_FOREACH(t, &tabshead, tabs) {
1875 t->flags &= ~TAB_CURRENT;
1878 tab->flags |= TAB_CURRENT;
1881 unsigned int
1882 tab_new_id(void)
1884 return tab_counter++;
1887 static struct tab *
1888 new_tab(const char *url)
1890 struct tab *tab;
1892 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1893 event_loopbreak();
1894 return NULL;
1897 TAILQ_INIT(&tab->hist.head);
1899 TAILQ_INIT(&tab->window.head);
1901 tab->id = tab_new_id();
1902 switch_to_tab(tab);
1904 if (TAILQ_EMPTY(&tabshead))
1905 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1906 else
1907 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1909 load_url_in_tab(tab, url);
1910 return tab;
1913 static void
1914 session_new_tab_cb(const char *url)
1916 new_tab(url);
1919 static void
1920 usage(void)
1922 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1925 int
1926 ui_init(int argc, char * const *argv)
1928 const char *url = NEW_TAB_URL;
1929 int ch;
1931 while ((ch = getopt(argc, argv, "")) != -1) {
1932 switch (ch) {
1933 default:
1934 usage();
1935 return 0;
1938 argc -= optind;
1939 argv += optind;
1941 if (argc != 0)
1942 url = argv[0];
1944 setlocale(LC_ALL, "");
1946 TAILQ_INIT(&global_map.m);
1947 global_map.unhandled_input = global_key_unbound;
1949 TAILQ_INIT(&minibuffer_map.m);
1951 TAILQ_INIT(&eecmd_history.head);
1952 TAILQ_INIT(&ir_history.head);
1953 TAILQ_INIT(&lu_history.head);
1955 ministate.line.type = LINE_TEXT;
1956 ministate.vline.parent = &ministate.line;
1957 ministate.window.current_line = &ministate.vline;
1959 /* initialize help window */
1960 TAILQ_INIT(&helpwin.head);
1962 base_map = &global_map;
1963 current_map = &global_map;
1964 load_default_keys();
1966 initscr();
1967 raw();
1968 noecho();
1970 nonl();
1971 intrflush(stdscr, FALSE);
1973 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1974 return 0;
1975 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1976 return 0;
1977 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1978 return 0;
1979 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1980 return 0;
1981 if ((help = newwin(1, 1, 1, 0)) == NULL)
1982 return 0;
1984 body_lines = LINES-3;
1985 body_cols = COLS;
1987 keypad(body, TRUE);
1988 scrollok(body, TRUE);
1990 /* non-blocking input */
1991 wtimeout(body, 0);
1993 mvwprintw(body, 0, 0, "");
1995 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1996 event_add(&stdioev, NULL);
1998 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1999 signal_add(&winchev, NULL);
2001 load_last_session(session_new_tab_cb);
2002 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2003 new_tab(url);
2005 return 1;
2008 void
2009 ui_on_tab_loaded(struct tab *tab)
2011 stop_loading_anim(tab);
2012 message("Loaded %s", tab->hist_cur->h);
2014 redraw_tabline();
2015 wrefresh(tabline);
2016 if (in_minibuffer)
2017 wrefresh(minibuf);
2018 else
2019 wrefresh(body);
2022 void
2023 ui_on_tab_refresh(struct tab *tab)
2025 wrap_page(&tab->window, body_cols);
2026 if (tab->flags & TAB_CURRENT) {
2027 restore_cursor(&tab->window);
2028 redraw_tab(tab);
2032 void
2033 ui_require_input(struct tab *tab, int hide)
2035 /* TODO: hard-switching to another tab is ugly */
2036 switch_to_tab(tab);
2038 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2039 &ir_history);
2040 strlcpy(ministate.prompt, "Input required: ",
2041 sizeof(ministate.prompt));
2042 redraw_tab(tab);
2045 void
2046 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2047 unsigned int data)
2049 size_t len;
2051 if (in_minibuffer) {
2052 fn(0, data);
2053 return;
2056 yornp_cb = fn;
2057 yornp_data = data;
2058 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2059 yornp_abort, NULL);
2061 len = sizeof(ministate.prompt);
2062 strlcpy(ministate.prompt, prompt, len);
2063 strlcat(ministate.prompt, " (y or n) ", len);
2064 redraw_tab(current_tab());
2067 void
2068 ui_notify(const char *fmt, ...)
2070 va_list ap;
2072 va_start(ap, fmt);
2073 vmessage(fmt, ap);
2074 va_end(ap);
2077 void
2078 ui_end(void)
2080 endwin();