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->current_line == NULL)
455 if ((vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
458 buffer->current_line = vl;
460 restore_cursor(buffer);
464 cmd_scroll_line_down(struct buffer *buffer)
468 if (buffer->current_line == NULL)
471 if ((vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
474 buffer->current_line = vl;
476 restore_cursor(buffer);
480 cmd_scroll_up(struct buffer *buffer)
486 for (; off > 0; --off)
487 cmd_scroll_line_up(buffer);
491 cmd_scroll_down(struct buffer *buffer)
497 for (; off > 0; --off)
498 cmd_scroll_line_down(buffer);
502 cmd_beginning_of_buffer(struct buffer *buffer)
504 buffer->current_line = TAILQ_FIRST(&buffer->head);
505 buffer->line_off = 0;
508 restore_cursor(buffer);
512 cmd_end_of_buffer(struct buffer *buffer)
516 off = buffer->line_max - body_lines;
519 buffer->line_off = off;
520 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
522 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
523 buffer->cpoff = body_cols;
524 restore_cursor(buffer);
528 cmd_kill_telescope(struct buffer *buffer)
535 cmd_push_button(struct buffer *buffer)
540 nth = buffer->line_off + buffer->curs_y;
541 if (nth >= buffer->line_max)
543 vl = nth_line(buffer, nth);
544 if (vl->parent->type != LINE_LINK)
547 load_url_in_tab(current_tab(), vl->parent->alt);
551 cmd_push_button_new_tab(struct buffer *buffer)
556 nth = buffer->line_off + buffer->curs_y;
557 if (nth > buffer->line_max)
559 vl = nth_line(buffer, nth);
560 if (vl->parent->type != LINE_LINK)
563 new_tab(vl->parent->alt);
567 cmd_previous_button(struct buffer *buffer)
570 if (buffer->current_line == NULL ||
571 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
572 message("No previous link");
575 cmd_previous_line(buffer);
576 } while (buffer->current_line->parent->type != LINE_LINK);
580 cmd_next_button(struct buffer *buffer)
583 if (buffer->current_line == NULL ||
584 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
585 message("No next link");
588 cmd_next_line(buffer);
589 } while (buffer->current_line->parent->type != LINE_LINK);
593 cmd_previous_page(struct buffer *buffer)
595 struct tab *tab = current_tab();
597 if (!load_previous_page(tab))
598 message("No previous page");
600 start_loading_anim(tab);
604 cmd_next_page(struct buffer *buffer)
606 struct tab *tab = current_tab();
608 if (!load_next_page(tab))
609 message("No next page");
611 start_loading_anim(tab);
615 cmd_clear_minibuf(struct buffer *buffer)
617 handle_clear_minibuf(0, 0, NULL);
621 cmd_execute_extended_command(struct buffer *buffer)
626 message("We don't have enable-recursive-minibuffers");
630 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
633 len = sizeof(ministate.prompt);
634 strlcpy(ministate.prompt, "", len);
637 strlcat(ministate.prompt, "M-", len);
639 strlcat(ministate.prompt, keyname(thiskey.key), len);
642 strlcat(ministate.prompt, " ", len);
646 cmd_tab_close(struct buffer *buffer)
651 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
652 TAILQ_NEXT(tab, tabs) == NULL) {
653 message("Can't close the only tab.");
657 if (evtimer_pending(&tab->loadingev, NULL))
658 evtimer_del(&tab->loadingev);
662 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
663 t = TAILQ_NEXT(tab, tabs);
664 TAILQ_REMOVE(&tabshead, tab, tabs);
671 cmd_tab_close_other(struct buffer *buffer)
675 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
676 if (t->flags & TAB_CURRENT)
680 TAILQ_REMOVE(&tabshead, t, tabs);
686 cmd_tab_new(struct buffer *buffer)
690 if ((url = new_tab_url) == NULL)
697 cmd_tab_next(struct buffer *buffer)
702 tab->flags &= ~TAB_CURRENT;
704 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
705 t = TAILQ_FIRST(&tabshead);
706 t->flags |= TAB_CURRENT;
707 t->flags &= ~TAB_URGENT;
711 cmd_tab_previous(struct buffer *buffer)
716 tab->flags &= ~TAB_CURRENT;
718 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
719 t = TAILQ_LAST(&tabshead, tabshead);
720 t->flags |= TAB_CURRENT;
721 t->flags &= ~TAB_URGENT;
725 cmd_tab_move(struct buffer *buffer)
730 t = TAILQ_NEXT(tab, tabs);
731 TAILQ_REMOVE(&tabshead, tab, tabs);
734 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
736 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
740 cmd_tab_move_to(struct buffer *buffer)
745 t = TAILQ_PREV(tab, tabshead, tabs);
746 TAILQ_REMOVE(&tabshead, tab, tabs);
749 if (TAILQ_EMPTY(&tabshead))
750 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
752 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
754 TAILQ_INSERT_BEFORE(t, tab, tabs);
758 cmd_load_url(struct buffer *buffer)
761 message("We don't have enable-recursive-minibuffers");
765 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
767 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
768 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
769 cmd_move_end_of_line(&ministate.buffer);
773 cmd_load_current_url(struct buffer *buffer)
775 struct tab *tab = current_tab();
778 message("We don't have enable-recursive-minibuffers");
782 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
784 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
785 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
786 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
790 cmd_bookmark_page(struct buffer *buffer)
792 struct tab *tab = current_tab();
794 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
795 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
796 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
797 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
801 cmd_list_bookmarks(struct buffer *buffer)
803 load_url_in_tab(current_tab(), "about:bookmarks");
807 cmd_toggle_help(struct buffer *buffer)
809 side_window = !side_window;
814 * ugly hack, but otherwise the window doesn't get updated
815 * until I call handle_resize a second time (i.e. C-l). I
816 * will be happy to know why something like this is needed.
818 handle_resize_nodelay(0, 0, NULL);
819 handle_resize_nodelay(0, 0, NULL);
823 cmd_olivetti_mode(struct buffer *buffer)
825 olivetti_mode = !olivetti_mode;
827 message("olivetti-mode enabled");
829 message("olivetti-mode disabled");
831 handle_resize_nodelay(0, 0, NULL);
835 cmd_mini_delete_char(struct buffer *buffer)
839 if (!in_minibuffer) {
840 message("text is read-only");
844 minibuffer_taint_hist();
846 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
851 memmove(c, n, strlen(n)+1);
855 cmd_mini_delete_backward_char(struct buffer *buffer)
859 if (!in_minibuffer) {
860 message("text is read-only");
864 minibuffer_taint_hist();
866 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
867 start = buffer->current_line->line;
870 p = utf8_prev_cp(c-1, start);
872 memmove(p, c, strlen(c)+1);
877 cmd_mini_kill_line(struct buffer *buffer)
881 if (!in_minibuffer) {
882 message("text is read-only");
886 minibuffer_taint_hist();
887 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
892 cmd_mini_abort(struct buffer *buffer)
901 cmd_mini_complete_and_exit(struct buffer *buffer)
906 minibuffer_taint_hist();
911 cmd_mini_previous_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_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
920 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
921 ministate.hist_off = ministate.history->len - 1;
922 if (ministate.hist_cur == NULL)
923 message("No prev item");
925 ministate.hist_off--;
928 if (ministate.hist_cur != NULL)
929 buffer->current_line->line = ministate.hist_cur->h;
933 cmd_mini_next_history_element(struct buffer *buffer)
935 if (ministate.history == NULL) {
936 message("No history");
940 if (ministate.hist_cur == NULL ||
941 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
942 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
943 ministate.hist_off = 0;
944 if (ministate.hist_cur == NULL)
945 message("No next item");
947 ministate.hist_off++;
950 if (ministate.hist_cur != NULL)
951 buffer->current_line->line = ministate.hist_cur->h;
955 global_key_unbound(void)
957 message("%s is undefined", keybuf);
961 minibuffer_hist_save_entry(void)
965 if (ministate.history == NULL)
968 if ((hist = calloc(1, sizeof(*hist))) == NULL)
971 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
973 if (TAILQ_EMPTY(&ministate.history->head))
974 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
976 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
977 ministate.history->len++;
981 * taint the minibuffer cache: if we're currently showing a history
982 * element, copy that to the current buf and reset the "history
986 minibuffer_taint_hist(void)
988 if (ministate.hist_cur == NULL)
991 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
992 ministate.hist_cur = NULL;
996 minibuffer_self_insert(void)
998 char *c, tmp[5] = {0};
1001 minibuffer_taint_hist();
1003 if (thiskey.cp == 0)
1006 len = utf8_encode(thiskey.cp, tmp);
1007 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1008 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1011 memmove(c + len, c, strlen(c)+1);
1012 memcpy(c, tmp, len);
1013 ministate.buffer.cpoff++;
1017 eecmd_self_insert(void)
1019 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1020 !unicode_isgraph(thiskey.cp)) {
1021 global_key_unbound();
1025 minibuffer_self_insert();
1033 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1034 if (!strcmp(cmd->cmd, ministate.buf)) {
1036 minibuffer_hist_save_entry();
1037 cmd->fn(current_buffer());
1042 message("No match");
1046 ir_self_insert(void)
1048 minibuffer_self_insert();
1054 char buf[1025] = {0};
1055 struct phos_uri uri;
1058 tab = current_tab();
1061 minibuffer_hist_save_entry();
1063 /* a bit ugly but... */
1064 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1065 phos_uri_set_query(&uri, ministate.buf);
1066 phos_serialize_uri(&uri, buf, sizeof(buf));
1067 load_url_in_tab(tab, buf);
1071 lu_self_insert(void)
1073 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1074 !unicode_isgraph(thiskey.key)) {
1075 global_key_unbound();
1079 minibuffer_self_insert();
1086 minibuffer_hist_save_entry();
1087 load_url_in_tab(current_tab(), ministate.buf);
1094 if (*ministate.buf != '\0')
1095 add_to_bookmarks(ministate.buf);
1101 yornp_self_insert(void)
1103 if (thiskey.key != 'y' && thiskey.key != 'n') {
1104 message("Please answer y or n");
1109 yornp_cb(thiskey.key == 'y', yornp_data);
1116 yornp_cb(0, yornp_data);
1120 read_self_insert(void)
1122 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1123 global_key_unbound();
1127 minibuffer_self_insert();
1134 read_cb(NULL, read_data);
1141 minibuffer_hist_save_entry();
1142 read_cb(ministate.buf, read_data);
1145 static struct vline *
1146 nth_line(struct buffer *buffer, size_t n)
1152 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1167 TAILQ_FOREACH(t, &tabshead, tabs) {
1168 if (t->flags & TAB_CURRENT)
1176 static struct buffer *
1177 current_buffer(void)
1180 return &ministate.buffer;
1181 return ¤t_tab()->buffer;
1189 if ((thiskey.key = wgetch(body)) == ERR)
1192 thiskey.meta = thiskey.key == 27;
1194 thiskey.key = wgetch(body);
1195 if (thiskey.key == ERR || thiskey.key == 27) {
1202 if ((unsigned int)thiskey.key < UINT8_MAX) {
1204 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1206 if ((thiskey.key = wgetch(body)) == ERR) {
1207 message("Error decoding user input");
1217 dispatch_stdio(int fd, short ev, void *d)
1220 const char *keyname;
1226 if (keybuf[0] != '\0')
1227 strlcat(keybuf, " ", sizeof(keybuf));
1229 strlcat(keybuf, "M-", sizeof(keybuf));
1230 if (thiskey.cp != 0) {
1231 utf8_encode(thiskey.cp, tmp);
1232 strlcat(keybuf, tmp, sizeof(keybuf));
1234 if ((keyname = unkbd(thiskey.key)) != NULL)
1235 strlcat(keybuf, keyname, sizeof(keybuf));
1237 tmp[0] = thiskey.key;
1238 strlcat(keybuf, tmp, sizeof(keybuf));
1242 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1243 if (k->meta == thiskey.meta &&
1244 k->key == thiskey.key) {
1246 current_map = &k->map;
1248 current_map = base_map;
1249 strlcpy(keybuf, "", sizeof(keybuf));
1250 k->fn(current_buffer());
1256 if (current_map->unhandled_input != NULL)
1257 current_map->unhandled_input();
1259 global_key_unbound();
1261 strlcpy(keybuf, "", sizeof(keybuf));
1262 current_map = base_map;
1268 redraw_tab(current_tab());
1272 handle_clear_minibuf(int fd, short ev, void *d)
1274 free(ministate.curmesg);
1275 ministate.curmesg = NULL;
1277 redraw_minibuffer();
1278 if (in_minibuffer) {
1288 handle_resize(int sig, short ev, void *d)
1290 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1291 event_del(&resizeev);
1293 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1294 evtimer_add(&resizeev, &resize_timer);
1298 handle_resize_nodelay(int s, short ev, void *d)
1306 /* move and resize the windows, in reverse order! */
1308 mvwin(minibuf, LINES-1, 0);
1309 wresize(minibuf, 1, COLS);
1311 mvwin(modeline, LINES-2, 0);
1312 wresize(modeline, 1, COLS);
1314 body_lines = LINES-3;
1318 help_cols = 0.3 * COLS;
1319 help_lines = LINES-3;
1321 wresize(help, help_lines, help_cols);
1323 wrap_page(&helpwin, help_cols);
1325 body_cols = COLS - help_cols - 1;
1326 mvwin(body, 1, help_cols);
1331 wresize(body, body_lines, body_cols);
1333 wresize(tabline, 1, COLS);
1335 tab = current_tab();
1337 wrap_page(&tab->buffer, body_cols);
1342 wrap_page(struct buffer *buffer, int width)
1345 const struct line *orig;
1350 orig = buffer->current_line == NULL
1352 : buffer->current_line->parent;
1353 buffer->current_line = NULL;
1355 buffer->force_redraw = 1;
1357 buffer->line_off = 0;
1359 empty_vlist(buffer);
1361 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1362 prfx = line_prefixes[l->type].prfx1;
1371 case LINE_PRE_START:
1373 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1375 case LINE_PRE_CONTENT:
1377 pre_width = MIN(fill_column, width);
1380 hardwrap_text(buffer, l, pre_width);
1384 if (orig == l && buffer->current_line == NULL) {
1385 buffer->line_off = buffer->line_max-1;
1386 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1389 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1390 if (vl == NULL || vl->parent != orig)
1392 buffer->current_line = vl;
1398 if (buffer->current_line == NULL)
1399 buffer->current_line = TAILQ_FIRST(&buffer->head);
1405 print_vline(WINDOW *window, struct vline *vl)
1407 const char *text = vl->line;
1409 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1410 int text_face = line_faces[vl->parent->type].text_prop;
1413 prfx = line_prefixes[vl->parent->type].prfx1;
1415 prfx = line_prefixes[vl->parent->type].prfx2;
1420 wattron(window, prefix_face);
1421 wprintw(window, "%s", prfx);
1422 wattroff(window, prefix_face);
1424 wattron(window, text_face);
1425 wprintw(window, "%s", text);
1426 wattroff(window, text_face);
1430 redraw_tabline(void)
1433 size_t toskip, ots, tabwidth, space, x;
1434 int current, y, truncated;
1438 tabwidth = sizeof(buf)+1;
1442 TAILQ_FOREACH(tab, &tabshead, tabs) {
1444 if (tab->flags & TAB_CURRENT)
1448 if (toskip * tabwidth < space)
1453 while (toskip != 0 &&
1454 (ots - toskip+1) * tabwidth < space)
1459 wattron(tabline, tab_face.background);
1460 wprintw(tabline, toskip == 0 ? " " : "<");
1461 wattroff(tabline, tab_face.background);
1464 TAILQ_FOREACH(tab, &tabshead, tabs) {
1472 getyx(tabline, y, x);
1473 if (x + sizeof(buf)+2 >= (size_t)COLS)
1476 current = tab->flags & TAB_CURRENT;
1478 if (*(title = tab->buffer.page.title) == '\0')
1479 title = tab->hist_cur->h;
1481 if (tab->flags & TAB_URGENT)
1482 strlcpy(buf, "!", sizeof(buf));
1484 strlcpy(buf, " ", sizeof(buf));
1486 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1487 /* truncation happens */
1488 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1490 /* pad with spaces */
1491 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1496 wattron(tabline, tab_face.current_tab);
1498 wattron(tabline, tab_face.tab);
1500 wprintw(tabline, "%s", buf);
1501 if (TAILQ_NEXT(tab, tabs) != NULL)
1502 wprintw(tabline, " ");
1505 wattroff(tabline, tab_face.current_tab);
1507 wattroff(tabline, tab_face.tab);
1510 wattron(tabline, tab_face.background);
1511 for (; x < (size_t)COLS; ++x)
1512 waddch(tabline, ' ');
1514 mvwprintw(tabline, 0, COLS-1, ">");
1518 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1524 * Don't bother redraw the body if nothing changed. Cursor
1525 * movements count as "nothing changed" if it hasn't produced
1526 * a scroll. Ensure that wmove is called though!
1528 if (!buffer->force_redraw &&
1529 buffer->last_line_off == buffer->line_off)
1534 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1535 buffer->force_redraw = 0;
1536 buffer->last_line_off = buffer->line_off;
1538 if (TAILQ_EMPTY(&buffer->head))
1542 vl = nth_line(buffer, buffer->line_off);
1543 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1544 wmove(win, l, x_offset);
1545 print_vline(win, vl);
1552 wmove(win, buffer->curs_y, buffer->curs_x);
1558 redraw_window(help, help_lines, &helpwin);
1562 redraw_body(struct tab *tab)
1564 redraw_window(body, body_lines, &tab->buffer);
1568 trust_status_char(enum trust_state ts)
1571 case TS_UNKNOWN: return 'u';
1572 case TS_UNTRUSTED: return '!';
1573 case TS_TRUSTED: return 'v';
1574 case TS_VERIFIED: return 'V';
1579 redraw_modeline(struct tab *tab)
1582 int x, y, max_x, max_y;
1583 const char *mode = tab->buffer.page.name;
1584 const char *spin = "-\\|/";
1587 wattron(modeline, modeline_face.background);
1588 wmove(modeline, 0, 0);
1590 wprintw(modeline, "-%c%c %s ",
1591 spin[tab->loading_anim_step],
1592 trust_status_char(tab->trust),
1593 mode == NULL ? "(none)" : mode);
1595 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1597 if (tab->buffer.line_max <= (size_t)body_lines)
1598 wprintw(modeline, "All ");
1599 else if (tab->buffer.line_off == 0)
1600 wprintw(modeline, "Top ");
1601 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1602 wprintw(modeline, "Bottom ");
1604 wprintw(modeline, "%.0f%% ", pct);
1606 wprintw(modeline, "%d/%d %s ",
1607 tab->buffer.line_off + tab->buffer.curs_y,
1608 tab->buffer.line_max,
1611 getyx(modeline, y, x);
1612 getmaxyx(modeline, max_y, max_x);
1617 for (; x < max_x; ++x)
1618 waddstr(modeline, "-");
1620 wattroff(modeline, modeline_face.background);
1624 redraw_minibuffer(void)
1627 size_t off_y, off_x = 0;
1630 wattron(minibuf, minibuffer_face.background);
1633 if (in_minibuffer) {
1634 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1635 if (ministate.hist_cur != NULL)
1636 wprintw(minibuf, "(%zu/%zu) ",
1637 ministate.hist_off + 1,
1638 ministate.history->len);
1640 getyx(minibuf, off_y, off_x);
1642 start = ministate.hist_cur != NULL
1643 ? ministate.hist_cur->h
1645 c = utf8_nth(ministate.buffer.current_line->line,
1646 ministate.buffer.cpoff);
1647 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1648 start = utf8_next_cp(start);
1651 waddstr(minibuf, start);
1654 if (ministate.curmesg != NULL)
1655 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1658 if (!in_minibuffer && ministate.curmesg == NULL)
1659 waddstr(minibuf, keybuf);
1661 /* If nothing else, show the URL at point */
1662 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1663 tab = current_tab();
1664 if (tab->buffer.current_line != NULL &&
1665 tab->buffer.current_line->parent->type == LINE_LINK)
1666 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1670 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1672 wattroff(minibuf, minibuffer_face.background);
1676 redraw_tab(struct tab *tab)
1683 restore_cursor(&tab->buffer);
1687 redraw_modeline(tab);
1688 redraw_minibuffer();
1690 wnoutrefresh(tabline);
1691 wnoutrefresh(modeline);
1693 if (in_minibuffer) {
1695 wnoutrefresh(minibuf);
1697 wnoutrefresh(minibuf);
1705 emit_help_item(char *prfx, void *fn)
1710 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1714 assert(cmd != NULL);
1716 if ((l = calloc(1, sizeof(*l))) == NULL)
1719 l->type = LINE_TEXT;
1722 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1724 if (TAILQ_EMPTY(&helpwin.page.head))
1725 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1727 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1731 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1737 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1738 strlcpy(p, prfx, sizeof(p));
1740 strlcat(p, " ", sizeof(p));
1742 strlcat(p, "M-", sizeof(p));
1743 if ((kn = unkbd(k->key)) != NULL)
1744 strlcat(p, kn, sizeof(p));
1746 strlcat(p, keyname(k->key), sizeof(p));
1749 rec_compute_help(&k->map, p, sizeof(p));
1751 emit_help_item(p, k->fn);
1756 recompute_help(void)
1760 empty_vlist(&helpwin);
1761 empty_linelist(&helpwin);
1762 rec_compute_help(current_map, p, sizeof(p));
1763 wrap_page(&helpwin, help_cols);
1767 vmessage(const char *fmt, va_list ap)
1769 if (evtimer_pending(&clminibufev, NULL))
1770 evtimer_del(&clminibufev);
1771 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1772 evtimer_add(&clminibufev, &clminibufev_timer);
1774 free(ministate.curmesg);
1776 /* TODO: what to do if the allocation fails here? */
1777 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1778 ministate.curmesg = NULL;
1780 redraw_minibuffer();
1781 if (in_minibuffer) {
1791 message(const char *fmt, ...)
1801 start_loading_anim(struct tab *tab)
1803 if (tab->loading_anim)
1805 tab->loading_anim = 1;
1806 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1807 evtimer_add(&tab->loadingev, &loadingev_timer);
1811 update_loading_anim(int fd, short ev, void *d)
1813 struct tab *tab = d;
1815 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1817 if (tab->flags & TAB_CURRENT) {
1818 redraw_modeline(tab);
1825 evtimer_add(&tab->loadingev, &loadingev_timer);
1829 stop_loading_anim(struct tab *tab)
1831 if (!tab->loading_anim)
1833 evtimer_del(&tab->loadingev);
1834 tab->loading_anim = 0;
1835 tab->loading_anim_step = 0;
1837 if (!(tab->flags & TAB_CURRENT))
1840 redraw_modeline(tab);
1849 load_url_in_tab(struct tab *tab, const char *url)
1851 message("Loading %s...", url);
1852 start_loading_anim(tab);
1855 tab->buffer.curs_x = 0;
1856 tab->buffer.curs_y = 0;
1861 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1862 void (*abortfn)(void), struct histhead *hist)
1865 base_map = &minibuffer_map;
1866 current_map = &minibuffer_map;
1868 base_map->unhandled_input = self_insert_fn;
1870 ministate.donefn = donefn;
1871 ministate.abortfn = abortfn;
1872 memset(ministate.buf, 0, sizeof(ministate.buf));
1873 ministate.buffer.current_line = &ministate.vline;
1874 ministate.buffer.current_line->line = ministate.buf;
1875 ministate.buffer.cpoff = 0;
1876 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1878 ministate.history = hist;
1879 ministate.hist_cur = NULL;
1880 ministate.hist_off = 0;
1884 exit_minibuffer(void)
1889 base_map = &global_map;
1890 current_map = &global_map;
1894 switch_to_tab(struct tab *tab)
1898 TAILQ_FOREACH(t, &tabshead, tabs) {
1899 t->flags &= ~TAB_CURRENT;
1902 tab->flags |= TAB_CURRENT;
1903 tab->flags &= ~TAB_URGENT;
1909 return tab_counter++;
1913 new_tab(const char *url)
1917 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1923 TAILQ_INIT(&tab->hist.head);
1925 TAILQ_INIT(&tab->buffer.head);
1927 tab->id = tab_new_id();
1930 if (TAILQ_EMPTY(&tabshead))
1931 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1933 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1935 load_url_in_tab(tab, url);
1940 session_new_tab_cb(const char *url)
1948 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1949 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1953 ui_init(int argc, char * const *argv)
1955 char path[PATH_MAX];
1956 const char *url = NEW_TAB_URL;
1957 int ch, configtest = 0, fonf = 0;
1959 strlcpy(path, getenv("HOME"), sizeof(path));
1960 strlcat(path, "/.telescope/config", sizeof(path));
1962 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1966 strlcpy(path, optarg, sizeof(path));
1982 parseconfig(path, fonf);
1991 setlocale(LC_ALL, "");
1993 TAILQ_INIT(&global_map.m);
1994 global_map.unhandled_input = global_key_unbound;
1996 TAILQ_INIT(&minibuffer_map.m);
1998 TAILQ_INIT(&eecmd_history.head);
1999 TAILQ_INIT(&ir_history.head);
2000 TAILQ_INIT(&lu_history.head);
2002 ministate.line.type = LINE_TEXT;
2003 ministate.vline.parent = &ministate.line;
2004 ministate.buffer.current_line = &ministate.vline;
2006 /* initialize help window */
2007 TAILQ_INIT(&helpwin.head);
2009 base_map = &global_map;
2010 current_map = &global_map;
2011 load_default_keys();
2018 intrflush(stdscr, FALSE);
2020 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2022 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2024 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2026 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2028 if ((help = newwin(1, 1, 1, 0)) == NULL)
2031 body_lines = LINES-3;
2037 scrollok(body, TRUE);
2039 /* non-blocking input */
2042 mvwprintw(body, 0, 0, "");
2044 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2045 event_add(&stdioev, NULL);
2047 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2048 signal_add(&winchev, NULL);
2050 load_last_session(session_new_tab_cb);
2051 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2058 ui_on_tab_loaded(struct tab *tab)
2060 stop_loading_anim(tab);
2061 message("Loaded %s", tab->hist_cur->h);
2072 ui_on_tab_refresh(struct tab *tab)
2074 wrap_page(&tab->buffer, body_cols);
2075 if (tab->flags & TAB_CURRENT) {
2076 restore_cursor(&tab->buffer);
2079 tab->flags |= TAB_URGENT;
2083 ui_require_input(struct tab *tab, int hide)
2085 /* TODO: hard-switching to another tab is ugly */
2088 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2090 strlcpy(ministate.prompt, "Input required: ",
2091 sizeof(ministate.prompt));
2096 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2101 if (in_minibuffer) {
2108 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2111 len = sizeof(ministate.prompt);
2112 strlcpy(ministate.prompt, prompt, len);
2113 strlcat(ministate.prompt, " (y or n) ", len);
2114 redraw_tab(current_tab());
2118 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2128 enter_minibuffer(read_self_insert, read_select, read_abort,
2131 len = sizeof(ministate.prompt);
2132 strlcpy(ministate.prompt, prompt, len);
2133 strlcat(ministate.prompt, ": ", len);
2134 redraw_tab(current_tab());
2138 ui_notify(const char *fmt, ...)