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 void handle_resize_timeout(int, short, void*);
132 static int wrap_page(struct window*, int);
133 static void print_vline(WINDOW*, struct vline*);
134 static void redraw_tabline(void);
135 static void redraw_window(WINDOW*, int, struct window*);
136 static void redraw_help(void);
137 static void redraw_body(struct tab*);
138 static void redraw_modeline(struct tab*);
139 static void redraw_minibuffer(void);
140 static void redraw_tab(struct tab*);
141 static void emit_help_item(char*, void*);
142 static void rec_compute_help(struct kmap*, char*, size_t);
143 static void recompute_help(void);
144 static void vmessage(const char*, va_list);
145 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
146 static void start_loading_anim(struct tab*);
147 static void update_loading_anim(int, short, void*);
148 static void stop_loading_anim(struct tab*);
149 static void load_url_in_tab(struct tab*, const char*);
150 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
151 static void exit_minibuffer(void);
152 static void switch_to_tab(struct tab*);
153 static struct tab *new_tab(const char*);
154 static void session_new_tab_cb(const char*);
155 static void usage(void);
157 static struct { short meta; int key; uint32_t cp; } thiskey;
159 static struct event resizeev;
160 static struct timeval resize_timer = { 0, 250000 };
162 static WINDOW *tabline, *body, *modeline, *minibuf;
163 static int body_lines, body_cols;
165 static WINDOW *help;
166 static struct window helpwin;
167 static int help_lines, help_cols;
169 static int side_window;
171 static struct event clminibufev;
172 static struct timeval clminibufev_timer = { 5, 0 };
173 static struct timeval loadingev_timer = { 0, 250000 };
175 static uint32_t tab_counter;
177 static char keybuf[64];
179 static void (*yornp_cb)(int, unsigned int);
180 static unsigned int yornp_data;
182 struct kmap global_map,
183 minibuffer_map,
184 *current_map,
185 *base_map;
187 static struct histhead eecmd_history,
188 ir_history,
189 lu_history;
191 static int in_minibuffer;
193 static struct {
194 char *curmesg;
196 char prompt[64];
197 void (*donefn)(void);
198 void (*abortfn)(void);
200 char buf[1025];
201 struct line line;
202 struct vline vline;
203 struct window window;
205 struct histhead *history;
206 struct hist *hist_cur;
207 size_t hist_off;
208 } ministate;
210 struct lineprefix {
211 const char *prfx1;
212 const char *prfx2;
213 } line_prefixes[] = {
214 [LINE_TEXT] = { "", "" },
215 [LINE_LINK] = { "=> ", " " },
216 [LINE_TITLE_1] = { "# ", " " },
217 [LINE_TITLE_2] = { "## ", " " },
218 [LINE_TITLE_3] = { "### ", " " },
219 [LINE_ITEM] = { "* ", " " },
220 [LINE_QUOTE] = { "> ", " " },
221 [LINE_PRE_START] = { "```", " " },
222 [LINE_PRE_CONTENT] = { "", "" },
223 [LINE_PRE_END] = { "```", "```" },
224 };
226 static struct line_face {
227 int prefix_prop;
228 int text_prop;
229 } line_faces[] = {
230 [LINE_TEXT] = { 0, 0 },
231 [LINE_LINK] = { 0, A_UNDERLINE },
232 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
233 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
234 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
235 [LINE_ITEM] = { 0, 0 },
236 [LINE_QUOTE] = { 0, A_DIM },
237 [LINE_PRE_START] = { 0, 0 },
238 [LINE_PRE_CONTENT] = { 0, 0 },
239 [LINE_PRE_END] = { 0, 0 },
240 };
242 static struct tab_face {
243 int background, tab, current_tab;
244 } tab_face = {
245 A_REVERSE, A_REVERSE, A_NORMAL
246 };
248 static inline void
249 global_set_key(const char *key, void (*fn)(struct window*))
251 if (!kmap_define_key(&global_map, key, fn))
252 _exit(1);
255 static inline void
256 minibuffer_set_key(const char *key, void (*fn)(struct window*))
258 if (!kmap_define_key(&minibuffer_map, key, fn))
259 _exit(1);
262 static void
263 load_default_keys(void)
265 /* === global map === */
267 /* emacs */
268 global_set_key("C-p", cmd_previous_line);
269 global_set_key("C-n", cmd_next_line);
270 global_set_key("C-f", cmd_forward_char);
271 global_set_key("C-b", cmd_backward_char);
272 global_set_key("M-{", cmd_backward_paragraph);
273 global_set_key("M-}", cmd_forward_paragraph);
274 global_set_key("C-a", cmd_move_beginning_of_line);
275 global_set_key("C-e", cmd_move_end_of_line);
277 global_set_key("M-v", cmd_scroll_up);
278 global_set_key("C-v", cmd_scroll_down);
279 global_set_key("M-space", cmd_scroll_up);
280 global_set_key("space", cmd_scroll_down);
282 global_set_key("M-<", cmd_beginning_of_buffer);
283 global_set_key("M->", cmd_end_of_buffer);
285 global_set_key("C-x C-c", cmd_kill_telescope);
287 global_set_key("C-g", cmd_clear_minibuf);
289 global_set_key("M-x", cmd_execute_extended_command);
290 global_set_key("C-x C-f", cmd_load_url);
291 global_set_key("C-x M-f", cmd_load_current_url);
293 global_set_key("C-x t 0", cmd_tab_close);
294 global_set_key("C-x t 1", cmd_tab_close_other);
295 global_set_key("C-x t 2", cmd_tab_new);
296 global_set_key("C-x t o", cmd_tab_next);
297 global_set_key("C-x t O", cmd_tab_previous);
298 global_set_key("C-x t m", cmd_tab_move);
299 global_set_key("C-x t M", cmd_tab_move_to);
301 global_set_key("C-M-b", cmd_previous_page);
302 global_set_key("C-M-f", cmd_next_page);
304 global_set_key("<f7> a", cmd_bookmark_page);
305 global_set_key("<f7> <f7>", cmd_list_bookmarks);
307 /* vi/vi-like */
308 global_set_key("k", cmd_previous_line);
309 global_set_key("j", cmd_next_line);
310 global_set_key("l", cmd_forward_char);
311 global_set_key("h", cmd_backward_char);
312 global_set_key("{", cmd_backward_paragraph);
313 global_set_key("}", cmd_forward_paragraph);
314 global_set_key("^", cmd_move_beginning_of_line);
315 global_set_key("$", cmd_move_end_of_line);
317 global_set_key("K", cmd_scroll_line_up);
318 global_set_key("J", cmd_scroll_line_down);
320 global_set_key("g g", cmd_beginning_of_buffer);
321 global_set_key("G", cmd_end_of_buffer);
323 global_set_key("g D", cmd_tab_close);
324 global_set_key("g N", cmd_tab_new);
325 global_set_key("g t", cmd_tab_next);
326 global_set_key("g T", cmd_tab_previous);
327 global_set_key("g M-t", cmd_tab_move);
328 global_set_key("g M-T", cmd_tab_move_to);
330 global_set_key("H", cmd_previous_page);
331 global_set_key("L", cmd_next_page);
333 /* tmp */
334 global_set_key("q", cmd_kill_telescope);
336 global_set_key("esc", cmd_clear_minibuf);
338 global_set_key(":", cmd_execute_extended_command);
340 /* cua */
341 global_set_key("<up>", cmd_previous_line);
342 global_set_key("<down>", cmd_next_line);
343 global_set_key("<right>", cmd_forward_char);
344 global_set_key("<left>", cmd_backward_char);
345 global_set_key("<prior>", cmd_scroll_up);
346 global_set_key("<next>", cmd_scroll_down);
348 global_set_key("M-<left>", cmd_previous_page);
349 global_set_key("M-<right>", cmd_next_page);
351 /* "ncurses standard" */
352 global_set_key("C-l", cmd_redraw);
354 /* global */
355 global_set_key("<f1>", cmd_toggle_help);
356 global_set_key("C-m", cmd_push_button);
357 global_set_key("M-enter", cmd_push_button_new_tab);
358 global_set_key("M-tab", cmd_previous_button);
359 global_set_key("tab", cmd_next_button);
361 /* === minibuffer map === */
362 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
363 minibuffer_set_key("C-g", cmd_mini_abort);
364 minibuffer_set_key("esc", cmd_mini_abort);
365 minibuffer_set_key("C-d", cmd_mini_delete_char);
366 minibuffer_set_key("del", cmd_mini_delete_backward_char);
367 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
368 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
370 minibuffer_set_key("C-b", cmd_backward_char);
371 minibuffer_set_key("C-f", cmd_forward_char);
372 minibuffer_set_key("<left>", cmd_backward_char);
373 minibuffer_set_key("<right>", cmd_forward_char);
374 minibuffer_set_key("C-e", cmd_move_end_of_line);
375 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
376 minibuffer_set_key("<end>", cmd_move_end_of_line);
377 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
378 minibuffer_set_key("C-k", cmd_mini_kill_line);
380 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
381 minibuffer_set_key("M-n", cmd_mini_next_history_element);
382 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
383 minibuffer_set_key("<down>", cmd_mini_next_history_element);
386 static void
387 restore_cursor(struct window *window)
389 struct vline *vl;
390 const char *prfx;
392 vl = window->current_line;
393 if (vl == NULL || vl->line == NULL)
394 window->curs_x = window->cpoff = 0;
395 else
396 window->curs_x = utf8_snwidth(vl->line, window->cpoff);
398 if (vl != NULL) {
399 prfx = line_prefixes[vl->parent->type].prfx1;
400 window->curs_x += utf8_swidth(prfx);
404 static void
405 cmd_previous_line(struct window *window)
407 struct vline *vl;
409 if (window->current_line == NULL
410 || (vl = TAILQ_PREV(window->current_line, vhead, vlines)) == NULL)
411 return;
413 if (--window->curs_y < 0) {
414 window->curs_y = 0;
415 cmd_scroll_line_up(window);
416 return;
419 window->current_line = vl;
420 restore_cursor(window);
423 static void
424 cmd_next_line(struct window *window)
426 struct vline *vl;
428 if (window->current_line == NULL
429 || (vl = TAILQ_NEXT(window->current_line, vlines)) == NULL)
430 return;
432 if (++window->curs_y > body_lines-1) {
433 window->curs_y = body_lines-1;
434 cmd_scroll_line_down(window);
435 return;
438 window->current_line = vl;
439 restore_cursor(window);
442 static void
443 cmd_backward_char(struct window *window)
445 if (window->cpoff != 0)
446 window->cpoff--;
447 restore_cursor(window);
450 static void
451 cmd_forward_char(struct window *window)
453 size_t len;
455 len = utf8_cplen(ministate.buf);
456 if (++window->cpoff > len)
457 window->cpoff = len;
458 restore_cursor(window);
461 static void
462 cmd_backward_paragraph(struct window *window)
464 do {
465 if (window->current_line == NULL ||
466 window->current_line == TAILQ_FIRST(&window->head)) {
467 message("No previous paragraph");
468 return;
470 cmd_previous_line(window);
471 } while (window->current_line->line != NULL ||
472 window->current_line->parent->type != LINE_TEXT);
475 static void
476 cmd_forward_paragraph(struct window *window)
478 do {
479 if (window->current_line == NULL ||
480 window->current_line == TAILQ_LAST(&window->head, vhead)) {
481 message("No next paragraph");
482 return;
484 cmd_next_line(window);
485 } while (window->current_line->line != NULL ||
486 window->current_line->parent->type != LINE_TEXT);
489 static void
490 cmd_move_beginning_of_line(struct window *window)
492 window->cpoff = 0;
493 restore_cursor(window);
496 static void
497 cmd_move_end_of_line(struct window *window)
499 struct vline *vl;
501 vl = window->current_line;
502 if (vl->line == NULL)
503 return;
504 window->cpoff = utf8_cplen(vl->line);
505 restore_cursor(window);
508 static void
509 cmd_redraw(struct window *window)
511 handle_resize(0, 0, NULL);
514 static void
515 cmd_scroll_line_up(struct window *window)
517 struct vline *vl;
519 if (window->line_off == 0)
520 return;
522 vl = nth_line(window, --window->line_off);
523 wscrl(body, -1);
524 wmove(body, 0, 0);
525 print_vline(body, vl);
527 window->current_line = TAILQ_PREV(window->current_line, vhead, vlines);
528 restore_cursor(window);
531 static void
532 cmd_scroll_line_down(struct window *window)
534 struct vline *vl;
536 vl = window->current_line;
537 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
538 return;
539 window->current_line = vl;
541 window->line_off++;
542 wscrl(body, 1);
544 if (window->line_max - window->line_off < (size_t)body_lines)
545 return;
547 vl = nth_line(window, window->line_off + body_lines-1);
548 wmove(body, body_lines-1, 0);
549 print_vline(body, vl);
551 restore_cursor(window);
554 static void
555 cmd_scroll_up(struct window *window)
557 size_t off;
559 off = body_lines-1;
561 for (; off > 0; --off)
562 cmd_scroll_line_up(window);
565 static void
566 cmd_scroll_down(struct window *window)
568 size_t off;
570 off = body_lines-1;
572 for (; off > 0; --off)
573 cmd_scroll_line_down(window);
576 static void
577 cmd_beginning_of_buffer(struct window *window)
579 window->current_line = TAILQ_FIRST(&window->head);
580 window->line_off = 0;
581 window->curs_y = 0;
582 window->cpoff = 0;
583 restore_cursor(window);
586 static void
587 cmd_end_of_buffer(struct window *window)
589 ssize_t off;
591 off = window->line_max - body_lines;
592 off = MAX(0, off);
594 window->line_off = off;
595 window->curs_y = MIN((size_t)body_lines, window->line_max-1);
597 window->current_line = TAILQ_LAST(&window->head, vhead);
598 window->cpoff = body_cols;
599 restore_cursor(window);
602 static void
603 cmd_kill_telescope(struct window *window)
605 save_session();
606 event_loopbreak();
609 static void
610 cmd_push_button(struct window *window)
612 struct vline *vl;
613 size_t nth;
615 nth = window->line_off + window->curs_y;
616 if (nth >= window->line_max)
617 return;
618 vl = nth_line(window, nth);
619 if (vl->parent->type != LINE_LINK)
620 return;
622 load_url_in_tab(current_tab(), vl->parent->alt);
625 static void
626 cmd_push_button_new_tab(struct window *window)
628 struct vline *vl;
629 size_t nth;
631 nth = window->line_off + window->curs_y;
632 if (nth > window->line_max)
633 return;
634 vl = nth_line(window, nth);
635 if (vl->parent->type != LINE_LINK)
636 return;
638 new_tab(vl->parent->alt);
641 static void
642 cmd_previous_button(struct window *window)
644 do {
645 if (window->current_line == NULL ||
646 window->current_line == TAILQ_FIRST(&window->head)) {
647 message("No previous link");
648 return;
650 cmd_previous_line(window);
651 } while (window->current_line->parent->type != LINE_LINK);
654 static void
655 cmd_next_button(struct window *window)
657 do {
658 if (window->current_line == NULL ||
659 window->current_line == TAILQ_LAST(&window->head, vhead)) {
660 message("No next link");
661 return;
663 cmd_next_line(window);
664 } while (window->current_line->parent->type != LINE_LINK);
667 static void
668 cmd_previous_page(struct window *window)
670 struct tab *tab = current_tab();
672 if (!load_previous_page(tab))
673 message("No previous page");
674 else
675 start_loading_anim(tab);
678 static void
679 cmd_next_page(struct window *window)
681 struct tab *tab = current_tab();
683 if (!load_next_page(tab))
684 message("No next page");
685 else
686 start_loading_anim(tab);
689 static void
690 cmd_clear_minibuf(struct window *window)
692 handle_clear_minibuf(0, 0, NULL);
695 static void
696 cmd_execute_extended_command(struct window *window)
698 size_t len;
700 if (in_minibuffer) {
701 message("We don't have enable-recursive-minibuffers");
702 return;
705 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
706 &eecmd_history);
708 len = sizeof(ministate.prompt);
709 strlcpy(ministate.prompt, "", len);
711 if (thiskey.meta)
712 strlcat(ministate.prompt, "M-", len);
714 strlcat(ministate.prompt, keyname(thiskey.key), len);
716 if (thiskey.meta)
717 strlcat(ministate.prompt, " ", len);
720 static void
721 cmd_tab_close(struct window *window)
723 struct tab *tab, *t;
725 tab = current_tab();
726 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
727 TAILQ_NEXT(tab, tabs) == NULL) {
728 message("Can't close the only tab.");
729 return;
732 if (evtimer_pending(&tab->loadingev, NULL))
733 evtimer_del(&tab->loadingev);
735 stop_tab(tab);
737 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
738 t = TAILQ_NEXT(tab, tabs);
739 TAILQ_REMOVE(&tabshead, tab, tabs);
740 free(tab);
742 switch_to_tab(t);
745 static void
746 cmd_tab_close_other(struct window *window)
748 struct tab *t, *i;
750 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
751 if (t->flags & TAB_CURRENT)
752 continue;
754 stop_tab(t);
755 TAILQ_REMOVE(&tabshead, t, tabs);
756 free(t);
760 static void
761 cmd_tab_new(struct window *window)
763 new_tab(NEW_TAB_URL);
766 static void
767 cmd_tab_next(struct window *window)
769 struct tab *tab, *t;
771 tab = current_tab();
772 tab->flags &= ~TAB_CURRENT;
774 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
775 t = TAILQ_FIRST(&tabshead);
776 t->flags |= TAB_CURRENT;
779 static void
780 cmd_tab_previous(struct window *window)
782 struct tab *tab, *t;
784 tab = current_tab();
785 tab->flags &= ~TAB_CURRENT;
787 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
788 t = TAILQ_LAST(&tabshead, tabshead);
789 t->flags |= TAB_CURRENT;
792 static void
793 cmd_tab_move(struct window *window)
795 struct tab *tab, *t;
797 tab = current_tab();
798 t = TAILQ_NEXT(tab, tabs);
799 TAILQ_REMOVE(&tabshead, tab, tabs);
801 if (t == NULL)
802 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
803 else
804 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
807 static void
808 cmd_tab_move_to(struct window *window)
810 struct tab *tab, *t;
812 tab = current_tab();
813 t = TAILQ_PREV(tab, tabshead, tabs);
814 TAILQ_REMOVE(&tabshead, tab, tabs);
816 if (t == NULL) {
817 if (TAILQ_EMPTY(&tabshead))
818 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
819 else
820 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
821 } else
822 TAILQ_INSERT_BEFORE(t, tab, tabs);
825 static void
826 cmd_load_url(struct window *window)
828 if (in_minibuffer) {
829 message("We don't have enable-recursive-minibuffers");
830 return;
833 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
834 &lu_history);
835 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
836 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
837 cmd_move_end_of_line(&ministate.window);
840 static void
841 cmd_load_current_url(struct window *window)
843 struct tab *tab = current_tab();
845 if (in_minibuffer) {
846 message("We don't have enable-recursive-minibuffers");
847 return;
850 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
851 &lu_history);
852 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
853 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
854 ministate.window.cpoff = utf8_cplen(ministate.buf);
857 static void
858 cmd_bookmark_page(struct window *window)
860 struct tab *tab = current_tab();
862 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
863 strlcpy(ministate.prompt, "Bookmark 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_list_bookmarks(struct window *window)
871 load_url_in_tab(current_tab(), "about:bookmarks");
874 static void
875 cmd_toggle_help(struct window *window)
877 side_window = !side_window;
878 if (side_window)
879 recompute_help();
881 /* ugly hack, but otherwise the window doesn't get updated
882 * until I call handle_resize a second time (i.e. C-l). I
883 * will be happy to know why something like this is needed. */
884 handle_resize(0, 0, NULL);
885 handle_resize(0, 0, NULL);
888 static void
889 cmd_mini_delete_char(struct window *window)
891 char *c, *n;
893 if (!in_minibuffer) {
894 message("text is read-only");
895 return;
898 minibuffer_taint_hist();
900 c = utf8_nth(window->current_line->line, window->cpoff);
901 if (*c == '\0')
902 return;
903 n = utf8_next_cp(c);
905 memmove(c, n, strlen(n)+1);
908 static void
909 cmd_mini_delete_backward_char(struct window *window)
911 char *c, *p, *start;
913 if (!in_minibuffer) {
914 message("text is read-only");
915 return;
918 minibuffer_taint_hist();
920 c = utf8_nth(window->current_line->line, window->cpoff);
921 start = window->current_line->line;
922 if (c == start)
923 return;
924 p = utf8_prev_cp(c-1, start);
926 memmove(p, c, strlen(c)+1);
927 window->cpoff--;
930 static void
931 cmd_mini_kill_line(struct window *window)
933 char *c;
935 if (!in_minibuffer) {
936 message("text is read-only");
937 return;
940 minibuffer_taint_hist();
941 c = utf8_nth(window->current_line->line, window->cpoff);
942 *c = '\0';
945 static void
946 cmd_mini_abort(struct window *window)
948 if (!in_minibuffer)
949 return;
951 ministate.abortfn();
954 static void
955 cmd_mini_complete_and_exit(struct window *window)
957 if (!in_minibuffer)
958 return;
960 minibuffer_taint_hist();
961 ministate.donefn();
964 static void
965 cmd_mini_previous_history_element(struct window *window)
967 if (ministate.history == NULL) {
968 message("No history");
969 return;
972 if (ministate.hist_cur == NULL ||
973 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
974 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
975 ministate.hist_off = ministate.history->len - 1;
976 if (ministate.hist_cur == NULL)
977 message("No prev item");
978 } else {
979 ministate.hist_off--;
982 if (ministate.hist_cur != NULL)
983 window->current_line->line = ministate.hist_cur->h;
986 static void
987 cmd_mini_next_history_element(struct window *window)
989 if (ministate.history == NULL) {
990 message("No history");
991 return;
994 if (ministate.hist_cur == NULL ||
995 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
996 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
997 ministate.hist_off = 0;
998 if (ministate.hist_cur == NULL)
999 message("No next item");
1000 } else {
1001 ministate.hist_off++;
1004 if (ministate.hist_cur != NULL)
1005 window->current_line->line = ministate.hist_cur->h;
1008 static void
1009 global_key_unbound(void)
1011 message("%s is undefined", keybuf);
1014 static void
1015 minibuffer_hist_save_entry(void)
1017 struct hist *hist;
1019 if (ministate.history == NULL)
1020 return;
1022 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1023 abort();
1025 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1027 if (TAILQ_EMPTY(&ministate.history->head))
1028 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1029 else
1030 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1031 ministate.history->len++;
1035 * taint the minibuffer cache: if we're currently showing a history
1036 * element, copy that to the current buf and reset the "history
1037 * navigation" thing.
1039 static void
1040 minibuffer_taint_hist(void)
1042 if (ministate.hist_cur == NULL)
1043 return;
1045 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1046 ministate.hist_cur = NULL;
1049 static void
1050 minibuffer_self_insert(void)
1052 char *c, tmp[5] = {0};
1053 size_t len;
1055 minibuffer_taint_hist();
1057 if (thiskey.cp == 0)
1058 return;
1060 len = utf8_encode(thiskey.cp, tmp);
1061 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1062 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1063 return;
1065 memmove(c + len, c, strlen(c)+1);
1066 memcpy(c, tmp, len);
1067 ministate.window.cpoff++;
1070 static void
1071 eecmd_self_insert(void)
1073 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1074 !unicode_isgraph(thiskey.cp)) {
1075 global_key_unbound();
1076 return;
1079 minibuffer_self_insert();
1082 static void
1083 eecmd_select(void)
1085 struct cmds *cmd;
1087 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1088 if (!strcmp(cmd->cmd, ministate.buf)) {
1089 exit_minibuffer();
1090 minibuffer_hist_save_entry();
1091 cmd->fn(current_window());
1092 return;
1096 message("No match");
1099 static void
1100 ir_self_insert(void)
1102 minibuffer_self_insert();
1105 static void
1106 ir_select(void)
1108 char buf[1025] = {0};
1109 struct phos_uri uri;
1110 struct tab *tab;
1112 tab = current_tab();
1114 exit_minibuffer();
1115 minibuffer_hist_save_entry();
1117 /* a bit ugly but... */
1118 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1119 phos_uri_set_query(&uri, ministate.buf);
1120 phos_serialize_uri(&uri, buf, sizeof(buf));
1121 load_url_in_tab(tab, buf);
1124 static void
1125 lu_self_insert(void)
1127 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1128 !unicode_isgraph(thiskey.key)) {
1129 global_key_unbound();
1130 return;
1133 minibuffer_self_insert();
1136 static void
1137 lu_select(void)
1139 exit_minibuffer();
1140 minibuffer_hist_save_entry();
1141 load_url_in_tab(current_tab(), ministate.buf);
1144 static void
1145 bp_select(void)
1147 exit_minibuffer();
1148 if (*ministate.buf != '\0')
1149 add_to_bookmarks(ministate.buf);
1150 else
1151 message("Abort.");
1154 static void
1155 yornp_self_insert(void)
1157 if (thiskey.key != 'y' && thiskey.key != 'n') {
1158 message("Please answer y or n");
1159 return;
1162 exit_minibuffer();
1163 yornp_cb(thiskey.key == 'y', yornp_data);
1166 static void
1167 yornp_abort(void)
1169 exit_minibuffer();
1170 yornp_cb(0, yornp_data);
1173 static struct vline *
1174 nth_line(struct window *window, size_t n)
1176 struct vline *vl;
1177 size_t i;
1179 i = 0;
1180 TAILQ_FOREACH(vl, &window->head, vlines) {
1181 if (i == n)
1182 return vl;
1183 i++;
1186 /* unreachable */
1187 abort();
1190 static struct tab *
1191 current_tab(void)
1193 struct tab *t;
1195 TAILQ_FOREACH(t, &tabshead, tabs) {
1196 if (t->flags & TAB_CURRENT)
1197 return t;
1200 /* unreachable */
1201 abort();
1204 static struct window *
1205 current_window(void)
1207 if (in_minibuffer)
1208 return &ministate.window;
1209 return &current_tab()->window;
1212 static int
1213 readkey(void)
1215 uint32_t state = 0;
1217 if ((thiskey.key = wgetch(body)) == ERR)
1218 return 0;
1220 thiskey.meta = thiskey.key == 27;
1221 if (thiskey.meta) {
1222 thiskey.key = wgetch(body);
1223 if (thiskey.key == ERR || thiskey.key == 27) {
1224 thiskey.meta = 0;
1225 thiskey.key = 27;
1229 thiskey.cp = 0;
1230 if ((unsigned int)thiskey.key < UINT8_MAX) {
1231 while (1) {
1232 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1233 break;
1234 if ((thiskey.key = wgetch(body)) == ERR) {
1235 message("Error decoding user input");
1236 return 0;
1241 return 1;
1244 static void
1245 dispatch_stdio(int fd, short ev, void *d)
1247 struct keymap *k;
1248 const char *keyname;
1249 char tmp[5] = {0};
1251 if (!readkey())
1252 return;
1254 if (keybuf[0] != '\0')
1255 strlcat(keybuf, " ", sizeof(keybuf));
1256 if (thiskey.meta)
1257 strlcat(keybuf, "M-", sizeof(keybuf));
1258 if (thiskey.cp != 0) {
1259 utf8_encode(thiskey.cp, tmp);
1260 strlcat(keybuf, tmp, sizeof(keybuf));
1261 } else {
1262 if ((keyname = unkbd(thiskey.key)) != NULL)
1263 strlcat(keybuf, keyname, sizeof(keybuf));
1264 else {
1265 tmp[0] = thiskey.key;
1266 strlcat(keybuf, tmp, sizeof(keybuf));
1270 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1271 if (k->meta == thiskey.meta &&
1272 k->key == thiskey.key) {
1273 if (k->fn == NULL)
1274 current_map = &k->map;
1275 else {
1276 current_map = base_map;
1277 strlcpy(keybuf, "", sizeof(keybuf));
1278 k->fn(current_window());
1280 goto done;
1284 if (current_map->unhandled_input != NULL)
1285 current_map->unhandled_input();
1286 else {
1287 global_key_unbound();
1290 strlcpy(keybuf, "", sizeof(keybuf));
1291 current_map = base_map;
1293 done:
1294 if (side_window)
1295 recompute_help();
1297 redraw_tab(current_tab());
1300 static void
1301 handle_clear_minibuf(int fd, short ev, void *d)
1303 free(ministate.curmesg);
1304 ministate.curmesg = NULL;
1306 redraw_minibuffer();
1307 if (in_minibuffer) {
1308 wrefresh(body);
1309 wrefresh(minibuf);
1310 } else {
1311 wrefresh(minibuf);
1312 wrefresh(body);
1316 static void
1317 handle_resize(int sig, short ev, void *d)
1319 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1320 event_del(&resizeev);
1322 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1323 evtimer_add(&resizeev, &resize_timer);
1326 static void
1327 handle_resize_timeout(int s, short ev, void *d)
1329 struct tab *tab;
1331 endwin();
1332 refresh();
1333 clear();
1335 /* move and resize the windows, in reverse order! */
1337 mvwin(minibuf, LINES-1, 0);
1338 wresize(minibuf, 1, COLS);
1340 mvwin(modeline, LINES-2, 0);
1341 wresize(modeline, 1, COLS);
1343 body_lines = LINES-3;
1344 body_cols = COLS;
1346 if (side_window) {
1347 help_cols = 0.3 * COLS;
1348 help_lines = LINES-3;
1349 mvwin(help, 1, 0);
1350 wresize(help, help_lines, help_cols);
1352 wrap_page(&helpwin, help_cols);
1354 body_cols = COLS - help_cols - 1;
1355 mvwin(body, 1, help_cols);
1356 } else
1357 mvwin(body, 1, 0);
1359 wresize(body, body_lines, body_cols);
1361 wresize(tabline, 1, COLS);
1363 tab = current_tab();
1365 wrap_page(&tab->window, body_cols);
1366 redraw_tab(tab);
1369 static int
1370 wrap_page(struct window *window, int width)
1372 struct line *l;
1373 const struct line *orig;
1374 struct vline *vl;
1375 const char *prfx;
1377 orig = window->current_line == NULL
1378 ? NULL
1379 : window->current_line->parent;
1380 window->current_line = NULL;
1382 window->curs_y = 0;
1383 window->line_off = 0;
1385 empty_vlist(window);
1387 TAILQ_FOREACH(l, &window->page.head, lines) {
1388 prfx = line_prefixes[l->type].prfx1;
1389 switch (l->type) {
1390 case LINE_TEXT:
1391 case LINE_LINK:
1392 case LINE_TITLE_1:
1393 case LINE_TITLE_2:
1394 case LINE_TITLE_3:
1395 case LINE_ITEM:
1396 case LINE_QUOTE:
1397 case LINE_PRE_START:
1398 case LINE_PRE_END:
1399 wrap_text(window, prfx, l, width);
1400 break;
1401 case LINE_PRE_CONTENT:
1402 hardwrap_text(window, l, width);
1403 break;
1406 if (orig == l && window->current_line == NULL) {
1407 window->line_off = window->line_max-1;
1408 window->current_line = TAILQ_LAST(&window->head, vhead);
1410 while (1) {
1411 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1412 if (vl == NULL || vl->parent != orig)
1413 break;
1414 window->current_line = vl;
1415 window->line_off--;
1420 if (window->current_line == NULL)
1421 window->current_line = TAILQ_FIRST(&window->head);
1423 return 1;
1426 static void
1427 print_vline(WINDOW *window, struct vline *vl)
1429 const char *text = vl->line;
1430 const char *prfx;
1431 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1432 int text_face = line_faces[vl->parent->type].text_prop;
1434 if (!vl->flags)
1435 prfx = line_prefixes[vl->parent->type].prfx1;
1436 else
1437 prfx = line_prefixes[vl->parent->type].prfx2;
1439 if (text == NULL)
1440 text = "";
1442 wattron(window, prefix_face);
1443 wprintw(window, "%s", prfx);
1444 wattroff(window, prefix_face);
1446 wattron(window, text_face);
1447 wprintw(window, "%s", text);
1448 wattroff(window, text_face);
1451 static void
1452 redraw_tabline(void)
1454 struct tab *tab;
1455 size_t toskip, ots, tabwidth, space, x;
1456 int current, y, truncated;
1457 const char *title;
1458 char buf[25];
1460 tabwidth = sizeof(buf)+1;
1461 space = COLS-2;
1463 toskip = 0;
1464 TAILQ_FOREACH(tab, &tabshead, tabs) {
1465 toskip++;
1466 if (tab->flags & TAB_CURRENT)
1467 break;
1470 if (toskip * tabwidth < space)
1471 toskip = 0;
1472 else {
1473 ots = toskip;
1474 toskip--;
1475 while (toskip != 0 &&
1476 (ots - toskip+1) * tabwidth < space)
1477 toskip--;
1480 werase(tabline);
1481 wattron(tabline, tab_face.background);
1482 wprintw(tabline, toskip == 0 ? " " : "<");
1483 wattroff(tabline, tab_face.background);
1485 truncated = 0;
1486 TAILQ_FOREACH(tab, &tabshead, tabs) {
1487 if (truncated)
1488 break;
1489 if (toskip != 0) {
1490 toskip--;
1491 continue;
1494 getyx(tabline, y, x);
1495 if (x + sizeof(buf)+2 >= (size_t)COLS)
1496 truncated = 1;
1498 current = tab->flags & TAB_CURRENT;
1500 if (*(title = tab->window.page.title) == '\0')
1501 title = tab->hist_cur->h;
1503 strlcpy(buf, " ", sizeof(buf));
1504 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1505 /* truncation happens */
1506 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1507 } else {
1508 /* pad with spaces */
1509 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1510 /* nop */ ;
1513 if (current)
1514 wattron(tabline, tab_face.current_tab);
1515 else
1516 wattron(tabline, tab_face.tab);
1518 wprintw(tabline, "%s", buf);
1519 if (TAILQ_NEXT(tab, tabs) != NULL)
1520 wprintw(tabline, " ");
1522 if (current)
1523 wattroff(tabline, tab_face.current_tab);
1524 else
1525 wattroff(tabline, tab_face.tab);
1528 wattron(tabline, tab_face.background);
1529 for (; x < (size_t)COLS; ++x)
1530 waddch(tabline, ' ');
1531 if (truncated)
1532 mvwprintw(tabline, 0, COLS-1, ">");
1535 static void
1536 redraw_window(WINDOW *win, int height, struct window *window)
1538 struct vline *vl;
1539 int l;
1541 werase(win);
1543 window->line_off = MIN(window->line_max-1, window->line_off);
1544 if (TAILQ_EMPTY(&window->head))
1545 return;
1547 l = 0;
1548 vl = nth_line(window, window->line_off);
1549 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1550 wmove(win, l, 0);
1551 print_vline(win, vl);
1552 l++;
1553 if (l == height)
1554 break;
1557 wmove(win, window->curs_y, window->curs_x);
1560 static void
1561 redraw_help(void)
1563 redraw_window(help, help_lines, &helpwin);
1566 static void
1567 redraw_body(struct tab *tab)
1569 redraw_window(body, body_lines, &tab->window);
1572 static inline char
1573 trust_status_char(enum trust_state ts)
1575 switch (ts) {
1576 case TS_UNKNOWN: return 'u';
1577 case TS_UNTRUSTED: return '!';
1578 case TS_TRUSTED: return 'v';
1579 case TS_VERIFIED: return 'V';
1583 static void
1584 redraw_modeline(struct tab *tab)
1586 double pct;
1587 int x, y, max_x, max_y;
1588 const char *mode = tab->window.page.name;
1589 const char *spin = "-\\|/";
1591 werase(modeline);
1592 wattron(modeline, A_REVERSE);
1593 wmove(modeline, 0, 0);
1595 wprintw(modeline, "-%c%c %s ",
1596 spin[tab->loading_anim_step],
1597 trust_status_char(tab->trust),
1598 mode == NULL ? "(none)" : mode);
1600 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1602 if (tab->window.line_max <= (size_t)body_lines)
1603 wprintw(modeline, "All ");
1604 else if (tab->window.line_off == 0)
1605 wprintw(modeline, "Top ");
1606 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1607 wprintw(modeline, "Bottom ");
1608 else
1609 wprintw(modeline, "%.0f%% ", pct);
1611 wprintw(modeline, "%d/%d %s ",
1612 tab->window.line_off + tab->window.curs_y,
1613 tab->window.line_max,
1614 tab->hist_cur->h);
1616 getyx(modeline, y, x);
1617 getmaxyx(modeline, max_y, max_x);
1619 (void)y;
1620 (void)max_y;
1622 for (; x < max_x; ++x)
1623 waddstr(modeline, "-");
1626 static void
1627 redraw_minibuffer(void)
1629 struct tab *tab;
1630 size_t off_y, off_x = 0;
1631 char *start, *c;
1633 werase(minibuf);
1635 if (in_minibuffer) {
1636 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1637 if (ministate.hist_cur != NULL)
1638 wprintw(minibuf, "(%zu/%zu) ",
1639 ministate.hist_off + 1,
1640 ministate.history->len);
1642 getyx(minibuf, off_y, off_x);
1644 start = ministate.hist_cur != NULL
1645 ? ministate.hist_cur->h
1646 : ministate.buf;
1647 c = utf8_nth(ministate.window.current_line->line,
1648 ministate.window.cpoff);
1649 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1650 start = utf8_next_cp(start);
1653 waddstr(minibuf, start);
1656 if (ministate.curmesg != NULL)
1657 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1658 ministate.curmesg);
1660 if (!in_minibuffer && ministate.curmesg == NULL)
1661 waddstr(minibuf, keybuf);
1663 /* If nothing else, show the URL at point */
1664 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1665 tab = current_tab();
1666 if (tab->window.current_line != NULL &&
1667 tab->window.current_line->parent->type == LINE_LINK)
1668 waddstr(minibuf, tab->window.current_line->parent->alt);
1671 if (in_minibuffer)
1672 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1675 static void
1676 redraw_tab(struct tab *tab)
1678 if (side_window) {
1679 redraw_help();
1680 wnoutrefresh(help);
1683 redraw_tabline();
1684 redraw_body(tab);
1685 redraw_modeline(tab);
1686 redraw_minibuffer();
1688 wnoutrefresh(tabline);
1689 wnoutrefresh(modeline);
1691 if (in_minibuffer) {
1692 wnoutrefresh(body);
1693 wnoutrefresh(minibuf);
1694 } else {
1695 wnoutrefresh(minibuf);
1696 wnoutrefresh(body);
1699 doupdate();
1702 static void
1703 emit_help_item(char *prfx, void *fn)
1705 struct line *l;
1706 struct cmds *cmd;
1708 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1709 if (fn == cmd->fn)
1710 break;
1712 assert(cmd != NULL);
1714 if ((l = calloc(1, sizeof(*l))) == NULL)
1715 abort();
1717 l->type = LINE_TEXT;
1718 l->alt = NULL;
1720 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1722 if (TAILQ_EMPTY(&helpwin.page.head))
1723 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1724 else
1725 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1728 static void
1729 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1731 struct keymap *k;
1732 char p[32];
1733 const char *kn;
1735 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1736 strlcpy(p, prfx, sizeof(p));
1737 if (*p != '\0')
1738 strlcat(p, " ", sizeof(p));
1739 if (k->meta)
1740 strlcat(p, "M-", sizeof(p));
1741 if ((kn = unkbd(k->key)) != NULL)
1742 strlcat(p, kn, sizeof(p));
1743 else
1744 strlcat(p, keyname(k->key), sizeof(p));
1746 if (k->fn == NULL)
1747 rec_compute_help(&k->map, p, sizeof(p));
1748 else
1749 emit_help_item(p, k->fn);
1753 static void
1754 recompute_help(void)
1756 char p[32] = { 0 };
1758 empty_vlist(&helpwin);
1759 empty_linelist(&helpwin);
1760 rec_compute_help(current_map, p, sizeof(p));
1761 wrap_page(&helpwin, help_cols);
1764 static void
1765 vmessage(const char *fmt, va_list ap)
1767 if (evtimer_pending(&clminibufev, NULL))
1768 evtimer_del(&clminibufev);
1769 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1770 evtimer_add(&clminibufev, &clminibufev_timer);
1772 free(ministate.curmesg);
1774 /* TODO: what to do if the allocation fails here? */
1775 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1776 ministate.curmesg = NULL;
1778 redraw_minibuffer();
1779 if (in_minibuffer) {
1780 wrefresh(body);
1781 wrefresh(minibuf);
1782 } else {
1783 wrefresh(minibuf);
1784 wrefresh(body);
1788 static void
1789 message(const char *fmt, ...)
1791 va_list ap;
1793 va_start(ap, fmt);
1794 vmessage(fmt, ap);
1795 va_end(ap);
1798 static void
1799 start_loading_anim(struct tab *tab)
1801 if (tab->loading_anim)
1802 return;
1803 tab->loading_anim = 1;
1804 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1805 evtimer_add(&tab->loadingev, &loadingev_timer);
1808 static void
1809 update_loading_anim(int fd, short ev, void *d)
1811 struct tab *tab = d;
1813 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1815 if (tab->flags & TAB_CURRENT) {
1816 redraw_modeline(tab);
1817 wrefresh(modeline);
1818 wrefresh(body);
1819 if (in_minibuffer)
1820 wrefresh(minibuf);
1823 evtimer_add(&tab->loadingev, &loadingev_timer);
1826 static void
1827 stop_loading_anim(struct tab *tab)
1829 if (!tab->loading_anim)
1830 return;
1831 evtimer_del(&tab->loadingev);
1832 tab->loading_anim = 0;
1833 tab->loading_anim_step = 0;
1835 if (!(tab->flags & TAB_CURRENT))
1836 return;
1838 redraw_modeline(tab);
1840 wrefresh(modeline);
1841 wrefresh(body);
1842 if (in_minibuffer)
1843 wrefresh(minibuf);
1846 static void
1847 load_url_in_tab(struct tab *tab, const char *url)
1849 message("Loading %s...", url);
1850 start_loading_anim(tab);
1851 load_url(tab, url);
1853 tab->window.curs_x = 0;
1854 tab->window.curs_y = 0;
1855 redraw_tab(tab);
1858 static void
1859 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1860 void (*abortfn)(void), struct histhead *hist)
1862 in_minibuffer = 1;
1863 base_map = &minibuffer_map;
1864 current_map = &minibuffer_map;
1866 base_map->unhandled_input = self_insert_fn;
1868 ministate.donefn = donefn;
1869 ministate.abortfn = abortfn;
1870 memset(ministate.buf, 0, sizeof(ministate.buf));
1871 ministate.window.current_line = &ministate.vline;
1872 ministate.window.current_line->line = ministate.buf;
1873 ministate.window.cpoff = 0;
1874 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1876 ministate.history = hist;
1877 ministate.hist_cur = NULL;
1878 ministate.hist_off = 0;
1881 static void
1882 exit_minibuffer(void)
1884 werase(minibuf);
1886 in_minibuffer = 0;
1887 base_map = &global_map;
1888 current_map = &global_map;
1891 static void
1892 switch_to_tab(struct tab *tab)
1894 struct tab *t;
1896 TAILQ_FOREACH(t, &tabshead, tabs) {
1897 t->flags &= ~TAB_CURRENT;
1900 tab->flags |= TAB_CURRENT;
1903 unsigned int
1904 tab_new_id(void)
1906 return tab_counter++;
1909 static struct tab *
1910 new_tab(const char *url)
1912 struct tab *tab;
1914 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1915 event_loopbreak();
1916 return NULL;
1919 TAILQ_INIT(&tab->hist.head);
1921 TAILQ_INIT(&tab->window.head);
1923 tab->id = tab_new_id();
1924 switch_to_tab(tab);
1926 if (TAILQ_EMPTY(&tabshead))
1927 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1928 else
1929 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1931 load_url_in_tab(tab, url);
1932 return tab;
1935 static void
1936 session_new_tab_cb(const char *url)
1938 new_tab(url);
1941 static void
1942 usage(void)
1944 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1947 int
1948 ui_init(int argc, char * const *argv)
1950 const char *url = NEW_TAB_URL;
1951 int ch;
1953 while ((ch = getopt(argc, argv, "")) != -1) {
1954 switch (ch) {
1955 default:
1956 usage();
1957 return 0;
1960 argc -= optind;
1961 argv += optind;
1963 if (argc != 0)
1964 url = argv[0];
1966 setlocale(LC_ALL, "");
1968 TAILQ_INIT(&global_map.m);
1969 global_map.unhandled_input = global_key_unbound;
1971 TAILQ_INIT(&minibuffer_map.m);
1973 TAILQ_INIT(&eecmd_history.head);
1974 TAILQ_INIT(&ir_history.head);
1975 TAILQ_INIT(&lu_history.head);
1977 ministate.line.type = LINE_TEXT;
1978 ministate.vline.parent = &ministate.line;
1979 ministate.window.current_line = &ministate.vline;
1981 /* initialize help window */
1982 TAILQ_INIT(&helpwin.head);
1984 base_map = &global_map;
1985 current_map = &global_map;
1986 load_default_keys();
1988 initscr();
1989 raw();
1990 noecho();
1992 nonl();
1993 intrflush(stdscr, FALSE);
1995 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1996 return 0;
1997 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1998 return 0;
1999 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2000 return 0;
2001 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2002 return 0;
2003 if ((help = newwin(1, 1, 1, 0)) == NULL)
2004 return 0;
2006 body_lines = LINES-3;
2007 body_cols = COLS;
2009 keypad(body, TRUE);
2010 scrollok(body, TRUE);
2012 /* non-blocking input */
2013 wtimeout(body, 0);
2015 mvwprintw(body, 0, 0, "");
2017 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2018 event_add(&stdioev, NULL);
2020 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2021 signal_add(&winchev, NULL);
2023 load_last_session(session_new_tab_cb);
2024 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2025 new_tab(url);
2027 return 1;
2030 void
2031 ui_on_tab_loaded(struct tab *tab)
2033 stop_loading_anim(tab);
2034 message("Loaded %s", tab->hist_cur->h);
2036 redraw_tabline();
2037 wrefresh(tabline);
2038 if (in_minibuffer)
2039 wrefresh(minibuf);
2040 else
2041 wrefresh(body);
2044 void
2045 ui_on_tab_refresh(struct tab *tab)
2047 wrap_page(&tab->window, body_cols);
2048 if (tab->flags & TAB_CURRENT) {
2049 restore_cursor(&tab->window);
2050 redraw_tab(tab);
2054 void
2055 ui_require_input(struct tab *tab, int hide)
2057 /* TODO: hard-switching to another tab is ugly */
2058 switch_to_tab(tab);
2060 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2061 &ir_history);
2062 strlcpy(ministate.prompt, "Input required: ",
2063 sizeof(ministate.prompt));
2064 redraw_tab(tab);
2067 void
2068 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2069 unsigned int data)
2071 size_t len;
2073 if (in_minibuffer) {
2074 fn(0, data);
2075 return;
2078 yornp_cb = fn;
2079 yornp_data = data;
2080 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2081 yornp_abort, NULL);
2083 len = sizeof(ministate.prompt);
2084 strlcpy(ministate.prompt, prompt, len);
2085 strlcat(ministate.prompt, " (y or n) ", len);
2086 redraw_tab(current_tab());
2089 void
2090 ui_notify(const char *fmt, ...)
2092 va_list ap;
2094 va_start(ap, fmt);
2095 vmessage(fmt, ap);
2096 va_end(ap);
2099 void
2100 ui_end(void)
2102 endwin();