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);
404 global_set_key("C-x C-c", cmd_kill_telescope);
406 global_set_key("C-g", cmd_clear_minibuf);
408 global_set_key("M-x", cmd_execute_extended_command);
409 global_set_key("C-x C-f", cmd_load_url);
410 global_set_key("C-x M-f", cmd_load_current_url);
412 global_set_key("C-x t 0", cmd_tab_close);
413 global_set_key("C-x t 2", cmd_tab_new);
414 global_set_key("C-x t o", cmd_tab_next);
415 global_set_key("C-x t O", cmd_tab_previous);
417 global_set_key("M-<", cmd_beginning_of_buffer);
418 global_set_key("M->", cmd_end_of_buffer);
420 /* vi/vi-like */
421 global_set_key("k", cmd_previous_line);
422 global_set_key("j", cmd_next_line);
423 global_set_key("l", cmd_forward_char);
424 global_set_key("h", cmd_backward_char);
425 global_set_key("^", cmd_move_beginning_of_line);
426 global_set_key("$", cmd_move_end_of_line);
428 global_set_key("K", cmd_scroll_line_up);
429 global_set_key("J", cmd_scroll_line_down);
431 global_set_key("g g", cmd_beginning_of_buffer);
432 global_set_key("G", cmd_end_of_buffer);
434 /* tmp */
435 global_set_key("q", cmd_kill_telescope);
437 global_set_key("esc", cmd_clear_minibuf);
439 global_set_key(":", cmd_execute_extended_command);
441 /* cua */
442 global_set_key("<up>", cmd_previous_line);
443 global_set_key("<down>", cmd_next_line);
444 global_set_key("<right>", cmd_forward_char);
445 global_set_key("<left>", cmd_backward_char);
446 global_set_key("<prior>", cmd_scroll_up);
447 global_set_key("<next>", cmd_scroll_down);
449 /* "ncurses standard" */
450 global_set_key("C-l", cmd_redraw);
452 /* global */
453 global_set_key("C-m", cmd_push_button);
454 global_set_key("M-enter", cmd_push_button_new_tab);
456 /* === minibuffer map === */
457 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
458 minibuffer_set_key("C-g", cmd_mini_abort);
459 minibuffer_set_key("esc", cmd_mini_abort);
460 minibuffer_set_key("C-d", cmd_mini_delete_char);
461 minibuffer_set_key("del", cmd_mini_delete_backward_char);
463 minibuffer_set_key("C-f", cmd_mini_forward_char);
464 minibuffer_set_key("C-b", cmd_mini_backward_char);
465 minibuffer_set_key("<right>", cmd_mini_forward_char);
466 minibuffer_set_key("<left>", cmd_mini_backward_char);
467 minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
468 minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
469 minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
470 minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
471 minibuffer_set_key("C-k", cmd_mini_kill_line);
473 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
474 minibuffer_set_key("M-n", cmd_mini_next_history_element);
475 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
476 minibuffer_set_key("<down>", cmd_mini_next_history_element);
479 static int
480 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len, int cont)
482 struct line *vl;
484 tab->s->line_max++;
486 if ((vl = calloc(1, sizeof(*vl))) == NULL)
487 return 0;
489 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
490 free(vl);
491 return 0;
494 vl->type = l->type;
495 if (len != 0)
496 memcpy(vl->line, buf, len);
497 vl->alt = l->alt;
498 vl->flags = cont;
500 if (TAILQ_EMPTY(&tab->s->head))
501 TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
502 else
503 TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
504 return 1;
507 static void
508 empty_vlist(struct tab *tab)
510 struct line *l, *t;
512 tab->s->line_max = 0;
514 TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
515 TAILQ_REMOVE(&tab->s->head, l, lines);
516 free(l->line);
517 /* l->alt references the original line! */
518 free(l);
522 static void
523 restore_cursor(struct tab *tab)
525 wmove(body, tab->s->curs_y, tab->s->curs_x);
528 static void
529 cmd_previous_line(struct tab *tab)
531 if (--tab->s->curs_y < 0) {
532 tab->s->curs_y = 0;
533 cmd_scroll_line_up(tab);
536 restore_cursor(tab);
539 static void
540 cmd_next_line(struct tab *tab)
542 if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
543 return;
545 if (++tab->s->curs_y > body_lines-1) {
546 tab->s->curs_y = body_lines-1;
547 cmd_scroll_line_down(tab);
550 restore_cursor(tab);
553 static void
554 cmd_forward_char(struct tab *tab)
556 tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
557 restore_cursor(tab);
560 static void
561 cmd_backward_char(struct tab *tab)
563 tab->s->curs_x = MAX(0, tab->s->curs_x-1);
564 restore_cursor(tab);
567 static void
568 cmd_move_beginning_of_line(struct tab *tab)
570 tab->s->curs_x = 0;
571 restore_cursor(tab);
574 static void
575 cmd_move_end_of_line(struct tab *tab)
577 struct line *line;
578 size_t off;
579 const char *prfx;
581 off = tab->s->line_off + tab->s->curs_y;
582 if (off >= tab->s->line_max) {
583 tab->s->curs_x = 0;
584 goto end;
587 line = nth_line(tab, off);
588 if (line->line != NULL)
589 tab->s->curs_x = strlen(line->line);
590 else
591 tab->s->curs_x = 0;
593 prfx = line_prefixes[line->type].prfx1;
594 tab->s->curs_x += strlen(prfx);
596 end:
597 restore_cursor(tab);
600 static void
601 cmd_redraw(struct tab *tab)
603 handle_resize(0, 0, NULL);
606 static void
607 cmd_scroll_line_up(struct tab *tab)
609 struct line *l;
611 if (tab->s->line_off == 0)
612 return;
614 l = nth_line(tab, --tab->s->line_off);
615 wscrl(body, -1);
616 wmove(body, 0, 0);
617 print_line(l);
620 static void
621 cmd_scroll_line_down(struct tab *tab)
623 struct line *l;
624 size_t n;
626 if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
627 return;
629 tab->s->line_off++;
630 wscrl(body, 1);
632 if (tab->s->line_max - tab->s->line_off < body_lines)
633 return;
635 l = nth_line(tab, tab->s->line_off + body_lines-1);
636 wmove(body, body_lines-1, 0);
637 print_line(l);
640 static void
641 cmd_scroll_up(struct tab *tab)
643 size_t off;
645 off = body_lines+1;
647 for (; off > 0; --off)
648 cmd_scroll_line_up(tab);
651 static void
652 cmd_scroll_down(struct tab *tab)
654 size_t off;
656 off = body_lines+1;
658 for (; off > 0; --off)
659 cmd_scroll_line_down(tab);
662 static void
663 cmd_beginning_of_buffer(struct tab *tab)
665 tab->s->line_off = 0;
666 tab->s->curs_y = 0;
667 redraw_body(tab);
670 static void
671 cmd_end_of_buffer(struct tab *tab)
673 ssize_t off;
675 off = tab->s->line_max - body_lines;
676 off = MAX(0, off);
678 tab->s->line_off = off;
679 tab->s->curs_y = MIN(body_lines, tab->s->line_max);
681 redraw_body(tab);
684 static void
685 cmd_kill_telescope(struct tab *tab)
687 event_loopbreak();
690 static void
691 cmd_push_button(struct tab *tab)
693 struct line *l;
694 size_t nth;
696 nth = tab->s->line_off + tab->s->curs_y;
697 if (nth > tab->s->line_max)
698 return;
699 l = nth_line(tab, nth);
700 if (l->type != LINE_LINK)
701 return;
703 load_url_in_tab(tab, l->alt);
706 static void
707 cmd_push_button_new_tab(struct tab *tab)
709 struct tab *t;
710 struct line *l;
711 size_t nth;
713 nth = tab->s->line_off + tab->s->curs_y;
714 if (nth > tab->s->line_max)
715 return;
716 l = nth_line(tab, nth);
717 if (l->type != LINE_LINK)
718 return;
720 t = new_tab();
721 memcpy(&t->url, &tab->url, sizeof(tab->url));
722 memcpy(&t->urlstr, &tab->urlstr, sizeof(tab->urlstr));
723 load_url_in_tab(t, l->alt);
726 static void
727 cmd_clear_minibuf(struct tab *tab)
729 handle_clear_minibuf(0, 0, NULL);
732 static void
733 cmd_execute_extended_command(struct tab *tab)
735 size_t len;
737 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
738 &eecmd_history);
740 len = sizeof(ministate.prompt);
741 strlcpy(ministate.prompt, "", len);
743 if (thiskey.meta)
744 strlcat(ministate.prompt, "M-", len);
746 strlcat(ministate.prompt, keyname(thiskey.key), len);
747 strlcat(ministate.prompt, " ", len);
750 static void
751 cmd_tab_close(struct tab *tab)
753 struct tab *t;
755 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
756 TAILQ_NEXT(tab, tabs) == NULL) {
757 message("Can't close the only tab.");
758 return;
761 stop_tab(tab);
763 t = TAILQ_PREV(tab, tabshead, tabs);
764 t->flags |= TAB_CURRENT;
766 TAILQ_REMOVE(&tabshead, tab, tabs);
768 free(tab->s);
769 free(tab);
772 static void
773 cmd_tab_new(struct tab *tab)
775 new_tab();
778 static void
779 cmd_tab_next(struct tab *tab)
781 struct tab *t;
783 tab->flags &= ~TAB_CURRENT;
785 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
786 t = TAILQ_FIRST(&tabshead);
787 t->flags |= TAB_CURRENT;
790 static void
791 cmd_tab_previous(struct tab *tab)
793 struct tab *t;
795 tab->flags &= ~TAB_CURRENT;
797 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
798 t = TAILQ_LAST(&tabshead, tabshead);
799 t->flags |= TAB_CURRENT;
802 static void
803 cmd_load_url(struct tab *tab)
805 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
806 &lu_history);
807 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
810 static void
811 cmd_load_current_url(struct tab *tab)
813 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
814 &lu_history);
815 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
816 strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
817 ministate.off = strlen(tab->urlstr);
818 ministate.len = ministate.off;
821 static void
822 global_key_unbound(void)
824 message("%s is undefined", keybuf);
827 static void
828 cmd_mini_delete_char(struct tab *tab)
830 minibuffer_taint_hist();
832 if (ministate.len == 0 || ministate.off == ministate.len)
833 return;
835 memmove(&ministate.buf[ministate.off],
836 &ministate.buf[ministate.off+1],
837 ministate.len - ministate.off + 1);
838 ministate.len--;
841 static void
842 cmd_mini_delete_backward_char(struct tab *tab)
844 minibuffer_taint_hist();
846 if (ministate.len == 0 || ministate.off == 0)
847 return;
849 memmove(&ministate.buf[ministate.off-1],
850 &ministate.buf[ministate.off],
851 ministate.len - ministate.off + 1);
852 ministate.off--;
853 ministate.len--;
856 static void
857 cmd_mini_forward_char(struct tab *tab)
859 if (ministate.off == ministate.len)
860 return;
861 ministate.off++;
864 static void
865 cmd_mini_backward_char(struct tab *tab)
867 if (ministate.off == 0)
868 return;
869 ministate.off--;
872 static void
873 cmd_mini_move_end_of_line(struct tab *tab)
875 ministate.off = ministate.len;
878 static void
879 cmd_mini_move_beginning_of_line(struct tab *tab)
881 ministate.off = 0;
884 static void
885 cmd_mini_kill_line(struct tab *tab)
887 minibuffer_taint_hist();
889 if (ministate.off == ministate.len)
890 return;
891 ministate.buf[ministate.off] = '\0';
892 ministate.len -= ministate.off;
895 static void
896 cmd_mini_abort(struct tab *tab)
898 ministate.abortfn();
901 static void
902 cmd_mini_complete_and_exit(struct tab *tab)
904 minibuffer_taint_hist();
905 ministate.donefn();
908 static void
909 cmd_mini_previous_history_element(struct tab *tab)
911 if (ministate.history == NULL) {
912 message("No history");
913 return;
916 if (ministate.hist_cur == NULL ||
917 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
918 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
919 ministate.hist_off = ministate.history->len - 1;
920 if (ministate.hist_cur == NULL)
921 message("No prev item");
922 } else {
923 ministate.hist_off--;
926 if (ministate.hist_cur != NULL) {
927 ministate.off = 0;
928 ministate.len = strlen(ministate.hist_cur->h);
932 static void
933 cmd_mini_next_history_element(struct tab *tab)
935 if (ministate.history == NULL) {
936 message("No history");
937 return;
940 if (ministate.hist_cur == NULL ||
941 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
942 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
943 ministate.hist_off = 0;
944 if (ministate.hist_cur == NULL)
945 message("No next item");
946 } else {
947 ministate.hist_off++;
950 if (ministate.hist_cur != NULL) {
951 ministate.off = 0;
952 ministate.len = strlen(ministate.hist_cur->h);
956 static void
957 minibuffer_hist_save_entry(void)
959 struct minibuf_hist *hist;
961 if (ministate.history == NULL)
962 return;
964 if ((hist = calloc(1, sizeof(*hist))) == NULL)
965 abort();
967 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
969 if (TAILQ_EMPTY(&ministate.history->head))
970 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
971 else
972 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
973 ministate.history->len++;
976 /*
977 * taint the minibuffer cache: if we're currently showing a history
978 * element, copy that to the current buf and reset the "history
979 * navigation" thing.
980 */
981 static void
982 minibuffer_taint_hist(void)
984 if (ministate.hist_cur == NULL)
985 return;
987 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
988 ministate.hist_cur = NULL;
991 static void
992 minibuffer_self_insert(void)
994 minibuffer_taint_hist();
996 if (ministate.len == sizeof(ministate.buf) -1)
997 return;
999 /* TODO: utf8 handling! */
1001 memmove(&ministate.buf[ministate.off+1],
1002 &ministate.buf[ministate.off],
1003 ministate.len - ministate.off + 1);
1004 ministate.buf[ministate.off] = thiskey.key;
1005 ministate.off++;
1006 ministate.len++;
1009 static void
1010 eecmd_self_insert(void)
1012 if (thiskey.meta || isspace(thiskey.key) ||
1013 !isgraph(thiskey.key)) {
1014 global_key_unbound();
1015 return;
1018 minibuffer_self_insert();
1021 static void
1022 eecmd_select(void)
1024 exit_minibuffer();
1025 minibuffer_hist_save_entry();
1026 message("TODO: try to execute %s", ministate.buf);
1029 static void
1030 ir_self_insert(void)
1032 minibuffer_self_insert();
1035 static void
1036 ir_select(void)
1038 char buf[1025] = {0};
1039 struct url url;
1040 struct tab *tab;
1042 tab = current_tab();
1044 exit_minibuffer();
1045 minibuffer_hist_save_entry();
1047 /* a bit ugly but... */
1048 memcpy(&url, &tab->url, sizeof(tab->url));
1049 url_set_query(&url, ministate.buf);
1050 url_unparse(&url, buf, sizeof(buf));
1051 load_url_in_tab(tab, buf);
1054 static void
1055 lu_self_insert(void)
1057 if (thiskey.meta || isspace(thiskey.key) ||
1058 !isgraph(thiskey.key)) {
1059 global_key_unbound();
1060 return;
1063 minibuffer_self_insert();
1066 static void
1067 lu_select(void)
1069 exit_minibuffer();
1070 minibuffer_hist_save_entry();
1071 load_url_in_tab(current_tab(), ministate.buf);
1074 static struct line *
1075 nth_line(struct tab *tab, size_t n)
1077 struct line *l;
1078 size_t i;
1080 i = 0;
1081 TAILQ_FOREACH(l, &tab->s->head, lines) {
1082 if (i == n)
1083 return l;
1084 i++;
1087 /* unreachable */
1088 abort();
1091 static struct tab *
1092 current_tab(void)
1094 struct tab *t;
1096 TAILQ_FOREACH(t, &tabshead, tabs) {
1097 if (t->flags & TAB_CURRENT)
1098 return t;
1101 /* unreachable */
1102 abort();
1105 static void
1106 dispatch_stdio(int fd, short ev, void *d)
1108 struct tab *tab;
1109 struct keymap *k;
1110 const char *keyname;
1111 char tmp[2] = {0};
1113 thiskey.key = wgetch(body);
1114 if (thiskey.key == ERR)
1115 return;
1116 if (thiskey.key == 27) {
1117 /* TODO: make escape-time customizable */
1119 thiskey.meta = 1;
1120 thiskey.key = wgetch(body);
1121 if (thiskey.key == ERR || thiskey.key == 27) {
1122 thiskey.meta = 0;
1123 thiskey.key = 27;
1125 } else
1126 thiskey.meta = 0;
1128 if (keybuf[0] != '\0')
1129 strlcat(keybuf, " ", sizeof(keybuf));
1130 if (thiskey.meta)
1131 strlcat(keybuf, "M-", sizeof(keybuf));
1132 if ((keyname = unkbd(thiskey.key)) != NULL)
1133 strlcat(keybuf, keyname, sizeof(keybuf));
1134 else {
1135 tmp[0] = thiskey.key;
1136 strlcat(keybuf, tmp, sizeof(keybuf));
1139 TAILQ_FOREACH(k, &current_map->m, keymaps) {
1140 if (k->meta == thiskey.meta &&
1141 k->key == thiskey.key) {
1142 if (k->fn == NULL)
1143 current_map = &k->map;
1144 else {
1145 current_map = base_map;
1146 strlcpy(keybuf, "", sizeof(keybuf));
1147 k->fn(current_tab());
1149 goto done;
1153 if (current_map->unhandled_input != NULL)
1154 current_map->unhandled_input();
1155 else {
1156 global_key_unbound();
1159 strlcpy(keybuf, "", sizeof(keybuf));
1160 current_map = base_map;
1162 done:
1163 redraw_tab(current_tab());
1166 static void
1167 handle_clear_minibuf(int fd, short ev, void *d)
1169 clminibufev_set = 0;
1171 free(ministate.curmesg);
1172 ministate.curmesg = NULL;
1174 redraw_minibuffer();
1175 if (in_minibuffer) {
1176 wrefresh(body);
1177 wrefresh(minibuf);
1178 } else {
1179 wrefresh(minibuf);
1180 wrefresh(body);
1184 static void
1185 handle_resize(int sig, short ev, void *d)
1187 struct tab *tab;
1189 endwin();
1190 refresh();
1191 clear();
1193 /* move and resize the windows, in reverse order! */
1195 mvwin(minibuf, LINES-1, 0);
1196 wresize(minibuf, 1, COLS);
1198 mvwin(modeline, LINES-2, 0);
1199 wresize(modeline, 1, COLS);
1201 wresize(body, LINES-3, COLS);
1202 body_lines = LINES-3;
1203 body_cols = COLS;
1205 wresize(tabline, 1, COLS);
1207 tab = current_tab();
1209 wrap_page(tab);
1210 redraw_tab(tab);
1214 * Helper function for wrap_text. Find the end of the current word
1215 * and the end of the separator after the word.
1217 static int
1218 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1220 *endword = s;
1221 *endword = s;
1223 if (*s == '\0')
1224 return 0;
1226 /* find the end of the current world */
1227 for (; *s != '\0'; ++s) {
1228 if (strchr(sep, *s) != NULL)
1229 break;
1232 *endword = s;
1234 /* find the end of the separator */
1235 for (; *s != '\0'; ++s) {
1236 if (strchr(sep, *s) == NULL)
1237 break;
1240 *endspc = s;
1242 return 1;
1245 static inline int
1246 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1247 const char **line, int *cont)
1249 if (!push_line(tab, l, *line, *off - zero, *cont))
1250 return 0;
1251 if (!*cont)
1252 *cont = 1;
1253 *line += *off - zero;
1254 *off = zero;
1255 return 1;
1258 static inline void
1259 emitstr(const char **s, size_t len, size_t *off)
1261 size_t i;
1263 /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1264 for (i = 0; i < len; ++i)
1265 addch((*s)[i]);
1266 *off += len;
1267 *s += len;
1271 * Build a list of visual line by wrapping the given line, assuming
1272 * that when printed will have a leading prefix prfx.
1274 * TODO: it considers each byte one cell on the screen!
1276 static void
1277 wrap_text(struct tab *tab, const char *prfx, struct line *l)
1279 size_t zero, off, len, split;
1280 int cont = 0;
1281 const char *endword, *endspc, *line, *linestart;
1283 zero = strlen(prfx);
1284 off = zero;
1285 line = l->line;
1286 linestart = l->line;
1288 while (word_boundaries(line, " \t-", &endword, &endspc)) {
1289 len = endword - line;
1290 if (off + len >= body_cols) {
1291 emitline(tab, zero, &off, l, &linestart, &cont);
1292 while (len >= body_cols) {
1293 /* hard wrap */
1294 emitline(tab, zero, &off, l, &linestart, &cont);
1295 len -= body_cols-1;
1296 line += body_cols-1;
1299 if (len != 0)
1300 off += len;
1301 } else
1302 off += len;
1304 /* print the spaces iff not at bol */
1305 len = endspc - endword;
1306 /* line = endspc; */
1307 if (off != zero) {
1308 if (off + len >= body_cols) {
1309 emitline(tab, zero, &off, l, &linestart, &cont);
1310 linestart = endspc;
1311 } else
1312 off += len;
1315 line = endspc;
1318 emitline(tab, zero, &off, l, &linestart, &cont);
1321 static int
1322 hardwrap_text(struct tab *tab, struct line *l)
1324 size_t off, len;
1325 int cont;
1326 const char *linestart;
1328 if (l->line == NULL)
1329 return emitline(tab, 0, &off, l, &linestart, &cont);
1331 len = strlen(l->line);
1332 off = 0;
1333 linestart = l->line;
1335 while (len >= COLS) {
1336 len -= COLS-1;
1337 off = COLS-1;
1338 if (!emitline(tab, 0, &off, l, &linestart, &cont))
1339 return 0;
1342 if (len != 0)
1343 return emitline(tab, 0, &len, l, &linestart, &cont);
1345 return 1;
1348 static int
1349 wrap_page(struct tab *tab)
1351 struct line *l;
1352 const char *prfx;
1354 empty_vlist(tab);
1356 TAILQ_FOREACH(l, &tab->page.head, lines) {
1357 prfx = line_prefixes[l->type].prfx1;
1358 switch (l->type) {
1359 case LINE_TEXT:
1360 case LINE_LINK:
1361 case LINE_TITLE_1:
1362 case LINE_TITLE_2:
1363 case LINE_TITLE_3:
1364 case LINE_ITEM:
1365 case LINE_QUOTE:
1366 wrap_text(tab, prfx, l);
1367 break;
1368 case LINE_PRE_START:
1369 case LINE_PRE_END:
1370 push_line(tab, l, NULL, 0, 0);
1371 break;
1372 case LINE_PRE_CONTENT:
1373 hardwrap_text(tab, l);
1374 break;
1377 return 1;
1380 static inline void
1381 print_line(struct line *l)
1383 const char *text = l->line;
1384 const char *prfx;
1385 int face = line_faces[l->type].prop;
1387 if (!l->flags)
1388 prfx = line_prefixes[l->type].prfx1;
1389 else
1390 prfx = line_prefixes[l->type].prfx2;
1392 if (text == NULL)
1393 text = "";
1395 if (face != 0)
1396 wattron(body, face);
1397 wprintw(body, "%s%s", prfx, text);
1398 if (face != 0)
1399 wattroff(body, face);
1402 static void
1403 redraw_tabline(void)
1405 struct tab *tab;
1406 int current;
1408 wclear(tabline);
1409 wbkgd(tabline, A_REVERSE);
1411 wprintw(tabline, " ");
1412 TAILQ_FOREACH(tab, &tabshead, tabs) {
1413 current = tab->flags & TAB_CURRENT;
1415 if (current)
1416 wattron(tabline, A_UNDERLINE);
1418 wprintw(tabline, "%s%d:todo title ",
1419 current ? "*" : " ", tab->id);
1421 if (current)
1422 wattroff(tabline, A_UNDERLINE);
1426 static void
1427 redraw_modeline(struct tab *tab)
1429 double pct;
1430 int x, y, max_x, max_y;
1431 const char *mode = tab->page.name;
1432 const char *spin = "-\\|/";
1434 wclear(modeline);
1435 wattron(modeline, A_REVERSE);
1436 wmove(modeline, 0, 0);
1438 wprintw(modeline, "-%c %s-mode ",
1439 spin[tab->s->loading_anim_step], mode);
1441 pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1443 if (tab->s->line_max <= body_lines)
1444 wprintw(modeline, "All ");
1445 else if (tab->s->line_off == 0)
1446 wprintw(modeline, "Top ");
1447 else if (tab->s->line_off + body_lines >= tab->s->line_max)
1448 wprintw(modeline, "Bottom ");
1449 else
1450 wprintw(modeline, "%.0f%% ", pct);
1452 wprintw(modeline, "%d/%d %s ",
1453 tab->s->line_off + tab->s->curs_y,
1454 tab->s->line_max,
1455 tab->urlstr);
1457 getyx(modeline, y, x);
1458 getmaxyx(modeline, max_y, max_x);
1460 (void)y;
1461 (void)max_y;
1463 for (; x < max_x; ++x)
1464 waddstr(modeline, "-");
1467 static void
1468 redraw_minibuffer(void)
1470 size_t skip = 0, off_x = 0, off_y = 0;
1472 wclear(minibuf);
1473 if (in_minibuffer) {
1474 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1475 if (ministate.hist_cur != NULL)
1476 wprintw(minibuf, "(%zu/%zu) ",
1477 ministate.hist_off + 1,
1478 ministate.history->len);
1480 getyx(minibuf, off_y, off_x);
1482 while (ministate.off - skip > COLS / 2) {
1483 skip += MIN(ministate.off/4, 1);
1486 if (ministate.hist_cur != NULL)
1487 wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1488 else
1489 wprintw(minibuf, "%s", ministate.buf + skip);
1492 if (ministate.curmesg != NULL) {
1493 if (in_minibuffer)
1494 wprintw(minibuf, " [%s]", ministate.curmesg);
1495 else
1496 wprintw(minibuf, "%s", ministate.curmesg);
1499 wmove(minibuf, 0, off_x + ministate.off - skip);
1502 static void
1503 redraw_tab(struct tab *tab)
1505 redraw_tabline();
1506 redraw_body(tab);
1507 redraw_modeline(tab);
1508 redraw_minibuffer();
1510 restore_cursor(tab);
1511 wrefresh(tabline);
1512 wrefresh(modeline);
1514 if (in_minibuffer) {
1515 wrefresh(body);
1516 wrefresh(minibuf);
1517 } else {
1518 wrefresh(minibuf);
1519 wrefresh(body);
1523 static void
1524 redraw_body(struct tab *tab)
1526 struct line *l;
1527 int line;
1529 werase(body);
1531 tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1532 if (TAILQ_EMPTY(&tab->s->head))
1533 return;
1535 line = 0;
1536 l = nth_line(tab, tab->s->line_off);
1537 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1538 wmove(body, line, 0);
1539 print_line(l);
1540 line++;
1541 if (line == body_lines)
1542 break;
1546 static void
1547 message(const char *fmt, ...)
1549 va_list ap;
1551 if (clminibufev_set)
1552 evtimer_del(&clminibufev);
1553 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1554 evtimer_add(&clminibufev, &clminibufev_timer);
1555 clminibufev_set = 1;
1557 free(ministate.curmesg);
1559 va_start(ap, fmt);
1560 /* TODO: what to do if the allocation fails here? */
1561 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1562 ministate.curmesg = NULL;
1563 va_end(ap);
1565 redraw_minibuffer();
1567 if (in_minibuffer) {
1568 wrefresh(body);
1569 wrefresh(minibuf);
1570 } else {
1571 wrefresh(minibuf);
1572 wrefresh(body);
1576 static void
1577 start_loading_anim(struct tab *tab)
1579 if (tab->s->loading_anim)
1580 return;
1581 tab->s->loading_anim = 1;
1582 evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1583 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1586 static void
1587 update_loading_anim(int fd, short ev, void *d)
1589 struct tab *tab = d;
1591 tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1593 redraw_modeline(tab);
1594 wrefresh(modeline);
1596 wrefresh(body);
1597 if (in_minibuffer)
1598 wrefresh(minibuf);
1600 evtimer_add(&tab->s->loadingev, &loadingev_timer);
1603 static void
1604 stop_loading_anim(struct tab *tab)
1606 if (!tab->s->loading_anim)
1607 return;
1608 evtimer_del(&tab->s->loadingev);
1609 tab->s->loading_anim = 0;
1610 tab->s->loading_anim_step = 0;
1612 redraw_modeline(tab);
1614 wrefresh(modeline);
1615 wrefresh(body);
1616 if (in_minibuffer)
1617 wrefresh(minibuf);
1620 static void
1621 load_url_in_tab(struct tab *tab, const char *url)
1623 empty_vlist(tab);
1624 message("Loading %s...", url);
1625 start_loading_anim(tab);
1626 load_url(tab, url);
1628 tab->s->curs_x = 0;
1629 tab->s->curs_y = 0;
1630 redraw_tab(tab);
1633 static void
1634 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1635 void (*abortfn)(void), struct minibuf_histhead *hist)
1637 in_minibuffer = 1;
1638 base_map = &minibuffer_map;
1639 current_map = &minibuffer_map;
1641 base_map->unhandled_input = self_insert_fn;
1643 ministate.donefn = donefn;
1644 ministate.abortfn = abortfn;
1645 memset(ministate.buf, 0, sizeof(ministate.buf));
1646 ministate.off = 0;
1647 ministate.len = 0;
1648 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1650 ministate.history = hist;
1651 ministate.hist_cur = NULL;
1652 ministate.hist_off = 0;
1655 static void
1656 exit_minibuffer(void)
1658 wclear(minibuf);
1660 in_minibuffer = 0;
1661 base_map = &global_map;
1662 current_map = &global_map;
1665 static void
1666 switch_to_tab(struct tab *tab)
1668 struct tab *t;
1670 TAILQ_FOREACH(t, &tabshead, tabs) {
1671 t->flags &= ~TAB_CURRENT;
1674 tab->flags |= TAB_CURRENT;
1677 static struct tab *
1678 new_tab(void)
1680 struct tab *tab, *t;
1681 const char *url = "about:new";
1683 if ((tab = calloc(1, sizeof(*tab))) == NULL)
1684 goto err;
1686 if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1687 goto err;
1689 TAILQ_INIT(&tab->s->head);
1691 tab->id = tab_counter++;
1692 switch_to_tab(tab);
1694 if (TAILQ_EMPTY(&tabshead))
1695 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1696 else
1697 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1699 load_url_in_tab(tab, url);
1700 return tab;
1702 err:
1703 event_loopbreak();
1704 return NULL;
1707 int
1708 ui_init(void)
1710 setlocale(LC_ALL, "");
1712 TAILQ_INIT(&global_map.m);
1713 global_map.unhandled_input = global_key_unbound;
1715 TAILQ_INIT(&minibuffer_map.m);
1717 TAILQ_INIT(&eecmd_history.head);
1718 TAILQ_INIT(&ir_history.head);
1719 TAILQ_INIT(&lu_history.head);
1721 base_map = &global_map;
1722 current_map = &global_map;
1723 load_default_keys();
1725 initscr();
1726 raw();
1727 noecho();
1729 nonl();
1730 intrflush(stdscr, FALSE);
1732 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1733 return 0;
1734 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1735 return 0;
1736 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1737 return 0;
1738 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1739 return 0;
1741 body_lines = LINES-3;
1742 body_cols = COLS;
1744 keypad(body, TRUE);
1745 scrollok(body, TRUE);
1747 /* non-blocking input */
1748 wtimeout(body, 0);
1750 mvwprintw(body, 0, 0, "");
1752 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1753 event_add(&stdioev, NULL);
1755 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1756 signal_add(&winchev, NULL);
1758 new_tab();
1760 return 1;
1763 void
1764 ui_on_tab_loaded(struct tab *tab)
1766 stop_loading_anim(tab);
1767 message("Loaded %s", tab->urlstr);
1770 void
1771 ui_on_tab_refresh(struct tab *tab)
1773 if (!(tab->flags & TAB_CURRENT))
1774 return;
1776 wrap_page(tab);
1777 redraw_tab(tab);
1780 void
1781 ui_require_input(struct tab *tab, int hide)
1783 /* TODO: hard-switching to another tab is ugly */
1784 switch_to_tab(tab);
1786 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1787 &ir_history);
1788 strlcpy(ministate.prompt, "Input required: ",
1789 sizeof(ministate.prompt));
1790 redraw_tab(tab);
1793 void
1794 ui_end(void)
1796 endwin();