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 <assert.h>
34 #include <curses.h>
35 #include <event.h>
36 #include <locale.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "minibuffer.h"
44 #include "telescope.h"
45 #include "ui.h"
46 #include "utf8.h"
48 static struct event stdioev, winchev;
50 static void restore_curs_x(struct buffer *);
52 static struct vline *nth_line(struct buffer*, size_t);
53 static int readkey(void);
54 static void dispatch_stdio(int, short, void*);
55 static void handle_clear_echoarea(int, short, void*);
56 static void handle_resize(int, short, void*);
57 static void handle_resize_nodelay(int, short, void*);
58 static int wrap_page(struct buffer*, int);
59 static void print_vline(int, int, WINDOW*, struct vline*);
60 static void redraw_tabline(void);
61 static void redraw_window(WINDOW*, int, int, struct buffer*);
62 static void redraw_help(void);
63 static void redraw_body(struct tab*);
64 static void redraw_modeline(struct tab*);
65 static void redraw_echoarea(void);
66 static void redraw_tab(struct tab*);
67 static void emit_help_item(char*, void*);
68 static void rec_compute_help(struct kmap*, char*, size_t);
69 static void recompute_help(void);
70 static void update_loading_anim(int, short, void*);
71 static void stop_loading_anim(struct tab*);
73 static int x_offset;
75 struct thiskey thiskey;
77 static struct event resizeev;
78 static struct timeval resize_timer = { 0, 250000 };
80 static WINDOW *tabline, *body, *modeline, *echoarea;
82 int body_lines, body_cols;
84 static WINDOW *help;
85 static struct buffer helpwin;
86 static int help_lines, help_cols;
88 static int side_window;
90 static struct event clechoev;
91 static struct timeval clechoev_timer = { 5, 0 };
92 static struct timeval loadingev_timer = { 0, 250000 };
94 static uint32_t tab_counter;
96 static char keybuf[64];
98 struct kmap global_map,
99 minibuffer_map,
100 *current_map,
101 *base_map;
103 int in_minibuffer;
105 static inline void
106 update_x_offset(void)
108 if (olivetti_mode && fill_column < body_cols)
109 x_offset = (body_cols - fill_column)/2;
110 else
111 x_offset = 0;
114 void
115 save_excursion(struct excursion *place, struct buffer *buffer)
117 place->curs_x = buffer->curs_x;
118 place->curs_y = buffer->curs_y;
119 place->line_off = buffer->line_off;
120 place->current_line = buffer->current_line;
121 place->cpoff = buffer->cpoff;
124 void
125 restore_excursion(struct excursion *place, struct buffer *buffer)
127 buffer->curs_x = place->curs_x;
128 buffer->curs_y = place->curs_y;
129 buffer->line_off = place->line_off;
130 buffer->current_line = place->current_line;
131 buffer->cpoff = place->cpoff;
134 static void
135 restore_curs_x(struct buffer *buffer)
137 struct vline *vl;
138 const char *prfx;
140 vl = buffer->current_line;
141 if (vl == NULL || vl->line == NULL)
142 buffer->curs_x = buffer->cpoff = 0;
143 else
144 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
146 buffer->curs_x += x_offset;
148 if (vl != NULL) {
149 prfx = line_prefixes[vl->parent->type].prfx1;
150 buffer->curs_x += utf8_swidth(prfx);
154 void
155 global_key_unbound(void)
157 message("%s is undefined", keybuf);
160 static struct vline *
161 nth_line(struct buffer *buffer, size_t n)
163 struct vline *vl;
164 size_t i;
166 i = 0;
167 TAILQ_FOREACH(vl, &buffer->head, vlines) {
168 if (i == n)
169 return vl;
170 i++;
173 /* unreachable */
174 abort();
177 struct tab *
178 current_tab(void)
180 struct tab *t;
182 TAILQ_FOREACH(t, &tabshead, tabs) {
183 if (t->flags & TAB_CURRENT)
184 return t;
187 /* unreachable */
188 abort();
191 struct buffer *
192 current_buffer(void)
194 if (in_minibuffer)
195 return &ministate.buffer;
196 return &current_tab()->buffer;
199 static int
200 readkey(void)
202 uint32_t state = 0;
204 if ((thiskey.key = wgetch(body)) == ERR)
205 return 0;
207 thiskey.meta = thiskey.key == 27;
208 if (thiskey.meta) {
209 thiskey.key = wgetch(body);
210 if (thiskey.key == ERR || thiskey.key == 27) {
211 thiskey.meta = 0;
212 thiskey.key = 27;
216 thiskey.cp = 0;
217 if ((unsigned int)thiskey.key < UINT8_MAX) {
218 while (1) {
219 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
220 break;
221 if ((thiskey.key = wgetch(body)) == ERR) {
222 message("Error decoding user input");
223 return 0;
228 return 1;
231 static void
232 dispatch_stdio(int fd, short ev, void *d)
234 struct keymap *k;
235 const char *keyname;
236 char tmp[5] = {0};
238 if (!readkey())
239 return;
241 if (keybuf[0] != '\0')
242 strlcat(keybuf, " ", sizeof(keybuf));
243 if (thiskey.meta)
244 strlcat(keybuf, "M-", sizeof(keybuf));
245 if (thiskey.cp != 0) {
246 utf8_encode(thiskey.cp, tmp);
247 strlcat(keybuf, tmp, sizeof(keybuf));
248 } else {
249 if ((keyname = unkbd(thiskey.key)) != NULL)
250 strlcat(keybuf, keyname, sizeof(keybuf));
251 else {
252 tmp[0] = thiskey.key;
253 strlcat(keybuf, tmp, sizeof(keybuf));
257 TAILQ_FOREACH(k, &current_map->m, keymaps) {
258 if (k->meta == thiskey.meta &&
259 k->key == thiskey.key) {
260 if (k->fn == NULL)
261 current_map = &k->map;
262 else {
263 current_map = base_map;
264 strlcpy(keybuf, "", sizeof(keybuf));
265 k->fn(current_buffer());
267 goto done;
271 if (current_map->unhandled_input != NULL)
272 current_map->unhandled_input();
273 else
274 global_key_unbound();
276 strlcpy(keybuf, "", sizeof(keybuf));
277 current_map = base_map;
279 done:
280 if (side_window)
281 recompute_help();
283 redraw_tab(current_tab());
286 static void
287 handle_clear_echoarea(int fd, short ev, void *d)
289 free(ministate.curmesg);
290 ministate.curmesg = NULL;
292 redraw_echoarea();
293 if (in_minibuffer) {
294 wrefresh(body);
295 wrefresh(echoarea);
296 } else {
297 wrefresh(echoarea);
298 wrefresh(body);
302 static void
303 handle_resize(int sig, short ev, void *d)
305 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
306 event_del(&resizeev);
308 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
309 evtimer_add(&resizeev, &resize_timer);
312 static void
313 handle_resize_nodelay(int s, short ev, void *d)
315 struct tab *tab;
317 endwin();
318 refresh();
319 clear();
321 /* move and resize the windows, in reverse order! */
323 mvwin(echoarea, LINES-1, 0);
324 wresize(echoarea, 1, COLS);
326 mvwin(modeline, LINES-2, 0);
327 wresize(modeline, 1, COLS);
329 body_lines = LINES-3;
330 body_cols = COLS;
332 if (side_window) {
333 help_cols = 0.3 * COLS;
334 help_lines = LINES-3;
335 mvwin(help, 1, 0);
336 wresize(help, help_lines, help_cols);
338 wrap_page(&helpwin, help_cols);
340 body_cols = COLS - help_cols - 1;
341 mvwin(body, 1, help_cols);
342 } else
343 mvwin(body, 1, 0);
345 update_x_offset();
346 wresize(body, body_lines, body_cols);
348 wresize(tabline, 1, COLS);
350 tab = current_tab();
352 wrap_page(&tab->buffer, body_cols);
353 redraw_tab(tab);
356 static int
357 wrap_page(struct buffer *buffer, int width)
359 struct line *l;
360 const struct line *top_orig, *orig;
361 struct vline *vl;
362 int pre_width;
363 const char *prfx;
365 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
366 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
368 buffer->top_line = NULL;
369 buffer->current_line = NULL;
371 buffer->force_redraw = 1;
372 buffer->curs_y = 0;
373 buffer->line_off = 0;
375 empty_vlist(buffer);
377 TAILQ_FOREACH(l, &buffer->page.head, lines) {
378 prfx = line_prefixes[l->type].prfx1;
379 switch (l->type) {
380 case LINE_TEXT:
381 case LINE_LINK:
382 case LINE_TITLE_1:
383 case LINE_TITLE_2:
384 case LINE_TITLE_3:
385 case LINE_ITEM:
386 case LINE_QUOTE:
387 case LINE_PRE_START:
388 case LINE_PRE_END:
389 wrap_text(buffer, prfx, l, MIN(fill_column, width));
390 break;
391 case LINE_PRE_CONTENT:
392 if (olivetti_mode)
393 pre_width = MIN(fill_column, width);
394 else
395 pre_width = width;
396 hardwrap_text(buffer, l, pre_width);
397 break;
400 if (top_orig == l && buffer->top_line == NULL) {
401 buffer->line_off = buffer->line_max-1;
402 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
404 while (1) {
405 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
406 if (vl == NULL || vl->parent != orig)
407 break;
408 buffer->top_line = vl;
409 buffer->line_off--;
413 if (orig == l && buffer->current_line == NULL) {
414 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
416 while (1) {
417 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
418 if (vl == NULL || vl->parent != orig)
419 break;
420 buffer->current_line = vl;
425 if (buffer->current_line == NULL)
426 buffer->current_line = TAILQ_FIRST(&buffer->head);
428 if (buffer->top_line == NULL)
429 buffer->top_line = buffer->current_line;
431 return 1;
434 static void
435 print_vline(int off, int width, WINDOW *window, struct vline *vl)
437 const char *text;
438 const char *prfx;
439 struct line_face *f;
440 int i, left, x, y;
442 f = &line_faces[vl->parent->type];
444 /* unused, set by getyx */
445 (void)y;
447 if (!vl->flags)
448 prfx = line_prefixes[vl->parent->type].prfx1;
449 else
450 prfx = line_prefixes[vl->parent->type].prfx2;
452 text = vl->line;
453 if (text == NULL)
454 text = "";
456 wattr_on(window, body_face.left, NULL);
457 for (i = 0; i < off; i++)
458 waddch(window, ' ');
459 wattr_off(window, body_face.left, NULL);
461 wattr_on(window, f->prefix, NULL);
462 wprintw(window, "%s", prfx);
463 wattr_off(window, f->prefix, NULL);
465 wattr_on(window, f->text, NULL);
466 wprintw(window, "%s", text);
467 wattr_off(window, f->text, NULL);
469 getyx(window, y, x);
471 left = width - x;
473 wattr_on(window, f->trail, NULL);
474 for (i = 0; i < left - off; ++i)
475 waddch(window, ' ');
476 wattr_off(window, f->trail, NULL);
478 wattr_on(window, body_face.right, NULL);
479 for (i = 0; i < off; i++)
480 waddch(window, ' ');
481 wattr_off(window, body_face.right, NULL);
485 static void
486 redraw_tabline(void)
488 struct tab *tab;
489 size_t toskip, ots, tabwidth, space, x;
490 int current, y, truncated;
491 const char *title;
492 char buf[25];
494 x = 0;
496 /* unused, but setted by a getyx */
497 (void)y;
499 tabwidth = sizeof(buf)+1;
500 space = COLS-2;
502 toskip = 0;
503 TAILQ_FOREACH(tab, &tabshead, tabs) {
504 toskip++;
505 if (tab->flags & TAB_CURRENT)
506 break;
509 if (toskip * tabwidth < space)
510 toskip = 0;
511 else {
512 ots = toskip;
513 toskip--;
514 while (toskip != 0 &&
515 (ots - toskip+1) * tabwidth < space)
516 toskip--;
519 werase(tabline);
520 wattr_on(tabline, tab_face.background, NULL);
521 wprintw(tabline, toskip == 0 ? " " : "<");
522 wattr_off(tabline, tab_face.background, NULL);
524 truncated = 0;
525 TAILQ_FOREACH(tab, &tabshead, tabs) {
526 if (truncated)
527 break;
528 if (toskip != 0) {
529 toskip--;
530 continue;
533 getyx(tabline, y, x);
534 if (x + sizeof(buf)+2 >= (size_t)COLS)
535 truncated = 1;
537 current = tab->flags & TAB_CURRENT;
539 if (*(title = tab->buffer.page.title) == '\0')
540 title = tab->hist_cur->h;
542 if (tab->flags & TAB_URGENT)
543 strlcpy(buf, "!", sizeof(buf));
544 else
545 strlcpy(buf, " ", sizeof(buf));
547 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
548 /* truncation happens */
549 strlcpy(&buf[sizeof(buf)-4], "...", 4);
550 } else {
551 /* pad with spaces */
552 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
553 /* nop */ ;
556 if (current)
557 wattr_on(tabline, tab_face.current, NULL);
558 else
559 wattr_on(tabline, tab_face.tab, NULL);
561 wprintw(tabline, "%s", buf);
562 if (TAILQ_NEXT(tab, tabs) != NULL)
563 wprintw(tabline, " ");
565 if (current)
566 wattr_off(tabline, tab_face.current, NULL);
567 else
568 wattr_off(tabline, tab_face.tab, NULL);
571 wattr_on(tabline, tab_face.background, NULL);
572 for (; x < (size_t)COLS; ++x)
573 waddch(tabline, ' ');
574 if (truncated)
575 mvwprintw(tabline, 0, COLS-1, ">");
576 wattr_off(tabline, tab_face.background, NULL);
579 /*
580 * Compute the first visible line around vl. Try to search forward
581 * until the end of the buffer; if a visible line is not found, search
582 * backward. Return NULL if no viable line was found.
583 */
584 static inline struct vline *
585 adjust_line(struct vline *vl, struct buffer *buffer)
587 struct vline *t;
589 if (!(vl->parent->flags & L_HIDDEN))
590 return vl;
592 /* search forward */
593 for (t = vl;
594 t != NULL && t->parent->flags & L_HIDDEN;
595 t = TAILQ_NEXT(t, vlines))
596 ; /* nop */
598 if (t != NULL)
599 return t;
601 /* search backward */
602 for (t = vl;
603 t != NULL && t->parent->flags & L_HIDDEN;
604 t = TAILQ_PREV(t, vhead, vlines))
605 ; /* nop */
607 return t;
610 static void
611 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
613 struct vline *vl;
614 int l, onscreen;
616 restore_curs_x(buffer);
618 /*
619 * TODO: ignoring buffer->force_update and always
620 * re-rendering. In theory we can recompute the y position
621 * without a re-render, and optimize here. It's not the only
622 * optimisation possible here, wscrl wolud also be an
623 * interesting one.
624 */
626 again:
627 werase(win);
628 buffer->curs_y = 0;
630 if (TAILQ_EMPTY(&buffer->head))
631 goto end;
633 buffer->top_line = adjust_line(buffer->top_line, buffer);
634 if (buffer->top_line == NULL)
635 goto end;
637 buffer->current_line = adjust_line(buffer->current_line, buffer);
639 l = 0;
640 onscreen = 0;
641 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
642 if (vl->parent->flags & L_HIDDEN)
643 continue;
645 wmove(win, l, 0);
646 print_vline(x_offset, width, win, vl);
648 if (vl == buffer->current_line)
649 onscreen = 1;
651 if (!onscreen)
652 buffer->curs_y++;
654 l++;
655 if (l == height)
656 break;
659 if (!onscreen) {
660 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
661 if (vl == buffer->current_line)
662 break;
663 if (vl->parent->flags & L_HIDDEN)
664 continue;
665 buffer->line_off++;
666 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
669 goto again;
672 buffer->last_line_off = buffer->line_off;
673 buffer->force_redraw = 0;
674 end:
675 wmove(win, buffer->curs_y, buffer->curs_x);
678 static void
679 redraw_help(void)
681 redraw_window(help, help_lines, help_cols, &helpwin);
684 static void
685 redraw_body(struct tab *tab)
687 static struct tab *last_tab;
689 if (last_tab != tab)
690 tab->buffer.force_redraw =1;
691 last_tab = tab;
693 redraw_window(body, body_lines, body_cols, &tab->buffer);
696 static inline char
697 trust_status_char(enum trust_state ts)
699 switch (ts) {
700 case TS_UNKNOWN: return 'u';
701 case TS_UNTRUSTED: return '!';
702 case TS_TEMP_TRUSTED: return '!';
703 case TS_TRUSTED: return 'v';
704 case TS_VERIFIED: return 'V';
705 default: return 'X';
709 static void
710 redraw_modeline(struct tab *tab)
712 double pct;
713 int x, y, max_x, max_y;
714 const char *mode = tab->buffer.page.name;
715 const char *spin = "-\\|/";
717 werase(modeline);
718 wattr_on(modeline, modeline_face.background, NULL);
719 wmove(modeline, 0, 0);
721 wprintw(modeline, "-%c%c %s ",
722 spin[tab->loading_anim_step],
723 trust_status_char(tab->trust),
724 mode == NULL ? "(none)" : mode);
726 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
728 if (tab->buffer.line_max <= (size_t)body_lines)
729 wprintw(modeline, "All ");
730 else if (tab->buffer.line_off == 0)
731 wprintw(modeline, "Top ");
732 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
733 wprintw(modeline, "Bottom ");
734 else
735 wprintw(modeline, "%.0f%% ", pct);
737 wprintw(modeline, "%d/%d %s ",
738 tab->buffer.line_off + tab->buffer.curs_y,
739 tab->buffer.line_max,
740 tab->hist_cur->h);
742 getyx(modeline, y, x);
743 getmaxyx(modeline, max_y, max_x);
745 (void)y;
746 (void)max_y;
748 for (; x < max_x; ++x)
749 waddstr(modeline, "-");
751 wattr_off(modeline, modeline_face.background, NULL);
754 static void
755 redraw_echoarea(void)
757 struct tab *tab;
758 size_t off_y, off_x = 0;
759 char *start = NULL, *c = NULL;
761 /* unused, but set by getyx */
762 (void)off_y;
764 wattr_on(echoarea, minibuffer_face.background, NULL);
765 werase(echoarea);
767 if (in_minibuffer) {
768 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
769 if (ministate.hist_cur != NULL)
770 wprintw(echoarea, "(%zu/%zu) ",
771 ministate.hist_off + 1,
772 ministate.history->len);
774 getyx(echoarea, off_y, off_x);
776 start = ministate.hist_cur != NULL
777 ? ministate.hist_cur->h
778 : ministate.buf;
779 c = utf8_nth(ministate.buffer.current_line->line,
780 ministate.buffer.cpoff);
781 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
782 start = utf8_next_cp(start);
785 waddstr(echoarea, start);
788 if (ministate.curmesg != NULL)
789 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
790 ministate.curmesg);
792 if (!in_minibuffer && ministate.curmesg == NULL)
793 waddstr(echoarea, keybuf);
795 /* If nothing else, show the URL at point */
796 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
797 tab = current_tab();
798 if (tab->buffer.current_line != NULL &&
799 tab->buffer.current_line->parent->type == LINE_LINK)
800 waddstr(echoarea, tab->buffer.current_line->parent->alt);
803 if (in_minibuffer)
804 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
806 wattr_off(echoarea, minibuffer_face.background, NULL);
809 static void
810 redraw_tab(struct tab *tab)
812 if (side_window) {
813 redraw_help();
814 wnoutrefresh(help);
817 redraw_tabline();
818 redraw_body(tab);
819 redraw_modeline(tab);
820 redraw_echoarea();
822 wnoutrefresh(tabline);
823 wnoutrefresh(modeline);
825 if (in_minibuffer) {
826 wnoutrefresh(body);
827 wnoutrefresh(echoarea);
828 } else {
829 wnoutrefresh(echoarea);
830 wnoutrefresh(body);
833 doupdate();
836 static void
837 emit_help_item(char *prfx, void *fn)
839 struct line *l;
840 struct cmd *cmd;
842 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
843 if (fn == cmd->fn)
844 break;
846 assert(cmd != NULL);
848 if ((l = calloc(1, sizeof(*l))) == NULL)
849 abort();
851 l->type = LINE_TEXT;
852 l->alt = NULL;
854 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
856 if (TAILQ_EMPTY(&helpwin.page.head))
857 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
858 else
859 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
862 static void
863 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
865 struct keymap *k;
866 char p[32];
867 const char *kn;
869 TAILQ_FOREACH(k, &keymap->m, keymaps) {
870 strlcpy(p, prfx, sizeof(p));
871 if (*p != '\0')
872 strlcat(p, " ", sizeof(p));
873 if (k->meta)
874 strlcat(p, "M-", sizeof(p));
875 if ((kn = unkbd(k->key)) != NULL)
876 strlcat(p, kn, sizeof(p));
877 else
878 strlcat(p, keyname(k->key), sizeof(p));
880 if (k->fn == NULL)
881 rec_compute_help(&k->map, p, sizeof(p));
882 else
883 emit_help_item(p, k->fn);
887 static void
888 recompute_help(void)
890 char p[32] = { 0 };
892 empty_vlist(&helpwin);
893 empty_linelist(&helpwin);
894 rec_compute_help(current_map, p, sizeof(p));
895 wrap_page(&helpwin, help_cols);
898 void
899 vmessage(const char *fmt, va_list ap)
901 if (evtimer_pending(&clechoev, NULL))
902 evtimer_del(&clechoev);
904 free(ministate.curmesg);
905 ministate.curmesg = NULL;
907 if (fmt != NULL) {
908 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
909 evtimer_add(&clechoev, &clechoev_timer);
911 /* TODO: what to do if the allocation fails here? */
912 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
913 ministate.curmesg = NULL;
916 redraw_echoarea();
917 if (in_minibuffer) {
918 wrefresh(body);
919 wrefresh(echoarea);
920 } else {
921 wrefresh(echoarea);
922 wrefresh(body);
926 void
927 message(const char *fmt, ...)
929 va_list ap;
931 va_start(ap, fmt);
932 vmessage(fmt, ap);
933 va_end(ap);
936 void
937 start_loading_anim(struct tab *tab)
939 if (tab->loading_anim)
940 return;
941 tab->loading_anim = 1;
942 evtimer_set(&tab->loadingev, update_loading_anim, tab);
943 evtimer_add(&tab->loadingev, &loadingev_timer);
946 static void
947 update_loading_anim(int fd, short ev, void *d)
949 struct tab *tab = d;
951 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
953 if (tab->flags & TAB_CURRENT) {
954 redraw_modeline(tab);
955 wrefresh(modeline);
956 wrefresh(body);
957 if (in_minibuffer)
958 wrefresh(echoarea);
961 evtimer_add(&tab->loadingev, &loadingev_timer);
964 static void
965 stop_loading_anim(struct tab *tab)
967 if (!tab->loading_anim)
968 return;
969 evtimer_del(&tab->loadingev);
970 tab->loading_anim = 0;
971 tab->loading_anim_step = 0;
973 if (!(tab->flags & TAB_CURRENT))
974 return;
976 redraw_modeline(tab);
978 wrefresh(modeline);
979 wrefresh(body);
980 if (in_minibuffer)
981 wrefresh(echoarea);
984 void
985 load_url_in_tab(struct tab *tab, const char *url)
987 message("Loading %s...", url);
988 start_loading_anim(tab);
989 load_url(tab, url);
991 tab->buffer.curs_x = 0;
992 tab->buffer.curs_y = 0;
993 redraw_tab(tab);
996 void
997 switch_to_tab(struct tab *tab)
999 struct tab *t;
1001 TAILQ_FOREACH(t, &tabshead, tabs) {
1002 t->flags &= ~TAB_CURRENT;
1005 tab->flags |= TAB_CURRENT;
1006 tab->flags &= ~TAB_URGENT;
1009 unsigned int
1010 tab_new_id(void)
1012 return tab_counter++;
1015 struct tab *
1016 new_tab(const char *url)
1018 struct tab *tab;
1020 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1021 event_loopbreak();
1022 return NULL;
1024 tab->fd = -1;
1026 TAILQ_INIT(&tab->hist.head);
1028 TAILQ_INIT(&tab->buffer.head);
1030 tab->id = tab_new_id();
1031 switch_to_tab(tab);
1033 if (TAILQ_EMPTY(&tabshead))
1034 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1035 else
1036 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1038 load_url_in_tab(tab, url);
1039 return tab;
1042 int
1043 ui_init()
1045 setlocale(LC_ALL, "");
1047 TAILQ_INIT(&eecmd_history.head);
1048 TAILQ_INIT(&ir_history.head);
1049 TAILQ_INIT(&lu_history.head);
1051 ministate.line.type = LINE_TEXT;
1052 ministate.vline.parent = &ministate.line;
1053 ministate.buffer.current_line = &ministate.vline;
1055 /* initialize help window */
1056 TAILQ_INIT(&helpwin.head);
1058 base_map = &global_map;
1059 current_map = &global_map;
1061 initscr();
1063 if (enable_colors) {
1064 if (has_colors()) {
1065 start_color();
1066 use_default_colors();
1067 } else
1068 enable_colors = 0;
1071 config_apply_style();
1073 raw();
1074 noecho();
1075 nonl();
1076 intrflush(stdscr, FALSE);
1078 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1079 return 0;
1080 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1081 return 0;
1082 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1083 return 0;
1084 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1085 return 0;
1086 if ((help = newwin(1, 1, 1, 0)) == NULL)
1087 return 0;
1089 body_lines = LINES-3;
1090 body_cols = COLS;
1092 wbkgd(body, body_face.body);
1093 wbkgd(echoarea, minibuffer_face.background);
1095 update_x_offset();
1097 keypad(body, TRUE);
1098 scrollok(body, FALSE);
1100 /* non-blocking input */
1101 wtimeout(body, 0);
1103 mvwprintw(body, 0, 0, "");
1105 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1106 event_add(&stdioev, NULL);
1108 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1109 signal_add(&winchev, NULL);
1111 return 1;
1114 void
1115 ui_on_tab_loaded(struct tab *tab)
1117 stop_loading_anim(tab);
1118 message("Loaded %s", tab->hist_cur->h);
1120 redraw_tabline();
1121 wrefresh(tabline);
1122 if (in_minibuffer)
1123 wrefresh(echoarea);
1124 else
1125 wrefresh(body);
1128 void
1129 ui_on_tab_refresh(struct tab *tab)
1131 wrap_page(&tab->buffer, body_cols);
1132 if (tab->flags & TAB_CURRENT)
1133 redraw_tab(tab);
1134 else
1135 tab->flags |= TAB_URGENT;
1138 const char *
1139 ui_keyname(int k)
1141 return keyname(k);
1144 void
1145 ui_toggle_side_window(void)
1147 side_window = !side_window;
1148 if (side_window)
1149 recompute_help();
1152 * ugly hack, but otherwise the window doesn't get updated
1153 * until I call handle_resize a second time (i.e. C-l). I
1154 * will be happy to know why something like this is needed.
1156 handle_resize_nodelay(0, 0, NULL);
1157 handle_resize_nodelay(0, 0, NULL);
1160 void
1161 ui_schedule_redraw(void)
1163 handle_resize_nodelay(0, 0, NULL);
1166 void
1167 ui_require_input(struct tab *tab, int hide)
1169 /* TODO: hard-switching to another tab is ugly */
1170 switch_to_tab(tab);
1172 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1173 &ir_history);
1174 strlcpy(ministate.prompt, "Input required: ",
1175 sizeof(ministate.prompt));
1176 redraw_tab(tab);
1179 void
1180 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1181 struct tab *data)
1183 yornp(prompt, fn, data);
1184 redraw_tab(current_tab());
1187 void
1188 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1189 unsigned int data)
1191 completing_read(prompt, fn, data);
1192 redraw_tab(current_tab());
1195 void
1196 ui_end(void)
1198 endwin();