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.
33 #include "telescope.h"
46 static struct event stdioev, winchev;
48 static void global_key_unbound(void);
49 static void minibuffer_hist_save_entry(void);
50 static void minibuffer_self_insert(void);
51 static void yornp_self_insert(void);
52 static void yornp_abort(void);
53 static void read_self_insert(void);
54 static void read_abort(void);
55 static void read_select(void);
57 static struct vline *nth_line(struct buffer*, size_t);
58 static struct buffer *current_buffer(void);
59 static int readkey(void);
60 static void dispatch_stdio(int, short, void*);
61 static void handle_clear_minibuf(int, short, void*);
62 static void handle_resize(int, short, void*);
63 static void handle_resize_nodelay(int, short, void*);
64 static int wrap_page(struct buffer*, int);
65 static void print_vline(int, int, WINDOW*, struct vline*);
66 static void redraw_tabline(void);
67 static void redraw_window(WINDOW*, int, int, struct buffer*);
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 redraw_tab(struct tab*);
73 static void emit_help_item(char*, void*);
74 static void rec_compute_help(struct kmap*, char*, size_t);
75 static void recompute_help(void);
76 static void update_loading_anim(int, short, void*);
77 static void stop_loading_anim(struct tab*);
78 static void session_new_tab_cb(const char*);
79 static void usage(void);
83 struct thiskey thiskey;
85 static struct event resizeev;
86 static struct timeval resize_timer = { 0, 250000 };
88 static WINDOW *tabline, *body, *modeline, *minibuf;
90 int body_lines, body_cols;
93 static struct buffer helpwin;
94 static int help_lines, help_cols;
96 static int side_window;
98 static struct event clminibufev;
99 static struct timeval clminibufev_timer = { 5, 0 };
100 static struct timeval loadingev_timer = { 0, 250000 };
102 static uint32_t tab_counter;
104 static char keybuf[64];
106 static void (*yornp_cb)(int, unsigned int);
107 static unsigned int yornp_data;
109 static void (*read_cb)(const char*, unsigned int);
110 static unsigned int read_data;
112 struct kmap global_map,
117 struct histhead eecmd_history,
124 struct ministate ministate;
127 update_x_offset(void)
129 if (olivetti_mode && fill_column < body_cols)
130 x_offset = (body_cols - fill_column)/2;
136 save_excursion(struct excursion *place, struct buffer *buffer)
138 place->curs_x = buffer->curs_x;
139 place->curs_y = buffer->curs_y;
140 place->line_off = buffer->line_off;
141 place->current_line = buffer->current_line;
142 place->cpoff = buffer->cpoff;
146 restore_excursion(struct excursion *place, struct buffer *buffer)
148 buffer->curs_x = place->curs_x;
149 buffer->curs_y = place->curs_y;
150 buffer->line_off = place->line_off;
151 buffer->current_line = place->current_line;
152 buffer->cpoff = place->cpoff;
156 restore_cursor(struct buffer *buffer)
161 vl = buffer->current_line;
162 if (vl == NULL || vl->line == NULL)
163 buffer->curs_x = buffer->cpoff = 0;
165 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
167 buffer->curs_x += x_offset;
170 prfx = line_prefixes[vl->parent->type].prfx1;
171 buffer->curs_x += utf8_swidth(prfx);
176 global_key_unbound(void)
178 message("%s is undefined", keybuf);
182 minibuffer_hist_save_entry(void)
186 if (ministate.history == NULL)
189 if ((hist = calloc(1, sizeof(*hist))) == NULL)
192 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
194 if (TAILQ_EMPTY(&ministate.history->head))
195 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
197 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
198 ministate.history->len++;
202 * taint the minibuffer cache: if we're currently showing a history
203 * element, copy that to the current buf and reset the "history
207 minibuffer_taint_hist(void)
209 if (ministate.hist_cur == NULL)
212 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
213 ministate.hist_cur = NULL;
217 minibuffer_self_insert(void)
219 char *c, tmp[5] = {0};
222 minibuffer_taint_hist();
227 len = utf8_encode(thiskey.cp, tmp);
228 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
229 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
232 memmove(c + len, c, strlen(c)+1);
234 ministate.buffer.cpoff++;
238 eecmd_self_insert(void)
240 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
241 !unicode_isgraph(thiskey.cp)) {
242 global_key_unbound();
246 minibuffer_self_insert();
254 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
255 if (!strcmp(cmd->cmd, ministate.buf)) {
257 minibuffer_hist_save_entry();
258 cmd->fn(current_buffer());
269 minibuffer_self_insert();
275 char buf[1025] = {0};
282 minibuffer_hist_save_entry();
284 /* a bit ugly but... */
285 memcpy(&uri, &tab->uri, sizeof(tab->uri));
286 phos_uri_set_query(&uri, ministate.buf);
287 phos_serialize_uri(&uri, buf, sizeof(buf));
288 load_url_in_tab(tab, buf);
294 if (thiskey.meta || unicode_isspace(thiskey.key) ||
295 !unicode_isgraph(thiskey.key)) {
296 global_key_unbound();
300 minibuffer_self_insert();
307 minibuffer_hist_save_entry();
308 load_url_in_tab(current_tab(), ministate.buf);
315 if (*ministate.buf != '\0')
316 add_to_bookmarks(ministate.buf);
322 yornp_self_insert(void)
324 if (thiskey.key != 'y' && thiskey.key != 'n') {
325 message("Please answer y or n");
330 yornp_cb(thiskey.key == 'y', yornp_data);
337 yornp_cb(0, yornp_data);
341 read_self_insert(void)
343 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
344 global_key_unbound();
348 minibuffer_self_insert();
355 read_cb(NULL, read_data);
362 minibuffer_hist_save_entry();
363 read_cb(ministate.buf, read_data);
366 static struct vline *
367 nth_line(struct buffer *buffer, size_t n)
373 TAILQ_FOREACH(vl, &buffer->head, vlines) {
388 TAILQ_FOREACH(t, &tabshead, tabs) {
389 if (t->flags & TAB_CURRENT)
401 return &ministate.buffer;
402 return ¤t_tab()->buffer;
410 if ((thiskey.key = wgetch(body)) == ERR)
413 thiskey.meta = thiskey.key == 27;
415 thiskey.key = wgetch(body);
416 if (thiskey.key == ERR || thiskey.key == 27) {
423 if ((unsigned int)thiskey.key < UINT8_MAX) {
425 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
427 if ((thiskey.key = wgetch(body)) == ERR) {
428 message("Error decoding user input");
438 dispatch_stdio(int fd, short ev, void *d)
447 if (keybuf[0] != '\0')
448 strlcat(keybuf, " ", sizeof(keybuf));
450 strlcat(keybuf, "M-", sizeof(keybuf));
451 if (thiskey.cp != 0) {
452 utf8_encode(thiskey.cp, tmp);
453 strlcat(keybuf, tmp, sizeof(keybuf));
455 if ((keyname = unkbd(thiskey.key)) != NULL)
456 strlcat(keybuf, keyname, sizeof(keybuf));
458 tmp[0] = thiskey.key;
459 strlcat(keybuf, tmp, sizeof(keybuf));
463 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
464 if (k->meta == thiskey.meta &&
465 k->key == thiskey.key) {
467 current_map = &k->map;
469 current_map = base_map;
470 strlcpy(keybuf, "", sizeof(keybuf));
471 k->fn(current_buffer());
477 if (current_map->unhandled_input != NULL)
478 current_map->unhandled_input();
480 global_key_unbound();
482 strlcpy(keybuf, "", sizeof(keybuf));
483 current_map = base_map;
489 redraw_tab(current_tab());
493 handle_clear_minibuf(int fd, short ev, void *d)
495 free(ministate.curmesg);
496 ministate.curmesg = NULL;
509 handle_resize(int sig, short ev, void *d)
511 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
512 event_del(&resizeev);
514 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
515 evtimer_add(&resizeev, &resize_timer);
519 handle_resize_nodelay(int s, short ev, void *d)
527 /* move and resize the windows, in reverse order! */
529 mvwin(minibuf, LINES-1, 0);
530 wresize(minibuf, 1, COLS);
532 mvwin(modeline, LINES-2, 0);
533 wresize(modeline, 1, COLS);
535 body_lines = LINES-3;
539 help_cols = 0.3 * COLS;
540 help_lines = LINES-3;
542 wresize(help, help_lines, help_cols);
544 wrap_page(&helpwin, help_cols);
546 body_cols = COLS - help_cols - 1;
547 mvwin(body, 1, help_cols);
552 wresize(body, body_lines, body_cols);
554 wresize(tabline, 1, COLS);
558 wrap_page(&tab->buffer, body_cols);
563 wrap_page(struct buffer *buffer, int width)
566 const struct line *orig;
571 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
573 buffer->top_line = NULL;
574 buffer->current_line = NULL;
576 buffer->force_redraw = 1;
578 buffer->line_off = 0;
582 TAILQ_FOREACH(l, &buffer->page.head, lines) {
583 prfx = line_prefixes[l->type].prfx1;
594 wrap_text(buffer, prfx, l, MIN(fill_column, width));
596 case LINE_PRE_CONTENT:
598 pre_width = MIN(fill_column, width);
601 hardwrap_text(buffer, l, pre_width);
605 if (orig == l && buffer->current_line == NULL) {
606 buffer->line_off = buffer->line_max-1;
607 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
610 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
611 if (vl == NULL || vl->parent != orig)
613 buffer->current_line = vl;
619 if (buffer->current_line == NULL)
620 buffer->current_line = TAILQ_FIRST(&buffer->head);
622 buffer->top_line = buffer->current_line;
628 print_vline(int off, int width, WINDOW *window, struct vline *vl)
635 f = &line_faces[vl->parent->type];
637 /* unused, set by getyx */
641 prfx = line_prefixes[vl->parent->type].prfx1;
643 prfx = line_prefixes[vl->parent->type].prfx2;
649 wattr_on(window, body_face.left, NULL);
650 for (i = 0; i < off; i++)
652 wattr_off(window, body_face.left, NULL);
654 wattr_on(window, f->prefix, NULL);
655 wprintw(window, "%s", prfx);
656 wattr_off(window, f->prefix, NULL);
658 wattr_on(window, f->text, NULL);
659 wprintw(window, "%s", text);
660 wattr_off(window, f->text, NULL);
666 wattr_on(window, f->trail, NULL);
667 for (i = 0; i < left - off - 1; ++i)
669 wattr_off(window, f->trail, NULL);
671 wattr_on(window, body_face.right, NULL);
672 for (i = 0; i < off; i++)
674 wattr_off(window, body_face.right, NULL);
682 size_t toskip, ots, tabwidth, space, x;
683 int current, y, truncated;
689 /* unused, but setted by a getyx */
692 tabwidth = sizeof(buf)+1;
696 TAILQ_FOREACH(tab, &tabshead, tabs) {
698 if (tab->flags & TAB_CURRENT)
702 if (toskip * tabwidth < space)
707 while (toskip != 0 &&
708 (ots - toskip+1) * tabwidth < space)
713 wattr_on(tabline, tab_face.background, NULL);
714 wprintw(tabline, toskip == 0 ? " " : "<");
715 wattr_off(tabline, tab_face.background, NULL);
718 TAILQ_FOREACH(tab, &tabshead, tabs) {
726 getyx(tabline, y, x);
727 if (x + sizeof(buf)+2 >= (size_t)COLS)
730 current = tab->flags & TAB_CURRENT;
732 if (*(title = tab->buffer.page.title) == '\0')
733 title = tab->hist_cur->h;
735 if (tab->flags & TAB_URGENT)
736 strlcpy(buf, "!", sizeof(buf));
738 strlcpy(buf, " ", sizeof(buf));
740 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
741 /* truncation happens */
742 strlcpy(&buf[sizeof(buf)-4], "...", 4);
744 /* pad with spaces */
745 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
750 wattr_on(tabline, tab_face.current, NULL);
752 wattr_on(tabline, tab_face.tab, NULL);
754 wprintw(tabline, "%s", buf);
755 if (TAILQ_NEXT(tab, tabs) != NULL)
756 wprintw(tabline, " ");
759 wattr_off(tabline, tab_face.current, NULL);
761 wattr_off(tabline, tab_face.tab, NULL);
764 wattr_on(tabline, tab_face.background, NULL);
765 for (; x < (size_t)COLS; ++x)
766 waddch(tabline, ' ');
768 mvwprintw(tabline, 0, COLS-1, ">");
769 wattr_off(tabline, tab_face.background, NULL);
773 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
779 * Don't bother redraw the body if nothing changed. Cursor
780 * movements count as "nothing changed" if it hasn't produced
781 * a scroll. Ensure that wmove is called though!
783 if (!buffer->force_redraw &&
784 buffer->last_line_off == buffer->line_off)
789 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
790 buffer->force_redraw = 0;
791 buffer->last_line_off = buffer->line_off;
793 if (TAILQ_EMPTY(&buffer->head))
797 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
799 print_vline(x_offset, width, win, vl);
806 wmove(win, buffer->curs_y, buffer->curs_x);
812 redraw_window(help, help_lines, help_cols, &helpwin);
816 redraw_body(struct tab *tab)
818 static struct tab *last_tab;
821 tab->buffer.force_redraw =1;
824 redraw_window(body, body_lines, body_cols, &tab->buffer);
828 trust_status_char(enum trust_state ts)
831 case TS_UNKNOWN: return 'u';
832 case TS_UNTRUSTED: return '!';
833 case TS_TRUSTED: return 'v';
834 case TS_VERIFIED: return 'V';
840 redraw_modeline(struct tab *tab)
843 int x, y, max_x, max_y;
844 const char *mode = tab->buffer.page.name;
845 const char *spin = "-\\|/";
848 wattr_on(modeline, modeline_face.background, NULL);
849 wmove(modeline, 0, 0);
851 wprintw(modeline, "-%c%c %s ",
852 spin[tab->loading_anim_step],
853 trust_status_char(tab->trust),
854 mode == NULL ? "(none)" : mode);
856 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
858 if (tab->buffer.line_max <= (size_t)body_lines)
859 wprintw(modeline, "All ");
860 else if (tab->buffer.line_off == 0)
861 wprintw(modeline, "Top ");
862 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
863 wprintw(modeline, "Bottom ");
865 wprintw(modeline, "%.0f%% ", pct);
867 wprintw(modeline, "%d/%d %s ",
868 tab->buffer.line_off + tab->buffer.curs_y,
869 tab->buffer.line_max,
872 getyx(modeline, y, x);
873 getmaxyx(modeline, max_y, max_x);
878 for (; x < max_x; ++x)
879 waddstr(modeline, "-");
881 wattr_off(modeline, modeline_face.background, NULL);
885 redraw_minibuffer(void)
888 size_t off_y, off_x = 0;
889 char *start = NULL, *c = NULL;
891 /* unused, but set by getyx */
894 wattr_on(minibuf, minibuffer_face.background, NULL);
898 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
899 if (ministate.hist_cur != NULL)
900 wprintw(minibuf, "(%zu/%zu) ",
901 ministate.hist_off + 1,
902 ministate.history->len);
904 getyx(minibuf, off_y, off_x);
906 start = ministate.hist_cur != NULL
907 ? ministate.hist_cur->h
909 c = utf8_nth(ministate.buffer.current_line->line,
910 ministate.buffer.cpoff);
911 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
912 start = utf8_next_cp(start);
915 waddstr(minibuf, start);
918 if (ministate.curmesg != NULL)
919 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
922 if (!in_minibuffer && ministate.curmesg == NULL)
923 waddstr(minibuf, keybuf);
925 /* If nothing else, show the URL at point */
926 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
928 if (tab->buffer.current_line != NULL &&
929 tab->buffer.current_line->parent->type == LINE_LINK)
930 waddstr(minibuf, tab->buffer.current_line->parent->alt);
934 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
936 wattr_off(minibuf, minibuffer_face.background, NULL);
940 redraw_tab(struct tab *tab)
947 restore_cursor(&tab->buffer);
951 redraw_modeline(tab);
954 wnoutrefresh(tabline);
955 wnoutrefresh(modeline);
959 wnoutrefresh(minibuf);
961 wnoutrefresh(minibuf);
969 emit_help_item(char *prfx, void *fn)
974 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
980 if ((l = calloc(1, sizeof(*l))) == NULL)
986 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
988 if (TAILQ_EMPTY(&helpwin.page.head))
989 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
991 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
995 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1001 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1002 strlcpy(p, prfx, sizeof(p));
1004 strlcat(p, " ", sizeof(p));
1006 strlcat(p, "M-", sizeof(p));
1007 if ((kn = unkbd(k->key)) != NULL)
1008 strlcat(p, kn, sizeof(p));
1010 strlcat(p, keyname(k->key), sizeof(p));
1013 rec_compute_help(&k->map, p, sizeof(p));
1015 emit_help_item(p, k->fn);
1020 recompute_help(void)
1024 empty_vlist(&helpwin);
1025 empty_linelist(&helpwin);
1026 rec_compute_help(current_map, p, sizeof(p));
1027 wrap_page(&helpwin, help_cols);
1031 vmessage(const char *fmt, va_list ap)
1033 if (evtimer_pending(&clminibufev, NULL))
1034 evtimer_del(&clminibufev);
1036 free(ministate.curmesg);
1037 ministate.curmesg = NULL;
1040 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1041 evtimer_add(&clminibufev, &clminibufev_timer);
1043 /* TODO: what to do if the allocation fails here? */
1044 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1045 ministate.curmesg = NULL;
1048 redraw_minibuffer();
1049 if (in_minibuffer) {
1059 message(const char *fmt, ...)
1069 start_loading_anim(struct tab *tab)
1071 if (tab->loading_anim)
1073 tab->loading_anim = 1;
1074 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1075 evtimer_add(&tab->loadingev, &loadingev_timer);
1079 update_loading_anim(int fd, short ev, void *d)
1081 struct tab *tab = d;
1083 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1085 if (tab->flags & TAB_CURRENT) {
1086 redraw_modeline(tab);
1093 evtimer_add(&tab->loadingev, &loadingev_timer);
1097 stop_loading_anim(struct tab *tab)
1099 if (!tab->loading_anim)
1101 evtimer_del(&tab->loadingev);
1102 tab->loading_anim = 0;
1103 tab->loading_anim_step = 0;
1105 if (!(tab->flags & TAB_CURRENT))
1108 redraw_modeline(tab);
1117 load_url_in_tab(struct tab *tab, const char *url)
1119 message("Loading %s...", url);
1120 start_loading_anim(tab);
1123 tab->buffer.curs_x = 0;
1124 tab->buffer.curs_y = 0;
1129 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1130 void (*abortfn)(void), struct histhead *hist)
1133 base_map = &minibuffer_map;
1134 current_map = &minibuffer_map;
1136 base_map->unhandled_input = self_insert_fn;
1138 ministate.donefn = donefn;
1139 ministate.abortfn = abortfn;
1140 memset(ministate.buf, 0, sizeof(ministate.buf));
1141 ministate.buffer.current_line = &ministate.vline;
1142 ministate.buffer.current_line->line = ministate.buf;
1143 ministate.buffer.cpoff = 0;
1144 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1146 ministate.history = hist;
1147 ministate.hist_cur = NULL;
1148 ministate.hist_off = 0;
1152 exit_minibuffer(void)
1157 base_map = &global_map;
1158 current_map = &global_map;
1162 switch_to_tab(struct tab *tab)
1166 TAILQ_FOREACH(t, &tabshead, tabs) {
1167 t->flags &= ~TAB_CURRENT;
1170 tab->flags |= TAB_CURRENT;
1171 tab->flags &= ~TAB_URGENT;
1177 return tab_counter++;
1181 new_tab(const char *url)
1185 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1191 TAILQ_INIT(&tab->hist.head);
1193 TAILQ_INIT(&tab->buffer.head);
1195 tab->id = tab_new_id();
1198 if (TAILQ_EMPTY(&tabshead))
1199 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1201 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1203 load_url_in_tab(tab, url);
1208 session_new_tab_cb(const char *url)
1216 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1217 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1221 ui_init(int argc, char * const *argv)
1223 char path[PATH_MAX];
1224 const char *url = NEW_TAB_URL;
1225 int ch, configtest = 0, fonf = 0;
1227 if (getenv("NO_COLOR") != NULL)
1230 strlcpy(path, getenv("HOME"), sizeof(path));
1231 strlcat(path, "/.telescope/config", sizeof(path));
1233 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1237 strlcpy(path, optarg, sizeof(path));
1253 /* setup keys before reading the config */
1254 TAILQ_INIT(&global_map.m);
1255 global_map.unhandled_input = global_key_unbound;
1257 TAILQ_INIT(&minibuffer_map.m);
1260 parseconfig(path, fonf);
1269 setlocale(LC_ALL, "");
1271 TAILQ_INIT(&eecmd_history.head);
1272 TAILQ_INIT(&ir_history.head);
1273 TAILQ_INIT(&lu_history.head);
1275 ministate.line.type = LINE_TEXT;
1276 ministate.vline.parent = &ministate.line;
1277 ministate.buffer.current_line = &ministate.vline;
1279 /* initialize help window */
1280 TAILQ_INIT(&helpwin.head);
1282 base_map = &global_map;
1283 current_map = &global_map;
1287 if (enable_colors) {
1290 use_default_colors();
1295 config_apply_style();
1300 intrflush(stdscr, FALSE);
1302 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1304 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1306 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1308 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1310 if ((help = newwin(1, 1, 1, 0)) == NULL)
1313 body_lines = LINES-3;
1316 wbkgd(body, body_face.body);
1317 wbkgd(minibuf, minibuffer_face.background);
1322 scrollok(body, FALSE);
1324 /* non-blocking input */
1327 mvwprintw(body, 0, 0, "");
1329 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1330 event_add(&stdioev, NULL);
1332 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1333 signal_add(&winchev, NULL);
1335 load_last_session(session_new_tab_cb);
1336 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1343 ui_on_tab_loaded(struct tab *tab)
1345 stop_loading_anim(tab);
1346 message("Loaded %s", tab->hist_cur->h);
1357 ui_on_tab_refresh(struct tab *tab)
1359 wrap_page(&tab->buffer, body_cols);
1360 if (tab->flags & TAB_CURRENT) {
1361 restore_cursor(&tab->buffer);
1364 tab->flags |= TAB_URGENT;
1374 ui_toggle_side_window(void)
1376 side_window = !side_window;
1381 * ugly hack, but otherwise the window doesn't get updated
1382 * until I call handle_resize a second time (i.e. C-l). I
1383 * will be happy to know why something like this is needed.
1385 handle_resize_nodelay(0, 0, NULL);
1386 handle_resize_nodelay(0, 0, NULL);
1390 ui_schedule_redraw(void)
1392 handle_resize_nodelay(0, 0, NULL);
1396 ui_require_input(struct tab *tab, int hide)
1398 /* TODO: hard-switching to another tab is ugly */
1401 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1403 strlcpy(ministate.prompt, "Input required: ",
1404 sizeof(ministate.prompt));
1409 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1414 if (in_minibuffer) {
1421 enter_minibuffer(yornp_self_insert, yornp_self_insert,
1424 len = sizeof(ministate.prompt);
1425 strlcpy(ministate.prompt, prompt, len);
1426 strlcat(ministate.prompt, " (y or n) ", len);
1427 redraw_tab(current_tab());
1431 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1441 enter_minibuffer(read_self_insert, read_select, read_abort,
1444 len = sizeof(ministate.prompt);
1445 strlcpy(ministate.prompt, prompt, len);
1446 strlcat(ministate.prompt, ": ", len);
1447 redraw_tab(current_tab());