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''.
32 *
33 * TODO: make the text formatting on-demand.
34 *
35 */
37 #include <telescope.h>
39 #include <ctype.h>
40 #include <curses.h>
41 #include <event.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #define TAB_CURRENT 0x1
51 static struct event stdioev, winchev;
53 static void load_default_keys(void);
54 static void empty_vlist(struct tab*);
55 static void restore_cursor(struct tab *);
57 static void cmd_previous_line(struct tab*);
58 static void cmd_next_line(struct tab*);
59 static void cmd_forward_char(struct tab*);
60 static void cmd_backward_char(struct tab*);
61 static void cmd_move_beginning_of_line(struct tab*);
62 static void cmd_move_end_of_line(struct tab*);
63 static void cmd_redraw(struct tab*);
64 static void cmd_scroll_line_down(struct tab*);
65 static void cmd_scroll_line_up(struct tab*);
66 static void cmd_scroll_up(struct tab*);
67 static void cmd_scroll_down(struct tab*);
68 static void cmd_beginning_of_buffer(struct tab*);
69 static void cmd_end_of_buffer(struct tab*);
70 static void cmd_kill_telescope(struct tab*);
71 static void cmd_push_button(struct tab*);
72 static void cmd_push_button_new_tab(struct tab*);
73 static void cmd_previous_page(struct tab*);
74 static void cmd_next_page(struct tab*);
75 static void cmd_clear_minibuf(struct tab*);
76 static void cmd_execute_extended_command(struct tab*);
77 static void cmd_tab_close(struct tab*);
78 static void cmd_tab_new(struct tab*);
79 static void cmd_tab_next(struct tab*);
80 static void cmd_tab_previous(struct tab*);
81 static void cmd_load_url(struct tab*);
82 static void cmd_load_current_url(struct tab*);
83 static void cmd_bookmark_page(struct tab*);
85 static void global_key_unbound(void);
87 static void cmd_mini_delete_char(struct tab*);
88 static void cmd_mini_delete_backward_char(struct tab*);
89 static void cmd_mini_forward_char(struct tab*);
90 static void cmd_mini_backward_char(struct tab*);
91 static void cmd_mini_move_end_of_line(struct tab*);
92 static void cmd_mini_move_beginning_of_line(struct tab*);
93 static void cmd_mini_kill_line(struct tab*);
94 static void cmd_mini_abort(struct tab*);
95 static void cmd_mini_complete_and_exit(struct tab*);
96 static void cmd_mini_previous_history_element(struct tab*);
97 static void cmd_mini_next_history_element(struct tab*);
99 static void minibuffer_hist_save_entry(void);
100 static void minibuffer_taint_hist(void);
101 static void minibuffer_self_insert(void);
102 static void eecmd_self_insert(void);
103 static void eecmd_select(void);
104 static void ir_self_insert(void);
105 static void ir_select(void);
106 static void lu_self_insert(void);
107 static void lu_select(void);
108 static void bp_select(void);
110 static struct vline *nth_line(struct tab*, size_t);
111 static struct tab *current_tab(void);
112 static void dispatch_stdio(int, short, void*);
113 static void handle_clear_minibuf(int, short, void*);
114 static void handle_resize(int, short, void*);
115 static int wrap_page(struct tab*);
116 static void print_vline(struct vline*);
117 static void redraw_tabline(void);
118 static void redraw_body(struct tab*);
119 static void redraw_modeline(struct tab*);
120 static void redraw_minibuffer(void);
121 static void redraw_tab(struct tab*);
122 static void vmessage(const char*, va_list);
123 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
124 static void start_loading_anim(struct tab*);
125 static void update_loading_anim(int, short, void*);
126 static void stop_loading_anim(struct tab*);
127 static void load_url_in_tab(struct tab*, const char*);
128 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
129 static void exit_minibuffer(void);
130 static void switch_to_tab(struct tab*);
131 static struct tab *new_tab(void);
133 static struct { int meta, key; } thiskey;
135 static WINDOW *tabline, *body, *modeline, *minibuf;
136 static int body_lines, body_cols;
138 static struct event clminibufev;
139 static int clminibufev_set;
140 static struct timeval clminibufev_timer = { 5, 0 };
141 static struct timeval loadingev_timer = { 0, 250000 };
143 static uint32_t tab_counter;
145 static char keybuf[64];
147 struct kmap global_map,
148 minibuffer_map,
149 *current_map,
150 *base_map;
152 static struct histhead eecmd_history,
153 ir_history,
154 lu_history;
156 static int in_minibuffer;
158 static struct {
159 char *curmesg;
161 char buf[1025];
162 size_t off, len;
163 char prompt[32];
164 void (*donefn)(void);
165 void (*abortfn)(void);
167 struct histhead *history;
168 struct hist *hist_cur;
169 size_t hist_off;
170 } ministate;
172 struct lineprefix {
173 const char *prfx1;
174 const char *prfx2;
175 } line_prefixes[] = {
176 [LINE_TEXT] = { "", "" },
177 [LINE_LINK] = { "=> ", " " },
178 [LINE_TITLE_1] = { "# ", " " },
179 [LINE_TITLE_2] = { "## ", " " },
180 [LINE_TITLE_3] = { "### ", " " },
181 [LINE_ITEM] = { "* ", " " },
182 [LINE_QUOTE] = { "> ", "> " },
183 [LINE_PRE_START] = { "```", "```" },
184 [LINE_PRE_CONTENT] = { "", "" },
185 [LINE_PRE_END] = { "```", "```" },
186 };
188 struct line_face {
189 int prop;
190 } line_faces[] = {
191 [LINE_TEXT] = { 0 },
192 [LINE_LINK] = { A_UNDERLINE },
193 [LINE_TITLE_1] = { A_BOLD },
194 [LINE_TITLE_2] = { A_BOLD },
195 [LINE_TITLE_3] = { A_BOLD },
196 [LINE_ITEM] = { 0 },
197 [LINE_QUOTE] = { A_DIM },
198 [LINE_PRE_START] = { 0 },
199 [LINE_PRE_CONTENT] = { 0 },
200 [LINE_PRE_END] = { 0 },
201 };
203 static void
204 empty_vlist(struct tab *tab)
206 struct vline *vl, *t;
208 tab->s.line_max = 0;
210 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
211 TAILQ_REMOVE(&tab->s.head, vl, vlines);
212 free(vl->line);
213 free(vl);
217 static inline void
218 global_set_key(const char *key, void (*fn)(struct tab*))
220 if (!kmap_define_key(&global_map, key, fn))
221 _exit(1);
224 static inline void
225 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
227 if (!kmap_define_key(&minibuffer_map, key, fn))
228 _exit(1);
231 static void
232 load_default_keys(void)
234 /* === global map === */
236 /* emacs */
237 global_set_key("C-p", cmd_previous_line);
238 global_set_key("C-n", cmd_next_line);
239 global_set_key("C-f", cmd_forward_char);
240 global_set_key("C-b", cmd_backward_char);
241 global_set_key("C-a", cmd_move_beginning_of_line);
242 global_set_key("C-e", cmd_move_end_of_line);
244 global_set_key("M-v", cmd_scroll_up);
245 global_set_key("C-v", cmd_scroll_down);
246 global_set_key("M-space", cmd_scroll_up);
247 global_set_key("space", cmd_scroll_down);
249 global_set_key("C-x C-c", cmd_kill_telescope);
251 global_set_key("C-g", cmd_clear_minibuf);
253 global_set_key("M-x", cmd_execute_extended_command);
254 global_set_key("C-x C-f", cmd_load_url);
255 global_set_key("C-x M-f", cmd_load_current_url);
257 global_set_key("C-x t 0", cmd_tab_close);
258 global_set_key("C-x t 2", cmd_tab_new);
259 global_set_key("C-x t o", cmd_tab_next);
260 global_set_key("C-x t O", cmd_tab_previous);
262 global_set_key("M-<", cmd_beginning_of_buffer);
263 global_set_key("M->", cmd_end_of_buffer);
265 global_set_key("C-M-b", cmd_previous_page);
266 global_set_key("C-M-f", cmd_next_page);
268 global_set_key("<f7> a", cmd_bookmark_page);
270 /* vi/vi-like */
271 global_set_key("k", cmd_previous_line);
272 global_set_key("j", cmd_next_line);
273 global_set_key("l", cmd_forward_char);
274 global_set_key("h", cmd_backward_char);
275 global_set_key("^", cmd_move_beginning_of_line);
276 global_set_key("$", cmd_move_end_of_line);
278 global_set_key("K", cmd_scroll_line_up);
279 global_set_key("J", cmd_scroll_line_down);
281 global_set_key("g g", cmd_beginning_of_buffer);
282 global_set_key("G", cmd_end_of_buffer);
284 global_set_key("H", cmd_previous_page);
285 global_set_key("L", cmd_next_page);
287 /* tmp */
288 global_set_key("q", cmd_kill_telescope);
290 global_set_key("esc", cmd_clear_minibuf);
292 global_set_key(":", cmd_execute_extended_command);
294 /* cua */
295 global_set_key("<up>", cmd_previous_line);
296 global_set_key("<down>", cmd_next_line);
297 global_set_key("<right>", cmd_forward_char);
298 global_set_key("<left>", cmd_backward_char);
299 global_set_key("<prior>", cmd_scroll_up);
300 global_set_key("<next>", cmd_scroll_down);
302 global_set_key("M-<left>", cmd_previous_page);
303 global_set_key("M-<right>", cmd_next_page);
305 /* "ncurses standard" */
306 global_set_key("C-l", cmd_redraw);
308 /* global */
309 global_set_key("C-m", cmd_push_button);
310 global_set_key("M-enter", cmd_push_button_new_tab);
312 /* === minibuffer map === */
313 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
314 minibuffer_set_key("C-g", cmd_mini_abort);
315 minibuffer_set_key("esc", cmd_mini_abort);
316 minibuffer_set_key("C-d", cmd_mini_delete_char);
317 minibuffer_set_key("del", cmd_mini_delete_backward_char);
319 minibuffer_set_key("C-f", cmd_mini_forward_char);
320 minibuffer_set_key("C-b", cmd_mini_backward_char);
321 minibuffer_set_key("<right>", cmd_mini_forward_char);
322 minibuffer_set_key("<left>", cmd_mini_backward_char);
323 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
324 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
325 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
326 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
327 minibuffer_set_key("C-k", cmd_mini_kill_line);
329 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
330 minibuffer_set_key("M-n", cmd_mini_next_history_element);
331 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
332 minibuffer_set_key("<down>", cmd_mini_next_history_element);
335 static void
336 restore_cursor(struct tab *tab)
338 wmove(body, tab->s.curs_y, tab->s.curs_x);
341 static void
342 cmd_previous_line(struct tab *tab)
344 if (--tab->s.curs_y < 0) {
345 tab->s.curs_y = 0;
346 cmd_scroll_line_up(tab);
349 restore_cursor(tab);
352 static void
353 cmd_next_line(struct tab *tab)
355 if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
356 return;
358 if (++tab->s.curs_y > body_lines-1) {
359 tab->s.curs_y = body_lines-1;
360 cmd_scroll_line_down(tab);
363 restore_cursor(tab);
366 static void
367 cmd_forward_char(struct tab *tab)
369 tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
370 restore_cursor(tab);
373 static void
374 cmd_backward_char(struct tab *tab)
376 tab->s.curs_x = MAX(0, tab->s.curs_x-1);
377 restore_cursor(tab);
380 static void
381 cmd_move_beginning_of_line(struct tab *tab)
383 tab->s.curs_x = 0;
384 restore_cursor(tab);
387 static void
388 cmd_move_end_of_line(struct tab *tab)
390 struct vline *vl;
391 size_t off;
392 const char *prfx;
394 off = tab->s.line_off + tab->s.curs_y;
395 if (off >= tab->s.line_max) {
396 tab->s.curs_x = 0;
397 goto end;
400 vl = nth_line(tab, off);
401 if (vl->line != NULL)
402 tab->s.curs_x = strlen(vl->line);
403 else
404 tab->s.curs_x = 0;
406 prfx = line_prefixes[vl->parent->type].prfx1;
407 tab->s.curs_x += strlen(prfx);
409 end:
410 restore_cursor(tab);
413 static void
414 cmd_redraw(struct tab *tab)
416 handle_resize(0, 0, NULL);
419 static void
420 cmd_scroll_line_up(struct tab *tab)
422 struct vline *vl;
424 if (tab->s.line_off == 0)
425 return;
427 vl = nth_line(tab, --tab->s.line_off);
428 wscrl(body, -1);
429 wmove(body, 0, 0);
430 print_vline(vl);
433 static void
434 cmd_scroll_line_down(struct tab *tab)
436 struct vline *vl;
438 if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
439 return;
441 tab->s.line_off++;
442 wscrl(body, 1);
444 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
445 return;
447 vl = nth_line(tab, tab->s.line_off + body_lines-1);
448 wmove(body, body_lines-1, 0);
449 print_vline(vl);
452 static void
453 cmd_scroll_up(struct tab *tab)
455 size_t off;
457 off = body_lines+1;
459 for (; off > 0; --off)
460 cmd_scroll_line_up(tab);
463 static void
464 cmd_scroll_down(struct tab *tab)
466 size_t off;
468 off = body_lines+1;
470 for (; off > 0; --off)
471 cmd_scroll_line_down(tab);
474 static void
475 cmd_beginning_of_buffer(struct tab *tab)
477 tab->s.line_off = 0;
478 tab->s.curs_y = 0;
479 redraw_body(tab);
482 static void
483 cmd_end_of_buffer(struct tab *tab)
485 ssize_t off;
487 off = tab->s.line_max - body_lines;
488 off = MAX(0, off);
490 tab->s.line_off = off;
491 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
493 redraw_body(tab);
496 static void
497 cmd_kill_telescope(struct tab *tab)
499 event_loopbreak();
502 static void
503 cmd_push_button(struct tab *tab)
505 struct vline *vl;
506 size_t nth;
508 nth = tab->s.line_off + tab->s.curs_y;
509 if (nth >= tab->s.line_max)
510 return;
511 vl = nth_line(tab, nth);
512 if (vl->parent->type != LINE_LINK)
513 return;
515 load_url_in_tab(tab, vl->parent->alt);
518 static void
519 cmd_push_button_new_tab(struct tab *tab)
521 struct tab *t;
522 struct vline *vl;
523 size_t nth;
525 nth = tab->s.line_off + tab->s.curs_y;
526 if (nth > tab->s.line_max)
527 return;
528 vl = nth_line(tab, nth);
529 if (vl->parent->type != LINE_LINK)
530 return;
532 t = new_tab();
533 memcpy(&t->url, &tab->url, sizeof(tab->url));
534 load_url_in_tab(t, vl->parent->alt);
537 static void
538 cmd_previous_page(struct tab *tab)
540 if (!load_previous_page(tab))
541 message("No previous page");
542 else
543 start_loading_anim(tab);
546 static void
547 cmd_next_page(struct tab *tab)
549 if (!load_next_page(tab))
550 message("No next page");
551 else
552 start_loading_anim(tab);
555 static void
556 cmd_clear_minibuf(struct tab *tab)
558 handle_clear_minibuf(0, 0, NULL);
561 static void
562 cmd_execute_extended_command(struct tab *tab)
564 size_t len;
566 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
567 &eecmd_history);
569 len = sizeof(ministate.prompt);
570 strlcpy(ministate.prompt, "", len);
572 if (thiskey.meta)
573 strlcat(ministate.prompt, "M-", len);
575 strlcat(ministate.prompt, keyname(thiskey.key), len);
576 strlcat(ministate.prompt, " ", len);
579 static void
580 cmd_tab_close(struct tab *tab)
582 struct tab *t;
584 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
585 TAILQ_NEXT(tab, tabs) == NULL) {
586 message("Can't close the only tab.");
587 return;
590 stop_tab(tab);
592 t = TAILQ_PREV(tab, tabshead, tabs);
593 t->flags |= TAB_CURRENT;
595 TAILQ_REMOVE(&tabshead, tab, tabs);
597 free(tab);
600 static void
601 cmd_tab_new(struct tab *tab)
603 new_tab();
606 static void
607 cmd_tab_next(struct tab *tab)
609 struct tab *t;
611 tab->flags &= ~TAB_CURRENT;
613 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
614 t = TAILQ_FIRST(&tabshead);
615 t->flags |= TAB_CURRENT;
618 static void
619 cmd_tab_previous(struct tab *tab)
621 struct tab *t;
623 tab->flags &= ~TAB_CURRENT;
625 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
626 t = TAILQ_LAST(&tabshead, tabshead);
627 t->flags |= TAB_CURRENT;
630 static void
631 cmd_load_url(struct tab *tab)
633 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
634 &lu_history);
635 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
638 static void
639 cmd_load_current_url(struct tab *tab)
641 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
642 &lu_history);
643 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
644 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
645 ministate.off = strlen(tab->hist_cur->h);
646 ministate.len = ministate.off;
649 static void
650 cmd_bookmark_page(struct tab *tab)
652 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
653 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
654 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
655 ministate.off = strlen(tab->hist_cur->h);
656 ministate.len = ministate.off;
659 static void
660 global_key_unbound(void)
662 message("%s is undefined", keybuf);
665 static void
666 cmd_mini_delete_char(struct tab *tab)
668 minibuffer_taint_hist();
670 if (ministate.len == 0 || ministate.off == ministate.len)
671 return;
673 memmove(&ministate.buf[ministate.off],
674 &ministate.buf[ministate.off+1],
675 ministate.len - ministate.off + 1);
676 ministate.len--;
679 static void
680 cmd_mini_delete_backward_char(struct tab *tab)
682 minibuffer_taint_hist();
684 if (ministate.len == 0 || ministate.off == 0)
685 return;
687 memmove(&ministate.buf[ministate.off-1],
688 &ministate.buf[ministate.off],
689 ministate.len - ministate.off + 1);
690 ministate.off--;
691 ministate.len--;
694 static void
695 cmd_mini_forward_char(struct tab *tab)
697 if (ministate.off == ministate.len)
698 return;
699 ministate.off++;
702 static void
703 cmd_mini_backward_char(struct tab *tab)
705 if (ministate.off == 0)
706 return;
707 ministate.off--;
710 static void
711 cmd_mini_move_end_of_line(struct tab *tab)
713 ministate.off = ministate.len;
716 static void
717 cmd_mini_move_beginning_of_line(struct tab *tab)
719 ministate.off = 0;
722 static void
723 cmd_mini_kill_line(struct tab *tab)
725 minibuffer_taint_hist();
727 if (ministate.off == ministate.len)
728 return;
729 ministate.buf[ministate.off] = '\0';
730 ministate.len -= ministate.off;
733 static void
734 cmd_mini_abort(struct tab *tab)
736 ministate.abortfn();
739 static void
740 cmd_mini_complete_and_exit(struct tab *tab)
742 minibuffer_taint_hist();
743 ministate.donefn();
746 static void
747 cmd_mini_previous_history_element(struct tab *tab)
749 if (ministate.history == NULL) {
750 message("No history");
751 return;
754 if (ministate.hist_cur == NULL ||
755 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
756 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
757 ministate.hist_off = ministate.history->len - 1;
758 if (ministate.hist_cur == NULL)
759 message("No prev item");
760 } else {
761 ministate.hist_off--;
764 if (ministate.hist_cur != NULL) {
765 ministate.off = 0;
766 ministate.len = strlen(ministate.hist_cur->h);
770 static void
771 cmd_mini_next_history_element(struct tab *tab)
773 if (ministate.history == NULL) {
774 message("No history");
775 return;
778 if (ministate.hist_cur == NULL ||
779 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
780 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
781 ministate.hist_off = 0;
782 if (ministate.hist_cur == NULL)
783 message("No next item");
784 } else {
785 ministate.hist_off++;
788 if (ministate.hist_cur != NULL) {
789 ministate.off = 0;
790 ministate.len = strlen(ministate.hist_cur->h);
794 static void
795 minibuffer_hist_save_entry(void)
797 struct hist *hist;
799 if (ministate.history == NULL)
800 return;
802 if ((hist = calloc(1, sizeof(*hist))) == NULL)
803 abort();
805 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
807 if (TAILQ_EMPTY(&ministate.history->head))
808 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
809 else
810 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
811 ministate.history->len++;
814 /*
815 * taint the minibuffer cache: if we're currently showing a history
816 * element, copy that to the current buf and reset the "history
817 * navigation" thing.
818 */
819 static void
820 minibuffer_taint_hist(void)
822 if (ministate.hist_cur == NULL)
823 return;
825 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
826 ministate.hist_cur = NULL;
829 static void
830 minibuffer_self_insert(void)
832 minibuffer_taint_hist();
834 if (ministate.len == sizeof(ministate.buf) -1)
835 return;
837 /* TODO: utf8 handling! */
839 memmove(&ministate.buf[ministate.off+1],
840 &ministate.buf[ministate.off],
841 ministate.len - ministate.off + 1);
842 ministate.buf[ministate.off] = thiskey.key;
843 ministate.off++;
844 ministate.len++;
847 static void
848 eecmd_self_insert(void)
850 if (thiskey.meta || isspace(thiskey.key) ||
851 !isgraph(thiskey.key)) {
852 global_key_unbound();
853 return;
856 minibuffer_self_insert();
859 static void
860 eecmd_select(void)
862 exit_minibuffer();
863 minibuffer_hist_save_entry();
864 message("TODO: try to execute %s", ministate.buf);
867 static void
868 ir_self_insert(void)
870 minibuffer_self_insert();
873 static void
874 ir_select(void)
876 char buf[1025] = {0};
877 struct url url;
878 struct tab *tab;
880 tab = current_tab();
882 exit_minibuffer();
883 minibuffer_hist_save_entry();
885 /* a bit ugly but... */
886 memcpy(&url, &tab->url, sizeof(tab->url));
887 url_set_query(&url, ministate.buf);
888 url_unparse(&url, buf, sizeof(buf));
889 load_url_in_tab(tab, buf);
892 static void
893 lu_self_insert(void)
895 if (thiskey.meta || isspace(thiskey.key) ||
896 !isgraph(thiskey.key)) {
897 global_key_unbound();
898 return;
901 minibuffer_self_insert();
904 static void
905 lu_select(void)
907 exit_minibuffer();
908 minibuffer_hist_save_entry();
909 load_url_in_tab(current_tab(), ministate.buf);
912 static void
913 bp_select(void)
915 exit_minibuffer();
916 if (*ministate.buf != '\0')
917 add_to_bookmarks(ministate.buf);
918 else
919 message("Abort.");
922 static struct vline *
923 nth_line(struct tab *tab, size_t n)
925 struct vline *vl;
926 size_t i;
928 i = 0;
929 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
930 if (i == n)
931 return vl;
932 i++;
935 /* unreachable */
936 abort();
939 static struct tab *
940 current_tab(void)
942 struct tab *t;
944 TAILQ_FOREACH(t, &tabshead, tabs) {
945 if (t->flags & TAB_CURRENT)
946 return t;
949 /* unreachable */
950 abort();
953 static void
954 dispatch_stdio(int fd, short ev, void *d)
956 struct keymap *k;
957 const char *keyname;
958 char tmp[2] = {0};
960 thiskey.key = wgetch(body);
961 if (thiskey.key == ERR)
962 return;
963 if (thiskey.key == 27) {
964 /* TODO: make escape-time customizable */
966 thiskey.meta = 1;
967 thiskey.key = wgetch(body);
968 if (thiskey.key == ERR || thiskey.key == 27) {
969 thiskey.meta = 0;
970 thiskey.key = 27;
972 } else
973 thiskey.meta = 0;
975 if (keybuf[0] != '\0')
976 strlcat(keybuf, " ", sizeof(keybuf));
977 if (thiskey.meta)
978 strlcat(keybuf, "M-", sizeof(keybuf));
979 if ((keyname = unkbd(thiskey.key)) != NULL)
980 strlcat(keybuf, keyname, sizeof(keybuf));
981 else {
982 tmp[0] = thiskey.key;
983 strlcat(keybuf, tmp, sizeof(keybuf));
986 TAILQ_FOREACH(k, &current_map->m, keymaps) {
987 if (k->meta == thiskey.meta &&
988 k->key == thiskey.key) {
989 if (k->fn == NULL)
990 current_map = &k->map;
991 else {
992 current_map = base_map;
993 strlcpy(keybuf, "", sizeof(keybuf));
994 k->fn(current_tab());
996 goto done;
1000 if (current_map->unhandled_input != NULL)
1001 current_map->unhandled_input();
1002 else {
1003 global_key_unbound();
1006 strlcpy(keybuf, "", sizeof(keybuf));
1007 current_map = base_map;
1009 done:
1010 redraw_tab(current_tab());
1013 static void
1014 handle_clear_minibuf(int fd, short ev, void *d)
1016 clminibufev_set = 0;
1018 free(ministate.curmesg);
1019 ministate.curmesg = NULL;
1021 redraw_minibuffer();
1022 if (in_minibuffer) {
1023 wrefresh(body);
1024 wrefresh(minibuf);
1025 } else {
1026 wrefresh(minibuf);
1027 wrefresh(body);
1031 static void
1032 handle_resize(int sig, short ev, void *d)
1034 struct tab *tab;
1036 endwin();
1037 refresh();
1038 clear();
1040 /* move and resize the windows, in reverse order! */
1042 mvwin(minibuf, LINES-1, 0);
1043 wresize(minibuf, 1, COLS);
1045 mvwin(modeline, LINES-2, 0);
1046 wresize(modeline, 1, COLS);
1048 wresize(body, LINES-3, COLS);
1049 body_lines = LINES-3;
1050 body_cols = COLS;
1052 wresize(tabline, 1, COLS);
1054 tab = current_tab();
1056 wrap_page(tab);
1057 redraw_tab(tab);
1060 static int
1061 wrap_page(struct tab *tab)
1063 struct line *l;
1064 const char *prfx;
1066 empty_vlist(tab);
1068 TAILQ_FOREACH(l, &tab->page.head, lines) {
1069 prfx = line_prefixes[l->type].prfx1;
1070 switch (l->type) {
1071 case LINE_TEXT:
1072 case LINE_LINK:
1073 case LINE_TITLE_1:
1074 case LINE_TITLE_2:
1075 case LINE_TITLE_3:
1076 case LINE_ITEM:
1077 case LINE_QUOTE:
1078 case LINE_PRE_START:
1079 case LINE_PRE_END:
1080 wrap_text(tab, prfx, l, body_cols);
1081 /* push_line(tab, l, NULL, 0, 0); */
1082 break;
1083 case LINE_PRE_CONTENT:
1084 hardwrap_text(tab, l, body_cols);
1085 break;
1088 return 1;
1091 static void
1092 print_vline(struct vline *vl)
1094 const char *text = vl->line;
1095 const char *prfx;
1096 int face = line_faces[vl->parent->type].prop;
1098 if (!vl->flags)
1099 prfx = line_prefixes[vl->parent->type].prfx1;
1100 else
1101 prfx = line_prefixes[vl->parent->type].prfx2;
1103 if (text == NULL)
1104 text = "";
1106 if (face != 0)
1107 wattron(body, face);
1108 wprintw(body, "%s%s", prfx, text);
1109 if (face != 0)
1110 wattroff(body, face);
1113 static void
1114 redraw_tabline(void)
1116 struct tab *tab;
1117 int current;
1118 const char *title;
1120 werase(tabline);
1121 wbkgd(tabline, A_REVERSE);
1123 wprintw(tabline, " ");
1124 TAILQ_FOREACH(tab, &tabshead, tabs) {
1125 current = tab->flags & TAB_CURRENT;
1127 if (*(title = tab->page.title) == '\0')
1128 title = tab->hist_cur->h;
1130 if (current)
1131 wattron(tabline, A_UNDERLINE);
1133 wprintw(tabline, "%s%d: %s",
1134 current ? "*" : " ", tab->id, title);
1136 if (current)
1137 wattroff(tabline, A_UNDERLINE);
1141 static void
1142 redraw_modeline(struct tab *tab)
1144 double pct;
1145 int x, y, max_x, max_y;
1146 const char *mode = tab->page.name;
1147 const char *spin = "-\\|/";
1149 werase(modeline);
1150 wattron(modeline, A_REVERSE);
1151 wmove(modeline, 0, 0);
1153 wprintw(modeline, "-%c %s ",
1154 spin[tab->s.loading_anim_step], mode);
1156 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1158 if (tab->s.line_max <= (size_t)body_lines)
1159 wprintw(modeline, "All ");
1160 else if (tab->s.line_off == 0)
1161 wprintw(modeline, "Top ");
1162 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1163 wprintw(modeline, "Bottom ");
1164 else
1165 wprintw(modeline, "%.0f%% ", pct);
1167 wprintw(modeline, "%d/%d %s ",
1168 tab->s.line_off + tab->s.curs_y,
1169 tab->s.line_max,
1170 tab->hist_cur->h);
1172 getyx(modeline, y, x);
1173 getmaxyx(modeline, max_y, max_x);
1175 (void)y;
1176 (void)max_y;
1178 for (; x < max_x; ++x)
1179 waddstr(modeline, "-");
1182 static void
1183 redraw_minibuffer(void)
1185 size_t skip = 0, off_x = 0, off_y = 0;
1187 werase(minibuf);
1188 if (in_minibuffer) {
1189 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1190 if (ministate.hist_cur != NULL)
1191 wprintw(minibuf, "(%zu/%zu) ",
1192 ministate.hist_off + 1,
1193 ministate.history->len);
1195 getyx(minibuf, off_y, off_x);
1197 while (ministate.off - skip > (size_t)COLS / 2) {
1198 skip += MIN(ministate.off/4, 1);
1201 if (ministate.hist_cur != NULL)
1202 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1203 else
1204 wprintw(minibuf, "%s", ministate.buf + skip);
1207 if (ministate.curmesg != NULL) {
1208 if (in_minibuffer)
1209 wprintw(minibuf, " [%s]", ministate.curmesg);
1210 else
1211 wprintw(minibuf, "%s", ministate.curmesg);
1214 if (!in_minibuffer && ministate.curmesg == NULL)
1215 wprintw(minibuf, "%s", keybuf);
1217 if (in_minibuffer)
1218 wmove(minibuf, 0, off_x + ministate.off - skip);
1221 static void
1222 redraw_tab(struct tab *tab)
1224 redraw_tabline();
1225 redraw_body(tab);
1226 redraw_modeline(tab);
1227 redraw_minibuffer();
1229 restore_cursor(tab);
1230 wrefresh(tabline);
1231 wrefresh(modeline);
1233 if (in_minibuffer) {
1234 wrefresh(body);
1235 wrefresh(minibuf);
1236 } else {
1237 wrefresh(minibuf);
1238 wrefresh(body);
1242 static void
1243 redraw_body(struct tab *tab)
1245 struct vline *vl;
1246 int line;
1248 werase(body);
1250 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1251 if (TAILQ_EMPTY(&tab->s.head))
1252 return;
1254 line = 0;
1255 vl = nth_line(tab, tab->s.line_off);
1256 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1257 wmove(body, line, 0);
1258 print_vline(vl);
1259 line++;
1260 if (line == body_lines)
1261 break;
1265 static void
1266 vmessage(const char *fmt, va_list ap)
1268 if (clminibufev_set)
1269 evtimer_del(&clminibufev);
1270 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1271 evtimer_add(&clminibufev, &clminibufev_timer);
1272 clminibufev_set = 1;
1274 free(ministate.curmesg);
1276 /* TODO: what to do if the allocation fails here? */
1277 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1278 ministate.curmesg = NULL;
1280 redraw_minibuffer();
1281 if (in_minibuffer) {
1282 wrefresh(body);
1283 wrefresh(minibuf);
1284 } else {
1285 wrefresh(minibuf);
1286 wrefresh(body);
1290 static void
1291 message(const char *fmt, ...)
1293 va_list ap;
1295 va_start(ap, fmt);
1296 vmessage(fmt, ap);
1297 va_end(ap);
1300 static void
1301 start_loading_anim(struct tab *tab)
1303 if (tab->s.loading_anim)
1304 return;
1305 tab->s.loading_anim = 1;
1306 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1307 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1310 static void
1311 update_loading_anim(int fd, short ev, void *d)
1313 struct tab *tab = d;
1315 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1317 redraw_modeline(tab);
1318 wrefresh(modeline);
1320 wrefresh(body);
1321 if (in_minibuffer)
1322 wrefresh(minibuf);
1324 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1327 static void
1328 stop_loading_anim(struct tab *tab)
1330 if (!tab->s.loading_anim)
1331 return;
1332 evtimer_del(&tab->s.loadingev);
1333 tab->s.loading_anim = 0;
1334 tab->s.loading_anim_step = 0;
1336 redraw_modeline(tab);
1338 wrefresh(modeline);
1339 wrefresh(body);
1340 if (in_minibuffer)
1341 wrefresh(minibuf);
1344 static void
1345 load_url_in_tab(struct tab *tab, const char *url)
1347 empty_vlist(tab);
1348 message("Loading %s...", url);
1349 start_loading_anim(tab);
1350 load_url(tab, url);
1352 tab->s.curs_x = 0;
1353 tab->s.curs_y = 0;
1354 redraw_tab(tab);
1357 static void
1358 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1359 void (*abortfn)(void), struct histhead *hist)
1361 in_minibuffer = 1;
1362 base_map = &minibuffer_map;
1363 current_map = &minibuffer_map;
1365 base_map->unhandled_input = self_insert_fn;
1367 ministate.donefn = donefn;
1368 ministate.abortfn = abortfn;
1369 memset(ministate.buf, 0, sizeof(ministate.buf));
1370 ministate.off = 0;
1371 ministate.len = 0;
1372 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1374 ministate.history = hist;
1375 ministate.hist_cur = NULL;
1376 ministate.hist_off = 0;
1379 static void
1380 exit_minibuffer(void)
1382 werase(minibuf);
1384 in_minibuffer = 0;
1385 base_map = &global_map;
1386 current_map = &global_map;
1389 static void
1390 switch_to_tab(struct tab *tab)
1392 struct tab *t;
1394 TAILQ_FOREACH(t, &tabshead, tabs) {
1395 t->flags &= ~TAB_CURRENT;
1398 tab->flags |= TAB_CURRENT;
1401 static struct tab *
1402 new_tab(void)
1404 struct tab *tab;
1405 const char *url = "about:new";
1407 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1408 goto err;
1410 TAILQ_INIT(&tab->hist.head);
1412 TAILQ_INIT(&tab->s.head);
1414 tab->id = tab_counter++;
1415 switch_to_tab(tab);
1417 if (TAILQ_EMPTY(&tabshead))
1418 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1419 else
1420 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1422 load_url_in_tab(tab, url);
1423 return tab;
1425 err:
1426 event_loopbreak();
1427 return NULL;
1430 int
1431 ui_init(void)
1433 setlocale(LC_ALL, "");
1435 TAILQ_INIT(&global_map.m);
1436 global_map.unhandled_input = global_key_unbound;
1438 TAILQ_INIT(&minibuffer_map.m);
1440 TAILQ_INIT(&eecmd_history.head);
1441 TAILQ_INIT(&ir_history.head);
1442 TAILQ_INIT(&lu_history.head);
1444 base_map = &global_map;
1445 current_map = &global_map;
1446 load_default_keys();
1448 initscr();
1449 raw();
1450 noecho();
1452 nonl();
1453 intrflush(stdscr, FALSE);
1455 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1456 return 0;
1457 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1458 return 0;
1459 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1460 return 0;
1461 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1462 return 0;
1464 body_lines = LINES-3;
1465 body_cols = COLS;
1467 keypad(body, TRUE);
1468 scrollok(body, TRUE);
1470 /* non-blocking input */
1471 wtimeout(body, 0);
1473 mvwprintw(body, 0, 0, "");
1475 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1476 event_add(&stdioev, NULL);
1478 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1479 signal_add(&winchev, NULL);
1481 new_tab();
1483 return 1;
1486 void
1487 ui_on_tab_loaded(struct tab *tab)
1489 stop_loading_anim(tab);
1490 message("Loaded %s", tab->hist_cur->h);
1493 void
1494 ui_on_tab_refresh(struct tab *tab)
1496 if (!(tab->flags & TAB_CURRENT))
1497 return;
1499 wrap_page(tab);
1500 redraw_tab(tab);
1503 void
1504 ui_require_input(struct tab *tab, int hide)
1506 /* TODO: hard-switching to another tab is ugly */
1507 switch_to_tab(tab);
1509 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1510 &ir_history);
1511 strlcpy(ministate.prompt, "Input required: ",
1512 sizeof(ministate.prompt));
1513 redraw_tab(tab);
1516 void
1517 ui_notify(const char *fmt, ...)
1519 va_list ap;
1521 va_start(ap, fmt);
1522 vmessage(fmt, ap);
1523 va_end(ap);
1526 void
1527 ui_end(void)
1529 endwin();