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 "minibuffer.h"
34 #include "telescope.h"
46 static struct event stdioev, winchev;
48 static void restore_curs_x(struct buffer *);
50 static struct vline *nth_line(struct buffer*, size_t);
51 static int readkey(void);
52 static void dispatch_stdio(int, short, void*);
53 static void handle_clear_echoarea(int, short, void*);
54 static void handle_resize(int, short, void*);
55 static void handle_resize_nodelay(int, short, void*);
56 static int wrap_page(struct buffer*, int);
57 static void print_vline(int, int, WINDOW*, struct vline*);
58 static void redraw_tabline(void);
59 static void redraw_window(WINDOW*, int, int, struct buffer*);
60 static void redraw_help(void);
61 static void redraw_body(struct tab*);
62 static void redraw_modeline(struct tab*);
63 static void redraw_echoarea(void);
64 static void redraw_tab(struct tab*);
65 static void emit_help_item(char*, void*);
66 static void rec_compute_help(struct kmap*, char*, size_t);
67 static void recompute_help(void);
68 static void update_loading_anim(int, short, void*);
69 static void stop_loading_anim(struct tab*);
73 struct thiskey thiskey;
75 static struct event resizeev;
76 static struct timeval resize_timer = { 0, 250000 };
78 static WINDOW *tabline, *body, *modeline, *echoarea;
80 int body_lines, body_cols;
83 static struct buffer helpwin;
84 static int help_lines, help_cols;
86 static int side_window;
88 static struct event clechoev;
89 static struct timeval clechoev_timer = { 5, 0 };
90 static struct timeval loadingev_timer = { 0, 250000 };
92 static uint32_t tab_counter;
94 static char keybuf[64];
96 struct kmap global_map,
104 update_x_offset(void)
106 if (olivetti_mode && fill_column < body_cols)
107 x_offset = (body_cols - fill_column)/2;
113 save_excursion(struct excursion *place, struct buffer *buffer)
115 place->curs_x = buffer->curs_x;
116 place->curs_y = buffer->curs_y;
117 place->line_off = buffer->line_off;
118 place->current_line = buffer->current_line;
119 place->cpoff = buffer->cpoff;
123 restore_excursion(struct excursion *place, struct buffer *buffer)
125 buffer->curs_x = place->curs_x;
126 buffer->curs_y = place->curs_y;
127 buffer->line_off = place->line_off;
128 buffer->current_line = place->current_line;
129 buffer->cpoff = place->cpoff;
133 restore_curs_x(struct buffer *buffer)
138 vl = buffer->current_line;
139 if (vl == NULL || vl->line == NULL)
140 buffer->curs_x = buffer->cpoff = 0;
142 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
144 buffer->curs_x += x_offset;
147 prfx = line_prefixes[vl->parent->type].prfx1;
148 buffer->curs_x += utf8_swidth(prfx);
153 global_key_unbound(void)
155 message("%s is undefined", keybuf);
158 static struct vline *
159 nth_line(struct buffer *buffer, size_t n)
165 TAILQ_FOREACH(vl, &buffer->head, vlines) {
180 TAILQ_FOREACH(t, &tabshead, tabs) {
181 if (t->flags & TAB_CURRENT)
193 return &ministate.buffer;
194 return ¤t_tab()->buffer;
202 if ((thiskey.key = wgetch(body)) == ERR)
205 thiskey.meta = thiskey.key == 27;
207 thiskey.key = wgetch(body);
208 if (thiskey.key == ERR || thiskey.key == 27) {
215 if ((unsigned int)thiskey.key < UINT8_MAX) {
217 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
219 if ((thiskey.key = wgetch(body)) == ERR) {
220 message("Error decoding user input");
230 dispatch_stdio(int fd, short ev, void *d)
239 if (keybuf[0] != '\0')
240 strlcat(keybuf, " ", sizeof(keybuf));
242 strlcat(keybuf, "M-", sizeof(keybuf));
243 if (thiskey.cp != 0) {
244 utf8_encode(thiskey.cp, tmp);
245 strlcat(keybuf, tmp, sizeof(keybuf));
247 if ((keyname = unkbd(thiskey.key)) != NULL)
248 strlcat(keybuf, keyname, sizeof(keybuf));
250 tmp[0] = thiskey.key;
251 strlcat(keybuf, tmp, sizeof(keybuf));
255 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
256 if (k->meta == thiskey.meta &&
257 k->key == thiskey.key) {
259 current_map = &k->map;
261 current_map = base_map;
262 strlcpy(keybuf, "", sizeof(keybuf));
263 k->fn(current_buffer());
269 if (current_map->unhandled_input != NULL)
270 current_map->unhandled_input();
272 global_key_unbound();
274 strlcpy(keybuf, "", sizeof(keybuf));
275 current_map = base_map;
281 redraw_tab(current_tab());
285 handle_clear_echoarea(int fd, short ev, void *d)
287 free(ministate.curmesg);
288 ministate.curmesg = NULL;
301 handle_resize(int sig, short ev, void *d)
303 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
304 event_del(&resizeev);
306 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
307 evtimer_add(&resizeev, &resize_timer);
311 handle_resize_nodelay(int s, short ev, void *d)
319 /* move and resize the windows, in reverse order! */
321 mvwin(echoarea, LINES-1, 0);
322 wresize(echoarea, 1, COLS);
324 mvwin(modeline, LINES-2, 0);
325 wresize(modeline, 1, COLS);
327 body_lines = LINES-3;
331 help_cols = 0.3 * COLS;
332 help_lines = LINES-3;
334 wresize(help, help_lines, help_cols);
336 wrap_page(&helpwin, help_cols);
338 body_cols = COLS - help_cols - 1;
339 mvwin(body, 1, help_cols);
344 wresize(body, body_lines, body_cols);
346 wresize(tabline, 1, COLS);
350 wrap_page(&tab->buffer, body_cols);
355 wrap_page(struct buffer *buffer, int width)
358 const struct line *top_orig, *orig;
363 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
364 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
366 buffer->top_line = NULL;
367 buffer->current_line = NULL;
369 buffer->force_redraw = 1;
371 buffer->line_off = 0;
375 TAILQ_FOREACH(l, &buffer->page.head, lines) {
376 prfx = line_prefixes[l->type].prfx1;
387 wrap_text(buffer, prfx, l, MIN(fill_column, width));
389 case LINE_PRE_CONTENT:
391 pre_width = MIN(fill_column, width);
394 hardwrap_text(buffer, l, pre_width);
398 if (top_orig == l && buffer->top_line == NULL) {
399 buffer->line_off = buffer->line_max-1;
400 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
403 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
404 if (vl == NULL || vl->parent != orig)
406 buffer->top_line = vl;
411 if (orig == l && buffer->current_line == NULL) {
412 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
415 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
416 if (vl == NULL || vl->parent != orig)
418 buffer->current_line = vl;
423 if (buffer->current_line == NULL)
424 buffer->current_line = TAILQ_FIRST(&buffer->head);
426 if (buffer->top_line == NULL)
427 buffer->top_line = buffer->current_line;
433 print_vline(int off, int width, WINDOW *window, struct vline *vl)
440 f = &line_faces[vl->parent->type];
442 /* unused, set by getyx */
446 prfx = line_prefixes[vl->parent->type].prfx1;
448 prfx = line_prefixes[vl->parent->type].prfx2;
454 wattr_on(window, body_face.left, NULL);
455 for (i = 0; i < off; i++)
457 wattr_off(window, body_face.left, NULL);
459 wattr_on(window, f->prefix, NULL);
460 wprintw(window, "%s", prfx);
461 wattr_off(window, f->prefix, NULL);
463 wattr_on(window, f->text, NULL);
464 wprintw(window, "%s", text);
465 wattr_off(window, f->text, NULL);
471 wattr_on(window, f->trail, NULL);
472 for (i = 0; i < left - off; ++i)
474 wattr_off(window, f->trail, NULL);
476 wattr_on(window, body_face.right, NULL);
477 for (i = 0; i < off; i++)
479 wattr_off(window, body_face.right, NULL);
487 size_t toskip, ots, tabwidth, space, x;
488 int current, y, truncated;
494 /* unused, but setted by a getyx */
497 tabwidth = sizeof(buf)+1;
501 TAILQ_FOREACH(tab, &tabshead, tabs) {
503 if (tab->flags & TAB_CURRENT)
507 if (toskip * tabwidth < space)
512 while (toskip != 0 &&
513 (ots - toskip+1) * tabwidth < space)
518 wattr_on(tabline, tab_face.background, NULL);
519 wprintw(tabline, toskip == 0 ? " " : "<");
520 wattr_off(tabline, tab_face.background, NULL);
523 TAILQ_FOREACH(tab, &tabshead, tabs) {
531 getyx(tabline, y, x);
532 if (x + sizeof(buf)+2 >= (size_t)COLS)
535 current = tab->flags & TAB_CURRENT;
537 if (*(title = tab->buffer.page.title) == '\0')
538 title = tab->hist_cur->h;
540 if (tab->flags & TAB_URGENT)
541 strlcpy(buf, "!", sizeof(buf));
543 strlcpy(buf, " ", sizeof(buf));
545 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
546 /* truncation happens */
547 strlcpy(&buf[sizeof(buf)-4], "...", 4);
549 /* pad with spaces */
550 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
555 wattr_on(tabline, tab_face.current, NULL);
557 wattr_on(tabline, tab_face.tab, NULL);
559 wprintw(tabline, "%s", buf);
560 if (TAILQ_NEXT(tab, tabs) != NULL)
561 wprintw(tabline, " ");
564 wattr_off(tabline, tab_face.current, NULL);
566 wattr_off(tabline, tab_face.tab, NULL);
569 wattr_on(tabline, tab_face.background, NULL);
570 for (; x < (size_t)COLS; ++x)
571 waddch(tabline, ' ');
573 mvwprintw(tabline, 0, COLS-1, ">");
574 wattr_off(tabline, tab_face.background, NULL);
578 * Compute the first visible line around vl. Try to search forward
579 * until the end of the buffer; if a visible line is not found, search
580 * backward. Return NULL if no viable line was found.
582 static inline struct vline *
583 adjust_line(struct vline *vl, struct buffer *buffer)
587 if (!(vl->parent->flags & L_HIDDEN))
592 t != NULL && t->parent->flags & L_HIDDEN;
593 t = TAILQ_NEXT(t, vlines))
599 /* search backward */
601 t != NULL && t->parent->flags & L_HIDDEN;
602 t = TAILQ_PREV(t, vhead, vlines))
609 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
614 restore_curs_x(buffer);
617 * TODO: ignoring buffer->force_update and always
618 * re-rendering. In theory we can recompute the y position
619 * without a re-render, and optimize here. It's not the only
620 * optimisation possible here, wscrl wolud also be an
628 if (TAILQ_EMPTY(&buffer->head))
631 buffer->top_line = adjust_line(buffer->top_line, buffer);
632 if (buffer->top_line == NULL)
635 buffer->current_line = adjust_line(buffer->current_line, buffer);
639 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
640 if (vl->parent->flags & L_HIDDEN)
644 print_vline(x_offset, width, win, vl);
646 if (vl == buffer->current_line)
658 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
659 if (vl == buffer->current_line)
661 if (vl->parent->flags & L_HIDDEN)
664 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
670 buffer->last_line_off = buffer->line_off;
671 buffer->force_redraw = 0;
673 wmove(win, buffer->curs_y, buffer->curs_x);
679 redraw_window(help, help_lines, help_cols, &helpwin);
683 redraw_body(struct tab *tab)
685 static struct tab *last_tab;
688 tab->buffer.force_redraw =1;
691 redraw_window(body, body_lines, body_cols, &tab->buffer);
695 trust_status_char(enum trust_state ts)
698 case TS_UNKNOWN: return 'u';
699 case TS_UNTRUSTED: return '!';
700 case TS_TEMP_TRUSTED: return '!';
701 case TS_TRUSTED: return 'v';
702 case TS_VERIFIED: return 'V';
708 redraw_modeline(struct tab *tab)
711 int x, y, max_x, max_y;
712 const char *mode = tab->buffer.page.name;
713 const char *spin = "-\\|/";
716 wattr_on(modeline, modeline_face.background, NULL);
717 wmove(modeline, 0, 0);
719 wprintw(modeline, "-%c%c %s ",
720 spin[tab->loading_anim_step],
721 trust_status_char(tab->trust),
722 mode == NULL ? "(none)" : mode);
724 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
726 if (tab->buffer.line_max <= (size_t)body_lines)
727 wprintw(modeline, "All ");
728 else if (tab->buffer.line_off == 0)
729 wprintw(modeline, "Top ");
730 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
731 wprintw(modeline, "Bottom ");
733 wprintw(modeline, "%.0f%% ", pct);
735 wprintw(modeline, "%d/%d %s ",
736 tab->buffer.line_off + tab->buffer.curs_y,
737 tab->buffer.line_max,
740 getyx(modeline, y, x);
741 getmaxyx(modeline, max_y, max_x);
746 for (; x < max_x; ++x)
747 waddstr(modeline, "-");
749 wattr_off(modeline, modeline_face.background, NULL);
753 redraw_echoarea(void)
756 size_t off_y, off_x = 0;
757 char *start = NULL, *c = NULL;
759 /* unused, but set by getyx */
762 wattr_on(echoarea, minibuffer_face.background, NULL);
766 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
767 if (ministate.hist_cur != NULL)
768 wprintw(echoarea, "(%zu/%zu) ",
769 ministate.hist_off + 1,
770 ministate.history->len);
772 getyx(echoarea, off_y, off_x);
774 start = ministate.hist_cur != NULL
775 ? ministate.hist_cur->h
777 c = utf8_nth(ministate.buffer.current_line->line,
778 ministate.buffer.cpoff);
779 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
780 start = utf8_next_cp(start);
783 waddstr(echoarea, start);
786 if (ministate.curmesg != NULL)
787 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
790 if (!in_minibuffer && ministate.curmesg == NULL)
791 waddstr(echoarea, keybuf);
793 /* If nothing else, show the URL at point */
794 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
796 if (tab->buffer.current_line != NULL &&
797 tab->buffer.current_line->parent->type == LINE_LINK)
798 waddstr(echoarea, tab->buffer.current_line->parent->alt);
802 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
804 wattr_off(echoarea, minibuffer_face.background, NULL);
808 redraw_tab(struct tab *tab)
817 redraw_modeline(tab);
820 wnoutrefresh(tabline);
821 wnoutrefresh(modeline);
825 wnoutrefresh(echoarea);
827 wnoutrefresh(echoarea);
835 emit_help_item(char *prfx, void *fn)
840 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
846 if ((l = calloc(1, sizeof(*l))) == NULL)
852 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
854 if (TAILQ_EMPTY(&helpwin.page.head))
855 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
857 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
861 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
867 TAILQ_FOREACH(k, &keymap->m, keymaps) {
868 strlcpy(p, prfx, sizeof(p));
870 strlcat(p, " ", sizeof(p));
872 strlcat(p, "M-", sizeof(p));
873 if ((kn = unkbd(k->key)) != NULL)
874 strlcat(p, kn, sizeof(p));
876 strlcat(p, keyname(k->key), sizeof(p));
879 rec_compute_help(&k->map, p, sizeof(p));
881 emit_help_item(p, k->fn);
890 empty_vlist(&helpwin);
891 empty_linelist(&helpwin);
892 rec_compute_help(current_map, p, sizeof(p));
893 wrap_page(&helpwin, help_cols);
897 vmessage(const char *fmt, va_list ap)
899 if (evtimer_pending(&clechoev, NULL))
900 evtimer_del(&clechoev);
902 free(ministate.curmesg);
903 ministate.curmesg = NULL;
906 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
907 evtimer_add(&clechoev, &clechoev_timer);
909 /* TODO: what to do if the allocation fails here? */
910 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
911 ministate.curmesg = NULL;
925 message(const char *fmt, ...)
935 start_loading_anim(struct tab *tab)
937 if (tab->loading_anim)
939 tab->loading_anim = 1;
940 evtimer_set(&tab->loadingev, update_loading_anim, tab);
941 evtimer_add(&tab->loadingev, &loadingev_timer);
945 update_loading_anim(int fd, short ev, void *d)
949 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
951 if (tab->flags & TAB_CURRENT) {
952 redraw_modeline(tab);
959 evtimer_add(&tab->loadingev, &loadingev_timer);
963 stop_loading_anim(struct tab *tab)
965 if (!tab->loading_anim)
967 evtimer_del(&tab->loadingev);
968 tab->loading_anim = 0;
969 tab->loading_anim_step = 0;
971 if (!(tab->flags & TAB_CURRENT))
974 redraw_modeline(tab);
983 load_url_in_tab(struct tab *tab, const char *url)
985 message("Loading %s...", url);
986 start_loading_anim(tab);
989 tab->buffer.curs_x = 0;
990 tab->buffer.curs_y = 0;
995 switch_to_tab(struct tab *tab)
999 TAILQ_FOREACH(t, &tabshead, tabs) {
1000 t->flags &= ~TAB_CURRENT;
1003 tab->flags |= TAB_CURRENT;
1004 tab->flags &= ~TAB_URGENT;
1010 return tab_counter++;
1014 new_tab(const char *url)
1018 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1024 TAILQ_INIT(&tab->hist.head);
1026 TAILQ_INIT(&tab->buffer.head);
1028 tab->id = tab_new_id();
1031 if (TAILQ_EMPTY(&tabshead))
1032 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1034 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1036 load_url_in_tab(tab, url);
1043 setlocale(LC_ALL, "");
1045 TAILQ_INIT(&eecmd_history.head);
1046 TAILQ_INIT(&ir_history.head);
1047 TAILQ_INIT(&lu_history.head);
1049 ministate.line.type = LINE_TEXT;
1050 ministate.vline.parent = &ministate.line;
1051 ministate.buffer.current_line = &ministate.vline;
1053 /* initialize help window */
1054 TAILQ_INIT(&helpwin.head);
1056 base_map = &global_map;
1057 current_map = &global_map;
1061 if (enable_colors) {
1064 use_default_colors();
1069 config_apply_style();
1074 intrflush(stdscr, FALSE);
1076 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1078 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1080 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1082 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1084 if ((help = newwin(1, 1, 1, 0)) == NULL)
1087 body_lines = LINES-3;
1090 wbkgd(body, body_face.body);
1091 wbkgd(echoarea, minibuffer_face.background);
1096 scrollok(body, FALSE);
1098 /* non-blocking input */
1101 mvwprintw(body, 0, 0, "");
1103 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1104 event_add(&stdioev, NULL);
1106 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1107 signal_add(&winchev, NULL);
1113 ui_on_tab_loaded(struct tab *tab)
1115 stop_loading_anim(tab);
1116 message("Loaded %s", tab->hist_cur->h);
1127 ui_on_tab_refresh(struct tab *tab)
1129 wrap_page(&tab->buffer, body_cols);
1130 if (tab->flags & TAB_CURRENT)
1133 tab->flags |= TAB_URGENT;
1143 ui_toggle_side_window(void)
1145 side_window = !side_window;
1150 * ugly hack, but otherwise the window doesn't get updated
1151 * until I call handle_resize a second time (i.e. C-l). I
1152 * will be happy to know why something like this is needed.
1154 handle_resize_nodelay(0, 0, NULL);
1155 handle_resize_nodelay(0, 0, NULL);
1159 ui_schedule_redraw(void)
1161 handle_resize_nodelay(0, 0, NULL);
1165 ui_require_input(struct tab *tab, int hide)
1167 /* TODO: hard-switching to another tab is ugly */
1170 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1172 strlcpy(ministate.prompt, "Input required: ",
1173 sizeof(ministate.prompt));
1178 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1181 yornp(prompt, fn, data);
1182 redraw_tab(current_tab());
1186 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1189 completing_read(prompt, fn, data);
1190 redraw_tab(current_tab());