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"
47 static struct event stdioev, winchev;
49 static void restore_curs_x(struct buffer *);
51 static struct vline *nth_line(struct buffer*, size_t);
52 static int readkey(void);
53 static void dispatch_stdio(int, short, void*);
54 static void handle_clear_echoarea(int, short, void*);
55 static void handle_resize(int, short, void*);
56 static void handle_resize_nodelay(int, short, void*);
57 static int wrap_page(struct buffer*, int);
58 static void print_vline(int, int, WINDOW*, struct vline*);
59 static void redraw_tabline(void);
60 static void redraw_window(WINDOW*, int, int, struct buffer*);
61 static void redraw_help(void);
62 static void redraw_body(struct tab*);
63 static void redraw_modeline(struct tab*);
64 static void redraw_echoarea(void);
65 static void redraw_tab(struct tab*);
66 static void emit_help_item(char*, void*);
67 static void rec_compute_help(struct kmap*, char*, size_t);
68 static void recompute_help(void);
69 static void update_loading_anim(int, short, void*);
70 static void stop_loading_anim(struct tab*);
72 static int x_offset;
74 struct thiskey thiskey;
76 static struct event resizeev;
77 static struct timeval resize_timer = { 0, 250000 };
79 static WINDOW *tabline, *body, *modeline, *echoarea;
81 int body_lines, body_cols;
83 static WINDOW *help;
84 static struct buffer helpwin;
85 static int help_lines, help_cols;
87 static int side_window;
89 static struct event clechoev;
90 static struct timeval clechoev_timer = { 5, 0 };
91 static struct timeval loadingev_timer = { 0, 250000 };
93 static uint32_t tab_counter;
95 static char keybuf[64];
97 struct kmap global_map,
98 minibuffer_map,
99 *current_map,
100 *base_map;
102 int in_minibuffer;
104 static inline void
105 update_x_offset(void)
107 if (olivetti_mode && fill_column < body_cols)
108 x_offset = (body_cols - fill_column)/2;
109 else
110 x_offset = 0;
113 void
114 save_excursion(struct excursion *place, struct buffer *buffer)
116 place->curs_x = buffer->curs_x;
117 place->curs_y = buffer->curs_y;
118 place->line_off = buffer->line_off;
119 place->current_line = buffer->current_line;
120 place->cpoff = buffer->cpoff;
123 void
124 restore_excursion(struct excursion *place, struct buffer *buffer)
126 buffer->curs_x = place->curs_x;
127 buffer->curs_y = place->curs_y;
128 buffer->line_off = place->line_off;
129 buffer->current_line = place->current_line;
130 buffer->cpoff = place->cpoff;
133 static void
134 restore_curs_x(struct buffer *buffer)
136 struct vline *vl;
137 const char *prfx;
139 vl = buffer->current_line;
140 if (vl == NULL || vl->line == NULL)
141 buffer->curs_x = buffer->cpoff = 0;
142 else
143 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
145 buffer->curs_x += x_offset;
147 if (vl != NULL) {
148 prfx = line_prefixes[vl->parent->type].prfx1;
149 buffer->curs_x += utf8_swidth(prfx);
153 void
154 global_key_unbound(void)
156 message("%s is undefined", keybuf);
159 static struct vline *
160 nth_line(struct buffer *buffer, size_t n)
162 struct vline *vl;
163 size_t i;
165 i = 0;
166 TAILQ_FOREACH(vl, &buffer->head, vlines) {
167 if (i == n)
168 return vl;
169 i++;
172 /* unreachable */
173 abort();
176 struct tab *
177 current_tab(void)
179 struct tab *t;
181 TAILQ_FOREACH(t, &tabshead, tabs) {
182 if (t->flags & TAB_CURRENT)
183 return t;
186 /* unreachable */
187 abort();
190 struct buffer *
191 current_buffer(void)
193 if (in_minibuffer)
194 return &ministate.buffer;
195 return &current_tab()->buffer;
198 static int
199 readkey(void)
201 uint32_t state = 0;
203 if ((thiskey.key = wgetch(body)) == ERR)
204 return 0;
206 thiskey.meta = thiskey.key == 27;
207 if (thiskey.meta) {
208 thiskey.key = wgetch(body);
209 if (thiskey.key == ERR || thiskey.key == 27) {
210 thiskey.meta = 0;
211 thiskey.key = 27;
215 thiskey.cp = 0;
216 if ((unsigned int)thiskey.key < UINT8_MAX) {
217 while (1) {
218 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
219 break;
220 if ((thiskey.key = wgetch(body)) == ERR) {
221 message("Error decoding user input");
222 return 0;
227 return 1;
230 static void
231 dispatch_stdio(int fd, short ev, void *d)
233 struct keymap *k;
234 const char *keyname;
235 char tmp[5] = {0};
237 if (!readkey())
238 return;
240 if (keybuf[0] != '\0')
241 strlcat(keybuf, " ", sizeof(keybuf));
242 if (thiskey.meta)
243 strlcat(keybuf, "M-", sizeof(keybuf));
244 if (thiskey.cp != 0) {
245 utf8_encode(thiskey.cp, tmp);
246 strlcat(keybuf, tmp, sizeof(keybuf));
247 } else {
248 if ((keyname = unkbd(thiskey.key)) != NULL)
249 strlcat(keybuf, keyname, sizeof(keybuf));
250 else {
251 tmp[0] = thiskey.key;
252 strlcat(keybuf, tmp, sizeof(keybuf));
256 TAILQ_FOREACH(k, &current_map->m, keymaps) {
257 if (k->meta == thiskey.meta &&
258 k->key == thiskey.key) {
259 if (k->fn == NULL)
260 current_map = &k->map;
261 else {
262 current_map = base_map;
263 strlcpy(keybuf, "", sizeof(keybuf));
264 k->fn(current_buffer());
266 goto done;
270 if (current_map->unhandled_input != NULL)
271 current_map->unhandled_input();
272 else
273 global_key_unbound();
275 strlcpy(keybuf, "", sizeof(keybuf));
276 current_map = base_map;
278 done:
279 if (side_window)
280 recompute_help();
282 redraw_tab(current_tab());
285 static void
286 handle_clear_echoarea(int fd, short ev, void *d)
288 free(ministate.curmesg);
289 ministate.curmesg = NULL;
291 redraw_echoarea();
292 if (in_minibuffer) {
293 wrefresh(body);
294 wrefresh(echoarea);
295 } else {
296 wrefresh(echoarea);
297 wrefresh(body);
301 static void
302 handle_resize(int sig, short ev, void *d)
304 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
305 event_del(&resizeev);
307 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
308 evtimer_add(&resizeev, &resize_timer);
311 static void
312 handle_resize_nodelay(int s, short ev, void *d)
314 struct tab *tab;
316 endwin();
317 refresh();
318 clear();
320 /* move and resize the windows, in reverse order! */
322 mvwin(echoarea, LINES-1, 0);
323 wresize(echoarea, 1, COLS);
325 mvwin(modeline, LINES-2, 0);
326 wresize(modeline, 1, COLS);
328 body_lines = LINES-3;
329 body_cols = COLS;
331 if (side_window) {
332 help_cols = 0.3 * COLS;
333 help_lines = LINES-3;
334 mvwin(help, 1, 0);
335 wresize(help, help_lines, help_cols);
337 wrap_page(&helpwin, help_cols);
339 body_cols = COLS - help_cols - 1;
340 mvwin(body, 1, help_cols);
341 } else
342 mvwin(body, 1, 0);
344 update_x_offset();
345 wresize(body, body_lines, body_cols);
347 wresize(tabline, 1, COLS);
349 tab = current_tab();
351 wrap_page(&tab->buffer, body_cols);
352 redraw_tab(tab);
355 static int
356 wrap_page(struct buffer *buffer, int width)
358 struct line *l;
359 const struct line *top_orig, *orig;
360 struct vline *vl;
361 int pre_width;
362 const char *prfx;
364 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
365 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
367 buffer->top_line = NULL;
368 buffer->current_line = NULL;
370 buffer->force_redraw = 1;
371 buffer->curs_y = 0;
372 buffer->line_off = 0;
374 empty_vlist(buffer);
376 TAILQ_FOREACH(l, &buffer->page.head, lines) {
377 prfx = line_prefixes[l->type].prfx1;
378 switch (l->type) {
379 case LINE_TEXT:
380 case LINE_LINK:
381 case LINE_TITLE_1:
382 case LINE_TITLE_2:
383 case LINE_TITLE_3:
384 case LINE_ITEM:
385 case LINE_QUOTE:
386 case LINE_PRE_START:
387 case LINE_PRE_END:
388 wrap_text(buffer, prfx, l, MIN(fill_column, width));
389 break;
390 case LINE_PRE_CONTENT:
391 if (olivetti_mode)
392 pre_width = MIN(fill_column, width);
393 else
394 pre_width = width;
395 hardwrap_text(buffer, l, pre_width);
396 break;
399 if (top_orig == l && buffer->top_line == NULL) {
400 buffer->line_off = buffer->line_max-1;
401 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
403 while (1) {
404 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
405 if (vl == NULL || vl->parent != orig)
406 break;
407 buffer->top_line = vl;
408 buffer->line_off--;
412 if (orig == l && buffer->current_line == NULL) {
413 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
415 while (1) {
416 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
417 if (vl == NULL || vl->parent != orig)
418 break;
419 buffer->current_line = vl;
424 if (buffer->current_line == NULL)
425 buffer->current_line = TAILQ_FIRST(&buffer->head);
427 if (buffer->top_line == NULL)
428 buffer->top_line = buffer->current_line;
430 return 1;
433 static void
434 print_vline(int off, int width, WINDOW *window, struct vline *vl)
436 const char *text;
437 const char *prfx;
438 struct line_face *f;
439 int i, left, x, y;
441 f = &line_faces[vl->parent->type];
443 /* unused, set by getyx */
444 (void)y;
446 if (!vl->flags)
447 prfx = line_prefixes[vl->parent->type].prfx1;
448 else
449 prfx = line_prefixes[vl->parent->type].prfx2;
451 text = vl->line;
452 if (text == NULL)
453 text = "";
455 wattr_on(window, body_face.left, NULL);
456 for (i = 0; i < off; i++)
457 waddch(window, ' ');
458 wattr_off(window, body_face.left, NULL);
460 wattr_on(window, f->prefix, NULL);
461 wprintw(window, "%s", prfx);
462 wattr_off(window, f->prefix, NULL);
464 wattr_on(window, f->text, NULL);
465 wprintw(window, "%s", text);
466 wattr_off(window, f->text, NULL);
468 getyx(window, y, x);
470 left = width - x;
472 wattr_on(window, f->trail, NULL);
473 for (i = 0; i < left - off; ++i)
474 waddch(window, ' ');
475 wattr_off(window, f->trail, NULL);
477 wattr_on(window, body_face.right, NULL);
478 for (i = 0; i < off; i++)
479 waddch(window, ' ');
480 wattr_off(window, body_face.right, NULL);
484 static void
485 redraw_tabline(void)
487 struct tab *tab;
488 size_t toskip, ots, tabwidth, space, x;
489 int current, y, truncated;
490 const char *title;
491 char buf[25];
493 x = 0;
495 /* unused, but setted by a getyx */
496 (void)y;
498 tabwidth = sizeof(buf)+1;
499 space = COLS-2;
501 toskip = 0;
502 TAILQ_FOREACH(tab, &tabshead, tabs) {
503 toskip++;
504 if (tab->flags & TAB_CURRENT)
505 break;
508 if (toskip * tabwidth < space)
509 toskip = 0;
510 else {
511 ots = toskip;
512 toskip--;
513 while (toskip != 0 &&
514 (ots - toskip+1) * tabwidth < space)
515 toskip--;
518 werase(tabline);
519 wattr_on(tabline, tab_face.background, NULL);
520 wprintw(tabline, toskip == 0 ? " " : "<");
521 wattr_off(tabline, tab_face.background, NULL);
523 truncated = 0;
524 TAILQ_FOREACH(tab, &tabshead, tabs) {
525 if (truncated)
526 break;
527 if (toskip != 0) {
528 toskip--;
529 continue;
532 getyx(tabline, y, x);
533 if (x + sizeof(buf)+2 >= (size_t)COLS)
534 truncated = 1;
536 current = tab->flags & TAB_CURRENT;
538 if (*(title = tab->buffer.page.title) == '\0')
539 title = tab->hist_cur->h;
541 if (tab->flags & TAB_URGENT)
542 strlcpy(buf, "!", sizeof(buf));
543 else
544 strlcpy(buf, " ", sizeof(buf));
546 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
547 /* truncation happens */
548 strlcpy(&buf[sizeof(buf)-4], "...", 4);
549 } else {
550 /* pad with spaces */
551 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
552 /* nop */ ;
555 if (current)
556 wattr_on(tabline, tab_face.current, NULL);
557 else
558 wattr_on(tabline, tab_face.tab, NULL);
560 wprintw(tabline, "%s", buf);
561 if (TAILQ_NEXT(tab, tabs) != NULL)
562 wprintw(tabline, " ");
564 if (current)
565 wattr_off(tabline, tab_face.current, NULL);
566 else
567 wattr_off(tabline, tab_face.tab, NULL);
570 wattr_on(tabline, tab_face.background, NULL);
571 for (; x < (size_t)COLS; ++x)
572 waddch(tabline, ' ');
573 if (truncated)
574 mvwprintw(tabline, 0, COLS-1, ">");
575 wattr_off(tabline, tab_face.background, NULL);
578 /*
579 * Compute the first visible line around vl. Try to search forward
580 * until the end of the buffer; if a visible line is not found, search
581 * backward. Return NULL if no viable line was found.
582 */
583 static inline struct vline *
584 adjust_line(struct vline *vl, struct buffer *buffer)
586 struct vline *t;
588 if (!(vl->parent->flags & L_HIDDEN))
589 return vl;
591 /* search forward */
592 for (t = vl;
593 t != NULL && t->parent->flags & L_HIDDEN;
594 t = TAILQ_NEXT(t, vlines))
595 ; /* nop */
597 if (t != NULL)
598 return t;
600 /* search backward */
601 for (t = vl;
602 t != NULL && t->parent->flags & L_HIDDEN;
603 t = TAILQ_PREV(t, vhead, vlines))
604 ; /* nop */
606 return t;
609 static void
610 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
612 struct vline *vl;
613 int l, onscreen;
615 restore_curs_x(buffer);
617 /*
618 * TODO: ignoring buffer->force_update and always
619 * re-rendering. In theory we can recompute the y position
620 * without a re-render, and optimize here. It's not the only
621 * optimisation possible here, wscrl wolud also be an
622 * interesting one.
623 */
625 again:
626 werase(win);
627 buffer->curs_y = 0;
629 if (TAILQ_EMPTY(&buffer->head))
630 goto end;
632 buffer->top_line = adjust_line(buffer->top_line, buffer);
633 if (buffer->top_line == NULL)
634 goto end;
636 buffer->current_line = adjust_line(buffer->current_line, buffer);
638 l = 0;
639 onscreen = 0;
640 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
641 if (vl->parent->flags & L_HIDDEN)
642 continue;
644 wmove(win, l, 0);
645 print_vline(x_offset, width, win, vl);
647 if (vl == buffer->current_line)
648 onscreen = 1;
650 if (!onscreen)
651 buffer->curs_y++;
653 l++;
654 if (l == height)
655 break;
658 if (!onscreen) {
659 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
660 if (vl == buffer->current_line)
661 break;
662 if (vl->parent->flags & L_HIDDEN)
663 continue;
664 buffer->line_off++;
665 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
668 goto again;
671 buffer->last_line_off = buffer->line_off;
672 buffer->force_redraw = 0;
673 end:
674 wmove(win, buffer->curs_y, buffer->curs_x);
677 static void
678 redraw_help(void)
680 redraw_window(help, help_lines, help_cols, &helpwin);
683 static void
684 redraw_body(struct tab *tab)
686 static struct tab *last_tab;
688 if (last_tab != tab)
689 tab->buffer.force_redraw =1;
690 last_tab = tab;
692 redraw_window(body, body_lines, body_cols, &tab->buffer);
695 static inline char
696 trust_status_char(enum trust_state ts)
698 switch (ts) {
699 case TS_UNKNOWN: return 'u';
700 case TS_UNTRUSTED: return '!';
701 case TS_TEMP_TRUSTED: return '!';
702 case TS_TRUSTED: return 'v';
703 case TS_VERIFIED: return 'V';
704 default: return 'X';
708 static void
709 redraw_modeline(struct tab *tab)
711 double pct;
712 int x, y, max_x, max_y;
713 const char *mode = tab->buffer.page.name;
714 const char *spin = "-\\|/";
716 werase(modeline);
717 wattr_on(modeline, modeline_face.background, NULL);
718 wmove(modeline, 0, 0);
720 wprintw(modeline, "-%c%c %s ",
721 spin[tab->loading_anim_step],
722 trust_status_char(tab->trust),
723 mode == NULL ? "(none)" : mode);
725 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
727 if (tab->buffer.line_max <= (size_t)body_lines)
728 wprintw(modeline, "All ");
729 else if (tab->buffer.line_off == 0)
730 wprintw(modeline, "Top ");
731 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
732 wprintw(modeline, "Bottom ");
733 else
734 wprintw(modeline, "%.0f%% ", pct);
736 wprintw(modeline, "%d/%d %s ",
737 tab->buffer.line_off + tab->buffer.curs_y,
738 tab->buffer.line_max,
739 tab->hist_cur->h);
741 getyx(modeline, y, x);
742 getmaxyx(modeline, max_y, max_x);
744 (void)y;
745 (void)max_y;
747 for (; x < max_x; ++x)
748 waddstr(modeline, "-");
750 wattr_off(modeline, modeline_face.background, NULL);
753 static void
754 redraw_echoarea(void)
756 struct tab *tab;
757 size_t off_y, off_x = 0;
758 char *start = NULL, *c = NULL;
760 /* unused, but set by getyx */
761 (void)off_y;
763 wattr_on(echoarea, minibuffer_face.background, NULL);
764 werase(echoarea);
766 if (in_minibuffer) {
767 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
768 if (ministate.hist_cur != NULL)
769 wprintw(echoarea, "(%zu/%zu) ",
770 ministate.hist_off + 1,
771 ministate.history->len);
773 getyx(echoarea, off_y, off_x);
775 start = ministate.hist_cur != NULL
776 ? ministate.hist_cur->h
777 : ministate.buf;
778 c = utf8_nth(ministate.buffer.current_line->line,
779 ministate.buffer.cpoff);
780 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
781 start = utf8_next_cp(start);
784 waddstr(echoarea, start);
787 if (ministate.curmesg != NULL)
788 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
789 ministate.curmesg);
791 if (!in_minibuffer && ministate.curmesg == NULL)
792 waddstr(echoarea, keybuf);
794 /* If nothing else, show the URL at point */
795 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
796 tab = current_tab();
797 if (tab->buffer.current_line != NULL &&
798 tab->buffer.current_line->parent->type == LINE_LINK)
799 waddstr(echoarea, tab->buffer.current_line->parent->alt);
802 if (in_minibuffer)
803 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
805 wattr_off(echoarea, minibuffer_face.background, NULL);
808 static void
809 redraw_tab(struct tab *tab)
811 if (side_window) {
812 redraw_help();
813 wnoutrefresh(help);
816 redraw_tabline();
817 redraw_body(tab);
818 redraw_modeline(tab);
819 redraw_echoarea();
821 wnoutrefresh(tabline);
822 wnoutrefresh(modeline);
824 if (in_minibuffer) {
825 wnoutrefresh(body);
826 wnoutrefresh(echoarea);
827 } else {
828 wnoutrefresh(echoarea);
829 wnoutrefresh(body);
832 doupdate();
835 static void
836 emit_help_item(char *prfx, void *fn)
838 struct line *l;
839 struct cmd *cmd;
841 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
842 if (fn == cmd->fn)
843 break;
845 assert(cmd != NULL);
847 if ((l = calloc(1, sizeof(*l))) == NULL)
848 abort();
850 l->type = LINE_TEXT;
851 l->alt = NULL;
853 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
855 if (TAILQ_EMPTY(&helpwin.page.head))
856 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
857 else
858 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
861 static void
862 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
864 struct keymap *k;
865 char p[32];
866 const char *kn;
868 TAILQ_FOREACH(k, &keymap->m, keymaps) {
869 strlcpy(p, prfx, sizeof(p));
870 if (*p != '\0')
871 strlcat(p, " ", sizeof(p));
872 if (k->meta)
873 strlcat(p, "M-", sizeof(p));
874 if ((kn = unkbd(k->key)) != NULL)
875 strlcat(p, kn, sizeof(p));
876 else
877 strlcat(p, keyname(k->key), sizeof(p));
879 if (k->fn == NULL)
880 rec_compute_help(&k->map, p, sizeof(p));
881 else
882 emit_help_item(p, k->fn);
886 static void
887 recompute_help(void)
889 char p[32] = { 0 };
891 empty_vlist(&helpwin);
892 empty_linelist(&helpwin);
893 rec_compute_help(current_map, p, sizeof(p));
894 wrap_page(&helpwin, help_cols);
897 void
898 vmessage(const char *fmt, va_list ap)
900 if (evtimer_pending(&clechoev, NULL))
901 evtimer_del(&clechoev);
903 free(ministate.curmesg);
904 ministate.curmesg = NULL;
906 if (fmt != NULL) {
907 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
908 evtimer_add(&clechoev, &clechoev_timer);
910 /* TODO: what to do if the allocation fails here? */
911 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
912 ministate.curmesg = NULL;
915 redraw_echoarea();
916 if (in_minibuffer) {
917 wrefresh(body);
918 wrefresh(echoarea);
919 } else {
920 wrefresh(echoarea);
921 wrefresh(body);
925 void
926 message(const char *fmt, ...)
928 va_list ap;
930 va_start(ap, fmt);
931 vmessage(fmt, ap);
932 va_end(ap);
935 void
936 start_loading_anim(struct tab *tab)
938 if (tab->loading_anim)
939 return;
940 tab->loading_anim = 1;
941 evtimer_set(&tab->loadingev, update_loading_anim, tab);
942 evtimer_add(&tab->loadingev, &loadingev_timer);
945 static void
946 update_loading_anim(int fd, short ev, void *d)
948 struct tab *tab = d;
950 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
952 if (tab->flags & TAB_CURRENT) {
953 redraw_modeline(tab);
954 wrefresh(modeline);
955 wrefresh(body);
956 if (in_minibuffer)
957 wrefresh(echoarea);
960 evtimer_add(&tab->loadingev, &loadingev_timer);
963 static void
964 stop_loading_anim(struct tab *tab)
966 if (!tab->loading_anim)
967 return;
968 evtimer_del(&tab->loadingev);
969 tab->loading_anim = 0;
970 tab->loading_anim_step = 0;
972 if (!(tab->flags & TAB_CURRENT))
973 return;
975 redraw_modeline(tab);
977 wrefresh(modeline);
978 wrefresh(body);
979 if (in_minibuffer)
980 wrefresh(echoarea);
983 void
984 load_url_in_tab(struct tab *tab, const char *url)
986 message("Loading %s...", url);
987 start_loading_anim(tab);
988 load_url(tab, url);
990 tab->buffer.curs_x = 0;
991 tab->buffer.curs_y = 0;
992 redraw_tab(tab);
995 void
996 switch_to_tab(struct tab *tab)
998 struct tab *t;
1000 TAILQ_FOREACH(t, &tabshead, tabs) {
1001 t->flags &= ~TAB_CURRENT;
1004 tab->flags |= TAB_CURRENT;
1005 tab->flags &= ~TAB_URGENT;
1008 unsigned int
1009 tab_new_id(void)
1011 return tab_counter++;
1014 struct tab *
1015 new_tab(const char *url)
1017 struct tab *tab;
1019 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1020 event_loopbreak();
1021 return NULL;
1023 tab->fd = -1;
1025 TAILQ_INIT(&tab->hist.head);
1027 TAILQ_INIT(&tab->buffer.head);
1029 tab->id = tab_new_id();
1030 switch_to_tab(tab);
1032 if (TAILQ_EMPTY(&tabshead))
1033 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1034 else
1035 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1037 load_url_in_tab(tab, url);
1038 return tab;
1041 int
1042 ui_init()
1044 setlocale(LC_ALL, "");
1046 TAILQ_INIT(&eecmd_history.head);
1047 TAILQ_INIT(&ir_history.head);
1048 TAILQ_INIT(&lu_history.head);
1050 ministate.line.type = LINE_TEXT;
1051 ministate.vline.parent = &ministate.line;
1052 ministate.buffer.current_line = &ministate.vline;
1054 /* initialize help window */
1055 TAILQ_INIT(&helpwin.head);
1057 base_map = &global_map;
1058 current_map = &global_map;
1060 initscr();
1062 if (enable_colors) {
1063 if (has_colors()) {
1064 start_color();
1065 use_default_colors();
1066 } else
1067 enable_colors = 0;
1070 config_apply_style();
1072 raw();
1073 noecho();
1074 nonl();
1075 intrflush(stdscr, FALSE);
1077 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1078 return 0;
1079 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1080 return 0;
1081 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1082 return 0;
1083 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1084 return 0;
1085 if ((help = newwin(1, 1, 1, 0)) == NULL)
1086 return 0;
1088 body_lines = LINES-3;
1089 body_cols = COLS;
1091 wbkgd(body, body_face.body);
1092 wbkgd(echoarea, minibuffer_face.background);
1094 update_x_offset();
1096 keypad(body, TRUE);
1097 scrollok(body, FALSE);
1099 /* non-blocking input */
1100 wtimeout(body, 0);
1102 mvwprintw(body, 0, 0, "");
1104 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1105 event_add(&stdioev, NULL);
1107 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1108 signal_add(&winchev, NULL);
1110 return 1;
1113 void
1114 ui_on_tab_loaded(struct tab *tab)
1116 stop_loading_anim(tab);
1117 message("Loaded %s", tab->hist_cur->h);
1119 redraw_tabline();
1120 wrefresh(tabline);
1121 if (in_minibuffer)
1122 wrefresh(echoarea);
1123 else
1124 wrefresh(body);
1127 void
1128 ui_on_tab_refresh(struct tab *tab)
1130 wrap_page(&tab->buffer, body_cols);
1131 if (tab->flags & TAB_CURRENT)
1132 redraw_tab(tab);
1133 else
1134 tab->flags |= TAB_URGENT;
1137 const char *
1138 ui_keyname(int k)
1140 return keyname(k);
1143 void
1144 ui_toggle_side_window(void)
1146 side_window = !side_window;
1147 if (side_window)
1148 recompute_help();
1151 * ugly hack, but otherwise the window doesn't get updated
1152 * until I call handle_resize a second time (i.e. C-l). I
1153 * will be happy to know why something like this is needed.
1155 handle_resize_nodelay(0, 0, NULL);
1156 handle_resize_nodelay(0, 0, NULL);
1159 void
1160 ui_schedule_redraw(void)
1162 handle_resize_nodelay(0, 0, NULL);
1165 void
1166 ui_require_input(struct tab *tab, int hide)
1168 /* TODO: hard-switching to another tab is ugly */
1169 switch_to_tab(tab);
1171 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1172 &ir_history);
1173 strlcpy(ministate.prompt, "Input required: ",
1174 sizeof(ministate.prompt));
1175 redraw_tab(tab);
1178 void
1179 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1180 struct tab *data)
1182 yornp(prompt, fn, data);
1183 redraw_tab(current_tab());
1186 void
1187 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1188 unsigned int data)
1190 completing_read(prompt, fn, data);
1191 redraw_tab(current_tab());
1194 void
1195 ui_end(void)
1197 endwin();