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);
715 strlcat(ministate.prompt, " ", len);
718 static void
719 cmd_tab_close(struct window *window)
721 struct tab *tab, *t;
723 tab = current_tab();
724 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
725 TAILQ_NEXT(tab, tabs) == NULL) {
726 message("Can't close the only tab.");
727 return;
730 if (evtimer_pending(&tab->loadingev, NULL))
731 evtimer_del(&tab->loadingev);
733 stop_tab(tab);
735 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
736 t = TAILQ_NEXT(tab, tabs);
737 TAILQ_REMOVE(&tabshead, tab, tabs);
738 free(tab);
740 switch_to_tab(t);
743 static void
744 cmd_tab_close_other(struct window *window)
746 struct tab *t, *i;
748 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
749 if (t->flags & TAB_CURRENT)
750 continue;
752 stop_tab(t);
753 TAILQ_REMOVE(&tabshead, t, tabs);
754 free(t);
758 static void
759 cmd_tab_new(struct window *window)
761 new_tab(NEW_TAB_URL);
764 static void
765 cmd_tab_next(struct window *window)
767 struct tab *tab, *t;
769 tab = current_tab();
770 tab->flags &= ~TAB_CURRENT;
772 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
773 t = TAILQ_FIRST(&tabshead);
774 t->flags |= TAB_CURRENT;
777 static void
778 cmd_tab_previous(struct window *window)
780 struct tab *tab, *t;
782 tab = current_tab();
783 tab->flags &= ~TAB_CURRENT;
785 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
786 t = TAILQ_LAST(&tabshead, tabshead);
787 t->flags |= TAB_CURRENT;
790 static void
791 cmd_tab_move(struct window *window)
793 struct tab *tab, *t;
795 tab = current_tab();
796 t = TAILQ_NEXT(tab, tabs);
797 TAILQ_REMOVE(&tabshead, tab, tabs);
799 if (t == NULL)
800 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
801 else
802 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
805 static void
806 cmd_tab_move_to(struct window *window)
808 struct tab *tab, *t;
810 tab = current_tab();
811 t = TAILQ_PREV(tab, tabshead, tabs);
812 TAILQ_REMOVE(&tabshead, tab, tabs);
814 if (t == NULL) {
815 if (TAILQ_EMPTY(&tabshead))
816 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
817 else
818 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
819 } else
820 TAILQ_INSERT_BEFORE(t, tab, tabs);
823 static void
824 cmd_load_url(struct window *window)
826 if (in_minibuffer) {
827 message("We don't have enable-recursive-minibuffers");
828 return;
831 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
832 &lu_history);
833 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
834 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
835 cmd_move_end_of_line(&ministate.window);
838 static void
839 cmd_load_current_url(struct window *window)
841 struct tab *tab = current_tab();
843 if (in_minibuffer) {
844 message("We don't have enable-recursive-minibuffers");
845 return;
848 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
849 &lu_history);
850 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
851 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
852 ministate.window.cpoff = utf8_cplen(ministate.buf);
855 static void
856 cmd_bookmark_page(struct window *window)
858 struct tab *tab = current_tab();
860 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
861 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
862 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
863 ministate.window.cpoff = utf8_cplen(ministate.buf);
866 static void
867 cmd_list_bookmarks(struct window *window)
869 load_url_in_tab(current_tab(), "about:bookmarks");
872 static void
873 cmd_toggle_help(struct window *window)
875 side_window = !side_window;
876 if (side_window)
877 recompute_help();
879 /* ugly hack, but otherwise the window doesn't get updated
880 * until I call handle_resize a second time (i.e. C-l). I
881 * will be happy to know why something like this is needed. */
882 handle_resize(0, 0, NULL);
883 handle_resize(0, 0, NULL);
886 static void
887 cmd_mini_delete_char(struct window *window)
889 char *c, *n;
891 if (!in_minibuffer) {
892 message("text is read-only");
893 return;
896 minibuffer_taint_hist();
898 c = utf8_nth(window->current_line->line, window->cpoff);
899 if (*c == '\0')
900 return;
901 n = utf8_next_cp(c);
903 memmove(c, n, strlen(n)+1);
906 static void
907 cmd_mini_delete_backward_char(struct window *window)
909 char *c, *p, *start;
911 if (!in_minibuffer) {
912 message("text is read-only");
913 return;
916 minibuffer_taint_hist();
918 c = utf8_nth(window->current_line->line, window->cpoff);
919 start = window->current_line->line;
920 if (c == start)
921 return;
922 p = utf8_prev_cp(c-1, start);
924 memmove(p, c, strlen(c)+1);
925 window->cpoff--;
928 static void
929 cmd_mini_kill_line(struct window *window)
931 char *c;
933 if (!in_minibuffer) {
934 message("text is read-only");
935 return;
938 minibuffer_taint_hist();
939 c = utf8_nth(window->current_line->line, window->cpoff);
940 *c = '\0';
943 static void
944 cmd_mini_abort(struct window *window)
946 if (!in_minibuffer)
947 return;
949 ministate.abortfn();
952 static void
953 cmd_mini_complete_and_exit(struct window *window)
955 if (!in_minibuffer)
956 return;
958 minibuffer_taint_hist();
959 ministate.donefn();
962 static void
963 cmd_mini_previous_history_element(struct window *window)
965 if (ministate.history == NULL) {
966 message("No history");
967 return;
970 if (ministate.hist_cur == NULL ||
971 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
972 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
973 ministate.hist_off = ministate.history->len - 1;
974 if (ministate.hist_cur == NULL)
975 message("No prev item");
976 } else {
977 ministate.hist_off--;
980 if (ministate.hist_cur != NULL)
981 window->current_line->line = ministate.hist_cur->h;
984 static void
985 cmd_mini_next_history_element(struct window *window)
987 if (ministate.history == NULL) {
988 message("No history");
989 return;
992 if (ministate.hist_cur == NULL ||
993 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
994 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
995 ministate.hist_off = 0;
996 if (ministate.hist_cur == NULL)
997 message("No next item");
998 } else {
999 ministate.hist_off++;
1002 if (ministate.hist_cur != NULL)
1003 window->current_line->line = ministate.hist_cur->h;
1006 static void
1007 global_key_unbound(void)
1009 message("%s is undefined", keybuf);
1012 static void
1013 minibuffer_hist_save_entry(void)
1015 struct hist *hist;
1017 if (ministate.history == NULL)
1018 return;
1020 if ((hist = calloc(1, sizeof(*hist))) == NULL)
1021 abort();
1023 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
1025 if (TAILQ_EMPTY(&ministate.history->head))
1026 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
1027 else
1028 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
1029 ministate.history->len++;
1033 * taint the minibuffer cache: if we're currently showing a history
1034 * element, copy that to the current buf and reset the "history
1035 * navigation" thing.
1037 static void
1038 minibuffer_taint_hist(void)
1040 if (ministate.hist_cur == NULL)
1041 return;
1043 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1044 ministate.hist_cur = NULL;
1047 static void
1048 minibuffer_self_insert(void)
1050 char *c, tmp[5] = {0};
1051 size_t len;
1053 minibuffer_taint_hist();
1055 if (thiskey.cp == 0)
1056 return;
1058 len = utf8_encode(thiskey.cp, tmp);
1059 c = utf8_nth(ministate.window.current_line->line, ministate.window.cpoff);
1060 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1061 return;
1063 memmove(c + len, c, strlen(c)+1);
1064 memcpy(c, tmp, len);
1065 ministate.window.cpoff++;
1068 static void
1069 eecmd_self_insert(void)
1071 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1072 !unicode_isgraph(thiskey.cp)) {
1073 global_key_unbound();
1074 return;
1077 minibuffer_self_insert();
1080 static void
1081 eecmd_select(void)
1083 struct cmds *cmd;
1085 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1086 if (!strcmp(cmd->cmd, ministate.buf)) {
1087 exit_minibuffer();
1088 minibuffer_hist_save_entry();
1089 cmd->fn(current_window());
1090 return;
1094 message("No match");
1097 static void
1098 ir_self_insert(void)
1100 minibuffer_self_insert();
1103 static void
1104 ir_select(void)
1106 char buf[1025] = {0};
1107 struct phos_uri uri;
1108 struct tab *tab;
1110 tab = current_tab();
1112 exit_minibuffer();
1113 minibuffer_hist_save_entry();
1115 /* a bit ugly but... */
1116 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1117 phos_uri_set_query(&uri, ministate.buf);
1118 phos_serialize_uri(&uri, buf, sizeof(buf));
1119 load_url_in_tab(tab, buf);
1122 static void
1123 lu_self_insert(void)
1125 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1126 !unicode_isgraph(thiskey.key)) {
1127 global_key_unbound();
1128 return;
1131 minibuffer_self_insert();
1134 static void
1135 lu_select(void)
1137 exit_minibuffer();
1138 minibuffer_hist_save_entry();
1139 load_url_in_tab(current_tab(), ministate.buf);
1142 static void
1143 bp_select(void)
1145 exit_minibuffer();
1146 if (*ministate.buf != '\0')
1147 add_to_bookmarks(ministate.buf);
1148 else
1149 message("Abort.");
1152 static void
1153 yornp_self_insert(void)
1155 if (thiskey.key != 'y' && thiskey.key != 'n') {
1156 message("Please answer y or n");
1157 return;
1160 exit_minibuffer();
1161 yornp_cb(thiskey.key == 'y', yornp_data);
1164 static void
1165 yornp_abort(void)
1167 exit_minibuffer();
1168 yornp_cb(0, yornp_data);
1171 static struct vline *
1172 nth_line(struct window *window, size_t n)
1174 struct vline *vl;
1175 size_t i;
1177 i = 0;
1178 TAILQ_FOREACH(vl, &window->head, vlines) {
1179 if (i == n)
1180 return vl;
1181 i++;
1184 /* unreachable */
1185 abort();
1188 static struct tab *
1189 current_tab(void)
1191 struct tab *t;
1193 TAILQ_FOREACH(t, &tabshead, tabs) {
1194 if (t->flags & TAB_CURRENT)
1195 return t;
1198 /* unreachable */
1199 abort();
1202 static struct window *
1203 current_window(void)
1205 if (in_minibuffer)
1206 return &ministate.window;
1207 return &current_tab()->window;
1210 static int
1211 readkey(void)
1213 uint32_t state = 0;
1215 if ((thiskey.key = wgetch(body)) == ERR)
1216 return 0;
1218 thiskey.meta = thiskey.key == 27;
1219 if (thiskey.meta) {
1220 thiskey.key = wgetch(body);
1221 if (thiskey.key == ERR || thiskey.key == 27) {
1222 thiskey.meta = 0;
1223 thiskey.key = 27;
1227 thiskey.cp = 0;
1228 if ((unsigned int)thiskey.key < UINT8_MAX) {
1229 while (1) {
1230 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1231 break;
1232 if ((thiskey.key = wgetch(body)) == ERR) {
1233 message("Error decoding user input");
1234 return 0;
1239 return 1;
1242 static void
1243 dispatch_stdio(int fd, short ev, void *d)
1245 struct keymap *k;
1246 const char *keyname;
1247 char tmp[5] = {0};
1249 if (!readkey())
1250 return;
1252 if (keybuf[0] != '\0')
1253 strlcat(keybuf, " ", sizeof(keybuf));
1254 if (thiskey.meta)
1255 strlcat(keybuf, "M-", sizeof(keybuf));
1256 if (thiskey.cp != 0) {
1257 utf8_encode(thiskey.cp, tmp);
1258 strlcat(keybuf, tmp, sizeof(keybuf));
1259 } else {
1260 if ((keyname = unkbd(thiskey.key)) != NULL)
1261 strlcat(keybuf, keyname, sizeof(keybuf));
1262 else {
1263 tmp[0] = thiskey.key;
1264 strlcat(keybuf, tmp, sizeof(keybuf));
1268 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1269 if (k->meta == thiskey.meta &&
1270 k->key == thiskey.key) {
1271 if (k->fn == NULL)
1272 current_map = &k->map;
1273 else {
1274 current_map = base_map;
1275 strlcpy(keybuf, "", sizeof(keybuf));
1276 k->fn(current_window());
1278 goto done;
1282 if (current_map->unhandled_input != NULL)
1283 current_map->unhandled_input();
1284 else {
1285 global_key_unbound();
1288 strlcpy(keybuf, "", sizeof(keybuf));
1289 current_map = base_map;
1291 done:
1292 if (side_window)
1293 recompute_help();
1295 redraw_tab(current_tab());
1298 static void
1299 handle_clear_minibuf(int fd, short ev, void *d)
1301 free(ministate.curmesg);
1302 ministate.curmesg = NULL;
1304 redraw_minibuffer();
1305 if (in_minibuffer) {
1306 wrefresh(body);
1307 wrefresh(minibuf);
1308 } else {
1309 wrefresh(minibuf);
1310 wrefresh(body);
1314 static void
1315 handle_resize(int sig, short ev, void *d)
1317 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1318 event_del(&resizeev);
1320 evtimer_set(&resizeev, handle_resize_timeout, NULL);
1321 evtimer_add(&resizeev, &resize_timer);
1324 static void
1325 handle_resize_timeout(int s, short ev, void *d)
1327 struct tab *tab;
1329 endwin();
1330 refresh();
1331 clear();
1333 /* move and resize the windows, in reverse order! */
1335 mvwin(minibuf, LINES-1, 0);
1336 wresize(minibuf, 1, COLS);
1338 mvwin(modeline, LINES-2, 0);
1339 wresize(modeline, 1, COLS);
1341 body_lines = LINES-3;
1342 body_cols = COLS;
1344 if (side_window) {
1345 help_cols = 0.3 * COLS;
1346 help_lines = LINES-3;
1347 mvwin(help, 1, 0);
1348 wresize(help, help_lines, help_cols);
1350 wrap_page(&helpwin, help_cols);
1352 body_cols = COLS - help_cols - 1;
1353 mvwin(body, 1, help_cols);
1354 } else
1355 mvwin(body, 1, 0);
1357 wresize(body, body_lines, body_cols);
1359 wresize(tabline, 1, COLS);
1361 tab = current_tab();
1363 wrap_page(&tab->window, body_cols);
1364 redraw_tab(tab);
1367 static int
1368 wrap_page(struct window *window, int width)
1370 struct line *l;
1371 const struct line *orig;
1372 struct vline *vl;
1373 const char *prfx;
1375 orig = window->current_line == NULL
1376 ? NULL
1377 : window->current_line->parent;
1378 window->current_line = NULL;
1380 window->curs_y = 0;
1381 window->line_off = 0;
1383 empty_vlist(window);
1385 TAILQ_FOREACH(l, &window->page.head, lines) {
1386 prfx = line_prefixes[l->type].prfx1;
1387 switch (l->type) {
1388 case LINE_TEXT:
1389 case LINE_LINK:
1390 case LINE_TITLE_1:
1391 case LINE_TITLE_2:
1392 case LINE_TITLE_3:
1393 case LINE_ITEM:
1394 case LINE_QUOTE:
1395 case LINE_PRE_START:
1396 case LINE_PRE_END:
1397 wrap_text(window, prfx, l, width);
1398 break;
1399 case LINE_PRE_CONTENT:
1400 hardwrap_text(window, l, width);
1401 break;
1404 if (orig == l && window->current_line == NULL) {
1405 window->line_off = window->line_max-1;
1406 window->current_line = TAILQ_LAST(&window->head, vhead);
1408 while (1) {
1409 vl = TAILQ_PREV(window->current_line, vhead, vlines);
1410 if (vl == NULL || vl->parent != orig)
1411 break;
1412 window->current_line = vl;
1413 window->line_off--;
1418 if (window->current_line == NULL)
1419 window->current_line = TAILQ_FIRST(&window->head);
1421 return 1;
1424 static void
1425 print_vline(WINDOW *window, struct vline *vl)
1427 const char *text = vl->line;
1428 const char *prfx;
1429 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1430 int text_face = line_faces[vl->parent->type].text_prop;
1432 if (!vl->flags)
1433 prfx = line_prefixes[vl->parent->type].prfx1;
1434 else
1435 prfx = line_prefixes[vl->parent->type].prfx2;
1437 if (text == NULL)
1438 text = "";
1440 wattron(window, prefix_face);
1441 wprintw(window, "%s", prfx);
1442 wattroff(window, prefix_face);
1444 wattron(window, text_face);
1445 wprintw(window, "%s", text);
1446 wattroff(window, text_face);
1449 static void
1450 redraw_tabline(void)
1452 struct tab *tab;
1453 size_t toskip, ots, tabwidth, space, x;
1454 int current, y, truncated;
1455 const char *title;
1456 char buf[25];
1458 tabwidth = sizeof(buf)+1;
1459 space = COLS-2;
1461 toskip = 0;
1462 TAILQ_FOREACH(tab, &tabshead, tabs) {
1463 toskip++;
1464 if (tab->flags & TAB_CURRENT)
1465 break;
1468 if (toskip * tabwidth < space)
1469 toskip = 0;
1470 else {
1471 ots = toskip;
1472 toskip--;
1473 while (toskip != 0 &&
1474 (ots - toskip+1) * tabwidth < space)
1475 toskip--;
1478 werase(tabline);
1479 wattron(tabline, tab_face.background);
1480 wprintw(tabline, toskip == 0 ? " " : "<");
1481 wattroff(tabline, tab_face.background);
1483 truncated = 0;
1484 TAILQ_FOREACH(tab, &tabshead, tabs) {
1485 if (truncated)
1486 break;
1487 if (toskip != 0) {
1488 toskip--;
1489 continue;
1492 getyx(tabline, y, x);
1493 if (x + sizeof(buf)+2 >= (size_t)COLS)
1494 truncated = 1;
1496 current = tab->flags & TAB_CURRENT;
1498 if (*(title = tab->window.page.title) == '\0')
1499 title = tab->hist_cur->h;
1501 strlcpy(buf, " ", sizeof(buf));
1502 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1503 /* truncation happens */
1504 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1505 } else {
1506 /* pad with spaces */
1507 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1508 /* nop */ ;
1511 if (current)
1512 wattron(tabline, tab_face.current_tab);
1513 else
1514 wattron(tabline, tab_face.tab);
1516 wprintw(tabline, "%s", buf);
1517 if (TAILQ_NEXT(tab, tabs) != NULL)
1518 wprintw(tabline, " ");
1520 if (current)
1521 wattroff(tabline, tab_face.current_tab);
1522 else
1523 wattroff(tabline, tab_face.tab);
1526 wattron(tabline, tab_face.background);
1527 for (; x < (size_t)COLS; ++x)
1528 waddch(tabline, ' ');
1529 if (truncated)
1530 mvwprintw(tabline, 0, COLS-1, ">");
1533 static void
1534 redraw_window(WINDOW *win, int height, struct window *window)
1536 struct vline *vl;
1537 int l;
1539 werase(win);
1541 window->line_off = MIN(window->line_max-1, window->line_off);
1542 if (TAILQ_EMPTY(&window->head))
1543 return;
1545 l = 0;
1546 vl = nth_line(window, window->line_off);
1547 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1548 wmove(win, l, 0);
1549 print_vline(win, vl);
1550 l++;
1551 if (l == height)
1552 break;
1555 wmove(win, window->curs_y, window->curs_x);
1558 static void
1559 redraw_help(void)
1561 redraw_window(help, help_lines, &helpwin);
1564 static void
1565 redraw_body(struct tab *tab)
1567 redraw_window(body, body_lines, &tab->window);
1570 static inline char
1571 trust_status_char(enum trust_state ts)
1573 switch (ts) {
1574 case TS_UNKNOWN: return 'u';
1575 case TS_UNTRUSTED: return '!';
1576 case TS_TRUSTED: return 'v';
1577 case TS_VERIFIED: return 'V';
1581 static void
1582 redraw_modeline(struct tab *tab)
1584 double pct;
1585 int x, y, max_x, max_y;
1586 const char *mode = tab->window.page.name;
1587 const char *spin = "-\\|/";
1589 werase(modeline);
1590 wattron(modeline, A_REVERSE);
1591 wmove(modeline, 0, 0);
1593 wprintw(modeline, "-%c%c %s ",
1594 spin[tab->loading_anim_step],
1595 trust_status_char(tab->trust),
1596 mode == NULL ? "(none)" : mode);
1598 pct = (tab->window.line_off + tab->window.curs_y) * 100.0 / tab->window.line_max;
1600 if (tab->window.line_max <= (size_t)body_lines)
1601 wprintw(modeline, "All ");
1602 else if (tab->window.line_off == 0)
1603 wprintw(modeline, "Top ");
1604 else if (tab->window.line_off + body_lines >= tab->window.line_max)
1605 wprintw(modeline, "Bottom ");
1606 else
1607 wprintw(modeline, "%.0f%% ", pct);
1609 wprintw(modeline, "%d/%d %s ",
1610 tab->window.line_off + tab->window.curs_y,
1611 tab->window.line_max,
1612 tab->hist_cur->h);
1614 getyx(modeline, y, x);
1615 getmaxyx(modeline, max_y, max_x);
1617 (void)y;
1618 (void)max_y;
1620 for (; x < max_x; ++x)
1621 waddstr(modeline, "-");
1624 static void
1625 redraw_minibuffer(void)
1627 struct tab *tab;
1628 size_t off_y, off_x = 0;
1629 char *start, *c;
1631 werase(minibuf);
1633 if (in_minibuffer) {
1634 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1635 if (ministate.hist_cur != NULL)
1636 wprintw(minibuf, "(%zu/%zu) ",
1637 ministate.hist_off + 1,
1638 ministate.history->len);
1640 getyx(minibuf, off_y, off_x);
1642 start = ministate.hist_cur != NULL
1643 ? ministate.hist_cur->h
1644 : ministate.buf;
1645 c = utf8_nth(ministate.window.current_line->line,
1646 ministate.window.cpoff);
1647 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1648 start = utf8_next_cp(start);
1651 waddstr(minibuf, start);
1654 if (ministate.curmesg != NULL)
1655 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1656 ministate.curmesg);
1658 if (!in_minibuffer && ministate.curmesg == NULL)
1659 waddstr(minibuf, keybuf);
1661 /* If nothing else, show the URL at point */
1662 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1663 tab = current_tab();
1664 if (tab->window.current_line != NULL &&
1665 tab->window.current_line->parent->type == LINE_LINK)
1666 waddstr(minibuf, tab->window.current_line->parent->alt);
1669 if (in_minibuffer)
1670 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1673 static void
1674 redraw_tab(struct tab *tab)
1676 if (side_window) {
1677 redraw_help();
1678 wnoutrefresh(help);
1681 redraw_tabline();
1682 redraw_body(tab);
1683 redraw_modeline(tab);
1684 redraw_minibuffer();
1686 wnoutrefresh(tabline);
1687 wnoutrefresh(modeline);
1689 if (in_minibuffer) {
1690 wnoutrefresh(body);
1691 wnoutrefresh(minibuf);
1692 } else {
1693 wnoutrefresh(minibuf);
1694 wnoutrefresh(body);
1697 doupdate();
1700 static void
1701 emit_help_item(char *prfx, void *fn)
1703 struct line *l;
1704 struct cmds *cmd;
1706 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1707 if (fn == cmd->fn)
1708 break;
1710 assert(cmd != NULL);
1712 if ((l = calloc(1, sizeof(*l))) == NULL)
1713 abort();
1715 l->type = LINE_TEXT;
1716 l->alt = NULL;
1718 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1720 if (TAILQ_EMPTY(&helpwin.page.head))
1721 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1722 else
1723 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1726 static void
1727 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1729 struct keymap *k;
1730 char p[32];
1731 const char *kn;
1733 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1734 strlcpy(p, prfx, sizeof(p));
1735 if (*p != '\0')
1736 strlcat(p, " ", sizeof(p));
1737 if (k->meta)
1738 strlcat(p, "M-", sizeof(p));
1739 if ((kn = unkbd(k->key)) != NULL)
1740 strlcat(p, kn, sizeof(p));
1741 else
1742 strlcat(p, keyname(k->key), sizeof(p));
1744 if (k->fn == NULL)
1745 rec_compute_help(&k->map, p, sizeof(p));
1746 else
1747 emit_help_item(p, k->fn);
1751 static void
1752 recompute_help(void)
1754 char p[32] = { 0 };
1756 empty_vlist(&helpwin);
1757 empty_linelist(&helpwin);
1758 rec_compute_help(current_map, p, sizeof(p));
1759 wrap_page(&helpwin, help_cols);
1762 static void
1763 vmessage(const char *fmt, va_list ap)
1765 if (evtimer_pending(&clminibufev, NULL))
1766 evtimer_del(&clminibufev);
1767 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1768 evtimer_add(&clminibufev, &clminibufev_timer);
1770 free(ministate.curmesg);
1772 /* TODO: what to do if the allocation fails here? */
1773 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1774 ministate.curmesg = NULL;
1776 redraw_minibuffer();
1777 if (in_minibuffer) {
1778 wrefresh(body);
1779 wrefresh(minibuf);
1780 } else {
1781 wrefresh(minibuf);
1782 wrefresh(body);
1786 static void
1787 message(const char *fmt, ...)
1789 va_list ap;
1791 va_start(ap, fmt);
1792 vmessage(fmt, ap);
1793 va_end(ap);
1796 static void
1797 start_loading_anim(struct tab *tab)
1799 if (tab->loading_anim)
1800 return;
1801 tab->loading_anim = 1;
1802 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1803 evtimer_add(&tab->loadingev, &loadingev_timer);
1806 static void
1807 update_loading_anim(int fd, short ev, void *d)
1809 struct tab *tab = d;
1811 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1813 if (tab->flags & TAB_CURRENT) {
1814 redraw_modeline(tab);
1815 wrefresh(modeline);
1816 wrefresh(body);
1817 if (in_minibuffer)
1818 wrefresh(minibuf);
1821 evtimer_add(&tab->loadingev, &loadingev_timer);
1824 static void
1825 stop_loading_anim(struct tab *tab)
1827 if (!tab->loading_anim)
1828 return;
1829 evtimer_del(&tab->loadingev);
1830 tab->loading_anim = 0;
1831 tab->loading_anim_step = 0;
1833 if (!(tab->flags & TAB_CURRENT))
1834 return;
1836 redraw_modeline(tab);
1838 wrefresh(modeline);
1839 wrefresh(body);
1840 if (in_minibuffer)
1841 wrefresh(minibuf);
1844 static void
1845 load_url_in_tab(struct tab *tab, const char *url)
1847 message("Loading %s...", url);
1848 start_loading_anim(tab);
1849 load_url(tab, url);
1851 tab->window.curs_x = 0;
1852 tab->window.curs_y = 0;
1853 redraw_tab(tab);
1856 static void
1857 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1858 void (*abortfn)(void), struct histhead *hist)
1860 in_minibuffer = 1;
1861 base_map = &minibuffer_map;
1862 current_map = &minibuffer_map;
1864 base_map->unhandled_input = self_insert_fn;
1866 ministate.donefn = donefn;
1867 ministate.abortfn = abortfn;
1868 memset(ministate.buf, 0, sizeof(ministate.buf));
1869 ministate.window.current_line = &ministate.vline;
1870 ministate.window.current_line->line = ministate.buf;
1871 ministate.window.cpoff = 0;
1872 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1874 ministate.history = hist;
1875 ministate.hist_cur = NULL;
1876 ministate.hist_off = 0;
1879 static void
1880 exit_minibuffer(void)
1882 werase(minibuf);
1884 in_minibuffer = 0;
1885 base_map = &global_map;
1886 current_map = &global_map;
1889 static void
1890 switch_to_tab(struct tab *tab)
1892 struct tab *t;
1894 TAILQ_FOREACH(t, &tabshead, tabs) {
1895 t->flags &= ~TAB_CURRENT;
1898 tab->flags |= TAB_CURRENT;
1901 unsigned int
1902 tab_new_id(void)
1904 return tab_counter++;
1907 static struct tab *
1908 new_tab(const char *url)
1910 struct tab *tab;
1912 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1913 event_loopbreak();
1914 return NULL;
1917 TAILQ_INIT(&tab->hist.head);
1919 TAILQ_INIT(&tab->window.head);
1921 tab->id = tab_new_id();
1922 switch_to_tab(tab);
1924 if (TAILQ_EMPTY(&tabshead))
1925 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1926 else
1927 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1929 load_url_in_tab(tab, url);
1930 return tab;
1933 static void
1934 session_new_tab_cb(const char *url)
1936 new_tab(url);
1939 static void
1940 usage(void)
1942 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1945 int
1946 ui_init(int argc, char * const *argv)
1948 const char *url = NEW_TAB_URL;
1949 int ch;
1951 while ((ch = getopt(argc, argv, "")) != -1) {
1952 switch (ch) {
1953 default:
1954 usage();
1955 return 0;
1958 argc -= optind;
1959 argv += optind;
1961 if (argc != 0)
1962 url = argv[0];
1964 setlocale(LC_ALL, "");
1966 TAILQ_INIT(&global_map.m);
1967 global_map.unhandled_input = global_key_unbound;
1969 TAILQ_INIT(&minibuffer_map.m);
1971 TAILQ_INIT(&eecmd_history.head);
1972 TAILQ_INIT(&ir_history.head);
1973 TAILQ_INIT(&lu_history.head);
1975 ministate.line.type = LINE_TEXT;
1976 ministate.vline.parent = &ministate.line;
1977 ministate.window.current_line = &ministate.vline;
1979 /* initialize help window */
1980 TAILQ_INIT(&helpwin.head);
1982 base_map = &global_map;
1983 current_map = &global_map;
1984 load_default_keys();
1986 initscr();
1987 raw();
1988 noecho();
1990 nonl();
1991 intrflush(stdscr, FALSE);
1993 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1994 return 0;
1995 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1996 return 0;
1997 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1998 return 0;
1999 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2000 return 0;
2001 if ((help = newwin(1, 1, 1, 0)) == NULL)
2002 return 0;
2004 body_lines = LINES-3;
2005 body_cols = COLS;
2007 keypad(body, TRUE);
2008 scrollok(body, TRUE);
2010 /* non-blocking input */
2011 wtimeout(body, 0);
2013 mvwprintw(body, 0, 0, "");
2015 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2016 event_add(&stdioev, NULL);
2018 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2019 signal_add(&winchev, NULL);
2021 load_last_session(session_new_tab_cb);
2022 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2023 new_tab(url);
2025 return 1;
2028 void
2029 ui_on_tab_loaded(struct tab *tab)
2031 stop_loading_anim(tab);
2032 message("Loaded %s", tab->hist_cur->h);
2034 redraw_tabline();
2035 wrefresh(tabline);
2036 if (in_minibuffer)
2037 wrefresh(minibuf);
2038 else
2039 wrefresh(body);
2042 void
2043 ui_on_tab_refresh(struct tab *tab)
2045 wrap_page(&tab->window, body_cols);
2046 if (tab->flags & TAB_CURRENT) {
2047 restore_cursor(&tab->window);
2048 redraw_tab(tab);
2052 void
2053 ui_require_input(struct tab *tab, int hide)
2055 /* TODO: hard-switching to another tab is ugly */
2056 switch_to_tab(tab);
2058 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2059 &ir_history);
2060 strlcpy(ministate.prompt, "Input required: ",
2061 sizeof(ministate.prompt));
2062 redraw_tab(tab);
2065 void
2066 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2067 unsigned int data)
2069 size_t len;
2071 if (in_minibuffer) {
2072 fn(0, data);
2073 return;
2076 yornp_cb = fn;
2077 yornp_data = data;
2078 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2079 yornp_abort, NULL);
2081 len = sizeof(ministate.prompt);
2082 strlcpy(ministate.prompt, prompt, len);
2083 strlcat(ministate.prompt, " (y or n) ", len);
2084 redraw_tab(current_tab());
2087 void
2088 ui_notify(const char *fmt, ...)
2090 va_list ap;
2092 va_start(ap, fmt);
2093 vmessage(fmt, ap);
2094 va_end(ap);
2097 void
2098 ui_end(void)
2100 endwin();