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
762 / tab->buffer.line_max;
764 if (tab->buffer.line_max <= (size_t)body_lines)
765 wprintw(modeline, "All ");
766 else if (tab->buffer.line_off == 0)
767 wprintw(modeline, "Top ");
768 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
769 wprintw(modeline, "Bottom ");
770 else
771 wprintw(modeline, "%.0f%% ", pct);
773 wprintw(modeline, "%d/%d %s ",
774 tab->buffer.line_off + tab->buffer.curs_y,
775 tab->buffer.line_max,
776 tab->hist_cur->h);
778 getyx(modeline, y, x);
779 getmaxyx(modeline, max_y, max_x);
781 (void)y;
782 (void)max_y;
784 for (; x < max_x; ++x)
785 waddstr(modeline, "-");
787 wattr_off(modeline, modeline_face.background, NULL);
790 static void
791 redraw_minibuffer(void)
793 wattr_on(echoarea, minibuffer_face.background, NULL);
794 werase(echoarea);
796 if (in_minibuffer)
797 do_redraw_minibuffer();
798 else
799 do_redraw_echoarea();
801 if (in_minibuffer == MB_COMPREAD)
802 do_redraw_minibuffer_compl();
804 wattr_off(echoarea, minibuffer_face.background, NULL);
807 static void
808 do_redraw_echoarea(void)
810 struct tab *tab;
812 if (ministate.curmesg != NULL)
813 wprintw(echoarea, "%s", ministate.curmesg);
814 else if (*keybuf != '\0')
815 waddstr(echoarea, keybuf);
816 else {
817 /* If nothing else, show the URL at point */
818 tab = current_tab();
819 if (tab->buffer.current_line != NULL &&
820 tab->buffer.current_line->parent->type == LINE_LINK)
821 waddstr(echoarea, tab->buffer.current_line->parent->alt);
825 static void
826 do_redraw_minibuffer(void)
828 size_t off_y, off_x = 0;
829 const char *start, *c;
831 /* unused, set by getyx */
832 (void)off_y;
834 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
835 if (ministate.hist_cur != NULL)
836 wprintw(echoarea, "(%zu/%zu) ",
837 ministate.hist_off + 1,
838 ministate.history->len);
840 getyx(echoarea, off_y, off_x);
842 start = ministate.hist_cur != NULL
843 ? ministate.hist_cur->h
844 : ministate.buf;
845 c = utf8_nth(ministate.buffer.current_line->line,
846 ministate.buffer.cpoff);
847 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
848 start = utf8_next_cp(start);
851 waddstr(echoarea, start);
853 if (ministate.curmesg != NULL)
854 wprintw(echoarea, " [%s]", ministate.curmesg);
856 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
859 static void
860 do_redraw_minibuffer_compl(void)
862 redraw_window(minibuffer, 10, body_cols,
863 &ministate.compl.buffer);
866 /*
867 * Place the cursor in the right ncurses window. If soft is 1, use
868 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
869 * wrefresh.
870 */
871 static void
872 place_cursor(int soft)
874 int (*touch)(WINDOW *);
876 if (soft)
877 touch = wnoutrefresh;
878 else
879 touch = wrefresh;
881 if (in_minibuffer) {
882 touch(body);
883 touch(echoarea);
884 } else {
885 touch(echoarea);
886 touch(body);
890 static void
891 redraw_tab(struct tab *tab)
893 if (too_small)
894 return;
896 if (side_window) {
897 redraw_help();
898 wnoutrefresh(help);
901 redraw_tabline();
902 redraw_body(tab);
903 redraw_modeline(tab);
904 redraw_minibuffer();
906 wnoutrefresh(tabline);
907 wnoutrefresh(modeline);
909 if (in_minibuffer == MB_COMPREAD)
910 wnoutrefresh(minibuffer);
912 place_cursor(1);
914 doupdate();
917 static void
918 emit_help_item(char *prfx, void *fn)
920 struct line *l;
921 struct cmd *cmd;
923 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
924 if (fn == cmd->fn)
925 break;
927 assert(cmd != NULL);
929 if ((l = calloc(1, sizeof(*l))) == NULL)
930 abort();
932 l->type = LINE_TEXT;
933 l->alt = NULL;
935 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
937 if (TAILQ_EMPTY(&helpwin.page.head))
938 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
939 else
940 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
943 static void
944 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
946 struct keymap *k;
947 char p[32];
948 const char *kn;
950 TAILQ_FOREACH(k, &keymap->m, keymaps) {
951 strlcpy(p, prfx, sizeof(p));
952 if (*p != '\0')
953 strlcat(p, " ", sizeof(p));
954 if (k->meta)
955 strlcat(p, "M-", sizeof(p));
956 if ((kn = unkbd(k->key)) != NULL)
957 strlcat(p, kn, sizeof(p));
958 else
959 strlcat(p, keyname(k->key), sizeof(p));
961 if (k->fn == NULL)
962 rec_compute_help(&k->map, p, sizeof(p));
963 else
964 emit_help_item(p, k->fn);
968 static void
969 recompute_help(void)
971 char p[32] = { 0 };
973 empty_vlist(&helpwin);
974 empty_linelist(&helpwin);
975 rec_compute_help(current_map, p, sizeof(p));
976 wrap_page(&helpwin, help_cols);
979 void
980 vmessage(const char *fmt, va_list ap)
982 if (evtimer_pending(&clechoev, NULL))
983 evtimer_del(&clechoev);
985 free(ministate.curmesg);
986 ministate.curmesg = NULL;
988 if (fmt != NULL) {
989 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
990 evtimer_add(&clechoev, &clechoev_timer);
992 /* TODO: what to do if the allocation fails here? */
993 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
994 ministate.curmesg = NULL;
997 redraw_minibuffer();
998 place_cursor(0);
1001 void
1002 message(const char *fmt, ...)
1004 va_list ap;
1006 va_start(ap, fmt);
1007 vmessage(fmt, ap);
1008 va_end(ap);
1011 void
1012 start_loading_anim(struct tab *tab)
1014 if (tab->loading_anim)
1015 return;
1016 tab->loading_anim = 1;
1017 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1018 evtimer_add(&tab->loadingev, &loadingev_timer);
1021 static void
1022 update_loading_anim(int fd, short ev, void *d)
1024 struct tab *tab = d;
1026 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1028 if (tab->flags & TAB_CURRENT) {
1029 redraw_modeline(tab);
1030 wrefresh(modeline);
1031 wrefresh(body);
1032 if (in_minibuffer)
1033 wrefresh(echoarea);
1036 evtimer_add(&tab->loadingev, &loadingev_timer);
1039 static void
1040 stop_loading_anim(struct tab *tab)
1042 if (!tab->loading_anim)
1043 return;
1044 evtimer_del(&tab->loadingev);
1045 tab->loading_anim = 0;
1046 tab->loading_anim_step = 0;
1048 if (!(tab->flags & TAB_CURRENT))
1049 return;
1051 redraw_modeline(tab);
1053 wrefresh(modeline);
1054 wrefresh(body);
1055 if (in_minibuffer)
1056 wrefresh(echoarea);
1059 void
1060 load_url_in_tab(struct tab *tab, const char *url)
1062 message("Loading %s...", url);
1063 start_loading_anim(tab);
1064 load_url(tab, url);
1066 tab->buffer.curs_x = 0;
1067 tab->buffer.curs_y = 0;
1068 redraw_tab(tab);
1071 void
1072 switch_to_tab(struct tab *tab)
1074 struct tab *t;
1076 TAILQ_FOREACH(t, &tabshead, tabs) {
1077 t->flags &= ~TAB_CURRENT;
1080 tab->flags |= TAB_CURRENT;
1081 tab->flags &= ~TAB_URGENT;
1084 unsigned int
1085 tab_new_id(void)
1087 return tab_counter++;
1090 struct tab *
1091 new_tab(const char *url)
1093 struct tab *tab;
1095 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1096 event_loopbreak();
1097 return NULL;
1099 tab->fd = -1;
1101 TAILQ_INIT(&tab->hist.head);
1103 TAILQ_INIT(&tab->buffer.head);
1105 tab->id = tab_new_id();
1106 switch_to_tab(tab);
1108 if (TAILQ_EMPTY(&tabshead))
1109 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1110 else
1111 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1113 load_url_in_tab(tab, url);
1114 return tab;
1117 int
1118 ui_init()
1120 setlocale(LC_ALL, "");
1122 TAILQ_INIT(&eecmd_history.head);
1123 TAILQ_INIT(&ir_history.head);
1124 TAILQ_INIT(&lu_history.head);
1126 ministate.line.type = LINE_TEXT;
1127 ministate.vline.parent = &ministate.line;
1128 ministate.buffer.current_line = &ministate.vline;
1130 /* initialize help window */
1131 TAILQ_INIT(&helpwin.head);
1133 base_map = &global_map;
1134 current_map = &global_map;
1136 initscr();
1138 if (enable_colors) {
1139 if (has_colors()) {
1140 start_color();
1141 use_default_colors();
1142 } else
1143 enable_colors = 0;
1146 config_apply_style();
1148 raw();
1149 noecho();
1150 nonl();
1151 intrflush(stdscr, FALSE);
1153 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1154 return 0;
1155 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1156 return 0;
1157 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1158 return 0;
1159 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1160 return 0;
1161 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1162 return 0;
1163 if ((help = newwin(1, 1, 1, 0)) == NULL)
1164 return 0;
1166 body_lines = LINES-3;
1167 body_cols = COLS;
1169 wbkgd(body, body_face.body);
1170 wbkgd(echoarea, minibuffer_face.background);
1172 update_x_offset();
1174 keypad(body, TRUE);
1175 scrollok(body, FALSE);
1177 /* non-blocking input */
1178 wtimeout(body, 0);
1180 mvwprintw(body, 0, 0, "");
1182 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1183 event_add(&stdioev, NULL);
1185 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1186 signal_add(&winchev, NULL);
1188 return 1;
1191 void
1192 ui_on_tab_loaded(struct tab *tab)
1194 stop_loading_anim(tab);
1195 message("Loaded %s", tab->hist_cur->h);
1197 redraw_tabline();
1198 wrefresh(tabline);
1199 place_cursor(0);
1202 void
1203 ui_on_tab_refresh(struct tab *tab)
1205 wrap_page(&tab->buffer, body_cols);
1206 if (tab->flags & TAB_CURRENT)
1207 redraw_tab(tab);
1208 else
1209 tab->flags |= TAB_URGENT;
1212 const char *
1213 ui_keyname(int k)
1215 return keyname(k);
1218 void
1219 ui_toggle_side_window(void)
1221 side_window = !side_window;
1222 if (side_window)
1223 recompute_help();
1226 * ugly hack, but otherwise the window doesn't get updated
1227 * until I call handle_resize a second time (i.e. C-l). I
1228 * will be happy to know why something like this is needed.
1230 handle_resize_nodelay(0, 0, NULL);
1231 handle_resize_nodelay(0, 0, NULL);
1234 void
1235 ui_schedule_redraw(void)
1237 struct timeval tv = {0, 0};
1239 if (event_pending(&resizeev, EV_TIMEOUT, NULL))
1240 event_del(&resizeev);
1242 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1243 evtimer_add(&resizeev, &tv);
1246 void
1247 ui_require_input(struct tab *tab, int hide)
1249 /* TODO: hard-switching to another tab is ugly */
1250 switch_to_tab(tab);
1252 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1253 &ir_history, NULL, NULL);
1254 strlcpy(ministate.prompt, "Input required: ",
1255 sizeof(ministate.prompt));
1256 redraw_tab(tab);
1259 void
1260 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1261 struct tab *data)
1263 yornp(prompt, fn, data);
1264 redraw_tab(current_tab());
1267 void
1268 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1269 struct tab *data)
1271 completing_read(prompt, fn, data);
1272 redraw_tab(current_tab());
1275 void
1276 ui_end(void)
1278 endwin();