Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
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.
26 *
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.
30 *
31 */
33 #include <assert.h>
34 #include <curses.h>
35 #include <event.h>
36 #include <locale.h>
37 #include <signal.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "defaults.h"
44 #include "minibuffer.h"
45 #include "telescope.h"
46 #include "ui.h"
47 #include "utf8.h"
49 static struct event stdioev, winchev;
51 static void restore_curs_x(struct buffer *);
53 static int readkey(void);
54 static void dispatch_stdio(int, short, void*);
55 static void handle_clear_echoarea(int, short, void*);
56 static void handle_resize(int, short, void*);
57 static void handle_resize_nodelay(int, short, void*);
58 static void rearrange_windows(void);
59 static void line_prefix_and_text(struct vline *, char *, size_t, const char **, const char **);
60 static void print_vline(int, int, WINDOW*, struct vline*);
61 static void redraw_tabline(void);
62 static void redraw_window(WINDOW*, int, int, int, struct buffer*);
63 static void redraw_help(void);
64 static void redraw_body(struct tab*);
65 static void redraw_modeline(struct tab*);
66 static void redraw_minibuffer(void);
67 static void do_redraw_echoarea(void);
68 static void do_redraw_minibuffer(void);
69 static void do_redraw_minibuffer_compl(void);
70 static void place_cursor(int);
71 static void redraw_tab(struct tab*);
72 static void update_loading_anim(int, short, void*);
73 static void stop_loading_anim(struct tab*);
75 /*
76 * Used to know when we're finished loading.
77 */
78 static int operating;
80 static int should_rearrange_windows;
81 static int too_small;
82 static int x_offset;
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;
94 static WINDOW *help;
95 /* not static so we can see them from help.c */
96 struct buffer helpwin;
97 int help_lines, help_cols;
99 static int side_window;
101 static struct event clechoev;
102 static struct timeval clechoev_timer = { 5, 0 };
103 static struct timeval loadingev_timer = { 0, 250000 };
105 static uint32_t tab_counter;
107 static char keybuf[64];
109 struct kmap global_map,
110 minibuffer_map,
111 *current_map,
112 *base_map;
114 int in_minibuffer;
116 static inline void
117 update_x_offset(void)
119 if (olivetti_mode && fill_column < body_cols)
120 x_offset = (body_cols - fill_column)/2;
121 else
122 x_offset = 0;
125 void
126 save_excursion(struct excursion *place, struct buffer *buffer)
128 place->curs_x = buffer->curs_x;
129 place->curs_y = buffer->curs_y;
130 place->line_off = buffer->line_off;
131 place->top_line = buffer->top_line;
132 place->current_line = buffer->current_line;
133 place->cpoff = buffer->cpoff;
136 void
137 restore_excursion(struct excursion *place, struct buffer *buffer)
139 buffer->curs_x = place->curs_x;
140 buffer->curs_y = place->curs_y;
141 buffer->line_off = place->line_off;
142 buffer->top_line = place->top_line;
143 buffer->current_line = place->current_line;
144 buffer->cpoff = place->cpoff;
147 static void
148 restore_curs_x(struct buffer *buffer)
150 struct vline *vl;
151 const char *prfx, *text;
153 vl = buffer->current_line;
154 if (vl == NULL || vl->line == NULL)
155 buffer->curs_x = buffer->cpoff = 0;
156 else if (vl->parent->data != NULL) {
157 text = vl->parent->data;
158 buffer->curs_x = utf8_snwidth(text + 1, buffer->cpoff) + 1;
159 } else
160 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
162 buffer->curs_x += x_offset;
164 if (vl == NULL)
165 return;
167 if (vl->parent->data != NULL)
168 buffer->curs_x += utf8_swidth_between(vl->parent->line, vl->parent->data);
169 else {
170 prfx = line_prefixes[vl->parent->type].prfx1;
171 buffer->curs_x += utf8_swidth(prfx);
175 void
176 global_key_unbound(void)
178 message("%s is undefined", keybuf);
181 struct buffer *
182 current_buffer(void)
184 if (in_minibuffer)
185 return &ministate.buffer;
186 return &current_tab->buffer;
189 static int
190 readkey(void)
192 uint32_t state = 0;
194 if ((thiskey.key = wgetch(body)) == ERR)
195 return 0;
197 thiskey.meta = thiskey.key == 27;
198 if (thiskey.meta) {
199 thiskey.key = wgetch(body);
200 if (thiskey.key == ERR || thiskey.key == 27) {
201 thiskey.meta = 0;
202 thiskey.key = 27;
206 thiskey.cp = 0;
207 if ((unsigned int)thiskey.key < UINT8_MAX) {
208 while (1) {
209 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
210 break;
211 if ((thiskey.key = wgetch(body)) == ERR) {
212 message("Error decoding user input");
213 return 0;
218 return 1;
221 static void
222 dispatch_stdio(int fd, short ev, void *d)
224 struct keymap *k;
225 const char *keyname;
226 char tmp[5] = {0};
228 /* TODO: schedule a redraw? */
229 if (too_small)
230 return;
232 if (!readkey())
233 return;
235 if (keybuf[0] != '\0')
236 strlcat(keybuf, " ", sizeof(keybuf));
237 if (thiskey.meta)
238 strlcat(keybuf, "M-", sizeof(keybuf));
239 if (thiskey.cp != 0) {
240 utf8_encode(thiskey.cp, tmp);
241 strlcat(keybuf, tmp, sizeof(keybuf));
242 } else if ((keyname = unkbd(thiskey.key)) != NULL) {
243 strlcat(keybuf, keyname, sizeof(keybuf));
244 } else {
245 tmp[0] = thiskey.key;
246 strlcat(keybuf, tmp, sizeof(keybuf));
249 TAILQ_FOREACH(k, &current_map->m, keymaps) {
250 if (k->meta == thiskey.meta &&
251 k->key == thiskey.key) {
252 if (k->fn == NULL)
253 current_map = &k->map;
254 else {
255 current_map = base_map;
256 strlcpy(keybuf, "", sizeof(keybuf));
257 k->fn(current_buffer());
259 goto done;
263 if (current_map->unhandled_input != NULL)
264 current_map->unhandled_input();
265 else
266 global_key_unbound();
268 strlcpy(keybuf, "", sizeof(keybuf));
269 current_map = base_map;
271 done:
272 if (side_window)
273 recompute_help();
275 if (should_rearrange_windows)
276 rearrange_windows();
277 redraw_tab(current_tab);
280 static void
281 handle_clear_echoarea(int fd, short ev, void *d)
283 free(ministate.curmesg);
284 ministate.curmesg = NULL;
286 redraw_minibuffer();
287 place_cursor(0);
290 static void
291 handle_resize(int sig, short ev, void *d)
293 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
294 event_del(&resizeev);
296 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
297 evtimer_add(&resizeev, &resize_timer);
300 static void
301 handle_resize_nodelay(int s, short ev, void *d)
303 endwin();
304 refresh();
305 clear();
307 rearrange_windows();
310 static void
311 rearrange_windows(void)
313 int lines;
315 should_rearrange_windows = 0;
317 lines = LINES;
319 if ((too_small = lines < 15)) {
320 erase();
321 printw("Window too small.");
322 refresh();
323 return;
326 /* move and resize the windows, in reverse order! */
328 if (in_minibuffer == MB_COMPREAD) {
329 mvwin(minibuffer, lines-10, 0);
330 wresize(minibuffer, 10, COLS);
331 lines -= 10;
333 wrap_page(&ministate.compl.buffer, COLS);
336 mvwin(echoarea, --lines, 0);
337 wresize(echoarea, 1, COLS);
339 mvwin(modeline, --lines, 0);
340 wresize(modeline, 1, COLS);
342 body_lines = --lines;
343 body_cols = COLS;
345 if (side_window) {
346 help_cols = 0.3 * COLS;
347 help_lines = lines;
348 mvwin(help, 1, 0);
349 wresize(help, help_lines, help_cols);
351 wrap_page(&helpwin, help_cols);
353 body_cols = COLS - help_cols - 1;
354 mvwin(body, 1, help_cols);
355 } else
356 mvwin(body, 1, 0);
358 update_x_offset();
359 wresize(body, body_lines, body_cols);
361 wresize(tabline, 1, COLS);
363 wrap_page(&current_tab->buffer, body_cols);
364 redraw_tab(current_tab);
367 static void
368 line_prefix_and_text(struct vline *vl, char *buf, size_t len,
369 const char **prfx_ret, const char **text_ret)
371 int type, i, cont, width;
372 char *space, *t;
374 if ((*text_ret = vl->line) == NULL)
375 *text_ret = "";
377 cont = vl->flags & L_CONTINUATION;
378 type = vl->parent->type;
379 if (!cont)
380 *prfx_ret = line_prefixes[type].prfx1;
381 else
382 *prfx_ret = line_prefixes[type].prfx2;
384 space = vl->parent->data;
385 if (!emojify_link || type != LINE_LINK || space == NULL)
386 return;
388 if (cont) {
389 memset(buf, 0, len);
390 width = utf8_swidth_between(vl->parent->line, space);
391 for (i = 0; i < width + 1; ++i)
392 strlcat(buf, " ", len);
393 } else {
394 strlcpy(buf, *text_ret, len);
395 if ((t = strchr(buf, ' ')) != NULL)
396 *t = '\0';
397 strlcat(buf, " ", len);
399 /* skip the emoji */
400 *text_ret += (space - vl->parent->line) + 1;
403 *prfx_ret = buf;
406 static inline void
407 print_vline_descr(int width, WINDOW *window, struct vline *vl)
409 int x, y, goal;
411 if (vl->parent->type != LINE_COMPL &&
412 vl->parent->type != LINE_COMPL_CURRENT)
413 return;
415 if (vl->parent->alt == NULL)
416 return;
418 (void)y;
419 getyx(window, y, x);
421 goal = width/2;
422 if (goal <= x)
423 wprintw(window, " ");
424 for (; goal > x; ++x)
425 wprintw(window, " ");
427 wprintw(window, "%s", vl->parent->alt);
430 /*
431 * Core part of the rendering. It prints a vline starting from the
432 * current cursor position. Printing a vline consists of skipping
433 * `off' columns (for olivetti-mode), print the correct prefix (which
434 * may be the emoji in case of emojified links-lines), printing the
435 * text itself, filling until width - off and filling off columns
436 * again.
437 */
438 static void
439 print_vline(int off, int width, WINDOW *window, struct vline *vl)
441 /*
442 * Believe me or not, I've seen emoji ten code points long!
443 * That means, to stay large, 4*10 bytes + NUL.
444 */
445 char emojibuf[41] = {0};
446 const char *text, *prfx;
447 struct line_face *f;
448 int i, left, x, y;
450 f = &line_faces[vl->parent->type];
452 /* unused, set by getyx */
453 (void)y;
455 line_prefix_and_text(vl, emojibuf, sizeof(emojibuf), &prfx, &text);
457 wattr_on(window, body_face.left, NULL);
458 for (i = 0; i < off; i++)
459 waddch(window, ' ');
460 wattr_off(window, body_face.left, NULL);
462 wattr_on(window, f->prefix, NULL);
463 wprintw(window, "%s", prfx);
464 wattr_off(window, f->prefix, NULL);
466 wattr_on(window, f->text, NULL);
467 wprintw(window, "%s", text);
468 print_vline_descr(width, window, vl);
469 wattr_off(window, f->text, NULL);
471 getyx(window, y, x);
473 left = width - x;
475 wattr_on(window, f->trail, NULL);
476 for (i = 0; i < left - off; ++i)
477 waddch(window, ' ');
478 wattr_off(window, f->trail, NULL);
480 wattr_on(window, body_face.right, NULL);
481 for (i = 0; i < off; i++)
482 waddch(window, ' ');
483 wattr_off(window, body_face.right, NULL);
487 static void
488 redraw_tabline(void)
490 struct tab *tab;
491 size_t toskip, ots, tabwidth, space, x;
492 int current, y, truncated, pair;
493 const char *title;
494 char buf[25];
496 x = 0;
498 /* unused, but setted by a getyx */
499 (void)y;
501 tabwidth = sizeof(buf)+1;
502 space = COLS-2;
504 toskip = 0;
505 TAILQ_FOREACH(tab, &tabshead, tabs) {
506 toskip++;
507 if (tab == current_tab)
508 break;
511 if (toskip * tabwidth < space)
512 toskip = 0;
513 else {
514 ots = toskip;
515 toskip--;
516 while (toskip != 0 &&
517 (ots - toskip+1) * tabwidth < space)
518 toskip--;
521 werase(tabline);
522 wattr_on(tabline, tab_face.background, NULL);
523 wprintw(tabline, toskip == 0 ? " " : "<");
524 wattr_off(tabline, tab_face.background, NULL);
526 truncated = 0;
527 TAILQ_FOREACH(tab, &tabshead, tabs) {
528 if (truncated)
529 break;
530 if (toskip != 0) {
531 toskip--;
532 continue;
535 getyx(tabline, y, x);
536 if (x + sizeof(buf)+2 >= (size_t)COLS)
537 truncated = 1;
539 current = tab == current_tab;
541 if (*(title = tab->buffer.page.title) == '\0')
542 title = tab->hist_cur->h;
544 if (tab->flags & TAB_URGENT)
545 strlcpy(buf, "!", sizeof(buf));
546 else
547 strlcpy(buf, " ", sizeof(buf));
549 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
550 /* truncation happens */
551 strlcpy(&buf[sizeof(buf)-4], "...", 4);
552 } else {
553 /* pad with spaces */
554 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
555 /* nop */ ;
558 pair = current ? tab_face.current : tab_face.tab;
559 wattr_on(tabline, pair, NULL);
560 wprintw(tabline, "%s", buf);
561 wattr_off(tabline, pair, NULL);
563 wattr_on(tabline, tab_face.background, NULL);
564 if (TAILQ_NEXT(tab, tabs) != NULL)
565 wprintw(tabline, "│");
566 wattr_off(tabline, tab_face.background, NULL);
569 wattr_on(tabline, tab_face.background, NULL);
570 for (; x < (size_t)COLS; ++x)
571 waddch(tabline, ' ');
572 if (truncated)
573 mvwprintw(tabline, 0, COLS-1, ">");
574 wattr_off(tabline, tab_face.background, NULL);
577 /*
578 * Compute the first visible line around vl. Try to search forward
579 * until the end of the buffer; if a visible line is not found, search
580 * backward. Return NULL if no viable line was found.
581 */
582 struct vline *
583 adjust_line(struct vline *vl, struct buffer *buffer)
585 struct vline *t;
587 if (vl == NULL)
588 return NULL;
590 if (!(vl->parent->flags & L_HIDDEN))
591 return vl;
593 /* search forward */
594 for (t = vl;
595 t != NULL && t->parent->flags & L_HIDDEN;
596 t = TAILQ_NEXT(t, vlines))
597 ; /* nop */
599 if (t != NULL)
600 return t;
602 /* search backward */
603 for (t = vl;
604 t != NULL && t->parent->flags & L_HIDDEN;
605 t = TAILQ_PREV(t, vhead, vlines))
606 ; /* nop */
608 return t;
611 static void
612 redraw_window(WINDOW *win, int off, int height, int width, struct buffer *buffer)
614 struct vline *vl;
615 int l, onscreen;
617 restore_curs_x(buffer);
619 /*
620 * TODO: ignoring buffer->force_update and always
621 * re-rendering. In theory we can recompute the y position
622 * without a re-render, and optimize here. It's not the only
623 * optimisation possible here, wscrl wolud also be an
624 * interesting one.
625 */
627 again:
628 werase(win);
629 buffer->curs_y = 0;
631 if (TAILQ_EMPTY(&buffer->head))
632 goto end;
634 if (buffer->top_line == NULL)
635 buffer->top_line = TAILQ_FIRST(&buffer->head);
637 buffer->top_line = adjust_line(buffer->top_line, buffer);
638 if (buffer->top_line == NULL)
639 goto end;
641 buffer->current_line = adjust_line(buffer->current_line, buffer);
643 l = 0;
644 onscreen = 0;
645 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
646 if (vl->parent->flags & L_HIDDEN)
647 continue;
649 wmove(win, l, 0);
650 print_vline(off, width, win, vl);
652 if (vl == buffer->current_line)
653 onscreen = 1;
655 if (!onscreen)
656 buffer->curs_y++;
658 l++;
659 if (l == height)
660 break;
663 if (!onscreen) {
664 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
665 if (vl == buffer->current_line)
666 break;
667 if (vl->parent->flags & L_HIDDEN)
668 continue;
669 buffer->line_off++;
670 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
673 if (vl != NULL)
674 goto again;
677 buffer->last_line_off = buffer->line_off;
678 buffer->force_redraw = 0;
679 end:
680 wmove(win, buffer->curs_y, buffer->curs_x);
683 static void
684 redraw_help(void)
686 redraw_window(help, 0, help_lines, help_cols, &helpwin);
689 static void
690 redraw_body(struct tab *tab)
692 static struct tab *last_tab;
694 if (last_tab != tab)
695 tab->buffer.force_redraw =1;
696 last_tab = tab;
698 redraw_window(body, x_offset, body_lines, body_cols, &tab->buffer);
701 static inline char
702 trust_status_char(enum trust_state ts)
704 switch (ts) {
705 case TS_UNKNOWN: return '-';
706 case TS_UNTRUSTED: return '!';
707 case TS_TEMP_TRUSTED: return '!';
708 case TS_TRUSTED: return 'v';
709 case TS_VERIFIED: return 'V';
710 default: return 'X';
714 static void
715 redraw_modeline(struct tab *tab)
717 double pct;
718 int x, y, max_x, max_y;
719 const char *mode = tab->buffer.page.name;
720 const char *spin = "-\\|/";
722 werase(modeline);
723 wattr_on(modeline, modeline_face.background, NULL);
724 wmove(modeline, 0, 0);
726 wprintw(modeline, "-%c%c %s ",
727 spin[tab->loading_anim_step],
728 trust_status_char(tab->trust),
729 mode == NULL ? "(none)" : mode);
731 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0
732 / tab->buffer.line_max;
734 if (tab->buffer.line_max <= (size_t)body_lines)
735 wprintw(modeline, "All ");
736 else if (tab->buffer.line_off == 0)
737 wprintw(modeline, "Top ");
738 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
739 wprintw(modeline, "Bottom ");
740 else
741 wprintw(modeline, "%.0f%% ", pct);
743 wprintw(modeline, "%d/%d %s ",
744 tab->buffer.line_off + tab->buffer.curs_y,
745 tab->buffer.line_max,
746 tab->hist_cur->h);
748 getyx(modeline, y, x);
749 getmaxyx(modeline, max_y, max_x);
751 (void)y;
752 (void)max_y;
754 for (; x < max_x; ++x)
755 waddstr(modeline, "-");
757 wattr_off(modeline, modeline_face.background, NULL);
760 static void
761 redraw_minibuffer(void)
763 wattr_on(echoarea, minibuffer_face.background, NULL);
764 werase(echoarea);
766 if (in_minibuffer)
767 do_redraw_minibuffer();
768 else
769 do_redraw_echoarea();
771 if (in_minibuffer == MB_COMPREAD)
772 do_redraw_minibuffer_compl();
774 wattr_off(echoarea, minibuffer_face.background, NULL);
777 static void
778 do_redraw_echoarea(void)
780 struct vline *vl;
782 if (ministate.curmesg != NULL)
783 wprintw(echoarea, "%s", ministate.curmesg);
784 else if (*keybuf != '\0')
785 waddstr(echoarea, keybuf);
786 else {
787 /* If nothing else, show the URL at point */
788 vl = current_tab->buffer.current_line;
789 if (vl != NULL && vl->parent->type == LINE_LINK)
790 waddstr(echoarea, vl->parent->alt);
794 static void
795 do_redraw_minibuffer(void)
797 size_t off_y, off_x = 0;
798 const char *start, *c;
800 /* unused, set by getyx */
801 (void)off_y;
803 wmove(echoarea, 0, 0);
805 if (in_minibuffer == MB_COMPREAD)
806 wprintw(echoarea, "(%2d) ",
807 ministate.compl.buffer.line_max);
809 wprintw(echoarea, "%s", ministate.prompt);
810 if (ministate.hist_cur != NULL)
811 wprintw(echoarea, "(%zu/%zu) ",
812 ministate.hist_off + 1,
813 ministate.history->len);
815 getyx(echoarea, off_y, off_x);
817 start = ministate.hist_cur != NULL
818 ? ministate.hist_cur->h
819 : ministate.buf;
820 c = utf8_nth(ministate.buffer.current_line->line,
821 ministate.buffer.cpoff);
822 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
823 start = utf8_next_cp(start);
826 waddstr(echoarea, start);
828 if (ministate.curmesg != NULL)
829 wprintw(echoarea, " [%s]", ministate.curmesg);
831 wmove(echoarea, 0, off_x + utf8_swidth_between(start, c));
834 static void
835 do_redraw_minibuffer_compl(void)
837 redraw_window(minibuffer, 0, 10, body_cols,
838 &ministate.compl.buffer);
841 /*
842 * Place the cursor in the right ncurses window. If soft is 1, use
843 * wnoutrefresh (which shouldn't cause any I/O); otherwise use
844 * wrefresh.
845 */
846 static void
847 place_cursor(int soft)
849 int (*touch)(WINDOW *);
851 if (soft)
852 touch = wnoutrefresh;
853 else
854 touch = wrefresh;
856 if (in_minibuffer) {
857 touch(body);
858 touch(echoarea);
859 } else {
860 touch(echoarea);
861 touch(body);
865 static void
866 redraw_tab(struct tab *tab)
868 if (too_small)
869 return;
871 if (side_window) {
872 redraw_help();
873 wnoutrefresh(help);
876 redraw_tabline();
877 redraw_body(tab);
878 redraw_modeline(tab);
879 redraw_minibuffer();
881 wnoutrefresh(tabline);
882 wnoutrefresh(modeline);
884 if (in_minibuffer == MB_COMPREAD)
885 wnoutrefresh(minibuffer);
887 place_cursor(1);
889 doupdate();
891 if (set_title)
892 dprintf(1, "\033]0;%s - Telescope\a",
893 current_tab->buffer.page.title);
896 void
897 vmessage(const char *fmt, va_list ap)
899 if (evtimer_pending(&clechoev, NULL))
900 evtimer_del(&clechoev);
902 free(ministate.curmesg);
903 ministate.curmesg = NULL;
905 if (fmt != NULL) {
906 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
907 evtimer_add(&clechoev, &clechoev_timer);
909 /* TODO: what to do if the allocation fails here? */
910 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
911 ministate.curmesg = NULL;
914 redraw_minibuffer();
915 place_cursor(0);
918 void
919 message(const char *fmt, ...)
921 va_list ap;
923 va_start(ap, fmt);
924 vmessage(fmt, ap);
925 va_end(ap);
928 void
929 start_loading_anim(struct tab *tab)
931 if (tab->loading_anim)
932 return;
933 tab->loading_anim = 1;
934 evtimer_set(&tab->loadingev, update_loading_anim, tab);
935 evtimer_add(&tab->loadingev, &loadingev_timer);
938 static void
939 update_loading_anim(int fd, short ev, void *d)
941 struct tab *tab = d;
943 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
945 if (tab == current_tab) {
946 redraw_modeline(tab);
947 wrefresh(modeline);
948 wrefresh(body);
949 if (in_minibuffer)
950 wrefresh(echoarea);
953 evtimer_add(&tab->loadingev, &loadingev_timer);
956 static void
957 stop_loading_anim(struct tab *tab)
959 if (!tab->loading_anim)
960 return;
961 evtimer_del(&tab->loadingev);
962 tab->loading_anim = 0;
963 tab->loading_anim_step = 0;
965 if (tab != current_tab)
966 return;
968 redraw_modeline(tab);
970 wrefresh(modeline);
971 wrefresh(body);
972 if (in_minibuffer)
973 wrefresh(echoarea);
976 void
977 load_url_in_tab(struct tab *tab, const char *url, const char *base)
979 if (!operating) {
980 load_url(tab, url, base);
981 return;
984 message("Loading %s...", url);
985 start_loading_anim(tab);
986 load_url(tab, url, base);
988 redraw_tab(tab);
991 void
992 switch_to_tab(struct tab *tab)
994 current_tab = tab;
995 tab->flags &= ~TAB_URGENT;
997 if (operating && tab->flags & TAB_LAZY)
998 load_url_in_tab(tab, tab->hist_cur->h, NULL);
1001 unsigned int
1002 tab_new_id(void)
1004 return tab_counter++;
1007 struct tab *
1008 new_tab(const char *url, const char *base)
1010 struct tab *tab;
1012 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1013 event_loopbreak();
1014 return NULL;
1016 tab->fd = -1;
1018 TAILQ_INIT(&tab->hist.head);
1020 TAILQ_INIT(&tab->buffer.head);
1022 tab->id = tab_new_id();
1023 if (!operating)
1024 tab->flags |= TAB_LAZY;
1025 switch_to_tab(tab);
1027 if (TAILQ_EMPTY(&tabshead))
1028 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1029 else
1030 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1032 load_url_in_tab(tab, url, base);
1033 return tab;
1036 int
1037 ui_print_colors(void)
1039 int colors = 0, pairs = 0, can_change = 0;
1040 int columns = 16, lines, color, i, j;
1042 initscr();
1043 if (has_colors()) {
1044 start_color();
1045 use_default_colors();
1047 colors = COLORS;
1048 pairs = COLOR_PAIRS;
1049 can_change = can_change_color();
1051 endwin();
1053 printf("Term info:\n");
1054 printf("TERM=%s COLORS=%d COLOR_PAIRS=%d can_change_colors=%d\n",
1055 getenv("TERM"), colors, pairs, can_change);
1056 printf("\n");
1058 if (colors == 0) {
1059 printf("No color support\n");
1060 return 0;
1063 printf("Available colors:\n\n");
1064 lines = (colors - 1) / columns + 1;
1065 color = 0;
1066 for (i = 0; i < lines; ++i) {
1067 for (j = 0; j < columns; ++j, ++color) {
1068 printf("\033[0;38;5;%dm %03d", color, color);
1070 printf("\n");
1073 printf("\033[0m");
1074 fflush(stdout);
1075 return 0;
1078 int
1079 ui_init()
1081 setlocale(LC_ALL, "");
1083 TAILQ_INIT(&eecmd_history.head);
1084 TAILQ_INIT(&ir_history.head);
1085 TAILQ_INIT(&lu_history.head);
1087 ministate.line.type = LINE_TEXT;
1088 ministate.vline.parent = &ministate.line;
1089 ministate.buffer.current_line = &ministate.vline;
1091 /* initialize help window */
1092 TAILQ_INIT(&helpwin.head);
1094 base_map = &global_map;
1095 current_map = &global_map;
1097 initscr();
1099 if (enable_colors) {
1100 if (has_colors()) {
1101 start_color();
1102 use_default_colors();
1103 } else
1104 enable_colors = 0;
1107 config_apply_style();
1109 raw();
1110 noecho();
1111 nonl();
1112 intrflush(stdscr, FALSE);
1114 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1115 return 0;
1116 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1117 return 0;
1118 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1119 return 0;
1120 if ((echoarea = newwin(1, COLS, LINES-1, 0)) == NULL)
1121 return 0;
1122 if ((minibuffer = newwin(1, COLS, LINES-1, 0)) == NULL)
1123 return 0;
1124 if ((help = newwin(1, 1, 1, 0)) == NULL)
1125 return 0;
1127 body_lines = LINES-3;
1128 body_cols = COLS;
1130 wbkgd(body, body_face.body);
1131 wbkgd(echoarea, minibuffer_face.background);
1133 update_x_offset();
1135 keypad(body, TRUE);
1136 scrollok(body, FALSE);
1138 /* non-blocking input */
1139 wtimeout(body, 0);
1141 mvwprintw(body, 0, 0, "");
1144 * Dummy so libevent2 won't complain that no event_base is set
1145 * when checking event_pending for the first time
1147 evtimer_set(&clechoev, handle_clear_echoarea, NULL);
1148 evtimer_set(&resizeev, handle_resize, NULL);
1150 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1151 event_add(&stdioev, NULL);
1153 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1154 signal_add(&winchev, NULL);
1156 return 1;
1159 void
1160 ui_main_loop(void)
1162 operating = 1;
1163 switch_to_tab(current_tab);
1164 redraw_tab(current_tab);
1166 event_dispatch();
1169 void
1170 ui_on_tab_loaded(struct tab *tab)
1172 stop_loading_anim(tab);
1173 message("Loaded %s", tab->hist_cur->h);
1175 redraw_tabline();
1176 wrefresh(tabline);
1177 place_cursor(0);
1180 void
1181 ui_on_tab_refresh(struct tab *tab)
1183 wrap_page(&tab->buffer, body_cols);
1184 if (tab == current_tab)
1185 redraw_tab(tab);
1186 else
1187 tab->flags |= TAB_URGENT;
1190 const char *
1191 ui_keyname(int k)
1193 return keyname(k);
1196 void
1197 ui_toggle_side_window(void)
1199 side_window = !side_window;
1200 if (side_window)
1201 recompute_help();
1204 * ugly hack, but otherwise the window doesn't get updated
1205 * until I call rearrange_windows a second time (e.g. via
1206 * C-l). I will be happy to know why something like this is
1207 * needed.
1209 rearrange_windows();
1210 rearrange_windows();
1213 void
1214 ui_schedule_redraw(void)
1216 should_rearrange_windows = 1;
1219 void
1220 ui_require_input(struct tab *tab, int hide)
1222 /* TODO: hard-switching to another tab is ugly */
1223 switch_to_tab(tab);
1225 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1226 &ir_history, NULL, NULL);
1227 strlcpy(ministate.prompt, "Input required: ",
1228 sizeof(ministate.prompt));
1229 redraw_tab(tab);
1232 void
1233 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1234 struct tab *data)
1236 yornp(prompt, fn, data);
1237 redraw_tab(current_tab);
1240 void
1241 ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1242 struct tab *data)
1244 completing_read(prompt, fn, data);
1245 redraw_tab(current_tab);
1248 void
1249 ui_suspend(void)
1251 endwin();
1253 kill(getpid(), SIGSTOP);
1255 refresh();
1256 clear();
1257 rearrange_windows();
1260 void
1261 ui_end(void)
1263 endwin();