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.
24 * There's a simple text wrapping algorithm.
26 * 1. if it's a line in a pre-formatted block:
29 * 2. there is enough room for the next word?
31 * b. no: break the current line.
32 * i. while there isn't enough space to draw the current
34 * ii. draw the remainder of the current word (can be the
36 * 3. render the spaces after the word
37 * a. but if there is not enough room break the line and
45 * ncurses allows you to scroll a window, but when a line goes out of
46 * the visible area it's forgotten. We keep a list of formatted lines
47 * (``visual lines'') that we know fits in the window, and draw them.
48 * This way is easy to scroll: just call wscrl and then render the
51 * This means that on every resize we have to clear our list of lines
52 * and re-render everything. A clever approach would be to do this
55 * TODO: make the text formatting on-demand.
59 #include <telescope.h>
71 #define TAB_CURRENT 0x1
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
78 static struct event stdioev, winchev;
80 static int kbd(const char*);
81 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
82 static void load_default_keys(void);
83 static int push_line(struct tab*, const struct line*, const char*, size_t);
84 static void empty_vlist(struct tab*);
85 static void restore_cursor(struct tab *);
87 static void cmd_previous_line(struct tab*);
88 static void cmd_next_line(struct tab*);
89 static void cmd_forward_char(struct tab*);
90 static void cmd_backward_char(struct tab*);
91 static void cmd_redraw(struct tab*);
92 static void cmd_scroll_line_down(struct tab*);
93 static void cmd_scroll_line_up(struct tab*);
94 static void cmd_scroll_up(struct tab*);
95 static void cmd_scroll_down(struct tab*);
96 static void cmd_kill_telescope(struct tab*);
97 static void cmd_push_button(struct tab*);
98 static void cmd_execute_extended_command(struct tab*);
99 static void cmd_tab_close(struct tab*);
100 static void cmd_tab_new(struct tab*);
101 static void cmd_tab_next(struct tab*);
102 static void cmd_tab_previous(struct tab*);
104 static void global_key_unbound(void);
106 static void cmd_mini_del(struct tab*);
107 static void cmd_mini_forward_char(struct tab*);
108 static void cmd_mini_backward_char(struct tab*);
109 static void cmd_mini_move_end_of_line(struct tab*);
110 static void cmd_mini_move_beginning_of_line(struct tab*);
111 static void cmd_mini_kill_line(struct tab*);
112 static void cmd_mini_abort();
113 static void cmd_mini_complete_and_exit();
115 static void eecmd_self_insert(void);
116 static void eecmd_select(void);
118 static struct line *nth_line(struct tab*, size_t);
119 static struct tab *current_tab(void);
120 static void dispatch_stdio(int, short, void*);
121 static void handle_clear_minibuf(int, short, void*);
122 static void handle_resize(int, short, void*);
123 static int word_bourdaries(const char*, const char*, const char**, const char**);
124 static void wrap_text(struct tab*, const char*, struct line*);
125 static int hardwrap_text(struct tab*, struct line*);
126 static int wrap_page(struct tab*);
127 static void print_line(struct line*);
128 static void redraw_tabline(void);
129 static void redraw_modeline(struct tab*);
130 static void redraw_minibuffer(void);
131 static void redraw_tab(struct tab*);
132 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
133 static void start_loading_anim(struct tab*);
134 static void update_loading_anim(int, short, void*);
135 static void stop_loading_anim(struct tab*);
136 static void load_url_in_tab(struct tab*, const char*);
137 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
138 static void exit_minibuffer(void);
139 static void new_tab(void);
141 static struct { int meta, key; } thiskey;
143 static WINDOW *tabline, *body, *modeline, *minibuf;
144 static int body_lines, body_cols;
146 static struct event clminibufev;
147 static int clminibufev_set;
148 static struct timeval clminibufev_timer = { 5, 0 };
149 static struct timeval loadingev_timer = { 0, 250000 };
151 static uint32_t tab_counter;
160 short loading_anim_step;
161 struct event loadingev;
163 TAILQ_HEAD(, line) head;
166 static char keybuf[64];
168 #define CTRL(n) ((n)&0x1F)
175 { "<down>", KEY_DOWN },
176 { "<left>", KEY_LEFT },
177 { "<right>", KEY_RIGHT },
178 { "<prior>", KEY_PPAGE },
179 { "<next>", KEY_NPAGE },
180 { "<home>", KEY_HOME },
181 { "<end>", KEY_END },
183 { "del", KEY_BACKSPACE },
187 { "enter", CTRL('m') },
188 { "ret", CTRL('m' )},
189 { "tab", CTRL('i') },
195 TAILQ_HEAD(map, keymap) m;
196 void (*unhandled_input)(void);
199 struct kmap global_map,
208 void (*fn)(struct tab*);
210 TAILQ_ENTRY(keymap) keymaps;
213 static int in_minibuffer;
221 void (*donefn)(void);
222 void (*abortfn)(void);
230 for (t = keytable; t->p != NULL; ++t) {
231 if (has_prefix(key, t->p))
243 for (t = keytable; t->p != NULL; ++t) {
252 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
255 struct keymap *entry;
258 if ((ctrl = has_prefix(key, "C-")))
260 if ((meta = has_prefix(key, "M-")))
269 /* skip key & spaces */
270 while (*key != '\0' && !isspace(*key))
272 while (*key != '\0' && isspace(*key))
275 TAILQ_FOREACH(entry, &map->m, keymaps) {
276 if (entry->meta == meta && entry->key == k) {
286 if ((entry = calloc(1, sizeof(*entry))) == NULL)
291 TAILQ_INIT(&entry->map.m);
293 if (TAILQ_EMPTY(&map->m))
294 TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
296 TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
307 global_set_key(const char *key, void (*fn)(struct tab*))
309 kmap_define_key(&global_map, key, fn);
313 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
315 kmap_define_key(&minibuffer_map, key, fn);
319 load_default_keys(void)
321 /* === global map === */
324 global_set_key("C-p", cmd_previous_line);
325 global_set_key("C-n", cmd_next_line);
326 global_set_key("C-f", cmd_forward_char);
327 global_set_key("C-b", cmd_backward_char);
329 global_set_key("M-v", cmd_scroll_up);
330 global_set_key("C-v", cmd_scroll_down);
332 global_set_key("C-x C-c", cmd_kill_telescope);
334 global_set_key("M-x", cmd_execute_extended_command);
336 global_set_key("C-x t 0", cmd_tab_close);
337 global_set_key("C-x t 2", cmd_tab_new);
338 global_set_key("C-x t o", cmd_tab_next);
339 global_set_key("C-x t O", cmd_tab_previous);
342 global_set_key("k", cmd_previous_line);
343 global_set_key("j", cmd_next_line);
344 global_set_key("l", cmd_forward_char);
345 global_set_key("h", cmd_backward_char);
347 global_set_key("K", cmd_scroll_line_up);
348 global_set_key("J", cmd_scroll_line_down);
351 global_set_key("q", cmd_kill_telescope);
353 global_set_key(":", cmd_execute_extended_command);
356 global_set_key("<up>", cmd_previous_line);
357 global_set_key("<down>", cmd_next_line);
358 global_set_key("<right>", cmd_forward_char);
359 global_set_key("<left>", cmd_backward_char);
360 global_set_key("<prior>", cmd_scroll_up);
361 global_set_key("<next>", cmd_scroll_down);
363 /* "ncurses standard" */
364 global_set_key("C-l", cmd_redraw);
367 global_set_key("C-m", cmd_push_button);
369 /* === minibuffer map === */
370 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
371 minibuffer_set_key("C-g", cmd_mini_abort);
372 minibuffer_set_key("esc", cmd_mini_abort);
373 minibuffer_set_key("del", cmd_mini_del);
375 minibuffer_set_key("C-f", cmd_mini_forward_char);
376 minibuffer_set_key("C-b", cmd_mini_backward_char);
377 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
378 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
379 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
380 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
381 minibuffer_set_key("C-k", cmd_mini_kill_line);
385 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
391 if ((vl = calloc(1, sizeof(*vl))) == NULL)
394 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
401 memcpy(vl->line, buf, len);
404 if (TAILQ_EMPTY(&tab->s->head))
405 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
407 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
412 empty_vlist(struct tab *tab)
416 tab->s->line_max = 0;
418 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
419 TAILQ_REMOVE(&tab->s->head, l, lines);
421 /* l->alt references the original line! */
427 restore_cursor(struct tab *tab)
429 wmove(body, tab->s->curs_y, tab->s->curs_x);
433 cmd_previous_line(struct tab *tab)
435 if (--tab->s->curs_y < 0) {
437 cmd_scroll_line_up(tab);
444 cmd_next_line(struct tab *tab)
446 if (++tab->s->curs_y > body_lines-1) {
447 tab->s->curs_y = body_lines-1;
448 cmd_scroll_line_down(tab);
455 cmd_forward_char(struct tab *tab)
457 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
462 cmd_backward_char(struct tab *tab)
464 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
469 cmd_redraw(struct tab *tab)
471 handle_resize(0, 0, NULL);
475 cmd_scroll_line_up(struct tab *tab)
479 if (tab->s->line_off == 0)
482 l = nth_line(tab, --tab->s->line_off);
489 cmd_scroll_line_down(struct tab *tab)
494 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
500 if (tab->s->line_max - tab->s->line_off < body_lines)
503 l = nth_line(tab, tab->s->line_off + body_lines-1);
504 wmove(body, body_lines-1, 0);
509 cmd_scroll_up(struct tab *tab)
515 for (; off > 0; --off)
516 cmd_scroll_line_up(tab);
520 cmd_scroll_down(struct tab *tab)
524 off = tab->s->line_off + body_lines;
525 off = MIN(tab->s->line_max, off);
527 for (; off >= 0; --off)
528 cmd_scroll_line_down(tab);
532 cmd_kill_telescope(struct tab *tab)
538 cmd_push_button(struct tab *tab)
543 nth = tab->s->line_off + tab->s->curs_y;
544 if (nth > tab->s->line_max)
546 l = nth_line(tab, nth);
547 if (l->type != LINE_LINK)
550 load_url_in_tab(tab, l->alt);
554 cmd_execute_extended_command(struct tab *tab)
558 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
560 len = sizeof(ministate.prompt);
561 strlcpy(ministate.prompt, "", len);
564 strlcat(ministate.prompt, "M-", len);
566 strlcat(ministate.prompt, keyname(thiskey.key), len);
567 strlcat(ministate.prompt, " ", len);
571 cmd_tab_close(struct tab *tab)
575 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
576 TAILQ_NEXT(tab, tabs) == NULL) {
577 message("Can't close the only tab.");
583 t = TAILQ_PREV(tab, tabshead, tabs);
584 t->flags |= TAB_CURRENT;
586 TAILQ_REMOVE(&tabshead, tab, tabs);
593 cmd_tab_new(struct tab *tab)
599 cmd_tab_next(struct tab *tab)
603 tab->flags &= ~TAB_CURRENT;
605 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
606 t = TAILQ_FIRST(&tabshead);
607 t->flags |= TAB_CURRENT;
611 cmd_tab_previous(struct tab *tab)
615 tab->flags &= ~TAB_CURRENT;
617 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
618 t = TAILQ_LAST(&tabshead, tabshead);
619 t->flags |= TAB_CURRENT;
623 global_key_unbound(void)
625 message("%s is undefined", keybuf);
629 cmd_mini_del(struct tab *tab)
631 if (ministate.len == 0 || ministate.off == 0)
634 memmove(&ministate.buf[ministate.off-1],
635 &ministate.buf[ministate.off],
636 ministate.len - ministate.off + 1);
642 cmd_mini_forward_char(struct tab *tab)
644 if (ministate.off == ministate.len)
650 cmd_mini_backward_char(struct tab *tab)
652 if (ministate.off == 0)
658 cmd_mini_move_end_of_line(struct tab *tab)
660 ministate.off = ministate.len;
664 cmd_mini_move_beginning_of_line(struct tab *tab)
670 cmd_mini_kill_line(struct tab *tab)
672 if (ministate.off == ministate.len)
674 ministate.buf[ministate.off] = '\0';
675 ministate.len -= ministate.off;
679 cmd_mini_abort(struct tab *tab)
685 cmd_mini_complete_and_exit(struct tab *tab)
691 eecmd_self_insert(void)
693 if (thiskey.meta || isspace(thiskey.key) ||
694 !isgraph(thiskey.key)) {
695 global_key_unbound();
699 if (ministate.len == sizeof(ministate.buf) -1)
702 /* TODO: utf8 handling! */
704 memmove(&ministate.buf[ministate.off+1],
705 &ministate.buf[ministate.off],
706 ministate.len - ministate.off + 1);
707 ministate.buf[ministate.off] = thiskey.key;
716 message("TODO: try to execute %s", ministate.buf);
720 nth_line(struct tab *tab, size_t n)
726 TAILQ_FOREACH(l, &tab->s->head, lines) {
741 TAILQ_FOREACH(t, &tabshead, tabs) {
742 if (t->flags & TAB_CURRENT)
751 dispatch_stdio(int fd, short ev, void *d)
757 thiskey.key = wgetch(body);
758 if (thiskey.key == ERR)
760 if (thiskey.key == 27) {
761 /* TODO: make escape-time customizable */
764 thiskey.key = wgetch(body);
765 if (thiskey.key == ERR)
770 if (keybuf[0] != '\0')
771 strlcat(keybuf, " ", sizeof(keybuf));
773 strlcat(keybuf, "M-", sizeof(keybuf));
774 if ((keyname = unkbd(thiskey.key)) != NULL)
775 strlcat(keybuf, keyname, sizeof(keybuf));
777 tmp[0] = thiskey.key;
778 strlcat(keybuf, tmp, sizeof(keybuf));
781 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
782 if (k->meta == thiskey.meta &&
783 k->key == thiskey.key) {
785 current_map = &k->map;
787 current_map = base_map;
788 strlcpy(keybuf, "", sizeof(keybuf));
789 k->fn(current_tab());
795 if (current_map->unhandled_input != NULL)
796 current_map->unhandled_input();
798 global_key_unbound();
801 strlcpy(keybuf, "", sizeof(keybuf));
802 current_map = base_map;
807 restore_cursor(current_tab());
821 handle_clear_minibuf(int fd, short ev, void *d)
825 free(ministate.curmesg);
826 ministate.curmesg = NULL;
839 handle_resize(int sig, short ev, void *d)
847 /* move and resize the windows, in reverse order! */
849 mvwin(minibuf, LINES-1, 0);
850 wresize(minibuf, 1, COLS);
852 mvwin(modeline, LINES-2, 0);
853 wresize(modeline, 1, COLS);
855 wresize(body, LINES-3, COLS);
856 body_lines = LINES-3;
859 wresize(tabline, 1, COLS);
868 * Helper function for wrap_text. Find the end of the current word
869 * and the end of the separator after the word.
872 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
880 /* find the end of the current world */
881 for (; *s != '\0'; ++s) {
882 if (strchr(sep, *s) != NULL)
888 /* find the end of the separator */
889 for (; *s != '\0'; ++s) {
890 if (strchr(sep, *s) == NULL)
900 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
903 if (!push_line(tab, l, *line, *off - zero))
905 *line += *off - zero;
911 emitstr(const char **s, size_t len, size_t *off)
915 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
916 for (i = 0; i < len; ++i)
923 * Build a list of visual line by wrapping the given line, assuming
924 * that when printed will have a leading prefix prfx.
926 * TODO: it considers each byte one cell on the screen!
929 wrap_text(struct tab *tab, const char *prfx, struct line *l)
931 size_t zero, off, len, split;
932 const char *endword, *endspc, *line, *linestart;
939 while (word_boundaries(line, " \t-", &endword, &endspc)) {
940 len = endword - line;
941 if (off + len >= body_cols) {
942 emitline(tab, zero, &off, l, &linestart);
943 while (len >= body_cols) {
945 emitline(tab, zero, &off, l, &linestart);
955 /* print the spaces iff not at bol */
956 len = endspc - endword;
959 if (off + len >= body_cols) {
960 emitline(tab, zero, &off, l, &linestart);
969 emitline(tab, zero, &off, l, &linestart);
973 hardwrap_text(struct tab *tab, struct line *l)
976 const char *linestart;
978 len = strlen(l->line);
982 while (len >= COLS) {
985 if (!emitline(tab, 0, &off, l, &linestart))
990 return emitline(tab, 0, &len, l, &linestart);
996 wrap_page(struct tab *tab)
1002 TAILQ_FOREACH(l, &tab->page.head, lines) {
1005 wrap_text(tab, "", l);
1008 wrap_text(tab, "=> ", l);
1011 wrap_text(tab, "# ", l);
1014 wrap_text(tab, "## ", l);
1017 wrap_text(tab, "### ", l);
1020 wrap_text(tab, "* ", l);
1023 wrap_text(tab, "> ", l);
1025 case LINE_PRE_START:
1027 push_line(tab, l, NULL, 0);
1029 case LINE_PRE_CONTENT:
1030 hardwrap_text(tab, l);
1038 print_line(struct line *l)
1040 const char *text = l->line;
1047 wprintw(body, "%s", text);
1050 wattron(body, A_UNDERLINE);
1051 wprintw(body, "=> %s", text);
1052 wattroff(body, A_UNDERLINE);
1055 wattron(body, A_BOLD);
1056 wprintw(body, "# %s", text);
1057 wattroff(body, A_BOLD);
1060 wattron(body, A_BOLD);
1061 wprintw(body, "## %s", text);
1062 wattroff(body, A_BOLD);
1065 wattron(body, A_BOLD);
1066 wprintw(body, "### %s", text);
1067 wattroff(body, A_BOLD);
1070 wprintw(body, "* %s", text);
1073 wattron(body, A_DIM);
1074 wprintw(body, "> %s", text);
1075 wattroff(body, A_DIM);
1077 case LINE_PRE_START:
1079 wprintw(body, "```");
1081 case LINE_PRE_CONTENT:
1082 wprintw(body, "%s", text);
1088 redraw_tabline(void)
1094 wbkgd(tabline, A_REVERSE);
1096 wprintw(tabline, " ");
1097 TAILQ_FOREACH(tab, &tabshead, tabs) {
1098 current = tab->flags & TAB_CURRENT;
1100 wattroff(tabline, A_REVERSE);
1101 wattron(tabline, A_STANDOUT);
1104 wprintw(tabline, " %d:todo title ",
1108 wattron(tabline, A_REVERSE);
1109 wattroff(tabline, A_STANDOUT);
1115 redraw_modeline(struct tab *tab)
1117 int x, y, max_x, max_y;
1118 const char *mode = "text/gemini-mode";
1119 const char *spin = "-\\|/";
1122 wattron(modeline, A_REVERSE);
1123 wmove(modeline, 0, 0);
1125 wprintw(modeline, "-%c %s %s ",
1126 spin[tab->s->loading_anim_step], mode, tab->urlstr);
1127 getyx(modeline, y, x);
1128 getmaxyx(modeline, max_y, max_x);
1133 for (; x < max_x; ++x)
1134 waddstr(modeline, "-");
1138 redraw_minibuffer(void)
1140 size_t skip = 0, off = 0;
1146 off = strlen(ministate.prompt);
1148 while (ministate.off - skip > COLS / 2) {
1149 skip += MIN(ministate.off/4, 1);
1152 mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1153 ministate.buf + skip);
1156 if (ministate.curmesg != NULL) {
1158 wprintw(minibuf, " [%s]", ministate.curmesg);
1160 wprintw(minibuf, "%s", ministate.curmesg);
1163 wmove(minibuf, 0, off + ministate.off - skip);
1167 redraw_tab(struct tab *tab)
1174 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1175 if (TAILQ_EMPTY(&tab->s->head))
1179 l = nth_line(tab, tab->s->line_off);
1180 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1181 wmove(body, line, 0);
1184 if (line == body_lines)
1189 redraw_modeline(tab);
1190 redraw_minibuffer();
1192 restore_cursor(tab);
1196 if (in_minibuffer) {
1206 message(const char *fmt, ...)
1210 if (clminibufev_set)
1211 evtimer_del(&clminibufev);
1212 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1213 evtimer_add(&clminibufev, &clminibufev_timer);
1214 clminibufev_set = 1;
1217 /* TODO: what to do if the allocation fails here? */
1218 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1219 ministate.curmesg = NULL;
1222 redraw_minibuffer();
1224 if (in_minibuffer) {
1234 start_loading_anim(struct tab *tab)
1236 if (tab->s->loading_anim)
1238 tab->s->loading_anim = 1;
1239 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1240 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1244 update_loading_anim(int fd, short ev, void *d)
1246 struct tab *tab = d;
1248 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1250 redraw_modeline(tab);
1257 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1261 stop_loading_anim(struct tab *tab)
1263 if (!tab->s->loading_anim)
1265 evtimer_del(&tab->s->loadingev);
1266 tab->s->loading_anim = 0;
1267 tab->s->loading_anim_step = 0;
1269 redraw_modeline(tab);
1278 load_url_in_tab(struct tab *tab, const char *url)
1281 message("Loading %s...", url);
1282 start_loading_anim(tab);
1291 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1292 void (*abortfn)(void))
1295 base_map = &minibuffer_map;
1296 current_map = &minibuffer_map;
1298 base_map->unhandled_input = self_insert_fn;
1300 ministate.donefn = donefn;
1301 ministate.abortfn = abortfn;
1302 memset(ministate.buf, 0, sizeof(ministate.buf));
1304 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1308 exit_minibuffer(void)
1313 base_map = &global_map;
1314 current_map = &global_map;
1320 struct tab *tab, *t;
1321 const char *url = "about:new";
1323 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1326 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1329 TAILQ_INIT(&tab->s->head);
1330 TAILQ_FOREACH(t, &tabshead, tabs) {
1331 t->flags &= ~TAB_CURRENT;
1334 tab->id = tab_counter++;
1335 tab->flags = TAB_CURRENT;
1337 if (TAILQ_EMPTY(&tabshead))
1338 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1340 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1342 load_url_in_tab(tab, url);
1352 setlocale(LC_ALL, "");
1354 TAILQ_INIT(&global_map.m);
1355 global_map.unhandled_input = global_key_unbound;
1357 TAILQ_INIT(&minibuffer_map.m);
1359 base_map = &global_map;
1360 current_map = &global_map;
1361 load_default_keys();
1368 intrflush(stdscr, FALSE);
1370 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1372 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1374 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1376 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1379 body_lines = LINES-3;
1383 scrollok(body, TRUE);
1385 /* non-blocking input */
1388 mvwprintw(body, 0, 0, "");
1390 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1391 event_add(&stdioev, NULL);
1393 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1394 signal_add(&winchev, NULL);
1402 ui_on_tab_loaded(struct tab *tab)
1404 stop_loading_anim(tab);
1405 message("Loaded %s", tab->urlstr);
1409 ui_on_tab_refresh(struct tab *tab)
1411 if (!(tab->flags & TAB_CURRENT))