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, 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 wrap_one(buffer, prfx, l, width);
421 break;
424 if (top_orig == l && buffer->top_line == NULL) {
425 buffer->line_off = buffer->line_max-1;
426 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
428 while (1) {
429 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
430 if (vl == NULL || vl->parent != orig)
431 break;
432 buffer->top_line = vl;
433 buffer->line_off--;
437 if (orig == l && buffer->current_line == NULL) {
438 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
440 while (1) {
441 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
442 if (vl == NULL || vl->parent != orig)
443 break;
444 buffer->current_line = vl;
449 if (buffer->current_line == NULL)
450 buffer->current_line = TAILQ_FIRST(&buffer->head);
452 if (buffer->top_line == NULL)
453 buffer->top_line = buffer->current_line;
455 return 1;
458 static void
459 print_vline(int off, int width, WINDOW *window, struct vline *vl)
461 const char *text;
462 const char *prfx;
463 struct line_face *f;
464 int i, left, x, y;
466 f = &line_faces[vl->parent->type];
468 /* unused, set by getyx */
469 (void)y;
471 if (!vl->flags)
472 prfx = line_prefixes[vl->parent->type].prfx1;
473 else
474 prfx = line_prefixes[vl->parent->type].prfx2;
476 text = vl->line;
477 if (text == NULL)
478 text = "";
480 wattr_on(window, body_face.left, NULL);
481 for (i = 0; i < off; i++)
482 waddch(window, ' ');
483 wattr_off(window, body_face.left, NULL);
485 wattr_on(window, f->prefix, NULL);
486 wprintw(window, "%s", prfx);
487 wattr_off(window, f->prefix, NULL);
489 wattr_on(window, f->text, NULL);
490 wprintw(window, "%s", text);
491 wattr_off(window, f->text, NULL);
493 getyx(window, y, x);
495 left = width - x;
497 wattr_on(window, f->trail, NULL);
498 for (i = 0; i < left - off; ++i)
499 waddch(window, ' ');
500 wattr_off(window, f->trail, NULL);
502 wattr_on(window, body_face.right, NULL);
503 for (i = 0; i < off; i++)
504 waddch(window, ' ');
505 wattr_off(window, body_face.right, NULL);
509 static void
510 redraw_tabline(void)
512 struct tab *tab;
513 size_t toskip, ots, tabwidth, space, x;
514 int current, y, truncated;
515 const char *title;
516 char buf[25];
518 x = 0;
520 /* unused, but setted by a getyx */
521 (void)y;
523 tabwidth = sizeof(buf)+1;
524 space = COLS-2;
526 toskip = 0;
527 TAILQ_FOREACH(tab, &tabshead, tabs) {
528 toskip++;
529 if (tab->flags & TAB_CURRENT)
530 break;
533 if (toskip * tabwidth < space)
534 toskip = 0;
535 else {
536 ots = toskip;
537 toskip--;
538 while (toskip != 0 &&
539 (ots - toskip+1) * tabwidth < space)
540 toskip--;
543 werase(tabline);
544 wattr_on(tabline, tab_face.background, NULL);
545 wprintw(tabline, toskip == 0 ? " " : "<");
546 wattr_off(tabline, tab_face.background, NULL);
548 truncated = 0;
549 TAILQ_FOREACH(tab, &tabshead, tabs) {
550 if (truncated)
551 break;
552 if (toskip != 0) {
553 toskip--;
554 continue;
557 getyx(tabline, y, x);
558 if (x + sizeof(buf)+2 >= (size_t)COLS)
559 truncated = 1;
561 current = tab->flags & TAB_CURRENT;
563 if (*(title = tab->buffer.page.title) == '\0')
564 title = tab->hist_cur->h;
566 if (tab->flags & TAB_URGENT)
567 strlcpy(buf, "!", sizeof(buf));
568 else
569 strlcpy(buf, " ", sizeof(buf));
571 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
572 /* truncation happens */
573 strlcpy(&buf[sizeof(buf)-4], "...", 4);
574 } else {
575 /* pad with spaces */
576 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
577 /* nop */ ;
580 if (current)
581 wattr_on(tabline, tab_face.current, NULL);
582 else
583 wattr_on(tabline, tab_face.tab, NULL);
585 wprintw(tabline, "%s", buf);
586 if (TAILQ_NEXT(tab, tabs) != NULL)
587 wprintw(tabline, " ");
589 if (current)
590 wattr_off(tabline, tab_face.current, NULL);
591 else
592 wattr_off(tabline, tab_face.tab, NULL);
595 wattr_on(tabline, tab_face.background, NULL);
596 for (; x < (size_t)COLS; ++x)
597 waddch(tabline, ' ');
598 if (truncated)
599 mvwprintw(tabline, 0, COLS-1, ">");
600 wattr_off(tabline, tab_face.background, NULL);
603 /*
604 * Compute the first visible line around vl. Try to search forward
605 * until the end of the buffer; if a visible line is not found, search
606 * backward. Return NULL if no viable line was found.
607 */
608 struct vline *
609 adjust_line(struct vline *vl, struct buffer *buffer)
611 struct vline *t;
613 if (vl == NULL)
614 return NULL;
616 if (!(vl->parent->flags & L_HIDDEN))
617 return vl;
619 /* search forward */
620 for (t = vl;
621 t != NULL && t->parent->flags & L_HIDDEN;
622 t = TAILQ_NEXT(t, vlines))
623 ; /* nop */
625 if (t != NULL)
626 return t;
628 /* search backward */
629 for (t = vl;
630 t != NULL && t->parent->flags & L_HIDDEN;
631 t = TAILQ_PREV(t, vhead, vlines))
632 ; /* nop */
634 return t;
637 static void
638 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
640 struct vline *vl;
641 int l, onscreen;
643 restore_curs_x(buffer);
645 /*
646 * TODO: ignoring buffer->force_update and always
647 * re-rendering. In theory we can recompute the y position
648 * without a re-render, and optimize here. It's not the only
649 * optimisation possible here, wscrl wolud also be an
650 * interesting one.
651 */
653 again:
654 werase(win);
655 buffer->curs_y = 0;
657 if (TAILQ_EMPTY(&buffer->head))
658 goto end;
660 if (buffer->top_line == NULL)
661 buffer->top_line = TAILQ_FIRST(&buffer->head);
663 buffer->top_line = adjust_line(buffer->top_line, buffer);
664 if (buffer->top_line == NULL)
665 goto end;
667 buffer->current_line = adjust_line(buffer->current_line, buffer);
669 l = 0;
670 onscreen = 0;
671 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
672 if (vl->parent->flags & L_HIDDEN)
673 continue;
675 wmove(win, l, 0);
676 print_vline(off, width, win, vl);
678 if (vl == buffer->current_line)
679 onscreen = 1;
681 if (!onscreen)
682 buffer->curs_y++;
684 l++;
685 if (l == height)
686 break;
689 if (!onscreen) {
690 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
691 if (vl == buffer->current_line)
692 break;
693 if (vl->parent->flags & L_HIDDEN)
694 continue;
695 buffer->line_off++;
696 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
699 if (vl != NULL)
700 goto again;
703 buffer->last_line_off = buffer->line_off;
704 buffer->force_redraw = 0;
705 end:
706 wmove(win, buffer->curs_y, buffer->curs_x);
709 static void
710 redraw_help(void)
712 redraw_window(help, 0, help_lines, help_cols, &helpwin);
715 static void
716 redraw_body(struct tab *tab)
718 static struct tab *last_tab;
720 if (last_tab != tab)
721 tab->buffer.force_redraw =1;
722 last_tab = tab;
724 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
727 static inline char
728 trust_status_char(enum trust_state ts)
730 switch (ts) {
731 case TS_UNKNOWN: return 'u';
732 case TS_UNTRUSTED: return '!';
733 case TS_TEMP_TRUSTED: return '!';
734 case TS_TRUSTED: return 'v';
735 case TS_VERIFIED: return 'V';
736 default: return 'X';
740 static void
741 redraw_modeline(struct tab *tab)
743 double pct;
744 int x, y, max_x, max_y;
745 const char *mode = tab->buffer.page.name;
746 const char *spin = "-\\|/";
748 werase(modeline);
749 wattr_on(modeline, modeline_face.background, NULL);
750 wmove(modeline, 0, 0);
752 wprintw(modeline, "-%c%c %s ",
753 spin[tab->loading_anim_step],
754 trust_status_char(tab->trust),
755 mode == NULL ? "(none)" : mode);
757 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
758 / tab->buffer.line_max;
760 if (tab->buffer.line_max <= (size_t)body_lines)
761 wprintw(modeline, "All ");
762 else if (tab->buffer.line_off == 0)
763 wprintw(modeline, "Top ");
764 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
765 wprintw(modeline, "Bottom ");
766 else
767 wprintw(modeline, "%.0f%% ", pct);
769 wprintw(modeline, "%d/%d %s ",
770 tab->buffer.line_off + tab->buffer.curs_y,
771 tab->buffer.line_max,
772 tab->hist_cur->h);
774 getyx(modeline, y, x);
775 getmaxyx(modeline, max_y, max_x);
777 (void)y;
778 (void)max_y;
780 for (; x < max_x; ++x)
781 waddstr(modeline, "-");
783 wattr_off(modeline, modeline_face.background, NULL);
786 static void
787 redraw_minibuffer(void)
789 wattr_on(echoarea, minibuffer_face.background, NULL);
790 werase(echoarea);
792 if (in_minibuffer)
793 do_redraw_minibuffer();
794 else
795 do_redraw_echoarea();
797 if (in_minibuffer == MB_COMPREAD)
798 do_redraw_minibuffer_compl();
800 wattr_off(echoarea, minibuffer_face.background, NULL);
803 static void
804 do_redraw_echoarea(void)
806 struct tab *tab;
808 if (ministate.curmesg != NULL)
809 wprintw(echoarea, "%s", ministate.curmesg);
810 else if (*keybuf != '\0')
811 waddstr(echoarea, keybuf);
812 else {
813 /* If nothing else, show the URL at point */
814 tab = current_tab();
815 if (tab->buffer.current_line != NULL &&
816 tab->buffer.current_line->parent->type == LINE_LINK)
817 waddstr(echoarea,
818 tab->buffer.current_line->parent->meta.alt);
822 static void
823 do_redraw_minibuffer(void)
825 size_t off_y, off_x = 0;
826 const char *start, *c;
828 /* unused, set by getyx */
829 (void)off_y;
831 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
832 if (ministate.hist_cur != NULL)
833 wprintw(echoarea, "(%zu/%zu) ",
834 ministate.hist_off + 1,
835 ministate.history->len);
837 getyx(echoarea, off_y, off_x);
839 start = ministate.hist_cur != NULL
840 ? ministate.hist_cur->h
841 : ministate.buf;
842 c = utf8_nth(ministate.buffer.current_line->line,
843 ministate.buffer.cpoff);
844 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
845 start = utf8_next_cp(start);
848 waddstr(echoarea, start);
850 if (ministate.curmesg != NULL)
851 wprintw(echoarea, " [%s]", ministate.curmesg);
853 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
856 static void
857 do_redraw_minibuffer_compl(void)
859 redraw_window(minibuffer, 0, 10, body_cols,
860 &ministate.compl.buffer);
863 /*
864 * Place the cursor in the right ncurses window. If soft is 1, use
865 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
866 * wrefresh.
867 */
868 static void
869 place_cursor(int soft)
871 int (*touch)(WINDOW *);
873 if (soft)
874 touch = wnoutrefresh;
875 else
876 touch = wrefresh;
878 if (in_minibuffer) {
879 touch(body);
880 touch(echoarea);
881 } else {
882 touch(echoarea);
883 touch(body);
887 static void
888 redraw_tab(struct tab *tab)
890 if (too_small)
891 return;
893 if (side_window) {
894 redraw_help();
895 wnoutrefresh(help);
898 redraw_tabline();
899 redraw_body(tab);
900 redraw_modeline(tab);
901 redraw_minibuffer();
903 wnoutrefresh(tabline);
904 wnoutrefresh(modeline);
906 if (in_minibuffer == MB_COMPREAD)
907 wnoutrefresh(minibuffer);
909 place_cursor(1);
911 doupdate();
914 static void
915 emit_help_item(char *prfx, void *fn)
917 struct line *l;
918 struct cmd *cmd;
920 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
921 if (fn == cmd->fn)
922 break;
924 assert(cmd != NULL);
926 if ((l = calloc(1, sizeof(*l))) == NULL)
927 abort();
929 l->type = LINE_TEXT;
930 l->meta.alt = NULL;
932 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
934 if (TAILQ_EMPTY(&helpwin.page.head))
935 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
936 else
937 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
940 static void
941 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
943 struct keymap *k;
944 char p[32];
945 const char *kn;
947 TAILQ_FOREACH(k, &keymap->m, keymaps) {
948 strlcpy(p, prfx, sizeof(p));
949 if (*p != '\0')
950 strlcat(p, " ", sizeof(p));
951 if (k->meta)
952 strlcat(p, "M-", sizeof(p));
953 if ((kn = unkbd(k->key)) != NULL)
954 strlcat(p, kn, sizeof(p));
955 else
956 strlcat(p, keyname(k->key), sizeof(p));
958 if (k->fn == NULL)
959 rec_compute_help(&k->map, p, sizeof(p));
960 else
961 emit_help_item(p, k->fn);
965 static void
966 recompute_help(void)
968 char p[32] = { 0 };
970 empty_vlist(&helpwin);
971 empty_linelist(&helpwin);
972 rec_compute_help(current_map, p, sizeof(p));
973 wrap_page(&helpwin, help_cols);
976 void
977 vmessage(const char *fmt, va_list ap)
979 if (evtimer_pending(&clechoev, NULL))
980 evtimer_del(&clechoev);
982 free(ministate.curmesg);
983 ministate.curmesg = NULL;
985 if (fmt != NULL) {
986 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
987 evtimer_add(&clechoev, &clechoev_timer);
989 /* TODO: what to do if the allocation fails here? */
990 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
991 ministate.curmesg = NULL;
994 redraw_minibuffer();
995 place_cursor(0);
998 void
999 message(const char *fmt, ...)
1001 va_list ap;
1003 va_start(ap, fmt);
1004 vmessage(fmt, ap);
1005 va_end(ap);
1008 void
1009 start_loading_anim(struct tab *tab)
1011 if (tab->loading_anim)
1012 return;
1013 tab->loading_anim = 1;
1014 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1015 evtimer_add(&tab->loadingev, &loadingev_timer);
1018 static void
1019 update_loading_anim(int fd, short ev, void *d)
1021 struct tab *tab = d;
1023 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1025 if (tab->flags & TAB_CURRENT) {
1026 redraw_modeline(tab);
1027 wrefresh(modeline);
1028 wrefresh(body);
1029 if (in_minibuffer)
1030 wrefresh(echoarea);
1033 evtimer_add(&tab->loadingev, &loadingev_timer);
1036 static void
1037 stop_loading_anim(struct tab *tab)
1039 if (!tab->loading_anim)
1040 return;
1041 evtimer_del(&tab->loadingev);
1042 tab->loading_anim = 0;
1043 tab->loading_anim_step = 0;
1045 if (!(tab->flags & TAB_CURRENT))
1046 return;
1048 redraw_modeline(tab);
1050 wrefresh(modeline);
1051 wrefresh(body);
1052 if (in_minibuffer)
1053 wrefresh(echoarea);
1056 void
1057 load_url_in_tab(struct tab *tab, const char *url)
1059 message("Loading %s...", url);
1060 start_loading_anim(tab);
1061 load_url(tab, url);
1063 tab->buffer.curs_x = 0;
1064 tab->buffer.curs_y = 0;
1065 redraw_tab(tab);
1068 void
1069 switch_to_tab(struct tab *tab)
1071 struct tab *t;
1073 TAILQ_FOREACH(t, &tabshead, tabs) {
1074 t->flags &= ~TAB_CURRENT;
1077 tab->flags |= TAB_CURRENT;
1078 tab->flags &= ~TAB_URGENT;
1081 unsigned int
1082 tab_new_id(void)
1084 return tab_counter++;
1087 struct tab *
1088 new_tab(const char *url)
1090 struct tab *tab;
1092 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1093 event_loopbreak();
1094 return NULL;
1096 tab->fd = -1;
1098 TAILQ_INIT(&tab->hist.head);
1100 TAILQ_INIT(&tab->buffer.head);
1102 tab->id = tab_new_id();
1103 switch_to_tab(tab);
1105 if (TAILQ_EMPTY(&tabshead))
1106 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1107 else
1108 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1110 load_url_in_tab(tab, url);
1111 return tab;
1114 int
1115 ui_init()
1117 setlocale(LC_ALL, "");
1119 TAILQ_INIT(&eecmd_history.head);
1120 TAILQ_INIT(&ir_history.head);
1121 TAILQ_INIT(&lu_history.head);
1123 ministate.line.type = LINE_TEXT;
1124 ministate.vline.parent = &ministate.line;
1125 ministate.buffer.current_line = &ministate.vline;
1127 /* initialize help window */
1128 TAILQ_INIT(&helpwin.head);
1130 base_map = &global_map;
1131 current_map = &global_map;
1133 initscr();
1135 if (enable_colors) {
1136 if (has_colors()) {
1137 start_color();
1138 use_default_colors();
1139 } else
1140 enable_colors = 0;
1143 config_apply_style();
1145 raw();
1146 noecho();
1147 nonl();
1148 intrflush(stdscr, FALSE);
1150 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1151 return 0;
1152 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1153 return 0;
1154 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1155 return 0;
1156 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1157 return 0;
1158 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1159 return 0;
1160 if ((help = newwin(1, 1, 1, 0)) == NULL)
1161 return 0;
1163 body_lines = LINES-3;
1164 body_cols = COLS;
1166 wbkgd(body, body_face.body);
1167 wbkgd(echoarea, minibuffer_face.background);
1169 update_x_offset();
1171 keypad(body, TRUE);
1172 scrollok(body, FALSE);
1174 /* non-blocking input */
1175 wtimeout(body, 0);
1177 mvwprintw(body, 0, 0, "");
1179 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1180 event_add(&stdioev, NULL);
1182 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1183 signal_add(&winchev, NULL);
1185 return 1;
1188 void
1189 ui_on_tab_loaded(struct tab *tab)
1191 stop_loading_anim(tab);
1192 message("Loaded %s", tab->hist_cur->h);
1194 redraw_tabline();
1195 wrefresh(tabline);
1196 place_cursor(0);
1199 void
1200 ui_on_tab_refresh(struct tab *tab)
1202 wrap_page(&tab->buffer, body_cols);
1203 if (tab->flags & TAB_CURRENT)
1204 redraw_tab(tab);
1205 else
1206 tab->flags |= TAB_URGENT;
1209 const char *
1210 ui_keyname(int k)
1212 return keyname(k);
1215 void
1216 ui_toggle_side_window(void)
1218 side_window = !side_window;
1219 if (side_window)
1220 recompute_help();
1223 * ugly hack, but otherwise the window doesn't get updated
1224 * until I call handle_resize a second time (i.e. C-l). I
1225 * will be happy to know why something like this is needed.
1227 handle_resize_nodelay(0, 0, NULL);
1228 handle_resize_nodelay(0, 0, NULL);
1231 void
1232 ui_schedule_redraw(void)
1234 struct timeval tv = {0, 0};
1236 if (event_pending(&resizeev, EV_TIMEOUT, NULL))
1237 event_del(&resizeev);
1239 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1240 evtimer_add(&resizeev, &tv);
1243 void
1244 ui_require_input(struct tab *tab, int hide)
1246 /* TODO: hard-switching to another tab is ugly */
1247 switch_to_tab(tab);
1249 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1250 &ir_history, NULL, NULL);
1251 strlcpy(ministate.prompt, "Input required: ",
1252 sizeof(ministate.prompt));
1253 redraw_tab(tab);
1256 void
1257 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1258 struct tab *data)
1260 yornp(prompt, fn, data);
1261 redraw_tab(current_tab());
1264 void
1265 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1266 struct tab *data)
1268 completing_read(prompt, fn, data);
1269 redraw_tab(current_tab());
1272 void
1273 ui_end(void)
1275 endwin();