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.
26 * This way is easy to scroll: just call wscrl and then render the
29 * This means that on every resize we have to clear our list of lines
30 * and re-render everything. A clever approach would be to do this
31 * ``on-demand'', but it's still missing.
35 #include "telescope.h"
49 #define TAB_CURRENT 0x1
50 #define TAB_URGENT 0x2
52 #define NEW_TAB_URL "about:new"
54 static struct event stdioev, winchev;
56 static void load_default_keys(void);
57 static void restore_cursor(struct buffer*);
59 static void global_key_unbound(void);
60 static void minibuffer_hist_save_entry(void);
61 static void minibuffer_taint_hist(void);
62 static void minibuffer_self_insert(void);
63 static void eecmd_self_insert(void);
64 static void eecmd_select(void);
65 static void ir_self_insert(void);
66 static void ir_select(void);
67 static void lu_self_insert(void);
68 static void lu_select(void);
69 static void bp_select(void);
70 static void yornp_self_insert(void);
71 static void yornp_abort(void);
72 static void read_self_insert(void);
73 static void read_abort(void);
74 static void read_select(void);
76 static struct vline *nth_line(struct buffer*, size_t);
77 static struct tab *current_tab(void);
78 static struct buffer *current_buffer(void);
79 static int readkey(void);
80 static void dispatch_stdio(int, short, void*);
81 static void handle_clear_minibuf(int, short, void*);
82 static void handle_resize(int, short, void*);
83 static void handle_resize_nodelay(int, short, void*);
84 static int wrap_page(struct buffer*, int);
85 static void print_vline(WINDOW*, struct vline*);
86 static void redraw_tabline(void);
87 static void redraw_window(WINDOW*, int, struct buffer*);
88 static void redraw_help(void);
89 static void redraw_body(struct tab*);
90 static void redraw_modeline(struct tab*);
91 static void redraw_minibuffer(void);
92 static void redraw_tab(struct tab*);
93 static void emit_help_item(char*, void*);
94 static void rec_compute_help(struct kmap*, char*, size_t);
95 static void recompute_help(void);
96 static void vmessage(const char*, va_list);
97 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
98 static void start_loading_anim(struct tab*);
99 static void update_loading_anim(int, short, void*);
100 static void stop_loading_anim(struct tab*);
101 static void load_url_in_tab(struct tab*, const char*);
102 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
103 static void exit_minibuffer(void);
104 static void switch_to_tab(struct tab*);
105 static struct tab *new_tab(const char*);
106 static void session_new_tab_cb(const char*);
107 static void usage(void);
111 static struct { short meta; int key; uint32_t cp; } thiskey;
113 static struct event resizeev;
114 static struct timeval resize_timer = { 0, 250000 };
116 static WINDOW *tabline, *body, *modeline, *minibuf;
117 static int body_lines, body_cols;
120 static struct buffer helpwin;
121 static int help_lines, help_cols;
123 static int side_window;
125 static struct event clminibufev;
126 static struct timeval clminibufev_timer = { 5, 0 };
127 static struct timeval loadingev_timer = { 0, 250000 };
129 static uint32_t tab_counter;
131 static char keybuf[64];
133 static void (*yornp_cb)(int, unsigned int);
134 static unsigned int yornp_data;
136 static void (*read_cb)(const char*, unsigned int);
137 static unsigned int read_data;
139 struct kmap global_map,
144 static struct histhead eecmd_history,
149 static int in_minibuffer;
155 void (*donefn)(void);
156 void (*abortfn)(void);
161 struct buffer buffer;
163 struct histhead *history;
164 struct hist *hist_cur;
171 if (olivetti_mode && fill_column < body_cols)
172 x_offset = (body_cols - fill_column)/2;
178 global_set_key(const char *key, void (*fn)(struct buffer*))
180 if (!kmap_define_key(&global_map, key, fn))
185 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
187 if (!kmap_define_key(&minibuffer_map, key, fn))
192 load_default_keys(void)
194 /* === global map === */
197 global_set_key("C-p", cmd_previous_line);
198 global_set_key("C-n", cmd_next_line);
199 global_set_key("C-f", cmd_forward_char);
200 global_set_key("C-b", cmd_backward_char);
201 global_set_key("M-{", cmd_backward_paragraph);
202 global_set_key("M-}", cmd_forward_paragraph);
203 global_set_key("C-a", cmd_move_beginning_of_line);
204 global_set_key("C-e", cmd_move_end_of_line);
206 global_set_key("M-v", cmd_scroll_up);
207 global_set_key("C-v", cmd_scroll_down);
208 global_set_key("M-space", cmd_scroll_up);
209 global_set_key("space", cmd_scroll_down);
211 global_set_key("M-<", cmd_beginning_of_buffer);
212 global_set_key("M->", cmd_end_of_buffer);
214 global_set_key("C-x C-c", cmd_kill_telescope);
216 global_set_key("C-g", cmd_clear_minibuf);
218 global_set_key("M-x", cmd_execute_extended_command);
219 global_set_key("C-x C-f", cmd_load_url);
220 global_set_key("C-x M-f", cmd_load_current_url);
222 global_set_key("C-x t 0", cmd_tab_close);
223 global_set_key("C-x t 1", cmd_tab_close_other);
224 global_set_key("C-x t 2", cmd_tab_new);
225 global_set_key("C-x t o", cmd_tab_next);
226 global_set_key("C-x t O", cmd_tab_previous);
227 global_set_key("C-x t m", cmd_tab_move);
228 global_set_key("C-x t M", cmd_tab_move_to);
230 global_set_key("C-M-b", cmd_previous_page);
231 global_set_key("C-M-f", cmd_next_page);
233 global_set_key("<f7> a", cmd_bookmark_page);
234 global_set_key("<f7> <f7>", cmd_list_bookmarks);
237 global_set_key("k", cmd_previous_line);
238 global_set_key("j", cmd_next_line);
239 global_set_key("l", cmd_forward_char);
240 global_set_key("h", cmd_backward_char);
241 global_set_key("{", cmd_backward_paragraph);
242 global_set_key("}", cmd_forward_paragraph);
243 global_set_key("^", cmd_move_beginning_of_line);
244 global_set_key("$", cmd_move_end_of_line);
246 global_set_key("K", cmd_scroll_line_up);
247 global_set_key("J", cmd_scroll_line_down);
249 global_set_key("g g", cmd_beginning_of_buffer);
250 global_set_key("G", cmd_end_of_buffer);
252 global_set_key("g D", cmd_tab_close);
253 global_set_key("g N", cmd_tab_new);
254 global_set_key("g t", cmd_tab_next);
255 global_set_key("g T", cmd_tab_previous);
256 global_set_key("g M-t", cmd_tab_move);
257 global_set_key("g M-T", cmd_tab_move_to);
259 global_set_key("H", cmd_previous_page);
260 global_set_key("L", cmd_next_page);
263 global_set_key("q", cmd_kill_telescope);
265 global_set_key("esc", cmd_clear_minibuf);
267 global_set_key(":", cmd_execute_extended_command);
270 global_set_key("<up>", cmd_previous_line);
271 global_set_key("<down>", cmd_next_line);
272 global_set_key("<right>", cmd_forward_char);
273 global_set_key("<left>", cmd_backward_char);
274 global_set_key("<prior>", cmd_scroll_up);
275 global_set_key("<next>", cmd_scroll_down);
277 global_set_key("M-<left>", cmd_previous_page);
278 global_set_key("M-<right>", cmd_next_page);
280 /* "ncurses standard" */
281 global_set_key("C-l", cmd_redraw);
284 global_set_key("<f1>", cmd_toggle_help);
285 global_set_key("C-m", cmd_push_button);
286 global_set_key("M-enter", cmd_push_button_new_tab);
287 global_set_key("M-tab", cmd_previous_button);
288 global_set_key("backtab", cmd_previous_button);
289 global_set_key("tab", cmd_next_button);
291 /* === minibuffer map === */
292 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
293 minibuffer_set_key("C-g", cmd_mini_abort);
294 minibuffer_set_key("esc", cmd_mini_abort);
295 minibuffer_set_key("C-d", cmd_mini_delete_char);
296 minibuffer_set_key("del", cmd_mini_delete_backward_char);
297 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
298 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
300 minibuffer_set_key("C-b", cmd_backward_char);
301 minibuffer_set_key("C-f", cmd_forward_char);
302 minibuffer_set_key("<left>", cmd_backward_char);
303 minibuffer_set_key("<right>", cmd_forward_char);
304 minibuffer_set_key("C-e", cmd_move_end_of_line);
305 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
306 minibuffer_set_key("<end>", cmd_move_end_of_line);
307 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
308 minibuffer_set_key("C-k", cmd_mini_kill_line);
310 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
311 minibuffer_set_key("M-n", cmd_mini_next_history_element);
312 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
313 minibuffer_set_key("<down>", cmd_mini_next_history_element);
317 restore_cursor(struct buffer *buffer)
322 vl = buffer->current_line;
323 if (vl == NULL || vl->line == NULL)
324 buffer->curs_x = buffer->cpoff = 0;
326 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
328 buffer->curs_x += x_offset;
331 prfx = line_prefixes[vl->parent->type].prfx1;
332 buffer->curs_x += utf8_swidth(prfx);
337 cmd_previous_line(struct buffer *buffer)
341 if (buffer->current_line == NULL
342 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
345 if (--buffer->curs_y < 0) {
347 cmd_scroll_line_up(buffer);
351 buffer->current_line = vl;
352 restore_cursor(buffer);
356 cmd_next_line(struct buffer *buffer)
360 if (buffer->current_line == NULL
361 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
364 if (++buffer->curs_y > body_lines-1) {
365 buffer->curs_y = body_lines-1;
366 cmd_scroll_line_down(buffer);
370 buffer->current_line = vl;
371 restore_cursor(buffer);
375 cmd_backward_char(struct buffer *buffer)
377 if (buffer->cpoff != 0)
379 restore_cursor(buffer);
383 cmd_forward_char(struct buffer *buffer)
387 if (buffer->current_line->line != NULL)
388 len = utf8_cplen(buffer->current_line->line);
389 if (++buffer->cpoff > len)
391 restore_cursor(buffer);
395 cmd_backward_paragraph(struct buffer *buffer)
398 if (buffer->current_line == NULL ||
399 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
400 message("No previous paragraph");
403 cmd_previous_line(buffer);
404 } while (buffer->current_line->line != NULL ||
405 buffer->current_line->parent->type != LINE_TEXT);
409 cmd_forward_paragraph(struct buffer *buffer)
412 if (buffer->current_line == NULL ||
413 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
414 message("No next paragraph");
417 cmd_next_line(buffer);
418 } while (buffer->current_line->line != NULL ||
419 buffer->current_line->parent->type != LINE_TEXT);
423 cmd_move_beginning_of_line(struct buffer *buffer)
426 restore_cursor(buffer);
430 cmd_move_end_of_line(struct buffer *buffer)
434 vl = buffer->current_line;
435 if (vl->line == NULL)
437 buffer->cpoff = utf8_cplen(vl->line);
438 restore_cursor(buffer);
442 cmd_redraw(struct buffer *buffer)
444 handle_resize(0, 0, NULL);
448 cmd_scroll_line_up(struct buffer *buffer)
452 if (buffer->line_off == 0)
455 vl = nth_line(buffer, --buffer->line_off);
458 print_vline(body, vl);
460 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
461 restore_cursor(buffer);
465 cmd_scroll_line_down(struct buffer *buffer)
469 vl = buffer->current_line;
470 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
472 buffer->current_line = vl;
477 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
480 vl = nth_line(buffer, buffer->line_off + body_lines-1);
481 wmove(body, body_lines-1, 0);
482 print_vline(body, vl);
484 restore_cursor(buffer);
488 cmd_scroll_up(struct buffer *buffer)
494 for (; off > 0; --off)
495 cmd_scroll_line_up(buffer);
499 cmd_scroll_down(struct buffer *buffer)
505 for (; off > 0; --off)
506 cmd_scroll_line_down(buffer);
510 cmd_beginning_of_buffer(struct buffer *buffer)
512 buffer->current_line = TAILQ_FIRST(&buffer->head);
513 buffer->line_off = 0;
516 restore_cursor(buffer);
520 cmd_end_of_buffer(struct buffer *buffer)
524 off = buffer->line_max - body_lines;
527 buffer->line_off = off;
528 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
530 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
531 buffer->cpoff = body_cols;
532 restore_cursor(buffer);
536 cmd_kill_telescope(struct buffer *buffer)
543 cmd_push_button(struct buffer *buffer)
548 nth = buffer->line_off + buffer->curs_y;
549 if (nth >= buffer->line_max)
551 vl = nth_line(buffer, nth);
552 if (vl->parent->type != LINE_LINK)
555 load_url_in_tab(current_tab(), vl->parent->alt);
559 cmd_push_button_new_tab(struct buffer *buffer)
564 nth = buffer->line_off + buffer->curs_y;
565 if (nth > buffer->line_max)
567 vl = nth_line(buffer, nth);
568 if (vl->parent->type != LINE_LINK)
571 new_tab(vl->parent->alt);
575 cmd_previous_button(struct buffer *buffer)
578 if (buffer->current_line == NULL ||
579 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
580 message("No previous link");
583 cmd_previous_line(buffer);
584 } while (buffer->current_line->parent->type != LINE_LINK);
588 cmd_next_button(struct buffer *buffer)
591 if (buffer->current_line == NULL ||
592 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
593 message("No next link");
596 cmd_next_line(buffer);
597 } while (buffer->current_line->parent->type != LINE_LINK);
601 cmd_previous_page(struct buffer *buffer)
603 struct tab *tab = current_tab();
605 if (!load_previous_page(tab))
606 message("No previous page");
608 start_loading_anim(tab);
612 cmd_next_page(struct buffer *buffer)
614 struct tab *tab = current_tab();
616 if (!load_next_page(tab))
617 message("No next page");
619 start_loading_anim(tab);
623 cmd_clear_minibuf(struct buffer *buffer)
625 handle_clear_minibuf(0, 0, NULL);
629 cmd_execute_extended_command(struct buffer *buffer)
634 message("We don't have enable-recursive-minibuffers");
638 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
641 len = sizeof(ministate.prompt);
642 strlcpy(ministate.prompt, "", len);
645 strlcat(ministate.prompt, "M-", len);
647 strlcat(ministate.prompt, keyname(thiskey.key), len);
650 strlcat(ministate.prompt, " ", len);
654 cmd_tab_close(struct buffer *buffer)
659 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
660 TAILQ_NEXT(tab, tabs) == NULL) {
661 message("Can't close the only tab.");
665 if (evtimer_pending(&tab->loadingev, NULL))
666 evtimer_del(&tab->loadingev);
670 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
671 t = TAILQ_NEXT(tab, tabs);
672 TAILQ_REMOVE(&tabshead, tab, tabs);
679 cmd_tab_close_other(struct buffer *buffer)
683 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
684 if (t->flags & TAB_CURRENT)
688 TAILQ_REMOVE(&tabshead, t, tabs);
694 cmd_tab_new(struct buffer *buffer)
698 if ((url = new_tab_url) == NULL)
705 cmd_tab_next(struct buffer *buffer)
710 tab->flags &= ~TAB_CURRENT;
712 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
713 t = TAILQ_FIRST(&tabshead);
714 t->flags |= TAB_CURRENT;
715 t->flags &= ~TAB_URGENT;
719 cmd_tab_previous(struct buffer *buffer)
724 tab->flags &= ~TAB_CURRENT;
726 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
727 t = TAILQ_LAST(&tabshead, tabshead);
728 t->flags |= TAB_CURRENT;
729 t->flags &= ~TAB_URGENT;
733 cmd_tab_move(struct buffer *buffer)
738 t = TAILQ_NEXT(tab, tabs);
739 TAILQ_REMOVE(&tabshead, tab, tabs);
742 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
744 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
748 cmd_tab_move_to(struct buffer *buffer)
753 t = TAILQ_PREV(tab, tabshead, tabs);
754 TAILQ_REMOVE(&tabshead, tab, tabs);
757 if (TAILQ_EMPTY(&tabshead))
758 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
760 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
762 TAILQ_INSERT_BEFORE(t, tab, tabs);
766 cmd_load_url(struct buffer *buffer)
769 message("We don't have enable-recursive-minibuffers");
773 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
775 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
776 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
777 cmd_move_end_of_line(&ministate.buffer);
781 cmd_load_current_url(struct buffer *buffer)
783 struct tab *tab = current_tab();
786 message("We don't have enable-recursive-minibuffers");
790 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
792 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
793 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
794 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
798 cmd_bookmark_page(struct buffer *buffer)
800 struct tab *tab = current_tab();
802 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
803 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
804 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
805 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
809 cmd_list_bookmarks(struct buffer *buffer)
811 load_url_in_tab(current_tab(), "about:bookmarks");
815 cmd_toggle_help(struct buffer *buffer)
817 side_window = !side_window;
822 * ugly hack, but otherwise the window doesn't get updated
823 * until I call handle_resize a second time (i.e. C-l). I
824 * will be happy to know why something like this is needed.
826 handle_resize_nodelay(0, 0, NULL);
827 handle_resize_nodelay(0, 0, NULL);
831 cmd_olivetti_mode(struct buffer *buffer)
833 olivetti_mode = !olivetti_mode;
835 message("olivetti-mode enabled");
837 message("olivetti-mode disabled");
839 handle_resize_nodelay(0, 0, NULL);
843 cmd_mini_delete_char(struct buffer *buffer)
847 if (!in_minibuffer) {
848 message("text is read-only");
852 minibuffer_taint_hist();
854 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
859 memmove(c, n, strlen(n)+1);
863 cmd_mini_delete_backward_char(struct buffer *buffer)
867 if (!in_minibuffer) {
868 message("text is read-only");
872 minibuffer_taint_hist();
874 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
875 start = buffer->current_line->line;
878 p = utf8_prev_cp(c-1, start);
880 memmove(p, c, strlen(c)+1);
885 cmd_mini_kill_line(struct buffer *buffer)
889 if (!in_minibuffer) {
890 message("text is read-only");
894 minibuffer_taint_hist();
895 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
900 cmd_mini_abort(struct buffer *buffer)
909 cmd_mini_complete_and_exit(struct buffer *buffer)
914 minibuffer_taint_hist();
919 cmd_mini_previous_history_element(struct buffer *buffer)
921 if (ministate.history == NULL) {
922 message("No history");
926 if (ministate.hist_cur == NULL ||
927 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
928 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
929 ministate.hist_off = ministate.history->len - 1;
930 if (ministate.hist_cur == NULL)
931 message("No prev item");
933 ministate.hist_off--;
936 if (ministate.hist_cur != NULL)
937 buffer->current_line->line = ministate.hist_cur->h;
941 cmd_mini_next_history_element(struct buffer *buffer)
943 if (ministate.history == NULL) {
944 message("No history");
948 if (ministate.hist_cur == NULL ||
949 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
950 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
951 ministate.hist_off = 0;
952 if (ministate.hist_cur == NULL)
953 message("No next item");
955 ministate.hist_off++;
958 if (ministate.hist_cur != NULL)
959 buffer->current_line->line = ministate.hist_cur->h;
963 global_key_unbound(void)
965 message("%s is undefined", keybuf);
969 minibuffer_hist_save_entry(void)
973 if (ministate.history == NULL)
976 if ((hist = calloc(1, sizeof(*hist))) == NULL)
979 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
981 if (TAILQ_EMPTY(&ministate.history->head))
982 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
984 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
985 ministate.history->len++;
989 * taint the minibuffer cache: if we're currently showing a history
990 * element, copy that to the current buf and reset the "history
994 minibuffer_taint_hist(void)
996 if (ministate.hist_cur == NULL)
999 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1000 ministate.hist_cur = NULL;
1004 minibuffer_self_insert(void)
1006 char *c, tmp[5] = {0};
1009 minibuffer_taint_hist();
1011 if (thiskey.cp == 0)
1014 len = utf8_encode(thiskey.cp, tmp);
1015 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1016 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1019 memmove(c + len, c, strlen(c)+1);
1020 memcpy(c, tmp, len);
1021 ministate.buffer.cpoff++;
1025 eecmd_self_insert(void)
1027 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1028 !unicode_isgraph(thiskey.cp)) {
1029 global_key_unbound();
1033 minibuffer_self_insert();
1041 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1042 if (!strcmp(cmd->cmd, ministate.buf)) {
1044 minibuffer_hist_save_entry();
1045 cmd->fn(current_buffer());
1050 message("No match");
1054 ir_self_insert(void)
1056 minibuffer_self_insert();
1062 char buf[1025] = {0};
1063 struct phos_uri uri;
1066 tab = current_tab();
1069 minibuffer_hist_save_entry();
1071 /* a bit ugly but... */
1072 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1073 phos_uri_set_query(&uri, ministate.buf);
1074 phos_serialize_uri(&uri, buf, sizeof(buf));
1075 load_url_in_tab(tab, buf);
1079 lu_self_insert(void)
1081 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1082 !unicode_isgraph(thiskey.key)) {
1083 global_key_unbound();
1087 minibuffer_self_insert();
1094 minibuffer_hist_save_entry();
1095 load_url_in_tab(current_tab(), ministate.buf);
1102 if (*ministate.buf != '\0')
1103 add_to_bookmarks(ministate.buf);
1109 yornp_self_insert(void)
1111 if (thiskey.key != 'y' && thiskey.key != 'n') {
1112 message("Please answer y or n");
1117 yornp_cb(thiskey.key == 'y', yornp_data);
1124 yornp_cb(0, yornp_data);
1128 read_self_insert(void)
1130 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1131 global_key_unbound();
1135 minibuffer_self_insert();
1142 read_cb(NULL, read_data);
1149 minibuffer_hist_save_entry();
1150 read_cb(ministate.buf, read_data);
1153 static struct vline *
1154 nth_line(struct buffer *buffer, size_t n)
1160 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1175 TAILQ_FOREACH(t, &tabshead, tabs) {
1176 if (t->flags & TAB_CURRENT)
1184 static struct buffer *
1185 current_buffer(void)
1188 return &ministate.buffer;
1189 return ¤t_tab()->buffer;
1197 if ((thiskey.key = wgetch(body)) == ERR)
1200 thiskey.meta = thiskey.key == 27;
1202 thiskey.key = wgetch(body);
1203 if (thiskey.key == ERR || thiskey.key == 27) {
1210 if ((unsigned int)thiskey.key < UINT8_MAX) {
1212 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1214 if ((thiskey.key = wgetch(body)) == ERR) {
1215 message("Error decoding user input");
1225 dispatch_stdio(int fd, short ev, void *d)
1228 const char *keyname;
1234 if (keybuf[0] != '\0')
1235 strlcat(keybuf, " ", sizeof(keybuf));
1237 strlcat(keybuf, "M-", sizeof(keybuf));
1238 if (thiskey.cp != 0) {
1239 utf8_encode(thiskey.cp, tmp);
1240 strlcat(keybuf, tmp, sizeof(keybuf));
1242 if ((keyname = unkbd(thiskey.key)) != NULL)
1243 strlcat(keybuf, keyname, sizeof(keybuf));
1245 tmp[0] = thiskey.key;
1246 strlcat(keybuf, tmp, sizeof(keybuf));
1250 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1251 if (k->meta == thiskey.meta &&
1252 k->key == thiskey.key) {
1254 current_map = &k->map;
1256 current_map = base_map;
1257 strlcpy(keybuf, "", sizeof(keybuf));
1258 k->fn(current_buffer());
1264 if (current_map->unhandled_input != NULL)
1265 current_map->unhandled_input();
1267 global_key_unbound();
1270 strlcpy(keybuf, "", sizeof(keybuf));
1271 current_map = base_map;
1277 redraw_tab(current_tab());
1281 handle_clear_minibuf(int fd, short ev, void *d)
1283 free(ministate.curmesg);
1284 ministate.curmesg = NULL;
1286 redraw_minibuffer();
1287 if (in_minibuffer) {
1297 handle_resize(int sig, short ev, void *d)
1299 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1300 event_del(&resizeev);
1302 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1303 evtimer_add(&resizeev, &resize_timer);
1307 handle_resize_nodelay(int s, short ev, void *d)
1315 /* move and resize the windows, in reverse order! */
1317 mvwin(minibuf, LINES-1, 0);
1318 wresize(minibuf, 1, COLS);
1320 mvwin(modeline, LINES-2, 0);
1321 wresize(modeline, 1, COLS);
1323 body_lines = LINES-3;
1327 help_cols = 0.3 * COLS;
1328 help_lines = LINES-3;
1330 wresize(help, help_lines, help_cols);
1332 wrap_page(&helpwin, help_cols);
1334 body_cols = COLS - help_cols - 1;
1335 mvwin(body, 1, help_cols);
1340 wresize(body, body_lines, body_cols);
1342 wresize(tabline, 1, COLS);
1344 tab = current_tab();
1346 wrap_page(&tab->buffer, body_cols);
1351 wrap_page(struct buffer *buffer, int width)
1354 const struct line *orig;
1359 orig = buffer->current_line == NULL
1361 : buffer->current_line->parent;
1362 buffer->current_line = NULL;
1365 buffer->line_off = 0;
1367 empty_vlist(buffer);
1369 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1370 prfx = line_prefixes[l->type].prfx1;
1379 case LINE_PRE_START:
1381 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1383 case LINE_PRE_CONTENT:
1385 pre_width = MIN(fill_column, width);
1388 hardwrap_text(buffer, l, pre_width);
1392 if (orig == l && buffer->current_line == NULL) {
1393 buffer->line_off = buffer->line_max-1;
1394 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1397 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1398 if (vl == NULL || vl->parent != orig)
1400 buffer->current_line = vl;
1406 if (buffer->current_line == NULL)
1407 buffer->current_line = TAILQ_FIRST(&buffer->head);
1413 print_vline(WINDOW *window, struct vline *vl)
1415 const char *text = vl->line;
1417 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1418 int text_face = line_faces[vl->parent->type].text_prop;
1421 prfx = line_prefixes[vl->parent->type].prfx1;
1423 prfx = line_prefixes[vl->parent->type].prfx2;
1428 wattron(window, prefix_face);
1429 wprintw(window, "%s", prfx);
1430 wattroff(window, prefix_face);
1432 wattron(window, text_face);
1433 wprintw(window, "%s", text);
1434 wattroff(window, text_face);
1438 redraw_tabline(void)
1441 size_t toskip, ots, tabwidth, space, x;
1442 int current, y, truncated;
1446 tabwidth = sizeof(buf)+1;
1450 TAILQ_FOREACH(tab, &tabshead, tabs) {
1452 if (tab->flags & TAB_CURRENT)
1456 if (toskip * tabwidth < space)
1461 while (toskip != 0 &&
1462 (ots - toskip+1) * tabwidth < space)
1467 wattron(tabline, tab_face.background);
1468 wprintw(tabline, toskip == 0 ? " " : "<");
1469 wattroff(tabline, tab_face.background);
1472 TAILQ_FOREACH(tab, &tabshead, tabs) {
1480 getyx(tabline, y, x);
1481 if (x + sizeof(buf)+2 >= (size_t)COLS)
1484 current = tab->flags & TAB_CURRENT;
1486 if (*(title = tab->buffer.page.title) == '\0')
1487 title = tab->hist_cur->h;
1489 if (tab->flags & TAB_URGENT)
1490 strlcpy(buf, "!", sizeof(buf));
1492 strlcpy(buf, " ", sizeof(buf));
1494 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1495 /* truncation happens */
1496 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1498 /* pad with spaces */
1499 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1504 wattron(tabline, tab_face.current_tab);
1506 wattron(tabline, tab_face.tab);
1508 wprintw(tabline, "%s", buf);
1509 if (TAILQ_NEXT(tab, tabs) != NULL)
1510 wprintw(tabline, " ");
1513 wattroff(tabline, tab_face.current_tab);
1515 wattroff(tabline, tab_face.tab);
1518 wattron(tabline, tab_face.background);
1519 for (; x < (size_t)COLS; ++x)
1520 waddch(tabline, ' ');
1522 mvwprintw(tabline, 0, COLS-1, ">");
1526 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1533 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1534 if (TAILQ_EMPTY(&buffer->head))
1538 vl = nth_line(buffer, buffer->line_off);
1539 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1540 wmove(win, l, x_offset);
1541 print_vline(win, vl);
1547 wmove(win, buffer->curs_y, buffer->curs_x);
1553 redraw_window(help, help_lines, &helpwin);
1557 redraw_body(struct tab *tab)
1559 redraw_window(body, body_lines, &tab->buffer);
1563 trust_status_char(enum trust_state ts)
1566 case TS_UNKNOWN: return 'u';
1567 case TS_UNTRUSTED: return '!';
1568 case TS_TRUSTED: return 'v';
1569 case TS_VERIFIED: return 'V';
1574 redraw_modeline(struct tab *tab)
1577 int x, y, max_x, max_y;
1578 const char *mode = tab->buffer.page.name;
1579 const char *spin = "-\\|/";
1582 wattron(modeline, modeline_face.background);
1583 wmove(modeline, 0, 0);
1585 wprintw(modeline, "-%c%c %s ",
1586 spin[tab->loading_anim_step],
1587 trust_status_char(tab->trust),
1588 mode == NULL ? "(none)" : mode);
1590 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1592 if (tab->buffer.line_max <= (size_t)body_lines)
1593 wprintw(modeline, "All ");
1594 else if (tab->buffer.line_off == 0)
1595 wprintw(modeline, "Top ");
1596 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1597 wprintw(modeline, "Bottom ");
1599 wprintw(modeline, "%.0f%% ", pct);
1601 wprintw(modeline, "%d/%d %s ",
1602 tab->buffer.line_off + tab->buffer.curs_y,
1603 tab->buffer.line_max,
1606 getyx(modeline, y, x);
1607 getmaxyx(modeline, max_y, max_x);
1612 for (; x < max_x; ++x)
1613 waddstr(modeline, "-");
1615 wattroff(modeline, modeline_face.background);
1619 redraw_minibuffer(void)
1622 size_t off_y, off_x = 0;
1625 wattron(minibuf, minibuffer_face.background);
1628 if (in_minibuffer) {
1629 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1630 if (ministate.hist_cur != NULL)
1631 wprintw(minibuf, "(%zu/%zu) ",
1632 ministate.hist_off + 1,
1633 ministate.history->len);
1635 getyx(minibuf, off_y, off_x);
1637 start = ministate.hist_cur != NULL
1638 ? ministate.hist_cur->h
1640 c = utf8_nth(ministate.buffer.current_line->line,
1641 ministate.buffer.cpoff);
1642 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1643 start = utf8_next_cp(start);
1646 waddstr(minibuf, start);
1649 if (ministate.curmesg != NULL)
1650 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1653 if (!in_minibuffer && ministate.curmesg == NULL)
1654 waddstr(minibuf, keybuf);
1656 /* If nothing else, show the URL at point */
1657 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1658 tab = current_tab();
1659 if (tab->buffer.current_line != NULL &&
1660 tab->buffer.current_line->parent->type == LINE_LINK)
1661 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1665 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1667 wattroff(minibuf, minibuffer_face.background);
1671 redraw_tab(struct tab *tab)
1678 restore_cursor(&tab->buffer);
1682 redraw_modeline(tab);
1683 redraw_minibuffer();
1685 wnoutrefresh(tabline);
1686 wnoutrefresh(modeline);
1688 if (in_minibuffer) {
1690 wnoutrefresh(minibuf);
1692 wnoutrefresh(minibuf);
1700 emit_help_item(char *prfx, void *fn)
1705 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1709 assert(cmd != NULL);
1711 if ((l = calloc(1, sizeof(*l))) == NULL)
1714 l->type = LINE_TEXT;
1717 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1719 if (TAILQ_EMPTY(&helpwin.page.head))
1720 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1722 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1726 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1732 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1733 strlcpy(p, prfx, sizeof(p));
1735 strlcat(p, " ", sizeof(p));
1737 strlcat(p, "M-", sizeof(p));
1738 if ((kn = unkbd(k->key)) != NULL)
1739 strlcat(p, kn, sizeof(p));
1741 strlcat(p, keyname(k->key), sizeof(p));
1744 rec_compute_help(&k->map, p, sizeof(p));
1746 emit_help_item(p, k->fn);
1751 recompute_help(void)
1755 empty_vlist(&helpwin);
1756 empty_linelist(&helpwin);
1757 rec_compute_help(current_map, p, sizeof(p));
1758 wrap_page(&helpwin, help_cols);
1762 vmessage(const char *fmt, va_list ap)
1764 if (evtimer_pending(&clminibufev, NULL))
1765 evtimer_del(&clminibufev);
1766 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1767 evtimer_add(&clminibufev, &clminibufev_timer);
1769 free(ministate.curmesg);
1771 /* TODO: what to do if the allocation fails here? */
1772 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1773 ministate.curmesg = NULL;
1775 redraw_minibuffer();
1776 if (in_minibuffer) {
1786 message(const char *fmt, ...)
1796 start_loading_anim(struct tab *tab)
1798 if (tab->loading_anim)
1800 tab->loading_anim = 1;
1801 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1802 evtimer_add(&tab->loadingev, &loadingev_timer);
1806 update_loading_anim(int fd, short ev, void *d)
1808 struct tab *tab = d;
1810 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1812 if (tab->flags & TAB_CURRENT) {
1813 redraw_modeline(tab);
1820 evtimer_add(&tab->loadingev, &loadingev_timer);
1824 stop_loading_anim(struct tab *tab)
1826 if (!tab->loading_anim)
1828 evtimer_del(&tab->loadingev);
1829 tab->loading_anim = 0;
1830 tab->loading_anim_step = 0;
1832 if (!(tab->flags & TAB_CURRENT))
1835 redraw_modeline(tab);
1844 load_url_in_tab(struct tab *tab, const char *url)
1846 message("Loading %s...", url);
1847 start_loading_anim(tab);
1850 tab->buffer.curs_x = 0;
1851 tab->buffer.curs_y = 0;
1856 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1857 void (*abortfn)(void), struct histhead *hist)
1860 base_map = &minibuffer_map;
1861 current_map = &minibuffer_map;
1863 base_map->unhandled_input = self_insert_fn;
1865 ministate.donefn = donefn;
1866 ministate.abortfn = abortfn;
1867 memset(ministate.buf, 0, sizeof(ministate.buf));
1868 ministate.buffer.current_line = &ministate.vline;
1869 ministate.buffer.current_line->line = ministate.buf;
1870 ministate.buffer.cpoff = 0;
1871 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1873 ministate.history = hist;
1874 ministate.hist_cur = NULL;
1875 ministate.hist_off = 0;
1879 exit_minibuffer(void)
1884 base_map = &global_map;
1885 current_map = &global_map;
1889 switch_to_tab(struct tab *tab)
1893 TAILQ_FOREACH(t, &tabshead, tabs) {
1894 t->flags &= ~TAB_CURRENT;
1897 tab->flags |= TAB_CURRENT;
1898 tab->flags &= ~TAB_URGENT;
1904 return tab_counter++;
1908 new_tab(const char *url)
1912 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1918 TAILQ_INIT(&tab->hist.head);
1920 TAILQ_INIT(&tab->buffer.head);
1922 tab->id = tab_new_id();
1925 if (TAILQ_EMPTY(&tabshead))
1926 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1928 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1930 load_url_in_tab(tab, url);
1935 session_new_tab_cb(const char *url)
1943 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1944 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1948 ui_init(int argc, char * const *argv)
1950 char path[PATH_MAX];
1951 const char *url = NEW_TAB_URL;
1952 int ch, configtest = 0, fonf = 0;
1954 strlcpy(path, getenv("HOME"), sizeof(path));
1955 strlcat(path, "/.telescope/config", sizeof(path));
1957 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1961 strlcpy(path, optarg, sizeof(path));
1977 parseconfig(path, fonf);
1986 setlocale(LC_ALL, "");
1988 TAILQ_INIT(&global_map.m);
1989 global_map.unhandled_input = global_key_unbound;
1991 TAILQ_INIT(&minibuffer_map.m);
1993 TAILQ_INIT(&eecmd_history.head);
1994 TAILQ_INIT(&ir_history.head);
1995 TAILQ_INIT(&lu_history.head);
1997 ministate.line.type = LINE_TEXT;
1998 ministate.vline.parent = &ministate.line;
1999 ministate.buffer.current_line = &ministate.vline;
2001 /* initialize help window */
2002 TAILQ_INIT(&helpwin.head);
2004 base_map = &global_map;
2005 current_map = &global_map;
2006 load_default_keys();
2013 intrflush(stdscr, FALSE);
2015 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2017 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2019 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2021 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2023 if ((help = newwin(1, 1, 1, 0)) == NULL)
2026 body_lines = LINES-3;
2032 scrollok(body, TRUE);
2034 /* non-blocking input */
2037 mvwprintw(body, 0, 0, "");
2039 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2040 event_add(&stdioev, NULL);
2042 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2043 signal_add(&winchev, NULL);
2045 load_last_session(session_new_tab_cb);
2046 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2053 ui_on_tab_loaded(struct tab *tab)
2055 stop_loading_anim(tab);
2056 message("Loaded %s", tab->hist_cur->h);
2067 ui_on_tab_refresh(struct tab *tab)
2069 wrap_page(&tab->buffer, body_cols);
2070 if (tab->flags & TAB_CURRENT) {
2071 restore_cursor(&tab->buffer);
2074 tab->flags |= TAB_URGENT;
2078 ui_require_input(struct tab *tab, int hide)
2080 /* TODO: hard-switching to another tab is ugly */
2083 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2085 strlcpy(ministate.prompt, "Input required: ",
2086 sizeof(ministate.prompt));
2091 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2096 if (in_minibuffer) {
2103 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2106 len = sizeof(ministate.prompt);
2107 strlcpy(ministate.prompt, prompt, len);
2108 strlcat(ministate.prompt, " (y or n) ", len);
2109 redraw_tab(current_tab());
2113 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2123 enter_minibuffer(read_self_insert, read_select, read_abort,
2126 len = sizeof(ministate.prompt);
2127 strlcpy(ministate.prompt, prompt, len);
2128 strlcat(ministate.prompt, ": ", len);
2129 redraw_tab(current_tab());
2133 ui_notify(const char *fmt, ...)