2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Ncurses UI for telescope.
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.
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.
33 #include "telescope.h"
45 static struct event stdioev, winchev;
47 static void restore_curs_x(struct buffer *);
49 static struct vline *nth_line(struct buffer*, size_t);
50 static int readkey(void);
51 static void dispatch_stdio(int, short, void*);
52 static void handle_clear_echoarea(int, short, void*);
53 static void handle_resize(int, short, void*);
54 static void handle_resize_nodelay(int, short, void*);
55 static int wrap_page(struct buffer*, int);
56 static void print_vline(int, int, WINDOW*, struct vline*);
57 static void redraw_tabline(void);
58 static void redraw_window(WINDOW*, int, int, struct buffer*);
59 static void redraw_help(void);
60 static void redraw_body(struct tab*);
61 static void redraw_modeline(struct tab*);
62 static void redraw_echoarea(void);
63 static void redraw_tab(struct tab*);
64 static void emit_help_item(char*, void*);
65 static void rec_compute_help(struct kmap*, char*, size_t);
66 static void recompute_help(void);
67 static void update_loading_anim(int, short, void*);
68 static void stop_loading_anim(struct tab*);
72 struct thiskey thiskey;
74 static struct event resizeev;
75 static struct timeval resize_timer = { 0, 250000 };
77 static WINDOW *tabline, *body, *modeline, *echoarea;
79 int body_lines, body_cols;
82 static struct buffer helpwin;
83 static int help_lines, help_cols;
85 static int side_window;
87 static struct event clechoev;
88 static struct timeval clechoev_timer = { 5, 0 };
89 static struct timeval loadingev_timer = { 0, 250000 };
91 static uint32_t tab_counter;
93 static char keybuf[64];
95 struct kmap global_map,
103 update_x_offset(void)
105 if (olivetti_mode && fill_column < body_cols)
106 x_offset = (body_cols - fill_column)/2;
112 save_excursion(struct excursion *place, struct buffer *buffer)
114 place->curs_x = buffer->curs_x;
115 place->curs_y = buffer->curs_y;
116 place->line_off = buffer->line_off;
117 place->current_line = buffer->current_line;
118 place->cpoff = buffer->cpoff;
122 restore_excursion(struct excursion *place, struct buffer *buffer)
124 buffer->curs_x = place->curs_x;
125 buffer->curs_y = place->curs_y;
126 buffer->line_off = place->line_off;
127 buffer->current_line = place->current_line;
128 buffer->cpoff = place->cpoff;
132 restore_curs_x(struct buffer *buffer)
137 vl = buffer->current_line;
138 if (vl == NULL || vl->line == NULL)
139 buffer->curs_x = buffer->cpoff = 0;
141 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
143 buffer->curs_x += x_offset;
146 prfx = line_prefixes[vl->parent->type].prfx1;
147 buffer->curs_x += utf8_swidth(prfx);
152 global_key_unbound(void)
154 message("%s is undefined", keybuf);
157 static struct vline *
158 nth_line(struct buffer *buffer, size_t n)
164 TAILQ_FOREACH(vl, &buffer->head, vlines) {
179 TAILQ_FOREACH(t, &tabshead, tabs) {
180 if (t->flags & TAB_CURRENT)
192 return &ministate.buffer;
193 return ¤t_tab()->buffer;
201 if ((thiskey.key = wgetch(body)) == ERR)
204 thiskey.meta = thiskey.key == 27;
206 thiskey.key = wgetch(body);
207 if (thiskey.key == ERR || thiskey.key == 27) {
214 if ((unsigned int)thiskey.key < UINT8_MAX) {
216 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
218 if ((thiskey.key = wgetch(body)) == ERR) {
219 message("Error decoding user input");
229 dispatch_stdio(int fd, short ev, void *d)
238 if (keybuf[0] != '\0')
239 strlcat(keybuf, " ", sizeof(keybuf));
241 strlcat(keybuf, "M-", sizeof(keybuf));
242 if (thiskey.cp != 0) {
243 utf8_encode(thiskey.cp, tmp);
244 strlcat(keybuf, tmp, sizeof(keybuf));
246 if ((keyname = unkbd(thiskey.key)) != NULL)
247 strlcat(keybuf, keyname, sizeof(keybuf));
249 tmp[0] = thiskey.key;
250 strlcat(keybuf, tmp, sizeof(keybuf));
254 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
255 if (k->meta == thiskey.meta &&
256 k->key == thiskey.key) {
258 current_map = &k->map;
260 current_map = base_map;
261 strlcpy(keybuf, "", sizeof(keybuf));
262 k->fn(current_buffer());
268 if (current_map->unhandled_input != NULL)
269 current_map->unhandled_input();
271 global_key_unbound();
273 strlcpy(keybuf, "", sizeof(keybuf));
274 current_map = base_map;
280 redraw_tab(current_tab());
284 handle_clear_echoarea(int fd, short ev, void *d)
286 free(ministate.curmesg);
287 ministate.curmesg = NULL;
300 handle_resize(int sig, short ev, void *d)
302 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
303 event_del(&resizeev);
305 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
306 evtimer_add(&resizeev, &resize_timer);
310 handle_resize_nodelay(int s, short ev, void *d)
318 /* move and resize the windows, in reverse order! */
320 mvwin(echoarea, LINES-1, 0);
321 wresize(echoarea, 1, COLS);
323 mvwin(modeline, LINES-2, 0);
324 wresize(modeline, 1, COLS);
326 body_lines = LINES-3;
330 help_cols = 0.3 * COLS;
331 help_lines = LINES-3;
333 wresize(help, help_lines, help_cols);
335 wrap_page(&helpwin, help_cols);
337 body_cols = COLS - help_cols - 1;
338 mvwin(body, 1, help_cols);
343 wresize(body, body_lines, body_cols);
345 wresize(tabline, 1, COLS);
349 wrap_page(&tab->buffer, body_cols);
354 wrap_page(struct buffer *buffer, int width)
357 const struct line *top_orig, *orig;
362 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
363 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
365 buffer->top_line = NULL;
366 buffer->current_line = NULL;
368 buffer->force_redraw = 1;
370 buffer->line_off = 0;
374 TAILQ_FOREACH(l, &buffer->page.head, lines) {
375 prfx = line_prefixes[l->type].prfx1;
386 wrap_text(buffer, prfx, l, MIN(fill_column, width));
388 case LINE_PRE_CONTENT:
390 pre_width = MIN(fill_column, width);
393 hardwrap_text(buffer, l, pre_width);
397 if (top_orig == l && buffer->top_line == NULL) {
398 buffer->line_off = buffer->line_max-1;
399 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
402 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
403 if (vl == NULL || vl->parent != orig)
405 buffer->top_line = vl;
410 if (orig == l && buffer->current_line == NULL) {
411 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
414 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
415 if (vl == NULL || vl->parent != orig)
417 buffer->current_line = vl;
422 if (buffer->current_line == NULL)
423 buffer->current_line = TAILQ_FIRST(&buffer->head);
425 if (buffer->top_line == NULL)
426 buffer->top_line = buffer->current_line;
432 print_vline(int off, int width, WINDOW *window, struct vline *vl)
439 f = &line_faces[vl->parent->type];
441 /* unused, set by getyx */
445 prfx = line_prefixes[vl->parent->type].prfx1;
447 prfx = line_prefixes[vl->parent->type].prfx2;
453 wattr_on(window, body_face.left, NULL);
454 for (i = 0; i < off; i++)
456 wattr_off(window, body_face.left, NULL);
458 wattr_on(window, f->prefix, NULL);
459 wprintw(window, "%s", prfx);
460 wattr_off(window, f->prefix, NULL);
462 wattr_on(window, f->text, NULL);
463 wprintw(window, "%s", text);
464 wattr_off(window, f->text, NULL);
470 wattr_on(window, f->trail, NULL);
471 for (i = 0; i < left - off; ++i)
473 wattr_off(window, f->trail, NULL);
475 wattr_on(window, body_face.right, NULL);
476 for (i = 0; i < off; i++)
478 wattr_off(window, body_face.right, NULL);
486 size_t toskip, ots, tabwidth, space, x;
487 int current, y, truncated;
493 /* unused, but setted by a getyx */
496 tabwidth = sizeof(buf)+1;
500 TAILQ_FOREACH(tab, &tabshead, tabs) {
502 if (tab->flags & TAB_CURRENT)
506 if (toskip * tabwidth < space)
511 while (toskip != 0 &&
512 (ots - toskip+1) * tabwidth < space)
517 wattr_on(tabline, tab_face.background, NULL);
518 wprintw(tabline, toskip == 0 ? " " : "<");
519 wattr_off(tabline, tab_face.background, NULL);
522 TAILQ_FOREACH(tab, &tabshead, tabs) {
530 getyx(tabline, y, x);
531 if (x + sizeof(buf)+2 >= (size_t)COLS)
534 current = tab->flags & TAB_CURRENT;
536 if (*(title = tab->buffer.page.title) == '\0')
537 title = tab->hist_cur->h;
539 if (tab->flags & TAB_URGENT)
540 strlcpy(buf, "!", sizeof(buf));
542 strlcpy(buf, " ", sizeof(buf));
544 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
545 /* truncation happens */
546 strlcpy(&buf[sizeof(buf)-4], "...", 4);
548 /* pad with spaces */
549 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
554 wattr_on(tabline, tab_face.current, NULL);
556 wattr_on(tabline, tab_face.tab, NULL);
558 wprintw(tabline, "%s", buf);
559 if (TAILQ_NEXT(tab, tabs) != NULL)
560 wprintw(tabline, " ");
563 wattr_off(tabline, tab_face.current, NULL);
565 wattr_off(tabline, tab_face.tab, NULL);
568 wattr_on(tabline, tab_face.background, NULL);
569 for (; x < (size_t)COLS; ++x)
570 waddch(tabline, ' ');
572 mvwprintw(tabline, 0, COLS-1, ">");
573 wattr_off(tabline, tab_face.background, NULL);
577 * Compute the first visible line around vl. Try to search forward
578 * until the end of the buffer; if a visible line is not found, search
579 * backward. Return NULL if no viable line was found.
581 static inline struct vline *
582 adjust_line(struct vline *vl, struct buffer *buffer)
586 if (!(vl->parent->flags & L_HIDDEN))
591 t != NULL && t->parent->flags & L_HIDDEN;
592 t = TAILQ_NEXT(t, vlines))
598 /* search backward */
600 t != NULL && t->parent->flags & L_HIDDEN;
601 t = TAILQ_PREV(t, vhead, vlines))
608 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
613 restore_curs_x(buffer);
616 * TODO: ignoring buffer->force_update and always
617 * re-rendering. In theory we can recompute the y position
618 * without a re-render, and optimize here. It's not the only
619 * optimisation possible here, wscrl wolud also be an
627 if (TAILQ_EMPTY(&buffer->head))
630 buffer->top_line = adjust_line(buffer->top_line, buffer);
631 if (buffer->top_line == NULL)
634 buffer->current_line = adjust_line(buffer->current_line, buffer);
638 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
639 if (vl->parent->flags & L_HIDDEN)
643 print_vline(x_offset, width, win, vl);
645 if (vl == buffer->current_line)
657 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
658 if (vl == buffer->current_line)
660 if (vl->parent->flags & L_HIDDEN)
663 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
669 buffer->last_line_off = buffer->line_off;
670 buffer->force_redraw = 0;
672 wmove(win, buffer->curs_y, buffer->curs_x);
678 redraw_window(help, help_lines, help_cols, &helpwin);
682 redraw_body(struct tab *tab)
684 static struct tab *last_tab;
687 tab->buffer.force_redraw =1;
690 redraw_window(body, body_lines, body_cols, &tab->buffer);
694 trust_status_char(enum trust_state ts)
697 case TS_UNKNOWN: return 'u';
698 case TS_UNTRUSTED: return '!';
699 case TS_TEMP_TRUSTED: return '!';
700 case TS_TRUSTED: return 'v';
701 case TS_VERIFIED: return 'V';
707 redraw_modeline(struct tab *tab)
710 int x, y, max_x, max_y;
711 const char *mode = tab->buffer.page.name;
712 const char *spin = "-\\|/";
715 wattr_on(modeline, modeline_face.background, NULL);
716 wmove(modeline, 0, 0);
718 wprintw(modeline, "-%c%c %s ",
719 spin[tab->loading_anim_step],
720 trust_status_char(tab->trust),
721 mode == NULL ? "(none)" : mode);
723 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
725 if (tab->buffer.line_max <= (size_t)body_lines)
726 wprintw(modeline, "All ");
727 else if (tab->buffer.line_off == 0)
728 wprintw(modeline, "Top ");
729 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
730 wprintw(modeline, "Bottom ");
732 wprintw(modeline, "%.0f%% ", pct);
734 wprintw(modeline, "%d/%d %s ",
735 tab->buffer.line_off + tab->buffer.curs_y,
736 tab->buffer.line_max,
739 getyx(modeline, y, x);
740 getmaxyx(modeline, max_y, max_x);
745 for (; x < max_x; ++x)
746 waddstr(modeline, "-");
748 wattr_off(modeline, modeline_face.background, NULL);
752 redraw_echoarea(void)
755 size_t off_y, off_x = 0;
756 char *start = NULL, *c = NULL;
758 /* unused, but set by getyx */
761 wattr_on(echoarea, minibuffer_face.background, NULL);
765 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
766 if (ministate.hist_cur != NULL)
767 wprintw(echoarea, "(%zu/%zu) ",
768 ministate.hist_off + 1,
769 ministate.history->len);
771 getyx(echoarea, off_y, off_x);
773 start = ministate.hist_cur != NULL
774 ? ministate.hist_cur->h
776 c = utf8_nth(ministate.buffer.current_line->line,
777 ministate.buffer.cpoff);
778 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
779 start = utf8_next_cp(start);
782 waddstr(echoarea, start);
785 if (ministate.curmesg != NULL)
786 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
789 if (!in_minibuffer && ministate.curmesg == NULL)
790 waddstr(echoarea, keybuf);
792 /* If nothing else, show the URL at point */
793 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
795 if (tab->buffer.current_line != NULL &&
796 tab->buffer.current_line->parent->type == LINE_LINK)
797 waddstr(echoarea, tab->buffer.current_line->parent->alt);
801 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
803 wattr_off(echoarea, minibuffer_face.background, NULL);
807 redraw_tab(struct tab *tab)
816 redraw_modeline(tab);
819 wnoutrefresh(tabline);
820 wnoutrefresh(modeline);
824 wnoutrefresh(echoarea);
826 wnoutrefresh(echoarea);
834 emit_help_item(char *prfx, void *fn)
839 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
845 if ((l = calloc(1, sizeof(*l))) == NULL)
851 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
853 if (TAILQ_EMPTY(&helpwin.page.head))
854 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
856 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
860 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
866 TAILQ_FOREACH(k, &keymap->m, keymaps) {
867 strlcpy(p, prfx, sizeof(p));
869 strlcat(p, " ", sizeof(p));
871 strlcat(p, "M-", sizeof(p));
872 if ((kn = unkbd(k->key)) != NULL)
873 strlcat(p, kn, sizeof(p));
875 strlcat(p, keyname(k->key), sizeof(p));
878 rec_compute_help(&k->map, p, sizeof(p));
880 emit_help_item(p, k->fn);
889 empty_vlist(&helpwin);
890 empty_linelist(&helpwin);
891 rec_compute_help(current_map, p, sizeof(p));
892 wrap_page(&helpwin, help_cols);
896 vmessage(const char *fmt, va_list ap)
898 if (evtimer_pending(&clechoev, NULL))
899 evtimer_del(&clechoev);
901 free(ministate.curmesg);
902 ministate.curmesg = NULL;
905 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
906 evtimer_add(&clechoev, &clechoev_timer);
908 /* TODO: what to do if the allocation fails here? */
909 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
910 ministate.curmesg = NULL;
924 message(const char *fmt, ...)
934 start_loading_anim(struct tab *tab)
936 if (tab->loading_anim)
938 tab->loading_anim = 1;
939 evtimer_set(&tab->loadingev, update_loading_anim, tab);
940 evtimer_add(&tab->loadingev, &loadingev_timer);
944 update_loading_anim(int fd, short ev, void *d)
948 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
950 if (tab->flags & TAB_CURRENT) {
951 redraw_modeline(tab);
958 evtimer_add(&tab->loadingev, &loadingev_timer);
962 stop_loading_anim(struct tab *tab)
964 if (!tab->loading_anim)
966 evtimer_del(&tab->loadingev);
967 tab->loading_anim = 0;
968 tab->loading_anim_step = 0;
970 if (!(tab->flags & TAB_CURRENT))
973 redraw_modeline(tab);
982 load_url_in_tab(struct tab *tab, const char *url)
984 message("Loading %s...", url);
985 start_loading_anim(tab);
988 tab->buffer.curs_x = 0;
989 tab->buffer.curs_y = 0;
994 switch_to_tab(struct tab *tab)
998 TAILQ_FOREACH(t, &tabshead, tabs) {
999 t->flags &= ~TAB_CURRENT;
1002 tab->flags |= TAB_CURRENT;
1003 tab->flags &= ~TAB_URGENT;
1009 return tab_counter++;
1013 new_tab(const char *url)
1017 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1023 TAILQ_INIT(&tab->hist.head);
1025 TAILQ_INIT(&tab->buffer.head);
1027 tab->id = tab_new_id();
1030 if (TAILQ_EMPTY(&tabshead))
1031 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1033 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1035 load_url_in_tab(tab, url);
1042 setlocale(LC_ALL, "");
1044 TAILQ_INIT(&eecmd_history.head);
1045 TAILQ_INIT(&ir_history.head);
1046 TAILQ_INIT(&lu_history.head);
1048 ministate.line.type = LINE_TEXT;
1049 ministate.vline.parent = &ministate.line;
1050 ministate.buffer.current_line = &ministate.vline;
1052 /* initialize help window */
1053 TAILQ_INIT(&helpwin.head);
1055 base_map = &global_map;
1056 current_map = &global_map;
1060 if (enable_colors) {
1063 use_default_colors();
1068 config_apply_style();
1073 intrflush(stdscr, FALSE);
1075 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1077 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1079 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1081 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1083 if ((help = newwin(1, 1, 1, 0)) == NULL)
1086 body_lines = LINES-3;
1089 wbkgd(body, body_face.body);
1090 wbkgd(echoarea, minibuffer_face.background);
1095 scrollok(body, FALSE);
1097 /* non-blocking input */
1100 mvwprintw(body, 0, 0, "");
1102 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1103 event_add(&stdioev, NULL);
1105 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1106 signal_add(&winchev, NULL);
1112 ui_on_tab_loaded(struct tab *tab)
1114 stop_loading_anim(tab);
1115 message("Loaded %s", tab->hist_cur->h);
1126 ui_on_tab_refresh(struct tab *tab)
1128 wrap_page(&tab->buffer, body_cols);
1129 if (tab->flags & TAB_CURRENT)
1132 tab->flags |= TAB_URGENT;
1142 ui_toggle_side_window(void)
1144 side_window = !side_window;
1149 * ugly hack, but otherwise the window doesn't get updated
1150 * until I call handle_resize a second time (i.e. C-l). I
1151 * will be happy to know why something like this is needed.
1153 handle_resize_nodelay(0, 0, NULL);
1154 handle_resize_nodelay(0, 0, NULL);
1158 ui_schedule_redraw(void)
1160 handle_resize_nodelay(0, 0, NULL);
1164 ui_require_input(struct tab *tab, int hide)
1166 /* TODO: hard-switching to another tab is ugly */
1169 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1171 strlcpy(ministate.prompt, "Input required: ",
1172 sizeof(ministate.prompt));
1177 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1180 yornp(prompt, fn, data);
1181 redraw_tab(current_tab());
1185 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1188 completing_read(prompt, fn, data);
1189 redraw_tab(current_tab());