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 <locale.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #define TAB_CURRENT 0x1
49 #define TAB_URGENT 0x2
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void restore_cursor(struct buffer*);
58 static void global_key_unbound(void);
59 static void minibuffer_hist_save_entry(void);
60 static void minibuffer_taint_hist(void);
61 static void minibuffer_self_insert(void);
62 static void eecmd_self_insert(void);
63 static void eecmd_select(void);
64 static void ir_self_insert(void);
65 static void ir_select(void);
66 static void lu_self_insert(void);
67 static void lu_select(void);
68 static void bp_select(void);
69 static void yornp_self_insert(void);
70 static void yornp_abort(void);
71 static void read_self_insert(void);
72 static void read_abort(void);
73 static void read_select(void);
75 static struct vline *nth_line(struct buffer*, size_t);
76 static struct tab *current_tab(void);
77 static struct buffer *current_buffer(void);
78 static int readkey(void);
79 static void dispatch_stdio(int, short, void*);
80 static void handle_clear_minibuf(int, short, void*);
81 static void handle_resize(int, short, void*);
82 static void handle_resize_nodelay(int, short, void*);
83 static int wrap_page(struct buffer*, int);
84 static void print_vline(WINDOW*, struct vline*);
85 static void redraw_tabline(void);
86 static void redraw_window(WINDOW*, int, struct buffer*);
87 static void redraw_help(void);
88 static void redraw_body(struct tab*);
89 static void redraw_modeline(struct tab*);
90 static void redraw_minibuffer(void);
91 static void redraw_tab(struct tab*);
92 static void emit_help_item(char*, void*);
93 static void rec_compute_help(struct kmap*, char*, size_t);
94 static void recompute_help(void);
95 static void vmessage(const char*, va_list);
96 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
97 static void start_loading_anim(struct tab*);
98 static void update_loading_anim(int, short, void*);
99 static void stop_loading_anim(struct tab*);
100 static void load_url_in_tab(struct tab*, const char*);
101 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
102 static void exit_minibuffer(void);
103 static void switch_to_tab(struct tab*);
104 static struct tab *new_tab(const char*);
105 static void session_new_tab_cb(const char*);
106 static void usage(void);
108 static struct { short meta; int key; uint32_t cp; } thiskey;
110 static struct event resizeev;
111 static struct timeval resize_timer = { 0, 250000 };
113 static WINDOW *tabline, *body, *modeline, *minibuf;
114 static int body_lines, body_cols;
116 static WINDOW *help;
117 static struct buffer helpwin;
118 static int help_lines, help_cols;
120 static int side_window;
122 static struct event clminibufev;
123 static struct timeval clminibufev_timer = { 5, 0 };
124 static struct timeval loadingev_timer = { 0, 250000 };
126 static uint32_t tab_counter;
128 static char keybuf[64];
130 static void (*yornp_cb)(int, unsigned int);
131 static unsigned int yornp_data;
133 static void (*read_cb)(const char*, unsigned int);
134 static unsigned int read_data;
136 struct kmap global_map,
137 minibuffer_map,
138 *current_map,
139 *base_map;
141 static struct histhead eecmd_history,
142 ir_history,
143 lu_history,
144 read_history;
146 static int in_minibuffer;
148 static struct {
149 char *curmesg;
151 char prompt[64];
152 void (*donefn)(void);
153 void (*abortfn)(void);
155 char buf[1025];
156 struct line line;
157 struct vline vline;
158 struct buffer buffer;
160 struct histhead *history;
161 struct hist *hist_cur;
162 size_t hist_off;
163 } ministate;
165 struct lineprefix {
166 const char *prfx1;
167 const char *prfx2;
168 } line_prefixes[] = {
169 [LINE_TEXT] = { "", "" },
170 [LINE_LINK] = { "=> ", " " },
171 [LINE_TITLE_1] = { "# ", " " },
172 [LINE_TITLE_2] = { "## ", " " },
173 [LINE_TITLE_3] = { "### ", " " },
174 [LINE_ITEM] = { "* ", " " },
175 [LINE_QUOTE] = { "> ", " " },
176 [LINE_PRE_START] = { "```", " " },
177 [LINE_PRE_CONTENT] = { "", "" },
178 [LINE_PRE_END] = { "```", "```" },
179 };
181 static struct line_face {
182 int prefix_prop;
183 int text_prop;
184 } line_faces[] = {
185 [LINE_TEXT] = { 0, 0 },
186 [LINE_LINK] = { 0, A_UNDERLINE },
187 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
188 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
189 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
190 [LINE_ITEM] = { 0, 0 },
191 [LINE_QUOTE] = { 0, A_DIM },
192 [LINE_PRE_START] = { 0, 0 },
193 [LINE_PRE_CONTENT] = { 0, 0 },
194 [LINE_PRE_END] = { 0, 0 },
195 };
197 static struct tab_face {
198 int background, tab, current_tab;
199 } tab_face = {
200 A_REVERSE, A_REVERSE, A_NORMAL
201 };
203 static inline void
204 global_set_key(const char *key, void (*fn)(struct buffer*))
206 if (!kmap_define_key(&global_map, key, fn))
207 _exit(1);
210 static inline void
211 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
213 if (!kmap_define_key(&minibuffer_map, key, fn))
214 _exit(1);
217 static void
218 load_default_keys(void)
220 /* === global map === */
222 /* emacs */
223 global_set_key("C-p", cmd_previous_line);
224 global_set_key("C-n", cmd_next_line);
225 global_set_key("C-f", cmd_forward_char);
226 global_set_key("C-b", cmd_backward_char);
227 global_set_key("M-{", cmd_backward_paragraph);
228 global_set_key("M-}", cmd_forward_paragraph);
229 global_set_key("C-a", cmd_move_beginning_of_line);
230 global_set_key("C-e", cmd_move_end_of_line);
232 global_set_key("M-v", cmd_scroll_up);
233 global_set_key("C-v", cmd_scroll_down);
234 global_set_key("M-space", cmd_scroll_up);
235 global_set_key("space", cmd_scroll_down);
237 global_set_key("M-<", cmd_beginning_of_buffer);
238 global_set_key("M->", cmd_end_of_buffer);
240 global_set_key("C-x C-c", cmd_kill_telescope);
242 global_set_key("C-g", cmd_clear_minibuf);
244 global_set_key("M-x", cmd_execute_extended_command);
245 global_set_key("C-x C-f", cmd_load_url);
246 global_set_key("C-x M-f", cmd_load_current_url);
248 global_set_key("C-x t 0", cmd_tab_close);
249 global_set_key("C-x t 1", cmd_tab_close_other);
250 global_set_key("C-x t 2", cmd_tab_new);
251 global_set_key("C-x t o", cmd_tab_next);
252 global_set_key("C-x t O", cmd_tab_previous);
253 global_set_key("C-x t m", cmd_tab_move);
254 global_set_key("C-x t M", cmd_tab_move_to);
256 global_set_key("C-M-b", cmd_previous_page);
257 global_set_key("C-M-f", cmd_next_page);
259 global_set_key("<f7> a", cmd_bookmark_page);
260 global_set_key("<f7> <f7>", cmd_list_bookmarks);
262 /* vi/vi-like */
263 global_set_key("k", cmd_previous_line);
264 global_set_key("j", cmd_next_line);
265 global_set_key("l", cmd_forward_char);
266 global_set_key("h", cmd_backward_char);
267 global_set_key("{", cmd_backward_paragraph);
268 global_set_key("}", cmd_forward_paragraph);
269 global_set_key("^", cmd_move_beginning_of_line);
270 global_set_key("$", cmd_move_end_of_line);
272 global_set_key("K", cmd_scroll_line_up);
273 global_set_key("J", cmd_scroll_line_down);
275 global_set_key("g g", cmd_beginning_of_buffer);
276 global_set_key("G", cmd_end_of_buffer);
278 global_set_key("g D", cmd_tab_close);
279 global_set_key("g N", cmd_tab_new);
280 global_set_key("g t", cmd_tab_next);
281 global_set_key("g T", cmd_tab_previous);
282 global_set_key("g M-t", cmd_tab_move);
283 global_set_key("g M-T", cmd_tab_move_to);
285 global_set_key("H", cmd_previous_page);
286 global_set_key("L", cmd_next_page);
288 /* tmp */
289 global_set_key("q", cmd_kill_telescope);
291 global_set_key("esc", cmd_clear_minibuf);
293 global_set_key(":", cmd_execute_extended_command);
295 /* cua */
296 global_set_key("<up>", cmd_previous_line);
297 global_set_key("<down>", cmd_next_line);
298 global_set_key("<right>", cmd_forward_char);
299 global_set_key("<left>", cmd_backward_char);
300 global_set_key("<prior>", cmd_scroll_up);
301 global_set_key("<next>", cmd_scroll_down);
303 global_set_key("M-<left>", cmd_previous_page);
304 global_set_key("M-<right>", cmd_next_page);
306 /* "ncurses standard" */
307 global_set_key("C-l", cmd_redraw);
309 /* global */
310 global_set_key("<f1>", cmd_toggle_help);
311 global_set_key("C-m", cmd_push_button);
312 global_set_key("M-enter", cmd_push_button_new_tab);
313 global_set_key("M-tab", cmd_previous_button);
314 global_set_key("tab", cmd_next_button);
316 /* === minibuffer map === */
317 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
318 minibuffer_set_key("C-g", cmd_mini_abort);
319 minibuffer_set_key("esc", cmd_mini_abort);
320 minibuffer_set_key("C-d", cmd_mini_delete_char);
321 minibuffer_set_key("del", cmd_mini_delete_backward_char);
322 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
323 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
325 minibuffer_set_key("C-b", cmd_backward_char);
326 minibuffer_set_key("C-f", cmd_forward_char);
327 minibuffer_set_key("<left>", cmd_backward_char);
328 minibuffer_set_key("<right>", cmd_forward_char);
329 minibuffer_set_key("C-e", cmd_move_end_of_line);
330 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
331 minibuffer_set_key("<end>", cmd_move_end_of_line);
332 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
333 minibuffer_set_key("C-k", cmd_mini_kill_line);
335 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
336 minibuffer_set_key("M-n", cmd_mini_next_history_element);
337 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
338 minibuffer_set_key("<down>", cmd_mini_next_history_element);
341 static void
342 restore_cursor(struct buffer *buffer)
344 struct vline *vl;
345 const char *prfx;
347 vl = buffer->current_line;
348 if (vl == NULL || vl->line == NULL)
349 buffer->curs_x = buffer->cpoff = 0;
350 else
351 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
353 if (vl != NULL) {
354 prfx = line_prefixes[vl->parent->type].prfx1;
355 buffer->curs_x += utf8_swidth(prfx);
359 void
360 cmd_previous_line(struct buffer *buffer)
362 struct vline *vl;
364 if (buffer->current_line == NULL
365 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
366 return;
368 if (--buffer->curs_y < 0) {
369 buffer->curs_y = 0;
370 cmd_scroll_line_up(buffer);
371 return;
374 buffer->current_line = vl;
375 restore_cursor(buffer);
378 void
379 cmd_next_line(struct buffer *buffer)
381 struct vline *vl;
383 if (buffer->current_line == NULL
384 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
385 return;
387 if (++buffer->curs_y > body_lines-1) {
388 buffer->curs_y = body_lines-1;
389 cmd_scroll_line_down(buffer);
390 return;
393 buffer->current_line = vl;
394 restore_cursor(buffer);
397 void
398 cmd_backward_char(struct buffer *buffer)
400 if (buffer->cpoff != 0)
401 buffer->cpoff--;
402 restore_cursor(buffer);
405 void
406 cmd_forward_char(struct buffer *buffer)
408 size_t len = 0;
410 if (buffer->current_line->line != NULL)
411 len = utf8_cplen(buffer->current_line->line);
412 if (++buffer->cpoff > len)
413 buffer->cpoff = len;
414 restore_cursor(buffer);
417 void
418 cmd_backward_paragraph(struct buffer *buffer)
420 do {
421 if (buffer->current_line == NULL ||
422 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
423 message("No previous paragraph");
424 return;
426 cmd_previous_line(buffer);
427 } while (buffer->current_line->line != NULL ||
428 buffer->current_line->parent->type != LINE_TEXT);
431 void
432 cmd_forward_paragraph(struct buffer *buffer)
434 do {
435 if (buffer->current_line == NULL ||
436 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
437 message("No next paragraph");
438 return;
440 cmd_next_line(buffer);
441 } while (buffer->current_line->line != NULL ||
442 buffer->current_line->parent->type != LINE_TEXT);
445 void
446 cmd_move_beginning_of_line(struct buffer *buffer)
448 buffer->cpoff = 0;
449 restore_cursor(buffer);
452 void
453 cmd_move_end_of_line(struct buffer *buffer)
455 struct vline *vl;
457 vl = buffer->current_line;
458 if (vl->line == NULL)
459 return;
460 buffer->cpoff = utf8_cplen(vl->line);
461 restore_cursor(buffer);
464 void
465 cmd_redraw(struct buffer *buffer)
467 handle_resize(0, 0, NULL);
470 void
471 cmd_scroll_line_up(struct buffer *buffer)
473 struct vline *vl;
475 if (buffer->line_off == 0)
476 return;
478 vl = nth_line(buffer, --buffer->line_off);
479 wscrl(body, -1);
480 wmove(body, 0, 0);
481 print_vline(body, vl);
483 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
484 restore_cursor(buffer);
487 void
488 cmd_scroll_line_down(struct buffer *buffer)
490 struct vline *vl;
492 vl = buffer->current_line;
493 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
494 return;
495 buffer->current_line = vl;
497 buffer->line_off++;
498 wscrl(body, 1);
500 if (buffer->line_max - buffer->line_off < (size_t)body_lines)
501 return;
503 vl = nth_line(buffer, buffer->line_off + body_lines-1);
504 wmove(body, body_lines-1, 0);
505 print_vline(body, vl);
507 restore_cursor(buffer);
510 void
511 cmd_scroll_up(struct buffer *buffer)
513 size_t off;
515 off = body_lines-1;
517 for (; off > 0; --off)
518 cmd_scroll_line_up(buffer);
521 void
522 cmd_scroll_down(struct buffer *buffer)
524 size_t off;
526 off = body_lines-1;
528 for (; off > 0; --off)
529 cmd_scroll_line_down(buffer);
532 void
533 cmd_beginning_of_buffer(struct buffer *buffer)
535 buffer->current_line = TAILQ_FIRST(&buffer->head);
536 buffer->line_off = 0;
537 buffer->curs_y = 0;
538 buffer->cpoff = 0;
539 restore_cursor(buffer);
542 void
543 cmd_end_of_buffer(struct buffer *buffer)
545 ssize_t off;
547 off = buffer->line_max - body_lines;
548 off = MAX(0, off);
550 buffer->line_off = off;
551 buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
553 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
554 buffer->cpoff = body_cols;
555 restore_cursor(buffer);
558 void
559 cmd_kill_telescope(struct buffer *buffer)
561 save_session();
562 event_loopbreak();
565 void
566 cmd_push_button(struct buffer *buffer)
568 struct vline *vl;
569 size_t nth;
571 nth = buffer->line_off + buffer->curs_y;
572 if (nth >= buffer->line_max)
573 return;
574 vl = nth_line(buffer, nth);
575 if (vl->parent->type != LINE_LINK)
576 return;
578 load_url_in_tab(current_tab(), vl->parent->alt);
581 void
582 cmd_push_button_new_tab(struct buffer *buffer)
584 struct vline *vl;
585 size_t nth;
587 nth = buffer->line_off + buffer->curs_y;
588 if (nth > buffer->line_max)
589 return;
590 vl = nth_line(buffer, nth);
591 if (vl->parent->type != LINE_LINK)
592 return;
594 new_tab(vl->parent->alt);
597 void
598 cmd_previous_button(struct buffer *buffer)
600 do {
601 if (buffer->current_line == NULL ||
602 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
603 message("No previous link");
604 return;
606 cmd_previous_line(buffer);
607 } while (buffer->current_line->parent->type != LINE_LINK);
610 void
611 cmd_next_button(struct buffer *buffer)
613 do {
614 if (buffer->current_line == NULL ||
615 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
616 message("No next link");
617 return;
619 cmd_next_line(buffer);
620 } while (buffer->current_line->parent->type != LINE_LINK);
623 void
624 cmd_previous_page(struct buffer *buffer)
626 struct tab *tab = current_tab();
628 if (!load_previous_page(tab))
629 message("No previous page");
630 else
631 start_loading_anim(tab);
634 void
635 cmd_next_page(struct buffer *buffer)
637 struct tab *tab = current_tab();
639 if (!load_next_page(tab))
640 message("No next page");
641 else
642 start_loading_anim(tab);
645 void
646 cmd_clear_minibuf(struct buffer *buffer)
648 handle_clear_minibuf(0, 0, NULL);
651 void
652 cmd_execute_extended_command(struct buffer *buffer)
654 size_t len;
656 if (in_minibuffer) {
657 message("We don't have enable-recursive-minibuffers");
658 return;
661 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
662 &eecmd_history);
664 len = sizeof(ministate.prompt);
665 strlcpy(ministate.prompt, "", len);
667 if (thiskey.meta)
668 strlcat(ministate.prompt, "M-", len);
670 strlcat(ministate.prompt, keyname(thiskey.key), len);
672 if (thiskey.meta)
673 strlcat(ministate.prompt, " ", len);
676 void
677 cmd_tab_close(struct buffer *buffer)
679 struct tab *tab, *t;
681 tab = current_tab();
682 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
683 TAILQ_NEXT(tab, tabs) == NULL) {
684 message("Can't close the only tab.");
685 return;
688 if (evtimer_pending(&tab->loadingev, NULL))
689 evtimer_del(&tab->loadingev);
691 stop_tab(tab);
693 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
694 t = TAILQ_NEXT(tab, tabs);
695 TAILQ_REMOVE(&tabshead, tab, tabs);
696 free(tab);
698 switch_to_tab(t);
701 void
702 cmd_tab_close_other(struct buffer *buffer)
704 struct tab *t, *i;
706 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
707 if (t->flags & TAB_CURRENT)
708 continue;
710 stop_tab(t);
711 TAILQ_REMOVE(&tabshead, t, tabs);
712 free(t);
716 void
717 cmd_tab_new(struct buffer *buffer)
719 new_tab(NEW_TAB_URL);
722 void
723 cmd_tab_next(struct buffer *buffer)
725 struct tab *tab, *t;
727 tab = current_tab();
728 tab->flags &= ~TAB_CURRENT;
730 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
731 t = TAILQ_FIRST(&tabshead);
732 t->flags |= TAB_CURRENT;
733 t->flags &= ~TAB_URGENT;
736 void
737 cmd_tab_previous(struct buffer *buffer)
739 struct tab *tab, *t;
741 tab = current_tab();
742 tab->flags &= ~TAB_CURRENT;
744 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
745 t = TAILQ_LAST(&tabshead, tabshead);
746 t->flags |= TAB_CURRENT;
747 t->flags &= ~TAB_URGENT;
750 void
751 cmd_tab_move(struct buffer *buffer)
753 struct tab *tab, *t;
755 tab = current_tab();
756 t = TAILQ_NEXT(tab, tabs);
757 TAILQ_REMOVE(&tabshead, tab, tabs);
759 if (t == NULL)
760 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
761 else
762 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
765 void
766 cmd_tab_move_to(struct buffer *buffer)
768 struct tab *tab, *t;
770 tab = current_tab();
771 t = TAILQ_PREV(tab, tabshead, tabs);
772 TAILQ_REMOVE(&tabshead, tab, tabs);
774 if (t == NULL) {
775 if (TAILQ_EMPTY(&tabshead))
776 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
777 else
778 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
779 } else
780 TAILQ_INSERT_BEFORE(t, tab, tabs);
783 void
784 cmd_load_url(struct buffer *buffer)
786 if (in_minibuffer) {
787 message("We don't have enable-recursive-minibuffers");
788 return;
791 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
792 &lu_history);
793 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
794 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
795 cmd_move_end_of_line(&ministate.buffer);
798 void
799 cmd_load_current_url(struct buffer *buffer)
801 struct tab *tab = current_tab();
803 if (in_minibuffer) {
804 message("We don't have enable-recursive-minibuffers");
805 return;
808 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
809 &lu_history);
810 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
811 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
812 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
815 void
816 cmd_bookmark_page(struct buffer *buffer)
818 struct tab *tab = current_tab();
820 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
821 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
822 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
823 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
826 void
827 cmd_list_bookmarks(struct buffer *buffer)
829 load_url_in_tab(current_tab(), "about:bookmarks");
832 void
833 cmd_toggle_help(struct buffer *buffer)
835 side_window = !side_window;
836 if (side_window)
837 recompute_help();
839 /*
840 * ugly hack, but otherwise the window doesn't get updated
841 * until I call handle_resize a second time (i.e. C-l). I
842 * will be happy to know why something like this is needed.
843 */
844 handle_resize_nodelay(0, 0, NULL);
845 handle_resize_nodelay(0, 0, NULL);
848 void
849 cmd_mini_delete_char(struct buffer *buffer)
851 char *c, *n;
853 if (!in_minibuffer) {
854 message("text is read-only");
855 return;
858 minibuffer_taint_hist();
860 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
861 if (*c == '\0')
862 return;
863 n = utf8_next_cp(c);
865 memmove(c, n, strlen(n)+1);
868 void
869 cmd_mini_delete_backward_char(struct buffer *buffer)
871 char *c, *p, *start;
873 if (!in_minibuffer) {
874 message("text is read-only");
875 return;
878 minibuffer_taint_hist();
880 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
881 start = buffer->current_line->line;
882 if (c == start)
883 return;
884 p = utf8_prev_cp(c-1, start);
886 memmove(p, c, strlen(c)+1);
887 buffer->cpoff--;
890 void
891 cmd_mini_kill_line(struct buffer *buffer)
893 char *c;
895 if (!in_minibuffer) {
896 message("text is read-only");
897 return;
900 minibuffer_taint_hist();
901 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
902 *c = '\0';
905 void
906 cmd_mini_abort(struct buffer *buffer)
908 if (!in_minibuffer)
909 return;
911 ministate.abortfn();
914 void
915 cmd_mini_complete_and_exit(struct buffer *buffer)
917 if (!in_minibuffer)
918 return;
920 minibuffer_taint_hist();
921 ministate.donefn();
924 void
925 cmd_mini_previous_history_element(struct buffer *buffer)
927 if (ministate.history == NULL) {
928 message("No history");
929 return;
932 if (ministate.hist_cur == NULL ||
933 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
934 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
935 ministate.hist_off = ministate.history->len - 1;
936 if (ministate.hist_cur == NULL)
937 message("No prev item");
938 } else {
939 ministate.hist_off--;
942 if (ministate.hist_cur != NULL)
943 buffer->current_line->line = ministate.hist_cur->h;
946 void
947 cmd_mini_next_history_element(struct buffer *buffer)
949 if (ministate.history == NULL) {
950 message("No history");
951 return;
954 if (ministate.hist_cur == NULL ||
955 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
956 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
957 ministate.hist_off = 0;
958 if (ministate.hist_cur == NULL)
959 message("No next item");
960 } else {
961 ministate.hist_off++;
964 if (ministate.hist_cur != NULL)
965 buffer->current_line->line = ministate.hist_cur->h;
968 static void
969 global_key_unbound(void)
971 message("%s is undefined", keybuf);
974 static void
975 minibuffer_hist_save_entry(void)
977 struct hist *hist;
979 if (ministate.history == NULL)
980 return;
982 if ((hist = calloc(1, sizeof(*hist))) == NULL)
983 abort();
985 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
987 if (TAILQ_EMPTY(&ministate.history->head))
988 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
989 else
990 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
991 ministate.history->len++;
994 /*
995 * taint the minibuffer cache: if we're currently showing a history
996 * element, copy that to the current buf and reset the "history
997 * navigation" thing.
998 */
999 static void
1000 minibuffer_taint_hist(void)
1002 if (ministate.hist_cur == NULL)
1003 return;
1005 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1006 ministate.hist_cur = NULL;
1009 static void
1010 minibuffer_self_insert(void)
1012 char *c, tmp[5] = {0};
1013 size_t len;
1015 minibuffer_taint_hist();
1017 if (thiskey.cp == 0)
1018 return;
1020 len = utf8_encode(thiskey.cp, tmp);
1021 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1022 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1023 return;
1025 memmove(c + len, c, strlen(c)+1);
1026 memcpy(c, tmp, len);
1027 ministate.buffer.cpoff++;
1030 static void
1031 eecmd_self_insert(void)
1033 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1034 !unicode_isgraph(thiskey.cp)) {
1035 global_key_unbound();
1036 return;
1039 minibuffer_self_insert();
1042 static void
1043 eecmd_select(void)
1045 struct cmds *cmd;
1047 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1048 if (!strcmp(cmd->cmd, ministate.buf)) {
1049 exit_minibuffer();
1050 minibuffer_hist_save_entry();
1051 cmd->fn(current_buffer());
1052 return;
1056 message("No match");
1059 static void
1060 ir_self_insert(void)
1062 minibuffer_self_insert();
1065 static void
1066 ir_select(void)
1068 char buf[1025] = {0};
1069 struct phos_uri uri;
1070 struct tab *tab;
1072 tab = current_tab();
1074 exit_minibuffer();
1075 minibuffer_hist_save_entry();
1077 /* a bit ugly but... */
1078 memcpy(&uri, &tab->uri, sizeof(tab->uri));
1079 phos_uri_set_query(&uri, ministate.buf);
1080 phos_serialize_uri(&uri, buf, sizeof(buf));
1081 load_url_in_tab(tab, buf);
1084 static void
1085 lu_self_insert(void)
1087 if (thiskey.meta || unicode_isspace(thiskey.key) ||
1088 !unicode_isgraph(thiskey.key)) {
1089 global_key_unbound();
1090 return;
1093 minibuffer_self_insert();
1096 static void
1097 lu_select(void)
1099 exit_minibuffer();
1100 minibuffer_hist_save_entry();
1101 load_url_in_tab(current_tab(), ministate.buf);
1104 static void
1105 bp_select(void)
1107 exit_minibuffer();
1108 if (*ministate.buf != '\0')
1109 add_to_bookmarks(ministate.buf);
1110 else
1111 message("Abort.");
1114 static void
1115 yornp_self_insert(void)
1117 if (thiskey.key != 'y' && thiskey.key != 'n') {
1118 message("Please answer y or n");
1119 return;
1122 exit_minibuffer();
1123 yornp_cb(thiskey.key == 'y', yornp_data);
1126 static void
1127 yornp_abort(void)
1129 exit_minibuffer();
1130 yornp_cb(0, yornp_data);
1133 static void
1134 read_self_insert(void)
1136 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1137 global_key_unbound();
1138 return;
1141 minibuffer_self_insert();
1144 static void
1145 read_abort(void)
1147 exit_minibuffer();
1148 read_cb(NULL, read_data);
1151 static void
1152 read_select(void)
1154 exit_minibuffer();
1155 minibuffer_hist_save_entry();
1156 read_cb(ministate.buf, read_data);
1159 static struct vline *
1160 nth_line(struct buffer *buffer, size_t n)
1162 struct vline *vl;
1163 size_t i;
1165 i = 0;
1166 TAILQ_FOREACH(vl, &buffer->head, vlines) {
1167 if (i == n)
1168 return vl;
1169 i++;
1172 /* unreachable */
1173 abort();
1176 static struct tab *
1177 current_tab(void)
1179 struct tab *t;
1181 TAILQ_FOREACH(t, &tabshead, tabs) {
1182 if (t->flags & TAB_CURRENT)
1183 return t;
1186 /* unreachable */
1187 abort();
1190 static struct buffer *
1191 current_buffer(void)
1193 if (in_minibuffer)
1194 return &ministate.buffer;
1195 return &current_tab()->buffer;
1198 static int
1199 readkey(void)
1201 uint32_t state = 0;
1203 if ((thiskey.key = wgetch(body)) == ERR)
1204 return 0;
1206 thiskey.meta = thiskey.key == 27;
1207 if (thiskey.meta) {
1208 thiskey.key = wgetch(body);
1209 if (thiskey.key == ERR || thiskey.key == 27) {
1210 thiskey.meta = 0;
1211 thiskey.key = 27;
1215 thiskey.cp = 0;
1216 if ((unsigned int)thiskey.key < UINT8_MAX) {
1217 while (1) {
1218 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1219 break;
1220 if ((thiskey.key = wgetch(body)) == ERR) {
1221 message("Error decoding user input");
1222 return 0;
1227 return 1;
1230 static void
1231 dispatch_stdio(int fd, short ev, void *d)
1233 struct keymap *k;
1234 const char *keyname;
1235 char tmp[5] = {0};
1237 if (!readkey())
1238 return;
1240 if (keybuf[0] != '\0')
1241 strlcat(keybuf, " ", sizeof(keybuf));
1242 if (thiskey.meta)
1243 strlcat(keybuf, "M-", sizeof(keybuf));
1244 if (thiskey.cp != 0) {
1245 utf8_encode(thiskey.cp, tmp);
1246 strlcat(keybuf, tmp, sizeof(keybuf));
1247 } else {
1248 if ((keyname = unkbd(thiskey.key)) != NULL)
1249 strlcat(keybuf, keyname, sizeof(keybuf));
1250 else {
1251 tmp[0] = thiskey.key;
1252 strlcat(keybuf, tmp, sizeof(keybuf));
1256 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1257 if (k->meta == thiskey.meta &&
1258 k->key == thiskey.key) {
1259 if (k->fn == NULL)
1260 current_map = &k->map;
1261 else {
1262 current_map = base_map;
1263 strlcpy(keybuf, "", sizeof(keybuf));
1264 k->fn(current_buffer());
1266 goto done;
1270 if (current_map->unhandled_input != NULL)
1271 current_map->unhandled_input();
1272 else {
1273 global_key_unbound();
1276 strlcpy(keybuf, "", sizeof(keybuf));
1277 current_map = base_map;
1279 done:
1280 if (side_window)
1281 recompute_help();
1283 redraw_tab(current_tab());
1286 static void
1287 handle_clear_minibuf(int fd, short ev, void *d)
1289 free(ministate.curmesg);
1290 ministate.curmesg = NULL;
1292 redraw_minibuffer();
1293 if (in_minibuffer) {
1294 wrefresh(body);
1295 wrefresh(minibuf);
1296 } else {
1297 wrefresh(minibuf);
1298 wrefresh(body);
1302 static void
1303 handle_resize(int sig, short ev, void *d)
1305 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1306 event_del(&resizeev);
1308 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1309 evtimer_add(&resizeev, &resize_timer);
1312 static void
1313 handle_resize_nodelay(int s, short ev, void *d)
1315 struct tab *tab;
1317 endwin();
1318 refresh();
1319 clear();
1321 /* move and resize the windows, in reverse order! */
1323 mvwin(minibuf, LINES-1, 0);
1324 wresize(minibuf, 1, COLS);
1326 mvwin(modeline, LINES-2, 0);
1327 wresize(modeline, 1, COLS);
1329 body_lines = LINES-3;
1330 body_cols = COLS;
1332 if (side_window) {
1333 help_cols = 0.3 * COLS;
1334 help_lines = LINES-3;
1335 mvwin(help, 1, 0);
1336 wresize(help, help_lines, help_cols);
1338 wrap_page(&helpwin, help_cols);
1340 body_cols = COLS - help_cols - 1;
1341 mvwin(body, 1, help_cols);
1342 } else
1343 mvwin(body, 1, 0);
1345 wresize(body, body_lines, body_cols);
1347 wresize(tabline, 1, COLS);
1349 tab = current_tab();
1351 wrap_page(&tab->buffer, body_cols);
1352 redraw_tab(tab);
1355 static int
1356 wrap_page(struct buffer *buffer, int width)
1358 struct line *l;
1359 const struct line *orig;
1360 struct vline *vl;
1361 const char *prfx;
1363 orig = buffer->current_line == NULL
1364 ? NULL
1365 : buffer->current_line->parent;
1366 buffer->current_line = NULL;
1368 buffer->curs_y = 0;
1369 buffer->line_off = 0;
1371 empty_vlist(buffer);
1373 TAILQ_FOREACH(l, &buffer->page.head, lines) {
1374 prfx = line_prefixes[l->type].prfx1;
1375 switch (l->type) {
1376 case LINE_TEXT:
1377 case LINE_LINK:
1378 case LINE_TITLE_1:
1379 case LINE_TITLE_2:
1380 case LINE_TITLE_3:
1381 case LINE_ITEM:
1382 case LINE_QUOTE:
1383 case LINE_PRE_START:
1384 case LINE_PRE_END:
1385 wrap_text(buffer, prfx, l, width);
1386 break;
1387 case LINE_PRE_CONTENT:
1388 hardwrap_text(buffer, l, 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, 0);
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, A_REVERSE);
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, "-");
1616 static void
1617 redraw_minibuffer(void)
1619 struct tab *tab;
1620 size_t off_y, off_x = 0;
1621 char *start, *c;
1623 werase(minibuf);
1625 if (in_minibuffer) {
1626 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1627 if (ministate.hist_cur != NULL)
1628 wprintw(minibuf, "(%zu/%zu) ",
1629 ministate.hist_off + 1,
1630 ministate.history->len);
1632 getyx(minibuf, off_y, off_x);
1634 start = ministate.hist_cur != NULL
1635 ? ministate.hist_cur->h
1636 : ministate.buf;
1637 c = utf8_nth(ministate.buffer.current_line->line,
1638 ministate.buffer.cpoff);
1639 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1640 start = utf8_next_cp(start);
1643 waddstr(minibuf, start);
1646 if (ministate.curmesg != NULL)
1647 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1648 ministate.curmesg);
1650 if (!in_minibuffer && ministate.curmesg == NULL)
1651 waddstr(minibuf, keybuf);
1653 /* If nothing else, show the URL at point */
1654 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1655 tab = current_tab();
1656 if (tab->buffer.current_line != NULL &&
1657 tab->buffer.current_line->parent->type == LINE_LINK)
1658 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1661 if (in_minibuffer)
1662 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1665 static void
1666 redraw_tab(struct tab *tab)
1668 if (side_window) {
1669 redraw_help();
1670 wnoutrefresh(help);
1673 redraw_tabline();
1674 redraw_body(tab);
1675 redraw_modeline(tab);
1676 redraw_minibuffer();
1678 wnoutrefresh(tabline);
1679 wnoutrefresh(modeline);
1681 if (in_minibuffer) {
1682 wnoutrefresh(body);
1683 wnoutrefresh(minibuf);
1684 } else {
1685 wnoutrefresh(minibuf);
1686 wnoutrefresh(body);
1689 doupdate();
1692 static void
1693 emit_help_item(char *prfx, void *fn)
1695 struct line *l;
1696 struct cmds *cmd;
1698 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1699 if (fn == cmd->fn)
1700 break;
1702 assert(cmd != NULL);
1704 if ((l = calloc(1, sizeof(*l))) == NULL)
1705 abort();
1707 l->type = LINE_TEXT;
1708 l->alt = NULL;
1710 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1712 if (TAILQ_EMPTY(&helpwin.page.head))
1713 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1714 else
1715 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1718 static void
1719 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1721 struct keymap *k;
1722 char p[32];
1723 const char *kn;
1725 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1726 strlcpy(p, prfx, sizeof(p));
1727 if (*p != '\0')
1728 strlcat(p, " ", sizeof(p));
1729 if (k->meta)
1730 strlcat(p, "M-", sizeof(p));
1731 if ((kn = unkbd(k->key)) != NULL)
1732 strlcat(p, kn, sizeof(p));
1733 else
1734 strlcat(p, keyname(k->key), sizeof(p));
1736 if (k->fn == NULL)
1737 rec_compute_help(&k->map, p, sizeof(p));
1738 else
1739 emit_help_item(p, k->fn);
1743 static void
1744 recompute_help(void)
1746 char p[32] = { 0 };
1748 empty_vlist(&helpwin);
1749 empty_linelist(&helpwin);
1750 rec_compute_help(current_map, p, sizeof(p));
1751 wrap_page(&helpwin, help_cols);
1754 static void
1755 vmessage(const char *fmt, va_list ap)
1757 if (evtimer_pending(&clminibufev, NULL))
1758 evtimer_del(&clminibufev);
1759 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1760 evtimer_add(&clminibufev, &clminibufev_timer);
1762 free(ministate.curmesg);
1764 /* TODO: what to do if the allocation fails here? */
1765 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1766 ministate.curmesg = NULL;
1768 redraw_minibuffer();
1769 if (in_minibuffer) {
1770 wrefresh(body);
1771 wrefresh(minibuf);
1772 } else {
1773 wrefresh(minibuf);
1774 wrefresh(body);
1778 static void
1779 message(const char *fmt, ...)
1781 va_list ap;
1783 va_start(ap, fmt);
1784 vmessage(fmt, ap);
1785 va_end(ap);
1788 static void
1789 start_loading_anim(struct tab *tab)
1791 if (tab->loading_anim)
1792 return;
1793 tab->loading_anim = 1;
1794 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1795 evtimer_add(&tab->loadingev, &loadingev_timer);
1798 static void
1799 update_loading_anim(int fd, short ev, void *d)
1801 struct tab *tab = d;
1803 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1805 if (tab->flags & TAB_CURRENT) {
1806 redraw_modeline(tab);
1807 wrefresh(modeline);
1808 wrefresh(body);
1809 if (in_minibuffer)
1810 wrefresh(minibuf);
1813 evtimer_add(&tab->loadingev, &loadingev_timer);
1816 static void
1817 stop_loading_anim(struct tab *tab)
1819 if (!tab->loading_anim)
1820 return;
1821 evtimer_del(&tab->loadingev);
1822 tab->loading_anim = 0;
1823 tab->loading_anim_step = 0;
1825 if (!(tab->flags & TAB_CURRENT))
1826 return;
1828 redraw_modeline(tab);
1830 wrefresh(modeline);
1831 wrefresh(body);
1832 if (in_minibuffer)
1833 wrefresh(minibuf);
1836 static void
1837 load_url_in_tab(struct tab *tab, const char *url)
1839 message("Loading %s...", url);
1840 start_loading_anim(tab);
1841 load_url(tab, url);
1843 tab->buffer.curs_x = 0;
1844 tab->buffer.curs_y = 0;
1845 redraw_tab(tab);
1848 static void
1849 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1850 void (*abortfn)(void), struct histhead *hist)
1852 in_minibuffer = 1;
1853 base_map = &minibuffer_map;
1854 current_map = &minibuffer_map;
1856 base_map->unhandled_input = self_insert_fn;
1858 ministate.donefn = donefn;
1859 ministate.abortfn = abortfn;
1860 memset(ministate.buf, 0, sizeof(ministate.buf));
1861 ministate.buffer.current_line = &ministate.vline;
1862 ministate.buffer.current_line->line = ministate.buf;
1863 ministate.buffer.cpoff = 0;
1864 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1866 ministate.history = hist;
1867 ministate.hist_cur = NULL;
1868 ministate.hist_off = 0;
1871 static void
1872 exit_minibuffer(void)
1874 werase(minibuf);
1876 in_minibuffer = 0;
1877 base_map = &global_map;
1878 current_map = &global_map;
1881 static void
1882 switch_to_tab(struct tab *tab)
1884 struct tab *t;
1886 TAILQ_FOREACH(t, &tabshead, tabs) {
1887 t->flags &= ~TAB_CURRENT;
1890 tab->flags |= TAB_CURRENT;
1891 tab->flags &= ~TAB_URGENT;
1894 unsigned int
1895 tab_new_id(void)
1897 return tab_counter++;
1900 static struct tab *
1901 new_tab(const char *url)
1903 struct tab *tab;
1905 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1906 event_loopbreak();
1907 return NULL;
1909 tab->fd = -1;
1911 TAILQ_INIT(&tab->hist.head);
1913 TAILQ_INIT(&tab->buffer.head);
1915 tab->id = tab_new_id();
1916 switch_to_tab(tab);
1918 if (TAILQ_EMPTY(&tabshead))
1919 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1920 else
1921 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1923 load_url_in_tab(tab, url);
1924 return tab;
1927 static void
1928 session_new_tab_cb(const char *url)
1930 new_tab(url);
1933 static void
1934 usage(void)
1936 fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1939 int
1940 ui_init(int argc, char * const *argv)
1942 const char *url = NEW_TAB_URL;
1943 int ch;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1946 switch (ch) {
1947 default:
1948 usage();
1949 return 0;
1952 argc -= optind;
1953 argv += optind;
1955 if (argc != 0)
1956 url = argv[0];
1958 setlocale(LC_ALL, "");
1960 TAILQ_INIT(&global_map.m);
1961 global_map.unhandled_input = global_key_unbound;
1963 TAILQ_INIT(&minibuffer_map.m);
1965 TAILQ_INIT(&eecmd_history.head);
1966 TAILQ_INIT(&ir_history.head);
1967 TAILQ_INIT(&lu_history.head);
1969 ministate.line.type = LINE_TEXT;
1970 ministate.vline.parent = &ministate.line;
1971 ministate.buffer.current_line = &ministate.vline;
1973 /* initialize help window */
1974 TAILQ_INIT(&helpwin.head);
1976 base_map = &global_map;
1977 current_map = &global_map;
1978 load_default_keys();
1980 initscr();
1981 raw();
1982 noecho();
1984 nonl();
1985 intrflush(stdscr, FALSE);
1987 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1988 return 0;
1989 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1990 return 0;
1991 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1992 return 0;
1993 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1994 return 0;
1995 if ((help = newwin(1, 1, 1, 0)) == NULL)
1996 return 0;
1998 body_lines = LINES-3;
1999 body_cols = COLS;
2001 keypad(body, TRUE);
2002 scrollok(body, TRUE);
2004 /* non-blocking input */
2005 wtimeout(body, 0);
2007 mvwprintw(body, 0, 0, "");
2009 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2010 event_add(&stdioev, NULL);
2012 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2013 signal_add(&winchev, NULL);
2015 load_last_session(session_new_tab_cb);
2016 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2017 new_tab(url);
2019 return 1;
2022 void
2023 ui_on_tab_loaded(struct tab *tab)
2025 stop_loading_anim(tab);
2026 message("Loaded %s", tab->hist_cur->h);
2028 redraw_tabline();
2029 wrefresh(tabline);
2030 if (in_minibuffer)
2031 wrefresh(minibuf);
2032 else
2033 wrefresh(body);
2036 void
2037 ui_on_tab_refresh(struct tab *tab)
2039 wrap_page(&tab->buffer, body_cols);
2040 if (tab->flags & TAB_CURRENT) {
2041 restore_cursor(&tab->buffer);
2042 redraw_tab(tab);
2043 } else
2044 tab->flags |= TAB_URGENT;
2047 void
2048 ui_require_input(struct tab *tab, int hide)
2050 /* TODO: hard-switching to another tab is ugly */
2051 switch_to_tab(tab);
2053 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2054 &ir_history);
2055 strlcpy(ministate.prompt, "Input required: ",
2056 sizeof(ministate.prompt));
2057 redraw_tab(tab);
2060 void
2061 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2062 unsigned int data)
2064 size_t len;
2066 if (in_minibuffer) {
2067 fn(0, data);
2068 return;
2071 yornp_cb = fn;
2072 yornp_data = data;
2073 enter_minibuffer(yornp_self_insert, yornp_self_insert,
2074 yornp_abort, NULL);
2076 len = sizeof(ministate.prompt);
2077 strlcpy(ministate.prompt, prompt, len);
2078 strlcat(ministate.prompt, " (y or n) ", len);
2079 redraw_tab(current_tab());
2082 void
2083 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2084 unsigned int data)
2086 size_t len;
2088 if (in_minibuffer)
2089 return;
2091 read_cb = fn;
2092 read_data = data;
2093 enter_minibuffer(read_self_insert, read_select, read_abort,
2094 &read_history);
2096 len = sizeof(ministate.prompt);
2097 strlcpy(ministate.prompt, prompt, len);
2098 strlcat(ministate.prompt, ": ", len);
2099 redraw_tab(current_tab());
2102 void
2103 ui_notify(const char *fmt, ...)
2105 va_list ap;
2107 va_start(ap, fmt);
2108 vmessage(fmt, ap);
2109 va_end(ap);
2112 void
2113 ui_end(void)
2115 endwin();