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"
36 #include "cmd.gen.h"
38 #include <assert.h>
39 #include <curses.h>
40 #include <event.h>
41 #include <locale.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #define TAB_CURRENT 0x1
49 #define TAB_URGENT 0x2
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void restore_cursor(struct window*);
58 static void global_key_unbound(void);
59 static void minibuffer_hist_save_entry(void);
60 static void minibuffer_taint_hist(void);
61 static void minibuffer_self_insert(void);
62 static void eecmd_self_insert(void);
63 static void eecmd_select(void);
64 static void ir_self_insert(void);
65 static void ir_select(void);
66 static void lu_self_insert(void);
67 static void lu_select(void);
68 static void bp_select(void);
69 static void yornp_self_insert(void);
70 static void yornp_abort(void);
72 static struct vline *nth_line(struct window*, size_t);
73 static struct tab *current_tab(void);
74 static struct window *current_window(void);
75 static int readkey(void);
76 static void dispatch_stdio(int, short, void*);
77 static void handle_clear_minibuf(int, short, void*);
78 static void handle_resize(int, short, void*);
79 static void handle_resize_timeout(int, short, void*);
80 static int wrap_page(struct window*, int);
81 static void print_vline(WINDOW*, struct vline*);
82 static void redraw_tabline(void);
83 static void redraw_window(WINDOW*, int, struct window*);
84 static void redraw_help(void);
85 static void redraw_body(struct tab*);
86 static void redraw_modeline(struct tab*);
87 static void redraw_minibuffer(void);
88 static void redraw_tab(struct tab*);
89 static void emit_help_item(char*, void*);
90 static void rec_compute_help(struct kmap*, char*, size_t);
91 static void recompute_help(void);
92 static void vmessage(const char*, va_list);
93 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
94 static void start_loading_anim(struct tab*);
95 static void update_loading_anim(int, short, void*);
96 static void stop_loading_anim(struct tab*);
97 static void load_url_in_tab(struct tab*, const char*);
98 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
99 static void exit_minibuffer(void);
100 static void switch_to_tab(struct tab*);
101 static struct tab *new_tab(const char*);
102 static void session_new_tab_cb(const char*);
103 static void usage(void);
105 static struct { short meta; int key; uint32_t cp; } thiskey;
107 static struct event resizeev;
108 static struct timeval resize_timer = { 0, 250000 };
110 static WINDOW *tabline, *body, *modeline, *minibuf;
111 static int body_lines, body_cols;
113 static WINDOW *help;
114 static struct window helpwin;
115 static int help_lines, help_cols;
117 static int side_window;
119 static struct event clminibufev;
120 static struct timeval clminibufev_timer = { 5, 0 };
121 static struct timeval loadingev_timer = { 0, 250000 };
123 static uint32_t tab_counter;
125 static char keybuf[64];
127 static void (*yornp_cb)(int, unsigned int);
128 static unsigned int yornp_data;
130 struct kmap global_map,
131 minibuffer_map,
132 *current_map,
133 *base_map;
135 static struct histhead eecmd_history,
136 ir_history,
137 lu_history;
139 static int in_minibuffer;
141 static struct {
142 char *curmesg;
144 char prompt[64];
145 void (*donefn)(void);
146 void (*abortfn)(void);
148 char buf[1025];
149 struct line line;
150 struct vline vline;
151 struct window window;
153 struct histhead *history;
154 struct hist *hist_cur;
155 size_t hist_off;
156 } ministate;
158 struct lineprefix {
159 const char *prfx1;
160 const char *prfx2;
161 } line_prefixes[] = {
162 [LINE_TEXT] = { "", "" },
163 [LINE_LINK] = { "=> ", " " },
164 [LINE_TITLE_1] = { "# ", " " },
165 [LINE_TITLE_2] = { "## ", " " },
166 [LINE_TITLE_3] = { "### ", " " },
167 [LINE_ITEM] = { "* ", " " },
168 [LINE_QUOTE] = { "> ", " " },
169 [LINE_PRE_START] = { "```", " " },
170 [LINE_PRE_CONTENT] = { "", "" },
171 [LINE_PRE_END] = { "```", "```" },
172 };
174 static struct line_face {
175 int prefix_prop;
176 int text_prop;
177 } line_faces[] = {
178 [LINE_TEXT] = { 0, 0 },
179 [LINE_LINK] = { 0, A_UNDERLINE },
180 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
181 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
182 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
183 [LINE_ITEM] = { 0, 0 },
184 [LINE_QUOTE] = { 0, A_DIM },
185 [LINE_PRE_START] = { 0, 0 },
186 [LINE_PRE_CONTENT] = { 0, 0 },
187 [LINE_PRE_END] = { 0, 0 },
188 };
190 static struct tab_face {
191 int background, tab, current_tab;
192 } tab_face = {
193 A_REVERSE, A_REVERSE, A_NORMAL
194 };
196 static inline void
197 global_set_key(const char *key, void (*fn)(struct window*))
199 if (!kmap_define_key(&global_map, key, fn))
200 _exit(1);
203 static inline void
204 minibuffer_set_key(const char *key, void (*fn)(struct window*))
206 if (!kmap_define_key(&minibuffer_map, key, fn))
207 _exit(1);
210 static void
211 load_default_keys(void)
213 /* === global map === */
215 /* emacs */
216 global_set_key("C-p", cmd_previous_line);
217 global_set_key("C-n", cmd_next_line);
218 global_set_key("C-f", cmd_forward_char);
219 global_set_key("C-b", cmd_backward_char);
220 global_set_key("M-{", cmd_backward_paragraph);
221 global_set_key("M-}", cmd_forward_paragraph);
222 global_set_key("C-a", cmd_move_beginning_of_line);
223 global_set_key("C-e", cmd_move_end_of_line);
225 global_set_key("M-v", cmd_scroll_up);
226 global_set_key("C-v", cmd_scroll_down);
227 global_set_key("M-space", cmd_scroll_up);
228 global_set_key("space", cmd_scroll_down);
230 global_set_key("M-<", cmd_beginning_of_buffer);
231 global_set_key("M->", cmd_end_of_buffer);
233 global_set_key("C-x C-c", cmd_kill_telescope);
235 global_set_key("C-g", cmd_clear_minibuf);
237 global_set_key("M-x", cmd_execute_extended_command);
238 global_set_key("C-x C-f", cmd_load_url);
239 global_set_key("C-x M-f", cmd_load_current_url);
241 global_set_key("C-x t 0", cmd_tab_close);
242 global_set_key("C-x t 1", cmd_tab_close_other);
243 global_set_key("C-x t 2", cmd_tab_new);
244 global_set_key("C-x t o", cmd_tab_next);
245 global_set_key("C-x t O", cmd_tab_previous);
246 global_set_key("C-x t m", cmd_tab_move);
247 global_set_key("C-x t M", cmd_tab_move_to);
249 global_set_key("C-M-b", cmd_previous_page);
250 global_set_key("C-M-f", cmd_next_page);
252 global_set_key("<f7> a", cmd_bookmark_page);
253 global_set_key("<f7> <f7>", cmd_list_bookmarks);
255 /* vi/vi-like */
256 global_set_key("k", cmd_previous_line);
257 global_set_key("j", cmd_next_line);
258 global_set_key("l", cmd_forward_char);
259 global_set_key("h", cmd_backward_char);
260 global_set_key("{", cmd_backward_paragraph);
261 global_set_key("}", cmd_forward_paragraph);
262 global_set_key("^", cmd_move_beginning_of_line);
263 global_set_key("$", cmd_move_end_of_line);
265 global_set_key("K", cmd_scroll_line_up);
266 global_set_key("J", cmd_scroll_line_down);
268 global_set_key("g g", cmd_beginning_of_buffer);
269 global_set_key("G", cmd_end_of_buffer);
271 global_set_key("g D", cmd_tab_close);
272 global_set_key("g N", cmd_tab_new);
273 global_set_key("g t", cmd_tab_next);
274 global_set_key("g T", cmd_tab_previous);
275 global_set_key("g M-t", cmd_tab_move);
276 global_set_key("g M-T", cmd_tab_move_to);
278 global_set_key("H", cmd_previous_page);
279 global_set_key("L", cmd_next_page);
281 /* tmp */
282 global_set_key("q", cmd_kill_telescope);
284 global_set_key("esc", cmd_clear_minibuf);
286 global_set_key(":", cmd_execute_extended_command);
288 /* cua */
289 global_set_key("<up>", cmd_previous_line);
290 global_set_key("<down>", cmd_next_line);
291 global_set_key("<right>", cmd_forward_char);
292 global_set_key("<left>", cmd_backward_char);
293 global_set_key("<prior>", cmd_scroll_up);
294 global_set_key("<next>", cmd_scroll_down);
296 global_set_key("M-<left>", cmd_previous_page);
297 global_set_key("M-<right>", cmd_next_page);
299 /* "ncurses standard" */
300 global_set_key("C-l", cmd_redraw);
302 /* global */
303 global_set_key("<f1>", cmd_toggle_help);
304 global_set_key("C-m", cmd_push_button);
305 global_set_key("M-enter", cmd_push_button_new_tab);
306 global_set_key("M-tab", cmd_previous_button);
307 global_set_key("tab", cmd_next_button);
309 /* === minibuffer map === */
310 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
311 minibuffer_set_key("C-g", cmd_mini_abort);
312 minibuffer_set_key("esc", cmd_mini_abort);
313 minibuffer_set_key("C-d", cmd_mini_delete_char);
314 minibuffer_set_key("del", cmd_mini_delete_backward_char);
315 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
316 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
318 minibuffer_set_key("C-b", cmd_backward_char);
319 minibuffer_set_key("C-f", cmd_forward_char);
320 minibuffer_set_key("<left>", cmd_backward_char);
321 minibuffer_set_key("<right>", cmd_forward_char);
322 minibuffer_set_key("C-e", cmd_move_end_of_line);
323 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
324 minibuffer_set_key("<end>", cmd_move_end_of_line);
325 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
326 minibuffer_set_key("C-k", cmd_mini_kill_line);
328 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
329 minibuffer_set_key("M-n", cmd_mini_next_history_element);
330 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
331 minibuffer_set_key("<down>", cmd_mini_next_history_element);
334 static void
335 restore_cursor(struct window *window)
337 struct vline *vl;
338 const char *prfx;
340 vl = window->current_line;
341 if (vl == NULL || vl->line == NULL)
342 window->curs_x = window->cpoff = 0;
343 else
344 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
346 if (vl != NULL) {
347 prfx = line_prefixes[vl->parent->type].prfx1;
348 window->curs_x += utf8_swidth(prfx);
352 void
353 cmd_previous_line(struct window *window)
355 struct vline *vl;
357 if (window->current_line == NULL
358 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
359 return;
361 if (--window->curs_y < 0) {
362 window->curs_y = 0;
363 cmd_scroll_line_up(window);
364 return;
367 window->current_line = vl;
368 restore_cursor(window);
371 void
372 cmd_next_line(struct window *window)
374 struct vline *vl;
376 if (window->current_line == NULL
377 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
378 return;
380 if (++window->curs_y > body_lines-1) {
381 window->curs_y = body_lines-1;
382 cmd_scroll_line_down(window);
383 return;
386 window->current_line = vl;
387 restore_cursor(window);
390 void
391 cmd_backward_char(struct window *window)
393 if (window->cpoff != 0)
394 window->cpoff--;
395 restore_cursor(window);
398 void
399 cmd_forward_char(struct window *window)
401 size_t len = 0;
403 if (window->current_line->line != NULL)
404 len = utf8_cplen(window->current_line->line);
405 if (++window->cpoff > len)
406 window->cpoff = len;
407 restore_cursor(window);
410 void
411 cmd_backward_paragraph(struct window *window)
413 do {
414 if (window->current_line == NULL ||
415 window->current_line == TAILQ_FIRST(&window->head)) {
416 message("No previous paragraph");
417 return;
419 cmd_previous_line(window);
420 } while (window->current_line->line != NULL ||
421 window->current_line->parent->type != LINE_TEXT);
424 void
425 cmd_forward_paragraph(struct window *window)
427 do {
428 if (window->current_line == NULL ||
429 window->current_line == TAILQ_LAST(&window->head, vhead)) {
430 message("No next paragraph");
431 return;
433 cmd_next_line(window);
434 } while (window->current_line->line != NULL ||
435 window->current_line->parent->type != LINE_TEXT);
438 void
439 cmd_move_beginning_of_line(struct window *window)
441 window->cpoff = 0;
442 restore_cursor(window);
445 void
446 cmd_move_end_of_line(struct window *window)
448 struct vline *vl;
450 vl = window->current_line;
451 if (vl->line == NULL)
452 return;
453 window->cpoff = utf8_cplen(vl->line);
454 restore_cursor(window);
457 void
458 cmd_redraw(struct window *window)
460 handle_resize(0, 0, NULL);
463 void
464 cmd_scroll_line_up(struct window *window)
466 struct vline *vl;
468 if (window->line_off == 0)
469 return;
471 vl = nth_line(window, --window->line_off);
472 wscrl(body, -1);
473 wmove(body, 0, 0);
474 print_vline(body, vl);
476 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
477 restore_cursor(window);
480 void
481 cmd_scroll_line_down(struct window *window)
483 struct vline *vl;
485 vl = window->current_line;
486 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
487 return;
488 window->current_line = vl;
490 window->line_off++;
491 wscrl(body, 1);
493 if (window->line_max - window->line_off < (size_t)body_lines)
494 return;
496 vl = nth_line(window, window->line_off + body_lines-1);
497 wmove(body, body_lines-1, 0);
498 print_vline(body, vl);
500 restore_cursor(window);
503 void
504 cmd_scroll_up(struct window *window)
506 size_t off;
508 off = body_lines-1;
510 for (; off > 0; --off)
511 cmd_scroll_line_up(window);
514 void
515 cmd_scroll_down(struct window *window)
517 size_t off;
519 off = body_lines-1;
521 for (; off > 0; --off)
522 cmd_scroll_line_down(window);
525 void
526 cmd_beginning_of_buffer(struct window *window)
528 window->current_line = TAILQ_FIRST(&window->head);
529 window->line_off = 0;
530 window->curs_y = 0;
531 window->cpoff = 0;
532 restore_cursor(window);
535 void
536 cmd_end_of_buffer(struct window *window)
538 ssize_t off;
540 off = window->line_max - body_lines;
541 off = MAX(0, off);
543 window->line_off = off;
544 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
546 window->current_line = TAILQ_LAST(&window->head, vhead);
547 window->cpoff = body_cols;
548 restore_cursor(window);
551 void
552 cmd_kill_telescope(struct window *window)
554 save_session();
555 event_loopbreak();
558 void
559 cmd_push_button(struct window *window)
561 struct vline *vl;
562 size_t nth;
564 nth = window->line_off + window->curs_y;
565 if (nth >= window->line_max)
566 return;
567 vl = nth_line(window, nth);
568 if (vl->parent->type != LINE_LINK)
569 return;
571 load_url_in_tab(current_tab(), vl->parent->alt);
574 void
575 cmd_push_button_new_tab(struct window *window)
577 struct vline *vl;
578 size_t nth;
580 nth = window->line_off + window->curs_y;
581 if (nth > window->line_max)
582 return;
583 vl = nth_line(window, nth);
584 if (vl->parent->type != LINE_LINK)
585 return;
587 new_tab(vl->parent->alt);
590 void
591 cmd_previous_button(struct window *window)
593 do {
594 if (window->current_line == NULL ||
595 window->current_line == TAILQ_FIRST(&window->head)) {
596 message("No previous link");
597 return;
599 cmd_previous_line(window);
600 } while (window->current_line->parent->type != LINE_LINK);
603 void
604 cmd_next_button(struct window *window)
606 do {
607 if (window->current_line == NULL ||
608 window->current_line == TAILQ_LAST(&window->head, vhead)) {
609 message("No next link");
610 return;
612 cmd_next_line(window);
613 } while (window->current_line->parent->type != LINE_LINK);
616 void
617 cmd_previous_page(struct window *window)
619 struct tab *tab = current_tab();
621 if (!load_previous_page(tab))
622 message("No previous page");
623 else
624 start_loading_anim(tab);
627 void
628 cmd_next_page(struct window *window)
630 struct tab *tab = current_tab();
632 if (!load_next_page(tab))
633 message("No next page");
634 else
635 start_loading_anim(tab);
638 void
639 cmd_clear_minibuf(struct window *window)
641 handle_clear_minibuf(0, 0, NULL);
644 void
645 cmd_execute_extended_command(struct window *window)
647 size_t len;
649 if (in_minibuffer) {
650 message("We don't have enable-recursive-minibuffers");
651 return;
654 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
655 &eecmd_history);
657 len = sizeof(ministate.prompt);
658 strlcpy(ministate.prompt, "", len);
660 if (thiskey.meta)
661 strlcat(ministate.prompt, "M-", len);
663 strlcat(ministate.prompt, keyname(thiskey.key), len);
665 if (thiskey.meta)
666 strlcat(ministate.prompt, " ", len);
669 void
670 cmd_tab_close(struct window *window)
672 struct tab *tab, *t;
674 tab = current_tab();
675 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
676 TAILQ_NEXT(tab, tabs) == NULL) {
677 message("Can't close the only tab.");
678 return;
681 if (evtimer_pending(&tab->loadingev, NULL))
682 evtimer_del(&tab->loadingev);
684 stop_tab(tab);
686 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
687 t = TAILQ_NEXT(tab, tabs);
688 TAILQ_REMOVE(&tabshead, tab, tabs);
689 free(tab);
691 switch_to_tab(t);
694 void
695 cmd_tab_close_other(struct window *window)
697 struct tab *t, *i;
699 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
700 if (t->flags & TAB_CURRENT)
701 continue;
703 stop_tab(t);
704 TAILQ_REMOVE(&tabshead, t, tabs);
705 free(t);
709 void
710 cmd_tab_new(struct window *window)
712 new_tab(NEW_TAB_URL);
715 void
716 cmd_tab_next(struct window *window)
718 struct tab *tab, *t;
720 tab = current_tab();
721 tab->flags &= ~TAB_CURRENT;
723 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
724 t = TAILQ_FIRST(&tabshead);
725 t->flags |= TAB_CURRENT;
726 t->flags &= ~TAB_URGENT;
729 void
730 cmd_tab_previous(struct window *window)
732 struct tab *tab, *t;
734 tab = current_tab();
735 tab->flags &= ~TAB_CURRENT;
737 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
738 t = TAILQ_LAST(&tabshead, tabshead);
739 t->flags |= TAB_CURRENT;
740 t->flags &= ~TAB_URGENT;
743 void
744 cmd_tab_move(struct window *window)
746 struct tab *tab, *t;
748 tab = current_tab();
749 t = TAILQ_NEXT(tab, tabs);
750 TAILQ_REMOVE(&tabshead, tab, tabs);
752 if (t == NULL)
753 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
754 else
755 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
758 void
759 cmd_tab_move_to(struct window *window)
761 struct tab *tab, *t;
763 tab = current_tab();
764 t = TAILQ_PREV(tab, tabshead, tabs);
765 TAILQ_REMOVE(&tabshead, tab, tabs);
767 if (t == NULL) {
768 if (TAILQ_EMPTY(&tabshead))
769 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
770 else
771 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
772 } else
773 TAILQ_INSERT_BEFORE(t, tab, tabs);
776 void
777 cmd_load_url(struct window *window)
779 if (in_minibuffer) {
780 message("We don't have enable-recursive-minibuffers");
781 return;
784 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
785 &lu_history);
786 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
787 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
788 cmd_move_end_of_line(&ministate.window);
791 void
792 cmd_load_current_url(struct window *window)
794 struct tab *tab = current_tab();
796 if (in_minibuffer) {
797 message("We don't have enable-recursive-minibuffers");
798 return;
801 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
802 &lu_history);
803 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
804 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
805 ministate.window.cpoff = utf8_cplen(ministate.buf);
808 void
809 cmd_bookmark_page(struct window *window)
811 struct tab *tab = current_tab();
813 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
814 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
815 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
816 ministate.window.cpoff = utf8_cplen(ministate.buf);
819 void
820 cmd_list_bookmarks(struct window *window)
822 load_url_in_tab(current_tab(), "about:bookmarks");
825 void
826 cmd_toggle_help(struct window *window)
828 side_window = !side_window;
829 if (side_window)
830 recompute_help();
832 /*
833 * ugly hack, but otherwise the window doesn't get updated
834 * until I call handle_resize a second time (i.e. C-l). I
835 * will be happy to know why something like this is needed.
836 */
837 handle_resize(0, 0, NULL);
838 handle_resize(0, 0, NULL);
841 void
842 cmd_mini_delete_char(struct window *window)
844 char *c, *n;
846 if (!in_minibuffer) {
847 message("text is read-only");
848 return;
851 minibuffer_taint_hist();
853 c = utf8_nth(window->current_line->line, window->cpoff);
854 if (*c == '\0')
855 return;
856 n = utf8_next_cp(c);
858 memmove(c, n, strlen(n)+1);
861 void
862 cmd_mini_delete_backward_char(struct window *window)
864 char *c, *p, *start;
866 if (!in_minibuffer) {
867 message("text is read-only");
868 return;
871 minibuffer_taint_hist();
873 c = utf8_nth(window->current_line->line, window->cpoff);
874 start = window->current_line->line;
875 if (c == start)
876 return;
877 p = utf8_prev_cp(c-1, start);
879 memmove(p, c, strlen(c)+1);
880 window->cpoff--;
883 void
884 cmd_mini_kill_line(struct window *window)
886 char *c;
888 if (!in_minibuffer) {
889 message("text is read-only");
890 return;
893 minibuffer_taint_hist();
894 c = utf8_nth(window->current_line->line, window->cpoff);
895 *c = '\0';
898 void
899 cmd_mini_abort(struct window *window)
901 if (!in_minibuffer)
902 return;
904 ministate.abortfn();
907 void
908 cmd_mini_complete_and_exit(struct window *window)
910 if (!in_minibuffer)
911 return;
913 minibuffer_taint_hist();
914 ministate.donefn();
917 void
918 cmd_mini_previous_history_element(struct window *window)
920 if (ministate.history == NULL) {
921 message("No history");
922 return;
925 if (ministate.hist_cur == NULL ||
926 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
927 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
928 ministate.hist_off = ministate.history->len - 1;
929 if (ministate.hist_cur == NULL)
930 message("No prev item");
931 } else {
932 ministate.hist_off--;
935 if (ministate.hist_cur != NULL)
936 window->current_line->line = ministate.hist_cur->h;
939 void
940 cmd_mini_next_history_element(struct window *window)
942 if (ministate.history == NULL) {
943 message("No history");
944 return;
947 if (ministate.hist_cur == NULL ||
948 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
949 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
950 ministate.hist_off = 0;
951 if (ministate.hist_cur == NULL)
952 message("No next item");
953 } else {
954 ministate.hist_off++;
957 if (ministate.hist_cur != NULL)
958 window->current_line->line = ministate.hist_cur->h;
961 static void
962 global_key_unbound(void)
964 message("%s is undefined", keybuf);
967 static void
968 minibuffer_hist_save_entry(void)
970 struct hist *hist;
972 if (ministate.history == NULL)
973 return;
975 if ((hist = calloc(1, sizeof(*hist))) == NULL)
976 abort();
978 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
980 if (TAILQ_EMPTY(&ministate.history->head))
981 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
982 else
983 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
984 ministate.history->len++;
987 /*
988 * taint the minibuffer cache: if we're currently showing a history
989 * element, copy that to the current buf and reset the "history
990 * navigation" thing.
991 */
992 static void
993 minibuffer_taint_hist(void)
995 if (ministate.hist_cur == NULL)
996 return;
998 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
999 ministate.hist_cur = NULL;
1002 static void
1003 minibuffer_self_insert(void)
1005 char *c, tmp[5] = {0};
1006 size_t len;
1008 minibuffer_taint_hist();
1010 if (thiskey.cp == 0)
1011 return;
1013 len = utf8_encode(thiskey.cp, tmp);
1014 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1015 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1016 return;
1018 memmove(c + len, c, strlen(c)+1);
1019 memcpy(c, tmp, len);
1020 ministate.window.cpoff++;
1023 static void
1024 eecmd_self_insert(void)
1026 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1027 !unicode_isgraph(thiskey.cp)) {
1028 global_key_unbound();
1029 return;
1032 minibuffer_self_insert();
1035 static void
1036 eecmd_select(void)
1038 struct cmds *cmd;
1040 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1041 if (!strcmp(cmd->cmd, ministate.buf)) {
1042 exit_minibuffer();
1043 minibuffer_hist_save_entry();
1044 cmd->fn(current_window());
1045 return;
1049 message("No match");
1052 static void
1053 ir_self_insert(void)
1055 minibuffer_self_insert();
1058 static void
1059 ir_select(void)
1061 char buf[1025] = {0};
1062 struct phos_uri uri;
1063 struct tab *tab;
1065 tab = current_tab();
1067 exit_minibuffer();
1068 minibuffer_hist_save_entry();
1070 /* a bit ugly but... */
1071 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1072 phos_uri_set_query(&uri, ministate.buf);
1073 phos_serialize_uri(&uri, buf, sizeof(buf));
1074 load_url_in_tab(tab, buf);
1077 static void
1078 lu_self_insert(void)
1080 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1081 !unicode_isgraph(thiskey.key)) {
1082 global_key_unbound();
1083 return;
1086 minibuffer_self_insert();
1089 static void
1090 lu_select(void)
1092 exit_minibuffer();
1093 minibuffer_hist_save_entry();
1094 load_url_in_tab(current_tab(), ministate.buf);
1097 static void
1098 bp_select(void)
1100 exit_minibuffer();
1101 if (*ministate.buf != '\0')
1102 add_to_bookmarks(ministate.buf);
1103 else
1104 message("Abort.");
1107 static void
1108 yornp_self_insert(void)
1110 if (thiskey.key != 'y' && thiskey.key != 'n') {
1111 message("Please answer y or n");
1112 return;
1115 exit_minibuffer();
1116 yornp_cb(thiskey.key == 'y', yornp_data);
1119 static void
1120 yornp_abort(void)
1122 exit_minibuffer();
1123 yornp_cb(0, yornp_data);
1126 static struct vline *
1127 nth_line(struct window *window, size_t n)
1129 struct vline *vl;
1130 size_t i;
1132 i = 0;
1133 TAILQ_FOREACH(vl, &window->head, vlines) {
1134 if (i == n)
1135 return vl;
1136 i++;
1139 /* unreachable */
1140 abort();
1143 static struct tab *
1144 current_tab(void)
1146 struct tab *t;
1148 TAILQ_FOREACH(t, &tabshead, tabs) {
1149 if (t->flags & TAB_CURRENT)
1150 return t;
1153 /* unreachable */
1154 abort();
1157 static struct window *
1158 current_window(void)
1160 if (in_minibuffer)
1161 return &ministate.window;
1162 return &current_tab()->window;
1165 static int
1166 readkey(void)
1168 uint32_t state = 0;
1170 if ((thiskey.key = wgetch(body)) == ERR)
1171 return 0;
1173 thiskey.meta = thiskey.key == 27;
1174 if (thiskey.meta) {
1175 thiskey.key = wgetch(body);
1176 if (thiskey.key == ERR || thiskey.key == 27) {
1177 thiskey.meta = 0;
1178 thiskey.key = 27;
1182 thiskey.cp = 0;
1183 if ((unsigned int)thiskey.key < UINT8_MAX) {
1184 while (1) {
1185 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1186 break;
1187 if ((thiskey.key = wgetch(body)) == ERR) {
1188 message("Error decoding user input");
1189 return 0;
1194 return 1;
1197 static void
1198 dispatch_stdio(int fd, short ev, void *d)
1200 struct keymap *k;
1201 const char *keyname;
1202 char tmp[5] = {0};
1204 if (!readkey())
1205 return;
1207 if (keybuf[0] != '\0')
1208 strlcat(keybuf, " ", sizeof(keybuf));
1209 if (thiskey.meta)
1210 strlcat(keybuf, "M-", sizeof(keybuf));
1211 if (thiskey.cp != 0) {
1212 utf8_encode(thiskey.cp, tmp);
1213 strlcat(keybuf, tmp, sizeof(keybuf));
1214 } else {
1215 if ((keyname = unkbd(thiskey.key)) != NULL)
1216 strlcat(keybuf, keyname, sizeof(keybuf));
1217 else {
1218 tmp[0] = thiskey.key;
1219 strlcat(keybuf, tmp, sizeof(keybuf));
1223 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1224 if (k->meta == thiskey.meta &&
1225 k->key == thiskey.key) {
1226 if (k->fn == NULL)
1227 current_map = &k->map;
1228 else {
1229 current_map = base_map;
1230 strlcpy(keybuf, "", sizeof(keybuf));
1231 k->fn(current_window());
1233 goto done;
1237 if (current_map->unhandled_input != NULL)
1238 current_map->unhandled_input();
1239 else {
1240 global_key_unbound();
1243 strlcpy(keybuf, "", sizeof(keybuf));
1244 current_map = base_map;
1246 done:
1247 if (side_window)
1248 recompute_help();
1250 redraw_tab(current_tab());
1253 static void
1254 handle_clear_minibuf(int fd, short ev, void *d)
1256 free(ministate.curmesg);
1257 ministate.curmesg = NULL;
1259 redraw_minibuffer();
1260 if (in_minibuffer) {
1261 wrefresh(body);
1262 wrefresh(minibuf);
1263 } else {
1264 wrefresh(minibuf);
1265 wrefresh(body);
1269 static void
1270 handle_resize(int sig, short ev, void *d)
1272 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1273 event_del(&resizeev);
1275 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1276 evtimer_add(&resizeev, &resize_timer);
1279 static void
1280 handle_resize_timeout(int s, short ev, void *d)
1282 struct tab *tab;
1284 endwin();
1285 refresh();
1286 clear();
1288 /* move and resize the windows, in reverse order! */
1290 mvwin(minibuf, LINES-1, 0);
1291 wresize(minibuf, 1, COLS);
1293 mvwin(modeline, LINES-2, 0);
1294 wresize(modeline, 1, COLS);
1296 body_lines = LINES-3;
1297 body_cols = COLS;
1299 if (side_window) {
1300 help_cols = 0.3 * COLS;
1301 help_lines = LINES-3;
1302 mvwin(help, 1, 0);
1303 wresize(help, help_lines, help_cols);
1305 wrap_page(&helpwin, help_cols);
1307 body_cols = COLS - help_cols - 1;
1308 mvwin(body, 1, help_cols);
1309 } else
1310 mvwin(body, 1, 0);
1312 wresize(body, body_lines, body_cols);
1314 wresize(tabline, 1, COLS);
1316 tab = current_tab();
1318 wrap_page(&tab->window, body_cols);
1319 redraw_tab(tab);
1322 static int
1323 wrap_page(struct window *window, int width)
1325 struct line *l;
1326 const struct line *orig;
1327 struct vline *vl;
1328 const char *prfx;
1330 orig = window->current_line == NULL
1331 ? NULL
1332 : window->current_line->parent;
1333 window->current_line = NULL;
1335 window->curs_y = 0;
1336 window->line_off = 0;
1338 empty_vlist(window);
1340 TAILQ_FOREACH(l, &window->page.head, lines) {
1341 prfx = line_prefixes[l->type].prfx1;
1342 switch (l->type) {
1343 case LINE_TEXT:
1344 case LINE_LINK:
1345 case LINE_TITLE_1:
1346 case LINE_TITLE_2:
1347 case LINE_TITLE_3:
1348 case LINE_ITEM:
1349 case LINE_QUOTE:
1350 case LINE_PRE_START:
1351 case LINE_PRE_END:
1352 wrap_text(window, prfx, l, width);
1353 break;
1354 case LINE_PRE_CONTENT:
1355 hardwrap_text(window, l, width);
1356 break;
1359 if (orig == l && window->current_line == NULL) {
1360 window->line_off = window->line_max-1;
1361 window->current_line = TAILQ_LAST(&window->head, vhead);
1363 while (1) {
1364 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1365 if (vl == NULL || vl->parent != orig)
1366 break;
1367 window->current_line = vl;
1368 window->line_off--;
1373 if (window->current_line == NULL)
1374 window->current_line = TAILQ_FIRST(&window->head);
1376 return 1;
1379 static void
1380 print_vline(WINDOW *window, struct vline *vl)
1382 const char *text = vl->line;
1383 const char *prfx;
1384 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1385 int text_face = line_faces[vl->parent->type].text_prop;
1387 if (!vl->flags)
1388 prfx = line_prefixes[vl->parent->type].prfx1;
1389 else
1390 prfx = line_prefixes[vl->parent->type].prfx2;
1392 if (text == NULL)
1393 text = "";
1395 wattron(window, prefix_face);
1396 wprintw(window, "%s", prfx);
1397 wattroff(window, prefix_face);
1399 wattron(window, text_face);
1400 wprintw(window, "%s", text);
1401 wattroff(window, text_face);
1404 static void
1405 redraw_tabline(void)
1407 struct tab *tab;
1408 size_t toskip, ots, tabwidth, space, x;
1409 int current, y, truncated;
1410 const char *title;
1411 char buf[25];
1413 tabwidth = sizeof(buf)+1;
1414 space = COLS-2;
1416 toskip = 0;
1417 TAILQ_FOREACH(tab, &tabshead, tabs) {
1418 toskip++;
1419 if (tab->flags & TAB_CURRENT)
1420 break;
1423 if (toskip * tabwidth < space)
1424 toskip = 0;
1425 else {
1426 ots = toskip;
1427 toskip--;
1428 while (toskip != 0 &&
1429 (ots - toskip+1) * tabwidth < space)
1430 toskip--;
1433 werase(tabline);
1434 wattron(tabline, tab_face.background);
1435 wprintw(tabline, toskip == 0 ? " " : "<");
1436 wattroff(tabline, tab_face.background);
1438 truncated = 0;
1439 TAILQ_FOREACH(tab, &tabshead, tabs) {
1440 if (truncated)
1441 break;
1442 if (toskip != 0) {
1443 toskip--;
1444 continue;
1447 getyx(tabline, y, x);
1448 if (x + sizeof(buf)+2 >= (size_t)COLS)
1449 truncated = 1;
1451 current = tab->flags & TAB_CURRENT;
1453 if (*(title = tab->window.page.title) == '\0')
1454 title = tab->hist_cur->h;
1456 if (tab->flags & TAB_URGENT)
1457 strlcpy(buf, "!", sizeof(buf));
1458 else
1459 strlcpy(buf, " ", sizeof(buf));
1461 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1462 /* truncation happens */
1463 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1464 } else {
1465 /* pad with spaces */
1466 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1467 /* nop */ ;
1470 if (current)
1471 wattron(tabline, tab_face.current_tab);
1472 else
1473 wattron(tabline, tab_face.tab);
1475 wprintw(tabline, "%s", buf);
1476 if (TAILQ_NEXT(tab, tabs) != NULL)
1477 wprintw(tabline, " ");
1479 if (current)
1480 wattroff(tabline, tab_face.current_tab);
1481 else
1482 wattroff(tabline, tab_face.tab);
1485 wattron(tabline, tab_face.background);
1486 for (; x < (size_t)COLS; ++x)
1487 waddch(tabline, ' ');
1488 if (truncated)
1489 mvwprintw(tabline, 0, COLS-1, ">");
1492 static void
1493 redraw_window(WINDOW *win, int height, struct window *window)
1495 struct vline *vl;
1496 int l;
1498 werase(win);
1500 window->line_off = MIN(window->line_max-1, window->line_off);
1501 if (TAILQ_EMPTY(&window->head))
1502 return;
1504 l = 0;
1505 vl = nth_line(window, window->line_off);
1506 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1507 wmove(win, l, 0);
1508 print_vline(win, vl);
1509 l++;
1510 if (l == height)
1511 break;
1514 wmove(win, window->curs_y, window->curs_x);
1517 static void
1518 redraw_help(void)
1520 redraw_window(help, help_lines, &helpwin);
1523 static void
1524 redraw_body(struct tab *tab)
1526 redraw_window(body, body_lines, &tab->window);
1529 static inline char
1530 trust_status_char(enum trust_state ts)
1532 switch (ts) {
1533 case TS_UNKNOWN: return 'u';
1534 case TS_UNTRUSTED: return '!';
1535 case TS_TRUSTED: return 'v';
1536 case TS_VERIFIED: return 'V';
1540 static void
1541 redraw_modeline(struct tab *tab)
1543 double pct;
1544 int x, y, max_x, max_y;
1545 const char *mode = tab->window.page.name;
1546 const char *spin = "-\\|/";
1548 werase(modeline);
1549 wattron(modeline, A_REVERSE);
1550 wmove(modeline, 0, 0);
1552 wprintw(modeline, "-%c%c %s ",
1553 spin[tab->loading_anim_step],
1554 trust_status_char(tab->trust),
1555 mode == NULL ? "(none)" : mode);
1557 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1559 if (tab->window.line_max <= (size_t)body_lines)
1560 wprintw(modeline, "All ");
1561 else if (tab->window.line_off == 0)
1562 wprintw(modeline, "Top ");
1563 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1564 wprintw(modeline, "Bottom ");
1565 else
1566 wprintw(modeline, "%.0f%% ", pct);
1568 wprintw(modeline, "%d/%d %s ",
1569 tab->window.line_off + tab->window.curs_y,
1570 tab->window.line_max,
1571 tab->hist_cur->h);
1573 getyx(modeline, y, x);
1574 getmaxyx(modeline, max_y, max_x);
1576 (void)y;
1577 (void)max_y;
1579 for (; x < max_x; ++x)
1580 waddstr(modeline, "-");
1583 static void
1584 redraw_minibuffer(void)
1586 struct tab *tab;
1587 size_t off_y, off_x = 0;
1588 char *start, *c;
1590 werase(minibuf);
1592 if (in_minibuffer) {
1593 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1594 if (ministate.hist_cur != NULL)
1595 wprintw(minibuf, "(%zu/%zu) ",
1596 ministate.hist_off + 1,
1597 ministate.history->len);
1599 getyx(minibuf, off_y, off_x);
1601 start = ministate.hist_cur != NULL
1602 ? ministate.hist_cur->h
1603 : ministate.buf;
1604 c = utf8_nth(ministate.window.current_line->line,
1605 ministate.window.cpoff);
1606 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1607 start = utf8_next_cp(start);
1610 waddstr(minibuf, start);
1613 if (ministate.curmesg != NULL)
1614 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1615 ministate.curmesg);
1617 if (!in_minibuffer && ministate.curmesg == NULL)
1618 waddstr(minibuf, keybuf);
1620 /* If nothing else, show the URL at point */
1621 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1622 tab = current_tab();
1623 if (tab->window.current_line != NULL &&
1624 tab->window.current_line->parent->type == LINE_LINK)
1625 waddstr(minibuf, tab->window.current_line->parent->alt);
1628 if (in_minibuffer)
1629 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1632 static void
1633 redraw_tab(struct tab *tab)
1635 if (side_window) {
1636 redraw_help();
1637 wnoutrefresh(help);
1640 redraw_tabline();
1641 redraw_body(tab);
1642 redraw_modeline(tab);
1643 redraw_minibuffer();
1645 wnoutrefresh(tabline);
1646 wnoutrefresh(modeline);
1648 if (in_minibuffer) {
1649 wnoutrefresh(body);
1650 wnoutrefresh(minibuf);
1651 } else {
1652 wnoutrefresh(minibuf);
1653 wnoutrefresh(body);
1656 doupdate();
1659 static void
1660 emit_help_item(char *prfx, void *fn)
1662 struct line *l;
1663 struct cmds *cmd;
1665 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1666 if (fn == cmd->fn)
1667 break;
1669 assert(cmd != NULL);
1671 if ((l = calloc(1, sizeof(*l))) == NULL)
1672 abort();
1674 l->type = LINE_TEXT;
1675 l->alt = NULL;
1677 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1679 if (TAILQ_EMPTY(&helpwin.page.head))
1680 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1681 else
1682 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1685 static void
1686 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1688 struct keymap *k;
1689 char p[32];
1690 const char *kn;
1692 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1693 strlcpy(p, prfx, sizeof(p));
1694 if (*p != '\0')
1695 strlcat(p, " ", sizeof(p));
1696 if (k->meta)
1697 strlcat(p, "M-", sizeof(p));
1698 if ((kn = unkbd(k->key)) != NULL)
1699 strlcat(p, kn, sizeof(p));
1700 else
1701 strlcat(p, keyname(k->key), sizeof(p));
1703 if (k->fn == NULL)
1704 rec_compute_help(&k->map, p, sizeof(p));
1705 else
1706 emit_help_item(p, k->fn);
1710 static void
1711 recompute_help(void)
1713 char p[32] = { 0 };
1715 empty_vlist(&helpwin);
1716 empty_linelist(&helpwin);
1717 rec_compute_help(current_map, p, sizeof(p));
1718 wrap_page(&helpwin, help_cols);
1721 static void
1722 vmessage(const char *fmt, va_list ap)
1724 if (evtimer_pending(&clminibufev, NULL))
1725 evtimer_del(&clminibufev);
1726 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1727 evtimer_add(&clminibufev, &clminibufev_timer);
1729 free(ministate.curmesg);
1731 /* TODO: what to do if the allocation fails here? */
1732 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1733 ministate.curmesg = NULL;
1735 redraw_minibuffer();
1736 if (in_minibuffer) {
1737 wrefresh(body);
1738 wrefresh(minibuf);
1739 } else {
1740 wrefresh(minibuf);
1741 wrefresh(body);
1745 static void
1746 message(const char *fmt, ...)
1748 va_list ap;
1750 va_start(ap, fmt);
1751 vmessage(fmt, ap);
1752 va_end(ap);
1755 static void
1756 start_loading_anim(struct tab *tab)
1758 if (tab->loading_anim)
1759 return;
1760 tab->loading_anim = 1;
1761 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1762 evtimer_add(&tab->loadingev, &loadingev_timer);
1765 static void
1766 update_loading_anim(int fd, short ev, void *d)
1768 struct tab *tab = d;
1770 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1772 if (tab->flags & TAB_CURRENT) {
1773 redraw_modeline(tab);
1774 wrefresh(modeline);
1775 wrefresh(body);
1776 if (in_minibuffer)
1777 wrefresh(minibuf);
1780 evtimer_add(&tab->loadingev, &loadingev_timer);
1783 static void
1784 stop_loading_anim(struct tab *tab)
1786 if (!tab->loading_anim)
1787 return;
1788 evtimer_del(&tab->loadingev);
1789 tab->loading_anim = 0;
1790 tab->loading_anim_step = 0;
1792 if (!(tab->flags & TAB_CURRENT))
1793 return;
1795 redraw_modeline(tab);
1797 wrefresh(modeline);
1798 wrefresh(body);
1799 if (in_minibuffer)
1800 wrefresh(minibuf);
1803 static void
1804 load_url_in_tab(struct tab *tab, const char *url)
1806 message("Loading %s...", url);
1807 start_loading_anim(tab);
1808 load_url(tab, url);
1810 tab->window.curs_x = 0;
1811 tab->window.curs_y = 0;
1812 redraw_tab(tab);
1815 static void
1816 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1817 void (*abortfn)(void), struct histhead *hist)
1819 in_minibuffer = 1;
1820 base_map = &minibuffer_map;
1821 current_map = &minibuffer_map;
1823 base_map->unhandled_input = self_insert_fn;
1825 ministate.donefn = donefn;
1826 ministate.abortfn = abortfn;
1827 memset(ministate.buf, 0, sizeof(ministate.buf));
1828 ministate.window.current_line = &ministate.vline;
1829 ministate.window.current_line->line = ministate.buf;
1830 ministate.window.cpoff = 0;
1831 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1833 ministate.history = hist;
1834 ministate.hist_cur = NULL;
1835 ministate.hist_off = 0;
1838 static void
1839 exit_minibuffer(void)
1841 werase(minibuf);
1843 in_minibuffer = 0;
1844 base_map = &global_map;
1845 current_map = &global_map;
1848 static void
1849 switch_to_tab(struct tab *tab)
1851 struct tab *t;
1853 TAILQ_FOREACH(t, &tabshead, tabs) {
1854 t->flags &= ~TAB_CURRENT;
1857 tab->flags |= TAB_CURRENT;
1860 unsigned int
1861 tab_new_id(void)
1863 return tab_counter++;
1866 static struct tab *
1867 new_tab(const char *url)
1869 struct tab *tab;
1871 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1872 event_loopbreak();
1873 return NULL;
1876 TAILQ_INIT(&tab->hist.head);
1878 TAILQ_INIT(&tab->window.head);
1880 tab->id = tab_new_id();
1881 switch_to_tab(tab);
1883 if (TAILQ_EMPTY(&tabshead))
1884 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1885 else
1886 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1888 load_url_in_tab(tab, url);
1889 return tab;
1892 static void
1893 session_new_tab_cb(const char *url)
1895 new_tab(url);
1898 static void
1899 usage(void)
1901 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1904 int
1905 ui_init(int argc, char * const *argv)
1907 const char *url = NEW_TAB_URL;
1908 int ch;
1910 while ((ch = getopt(argc, argv, "")) != -1) {
1911 switch (ch) {
1912 default:
1913 usage();
1914 return 0;
1917 argc -= optind;
1918 argv += optind;
1920 if (argc != 0)
1921 url = argv[0];
1923 setlocale(LC_ALL, "");
1925 TAILQ_INIT(&global_map.m);
1926 global_map.unhandled_input = global_key_unbound;
1928 TAILQ_INIT(&minibuffer_map.m);
1930 TAILQ_INIT(&eecmd_history.head);
1931 TAILQ_INIT(&ir_history.head);
1932 TAILQ_INIT(&lu_history.head);
1934 ministate.line.type = LINE_TEXT;
1935 ministate.vline.parent = &ministate.line;
1936 ministate.window.current_line = &ministate.vline;
1938 /* initialize help window */
1939 TAILQ_INIT(&helpwin.head);
1941 base_map = &global_map;
1942 current_map = &global_map;
1943 load_default_keys();
1945 initscr();
1946 raw();
1947 noecho();
1949 nonl();
1950 intrflush(stdscr, FALSE);
1952 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1953 return 0;
1954 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1955 return 0;
1956 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1957 return 0;
1958 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1959 return 0;
1960 if ((help = newwin(1, 1, 1, 0)) == NULL)
1961 return 0;
1963 body_lines = LINES-3;
1964 body_cols = COLS;
1966 keypad(body, TRUE);
1967 scrollok(body, TRUE);
1969 /* non-blocking input */
1970 wtimeout(body, 0);
1972 mvwprintw(body, 0, 0, "");
1974 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1975 event_add(&stdioev, NULL);
1977 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1978 signal_add(&winchev, NULL);
1980 load_last_session(session_new_tab_cb);
1981 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1982 new_tab(url);
1984 return 1;
1987 void
1988 ui_on_tab_loaded(struct tab *tab)
1990 stop_loading_anim(tab);
1991 message("Loaded %s", tab->hist_cur->h);
1993 redraw_tabline();
1994 wrefresh(tabline);
1995 if (in_minibuffer)
1996 wrefresh(minibuf);
1997 else
1998 wrefresh(body);
2001 void
2002 ui_on_tab_refresh(struct tab *tab)
2004 wrap_page(&tab->window, body_cols);
2005 if (tab->flags & TAB_CURRENT) {
2006 restore_cursor(&tab->window);
2007 redraw_tab(tab);
2008 } else
2009 tab->flags |= TAB_URGENT;
2012 void
2013 ui_require_input(struct tab *tab, int hide)
2015 /* TODO: hard-switching to another tab is ugly */
2016 switch_to_tab(tab);
2018 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2019 &ir_history);
2020 strlcpy(ministate.prompt, "Input required: ",
2021 sizeof(ministate.prompt));
2022 redraw_tab(tab);
2025 void
2026 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2027 unsigned int data)
2029 size_t len;
2031 if (in_minibuffer) {
2032 fn(0, data);
2033 return;
2036 yornp_cb = fn;
2037 yornp_data = data;
2038 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2039 yornp_abort, NULL);
2041 len = sizeof(ministate.prompt);
2042 strlcpy(ministate.prompt, prompt, len);
2043 strlcat(ministate.prompt, " (y or n) ", len);
2044 redraw_tab(current_tab());
2047 void
2048 ui_notify(const char *fmt, ...)
2050 va_list ap;
2052 va_start(ap, fmt);
2053 vmessage(fmt, ap);
2054 va_end(ap);
2057 void
2058 ui_end(void)
2060 endwin();