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 restore_curs_x(struct buffer *);
49 static void global_key_unbound(void);
50 static void minibuffer_hist_save_entry(void);
51 static void minibuffer_self_insert(void);
52 static void yornp_self_insert(void);
53 static void yornp_abort(void);
54 static void read_self_insert(void);
55 static void read_abort(void);
56 static void read_select(void);
58 static struct vline *nth_line(struct buffer*, size_t);
59 static struct buffer *current_buffer(void);
60 static int readkey(void);
61 static void dispatch_stdio(int, short, void*);
62 static void handle_clear_minibuf(int, short, void*);
63 static void handle_resize(int, short, void*);
64 static void handle_resize_nodelay(int, short, void*);
65 static int wrap_page(struct buffer*, int);
66 static void print_vline(int, int, WINDOW*, struct vline*);
67 static void redraw_tabline(void);
68 static void redraw_window(WINDOW*, int, int, struct buffer*);
69 static void redraw_help(void);
70 static void redraw_body(struct tab*);
71 static void redraw_modeline(struct tab*);
72 static void redraw_minibuffer(void);
73 static void redraw_tab(struct tab*);
74 static void emit_help_item(char*, void*);
75 static void rec_compute_help(struct kmap*, char*, size_t);
76 static void recompute_help(void);
77 static void update_loading_anim(int, short, void*);
78 static void stop_loading_anim(struct tab*);
79 static void session_new_tab_cb(const char*);
80 static void usage(void);
84 struct thiskey thiskey;
86 static struct event resizeev;
87 static struct timeval resize_timer = { 0, 250000 };
89 static WINDOW *tabline, *body, *modeline, *minibuf;
91 int body_lines, body_cols;
94 static struct buffer helpwin;
95 static int help_lines, help_cols;
97 static int side_window;
99 static struct event clminibufev;
100 static struct timeval clminibufev_timer = { 5, 0 };
101 static struct timeval loadingev_timer = { 0, 250000 };
103 static uint32_t tab_counter;
105 static char keybuf[64];
107 static void (*yornp_cb)(int, struct tab *);
108 static struct tab *yornp_data;
110 static void (*read_cb)(const char*, unsigned int);
111 static unsigned int read_data;
113 struct kmap global_map,
118 struct histhead eecmd_history,
125 struct ministate ministate;
128 update_x_offset(void)
130 if (olivetti_mode && fill_column < body_cols)
131 x_offset = (body_cols - fill_column)/2;
137 save_excursion(struct excursion *place, struct buffer *buffer)
139 place->curs_x = buffer->curs_x;
140 place->curs_y = buffer->curs_y;
141 place->line_off = buffer->line_off;
142 place->current_line = buffer->current_line;
143 place->cpoff = buffer->cpoff;
147 restore_excursion(struct excursion *place, struct buffer *buffer)
149 buffer->curs_x = place->curs_x;
150 buffer->curs_y = place->curs_y;
151 buffer->line_off = place->line_off;
152 buffer->current_line = place->current_line;
153 buffer->cpoff = place->cpoff;
157 restore_curs_x(struct buffer *buffer)
162 vl = buffer->current_line;
163 if (vl == NULL || vl->line == NULL)
164 buffer->curs_x = buffer->cpoff = 0;
166 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
168 buffer->curs_x += x_offset;
171 prfx = line_prefixes[vl->parent->type].prfx1;
172 buffer->curs_x += utf8_swidth(prfx);
177 global_key_unbound(void)
179 message("%s is undefined", keybuf);
183 minibuffer_hist_save_entry(void)
187 if (ministate.history == NULL)
190 if ((hist = calloc(1, sizeof(*hist))) == NULL)
193 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
195 if (TAILQ_EMPTY(&ministate.history->head))
196 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
198 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
199 ministate.history->len++;
203 * taint the minibuffer cache: if we're currently showing a history
204 * element, copy that to the current buf and reset the "history
208 minibuffer_taint_hist(void)
210 if (ministate.hist_cur == NULL)
213 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
214 ministate.hist_cur = NULL;
218 minibuffer_self_insert(void)
220 char *c, tmp[5] = {0};
223 minibuffer_taint_hist();
228 len = utf8_encode(thiskey.cp, tmp);
229 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
230 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
233 memmove(c + len, c, strlen(c)+1);
235 ministate.buffer.cpoff++;
239 eecmd_self_insert(void)
241 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
242 !unicode_isgraph(thiskey.cp)) {
243 global_key_unbound();
247 minibuffer_self_insert();
255 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
256 if (!strcmp(cmd->cmd, ministate.buf)) {
258 minibuffer_hist_save_entry();
259 cmd->fn(current_buffer());
270 minibuffer_self_insert();
276 char buf[1025] = {0};
283 minibuffer_hist_save_entry();
285 /* a bit ugly but... */
286 memcpy(&uri, &tab->uri, sizeof(tab->uri));
287 phos_uri_set_query(&uri, ministate.buf);
288 phos_serialize_uri(&uri, buf, sizeof(buf));
289 load_url_in_tab(tab, buf);
295 if (thiskey.meta || unicode_isspace(thiskey.key) ||
296 !unicode_isgraph(thiskey.key)) {
297 global_key_unbound();
301 minibuffer_self_insert();
308 minibuffer_hist_save_entry();
309 load_url_in_tab(current_tab(), ministate.buf);
316 if (*ministate.buf != '\0')
317 add_to_bookmarks(ministate.buf);
323 yornp_self_insert(void)
325 if (thiskey.key != 'y' && thiskey.key != 'n') {
326 message("Please answer y or n");
331 yornp_cb(thiskey.key == 'y', yornp_data);
338 yornp_cb(0, yornp_data);
342 read_self_insert(void)
344 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
345 global_key_unbound();
349 minibuffer_self_insert();
356 read_cb(NULL, read_data);
363 minibuffer_hist_save_entry();
364 read_cb(ministate.buf, read_data);
367 static struct vline *
368 nth_line(struct buffer *buffer, size_t n)
374 TAILQ_FOREACH(vl, &buffer->head, vlines) {
389 TAILQ_FOREACH(t, &tabshead, tabs) {
390 if (t->flags & TAB_CURRENT)
402 return &ministate.buffer;
403 return ¤t_tab()->buffer;
411 if ((thiskey.key = wgetch(body)) == ERR)
414 thiskey.meta = thiskey.key == 27;
416 thiskey.key = wgetch(body);
417 if (thiskey.key == ERR || thiskey.key == 27) {
424 if ((unsigned int)thiskey.key < UINT8_MAX) {
426 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
428 if ((thiskey.key = wgetch(body)) == ERR) {
429 message("Error decoding user input");
439 dispatch_stdio(int fd, short ev, void *d)
448 if (keybuf[0] != '\0')
449 strlcat(keybuf, " ", sizeof(keybuf));
451 strlcat(keybuf, "M-", sizeof(keybuf));
452 if (thiskey.cp != 0) {
453 utf8_encode(thiskey.cp, tmp);
454 strlcat(keybuf, tmp, sizeof(keybuf));
456 if ((keyname = unkbd(thiskey.key)) != NULL)
457 strlcat(keybuf, keyname, sizeof(keybuf));
459 tmp[0] = thiskey.key;
460 strlcat(keybuf, tmp, sizeof(keybuf));
464 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
465 if (k->meta == thiskey.meta &&
466 k->key == thiskey.key) {
468 current_map = &k->map;
470 current_map = base_map;
471 strlcpy(keybuf, "", sizeof(keybuf));
472 k->fn(current_buffer());
478 if (current_map->unhandled_input != NULL)
479 current_map->unhandled_input();
481 global_key_unbound();
483 strlcpy(keybuf, "", sizeof(keybuf));
484 current_map = base_map;
490 redraw_tab(current_tab());
494 handle_clear_minibuf(int fd, short ev, void *d)
496 free(ministate.curmesg);
497 ministate.curmesg = NULL;
510 handle_resize(int sig, short ev, void *d)
512 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
513 event_del(&resizeev);
515 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
516 evtimer_add(&resizeev, &resize_timer);
520 handle_resize_nodelay(int s, short ev, void *d)
528 /* move and resize the windows, in reverse order! */
530 mvwin(minibuf, LINES-1, 0);
531 wresize(minibuf, 1, COLS);
533 mvwin(modeline, LINES-2, 0);
534 wresize(modeline, 1, COLS);
536 body_lines = LINES-3;
540 help_cols = 0.3 * COLS;
541 help_lines = LINES-3;
543 wresize(help, help_lines, help_cols);
545 wrap_page(&helpwin, help_cols);
547 body_cols = COLS - help_cols - 1;
548 mvwin(body, 1, help_cols);
553 wresize(body, body_lines, body_cols);
555 wresize(tabline, 1, COLS);
559 wrap_page(&tab->buffer, body_cols);
564 wrap_page(struct buffer *buffer, int width)
567 const struct line *top_orig, *orig;
572 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
573 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
575 buffer->top_line = NULL;
576 buffer->current_line = NULL;
578 buffer->force_redraw = 1;
580 buffer->line_off = 0;
584 TAILQ_FOREACH(l, &buffer->page.head, lines) {
585 prfx = line_prefixes[l->type].prfx1;
596 wrap_text(buffer, prfx, l, MIN(fill_column, width));
598 case LINE_PRE_CONTENT:
600 pre_width = MIN(fill_column, width);
603 hardwrap_text(buffer, l, pre_width);
607 if (top_orig == l && buffer->top_line == NULL) {
608 buffer->line_off = buffer->line_max-1;
609 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
612 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
613 if (vl == NULL || vl->parent != orig)
615 buffer->top_line = vl;
620 if (orig == l && buffer->current_line == NULL) {
621 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
624 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
625 if (vl == NULL || vl->parent != orig)
627 buffer->current_line = vl;
632 if (buffer->current_line == NULL)
633 buffer->current_line = TAILQ_FIRST(&buffer->head);
635 if (buffer->top_line == NULL)
636 buffer->top_line = buffer->current_line;
642 print_vline(int off, int width, WINDOW *window, struct vline *vl)
649 f = &line_faces[vl->parent->type];
651 /* unused, set by getyx */
655 prfx = line_prefixes[vl->parent->type].prfx1;
657 prfx = line_prefixes[vl->parent->type].prfx2;
663 wattr_on(window, body_face.left, NULL);
664 for (i = 0; i < off; i++)
666 wattr_off(window, body_face.left, NULL);
668 wattr_on(window, f->prefix, NULL);
669 wprintw(window, "%s", prfx);
670 wattr_off(window, f->prefix, NULL);
672 wattr_on(window, f->text, NULL);
673 wprintw(window, "%s", text);
674 wattr_off(window, f->text, NULL);
680 wattr_on(window, f->trail, NULL);
681 for (i = 0; i < left - off - 1; ++i)
683 wattr_off(window, f->trail, NULL);
685 wattr_on(window, body_face.right, NULL);
686 for (i = 0; i < off; i++)
688 wattr_off(window, body_face.right, NULL);
696 size_t toskip, ots, tabwidth, space, x;
697 int current, y, truncated;
703 /* unused, but setted by a getyx */
706 tabwidth = sizeof(buf)+1;
710 TAILQ_FOREACH(tab, &tabshead, tabs) {
712 if (tab->flags & TAB_CURRENT)
716 if (toskip * tabwidth < space)
721 while (toskip != 0 &&
722 (ots - toskip+1) * tabwidth < space)
727 wattr_on(tabline, tab_face.background, NULL);
728 wprintw(tabline, toskip == 0 ? " " : "<");
729 wattr_off(tabline, tab_face.background, NULL);
732 TAILQ_FOREACH(tab, &tabshead, tabs) {
740 getyx(tabline, y, x);
741 if (x + sizeof(buf)+2 >= (size_t)COLS)
744 current = tab->flags & TAB_CURRENT;
746 if (*(title = tab->buffer.page.title) == '\0')
747 title = tab->hist_cur->h;
749 if (tab->flags & TAB_URGENT)
750 strlcpy(buf, "!", sizeof(buf));
752 strlcpy(buf, " ", sizeof(buf));
754 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
755 /* truncation happens */
756 strlcpy(&buf[sizeof(buf)-4], "...", 4);
758 /* pad with spaces */
759 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
764 wattr_on(tabline, tab_face.current, NULL);
766 wattr_on(tabline, tab_face.tab, NULL);
768 wprintw(tabline, "%s", buf);
769 if (TAILQ_NEXT(tab, tabs) != NULL)
770 wprintw(tabline, " ");
773 wattr_off(tabline, tab_face.current, NULL);
775 wattr_off(tabline, tab_face.tab, NULL);
778 wattr_on(tabline, tab_face.background, NULL);
779 for (; x < (size_t)COLS; ++x)
780 waddch(tabline, ' ');
782 mvwprintw(tabline, 0, COLS-1, ">");
783 wattr_off(tabline, tab_face.background, NULL);
787 * Compute the first visible line around vl. Try to search forward
788 * until the end of the buffer; if a visible line is not found, search
789 * backward. Return NULL if no viable line was found.
791 static inline struct vline *
792 adjust_line(struct vline *vl, struct buffer *buffer)
796 if (!(vl->parent->flags & L_HIDDEN))
801 t != NULL && t->parent->flags & L_HIDDEN;
802 t = TAILQ_NEXT(t, vlines))
808 /* search backward */
810 t != NULL && t->parent->flags & L_HIDDEN;
811 t = TAILQ_PREV(t, vhead, vlines))
818 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
823 restore_curs_x(buffer);
826 * TODO: ignoring buffer->force_update and always
827 * re-rendering. In theory we can recompute the y position
828 * without a re-render, and optimize here. It's not the only
829 * optimisation possible here, wscrl wolud also be an
837 if (TAILQ_EMPTY(&buffer->head))
840 buffer->top_line = adjust_line(buffer->top_line, buffer);
841 if (buffer->top_line == NULL)
844 buffer->current_line = adjust_line(buffer->current_line, buffer);
848 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
849 if (vl->parent->flags & L_HIDDEN)
853 print_vline(x_offset, width, win, vl);
855 if (vl == buffer->current_line)
867 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
868 if (vl == buffer->current_line)
870 if (vl->parent->flags & L_HIDDEN)
873 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
879 buffer->last_line_off = buffer->line_off;
880 buffer->force_redraw = 0;
882 wmove(win, buffer->curs_y, buffer->curs_x);
888 redraw_window(help, help_lines, help_cols, &helpwin);
892 redraw_body(struct tab *tab)
894 static struct tab *last_tab;
897 tab->buffer.force_redraw =1;
900 redraw_window(body, body_lines, body_cols, &tab->buffer);
904 trust_status_char(enum trust_state ts)
907 case TS_UNKNOWN: return 'u';
908 case TS_UNTRUSTED: return '!';
909 case TS_TEMP_TRUSTED: return '!';
910 case TS_TRUSTED: return 'v';
911 case TS_VERIFIED: return 'V';
917 redraw_modeline(struct tab *tab)
920 int x, y, max_x, max_y;
921 const char *mode = tab->buffer.page.name;
922 const char *spin = "-\\|/";
925 wattr_on(modeline, modeline_face.background, NULL);
926 wmove(modeline, 0, 0);
928 wprintw(modeline, "-%c%c %s ",
929 spin[tab->loading_anim_step],
930 trust_status_char(tab->trust),
931 mode == NULL ? "(none)" : mode);
933 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
935 if (tab->buffer.line_max <= (size_t)body_lines)
936 wprintw(modeline, "All ");
937 else if (tab->buffer.line_off == 0)
938 wprintw(modeline, "Top ");
939 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
940 wprintw(modeline, "Bottom ");
942 wprintw(modeline, "%.0f%% ", pct);
944 wprintw(modeline, "%d/%d %s ",
945 tab->buffer.line_off + tab->buffer.curs_y,
946 tab->buffer.line_max,
949 getyx(modeline, y, x);
950 getmaxyx(modeline, max_y, max_x);
955 for (; x < max_x; ++x)
956 waddstr(modeline, "-");
958 wattr_off(modeline, modeline_face.background, NULL);
962 redraw_minibuffer(void)
965 size_t off_y, off_x = 0;
966 char *start = NULL, *c = NULL;
968 /* unused, but set by getyx */
971 wattr_on(minibuf, minibuffer_face.background, NULL);
975 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
976 if (ministate.hist_cur != NULL)
977 wprintw(minibuf, "(%zu/%zu) ",
978 ministate.hist_off + 1,
979 ministate.history->len);
981 getyx(minibuf, off_y, off_x);
983 start = ministate.hist_cur != NULL
984 ? ministate.hist_cur->h
986 c = utf8_nth(ministate.buffer.current_line->line,
987 ministate.buffer.cpoff);
988 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
989 start = utf8_next_cp(start);
992 waddstr(minibuf, start);
995 if (ministate.curmesg != NULL)
996 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
999 if (!in_minibuffer && ministate.curmesg == NULL)
1000 waddstr(minibuf, keybuf);
1002 /* If nothing else, show the URL at point */
1003 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1004 tab = current_tab();
1005 if (tab->buffer.current_line != NULL &&
1006 tab->buffer.current_line->parent->type == LINE_LINK)
1007 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1011 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1013 wattr_off(minibuf, minibuffer_face.background, NULL);
1017 redraw_tab(struct tab *tab)
1026 redraw_modeline(tab);
1027 redraw_minibuffer();
1029 wnoutrefresh(tabline);
1030 wnoutrefresh(modeline);
1032 if (in_minibuffer) {
1034 wnoutrefresh(minibuf);
1036 wnoutrefresh(minibuf);
1044 emit_help_item(char *prfx, void *fn)
1049 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1053 assert(cmd != NULL);
1055 if ((l = calloc(1, sizeof(*l))) == NULL)
1058 l->type = LINE_TEXT;
1061 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1063 if (TAILQ_EMPTY(&helpwin.page.head))
1064 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1066 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1070 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1076 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1077 strlcpy(p, prfx, sizeof(p));
1079 strlcat(p, " ", sizeof(p));
1081 strlcat(p, "M-", sizeof(p));
1082 if ((kn = unkbd(k->key)) != NULL)
1083 strlcat(p, kn, sizeof(p));
1085 strlcat(p, keyname(k->key), sizeof(p));
1088 rec_compute_help(&k->map, p, sizeof(p));
1090 emit_help_item(p, k->fn);
1095 recompute_help(void)
1099 empty_vlist(&helpwin);
1100 empty_linelist(&helpwin);
1101 rec_compute_help(current_map, p, sizeof(p));
1102 wrap_page(&helpwin, help_cols);
1106 vmessage(const char *fmt, va_list ap)
1108 if (evtimer_pending(&clminibufev, NULL))
1109 evtimer_del(&clminibufev);
1111 free(ministate.curmesg);
1112 ministate.curmesg = NULL;
1115 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1116 evtimer_add(&clminibufev, &clminibufev_timer);
1118 /* TODO: what to do if the allocation fails here? */
1119 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1120 ministate.curmesg = NULL;
1123 redraw_minibuffer();
1124 if (in_minibuffer) {
1134 message(const char *fmt, ...)
1144 start_loading_anim(struct tab *tab)
1146 if (tab->loading_anim)
1148 tab->loading_anim = 1;
1149 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1150 evtimer_add(&tab->loadingev, &loadingev_timer);
1154 update_loading_anim(int fd, short ev, void *d)
1156 struct tab *tab = d;
1158 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1160 if (tab->flags & TAB_CURRENT) {
1161 redraw_modeline(tab);
1168 evtimer_add(&tab->loadingev, &loadingev_timer);
1172 stop_loading_anim(struct tab *tab)
1174 if (!tab->loading_anim)
1176 evtimer_del(&tab->loadingev);
1177 tab->loading_anim = 0;
1178 tab->loading_anim_step = 0;
1180 if (!(tab->flags & TAB_CURRENT))
1183 redraw_modeline(tab);
1192 load_url_in_tab(struct tab *tab, const char *url)
1194 message("Loading %s...", url);
1195 start_loading_anim(tab);
1198 tab->buffer.curs_x = 0;
1199 tab->buffer.curs_y = 0;
1204 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1205 void (*abortfn)(void), struct histhead *hist)
1208 base_map = &minibuffer_map;
1209 current_map = &minibuffer_map;
1211 base_map->unhandled_input = self_insert_fn;
1213 ministate.donefn = donefn;
1214 ministate.abortfn = abortfn;
1215 memset(ministate.buf, 0, sizeof(ministate.buf));
1216 ministate.buffer.current_line = &ministate.vline;
1217 ministate.buffer.current_line->line = ministate.buf;
1218 ministate.buffer.cpoff = 0;
1219 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1221 ministate.history = hist;
1222 ministate.hist_cur = NULL;
1223 ministate.hist_off = 0;
1227 exit_minibuffer(void)
1232 base_map = &global_map;
1233 current_map = &global_map;
1237 switch_to_tab(struct tab *tab)
1241 TAILQ_FOREACH(t, &tabshead, tabs) {
1242 t->flags &= ~TAB_CURRENT;
1245 tab->flags |= TAB_CURRENT;
1246 tab->flags &= ~TAB_URGENT;
1252 return tab_counter++;
1256 new_tab(const char *url)
1260 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1266 TAILQ_INIT(&tab->hist.head);
1268 TAILQ_INIT(&tab->buffer.head);
1270 tab->id = tab_new_id();
1273 if (TAILQ_EMPTY(&tabshead))
1274 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1276 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1278 load_url_in_tab(tab, url);
1283 session_new_tab_cb(const char *url)
1291 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1292 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1296 ui_init(int argc, char * const *argv)
1298 char path[PATH_MAX];
1299 const char *url = NEW_TAB_URL;
1300 int ch, configtest = 0, fonf = 0;
1302 if (getenv("NO_COLOR") != NULL)
1305 strlcpy(path, getenv("HOME"), sizeof(path));
1306 strlcat(path, "/.telescope/config", sizeof(path));
1308 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1312 strlcpy(path, optarg, sizeof(path));
1328 /* setup keys before reading the config */
1329 TAILQ_INIT(&global_map.m);
1330 global_map.unhandled_input = global_key_unbound;
1332 TAILQ_INIT(&minibuffer_map.m);
1335 parseconfig(path, fonf);
1344 setlocale(LC_ALL, "");
1346 TAILQ_INIT(&eecmd_history.head);
1347 TAILQ_INIT(&ir_history.head);
1348 TAILQ_INIT(&lu_history.head);
1350 ministate.line.type = LINE_TEXT;
1351 ministate.vline.parent = &ministate.line;
1352 ministate.buffer.current_line = &ministate.vline;
1354 /* initialize help window */
1355 TAILQ_INIT(&helpwin.head);
1357 base_map = &global_map;
1358 current_map = &global_map;
1362 if (enable_colors) {
1365 use_default_colors();
1370 config_apply_style();
1375 intrflush(stdscr, FALSE);
1377 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1379 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1381 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1383 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1385 if ((help = newwin(1, 1, 1, 0)) == NULL)
1388 body_lines = LINES-3;
1391 wbkgd(body, body_face.body);
1392 wbkgd(minibuf, minibuffer_face.background);
1397 scrollok(body, FALSE);
1399 /* non-blocking input */
1402 mvwprintw(body, 0, 0, "");
1404 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1405 event_add(&stdioev, NULL);
1407 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1408 signal_add(&winchev, NULL);
1410 load_last_session(session_new_tab_cb);
1411 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1418 ui_on_tab_loaded(struct tab *tab)
1420 stop_loading_anim(tab);
1421 message("Loaded %s", tab->hist_cur->h);
1432 ui_on_tab_refresh(struct tab *tab)
1434 wrap_page(&tab->buffer, body_cols);
1435 if (tab->flags & TAB_CURRENT)
1438 tab->flags |= TAB_URGENT;
1448 ui_toggle_side_window(void)
1450 side_window = !side_window;
1455 * ugly hack, but otherwise the window doesn't get updated
1456 * until I call handle_resize a second time (i.e. C-l). I
1457 * will be happy to know why something like this is needed.
1459 handle_resize_nodelay(0, 0, NULL);
1460 handle_resize_nodelay(0, 0, NULL);
1464 ui_schedule_redraw(void)
1466 handle_resize_nodelay(0, 0, NULL);
1470 ui_require_input(struct tab *tab, int hide)
1472 /* TODO: hard-switching to another tab is ugly */
1475 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1477 strlcpy(ministate.prompt, "Input required: ",
1478 sizeof(ministate.prompt));
1483 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1488 if (in_minibuffer) {
1495 enter_minibuffer(yornp_self_insert, yornp_self_insert,
1498 len = sizeof(ministate.prompt);
1499 strlcpy(ministate.prompt, prompt, len);
1500 strlcat(ministate.prompt, " (y or n) ", len);
1501 redraw_tab(current_tab());
1505 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1515 enter_minibuffer(read_self_insert, read_select, read_abort,
1518 len = sizeof(ministate.prompt);
1519 strlcpy(ministate.prompt, prompt, len);
1520 strlcat(ministate.prompt, ": ", len);
1521 redraw_tab(current_tab());