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 place_cursor(int);
69 static void redraw_tab(struct tab*);
70 static void emit_help_item(char*, void*);
71 static void rec_compute_help(struct kmap*, char*, size_t);
72 static void recompute_help(void);
73 static void update_loading_anim(int, short, void*);
74 static void stop_loading_anim(struct tab*);
76 static int x_offset;
78 struct thiskey thiskey;
80 static struct event resizeev;
81 static struct timeval resize_timer = { 0, 250000 };
83 static WINDOW *tabline, *body, *modeline, *echoarea;
85 int body_lines, body_cols;
87 static WINDOW *help;
88 static struct buffer helpwin;
89 static int help_lines, help_cols;
91 static int side_window;
93 static struct event clechoev;
94 static struct timeval clechoev_timer = { 5, 0 };
95 static struct timeval loadingev_timer = { 0, 250000 };
97 static uint32_t tab_counter;
99 static char keybuf[64];
101 struct kmap global_map,
102 minibuffer_map,
103 *current_map,
104 *base_map;
106 int in_minibuffer;
108 static inline void
109 update_x_offset(void)
111 if (olivetti_mode && fill_column < body_cols)
112 x_offset = (body_cols - fill_column)/2;
113 else
114 x_offset = 0;
117 void
118 save_excursion(struct excursion *place, struct buffer *buffer)
120 place->curs_x = buffer->curs_x;
121 place->curs_y = buffer->curs_y;
122 place->line_off = buffer->line_off;
123 place->current_line = buffer->current_line;
124 place->cpoff = buffer->cpoff;
127 void
128 restore_excursion(struct excursion *place, struct buffer *buffer)
130 buffer->curs_x = place->curs_x;
131 buffer->curs_y = place->curs_y;
132 buffer->line_off = place->line_off;
133 buffer->current_line = place->current_line;
134 buffer->cpoff = place->cpoff;
137 static void
138 restore_curs_x(struct buffer *buffer)
140 struct vline *vl;
141 const char *prfx;
143 vl = buffer->current_line;
144 if (vl == NULL || vl->line == NULL)
145 buffer->curs_x = buffer->cpoff = 0;
146 else
147 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
149 buffer->curs_x += x_offset;
151 if (vl != NULL) {
152 prfx = line_prefixes[vl->parent->type].prfx1;
153 buffer->curs_x += utf8_swidth(prfx);
157 void
158 global_key_unbound(void)
160 message("%s is undefined", keybuf);
163 static struct vline *
164 nth_line(struct buffer *buffer, size_t n)
166 struct vline *vl;
167 size_t i;
169 i = 0;
170 TAILQ_FOREACH(vl, &buffer->head, vlines) {
171 if (i == n)
172 return vl;
173 i++;
176 /* unreachable */
177 abort();
180 struct tab *
181 current_tab(void)
183 struct tab *t;
185 TAILQ_FOREACH(t, &tabshead, tabs) {
186 if (t->flags & TAB_CURRENT)
187 return t;
190 /* unreachable */
191 abort();
194 struct buffer *
195 current_buffer(void)
197 if (in_minibuffer)
198 return &ministate.buffer;
199 return &current_tab()->buffer;
202 static int
203 readkey(void)
205 uint32_t state = 0;
207 if ((thiskey.key = wgetch(body)) == ERR)
208 return 0;
210 thiskey.meta = thiskey.key == 27;
211 if (thiskey.meta) {
212 thiskey.key = wgetch(body);
213 if (thiskey.key == ERR || thiskey.key == 27) {
214 thiskey.meta = 0;
215 thiskey.key = 27;
219 thiskey.cp = 0;
220 if ((unsigned int)thiskey.key < UINT8_MAX) {
221 while (1) {
222 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
223 break;
224 if ((thiskey.key = wgetch(body)) == ERR) {
225 message("Error decoding user input");
226 return 0;
231 return 1;
234 static void
235 dispatch_stdio(int fd, short ev, void *d)
237 struct keymap *k;
238 const char *keyname;
239 char tmp[5] = {0};
241 if (!readkey())
242 return;
244 if (keybuf[0] != '\0')
245 strlcat(keybuf, " ", sizeof(keybuf));
246 if (thiskey.meta)
247 strlcat(keybuf, "M-", sizeof(keybuf));
248 if (thiskey.cp != 0) {
249 utf8_encode(thiskey.cp, tmp);
250 strlcat(keybuf, tmp, sizeof(keybuf));
251 } else {
252 if ((keyname = unkbd(thiskey.key)) != NULL)
253 strlcat(keybuf, keyname, sizeof(keybuf));
254 else {
255 tmp[0] = thiskey.key;
256 strlcat(keybuf, tmp, sizeof(keybuf));
260 TAILQ_FOREACH(k, &current_map->m, keymaps) {
261 if (k->meta == thiskey.meta &&
262 k->key == thiskey.key) {
263 if (k->fn == NULL)
264 current_map = &k->map;
265 else {
266 current_map = base_map;
267 strlcpy(keybuf, "", sizeof(keybuf));
268 k->fn(current_buffer());
270 goto done;
274 if (current_map->unhandled_input != NULL)
275 current_map->unhandled_input();
276 else
277 global_key_unbound();
279 strlcpy(keybuf, "", sizeof(keybuf));
280 current_map = base_map;
282 done:
283 if (side_window)
284 recompute_help();
286 redraw_tab(current_tab());
289 static void
290 handle_clear_echoarea(int fd, short ev, void *d)
292 free(ministate.curmesg);
293 ministate.curmesg = NULL;
295 redraw_minibuffer();
296 place_cursor(0);
299 static void
300 handle_resize(int sig, short ev, void *d)
302 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
303 event_del(&resizeev);
305 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
306 evtimer_add(&resizeev, &resize_timer);
309 static void
310 handle_resize_nodelay(int s, short ev, void *d)
312 struct tab *tab;
314 endwin();
315 refresh();
316 clear();
318 /* move and resize the windows, in reverse order! */
320 mvwin(echoarea, LINES-1, 0);
321 wresize(echoarea, 1, COLS);
323 mvwin(modeline, LINES-2, 0);
324 wresize(modeline, 1, COLS);
326 body_lines = LINES-3;
327 body_cols = COLS;
329 if (side_window) {
330 help_cols = 0.3 * COLS;
331 help_lines = LINES-3;
332 mvwin(help, 1, 0);
333 wresize(help, help_lines, help_cols);
335 wrap_page(&helpwin, help_cols);
337 body_cols = COLS - help_cols - 1;
338 mvwin(body, 1, help_cols);
339 } else
340 mvwin(body, 1, 0);
342 update_x_offset();
343 wresize(body, body_lines, body_cols);
345 wresize(tabline, 1, COLS);
347 tab = current_tab();
349 wrap_page(&tab->buffer, body_cols);
350 redraw_tab(tab);
353 static int
354 wrap_page(struct buffer *buffer, int width)
356 struct line *l;
357 const struct line *top_orig, *orig;
358 struct vline *vl;
359 int pre_width;
360 const char *prfx;
362 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
363 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
365 buffer->top_line = NULL;
366 buffer->current_line = NULL;
368 buffer->force_redraw = 1;
369 buffer->curs_y = 0;
370 buffer->line_off = 0;
372 empty_vlist(buffer);
374 TAILQ_FOREACH(l, &buffer->page.head, lines) {
375 prfx = line_prefixes[l->type].prfx1;
376 switch (l->type) {
377 case LINE_TEXT:
378 case LINE_LINK:
379 case LINE_TITLE_1:
380 case LINE_TITLE_2:
381 case LINE_TITLE_3:
382 case LINE_ITEM:
383 case LINE_QUOTE:
384 case LINE_PRE_START:
385 case LINE_PRE_END:
386 wrap_text(buffer, prfx, l, MIN(fill_column, width));
387 break;
388 case LINE_PRE_CONTENT:
389 if (olivetti_mode)
390 pre_width = MIN(fill_column, width);
391 else
392 pre_width = width;
393 hardwrap_text(buffer, l, pre_width);
394 break;
397 if (top_orig == l && buffer->top_line == NULL) {
398 buffer->line_off = buffer->line_max-1;
399 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
401 while (1) {
402 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
403 if (vl == NULL || vl->parent != orig)
404 break;
405 buffer->top_line = vl;
406 buffer->line_off--;
410 if (orig == l && buffer->current_line == NULL) {
411 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
413 while (1) {
414 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
415 if (vl == NULL || vl->parent != orig)
416 break;
417 buffer->current_line = vl;
422 if (buffer->current_line == NULL)
423 buffer->current_line = TAILQ_FIRST(&buffer->head);
425 if (buffer->top_line == NULL)
426 buffer->top_line = buffer->current_line;
428 return 1;
431 static void
432 print_vline(int off, int width, WINDOW *window, struct vline *vl)
434 const char *text;
435 const char *prfx;
436 struct line_face *f;
437 int i, left, x, y;
439 f = &line_faces[vl->parent->type];
441 /* unused, set by getyx */
442 (void)y;
444 if (!vl->flags)
445 prfx = line_prefixes[vl->parent->type].prfx1;
446 else
447 prfx = line_prefixes[vl->parent->type].prfx2;
449 text = vl->line;
450 if (text == NULL)
451 text = "";
453 wattr_on(window, body_face.left, NULL);
454 for (i = 0; i < off; i++)
455 waddch(window, ' ');
456 wattr_off(window, body_face.left, NULL);
458 wattr_on(window, f->prefix, NULL);
459 wprintw(window, "%s", prfx);
460 wattr_off(window, f->prefix, NULL);
462 wattr_on(window, f->text, NULL);
463 wprintw(window, "%s", text);
464 wattr_off(window, f->text, NULL);
466 getyx(window, y, x);
468 left = width - x;
470 wattr_on(window, f->trail, NULL);
471 for (i = 0; i < left - off; ++i)
472 waddch(window, ' ');
473 wattr_off(window, f->trail, NULL);
475 wattr_on(window, body_face.right, NULL);
476 for (i = 0; i < off; i++)
477 waddch(window, ' ');
478 wattr_off(window, body_face.right, NULL);
482 static void
483 redraw_tabline(void)
485 struct tab *tab;
486 size_t toskip, ots, tabwidth, space, x;
487 int current, y, truncated;
488 const char *title;
489 char buf[25];
491 x = 0;
493 /* unused, but setted by a getyx */
494 (void)y;
496 tabwidth = sizeof(buf)+1;
497 space = COLS-2;
499 toskip = 0;
500 TAILQ_FOREACH(tab, &tabshead, tabs) {
501 toskip++;
502 if (tab->flags & TAB_CURRENT)
503 break;
506 if (toskip * tabwidth < space)
507 toskip = 0;
508 else {
509 ots = toskip;
510 toskip--;
511 while (toskip != 0 &&
512 (ots - toskip+1) * tabwidth < space)
513 toskip--;
516 werase(tabline);
517 wattr_on(tabline, tab_face.background, NULL);
518 wprintw(tabline, toskip == 0 ? " " : "<");
519 wattr_off(tabline, tab_face.background, NULL);
521 truncated = 0;
522 TAILQ_FOREACH(tab, &tabshead, tabs) {
523 if (truncated)
524 break;
525 if (toskip != 0) {
526 toskip--;
527 continue;
530 getyx(tabline, y, x);
531 if (x + sizeof(buf)+2 >= (size_t)COLS)
532 truncated = 1;
534 current = tab->flags & TAB_CURRENT;
536 if (*(title = tab->buffer.page.title) == '\0')
537 title = tab->hist_cur->h;
539 if (tab->flags & TAB_URGENT)
540 strlcpy(buf, "!", sizeof(buf));
541 else
542 strlcpy(buf, " ", sizeof(buf));
544 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
545 /* truncation happens */
546 strlcpy(&buf[sizeof(buf)-4], "...", 4);
547 } else {
548 /* pad with spaces */
549 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
550 /* nop */ ;
553 if (current)
554 wattr_on(tabline, tab_face.current, NULL);
555 else
556 wattr_on(tabline, tab_face.tab, NULL);
558 wprintw(tabline, "%s", buf);
559 if (TAILQ_NEXT(tab, tabs) != NULL)
560 wprintw(tabline, " ");
562 if (current)
563 wattr_off(tabline, tab_face.current, NULL);
564 else
565 wattr_off(tabline, tab_face.tab, NULL);
568 wattr_on(tabline, tab_face.background, NULL);
569 for (; x < (size_t)COLS; ++x)
570 waddch(tabline, ' ');
571 if (truncated)
572 mvwprintw(tabline, 0, COLS-1, ">");
573 wattr_off(tabline, tab_face.background, NULL);
576 /*
577 * Compute the first visible line around vl. Try to search forward
578 * until the end of the buffer; if a visible line is not found, search
579 * backward. Return NULL if no viable line was found.
580 */
581 static inline struct vline *
582 adjust_line(struct vline *vl, struct buffer *buffer)
584 struct vline *t;
586 if (!(vl->parent->flags & L_HIDDEN))
587 return vl;
589 /* search forward */
590 for (t = vl;
591 t != NULL && t->parent->flags & L_HIDDEN;
592 t = TAILQ_NEXT(t, vlines))
593 ; /* nop */
595 if (t != NULL)
596 return t;
598 /* search backward */
599 for (t = vl;
600 t != NULL && t->parent->flags & L_HIDDEN;
601 t = TAILQ_PREV(t, vhead, vlines))
602 ; /* nop */
604 return t;
607 static void
608 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
610 struct vline *vl;
611 int l, onscreen;
613 restore_curs_x(buffer);
615 /*
616 * TODO: ignoring buffer->force_update and always
617 * re-rendering. In theory we can recompute the y position
618 * without a re-render, and optimize here. It's not the only
619 * optimisation possible here, wscrl wolud also be an
620 * interesting one.
621 */
623 again:
624 werase(win);
625 buffer->curs_y = 0;
627 if (TAILQ_EMPTY(&buffer->head))
628 goto end;
630 buffer->top_line = adjust_line(buffer->top_line, buffer);
631 if (buffer->top_line == NULL)
632 goto end;
634 buffer->current_line = adjust_line(buffer->current_line, buffer);
636 l = 0;
637 onscreen = 0;
638 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
639 if (vl->parent->flags & L_HIDDEN)
640 continue;
642 wmove(win, l, 0);
643 print_vline(x_offset, width, win, vl);
645 if (vl == buffer->current_line)
646 onscreen = 1;
648 if (!onscreen)
649 buffer->curs_y++;
651 l++;
652 if (l == height)
653 break;
656 if (!onscreen) {
657 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
658 if (vl == buffer->current_line)
659 break;
660 if (vl->parent->flags & L_HIDDEN)
661 continue;
662 buffer->line_off++;
663 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
666 goto again;
669 buffer->last_line_off = buffer->line_off;
670 buffer->force_redraw = 0;
671 end:
672 wmove(win, buffer->curs_y, buffer->curs_x);
675 static void
676 redraw_help(void)
678 redraw_window(help, help_lines, help_cols, &helpwin);
681 static void
682 redraw_body(struct tab *tab)
684 static struct tab *last_tab;
686 if (last_tab != tab)
687 tab->buffer.force_redraw =1;
688 last_tab = tab;
690 redraw_window(body, body_lines, body_cols, &tab->buffer);
693 static inline char
694 trust_status_char(enum trust_state ts)
696 switch (ts) {
697 case TS_UNKNOWN: return 'u';
698 case TS_UNTRUSTED: return '!';
699 case TS_TEMP_TRUSTED: return '!';
700 case TS_TRUSTED: return 'v';
701 case TS_VERIFIED: return 'V';
702 default: return 'X';
706 static void
707 redraw_modeline(struct tab *tab)
709 double pct;
710 int x, y, max_x, max_y;
711 const char *mode = tab->buffer.page.name;
712 const char *spin = "-\\|/";
714 werase(modeline);
715 wattr_on(modeline, modeline_face.background, NULL);
716 wmove(modeline, 0, 0);
718 wprintw(modeline, "-%c%c %s ",
719 spin[tab->loading_anim_step],
720 trust_status_char(tab->trust),
721 mode == NULL ? "(none)" : mode);
723 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
725 if (tab->buffer.line_max <= (size_t)body_lines)
726 wprintw(modeline, "All ");
727 else if (tab->buffer.line_off == 0)
728 wprintw(modeline, "Top ");
729 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
730 wprintw(modeline, "Bottom ");
731 else
732 wprintw(modeline, "%.0f%% ", pct);
734 wprintw(modeline, "%d/%d %s ",
735 tab->buffer.line_off + tab->buffer.curs_y,
736 tab->buffer.line_max,
737 tab->hist_cur->h);
739 getyx(modeline, y, x);
740 getmaxyx(modeline, max_y, max_x);
742 (void)y;
743 (void)max_y;
745 for (; x < max_x; ++x)
746 waddstr(modeline, "-");
748 wattr_off(modeline, modeline_face.background, NULL);
751 static void
752 redraw_minibuffer(void)
754 wattr_on(echoarea, minibuffer_face.background, NULL);
755 werase(echoarea);
757 if (in_minibuffer)
758 do_redraw_minibuffer();
759 else
760 do_redraw_echoarea();
762 wattr_off(echoarea, minibuffer_face.background, NULL);
765 static void
766 do_redraw_echoarea(void)
768 struct tab *tab;
770 if (ministate.curmesg != NULL)
771 wprintw(echoarea, "%s", ministate.curmesg);
772 else if (*keybuf != '\0')
773 waddstr(echoarea, keybuf);
774 else {
775 /* If nothing else, show the URL at point */
776 tab = current_tab();
777 if (tab->buffer.current_line != NULL &&
778 tab->buffer.current_line->parent->type == LINE_LINK)
779 waddstr(echoarea, tab->buffer.current_line->parent->alt);
783 static void
784 do_redraw_minibuffer(void)
786 size_t off_y, off_x = 0;
787 const char *start, *c;
789 /* unused, set by getyx */
790 (void)off_y;
792 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
793 if (ministate.hist_cur != NULL)
794 wprintw(echoarea, "(%zu/%zu) ",
795 ministate.hist_off + 1,
796 ministate.history->len);
798 getyx(echoarea, off_y, off_x);
800 start = ministate.hist_cur != NULL
801 ? ministate.hist_cur->h
802 : ministate.buf;
803 c = utf8_nth(ministate.buffer.current_line->line,
804 ministate.buffer.cpoff);
805 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
806 start = utf8_next_cp(start);
809 waddstr(echoarea, start);
811 if (ministate.curmesg != NULL)
812 wprintw(echoarea, " [%s]", ministate.curmesg);
814 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
817 /*
818 * Place the cursor in the right ncurses window. If soft is 1, use
819 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
820 * wrefresh.
821 */
822 static void
823 place_cursor(int soft)
825 int (*touch)(WINDOW *);
827 if (soft)
828 touch = wnoutrefresh;
829 else
830 touch = wrefresh;
832 if (in_minibuffer) {
833 touch(body);
834 touch(echoarea);
835 } else {
836 touch(echoarea);
837 touch(body);
841 static void
842 redraw_tab(struct tab *tab)
844 if (side_window) {
845 redraw_help();
846 wnoutrefresh(help);
849 redraw_tabline();
850 redraw_body(tab);
851 redraw_modeline(tab);
852 redraw_minibuffer();
854 wnoutrefresh(tabline);
855 wnoutrefresh(modeline);
857 place_cursor(1);
859 doupdate();
862 static void
863 emit_help_item(char *prfx, void *fn)
865 struct line *l;
866 struct cmd *cmd;
868 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
869 if (fn == cmd->fn)
870 break;
872 assert(cmd != NULL);
874 if ((l = calloc(1, sizeof(*l))) == NULL)
875 abort();
877 l->type = LINE_TEXT;
878 l->alt = NULL;
880 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
882 if (TAILQ_EMPTY(&helpwin.page.head))
883 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
884 else
885 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
888 static void
889 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
891 struct keymap *k;
892 char p[32];
893 const char *kn;
895 TAILQ_FOREACH(k, &keymap->m, keymaps) {
896 strlcpy(p, prfx, sizeof(p));
897 if (*p != '\0')
898 strlcat(p, " ", sizeof(p));
899 if (k->meta)
900 strlcat(p, "M-", sizeof(p));
901 if ((kn = unkbd(k->key)) != NULL)
902 strlcat(p, kn, sizeof(p));
903 else
904 strlcat(p, keyname(k->key), sizeof(p));
906 if (k->fn == NULL)
907 rec_compute_help(&k->map, p, sizeof(p));
908 else
909 emit_help_item(p, k->fn);
913 static void
914 recompute_help(void)
916 char p[32] = { 0 };
918 empty_vlist(&helpwin);
919 empty_linelist(&helpwin);
920 rec_compute_help(current_map, p, sizeof(p));
921 wrap_page(&helpwin, help_cols);
924 void
925 vmessage(const char *fmt, va_list ap)
927 if (evtimer_pending(&clechoev, NULL))
928 evtimer_del(&clechoev);
930 free(ministate.curmesg);
931 ministate.curmesg = NULL;
933 if (fmt != NULL) {
934 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
935 evtimer_add(&clechoev, &clechoev_timer);
937 /* TODO: what to do if the allocation fails here? */
938 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
939 ministate.curmesg = NULL;
942 redraw_minibuffer();
943 place_cursor(0);
946 void
947 message(const char *fmt, ...)
949 va_list ap;
951 va_start(ap, fmt);
952 vmessage(fmt, ap);
953 va_end(ap);
956 void
957 start_loading_anim(struct tab *tab)
959 if (tab->loading_anim)
960 return;
961 tab->loading_anim = 1;
962 evtimer_set(&tab->loadingev, update_loading_anim, tab);
963 evtimer_add(&tab->loadingev, &loadingev_timer);
966 static void
967 update_loading_anim(int fd, short ev, void *d)
969 struct tab *tab = d;
971 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
973 if (tab->flags & TAB_CURRENT) {
974 redraw_modeline(tab);
975 wrefresh(modeline);
976 wrefresh(body);
977 if (in_minibuffer)
978 wrefresh(echoarea);
981 evtimer_add(&tab->loadingev, &loadingev_timer);
984 static void
985 stop_loading_anim(struct tab *tab)
987 if (!tab->loading_anim)
988 return;
989 evtimer_del(&tab->loadingev);
990 tab->loading_anim = 0;
991 tab->loading_anim_step = 0;
993 if (!(tab->flags & TAB_CURRENT))
994 return;
996 redraw_modeline(tab);
998 wrefresh(modeline);
999 wrefresh(body);
1000 if (in_minibuffer)
1001 wrefresh(echoarea);
1004 void
1005 load_url_in_tab(struct tab *tab, const char *url)
1007 message("Loading %s...", url);
1008 start_loading_anim(tab);
1009 load_url(tab, url);
1011 tab->buffer.curs_x = 0;
1012 tab->buffer.curs_y = 0;
1013 redraw_tab(tab);
1016 void
1017 switch_to_tab(struct tab *tab)
1019 struct tab *t;
1021 TAILQ_FOREACH(t, &tabshead, tabs) {
1022 t->flags &= ~TAB_CURRENT;
1025 tab->flags |= TAB_CURRENT;
1026 tab->flags &= ~TAB_URGENT;
1029 unsigned int
1030 tab_new_id(void)
1032 return tab_counter++;
1035 struct tab *
1036 new_tab(const char *url)
1038 struct tab *tab;
1040 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1041 event_loopbreak();
1042 return NULL;
1044 tab->fd = -1;
1046 TAILQ_INIT(&tab->hist.head);
1048 TAILQ_INIT(&tab->buffer.head);
1050 tab->id = tab_new_id();
1051 switch_to_tab(tab);
1053 if (TAILQ_EMPTY(&tabshead))
1054 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1055 else
1056 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1058 load_url_in_tab(tab, url);
1059 return tab;
1062 int
1063 ui_init()
1065 setlocale(LC_ALL, "");
1067 TAILQ_INIT(&eecmd_history.head);
1068 TAILQ_INIT(&ir_history.head);
1069 TAILQ_INIT(&lu_history.head);
1071 ministate.line.type = LINE_TEXT;
1072 ministate.vline.parent = &ministate.line;
1073 ministate.buffer.current_line = &ministate.vline;
1075 /* initialize help window */
1076 TAILQ_INIT(&helpwin.head);
1078 base_map = &global_map;
1079 current_map = &global_map;
1081 initscr();
1083 if (enable_colors) {
1084 if (has_colors()) {
1085 start_color();
1086 use_default_colors();
1087 } else
1088 enable_colors = 0;
1091 config_apply_style();
1093 raw();
1094 noecho();
1095 nonl();
1096 intrflush(stdscr, FALSE);
1098 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1099 return 0;
1100 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1101 return 0;
1102 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1103 return 0;
1104 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1105 return 0;
1106 if ((help = newwin(1, 1, 1, 0)) == NULL)
1107 return 0;
1109 body_lines = LINES-3;
1110 body_cols = COLS;
1112 wbkgd(body, body_face.body);
1113 wbkgd(echoarea, minibuffer_face.background);
1115 update_x_offset();
1117 keypad(body, TRUE);
1118 scrollok(body, FALSE);
1120 /* non-blocking input */
1121 wtimeout(body, 0);
1123 mvwprintw(body, 0, 0, "");
1125 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1126 event_add(&stdioev, NULL);
1128 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1129 signal_add(&winchev, NULL);
1131 return 1;
1134 void
1135 ui_on_tab_loaded(struct tab *tab)
1137 stop_loading_anim(tab);
1138 message("Loaded %s", tab->hist_cur->h);
1140 redraw_tabline();
1141 wrefresh(tabline);
1142 place_cursor(0);
1145 void
1146 ui_on_tab_refresh(struct tab *tab)
1148 wrap_page(&tab->buffer, body_cols);
1149 if (tab->flags & TAB_CURRENT)
1150 redraw_tab(tab);
1151 else
1152 tab->flags |= TAB_URGENT;
1155 const char *
1156 ui_keyname(int k)
1158 return keyname(k);
1161 void
1162 ui_toggle_side_window(void)
1164 side_window = !side_window;
1165 if (side_window)
1166 recompute_help();
1169 * ugly hack, but otherwise the window doesn't get updated
1170 * until I call handle_resize a second time (i.e. C-l). I
1171 * will be happy to know why something like this is needed.
1173 handle_resize_nodelay(0, 0, NULL);
1174 handle_resize_nodelay(0, 0, NULL);
1177 void
1178 ui_schedule_redraw(void)
1180 handle_resize_nodelay(0, 0, NULL);
1183 void
1184 ui_require_input(struct tab *tab, int hide)
1186 /* TODO: hard-switching to another tab is ugly */
1187 switch_to_tab(tab);
1189 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1190 &ir_history);
1191 strlcpy(ministate.prompt, "Input required: ",
1192 sizeof(ministate.prompt));
1193 redraw_tab(tab);
1196 void
1197 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1198 struct tab *data)
1200 yornp(prompt, fn, data);
1201 redraw_tab(current_tab());
1204 void
1205 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1206 unsigned int data)
1208 completing_read(prompt, fn, data);
1209 redraw_tab(current_tab());
1212 void
1213 ui_end(void)
1215 endwin();