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_minibuffer(void);
66 static void do_redraw_echoarea(void);
67 static void do_redraw_minibuffer(void);
68 static void redraw_tab(struct tab*);
69 static void emit_help_item(char*, void*);
70 static void rec_compute_help(struct kmap*, char*, size_t);
71 static void recompute_help(void);
72 static void update_loading_anim(int, short, void*);
73 static void stop_loading_anim(struct tab*);
77 struct thiskey thiskey;
79 static struct event resizeev;
80 static struct timeval resize_timer = { 0, 250000 };
82 static WINDOW *tabline, *body, *modeline, *echoarea;
84 int body_lines, body_cols;
87 static struct buffer helpwin;
88 static int help_lines, help_cols;
90 static int side_window;
92 static struct event clechoev;
93 static struct timeval clechoev_timer = { 5, 0 };
94 static struct timeval loadingev_timer = { 0, 250000 };
96 static uint32_t tab_counter;
98 static char keybuf[64];
100 struct kmap global_map,
108 update_x_offset(void)
110 if (olivetti_mode && fill_column < body_cols)
111 x_offset = (body_cols - fill_column)/2;
117 save_excursion(struct excursion *place, struct buffer *buffer)
119 place->curs_x = buffer->curs_x;
120 place->curs_y = buffer->curs_y;
121 place->line_off = buffer->line_off;
122 place->current_line = buffer->current_line;
123 place->cpoff = buffer->cpoff;
127 restore_excursion(struct excursion *place, struct buffer *buffer)
129 buffer->curs_x = place->curs_x;
130 buffer->curs_y = place->curs_y;
131 buffer->line_off = place->line_off;
132 buffer->current_line = place->current_line;
133 buffer->cpoff = place->cpoff;
137 restore_curs_x(struct buffer *buffer)
142 vl = buffer->current_line;
143 if (vl == NULL || vl->line == NULL)
144 buffer->curs_x = buffer->cpoff = 0;
146 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
148 buffer->curs_x += x_offset;
151 prfx = line_prefixes[vl->parent->type].prfx1;
152 buffer->curs_x += utf8_swidth(prfx);
157 global_key_unbound(void)
159 message("%s is undefined", keybuf);
162 static struct vline *
163 nth_line(struct buffer *buffer, size_t n)
169 TAILQ_FOREACH(vl, &buffer->head, vlines) {
184 TAILQ_FOREACH(t, &tabshead, tabs) {
185 if (t->flags & TAB_CURRENT)
197 return &ministate.buffer;
198 return ¤t_tab()->buffer;
206 if ((thiskey.key = wgetch(body)) == ERR)
209 thiskey.meta = thiskey.key == 27;
211 thiskey.key = wgetch(body);
212 if (thiskey.key == ERR || thiskey.key == 27) {
219 if ((unsigned int)thiskey.key < UINT8_MAX) {
221 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
223 if ((thiskey.key = wgetch(body)) == ERR) {
224 message("Error decoding user input");
234 dispatch_stdio(int fd, short ev, void *d)
243 if (keybuf[0] != '\0')
244 strlcat(keybuf, " ", sizeof(keybuf));
246 strlcat(keybuf, "M-", sizeof(keybuf));
247 if (thiskey.cp != 0) {
248 utf8_encode(thiskey.cp, tmp);
249 strlcat(keybuf, tmp, sizeof(keybuf));
251 if ((keyname = unkbd(thiskey.key)) != NULL)
252 strlcat(keybuf, keyname, sizeof(keybuf));
254 tmp[0] = thiskey.key;
255 strlcat(keybuf, tmp, sizeof(keybuf));
259 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
260 if (k->meta == thiskey.meta &&
261 k->key == thiskey.key) {
263 current_map = &k->map;
265 current_map = base_map;
266 strlcpy(keybuf, "", sizeof(keybuf));
267 k->fn(current_buffer());
273 if (current_map->unhandled_input != NULL)
274 current_map->unhandled_input();
276 global_key_unbound();
278 strlcpy(keybuf, "", sizeof(keybuf));
279 current_map = base_map;
285 redraw_tab(current_tab());
289 handle_clear_echoarea(int fd, short ev, void *d)
291 free(ministate.curmesg);
292 ministate.curmesg = NULL;
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);
315 handle_resize_nodelay(int s, short ev, void *d)
323 /* move and resize the windows, in reverse order! */
325 mvwin(echoarea, LINES-1, 0);
326 wresize(echoarea, 1, COLS);
328 mvwin(modeline, LINES-2, 0);
329 wresize(modeline, 1, COLS);
331 body_lines = LINES-3;
335 help_cols = 0.3 * COLS;
336 help_lines = LINES-3;
338 wresize(help, help_lines, help_cols);
340 wrap_page(&helpwin, help_cols);
342 body_cols = COLS - help_cols - 1;
343 mvwin(body, 1, help_cols);
348 wresize(body, body_lines, body_cols);
350 wresize(tabline, 1, COLS);
354 wrap_page(&tab->buffer, body_cols);
359 wrap_page(struct buffer *buffer, int width)
362 const struct line *top_orig, *orig;
367 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
368 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
370 buffer->top_line = NULL;
371 buffer->current_line = NULL;
373 buffer->force_redraw = 1;
375 buffer->line_off = 0;
379 TAILQ_FOREACH(l, &buffer->page.head, lines) {
380 prfx = line_prefixes[l->type].prfx1;
391 wrap_text(buffer, prfx, l, MIN(fill_column, width));
393 case LINE_PRE_CONTENT:
395 pre_width = MIN(fill_column, width);
398 hardwrap_text(buffer, l, pre_width);
402 if (top_orig == l && buffer->top_line == NULL) {
403 buffer->line_off = buffer->line_max-1;
404 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
407 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
408 if (vl == NULL || vl->parent != orig)
410 buffer->top_line = vl;
415 if (orig == l && buffer->current_line == NULL) {
416 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
419 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
420 if (vl == NULL || vl->parent != orig)
422 buffer->current_line = vl;
427 if (buffer->current_line == NULL)
428 buffer->current_line = TAILQ_FIRST(&buffer->head);
430 if (buffer->top_line == NULL)
431 buffer->top_line = buffer->current_line;
437 print_vline(int off, int width, WINDOW *window, struct vline *vl)
444 f = &line_faces[vl->parent->type];
446 /* unused, set by getyx */
450 prfx = line_prefixes[vl->parent->type].prfx1;
452 prfx = line_prefixes[vl->parent->type].prfx2;
458 wattr_on(window, body_face.left, NULL);
459 for (i = 0; i < off; i++)
461 wattr_off(window, body_face.left, NULL);
463 wattr_on(window, f->prefix, NULL);
464 wprintw(window, "%s", prfx);
465 wattr_off(window, f->prefix, NULL);
467 wattr_on(window, f->text, NULL);
468 wprintw(window, "%s", text);
469 wattr_off(window, f->text, NULL);
475 wattr_on(window, f->trail, NULL);
476 for (i = 0; i < left - off; ++i)
478 wattr_off(window, f->trail, NULL);
480 wattr_on(window, body_face.right, NULL);
481 for (i = 0; i < off; i++)
483 wattr_off(window, body_face.right, NULL);
491 size_t toskip, ots, tabwidth, space, x;
492 int current, y, truncated;
498 /* unused, but setted by a getyx */
501 tabwidth = sizeof(buf)+1;
505 TAILQ_FOREACH(tab, &tabshead, tabs) {
507 if (tab->flags & TAB_CURRENT)
511 if (toskip * tabwidth < space)
516 while (toskip != 0 &&
517 (ots - toskip+1) * tabwidth < space)
522 wattr_on(tabline, tab_face.background, NULL);
523 wprintw(tabline, toskip == 0 ? " " : "<");
524 wattr_off(tabline, tab_face.background, NULL);
527 TAILQ_FOREACH(tab, &tabshead, tabs) {
535 getyx(tabline, y, x);
536 if (x + sizeof(buf)+2 >= (size_t)COLS)
539 current = tab->flags & TAB_CURRENT;
541 if (*(title = tab->buffer.page.title) == '\0')
542 title = tab->hist_cur->h;
544 if (tab->flags & TAB_URGENT)
545 strlcpy(buf, "!", sizeof(buf));
547 strlcpy(buf, " ", sizeof(buf));
549 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
550 /* truncation happens */
551 strlcpy(&buf[sizeof(buf)-4], "...", 4);
553 /* pad with spaces */
554 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
559 wattr_on(tabline, tab_face.current, NULL);
561 wattr_on(tabline, tab_face.tab, NULL);
563 wprintw(tabline, "%s", buf);
564 if (TAILQ_NEXT(tab, tabs) != NULL)
565 wprintw(tabline, " ");
568 wattr_off(tabline, tab_face.current, NULL);
570 wattr_off(tabline, tab_face.tab, NULL);
573 wattr_on(tabline, tab_face.background, NULL);
574 for (; x < (size_t)COLS; ++x)
575 waddch(tabline, ' ');
577 mvwprintw(tabline, 0, COLS-1, ">");
578 wattr_off(tabline, tab_face.background, NULL);
582 * Compute the first visible line around vl. Try to search forward
583 * until the end of the buffer; if a visible line is not found, search
584 * backward. Return NULL if no viable line was found.
586 static inline struct vline *
587 adjust_line(struct vline *vl, struct buffer *buffer)
591 if (!(vl->parent->flags & L_HIDDEN))
596 t != NULL && t->parent->flags & L_HIDDEN;
597 t = TAILQ_NEXT(t, vlines))
603 /* search backward */
605 t != NULL && t->parent->flags & L_HIDDEN;
606 t = TAILQ_PREV(t, vhead, vlines))
613 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
618 restore_curs_x(buffer);
621 * TODO: ignoring buffer->force_update and always
622 * re-rendering. In theory we can recompute the y position
623 * without a re-render, and optimize here. It's not the only
624 * optimisation possible here, wscrl wolud also be an
632 if (TAILQ_EMPTY(&buffer->head))
635 buffer->top_line = adjust_line(buffer->top_line, buffer);
636 if (buffer->top_line == NULL)
639 buffer->current_line = adjust_line(buffer->current_line, buffer);
643 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
644 if (vl->parent->flags & L_HIDDEN)
648 print_vline(x_offset, width, win, vl);
650 if (vl == buffer->current_line)
662 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
663 if (vl == buffer->current_line)
665 if (vl->parent->flags & L_HIDDEN)
668 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
674 buffer->last_line_off = buffer->line_off;
675 buffer->force_redraw = 0;
677 wmove(win, buffer->curs_y, buffer->curs_x);
683 redraw_window(help, help_lines, help_cols, &helpwin);
687 redraw_body(struct tab *tab)
689 static struct tab *last_tab;
692 tab->buffer.force_redraw =1;
695 redraw_window(body, body_lines, body_cols, &tab->buffer);
699 trust_status_char(enum trust_state ts)
702 case TS_UNKNOWN: return 'u';
703 case TS_UNTRUSTED: return '!';
704 case TS_TEMP_TRUSTED: return '!';
705 case TS_TRUSTED: return 'v';
706 case TS_VERIFIED: return 'V';
712 redraw_modeline(struct tab *tab)
715 int x, y, max_x, max_y;
716 const char *mode = tab->buffer.page.name;
717 const char *spin = "-\\|/";
720 wattr_on(modeline, modeline_face.background, NULL);
721 wmove(modeline, 0, 0);
723 wprintw(modeline, "-%c%c %s ",
724 spin[tab->loading_anim_step],
725 trust_status_char(tab->trust),
726 mode == NULL ? "(none)" : mode);
728 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
730 if (tab->buffer.line_max <= (size_t)body_lines)
731 wprintw(modeline, "All ");
732 else if (tab->buffer.line_off == 0)
733 wprintw(modeline, "Top ");
734 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
735 wprintw(modeline, "Bottom ");
737 wprintw(modeline, "%.0f%% ", pct);
739 wprintw(modeline, "%d/%d %s ",
740 tab->buffer.line_off + tab->buffer.curs_y,
741 tab->buffer.line_max,
744 getyx(modeline, y, x);
745 getmaxyx(modeline, max_y, max_x);
750 for (; x < max_x; ++x)
751 waddstr(modeline, "-");
753 wattr_off(modeline, modeline_face.background, NULL);
757 redraw_minibuffer(void)
759 wattr_on(echoarea, minibuffer_face.background, NULL);
763 do_redraw_minibuffer();
765 do_redraw_echoarea();
767 wattr_off(echoarea, minibuffer_face.background, NULL);
771 do_redraw_echoarea(void)
775 if (ministate.curmesg != NULL)
776 wprintw(echoarea, "%s", ministate.curmesg);
777 else if (*keybuf != '\0')
778 waddstr(echoarea, keybuf);
780 /* If nothing else, show the URL at point */
782 if (tab->buffer.current_line != NULL &&
783 tab->buffer.current_line->parent->type == LINE_LINK)
784 waddstr(echoarea, tab->buffer.current_line->parent->alt);
789 do_redraw_minibuffer(void)
791 size_t off_y, off_x = 0;
792 const char *start, *c;
794 /* unused, set by getyx */
797 mvwprintw(echoarea, 0, 0, "%s", ministate.prompt);
798 if (ministate.hist_cur != NULL)
799 wprintw(echoarea, "(%zu/%zu) ",
800 ministate.hist_off + 1,
801 ministate.history->len);
803 getyx(echoarea, off_y, off_x);
805 start = ministate.hist_cur != NULL
806 ? ministate.hist_cur->h
808 c = utf8_nth(ministate.buffer.current_line->line,
809 ministate.buffer.cpoff);
810 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
811 start = utf8_next_cp(start);
814 waddstr(echoarea, start);
816 if (ministate.curmesg != NULL)
817 wprintw(echoarea, " [%s]", ministate.curmesg);
819 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
823 redraw_tab(struct tab *tab)
832 redraw_modeline(tab);
835 wnoutrefresh(tabline);
836 wnoutrefresh(modeline);
840 wnoutrefresh(echoarea);
842 wnoutrefresh(echoarea);
850 emit_help_item(char *prfx, void *fn)
855 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
861 if ((l = calloc(1, sizeof(*l))) == NULL)
867 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
869 if (TAILQ_EMPTY(&helpwin.page.head))
870 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
872 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
876 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
882 TAILQ_FOREACH(k, &keymap->m, keymaps) {
883 strlcpy(p, prfx, sizeof(p));
885 strlcat(p, " ", sizeof(p));
887 strlcat(p, "M-", sizeof(p));
888 if ((kn = unkbd(k->key)) != NULL)
889 strlcat(p, kn, sizeof(p));
891 strlcat(p, keyname(k->key), sizeof(p));
894 rec_compute_help(&k->map, p, sizeof(p));
896 emit_help_item(p, k->fn);
905 empty_vlist(&helpwin);
906 empty_linelist(&helpwin);
907 rec_compute_help(current_map, p, sizeof(p));
908 wrap_page(&helpwin, help_cols);
912 vmessage(const char *fmt, va_list ap)
914 if (evtimer_pending(&clechoev, NULL))
915 evtimer_del(&clechoev);
917 free(ministate.curmesg);
918 ministate.curmesg = NULL;
921 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
922 evtimer_add(&clechoev, &clechoev_timer);
924 /* TODO: what to do if the allocation fails here? */
925 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
926 ministate.curmesg = NULL;
940 message(const char *fmt, ...)
950 start_loading_anim(struct tab *tab)
952 if (tab->loading_anim)
954 tab->loading_anim = 1;
955 evtimer_set(&tab->loadingev, update_loading_anim, tab);
956 evtimer_add(&tab->loadingev, &loadingev_timer);
960 update_loading_anim(int fd, short ev, void *d)
964 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
966 if (tab->flags & TAB_CURRENT) {
967 redraw_modeline(tab);
974 evtimer_add(&tab->loadingev, &loadingev_timer);
978 stop_loading_anim(struct tab *tab)
980 if (!tab->loading_anim)
982 evtimer_del(&tab->loadingev);
983 tab->loading_anim = 0;
984 tab->loading_anim_step = 0;
986 if (!(tab->flags & TAB_CURRENT))
989 redraw_modeline(tab);
998 load_url_in_tab(struct tab *tab, const char *url)
1000 message("Loading %s...", url);
1001 start_loading_anim(tab);
1004 tab->buffer.curs_x = 0;
1005 tab->buffer.curs_y = 0;
1010 switch_to_tab(struct tab *tab)
1014 TAILQ_FOREACH(t, &tabshead, tabs) {
1015 t->flags &= ~TAB_CURRENT;
1018 tab->flags |= TAB_CURRENT;
1019 tab->flags &= ~TAB_URGENT;
1025 return tab_counter++;
1029 new_tab(const char *url)
1033 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1039 TAILQ_INIT(&tab->hist.head);
1041 TAILQ_INIT(&tab->buffer.head);
1043 tab->id = tab_new_id();
1046 if (TAILQ_EMPTY(&tabshead))
1047 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1049 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1051 load_url_in_tab(tab, url);
1058 setlocale(LC_ALL, "");
1060 TAILQ_INIT(&eecmd_history.head);
1061 TAILQ_INIT(&ir_history.head);
1062 TAILQ_INIT(&lu_history.head);
1064 ministate.line.type = LINE_TEXT;
1065 ministate.vline.parent = &ministate.line;
1066 ministate.buffer.current_line = &ministate.vline;
1068 /* initialize help window */
1069 TAILQ_INIT(&helpwin.head);
1071 base_map = &global_map;
1072 current_map = &global_map;
1076 if (enable_colors) {
1079 use_default_colors();
1084 config_apply_style();
1089 intrflush(stdscr, FALSE);
1091 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1093 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1095 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1097 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1099 if ((help = newwin(1, 1, 1, 0)) == NULL)
1102 body_lines = LINES-3;
1105 wbkgd(body, body_face.body);
1106 wbkgd(echoarea, minibuffer_face.background);
1111 scrollok(body, FALSE);
1113 /* non-blocking input */
1116 mvwprintw(body, 0, 0, "");
1118 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1119 event_add(&stdioev, NULL);
1121 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1122 signal_add(&winchev, NULL);
1128 ui_on_tab_loaded(struct tab *tab)
1130 stop_loading_anim(tab);
1131 message("Loaded %s", tab->hist_cur->h);
1142 ui_on_tab_refresh(struct tab *tab)
1144 wrap_page(&tab->buffer, body_cols);
1145 if (tab->flags & TAB_CURRENT)
1148 tab->flags |= TAB_URGENT;
1158 ui_toggle_side_window(void)
1160 side_window = !side_window;
1165 * ugly hack, but otherwise the window doesn't get updated
1166 * until I call handle_resize a second time (i.e. C-l). I
1167 * will be happy to know why something like this is needed.
1169 handle_resize_nodelay(0, 0, NULL);
1170 handle_resize_nodelay(0, 0, NULL);
1174 ui_schedule_redraw(void)
1176 handle_resize_nodelay(0, 0, NULL);
1180 ui_require_input(struct tab *tab, int hide)
1182 /* TODO: hard-switching to another tab is ugly */
1185 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1187 strlcpy(ministate.prompt, "Input required: ",
1188 sizeof(ministate.prompt));
1193 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1196 yornp(prompt, fn, data);
1197 redraw_tab(current_tab());
1201 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1204 completing_read(prompt, fn, data);
1205 redraw_tab(current_tab());