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*);
81 * Used to know when we're finished loading.
85 static int should_rearrange_windows;
89 struct thiskey thiskey;
90 struct tab *current_tab;
92 static struct event resizeev;
93 static struct timeval resize_timer = { 0, 250000 };
95 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
97 int body_lines, body_cols;
100 static struct buffer helpwin;
101 static int help_lines, help_cols;
103 static int side_window;
105 static struct event clechoev;
106 static struct timeval clechoev_timer = { 5, 0 };
107 static struct timeval loadingev_timer = { 0, 250000 };
109 static uint32_t tab_counter;
111 static char keybuf[64];
113 struct kmap global_map,
121 update_x_offset(void)
123 if (olivetti_mode && fill_column < body_cols)
124 x_offset = (body_cols - fill_column)/2;
130 save_excursion(struct excursion *place, struct buffer *buffer)
132 place->curs_x = buffer->curs_x;
133 place->curs_y = buffer->curs_y;
134 place->line_off = buffer->line_off;
135 place->current_line = buffer->current_line;
136 place->cpoff = buffer->cpoff;
140 restore_excursion(struct excursion *place, struct buffer *buffer)
142 buffer->curs_x = place->curs_x;
143 buffer->curs_y = place->curs_y;
144 buffer->line_off = place->line_off;
145 buffer->current_line = place->current_line;
146 buffer->cpoff = place->cpoff;
150 restore_curs_x(struct buffer *buffer)
155 vl = buffer->current_line;
156 if (vl == NULL || vl->line == NULL)
157 buffer->curs_x = buffer->cpoff = 0;
158 else if (vl->parent->data != NULL)
159 buffer->curs_x = utf8_snwidth(vl->parent->data+1, buffer->cpoff) + 1;
161 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
163 buffer->curs_x += x_offset;
168 if (vl->parent->data != NULL)
169 buffer->curs_x += utf8_swidth_between(vl->parent->line, vl->parent->data);
171 prfx = line_prefixes[vl->parent->type].prfx1;
172 buffer->curs_x += utf8_swidth(prfx);
177 global_key_unbound(void)
179 message("%s is undefined", keybuf);
182 static struct vline *
183 nth_line(struct buffer *buffer, size_t n)
189 TAILQ_FOREACH(vl, &buffer->head, vlines) {
203 return &ministate.buffer;
204 return ¤t_tab->buffer;
212 if ((thiskey.key = wgetch(body)) == ERR)
215 thiskey.meta = thiskey.key == 27;
217 thiskey.key = wgetch(body);
218 if (thiskey.key == ERR || thiskey.key == 27) {
225 if ((unsigned int)thiskey.key < UINT8_MAX) {
227 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
229 if ((thiskey.key = wgetch(body)) == ERR) {
230 message("Error decoding user input");
240 dispatch_stdio(int fd, short ev, void *d)
246 /* TODO: schedule a redraw? */
253 if (keybuf[0] != '\0')
254 strlcat(keybuf, " ", sizeof(keybuf));
256 strlcat(keybuf, "M-", sizeof(keybuf));
257 if (thiskey.cp != 0) {
258 utf8_encode(thiskey.cp, tmp);
259 strlcat(keybuf, tmp, sizeof(keybuf));
260 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
261 strlcat(keybuf, keyname, sizeof(keybuf));
263 tmp[0] = thiskey.key;
264 strlcat(keybuf, tmp, sizeof(keybuf));
267 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
268 if (k->meta == thiskey.meta &&
269 k->key == thiskey.key) {
271 current_map = &k->map;
273 current_map = base_map;
274 strlcpy(keybuf, "", sizeof(keybuf));
275 k->fn(current_buffer());
281 if (current_map->unhandled_input != NULL)
282 current_map->unhandled_input();
284 global_key_unbound();
286 strlcpy(keybuf, "", sizeof(keybuf));
287 current_map = base_map;
293 if (should_rearrange_windows)
295 redraw_tab(current_tab);
299 handle_clear_echoarea(int fd, short ev, void *d)
301 free(ministate.curmesg);
302 ministate.curmesg = NULL;
309 handle_resize(int sig, short ev, void *d)
311 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
312 event_del(&resizeev);
314 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
315 evtimer_add(&resizeev, &resize_timer);
319 handle_resize_nodelay(int s, short ev, void *d)
329 rearrange_windows(void)
333 should_rearrange_windows = 0;
337 if ((too_small = lines < 15)) {
339 printw("Window too small.");
344 /* move and resize the windows, in reverse order! */
346 if (in_minibuffer == MB_COMPREAD) {
347 mvwin(minibuffer, lines-10, 0);
348 wresize(minibuffer, 10, COLS);
351 wrap_page(&ministate.compl.buffer, COLS);
354 mvwin(echoarea, --lines, 0);
355 wresize(echoarea, 1, COLS);
357 mvwin(modeline, --lines, 0);
358 wresize(modeline, 1, COLS);
360 body_lines = --lines;
364 help_cols = 0.3 * COLS;
367 wresize(help, help_lines, help_cols);
369 wrap_page(&helpwin, help_cols);
371 body_cols = COLS - help_cols - 1;
372 mvwin(body, 1, help_cols);
377 wresize(body, body_lines, body_cols);
379 wresize(tabline, 1, COLS);
381 wrap_page(¤t_tab->buffer, body_cols);
382 redraw_tab(current_tab);
386 wrap_page(struct buffer *buffer, int width)
389 const struct line *top_orig, *orig;
394 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
395 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
397 buffer->top_line = NULL;
398 buffer->current_line = NULL;
400 buffer->force_redraw = 1;
402 buffer->line_off = 0;
406 TAILQ_FOREACH(l, &buffer->page.head, lines) {
407 prfx = line_prefixes[l->type].prfx1;
418 wrap_text(buffer, prfx, l, MIN(fill_column, width));
420 case LINE_PRE_CONTENT:
422 pre_width = MIN(fill_column, width);
425 hardwrap_text(buffer, l, pre_width);
428 case LINE_COMPL_CURRENT:
429 wrap_one(buffer, prfx, l, width);
433 if (top_orig == l && buffer->top_line == NULL) {
434 buffer->line_off = buffer->line_max-1;
435 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
438 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
439 if (vl == NULL || vl->parent != orig)
441 buffer->top_line = vl;
446 if (orig == l && buffer->current_line == NULL) {
447 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
450 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
451 if (vl == NULL || vl->parent != orig)
453 buffer->current_line = vl;
458 if (buffer->current_line == NULL)
459 buffer->current_line = TAILQ_FIRST(&buffer->head);
461 if (buffer->top_line == NULL)
462 buffer->top_line = buffer->current_line;
468 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
469 const char **prfx_ret, const char **text_ret)
471 int type, i, cont, width;
474 if ((*text_ret = vl->line) == NULL)
477 cont = vl->flags & L_CONTINUATION;
478 type = vl->parent->type;
480 *prfx_ret = line_prefixes[type].prfx1;
482 *prfx_ret = line_prefixes[type].prfx2;
484 space = vl->parent->data;
485 if (!emojify_link || type != LINE_LINK || space == NULL)
490 width = utf8_swidth_between(vl->parent->line, space);
491 for (i = 0; i < width + 1; ++i)
492 strlcat(buf, " ", len);
494 strlcpy(buf, *text_ret, len);
495 if ((t = strchr(buf, ' ')) != NULL)
497 strlcat(buf, " ", len);
500 *text_ret += (space - vl->parent->line) + 1;
507 * Core part of the rendering. It prints a vline starting from the
508 * current cursor position. Printing a vline consists of skipping
509 * `off' columns (for olivetti-mode), print the correct prefix (which
510 * may be the emoji in case of emojified links-lines), printing the
511 * text itself, filling until width - off and filling off columns
515 print_vline(int off, int width, WINDOW *window, struct vline *vl)
518 * Believe me or not, I've seen emoji ten code points long!
519 * That means, to stay large, 4*10 bytes + NUL.
521 char emojibuf[41] = {0};
522 const char *text, *prfx;
526 f = &line_faces[vl->parent->type];
528 /* unused, set by getyx */
531 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
533 wattr_on(window, body_face.left, NULL);
534 for (i = 0; i < off; i++)
536 wattr_off(window, body_face.left, NULL);
538 wattr_on(window, f->prefix, NULL);
539 wprintw(window, "%s", prfx);
540 wattr_off(window, f->prefix, NULL);
542 wattr_on(window, f->text, NULL);
543 wprintw(window, "%s", text);
544 wattr_off(window, f->text, NULL);
550 wattr_on(window, f->trail, NULL);
551 for (i = 0; i < left - off; ++i)
553 wattr_off(window, f->trail, NULL);
555 wattr_on(window, body_face.right, NULL);
556 for (i = 0; i < off; i++)
558 wattr_off(window, body_face.right, NULL);
566 size_t toskip, ots, tabwidth, space, x;
567 int current, y, truncated, pair;
573 /* unused, but setted by a getyx */
576 tabwidth = sizeof(buf)+1;
580 TAILQ_FOREACH(tab, &tabshead, tabs) {
582 if (tab == current_tab)
586 if (toskip * tabwidth < space)
591 while (toskip != 0 &&
592 (ots - toskip+1) * tabwidth < space)
597 wattr_on(tabline, tab_face.background, NULL);
598 wprintw(tabline, toskip == 0 ? " " : "<");
599 wattr_off(tabline, tab_face.background, NULL);
602 TAILQ_FOREACH(tab, &tabshead, tabs) {
610 getyx(tabline, y, x);
611 if (x + sizeof(buf)+2 >= (size_t)COLS)
614 current = tab == current_tab;
616 if (*(title = tab->buffer.page.title) == '\0')
617 title = tab->hist_cur->h;
619 if (tab->flags & TAB_URGENT)
620 strlcpy(buf, "!", sizeof(buf));
622 strlcpy(buf, " ", sizeof(buf));
624 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
625 /* truncation happens */
626 strlcpy(&buf[sizeof(buf)-4], "...", 4);
628 /* pad with spaces */
629 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
633 pair = current ? tab_face.current : tab_face.tab;
634 wattr_on(tabline, pair, NULL);
635 wprintw(tabline, "%s", buf);
636 wattr_off(tabline, pair, NULL);
638 wattr_on(tabline, tab_face.background, NULL);
639 if (TAILQ_NEXT(tab, tabs) != NULL)
640 wprintw(tabline, "│");
641 wattr_off(tabline, tab_face.background, NULL);
644 wattr_on(tabline, tab_face.background, NULL);
645 for (; x < (size_t)COLS; ++x)
646 waddch(tabline, ' ');
648 mvwprintw(tabline, 0, COLS-1, ">");
649 wattr_off(tabline, tab_face.background, NULL);
653 * Compute the first visible line around vl. Try to search forward
654 * until the end of the buffer; if a visible line is not found, search
655 * backward. Return NULL if no viable line was found.
658 adjust_line(struct vline *vl, struct buffer *buffer)
665 if (!(vl->parent->flags & L_HIDDEN))
670 t != NULL && t->parent->flags & L_HIDDEN;
671 t = TAILQ_NEXT(t, vlines))
677 /* search backward */
679 t != NULL && t->parent->flags & L_HIDDEN;
680 t = TAILQ_PREV(t, vhead, vlines))
687 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
692 restore_curs_x(buffer);
695 * TODO: ignoring buffer->force_update and always
696 * re-rendering. In theory we can recompute the y position
697 * without a re-render, and optimize here. It's not the only
698 * optimisation possible here, wscrl wolud also be an
706 if (TAILQ_EMPTY(&buffer->head))
709 if (buffer->top_line == NULL)
710 buffer->top_line = TAILQ_FIRST(&buffer->head);
712 buffer->top_line = adjust_line(buffer->top_line, buffer);
713 if (buffer->top_line == NULL)
716 buffer->current_line = adjust_line(buffer->current_line, buffer);
720 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
721 if (vl->parent->flags & L_HIDDEN)
725 print_vline(off, width, win, vl);
727 if (vl == buffer->current_line)
739 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
740 if (vl == buffer->current_line)
742 if (vl->parent->flags & L_HIDDEN)
745 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
752 buffer->last_line_off = buffer->line_off;
753 buffer->force_redraw = 0;
755 wmove(win, buffer->curs_y, buffer->curs_x);
761 redraw_window(help, 0, help_lines, help_cols, &helpwin);
765 redraw_body(struct tab *tab)
767 static struct tab *last_tab;
770 tab->buffer.force_redraw =1;
773 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
777 trust_status_char(enum trust_state ts)
780 case TS_UNKNOWN: return 'u';
781 case TS_UNTRUSTED: return '!';
782 case TS_TEMP_TRUSTED: return '!';
783 case TS_TRUSTED: return 'v';
784 case TS_VERIFIED: return 'V';
790 redraw_modeline(struct tab *tab)
793 int x, y, max_x, max_y;
794 const char *mode = tab->buffer.page.name;
795 const char *spin = "-\\|/";
798 wattr_on(modeline, modeline_face.background, NULL);
799 wmove(modeline, 0, 0);
801 wprintw(modeline, "-%c%c %s ",
802 spin[tab->loading_anim_step],
803 trust_status_char(tab->trust),
804 mode == NULL ? "(none)" : mode);
806 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
807 / tab->buffer.line_max;
809 if (tab->buffer.line_max <= (size_t)body_lines)
810 wprintw(modeline, "All ");
811 else if (tab->buffer.line_off == 0)
812 wprintw(modeline, "Top ");
813 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
814 wprintw(modeline, "Bottom ");
816 wprintw(modeline, "%.0f%% ", pct);
818 wprintw(modeline, "%d/%d %s ",
819 tab->buffer.line_off + tab->buffer.curs_y,
820 tab->buffer.line_max,
823 getyx(modeline, y, x);
824 getmaxyx(modeline, max_y, max_x);
829 for (; x < max_x; ++x)
830 waddstr(modeline, "-");
832 wattr_off(modeline, modeline_face.background, NULL);
836 redraw_minibuffer(void)
838 wattr_on(echoarea, minibuffer_face.background, NULL);
842 do_redraw_minibuffer();
844 do_redraw_echoarea();
846 if (in_minibuffer == MB_COMPREAD)
847 do_redraw_minibuffer_compl();
849 wattr_off(echoarea, minibuffer_face.background, NULL);
853 do_redraw_echoarea(void)
857 if (ministate.curmesg != NULL)
858 wprintw(echoarea, "%s", ministate.curmesg);
859 else if (*keybuf != '\0')
860 waddstr(echoarea, keybuf);
862 /* If nothing else, show the URL at point */
863 vl = current_tab->buffer.current_line;
864 if (vl != NULL && vl->parent->type == LINE_LINK)
865 waddstr(echoarea, vl->parent->alt);
870 do_redraw_minibuffer(void)
872 size_t off_y, off_x = 0;
873 const char *start, *c;
875 /* unused, set by getyx */
878 wmove(echoarea, 0, 0);
880 if (in_minibuffer == MB_COMPREAD)
881 wprintw(echoarea, "(%2d) ",
882 ministate.compl.buffer.line_max);
884 wprintw(echoarea, "%s", ministate.prompt);
885 if (ministate.hist_cur != NULL)
886 wprintw(echoarea, "(%zu/%zu) ",
887 ministate.hist_off + 1,
888 ministate.history->len);
890 getyx(echoarea, off_y, off_x);
892 start = ministate.hist_cur != NULL
893 ? ministate.hist_cur->h
895 c = utf8_nth(ministate.buffer.current_line->line,
896 ministate.buffer.cpoff);
897 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
898 start = utf8_next_cp(start);
901 waddstr(echoarea, start);
903 if (ministate.curmesg != NULL)
904 wprintw(echoarea, " [%s]", ministate.curmesg);
906 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
910 do_redraw_minibuffer_compl(void)
912 redraw_window(minibuffer, 0, 10, body_cols,
913 &ministate.compl.buffer);
917 * Place the cursor in the right ncurses window. If soft is 1, use
918 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
922 place_cursor(int soft)
924 int (*touch)(WINDOW *);
927 touch = wnoutrefresh;
941 redraw_tab(struct tab *tab)
953 redraw_modeline(tab);
956 wnoutrefresh(tabline);
957 wnoutrefresh(modeline);
959 if (in_minibuffer == MB_COMPREAD)
960 wnoutrefresh(minibuffer);
967 dprintf(1, "\033]0;%s - Telescope\a",
968 current_tab->buffer.page.title);
972 emit_help_item(char *prfx, void *fn)
977 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
983 if ((l = calloc(1, sizeof(*l))) == NULL)
989 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
991 if (TAILQ_EMPTY(&helpwin.page.head))
992 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
994 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
998 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1004 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1005 strlcpy(p, prfx, sizeof(p));
1007 strlcat(p, " ", sizeof(p));
1009 strlcat(p, "M-", sizeof(p));
1010 if ((kn = unkbd(k->key)) != NULL)
1011 strlcat(p, kn, sizeof(p));
1013 strlcat(p, keyname(k->key), sizeof(p));
1016 rec_compute_help(&k->map, p, sizeof(p));
1018 emit_help_item(p, k->fn);
1023 recompute_help(void)
1027 empty_vlist(&helpwin);
1028 empty_linelist(&helpwin);
1029 rec_compute_help(current_map, p, sizeof(p));
1030 wrap_page(&helpwin, help_cols);
1034 vmessage(const char *fmt, va_list ap)
1036 if (evtimer_pending(&clechoev, NULL))
1037 evtimer_del(&clechoev);
1039 free(ministate.curmesg);
1040 ministate.curmesg = NULL;
1043 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1044 evtimer_add(&clechoev, &clechoev_timer);
1046 /* TODO: what to do if the allocation fails here? */
1047 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1048 ministate.curmesg = NULL;
1051 redraw_minibuffer();
1056 message(const char *fmt, ...)
1066 start_loading_anim(struct tab *tab)
1068 if (tab->loading_anim)
1070 tab->loading_anim = 1;
1071 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1072 evtimer_add(&tab->loadingev, &loadingev_timer);
1076 update_loading_anim(int fd, short ev, void *d)
1078 struct tab *tab = d;
1080 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1082 if (tab == current_tab) {
1083 redraw_modeline(tab);
1090 evtimer_add(&tab->loadingev, &loadingev_timer);
1094 stop_loading_anim(struct tab *tab)
1096 if (!tab->loading_anim)
1098 evtimer_del(&tab->loadingev);
1099 tab->loading_anim = 0;
1100 tab->loading_anim_step = 0;
1102 if (tab != current_tab)
1105 redraw_modeline(tab);
1114 load_url_in_tab(struct tab *tab, const char *url)
1121 message("Loading %s...", url);
1122 start_loading_anim(tab);
1129 switch_to_tab(struct tab *tab)
1132 tab->flags &= ~TAB_URGENT;
1134 if (operating && tab->flags & TAB_LAZY)
1135 load_url_in_tab(tab, tab->hist_cur->h);
1141 return tab_counter++;
1145 new_tab(const char *url)
1149 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1155 TAILQ_INIT(&tab->hist.head);
1157 TAILQ_INIT(&tab->buffer.head);
1159 tab->id = tab_new_id();
1161 tab->flags |= TAB_LAZY;
1164 if (TAILQ_EMPTY(&tabshead))
1165 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1167 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1169 load_url_in_tab(tab, url);
1174 ui_print_colors(void)
1176 int colors = 0, pairs = 0, can_change = 0;
1177 int columns = 16, lines, color, i, j;
1182 use_default_colors();
1185 pairs = COLOR_PAIRS;
1186 can_change = can_change_color();
1190 printf("Term info:\n");
1191 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1192 getenv("TERM"), colors, pairs, can_change);
1196 printf("No color support\n");
1200 printf("Available colors:\n\n");
1201 lines = (colors - 1) / columns + 1;
1203 for (i = 0; i < lines; ++i) {
1204 for (j = 0; j < columns; ++j, ++color) {
1205 printf("\033[0;38;5;%dm %03d", color, color);
1218 setlocale(LC_ALL, "");
1220 TAILQ_INIT(&eecmd_history.head);
1221 TAILQ_INIT(&ir_history.head);
1222 TAILQ_INIT(&lu_history.head);
1224 ministate.line.type = LINE_TEXT;
1225 ministate.vline.parent = &ministate.line;
1226 ministate.buffer.current_line = &ministate.vline;
1228 /* initialize help window */
1229 TAILQ_INIT(&helpwin.head);
1231 base_map = &global_map;
1232 current_map = &global_map;
1236 if (enable_colors) {
1239 use_default_colors();
1244 config_apply_style();
1249 intrflush(stdscr, FALSE);
1251 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1253 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1255 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1257 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1259 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1261 if ((help = newwin(1, 1, 1, 0)) == NULL)
1264 body_lines = LINES-3;
1267 wbkgd(body, body_face.body);
1268 wbkgd(echoarea, minibuffer_face.background);
1273 scrollok(body, FALSE);
1275 /* non-blocking input */
1278 mvwprintw(body, 0, 0, "");
1281 * Dummy so libevent2 won't complain that no event_base is set
1282 * when checking event_pending for the first time
1284 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1285 evtimer_add(&clechoev, &clechoev_timer);
1286 evtimer_set(&resizeev, handle_resize, NULL);
1287 evtimer_add(&resizeev, &resize_timer);
1289 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1290 event_add(&stdioev, NULL);
1292 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1293 signal_add(&winchev, NULL);
1302 switch_to_tab(current_tab);
1303 redraw_tab(current_tab);
1309 ui_on_tab_loaded(struct tab *tab)
1311 stop_loading_anim(tab);
1312 message("Loaded %s", tab->hist_cur->h);
1320 ui_on_tab_refresh(struct tab *tab)
1322 wrap_page(&tab->buffer, body_cols);
1323 if (tab == current_tab)
1326 tab->flags |= TAB_URGENT;
1336 ui_toggle_side_window(void)
1338 side_window = !side_window;
1343 * ugly hack, but otherwise the window doesn't get updated
1344 * until I call rearrange_windows a second time (e.g. via
1345 * C-l). I will be happy to know why something like this is
1348 rearrange_windows();
1349 rearrange_windows();
1353 ui_schedule_redraw(void)
1355 should_rearrange_windows = 1;
1359 ui_require_input(struct tab *tab, int hide)
1361 /* TODO: hard-switching to another tab is ugly */
1364 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1365 &ir_history, NULL, NULL);
1366 strlcpy(ministate.prompt, "Input required: ",
1367 sizeof(ministate.prompt));
1372 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1375 yornp(prompt, fn, data);
1376 redraw_tab(current_tab);
1380 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1383 completing_read(prompt, fn, data);
1384 redraw_tab(current_tab);
1392 kill(getpid(), SIGSTOP);
1396 rearrange_windows();