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 *
21 * Text wrapping
22 * =============
23 *
24 * There's a simple text wrapping algorithm.
25 *
26 * 1. if it's a line in a pre-formatted block:
27 * a. hard wrap.
28 * b. repeat
29 * 2. there is enough room for the next word?
30 * a. yes: render it
31 * b. no: break the current line.
32 * i. while there isn't enough space to draw the current
33 * word, hard-wrap it
34 * ii. draw the remainder of the current word (can be the
35 * the entirely)
36 * 3. render the spaces after the word
37 * a. but if there is not enough room break the line and
38 * forget them
39 * 4. repeat
40 *
41 *
42 * Text scrolling
43 * ==============
44 *
45 * ncurses allows you to scroll a window, but when a line goes out of
46 * the visible area it's forgotten. We keep a list of formatted lines
47 * (``visual lines'') that we know fits in the window, and draw them.
48 * This way is easy to scroll: just call wscrl and then render the
49 * first/last line!
50 *
51 * This means that on every resize we have to clear our list of lines
52 * and re-render everything. A clever approach would be to do this
53 * ``on-demand''.
54 *
55 * TODO: make the text formatting on-demand.
56 *
57 */
59 #include <telescope.h>
61 #include <ctype.h>
62 #include <curses.h>
63 #include <event.h>
64 #include <locale.h>
65 #include <signal.h>
66 #include <stdarg.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
71 #define TAB_CURRENT 0x1
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
76 struct kmap;
77 struct minibuf_histhead;
79 static struct event stdioev, winchev;
81 static int kbd(const char*);
82 static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
83 static void load_default_keys(void);
84 static int push_line(struct tab*, const struct line*, const char*, size_t, int);
85 static void empty_vlist(struct tab*);
86 static void restore_cursor(struct tab *);
88 static void cmd_previous_line(struct tab*);
89 static void cmd_next_line(struct tab*);
90 static void cmd_forward_char(struct tab*);
91 static void cmd_backward_char(struct tab*);
92 static void cmd_move_beginning_of_line(struct tab*);
93 static void cmd_move_end_of_line(struct tab*);
94 static void cmd_redraw(struct tab*);
95 static void cmd_scroll_line_down(struct tab*);
96 static void cmd_scroll_line_up(struct tab*);
97 static void cmd_scroll_up(struct tab*);
98 static void cmd_scroll_down(struct tab*);
99 static void cmd_beginning_of_buffer(struct tab*);
100 static void cmd_end_of_buffer(struct tab*);
101 static void cmd_kill_telescope(struct tab*);
102 static void cmd_push_button(struct tab*);
103 static void cmd_push_button_new_tab(struct tab*);
104 static void cmd_clear_minibuf(struct tab*);
105 static void cmd_execute_extended_command(struct tab*);
106 static void cmd_tab_close(struct tab*);
107 static void cmd_tab_new(struct tab*);
108 static void cmd_tab_next(struct tab*);
109 static void cmd_tab_previous(struct tab*);
110 static void cmd_load_url(struct tab*);
111 static void cmd_load_current_url(struct tab*);
113 static void global_key_unbound(void);
115 static void cmd_mini_delete_char(struct tab*);
116 static void cmd_mini_delete_backward_char(struct tab*);
117 static void cmd_mini_forward_char(struct tab*);
118 static void cmd_mini_backward_char(struct tab*);
119 static void cmd_mini_move_end_of_line(struct tab*);
120 static void cmd_mini_move_beginning_of_line(struct tab*);
121 static void cmd_mini_kill_line(struct tab*);
122 static void cmd_mini_abort(struct tab*);
123 static void cmd_mini_complete_and_exit(struct tab*);
124 static void cmd_mini_previous_history_element(struct tab*);
125 static void cmd_mini_next_history_element(struct tab*);
127 static void minibuffer_hist_save_entry(void);
128 static void minibuffer_taint_hist(void);
129 static void minibuffer_self_insert(void);
130 static void eecmd_self_insert(void);
131 static void eecmd_select(void);
132 static void ir_self_insert(void);
133 static void ir_select(void);
134 static void lu_self_insert(void);
135 static void lu_select(void);
137 static struct line *nth_line(struct tab*, size_t);
138 static struct tab *current_tab(void);
139 static void dispatch_stdio(int, short, void*);
140 static void handle_clear_minibuf(int, short, void*);
141 static void handle_resize(int, short, void*);
142 static int word_bourdaries(const char*, const char*, const char**, const char**);
143 static void wrap_text(struct tab*, const char*, struct line*);
144 static int hardwrap_text(struct tab*, struct line*);
145 static int wrap_page(struct tab*);
146 static void print_line(struct line*);
147 static void redraw_tabline(void);
148 static void redraw_body(struct tab*);
149 static void redraw_modeline(struct tab*);
150 static void redraw_minibuffer(void);
151 static void redraw_tab(struct tab*);
152 static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
153 static void start_loading_anim(struct tab*);
154 static void update_loading_anim(int, short, void*);
155 static void stop_loading_anim(struct tab*);
156 static void load_url_in_tab(struct tab*, const char*);
157 static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct minibuf_histhead*);
158 static void exit_minibuffer(void);
159 static void switch_to_tab(struct tab*);
160 static struct tab *new_tab(void);
162 static struct { int meta, key; } thiskey;
164 static WINDOW *tabline, *body, *modeline, *minibuf;
165 static int body_lines, body_cols;
167 static struct event clminibufev;
168 static int clminibufev_set;
169 static struct timeval clminibufev_timer = { 5, 0 };
170 static struct timeval loadingev_timer = { 0, 250000 };
172 static uint32_t tab_counter;
174 struct ui_state {
175 int curs_x;
176 int curs_y;
177 size_t line_off;
178 size_t line_max;
180 short loading_anim;
181 short loading_anim_step;
182 struct event loadingev;
184 TAILQ_HEAD(, line) head;
185 };
187 static char keybuf[64];
189 #define CTRL(n) ((n)&0x1F)
191 struct keytable {
192 char *p;
193 int k;
194 } keytable[] = {
195 { "<up>", KEY_UP },
196 { "<down>", KEY_DOWN },
197 { "<left>", KEY_LEFT },
198 { "<right>", KEY_RIGHT },
199 { "<prior>", KEY_PPAGE },
200 { "<next>", KEY_NPAGE },
201 { "<home>", KEY_HOME },
202 { "<end>", KEY_END },
203 /* ... */
204 { "del", KEY_BACKSPACE },
205 { "esc", 27 },
206 { "space", ' ' },
207 { "spc", ' ' },
208 { "enter", CTRL('m') },
209 { "ret", CTRL('m' )},
210 { "tab", CTRL('i') },
211 /* ... */
212 { NULL, 0 },
213 };
215 struct kmap {
216 TAILQ_HEAD(map, keymap) m;
217 void (*unhandled_input)(void);
218 };
220 struct kmap global_map,
221 minibuffer_map,
222 *current_map,
223 *base_map;
225 struct keymap {
226 int meta;
227 int key;
228 struct kmap map;
229 void (*fn)(struct tab*);
231 TAILQ_ENTRY(keymap) keymaps;
232 };
234 /* TODO: limit to a maximum number of entries */
235 struct minibuf_histhead {
236 TAILQ_HEAD(mhisthead, minibuf_hist) head;
237 size_t len;
238 };
239 struct minibuf_hist {
240 char h[1025];
241 TAILQ_ENTRY(minibuf_hist) entries;
242 };
244 static struct minibuf_histhead eecmd_history,
245 ir_history,
246 lu_history;
248 static int in_minibuffer;
250 static struct {
251 char *curmesg;
253 char buf[1025];
254 size_t off, len;
255 char prompt[32];
256 void (*donefn)(void);
257 void (*abortfn)(void);
259 struct minibuf_histhead *history;
260 struct minibuf_hist *hist_cur;
261 size_t hist_off;
262 } ministate;
264 struct lineprefix {
265 const char *prfx1;
266 const char *prfx2;
267 } line_prefixes[] = {
268 [LINE_TEXT] = { "", "" },
269 [LINE_LINK] = { "=> ", " " },
270 [LINE_TITLE_1] = { "# ", " " },
271 [LINE_TITLE_2] = { "## ", " " },
272 [LINE_TITLE_3] = { "### ", " " },
273 [LINE_ITEM] = { "* ", " " },
274 [LINE_QUOTE] = { "> ", "> " },
275 [LINE_PRE_START] = { "```", "```" },
276 [LINE_PRE_CONTENT] = { "", "" },
277 [LINE_PRE_END] = { "```", "```" },
278 };
280 struct line_face {
281 int prop;
282 } line_faces[] = {
283 [LINE_TEXT] = { 0 },
284 [LINE_LINK] = { A_UNDERLINE },
285 [LINE_TITLE_1] = { A_BOLD },
286 [LINE_TITLE_2] = { A_BOLD },
287 [LINE_TITLE_3] = { A_BOLD },
288 [LINE_ITEM] = { 0 },
289 [LINE_QUOTE] = { A_DIM },
290 [LINE_PRE_START] = { 0 },
291 [LINE_PRE_CONTENT] = { 0 },
292 [LINE_PRE_END] = { 0 },
293 };
295 static int
296 kbd(const char *key)
298 struct keytable *t;
300 for (t = keytable; t->p != NULL; ++t) {
301 if (has_prefix(key, t->p))
302 return t->k;
305 return *key;
308 static const char *
309 unkbd(int k)
311 struct keytable *t;
313 for (t = keytable; t->p != NULL; ++t) {
314 if (k == t->k)
315 return t->p;
318 return NULL;
321 static void
322 kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
324 int ctrl, meta, k;
325 struct keymap *entry;
327 again:
328 if ((ctrl = has_prefix(key, "C-")))
329 key += 2;
330 if ((meta = has_prefix(key, "M-")))
331 key += 2;
332 if (*key == '\0')
333 _exit(1);
334 k = kbd(key);
336 if (ctrl)
337 k = CTRL(k);
339 /* skip key & spaces */
340 while (*key != '\0' && !isspace(*key))
341 ++key;
342 while (*key != '\0' && isspace(*key))
343 ++key;
345 TAILQ_FOREACH(entry, &map->m, keymaps) {
346 if (entry->meta == meta && entry->key == k) {
347 if (*key == '\0') {
348 entry->fn = fn;
349 return;
351 map = &entry->map;
352 goto again;
356 if ((entry = calloc(1, sizeof(*entry))) == NULL)
357 abort();
359 entry->meta = meta;
360 entry->key = k;
361 TAILQ_INIT(&entry->map.m);
363 if (TAILQ_EMPTY(&map->m))
364 TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
365 else
366 TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
368 if (*key != '\0') {
369 map = &entry->map;
370 goto again;
373 entry->fn = fn;
376 static inline void
377 global_set_key(const char *key, void (*fn)(struct tab*))
379 kmap_define_key(&global_map, key, fn);
382 static inline void
383 minibuffer_set_key(const char *key, void (*fn)(struct tab*))
385 kmap_define_key(&minibuffer_map, key, fn);
388 static void
389 load_default_keys(void)
391 /* === global map === */
393 /* emacs */
394 global_set_key("C-p", cmd_previous_line);
395 global_set_key("C-n", cmd_next_line);
396 global_set_key("C-f", cmd_forward_char);
397 global_set_key("C-b", cmd_backward_char);
398 global_set_key("C-a", cmd_move_beginning_of_line);
399 global_set_key("C-e", cmd_move_end_of_line);
401 global_set_key("M-v", cmd_scroll_up);
402 global_set_key("C-v", cmd_scroll_down);
403 global_set_key("M-space", cmd_scroll_up);
404 global_set_key("space", cmd_scroll_down);
406 global_set_key("C-x C-c", cmd_kill_telescope);
408 global_set_key("C-g", cmd_clear_minibuf);
410 global_set_key("M-x", cmd_execute_extended_command);
411 global_set_key("C-x C-f", cmd_load_url);
412 global_set_key("C-x M-f", cmd_load_current_url);
414 global_set_key("C-x t 0", cmd_tab_close);
415 global_set_key("C-x t 2", cmd_tab_new);
416 global_set_key("C-x t o", cmd_tab_next);
417 global_set_key("C-x t O", cmd_tab_previous);
419 global_set_key("M-<", cmd_beginning_of_buffer);
420 global_set_key("M->", cmd_end_of_buffer);
422 /* vi/vi-like */
423 global_set_key("k", cmd_previous_line);
424 global_set_key("j", cmd_next_line);
425 global_set_key("l", cmd_forward_char);
426 global_set_key("h", cmd_backward_char);
427 global_set_key("^", cmd_move_beginning_of_line);
428 global_set_key("$", cmd_move_end_of_line);
430 global_set_key("K", cmd_scroll_line_up);
431 global_set_key("J", cmd_scroll_line_down);
433 global_set_key("g g", cmd_beginning_of_buffer);
434 global_set_key("G", cmd_end_of_buffer);
436 /* tmp */
437 global_set_key("q", cmd_kill_telescope);
439 global_set_key("esc", cmd_clear_minibuf);
441 global_set_key(":", cmd_execute_extended_command);
443 /* cua */
444 global_set_key("<up>", cmd_previous_line);
445 global_set_key("<down>", cmd_next_line);
446 global_set_key("<right>", cmd_forward_char);
447 global_set_key("<left>", cmd_backward_char);
448 global_set_key("<prior>", cmd_scroll_up);
449 global_set_key("<next>", cmd_scroll_down);
451 /* "ncurses standard" */
452 global_set_key("C-l", cmd_redraw);
454 /* global */
455 global_set_key("C-m", cmd_push_button);
456 global_set_key("M-enter", cmd_push_button_new_tab);
458 /* === minibuffer map === */
459 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
460 minibuffer_set_key("C-g", cmd_mini_abort);
461 minibuffer_set_key("esc", cmd_mini_abort);
462 minibuffer_set_key("C-d", cmd_mini_delete_char);
463 minibuffer_set_key("del", cmd_mini_delete_backward_char);
465 minibuffer_set_key("C-f", cmd_mini_forward_char);
466 minibuffer_set_key("C-b", cmd_mini_backward_char);
467 minibuffer_set_key("<right>", cmd_mini_forward_char);
468 minibuffer_set_key("<left>", cmd_mini_backward_char);
469 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
470 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
471 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
472 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
473 minibuffer_set_key("C-k", cmd_mini_kill_line);
475 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
476 minibuffer_set_key("M-n", cmd_mini_next_history_element);
477 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
478 minibuffer_set_key("<down>", cmd_mini_next_history_element);
481 static int
482 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len, int cont)
484 struct line *vl;
486 tab->s->line_max++;
488 if ((vl = calloc(1, sizeof(*vl))) == NULL)
489 return 0;
491 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
492 free(vl);
493 return 0;
496 vl->type = l->type;
497 if (len != 0)
498 memcpy(vl->line, buf, len);
499 vl->alt = l->alt;
500 vl->flags = cont;
502 if (TAILQ_EMPTY(&tab->s->head))
503 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
504 else
505 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
506 return 1;
509 static void
510 empty_vlist(struct tab *tab)
512 struct line *l, *t;
514 tab->s->line_max = 0;
516 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
517 TAILQ_REMOVE(&tab->s->head, l, lines);
518 free(l->line);
519 /* l->alt references the original line! */
520 free(l);
524 static void
525 restore_cursor(struct tab *tab)
527 wmove(body, tab->s->curs_y, tab->s->curs_x);
530 static void
531 cmd_previous_line(struct tab *tab)
533 if (--tab->s->curs_y < 0) {
534 tab->s->curs_y = 0;
535 cmd_scroll_line_up(tab);
538 restore_cursor(tab);
541 static void
542 cmd_next_line(struct tab *tab)
544 if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
545 return;
547 if (++tab->s->curs_y > body_lines-1) {
548 tab->s->curs_y = body_lines-1;
549 cmd_scroll_line_down(tab);
552 restore_cursor(tab);
555 static void
556 cmd_forward_char(struct tab *tab)
558 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
559 restore_cursor(tab);
562 static void
563 cmd_backward_char(struct tab *tab)
565 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
566 restore_cursor(tab);
569 static void
570 cmd_move_beginning_of_line(struct tab *tab)
572 tab->s->curs_x = 0;
573 restore_cursor(tab);
576 static void
577 cmd_move_end_of_line(struct tab *tab)
579 struct line *line;
580 size_t off;
581 const char *prfx;
583 off = tab->s->line_off + tab->s->curs_y;
584 if (off >= tab->s->line_max) {
585 tab->s->curs_x = 0;
586 goto end;
589 line = nth_line(tab, off);
590 if (line->line != NULL)
591 tab->s->curs_x = strlen(line->line);
592 else
593 tab->s->curs_x = 0;
595 prfx = line_prefixes[line->type].prfx1;
596 tab->s->curs_x += strlen(prfx);
598 end:
599 restore_cursor(tab);
602 static void
603 cmd_redraw(struct tab *tab)
605 handle_resize(0, 0, NULL);
608 static void
609 cmd_scroll_line_up(struct tab *tab)
611 struct line *l;
613 if (tab->s->line_off == 0)
614 return;
616 l = nth_line(tab, --tab->s->line_off);
617 wscrl(body, -1);
618 wmove(body, 0, 0);
619 print_line(l);
622 static void
623 cmd_scroll_line_down(struct tab *tab)
625 struct line *l;
626 size_t n;
628 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
629 return;
631 tab->s->line_off++;
632 wscrl(body, 1);
634 if (tab->s->line_max - tab->s->line_off < body_lines)
635 return;
637 l = nth_line(tab, tab->s->line_off + body_lines-1);
638 wmove(body, body_lines-1, 0);
639 print_line(l);
642 static void
643 cmd_scroll_up(struct tab *tab)
645 size_t off;
647 off = body_lines+1;
649 for (; off > 0; --off)
650 cmd_scroll_line_up(tab);
653 static void
654 cmd_scroll_down(struct tab *tab)
656 size_t off;
658 off = body_lines+1;
660 for (; off > 0; --off)
661 cmd_scroll_line_down(tab);
664 static void
665 cmd_beginning_of_buffer(struct tab *tab)
667 tab->s->line_off = 0;
668 tab->s->curs_y = 0;
669 redraw_body(tab);
672 static void
673 cmd_end_of_buffer(struct tab *tab)
675 ssize_t off;
677 off = tab->s->line_max - body_lines;
678 off = MAX(0, off);
680 tab->s->line_off = off;
681 tab->s->curs_y = MIN(body_lines, tab->s->line_max);
683 redraw_body(tab);
686 static void
687 cmd_kill_telescope(struct tab *tab)
689 event_loopbreak();
692 static void
693 cmd_push_button(struct tab *tab)
695 struct line *l;
696 size_t nth;
698 nth = tab->s->line_off + tab->s->curs_y;
699 if (nth >= tab->s->line_max)
700 return;
701 l = nth_line(tab, nth);
702 if (l->type != LINE_LINK)
703 return;
705 load_url_in_tab(tab, l->alt);
708 static void
709 cmd_push_button_new_tab(struct tab *tab)
711 struct tab *t;
712 struct line *l;
713 size_t nth;
715 nth = tab->s->line_off + tab->s->curs_y;
716 if (nth > tab->s->line_max)
717 return;
718 l = nth_line(tab, nth);
719 if (l->type != LINE_LINK)
720 return;
722 t = new_tab();
723 memcpy(&t->url, &tab->url, sizeof(tab->url));
724 memcpy(&t->urlstr, &tab->urlstr, sizeof(tab->urlstr));
725 load_url_in_tab(t, l->alt);
728 static void
729 cmd_clear_minibuf(struct tab *tab)
731 handle_clear_minibuf(0, 0, NULL);
734 static void
735 cmd_execute_extended_command(struct tab *tab)
737 size_t len;
739 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
740 &eecmd_history);
742 len = sizeof(ministate.prompt);
743 strlcpy(ministate.prompt, "", len);
745 if (thiskey.meta)
746 strlcat(ministate.prompt, "M-", len);
748 strlcat(ministate.prompt, keyname(thiskey.key), len);
749 strlcat(ministate.prompt, " ", len);
752 static void
753 cmd_tab_close(struct tab *tab)
755 struct tab *t;
757 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
758 TAILQ_NEXT(tab, tabs) == NULL) {
759 message("Can't close the only tab.");
760 return;
763 stop_tab(tab);
765 t = TAILQ_PREV(tab, tabshead, tabs);
766 t->flags |= TAB_CURRENT;
768 TAILQ_REMOVE(&tabshead, tab, tabs);
770 free(tab->s);
771 free(tab);
774 static void
775 cmd_tab_new(struct tab *tab)
777 new_tab();
780 static void
781 cmd_tab_next(struct tab *tab)
783 struct tab *t;
785 tab->flags &= ~TAB_CURRENT;
787 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
788 t = TAILQ_FIRST(&tabshead);
789 t->flags |= TAB_CURRENT;
792 static void
793 cmd_tab_previous(struct tab *tab)
795 struct tab *t;
797 tab->flags &= ~TAB_CURRENT;
799 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
800 t = TAILQ_LAST(&tabshead, tabshead);
801 t->flags |= TAB_CURRENT;
804 static void
805 cmd_load_url(struct tab *tab)
807 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
808 &lu_history);
809 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
812 static void
813 cmd_load_current_url(struct tab *tab)
815 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
816 &lu_history);
817 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
818 strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
819 ministate.off = strlen(tab->urlstr);
820 ministate.len = ministate.off;
823 static void
824 global_key_unbound(void)
826 message("%s is undefined", keybuf);
829 static void
830 cmd_mini_delete_char(struct tab *tab)
832 minibuffer_taint_hist();
834 if (ministate.len == 0 || ministate.off == ministate.len)
835 return;
837 memmove(&ministate.buf[ministate.off],
838 &ministate.buf[ministate.off+1],
839 ministate.len - ministate.off + 1);
840 ministate.len--;
843 static void
844 cmd_mini_delete_backward_char(struct tab *tab)
846 minibuffer_taint_hist();
848 if (ministate.len == 0 || ministate.off == 0)
849 return;
851 memmove(&ministate.buf[ministate.off-1],
852 &ministate.buf[ministate.off],
853 ministate.len - ministate.off + 1);
854 ministate.off--;
855 ministate.len--;
858 static void
859 cmd_mini_forward_char(struct tab *tab)
861 if (ministate.off == ministate.len)
862 return;
863 ministate.off++;
866 static void
867 cmd_mini_backward_char(struct tab *tab)
869 if (ministate.off == 0)
870 return;
871 ministate.off--;
874 static void
875 cmd_mini_move_end_of_line(struct tab *tab)
877 ministate.off = ministate.len;
880 static void
881 cmd_mini_move_beginning_of_line(struct tab *tab)
883 ministate.off = 0;
886 static void
887 cmd_mini_kill_line(struct tab *tab)
889 minibuffer_taint_hist();
891 if (ministate.off == ministate.len)
892 return;
893 ministate.buf[ministate.off] = '\0';
894 ministate.len -= ministate.off;
897 static void
898 cmd_mini_abort(struct tab *tab)
900 ministate.abortfn();
903 static void
904 cmd_mini_complete_and_exit(struct tab *tab)
906 minibuffer_taint_hist();
907 ministate.donefn();
910 static void
911 cmd_mini_previous_history_element(struct tab *tab)
913 if (ministate.history == NULL) {
914 message("No history");
915 return;
918 if (ministate.hist_cur == NULL ||
919 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
920 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
921 ministate.hist_off = ministate.history->len - 1;
922 if (ministate.hist_cur == NULL)
923 message("No prev item");
924 } else {
925 ministate.hist_off--;
928 if (ministate.hist_cur != NULL) {
929 ministate.off = 0;
930 ministate.len = strlen(ministate.hist_cur->h);
934 static void
935 cmd_mini_next_history_element(struct tab *tab)
937 if (ministate.history == NULL) {
938 message("No history");
939 return;
942 if (ministate.hist_cur == NULL ||
943 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
944 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
945 ministate.hist_off = 0;
946 if (ministate.hist_cur == NULL)
947 message("No next item");
948 } else {
949 ministate.hist_off++;
952 if (ministate.hist_cur != NULL) {
953 ministate.off = 0;
954 ministate.len = strlen(ministate.hist_cur->h);
958 static void
959 minibuffer_hist_save_entry(void)
961 struct minibuf_hist *hist;
963 if (ministate.history == NULL)
964 return;
966 if ((hist = calloc(1, sizeof(*hist))) == NULL)
967 abort();
969 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
971 if (TAILQ_EMPTY(&ministate.history->head))
972 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
973 else
974 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
975 ministate.history->len++;
978 /*
979 * taint the minibuffer cache: if we're currently showing a history
980 * element, copy that to the current buf and reset the "history
981 * navigation" thing.
982 */
983 static void
984 minibuffer_taint_hist(void)
986 if (ministate.hist_cur == NULL)
987 return;
989 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
990 ministate.hist_cur = NULL;
993 static void
994 minibuffer_self_insert(void)
996 minibuffer_taint_hist();
998 if (ministate.len == sizeof(ministate.buf) -1)
999 return;
1001 /* TODO: utf8 handling! */
1003 memmove(&ministate.buf[ministate.off+1],
1004 &ministate.buf[ministate.off],
1005 ministate.len - ministate.off + 1);
1006 ministate.buf[ministate.off] = thiskey.key;
1007 ministate.off++;
1008 ministate.len++;
1011 static void
1012 eecmd_self_insert(void)
1014 if (thiskey.meta || isspace(thiskey.key) ||
1015 !isgraph(thiskey.key)) {
1016 global_key_unbound();
1017 return;
1020 minibuffer_self_insert();
1023 static void
1024 eecmd_select(void)
1026 exit_minibuffer();
1027 minibuffer_hist_save_entry();
1028 message("TODO: try to execute %s", ministate.buf);
1031 static void
1032 ir_self_insert(void)
1034 minibuffer_self_insert();
1037 static void
1038 ir_select(void)
1040 char buf[1025] = {0};
1041 struct url url;
1042 struct tab *tab;
1044 tab = current_tab();
1046 exit_minibuffer();
1047 minibuffer_hist_save_entry();
1049 /* a bit ugly but... */
1050 memcpy(&url, &tab->url, sizeof(tab->url));
1051 url_set_query(&url, ministate.buf);
1052 url_unparse(&url, buf, sizeof(buf));
1053 load_url_in_tab(tab, buf);
1056 static void
1057 lu_self_insert(void)
1059 if (thiskey.meta || isspace(thiskey.key) ||
1060 !isgraph(thiskey.key)) {
1061 global_key_unbound();
1062 return;
1065 minibuffer_self_insert();
1068 static void
1069 lu_select(void)
1071 exit_minibuffer();
1072 minibuffer_hist_save_entry();
1073 load_url_in_tab(current_tab(), ministate.buf);
1076 static struct line *
1077 nth_line(struct tab *tab, size_t n)
1079 struct line *l;
1080 size_t i;
1082 i = 0;
1083 TAILQ_FOREACH(l, &tab->s->head, lines) {
1084 if (i == n)
1085 return l;
1086 i++;
1089 /* unreachable */
1090 abort();
1093 static struct tab *
1094 current_tab(void)
1096 struct tab *t;
1098 TAILQ_FOREACH(t, &tabshead, tabs) {
1099 if (t->flags & TAB_CURRENT)
1100 return t;
1103 /* unreachable */
1104 abort();
1107 static void
1108 dispatch_stdio(int fd, short ev, void *d)
1110 struct tab *tab;
1111 struct keymap *k;
1112 const char *keyname;
1113 char tmp[2] = {0};
1115 thiskey.key = wgetch(body);
1116 if (thiskey.key == ERR)
1117 return;
1118 if (thiskey.key == 27) {
1119 /* TODO: make escape-time customizable */
1121 thiskey.meta = 1;
1122 thiskey.key = wgetch(body);
1123 if (thiskey.key == ERR || thiskey.key == 27) {
1124 thiskey.meta = 0;
1125 thiskey.key = 27;
1127 } else
1128 thiskey.meta = 0;
1130 if (keybuf[0] != '\0')
1131 strlcat(keybuf, " ", sizeof(keybuf));
1132 if (thiskey.meta)
1133 strlcat(keybuf, "M-", sizeof(keybuf));
1134 if ((keyname = unkbd(thiskey.key)) != NULL)
1135 strlcat(keybuf, keyname, sizeof(keybuf));
1136 else {
1137 tmp[0] = thiskey.key;
1138 strlcat(keybuf, tmp, sizeof(keybuf));
1141 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1142 if (k->meta == thiskey.meta &&
1143 k->key == thiskey.key) {
1144 if (k->fn == NULL)
1145 current_map = &k->map;
1146 else {
1147 current_map = base_map;
1148 strlcpy(keybuf, "", sizeof(keybuf));
1149 k->fn(current_tab());
1151 goto done;
1155 if (current_map->unhandled_input != NULL)
1156 current_map->unhandled_input();
1157 else {
1158 global_key_unbound();
1161 strlcpy(keybuf, "", sizeof(keybuf));
1162 current_map = base_map;
1164 done:
1165 redraw_tab(current_tab());
1168 static void
1169 handle_clear_minibuf(int fd, short ev, void *d)
1171 clminibufev_set = 0;
1173 free(ministate.curmesg);
1174 ministate.curmesg = NULL;
1176 redraw_minibuffer();
1177 if (in_minibuffer) {
1178 wrefresh(body);
1179 wrefresh(minibuf);
1180 } else {
1181 wrefresh(minibuf);
1182 wrefresh(body);
1186 static void
1187 handle_resize(int sig, short ev, void *d)
1189 struct tab *tab;
1191 endwin();
1192 refresh();
1193 clear();
1195 /* move and resize the windows, in reverse order! */
1197 mvwin(minibuf, LINES-1, 0);
1198 wresize(minibuf, 1, COLS);
1200 mvwin(modeline, LINES-2, 0);
1201 wresize(modeline, 1, COLS);
1203 wresize(body, LINES-3, COLS);
1204 body_lines = LINES-3;
1205 body_cols = COLS;
1207 wresize(tabline, 1, COLS);
1209 tab = current_tab();
1211 wrap_page(tab);
1212 redraw_tab(tab);
1216 * Helper function for wrap_text. Find the end of the current word
1217 * and the end of the separator after the word.
1219 static int
1220 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1222 *endword = s;
1223 *endword = s;
1225 if (*s == '\0')
1226 return 0;
1228 /* find the end of the current world */
1229 for (; *s != '\0'; ++s) {
1230 if (strchr(sep, *s) != NULL)
1231 break;
1234 *endword = s;
1236 /* find the end of the separator */
1237 for (; *s != '\0'; ++s) {
1238 if (strchr(sep, *s) == NULL)
1239 break;
1242 *endspc = s;
1244 return 1;
1247 static inline int
1248 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1249 const char **line, int *cont)
1251 if (!push_line(tab, l, *line, *off - zero, *cont))
1252 return 0;
1253 if (!*cont)
1254 *cont = 1;
1255 *line += *off - zero;
1256 *off = zero;
1257 return 1;
1260 static inline void
1261 emitstr(const char **s, size_t len, size_t *off)
1263 size_t i;
1265 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1266 for (i = 0; i < len; ++i)
1267 addch((*s)[i]);
1268 *off += len;
1269 *s += len;
1273 * Build a list of visual line by wrapping the given line, assuming
1274 * that when printed will have a leading prefix prfx.
1276 * TODO: it considers each byte one cell on the screen!
1278 static void
1279 wrap_text(struct tab *tab, const char *prfx, struct line *l)
1281 size_t zero, off, len, split;
1282 int cont = 0;
1283 const char *endword, *endspc, *line, *linestart;
1285 zero = strlen(prfx);
1286 off = zero;
1287 line = l->line;
1288 linestart = l->line;
1290 while (word_boundaries(line, " \t-", &endword, &endspc)) {
1291 len = endword - line;
1292 if (off + len >= body_cols) {
1293 emitline(tab, zero, &off, l, &linestart, &cont);
1294 while (len >= body_cols) {
1295 /* hard wrap */
1296 emitline(tab, zero, &off, l, &linestart, &cont);
1297 len -= body_cols-1;
1298 line += body_cols-1;
1301 if (len != 0)
1302 off += len;
1303 } else
1304 off += len;
1306 /* print the spaces iff not at bol */
1307 len = endspc - endword;
1308 /* line = endspc; */
1309 if (off != zero) {
1310 if (off + len >= body_cols) {
1311 emitline(tab, zero, &off, l, &linestart, &cont);
1312 linestart = endspc;
1313 } else
1314 off += len;
1317 line = endspc;
1320 emitline(tab, zero, &off, l, &linestart, &cont);
1323 static int
1324 hardwrap_text(struct tab *tab, struct line *l)
1326 size_t off, len;
1327 int cont;
1328 const char *linestart;
1330 if (l->line == NULL)
1331 return emitline(tab, 0, &off, l, &linestart, &cont);
1333 len = strlen(l->line);
1334 off = 0;
1335 linestart = l->line;
1337 while (len >= COLS) {
1338 len -= COLS-1;
1339 off = COLS-1;
1340 if (!emitline(tab, 0, &off, l, &linestart, &cont))
1341 return 0;
1344 if (len != 0)
1345 return emitline(tab, 0, &len, l, &linestart, &cont);
1347 return 1;
1350 static int
1351 wrap_page(struct tab *tab)
1353 struct line *l;
1354 const char *prfx;
1356 empty_vlist(tab);
1358 TAILQ_FOREACH(l, &tab->page.head, lines) {
1359 prfx = line_prefixes[l->type].prfx1;
1360 switch (l->type) {
1361 case LINE_TEXT:
1362 case LINE_LINK:
1363 case LINE_TITLE_1:
1364 case LINE_TITLE_2:
1365 case LINE_TITLE_3:
1366 case LINE_ITEM:
1367 case LINE_QUOTE:
1368 wrap_text(tab, prfx, l);
1369 break;
1370 case LINE_PRE_START:
1371 case LINE_PRE_END:
1372 push_line(tab, l, NULL, 0, 0);
1373 break;
1374 case LINE_PRE_CONTENT:
1375 hardwrap_text(tab, l);
1376 break;
1379 return 1;
1382 static inline void
1383 print_line(struct line *l)
1385 const char *text = l->line;
1386 const char *prfx;
1387 int face = line_faces[l->type].prop;
1389 if (!l->flags)
1390 prfx = line_prefixes[l->type].prfx1;
1391 else
1392 prfx = line_prefixes[l->type].prfx2;
1394 if (text == NULL)
1395 text = "";
1397 if (face != 0)
1398 wattron(body, face);
1399 wprintw(body, "%s%s", prfx, text);
1400 if (face != 0)
1401 wattroff(body, face);
1404 static void
1405 redraw_tabline(void)
1407 struct tab *tab;
1408 int current;
1410 werase(tabline);
1411 wbkgd(tabline, A_REVERSE);
1413 wprintw(tabline, " ");
1414 TAILQ_FOREACH(tab, &tabshead, tabs) {
1415 current = tab->flags & TAB_CURRENT;
1417 if (current)
1418 wattron(tabline, A_UNDERLINE);
1420 wprintw(tabline, "%s%d:todo title ",
1421 current ? "*" : " ", tab->id);
1423 if (current)
1424 wattroff(tabline, A_UNDERLINE);
1428 static void
1429 redraw_modeline(struct tab *tab)
1431 double pct;
1432 int x, y, max_x, max_y;
1433 const char *mode = tab->page.name;
1434 const char *spin = "-\\|/";
1436 werase(modeline);
1437 wattron(modeline, A_REVERSE);
1438 wmove(modeline, 0, 0);
1440 wprintw(modeline, "-%c %s-mode ",
1441 spin[tab->s->loading_anim_step], mode);
1443 pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1445 if (tab->s->line_max <= body_lines)
1446 wprintw(modeline, "All ");
1447 else if (tab->s->line_off == 0)
1448 wprintw(modeline, "Top ");
1449 else if (tab->s->line_off + body_lines >= tab->s->line_max)
1450 wprintw(modeline, "Bottom ");
1451 else
1452 wprintw(modeline, "%.0f%% ", pct);
1454 wprintw(modeline, "%d/%d %s ",
1455 tab->s->line_off + tab->s->curs_y,
1456 tab->s->line_max,
1457 tab->urlstr);
1459 getyx(modeline, y, x);
1460 getmaxyx(modeline, max_y, max_x);
1462 (void)y;
1463 (void)max_y;
1465 for (; x < max_x; ++x)
1466 waddstr(modeline, "-");
1469 static void
1470 redraw_minibuffer(void)
1472 size_t skip = 0, off_x = 0, off_y = 0;
1474 werase(minibuf);
1475 if (in_minibuffer) {
1476 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1477 if (ministate.hist_cur != NULL)
1478 wprintw(minibuf, "(%zu/%zu) ",
1479 ministate.hist_off + 1,
1480 ministate.history->len);
1482 getyx(minibuf, off_y, off_x);
1484 while (ministate.off - skip > COLS / 2) {
1485 skip += MIN(ministate.off/4, 1);
1488 if (ministate.hist_cur != NULL)
1489 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1490 else
1491 wprintw(minibuf, "%s", ministate.buf + skip);
1494 if (ministate.curmesg != NULL) {
1495 if (in_minibuffer)
1496 wprintw(minibuf, " [%s]", ministate.curmesg);
1497 else
1498 wprintw(minibuf, "%s", ministate.curmesg);
1501 if (!in_minibuffer && ministate.curmesg == NULL)
1502 wprintw(minibuf, "%s", keybuf);
1504 wmove(minibuf, 0, off_x + ministate.off - skip);
1507 static void
1508 redraw_tab(struct tab *tab)
1510 redraw_tabline();
1511 redraw_body(tab);
1512 redraw_modeline(tab);
1513 redraw_minibuffer();
1515 restore_cursor(tab);
1516 wrefresh(tabline);
1517 wrefresh(modeline);
1519 if (in_minibuffer) {
1520 wrefresh(body);
1521 wrefresh(minibuf);
1522 } else {
1523 wrefresh(minibuf);
1524 wrefresh(body);
1528 static void
1529 redraw_body(struct tab *tab)
1531 struct line *l;
1532 int line;
1534 werase(body);
1536 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1537 if (TAILQ_EMPTY(&tab->s->head))
1538 return;
1540 line = 0;
1541 l = nth_line(tab, tab->s->line_off);
1542 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1543 wmove(body, line, 0);
1544 print_line(l);
1545 line++;
1546 if (line == body_lines)
1547 break;
1551 static void
1552 message(const char *fmt, ...)
1554 va_list ap;
1556 if (clminibufev_set)
1557 evtimer_del(&clminibufev);
1558 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1559 evtimer_add(&clminibufev, &clminibufev_timer);
1560 clminibufev_set = 1;
1562 free(ministate.curmesg);
1564 va_start(ap, fmt);
1565 /* TODO: what to do if the allocation fails here? */
1566 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1567 ministate.curmesg = NULL;
1568 va_end(ap);
1570 redraw_minibuffer();
1572 if (in_minibuffer) {
1573 wrefresh(body);
1574 wrefresh(minibuf);
1575 } else {
1576 wrefresh(minibuf);
1577 wrefresh(body);
1581 static void
1582 start_loading_anim(struct tab *tab)
1584 if (tab->s->loading_anim)
1585 return;
1586 tab->s->loading_anim = 1;
1587 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1588 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1591 static void
1592 update_loading_anim(int fd, short ev, void *d)
1594 struct tab *tab = d;
1596 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1598 redraw_modeline(tab);
1599 wrefresh(modeline);
1601 wrefresh(body);
1602 if (in_minibuffer)
1603 wrefresh(minibuf);
1605 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1608 static void
1609 stop_loading_anim(struct tab *tab)
1611 if (!tab->s->loading_anim)
1612 return;
1613 evtimer_del(&tab->s->loadingev);
1614 tab->s->loading_anim = 0;
1615 tab->s->loading_anim_step = 0;
1617 redraw_modeline(tab);
1619 wrefresh(modeline);
1620 wrefresh(body);
1621 if (in_minibuffer)
1622 wrefresh(minibuf);
1625 static void
1626 load_url_in_tab(struct tab *tab, const char *url)
1628 empty_vlist(tab);
1629 message("Loading %s...", url);
1630 start_loading_anim(tab);
1631 load_url(tab, url);
1633 tab->s->curs_x = 0;
1634 tab->s->curs_y = 0;
1635 redraw_tab(tab);
1638 static void
1639 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1640 void (*abortfn)(void), struct minibuf_histhead *hist)
1642 in_minibuffer = 1;
1643 base_map = &minibuffer_map;
1644 current_map = &minibuffer_map;
1646 base_map->unhandled_input = self_insert_fn;
1648 ministate.donefn = donefn;
1649 ministate.abortfn = abortfn;
1650 memset(ministate.buf, 0, sizeof(ministate.buf));
1651 ministate.off = 0;
1652 ministate.len = 0;
1653 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1655 ministate.history = hist;
1656 ministate.hist_cur = NULL;
1657 ministate.hist_off = 0;
1660 static void
1661 exit_minibuffer(void)
1663 werase(minibuf);
1665 in_minibuffer = 0;
1666 base_map = &global_map;
1667 current_map = &global_map;
1670 static void
1671 switch_to_tab(struct tab *tab)
1673 struct tab *t;
1675 TAILQ_FOREACH(t, &tabshead, tabs) {
1676 t->flags &= ~TAB_CURRENT;
1679 tab->flags |= TAB_CURRENT;
1682 static struct tab *
1683 new_tab(void)
1685 struct tab *tab, *t;
1686 const char *url = "about:new";
1688 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1689 goto err;
1691 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1692 goto err;
1694 TAILQ_INIT(&tab->s->head);
1696 tab->id = tab_counter++;
1697 switch_to_tab(tab);
1699 if (TAILQ_EMPTY(&tabshead))
1700 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1701 else
1702 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1704 load_url_in_tab(tab, url);
1705 return tab;
1707 err:
1708 event_loopbreak();
1709 return NULL;
1712 int
1713 ui_init(void)
1715 setlocale(LC_ALL, "");
1717 TAILQ_INIT(&global_map.m);
1718 global_map.unhandled_input = global_key_unbound;
1720 TAILQ_INIT(&minibuffer_map.m);
1722 TAILQ_INIT(&eecmd_history.head);
1723 TAILQ_INIT(&ir_history.head);
1724 TAILQ_INIT(&lu_history.head);
1726 base_map = &global_map;
1727 current_map = &global_map;
1728 load_default_keys();
1730 initscr();
1731 raw();
1732 noecho();
1734 nonl();
1735 intrflush(stdscr, FALSE);
1737 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1738 return 0;
1739 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1740 return 0;
1741 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1742 return 0;
1743 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1744 return 0;
1746 body_lines = LINES-3;
1747 body_cols = COLS;
1749 keypad(body, TRUE);
1750 scrollok(body, TRUE);
1752 /* non-blocking input */
1753 wtimeout(body, 0);
1755 mvwprintw(body, 0, 0, "");
1757 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1758 event_add(&stdioev, NULL);
1760 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1761 signal_add(&winchev, NULL);
1763 new_tab();
1765 return 1;
1768 void
1769 ui_on_tab_loaded(struct tab *tab)
1771 stop_loading_anim(tab);
1772 message("Loaded %s", tab->urlstr);
1775 void
1776 ui_on_tab_refresh(struct tab *tab)
1778 if (!(tab->flags & TAB_CURRENT))
1779 return;
1781 wrap_page(tab);
1782 redraw_tab(tab);
1785 void
1786 ui_require_input(struct tab *tab, int hide)
1788 /* TODO: hard-switching to another tab is ugly */
1789 switch_to_tab(tab);
1791 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1792 &ir_history);
1793 strlcpy(ministate.prompt, "Input required: ",
1794 sizeof(ministate.prompt));
1795 redraw_tab(tab);
1798 void
1799 ui_end(void)
1801 endwin();