2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Ncurses UI for telescope.
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
29 * This means that on every resize we have to clear our list of lines
30 * and re-render everything. A clever approach would be to do this
31 * ``on-demand'', but it's still missing.
35 #include "telescope.h"
49 static struct event stdioev, winchev;
51 static void load_default_keys(void);
53 static void global_key_unbound(void);
54 static void minibuffer_hist_save_entry(void);
55 static void minibuffer_self_insert(void);
56 static void yornp_self_insert(void);
57 static void yornp_abort(void);
58 static void read_self_insert(void);
59 static void read_abort(void);
60 static void read_select(void);
62 static struct vline *nth_line(struct buffer*, size_t);
63 static struct buffer *current_buffer(void);
64 static int readkey(void);
65 static void dispatch_stdio(int, short, void*);
66 static void handle_clear_minibuf(int, short, void*);
67 static void handle_resize(int, short, void*);
68 static void handle_resize_nodelay(int, short, void*);
69 static int wrap_page(struct buffer*, int);
70 static void print_vline(int, int, WINDOW*, struct vline*);
71 static void redraw_tabline(void);
72 static void redraw_window(WINDOW*, int, int, struct buffer*);
73 static void redraw_help(void);
74 static void redraw_body(struct tab*);
75 static void redraw_modeline(struct tab*);
76 static void redraw_minibuffer(void);
77 static void redraw_tab(struct tab*);
78 static void emit_help_item(char*, void*);
79 static void rec_compute_help(struct kmap*, char*, size_t);
80 static void recompute_help(void);
81 static void update_loading_anim(int, short, void*);
82 static void stop_loading_anim(struct tab*);
83 static void session_new_tab_cb(const char*);
84 static void usage(void);
88 struct thiskey thiskey;
90 static struct event resizeev;
91 static struct timeval resize_timer = { 0, 250000 };
93 static WINDOW *tabline, *body, *modeline, *minibuf;
95 int body_lines, body_cols;
98 static struct buffer helpwin;
99 static int help_lines, help_cols;
101 static int side_window;
103 static struct event clminibufev;
104 static struct timeval clminibufev_timer = { 5, 0 };
105 static struct timeval loadingev_timer = { 0, 250000 };
107 static uint32_t tab_counter;
109 static char keybuf[64];
111 static void (*yornp_cb)(int, unsigned int);
112 static unsigned int yornp_data;
114 static void (*read_cb)(const char*, unsigned int);
115 static unsigned int read_data;
117 struct kmap global_map,
122 struct histhead eecmd_history,
129 struct ministate ministate;
132 update_x_offset(void)
134 if (olivetti_mode && fill_column < body_cols)
135 x_offset = (body_cols - fill_column)/2;
141 global_set_key(const char *key, void (*fn)(struct buffer*))
143 if (!kmap_define_key(&global_map, key, fn))
148 minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
150 if (!kmap_define_key(&minibuffer_map, key, fn))
155 load_default_keys(void)
157 /* === global map === */
160 global_set_key("C-p", cmd_previous_line);
161 global_set_key("C-n", cmd_next_line);
162 global_set_key("C-f", cmd_forward_char);
163 global_set_key("C-b", cmd_backward_char);
164 global_set_key("M-{", cmd_backward_paragraph);
165 global_set_key("M-}", cmd_forward_paragraph);
166 global_set_key("C-a", cmd_move_beginning_of_line);
167 global_set_key("C-e", cmd_move_end_of_line);
169 global_set_key("M-v", cmd_scroll_up);
170 global_set_key("C-v", cmd_scroll_down);
171 global_set_key("M-space", cmd_scroll_up);
172 global_set_key("space", cmd_scroll_down);
174 global_set_key("M-<", cmd_beginning_of_buffer);
175 global_set_key("M->", cmd_end_of_buffer);
177 global_set_key("C-x C-c", cmd_kill_telescope);
179 global_set_key("C-g", cmd_clear_minibuf);
181 global_set_key("M-x", cmd_execute_extended_command);
182 global_set_key("C-x C-f", cmd_load_url);
183 global_set_key("C-x M-f", cmd_load_current_url);
185 global_set_key("C-x t 0", cmd_tab_close);
186 global_set_key("C-x t 1", cmd_tab_close_other);
187 global_set_key("C-x t 2", cmd_tab_new);
188 global_set_key("C-x t o", cmd_tab_next);
189 global_set_key("C-x t O", cmd_tab_previous);
190 global_set_key("C-x t m", cmd_tab_move);
191 global_set_key("C-x t M", cmd_tab_move_to);
193 global_set_key("C-M-b", cmd_previous_page);
194 global_set_key("C-M-f", cmd_next_page);
196 global_set_key("<f7> a", cmd_bookmark_page);
197 global_set_key("<f7> <f7>", cmd_list_bookmarks);
200 global_set_key("k", cmd_previous_line);
201 global_set_key("j", cmd_next_line);
202 global_set_key("l", cmd_forward_char);
203 global_set_key("h", cmd_backward_char);
204 global_set_key("{", cmd_backward_paragraph);
205 global_set_key("}", cmd_forward_paragraph);
206 global_set_key("^", cmd_move_beginning_of_line);
207 global_set_key("$", cmd_move_end_of_line);
209 global_set_key("K", cmd_scroll_line_up);
210 global_set_key("J", cmd_scroll_line_down);
212 global_set_key("g g", cmd_beginning_of_buffer);
213 global_set_key("G", cmd_end_of_buffer);
215 global_set_key("g D", cmd_tab_close);
216 global_set_key("g N", cmd_tab_new);
217 global_set_key("g t", cmd_tab_next);
218 global_set_key("g T", cmd_tab_previous);
219 global_set_key("g M-t", cmd_tab_move);
220 global_set_key("g M-T", cmd_tab_move_to);
222 global_set_key("H", cmd_previous_page);
223 global_set_key("L", cmd_next_page);
226 global_set_key("q", cmd_kill_telescope);
228 global_set_key("esc", cmd_clear_minibuf);
230 global_set_key(":", cmd_execute_extended_command);
233 global_set_key("<up>", cmd_previous_line);
234 global_set_key("<down>", cmd_next_line);
235 global_set_key("<right>", cmd_forward_char);
236 global_set_key("<left>", cmd_backward_char);
237 global_set_key("<prior>", cmd_scroll_up);
238 global_set_key("<next>", cmd_scroll_down);
240 global_set_key("M-<left>", cmd_previous_page);
241 global_set_key("M-<right>", cmd_next_page);
243 /* "ncurses standard" */
244 global_set_key("C-l", cmd_redraw);
247 global_set_key("<f1>", cmd_toggle_help);
248 global_set_key("C-m", cmd_push_button);
249 global_set_key("M-enter", cmd_push_button_new_tab);
250 global_set_key("M-tab", cmd_previous_button);
251 global_set_key("backtab", cmd_previous_button);
252 global_set_key("tab", cmd_next_button);
254 /* === minibuffer map === */
255 minibuffer_set_key("ret", cmd_mini_complete_and_exit);
256 minibuffer_set_key("C-g", cmd_mini_abort);
257 minibuffer_set_key("esc", cmd_mini_abort);
258 minibuffer_set_key("C-d", cmd_mini_delete_char);
259 minibuffer_set_key("del", cmd_mini_delete_backward_char);
260 minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
261 minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
263 minibuffer_set_key("C-b", cmd_backward_char);
264 minibuffer_set_key("C-f", cmd_forward_char);
265 minibuffer_set_key("<left>", cmd_backward_char);
266 minibuffer_set_key("<right>", cmd_forward_char);
267 minibuffer_set_key("C-e", cmd_move_end_of_line);
268 minibuffer_set_key("C-a", cmd_move_beginning_of_line);
269 minibuffer_set_key("<end>", cmd_move_end_of_line);
270 minibuffer_set_key("<home>", cmd_move_beginning_of_line);
271 minibuffer_set_key("C-k", cmd_mini_kill_line);
273 minibuffer_set_key("M-p", cmd_mini_previous_history_element);
274 minibuffer_set_key("M-n", cmd_mini_next_history_element);
275 minibuffer_set_key("<up>", cmd_mini_previous_history_element);
276 minibuffer_set_key("<down>", cmd_mini_next_history_element);
280 restore_cursor(struct buffer *buffer)
285 vl = buffer->current_line;
286 if (vl == NULL || vl->line == NULL)
287 buffer->curs_x = buffer->cpoff = 0;
289 buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
291 buffer->curs_x += x_offset;
294 prfx = line_prefixes[vl->parent->type].prfx1;
295 buffer->curs_x += utf8_swidth(prfx);
300 global_key_unbound(void)
302 message("%s is undefined", keybuf);
306 minibuffer_hist_save_entry(void)
310 if (ministate.history == NULL)
313 if ((hist = calloc(1, sizeof(*hist))) == NULL)
316 strlcpy(hist->h, ministate.buf, sizeof(hist->h));
318 if (TAILQ_EMPTY(&ministate.history->head))
319 TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
321 TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
322 ministate.history->len++;
326 * taint the minibuffer cache: if we're currently showing a history
327 * element, copy that to the current buf and reset the "history
331 minibuffer_taint_hist(void)
333 if (ministate.hist_cur == NULL)
336 strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
337 ministate.hist_cur = NULL;
341 minibuffer_self_insert(void)
343 char *c, tmp[5] = {0};
346 minibuffer_taint_hist();
351 len = utf8_encode(thiskey.cp, tmp);
352 c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
353 if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
356 memmove(c + len, c, strlen(c)+1);
358 ministate.buffer.cpoff++;
362 eecmd_self_insert(void)
364 if (thiskey.meta || unicode_isspace(thiskey.cp) ||
365 !unicode_isgraph(thiskey.cp)) {
366 global_key_unbound();
370 minibuffer_self_insert();
378 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
379 if (!strcmp(cmd->cmd, ministate.buf)) {
381 minibuffer_hist_save_entry();
382 cmd->fn(current_buffer());
393 minibuffer_self_insert();
399 char buf[1025] = {0};
406 minibuffer_hist_save_entry();
408 /* a bit ugly but... */
409 memcpy(&uri, &tab->uri, sizeof(tab->uri));
410 phos_uri_set_query(&uri, ministate.buf);
411 phos_serialize_uri(&uri, buf, sizeof(buf));
412 load_url_in_tab(tab, buf);
418 if (thiskey.meta || unicode_isspace(thiskey.key) ||
419 !unicode_isgraph(thiskey.key)) {
420 global_key_unbound();
424 minibuffer_self_insert();
431 minibuffer_hist_save_entry();
432 load_url_in_tab(current_tab(), ministate.buf);
439 if (*ministate.buf != '\0')
440 add_to_bookmarks(ministate.buf);
446 yornp_self_insert(void)
448 if (thiskey.key != 'y' && thiskey.key != 'n') {
449 message("Please answer y or n");
454 yornp_cb(thiskey.key == 'y', yornp_data);
461 yornp_cb(0, yornp_data);
465 read_self_insert(void)
467 if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
468 global_key_unbound();
472 minibuffer_self_insert();
479 read_cb(NULL, read_data);
486 minibuffer_hist_save_entry();
487 read_cb(ministate.buf, read_data);
490 static struct vline *
491 nth_line(struct buffer *buffer, size_t n)
497 TAILQ_FOREACH(vl, &buffer->head, vlines) {
512 TAILQ_FOREACH(t, &tabshead, tabs) {
513 if (t->flags & TAB_CURRENT)
525 return &ministate.buffer;
526 return ¤t_tab()->buffer;
534 if ((thiskey.key = wgetch(body)) == ERR)
537 thiskey.meta = thiskey.key == 27;
539 thiskey.key = wgetch(body);
540 if (thiskey.key == ERR || thiskey.key == 27) {
547 if ((unsigned int)thiskey.key < UINT8_MAX) {
549 if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
551 if ((thiskey.key = wgetch(body)) == ERR) {
552 message("Error decoding user input");
562 dispatch_stdio(int fd, short ev, void *d)
571 if (keybuf[0] != '\0')
572 strlcat(keybuf, " ", sizeof(keybuf));
574 strlcat(keybuf, "M-", sizeof(keybuf));
575 if (thiskey.cp != 0) {
576 utf8_encode(thiskey.cp, tmp);
577 strlcat(keybuf, tmp, sizeof(keybuf));
579 if ((keyname = unkbd(thiskey.key)) != NULL)
580 strlcat(keybuf, keyname, sizeof(keybuf));
582 tmp[0] = thiskey.key;
583 strlcat(keybuf, tmp, sizeof(keybuf));
587 TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
588 if (k->meta == thiskey.meta &&
589 k->key == thiskey.key) {
591 current_map = &k->map;
593 current_map = base_map;
594 strlcpy(keybuf, "", sizeof(keybuf));
595 k->fn(current_buffer());
601 if (current_map->unhandled_input != NULL)
602 current_map->unhandled_input();
604 global_key_unbound();
606 strlcpy(keybuf, "", sizeof(keybuf));
607 current_map = base_map;
613 redraw_tab(current_tab());
617 handle_clear_minibuf(int fd, short ev, void *d)
619 free(ministate.curmesg);
620 ministate.curmesg = NULL;
633 handle_resize(int sig, short ev, void *d)
635 if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
636 event_del(&resizeev);
638 evtimer_set(&resizeev, handle_resize_nodelay, NULL);
639 evtimer_add(&resizeev, &resize_timer);
643 handle_resize_nodelay(int s, short ev, void *d)
651 /* move and resize the windows, in reverse order! */
653 mvwin(minibuf, LINES-1, 0);
654 wresize(minibuf, 1, COLS);
656 mvwin(modeline, LINES-2, 0);
657 wresize(modeline, 1, COLS);
659 body_lines = LINES-3;
663 help_cols = 0.3 * COLS;
664 help_lines = LINES-3;
666 wresize(help, help_lines, help_cols);
668 wrap_page(&helpwin, help_cols);
670 body_cols = COLS - help_cols - 1;
671 mvwin(body, 1, help_cols);
676 wresize(body, body_lines, body_cols);
678 wresize(tabline, 1, COLS);
682 wrap_page(&tab->buffer, body_cols);
687 wrap_page(struct buffer *buffer, int width)
690 const struct line *orig;
695 orig = buffer->current_line == NULL
697 : buffer->current_line->parent;
698 buffer->current_line = NULL;
700 buffer->force_redraw = 1;
702 buffer->line_off = 0;
706 TAILQ_FOREACH(l, &buffer->page.head, lines) {
707 prfx = line_prefixes[l->type].prfx1;
718 wrap_text(buffer, prfx, l, MIN(fill_column, width));
720 case LINE_PRE_CONTENT:
722 pre_width = MIN(fill_column, width);
725 hardwrap_text(buffer, l, pre_width);
729 if (orig == l && buffer->current_line == NULL) {
730 buffer->line_off = buffer->line_max-1;
731 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
734 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
735 if (vl == NULL || vl->parent != orig)
737 buffer->current_line = vl;
743 if (buffer->current_line == NULL)
744 buffer->current_line = TAILQ_FIRST(&buffer->head);
750 print_vline(int off, int width, WINDOW *window, struct vline *vl)
752 const char *text = vl->line;
754 int prefix_face = line_faces[vl->parent->type].prefix_prop;
755 int text_face = line_faces[vl->parent->type].text_prop;
758 /* unused, set by wgetyx */
762 prfx = line_prefixes[vl->parent->type].prfx1;
764 prfx = line_prefixes[vl->parent->type].prfx2;
769 /* TODO: load the pair for the default background */
770 for (i = 0; i < off; i++)
773 wattron(window, prefix_face);
774 wprintw(window, "%s", prfx);
775 wattroff(window, prefix_face);
777 wattron(window, text_face);
778 wprintw(window, "%s", text);
784 /* draw the rest of the line with line face */
785 for (i = 0; i < left - off; ++i)
788 wattroff(window, text_face);
795 size_t toskip, ots, tabwidth, space, x;
796 int current, y, truncated;
802 /* unused, but setted by a getyx */
805 tabwidth = sizeof(buf)+1;
809 TAILQ_FOREACH(tab, &tabshead, tabs) {
811 if (tab->flags & TAB_CURRENT)
815 if (toskip * tabwidth < space)
820 while (toskip != 0 &&
821 (ots - toskip+1) * tabwidth < space)
826 wattron(tabline, tab_face.background);
827 wprintw(tabline, toskip == 0 ? " " : "<");
828 wattroff(tabline, tab_face.background);
831 TAILQ_FOREACH(tab, &tabshead, tabs) {
839 getyx(tabline, y, x);
840 if (x + sizeof(buf)+2 >= (size_t)COLS)
843 current = tab->flags & TAB_CURRENT;
845 if (*(title = tab->buffer.page.title) == '\0')
846 title = tab->hist_cur->h;
848 if (tab->flags & TAB_URGENT)
849 strlcpy(buf, "!", sizeof(buf));
851 strlcpy(buf, " ", sizeof(buf));
853 if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
854 /* truncation happens */
855 strlcpy(&buf[sizeof(buf)-4], "...", 4);
857 /* pad with spaces */
858 while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
863 wattron(tabline, tab_face.current_tab);
865 wattron(tabline, tab_face.tab);
867 wprintw(tabline, "%s", buf);
868 if (TAILQ_NEXT(tab, tabs) != NULL)
869 wprintw(tabline, " ");
872 wattroff(tabline, tab_face.current_tab);
874 wattroff(tabline, tab_face.tab);
877 wattron(tabline, tab_face.background);
878 for (; x < (size_t)COLS; ++x)
879 waddch(tabline, ' ');
881 mvwprintw(tabline, 0, COLS-1, ">");
885 redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
891 * Don't bother redraw the body if nothing changed. Cursor
892 * movements count as "nothing changed" if it hasn't produced
893 * a scroll. Ensure that wmove is called though!
895 if (!buffer->force_redraw &&
896 buffer->last_line_off == buffer->line_off)
901 buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
902 buffer->force_redraw = 0;
903 buffer->last_line_off = buffer->line_off;
905 if (TAILQ_EMPTY(&buffer->head))
909 vl = nth_line(buffer, buffer->line_off);
910 for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
912 print_vline(x_offset, width, win, vl);
919 wmove(win, buffer->curs_y, buffer->curs_x);
925 redraw_window(help, help_lines, help_cols, &helpwin);
929 redraw_body(struct tab *tab)
931 static struct tab *last_tab;
934 tab->buffer.force_redraw =1;
937 redraw_window(body, body_lines, body_cols, &tab->buffer);
941 trust_status_char(enum trust_state ts)
944 case TS_UNKNOWN: return 'u';
945 case TS_UNTRUSTED: return '!';
946 case TS_TRUSTED: return 'v';
947 case TS_VERIFIED: return 'V';
953 redraw_modeline(struct tab *tab)
956 int x, y, max_x, max_y;
957 const char *mode = tab->buffer.page.name;
958 const char *spin = "-\\|/";
961 wattron(modeline, modeline_face.background);
962 wmove(modeline, 0, 0);
964 wprintw(modeline, "-%c%c %s ",
965 spin[tab->loading_anim_step],
966 trust_status_char(tab->trust),
967 mode == NULL ? "(none)" : mode);
969 pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
971 if (tab->buffer.line_max <= (size_t)body_lines)
972 wprintw(modeline, "All ");
973 else if (tab->buffer.line_off == 0)
974 wprintw(modeline, "Top ");
975 else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
976 wprintw(modeline, "Bottom ");
978 wprintw(modeline, "%.0f%% ", pct);
980 wprintw(modeline, "%d/%d %s ",
981 tab->buffer.line_off + tab->buffer.curs_y,
982 tab->buffer.line_max,
985 getyx(modeline, y, x);
986 getmaxyx(modeline, max_y, max_x);
991 for (; x < max_x; ++x)
992 waddstr(modeline, "-");
994 wattroff(modeline, modeline_face.background);
998 redraw_minibuffer(void)
1001 size_t off_y, off_x = 0;
1002 char *start = NULL, *c = NULL;
1004 /* unused, but set by getyx */
1007 wattron(minibuf, minibuffer_face.background);
1010 if (in_minibuffer) {
1011 mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1012 if (ministate.hist_cur != NULL)
1013 wprintw(minibuf, "(%zu/%zu) ",
1014 ministate.hist_off + 1,
1015 ministate.history->len);
1017 getyx(minibuf, off_y, off_x);
1019 start = ministate.hist_cur != NULL
1020 ? ministate.hist_cur->h
1022 c = utf8_nth(ministate.buffer.current_line->line,
1023 ministate.buffer.cpoff);
1024 while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1025 start = utf8_next_cp(start);
1028 waddstr(minibuf, start);
1031 if (ministate.curmesg != NULL)
1032 wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1035 if (!in_minibuffer && ministate.curmesg == NULL)
1036 waddstr(minibuf, keybuf);
1038 /* If nothing else, show the URL at point */
1039 if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1040 tab = current_tab();
1041 if (tab->buffer.current_line != NULL &&
1042 tab->buffer.current_line->parent->type == LINE_LINK)
1043 waddstr(minibuf, tab->buffer.current_line->parent->alt);
1047 wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1049 wattroff(minibuf, minibuffer_face.background);
1053 redraw_tab(struct tab *tab)
1060 restore_cursor(&tab->buffer);
1064 redraw_modeline(tab);
1065 redraw_minibuffer();
1067 wnoutrefresh(tabline);
1068 wnoutrefresh(modeline);
1070 if (in_minibuffer) {
1072 wnoutrefresh(minibuf);
1074 wnoutrefresh(minibuf);
1082 emit_help_item(char *prfx, void *fn)
1087 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1091 assert(cmd != NULL);
1093 if ((l = calloc(1, sizeof(*l))) == NULL)
1096 l->type = LINE_TEXT;
1099 asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1101 if (TAILQ_EMPTY(&helpwin.page.head))
1102 TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1104 TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1108 rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1114 TAILQ_FOREACH(k, &keymap->m, keymaps) {
1115 strlcpy(p, prfx, sizeof(p));
1117 strlcat(p, " ", sizeof(p));
1119 strlcat(p, "M-", sizeof(p));
1120 if ((kn = unkbd(k->key)) != NULL)
1121 strlcat(p, kn, sizeof(p));
1123 strlcat(p, keyname(k->key), sizeof(p));
1126 rec_compute_help(&k->map, p, sizeof(p));
1128 emit_help_item(p, k->fn);
1133 recompute_help(void)
1137 empty_vlist(&helpwin);
1138 empty_linelist(&helpwin);
1139 rec_compute_help(current_map, p, sizeof(p));
1140 wrap_page(&helpwin, help_cols);
1144 vmessage(const char *fmt, va_list ap)
1146 if (evtimer_pending(&clminibufev, NULL))
1147 evtimer_del(&clminibufev);
1149 free(ministate.curmesg);
1150 ministate.curmesg = NULL;
1153 evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1154 evtimer_add(&clminibufev, &clminibufev_timer);
1156 /* TODO: what to do if the allocation fails here? */
1157 if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1158 ministate.curmesg = NULL;
1161 redraw_minibuffer();
1162 if (in_minibuffer) {
1172 message(const char *fmt, ...)
1182 start_loading_anim(struct tab *tab)
1184 if (tab->loading_anim)
1186 tab->loading_anim = 1;
1187 evtimer_set(&tab->loadingev, update_loading_anim, tab);
1188 evtimer_add(&tab->loadingev, &loadingev_timer);
1192 update_loading_anim(int fd, short ev, void *d)
1194 struct tab *tab = d;
1196 tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1198 if (tab->flags & TAB_CURRENT) {
1199 redraw_modeline(tab);
1206 evtimer_add(&tab->loadingev, &loadingev_timer);
1210 stop_loading_anim(struct tab *tab)
1212 if (!tab->loading_anim)
1214 evtimer_del(&tab->loadingev);
1215 tab->loading_anim = 0;
1216 tab->loading_anim_step = 0;
1218 if (!(tab->flags & TAB_CURRENT))
1221 redraw_modeline(tab);
1230 load_url_in_tab(struct tab *tab, const char *url)
1232 message("Loading %s...", url);
1233 start_loading_anim(tab);
1236 tab->buffer.curs_x = 0;
1237 tab->buffer.curs_y = 0;
1242 enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1243 void (*abortfn)(void), struct histhead *hist)
1246 base_map = &minibuffer_map;
1247 current_map = &minibuffer_map;
1249 base_map->unhandled_input = self_insert_fn;
1251 ministate.donefn = donefn;
1252 ministate.abortfn = abortfn;
1253 memset(ministate.buf, 0, sizeof(ministate.buf));
1254 ministate.buffer.current_line = &ministate.vline;
1255 ministate.buffer.current_line->line = ministate.buf;
1256 ministate.buffer.cpoff = 0;
1257 strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1259 ministate.history = hist;
1260 ministate.hist_cur = NULL;
1261 ministate.hist_off = 0;
1265 exit_minibuffer(void)
1270 base_map = &global_map;
1271 current_map = &global_map;
1275 switch_to_tab(struct tab *tab)
1279 TAILQ_FOREACH(t, &tabshead, tabs) {
1280 t->flags &= ~TAB_CURRENT;
1283 tab->flags |= TAB_CURRENT;
1284 tab->flags &= ~TAB_URGENT;
1290 return tab_counter++;
1294 new_tab(const char *url)
1298 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1304 TAILQ_INIT(&tab->hist.head);
1306 TAILQ_INIT(&tab->buffer.head);
1308 tab->id = tab_new_id();
1311 if (TAILQ_EMPTY(&tabshead))
1312 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1314 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1316 load_url_in_tab(tab, url);
1321 session_new_tab_cb(const char *url)
1329 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1330 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1334 ui_init(int argc, char * const *argv)
1336 char path[PATH_MAX];
1337 const char *url = NEW_TAB_URL;
1338 int ch, configtest = 0, fonf = 0;
1340 if (getenv("NO_COLOR") != NULL)
1343 strlcpy(path, getenv("HOME"), sizeof(path));
1344 strlcat(path, "/.telescope/config", sizeof(path));
1346 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1350 strlcpy(path, optarg, sizeof(path));
1366 parseconfig(path, fonf);
1375 setlocale(LC_ALL, "");
1377 TAILQ_INIT(&global_map.m);
1378 global_map.unhandled_input = global_key_unbound;
1380 TAILQ_INIT(&minibuffer_map.m);
1382 TAILQ_INIT(&eecmd_history.head);
1383 TAILQ_INIT(&ir_history.head);
1384 TAILQ_INIT(&lu_history.head);
1386 ministate.line.type = LINE_TEXT;
1387 ministate.vline.parent = &ministate.line;
1388 ministate.buffer.current_line = &ministate.vline;
1390 /* initialize help window */
1391 TAILQ_INIT(&helpwin.head);
1393 base_map = &global_map;
1394 current_map = &global_map;
1395 load_default_keys();
1399 if (enable_colors) {
1402 config_apply_colors();
1410 intrflush(stdscr, FALSE);
1412 if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1414 if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1416 if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1418 if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1420 if ((help = newwin(1, 1, 1, 0)) == NULL)
1423 body_lines = LINES-3;
1429 scrollok(body, FALSE);
1431 /* non-blocking input */
1434 mvwprintw(body, 0, 0, "");
1436 event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1437 event_add(&stdioev, NULL);
1439 signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1440 signal_add(&winchev, NULL);
1442 load_last_session(session_new_tab_cb);
1443 if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1450 ui_on_tab_loaded(struct tab *tab)
1452 stop_loading_anim(tab);
1453 message("Loaded %s", tab->hist_cur->h);
1464 ui_on_tab_refresh(struct tab *tab)
1466 wrap_page(&tab->buffer, body_cols);
1467 if (tab->flags & TAB_CURRENT) {
1468 restore_cursor(&tab->buffer);
1471 tab->flags |= TAB_URGENT;
1481 ui_toggle_side_window(void)
1483 side_window = !side_window;
1488 * ugly hack, but otherwise the window doesn't get updated
1489 * until I call handle_resize a second time (i.e. C-l). I
1490 * will be happy to know why something like this is needed.
1492 handle_resize_nodelay(0, 0, NULL);
1493 handle_resize_nodelay(0, 0, NULL);
1497 ui_schedule_redraw(void)
1499 handle_resize_nodelay(0, 0, NULL);
1503 ui_require_input(struct tab *tab, int hide)
1505 /* TODO: hard-switching to another tab is ugly */
1508 enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1510 strlcpy(ministate.prompt, "Input required: ",
1511 sizeof(ministate.prompt));
1516 ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1521 if (in_minibuffer) {
1528 enter_minibuffer(yornp_self_insert, yornp_self_insert,
1531 len = sizeof(ministate.prompt);
1532 strlcpy(ministate.prompt, prompt, len);
1533 strlcat(ministate.prompt, " (y or n) ", len);
1534 redraw_tab(current_tab());
1538 ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1548 enter_minibuffer(read_self_insert, read_select, read_abort,
1551 len = sizeof(ministate.prompt);
1552 strlcpy(ministate.prompt, prompt, len);
1553 strlcat(ministate.prompt, ": ", len);
1554 redraw_tab(current_tab());