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);
109 static struct { short meta; int key; uint32_t cp; } thiskey;
111 static struct event resizeev;
112 static struct timeval resize_timer = { 0, 250000 };
114 static WINDOW *tabline, *body, *modeline, *minibuf;
115 static int body_lines, body_cols;
118 static struct buffer helpwin;
119 static int help_lines, help_cols;
121 static int side_window;
123 static struct event clminibufev;
124 static struct timeval clminibufev_timer = { 5, 0 };
125 static struct timeval loadingev_timer = { 0, 250000 };
127 static uint32_t tab_counter;
129 static char keybuf[64];
131 static void (*yornp_cb)(int, unsigned int);
132 static unsigned int yornp_data;
134 static void (*read_cb)(const char*, unsigned int);
135 static unsigned int read_data;
137 struct kmap global_map,
142 static struct histhead eecmd_history,
147 static int in_minibuffer;
153 void (*donefn)(void);
154 void (*abortfn)(void);
159 struct buffer buffer;
161 struct histhead *history;
162 struct hist *hist_cur;
167 global_set_key(const char *key, void (*fn)(struct buffer*))
169 if (!kmap_define_key(&global_map, key, fn))
174 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
176 if (!kmap_define_key(&minibuffer_map, key, fn))
181 load_default_keys(void)
183 /* === global map === */
186 global_set_key("C-p", cmd_previous_line);
187 global_set_key("C-n", cmd_next_line);
188 global_set_key("C-f", cmd_forward_char);
189 global_set_key("C-b", cmd_backward_char);
190 global_set_key("M-{", cmd_backward_paragraph);
191 global_set_key("M-}", cmd_forward_paragraph);
192 global_set_key("C-a", cmd_move_beginning_of_line);
193 global_set_key("C-e", cmd_move_end_of_line);
195 global_set_key("M-v", cmd_scroll_up);
196 global_set_key("C-v", cmd_scroll_down);
197 global_set_key("M-space", cmd_scroll_up);
198 global_set_key("space", cmd_scroll_down);
200 global_set_key("M-<", cmd_beginning_of_buffer);
201 global_set_key("M->", cmd_end_of_buffer);
203 global_set_key("C-x C-c", cmd_kill_telescope);
205 global_set_key("C-g", cmd_clear_minibuf);
207 global_set_key("M-x", cmd_execute_extended_command);
208 global_set_key("C-x C-f", cmd_load_url);
209 global_set_key("C-x M-f", cmd_load_current_url);
211 global_set_key("C-x t 0", cmd_tab_close);
212 global_set_key("C-x t 1", cmd_tab_close_other);
213 global_set_key("C-x t 2", cmd_tab_new);
214 global_set_key("C-x t o", cmd_tab_next);
215 global_set_key("C-x t O", cmd_tab_previous);
216 global_set_key("C-x t m", cmd_tab_move);
217 global_set_key("C-x t M", cmd_tab_move_to);
219 global_set_key("C-M-b", cmd_previous_page);
220 global_set_key("C-M-f", cmd_next_page);
222 global_set_key("<f7> a", cmd_bookmark_page);
223 global_set_key("<f7> <f7>", cmd_list_bookmarks);
226 global_set_key("k", cmd_previous_line);
227 global_set_key("j", cmd_next_line);
228 global_set_key("l", cmd_forward_char);
229 global_set_key("h", cmd_backward_char);
230 global_set_key("{", cmd_backward_paragraph);
231 global_set_key("}", cmd_forward_paragraph);
232 global_set_key("^", cmd_move_beginning_of_line);
233 global_set_key("$", cmd_move_end_of_line);
235 global_set_key("K", cmd_scroll_line_up);
236 global_set_key("J", cmd_scroll_line_down);
238 global_set_key("g g", cmd_beginning_of_buffer);
239 global_set_key("G", cmd_end_of_buffer);
241 global_set_key("g D", cmd_tab_close);
242 global_set_key("g N", cmd_tab_new);
243 global_set_key("g t", cmd_tab_next);
244 global_set_key("g T", cmd_tab_previous);
245 global_set_key("g M-t", cmd_tab_move);
246 global_set_key("g M-T", cmd_tab_move_to);
248 global_set_key("H", cmd_previous_page);
249 global_set_key("L", cmd_next_page);
252 global_set_key("q", cmd_kill_telescope);
254 global_set_key("esc", cmd_clear_minibuf);
256 global_set_key(":", cmd_execute_extended_command);
259 global_set_key("<up>", cmd_previous_line);
260 global_set_key("<down>", cmd_next_line);
261 global_set_key("<right>", cmd_forward_char);
262 global_set_key("<left>", cmd_backward_char);
263 global_set_key("<prior>", cmd_scroll_up);
264 global_set_key("<next>", cmd_scroll_down);
266 global_set_key("M-<left>", cmd_previous_page);
267 global_set_key("M-<right>", cmd_next_page);
269 /* "ncurses standard" */
270 global_set_key("C-l", cmd_redraw);
273 global_set_key("<f1>", cmd_toggle_help);
274 global_set_key("C-m", cmd_push_button);
275 global_set_key("M-enter", cmd_push_button_new_tab);
276 global_set_key("M-tab", cmd_previous_button);
277 global_set_key("backtab", cmd_previous_button);
278 global_set_key("tab", cmd_next_button);
280 /* === minibuffer map === */
281 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
282 minibuffer_set_key("C-g", cmd_mini_abort);
283 minibuffer_set_key("esc", cmd_mini_abort);
284 minibuffer_set_key("C-d", cmd_mini_delete_char);
285 minibuffer_set_key("del", cmd_mini_delete_backward_char);
286 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
287 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
289 minibuffer_set_key("C-b", cmd_backward_char);
290 minibuffer_set_key("C-f", cmd_forward_char);
291 minibuffer_set_key("<left>", cmd_backward_char);
292 minibuffer_set_key("<right>", cmd_forward_char);
293 minibuffer_set_key("C-e", cmd_move_end_of_line);
294 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
295 minibuffer_set_key("<end>", cmd_move_end_of_line);
296 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
297 minibuffer_set_key("C-k", cmd_mini_kill_line);
299 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
300 minibuffer_set_key("M-n", cmd_mini_next_history_element);
301 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
302 minibuffer_set_key("<down>", cmd_mini_next_history_element);
306 restore_cursor(struct buffer *buffer)
311 vl = buffer->current_line;
312 if (vl == NULL || vl->line == NULL)
313 buffer->curs_x = buffer->cpoff = 0;
315 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
318 prfx = line_prefixes[vl->parent->type].prfx1;
319 buffer->curs_x += utf8_swidth(prfx);
324 cmd_previous_line(struct buffer *buffer)
328 if (buffer->current_line == NULL
329 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
332 if (--buffer->curs_y < 0) {
334 cmd_scroll_line_up(buffer);
338 buffer->current_line = vl;
339 restore_cursor(buffer);
343 cmd_next_line(struct buffer *buffer)
347 if (buffer->current_line == NULL
348 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
351 if (++buffer->curs_y > body_lines-1) {
352 buffer->curs_y = body_lines-1;
353 cmd_scroll_line_down(buffer);
357 buffer->current_line = vl;
358 restore_cursor(buffer);
362 cmd_backward_char(struct buffer *buffer)
364 if (buffer->cpoff != 0)
366 restore_cursor(buffer);
370 cmd_forward_char(struct buffer *buffer)
374 if (buffer->current_line->line != NULL)
375 len = utf8_cplen(buffer->current_line->line);
376 if (++buffer->cpoff > len)
378 restore_cursor(buffer);
382 cmd_backward_paragraph(struct buffer *buffer)
385 if (buffer->current_line == NULL ||
386 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
387 message("No previous paragraph");
390 cmd_previous_line(buffer);
391 } while (buffer->current_line->line != NULL ||
392 buffer->current_line->parent->type != LINE_TEXT);
396 cmd_forward_paragraph(struct buffer *buffer)
399 if (buffer->current_line == NULL ||
400 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
401 message("No next paragraph");
404 cmd_next_line(buffer);
405 } while (buffer->current_line->line != NULL ||
406 buffer->current_line->parent->type != LINE_TEXT);
410 cmd_move_beginning_of_line(struct buffer *buffer)
413 restore_cursor(buffer);
417 cmd_move_end_of_line(struct buffer *buffer)
421 vl = buffer->current_line;
422 if (vl->line == NULL)
424 buffer->cpoff = utf8_cplen(vl->line);
425 restore_cursor(buffer);
429 cmd_redraw(struct buffer *buffer)
431 handle_resize(0, 0, NULL);
435 cmd_scroll_line_up(struct buffer *buffer)
439 if (buffer->line_off == 0)
442 vl = nth_line(buffer, --buffer->line_off);
445 print_vline(body, vl);
447 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
448 restore_cursor(buffer);
452 cmd_scroll_line_down(struct buffer *buffer)
456 vl = buffer->current_line;
457 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
459 buffer->current_line = vl;
464 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
467 vl = nth_line(buffer, buffer->line_off + body_lines-1);
468 wmove(body, body_lines-1, 0);
469 print_vline(body, vl);
471 restore_cursor(buffer);
475 cmd_scroll_up(struct buffer *buffer)
481 for (; off > 0; --off)
482 cmd_scroll_line_up(buffer);
486 cmd_scroll_down(struct buffer *buffer)
492 for (; off > 0; --off)
493 cmd_scroll_line_down(buffer);
497 cmd_beginning_of_buffer(struct buffer *buffer)
499 buffer->current_line = TAILQ_FIRST(&buffer->head);
500 buffer->line_off = 0;
503 restore_cursor(buffer);
507 cmd_end_of_buffer(struct buffer *buffer)
511 off = buffer->line_max - body_lines;
514 buffer->line_off = off;
515 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
517 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
518 buffer->cpoff = body_cols;
519 restore_cursor(buffer);
523 cmd_kill_telescope(struct buffer *buffer)
530 cmd_push_button(struct buffer *buffer)
535 nth = buffer->line_off + buffer->curs_y;
536 if (nth >= buffer->line_max)
538 vl = nth_line(buffer, nth);
539 if (vl->parent->type != LINE_LINK)
542 load_url_in_tab(current_tab(), vl->parent->alt);
546 cmd_push_button_new_tab(struct buffer *buffer)
551 nth = buffer->line_off + buffer->curs_y;
552 if (nth > buffer->line_max)
554 vl = nth_line(buffer, nth);
555 if (vl->parent->type != LINE_LINK)
558 new_tab(vl->parent->alt);
562 cmd_previous_button(struct buffer *buffer)
565 if (buffer->current_line == NULL ||
566 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
567 message("No previous link");
570 cmd_previous_line(buffer);
571 } while (buffer->current_line->parent->type != LINE_LINK);
575 cmd_next_button(struct buffer *buffer)
578 if (buffer->current_line == NULL ||
579 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
580 message("No next link");
583 cmd_next_line(buffer);
584 } while (buffer->current_line->parent->type != LINE_LINK);
588 cmd_previous_page(struct buffer *buffer)
590 struct tab *tab = current_tab();
592 if (!load_previous_page(tab))
593 message("No previous page");
595 start_loading_anim(tab);
599 cmd_next_page(struct buffer *buffer)
601 struct tab *tab = current_tab();
603 if (!load_next_page(tab))
604 message("No next page");
606 start_loading_anim(tab);
610 cmd_clear_minibuf(struct buffer *buffer)
612 handle_clear_minibuf(0, 0, NULL);
616 cmd_execute_extended_command(struct buffer *buffer)
621 message("We don't have enable-recursive-minibuffers");
625 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
628 len = sizeof(ministate.prompt);
629 strlcpy(ministate.prompt, "", len);
632 strlcat(ministate.prompt, "M-", len);
634 strlcat(ministate.prompt, keyname(thiskey.key), len);
637 strlcat(ministate.prompt, " ", len);
641 cmd_tab_close(struct buffer *buffer)
646 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
647 TAILQ_NEXT(tab, tabs) == NULL) {
648 message("Can't close the only tab.");
652 if (evtimer_pending(&tab->loadingev, NULL))
653 evtimer_del(&tab->loadingev);
657 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
658 t = TAILQ_NEXT(tab, tabs);
659 TAILQ_REMOVE(&tabshead, tab, tabs);
666 cmd_tab_close_other(struct buffer *buffer)
670 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
671 if (t->flags & TAB_CURRENT)
675 TAILQ_REMOVE(&tabshead, t, tabs);
681 cmd_tab_new(struct buffer *buffer)
685 if ((url = new_tab_url) == NULL)
692 cmd_tab_next(struct buffer *buffer)
697 tab->flags &= ~TAB_CURRENT;
699 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
700 t = TAILQ_FIRST(&tabshead);
701 t->flags |= TAB_CURRENT;
702 t->flags &= ~TAB_URGENT;
706 cmd_tab_previous(struct buffer *buffer)
711 tab->flags &= ~TAB_CURRENT;
713 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
714 t = TAILQ_LAST(&tabshead, tabshead);
715 t->flags |= TAB_CURRENT;
716 t->flags &= ~TAB_URGENT;
720 cmd_tab_move(struct buffer *buffer)
725 t = TAILQ_NEXT(tab, tabs);
726 TAILQ_REMOVE(&tabshead, tab, tabs);
729 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
731 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
735 cmd_tab_move_to(struct buffer *buffer)
740 t = TAILQ_PREV(tab, tabshead, tabs);
741 TAILQ_REMOVE(&tabshead, tab, tabs);
744 if (TAILQ_EMPTY(&tabshead))
745 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
747 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
749 TAILQ_INSERT_BEFORE(t, tab, tabs);
753 cmd_load_url(struct buffer *buffer)
756 message("We don't have enable-recursive-minibuffers");
760 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
762 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
763 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
764 cmd_move_end_of_line(&ministate.buffer);
768 cmd_load_current_url(struct buffer *buffer)
770 struct tab *tab = current_tab();
773 message("We don't have enable-recursive-minibuffers");
777 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
779 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
780 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
781 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
785 cmd_bookmark_page(struct buffer *buffer)
787 struct tab *tab = current_tab();
789 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
790 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
791 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
792 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
796 cmd_list_bookmarks(struct buffer *buffer)
798 load_url_in_tab(current_tab(), "about:bookmarks");
802 cmd_toggle_help(struct buffer *buffer)
804 side_window = !side_window;
809 * ugly hack, but otherwise the window doesn't get updated
810 * until I call handle_resize a second time (i.e. C-l). I
811 * will be happy to know why something like this is needed.
813 handle_resize_nodelay(0, 0, NULL);
814 handle_resize_nodelay(0, 0, NULL);
818 cmd_mini_delete_char(struct buffer *buffer)
822 if (!in_minibuffer) {
823 message("text is read-only");
827 minibuffer_taint_hist();
829 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
834 memmove(c, n, strlen(n)+1);
838 cmd_mini_delete_backward_char(struct buffer *buffer)
842 if (!in_minibuffer) {
843 message("text is read-only");
847 minibuffer_taint_hist();
849 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
850 start = buffer->current_line->line;
853 p = utf8_prev_cp(c-1, start);
855 memmove(p, c, strlen(c)+1);
860 cmd_mini_kill_line(struct buffer *buffer)
864 if (!in_minibuffer) {
865 message("text is read-only");
869 minibuffer_taint_hist();
870 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
875 cmd_mini_abort(struct buffer *buffer)
884 cmd_mini_complete_and_exit(struct buffer *buffer)
889 minibuffer_taint_hist();
894 cmd_mini_previous_history_element(struct buffer *buffer)
896 if (ministate.history == NULL) {
897 message("No history");
901 if (ministate.hist_cur == NULL ||
902 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
903 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
904 ministate.hist_off = ministate.history->len - 1;
905 if (ministate.hist_cur == NULL)
906 message("No prev item");
908 ministate.hist_off--;
911 if (ministate.hist_cur != NULL)
912 buffer->current_line->line = ministate.hist_cur->h;
916 cmd_mini_next_history_element(struct buffer *buffer)
918 if (ministate.history == NULL) {
919 message("No history");
923 if (ministate.hist_cur == NULL ||
924 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
925 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
926 ministate.hist_off = 0;
927 if (ministate.hist_cur == NULL)
928 message("No next item");
930 ministate.hist_off++;
933 if (ministate.hist_cur != NULL)
934 buffer->current_line->line = ministate.hist_cur->h;
938 global_key_unbound(void)
940 message("%s is undefined", keybuf);
944 minibuffer_hist_save_entry(void)
948 if (ministate.history == NULL)
951 if ((hist = calloc(1, sizeof(*hist))) == NULL)
954 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
956 if (TAILQ_EMPTY(&ministate.history->head))
957 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
959 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
960 ministate.history->len++;
964 * taint the minibuffer cache: if we're currently showing a history
965 * element, copy that to the current buf and reset the "history
969 minibuffer_taint_hist(void)
971 if (ministate.hist_cur == NULL)
974 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
975 ministate.hist_cur = NULL;
979 minibuffer_self_insert(void)
981 char *c, tmp[5] = {0};
984 minibuffer_taint_hist();
989 len = utf8_encode(thiskey.cp, tmp);
990 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
991 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
994 memmove(c + len, c, strlen(c)+1);
996 ministate.buffer.cpoff++;
1000 eecmd_self_insert(void)
1002 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1003 !unicode_isgraph(thiskey.cp)) {
1004 global_key_unbound();
1008 minibuffer_self_insert();
1016 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1017 if (!strcmp(cmd->cmd, ministate.buf)) {
1019 minibuffer_hist_save_entry();
1020 cmd->fn(current_buffer());
1025 message("No match");
1029 ir_self_insert(void)
1031 minibuffer_self_insert();
1037 char buf[1025] = {0};
1038 struct phos_uri uri;
1041 tab = current_tab();
1044 minibuffer_hist_save_entry();
1046 /* a bit ugly but... */
1047 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1048 phos_uri_set_query(&uri, ministate.buf);
1049 phos_serialize_uri(&uri, buf, sizeof(buf));
1050 load_url_in_tab(tab, buf);
1054 lu_self_insert(void)
1056 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1057 !unicode_isgraph(thiskey.key)) {
1058 global_key_unbound();
1062 minibuffer_self_insert();
1069 minibuffer_hist_save_entry();
1070 load_url_in_tab(current_tab(), ministate.buf);
1077 if (*ministate.buf != '\0')
1078 add_to_bookmarks(ministate.buf);
1084 yornp_self_insert(void)
1086 if (thiskey.key != 'y' && thiskey.key != 'n') {
1087 message("Please answer y or n");
1092 yornp_cb(thiskey.key == 'y', yornp_data);
1099 yornp_cb(0, yornp_data);
1103 read_self_insert(void)
1105 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1106 global_key_unbound();
1110 minibuffer_self_insert();
1117 read_cb(NULL, read_data);
1124 minibuffer_hist_save_entry();
1125 read_cb(ministate.buf, read_data);
1128 static struct vline *
1129 nth_line(struct buffer *buffer, size_t n)
1135 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1150 TAILQ_FOREACH(t, &tabshead, tabs) {
1151 if (t->flags & TAB_CURRENT)
1159 static struct buffer *
1160 current_buffer(void)
1163 return &ministate.buffer;
1164 return ¤t_tab()->buffer;
1172 if ((thiskey.key = wgetch(body)) == ERR)
1175 thiskey.meta = thiskey.key == 27;
1177 thiskey.key = wgetch(body);
1178 if (thiskey.key == ERR || thiskey.key == 27) {
1185 if ((unsigned int)thiskey.key < UINT8_MAX) {
1187 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1189 if ((thiskey.key = wgetch(body)) == ERR) {
1190 message("Error decoding user input");
1200 dispatch_stdio(int fd, short ev, void *d)
1203 const char *keyname;
1209 if (keybuf[0] != '\0')
1210 strlcat(keybuf, " ", sizeof(keybuf));
1212 strlcat(keybuf, "M-", sizeof(keybuf));
1213 if (thiskey.cp != 0) {
1214 utf8_encode(thiskey.cp, tmp);
1215 strlcat(keybuf, tmp, sizeof(keybuf));
1217 if ((keyname = unkbd(thiskey.key)) != NULL)
1218 strlcat(keybuf, keyname, sizeof(keybuf));
1220 tmp[0] = thiskey.key;
1221 strlcat(keybuf, tmp, sizeof(keybuf));
1225 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1226 if (k->meta == thiskey.meta &&
1227 k->key == thiskey.key) {
1229 current_map = &k->map;
1231 current_map = base_map;
1232 strlcpy(keybuf, "", sizeof(keybuf));
1233 k->fn(current_buffer());
1239 if (current_map->unhandled_input != NULL)
1240 current_map->unhandled_input();
1242 global_key_unbound();
1245 strlcpy(keybuf, "", sizeof(keybuf));
1246 current_map = base_map;
1252 redraw_tab(current_tab());
1256 handle_clear_minibuf(int fd, short ev, void *d)
1258 free(ministate.curmesg);
1259 ministate.curmesg = NULL;
1261 redraw_minibuffer();
1262 if (in_minibuffer) {
1272 handle_resize(int sig, short ev, void *d)
1274 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1275 event_del(&resizeev);
1277 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1278 evtimer_add(&resizeev, &resize_timer);
1282 handle_resize_nodelay(int s, short ev, void *d)
1290 /* move and resize the windows, in reverse order! */
1292 mvwin(minibuf, LINES-1, 0);
1293 wresize(minibuf, 1, COLS);
1295 mvwin(modeline, LINES-2, 0);
1296 wresize(modeline, 1, COLS);
1298 body_lines = LINES-3;
1302 help_cols = 0.3 * COLS;
1303 help_lines = LINES-3;
1305 wresize(help, help_lines, help_cols);
1307 wrap_page(&helpwin, help_cols);
1309 body_cols = COLS - help_cols - 1;
1310 mvwin(body, 1, help_cols);
1314 wresize(body, body_lines, body_cols);
1316 wresize(tabline, 1, COLS);
1318 tab = current_tab();
1320 wrap_page(&tab->buffer, body_cols);
1325 wrap_page(struct buffer *buffer, int width)
1328 const struct line *orig;
1332 orig = buffer->current_line == NULL
1334 : buffer->current_line->parent;
1335 buffer->current_line = NULL;
1338 buffer->line_off = 0;
1340 empty_vlist(buffer);
1342 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1343 prfx = line_prefixes[l->type].prfx1;
1352 case LINE_PRE_START:
1354 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1356 case LINE_PRE_CONTENT:
1357 hardwrap_text(buffer, l, width);
1361 if (orig == l && buffer->current_line == NULL) {
1362 buffer->line_off = buffer->line_max-1;
1363 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1366 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1367 if (vl == NULL || vl->parent != orig)
1369 buffer->current_line = vl;
1375 if (buffer->current_line == NULL)
1376 buffer->current_line = TAILQ_FIRST(&buffer->head);
1382 print_vline(WINDOW *window, struct vline *vl)
1384 const char *text = vl->line;
1386 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1387 int text_face = line_faces[vl->parent->type].text_prop;
1390 prfx = line_prefixes[vl->parent->type].prfx1;
1392 prfx = line_prefixes[vl->parent->type].prfx2;
1397 wattron(window, prefix_face);
1398 wprintw(window, "%s", prfx);
1399 wattroff(window, prefix_face);
1401 wattron(window, text_face);
1402 wprintw(window, "%s", text);
1403 wattroff(window, text_face);
1407 redraw_tabline(void)
1410 size_t toskip, ots, tabwidth, space, x;
1411 int current, y, truncated;
1415 tabwidth = sizeof(buf)+1;
1419 TAILQ_FOREACH(tab, &tabshead, tabs) {
1421 if (tab->flags & TAB_CURRENT)
1425 if (toskip * tabwidth < space)
1430 while (toskip != 0 &&
1431 (ots - toskip+1) * tabwidth < space)
1436 wattron(tabline, tab_face.background);
1437 wprintw(tabline, toskip == 0 ? " " : "<");
1438 wattroff(tabline, tab_face.background);
1441 TAILQ_FOREACH(tab, &tabshead, tabs) {
1449 getyx(tabline, y, x);
1450 if (x + sizeof(buf)+2 >= (size_t)COLS)
1453 current = tab->flags & TAB_CURRENT;
1455 if (*(title = tab->buffer.page.title) == '\0')
1456 title = tab->hist_cur->h;
1458 if (tab->flags & TAB_URGENT)
1459 strlcpy(buf, "!", sizeof(buf));
1461 strlcpy(buf, " ", sizeof(buf));
1463 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1464 /* truncation happens */
1465 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1467 /* pad with spaces */
1468 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1473 wattron(tabline, tab_face.current_tab);
1475 wattron(tabline, tab_face.tab);
1477 wprintw(tabline, "%s", buf);
1478 if (TAILQ_NEXT(tab, tabs) != NULL)
1479 wprintw(tabline, " ");
1482 wattroff(tabline, tab_face.current_tab);
1484 wattroff(tabline, tab_face.tab);
1487 wattron(tabline, tab_face.background);
1488 for (; x < (size_t)COLS; ++x)
1489 waddch(tabline, ' ');
1491 mvwprintw(tabline, 0, COLS-1, ">");
1495 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1502 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1503 if (TAILQ_EMPTY(&buffer->head))
1507 vl = nth_line(buffer, buffer->line_off);
1508 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1510 print_vline(win, vl);
1516 wmove(win, buffer->curs_y, buffer->curs_x);
1522 redraw_window(help, help_lines, &helpwin);
1526 redraw_body(struct tab *tab)
1528 redraw_window(body, body_lines, &tab->buffer);
1532 trust_status_char(enum trust_state ts)
1535 case TS_UNKNOWN: return 'u';
1536 case TS_UNTRUSTED: return '!';
1537 case TS_TRUSTED: return 'v';
1538 case TS_VERIFIED: return 'V';
1543 redraw_modeline(struct tab *tab)
1546 int x, y, max_x, max_y;
1547 const char *mode = tab->buffer.page.name;
1548 const char *spin = "-\\|/";
1551 wattron(modeline, modeline_face.background);
1552 wmove(modeline, 0, 0);
1554 wprintw(modeline, "-%c%c %s ",
1555 spin[tab->loading_anim_step],
1556 trust_status_char(tab->trust),
1557 mode == NULL ? "(none)" : mode);
1559 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1561 if (tab->buffer.line_max <= (size_t)body_lines)
1562 wprintw(modeline, "All ");
1563 else if (tab->buffer.line_off == 0)
1564 wprintw(modeline, "Top ");
1565 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1566 wprintw(modeline, "Bottom ");
1568 wprintw(modeline, "%.0f%% ", pct);
1570 wprintw(modeline, "%d/%d %s ",
1571 tab->buffer.line_off + tab->buffer.curs_y,
1572 tab->buffer.line_max,
1575 getyx(modeline, y, x);
1576 getmaxyx(modeline, max_y, max_x);
1581 for (; x < max_x; ++x)
1582 waddstr(modeline, "-");
1584 wattroff(modeline, modeline_face.background);
1588 redraw_minibuffer(void)
1591 size_t off_y, off_x = 0;
1594 wattron(minibuf, minibuffer_face.background);
1597 if (in_minibuffer) {
1598 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1599 if (ministate.hist_cur != NULL)
1600 wprintw(minibuf, "(%zu/%zu) ",
1601 ministate.hist_off + 1,
1602 ministate.history->len);
1604 getyx(minibuf, off_y, off_x);
1606 start = ministate.hist_cur != NULL
1607 ? ministate.hist_cur->h
1609 c = utf8_nth(ministate.buffer.current_line->line,
1610 ministate.buffer.cpoff);
1611 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1612 start = utf8_next_cp(start);
1615 waddstr(minibuf, start);
1618 if (ministate.curmesg != NULL)
1619 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1622 if (!in_minibuffer && ministate.curmesg == NULL)
1623 waddstr(minibuf, keybuf);
1625 /* If nothing else, show the URL at point */
1626 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1627 tab = current_tab();
1628 if (tab->buffer.current_line != NULL &&
1629 tab->buffer.current_line->parent->type == LINE_LINK)
1630 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1634 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1636 wattroff(minibuf, minibuffer_face.background);
1640 redraw_tab(struct tab *tab)
1649 redraw_modeline(tab);
1650 redraw_minibuffer();
1652 wnoutrefresh(tabline);
1653 wnoutrefresh(modeline);
1655 if (in_minibuffer) {
1657 wnoutrefresh(minibuf);
1659 wnoutrefresh(minibuf);
1667 emit_help_item(char *prfx, void *fn)
1672 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1676 assert(cmd != NULL);
1678 if ((l = calloc(1, sizeof(*l))) == NULL)
1681 l->type = LINE_TEXT;
1684 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1686 if (TAILQ_EMPTY(&helpwin.page.head))
1687 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1689 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1693 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1699 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1700 strlcpy(p, prfx, sizeof(p));
1702 strlcat(p, " ", sizeof(p));
1704 strlcat(p, "M-", sizeof(p));
1705 if ((kn = unkbd(k->key)) != NULL)
1706 strlcat(p, kn, sizeof(p));
1708 strlcat(p, keyname(k->key), sizeof(p));
1711 rec_compute_help(&k->map, p, sizeof(p));
1713 emit_help_item(p, k->fn);
1718 recompute_help(void)
1722 empty_vlist(&helpwin);
1723 empty_linelist(&helpwin);
1724 rec_compute_help(current_map, p, sizeof(p));
1725 wrap_page(&helpwin, help_cols);
1729 vmessage(const char *fmt, va_list ap)
1731 if (evtimer_pending(&clminibufev, NULL))
1732 evtimer_del(&clminibufev);
1733 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1734 evtimer_add(&clminibufev, &clminibufev_timer);
1736 free(ministate.curmesg);
1738 /* TODO: what to do if the allocation fails here? */
1739 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1740 ministate.curmesg = NULL;
1742 redraw_minibuffer();
1743 if (in_minibuffer) {
1753 message(const char *fmt, ...)
1763 start_loading_anim(struct tab *tab)
1765 if (tab->loading_anim)
1767 tab->loading_anim = 1;
1768 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1769 evtimer_add(&tab->loadingev, &loadingev_timer);
1773 update_loading_anim(int fd, short ev, void *d)
1775 struct tab *tab = d;
1777 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1779 if (tab->flags & TAB_CURRENT) {
1780 redraw_modeline(tab);
1787 evtimer_add(&tab->loadingev, &loadingev_timer);
1791 stop_loading_anim(struct tab *tab)
1793 if (!tab->loading_anim)
1795 evtimer_del(&tab->loadingev);
1796 tab->loading_anim = 0;
1797 tab->loading_anim_step = 0;
1799 if (!(tab->flags & TAB_CURRENT))
1802 redraw_modeline(tab);
1811 load_url_in_tab(struct tab *tab, const char *url)
1813 message("Loading %s...", url);
1814 start_loading_anim(tab);
1817 tab->buffer.curs_x = 0;
1818 tab->buffer.curs_y = 0;
1823 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1824 void (*abortfn)(void), struct histhead *hist)
1827 base_map = &minibuffer_map;
1828 current_map = &minibuffer_map;
1830 base_map->unhandled_input = self_insert_fn;
1832 ministate.donefn = donefn;
1833 ministate.abortfn = abortfn;
1834 memset(ministate.buf, 0, sizeof(ministate.buf));
1835 ministate.buffer.current_line = &ministate.vline;
1836 ministate.buffer.current_line->line = ministate.buf;
1837 ministate.buffer.cpoff = 0;
1838 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1840 ministate.history = hist;
1841 ministate.hist_cur = NULL;
1842 ministate.hist_off = 0;
1846 exit_minibuffer(void)
1851 base_map = &global_map;
1852 current_map = &global_map;
1856 switch_to_tab(struct tab *tab)
1860 TAILQ_FOREACH(t, &tabshead, tabs) {
1861 t->flags &= ~TAB_CURRENT;
1864 tab->flags |= TAB_CURRENT;
1865 tab->flags &= ~TAB_URGENT;
1871 return tab_counter++;
1875 new_tab(const char *url)
1879 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1885 TAILQ_INIT(&tab->hist.head);
1887 TAILQ_INIT(&tab->buffer.head);
1889 tab->id = tab_new_id();
1892 if (TAILQ_EMPTY(&tabshead))
1893 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1895 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1897 load_url_in_tab(tab, url);
1902 session_new_tab_cb(const char *url)
1910 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1911 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1915 ui_init(int argc, char * const *argv)
1917 char path[PATH_MAX];
1918 const char *url = NEW_TAB_URL;
1919 int ch, configtest = 0, fonf = 0;
1921 strlcpy(path, getenv("HOME"), sizeof(path));
1922 strlcat(path, "/.telescope/config", sizeof(path));
1924 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1928 strlcpy(path, optarg, sizeof(path));
1944 parseconfig(path, fonf);
1953 setlocale(LC_ALL, "");
1955 TAILQ_INIT(&global_map.m);
1956 global_map.unhandled_input = global_key_unbound;
1958 TAILQ_INIT(&minibuffer_map.m);
1960 TAILQ_INIT(&eecmd_history.head);
1961 TAILQ_INIT(&ir_history.head);
1962 TAILQ_INIT(&lu_history.head);
1964 ministate.line.type = LINE_TEXT;
1965 ministate.vline.parent = &ministate.line;
1966 ministate.buffer.current_line = &ministate.vline;
1968 /* initialize help window */
1969 TAILQ_INIT(&helpwin.head);
1971 base_map = &global_map;
1972 current_map = &global_map;
1973 load_default_keys();
1980 intrflush(stdscr, FALSE);
1982 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1984 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1986 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1988 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1990 if ((help = newwin(1, 1, 1, 0)) == NULL)
1993 body_lines = LINES-3;
1997 scrollok(body, TRUE);
1999 /* non-blocking input */
2002 mvwprintw(body, 0, 0, "");
2004 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2005 event_add(&stdioev, NULL);
2007 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2008 signal_add(&winchev, NULL);
2010 load_last_session(session_new_tab_cb);
2011 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2018 ui_on_tab_loaded(struct tab *tab)
2020 stop_loading_anim(tab);
2021 message("Loaded %s", tab->hist_cur->h);
2032 ui_on_tab_refresh(struct tab *tab)
2034 wrap_page(&tab->buffer, body_cols);
2035 if (tab->flags & TAB_CURRENT) {
2036 restore_cursor(&tab->buffer);
2039 tab->flags |= TAB_URGENT;
2043 ui_require_input(struct tab *tab, int hide)
2045 /* TODO: hard-switching to another tab is ugly */
2048 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2050 strlcpy(ministate.prompt, "Input required: ",
2051 sizeof(ministate.prompt));
2056 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2061 if (in_minibuffer) {
2068 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2071 len = sizeof(ministate.prompt);
2072 strlcpy(ministate.prompt, prompt, len);
2073 strlcat(ministate.prompt, " (y or n) ", len);
2074 redraw_tab(current_tab());
2078 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2088 enter_minibuffer(read_self_insert, read_select, read_abort,
2091 len = sizeof(ministate.prompt);
2092 strlcpy(ministate.prompt, prompt, len);
2093 strlcat(ministate.prompt, ": ", len);
2094 redraw_tab(current_tab());
2098 ui_notify(const char *fmt, ...)