Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
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
27 * first/last line!
28 *
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.
32 *
33 */
35 #include "telescope.h"
36 #include "cmd.gen.h"
38 #include <assert.h>
39 #include <curses.h>
40 #include <event.h>
41 #include <limits.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.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 int x_offset;
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;
119 static WINDOW *help;
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,
140 minibuffer_map,
141 *current_map,
142 *base_map;
144 static struct histhead eecmd_history,
145 ir_history,
146 lu_history,
147 read_history;
149 static int in_minibuffer;
151 static struct {
152 char *curmesg;
154 char prompt[64];
155 void (*donefn)(void);
156 void (*abortfn)(void);
158 char buf[1025];
159 struct line line;
160 struct vline vline;
161 struct buffer buffer;
163 struct histhead *history;
164 struct hist *hist_cur;
165 size_t hist_off;
166 } ministate;
168 static inline void
169 update_x_offset()
171 if (olivetti_mode && fill_column < body_cols)
172 x_offset = (body_cols - fill_column)/2;
173 else
174 x_offset = 0;
177 static inline void
178 global_set_key(const char *key, void (*fn)(struct buffer*))
180 if (!kmap_define_key(&global_map, key, fn))
181 _exit(1);
184 static inline void
185 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
187 if (!kmap_define_key(&minibuffer_map, key, fn))
188 _exit(1);
191 static void
192 load_default_keys(void)
194 /* === global map === */
196 /* emacs */
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);
236 /* vi/vi-like */
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);
262 /* tmp */
263 global_set_key("q", cmd_kill_telescope);
265 global_set_key("esc", cmd_clear_minibuf);
267 global_set_key(":", cmd_execute_extended_command);
269 /* cua */
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);
283 /* global */
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);
316 static void
317 restore_cursor(struct buffer *buffer)
319 struct vline *vl;
320 const char *prfx;
322 vl = buffer->current_line;
323 if (vl == NULL || vl->line == NULL)
324 buffer->curs_x = buffer->cpoff = 0;
325 else
326 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
328 buffer->curs_x += x_offset;
330 if (vl != NULL) {
331 prfx = line_prefixes[vl->parent->type].prfx1;
332 buffer->curs_x += utf8_swidth(prfx);
336 void
337 cmd_previous_line(struct buffer *buffer)
339 struct vline *vl;
341 if (buffer->current_line == NULL
342 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
343 return;
345 if (--buffer->curs_y < 0) {
346 buffer->curs_y = 0;
347 cmd_scroll_line_up(buffer);
348 return;
351 buffer->current_line = vl;
352 restore_cursor(buffer);
355 void
356 cmd_next_line(struct buffer *buffer)
358 struct vline *vl;
360 if (buffer->current_line == NULL
361 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
362 return;
364 if (++buffer->curs_y > body_lines-1) {
365 buffer->curs_y = body_lines-1;
366 cmd_scroll_line_down(buffer);
367 return;
370 buffer->current_line = vl;
371 restore_cursor(buffer);
374 void
375 cmd_backward_char(struct buffer *buffer)
377 if (buffer->cpoff != 0)
378 buffer->cpoff--;
379 restore_cursor(buffer);
382 void
383 cmd_forward_char(struct buffer *buffer)
385 size_t len = 0;
387 if (buffer->current_line->line != NULL)
388 len = utf8_cplen(buffer->current_line->line);
389 if (++buffer->cpoff > len)
390 buffer->cpoff = len;
391 restore_cursor(buffer);
394 void
395 cmd_backward_paragraph(struct buffer *buffer)
397 do {
398 if (buffer->current_line == NULL ||
399 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
400 message("No previous paragraph");
401 return;
403 cmd_previous_line(buffer);
404 } while (buffer->current_line->line != NULL ||
405 buffer->current_line->parent->type != LINE_TEXT);
408 void
409 cmd_forward_paragraph(struct buffer *buffer)
411 do {
412 if (buffer->current_line == NULL ||
413 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
414 message("No next paragraph");
415 return;
417 cmd_next_line(buffer);
418 } while (buffer->current_line->line != NULL ||
419 buffer->current_line->parent->type != LINE_TEXT);
422 void
423 cmd_move_beginning_of_line(struct buffer *buffer)
425 buffer->cpoff = 0;
426 restore_cursor(buffer);
429 void
430 cmd_move_end_of_line(struct buffer *buffer)
432 struct vline *vl;
434 vl = buffer->current_line;
435 if (vl->line == NULL)
436 return;
437 buffer->cpoff = utf8_cplen(vl->line);
438 restore_cursor(buffer);
441 void
442 cmd_redraw(struct buffer *buffer)
444 handle_resize(0, 0, NULL);
447 void
448 cmd_scroll_line_up(struct buffer *buffer)
450 struct vline *vl;
452 if (buffer->current_line == NULL)
453 return;
455 if ((vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
456 return;
458 buffer->current_line = vl;
459 buffer->line_off--;
460 restore_cursor(buffer);
463 void
464 cmd_scroll_line_down(struct buffer *buffer)
466 struct vline *vl;
468 if (buffer->current_line == NULL)
469 return;
471 if ((vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
472 return;
474 buffer->current_line = vl;
475 buffer->line_off++;
476 restore_cursor(buffer);
479 void
480 cmd_scroll_up(struct buffer *buffer)
482 size_t off;
484 off = body_lines-1;
486 for (; off > 0; --off)
487 cmd_scroll_line_up(buffer);
490 void
491 cmd_scroll_down(struct buffer *buffer)
493 size_t off;
495 off = body_lines-1;
497 for (; off > 0; --off)
498 cmd_scroll_line_down(buffer);
501 void
502 cmd_beginning_of_buffer(struct buffer *buffer)
504 buffer->current_line = TAILQ_FIRST(&buffer->head);
505 buffer->line_off = 0;
506 buffer->curs_y = 0;
507 buffer->cpoff = 0;
508 restore_cursor(buffer);
511 void
512 cmd_end_of_buffer(struct buffer *buffer)
514 ssize_t off;
516 off = buffer->line_max - body_lines;
517 off = MAX(0, off);
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);
527 void
528 cmd_kill_telescope(struct buffer *buffer)
530 save_session();
531 event_loopbreak();
534 void
535 cmd_push_button(struct buffer *buffer)
537 struct vline *vl;
538 size_t nth;
540 nth = buffer->line_off + buffer->curs_y;
541 if (nth >= buffer->line_max)
542 return;
543 vl = nth_line(buffer, nth);
544 if (vl->parent->type != LINE_LINK)
545 return;
547 load_url_in_tab(current_tab(), vl->parent->alt);
550 void
551 cmd_push_button_new_tab(struct buffer *buffer)
553 struct vline *vl;
554 size_t nth;
556 nth = buffer->line_off + buffer->curs_y;
557 if (nth > buffer->line_max)
558 return;
559 vl = nth_line(buffer, nth);
560 if (vl->parent->type != LINE_LINK)
561 return;
563 new_tab(vl->parent->alt);
566 void
567 cmd_previous_button(struct buffer *buffer)
569 do {
570 if (buffer->current_line == NULL ||
571 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
572 message("No previous link");
573 return;
575 cmd_previous_line(buffer);
576 } while (buffer->current_line->parent->type != LINE_LINK);
579 void
580 cmd_next_button(struct buffer *buffer)
582 do {
583 if (buffer->current_line == NULL ||
584 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
585 message("No next link");
586 return;
588 cmd_next_line(buffer);
589 } while (buffer->current_line->parent->type != LINE_LINK);
592 void
593 cmd_previous_page(struct buffer *buffer)
595 struct tab *tab = current_tab();
597 if (!load_previous_page(tab))
598 message("No previous page");
599 else
600 start_loading_anim(tab);
603 void
604 cmd_next_page(struct buffer *buffer)
606 struct tab *tab = current_tab();
608 if (!load_next_page(tab))
609 message("No next page");
610 else
611 start_loading_anim(tab);
614 void
615 cmd_clear_minibuf(struct buffer *buffer)
617 handle_clear_minibuf(0, 0, NULL);
620 void
621 cmd_execute_extended_command(struct buffer *buffer)
623 size_t len;
625 if (in_minibuffer) {
626 message("We don't have enable-recursive-minibuffers");
627 return;
630 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
631 &eecmd_history);
633 len = sizeof(ministate.prompt);
634 strlcpy(ministate.prompt, "", len);
636 if (thiskey.meta)
637 strlcat(ministate.prompt, "M-", len);
639 strlcat(ministate.prompt, keyname(thiskey.key), len);
641 if (thiskey.meta)
642 strlcat(ministate.prompt, " ", len);
645 void
646 cmd_tab_close(struct buffer *buffer)
648 struct tab *tab, *t;
650 tab = current_tab();
651 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
652 TAILQ_NEXT(tab, tabs) == NULL) {
653 message("Can't close the only tab.");
654 return;
657 if (evtimer_pending(&tab->loadingev, NULL))
658 evtimer_del(&tab->loadingev);
660 stop_tab(tab);
662 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
663 t = TAILQ_NEXT(tab, tabs);
664 TAILQ_REMOVE(&tabshead, tab, tabs);
665 free(tab);
667 switch_to_tab(t);
670 void
671 cmd_tab_close_other(struct buffer *buffer)
673 struct tab *t, *i;
675 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
676 if (t->flags & TAB_CURRENT)
677 continue;
679 stop_tab(t);
680 TAILQ_REMOVE(&tabshead, t, tabs);
681 free(t);
685 void
686 cmd_tab_new(struct buffer *buffer)
688 const char *url;
690 if ((url = new_tab_url) == NULL)
691 url = NEW_TAB_URL;
693 new_tab(url);
696 void
697 cmd_tab_next(struct buffer *buffer)
699 struct tab *tab, *t;
701 tab = current_tab();
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;
710 void
711 cmd_tab_previous(struct buffer *buffer)
713 struct tab *tab, *t;
715 tab = current_tab();
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;
724 void
725 cmd_tab_move(struct buffer *buffer)
727 struct tab *tab, *t;
729 tab = current_tab();
730 t = TAILQ_NEXT(tab, tabs);
731 TAILQ_REMOVE(&tabshead, tab, tabs);
733 if (t == NULL)
734 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
735 else
736 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
739 void
740 cmd_tab_move_to(struct buffer *buffer)
742 struct tab *tab, *t;
744 tab = current_tab();
745 t = TAILQ_PREV(tab, tabshead, tabs);
746 TAILQ_REMOVE(&tabshead, tab, tabs);
748 if (t == NULL) {
749 if (TAILQ_EMPTY(&tabshead))
750 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
751 else
752 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
753 } else
754 TAILQ_INSERT_BEFORE(t, tab, tabs);
757 void
758 cmd_load_url(struct buffer *buffer)
760 if (in_minibuffer) {
761 message("We don't have enable-recursive-minibuffers");
762 return;
765 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
766 &lu_history);
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);
772 void
773 cmd_load_current_url(struct buffer *buffer)
775 struct tab *tab = current_tab();
777 if (in_minibuffer) {
778 message("We don't have enable-recursive-minibuffers");
779 return;
782 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
783 &lu_history);
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);
789 void
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);
800 void
801 cmd_list_bookmarks(struct buffer *buffer)
803 load_url_in_tab(current_tab(), "about:bookmarks");
806 void
807 cmd_toggle_help(struct buffer *buffer)
809 side_window = !side_window;
810 if (side_window)
811 recompute_help();
813 /*
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.
817 */
818 handle_resize_nodelay(0, 0, NULL);
819 handle_resize_nodelay(0, 0, NULL);
822 void
823 cmd_olivetti_mode(struct buffer *buffer)
825 olivetti_mode = !olivetti_mode;
826 if (olivetti_mode)
827 message("olivetti-mode enabled");
828 else
829 message("olivetti-mode disabled");
831 handle_resize_nodelay(0, 0, NULL);
834 void
835 cmd_mini_delete_char(struct buffer *buffer)
837 char *c, *n;
839 if (!in_minibuffer) {
840 message("text is read-only");
841 return;
844 minibuffer_taint_hist();
846 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
847 if (*c == '\0')
848 return;
849 n = utf8_next_cp(c);
851 memmove(c, n, strlen(n)+1);
854 void
855 cmd_mini_delete_backward_char(struct buffer *buffer)
857 char *c, *p, *start;
859 if (!in_minibuffer) {
860 message("text is read-only");
861 return;
864 minibuffer_taint_hist();
866 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
867 start = buffer->current_line->line;
868 if (c == start)
869 return;
870 p = utf8_prev_cp(c-1, start);
872 memmove(p, c, strlen(c)+1);
873 buffer->cpoff--;
876 void
877 cmd_mini_kill_line(struct buffer *buffer)
879 char *c;
881 if (!in_minibuffer) {
882 message("text is read-only");
883 return;
886 minibuffer_taint_hist();
887 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
888 *c = '\0';
891 void
892 cmd_mini_abort(struct buffer *buffer)
894 if (!in_minibuffer)
895 return;
897 ministate.abortfn();
900 void
901 cmd_mini_complete_and_exit(struct buffer *buffer)
903 if (!in_minibuffer)
904 return;
906 minibuffer_taint_hist();
907 ministate.donefn();
910 void
911 cmd_mini_previous_history_element(struct buffer *buffer)
913 if (ministate.history == NULL) {
914 message("No history");
915 return;
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");
924 } else {
925 ministate.hist_off--;
928 if (ministate.hist_cur != NULL)
929 buffer->current_line->line = ministate.hist_cur->h;
932 void
933 cmd_mini_next_history_element(struct buffer *buffer)
935 if (ministate.history == NULL) {
936 message("No history");
937 return;
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");
946 } else {
947 ministate.hist_off++;
950 if (ministate.hist_cur != NULL)
951 buffer->current_line->line = ministate.hist_cur->h;
954 static void
955 global_key_unbound(void)
957 message("%s is undefined", keybuf);
960 static void
961 minibuffer_hist_save_entry(void)
963 struct hist *hist;
965 if (ministate.history == NULL)
966 return;
968 if ((hist = calloc(1, sizeof(*hist))) == NULL)
969 abort();
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);
975 else
976 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
977 ministate.history->len++;
980 /*
981 * taint the minibuffer cache: if we're currently showing a history
982 * element, copy that to the current buf and reset the "history
983 * navigation" thing.
984 */
985 static void
986 minibuffer_taint_hist(void)
988 if (ministate.hist_cur == NULL)
989 return;
991 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
992 ministate.hist_cur = NULL;
995 static void
996 minibuffer_self_insert(void)
998 char *c, tmp[5] = {0};
999 size_t len;
1001 minibuffer_taint_hist();
1003 if (thiskey.cp == 0)
1004 return;
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)
1009 return;
1011 memmove(c + len, c, strlen(c)+1);
1012 memcpy(c, tmp, len);
1013 ministate.buffer.cpoff++;
1016 static void
1017 eecmd_self_insert(void)
1019 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1020 !unicode_isgraph(thiskey.cp)) {
1021 global_key_unbound();
1022 return;
1025 minibuffer_self_insert();
1028 static void
1029 eecmd_select(void)
1031 struct cmds *cmd;
1033 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1034 if (!strcmp(cmd->cmd, ministate.buf)) {
1035 exit_minibuffer();
1036 minibuffer_hist_save_entry();
1037 cmd->fn(current_buffer());
1038 return;
1042 message("No match");
1045 static void
1046 ir_self_insert(void)
1048 minibuffer_self_insert();
1051 static void
1052 ir_select(void)
1054 char buf[1025] = {0};
1055 struct phos_uri uri;
1056 struct tab *tab;
1058 tab = current_tab();
1060 exit_minibuffer();
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);
1070 static void
1071 lu_self_insert(void)
1073 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1074 !unicode_isgraph(thiskey.key)) {
1075 global_key_unbound();
1076 return;
1079 minibuffer_self_insert();
1082 static void
1083 lu_select(void)
1085 exit_minibuffer();
1086 minibuffer_hist_save_entry();
1087 load_url_in_tab(current_tab(), ministate.buf);
1090 static void
1091 bp_select(void)
1093 exit_minibuffer();
1094 if (*ministate.buf != '\0')
1095 add_to_bookmarks(ministate.buf);
1096 else
1097 message("Abort.");
1100 static void
1101 yornp_self_insert(void)
1103 if (thiskey.key != 'y' && thiskey.key != 'n') {
1104 message("Please answer y or n");
1105 return;
1108 exit_minibuffer();
1109 yornp_cb(thiskey.key == 'y', yornp_data);
1112 static void
1113 yornp_abort(void)
1115 exit_minibuffer();
1116 yornp_cb(0, yornp_data);
1119 static void
1120 read_self_insert(void)
1122 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1123 global_key_unbound();
1124 return;
1127 minibuffer_self_insert();
1130 static void
1131 read_abort(void)
1133 exit_minibuffer();
1134 read_cb(NULL, read_data);
1137 static void
1138 read_select(void)
1140 exit_minibuffer();
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)
1148 struct vline *vl;
1149 size_t i;
1151 i = 0;
1152 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1153 if (i == n)
1154 return vl;
1155 i++;
1158 /* unreachable */
1159 abort();
1162 static struct tab *
1163 current_tab(void)
1165 struct tab *t;
1167 TAILQ_FOREACH(t, &tabshead, tabs) {
1168 if (t->flags & TAB_CURRENT)
1169 return t;
1172 /* unreachable */
1173 abort();
1176 static struct buffer *
1177 current_buffer(void)
1179 if (in_minibuffer)
1180 return &ministate.buffer;
1181 return &current_tab()->buffer;
1184 static int
1185 readkey(void)
1187 uint32_t state = 0;
1189 if ((thiskey.key = wgetch(body)) == ERR)
1190 return 0;
1192 thiskey.meta = thiskey.key == 27;
1193 if (thiskey.meta) {
1194 thiskey.key = wgetch(body);
1195 if (thiskey.key == ERR || thiskey.key == 27) {
1196 thiskey.meta = 0;
1197 thiskey.key = 27;
1201 thiskey.cp = 0;
1202 if ((unsigned int)thiskey.key < UINT8_MAX) {
1203 while (1) {
1204 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1205 break;
1206 if ((thiskey.key = wgetch(body)) == ERR) {
1207 message("Error decoding user input");
1208 return 0;
1213 return 1;
1216 static void
1217 dispatch_stdio(int fd, short ev, void *d)
1219 struct keymap *k;
1220 const char *keyname;
1221 char tmp[5] = {0};
1223 if (!readkey())
1224 return;
1226 if (keybuf[0] != '\0')
1227 strlcat(keybuf, " ", sizeof(keybuf));
1228 if (thiskey.meta)
1229 strlcat(keybuf, "M-", sizeof(keybuf));
1230 if (thiskey.cp != 0) {
1231 utf8_encode(thiskey.cp, tmp);
1232 strlcat(keybuf, tmp, sizeof(keybuf));
1233 } else {
1234 if ((keyname = unkbd(thiskey.key)) != NULL)
1235 strlcat(keybuf, keyname, sizeof(keybuf));
1236 else {
1237 tmp[0] = thiskey.key;
1238 strlcat(keybuf, tmp, sizeof(keybuf));
1242 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1243 if (k->meta == thiskey.meta &&
1244 k->key == thiskey.key) {
1245 if (k->fn == NULL)
1246 current_map = &k->map;
1247 else {
1248 current_map = base_map;
1249 strlcpy(keybuf, "", sizeof(keybuf));
1250 k->fn(current_buffer());
1252 goto done;
1256 if (current_map->unhandled_input != NULL)
1257 current_map->unhandled_input();
1258 else
1259 global_key_unbound();
1261 strlcpy(keybuf, "", sizeof(keybuf));
1262 current_map = base_map;
1264 done:
1265 if (side_window)
1266 recompute_help();
1268 redraw_tab(current_tab());
1271 static void
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) {
1279 wrefresh(body);
1280 wrefresh(minibuf);
1281 } else {
1282 wrefresh(minibuf);
1283 wrefresh(body);
1287 static void
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);
1297 static void
1298 handle_resize_nodelay(int s, short ev, void *d)
1300 struct tab *tab;
1302 endwin();
1303 refresh();
1304 clear();
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;
1315 body_cols = COLS;
1317 if (side_window) {
1318 help_cols = 0.3 * COLS;
1319 help_lines = LINES-3;
1320 mvwin(help, 1, 0);
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);
1327 } else
1328 mvwin(body, 1, 0);
1330 update_x_offset();
1331 wresize(body, body_lines, body_cols);
1333 wresize(tabline, 1, COLS);
1335 tab = current_tab();
1337 wrap_page(&tab->buffer, body_cols);
1338 redraw_tab(tab);
1341 static int
1342 wrap_page(struct buffer *buffer, int width)
1344 struct line *l;
1345 const struct line *orig;
1346 struct vline *vl;
1347 int pre_width;
1348 const char *prfx;
1350 orig = buffer->current_line == NULL
1351 ? NULL
1352 : buffer->current_line->parent;
1353 buffer->current_line = NULL;
1355 buffer->force_redraw = 1;
1356 buffer->curs_y = 0;
1357 buffer->line_off = 0;
1359 empty_vlist(buffer);
1361 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1362 prfx = line_prefixes[l->type].prfx1;
1363 switch (l->type) {
1364 case LINE_TEXT:
1365 case LINE_LINK:
1366 case LINE_TITLE_1:
1367 case LINE_TITLE_2:
1368 case LINE_TITLE_3:
1369 case LINE_ITEM:
1370 case LINE_QUOTE:
1371 case LINE_PRE_START:
1372 case LINE_PRE_END:
1373 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1374 break;
1375 case LINE_PRE_CONTENT:
1376 if (olivetti_mode)
1377 pre_width = MIN(fill_column, width);
1378 else
1379 pre_width = width;
1380 hardwrap_text(buffer, l, pre_width);
1381 break;
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);
1388 while (1) {
1389 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1390 if (vl == NULL || vl->parent != orig)
1391 break;
1392 buffer->current_line = vl;
1393 buffer->line_off--;
1398 if (buffer->current_line == NULL)
1399 buffer->current_line = TAILQ_FIRST(&buffer->head);
1401 return 1;
1404 static void
1405 print_vline(WINDOW *window, struct vline *vl)
1407 const char *text = vl->line;
1408 const char *prfx;
1409 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1410 int text_face = line_faces[vl->parent->type].text_prop;
1412 if (!vl->flags)
1413 prfx = line_prefixes[vl->parent->type].prfx1;
1414 else
1415 prfx = line_prefixes[vl->parent->type].prfx2;
1417 if (text == NULL)
1418 text = "";
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);
1429 static void
1430 redraw_tabline(void)
1432 struct tab *tab;
1433 size_t toskip, ots, tabwidth, space, x;
1434 int current, y, truncated;
1435 const char *title;
1436 char buf[25];
1438 tabwidth = sizeof(buf)+1;
1439 space = COLS-2;
1441 toskip = 0;
1442 TAILQ_FOREACH(tab, &tabshead, tabs) {
1443 toskip++;
1444 if (tab->flags & TAB_CURRENT)
1445 break;
1448 if (toskip * tabwidth < space)
1449 toskip = 0;
1450 else {
1451 ots = toskip;
1452 toskip--;
1453 while (toskip != 0 &&
1454 (ots - toskip+1) * tabwidth < space)
1455 toskip--;
1458 werase(tabline);
1459 wattron(tabline, tab_face.background);
1460 wprintw(tabline, toskip == 0 ? " " : "<");
1461 wattroff(tabline, tab_face.background);
1463 truncated = 0;
1464 TAILQ_FOREACH(tab, &tabshead, tabs) {
1465 if (truncated)
1466 break;
1467 if (toskip != 0) {
1468 toskip--;
1469 continue;
1472 getyx(tabline, y, x);
1473 if (x + sizeof(buf)+2 >= (size_t)COLS)
1474 truncated = 1;
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));
1483 else
1484 strlcpy(buf, " ", sizeof(buf));
1486 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1487 /* truncation happens */
1488 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1489 } else {
1490 /* pad with spaces */
1491 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1492 /* nop */ ;
1495 if (current)
1496 wattron(tabline, tab_face.current_tab);
1497 else
1498 wattron(tabline, tab_face.tab);
1500 wprintw(tabline, "%s", buf);
1501 if (TAILQ_NEXT(tab, tabs) != NULL)
1502 wprintw(tabline, " ");
1504 if (current)
1505 wattroff(tabline, tab_face.current_tab);
1506 else
1507 wattroff(tabline, tab_face.tab);
1510 wattron(tabline, tab_face.background);
1511 for (; x < (size_t)COLS; ++x)
1512 waddch(tabline, ' ');
1513 if (truncated)
1514 mvwprintw(tabline, 0, COLS-1, ">");
1517 static void
1518 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1520 struct vline *vl;
1521 int l;
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)
1530 goto end;
1532 werase(win);
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))
1539 goto end;
1541 l = 0;
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);
1546 l++;
1547 if (l == height)
1548 break;
1551 end:
1552 wmove(win, buffer->curs_y, buffer->curs_x);
1555 static void
1556 redraw_help(void)
1558 redraw_window(help, help_lines, &helpwin);
1561 static void
1562 redraw_body(struct tab *tab)
1564 redraw_window(body, body_lines, &tab->buffer);
1567 static inline char
1568 trust_status_char(enum trust_state ts)
1570 switch (ts) {
1571 case TS_UNKNOWN: return 'u';
1572 case TS_UNTRUSTED: return '!';
1573 case TS_TRUSTED: return 'v';
1574 case TS_VERIFIED: return 'V';
1578 static void
1579 redraw_modeline(struct tab *tab)
1581 double pct;
1582 int x, y, max_x, max_y;
1583 const char *mode = tab->buffer.page.name;
1584 const char *spin = "-\\|/";
1586 werase(modeline);
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 ");
1603 else
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,
1609 tab->hist_cur->h);
1611 getyx(modeline, y, x);
1612 getmaxyx(modeline, max_y, max_x);
1614 (void)y;
1615 (void)max_y;
1617 for (; x < max_x; ++x)
1618 waddstr(modeline, "-");
1620 wattroff(modeline, modeline_face.background);
1623 static void
1624 redraw_minibuffer(void)
1626 struct tab *tab;
1627 size_t off_y, off_x = 0;
1628 char *start, *c;
1630 wattron(minibuf, minibuffer_face.background);
1631 werase(minibuf);
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
1644 : ministate.buf;
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",
1656 ministate.curmesg);
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);
1669 if (in_minibuffer)
1670 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1672 wattroff(minibuf, minibuffer_face.background);
1675 static void
1676 redraw_tab(struct tab *tab)
1678 if (side_window) {
1679 redraw_help();
1680 wnoutrefresh(help);
1683 restore_cursor(&tab->buffer);
1685 redraw_tabline();
1686 redraw_body(tab);
1687 redraw_modeline(tab);
1688 redraw_minibuffer();
1690 wnoutrefresh(tabline);
1691 wnoutrefresh(modeline);
1693 if (in_minibuffer) {
1694 wnoutrefresh(body);
1695 wnoutrefresh(minibuf);
1696 } else {
1697 wnoutrefresh(minibuf);
1698 wnoutrefresh(body);
1701 doupdate();
1704 static void
1705 emit_help_item(char *prfx, void *fn)
1707 struct line *l;
1708 struct cmds *cmd;
1710 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1711 if (fn == cmd->fn)
1712 break;
1714 assert(cmd != NULL);
1716 if ((l = calloc(1, sizeof(*l))) == NULL)
1717 abort();
1719 l->type = LINE_TEXT;
1720 l->alt = NULL;
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);
1726 else
1727 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1730 static void
1731 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1733 struct keymap *k;
1734 char p[32];
1735 const char *kn;
1737 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1738 strlcpy(p, prfx, sizeof(p));
1739 if (*p != '\0')
1740 strlcat(p, " ", sizeof(p));
1741 if (k->meta)
1742 strlcat(p, "M-", sizeof(p));
1743 if ((kn = unkbd(k->key)) != NULL)
1744 strlcat(p, kn, sizeof(p));
1745 else
1746 strlcat(p, keyname(k->key), sizeof(p));
1748 if (k->fn == NULL)
1749 rec_compute_help(&k->map, p, sizeof(p));
1750 else
1751 emit_help_item(p, k->fn);
1755 static void
1756 recompute_help(void)
1758 char p[32] = { 0 };
1760 empty_vlist(&helpwin);
1761 empty_linelist(&helpwin);
1762 rec_compute_help(current_map, p, sizeof(p));
1763 wrap_page(&helpwin, help_cols);
1766 static void
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) {
1782 wrefresh(body);
1783 wrefresh(minibuf);
1784 } else {
1785 wrefresh(minibuf);
1786 wrefresh(body);
1790 static void
1791 message(const char *fmt, ...)
1793 va_list ap;
1795 va_start(ap, fmt);
1796 vmessage(fmt, ap);
1797 va_end(ap);
1800 static void
1801 start_loading_anim(struct tab *tab)
1803 if (tab->loading_anim)
1804 return;
1805 tab->loading_anim = 1;
1806 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1807 evtimer_add(&tab->loadingev, &loadingev_timer);
1810 static void
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);
1819 wrefresh(modeline);
1820 wrefresh(body);
1821 if (in_minibuffer)
1822 wrefresh(minibuf);
1825 evtimer_add(&tab->loadingev, &loadingev_timer);
1828 static void
1829 stop_loading_anim(struct tab *tab)
1831 if (!tab->loading_anim)
1832 return;
1833 evtimer_del(&tab->loadingev);
1834 tab->loading_anim = 0;
1835 tab->loading_anim_step = 0;
1837 if (!(tab->flags & TAB_CURRENT))
1838 return;
1840 redraw_modeline(tab);
1842 wrefresh(modeline);
1843 wrefresh(body);
1844 if (in_minibuffer)
1845 wrefresh(minibuf);
1848 static void
1849 load_url_in_tab(struct tab *tab, const char *url)
1851 message("Loading %s...", url);
1852 start_loading_anim(tab);
1853 load_url(tab, url);
1855 tab->buffer.curs_x = 0;
1856 tab->buffer.curs_y = 0;
1857 redraw_tab(tab);
1860 static void
1861 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1862 void (*abortfn)(void), struct histhead *hist)
1864 in_minibuffer = 1;
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;
1883 static void
1884 exit_minibuffer(void)
1886 werase(minibuf);
1888 in_minibuffer = 0;
1889 base_map = &global_map;
1890 current_map = &global_map;
1893 static void
1894 switch_to_tab(struct tab *tab)
1896 struct tab *t;
1898 TAILQ_FOREACH(t, &tabshead, tabs) {
1899 t->flags &= ~TAB_CURRENT;
1902 tab->flags |= TAB_CURRENT;
1903 tab->flags &= ~TAB_URGENT;
1906 unsigned int
1907 tab_new_id(void)
1909 return tab_counter++;
1912 static struct tab *
1913 new_tab(const char *url)
1915 struct tab *tab;
1917 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1918 event_loopbreak();
1919 return NULL;
1921 tab->fd = -1;
1923 TAILQ_INIT(&tab->hist.head);
1925 TAILQ_INIT(&tab->buffer.head);
1927 tab->id = tab_new_id();
1928 switch_to_tab(tab);
1930 if (TAILQ_EMPTY(&tabshead))
1931 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1932 else
1933 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1935 load_url_in_tab(tab, url);
1936 return tab;
1939 static void
1940 session_new_tab_cb(const char *url)
1942 new_tab(url);
1945 static void
1946 usage(void)
1948 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1949 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1952 int
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) {
1963 switch (ch) {
1964 case 'c':
1965 fonf = 1;
1966 strlcpy(path, optarg, sizeof(path));
1967 break;
1968 case 'n':
1969 configtest = 1;
1970 break;
1971 case 'h':
1972 usage();
1973 return 0;
1974 default:
1975 usage();
1976 return 1;
1979 argc -= optind;
1980 argv += optind;
1982 parseconfig(path, fonf);
1983 if (configtest){
1984 puts("config OK");
1985 exit(0);
1988 if (argc != 0)
1989 url = argv[0];
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();
2013 initscr();
2014 raw();
2015 noecho();
2017 nonl();
2018 intrflush(stdscr, FALSE);
2020 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2021 return 0;
2022 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2023 return 0;
2024 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2025 return 0;
2026 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2027 return 0;
2028 if ((help = newwin(1, 1, 1, 0)) == NULL)
2029 return 0;
2031 body_lines = LINES-3;
2032 body_cols = COLS;
2034 update_x_offset();
2036 keypad(body, TRUE);
2037 scrollok(body, TRUE);
2039 /* non-blocking input */
2040 wtimeout(body, 0);
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))
2052 new_tab(url);
2054 return 1;
2057 void
2058 ui_on_tab_loaded(struct tab *tab)
2060 stop_loading_anim(tab);
2061 message("Loaded %s", tab->hist_cur->h);
2063 redraw_tabline();
2064 wrefresh(tabline);
2065 if (in_minibuffer)
2066 wrefresh(minibuf);
2067 else
2068 wrefresh(body);
2071 void
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);
2077 redraw_tab(tab);
2078 } else
2079 tab->flags |= TAB_URGENT;
2082 void
2083 ui_require_input(struct tab *tab, int hide)
2085 /* TODO: hard-switching to another tab is ugly */
2086 switch_to_tab(tab);
2088 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2089 &ir_history);
2090 strlcpy(ministate.prompt, "Input required: ",
2091 sizeof(ministate.prompt));
2092 redraw_tab(tab);
2095 void
2096 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2097 unsigned int data)
2099 size_t len;
2101 if (in_minibuffer) {
2102 fn(0, data);
2103 return;
2106 yornp_cb = fn;
2107 yornp_data = data;
2108 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2109 yornp_abort, NULL);
2111 len = sizeof(ministate.prompt);
2112 strlcpy(ministate.prompt, prompt, len);
2113 strlcat(ministate.prompt, " (y or n) ", len);
2114 redraw_tab(current_tab());
2117 void
2118 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2119 unsigned int data)
2121 size_t len;
2123 if (in_minibuffer)
2124 return;
2126 read_cb = fn;
2127 read_data = data;
2128 enter_minibuffer(read_self_insert, read_select, read_abort,
2129 &read_history);
2131 len = sizeof(ministate.prompt);
2132 strlcpy(ministate.prompt, prompt, len);
2133 strlcat(ministate.prompt, ": ", len);
2134 redraw_tab(current_tab());
2137 void
2138 ui_notify(const char *fmt, ...)
2140 va_list ap;
2142 va_start(ap, fmt);
2143 vmessage(fmt, ap);
2144 va_end(ap);
2147 void
2148 ui_end(void)
2150 endwin();