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));
826 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
827 cmd_move_end_of_line(&ministate.window);
830 static void
831 cmd_load_current_url(struct window *window)
833 struct tab *tab = current_tab();
835 if (in_minibuffer) {
836 message("We don't have enable-recursive-minibuffers");
837 return;
840 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
841 &lu_history);
842 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
843 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
844 ministate.window.cpoff = utf8_cplen(ministate.buf);
847 static void
848 cmd_bookmark_page(struct window *window)
850 struct tab *tab = current_tab();
852 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
853 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
854 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
855 ministate.window.cpoff = utf8_cplen(ministate.buf);
858 static void
859 cmd_list_bookmarks(struct window *window)
861 load_url_in_tab(current_tab(), "about:bookmarks");
864 static void
865 cmd_toggle_help(struct window *window)
867 side_window = !side_window;
868 if (side_window)
869 recompute_help();
871 /* ugly hack, but otherwise the window doesn't get updated
872 * until I call handle_resize a second time (i.e. C-l). I
873 * will be happy to know why something like this is needed. */
874 handle_resize(0, 0, NULL);
875 handle_resize(0, 0, NULL);
878 static void
879 cmd_mini_delete_char(struct window *window)
881 char *c, *n;
883 if (!in_minibuffer) {
884 message("text is read-only");
885 return;
888 minibuffer_taint_hist();
890 c = utf8_nth(window->current_line->line, window->cpoff);
891 if (*c == '\0')
892 return;
893 n = utf8_next_cp(c);
895 memmove(c, n, strlen(n)+1);
898 static void
899 cmd_mini_delete_backward_char(struct window *window)
901 char *c, *p, *start;
903 if (!in_minibuffer) {
904 message("text is read-only");
905 return;
908 minibuffer_taint_hist();
910 c = utf8_nth(window->current_line->line, window->cpoff);
911 start = window->current_line->line;
912 if (c == start)
913 return;
914 p = utf8_prev_cp(c-1, start);
916 memmove(p, c, strlen(c)+1);
917 window->cpoff--;
920 static void
921 cmd_mini_kill_line(struct window *window)
923 char *c;
925 if (!in_minibuffer) {
926 message("text is read-only");
927 return;
930 minibuffer_taint_hist();
931 c = utf8_nth(window->current_line->line, window->cpoff);
932 *c = '\0';
935 static void
936 cmd_mini_abort(struct window *window)
938 if (!in_minibuffer)
939 return;
941 ministate.abortfn();
944 static void
945 cmd_mini_complete_and_exit(struct window *window)
947 if (!in_minibuffer)
948 return;
950 minibuffer_taint_hist();
951 ministate.donefn();
954 static void
955 cmd_mini_previous_history_element(struct window *window)
957 if (ministate.history == NULL) {
958 message("No history");
959 return;
962 if (ministate.hist_cur == NULL ||
963 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
964 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
965 ministate.hist_off = ministate.history->len - 1;
966 if (ministate.hist_cur == NULL)
967 message("No prev item");
968 } else {
969 ministate.hist_off--;
972 if (ministate.hist_cur != NULL)
973 window->current_line->line = ministate.hist_cur->h;
976 static void
977 cmd_mini_next_history_element(struct window *window)
979 if (ministate.history == NULL) {
980 message("No history");
981 return;
984 if (ministate.hist_cur == NULL ||
985 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
986 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
987 ministate.hist_off = 0;
988 if (ministate.hist_cur == NULL)
989 message("No next item");
990 } else {
991 ministate.hist_off++;
994 if (ministate.hist_cur != NULL)
995 window->current_line->line = ministate.hist_cur->h;
998 static void
999 global_key_unbound(void)
1001 message("%s is undefined", keybuf);
1004 static void
1005 minibuffer_hist_save_entry(void)
1007 struct hist *hist;
1009 if (ministate.history == NULL)
1010 return;
1012 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1013 abort();
1015 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1017 if (TAILQ_EMPTY(&ministate.history->head))
1018 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1019 else
1020 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1021 ministate.history->len++;
1025 * taint the minibuffer cache: if we're currently showing a history
1026 * element, copy that to the current buf and reset the "history
1027 * navigation" thing.
1029 static void
1030 minibuffer_taint_hist(void)
1032 if (ministate.hist_cur == NULL)
1033 return;
1035 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1036 ministate.hist_cur = NULL;
1039 static void
1040 minibuffer_self_insert(void)
1042 char *c, tmp[5] = {0};
1043 size_t len;
1045 minibuffer_taint_hist();
1047 if (thiskey.cp == 0)
1048 return;
1050 len = utf8_encode(thiskey.cp, tmp);
1051 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1052 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1053 return;
1055 memmove(c + len, c, strlen(c)+1);
1056 memcpy(c, tmp, len);
1057 ministate.window.cpoff++;
1060 static void
1061 eecmd_self_insert(void)
1063 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1064 !unicode_isgraph(thiskey.cp)) {
1065 global_key_unbound();
1066 return;
1069 minibuffer_self_insert();
1072 static void
1073 eecmd_select(void)
1075 struct cmds *cmd;
1077 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1078 if (!strcmp(cmd->cmd, ministate.buf)) {
1079 exit_minibuffer();
1080 minibuffer_hist_save_entry();
1081 cmd->fn(current_window());
1082 return;
1086 message("No match");
1089 static void
1090 ir_self_insert(void)
1092 minibuffer_self_insert();
1095 static void
1096 ir_select(void)
1098 char buf[1025] = {0};
1099 struct phos_uri uri;
1100 struct tab *tab;
1102 tab = current_tab();
1104 exit_minibuffer();
1105 minibuffer_hist_save_entry();
1107 /* a bit ugly but... */
1108 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1109 phos_uri_set_query(&uri, ministate.buf);
1110 phos_serialize_uri(&uri, buf, sizeof(buf));
1111 load_url_in_tab(tab, buf);
1114 static void
1115 lu_self_insert(void)
1117 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1118 !unicode_isgraph(thiskey.key)) {
1119 global_key_unbound();
1120 return;
1123 minibuffer_self_insert();
1126 static void
1127 lu_select(void)
1129 exit_minibuffer();
1130 minibuffer_hist_save_entry();
1131 load_url_in_tab(current_tab(), ministate.buf);
1134 static void
1135 bp_select(void)
1137 exit_minibuffer();
1138 if (*ministate.buf != '\0')
1139 add_to_bookmarks(ministate.buf);
1140 else
1141 message("Abort.");
1144 static void
1145 yornp_self_insert(void)
1147 if (thiskey.key != 'y' && thiskey.key != 'n') {
1148 message("Please answer y or n");
1149 return;
1152 exit_minibuffer();
1153 yornp_cb(thiskey.key == 'y', yornp_data);
1156 static void
1157 yornp_abort(void)
1159 exit_minibuffer();
1160 yornp_cb(0, yornp_data);
1163 static struct vline *
1164 nth_line(struct window *window, size_t n)
1166 struct vline *vl;
1167 size_t i;
1169 i = 0;
1170 TAILQ_FOREACH(vl, &window->head, vlines) {
1171 if (i == n)
1172 return vl;
1173 i++;
1176 /* unreachable */
1177 abort();
1180 static struct tab *
1181 current_tab(void)
1183 struct tab *t;
1185 TAILQ_FOREACH(t, &tabshead, tabs) {
1186 if (t->flags & TAB_CURRENT)
1187 return t;
1190 /* unreachable */
1191 abort();
1194 static struct window *
1195 current_window(void)
1197 if (in_minibuffer)
1198 return &ministate.window;
1199 return &current_tab()->window;
1202 static int
1203 readkey(void)
1205 uint32_t state = 0;
1207 if ((thiskey.key = wgetch(body)) == ERR)
1208 return 0;
1210 thiskey.meta = thiskey.key == 27;
1211 if (thiskey.meta) {
1212 thiskey.key = wgetch(body);
1213 if (thiskey.key == ERR || thiskey.key == 27) {
1214 thiskey.meta = 0;
1215 thiskey.key = 27;
1219 thiskey.cp = 0;
1220 if ((unsigned int)thiskey.key < UINT8_MAX) {
1221 while (1) {
1222 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1223 break;
1224 if ((thiskey.key = wgetch(body)) == ERR) {
1225 message("Error decoding user input");
1226 return 0;
1231 return 1;
1234 static void
1235 dispatch_stdio(int fd, short ev, void *d)
1237 struct keymap *k;
1238 const char *keyname;
1239 char tmp[5] = {0};
1241 if (!readkey())
1242 return;
1244 if (keybuf[0] != '\0')
1245 strlcat(keybuf, " ", sizeof(keybuf));
1246 if (thiskey.meta)
1247 strlcat(keybuf, "M-", sizeof(keybuf));
1248 if (thiskey.cp != 0) {
1249 utf8_encode(thiskey.cp, tmp);
1250 strlcat(keybuf, tmp, sizeof(keybuf));
1251 } else {
1252 if ((keyname = unkbd(thiskey.key)) != NULL)
1253 strlcat(keybuf, keyname, sizeof(keybuf));
1254 else {
1255 tmp[0] = thiskey.key;
1256 strlcat(keybuf, tmp, sizeof(keybuf));
1260 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1261 if (k->meta == thiskey.meta &&
1262 k->key == thiskey.key) {
1263 if (k->fn == NULL)
1264 current_map = &k->map;
1265 else {
1266 current_map = base_map;
1267 strlcpy(keybuf, "", sizeof(keybuf));
1268 k->fn(current_window());
1270 goto done;
1274 if (current_map->unhandled_input != NULL)
1275 current_map->unhandled_input();
1276 else {
1277 global_key_unbound();
1280 strlcpy(keybuf, "", sizeof(keybuf));
1281 current_map = base_map;
1283 done:
1284 if (side_window)
1285 recompute_help();
1287 redraw_tab(current_tab());
1290 static void
1291 handle_clear_minibuf(int fd, short ev, void *d)
1293 free(ministate.curmesg);
1294 ministate.curmesg = NULL;
1296 redraw_minibuffer();
1297 if (in_minibuffer) {
1298 wrefresh(body);
1299 wrefresh(minibuf);
1300 } else {
1301 wrefresh(minibuf);
1302 wrefresh(body);
1306 static void
1307 handle_resize(int sig, short ev, void *d)
1309 struct tab *tab;
1311 endwin();
1312 refresh();
1313 clear();
1315 /* move and resize the windows, in reverse order! */
1317 mvwin(minibuf, LINES-1, 0);
1318 wresize(minibuf, 1, COLS);
1320 mvwin(modeline, LINES-2, 0);
1321 wresize(modeline, 1, COLS);
1323 body_lines = LINES-3;
1324 body_cols = COLS;
1326 if (side_window) {
1327 help_cols = 0.3 * COLS;
1328 help_lines = LINES-3;
1329 mvwin(help, 1, 0);
1330 wresize(help, help_lines, help_cols);
1332 wrap_page(&helpwin, help_cols);
1334 body_cols = COLS - help_cols - 1;
1335 mvwin(body, 1, help_cols);
1336 } else
1337 mvwin(body, 1, 0);
1339 wresize(body, body_lines, body_cols);
1341 wresize(tabline, 1, COLS);
1343 tab = current_tab();
1345 wrap_page(&tab->window, body_cols);
1346 redraw_tab(tab);
1349 static int
1350 wrap_page(struct window *window, int width)
1352 struct line *l;
1353 const struct line *orig;
1354 struct vline *vl;
1355 const char *prfx;
1357 orig = window->current_line == NULL
1358 ? NULL
1359 : window->current_line->parent;
1360 window->current_line = NULL;
1362 window->curs_y = 0;
1363 window->line_off = 0;
1365 empty_vlist(window);
1367 TAILQ_FOREACH(l, &window->page.head, lines) {
1368 prfx = line_prefixes[l->type].prfx1;
1369 switch (l->type) {
1370 case LINE_TEXT:
1371 case LINE_LINK:
1372 case LINE_TITLE_1:
1373 case LINE_TITLE_2:
1374 case LINE_TITLE_3:
1375 case LINE_ITEM:
1376 case LINE_QUOTE:
1377 case LINE_PRE_START:
1378 case LINE_PRE_END:
1379 wrap_text(window, prfx, l, width);
1380 break;
1381 case LINE_PRE_CONTENT:
1382 hardwrap_text(window, l, width);
1383 break;
1386 if (orig == l && window->current_line == NULL) {
1387 window->line_off = window->line_max-1;
1388 window->current_line = TAILQ_LAST(&window->head, vhead);
1390 while (1) {
1391 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1392 if (vl == NULL || vl->parent != orig)
1393 break;
1394 window->current_line = vl;
1395 window->line_off--;
1400 if (window->current_line == NULL)
1401 window->current_line = TAILQ_FIRST(&window->head);
1403 return 1;
1406 static void
1407 print_vline(WINDOW *window, struct vline *vl)
1409 const char *text = vl->line;
1410 const char *prfx;
1411 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1412 int text_face = line_faces[vl->parent->type].text_prop;
1414 if (!vl->flags)
1415 prfx = line_prefixes[vl->parent->type].prfx1;
1416 else
1417 prfx = line_prefixes[vl->parent->type].prfx2;
1419 if (text == NULL)
1420 text = "";
1422 wattron(window, prefix_face);
1423 wprintw(window, "%s", prfx);
1424 wattroff(window, prefix_face);
1426 wattron(window, text_face);
1427 wprintw(window, "%s", text);
1428 wattroff(window, text_face);
1431 static void
1432 redraw_tabline(void)
1434 struct tab *tab;
1435 size_t toskip, ots, tabwidth, space, x;
1436 int current, y, truncated;
1437 const char *title;
1438 char buf[25];
1440 tabwidth = sizeof(buf)+1;
1441 space = COLS-2;
1443 toskip = 0;
1444 TAILQ_FOREACH(tab, &tabshead, tabs) {
1445 toskip++;
1446 if (tab->flags & TAB_CURRENT)
1447 break;
1450 if (toskip * tabwidth < space)
1451 toskip = 0;
1452 else {
1453 ots = toskip;
1454 toskip--;
1455 while (toskip != 0 &&
1456 (ots - toskip+1) * tabwidth < space)
1457 toskip--;
1460 werase(tabline);
1461 wattron(tabline, tab_face.background);
1462 wprintw(tabline, toskip == 0 ? " " : "<");
1463 wattroff(tabline, tab_face.background);
1465 truncated = 0;
1466 TAILQ_FOREACH(tab, &tabshead, tabs) {
1467 if (truncated)
1468 break;
1469 if (toskip != 0) {
1470 toskip--;
1471 continue;
1474 getyx(tabline, y, x);
1475 if (x + sizeof(buf)+2 >= (size_t)COLS)
1476 truncated = 1;
1478 current = tab->flags & TAB_CURRENT;
1480 if (*(title = tab->window.page.title) == '\0')
1481 title = tab->hist_cur->h;
1483 strlcpy(buf, " ", sizeof(buf));
1484 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1485 /* truncation happens */
1486 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1487 } else {
1488 /* pad with spaces */
1489 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1490 /* nop */ ;
1493 if (current)
1494 wattron(tabline, tab_face.current_tab);
1495 else
1496 wattron(tabline, tab_face.tab);
1498 wprintw(tabline, "%s", buf);
1499 if (TAILQ_NEXT(tab, tabs) != NULL)
1500 wprintw(tabline, " ");
1502 if (current)
1503 wattroff(tabline, tab_face.current_tab);
1504 else
1505 wattroff(tabline, tab_face.tab);
1508 wattron(tabline, tab_face.background);
1509 for (; x < (size_t)COLS; ++x)
1510 waddch(tabline, ' ');
1511 if (truncated)
1512 mvwprintw(tabline, 0, COLS-1, ">");
1515 static void
1516 redraw_window(WINDOW *win, int height, struct window *window)
1518 struct vline *vl;
1519 int l;
1521 werase(win);
1523 window->line_off = MIN(window->line_max-1, window->line_off);
1524 if (TAILQ_EMPTY(&window->head))
1525 return;
1527 l = 0;
1528 vl = nth_line(window, window->line_off);
1529 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1530 wmove(win, l, 0);
1531 print_vline(win, vl);
1532 l++;
1533 if (l == height)
1534 break;
1537 wmove(win, window->curs_y, window->curs_x);
1540 static void
1541 redraw_help(void)
1543 redraw_window(help, help_lines, &helpwin);
1546 static void
1547 redraw_body(struct tab *tab)
1549 redraw_window(body, body_lines, &tab->window);
1552 static inline char
1553 trust_status_char(enum trust_state ts)
1555 switch (ts) {
1556 case TS_UNKNOWN: return 'u';
1557 case TS_UNTRUSTED: return '!';
1558 case TS_TRUSTED: return 'v';
1559 case TS_VERIFIED: return 'V';
1563 static void
1564 redraw_modeline(struct tab *tab)
1566 double pct;
1567 int x, y, max_x, max_y;
1568 const char *mode = tab->window.page.name;
1569 const char *spin = "-\\|/";
1571 werase(modeline);
1572 wattron(modeline, A_REVERSE);
1573 wmove(modeline, 0, 0);
1575 wprintw(modeline, "-%c%c %s ",
1576 spin[tab->loading_anim_step],
1577 trust_status_char(tab->trust),
1578 mode == NULL ? "(none)" : mode);
1580 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1582 if (tab->window.line_max <= (size_t)body_lines)
1583 wprintw(modeline, "All ");
1584 else if (tab->window.line_off == 0)
1585 wprintw(modeline, "Top ");
1586 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1587 wprintw(modeline, "Bottom ");
1588 else
1589 wprintw(modeline, "%.0f%% ", pct);
1591 wprintw(modeline, "%d/%d %s ",
1592 tab->window.line_off + tab->window.curs_y,
1593 tab->window.line_max,
1594 tab->hist_cur->h);
1596 getyx(modeline, y, x);
1597 getmaxyx(modeline, max_y, max_x);
1599 (void)y;
1600 (void)max_y;
1602 for (; x < max_x; ++x)
1603 waddstr(modeline, "-");
1606 static void
1607 redraw_minibuffer(void)
1609 struct tab *tab;
1610 size_t off_y, off_x = 0;
1611 char *start, *c;
1613 werase(minibuf);
1615 if (in_minibuffer) {
1616 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1617 if (ministate.hist_cur != NULL)
1618 wprintw(minibuf, "(%zu/%zu) ",
1619 ministate.hist_off + 1,
1620 ministate.history->len);
1622 getyx(minibuf, off_y, off_x);
1624 start = ministate.hist_cur != NULL
1625 ? ministate.hist_cur->h
1626 : ministate.buf;
1627 c = utf8_nth(ministate.window.current_line->line,
1628 ministate.window.cpoff);
1629 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1630 start = utf8_next_cp(start);
1633 waddstr(minibuf, start);
1636 if (ministate.curmesg != NULL)
1637 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1638 ministate.curmesg);
1640 if (!in_minibuffer && ministate.curmesg == NULL)
1641 waddstr(minibuf, keybuf);
1643 /* If nothing else, show the URL at point */
1644 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1645 tab = current_tab();
1646 if (tab->window.current_line != NULL &&
1647 tab->window.current_line->parent->type == LINE_LINK)
1648 waddstr(minibuf, tab->window.current_line->parent->alt);
1651 if (in_minibuffer)
1652 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1655 static void
1656 redraw_tab(struct tab *tab)
1658 if (side_window) {
1659 redraw_help();
1660 wnoutrefresh(help);
1663 redraw_tabline();
1664 redraw_body(tab);
1665 redraw_modeline(tab);
1666 redraw_minibuffer();
1668 wnoutrefresh(tabline);
1669 wnoutrefresh(modeline);
1671 if (in_minibuffer) {
1672 wnoutrefresh(body);
1673 wnoutrefresh(minibuf);
1674 } else {
1675 wnoutrefresh(minibuf);
1676 wnoutrefresh(body);
1679 doupdate();
1682 static void
1683 emit_help_item(char *prfx, void *fn)
1685 struct line *l;
1686 struct cmds *cmd;
1688 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1689 if (fn == cmd->fn)
1690 break;
1692 assert(cmd != NULL);
1694 if ((l = calloc(1, sizeof(*l))) == NULL)
1695 abort();
1697 l->type = LINE_TEXT;
1698 l->alt = NULL;
1700 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1702 if (TAILQ_EMPTY(&helpwin.page.head))
1703 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1704 else
1705 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1708 static void
1709 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1711 struct keymap *k;
1712 char p[32];
1713 const char *kn;
1715 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1716 strlcpy(p, prfx, sizeof(p));
1717 if (*p != '\0')
1718 strlcat(p, " ", sizeof(p));
1719 if (k->meta)
1720 strlcat(p, "M-", sizeof(p));
1721 if ((kn = unkbd(k->key)) != NULL)
1722 strlcat(p, kn, sizeof(p));
1723 else
1724 strlcat(p, keyname(k->key), sizeof(p));
1726 if (k->fn == NULL)
1727 rec_compute_help(&k->map, p, sizeof(p));
1728 else
1729 emit_help_item(p, k->fn);
1733 static void
1734 recompute_help(void)
1736 char p[32] = { 0 };
1738 empty_vlist(&helpwin);
1739 empty_linelist(&helpwin);
1740 rec_compute_help(current_map, p, sizeof(p));
1741 wrap_page(&helpwin, help_cols);
1744 static void
1745 vmessage(const char *fmt, va_list ap)
1747 if (evtimer_pending(&clminibufev, NULL))
1748 evtimer_del(&clminibufev);
1749 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1750 evtimer_add(&clminibufev, &clminibufev_timer);
1752 free(ministate.curmesg);
1754 /* TODO: what to do if the allocation fails here? */
1755 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1756 ministate.curmesg = NULL;
1758 redraw_minibuffer();
1759 if (in_minibuffer) {
1760 wrefresh(body);
1761 wrefresh(minibuf);
1762 } else {
1763 wrefresh(minibuf);
1764 wrefresh(body);
1768 static void
1769 message(const char *fmt, ...)
1771 va_list ap;
1773 va_start(ap, fmt);
1774 vmessage(fmt, ap);
1775 va_end(ap);
1778 static void
1779 start_loading_anim(struct tab *tab)
1781 if (tab->loading_anim)
1782 return;
1783 tab->loading_anim = 1;
1784 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1785 evtimer_add(&tab->loadingev, &loadingev_timer);
1788 static void
1789 update_loading_anim(int fd, short ev, void *d)
1791 struct tab *tab = d;
1793 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1795 if (tab->flags & TAB_CURRENT) {
1796 redraw_modeline(tab);
1797 wrefresh(modeline);
1798 wrefresh(body);
1799 if (in_minibuffer)
1800 wrefresh(minibuf);
1803 evtimer_add(&tab->loadingev, &loadingev_timer);
1806 static void
1807 stop_loading_anim(struct tab *tab)
1809 if (!tab->loading_anim)
1810 return;
1811 evtimer_del(&tab->loadingev);
1812 tab->loading_anim = 0;
1813 tab->loading_anim_step = 0;
1815 if (!(tab->flags & TAB_CURRENT))
1816 return;
1818 redraw_modeline(tab);
1820 wrefresh(modeline);
1821 wrefresh(body);
1822 if (in_minibuffer)
1823 wrefresh(minibuf);
1826 static void
1827 load_url_in_tab(struct tab *tab, const char *url)
1829 message("Loading %s...", url);
1830 start_loading_anim(tab);
1831 load_url(tab, url);
1833 tab->window.curs_x = 0;
1834 tab->window.curs_y = 0;
1835 redraw_tab(tab);
1838 static void
1839 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1840 void (*abortfn)(void), struct histhead *hist)
1842 in_minibuffer = 1;
1843 base_map = &minibuffer_map;
1844 current_map = &minibuffer_map;
1846 base_map->unhandled_input = self_insert_fn;
1848 ministate.donefn = donefn;
1849 ministate.abortfn = abortfn;
1850 memset(ministate.buf, 0, sizeof(ministate.buf));
1851 ministate.window.current_line = &ministate.vline;
1852 ministate.window.current_line->line = ministate.buf;
1853 ministate.window.cpoff = 0;
1854 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1856 ministate.history = hist;
1857 ministate.hist_cur = NULL;
1858 ministate.hist_off = 0;
1861 static void
1862 exit_minibuffer(void)
1864 werase(minibuf);
1866 in_minibuffer = 0;
1867 base_map = &global_map;
1868 current_map = &global_map;
1871 static void
1872 switch_to_tab(struct tab *tab)
1874 struct tab *t;
1876 TAILQ_FOREACH(t, &tabshead, tabs) {
1877 t->flags &= ~TAB_CURRENT;
1880 tab->flags |= TAB_CURRENT;
1883 unsigned int
1884 tab_new_id(void)
1886 return tab_counter++;
1889 static struct tab *
1890 new_tab(const char *url)
1892 struct tab *tab;
1894 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1895 event_loopbreak();
1896 return NULL;
1899 TAILQ_INIT(&tab->hist.head);
1901 TAILQ_INIT(&tab->window.head);
1903 tab->id = tab_new_id();
1904 switch_to_tab(tab);
1906 if (TAILQ_EMPTY(&tabshead))
1907 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1908 else
1909 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1911 load_url_in_tab(tab, url);
1912 return tab;
1915 static void
1916 session_new_tab_cb(const char *url)
1918 new_tab(url);
1921 static void
1922 usage(void)
1924 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1927 int
1928 ui_init(int argc, char * const *argv)
1930 const char *url = NEW_TAB_URL;
1931 int ch;
1933 while ((ch = getopt(argc, argv, "")) != -1) {
1934 switch (ch) {
1935 default:
1936 usage();
1937 return 0;
1940 argc -= optind;
1941 argv += optind;
1943 if (argc != 0)
1944 url = argv[0];
1946 setlocale(LC_ALL, "");
1948 TAILQ_INIT(&global_map.m);
1949 global_map.unhandled_input = global_key_unbound;
1951 TAILQ_INIT(&minibuffer_map.m);
1953 TAILQ_INIT(&eecmd_history.head);
1954 TAILQ_INIT(&ir_history.head);
1955 TAILQ_INIT(&lu_history.head);
1957 ministate.line.type = LINE_TEXT;
1958 ministate.vline.parent = &ministate.line;
1959 ministate.window.current_line = &ministate.vline;
1961 /* initialize help window */
1962 TAILQ_INIT(&helpwin.head);
1964 base_map = &global_map;
1965 current_map = &global_map;
1966 load_default_keys();
1968 initscr();
1969 raw();
1970 noecho();
1972 nonl();
1973 intrflush(stdscr, FALSE);
1975 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1976 return 0;
1977 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1978 return 0;
1979 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1980 return 0;
1981 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1982 return 0;
1983 if ((help = newwin(1, 1, 1, 0)) == NULL)
1984 return 0;
1986 body_lines = LINES-3;
1987 body_cols = COLS;
1989 keypad(body, TRUE);
1990 scrollok(body, TRUE);
1992 /* non-blocking input */
1993 wtimeout(body, 0);
1995 mvwprintw(body, 0, 0, "");
1997 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1998 event_add(&stdioev, NULL);
2000 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2001 signal_add(&winchev, NULL);
2003 load_last_session(session_new_tab_cb);
2004 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2005 new_tab(url);
2007 return 1;
2010 void
2011 ui_on_tab_loaded(struct tab *tab)
2013 stop_loading_anim(tab);
2014 message("Loaded %s", tab->hist_cur->h);
2016 redraw_tabline();
2017 wrefresh(tabline);
2018 if (in_minibuffer)
2019 wrefresh(minibuf);
2020 else
2021 wrefresh(body);
2024 void
2025 ui_on_tab_refresh(struct tab *tab)
2027 wrap_page(&tab->window, body_cols);
2028 if (tab->flags & TAB_CURRENT) {
2029 restore_cursor(&tab->window);
2030 redraw_tab(tab);
2034 void
2035 ui_require_input(struct tab *tab, int hide)
2037 /* TODO: hard-switching to another tab is ugly */
2038 switch_to_tab(tab);
2040 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2041 &ir_history);
2042 strlcpy(ministate.prompt, "Input required: ",
2043 sizeof(ministate.prompt));
2044 redraw_tab(tab);
2047 void
2048 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2049 unsigned int data)
2051 size_t len;
2053 if (in_minibuffer) {
2054 fn(0, data);
2055 return;
2058 yornp_cb = fn;
2059 yornp_data = data;
2060 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2061 yornp_abort, NULL);
2063 len = sizeof(ministate.prompt);
2064 strlcpy(ministate.prompt, prompt, len);
2065 strlcat(ministate.prompt, " (y or n) ", len);
2066 redraw_tab(current_tab());
2069 void
2070 ui_notify(const char *fmt, ...)
2072 va_list ap;
2074 va_start(ap, fmt);
2075 vmessage(fmt, ap);
2076 va_end(ap);
2079 void
2080 ui_end(void)
2082 endwin();