Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * Ncurses UI for telescope.
19 *
20 * Text scrolling
21 * ==============
22 *
23 * ncurses allows you to scroll a window, but when a line goes out of
24 * the visible area it's forgotten. We keep a list of formatted lines
25 * (``visual lines'') that we know fits in the window, and draw them.
26 * This way is easy to scroll: just call wscrl and then render the
27 * first/last line!
28 *
29 * This means that on every resize we have to clear our list of lines
30 * and re-render everything. A clever approach would be to do this
31 * ``on-demand'', but it's still missing.
32 *
33 */
35 #include "telescope.h"
36 #include "cmd.gen.h"
38 #include <assert.h>
39 #include <curses.h>
40 #include <event.h>
41 #include <limits.h>
42 #include <locale.h>
43 #include <signal.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #define TAB_CURRENT 0x1
50 #define TAB_URGENT 0x2
52 #define NEW_TAB_URL "about:new"
54 static struct event stdioev, winchev;
56 static void load_default_keys(void);
57 static void restore_cursor(struct buffer*);
59 static void global_key_unbound(void);
60 static void minibuffer_hist_save_entry(void);
61 static void minibuffer_taint_hist(void);
62 static void minibuffer_self_insert(void);
63 static void eecmd_self_insert(void);
64 static void eecmd_select(void);
65 static void ir_self_insert(void);
66 static void ir_select(void);
67 static void lu_self_insert(void);
68 static void lu_select(void);
69 static void bp_select(void);
70 static void yornp_self_insert(void);
71 static void yornp_abort(void);
72 static void read_self_insert(void);
73 static void read_abort(void);
74 static void read_select(void);
76 static struct vline *nth_line(struct buffer*, size_t);
77 static struct tab *current_tab(void);
78 static struct buffer *current_buffer(void);
79 static int readkey(void);
80 static void dispatch_stdio(int, short, void*);
81 static void handle_clear_minibuf(int, short, void*);
82 static void handle_resize(int, short, void*);
83 static void handle_resize_nodelay(int, short, void*);
84 static int wrap_page(struct buffer*, int);
85 static void print_vline(WINDOW*, struct vline*);
86 static void redraw_tabline(void);
87 static void redraw_window(WINDOW*, int, struct buffer*);
88 static void redraw_help(void);
89 static void redraw_body(struct tab*);
90 static void redraw_modeline(struct tab*);
91 static void redraw_minibuffer(void);
92 static void redraw_tab(struct tab*);
93 static void emit_help_item(char*, void*);
94 static void rec_compute_help(struct kmap*, char*, size_t);
95 static void recompute_help(void);
96 static void vmessage(const char*, va_list);
97 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
98 static void start_loading_anim(struct tab*);
99 static void update_loading_anim(int, short, void*);
100 static void stop_loading_anim(struct tab*);
101 static void load_url_in_tab(struct tab*, const char*);
102 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
103 static void exit_minibuffer(void);
104 static void switch_to_tab(struct tab*);
105 static struct tab *new_tab(const char*);
106 static void session_new_tab_cb(const char*);
107 static void usage(void);
109 static int x_offset;
111 static struct { short meta; int key; uint32_t cp; } thiskey;
113 static struct event resizeev;
114 static struct timeval resize_timer = { 0, 250000 };
116 static WINDOW *tabline, *body, *modeline, *minibuf;
117 static int body_lines, body_cols;
119 static WINDOW *help;
120 static struct buffer helpwin;
121 static int help_lines, help_cols;
123 static int side_window;
125 static struct event clminibufev;
126 static struct timeval clminibufev_timer = { 5, 0 };
127 static struct timeval loadingev_timer = { 0, 250000 };
129 static uint32_t tab_counter;
131 static char keybuf[64];
133 static void (*yornp_cb)(int, unsigned int);
134 static unsigned int yornp_data;
136 static void (*read_cb)(const char*, unsigned int);
137 static unsigned int read_data;
139 struct kmap global_map,
140 minibuffer_map,
141 *current_map,
142 *base_map;
144 static struct histhead eecmd_history,
145 ir_history,
146 lu_history,
147 read_history;
149 static int in_minibuffer;
151 static struct {
152 char *curmesg;
154 char prompt[64];
155 void (*donefn)(void);
156 void (*abortfn)(void);
158 char buf[1025];
159 struct line line;
160 struct vline vline;
161 struct buffer buffer;
163 struct histhead *history;
164 struct hist *hist_cur;
165 size_t hist_off;
166 } ministate;
168 static inline void
169 update_x_offset()
171 if (olivetti_mode && fill_column < body_cols)
172 x_offset = (body_cols - fill_column)/2;
173 else
174 x_offset = 0;
177 static inline void
178 global_set_key(const char *key, void (*fn)(struct buffer*))
180 if (!kmap_define_key(&global_map, key, fn))
181 _exit(1);
184 static inline void
185 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
187 if (!kmap_define_key(&minibuffer_map, key, fn))
188 _exit(1);
191 static void
192 load_default_keys(void)
194 /* === global map === */
196 /* emacs */
197 global_set_key("C-p", cmd_previous_line);
198 global_set_key("C-n", cmd_next_line);
199 global_set_key("C-f", cmd_forward_char);
200 global_set_key("C-b", cmd_backward_char);
201 global_set_key("M-{", cmd_backward_paragraph);
202 global_set_key("M-}", cmd_forward_paragraph);
203 global_set_key("C-a", cmd_move_beginning_of_line);
204 global_set_key("C-e", cmd_move_end_of_line);
206 global_set_key("M-v", cmd_scroll_up);
207 global_set_key("C-v", cmd_scroll_down);
208 global_set_key("M-space", cmd_scroll_up);
209 global_set_key("space", cmd_scroll_down);
211 global_set_key("M-<", cmd_beginning_of_buffer);
212 global_set_key("M->", cmd_end_of_buffer);
214 global_set_key("C-x C-c", cmd_kill_telescope);
216 global_set_key("C-g", cmd_clear_minibuf);
218 global_set_key("M-x", cmd_execute_extended_command);
219 global_set_key("C-x C-f", cmd_load_url);
220 global_set_key("C-x M-f", cmd_load_current_url);
222 global_set_key("C-x t 0", cmd_tab_close);
223 global_set_key("C-x t 1", cmd_tab_close_other);
224 global_set_key("C-x t 2", cmd_tab_new);
225 global_set_key("C-x t o", cmd_tab_next);
226 global_set_key("C-x t O", cmd_tab_previous);
227 global_set_key("C-x t m", cmd_tab_move);
228 global_set_key("C-x t M", cmd_tab_move_to);
230 global_set_key("C-M-b", cmd_previous_page);
231 global_set_key("C-M-f", cmd_next_page);
233 global_set_key("<f7> a", cmd_bookmark_page);
234 global_set_key("<f7> <f7>", cmd_list_bookmarks);
236 /* vi/vi-like */
237 global_set_key("k", cmd_previous_line);
238 global_set_key("j", cmd_next_line);
239 global_set_key("l", cmd_forward_char);
240 global_set_key("h", cmd_backward_char);
241 global_set_key("{", cmd_backward_paragraph);
242 global_set_key("}", cmd_forward_paragraph);
243 global_set_key("^", cmd_move_beginning_of_line);
244 global_set_key("$", cmd_move_end_of_line);
246 global_set_key("K", cmd_scroll_line_up);
247 global_set_key("J", cmd_scroll_line_down);
249 global_set_key("g g", cmd_beginning_of_buffer);
250 global_set_key("G", cmd_end_of_buffer);
252 global_set_key("g D", cmd_tab_close);
253 global_set_key("g N", cmd_tab_new);
254 global_set_key("g t", cmd_tab_next);
255 global_set_key("g T", cmd_tab_previous);
256 global_set_key("g M-t", cmd_tab_move);
257 global_set_key("g M-T", cmd_tab_move_to);
259 global_set_key("H", cmd_previous_page);
260 global_set_key("L", cmd_next_page);
262 /* tmp */
263 global_set_key("q", cmd_kill_telescope);
265 global_set_key("esc", cmd_clear_minibuf);
267 global_set_key(":", cmd_execute_extended_command);
269 /* cua */
270 global_set_key("<up>", cmd_previous_line);
271 global_set_key("<down>", cmd_next_line);
272 global_set_key("<right>", cmd_forward_char);
273 global_set_key("<left>", cmd_backward_char);
274 global_set_key("<prior>", cmd_scroll_up);
275 global_set_key("<next>", cmd_scroll_down);
277 global_set_key("M-<left>", cmd_previous_page);
278 global_set_key("M-<right>", cmd_next_page);
280 /* "ncurses standard" */
281 global_set_key("C-l", cmd_redraw);
283 /* global */
284 global_set_key("<f1>", cmd_toggle_help);
285 global_set_key("C-m", cmd_push_button);
286 global_set_key("M-enter", cmd_push_button_new_tab);
287 global_set_key("M-tab", cmd_previous_button);
288 global_set_key("backtab", cmd_previous_button);
289 global_set_key("tab", cmd_next_button);
291 /* === minibuffer map === */
292 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
293 minibuffer_set_key("C-g", cmd_mini_abort);
294 minibuffer_set_key("esc", cmd_mini_abort);
295 minibuffer_set_key("C-d", cmd_mini_delete_char);
296 minibuffer_set_key("del", cmd_mini_delete_backward_char);
297 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
298 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
300 minibuffer_set_key("C-b", cmd_backward_char);
301 minibuffer_set_key("C-f", cmd_forward_char);
302 minibuffer_set_key("<left>", cmd_backward_char);
303 minibuffer_set_key("<right>", cmd_forward_char);
304 minibuffer_set_key("C-e", cmd_move_end_of_line);
305 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
306 minibuffer_set_key("<end>", cmd_move_end_of_line);
307 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
308 minibuffer_set_key("C-k", cmd_mini_kill_line);
310 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
311 minibuffer_set_key("M-n", cmd_mini_next_history_element);
312 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
313 minibuffer_set_key("<down>", cmd_mini_next_history_element);
316 static void
317 restore_cursor(struct buffer *buffer)
319 struct vline *vl;
320 const char *prfx;
322 vl = buffer->current_line;
323 if (vl == NULL || vl->line == NULL)
324 buffer->curs_x = buffer->cpoff = 0;
325 else
326 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
328 buffer->curs_x += x_offset;
330 if (vl != NULL) {
331 prfx = line_prefixes[vl->parent->type].prfx1;
332 buffer->curs_x += utf8_swidth(prfx);
336 void
337 cmd_previous_line(struct buffer *buffer)
339 struct vline *vl;
341 if (buffer->current_line == NULL
342 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
343 return;
345 if (--buffer->curs_y < 0) {
346 buffer->curs_y = 0;
347 cmd_scroll_line_up(buffer);
348 return;
351 buffer->current_line = vl;
352 restore_cursor(buffer);
355 void
356 cmd_next_line(struct buffer *buffer)
358 struct vline *vl;
360 if (buffer->current_line == NULL
361 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
362 return;
364 if (++buffer->curs_y > body_lines-1) {
365 buffer->curs_y = body_lines-1;
366 cmd_scroll_line_down(buffer);
367 return;
370 buffer->current_line = vl;
371 restore_cursor(buffer);
374 void
375 cmd_backward_char(struct buffer *buffer)
377 if (buffer->cpoff != 0)
378 buffer->cpoff--;
379 restore_cursor(buffer);
382 void
383 cmd_forward_char(struct buffer *buffer)
385 size_t len = 0;
387 if (buffer->current_line->line != NULL)
388 len = utf8_cplen(buffer->current_line->line);
389 if (++buffer->cpoff > len)
390 buffer->cpoff = len;
391 restore_cursor(buffer);
394 void
395 cmd_backward_paragraph(struct buffer *buffer)
397 do {
398 if (buffer->current_line == NULL ||
399 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
400 message("No previous paragraph");
401 return;
403 cmd_previous_line(buffer);
404 } while (buffer->current_line->line != NULL ||
405 buffer->current_line->parent->type != LINE_TEXT);
408 void
409 cmd_forward_paragraph(struct buffer *buffer)
411 do {
412 if (buffer->current_line == NULL ||
413 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
414 message("No next paragraph");
415 return;
417 cmd_next_line(buffer);
418 } while (buffer->current_line->line != NULL ||
419 buffer->current_line->parent->type != LINE_TEXT);
422 void
423 cmd_move_beginning_of_line(struct buffer *buffer)
425 buffer->cpoff = 0;
426 restore_cursor(buffer);
429 void
430 cmd_move_end_of_line(struct buffer *buffer)
432 struct vline *vl;
434 vl = buffer->current_line;
435 if (vl->line == NULL)
436 return;
437 buffer->cpoff = utf8_cplen(vl->line);
438 restore_cursor(buffer);
441 void
442 cmd_redraw(struct buffer *buffer)
444 handle_resize(0, 0, NULL);
447 void
448 cmd_scroll_line_up(struct buffer *buffer)
450 struct vline *vl;
452 if (buffer->line_off == 0)
453 return;
455 vl = nth_line(buffer, --buffer->line_off);
456 wscrl(body, -1);
457 wmove(body, 0, 0);
458 print_vline(body, vl);
460 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
461 restore_cursor(buffer);
464 void
465 cmd_scroll_line_down(struct buffer *buffer)
467 struct vline *vl;
469 vl = buffer->current_line;
470 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
471 return;
472 buffer->current_line = vl;
474 buffer->line_off++;
475 wscrl(body, 1);
477 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
478 return;
480 vl = nth_line(buffer, buffer->line_off + body_lines-1);
481 wmove(body, body_lines-1, 0);
482 print_vline(body, vl);
484 restore_cursor(buffer);
487 void
488 cmd_scroll_up(struct buffer *buffer)
490 size_t off;
492 off = body_lines-1;
494 for (; off > 0; --off)
495 cmd_scroll_line_up(buffer);
498 void
499 cmd_scroll_down(struct buffer *buffer)
501 size_t off;
503 off = body_lines-1;
505 for (; off > 0; --off)
506 cmd_scroll_line_down(buffer);
509 void
510 cmd_beginning_of_buffer(struct buffer *buffer)
512 buffer->current_line = TAILQ_FIRST(&buffer->head);
513 buffer->line_off = 0;
514 buffer->curs_y = 0;
515 buffer->cpoff = 0;
516 restore_cursor(buffer);
519 void
520 cmd_end_of_buffer(struct buffer *buffer)
522 ssize_t off;
524 off = buffer->line_max - body_lines;
525 off = MAX(0, off);
527 buffer->line_off = off;
528 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
530 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
531 buffer->cpoff = body_cols;
532 restore_cursor(buffer);
535 void
536 cmd_kill_telescope(struct buffer *buffer)
538 save_session();
539 event_loopbreak();
542 void
543 cmd_push_button(struct buffer *buffer)
545 struct vline *vl;
546 size_t nth;
548 nth = buffer->line_off + buffer->curs_y;
549 if (nth >= buffer->line_max)
550 return;
551 vl = nth_line(buffer, nth);
552 if (vl->parent->type != LINE_LINK)
553 return;
555 load_url_in_tab(current_tab(), vl->parent->alt);
558 void
559 cmd_push_button_new_tab(struct buffer *buffer)
561 struct vline *vl;
562 size_t nth;
564 nth = buffer->line_off + buffer->curs_y;
565 if (nth > buffer->line_max)
566 return;
567 vl = nth_line(buffer, nth);
568 if (vl->parent->type != LINE_LINK)
569 return;
571 new_tab(vl->parent->alt);
574 void
575 cmd_previous_button(struct buffer *buffer)
577 do {
578 if (buffer->current_line == NULL ||
579 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
580 message("No previous link");
581 return;
583 cmd_previous_line(buffer);
584 } while (buffer->current_line->parent->type != LINE_LINK);
587 void
588 cmd_next_button(struct buffer *buffer)
590 do {
591 if (buffer->current_line == NULL ||
592 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
593 message("No next link");
594 return;
596 cmd_next_line(buffer);
597 } while (buffer->current_line->parent->type != LINE_LINK);
600 void
601 cmd_previous_page(struct buffer *buffer)
603 struct tab *tab = current_tab();
605 if (!load_previous_page(tab))
606 message("No previous page");
607 else
608 start_loading_anim(tab);
611 void
612 cmd_next_page(struct buffer *buffer)
614 struct tab *tab = current_tab();
616 if (!load_next_page(tab))
617 message("No next page");
618 else
619 start_loading_anim(tab);
622 void
623 cmd_clear_minibuf(struct buffer *buffer)
625 handle_clear_minibuf(0, 0, NULL);
628 void
629 cmd_execute_extended_command(struct buffer *buffer)
631 size_t len;
633 if (in_minibuffer) {
634 message("We don't have enable-recursive-minibuffers");
635 return;
638 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
639 &eecmd_history);
641 len = sizeof(ministate.prompt);
642 strlcpy(ministate.prompt, "", len);
644 if (thiskey.meta)
645 strlcat(ministate.prompt, "M-", len);
647 strlcat(ministate.prompt, keyname(thiskey.key), len);
649 if (thiskey.meta)
650 strlcat(ministate.prompt, " ", len);
653 void
654 cmd_tab_close(struct buffer *buffer)
656 struct tab *tab, *t;
658 tab = current_tab();
659 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
660 TAILQ_NEXT(tab, tabs) == NULL) {
661 message("Can't close the only tab.");
662 return;
665 if (evtimer_pending(&tab->loadingev, NULL))
666 evtimer_del(&tab->loadingev);
668 stop_tab(tab);
670 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
671 t = TAILQ_NEXT(tab, tabs);
672 TAILQ_REMOVE(&tabshead, tab, tabs);
673 free(tab);
675 switch_to_tab(t);
678 void
679 cmd_tab_close_other(struct buffer *buffer)
681 struct tab *t, *i;
683 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
684 if (t->flags & TAB_CURRENT)
685 continue;
687 stop_tab(t);
688 TAILQ_REMOVE(&tabshead, t, tabs);
689 free(t);
693 void
694 cmd_tab_new(struct buffer *buffer)
696 const char *url;
698 if ((url = new_tab_url) == NULL)
699 url = NEW_TAB_URL;
701 new_tab(url);
704 void
705 cmd_tab_next(struct buffer *buffer)
707 struct tab *tab, *t;
709 tab = current_tab();
710 tab->flags &= ~TAB_CURRENT;
712 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
713 t = TAILQ_FIRST(&tabshead);
714 t->flags |= TAB_CURRENT;
715 t->flags &= ~TAB_URGENT;
718 void
719 cmd_tab_previous(struct buffer *buffer)
721 struct tab *tab, *t;
723 tab = current_tab();
724 tab->flags &= ~TAB_CURRENT;
726 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
727 t = TAILQ_LAST(&tabshead, tabshead);
728 t->flags |= TAB_CURRENT;
729 t->flags &= ~TAB_URGENT;
732 void
733 cmd_tab_move(struct buffer *buffer)
735 struct tab *tab, *t;
737 tab = current_tab();
738 t = TAILQ_NEXT(tab, tabs);
739 TAILQ_REMOVE(&tabshead, tab, tabs);
741 if (t == NULL)
742 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
743 else
744 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
747 void
748 cmd_tab_move_to(struct buffer *buffer)
750 struct tab *tab, *t;
752 tab = current_tab();
753 t = TAILQ_PREV(tab, tabshead, tabs);
754 TAILQ_REMOVE(&tabshead, tab, tabs);
756 if (t == NULL) {
757 if (TAILQ_EMPTY(&tabshead))
758 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
759 else
760 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
761 } else
762 TAILQ_INSERT_BEFORE(t, tab, tabs);
765 void
766 cmd_load_url(struct buffer *buffer)
768 if (in_minibuffer) {
769 message("We don't have enable-recursive-minibuffers");
770 return;
773 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
774 &lu_history);
775 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
776 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
777 cmd_move_end_of_line(&ministate.buffer);
780 void
781 cmd_load_current_url(struct buffer *buffer)
783 struct tab *tab = current_tab();
785 if (in_minibuffer) {
786 message("We don't have enable-recursive-minibuffers");
787 return;
790 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
791 &lu_history);
792 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
793 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
794 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
797 void
798 cmd_bookmark_page(struct buffer *buffer)
800 struct tab *tab = current_tab();
802 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
803 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
804 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
805 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
808 void
809 cmd_list_bookmarks(struct buffer *buffer)
811 load_url_in_tab(current_tab(), "about:bookmarks");
814 void
815 cmd_toggle_help(struct buffer *buffer)
817 side_window = !side_window;
818 if (side_window)
819 recompute_help();
821 /*
822 * ugly hack, but otherwise the window doesn't get updated
823 * until I call handle_resize a second time (i.e. C-l). I
824 * will be happy to know why something like this is needed.
825 */
826 handle_resize_nodelay(0, 0, NULL);
827 handle_resize_nodelay(0, 0, NULL);
830 void
831 cmd_olivetti_mode(struct buffer *buffer)
833 olivetti_mode = !olivetti_mode;
834 if (olivetti_mode)
835 message("olivetti-mode enabled");
836 else
837 message("olivetti-mode disabled");
839 handle_resize_nodelay(0, 0, NULL);
842 void
843 cmd_mini_delete_char(struct buffer *buffer)
845 char *c, *n;
847 if (!in_minibuffer) {
848 message("text is read-only");
849 return;
852 minibuffer_taint_hist();
854 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
855 if (*c == '\0')
856 return;
857 n = utf8_next_cp(c);
859 memmove(c, n, strlen(n)+1);
862 void
863 cmd_mini_delete_backward_char(struct buffer *buffer)
865 char *c, *p, *start;
867 if (!in_minibuffer) {
868 message("text is read-only");
869 return;
872 minibuffer_taint_hist();
874 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
875 start = buffer->current_line->line;
876 if (c == start)
877 return;
878 p = utf8_prev_cp(c-1, start);
880 memmove(p, c, strlen(c)+1);
881 buffer->cpoff--;
884 void
885 cmd_mini_kill_line(struct buffer *buffer)
887 char *c;
889 if (!in_minibuffer) {
890 message("text is read-only");
891 return;
894 minibuffer_taint_hist();
895 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
896 *c = '\0';
899 void
900 cmd_mini_abort(struct buffer *buffer)
902 if (!in_minibuffer)
903 return;
905 ministate.abortfn();
908 void
909 cmd_mini_complete_and_exit(struct buffer *buffer)
911 if (!in_minibuffer)
912 return;
914 minibuffer_taint_hist();
915 ministate.donefn();
918 void
919 cmd_mini_previous_history_element(struct buffer *buffer)
921 if (ministate.history == NULL) {
922 message("No history");
923 return;
926 if (ministate.hist_cur == NULL ||
927 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
928 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
929 ministate.hist_off = ministate.history->len - 1;
930 if (ministate.hist_cur == NULL)
931 message("No prev item");
932 } else {
933 ministate.hist_off--;
936 if (ministate.hist_cur != NULL)
937 buffer->current_line->line = ministate.hist_cur->h;
940 void
941 cmd_mini_next_history_element(struct buffer *buffer)
943 if (ministate.history == NULL) {
944 message("No history");
945 return;
948 if (ministate.hist_cur == NULL ||
949 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
950 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
951 ministate.hist_off = 0;
952 if (ministate.hist_cur == NULL)
953 message("No next item");
954 } else {
955 ministate.hist_off++;
958 if (ministate.hist_cur != NULL)
959 buffer->current_line->line = ministate.hist_cur->h;
962 static void
963 global_key_unbound(void)
965 message("%s is undefined", keybuf);
968 static void
969 minibuffer_hist_save_entry(void)
971 struct hist *hist;
973 if (ministate.history == NULL)
974 return;
976 if ((hist = calloc(1, sizeof(*hist))) == NULL)
977 abort();
979 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
981 if (TAILQ_EMPTY(&ministate.history->head))
982 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
983 else
984 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
985 ministate.history->len++;
988 /*
989 * taint the minibuffer cache: if we're currently showing a history
990 * element, copy that to the current buf and reset the "history
991 * navigation" thing.
992 */
993 static void
994 minibuffer_taint_hist(void)
996 if (ministate.hist_cur == NULL)
997 return;
999 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1000 ministate.hist_cur = NULL;
1003 static void
1004 minibuffer_self_insert(void)
1006 char *c, tmp[5] = {0};
1007 size_t len;
1009 minibuffer_taint_hist();
1011 if (thiskey.cp == 0)
1012 return;
1014 len = utf8_encode(thiskey.cp, tmp);
1015 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1016 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1017 return;
1019 memmove(c + len, c, strlen(c)+1);
1020 memcpy(c, tmp, len);
1021 ministate.buffer.cpoff++;
1024 static void
1025 eecmd_self_insert(void)
1027 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1028 !unicode_isgraph(thiskey.cp)) {
1029 global_key_unbound();
1030 return;
1033 minibuffer_self_insert();
1036 static void
1037 eecmd_select(void)
1039 struct cmds *cmd;
1041 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1042 if (!strcmp(cmd->cmd, ministate.buf)) {
1043 exit_minibuffer();
1044 minibuffer_hist_save_entry();
1045 cmd->fn(current_buffer());
1046 return;
1050 message("No match");
1053 static void
1054 ir_self_insert(void)
1056 minibuffer_self_insert();
1059 static void
1060 ir_select(void)
1062 char buf[1025] = {0};
1063 struct phos_uri uri;
1064 struct tab *tab;
1066 tab = current_tab();
1068 exit_minibuffer();
1069 minibuffer_hist_save_entry();
1071 /* a bit ugly but... */
1072 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1073 phos_uri_set_query(&uri, ministate.buf);
1074 phos_serialize_uri(&uri, buf, sizeof(buf));
1075 load_url_in_tab(tab, buf);
1078 static void
1079 lu_self_insert(void)
1081 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1082 !unicode_isgraph(thiskey.key)) {
1083 global_key_unbound();
1084 return;
1087 minibuffer_self_insert();
1090 static void
1091 lu_select(void)
1093 exit_minibuffer();
1094 minibuffer_hist_save_entry();
1095 load_url_in_tab(current_tab(), ministate.buf);
1098 static void
1099 bp_select(void)
1101 exit_minibuffer();
1102 if (*ministate.buf != '\0')
1103 add_to_bookmarks(ministate.buf);
1104 else
1105 message("Abort.");
1108 static void
1109 yornp_self_insert(void)
1111 if (thiskey.key != 'y' && thiskey.key != 'n') {
1112 message("Please answer y or n");
1113 return;
1116 exit_minibuffer();
1117 yornp_cb(thiskey.key == 'y', yornp_data);
1120 static void
1121 yornp_abort(void)
1123 exit_minibuffer();
1124 yornp_cb(0, yornp_data);
1127 static void
1128 read_self_insert(void)
1130 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1131 global_key_unbound();
1132 return;
1135 minibuffer_self_insert();
1138 static void
1139 read_abort(void)
1141 exit_minibuffer();
1142 read_cb(NULL, read_data);
1145 static void
1146 read_select(void)
1148 exit_minibuffer();
1149 minibuffer_hist_save_entry();
1150 read_cb(ministate.buf, read_data);
1153 static struct vline *
1154 nth_line(struct buffer *buffer, size_t n)
1156 struct vline *vl;
1157 size_t i;
1159 i = 0;
1160 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1161 if (i == n)
1162 return vl;
1163 i++;
1166 /* unreachable */
1167 abort();
1170 static struct tab *
1171 current_tab(void)
1173 struct tab *t;
1175 TAILQ_FOREACH(t, &tabshead, tabs) {
1176 if (t->flags & TAB_CURRENT)
1177 return t;
1180 /* unreachable */
1181 abort();
1184 static struct buffer *
1185 current_buffer(void)
1187 if (in_minibuffer)
1188 return &ministate.buffer;
1189 return &current_tab()->buffer;
1192 static int
1193 readkey(void)
1195 uint32_t state = 0;
1197 if ((thiskey.key = wgetch(body)) == ERR)
1198 return 0;
1200 thiskey.meta = thiskey.key == 27;
1201 if (thiskey.meta) {
1202 thiskey.key = wgetch(body);
1203 if (thiskey.key == ERR || thiskey.key == 27) {
1204 thiskey.meta = 0;
1205 thiskey.key = 27;
1209 thiskey.cp = 0;
1210 if ((unsigned int)thiskey.key < UINT8_MAX) {
1211 while (1) {
1212 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1213 break;
1214 if ((thiskey.key = wgetch(body)) == ERR) {
1215 message("Error decoding user input");
1216 return 0;
1221 return 1;
1224 static void
1225 dispatch_stdio(int fd, short ev, void *d)
1227 struct keymap *k;
1228 const char *keyname;
1229 char tmp[5] = {0};
1231 if (!readkey())
1232 return;
1234 if (keybuf[0] != '\0')
1235 strlcat(keybuf, " ", sizeof(keybuf));
1236 if (thiskey.meta)
1237 strlcat(keybuf, "M-", sizeof(keybuf));
1238 if (thiskey.cp != 0) {
1239 utf8_encode(thiskey.cp, tmp);
1240 strlcat(keybuf, tmp, sizeof(keybuf));
1241 } else {
1242 if ((keyname = unkbd(thiskey.key)) != NULL)
1243 strlcat(keybuf, keyname, sizeof(keybuf));
1244 else {
1245 tmp[0] = thiskey.key;
1246 strlcat(keybuf, tmp, sizeof(keybuf));
1250 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1251 if (k->meta == thiskey.meta &&
1252 k->key == thiskey.key) {
1253 if (k->fn == NULL)
1254 current_map = &k->map;
1255 else {
1256 current_map = base_map;
1257 strlcpy(keybuf, "", sizeof(keybuf));
1258 k->fn(current_buffer());
1260 goto done;
1264 if (current_map->unhandled_input != NULL)
1265 current_map->unhandled_input();
1266 else
1267 global_key_unbound();
1269 strlcpy(keybuf, "", sizeof(keybuf));
1270 current_map = base_map;
1272 done:
1273 if (side_window)
1274 recompute_help();
1276 redraw_tab(current_tab());
1279 static void
1280 handle_clear_minibuf(int fd, short ev, void *d)
1282 free(ministate.curmesg);
1283 ministate.curmesg = NULL;
1285 redraw_minibuffer();
1286 if (in_minibuffer) {
1287 wrefresh(body);
1288 wrefresh(minibuf);
1289 } else {
1290 wrefresh(minibuf);
1291 wrefresh(body);
1295 static void
1296 handle_resize(int sig, short ev, void *d)
1298 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1299 event_del(&resizeev);
1301 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1302 evtimer_add(&resizeev, &resize_timer);
1305 static void
1306 handle_resize_nodelay(int s, short ev, void *d)
1308 struct tab *tab;
1310 endwin();
1311 refresh();
1312 clear();
1314 /* move and resize the windows, in reverse order! */
1316 mvwin(minibuf, LINES-1, 0);
1317 wresize(minibuf, 1, COLS);
1319 mvwin(modeline, LINES-2, 0);
1320 wresize(modeline, 1, COLS);
1322 body_lines = LINES-3;
1323 body_cols = COLS;
1325 if (side_window) {
1326 help_cols = 0.3 * COLS;
1327 help_lines = LINES-3;
1328 mvwin(help, 1, 0);
1329 wresize(help, help_lines, help_cols);
1331 wrap_page(&helpwin, help_cols);
1333 body_cols = COLS - help_cols - 1;
1334 mvwin(body, 1, help_cols);
1335 } else
1336 mvwin(body, 1, 0);
1338 update_x_offset();
1339 wresize(body, body_lines, body_cols);
1341 wresize(tabline, 1, COLS);
1343 tab = current_tab();
1345 wrap_page(&tab->buffer, body_cols);
1346 redraw_tab(tab);
1349 static int
1350 wrap_page(struct buffer *buffer, int width)
1352 struct line *l;
1353 const struct line *orig;
1354 struct vline *vl;
1355 int pre_width;
1356 const char *prfx;
1358 orig = buffer->current_line == NULL
1359 ? NULL
1360 : buffer->current_line->parent;
1361 buffer->current_line = NULL;
1363 buffer->curs_y = 0;
1364 buffer->line_off = 0;
1366 empty_vlist(buffer);
1368 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1369 prfx = line_prefixes[l->type].prfx1;
1370 switch (l->type) {
1371 case LINE_TEXT:
1372 case LINE_LINK:
1373 case LINE_TITLE_1:
1374 case LINE_TITLE_2:
1375 case LINE_TITLE_3:
1376 case LINE_ITEM:
1377 case LINE_QUOTE:
1378 case LINE_PRE_START:
1379 case LINE_PRE_END:
1380 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1381 break;
1382 case LINE_PRE_CONTENT:
1383 if (olivetti_mode)
1384 pre_width = MIN(fill_column, width);
1385 else
1386 pre_width = width;
1387 hardwrap_text(buffer, l, pre_width);
1388 break;
1391 if (orig == l && buffer->current_line == NULL) {
1392 buffer->line_off = buffer->line_max-1;
1393 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1395 while (1) {
1396 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1397 if (vl == NULL || vl->parent != orig)
1398 break;
1399 buffer->current_line = vl;
1400 buffer->line_off--;
1405 if (buffer->current_line == NULL)
1406 buffer->current_line = TAILQ_FIRST(&buffer->head);
1408 return 1;
1411 static void
1412 print_vline(WINDOW *window, struct vline *vl)
1414 const char *text = vl->line;
1415 const char *prfx;
1416 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1417 int text_face = line_faces[vl->parent->type].text_prop;
1419 if (!vl->flags)
1420 prfx = line_prefixes[vl->parent->type].prfx1;
1421 else
1422 prfx = line_prefixes[vl->parent->type].prfx2;
1424 if (text == NULL)
1425 text = "";
1427 wattron(window, prefix_face);
1428 wprintw(window, "%s", prfx);
1429 wattroff(window, prefix_face);
1431 wattron(window, text_face);
1432 wprintw(window, "%s", text);
1433 wattroff(window, text_face);
1436 static void
1437 redraw_tabline(void)
1439 struct tab *tab;
1440 size_t toskip, ots, tabwidth, space, x;
1441 int current, y, truncated;
1442 const char *title;
1443 char buf[25];
1445 tabwidth = sizeof(buf)+1;
1446 space = COLS-2;
1448 toskip = 0;
1449 TAILQ_FOREACH(tab, &tabshead, tabs) {
1450 toskip++;
1451 if (tab->flags & TAB_CURRENT)
1452 break;
1455 if (toskip * tabwidth < space)
1456 toskip = 0;
1457 else {
1458 ots = toskip;
1459 toskip--;
1460 while (toskip != 0 &&
1461 (ots - toskip+1) * tabwidth < space)
1462 toskip--;
1465 werase(tabline);
1466 wattron(tabline, tab_face.background);
1467 wprintw(tabline, toskip == 0 ? " " : "<");
1468 wattroff(tabline, tab_face.background);
1470 truncated = 0;
1471 TAILQ_FOREACH(tab, &tabshead, tabs) {
1472 if (truncated)
1473 break;
1474 if (toskip != 0) {
1475 toskip--;
1476 continue;
1479 getyx(tabline, y, x);
1480 if (x + sizeof(buf)+2 >= (size_t)COLS)
1481 truncated = 1;
1483 current = tab->flags & TAB_CURRENT;
1485 if (*(title = tab->buffer.page.title) == '\0')
1486 title = tab->hist_cur->h;
1488 if (tab->flags & TAB_URGENT)
1489 strlcpy(buf, "!", sizeof(buf));
1490 else
1491 strlcpy(buf, " ", sizeof(buf));
1493 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1494 /* truncation happens */
1495 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1496 } else {
1497 /* pad with spaces */
1498 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1499 /* nop */ ;
1502 if (current)
1503 wattron(tabline, tab_face.current_tab);
1504 else
1505 wattron(tabline, tab_face.tab);
1507 wprintw(tabline, "%s", buf);
1508 if (TAILQ_NEXT(tab, tabs) != NULL)
1509 wprintw(tabline, " ");
1511 if (current)
1512 wattroff(tabline, tab_face.current_tab);
1513 else
1514 wattroff(tabline, tab_face.tab);
1517 wattron(tabline, tab_face.background);
1518 for (; x < (size_t)COLS; ++x)
1519 waddch(tabline, ' ');
1520 if (truncated)
1521 mvwprintw(tabline, 0, COLS-1, ">");
1524 static void
1525 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1527 struct vline *vl;
1528 int l;
1530 werase(win);
1532 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1533 if (TAILQ_EMPTY(&buffer->head))
1534 return;
1536 l = 0;
1537 vl = nth_line(buffer, buffer->line_off);
1538 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1539 wmove(win, l, x_offset);
1540 print_vline(win, vl);
1541 l++;
1542 if (l == height)
1543 break;
1546 wmove(win, buffer->curs_y, buffer->curs_x);
1549 static void
1550 redraw_help(void)
1552 redraw_window(help, help_lines, &helpwin);
1555 static void
1556 redraw_body(struct tab *tab)
1558 redraw_window(body, body_lines, &tab->buffer);
1561 static inline char
1562 trust_status_char(enum trust_state ts)
1564 switch (ts) {
1565 case TS_UNKNOWN: return 'u';
1566 case TS_UNTRUSTED: return '!';
1567 case TS_TRUSTED: return 'v';
1568 case TS_VERIFIED: return 'V';
1572 static void
1573 redraw_modeline(struct tab *tab)
1575 double pct;
1576 int x, y, max_x, max_y;
1577 const char *mode = tab->buffer.page.name;
1578 const char *spin = "-\\|/";
1580 werase(modeline);
1581 wattron(modeline, modeline_face.background);
1582 wmove(modeline, 0, 0);
1584 wprintw(modeline, "-%c%c %s ",
1585 spin[tab->loading_anim_step],
1586 trust_status_char(tab->trust),
1587 mode == NULL ? "(none)" : mode);
1589 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1591 if (tab->buffer.line_max <= (size_t)body_lines)
1592 wprintw(modeline, "All ");
1593 else if (tab->buffer.line_off == 0)
1594 wprintw(modeline, "Top ");
1595 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1596 wprintw(modeline, "Bottom ");
1597 else
1598 wprintw(modeline, "%.0f%% ", pct);
1600 wprintw(modeline, "%d/%d %s ",
1601 tab->buffer.line_off + tab->buffer.curs_y,
1602 tab->buffer.line_max,
1603 tab->hist_cur->h);
1605 getyx(modeline, y, x);
1606 getmaxyx(modeline, max_y, max_x);
1608 (void)y;
1609 (void)max_y;
1611 for (; x < max_x; ++x)
1612 waddstr(modeline, "-");
1614 wattroff(modeline, modeline_face.background);
1617 static void
1618 redraw_minibuffer(void)
1620 struct tab *tab;
1621 size_t off_y, off_x = 0;
1622 char *start, *c;
1624 wattron(minibuf, minibuffer_face.background);
1625 werase(minibuf);
1627 if (in_minibuffer) {
1628 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1629 if (ministate.hist_cur != NULL)
1630 wprintw(minibuf, "(%zu/%zu) ",
1631 ministate.hist_off + 1,
1632 ministate.history->len);
1634 getyx(minibuf, off_y, off_x);
1636 start = ministate.hist_cur != NULL
1637 ? ministate.hist_cur->h
1638 : ministate.buf;
1639 c = utf8_nth(ministate.buffer.current_line->line,
1640 ministate.buffer.cpoff);
1641 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1642 start = utf8_next_cp(start);
1645 waddstr(minibuf, start);
1648 if (ministate.curmesg != NULL)
1649 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1650 ministate.curmesg);
1652 if (!in_minibuffer && ministate.curmesg == NULL)
1653 waddstr(minibuf, keybuf);
1655 /* If nothing else, show the URL at point */
1656 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1657 tab = current_tab();
1658 if (tab->buffer.current_line != NULL &&
1659 tab->buffer.current_line->parent->type == LINE_LINK)
1660 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1663 if (in_minibuffer)
1664 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1666 wattroff(minibuf, minibuffer_face.background);
1669 static void
1670 redraw_tab(struct tab *tab)
1672 if (side_window) {
1673 redraw_help();
1674 wnoutrefresh(help);
1677 restore_cursor(&tab->buffer);
1679 redraw_tabline();
1680 redraw_body(tab);
1681 redraw_modeline(tab);
1682 redraw_minibuffer();
1684 wnoutrefresh(tabline);
1685 wnoutrefresh(modeline);
1687 if (in_minibuffer) {
1688 wnoutrefresh(body);
1689 wnoutrefresh(minibuf);
1690 } else {
1691 wnoutrefresh(minibuf);
1692 wnoutrefresh(body);
1695 doupdate();
1698 static void
1699 emit_help_item(char *prfx, void *fn)
1701 struct line *l;
1702 struct cmds *cmd;
1704 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1705 if (fn == cmd->fn)
1706 break;
1708 assert(cmd != NULL);
1710 if ((l = calloc(1, sizeof(*l))) == NULL)
1711 abort();
1713 l->type = LINE_TEXT;
1714 l->alt = NULL;
1716 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1718 if (TAILQ_EMPTY(&helpwin.page.head))
1719 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1720 else
1721 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1724 static void
1725 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1727 struct keymap *k;
1728 char p[32];
1729 const char *kn;
1731 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1732 strlcpy(p, prfx, sizeof(p));
1733 if (*p != '\0')
1734 strlcat(p, " ", sizeof(p));
1735 if (k->meta)
1736 strlcat(p, "M-", sizeof(p));
1737 if ((kn = unkbd(k->key)) != NULL)
1738 strlcat(p, kn, sizeof(p));
1739 else
1740 strlcat(p, keyname(k->key), sizeof(p));
1742 if (k->fn == NULL)
1743 rec_compute_help(&k->map, p, sizeof(p));
1744 else
1745 emit_help_item(p, k->fn);
1749 static void
1750 recompute_help(void)
1752 char p[32] = { 0 };
1754 empty_vlist(&helpwin);
1755 empty_linelist(&helpwin);
1756 rec_compute_help(current_map, p, sizeof(p));
1757 wrap_page(&helpwin, help_cols);
1760 static void
1761 vmessage(const char *fmt, va_list ap)
1763 if (evtimer_pending(&clminibufev, NULL))
1764 evtimer_del(&clminibufev);
1765 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1766 evtimer_add(&clminibufev, &clminibufev_timer);
1768 free(ministate.curmesg);
1770 /* TODO: what to do if the allocation fails here? */
1771 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1772 ministate.curmesg = NULL;
1774 redraw_minibuffer();
1775 if (in_minibuffer) {
1776 wrefresh(body);
1777 wrefresh(minibuf);
1778 } else {
1779 wrefresh(minibuf);
1780 wrefresh(body);
1784 static void
1785 message(const char *fmt, ...)
1787 va_list ap;
1789 va_start(ap, fmt);
1790 vmessage(fmt, ap);
1791 va_end(ap);
1794 static void
1795 start_loading_anim(struct tab *tab)
1797 if (tab->loading_anim)
1798 return;
1799 tab->loading_anim = 1;
1800 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1801 evtimer_add(&tab->loadingev, &loadingev_timer);
1804 static void
1805 update_loading_anim(int fd, short ev, void *d)
1807 struct tab *tab = d;
1809 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1811 if (tab->flags & TAB_CURRENT) {
1812 redraw_modeline(tab);
1813 wrefresh(modeline);
1814 wrefresh(body);
1815 if (in_minibuffer)
1816 wrefresh(minibuf);
1819 evtimer_add(&tab->loadingev, &loadingev_timer);
1822 static void
1823 stop_loading_anim(struct tab *tab)
1825 if (!tab->loading_anim)
1826 return;
1827 evtimer_del(&tab->loadingev);
1828 tab->loading_anim = 0;
1829 tab->loading_anim_step = 0;
1831 if (!(tab->flags & TAB_CURRENT))
1832 return;
1834 redraw_modeline(tab);
1836 wrefresh(modeline);
1837 wrefresh(body);
1838 if (in_minibuffer)
1839 wrefresh(minibuf);
1842 static void
1843 load_url_in_tab(struct tab *tab, const char *url)
1845 message("Loading %s...", url);
1846 start_loading_anim(tab);
1847 load_url(tab, url);
1849 tab->buffer.curs_x = 0;
1850 tab->buffer.curs_y = 0;
1851 redraw_tab(tab);
1854 static void
1855 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1856 void (*abortfn)(void), struct histhead *hist)
1858 in_minibuffer = 1;
1859 base_map = &minibuffer_map;
1860 current_map = &minibuffer_map;
1862 base_map->unhandled_input = self_insert_fn;
1864 ministate.donefn = donefn;
1865 ministate.abortfn = abortfn;
1866 memset(ministate.buf, 0, sizeof(ministate.buf));
1867 ministate.buffer.current_line = &ministate.vline;
1868 ministate.buffer.current_line->line = ministate.buf;
1869 ministate.buffer.cpoff = 0;
1870 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1872 ministate.history = hist;
1873 ministate.hist_cur = NULL;
1874 ministate.hist_off = 0;
1877 static void
1878 exit_minibuffer(void)
1880 werase(minibuf);
1882 in_minibuffer = 0;
1883 base_map = &global_map;
1884 current_map = &global_map;
1887 static void
1888 switch_to_tab(struct tab *tab)
1890 struct tab *t;
1892 TAILQ_FOREACH(t, &tabshead, tabs) {
1893 t->flags &= ~TAB_CURRENT;
1896 tab->flags |= TAB_CURRENT;
1897 tab->flags &= ~TAB_URGENT;
1900 unsigned int
1901 tab_new_id(void)
1903 return tab_counter++;
1906 static struct tab *
1907 new_tab(const char *url)
1909 struct tab *tab;
1911 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1912 event_loopbreak();
1913 return NULL;
1915 tab->fd = -1;
1917 TAILQ_INIT(&tab->hist.head);
1919 TAILQ_INIT(&tab->buffer.head);
1921 tab->id = tab_new_id();
1922 switch_to_tab(tab);
1924 if (TAILQ_EMPTY(&tabshead))
1925 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1926 else
1927 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1929 load_url_in_tab(tab, url);
1930 return tab;
1933 static void
1934 session_new_tab_cb(const char *url)
1936 new_tab(url);
1939 static void
1940 usage(void)
1942 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1943 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1946 int
1947 ui_init(int argc, char * const *argv)
1949 char path[PATH_MAX];
1950 const char *url = NEW_TAB_URL;
1951 int ch, configtest = 0, fonf = 0;
1953 strlcpy(path, getenv("HOME"), sizeof(path));
1954 strlcat(path, "/.telescope/config", sizeof(path));
1956 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1957 switch (ch) {
1958 case 'c':
1959 fonf = 1;
1960 strlcpy(path, optarg, sizeof(path));
1961 break;
1962 case 'n':
1963 configtest = 1;
1964 break;
1965 case 'h':
1966 usage();
1967 return 0;
1968 default:
1969 usage();
1970 return 1;
1973 argc -= optind;
1974 argv += optind;
1976 parseconfig(path, fonf);
1977 if (configtest){
1978 puts("config OK");
1979 exit(0);
1982 if (argc != 0)
1983 url = argv[0];
1985 setlocale(LC_ALL, "");
1987 TAILQ_INIT(&global_map.m);
1988 global_map.unhandled_input = global_key_unbound;
1990 TAILQ_INIT(&minibuffer_map.m);
1992 TAILQ_INIT(&eecmd_history.head);
1993 TAILQ_INIT(&ir_history.head);
1994 TAILQ_INIT(&lu_history.head);
1996 ministate.line.type = LINE_TEXT;
1997 ministate.vline.parent = &ministate.line;
1998 ministate.buffer.current_line = &ministate.vline;
2000 /* initialize help window */
2001 TAILQ_INIT(&helpwin.head);
2003 base_map = &global_map;
2004 current_map = &global_map;
2005 load_default_keys();
2007 initscr();
2008 raw();
2009 noecho();
2011 nonl();
2012 intrflush(stdscr, FALSE);
2014 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2015 return 0;
2016 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2017 return 0;
2018 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2019 return 0;
2020 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2021 return 0;
2022 if ((help = newwin(1, 1, 1, 0)) == NULL)
2023 return 0;
2025 body_lines = LINES-3;
2026 body_cols = COLS;
2028 update_x_offset();
2030 keypad(body, TRUE);
2031 scrollok(body, TRUE);
2033 /* non-blocking input */
2034 wtimeout(body, 0);
2036 mvwprintw(body, 0, 0, "");
2038 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2039 event_add(&stdioev, NULL);
2041 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2042 signal_add(&winchev, NULL);
2044 load_last_session(session_new_tab_cb);
2045 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2046 new_tab(url);
2048 return 1;
2051 void
2052 ui_on_tab_loaded(struct tab *tab)
2054 stop_loading_anim(tab);
2055 message("Loaded %s", tab->hist_cur->h);
2057 redraw_tabline();
2058 wrefresh(tabline);
2059 if (in_minibuffer)
2060 wrefresh(minibuf);
2061 else
2062 wrefresh(body);
2065 void
2066 ui_on_tab_refresh(struct tab *tab)
2068 wrap_page(&tab->buffer, body_cols);
2069 if (tab->flags & TAB_CURRENT) {
2070 restore_cursor(&tab->buffer);
2071 redraw_tab(tab);
2072 } else
2073 tab->flags |= TAB_URGENT;
2076 void
2077 ui_require_input(struct tab *tab, int hide)
2079 /* TODO: hard-switching to another tab is ugly */
2080 switch_to_tab(tab);
2082 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2083 &ir_history);
2084 strlcpy(ministate.prompt, "Input required: ",
2085 sizeof(ministate.prompt));
2086 redraw_tab(tab);
2089 void
2090 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2091 unsigned int data)
2093 size_t len;
2095 if (in_minibuffer) {
2096 fn(0, data);
2097 return;
2100 yornp_cb = fn;
2101 yornp_data = data;
2102 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2103 yornp_abort, NULL);
2105 len = sizeof(ministate.prompt);
2106 strlcpy(ministate.prompt, prompt, len);
2107 strlcat(ministate.prompt, " (y or n) ", len);
2108 redraw_tab(current_tab());
2111 void
2112 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2113 unsigned int data)
2115 size_t len;
2117 if (in_minibuffer)
2118 return;
2120 read_cb = fn;
2121 read_data = data;
2122 enter_minibuffer(read_self_insert, read_select, read_abort,
2123 &read_history);
2125 len = sizeof(ministate.prompt);
2126 strlcpy(ministate.prompt, prompt, len);
2127 strlcat(ministate.prompt, ": ", len);
2128 redraw_tab(current_tab());
2131 void
2132 ui_notify(const char *fmt, ...)
2134 va_list ap;
2136 va_start(ap, fmt);
2137 vmessage(fmt, ap);
2138 va_end(ap);
2141 void
2142 ui_end(void)
2144 endwin();