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 x_offset;
79 struct thiskey thiskey;
81 static struct event resizeev;
82 static struct timeval resize_timer = { 0, 250000 };
84 static WINDOW *tabline, *body, *modeline, *echoarea;
86 int body_lines, body_cols;
88 static WINDOW *help;
89 static struct buffer helpwin;
90 static int help_lines, help_cols;
92 static int side_window;
94 static struct event clechoev;
95 static struct timeval clechoev_timer = { 5, 0 };
96 static struct timeval loadingev_timer = { 0, 250000 };
98 static uint32_t tab_counter;
100 static char keybuf[64];
102 struct kmap global_map,
103 minibuffer_map,
104 *current_map,
105 *base_map;
107 int in_minibuffer;
109 static inline void
110 update_x_offset(void)
112 if (olivetti_mode && fill_column < body_cols)
113 x_offset = (body_cols - fill_column)/2;
114 else
115 x_offset = 0;
118 void
119 save_excursion(struct excursion *place, struct buffer *buffer)
121 place->curs_x = buffer->curs_x;
122 place->curs_y = buffer->curs_y;
123 place->line_off = buffer->line_off;
124 place->current_line = buffer->current_line;
125 place->cpoff = buffer->cpoff;
128 void
129 restore_excursion(struct excursion *place, struct buffer *buffer)
131 buffer->curs_x = place->curs_x;
132 buffer->curs_y = place->curs_y;
133 buffer->line_off = place->line_off;
134 buffer->current_line = place->current_line;
135 buffer->cpoff = place->cpoff;
138 static void
139 restore_curs_x(struct buffer *buffer)
141 struct vline *vl;
142 const char *prfx;
144 vl = buffer->current_line;
145 if (vl == NULL || vl->line == NULL)
146 buffer->curs_x = buffer->cpoff = 0;
147 else
148 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
150 buffer->curs_x += x_offset;
152 if (vl != NULL) {
153 prfx = line_prefixes[vl->parent->type].prfx1;
154 buffer->curs_x += utf8_swidth(prfx);
158 void
159 global_key_unbound(void)
161 message("%s is undefined", keybuf);
164 static struct vline *
165 nth_line(struct buffer *buffer, size_t n)
167 struct vline *vl;
168 size_t i;
170 i = 0;
171 TAILQ_FOREACH(vl, &buffer->head, vlines) {
172 if (i == n)
173 return vl;
174 i++;
177 /* unreachable */
178 abort();
181 struct tab *
182 current_tab(void)
184 struct tab *t;
186 TAILQ_FOREACH(t, &tabshead, tabs) {
187 if (t->flags & TAB_CURRENT)
188 return t;
191 /* unreachable */
192 abort();
195 struct buffer *
196 current_buffer(void)
198 if (in_minibuffer)
199 return &ministate.buffer;
200 return &current_tab()->buffer;
203 static int
204 readkey(void)
206 uint32_t state = 0;
208 if ((thiskey.key = wgetch(body)) == ERR)
209 return 0;
211 thiskey.meta = thiskey.key == 27;
212 if (thiskey.meta) {
213 thiskey.key = wgetch(body);
214 if (thiskey.key == ERR || thiskey.key == 27) {
215 thiskey.meta = 0;
216 thiskey.key = 27;
220 thiskey.cp = 0;
221 if ((unsigned int)thiskey.key < UINT8_MAX) {
222 while (1) {
223 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
224 break;
225 if ((thiskey.key = wgetch(body)) == ERR) {
226 message("Error decoding user input");
227 return 0;
232 return 1;
235 static void
236 dispatch_stdio(int fd, short ev, void *d)
238 struct keymap *k;
239 const char *keyname;
240 char tmp[5] = {0};
242 if (!readkey())
243 return;
245 if (keybuf[0] != '\0')
246 strlcat(keybuf, " ", sizeof(keybuf));
247 if (thiskey.meta)
248 strlcat(keybuf, "M-", sizeof(keybuf));
249 if (thiskey.cp != 0) {
250 utf8_encode(thiskey.cp, tmp);
251 strlcat(keybuf, tmp, sizeof(keybuf));
252 } else {
253 if ((keyname = unkbd(thiskey.key)) != NULL)
254 strlcat(keybuf, keyname, sizeof(keybuf));
255 else {
256 tmp[0] = thiskey.key;
257 strlcat(keybuf, tmp, sizeof(keybuf));
261 TAILQ_FOREACH(k, &current_map->m, keymaps) {
262 if (k->meta == thiskey.meta &&
263 k->key == thiskey.key) {
264 if (k->fn == NULL)
265 current_map = &k->map;
266 else {
267 current_map = base_map;
268 strlcpy(keybuf, "", sizeof(keybuf));
269 k->fn(current_buffer());
271 goto done;
275 if (current_map->unhandled_input != NULL)
276 current_map->unhandled_input();
277 else
278 global_key_unbound();
280 strlcpy(keybuf, "", sizeof(keybuf));
281 current_map = base_map;
283 done:
284 if (side_window)
285 recompute_help();
287 redraw_tab(current_tab());
290 static void
291 handle_clear_echoarea(int fd, short ev, void *d)
293 free(ministate.curmesg);
294 ministate.curmesg = NULL;
296 redraw_minibuffer();
297 place_cursor(0);
300 static void
301 handle_resize(int sig, short ev, void *d)
303 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
304 event_del(&resizeev);
306 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
307 evtimer_add(&resizeev, &resize_timer);
310 static void
311 handle_resize_nodelay(int s, short ev, void *d)
313 struct tab *tab;
315 endwin();
316 refresh();
317 clear();
319 /* move and resize the windows, in reverse order! */
321 mvwin(echoarea, LINES-1, 0);
322 wresize(echoarea, 1, COLS);
324 mvwin(modeline, LINES-2, 0);
325 wresize(modeline, 1, COLS);
327 body_lines = LINES-3;
328 body_cols = COLS;
330 if (side_window) {
331 help_cols = 0.3 * COLS;
332 help_lines = LINES-3;
333 mvwin(help, 1, 0);
334 wresize(help, help_lines, help_cols);
336 wrap_page(&helpwin, help_cols);
338 body_cols = COLS - help_cols - 1;
339 mvwin(body, 1, help_cols);
340 } else
341 mvwin(body, 1, 0);
343 update_x_offset();
344 wresize(body, body_lines, body_cols);
346 wresize(tabline, 1, COLS);
348 tab = current_tab();
350 wrap_page(&tab->buffer, body_cols);
351 redraw_tab(tab);
354 static int
355 wrap_page(struct buffer *buffer, int width)
357 struct line *l;
358 const struct line *top_orig, *orig;
359 struct vline *vl;
360 int pre_width;
361 const char *prfx;
363 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
364 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
366 buffer->top_line = NULL;
367 buffer->current_line = NULL;
369 buffer->force_redraw = 1;
370 buffer->curs_y = 0;
371 buffer->line_off = 0;
373 empty_vlist(buffer);
375 TAILQ_FOREACH(l, &buffer->page.head, lines) {
376 prfx = line_prefixes[l->type].prfx1;
377 switch (l->type) {
378 case LINE_TEXT:
379 case LINE_LINK:
380 case LINE_TITLE_1:
381 case LINE_TITLE_2:
382 case LINE_TITLE_3:
383 case LINE_ITEM:
384 case LINE_QUOTE:
385 case LINE_PRE_START:
386 case LINE_PRE_END:
387 wrap_text(buffer, prfx, l, MIN(fill_column, width));
388 break;
389 case LINE_PRE_CONTENT:
390 if (olivetti_mode)
391 pre_width = MIN(fill_column, width);
392 else
393 pre_width = width;
394 hardwrap_text(buffer, l, pre_width);
395 break;
398 if (top_orig == l && buffer->top_line == NULL) {
399 buffer->line_off = buffer->line_max-1;
400 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
402 while (1) {
403 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
404 if (vl == NULL || vl->parent != orig)
405 break;
406 buffer->top_line = vl;
407 buffer->line_off--;
411 if (orig == l && buffer->current_line == NULL) {
412 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
414 while (1) {
415 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
416 if (vl == NULL || vl->parent != orig)
417 break;
418 buffer->current_line = vl;
423 if (buffer->current_line == NULL)
424 buffer->current_line = TAILQ_FIRST(&buffer->head);
426 if (buffer->top_line == NULL)
427 buffer->top_line = buffer->current_line;
429 return 1;
432 static void
433 print_vline(int off, int width, WINDOW *window, struct vline *vl)
435 const char *text;
436 const char *prfx;
437 struct line_face *f;
438 int i, left, x, y;
440 f = &line_faces[vl->parent->type];
442 /* unused, set by getyx */
443 (void)y;
445 if (!vl->flags)
446 prfx = line_prefixes[vl->parent->type].prfx1;
447 else
448 prfx = line_prefixes[vl->parent->type].prfx2;
450 text = vl->line;
451 if (text == NULL)
452 text = "";
454 wattr_on(window, body_face.left, NULL);
455 for (i = 0; i < off; i++)
456 waddch(window, ' ');
457 wattr_off(window, body_face.left, NULL);
459 wattr_on(window, f->prefix, NULL);
460 wprintw(window, "%s", prfx);
461 wattr_off(window, f->prefix, NULL);
463 wattr_on(window, f->text, NULL);
464 wprintw(window, "%s", text);
465 wattr_off(window, f->text, NULL);
467 getyx(window, y, x);
469 left = width - x;
471 wattr_on(window, f->trail, NULL);
472 for (i = 0; i < left - off; ++i)
473 waddch(window, ' ');
474 wattr_off(window, f->trail, NULL);
476 wattr_on(window, body_face.right, NULL);
477 for (i = 0; i < off; i++)
478 waddch(window, ' ');
479 wattr_off(window, body_face.right, NULL);
483 static void
484 redraw_tabline(void)
486 struct tab *tab;
487 size_t toskip, ots, tabwidth, space, x;
488 int current, y, truncated;
489 const char *title;
490 char buf[25];
492 x = 0;
494 /* unused, but setted by a getyx */
495 (void)y;
497 tabwidth = sizeof(buf)+1;
498 space = COLS-2;
500 toskip = 0;
501 TAILQ_FOREACH(tab, &tabshead, tabs) {
502 toskip++;
503 if (tab->flags & TAB_CURRENT)
504 break;
507 if (toskip * tabwidth < space)
508 toskip = 0;
509 else {
510 ots = toskip;
511 toskip--;
512 while (toskip != 0 &&
513 (ots - toskip+1) * tabwidth < space)
514 toskip--;
517 werase(tabline);
518 wattr_on(tabline, tab_face.background, NULL);
519 wprintw(tabline, toskip == 0 ? " " : "<");
520 wattr_off(tabline, tab_face.background, NULL);
522 truncated = 0;
523 TAILQ_FOREACH(tab, &tabshead, tabs) {
524 if (truncated)
525 break;
526 if (toskip != 0) {
527 toskip--;
528 continue;
531 getyx(tabline, y, x);
532 if (x + sizeof(buf)+2 >= (size_t)COLS)
533 truncated = 1;
535 current = tab->flags & TAB_CURRENT;
537 if (*(title = tab->buffer.page.title) == '\0')
538 title = tab->hist_cur->h;
540 if (tab->flags & TAB_URGENT)
541 strlcpy(buf, "!", sizeof(buf));
542 else
543 strlcpy(buf, " ", sizeof(buf));
545 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
546 /* truncation happens */
547 strlcpy(&buf[sizeof(buf)-4], "...", 4);
548 } else {
549 /* pad with spaces */
550 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
551 /* nop */ ;
554 if (current)
555 wattr_on(tabline, tab_face.current, NULL);
556 else
557 wattr_on(tabline, tab_face.tab, NULL);
559 wprintw(tabline, "%s", buf);
560 if (TAILQ_NEXT(tab, tabs) != NULL)
561 wprintw(tabline, " ");
563 if (current)
564 wattr_off(tabline, tab_face.current, NULL);
565 else
566 wattr_off(tabline, tab_face.tab, NULL);
569 wattr_on(tabline, tab_face.background, NULL);
570 for (; x < (size_t)COLS; ++x)
571 waddch(tabline, ' ');
572 if (truncated)
573 mvwprintw(tabline, 0, COLS-1, ">");
574 wattr_off(tabline, tab_face.background, NULL);
577 /*
578 * Compute the first visible line around vl. Try to search forward
579 * until the end of the buffer; if a visible line is not found, search
580 * backward. Return NULL if no viable line was found.
581 */
582 static inline struct vline *
583 adjust_line(struct vline *vl, struct buffer *buffer)
585 struct vline *t;
587 if (!(vl->parent->flags & L_HIDDEN))
588 return vl;
590 /* search forward */
591 for (t = vl;
592 t != NULL && t->parent->flags & L_HIDDEN;
593 t = TAILQ_NEXT(t, vlines))
594 ; /* nop */
596 if (t != NULL)
597 return t;
599 /* search backward */
600 for (t = vl;
601 t != NULL && t->parent->flags & L_HIDDEN;
602 t = TAILQ_PREV(t, vhead, vlines))
603 ; /* nop */
605 return t;
608 static void
609 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
611 struct vline *vl;
612 int l, onscreen;
614 restore_curs_x(buffer);
616 /*
617 * TODO: ignoring buffer->force_update and always
618 * re-rendering. In theory we can recompute the y position
619 * without a re-render, and optimize here. It's not the only
620 * optimisation possible here, wscrl wolud also be an
621 * interesting one.
622 */
624 again:
625 werase(win);
626 buffer->curs_y = 0;
628 if (TAILQ_EMPTY(&buffer->head))
629 goto end;
631 buffer->top_line = adjust_line(buffer->top_line, buffer);
632 if (buffer->top_line == NULL)
633 goto end;
635 buffer->current_line = adjust_line(buffer->current_line, buffer);
637 l = 0;
638 onscreen = 0;
639 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
640 if (vl->parent->flags & L_HIDDEN)
641 continue;
643 wmove(win, l, 0);
644 print_vline(x_offset, width, win, vl);
646 if (vl == buffer->current_line)
647 onscreen = 1;
649 if (!onscreen)
650 buffer->curs_y++;
652 l++;
653 if (l == height)
654 break;
657 if (!onscreen) {
658 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
659 if (vl == buffer->current_line)
660 break;
661 if (vl->parent->flags & L_HIDDEN)
662 continue;
663 buffer->line_off++;
664 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
667 goto again;
670 buffer->last_line_off = buffer->line_off;
671 buffer->force_redraw = 0;
672 end:
673 wmove(win, buffer->curs_y, buffer->curs_x);
676 static void
677 redraw_help(void)
679 redraw_window(help, help_lines, help_cols, &helpwin);
682 static void
683 redraw_body(struct tab *tab)
685 static struct tab *last_tab;
687 if (last_tab != tab)
688 tab->buffer.force_redraw =1;
689 last_tab = tab;
691 redraw_window(body, body_lines, body_cols, &tab->buffer);
694 static inline char
695 trust_status_char(enum trust_state ts)
697 switch (ts) {
698 case TS_UNKNOWN: return 'u';
699 case TS_UNTRUSTED: return '!';
700 case TS_TEMP_TRUSTED: return '!';
701 case TS_TRUSTED: return 'v';
702 case TS_VERIFIED: return 'V';
703 default: return 'X';
707 static void
708 redraw_modeline(struct tab *tab)
710 double pct;
711 int x, y, max_x, max_y;
712 const char *mode = tab->buffer.page.name;
713 const char *spin = "-\\|/";
715 werase(modeline);
716 wattr_on(modeline, modeline_face.background, NULL);
717 wmove(modeline, 0, 0);
719 wprintw(modeline, "-%c%c %s ",
720 spin[tab->loading_anim_step],
721 trust_status_char(tab->trust),
722 mode == NULL ? "(none)" : mode);
724 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
726 if (tab->buffer.line_max <= (size_t)body_lines)
727 wprintw(modeline, "All ");
728 else if (tab->buffer.line_off == 0)
729 wprintw(modeline, "Top ");
730 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
731 wprintw(modeline, "Bottom ");
732 else
733 wprintw(modeline, "%.0f%% ", pct);
735 wprintw(modeline, "%d/%d %s ",
736 tab->buffer.line_off + tab->buffer.curs_y,
737 tab->buffer.line_max,
738 tab->hist_cur->h);
740 getyx(modeline, y, x);
741 getmaxyx(modeline, max_y, max_x);
743 (void)y;
744 (void)max_y;
746 for (; x < max_x; ++x)
747 waddstr(modeline, "-");
749 wattr_off(modeline, modeline_face.background, NULL);
752 static void
753 redraw_minibuffer(void)
755 wattr_on(echoarea, minibuffer_face.background, NULL);
756 werase(echoarea);
758 if (in_minibuffer)
759 do_redraw_minibuffer();
760 else
761 do_redraw_echoarea();
763 wattr_off(echoarea, minibuffer_face.background, NULL);
766 static void
767 do_redraw_echoarea(void)
769 struct tab *tab;
771 if (ministate.curmesg != NULL)
772 wprintw(echoarea, "%s", ministate.curmesg);
773 else if (*keybuf != '\0')
774 waddstr(echoarea, keybuf);
775 else {
776 /* If nothing else, show the URL at point */
777 tab = current_tab();
778 if (tab->buffer.current_line != NULL &&
779 tab->buffer.current_line->parent->type == LINE_LINK)
780 waddstr(echoarea, tab->buffer.current_line->parent->alt);
784 static void
785 do_redraw_minibuffer(void)
787 size_t off_y, off_x = 0;
788 const char *start, *c;
790 /* unused, set by getyx */
791 (void)off_y;
793 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
794 if (ministate.hist_cur != NULL)
795 wprintw(echoarea, "(%zu/%zu) ",
796 ministate.hist_off + 1,
797 ministate.history->len);
799 getyx(echoarea, off_y, off_x);
801 start = ministate.hist_cur != NULL
802 ? ministate.hist_cur->h
803 : ministate.buf;
804 c = utf8_nth(ministate.buffer.current_line->line,
805 ministate.buffer.cpoff);
806 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
807 start = utf8_next_cp(start);
810 waddstr(echoarea, start);
812 if (ministate.curmesg != NULL)
813 wprintw(echoarea, " [%s]", ministate.curmesg);
815 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
818 /*
819 * Place the cursor in the right ncurses window. If soft is 1, use
820 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
821 * wrefresh.
822 */
823 static void
824 place_cursor(int soft)
826 int (*touch)(WINDOW *);
828 if (soft)
829 touch = wnoutrefresh;
830 else
831 touch = wrefresh;
833 if (in_minibuffer) {
834 touch(body);
835 touch(echoarea);
836 } else {
837 touch(echoarea);
838 touch(body);
842 static void
843 redraw_tab(struct tab *tab)
845 if (side_window) {
846 redraw_help();
847 wnoutrefresh(help);
850 redraw_tabline();
851 redraw_body(tab);
852 redraw_modeline(tab);
853 redraw_minibuffer();
855 wnoutrefresh(tabline);
856 wnoutrefresh(modeline);
858 place_cursor(1);
860 doupdate();
863 static void
864 emit_help_item(char *prfx, void *fn)
866 struct line *l;
867 struct cmd *cmd;
869 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
870 if (fn == cmd->fn)
871 break;
873 assert(cmd != NULL);
875 if ((l = calloc(1, sizeof(*l))) == NULL)
876 abort();
878 l->type = LINE_TEXT;
879 l->alt = NULL;
881 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
883 if (TAILQ_EMPTY(&helpwin.page.head))
884 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
885 else
886 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
889 static void
890 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
892 struct keymap *k;
893 char p[32];
894 const char *kn;
896 TAILQ_FOREACH(k, &keymap->m, keymaps) {
897 strlcpy(p, prfx, sizeof(p));
898 if (*p != '\0')
899 strlcat(p, " ", sizeof(p));
900 if (k->meta)
901 strlcat(p, "M-", sizeof(p));
902 if ((kn = unkbd(k->key)) != NULL)
903 strlcat(p, kn, sizeof(p));
904 else
905 strlcat(p, keyname(k->key), sizeof(p));
907 if (k->fn == NULL)
908 rec_compute_help(&k->map, p, sizeof(p));
909 else
910 emit_help_item(p, k->fn);
914 static void
915 recompute_help(void)
917 char p[32] = { 0 };
919 empty_vlist(&helpwin);
920 empty_linelist(&helpwin);
921 rec_compute_help(current_map, p, sizeof(p));
922 wrap_page(&helpwin, help_cols);
925 void
926 vmessage(const char *fmt, va_list ap)
928 if (evtimer_pending(&clechoev, NULL))
929 evtimer_del(&clechoev);
931 free(ministate.curmesg);
932 ministate.curmesg = NULL;
934 if (fmt != NULL) {
935 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
936 evtimer_add(&clechoev, &clechoev_timer);
938 /* TODO: what to do if the allocation fails here? */
939 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
940 ministate.curmesg = NULL;
943 redraw_minibuffer();
944 place_cursor(0);
947 void
948 message(const char *fmt, ...)
950 va_list ap;
952 va_start(ap, fmt);
953 vmessage(fmt, ap);
954 va_end(ap);
957 void
958 start_loading_anim(struct tab *tab)
960 if (tab->loading_anim)
961 return;
962 tab->loading_anim = 1;
963 evtimer_set(&tab->loadingev, update_loading_anim, tab);
964 evtimer_add(&tab->loadingev, &loadingev_timer);
967 static void
968 update_loading_anim(int fd, short ev, void *d)
970 struct tab *tab = d;
972 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
974 if (tab->flags & TAB_CURRENT) {
975 redraw_modeline(tab);
976 wrefresh(modeline);
977 wrefresh(body);
978 if (in_minibuffer)
979 wrefresh(echoarea);
982 evtimer_add(&tab->loadingev, &loadingev_timer);
985 static void
986 stop_loading_anim(struct tab *tab)
988 if (!tab->loading_anim)
989 return;
990 evtimer_del(&tab->loadingev);
991 tab->loading_anim = 0;
992 tab->loading_anim_step = 0;
994 if (!(tab->flags & TAB_CURRENT))
995 return;
997 redraw_modeline(tab);
999 wrefresh(modeline);
1000 wrefresh(body);
1001 if (in_minibuffer)
1002 wrefresh(echoarea);
1005 void
1006 load_url_in_tab(struct tab *tab, const char *url)
1008 message("Loading %s...", url);
1009 start_loading_anim(tab);
1010 load_url(tab, url);
1012 tab->buffer.curs_x = 0;
1013 tab->buffer.curs_y = 0;
1014 redraw_tab(tab);
1017 void
1018 switch_to_tab(struct tab *tab)
1020 struct tab *t;
1022 TAILQ_FOREACH(t, &tabshead, tabs) {
1023 t->flags &= ~TAB_CURRENT;
1026 tab->flags |= TAB_CURRENT;
1027 tab->flags &= ~TAB_URGENT;
1030 unsigned int
1031 tab_new_id(void)
1033 return tab_counter++;
1036 struct tab *
1037 new_tab(const char *url)
1039 struct tab *tab;
1041 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1042 event_loopbreak();
1043 return NULL;
1045 tab->fd = -1;
1047 TAILQ_INIT(&tab->hist.head);
1049 TAILQ_INIT(&tab->buffer.head);
1051 tab->id = tab_new_id();
1052 switch_to_tab(tab);
1054 if (TAILQ_EMPTY(&tabshead))
1055 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1056 else
1057 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1059 load_url_in_tab(tab, url);
1060 return tab;
1063 int
1064 ui_init()
1066 setlocale(LC_ALL, "");
1068 TAILQ_INIT(&eecmd_history.head);
1069 TAILQ_INIT(&ir_history.head);
1070 TAILQ_INIT(&lu_history.head);
1072 ministate.line.type = LINE_TEXT;
1073 ministate.vline.parent = &ministate.line;
1074 ministate.buffer.current_line = &ministate.vline;
1076 /* initialize help window */
1077 TAILQ_INIT(&helpwin.head);
1079 base_map = &global_map;
1080 current_map = &global_map;
1082 initscr();
1084 if (enable_colors) {
1085 if (has_colors()) {
1086 start_color();
1087 use_default_colors();
1088 } else
1089 enable_colors = 0;
1092 config_apply_style();
1094 raw();
1095 noecho();
1096 nonl();
1097 intrflush(stdscr, FALSE);
1099 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1100 return 0;
1101 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1102 return 0;
1103 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1104 return 0;
1105 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1106 return 0;
1107 if ((help = newwin(1, 1, 1, 0)) == NULL)
1108 return 0;
1110 body_lines = LINES-3;
1111 body_cols = COLS;
1113 wbkgd(body, body_face.body);
1114 wbkgd(echoarea, minibuffer_face.background);
1116 update_x_offset();
1118 keypad(body, TRUE);
1119 scrollok(body, FALSE);
1121 /* non-blocking input */
1122 wtimeout(body, 0);
1124 mvwprintw(body, 0, 0, "");
1126 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1127 event_add(&stdioev, NULL);
1129 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1130 signal_add(&winchev, NULL);
1132 return 1;
1135 void
1136 ui_on_tab_loaded(struct tab *tab)
1138 stop_loading_anim(tab);
1139 message("Loaded %s", tab->hist_cur->h);
1141 redraw_tabline();
1142 wrefresh(tabline);
1143 place_cursor(0);
1146 void
1147 ui_on_tab_refresh(struct tab *tab)
1149 wrap_page(&tab->buffer, body_cols);
1150 if (tab->flags & TAB_CURRENT)
1151 redraw_tab(tab);
1152 else
1153 tab->flags |= TAB_URGENT;
1156 const char *
1157 ui_keyname(int k)
1159 return keyname(k);
1162 void
1163 ui_toggle_side_window(void)
1165 side_window = !side_window;
1166 if (side_window)
1167 recompute_help();
1170 * ugly hack, but otherwise the window doesn't get updated
1171 * until I call handle_resize a second time (i.e. C-l). I
1172 * will be happy to know why something like this is needed.
1174 handle_resize_nodelay(0, 0, NULL);
1175 handle_resize_nodelay(0, 0, NULL);
1178 void
1179 ui_schedule_redraw(void)
1181 handle_resize_nodelay(0, 0, NULL);
1184 void
1185 ui_require_input(struct tab *tab, int hide)
1187 /* TODO: hard-switching to another tab is ugly */
1188 switch_to_tab(tab);
1190 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1191 &ir_history);
1192 strlcpy(ministate.prompt, "Input required: ",
1193 sizeof(ministate.prompt));
1194 redraw_tab(tab);
1197 void
1198 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1199 struct tab *data)
1201 yornp(prompt, fn, data);
1202 redraw_tab(current_tab());
1205 void
1206 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1207 struct tab *data)
1209 completing_read(prompt, fn, data);
1210 redraw_tab(current_tab());
1213 void
1214 ui_end(void)
1216 endwin();