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))
77 struct minibuf_histhead;
79 static struct event stdioev, winchev;
81 static int kbd(const char*);
82 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
83 static void load_default_keys(void);
84 static int push_line(struct tab*, const struct line*, const char*, size_t);
85 static void empty_vlist(struct tab*);
86 static void restore_cursor(struct tab *);
88 static void cmd_previous_line(struct tab*);
89 static void cmd_next_line(struct tab*);
90 static void cmd_forward_char(struct tab*);
91 static void cmd_backward_char(struct tab*);
92 static void cmd_move_beginning_of_line(struct tab*);
93 static void cmd_move_end_of_line(struct tab*);
94 static void cmd_redraw(struct tab*);
95 static void cmd_scroll_line_down(struct tab*);
96 static void cmd_scroll_line_up(struct tab*);
97 static void cmd_scroll_up(struct tab*);
98 static void cmd_scroll_down(struct tab*);
99 static void cmd_beginning_of_buffer(struct tab*);
100 static void cmd_end_of_buffer(struct tab*);
101 static void cmd_kill_telescope(struct tab*);
102 static void cmd_push_button(struct tab*);
103 static void cmd_clear_minibuf(struct tab*);
104 static void cmd_execute_extended_command(struct tab*);
105 static void cmd_tab_close(struct tab*);
106 static void cmd_tab_new(struct tab*);
107 static void cmd_tab_next(struct tab*);
108 static void cmd_tab_previous(struct tab*);
109 static void cmd_load_url(struct tab*);
110 static void cmd_load_current_url(struct tab*);
112 static void global_key_unbound(void);
114 static void cmd_mini_delete_char(struct tab*);
115 static void cmd_mini_delete_backward_char(struct tab*);
116 static void cmd_mini_forward_char(struct tab*);
117 static void cmd_mini_backward_char(struct tab*);
118 static void cmd_mini_move_end_of_line(struct tab*);
119 static void cmd_mini_move_beginning_of_line(struct tab*);
120 static void cmd_mini_kill_line(struct tab*);
121 static void cmd_mini_abort(struct tab*);
122 static void cmd_mini_complete_and_exit(struct tab*);
123 static void cmd_mini_previous_history_element(struct tab*);
124 static void cmd_mini_next_history_element(struct tab*);
126 static void minibuffer_hist_save_entry(void);
127 static void minibuffer_taint_hist(void);
128 static void minibuffer_self_insert(void);
129 static void eecmd_self_insert(void);
130 static void eecmd_select(void);
131 static void ir_self_insert(void);
132 static void ir_select(void);
133 static void lu_self_insert(void);
134 static void lu_select(void);
136 static struct line *nth_line(struct tab*, size_t);
137 static struct tab *current_tab(void);
138 static void dispatch_stdio(int, short, void*);
139 static void handle_clear_minibuf(int, short, void*);
140 static void handle_resize(int, short, void*);
141 static int word_bourdaries(const char*, const char*, const char**, const char**);
142 static void wrap_text(struct tab*, const char*, struct line*);
143 static int hardwrap_text(struct tab*, struct line*);
144 static int wrap_page(struct tab*);
145 static void print_line(struct line*);
146 static void redraw_tabline(void);
147 static void redraw_body(struct tab*);
148 static void redraw_modeline(struct tab*);
149 static void redraw_minibuffer(void);
150 static void redraw_tab(struct tab*);
151 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
152 static void start_loading_anim(struct tab*);
153 static void update_loading_anim(int, short, void*);
154 static void stop_loading_anim(struct tab*);
155 static void load_url_in_tab(struct tab*, const char*);
156 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct minibuf_histhead*);
157 static void exit_minibuffer(void);
158 static void switch_to_tab(struct tab*);
159 static void new_tab(void);
161 static struct { int meta, key; } thiskey;
163 static WINDOW *tabline, *body, *modeline, *minibuf;
164 static int body_lines, body_cols;
166 static struct event clminibufev;
167 static int clminibufev_set;
168 static struct timeval clminibufev_timer = { 5, 0 };
169 static struct timeval loadingev_timer = { 0, 250000 };
171 static uint32_t tab_counter;
180 short loading_anim_step;
181 struct event loadingev;
183 TAILQ_HEAD(, line) head;
186 static char keybuf[64];
188 #define CTRL(n) ((n)&0x1F)
195 { "<down>", KEY_DOWN },
196 { "<left>", KEY_LEFT },
197 { "<right>", KEY_RIGHT },
198 { "<prior>", KEY_PPAGE },
199 { "<next>", KEY_NPAGE },
200 { "<home>", KEY_HOME },
201 { "<end>", KEY_END },
203 { "del", KEY_BACKSPACE },
207 { "enter", CTRL('m') },
208 { "ret", CTRL('m' )},
209 { "tab", CTRL('i') },
215 TAILQ_HEAD(map, keymap) m;
216 void (*unhandled_input)(void);
219 struct kmap global_map,
228 void (*fn)(struct tab*);
230 TAILQ_ENTRY(keymap) keymaps;
233 /* TODO: limit to a maximum number of entries */
234 struct minibuf_histhead {
235 TAILQ_HEAD(mhisthead, minibuf_hist) head;
238 struct minibuf_hist {
240 TAILQ_ENTRY(minibuf_hist) entries;
243 static struct minibuf_histhead eecmd_history,
247 static int in_minibuffer;
255 void (*donefn)(void);
256 void (*abortfn)(void);
258 struct minibuf_histhead *history;
259 struct minibuf_hist *hist_cur;
266 } line_prefixes[] = {
267 [LINE_TEXT] = { "", "" },
268 [LINE_LINK] = { "=> ", " " },
269 [LINE_TITLE_1] = { "# ", " " },
270 [LINE_TITLE_2] = { "## ", " " },
271 [LINE_TITLE_3] = { "### ", " " },
272 [LINE_ITEM] = { "* ", " " },
273 [LINE_QUOTE] = { "> ", "> " },
274 [LINE_PRE_START] = { "```", "```" },
275 [LINE_PRE_CONTENT] = { "", "" },
276 [LINE_PRE_END] = { "```", "```" },
283 [LINE_LINK] = { A_UNDERLINE },
284 [LINE_TITLE_1] = { A_BOLD },
285 [LINE_TITLE_2] = { A_BOLD },
286 [LINE_TITLE_3] = { A_BOLD },
288 [LINE_QUOTE] = { A_DIM },
289 [LINE_PRE_START] = { 0 },
290 [LINE_PRE_CONTENT] = { 0 },
291 [LINE_PRE_END] = { 0 },
299 for (t = keytable; t->p != NULL; ++t) {
300 if (has_prefix(key, t->p))
312 for (t = keytable; t->p != NULL; ++t) {
321 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
324 struct keymap *entry;
327 if ((ctrl = has_prefix(key, "C-")))
329 if ((meta = has_prefix(key, "M-")))
338 /* skip key & spaces */
339 while (*key != '\0' && !isspace(*key))
341 while (*key != '\0' && isspace(*key))
344 TAILQ_FOREACH(entry, &map->m, keymaps) {
345 if (entry->meta == meta && entry->key == k) {
355 if ((entry = calloc(1, sizeof(*entry))) == NULL)
360 TAILQ_INIT(&entry->map.m);
362 if (TAILQ_EMPTY(&map->m))
363 TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
365 TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
376 global_set_key(const char *key, void (*fn)(struct tab*))
378 kmap_define_key(&global_map, key, fn);
382 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
384 kmap_define_key(&minibuffer_map, key, fn);
388 load_default_keys(void)
390 /* === global map === */
393 global_set_key("C-p", cmd_previous_line);
394 global_set_key("C-n", cmd_next_line);
395 global_set_key("C-f", cmd_forward_char);
396 global_set_key("C-b", cmd_backward_char);
397 global_set_key("C-a", cmd_move_beginning_of_line);
398 global_set_key("C-e", cmd_move_end_of_line);
400 global_set_key("M-v", cmd_scroll_up);
401 global_set_key("C-v", cmd_scroll_down);
403 global_set_key("C-x C-c", cmd_kill_telescope);
405 global_set_key("C-g", cmd_clear_minibuf);
407 global_set_key("M-x", cmd_execute_extended_command);
408 global_set_key("C-x C-f", cmd_load_url);
409 global_set_key("C-x M-f", cmd_load_current_url);
411 global_set_key("C-x t 0", cmd_tab_close);
412 global_set_key("C-x t 2", cmd_tab_new);
413 global_set_key("C-x t o", cmd_tab_next);
414 global_set_key("C-x t O", cmd_tab_previous);
416 global_set_key("M-<", cmd_beginning_of_buffer);
417 global_set_key("M->", cmd_end_of_buffer);
420 global_set_key("k", cmd_previous_line);
421 global_set_key("j", cmd_next_line);
422 global_set_key("l", cmd_forward_char);
423 global_set_key("h", cmd_backward_char);
424 global_set_key("^", cmd_move_beginning_of_line);
425 global_set_key("$", cmd_move_end_of_line);
427 global_set_key("K", cmd_scroll_line_up);
428 global_set_key("J", cmd_scroll_line_down);
430 global_set_key("g g", cmd_beginning_of_buffer);
431 global_set_key("G", cmd_end_of_buffer);
434 global_set_key("q", cmd_kill_telescope);
436 global_set_key("esc", cmd_clear_minibuf);
438 global_set_key(":", cmd_execute_extended_command);
441 global_set_key("<up>", cmd_previous_line);
442 global_set_key("<down>", cmd_next_line);
443 global_set_key("<right>", cmd_forward_char);
444 global_set_key("<left>", cmd_backward_char);
445 global_set_key("<prior>", cmd_scroll_up);
446 global_set_key("<next>", cmd_scroll_down);
448 /* "ncurses standard" */
449 global_set_key("C-l", cmd_redraw);
452 global_set_key("C-m", cmd_push_button);
454 /* === minibuffer map === */
455 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
456 minibuffer_set_key("C-g", cmd_mini_abort);
457 minibuffer_set_key("esc", cmd_mini_abort);
458 minibuffer_set_key("C-d", cmd_mini_delete_char);
459 minibuffer_set_key("del", cmd_mini_delete_backward_char);
461 minibuffer_set_key("C-f", cmd_mini_forward_char);
462 minibuffer_set_key("C-b", cmd_mini_backward_char);
463 minibuffer_set_key("<right>", cmd_mini_forward_char);
464 minibuffer_set_key("<left>", cmd_mini_backward_char);
465 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
466 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
467 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
468 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
469 minibuffer_set_key("C-k", cmd_mini_kill_line);
471 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
472 minibuffer_set_key("M-n", cmd_mini_next_history_element);
473 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
474 minibuffer_set_key("<down>", cmd_mini_next_history_element);
478 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
484 if ((vl = calloc(1, sizeof(*vl))) == NULL)
487 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
494 memcpy(vl->line, buf, len);
497 if (TAILQ_EMPTY(&tab->s->head))
498 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
500 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
505 empty_vlist(struct tab *tab)
509 tab->s->line_max = 0;
511 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
512 TAILQ_REMOVE(&tab->s->head, l, lines);
514 /* l->alt references the original line! */
520 restore_cursor(struct tab *tab)
522 wmove(body, tab->s->curs_y, tab->s->curs_x);
526 cmd_previous_line(struct tab *tab)
528 if (--tab->s->curs_y < 0) {
530 cmd_scroll_line_up(tab);
537 cmd_next_line(struct tab *tab)
539 if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
542 if (++tab->s->curs_y > body_lines-1) {
543 tab->s->curs_y = body_lines-1;
544 cmd_scroll_line_down(tab);
551 cmd_forward_char(struct tab *tab)
553 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
558 cmd_backward_char(struct tab *tab)
560 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
565 cmd_move_beginning_of_line(struct tab *tab)
572 cmd_move_end_of_line(struct tab *tab)
578 off = tab->s->line_off + tab->s->curs_y;
579 if (off >= tab->s->line_max) {
584 line = nth_line(tab, off);
585 if (line->line != NULL)
586 tab->s->curs_x = strlen(line->line);
590 prfx = line_prefixes[line->type].prfx1;
591 tab->s->curs_x += strlen(prfx);
598 cmd_redraw(struct tab *tab)
600 handle_resize(0, 0, NULL);
604 cmd_scroll_line_up(struct tab *tab)
608 if (tab->s->line_off == 0)
611 l = nth_line(tab, --tab->s->line_off);
618 cmd_scroll_line_down(struct tab *tab)
623 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
629 if (tab->s->line_max - tab->s->line_off < body_lines)
632 l = nth_line(tab, tab->s->line_off + body_lines-1);
633 wmove(body, body_lines-1, 0);
638 cmd_scroll_up(struct tab *tab)
644 for (; off > 0; --off)
645 cmd_scroll_line_up(tab);
649 cmd_scroll_down(struct tab *tab)
653 off = tab->s->line_off + body_lines;
654 off = MIN(tab->s->line_max, off);
656 for (; off >= 0; --off)
657 cmd_scroll_line_down(tab);
661 cmd_beginning_of_buffer(struct tab *tab)
663 tab->s->line_off = 0;
669 cmd_end_of_buffer(struct tab *tab)
673 off = tab->s->line_max - body_lines;
676 tab->s->line_off = off;
677 tab->s->curs_y = MIN(body_lines, tab->s->line_max);
683 cmd_kill_telescope(struct tab *tab)
689 cmd_push_button(struct tab *tab)
694 nth = tab->s->line_off + tab->s->curs_y;
695 if (nth > tab->s->line_max)
697 l = nth_line(tab, nth);
698 if (l->type != LINE_LINK)
701 load_url_in_tab(tab, l->alt);
705 cmd_clear_minibuf(struct tab *tab)
707 handle_clear_minibuf(0, 0, NULL);
711 cmd_execute_extended_command(struct tab *tab)
715 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
718 len = sizeof(ministate.prompt);
719 strlcpy(ministate.prompt, "", len);
722 strlcat(ministate.prompt, "M-", len);
724 strlcat(ministate.prompt, keyname(thiskey.key), len);
725 strlcat(ministate.prompt, " ", len);
729 cmd_tab_close(struct tab *tab)
733 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
734 TAILQ_NEXT(tab, tabs) == NULL) {
735 message("Can't close the only tab.");
741 t = TAILQ_PREV(tab, tabshead, tabs);
742 t->flags |= TAB_CURRENT;
744 TAILQ_REMOVE(&tabshead, tab, tabs);
751 cmd_tab_new(struct tab *tab)
757 cmd_tab_next(struct tab *tab)
761 tab->flags &= ~TAB_CURRENT;
763 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
764 t = TAILQ_FIRST(&tabshead);
765 t->flags |= TAB_CURRENT;
769 cmd_tab_previous(struct tab *tab)
773 tab->flags &= ~TAB_CURRENT;
775 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
776 t = TAILQ_LAST(&tabshead, tabshead);
777 t->flags |= TAB_CURRENT;
781 cmd_load_url(struct tab *tab)
783 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
785 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
789 cmd_load_current_url(struct tab *tab)
791 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
793 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
794 strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
795 ministate.off = strlen(tab->urlstr);
796 ministate.len = ministate.off;
800 global_key_unbound(void)
802 message("%s is undefined", keybuf);
806 cmd_mini_delete_char(struct tab *tab)
808 if (ministate.len == 0 || ministate.off == ministate.len)
811 memmove(&ministate.buf[ministate.off],
812 &ministate.buf[ministate.off+1],
813 ministate.len - ministate.off + 1);
818 cmd_mini_delete_backward_char(struct tab *tab)
820 if (ministate.len == 0 || ministate.off == 0)
823 memmove(&ministate.buf[ministate.off-1],
824 &ministate.buf[ministate.off],
825 ministate.len - ministate.off + 1);
831 cmd_mini_forward_char(struct tab *tab)
833 if (ministate.off == ministate.len)
839 cmd_mini_backward_char(struct tab *tab)
841 if (ministate.off == 0)
847 cmd_mini_move_end_of_line(struct tab *tab)
849 ministate.off = ministate.len;
853 cmd_mini_move_beginning_of_line(struct tab *tab)
859 cmd_mini_kill_line(struct tab *tab)
861 if (ministate.off == ministate.len)
863 ministate.buf[ministate.off] = '\0';
864 ministate.len -= ministate.off;
868 cmd_mini_abort(struct tab *tab)
874 cmd_mini_complete_and_exit(struct tab *tab)
876 minibuffer_taint_hist();
881 cmd_mini_previous_history_element(struct tab *tab)
883 if (ministate.history == NULL) {
884 message("No history");
888 if (ministate.hist_cur == NULL ||
889 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
890 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
891 ministate.hist_off = ministate.history->len - 1;
892 if (ministate.hist_cur == NULL)
893 message("No prev item");
895 ministate.hist_off--;
898 if (ministate.hist_cur != NULL) {
900 ministate.len = strlen(ministate.hist_cur->h);
905 cmd_mini_next_history_element(struct tab *tab)
907 if (ministate.history == NULL) {
908 message("No history");
912 if (ministate.hist_cur == NULL ||
913 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
914 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
915 ministate.hist_off = 0;
916 if (ministate.hist_cur == NULL)
917 message("No next item");
919 ministate.hist_off++;
922 if (ministate.hist_cur != NULL) {
924 ministate.len = strlen(ministate.hist_cur->h);
929 minibuffer_hist_save_entry(void)
931 struct minibuf_hist *hist;
933 if (ministate.history == NULL)
936 if ((hist = calloc(1, sizeof(*hist))) == NULL)
939 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
941 if (TAILQ_EMPTY(&ministate.history->head))
942 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
944 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
945 ministate.history->len++;
949 * taint the minibuffer cache: if we're currently showing a history
950 * element, copy that to the current buf and reset the "history
954 minibuffer_taint_hist(void)
956 if (ministate.hist_cur == NULL)
959 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
960 ministate.hist_cur = NULL;
964 minibuffer_self_insert(void)
966 minibuffer_taint_hist();
968 if (ministate.len == sizeof(ministate.buf) -1)
971 /* TODO: utf8 handling! */
973 memmove(&ministate.buf[ministate.off+1],
974 &ministate.buf[ministate.off],
975 ministate.len - ministate.off + 1);
976 ministate.buf[ministate.off] = thiskey.key;
982 eecmd_self_insert(void)
984 if (thiskey.meta || isspace(thiskey.key) ||
985 !isgraph(thiskey.key)) {
986 global_key_unbound();
990 minibuffer_self_insert();
997 minibuffer_hist_save_entry();
998 message("TODO: try to execute %s", ministate.buf);
1002 ir_self_insert(void)
1004 minibuffer_self_insert();
1010 char buf[1025] = {0};
1014 tab = current_tab();
1017 minibuffer_hist_save_entry();
1019 /* a bit ugly but... */
1020 memcpy(&url, &tab->url, sizeof(tab->url));
1021 url_set_query(&url, ministate.buf);
1022 url_unparse(&url, buf, sizeof(buf));
1023 load_url_in_tab(tab, buf);
1027 lu_self_insert(void)
1029 if (thiskey.meta || isspace(thiskey.key) ||
1030 !isgraph(thiskey.key)) {
1031 global_key_unbound();
1035 minibuffer_self_insert();
1042 minibuffer_hist_save_entry();
1043 load_url_in_tab(current_tab(), ministate.buf);
1046 static struct line *
1047 nth_line(struct tab *tab, size_t n)
1053 TAILQ_FOREACH(l, &tab->s->head, lines) {
1068 TAILQ_FOREACH(t, &tabshead, tabs) {
1069 if (t->flags & TAB_CURRENT)
1078 dispatch_stdio(int fd, short ev, void *d)
1082 const char *keyname;
1085 thiskey.key = wgetch(body);
1086 if (thiskey.key == ERR)
1088 if (thiskey.key == 27) {
1089 /* TODO: make escape-time customizable */
1092 thiskey.key = wgetch(body);
1093 if (thiskey.key == ERR || thiskey.key == 27) {
1100 if (keybuf[0] != '\0')
1101 strlcat(keybuf, " ", sizeof(keybuf));
1103 strlcat(keybuf, "M-", sizeof(keybuf));
1104 if ((keyname = unkbd(thiskey.key)) != NULL)
1105 strlcat(keybuf, keyname, sizeof(keybuf));
1107 tmp[0] = thiskey.key;
1108 strlcat(keybuf, tmp, sizeof(keybuf));
1111 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1112 if (k->meta == thiskey.meta &&
1113 k->key == thiskey.key) {
1115 current_map = &k->map;
1117 current_map = base_map;
1118 strlcpy(keybuf, "", sizeof(keybuf));
1119 k->fn(current_tab());
1125 if (current_map->unhandled_input != NULL)
1126 current_map->unhandled_input();
1128 global_key_unbound();
1131 strlcpy(keybuf, "", sizeof(keybuf));
1132 current_map = base_map;
1135 redraw_tab(current_tab());
1139 handle_clear_minibuf(int fd, short ev, void *d)
1141 clminibufev_set = 0;
1143 free(ministate.curmesg);
1144 ministate.curmesg = NULL;
1146 redraw_minibuffer();
1147 if (in_minibuffer) {
1157 handle_resize(int sig, short ev, void *d)
1165 /* move and resize the windows, in reverse order! */
1167 mvwin(minibuf, LINES-1, 0);
1168 wresize(minibuf, 1, COLS);
1170 mvwin(modeline, LINES-2, 0);
1171 wresize(modeline, 1, COLS);
1173 wresize(body, LINES-3, COLS);
1174 body_lines = LINES-3;
1177 wresize(tabline, 1, COLS);
1179 tab = current_tab();
1186 * Helper function for wrap_text. Find the end of the current word
1187 * and the end of the separator after the word.
1190 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1198 /* find the end of the current world */
1199 for (; *s != '\0'; ++s) {
1200 if (strchr(sep, *s) != NULL)
1206 /* find the end of the separator */
1207 for (; *s != '\0'; ++s) {
1208 if (strchr(sep, *s) == NULL)
1218 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1221 if (!push_line(tab, l, *line, *off - zero))
1223 *line += *off - zero;
1229 emitstr(const char **s, size_t len, size_t *off)
1233 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1234 for (i = 0; i < len; ++i)
1241 * Build a list of visual line by wrapping the given line, assuming
1242 * that when printed will have a leading prefix prfx.
1244 * TODO: it considers each byte one cell on the screen!
1247 wrap_text(struct tab *tab, const char *prfx, struct line *l)
1249 size_t zero, off, len, split;
1250 const char *endword, *endspc, *line, *linestart;
1252 zero = strlen(prfx);
1255 linestart = l->line;
1257 while (word_boundaries(line, " \t-", &endword, &endspc)) {
1258 len = endword - line;
1259 if (off + len >= body_cols) {
1260 emitline(tab, zero, &off, l, &linestart);
1261 while (len >= body_cols) {
1263 emitline(tab, zero, &off, l, &linestart);
1265 line += body_cols-1;
1273 /* print the spaces iff not at bol */
1274 len = endspc - endword;
1275 /* line = endspc; */
1277 if (off + len >= body_cols) {
1278 emitline(tab, zero, &off, l, &linestart);
1287 emitline(tab, zero, &off, l, &linestart);
1291 hardwrap_text(struct tab *tab, struct line *l)
1294 const char *linestart;
1296 if (l->line == NULL)
1297 return emitline(tab, 0, &off, l, &linestart);
1299 len = strlen(l->line);
1301 linestart = l->line;
1303 while (len >= COLS) {
1306 if (!emitline(tab, 0, &off, l, &linestart))
1311 return emitline(tab, 0, &len, l, &linestart);
1317 wrap_page(struct tab *tab)
1324 TAILQ_FOREACH(l, &tab->page.head, lines) {
1325 prfx = line_prefixes[l->type].prfx1;
1334 wrap_text(tab, prfx, l);
1336 case LINE_PRE_START:
1338 push_line(tab, l, NULL, 0);
1340 case LINE_PRE_CONTENT:
1341 hardwrap_text(tab, l);
1349 print_line(struct line *l)
1351 const char *text = l->line;
1352 const char *prfx = line_prefixes[l->type].prfx1;
1353 int face = line_faces[l->type].prop;
1359 wattron(body, face);
1360 wprintw(body, "%s%s", prfx, text);
1362 wattroff(body, face);
1366 redraw_tabline(void)
1372 wbkgd(tabline, A_REVERSE);
1374 wprintw(tabline, " ");
1375 TAILQ_FOREACH(tab, &tabshead, tabs) {
1376 current = tab->flags & TAB_CURRENT;
1379 wattron(tabline, A_UNDERLINE);
1381 wprintw(tabline, "%s%d:todo title ",
1382 current ? "*" : " ", tab->id);
1385 wattroff(tabline, A_UNDERLINE);
1390 redraw_modeline(struct tab *tab)
1393 int x, y, max_x, max_y;
1394 const char *mode = tab->page.name;
1395 const char *spin = "-\\|/";
1398 wattron(modeline, A_REVERSE);
1399 wmove(modeline, 0, 0);
1401 wprintw(modeline, "-%c %s-mode ",
1402 spin[tab->s->loading_anim_step], mode);
1404 pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1406 if (tab->s->line_max <= body_lines)
1407 wprintw(modeline, "All ");
1408 else if (tab->s->line_off == 0)
1409 wprintw(modeline, "Top ");
1410 else if (tab->s->line_off + body_lines >= tab->s->line_max)
1411 wprintw(modeline, "Bottom ");
1413 wprintw(modeline, "%.0f%% ", pct);
1415 wprintw(modeline, "%d/%d %s ",
1416 tab->s->line_off + tab->s->curs_y,
1420 getyx(modeline, y, x);
1421 getmaxyx(modeline, max_y, max_x);
1426 for (; x < max_x; ++x)
1427 waddstr(modeline, "-");
1431 redraw_minibuffer(void)
1433 size_t skip = 0, off_x = 0, off_y = 0;
1436 if (in_minibuffer) {
1437 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1438 if (ministate.hist_cur != NULL)
1439 wprintw(minibuf, "(%zu/%zu) ",
1440 ministate.hist_off + 1,
1441 ministate.history->len);
1443 getyx(minibuf, off_y, off_x);
1445 while (ministate.off - skip > COLS / 2) {
1446 skip += MIN(ministate.off/4, 1);
1449 if (ministate.hist_cur != NULL)
1450 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1452 wprintw(minibuf, "%s", ministate.buf + skip);
1455 if (ministate.curmesg != NULL) {
1457 wprintw(minibuf, " [%s]", ministate.curmesg);
1459 wprintw(minibuf, "%s", ministate.curmesg);
1462 wmove(minibuf, 0, off_x + ministate.off - skip);
1466 redraw_tab(struct tab *tab)
1470 redraw_modeline(tab);
1471 redraw_minibuffer();
1473 restore_cursor(tab);
1477 if (in_minibuffer) {
1487 redraw_body(struct tab *tab)
1494 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1495 if (TAILQ_EMPTY(&tab->s->head))
1499 l = nth_line(tab, tab->s->line_off);
1500 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1501 wmove(body, line, 0);
1504 if (line == body_lines)
1510 message(const char *fmt, ...)
1514 if (clminibufev_set)
1515 evtimer_del(&clminibufev);
1516 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1517 evtimer_add(&clminibufev, &clminibufev_timer);
1518 clminibufev_set = 1;
1521 /* TODO: what to do if the allocation fails here? */
1522 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1523 ministate.curmesg = NULL;
1526 redraw_minibuffer();
1528 if (in_minibuffer) {
1538 start_loading_anim(struct tab *tab)
1540 if (tab->s->loading_anim)
1542 tab->s->loading_anim = 1;
1543 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1544 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1548 update_loading_anim(int fd, short ev, void *d)
1550 struct tab *tab = d;
1552 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1554 redraw_modeline(tab);
1561 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1565 stop_loading_anim(struct tab *tab)
1567 if (!tab->s->loading_anim)
1569 evtimer_del(&tab->s->loadingev);
1570 tab->s->loading_anim = 0;
1571 tab->s->loading_anim_step = 0;
1573 redraw_modeline(tab);
1582 load_url_in_tab(struct tab *tab, const char *url)
1585 message("Loading %s...", url);
1586 start_loading_anim(tab);
1595 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1596 void (*abortfn)(void), struct minibuf_histhead *hist)
1599 base_map = &minibuffer_map;
1600 current_map = &minibuffer_map;
1602 base_map->unhandled_input = self_insert_fn;
1604 ministate.donefn = donefn;
1605 ministate.abortfn = abortfn;
1606 memset(ministate.buf, 0, sizeof(ministate.buf));
1609 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1611 ministate.history = hist;
1612 ministate.hist_cur = NULL;
1613 ministate.hist_off = 0;
1617 exit_minibuffer(void)
1622 base_map = &global_map;
1623 current_map = &global_map;
1627 switch_to_tab(struct tab *tab)
1631 TAILQ_FOREACH(t, &tabshead, tabs) {
1632 t->flags &= ~TAB_CURRENT;
1635 tab->flags |= TAB_CURRENT;
1641 struct tab *tab, *t;
1642 const char *url = "about:new";
1644 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1647 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1650 TAILQ_INIT(&tab->s->head);
1652 tab->id = tab_counter++;
1655 if (TAILQ_EMPTY(&tabshead))
1656 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1658 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1660 load_url_in_tab(tab, url);
1670 setlocale(LC_ALL, "");
1672 TAILQ_INIT(&global_map.m);
1673 global_map.unhandled_input = global_key_unbound;
1675 TAILQ_INIT(&minibuffer_map.m);
1677 TAILQ_INIT(&eecmd_history.head);
1678 TAILQ_INIT(&ir_history.head);
1679 TAILQ_INIT(&lu_history.head);
1681 base_map = &global_map;
1682 current_map = &global_map;
1683 load_default_keys();
1690 intrflush(stdscr, FALSE);
1692 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1694 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1696 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1698 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1701 body_lines = LINES-3;
1705 scrollok(body, TRUE);
1707 /* non-blocking input */
1710 mvwprintw(body, 0, 0, "");
1712 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1713 event_add(&stdioev, NULL);
1715 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1716 signal_add(&winchev, NULL);
1724 ui_on_tab_loaded(struct tab *tab)
1726 stop_loading_anim(tab);
1727 message("Loaded %s", tab->urlstr);
1731 ui_on_tab_refresh(struct tab *tab)
1733 if (!(tab->flags & TAB_CURRENT))
1741 ui_require_input(struct tab *tab, int hide)
1743 /* TODO: hard-switching to another tab is ugly */
1746 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1748 strlcpy(ministate.prompt, "Input required: ",
1749 sizeof(ministate.prompt));