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 "defaults.h"
44 #include "minibuffer.h"
45 #include "telescope.h"
46 #include "ui.h"
47 #include "utf8.h"
49 static struct event stdioev, winchev;
51 static void restore_curs_x(struct buffer *);
53 static struct vline *nth_line(struct buffer*, size_t);
54 static int readkey(void);
55 static void dispatch_stdio(int, short, void*);
56 static void handle_clear_echoarea(int, short, void*);
57 static void handle_resize(int, short, void*);
58 static void handle_resize_nodelay(int, short, void*);
59 static void rearrange_windows(void);
60 static int wrap_page(struct buffer*, int);
61 static void print_vline(int, int, WINDOW*, struct vline*);
62 static void redraw_tabline(void);
63 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
64 static void redraw_help(void);
65 static void redraw_body(struct tab*);
66 static void redraw_modeline(struct tab*);
67 static void redraw_minibuffer(void);
68 static void do_redraw_echoarea(void);
69 static void do_redraw_minibuffer(void);
70 static void do_redraw_minibuffer_compl(void);
71 static void place_cursor(int);
72 static void redraw_tab(struct tab*);
73 static void emit_help_item(char*, void*);
74 static void rec_compute_help(struct kmap*, char*, size_t);
75 static void recompute_help(void);
76 static void update_loading_anim(int, short, void*);
77 static void stop_loading_anim(struct tab*);
79 static int should_rearrange_windows;
80 static int too_small;
81 static int x_offset;
83 struct thiskey thiskey;
85 static struct event resizeev;
86 static struct timeval resize_timer = { 0, 250000 };
88 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
90 int body_lines, body_cols;
92 static WINDOW *help;
93 static struct buffer helpwin;
94 static int help_lines, help_cols;
96 static int side_window;
98 static struct event clechoev;
99 static struct timeval clechoev_timer = { 5, 0 };
100 static struct timeval loadingev_timer = { 0, 250000 };
102 static uint32_t tab_counter;
104 static char keybuf[64];
106 struct kmap global_map,
107 minibuffer_map,
108 *current_map,
109 *base_map;
111 int in_minibuffer;
113 static inline void
114 update_x_offset(void)
116 if (olivetti_mode && fill_column < body_cols)
117 x_offset = (body_cols - fill_column)/2;
118 else
119 x_offset = 0;
122 void
123 save_excursion(struct excursion *place, struct buffer *buffer)
125 place->curs_x = buffer->curs_x;
126 place->curs_y = buffer->curs_y;
127 place->line_off = buffer->line_off;
128 place->current_line = buffer->current_line;
129 place->cpoff = buffer->cpoff;
132 void
133 restore_excursion(struct excursion *place, struct buffer *buffer)
135 buffer->curs_x = place->curs_x;
136 buffer->curs_y = place->curs_y;
137 buffer->line_off = place->line_off;
138 buffer->current_line = place->current_line;
139 buffer->cpoff = place->cpoff;
142 static void
143 restore_curs_x(struct buffer *buffer)
145 struct vline *vl;
146 const char *prfx;
148 vl = buffer->current_line;
149 if (vl == NULL || vl->line == NULL)
150 buffer->curs_x = buffer->cpoff = 0;
151 else
152 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
154 buffer->curs_x += x_offset;
156 if (vl != NULL) {
157 prfx = line_prefixes[vl->parent->type].prfx1;
158 buffer->curs_x += utf8_swidth(prfx);
162 void
163 global_key_unbound(void)
165 message("%s is undefined", keybuf);
168 static struct vline *
169 nth_line(struct buffer *buffer, size_t n)
171 struct vline *vl;
172 size_t i;
174 i = 0;
175 TAILQ_FOREACH(vl, &buffer->head, vlines) {
176 if (i == n)
177 return vl;
178 i++;
181 /* unreachable */
182 abort();
185 struct tab *
186 current_tab(void)
188 struct tab *t;
190 TAILQ_FOREACH(t, &tabshead, tabs) {
191 if (t->flags & TAB_CURRENT)
192 return t;
195 /* unreachable */
196 abort();
199 struct buffer *
200 current_buffer(void)
202 if (in_minibuffer)
203 return &ministate.buffer;
204 return &current_tab()->buffer;
207 static int
208 readkey(void)
210 uint32_t state = 0;
212 if ((thiskey.key = wgetch(body)) == ERR)
213 return 0;
215 thiskey.meta = thiskey.key == 27;
216 if (thiskey.meta) {
217 thiskey.key = wgetch(body);
218 if (thiskey.key == ERR || thiskey.key == 27) {
219 thiskey.meta = 0;
220 thiskey.key = 27;
224 thiskey.cp = 0;
225 if ((unsigned int)thiskey.key < UINT8_MAX) {
226 while (1) {
227 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
228 break;
229 if ((thiskey.key = wgetch(body)) == ERR) {
230 message("Error decoding user input");
231 return 0;
236 return 1;
239 static void
240 dispatch_stdio(int fd, short ev, void *d)
242 struct keymap *k;
243 const char *keyname;
244 char tmp[5] = {0};
246 /* TODO: schedule a redraw? */
247 if (too_small)
248 return;
250 if (!readkey())
251 return;
253 if (keybuf[0] != '\0')
254 strlcat(keybuf, " ", sizeof(keybuf));
255 if (thiskey.meta)
256 strlcat(keybuf, "M-", sizeof(keybuf));
257 if (thiskey.cp != 0) {
258 utf8_encode(thiskey.cp, tmp);
259 strlcat(keybuf, tmp, sizeof(keybuf));
260 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
261 strlcat(keybuf, keyname, sizeof(keybuf));
262 } else {
263 tmp[0] = thiskey.key;
264 strlcat(keybuf, tmp, sizeof(keybuf));
267 TAILQ_FOREACH(k, &current_map->m, keymaps) {
268 if (k->meta == thiskey.meta &&
269 k->key == thiskey.key) {
270 if (k->fn == NULL)
271 current_map = &k->map;
272 else {
273 current_map = base_map;
274 strlcpy(keybuf, "", sizeof(keybuf));
275 k->fn(current_buffer());
277 goto done;
281 if (current_map->unhandled_input != NULL)
282 current_map->unhandled_input();
283 else
284 global_key_unbound();
286 strlcpy(keybuf, "", sizeof(keybuf));
287 current_map = base_map;
289 done:
290 if (side_window)
291 recompute_help();
293 if (should_rearrange_windows)
294 rearrange_windows();
295 redraw_tab(current_tab());
298 static void
299 handle_clear_echoarea(int fd, short ev, void *d)
301 free(ministate.curmesg);
302 ministate.curmesg = NULL;
304 redraw_minibuffer();
305 place_cursor(0);
308 static void
309 handle_resize(int sig, short ev, void *d)
311 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
312 event_del(&resizeev);
314 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
315 evtimer_add(&resizeev, &resize_timer);
318 static void
319 handle_resize_nodelay(int s, short ev, void *d)
321 endwin();
322 refresh();
323 clear();
325 rearrange_windows();
328 static void
329 rearrange_windows(void)
331 struct tab *tab;
332 int lines;
334 should_rearrange_windows = 0;
336 lines = LINES;
338 if ((too_small = lines < 15)) {
339 erase();
340 printw("Window too small.");
341 refresh();
342 return;
345 /* move and resize the windows, in reverse order! */
347 if (in_minibuffer == MB_COMPREAD) {
348 mvwin(minibuffer, lines-10, 0);
349 wresize(minibuffer, 10, COLS);
350 lines -= 10;
352 wrap_page(&ministate.compl.buffer, COLS);
355 mvwin(echoarea, --lines, 0);
356 wresize(echoarea, 1, COLS);
358 mvwin(modeline, --lines, 0);
359 wresize(modeline, 1, COLS);
361 body_lines = --lines;
362 body_cols = COLS;
364 if (side_window) {
365 help_cols = 0.3 * COLS;
366 help_lines = lines;
367 mvwin(help, 1, 0);
368 wresize(help, help_lines, help_cols);
370 wrap_page(&helpwin, help_cols);
372 body_cols = COLS - help_cols - 1;
373 mvwin(body, 1, help_cols);
374 } else
375 mvwin(body, 1, 0);
377 update_x_offset();
378 wresize(body, body_lines, body_cols);
380 wresize(tabline, 1, COLS);
382 tab = current_tab();
384 wrap_page(&tab->buffer, body_cols);
385 redraw_tab(tab);
388 static int
389 wrap_page(struct buffer *buffer, int width)
391 struct line *l;
392 const struct line *top_orig, *orig;
393 struct vline *vl;
394 int pre_width;
395 const char *prfx;
397 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
398 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
400 buffer->top_line = NULL;
401 buffer->current_line = NULL;
403 buffer->force_redraw = 1;
404 buffer->curs_y = 0;
405 buffer->line_off = 0;
407 empty_vlist(buffer);
409 TAILQ_FOREACH(l, &buffer->page.head, lines) {
410 prfx = line_prefixes[l->type].prfx1;
411 switch (l->type) {
412 case LINE_TEXT:
413 case LINE_LINK:
414 case LINE_TITLE_1:
415 case LINE_TITLE_2:
416 case LINE_TITLE_3:
417 case LINE_ITEM:
418 case LINE_QUOTE:
419 case LINE_PRE_START:
420 case LINE_PRE_END:
421 wrap_text(buffer, prfx, l, MIN(fill_column, width));
422 break;
423 case LINE_PRE_CONTENT:
424 if (olivetti_mode)
425 pre_width = MIN(fill_column, width);
426 else
427 pre_width = width;
428 hardwrap_text(buffer, l, pre_width);
429 break;
430 case LINE_COMPL:
431 case LINE_COMPL_CURRENT:
432 wrap_one(buffer, prfx, l, width);
433 break;
436 if (top_orig == l && buffer->top_line == NULL) {
437 buffer->line_off = buffer->line_max-1;
438 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
440 while (1) {
441 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
442 if (vl == NULL || vl->parent != orig)
443 break;
444 buffer->top_line = vl;
445 buffer->line_off--;
449 if (orig == l && buffer->current_line == NULL) {
450 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
452 while (1) {
453 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
454 if (vl == NULL || vl->parent != orig)
455 break;
456 buffer->current_line = vl;
461 if (buffer->current_line == NULL)
462 buffer->current_line = TAILQ_FIRST(&buffer->head);
464 if (buffer->top_line == NULL)
465 buffer->top_line = buffer->current_line;
467 return 1;
470 static void
471 print_vline(int off, int width, WINDOW *window, struct vline *vl)
473 const char *text;
474 const char *prfx;
475 struct line_face *f;
476 int i, left, x, y;
478 f = &line_faces[vl->parent->type];
480 /* unused, set by getyx */
481 (void)y;
483 if (!vl->flags)
484 prfx = line_prefixes[vl->parent->type].prfx1;
485 else
486 prfx = line_prefixes[vl->parent->type].prfx2;
488 text = vl->line;
489 if (text == NULL)
490 text = "";
492 wattr_on(window, body_face.left, NULL);
493 for (i = 0; i < off; i++)
494 waddch(window, ' ');
495 wattr_off(window, body_face.left, NULL);
497 wattr_on(window, f->prefix, NULL);
498 wprintw(window, "%s", prfx);
499 wattr_off(window, f->prefix, NULL);
501 wattr_on(window, f->text, NULL);
502 wprintw(window, "%s", text);
503 wattr_off(window, f->text, NULL);
505 getyx(window, y, x);
507 left = width - x;
509 wattr_on(window, f->trail, NULL);
510 for (i = 0; i < left - off; ++i)
511 waddch(window, ' ');
512 wattr_off(window, f->trail, NULL);
514 wattr_on(window, body_face.right, NULL);
515 for (i = 0; i < off; i++)
516 waddch(window, ' ');
517 wattr_off(window, body_face.right, NULL);
521 static void
522 redraw_tabline(void)
524 struct tab *tab;
525 size_t toskip, ots, tabwidth, space, x;
526 int current, y, truncated;
527 const char *title;
528 char buf[25];
530 x = 0;
532 /* unused, but setted by a getyx */
533 (void)y;
535 tabwidth = sizeof(buf)+1;
536 space = COLS-2;
538 toskip = 0;
539 TAILQ_FOREACH(tab, &tabshead, tabs) {
540 toskip++;
541 if (tab->flags & TAB_CURRENT)
542 break;
545 if (toskip * tabwidth < space)
546 toskip = 0;
547 else {
548 ots = toskip;
549 toskip--;
550 while (toskip != 0 &&
551 (ots - toskip+1) * tabwidth < space)
552 toskip--;
555 werase(tabline);
556 wattr_on(tabline, tab_face.background, NULL);
557 wprintw(tabline, toskip == 0 ? " " : "<");
558 wattr_off(tabline, tab_face.background, NULL);
560 truncated = 0;
561 TAILQ_FOREACH(tab, &tabshead, tabs) {
562 if (truncated)
563 break;
564 if (toskip != 0) {
565 toskip--;
566 continue;
569 getyx(tabline, y, x);
570 if (x + sizeof(buf)+2 >= (size_t)COLS)
571 truncated = 1;
573 current = tab->flags & TAB_CURRENT;
575 if (*(title = tab->buffer.page.title) == '\0')
576 title = tab->hist_cur->h;
578 if (tab->flags & TAB_URGENT)
579 strlcpy(buf, "!", sizeof(buf));
580 else
581 strlcpy(buf, " ", sizeof(buf));
583 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
584 /* truncation happens */
585 strlcpy(&buf[sizeof(buf)-4], "...", 4);
586 } else {
587 /* pad with spaces */
588 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
589 /* nop */ ;
592 if (current)
593 wattr_on(tabline, tab_face.current, NULL);
594 else
595 wattr_on(tabline, tab_face.tab, NULL);
597 wprintw(tabline, "%s", buf);
598 if (TAILQ_NEXT(tab, tabs) != NULL)
599 wprintw(tabline, " ");
601 if (current)
602 wattr_off(tabline, tab_face.current, NULL);
603 else
604 wattr_off(tabline, tab_face.tab, NULL);
607 wattr_on(tabline, tab_face.background, NULL);
608 for (; x < (size_t)COLS; ++x)
609 waddch(tabline, ' ');
610 if (truncated)
611 mvwprintw(tabline, 0, COLS-1, ">");
612 wattr_off(tabline, tab_face.background, NULL);
615 /*
616 * Compute the first visible line around vl. Try to search forward
617 * until the end of the buffer; if a visible line is not found, search
618 * backward. Return NULL if no viable line was found.
619 */
620 struct vline *
621 adjust_line(struct vline *vl, struct buffer *buffer)
623 struct vline *t;
625 if (vl == NULL)
626 return NULL;
628 if (!(vl->parent->flags & L_HIDDEN))
629 return vl;
631 /* search forward */
632 for (t = vl;
633 t != NULL && t->parent->flags & L_HIDDEN;
634 t = TAILQ_NEXT(t, vlines))
635 ; /* nop */
637 if (t != NULL)
638 return t;
640 /* search backward */
641 for (t = vl;
642 t != NULL && t->parent->flags & L_HIDDEN;
643 t = TAILQ_PREV(t, vhead, vlines))
644 ; /* nop */
646 return t;
649 static void
650 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
652 struct vline *vl;
653 int l, onscreen;
655 restore_curs_x(buffer);
657 /*
658 * TODO: ignoring buffer->force_update and always
659 * re-rendering. In theory we can recompute the y position
660 * without a re-render, and optimize here. It's not the only
661 * optimisation possible here, wscrl wolud also be an
662 * interesting one.
663 */
665 again:
666 werase(win);
667 buffer->curs_y = 0;
669 if (TAILQ_EMPTY(&buffer->head))
670 goto end;
672 if (buffer->top_line == NULL)
673 buffer->top_line = TAILQ_FIRST(&buffer->head);
675 buffer->top_line = adjust_line(buffer->top_line, buffer);
676 if (buffer->top_line == NULL)
677 goto end;
679 buffer->current_line = adjust_line(buffer->current_line, buffer);
681 l = 0;
682 onscreen = 0;
683 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
684 if (vl->parent->flags & L_HIDDEN)
685 continue;
687 wmove(win, l, 0);
688 print_vline(off, width, win, vl);
690 if (vl == buffer->current_line)
691 onscreen = 1;
693 if (!onscreen)
694 buffer->curs_y++;
696 l++;
697 if (l == height)
698 break;
701 if (!onscreen) {
702 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
703 if (vl == buffer->current_line)
704 break;
705 if (vl->parent->flags & L_HIDDEN)
706 continue;
707 buffer->line_off++;
708 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
711 if (vl != NULL)
712 goto again;
715 buffer->last_line_off = buffer->line_off;
716 buffer->force_redraw = 0;
717 end:
718 wmove(win, buffer->curs_y, buffer->curs_x);
721 static void
722 redraw_help(void)
724 redraw_window(help, 0, help_lines, help_cols, &helpwin);
727 static void
728 redraw_body(struct tab *tab)
730 static struct tab *last_tab;
732 if (last_tab != tab)
733 tab->buffer.force_redraw =1;
734 last_tab = tab;
736 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
739 static inline char
740 trust_status_char(enum trust_state ts)
742 switch (ts) {
743 case TS_UNKNOWN: return 'u';
744 case TS_UNTRUSTED: return '!';
745 case TS_TEMP_TRUSTED: return '!';
746 case TS_TRUSTED: return 'v';
747 case TS_VERIFIED: return 'V';
748 default: return 'X';
752 static void
753 redraw_modeline(struct tab *tab)
755 double pct;
756 int x, y, max_x, max_y;
757 const char *mode = tab->buffer.page.name;
758 const char *spin = "-\\|/";
760 werase(modeline);
761 wattr_on(modeline, modeline_face.background, NULL);
762 wmove(modeline, 0, 0);
764 wprintw(modeline, "-%c%c %s ",
765 spin[tab->loading_anim_step],
766 trust_status_char(tab->trust),
767 mode == NULL ? "(none)" : mode);
769 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
770 / tab->buffer.line_max;
772 if (tab->buffer.line_max <= (size_t)body_lines)
773 wprintw(modeline, "All ");
774 else if (tab->buffer.line_off == 0)
775 wprintw(modeline, "Top ");
776 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
777 wprintw(modeline, "Bottom ");
778 else
779 wprintw(modeline, "%.0f%% ", pct);
781 wprintw(modeline, "%d/%d %s ",
782 tab->buffer.line_off + tab->buffer.curs_y,
783 tab->buffer.line_max,
784 tab->hist_cur->h);
786 getyx(modeline, y, x);
787 getmaxyx(modeline, max_y, max_x);
789 (void)y;
790 (void)max_y;
792 for (; x < max_x; ++x)
793 waddstr(modeline, "-");
795 wattr_off(modeline, modeline_face.background, NULL);
798 static void
799 redraw_minibuffer(void)
801 wattr_on(echoarea, minibuffer_face.background, NULL);
802 werase(echoarea);
804 if (in_minibuffer)
805 do_redraw_minibuffer();
806 else
807 do_redraw_echoarea();
809 if (in_minibuffer == MB_COMPREAD)
810 do_redraw_minibuffer_compl();
812 wattr_off(echoarea, minibuffer_face.background, NULL);
815 static void
816 do_redraw_echoarea(void)
818 struct tab *tab;
820 if (ministate.curmesg != NULL)
821 wprintw(echoarea, "%s", ministate.curmesg);
822 else if (*keybuf != '\0')
823 waddstr(echoarea, keybuf);
824 else {
825 /* If nothing else, show the URL at point */
826 tab = current_tab();
827 if (tab->buffer.current_line != NULL &&
828 tab->buffer.current_line->parent->type == LINE_LINK)
829 waddstr(echoarea,
830 tab->buffer.current_line->parent->meta.alt);
834 static void
835 do_redraw_minibuffer(void)
837 size_t off_y, off_x = 0;
838 const char *start, *c;
840 /* unused, set by getyx */
841 (void)off_y;
843 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
844 if (ministate.hist_cur != NULL)
845 wprintw(echoarea, "(%zu/%zu) ",
846 ministate.hist_off + 1,
847 ministate.history->len);
849 getyx(echoarea, off_y, off_x);
851 start = ministate.hist_cur != NULL
852 ? ministate.hist_cur->h
853 : ministate.buf;
854 c = utf8_nth(ministate.buffer.current_line->line,
855 ministate.buffer.cpoff);
856 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
857 start = utf8_next_cp(start);
860 waddstr(echoarea, start);
862 if (ministate.curmesg != NULL)
863 wprintw(echoarea, " [%s]", ministate.curmesg);
865 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
868 static void
869 do_redraw_minibuffer_compl(void)
871 redraw_window(minibuffer, 0, 10, body_cols,
872 &ministate.compl.buffer);
875 /*
876 * Place the cursor in the right ncurses window. If soft is 1, use
877 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
878 * wrefresh.
879 */
880 static void
881 place_cursor(int soft)
883 int (*touch)(WINDOW *);
885 if (soft)
886 touch = wnoutrefresh;
887 else
888 touch = wrefresh;
890 if (in_minibuffer) {
891 touch(body);
892 touch(echoarea);
893 } else {
894 touch(echoarea);
895 touch(body);
899 static void
900 redraw_tab(struct tab *tab)
902 if (too_small)
903 return;
905 if (side_window) {
906 redraw_help();
907 wnoutrefresh(help);
910 redraw_tabline();
911 redraw_body(tab);
912 redraw_modeline(tab);
913 redraw_minibuffer();
915 wnoutrefresh(tabline);
916 wnoutrefresh(modeline);
918 if (in_minibuffer == MB_COMPREAD)
919 wnoutrefresh(minibuffer);
921 place_cursor(1);
923 doupdate();
926 static void
927 emit_help_item(char *prfx, void *fn)
929 struct line *l;
930 struct cmd *cmd;
932 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
933 if (fn == cmd->fn)
934 break;
936 assert(cmd != NULL);
938 if ((l = calloc(1, sizeof(*l))) == NULL)
939 abort();
941 l->type = LINE_TEXT;
942 l->meta.alt = NULL;
944 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
946 if (TAILQ_EMPTY(&helpwin.page.head))
947 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
948 else
949 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
952 static void
953 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
955 struct keymap *k;
956 char p[32];
957 const char *kn;
959 TAILQ_FOREACH(k, &keymap->m, keymaps) {
960 strlcpy(p, prfx, sizeof(p));
961 if (*p != '\0')
962 strlcat(p, " ", sizeof(p));
963 if (k->meta)
964 strlcat(p, "M-", sizeof(p));
965 if ((kn = unkbd(k->key)) != NULL)
966 strlcat(p, kn, sizeof(p));
967 else
968 strlcat(p, keyname(k->key), sizeof(p));
970 if (k->fn == NULL)
971 rec_compute_help(&k->map, p, sizeof(p));
972 else
973 emit_help_item(p, k->fn);
977 static void
978 recompute_help(void)
980 char p[32] = { 0 };
982 empty_vlist(&helpwin);
983 empty_linelist(&helpwin);
984 rec_compute_help(current_map, p, sizeof(p));
985 wrap_page(&helpwin, help_cols);
988 void
989 vmessage(const char *fmt, va_list ap)
991 if (evtimer_pending(&clechoev, NULL))
992 evtimer_del(&clechoev);
994 free(ministate.curmesg);
995 ministate.curmesg = NULL;
997 if (fmt != NULL) {
998 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
999 evtimer_add(&clechoev, &clechoev_timer);
1001 /* TODO: what to do if the allocation fails here? */
1002 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1003 ministate.curmesg = NULL;
1006 redraw_minibuffer();
1007 place_cursor(0);
1010 void
1011 message(const char *fmt, ...)
1013 va_list ap;
1015 va_start(ap, fmt);
1016 vmessage(fmt, ap);
1017 va_end(ap);
1020 void
1021 start_loading_anim(struct tab *tab)
1023 if (tab->loading_anim)
1024 return;
1025 tab->loading_anim = 1;
1026 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1027 evtimer_add(&tab->loadingev, &loadingev_timer);
1030 static void
1031 update_loading_anim(int fd, short ev, void *d)
1033 struct tab *tab = d;
1035 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1037 if (tab->flags & TAB_CURRENT) {
1038 redraw_modeline(tab);
1039 wrefresh(modeline);
1040 wrefresh(body);
1041 if (in_minibuffer)
1042 wrefresh(echoarea);
1045 evtimer_add(&tab->loadingev, &loadingev_timer);
1048 static void
1049 stop_loading_anim(struct tab *tab)
1051 if (!tab->loading_anim)
1052 return;
1053 evtimer_del(&tab->loadingev);
1054 tab->loading_anim = 0;
1055 tab->loading_anim_step = 0;
1057 if (!(tab->flags & TAB_CURRENT))
1058 return;
1060 redraw_modeline(tab);
1062 wrefresh(modeline);
1063 wrefresh(body);
1064 if (in_minibuffer)
1065 wrefresh(echoarea);
1068 void
1069 load_url_in_tab(struct tab *tab, const char *url)
1071 message("Loading %s...", url);
1072 start_loading_anim(tab);
1073 load_url(tab, url);
1075 tab->buffer.curs_x = 0;
1076 tab->buffer.curs_y = 0;
1077 redraw_tab(tab);
1080 void
1081 switch_to_tab(struct tab *tab)
1083 struct tab *t;
1085 TAILQ_FOREACH(t, &tabshead, tabs) {
1086 t->flags &= ~TAB_CURRENT;
1089 tab->flags |= TAB_CURRENT;
1090 tab->flags &= ~TAB_URGENT;
1093 unsigned int
1094 tab_new_id(void)
1096 return tab_counter++;
1099 struct tab *
1100 new_tab(const char *url)
1102 struct tab *tab;
1104 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1105 event_loopbreak();
1106 return NULL;
1108 tab->fd = -1;
1110 TAILQ_INIT(&tab->hist.head);
1112 TAILQ_INIT(&tab->buffer.head);
1114 tab->id = tab_new_id();
1115 switch_to_tab(tab);
1117 if (TAILQ_EMPTY(&tabshead))
1118 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1119 else
1120 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1122 load_url_in_tab(tab, url);
1123 return tab;
1126 int
1127 ui_print_colors(void)
1129 int colors = 0, pairs = 0, can_change = 0;
1130 int columns = 16, lines, color, i, j;
1132 initscr();
1133 if (has_colors()) {
1134 start_color();
1135 use_default_colors();
1137 colors = COLORS;
1138 pairs = COLOR_PAIRS;
1139 can_change = can_change_color();
1141 endwin();
1143 printf("Term info:\n");
1144 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1145 getenv("TERM"), colors, pairs, can_change);
1146 printf("\n");
1148 if (colors == 0) {
1149 printf("No color support\n");
1150 return 0;
1153 printf("Available colors:\n\n");
1154 lines = (colors - 1) / columns + 1;
1155 color = 0;
1156 for (i = 0; i < lines; ++i) {
1157 for (j = 0; j < columns; ++j, ++color) {
1158 printf("\033[0;38;5;%dm %03d", color, color);
1160 printf("\n");
1163 printf("\033[0m");
1164 fflush(stdout);
1165 return 0;
1168 int
1169 ui_init()
1171 setlocale(LC_ALL, "");
1173 TAILQ_INIT(&eecmd_history.head);
1174 TAILQ_INIT(&ir_history.head);
1175 TAILQ_INIT(&lu_history.head);
1177 ministate.line.type = LINE_TEXT;
1178 ministate.vline.parent = &ministate.line;
1179 ministate.buffer.current_line = &ministate.vline;
1181 /* initialize help window */
1182 TAILQ_INIT(&helpwin.head);
1184 base_map = &global_map;
1185 current_map = &global_map;
1187 initscr();
1189 if (enable_colors) {
1190 if (has_colors()) {
1191 start_color();
1192 use_default_colors();
1193 } else
1194 enable_colors = 0;
1197 config_apply_style();
1199 raw();
1200 noecho();
1201 nonl();
1202 intrflush(stdscr, FALSE);
1204 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1205 return 0;
1206 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1207 return 0;
1208 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1209 return 0;
1210 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1211 return 0;
1212 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1213 return 0;
1214 if ((help = newwin(1, 1, 1, 0)) == NULL)
1215 return 0;
1217 body_lines = LINES-3;
1218 body_cols = COLS;
1220 wbkgd(body, body_face.body);
1221 wbkgd(echoarea, minibuffer_face.background);
1223 update_x_offset();
1225 keypad(body, TRUE);
1226 scrollok(body, FALSE);
1228 /* non-blocking input */
1229 wtimeout(body, 0);
1231 mvwprintw(body, 0, 0, "");
1233 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1234 event_add(&stdioev, NULL);
1236 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1237 signal_add(&winchev, NULL);
1239 return 1;
1242 void
1243 ui_on_tab_loaded(struct tab *tab)
1245 stop_loading_anim(tab);
1246 message("Loaded %s", tab->hist_cur->h);
1248 redraw_tabline();
1249 wrefresh(tabline);
1250 place_cursor(0);
1253 void
1254 ui_on_tab_refresh(struct tab *tab)
1256 wrap_page(&tab->buffer, body_cols);
1257 if (tab->flags & TAB_CURRENT)
1258 redraw_tab(tab);
1259 else
1260 tab->flags |= TAB_URGENT;
1263 const char *
1264 ui_keyname(int k)
1266 return keyname(k);
1269 void
1270 ui_toggle_side_window(void)
1272 side_window = !side_window;
1273 if (side_window)
1274 recompute_help();
1277 * ugly hack, but otherwise the window doesn't get updated
1278 * until I call rearrange_windows a second time (e.g. via
1279 * C-l). I will be happy to know why something like this is
1280 * needed.
1282 rearrange_windows();
1283 rearrange_windows();
1286 void
1287 ui_schedule_redraw(void)
1289 should_rearrange_windows = 1;
1292 void
1293 ui_require_input(struct tab *tab, int hide)
1295 /* TODO: hard-switching to another tab is ugly */
1296 switch_to_tab(tab);
1298 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1299 &ir_history, NULL, NULL);
1300 strlcpy(ministate.prompt, "Input required: ",
1301 sizeof(ministate.prompt));
1302 redraw_tab(tab);
1305 void
1306 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1307 struct tab *data)
1309 yornp(prompt, fn, data);
1310 redraw_tab(current_tab());
1313 void
1314 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1315 struct tab *data)
1317 completing_read(prompt, fn, data);
1318 redraw_tab(current_tab());
1321 void
1322 ui_end(void)
1324 endwin();