2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
20 1d08c280 2021-03-06 op * Text scrolling
21 1d08c280 2021-03-06 op * ==============
23 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
24 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
25 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
26 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
27 1d08c280 2021-03-06 op * first/last line!
29 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
30 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
31 1d08c280 2021-03-06 op * ``on-demand''.
33 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
37 5e11c00c 2021-03-02 op #include <telescope.h>
39 f832146f 2021-03-09 op #include <ctype.h>
40 5e11c00c 2021-03-02 op #include <curses.h>
41 5e11c00c 2021-03-02 op #include <event.h>
42 5e11c00c 2021-03-02 op #include <locale.h>
43 5e11c00c 2021-03-02 op #include <signal.h>
44 7953dd72 2021-03-07 op #include <stdarg.h>
45 eb259e66 2021-03-02 op #include <stdlib.h>
46 eb259e66 2021-03-02 op #include <string.h>
47 f832146f 2021-03-09 op #include <unistd.h>
49 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
51 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
53 f832146f 2021-03-09 op static void load_default_keys(void);
54 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
55 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
57 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
58 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
59 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
60 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
61 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
62 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
63 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
64 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
65 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
66 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
67 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
68 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
69 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
70 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
71 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
72 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
73 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
74 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
75 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
76 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
77 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
78 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
79 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
80 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
81 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
82 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
83 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
85 9ca15951 2021-03-09 op static void global_key_unbound(void);
87 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
88 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
89 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
90 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
91 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
92 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
93 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
94 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
95 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
96 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
97 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
99 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
100 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
101 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
102 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
103 b360ebb3 2021-03-10 op static void eecmd_select(void);
104 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
105 5cd2ebb1 2021-03-11 op static void ir_select(void);
106 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
107 5cd2ebb1 2021-03-11 op static void lu_select(void);
108 740f578b 2021-03-15 op static void bp_select(void);
110 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
111 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
112 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
113 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
114 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
115 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
116 9a25f829 2021-03-14 op static void print_vline(struct vline*);
117 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
118 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
119 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
120 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
121 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
122 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
123 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
124 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
125 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
126 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
127 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
128 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
129 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
130 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
131 b1df9b71 2021-03-12 op static struct tab *new_tab(void);
133 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
135 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
136 48e9d457 2021-03-06 op static int body_lines, body_cols;
138 48e9d457 2021-03-06 op static struct event clminibufev;
139 48e9d457 2021-03-06 op static int clminibufev_set;
140 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
141 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
143 bcb0b073 2021-03-07 op static uint32_t tab_counter;
145 7c7d7bb7 2021-03-10 op static char keybuf[64];
147 9ca15951 2021-03-09 op struct kmap global_map,
148 fa3fd864 2021-03-10 op minibuffer_map,
149 9ca15951 2021-03-09 op *current_map,
152 3148eeac 2021-03-13 op static struct histhead eecmd_history,
156 9ca15951 2021-03-09 op static int in_minibuffer;
158 9ca15951 2021-03-09 op static struct {
159 9cb0f9ce 2021-03-10 op char *curmesg;
161 040fbdf8 2021-03-10 op char buf[1025];
162 9ca15951 2021-03-09 op size_t off, len;
163 5cd2ebb1 2021-03-11 op char prompt[32];
164 b360ebb3 2021-03-10 op void (*donefn)(void);
165 b360ebb3 2021-03-10 op void (*abortfn)(void);
167 3148eeac 2021-03-13 op struct histhead *history;
168 3148eeac 2021-03-13 op struct hist *hist_cur;
169 22268e11 2021-03-11 op size_t hist_off;
172 f95d82dd 2021-03-11 op struct lineprefix {
173 f95d82dd 2021-03-11 op const char *prfx1;
174 f95d82dd 2021-03-11 op const char *prfx2;
175 f95d82dd 2021-03-11 op } line_prefixes[] = {
176 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
177 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
178 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
179 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
180 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
181 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
182 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
183 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
184 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
185 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
188 d3d56d8a 2021-03-11 op struct line_face {
190 d3d56d8a 2021-03-11 op } line_faces[] = {
191 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
192 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
193 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
194 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
195 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
196 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
197 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
198 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
199 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
200 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
204 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
206 65d9b3ca 2021-03-14 op struct vline *vl, *t;
208 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
210 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
211 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
212 65d9b3ca 2021-03-14 op free(vl->line);
217 f832146f 2021-03-09 op static inline void
218 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
220 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
224 9ca15951 2021-03-09 op static inline void
225 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
227 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
232 f832146f 2021-03-09 op load_default_keys(void)
234 9ca15951 2021-03-09 op /* === global map === */
237 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
238 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
239 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
240 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
241 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
242 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
244 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
245 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
246 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
247 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
249 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
251 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
253 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
254 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
255 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
257 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
258 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
259 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
260 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
262 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
263 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
265 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
266 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
268 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
270 f832146f 2021-03-09 op /* vi/vi-like */
271 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
272 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
273 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
274 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
275 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
276 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
278 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
279 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
281 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
282 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
284 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
285 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
288 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
290 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
292 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
295 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
296 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
297 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
298 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
299 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
300 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
302 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
303 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
305 f832146f 2021-03-09 op /* "ncurses standard" */
306 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
309 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
310 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
312 fa3fd864 2021-03-10 op /* === minibuffer map === */
313 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
314 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
315 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
316 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
317 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
319 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
320 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
321 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
322 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
323 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
324 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
325 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
326 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
327 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
329 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
330 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
331 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
332 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
336 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
338 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
342 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
344 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
345 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
346 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
349 48e9d457 2021-03-06 op restore_cursor(tab);
353 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
355 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
358 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
359 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
360 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
363 48e9d457 2021-03-06 op restore_cursor(tab);
367 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
369 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
370 48e9d457 2021-03-06 op restore_cursor(tab);
374 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
376 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
377 a329982b 2021-03-11 op restore_cursor(tab);
381 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
383 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
384 a329982b 2021-03-11 op restore_cursor(tab);
388 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
390 9a25f829 2021-03-14 op struct vline *vl;
392 f95d82dd 2021-03-11 op const char *prfx;
394 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
395 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
396 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
400 9a25f829 2021-03-14 op vl = nth_line(tab, off);
401 9a25f829 2021-03-14 op if (vl->line != NULL)
402 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
404 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
406 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
407 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
410 48e9d457 2021-03-06 op restore_cursor(tab);
414 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
416 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
420 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
422 9a25f829 2021-03-14 op struct vline *vl;
424 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
427 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
428 48e9d457 2021-03-06 op wscrl(body, -1);
429 48e9d457 2021-03-06 op wmove(body, 0, 0);
430 9a25f829 2021-03-14 op print_vline(vl);
434 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
436 9a25f829 2021-03-14 op struct vline *vl;
439 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
442 65d9b3ca 2021-03-14 op tab->s.line_off++;
443 48e9d457 2021-03-06 op wscrl(body, 1);
445 65d9b3ca 2021-03-14 op if (tab->s.line_max - tab->s.line_off < body_lines)
448 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
449 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
450 9a25f829 2021-03-14 op print_vline(vl);
454 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
458 ed44414d 2021-03-09 op off = body_lines+1;
460 ed44414d 2021-03-09 op for (; off > 0; --off)
461 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
465 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
469 338eecdc 2021-03-12 op off = body_lines+1;
471 338eecdc 2021-03-12 op for (; off > 0; --off)
472 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
476 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
478 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
479 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
480 e19f9a04 2021-03-11 op redraw_body(tab);
484 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
488 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
489 e19f9a04 2021-03-11 op off = MAX(0, off);
491 65d9b3ca 2021-03-14 op tab->s.line_off = off;
492 65d9b3ca 2021-03-14 op tab->s.curs_y = MIN(body_lines, tab->s.line_max);
494 e19f9a04 2021-03-11 op redraw_body(tab);
498 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
500 1d08c280 2021-03-06 op event_loopbreak();
504 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
506 9a25f829 2021-03-14 op struct vline *vl;
509 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
510 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
512 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
513 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
516 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
520 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
522 b1df9b71 2021-03-12 op struct tab *t;
523 9a25f829 2021-03-14 op struct vline *vl;
526 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
527 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
529 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
530 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
533 b1df9b71 2021-03-12 op t = new_tab();
534 b1df9b71 2021-03-12 op memcpy(&t->url, &tab->url, sizeof(tab->url));
535 9a25f829 2021-03-14 op load_url_in_tab(t, vl->parent->alt);
539 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
541 2051e653 2021-03-13 op if (!load_previous_page(tab))
542 2051e653 2021-03-13 op message("No previous page");
544 c40c2250 2021-03-14 op start_loading_anim(tab);
548 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
550 2051e653 2021-03-13 op if (!load_next_page(tab))
551 2051e653 2021-03-13 op message("No next page");
553 c40c2250 2021-03-14 op start_loading_anim(tab);
557 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
559 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
563 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
567 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
568 22268e11 2021-03-11 op &eecmd_history);
570 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
571 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
573 9ca15951 2021-03-09 op if (thiskey.meta)
574 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
576 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
577 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
581 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
583 7c7d7bb7 2021-03-10 op struct tab *t;
585 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
586 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
587 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
591 7c7d7bb7 2021-03-10 op stop_tab(tab);
593 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
594 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
596 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
602 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
608 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
610 7c7d7bb7 2021-03-10 op struct tab *t;
612 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
614 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
615 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
616 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
620 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
622 7c7d7bb7 2021-03-10 op struct tab *t;
624 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
626 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
627 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
628 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
632 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
634 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
635 22268e11 2021-03-11 op &lu_history);
636 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
640 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
642 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
643 22268e11 2021-03-11 op &lu_history);
644 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
645 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
646 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
647 740f578b 2021-03-15 op ministate.len = ministate.off;
651 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
653 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
654 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
655 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
656 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
657 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
661 7c7d7bb7 2021-03-10 op global_key_unbound(void)
663 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
667 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
669 3eecee9e 2021-03-12 op minibuffer_taint_hist();
671 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
674 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
675 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
676 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
677 d67133bf 2021-03-12 op ministate.len--;
681 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
683 3eecee9e 2021-03-12 op minibuffer_taint_hist();
685 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
688 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
689 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
690 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
691 9ca15951 2021-03-09 op ministate.off--;
692 9ca15951 2021-03-09 op ministate.len--;
696 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
698 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
700 9ca15951 2021-03-09 op ministate.off++;
704 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
706 9ca15951 2021-03-09 op if (ministate.off == 0)
708 9ca15951 2021-03-09 op ministate.off--;
712 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
714 9ca15951 2021-03-09 op ministate.off = ministate.len;
718 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
720 9ca15951 2021-03-09 op ministate.off = 0;
724 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
726 3eecee9e 2021-03-12 op minibuffer_taint_hist();
728 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
730 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
731 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
735 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
737 b360ebb3 2021-03-10 op ministate.abortfn();
741 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
743 22268e11 2021-03-11 op minibuffer_taint_hist();
744 b360ebb3 2021-03-10 op ministate.donefn();
748 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
750 22268e11 2021-03-11 op if (ministate.history == NULL) {
751 22268e11 2021-03-11 op message("No history");
755 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
756 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
757 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
758 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
759 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
760 22268e11 2021-03-11 op message("No prev item");
762 22268e11 2021-03-11 op ministate.hist_off--;
765 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
766 22268e11 2021-03-11 op ministate.off = 0;
767 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
772 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
774 22268e11 2021-03-11 op if (ministate.history == NULL) {
775 22268e11 2021-03-11 op message("No history");
779 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
780 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
781 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
782 22268e11 2021-03-11 op ministate.hist_off = 0;
783 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
784 22268e11 2021-03-11 op message("No next item");
786 22268e11 2021-03-11 op ministate.hist_off++;
789 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
790 22268e11 2021-03-11 op ministate.off = 0;
791 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
796 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
798 3148eeac 2021-03-13 op struct hist *hist;
800 22268e11 2021-03-11 op if (ministate.history == NULL)
803 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
806 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
808 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
809 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
811 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
812 22268e11 2021-03-11 op ministate.history->len++;
816 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
817 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
818 22268e11 2021-03-11 op * navigation" thing.
821 22268e11 2021-03-11 op minibuffer_taint_hist(void)
823 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
826 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
827 22268e11 2021-03-11 op ministate.hist_cur = NULL;
831 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
833 22268e11 2021-03-11 op minibuffer_taint_hist();
835 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
838 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
840 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
841 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
842 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
843 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
844 9ca15951 2021-03-09 op ministate.off++;
845 9ca15951 2021-03-09 op ministate.len++;
849 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
851 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
852 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
853 5cd2ebb1 2021-03-11 op global_key_unbound();
857 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
861 b360ebb3 2021-03-10 op eecmd_select(void)
863 b360ebb3 2021-03-10 op exit_minibuffer();
864 22268e11 2021-03-11 op minibuffer_hist_save_entry();
865 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
869 5cd2ebb1 2021-03-11 op ir_self_insert(void)
871 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
875 5cd2ebb1 2021-03-11 op ir_select(void)
877 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
878 5cd2ebb1 2021-03-11 op struct url url;
879 5cd2ebb1 2021-03-11 op struct tab *tab;
881 5cd2ebb1 2021-03-11 op tab = current_tab();
883 5cd2ebb1 2021-03-11 op exit_minibuffer();
884 22268e11 2021-03-11 op minibuffer_hist_save_entry();
886 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
887 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
888 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
889 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
890 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
894 5cd2ebb1 2021-03-11 op lu_self_insert(void)
896 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
897 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
898 5cd2ebb1 2021-03-11 op global_key_unbound();
902 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
906 5cd2ebb1 2021-03-11 op lu_select(void)
908 5cd2ebb1 2021-03-11 op exit_minibuffer();
909 22268e11 2021-03-11 op minibuffer_hist_save_entry();
910 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
914 740f578b 2021-03-15 op bp_select(void)
916 740f578b 2021-03-15 op exit_minibuffer();
917 740f578b 2021-03-15 op if (*ministate.buf != '\0')
918 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
920 740f578b 2021-03-15 op message("Abort.");
923 9a25f829 2021-03-14 op static struct vline *
924 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
926 9a25f829 2021-03-14 op struct vline *vl;
930 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
936 48e9d457 2021-03-06 op /* unreachable */
940 5e11c00c 2021-03-02 op static struct tab *
941 5e11c00c 2021-03-02 op current_tab(void)
943 5e11c00c 2021-03-02 op struct tab *t;
945 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
946 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
950 5e11c00c 2021-03-02 op /* unreachable */
955 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
957 481340cc 2021-03-11 op struct tab *tab;
958 f832146f 2021-03-09 op struct keymap *k;
959 7c7d7bb7 2021-03-10 op const char *keyname;
960 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
962 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
963 9ca15951 2021-03-09 op if (thiskey.key == ERR)
965 9ca15951 2021-03-09 op if (thiskey.key == 27) {
966 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
968 9ca15951 2021-03-09 op thiskey.meta = 1;
969 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
970 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
971 c314a314 2021-03-11 op thiskey.meta = 0;
972 9ca15951 2021-03-09 op thiskey.key = 27;
975 9ca15951 2021-03-09 op thiskey.meta = 0;
977 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
978 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
979 7c7d7bb7 2021-03-10 op if (thiskey.meta)
980 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
981 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
982 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
984 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
985 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
988 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
989 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
990 9ca15951 2021-03-09 op k->key == thiskey.key) {
991 f832146f 2021-03-09 op if (k->fn == NULL)
992 f832146f 2021-03-09 op current_map = &k->map;
994 9ca15951 2021-03-09 op current_map = base_map;
995 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
996 f832146f 2021-03-09 op k->fn(current_tab());
1002 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1003 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1005 7c7d7bb7 2021-03-10 op global_key_unbound();
1008 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1009 9ca15951 2021-03-09 op current_map = base_map;
1012 179f0f58 2021-03-11 op redraw_tab(current_tab());
1016 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1018 a6d450c1 2021-03-06 op clminibufev_set = 0;
1020 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1021 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1023 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1024 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1025 9cb0f9ce 2021-03-10 op wrefresh(body);
1026 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1028 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1029 9cb0f9ce 2021-03-10 op wrefresh(body);
1034 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1036 1d08c280 2021-03-06 op struct tab *tab;
1042 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1044 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1045 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1047 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1048 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1050 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1051 48e9d457 2021-03-06 op body_lines = LINES-3;
1052 bd9637e9 2021-03-06 op body_cols = COLS;
1054 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1056 1d08c280 2021-03-06 op tab = current_tab();
1058 1d08c280 2021-03-06 op wrap_page(tab);
1059 1d08c280 2021-03-06 op redraw_tab(tab);
1063 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1065 1d08c280 2021-03-06 op struct line *l;
1066 0d568960 2021-03-11 op const char *prfx;
1068 1d08c280 2021-03-06 op empty_vlist(tab);
1070 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1071 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1072 5e11c00c 2021-03-02 op switch (l->type) {
1073 5e11c00c 2021-03-02 op case LINE_TEXT:
1074 5e11c00c 2021-03-02 op case LINE_LINK:
1075 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1076 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1077 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1078 5e11c00c 2021-03-02 op case LINE_ITEM:
1079 5e11c00c 2021-03-02 op case LINE_QUOTE:
1080 5e11c00c 2021-03-02 op case LINE_PRE_START:
1081 5e11c00c 2021-03-02 op case LINE_PRE_END:
1082 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1083 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1085 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1086 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1093 1d08c280 2021-03-06 op static inline void
1094 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1096 9a25f829 2021-03-14 op const char *text = vl->line;
1097 768db5bb 2021-03-12 op const char *prfx;
1098 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1100 9a25f829 2021-03-14 op if (!vl->flags)
1101 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1103 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1105 bd9637e9 2021-03-06 op if (text == NULL)
1108 d3d56d8a 2021-03-11 op if (face != 0)
1109 d3d56d8a 2021-03-11 op wattron(body, face);
1110 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1111 d3d56d8a 2021-03-11 op if (face != 0)
1112 d3d56d8a 2021-03-11 op wattroff(body, face);
1116 8af5e5ed 2021-03-08 op redraw_tabline(void)
1118 7c7d7bb7 2021-03-10 op struct tab *tab;
1119 7c7d7bb7 2021-03-10 op int current;
1120 dc5df781 2021-03-13 op const char *title;
1122 156f1501 2021-03-13 op werase(tabline);
1123 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1125 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1126 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1127 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1129 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1130 2051e653 2021-03-13 op title = tab->hist_cur->h;
1132 a329982b 2021-03-11 op if (current)
1133 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1135 dc5df781 2021-03-13 op wprintw(tabline, "%s%d: %s",
1136 dc5df781 2021-03-13 op current ? "*" : " ", tab->id, title);
1138 a329982b 2021-03-11 op if (current)
1139 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1144 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1147 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1148 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1149 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1151 156f1501 2021-03-13 op werase(modeline);
1152 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1153 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1155 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1156 65d9b3ca 2021-03-14 op spin[tab->s.loading_anim_step], mode);
1158 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1160 65d9b3ca 2021-03-14 op if (tab->s.line_max <= body_lines)
1161 481340cc 2021-03-11 op wprintw(modeline, "All ");
1162 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1163 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1164 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1165 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1167 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1169 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1170 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1171 65d9b3ca 2021-03-14 op tab->s.line_max,
1172 2051e653 2021-03-13 op tab->hist_cur->h);
1174 48e9d457 2021-03-06 op getyx(modeline, y, x);
1175 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1178 48e9d457 2021-03-06 op (void)max_y;
1180 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1181 48e9d457 2021-03-06 op waddstr(modeline, "-");
1185 9ca15951 2021-03-09 op redraw_minibuffer(void)
1187 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1189 156f1501 2021-03-13 op werase(minibuf);
1190 22268e11 2021-03-11 op if (in_minibuffer) {
1191 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1192 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1193 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1194 22268e11 2021-03-11 op ministate.hist_off + 1,
1195 22268e11 2021-03-11 op ministate.history->len);
1197 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1199 22268e11 2021-03-11 op while (ministate.off - skip > COLS / 2) {
1200 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1203 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1204 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1206 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1209 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1210 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1211 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1213 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1216 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1217 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1219 2aaf475e 2021-03-13 op if (in_minibuffer)
1220 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1224 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1226 e19f9a04 2021-03-11 op redraw_tabline();
1227 e19f9a04 2021-03-11 op redraw_body(tab);
1228 e19f9a04 2021-03-11 op redraw_modeline(tab);
1229 e19f9a04 2021-03-11 op redraw_minibuffer();
1231 e19f9a04 2021-03-11 op restore_cursor(tab);
1232 e19f9a04 2021-03-11 op wrefresh(tabline);
1233 e19f9a04 2021-03-11 op wrefresh(modeline);
1235 e19f9a04 2021-03-11 op if (in_minibuffer) {
1236 e19f9a04 2021-03-11 op wrefresh(body);
1237 e19f9a04 2021-03-11 op wrefresh(minibuf);
1239 e19f9a04 2021-03-11 op wrefresh(minibuf);
1240 e19f9a04 2021-03-11 op wrefresh(body);
1245 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1247 9a25f829 2021-03-14 op struct vline *vl;
1250 48e9d457 2021-03-06 op werase(body);
1252 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1253 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1257 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1258 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1259 48e9d457 2021-03-06 op wmove(body, line, 0);
1260 9a25f829 2021-03-14 op print_vline(vl);
1262 48e9d457 2021-03-06 op if (line == body_lines)
1268 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1270 7953dd72 2021-03-07 op if (clminibufev_set)
1271 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1272 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1273 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1274 7953dd72 2021-03-07 op clminibufev_set = 1;
1276 bf992581 2021-03-12 op free(ministate.curmesg);
1278 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1279 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1280 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1282 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1283 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1284 9cb0f9ce 2021-03-10 op wrefresh(body);
1285 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1287 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1288 9cb0f9ce 2021-03-10 op wrefresh(body);
1293 740f578b 2021-03-15 op message(const char *fmt, ...)
1297 740f578b 2021-03-15 op va_start(ap, fmt);
1298 740f578b 2021-03-15 op vmessage(fmt, ap);
1303 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1305 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1307 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1308 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1309 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1313 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1315 8af5e5ed 2021-03-08 op struct tab *tab = d;
1317 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1319 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1320 8af5e5ed 2021-03-08 op wrefresh(modeline);
1322 8af5e5ed 2021-03-08 op wrefresh(body);
1323 9ca15951 2021-03-09 op if (in_minibuffer)
1324 9ca15951 2021-03-09 op wrefresh(minibuf);
1326 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1330 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1332 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1334 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1335 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1336 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1338 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1340 43a1b8d0 2021-03-09 op wrefresh(modeline);
1341 43a1b8d0 2021-03-09 op wrefresh(body);
1342 9ca15951 2021-03-09 op if (in_minibuffer)
1343 9ca15951 2021-03-09 op wrefresh(minibuf);
1347 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1349 43a1b8d0 2021-03-09 op empty_vlist(tab);
1350 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1351 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1352 8af5e5ed 2021-03-08 op load_url(tab, url);
1354 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1355 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1356 43a1b8d0 2021-03-09 op redraw_tab(tab);
1360 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1361 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1363 9ca15951 2021-03-09 op in_minibuffer = 1;
1364 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1365 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1367 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1369 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1370 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1371 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1372 9ca15951 2021-03-09 op ministate.off = 0;
1373 bb4e4ddf 2021-03-12 op ministate.len = 0;
1374 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1376 22268e11 2021-03-11 op ministate.history = hist;
1377 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1378 22268e11 2021-03-11 op ministate.hist_off = 0;
1382 b360ebb3 2021-03-10 op exit_minibuffer(void)
1384 156f1501 2021-03-13 op werase(minibuf);
1386 9ca15951 2021-03-09 op in_minibuffer = 0;
1387 9ca15951 2021-03-09 op base_map = &global_map;
1388 9ca15951 2021-03-09 op current_map = &global_map;
1392 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1394 5cd2ebb1 2021-03-11 op struct tab *t;
1396 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1397 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1400 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1403 b1df9b71 2021-03-12 op static struct tab *
1404 bcb0b073 2021-03-07 op new_tab(void)
1406 bcb0b073 2021-03-07 op struct tab *tab, *t;
1407 bcb0b073 2021-03-07 op const char *url = "about:new";
1409 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1412 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1414 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1416 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1417 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1419 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1420 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1422 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1424 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1428 bcb0b073 2021-03-07 op event_loopbreak();
1429 b1df9b71 2021-03-12 op return NULL;
1433 5e11c00c 2021-03-02 op ui_init(void)
1435 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1437 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1438 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1440 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1442 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1443 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1444 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1446 9ca15951 2021-03-09 op base_map = &global_map;
1447 f832146f 2021-03-09 op current_map = &global_map;
1448 f832146f 2021-03-09 op load_default_keys();
1455 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1457 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1459 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1461 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1463 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1466 48e9d457 2021-03-06 op body_lines = LINES-3;
1467 48e9d457 2021-03-06 op body_cols = COLS;
1469 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1470 48e9d457 2021-03-06 op scrollok(body, TRUE);
1472 5e11c00c 2021-03-02 op /* non-blocking input */
1473 48e9d457 2021-03-06 op wtimeout(body, 0);
1475 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1477 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1478 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1480 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1481 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1489 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1491 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1492 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1496 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1498 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1501 1d08c280 2021-03-06 op wrap_page(tab);
1502 5e11c00c 2021-03-02 op redraw_tab(tab);
1506 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1508 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1509 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1511 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1512 22268e11 2021-03-11 op &ir_history);
1513 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1514 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1515 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1519 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1523 740f578b 2021-03-15 op va_start(ap, fmt);
1524 740f578b 2021-03-15 op vmessage(fmt, ap);
1529 5e11c00c 2021-03-02 op ui_end(void)