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"
48 #define TAB_CURRENT 0x1
49 #define TAB_URGENT 0x2
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void restore_cursor(struct buffer*);
58 static void global_key_unbound(void);
59 static void minibuffer_hist_save_entry(void);
60 static void minibuffer_taint_hist(void);
61 static void minibuffer_self_insert(void);
62 static void eecmd_self_insert(void);
63 static void eecmd_select(void);
64 static void ir_self_insert(void);
65 static void ir_select(void);
66 static void lu_self_insert(void);
67 static void lu_select(void);
68 static void bp_select(void);
69 static void yornp_self_insert(void);
70 static void yornp_abort(void);
71 static void read_self_insert(void);
72 static void read_abort(void);
73 static void read_select(void);
75 static struct vline *nth_line(struct buffer*, size_t);
76 static struct tab *current_tab(void);
77 static struct buffer *current_buffer(void);
78 static int readkey(void);
79 static void dispatch_stdio(int, short, void*);
80 static void handle_clear_minibuf(int, short, void*);
81 static void handle_resize(int, short, void*);
82 static void handle_resize_nodelay(int, short, void*);
83 static int wrap_page(struct buffer*, int);
84 static void print_vline(WINDOW*, struct vline*);
85 static void redraw_tabline(void);
86 static void redraw_window(WINDOW*, int, struct buffer*);
87 static void redraw_help(void);
88 static void redraw_body(struct tab*);
89 static void redraw_modeline(struct tab*);
90 static void redraw_minibuffer(void);
91 static void redraw_tab(struct tab*);
92 static void emit_help_item(char*, void*);
93 static void rec_compute_help(struct kmap*, char*, size_t);
94 static void recompute_help(void);
95 static void vmessage(const char*, va_list);
96 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
97 static void start_loading_anim(struct tab*);
98 static void update_loading_anim(int, short, void*);
99 static void stop_loading_anim(struct tab*);
100 static void load_url_in_tab(struct tab*, const char*);
101 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
102 static void exit_minibuffer(void);
103 static void switch_to_tab(struct tab*);
104 static struct tab *new_tab(const char*);
105 static void session_new_tab_cb(const char*);
106 static void usage(void);
108 static struct { short meta; int key; uint32_t cp; } thiskey;
110 static struct event resizeev;
111 static struct timeval resize_timer = { 0, 250000 };
113 static WINDOW *tabline, *body, *modeline, *minibuf;
114 static int body_lines, body_cols;
117 static struct buffer helpwin;
118 static int help_lines, help_cols;
120 static int side_window;
122 static struct event clminibufev;
123 static struct timeval clminibufev_timer = { 5, 0 };
124 static struct timeval loadingev_timer = { 0, 250000 };
126 static uint32_t tab_counter;
128 static char keybuf[64];
130 static void (*yornp_cb)(int, unsigned int);
131 static unsigned int yornp_data;
133 static void (*read_cb)(const char*, unsigned int);
134 static unsigned int read_data;
136 struct kmap global_map,
141 static struct histhead eecmd_history,
146 static int in_minibuffer;
152 void (*donefn)(void);
153 void (*abortfn)(void);
158 struct buffer buffer;
160 struct histhead *history;
161 struct hist *hist_cur;
168 } line_prefixes[] = {
169 [LINE_TEXT] = { "", "" },
170 [LINE_LINK] = { "=> ", " " },
171 [LINE_TITLE_1] = { "# ", " " },
172 [LINE_TITLE_2] = { "## ", " " },
173 [LINE_TITLE_3] = { "### ", " " },
174 [LINE_ITEM] = { "* ", " " },
175 [LINE_QUOTE] = { "> ", " " },
176 [LINE_PRE_START] = { "```", " " },
177 [LINE_PRE_CONTENT] = { "", "" },
178 [LINE_PRE_END] = { "```", "```" },
181 static struct line_face {
185 [LINE_TEXT] = { 0, 0 },
186 [LINE_LINK] = { 0, A_UNDERLINE },
187 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
188 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
189 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
190 [LINE_ITEM] = { 0, 0 },
191 [LINE_QUOTE] = { 0, A_DIM },
192 [LINE_PRE_START] = { 0, 0 },
193 [LINE_PRE_CONTENT] = { 0, 0 },
194 [LINE_PRE_END] = { 0, 0 },
197 static struct tab_face {
198 int background, tab, current_tab;
200 A_REVERSE, A_REVERSE, A_NORMAL
204 global_set_key(const char *key, void (*fn)(struct buffer*))
206 if (!kmap_define_key(&global_map, key, fn))
211 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
213 if (!kmap_define_key(&minibuffer_map, key, fn))
218 load_default_keys(void)
220 /* === global map === */
223 global_set_key("C-p", cmd_previous_line);
224 global_set_key("C-n", cmd_next_line);
225 global_set_key("C-f", cmd_forward_char);
226 global_set_key("C-b", cmd_backward_char);
227 global_set_key("M-{", cmd_backward_paragraph);
228 global_set_key("M-}", cmd_forward_paragraph);
229 global_set_key("C-a", cmd_move_beginning_of_line);
230 global_set_key("C-e", cmd_move_end_of_line);
232 global_set_key("M-v", cmd_scroll_up);
233 global_set_key("C-v", cmd_scroll_down);
234 global_set_key("M-space", cmd_scroll_up);
235 global_set_key("space", cmd_scroll_down);
237 global_set_key("M-<", cmd_beginning_of_buffer);
238 global_set_key("M->", cmd_end_of_buffer);
240 global_set_key("C-x C-c", cmd_kill_telescope);
242 global_set_key("C-g", cmd_clear_minibuf);
244 global_set_key("M-x", cmd_execute_extended_command);
245 global_set_key("C-x C-f", cmd_load_url);
246 global_set_key("C-x M-f", cmd_load_current_url);
248 global_set_key("C-x t 0", cmd_tab_close);
249 global_set_key("C-x t 1", cmd_tab_close_other);
250 global_set_key("C-x t 2", cmd_tab_new);
251 global_set_key("C-x t o", cmd_tab_next);
252 global_set_key("C-x t O", cmd_tab_previous);
253 global_set_key("C-x t m", cmd_tab_move);
254 global_set_key("C-x t M", cmd_tab_move_to);
256 global_set_key("C-M-b", cmd_previous_page);
257 global_set_key("C-M-f", cmd_next_page);
259 global_set_key("<f7> a", cmd_bookmark_page);
260 global_set_key("<f7> <f7>", cmd_list_bookmarks);
263 global_set_key("k", cmd_previous_line);
264 global_set_key("j", cmd_next_line);
265 global_set_key("l", cmd_forward_char);
266 global_set_key("h", cmd_backward_char);
267 global_set_key("{", cmd_backward_paragraph);
268 global_set_key("}", cmd_forward_paragraph);
269 global_set_key("^", cmd_move_beginning_of_line);
270 global_set_key("$", cmd_move_end_of_line);
272 global_set_key("K", cmd_scroll_line_up);
273 global_set_key("J", cmd_scroll_line_down);
275 global_set_key("g g", cmd_beginning_of_buffer);
276 global_set_key("G", cmd_end_of_buffer);
278 global_set_key("g D", cmd_tab_close);
279 global_set_key("g N", cmd_tab_new);
280 global_set_key("g t", cmd_tab_next);
281 global_set_key("g T", cmd_tab_previous);
282 global_set_key("g M-t", cmd_tab_move);
283 global_set_key("g M-T", cmd_tab_move_to);
285 global_set_key("H", cmd_previous_page);
286 global_set_key("L", cmd_next_page);
289 global_set_key("q", cmd_kill_telescope);
291 global_set_key("esc", cmd_clear_minibuf);
293 global_set_key(":", cmd_execute_extended_command);
296 global_set_key("<up>", cmd_previous_line);
297 global_set_key("<down>", cmd_next_line);
298 global_set_key("<right>", cmd_forward_char);
299 global_set_key("<left>", cmd_backward_char);
300 global_set_key("<prior>", cmd_scroll_up);
301 global_set_key("<next>", cmd_scroll_down);
303 global_set_key("M-<left>", cmd_previous_page);
304 global_set_key("M-<right>", cmd_next_page);
306 /* "ncurses standard" */
307 global_set_key("C-l", cmd_redraw);
310 global_set_key("<f1>", cmd_toggle_help);
311 global_set_key("C-m", cmd_push_button);
312 global_set_key("M-enter", cmd_push_button_new_tab);
313 global_set_key("M-tab", cmd_previous_button);
314 global_set_key("tab", cmd_next_button);
316 /* === minibuffer map === */
317 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
318 minibuffer_set_key("C-g", cmd_mini_abort);
319 minibuffer_set_key("esc", cmd_mini_abort);
320 minibuffer_set_key("C-d", cmd_mini_delete_char);
321 minibuffer_set_key("del", cmd_mini_delete_backward_char);
322 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
323 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
325 minibuffer_set_key("C-b", cmd_backward_char);
326 minibuffer_set_key("C-f", cmd_forward_char);
327 minibuffer_set_key("<left>", cmd_backward_char);
328 minibuffer_set_key("<right>", cmd_forward_char);
329 minibuffer_set_key("C-e", cmd_move_end_of_line);
330 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
331 minibuffer_set_key("<end>", cmd_move_end_of_line);
332 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
333 minibuffer_set_key("C-k", cmd_mini_kill_line);
335 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
336 minibuffer_set_key("M-n", cmd_mini_next_history_element);
337 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
338 minibuffer_set_key("<down>", cmd_mini_next_history_element);
342 restore_cursor(struct buffer *buffer)
347 vl = buffer->current_line;
348 if (vl == NULL || vl->line == NULL)
349 buffer->curs_x = buffer->cpoff = 0;
351 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
354 prfx = line_prefixes[vl->parent->type].prfx1;
355 buffer->curs_x += utf8_swidth(prfx);
360 cmd_previous_line(struct buffer *buffer)
364 if (buffer->current_line == NULL
365 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
368 if (--buffer->curs_y < 0) {
370 cmd_scroll_line_up(buffer);
374 buffer->current_line = vl;
375 restore_cursor(buffer);
379 cmd_next_line(struct buffer *buffer)
383 if (buffer->current_line == NULL
384 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
387 if (++buffer->curs_y > body_lines-1) {
388 buffer->curs_y = body_lines-1;
389 cmd_scroll_line_down(buffer);
393 buffer->current_line = vl;
394 restore_cursor(buffer);
398 cmd_backward_char(struct buffer *buffer)
400 if (buffer->cpoff != 0)
402 restore_cursor(buffer);
406 cmd_forward_char(struct buffer *buffer)
410 if (buffer->current_line->line != NULL)
411 len = utf8_cplen(buffer->current_line->line);
412 if (++buffer->cpoff > len)
414 restore_cursor(buffer);
418 cmd_backward_paragraph(struct buffer *buffer)
421 if (buffer->current_line == NULL ||
422 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
423 message("No previous paragraph");
426 cmd_previous_line(buffer);
427 } while (buffer->current_line->line != NULL ||
428 buffer->current_line->parent->type != LINE_TEXT);
432 cmd_forward_paragraph(struct buffer *buffer)
435 if (buffer->current_line == NULL ||
436 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
437 message("No next paragraph");
440 cmd_next_line(buffer);
441 } while (buffer->current_line->line != NULL ||
442 buffer->current_line->parent->type != LINE_TEXT);
446 cmd_move_beginning_of_line(struct buffer *buffer)
449 restore_cursor(buffer);
453 cmd_move_end_of_line(struct buffer *buffer)
457 vl = buffer->current_line;
458 if (vl->line == NULL)
460 buffer->cpoff = utf8_cplen(vl->line);
461 restore_cursor(buffer);
465 cmd_redraw(struct buffer *buffer)
467 handle_resize(0, 0, NULL);
471 cmd_scroll_line_up(struct buffer *buffer)
475 if (buffer->line_off == 0)
478 vl = nth_line(buffer, --buffer->line_off);
481 print_vline(body, vl);
483 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
484 restore_cursor(buffer);
488 cmd_scroll_line_down(struct buffer *buffer)
492 vl = buffer->current_line;
493 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
495 buffer->current_line = vl;
500 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
503 vl = nth_line(buffer, buffer->line_off + body_lines-1);
504 wmove(body, body_lines-1, 0);
505 print_vline(body, vl);
507 restore_cursor(buffer);
511 cmd_scroll_up(struct buffer *buffer)
517 for (; off > 0; --off)
518 cmd_scroll_line_up(buffer);
522 cmd_scroll_down(struct buffer *buffer)
528 for (; off > 0; --off)
529 cmd_scroll_line_down(buffer);
533 cmd_beginning_of_buffer(struct buffer *buffer)
535 buffer->current_line = TAILQ_FIRST(&buffer->head);
536 buffer->line_off = 0;
539 restore_cursor(buffer);
543 cmd_end_of_buffer(struct buffer *buffer)
547 off = buffer->line_max - body_lines;
550 buffer->line_off = off;
551 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
553 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
554 buffer->cpoff = body_cols;
555 restore_cursor(buffer);
559 cmd_kill_telescope(struct buffer *buffer)
566 cmd_push_button(struct buffer *buffer)
571 nth = buffer->line_off + buffer->curs_y;
572 if (nth >= buffer->line_max)
574 vl = nth_line(buffer, nth);
575 if (vl->parent->type != LINE_LINK)
578 load_url_in_tab(current_tab(), vl->parent->alt);
582 cmd_push_button_new_tab(struct buffer *buffer)
587 nth = buffer->line_off + buffer->curs_y;
588 if (nth > buffer->line_max)
590 vl = nth_line(buffer, nth);
591 if (vl->parent->type != LINE_LINK)
594 new_tab(vl->parent->alt);
598 cmd_previous_button(struct buffer *buffer)
601 if (buffer->current_line == NULL ||
602 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
603 message("No previous link");
606 cmd_previous_line(buffer);
607 } while (buffer->current_line->parent->type != LINE_LINK);
611 cmd_next_button(struct buffer *buffer)
614 if (buffer->current_line == NULL ||
615 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
616 message("No next link");
619 cmd_next_line(buffer);
620 } while (buffer->current_line->parent->type != LINE_LINK);
624 cmd_previous_page(struct buffer *buffer)
626 struct tab *tab = current_tab();
628 if (!load_previous_page(tab))
629 message("No previous page");
631 start_loading_anim(tab);
635 cmd_next_page(struct buffer *buffer)
637 struct tab *tab = current_tab();
639 if (!load_next_page(tab))
640 message("No next page");
642 start_loading_anim(tab);
646 cmd_clear_minibuf(struct buffer *buffer)
648 handle_clear_minibuf(0, 0, NULL);
652 cmd_execute_extended_command(struct buffer *buffer)
657 message("We don't have enable-recursive-minibuffers");
661 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
664 len = sizeof(ministate.prompt);
665 strlcpy(ministate.prompt, "", len);
668 strlcat(ministate.prompt, "M-", len);
670 strlcat(ministate.prompt, keyname(thiskey.key), len);
673 strlcat(ministate.prompt, " ", len);
677 cmd_tab_close(struct buffer *buffer)
682 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
683 TAILQ_NEXT(tab, tabs) == NULL) {
684 message("Can't close the only tab.");
688 if (evtimer_pending(&tab->loadingev, NULL))
689 evtimer_del(&tab->loadingev);
693 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
694 t = TAILQ_NEXT(tab, tabs);
695 TAILQ_REMOVE(&tabshead, tab, tabs);
702 cmd_tab_close_other(struct buffer *buffer)
706 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
707 if (t->flags & TAB_CURRENT)
711 TAILQ_REMOVE(&tabshead, t, tabs);
717 cmd_tab_new(struct buffer *buffer)
719 new_tab(NEW_TAB_URL);
723 cmd_tab_next(struct buffer *buffer)
728 tab->flags &= ~TAB_CURRENT;
730 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
731 t = TAILQ_FIRST(&tabshead);
732 t->flags |= TAB_CURRENT;
733 t->flags &= ~TAB_URGENT;
737 cmd_tab_previous(struct buffer *buffer)
742 tab->flags &= ~TAB_CURRENT;
744 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
745 t = TAILQ_LAST(&tabshead, tabshead);
746 t->flags |= TAB_CURRENT;
747 t->flags &= ~TAB_URGENT;
751 cmd_tab_move(struct buffer *buffer)
756 t = TAILQ_NEXT(tab, tabs);
757 TAILQ_REMOVE(&tabshead, tab, tabs);
760 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
762 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
766 cmd_tab_move_to(struct buffer *buffer)
771 t = TAILQ_PREV(tab, tabshead, tabs);
772 TAILQ_REMOVE(&tabshead, tab, tabs);
775 if (TAILQ_EMPTY(&tabshead))
776 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
778 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
780 TAILQ_INSERT_BEFORE(t, tab, tabs);
784 cmd_load_url(struct buffer *buffer)
787 message("We don't have enable-recursive-minibuffers");
791 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
793 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
794 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
795 cmd_move_end_of_line(&ministate.buffer);
799 cmd_load_current_url(struct buffer *buffer)
801 struct tab *tab = current_tab();
804 message("We don't have enable-recursive-minibuffers");
808 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
810 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
811 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
812 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
816 cmd_bookmark_page(struct buffer *buffer)
818 struct tab *tab = current_tab();
820 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
821 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
822 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
823 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
827 cmd_list_bookmarks(struct buffer *buffer)
829 load_url_in_tab(current_tab(), "about:bookmarks");
833 cmd_toggle_help(struct buffer *buffer)
835 side_window = !side_window;
840 * ugly hack, but otherwise the window doesn't get updated
841 * until I call handle_resize a second time (i.e. C-l). I
842 * will be happy to know why something like this is needed.
844 handle_resize_nodelay(0, 0, NULL);
845 handle_resize_nodelay(0, 0, NULL);
849 cmd_mini_delete_char(struct buffer *buffer)
853 if (!in_minibuffer) {
854 message("text is read-only");
858 minibuffer_taint_hist();
860 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
865 memmove(c, n, strlen(n)+1);
869 cmd_mini_delete_backward_char(struct buffer *buffer)
873 if (!in_minibuffer) {
874 message("text is read-only");
878 minibuffer_taint_hist();
880 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
881 start = buffer->current_line->line;
884 p = utf8_prev_cp(c-1, start);
886 memmove(p, c, strlen(c)+1);
891 cmd_mini_kill_line(struct buffer *buffer)
895 if (!in_minibuffer) {
896 message("text is read-only");
900 minibuffer_taint_hist();
901 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
906 cmd_mini_abort(struct buffer *buffer)
915 cmd_mini_complete_and_exit(struct buffer *buffer)
920 minibuffer_taint_hist();
925 cmd_mini_previous_history_element(struct buffer *buffer)
927 if (ministate.history == NULL) {
928 message("No history");
932 if (ministate.hist_cur == NULL ||
933 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
934 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
935 ministate.hist_off = ministate.history->len - 1;
936 if (ministate.hist_cur == NULL)
937 message("No prev item");
939 ministate.hist_off--;
942 if (ministate.hist_cur != NULL)
943 buffer->current_line->line = ministate.hist_cur->h;
947 cmd_mini_next_history_element(struct buffer *buffer)
949 if (ministate.history == NULL) {
950 message("No history");
954 if (ministate.hist_cur == NULL ||
955 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
956 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
957 ministate.hist_off = 0;
958 if (ministate.hist_cur == NULL)
959 message("No next item");
961 ministate.hist_off++;
964 if (ministate.hist_cur != NULL)
965 buffer->current_line->line = ministate.hist_cur->h;
969 global_key_unbound(void)
971 message("%s is undefined", keybuf);
975 minibuffer_hist_save_entry(void)
979 if (ministate.history == NULL)
982 if ((hist = calloc(1, sizeof(*hist))) == NULL)
985 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
987 if (TAILQ_EMPTY(&ministate.history->head))
988 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
990 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
991 ministate.history->len++;
995 * taint the minibuffer cache: if we're currently showing a history
996 * element, copy that to the current buf and reset the "history
1000 minibuffer_taint_hist(void)
1002 if (ministate.hist_cur == NULL)
1005 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1006 ministate.hist_cur = NULL;
1010 minibuffer_self_insert(void)
1012 char *c, tmp[5] = {0};
1015 minibuffer_taint_hist();
1017 if (thiskey.cp == 0)
1020 len = utf8_encode(thiskey.cp, tmp);
1021 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1022 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1025 memmove(c + len, c, strlen(c)+1);
1026 memcpy(c, tmp, len);
1027 ministate.buffer.cpoff++;
1031 eecmd_self_insert(void)
1033 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1034 !unicode_isgraph(thiskey.cp)) {
1035 global_key_unbound();
1039 minibuffer_self_insert();
1047 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1048 if (!strcmp(cmd->cmd, ministate.buf)) {
1050 minibuffer_hist_save_entry();
1051 cmd->fn(current_buffer());
1056 message("No match");
1060 ir_self_insert(void)
1062 minibuffer_self_insert();
1068 char buf[1025] = {0};
1069 struct phos_uri uri;
1072 tab = current_tab();
1075 minibuffer_hist_save_entry();
1077 /* a bit ugly but... */
1078 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1079 phos_uri_set_query(&uri, ministate.buf);
1080 phos_serialize_uri(&uri, buf, sizeof(buf));
1081 load_url_in_tab(tab, buf);
1085 lu_self_insert(void)
1087 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1088 !unicode_isgraph(thiskey.key)) {
1089 global_key_unbound();
1093 minibuffer_self_insert();
1100 minibuffer_hist_save_entry();
1101 load_url_in_tab(current_tab(), ministate.buf);
1108 if (*ministate.buf != '\0')
1109 add_to_bookmarks(ministate.buf);
1115 yornp_self_insert(void)
1117 if (thiskey.key != 'y' && thiskey.key != 'n') {
1118 message("Please answer y or n");
1123 yornp_cb(thiskey.key == 'y', yornp_data);
1130 yornp_cb(0, yornp_data);
1134 read_self_insert(void)
1136 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1137 global_key_unbound();
1141 minibuffer_self_insert();
1148 read_cb(NULL, read_data);
1155 minibuffer_hist_save_entry();
1156 read_cb(ministate.buf, read_data);
1159 static struct vline *
1160 nth_line(struct buffer *buffer, size_t n)
1166 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1181 TAILQ_FOREACH(t, &tabshead, tabs) {
1182 if (t->flags & TAB_CURRENT)
1190 static struct buffer *
1191 current_buffer(void)
1194 return &ministate.buffer;
1195 return ¤t_tab()->buffer;
1203 if ((thiskey.key = wgetch(body)) == ERR)
1206 thiskey.meta = thiskey.key == 27;
1208 thiskey.key = wgetch(body);
1209 if (thiskey.key == ERR || thiskey.key == 27) {
1216 if ((unsigned int)thiskey.key < UINT8_MAX) {
1218 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1220 if ((thiskey.key = wgetch(body)) == ERR) {
1221 message("Error decoding user input");
1231 dispatch_stdio(int fd, short ev, void *d)
1234 const char *keyname;
1240 if (keybuf[0] != '\0')
1241 strlcat(keybuf, " ", sizeof(keybuf));
1243 strlcat(keybuf, "M-", sizeof(keybuf));
1244 if (thiskey.cp != 0) {
1245 utf8_encode(thiskey.cp, tmp);
1246 strlcat(keybuf, tmp, sizeof(keybuf));
1248 if ((keyname = unkbd(thiskey.key)) != NULL)
1249 strlcat(keybuf, keyname, sizeof(keybuf));
1251 tmp[0] = thiskey.key;
1252 strlcat(keybuf, tmp, sizeof(keybuf));
1256 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1257 if (k->meta == thiskey.meta &&
1258 k->key == thiskey.key) {
1260 current_map = &k->map;
1262 current_map = base_map;
1263 strlcpy(keybuf, "", sizeof(keybuf));
1264 k->fn(current_buffer());
1270 if (current_map->unhandled_input != NULL)
1271 current_map->unhandled_input();
1273 global_key_unbound();
1276 strlcpy(keybuf, "", sizeof(keybuf));
1277 current_map = base_map;
1283 redraw_tab(current_tab());
1287 handle_clear_minibuf(int fd, short ev, void *d)
1289 free(ministate.curmesg);
1290 ministate.curmesg = NULL;
1292 redraw_minibuffer();
1293 if (in_minibuffer) {
1303 handle_resize(int sig, short ev, void *d)
1305 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1306 event_del(&resizeev);
1308 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1309 evtimer_add(&resizeev, &resize_timer);
1313 handle_resize_nodelay(int s, short ev, void *d)
1321 /* move and resize the windows, in reverse order! */
1323 mvwin(minibuf, LINES-1, 0);
1324 wresize(minibuf, 1, COLS);
1326 mvwin(modeline, LINES-2, 0);
1327 wresize(modeline, 1, COLS);
1329 body_lines = LINES-3;
1333 help_cols = 0.3 * COLS;
1334 help_lines = LINES-3;
1336 wresize(help, help_lines, help_cols);
1338 wrap_page(&helpwin, help_cols);
1340 body_cols = COLS - help_cols - 1;
1341 mvwin(body, 1, help_cols);
1345 wresize(body, body_lines, body_cols);
1347 wresize(tabline, 1, COLS);
1349 tab = current_tab();
1351 wrap_page(&tab->buffer, body_cols);
1356 wrap_page(struct buffer *buffer, int width)
1359 const struct line *orig;
1363 orig = buffer->current_line == NULL
1365 : buffer->current_line->parent;
1366 buffer->current_line = NULL;
1369 buffer->line_off = 0;
1371 empty_vlist(buffer);
1373 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1374 prfx = line_prefixes[l->type].prfx1;
1383 case LINE_PRE_START:
1385 wrap_text(buffer, prfx, l, width);
1387 case LINE_PRE_CONTENT:
1388 hardwrap_text(buffer, l, width);
1392 if (orig == l && buffer->current_line == NULL) {
1393 buffer->line_off = buffer->line_max-1;
1394 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1397 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1398 if (vl == NULL || vl->parent != orig)
1400 buffer->current_line = vl;
1406 if (buffer->current_line == NULL)
1407 buffer->current_line = TAILQ_FIRST(&buffer->head);
1413 print_vline(WINDOW *window, struct vline *vl)
1415 const char *text = vl->line;
1417 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1418 int text_face = line_faces[vl->parent->type].text_prop;
1421 prfx = line_prefixes[vl->parent->type].prfx1;
1423 prfx = line_prefixes[vl->parent->type].prfx2;
1428 wattron(window, prefix_face);
1429 wprintw(window, "%s", prfx);
1430 wattroff(window, prefix_face);
1432 wattron(window, text_face);
1433 wprintw(window, "%s", text);
1434 wattroff(window, text_face);
1438 redraw_tabline(void)
1441 size_t toskip, ots, tabwidth, space, x;
1442 int current, y, truncated;
1446 tabwidth = sizeof(buf)+1;
1450 TAILQ_FOREACH(tab, &tabshead, tabs) {
1452 if (tab->flags & TAB_CURRENT)
1456 if (toskip * tabwidth < space)
1461 while (toskip != 0 &&
1462 (ots - toskip+1) * tabwidth < space)
1467 wattron(tabline, tab_face.background);
1468 wprintw(tabline, toskip == 0 ? " " : "<");
1469 wattroff(tabline, tab_face.background);
1472 TAILQ_FOREACH(tab, &tabshead, tabs) {
1480 getyx(tabline, y, x);
1481 if (x + sizeof(buf)+2 >= (size_t)COLS)
1484 current = tab->flags & TAB_CURRENT;
1486 if (*(title = tab->buffer.page.title) == '\0')
1487 title = tab->hist_cur->h;
1489 if (tab->flags & TAB_URGENT)
1490 strlcpy(buf, "!", sizeof(buf));
1492 strlcpy(buf, " ", sizeof(buf));
1494 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1495 /* truncation happens */
1496 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1498 /* pad with spaces */
1499 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1504 wattron(tabline, tab_face.current_tab);
1506 wattron(tabline, tab_face.tab);
1508 wprintw(tabline, "%s", buf);
1509 if (TAILQ_NEXT(tab, tabs) != NULL)
1510 wprintw(tabline, " ");
1513 wattroff(tabline, tab_face.current_tab);
1515 wattroff(tabline, tab_face.tab);
1518 wattron(tabline, tab_face.background);
1519 for (; x < (size_t)COLS; ++x)
1520 waddch(tabline, ' ');
1522 mvwprintw(tabline, 0, COLS-1, ">");
1526 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1533 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1534 if (TAILQ_EMPTY(&buffer->head))
1538 vl = nth_line(buffer, buffer->line_off);
1539 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1541 print_vline(win, vl);
1547 wmove(win, buffer->curs_y, buffer->curs_x);
1553 redraw_window(help, help_lines, &helpwin);
1557 redraw_body(struct tab *tab)
1559 redraw_window(body, body_lines, &tab->buffer);
1563 trust_status_char(enum trust_state ts)
1566 case TS_UNKNOWN: return 'u';
1567 case TS_UNTRUSTED: return '!';
1568 case TS_TRUSTED: return 'v';
1569 case TS_VERIFIED: return 'V';
1574 redraw_modeline(struct tab *tab)
1577 int x, y, max_x, max_y;
1578 const char *mode = tab->buffer.page.name;
1579 const char *spin = "-\\|/";
1582 wattron(modeline, A_REVERSE);
1583 wmove(modeline, 0, 0);
1585 wprintw(modeline, "-%c%c %s ",
1586 spin[tab->loading_anim_step],
1587 trust_status_char(tab->trust),
1588 mode == NULL ? "(none)" : mode);
1590 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1592 if (tab->buffer.line_max <= (size_t)body_lines)
1593 wprintw(modeline, "All ");
1594 else if (tab->buffer.line_off == 0)
1595 wprintw(modeline, "Top ");
1596 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1597 wprintw(modeline, "Bottom ");
1599 wprintw(modeline, "%.0f%% ", pct);
1601 wprintw(modeline, "%d/%d %s ",
1602 tab->buffer.line_off + tab->buffer.curs_y,
1603 tab->buffer.line_max,
1606 getyx(modeline, y, x);
1607 getmaxyx(modeline, max_y, max_x);
1612 for (; x < max_x; ++x)
1613 waddstr(modeline, "-");
1617 redraw_minibuffer(void)
1620 size_t off_y, off_x = 0;
1625 if (in_minibuffer) {
1626 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1627 if (ministate.hist_cur != NULL)
1628 wprintw(minibuf, "(%zu/%zu) ",
1629 ministate.hist_off + 1,
1630 ministate.history->len);
1632 getyx(minibuf, off_y, off_x);
1634 start = ministate.hist_cur != NULL
1635 ? ministate.hist_cur->h
1637 c = utf8_nth(ministate.buffer.current_line->line,
1638 ministate.buffer.cpoff);
1639 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1640 start = utf8_next_cp(start);
1643 waddstr(minibuf, start);
1646 if (ministate.curmesg != NULL)
1647 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1650 if (!in_minibuffer && ministate.curmesg == NULL)
1651 waddstr(minibuf, keybuf);
1653 /* If nothing else, show the URL at point */
1654 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1655 tab = current_tab();
1656 if (tab->buffer.current_line != NULL &&
1657 tab->buffer.current_line->parent->type == LINE_LINK)
1658 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1662 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1666 redraw_tab(struct tab *tab)
1675 redraw_modeline(tab);
1676 redraw_minibuffer();
1678 wnoutrefresh(tabline);
1679 wnoutrefresh(modeline);
1681 if (in_minibuffer) {
1683 wnoutrefresh(minibuf);
1685 wnoutrefresh(minibuf);
1693 emit_help_item(char *prfx, void *fn)
1698 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1702 assert(cmd != NULL);
1704 if ((l = calloc(1, sizeof(*l))) == NULL)
1707 l->type = LINE_TEXT;
1710 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1712 if (TAILQ_EMPTY(&helpwin.page.head))
1713 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1715 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1719 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1725 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1726 strlcpy(p, prfx, sizeof(p));
1728 strlcat(p, " ", sizeof(p));
1730 strlcat(p, "M-", sizeof(p));
1731 if ((kn = unkbd(k->key)) != NULL)
1732 strlcat(p, kn, sizeof(p));
1734 strlcat(p, keyname(k->key), sizeof(p));
1737 rec_compute_help(&k->map, p, sizeof(p));
1739 emit_help_item(p, k->fn);
1744 recompute_help(void)
1748 empty_vlist(&helpwin);
1749 empty_linelist(&helpwin);
1750 rec_compute_help(current_map, p, sizeof(p));
1751 wrap_page(&helpwin, help_cols);
1755 vmessage(const char *fmt, va_list ap)
1757 if (evtimer_pending(&clminibufev, NULL))
1758 evtimer_del(&clminibufev);
1759 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1760 evtimer_add(&clminibufev, &clminibufev_timer);
1762 free(ministate.curmesg);
1764 /* TODO: what to do if the allocation fails here? */
1765 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1766 ministate.curmesg = NULL;
1768 redraw_minibuffer();
1769 if (in_minibuffer) {
1779 message(const char *fmt, ...)
1789 start_loading_anim(struct tab *tab)
1791 if (tab->loading_anim)
1793 tab->loading_anim = 1;
1794 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1795 evtimer_add(&tab->loadingev, &loadingev_timer);
1799 update_loading_anim(int fd, short ev, void *d)
1801 struct tab *tab = d;
1803 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1805 if (tab->flags & TAB_CURRENT) {
1806 redraw_modeline(tab);
1813 evtimer_add(&tab->loadingev, &loadingev_timer);
1817 stop_loading_anim(struct tab *tab)
1819 if (!tab->loading_anim)
1821 evtimer_del(&tab->loadingev);
1822 tab->loading_anim = 0;
1823 tab->loading_anim_step = 0;
1825 if (!(tab->flags & TAB_CURRENT))
1828 redraw_modeline(tab);
1837 load_url_in_tab(struct tab *tab, const char *url)
1839 message("Loading %s...", url);
1840 start_loading_anim(tab);
1843 tab->buffer.curs_x = 0;
1844 tab->buffer.curs_y = 0;
1849 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1850 void (*abortfn)(void), struct histhead *hist)
1853 base_map = &minibuffer_map;
1854 current_map = &minibuffer_map;
1856 base_map->unhandled_input = self_insert_fn;
1858 ministate.donefn = donefn;
1859 ministate.abortfn = abortfn;
1860 memset(ministate.buf, 0, sizeof(ministate.buf));
1861 ministate.buffer.current_line = &ministate.vline;
1862 ministate.buffer.current_line->line = ministate.buf;
1863 ministate.buffer.cpoff = 0;
1864 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1866 ministate.history = hist;
1867 ministate.hist_cur = NULL;
1868 ministate.hist_off = 0;
1872 exit_minibuffer(void)
1877 base_map = &global_map;
1878 current_map = &global_map;
1882 switch_to_tab(struct tab *tab)
1886 TAILQ_FOREACH(t, &tabshead, tabs) {
1887 t->flags &= ~TAB_CURRENT;
1890 tab->flags |= TAB_CURRENT;
1891 tab->flags &= ~TAB_URGENT;
1897 return tab_counter++;
1901 new_tab(const char *url)
1905 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1911 TAILQ_INIT(&tab->hist.head);
1913 TAILQ_INIT(&tab->buffer.head);
1915 tab->id = tab_new_id();
1918 if (TAILQ_EMPTY(&tabshead))
1919 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1921 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1923 load_url_in_tab(tab, url);
1928 session_new_tab_cb(const char *url)
1936 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1940 ui_init(int argc, char * const *argv)
1942 const char *url = NEW_TAB_URL;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1958 setlocale(LC_ALL, "");
1960 TAILQ_INIT(&global_map.m);
1961 global_map.unhandled_input = global_key_unbound;
1963 TAILQ_INIT(&minibuffer_map.m);
1965 TAILQ_INIT(&eecmd_history.head);
1966 TAILQ_INIT(&ir_history.head);
1967 TAILQ_INIT(&lu_history.head);
1969 ministate.line.type = LINE_TEXT;
1970 ministate.vline.parent = &ministate.line;
1971 ministate.buffer.current_line = &ministate.vline;
1973 /* initialize help window */
1974 TAILQ_INIT(&helpwin.head);
1976 base_map = &global_map;
1977 current_map = &global_map;
1978 load_default_keys();
1985 intrflush(stdscr, FALSE);
1987 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1989 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1991 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1993 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1995 if ((help = newwin(1, 1, 1, 0)) == NULL)
1998 body_lines = LINES-3;
2002 scrollok(body, TRUE);
2004 /* non-blocking input */
2007 mvwprintw(body, 0, 0, "");
2009 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2010 event_add(&stdioev, NULL);
2012 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2013 signal_add(&winchev, NULL);
2015 load_last_session(session_new_tab_cb);
2016 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2023 ui_on_tab_loaded(struct tab *tab)
2025 stop_loading_anim(tab);
2026 message("Loaded %s", tab->hist_cur->h);
2037 ui_on_tab_refresh(struct tab *tab)
2039 wrap_page(&tab->buffer, body_cols);
2040 if (tab->flags & TAB_CURRENT) {
2041 restore_cursor(&tab->buffer);
2044 tab->flags |= TAB_URGENT;
2048 ui_require_input(struct tab *tab, int hide)
2050 /* TODO: hard-switching to another tab is ugly */
2053 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2055 strlcpy(ministate.prompt, "Input required: ",
2056 sizeof(ministate.prompt));
2061 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2066 if (in_minibuffer) {
2073 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2076 len = sizeof(ministate.prompt);
2077 strlcpy(ministate.prompt, prompt, len);
2078 strlcat(ministate.prompt, " (y or n) ", len);
2079 redraw_tab(current_tab());
2083 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2093 enter_minibuffer(read_self_insert, read_select, read_abort,
2096 len = sizeof(ministate.prompt);
2097 strlcpy(ministate.prompt, prompt, len);
2098 strlcat(ministate.prompt, ": ", len);
2099 redraw_tab(current_tab());
2103 ui_notify(const char *fmt, ...)