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 struct { short meta; int key; uint32_t cp; } thiskey;
111 static struct event resizeev;
112 static struct timeval resize_timer = { 0, 250000 };
114 static WINDOW *tabline, *body, *modeline, *minibuf;
115 static int body_lines, body_cols;
117 static WINDOW *help;
118 static struct buffer helpwin;
119 static int help_lines, help_cols;
121 static int side_window;
123 static struct event clminibufev;
124 static struct timeval clminibufev_timer = { 5, 0 };
125 static struct timeval loadingev_timer = { 0, 250000 };
127 static uint32_t tab_counter;
129 static char keybuf[64];
131 static void (*yornp_cb)(int, unsigned int);
132 static unsigned int yornp_data;
134 static void (*read_cb)(const char*, unsigned int);
135 static unsigned int read_data;
137 struct kmap global_map,
138 minibuffer_map,
139 *current_map,
140 *base_map;
142 static struct histhead eecmd_history,
143 ir_history,
144 lu_history,
145 read_history;
147 static int in_minibuffer;
149 static struct {
150 char *curmesg;
152 char prompt[64];
153 void (*donefn)(void);
154 void (*abortfn)(void);
156 char buf[1025];
157 struct line line;
158 struct vline vline;
159 struct buffer buffer;
161 struct histhead *history;
162 struct hist *hist_cur;
163 size_t hist_off;
164 } ministate;
166 static inline void
167 global_set_key(const char *key, void (*fn)(struct buffer*))
169 if (!kmap_define_key(&global_map, key, fn))
170 _exit(1);
173 static inline void
174 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
176 if (!kmap_define_key(&minibuffer_map, key, fn))
177 _exit(1);
180 static void
181 load_default_keys(void)
183 /* === global map === */
185 /* emacs */
186 global_set_key("C-p", cmd_previous_line);
187 global_set_key("C-n", cmd_next_line);
188 global_set_key("C-f", cmd_forward_char);
189 global_set_key("C-b", cmd_backward_char);
190 global_set_key("M-{", cmd_backward_paragraph);
191 global_set_key("M-}", cmd_forward_paragraph);
192 global_set_key("C-a", cmd_move_beginning_of_line);
193 global_set_key("C-e", cmd_move_end_of_line);
195 global_set_key("M-v", cmd_scroll_up);
196 global_set_key("C-v", cmd_scroll_down);
197 global_set_key("M-space", cmd_scroll_up);
198 global_set_key("space", cmd_scroll_down);
200 global_set_key("M-<", cmd_beginning_of_buffer);
201 global_set_key("M->", cmd_end_of_buffer);
203 global_set_key("C-x C-c", cmd_kill_telescope);
205 global_set_key("C-g", cmd_clear_minibuf);
207 global_set_key("M-x", cmd_execute_extended_command);
208 global_set_key("C-x C-f", cmd_load_url);
209 global_set_key("C-x M-f", cmd_load_current_url);
211 global_set_key("C-x t 0", cmd_tab_close);
212 global_set_key("C-x t 1", cmd_tab_close_other);
213 global_set_key("C-x t 2", cmd_tab_new);
214 global_set_key("C-x t o", cmd_tab_next);
215 global_set_key("C-x t O", cmd_tab_previous);
216 global_set_key("C-x t m", cmd_tab_move);
217 global_set_key("C-x t M", cmd_tab_move_to);
219 global_set_key("C-M-b", cmd_previous_page);
220 global_set_key("C-M-f", cmd_next_page);
222 global_set_key("<f7> a", cmd_bookmark_page);
223 global_set_key("<f7> <f7>", cmd_list_bookmarks);
225 /* vi/vi-like */
226 global_set_key("k", cmd_previous_line);
227 global_set_key("j", cmd_next_line);
228 global_set_key("l", cmd_forward_char);
229 global_set_key("h", cmd_backward_char);
230 global_set_key("{", cmd_backward_paragraph);
231 global_set_key("}", cmd_forward_paragraph);
232 global_set_key("^", cmd_move_beginning_of_line);
233 global_set_key("$", cmd_move_end_of_line);
235 global_set_key("K", cmd_scroll_line_up);
236 global_set_key("J", cmd_scroll_line_down);
238 global_set_key("g g", cmd_beginning_of_buffer);
239 global_set_key("G", cmd_end_of_buffer);
241 global_set_key("g D", cmd_tab_close);
242 global_set_key("g N", cmd_tab_new);
243 global_set_key("g t", cmd_tab_next);
244 global_set_key("g T", cmd_tab_previous);
245 global_set_key("g M-t", cmd_tab_move);
246 global_set_key("g M-T", cmd_tab_move_to);
248 global_set_key("H", cmd_previous_page);
249 global_set_key("L", cmd_next_page);
251 /* tmp */
252 global_set_key("q", cmd_kill_telescope);
254 global_set_key("esc", cmd_clear_minibuf);
256 global_set_key(":", cmd_execute_extended_command);
258 /* cua */
259 global_set_key("<up>", cmd_previous_line);
260 global_set_key("<down>", cmd_next_line);
261 global_set_key("<right>", cmd_forward_char);
262 global_set_key("<left>", cmd_backward_char);
263 global_set_key("<prior>", cmd_scroll_up);
264 global_set_key("<next>", cmd_scroll_down);
266 global_set_key("M-<left>", cmd_previous_page);
267 global_set_key("M-<right>", cmd_next_page);
269 /* "ncurses standard" */
270 global_set_key("C-l", cmd_redraw);
272 /* global */
273 global_set_key("<f1>", cmd_toggle_help);
274 global_set_key("C-m", cmd_push_button);
275 global_set_key("M-enter", cmd_push_button_new_tab);
276 global_set_key("M-tab", cmd_previous_button);
277 global_set_key("backtab", cmd_previous_button);
278 global_set_key("tab", cmd_next_button);
280 /* === minibuffer map === */
281 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
282 minibuffer_set_key("C-g", cmd_mini_abort);
283 minibuffer_set_key("esc", cmd_mini_abort);
284 minibuffer_set_key("C-d", cmd_mini_delete_char);
285 minibuffer_set_key("del", cmd_mini_delete_backward_char);
286 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
287 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
289 minibuffer_set_key("C-b", cmd_backward_char);
290 minibuffer_set_key("C-f", cmd_forward_char);
291 minibuffer_set_key("<left>", cmd_backward_char);
292 minibuffer_set_key("<right>", cmd_forward_char);
293 minibuffer_set_key("C-e", cmd_move_end_of_line);
294 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
295 minibuffer_set_key("<end>", cmd_move_end_of_line);
296 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
297 minibuffer_set_key("C-k", cmd_mini_kill_line);
299 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
300 minibuffer_set_key("M-n", cmd_mini_next_history_element);
301 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
302 minibuffer_set_key("<down>", cmd_mini_next_history_element);
305 static void
306 restore_cursor(struct buffer *buffer)
308 struct vline *vl;
309 const char *prfx;
311 vl = buffer->current_line;
312 if (vl == NULL || vl->line == NULL)
313 buffer->curs_x = buffer->cpoff = 0;
314 else
315 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
317 if (vl != NULL) {
318 prfx = line_prefixes[vl->parent->type].prfx1;
319 buffer->curs_x += utf8_swidth(prfx);
323 void
324 cmd_previous_line(struct buffer *buffer)
326 struct vline *vl;
328 if (buffer->current_line == NULL
329 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
330 return;
332 if (--buffer->curs_y < 0) {
333 buffer->curs_y = 0;
334 cmd_scroll_line_up(buffer);
335 return;
338 buffer->current_line = vl;
339 restore_cursor(buffer);
342 void
343 cmd_next_line(struct buffer *buffer)
345 struct vline *vl;
347 if (buffer->current_line == NULL
348 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
349 return;
351 if (++buffer->curs_y > body_lines-1) {
352 buffer->curs_y = body_lines-1;
353 cmd_scroll_line_down(buffer);
354 return;
357 buffer->current_line = vl;
358 restore_cursor(buffer);
361 void
362 cmd_backward_char(struct buffer *buffer)
364 if (buffer->cpoff != 0)
365 buffer->cpoff--;
366 restore_cursor(buffer);
369 void
370 cmd_forward_char(struct buffer *buffer)
372 size_t len = 0;
374 if (buffer->current_line->line != NULL)
375 len = utf8_cplen(buffer->current_line->line);
376 if (++buffer->cpoff > len)
377 buffer->cpoff = len;
378 restore_cursor(buffer);
381 void
382 cmd_backward_paragraph(struct buffer *buffer)
384 do {
385 if (buffer->current_line == NULL ||
386 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
387 message("No previous paragraph");
388 return;
390 cmd_previous_line(buffer);
391 } while (buffer->current_line->line != NULL ||
392 buffer->current_line->parent->type != LINE_TEXT);
395 void
396 cmd_forward_paragraph(struct buffer *buffer)
398 do {
399 if (buffer->current_line == NULL ||
400 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
401 message("No next paragraph");
402 return;
404 cmd_next_line(buffer);
405 } while (buffer->current_line->line != NULL ||
406 buffer->current_line->parent->type != LINE_TEXT);
409 void
410 cmd_move_beginning_of_line(struct buffer *buffer)
412 buffer->cpoff = 0;
413 restore_cursor(buffer);
416 void
417 cmd_move_end_of_line(struct buffer *buffer)
419 struct vline *vl;
421 vl = buffer->current_line;
422 if (vl->line == NULL)
423 return;
424 buffer->cpoff = utf8_cplen(vl->line);
425 restore_cursor(buffer);
428 void
429 cmd_redraw(struct buffer *buffer)
431 handle_resize(0, 0, NULL);
434 void
435 cmd_scroll_line_up(struct buffer *buffer)
437 struct vline *vl;
439 if (buffer->line_off == 0)
440 return;
442 vl = nth_line(buffer, --buffer->line_off);
443 wscrl(body, -1);
444 wmove(body, 0, 0);
445 print_vline(body, vl);
447 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
448 restore_cursor(buffer);
451 void
452 cmd_scroll_line_down(struct buffer *buffer)
454 struct vline *vl;
456 vl = buffer->current_line;
457 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
458 return;
459 buffer->current_line = vl;
461 buffer->line_off++;
462 wscrl(body, 1);
464 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
465 return;
467 vl = nth_line(buffer, buffer->line_off + body_lines-1);
468 wmove(body, body_lines-1, 0);
469 print_vline(body, vl);
471 restore_cursor(buffer);
474 void
475 cmd_scroll_up(struct buffer *buffer)
477 size_t off;
479 off = body_lines-1;
481 for (; off > 0; --off)
482 cmd_scroll_line_up(buffer);
485 void
486 cmd_scroll_down(struct buffer *buffer)
488 size_t off;
490 off = body_lines-1;
492 for (; off > 0; --off)
493 cmd_scroll_line_down(buffer);
496 void
497 cmd_beginning_of_buffer(struct buffer *buffer)
499 buffer->current_line = TAILQ_FIRST(&buffer->head);
500 buffer->line_off = 0;
501 buffer->curs_y = 0;
502 buffer->cpoff = 0;
503 restore_cursor(buffer);
506 void
507 cmd_end_of_buffer(struct buffer *buffer)
509 ssize_t off;
511 off = buffer->line_max - body_lines;
512 off = MAX(0, off);
514 buffer->line_off = off;
515 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
517 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
518 buffer->cpoff = body_cols;
519 restore_cursor(buffer);
522 void
523 cmd_kill_telescope(struct buffer *buffer)
525 save_session();
526 event_loopbreak();
529 void
530 cmd_push_button(struct buffer *buffer)
532 struct vline *vl;
533 size_t nth;
535 nth = buffer->line_off + buffer->curs_y;
536 if (nth >= buffer->line_max)
537 return;
538 vl = nth_line(buffer, nth);
539 if (vl->parent->type != LINE_LINK)
540 return;
542 load_url_in_tab(current_tab(), vl->parent->alt);
545 void
546 cmd_push_button_new_tab(struct buffer *buffer)
548 struct vline *vl;
549 size_t nth;
551 nth = buffer->line_off + buffer->curs_y;
552 if (nth > buffer->line_max)
553 return;
554 vl = nth_line(buffer, nth);
555 if (vl->parent->type != LINE_LINK)
556 return;
558 new_tab(vl->parent->alt);
561 void
562 cmd_previous_button(struct buffer *buffer)
564 do {
565 if (buffer->current_line == NULL ||
566 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
567 message("No previous link");
568 return;
570 cmd_previous_line(buffer);
571 } while (buffer->current_line->parent->type != LINE_LINK);
574 void
575 cmd_next_button(struct buffer *buffer)
577 do {
578 if (buffer->current_line == NULL ||
579 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
580 message("No next link");
581 return;
583 cmd_next_line(buffer);
584 } while (buffer->current_line->parent->type != LINE_LINK);
587 void
588 cmd_previous_page(struct buffer *buffer)
590 struct tab *tab = current_tab();
592 if (!load_previous_page(tab))
593 message("No previous page");
594 else
595 start_loading_anim(tab);
598 void
599 cmd_next_page(struct buffer *buffer)
601 struct tab *tab = current_tab();
603 if (!load_next_page(tab))
604 message("No next page");
605 else
606 start_loading_anim(tab);
609 void
610 cmd_clear_minibuf(struct buffer *buffer)
612 handle_clear_minibuf(0, 0, NULL);
615 void
616 cmd_execute_extended_command(struct buffer *buffer)
618 size_t len;
620 if (in_minibuffer) {
621 message("We don't have enable-recursive-minibuffers");
622 return;
625 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
626 &eecmd_history);
628 len = sizeof(ministate.prompt);
629 strlcpy(ministate.prompt, "", len);
631 if (thiskey.meta)
632 strlcat(ministate.prompt, "M-", len);
634 strlcat(ministate.prompt, keyname(thiskey.key), len);
636 if (thiskey.meta)
637 strlcat(ministate.prompt, " ", len);
640 void
641 cmd_tab_close(struct buffer *buffer)
643 struct tab *tab, *t;
645 tab = current_tab();
646 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
647 TAILQ_NEXT(tab, tabs) == NULL) {
648 message("Can't close the only tab.");
649 return;
652 if (evtimer_pending(&tab->loadingev, NULL))
653 evtimer_del(&tab->loadingev);
655 stop_tab(tab);
657 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
658 t = TAILQ_NEXT(tab, tabs);
659 TAILQ_REMOVE(&tabshead, tab, tabs);
660 free(tab);
662 switch_to_tab(t);
665 void
666 cmd_tab_close_other(struct buffer *buffer)
668 struct tab *t, *i;
670 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
671 if (t->flags & TAB_CURRENT)
672 continue;
674 stop_tab(t);
675 TAILQ_REMOVE(&tabshead, t, tabs);
676 free(t);
680 void
681 cmd_tab_new(struct buffer *buffer)
683 new_tab(NEW_TAB_URL);
686 void
687 cmd_tab_next(struct buffer *buffer)
689 struct tab *tab, *t;
691 tab = current_tab();
692 tab->flags &= ~TAB_CURRENT;
694 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
695 t = TAILQ_FIRST(&tabshead);
696 t->flags |= TAB_CURRENT;
697 t->flags &= ~TAB_URGENT;
700 void
701 cmd_tab_previous(struct buffer *buffer)
703 struct tab *tab, *t;
705 tab = current_tab();
706 tab->flags &= ~TAB_CURRENT;
708 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
709 t = TAILQ_LAST(&tabshead, tabshead);
710 t->flags |= TAB_CURRENT;
711 t->flags &= ~TAB_URGENT;
714 void
715 cmd_tab_move(struct buffer *buffer)
717 struct tab *tab, *t;
719 tab = current_tab();
720 t = TAILQ_NEXT(tab, tabs);
721 TAILQ_REMOVE(&tabshead, tab, tabs);
723 if (t == NULL)
724 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
725 else
726 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
729 void
730 cmd_tab_move_to(struct buffer *buffer)
732 struct tab *tab, *t;
734 tab = current_tab();
735 t = TAILQ_PREV(tab, tabshead, tabs);
736 TAILQ_REMOVE(&tabshead, tab, tabs);
738 if (t == NULL) {
739 if (TAILQ_EMPTY(&tabshead))
740 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
741 else
742 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
743 } else
744 TAILQ_INSERT_BEFORE(t, tab, tabs);
747 void
748 cmd_load_url(struct buffer *buffer)
750 if (in_minibuffer) {
751 message("We don't have enable-recursive-minibuffers");
752 return;
755 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
756 &lu_history);
757 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
758 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
759 cmd_move_end_of_line(&ministate.buffer);
762 void
763 cmd_load_current_url(struct buffer *buffer)
765 struct tab *tab = current_tab();
767 if (in_minibuffer) {
768 message("We don't have enable-recursive-minibuffers");
769 return;
772 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
773 &lu_history);
774 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
775 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
776 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
779 void
780 cmd_bookmark_page(struct buffer *buffer)
782 struct tab *tab = current_tab();
784 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
785 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
786 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
787 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
790 void
791 cmd_list_bookmarks(struct buffer *buffer)
793 load_url_in_tab(current_tab(), "about:bookmarks");
796 void
797 cmd_toggle_help(struct buffer *buffer)
799 side_window = !side_window;
800 if (side_window)
801 recompute_help();
803 /*
804 * ugly hack, but otherwise the window doesn't get updated
805 * until I call handle_resize a second time (i.e. C-l). I
806 * will be happy to know why something like this is needed.
807 */
808 handle_resize_nodelay(0, 0, NULL);
809 handle_resize_nodelay(0, 0, NULL);
812 void
813 cmd_mini_delete_char(struct buffer *buffer)
815 char *c, *n;
817 if (!in_minibuffer) {
818 message("text is read-only");
819 return;
822 minibuffer_taint_hist();
824 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
825 if (*c == '\0')
826 return;
827 n = utf8_next_cp(c);
829 memmove(c, n, strlen(n)+1);
832 void
833 cmd_mini_delete_backward_char(struct buffer *buffer)
835 char *c, *p, *start;
837 if (!in_minibuffer) {
838 message("text is read-only");
839 return;
842 minibuffer_taint_hist();
844 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
845 start = buffer->current_line->line;
846 if (c == start)
847 return;
848 p = utf8_prev_cp(c-1, start);
850 memmove(p, c, strlen(c)+1);
851 buffer->cpoff--;
854 void
855 cmd_mini_kill_line(struct buffer *buffer)
857 char *c;
859 if (!in_minibuffer) {
860 message("text is read-only");
861 return;
864 minibuffer_taint_hist();
865 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
866 *c = '\0';
869 void
870 cmd_mini_abort(struct buffer *buffer)
872 if (!in_minibuffer)
873 return;
875 ministate.abortfn();
878 void
879 cmd_mini_complete_and_exit(struct buffer *buffer)
881 if (!in_minibuffer)
882 return;
884 minibuffer_taint_hist();
885 ministate.donefn();
888 void
889 cmd_mini_previous_history_element(struct buffer *buffer)
891 if (ministate.history == NULL) {
892 message("No history");
893 return;
896 if (ministate.hist_cur == NULL ||
897 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
898 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
899 ministate.hist_off = ministate.history->len - 1;
900 if (ministate.hist_cur == NULL)
901 message("No prev item");
902 } else {
903 ministate.hist_off--;
906 if (ministate.hist_cur != NULL)
907 buffer->current_line->line = ministate.hist_cur->h;
910 void
911 cmd_mini_next_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_NEXT(ministate.hist_cur, entries)) == NULL) {
920 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
921 ministate.hist_off = 0;
922 if (ministate.hist_cur == NULL)
923 message("No next item");
924 } else {
925 ministate.hist_off++;
928 if (ministate.hist_cur != NULL)
929 buffer->current_line->line = ministate.hist_cur->h;
932 static void
933 global_key_unbound(void)
935 message("%s is undefined", keybuf);
938 static void
939 minibuffer_hist_save_entry(void)
941 struct hist *hist;
943 if (ministate.history == NULL)
944 return;
946 if ((hist = calloc(1, sizeof(*hist))) == NULL)
947 abort();
949 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
951 if (TAILQ_EMPTY(&ministate.history->head))
952 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
953 else
954 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
955 ministate.history->len++;
958 /*
959 * taint the minibuffer cache: if we're currently showing a history
960 * element, copy that to the current buf and reset the "history
961 * navigation" thing.
962 */
963 static void
964 minibuffer_taint_hist(void)
966 if (ministate.hist_cur == NULL)
967 return;
969 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
970 ministate.hist_cur = NULL;
973 static void
974 minibuffer_self_insert(void)
976 char *c, tmp[5] = {0};
977 size_t len;
979 minibuffer_taint_hist();
981 if (thiskey.cp == 0)
982 return;
984 len = utf8_encode(thiskey.cp, tmp);
985 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
986 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
987 return;
989 memmove(c + len, c, strlen(c)+1);
990 memcpy(c, tmp, len);
991 ministate.buffer.cpoff++;
994 static void
995 eecmd_self_insert(void)
997 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
998 !unicode_isgraph(thiskey.cp)) {
999 global_key_unbound();
1000 return;
1003 minibuffer_self_insert();
1006 static void
1007 eecmd_select(void)
1009 struct cmds *cmd;
1011 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1012 if (!strcmp(cmd->cmd, ministate.buf)) {
1013 exit_minibuffer();
1014 minibuffer_hist_save_entry();
1015 cmd->fn(current_buffer());
1016 return;
1020 message("No match");
1023 static void
1024 ir_self_insert(void)
1026 minibuffer_self_insert();
1029 static void
1030 ir_select(void)
1032 char buf[1025] = {0};
1033 struct phos_uri uri;
1034 struct tab *tab;
1036 tab = current_tab();
1038 exit_minibuffer();
1039 minibuffer_hist_save_entry();
1041 /* a bit ugly but... */
1042 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1043 phos_uri_set_query(&uri, ministate.buf);
1044 phos_serialize_uri(&uri, buf, sizeof(buf));
1045 load_url_in_tab(tab, buf);
1048 static void
1049 lu_self_insert(void)
1051 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1052 !unicode_isgraph(thiskey.key)) {
1053 global_key_unbound();
1054 return;
1057 minibuffer_self_insert();
1060 static void
1061 lu_select(void)
1063 exit_minibuffer();
1064 minibuffer_hist_save_entry();
1065 load_url_in_tab(current_tab(), ministate.buf);
1068 static void
1069 bp_select(void)
1071 exit_minibuffer();
1072 if (*ministate.buf != '\0')
1073 add_to_bookmarks(ministate.buf);
1074 else
1075 message("Abort.");
1078 static void
1079 yornp_self_insert(void)
1081 if (thiskey.key != 'y' && thiskey.key != 'n') {
1082 message("Please answer y or n");
1083 return;
1086 exit_minibuffer();
1087 yornp_cb(thiskey.key == 'y', yornp_data);
1090 static void
1091 yornp_abort(void)
1093 exit_minibuffer();
1094 yornp_cb(0, yornp_data);
1097 static void
1098 read_self_insert(void)
1100 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1101 global_key_unbound();
1102 return;
1105 minibuffer_self_insert();
1108 static void
1109 read_abort(void)
1111 exit_minibuffer();
1112 read_cb(NULL, read_data);
1115 static void
1116 read_select(void)
1118 exit_minibuffer();
1119 minibuffer_hist_save_entry();
1120 read_cb(ministate.buf, read_data);
1123 static struct vline *
1124 nth_line(struct buffer *buffer, size_t n)
1126 struct vline *vl;
1127 size_t i;
1129 i = 0;
1130 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1131 if (i == n)
1132 return vl;
1133 i++;
1136 /* unreachable */
1137 abort();
1140 static struct tab *
1141 current_tab(void)
1143 struct tab *t;
1145 TAILQ_FOREACH(t, &tabshead, tabs) {
1146 if (t->flags & TAB_CURRENT)
1147 return t;
1150 /* unreachable */
1151 abort();
1154 static struct buffer *
1155 current_buffer(void)
1157 if (in_minibuffer)
1158 return &ministate.buffer;
1159 return &current_tab()->buffer;
1162 static int
1163 readkey(void)
1165 uint32_t state = 0;
1167 if ((thiskey.key = wgetch(body)) == ERR)
1168 return 0;
1170 thiskey.meta = thiskey.key == 27;
1171 if (thiskey.meta) {
1172 thiskey.key = wgetch(body);
1173 if (thiskey.key == ERR || thiskey.key == 27) {
1174 thiskey.meta = 0;
1175 thiskey.key = 27;
1179 thiskey.cp = 0;
1180 if ((unsigned int)thiskey.key < UINT8_MAX) {
1181 while (1) {
1182 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1183 break;
1184 if ((thiskey.key = wgetch(body)) == ERR) {
1185 message("Error decoding user input");
1186 return 0;
1191 return 1;
1194 static void
1195 dispatch_stdio(int fd, short ev, void *d)
1197 struct keymap *k;
1198 const char *keyname;
1199 char tmp[5] = {0};
1201 if (!readkey())
1202 return;
1204 if (keybuf[0] != '\0')
1205 strlcat(keybuf, " ", sizeof(keybuf));
1206 if (thiskey.meta)
1207 strlcat(keybuf, "M-", sizeof(keybuf));
1208 if (thiskey.cp != 0) {
1209 utf8_encode(thiskey.cp, tmp);
1210 strlcat(keybuf, tmp, sizeof(keybuf));
1211 } else {
1212 if ((keyname = unkbd(thiskey.key)) != NULL)
1213 strlcat(keybuf, keyname, sizeof(keybuf));
1214 else {
1215 tmp[0] = thiskey.key;
1216 strlcat(keybuf, tmp, sizeof(keybuf));
1220 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1221 if (k->meta == thiskey.meta &&
1222 k->key == thiskey.key) {
1223 if (k->fn == NULL)
1224 current_map = &k->map;
1225 else {
1226 current_map = base_map;
1227 strlcpy(keybuf, "", sizeof(keybuf));
1228 k->fn(current_buffer());
1230 goto done;
1234 if (current_map->unhandled_input != NULL)
1235 current_map->unhandled_input();
1236 else {
1237 global_key_unbound();
1240 strlcpy(keybuf, "", sizeof(keybuf));
1241 current_map = base_map;
1243 done:
1244 if (side_window)
1245 recompute_help();
1247 redraw_tab(current_tab());
1250 static void
1251 handle_clear_minibuf(int fd, short ev, void *d)
1253 free(ministate.curmesg);
1254 ministate.curmesg = NULL;
1256 redraw_minibuffer();
1257 if (in_minibuffer) {
1258 wrefresh(body);
1259 wrefresh(minibuf);
1260 } else {
1261 wrefresh(minibuf);
1262 wrefresh(body);
1266 static void
1267 handle_resize(int sig, short ev, void *d)
1269 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1270 event_del(&resizeev);
1272 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1273 evtimer_add(&resizeev, &resize_timer);
1276 static void
1277 handle_resize_nodelay(int s, short ev, void *d)
1279 struct tab *tab;
1281 endwin();
1282 refresh();
1283 clear();
1285 /* move and resize the windows, in reverse order! */
1287 mvwin(minibuf, LINES-1, 0);
1288 wresize(minibuf, 1, COLS);
1290 mvwin(modeline, LINES-2, 0);
1291 wresize(modeline, 1, COLS);
1293 body_lines = LINES-3;
1294 body_cols = COLS;
1296 if (side_window) {
1297 help_cols = 0.3 * COLS;
1298 help_lines = LINES-3;
1299 mvwin(help, 1, 0);
1300 wresize(help, help_lines, help_cols);
1302 wrap_page(&helpwin, help_cols);
1304 body_cols = COLS - help_cols - 1;
1305 mvwin(body, 1, help_cols);
1306 } else
1307 mvwin(body, 1, 0);
1309 wresize(body, body_lines, body_cols);
1311 wresize(tabline, 1, COLS);
1313 tab = current_tab();
1315 wrap_page(&tab->buffer, body_cols);
1316 redraw_tab(tab);
1319 static int
1320 wrap_page(struct buffer *buffer, int width)
1322 struct line *l;
1323 const struct line *orig;
1324 struct vline *vl;
1325 const char *prfx;
1327 orig = buffer->current_line == NULL
1328 ? NULL
1329 : buffer->current_line->parent;
1330 buffer->current_line = NULL;
1332 buffer->curs_y = 0;
1333 buffer->line_off = 0;
1335 empty_vlist(buffer);
1337 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1338 prfx = line_prefixes[l->type].prfx1;
1339 switch (l->type) {
1340 case LINE_TEXT:
1341 case LINE_LINK:
1342 case LINE_TITLE_1:
1343 case LINE_TITLE_2:
1344 case LINE_TITLE_3:
1345 case LINE_ITEM:
1346 case LINE_QUOTE:
1347 case LINE_PRE_START:
1348 case LINE_PRE_END:
1349 wrap_text(buffer, prfx, l, width);
1350 break;
1351 case LINE_PRE_CONTENT:
1352 hardwrap_text(buffer, l, width);
1353 break;
1356 if (orig == l && buffer->current_line == NULL) {
1357 buffer->line_off = buffer->line_max-1;
1358 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1360 while (1) {
1361 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1362 if (vl == NULL || vl->parent != orig)
1363 break;
1364 buffer->current_line = vl;
1365 buffer->line_off--;
1370 if (buffer->current_line == NULL)
1371 buffer->current_line = TAILQ_FIRST(&buffer->head);
1373 return 1;
1376 static void
1377 print_vline(WINDOW *window, struct vline *vl)
1379 const char *text = vl->line;
1380 const char *prfx;
1381 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1382 int text_face = line_faces[vl->parent->type].text_prop;
1384 if (!vl->flags)
1385 prfx = line_prefixes[vl->parent->type].prfx1;
1386 else
1387 prfx = line_prefixes[vl->parent->type].prfx2;
1389 if (text == NULL)
1390 text = "";
1392 wattron(window, prefix_face);
1393 wprintw(window, "%s", prfx);
1394 wattroff(window, prefix_face);
1396 wattron(window, text_face);
1397 wprintw(window, "%s", text);
1398 wattroff(window, text_face);
1401 static void
1402 redraw_tabline(void)
1404 struct tab *tab;
1405 size_t toskip, ots, tabwidth, space, x;
1406 int current, y, truncated;
1407 const char *title;
1408 char buf[25];
1410 tabwidth = sizeof(buf)+1;
1411 space = COLS-2;
1413 toskip = 0;
1414 TAILQ_FOREACH(tab, &tabshead, tabs) {
1415 toskip++;
1416 if (tab->flags & TAB_CURRENT)
1417 break;
1420 if (toskip * tabwidth < space)
1421 toskip = 0;
1422 else {
1423 ots = toskip;
1424 toskip--;
1425 while (toskip != 0 &&
1426 (ots - toskip+1) * tabwidth < space)
1427 toskip--;
1430 werase(tabline);
1431 wattron(tabline, tab_face.background);
1432 wprintw(tabline, toskip == 0 ? " " : "<");
1433 wattroff(tabline, tab_face.background);
1435 truncated = 0;
1436 TAILQ_FOREACH(tab, &tabshead, tabs) {
1437 if (truncated)
1438 break;
1439 if (toskip != 0) {
1440 toskip--;
1441 continue;
1444 getyx(tabline, y, x);
1445 if (x + sizeof(buf)+2 >= (size_t)COLS)
1446 truncated = 1;
1448 current = tab->flags & TAB_CURRENT;
1450 if (*(title = tab->buffer.page.title) == '\0')
1451 title = tab->hist_cur->h;
1453 if (tab->flags & TAB_URGENT)
1454 strlcpy(buf, "!", sizeof(buf));
1455 else
1456 strlcpy(buf, " ", sizeof(buf));
1458 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1459 /* truncation happens */
1460 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1461 } else {
1462 /* pad with spaces */
1463 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1464 /* nop */ ;
1467 if (current)
1468 wattron(tabline, tab_face.current_tab);
1469 else
1470 wattron(tabline, tab_face.tab);
1472 wprintw(tabline, "%s", buf);
1473 if (TAILQ_NEXT(tab, tabs) != NULL)
1474 wprintw(tabline, " ");
1476 if (current)
1477 wattroff(tabline, tab_face.current_tab);
1478 else
1479 wattroff(tabline, tab_face.tab);
1482 wattron(tabline, tab_face.background);
1483 for (; x < (size_t)COLS; ++x)
1484 waddch(tabline, ' ');
1485 if (truncated)
1486 mvwprintw(tabline, 0, COLS-1, ">");
1489 static void
1490 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1492 struct vline *vl;
1493 int l;
1495 werase(win);
1497 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1498 if (TAILQ_EMPTY(&buffer->head))
1499 return;
1501 l = 0;
1502 vl = nth_line(buffer, buffer->line_off);
1503 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1504 wmove(win, l, 0);
1505 print_vline(win, vl);
1506 l++;
1507 if (l == height)
1508 break;
1511 wmove(win, buffer->curs_y, buffer->curs_x);
1514 static void
1515 redraw_help(void)
1517 redraw_window(help, help_lines, &helpwin);
1520 static void
1521 redraw_body(struct tab *tab)
1523 redraw_window(body, body_lines, &tab->buffer);
1526 static inline char
1527 trust_status_char(enum trust_state ts)
1529 switch (ts) {
1530 case TS_UNKNOWN: return 'u';
1531 case TS_UNTRUSTED: return '!';
1532 case TS_TRUSTED: return 'v';
1533 case TS_VERIFIED: return 'V';
1537 static void
1538 redraw_modeline(struct tab *tab)
1540 double pct;
1541 int x, y, max_x, max_y;
1542 const char *mode = tab->buffer.page.name;
1543 const char *spin = "-\\|/";
1545 werase(modeline);
1546 wattron(modeline, A_REVERSE);
1547 wmove(modeline, 0, 0);
1549 wprintw(modeline, "-%c%c %s ",
1550 spin[tab->loading_anim_step],
1551 trust_status_char(tab->trust),
1552 mode == NULL ? "(none)" : mode);
1554 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1556 if (tab->buffer.line_max <= (size_t)body_lines)
1557 wprintw(modeline, "All ");
1558 else if (tab->buffer.line_off == 0)
1559 wprintw(modeline, "Top ");
1560 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1561 wprintw(modeline, "Bottom ");
1562 else
1563 wprintw(modeline, "%.0f%% ", pct);
1565 wprintw(modeline, "%d/%d %s ",
1566 tab->buffer.line_off + tab->buffer.curs_y,
1567 tab->buffer.line_max,
1568 tab->hist_cur->h);
1570 getyx(modeline, y, x);
1571 getmaxyx(modeline, max_y, max_x);
1573 (void)y;
1574 (void)max_y;
1576 for (; x < max_x; ++x)
1577 waddstr(modeline, "-");
1580 static void
1581 redraw_minibuffer(void)
1583 struct tab *tab;
1584 size_t off_y, off_x = 0;
1585 char *start, *c;
1587 werase(minibuf);
1589 if (in_minibuffer) {
1590 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1591 if (ministate.hist_cur != NULL)
1592 wprintw(minibuf, "(%zu/%zu) ",
1593 ministate.hist_off + 1,
1594 ministate.history->len);
1596 getyx(minibuf, off_y, off_x);
1598 start = ministate.hist_cur != NULL
1599 ? ministate.hist_cur->h
1600 : ministate.buf;
1601 c = utf8_nth(ministate.buffer.current_line->line,
1602 ministate.buffer.cpoff);
1603 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1604 start = utf8_next_cp(start);
1607 waddstr(minibuf, start);
1610 if (ministate.curmesg != NULL)
1611 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1612 ministate.curmesg);
1614 if (!in_minibuffer && ministate.curmesg == NULL)
1615 waddstr(minibuf, keybuf);
1617 /* If nothing else, show the URL at point */
1618 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1619 tab = current_tab();
1620 if (tab->buffer.current_line != NULL &&
1621 tab->buffer.current_line->parent->type == LINE_LINK)
1622 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1625 if (in_minibuffer)
1626 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1629 static void
1630 redraw_tab(struct tab *tab)
1632 if (side_window) {
1633 redraw_help();
1634 wnoutrefresh(help);
1637 redraw_tabline();
1638 redraw_body(tab);
1639 redraw_modeline(tab);
1640 redraw_minibuffer();
1642 wnoutrefresh(tabline);
1643 wnoutrefresh(modeline);
1645 if (in_minibuffer) {
1646 wnoutrefresh(body);
1647 wnoutrefresh(minibuf);
1648 } else {
1649 wnoutrefresh(minibuf);
1650 wnoutrefresh(body);
1653 doupdate();
1656 static void
1657 emit_help_item(char *prfx, void *fn)
1659 struct line *l;
1660 struct cmds *cmd;
1662 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1663 if (fn == cmd->fn)
1664 break;
1666 assert(cmd != NULL);
1668 if ((l = calloc(1, sizeof(*l))) == NULL)
1669 abort();
1671 l->type = LINE_TEXT;
1672 l->alt = NULL;
1674 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1676 if (TAILQ_EMPTY(&helpwin.page.head))
1677 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1678 else
1679 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1682 static void
1683 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1685 struct keymap *k;
1686 char p[32];
1687 const char *kn;
1689 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1690 strlcpy(p, prfx, sizeof(p));
1691 if (*p != '\0')
1692 strlcat(p, " ", sizeof(p));
1693 if (k->meta)
1694 strlcat(p, "M-", sizeof(p));
1695 if ((kn = unkbd(k->key)) != NULL)
1696 strlcat(p, kn, sizeof(p));
1697 else
1698 strlcat(p, keyname(k->key), sizeof(p));
1700 if (k->fn == NULL)
1701 rec_compute_help(&k->map, p, sizeof(p));
1702 else
1703 emit_help_item(p, k->fn);
1707 static void
1708 recompute_help(void)
1710 char p[32] = { 0 };
1712 empty_vlist(&helpwin);
1713 empty_linelist(&helpwin);
1714 rec_compute_help(current_map, p, sizeof(p));
1715 wrap_page(&helpwin, help_cols);
1718 static void
1719 vmessage(const char *fmt, va_list ap)
1721 if (evtimer_pending(&clminibufev, NULL))
1722 evtimer_del(&clminibufev);
1723 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1724 evtimer_add(&clminibufev, &clminibufev_timer);
1726 free(ministate.curmesg);
1728 /* TODO: what to do if the allocation fails here? */
1729 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1730 ministate.curmesg = NULL;
1732 redraw_minibuffer();
1733 if (in_minibuffer) {
1734 wrefresh(body);
1735 wrefresh(minibuf);
1736 } else {
1737 wrefresh(minibuf);
1738 wrefresh(body);
1742 static void
1743 message(const char *fmt, ...)
1745 va_list ap;
1747 va_start(ap, fmt);
1748 vmessage(fmt, ap);
1749 va_end(ap);
1752 static void
1753 start_loading_anim(struct tab *tab)
1755 if (tab->loading_anim)
1756 return;
1757 tab->loading_anim = 1;
1758 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1759 evtimer_add(&tab->loadingev, &loadingev_timer);
1762 static void
1763 update_loading_anim(int fd, short ev, void *d)
1765 struct tab *tab = d;
1767 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1769 if (tab->flags & TAB_CURRENT) {
1770 redraw_modeline(tab);
1771 wrefresh(modeline);
1772 wrefresh(body);
1773 if (in_minibuffer)
1774 wrefresh(minibuf);
1777 evtimer_add(&tab->loadingev, &loadingev_timer);
1780 static void
1781 stop_loading_anim(struct tab *tab)
1783 if (!tab->loading_anim)
1784 return;
1785 evtimer_del(&tab->loadingev);
1786 tab->loading_anim = 0;
1787 tab->loading_anim_step = 0;
1789 if (!(tab->flags & TAB_CURRENT))
1790 return;
1792 redraw_modeline(tab);
1794 wrefresh(modeline);
1795 wrefresh(body);
1796 if (in_minibuffer)
1797 wrefresh(minibuf);
1800 static void
1801 load_url_in_tab(struct tab *tab, const char *url)
1803 message("Loading %s...", url);
1804 start_loading_anim(tab);
1805 load_url(tab, url);
1807 tab->buffer.curs_x = 0;
1808 tab->buffer.curs_y = 0;
1809 redraw_tab(tab);
1812 static void
1813 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1814 void (*abortfn)(void), struct histhead *hist)
1816 in_minibuffer = 1;
1817 base_map = &minibuffer_map;
1818 current_map = &minibuffer_map;
1820 base_map->unhandled_input = self_insert_fn;
1822 ministate.donefn = donefn;
1823 ministate.abortfn = abortfn;
1824 memset(ministate.buf, 0, sizeof(ministate.buf));
1825 ministate.buffer.current_line = &ministate.vline;
1826 ministate.buffer.current_line->line = ministate.buf;
1827 ministate.buffer.cpoff = 0;
1828 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1830 ministate.history = hist;
1831 ministate.hist_cur = NULL;
1832 ministate.hist_off = 0;
1835 static void
1836 exit_minibuffer(void)
1838 werase(minibuf);
1840 in_minibuffer = 0;
1841 base_map = &global_map;
1842 current_map = &global_map;
1845 static void
1846 switch_to_tab(struct tab *tab)
1848 struct tab *t;
1850 TAILQ_FOREACH(t, &tabshead, tabs) {
1851 t->flags &= ~TAB_CURRENT;
1854 tab->flags |= TAB_CURRENT;
1855 tab->flags &= ~TAB_URGENT;
1858 unsigned int
1859 tab_new_id(void)
1861 return tab_counter++;
1864 static struct tab *
1865 new_tab(const char *url)
1867 struct tab *tab;
1869 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1870 event_loopbreak();
1871 return NULL;
1873 tab->fd = -1;
1875 TAILQ_INIT(&tab->hist.head);
1877 TAILQ_INIT(&tab->buffer.head);
1879 tab->id = tab_new_id();
1880 switch_to_tab(tab);
1882 if (TAILQ_EMPTY(&tabshead))
1883 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1884 else
1885 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1887 load_url_in_tab(tab, url);
1888 return tab;
1891 static void
1892 session_new_tab_cb(const char *url)
1894 new_tab(url);
1897 static void
1898 usage(void)
1900 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1901 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1904 int
1905 ui_init(int argc, char * const *argv)
1907 char path[PATH_MAX];
1908 const char *url = NEW_TAB_URL;
1909 int ch, configtest = 0, fonf = 0;
1911 strlcpy(path, getenv("HOME"), sizeof(path));
1912 strlcat(path, "/.telescope/config", sizeof(path));
1914 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1915 switch (ch) {
1916 case 'c':
1917 fonf = 1;
1918 strlcpy(path, optarg, sizeof(path));
1919 break;
1920 case 'n':
1921 configtest = 1;
1922 break;
1923 case 'h':
1924 usage();
1925 return 0;
1926 default:
1927 usage();
1928 return 1;
1931 argc -= optind;
1932 argv += optind;
1934 parseconfig(path, fonf);
1935 if (configtest){
1936 puts("config OK");
1937 exit(0);
1940 if (argc != 0)
1941 url = argv[0];
1943 setlocale(LC_ALL, "");
1945 TAILQ_INIT(&global_map.m);
1946 global_map.unhandled_input = global_key_unbound;
1948 TAILQ_INIT(&minibuffer_map.m);
1950 TAILQ_INIT(&eecmd_history.head);
1951 TAILQ_INIT(&ir_history.head);
1952 TAILQ_INIT(&lu_history.head);
1954 ministate.line.type = LINE_TEXT;
1955 ministate.vline.parent = &ministate.line;
1956 ministate.buffer.current_line = &ministate.vline;
1958 /* initialize help window */
1959 TAILQ_INIT(&helpwin.head);
1961 base_map = &global_map;
1962 current_map = &global_map;
1963 load_default_keys();
1965 initscr();
1966 raw();
1967 noecho();
1969 nonl();
1970 intrflush(stdscr, FALSE);
1972 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1973 return 0;
1974 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1975 return 0;
1976 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1977 return 0;
1978 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1979 return 0;
1980 if ((help = newwin(1, 1, 1, 0)) == NULL)
1981 return 0;
1983 body_lines = LINES-3;
1984 body_cols = COLS;
1986 keypad(body, TRUE);
1987 scrollok(body, TRUE);
1989 /* non-blocking input */
1990 wtimeout(body, 0);
1992 mvwprintw(body, 0, 0, "");
1994 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1995 event_add(&stdioev, NULL);
1997 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1998 signal_add(&winchev, NULL);
2000 load_last_session(session_new_tab_cb);
2001 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2002 new_tab(url);
2004 return 1;
2007 void
2008 ui_on_tab_loaded(struct tab *tab)
2010 stop_loading_anim(tab);
2011 message("Loaded %s", tab->hist_cur->h);
2013 redraw_tabline();
2014 wrefresh(tabline);
2015 if (in_minibuffer)
2016 wrefresh(minibuf);
2017 else
2018 wrefresh(body);
2021 void
2022 ui_on_tab_refresh(struct tab *tab)
2024 wrap_page(&tab->buffer, body_cols);
2025 if (tab->flags & TAB_CURRENT) {
2026 restore_cursor(&tab->buffer);
2027 redraw_tab(tab);
2028 } else
2029 tab->flags |= TAB_URGENT;
2032 void
2033 ui_require_input(struct tab *tab, int hide)
2035 /* TODO: hard-switching to another tab is ugly */
2036 switch_to_tab(tab);
2038 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2039 &ir_history);
2040 strlcpy(ministate.prompt, "Input required: ",
2041 sizeof(ministate.prompt));
2042 redraw_tab(tab);
2045 void
2046 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2047 unsigned int data)
2049 size_t len;
2051 if (in_minibuffer) {
2052 fn(0, data);
2053 return;
2056 yornp_cb = fn;
2057 yornp_data = data;
2058 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2059 yornp_abort, NULL);
2061 len = sizeof(ministate.prompt);
2062 strlcpy(ministate.prompt, prompt, len);
2063 strlcat(ministate.prompt, " (y or n) ", len);
2064 redraw_tab(current_tab());
2067 void
2068 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2069 unsigned int data)
2071 size_t len;
2073 if (in_minibuffer)
2074 return;
2076 read_cb = fn;
2077 read_data = data;
2078 enter_minibuffer(read_self_insert, read_select, read_abort,
2079 &read_history);
2081 len = sizeof(ministate.prompt);
2082 strlcpy(ministate.prompt, prompt, len);
2083 strlcat(ministate.prompt, ": ", len);
2084 redraw_tab(current_tab());
2087 void
2088 ui_notify(const char *fmt, ...)
2090 va_list ap;
2092 va_start(ap, fmt);
2093 vmessage(fmt, ap);
2094 va_end(ap);
2097 void
2098 ui_end(void)
2100 endwin();