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 too_small;
78 static int x_offset;
80 struct thiskey thiskey;
82 static struct event resizeev;
83 static struct timeval resize_timer = { 0, 250000 };
85 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
87 int body_lines, body_cols;
89 static WINDOW *help;
90 static struct buffer helpwin;
91 static int help_lines, help_cols;
93 static int side_window;
95 static struct event clechoev;
96 static struct timeval clechoev_timer = { 5, 0 };
97 static struct timeval loadingev_timer = { 0, 250000 };
99 static uint32_t tab_counter;
101 static char keybuf[64];
103 struct kmap global_map,
104 minibuffer_map,
105 *current_map,
106 *base_map;
108 int in_minibuffer;
110 static inline void
111 update_x_offset(void)
113 if (olivetti_mode && fill_column < body_cols)
114 x_offset = (body_cols - fill_column)/2;
115 else
116 x_offset = 0;
119 void
120 save_excursion(struct excursion *place, struct buffer *buffer)
122 place->curs_x = buffer->curs_x;
123 place->curs_y = buffer->curs_y;
124 place->line_off = buffer->line_off;
125 place->current_line = buffer->current_line;
126 place->cpoff = buffer->cpoff;
129 void
130 restore_excursion(struct excursion *place, struct buffer *buffer)
132 buffer->curs_x = place->curs_x;
133 buffer->curs_y = place->curs_y;
134 buffer->line_off = place->line_off;
135 buffer->current_line = place->current_line;
136 buffer->cpoff = place->cpoff;
139 static void
140 restore_curs_x(struct buffer *buffer)
142 struct vline *vl;
143 const char *prfx;
145 vl = buffer->current_line;
146 if (vl == NULL || vl->line == NULL)
147 buffer->curs_x = buffer->cpoff = 0;
148 else
149 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
151 buffer->curs_x += x_offset;
153 if (vl != NULL) {
154 prfx = line_prefixes[vl->parent->type].prfx1;
155 buffer->curs_x += utf8_swidth(prfx);
159 void
160 global_key_unbound(void)
162 message("%s is undefined", keybuf);
165 static struct vline *
166 nth_line(struct buffer *buffer, size_t n)
168 struct vline *vl;
169 size_t i;
171 i = 0;
172 TAILQ_FOREACH(vl, &buffer->head, vlines) {
173 if (i == n)
174 return vl;
175 i++;
178 /* unreachable */
179 abort();
182 struct tab *
183 current_tab(void)
185 struct tab *t;
187 TAILQ_FOREACH(t, &tabshead, tabs) {
188 if (t->flags & TAB_CURRENT)
189 return t;
192 /* unreachable */
193 abort();
196 struct buffer *
197 current_buffer(void)
199 if (in_minibuffer)
200 return &ministate.buffer;
201 return &current_tab()->buffer;
204 static int
205 readkey(void)
207 uint32_t state = 0;
209 if ((thiskey.key = wgetch(body)) == ERR)
210 return 0;
212 thiskey.meta = thiskey.key == 27;
213 if (thiskey.meta) {
214 thiskey.key = wgetch(body);
215 if (thiskey.key == ERR || thiskey.key == 27) {
216 thiskey.meta = 0;
217 thiskey.key = 27;
221 thiskey.cp = 0;
222 if ((unsigned int)thiskey.key < UINT8_MAX) {
223 while (1) {
224 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
225 break;
226 if ((thiskey.key = wgetch(body)) == ERR) {
227 message("Error decoding user input");
228 return 0;
233 return 1;
236 static void
237 dispatch_stdio(int fd, short ev, void *d)
239 struct keymap *k;
240 const char *keyname;
241 char tmp[5] = {0};
243 /* TODO: schedule a redraw? */
244 if (too_small)
245 return;
247 if (!readkey())
248 return;
250 if (keybuf[0] != '\0')
251 strlcat(keybuf, " ", sizeof(keybuf));
252 if (thiskey.meta)
253 strlcat(keybuf, "M-", sizeof(keybuf));
254 if (thiskey.cp != 0) {
255 utf8_encode(thiskey.cp, tmp);
256 strlcat(keybuf, tmp, sizeof(keybuf));
257 } else {
258 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));
266 TAILQ_FOREACH(k, &current_map->m, keymaps) {
267 if (k->meta == thiskey.meta &&
268 k->key == thiskey.key) {
269 if (k->fn == NULL)
270 current_map = &k->map;
271 else {
272 current_map = base_map;
273 strlcpy(keybuf, "", sizeof(keybuf));
274 k->fn(current_buffer());
276 goto done;
280 if (current_map->unhandled_input != NULL)
281 current_map->unhandled_input();
282 else
283 global_key_unbound();
285 strlcpy(keybuf, "", sizeof(keybuf));
286 current_map = base_map;
288 done:
289 if (side_window)
290 recompute_help();
292 redraw_tab(current_tab());
295 static void
296 handle_clear_echoarea(int fd, short ev, void *d)
298 free(ministate.curmesg);
299 ministate.curmesg = NULL;
301 redraw_minibuffer();
302 place_cursor(0);
305 static void
306 handle_resize(int sig, short ev, void *d)
308 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
309 event_del(&resizeev);
311 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
312 evtimer_add(&resizeev, &resize_timer);
315 static void
316 handle_resize_nodelay(int s, short ev, void *d)
318 struct tab *tab;
319 int lines;
321 endwin();
322 refresh();
323 clear();
325 lines = LINES;
327 if ((too_small = lines < 15)) {
328 erase();
329 printw("Window too small.");
330 refresh();
331 return;
334 /* move and resize the windows, in reverse order! */
336 mvwin(echoarea, --lines, 0);
337 wresize(echoarea, 1, COLS);
339 mvwin(modeline, --lines, 0);
340 wresize(modeline, 1, COLS);
342 body_lines = --lines;
343 body_cols = COLS;
345 if (side_window) {
346 help_cols = 0.3 * COLS;
347 help_lines = lines;
348 mvwin(help, 1, 0);
349 wresize(help, help_lines, help_cols);
351 wrap_page(&helpwin, help_cols);
353 body_cols = COLS - help_cols - 1;
354 mvwin(body, 1, help_cols);
355 } else
356 mvwin(body, 1, 0);
358 update_x_offset();
359 wresize(body, body_lines, body_cols);
361 wresize(tabline, 1, COLS);
363 tab = current_tab();
365 wrap_page(&tab->buffer, body_cols);
366 redraw_tab(tab);
369 static int
370 wrap_page(struct buffer *buffer, int width)
372 struct line *l;
373 const struct line *top_orig, *orig;
374 struct vline *vl;
375 int pre_width;
376 const char *prfx;
378 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
379 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
381 buffer->top_line = NULL;
382 buffer->current_line = NULL;
384 buffer->force_redraw = 1;
385 buffer->curs_y = 0;
386 buffer->line_off = 0;
388 empty_vlist(buffer);
390 TAILQ_FOREACH(l, &buffer->page.head, lines) {
391 prfx = line_prefixes[l->type].prfx1;
392 switch (l->type) {
393 case LINE_TEXT:
394 case LINE_LINK:
395 case LINE_TITLE_1:
396 case LINE_TITLE_2:
397 case LINE_TITLE_3:
398 case LINE_ITEM:
399 case LINE_QUOTE:
400 case LINE_PRE_START:
401 case LINE_PRE_END:
402 wrap_text(buffer, prfx, l, MIN(fill_column, width));
403 break;
404 case LINE_PRE_CONTENT:
405 if (olivetti_mode)
406 pre_width = MIN(fill_column, width);
407 else
408 pre_width = width;
409 hardwrap_text(buffer, l, pre_width);
410 break;
413 if (top_orig == l && buffer->top_line == NULL) {
414 buffer->line_off = buffer->line_max-1;
415 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
417 while (1) {
418 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
419 if (vl == NULL || vl->parent != orig)
420 break;
421 buffer->top_line = vl;
422 buffer->line_off--;
426 if (orig == l && buffer->current_line == NULL) {
427 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
429 while (1) {
430 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
431 if (vl == NULL || vl->parent != orig)
432 break;
433 buffer->current_line = vl;
438 if (buffer->current_line == NULL)
439 buffer->current_line = TAILQ_FIRST(&buffer->head);
441 if (buffer->top_line == NULL)
442 buffer->top_line = buffer->current_line;
444 return 1;
447 static void
448 print_vline(int off, int width, WINDOW *window, struct vline *vl)
450 const char *text;
451 const char *prfx;
452 struct line_face *f;
453 int i, left, x, y;
455 f = &line_faces[vl->parent->type];
457 /* unused, set by getyx */
458 (void)y;
460 if (!vl->flags)
461 prfx = line_prefixes[vl->parent->type].prfx1;
462 else
463 prfx = line_prefixes[vl->parent->type].prfx2;
465 text = vl->line;
466 if (text == NULL)
467 text = "";
469 wattr_on(window, body_face.left, NULL);
470 for (i = 0; i < off; i++)
471 waddch(window, ' ');
472 wattr_off(window, body_face.left, NULL);
474 wattr_on(window, f->prefix, NULL);
475 wprintw(window, "%s", prfx);
476 wattr_off(window, f->prefix, NULL);
478 wattr_on(window, f->text, NULL);
479 wprintw(window, "%s", text);
480 wattr_off(window, f->text, NULL);
482 getyx(window, y, x);
484 left = width - x;
486 wattr_on(window, f->trail, NULL);
487 for (i = 0; i < left - off; ++i)
488 waddch(window, ' ');
489 wattr_off(window, f->trail, NULL);
491 wattr_on(window, body_face.right, NULL);
492 for (i = 0; i < off; i++)
493 waddch(window, ' ');
494 wattr_off(window, body_face.right, NULL);
498 static void
499 redraw_tabline(void)
501 struct tab *tab;
502 size_t toskip, ots, tabwidth, space, x;
503 int current, y, truncated;
504 const char *title;
505 char buf[25];
507 x = 0;
509 /* unused, but setted by a getyx */
510 (void)y;
512 tabwidth = sizeof(buf)+1;
513 space = COLS-2;
515 toskip = 0;
516 TAILQ_FOREACH(tab, &tabshead, tabs) {
517 toskip++;
518 if (tab->flags & TAB_CURRENT)
519 break;
522 if (toskip * tabwidth < space)
523 toskip = 0;
524 else {
525 ots = toskip;
526 toskip--;
527 while (toskip != 0 &&
528 (ots - toskip+1) * tabwidth < space)
529 toskip--;
532 werase(tabline);
533 wattr_on(tabline, tab_face.background, NULL);
534 wprintw(tabline, toskip == 0 ? " " : "<");
535 wattr_off(tabline, tab_face.background, NULL);
537 truncated = 0;
538 TAILQ_FOREACH(tab, &tabshead, tabs) {
539 if (truncated)
540 break;
541 if (toskip != 0) {
542 toskip--;
543 continue;
546 getyx(tabline, y, x);
547 if (x + sizeof(buf)+2 >= (size_t)COLS)
548 truncated = 1;
550 current = tab->flags & TAB_CURRENT;
552 if (*(title = tab->buffer.page.title) == '\0')
553 title = tab->hist_cur->h;
555 if (tab->flags & TAB_URGENT)
556 strlcpy(buf, "!", sizeof(buf));
557 else
558 strlcpy(buf, " ", sizeof(buf));
560 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
561 /* truncation happens */
562 strlcpy(&buf[sizeof(buf)-4], "...", 4);
563 } else {
564 /* pad with spaces */
565 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
566 /* nop */ ;
569 if (current)
570 wattr_on(tabline, tab_face.current, NULL);
571 else
572 wattr_on(tabline, tab_face.tab, NULL);
574 wprintw(tabline, "%s", buf);
575 if (TAILQ_NEXT(tab, tabs) != NULL)
576 wprintw(tabline, " ");
578 if (current)
579 wattr_off(tabline, tab_face.current, NULL);
580 else
581 wattr_off(tabline, tab_face.tab, NULL);
584 wattr_on(tabline, tab_face.background, NULL);
585 for (; x < (size_t)COLS; ++x)
586 waddch(tabline, ' ');
587 if (truncated)
588 mvwprintw(tabline, 0, COLS-1, ">");
589 wattr_off(tabline, tab_face.background, NULL);
592 /*
593 * Compute the first visible line around vl. Try to search forward
594 * until the end of the buffer; if a visible line is not found, search
595 * backward. Return NULL if no viable line was found.
596 */
597 static inline struct vline *
598 adjust_line(struct vline *vl, struct buffer *buffer)
600 struct vline *t;
602 if (!(vl->parent->flags & L_HIDDEN))
603 return vl;
605 /* search forward */
606 for (t = vl;
607 t != NULL && t->parent->flags & L_HIDDEN;
608 t = TAILQ_NEXT(t, vlines))
609 ; /* nop */
611 if (t != NULL)
612 return t;
614 /* search backward */
615 for (t = vl;
616 t != NULL && t->parent->flags & L_HIDDEN;
617 t = TAILQ_PREV(t, vhead, vlines))
618 ; /* nop */
620 return t;
623 static void
624 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
626 struct vline *vl;
627 int l, onscreen;
629 restore_curs_x(buffer);
631 /*
632 * TODO: ignoring buffer->force_update and always
633 * re-rendering. In theory we can recompute the y position
634 * without a re-render, and optimize here. It's not the only
635 * optimisation possible here, wscrl wolud also be an
636 * interesting one.
637 */
639 again:
640 werase(win);
641 buffer->curs_y = 0;
643 if (TAILQ_EMPTY(&buffer->head))
644 goto end;
646 buffer->top_line = adjust_line(buffer->top_line, buffer);
647 if (buffer->top_line == NULL)
648 goto end;
650 buffer->current_line = adjust_line(buffer->current_line, buffer);
652 l = 0;
653 onscreen = 0;
654 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
655 if (vl->parent->flags & L_HIDDEN)
656 continue;
658 wmove(win, l, 0);
659 print_vline(x_offset, width, win, vl);
661 if (vl == buffer->current_line)
662 onscreen = 1;
664 if (!onscreen)
665 buffer->curs_y++;
667 l++;
668 if (l == height)
669 break;
672 if (!onscreen) {
673 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
674 if (vl == buffer->current_line)
675 break;
676 if (vl->parent->flags & L_HIDDEN)
677 continue;
678 buffer->line_off++;
679 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
682 goto again;
685 buffer->last_line_off = buffer->line_off;
686 buffer->force_redraw = 0;
687 end:
688 wmove(win, buffer->curs_y, buffer->curs_x);
691 static void
692 redraw_help(void)
694 redraw_window(help, help_lines, help_cols, &helpwin);
697 static void
698 redraw_body(struct tab *tab)
700 static struct tab *last_tab;
702 if (last_tab != tab)
703 tab->buffer.force_redraw =1;
704 last_tab = tab;
706 redraw_window(body, body_lines, body_cols, &tab->buffer);
709 static inline char
710 trust_status_char(enum trust_state ts)
712 switch (ts) {
713 case TS_UNKNOWN: return 'u';
714 case TS_UNTRUSTED: return '!';
715 case TS_TEMP_TRUSTED: return '!';
716 case TS_TRUSTED: return 'v';
717 case TS_VERIFIED: return 'V';
718 default: return 'X';
722 static void
723 redraw_modeline(struct tab *tab)
725 double pct;
726 int x, y, max_x, max_y;
727 const char *mode = tab->buffer.page.name;
728 const char *spin = "-\\|/";
730 werase(modeline);
731 wattr_on(modeline, modeline_face.background, NULL);
732 wmove(modeline, 0, 0);
734 wprintw(modeline, "-%c%c %s ",
735 spin[tab->loading_anim_step],
736 trust_status_char(tab->trust),
737 mode == NULL ? "(none)" : mode);
739 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
741 if (tab->buffer.line_max <= (size_t)body_lines)
742 wprintw(modeline, "All ");
743 else if (tab->buffer.line_off == 0)
744 wprintw(modeline, "Top ");
745 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
746 wprintw(modeline, "Bottom ");
747 else
748 wprintw(modeline, "%.0f%% ", pct);
750 wprintw(modeline, "%d/%d %s ",
751 tab->buffer.line_off + tab->buffer.curs_y,
752 tab->buffer.line_max,
753 tab->hist_cur->h);
755 getyx(modeline, y, x);
756 getmaxyx(modeline, max_y, max_x);
758 (void)y;
759 (void)max_y;
761 for (; x < max_x; ++x)
762 waddstr(modeline, "-");
764 wattr_off(modeline, modeline_face.background, NULL);
767 static void
768 redraw_minibuffer(void)
770 wattr_on(echoarea, minibuffer_face.background, NULL);
771 werase(echoarea);
773 if (in_minibuffer)
774 do_redraw_minibuffer();
775 else
776 do_redraw_echoarea();
778 wattr_off(echoarea, minibuffer_face.background, NULL);
781 static void
782 do_redraw_echoarea(void)
784 struct tab *tab;
786 if (ministate.curmesg != NULL)
787 wprintw(echoarea, "%s", ministate.curmesg);
788 else if (*keybuf != '\0')
789 waddstr(echoarea, keybuf);
790 else {
791 /* If nothing else, show the URL at point */
792 tab = current_tab();
793 if (tab->buffer.current_line != NULL &&
794 tab->buffer.current_line->parent->type == LINE_LINK)
795 waddstr(echoarea, tab->buffer.current_line->parent->alt);
799 static void
800 do_redraw_minibuffer(void)
802 size_t off_y, off_x = 0;
803 const char *start, *c;
805 /* unused, set by getyx */
806 (void)off_y;
808 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
809 if (ministate.hist_cur != NULL)
810 wprintw(echoarea, "(%zu/%zu) ",
811 ministate.hist_off + 1,
812 ministate.history->len);
814 getyx(echoarea, off_y, off_x);
816 start = ministate.hist_cur != NULL
817 ? ministate.hist_cur->h
818 : ministate.buf;
819 c = utf8_nth(ministate.buffer.current_line->line,
820 ministate.buffer.cpoff);
821 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
822 start = utf8_next_cp(start);
825 waddstr(echoarea, start);
827 if (ministate.curmesg != NULL)
828 wprintw(echoarea, " [%s]", ministate.curmesg);
830 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
833 /*
834 * Place the cursor in the right ncurses window. If soft is 1, use
835 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
836 * wrefresh.
837 */
838 static void
839 place_cursor(int soft)
841 int (*touch)(WINDOW *);
843 if (soft)
844 touch = wnoutrefresh;
845 else
846 touch = wrefresh;
848 if (in_minibuffer) {
849 touch(body);
850 touch(echoarea);
851 } else {
852 touch(echoarea);
853 touch(body);
857 static void
858 redraw_tab(struct tab *tab)
860 if (too_small)
861 return;
863 if (side_window) {
864 redraw_help();
865 wnoutrefresh(help);
868 redraw_tabline();
869 redraw_body(tab);
870 redraw_modeline(tab);
871 redraw_minibuffer();
873 wnoutrefresh(tabline);
874 wnoutrefresh(modeline);
876 place_cursor(1);
878 doupdate();
881 static void
882 emit_help_item(char *prfx, void *fn)
884 struct line *l;
885 struct cmd *cmd;
887 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
888 if (fn == cmd->fn)
889 break;
891 assert(cmd != NULL);
893 if ((l = calloc(1, sizeof(*l))) == NULL)
894 abort();
896 l->type = LINE_TEXT;
897 l->alt = NULL;
899 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
901 if (TAILQ_EMPTY(&helpwin.page.head))
902 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
903 else
904 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
907 static void
908 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
910 struct keymap *k;
911 char p[32];
912 const char *kn;
914 TAILQ_FOREACH(k, &keymap->m, keymaps) {
915 strlcpy(p, prfx, sizeof(p));
916 if (*p != '\0')
917 strlcat(p, " ", sizeof(p));
918 if (k->meta)
919 strlcat(p, "M-", sizeof(p));
920 if ((kn = unkbd(k->key)) != NULL)
921 strlcat(p, kn, sizeof(p));
922 else
923 strlcat(p, keyname(k->key), sizeof(p));
925 if (k->fn == NULL)
926 rec_compute_help(&k->map, p, sizeof(p));
927 else
928 emit_help_item(p, k->fn);
932 static void
933 recompute_help(void)
935 char p[32] = { 0 };
937 empty_vlist(&helpwin);
938 empty_linelist(&helpwin);
939 rec_compute_help(current_map, p, sizeof(p));
940 wrap_page(&helpwin, help_cols);
943 void
944 vmessage(const char *fmt, va_list ap)
946 if (evtimer_pending(&clechoev, NULL))
947 evtimer_del(&clechoev);
949 free(ministate.curmesg);
950 ministate.curmesg = NULL;
952 if (fmt != NULL) {
953 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
954 evtimer_add(&clechoev, &clechoev_timer);
956 /* TODO: what to do if the allocation fails here? */
957 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
958 ministate.curmesg = NULL;
961 redraw_minibuffer();
962 place_cursor(0);
965 void
966 message(const char *fmt, ...)
968 va_list ap;
970 va_start(ap, fmt);
971 vmessage(fmt, ap);
972 va_end(ap);
975 void
976 start_loading_anim(struct tab *tab)
978 if (tab->loading_anim)
979 return;
980 tab->loading_anim = 1;
981 evtimer_set(&tab->loadingev, update_loading_anim, tab);
982 evtimer_add(&tab->loadingev, &loadingev_timer);
985 static void
986 update_loading_anim(int fd, short ev, void *d)
988 struct tab *tab = d;
990 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
992 if (tab->flags & TAB_CURRENT) {
993 redraw_modeline(tab);
994 wrefresh(modeline);
995 wrefresh(body);
996 if (in_minibuffer)
997 wrefresh(echoarea);
1000 evtimer_add(&tab->loadingev, &loadingev_timer);
1003 static void
1004 stop_loading_anim(struct tab *tab)
1006 if (!tab->loading_anim)
1007 return;
1008 evtimer_del(&tab->loadingev);
1009 tab->loading_anim = 0;
1010 tab->loading_anim_step = 0;
1012 if (!(tab->flags & TAB_CURRENT))
1013 return;
1015 redraw_modeline(tab);
1017 wrefresh(modeline);
1018 wrefresh(body);
1019 if (in_minibuffer)
1020 wrefresh(echoarea);
1023 void
1024 load_url_in_tab(struct tab *tab, const char *url)
1026 message("Loading %s...", url);
1027 start_loading_anim(tab);
1028 load_url(tab, url);
1030 tab->buffer.curs_x = 0;
1031 tab->buffer.curs_y = 0;
1032 redraw_tab(tab);
1035 void
1036 switch_to_tab(struct tab *tab)
1038 struct tab *t;
1040 TAILQ_FOREACH(t, &tabshead, tabs) {
1041 t->flags &= ~TAB_CURRENT;
1044 tab->flags |= TAB_CURRENT;
1045 tab->flags &= ~TAB_URGENT;
1048 unsigned int
1049 tab_new_id(void)
1051 return tab_counter++;
1054 struct tab *
1055 new_tab(const char *url)
1057 struct tab *tab;
1059 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1060 event_loopbreak();
1061 return NULL;
1063 tab->fd = -1;
1065 TAILQ_INIT(&tab->hist.head);
1067 TAILQ_INIT(&tab->buffer.head);
1069 tab->id = tab_new_id();
1070 switch_to_tab(tab);
1072 if (TAILQ_EMPTY(&tabshead))
1073 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1074 else
1075 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1077 load_url_in_tab(tab, url);
1078 return tab;
1081 int
1082 ui_init()
1084 setlocale(LC_ALL, "");
1086 TAILQ_INIT(&eecmd_history.head);
1087 TAILQ_INIT(&ir_history.head);
1088 TAILQ_INIT(&lu_history.head);
1090 ministate.line.type = LINE_TEXT;
1091 ministate.vline.parent = &ministate.line;
1092 ministate.buffer.current_line = &ministate.vline;
1094 /* initialize help window */
1095 TAILQ_INIT(&helpwin.head);
1097 base_map = &global_map;
1098 current_map = &global_map;
1100 initscr();
1102 if (enable_colors) {
1103 if (has_colors()) {
1104 start_color();
1105 use_default_colors();
1106 } else
1107 enable_colors = 0;
1110 config_apply_style();
1112 raw();
1113 noecho();
1114 nonl();
1115 intrflush(stdscr, FALSE);
1117 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1118 return 0;
1119 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1120 return 0;
1121 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1122 return 0;
1123 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1124 return 0;
1125 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1126 return 0;
1127 if ((help = newwin(1, 1, 1, 0)) == NULL)
1128 return 0;
1130 body_lines = LINES-3;
1131 body_cols = COLS;
1133 wbkgd(body, body_face.body);
1134 wbkgd(echoarea, minibuffer_face.background);
1136 update_x_offset();
1138 keypad(body, TRUE);
1139 scrollok(body, FALSE);
1141 /* non-blocking input */
1142 wtimeout(body, 0);
1144 mvwprintw(body, 0, 0, "");
1146 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1147 event_add(&stdioev, NULL);
1149 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1150 signal_add(&winchev, NULL);
1152 return 1;
1155 void
1156 ui_on_tab_loaded(struct tab *tab)
1158 stop_loading_anim(tab);
1159 message("Loaded %s", tab->hist_cur->h);
1161 redraw_tabline();
1162 wrefresh(tabline);
1163 place_cursor(0);
1166 void
1167 ui_on_tab_refresh(struct tab *tab)
1169 wrap_page(&tab->buffer, body_cols);
1170 if (tab->flags & TAB_CURRENT)
1171 redraw_tab(tab);
1172 else
1173 tab->flags |= TAB_URGENT;
1176 const char *
1177 ui_keyname(int k)
1179 return keyname(k);
1182 void
1183 ui_toggle_side_window(void)
1185 side_window = !side_window;
1186 if (side_window)
1187 recompute_help();
1190 * ugly hack, but otherwise the window doesn't get updated
1191 * until I call handle_resize a second time (i.e. C-l). I
1192 * will be happy to know why something like this is needed.
1194 handle_resize_nodelay(0, 0, NULL);
1195 handle_resize_nodelay(0, 0, NULL);
1198 void
1199 ui_schedule_redraw(void)
1201 handle_resize_nodelay(0, 0, NULL);
1204 void
1205 ui_require_input(struct tab *tab, int hide)
1207 /* TODO: hard-switching to another tab is ugly */
1208 switch_to_tab(tab);
1210 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1211 &ir_history, NULL, NULL);
1212 strlcpy(ministate.prompt, "Input required: ",
1213 sizeof(ministate.prompt));
1214 redraw_tab(tab);
1217 void
1218 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1219 struct tab *data)
1221 yornp(prompt, fn, data);
1222 redraw_tab(current_tab());
1225 void
1226 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1227 struct tab *data)
1229 completing_read(prompt, fn, data, NULL, NULL);
1230 redraw_tab(current_tab());
1233 void
1234 ui_end(void)
1236 endwin();