2 * Copyright (c) 2021, 2024 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.
56 #include "minibuffer.h"
58 #include "telescope.h"
62 static void set_scroll_position(struct tab *, size_t, size_t);
64 static void restore_curs_x(struct buffer *);
66 static int readkey(void);
67 static void dispatch_stdio(int, int, void*);
68 static void handle_signal(int, int, void*);
69 static void handle_resize_nodelay(int, int, void*);
70 static void handle_download_refresh(int, int, void *);
71 static void rearrange_windows(void);
72 static void line_prefix_and_text(int, struct vline *, char *, size_t, const char **, const char **, int *);
73 static void print_vline(int, int, WINDOW*, struct vline*);
74 static void redraw_tabline(void);
75 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
76 static void redraw_download(void);
77 static void redraw_help(void);
78 static void redraw_body(struct tab*);
79 static void redraw_modeline(struct tab*);
80 static void redraw_minibuffer(void);
81 static void do_redraw_echoarea(void);
82 static void do_redraw_minibuffer(void);
83 static void do_redraw_minibuffer_compl(void);
84 static void place_cursor(int);
85 static void redraw_tab(struct tab*);
86 static void update_loading_anim(int, int, void*);
87 static void stop_loading_anim(struct tab*);
89 static int should_rearrange_windows;
90 static int show_tab_bar;
94 struct thiskey thiskey;
95 struct tab *current_tab;
97 static unsigned int resize_timer;
98 static struct timeval resize_tv = { 0, 250000 };
100 static unsigned int download_timer;
101 static struct timeval download_refresh_timer = { 0, 250000 };
103 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
105 int body_lines, body_cols;
108 /* not static so we can see them from help.c */
109 struct buffer helpwin;
110 int help_lines, help_cols;
112 static WINDOW *download;
113 /* not static so we can see them from download.c */
114 struct buffer downloadwin;
118 static int side_window;
119 static int in_side_window;
121 static struct timeval loading_tv = { 0, 250000 };
123 static char keybuf[64];
125 /* XXX: don't forget to init these in main() */
126 struct kmap global_map,
132 update_x_offset(void)
134 if (olivetti_mode && fill_column < body_cols)
135 x_offset = (body_cols - fill_column)/2;
141 set_scroll_position(struct tab *tab, size_t top, size_t cur)
148 last = TAILQ_FIRST(&tab->buffer.head);
149 TAILQ_FOREACH(vl, &tab->buffer.vhead, vlines) {
150 if (last != vl->parent) {
155 if (!topfound && i == top) {
157 tab->buffer.top_line = vl;
161 tab->buffer.current_line = vl;
167 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.vhead);
169 tab->buffer.current_line = tab->buffer.top_line;
173 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
181 if (tab->buffer.top_line == NULL ||
182 tab->buffer.current_line == NULL)
185 TAILQ_FOREACH(l, &tab->buffer.head, lines) {
186 if (tab->buffer.top_line->parent == l)
188 if (tab->buffer.current_line->parent == l)
198 save_excursion(struct excursion *place, struct buffer *buffer)
200 place->curs_x = buffer->curs_x;
201 place->curs_y = buffer->curs_y;
202 place->line_off = buffer->line_off;
203 place->top_line = buffer->top_line;
204 place->current_line = buffer->current_line;
205 place->point_offset = buffer->point_offset;
209 restore_excursion(struct excursion *place, struct buffer *buffer)
211 buffer->curs_x = place->curs_x;
212 buffer->curs_y = place->curs_y;
213 buffer->line_off = place->line_off;
214 buffer->top_line = place->top_line;
215 buffer->current_line = place->current_line;
216 buffer->point_offset = place->point_offset;
220 restore_curs_x(struct buffer *buffer)
223 struct lineprefix *lp = line_prefixes;
224 const char *prfx, *text;
226 if (dont_apply_styling)
231 /* small hack: don't olivetti-mode the download pane */
232 if (buffer != &downloadwin)
233 buffer->curs_x += x_offset;
235 vl = buffer->current_line;
236 if (vl == NULL || vl->len == 0 || vl->parent == NULL)
237 buffer->curs_x += buffer->point_offset = 0;
238 else if (vl->parent->data != NULL) {
239 text = vl->parent->data;
240 buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
243 text = vl->parent->line + vl->from;
244 buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
251 if (vl->parent->data != NULL)
252 buffer->curs_x += utf8_swidth_between(vl->parent->line,
253 vl->parent->data, buffer->curs_x);
255 prfx = lp[vl->parent->type].prfx1;
256 buffer->curs_x += utf8_swidth(prfx, buffer->curs_x);
261 global_key_unbound(void)
263 message("%s is undefined", keybuf);
270 return &ministate.buffer;
271 if (in_side_window & SIDE_WINDOW_LEFT)
273 if (in_side_window & SIDE_WINDOW_BOTTOM)
275 return ¤t_tab->buffer;
283 if ((thiskey.key = wgetch(body)) == ERR)
286 thiskey.meta = thiskey.key == '\e';
288 thiskey.key = wgetch(body);
289 if (thiskey.key == ERR || thiskey.key == '\e') {
297 if ((unsigned int)thiskey.key >= UINT8_MAX)
301 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
303 if ((thiskey.key = wgetch(body)) == ERR) {
304 message("Error decoding user input");
313 dispatch_stdio(int fd, int ev, void *d)
319 /* TODO: schedule a redraw? */
326 if (keybuf[0] != '\0')
327 strlcat(keybuf, " ", sizeof(keybuf));
329 strlcat(keybuf, "M-", sizeof(keybuf));
330 if (thiskey.cp != 0) {
331 grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
332 strlcat(keybuf, tmp, sizeof(keybuf));
333 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
334 strlcat(keybuf, keyname, sizeof(keybuf));
336 tmp[0] = thiskey.key;
337 strlcat(keybuf, tmp, sizeof(keybuf));
340 lk = lookup_key(¤t_map, &thiskey, current_buffer());
341 if (lk == LK_UNBOUND) {
342 if (current_map->unhandled_input != NULL)
343 current_map->unhandled_input();
345 global_key_unbound();
347 if (lk != LK_ADVANCED_MAP) {
348 current_map = base_map;
349 strlcpy(keybuf, "", sizeof(keybuf));
352 if (side_window & SIDE_WINDOW_LEFT)
355 if (should_rearrange_windows)
357 redraw_tab(current_tab);
361 handle_signal(int sig, int ev, void *d)
367 ev_timer_cancel(resize_timer);
368 resize_timer = ev_timer(&resize_tv, handle_resize_nodelay,
373 ret = waitpid(-1, NULL, WNOHANG);
374 } while (ret == -1 && errno == EINTR);
380 handle_resize_nodelay(int s, int ev, void *d)
390 handle_download_refresh(int s, int v, void *d)
392 if (side_window & SIDE_WINDOW_BOTTOM) {
393 recompute_downloads();
394 redraw_tab(current_tab);
399 should_show_tab_bar(void)
401 if (tab_bar_show == -1)
403 if (tab_bar_show == 0)
406 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
410 rearrange_windows(void)
413 int minibuffer_lines;
415 should_rearrange_windows = 0;
416 show_tab_bar = should_show_tab_bar();
420 /* 3 lines for the ui and 12 for the body and minibuffer */
421 if ((too_small = lines < 15)) {
423 printw("Window too small.");
428 /* move and resize the windows, in reverse order! */
430 if (in_minibuffer == MB_COMPREAD) {
431 minibuffer_lines = MIN(10, lines/2);
432 mvwin(minibuffer, lines - minibuffer_lines, 0);
433 wresize(minibuffer, minibuffer_lines, COLS);
434 lines -= minibuffer_lines;
436 wrap_page(&ministate.compl.buffer, COLS, 0);
439 mvwin(echoarea, --lines, 0);
440 wresize(echoarea, 1, COLS);
442 mvwin(modeline, --lines, 0);
443 wresize(modeline, 1, COLS);
445 if (side_window & SIDE_WINDOW_BOTTOM) {
446 download_lines = MIN(5, lines/2);
447 download_cols = COLS;
448 mvwin(download, lines - download_lines, 0);
449 wresize(download, download_lines, download_cols);
450 lines -= download_lines;
452 wrap_page(&downloadwin, download_cols, 0);
455 body_lines = show_tab_bar ? --lines : lines;
459 * Here we make the assumption that show_tab_bar is either 0
460 * or 1, and reuse that as argument to mvwin.
462 if (side_window & SIDE_WINDOW_LEFT) {
463 help_cols = 0.3 * COLS;
465 mvwin(help, show_tab_bar, 0);
466 wresize(help, help_lines, help_cols);
468 wrap_page(&helpwin, help_cols, 0);
470 body_cols = COLS - help_cols - 1;
471 mvwin(body, show_tab_bar, help_cols);
473 mvwin(body, show_tab_bar, 0);
476 wresize(body, body_lines, body_cols);
479 wresize(tabline, 1, COLS);
481 wrap_page(¤t_tab->buffer, body_cols, x_offset);
482 redraw_tab(current_tab);
486 line_prefix_and_text(int col, struct vline *vl, char *buf, size_t len,
487 const char **prfx_ret, const char **text_ret, int *text_len)
489 struct lineprefix *lp = line_prefixes;
494 if (dont_apply_styling)
502 cont = vl->flags & L_CONTINUATION;
503 type = vl->parent->type;
505 *prfx_ret = lp[type].prfx1;
507 *prfx_ret = lp[type].prfx2;
509 space = vl->parent->data;
510 *text_ret = vl->parent->line + vl->from;
511 *text_len = MIN(INT_MAX, vl->len);
512 if (!emojify_link || type != LINE_LINK || space == NULL) {
518 width = utf8_swidth_between(vl->parent->line, space, col);
519 for (i = 0; i < width + 1 && i < len - 1; ++i)
522 strlcpy(buf, vl->parent->line, len);
523 if ((t = strchr(buf, ' ')) != NULL)
525 strlcat(buf, " ", len);
532 print_vline_descr(int width, WINDOW *window, struct vline *vl)
536 switch (vl->parent->type) {
538 case LINE_COMPL_CURRENT:
542 case LINE_DOWNLOAD_DONE:
552 if (vl->parent->alt == NULL)
559 wprintw(window, " ");
560 for (; goal > x; ++x)
561 wprintw(window, " ");
563 wprintw(window, "%s", vl->parent->alt);
567 * Core part of the rendering. It prints a vline starting from the
568 * current cursor position. Printing a vline consists of skipping
569 * `off' columns (for olivetti-mode), print the correct prefix (which
570 * may be the emoji in case of emojified links-lines), printing the
571 * text itself, filling until width - off and filling off columns
575 print_vline(int off, int width, WINDOW *window, struct vline *vl)
578 * Believe me or not, I've seen emoji ten code points long!
579 * That means, to stay large, 4*10 bytes + NUL.
581 char emojibuf[41] = {0};
582 const char *text, *prfx;
584 int i, left, x, y, textlen;
586 f = &line_faces[vl->parent->type];
588 /* unused, set by getyx */
591 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
594 line_prefix_and_text(off, vl, emojibuf, sizeof(emojibuf), &prfx,
597 wattr_on(window, body_face.left, NULL);
598 for (i = 0; i < off; i++)
600 wattr_off(window, body_face.left, NULL);
602 wattr_on(window, f->prefix, NULL);
603 wprintw(window, "%s", prfx);
604 wattr_off(window, f->prefix, NULL);
606 wattr_on(window, f->text, NULL);
608 wprintw(window, "%.*s", textlen, text);
609 print_vline_descr(width, window, vl);
610 wattr_off(window, f->text, NULL);
616 wattr_on(window, f->trail, NULL);
617 for (i = 0; i < left - off; ++i)
619 wattr_off(window, f->trail, NULL);
621 wattr_on(window, body_face.right, NULL);
622 for (i = 0; i < off; i++)
624 wattr_off(window, body_face.right, NULL);
631 size_t toskip, ots, tabwidth, space, x;
632 int current, y, truncated, pair;
638 /* unused, but set by a getyx */
641 tabwidth = sizeof(buf)+1;
645 TAILQ_FOREACH(tab, &tabshead, tabs) {
647 if (tab == current_tab)
651 if (toskip * tabwidth <= space)
656 while (toskip != 0 &&
657 (ots - toskip+1) * tabwidth < space)
662 wattr_on(tabline, tab_face.background, NULL);
663 wprintw(tabline, toskip == 0 ? " " : "<");
664 wattr_off(tabline, tab_face.background, NULL);
667 TAILQ_FOREACH(tab, &tabshead, tabs) {
675 getyx(tabline, y, x);
676 if (x + sizeof(buf)+2 >= (size_t)COLS)
679 current = tab == current_tab;
681 if (*(title = tab->buffer.title) == '\0')
682 title = hist_cur(tab->hist);
684 if (tab->flags & TAB_URGENT)
685 strlcpy(buf, "!", sizeof(buf));
687 strlcpy(buf, " ", sizeof(buf));
689 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
690 /* truncation happens */
691 strlcpy(&buf[sizeof(buf)-4], "...", 4);
693 /* pad with spaces */
694 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
698 pair = current ? tab_face.current : tab_face.tab;
699 wattr_on(tabline, pair, NULL);
700 wprintw(tabline, "%s", buf);
701 wattr_off(tabline, pair, NULL);
703 wattr_on(tabline, tab_face.background, NULL);
704 if (TAILQ_NEXT(tab, tabs) != NULL)
705 wprintw(tabline, "┃");
706 wattr_off(tabline, tab_face.background, NULL);
709 wattr_on(tabline, tab_face.background, NULL);
710 for (; x < (size_t)COLS; ++x)
711 waddch(tabline, ' ');
713 mvwprintw(tabline, 0, COLS-1, ">");
714 wattr_off(tabline, tab_face.background, NULL);
718 * Compute the first visible line around vl. Try to search forward
719 * until the end of the buffer; if a visible line is not found, search
720 * backward. Return NULL if no viable line was found.
723 adjust_line(struct vline *vl, struct buffer *buffer)
730 if (!(vl->parent->flags & L_HIDDEN))
735 t != NULL && t->parent->flags & L_HIDDEN;
736 t = TAILQ_NEXT(t, vlines))
742 /* search backward */
744 t != NULL && t->parent->flags & L_HIDDEN;
745 t = TAILQ_PREV(t, vhead, vlines))
752 redraw_window(WINDOW *win, int off, int height, int width,
753 int show_fringe, struct buffer *buffer)
756 int onscreen = 0, l = 0;
758 restore_curs_x(buffer);
761 * TODO: ignoring buffer->force_update and always
762 * re-rendering. In theory we can recompute the y position
763 * without a re-render, and optimize here. It's not the only
764 * optimisation possible here, wscrl wolud also be an
772 if (TAILQ_EMPTY(&buffer->head))
775 if (buffer->top_line == NULL)
776 buffer->top_line = TAILQ_FIRST(&buffer->vhead);
778 buffer->top_line = adjust_line(buffer->top_line, buffer);
779 if (buffer->top_line == NULL)
782 buffer->current_line = adjust_line(buffer->current_line, buffer);
784 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
785 if (vl->parent->flags & L_HIDDEN)
789 print_vline(off, width, win, vl);
791 if (vl == buffer->current_line)
803 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
804 if (vl == buffer->current_line)
806 if (vl->parent->flags & L_HIDDEN)
809 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
816 buffer->last_line_off = buffer->line_off;
817 buffer->force_redraw = 0;
819 for (; show_fringe && l < height; l++)
820 print_vline(off, width, win, &fringe);
822 wmove(win, buffer->curs_y, buffer->curs_x);
826 redraw_download(void)
828 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
834 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
838 redraw_body(struct tab *tab)
840 static struct tab *last_tab;
843 tab->buffer.force_redraw =1;
846 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
850 trust_status_char(enum trust_state ts)
853 case TS_UNKNOWN: return '-';
854 case TS_UNTRUSTED: return '!';
855 case TS_TEMP_TRUSTED: return '!';
856 case TS_TRUSTED: return 'v';
857 case TS_VERIFIED: return 'V';
863 redraw_modeline(struct tab *tab)
865 struct buffer *buffer;
867 int x, y, max_x, max_y;
869 const char *spin = "-\\|/";
871 buffer = current_buffer();
875 wattr_on(modeline, modeline_face.background, NULL);
876 wmove(modeline, 0, 0);
878 wprintw(modeline, "-%c%c%c%c %s ",
879 spin[tab->loading_anim_step],
880 trust_status_char(tab->trust),
881 tab->client_cert ? 'C' : '-',
882 tab->faulty_gemserver ? 'W' : '-',
883 mode == NULL ? "(none)" : mode);
885 pct = (buffer->line_off + buffer->curs_y) * 100.0
888 if (buffer->line_max <= (size_t)body_lines)
889 wprintw(modeline, "All ");
890 else if (buffer->line_off == 0)
891 wprintw(modeline, "Top ");
892 else if (buffer->line_off + body_lines >= buffer->line_max)
893 wprintw(modeline, "Bottom ");
895 wprintw(modeline, "%.0f%% ", pct);
897 wprintw(modeline, "%zu/%zu %s ",
898 buffer->line_off + buffer->curs_y,
900 hist_cur(tab->hist));
902 getyx(modeline, y, x);
903 getmaxyx(modeline, max_y, max_x);
908 for (; x < max_x; ++x)
909 waddstr(modeline, "-");
911 wattr_off(modeline, modeline_face.background, NULL);
915 redraw_minibuffer(void)
917 wattr_on(echoarea, minibuffer_face.background, NULL);
921 do_redraw_minibuffer();
923 do_redraw_echoarea();
925 if (in_minibuffer == MB_COMPREAD)
926 do_redraw_minibuffer_compl();
928 wattr_off(echoarea, minibuffer_face.background, NULL);
932 do_redraw_echoarea(void)
936 if (ministate.curmesg != NULL)
937 wprintw(echoarea, "%s", ministate.curmesg);
938 else if (*keybuf != '\0')
939 waddstr(echoarea, keybuf);
941 /* If nothing else, show the URL at point */
942 vl = current_tab->buffer.current_line;
943 if (vl != NULL && vl->parent->type == LINE_LINK)
944 waddstr(echoarea, vl->parent->alt);
949 do_redraw_minibuffer(void)
951 struct buffer *cmplbuf, *buffer;
952 size_t off_y, off_x = 0;
953 const char *start, *c;
956 cmplbuf = &ministate.compl.buffer;
957 buffer = &ministate.buffer;
958 (void)off_y; /* unused, set by getyx */
960 wmove(echoarea, 0, 0);
962 if (in_minibuffer == MB_COMPREAD)
963 wprintw(echoarea, "(%2zu) ",
966 wprintw(echoarea, "%s", ministate.prompt);
967 if (!ministate.editing)
968 wprintw(echoarea, "(%zu/%zu) ",
969 hist_off(ministate.hist) + 1,
970 hist_size(ministate.hist));
972 getyx(echoarea, off_y, off_x);
974 start = ministate.buf;
975 if (!ministate.editing)
976 start = hist_cur(ministate.hist);
977 line = buffer->current_line->parent->line + buffer->current_line->from;
978 c = line + buffer->point_offset;
979 while (start < c && utf8_swidth_between(start, c, off_x) > (size_t)COLS/2) {
980 start += grapheme_next_character_break_utf8(start, SIZE_MAX);
983 waddstr(echoarea, start);
985 if (ministate.curmesg != NULL)
986 wprintw(echoarea, " [%s]", ministate.curmesg);
988 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c, off_x));
992 do_redraw_minibuffer_compl(void)
994 redraw_window(minibuffer, 0, 10, COLS, 1,
995 &ministate.compl.buffer);
999 * Place the cursor in the right ncurses window. If soft is 1, use
1000 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
1004 place_cursor(int soft)
1006 int (*touch)(WINDOW *);
1009 touch = wnoutrefresh;
1013 if (in_minibuffer) {
1014 if (side_window & SIDE_WINDOW_LEFT)
1016 if (side_window & SIDE_WINDOW_BOTTOM)
1020 } else if (in_side_window & SIDE_WINDOW_LEFT) {
1023 if (in_side_window & SIDE_WINDOW_BOTTOM)
1026 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
1029 if (in_side_window & SIDE_WINDOW_LEFT)
1033 if (side_window & SIDE_WINDOW_LEFT)
1035 if (side_window & SIDE_WINDOW_BOTTOM)
1043 redraw_tab(struct tab *tab)
1048 if (side_window & SIDE_WINDOW_LEFT) {
1053 if (side_window & SIDE_WINDOW_BOTTOM) {
1055 wnoutrefresh(download);
1062 redraw_modeline(tab);
1063 redraw_minibuffer();
1065 wnoutrefresh(tabline);
1066 wnoutrefresh(modeline);
1068 if (in_minibuffer == MB_COMPREAD)
1069 wnoutrefresh(minibuffer);
1076 dprintf(1, "\033]2;%s - Telescope\a",
1077 current_tab->buffer.title);
1081 start_loading_anim(struct tab *tab)
1083 if (tab->loading_anim)
1085 tab->loading_anim = 1;
1087 ev_timer_cancel(tab->loading_timer);
1088 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1092 update_loading_anim(int fd, int ev, void *d)
1094 struct tab *tab = d;
1096 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1098 if (tab == current_tab) {
1099 redraw_modeline(tab);
1106 tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1110 stop_loading_anim(struct tab *tab)
1112 if (!tab->loading_anim)
1115 ev_timer_cancel(tab->loading_timer);
1116 tab->loading_anim = 0;
1117 tab->loading_anim_step = 0;
1119 if (tab != current_tab)
1122 redraw_modeline(tab);
1133 setlocale(LC_ALL, "");
1135 if (TAILQ_EMPTY(&global_map.m)) {
1136 fprintf(stderr, "no keys defined!\n");
1142 /* initialize download window */
1143 TAILQ_INIT(&downloadwin.head);
1144 TAILQ_INIT(&downloadwin.vhead);
1146 /* initialize help window */
1147 TAILQ_INIT(&helpwin.head);
1148 TAILQ_INIT(&helpwin.vhead);
1150 base_map = &global_map;
1151 current_map = &global_map;
1155 if (enable_colors) {
1158 use_default_colors();
1163 config_apply_style();
1168 intrflush(stdscr, FALSE);
1170 if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1172 if ((body = newwin(1, 1, 0, 0)) == NULL)
1174 if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1176 if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1178 if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1180 if ((download = newwin(1, 1, 0, 0)) == NULL)
1182 if ((help = newwin(1, 1, 0, 0)) == NULL)
1185 wbkgd(body, body_face.body);
1186 wbkgd(download, download_face.background);
1187 wbkgd(echoarea, minibuffer_face.background);
1192 scrollok(body, FALSE);
1194 /* non-blocking input */
1206 if (ev_signal(SIGWINCH, handle_signal, NULL) == -1 ||
1207 ev_signal(SIGCHLD, handle_signal, NULL) == -1 ||
1208 ev_add(0, EV_READ, dispatch_stdio, NULL) == -1)
1209 err(1, "ev_signal or ev_add failed");
1211 switch_to_tab(current_tab);
1212 rearrange_windows();
1218 ui_on_tab_loaded(struct tab *tab)
1220 size_t line_off, curr_off;
1222 stop_loading_anim(tab);
1223 message("Loaded %s", hist_cur(tab->hist));
1225 hist_cur_offs(tab->hist, &line_off, &curr_off);
1226 if (curr_off != 0 &&
1227 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.vhead)) {
1228 set_scroll_position(tab, line_off, curr_off);
1241 ui_on_tab_refresh(struct tab *tab)
1243 wrap_page(&tab->buffer, body_cols, x_offset);
1244 if (tab == current_tab)
1247 tab->flags |= TAB_URGENT;
1251 ui_force_tab_refresh(struct tab *tab)
1254 rearrange_windows();
1259 ui_on_download_refresh(void)
1261 if (ev_timer_pending(download_timer))
1264 download_timer = ev_timer(&download_refresh_timer,
1265 handle_download_refresh, NULL);
1269 open_download(int res, void *data)
1271 struct download *d = data;
1272 struct mailcap *mc = NULL;
1273 enum exec_mode mode = EXEC_BACKGROUND;
1278 if ((mc = mailcap_cmd_from_mimetype(d->mime_type, d->path)) == NULL)
1281 if (mc->flags & MAILCAP_NEEDSTERMINAL)
1282 mode = EXEC_FOREGROUND;
1284 message("Loaded %s with %s", d->mime_type, mc->cmd_argv[0]);
1285 exec_cmd(mc->cmd_argv, mode);
1289 ui_prompt_download_cmd(struct download *d)
1293 snprintf(prompt, sizeof(prompt), "Open %s?", d->path);
1294 ui_yornp(prompt, open_download, d);
1298 ui_remotely_open_link(const char *uri)
1300 new_tab(uri, NULL, NULL);
1301 ui_on_tab_refresh(current_tab);
1315 ui_toggle_side_window(int kind)
1317 if (in_side_window & kind)
1320 side_window ^= kind;
1321 if (side_window & SIDE_WINDOW_LEFT)
1323 if (side_window & SIDE_WINDOW_BOTTOM)
1324 recompute_downloads();
1327 * ugly hack, but otherwise the window doesn't get updated
1328 * until I call rearrange_windows a second time (e.g. via
1329 * C-l). I will be happy to know why something like this is
1332 rearrange_windows();
1333 rearrange_windows();
1337 ui_show_downloads_pane(void)
1339 if (!(side_window & SIDE_WINDOW_BOTTOM))
1340 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1344 ui_schedule_redraw(void)
1346 should_rearrange_windows = 1;
1350 ui_require_input(struct tab *tab, int hide, void (*fn)(const char *))
1352 struct minibuffer m = {
1353 .self_insert = sensible_self_insert,
1355 .history = ir_history,
1358 /* TODO: hard-switching to another tab is ugly */
1361 enter_minibuffer(&m, "Input required: ");
1366 ui_after_message_hook(void)
1368 redraw_minibuffer();
1373 ui_yornp(const char *prompt, void (*fn)(int, void *), void *data)
1375 yornp(prompt, fn, data);
1376 redraw_tab(current_tab);
1380 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1381 struct tab *data, const char *input)
1383 minibuffer_read(prompt, fn, data, input);
1384 redraw_tab(current_tab);
1388 ui_other_window(void)
1390 if (in_side_window & SIDE_WINDOW_LEFT &&
1391 side_window & SIDE_WINDOW_BOTTOM)
1392 in_side_window = SIDE_WINDOW_BOTTOM;
1393 else if (in_side_window)
1395 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1396 in_side_window = SIDE_WINDOW_LEFT;
1397 else if (!in_side_window && side_window)
1398 in_side_window = SIDE_WINDOW_BOTTOM;
1400 message("No other window to select");
1414 rearrange_windows();