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"
45 static struct event stdioev, winchev;
47 static void restore_curs_x(struct buffer *);
48 static void minibuffer_hist_save_entry(void);
49 static void minibuffer_self_insert(void);
50 static void yornp_self_insert(void);
51 static void yornp_abort(void);
52 static void read_self_insert(void);
53 static void read_abort(void);
54 static void read_select(void);
56 static struct vline *nth_line(struct buffer*, size_t);
57 static struct buffer *current_buffer(void);
58 static int readkey(void);
59 static void dispatch_stdio(int, short, void*);
60 static void handle_clear_minibuf(int, short, void*);
61 static void handle_resize(int, short, void*);
62 static void handle_resize_nodelay(int, short, void*);
63 static int wrap_page(struct buffer*, int);
64 static void print_vline(int, int, WINDOW*, struct vline*);
65 static void redraw_tabline(void);
66 static void redraw_window(WINDOW*, int, int, struct buffer*);
67 static void redraw_help(void);
68 static void redraw_body(struct tab*);
69 static void redraw_modeline(struct tab*);
70 static void redraw_minibuffer(void);
71 static void redraw_tab(struct tab*);
72 static void emit_help_item(char*, void*);
73 static void rec_compute_help(struct kmap*, char*, size_t);
74 static void recompute_help(void);
75 static void update_loading_anim(int, short, void*);
76 static void stop_loading_anim(struct tab*);
80 struct thiskey thiskey;
82 static struct event resizeev;
83 static struct timeval resize_timer = { 0, 250000 };
85 static WINDOW *tabline, *body, *modeline, *minibuf;
87 int body_lines, body_cols;
90 static struct buffer helpwin;
91 static int help_lines, help_cols;
93 static int side_window;
95 static struct event clminibufev;
96 static struct timeval clminibufev_timer = { 5, 0 };
97 static struct timeval loadingev_timer = { 0, 250000 };
99 static uint32_t tab_counter;
101 static char keybuf[64];
103 static void (*yornp_cb)(int, struct tab *);
104 static struct tab *yornp_data;
106 static void (*read_cb)(const char*, unsigned int);
107 static unsigned int read_data;
109 struct kmap global_map,
114 struct histhead eecmd_history,
121 struct ministate ministate;
124 update_x_offset(void)
126 if (olivetti_mode && fill_column < body_cols)
127 x_offset = (body_cols - fill_column)/2;
133 save_excursion(struct excursion *place, struct buffer *buffer)
135 place->curs_x = buffer->curs_x;
136 place->curs_y = buffer->curs_y;
137 place->line_off = buffer->line_off;
138 place->current_line = buffer->current_line;
139 place->cpoff = buffer->cpoff;
143 restore_excursion(struct excursion *place, struct buffer *buffer)
145 buffer->curs_x = place->curs_x;
146 buffer->curs_y = place->curs_y;
147 buffer->line_off = place->line_off;
148 buffer->current_line = place->current_line;
149 buffer->cpoff = place->cpoff;
153 restore_curs_x(struct buffer *buffer)
158 vl = buffer->current_line;
159 if (vl == NULL || vl->line == NULL)
160 buffer->curs_x = buffer->cpoff = 0;
162 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
164 buffer->curs_x += x_offset;
167 prfx = line_prefixes[vl->parent->type].prfx1;
168 buffer->curs_x += utf8_swidth(prfx);
173 global_key_unbound(void)
175 message("%s is undefined", keybuf);
179 minibuffer_hist_save_entry(void)
183 if (ministate.history == NULL)
186 if ((hist = calloc(1, sizeof(*hist))) == NULL)
189 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
191 if (TAILQ_EMPTY(&ministate.history->head))
192 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
194 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
195 ministate.history->len++;
199 * taint the minibuffer cache: if we're currently showing a history
200 * element, copy that to the current buf and reset the "history
204 minibuffer_taint_hist(void)
206 if (ministate.hist_cur == NULL)
209 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
210 ministate.hist_cur = NULL;
214 minibuffer_self_insert(void)
216 char *c, tmp[5] = {0};
219 minibuffer_taint_hist();
224 len = utf8_encode(thiskey.cp, tmp);
225 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
226 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
229 memmove(c + len, c, strlen(c)+1);
231 ministate.buffer.cpoff++;
235 eecmd_self_insert(void)
237 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
238 !unicode_isgraph(thiskey.cp)) {
239 global_key_unbound();
243 minibuffer_self_insert();
251 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
252 if (!strcmp(cmd->cmd, ministate.buf)) {
254 minibuffer_hist_save_entry();
255 cmd->fn(current_buffer());
266 minibuffer_self_insert();
272 char buf[1025] = {0};
279 minibuffer_hist_save_entry();
281 /* a bit ugly but... */
282 memcpy(&uri, &tab->uri, sizeof(tab->uri));
283 phos_uri_set_query(&uri, ministate.buf);
284 phos_serialize_uri(&uri, buf, sizeof(buf));
285 load_url_in_tab(tab, buf);
291 if (thiskey.meta || unicode_isspace(thiskey.key) ||
292 !unicode_isgraph(thiskey.key)) {
293 global_key_unbound();
297 minibuffer_self_insert();
304 minibuffer_hist_save_entry();
305 load_url_in_tab(current_tab(), ministate.buf);
312 if (*ministate.buf != '\0')
313 add_to_bookmarks(ministate.buf);
319 yornp_self_insert(void)
321 if (thiskey.key != 'y' && thiskey.key != 'n') {
322 message("Please answer y or n");
327 yornp_cb(thiskey.key == 'y', yornp_data);
334 yornp_cb(0, yornp_data);
338 read_self_insert(void)
340 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
341 global_key_unbound();
345 minibuffer_self_insert();
352 read_cb(NULL, read_data);
359 minibuffer_hist_save_entry();
360 read_cb(ministate.buf, read_data);
363 static struct vline *
364 nth_line(struct buffer *buffer, size_t n)
370 TAILQ_FOREACH(vl, &buffer->head, vlines) {
385 TAILQ_FOREACH(t, &tabshead, tabs) {
386 if (t->flags & TAB_CURRENT)
398 return &ministate.buffer;
399 return ¤t_tab()->buffer;
407 if ((thiskey.key = wgetch(body)) == ERR)
410 thiskey.meta = thiskey.key == 27;
412 thiskey.key = wgetch(body);
413 if (thiskey.key == ERR || thiskey.key == 27) {
420 if ((unsigned int)thiskey.key < UINT8_MAX) {
422 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
424 if ((thiskey.key = wgetch(body)) == ERR) {
425 message("Error decoding user input");
435 dispatch_stdio(int fd, short ev, void *d)
444 if (keybuf[0] != '\0')
445 strlcat(keybuf, " ", sizeof(keybuf));
447 strlcat(keybuf, "M-", sizeof(keybuf));
448 if (thiskey.cp != 0) {
449 utf8_encode(thiskey.cp, tmp);
450 strlcat(keybuf, tmp, sizeof(keybuf));
452 if ((keyname = unkbd(thiskey.key)) != NULL)
453 strlcat(keybuf, keyname, sizeof(keybuf));
455 tmp[0] = thiskey.key;
456 strlcat(keybuf, tmp, sizeof(keybuf));
460 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
461 if (k->meta == thiskey.meta &&
462 k->key == thiskey.key) {
464 current_map = &k->map;
466 current_map = base_map;
467 strlcpy(keybuf, "", sizeof(keybuf));
468 k->fn(current_buffer());
474 if (current_map->unhandled_input != NULL)
475 current_map->unhandled_input();
477 global_key_unbound();
479 strlcpy(keybuf, "", sizeof(keybuf));
480 current_map = base_map;
486 redraw_tab(current_tab());
490 handle_clear_minibuf(int fd, short ev, void *d)
492 free(ministate.curmesg);
493 ministate.curmesg = NULL;
506 handle_resize(int sig, short ev, void *d)
508 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
509 event_del(&resizeev);
511 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
512 evtimer_add(&resizeev, &resize_timer);
516 handle_resize_nodelay(int s, short ev, void *d)
524 /* move and resize the windows, in reverse order! */
526 mvwin(minibuf, LINES-1, 0);
527 wresize(minibuf, 1, COLS);
529 mvwin(modeline, LINES-2, 0);
530 wresize(modeline, 1, COLS);
532 body_lines = LINES-3;
536 help_cols = 0.3 * COLS;
537 help_lines = LINES-3;
539 wresize(help, help_lines, help_cols);
541 wrap_page(&helpwin, help_cols);
543 body_cols = COLS - help_cols - 1;
544 mvwin(body, 1, help_cols);
549 wresize(body, body_lines, body_cols);
551 wresize(tabline, 1, COLS);
555 wrap_page(&tab->buffer, body_cols);
560 wrap_page(struct buffer *buffer, int width)
563 const struct line *top_orig, *orig;
568 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
569 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
571 buffer->top_line = NULL;
572 buffer->current_line = NULL;
574 buffer->force_redraw = 1;
576 buffer->line_off = 0;
580 TAILQ_FOREACH(l, &buffer->page.head, lines) {
581 prfx = line_prefixes[l->type].prfx1;
592 wrap_text(buffer, prfx, l, MIN(fill_column, width));
594 case LINE_PRE_CONTENT:
596 pre_width = MIN(fill_column, width);
599 hardwrap_text(buffer, l, pre_width);
603 if (top_orig == l && buffer->top_line == NULL) {
604 buffer->line_off = buffer->line_max-1;
605 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
608 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
609 if (vl == NULL || vl->parent != orig)
611 buffer->top_line = vl;
616 if (orig == l && buffer->current_line == NULL) {
617 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
620 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
621 if (vl == NULL || vl->parent != orig)
623 buffer->current_line = vl;
628 if (buffer->current_line == NULL)
629 buffer->current_line = TAILQ_FIRST(&buffer->head);
631 if (buffer->top_line == NULL)
632 buffer->top_line = buffer->current_line;
638 print_vline(int off, int width, WINDOW *window, struct vline *vl)
645 f = &line_faces[vl->parent->type];
647 /* unused, set by getyx */
651 prfx = line_prefixes[vl->parent->type].prfx1;
653 prfx = line_prefixes[vl->parent->type].prfx2;
659 wattr_on(window, body_face.left, NULL);
660 for (i = 0; i < off; i++)
662 wattr_off(window, body_face.left, NULL);
664 wattr_on(window, f->prefix, NULL);
665 wprintw(window, "%s", prfx);
666 wattr_off(window, f->prefix, NULL);
668 wattr_on(window, f->text, NULL);
669 wprintw(window, "%s", text);
670 wattr_off(window, f->text, NULL);
676 wattr_on(window, f->trail, NULL);
677 for (i = 0; i < left - off; ++i)
679 wattr_off(window, f->trail, NULL);
681 wattr_on(window, body_face.right, NULL);
682 for (i = 0; i < off; i++)
684 wattr_off(window, body_face.right, NULL);
692 size_t toskip, ots, tabwidth, space, x;
693 int current, y, truncated;
699 /* unused, but setted by a getyx */
702 tabwidth = sizeof(buf)+1;
706 TAILQ_FOREACH(tab, &tabshead, tabs) {
708 if (tab->flags & TAB_CURRENT)
712 if (toskip * tabwidth < space)
717 while (toskip != 0 &&
718 (ots - toskip+1) * tabwidth < space)
723 wattr_on(tabline, tab_face.background, NULL);
724 wprintw(tabline, toskip == 0 ? " " : "<");
725 wattr_off(tabline, tab_face.background, NULL);
728 TAILQ_FOREACH(tab, &tabshead, tabs) {
736 getyx(tabline, y, x);
737 if (x + sizeof(buf)+2 >= (size_t)COLS)
740 current = tab->flags & TAB_CURRENT;
742 if (*(title = tab->buffer.page.title) == '\0')
743 title = tab->hist_cur->h;
745 if (tab->flags & TAB_URGENT)
746 strlcpy(buf, "!", sizeof(buf));
748 strlcpy(buf, " ", sizeof(buf));
750 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
751 /* truncation happens */
752 strlcpy(&buf[sizeof(buf)-4], "...", 4);
754 /* pad with spaces */
755 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
760 wattr_on(tabline, tab_face.current, NULL);
762 wattr_on(tabline, tab_face.tab, NULL);
764 wprintw(tabline, "%s", buf);
765 if (TAILQ_NEXT(tab, tabs) != NULL)
766 wprintw(tabline, " ");
769 wattr_off(tabline, tab_face.current, NULL);
771 wattr_off(tabline, tab_face.tab, NULL);
774 wattr_on(tabline, tab_face.background, NULL);
775 for (; x < (size_t)COLS; ++x)
776 waddch(tabline, ' ');
778 mvwprintw(tabline, 0, COLS-1, ">");
779 wattr_off(tabline, tab_face.background, NULL);
783 * Compute the first visible line around vl. Try to search forward
784 * until the end of the buffer; if a visible line is not found, search
785 * backward. Return NULL if no viable line was found.
787 static inline struct vline *
788 adjust_line(struct vline *vl, struct buffer *buffer)
792 if (!(vl->parent->flags & L_HIDDEN))
797 t != NULL && t->parent->flags & L_HIDDEN;
798 t = TAILQ_NEXT(t, vlines))
804 /* search backward */
806 t != NULL && t->parent->flags & L_HIDDEN;
807 t = TAILQ_PREV(t, vhead, vlines))
814 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
819 restore_curs_x(buffer);
822 * TODO: ignoring buffer->force_update and always
823 * re-rendering. In theory we can recompute the y position
824 * without a re-render, and optimize here. It's not the only
825 * optimisation possible here, wscrl wolud also be an
833 if (TAILQ_EMPTY(&buffer->head))
836 buffer->top_line = adjust_line(buffer->top_line, buffer);
837 if (buffer->top_line == NULL)
840 buffer->current_line = adjust_line(buffer->current_line, buffer);
844 for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
845 if (vl->parent->flags & L_HIDDEN)
849 print_vline(x_offset, width, win, vl);
851 if (vl == buffer->current_line)
863 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
864 if (vl == buffer->current_line)
866 if (vl->parent->flags & L_HIDDEN)
869 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
875 buffer->last_line_off = buffer->line_off;
876 buffer->force_redraw = 0;
878 wmove(win, buffer->curs_y, buffer->curs_x);
884 redraw_window(help, help_lines, help_cols, &helpwin);
888 redraw_body(struct tab *tab)
890 static struct tab *last_tab;
893 tab->buffer.force_redraw =1;
896 redraw_window(body, body_lines, body_cols, &tab->buffer);
900 trust_status_char(enum trust_state ts)
903 case TS_UNKNOWN: return 'u';
904 case TS_UNTRUSTED: return '!';
905 case TS_TEMP_TRUSTED: return '!';
906 case TS_TRUSTED: return 'v';
907 case TS_VERIFIED: return 'V';
913 redraw_modeline(struct tab *tab)
916 int x, y, max_x, max_y;
917 const char *mode = tab->buffer.page.name;
918 const char *spin = "-\\|/";
921 wattr_on(modeline, modeline_face.background, NULL);
922 wmove(modeline, 0, 0);
924 wprintw(modeline, "-%c%c %s ",
925 spin[tab->loading_anim_step],
926 trust_status_char(tab->trust),
927 mode == NULL ? "(none)" : mode);
929 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
931 if (tab->buffer.line_max <= (size_t)body_lines)
932 wprintw(modeline, "All ");
933 else if (tab->buffer.line_off == 0)
934 wprintw(modeline, "Top ");
935 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
936 wprintw(modeline, "Bottom ");
938 wprintw(modeline, "%.0f%% ", pct);
940 wprintw(modeline, "%d/%d %s ",
941 tab->buffer.line_off + tab->buffer.curs_y,
942 tab->buffer.line_max,
945 getyx(modeline, y, x);
946 getmaxyx(modeline, max_y, max_x);
951 for (; x < max_x; ++x)
952 waddstr(modeline, "-");
954 wattr_off(modeline, modeline_face.background, NULL);
958 redraw_minibuffer(void)
961 size_t off_y, off_x = 0;
962 char *start = NULL, *c = NULL;
964 /* unused, but set by getyx */
967 wattr_on(minibuf, minibuffer_face.background, NULL);
971 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
972 if (ministate.hist_cur != NULL)
973 wprintw(minibuf, "(%zu/%zu) ",
974 ministate.hist_off + 1,
975 ministate.history->len);
977 getyx(minibuf, off_y, off_x);
979 start = ministate.hist_cur != NULL
980 ? ministate.hist_cur->h
982 c = utf8_nth(ministate.buffer.current_line->line,
983 ministate.buffer.cpoff);
984 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
985 start = utf8_next_cp(start);
988 waddstr(minibuf, start);
991 if (ministate.curmesg != NULL)
992 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
995 if (!in_minibuffer && ministate.curmesg == NULL)
996 waddstr(minibuf, keybuf);
998 /* If nothing else, show the URL at point */
999 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1000 tab = current_tab();
1001 if (tab->buffer.current_line != NULL &&
1002 tab->buffer.current_line->parent->type == LINE_LINK)
1003 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1007 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1009 wattr_off(minibuf, minibuffer_face.background, NULL);
1013 redraw_tab(struct tab *tab)
1022 redraw_modeline(tab);
1023 redraw_minibuffer();
1025 wnoutrefresh(tabline);
1026 wnoutrefresh(modeline);
1028 if (in_minibuffer) {
1030 wnoutrefresh(minibuf);
1032 wnoutrefresh(minibuf);
1040 emit_help_item(char *prfx, void *fn)
1045 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1049 assert(cmd != NULL);
1051 if ((l = calloc(1, sizeof(*l))) == NULL)
1054 l->type = LINE_TEXT;
1057 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1059 if (TAILQ_EMPTY(&helpwin.page.head))
1060 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1062 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1066 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1072 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1073 strlcpy(p, prfx, sizeof(p));
1075 strlcat(p, " ", sizeof(p));
1077 strlcat(p, "M-", sizeof(p));
1078 if ((kn = unkbd(k->key)) != NULL)
1079 strlcat(p, kn, sizeof(p));
1081 strlcat(p, keyname(k->key), sizeof(p));
1084 rec_compute_help(&k->map, p, sizeof(p));
1086 emit_help_item(p, k->fn);
1091 recompute_help(void)
1095 empty_vlist(&helpwin);
1096 empty_linelist(&helpwin);
1097 rec_compute_help(current_map, p, sizeof(p));
1098 wrap_page(&helpwin, help_cols);
1102 vmessage(const char *fmt, va_list ap)
1104 if (evtimer_pending(&clminibufev, NULL))
1105 evtimer_del(&clminibufev);
1107 free(ministate.curmesg);
1108 ministate.curmesg = NULL;
1111 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1112 evtimer_add(&clminibufev, &clminibufev_timer);
1114 /* TODO: what to do if the allocation fails here? */
1115 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1116 ministate.curmesg = NULL;
1119 redraw_minibuffer();
1120 if (in_minibuffer) {
1130 message(const char *fmt, ...)
1140 start_loading_anim(struct tab *tab)
1142 if (tab->loading_anim)
1144 tab->loading_anim = 1;
1145 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1146 evtimer_add(&tab->loadingev, &loadingev_timer);
1150 update_loading_anim(int fd, short ev, void *d)
1152 struct tab *tab = d;
1154 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1156 if (tab->flags & TAB_CURRENT) {
1157 redraw_modeline(tab);
1164 evtimer_add(&tab->loadingev, &loadingev_timer);
1168 stop_loading_anim(struct tab *tab)
1170 if (!tab->loading_anim)
1172 evtimer_del(&tab->loadingev);
1173 tab->loading_anim = 0;
1174 tab->loading_anim_step = 0;
1176 if (!(tab->flags & TAB_CURRENT))
1179 redraw_modeline(tab);
1188 load_url_in_tab(struct tab *tab, const char *url)
1190 message("Loading %s...", url);
1191 start_loading_anim(tab);
1194 tab->buffer.curs_x = 0;
1195 tab->buffer.curs_y = 0;
1200 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1201 void (*abortfn)(void), struct histhead *hist)
1204 base_map = &minibuffer_map;
1205 current_map = &minibuffer_map;
1207 base_map->unhandled_input = self_insert_fn;
1209 ministate.donefn = donefn;
1210 ministate.abortfn = abortfn;
1211 memset(ministate.buf, 0, sizeof(ministate.buf));
1212 ministate.buffer.current_line = &ministate.vline;
1213 ministate.buffer.current_line->line = ministate.buf;
1214 ministate.buffer.cpoff = 0;
1215 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1217 ministate.history = hist;
1218 ministate.hist_cur = NULL;
1219 ministate.hist_off = 0;
1223 exit_minibuffer(void)
1228 base_map = &global_map;
1229 current_map = &global_map;
1233 switch_to_tab(struct tab *tab)
1237 TAILQ_FOREACH(t, &tabshead, tabs) {
1238 t->flags &= ~TAB_CURRENT;
1241 tab->flags |= TAB_CURRENT;
1242 tab->flags &= ~TAB_URGENT;
1248 return tab_counter++;
1252 new_tab(const char *url)
1256 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1262 TAILQ_INIT(&tab->hist.head);
1264 TAILQ_INIT(&tab->buffer.head);
1266 tab->id = tab_new_id();
1269 if (TAILQ_EMPTY(&tabshead))
1270 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1272 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1274 load_url_in_tab(tab, url);
1281 setlocale(LC_ALL, "");
1283 TAILQ_INIT(&eecmd_history.head);
1284 TAILQ_INIT(&ir_history.head);
1285 TAILQ_INIT(&lu_history.head);
1287 ministate.line.type = LINE_TEXT;
1288 ministate.vline.parent = &ministate.line;
1289 ministate.buffer.current_line = &ministate.vline;
1291 /* initialize help window */
1292 TAILQ_INIT(&helpwin.head);
1294 base_map = &global_map;
1295 current_map = &global_map;
1299 if (enable_colors) {
1302 use_default_colors();
1307 config_apply_style();
1312 intrflush(stdscr, FALSE);
1314 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1316 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1318 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1320 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1322 if ((help = newwin(1, 1, 1, 0)) == NULL)
1325 body_lines = LINES-3;
1328 wbkgd(body, body_face.body);
1329 wbkgd(minibuf, minibuffer_face.background);
1334 scrollok(body, FALSE);
1336 /* non-blocking input */
1339 mvwprintw(body, 0, 0, "");
1341 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1342 event_add(&stdioev, NULL);
1344 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1345 signal_add(&winchev, NULL);
1351 ui_on_tab_loaded(struct tab *tab)
1353 stop_loading_anim(tab);
1354 message("Loaded %s", tab->hist_cur->h);
1365 ui_on_tab_refresh(struct tab *tab)
1367 wrap_page(&tab->buffer, body_cols);
1368 if (tab->flags & TAB_CURRENT)
1371 tab->flags |= TAB_URGENT;
1381 ui_toggle_side_window(void)
1383 side_window = !side_window;
1388 * ugly hack, but otherwise the window doesn't get updated
1389 * until I call handle_resize a second time (i.e. C-l). I
1390 * will be happy to know why something like this is needed.
1392 handle_resize_nodelay(0, 0, NULL);
1393 handle_resize_nodelay(0, 0, NULL);
1397 ui_schedule_redraw(void)
1399 handle_resize_nodelay(0, 0, NULL);
1403 ui_require_input(struct tab *tab, int hide)
1405 /* TODO: hard-switching to another tab is ugly */
1408 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1410 strlcpy(ministate.prompt, "Input required: ",
1411 sizeof(ministate.prompt));
1416 ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1421 if (in_minibuffer) {
1428 enter_minibuffer(yornp_self_insert, yornp_self_insert,
1431 len = sizeof(ministate.prompt);
1432 strlcpy(ministate.prompt, prompt, len);
1433 strlcat(ministate.prompt, " (y or n) ", len);
1434 redraw_tab(current_tab());
1438 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1448 enter_minibuffer(read_self_insert, read_select, read_abort,
1451 len = sizeof(ministate.prompt);
1452 strlcpy(ministate.prompt, prompt, len);
1453 strlcat(ministate.prompt, ": ", len);
1454 redraw_tab(current_tab());