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("tab", cmd_next_button);
279 /* === minibuffer map === */
280 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
281 minibuffer_set_key("C-g", cmd_mini_abort);
282 minibuffer_set_key("esc", cmd_mini_abort);
283 minibuffer_set_key("C-d", cmd_mini_delete_char);
284 minibuffer_set_key("del", cmd_mini_delete_backward_char);
285 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
286 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
288 minibuffer_set_key("C-b", cmd_backward_char);
289 minibuffer_set_key("C-f", cmd_forward_char);
290 minibuffer_set_key("<left>", cmd_backward_char);
291 minibuffer_set_key("<right>", cmd_forward_char);
292 minibuffer_set_key("C-e", cmd_move_end_of_line);
293 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
294 minibuffer_set_key("<end>", cmd_move_end_of_line);
295 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
296 minibuffer_set_key("C-k", cmd_mini_kill_line);
298 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
299 minibuffer_set_key("M-n", cmd_mini_next_history_element);
300 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
301 minibuffer_set_key("<down>", cmd_mini_next_history_element);
304 static void
305 restore_cursor(struct buffer *buffer)
307 struct vline *vl;
308 const char *prfx;
310 vl = buffer->current_line;
311 if (vl == NULL || vl->line == NULL)
312 buffer->curs_x = buffer->cpoff = 0;
313 else
314 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
316 if (vl != NULL) {
317 prfx = line_prefixes[vl->parent->type].prfx1;
318 buffer->curs_x += utf8_swidth(prfx);
322 void
323 cmd_previous_line(struct buffer *buffer)
325 struct vline *vl;
327 if (buffer->current_line == NULL
328 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
329 return;
331 if (--buffer->curs_y < 0) {
332 buffer->curs_y = 0;
333 cmd_scroll_line_up(buffer);
334 return;
337 buffer->current_line = vl;
338 restore_cursor(buffer);
341 void
342 cmd_next_line(struct buffer *buffer)
344 struct vline *vl;
346 if (buffer->current_line == NULL
347 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
348 return;
350 if (++buffer->curs_y > body_lines-1) {
351 buffer->curs_y = body_lines-1;
352 cmd_scroll_line_down(buffer);
353 return;
356 buffer->current_line = vl;
357 restore_cursor(buffer);
360 void
361 cmd_backward_char(struct buffer *buffer)
363 if (buffer->cpoff != 0)
364 buffer->cpoff--;
365 restore_cursor(buffer);
368 void
369 cmd_forward_char(struct buffer *buffer)
371 size_t len = 0;
373 if (buffer->current_line->line != NULL)
374 len = utf8_cplen(buffer->current_line->line);
375 if (++buffer->cpoff > len)
376 buffer->cpoff = len;
377 restore_cursor(buffer);
380 void
381 cmd_backward_paragraph(struct buffer *buffer)
383 do {
384 if (buffer->current_line == NULL ||
385 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
386 message("No previous paragraph");
387 return;
389 cmd_previous_line(buffer);
390 } while (buffer->current_line->line != NULL ||
391 buffer->current_line->parent->type != LINE_TEXT);
394 void
395 cmd_forward_paragraph(struct buffer *buffer)
397 do {
398 if (buffer->current_line == NULL ||
399 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
400 message("No next paragraph");
401 return;
403 cmd_next_line(buffer);
404 } while (buffer->current_line->line != NULL ||
405 buffer->current_line->parent->type != LINE_TEXT);
408 void
409 cmd_move_beginning_of_line(struct buffer *buffer)
411 buffer->cpoff = 0;
412 restore_cursor(buffer);
415 void
416 cmd_move_end_of_line(struct buffer *buffer)
418 struct vline *vl;
420 vl = buffer->current_line;
421 if (vl->line == NULL)
422 return;
423 buffer->cpoff = utf8_cplen(vl->line);
424 restore_cursor(buffer);
427 void
428 cmd_redraw(struct buffer *buffer)
430 handle_resize(0, 0, NULL);
433 void
434 cmd_scroll_line_up(struct buffer *buffer)
436 struct vline *vl;
438 if (buffer->line_off == 0)
439 return;
441 vl = nth_line(buffer, --buffer->line_off);
442 wscrl(body, -1);
443 wmove(body, 0, 0);
444 print_vline(body, vl);
446 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
447 restore_cursor(buffer);
450 void
451 cmd_scroll_line_down(struct buffer *buffer)
453 struct vline *vl;
455 vl = buffer->current_line;
456 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
457 return;
458 buffer->current_line = vl;
460 buffer->line_off++;
461 wscrl(body, 1);
463 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
464 return;
466 vl = nth_line(buffer, buffer->line_off + body_lines-1);
467 wmove(body, body_lines-1, 0);
468 print_vline(body, vl);
470 restore_cursor(buffer);
473 void
474 cmd_scroll_up(struct buffer *buffer)
476 size_t off;
478 off = body_lines-1;
480 for (; off > 0; --off)
481 cmd_scroll_line_up(buffer);
484 void
485 cmd_scroll_down(struct buffer *buffer)
487 size_t off;
489 off = body_lines-1;
491 for (; off > 0; --off)
492 cmd_scroll_line_down(buffer);
495 void
496 cmd_beginning_of_buffer(struct buffer *buffer)
498 buffer->current_line = TAILQ_FIRST(&buffer->head);
499 buffer->line_off = 0;
500 buffer->curs_y = 0;
501 buffer->cpoff = 0;
502 restore_cursor(buffer);
505 void
506 cmd_end_of_buffer(struct buffer *buffer)
508 ssize_t off;
510 off = buffer->line_max - body_lines;
511 off = MAX(0, off);
513 buffer->line_off = off;
514 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
516 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
517 buffer->cpoff = body_cols;
518 restore_cursor(buffer);
521 void
522 cmd_kill_telescope(struct buffer *buffer)
524 save_session();
525 event_loopbreak();
528 void
529 cmd_push_button(struct buffer *buffer)
531 struct vline *vl;
532 size_t nth;
534 nth = buffer->line_off + buffer->curs_y;
535 if (nth >= buffer->line_max)
536 return;
537 vl = nth_line(buffer, nth);
538 if (vl->parent->type != LINE_LINK)
539 return;
541 load_url_in_tab(current_tab(), vl->parent->alt);
544 void
545 cmd_push_button_new_tab(struct buffer *buffer)
547 struct vline *vl;
548 size_t nth;
550 nth = buffer->line_off + buffer->curs_y;
551 if (nth > buffer->line_max)
552 return;
553 vl = nth_line(buffer, nth);
554 if (vl->parent->type != LINE_LINK)
555 return;
557 new_tab(vl->parent->alt);
560 void
561 cmd_previous_button(struct buffer *buffer)
563 do {
564 if (buffer->current_line == NULL ||
565 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
566 message("No previous link");
567 return;
569 cmd_previous_line(buffer);
570 } while (buffer->current_line->parent->type != LINE_LINK);
573 void
574 cmd_next_button(struct buffer *buffer)
576 do {
577 if (buffer->current_line == NULL ||
578 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
579 message("No next link");
580 return;
582 cmd_next_line(buffer);
583 } while (buffer->current_line->parent->type != LINE_LINK);
586 void
587 cmd_previous_page(struct buffer *buffer)
589 struct tab *tab = current_tab();
591 if (!load_previous_page(tab))
592 message("No previous page");
593 else
594 start_loading_anim(tab);
597 void
598 cmd_next_page(struct buffer *buffer)
600 struct tab *tab = current_tab();
602 if (!load_next_page(tab))
603 message("No next page");
604 else
605 start_loading_anim(tab);
608 void
609 cmd_clear_minibuf(struct buffer *buffer)
611 handle_clear_minibuf(0, 0, NULL);
614 void
615 cmd_execute_extended_command(struct buffer *buffer)
617 size_t len;
619 if (in_minibuffer) {
620 message("We don't have enable-recursive-minibuffers");
621 return;
624 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
625 &eecmd_history);
627 len = sizeof(ministate.prompt);
628 strlcpy(ministate.prompt, "", len);
630 if (thiskey.meta)
631 strlcat(ministate.prompt, "M-", len);
633 strlcat(ministate.prompt, keyname(thiskey.key), len);
635 if (thiskey.meta)
636 strlcat(ministate.prompt, " ", len);
639 void
640 cmd_tab_close(struct buffer *buffer)
642 struct tab *tab, *t;
644 tab = current_tab();
645 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
646 TAILQ_NEXT(tab, tabs) == NULL) {
647 message("Can't close the only tab.");
648 return;
651 if (evtimer_pending(&tab->loadingev, NULL))
652 evtimer_del(&tab->loadingev);
654 stop_tab(tab);
656 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
657 t = TAILQ_NEXT(tab, tabs);
658 TAILQ_REMOVE(&tabshead, tab, tabs);
659 free(tab);
661 switch_to_tab(t);
664 void
665 cmd_tab_close_other(struct buffer *buffer)
667 struct tab *t, *i;
669 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
670 if (t->flags & TAB_CURRENT)
671 continue;
673 stop_tab(t);
674 TAILQ_REMOVE(&tabshead, t, tabs);
675 free(t);
679 void
680 cmd_tab_new(struct buffer *buffer)
682 new_tab(NEW_TAB_URL);
685 void
686 cmd_tab_next(struct buffer *buffer)
688 struct tab *tab, *t;
690 tab = current_tab();
691 tab->flags &= ~TAB_CURRENT;
693 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
694 t = TAILQ_FIRST(&tabshead);
695 t->flags |= TAB_CURRENT;
696 t->flags &= ~TAB_URGENT;
699 void
700 cmd_tab_previous(struct buffer *buffer)
702 struct tab *tab, *t;
704 tab = current_tab();
705 tab->flags &= ~TAB_CURRENT;
707 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
708 t = TAILQ_LAST(&tabshead, tabshead);
709 t->flags |= TAB_CURRENT;
710 t->flags &= ~TAB_URGENT;
713 void
714 cmd_tab_move(struct buffer *buffer)
716 struct tab *tab, *t;
718 tab = current_tab();
719 t = TAILQ_NEXT(tab, tabs);
720 TAILQ_REMOVE(&tabshead, tab, tabs);
722 if (t == NULL)
723 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
724 else
725 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
728 void
729 cmd_tab_move_to(struct buffer *buffer)
731 struct tab *tab, *t;
733 tab = current_tab();
734 t = TAILQ_PREV(tab, tabshead, tabs);
735 TAILQ_REMOVE(&tabshead, tab, tabs);
737 if (t == NULL) {
738 if (TAILQ_EMPTY(&tabshead))
739 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
740 else
741 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
742 } else
743 TAILQ_INSERT_BEFORE(t, tab, tabs);
746 void
747 cmd_load_url(struct buffer *buffer)
749 if (in_minibuffer) {
750 message("We don't have enable-recursive-minibuffers");
751 return;
754 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
755 &lu_history);
756 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
757 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
758 cmd_move_end_of_line(&ministate.buffer);
761 void
762 cmd_load_current_url(struct buffer *buffer)
764 struct tab *tab = current_tab();
766 if (in_minibuffer) {
767 message("We don't have enable-recursive-minibuffers");
768 return;
771 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
772 &lu_history);
773 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
774 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
775 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
778 void
779 cmd_bookmark_page(struct buffer *buffer)
781 struct tab *tab = current_tab();
783 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
784 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
785 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
786 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
789 void
790 cmd_list_bookmarks(struct buffer *buffer)
792 load_url_in_tab(current_tab(), "about:bookmarks");
795 void
796 cmd_toggle_help(struct buffer *buffer)
798 side_window = !side_window;
799 if (side_window)
800 recompute_help();
802 /*
803 * ugly hack, but otherwise the window doesn't get updated
804 * until I call handle_resize a second time (i.e. C-l). I
805 * will be happy to know why something like this is needed.
806 */
807 handle_resize_nodelay(0, 0, NULL);
808 handle_resize_nodelay(0, 0, NULL);
811 void
812 cmd_mini_delete_char(struct buffer *buffer)
814 char *c, *n;
816 if (!in_minibuffer) {
817 message("text is read-only");
818 return;
821 minibuffer_taint_hist();
823 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
824 if (*c == '\0')
825 return;
826 n = utf8_next_cp(c);
828 memmove(c, n, strlen(n)+1);
831 void
832 cmd_mini_delete_backward_char(struct buffer *buffer)
834 char *c, *p, *start;
836 if (!in_minibuffer) {
837 message("text is read-only");
838 return;
841 minibuffer_taint_hist();
843 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
844 start = buffer->current_line->line;
845 if (c == start)
846 return;
847 p = utf8_prev_cp(c-1, start);
849 memmove(p, c, strlen(c)+1);
850 buffer->cpoff--;
853 void
854 cmd_mini_kill_line(struct buffer *buffer)
856 char *c;
858 if (!in_minibuffer) {
859 message("text is read-only");
860 return;
863 minibuffer_taint_hist();
864 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
865 *c = '\0';
868 void
869 cmd_mini_abort(struct buffer *buffer)
871 if (!in_minibuffer)
872 return;
874 ministate.abortfn();
877 void
878 cmd_mini_complete_and_exit(struct buffer *buffer)
880 if (!in_minibuffer)
881 return;
883 minibuffer_taint_hist();
884 ministate.donefn();
887 void
888 cmd_mini_previous_history_element(struct buffer *buffer)
890 if (ministate.history == NULL) {
891 message("No history");
892 return;
895 if (ministate.hist_cur == NULL ||
896 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
897 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
898 ministate.hist_off = ministate.history->len - 1;
899 if (ministate.hist_cur == NULL)
900 message("No prev item");
901 } else {
902 ministate.hist_off--;
905 if (ministate.hist_cur != NULL)
906 buffer->current_line->line = ministate.hist_cur->h;
909 void
910 cmd_mini_next_history_element(struct buffer *buffer)
912 if (ministate.history == NULL) {
913 message("No history");
914 return;
917 if (ministate.hist_cur == NULL ||
918 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
919 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
920 ministate.hist_off = 0;
921 if (ministate.hist_cur == NULL)
922 message("No next item");
923 } else {
924 ministate.hist_off++;
927 if (ministate.hist_cur != NULL)
928 buffer->current_line->line = ministate.hist_cur->h;
931 static void
932 global_key_unbound(void)
934 message("%s is undefined", keybuf);
937 static void
938 minibuffer_hist_save_entry(void)
940 struct hist *hist;
942 if (ministate.history == NULL)
943 return;
945 if ((hist = calloc(1, sizeof(*hist))) == NULL)
946 abort();
948 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
950 if (TAILQ_EMPTY(&ministate.history->head))
951 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
952 else
953 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
954 ministate.history->len++;
957 /*
958 * taint the minibuffer cache: if we're currently showing a history
959 * element, copy that to the current buf and reset the "history
960 * navigation" thing.
961 */
962 static void
963 minibuffer_taint_hist(void)
965 if (ministate.hist_cur == NULL)
966 return;
968 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
969 ministate.hist_cur = NULL;
972 static void
973 minibuffer_self_insert(void)
975 char *c, tmp[5] = {0};
976 size_t len;
978 minibuffer_taint_hist();
980 if (thiskey.cp == 0)
981 return;
983 len = utf8_encode(thiskey.cp, tmp);
984 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
985 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
986 return;
988 memmove(c + len, c, strlen(c)+1);
989 memcpy(c, tmp, len);
990 ministate.buffer.cpoff++;
993 static void
994 eecmd_self_insert(void)
996 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
997 !unicode_isgraph(thiskey.cp)) {
998 global_key_unbound();
999 return;
1002 minibuffer_self_insert();
1005 static void
1006 eecmd_select(void)
1008 struct cmds *cmd;
1010 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1011 if (!strcmp(cmd->cmd, ministate.buf)) {
1012 exit_minibuffer();
1013 minibuffer_hist_save_entry();
1014 cmd->fn(current_buffer());
1015 return;
1019 message("No match");
1022 static void
1023 ir_self_insert(void)
1025 minibuffer_self_insert();
1028 static void
1029 ir_select(void)
1031 char buf[1025] = {0};
1032 struct phos_uri uri;
1033 struct tab *tab;
1035 tab = current_tab();
1037 exit_minibuffer();
1038 minibuffer_hist_save_entry();
1040 /* a bit ugly but... */
1041 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1042 phos_uri_set_query(&uri, ministate.buf);
1043 phos_serialize_uri(&uri, buf, sizeof(buf));
1044 load_url_in_tab(tab, buf);
1047 static void
1048 lu_self_insert(void)
1050 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1051 !unicode_isgraph(thiskey.key)) {
1052 global_key_unbound();
1053 return;
1056 minibuffer_self_insert();
1059 static void
1060 lu_select(void)
1062 exit_minibuffer();
1063 minibuffer_hist_save_entry();
1064 load_url_in_tab(current_tab(), ministate.buf);
1067 static void
1068 bp_select(void)
1070 exit_minibuffer();
1071 if (*ministate.buf != '\0')
1072 add_to_bookmarks(ministate.buf);
1073 else
1074 message("Abort.");
1077 static void
1078 yornp_self_insert(void)
1080 if (thiskey.key != 'y' && thiskey.key != 'n') {
1081 message("Please answer y or n");
1082 return;
1085 exit_minibuffer();
1086 yornp_cb(thiskey.key == 'y', yornp_data);
1089 static void
1090 yornp_abort(void)
1092 exit_minibuffer();
1093 yornp_cb(0, yornp_data);
1096 static void
1097 read_self_insert(void)
1099 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1100 global_key_unbound();
1101 return;
1104 minibuffer_self_insert();
1107 static void
1108 read_abort(void)
1110 exit_minibuffer();
1111 read_cb(NULL, read_data);
1114 static void
1115 read_select(void)
1117 exit_minibuffer();
1118 minibuffer_hist_save_entry();
1119 read_cb(ministate.buf, read_data);
1122 static struct vline *
1123 nth_line(struct buffer *buffer, size_t n)
1125 struct vline *vl;
1126 size_t i;
1128 i = 0;
1129 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1130 if (i == n)
1131 return vl;
1132 i++;
1135 /* unreachable */
1136 abort();
1139 static struct tab *
1140 current_tab(void)
1142 struct tab *t;
1144 TAILQ_FOREACH(t, &tabshead, tabs) {
1145 if (t->flags & TAB_CURRENT)
1146 return t;
1149 /* unreachable */
1150 abort();
1153 static struct buffer *
1154 current_buffer(void)
1156 if (in_minibuffer)
1157 return &ministate.buffer;
1158 return &current_tab()->buffer;
1161 static int
1162 readkey(void)
1164 uint32_t state = 0;
1166 if ((thiskey.key = wgetch(body)) == ERR)
1167 return 0;
1169 thiskey.meta = thiskey.key == 27;
1170 if (thiskey.meta) {
1171 thiskey.key = wgetch(body);
1172 if (thiskey.key == ERR || thiskey.key == 27) {
1173 thiskey.meta = 0;
1174 thiskey.key = 27;
1178 thiskey.cp = 0;
1179 if ((unsigned int)thiskey.key < UINT8_MAX) {
1180 while (1) {
1181 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1182 break;
1183 if ((thiskey.key = wgetch(body)) == ERR) {
1184 message("Error decoding user input");
1185 return 0;
1190 return 1;
1193 static void
1194 dispatch_stdio(int fd, short ev, void *d)
1196 struct keymap *k;
1197 const char *keyname;
1198 char tmp[5] = {0};
1200 if (!readkey())
1201 return;
1203 if (keybuf[0] != '\0')
1204 strlcat(keybuf, " ", sizeof(keybuf));
1205 if (thiskey.meta)
1206 strlcat(keybuf, "M-", sizeof(keybuf));
1207 if (thiskey.cp != 0) {
1208 utf8_encode(thiskey.cp, tmp);
1209 strlcat(keybuf, tmp, sizeof(keybuf));
1210 } else {
1211 if ((keyname = unkbd(thiskey.key)) != NULL)
1212 strlcat(keybuf, keyname, sizeof(keybuf));
1213 else {
1214 tmp[0] = thiskey.key;
1215 strlcat(keybuf, tmp, sizeof(keybuf));
1219 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1220 if (k->meta == thiskey.meta &&
1221 k->key == thiskey.key) {
1222 if (k->fn == NULL)
1223 current_map = &k->map;
1224 else {
1225 current_map = base_map;
1226 strlcpy(keybuf, "", sizeof(keybuf));
1227 k->fn(current_buffer());
1229 goto done;
1233 if (current_map->unhandled_input != NULL)
1234 current_map->unhandled_input();
1235 else {
1236 global_key_unbound();
1239 strlcpy(keybuf, "", sizeof(keybuf));
1240 current_map = base_map;
1242 done:
1243 if (side_window)
1244 recompute_help();
1246 redraw_tab(current_tab());
1249 static void
1250 handle_clear_minibuf(int fd, short ev, void *d)
1252 free(ministate.curmesg);
1253 ministate.curmesg = NULL;
1255 redraw_minibuffer();
1256 if (in_minibuffer) {
1257 wrefresh(body);
1258 wrefresh(minibuf);
1259 } else {
1260 wrefresh(minibuf);
1261 wrefresh(body);
1265 static void
1266 handle_resize(int sig, short ev, void *d)
1268 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1269 event_del(&resizeev);
1271 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1272 evtimer_add(&resizeev, &resize_timer);
1275 static void
1276 handle_resize_nodelay(int s, short ev, void *d)
1278 struct tab *tab;
1280 endwin();
1281 refresh();
1282 clear();
1284 /* move and resize the windows, in reverse order! */
1286 mvwin(minibuf, LINES-1, 0);
1287 wresize(minibuf, 1, COLS);
1289 mvwin(modeline, LINES-2, 0);
1290 wresize(modeline, 1, COLS);
1292 body_lines = LINES-3;
1293 body_cols = COLS;
1295 if (side_window) {
1296 help_cols = 0.3 * COLS;
1297 help_lines = LINES-3;
1298 mvwin(help, 1, 0);
1299 wresize(help, help_lines, help_cols);
1301 wrap_page(&helpwin, help_cols);
1303 body_cols = COLS - help_cols - 1;
1304 mvwin(body, 1, help_cols);
1305 } else
1306 mvwin(body, 1, 0);
1308 wresize(body, body_lines, body_cols);
1310 wresize(tabline, 1, COLS);
1312 tab = current_tab();
1314 wrap_page(&tab->buffer, body_cols);
1315 redraw_tab(tab);
1318 static int
1319 wrap_page(struct buffer *buffer, int width)
1321 struct line *l;
1322 const struct line *orig;
1323 struct vline *vl;
1324 const char *prfx;
1326 orig = buffer->current_line == NULL
1327 ? NULL
1328 : buffer->current_line->parent;
1329 buffer->current_line = NULL;
1331 buffer->curs_y = 0;
1332 buffer->line_off = 0;
1334 empty_vlist(buffer);
1336 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1337 prfx = line_prefixes[l->type].prfx1;
1338 switch (l->type) {
1339 case LINE_TEXT:
1340 case LINE_LINK:
1341 case LINE_TITLE_1:
1342 case LINE_TITLE_2:
1343 case LINE_TITLE_3:
1344 case LINE_ITEM:
1345 case LINE_QUOTE:
1346 case LINE_PRE_START:
1347 case LINE_PRE_END:
1348 wrap_text(buffer, prfx, l, width);
1349 break;
1350 case LINE_PRE_CONTENT:
1351 hardwrap_text(buffer, l, width);
1352 break;
1355 if (orig == l && buffer->current_line == NULL) {
1356 buffer->line_off = buffer->line_max-1;
1357 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1359 while (1) {
1360 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1361 if (vl == NULL || vl->parent != orig)
1362 break;
1363 buffer->current_line = vl;
1364 buffer->line_off--;
1369 if (buffer->current_line == NULL)
1370 buffer->current_line = TAILQ_FIRST(&buffer->head);
1372 return 1;
1375 static void
1376 print_vline(WINDOW *window, struct vline *vl)
1378 const char *text = vl->line;
1379 const char *prfx;
1380 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1381 int text_face = line_faces[vl->parent->type].text_prop;
1383 if (!vl->flags)
1384 prfx = line_prefixes[vl->parent->type].prfx1;
1385 else
1386 prfx = line_prefixes[vl->parent->type].prfx2;
1388 if (text == NULL)
1389 text = "";
1391 wattron(window, prefix_face);
1392 wprintw(window, "%s", prfx);
1393 wattroff(window, prefix_face);
1395 wattron(window, text_face);
1396 wprintw(window, "%s", text);
1397 wattroff(window, text_face);
1400 static void
1401 redraw_tabline(void)
1403 struct tab *tab;
1404 size_t toskip, ots, tabwidth, space, x;
1405 int current, y, truncated;
1406 const char *title;
1407 char buf[25];
1409 tabwidth = sizeof(buf)+1;
1410 space = COLS-2;
1412 toskip = 0;
1413 TAILQ_FOREACH(tab, &tabshead, tabs) {
1414 toskip++;
1415 if (tab->flags & TAB_CURRENT)
1416 break;
1419 if (toskip * tabwidth < space)
1420 toskip = 0;
1421 else {
1422 ots = toskip;
1423 toskip--;
1424 while (toskip != 0 &&
1425 (ots - toskip+1) * tabwidth < space)
1426 toskip--;
1429 werase(tabline);
1430 wattron(tabline, tab_face.background);
1431 wprintw(tabline, toskip == 0 ? " " : "<");
1432 wattroff(tabline, tab_face.background);
1434 truncated = 0;
1435 TAILQ_FOREACH(tab, &tabshead, tabs) {
1436 if (truncated)
1437 break;
1438 if (toskip != 0) {
1439 toskip--;
1440 continue;
1443 getyx(tabline, y, x);
1444 if (x + sizeof(buf)+2 >= (size_t)COLS)
1445 truncated = 1;
1447 current = tab->flags & TAB_CURRENT;
1449 if (*(title = tab->buffer.page.title) == '\0')
1450 title = tab->hist_cur->h;
1452 if (tab->flags & TAB_URGENT)
1453 strlcpy(buf, "!", sizeof(buf));
1454 else
1455 strlcpy(buf, " ", sizeof(buf));
1457 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1458 /* truncation happens */
1459 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1460 } else {
1461 /* pad with spaces */
1462 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1463 /* nop */ ;
1466 if (current)
1467 wattron(tabline, tab_face.current_tab);
1468 else
1469 wattron(tabline, tab_face.tab);
1471 wprintw(tabline, "%s", buf);
1472 if (TAILQ_NEXT(tab, tabs) != NULL)
1473 wprintw(tabline, " ");
1475 if (current)
1476 wattroff(tabline, tab_face.current_tab);
1477 else
1478 wattroff(tabline, tab_face.tab);
1481 wattron(tabline, tab_face.background);
1482 for (; x < (size_t)COLS; ++x)
1483 waddch(tabline, ' ');
1484 if (truncated)
1485 mvwprintw(tabline, 0, COLS-1, ">");
1488 static void
1489 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1491 struct vline *vl;
1492 int l;
1494 werase(win);
1496 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1497 if (TAILQ_EMPTY(&buffer->head))
1498 return;
1500 l = 0;
1501 vl = nth_line(buffer, buffer->line_off);
1502 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1503 wmove(win, l, 0);
1504 print_vline(win, vl);
1505 l++;
1506 if (l == height)
1507 break;
1510 wmove(win, buffer->curs_y, buffer->curs_x);
1513 static void
1514 redraw_help(void)
1516 redraw_window(help, help_lines, &helpwin);
1519 static void
1520 redraw_body(struct tab *tab)
1522 redraw_window(body, body_lines, &tab->buffer);
1525 static inline char
1526 trust_status_char(enum trust_state ts)
1528 switch (ts) {
1529 case TS_UNKNOWN: return 'u';
1530 case TS_UNTRUSTED: return '!';
1531 case TS_TRUSTED: return 'v';
1532 case TS_VERIFIED: return 'V';
1536 static void
1537 redraw_modeline(struct tab *tab)
1539 double pct;
1540 int x, y, max_x, max_y;
1541 const char *mode = tab->buffer.page.name;
1542 const char *spin = "-\\|/";
1544 werase(modeline);
1545 wattron(modeline, A_REVERSE);
1546 wmove(modeline, 0, 0);
1548 wprintw(modeline, "-%c%c %s ",
1549 spin[tab->loading_anim_step],
1550 trust_status_char(tab->trust),
1551 mode == NULL ? "(none)" : mode);
1553 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1555 if (tab->buffer.line_max <= (size_t)body_lines)
1556 wprintw(modeline, "All ");
1557 else if (tab->buffer.line_off == 0)
1558 wprintw(modeline, "Top ");
1559 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1560 wprintw(modeline, "Bottom ");
1561 else
1562 wprintw(modeline, "%.0f%% ", pct);
1564 wprintw(modeline, "%d/%d %s ",
1565 tab->buffer.line_off + tab->buffer.curs_y,
1566 tab->buffer.line_max,
1567 tab->hist_cur->h);
1569 getyx(modeline, y, x);
1570 getmaxyx(modeline, max_y, max_x);
1572 (void)y;
1573 (void)max_y;
1575 for (; x < max_x; ++x)
1576 waddstr(modeline, "-");
1579 static void
1580 redraw_minibuffer(void)
1582 struct tab *tab;
1583 size_t off_y, off_x = 0;
1584 char *start, *c;
1586 werase(minibuf);
1588 if (in_minibuffer) {
1589 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1590 if (ministate.hist_cur != NULL)
1591 wprintw(minibuf, "(%zu/%zu) ",
1592 ministate.hist_off + 1,
1593 ministate.history->len);
1595 getyx(minibuf, off_y, off_x);
1597 start = ministate.hist_cur != NULL
1598 ? ministate.hist_cur->h
1599 : ministate.buf;
1600 c = utf8_nth(ministate.buffer.current_line->line,
1601 ministate.buffer.cpoff);
1602 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1603 start = utf8_next_cp(start);
1606 waddstr(minibuf, start);
1609 if (ministate.curmesg != NULL)
1610 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1611 ministate.curmesg);
1613 if (!in_minibuffer && ministate.curmesg == NULL)
1614 waddstr(minibuf, keybuf);
1616 /* If nothing else, show the URL at point */
1617 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1618 tab = current_tab();
1619 if (tab->buffer.current_line != NULL &&
1620 tab->buffer.current_line->parent->type == LINE_LINK)
1621 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1624 if (in_minibuffer)
1625 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1628 static void
1629 redraw_tab(struct tab *tab)
1631 if (side_window) {
1632 redraw_help();
1633 wnoutrefresh(help);
1636 redraw_tabline();
1637 redraw_body(tab);
1638 redraw_modeline(tab);
1639 redraw_minibuffer();
1641 wnoutrefresh(tabline);
1642 wnoutrefresh(modeline);
1644 if (in_minibuffer) {
1645 wnoutrefresh(body);
1646 wnoutrefresh(minibuf);
1647 } else {
1648 wnoutrefresh(minibuf);
1649 wnoutrefresh(body);
1652 doupdate();
1655 static void
1656 emit_help_item(char *prfx, void *fn)
1658 struct line *l;
1659 struct cmds *cmd;
1661 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1662 if (fn == cmd->fn)
1663 break;
1665 assert(cmd != NULL);
1667 if ((l = calloc(1, sizeof(*l))) == NULL)
1668 abort();
1670 l->type = LINE_TEXT;
1671 l->alt = NULL;
1673 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1675 if (TAILQ_EMPTY(&helpwin.page.head))
1676 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1677 else
1678 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1681 static void
1682 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1684 struct keymap *k;
1685 char p[32];
1686 const char *kn;
1688 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1689 strlcpy(p, prfx, sizeof(p));
1690 if (*p != '\0')
1691 strlcat(p, " ", sizeof(p));
1692 if (k->meta)
1693 strlcat(p, "M-", sizeof(p));
1694 if ((kn = unkbd(k->key)) != NULL)
1695 strlcat(p, kn, sizeof(p));
1696 else
1697 strlcat(p, keyname(k->key), sizeof(p));
1699 if (k->fn == NULL)
1700 rec_compute_help(&k->map, p, sizeof(p));
1701 else
1702 emit_help_item(p, k->fn);
1706 static void
1707 recompute_help(void)
1709 char p[32] = { 0 };
1711 empty_vlist(&helpwin);
1712 empty_linelist(&helpwin);
1713 rec_compute_help(current_map, p, sizeof(p));
1714 wrap_page(&helpwin, help_cols);
1717 static void
1718 vmessage(const char *fmt, va_list ap)
1720 if (evtimer_pending(&clminibufev, NULL))
1721 evtimer_del(&clminibufev);
1722 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1723 evtimer_add(&clminibufev, &clminibufev_timer);
1725 free(ministate.curmesg);
1727 /* TODO: what to do if the allocation fails here? */
1728 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1729 ministate.curmesg = NULL;
1731 redraw_minibuffer();
1732 if (in_minibuffer) {
1733 wrefresh(body);
1734 wrefresh(minibuf);
1735 } else {
1736 wrefresh(minibuf);
1737 wrefresh(body);
1741 static void
1742 message(const char *fmt, ...)
1744 va_list ap;
1746 va_start(ap, fmt);
1747 vmessage(fmt, ap);
1748 va_end(ap);
1751 static void
1752 start_loading_anim(struct tab *tab)
1754 if (tab->loading_anim)
1755 return;
1756 tab->loading_anim = 1;
1757 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1758 evtimer_add(&tab->loadingev, &loadingev_timer);
1761 static void
1762 update_loading_anim(int fd, short ev, void *d)
1764 struct tab *tab = d;
1766 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1768 if (tab->flags & TAB_CURRENT) {
1769 redraw_modeline(tab);
1770 wrefresh(modeline);
1771 wrefresh(body);
1772 if (in_minibuffer)
1773 wrefresh(minibuf);
1776 evtimer_add(&tab->loadingev, &loadingev_timer);
1779 static void
1780 stop_loading_anim(struct tab *tab)
1782 if (!tab->loading_anim)
1783 return;
1784 evtimer_del(&tab->loadingev);
1785 tab->loading_anim = 0;
1786 tab->loading_anim_step = 0;
1788 if (!(tab->flags & TAB_CURRENT))
1789 return;
1791 redraw_modeline(tab);
1793 wrefresh(modeline);
1794 wrefresh(body);
1795 if (in_minibuffer)
1796 wrefresh(minibuf);
1799 static void
1800 load_url_in_tab(struct tab *tab, const char *url)
1802 message("Loading %s...", url);
1803 start_loading_anim(tab);
1804 load_url(tab, url);
1806 tab->buffer.curs_x = 0;
1807 tab->buffer.curs_y = 0;
1808 redraw_tab(tab);
1811 static void
1812 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1813 void (*abortfn)(void), struct histhead *hist)
1815 in_minibuffer = 1;
1816 base_map = &minibuffer_map;
1817 current_map = &minibuffer_map;
1819 base_map->unhandled_input = self_insert_fn;
1821 ministate.donefn = donefn;
1822 ministate.abortfn = abortfn;
1823 memset(ministate.buf, 0, sizeof(ministate.buf));
1824 ministate.buffer.current_line = &ministate.vline;
1825 ministate.buffer.current_line->line = ministate.buf;
1826 ministate.buffer.cpoff = 0;
1827 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1829 ministate.history = hist;
1830 ministate.hist_cur = NULL;
1831 ministate.hist_off = 0;
1834 static void
1835 exit_minibuffer(void)
1837 werase(minibuf);
1839 in_minibuffer = 0;
1840 base_map = &global_map;
1841 current_map = &global_map;
1844 static void
1845 switch_to_tab(struct tab *tab)
1847 struct tab *t;
1849 TAILQ_FOREACH(t, &tabshead, tabs) {
1850 t->flags &= ~TAB_CURRENT;
1853 tab->flags |= TAB_CURRENT;
1854 tab->flags &= ~TAB_URGENT;
1857 unsigned int
1858 tab_new_id(void)
1860 return tab_counter++;
1863 static struct tab *
1864 new_tab(const char *url)
1866 struct tab *tab;
1868 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1869 event_loopbreak();
1870 return NULL;
1872 tab->fd = -1;
1874 TAILQ_INIT(&tab->hist.head);
1876 TAILQ_INIT(&tab->buffer.head);
1878 tab->id = tab_new_id();
1879 switch_to_tab(tab);
1881 if (TAILQ_EMPTY(&tabshead))
1882 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1883 else
1884 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1886 load_url_in_tab(tab, url);
1887 return tab;
1890 static void
1891 session_new_tab_cb(const char *url)
1893 new_tab(url);
1896 static void
1897 usage(void)
1899 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1900 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1903 int
1904 ui_init(int argc, char * const *argv)
1906 char path[PATH_MAX];
1907 const char *url = NEW_TAB_URL;
1908 int ch, configtest = 0, fonf = 0;
1910 strlcpy(path, getenv("HOME"), sizeof(path));
1911 strlcat(path, "/.telescope/config", sizeof(path));
1913 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1914 switch (ch) {
1915 case 'c':
1916 fonf = 1;
1917 strlcpy(path, optarg, sizeof(path));
1918 break;
1919 case 'n':
1920 configtest = 1;
1921 break;
1922 case 'h':
1923 usage();
1924 return 0;
1925 default:
1926 usage();
1927 return 1;
1930 argc -= optind;
1931 argv += optind;
1933 parseconfig(path, fonf);
1934 if (configtest){
1935 puts("config OK");
1936 exit(0);
1939 if (argc != 0)
1940 url = argv[0];
1942 setlocale(LC_ALL, "");
1944 TAILQ_INIT(&global_map.m);
1945 global_map.unhandled_input = global_key_unbound;
1947 TAILQ_INIT(&minibuffer_map.m);
1949 TAILQ_INIT(&eecmd_history.head);
1950 TAILQ_INIT(&ir_history.head);
1951 TAILQ_INIT(&lu_history.head);
1953 ministate.line.type = LINE_TEXT;
1954 ministate.vline.parent = &ministate.line;
1955 ministate.buffer.current_line = &ministate.vline;
1957 /* initialize help window */
1958 TAILQ_INIT(&helpwin.head);
1960 base_map = &global_map;
1961 current_map = &global_map;
1962 load_default_keys();
1964 initscr();
1965 raw();
1966 noecho();
1968 nonl();
1969 intrflush(stdscr, FALSE);
1971 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1972 return 0;
1973 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1974 return 0;
1975 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1976 return 0;
1977 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1978 return 0;
1979 if ((help = newwin(1, 1, 1, 0)) == NULL)
1980 return 0;
1982 body_lines = LINES-3;
1983 body_cols = COLS;
1985 keypad(body, TRUE);
1986 scrollok(body, TRUE);
1988 /* non-blocking input */
1989 wtimeout(body, 0);
1991 mvwprintw(body, 0, 0, "");
1993 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1994 event_add(&stdioev, NULL);
1996 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1997 signal_add(&winchev, NULL);
1999 load_last_session(session_new_tab_cb);
2000 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2001 new_tab(url);
2003 return 1;
2006 void
2007 ui_on_tab_loaded(struct tab *tab)
2009 stop_loading_anim(tab);
2010 message("Loaded %s", tab->hist_cur->h);
2012 redraw_tabline();
2013 wrefresh(tabline);
2014 if (in_minibuffer)
2015 wrefresh(minibuf);
2016 else
2017 wrefresh(body);
2020 void
2021 ui_on_tab_refresh(struct tab *tab)
2023 wrap_page(&tab->buffer, body_cols);
2024 if (tab->flags & TAB_CURRENT) {
2025 restore_cursor(&tab->buffer);
2026 redraw_tab(tab);
2027 } else
2028 tab->flags |= TAB_URGENT;
2031 void
2032 ui_require_input(struct tab *tab, int hide)
2034 /* TODO: hard-switching to another tab is ugly */
2035 switch_to_tab(tab);
2037 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2038 &ir_history);
2039 strlcpy(ministate.prompt, "Input required: ",
2040 sizeof(ministate.prompt));
2041 redraw_tab(tab);
2044 void
2045 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2046 unsigned int data)
2048 size_t len;
2050 if (in_minibuffer) {
2051 fn(0, data);
2052 return;
2055 yornp_cb = fn;
2056 yornp_data = data;
2057 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2058 yornp_abort, NULL);
2060 len = sizeof(ministate.prompt);
2061 strlcpy(ministate.prompt, prompt, len);
2062 strlcat(ministate.prompt, " (y or n) ", len);
2063 redraw_tab(current_tab());
2066 void
2067 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2068 unsigned int data)
2070 size_t len;
2072 if (in_minibuffer)
2073 return;
2075 read_cb = fn;
2076 read_data = data;
2077 enter_minibuffer(read_self_insert, read_select, read_abort,
2078 &read_history);
2080 len = sizeof(ministate.prompt);
2081 strlcpy(ministate.prompt, prompt, len);
2082 strlcat(ministate.prompt, ": ", len);
2083 redraw_tab(current_tab());
2086 void
2087 ui_notify(const char *fmt, ...)
2089 va_list ap;
2091 va_start(ap, fmt);
2092 vmessage(fmt, ap);
2093 va_end(ap);
2096 void
2097 ui_end(void)
2099 endwin();