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 *
27 * This means that on every resize we have to clear our list of lines
28 * and re-render everything. A clever approach would be to do this
29 * ``on-demand'', but it's still missing.
30 *
31 */
33 #include "telescope.h"
35 #include <assert.h>
36 #include <curses.h>
37 #include <event.h>
38 #include <locale.h>
39 #include <signal.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 static struct event stdioev, winchev;
47 static void restore_curs_x(struct buffer *);
49 static struct vline *nth_line(struct buffer*, size_t);
50 static int readkey(void);
51 static void dispatch_stdio(int, short, void*);
52 static void handle_clear_echoarea(int, short, void*);
53 static void handle_resize(int, short, void*);
54 static void handle_resize_nodelay(int, short, void*);
55 static int wrap_page(struct buffer*, int);
56 static void print_vline(int, int, WINDOW*, struct vline*);
57 static void redraw_tabline(void);
58 static void redraw_window(WINDOW*, int, int, struct buffer*);
59 static void redraw_help(void);
60 static void redraw_body(struct tab*);
61 static void redraw_modeline(struct tab*);
62 static void redraw_echoarea(void);
63 static void redraw_tab(struct tab*);
64 static void emit_help_item(char*, void*);
65 static void rec_compute_help(struct kmap*, char*, size_t);
66 static void recompute_help(void);
67 static void update_loading_anim(int, short, void*);
68 static void stop_loading_anim(struct tab*);
70 static int x_offset;
72 struct thiskey thiskey;
74 static struct event resizeev;
75 static struct timeval resize_timer = { 0, 250000 };
77 static WINDOW *tabline, *body, *modeline, *echoarea;
79 int body_lines, body_cols;
81 static WINDOW *help;
82 static struct buffer helpwin;
83 static int help_lines, help_cols;
85 static int side_window;
87 static struct event clechoev;
88 static struct timeval clechoev_timer = { 5, 0 };
89 static struct timeval loadingev_timer = { 0, 250000 };
91 static uint32_t tab_counter;
93 static char keybuf[64];
95 struct kmap global_map,
96 minibuffer_map,
97 *current_map,
98 *base_map;
100 int in_minibuffer;
102 static inline void
103 update_x_offset(void)
105 if (olivetti_mode && fill_column < body_cols)
106 x_offset = (body_cols - fill_column)/2;
107 else
108 x_offset = 0;
111 void
112 save_excursion(struct excursion *place, struct buffer *buffer)
114 place->curs_x = buffer->curs_x;
115 place->curs_y = buffer->curs_y;
116 place->line_off = buffer->line_off;
117 place->current_line = buffer->current_line;
118 place->cpoff = buffer->cpoff;
121 void
122 restore_excursion(struct excursion *place, struct buffer *buffer)
124 buffer->curs_x = place->curs_x;
125 buffer->curs_y = place->curs_y;
126 buffer->line_off = place->line_off;
127 buffer->current_line = place->current_line;
128 buffer->cpoff = place->cpoff;
131 static void
132 restore_curs_x(struct buffer *buffer)
134 struct vline *vl;
135 const char *prfx;
137 vl = buffer->current_line;
138 if (vl == NULL || vl->line == NULL)
139 buffer->curs_x = buffer->cpoff = 0;
140 else
141 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
143 buffer->curs_x += x_offset;
145 if (vl != NULL) {
146 prfx = line_prefixes[vl->parent->type].prfx1;
147 buffer->curs_x += utf8_swidth(prfx);
151 void
152 global_key_unbound(void)
154 message("%s is undefined", keybuf);
157 static struct vline *
158 nth_line(struct buffer *buffer, size_t n)
160 struct vline *vl;
161 size_t i;
163 i = 0;
164 TAILQ_FOREACH(vl, &buffer->head, vlines) {
165 if (i == n)
166 return vl;
167 i++;
170 /* unreachable */
171 abort();
174 struct tab *
175 current_tab(void)
177 struct tab *t;
179 TAILQ_FOREACH(t, &tabshead, tabs) {
180 if (t->flags & TAB_CURRENT)
181 return t;
184 /* unreachable */
185 abort();
188 struct buffer *
189 current_buffer(void)
191 if (in_minibuffer)
192 return &ministate.buffer;
193 return &current_tab()->buffer;
196 static int
197 readkey(void)
199 uint32_t state = 0;
201 if ((thiskey.key = wgetch(body)) == ERR)
202 return 0;
204 thiskey.meta = thiskey.key == 27;
205 if (thiskey.meta) {
206 thiskey.key = wgetch(body);
207 if (thiskey.key == ERR || thiskey.key == 27) {
208 thiskey.meta = 0;
209 thiskey.key = 27;
213 thiskey.cp = 0;
214 if ((unsigned int)thiskey.key < UINT8_MAX) {
215 while (1) {
216 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
217 break;
218 if ((thiskey.key = wgetch(body)) == ERR) {
219 message("Error decoding user input");
220 return 0;
225 return 1;
228 static void
229 dispatch_stdio(int fd, short ev, void *d)
231 struct keymap *k;
232 const char *keyname;
233 char tmp[5] = {0};
235 if (!readkey())
236 return;
238 if (keybuf[0] != '\0')
239 strlcat(keybuf, " ", sizeof(keybuf));
240 if (thiskey.meta)
241 strlcat(keybuf, "M-", sizeof(keybuf));
242 if (thiskey.cp != 0) {
243 utf8_encode(thiskey.cp, tmp);
244 strlcat(keybuf, tmp, sizeof(keybuf));
245 } else {
246 if ((keyname = unkbd(thiskey.key)) != NULL)
247 strlcat(keybuf, keyname, sizeof(keybuf));
248 else {
249 tmp[0] = thiskey.key;
250 strlcat(keybuf, tmp, sizeof(keybuf));
254 TAILQ_FOREACH(k, &current_map->m, keymaps) {
255 if (k->meta == thiskey.meta &&
256 k->key == thiskey.key) {
257 if (k->fn == NULL)
258 current_map = &k->map;
259 else {
260 current_map = base_map;
261 strlcpy(keybuf, "", sizeof(keybuf));
262 k->fn(current_buffer());
264 goto done;
268 if (current_map->unhandled_input != NULL)
269 current_map->unhandled_input();
270 else
271 global_key_unbound();
273 strlcpy(keybuf, "", sizeof(keybuf));
274 current_map = base_map;
276 done:
277 if (side_window)
278 recompute_help();
280 redraw_tab(current_tab());
283 static void
284 handle_clear_echoarea(int fd, short ev, void *d)
286 free(ministate.curmesg);
287 ministate.curmesg = NULL;
289 redraw_echoarea();
290 if (in_minibuffer) {
291 wrefresh(body);
292 wrefresh(echoarea);
293 } else {
294 wrefresh(echoarea);
295 wrefresh(body);
299 static void
300 handle_resize(int sig, short ev, void *d)
302 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
303 event_del(&resizeev);
305 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
306 evtimer_add(&resizeev, &resize_timer);
309 static void
310 handle_resize_nodelay(int s, short ev, void *d)
312 struct tab *tab;
314 endwin();
315 refresh();
316 clear();
318 /* move and resize the windows, in reverse order! */
320 mvwin(echoarea, LINES-1, 0);
321 wresize(echoarea, 1, COLS);
323 mvwin(modeline, LINES-2, 0);
324 wresize(modeline, 1, COLS);
326 body_lines = LINES-3;
327 body_cols = COLS;
329 if (side_window) {
330 help_cols = 0.3 * COLS;
331 help_lines = LINES-3;
332 mvwin(help, 1, 0);
333 wresize(help, help_lines, help_cols);
335 wrap_page(&helpwin, help_cols);
337 body_cols = COLS - help_cols - 1;
338 mvwin(body, 1, help_cols);
339 } else
340 mvwin(body, 1, 0);
342 update_x_offset();
343 wresize(body, body_lines, body_cols);
345 wresize(tabline, 1, COLS);
347 tab = current_tab();
349 wrap_page(&tab->buffer, body_cols);
350 redraw_tab(tab);
353 static int
354 wrap_page(struct buffer *buffer, int width)
356 struct line *l;
357 const struct line *top_orig, *orig;
358 struct vline *vl;
359 int pre_width;
360 const char *prfx;
362 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
363 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
365 buffer->top_line = NULL;
366 buffer->current_line = NULL;
368 buffer->force_redraw = 1;
369 buffer->curs_y = 0;
370 buffer->line_off = 0;
372 empty_vlist(buffer);
374 TAILQ_FOREACH(l, &buffer->page.head, lines) {
375 prfx = line_prefixes[l->type].prfx1;
376 switch (l->type) {
377 case LINE_TEXT:
378 case LINE_LINK:
379 case LINE_TITLE_1:
380 case LINE_TITLE_2:
381 case LINE_TITLE_3:
382 case LINE_ITEM:
383 case LINE_QUOTE:
384 case LINE_PRE_START:
385 case LINE_PRE_END:
386 wrap_text(buffer, prfx, l, MIN(fill_column, width));
387 break;
388 case LINE_PRE_CONTENT:
389 if (olivetti_mode)
390 pre_width = MIN(fill_column, width);
391 else
392 pre_width = width;
393 hardwrap_text(buffer, l, pre_width);
394 break;
397 if (top_orig == l && buffer->top_line == NULL) {
398 buffer->line_off = buffer->line_max-1;
399 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
401 while (1) {
402 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
403 if (vl == NULL || vl->parent != orig)
404 break;
405 buffer->top_line = vl;
406 buffer->line_off--;
410 if (orig == l && buffer->current_line == NULL) {
411 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
413 while (1) {
414 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
415 if (vl == NULL || vl->parent != orig)
416 break;
417 buffer->current_line = vl;
422 if (buffer->current_line == NULL)
423 buffer->current_line = TAILQ_FIRST(&buffer->head);
425 if (buffer->top_line == NULL)
426 buffer->top_line = buffer->current_line;
428 return 1;
431 static void
432 print_vline(int off, int width, WINDOW *window, struct vline *vl)
434 const char *text;
435 const char *prfx;
436 struct line_face *f;
437 int i, left, x, y;
439 f = &line_faces[vl->parent->type];
441 /* unused, set by getyx */
442 (void)y;
444 if (!vl->flags)
445 prfx = line_prefixes[vl->parent->type].prfx1;
446 else
447 prfx = line_prefixes[vl->parent->type].prfx2;
449 text = vl->line;
450 if (text == NULL)
451 text = "";
453 wattr_on(window, body_face.left, NULL);
454 for (i = 0; i < off; i++)
455 waddch(window, ' ');
456 wattr_off(window, body_face.left, NULL);
458 wattr_on(window, f->prefix, NULL);
459 wprintw(window, "%s", prfx);
460 wattr_off(window, f->prefix, NULL);
462 wattr_on(window, f->text, NULL);
463 wprintw(window, "%s", text);
464 wattr_off(window, f->text, NULL);
466 getyx(window, y, x);
468 left = width - x;
470 wattr_on(window, f->trail, NULL);
471 for (i = 0; i < left - off; ++i)
472 waddch(window, ' ');
473 wattr_off(window, f->trail, NULL);
475 wattr_on(window, body_face.right, NULL);
476 for (i = 0; i < off; i++)
477 waddch(window, ' ');
478 wattr_off(window, body_face.right, NULL);
482 static void
483 redraw_tabline(void)
485 struct tab *tab;
486 size_t toskip, ots, tabwidth, space, x;
487 int current, y, truncated;
488 const char *title;
489 char buf[25];
491 x = 0;
493 /* unused, but setted by a getyx */
494 (void)y;
496 tabwidth = sizeof(buf)+1;
497 space = COLS-2;
499 toskip = 0;
500 TAILQ_FOREACH(tab, &tabshead, tabs) {
501 toskip++;
502 if (tab->flags & TAB_CURRENT)
503 break;
506 if (toskip * tabwidth < space)
507 toskip = 0;
508 else {
509 ots = toskip;
510 toskip--;
511 while (toskip != 0 &&
512 (ots - toskip+1) * tabwidth < space)
513 toskip--;
516 werase(tabline);
517 wattr_on(tabline, tab_face.background, NULL);
518 wprintw(tabline, toskip == 0 ? " " : "<");
519 wattr_off(tabline, tab_face.background, NULL);
521 truncated = 0;
522 TAILQ_FOREACH(tab, &tabshead, tabs) {
523 if (truncated)
524 break;
525 if (toskip != 0) {
526 toskip--;
527 continue;
530 getyx(tabline, y, x);
531 if (x + sizeof(buf)+2 >= (size_t)COLS)
532 truncated = 1;
534 current = tab->flags & TAB_CURRENT;
536 if (*(title = tab->buffer.page.title) == '\0')
537 title = tab->hist_cur->h;
539 if (tab->flags & TAB_URGENT)
540 strlcpy(buf, "!", sizeof(buf));
541 else
542 strlcpy(buf, " ", sizeof(buf));
544 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
545 /* truncation happens */
546 strlcpy(&buf[sizeof(buf)-4], "...", 4);
547 } else {
548 /* pad with spaces */
549 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
550 /* nop */ ;
553 if (current)
554 wattr_on(tabline, tab_face.current, NULL);
555 else
556 wattr_on(tabline, tab_face.tab, NULL);
558 wprintw(tabline, "%s", buf);
559 if (TAILQ_NEXT(tab, tabs) != NULL)
560 wprintw(tabline, " ");
562 if (current)
563 wattr_off(tabline, tab_face.current, NULL);
564 else
565 wattr_off(tabline, tab_face.tab, NULL);
568 wattr_on(tabline, tab_face.background, NULL);
569 for (; x < (size_t)COLS; ++x)
570 waddch(tabline, ' ');
571 if (truncated)
572 mvwprintw(tabline, 0, COLS-1, ">");
573 wattr_off(tabline, tab_face.background, NULL);
576 /*
577 * Compute the first visible line around vl. Try to search forward
578 * until the end of the buffer; if a visible line is not found, search
579 * backward. Return NULL if no viable line was found.
580 */
581 static inline struct vline *
582 adjust_line(struct vline *vl, struct buffer *buffer)
584 struct vline *t;
586 if (!(vl->parent->flags & L_HIDDEN))
587 return vl;
589 /* search forward */
590 for (t = vl;
591 t != NULL && t->parent->flags & L_HIDDEN;
592 t = TAILQ_NEXT(t, vlines))
593 ; /* nop */
595 if (t != NULL)
596 return t;
598 /* search backward */
599 for (t = vl;
600 t != NULL && t->parent->flags & L_HIDDEN;
601 t = TAILQ_PREV(t, vhead, vlines))
602 ; /* nop */
604 return t;
607 static void
608 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
610 struct vline *vl;
611 int l, onscreen;
613 restore_curs_x(buffer);
615 /*
616 * TODO: ignoring buffer->force_update and always
617 * re-rendering. In theory we can recompute the y position
618 * without a re-render, and optimize here. It's not the only
619 * optimisation possible here, wscrl wolud also be an
620 * interesting one.
621 */
623 again:
624 werase(win);
625 buffer->curs_y = 0;
627 if (TAILQ_EMPTY(&buffer->head))
628 goto end;
630 buffer->top_line = adjust_line(buffer->top_line, buffer);
631 if (buffer->top_line == NULL)
632 goto end;
634 buffer->current_line = adjust_line(buffer->current_line, buffer);
636 l = 0;
637 onscreen = 0;
638 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
639 if (vl->parent->flags & L_HIDDEN)
640 continue;
642 wmove(win, l, 0);
643 print_vline(x_offset, width, win, vl);
645 if (vl == buffer->current_line)
646 onscreen = 1;
648 if (!onscreen)
649 buffer->curs_y++;
651 l++;
652 if (l == height)
653 break;
656 if (!onscreen) {
657 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
658 if (vl == buffer->current_line)
659 break;
660 if (vl->parent->flags & L_HIDDEN)
661 continue;
662 buffer->line_off++;
663 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
666 goto again;
669 buffer->last_line_off = buffer->line_off;
670 buffer->force_redraw = 0;
671 end:
672 wmove(win, buffer->curs_y, buffer->curs_x);
675 static void
676 redraw_help(void)
678 redraw_window(help, help_lines, help_cols, &helpwin);
681 static void
682 redraw_body(struct tab *tab)
684 static struct tab *last_tab;
686 if (last_tab != tab)
687 tab->buffer.force_redraw =1;
688 last_tab = tab;
690 redraw_window(body, body_lines, body_cols, &tab->buffer);
693 static inline char
694 trust_status_char(enum trust_state ts)
696 switch (ts) {
697 case TS_UNKNOWN: return 'u';
698 case TS_UNTRUSTED: return '!';
699 case TS_TEMP_TRUSTED: return '!';
700 case TS_TRUSTED: return 'v';
701 case TS_VERIFIED: return 'V';
702 default: return 'X';
706 static void
707 redraw_modeline(struct tab *tab)
709 double pct;
710 int x, y, max_x, max_y;
711 const char *mode = tab->buffer.page.name;
712 const char *spin = "-\\|/";
714 werase(modeline);
715 wattr_on(modeline, modeline_face.background, NULL);
716 wmove(modeline, 0, 0);
718 wprintw(modeline, "-%c%c %s ",
719 spin[tab->loading_anim_step],
720 trust_status_char(tab->trust),
721 mode == NULL ? "(none)" : mode);
723 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
725 if (tab->buffer.line_max <= (size_t)body_lines)
726 wprintw(modeline, "All ");
727 else if (tab->buffer.line_off == 0)
728 wprintw(modeline, "Top ");
729 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
730 wprintw(modeline, "Bottom ");
731 else
732 wprintw(modeline, "%.0f%% ", pct);
734 wprintw(modeline, "%d/%d %s ",
735 tab->buffer.line_off + tab->buffer.curs_y,
736 tab->buffer.line_max,
737 tab->hist_cur->h);
739 getyx(modeline, y, x);
740 getmaxyx(modeline, max_y, max_x);
742 (void)y;
743 (void)max_y;
745 for (; x < max_x; ++x)
746 waddstr(modeline, "-");
748 wattr_off(modeline, modeline_face.background, NULL);
751 static void
752 redraw_echoarea(void)
754 struct tab *tab;
755 size_t off_y, off_x = 0;
756 char *start = NULL, *c = NULL;
758 /* unused, but set by getyx */
759 (void)off_y;
761 wattr_on(echoarea, minibuffer_face.background, NULL);
762 werase(echoarea);
764 if (in_minibuffer) {
765 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
766 if (ministate.hist_cur != NULL)
767 wprintw(echoarea, "(%zu/%zu) ",
768 ministate.hist_off + 1,
769 ministate.history->len);
771 getyx(echoarea, off_y, off_x);
773 start = ministate.hist_cur != NULL
774 ? ministate.hist_cur->h
775 : ministate.buf;
776 c = utf8_nth(ministate.buffer.current_line->line,
777 ministate.buffer.cpoff);
778 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
779 start = utf8_next_cp(start);
782 waddstr(echoarea, start);
785 if (ministate.curmesg != NULL)
786 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
787 ministate.curmesg);
789 if (!in_minibuffer && ministate.curmesg == NULL)
790 waddstr(echoarea, keybuf);
792 /* If nothing else, show the URL at point */
793 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
794 tab = current_tab();
795 if (tab->buffer.current_line != NULL &&
796 tab->buffer.current_line->parent->type == LINE_LINK)
797 waddstr(echoarea, tab->buffer.current_line->parent->alt);
800 if (in_minibuffer)
801 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
803 wattr_off(echoarea, minibuffer_face.background, NULL);
806 static void
807 redraw_tab(struct tab *tab)
809 if (side_window) {
810 redraw_help();
811 wnoutrefresh(help);
814 redraw_tabline();
815 redraw_body(tab);
816 redraw_modeline(tab);
817 redraw_echoarea();
819 wnoutrefresh(tabline);
820 wnoutrefresh(modeline);
822 if (in_minibuffer) {
823 wnoutrefresh(body);
824 wnoutrefresh(echoarea);
825 } else {
826 wnoutrefresh(echoarea);
827 wnoutrefresh(body);
830 doupdate();
833 static void
834 emit_help_item(char *prfx, void *fn)
836 struct line *l;
837 struct cmd *cmd;
839 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
840 if (fn == cmd->fn)
841 break;
843 assert(cmd != NULL);
845 if ((l = calloc(1, sizeof(*l))) == NULL)
846 abort();
848 l->type = LINE_TEXT;
849 l->alt = NULL;
851 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
853 if (TAILQ_EMPTY(&helpwin.page.head))
854 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
855 else
856 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
859 static void
860 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
862 struct keymap *k;
863 char p[32];
864 const char *kn;
866 TAILQ_FOREACH(k, &keymap->m, keymaps) {
867 strlcpy(p, prfx, sizeof(p));
868 if (*p != '\0')
869 strlcat(p, " ", sizeof(p));
870 if (k->meta)
871 strlcat(p, "M-", sizeof(p));
872 if ((kn = unkbd(k->key)) != NULL)
873 strlcat(p, kn, sizeof(p));
874 else
875 strlcat(p, keyname(k->key), sizeof(p));
877 if (k->fn == NULL)
878 rec_compute_help(&k->map, p, sizeof(p));
879 else
880 emit_help_item(p, k->fn);
884 static void
885 recompute_help(void)
887 char p[32] = { 0 };
889 empty_vlist(&helpwin);
890 empty_linelist(&helpwin);
891 rec_compute_help(current_map, p, sizeof(p));
892 wrap_page(&helpwin, help_cols);
895 void
896 vmessage(const char *fmt, va_list ap)
898 if (evtimer_pending(&clechoev, NULL))
899 evtimer_del(&clechoev);
901 free(ministate.curmesg);
902 ministate.curmesg = NULL;
904 if (fmt != NULL) {
905 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
906 evtimer_add(&clechoev, &clechoev_timer);
908 /* TODO: what to do if the allocation fails here? */
909 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
910 ministate.curmesg = NULL;
913 redraw_echoarea();
914 if (in_minibuffer) {
915 wrefresh(body);
916 wrefresh(echoarea);
917 } else {
918 wrefresh(echoarea);
919 wrefresh(body);
923 void
924 message(const char *fmt, ...)
926 va_list ap;
928 va_start(ap, fmt);
929 vmessage(fmt, ap);
930 va_end(ap);
933 void
934 start_loading_anim(struct tab *tab)
936 if (tab->loading_anim)
937 return;
938 tab->loading_anim = 1;
939 evtimer_set(&tab->loadingev, update_loading_anim, tab);
940 evtimer_add(&tab->loadingev, &loadingev_timer);
943 static void
944 update_loading_anim(int fd, short ev, void *d)
946 struct tab *tab = d;
948 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
950 if (tab->flags & TAB_CURRENT) {
951 redraw_modeline(tab);
952 wrefresh(modeline);
953 wrefresh(body);
954 if (in_minibuffer)
955 wrefresh(echoarea);
958 evtimer_add(&tab->loadingev, &loadingev_timer);
961 static void
962 stop_loading_anim(struct tab *tab)
964 if (!tab->loading_anim)
965 return;
966 evtimer_del(&tab->loadingev);
967 tab->loading_anim = 0;
968 tab->loading_anim_step = 0;
970 if (!(tab->flags & TAB_CURRENT))
971 return;
973 redraw_modeline(tab);
975 wrefresh(modeline);
976 wrefresh(body);
977 if (in_minibuffer)
978 wrefresh(echoarea);
981 void
982 load_url_in_tab(struct tab *tab, const char *url)
984 message("Loading %s...", url);
985 start_loading_anim(tab);
986 load_url(tab, url);
988 tab->buffer.curs_x = 0;
989 tab->buffer.curs_y = 0;
990 redraw_tab(tab);
993 void
994 switch_to_tab(struct tab *tab)
996 struct tab *t;
998 TAILQ_FOREACH(t, &tabshead, tabs) {
999 t->flags &= ~TAB_CURRENT;
1002 tab->flags |= TAB_CURRENT;
1003 tab->flags &= ~TAB_URGENT;
1006 unsigned int
1007 tab_new_id(void)
1009 return tab_counter++;
1012 struct tab *
1013 new_tab(const char *url)
1015 struct tab *tab;
1017 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1018 event_loopbreak();
1019 return NULL;
1021 tab->fd = -1;
1023 TAILQ_INIT(&tab->hist.head);
1025 TAILQ_INIT(&tab->buffer.head);
1027 tab->id = tab_new_id();
1028 switch_to_tab(tab);
1030 if (TAILQ_EMPTY(&tabshead))
1031 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1032 else
1033 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1035 load_url_in_tab(tab, url);
1036 return tab;
1039 int
1040 ui_init()
1042 setlocale(LC_ALL, "");
1044 TAILQ_INIT(&eecmd_history.head);
1045 TAILQ_INIT(&ir_history.head);
1046 TAILQ_INIT(&lu_history.head);
1048 ministate.line.type = LINE_TEXT;
1049 ministate.vline.parent = &ministate.line;
1050 ministate.buffer.current_line = &ministate.vline;
1052 /* initialize help window */
1053 TAILQ_INIT(&helpwin.head);
1055 base_map = &global_map;
1056 current_map = &global_map;
1058 initscr();
1060 if (enable_colors) {
1061 if (has_colors()) {
1062 start_color();
1063 use_default_colors();
1064 } else
1065 enable_colors = 0;
1068 config_apply_style();
1070 raw();
1071 noecho();
1072 nonl();
1073 intrflush(stdscr, FALSE);
1075 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1076 return 0;
1077 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1078 return 0;
1079 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1080 return 0;
1081 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1082 return 0;
1083 if ((help = newwin(1, 1, 1, 0)) == NULL)
1084 return 0;
1086 body_lines = LINES-3;
1087 body_cols = COLS;
1089 wbkgd(body, body_face.body);
1090 wbkgd(echoarea, minibuffer_face.background);
1092 update_x_offset();
1094 keypad(body, TRUE);
1095 scrollok(body, FALSE);
1097 /* non-blocking input */
1098 wtimeout(body, 0);
1100 mvwprintw(body, 0, 0, "");
1102 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1103 event_add(&stdioev, NULL);
1105 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1106 signal_add(&winchev, NULL);
1108 return 1;
1111 void
1112 ui_on_tab_loaded(struct tab *tab)
1114 stop_loading_anim(tab);
1115 message("Loaded %s", tab->hist_cur->h);
1117 redraw_tabline();
1118 wrefresh(tabline);
1119 if (in_minibuffer)
1120 wrefresh(echoarea);
1121 else
1122 wrefresh(body);
1125 void
1126 ui_on_tab_refresh(struct tab *tab)
1128 wrap_page(&tab->buffer, body_cols);
1129 if (tab->flags & TAB_CURRENT)
1130 redraw_tab(tab);
1131 else
1132 tab->flags |= TAB_URGENT;
1135 const char *
1136 ui_keyname(int k)
1138 return keyname(k);
1141 void
1142 ui_toggle_side_window(void)
1144 side_window = !side_window;
1145 if (side_window)
1146 recompute_help();
1149 * ugly hack, but otherwise the window doesn't get updated
1150 * until I call handle_resize a second time (i.e. C-l). I
1151 * will be happy to know why something like this is needed.
1153 handle_resize_nodelay(0, 0, NULL);
1154 handle_resize_nodelay(0, 0, NULL);
1157 void
1158 ui_schedule_redraw(void)
1160 handle_resize_nodelay(0, 0, NULL);
1163 void
1164 ui_require_input(struct tab *tab, int hide)
1166 /* TODO: hard-switching to another tab is ugly */
1167 switch_to_tab(tab);
1169 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1170 &ir_history);
1171 strlcpy(ministate.prompt, "Input required: ",
1172 sizeof(ministate.prompt));
1173 redraw_tab(tab);
1176 void
1177 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1178 struct tab *data)
1180 yornp(prompt, fn, data);
1181 redraw_tab(current_tab());
1184 void
1185 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1186 unsigned int data)
1188 completing_read(prompt, fn, data);
1189 redraw_tab(current_tab());
1192 void
1193 ui_end(void)
1195 endwin();