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 *
21 * Text wrapping
22 * =============
23 *
24 * There's a simple text wrapping algorithm.
25 *
26 * 1. if it's a line in a pre-formatted block:
27 * a. hard wrap.
28 * b. repeat
29 * 2. there is enough room for the next word?
30 * a. yes: render it
31 * b. no: break the current line.
32 * i. while there isn't enough space to draw the current
33 * word, hard-wrap it
34 * ii. draw the remainder of the current word (can be the
35 * the entirely)
36 * 3. render the spaces after the word
37 * a. but if there is not enough room break the line and
38 * forget them
39 * 4. repeat
40 *
41 *
42 * Text scrolling
43 * ==============
44 *
45 * ncurses allows you to scroll a window, but when a line goes out of
46 * the visible area it's forgotten. We keep a list of formatted lines
47 * (``visual lines'') that we know fits in the window, and draw them.
48 * This way is easy to scroll: just call wscrl and then render the
49 * first/last line!
50 *
51 * This means that on every resize we have to clear our list of lines
52 * and re-render everything. A clever approach would be to do this
53 * ``on-demand''.
54 *
55 * TODO: make the text formatting on-demand.
56 *
57 */
59 #include <telescope.h>
61 #include <ctype.h>
62 #include <curses.h>
63 #include <event.h>
64 #include <locale.h>
65 #include <signal.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
71 #define TAB_CURRENT 0x1
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
76 struct kmap;
78 static struct event stdioev, winchev;
80 static int kbd(const char*);
81 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
82 static void load_default_keys(void);
83 static int push_line(struct tab*, const struct line*, const char*, size_t);
84 static void empty_vlist(struct tab*);
85 static void restore_cursor(struct tab *);
87 static void cmd_previous_line(struct tab*);
88 static void cmd_next_line(struct tab*);
89 static void cmd_forward_char(struct tab*);
90 static void cmd_backward_char(struct tab*);
91 static void cmd_redraw(struct tab*);
92 static void cmd_scroll_line_down(struct tab*);
93 static void cmd_scroll_line_up(struct tab*);
94 static void cmd_scroll_up(struct tab*);
95 static void cmd_scroll_down(struct tab*);
96 static void cmd_kill_telescope(struct tab*);
97 static void cmd_push_button(struct tab*);
98 static void cmd_execute_extended_command(struct tab*);
99 static void cmd_tab_close(struct tab*);
100 static void cmd_tab_new(struct tab*);
101 static void cmd_tab_next(struct tab*);
102 static void cmd_tab_previous(struct tab*);
104 static void global_key_unbound(void);
106 static void cmd_mini_del(struct tab*);
107 static void cmd_mini_forward_char(struct tab*);
108 static void cmd_mini_backward_char(struct tab*);
109 static void cmd_mini_move_end_of_line(struct tab*);
110 static void cmd_mini_move_beginning_of_line(struct tab*);
111 static void cmd_mini_kill_line(struct tab*);
112 static void cmd_mini_abort();
113 static void cmd_mini_complete_and_exit();
115 static void eecmd_self_insert(void);
116 static void eecmd_select(void);
118 static struct line *nth_line(struct tab*, size_t);
119 static struct tab *current_tab(void);
120 static void dispatch_stdio(int, short, void*);
121 static void handle_clear_minibuf(int, short, void*);
122 static void handle_resize(int, short, void*);
123 static int word_bourdaries(const char*, const char*, const char**, const char**);
124 static void wrap_text(struct tab*, const char*, struct line*);
125 static int hardwrap_text(struct tab*, struct line*);
126 static int wrap_page(struct tab*);
127 static void print_line(struct line*);
128 static void redraw_tabline(void);
129 static void redraw_modeline(struct tab*);
130 static void redraw_minibuffer(void);
131 static void redraw_tab(struct tab*);
132 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
133 static void start_loading_anim(struct tab*);
134 static void update_loading_anim(int, short, void*);
135 static void stop_loading_anim(struct tab*);
136 static void load_url_in_tab(struct tab*, const char*);
137 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
138 static void exit_minibuffer(void);
139 static void new_tab(void);
141 static struct { int meta, key; } thiskey;
143 static WINDOW *tabline, *body, *modeline, *minibuf;
144 static int body_lines, body_cols;
146 static struct event clminibufev;
147 static int clminibufev_set;
148 static struct timeval clminibufev_timer = { 5, 0 };
149 static struct timeval loadingev_timer = { 0, 250000 };
151 static uint32_t tab_counter;
153 struct ui_state {
154 int curs_x;
155 int curs_y;
156 size_t line_off;
157 size_t line_max;
159 short loading_anim;
160 short loading_anim_step;
161 struct event loadingev;
163 TAILQ_HEAD(, line) head;
164 };
166 static char keybuf[64];
168 #define CTRL(n) ((n)&0x1F)
170 struct keytable {
171 char *p;
172 int k;
173 } keytable[] = {
174 { "<up>", KEY_UP },
175 { "<down>", KEY_DOWN },
176 { "<left>", KEY_LEFT },
177 { "<right>", KEY_RIGHT },
178 { "<prior>", KEY_PPAGE },
179 { "<next>", KEY_NPAGE },
180 { "<home>", KEY_HOME },
181 { "<end>", KEY_END },
182 /* ... */
183 { "del", KEY_BACKSPACE },
184 { "esc", 27 },
185 { "space", ' ' },
186 { "spc", ' ' },
187 { "enter", CTRL('m') },
188 { "ret", CTRL('m' )},
189 { "tab", CTRL('i') },
190 /* ... */
191 { NULL, 0 },
192 };
194 struct kmap {
195 TAILQ_HEAD(map, keymap) m;
196 void (*unhandled_input)(void);
197 };
199 struct kmap global_map,
200 minibuffer_map,
201 *current_map,
202 *base_map;
204 struct keymap {
205 int meta;
206 int key;
207 struct kmap map;
208 void (*fn)(struct tab*);
210 TAILQ_ENTRY(keymap) keymaps;
211 };
213 static int in_minibuffer;
215 static struct {
216 char *curmesg;
218 char buf[1025];
219 size_t off, len;
220 char prompt[16];
221 void (*donefn)(void);
222 void (*abortfn)(void);
223 } ministate;
225 static int
226 kbd(const char *key)
228 struct keytable *t;
230 for (t = keytable; t->p != NULL; ++t) {
231 if (has_prefix(key, t->p))
232 return t->k;
235 return *key;
238 static const char *
239 unkbd(int k)
241 struct keytable *t;
243 for (t = keytable; t->p != NULL; ++t) {
244 if (k == t->k)
245 return t->p;
248 return NULL;
251 static void
252 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
254 int ctrl, meta, k;
255 struct keymap *entry;
257 again:
258 if ((ctrl = has_prefix(key, "C-")))
259 key += 2;
260 if ((meta = has_prefix(key, "M-")))
261 key += 2;
262 if (*key == '\0')
263 _exit(1);
264 k = kbd(key);
266 if (ctrl)
267 k = CTRL(k);
269 /* skip key & spaces */
270 while (*key != '\0' && !isspace(*key))
271 ++key;
272 while (*key != '\0' && isspace(*key))
273 ++key;
275 TAILQ_FOREACH(entry, &map->m, keymaps) {
276 if (entry->meta == meta && entry->key == k) {
277 if (*key == '\0') {
278 entry->fn = fn;
279 return;
281 map = &entry->map;
282 goto again;
286 if ((entry = calloc(1, sizeof(*entry))) == NULL)
287 abort();
289 entry->meta = meta;
290 entry->key = k;
291 TAILQ_INIT(&entry->map.m);
293 if (TAILQ_EMPTY(&map->m))
294 TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
295 else
296 TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
298 if (*key != '\0') {
299 map = &entry->map;
300 goto again;
303 entry->fn = fn;
306 static inline void
307 global_set_key(const char *key, void (*fn)(struct tab*))
309 kmap_define_key(&global_map, key, fn);
312 static inline void
313 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
315 kmap_define_key(&minibuffer_map, key, fn);
318 static void
319 load_default_keys(void)
321 /* === global map === */
323 /* emacs */
324 global_set_key("C-p", cmd_previous_line);
325 global_set_key("C-n", cmd_next_line);
326 global_set_key("C-f", cmd_forward_char);
327 global_set_key("C-b", cmd_backward_char);
329 global_set_key("M-v", cmd_scroll_up);
330 global_set_key("C-v", cmd_scroll_down);
332 global_set_key("C-x C-c", cmd_kill_telescope);
334 global_set_key("M-x", cmd_execute_extended_command);
336 global_set_key("C-x t 0", cmd_tab_close);
337 global_set_key("C-x t 2", cmd_tab_new);
338 global_set_key("C-x t o", cmd_tab_next);
339 global_set_key("C-x t O", cmd_tab_previous);
341 /* vi/vi-like */
342 global_set_key("k", cmd_previous_line);
343 global_set_key("j", cmd_next_line);
344 global_set_key("l", cmd_forward_char);
345 global_set_key("h", cmd_backward_char);
347 global_set_key("K", cmd_scroll_line_up);
348 global_set_key("J", cmd_scroll_line_down);
350 /* tmp */
351 global_set_key("q", cmd_kill_telescope);
353 global_set_key(":", cmd_execute_extended_command);
355 /* cua */
356 global_set_key("<up>", cmd_previous_line);
357 global_set_key("<down>", cmd_next_line);
358 global_set_key("<right>", cmd_forward_char);
359 global_set_key("<left>", cmd_backward_char);
360 global_set_key("<prior>", cmd_scroll_up);
361 global_set_key("<next>", cmd_scroll_down);
363 /* "ncurses standard" */
364 global_set_key("C-l", cmd_redraw);
366 /* global */
367 global_set_key("C-m", cmd_push_button);
369 /* === minibuffer map === */
370 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
371 minibuffer_set_key("C-g", cmd_mini_abort);
372 minibuffer_set_key("esc", cmd_mini_abort);
373 minibuffer_set_key("del", cmd_mini_del);
375 minibuffer_set_key("C-f", cmd_mini_forward_char);
376 minibuffer_set_key("C-b", cmd_mini_backward_char);
377 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
378 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
379 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
380 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
381 minibuffer_set_key("C-k", cmd_mini_kill_line);
384 static int
385 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
387 struct line *vl;
389 tab->s->line_max++;
391 if ((vl = calloc(1, sizeof(*vl))) == NULL)
392 return 0;
394 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
395 free(vl);
396 return 0;
399 vl->type = l->type;
400 if (len != 0)
401 memcpy(vl->line, buf, len);
402 vl->alt = l->alt;
404 if (TAILQ_EMPTY(&tab->s->head))
405 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
406 else
407 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
408 return 1;
411 static void
412 empty_vlist(struct tab *tab)
414 struct line *l, *t;
416 tab->s->line_max = 0;
418 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
419 TAILQ_REMOVE(&tab->s->head, l, lines);
420 free(l->line);
421 /* l->alt references the original line! */
422 free(l);
426 static void
427 restore_cursor(struct tab *tab)
429 wmove(body, tab->s->curs_y, tab->s->curs_x);
432 static void
433 cmd_previous_line(struct tab *tab)
435 if (--tab->s->curs_y < 0) {
436 tab->s->curs_y = 0;
437 cmd_scroll_line_up(tab);
440 restore_cursor(tab);
443 static void
444 cmd_next_line(struct tab *tab)
446 if (++tab->s->curs_y > body_lines-1) {
447 tab->s->curs_y = body_lines-1;
448 cmd_scroll_line_down(tab);
451 restore_cursor(tab);
454 static void
455 cmd_forward_char(struct tab *tab)
457 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
458 restore_cursor(tab);
461 static void
462 cmd_backward_char(struct tab *tab)
464 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
465 restore_cursor(tab);
468 static void
469 cmd_redraw(struct tab *tab)
471 handle_resize(0, 0, NULL);
474 static void
475 cmd_scroll_line_up(struct tab *tab)
477 struct line *l;
479 if (tab->s->line_off == 0)
480 return;
482 l = nth_line(tab, --tab->s->line_off);
483 wscrl(body, -1);
484 wmove(body, 0, 0);
485 print_line(l);
488 static void
489 cmd_scroll_line_down(struct tab *tab)
491 struct line *l;
492 size_t n;
494 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
495 return;
497 tab->s->line_off++;
498 wscrl(body, 1);
500 if (tab->s->line_max - tab->s->line_off < body_lines)
501 return;
503 l = nth_line(tab, tab->s->line_off + body_lines-1);
504 wmove(body, body_lines-1, 0);
505 print_line(l);
508 static void
509 cmd_scroll_up(struct tab *tab)
511 size_t off;
513 off = body_lines+1;
515 for (; off > 0; --off)
516 cmd_scroll_line_up(tab);
519 static void
520 cmd_scroll_down(struct tab *tab)
522 ssize_t off;
524 off = tab->s->line_off + body_lines;
525 off = MIN(tab->s->line_max, off);
527 for (; off >= 0; --off)
528 cmd_scroll_line_down(tab);
531 static void
532 cmd_kill_telescope(struct tab *tab)
534 event_loopbreak();
537 static void
538 cmd_push_button(struct tab *tab)
540 struct line *l;
541 size_t nth;
543 nth = tab->s->line_off + tab->s->curs_y;
544 if (nth > tab->s->line_max)
545 return;
546 l = nth_line(tab, nth);
547 if (l->type != LINE_LINK)
548 return;
550 load_url_in_tab(tab, l->alt);
553 static void
554 cmd_execute_extended_command(struct tab *tab)
556 size_t len;
558 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
560 len = sizeof(ministate.prompt);
561 strlcpy(ministate.prompt, "", len);
563 if (thiskey.meta)
564 strlcat(ministate.prompt, "M-", len);
566 strlcat(ministate.prompt, keyname(thiskey.key), len);
567 strlcat(ministate.prompt, " ", len);
570 static void
571 cmd_tab_close(struct tab *tab)
573 struct tab *t;
575 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
576 TAILQ_NEXT(tab, tabs) == NULL) {
577 message("Can't close the only tab.");
578 return;
581 stop_tab(tab);
583 t = TAILQ_PREV(tab, tabshead, tabs);
584 t->flags |= TAB_CURRENT;
586 TAILQ_REMOVE(&tabshead, tab, tabs);
588 free(tab->s);
589 free(tab);
592 static void
593 cmd_tab_new(struct tab *tab)
595 new_tab();
598 static void
599 cmd_tab_next(struct tab *tab)
601 struct tab *t;
603 tab->flags &= ~TAB_CURRENT;
605 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
606 t = TAILQ_FIRST(&tabshead);
607 t->flags |= TAB_CURRENT;
610 static void
611 cmd_tab_previous(struct tab *tab)
613 struct tab *t;
615 tab->flags &= ~TAB_CURRENT;
617 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
618 t = TAILQ_LAST(&tabshead, tabshead);
619 t->flags |= TAB_CURRENT;
622 static void
623 global_key_unbound(void)
625 message("%s is undefined", keybuf);
628 static void
629 cmd_mini_del(struct tab *tab)
631 if (ministate.len == 0 || ministate.off == 0)
632 return;
634 memmove(&ministate.buf[ministate.off-1],
635 &ministate.buf[ministate.off],
636 ministate.len - ministate.off + 1);
637 ministate.off--;
638 ministate.len--;
641 static void
642 cmd_mini_forward_char(struct tab *tab)
644 if (ministate.off == ministate.len)
645 return;
646 ministate.off++;
649 static void
650 cmd_mini_backward_char(struct tab *tab)
652 if (ministate.off == 0)
653 return;
654 ministate.off--;
657 static void
658 cmd_mini_move_end_of_line(struct tab *tab)
660 ministate.off = ministate.len;
663 static void
664 cmd_mini_move_beginning_of_line(struct tab *tab)
666 ministate.off = 0;
669 static void
670 cmd_mini_kill_line(struct tab *tab)
672 if (ministate.off == ministate.len)
673 return;
674 ministate.buf[ministate.off] = '\0';
675 ministate.len -= ministate.off;
678 static void
679 cmd_mini_abort(struct tab *tab)
681 ministate.abortfn();
684 static void
685 cmd_mini_complete_and_exit(struct tab *tab)
687 ministate.donefn();
690 static void
691 eecmd_self_insert(void)
693 if (thiskey.meta || isspace(thiskey.key) ||
694 !isgraph(thiskey.key)) {
695 global_key_unbound();
696 return;
699 if (ministate.len == sizeof(ministate.buf) -1)
700 return;
702 /* TODO: utf8 handling! */
704 memmove(&ministate.buf[ministate.off+1],
705 &ministate.buf[ministate.off],
706 ministate.len - ministate.off + 1);
707 ministate.buf[ministate.off] = thiskey.key;
708 ministate.off++;
709 ministate.len++;
712 static void
713 eecmd_select(void)
715 exit_minibuffer();
716 message("TODO: try to execute %s", ministate.buf);
719 static struct line *
720 nth_line(struct tab *tab, size_t n)
722 struct line *l;
723 size_t i;
725 i = 0;
726 TAILQ_FOREACH(l, &tab->s->head, lines) {
727 if (i == n)
728 return l;
729 i++;
732 /* unreachable */
733 abort();
736 static struct tab *
737 current_tab(void)
739 struct tab *t;
741 TAILQ_FOREACH(t, &tabshead, tabs) {
742 if (t->flags & TAB_CURRENT)
743 return t;
746 /* unreachable */
747 abort();
750 static void
751 dispatch_stdio(int fd, short ev, void *d)
753 struct keymap *k;
754 const char *keyname;
755 char tmp[2] = {0};
757 thiskey.key = wgetch(body);
758 if (thiskey.key == ERR)
759 return;
760 if (thiskey.key == 27) {
761 /* TODO: make escape-time customizable */
763 thiskey.meta = 1;
764 thiskey.key = wgetch(body);
765 if (thiskey.key == ERR)
766 thiskey.key = 27;
767 } else
768 thiskey.meta = 0;
770 if (keybuf[0] != '\0')
771 strlcat(keybuf, " ", sizeof(keybuf));
772 if (thiskey.meta)
773 strlcat(keybuf, "M-", sizeof(keybuf));
774 if ((keyname = unkbd(thiskey.key)) != NULL)
775 strlcat(keybuf, keyname, sizeof(keybuf));
776 else {
777 tmp[0] = thiskey.key;
778 strlcat(keybuf, tmp, sizeof(keybuf));
781 TAILQ_FOREACH(k, &current_map->m, keymaps) {
782 if (k->meta == thiskey.meta &&
783 k->key == thiskey.key) {
784 if (k->fn == NULL)
785 current_map = &k->map;
786 else {
787 current_map = base_map;
788 strlcpy(keybuf, "", sizeof(keybuf));
789 k->fn(current_tab());
791 goto done;
795 if (current_map->unhandled_input != NULL)
796 current_map->unhandled_input();
797 else {
798 global_key_unbound();
801 strlcpy(keybuf, "", sizeof(keybuf));
802 current_map = base_map;
804 done:
805 redraw_tabline();
806 redraw_minibuffer();
807 restore_cursor(current_tab());
808 wrefresh(tabline);
809 wrefresh(modeline);
811 if (in_minibuffer) {
812 wrefresh(body);
813 wrefresh(minibuf);
814 } else {
815 wrefresh(minibuf);
816 wrefresh(body);
820 static void
821 handle_clear_minibuf(int fd, short ev, void *d)
823 clminibufev_set = 0;
825 free(ministate.curmesg);
826 ministate.curmesg = NULL;
828 redraw_minibuffer();
829 if (in_minibuffer) {
830 wrefresh(body);
831 wrefresh(minibuf);
832 } else {
833 wrefresh(minibuf);
834 wrefresh(body);
838 static void
839 handle_resize(int sig, short ev, void *d)
841 struct tab *tab;
843 endwin();
844 refresh();
845 clear();
847 /* move and resize the windows, in reverse order! */
849 mvwin(minibuf, LINES-1, 0);
850 wresize(minibuf, 1, COLS);
852 mvwin(modeline, LINES-2, 0);
853 wresize(modeline, 1, COLS);
855 wresize(body, LINES-3, COLS);
856 body_lines = LINES-3;
857 body_cols = COLS;
859 wresize(tabline, 1, COLS);
861 tab = current_tab();
863 wrap_page(tab);
864 redraw_tab(tab);
867 /*
868 * Helper function for wrap_text. Find the end of the current word
869 * and the end of the separator after the word.
870 */
871 static int
872 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
874 *endword = s;
875 *endword = s;
877 if (*s == '\0')
878 return 0;
880 /* find the end of the current world */
881 for (; *s != '\0'; ++s) {
882 if (strchr(sep, *s) != NULL)
883 break;
886 *endword = s;
888 /* find the end of the separator */
889 for (; *s != '\0'; ++s) {
890 if (strchr(sep, *s) == NULL)
891 break;
894 *endspc = s;
896 return 1;
899 static inline int
900 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
901 const char **line)
903 if (!push_line(tab, l, *line, *off - zero))
904 return 0;
905 *line += *off - zero;
906 *off = zero;
907 return 1;
910 static inline void
911 emitstr(const char **s, size_t len, size_t *off)
913 size_t i;
915 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
916 for (i = 0; i < len; ++i)
917 addch((*s)[i]);
918 *off += len;
919 *s += len;
922 /*
923 * Build a list of visual line by wrapping the given line, assuming
924 * that when printed will have a leading prefix prfx.
926 * TODO: it considers each byte one cell on the screen!
927 */
928 static void
929 wrap_text(struct tab *tab, const char *prfx, struct line *l)
931 size_t zero, off, len, split;
932 const char *endword, *endspc, *line, *linestart;
934 zero = strlen(prfx);
935 off = zero;
936 line = l->line;
937 linestart = l->line;
939 while (word_boundaries(line, " \t-", &endword, &endspc)) {
940 len = endword - line;
941 if (off + len >= body_cols) {
942 emitline(tab, zero, &off, l, &linestart);
943 while (len >= body_cols) {
944 /* hard wrap */
945 emitline(tab, zero, &off, l, &linestart);
946 len -= body_cols-1;
947 line += body_cols-1;
950 if (len != 0)
951 off += len;
952 } else
953 off += len;
955 /* print the spaces iff not at bol */
956 len = endspc - endword;
957 /* line = endspc; */
958 if (off != zero) {
959 if (off + len >= body_cols) {
960 emitline(tab, zero, &off, l, &linestart);
961 linestart = endspc;
962 } else
963 off += len;
966 line = endspc;
969 emitline(tab, zero, &off, l, &linestart);
972 static int
973 hardwrap_text(struct tab *tab, struct line *l)
975 size_t off, len;
976 const char *linestart;
978 len = strlen(l->line);
979 off = 0;
980 linestart = l->line;
982 while (len >= COLS) {
983 len -= COLS-1;
984 off = COLS-1;
985 if (!emitline(tab, 0, &off, l, &linestart))
986 return 0;
989 if (len != 0)
990 return emitline(tab, 0, &len, l, &linestart);
992 return 1;
995 static int
996 wrap_page(struct tab *tab)
998 struct line *l;
1000 empty_vlist(tab);
1002 TAILQ_FOREACH(l, &tab->page.head, lines) {
1003 switch (l->type) {
1004 case LINE_TEXT:
1005 wrap_text(tab, "", l);
1006 break;
1007 case LINE_LINK:
1008 wrap_text(tab, "=> ", l);
1009 break;
1010 case LINE_TITLE_1:
1011 wrap_text(tab, "# ", l);
1012 break;
1013 case LINE_TITLE_2:
1014 wrap_text(tab, "## ", l);
1015 break;
1016 case LINE_TITLE_3:
1017 wrap_text(tab, "### ", l);
1018 break;
1019 case LINE_ITEM:
1020 wrap_text(tab, "* ", l);
1021 break;
1022 case LINE_QUOTE:
1023 wrap_text(tab, "> ", l);
1024 break;
1025 case LINE_PRE_START:
1026 case LINE_PRE_END:
1027 push_line(tab, l, NULL, 0);
1028 break;
1029 case LINE_PRE_CONTENT:
1030 hardwrap_text(tab, l);
1031 break;
1034 return 1;
1037 static inline void
1038 print_line(struct line *l)
1040 const char *text = l->line;
1042 if (text == NULL)
1043 text = "";
1045 switch (l->type) {
1046 case LINE_TEXT:
1047 wprintw(body, "%s", text);
1048 break;
1049 case LINE_LINK:
1050 wattron(body, A_UNDERLINE);
1051 wprintw(body, "=> %s", text);
1052 wattroff(body, A_UNDERLINE);
1053 return;
1054 case LINE_TITLE_1:
1055 wattron(body, A_BOLD);
1056 wprintw(body, "# %s", text);
1057 wattroff(body, A_BOLD);
1058 return;
1059 case LINE_TITLE_2:
1060 wattron(body, A_BOLD);
1061 wprintw(body, "## %s", text);
1062 wattroff(body, A_BOLD);
1063 return;
1064 case LINE_TITLE_3:
1065 wattron(body, A_BOLD);
1066 wprintw(body, "### %s", text);
1067 wattroff(body, A_BOLD);
1068 return;
1069 case LINE_ITEM:
1070 wprintw(body, "* %s", text);
1071 return;
1072 case LINE_QUOTE:
1073 wattron(body, A_DIM);
1074 wprintw(body, "> %s", text);
1075 wattroff(body, A_DIM);
1076 return;
1077 case LINE_PRE_START:
1078 case LINE_PRE_END:
1079 wprintw(body, "```");
1080 return;
1081 case LINE_PRE_CONTENT:
1082 wprintw(body, "%s", text);
1083 return;
1087 static void
1088 redraw_tabline(void)
1090 struct tab *tab;
1091 int current;
1093 wclear(tabline);
1094 wbkgd(tabline, A_REVERSE);
1096 wprintw(tabline, " ");
1097 TAILQ_FOREACH(tab, &tabshead, tabs) {
1098 current = tab->flags & TAB_CURRENT;
1099 if (current) {
1100 wattroff(tabline, A_REVERSE);
1101 wattron(tabline, A_STANDOUT);
1104 wprintw(tabline, " %d:todo title ",
1105 tab->id);
1107 if (current) {
1108 wattron(tabline, A_REVERSE);
1109 wattroff(tabline, A_STANDOUT);
1114 static void
1115 redraw_modeline(struct tab *tab)
1117 int x, y, max_x, max_y;
1118 const char *mode = "text/gemini-mode";
1119 const char *spin = "-\\|/";
1121 wclear(modeline);
1122 wattron(modeline, A_REVERSE);
1123 wmove(modeline, 0, 0);
1125 wprintw(modeline, "-%c %s %s ",
1126 spin[tab->s->loading_anim_step], mode, tab->urlstr);
1127 getyx(modeline, y, x);
1128 getmaxyx(modeline, max_y, max_x);
1130 (void)y;
1131 (void)max_y;
1133 for (; x < max_x; ++x)
1134 waddstr(modeline, "-");
1137 static void
1138 redraw_minibuffer(void)
1140 size_t skip = 0, off = 0;
1142 wclear(minibuf);
1143 if (!in_minibuffer)
1144 goto message;
1146 off = strlen(ministate.prompt);
1148 while (ministate.off - skip > COLS / 2) {
1149 skip += MIN(ministate.off/4, 1);
1152 mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1153 ministate.buf + skip);
1155 message:
1156 if (ministate.curmesg != NULL) {
1157 if (in_minibuffer)
1158 wprintw(minibuf, " [%s]", ministate.curmesg);
1159 else
1160 wprintw(minibuf, "%s", ministate.curmesg);
1163 wmove(minibuf, 0, off + ministate.off - skip);
1166 static void
1167 redraw_tab(struct tab *tab)
1169 struct line *l;
1170 int line;
1172 werase(body);
1174 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1175 if (TAILQ_EMPTY(&tab->s->head))
1176 return;
1178 line = 0;
1179 l = nth_line(tab, tab->s->line_off);
1180 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1181 wmove(body, line, 0);
1182 print_line(l);
1183 line++;
1184 if (line == body_lines)
1185 break;
1188 redraw_tabline();
1189 redraw_modeline(tab);
1190 redraw_minibuffer();
1192 restore_cursor(tab);
1193 wrefresh(tabline);
1194 wrefresh(modeline);
1196 if (in_minibuffer) {
1197 wrefresh(body);
1198 wrefresh(minibuf);
1199 } else {
1200 wrefresh(minibuf);
1201 wrefresh(body);
1205 static void
1206 message(const char *fmt, ...)
1208 va_list ap;
1210 if (clminibufev_set)
1211 evtimer_del(&clminibufev);
1212 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1213 evtimer_add(&clminibufev, &clminibufev_timer);
1214 clminibufev_set = 1;
1216 va_start(ap, fmt);
1217 /* TODO: what to do if the allocation fails here? */
1218 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1219 ministate.curmesg = NULL;
1220 va_end(ap);
1222 redraw_minibuffer();
1224 if (in_minibuffer) {
1225 wrefresh(body);
1226 wrefresh(minibuf);
1227 } else {
1228 wrefresh(minibuf);
1229 wrefresh(body);
1233 static void
1234 start_loading_anim(struct tab *tab)
1236 if (tab->s->loading_anim)
1237 return;
1238 tab->s->loading_anim = 1;
1239 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1240 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1243 static void
1244 update_loading_anim(int fd, short ev, void *d)
1246 struct tab *tab = d;
1248 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1250 redraw_modeline(tab);
1251 wrefresh(modeline);
1253 wrefresh(body);
1254 if (in_minibuffer)
1255 wrefresh(minibuf);
1257 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1260 static void
1261 stop_loading_anim(struct tab *tab)
1263 if (!tab->s->loading_anim)
1264 return;
1265 evtimer_del(&tab->s->loadingev);
1266 tab->s->loading_anim = 0;
1267 tab->s->loading_anim_step = 0;
1269 redraw_modeline(tab);
1271 wrefresh(modeline);
1272 wrefresh(body);
1273 if (in_minibuffer)
1274 wrefresh(minibuf);
1277 static void
1278 load_url_in_tab(struct tab *tab, const char *url)
1280 empty_vlist(tab);
1281 message("Loading %s...", url);
1282 start_loading_anim(tab);
1283 load_url(tab, url);
1285 tab->s->curs_x = 0;
1286 tab->s->curs_y = 0;
1287 redraw_tab(tab);
1290 static void
1291 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1292 void (*abortfn)(void))
1294 in_minibuffer = 1;
1295 base_map = &minibuffer_map;
1296 current_map = &minibuffer_map;
1298 base_map->unhandled_input = self_insert_fn;
1300 ministate.donefn = donefn;
1301 ministate.abortfn = abortfn;
1302 memset(ministate.buf, 0, sizeof(ministate.buf));
1303 ministate.off = 0;
1304 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1307 static void
1308 exit_minibuffer(void)
1310 wclear(minibuf);
1312 in_minibuffer = 0;
1313 base_map = &global_map;
1314 current_map = &global_map;
1317 static void
1318 new_tab(void)
1320 struct tab *tab, *t;
1321 const char *url = "about:new";
1323 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1324 goto err;
1326 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1327 goto err;
1329 TAILQ_INIT(&tab->s->head);
1330 TAILQ_FOREACH(t, &tabshead, tabs) {
1331 t->flags &= ~TAB_CURRENT;
1334 tab->id = tab_counter++;
1335 tab->flags = TAB_CURRENT;
1337 if (TAILQ_EMPTY(&tabshead))
1338 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1339 else
1340 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1342 load_url_in_tab(tab, url);
1343 return;
1345 err:
1346 event_loopbreak();
1349 int
1350 ui_init(void)
1352 setlocale(LC_ALL, "");
1354 TAILQ_INIT(&global_map.m);
1355 global_map.unhandled_input = global_key_unbound;
1357 TAILQ_INIT(&minibuffer_map.m);
1359 base_map = &global_map;
1360 current_map = &global_map;
1361 load_default_keys();
1363 initscr();
1364 raw();
1365 noecho();
1367 nonl();
1368 intrflush(stdscr, FALSE);
1370 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1371 return 0;
1372 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1373 return 0;
1374 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1375 return 0;
1376 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1377 return 0;
1379 body_lines = LINES-3;
1380 body_cols = COLS;
1382 keypad(body, TRUE);
1383 scrollok(body, TRUE);
1385 /* non-blocking input */
1386 wtimeout(body, 0);
1388 mvwprintw(body, 0, 0, "");
1390 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1391 event_add(&stdioev, NULL);
1393 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1394 signal_add(&winchev, NULL);
1396 new_tab();
1398 return 1;
1401 void
1402 ui_on_tab_loaded(struct tab *tab)
1404 stop_loading_anim(tab);
1405 message("Loaded %s", tab->urlstr);
1408 void
1409 ui_on_tab_refresh(struct tab *tab)
1411 if (!(tab->flags & TAB_CURRENT))
1412 return;
1414 wrap_page(tab);
1415 redraw_tab(tab);
1418 void
1419 ui_end(void)
1421 endwin();