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();
1270 strlcpy(keybuf, "", sizeof(keybuf));
1271 current_map = base_map;
1273 done:
1274 if (side_window)
1275 recompute_help();
1277 redraw_tab(current_tab());
1280 static void
1281 handle_clear_minibuf(int fd, short ev, void *d)
1283 free(ministate.curmesg);
1284 ministate.curmesg = NULL;
1286 redraw_minibuffer();
1287 if (in_minibuffer) {
1288 wrefresh(body);
1289 wrefresh(minibuf);
1290 } else {
1291 wrefresh(minibuf);
1292 wrefresh(body);
1296 static void
1297 handle_resize(int sig, short ev, void *d)
1299 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1300 event_del(&resizeev);
1302 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1303 evtimer_add(&resizeev, &resize_timer);
1306 static void
1307 handle_resize_nodelay(int s, short ev, void *d)
1309 struct tab *tab;
1311 endwin();
1312 refresh();
1313 clear();
1315 /* move and resize the windows, in reverse order! */
1317 mvwin(minibuf, LINES-1, 0);
1318 wresize(minibuf, 1, COLS);
1320 mvwin(modeline, LINES-2, 0);
1321 wresize(modeline, 1, COLS);
1323 body_lines = LINES-3;
1324 body_cols = COLS;
1326 if (side_window) {
1327 help_cols = 0.3 * COLS;
1328 help_lines = LINES-3;
1329 mvwin(help, 1, 0);
1330 wresize(help, help_lines, help_cols);
1332 wrap_page(&helpwin, help_cols);
1334 body_cols = COLS - help_cols - 1;
1335 mvwin(body, 1, help_cols);
1336 } else
1337 mvwin(body, 1, 0);
1339 update_x_offset();
1340 wresize(body, body_lines, body_cols);
1342 wresize(tabline, 1, COLS);
1344 tab = current_tab();
1346 wrap_page(&tab->buffer, body_cols);
1347 redraw_tab(tab);
1350 static int
1351 wrap_page(struct buffer *buffer, int width)
1353 struct line *l;
1354 const struct line *orig;
1355 struct vline *vl;
1356 int pre_width;
1357 const char *prfx;
1359 orig = buffer->current_line == NULL
1360 ? NULL
1361 : buffer->current_line->parent;
1362 buffer->current_line = NULL;
1364 buffer->curs_y = 0;
1365 buffer->line_off = 0;
1367 empty_vlist(buffer);
1369 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1370 prfx = line_prefixes[l->type].prfx1;
1371 switch (l->type) {
1372 case LINE_TEXT:
1373 case LINE_LINK:
1374 case LINE_TITLE_1:
1375 case LINE_TITLE_2:
1376 case LINE_TITLE_3:
1377 case LINE_ITEM:
1378 case LINE_QUOTE:
1379 case LINE_PRE_START:
1380 case LINE_PRE_END:
1381 wrap_text(buffer, prfx, l, MIN(fill_column, width));
1382 break;
1383 case LINE_PRE_CONTENT:
1384 if (olivetti_mode)
1385 pre_width = MIN(fill_column, width);
1386 else
1387 pre_width = width;
1388 hardwrap_text(buffer, l, pre_width);
1389 break;
1392 if (orig == l && buffer->current_line == NULL) {
1393 buffer->line_off = buffer->line_max-1;
1394 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1396 while (1) {
1397 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1398 if (vl == NULL || vl->parent != orig)
1399 break;
1400 buffer->current_line = vl;
1401 buffer->line_off--;
1406 if (buffer->current_line == NULL)
1407 buffer->current_line = TAILQ_FIRST(&buffer->head);
1409 return 1;
1412 static void
1413 print_vline(WINDOW *window, struct vline *vl)
1415 const char *text = vl->line;
1416 const char *prfx;
1417 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1418 int text_face = line_faces[vl->parent->type].text_prop;
1420 if (!vl->flags)
1421 prfx = line_prefixes[vl->parent->type].prfx1;
1422 else
1423 prfx = line_prefixes[vl->parent->type].prfx2;
1425 if (text == NULL)
1426 text = "";
1428 wattron(window, prefix_face);
1429 wprintw(window, "%s", prfx);
1430 wattroff(window, prefix_face);
1432 wattron(window, text_face);
1433 wprintw(window, "%s", text);
1434 wattroff(window, text_face);
1437 static void
1438 redraw_tabline(void)
1440 struct tab *tab;
1441 size_t toskip, ots, tabwidth, space, x;
1442 int current, y, truncated;
1443 const char *title;
1444 char buf[25];
1446 tabwidth = sizeof(buf)+1;
1447 space = COLS-2;
1449 toskip = 0;
1450 TAILQ_FOREACH(tab, &tabshead, tabs) {
1451 toskip++;
1452 if (tab->flags & TAB_CURRENT)
1453 break;
1456 if (toskip * tabwidth < space)
1457 toskip = 0;
1458 else {
1459 ots = toskip;
1460 toskip--;
1461 while (toskip != 0 &&
1462 (ots - toskip+1) * tabwidth < space)
1463 toskip--;
1466 werase(tabline);
1467 wattron(tabline, tab_face.background);
1468 wprintw(tabline, toskip == 0 ? " " : "<");
1469 wattroff(tabline, tab_face.background);
1471 truncated = 0;
1472 TAILQ_FOREACH(tab, &tabshead, tabs) {
1473 if (truncated)
1474 break;
1475 if (toskip != 0) {
1476 toskip--;
1477 continue;
1480 getyx(tabline, y, x);
1481 if (x + sizeof(buf)+2 >= (size_t)COLS)
1482 truncated = 1;
1484 current = tab->flags & TAB_CURRENT;
1486 if (*(title = tab->buffer.page.title) == '\0')
1487 title = tab->hist_cur->h;
1489 if (tab->flags & TAB_URGENT)
1490 strlcpy(buf, "!", sizeof(buf));
1491 else
1492 strlcpy(buf, " ", sizeof(buf));
1494 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1495 /* truncation happens */
1496 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1497 } else {
1498 /* pad with spaces */
1499 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1500 /* nop */ ;
1503 if (current)
1504 wattron(tabline, tab_face.current_tab);
1505 else
1506 wattron(tabline, tab_face.tab);
1508 wprintw(tabline, "%s", buf);
1509 if (TAILQ_NEXT(tab, tabs) != NULL)
1510 wprintw(tabline, " ");
1512 if (current)
1513 wattroff(tabline, tab_face.current_tab);
1514 else
1515 wattroff(tabline, tab_face.tab);
1518 wattron(tabline, tab_face.background);
1519 for (; x < (size_t)COLS; ++x)
1520 waddch(tabline, ' ');
1521 if (truncated)
1522 mvwprintw(tabline, 0, COLS-1, ">");
1525 static void
1526 redraw_window(WINDOW *win, int height, struct buffer *buffer)
1528 struct vline *vl;
1529 int l;
1531 werase(win);
1533 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1534 if (TAILQ_EMPTY(&buffer->head))
1535 return;
1537 l = 0;
1538 vl = nth_line(buffer, buffer->line_off);
1539 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1540 wmove(win, l, x_offset);
1541 print_vline(win, vl);
1542 l++;
1543 if (l == height)
1544 break;
1547 wmove(win, buffer->curs_y, buffer->curs_x);
1550 static void
1551 redraw_help(void)
1553 redraw_window(help, help_lines, &helpwin);
1556 static void
1557 redraw_body(struct tab *tab)
1559 redraw_window(body, body_lines, &tab->buffer);
1562 static inline char
1563 trust_status_char(enum trust_state ts)
1565 switch (ts) {
1566 case TS_UNKNOWN: return 'u';
1567 case TS_UNTRUSTED: return '!';
1568 case TS_TRUSTED: return 'v';
1569 case TS_VERIFIED: return 'V';
1573 static void
1574 redraw_modeline(struct tab *tab)
1576 double pct;
1577 int x, y, max_x, max_y;
1578 const char *mode = tab->buffer.page.name;
1579 const char *spin = "-\\|/";
1581 werase(modeline);
1582 wattron(modeline, modeline_face.background);
1583 wmove(modeline, 0, 0);
1585 wprintw(modeline, "-%c%c %s ",
1586 spin[tab->loading_anim_step],
1587 trust_status_char(tab->trust),
1588 mode == NULL ? "(none)" : mode);
1590 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1592 if (tab->buffer.line_max <= (size_t)body_lines)
1593 wprintw(modeline, "All ");
1594 else if (tab->buffer.line_off == 0)
1595 wprintw(modeline, "Top ");
1596 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1597 wprintw(modeline, "Bottom ");
1598 else
1599 wprintw(modeline, "%.0f%% ", pct);
1601 wprintw(modeline, "%d/%d %s ",
1602 tab->buffer.line_off + tab->buffer.curs_y,
1603 tab->buffer.line_max,
1604 tab->hist_cur->h);
1606 getyx(modeline, y, x);
1607 getmaxyx(modeline, max_y, max_x);
1609 (void)y;
1610 (void)max_y;
1612 for (; x < max_x; ++x)
1613 waddstr(modeline, "-");
1615 wattroff(modeline, modeline_face.background);
1618 static void
1619 redraw_minibuffer(void)
1621 struct tab *tab;
1622 size_t off_y, off_x = 0;
1623 char *start, *c;
1625 wattron(minibuf, minibuffer_face.background);
1626 werase(minibuf);
1628 if (in_minibuffer) {
1629 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1630 if (ministate.hist_cur != NULL)
1631 wprintw(minibuf, "(%zu/%zu) ",
1632 ministate.hist_off + 1,
1633 ministate.history->len);
1635 getyx(minibuf, off_y, off_x);
1637 start = ministate.hist_cur != NULL
1638 ? ministate.hist_cur->h
1639 : ministate.buf;
1640 c = utf8_nth(ministate.buffer.current_line->line,
1641 ministate.buffer.cpoff);
1642 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1643 start = utf8_next_cp(start);
1646 waddstr(minibuf, start);
1649 if (ministate.curmesg != NULL)
1650 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1651 ministate.curmesg);
1653 if (!in_minibuffer && ministate.curmesg == NULL)
1654 waddstr(minibuf, keybuf);
1656 /* If nothing else, show the URL at point */
1657 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1658 tab = current_tab();
1659 if (tab->buffer.current_line != NULL &&
1660 tab->buffer.current_line->parent->type == LINE_LINK)
1661 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1664 if (in_minibuffer)
1665 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1667 wattroff(minibuf, minibuffer_face.background);
1670 static void
1671 redraw_tab(struct tab *tab)
1673 if (side_window) {
1674 redraw_help();
1675 wnoutrefresh(help);
1678 restore_cursor(&tab->buffer);
1680 redraw_tabline();
1681 redraw_body(tab);
1682 redraw_modeline(tab);
1683 redraw_minibuffer();
1685 wnoutrefresh(tabline);
1686 wnoutrefresh(modeline);
1688 if (in_minibuffer) {
1689 wnoutrefresh(body);
1690 wnoutrefresh(minibuf);
1691 } else {
1692 wnoutrefresh(minibuf);
1693 wnoutrefresh(body);
1696 doupdate();
1699 static void
1700 emit_help_item(char *prfx, void *fn)
1702 struct line *l;
1703 struct cmds *cmd;
1705 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1706 if (fn == cmd->fn)
1707 break;
1709 assert(cmd != NULL);
1711 if ((l = calloc(1, sizeof(*l))) == NULL)
1712 abort();
1714 l->type = LINE_TEXT;
1715 l->alt = NULL;
1717 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1719 if (TAILQ_EMPTY(&helpwin.page.head))
1720 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1721 else
1722 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1725 static void
1726 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1728 struct keymap *k;
1729 char p[32];
1730 const char *kn;
1732 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1733 strlcpy(p, prfx, sizeof(p));
1734 if (*p != '\0')
1735 strlcat(p, " ", sizeof(p));
1736 if (k->meta)
1737 strlcat(p, "M-", sizeof(p));
1738 if ((kn = unkbd(k->key)) != NULL)
1739 strlcat(p, kn, sizeof(p));
1740 else
1741 strlcat(p, keyname(k->key), sizeof(p));
1743 if (k->fn == NULL)
1744 rec_compute_help(&k->map, p, sizeof(p));
1745 else
1746 emit_help_item(p, k->fn);
1750 static void
1751 recompute_help(void)
1753 char p[32] = { 0 };
1755 empty_vlist(&helpwin);
1756 empty_linelist(&helpwin);
1757 rec_compute_help(current_map, p, sizeof(p));
1758 wrap_page(&helpwin, help_cols);
1761 static void
1762 vmessage(const char *fmt, va_list ap)
1764 if (evtimer_pending(&clminibufev, NULL))
1765 evtimer_del(&clminibufev);
1766 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1767 evtimer_add(&clminibufev, &clminibufev_timer);
1769 free(ministate.curmesg);
1771 /* TODO: what to do if the allocation fails here? */
1772 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1773 ministate.curmesg = NULL;
1775 redraw_minibuffer();
1776 if (in_minibuffer) {
1777 wrefresh(body);
1778 wrefresh(minibuf);
1779 } else {
1780 wrefresh(minibuf);
1781 wrefresh(body);
1785 static void
1786 message(const char *fmt, ...)
1788 va_list ap;
1790 va_start(ap, fmt);
1791 vmessage(fmt, ap);
1792 va_end(ap);
1795 static void
1796 start_loading_anim(struct tab *tab)
1798 if (tab->loading_anim)
1799 return;
1800 tab->loading_anim = 1;
1801 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1802 evtimer_add(&tab->loadingev, &loadingev_timer);
1805 static void
1806 update_loading_anim(int fd, short ev, void *d)
1808 struct tab *tab = d;
1810 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1812 if (tab->flags & TAB_CURRENT) {
1813 redraw_modeline(tab);
1814 wrefresh(modeline);
1815 wrefresh(body);
1816 if (in_minibuffer)
1817 wrefresh(minibuf);
1820 evtimer_add(&tab->loadingev, &loadingev_timer);
1823 static void
1824 stop_loading_anim(struct tab *tab)
1826 if (!tab->loading_anim)
1827 return;
1828 evtimer_del(&tab->loadingev);
1829 tab->loading_anim = 0;
1830 tab->loading_anim_step = 0;
1832 if (!(tab->flags & TAB_CURRENT))
1833 return;
1835 redraw_modeline(tab);
1837 wrefresh(modeline);
1838 wrefresh(body);
1839 if (in_minibuffer)
1840 wrefresh(minibuf);
1843 static void
1844 load_url_in_tab(struct tab *tab, const char *url)
1846 message("Loading %s...", url);
1847 start_loading_anim(tab);
1848 load_url(tab, url);
1850 tab->buffer.curs_x = 0;
1851 tab->buffer.curs_y = 0;
1852 redraw_tab(tab);
1855 static void
1856 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1857 void (*abortfn)(void), struct histhead *hist)
1859 in_minibuffer = 1;
1860 base_map = &minibuffer_map;
1861 current_map = &minibuffer_map;
1863 base_map->unhandled_input = self_insert_fn;
1865 ministate.donefn = donefn;
1866 ministate.abortfn = abortfn;
1867 memset(ministate.buf, 0, sizeof(ministate.buf));
1868 ministate.buffer.current_line = &ministate.vline;
1869 ministate.buffer.current_line->line = ministate.buf;
1870 ministate.buffer.cpoff = 0;
1871 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1873 ministate.history = hist;
1874 ministate.hist_cur = NULL;
1875 ministate.hist_off = 0;
1878 static void
1879 exit_minibuffer(void)
1881 werase(minibuf);
1883 in_minibuffer = 0;
1884 base_map = &global_map;
1885 current_map = &global_map;
1888 static void
1889 switch_to_tab(struct tab *tab)
1891 struct tab *t;
1893 TAILQ_FOREACH(t, &tabshead, tabs) {
1894 t->flags &= ~TAB_CURRENT;
1897 tab->flags |= TAB_CURRENT;
1898 tab->flags &= ~TAB_URGENT;
1901 unsigned int
1902 tab_new_id(void)
1904 return tab_counter++;
1907 static struct tab *
1908 new_tab(const char *url)
1910 struct tab *tab;
1912 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1913 event_loopbreak();
1914 return NULL;
1916 tab->fd = -1;
1918 TAILQ_INIT(&tab->hist.head);
1920 TAILQ_INIT(&tab->buffer.head);
1922 tab->id = tab_new_id();
1923 switch_to_tab(tab);
1925 if (TAILQ_EMPTY(&tabshead))
1926 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1927 else
1928 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1930 load_url_in_tab(tab, url);
1931 return tab;
1934 static void
1935 session_new_tab_cb(const char *url)
1937 new_tab(url);
1940 static void
1941 usage(void)
1943 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1944 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1947 int
1948 ui_init(int argc, char * const *argv)
1950 char path[PATH_MAX];
1951 const char *url = NEW_TAB_URL;
1952 int ch, configtest = 0, fonf = 0;
1954 strlcpy(path, getenv("HOME"), sizeof(path));
1955 strlcat(path, "/.telescope/config", sizeof(path));
1957 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1958 switch (ch) {
1959 case 'c':
1960 fonf = 1;
1961 strlcpy(path, optarg, sizeof(path));
1962 break;
1963 case 'n':
1964 configtest = 1;
1965 break;
1966 case 'h':
1967 usage();
1968 return 0;
1969 default:
1970 usage();
1971 return 1;
1974 argc -= optind;
1975 argv += optind;
1977 parseconfig(path, fonf);
1978 if (configtest){
1979 puts("config OK");
1980 exit(0);
1983 if (argc != 0)
1984 url = argv[0];
1986 setlocale(LC_ALL, "");
1988 TAILQ_INIT(&global_map.m);
1989 global_map.unhandled_input = global_key_unbound;
1991 TAILQ_INIT(&minibuffer_map.m);
1993 TAILQ_INIT(&eecmd_history.head);
1994 TAILQ_INIT(&ir_history.head);
1995 TAILQ_INIT(&lu_history.head);
1997 ministate.line.type = LINE_TEXT;
1998 ministate.vline.parent = &ministate.line;
1999 ministate.buffer.current_line = &ministate.vline;
2001 /* initialize help window */
2002 TAILQ_INIT(&helpwin.head);
2004 base_map = &global_map;
2005 current_map = &global_map;
2006 load_default_keys();
2008 initscr();
2009 raw();
2010 noecho();
2012 nonl();
2013 intrflush(stdscr, FALSE);
2015 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2016 return 0;
2017 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2018 return 0;
2019 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2020 return 0;
2021 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2022 return 0;
2023 if ((help = newwin(1, 1, 1, 0)) == NULL)
2024 return 0;
2026 body_lines = LINES-3;
2027 body_cols = COLS;
2029 update_x_offset();
2031 keypad(body, TRUE);
2032 scrollok(body, TRUE);
2034 /* non-blocking input */
2035 wtimeout(body, 0);
2037 mvwprintw(body, 0, 0, "");
2039 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2040 event_add(&stdioev, NULL);
2042 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2043 signal_add(&winchev, NULL);
2045 load_last_session(session_new_tab_cb);
2046 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2047 new_tab(url);
2049 return 1;
2052 void
2053 ui_on_tab_loaded(struct tab *tab)
2055 stop_loading_anim(tab);
2056 message("Loaded %s", tab->hist_cur->h);
2058 redraw_tabline();
2059 wrefresh(tabline);
2060 if (in_minibuffer)
2061 wrefresh(minibuf);
2062 else
2063 wrefresh(body);
2066 void
2067 ui_on_tab_refresh(struct tab *tab)
2069 wrap_page(&tab->buffer, body_cols);
2070 if (tab->flags & TAB_CURRENT) {
2071 restore_cursor(&tab->buffer);
2072 redraw_tab(tab);
2073 } else
2074 tab->flags |= TAB_URGENT;
2077 void
2078 ui_require_input(struct tab *tab, int hide)
2080 /* TODO: hard-switching to another tab is ugly */
2081 switch_to_tab(tab);
2083 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2084 &ir_history);
2085 strlcpy(ministate.prompt, "Input required: ",
2086 sizeof(ministate.prompt));
2087 redraw_tab(tab);
2090 void
2091 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2092 unsigned int data)
2094 size_t len;
2096 if (in_minibuffer) {
2097 fn(0, data);
2098 return;
2101 yornp_cb = fn;
2102 yornp_data = data;
2103 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2104 yornp_abort, NULL);
2106 len = sizeof(ministate.prompt);
2107 strlcpy(ministate.prompt, prompt, len);
2108 strlcat(ministate.prompt, " (y or n) ", len);
2109 redraw_tab(current_tab());
2112 void
2113 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2114 unsigned int data)
2116 size_t len;
2118 if (in_minibuffer)
2119 return;
2121 read_cb = fn;
2122 read_data = data;
2123 enter_minibuffer(read_self_insert, read_select, read_abort,
2124 &read_history);
2126 len = sizeof(ministate.prompt);
2127 strlcpy(ministate.prompt, prompt, len);
2128 strlcat(ministate.prompt, ": ", len);
2129 redraw_tab(current_tab());
2132 void
2133 ui_notify(const char *fmt, ...)
2135 va_list ap;
2137 va_start(ap, fmt);
2138 vmessage(fmt, ap);
2139 va_end(ap);
2142 void
2143 ui_end(void)
2145 endwin();