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
48 #define TAB_URGENT 0x2
50 #define NEW_TAB_URL "about:new"
52 static struct event stdioev, winchev;
54 static void load_default_keys(void);
55 static void restore_cursor(struct window*);
57 #define CMD(fnname) static void fnname(struct window *)
58 #define DEFALIAS(s, d) /* nothing */
60 CMD(cmd_previous_line);
61 CMD(cmd_next_line);
62 CMD(cmd_backward_char);
63 CMD(cmd_forward_char);
64 CMD(cmd_backward_paragraph);
65 CMD(cmd_forward_paragraph);
66 CMD(cmd_move_beginning_of_line);
67 CMD(cmd_move_end_of_line);
68 CMD(cmd_redraw);
69 CMD(cmd_scroll_line_down);
70 CMD(cmd_scroll_line_up);
71 CMD(cmd_scroll_up);
72 CMD(cmd_scroll_down);
73 CMD(cmd_beginning_of_buffer);
74 CMD(cmd_end_of_buffer);
76 CMD(cmd_kill_telescope);
77 DEFALIAS(q, cmd_kill_telescope)
78 DEFALIAS(wq, cmd_kill_telescope)
80 CMD(cmd_push_button);
81 CMD(cmd_push_button_new_tab);
82 CMD(cmd_previous_button);
83 CMD(cmd_next_button);
84 CMD(cmd_previous_page);
85 CMD(cmd_next_page);
86 CMD(cmd_clear_minibuf);
87 CMD(cmd_execute_extended_command);
88 CMD(cmd_tab_close);
89 CMD(cmd_tab_close_other);
91 CMD(cmd_tab_new);
92 DEFALIAS(tabnew, cmd_tab_new)
94 CMD(cmd_tab_next);
95 DEFALIAS(tabn, cmd_tab_next)
97 CMD(cmd_tab_previous);
98 DEFALIAS(tabp, cmd_tab_previous)
100 CMD(cmd_tab_move);
101 CMD(cmd_tab_move_to);
102 CMD(cmd_load_url);
103 CMD(cmd_load_current_url);
104 CMD(cmd_bookmark_page);
105 CMD(cmd_list_bookmarks);
106 CMD(cmd_toggle_help);
108 CMD(cmd_mini_delete_char);
109 CMD(cmd_mini_delete_backward_char);
110 CMD(cmd_mini_kill_line);
111 CMD(cmd_mini_abort);
112 CMD(cmd_mini_complete_and_exit);
113 CMD(cmd_mini_previous_history_element);
114 CMD(cmd_mini_next_history_element);
116 #include "cmd.gen.h"
118 static void global_key_unbound(void);
119 static void minibuffer_hist_save_entry(void);
120 static void minibuffer_taint_hist(void);
121 static void minibuffer_self_insert(void);
122 static void eecmd_self_insert(void);
123 static void eecmd_select(void);
124 static void ir_self_insert(void);
125 static void ir_select(void);
126 static void lu_self_insert(void);
127 static void lu_select(void);
128 static void bp_select(void);
129 static void yornp_self_insert(void);
130 static void yornp_abort(void);
132 static struct vline *nth_line(struct window*, size_t);
133 static struct tab *current_tab(void);
134 static struct window *current_window(void);
135 static int readkey(void);
136 static void dispatch_stdio(int, short, void*);
137 static void handle_clear_minibuf(int, short, void*);
138 static void handle_resize(int, short, void*);
139 static void handle_resize_timeout(int, short, void*);
140 static int wrap_page(struct window*, int);
141 static void print_vline(WINDOW*, struct vline*);
142 static void redraw_tabline(void);
143 static void redraw_window(WINDOW*, int, struct window*);
144 static void redraw_help(void);
145 static void redraw_body(struct tab*);
146 static void redraw_modeline(struct tab*);
147 static void redraw_minibuffer(void);
148 static void redraw_tab(struct tab*);
149 static void emit_help_item(char*, void*);
150 static void rec_compute_help(struct kmap*, char*, size_t);
151 static void recompute_help(void);
152 static void vmessage(const char*, va_list);
153 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
154 static void start_loading_anim(struct tab*);
155 static void update_loading_anim(int, short, void*);
156 static void stop_loading_anim(struct tab*);
157 static void load_url_in_tab(struct tab*, const char*);
158 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
159 static void exit_minibuffer(void);
160 static void switch_to_tab(struct tab*);
161 static struct tab *new_tab(const char*);
162 static void session_new_tab_cb(const char*);
163 static void usage(void);
165 static struct { short meta; int key; uint32_t cp; } thiskey;
167 static struct event resizeev;
168 static struct timeval resize_timer = { 0, 250000 };
170 static WINDOW *tabline, *body, *modeline, *minibuf;
171 static int body_lines, body_cols;
173 static WINDOW *help;
174 static struct window helpwin;
175 static int help_lines, help_cols;
177 static int side_window;
179 static struct event clminibufev;
180 static struct timeval clminibufev_timer = { 5, 0 };
181 static struct timeval loadingev_timer = { 0, 250000 };
183 static uint32_t tab_counter;
185 static char keybuf[64];
187 static void (*yornp_cb)(int, unsigned int);
188 static unsigned int yornp_data;
190 struct kmap global_map,
191 minibuffer_map,
192 *current_map,
193 *base_map;
195 static struct histhead eecmd_history,
196 ir_history,
197 lu_history;
199 static int in_minibuffer;
201 static struct {
202 char *curmesg;
204 char prompt[64];
205 void (*donefn)(void);
206 void (*abortfn)(void);
208 char buf[1025];
209 struct line line;
210 struct vline vline;
211 struct window window;
213 struct histhead *history;
214 struct hist *hist_cur;
215 size_t hist_off;
216 } ministate;
218 struct lineprefix {
219 const char *prfx1;
220 const char *prfx2;
221 } line_prefixes[] = {
222 [LINE_TEXT] = { "", "" },
223 [LINE_LINK] = { "=> ", " " },
224 [LINE_TITLE_1] = { "# ", " " },
225 [LINE_TITLE_2] = { "## ", " " },
226 [LINE_TITLE_3] = { "### ", " " },
227 [LINE_ITEM] = { "* ", " " },
228 [LINE_QUOTE] = { "> ", " " },
229 [LINE_PRE_START] = { "```", " " },
230 [LINE_PRE_CONTENT] = { "", "" },
231 [LINE_PRE_END] = { "```", "```" },
232 };
234 static struct line_face {
235 int prefix_prop;
236 int text_prop;
237 } line_faces[] = {
238 [LINE_TEXT] = { 0, 0 },
239 [LINE_LINK] = { 0, A_UNDERLINE },
240 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
241 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
242 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
243 [LINE_ITEM] = { 0, 0 },
244 [LINE_QUOTE] = { 0, A_DIM },
245 [LINE_PRE_START] = { 0, 0 },
246 [LINE_PRE_CONTENT] = { 0, 0 },
247 [LINE_PRE_END] = { 0, 0 },
248 };
250 static struct tab_face {
251 int background, tab, current_tab;
252 } tab_face = {
253 A_REVERSE, A_REVERSE, A_NORMAL
254 };
256 static inline void
257 global_set_key(const char *key, void (*fn)(struct window*))
259 if (!kmap_define_key(&global_map, key, fn))
260 _exit(1);
263 static inline void
264 minibuffer_set_key(const char *key, void (*fn)(struct window*))
266 if (!kmap_define_key(&minibuffer_map, key, fn))
267 _exit(1);
270 static void
271 load_default_keys(void)
273 /* === global map === */
275 /* emacs */
276 global_set_key("C-p", cmd_previous_line);
277 global_set_key("C-n", cmd_next_line);
278 global_set_key("C-f", cmd_forward_char);
279 global_set_key("C-b", cmd_backward_char);
280 global_set_key("M-{", cmd_backward_paragraph);
281 global_set_key("M-}", cmd_forward_paragraph);
282 global_set_key("C-a", cmd_move_beginning_of_line);
283 global_set_key("C-e", cmd_move_end_of_line);
285 global_set_key("M-v", cmd_scroll_up);
286 global_set_key("C-v", cmd_scroll_down);
287 global_set_key("M-space", cmd_scroll_up);
288 global_set_key("space", cmd_scroll_down);
290 global_set_key("M-<", cmd_beginning_of_buffer);
291 global_set_key("M->", cmd_end_of_buffer);
293 global_set_key("C-x C-c", cmd_kill_telescope);
295 global_set_key("C-g", cmd_clear_minibuf);
297 global_set_key("M-x", cmd_execute_extended_command);
298 global_set_key("C-x C-f", cmd_load_url);
299 global_set_key("C-x M-f", cmd_load_current_url);
301 global_set_key("C-x t 0", cmd_tab_close);
302 global_set_key("C-x t 1", cmd_tab_close_other);
303 global_set_key("C-x t 2", cmd_tab_new);
304 global_set_key("C-x t o", cmd_tab_next);
305 global_set_key("C-x t O", cmd_tab_previous);
306 global_set_key("C-x t m", cmd_tab_move);
307 global_set_key("C-x t M", cmd_tab_move_to);
309 global_set_key("C-M-b", cmd_previous_page);
310 global_set_key("C-M-f", cmd_next_page);
312 global_set_key("<f7> a", cmd_bookmark_page);
313 global_set_key("<f7> <f7>", cmd_list_bookmarks);
315 /* vi/vi-like */
316 global_set_key("k", cmd_previous_line);
317 global_set_key("j", cmd_next_line);
318 global_set_key("l", cmd_forward_char);
319 global_set_key("h", cmd_backward_char);
320 global_set_key("{", cmd_backward_paragraph);
321 global_set_key("}", cmd_forward_paragraph);
322 global_set_key("^", cmd_move_beginning_of_line);
323 global_set_key("$", cmd_move_end_of_line);
325 global_set_key("K", cmd_scroll_line_up);
326 global_set_key("J", cmd_scroll_line_down);
328 global_set_key("g g", cmd_beginning_of_buffer);
329 global_set_key("G", cmd_end_of_buffer);
331 global_set_key("g D", cmd_tab_close);
332 global_set_key("g N", cmd_tab_new);
333 global_set_key("g t", cmd_tab_next);
334 global_set_key("g T", cmd_tab_previous);
335 global_set_key("g M-t", cmd_tab_move);
336 global_set_key("g M-T", cmd_tab_move_to);
338 global_set_key("H", cmd_previous_page);
339 global_set_key("L", cmd_next_page);
341 /* tmp */
342 global_set_key("q", cmd_kill_telescope);
344 global_set_key("esc", cmd_clear_minibuf);
346 global_set_key(":", cmd_execute_extended_command);
348 /* cua */
349 global_set_key("<up>", cmd_previous_line);
350 global_set_key("<down>", cmd_next_line);
351 global_set_key("<right>", cmd_forward_char);
352 global_set_key("<left>", cmd_backward_char);
353 global_set_key("<prior>", cmd_scroll_up);
354 global_set_key("<next>", cmd_scroll_down);
356 global_set_key("M-<left>", cmd_previous_page);
357 global_set_key("M-<right>", cmd_next_page);
359 /* "ncurses standard" */
360 global_set_key("C-l", cmd_redraw);
362 /* global */
363 global_set_key("<f1>", cmd_toggle_help);
364 global_set_key("C-m", cmd_push_button);
365 global_set_key("M-enter", cmd_push_button_new_tab);
366 global_set_key("M-tab", cmd_previous_button);
367 global_set_key("tab", cmd_next_button);
369 /* === minibuffer map === */
370 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
371 minibuffer_set_key("C-g", cmd_mini_abort);
372 minibuffer_set_key("esc", cmd_mini_abort);
373 minibuffer_set_key("C-d", cmd_mini_delete_char);
374 minibuffer_set_key("del", cmd_mini_delete_backward_char);
375 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
376 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
378 minibuffer_set_key("C-b", cmd_backward_char);
379 minibuffer_set_key("C-f", cmd_forward_char);
380 minibuffer_set_key("<left>", cmd_backward_char);
381 minibuffer_set_key("<right>", cmd_forward_char);
382 minibuffer_set_key("C-e", cmd_move_end_of_line);
383 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
384 minibuffer_set_key("<end>", cmd_move_end_of_line);
385 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
386 minibuffer_set_key("C-k", cmd_mini_kill_line);
388 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
389 minibuffer_set_key("M-n", cmd_mini_next_history_element);
390 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
391 minibuffer_set_key("<down>", cmd_mini_next_history_element);
394 static void
395 restore_cursor(struct window *window)
397 struct vline *vl;
398 const char *prfx;
400 vl = window->current_line;
401 if (vl == NULL || vl->line == NULL)
402 window->curs_x = window->cpoff = 0;
403 else
404 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
406 if (vl != NULL) {
407 prfx = line_prefixes[vl->parent->type].prfx1;
408 window->curs_x += utf8_swidth(prfx);
412 static void
413 cmd_previous_line(struct window *window)
415 struct vline *vl;
417 if (window->current_line == NULL
418 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
419 return;
421 if (--window->curs_y < 0) {
422 window->curs_y = 0;
423 cmd_scroll_line_up(window);
424 return;
427 window->current_line = vl;
428 restore_cursor(window);
431 static void
432 cmd_next_line(struct window *window)
434 struct vline *vl;
436 if (window->current_line == NULL
437 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
438 return;
440 if (++window->curs_y > body_lines-1) {
441 window->curs_y = body_lines-1;
442 cmd_scroll_line_down(window);
443 return;
446 window->current_line = vl;
447 restore_cursor(window);
450 static void
451 cmd_backward_char(struct window *window)
453 if (window->cpoff != 0)
454 window->cpoff--;
455 restore_cursor(window);
458 static void
459 cmd_forward_char(struct window *window)
461 size_t len = 0;
463 if (window->current_line->line != NULL)
464 len = utf8_cplen(window->current_line->line);
465 if (++window->cpoff > len)
466 window->cpoff = len;
467 restore_cursor(window);
470 static void
471 cmd_backward_paragraph(struct window *window)
473 do {
474 if (window->current_line == NULL ||
475 window->current_line == TAILQ_FIRST(&window->head)) {
476 message("No previous paragraph");
477 return;
479 cmd_previous_line(window);
480 } while (window->current_line->line != NULL ||
481 window->current_line->parent->type != LINE_TEXT);
484 static void
485 cmd_forward_paragraph(struct window *window)
487 do {
488 if (window->current_line == NULL ||
489 window->current_line == TAILQ_LAST(&window->head, vhead)) {
490 message("No next paragraph");
491 return;
493 cmd_next_line(window);
494 } while (window->current_line->line != NULL ||
495 window->current_line->parent->type != LINE_TEXT);
498 static void
499 cmd_move_beginning_of_line(struct window *window)
501 window->cpoff = 0;
502 restore_cursor(window);
505 static void
506 cmd_move_end_of_line(struct window *window)
508 struct vline *vl;
510 vl = window->current_line;
511 if (vl->line == NULL)
512 return;
513 window->cpoff = utf8_cplen(vl->line);
514 restore_cursor(window);
517 static void
518 cmd_redraw(struct window *window)
520 handle_resize(0, 0, NULL);
523 static void
524 cmd_scroll_line_up(struct window *window)
526 struct vline *vl;
528 if (window->line_off == 0)
529 return;
531 vl = nth_line(window, --window->line_off);
532 wscrl(body, -1);
533 wmove(body, 0, 0);
534 print_vline(body, vl);
536 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
537 restore_cursor(window);
540 static void
541 cmd_scroll_line_down(struct window *window)
543 struct vline *vl;
545 vl = window->current_line;
546 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
547 return;
548 window->current_line = vl;
550 window->line_off++;
551 wscrl(body, 1);
553 if (window->line_max - window->line_off < (size_t)body_lines)
554 return;
556 vl = nth_line(window, window->line_off + body_lines-1);
557 wmove(body, body_lines-1, 0);
558 print_vline(body, vl);
560 restore_cursor(window);
563 static void
564 cmd_scroll_up(struct window *window)
566 size_t off;
568 off = body_lines-1;
570 for (; off > 0; --off)
571 cmd_scroll_line_up(window);
574 static void
575 cmd_scroll_down(struct window *window)
577 size_t off;
579 off = body_lines-1;
581 for (; off > 0; --off)
582 cmd_scroll_line_down(window);
585 static void
586 cmd_beginning_of_buffer(struct window *window)
588 window->current_line = TAILQ_FIRST(&window->head);
589 window->line_off = 0;
590 window->curs_y = 0;
591 window->cpoff = 0;
592 restore_cursor(window);
595 static void
596 cmd_end_of_buffer(struct window *window)
598 ssize_t off;
600 off = window->line_max - body_lines;
601 off = MAX(0, off);
603 window->line_off = off;
604 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
606 window->current_line = TAILQ_LAST(&window->head, vhead);
607 window->cpoff = body_cols;
608 restore_cursor(window);
611 static void
612 cmd_kill_telescope(struct window *window)
614 save_session();
615 event_loopbreak();
618 static void
619 cmd_push_button(struct window *window)
621 struct vline *vl;
622 size_t nth;
624 nth = window->line_off + window->curs_y;
625 if (nth >= window->line_max)
626 return;
627 vl = nth_line(window, nth);
628 if (vl->parent->type != LINE_LINK)
629 return;
631 load_url_in_tab(current_tab(), vl->parent->alt);
634 static void
635 cmd_push_button_new_tab(struct window *window)
637 struct vline *vl;
638 size_t nth;
640 nth = window->line_off + window->curs_y;
641 if (nth > window->line_max)
642 return;
643 vl = nth_line(window, nth);
644 if (vl->parent->type != LINE_LINK)
645 return;
647 new_tab(vl->parent->alt);
650 static void
651 cmd_previous_button(struct window *window)
653 do {
654 if (window->current_line == NULL ||
655 window->current_line == TAILQ_FIRST(&window->head)) {
656 message("No previous link");
657 return;
659 cmd_previous_line(window);
660 } while (window->current_line->parent->type != LINE_LINK);
663 static void
664 cmd_next_button(struct window *window)
666 do {
667 if (window->current_line == NULL ||
668 window->current_line == TAILQ_LAST(&window->head, vhead)) {
669 message("No next link");
670 return;
672 cmd_next_line(window);
673 } while (window->current_line->parent->type != LINE_LINK);
676 static void
677 cmd_previous_page(struct window *window)
679 struct tab *tab = current_tab();
681 if (!load_previous_page(tab))
682 message("No previous page");
683 else
684 start_loading_anim(tab);
687 static void
688 cmd_next_page(struct window *window)
690 struct tab *tab = current_tab();
692 if (!load_next_page(tab))
693 message("No next page");
694 else
695 start_loading_anim(tab);
698 static void
699 cmd_clear_minibuf(struct window *window)
701 handle_clear_minibuf(0, 0, NULL);
704 static void
705 cmd_execute_extended_command(struct window *window)
707 size_t len;
709 if (in_minibuffer) {
710 message("We don't have enable-recursive-minibuffers");
711 return;
714 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
715 &eecmd_history);
717 len = sizeof(ministate.prompt);
718 strlcpy(ministate.prompt, "", len);
720 if (thiskey.meta)
721 strlcat(ministate.prompt, "M-", len);
723 strlcat(ministate.prompt, keyname(thiskey.key), len);
725 if (thiskey.meta)
726 strlcat(ministate.prompt, " ", len);
729 static void
730 cmd_tab_close(struct window *window)
732 struct tab *tab, *t;
734 tab = current_tab();
735 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
736 TAILQ_NEXT(tab, tabs) == NULL) {
737 message("Can't close the only tab.");
738 return;
741 if (evtimer_pending(&tab->loadingev, NULL))
742 evtimer_del(&tab->loadingev);
744 stop_tab(tab);
746 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
747 t = TAILQ_NEXT(tab, tabs);
748 TAILQ_REMOVE(&tabshead, tab, tabs);
749 free(tab);
751 switch_to_tab(t);
754 static void
755 cmd_tab_close_other(struct window *window)
757 struct tab *t, *i;
759 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
760 if (t->flags & TAB_CURRENT)
761 continue;
763 stop_tab(t);
764 TAILQ_REMOVE(&tabshead, t, tabs);
765 free(t);
769 static void
770 cmd_tab_new(struct window *window)
772 new_tab(NEW_TAB_URL);
775 static void
776 cmd_tab_next(struct window *window)
778 struct tab *tab, *t;
780 tab = current_tab();
781 tab->flags &= ~TAB_CURRENT;
783 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
784 t = TAILQ_FIRST(&tabshead);
785 t->flags |= TAB_CURRENT;
786 t->flags &= ~TAB_URGENT;
789 static void
790 cmd_tab_previous(struct window *window)
792 struct tab *tab, *t;
794 tab = current_tab();
795 tab->flags &= ~TAB_CURRENT;
797 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
798 t = TAILQ_LAST(&tabshead, tabshead);
799 t->flags |= TAB_CURRENT;
800 t->flags &= ~TAB_URGENT;
803 static void
804 cmd_tab_move(struct window *window)
806 struct tab *tab, *t;
808 tab = current_tab();
809 t = TAILQ_NEXT(tab, tabs);
810 TAILQ_REMOVE(&tabshead, tab, tabs);
812 if (t == NULL)
813 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
814 else
815 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
818 static void
819 cmd_tab_move_to(struct window *window)
821 struct tab *tab, *t;
823 tab = current_tab();
824 t = TAILQ_PREV(tab, tabshead, tabs);
825 TAILQ_REMOVE(&tabshead, tab, tabs);
827 if (t == NULL) {
828 if (TAILQ_EMPTY(&tabshead))
829 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
830 else
831 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
832 } else
833 TAILQ_INSERT_BEFORE(t, tab, tabs);
836 static void
837 cmd_load_url(struct window *window)
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, "gemini://", sizeof(ministate.buf));
848 cmd_move_end_of_line(&ministate.window);
851 static void
852 cmd_load_current_url(struct window *window)
854 struct tab *tab = current_tab();
856 if (in_minibuffer) {
857 message("We don't have enable-recursive-minibuffers");
858 return;
861 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
862 &lu_history);
863 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
864 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
865 ministate.window.cpoff = utf8_cplen(ministate.buf);
868 static void
869 cmd_bookmark_page(struct window *window)
871 struct tab *tab = current_tab();
873 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
874 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
875 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
876 ministate.window.cpoff = utf8_cplen(ministate.buf);
879 static void
880 cmd_list_bookmarks(struct window *window)
882 load_url_in_tab(current_tab(), "about:bookmarks");
885 static void
886 cmd_toggle_help(struct window *window)
888 side_window = !side_window;
889 if (side_window)
890 recompute_help();
892 /*
893 * ugly hack, but otherwise the window doesn't get updated
894 * until I call handle_resize a second time (i.e. C-l). I
895 * will be happy to know why something like this is needed.
896 */
897 handle_resize(0, 0, NULL);
898 handle_resize(0, 0, NULL);
901 static void
902 cmd_mini_delete_char(struct window *window)
904 char *c, *n;
906 if (!in_minibuffer) {
907 message("text is read-only");
908 return;
911 minibuffer_taint_hist();
913 c = utf8_nth(window->current_line->line, window->cpoff);
914 if (*c == '\0')
915 return;
916 n = utf8_next_cp(c);
918 memmove(c, n, strlen(n)+1);
921 static void
922 cmd_mini_delete_backward_char(struct window *window)
924 char *c, *p, *start;
926 if (!in_minibuffer) {
927 message("text is read-only");
928 return;
931 minibuffer_taint_hist();
933 c = utf8_nth(window->current_line->line, window->cpoff);
934 start = window->current_line->line;
935 if (c == start)
936 return;
937 p = utf8_prev_cp(c-1, start);
939 memmove(p, c, strlen(c)+1);
940 window->cpoff--;
943 static void
944 cmd_mini_kill_line(struct window *window)
946 char *c;
948 if (!in_minibuffer) {
949 message("text is read-only");
950 return;
953 minibuffer_taint_hist();
954 c = utf8_nth(window->current_line->line, window->cpoff);
955 *c = '\0';
958 static void
959 cmd_mini_abort(struct window *window)
961 if (!in_minibuffer)
962 return;
964 ministate.abortfn();
967 static void
968 cmd_mini_complete_and_exit(struct window *window)
970 if (!in_minibuffer)
971 return;
973 minibuffer_taint_hist();
974 ministate.donefn();
977 static void
978 cmd_mini_previous_history_element(struct window *window)
980 if (ministate.history == NULL) {
981 message("No history");
982 return;
985 if (ministate.hist_cur == NULL ||
986 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
987 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
988 ministate.hist_off = ministate.history->len - 1;
989 if (ministate.hist_cur == NULL)
990 message("No prev item");
991 } else {
992 ministate.hist_off--;
995 if (ministate.hist_cur != NULL)
996 window->current_line->line = ministate.hist_cur->h;
999 static void
1000 cmd_mini_next_history_element(struct window *window)
1002 if (ministate.history == NULL) {
1003 message("No history");
1004 return;
1007 if (ministate.hist_cur == NULL ||
1008 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
1009 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
1010 ministate.hist_off = 0;
1011 if (ministate.hist_cur == NULL)
1012 message("No next item");
1013 } else {
1014 ministate.hist_off++;
1017 if (ministate.hist_cur != NULL)
1018 window->current_line->line = ministate.hist_cur->h;
1021 static void
1022 global_key_unbound(void)
1024 message("%s is undefined", keybuf);
1027 static void
1028 minibuffer_hist_save_entry(void)
1030 struct hist *hist;
1032 if (ministate.history == NULL)
1033 return;
1035 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1036 abort();
1038 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1040 if (TAILQ_EMPTY(&ministate.history->head))
1041 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1042 else
1043 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1044 ministate.history->len++;
1048 * taint the minibuffer cache: if we're currently showing a history
1049 * element, copy that to the current buf and reset the "history
1050 * navigation" thing.
1052 static void
1053 minibuffer_taint_hist(void)
1055 if (ministate.hist_cur == NULL)
1056 return;
1058 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1059 ministate.hist_cur = NULL;
1062 static void
1063 minibuffer_self_insert(void)
1065 char *c, tmp[5] = {0};
1066 size_t len;
1068 minibuffer_taint_hist();
1070 if (thiskey.cp == 0)
1071 return;
1073 len = utf8_encode(thiskey.cp, tmp);
1074 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1075 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1076 return;
1078 memmove(c + len, c, strlen(c)+1);
1079 memcpy(c, tmp, len);
1080 ministate.window.cpoff++;
1083 static void
1084 eecmd_self_insert(void)
1086 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1087 !unicode_isgraph(thiskey.cp)) {
1088 global_key_unbound();
1089 return;
1092 minibuffer_self_insert();
1095 static void
1096 eecmd_select(void)
1098 struct cmds *cmd;
1100 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1101 if (!strcmp(cmd->cmd, ministate.buf)) {
1102 exit_minibuffer();
1103 minibuffer_hist_save_entry();
1104 cmd->fn(current_window());
1105 return;
1109 message("No match");
1112 static void
1113 ir_self_insert(void)
1115 minibuffer_self_insert();
1118 static void
1119 ir_select(void)
1121 char buf[1025] = {0};
1122 struct phos_uri uri;
1123 struct tab *tab;
1125 tab = current_tab();
1127 exit_minibuffer();
1128 minibuffer_hist_save_entry();
1130 /* a bit ugly but... */
1131 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1132 phos_uri_set_query(&uri, ministate.buf);
1133 phos_serialize_uri(&uri, buf, sizeof(buf));
1134 load_url_in_tab(tab, buf);
1137 static void
1138 lu_self_insert(void)
1140 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1141 !unicode_isgraph(thiskey.key)) {
1142 global_key_unbound();
1143 return;
1146 minibuffer_self_insert();
1149 static void
1150 lu_select(void)
1152 exit_minibuffer();
1153 minibuffer_hist_save_entry();
1154 load_url_in_tab(current_tab(), ministate.buf);
1157 static void
1158 bp_select(void)
1160 exit_minibuffer();
1161 if (*ministate.buf != '\0')
1162 add_to_bookmarks(ministate.buf);
1163 else
1164 message("Abort.");
1167 static void
1168 yornp_self_insert(void)
1170 if (thiskey.key != 'y' && thiskey.key != 'n') {
1171 message("Please answer y or n");
1172 return;
1175 exit_minibuffer();
1176 yornp_cb(thiskey.key == 'y', yornp_data);
1179 static void
1180 yornp_abort(void)
1182 exit_minibuffer();
1183 yornp_cb(0, yornp_data);
1186 static struct vline *
1187 nth_line(struct window *window, size_t n)
1189 struct vline *vl;
1190 size_t i;
1192 i = 0;
1193 TAILQ_FOREACH(vl, &window->head, vlines) {
1194 if (i == n)
1195 return vl;
1196 i++;
1199 /* unreachable */
1200 abort();
1203 static struct tab *
1204 current_tab(void)
1206 struct tab *t;
1208 TAILQ_FOREACH(t, &tabshead, tabs) {
1209 if (t->flags & TAB_CURRENT)
1210 return t;
1213 /* unreachable */
1214 abort();
1217 static struct window *
1218 current_window(void)
1220 if (in_minibuffer)
1221 return &ministate.window;
1222 return &current_tab()->window;
1225 static int
1226 readkey(void)
1228 uint32_t state = 0;
1230 if ((thiskey.key = wgetch(body)) == ERR)
1231 return 0;
1233 thiskey.meta = thiskey.key == 27;
1234 if (thiskey.meta) {
1235 thiskey.key = wgetch(body);
1236 if (thiskey.key == ERR || thiskey.key == 27) {
1237 thiskey.meta = 0;
1238 thiskey.key = 27;
1242 thiskey.cp = 0;
1243 if ((unsigned int)thiskey.key < UINT8_MAX) {
1244 while (1) {
1245 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1246 break;
1247 if ((thiskey.key = wgetch(body)) == ERR) {
1248 message("Error decoding user input");
1249 return 0;
1254 return 1;
1257 static void
1258 dispatch_stdio(int fd, short ev, void *d)
1260 struct keymap *k;
1261 const char *keyname;
1262 char tmp[5] = {0};
1264 if (!readkey())
1265 return;
1267 if (keybuf[0] != '\0')
1268 strlcat(keybuf, " ", sizeof(keybuf));
1269 if (thiskey.meta)
1270 strlcat(keybuf, "M-", sizeof(keybuf));
1271 if (thiskey.cp != 0) {
1272 utf8_encode(thiskey.cp, tmp);
1273 strlcat(keybuf, tmp, sizeof(keybuf));
1274 } else {
1275 if ((keyname = unkbd(thiskey.key)) != NULL)
1276 strlcat(keybuf, keyname, sizeof(keybuf));
1277 else {
1278 tmp[0] = thiskey.key;
1279 strlcat(keybuf, tmp, sizeof(keybuf));
1283 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1284 if (k->meta == thiskey.meta &&
1285 k->key == thiskey.key) {
1286 if (k->fn == NULL)
1287 current_map = &k->map;
1288 else {
1289 current_map = base_map;
1290 strlcpy(keybuf, "", sizeof(keybuf));
1291 k->fn(current_window());
1293 goto done;
1297 if (current_map->unhandled_input != NULL)
1298 current_map->unhandled_input();
1299 else {
1300 global_key_unbound();
1303 strlcpy(keybuf, "", sizeof(keybuf));
1304 current_map = base_map;
1306 done:
1307 if (side_window)
1308 recompute_help();
1310 redraw_tab(current_tab());
1313 static void
1314 handle_clear_minibuf(int fd, short ev, void *d)
1316 free(ministate.curmesg);
1317 ministate.curmesg = NULL;
1319 redraw_minibuffer();
1320 if (in_minibuffer) {
1321 wrefresh(body);
1322 wrefresh(minibuf);
1323 } else {
1324 wrefresh(minibuf);
1325 wrefresh(body);
1329 static void
1330 handle_resize(int sig, short ev, void *d)
1332 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1333 event_del(&resizeev);
1335 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1336 evtimer_add(&resizeev, &resize_timer);
1339 static void
1340 handle_resize_timeout(int s, short ev, void *d)
1342 struct tab *tab;
1344 endwin();
1345 refresh();
1346 clear();
1348 /* move and resize the windows, in reverse order! */
1350 mvwin(minibuf, LINES-1, 0);
1351 wresize(minibuf, 1, COLS);
1353 mvwin(modeline, LINES-2, 0);
1354 wresize(modeline, 1, COLS);
1356 body_lines = LINES-3;
1357 body_cols = COLS;
1359 if (side_window) {
1360 help_cols = 0.3 * COLS;
1361 help_lines = LINES-3;
1362 mvwin(help, 1, 0);
1363 wresize(help, help_lines, help_cols);
1365 wrap_page(&helpwin, help_cols);
1367 body_cols = COLS - help_cols - 1;
1368 mvwin(body, 1, help_cols);
1369 } else
1370 mvwin(body, 1, 0);
1372 wresize(body, body_lines, body_cols);
1374 wresize(tabline, 1, COLS);
1376 tab = current_tab();
1378 wrap_page(&tab->window, body_cols);
1379 redraw_tab(tab);
1382 static int
1383 wrap_page(struct window *window, int width)
1385 struct line *l;
1386 const struct line *orig;
1387 struct vline *vl;
1388 const char *prfx;
1390 orig = window->current_line == NULL
1391 ? NULL
1392 : window->current_line->parent;
1393 window->current_line = NULL;
1395 window->curs_y = 0;
1396 window->line_off = 0;
1398 empty_vlist(window);
1400 TAILQ_FOREACH(l, &window->page.head, lines) {
1401 prfx = line_prefixes[l->type].prfx1;
1402 switch (l->type) {
1403 case LINE_TEXT:
1404 case LINE_LINK:
1405 case LINE_TITLE_1:
1406 case LINE_TITLE_2:
1407 case LINE_TITLE_3:
1408 case LINE_ITEM:
1409 case LINE_QUOTE:
1410 case LINE_PRE_START:
1411 case LINE_PRE_END:
1412 wrap_text(window, prfx, l, width);
1413 break;
1414 case LINE_PRE_CONTENT:
1415 hardwrap_text(window, l, width);
1416 break;
1419 if (orig == l && window->current_line == NULL) {
1420 window->line_off = window->line_max-1;
1421 window->current_line = TAILQ_LAST(&window->head, vhead);
1423 while (1) {
1424 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1425 if (vl == NULL || vl->parent != orig)
1426 break;
1427 window->current_line = vl;
1428 window->line_off--;
1433 if (window->current_line == NULL)
1434 window->current_line = TAILQ_FIRST(&window->head);
1436 return 1;
1439 static void
1440 print_vline(WINDOW *window, struct vline *vl)
1442 const char *text = vl->line;
1443 const char *prfx;
1444 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1445 int text_face = line_faces[vl->parent->type].text_prop;
1447 if (!vl->flags)
1448 prfx = line_prefixes[vl->parent->type].prfx1;
1449 else
1450 prfx = line_prefixes[vl->parent->type].prfx2;
1452 if (text == NULL)
1453 text = "";
1455 wattron(window, prefix_face);
1456 wprintw(window, "%s", prfx);
1457 wattroff(window, prefix_face);
1459 wattron(window, text_face);
1460 wprintw(window, "%s", text);
1461 wattroff(window, text_face);
1464 static void
1465 redraw_tabline(void)
1467 struct tab *tab;
1468 size_t toskip, ots, tabwidth, space, x;
1469 int current, y, truncated;
1470 const char *title;
1471 char buf[25];
1473 tabwidth = sizeof(buf)+1;
1474 space = COLS-2;
1476 toskip = 0;
1477 TAILQ_FOREACH(tab, &tabshead, tabs) {
1478 toskip++;
1479 if (tab->flags & TAB_CURRENT)
1480 break;
1483 if (toskip * tabwidth < space)
1484 toskip = 0;
1485 else {
1486 ots = toskip;
1487 toskip--;
1488 while (toskip != 0 &&
1489 (ots - toskip+1) * tabwidth < space)
1490 toskip--;
1493 werase(tabline);
1494 wattron(tabline, tab_face.background);
1495 wprintw(tabline, toskip == 0 ? " " : "<");
1496 wattroff(tabline, tab_face.background);
1498 truncated = 0;
1499 TAILQ_FOREACH(tab, &tabshead, tabs) {
1500 if (truncated)
1501 break;
1502 if (toskip != 0) {
1503 toskip--;
1504 continue;
1507 getyx(tabline, y, x);
1508 if (x + sizeof(buf)+2 >= (size_t)COLS)
1509 truncated = 1;
1511 current = tab->flags & TAB_CURRENT;
1513 if (*(title = tab->window.page.title) == '\0')
1514 title = tab->hist_cur->h;
1516 if (tab->flags & TAB_URGENT)
1517 strlcpy(buf, "!", sizeof(buf));
1518 else
1519 strlcpy(buf, " ", sizeof(buf));
1521 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1522 /* truncation happens */
1523 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1524 } else {
1525 /* pad with spaces */
1526 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1527 /* nop */ ;
1530 if (current)
1531 wattron(tabline, tab_face.current_tab);
1532 else
1533 wattron(tabline, tab_face.tab);
1535 wprintw(tabline, "%s", buf);
1536 if (TAILQ_NEXT(tab, tabs) != NULL)
1537 wprintw(tabline, " ");
1539 if (current)
1540 wattroff(tabline, tab_face.current_tab);
1541 else
1542 wattroff(tabline, tab_face.tab);
1545 wattron(tabline, tab_face.background);
1546 for (; x < (size_t)COLS; ++x)
1547 waddch(tabline, ' ');
1548 if (truncated)
1549 mvwprintw(tabline, 0, COLS-1, ">");
1552 static void
1553 redraw_window(WINDOW *win, int height, struct window *window)
1555 struct vline *vl;
1556 int l;
1558 werase(win);
1560 window->line_off = MIN(window->line_max-1, window->line_off);
1561 if (TAILQ_EMPTY(&window->head))
1562 return;
1564 l = 0;
1565 vl = nth_line(window, window->line_off);
1566 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1567 wmove(win, l, 0);
1568 print_vline(win, vl);
1569 l++;
1570 if (l == height)
1571 break;
1574 wmove(win, window->curs_y, window->curs_x);
1577 static void
1578 redraw_help(void)
1580 redraw_window(help, help_lines, &helpwin);
1583 static void
1584 redraw_body(struct tab *tab)
1586 redraw_window(body, body_lines, &tab->window);
1589 static inline char
1590 trust_status_char(enum trust_state ts)
1592 switch (ts) {
1593 case TS_UNKNOWN: return 'u';
1594 case TS_UNTRUSTED: return '!';
1595 case TS_TRUSTED: return 'v';
1596 case TS_VERIFIED: return 'V';
1600 static void
1601 redraw_modeline(struct tab *tab)
1603 double pct;
1604 int x, y, max_x, max_y;
1605 const char *mode = tab->window.page.name;
1606 const char *spin = "-\\|/";
1608 werase(modeline);
1609 wattron(modeline, A_REVERSE);
1610 wmove(modeline, 0, 0);
1612 wprintw(modeline, "-%c%c %s ",
1613 spin[tab->loading_anim_step],
1614 trust_status_char(tab->trust),
1615 mode == NULL ? "(none)" : mode);
1617 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1619 if (tab->window.line_max <= (size_t)body_lines)
1620 wprintw(modeline, "All ");
1621 else if (tab->window.line_off == 0)
1622 wprintw(modeline, "Top ");
1623 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1624 wprintw(modeline, "Bottom ");
1625 else
1626 wprintw(modeline, "%.0f%% ", pct);
1628 wprintw(modeline, "%d/%d %s ",
1629 tab->window.line_off + tab->window.curs_y,
1630 tab->window.line_max,
1631 tab->hist_cur->h);
1633 getyx(modeline, y, x);
1634 getmaxyx(modeline, max_y, max_x);
1636 (void)y;
1637 (void)max_y;
1639 for (; x < max_x; ++x)
1640 waddstr(modeline, "-");
1643 static void
1644 redraw_minibuffer(void)
1646 struct tab *tab;
1647 size_t off_y, off_x = 0;
1648 char *start, *c;
1650 werase(minibuf);
1652 if (in_minibuffer) {
1653 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1654 if (ministate.hist_cur != NULL)
1655 wprintw(minibuf, "(%zu/%zu) ",
1656 ministate.hist_off + 1,
1657 ministate.history->len);
1659 getyx(minibuf, off_y, off_x);
1661 start = ministate.hist_cur != NULL
1662 ? ministate.hist_cur->h
1663 : ministate.buf;
1664 c = utf8_nth(ministate.window.current_line->line,
1665 ministate.window.cpoff);
1666 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1667 start = utf8_next_cp(start);
1670 waddstr(minibuf, start);
1673 if (ministate.curmesg != NULL)
1674 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1675 ministate.curmesg);
1677 if (!in_minibuffer && ministate.curmesg == NULL)
1678 waddstr(minibuf, keybuf);
1680 /* If nothing else, show the URL at point */
1681 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1682 tab = current_tab();
1683 if (tab->window.current_line != NULL &&
1684 tab->window.current_line->parent->type == LINE_LINK)
1685 waddstr(minibuf, tab->window.current_line->parent->alt);
1688 if (in_minibuffer)
1689 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1692 static void
1693 redraw_tab(struct tab *tab)
1695 if (side_window) {
1696 redraw_help();
1697 wnoutrefresh(help);
1700 redraw_tabline();
1701 redraw_body(tab);
1702 redraw_modeline(tab);
1703 redraw_minibuffer();
1705 wnoutrefresh(tabline);
1706 wnoutrefresh(modeline);
1708 if (in_minibuffer) {
1709 wnoutrefresh(body);
1710 wnoutrefresh(minibuf);
1711 } else {
1712 wnoutrefresh(minibuf);
1713 wnoutrefresh(body);
1716 doupdate();
1719 static void
1720 emit_help_item(char *prfx, void *fn)
1722 struct line *l;
1723 struct cmds *cmd;
1725 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1726 if (fn == cmd->fn)
1727 break;
1729 assert(cmd != NULL);
1731 if ((l = calloc(1, sizeof(*l))) == NULL)
1732 abort();
1734 l->type = LINE_TEXT;
1735 l->alt = NULL;
1737 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1739 if (TAILQ_EMPTY(&helpwin.page.head))
1740 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1741 else
1742 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1745 static void
1746 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1748 struct keymap *k;
1749 char p[32];
1750 const char *kn;
1752 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1753 strlcpy(p, prfx, sizeof(p));
1754 if (*p != '\0')
1755 strlcat(p, " ", sizeof(p));
1756 if (k->meta)
1757 strlcat(p, "M-", sizeof(p));
1758 if ((kn = unkbd(k->key)) != NULL)
1759 strlcat(p, kn, sizeof(p));
1760 else
1761 strlcat(p, keyname(k->key), sizeof(p));
1763 if (k->fn == NULL)
1764 rec_compute_help(&k->map, p, sizeof(p));
1765 else
1766 emit_help_item(p, k->fn);
1770 static void
1771 recompute_help(void)
1773 char p[32] = { 0 };
1775 empty_vlist(&helpwin);
1776 empty_linelist(&helpwin);
1777 rec_compute_help(current_map, p, sizeof(p));
1778 wrap_page(&helpwin, help_cols);
1781 static void
1782 vmessage(const char *fmt, va_list ap)
1784 if (evtimer_pending(&clminibufev, NULL))
1785 evtimer_del(&clminibufev);
1786 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1787 evtimer_add(&clminibufev, &clminibufev_timer);
1789 free(ministate.curmesg);
1791 /* TODO: what to do if the allocation fails here? */
1792 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1793 ministate.curmesg = NULL;
1795 redraw_minibuffer();
1796 if (in_minibuffer) {
1797 wrefresh(body);
1798 wrefresh(minibuf);
1799 } else {
1800 wrefresh(minibuf);
1801 wrefresh(body);
1805 static void
1806 message(const char *fmt, ...)
1808 va_list ap;
1810 va_start(ap, fmt);
1811 vmessage(fmt, ap);
1812 va_end(ap);
1815 static void
1816 start_loading_anim(struct tab *tab)
1818 if (tab->loading_anim)
1819 return;
1820 tab->loading_anim = 1;
1821 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1822 evtimer_add(&tab->loadingev, &loadingev_timer);
1825 static void
1826 update_loading_anim(int fd, short ev, void *d)
1828 struct tab *tab = d;
1830 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1832 if (tab->flags & TAB_CURRENT) {
1833 redraw_modeline(tab);
1834 wrefresh(modeline);
1835 wrefresh(body);
1836 if (in_minibuffer)
1837 wrefresh(minibuf);
1840 evtimer_add(&tab->loadingev, &loadingev_timer);
1843 static void
1844 stop_loading_anim(struct tab *tab)
1846 if (!tab->loading_anim)
1847 return;
1848 evtimer_del(&tab->loadingev);
1849 tab->loading_anim = 0;
1850 tab->loading_anim_step = 0;
1852 if (!(tab->flags & TAB_CURRENT))
1853 return;
1855 redraw_modeline(tab);
1857 wrefresh(modeline);
1858 wrefresh(body);
1859 if (in_minibuffer)
1860 wrefresh(minibuf);
1863 static void
1864 load_url_in_tab(struct tab *tab, const char *url)
1866 message("Loading %s...", url);
1867 start_loading_anim(tab);
1868 load_url(tab, url);
1870 tab->window.curs_x = 0;
1871 tab->window.curs_y = 0;
1872 redraw_tab(tab);
1875 static void
1876 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1877 void (*abortfn)(void), struct histhead *hist)
1879 in_minibuffer = 1;
1880 base_map = &minibuffer_map;
1881 current_map = &minibuffer_map;
1883 base_map->unhandled_input = self_insert_fn;
1885 ministate.donefn = donefn;
1886 ministate.abortfn = abortfn;
1887 memset(ministate.buf, 0, sizeof(ministate.buf));
1888 ministate.window.current_line = &ministate.vline;
1889 ministate.window.current_line->line = ministate.buf;
1890 ministate.window.cpoff = 0;
1891 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1893 ministate.history = hist;
1894 ministate.hist_cur = NULL;
1895 ministate.hist_off = 0;
1898 static void
1899 exit_minibuffer(void)
1901 werase(minibuf);
1903 in_minibuffer = 0;
1904 base_map = &global_map;
1905 current_map = &global_map;
1908 static void
1909 switch_to_tab(struct tab *tab)
1911 struct tab *t;
1913 TAILQ_FOREACH(t, &tabshead, tabs) {
1914 t->flags &= ~TAB_CURRENT;
1917 tab->flags |= TAB_CURRENT;
1920 unsigned int
1921 tab_new_id(void)
1923 return tab_counter++;
1926 static struct tab *
1927 new_tab(const char *url)
1929 struct tab *tab;
1931 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1932 event_loopbreak();
1933 return NULL;
1936 TAILQ_INIT(&tab->hist.head);
1938 TAILQ_INIT(&tab->window.head);
1940 tab->id = tab_new_id();
1941 switch_to_tab(tab);
1943 if (TAILQ_EMPTY(&tabshead))
1944 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1945 else
1946 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1948 load_url_in_tab(tab, url);
1949 return tab;
1952 static void
1953 session_new_tab_cb(const char *url)
1955 new_tab(url);
1958 static void
1959 usage(void)
1961 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1964 int
1965 ui_init(int argc, char * const *argv)
1967 const char *url = NEW_TAB_URL;
1968 int ch;
1970 while ((ch = getopt(argc, argv, "")) != -1) {
1971 switch (ch) {
1972 default:
1973 usage();
1974 return 0;
1977 argc -= optind;
1978 argv += optind;
1980 if (argc != 0)
1981 url = argv[0];
1983 setlocale(LC_ALL, "");
1985 TAILQ_INIT(&global_map.m);
1986 global_map.unhandled_input = global_key_unbound;
1988 TAILQ_INIT(&minibuffer_map.m);
1990 TAILQ_INIT(&eecmd_history.head);
1991 TAILQ_INIT(&ir_history.head);
1992 TAILQ_INIT(&lu_history.head);
1994 ministate.line.type = LINE_TEXT;
1995 ministate.vline.parent = &ministate.line;
1996 ministate.window.current_line = &ministate.vline;
1998 /* initialize help window */
1999 TAILQ_INIT(&helpwin.head);
2001 base_map = &global_map;
2002 current_map = &global_map;
2003 load_default_keys();
2005 initscr();
2006 raw();
2007 noecho();
2009 nonl();
2010 intrflush(stdscr, FALSE);
2012 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2013 return 0;
2014 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2015 return 0;
2016 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2017 return 0;
2018 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2019 return 0;
2020 if ((help = newwin(1, 1, 1, 0)) == NULL)
2021 return 0;
2023 body_lines = LINES-3;
2024 body_cols = COLS;
2026 keypad(body, TRUE);
2027 scrollok(body, TRUE);
2029 /* non-blocking input */
2030 wtimeout(body, 0);
2032 mvwprintw(body, 0, 0, "");
2034 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2035 event_add(&stdioev, NULL);
2037 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2038 signal_add(&winchev, NULL);
2040 load_last_session(session_new_tab_cb);
2041 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2042 new_tab(url);
2044 return 1;
2047 void
2048 ui_on_tab_loaded(struct tab *tab)
2050 stop_loading_anim(tab);
2051 message("Loaded %s", tab->hist_cur->h);
2053 redraw_tabline();
2054 wrefresh(tabline);
2055 if (in_minibuffer)
2056 wrefresh(minibuf);
2057 else
2058 wrefresh(body);
2061 void
2062 ui_on_tab_refresh(struct tab *tab)
2064 wrap_page(&tab->window, body_cols);
2065 if (tab->flags & TAB_CURRENT) {
2066 restore_cursor(&tab->window);
2067 redraw_tab(tab);
2068 } else
2069 tab->flags |= TAB_URGENT;
2072 void
2073 ui_require_input(struct tab *tab, int hide)
2075 /* TODO: hard-switching to another tab is ugly */
2076 switch_to_tab(tab);
2078 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2079 &ir_history);
2080 strlcpy(ministate.prompt, "Input required: ",
2081 sizeof(ministate.prompt));
2082 redraw_tab(tab);
2085 void
2086 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2087 unsigned int data)
2089 size_t len;
2091 if (in_minibuffer) {
2092 fn(0, data);
2093 return;
2096 yornp_cb = fn;
2097 yornp_data = data;
2098 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2099 yornp_abort, NULL);
2101 len = sizeof(ministate.prompt);
2102 strlcpy(ministate.prompt, prompt, len);
2103 strlcat(ministate.prompt, " (y or n) ", len);
2104 redraw_tab(current_tab());
2107 void
2108 ui_notify(const char *fmt, ...)
2110 va_list ap;
2112 va_start(ap, fmt);
2113 vmessage(fmt, ap);
2114 va_end(ap);
2117 void
2118 ui_end(void)
2120 endwin();