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, *minibuffer;
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;
314 int lines;
316 endwin();
317 refresh();
318 clear();
320 lines = LINES;
322 /* move and resize the windows, in reverse order! */
324 mvwin(echoarea, --lines, 0);
325 wresize(echoarea, 1, COLS);
327 mvwin(modeline, --lines, 0);
328 wresize(modeline, 1, COLS);
330 body_lines = --lines;
331 body_cols = COLS;
333 if (side_window) {
334 help_cols = 0.3 * COLS;
335 help_lines = lines;
336 mvwin(help, 1, 0);
337 wresize(help, help_lines, help_cols);
339 wrap_page(&helpwin, help_cols);
341 body_cols = COLS - help_cols - 1;
342 mvwin(body, 1, help_cols);
343 } else
344 mvwin(body, 1, 0);
346 update_x_offset();
347 wresize(body, body_lines, body_cols);
349 wresize(tabline, 1, COLS);
351 tab = current_tab();
353 wrap_page(&tab->buffer, body_cols);
354 redraw_tab(tab);
357 static int
358 wrap_page(struct buffer *buffer, int width)
360 struct line *l;
361 const struct line *top_orig, *orig;
362 struct vline *vl;
363 int pre_width;
364 const char *prfx;
366 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
367 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
369 buffer->top_line = NULL;
370 buffer->current_line = NULL;
372 buffer->force_redraw = 1;
373 buffer->curs_y = 0;
374 buffer->line_off = 0;
376 empty_vlist(buffer);
378 TAILQ_FOREACH(l, &buffer->page.head, lines) {
379 prfx = line_prefixes[l->type].prfx1;
380 switch (l->type) {
381 case LINE_TEXT:
382 case LINE_LINK:
383 case LINE_TITLE_1:
384 case LINE_TITLE_2:
385 case LINE_TITLE_3:
386 case LINE_ITEM:
387 case LINE_QUOTE:
388 case LINE_PRE_START:
389 case LINE_PRE_END:
390 wrap_text(buffer, prfx, l, MIN(fill_column, width));
391 break;
392 case LINE_PRE_CONTENT:
393 if (olivetti_mode)
394 pre_width = MIN(fill_column, width);
395 else
396 pre_width = width;
397 hardwrap_text(buffer, l, pre_width);
398 break;
401 if (top_orig == l && buffer->top_line == NULL) {
402 buffer->line_off = buffer->line_max-1;
403 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
405 while (1) {
406 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
407 if (vl == NULL || vl->parent != orig)
408 break;
409 buffer->top_line = vl;
410 buffer->line_off--;
414 if (orig == l && buffer->current_line == NULL) {
415 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
417 while (1) {
418 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
419 if (vl == NULL || vl->parent != orig)
420 break;
421 buffer->current_line = vl;
426 if (buffer->current_line == NULL)
427 buffer->current_line = TAILQ_FIRST(&buffer->head);
429 if (buffer->top_line == NULL)
430 buffer->top_line = buffer->current_line;
432 return 1;
435 static void
436 print_vline(int off, int width, WINDOW *window, struct vline *vl)
438 const char *text;
439 const char *prfx;
440 struct line_face *f;
441 int i, left, x, y;
443 f = &line_faces[vl->parent->type];
445 /* unused, set by getyx */
446 (void)y;
448 if (!vl->flags)
449 prfx = line_prefixes[vl->parent->type].prfx1;
450 else
451 prfx = line_prefixes[vl->parent->type].prfx2;
453 text = vl->line;
454 if (text == NULL)
455 text = "";
457 wattr_on(window, body_face.left, NULL);
458 for (i = 0; i < off; i++)
459 waddch(window, ' ');
460 wattr_off(window, body_face.left, NULL);
462 wattr_on(window, f->prefix, NULL);
463 wprintw(window, "%s", prfx);
464 wattr_off(window, f->prefix, NULL);
466 wattr_on(window, f->text, NULL);
467 wprintw(window, "%s", text);
468 wattr_off(window, f->text, NULL);
470 getyx(window, y, x);
472 left = width - x;
474 wattr_on(window, f->trail, NULL);
475 for (i = 0; i < left - off; ++i)
476 waddch(window, ' ');
477 wattr_off(window, f->trail, NULL);
479 wattr_on(window, body_face.right, NULL);
480 for (i = 0; i < off; i++)
481 waddch(window, ' ');
482 wattr_off(window, body_face.right, NULL);
486 static void
487 redraw_tabline(void)
489 struct tab *tab;
490 size_t toskip, ots, tabwidth, space, x;
491 int current, y, truncated;
492 const char *title;
493 char buf[25];
495 x = 0;
497 /* unused, but setted by a getyx */
498 (void)y;
500 tabwidth = sizeof(buf)+1;
501 space = COLS-2;
503 toskip = 0;
504 TAILQ_FOREACH(tab, &tabshead, tabs) {
505 toskip++;
506 if (tab->flags & TAB_CURRENT)
507 break;
510 if (toskip * tabwidth < space)
511 toskip = 0;
512 else {
513 ots = toskip;
514 toskip--;
515 while (toskip != 0 &&
516 (ots - toskip+1) * tabwidth < space)
517 toskip--;
520 werase(tabline);
521 wattr_on(tabline, tab_face.background, NULL);
522 wprintw(tabline, toskip == 0 ? " " : "<");
523 wattr_off(tabline, tab_face.background, NULL);
525 truncated = 0;
526 TAILQ_FOREACH(tab, &tabshead, tabs) {
527 if (truncated)
528 break;
529 if (toskip != 0) {
530 toskip--;
531 continue;
534 getyx(tabline, y, x);
535 if (x + sizeof(buf)+2 >= (size_t)COLS)
536 truncated = 1;
538 current = tab->flags & TAB_CURRENT;
540 if (*(title = tab->buffer.page.title) == '\0')
541 title = tab->hist_cur->h;
543 if (tab->flags & TAB_URGENT)
544 strlcpy(buf, "!", sizeof(buf));
545 else
546 strlcpy(buf, " ", sizeof(buf));
548 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
549 /* truncation happens */
550 strlcpy(&buf[sizeof(buf)-4], "...", 4);
551 } else {
552 /* pad with spaces */
553 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
554 /* nop */ ;
557 if (current)
558 wattr_on(tabline, tab_face.current, NULL);
559 else
560 wattr_on(tabline, tab_face.tab, NULL);
562 wprintw(tabline, "%s", buf);
563 if (TAILQ_NEXT(tab, tabs) != NULL)
564 wprintw(tabline, " ");
566 if (current)
567 wattr_off(tabline, tab_face.current, NULL);
568 else
569 wattr_off(tabline, tab_face.tab, NULL);
572 wattr_on(tabline, tab_face.background, NULL);
573 for (; x < (size_t)COLS; ++x)
574 waddch(tabline, ' ');
575 if (truncated)
576 mvwprintw(tabline, 0, COLS-1, ">");
577 wattr_off(tabline, tab_face.background, NULL);
580 /*
581 * Compute the first visible line around vl. Try to search forward
582 * until the end of the buffer; if a visible line is not found, search
583 * backward. Return NULL if no viable line was found.
584 */
585 static inline struct vline *
586 adjust_line(struct vline *vl, struct buffer *buffer)
588 struct vline *t;
590 if (!(vl->parent->flags & L_HIDDEN))
591 return vl;
593 /* search forward */
594 for (t = vl;
595 t != NULL && t->parent->flags & L_HIDDEN;
596 t = TAILQ_NEXT(t, vlines))
597 ; /* nop */
599 if (t != NULL)
600 return t;
602 /* search backward */
603 for (t = vl;
604 t != NULL && t->parent->flags & L_HIDDEN;
605 t = TAILQ_PREV(t, vhead, vlines))
606 ; /* nop */
608 return t;
611 static void
612 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
614 struct vline *vl;
615 int l, onscreen;
617 restore_curs_x(buffer);
619 /*
620 * TODO: ignoring buffer->force_update and always
621 * re-rendering. In theory we can recompute the y position
622 * without a re-render, and optimize here. It's not the only
623 * optimisation possible here, wscrl wolud also be an
624 * interesting one.
625 */
627 again:
628 werase(win);
629 buffer->curs_y = 0;
631 if (TAILQ_EMPTY(&buffer->head))
632 goto end;
634 buffer->top_line = adjust_line(buffer->top_line, buffer);
635 if (buffer->top_line == NULL)
636 goto end;
638 buffer->current_line = adjust_line(buffer->current_line, buffer);
640 l = 0;
641 onscreen = 0;
642 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
643 if (vl->parent->flags & L_HIDDEN)
644 continue;
646 wmove(win, l, 0);
647 print_vline(x_offset, width, win, vl);
649 if (vl == buffer->current_line)
650 onscreen = 1;
652 if (!onscreen)
653 buffer->curs_y++;
655 l++;
656 if (l == height)
657 break;
660 if (!onscreen) {
661 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
662 if (vl == buffer->current_line)
663 break;
664 if (vl->parent->flags & L_HIDDEN)
665 continue;
666 buffer->line_off++;
667 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
670 goto again;
673 buffer->last_line_off = buffer->line_off;
674 buffer->force_redraw = 0;
675 end:
676 wmove(win, buffer->curs_y, buffer->curs_x);
679 static void
680 redraw_help(void)
682 redraw_window(help, help_lines, help_cols, &helpwin);
685 static void
686 redraw_body(struct tab *tab)
688 static struct tab *last_tab;
690 if (last_tab != tab)
691 tab->buffer.force_redraw =1;
692 last_tab = tab;
694 redraw_window(body, body_lines, body_cols, &tab->buffer);
697 static inline char
698 trust_status_char(enum trust_state ts)
700 switch (ts) {
701 case TS_UNKNOWN: return 'u';
702 case TS_UNTRUSTED: return '!';
703 case TS_TEMP_TRUSTED: return '!';
704 case TS_TRUSTED: return 'v';
705 case TS_VERIFIED: return 'V';
706 default: return 'X';
710 static void
711 redraw_modeline(struct tab *tab)
713 double pct;
714 int x, y, max_x, max_y;
715 const char *mode = tab->buffer.page.name;
716 const char *spin = "-\\|/";
718 werase(modeline);
719 wattr_on(modeline, modeline_face.background, NULL);
720 wmove(modeline, 0, 0);
722 wprintw(modeline, "-%c%c %s ",
723 spin[tab->loading_anim_step],
724 trust_status_char(tab->trust),
725 mode == NULL ? "(none)" : mode);
727 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
729 if (tab->buffer.line_max <= (size_t)body_lines)
730 wprintw(modeline, "All ");
731 else if (tab->buffer.line_off == 0)
732 wprintw(modeline, "Top ");
733 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
734 wprintw(modeline, "Bottom ");
735 else
736 wprintw(modeline, "%.0f%% ", pct);
738 wprintw(modeline, "%d/%d %s ",
739 tab->buffer.line_off + tab->buffer.curs_y,
740 tab->buffer.line_max,
741 tab->hist_cur->h);
743 getyx(modeline, y, x);
744 getmaxyx(modeline, max_y, max_x);
746 (void)y;
747 (void)max_y;
749 for (; x < max_x; ++x)
750 waddstr(modeline, "-");
752 wattr_off(modeline, modeline_face.background, NULL);
755 static void
756 redraw_minibuffer(void)
758 wattr_on(echoarea, minibuffer_face.background, NULL);
759 werase(echoarea);
761 if (in_minibuffer)
762 do_redraw_minibuffer();
763 else
764 do_redraw_echoarea();
766 wattr_off(echoarea, minibuffer_face.background, NULL);
769 static void
770 do_redraw_echoarea(void)
772 struct tab *tab;
774 if (ministate.curmesg != NULL)
775 wprintw(echoarea, "%s", ministate.curmesg);
776 else if (*keybuf != '\0')
777 waddstr(echoarea, keybuf);
778 else {
779 /* If nothing else, show the URL at point */
780 tab = current_tab();
781 if (tab->buffer.current_line != NULL &&
782 tab->buffer.current_line->parent->type == LINE_LINK)
783 waddstr(echoarea, tab->buffer.current_line->parent->alt);
787 static void
788 do_redraw_minibuffer(void)
790 size_t off_y, off_x = 0;
791 const char *start, *c;
793 /* unused, set by getyx */
794 (void)off_y;
796 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
797 if (ministate.hist_cur != NULL)
798 wprintw(echoarea, "(%zu/%zu) ",
799 ministate.hist_off + 1,
800 ministate.history->len);
802 getyx(echoarea, off_y, off_x);
804 start = ministate.hist_cur != NULL
805 ? ministate.hist_cur->h
806 : ministate.buf;
807 c = utf8_nth(ministate.buffer.current_line->line,
808 ministate.buffer.cpoff);
809 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
810 start = utf8_next_cp(start);
813 waddstr(echoarea, start);
815 if (ministate.curmesg != NULL)
816 wprintw(echoarea, " [%s]", ministate.curmesg);
818 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
821 /*
822 * Place the cursor in the right ncurses window. If soft is 1, use
823 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
824 * wrefresh.
825 */
826 static void
827 place_cursor(int soft)
829 int (*touch)(WINDOW *);
831 if (soft)
832 touch = wnoutrefresh;
833 else
834 touch = wrefresh;
836 if (in_minibuffer) {
837 touch(body);
838 touch(echoarea);
839 } else {
840 touch(echoarea);
841 touch(body);
845 static void
846 redraw_tab(struct tab *tab)
848 if (side_window) {
849 redraw_help();
850 wnoutrefresh(help);
853 redraw_tabline();
854 redraw_body(tab);
855 redraw_modeline(tab);
856 redraw_minibuffer();
858 wnoutrefresh(tabline);
859 wnoutrefresh(modeline);
861 place_cursor(1);
863 doupdate();
866 static void
867 emit_help_item(char *prfx, void *fn)
869 struct line *l;
870 struct cmd *cmd;
872 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
873 if (fn == cmd->fn)
874 break;
876 assert(cmd != NULL);
878 if ((l = calloc(1, sizeof(*l))) == NULL)
879 abort();
881 l->type = LINE_TEXT;
882 l->alt = NULL;
884 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
886 if (TAILQ_EMPTY(&helpwin.page.head))
887 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
888 else
889 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
892 static void
893 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
895 struct keymap *k;
896 char p[32];
897 const char *kn;
899 TAILQ_FOREACH(k, &keymap->m, keymaps) {
900 strlcpy(p, prfx, sizeof(p));
901 if (*p != '\0')
902 strlcat(p, " ", sizeof(p));
903 if (k->meta)
904 strlcat(p, "M-", sizeof(p));
905 if ((kn = unkbd(k->key)) != NULL)
906 strlcat(p, kn, sizeof(p));
907 else
908 strlcat(p, keyname(k->key), sizeof(p));
910 if (k->fn == NULL)
911 rec_compute_help(&k->map, p, sizeof(p));
912 else
913 emit_help_item(p, k->fn);
917 static void
918 recompute_help(void)
920 char p[32] = { 0 };
922 empty_vlist(&helpwin);
923 empty_linelist(&helpwin);
924 rec_compute_help(current_map, p, sizeof(p));
925 wrap_page(&helpwin, help_cols);
928 void
929 vmessage(const char *fmt, va_list ap)
931 if (evtimer_pending(&clechoev, NULL))
932 evtimer_del(&clechoev);
934 free(ministate.curmesg);
935 ministate.curmesg = NULL;
937 if (fmt != NULL) {
938 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
939 evtimer_add(&clechoev, &clechoev_timer);
941 /* TODO: what to do if the allocation fails here? */
942 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
943 ministate.curmesg = NULL;
946 redraw_minibuffer();
947 place_cursor(0);
950 void
951 message(const char *fmt, ...)
953 va_list ap;
955 va_start(ap, fmt);
956 vmessage(fmt, ap);
957 va_end(ap);
960 void
961 start_loading_anim(struct tab *tab)
963 if (tab->loading_anim)
964 return;
965 tab->loading_anim = 1;
966 evtimer_set(&tab->loadingev, update_loading_anim, tab);
967 evtimer_add(&tab->loadingev, &loadingev_timer);
970 static void
971 update_loading_anim(int fd, short ev, void *d)
973 struct tab *tab = d;
975 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
977 if (tab->flags & TAB_CURRENT) {
978 redraw_modeline(tab);
979 wrefresh(modeline);
980 wrefresh(body);
981 if (in_minibuffer)
982 wrefresh(echoarea);
985 evtimer_add(&tab->loadingev, &loadingev_timer);
988 static void
989 stop_loading_anim(struct tab *tab)
991 if (!tab->loading_anim)
992 return;
993 evtimer_del(&tab->loadingev);
994 tab->loading_anim = 0;
995 tab->loading_anim_step = 0;
997 if (!(tab->flags & TAB_CURRENT))
998 return;
1000 redraw_modeline(tab);
1002 wrefresh(modeline);
1003 wrefresh(body);
1004 if (in_minibuffer)
1005 wrefresh(echoarea);
1008 void
1009 load_url_in_tab(struct tab *tab, const char *url)
1011 message("Loading %s...", url);
1012 start_loading_anim(tab);
1013 load_url(tab, url);
1015 tab->buffer.curs_x = 0;
1016 tab->buffer.curs_y = 0;
1017 redraw_tab(tab);
1020 void
1021 switch_to_tab(struct tab *tab)
1023 struct tab *t;
1025 TAILQ_FOREACH(t, &tabshead, tabs) {
1026 t->flags &= ~TAB_CURRENT;
1029 tab->flags |= TAB_CURRENT;
1030 tab->flags &= ~TAB_URGENT;
1033 unsigned int
1034 tab_new_id(void)
1036 return tab_counter++;
1039 struct tab *
1040 new_tab(const char *url)
1042 struct tab *tab;
1044 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1045 event_loopbreak();
1046 return NULL;
1048 tab->fd = -1;
1050 TAILQ_INIT(&tab->hist.head);
1052 TAILQ_INIT(&tab->buffer.head);
1054 tab->id = tab_new_id();
1055 switch_to_tab(tab);
1057 if (TAILQ_EMPTY(&tabshead))
1058 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1059 else
1060 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1062 load_url_in_tab(tab, url);
1063 return tab;
1066 int
1067 ui_init()
1069 setlocale(LC_ALL, "");
1071 TAILQ_INIT(&eecmd_history.head);
1072 TAILQ_INIT(&ir_history.head);
1073 TAILQ_INIT(&lu_history.head);
1075 ministate.line.type = LINE_TEXT;
1076 ministate.vline.parent = &ministate.line;
1077 ministate.buffer.current_line = &ministate.vline;
1079 /* initialize help window */
1080 TAILQ_INIT(&helpwin.head);
1082 base_map = &global_map;
1083 current_map = &global_map;
1085 initscr();
1087 if (enable_colors) {
1088 if (has_colors()) {
1089 start_color();
1090 use_default_colors();
1091 } else
1092 enable_colors = 0;
1095 config_apply_style();
1097 raw();
1098 noecho();
1099 nonl();
1100 intrflush(stdscr, FALSE);
1102 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1103 return 0;
1104 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1105 return 0;
1106 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1107 return 0;
1108 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1109 return 0;
1110 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1111 return 0;
1112 if ((help = newwin(1, 1, 1, 0)) == NULL)
1113 return 0;
1115 body_lines = LINES-3;
1116 body_cols = COLS;
1118 wbkgd(body, body_face.body);
1119 wbkgd(echoarea, minibuffer_face.background);
1121 update_x_offset();
1123 keypad(body, TRUE);
1124 scrollok(body, FALSE);
1126 /* non-blocking input */
1127 wtimeout(body, 0);
1129 mvwprintw(body, 0, 0, "");
1131 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1132 event_add(&stdioev, NULL);
1134 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1135 signal_add(&winchev, NULL);
1137 return 1;
1140 void
1141 ui_on_tab_loaded(struct tab *tab)
1143 stop_loading_anim(tab);
1144 message("Loaded %s", tab->hist_cur->h);
1146 redraw_tabline();
1147 wrefresh(tabline);
1148 place_cursor(0);
1151 void
1152 ui_on_tab_refresh(struct tab *tab)
1154 wrap_page(&tab->buffer, body_cols);
1155 if (tab->flags & TAB_CURRENT)
1156 redraw_tab(tab);
1157 else
1158 tab->flags |= TAB_URGENT;
1161 const char *
1162 ui_keyname(int k)
1164 return keyname(k);
1167 void
1168 ui_toggle_side_window(void)
1170 side_window = !side_window;
1171 if (side_window)
1172 recompute_help();
1175 * ugly hack, but otherwise the window doesn't get updated
1176 * until I call handle_resize a second time (i.e. C-l). I
1177 * will be happy to know why something like this is needed.
1179 handle_resize_nodelay(0, 0, NULL);
1180 handle_resize_nodelay(0, 0, NULL);
1183 void
1184 ui_schedule_redraw(void)
1186 handle_resize_nodelay(0, 0, NULL);
1189 void
1190 ui_require_input(struct tab *tab, int hide)
1192 /* TODO: hard-switching to another tab is ugly */
1193 switch_to_tab(tab);
1195 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1196 &ir_history, NULL, NULL);
1197 strlcpy(ministate.prompt, "Input required: ",
1198 sizeof(ministate.prompt));
1199 redraw_tab(tab);
1202 void
1203 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1204 struct tab *data)
1206 yornp(prompt, fn, data);
1207 redraw_tab(current_tab());
1210 void
1211 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1212 struct tab *data)
1214 completing_read(prompt, fn, data, NULL, NULL);
1215 redraw_tab(current_tab());
1218 void
1219 ui_end(void)
1221 endwin();