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_minibuffer(void);
66 static void do_redraw_echoarea(void);
67 static void do_redraw_minibuffer(void);
68 static void redraw_tab(struct tab*);
69 static void emit_help_item(char*, void*);
70 static void rec_compute_help(struct kmap*, char*, size_t);
71 static void recompute_help(void);
72 static void update_loading_anim(int, short, void*);
73 static void stop_loading_anim(struct tab*);
75 static int x_offset;
77 struct thiskey thiskey;
79 static struct event resizeev;
80 static struct timeval resize_timer = { 0, 250000 };
82 static WINDOW *tabline, *body, *modeline, *echoarea;
84 int body_lines, body_cols;
86 static WINDOW *help;
87 static struct buffer helpwin;
88 static int help_lines, help_cols;
90 static int side_window;
92 static struct event clechoev;
93 static struct timeval clechoev_timer = { 5, 0 };
94 static struct timeval loadingev_timer = { 0, 250000 };
96 static uint32_t tab_counter;
98 static char keybuf[64];
100 struct kmap global_map,
101 minibuffer_map,
102 *current_map,
103 *base_map;
105 int in_minibuffer;
107 static inline void
108 update_x_offset(void)
110 if (olivetti_mode && fill_column < body_cols)
111 x_offset = (body_cols - fill_column)/2;
112 else
113 x_offset = 0;
116 void
117 save_excursion(struct excursion *place, struct buffer *buffer)
119 place->curs_x = buffer->curs_x;
120 place->curs_y = buffer->curs_y;
121 place->line_off = buffer->line_off;
122 place->current_line = buffer->current_line;
123 place->cpoff = buffer->cpoff;
126 void
127 restore_excursion(struct excursion *place, struct buffer *buffer)
129 buffer->curs_x = place->curs_x;
130 buffer->curs_y = place->curs_y;
131 buffer->line_off = place->line_off;
132 buffer->current_line = place->current_line;
133 buffer->cpoff = place->cpoff;
136 static void
137 restore_curs_x(struct buffer *buffer)
139 struct vline *vl;
140 const char *prfx;
142 vl = buffer->current_line;
143 if (vl == NULL || vl->line == NULL)
144 buffer->curs_x = buffer->cpoff = 0;
145 else
146 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
148 buffer->curs_x += x_offset;
150 if (vl != NULL) {
151 prfx = line_prefixes[vl->parent->type].prfx1;
152 buffer->curs_x += utf8_swidth(prfx);
156 void
157 global_key_unbound(void)
159 message("%s is undefined", keybuf);
162 static struct vline *
163 nth_line(struct buffer *buffer, size_t n)
165 struct vline *vl;
166 size_t i;
168 i = 0;
169 TAILQ_FOREACH(vl, &buffer->head, vlines) {
170 if (i == n)
171 return vl;
172 i++;
175 /* unreachable */
176 abort();
179 struct tab *
180 current_tab(void)
182 struct tab *t;
184 TAILQ_FOREACH(t, &tabshead, tabs) {
185 if (t->flags & TAB_CURRENT)
186 return t;
189 /* unreachable */
190 abort();
193 struct buffer *
194 current_buffer(void)
196 if (in_minibuffer)
197 return &ministate.buffer;
198 return &current_tab()->buffer;
201 static int
202 readkey(void)
204 uint32_t state = 0;
206 if ((thiskey.key = wgetch(body)) == ERR)
207 return 0;
209 thiskey.meta = thiskey.key == 27;
210 if (thiskey.meta) {
211 thiskey.key = wgetch(body);
212 if (thiskey.key == ERR || thiskey.key == 27) {
213 thiskey.meta = 0;
214 thiskey.key = 27;
218 thiskey.cp = 0;
219 if ((unsigned int)thiskey.key < UINT8_MAX) {
220 while (1) {
221 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
222 break;
223 if ((thiskey.key = wgetch(body)) == ERR) {
224 message("Error decoding user input");
225 return 0;
230 return 1;
233 static void
234 dispatch_stdio(int fd, short ev, void *d)
236 struct keymap *k;
237 const char *keyname;
238 char tmp[5] = {0};
240 if (!readkey())
241 return;
243 if (keybuf[0] != '\0')
244 strlcat(keybuf, " ", sizeof(keybuf));
245 if (thiskey.meta)
246 strlcat(keybuf, "M-", sizeof(keybuf));
247 if (thiskey.cp != 0) {
248 utf8_encode(thiskey.cp, tmp);
249 strlcat(keybuf, tmp, sizeof(keybuf));
250 } else {
251 if ((keyname = unkbd(thiskey.key)) != NULL)
252 strlcat(keybuf, keyname, sizeof(keybuf));
253 else {
254 tmp[0] = thiskey.key;
255 strlcat(keybuf, tmp, sizeof(keybuf));
259 TAILQ_FOREACH(k, &current_map->m, keymaps) {
260 if (k->meta == thiskey.meta &&
261 k->key == thiskey.key) {
262 if (k->fn == NULL)
263 current_map = &k->map;
264 else {
265 current_map = base_map;
266 strlcpy(keybuf, "", sizeof(keybuf));
267 k->fn(current_buffer());
269 goto done;
273 if (current_map->unhandled_input != NULL)
274 current_map->unhandled_input();
275 else
276 global_key_unbound();
278 strlcpy(keybuf, "", sizeof(keybuf));
279 current_map = base_map;
281 done:
282 if (side_window)
283 recompute_help();
285 redraw_tab(current_tab());
288 static void
289 handle_clear_echoarea(int fd, short ev, void *d)
291 free(ministate.curmesg);
292 ministate.curmesg = NULL;
294 redraw_minibuffer();
295 if (in_minibuffer) {
296 wrefresh(body);
297 wrefresh(echoarea);
298 } else {
299 wrefresh(echoarea);
300 wrefresh(body);
304 static void
305 handle_resize(int sig, short ev, void *d)
307 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
308 event_del(&resizeev);
310 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
311 evtimer_add(&resizeev, &resize_timer);
314 static void
315 handle_resize_nodelay(int s, short ev, void *d)
317 struct tab *tab;
319 endwin();
320 refresh();
321 clear();
323 /* move and resize the windows, in reverse order! */
325 mvwin(echoarea, LINES-1, 0);
326 wresize(echoarea, 1, COLS);
328 mvwin(modeline, LINES-2, 0);
329 wresize(modeline, 1, COLS);
331 body_lines = LINES-3;
332 body_cols = COLS;
334 if (side_window) {
335 help_cols = 0.3 * COLS;
336 help_lines = LINES-3;
337 mvwin(help, 1, 0);
338 wresize(help, help_lines, help_cols);
340 wrap_page(&helpwin, help_cols);
342 body_cols = COLS - help_cols - 1;
343 mvwin(body, 1, help_cols);
344 } else
345 mvwin(body, 1, 0);
347 update_x_offset();
348 wresize(body, body_lines, body_cols);
350 wresize(tabline, 1, COLS);
352 tab = current_tab();
354 wrap_page(&tab->buffer, body_cols);
355 redraw_tab(tab);
358 static int
359 wrap_page(struct buffer *buffer, int width)
361 struct line *l;
362 const struct line *top_orig, *orig;
363 struct vline *vl;
364 int pre_width;
365 const char *prfx;
367 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
368 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
370 buffer->top_line = NULL;
371 buffer->current_line = NULL;
373 buffer->force_redraw = 1;
374 buffer->curs_y = 0;
375 buffer->line_off = 0;
377 empty_vlist(buffer);
379 TAILQ_FOREACH(l, &buffer->page.head, lines) {
380 prfx = line_prefixes[l->type].prfx1;
381 switch (l->type) {
382 case LINE_TEXT:
383 case LINE_LINK:
384 case LINE_TITLE_1:
385 case LINE_TITLE_2:
386 case LINE_TITLE_3:
387 case LINE_ITEM:
388 case LINE_QUOTE:
389 case LINE_PRE_START:
390 case LINE_PRE_END:
391 wrap_text(buffer, prfx, l, MIN(fill_column, width));
392 break;
393 case LINE_PRE_CONTENT:
394 if (olivetti_mode)
395 pre_width = MIN(fill_column, width);
396 else
397 pre_width = width;
398 hardwrap_text(buffer, l, pre_width);
399 break;
402 if (top_orig == l && buffer->top_line == NULL) {
403 buffer->line_off = buffer->line_max-1;
404 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
406 while (1) {
407 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
408 if (vl == NULL || vl->parent != orig)
409 break;
410 buffer->top_line = vl;
411 buffer->line_off--;
415 if (orig == l && buffer->current_line == NULL) {
416 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
418 while (1) {
419 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
420 if (vl == NULL || vl->parent != orig)
421 break;
422 buffer->current_line = vl;
427 if (buffer->current_line == NULL)
428 buffer->current_line = TAILQ_FIRST(&buffer->head);
430 if (buffer->top_line == NULL)
431 buffer->top_line = buffer->current_line;
433 return 1;
436 static void
437 print_vline(int off, int width, WINDOW *window, struct vline *vl)
439 const char *text;
440 const char *prfx;
441 struct line_face *f;
442 int i, left, x, y;
444 f = &line_faces[vl->parent->type];
446 /* unused, set by getyx */
447 (void)y;
449 if (!vl->flags)
450 prfx = line_prefixes[vl->parent->type].prfx1;
451 else
452 prfx = line_prefixes[vl->parent->type].prfx2;
454 text = vl->line;
455 if (text == NULL)
456 text = "";
458 wattr_on(window, body_face.left, NULL);
459 for (i = 0; i < off; i++)
460 waddch(window, ' ');
461 wattr_off(window, body_face.left, NULL);
463 wattr_on(window, f->prefix, NULL);
464 wprintw(window, "%s", prfx);
465 wattr_off(window, f->prefix, NULL);
467 wattr_on(window, f->text, NULL);
468 wprintw(window, "%s", text);
469 wattr_off(window, f->text, NULL);
471 getyx(window, y, x);
473 left = width - x;
475 wattr_on(window, f->trail, NULL);
476 for (i = 0; i < left - off; ++i)
477 waddch(window, ' ');
478 wattr_off(window, f->trail, NULL);
480 wattr_on(window, body_face.right, NULL);
481 for (i = 0; i < off; i++)
482 waddch(window, ' ');
483 wattr_off(window, body_face.right, NULL);
487 static void
488 redraw_tabline(void)
490 struct tab *tab;
491 size_t toskip, ots, tabwidth, space, x;
492 int current, y, truncated;
493 const char *title;
494 char buf[25];
496 x = 0;
498 /* unused, but setted by a getyx */
499 (void)y;
501 tabwidth = sizeof(buf)+1;
502 space = COLS-2;
504 toskip = 0;
505 TAILQ_FOREACH(tab, &tabshead, tabs) {
506 toskip++;
507 if (tab->flags & TAB_CURRENT)
508 break;
511 if (toskip * tabwidth < space)
512 toskip = 0;
513 else {
514 ots = toskip;
515 toskip--;
516 while (toskip != 0 &&
517 (ots - toskip+1) * tabwidth < space)
518 toskip--;
521 werase(tabline);
522 wattr_on(tabline, tab_face.background, NULL);
523 wprintw(tabline, toskip == 0 ? " " : "<");
524 wattr_off(tabline, tab_face.background, NULL);
526 truncated = 0;
527 TAILQ_FOREACH(tab, &tabshead, tabs) {
528 if (truncated)
529 break;
530 if (toskip != 0) {
531 toskip--;
532 continue;
535 getyx(tabline, y, x);
536 if (x + sizeof(buf)+2 >= (size_t)COLS)
537 truncated = 1;
539 current = tab->flags & TAB_CURRENT;
541 if (*(title = tab->buffer.page.title) == '\0')
542 title = tab->hist_cur->h;
544 if (tab->flags & TAB_URGENT)
545 strlcpy(buf, "!", sizeof(buf));
546 else
547 strlcpy(buf, " ", sizeof(buf));
549 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
550 /* truncation happens */
551 strlcpy(&buf[sizeof(buf)-4], "...", 4);
552 } else {
553 /* pad with spaces */
554 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
555 /* nop */ ;
558 if (current)
559 wattr_on(tabline, tab_face.current, NULL);
560 else
561 wattr_on(tabline, tab_face.tab, NULL);
563 wprintw(tabline, "%s", buf);
564 if (TAILQ_NEXT(tab, tabs) != NULL)
565 wprintw(tabline, " ");
567 if (current)
568 wattr_off(tabline, tab_face.current, NULL);
569 else
570 wattr_off(tabline, tab_face.tab, NULL);
573 wattr_on(tabline, tab_face.background, NULL);
574 for (; x < (size_t)COLS; ++x)
575 waddch(tabline, ' ');
576 if (truncated)
577 mvwprintw(tabline, 0, COLS-1, ">");
578 wattr_off(tabline, tab_face.background, NULL);
581 /*
582 * Compute the first visible line around vl. Try to search forward
583 * until the end of the buffer; if a visible line is not found, search
584 * backward. Return NULL if no viable line was found.
585 */
586 static inline struct vline *
587 adjust_line(struct vline *vl, struct buffer *buffer)
589 struct vline *t;
591 if (!(vl->parent->flags & L_HIDDEN))
592 return vl;
594 /* search forward */
595 for (t = vl;
596 t != NULL && t->parent->flags & L_HIDDEN;
597 t = TAILQ_NEXT(t, vlines))
598 ; /* nop */
600 if (t != NULL)
601 return t;
603 /* search backward */
604 for (t = vl;
605 t != NULL && t->parent->flags & L_HIDDEN;
606 t = TAILQ_PREV(t, vhead, vlines))
607 ; /* nop */
609 return t;
612 static void
613 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
615 struct vline *vl;
616 int l, onscreen;
618 restore_curs_x(buffer);
620 /*
621 * TODO: ignoring buffer->force_update and always
622 * re-rendering. In theory we can recompute the y position
623 * without a re-render, and optimize here. It's not the only
624 * optimisation possible here, wscrl wolud also be an
625 * interesting one.
626 */
628 again:
629 werase(win);
630 buffer->curs_y = 0;
632 if (TAILQ_EMPTY(&buffer->head))
633 goto end;
635 buffer->top_line = adjust_line(buffer->top_line, buffer);
636 if (buffer->top_line == NULL)
637 goto end;
639 buffer->current_line = adjust_line(buffer->current_line, buffer);
641 l = 0;
642 onscreen = 0;
643 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
644 if (vl->parent->flags & L_HIDDEN)
645 continue;
647 wmove(win, l, 0);
648 print_vline(x_offset, width, win, vl);
650 if (vl == buffer->current_line)
651 onscreen = 1;
653 if (!onscreen)
654 buffer->curs_y++;
656 l++;
657 if (l == height)
658 break;
661 if (!onscreen) {
662 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
663 if (vl == buffer->current_line)
664 break;
665 if (vl->parent->flags & L_HIDDEN)
666 continue;
667 buffer->line_off++;
668 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
671 goto again;
674 buffer->last_line_off = buffer->line_off;
675 buffer->force_redraw = 0;
676 end:
677 wmove(win, buffer->curs_y, buffer->curs_x);
680 static void
681 redraw_help(void)
683 redraw_window(help, help_lines, help_cols, &helpwin);
686 static void
687 redraw_body(struct tab *tab)
689 static struct tab *last_tab;
691 if (last_tab != tab)
692 tab->buffer.force_redraw =1;
693 last_tab = tab;
695 redraw_window(body, body_lines, body_cols, &tab->buffer);
698 static inline char
699 trust_status_char(enum trust_state ts)
701 switch (ts) {
702 case TS_UNKNOWN: return 'u';
703 case TS_UNTRUSTED: return '!';
704 case TS_TEMP_TRUSTED: return '!';
705 case TS_TRUSTED: return 'v';
706 case TS_VERIFIED: return 'V';
707 default: return 'X';
711 static void
712 redraw_modeline(struct tab *tab)
714 double pct;
715 int x, y, max_x, max_y;
716 const char *mode = tab->buffer.page.name;
717 const char *spin = "-\\|/";
719 werase(modeline);
720 wattr_on(modeline, modeline_face.background, NULL);
721 wmove(modeline, 0, 0);
723 wprintw(modeline, "-%c%c %s ",
724 spin[tab->loading_anim_step],
725 trust_status_char(tab->trust),
726 mode == NULL ? "(none)" : mode);
728 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
730 if (tab->buffer.line_max <= (size_t)body_lines)
731 wprintw(modeline, "All ");
732 else if (tab->buffer.line_off == 0)
733 wprintw(modeline, "Top ");
734 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
735 wprintw(modeline, "Bottom ");
736 else
737 wprintw(modeline, "%.0f%% ", pct);
739 wprintw(modeline, "%d/%d %s ",
740 tab->buffer.line_off + tab->buffer.curs_y,
741 tab->buffer.line_max,
742 tab->hist_cur->h);
744 getyx(modeline, y, x);
745 getmaxyx(modeline, max_y, max_x);
747 (void)y;
748 (void)max_y;
750 for (; x < max_x; ++x)
751 waddstr(modeline, "-");
753 wattr_off(modeline, modeline_face.background, NULL);
756 static void
757 redraw_minibuffer(void)
759 wattr_on(echoarea, minibuffer_face.background, NULL);
760 werase(echoarea);
762 if (in_minibuffer)
763 do_redraw_minibuffer();
764 else
765 do_redraw_echoarea();
767 wattr_off(echoarea, minibuffer_face.background, NULL);
770 static void
771 do_redraw_echoarea(void)
773 struct tab *tab;
775 if (ministate.curmesg != NULL)
776 wprintw(echoarea, "%s", ministate.curmesg);
777 else if (*keybuf != '\0')
778 waddstr(echoarea, keybuf);
779 else {
780 /* If nothing else, show the URL at point */
781 tab = current_tab();
782 if (tab->buffer.current_line != NULL &&
783 tab->buffer.current_line->parent->type == LINE_LINK)
784 waddstr(echoarea, tab->buffer.current_line->parent->alt);
788 static void
789 do_redraw_minibuffer(void)
791 size_t off_y, off_x = 0;
792 const char *start, *c;
794 /* unused, set by getyx */
795 (void)off_y;
797 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
798 if (ministate.hist_cur != NULL)
799 wprintw(echoarea, "(%zu/%zu) ",
800 ministate.hist_off + 1,
801 ministate.history->len);
803 getyx(echoarea, off_y, off_x);
805 start = ministate.hist_cur != NULL
806 ? ministate.hist_cur->h
807 : ministate.buf;
808 c = utf8_nth(ministate.buffer.current_line->line,
809 ministate.buffer.cpoff);
810 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
811 start = utf8_next_cp(start);
814 waddstr(echoarea, start);
816 if (ministate.curmesg != NULL)
817 wprintw(echoarea, " [%s]", ministate.curmesg);
819 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
822 static void
823 redraw_tab(struct tab *tab)
825 if (side_window) {
826 redraw_help();
827 wnoutrefresh(help);
830 redraw_tabline();
831 redraw_body(tab);
832 redraw_modeline(tab);
833 redraw_minibuffer();
835 wnoutrefresh(tabline);
836 wnoutrefresh(modeline);
838 if (in_minibuffer) {
839 wnoutrefresh(body);
840 wnoutrefresh(echoarea);
841 } else {
842 wnoutrefresh(echoarea);
843 wnoutrefresh(body);
846 doupdate();
849 static void
850 emit_help_item(char *prfx, void *fn)
852 struct line *l;
853 struct cmd *cmd;
855 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
856 if (fn == cmd->fn)
857 break;
859 assert(cmd != NULL);
861 if ((l = calloc(1, sizeof(*l))) == NULL)
862 abort();
864 l->type = LINE_TEXT;
865 l->alt = NULL;
867 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
869 if (TAILQ_EMPTY(&helpwin.page.head))
870 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
871 else
872 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
875 static void
876 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
878 struct keymap *k;
879 char p[32];
880 const char *kn;
882 TAILQ_FOREACH(k, &keymap->m, keymaps) {
883 strlcpy(p, prfx, sizeof(p));
884 if (*p != '\0')
885 strlcat(p, " ", sizeof(p));
886 if (k->meta)
887 strlcat(p, "M-", sizeof(p));
888 if ((kn = unkbd(k->key)) != NULL)
889 strlcat(p, kn, sizeof(p));
890 else
891 strlcat(p, keyname(k->key), sizeof(p));
893 if (k->fn == NULL)
894 rec_compute_help(&k->map, p, sizeof(p));
895 else
896 emit_help_item(p, k->fn);
900 static void
901 recompute_help(void)
903 char p[32] = { 0 };
905 empty_vlist(&helpwin);
906 empty_linelist(&helpwin);
907 rec_compute_help(current_map, p, sizeof(p));
908 wrap_page(&helpwin, help_cols);
911 void
912 vmessage(const char *fmt, va_list ap)
914 if (evtimer_pending(&clechoev, NULL))
915 evtimer_del(&clechoev);
917 free(ministate.curmesg);
918 ministate.curmesg = NULL;
920 if (fmt != NULL) {
921 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
922 evtimer_add(&clechoev, &clechoev_timer);
924 /* TODO: what to do if the allocation fails here? */
925 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
926 ministate.curmesg = NULL;
929 redraw_minibuffer();
930 if (in_minibuffer) {
931 wrefresh(body);
932 wrefresh(echoarea);
933 } else {
934 wrefresh(echoarea);
935 wrefresh(body);
939 void
940 message(const char *fmt, ...)
942 va_list ap;
944 va_start(ap, fmt);
945 vmessage(fmt, ap);
946 va_end(ap);
949 void
950 start_loading_anim(struct tab *tab)
952 if (tab->loading_anim)
953 return;
954 tab->loading_anim = 1;
955 evtimer_set(&tab->loadingev, update_loading_anim, tab);
956 evtimer_add(&tab->loadingev, &loadingev_timer);
959 static void
960 update_loading_anim(int fd, short ev, void *d)
962 struct tab *tab = d;
964 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
966 if (tab->flags & TAB_CURRENT) {
967 redraw_modeline(tab);
968 wrefresh(modeline);
969 wrefresh(body);
970 if (in_minibuffer)
971 wrefresh(echoarea);
974 evtimer_add(&tab->loadingev, &loadingev_timer);
977 static void
978 stop_loading_anim(struct tab *tab)
980 if (!tab->loading_anim)
981 return;
982 evtimer_del(&tab->loadingev);
983 tab->loading_anim = 0;
984 tab->loading_anim_step = 0;
986 if (!(tab->flags & TAB_CURRENT))
987 return;
989 redraw_modeline(tab);
991 wrefresh(modeline);
992 wrefresh(body);
993 if (in_minibuffer)
994 wrefresh(echoarea);
997 void
998 load_url_in_tab(struct tab *tab, const char *url)
1000 message("Loading %s...", url);
1001 start_loading_anim(tab);
1002 load_url(tab, url);
1004 tab->buffer.curs_x = 0;
1005 tab->buffer.curs_y = 0;
1006 redraw_tab(tab);
1009 void
1010 switch_to_tab(struct tab *tab)
1012 struct tab *t;
1014 TAILQ_FOREACH(t, &tabshead, tabs) {
1015 t->flags &= ~TAB_CURRENT;
1018 tab->flags |= TAB_CURRENT;
1019 tab->flags &= ~TAB_URGENT;
1022 unsigned int
1023 tab_new_id(void)
1025 return tab_counter++;
1028 struct tab *
1029 new_tab(const char *url)
1031 struct tab *tab;
1033 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1034 event_loopbreak();
1035 return NULL;
1037 tab->fd = -1;
1039 TAILQ_INIT(&tab->hist.head);
1041 TAILQ_INIT(&tab->buffer.head);
1043 tab->id = tab_new_id();
1044 switch_to_tab(tab);
1046 if (TAILQ_EMPTY(&tabshead))
1047 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1048 else
1049 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1051 load_url_in_tab(tab, url);
1052 return tab;
1055 int
1056 ui_init()
1058 setlocale(LC_ALL, "");
1060 TAILQ_INIT(&eecmd_history.head);
1061 TAILQ_INIT(&ir_history.head);
1062 TAILQ_INIT(&lu_history.head);
1064 ministate.line.type = LINE_TEXT;
1065 ministate.vline.parent = &ministate.line;
1066 ministate.buffer.current_line = &ministate.vline;
1068 /* initialize help window */
1069 TAILQ_INIT(&helpwin.head);
1071 base_map = &global_map;
1072 current_map = &global_map;
1074 initscr();
1076 if (enable_colors) {
1077 if (has_colors()) {
1078 start_color();
1079 use_default_colors();
1080 } else
1081 enable_colors = 0;
1084 config_apply_style();
1086 raw();
1087 noecho();
1088 nonl();
1089 intrflush(stdscr, FALSE);
1091 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1092 return 0;
1093 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1094 return 0;
1095 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1096 return 0;
1097 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1098 return 0;
1099 if ((help = newwin(1, 1, 1, 0)) == NULL)
1100 return 0;
1102 body_lines = LINES-3;
1103 body_cols = COLS;
1105 wbkgd(body, body_face.body);
1106 wbkgd(echoarea, minibuffer_face.background);
1108 update_x_offset();
1110 keypad(body, TRUE);
1111 scrollok(body, FALSE);
1113 /* non-blocking input */
1114 wtimeout(body, 0);
1116 mvwprintw(body, 0, 0, "");
1118 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1119 event_add(&stdioev, NULL);
1121 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1122 signal_add(&winchev, NULL);
1124 return 1;
1127 void
1128 ui_on_tab_loaded(struct tab *tab)
1130 stop_loading_anim(tab);
1131 message("Loaded %s", tab->hist_cur->h);
1133 redraw_tabline();
1134 wrefresh(tabline);
1135 if (in_minibuffer)
1136 wrefresh(echoarea);
1137 else
1138 wrefresh(body);
1141 void
1142 ui_on_tab_refresh(struct tab *tab)
1144 wrap_page(&tab->buffer, body_cols);
1145 if (tab->flags & TAB_CURRENT)
1146 redraw_tab(tab);
1147 else
1148 tab->flags |= TAB_URGENT;
1151 const char *
1152 ui_keyname(int k)
1154 return keyname(k);
1157 void
1158 ui_toggle_side_window(void)
1160 side_window = !side_window;
1161 if (side_window)
1162 recompute_help();
1165 * ugly hack, but otherwise the window doesn't get updated
1166 * until I call handle_resize a second time (i.e. C-l). I
1167 * will be happy to know why something like this is needed.
1169 handle_resize_nodelay(0, 0, NULL);
1170 handle_resize_nodelay(0, 0, NULL);
1173 void
1174 ui_schedule_redraw(void)
1176 handle_resize_nodelay(0, 0, NULL);
1179 void
1180 ui_require_input(struct tab *tab, int hide)
1182 /* TODO: hard-switching to another tab is ugly */
1183 switch_to_tab(tab);
1185 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1186 &ir_history);
1187 strlcpy(ministate.prompt, "Input required: ",
1188 sizeof(ministate.prompt));
1189 redraw_tab(tab);
1192 void
1193 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1194 struct tab *data)
1196 yornp(prompt, fn, data);
1197 redraw_tab(current_tab());
1200 void
1201 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1202 unsigned int data)
1204 completing_read(prompt, fn, data);
1205 redraw_tab(current_tab());
1208 void
1209 ui_end(void)
1211 endwin();