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''.
32 *
33 * TODO: make the text formatting on-demand.
34 *
35 */
37 #include <telescope.h>
39 #include <ctype.h>
40 #include <curses.h>
41 #include <event.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
51 #define NEW_TAB_URL "about:new"
53 static struct event stdioev, winchev;
55 static void load_default_keys(void);
56 static void empty_vlist(struct tab*);
57 static void restore_cursor(struct tab *);
59 static void cmd_previous_line(struct tab*);
60 static void cmd_next_line(struct tab*);
61 static void cmd_backward_char(struct tab*);
62 static void cmd_forward_char(struct tab*);
63 static void cmd_backward_paragraph(struct tab*);
64 static void cmd_forward_paragraph(struct tab*);
65 static void cmd_move_beginning_of_line(struct tab*);
66 static void cmd_move_end_of_line(struct tab*);
67 static void cmd_redraw(struct tab*);
68 static void cmd_scroll_line_down(struct tab*);
69 static void cmd_scroll_line_up(struct tab*);
70 static void cmd_scroll_up(struct tab*);
71 static void cmd_scroll_down(struct tab*);
72 static void cmd_beginning_of_buffer(struct tab*);
73 static void cmd_end_of_buffer(struct tab*);
74 static void cmd_kill_telescope(struct tab*);
75 static void cmd_push_button(struct tab*);
76 static void cmd_push_button_new_tab(struct tab*);
77 static void cmd_previous_button(struct tab*);
78 static void cmd_next_button(struct tab*);
79 static void cmd_previous_page(struct tab*);
80 static void cmd_next_page(struct tab*);
81 static void cmd_clear_minibuf(struct tab*);
82 static void cmd_execute_extended_command(struct tab*);
83 static void cmd_tab_close(struct tab*);
84 static void cmd_tab_close_other(struct tab*);
85 static void cmd_tab_new(struct tab*);
86 static void cmd_tab_next(struct tab*);
87 static void cmd_tab_previous(struct tab*);
88 static void cmd_load_url(struct tab*);
89 static void cmd_load_current_url(struct tab*);
90 static void cmd_bookmark_page(struct tab*);
91 static void cmd_goto_bookmarks(struct tab*);
93 static void global_key_unbound(void);
95 static void cmd_mini_delete_char(struct tab*);
96 static void cmd_mini_delete_backward_char(struct tab*);
97 static void cmd_mini_forward_char(struct tab*);
98 static void cmd_mini_backward_char(struct tab*);
99 static void cmd_mini_move_end_of_line(struct tab*);
100 static void cmd_mini_move_beginning_of_line(struct tab*);
101 static void cmd_mini_kill_line(struct tab*);
102 static void cmd_mini_abort(struct tab*);
103 static void cmd_mini_complete_and_exit(struct tab*);
104 static void cmd_mini_previous_history_element(struct tab*);
105 static void cmd_mini_next_history_element(struct tab*);
107 static void minibuffer_hist_save_entry(void);
108 static void minibuffer_taint_hist(void);
109 static void minibuffer_self_insert(void);
110 static void eecmd_self_insert(void);
111 static void eecmd_select(void);
112 static void ir_self_insert(void);
113 static void ir_select(void);
114 static void lu_self_insert(void);
115 static void lu_select(void);
116 static void bp_select(void);
118 static struct vline *nth_line(struct tab*, size_t);
119 static struct tab *current_tab(void);
120 static void dispatch_stdio(int, short, void*);
121 static void handle_clear_minibuf(int, short, void*);
122 static void handle_resize(int, short, void*);
123 static int wrap_page(struct tab*);
124 static void print_vline(struct vline*);
125 static void redraw_tabline(void);
126 static void redraw_body(struct tab*);
127 static void redraw_modeline(struct tab*);
128 static void redraw_minibuffer(void);
129 static void redraw_tab(struct tab*);
130 static void vmessage(const char*, va_list);
131 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
132 static void start_loading_anim(struct tab*);
133 static void update_loading_anim(int, short, void*);
134 static void stop_loading_anim(struct tab*);
135 static void load_url_in_tab(struct tab*, const char*);
136 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
137 static void exit_minibuffer(void);
138 static void switch_to_tab(struct tab*);
139 static struct tab *new_tab(const char*);
141 static struct { int meta, key; } thiskey;
143 static WINDOW *tabline, *body, *modeline, *minibuf;
144 static int body_lines, body_cols;
146 static struct event clminibufev;
147 static struct timeval clminibufev_timer = { 5, 0 };
148 static struct timeval loadingev_timer = { 0, 250000 };
150 static uint32_t tab_counter;
152 static char keybuf[64];
154 struct kmap global_map,
155 minibuffer_map,
156 *current_map,
157 *base_map;
159 static struct histhead eecmd_history,
160 ir_history,
161 lu_history;
163 static int in_minibuffer;
165 static struct {
166 char *curmesg;
168 char buf[1025];
169 size_t off, len;
170 char prompt[32];
171 void (*donefn)(void);
172 void (*abortfn)(void);
174 struct histhead *history;
175 struct hist *hist_cur;
176 size_t hist_off;
177 } ministate;
179 struct lineprefix {
180 const char *prfx1;
181 const char *prfx2;
182 } line_prefixes[] = {
183 [LINE_TEXT] = { "", "" },
184 [LINE_LINK] = { "=> ", " " },
185 [LINE_TITLE_1] = { "# ", " " },
186 [LINE_TITLE_2] = { "## ", " " },
187 [LINE_TITLE_3] = { "### ", " " },
188 [LINE_ITEM] = { "* ", " " },
189 [LINE_QUOTE] = { "> ", "> " },
190 [LINE_PRE_START] = { "```", " " },
191 [LINE_PRE_CONTENT] = { "", "" },
192 [LINE_PRE_END] = { "```", "```" },
193 };
195 static struct line_face {
196 int prefix_prop;
197 int text_prop;
198 } line_faces[] = {
199 [LINE_TEXT] = { 0, 0 },
200 [LINE_LINK] = { 0, A_UNDERLINE },
201 [LINE_TITLE_1] = { A_BOLD, A_BOLD },
202 [LINE_TITLE_2] = { A_BOLD, A_BOLD },
203 [LINE_TITLE_3] = { A_BOLD, A_BOLD },
204 [LINE_ITEM] = { 0, 0 },
205 [LINE_QUOTE] = { 0, A_DIM },
206 [LINE_PRE_START] = { 0, 0 },
207 [LINE_PRE_CONTENT] = { 0, 0 },
208 [LINE_PRE_END] = { 0, 0 },
209 };
211 static struct tab_face {
212 int background, tab, current_tab;
213 } tab_face = {
214 A_REVERSE, A_REVERSE, A_NORMAL
215 };
217 static void
218 empty_vlist(struct tab *tab)
220 struct vline *vl, *t;
222 tab->s.current_line = NULL;
223 tab->s.line_max = 0;
225 TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
226 TAILQ_REMOVE(&tab->s.head, vl, vlines);
227 free(vl->line);
228 free(vl);
232 static inline void
233 global_set_key(const char *key, void (*fn)(struct tab*))
235 if (!kmap_define_key(&global_map, key, fn))
236 _exit(1);
239 static inline void
240 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
242 if (!kmap_define_key(&minibuffer_map, key, fn))
243 _exit(1);
246 static void
247 load_default_keys(void)
249 /* === global map === */
251 /* emacs */
252 global_set_key("C-p", cmd_previous_line);
253 global_set_key("C-n", cmd_next_line);
254 global_set_key("C-f", cmd_forward_char);
255 global_set_key("C-b", cmd_backward_char);
256 global_set_key("M-{", cmd_backward_paragraph);
257 global_set_key("M-}", cmd_forward_paragraph);
258 global_set_key("C-a", cmd_move_beginning_of_line);
259 global_set_key("C-e", cmd_move_end_of_line);
261 global_set_key("M-v", cmd_scroll_up);
262 global_set_key("C-v", cmd_scroll_down);
263 global_set_key("M-space", cmd_scroll_up);
264 global_set_key("space", cmd_scroll_down);
266 global_set_key("C-x C-c", cmd_kill_telescope);
268 global_set_key("C-g", cmd_clear_minibuf);
270 global_set_key("M-x", cmd_execute_extended_command);
271 global_set_key("C-x C-f", cmd_load_url);
272 global_set_key("C-x M-f", cmd_load_current_url);
274 global_set_key("C-x t 0", cmd_tab_close);
275 global_set_key("C-x t 1", cmd_tab_close_other);
276 global_set_key("C-x t 2", cmd_tab_new);
277 global_set_key("C-x t o", cmd_tab_next);
278 global_set_key("C-x t O", cmd_tab_previous);
280 global_set_key("M-<", cmd_beginning_of_buffer);
281 global_set_key("M->", cmd_end_of_buffer);
283 global_set_key("C-M-b", cmd_previous_page);
284 global_set_key("C-M-f", cmd_next_page);
286 global_set_key("<f7> a", cmd_bookmark_page);
287 global_set_key("<f7> <f7>", cmd_goto_bookmarks);
289 /* vi/vi-like */
290 global_set_key("k", cmd_previous_line);
291 global_set_key("j", cmd_next_line);
292 global_set_key("l", cmd_forward_char);
293 global_set_key("h", cmd_backward_char);
294 global_set_key("{", cmd_backward_paragraph);
295 global_set_key("}", cmd_forward_paragraph);
296 global_set_key("^", cmd_move_beginning_of_line);
297 global_set_key("$", cmd_move_end_of_line);
299 global_set_key("K", cmd_scroll_line_up);
300 global_set_key("J", cmd_scroll_line_down);
302 global_set_key("g D", cmd_tab_close);
303 global_set_key("g N", cmd_tab_new);
304 global_set_key("g t", cmd_tab_next);
305 global_set_key("g T", cmd_tab_previous);
307 global_set_key("g g", cmd_beginning_of_buffer);
308 global_set_key("G", cmd_end_of_buffer);
310 global_set_key("H", cmd_previous_page);
311 global_set_key("L", cmd_next_page);
313 /* tmp */
314 global_set_key("q", cmd_kill_telescope);
316 global_set_key("esc", cmd_clear_minibuf);
318 global_set_key(":", cmd_execute_extended_command);
320 /* cua */
321 global_set_key("<up>", cmd_previous_line);
322 global_set_key("<down>", cmd_next_line);
323 global_set_key("<right>", cmd_forward_char);
324 global_set_key("<left>", cmd_backward_char);
325 global_set_key("<prior>", cmd_scroll_up);
326 global_set_key("<next>", cmd_scroll_down);
328 global_set_key("M-<left>", cmd_previous_page);
329 global_set_key("M-<right>", cmd_next_page);
331 /* "ncurses standard" */
332 global_set_key("C-l", cmd_redraw);
334 /* global */
335 global_set_key("C-m", cmd_push_button);
336 global_set_key("M-enter", cmd_push_button_new_tab);
337 global_set_key("M-tab", cmd_previous_button);
338 global_set_key("tab", cmd_next_button);
340 /* === minibuffer map === */
341 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
342 minibuffer_set_key("C-g", cmd_mini_abort);
343 minibuffer_set_key("esc", cmd_mini_abort);
344 minibuffer_set_key("C-d", cmd_mini_delete_char);
345 minibuffer_set_key("del", cmd_mini_delete_backward_char);
347 minibuffer_set_key("C-f", cmd_mini_forward_char);
348 minibuffer_set_key("C-b", cmd_mini_backward_char);
349 minibuffer_set_key("<right>", cmd_mini_forward_char);
350 minibuffer_set_key("<left>", cmd_mini_backward_char);
351 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
352 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
353 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
354 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
355 minibuffer_set_key("C-k", cmd_mini_kill_line);
357 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
358 minibuffer_set_key("M-n", cmd_mini_next_history_element);
359 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
360 minibuffer_set_key("<down>", cmd_mini_next_history_element);
363 static void
364 restore_cursor(struct tab *tab)
366 struct vline *vl;
367 const char *prfx;
369 vl =tab->s.current_line;
370 if (vl == NULL || vl->line == NULL)
371 tab->s.curs_x = tab->s.line_x = 0;
372 else
373 tab->s.line_x = MIN(tab->s.line_x, strlen(vl->line));
375 if (vl != NULL) {
376 prfx = line_prefixes[vl->parent->type].prfx1;
377 tab->s.curs_x = tab->s.line_x + strlen(prfx);
380 wmove(body, tab->s.curs_y, tab->s.curs_x);
383 static void
384 cmd_previous_line(struct tab *tab)
386 struct vline *vl;
388 if (tab->s.current_line == NULL
389 || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
390 return;
392 if (--tab->s.curs_y < 0) {
393 tab->s.curs_y = 0;
394 cmd_scroll_line_up(tab);
395 return;
398 tab->s.current_line = vl;
399 restore_cursor(tab);
402 static void
403 cmd_next_line(struct tab *tab)
405 struct vline *vl;
407 if (tab->s.current_line == NULL
408 || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
409 return;
411 if (++tab->s.curs_y > body_lines-1) {
412 tab->s.curs_y = body_lines-1;
413 cmd_scroll_line_down(tab);
414 return;
417 tab->s.current_line = vl;
418 restore_cursor(tab);
421 static void
422 cmd_backward_char(struct tab *tab)
424 if (tab->s.line_x != 0)
425 tab->s.line_x--;
426 restore_cursor(tab);
429 static void
430 cmd_forward_char(struct tab *tab)
432 tab->s.line_x++;
433 restore_cursor(tab);
436 static void
437 cmd_backward_paragraph(struct tab *tab)
439 do {
440 if (tab->s.current_line == NULL ||
441 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
442 message("No previous paragraph");
443 return;
445 cmd_previous_line(tab);
446 } while (tab->s.current_line->line != NULL ||
447 tab->s.current_line->parent->type != LINE_TEXT);
450 static void
451 cmd_forward_paragraph(struct tab *tab)
453 do {
454 if (tab->s.current_line == NULL ||
455 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
456 message("No next paragraph");
457 return;
459 cmd_next_line(tab);
460 } while (tab->s.current_line->line != NULL ||
461 tab->s.current_line->parent->type != LINE_TEXT);
464 static void
465 cmd_move_beginning_of_line(struct tab *tab)
467 tab->s.line_x = 0;
468 restore_cursor(tab);
471 static void
472 cmd_move_end_of_line(struct tab *tab)
474 struct vline *vl;
476 vl = tab->s.current_line;
477 if (vl->line == NULL)
478 return;
479 tab->s.line_x = body_cols;
480 restore_cursor(tab);
483 static void
484 cmd_redraw(struct tab *tab)
486 handle_resize(0, 0, NULL);
489 static void
490 cmd_scroll_line_up(struct tab *tab)
492 struct vline *vl;
494 if (tab->s.line_off == 0)
495 return;
497 vl = nth_line(tab, --tab->s.line_off);
498 wscrl(body, -1);
499 wmove(body, 0, 0);
500 print_vline(vl);
502 tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
505 static void
506 cmd_scroll_line_down(struct tab *tab)
508 struct vline *vl;
510 vl = tab->s.current_line;
511 if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
512 return;
513 tab->s.current_line = vl;
515 tab->s.line_off++;
516 wscrl(body, 1);
518 if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
519 return;
521 vl = nth_line(tab, tab->s.line_off + body_lines-1);
522 wmove(body, body_lines-1, 0);
523 print_vline(vl);
526 static void
527 cmd_scroll_up(struct tab *tab)
529 size_t off;
531 off = body_lines+1;
533 for (; off > 0; --off)
534 cmd_scroll_line_up(tab);
537 static void
538 cmd_scroll_down(struct tab *tab)
540 size_t off;
542 off = body_lines+1;
544 for (; off > 0; --off)
545 cmd_scroll_line_down(tab);
548 static void
549 cmd_beginning_of_buffer(struct tab *tab)
551 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
552 tab->s.line_off = 0;
553 tab->s.curs_y = 0;
554 tab->s.line_x = 0;
555 restore_cursor(tab);
556 redraw_body(tab);
559 static void
560 cmd_end_of_buffer(struct tab *tab)
562 ssize_t off;
564 off = tab->s.line_max - body_lines;
565 off = MAX(0, off);
567 tab->s.line_off = off;
568 tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
570 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
571 tab->s.line_x = body_cols;
572 restore_cursor(tab);
573 redraw_body(tab);
576 static void
577 cmd_kill_telescope(struct tab *tab)
579 event_loopbreak();
582 static void
583 cmd_push_button(struct tab *tab)
585 struct vline *vl;
586 size_t nth;
588 nth = tab->s.line_off + tab->s.curs_y;
589 if (nth >= tab->s.line_max)
590 return;
591 vl = nth_line(tab, nth);
592 if (vl->parent->type != LINE_LINK)
593 return;
595 load_url_in_tab(tab, vl->parent->alt);
598 static void
599 cmd_push_button_new_tab(struct tab *tab)
601 struct vline *vl;
602 size_t nth;
604 nth = tab->s.line_off + tab->s.curs_y;
605 if (nth > tab->s.line_max)
606 return;
607 vl = nth_line(tab, nth);
608 if (vl->parent->type != LINE_LINK)
609 return;
611 new_tab(vl->parent->alt);
614 static void
615 cmd_previous_button(struct tab *tab)
617 do {
618 if (tab->s.current_line == NULL ||
619 tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
620 message("No previous link");
621 return;
623 cmd_previous_line(tab);
624 } while (tab->s.current_line->parent->type != LINE_LINK);
627 static void
628 cmd_next_button(struct tab *tab)
630 do {
631 if (tab->s.current_line == NULL ||
632 tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
633 message("No next link");
634 return;
636 cmd_next_line(tab);
637 } while (tab->s.current_line->parent->type != LINE_LINK);
640 static void
641 cmd_previous_page(struct tab *tab)
643 if (!load_previous_page(tab))
644 message("No previous page");
645 else
646 start_loading_anim(tab);
649 static void
650 cmd_next_page(struct tab *tab)
652 if (!load_next_page(tab))
653 message("No next page");
654 else
655 start_loading_anim(tab);
658 static void
659 cmd_clear_minibuf(struct tab *tab)
661 handle_clear_minibuf(0, 0, NULL);
664 static void
665 cmd_execute_extended_command(struct tab *tab)
667 size_t len;
669 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
670 &eecmd_history);
672 len = sizeof(ministate.prompt);
673 strlcpy(ministate.prompt, "", len);
675 if (thiskey.meta)
676 strlcat(ministate.prompt, "M-", len);
678 strlcat(ministate.prompt, keyname(thiskey.key), len);
679 strlcat(ministate.prompt, " ", len);
682 static void
683 cmd_tab_close(struct tab *tab)
685 struct tab *t;
687 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
688 TAILQ_NEXT(tab, tabs) == NULL) {
689 message("Can't close the only tab.");
690 return;
693 if (evtimer_pending(&tab->s.loadingev, NULL))
694 evtimer_del(&tab->s.loadingev);
696 stop_tab(tab);
698 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
699 t = TAILQ_NEXT(tab, tabs);
700 TAILQ_REMOVE(&tabshead, tab, tabs);
701 free(tab);
703 switch_to_tab(t);
706 static void
707 cmd_tab_close_other(struct tab *tab)
709 struct tab *t, *i;
711 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
712 if (t->flags & TAB_CURRENT)
713 continue;
715 stop_tab(t);
716 TAILQ_REMOVE(&tabshead, t, tabs);
717 free(t);
721 static void
722 cmd_tab_new(struct tab *tab)
724 new_tab(NEW_TAB_URL);
727 static void
728 cmd_tab_next(struct tab *tab)
730 struct tab *t;
732 tab->flags &= ~TAB_CURRENT;
734 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
735 t = TAILQ_FIRST(&tabshead);
736 t->flags |= TAB_CURRENT;
739 static void
740 cmd_tab_previous(struct tab *tab)
742 struct tab *t;
744 tab->flags &= ~TAB_CURRENT;
746 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
747 t = TAILQ_LAST(&tabshead, tabshead);
748 t->flags |= TAB_CURRENT;
751 static void
752 cmd_load_url(struct tab *tab)
754 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
755 &lu_history);
756 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
759 static void
760 cmd_load_current_url(struct tab *tab)
762 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
763 &lu_history);
764 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
765 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
766 ministate.off = strlen(tab->hist_cur->h);
767 ministate.len = ministate.off;
770 static void
771 cmd_bookmark_page(struct tab *tab)
773 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
774 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
775 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
776 ministate.off = strlen(tab->hist_cur->h);
777 ministate.len = ministate.off;
780 static void
781 cmd_goto_bookmarks(struct tab *tab)
783 load_url_in_tab(tab, "about:bookmarks");
786 static void
787 global_key_unbound(void)
789 message("%s is undefined", keybuf);
792 static void
793 cmd_mini_delete_char(struct tab *tab)
795 minibuffer_taint_hist();
797 if (ministate.len == 0 || ministate.off == ministate.len)
798 return;
800 memmove(&ministate.buf[ministate.off],
801 &ministate.buf[ministate.off+1],
802 ministate.len - ministate.off + 1);
803 ministate.len--;
806 static void
807 cmd_mini_delete_backward_char(struct tab *tab)
809 minibuffer_taint_hist();
811 if (ministate.len == 0 || ministate.off == 0)
812 return;
814 memmove(&ministate.buf[ministate.off-1],
815 &ministate.buf[ministate.off],
816 ministate.len - ministate.off + 1);
817 ministate.off--;
818 ministate.len--;
821 static void
822 cmd_mini_forward_char(struct tab *tab)
824 if (ministate.off == ministate.len)
825 return;
826 ministate.off++;
829 static void
830 cmd_mini_backward_char(struct tab *tab)
832 if (ministate.off == 0)
833 return;
834 ministate.off--;
837 static void
838 cmd_mini_move_end_of_line(struct tab *tab)
840 ministate.off = ministate.len;
843 static void
844 cmd_mini_move_beginning_of_line(struct tab *tab)
846 ministate.off = 0;
849 static void
850 cmd_mini_kill_line(struct tab *tab)
852 minibuffer_taint_hist();
854 if (ministate.off == ministate.len)
855 return;
856 ministate.buf[ministate.off] = '\0';
857 ministate.len -= ministate.off;
860 static void
861 cmd_mini_abort(struct tab *tab)
863 ministate.abortfn();
866 static void
867 cmd_mini_complete_and_exit(struct tab *tab)
869 minibuffer_taint_hist();
870 ministate.donefn();
873 static void
874 cmd_mini_previous_history_element(struct tab *tab)
876 if (ministate.history == NULL) {
877 message("No history");
878 return;
881 if (ministate.hist_cur == NULL ||
882 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
883 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
884 ministate.hist_off = ministate.history->len - 1;
885 if (ministate.hist_cur == NULL)
886 message("No prev item");
887 } else {
888 ministate.hist_off--;
891 if (ministate.hist_cur != NULL) {
892 ministate.off = 0;
893 ministate.len = strlen(ministate.hist_cur->h);
897 static void
898 cmd_mini_next_history_element(struct tab *tab)
900 if (ministate.history == NULL) {
901 message("No history");
902 return;
905 if (ministate.hist_cur == NULL ||
906 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
907 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
908 ministate.hist_off = 0;
909 if (ministate.hist_cur == NULL)
910 message("No next item");
911 } else {
912 ministate.hist_off++;
915 if (ministate.hist_cur != NULL) {
916 ministate.off = 0;
917 ministate.len = strlen(ministate.hist_cur->h);
921 static void
922 minibuffer_hist_save_entry(void)
924 struct hist *hist;
926 if (ministate.history == NULL)
927 return;
929 if ((hist = calloc(1, sizeof(*hist))) == NULL)
930 abort();
932 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
934 if (TAILQ_EMPTY(&ministate.history->head))
935 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
936 else
937 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
938 ministate.history->len++;
941 /*
942 * taint the minibuffer cache: if we're currently showing a history
943 * element, copy that to the current buf and reset the "history
944 * navigation" thing.
945 */
946 static void
947 minibuffer_taint_hist(void)
949 if (ministate.hist_cur == NULL)
950 return;
952 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
953 ministate.hist_cur = NULL;
956 static void
957 minibuffer_self_insert(void)
959 minibuffer_taint_hist();
961 if (ministate.len == sizeof(ministate.buf) -1)
962 return;
964 /* TODO: utf8 handling! */
966 memmove(&ministate.buf[ministate.off+1],
967 &ministate.buf[ministate.off],
968 ministate.len - ministate.off + 1);
969 ministate.buf[ministate.off] = thiskey.key;
970 ministate.off++;
971 ministate.len++;
974 static void
975 eecmd_self_insert(void)
977 if (thiskey.meta || isspace(thiskey.key) ||
978 !isgraph(thiskey.key)) {
979 global_key_unbound();
980 return;
983 minibuffer_self_insert();
986 static void
987 eecmd_select(void)
989 exit_minibuffer();
990 minibuffer_hist_save_entry();
991 message("TODO: try to execute %s", ministate.buf);
994 static void
995 ir_self_insert(void)
997 minibuffer_self_insert();
1000 static void
1001 ir_select(void)
1003 char buf[1025] = {0};
1004 struct url url;
1005 struct tab *tab;
1007 tab = current_tab();
1009 exit_minibuffer();
1010 minibuffer_hist_save_entry();
1012 /* a bit ugly but... */
1013 memcpy(&url, &tab->url, sizeof(tab->url));
1014 url_set_query(&url, ministate.buf);
1015 url_unparse(&url, buf, sizeof(buf));
1016 load_url_in_tab(tab, buf);
1019 static void
1020 lu_self_insert(void)
1022 if (thiskey.meta || isspace(thiskey.key) ||
1023 !isgraph(thiskey.key)) {
1024 global_key_unbound();
1025 return;
1028 minibuffer_self_insert();
1031 static void
1032 lu_select(void)
1034 exit_minibuffer();
1035 minibuffer_hist_save_entry();
1036 load_url_in_tab(current_tab(), ministate.buf);
1039 static void
1040 bp_select(void)
1042 exit_minibuffer();
1043 if (*ministate.buf != '\0')
1044 add_to_bookmarks(ministate.buf);
1045 else
1046 message("Abort.");
1049 static struct vline *
1050 nth_line(struct tab *tab, size_t n)
1052 struct vline *vl;
1053 size_t i;
1055 i = 0;
1056 TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1057 if (i == n)
1058 return vl;
1059 i++;
1062 /* unreachable */
1063 abort();
1066 static struct tab *
1067 current_tab(void)
1069 struct tab *t;
1071 TAILQ_FOREACH(t, &tabshead, tabs) {
1072 if (t->flags & TAB_CURRENT)
1073 return t;
1076 /* unreachable */
1077 abort();
1080 static void
1081 dispatch_stdio(int fd, short ev, void *d)
1083 struct keymap *k;
1084 const char *keyname;
1085 char tmp[2] = {0};
1087 thiskey.key = wgetch(body);
1088 if (thiskey.key == ERR)
1089 return;
1090 if (thiskey.key == 27) {
1091 /* TODO: make escape-time customizable */
1093 thiskey.meta = 1;
1094 thiskey.key = wgetch(body);
1095 if (thiskey.key == ERR || thiskey.key == 27) {
1096 thiskey.meta = 0;
1097 thiskey.key = 27;
1099 } else
1100 thiskey.meta = 0;
1102 if (keybuf[0] != '\0')
1103 strlcat(keybuf, " ", sizeof(keybuf));
1104 if (thiskey.meta)
1105 strlcat(keybuf, "M-", sizeof(keybuf));
1106 if ((keyname = unkbd(thiskey.key)) != NULL)
1107 strlcat(keybuf, keyname, sizeof(keybuf));
1108 else {
1109 tmp[0] = thiskey.key;
1110 strlcat(keybuf, tmp, sizeof(keybuf));
1113 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1114 if (k->meta == thiskey.meta &&
1115 k->key == thiskey.key) {
1116 if (k->fn == NULL)
1117 current_map = &k->map;
1118 else {
1119 current_map = base_map;
1120 strlcpy(keybuf, "", sizeof(keybuf));
1121 k->fn(current_tab());
1123 goto done;
1127 if (current_map->unhandled_input != NULL)
1128 current_map->unhandled_input();
1129 else {
1130 global_key_unbound();
1133 strlcpy(keybuf, "", sizeof(keybuf));
1134 current_map = base_map;
1136 done:
1137 redraw_tab(current_tab());
1140 static void
1141 handle_clear_minibuf(int fd, short ev, void *d)
1143 free(ministate.curmesg);
1144 ministate.curmesg = NULL;
1146 redraw_minibuffer();
1147 if (in_minibuffer) {
1148 wrefresh(body);
1149 wrefresh(minibuf);
1150 } else {
1151 wrefresh(minibuf);
1152 wrefresh(body);
1156 static void
1157 handle_resize(int sig, short ev, void *d)
1159 struct tab *tab;
1161 endwin();
1162 refresh();
1163 clear();
1165 /* move and resize the windows, in reverse order! */
1167 mvwin(minibuf, LINES-1, 0);
1168 wresize(minibuf, 1, COLS);
1170 mvwin(modeline, LINES-2, 0);
1171 wresize(modeline, 1, COLS);
1173 wresize(body, LINES-3, COLS);
1174 body_lines = LINES-3;
1175 body_cols = COLS;
1177 wresize(tabline, 1, COLS);
1179 tab = current_tab();
1181 wrap_page(tab);
1182 redraw_tab(tab);
1185 static int
1186 wrap_page(struct tab *tab)
1188 struct line *l;
1189 const struct line *orig;
1190 struct vline *vl;
1191 const char *prfx;
1193 orig = tab->s.current_line == NULL
1194 ? NULL
1195 : tab->s.current_line->parent;
1196 tab->s.current_line = NULL;
1198 tab->s.curs_y = 0;
1199 tab->s.line_off = 0;
1201 empty_vlist(tab);
1203 TAILQ_FOREACH(l, &tab->page.head, lines) {
1204 prfx = line_prefixes[l->type].prfx1;
1205 switch (l->type) {
1206 case LINE_TEXT:
1207 case LINE_LINK:
1208 case LINE_TITLE_1:
1209 case LINE_TITLE_2:
1210 case LINE_TITLE_3:
1211 case LINE_ITEM:
1212 case LINE_QUOTE:
1213 case LINE_PRE_START:
1214 case LINE_PRE_END:
1215 wrap_text(tab, prfx, l, body_cols);
1216 break;
1217 case LINE_PRE_CONTENT:
1218 hardwrap_text(tab, l, body_cols);
1219 break;
1222 if (orig == l && tab->s.current_line == NULL) {
1223 tab->s.line_off = tab->s.line_max-1;
1224 tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1226 while (1) {
1227 vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1228 if (vl == NULL || vl->parent != orig)
1229 break;
1230 tab->s.current_line = vl;
1231 tab->s.line_off--;
1236 if (tab->s.current_line == NULL)
1237 tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1239 return 1;
1242 static void
1243 print_vline(struct vline *vl)
1245 const char *text = vl->line;
1246 const char *prfx;
1247 int prefix_face = line_faces[vl->parent->type].prefix_prop;
1248 int text_face = line_faces[vl->parent->type].text_prop;
1250 if (!vl->flags)
1251 prfx = line_prefixes[vl->parent->type].prfx1;
1252 else
1253 prfx = line_prefixes[vl->parent->type].prfx2;
1255 if (text == NULL)
1256 text = "";
1258 wattron(body, prefix_face);
1259 wprintw(body, "%s", prfx);
1260 wattroff(body, prefix_face);
1262 wattron(body, text_face);
1263 wprintw(body, "%s", text);
1264 wattroff(body, text_face);
1267 static void
1268 redraw_tabline(void)
1270 struct tab *tab;
1271 size_t toskip;
1272 int current, x, y, truncated;
1273 const char *title;
1274 char buf[25];
1276 toskip = 0;
1277 x = 1;
1278 TAILQ_FOREACH(tab, &tabshead, tabs) {
1279 x += sizeof(buf) + 1;
1280 toskip++;
1281 if (tab->flags & TAB_CURRENT)
1282 break;
1284 if (x < COLS-2)
1285 toskip = 0;
1286 else
1287 toskip--;
1289 werase(tabline);
1290 wattron(tabline, tab_face.background);
1291 wprintw(tabline, toskip == 0 ? " " : "<");
1292 wattroff(tabline, tab_face.background);
1294 truncated = 0;
1295 TAILQ_FOREACH(tab, &tabshead, tabs) {
1296 if (truncated)
1297 break;
1298 if (toskip != 0) {
1299 toskip--;
1300 continue;
1303 getyx(tabline, y, x);
1304 if (x + sizeof(buf)+2 >= (size_t)COLS)
1305 truncated = 1;
1307 current = tab->flags & TAB_CURRENT;
1309 if (*(title = tab->page.title) == '\0')
1310 title = tab->hist_cur->h;
1312 strlcpy(buf, " ", sizeof(buf));
1313 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1314 /* truncation happens */
1315 strlcpy(&buf[sizeof(buf)-4], "...", 4);
1316 } else {
1317 /* pad with spaces */
1318 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1319 /* nop */ ;
1322 if (current)
1323 wattron(tabline, tab_face.current_tab);
1324 else
1325 wattron(tabline, tab_face.tab);
1327 wprintw(tabline, "%s", buf);
1328 if (TAILQ_NEXT(tab, tabs) != NULL)
1329 wprintw(tabline, " ");
1331 if (current)
1332 wattroff(tabline, tab_face.current_tab);
1333 else
1334 wattroff(tabline, tab_face.tab);
1337 wattron(tabline, tab_face.background);
1338 for (; x < COLS; ++x)
1339 waddch(tabline, ' ');
1340 if (truncated)
1341 mvwprintw(tabline, 0, COLS-1, ">");
1344 static inline char
1345 trust_status_char(enum trust_state ts)
1347 switch (ts) {
1348 case TS_UNKNOWN: return 'u';
1349 case TS_UNTRUSTED: return '!';
1350 case TS_TRUSTED: return 'v';
1351 case TS_VERIFIED: return 'V';
1355 static void
1356 redraw_modeline(struct tab *tab)
1358 double pct;
1359 int x, y, max_x, max_y;
1360 const char *mode = tab->page.name;
1361 const char *spin = "-\\|/";
1363 werase(modeline);
1364 wattron(modeline, A_REVERSE);
1365 wmove(modeline, 0, 0);
1367 wprintw(modeline, "-%c%c %s ",
1368 spin[tab->s.loading_anim_step],
1369 trust_status_char(tab->trust),
1370 mode == NULL ? "(none)" : mode);
1372 pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1374 if (tab->s.line_max <= (size_t)body_lines)
1375 wprintw(modeline, "All ");
1376 else if (tab->s.line_off == 0)
1377 wprintw(modeline, "Top ");
1378 else if (tab->s.line_off + body_lines >= tab->s.line_max)
1379 wprintw(modeline, "Bottom ");
1380 else
1381 wprintw(modeline, "%.0f%% ", pct);
1383 wprintw(modeline, "%d/%d %s ",
1384 tab->s.line_off + tab->s.curs_y,
1385 tab->s.line_max,
1386 tab->hist_cur->h);
1388 getyx(modeline, y, x);
1389 getmaxyx(modeline, max_y, max_x);
1391 (void)y;
1392 (void)max_y;
1394 for (; x < max_x; ++x)
1395 waddstr(modeline, "-");
1398 static void
1399 redraw_minibuffer(void)
1401 size_t skip = 0, off_x = 0, off_y = 0;
1403 werase(minibuf);
1404 if (in_minibuffer) {
1405 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1406 if (ministate.hist_cur != NULL)
1407 wprintw(minibuf, "(%zu/%zu) ",
1408 ministate.hist_off + 1,
1409 ministate.history->len);
1411 getyx(minibuf, off_y, off_x);
1413 while (ministate.off - skip > (size_t)COLS / 2) {
1414 skip += MIN(ministate.off/4, 1);
1417 if (ministate.hist_cur != NULL)
1418 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1419 else
1420 wprintw(minibuf, "%s", ministate.buf + skip);
1423 if (ministate.curmesg != NULL) {
1424 if (in_minibuffer)
1425 wprintw(minibuf, " [%s]", ministate.curmesg);
1426 else
1427 wprintw(minibuf, "%s", ministate.curmesg);
1430 if (!in_minibuffer && ministate.curmesg == NULL)
1431 wprintw(minibuf, "%s", keybuf);
1433 if (in_minibuffer)
1434 wmove(minibuf, 0, off_x + ministate.off - skip);
1437 static void
1438 redraw_tab(struct tab *tab)
1440 redraw_tabline();
1441 redraw_body(tab);
1442 redraw_modeline(tab);
1443 redraw_minibuffer();
1445 restore_cursor(tab);
1446 wrefresh(tabline);
1447 wrefresh(modeline);
1449 if (in_minibuffer) {
1450 wrefresh(body);
1451 wrefresh(minibuf);
1452 } else {
1453 wrefresh(minibuf);
1454 wrefresh(body);
1458 static void
1459 redraw_body(struct tab *tab)
1461 struct vline *vl;
1462 int line;
1464 werase(body);
1466 tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1467 if (TAILQ_EMPTY(&tab->s.head))
1468 return;
1470 line = 0;
1471 vl = nth_line(tab, tab->s.line_off);
1472 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1473 wmove(body, line, 0);
1474 print_vline(vl);
1475 line++;
1476 if (line == body_lines)
1477 break;
1481 static void
1482 vmessage(const char *fmt, va_list ap)
1484 if (evtimer_pending(&clminibufev, NULL))
1485 evtimer_del(&clminibufev);
1486 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1487 evtimer_add(&clminibufev, &clminibufev_timer);
1489 free(ministate.curmesg);
1491 /* TODO: what to do if the allocation fails here? */
1492 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1493 ministate.curmesg = NULL;
1495 redraw_minibuffer();
1496 if (in_minibuffer) {
1497 wrefresh(body);
1498 wrefresh(minibuf);
1499 } else {
1500 wrefresh(minibuf);
1501 wrefresh(body);
1505 static void
1506 message(const char *fmt, ...)
1508 va_list ap;
1510 va_start(ap, fmt);
1511 vmessage(fmt, ap);
1512 va_end(ap);
1515 static void
1516 start_loading_anim(struct tab *tab)
1518 if (tab->s.loading_anim)
1519 return;
1520 tab->s.loading_anim = 1;
1521 evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1522 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1525 static void
1526 update_loading_anim(int fd, short ev, void *d)
1528 struct tab *tab = d;
1530 tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1532 if (tab->flags & TAB_CURRENT) {
1533 redraw_modeline(tab);
1534 wrefresh(modeline);
1535 wrefresh(body);
1536 if (in_minibuffer)
1537 wrefresh(minibuf);
1540 evtimer_add(&tab->s.loadingev, &loadingev_timer);
1543 static void
1544 stop_loading_anim(struct tab *tab)
1546 if (!tab->s.loading_anim)
1547 return;
1548 evtimer_del(&tab->s.loadingev);
1549 tab->s.loading_anim = 0;
1550 tab->s.loading_anim_step = 0;
1552 redraw_modeline(tab);
1554 wrefresh(modeline);
1555 wrefresh(body);
1556 if (in_minibuffer)
1557 wrefresh(minibuf);
1560 static void
1561 load_url_in_tab(struct tab *tab, const char *url)
1563 empty_vlist(tab);
1564 message("Loading %s...", url);
1565 start_loading_anim(tab);
1566 load_url(tab, url);
1568 tab->s.curs_x = 0;
1569 tab->s.curs_y = 0;
1570 redraw_tab(tab);
1573 static void
1574 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1575 void (*abortfn)(void), struct histhead *hist)
1577 in_minibuffer = 1;
1578 base_map = &minibuffer_map;
1579 current_map = &minibuffer_map;
1581 base_map->unhandled_input = self_insert_fn;
1583 ministate.donefn = donefn;
1584 ministate.abortfn = abortfn;
1585 memset(ministate.buf, 0, sizeof(ministate.buf));
1586 ministate.off = 0;
1587 ministate.len = 0;
1588 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1590 ministate.history = hist;
1591 ministate.hist_cur = NULL;
1592 ministate.hist_off = 0;
1595 static void
1596 exit_minibuffer(void)
1598 werase(minibuf);
1600 in_minibuffer = 0;
1601 base_map = &global_map;
1602 current_map = &global_map;
1605 static void
1606 switch_to_tab(struct tab *tab)
1608 struct tab *t;
1610 TAILQ_FOREACH(t, &tabshead, tabs) {
1611 t->flags &= ~TAB_CURRENT;
1614 tab->flags |= TAB_CURRENT;
1617 static struct tab *
1618 new_tab(const char *url)
1620 struct tab *tab;
1622 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1623 event_loopbreak();
1624 return NULL;
1627 TAILQ_INIT(&tab->hist.head);
1629 TAILQ_INIT(&tab->s.head);
1631 tab->id = tab_counter++;
1632 switch_to_tab(tab);
1634 if (TAILQ_EMPTY(&tabshead))
1635 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1636 else
1637 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1639 load_url_in_tab(tab, url);
1640 return tab;
1643 int
1644 ui_init(void)
1646 setlocale(LC_ALL, "");
1648 TAILQ_INIT(&global_map.m);
1649 global_map.unhandled_input = global_key_unbound;
1651 TAILQ_INIT(&minibuffer_map.m);
1653 TAILQ_INIT(&eecmd_history.head);
1654 TAILQ_INIT(&ir_history.head);
1655 TAILQ_INIT(&lu_history.head);
1657 base_map = &global_map;
1658 current_map = &global_map;
1659 load_default_keys();
1661 initscr();
1662 raw();
1663 noecho();
1665 nonl();
1666 intrflush(stdscr, FALSE);
1668 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1669 return 0;
1670 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1671 return 0;
1672 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1673 return 0;
1674 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1675 return 0;
1677 body_lines = LINES-3;
1678 body_cols = COLS;
1680 keypad(body, TRUE);
1681 scrollok(body, TRUE);
1683 /* non-blocking input */
1684 wtimeout(body, 0);
1686 mvwprintw(body, 0, 0, "");
1688 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1689 event_add(&stdioev, NULL);
1691 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1692 signal_add(&winchev, NULL);
1694 new_tab(NEW_TAB_URL);
1696 return 1;
1699 void
1700 ui_on_tab_loaded(struct tab *tab)
1702 stop_loading_anim(tab);
1703 message("Loaded %s", tab->hist_cur->h);
1706 void
1707 ui_on_tab_refresh(struct tab *tab)
1709 if (!(tab->flags & TAB_CURRENT))
1710 return;
1712 wrap_page(tab);
1713 redraw_tab(tab);
1716 void
1717 ui_require_input(struct tab *tab, int hide)
1719 /* TODO: hard-switching to another tab is ugly */
1720 switch_to_tab(tab);
1722 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1723 &ir_history);
1724 strlcpy(ministate.prompt, "Input required: ",
1725 sizeof(ministate.prompt));
1726 redraw_tab(tab);
1729 void
1730 ui_notify(const char *fmt, ...)
1732 va_list ap;
1734 va_start(ap, fmt);
1735 vmessage(fmt, ap);
1736 va_end(ap);
1739 void
1740 ui_end(void)
1742 endwin();