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 size_t len;
451 len = utf8_cplen(ministate.buf);
452 if (++window->cpoff > len)
453 window->cpoff = len;
454 restore_cursor(window);
457 static void
458 cmd_backward_paragraph(struct window *window)
460 do {
461 if (window->current_line == NULL ||
462 window->current_line == TAILQ_FIRST(&window->head)) {
463 message("No previous paragraph");
464 return;
466 cmd_previous_line(window);
467 } while (window->current_line->line != NULL ||
468 window->current_line->parent->type != LINE_TEXT);
471 static void
472 cmd_forward_paragraph(struct window *window)
474 do {
475 if (window->current_line == NULL ||
476 window->current_line == TAILQ_LAST(&window->head, vhead)) {
477 message("No next paragraph");
478 return;
480 cmd_next_line(window);
481 } while (window->current_line->line != NULL ||
482 window->current_line->parent->type != LINE_TEXT);
485 static void
486 cmd_move_beginning_of_line(struct window *window)
488 window->cpoff = 0;
489 restore_cursor(window);
492 static void
493 cmd_move_end_of_line(struct window *window)
495 struct vline *vl;
497 vl = window->current_line;
498 if (vl->line == NULL)
499 return;
500 window->cpoff = utf8_cplen(vl->line);
501 restore_cursor(window);
504 static void
505 cmd_redraw(struct window *window)
507 handle_resize(0, 0, NULL);
510 static void
511 cmd_scroll_line_up(struct window *window)
513 struct vline *vl;
515 if (window->line_off == 0)
516 return;
518 vl = nth_line(window, --window->line_off);
519 wscrl(body, -1);
520 wmove(body, 0, 0);
521 print_vline(body, vl);
523 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
524 restore_cursor(window);
527 static void
528 cmd_scroll_line_down(struct window *window)
530 struct vline *vl;
532 vl = window->current_line;
533 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
534 return;
535 window->current_line = vl;
537 window->line_off++;
538 wscrl(body, 1);
540 if (window->line_max - window->line_off < (size_t)body_lines)
541 return;
543 vl = nth_line(window, window->line_off + body_lines-1);
544 wmove(body, body_lines-1, 0);
545 print_vline(body, vl);
547 restore_cursor(window);
550 static void
551 cmd_scroll_up(struct window *window)
553 size_t off;
555 off = body_lines-1;
557 for (; off > 0; --off)
558 cmd_scroll_line_up(window);
561 static void
562 cmd_scroll_down(struct window *window)
564 size_t off;
566 off = body_lines-1;
568 for (; off > 0; --off)
569 cmd_scroll_line_down(window);
572 static void
573 cmd_beginning_of_buffer(struct window *window)
575 window->current_line = TAILQ_FIRST(&window->head);
576 window->line_off = 0;
577 window->curs_y = 0;
578 window->cpoff = 0;
579 restore_cursor(window);
582 static void
583 cmd_end_of_buffer(struct window *window)
585 ssize_t off;
587 off = window->line_max - body_lines;
588 off = MAX(0, off);
590 window->line_off = off;
591 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
593 window->current_line = TAILQ_LAST(&window->head, vhead);
594 window->cpoff = body_cols;
595 restore_cursor(window);
598 static void
599 cmd_kill_telescope(struct window *window)
601 save_session();
602 event_loopbreak();
605 static void
606 cmd_push_button(struct window *window)
608 struct vline *vl;
609 size_t nth;
611 nth = window->line_off + window->curs_y;
612 if (nth >= window->line_max)
613 return;
614 vl = nth_line(window, nth);
615 if (vl->parent->type != LINE_LINK)
616 return;
618 load_url_in_tab(current_tab(), vl->parent->alt);
621 static void
622 cmd_push_button_new_tab(struct window *window)
624 struct vline *vl;
625 size_t nth;
627 nth = window->line_off + window->curs_y;
628 if (nth > window->line_max)
629 return;
630 vl = nth_line(window, nth);
631 if (vl->parent->type != LINE_LINK)
632 return;
634 new_tab(vl->parent->alt);
637 static void
638 cmd_previous_button(struct window *window)
640 do {
641 if (window->current_line == NULL ||
642 window->current_line == TAILQ_FIRST(&window->head)) {
643 message("No previous link");
644 return;
646 cmd_previous_line(window);
647 } while (window->current_line->parent->type != LINE_LINK);
650 static void
651 cmd_next_button(struct window *window)
653 do {
654 if (window->current_line == NULL ||
655 window->current_line == TAILQ_LAST(&window->head, vhead)) {
656 message("No next link");
657 return;
659 cmd_next_line(window);
660 } while (window->current_line->parent->type != LINE_LINK);
663 static void
664 cmd_previous_page(struct window *window)
666 struct tab *tab = current_tab();
668 if (!load_previous_page(tab))
669 message("No previous page");
670 else
671 start_loading_anim(tab);
674 static void
675 cmd_next_page(struct window *window)
677 struct tab *tab = current_tab();
679 if (!load_next_page(tab))
680 message("No next page");
681 else
682 start_loading_anim(tab);
685 static void
686 cmd_clear_minibuf(struct window *window)
688 handle_clear_minibuf(0, 0, NULL);
691 static void
692 cmd_execute_extended_command(struct window *window)
694 size_t len;
696 if (in_minibuffer) {
697 message("We don't have enable-recursive-minibuffers");
698 return;
701 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
702 &eecmd_history);
704 len = sizeof(ministate.prompt);
705 strlcpy(ministate.prompt, "", len);
707 if (thiskey.meta)
708 strlcat(ministate.prompt, "M-", len);
710 strlcat(ministate.prompt, keyname(thiskey.key), len);
711 strlcat(ministate.prompt, " ", len);
714 static void
715 cmd_tab_close(struct window *window)
717 struct tab *tab, *t;
719 tab = current_tab();
720 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
721 TAILQ_NEXT(tab, tabs) == NULL) {
722 message("Can't close the only tab.");
723 return;
726 if (evtimer_pending(&tab->loadingev, NULL))
727 evtimer_del(&tab->loadingev);
729 stop_tab(tab);
731 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
732 t = TAILQ_NEXT(tab, tabs);
733 TAILQ_REMOVE(&tabshead, tab, tabs);
734 free(tab);
736 switch_to_tab(t);
739 static void
740 cmd_tab_close_other(struct window *window)
742 struct tab *t, *i;
744 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
745 if (t->flags & TAB_CURRENT)
746 continue;
748 stop_tab(t);
749 TAILQ_REMOVE(&tabshead, t, tabs);
750 free(t);
754 static void
755 cmd_tab_new(struct window *window)
757 new_tab(NEW_TAB_URL);
760 static void
761 cmd_tab_next(struct window *window)
763 struct tab *tab, *t;
765 tab = current_tab();
766 tab->flags &= ~TAB_CURRENT;
768 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
769 t = TAILQ_FIRST(&tabshead);
770 t->flags |= TAB_CURRENT;
773 static void
774 cmd_tab_previous(struct window *window)
776 struct tab *tab, *t;
778 tab = current_tab();
779 tab->flags &= ~TAB_CURRENT;
781 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
782 t = TAILQ_LAST(&tabshead, tabshead);
783 t->flags |= TAB_CURRENT;
786 static void
787 cmd_tab_move(struct window *window)
789 struct tab *tab, *t;
791 tab = current_tab();
792 t = TAILQ_NEXT(tab, tabs);
793 TAILQ_REMOVE(&tabshead, tab, tabs);
795 if (t == NULL)
796 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
797 else
798 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
801 static void
802 cmd_tab_move_to(struct window *window)
804 struct tab *tab, *t;
806 tab = current_tab();
807 t = TAILQ_PREV(tab, tabshead, tabs);
808 TAILQ_REMOVE(&tabshead, tab, tabs);
810 if (t == NULL) {
811 if (TAILQ_EMPTY(&tabshead))
812 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
813 else
814 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
815 } else
816 TAILQ_INSERT_BEFORE(t, tab, tabs);
819 static void
820 cmd_load_url(struct window *window)
822 if (in_minibuffer) {
823 message("We don't have enable-recursive-minibuffers");
824 return;
827 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
828 &lu_history);
829 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
830 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
831 cmd_move_end_of_line(&ministate.window);
834 static void
835 cmd_load_current_url(struct window *window)
837 struct tab *tab = current_tab();
839 if (in_minibuffer) {
840 message("We don't have enable-recursive-minibuffers");
841 return;
844 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
845 &lu_history);
846 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
847 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
848 ministate.window.cpoff = utf8_cplen(ministate.buf);
851 static void
852 cmd_bookmark_page(struct window *window)
854 struct tab *tab = current_tab();
856 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
857 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
858 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
859 ministate.window.cpoff = utf8_cplen(ministate.buf);
862 static void
863 cmd_list_bookmarks(struct window *window)
865 load_url_in_tab(current_tab(), "about:bookmarks");
868 static void
869 cmd_toggle_help(struct window *window)
871 side_window = !side_window;
872 if (side_window)
873 recompute_help();
875 /* ugly hack, but otherwise the window doesn't get updated
876 * until I call handle_resize a second time (i.e. C-l). I
877 * will be happy to know why something like this is needed. */
878 handle_resize(0, 0, NULL);
879 handle_resize(0, 0, NULL);
882 static void
883 cmd_mini_delete_char(struct window *window)
885 char *c, *n;
887 if (!in_minibuffer) {
888 message("text is read-only");
889 return;
892 minibuffer_taint_hist();
894 c = utf8_nth(window->current_line->line, window->cpoff);
895 if (*c == '\0')
896 return;
897 n = utf8_next_cp(c);
899 memmove(c, n, strlen(n)+1);
902 static void
903 cmd_mini_delete_backward_char(struct window *window)
905 char *c, *p, *start;
907 if (!in_minibuffer) {
908 message("text is read-only");
909 return;
912 minibuffer_taint_hist();
914 c = utf8_nth(window->current_line->line, window->cpoff);
915 start = window->current_line->line;
916 if (c == start)
917 return;
918 p = utf8_prev_cp(c-1, start);
920 memmove(p, c, strlen(c)+1);
921 window->cpoff--;
924 static void
925 cmd_mini_kill_line(struct window *window)
927 char *c;
929 if (!in_minibuffer) {
930 message("text is read-only");
931 return;
934 minibuffer_taint_hist();
935 c = utf8_nth(window->current_line->line, window->cpoff);
936 *c = '\0';
939 static void
940 cmd_mini_abort(struct window *window)
942 if (!in_minibuffer)
943 return;
945 ministate.abortfn();
948 static void
949 cmd_mini_complete_and_exit(struct window *window)
951 if (!in_minibuffer)
952 return;
954 minibuffer_taint_hist();
955 ministate.donefn();
958 static void
959 cmd_mini_previous_history_element(struct window *window)
961 if (ministate.history == NULL) {
962 message("No history");
963 return;
966 if (ministate.hist_cur == NULL ||
967 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
968 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
969 ministate.hist_off = ministate.history->len - 1;
970 if (ministate.hist_cur == NULL)
971 message("No prev item");
972 } else {
973 ministate.hist_off--;
976 if (ministate.hist_cur != NULL)
977 window->current_line->line = ministate.hist_cur->h;
980 static void
981 cmd_mini_next_history_element(struct window *window)
983 if (ministate.history == NULL) {
984 message("No history");
985 return;
988 if (ministate.hist_cur == NULL ||
989 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
990 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
991 ministate.hist_off = 0;
992 if (ministate.hist_cur == NULL)
993 message("No next item");
994 } else {
995 ministate.hist_off++;
998 if (ministate.hist_cur != NULL)
999 window->current_line->line = ministate.hist_cur->h;
1002 static void
1003 global_key_unbound(void)
1005 message("%s is undefined", keybuf);
1008 static void
1009 minibuffer_hist_save_entry(void)
1011 struct hist *hist;
1013 if (ministate.history == NULL)
1014 return;
1016 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1017 abort();
1019 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1021 if (TAILQ_EMPTY(&ministate.history->head))
1022 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1023 else
1024 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1025 ministate.history->len++;
1029 * taint the minibuffer cache: if we're currently showing a history
1030 * element, copy that to the current buf and reset the "history
1031 * navigation" thing.
1033 static void
1034 minibuffer_taint_hist(void)
1036 if (ministate.hist_cur == NULL)
1037 return;
1039 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1040 ministate.hist_cur = NULL;
1043 static void
1044 minibuffer_self_insert(void)
1046 char *c, tmp[5] = {0};
1047 size_t len;
1049 minibuffer_taint_hist();
1051 if (thiskey.cp == 0)
1052 return;
1054 len = utf8_encode(thiskey.cp, tmp);
1055 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1056 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1057 return;
1059 memmove(c + len, c, strlen(c)+1);
1060 memcpy(c, tmp, len);
1061 ministate.window.cpoff++;
1064 static void
1065 eecmd_self_insert(void)
1067 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1068 !unicode_isgraph(thiskey.cp)) {
1069 global_key_unbound();
1070 return;
1073 minibuffer_self_insert();
1076 static void
1077 eecmd_select(void)
1079 struct cmds *cmd;
1081 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1082 if (!strcmp(cmd->cmd, ministate.buf)) {
1083 exit_minibuffer();
1084 minibuffer_hist_save_entry();
1085 cmd->fn(current_window());
1086 return;
1090 message("No match");
1093 static void
1094 ir_self_insert(void)
1096 minibuffer_self_insert();
1099 static void
1100 ir_select(void)
1102 char buf[1025] = {0};
1103 struct phos_uri uri;
1104 struct tab *tab;
1106 tab = current_tab();
1108 exit_minibuffer();
1109 minibuffer_hist_save_entry();
1111 /* a bit ugly but... */
1112 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1113 phos_uri_set_query(&uri, ministate.buf);
1114 phos_serialize_uri(&uri, buf, sizeof(buf));
1115 load_url_in_tab(tab, buf);
1118 static void
1119 lu_self_insert(void)
1121 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1122 !unicode_isgraph(thiskey.key)) {
1123 global_key_unbound();
1124 return;
1127 minibuffer_self_insert();
1130 static void
1131 lu_select(void)
1133 exit_minibuffer();
1134 minibuffer_hist_save_entry();
1135 load_url_in_tab(current_tab(), ministate.buf);
1138 static void
1139 bp_select(void)
1141 exit_minibuffer();
1142 if (*ministate.buf != '\0')
1143 add_to_bookmarks(ministate.buf);
1144 else
1145 message("Abort.");
1148 static void
1149 yornp_self_insert(void)
1151 if (thiskey.key != 'y' && thiskey.key != 'n') {
1152 message("Please answer y or n");
1153 return;
1156 exit_minibuffer();
1157 yornp_cb(thiskey.key == 'y', yornp_data);
1160 static void
1161 yornp_abort(void)
1163 exit_minibuffer();
1164 yornp_cb(0, yornp_data);
1167 static struct vline *
1168 nth_line(struct window *window, size_t n)
1170 struct vline *vl;
1171 size_t i;
1173 i = 0;
1174 TAILQ_FOREACH(vl, &window->head, vlines) {
1175 if (i == n)
1176 return vl;
1177 i++;
1180 /* unreachable */
1181 abort();
1184 static struct tab *
1185 current_tab(void)
1187 struct tab *t;
1189 TAILQ_FOREACH(t, &tabshead, tabs) {
1190 if (t->flags & TAB_CURRENT)
1191 return t;
1194 /* unreachable */
1195 abort();
1198 static struct window *
1199 current_window(void)
1201 if (in_minibuffer)
1202 return &ministate.window;
1203 return &current_tab()->window;
1206 static int
1207 readkey(void)
1209 uint32_t state = 0;
1211 if ((thiskey.key = wgetch(body)) == ERR)
1212 return 0;
1214 thiskey.meta = thiskey.key == 27;
1215 if (thiskey.meta) {
1216 thiskey.key = wgetch(body);
1217 if (thiskey.key == ERR || thiskey.key == 27) {
1218 thiskey.meta = 0;
1219 thiskey.key = 27;
1223 thiskey.cp = 0;
1224 if ((unsigned int)thiskey.key < UINT8_MAX) {
1225 while (1) {
1226 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1227 break;
1228 if ((thiskey.key = wgetch(body)) == ERR) {
1229 message("Error decoding user input");
1230 return 0;
1235 return 1;
1238 static void
1239 dispatch_stdio(int fd, short ev, void *d)
1241 struct keymap *k;
1242 const char *keyname;
1243 char tmp[5] = {0};
1245 if (!readkey())
1246 return;
1248 if (keybuf[0] != '\0')
1249 strlcat(keybuf, " ", sizeof(keybuf));
1250 if (thiskey.meta)
1251 strlcat(keybuf, "M-", sizeof(keybuf));
1252 if (thiskey.cp != 0) {
1253 utf8_encode(thiskey.cp, tmp);
1254 strlcat(keybuf, tmp, sizeof(keybuf));
1255 } else {
1256 if ((keyname = unkbd(thiskey.key)) != NULL)
1257 strlcat(keybuf, keyname, sizeof(keybuf));
1258 else {
1259 tmp[0] = thiskey.key;
1260 strlcat(keybuf, tmp, sizeof(keybuf));
1264 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1265 if (k->meta == thiskey.meta &&
1266 k->key == thiskey.key) {
1267 if (k->fn == NULL)
1268 current_map = &k->map;
1269 else {
1270 current_map = base_map;
1271 strlcpy(keybuf, "", sizeof(keybuf));
1272 k->fn(current_window());
1274 goto done;
1278 if (current_map->unhandled_input != NULL)
1279 current_map->unhandled_input();
1280 else {
1281 global_key_unbound();
1284 strlcpy(keybuf, "", sizeof(keybuf));
1285 current_map = base_map;
1287 done:
1288 if (side_window)
1289 recompute_help();
1291 redraw_tab(current_tab());
1294 static void
1295 handle_clear_minibuf(int fd, short ev, void *d)
1297 free(ministate.curmesg);
1298 ministate.curmesg = NULL;
1300 redraw_minibuffer();
1301 if (in_minibuffer) {
1302 wrefresh(body);
1303 wrefresh(minibuf);
1304 } else {
1305 wrefresh(minibuf);
1306 wrefresh(body);
1310 static void
1311 handle_resize(int sig, short ev, void *d)
1313 struct tab *tab;
1315 endwin();
1316 refresh();
1317 clear();
1319 /* move and resize the windows, in reverse order! */
1321 mvwin(minibuf, LINES-1, 0);
1322 wresize(minibuf, 1, COLS);
1324 mvwin(modeline, LINES-2, 0);
1325 wresize(modeline, 1, COLS);
1327 body_lines = LINES-3;
1328 body_cols = COLS;
1330 if (side_window) {
1331 help_cols = 0.3 * COLS;
1332 help_lines = LINES-3;
1333 mvwin(help, 1, 0);
1334 wresize(help, help_lines, help_cols);
1336 wrap_page(&helpwin, help_cols);
1338 body_cols = COLS - help_cols - 1;
1339 mvwin(body, 1, help_cols);
1340 } else
1341 mvwin(body, 1, 0);
1343 wresize(body, body_lines, body_cols);
1345 wresize(tabline, 1, COLS);
1347 tab = current_tab();
1349 wrap_page(&tab->window, body_cols);
1350 redraw_tab(tab);
1353 static int
1354 wrap_page(struct window *window, int width)
1356 struct line *l;
1357 const struct line *orig;
1358 struct vline *vl;
1359 const char *prfx;
1361 orig = window->current_line == NULL
1362 ? NULL
1363 : window->current_line->parent;
1364 window->current_line = NULL;
1366 window->curs_y = 0;
1367 window->line_off = 0;
1369 empty_vlist(window);
1371 TAILQ_FOREACH(l, &window->page.head, lines) {
1372 prfx = line_prefixes[l->type].prfx1;
1373 switch (l->type) {
1374 case LINE_TEXT:
1375 case LINE_LINK:
1376 case LINE_TITLE_1:
1377 case LINE_TITLE_2:
1378 case LINE_TITLE_3:
1379 case LINE_ITEM:
1380 case LINE_QUOTE:
1381 case LINE_PRE_START:
1382 case LINE_PRE_END:
1383 wrap_text(window, prfx, l, width);
1384 break;
1385 case LINE_PRE_CONTENT:
1386 hardwrap_text(window, l, width);
1387 break;
1390 if (orig == l && window->current_line == NULL) {
1391 window->line_off = window->line_max-1;
1392 window->current_line = TAILQ_LAST(&window->head, vhead);
1394 while (1) {
1395 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1396 if (vl == NULL || vl->parent != orig)
1397 break;
1398 window->current_line = vl;
1399 window->line_off--;
1404 if (window->current_line == NULL)
1405 window->current_line = TAILQ_FIRST(&window->head);
1407 return 1;
1410 static void
1411 print_vline(WINDOW *window, struct vline *vl)
1413 const char *text = vl->line;
1414 const char *prfx;
1415 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1416 int text_face = line_faces[vl->parent->type].text_prop;
1418 if (!vl->flags)
1419 prfx = line_prefixes[vl->parent->type].prfx1;
1420 else
1421 prfx = line_prefixes[vl->parent->type].prfx2;
1423 if (text == NULL)
1424 text = "";
1426 wattron(window, prefix_face);
1427 wprintw(window, "%s", prfx);
1428 wattroff(window, prefix_face);
1430 wattron(window, text_face);
1431 wprintw(window, "%s", text);
1432 wattroff(window, text_face);
1435 static void
1436 redraw_tabline(void)
1438 struct tab *tab;
1439 size_t toskip, ots, tabwidth, space, x;
1440 int current, y, truncated;
1441 const char *title;
1442 char buf[25];
1444 tabwidth = sizeof(buf)+1;
1445 space = COLS-2;
1447 toskip = 0;
1448 TAILQ_FOREACH(tab, &tabshead, tabs) {
1449 toskip++;
1450 if (tab->flags & TAB_CURRENT)
1451 break;
1454 if (toskip * tabwidth < space)
1455 toskip = 0;
1456 else {
1457 ots = toskip;
1458 toskip--;
1459 while (toskip != 0 &&
1460 (ots - toskip+1) * tabwidth < space)
1461 toskip--;
1464 werase(tabline);
1465 wattron(tabline, tab_face.background);
1466 wprintw(tabline, toskip == 0 ? " " : "<");
1467 wattroff(tabline, tab_face.background);
1469 truncated = 0;
1470 TAILQ_FOREACH(tab, &tabshead, tabs) {
1471 if (truncated)
1472 break;
1473 if (toskip != 0) {
1474 toskip--;
1475 continue;
1478 getyx(tabline, y, x);
1479 if (x + sizeof(buf)+2 >= (size_t)COLS)
1480 truncated = 1;
1482 current = tab->flags & TAB_CURRENT;
1484 if (*(title = tab->window.page.title) == '\0')
1485 title = tab->hist_cur->h;
1487 strlcpy(buf, " ", sizeof(buf));
1488 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1489 /* truncation happens */
1490 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1491 } else {
1492 /* pad with spaces */
1493 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1494 /* nop */ ;
1497 if (current)
1498 wattron(tabline, tab_face.current_tab);
1499 else
1500 wattron(tabline, tab_face.tab);
1502 wprintw(tabline, "%s", buf);
1503 if (TAILQ_NEXT(tab, tabs) != NULL)
1504 wprintw(tabline, " ");
1506 if (current)
1507 wattroff(tabline, tab_face.current_tab);
1508 else
1509 wattroff(tabline, tab_face.tab);
1512 wattron(tabline, tab_face.background);
1513 for (; x < (size_t)COLS; ++x)
1514 waddch(tabline, ' ');
1515 if (truncated)
1516 mvwprintw(tabline, 0, COLS-1, ">");
1519 static void
1520 redraw_window(WINDOW *win, int height, struct window *window)
1522 struct vline *vl;
1523 int l;
1525 werase(win);
1527 window->line_off = MIN(window->line_max-1, window->line_off);
1528 if (TAILQ_EMPTY(&window->head))
1529 return;
1531 l = 0;
1532 vl = nth_line(window, window->line_off);
1533 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1534 wmove(win, l, 0);
1535 print_vline(win, vl);
1536 l++;
1537 if (l == height)
1538 break;
1541 wmove(win, window->curs_y, window->curs_x);
1544 static void
1545 redraw_help(void)
1547 redraw_window(help, help_lines, &helpwin);
1550 static void
1551 redraw_body(struct tab *tab)
1553 redraw_window(body, body_lines, &tab->window);
1556 static inline char
1557 trust_status_char(enum trust_state ts)
1559 switch (ts) {
1560 case TS_UNKNOWN: return 'u';
1561 case TS_UNTRUSTED: return '!';
1562 case TS_TRUSTED: return 'v';
1563 case TS_VERIFIED: return 'V';
1567 static void
1568 redraw_modeline(struct tab *tab)
1570 double pct;
1571 int x, y, max_x, max_y;
1572 const char *mode = tab->window.page.name;
1573 const char *spin = "-\\|/";
1575 werase(modeline);
1576 wattron(modeline, A_REVERSE);
1577 wmove(modeline, 0, 0);
1579 wprintw(modeline, "-%c%c %s ",
1580 spin[tab->loading_anim_step],
1581 trust_status_char(tab->trust),
1582 mode == NULL ? "(none)" : mode);
1584 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1586 if (tab->window.line_max <= (size_t)body_lines)
1587 wprintw(modeline, "All ");
1588 else if (tab->window.line_off == 0)
1589 wprintw(modeline, "Top ");
1590 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1591 wprintw(modeline, "Bottom ");
1592 else
1593 wprintw(modeline, "%.0f%% ", pct);
1595 wprintw(modeline, "%d/%d %s ",
1596 tab->window.line_off + tab->window.curs_y,
1597 tab->window.line_max,
1598 tab->hist_cur->h);
1600 getyx(modeline, y, x);
1601 getmaxyx(modeline, max_y, max_x);
1603 (void)y;
1604 (void)max_y;
1606 for (; x < max_x; ++x)
1607 waddstr(modeline, "-");
1610 static void
1611 redraw_minibuffer(void)
1613 struct tab *tab;
1614 size_t off_y, off_x = 0;
1615 char *start, *c;
1617 werase(minibuf);
1619 if (in_minibuffer) {
1620 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1621 if (ministate.hist_cur != NULL)
1622 wprintw(minibuf, "(%zu/%zu) ",
1623 ministate.hist_off + 1,
1624 ministate.history->len);
1626 getyx(minibuf, off_y, off_x);
1628 start = ministate.hist_cur != NULL
1629 ? ministate.hist_cur->h
1630 : ministate.buf;
1631 c = utf8_nth(ministate.window.current_line->line,
1632 ministate.window.cpoff);
1633 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1634 start = utf8_next_cp(start);
1637 waddstr(minibuf, start);
1640 if (ministate.curmesg != NULL)
1641 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1642 ministate.curmesg);
1644 if (!in_minibuffer && ministate.curmesg == NULL)
1645 waddstr(minibuf, keybuf);
1647 /* If nothing else, show the URL at point */
1648 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1649 tab = current_tab();
1650 if (tab->window.current_line != NULL &&
1651 tab->window.current_line->parent->type == LINE_LINK)
1652 waddstr(minibuf, tab->window.current_line->parent->alt);
1655 if (in_minibuffer)
1656 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1659 static void
1660 redraw_tab(struct tab *tab)
1662 if (side_window) {
1663 redraw_help();
1664 wnoutrefresh(help);
1667 redraw_tabline();
1668 redraw_body(tab);
1669 redraw_modeline(tab);
1670 redraw_minibuffer();
1672 wnoutrefresh(tabline);
1673 wnoutrefresh(modeline);
1675 if (in_minibuffer) {
1676 wnoutrefresh(body);
1677 wnoutrefresh(minibuf);
1678 } else {
1679 wnoutrefresh(minibuf);
1680 wnoutrefresh(body);
1683 doupdate();
1686 static void
1687 emit_help_item(char *prfx, void *fn)
1689 struct line *l;
1690 struct cmds *cmd;
1692 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1693 if (fn == cmd->fn)
1694 break;
1696 assert(cmd != NULL);
1698 if ((l = calloc(1, sizeof(*l))) == NULL)
1699 abort();
1701 l->type = LINE_TEXT;
1702 l->alt = NULL;
1704 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1706 if (TAILQ_EMPTY(&helpwin.page.head))
1707 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1708 else
1709 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1712 static void
1713 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1715 struct keymap *k;
1716 char p[32];
1717 const char *kn;
1719 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1720 strlcpy(p, prfx, sizeof(p));
1721 if (*p != '\0')
1722 strlcat(p, " ", sizeof(p));
1723 if (k->meta)
1724 strlcat(p, "M-", sizeof(p));
1725 if ((kn = unkbd(k->key)) != NULL)
1726 strlcat(p, kn, sizeof(p));
1727 else
1728 strlcat(p, keyname(k->key), sizeof(p));
1730 if (k->fn == NULL)
1731 rec_compute_help(&k->map, p, sizeof(p));
1732 else
1733 emit_help_item(p, k->fn);
1737 static void
1738 recompute_help(void)
1740 char p[32] = { 0 };
1742 empty_vlist(&helpwin);
1743 empty_linelist(&helpwin);
1744 rec_compute_help(current_map, p, sizeof(p));
1745 wrap_page(&helpwin, help_cols);
1748 static void
1749 vmessage(const char *fmt, va_list ap)
1751 if (evtimer_pending(&clminibufev, NULL))
1752 evtimer_del(&clminibufev);
1753 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1754 evtimer_add(&clminibufev, &clminibufev_timer);
1756 free(ministate.curmesg);
1758 /* TODO: what to do if the allocation fails here? */
1759 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1760 ministate.curmesg = NULL;
1762 redraw_minibuffer();
1763 if (in_minibuffer) {
1764 wrefresh(body);
1765 wrefresh(minibuf);
1766 } else {
1767 wrefresh(minibuf);
1768 wrefresh(body);
1772 static void
1773 message(const char *fmt, ...)
1775 va_list ap;
1777 va_start(ap, fmt);
1778 vmessage(fmt, ap);
1779 va_end(ap);
1782 static void
1783 start_loading_anim(struct tab *tab)
1785 if (tab->loading_anim)
1786 return;
1787 tab->loading_anim = 1;
1788 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1789 evtimer_add(&tab->loadingev, &loadingev_timer);
1792 static void
1793 update_loading_anim(int fd, short ev, void *d)
1795 struct tab *tab = d;
1797 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1799 if (tab->flags & TAB_CURRENT) {
1800 redraw_modeline(tab);
1801 wrefresh(modeline);
1802 wrefresh(body);
1803 if (in_minibuffer)
1804 wrefresh(minibuf);
1807 evtimer_add(&tab->loadingev, &loadingev_timer);
1810 static void
1811 stop_loading_anim(struct tab *tab)
1813 if (!tab->loading_anim)
1814 return;
1815 evtimer_del(&tab->loadingev);
1816 tab->loading_anim = 0;
1817 tab->loading_anim_step = 0;
1819 if (!(tab->flags & TAB_CURRENT))
1820 return;
1822 redraw_modeline(tab);
1824 wrefresh(modeline);
1825 wrefresh(body);
1826 if (in_minibuffer)
1827 wrefresh(minibuf);
1830 static void
1831 load_url_in_tab(struct tab *tab, const char *url)
1833 message("Loading %s...", url);
1834 start_loading_anim(tab);
1835 load_url(tab, url);
1837 tab->window.curs_x = 0;
1838 tab->window.curs_y = 0;
1839 redraw_tab(tab);
1842 static void
1843 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1844 void (*abortfn)(void), struct histhead *hist)
1846 in_minibuffer = 1;
1847 base_map = &minibuffer_map;
1848 current_map = &minibuffer_map;
1850 base_map->unhandled_input = self_insert_fn;
1852 ministate.donefn = donefn;
1853 ministate.abortfn = abortfn;
1854 memset(ministate.buf, 0, sizeof(ministate.buf));
1855 ministate.window.current_line = &ministate.vline;
1856 ministate.window.current_line->line = ministate.buf;
1857 ministate.window.cpoff = 0;
1858 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1860 ministate.history = hist;
1861 ministate.hist_cur = NULL;
1862 ministate.hist_off = 0;
1865 static void
1866 exit_minibuffer(void)
1868 werase(minibuf);
1870 in_minibuffer = 0;
1871 base_map = &global_map;
1872 current_map = &global_map;
1875 static void
1876 switch_to_tab(struct tab *tab)
1878 struct tab *t;
1880 TAILQ_FOREACH(t, &tabshead, tabs) {
1881 t->flags &= ~TAB_CURRENT;
1884 tab->flags |= TAB_CURRENT;
1887 unsigned int
1888 tab_new_id(void)
1890 return tab_counter++;
1893 static struct tab *
1894 new_tab(const char *url)
1896 struct tab *tab;
1898 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1899 event_loopbreak();
1900 return NULL;
1903 TAILQ_INIT(&tab->hist.head);
1905 TAILQ_INIT(&tab->window.head);
1907 tab->id = tab_new_id();
1908 switch_to_tab(tab);
1910 if (TAILQ_EMPTY(&tabshead))
1911 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1912 else
1913 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1915 load_url_in_tab(tab, url);
1916 return tab;
1919 static void
1920 session_new_tab_cb(const char *url)
1922 new_tab(url);
1925 static void
1926 usage(void)
1928 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1931 int
1932 ui_init(int argc, char * const *argv)
1934 const char *url = NEW_TAB_URL;
1935 int ch;
1937 while ((ch = getopt(argc, argv, "")) != -1) {
1938 switch (ch) {
1939 default:
1940 usage();
1941 return 0;
1944 argc -= optind;
1945 argv += optind;
1947 if (argc != 0)
1948 url = argv[0];
1950 setlocale(LC_ALL, "");
1952 TAILQ_INIT(&global_map.m);
1953 global_map.unhandled_input = global_key_unbound;
1955 TAILQ_INIT(&minibuffer_map.m);
1957 TAILQ_INIT(&eecmd_history.head);
1958 TAILQ_INIT(&ir_history.head);
1959 TAILQ_INIT(&lu_history.head);
1961 ministate.line.type = LINE_TEXT;
1962 ministate.vline.parent = &ministate.line;
1963 ministate.window.current_line = &ministate.vline;
1965 /* initialize help window */
1966 TAILQ_INIT(&helpwin.head);
1968 base_map = &global_map;
1969 current_map = &global_map;
1970 load_default_keys();
1972 initscr();
1973 raw();
1974 noecho();
1976 nonl();
1977 intrflush(stdscr, FALSE);
1979 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1980 return 0;
1981 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1982 return 0;
1983 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1984 return 0;
1985 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1986 return 0;
1987 if ((help = newwin(1, 1, 1, 0)) == NULL)
1988 return 0;
1990 body_lines = LINES-3;
1991 body_cols = COLS;
1993 keypad(body, TRUE);
1994 scrollok(body, TRUE);
1996 /* non-blocking input */
1997 wtimeout(body, 0);
1999 mvwprintw(body, 0, 0, "");
2001 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2002 event_add(&stdioev, NULL);
2004 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2005 signal_add(&winchev, NULL);
2007 load_last_session(session_new_tab_cb);
2008 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2009 new_tab(url);
2011 return 1;
2014 void
2015 ui_on_tab_loaded(struct tab *tab)
2017 stop_loading_anim(tab);
2018 message("Loaded %s", tab->hist_cur->h);
2020 redraw_tabline();
2021 wrefresh(tabline);
2022 if (in_minibuffer)
2023 wrefresh(minibuf);
2024 else
2025 wrefresh(body);
2028 void
2029 ui_on_tab_refresh(struct tab *tab)
2031 wrap_page(&tab->window, body_cols);
2032 if (tab->flags & TAB_CURRENT) {
2033 restore_cursor(&tab->window);
2034 redraw_tab(tab);
2038 void
2039 ui_require_input(struct tab *tab, int hide)
2041 /* TODO: hard-switching to another tab is ugly */
2042 switch_to_tab(tab);
2044 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2045 &ir_history);
2046 strlcpy(ministate.prompt, "Input required: ",
2047 sizeof(ministate.prompt));
2048 redraw_tab(tab);
2051 void
2052 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2053 unsigned int data)
2055 size_t len;
2057 if (in_minibuffer) {
2058 fn(0, data);
2059 return;
2062 yornp_cb = fn;
2063 yornp_data = data;
2064 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2065 yornp_abort, NULL);
2067 len = sizeof(ministate.prompt);
2068 strlcpy(ministate.prompt, prompt, len);
2069 strlcat(ministate.prompt, " (y or n) ", len);
2070 redraw_tab(current_tab());
2073 void
2074 ui_notify(const char *fmt, ...)
2076 va_list ap;
2078 va_start(ap, fmt);
2079 vmessage(fmt, ap);
2080 va_end(ap);
2083 void
2084 ui_end(void)
2086 endwin();