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 const char *url;
685 if ((url = new_tab_url) == NULL)
686 url = NEW_TAB_URL;
688 new_tab(url);
691 void
692 cmd_tab_next(struct buffer *buffer)
694 struct tab *tab, *t;
696 tab = current_tab();
697 tab->flags &= ~TAB_CURRENT;
699 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
700 t = TAILQ_FIRST(&tabshead);
701 t->flags |= TAB_CURRENT;
702 t->flags &= ~TAB_URGENT;
705 void
706 cmd_tab_previous(struct buffer *buffer)
708 struct tab *tab, *t;
710 tab = current_tab();
711 tab->flags &= ~TAB_CURRENT;
713 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
714 t = TAILQ_LAST(&tabshead, tabshead);
715 t->flags |= TAB_CURRENT;
716 t->flags &= ~TAB_URGENT;
719 void
720 cmd_tab_move(struct buffer *buffer)
722 struct tab *tab, *t;
724 tab = current_tab();
725 t = TAILQ_NEXT(tab, tabs);
726 TAILQ_REMOVE(&tabshead, tab, tabs);
728 if (t == NULL)
729 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
730 else
731 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
734 void
735 cmd_tab_move_to(struct buffer *buffer)
737 struct tab *tab, *t;
739 tab = current_tab();
740 t = TAILQ_PREV(tab, tabshead, tabs);
741 TAILQ_REMOVE(&tabshead, tab, tabs);
743 if (t == NULL) {
744 if (TAILQ_EMPTY(&tabshead))
745 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
746 else
747 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
748 } else
749 TAILQ_INSERT_BEFORE(t, tab, tabs);
752 void
753 cmd_load_url(struct buffer *buffer)
755 if (in_minibuffer) {
756 message("We don't have enable-recursive-minibuffers");
757 return;
760 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
761 &lu_history);
762 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
763 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
764 cmd_move_end_of_line(&ministate.buffer);
767 void
768 cmd_load_current_url(struct buffer *buffer)
770 struct tab *tab = current_tab();
772 if (in_minibuffer) {
773 message("We don't have enable-recursive-minibuffers");
774 return;
777 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
778 &lu_history);
779 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
780 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
781 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
784 void
785 cmd_bookmark_page(struct buffer *buffer)
787 struct tab *tab = current_tab();
789 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
790 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
791 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
792 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
795 void
796 cmd_list_bookmarks(struct buffer *buffer)
798 load_url_in_tab(current_tab(), "about:bookmarks");
801 void
802 cmd_toggle_help(struct buffer *buffer)
804 side_window = !side_window;
805 if (side_window)
806 recompute_help();
808 /*
809 * ugly hack, but otherwise the window doesn't get updated
810 * until I call handle_resize a second time (i.e. C-l). I
811 * will be happy to know why something like this is needed.
812 */
813 handle_resize_nodelay(0, 0, NULL);
814 handle_resize_nodelay(0, 0, NULL);
817 void
818 cmd_mini_delete_char(struct buffer *buffer)
820 char *c, *n;
822 if (!in_minibuffer) {
823 message("text is read-only");
824 return;
827 minibuffer_taint_hist();
829 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
830 if (*c == '\0')
831 return;
832 n = utf8_next_cp(c);
834 memmove(c, n, strlen(n)+1);
837 void
838 cmd_mini_delete_backward_char(struct buffer *buffer)
840 char *c, *p, *start;
842 if (!in_minibuffer) {
843 message("text is read-only");
844 return;
847 minibuffer_taint_hist();
849 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
850 start = buffer->current_line->line;
851 if (c == start)
852 return;
853 p = utf8_prev_cp(c-1, start);
855 memmove(p, c, strlen(c)+1);
856 buffer->cpoff--;
859 void
860 cmd_mini_kill_line(struct buffer *buffer)
862 char *c;
864 if (!in_minibuffer) {
865 message("text is read-only");
866 return;
869 minibuffer_taint_hist();
870 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
871 *c = '\0';
874 void
875 cmd_mini_abort(struct buffer *buffer)
877 if (!in_minibuffer)
878 return;
880 ministate.abortfn();
883 void
884 cmd_mini_complete_and_exit(struct buffer *buffer)
886 if (!in_minibuffer)
887 return;
889 minibuffer_taint_hist();
890 ministate.donefn();
893 void
894 cmd_mini_previous_history_element(struct buffer *buffer)
896 if (ministate.history == NULL) {
897 message("No history");
898 return;
901 if (ministate.hist_cur == NULL ||
902 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
903 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
904 ministate.hist_off = ministate.history->len - 1;
905 if (ministate.hist_cur == NULL)
906 message("No prev item");
907 } else {
908 ministate.hist_off--;
911 if (ministate.hist_cur != NULL)
912 buffer->current_line->line = ministate.hist_cur->h;
915 void
916 cmd_mini_next_history_element(struct buffer *buffer)
918 if (ministate.history == NULL) {
919 message("No history");
920 return;
923 if (ministate.hist_cur == NULL ||
924 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
925 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
926 ministate.hist_off = 0;
927 if (ministate.hist_cur == NULL)
928 message("No next item");
929 } else {
930 ministate.hist_off++;
933 if (ministate.hist_cur != NULL)
934 buffer->current_line->line = ministate.hist_cur->h;
937 static void
938 global_key_unbound(void)
940 message("%s is undefined", keybuf);
943 static void
944 minibuffer_hist_save_entry(void)
946 struct hist *hist;
948 if (ministate.history == NULL)
949 return;
951 if ((hist = calloc(1, sizeof(*hist))) == NULL)
952 abort();
954 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
956 if (TAILQ_EMPTY(&ministate.history->head))
957 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
958 else
959 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
960 ministate.history->len++;
963 /*
964 * taint the minibuffer cache: if we're currently showing a history
965 * element, copy that to the current buf and reset the "history
966 * navigation" thing.
967 */
968 static void
969 minibuffer_taint_hist(void)
971 if (ministate.hist_cur == NULL)
972 return;
974 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
975 ministate.hist_cur = NULL;
978 static void
979 minibuffer_self_insert(void)
981 char *c, tmp[5] = {0};
982 size_t len;
984 minibuffer_taint_hist();
986 if (thiskey.cp == 0)
987 return;
989 len = utf8_encode(thiskey.cp, tmp);
990 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
991 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
992 return;
994 memmove(c + len, c, strlen(c)+1);
995 memcpy(c, tmp, len);
996 ministate.buffer.cpoff++;
999 static void
1000 eecmd_self_insert(void)
1002 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1003 !unicode_isgraph(thiskey.cp)) {
1004 global_key_unbound();
1005 return;
1008 minibuffer_self_insert();
1011 static void
1012 eecmd_select(void)
1014 struct cmds *cmd;
1016 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1017 if (!strcmp(cmd->cmd, ministate.buf)) {
1018 exit_minibuffer();
1019 minibuffer_hist_save_entry();
1020 cmd->fn(current_buffer());
1021 return;
1025 message("No match");
1028 static void
1029 ir_self_insert(void)
1031 minibuffer_self_insert();
1034 static void
1035 ir_select(void)
1037 char buf[1025] = {0};
1038 struct phos_uri uri;
1039 struct tab *tab;
1041 tab = current_tab();
1043 exit_minibuffer();
1044 minibuffer_hist_save_entry();
1046 /* a bit ugly but... */
1047 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1048 phos_uri_set_query(&uri, ministate.buf);
1049 phos_serialize_uri(&uri, buf, sizeof(buf));
1050 load_url_in_tab(tab, buf);
1053 static void
1054 lu_self_insert(void)
1056 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1057 !unicode_isgraph(thiskey.key)) {
1058 global_key_unbound();
1059 return;
1062 minibuffer_self_insert();
1065 static void
1066 lu_select(void)
1068 exit_minibuffer();
1069 minibuffer_hist_save_entry();
1070 load_url_in_tab(current_tab(), ministate.buf);
1073 static void
1074 bp_select(void)
1076 exit_minibuffer();
1077 if (*ministate.buf != '\0')
1078 add_to_bookmarks(ministate.buf);
1079 else
1080 message("Abort.");
1083 static void
1084 yornp_self_insert(void)
1086 if (thiskey.key != 'y' && thiskey.key != 'n') {
1087 message("Please answer y or n");
1088 return;
1091 exit_minibuffer();
1092 yornp_cb(thiskey.key == 'y', yornp_data);
1095 static void
1096 yornp_abort(void)
1098 exit_minibuffer();
1099 yornp_cb(0, yornp_data);
1102 static void
1103 read_self_insert(void)
1105 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1106 global_key_unbound();
1107 return;
1110 minibuffer_self_insert();
1113 static void
1114 read_abort(void)
1116 exit_minibuffer();
1117 read_cb(NULL, read_data);
1120 static void
1121 read_select(void)
1123 exit_minibuffer();
1124 minibuffer_hist_save_entry();
1125 read_cb(ministate.buf, read_data);
1128 static struct vline *
1129 nth_line(struct buffer *buffer, size_t n)
1131 struct vline *vl;
1132 size_t i;
1134 i = 0;
1135 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1136 if (i == n)
1137 return vl;
1138 i++;
1141 /* unreachable */
1142 abort();
1145 static struct tab *
1146 current_tab(void)
1148 struct tab *t;
1150 TAILQ_FOREACH(t, &tabshead, tabs) {
1151 if (t->flags & TAB_CURRENT)
1152 return t;
1155 /* unreachable */
1156 abort();
1159 static struct buffer *
1160 current_buffer(void)
1162 if (in_minibuffer)
1163 return &ministate.buffer;
1164 return &current_tab()->buffer;
1167 static int
1168 readkey(void)
1170 uint32_t state = 0;
1172 if ((thiskey.key = wgetch(body)) == ERR)
1173 return 0;
1175 thiskey.meta = thiskey.key == 27;
1176 if (thiskey.meta) {
1177 thiskey.key = wgetch(body);
1178 if (thiskey.key == ERR || thiskey.key == 27) {
1179 thiskey.meta = 0;
1180 thiskey.key = 27;
1184 thiskey.cp = 0;
1185 if ((unsigned int)thiskey.key < UINT8_MAX) {
1186 while (1) {
1187 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1188 break;
1189 if ((thiskey.key = wgetch(body)) == ERR) {
1190 message("Error decoding user input");
1191 return 0;
1196 return 1;
1199 static void
1200 dispatch_stdio(int fd, short ev, void *d)
1202 struct keymap *k;
1203 const char *keyname;
1204 char tmp[5] = {0};
1206 if (!readkey())
1207 return;
1209 if (keybuf[0] != '\0')
1210 strlcat(keybuf, " ", sizeof(keybuf));
1211 if (thiskey.meta)
1212 strlcat(keybuf, "M-", sizeof(keybuf));
1213 if (thiskey.cp != 0) {
1214 utf8_encode(thiskey.cp, tmp);
1215 strlcat(keybuf, tmp, sizeof(keybuf));
1216 } else {
1217 if ((keyname = unkbd(thiskey.key)) != NULL)
1218 strlcat(keybuf, keyname, sizeof(keybuf));
1219 else {
1220 tmp[0] = thiskey.key;
1221 strlcat(keybuf, tmp, sizeof(keybuf));
1225 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1226 if (k->meta == thiskey.meta &&
1227 k->key == thiskey.key) {
1228 if (k->fn == NULL)
1229 current_map = &k->map;
1230 else {
1231 current_map = base_map;
1232 strlcpy(keybuf, "", sizeof(keybuf));
1233 k->fn(current_buffer());
1235 goto done;
1239 if (current_map->unhandled_input != NULL)
1240 current_map->unhandled_input();
1241 else {
1242 global_key_unbound();
1245 strlcpy(keybuf, "", sizeof(keybuf));
1246 current_map = base_map;
1248 done:
1249 if (side_window)
1250 recompute_help();
1252 redraw_tab(current_tab());
1255 static void
1256 handle_clear_minibuf(int fd, short ev, void *d)
1258 free(ministate.curmesg);
1259 ministate.curmesg = NULL;
1261 redraw_minibuffer();
1262 if (in_minibuffer) {
1263 wrefresh(body);
1264 wrefresh(minibuf);
1265 } else {
1266 wrefresh(minibuf);
1267 wrefresh(body);
1271 static void
1272 handle_resize(int sig, short ev, void *d)
1274 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1275 event_del(&resizeev);
1277 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1278 evtimer_add(&resizeev, &resize_timer);
1281 static void
1282 handle_resize_nodelay(int s, short ev, void *d)
1284 struct tab *tab;
1286 endwin();
1287 refresh();
1288 clear();
1290 /* move and resize the windows, in reverse order! */
1292 mvwin(minibuf, LINES-1, 0);
1293 wresize(minibuf, 1, COLS);
1295 mvwin(modeline, LINES-2, 0);
1296 wresize(modeline, 1, COLS);
1298 body_lines = LINES-3;
1299 body_cols = COLS;
1301 if (side_window) {
1302 help_cols = 0.3 * COLS;
1303 help_lines = LINES-3;
1304 mvwin(help, 1, 0);
1305 wresize(help, help_lines, help_cols);
1307 wrap_page(&helpwin, help_cols);
1309 body_cols = COLS - help_cols - 1;
1310 mvwin(body, 1, help_cols);
1311 } else
1312 mvwin(body, 1, 0);
1314 wresize(body, body_lines, body_cols);
1316 wresize(tabline, 1, COLS);
1318 tab = current_tab();
1320 wrap_page(&tab->buffer, body_cols);
1321 redraw_tab(tab);
1324 static int
1325 wrap_page(struct buffer *buffer, int width)
1327 struct line *l;
1328 const struct line *orig;
1329 struct vline *vl;
1330 const char *prfx;
1332 orig = buffer->current_line == NULL
1333 ? NULL
1334 : buffer->current_line->parent;
1335 buffer->current_line = NULL;
1337 buffer->curs_y = 0;
1338 buffer->line_off = 0;
1340 empty_vlist(buffer);
1342 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1343 prfx = line_prefixes[l->type].prfx1;
1344 switch (l->type) {
1345 case LINE_TEXT:
1346 case LINE_LINK:
1347 case LINE_TITLE_1:
1348 case LINE_TITLE_2:
1349 case LINE_TITLE_3:
1350 case LINE_ITEM:
1351 case LINE_QUOTE:
1352 case LINE_PRE_START:
1353 case LINE_PRE_END:
1354 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1355 break;
1356 case LINE_PRE_CONTENT:
1357 hardwrap_text(buffer, l, width);
1358 break;
1361 if (orig == l && buffer->current_line == NULL) {
1362 buffer->line_off = buffer->line_max-1;
1363 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1365 while (1) {
1366 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1367 if (vl == NULL || vl->parent != orig)
1368 break;
1369 buffer->current_line = vl;
1370 buffer->line_off--;
1375 if (buffer->current_line == NULL)
1376 buffer->current_line = TAILQ_FIRST(&buffer->head);
1378 return 1;
1381 static void
1382 print_vline(WINDOW *window, struct vline *vl)
1384 const char *text = vl->line;
1385 const char *prfx;
1386 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1387 int text_face = line_faces[vl->parent->type].text_prop;
1389 if (!vl->flags)
1390 prfx = line_prefixes[vl->parent->type].prfx1;
1391 else
1392 prfx = line_prefixes[vl->parent->type].prfx2;
1394 if (text == NULL)
1395 text = "";
1397 wattron(window, prefix_face);
1398 wprintw(window, "%s", prfx);
1399 wattroff(window, prefix_face);
1401 wattron(window, text_face);
1402 wprintw(window, "%s", text);
1403 wattroff(window, text_face);
1406 static void
1407 redraw_tabline(void)
1409 struct tab *tab;
1410 size_t toskip, ots, tabwidth, space, x;
1411 int current, y, truncated;
1412 const char *title;
1413 char buf[25];
1415 tabwidth = sizeof(buf)+1;
1416 space = COLS-2;
1418 toskip = 0;
1419 TAILQ_FOREACH(tab, &tabshead, tabs) {
1420 toskip++;
1421 if (tab->flags & TAB_CURRENT)
1422 break;
1425 if (toskip * tabwidth < space)
1426 toskip = 0;
1427 else {
1428 ots = toskip;
1429 toskip--;
1430 while (toskip != 0 &&
1431 (ots - toskip+1) * tabwidth < space)
1432 toskip--;
1435 werase(tabline);
1436 wattron(tabline, tab_face.background);
1437 wprintw(tabline, toskip == 0 ? " " : "<");
1438 wattroff(tabline, tab_face.background);
1440 truncated = 0;
1441 TAILQ_FOREACH(tab, &tabshead, tabs) {
1442 if (truncated)
1443 break;
1444 if (toskip != 0) {
1445 toskip--;
1446 continue;
1449 getyx(tabline, y, x);
1450 if (x + sizeof(buf)+2 >= (size_t)COLS)
1451 truncated = 1;
1453 current = tab->flags & TAB_CURRENT;
1455 if (*(title = tab->buffer.page.title) == '\0')
1456 title = tab->hist_cur->h;
1458 if (tab->flags & TAB_URGENT)
1459 strlcpy(buf, "!", sizeof(buf));
1460 else
1461 strlcpy(buf, " ", sizeof(buf));
1463 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1464 /* truncation happens */
1465 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1466 } else {
1467 /* pad with spaces */
1468 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1469 /* nop */ ;
1472 if (current)
1473 wattron(tabline, tab_face.current_tab);
1474 else
1475 wattron(tabline, tab_face.tab);
1477 wprintw(tabline, "%s", buf);
1478 if (TAILQ_NEXT(tab, tabs) != NULL)
1479 wprintw(tabline, " ");
1481 if (current)
1482 wattroff(tabline, tab_face.current_tab);
1483 else
1484 wattroff(tabline, tab_face.tab);
1487 wattron(tabline, tab_face.background);
1488 for (; x < (size_t)COLS; ++x)
1489 waddch(tabline, ' ');
1490 if (truncated)
1491 mvwprintw(tabline, 0, COLS-1, ">");
1494 static void
1495 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1497 struct vline *vl;
1498 int l;
1500 werase(win);
1502 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1503 if (TAILQ_EMPTY(&buffer->head))
1504 return;
1506 l = 0;
1507 vl = nth_line(buffer, buffer->line_off);
1508 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1509 wmove(win, l, 0);
1510 print_vline(win, vl);
1511 l++;
1512 if (l == height)
1513 break;
1516 wmove(win, buffer->curs_y, buffer->curs_x);
1519 static void
1520 redraw_help(void)
1522 redraw_window(help, help_lines, &helpwin);
1525 static void
1526 redraw_body(struct tab *tab)
1528 redraw_window(body, body_lines, &tab->buffer);
1531 static inline char
1532 trust_status_char(enum trust_state ts)
1534 switch (ts) {
1535 case TS_UNKNOWN: return 'u';
1536 case TS_UNTRUSTED: return '!';
1537 case TS_TRUSTED: return 'v';
1538 case TS_VERIFIED: return 'V';
1542 static void
1543 redraw_modeline(struct tab *tab)
1545 double pct;
1546 int x, y, max_x, max_y;
1547 const char *mode = tab->buffer.page.name;
1548 const char *spin = "-\\|/";
1550 werase(modeline);
1551 wattron(modeline, modeline_face.background);
1552 wmove(modeline, 0, 0);
1554 wprintw(modeline, "-%c%c %s ",
1555 spin[tab->loading_anim_step],
1556 trust_status_char(tab->trust),
1557 mode == NULL ? "(none)" : mode);
1559 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1561 if (tab->buffer.line_max <= (size_t)body_lines)
1562 wprintw(modeline, "All ");
1563 else if (tab->buffer.line_off == 0)
1564 wprintw(modeline, "Top ");
1565 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1566 wprintw(modeline, "Bottom ");
1567 else
1568 wprintw(modeline, "%.0f%% ", pct);
1570 wprintw(modeline, "%d/%d %s ",
1571 tab->buffer.line_off + tab->buffer.curs_y,
1572 tab->buffer.line_max,
1573 tab->hist_cur->h);
1575 getyx(modeline, y, x);
1576 getmaxyx(modeline, max_y, max_x);
1578 (void)y;
1579 (void)max_y;
1581 for (; x < max_x; ++x)
1582 waddstr(modeline, "-");
1584 wattroff(modeline, modeline_face.background);
1587 static void
1588 redraw_minibuffer(void)
1590 struct tab *tab;
1591 size_t off_y, off_x = 0;
1592 char *start, *c;
1594 wattron(minibuf, minibuffer_face.background);
1595 werase(minibuf);
1597 if (in_minibuffer) {
1598 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1599 if (ministate.hist_cur != NULL)
1600 wprintw(minibuf, "(%zu/%zu) ",
1601 ministate.hist_off + 1,
1602 ministate.history->len);
1604 getyx(minibuf, off_y, off_x);
1606 start = ministate.hist_cur != NULL
1607 ? ministate.hist_cur->h
1608 : ministate.buf;
1609 c = utf8_nth(ministate.buffer.current_line->line,
1610 ministate.buffer.cpoff);
1611 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1612 start = utf8_next_cp(start);
1615 waddstr(minibuf, start);
1618 if (ministate.curmesg != NULL)
1619 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1620 ministate.curmesg);
1622 if (!in_minibuffer && ministate.curmesg == NULL)
1623 waddstr(minibuf, keybuf);
1625 /* If nothing else, show the URL at point */
1626 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1627 tab = current_tab();
1628 if (tab->buffer.current_line != NULL &&
1629 tab->buffer.current_line->parent->type == LINE_LINK)
1630 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1633 if (in_minibuffer)
1634 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1636 wattroff(minibuf, minibuffer_face.background);
1639 static void
1640 redraw_tab(struct tab *tab)
1642 if (side_window) {
1643 redraw_help();
1644 wnoutrefresh(help);
1647 redraw_tabline();
1648 redraw_body(tab);
1649 redraw_modeline(tab);
1650 redraw_minibuffer();
1652 wnoutrefresh(tabline);
1653 wnoutrefresh(modeline);
1655 if (in_minibuffer) {
1656 wnoutrefresh(body);
1657 wnoutrefresh(minibuf);
1658 } else {
1659 wnoutrefresh(minibuf);
1660 wnoutrefresh(body);
1663 doupdate();
1666 static void
1667 emit_help_item(char *prfx, void *fn)
1669 struct line *l;
1670 struct cmds *cmd;
1672 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1673 if (fn == cmd->fn)
1674 break;
1676 assert(cmd != NULL);
1678 if ((l = calloc(1, sizeof(*l))) == NULL)
1679 abort();
1681 l->type = LINE_TEXT;
1682 l->alt = NULL;
1684 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1686 if (TAILQ_EMPTY(&helpwin.page.head))
1687 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1688 else
1689 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1692 static void
1693 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1695 struct keymap *k;
1696 char p[32];
1697 const char *kn;
1699 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1700 strlcpy(p, prfx, sizeof(p));
1701 if (*p != '\0')
1702 strlcat(p, " ", sizeof(p));
1703 if (k->meta)
1704 strlcat(p, "M-", sizeof(p));
1705 if ((kn = unkbd(k->key)) != NULL)
1706 strlcat(p, kn, sizeof(p));
1707 else
1708 strlcat(p, keyname(k->key), sizeof(p));
1710 if (k->fn == NULL)
1711 rec_compute_help(&k->map, p, sizeof(p));
1712 else
1713 emit_help_item(p, k->fn);
1717 static void
1718 recompute_help(void)
1720 char p[32] = { 0 };
1722 empty_vlist(&helpwin);
1723 empty_linelist(&helpwin);
1724 rec_compute_help(current_map, p, sizeof(p));
1725 wrap_page(&helpwin, help_cols);
1728 static void
1729 vmessage(const char *fmt, va_list ap)
1731 if (evtimer_pending(&clminibufev, NULL))
1732 evtimer_del(&clminibufev);
1733 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1734 evtimer_add(&clminibufev, &clminibufev_timer);
1736 free(ministate.curmesg);
1738 /* TODO: what to do if the allocation fails here? */
1739 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1740 ministate.curmesg = NULL;
1742 redraw_minibuffer();
1743 if (in_minibuffer) {
1744 wrefresh(body);
1745 wrefresh(minibuf);
1746 } else {
1747 wrefresh(minibuf);
1748 wrefresh(body);
1752 static void
1753 message(const char *fmt, ...)
1755 va_list ap;
1757 va_start(ap, fmt);
1758 vmessage(fmt, ap);
1759 va_end(ap);
1762 static void
1763 start_loading_anim(struct tab *tab)
1765 if (tab->loading_anim)
1766 return;
1767 tab->loading_anim = 1;
1768 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1769 evtimer_add(&tab->loadingev, &loadingev_timer);
1772 static void
1773 update_loading_anim(int fd, short ev, void *d)
1775 struct tab *tab = d;
1777 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1779 if (tab->flags & TAB_CURRENT) {
1780 redraw_modeline(tab);
1781 wrefresh(modeline);
1782 wrefresh(body);
1783 if (in_minibuffer)
1784 wrefresh(minibuf);
1787 evtimer_add(&tab->loadingev, &loadingev_timer);
1790 static void
1791 stop_loading_anim(struct tab *tab)
1793 if (!tab->loading_anim)
1794 return;
1795 evtimer_del(&tab->loadingev);
1796 tab->loading_anim = 0;
1797 tab->loading_anim_step = 0;
1799 if (!(tab->flags & TAB_CURRENT))
1800 return;
1802 redraw_modeline(tab);
1804 wrefresh(modeline);
1805 wrefresh(body);
1806 if (in_minibuffer)
1807 wrefresh(minibuf);
1810 static void
1811 load_url_in_tab(struct tab *tab, const char *url)
1813 message("Loading %s...", url);
1814 start_loading_anim(tab);
1815 load_url(tab, url);
1817 tab->buffer.curs_x = 0;
1818 tab->buffer.curs_y = 0;
1819 redraw_tab(tab);
1822 static void
1823 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1824 void (*abortfn)(void), struct histhead *hist)
1826 in_minibuffer = 1;
1827 base_map = &minibuffer_map;
1828 current_map = &minibuffer_map;
1830 base_map->unhandled_input = self_insert_fn;
1832 ministate.donefn = donefn;
1833 ministate.abortfn = abortfn;
1834 memset(ministate.buf, 0, sizeof(ministate.buf));
1835 ministate.buffer.current_line = &ministate.vline;
1836 ministate.buffer.current_line->line = ministate.buf;
1837 ministate.buffer.cpoff = 0;
1838 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1840 ministate.history = hist;
1841 ministate.hist_cur = NULL;
1842 ministate.hist_off = 0;
1845 static void
1846 exit_minibuffer(void)
1848 werase(minibuf);
1850 in_minibuffer = 0;
1851 base_map = &global_map;
1852 current_map = &global_map;
1855 static void
1856 switch_to_tab(struct tab *tab)
1858 struct tab *t;
1860 TAILQ_FOREACH(t, &tabshead, tabs) {
1861 t->flags &= ~TAB_CURRENT;
1864 tab->flags |= TAB_CURRENT;
1865 tab->flags &= ~TAB_URGENT;
1868 unsigned int
1869 tab_new_id(void)
1871 return tab_counter++;
1874 static struct tab *
1875 new_tab(const char *url)
1877 struct tab *tab;
1879 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1880 event_loopbreak();
1881 return NULL;
1883 tab->fd = -1;
1885 TAILQ_INIT(&tab->hist.head);
1887 TAILQ_INIT(&tab->buffer.head);
1889 tab->id = tab_new_id();
1890 switch_to_tab(tab);
1892 if (TAILQ_EMPTY(&tabshead))
1893 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1894 else
1895 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1897 load_url_in_tab(tab, url);
1898 return tab;
1901 static void
1902 session_new_tab_cb(const char *url)
1904 new_tab(url);
1907 static void
1908 usage(void)
1910 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1911 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1914 int
1915 ui_init(int argc, char * const *argv)
1917 char path[PATH_MAX];
1918 const char *url = NEW_TAB_URL;
1919 int ch, configtest = 0, fonf = 0;
1921 strlcpy(path, getenv("HOME"), sizeof(path));
1922 strlcat(path, "/.telescope/config", sizeof(path));
1924 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1925 switch (ch) {
1926 case 'c':
1927 fonf = 1;
1928 strlcpy(path, optarg, sizeof(path));
1929 break;
1930 case 'n':
1931 configtest = 1;
1932 break;
1933 case 'h':
1934 usage();
1935 return 0;
1936 default:
1937 usage();
1938 return 1;
1941 argc -= optind;
1942 argv += optind;
1944 parseconfig(path, fonf);
1945 if (configtest){
1946 puts("config OK");
1947 exit(0);
1950 if (argc != 0)
1951 url = argv[0];
1953 setlocale(LC_ALL, "");
1955 TAILQ_INIT(&global_map.m);
1956 global_map.unhandled_input = global_key_unbound;
1958 TAILQ_INIT(&minibuffer_map.m);
1960 TAILQ_INIT(&eecmd_history.head);
1961 TAILQ_INIT(&ir_history.head);
1962 TAILQ_INIT(&lu_history.head);
1964 ministate.line.type = LINE_TEXT;
1965 ministate.vline.parent = &ministate.line;
1966 ministate.buffer.current_line = &ministate.vline;
1968 /* initialize help window */
1969 TAILQ_INIT(&helpwin.head);
1971 base_map = &global_map;
1972 current_map = &global_map;
1973 load_default_keys();
1975 initscr();
1976 raw();
1977 noecho();
1979 nonl();
1980 intrflush(stdscr, FALSE);
1982 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1983 return 0;
1984 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1985 return 0;
1986 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1987 return 0;
1988 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1989 return 0;
1990 if ((help = newwin(1, 1, 1, 0)) == NULL)
1991 return 0;
1993 body_lines = LINES-3;
1994 body_cols = COLS;
1996 keypad(body, TRUE);
1997 scrollok(body, TRUE);
1999 /* non-blocking input */
2000 wtimeout(body, 0);
2002 mvwprintw(body, 0, 0, "");
2004 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2005 event_add(&stdioev, NULL);
2007 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2008 signal_add(&winchev, NULL);
2010 load_last_session(session_new_tab_cb);
2011 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2012 new_tab(url);
2014 return 1;
2017 void
2018 ui_on_tab_loaded(struct tab *tab)
2020 stop_loading_anim(tab);
2021 message("Loaded %s", tab->hist_cur->h);
2023 redraw_tabline();
2024 wrefresh(tabline);
2025 if (in_minibuffer)
2026 wrefresh(minibuf);
2027 else
2028 wrefresh(body);
2031 void
2032 ui_on_tab_refresh(struct tab *tab)
2034 wrap_page(&tab->buffer, body_cols);
2035 if (tab->flags & TAB_CURRENT) {
2036 restore_cursor(&tab->buffer);
2037 redraw_tab(tab);
2038 } else
2039 tab->flags |= TAB_URGENT;
2042 void
2043 ui_require_input(struct tab *tab, int hide)
2045 /* TODO: hard-switching to another tab is ugly */
2046 switch_to_tab(tab);
2048 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2049 &ir_history);
2050 strlcpy(ministate.prompt, "Input required: ",
2051 sizeof(ministate.prompt));
2052 redraw_tab(tab);
2055 void
2056 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2057 unsigned int data)
2059 size_t len;
2061 if (in_minibuffer) {
2062 fn(0, data);
2063 return;
2066 yornp_cb = fn;
2067 yornp_data = data;
2068 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2069 yornp_abort, NULL);
2071 len = sizeof(ministate.prompt);
2072 strlcpy(ministate.prompt, prompt, len);
2073 strlcat(ministate.prompt, " (y or n) ", len);
2074 redraw_tab(current_tab());
2077 void
2078 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2079 unsigned int data)
2081 size_t len;
2083 if (in_minibuffer)
2084 return;
2086 read_cb = fn;
2087 read_data = data;
2088 enter_minibuffer(read_self_insert, read_select, read_abort,
2089 &read_history);
2091 len = sizeof(ministate.prompt);
2092 strlcpy(ministate.prompt, prompt, len);
2093 strlcat(ministate.prompt, ": ", len);
2094 redraw_tab(current_tab());
2097 void
2098 ui_notify(const char *fmt, ...)
2100 va_list ap;
2102 va_start(ap, fmt);
2103 vmessage(fmt, ap);
2104 va_end(ap);
2107 void
2108 ui_end(void)
2110 endwin();