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 int wrap_page(struct buffer*, int);
60 static void print_vline(int, int, WINDOW*, struct vline*);
61 static void redraw_tabline(void);
62 static void redraw_window(WINDOW*, int, int, struct buffer*);
63 static void redraw_help(void);
64 static void redraw_body(struct tab*);
65 static void redraw_modeline(struct tab*);
66 static void redraw_minibuffer(void);
67 static void do_redraw_echoarea(void);
68 static void do_redraw_minibuffer(void);
69 static void place_cursor(int);
70 static void redraw_tab(struct tab*);
71 static void emit_help_item(char*, void*);
72 static void rec_compute_help(struct kmap*, char*, size_t);
73 static void recompute_help(void);
74 static void update_loading_anim(int, short, void*);
75 static void stop_loading_anim(struct tab*);
77 static int too_small;
78 static int x_offset;
80 struct thiskey thiskey;
82 static struct event resizeev;
83 static struct timeval resize_timer = { 0, 250000 };
85 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
87 int body_lines, body_cols;
89 static WINDOW *help;
90 static struct buffer helpwin;
91 static int help_lines, help_cols;
93 static int side_window;
95 static struct event clechoev;
96 static struct timeval clechoev_timer = { 5, 0 };
97 static struct timeval loadingev_timer = { 0, 250000 };
99 static uint32_t tab_counter;
101 static char keybuf[64];
103 struct kmap global_map,
104 minibuffer_map,
105 *current_map,
106 *base_map;
108 int in_minibuffer;
110 static inline void
111 update_x_offset(void)
113 if (olivetti_mode && fill_column < body_cols)
114 x_offset = (body_cols - fill_column)/2;
115 else
116 x_offset = 0;
119 void
120 save_excursion(struct excursion *place, struct buffer *buffer)
122 place->curs_x = buffer->curs_x;
123 place->curs_y = buffer->curs_y;
124 place->line_off = buffer->line_off;
125 place->current_line = buffer->current_line;
126 place->cpoff = buffer->cpoff;
129 void
130 restore_excursion(struct excursion *place, struct buffer *buffer)
132 buffer->curs_x = place->curs_x;
133 buffer->curs_y = place->curs_y;
134 buffer->line_off = place->line_off;
135 buffer->current_line = place->current_line;
136 buffer->cpoff = place->cpoff;
139 static void
140 restore_curs_x(struct buffer *buffer)
142 struct vline *vl;
143 const char *prfx;
145 vl = buffer->current_line;
146 if (vl == NULL || vl->line == NULL)
147 buffer->curs_x = buffer->cpoff = 0;
148 else
149 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
151 buffer->curs_x += x_offset;
153 if (vl != NULL) {
154 prfx = line_prefixes[vl->parent->type].prfx1;
155 buffer->curs_x += utf8_swidth(prfx);
159 void
160 global_key_unbound(void)
162 message("%s is undefined", keybuf);
165 static struct vline *
166 nth_line(struct buffer *buffer, size_t n)
168 struct vline *vl;
169 size_t i;
171 i = 0;
172 TAILQ_FOREACH(vl, &buffer->head, vlines) {
173 if (i == n)
174 return vl;
175 i++;
178 /* unreachable */
179 abort();
182 struct tab *
183 current_tab(void)
185 struct tab *t;
187 TAILQ_FOREACH(t, &tabshead, tabs) {
188 if (t->flags & TAB_CURRENT)
189 return t;
192 /* unreachable */
193 abort();
196 struct buffer *
197 current_buffer(void)
199 if (in_minibuffer)
200 return &ministate.buffer;
201 return &current_tab()->buffer;
204 static int
205 readkey(void)
207 uint32_t state = 0;
209 if ((thiskey.key = wgetch(body)) == ERR)
210 return 0;
212 thiskey.meta = thiskey.key == 27;
213 if (thiskey.meta) {
214 thiskey.key = wgetch(body);
215 if (thiskey.key == ERR || thiskey.key == 27) {
216 thiskey.meta = 0;
217 thiskey.key = 27;
221 thiskey.cp = 0;
222 if ((unsigned int)thiskey.key < UINT8_MAX) {
223 while (1) {
224 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
225 break;
226 if ((thiskey.key = wgetch(body)) == ERR) {
227 message("Error decoding user input");
228 return 0;
233 return 1;
236 static void
237 dispatch_stdio(int fd, short ev, void *d)
239 struct keymap *k;
240 const char *keyname;
241 char tmp[5] = {0};
243 /* TODO: schedule a redraw? */
244 if (too_small)
245 return;
247 if (!readkey())
248 return;
250 if (keybuf[0] != '\0')
251 strlcat(keybuf, " ", sizeof(keybuf));
252 if (thiskey.meta)
253 strlcat(keybuf, "M-", sizeof(keybuf));
254 if (thiskey.cp != 0) {
255 utf8_encode(thiskey.cp, tmp);
256 strlcat(keybuf, tmp, sizeof(keybuf));
257 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
258 strlcat(keybuf, keyname, sizeof(keybuf));
259 } else {
260 tmp[0] = thiskey.key;
261 strlcat(keybuf, tmp, sizeof(keybuf));
264 TAILQ_FOREACH(k, &current_map->m, keymaps) {
265 if (k->meta == thiskey.meta &&
266 k->key == thiskey.key) {
267 if (k->fn == NULL)
268 current_map = &k->map;
269 else {
270 current_map = base_map;
271 strlcpy(keybuf, "", sizeof(keybuf));
272 k->fn(current_buffer());
274 goto done;
278 if (current_map->unhandled_input != NULL)
279 current_map->unhandled_input();
280 else
281 global_key_unbound();
283 strlcpy(keybuf, "", sizeof(keybuf));
284 current_map = base_map;
286 done:
287 if (side_window)
288 recompute_help();
290 redraw_tab(current_tab());
293 static void
294 handle_clear_echoarea(int fd, short ev, void *d)
296 free(ministate.curmesg);
297 ministate.curmesg = NULL;
299 redraw_minibuffer();
300 place_cursor(0);
303 static void
304 handle_resize(int sig, short ev, void *d)
306 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
307 event_del(&resizeev);
309 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
310 evtimer_add(&resizeev, &resize_timer);
313 static void
314 handle_resize_nodelay(int s, short ev, void *d)
316 struct tab *tab;
317 int lines;
319 endwin();
320 refresh();
321 clear();
323 lines = LINES;
325 if ((too_small = lines < 15)) {
326 erase();
327 printw("Window too small.");
328 refresh();
329 return;
332 /* move and resize the windows, in reverse order! */
334 mvwin(echoarea, --lines, 0);
335 wresize(echoarea, 1, COLS);
337 mvwin(modeline, --lines, 0);
338 wresize(modeline, 1, COLS);
340 body_lines = --lines;
341 body_cols = COLS;
343 if (side_window) {
344 help_cols = 0.3 * COLS;
345 help_lines = lines;
346 mvwin(help, 1, 0);
347 wresize(help, help_lines, help_cols);
349 wrap_page(&helpwin, help_cols);
351 body_cols = COLS - help_cols - 1;
352 mvwin(body, 1, help_cols);
353 } else
354 mvwin(body, 1, 0);
356 update_x_offset();
357 wresize(body, body_lines, body_cols);
359 wresize(tabline, 1, COLS);
361 tab = current_tab();
363 wrap_page(&tab->buffer, body_cols);
364 redraw_tab(tab);
367 static int
368 wrap_page(struct buffer *buffer, int width)
370 struct line *l;
371 const struct line *top_orig, *orig;
372 struct vline *vl;
373 int pre_width;
374 const char *prfx;
376 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
377 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
379 buffer->top_line = NULL;
380 buffer->current_line = NULL;
382 buffer->force_redraw = 1;
383 buffer->curs_y = 0;
384 buffer->line_off = 0;
386 empty_vlist(buffer);
388 TAILQ_FOREACH(l, &buffer->page.head, lines) {
389 prfx = line_prefixes[l->type].prfx1;
390 switch (l->type) {
391 case LINE_TEXT:
392 case LINE_LINK:
393 case LINE_TITLE_1:
394 case LINE_TITLE_2:
395 case LINE_TITLE_3:
396 case LINE_ITEM:
397 case LINE_QUOTE:
398 case LINE_PRE_START:
399 case LINE_PRE_END:
400 wrap_text(buffer, prfx, l, MIN(fill_column, width));
401 break;
402 case LINE_PRE_CONTENT:
403 if (olivetti_mode)
404 pre_width = MIN(fill_column, width);
405 else
406 pre_width = width;
407 hardwrap_text(buffer, l, pre_width);
408 break;
411 if (top_orig == l && buffer->top_line == NULL) {
412 buffer->line_off = buffer->line_max-1;
413 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
415 while (1) {
416 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
417 if (vl == NULL || vl->parent != orig)
418 break;
419 buffer->top_line = vl;
420 buffer->line_off--;
424 if (orig == l && buffer->current_line == NULL) {
425 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
427 while (1) {
428 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
429 if (vl == NULL || vl->parent != orig)
430 break;
431 buffer->current_line = vl;
436 if (buffer->current_line == NULL)
437 buffer->current_line = TAILQ_FIRST(&buffer->head);
439 if (buffer->top_line == NULL)
440 buffer->top_line = buffer->current_line;
442 return 1;
445 static void
446 print_vline(int off, int width, WINDOW *window, struct vline *vl)
448 const char *text;
449 const char *prfx;
450 struct line_face *f;
451 int i, left, x, y;
453 f = &line_faces[vl->parent->type];
455 /* unused, set by getyx */
456 (void)y;
458 if (!vl->flags)
459 prfx = line_prefixes[vl->parent->type].prfx1;
460 else
461 prfx = line_prefixes[vl->parent->type].prfx2;
463 text = vl->line;
464 if (text == NULL)
465 text = "";
467 wattr_on(window, body_face.left, NULL);
468 for (i = 0; i < off; i++)
469 waddch(window, ' ');
470 wattr_off(window, body_face.left, NULL);
472 wattr_on(window, f->prefix, NULL);
473 wprintw(window, "%s", prfx);
474 wattr_off(window, f->prefix, NULL);
476 wattr_on(window, f->text, NULL);
477 wprintw(window, "%s", text);
478 wattr_off(window, f->text, NULL);
480 getyx(window, y, x);
482 left = width - x;
484 wattr_on(window, f->trail, NULL);
485 for (i = 0; i < left - off; ++i)
486 waddch(window, ' ');
487 wattr_off(window, f->trail, NULL);
489 wattr_on(window, body_face.right, NULL);
490 for (i = 0; i < off; i++)
491 waddch(window, ' ');
492 wattr_off(window, body_face.right, NULL);
496 static void
497 redraw_tabline(void)
499 struct tab *tab;
500 size_t toskip, ots, tabwidth, space, x;
501 int current, y, truncated;
502 const char *title;
503 char buf[25];
505 x = 0;
507 /* unused, but setted by a getyx */
508 (void)y;
510 tabwidth = sizeof(buf)+1;
511 space = COLS-2;
513 toskip = 0;
514 TAILQ_FOREACH(tab, &tabshead, tabs) {
515 toskip++;
516 if (tab->flags & TAB_CURRENT)
517 break;
520 if (toskip * tabwidth < space)
521 toskip = 0;
522 else {
523 ots = toskip;
524 toskip--;
525 while (toskip != 0 &&
526 (ots - toskip+1) * tabwidth < space)
527 toskip--;
530 werase(tabline);
531 wattr_on(tabline, tab_face.background, NULL);
532 wprintw(tabline, toskip == 0 ? " " : "<");
533 wattr_off(tabline, tab_face.background, NULL);
535 truncated = 0;
536 TAILQ_FOREACH(tab, &tabshead, tabs) {
537 if (truncated)
538 break;
539 if (toskip != 0) {
540 toskip--;
541 continue;
544 getyx(tabline, y, x);
545 if (x + sizeof(buf)+2 >= (size_t)COLS)
546 truncated = 1;
548 current = tab->flags & TAB_CURRENT;
550 if (*(title = tab->buffer.page.title) == '\0')
551 title = tab->hist_cur->h;
553 if (tab->flags & TAB_URGENT)
554 strlcpy(buf, "!", sizeof(buf));
555 else
556 strlcpy(buf, " ", sizeof(buf));
558 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
559 /* truncation happens */
560 strlcpy(&buf[sizeof(buf)-4], "...", 4);
561 } else {
562 /* pad with spaces */
563 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
564 /* nop */ ;
567 if (current)
568 wattr_on(tabline, tab_face.current, NULL);
569 else
570 wattr_on(tabline, tab_face.tab, NULL);
572 wprintw(tabline, "%s", buf);
573 if (TAILQ_NEXT(tab, tabs) != NULL)
574 wprintw(tabline, " ");
576 if (current)
577 wattr_off(tabline, tab_face.current, NULL);
578 else
579 wattr_off(tabline, tab_face.tab, NULL);
582 wattr_on(tabline, tab_face.background, NULL);
583 for (; x < (size_t)COLS; ++x)
584 waddch(tabline, ' ');
585 if (truncated)
586 mvwprintw(tabline, 0, COLS-1, ">");
587 wattr_off(tabline, tab_face.background, NULL);
590 /*
591 * Compute the first visible line around vl. Try to search forward
592 * until the end of the buffer; if a visible line is not found, search
593 * backward. Return NULL if no viable line was found.
594 */
595 static inline struct vline *
596 adjust_line(struct vline *vl, struct buffer *buffer)
598 struct vline *t;
600 if (!(vl->parent->flags & L_HIDDEN))
601 return vl;
603 /* search forward */
604 for (t = vl;
605 t != NULL && t->parent->flags & L_HIDDEN;
606 t = TAILQ_NEXT(t, vlines))
607 ; /* nop */
609 if (t != NULL)
610 return t;
612 /* search backward */
613 for (t = vl;
614 t != NULL && t->parent->flags & L_HIDDEN;
615 t = TAILQ_PREV(t, vhead, vlines))
616 ; /* nop */
618 return t;
621 static void
622 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
624 struct vline *vl;
625 int l, onscreen;
627 restore_curs_x(buffer);
629 /*
630 * TODO: ignoring buffer->force_update and always
631 * re-rendering. In theory we can recompute the y position
632 * without a re-render, and optimize here. It's not the only
633 * optimisation possible here, wscrl wolud also be an
634 * interesting one.
635 */
637 again:
638 werase(win);
639 buffer->curs_y = 0;
641 if (TAILQ_EMPTY(&buffer->head))
642 goto end;
644 buffer->top_line = adjust_line(buffer->top_line, buffer);
645 if (buffer->top_line == NULL)
646 goto end;
648 buffer->current_line = adjust_line(buffer->current_line, buffer);
650 l = 0;
651 onscreen = 0;
652 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
653 if (vl->parent->flags & L_HIDDEN)
654 continue;
656 wmove(win, l, 0);
657 print_vline(x_offset, width, win, vl);
659 if (vl == buffer->current_line)
660 onscreen = 1;
662 if (!onscreen)
663 buffer->curs_y++;
665 l++;
666 if (l == height)
667 break;
670 if (!onscreen) {
671 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
672 if (vl == buffer->current_line)
673 break;
674 if (vl->parent->flags & L_HIDDEN)
675 continue;
676 buffer->line_off++;
677 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
680 goto again;
683 buffer->last_line_off = buffer->line_off;
684 buffer->force_redraw = 0;
685 end:
686 wmove(win, buffer->curs_y, buffer->curs_x);
689 static void
690 redraw_help(void)
692 redraw_window(help, help_lines, help_cols, &helpwin);
695 static void
696 redraw_body(struct tab *tab)
698 static struct tab *last_tab;
700 if (last_tab != tab)
701 tab->buffer.force_redraw =1;
702 last_tab = tab;
704 redraw_window(body, body_lines, body_cols, &tab->buffer);
707 static inline char
708 trust_status_char(enum trust_state ts)
710 switch (ts) {
711 case TS_UNKNOWN: return 'u';
712 case TS_UNTRUSTED: return '!';
713 case TS_TEMP_TRUSTED: return '!';
714 case TS_TRUSTED: return 'v';
715 case TS_VERIFIED: return 'V';
716 default: return 'X';
720 static void
721 redraw_modeline(struct tab *tab)
723 double pct;
724 int x, y, max_x, max_y;
725 const char *mode = tab->buffer.page.name;
726 const char *spin = "-\\|/";
728 werase(modeline);
729 wattr_on(modeline, modeline_face.background, NULL);
730 wmove(modeline, 0, 0);
732 wprintw(modeline, "-%c%c %s ",
733 spin[tab->loading_anim_step],
734 trust_status_char(tab->trust),
735 mode == NULL ? "(none)" : mode);
737 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
739 if (tab->buffer.line_max <= (size_t)body_lines)
740 wprintw(modeline, "All ");
741 else if (tab->buffer.line_off == 0)
742 wprintw(modeline, "Top ");
743 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
744 wprintw(modeline, "Bottom ");
745 else
746 wprintw(modeline, "%.0f%% ", pct);
748 wprintw(modeline, "%d/%d %s ",
749 tab->buffer.line_off + tab->buffer.curs_y,
750 tab->buffer.line_max,
751 tab->hist_cur->h);
753 getyx(modeline, y, x);
754 getmaxyx(modeline, max_y, max_x);
756 (void)y;
757 (void)max_y;
759 for (; x < max_x; ++x)
760 waddstr(modeline, "-");
762 wattr_off(modeline, modeline_face.background, NULL);
765 static void
766 redraw_minibuffer(void)
768 wattr_on(echoarea, minibuffer_face.background, NULL);
769 werase(echoarea);
771 if (in_minibuffer)
772 do_redraw_minibuffer();
773 else
774 do_redraw_echoarea();
776 wattr_off(echoarea, minibuffer_face.background, NULL);
779 static void
780 do_redraw_echoarea(void)
782 struct tab *tab;
784 if (ministate.curmesg != NULL)
785 wprintw(echoarea, "%s", ministate.curmesg);
786 else if (*keybuf != '\0')
787 waddstr(echoarea, keybuf);
788 else {
789 /* If nothing else, show the URL at point */
790 tab = current_tab();
791 if (tab->buffer.current_line != NULL &&
792 tab->buffer.current_line->parent->type == LINE_LINK)
793 waddstr(echoarea, tab->buffer.current_line->parent->alt);
797 static void
798 do_redraw_minibuffer(void)
800 size_t off_y, off_x = 0;
801 const char *start, *c;
803 /* unused, set by getyx */
804 (void)off_y;
806 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
807 if (ministate.hist_cur != NULL)
808 wprintw(echoarea, "(%zu/%zu) ",
809 ministate.hist_off + 1,
810 ministate.history->len);
812 getyx(echoarea, off_y, off_x);
814 start = ministate.hist_cur != NULL
815 ? ministate.hist_cur->h
816 : ministate.buf;
817 c = utf8_nth(ministate.buffer.current_line->line,
818 ministate.buffer.cpoff);
819 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
820 start = utf8_next_cp(start);
823 waddstr(echoarea, start);
825 if (ministate.curmesg != NULL)
826 wprintw(echoarea, " [%s]", ministate.curmesg);
828 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
831 /*
832 * Place the cursor in the right ncurses window. If soft is 1, use
833 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
834 * wrefresh.
835 */
836 static void
837 place_cursor(int soft)
839 int (*touch)(WINDOW *);
841 if (soft)
842 touch = wnoutrefresh;
843 else
844 touch = wrefresh;
846 if (in_minibuffer) {
847 touch(body);
848 touch(echoarea);
849 } else {
850 touch(echoarea);
851 touch(body);
855 static void
856 redraw_tab(struct tab *tab)
858 if (too_small)
859 return;
861 if (side_window) {
862 redraw_help();
863 wnoutrefresh(help);
866 redraw_tabline();
867 redraw_body(tab);
868 redraw_modeline(tab);
869 redraw_minibuffer();
871 wnoutrefresh(tabline);
872 wnoutrefresh(modeline);
874 place_cursor(1);
876 doupdate();
879 static void
880 emit_help_item(char *prfx, void *fn)
882 struct line *l;
883 struct cmd *cmd;
885 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
886 if (fn == cmd->fn)
887 break;
889 assert(cmd != NULL);
891 if ((l = calloc(1, sizeof(*l))) == NULL)
892 abort();
894 l->type = LINE_TEXT;
895 l->alt = NULL;
897 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
899 if (TAILQ_EMPTY(&helpwin.page.head))
900 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
901 else
902 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
905 static void
906 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
908 struct keymap *k;
909 char p[32];
910 const char *kn;
912 TAILQ_FOREACH(k, &keymap->m, keymaps) {
913 strlcpy(p, prfx, sizeof(p));
914 if (*p != '\0')
915 strlcat(p, " ", sizeof(p));
916 if (k->meta)
917 strlcat(p, "M-", sizeof(p));
918 if ((kn = unkbd(k->key)) != NULL)
919 strlcat(p, kn, sizeof(p));
920 else
921 strlcat(p, keyname(k->key), sizeof(p));
923 if (k->fn == NULL)
924 rec_compute_help(&k->map, p, sizeof(p));
925 else
926 emit_help_item(p, k->fn);
930 static void
931 recompute_help(void)
933 char p[32] = { 0 };
935 empty_vlist(&helpwin);
936 empty_linelist(&helpwin);
937 rec_compute_help(current_map, p, sizeof(p));
938 wrap_page(&helpwin, help_cols);
941 void
942 vmessage(const char *fmt, va_list ap)
944 if (evtimer_pending(&clechoev, NULL))
945 evtimer_del(&clechoev);
947 free(ministate.curmesg);
948 ministate.curmesg = NULL;
950 if (fmt != NULL) {
951 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
952 evtimer_add(&clechoev, &clechoev_timer);
954 /* TODO: what to do if the allocation fails here? */
955 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
956 ministate.curmesg = NULL;
959 redraw_minibuffer();
960 place_cursor(0);
963 void
964 message(const char *fmt, ...)
966 va_list ap;
968 va_start(ap, fmt);
969 vmessage(fmt, ap);
970 va_end(ap);
973 void
974 start_loading_anim(struct tab *tab)
976 if (tab->loading_anim)
977 return;
978 tab->loading_anim = 1;
979 evtimer_set(&tab->loadingev, update_loading_anim, tab);
980 evtimer_add(&tab->loadingev, &loadingev_timer);
983 static void
984 update_loading_anim(int fd, short ev, void *d)
986 struct tab *tab = d;
988 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
990 if (tab->flags & TAB_CURRENT) {
991 redraw_modeline(tab);
992 wrefresh(modeline);
993 wrefresh(body);
994 if (in_minibuffer)
995 wrefresh(echoarea);
998 evtimer_add(&tab->loadingev, &loadingev_timer);
1001 static void
1002 stop_loading_anim(struct tab *tab)
1004 if (!tab->loading_anim)
1005 return;
1006 evtimer_del(&tab->loadingev);
1007 tab->loading_anim = 0;
1008 tab->loading_anim_step = 0;
1010 if (!(tab->flags & TAB_CURRENT))
1011 return;
1013 redraw_modeline(tab);
1015 wrefresh(modeline);
1016 wrefresh(body);
1017 if (in_minibuffer)
1018 wrefresh(echoarea);
1021 void
1022 load_url_in_tab(struct tab *tab, const char *url)
1024 message("Loading %s...", url);
1025 start_loading_anim(tab);
1026 load_url(tab, url);
1028 tab->buffer.curs_x = 0;
1029 tab->buffer.curs_y = 0;
1030 redraw_tab(tab);
1033 void
1034 switch_to_tab(struct tab *tab)
1036 struct tab *t;
1038 TAILQ_FOREACH(t, &tabshead, tabs) {
1039 t->flags &= ~TAB_CURRENT;
1042 tab->flags |= TAB_CURRENT;
1043 tab->flags &= ~TAB_URGENT;
1046 unsigned int
1047 tab_new_id(void)
1049 return tab_counter++;
1052 struct tab *
1053 new_tab(const char *url)
1055 struct tab *tab;
1057 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1058 event_loopbreak();
1059 return NULL;
1061 tab->fd = -1;
1063 TAILQ_INIT(&tab->hist.head);
1065 TAILQ_INIT(&tab->buffer.head);
1067 tab->id = tab_new_id();
1068 switch_to_tab(tab);
1070 if (TAILQ_EMPTY(&tabshead))
1071 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1072 else
1073 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1075 load_url_in_tab(tab, url);
1076 return tab;
1079 int
1080 ui_init()
1082 setlocale(LC_ALL, "");
1084 TAILQ_INIT(&eecmd_history.head);
1085 TAILQ_INIT(&ir_history.head);
1086 TAILQ_INIT(&lu_history.head);
1088 ministate.line.type = LINE_TEXT;
1089 ministate.vline.parent = &ministate.line;
1090 ministate.buffer.current_line = &ministate.vline;
1092 /* initialize help window */
1093 TAILQ_INIT(&helpwin.head);
1095 base_map = &global_map;
1096 current_map = &global_map;
1098 initscr();
1100 if (enable_colors) {
1101 if (has_colors()) {
1102 start_color();
1103 use_default_colors();
1104 } else
1105 enable_colors = 0;
1108 config_apply_style();
1110 raw();
1111 noecho();
1112 nonl();
1113 intrflush(stdscr, FALSE);
1115 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1116 return 0;
1117 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1118 return 0;
1119 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1120 return 0;
1121 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1122 return 0;
1123 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1124 return 0;
1125 if ((help = newwin(1, 1, 1, 0)) == NULL)
1126 return 0;
1128 body_lines = LINES-3;
1129 body_cols = COLS;
1131 wbkgd(body, body_face.body);
1132 wbkgd(echoarea, minibuffer_face.background);
1134 update_x_offset();
1136 keypad(body, TRUE);
1137 scrollok(body, FALSE);
1139 /* non-blocking input */
1140 wtimeout(body, 0);
1142 mvwprintw(body, 0, 0, "");
1144 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1145 event_add(&stdioev, NULL);
1147 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1148 signal_add(&winchev, NULL);
1150 return 1;
1153 void
1154 ui_on_tab_loaded(struct tab *tab)
1156 stop_loading_anim(tab);
1157 message("Loaded %s", tab->hist_cur->h);
1159 redraw_tabline();
1160 wrefresh(tabline);
1161 place_cursor(0);
1164 void
1165 ui_on_tab_refresh(struct tab *tab)
1167 wrap_page(&tab->buffer, body_cols);
1168 if (tab->flags & TAB_CURRENT)
1169 redraw_tab(tab);
1170 else
1171 tab->flags |= TAB_URGENT;
1174 const char *
1175 ui_keyname(int k)
1177 return keyname(k);
1180 void
1181 ui_toggle_side_window(void)
1183 side_window = !side_window;
1184 if (side_window)
1185 recompute_help();
1188 * ugly hack, but otherwise the window doesn't get updated
1189 * until I call handle_resize a second time (i.e. C-l). I
1190 * will be happy to know why something like this is needed.
1192 handle_resize_nodelay(0, 0, NULL);
1193 handle_resize_nodelay(0, 0, NULL);
1196 void
1197 ui_schedule_redraw(void)
1199 handle_resize_nodelay(0, 0, NULL);
1202 void
1203 ui_require_input(struct tab *tab, int hide)
1205 /* TODO: hard-switching to another tab is ugly */
1206 switch_to_tab(tab);
1208 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1209 &ir_history, NULL, NULL);
1210 strlcpy(ministate.prompt, "Input required: ",
1211 sizeof(ministate.prompt));
1212 redraw_tab(tab);
1215 void
1216 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1217 struct tab *data)
1219 yornp(prompt, fn, data);
1220 redraw_tab(current_tab());
1223 void
1224 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1225 struct tab *data)
1227 completing_read(prompt, fn, data, NULL, NULL);
1228 redraw_tab(current_tab());
1231 void
1232 ui_end(void)
1234 endwin();