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.
45 #include "minibuffer.h"
47 #include "telescope.h"
51 static struct event stdioev, winchev;
53 static void set_scroll_position(struct tab *, size_t, size_t);
55 static void restore_curs_x(struct buffer *);
57 static int readkey(void);
58 static void dispatch_stdio(int, short, void*);
59 static void handle_resize(int, short, void*);
60 static void handle_resize_nodelay(int, short, void*);
61 static void rearrange_windows(void);
62 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
63 static void print_vline(int, int, WINDOW*, struct vline*);
64 static void redraw_tabline(void);
65 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
66 static void redraw_download(void);
67 static void redraw_help(void);
68 static void redraw_body(struct tab*);
69 static void redraw_modeline(struct tab*);
70 static void redraw_minibuffer(void);
71 static void do_redraw_echoarea(void);
72 static void do_redraw_minibuffer(void);
73 static void do_redraw_minibuffer_compl(void);
74 static void place_cursor(int);
75 static void redraw_tab(struct tab*);
76 static void update_loading_anim(int, short, void*);
77 static void stop_loading_anim(struct tab*);
79 static int should_rearrange_windows;
80 static int show_tab_bar;
84 struct thiskey thiskey;
85 struct tab *current_tab;
87 static struct event resizeev;
88 static struct timeval resize_timer = { 0, 250000 };
90 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
92 int body_lines, body_cols;
95 /* not static so we can see them from help.c */
96 struct buffer helpwin;
97 int help_lines, help_cols;
99 static WINDOW *download;
100 /* not static so we can see them from download.c */
101 struct buffer downloadwin;
105 static int side_window;
106 static int in_side_window;
108 static struct timeval loadingev_timer = { 0, 250000 };
110 static char keybuf[64];
112 /* XXX: don't forget to init these in main() */
113 struct kmap global_map,
119 update_x_offset(void)
121 if (olivetti_mode && fill_column < body_cols)
122 x_offset = (body_cols - fill_column)/2;
128 set_scroll_position(struct tab *tab, size_t top, size_t cur)
135 last = TAILQ_FIRST(&tab->buffer.page.head);
136 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
137 if (last != vl->parent) {
142 if (!topfound && i == top) {
144 tab->buffer.top_line = vl;
148 tab->buffer.current_line = vl;
154 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
156 tab->buffer.current_line = tab->buffer.top_line;
160 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
168 if (tab->buffer.top_line == NULL ||
169 tab->buffer.current_line == NULL)
172 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
173 if (tab->buffer.top_line->parent == l)
175 if (tab->buffer.current_line->parent == l)
185 save_excursion(struct excursion *place, struct buffer *buffer)
187 place->curs_x = buffer->curs_x;
188 place->curs_y = buffer->curs_y;
189 place->line_off = buffer->line_off;
190 place->top_line = buffer->top_line;
191 place->current_line = buffer->current_line;
192 place->cpoff = buffer->cpoff;
196 restore_excursion(struct excursion *place, struct buffer *buffer)
198 buffer->curs_x = place->curs_x;
199 buffer->curs_y = place->curs_y;
200 buffer->line_off = place->line_off;
201 buffer->top_line = place->top_line;
202 buffer->current_line = place->current_line;
203 buffer->cpoff = place->cpoff;
207 restore_curs_x(struct buffer *buffer)
210 const char *prfx, *text;
212 vl = buffer->current_line;
213 if (vl == NULL || vl->line == NULL)
214 buffer->curs_x = buffer->cpoff = 0;
215 else if (vl->parent->data != NULL) {
216 text = vl->parent->data;
217 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
219 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
221 /* small hack: don't olivetti-mode the download pane */
222 if (buffer != &downloadwin)
223 buffer->curs_x += x_offset;
228 if (vl->parent->data != NULL)
229 buffer->curs_x += utf8_swidth_between(vl->parent->line,
232 prfx = line_prefixes[vl->parent->type].prfx1;
233 buffer->curs_x += utf8_swidth(prfx);
238 global_key_unbound(void)
240 message("%s is undefined", keybuf);
247 return &ministate.buffer;
248 if (in_side_window & SIDE_WINDOW_LEFT)
250 if (in_side_window & SIDE_WINDOW_BOTTOM)
252 return ¤t_tab->buffer;
260 if ((thiskey.key = wgetch(body)) == ERR)
263 thiskey.meta = thiskey.key == '\e';
265 thiskey.key = wgetch(body);
266 if (thiskey.key == ERR || thiskey.key == '\e') {
274 if ((unsigned int)thiskey.key >= UINT8_MAX)
278 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
280 if ((thiskey.key = wgetch(body)) == ERR) {
281 message("Error decoding user input");
290 dispatch_stdio(int fd, short ev, void *d)
296 /* TODO: schedule a redraw? */
303 if (keybuf[0] != '\0')
304 strlcat(keybuf, " ", sizeof(keybuf));
306 strlcat(keybuf, "M-", sizeof(keybuf));
307 if (thiskey.cp != 0) {
308 utf8_encode(thiskey.cp, tmp);
309 strlcat(keybuf, tmp, sizeof(keybuf));
310 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
311 strlcat(keybuf, keyname, sizeof(keybuf));
313 tmp[0] = thiskey.key;
314 strlcat(keybuf, tmp, sizeof(keybuf));
317 lk = lookup_key(¤t_map, &thiskey, current_buffer());
318 if (lk == LK_UNBOUND) {
319 if (current_map->unhandled_input != NULL)
320 current_map->unhandled_input();
322 global_key_unbound();
324 if (lk != LK_ADVANCED_MAP) {
325 current_map = base_map;
326 strlcpy(keybuf, "", sizeof(keybuf));
329 if (side_window & SIDE_WINDOW_LEFT)
332 if (should_rearrange_windows)
334 redraw_tab(current_tab);
338 handle_resize(int sig, short ev, void *d)
340 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
341 event_del(&resizeev);
343 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
344 evtimer_add(&resizeev, &resize_timer);
348 handle_resize_nodelay(int s, short ev, void *d)
358 should_show_tab_bar(void)
360 if (tab_bar_show == -1)
362 if (tab_bar_show == 0)
365 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
369 rearrange_windows(void)
372 int minibuffer_lines;
374 should_rearrange_windows = 0;
375 show_tab_bar = should_show_tab_bar();
379 /* 3 lines for the ui and 12 for the */
380 if ((too_small = lines < 15)) {
382 printw("Window too small.");
387 /* move and resize the windows, in reverse order! */
389 if (in_minibuffer == MB_COMPREAD) {
390 minibuffer_lines = MIN(10, lines/2);
391 mvwin(minibuffer, lines - minibuffer_lines, 0);
392 wresize(minibuffer, minibuffer_lines, COLS);
393 lines -= minibuffer_lines;
395 wrap_page(&ministate.compl.buffer, COLS);
398 mvwin(echoarea, --lines, 0);
399 wresize(echoarea, 1, COLS);
401 mvwin(modeline, --lines, 0);
402 wresize(modeline, 1, COLS);
404 if (side_window & SIDE_WINDOW_BOTTOM) {
405 download_lines = MIN(5, lines/2);
406 download_cols = COLS;
407 mvwin(download, lines - download_lines, 0);
408 wresize(download, download_lines, download_cols);
409 lines -= download_lines;
411 wrap_page(&downloadwin, download_cols);
414 body_lines = show_tab_bar ? --lines : lines;
418 * Here we make the assumption that show_tab_bar is either 0
419 * or 1, and reuse that as argument to mvwin.
421 if (side_window & SIDE_WINDOW_LEFT) {
422 help_cols = 0.3 * COLS;
424 mvwin(help, show_tab_bar, 0);
425 wresize(help, help_lines, help_cols);
427 wrap_page(&helpwin, help_cols);
429 body_cols = COLS - help_cols - 1;
430 mvwin(body, show_tab_bar, help_cols);
432 mvwin(body, show_tab_bar, 0);
435 wresize(body, body_lines, body_cols);
438 wresize(tabline, 1, COLS);
440 wrap_page(¤t_tab->buffer, body_cols);
441 redraw_tab(current_tab);
445 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
446 const char **prfx_ret, const char **text_ret)
448 int type, i, cont, width;
451 if ((*text_ret = vl->line) == NULL)
454 cont = vl->flags & L_CONTINUATION;
455 type = vl->parent->type;
457 *prfx_ret = line_prefixes[type].prfx1;
459 *prfx_ret = line_prefixes[type].prfx2;
461 space = vl->parent->data;
462 if (!emojify_link || type != LINE_LINK || space == NULL)
467 width = utf8_swidth_between(vl->parent->line, space);
468 for (i = 0; i < width + 1; ++i)
469 strlcat(buf, " ", len);
471 strlcpy(buf, *text_ret, len);
472 if ((t = strchr(buf, ' ')) != NULL)
474 strlcat(buf, " ", len);
477 *text_ret += (space - vl->parent->line) + 1;
484 print_vline_descr(int width, WINDOW *window, struct vline *vl)
488 switch (vl->parent->type) {
490 case LINE_COMPL_CURRENT:
494 case LINE_DOWNLOAD_DONE:
504 if (vl->parent->alt == NULL)
511 wprintw(window, " ");
512 for (; goal > x; ++x)
513 wprintw(window, " ");
515 wprintw(window, "%s", vl->parent->alt);
519 * Core part of the rendering. It prints a vline starting from the
520 * current cursor position. Printing a vline consists of skipping
521 * `off' columns (for olivetti-mode), print the correct prefix (which
522 * may be the emoji in case of emojified links-lines), printing the
523 * text itself, filling until width - off and filling off columns
527 print_vline(int off, int width, WINDOW *window, struct vline *vl)
530 * Believe me or not, I've seen emoji ten code points long!
531 * That means, to stay large, 4*10 bytes + NUL.
533 char emojibuf[41] = {0};
534 const char *text, *prfx;
538 f = &line_faces[vl->parent->type];
540 /* unused, set by getyx */
543 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
546 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
548 wattr_on(window, body_face.left, NULL);
549 for (i = 0; i < off; i++)
551 wattr_off(window, body_face.left, NULL);
553 wattr_on(window, f->prefix, NULL);
554 wprintw(window, "%s", prfx);
555 wattr_off(window, f->prefix, NULL);
557 wattr_on(window, f->text, NULL);
558 wprintw(window, "%s", text);
559 print_vline_descr(width, window, vl);
560 wattr_off(window, f->text, NULL);
566 wattr_on(window, f->trail, NULL);
567 for (i = 0; i < left - off; ++i)
569 wattr_off(window, f->trail, NULL);
571 wattr_on(window, body_face.right, NULL);
572 for (i = 0; i < off; i++)
574 wattr_off(window, body_face.right, NULL);
582 size_t toskip, ots, tabwidth, space, x;
583 int current, y, truncated, pair;
589 /* unused, but setted by a getyx */
592 tabwidth = sizeof(buf)+1;
596 TAILQ_FOREACH(tab, &tabshead, tabs) {
598 if (tab == current_tab)
602 if (toskip * tabwidth <= space)
607 while (toskip != 0 &&
608 (ots - toskip+1) * tabwidth < space)
613 wattr_on(tabline, tab_face.background, NULL);
614 wprintw(tabline, toskip == 0 ? " " : "<");
615 wattr_off(tabline, tab_face.background, NULL);
618 TAILQ_FOREACH(tab, &tabshead, tabs) {
626 getyx(tabline, y, x);
627 if (x + sizeof(buf)+2 >= (size_t)COLS)
630 current = tab == current_tab;
632 if (*(title = tab->buffer.page.title) == '\0')
633 title = tab->hist_cur->h;
635 if (tab->flags & TAB_URGENT)
636 strlcpy(buf, "!", sizeof(buf));
638 strlcpy(buf, " ", sizeof(buf));
640 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
641 /* truncation happens */
642 strlcpy(&buf[sizeof(buf)-4], "...", 4);
644 /* pad with spaces */
645 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
649 pair = current ? tab_face.current : tab_face.tab;
650 wattr_on(tabline, pair, NULL);
651 wprintw(tabline, "%s", buf);
652 wattr_off(tabline, pair, NULL);
654 wattr_on(tabline, tab_face.background, NULL);
655 if (TAILQ_NEXT(tab, tabs) != NULL)
656 wprintw(tabline, "┃");
657 wattr_off(tabline, tab_face.background, NULL);
660 wattr_on(tabline, tab_face.background, NULL);
661 for (; x < (size_t)COLS; ++x)
662 waddch(tabline, ' ');
664 mvwprintw(tabline, 0, COLS-1, ">");
665 wattr_off(tabline, tab_face.background, NULL);
669 * Compute the first visible line around vl. Try to search forward
670 * until the end of the buffer; if a visible line is not found, search
671 * backward. Return NULL if no viable line was found.
674 adjust_line(struct vline *vl, struct buffer *buffer)
681 if (!(vl->parent->flags & L_HIDDEN))
686 t != NULL && t->parent->flags & L_HIDDEN;
687 t = TAILQ_NEXT(t, vlines))
693 /* search backward */
695 t != NULL && t->parent->flags & L_HIDDEN;
696 t = TAILQ_PREV(t, vhead, vlines))
703 redraw_window(WINDOW *win, int off, int height, int width,
704 int show_fringe, struct buffer *buffer)
707 int onscreen = 0, l = 0;
709 restore_curs_x(buffer);
712 * TODO: ignoring buffer->force_update and always
713 * re-rendering. In theory we can recompute the y position
714 * without a re-render, and optimize here. It's not the only
715 * optimisation possible here, wscrl wolud also be an
723 if (TAILQ_EMPTY(&buffer->head))
726 if (buffer->top_line == NULL)
727 buffer->top_line = TAILQ_FIRST(&buffer->head);
729 buffer->top_line = adjust_line(buffer->top_line, buffer);
730 if (buffer->top_line == NULL)
733 buffer->current_line = adjust_line(buffer->current_line, buffer);
735 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
736 if (vl->parent->flags & L_HIDDEN)
740 print_vline(off, width, win, vl);
742 if (vl == buffer->current_line)
754 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
755 if (vl == buffer->current_line)
757 if (vl->parent->flags & L_HIDDEN)
760 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
767 buffer->last_line_off = buffer->line_off;
768 buffer->force_redraw = 0;
770 for (; show_fringe && l < height; l++)
771 print_vline(off, width, win, &fringe);
773 wmove(win, buffer->curs_y, buffer->curs_x);
777 redraw_download(void)
779 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
785 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
789 redraw_body(struct tab *tab)
791 static struct tab *last_tab;
794 tab->buffer.force_redraw =1;
797 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
801 trust_status_char(enum trust_state ts)
804 case TS_UNKNOWN: return '-';
805 case TS_UNTRUSTED: return '!';
806 case TS_TEMP_TRUSTED: return '!';
807 case TS_TRUSTED: return 'v';
808 case TS_VERIFIED: return 'V';
814 redraw_modeline(struct tab *tab)
816 struct buffer *buffer;
818 int x, y, max_x, max_y;
820 const char *spin = "-\\|/";
822 buffer = current_buffer();
823 mode = buffer->page.name;
826 wattr_on(modeline, modeline_face.background, NULL);
827 wmove(modeline, 0, 0);
829 wprintw(modeline, "-%c%c- %s ",
830 spin[tab->loading_anim_step],
831 trust_status_char(tab->trust),
832 mode == NULL ? "(none)" : mode);
834 pct = (buffer->line_off + buffer->curs_y) * 100.0
837 if (buffer->line_max <= (size_t)body_lines)
838 wprintw(modeline, "All ");
839 else if (buffer->line_off == 0)
840 wprintw(modeline, "Top ");
841 else if (buffer->line_off + body_lines >= buffer->line_max)
842 wprintw(modeline, "Bottom ");
844 wprintw(modeline, "%.0f%% ", pct);
846 wprintw(modeline, "%zu/%zu %s ",
847 buffer->line_off + buffer->curs_y,
851 getyx(modeline, y, x);
852 getmaxyx(modeline, max_y, max_x);
857 for (; x < max_x; ++x)
858 waddstr(modeline, "-");
860 wattr_off(modeline, modeline_face.background, NULL);
864 redraw_minibuffer(void)
866 wattr_on(echoarea, minibuffer_face.background, NULL);
870 do_redraw_minibuffer();
872 do_redraw_echoarea();
874 if (in_minibuffer == MB_COMPREAD)
875 do_redraw_minibuffer_compl();
877 wattr_off(echoarea, minibuffer_face.background, NULL);
881 do_redraw_echoarea(void)
885 if (ministate.curmesg != NULL)
886 wprintw(echoarea, "%s", ministate.curmesg);
887 else if (*keybuf != '\0')
888 waddstr(echoarea, keybuf);
890 /* If nothing else, show the URL at point */
891 vl = current_tab->buffer.current_line;
892 if (vl != NULL && vl->parent->type == LINE_LINK)
893 waddstr(echoarea, vl->parent->alt);
898 do_redraw_minibuffer(void)
900 struct buffer *cmplbuf, *buffer;
901 size_t off_y, off_x = 0;
902 const char *start, *c;
904 cmplbuf = &ministate.compl.buffer;
905 buffer = &ministate.buffer;
906 (void)off_y; /* unused, set by getyx */
908 wmove(echoarea, 0, 0);
910 if (in_minibuffer == MB_COMPREAD)
911 wprintw(echoarea, "(%2zu) ",
914 wprintw(echoarea, "%s", ministate.prompt);
915 if (ministate.hist_cur != NULL)
916 wprintw(echoarea, "(%zu/%zu) ",
917 ministate.hist_off + 1,
918 ministate.history->len);
920 getyx(echoarea, off_y, off_x);
922 start = ministate.hist_cur != NULL
923 ? ministate.hist_cur->h
925 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
926 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
927 start = utf8_next_cp(start);
930 waddstr(echoarea, start);
932 if (ministate.curmesg != NULL)
933 wprintw(echoarea, " [%s]", ministate.curmesg);
935 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
939 do_redraw_minibuffer_compl(void)
941 redraw_window(minibuffer, 0, 10, COLS, 1,
942 &ministate.compl.buffer);
946 * Place the cursor in the right ncurses window. If soft is 1, use
947 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
951 place_cursor(int soft)
953 int (*touch)(WINDOW *);
956 touch = wnoutrefresh;
961 if (side_window & SIDE_WINDOW_LEFT)
963 if (side_window & SIDE_WINDOW_BOTTOM)
967 } else if (in_side_window & SIDE_WINDOW_LEFT) {
970 if (in_side_window & SIDE_WINDOW_BOTTOM)
973 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
976 if (in_side_window & SIDE_WINDOW_LEFT)
980 if (side_window & SIDE_WINDOW_LEFT)
982 if (side_window & SIDE_WINDOW_BOTTOM)
990 redraw_tab(struct tab *tab)
995 if (side_window & SIDE_WINDOW_LEFT) {
1000 if (side_window & SIDE_WINDOW_BOTTOM) {
1002 wnoutrefresh(download);
1009 redraw_modeline(tab);
1010 redraw_minibuffer();
1012 wnoutrefresh(tabline);
1013 wnoutrefresh(modeline);
1015 if (in_minibuffer == MB_COMPREAD)
1016 wnoutrefresh(minibuffer);
1023 dprintf(1, "\033]2;%s - Telescope\a",
1024 current_tab->buffer.page.title);
1028 start_loading_anim(struct tab *tab)
1030 if (tab->loading_anim)
1032 tab->loading_anim = 1;
1033 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1034 evtimer_add(&tab->loadingev, &loadingev_timer);
1038 update_loading_anim(int fd, short ev, void *d)
1040 struct tab *tab = d;
1042 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1044 if (tab == current_tab) {
1045 redraw_modeline(tab);
1052 evtimer_add(&tab->loadingev, &loadingev_timer);
1056 stop_loading_anim(struct tab *tab)
1058 if (!tab->loading_anim)
1060 evtimer_del(&tab->loadingev);
1061 tab->loading_anim = 0;
1062 tab->loading_anim_step = 0;
1064 if (tab != current_tab)
1067 redraw_modeline(tab);
1076 ui_print_colors(void)
1078 int colors = 0, pairs = 0, can_change = 0;
1079 int columns = 16, lines, color, i, j;
1084 use_default_colors();
1087 pairs = COLOR_PAIRS;
1088 can_change = can_change_color();
1092 printf("Term info:\n");
1093 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1094 getenv("TERM"), colors, pairs, can_change);
1098 printf("No color support\n");
1102 printf("Available colors:\n\n");
1103 lines = (colors - 1) / columns + 1;
1105 for (i = 0; i < lines; ++i) {
1106 for (j = 0; j < columns; ++j, ++color) {
1107 printf("\033[0;38;5;%dm %03d", color, color);
1120 setlocale(LC_ALL, "");
1122 if (TAILQ_EMPTY(&global_map.m)) {
1123 fprintf(stderr, "no keys defined!\n");
1129 /* initialize download window */
1130 TAILQ_INIT(&downloadwin.head);
1131 TAILQ_INIT(&downloadwin.page.head);
1133 /* initialize help window */
1134 TAILQ_INIT(&helpwin.head);
1135 TAILQ_INIT(&helpwin.page.head);
1137 base_map = &global_map;
1138 current_map = &global_map;
1142 if (enable_colors) {
1145 use_default_colors();
1150 config_apply_style();
1155 intrflush(stdscr, FALSE);
1157 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1159 if ((body = newwin(1, 1, 0, 0)) == NULL)
1161 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1163 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1165 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1167 if ((download = newwin(1, 1, 0, 0)) == NULL)
1169 if ((help = newwin(1, 1, 0, 0)) == NULL)
1172 wbkgd(body, body_face.body);
1173 wbkgd(download, download_face.background);
1174 wbkgd(echoarea, minibuffer_face.background);
1179 scrollok(body, FALSE);
1181 /* non-blocking input */
1185 mvwprintw(body, 0, 0, "");
1193 evtimer_set(&resizeev, handle_resize, NULL);
1195 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1196 event_add(&stdioev, NULL);
1198 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1199 signal_add(&winchev, NULL);
1201 switch_to_tab(current_tab);
1202 rearrange_windows();
1206 ui_on_tab_loaded(struct tab *tab)
1208 stop_loading_anim(tab);
1209 message("Loaded %s", tab->hist_cur->h);
1211 if (tab->hist_cur->current_off != 0 &&
1212 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1213 set_scroll_position(tab, tab->hist_cur->line_off,
1214 tab->hist_cur->current_off);
1227 ui_on_tab_refresh(struct tab *tab)
1229 wrap_page(&tab->buffer, body_cols);
1230 if (tab == current_tab)
1233 tab->flags |= TAB_URGENT;
1237 ui_on_download_refresh(void)
1239 if (side_window & SIDE_WINDOW_BOTTOM) {
1240 recompute_downloads();
1241 redraw_tab(current_tab);
1246 ui_remotely_open_link(const char *uri)
1248 new_tab(uri, NULL, NULL);
1249 ui_on_tab_refresh(current_tab);
1263 ui_toggle_side_window(int kind)
1265 if (in_side_window & kind)
1268 side_window ^= kind;
1269 if (side_window & SIDE_WINDOW_LEFT)
1271 if (side_window & SIDE_WINDOW_BOTTOM)
1272 recompute_downloads();
1275 * ugly hack, but otherwise the window doesn't get updated
1276 * until I call rearrange_windows a second time (e.g. via
1277 * C-l). I will be happy to know why something like this is
1280 rearrange_windows();
1281 rearrange_windows();
1285 ui_show_downloads_pane(void)
1287 if (!(side_window & SIDE_WINDOW_BOTTOM))
1288 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1292 ui_schedule_redraw(void)
1294 should_rearrange_windows = 1;
1298 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1300 /* TODO: hard-switching to another tab is ugly */
1303 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1304 &ir_history, NULL, NULL);
1305 strlcpy(ministate.prompt, "Input required: ",
1306 sizeof(ministate.prompt));
1311 ui_after_message_hook(void)
1313 redraw_minibuffer();
1318 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1321 yornp(prompt, fn, data);
1322 redraw_tab(current_tab);
1326 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1327 struct tab *data, const char *input)
1329 minibuffer_read(prompt, fn, data);
1331 if (input != NULL) {
1332 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1333 cmd_move_end_of_line(&ministate.buffer);
1336 redraw_tab(current_tab);
1340 ui_other_window(void)
1342 if (in_side_window & SIDE_WINDOW_LEFT &&
1343 side_window & SIDE_WINDOW_BOTTOM)
1344 in_side_window = SIDE_WINDOW_BOTTOM;
1345 else if (in_side_window)
1347 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1348 in_side_window = SIDE_WINDOW_LEFT;
1349 else if (!in_side_window && side_window)
1350 in_side_window = SIDE_WINDOW_BOTTOM;
1352 message("No other window to select");
1360 kill(getpid(), SIGSTOP);
1364 rearrange_windows();