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.
43 #include "minibuffer.h"
44 #include "telescope.h"
48 static struct event stdioev, winchev;
50 static void restore_curs_x(struct buffer *);
52 static struct vline *nth_line(struct buffer*, size_t);
53 static int readkey(void);
54 static void dispatch_stdio(int, short, void*);
55 static void handle_clear_echoarea(int, short, void*);
56 static void handle_resize(int, short, void*);
57 static void handle_resize_nodelay(int, short, void*);
58 static int wrap_page(struct buffer*, int);
59 static void print_vline(int, int, WINDOW*, struct vline*);
60 static void redraw_tabline(void);
61 static void redraw_window(WINDOW*, int, int, struct buffer*);
62 static void redraw_help(void);
63 static void redraw_body(struct tab*);
64 static void redraw_modeline(struct tab*);
65 static void redraw_echoarea(void);
66 static void redraw_tab(struct tab*);
67 static void emit_help_item(char*, void*);
68 static void rec_compute_help(struct kmap*, char*, size_t);
69 static void recompute_help(void);
70 static void update_loading_anim(int, short, void*);
71 static void stop_loading_anim(struct tab*);
75 struct thiskey thiskey;
77 static struct event resizeev;
78 static struct timeval resize_timer = { 0, 250000 };
80 static WINDOW *tabline, *body, *modeline, *echoarea;
82 int body_lines, body_cols;
85 static struct buffer helpwin;
86 static int help_lines, help_cols;
88 static int side_window;
90 static struct event clechoev;
91 static struct timeval clechoev_timer = { 5, 0 };
92 static struct timeval loadingev_timer = { 0, 250000 };
94 static uint32_t tab_counter;
96 static char keybuf[64];
98 struct kmap global_map,
106 update_x_offset(void)
108 if (olivetti_mode && fill_column < body_cols)
109 x_offset = (body_cols - fill_column)/2;
115 save_excursion(struct excursion *place, struct buffer *buffer)
117 place->curs_x = buffer->curs_x;
118 place->curs_y = buffer->curs_y;
119 place->line_off = buffer->line_off;
120 place->current_line = buffer->current_line;
121 place->cpoff = buffer->cpoff;
125 restore_excursion(struct excursion *place, struct buffer *buffer)
127 buffer->curs_x = place->curs_x;
128 buffer->curs_y = place->curs_y;
129 buffer->line_off = place->line_off;
130 buffer->current_line = place->current_line;
131 buffer->cpoff = place->cpoff;
135 restore_curs_x(struct buffer *buffer)
140 vl = buffer->current_line;
141 if (vl == NULL || vl->line == NULL)
142 buffer->curs_x = buffer->cpoff = 0;
144 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
146 buffer->curs_x += x_offset;
149 prfx = line_prefixes[vl->parent->type].prfx1;
150 buffer->curs_x += utf8_swidth(prfx);
155 global_key_unbound(void)
157 message("%s is undefined", keybuf);
160 static struct vline *
161 nth_line(struct buffer *buffer, size_t n)
167 TAILQ_FOREACH(vl, &buffer->head, vlines) {
182 TAILQ_FOREACH(t, &tabshead, tabs) {
183 if (t->flags & TAB_CURRENT)
195 return &ministate.buffer;
196 return ¤t_tab()->buffer;
204 if ((thiskey.key = wgetch(body)) == ERR)
207 thiskey.meta = thiskey.key == 27;
209 thiskey.key = wgetch(body);
210 if (thiskey.key == ERR || thiskey.key == 27) {
217 if ((unsigned int)thiskey.key < UINT8_MAX) {
219 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
221 if ((thiskey.key = wgetch(body)) == ERR) {
222 message("Error decoding user input");
232 dispatch_stdio(int fd, short ev, void *d)
241 if (keybuf[0] != '\0')
242 strlcat(keybuf, " ", sizeof(keybuf));
244 strlcat(keybuf, "M-", sizeof(keybuf));
245 if (thiskey.cp != 0) {
246 utf8_encode(thiskey.cp, tmp);
247 strlcat(keybuf, tmp, sizeof(keybuf));
249 if ((keyname = unkbd(thiskey.key)) != NULL)
250 strlcat(keybuf, keyname, sizeof(keybuf));
252 tmp[0] = thiskey.key;
253 strlcat(keybuf, tmp, sizeof(keybuf));
257 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
258 if (k->meta == thiskey.meta &&
259 k->key == thiskey.key) {
261 current_map = &k->map;
263 current_map = base_map;
264 strlcpy(keybuf, "", sizeof(keybuf));
265 k->fn(current_buffer());
271 if (current_map->unhandled_input != NULL)
272 current_map->unhandled_input();
274 global_key_unbound();
276 strlcpy(keybuf, "", sizeof(keybuf));
277 current_map = base_map;
283 redraw_tab(current_tab());
287 handle_clear_echoarea(int fd, short ev, void *d)
289 free(ministate.curmesg);
290 ministate.curmesg = NULL;
303 handle_resize(int sig, short ev, void *d)
305 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
306 event_del(&resizeev);
308 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
309 evtimer_add(&resizeev, &resize_timer);
313 handle_resize_nodelay(int s, short ev, void *d)
321 /* move and resize the windows, in reverse order! */
323 mvwin(echoarea, LINES-1, 0);
324 wresize(echoarea, 1, COLS);
326 mvwin(modeline, LINES-2, 0);
327 wresize(modeline, 1, COLS);
329 body_lines = LINES-3;
333 help_cols = 0.3 * COLS;
334 help_lines = LINES-3;
336 wresize(help, help_lines, help_cols);
338 wrap_page(&helpwin, help_cols);
340 body_cols = COLS - help_cols - 1;
341 mvwin(body, 1, help_cols);
346 wresize(body, body_lines, body_cols);
348 wresize(tabline, 1, COLS);
352 wrap_page(&tab->buffer, body_cols);
357 wrap_page(struct buffer *buffer, int width)
360 const struct line *top_orig, *orig;
365 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
366 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
368 buffer->top_line = NULL;
369 buffer->current_line = NULL;
371 buffer->force_redraw = 1;
373 buffer->line_off = 0;
377 TAILQ_FOREACH(l, &buffer->page.head, lines) {
378 prfx = line_prefixes[l->type].prfx1;
389 wrap_text(buffer, prfx, l, MIN(fill_column, width));
391 case LINE_PRE_CONTENT:
393 pre_width = MIN(fill_column, width);
396 hardwrap_text(buffer, l, pre_width);
400 if (top_orig == l && buffer->top_line == NULL) {
401 buffer->line_off = buffer->line_max-1;
402 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
405 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
406 if (vl == NULL || vl->parent != orig)
408 buffer->top_line = vl;
413 if (orig == l && buffer->current_line == NULL) {
414 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
417 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
418 if (vl == NULL || vl->parent != orig)
420 buffer->current_line = vl;
425 if (buffer->current_line == NULL)
426 buffer->current_line = TAILQ_FIRST(&buffer->head);
428 if (buffer->top_line == NULL)
429 buffer->top_line = buffer->current_line;
435 print_vline(int off, int width, WINDOW *window, struct vline *vl)
442 f = &line_faces[vl->parent->type];
444 /* unused, set by getyx */
448 prfx = line_prefixes[vl->parent->type].prfx1;
450 prfx = line_prefixes[vl->parent->type].prfx2;
456 wattr_on(window, body_face.left, NULL);
457 for (i = 0; i < off; i++)
459 wattr_off(window, body_face.left, NULL);
461 wattr_on(window, f->prefix, NULL);
462 wprintw(window, "%s", prfx);
463 wattr_off(window, f->prefix, NULL);
465 wattr_on(window, f->text, NULL);
466 wprintw(window, "%s", text);
467 wattr_off(window, f->text, NULL);
473 wattr_on(window, f->trail, NULL);
474 for (i = 0; i < left - off; ++i)
476 wattr_off(window, f->trail, NULL);
478 wattr_on(window, body_face.right, NULL);
479 for (i = 0; i < off; i++)
481 wattr_off(window, body_face.right, NULL);
489 size_t toskip, ots, tabwidth, space, x;
490 int current, y, truncated;
496 /* unused, but setted by a getyx */
499 tabwidth = sizeof(buf)+1;
503 TAILQ_FOREACH(tab, &tabshead, tabs) {
505 if (tab->flags & TAB_CURRENT)
509 if (toskip * tabwidth < space)
514 while (toskip != 0 &&
515 (ots - toskip+1) * tabwidth < space)
520 wattr_on(tabline, tab_face.background, NULL);
521 wprintw(tabline, toskip == 0 ? " " : "<");
522 wattr_off(tabline, tab_face.background, NULL);
525 TAILQ_FOREACH(tab, &tabshead, tabs) {
533 getyx(tabline, y, x);
534 if (x + sizeof(buf)+2 >= (size_t)COLS)
537 current = tab->flags & TAB_CURRENT;
539 if (*(title = tab->buffer.page.title) == '\0')
540 title = tab->hist_cur->h;
542 if (tab->flags & TAB_URGENT)
543 strlcpy(buf, "!", sizeof(buf));
545 strlcpy(buf, " ", sizeof(buf));
547 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
548 /* truncation happens */
549 strlcpy(&buf[sizeof(buf)-4], "...", 4);
551 /* pad with spaces */
552 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
557 wattr_on(tabline, tab_face.current, NULL);
559 wattr_on(tabline, tab_face.tab, NULL);
561 wprintw(tabline, "%s", buf);
562 if (TAILQ_NEXT(tab, tabs) != NULL)
563 wprintw(tabline, " ");
566 wattr_off(tabline, tab_face.current, NULL);
568 wattr_off(tabline, tab_face.tab, NULL);
571 wattr_on(tabline, tab_face.background, NULL);
572 for (; x < (size_t)COLS; ++x)
573 waddch(tabline, ' ');
575 mvwprintw(tabline, 0, COLS-1, ">");
576 wattr_off(tabline, tab_face.background, NULL);
580 * Compute the first visible line around vl. Try to search forward
581 * until the end of the buffer; if a visible line is not found, search
582 * backward. Return NULL if no viable line was found.
584 static inline struct vline *
585 adjust_line(struct vline *vl, struct buffer *buffer)
589 if (!(vl->parent->flags & L_HIDDEN))
594 t != NULL && t->parent->flags & L_HIDDEN;
595 t = TAILQ_NEXT(t, vlines))
601 /* search backward */
603 t != NULL && t->parent->flags & L_HIDDEN;
604 t = TAILQ_PREV(t, vhead, vlines))
611 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
616 restore_curs_x(buffer);
619 * TODO: ignoring buffer->force_update and always
620 * re-rendering. In theory we can recompute the y position
621 * without a re-render, and optimize here. It's not the only
622 * optimisation possible here, wscrl wolud also be an
630 if (TAILQ_EMPTY(&buffer->head))
633 buffer->top_line = adjust_line(buffer->top_line, buffer);
634 if (buffer->top_line == NULL)
637 buffer->current_line = adjust_line(buffer->current_line, buffer);
641 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
642 if (vl->parent->flags & L_HIDDEN)
646 print_vline(x_offset, width, win, vl);
648 if (vl == buffer->current_line)
660 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
661 if (vl == buffer->current_line)
663 if (vl->parent->flags & L_HIDDEN)
666 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
672 buffer->last_line_off = buffer->line_off;
673 buffer->force_redraw = 0;
675 wmove(win, buffer->curs_y, buffer->curs_x);
681 redraw_window(help, help_lines, help_cols, &helpwin);
685 redraw_body(struct tab *tab)
687 static struct tab *last_tab;
690 tab->buffer.force_redraw =1;
693 redraw_window(body, body_lines, body_cols, &tab->buffer);
697 trust_status_char(enum trust_state ts)
700 case TS_UNKNOWN: return 'u';
701 case TS_UNTRUSTED: return '!';
702 case TS_TEMP_TRUSTED: return '!';
703 case TS_TRUSTED: return 'v';
704 case TS_VERIFIED: return 'V';
710 redraw_modeline(struct tab *tab)
713 int x, y, max_x, max_y;
714 const char *mode = tab->buffer.page.name;
715 const char *spin = "-\\|/";
718 wattr_on(modeline, modeline_face.background, NULL);
719 wmove(modeline, 0, 0);
721 wprintw(modeline, "-%c%c %s ",
722 spin[tab->loading_anim_step],
723 trust_status_char(tab->trust),
724 mode == NULL ? "(none)" : mode);
726 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
728 if (tab->buffer.line_max <= (size_t)body_lines)
729 wprintw(modeline, "All ");
730 else if (tab->buffer.line_off == 0)
731 wprintw(modeline, "Top ");
732 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
733 wprintw(modeline, "Bottom ");
735 wprintw(modeline, "%.0f%% ", pct);
737 wprintw(modeline, "%d/%d %s ",
738 tab->buffer.line_off + tab->buffer.curs_y,
739 tab->buffer.line_max,
742 getyx(modeline, y, x);
743 getmaxyx(modeline, max_y, max_x);
748 for (; x < max_x; ++x)
749 waddstr(modeline, "-");
751 wattr_off(modeline, modeline_face.background, NULL);
755 redraw_echoarea(void)
758 size_t off_y, off_x = 0;
759 char *start = NULL, *c = NULL;
761 /* unused, but set by getyx */
764 wattr_on(echoarea, minibuffer_face.background, NULL);
768 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
769 if (ministate.hist_cur != NULL)
770 wprintw(echoarea, "(%zu/%zu) ",
771 ministate.hist_off + 1,
772 ministate.history->len);
774 getyx(echoarea, off_y, off_x);
776 start = ministate.hist_cur != NULL
777 ? ministate.hist_cur->h
779 c = utf8_nth(ministate.buffer.current_line->line,
780 ministate.buffer.cpoff);
781 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
782 start = utf8_next_cp(start);
785 waddstr(echoarea, start);
788 if (ministate.curmesg != NULL)
789 wprintw(echoarea, in_minibuffer ? " [%s]" : "%s",
792 if (!in_minibuffer && ministate.curmesg == NULL)
793 waddstr(echoarea, keybuf);
795 /* If nothing else, show the URL at point */
796 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
798 if (tab->buffer.current_line != NULL &&
799 tab->buffer.current_line->parent->type == LINE_LINK)
800 waddstr(echoarea, tab->buffer.current_line->parent->alt);
804 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
806 wattr_off(echoarea, minibuffer_face.background, NULL);
810 redraw_tab(struct tab *tab)
819 redraw_modeline(tab);
822 wnoutrefresh(tabline);
823 wnoutrefresh(modeline);
827 wnoutrefresh(echoarea);
829 wnoutrefresh(echoarea);
837 emit_help_item(char *prfx, void *fn)
842 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
848 if ((l = calloc(1, sizeof(*l))) == NULL)
854 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
856 if (TAILQ_EMPTY(&helpwin.page.head))
857 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
859 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
863 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
869 TAILQ_FOREACH(k, &keymap->m, keymaps) {
870 strlcpy(p, prfx, sizeof(p));
872 strlcat(p, " ", sizeof(p));
874 strlcat(p, "M-", sizeof(p));
875 if ((kn = unkbd(k->key)) != NULL)
876 strlcat(p, kn, sizeof(p));
878 strlcat(p, keyname(k->key), sizeof(p));
881 rec_compute_help(&k->map, p, sizeof(p));
883 emit_help_item(p, k->fn);
892 empty_vlist(&helpwin);
893 empty_linelist(&helpwin);
894 rec_compute_help(current_map, p, sizeof(p));
895 wrap_page(&helpwin, help_cols);
899 vmessage(const char *fmt, va_list ap)
901 if (evtimer_pending(&clechoev, NULL))
902 evtimer_del(&clechoev);
904 free(ministate.curmesg);
905 ministate.curmesg = NULL;
908 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
909 evtimer_add(&clechoev, &clechoev_timer);
911 /* TODO: what to do if the allocation fails here? */
912 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
913 ministate.curmesg = NULL;
927 message(const char *fmt, ...)
937 start_loading_anim(struct tab *tab)
939 if (tab->loading_anim)
941 tab->loading_anim = 1;
942 evtimer_set(&tab->loadingev, update_loading_anim, tab);
943 evtimer_add(&tab->loadingev, &loadingev_timer);
947 update_loading_anim(int fd, short ev, void *d)
951 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
953 if (tab->flags & TAB_CURRENT) {
954 redraw_modeline(tab);
961 evtimer_add(&tab->loadingev, &loadingev_timer);
965 stop_loading_anim(struct tab *tab)
967 if (!tab->loading_anim)
969 evtimer_del(&tab->loadingev);
970 tab->loading_anim = 0;
971 tab->loading_anim_step = 0;
973 if (!(tab->flags & TAB_CURRENT))
976 redraw_modeline(tab);
985 load_url_in_tab(struct tab *tab, const char *url)
987 message("Loading %s...", url);
988 start_loading_anim(tab);
991 tab->buffer.curs_x = 0;
992 tab->buffer.curs_y = 0;
997 switch_to_tab(struct tab *tab)
1001 TAILQ_FOREACH(t, &tabshead, tabs) {
1002 t->flags &= ~TAB_CURRENT;
1005 tab->flags |= TAB_CURRENT;
1006 tab->flags &= ~TAB_URGENT;
1012 return tab_counter++;
1016 new_tab(const char *url)
1020 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1026 TAILQ_INIT(&tab->hist.head);
1028 TAILQ_INIT(&tab->buffer.head);
1030 tab->id = tab_new_id();
1033 if (TAILQ_EMPTY(&tabshead))
1034 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1036 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1038 load_url_in_tab(tab, url);
1045 setlocale(LC_ALL, "");
1047 TAILQ_INIT(&eecmd_history.head);
1048 TAILQ_INIT(&ir_history.head);
1049 TAILQ_INIT(&lu_history.head);
1051 ministate.line.type = LINE_TEXT;
1052 ministate.vline.parent = &ministate.line;
1053 ministate.buffer.current_line = &ministate.vline;
1055 /* initialize help window */
1056 TAILQ_INIT(&helpwin.head);
1058 base_map = &global_map;
1059 current_map = &global_map;
1063 if (enable_colors) {
1066 use_default_colors();
1071 config_apply_style();
1076 intrflush(stdscr, FALSE);
1078 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1080 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1082 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1084 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1086 if ((help = newwin(1, 1, 1, 0)) == NULL)
1089 body_lines = LINES-3;
1092 wbkgd(body, body_face.body);
1093 wbkgd(echoarea, minibuffer_face.background);
1098 scrollok(body, FALSE);
1100 /* non-blocking input */
1103 mvwprintw(body, 0, 0, "");
1105 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1106 event_add(&stdioev, NULL);
1108 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1109 signal_add(&winchev, NULL);
1115 ui_on_tab_loaded(struct tab *tab)
1117 stop_loading_anim(tab);
1118 message("Loaded %s", tab->hist_cur->h);
1129 ui_on_tab_refresh(struct tab *tab)
1131 wrap_page(&tab->buffer, body_cols);
1132 if (tab->flags & TAB_CURRENT)
1135 tab->flags |= TAB_URGENT;
1145 ui_toggle_side_window(void)
1147 side_window = !side_window;
1152 * ugly hack, but otherwise the window doesn't get updated
1153 * until I call handle_resize a second time (i.e. C-l). I
1154 * will be happy to know why something like this is needed.
1156 handle_resize_nodelay(0, 0, NULL);
1157 handle_resize_nodelay(0, 0, NULL);
1161 ui_schedule_redraw(void)
1163 handle_resize_nodelay(0, 0, NULL);
1167 ui_require_input(struct tab *tab, int hide)
1169 /* TODO: hard-switching to another tab is ugly */
1172 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1174 strlcpy(ministate.prompt, "Input required: ",
1175 sizeof(ministate.prompt));
1180 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1183 yornp(prompt, fn, data);
1184 redraw_tab(current_tab());
1188 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1191 completing_read(prompt, fn, data);
1192 redraw_tab(current_tab());