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)
683 new_tab(NEW_TAB_URL);
687 cmd_tab_next(struct buffer *buffer)
692 tab->flags &= ~TAB_CURRENT;
694 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
695 t = TAILQ_FIRST(&tabshead);
696 t->flags |= TAB_CURRENT;
697 t->flags &= ~TAB_URGENT;
701 cmd_tab_previous(struct buffer *buffer)
706 tab->flags &= ~TAB_CURRENT;
708 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
709 t = TAILQ_LAST(&tabshead, tabshead);
710 t->flags |= TAB_CURRENT;
711 t->flags &= ~TAB_URGENT;
715 cmd_tab_move(struct buffer *buffer)
720 t = TAILQ_NEXT(tab, tabs);
721 TAILQ_REMOVE(&tabshead, tab, tabs);
724 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
726 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
730 cmd_tab_move_to(struct buffer *buffer)
735 t = TAILQ_PREV(tab, tabshead, tabs);
736 TAILQ_REMOVE(&tabshead, tab, tabs);
739 if (TAILQ_EMPTY(&tabshead))
740 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
742 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
744 TAILQ_INSERT_BEFORE(t, tab, tabs);
748 cmd_load_url(struct buffer *buffer)
751 message("We don't have enable-recursive-minibuffers");
755 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
757 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
758 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
759 cmd_move_end_of_line(&ministate.buffer);
763 cmd_load_current_url(struct buffer *buffer)
765 struct tab *tab = current_tab();
768 message("We don't have enable-recursive-minibuffers");
772 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
774 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
775 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
776 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
780 cmd_bookmark_page(struct buffer *buffer)
782 struct tab *tab = current_tab();
784 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
785 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
786 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
787 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
791 cmd_list_bookmarks(struct buffer *buffer)
793 load_url_in_tab(current_tab(), "about:bookmarks");
797 cmd_toggle_help(struct buffer *buffer)
799 side_window = !side_window;
804 * ugly hack, but otherwise the window doesn't get updated
805 * until I call handle_resize a second time (i.e. C-l). I
806 * will be happy to know why something like this is needed.
808 handle_resize_nodelay(0, 0, NULL);
809 handle_resize_nodelay(0, 0, NULL);
813 cmd_mini_delete_char(struct buffer *buffer)
817 if (!in_minibuffer) {
818 message("text is read-only");
822 minibuffer_taint_hist();
824 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
829 memmove(c, n, strlen(n)+1);
833 cmd_mini_delete_backward_char(struct buffer *buffer)
837 if (!in_minibuffer) {
838 message("text is read-only");
842 minibuffer_taint_hist();
844 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
845 start = buffer->current_line->line;
848 p = utf8_prev_cp(c-1, start);
850 memmove(p, c, strlen(c)+1);
855 cmd_mini_kill_line(struct buffer *buffer)
859 if (!in_minibuffer) {
860 message("text is read-only");
864 minibuffer_taint_hist();
865 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
870 cmd_mini_abort(struct buffer *buffer)
879 cmd_mini_complete_and_exit(struct buffer *buffer)
884 minibuffer_taint_hist();
889 cmd_mini_previous_history_element(struct buffer *buffer)
891 if (ministate.history == NULL) {
892 message("No history");
896 if (ministate.hist_cur == NULL ||
897 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
898 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
899 ministate.hist_off = ministate.history->len - 1;
900 if (ministate.hist_cur == NULL)
901 message("No prev item");
903 ministate.hist_off--;
906 if (ministate.hist_cur != NULL)
907 buffer->current_line->line = ministate.hist_cur->h;
911 cmd_mini_next_history_element(struct buffer *buffer)
913 if (ministate.history == NULL) {
914 message("No history");
918 if (ministate.hist_cur == NULL ||
919 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
920 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
921 ministate.hist_off = 0;
922 if (ministate.hist_cur == NULL)
923 message("No next item");
925 ministate.hist_off++;
928 if (ministate.hist_cur != NULL)
929 buffer->current_line->line = ministate.hist_cur->h;
933 global_key_unbound(void)
935 message("%s is undefined", keybuf);
939 minibuffer_hist_save_entry(void)
943 if (ministate.history == NULL)
946 if ((hist = calloc(1, sizeof(*hist))) == NULL)
949 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
951 if (TAILQ_EMPTY(&ministate.history->head))
952 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
954 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
955 ministate.history->len++;
959 * taint the minibuffer cache: if we're currently showing a history
960 * element, copy that to the current buf and reset the "history
964 minibuffer_taint_hist(void)
966 if (ministate.hist_cur == NULL)
969 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
970 ministate.hist_cur = NULL;
974 minibuffer_self_insert(void)
976 char *c, tmp[5] = {0};
979 minibuffer_taint_hist();
984 len = utf8_encode(thiskey.cp, tmp);
985 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
986 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
989 memmove(c + len, c, strlen(c)+1);
991 ministate.buffer.cpoff++;
995 eecmd_self_insert(void)
997 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
998 !unicode_isgraph(thiskey.cp)) {
999 global_key_unbound();
1003 minibuffer_self_insert();
1011 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1012 if (!strcmp(cmd->cmd, ministate.buf)) {
1014 minibuffer_hist_save_entry();
1015 cmd->fn(current_buffer());
1020 message("No match");
1024 ir_self_insert(void)
1026 minibuffer_self_insert();
1032 char buf[1025] = {0};
1033 struct phos_uri uri;
1036 tab = current_tab();
1039 minibuffer_hist_save_entry();
1041 /* a bit ugly but... */
1042 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1043 phos_uri_set_query(&uri, ministate.buf);
1044 phos_serialize_uri(&uri, buf, sizeof(buf));
1045 load_url_in_tab(tab, buf);
1049 lu_self_insert(void)
1051 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1052 !unicode_isgraph(thiskey.key)) {
1053 global_key_unbound();
1057 minibuffer_self_insert();
1064 minibuffer_hist_save_entry();
1065 load_url_in_tab(current_tab(), ministate.buf);
1072 if (*ministate.buf != '\0')
1073 add_to_bookmarks(ministate.buf);
1079 yornp_self_insert(void)
1081 if (thiskey.key != 'y' && thiskey.key != 'n') {
1082 message("Please answer y or n");
1087 yornp_cb(thiskey.key == 'y', yornp_data);
1094 yornp_cb(0, yornp_data);
1098 read_self_insert(void)
1100 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1101 global_key_unbound();
1105 minibuffer_self_insert();
1112 read_cb(NULL, read_data);
1119 minibuffer_hist_save_entry();
1120 read_cb(ministate.buf, read_data);
1123 static struct vline *
1124 nth_line(struct buffer *buffer, size_t n)
1130 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1145 TAILQ_FOREACH(t, &tabshead, tabs) {
1146 if (t->flags & TAB_CURRENT)
1154 static struct buffer *
1155 current_buffer(void)
1158 return &ministate.buffer;
1159 return ¤t_tab()->buffer;
1167 if ((thiskey.key = wgetch(body)) == ERR)
1170 thiskey.meta = thiskey.key == 27;
1172 thiskey.key = wgetch(body);
1173 if (thiskey.key == ERR || thiskey.key == 27) {
1180 if ((unsigned int)thiskey.key < UINT8_MAX) {
1182 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1184 if ((thiskey.key = wgetch(body)) == ERR) {
1185 message("Error decoding user input");
1195 dispatch_stdio(int fd, short ev, void *d)
1198 const char *keyname;
1204 if (keybuf[0] != '\0')
1205 strlcat(keybuf, " ", sizeof(keybuf));
1207 strlcat(keybuf, "M-", sizeof(keybuf));
1208 if (thiskey.cp != 0) {
1209 utf8_encode(thiskey.cp, tmp);
1210 strlcat(keybuf, tmp, sizeof(keybuf));
1212 if ((keyname = unkbd(thiskey.key)) != NULL)
1213 strlcat(keybuf, keyname, sizeof(keybuf));
1215 tmp[0] = thiskey.key;
1216 strlcat(keybuf, tmp, sizeof(keybuf));
1220 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1221 if (k->meta == thiskey.meta &&
1222 k->key == thiskey.key) {
1224 current_map = &k->map;
1226 current_map = base_map;
1227 strlcpy(keybuf, "", sizeof(keybuf));
1228 k->fn(current_buffer());
1234 if (current_map->unhandled_input != NULL)
1235 current_map->unhandled_input();
1237 global_key_unbound();
1240 strlcpy(keybuf, "", sizeof(keybuf));
1241 current_map = base_map;
1247 redraw_tab(current_tab());
1251 handle_clear_minibuf(int fd, short ev, void *d)
1253 free(ministate.curmesg);
1254 ministate.curmesg = NULL;
1256 redraw_minibuffer();
1257 if (in_minibuffer) {
1267 handle_resize(int sig, short ev, void *d)
1269 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1270 event_del(&resizeev);
1272 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1273 evtimer_add(&resizeev, &resize_timer);
1277 handle_resize_nodelay(int s, short ev, void *d)
1285 /* move and resize the windows, in reverse order! */
1287 mvwin(minibuf, LINES-1, 0);
1288 wresize(minibuf, 1, COLS);
1290 mvwin(modeline, LINES-2, 0);
1291 wresize(modeline, 1, COLS);
1293 body_lines = LINES-3;
1297 help_cols = 0.3 * COLS;
1298 help_lines = LINES-3;
1300 wresize(help, help_lines, help_cols);
1302 wrap_page(&helpwin, help_cols);
1304 body_cols = COLS - help_cols - 1;
1305 mvwin(body, 1, help_cols);
1309 wresize(body, body_lines, body_cols);
1311 wresize(tabline, 1, COLS);
1313 tab = current_tab();
1315 wrap_page(&tab->buffer, body_cols);
1320 wrap_page(struct buffer *buffer, int width)
1323 const struct line *orig;
1327 orig = buffer->current_line == NULL
1329 : buffer->current_line->parent;
1330 buffer->current_line = NULL;
1333 buffer->line_off = 0;
1335 empty_vlist(buffer);
1337 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1338 prfx = line_prefixes[l->type].prfx1;
1347 case LINE_PRE_START:
1349 wrap_text(buffer, prfx, l, width);
1351 case LINE_PRE_CONTENT:
1352 hardwrap_text(buffer, l, width);
1356 if (orig == l && buffer->current_line == NULL) {
1357 buffer->line_off = buffer->line_max-1;
1358 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1361 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1362 if (vl == NULL || vl->parent != orig)
1364 buffer->current_line = vl;
1370 if (buffer->current_line == NULL)
1371 buffer->current_line = TAILQ_FIRST(&buffer->head);
1377 print_vline(WINDOW *window, struct vline *vl)
1379 const char *text = vl->line;
1381 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1382 int text_face = line_faces[vl->parent->type].text_prop;
1385 prfx = line_prefixes[vl->parent->type].prfx1;
1387 prfx = line_prefixes[vl->parent->type].prfx2;
1392 wattron(window, prefix_face);
1393 wprintw(window, "%s", prfx);
1394 wattroff(window, prefix_face);
1396 wattron(window, text_face);
1397 wprintw(window, "%s", text);
1398 wattroff(window, text_face);
1402 redraw_tabline(void)
1405 size_t toskip, ots, tabwidth, space, x;
1406 int current, y, truncated;
1410 tabwidth = sizeof(buf)+1;
1414 TAILQ_FOREACH(tab, &tabshead, tabs) {
1416 if (tab->flags & TAB_CURRENT)
1420 if (toskip * tabwidth < space)
1425 while (toskip != 0 &&
1426 (ots - toskip+1) * tabwidth < space)
1431 wattron(tabline, tab_face.background);
1432 wprintw(tabline, toskip == 0 ? " " : "<");
1433 wattroff(tabline, tab_face.background);
1436 TAILQ_FOREACH(tab, &tabshead, tabs) {
1444 getyx(tabline, y, x);
1445 if (x + sizeof(buf)+2 >= (size_t)COLS)
1448 current = tab->flags & TAB_CURRENT;
1450 if (*(title = tab->buffer.page.title) == '\0')
1451 title = tab->hist_cur->h;
1453 if (tab->flags & TAB_URGENT)
1454 strlcpy(buf, "!", sizeof(buf));
1456 strlcpy(buf, " ", sizeof(buf));
1458 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1459 /* truncation happens */
1460 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1462 /* pad with spaces */
1463 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1468 wattron(tabline, tab_face.current_tab);
1470 wattron(tabline, tab_face.tab);
1472 wprintw(tabline, "%s", buf);
1473 if (TAILQ_NEXT(tab, tabs) != NULL)
1474 wprintw(tabline, " ");
1477 wattroff(tabline, tab_face.current_tab);
1479 wattroff(tabline, tab_face.tab);
1482 wattron(tabline, tab_face.background);
1483 for (; x < (size_t)COLS; ++x)
1484 waddch(tabline, ' ');
1486 mvwprintw(tabline, 0, COLS-1, ">");
1490 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1497 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1498 if (TAILQ_EMPTY(&buffer->head))
1502 vl = nth_line(buffer, buffer->line_off);
1503 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1505 print_vline(win, vl);
1511 wmove(win, buffer->curs_y, buffer->curs_x);
1517 redraw_window(help, help_lines, &helpwin);
1521 redraw_body(struct tab *tab)
1523 redraw_window(body, body_lines, &tab->buffer);
1527 trust_status_char(enum trust_state ts)
1530 case TS_UNKNOWN: return 'u';
1531 case TS_UNTRUSTED: return '!';
1532 case TS_TRUSTED: return 'v';
1533 case TS_VERIFIED: return 'V';
1538 redraw_modeline(struct tab *tab)
1541 int x, y, max_x, max_y;
1542 const char *mode = tab->buffer.page.name;
1543 const char *spin = "-\\|/";
1546 wattron(modeline, A_REVERSE);
1547 wmove(modeline, 0, 0);
1549 wprintw(modeline, "-%c%c %s ",
1550 spin[tab->loading_anim_step],
1551 trust_status_char(tab->trust),
1552 mode == NULL ? "(none)" : mode);
1554 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1556 if (tab->buffer.line_max <= (size_t)body_lines)
1557 wprintw(modeline, "All ");
1558 else if (tab->buffer.line_off == 0)
1559 wprintw(modeline, "Top ");
1560 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1561 wprintw(modeline, "Bottom ");
1563 wprintw(modeline, "%.0f%% ", pct);
1565 wprintw(modeline, "%d/%d %s ",
1566 tab->buffer.line_off + tab->buffer.curs_y,
1567 tab->buffer.line_max,
1570 getyx(modeline, y, x);
1571 getmaxyx(modeline, max_y, max_x);
1576 for (; x < max_x; ++x)
1577 waddstr(modeline, "-");
1581 redraw_minibuffer(void)
1584 size_t off_y, off_x = 0;
1589 if (in_minibuffer) {
1590 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1591 if (ministate.hist_cur != NULL)
1592 wprintw(minibuf, "(%zu/%zu) ",
1593 ministate.hist_off + 1,
1594 ministate.history->len);
1596 getyx(minibuf, off_y, off_x);
1598 start = ministate.hist_cur != NULL
1599 ? ministate.hist_cur->h
1601 c = utf8_nth(ministate.buffer.current_line->line,
1602 ministate.buffer.cpoff);
1603 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1604 start = utf8_next_cp(start);
1607 waddstr(minibuf, start);
1610 if (ministate.curmesg != NULL)
1611 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1614 if (!in_minibuffer && ministate.curmesg == NULL)
1615 waddstr(minibuf, keybuf);
1617 /* If nothing else, show the URL at point */
1618 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1619 tab = current_tab();
1620 if (tab->buffer.current_line != NULL &&
1621 tab->buffer.current_line->parent->type == LINE_LINK)
1622 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1626 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1630 redraw_tab(struct tab *tab)
1639 redraw_modeline(tab);
1640 redraw_minibuffer();
1642 wnoutrefresh(tabline);
1643 wnoutrefresh(modeline);
1645 if (in_minibuffer) {
1647 wnoutrefresh(minibuf);
1649 wnoutrefresh(minibuf);
1657 emit_help_item(char *prfx, void *fn)
1662 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1666 assert(cmd != NULL);
1668 if ((l = calloc(1, sizeof(*l))) == NULL)
1671 l->type = LINE_TEXT;
1674 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1676 if (TAILQ_EMPTY(&helpwin.page.head))
1677 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1679 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1683 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1689 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1690 strlcpy(p, prfx, sizeof(p));
1692 strlcat(p, " ", sizeof(p));
1694 strlcat(p, "M-", sizeof(p));
1695 if ((kn = unkbd(k->key)) != NULL)
1696 strlcat(p, kn, sizeof(p));
1698 strlcat(p, keyname(k->key), sizeof(p));
1701 rec_compute_help(&k->map, p, sizeof(p));
1703 emit_help_item(p, k->fn);
1708 recompute_help(void)
1712 empty_vlist(&helpwin);
1713 empty_linelist(&helpwin);
1714 rec_compute_help(current_map, p, sizeof(p));
1715 wrap_page(&helpwin, help_cols);
1719 vmessage(const char *fmt, va_list ap)
1721 if (evtimer_pending(&clminibufev, NULL))
1722 evtimer_del(&clminibufev);
1723 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1724 evtimer_add(&clminibufev, &clminibufev_timer);
1726 free(ministate.curmesg);
1728 /* TODO: what to do if the allocation fails here? */
1729 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1730 ministate.curmesg = NULL;
1732 redraw_minibuffer();
1733 if (in_minibuffer) {
1743 message(const char *fmt, ...)
1753 start_loading_anim(struct tab *tab)
1755 if (tab->loading_anim)
1757 tab->loading_anim = 1;
1758 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1759 evtimer_add(&tab->loadingev, &loadingev_timer);
1763 update_loading_anim(int fd, short ev, void *d)
1765 struct tab *tab = d;
1767 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1769 if (tab->flags & TAB_CURRENT) {
1770 redraw_modeline(tab);
1777 evtimer_add(&tab->loadingev, &loadingev_timer);
1781 stop_loading_anim(struct tab *tab)
1783 if (!tab->loading_anim)
1785 evtimer_del(&tab->loadingev);
1786 tab->loading_anim = 0;
1787 tab->loading_anim_step = 0;
1789 if (!(tab->flags & TAB_CURRENT))
1792 redraw_modeline(tab);
1801 load_url_in_tab(struct tab *tab, const char *url)
1803 message("Loading %s...", url);
1804 start_loading_anim(tab);
1807 tab->buffer.curs_x = 0;
1808 tab->buffer.curs_y = 0;
1813 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1814 void (*abortfn)(void), struct histhead *hist)
1817 base_map = &minibuffer_map;
1818 current_map = &minibuffer_map;
1820 base_map->unhandled_input = self_insert_fn;
1822 ministate.donefn = donefn;
1823 ministate.abortfn = abortfn;
1824 memset(ministate.buf, 0, sizeof(ministate.buf));
1825 ministate.buffer.current_line = &ministate.vline;
1826 ministate.buffer.current_line->line = ministate.buf;
1827 ministate.buffer.cpoff = 0;
1828 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1830 ministate.history = hist;
1831 ministate.hist_cur = NULL;
1832 ministate.hist_off = 0;
1836 exit_minibuffer(void)
1841 base_map = &global_map;
1842 current_map = &global_map;
1846 switch_to_tab(struct tab *tab)
1850 TAILQ_FOREACH(t, &tabshead, tabs) {
1851 t->flags &= ~TAB_CURRENT;
1854 tab->flags |= TAB_CURRENT;
1855 tab->flags &= ~TAB_URGENT;
1861 return tab_counter++;
1865 new_tab(const char *url)
1869 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1875 TAILQ_INIT(&tab->hist.head);
1877 TAILQ_INIT(&tab->buffer.head);
1879 tab->id = tab_new_id();
1882 if (TAILQ_EMPTY(&tabshead))
1883 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1885 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1887 load_url_in_tab(tab, url);
1892 session_new_tab_cb(const char *url)
1900 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1901 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1905 ui_init(int argc, char * const *argv)
1907 char path[PATH_MAX];
1908 const char *url = NEW_TAB_URL;
1909 int ch, configtest = 0, fonf = 0;
1911 strlcpy(path, getenv("HOME"), sizeof(path));
1912 strlcat(path, "/.telescope/config", sizeof(path));
1914 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1918 strlcpy(path, optarg, sizeof(path));
1934 parseconfig(path, fonf);
1943 setlocale(LC_ALL, "");
1945 TAILQ_INIT(&global_map.m);
1946 global_map.unhandled_input = global_key_unbound;
1948 TAILQ_INIT(&minibuffer_map.m);
1950 TAILQ_INIT(&eecmd_history.head);
1951 TAILQ_INIT(&ir_history.head);
1952 TAILQ_INIT(&lu_history.head);
1954 ministate.line.type = LINE_TEXT;
1955 ministate.vline.parent = &ministate.line;
1956 ministate.buffer.current_line = &ministate.vline;
1958 /* initialize help window */
1959 TAILQ_INIT(&helpwin.head);
1961 base_map = &global_map;
1962 current_map = &global_map;
1963 load_default_keys();
1970 intrflush(stdscr, FALSE);
1972 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1974 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1976 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1978 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1980 if ((help = newwin(1, 1, 1, 0)) == NULL)
1983 body_lines = LINES-3;
1987 scrollok(body, TRUE);
1989 /* non-blocking input */
1992 mvwprintw(body, 0, 0, "");
1994 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1995 event_add(&stdioev, NULL);
1997 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1998 signal_add(&winchev, NULL);
2000 load_last_session(session_new_tab_cb);
2001 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2008 ui_on_tab_loaded(struct tab *tab)
2010 stop_loading_anim(tab);
2011 message("Loaded %s", tab->hist_cur->h);
2022 ui_on_tab_refresh(struct tab *tab)
2024 wrap_page(&tab->buffer, body_cols);
2025 if (tab->flags & TAB_CURRENT) {
2026 restore_cursor(&tab->buffer);
2029 tab->flags |= TAB_URGENT;
2033 ui_require_input(struct tab *tab, int hide)
2035 /* TODO: hard-switching to another tab is ugly */
2038 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2040 strlcpy(ministate.prompt, "Input required: ",
2041 sizeof(ministate.prompt));
2046 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2051 if (in_minibuffer) {
2058 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2061 len = sizeof(ministate.prompt);
2062 strlcpy(ministate.prompt, prompt, len);
2063 strlcat(ministate.prompt, " (y or n) ", len);
2064 redraw_tab(current_tab());
2068 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2078 enter_minibuffer(read_self_insert, read_select, read_abort,
2081 len = sizeof(ministate.prompt);
2082 strlcpy(ministate.prompt, prompt, len);
2083 strlcat(ministate.prompt, ": ", len);
2084 redraw_tab(current_tab());
2088 ui_notify(const char *fmt, ...)