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 do_redraw_minibuffer_compl(void);
70 static void place_cursor(int);
71 static void redraw_tab(struct tab*);
72 static void emit_help_item(char*, void*);
73 static void rec_compute_help(struct kmap*, char*, size_t);
74 static void recompute_help(void);
75 static void update_loading_anim(int, short, void*);
76 static void stop_loading_anim(struct tab*);
78 static int too_small;
79 static int x_offset;
81 struct thiskey thiskey;
83 static struct event resizeev;
84 static struct timeval resize_timer = { 0, 250000 };
86 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
88 int body_lines, body_cols;
90 static WINDOW *help;
91 static struct buffer helpwin;
92 static int help_lines, help_cols;
94 static int side_window;
96 static struct event clechoev;
97 static struct timeval clechoev_timer = { 5, 0 };
98 static struct timeval loadingev_timer = { 0, 250000 };
100 static uint32_t tab_counter;
102 static char keybuf[64];
104 struct kmap global_map,
105 minibuffer_map,
106 *current_map,
107 *base_map;
109 int in_minibuffer;
111 static inline void
112 update_x_offset(void)
114 if (olivetti_mode && fill_column < body_cols)
115 x_offset = (body_cols - fill_column)/2;
116 else
117 x_offset = 0;
120 void
121 save_excursion(struct excursion *place, struct buffer *buffer)
123 place->curs_x = buffer->curs_x;
124 place->curs_y = buffer->curs_y;
125 place->line_off = buffer->line_off;
126 place->current_line = buffer->current_line;
127 place->cpoff = buffer->cpoff;
130 void
131 restore_excursion(struct excursion *place, struct buffer *buffer)
133 buffer->curs_x = place->curs_x;
134 buffer->curs_y = place->curs_y;
135 buffer->line_off = place->line_off;
136 buffer->current_line = place->current_line;
137 buffer->cpoff = place->cpoff;
140 static void
141 restore_curs_x(struct buffer *buffer)
143 struct vline *vl;
144 const char *prfx;
146 vl = buffer->current_line;
147 if (vl == NULL || vl->line == NULL)
148 buffer->curs_x = buffer->cpoff = 0;
149 else
150 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
152 buffer->curs_x += x_offset;
154 if (vl != NULL) {
155 prfx = line_prefixes[vl->parent->type].prfx1;
156 buffer->curs_x += utf8_swidth(prfx);
160 void
161 global_key_unbound(void)
163 message("%s is undefined", keybuf);
166 static struct vline *
167 nth_line(struct buffer *buffer, size_t n)
169 struct vline *vl;
170 size_t i;
172 i = 0;
173 TAILQ_FOREACH(vl, &buffer->head, vlines) {
174 if (i == n)
175 return vl;
176 i++;
179 /* unreachable */
180 abort();
183 struct tab *
184 current_tab(void)
186 struct tab *t;
188 TAILQ_FOREACH(t, &tabshead, tabs) {
189 if (t->flags & TAB_CURRENT)
190 return t;
193 /* unreachable */
194 abort();
197 struct buffer *
198 current_buffer(void)
200 if (in_minibuffer)
201 return &ministate.buffer;
202 return &current_tab()->buffer;
205 static int
206 readkey(void)
208 uint32_t state = 0;
210 if ((thiskey.key = wgetch(body)) == ERR)
211 return 0;
213 thiskey.meta = thiskey.key == 27;
214 if (thiskey.meta) {
215 thiskey.key = wgetch(body);
216 if (thiskey.key == ERR || thiskey.key == 27) {
217 thiskey.meta = 0;
218 thiskey.key = 27;
222 thiskey.cp = 0;
223 if ((unsigned int)thiskey.key < UINT8_MAX) {
224 while (1) {
225 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
226 break;
227 if ((thiskey.key = wgetch(body)) == ERR) {
228 message("Error decoding user input");
229 return 0;
234 return 1;
237 static void
238 dispatch_stdio(int fd, short ev, void *d)
240 struct keymap *k;
241 const char *keyname;
242 char tmp[5] = {0};
244 /* TODO: schedule a redraw? */
245 if (too_small)
246 return;
248 if (!readkey())
249 return;
251 if (keybuf[0] != '\0')
252 strlcat(keybuf, " ", sizeof(keybuf));
253 if (thiskey.meta)
254 strlcat(keybuf, "M-", sizeof(keybuf));
255 if (thiskey.cp != 0) {
256 utf8_encode(thiskey.cp, tmp);
257 strlcat(keybuf, tmp, sizeof(keybuf));
258 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
259 strlcat(keybuf, keyname, sizeof(keybuf));
260 } else {
261 tmp[0] = thiskey.key;
262 strlcat(keybuf, tmp, sizeof(keybuf));
265 TAILQ_FOREACH(k, &current_map->m, keymaps) {
266 if (k->meta == thiskey.meta &&
267 k->key == thiskey.key) {
268 if (k->fn == NULL)
269 current_map = &k->map;
270 else {
271 current_map = base_map;
272 strlcpy(keybuf, "", sizeof(keybuf));
273 k->fn(current_buffer());
275 goto done;
279 if (current_map->unhandled_input != NULL)
280 current_map->unhandled_input();
281 else
282 global_key_unbound();
284 strlcpy(keybuf, "", sizeof(keybuf));
285 current_map = base_map;
287 done:
288 if (side_window)
289 recompute_help();
291 redraw_tab(current_tab());
294 static void
295 handle_clear_echoarea(int fd, short ev, void *d)
297 free(ministate.curmesg);
298 ministate.curmesg = NULL;
300 redraw_minibuffer();
301 place_cursor(0);
304 static void
305 handle_resize(int sig, short ev, void *d)
307 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
308 event_del(&resizeev);
310 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
311 evtimer_add(&resizeev, &resize_timer);
314 static void
315 handle_resize_nodelay(int s, short ev, void *d)
317 struct tab *tab;
318 int lines;
320 endwin();
321 refresh();
322 clear();
324 lines = LINES;
326 if ((too_small = lines < 15)) {
327 erase();
328 printw("Window too small.");
329 refresh();
330 return;
333 /* move and resize the windows, in reverse order! */
335 if (in_minibuffer == MB_COMPREAD) {
336 mvwin(minibuffer, lines-10, 0);
337 wresize(minibuffer, 10, COLS);
338 lines -= 10;
340 wrap_page(&ministate.compl.buffer, COLS);
343 mvwin(echoarea, --lines, 0);
344 wresize(echoarea, 1, COLS);
346 mvwin(modeline, --lines, 0);
347 wresize(modeline, 1, COLS);
349 body_lines = --lines;
350 body_cols = COLS;
352 if (side_window) {
353 help_cols = 0.3 * COLS;
354 help_lines = lines;
355 mvwin(help, 1, 0);
356 wresize(help, help_lines, help_cols);
358 wrap_page(&helpwin, help_cols);
360 body_cols = COLS - help_cols - 1;
361 mvwin(body, 1, help_cols);
362 } else
363 mvwin(body, 1, 0);
365 update_x_offset();
366 wresize(body, body_lines, body_cols);
368 wresize(tabline, 1, COLS);
370 tab = current_tab();
372 wrap_page(&tab->buffer, body_cols);
373 redraw_tab(tab);
376 static int
377 wrap_page(struct buffer *buffer, int width)
379 struct line *l;
380 const struct line *top_orig, *orig;
381 struct vline *vl;
382 int pre_width;
383 const char *prfx;
385 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
386 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
388 buffer->top_line = NULL;
389 buffer->current_line = NULL;
391 buffer->force_redraw = 1;
392 buffer->curs_y = 0;
393 buffer->line_off = 0;
395 empty_vlist(buffer);
397 TAILQ_FOREACH(l, &buffer->page.head, lines) {
398 prfx = line_prefixes[l->type].prfx1;
399 switch (l->type) {
400 case LINE_TEXT:
401 case LINE_LINK:
402 case LINE_TITLE_1:
403 case LINE_TITLE_2:
404 case LINE_TITLE_3:
405 case LINE_ITEM:
406 case LINE_QUOTE:
407 case LINE_PRE_START:
408 case LINE_PRE_END:
409 wrap_text(buffer, prfx, l, MIN(fill_column, width));
410 break;
411 case LINE_PRE_CONTENT:
412 if (olivetti_mode)
413 pre_width = MIN(fill_column, width);
414 else
415 pre_width = width;
416 hardwrap_text(buffer, l, pre_width);
417 break;
418 case LINE_COMPL:
419 case LINE_COMPL_CURRENT:
420 /*
421 * TODO: should be width, but will break the
422 * rendering. Fix when unlocking completions
423 * buffer from olivetti-mode.
424 */
425 wrap_one(buffer, prfx, l, MIN(fill_column, width));
428 if (top_orig == l && buffer->top_line == NULL) {
429 buffer->line_off = buffer->line_max-1;
430 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
432 while (1) {
433 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
434 if (vl == NULL || vl->parent != orig)
435 break;
436 buffer->top_line = vl;
437 buffer->line_off--;
441 if (orig == l && buffer->current_line == NULL) {
442 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
444 while (1) {
445 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
446 if (vl == NULL || vl->parent != orig)
447 break;
448 buffer->current_line = vl;
453 if (buffer->current_line == NULL)
454 buffer->current_line = TAILQ_FIRST(&buffer->head);
456 if (buffer->top_line == NULL)
457 buffer->top_line = buffer->current_line;
459 return 1;
462 static void
463 print_vline(int off, int width, WINDOW *window, struct vline *vl)
465 const char *text;
466 const char *prfx;
467 struct line_face *f;
468 int i, left, x, y;
470 f = &line_faces[vl->parent->type];
472 /* unused, set by getyx */
473 (void)y;
475 if (!vl->flags)
476 prfx = line_prefixes[vl->parent->type].prfx1;
477 else
478 prfx = line_prefixes[vl->parent->type].prfx2;
480 text = vl->line;
481 if (text == NULL)
482 text = "";
484 wattr_on(window, body_face.left, NULL);
485 for (i = 0; i < off; i++)
486 waddch(window, ' ');
487 wattr_off(window, body_face.left, NULL);
489 wattr_on(window, f->prefix, NULL);
490 wprintw(window, "%s", prfx);
491 wattr_off(window, f->prefix, NULL);
493 wattr_on(window, f->text, NULL);
494 wprintw(window, "%s", text);
495 wattr_off(window, f->text, NULL);
497 getyx(window, y, x);
499 left = width - x;
501 wattr_on(window, f->trail, NULL);
502 for (i = 0; i < left - off; ++i)
503 waddch(window, ' ');
504 wattr_off(window, f->trail, NULL);
506 wattr_on(window, body_face.right, NULL);
507 for (i = 0; i < off; i++)
508 waddch(window, ' ');
509 wattr_off(window, body_face.right, NULL);
513 static void
514 redraw_tabline(void)
516 struct tab *tab;
517 size_t toskip, ots, tabwidth, space, x;
518 int current, y, truncated;
519 const char *title;
520 char buf[25];
522 x = 0;
524 /* unused, but setted by a getyx */
525 (void)y;
527 tabwidth = sizeof(buf)+1;
528 space = COLS-2;
530 toskip = 0;
531 TAILQ_FOREACH(tab, &tabshead, tabs) {
532 toskip++;
533 if (tab->flags & TAB_CURRENT)
534 break;
537 if (toskip * tabwidth < space)
538 toskip = 0;
539 else {
540 ots = toskip;
541 toskip--;
542 while (toskip != 0 &&
543 (ots - toskip+1) * tabwidth < space)
544 toskip--;
547 werase(tabline);
548 wattr_on(tabline, tab_face.background, NULL);
549 wprintw(tabline, toskip == 0 ? " " : "<");
550 wattr_off(tabline, tab_face.background, NULL);
552 truncated = 0;
553 TAILQ_FOREACH(tab, &tabshead, tabs) {
554 if (truncated)
555 break;
556 if (toskip != 0) {
557 toskip--;
558 continue;
561 getyx(tabline, y, x);
562 if (x + sizeof(buf)+2 >= (size_t)COLS)
563 truncated = 1;
565 current = tab->flags & TAB_CURRENT;
567 if (*(title = tab->buffer.page.title) == '\0')
568 title = tab->hist_cur->h;
570 if (tab->flags & TAB_URGENT)
571 strlcpy(buf, "!", sizeof(buf));
572 else
573 strlcpy(buf, " ", sizeof(buf));
575 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
576 /* truncation happens */
577 strlcpy(&buf[sizeof(buf)-4], "...", 4);
578 } else {
579 /* pad with spaces */
580 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
581 /* nop */ ;
584 if (current)
585 wattr_on(tabline, tab_face.current, NULL);
586 else
587 wattr_on(tabline, tab_face.tab, NULL);
589 wprintw(tabline, "%s", buf);
590 if (TAILQ_NEXT(tab, tabs) != NULL)
591 wprintw(tabline, " ");
593 if (current)
594 wattr_off(tabline, tab_face.current, NULL);
595 else
596 wattr_off(tabline, tab_face.tab, NULL);
599 wattr_on(tabline, tab_face.background, NULL);
600 for (; x < (size_t)COLS; ++x)
601 waddch(tabline, ' ');
602 if (truncated)
603 mvwprintw(tabline, 0, COLS-1, ">");
604 wattr_off(tabline, tab_face.background, NULL);
607 /*
608 * Compute the first visible line around vl. Try to search forward
609 * until the end of the buffer; if a visible line is not found, search
610 * backward. Return NULL if no viable line was found.
611 */
612 struct vline *
613 adjust_line(struct vline *vl, struct buffer *buffer)
615 struct vline *t;
617 if (vl == NULL)
618 return NULL;
620 if (!(vl->parent->flags & L_HIDDEN))
621 return vl;
623 /* search forward */
624 for (t = vl;
625 t != NULL && t->parent->flags & L_HIDDEN;
626 t = TAILQ_NEXT(t, vlines))
627 ; /* nop */
629 if (t != NULL)
630 return t;
632 /* search backward */
633 for (t = vl;
634 t != NULL && t->parent->flags & L_HIDDEN;
635 t = TAILQ_PREV(t, vhead, vlines))
636 ; /* nop */
638 return t;
641 static void
642 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
644 struct vline *vl;
645 int l, onscreen;
647 restore_curs_x(buffer);
649 /*
650 * TODO: ignoring buffer->force_update and always
651 * re-rendering. In theory we can recompute the y position
652 * without a re-render, and optimize here. It's not the only
653 * optimisation possible here, wscrl wolud also be an
654 * interesting one.
655 */
657 again:
658 werase(win);
659 buffer->curs_y = 0;
661 if (TAILQ_EMPTY(&buffer->head))
662 goto end;
664 if (buffer->top_line == NULL)
665 buffer->top_line = TAILQ_FIRST(&buffer->head);
667 buffer->top_line = adjust_line(buffer->top_line, buffer);
668 if (buffer->top_line == NULL)
669 goto end;
671 buffer->current_line = adjust_line(buffer->current_line, buffer);
673 l = 0;
674 onscreen = 0;
675 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
676 if (vl->parent->flags & L_HIDDEN)
677 continue;
679 wmove(win, l, 0);
680 print_vline(x_offset, width, win, vl);
682 if (vl == buffer->current_line)
683 onscreen = 1;
685 if (!onscreen)
686 buffer->curs_y++;
688 l++;
689 if (l == height)
690 break;
693 if (!onscreen) {
694 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
695 if (vl == buffer->current_line)
696 break;
697 if (vl->parent->flags & L_HIDDEN)
698 continue;
699 buffer->line_off++;
700 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
703 if (vl != NULL)
704 goto again;
707 buffer->last_line_off = buffer->line_off;
708 buffer->force_redraw = 0;
709 end:
710 wmove(win, buffer->curs_y, buffer->curs_x);
713 static void
714 redraw_help(void)
716 redraw_window(help, help_lines, help_cols, &helpwin);
719 static void
720 redraw_body(struct tab *tab)
722 static struct tab *last_tab;
724 if (last_tab != tab)
725 tab->buffer.force_redraw =1;
726 last_tab = tab;
728 redraw_window(body, body_lines, body_cols, &tab->buffer);
731 static inline char
732 trust_status_char(enum trust_state ts)
734 switch (ts) {
735 case TS_UNKNOWN: return 'u';
736 case TS_UNTRUSTED: return '!';
737 case TS_TEMP_TRUSTED: return '!';
738 case TS_TRUSTED: return 'v';
739 case TS_VERIFIED: return 'V';
740 default: return 'X';
744 static void
745 redraw_modeline(struct tab *tab)
747 double pct;
748 int x, y, max_x, max_y;
749 const char *mode = tab->buffer.page.name;
750 const char *spin = "-\\|/";
752 werase(modeline);
753 wattr_on(modeline, modeline_face.background, NULL);
754 wmove(modeline, 0, 0);
756 wprintw(modeline, "-%c%c %s ",
757 spin[tab->loading_anim_step],
758 trust_status_char(tab->trust),
759 mode == NULL ? "(none)" : mode);
761 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
763 if (tab->buffer.line_max <= (size_t)body_lines)
764 wprintw(modeline, "All ");
765 else if (tab->buffer.line_off == 0)
766 wprintw(modeline, "Top ");
767 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
768 wprintw(modeline, "Bottom ");
769 else
770 wprintw(modeline, "%.0f%% ", pct);
772 wprintw(modeline, "%d/%d %s ",
773 tab->buffer.line_off + tab->buffer.curs_y,
774 tab->buffer.line_max,
775 tab->hist_cur->h);
777 getyx(modeline, y, x);
778 getmaxyx(modeline, max_y, max_x);
780 (void)y;
781 (void)max_y;
783 for (; x < max_x; ++x)
784 waddstr(modeline, "-");
786 wattr_off(modeline, modeline_face.background, NULL);
789 static void
790 redraw_minibuffer(void)
792 wattr_on(echoarea, minibuffer_face.background, NULL);
793 werase(echoarea);
795 if (in_minibuffer)
796 do_redraw_minibuffer();
797 else
798 do_redraw_echoarea();
800 if (in_minibuffer == MB_COMPREAD)
801 do_redraw_minibuffer_compl();
803 wattr_off(echoarea, minibuffer_face.background, NULL);
806 static void
807 do_redraw_echoarea(void)
809 struct tab *tab;
811 if (ministate.curmesg != NULL)
812 wprintw(echoarea, "%s", ministate.curmesg);
813 else if (*keybuf != '\0')
814 waddstr(echoarea, keybuf);
815 else {
816 /* If nothing else, show the URL at point */
817 tab = current_tab();
818 if (tab->buffer.current_line != NULL &&
819 tab->buffer.current_line->parent->type == LINE_LINK)
820 waddstr(echoarea, tab->buffer.current_line->parent->alt);
824 static void
825 do_redraw_minibuffer(void)
827 size_t off_y, off_x = 0;
828 const char *start, *c;
830 /* unused, set by getyx */
831 (void)off_y;
833 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
834 if (ministate.hist_cur != NULL)
835 wprintw(echoarea, "(%zu/%zu) ",
836 ministate.hist_off + 1,
837 ministate.history->len);
839 getyx(echoarea, off_y, off_x);
841 start = ministate.hist_cur != NULL
842 ? ministate.hist_cur->h
843 : ministate.buf;
844 c = utf8_nth(ministate.buffer.current_line->line,
845 ministate.buffer.cpoff);
846 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
847 start = utf8_next_cp(start);
850 waddstr(echoarea, start);
852 if (ministate.curmesg != NULL)
853 wprintw(echoarea, " [%s]", ministate.curmesg);
855 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
858 static void
859 do_redraw_minibuffer_compl(void)
861 redraw_window(minibuffer, 10, body_cols,
862 &ministate.compl.buffer);
865 /*
866 * Place the cursor in the right ncurses window. If soft is 1, use
867 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
868 * wrefresh.
869 */
870 static void
871 place_cursor(int soft)
873 int (*touch)(WINDOW *);
875 if (soft)
876 touch = wnoutrefresh;
877 else
878 touch = wrefresh;
880 if (in_minibuffer) {
881 touch(body);
882 touch(echoarea);
883 } else {
884 touch(echoarea);
885 touch(body);
889 static void
890 redraw_tab(struct tab *tab)
892 if (too_small)
893 return;
895 if (side_window) {
896 redraw_help();
897 wnoutrefresh(help);
900 redraw_tabline();
901 redraw_body(tab);
902 redraw_modeline(tab);
903 redraw_minibuffer();
905 wnoutrefresh(tabline);
906 wnoutrefresh(modeline);
908 if (in_minibuffer == MB_COMPREAD)
909 wnoutrefresh(minibuffer);
911 place_cursor(1);
913 doupdate();
916 static void
917 emit_help_item(char *prfx, void *fn)
919 struct line *l;
920 struct cmd *cmd;
922 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
923 if (fn == cmd->fn)
924 break;
926 assert(cmd != NULL);
928 if ((l = calloc(1, sizeof(*l))) == NULL)
929 abort();
931 l->type = LINE_TEXT;
932 l->alt = NULL;
934 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
936 if (TAILQ_EMPTY(&helpwin.page.head))
937 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
938 else
939 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
942 static void
943 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
945 struct keymap *k;
946 char p[32];
947 const char *kn;
949 TAILQ_FOREACH(k, &keymap->m, keymaps) {
950 strlcpy(p, prfx, sizeof(p));
951 if (*p != '\0')
952 strlcat(p, " ", sizeof(p));
953 if (k->meta)
954 strlcat(p, "M-", sizeof(p));
955 if ((kn = unkbd(k->key)) != NULL)
956 strlcat(p, kn, sizeof(p));
957 else
958 strlcat(p, keyname(k->key), sizeof(p));
960 if (k->fn == NULL)
961 rec_compute_help(&k->map, p, sizeof(p));
962 else
963 emit_help_item(p, k->fn);
967 static void
968 recompute_help(void)
970 char p[32] = { 0 };
972 empty_vlist(&helpwin);
973 empty_linelist(&helpwin);
974 rec_compute_help(current_map, p, sizeof(p));
975 wrap_page(&helpwin, help_cols);
978 void
979 vmessage(const char *fmt, va_list ap)
981 if (evtimer_pending(&clechoev, NULL))
982 evtimer_del(&clechoev);
984 free(ministate.curmesg);
985 ministate.curmesg = NULL;
987 if (fmt != NULL) {
988 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
989 evtimer_add(&clechoev, &clechoev_timer);
991 /* TODO: what to do if the allocation fails here? */
992 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
993 ministate.curmesg = NULL;
996 redraw_minibuffer();
997 place_cursor(0);
1000 void
1001 message(const char *fmt, ...)
1003 va_list ap;
1005 va_start(ap, fmt);
1006 vmessage(fmt, ap);
1007 va_end(ap);
1010 void
1011 start_loading_anim(struct tab *tab)
1013 if (tab->loading_anim)
1014 return;
1015 tab->loading_anim = 1;
1016 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1017 evtimer_add(&tab->loadingev, &loadingev_timer);
1020 static void
1021 update_loading_anim(int fd, short ev, void *d)
1023 struct tab *tab = d;
1025 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1027 if (tab->flags & TAB_CURRENT) {
1028 redraw_modeline(tab);
1029 wrefresh(modeline);
1030 wrefresh(body);
1031 if (in_minibuffer)
1032 wrefresh(echoarea);
1035 evtimer_add(&tab->loadingev, &loadingev_timer);
1038 static void
1039 stop_loading_anim(struct tab *tab)
1041 if (!tab->loading_anim)
1042 return;
1043 evtimer_del(&tab->loadingev);
1044 tab->loading_anim = 0;
1045 tab->loading_anim_step = 0;
1047 if (!(tab->flags & TAB_CURRENT))
1048 return;
1050 redraw_modeline(tab);
1052 wrefresh(modeline);
1053 wrefresh(body);
1054 if (in_minibuffer)
1055 wrefresh(echoarea);
1058 void
1059 load_url_in_tab(struct tab *tab, const char *url)
1061 message("Loading %s...", url);
1062 start_loading_anim(tab);
1063 load_url(tab, url);
1065 tab->buffer.curs_x = 0;
1066 tab->buffer.curs_y = 0;
1067 redraw_tab(tab);
1070 void
1071 switch_to_tab(struct tab *tab)
1073 struct tab *t;
1075 TAILQ_FOREACH(t, &tabshead, tabs) {
1076 t->flags &= ~TAB_CURRENT;
1079 tab->flags |= TAB_CURRENT;
1080 tab->flags &= ~TAB_URGENT;
1083 unsigned int
1084 tab_new_id(void)
1086 return tab_counter++;
1089 struct tab *
1090 new_tab(const char *url)
1092 struct tab *tab;
1094 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1095 event_loopbreak();
1096 return NULL;
1098 tab->fd = -1;
1100 TAILQ_INIT(&tab->hist.head);
1102 TAILQ_INIT(&tab->buffer.head);
1104 tab->id = tab_new_id();
1105 switch_to_tab(tab);
1107 if (TAILQ_EMPTY(&tabshead))
1108 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1109 else
1110 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1112 load_url_in_tab(tab, url);
1113 return tab;
1116 int
1117 ui_init()
1119 setlocale(LC_ALL, "");
1121 TAILQ_INIT(&eecmd_history.head);
1122 TAILQ_INIT(&ir_history.head);
1123 TAILQ_INIT(&lu_history.head);
1125 ministate.line.type = LINE_TEXT;
1126 ministate.vline.parent = &ministate.line;
1127 ministate.buffer.current_line = &ministate.vline;
1129 /* initialize help window */
1130 TAILQ_INIT(&helpwin.head);
1132 base_map = &global_map;
1133 current_map = &global_map;
1135 initscr();
1137 if (enable_colors) {
1138 if (has_colors()) {
1139 start_color();
1140 use_default_colors();
1141 } else
1142 enable_colors = 0;
1145 config_apply_style();
1147 raw();
1148 noecho();
1149 nonl();
1150 intrflush(stdscr, FALSE);
1152 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1153 return 0;
1154 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1155 return 0;
1156 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1157 return 0;
1158 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1159 return 0;
1160 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1161 return 0;
1162 if ((help = newwin(1, 1, 1, 0)) == NULL)
1163 return 0;
1165 body_lines = LINES-3;
1166 body_cols = COLS;
1168 wbkgd(body, body_face.body);
1169 wbkgd(echoarea, minibuffer_face.background);
1171 update_x_offset();
1173 keypad(body, TRUE);
1174 scrollok(body, FALSE);
1176 /* non-blocking input */
1177 wtimeout(body, 0);
1179 mvwprintw(body, 0, 0, "");
1181 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1182 event_add(&stdioev, NULL);
1184 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1185 signal_add(&winchev, NULL);
1187 return 1;
1190 void
1191 ui_on_tab_loaded(struct tab *tab)
1193 stop_loading_anim(tab);
1194 message("Loaded %s", tab->hist_cur->h);
1196 redraw_tabline();
1197 wrefresh(tabline);
1198 place_cursor(0);
1201 void
1202 ui_on_tab_refresh(struct tab *tab)
1204 wrap_page(&tab->buffer, body_cols);
1205 if (tab->flags & TAB_CURRENT)
1206 redraw_tab(tab);
1207 else
1208 tab->flags |= TAB_URGENT;
1211 const char *
1212 ui_keyname(int k)
1214 return keyname(k);
1217 void
1218 ui_toggle_side_window(void)
1220 side_window = !side_window;
1221 if (side_window)
1222 recompute_help();
1225 * ugly hack, but otherwise the window doesn't get updated
1226 * until I call handle_resize a second time (i.e. C-l). I
1227 * will be happy to know why something like this is needed.
1229 handle_resize_nodelay(0, 0, NULL);
1230 handle_resize_nodelay(0, 0, NULL);
1233 void
1234 ui_schedule_redraw(void)
1236 struct timeval tv = {0, 0};
1238 if (event_pending(&resizeev, EV_TIMEOUT, NULL))
1239 event_del(&resizeev);
1241 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1242 evtimer_add(&resizeev, &tv);
1245 void
1246 ui_require_input(struct tab *tab, int hide)
1248 /* TODO: hard-switching to another tab is ugly */
1249 switch_to_tab(tab);
1251 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1252 &ir_history, NULL, NULL);
1253 strlcpy(ministate.prompt, "Input required: ",
1254 sizeof(ministate.prompt));
1255 redraw_tab(tab);
1258 void
1259 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1260 struct tab *data)
1262 yornp(prompt, fn, data);
1263 redraw_tab(current_tab());
1266 void
1267 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1268 struct tab *data)
1270 completing_read(prompt, fn, data);
1271 redraw_tab(current_tab());
1274 void
1275 ui_end(void)
1277 endwin();