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();
1269 strlcpy(keybuf, "", sizeof(keybuf));
1270 current_map = base_map;
1276 redraw_tab(current_tab());
1280 handle_clear_minibuf(int fd, short ev, void *d)
1282 free(ministate.curmesg);
1283 ministate.curmesg = NULL;
1285 redraw_minibuffer();
1286 if (in_minibuffer) {
1296 handle_resize(int sig, short ev, void *d)
1298 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1299 event_del(&resizeev);
1301 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1302 evtimer_add(&resizeev, &resize_timer);
1306 handle_resize_nodelay(int s, short ev, void *d)
1314 /* move and resize the windows, in reverse order! */
1316 mvwin(minibuf, LINES-1, 0);
1317 wresize(minibuf, 1, COLS);
1319 mvwin(modeline, LINES-2, 0);
1320 wresize(modeline, 1, COLS);
1322 body_lines = LINES-3;
1326 help_cols = 0.3 * COLS;
1327 help_lines = LINES-3;
1329 wresize(help, help_lines, help_cols);
1331 wrap_page(&helpwin, help_cols);
1333 body_cols = COLS - help_cols - 1;
1334 mvwin(body, 1, help_cols);
1339 wresize(body, body_lines, body_cols);
1341 wresize(tabline, 1, COLS);
1343 tab = current_tab();
1345 wrap_page(&tab->buffer, body_cols);
1350 wrap_page(struct buffer *buffer, int width)
1353 const struct line *orig;
1358 orig = buffer->current_line == NULL
1360 : buffer->current_line->parent;
1361 buffer->current_line = NULL;
1364 buffer->line_off = 0;
1366 empty_vlist(buffer);
1368 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1369 prfx = line_prefixes[l->type].prfx1;
1378 case LINE_PRE_START:
1380 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1382 case LINE_PRE_CONTENT:
1384 pre_width = MIN(fill_column, width);
1387 hardwrap_text(buffer, l, pre_width);
1391 if (orig == l && buffer->current_line == NULL) {
1392 buffer->line_off = buffer->line_max-1;
1393 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1396 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1397 if (vl == NULL || vl->parent != orig)
1399 buffer->current_line = vl;
1405 if (buffer->current_line == NULL)
1406 buffer->current_line = TAILQ_FIRST(&buffer->head);
1412 print_vline(WINDOW *window, struct vline *vl)
1414 const char *text = vl->line;
1416 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1417 int text_face = line_faces[vl->parent->type].text_prop;
1420 prfx = line_prefixes[vl->parent->type].prfx1;
1422 prfx = line_prefixes[vl->parent->type].prfx2;
1427 wattron(window, prefix_face);
1428 wprintw(window, "%s", prfx);
1429 wattroff(window, prefix_face);
1431 wattron(window, text_face);
1432 wprintw(window, "%s", text);
1433 wattroff(window, text_face);
1437 redraw_tabline(void)
1440 size_t toskip, ots, tabwidth, space, x;
1441 int current, y, truncated;
1445 tabwidth = sizeof(buf)+1;
1449 TAILQ_FOREACH(tab, &tabshead, tabs) {
1451 if (tab->flags & TAB_CURRENT)
1455 if (toskip * tabwidth < space)
1460 while (toskip != 0 &&
1461 (ots - toskip+1) * tabwidth < space)
1466 wattron(tabline, tab_face.background);
1467 wprintw(tabline, toskip == 0 ? " " : "<");
1468 wattroff(tabline, tab_face.background);
1471 TAILQ_FOREACH(tab, &tabshead, tabs) {
1479 getyx(tabline, y, x);
1480 if (x + sizeof(buf)+2 >= (size_t)COLS)
1483 current = tab->flags & TAB_CURRENT;
1485 if (*(title = tab->buffer.page.title) == '\0')
1486 title = tab->hist_cur->h;
1488 if (tab->flags & TAB_URGENT)
1489 strlcpy(buf, "!", sizeof(buf));
1491 strlcpy(buf, " ", sizeof(buf));
1493 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1494 /* truncation happens */
1495 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1497 /* pad with spaces */
1498 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1503 wattron(tabline, tab_face.current_tab);
1505 wattron(tabline, tab_face.tab);
1507 wprintw(tabline, "%s", buf);
1508 if (TAILQ_NEXT(tab, tabs) != NULL)
1509 wprintw(tabline, " ");
1512 wattroff(tabline, tab_face.current_tab);
1514 wattroff(tabline, tab_face.tab);
1517 wattron(tabline, tab_face.background);
1518 for (; x < (size_t)COLS; ++x)
1519 waddch(tabline, ' ');
1521 mvwprintw(tabline, 0, COLS-1, ">");
1525 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1532 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1533 if (TAILQ_EMPTY(&buffer->head))
1537 vl = nth_line(buffer, buffer->line_off);
1538 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1539 wmove(win, l, x_offset);
1540 print_vline(win, vl);
1546 wmove(win, buffer->curs_y, buffer->curs_x);
1552 redraw_window(help, help_lines, &helpwin);
1556 redraw_body(struct tab *tab)
1558 redraw_window(body, body_lines, &tab->buffer);
1562 trust_status_char(enum trust_state ts)
1565 case TS_UNKNOWN: return 'u';
1566 case TS_UNTRUSTED: return '!';
1567 case TS_TRUSTED: return 'v';
1568 case TS_VERIFIED: return 'V';
1573 redraw_modeline(struct tab *tab)
1576 int x, y, max_x, max_y;
1577 const char *mode = tab->buffer.page.name;
1578 const char *spin = "-\\|/";
1581 wattron(modeline, modeline_face.background);
1582 wmove(modeline, 0, 0);
1584 wprintw(modeline, "-%c%c %s ",
1585 spin[tab->loading_anim_step],
1586 trust_status_char(tab->trust),
1587 mode == NULL ? "(none)" : mode);
1589 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1591 if (tab->buffer.line_max <= (size_t)body_lines)
1592 wprintw(modeline, "All ");
1593 else if (tab->buffer.line_off == 0)
1594 wprintw(modeline, "Top ");
1595 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1596 wprintw(modeline, "Bottom ");
1598 wprintw(modeline, "%.0f%% ", pct);
1600 wprintw(modeline, "%d/%d %s ",
1601 tab->buffer.line_off + tab->buffer.curs_y,
1602 tab->buffer.line_max,
1605 getyx(modeline, y, x);
1606 getmaxyx(modeline, max_y, max_x);
1611 for (; x < max_x; ++x)
1612 waddstr(modeline, "-");
1614 wattroff(modeline, modeline_face.background);
1618 redraw_minibuffer(void)
1621 size_t off_y, off_x = 0;
1624 wattron(minibuf, minibuffer_face.background);
1627 if (in_minibuffer) {
1628 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1629 if (ministate.hist_cur != NULL)
1630 wprintw(minibuf, "(%zu/%zu) ",
1631 ministate.hist_off + 1,
1632 ministate.history->len);
1634 getyx(minibuf, off_y, off_x);
1636 start = ministate.hist_cur != NULL
1637 ? ministate.hist_cur->h
1639 c = utf8_nth(ministate.buffer.current_line->line,
1640 ministate.buffer.cpoff);
1641 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1642 start = utf8_next_cp(start);
1645 waddstr(minibuf, start);
1648 if (ministate.curmesg != NULL)
1649 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1652 if (!in_minibuffer && ministate.curmesg == NULL)
1653 waddstr(minibuf, keybuf);
1655 /* If nothing else, show the URL at point */
1656 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1657 tab = current_tab();
1658 if (tab->buffer.current_line != NULL &&
1659 tab->buffer.current_line->parent->type == LINE_LINK)
1660 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1664 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1666 wattroff(minibuf, minibuffer_face.background);
1670 redraw_tab(struct tab *tab)
1677 restore_cursor(&tab->buffer);
1681 redraw_modeline(tab);
1682 redraw_minibuffer();
1684 wnoutrefresh(tabline);
1685 wnoutrefresh(modeline);
1687 if (in_minibuffer) {
1689 wnoutrefresh(minibuf);
1691 wnoutrefresh(minibuf);
1699 emit_help_item(char *prfx, void *fn)
1704 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1708 assert(cmd != NULL);
1710 if ((l = calloc(1, sizeof(*l))) == NULL)
1713 l->type = LINE_TEXT;
1716 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1718 if (TAILQ_EMPTY(&helpwin.page.head))
1719 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1721 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1725 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1731 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1732 strlcpy(p, prfx, sizeof(p));
1734 strlcat(p, " ", sizeof(p));
1736 strlcat(p, "M-", sizeof(p));
1737 if ((kn = unkbd(k->key)) != NULL)
1738 strlcat(p, kn, sizeof(p));
1740 strlcat(p, keyname(k->key), sizeof(p));
1743 rec_compute_help(&k->map, p, sizeof(p));
1745 emit_help_item(p, k->fn);
1750 recompute_help(void)
1754 empty_vlist(&helpwin);
1755 empty_linelist(&helpwin);
1756 rec_compute_help(current_map, p, sizeof(p));
1757 wrap_page(&helpwin, help_cols);
1761 vmessage(const char *fmt, va_list ap)
1763 if (evtimer_pending(&clminibufev, NULL))
1764 evtimer_del(&clminibufev);
1765 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1766 evtimer_add(&clminibufev, &clminibufev_timer);
1768 free(ministate.curmesg);
1770 /* TODO: what to do if the allocation fails here? */
1771 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1772 ministate.curmesg = NULL;
1774 redraw_minibuffer();
1775 if (in_minibuffer) {
1785 message(const char *fmt, ...)
1795 start_loading_anim(struct tab *tab)
1797 if (tab->loading_anim)
1799 tab->loading_anim = 1;
1800 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1801 evtimer_add(&tab->loadingev, &loadingev_timer);
1805 update_loading_anim(int fd, short ev, void *d)
1807 struct tab *tab = d;
1809 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1811 if (tab->flags & TAB_CURRENT) {
1812 redraw_modeline(tab);
1819 evtimer_add(&tab->loadingev, &loadingev_timer);
1823 stop_loading_anim(struct tab *tab)
1825 if (!tab->loading_anim)
1827 evtimer_del(&tab->loadingev);
1828 tab->loading_anim = 0;
1829 tab->loading_anim_step = 0;
1831 if (!(tab->flags & TAB_CURRENT))
1834 redraw_modeline(tab);
1843 load_url_in_tab(struct tab *tab, const char *url)
1845 message("Loading %s...", url);
1846 start_loading_anim(tab);
1849 tab->buffer.curs_x = 0;
1850 tab->buffer.curs_y = 0;
1855 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1856 void (*abortfn)(void), struct histhead *hist)
1859 base_map = &minibuffer_map;
1860 current_map = &minibuffer_map;
1862 base_map->unhandled_input = self_insert_fn;
1864 ministate.donefn = donefn;
1865 ministate.abortfn = abortfn;
1866 memset(ministate.buf, 0, sizeof(ministate.buf));
1867 ministate.buffer.current_line = &ministate.vline;
1868 ministate.buffer.current_line->line = ministate.buf;
1869 ministate.buffer.cpoff = 0;
1870 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1872 ministate.history = hist;
1873 ministate.hist_cur = NULL;
1874 ministate.hist_off = 0;
1878 exit_minibuffer(void)
1883 base_map = &global_map;
1884 current_map = &global_map;
1888 switch_to_tab(struct tab *tab)
1892 TAILQ_FOREACH(t, &tabshead, tabs) {
1893 t->flags &= ~TAB_CURRENT;
1896 tab->flags |= TAB_CURRENT;
1897 tab->flags &= ~TAB_URGENT;
1903 return tab_counter++;
1907 new_tab(const char *url)
1911 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1917 TAILQ_INIT(&tab->hist.head);
1919 TAILQ_INIT(&tab->buffer.head);
1921 tab->id = tab_new_id();
1924 if (TAILQ_EMPTY(&tabshead))
1925 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1927 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1929 load_url_in_tab(tab, url);
1934 session_new_tab_cb(const char *url)
1942 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1943 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1947 ui_init(int argc, char * const *argv)
1949 char path[PATH_MAX];
1950 const char *url = NEW_TAB_URL;
1951 int ch, configtest = 0, fonf = 0;
1953 strlcpy(path, getenv("HOME"), sizeof(path));
1954 strlcat(path, "/.telescope/config", sizeof(path));
1956 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1960 strlcpy(path, optarg, sizeof(path));
1976 parseconfig(path, fonf);
1985 setlocale(LC_ALL, "");
1987 TAILQ_INIT(&global_map.m);
1988 global_map.unhandled_input = global_key_unbound;
1990 TAILQ_INIT(&minibuffer_map.m);
1992 TAILQ_INIT(&eecmd_history.head);
1993 TAILQ_INIT(&ir_history.head);
1994 TAILQ_INIT(&lu_history.head);
1996 ministate.line.type = LINE_TEXT;
1997 ministate.vline.parent = &ministate.line;
1998 ministate.buffer.current_line = &ministate.vline;
2000 /* initialize help window */
2001 TAILQ_INIT(&helpwin.head);
2003 base_map = &global_map;
2004 current_map = &global_map;
2005 load_default_keys();
2012 intrflush(stdscr, FALSE);
2014 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2016 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2018 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2020 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2022 if ((help = newwin(1, 1, 1, 0)) == NULL)
2025 body_lines = LINES-3;
2031 scrollok(body, TRUE);
2033 /* non-blocking input */
2036 mvwprintw(body, 0, 0, "");
2038 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2039 event_add(&stdioev, NULL);
2041 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2042 signal_add(&winchev, NULL);
2044 load_last_session(session_new_tab_cb);
2045 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2052 ui_on_tab_loaded(struct tab *tab)
2054 stop_loading_anim(tab);
2055 message("Loaded %s", tab->hist_cur->h);
2066 ui_on_tab_refresh(struct tab *tab)
2068 wrap_page(&tab->buffer, body_cols);
2069 if (tab->flags & TAB_CURRENT) {
2070 restore_cursor(&tab->buffer);
2073 tab->flags |= TAB_URGENT;
2077 ui_require_input(struct tab *tab, int hide)
2079 /* TODO: hard-switching to another tab is ugly */
2082 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2084 strlcpy(ministate.prompt, "Input required: ",
2085 sizeof(ministate.prompt));
2090 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2095 if (in_minibuffer) {
2102 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2105 len = sizeof(ministate.prompt);
2106 strlcpy(ministate.prompt, prompt, len);
2107 strlcat(ministate.prompt, " (y or n) ", len);
2108 redraw_tab(current_tab());
2112 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2122 enter_minibuffer(read_self_insert, read_select, read_abort,
2125 len = sizeof(ministate.prompt);
2126 strlcpy(ministate.prompt, prompt, len);
2127 strlcat(ministate.prompt, ": ", len);
2128 redraw_tab(current_tab());
2132 ui_notify(const char *fmt, ...)