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.
44 #include "minibuffer.h"
45 #include "telescope.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 void rearrange_windows(void);
60 static int wrap_page(struct buffer*, int);
61 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
62 static void print_vline(int, int, WINDOW*, struct vline*);
63 static void redraw_tabline(void);
64 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
65 static void redraw_help(void);
66 static void redraw_body(struct tab*);
67 static void redraw_modeline(struct tab*);
68 static void redraw_minibuffer(void);
69 static void do_redraw_echoarea(void);
70 static void do_redraw_minibuffer(void);
71 static void do_redraw_minibuffer_compl(void);
72 static void place_cursor(int);
73 static void redraw_tab(struct tab*);
74 static void emit_help_item(char*, void*);
75 static void rec_compute_help(struct kmap*, char*, size_t);
76 static void recompute_help(void);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
80 static int should_rearrange_windows;
84 struct thiskey thiskey;
86 static struct event resizeev;
87 static struct timeval resize_timer = { 0, 250000 };
89 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
91 int body_lines, body_cols;
94 static struct buffer helpwin;
95 static int help_lines, help_cols;
97 static int side_window;
99 static struct event clechoev;
100 static struct timeval clechoev_timer = { 5, 0 };
101 static struct timeval loadingev_timer = { 0, 250000 };
103 static uint32_t tab_counter;
105 static char keybuf[64];
107 struct kmap global_map,
115 update_x_offset(void)
117 if (olivetti_mode && fill_column < body_cols)
118 x_offset = (body_cols - fill_column)/2;
124 save_excursion(struct excursion *place, struct buffer *buffer)
126 place->curs_x = buffer->curs_x;
127 place->curs_y = buffer->curs_y;
128 place->line_off = buffer->line_off;
129 place->current_line = buffer->current_line;
130 place->cpoff = buffer->cpoff;
134 restore_excursion(struct excursion *place, struct buffer *buffer)
136 buffer->curs_x = place->curs_x;
137 buffer->curs_y = place->curs_y;
138 buffer->line_off = place->line_off;
139 buffer->current_line = place->current_line;
140 buffer->cpoff = place->cpoff;
144 restore_curs_x(struct buffer *buffer)
149 vl = buffer->current_line;
150 if (vl == NULL || vl->line == NULL)
151 buffer->curs_x = buffer->cpoff = 0;
152 else if (vl->parent->data != NULL)
153 buffer->curs_x = utf8_snwidth(vl->parent->data+1, buffer->cpoff) + 1;
155 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
157 buffer->curs_x += x_offset;
160 prfx = line_prefixes[vl->parent->type].prfx1;
161 buffer->curs_x += utf8_swidth(prfx);
166 global_key_unbound(void)
168 message("%s is undefined", keybuf);
171 static struct vline *
172 nth_line(struct buffer *buffer, size_t n)
178 TAILQ_FOREACH(vl, &buffer->head, vlines) {
193 TAILQ_FOREACH(t, &tabshead, tabs) {
194 if (t->flags & TAB_CURRENT)
206 return &ministate.buffer;
207 return ¤t_tab()->buffer;
215 if ((thiskey.key = wgetch(body)) == ERR)
218 thiskey.meta = thiskey.key == 27;
220 thiskey.key = wgetch(body);
221 if (thiskey.key == ERR || thiskey.key == 27) {
228 if ((unsigned int)thiskey.key < UINT8_MAX) {
230 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
232 if ((thiskey.key = wgetch(body)) == ERR) {
233 message("Error decoding user input");
243 dispatch_stdio(int fd, short ev, void *d)
249 /* TODO: schedule a redraw? */
256 if (keybuf[0] != '\0')
257 strlcat(keybuf, " ", sizeof(keybuf));
259 strlcat(keybuf, "M-", sizeof(keybuf));
260 if (thiskey.cp != 0) {
261 utf8_encode(thiskey.cp, tmp);
262 strlcat(keybuf, tmp, sizeof(keybuf));
263 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
264 strlcat(keybuf, keyname, sizeof(keybuf));
266 tmp[0] = thiskey.key;
267 strlcat(keybuf, tmp, sizeof(keybuf));
270 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
271 if (k->meta == thiskey.meta &&
272 k->key == thiskey.key) {
274 current_map = &k->map;
276 current_map = base_map;
277 strlcpy(keybuf, "", sizeof(keybuf));
278 k->fn(current_buffer());
284 if (current_map->unhandled_input != NULL)
285 current_map->unhandled_input();
287 global_key_unbound();
289 strlcpy(keybuf, "", sizeof(keybuf));
290 current_map = base_map;
296 if (should_rearrange_windows)
298 redraw_tab(current_tab());
302 handle_clear_echoarea(int fd, short ev, void *d)
304 free(ministate.curmesg);
305 ministate.curmesg = NULL;
312 handle_resize(int sig, short ev, void *d)
314 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
315 event_del(&resizeev);
317 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
318 evtimer_add(&resizeev, &resize_timer);
322 handle_resize_nodelay(int s, short ev, void *d)
332 rearrange_windows(void)
337 should_rearrange_windows = 0;
341 if ((too_small = lines < 15)) {
343 printw("Window too small.");
348 /* move and resize the windows, in reverse order! */
350 if (in_minibuffer == MB_COMPREAD) {
351 mvwin(minibuffer, lines-10, 0);
352 wresize(minibuffer, 10, COLS);
355 wrap_page(&ministate.compl.buffer, COLS);
358 mvwin(echoarea, --lines, 0);
359 wresize(echoarea, 1, COLS);
361 mvwin(modeline, --lines, 0);
362 wresize(modeline, 1, COLS);
364 body_lines = --lines;
368 help_cols = 0.3 * COLS;
371 wresize(help, help_lines, help_cols);
373 wrap_page(&helpwin, help_cols);
375 body_cols = COLS - help_cols - 1;
376 mvwin(body, 1, help_cols);
381 wresize(body, body_lines, body_cols);
383 wresize(tabline, 1, COLS);
387 wrap_page(&tab->buffer, body_cols);
392 wrap_page(struct buffer *buffer, int width)
395 const struct line *top_orig, *orig;
400 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
401 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
403 buffer->top_line = NULL;
404 buffer->current_line = NULL;
406 buffer->force_redraw = 1;
408 buffer->line_off = 0;
412 TAILQ_FOREACH(l, &buffer->page.head, lines) {
413 prfx = line_prefixes[l->type].prfx1;
424 wrap_text(buffer, prfx, l, MIN(fill_column, width));
426 case LINE_PRE_CONTENT:
428 pre_width = MIN(fill_column, width);
431 hardwrap_text(buffer, l, pre_width);
434 case LINE_COMPL_CURRENT:
435 wrap_one(buffer, prfx, l, width);
439 if (top_orig == l && buffer->top_line == NULL) {
440 buffer->line_off = buffer->line_max-1;
441 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
444 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
445 if (vl == NULL || vl->parent != orig)
447 buffer->top_line = vl;
452 if (orig == l && buffer->current_line == NULL) {
453 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
456 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
457 if (vl == NULL || vl->parent != orig)
459 buffer->current_line = vl;
464 if (buffer->current_line == NULL)
465 buffer->current_line = TAILQ_FIRST(&buffer->head);
467 if (buffer->top_line == NULL)
468 buffer->top_line = buffer->current_line;
474 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
475 const char **prfx_ret, const char **text_ret)
477 int type, i, cont, width;
480 if ((*text_ret = vl->line) == NULL)
483 cont = vl->flags & L_CONTINUATION;
484 type = vl->parent->type;
486 *prfx_ret = line_prefixes[type].prfx1;
488 *prfx_ret = line_prefixes[type].prfx2;
490 space = vl->parent->data;
491 if (!emojify_link || type != LINE_LINK || space == NULL)
496 width = utf8_swidth_between(vl->parent->line, space);
497 for (i = 0; i < width + 1; ++i)
498 strlcat(buf, " ", len);
500 strlcpy(buf, *text_ret, len);
501 if ((t = strchr(buf, ' ')) != NULL)
503 strlcat(buf, " ", len);
506 *text_ret += (space - vl->parent->line) + 1;
513 * Core part of the rendering. It prints a vline starting from the
514 * current cursor position. Printing a vline consists of skipping
515 * `off' columns (for olivetti-mode), print the correct prefix (which
516 * may be the emoji in case of emojified links-lines), printing the
517 * text itself, filling until width - off and filling off columns
521 print_vline(int off, int width, WINDOW *window, struct vline *vl)
524 * Believe me or not, I've seen emoji ten code points long!
525 * That means, to stay large, 4*10 bytes + NUL.
527 char emojibuf[41] = {0};
528 const char *text, *prfx;
532 f = &line_faces[vl->parent->type];
534 /* unused, set by getyx */
537 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
539 wattr_on(window, body_face.left, NULL);
540 for (i = 0; i < off; i++)
542 wattr_off(window, body_face.left, NULL);
544 wattr_on(window, f->prefix, NULL);
545 wprintw(window, "%s", prfx);
546 wattr_off(window, f->prefix, NULL);
548 wattr_on(window, f->text, NULL);
549 wprintw(window, "%s", text);
550 wattr_off(window, f->text, NULL);
556 wattr_on(window, f->trail, NULL);
557 for (i = 0; i < left - off; ++i)
559 wattr_off(window, f->trail, NULL);
561 wattr_on(window, body_face.right, NULL);
562 for (i = 0; i < off; i++)
564 wattr_off(window, body_face.right, NULL);
572 size_t toskip, ots, tabwidth, space, x;
573 int current, y, truncated, pair;
579 /* unused, but setted by a getyx */
582 tabwidth = sizeof(buf)+1;
586 TAILQ_FOREACH(tab, &tabshead, tabs) {
588 if (tab->flags & TAB_CURRENT)
592 if (toskip * tabwidth < space)
597 while (toskip != 0 &&
598 (ots - toskip+1) * tabwidth < space)
603 wattr_on(tabline, tab_face.background, NULL);
604 wprintw(tabline, toskip == 0 ? " " : "<");
605 wattr_off(tabline, tab_face.background, NULL);
608 TAILQ_FOREACH(tab, &tabshead, tabs) {
616 getyx(tabline, y, x);
617 if (x + sizeof(buf)+2 >= (size_t)COLS)
620 current = tab->flags & TAB_CURRENT;
622 if (*(title = tab->buffer.page.title) == '\0')
623 title = tab->hist_cur->h;
625 if (tab->flags & TAB_URGENT)
626 strlcpy(buf, "!", sizeof(buf));
628 strlcpy(buf, " ", sizeof(buf));
630 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
631 /* truncation happens */
632 strlcpy(&buf[sizeof(buf)-4], "...", 4);
634 /* pad with spaces */
635 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
639 pair = current ? tab_face.current : tab_face.tab;
640 wattr_on(tabline, pair, NULL);
641 wprintw(tabline, "%s", buf);
642 wattr_off(tabline, pair, NULL);
644 wattr_on(tabline, tab_face.background, NULL);
645 if (TAILQ_NEXT(tab, tabs) != NULL)
646 wprintw(tabline, "│");
647 wattr_off(tabline, tab_face.background, NULL);
650 wattr_on(tabline, tab_face.background, NULL);
651 for (; x < (size_t)COLS; ++x)
652 waddch(tabline, ' ');
654 mvwprintw(tabline, 0, COLS-1, ">");
655 wattr_off(tabline, tab_face.background, NULL);
659 * Compute the first visible line around vl. Try to search forward
660 * until the end of the buffer; if a visible line is not found, search
661 * backward. Return NULL if no viable line was found.
664 adjust_line(struct vline *vl, struct buffer *buffer)
671 if (!(vl->parent->flags & L_HIDDEN))
676 t != NULL && t->parent->flags & L_HIDDEN;
677 t = TAILQ_NEXT(t, vlines))
683 /* search backward */
685 t != NULL && t->parent->flags & L_HIDDEN;
686 t = TAILQ_PREV(t, vhead, vlines))
693 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
698 restore_curs_x(buffer);
701 * TODO: ignoring buffer->force_update and always
702 * re-rendering. In theory we can recompute the y position
703 * without a re-render, and optimize here. It's not the only
704 * optimisation possible here, wscrl wolud also be an
712 if (TAILQ_EMPTY(&buffer->head))
715 if (buffer->top_line == NULL)
716 buffer->top_line = TAILQ_FIRST(&buffer->head);
718 buffer->top_line = adjust_line(buffer->top_line, buffer);
719 if (buffer->top_line == NULL)
722 buffer->current_line = adjust_line(buffer->current_line, buffer);
726 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
727 if (vl->parent->flags & L_HIDDEN)
731 print_vline(off, width, win, vl);
733 if (vl == buffer->current_line)
745 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
746 if (vl == buffer->current_line)
748 if (vl->parent->flags & L_HIDDEN)
751 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
758 buffer->last_line_off = buffer->line_off;
759 buffer->force_redraw = 0;
761 wmove(win, buffer->curs_y, buffer->curs_x);
767 redraw_window(help, 0, help_lines, help_cols, &helpwin);
771 redraw_body(struct tab *tab)
773 static struct tab *last_tab;
776 tab->buffer.force_redraw =1;
779 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
783 trust_status_char(enum trust_state ts)
786 case TS_UNKNOWN: return 'u';
787 case TS_UNTRUSTED: return '!';
788 case TS_TEMP_TRUSTED: return '!';
789 case TS_TRUSTED: return 'v';
790 case TS_VERIFIED: return 'V';
796 redraw_modeline(struct tab *tab)
799 int x, y, max_x, max_y;
800 const char *mode = tab->buffer.page.name;
801 const char *spin = "-\\|/";
804 wattr_on(modeline, modeline_face.background, NULL);
805 wmove(modeline, 0, 0);
807 wprintw(modeline, "-%c%c %s ",
808 spin[tab->loading_anim_step],
809 trust_status_char(tab->trust),
810 mode == NULL ? "(none)" : mode);
812 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
813 / tab->buffer.line_max;
815 if (tab->buffer.line_max <= (size_t)body_lines)
816 wprintw(modeline, "All ");
817 else if (tab->buffer.line_off == 0)
818 wprintw(modeline, "Top ");
819 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
820 wprintw(modeline, "Bottom ");
822 wprintw(modeline, "%.0f%% ", pct);
824 wprintw(modeline, "%d/%d %s ",
825 tab->buffer.line_off + tab->buffer.curs_y,
826 tab->buffer.line_max,
829 getyx(modeline, y, x);
830 getmaxyx(modeline, max_y, max_x);
835 for (; x < max_x; ++x)
836 waddstr(modeline, "-");
838 wattr_off(modeline, modeline_face.background, NULL);
842 redraw_minibuffer(void)
844 wattr_on(echoarea, minibuffer_face.background, NULL);
848 do_redraw_minibuffer();
850 do_redraw_echoarea();
852 if (in_minibuffer == MB_COMPREAD)
853 do_redraw_minibuffer_compl();
855 wattr_off(echoarea, minibuffer_face.background, NULL);
859 do_redraw_echoarea(void)
863 if (ministate.curmesg != NULL)
864 wprintw(echoarea, "%s", ministate.curmesg);
865 else if (*keybuf != '\0')
866 waddstr(echoarea, keybuf);
868 /* If nothing else, show the URL at point */
870 if (tab->buffer.current_line != NULL &&
871 tab->buffer.current_line->parent->type == LINE_LINK)
873 tab->buffer.current_line->parent->alt);
878 do_redraw_minibuffer(void)
880 size_t off_y, off_x = 0;
881 const char *start, *c;
883 /* unused, set by getyx */
886 wmove(echoarea, 0, 0);
888 if (in_minibuffer == MB_COMPREAD)
889 wprintw(echoarea, "(%2d) ",
890 ministate.compl.buffer.line_max);
892 wprintw(echoarea, "%s", ministate.prompt);
893 if (ministate.hist_cur != NULL)
894 wprintw(echoarea, "(%zu/%zu) ",
895 ministate.hist_off + 1,
896 ministate.history->len);
898 getyx(echoarea, off_y, off_x);
900 start = ministate.hist_cur != NULL
901 ? ministate.hist_cur->h
903 c = utf8_nth(ministate.buffer.current_line->line,
904 ministate.buffer.cpoff);
905 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
906 start = utf8_next_cp(start);
909 waddstr(echoarea, start);
911 if (ministate.curmesg != NULL)
912 wprintw(echoarea, " [%s]", ministate.curmesg);
914 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
918 do_redraw_minibuffer_compl(void)
920 redraw_window(minibuffer, 0, 10, body_cols,
921 &ministate.compl.buffer);
925 * Place the cursor in the right ncurses window. If soft is 1, use
926 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
930 place_cursor(int soft)
932 int (*touch)(WINDOW *);
935 touch = wnoutrefresh;
949 redraw_tab(struct tab *tab)
961 redraw_modeline(tab);
964 wnoutrefresh(tabline);
965 wnoutrefresh(modeline);
967 if (in_minibuffer == MB_COMPREAD)
968 wnoutrefresh(minibuffer);
976 emit_help_item(char *prfx, void *fn)
981 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
987 if ((l = calloc(1, sizeof(*l))) == NULL)
993 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
995 if (TAILQ_EMPTY(&helpwin.page.head))
996 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
998 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1002 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1008 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1009 strlcpy(p, prfx, sizeof(p));
1011 strlcat(p, " ", sizeof(p));
1013 strlcat(p, "M-", sizeof(p));
1014 if ((kn = unkbd(k->key)) != NULL)
1015 strlcat(p, kn, sizeof(p));
1017 strlcat(p, keyname(k->key), sizeof(p));
1020 rec_compute_help(&k->map, p, sizeof(p));
1022 emit_help_item(p, k->fn);
1027 recompute_help(void)
1031 empty_vlist(&helpwin);
1032 empty_linelist(&helpwin);
1033 rec_compute_help(current_map, p, sizeof(p));
1034 wrap_page(&helpwin, help_cols);
1038 vmessage(const char *fmt, va_list ap)
1040 if (evtimer_pending(&clechoev, NULL))
1041 evtimer_del(&clechoev);
1043 free(ministate.curmesg);
1044 ministate.curmesg = NULL;
1047 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1048 evtimer_add(&clechoev, &clechoev_timer);
1050 /* TODO: what to do if the allocation fails here? */
1051 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1052 ministate.curmesg = NULL;
1055 redraw_minibuffer();
1060 message(const char *fmt, ...)
1070 start_loading_anim(struct tab *tab)
1072 if (tab->loading_anim)
1074 tab->loading_anim = 1;
1075 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1076 evtimer_add(&tab->loadingev, &loadingev_timer);
1080 update_loading_anim(int fd, short ev, void *d)
1082 struct tab *tab = d;
1084 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1086 if (tab->flags & TAB_CURRENT) {
1087 redraw_modeline(tab);
1094 evtimer_add(&tab->loadingev, &loadingev_timer);
1098 stop_loading_anim(struct tab *tab)
1100 if (!tab->loading_anim)
1102 evtimer_del(&tab->loadingev);
1103 tab->loading_anim = 0;
1104 tab->loading_anim_step = 0;
1106 if (!(tab->flags & TAB_CURRENT))
1109 redraw_modeline(tab);
1118 load_url_in_tab(struct tab *tab, const char *url)
1120 message("Loading %s...", url);
1121 start_loading_anim(tab);
1124 tab->buffer.curs_x = 0;
1125 tab->buffer.curs_y = 0;
1130 switch_to_tab(struct tab *tab)
1134 TAILQ_FOREACH(t, &tabshead, tabs) {
1135 t->flags &= ~TAB_CURRENT;
1138 tab->flags |= TAB_CURRENT;
1139 tab->flags &= ~TAB_URGENT;
1145 return tab_counter++;
1149 new_tab(const char *url)
1153 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1159 TAILQ_INIT(&tab->hist.head);
1161 TAILQ_INIT(&tab->buffer.head);
1163 tab->id = tab_new_id();
1166 if (TAILQ_EMPTY(&tabshead))
1167 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1169 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1171 load_url_in_tab(tab, url);
1176 ui_print_colors(void)
1178 int colors = 0, pairs = 0, can_change = 0;
1179 int columns = 16, lines, color, i, j;
1184 use_default_colors();
1187 pairs = COLOR_PAIRS;
1188 can_change = can_change_color();
1192 printf("Term info:\n");
1193 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1194 getenv("TERM"), colors, pairs, can_change);
1198 printf("No color support\n");
1202 printf("Available colors:\n\n");
1203 lines = (colors - 1) / columns + 1;
1205 for (i = 0; i < lines; ++i) {
1206 for (j = 0; j < columns; ++j, ++color) {
1207 printf("\033[0;38;5;%dm %03d", color, color);
1220 setlocale(LC_ALL, "");
1222 TAILQ_INIT(&eecmd_history.head);
1223 TAILQ_INIT(&ir_history.head);
1224 TAILQ_INIT(&lu_history.head);
1226 ministate.line.type = LINE_TEXT;
1227 ministate.vline.parent = &ministate.line;
1228 ministate.buffer.current_line = &ministate.vline;
1230 /* initialize help window */
1231 TAILQ_INIT(&helpwin.head);
1233 base_map = &global_map;
1234 current_map = &global_map;
1238 if (enable_colors) {
1241 use_default_colors();
1246 config_apply_style();
1251 intrflush(stdscr, FALSE);
1253 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1255 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1257 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1259 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1261 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1263 if ((help = newwin(1, 1, 1, 0)) == NULL)
1266 body_lines = LINES-3;
1269 wbkgd(body, body_face.body);
1270 wbkgd(echoarea, minibuffer_face.background);
1275 scrollok(body, FALSE);
1277 /* non-blocking input */
1280 mvwprintw(body, 0, 0, "");
1283 * Dummy so libevent2 won't complain that no event_base is set
1284 * when checking event_pending for the first time
1286 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1287 evtimer_add(&clechoev, &clechoev_timer);
1288 evtimer_set(&resizeev, handle_resize, NULL);
1289 evtimer_add(&resizeev, &resize_timer);
1291 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1292 event_add(&stdioev, NULL);
1294 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1295 signal_add(&winchev, NULL);
1301 ui_on_tab_loaded(struct tab *tab)
1303 stop_loading_anim(tab);
1304 message("Loaded %s", tab->hist_cur->h);
1312 ui_on_tab_refresh(struct tab *tab)
1314 wrap_page(&tab->buffer, body_cols);
1315 if (tab->flags & TAB_CURRENT)
1318 tab->flags |= TAB_URGENT;
1328 ui_toggle_side_window(void)
1330 side_window = !side_window;
1335 * ugly hack, but otherwise the window doesn't get updated
1336 * until I call rearrange_windows a second time (e.g. via
1337 * C-l). I will be happy to know why something like this is
1340 rearrange_windows();
1341 rearrange_windows();
1345 ui_schedule_redraw(void)
1347 should_rearrange_windows = 1;
1351 ui_require_input(struct tab *tab, int hide)
1353 /* TODO: hard-switching to another tab is ugly */
1356 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1357 &ir_history, NULL, NULL);
1358 strlcpy(ministate.prompt, "Input required: ",
1359 sizeof(ministate.prompt));
1364 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1367 yornp(prompt, fn, data);
1368 redraw_tab(current_tab());
1372 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1375 completing_read(prompt, fn, data);
1376 redraw_tab(current_tab());
1384 kill(getpid(), SIGSTOP);
1388 rearrange_windows();