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 handle_download_refresh(int, short, void *);
62 static void rearrange_windows(void);
63 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
64 static void print_vline(int, int, WINDOW*, struct vline*);
65 static void redraw_tabline(void);
66 static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
67 static void redraw_download(void);
68 static void redraw_help(void);
69 static void redraw_body(struct tab*);
70 static void redraw_modeline(struct tab*);
71 static void redraw_minibuffer(void);
72 static void do_redraw_echoarea(void);
73 static void do_redraw_minibuffer(void);
74 static void do_redraw_minibuffer_compl(void);
75 static void place_cursor(int);
76 static void redraw_tab(struct tab*);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
80 static int should_rearrange_windows;
81 static int show_tab_bar;
85 struct thiskey thiskey;
86 struct tab *current_tab;
88 static struct event resizeev;
89 static struct timeval resize_timer = { 0, 250000 };
91 static struct event download_refreshev;
92 static struct timeval download_refresh_timer = { 0, 250000 };
94 static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
96 int body_lines, body_cols;
99 /* not static so we can see them from help.c */
100 struct buffer helpwin;
101 int help_lines, help_cols;
103 static WINDOW *download;
104 /* not static so we can see them from download.c */
105 struct buffer downloadwin;
109 static int side_window;
110 static int in_side_window;
112 static struct timeval loadingev_timer = { 0, 250000 };
114 static char keybuf[64];
116 /* XXX: don't forget to init these in main() */
117 struct kmap global_map,
123 update_x_offset(void)
125 if (olivetti_mode && fill_column < body_cols)
126 x_offset = (body_cols - fill_column)/2;
132 set_scroll_position(struct tab *tab, size_t top, size_t cur)
139 last = TAILQ_FIRST(&tab->buffer.page.head);
140 TAILQ_FOREACH(vl, &tab->buffer.head, vlines) {
141 if (last != vl->parent) {
146 if (!topfound && i == top) {
148 tab->buffer.top_line = vl;
152 tab->buffer.current_line = vl;
158 tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.head);
160 tab->buffer.current_line = tab->buffer.top_line;
164 get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
172 if (tab->buffer.top_line == NULL ||
173 tab->buffer.current_line == NULL)
176 TAILQ_FOREACH(l, &tab->buffer.page.head, lines) {
177 if (tab->buffer.top_line->parent == l)
179 if (tab->buffer.current_line->parent == l)
189 save_excursion(struct excursion *place, struct buffer *buffer)
191 place->curs_x = buffer->curs_x;
192 place->curs_y = buffer->curs_y;
193 place->line_off = buffer->line_off;
194 place->top_line = buffer->top_line;
195 place->current_line = buffer->current_line;
196 place->cpoff = buffer->cpoff;
200 restore_excursion(struct excursion *place, struct buffer *buffer)
202 buffer->curs_x = place->curs_x;
203 buffer->curs_y = place->curs_y;
204 buffer->line_off = place->line_off;
205 buffer->top_line = place->top_line;
206 buffer->current_line = place->current_line;
207 buffer->cpoff = place->cpoff;
211 restore_curs_x(struct buffer *buffer)
214 const char *prfx, *text;
216 vl = buffer->current_line;
217 if (vl == NULL || vl->line == NULL)
218 buffer->curs_x = buffer->cpoff = 0;
219 else if (vl->parent->data != NULL) {
220 text = vl->parent->data;
221 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
223 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
225 /* small hack: don't olivetti-mode the download pane */
226 if (buffer != &downloadwin)
227 buffer->curs_x += x_offset;
232 if (vl->parent->data != NULL)
233 buffer->curs_x += utf8_swidth_between(vl->parent->line,
236 prfx = line_prefixes[vl->parent->type].prfx1;
237 buffer->curs_x += utf8_swidth(prfx);
242 global_key_unbound(void)
244 message("%s is undefined", keybuf);
251 return &ministate.buffer;
252 if (in_side_window & SIDE_WINDOW_LEFT)
254 if (in_side_window & SIDE_WINDOW_BOTTOM)
256 return ¤t_tab->buffer;
264 if ((thiskey.key = wgetch(body)) == ERR)
267 thiskey.meta = thiskey.key == '\e';
269 thiskey.key = wgetch(body);
270 if (thiskey.key == ERR || thiskey.key == '\e') {
278 if ((unsigned int)thiskey.key >= UINT8_MAX)
282 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
284 if ((thiskey.key = wgetch(body)) == ERR) {
285 message("Error decoding user input");
294 dispatch_stdio(int fd, short ev, void *d)
300 /* TODO: schedule a redraw? */
307 if (keybuf[0] != '\0')
308 strlcat(keybuf, " ", sizeof(keybuf));
310 strlcat(keybuf, "M-", sizeof(keybuf));
311 if (thiskey.cp != 0) {
312 utf8_encode(thiskey.cp, tmp);
313 strlcat(keybuf, tmp, sizeof(keybuf));
314 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
315 strlcat(keybuf, keyname, sizeof(keybuf));
317 tmp[0] = thiskey.key;
318 strlcat(keybuf, tmp, sizeof(keybuf));
321 lk = lookup_key(¤t_map, &thiskey, current_buffer());
322 if (lk == LK_UNBOUND) {
323 if (current_map->unhandled_input != NULL)
324 current_map->unhandled_input();
326 global_key_unbound();
328 if (lk != LK_ADVANCED_MAP) {
329 current_map = base_map;
330 strlcpy(keybuf, "", sizeof(keybuf));
333 if (side_window & SIDE_WINDOW_LEFT)
336 if (should_rearrange_windows)
338 redraw_tab(current_tab);
342 handle_resize(int sig, short ev, void *d)
344 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
345 event_del(&resizeev);
347 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
348 evtimer_add(&resizeev, &resize_timer);
352 handle_resize_nodelay(int s, short ev, void *d)
362 handle_download_refresh(int s, short v, void *d)
364 if (side_window & SIDE_WINDOW_BOTTOM) {
365 recompute_downloads();
366 redraw_tab(current_tab);
371 should_show_tab_bar(void)
373 if (tab_bar_show == -1)
375 if (tab_bar_show == 0)
378 return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
382 rearrange_windows(void)
385 int minibuffer_lines;
387 should_rearrange_windows = 0;
388 show_tab_bar = should_show_tab_bar();
392 /* 3 lines for the ui and 12 for the */
393 if ((too_small = lines < 15)) {
395 printw("Window too small.");
400 /* move and resize the windows, in reverse order! */
402 if (in_minibuffer == MB_COMPREAD) {
403 minibuffer_lines = MIN(10, lines/2);
404 mvwin(minibuffer, lines - minibuffer_lines, 0);
405 wresize(minibuffer, minibuffer_lines, COLS);
406 lines -= minibuffer_lines;
408 wrap_page(&ministate.compl.buffer, COLS);
411 mvwin(echoarea, --lines, 0);
412 wresize(echoarea, 1, COLS);
414 mvwin(modeline, --lines, 0);
415 wresize(modeline, 1, COLS);
417 if (side_window & SIDE_WINDOW_BOTTOM) {
418 download_lines = MIN(5, lines/2);
419 download_cols = COLS;
420 mvwin(download, lines - download_lines, 0);
421 wresize(download, download_lines, download_cols);
422 lines -= download_lines;
424 wrap_page(&downloadwin, download_cols);
427 body_lines = show_tab_bar ? --lines : lines;
431 * Here we make the assumption that show_tab_bar is either 0
432 * or 1, and reuse that as argument to mvwin.
434 if (side_window & SIDE_WINDOW_LEFT) {
435 help_cols = 0.3 * COLS;
437 mvwin(help, show_tab_bar, 0);
438 wresize(help, help_lines, help_cols);
440 wrap_page(&helpwin, help_cols);
442 body_cols = COLS - help_cols - 1;
443 mvwin(body, show_tab_bar, help_cols);
445 mvwin(body, show_tab_bar, 0);
448 wresize(body, body_lines, body_cols);
451 wresize(tabline, 1, COLS);
453 wrap_page(¤t_tab->buffer, body_cols);
454 redraw_tab(current_tab);
458 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
459 const char **prfx_ret, const char **text_ret)
461 int type, i, cont, width;
464 if ((*text_ret = vl->line) == NULL)
467 cont = vl->flags & L_CONTINUATION;
468 type = vl->parent->type;
470 *prfx_ret = line_prefixes[type].prfx1;
472 *prfx_ret = line_prefixes[type].prfx2;
474 space = vl->parent->data;
475 if (!emojify_link || type != LINE_LINK || space == NULL)
480 width = utf8_swidth_between(vl->parent->line, space);
481 for (i = 0; i < width + 1; ++i)
482 strlcat(buf, " ", len);
484 strlcpy(buf, *text_ret, len);
485 if ((t = strchr(buf, ' ')) != NULL)
487 strlcat(buf, " ", len);
490 *text_ret += (space - vl->parent->line) + 1;
497 print_vline_descr(int width, WINDOW *window, struct vline *vl)
501 switch (vl->parent->type) {
503 case LINE_COMPL_CURRENT:
507 case LINE_DOWNLOAD_DONE:
517 if (vl->parent->alt == NULL)
524 wprintw(window, " ");
525 for (; goal > x; ++x)
526 wprintw(window, " ");
528 wprintw(window, "%s", vl->parent->alt);
532 * Core part of the rendering. It prints a vline starting from the
533 * current cursor position. Printing a vline consists of skipping
534 * `off' columns (for olivetti-mode), print the correct prefix (which
535 * may be the emoji in case of emojified links-lines), printing the
536 * text itself, filling until width - off and filling off columns
540 print_vline(int off, int width, WINDOW *window, struct vline *vl)
543 * Believe me or not, I've seen emoji ten code points long!
544 * That means, to stay large, 4*10 bytes + NUL.
546 char emojibuf[41] = {0};
547 const char *text, *prfx;
551 f = &line_faces[vl->parent->type];
553 /* unused, set by getyx */
556 if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
559 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
561 wattr_on(window, body_face.left, NULL);
562 for (i = 0; i < off; i++)
564 wattr_off(window, body_face.left, NULL);
566 wattr_on(window, f->prefix, NULL);
567 wprintw(window, "%s", prfx);
568 wattr_off(window, f->prefix, NULL);
570 wattr_on(window, f->text, NULL);
571 wprintw(window, "%s", text);
572 print_vline_descr(width, window, vl);
573 wattr_off(window, f->text, NULL);
579 wattr_on(window, f->trail, NULL);
580 for (i = 0; i < left - off; ++i)
582 wattr_off(window, f->trail, NULL);
584 wattr_on(window, body_face.right, NULL);
585 for (i = 0; i < off; i++)
587 wattr_off(window, body_face.right, NULL);
595 size_t toskip, ots, tabwidth, space, x;
596 int current, y, truncated, pair;
602 /* unused, but setted by a getyx */
605 tabwidth = sizeof(buf)+1;
609 TAILQ_FOREACH(tab, &tabshead, tabs) {
611 if (tab == current_tab)
615 if (toskip * tabwidth <= space)
620 while (toskip != 0 &&
621 (ots - toskip+1) * tabwidth < space)
626 wattr_on(tabline, tab_face.background, NULL);
627 wprintw(tabline, toskip == 0 ? " " : "<");
628 wattr_off(tabline, tab_face.background, NULL);
631 TAILQ_FOREACH(tab, &tabshead, tabs) {
639 getyx(tabline, y, x);
640 if (x + sizeof(buf)+2 >= (size_t)COLS)
643 current = tab == current_tab;
645 if (*(title = tab->buffer.page.title) == '\0')
646 title = tab->hist_cur->h;
648 if (tab->flags & TAB_URGENT)
649 strlcpy(buf, "!", sizeof(buf));
651 strlcpy(buf, " ", sizeof(buf));
653 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
654 /* truncation happens */
655 strlcpy(&buf[sizeof(buf)-4], "...", 4);
657 /* pad with spaces */
658 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
662 pair = current ? tab_face.current : tab_face.tab;
663 wattr_on(tabline, pair, NULL);
664 wprintw(tabline, "%s", buf);
665 wattr_off(tabline, pair, NULL);
667 wattr_on(tabline, tab_face.background, NULL);
668 if (TAILQ_NEXT(tab, tabs) != NULL)
669 wprintw(tabline, "┃");
670 wattr_off(tabline, tab_face.background, NULL);
673 wattr_on(tabline, tab_face.background, NULL);
674 for (; x < (size_t)COLS; ++x)
675 waddch(tabline, ' ');
677 mvwprintw(tabline, 0, COLS-1, ">");
678 wattr_off(tabline, tab_face.background, NULL);
682 * Compute the first visible line around vl. Try to search forward
683 * until the end of the buffer; if a visible line is not found, search
684 * backward. Return NULL if no viable line was found.
687 adjust_line(struct vline *vl, struct buffer *buffer)
694 if (!(vl->parent->flags & L_HIDDEN))
699 t != NULL && t->parent->flags & L_HIDDEN;
700 t = TAILQ_NEXT(t, vlines))
706 /* search backward */
708 t != NULL && t->parent->flags & L_HIDDEN;
709 t = TAILQ_PREV(t, vhead, vlines))
716 redraw_window(WINDOW *win, int off, int height, int width,
717 int show_fringe, struct buffer *buffer)
720 int onscreen = 0, l = 0;
722 restore_curs_x(buffer);
725 * TODO: ignoring buffer->force_update and always
726 * re-rendering. In theory we can recompute the y position
727 * without a re-render, and optimize here. It's not the only
728 * optimisation possible here, wscrl wolud also be an
736 if (TAILQ_EMPTY(&buffer->head))
739 if (buffer->top_line == NULL)
740 buffer->top_line = TAILQ_FIRST(&buffer->head);
742 buffer->top_line = adjust_line(buffer->top_line, buffer);
743 if (buffer->top_line == NULL)
746 buffer->current_line = adjust_line(buffer->current_line, buffer);
748 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
749 if (vl->parent->flags & L_HIDDEN)
753 print_vline(off, width, win, vl);
755 if (vl == buffer->current_line)
767 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
768 if (vl == buffer->current_line)
770 if (vl->parent->flags & L_HIDDEN)
773 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
780 buffer->last_line_off = buffer->line_off;
781 buffer->force_redraw = 0;
783 for (; show_fringe && l < height; l++)
784 print_vline(off, width, win, &fringe);
786 wmove(win, buffer->curs_y, buffer->curs_x);
790 redraw_download(void)
792 redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
798 redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
802 redraw_body(struct tab *tab)
804 static struct tab *last_tab;
807 tab->buffer.force_redraw =1;
810 redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
814 trust_status_char(enum trust_state ts)
817 case TS_UNKNOWN: return '-';
818 case TS_UNTRUSTED: return '!';
819 case TS_TEMP_TRUSTED: return '!';
820 case TS_TRUSTED: return 'v';
821 case TS_VERIFIED: return 'V';
827 redraw_modeline(struct tab *tab)
829 struct buffer *buffer;
831 int x, y, max_x, max_y;
833 const char *spin = "-\\|/";
835 buffer = current_buffer();
836 mode = buffer->page.name;
839 wattr_on(modeline, modeline_face.background, NULL);
840 wmove(modeline, 0, 0);
842 wprintw(modeline, "-%c%c- %s ",
843 spin[tab->loading_anim_step],
844 trust_status_char(tab->trust),
845 mode == NULL ? "(none)" : mode);
847 pct = (buffer->line_off + buffer->curs_y) * 100.0
850 if (buffer->line_max <= (size_t)body_lines)
851 wprintw(modeline, "All ");
852 else if (buffer->line_off == 0)
853 wprintw(modeline, "Top ");
854 else if (buffer->line_off + body_lines >= buffer->line_max)
855 wprintw(modeline, "Bottom ");
857 wprintw(modeline, "%.0f%% ", pct);
859 wprintw(modeline, "%zu/%zu %s ",
860 buffer->line_off + buffer->curs_y,
864 getyx(modeline, y, x);
865 getmaxyx(modeline, max_y, max_x);
870 for (; x < max_x; ++x)
871 waddstr(modeline, "-");
873 wattr_off(modeline, modeline_face.background, NULL);
877 redraw_minibuffer(void)
879 wattr_on(echoarea, minibuffer_face.background, NULL);
883 do_redraw_minibuffer();
885 do_redraw_echoarea();
887 if (in_minibuffer == MB_COMPREAD)
888 do_redraw_minibuffer_compl();
890 wattr_off(echoarea, minibuffer_face.background, NULL);
894 do_redraw_echoarea(void)
898 if (ministate.curmesg != NULL)
899 wprintw(echoarea, "%s", ministate.curmesg);
900 else if (*keybuf != '\0')
901 waddstr(echoarea, keybuf);
903 /* If nothing else, show the URL at point */
904 vl = current_tab->buffer.current_line;
905 if (vl != NULL && vl->parent->type == LINE_LINK)
906 waddstr(echoarea, vl->parent->alt);
911 do_redraw_minibuffer(void)
913 struct buffer *cmplbuf, *buffer;
914 size_t off_y, off_x = 0;
915 const char *start, *c;
917 cmplbuf = &ministate.compl.buffer;
918 buffer = &ministate.buffer;
919 (void)off_y; /* unused, set by getyx */
921 wmove(echoarea, 0, 0);
923 if (in_minibuffer == MB_COMPREAD)
924 wprintw(echoarea, "(%2zu) ",
927 wprintw(echoarea, "%s", ministate.prompt);
928 if (ministate.hist_cur != NULL)
929 wprintw(echoarea, "(%zu/%zu) ",
930 ministate.hist_off + 1,
931 ministate.history->len);
933 getyx(echoarea, off_y, off_x);
935 start = ministate.hist_cur != NULL
936 ? ministate.hist_cur->h
938 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
939 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
940 start = utf8_next_cp(start);
943 waddstr(echoarea, start);
945 if (ministate.curmesg != NULL)
946 wprintw(echoarea, " [%s]", ministate.curmesg);
948 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
952 do_redraw_minibuffer_compl(void)
954 redraw_window(minibuffer, 0, 10, COLS, 1,
955 &ministate.compl.buffer);
959 * Place the cursor in the right ncurses window. If soft is 1, use
960 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
964 place_cursor(int soft)
966 int (*touch)(WINDOW *);
969 touch = wnoutrefresh;
974 if (side_window & SIDE_WINDOW_LEFT)
976 if (side_window & SIDE_WINDOW_BOTTOM)
980 } else if (in_side_window & SIDE_WINDOW_LEFT) {
983 if (in_side_window & SIDE_WINDOW_BOTTOM)
986 } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
989 if (in_side_window & SIDE_WINDOW_LEFT)
993 if (side_window & SIDE_WINDOW_LEFT)
995 if (side_window & SIDE_WINDOW_BOTTOM)
1003 redraw_tab(struct tab *tab)
1008 if (side_window & SIDE_WINDOW_LEFT) {
1013 if (side_window & SIDE_WINDOW_BOTTOM) {
1015 wnoutrefresh(download);
1022 redraw_modeline(tab);
1023 redraw_minibuffer();
1025 wnoutrefresh(tabline);
1026 wnoutrefresh(modeline);
1028 if (in_minibuffer == MB_COMPREAD)
1029 wnoutrefresh(minibuffer);
1036 dprintf(1, "\033]2;%s - Telescope\a",
1037 current_tab->buffer.page.title);
1041 start_loading_anim(struct tab *tab)
1043 if (tab->loading_anim)
1045 tab->loading_anim = 1;
1046 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1047 evtimer_add(&tab->loadingev, &loadingev_timer);
1051 update_loading_anim(int fd, short ev, void *d)
1053 struct tab *tab = d;
1055 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1057 if (tab == current_tab) {
1058 redraw_modeline(tab);
1065 evtimer_add(&tab->loadingev, &loadingev_timer);
1069 stop_loading_anim(struct tab *tab)
1071 if (!tab->loading_anim)
1073 evtimer_del(&tab->loadingev);
1074 tab->loading_anim = 0;
1075 tab->loading_anim_step = 0;
1077 if (tab != current_tab)
1080 redraw_modeline(tab);
1089 ui_print_colors(void)
1091 int colors = 0, pairs = 0, can_change = 0;
1092 int columns = 16, lines, color, i, j;
1097 use_default_colors();
1100 pairs = COLOR_PAIRS;
1101 can_change = can_change_color();
1105 printf("Term info:\n");
1106 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1107 getenv("TERM"), colors, pairs, can_change);
1111 printf("No color support\n");
1115 printf("Available colors:\n\n");
1116 lines = (colors - 1) / columns + 1;
1118 for (i = 0; i < lines; ++i) {
1119 for (j = 0; j < columns; ++j, ++color) {
1120 printf("\033[0;38;5;%dm %03d", color, color);
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.page.head);
1146 /* initialize help window */
1147 TAILQ_INIT(&helpwin.head);
1148 TAILQ_INIT(&helpwin.page.head);
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 */
1198 mvwprintw(body, 0, 0, "");
1206 evtimer_set(&resizeev, handle_resize, NULL);
1207 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1209 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1210 event_add(&stdioev, NULL);
1212 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1213 signal_add(&winchev, NULL);
1215 switch_to_tab(current_tab);
1216 rearrange_windows();
1222 ui_on_tab_loaded(struct tab *tab)
1224 stop_loading_anim(tab);
1225 message("Loaded %s", tab->hist_cur->h);
1227 if (tab->hist_cur->current_off != 0 &&
1228 tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.head)) {
1229 set_scroll_position(tab, tab->hist_cur->line_off,
1230 tab->hist_cur->current_off);
1243 ui_on_tab_refresh(struct tab *tab)
1245 wrap_page(&tab->buffer, body_cols);
1246 if (tab == current_tab)
1249 tab->flags |= TAB_URGENT;
1253 ui_on_download_refresh(void)
1255 if (event_pending(&download_refreshev, EV_TIMEOUT, NULL))
1258 evtimer_set(&download_refreshev, handle_download_refresh, NULL);
1259 evtimer_add(&download_refreshev, &download_refresh_timer);
1263 ui_remotely_open_link(const char *uri)
1265 new_tab(uri, NULL, NULL);
1266 ui_on_tab_refresh(current_tab);
1280 ui_toggle_side_window(int kind)
1282 if (in_side_window & kind)
1285 side_window ^= kind;
1286 if (side_window & SIDE_WINDOW_LEFT)
1288 if (side_window & SIDE_WINDOW_BOTTOM)
1289 recompute_downloads();
1292 * ugly hack, but otherwise the window doesn't get updated
1293 * until I call rearrange_windows a second time (e.g. via
1294 * C-l). I will be happy to know why something like this is
1297 rearrange_windows();
1298 rearrange_windows();
1302 ui_show_downloads_pane(void)
1304 if (!(side_window & SIDE_WINDOW_BOTTOM))
1305 ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1309 ui_schedule_redraw(void)
1311 should_rearrange_windows = 1;
1315 ui_require_input(struct tab *tab, int hide, void (*fn)(void))
1317 /* TODO: hard-switching to another tab is ugly */
1320 enter_minibuffer(sensible_self_insert, fn, exit_minibuffer,
1321 &ir_history, NULL, NULL, 0);
1322 strlcpy(ministate.prompt, "Input required: ",
1323 sizeof(ministate.prompt));
1328 ui_after_message_hook(void)
1330 redraw_minibuffer();
1335 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1338 yornp(prompt, fn, data);
1339 redraw_tab(current_tab);
1343 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1344 struct tab *data, const char *input)
1346 minibuffer_read(prompt, fn, data);
1348 if (input != NULL) {
1349 strlcpy(ministate.buf, input, sizeof(ministate.buf));
1350 cmd_move_end_of_line(&ministate.buffer);
1353 redraw_tab(current_tab);
1357 ui_other_window(void)
1359 if (in_side_window & SIDE_WINDOW_LEFT &&
1360 side_window & SIDE_WINDOW_BOTTOM)
1361 in_side_window = SIDE_WINDOW_BOTTOM;
1362 else if (in_side_window)
1364 else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1365 in_side_window = SIDE_WINDOW_LEFT;
1366 else if (!in_side_window && side_window)
1367 in_side_window = SIDE_WINDOW_BOTTOM;
1369 message("No other window to select");
1377 kill(getpid(), SIGSTOP);
1381 rearrange_windows();