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 1b81bc33 2021-03-15 op #define NEW_TAB_URL "about:new"
53 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
55 f832146f 2021-03-09 op static void load_default_keys(void);
56 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
57 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
59 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
60 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
61 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
62 88597eaf 2021-03-17 op static void cmd_forward_char(struct tab*);
63 852d03e8 2021-03-17 op static void cmd_backward_paragraph(struct tab*);
64 852d03e8 2021-03-17 op static void cmd_forward_paragraph(struct tab*);
65 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
66 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
67 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
68 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
69 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
70 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
71 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
72 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
73 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
74 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
75 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
76 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
77 a8c28919 2021-03-16 op static void cmd_previous_button(struct tab*);
78 a8c28919 2021-03-16 op static void cmd_next_button(struct tab*);
79 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
80 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
81 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
82 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
83 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
84 fea845b6 2021-03-15 op static void cmd_tab_close_other(struct tab*);
85 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
86 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
87 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
88 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
89 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
90 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
91 de0b2139 2021-03-15 op static void cmd_goto_bookmarks(struct tab*);
93 9ca15951 2021-03-09 op static void global_key_unbound(void);
95 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
96 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
97 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
98 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
99 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
100 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
101 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
102 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
103 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
104 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
105 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
107 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
108 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
109 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
110 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
111 b360ebb3 2021-03-10 op static void eecmd_select(void);
112 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
113 5cd2ebb1 2021-03-11 op static void ir_select(void);
114 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
115 5cd2ebb1 2021-03-11 op static void lu_select(void);
116 740f578b 2021-03-15 op static void bp_select(void);
118 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
119 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
120 8947c1f2 2021-03-21 op static int readkey(void);
121 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
122 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
123 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
124 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
125 9a25f829 2021-03-14 op static void print_vline(struct vline*);
126 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
127 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
128 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
129 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
130 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
131 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
132 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
133 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
134 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
135 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
136 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
137 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
138 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
139 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
140 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
141 941b3761 2021-03-18 op static void usage(void);
143 8947c1f2 2021-03-21 op static struct { short meta; int key; uint32_t cp; } thiskey;
145 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
146 48e9d457 2021-03-06 op static int body_lines, body_cols;
148 48e9d457 2021-03-06 op static struct event clminibufev;
149 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
150 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
152 bcb0b073 2021-03-07 op static uint32_t tab_counter;
154 7c7d7bb7 2021-03-10 op static char keybuf[64];
156 9ca15951 2021-03-09 op struct kmap global_map,
157 fa3fd864 2021-03-10 op minibuffer_map,
158 9ca15951 2021-03-09 op *current_map,
161 3148eeac 2021-03-13 op static struct histhead eecmd_history,
165 9ca15951 2021-03-09 op static int in_minibuffer;
167 9ca15951 2021-03-09 op static struct {
168 9cb0f9ce 2021-03-10 op char *curmesg;
170 040fbdf8 2021-03-10 op char buf[1025];
171 9ca15951 2021-03-09 op size_t off, len;
172 5cd2ebb1 2021-03-11 op char prompt[32];
173 b360ebb3 2021-03-10 op void (*donefn)(void);
174 b360ebb3 2021-03-10 op void (*abortfn)(void);
176 3148eeac 2021-03-13 op struct histhead *history;
177 3148eeac 2021-03-13 op struct hist *hist_cur;
178 22268e11 2021-03-11 op size_t hist_off;
181 f95d82dd 2021-03-11 op struct lineprefix {
182 f95d82dd 2021-03-11 op const char *prfx1;
183 f95d82dd 2021-03-11 op const char *prfx2;
184 f95d82dd 2021-03-11 op } line_prefixes[] = {
185 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
186 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
187 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
188 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
189 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
190 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
191 71d5deec 2021-03-19 op [LINE_QUOTE] = { "> ", " " },
192 803ff456 2021-03-17 op [LINE_PRE_START] = { "```", " " },
193 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
194 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
197 31840026 2021-03-16 op static struct line_face {
198 803ff456 2021-03-17 op int prefix_prop;
199 803ff456 2021-03-17 op int text_prop;
200 d3d56d8a 2021-03-11 op } line_faces[] = {
201 803ff456 2021-03-17 op [LINE_TEXT] = { 0, 0 },
202 803ff456 2021-03-17 op [LINE_LINK] = { 0, A_UNDERLINE },
203 803ff456 2021-03-17 op [LINE_TITLE_1] = { A_BOLD, A_BOLD },
204 803ff456 2021-03-17 op [LINE_TITLE_2] = { A_BOLD, A_BOLD },
205 803ff456 2021-03-17 op [LINE_TITLE_3] = { A_BOLD, A_BOLD },
206 803ff456 2021-03-17 op [LINE_ITEM] = { 0, 0 },
207 803ff456 2021-03-17 op [LINE_QUOTE] = { 0, A_DIM },
208 803ff456 2021-03-17 op [LINE_PRE_START] = { 0, 0 },
209 803ff456 2021-03-17 op [LINE_PRE_CONTENT] = { 0, 0 },
210 803ff456 2021-03-17 op [LINE_PRE_END] = { 0, 0 },
213 cb6c7aa0 2021-03-16 op static struct tab_face {
214 cb6c7aa0 2021-03-16 op int background, tab, current_tab;
215 cb6c7aa0 2021-03-16 op } tab_face = {
216 cb6c7aa0 2021-03-16 op A_REVERSE, A_REVERSE, A_NORMAL
220 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
222 65d9b3ca 2021-03-14 op struct vline *vl, *t;
224 452589f7 2021-03-16 op tab->s.current_line = NULL;
225 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
227 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
228 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
229 65d9b3ca 2021-03-14 op free(vl->line);
234 f832146f 2021-03-09 op static inline void
235 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
237 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
241 9ca15951 2021-03-09 op static inline void
242 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
244 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
249 f832146f 2021-03-09 op load_default_keys(void)
251 9ca15951 2021-03-09 op /* === global map === */
254 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
255 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
256 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
257 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
258 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
259 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
260 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
261 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
263 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
264 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
265 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
266 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
268 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
270 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
272 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
273 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
274 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
276 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
277 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
278 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
279 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
280 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
282 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
283 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
285 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
286 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
288 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
289 de0b2139 2021-03-15 op global_set_key("<f7> <f7>", cmd_goto_bookmarks);
291 f832146f 2021-03-09 op /* vi/vi-like */
292 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
293 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
294 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
295 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
296 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
297 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
298 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
299 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
301 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
302 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
304 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
305 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
306 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
307 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
309 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
310 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
312 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
313 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
316 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
318 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
320 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
323 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
324 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
325 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
326 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
327 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
328 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
330 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
331 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
333 f832146f 2021-03-09 op /* "ncurses standard" */
334 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
337 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
338 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
339 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
340 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
342 fa3fd864 2021-03-10 op /* === minibuffer map === */
343 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
344 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
345 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
346 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
347 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
349 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
350 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
351 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
352 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
353 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
354 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
355 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
356 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
357 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
359 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
360 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
361 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
362 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
366 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
368 452589f7 2021-03-16 op struct vline *vl;
369 452589f7 2021-03-16 op const char *prfx;
371 452589f7 2021-03-16 op vl =tab->s.current_line;
372 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
373 452589f7 2021-03-16 op tab->s.curs_x = tab->s.line_x = 0;
375 174b3cdf 2021-03-21 op tab->s.curs_x = utf8_snwidth(vl->line, tab->s.line_x);
377 452589f7 2021-03-16 op if (vl != NULL) {
378 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
379 174b3cdf 2021-03-21 op tab->s.curs_x += utf8_swidth(prfx);
384 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
386 452589f7 2021-03-16 op struct vline *vl;
388 452589f7 2021-03-16 op if (tab->s.current_line == NULL
389 452589f7 2021-03-16 op || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
392 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
393 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
394 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
398 452589f7 2021-03-16 op tab->s.current_line = vl;
399 48e9d457 2021-03-06 op restore_cursor(tab);
403 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
405 452589f7 2021-03-16 op struct vline *vl;
407 452589f7 2021-03-16 op if (tab->s.current_line == NULL
408 452589f7 2021-03-16 op || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
411 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
412 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
413 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
417 452589f7 2021-03-16 op tab->s.current_line = vl;
418 48e9d457 2021-03-06 op restore_cursor(tab);
422 88597eaf 2021-03-17 op cmd_backward_char(struct tab *tab)
424 88597eaf 2021-03-17 op if (tab->s.line_x != 0)
425 88597eaf 2021-03-17 op tab->s.line_x--;
426 48e9d457 2021-03-06 op restore_cursor(tab);
430 88597eaf 2021-03-17 op cmd_forward_char(struct tab *tab)
432 88597eaf 2021-03-17 op tab->s.line_x++;
433 a329982b 2021-03-11 op restore_cursor(tab);
437 852d03e8 2021-03-17 op cmd_backward_paragraph(struct tab *tab)
440 852d03e8 2021-03-17 op if (tab->s.current_line == NULL ||
441 852d03e8 2021-03-17 op tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
442 852d03e8 2021-03-17 op message("No previous paragraph");
445 852d03e8 2021-03-17 op cmd_previous_line(tab);
446 852d03e8 2021-03-17 op } while (tab->s.current_line->line != NULL ||
447 852d03e8 2021-03-17 op tab->s.current_line->parent->type != LINE_TEXT);
451 852d03e8 2021-03-17 op cmd_forward_paragraph(struct tab *tab)
454 852d03e8 2021-03-17 op if (tab->s.current_line == NULL ||
455 852d03e8 2021-03-17 op tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
456 852d03e8 2021-03-17 op message("No next paragraph");
459 852d03e8 2021-03-17 op cmd_next_line(tab);
460 852d03e8 2021-03-17 op } while (tab->s.current_line->line != NULL ||
461 852d03e8 2021-03-17 op tab->s.current_line->parent->type != LINE_TEXT);
465 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
467 452589f7 2021-03-16 op tab->s.line_x = 0;
468 a329982b 2021-03-11 op restore_cursor(tab);
472 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
474 9a25f829 2021-03-14 op struct vline *vl;
476 452589f7 2021-03-16 op vl = tab->s.current_line;
477 452589f7 2021-03-16 op if (vl->line == NULL)
479 174b3cdf 2021-03-21 op tab->s.line_x = utf8_cplen(vl->line);
480 48e9d457 2021-03-06 op restore_cursor(tab);
484 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
486 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
490 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
492 9a25f829 2021-03-14 op struct vline *vl;
494 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
497 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
498 48e9d457 2021-03-06 op wscrl(body, -1);
499 48e9d457 2021-03-06 op wmove(body, 0, 0);
500 9a25f829 2021-03-14 op print_vline(vl);
502 452589f7 2021-03-16 op tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
503 174b3cdf 2021-03-21 op restore_cursor(tab);
507 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
509 9a25f829 2021-03-14 op struct vline *vl;
511 452589f7 2021-03-16 op vl = tab->s.current_line;
512 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
514 452589f7 2021-03-16 op tab->s.current_line = vl;
516 65d9b3ca 2021-03-14 op tab->s.line_off++;
517 48e9d457 2021-03-06 op wscrl(body, 1);
519 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
522 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
523 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
524 9a25f829 2021-03-14 op print_vline(vl);
526 174b3cdf 2021-03-21 op restore_cursor(tab);
530 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
534 ed44414d 2021-03-09 op off = body_lines+1;
536 ed44414d 2021-03-09 op for (; off > 0; --off)
537 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
541 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
545 338eecdc 2021-03-12 op off = body_lines+1;
547 338eecdc 2021-03-12 op for (; off > 0; --off)
548 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
552 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
554 452589f7 2021-03-16 op tab->s.current_line = TAILQ_FIRST(&tab->s.head);
555 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
556 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
557 452589f7 2021-03-16 op tab->s.line_x = 0;
558 452589f7 2021-03-16 op restore_cursor(tab);
559 e19f9a04 2021-03-11 op redraw_body(tab);
563 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
567 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
568 e19f9a04 2021-03-11 op off = MAX(0, off);
570 65d9b3ca 2021-03-14 op tab->s.line_off = off;
571 452589f7 2021-03-16 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
573 452589f7 2021-03-16 op tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
574 452589f7 2021-03-16 op tab->s.line_x = body_cols;
575 452589f7 2021-03-16 op restore_cursor(tab);
576 e19f9a04 2021-03-11 op redraw_body(tab);
580 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
582 1d08c280 2021-03-06 op event_loopbreak();
586 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
588 9a25f829 2021-03-14 op struct vline *vl;
591 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
592 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
594 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
595 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
598 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
602 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
604 9a25f829 2021-03-14 op struct vline *vl;
607 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
608 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
610 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
611 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
614 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
618 a8c28919 2021-03-16 op cmd_previous_button(struct tab *tab)
621 a8c28919 2021-03-16 op if (tab->s.current_line == NULL ||
622 a8c28919 2021-03-16 op tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
623 a8c28919 2021-03-16 op message("No previous link");
626 a8c28919 2021-03-16 op cmd_previous_line(tab);
627 a8c28919 2021-03-16 op } while (tab->s.current_line->parent->type != LINE_LINK);
631 a8c28919 2021-03-16 op cmd_next_button(struct tab *tab)
634 a8c28919 2021-03-16 op if (tab->s.current_line == NULL ||
635 a8c28919 2021-03-16 op tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
636 a8c28919 2021-03-16 op message("No next link");
639 a8c28919 2021-03-16 op cmd_next_line(tab);
640 a8c28919 2021-03-16 op } while (tab->s.current_line->parent->type != LINE_LINK);
644 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
646 2051e653 2021-03-13 op if (!load_previous_page(tab))
647 2051e653 2021-03-13 op message("No previous page");
649 c40c2250 2021-03-14 op start_loading_anim(tab);
653 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
655 2051e653 2021-03-13 op if (!load_next_page(tab))
656 2051e653 2021-03-13 op message("No next page");
658 c40c2250 2021-03-14 op start_loading_anim(tab);
662 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
664 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
668 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
672 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
673 22268e11 2021-03-11 op &eecmd_history);
675 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
676 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
678 9ca15951 2021-03-09 op if (thiskey.meta)
679 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
681 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
682 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
686 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
688 7c7d7bb7 2021-03-10 op struct tab *t;
690 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
691 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
692 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
696 d87ffbdc 2021-03-17 op if (evtimer_pending(&tab->s.loadingev, NULL))
697 d87ffbdc 2021-03-17 op evtimer_del(&tab->s.loadingev);
699 7c7d7bb7 2021-03-10 op stop_tab(tab);
701 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
702 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
703 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
706 9d24c64f 2021-03-17 op switch_to_tab(t);
710 fea845b6 2021-03-15 op cmd_tab_close_other(struct tab *tab)
712 fea845b6 2021-03-15 op struct tab *t, *i;
714 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
715 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
719 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
725 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
727 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
731 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
733 7c7d7bb7 2021-03-10 op struct tab *t;
735 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
737 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
738 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
739 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
743 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
745 7c7d7bb7 2021-03-10 op struct tab *t;
747 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
749 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
750 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
751 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
755 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
757 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
758 22268e11 2021-03-11 op &lu_history);
759 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
763 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
765 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
766 22268e11 2021-03-11 op &lu_history);
767 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
768 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
769 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
770 740f578b 2021-03-15 op ministate.len = ministate.off;
774 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
776 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
777 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
778 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
779 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
780 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
784 de0b2139 2021-03-15 op cmd_goto_bookmarks(struct tab *tab)
786 de0b2139 2021-03-15 op load_url_in_tab(tab, "about:bookmarks");
790 7c7d7bb7 2021-03-10 op global_key_unbound(void)
792 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
796 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
798 3eecee9e 2021-03-12 op minibuffer_taint_hist();
800 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
803 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
804 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
805 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
806 d67133bf 2021-03-12 op ministate.len--;
810 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
812 3eecee9e 2021-03-12 op minibuffer_taint_hist();
814 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
817 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
818 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
819 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
820 9ca15951 2021-03-09 op ministate.off--;
821 9ca15951 2021-03-09 op ministate.len--;
825 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
827 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
829 9ca15951 2021-03-09 op ministate.off++;
833 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
835 9ca15951 2021-03-09 op if (ministate.off == 0)
837 9ca15951 2021-03-09 op ministate.off--;
841 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
843 9ca15951 2021-03-09 op ministate.off = ministate.len;
847 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
849 9ca15951 2021-03-09 op ministate.off = 0;
853 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
855 3eecee9e 2021-03-12 op minibuffer_taint_hist();
857 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
859 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
860 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
864 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
866 b360ebb3 2021-03-10 op ministate.abortfn();
870 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
872 22268e11 2021-03-11 op minibuffer_taint_hist();
873 b360ebb3 2021-03-10 op ministate.donefn();
877 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
879 22268e11 2021-03-11 op if (ministate.history == NULL) {
880 22268e11 2021-03-11 op message("No history");
884 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
885 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
886 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
887 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
888 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
889 22268e11 2021-03-11 op message("No prev item");
891 22268e11 2021-03-11 op ministate.hist_off--;
894 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
895 22268e11 2021-03-11 op ministate.off = 0;
896 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
901 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
903 22268e11 2021-03-11 op if (ministate.history == NULL) {
904 22268e11 2021-03-11 op message("No history");
908 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
909 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
910 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
911 22268e11 2021-03-11 op ministate.hist_off = 0;
912 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
913 22268e11 2021-03-11 op message("No next item");
915 22268e11 2021-03-11 op ministate.hist_off++;
918 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
919 22268e11 2021-03-11 op ministate.off = 0;
920 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
925 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
927 3148eeac 2021-03-13 op struct hist *hist;
929 22268e11 2021-03-11 op if (ministate.history == NULL)
932 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
935 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
937 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
938 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
940 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
941 22268e11 2021-03-11 op ministate.history->len++;
945 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
946 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
947 22268e11 2021-03-11 op * navigation" thing.
950 22268e11 2021-03-11 op minibuffer_taint_hist(void)
952 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
955 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
956 22268e11 2021-03-11 op ministate.hist_cur = NULL;
960 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
962 22268e11 2021-03-11 op minibuffer_taint_hist();
964 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
967 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
969 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
970 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
971 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
972 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
973 9ca15951 2021-03-09 op ministate.off++;
974 9ca15951 2021-03-09 op ministate.len++;
978 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
980 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
981 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
982 5cd2ebb1 2021-03-11 op global_key_unbound();
986 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
990 b360ebb3 2021-03-10 op eecmd_select(void)
992 b360ebb3 2021-03-10 op exit_minibuffer();
993 22268e11 2021-03-11 op minibuffer_hist_save_entry();
994 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
998 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1000 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1004 5cd2ebb1 2021-03-11 op ir_select(void)
1006 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1007 5cd2ebb1 2021-03-11 op struct url url;
1008 5cd2ebb1 2021-03-11 op struct tab *tab;
1010 5cd2ebb1 2021-03-11 op tab = current_tab();
1012 5cd2ebb1 2021-03-11 op exit_minibuffer();
1013 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1015 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1016 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
1017 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
1018 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
1019 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1023 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1025 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
1026 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
1027 5cd2ebb1 2021-03-11 op global_key_unbound();
1031 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1035 5cd2ebb1 2021-03-11 op lu_select(void)
1037 5cd2ebb1 2021-03-11 op exit_minibuffer();
1038 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1039 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1043 740f578b 2021-03-15 op bp_select(void)
1045 740f578b 2021-03-15 op exit_minibuffer();
1046 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1047 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1049 740f578b 2021-03-15 op message("Abort.");
1052 9a25f829 2021-03-14 op static struct vline *
1053 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
1055 9a25f829 2021-03-14 op struct vline *vl;
1059 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1065 48e9d457 2021-03-06 op /* unreachable */
1069 5e11c00c 2021-03-02 op static struct tab *
1070 5e11c00c 2021-03-02 op current_tab(void)
1072 5e11c00c 2021-03-02 op struct tab *t;
1074 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1075 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1079 5e11c00c 2021-03-02 op /* unreachable */
1084 8947c1f2 2021-03-21 op readkey(void)
1086 8947c1f2 2021-03-21 op uint32_t state = 0;
1088 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
1091 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
1092 8947c1f2 2021-03-21 op if (thiskey.meta) {
1093 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1094 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1095 c314a314 2021-03-11 op thiskey.meta = 0;
1096 9ca15951 2021-03-09 op thiskey.key = 27;
1100 8947c1f2 2021-03-21 op thiskey.cp = 0;
1101 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
1103 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1105 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
1106 8947c1f2 2021-03-21 op message("Error decoding user input");
1116 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
1118 8947c1f2 2021-03-21 op struct keymap *k;
1119 8947c1f2 2021-03-21 op const char *keyname;
1120 8947c1f2 2021-03-21 op char tmp[5] = {0};
1122 8947c1f2 2021-03-21 op if (!readkey())
1125 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1126 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1127 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1128 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1129 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
1130 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
1131 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1133 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
1134 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
1136 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
1137 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
1141 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1142 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1143 9ca15951 2021-03-09 op k->key == thiskey.key) {
1144 f832146f 2021-03-09 op if (k->fn == NULL)
1145 f832146f 2021-03-09 op current_map = &k->map;
1147 9ca15951 2021-03-09 op current_map = base_map;
1148 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1149 f832146f 2021-03-09 op k->fn(current_tab());
1155 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1156 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1158 7c7d7bb7 2021-03-10 op global_key_unbound();
1161 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1162 9ca15951 2021-03-09 op current_map = base_map;
1165 179f0f58 2021-03-11 op redraw_tab(current_tab());
1169 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1171 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1172 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1174 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1175 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1176 9cb0f9ce 2021-03-10 op wrefresh(body);
1177 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1179 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1180 9cb0f9ce 2021-03-10 op wrefresh(body);
1185 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1187 1d08c280 2021-03-06 op struct tab *tab;
1193 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1195 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1196 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1198 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1199 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1201 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1202 48e9d457 2021-03-06 op body_lines = LINES-3;
1203 bd9637e9 2021-03-06 op body_cols = COLS;
1205 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1207 1d08c280 2021-03-06 op tab = current_tab();
1209 1d08c280 2021-03-06 op wrap_page(tab);
1210 1d08c280 2021-03-06 op redraw_tab(tab);
1214 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1216 452589f7 2021-03-16 op struct line *l;
1217 452589f7 2021-03-16 op const struct line *orig;
1218 d511dc85 2021-03-16 op struct vline *vl;
1219 452589f7 2021-03-16 op const char *prfx;
1221 452589f7 2021-03-16 op orig = tab->s.current_line == NULL
1223 452589f7 2021-03-16 op : tab->s.current_line->parent;
1224 452589f7 2021-03-16 op tab->s.current_line = NULL;
1226 452589f7 2021-03-16 op tab->s.curs_y = 0;
1227 452589f7 2021-03-16 op tab->s.line_off = 0;
1229 1d08c280 2021-03-06 op empty_vlist(tab);
1231 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1232 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1233 5e11c00c 2021-03-02 op switch (l->type) {
1234 5e11c00c 2021-03-02 op case LINE_TEXT:
1235 5e11c00c 2021-03-02 op case LINE_LINK:
1236 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1237 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1238 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1239 5e11c00c 2021-03-02 op case LINE_ITEM:
1240 5e11c00c 2021-03-02 op case LINE_QUOTE:
1241 5e11c00c 2021-03-02 op case LINE_PRE_START:
1242 5e11c00c 2021-03-02 op case LINE_PRE_END:
1243 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1245 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1246 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1250 452589f7 2021-03-16 op if (orig == l && tab->s.current_line == NULL) {
1251 452589f7 2021-03-16 op tab->s.line_off = tab->s.line_max-1;
1252 452589f7 2021-03-16 op tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1255 d511dc85 2021-03-16 op vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1256 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1258 d511dc85 2021-03-16 op tab->s.current_line = vl;
1259 d511dc85 2021-03-16 op tab->s.line_off--;
1264 452589f7 2021-03-16 op if (tab->s.current_line == NULL)
1265 452589f7 2021-03-16 op tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1271 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1273 9a25f829 2021-03-14 op const char *text = vl->line;
1274 768db5bb 2021-03-12 op const char *prfx;
1275 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1276 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1278 9a25f829 2021-03-14 op if (!vl->flags)
1279 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1281 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1283 bd9637e9 2021-03-06 op if (text == NULL)
1286 803ff456 2021-03-17 op wattron(body, prefix_face);
1287 803ff456 2021-03-17 op wprintw(body, "%s", prfx);
1288 803ff456 2021-03-17 op wattroff(body, prefix_face);
1290 803ff456 2021-03-17 op wattron(body, text_face);
1291 803ff456 2021-03-17 op wprintw(body, "%s", text);
1292 803ff456 2021-03-17 op wattroff(body, text_face);
1296 8af5e5ed 2021-03-08 op redraw_tabline(void)
1298 7c7d7bb7 2021-03-10 op struct tab *tab;
1299 a636f50e 2021-03-16 op size_t toskip;
1300 a636f50e 2021-03-16 op int current, x, y, truncated;
1301 dc5df781 2021-03-13 op const char *title;
1302 119f393c 2021-03-16 op char buf[25];
1306 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1307 a636f50e 2021-03-16 op x += sizeof(buf) + 1;
1309 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1312 a636f50e 2021-03-16 op if (x < COLS-2)
1317 a636f50e 2021-03-16 op werase(tabline);
1318 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1319 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1320 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1322 a636f50e 2021-03-16 op truncated = 0;
1323 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1324 a636f50e 2021-03-16 op if (truncated)
1326 a636f50e 2021-03-16 op if (toskip != 0) {
1331 a636f50e 2021-03-16 op getyx(tabline, y, x);
1332 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1333 a636f50e 2021-03-16 op truncated = 1;
1335 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1337 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1338 2051e653 2021-03-13 op title = tab->hist_cur->h;
1340 119f393c 2021-03-16 op strlcpy(buf, " ", sizeof(buf));
1341 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1342 119f393c 2021-03-16 op /* truncation happens */
1343 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1345 119f393c 2021-03-16 op /* pad with spaces */
1346 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1350 a329982b 2021-03-11 op if (current)
1351 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1353 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1355 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1356 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1357 119f393c 2021-03-16 op wprintw(tabline, " ");
1359 cb6c7aa0 2021-03-16 op if (current)
1360 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1362 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1365 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1366 119f393c 2021-03-16 op for (; x < COLS; ++x)
1367 119f393c 2021-03-16 op waddch(tabline, ' ');
1368 a636f50e 2021-03-16 op if (truncated)
1369 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1372 10346511 2021-03-17 op static inline char
1373 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1375 10346511 2021-03-17 op switch (ts) {
1376 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1377 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1378 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1379 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1384 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1387 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1388 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1389 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1391 156f1501 2021-03-13 op werase(modeline);
1392 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1393 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1395 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1396 58c75fab 2021-03-16 op spin[tab->s.loading_anim_step],
1397 10346511 2021-03-17 op trust_status_char(tab->trust),
1398 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1400 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1402 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1403 481340cc 2021-03-11 op wprintw(modeline, "All ");
1404 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1405 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1406 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1407 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1409 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1411 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1412 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1413 65d9b3ca 2021-03-14 op tab->s.line_max,
1414 2051e653 2021-03-13 op tab->hist_cur->h);
1416 48e9d457 2021-03-06 op getyx(modeline, y, x);
1417 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1420 48e9d457 2021-03-06 op (void)max_y;
1422 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1423 48e9d457 2021-03-06 op waddstr(modeline, "-");
1427 9ca15951 2021-03-09 op redraw_minibuffer(void)
1429 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1430 8300dd3c 2021-03-18 op struct tab *tab;
1432 156f1501 2021-03-13 op werase(minibuf);
1433 22268e11 2021-03-11 op if (in_minibuffer) {
1434 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1435 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1436 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1437 22268e11 2021-03-11 op ministate.hist_off + 1,
1438 22268e11 2021-03-11 op ministate.history->len);
1440 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1442 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1443 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1446 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1447 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1449 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1452 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1453 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1454 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1456 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1459 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1460 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1462 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1463 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1464 8300dd3c 2021-03-18 op tab = current_tab();
1465 8300dd3c 2021-03-18 op if (tab->s.current_line != NULL &&
1466 8300dd3c 2021-03-18 op tab->s.current_line->parent->type == LINE_LINK)
1467 8300dd3c 2021-03-18 op wprintw(minibuf, "%s",
1468 8300dd3c 2021-03-18 op tab->s.current_line->parent->alt);
1471 2aaf475e 2021-03-13 op if (in_minibuffer)
1472 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1476 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1478 e19f9a04 2021-03-11 op redraw_tabline();
1479 e19f9a04 2021-03-11 op redraw_body(tab);
1480 e19f9a04 2021-03-11 op redraw_modeline(tab);
1481 e19f9a04 2021-03-11 op redraw_minibuffer();
1483 e19f9a04 2021-03-11 op wrefresh(tabline);
1484 e19f9a04 2021-03-11 op wrefresh(modeline);
1486 e19f9a04 2021-03-11 op if (in_minibuffer) {
1487 e19f9a04 2021-03-11 op wrefresh(body);
1488 e19f9a04 2021-03-11 op wrefresh(minibuf);
1490 e19f9a04 2021-03-11 op wrefresh(minibuf);
1491 e19f9a04 2021-03-11 op wrefresh(body);
1496 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1498 9a25f829 2021-03-14 op struct vline *vl;
1501 48e9d457 2021-03-06 op werase(body);
1503 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1504 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1508 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1509 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1510 48e9d457 2021-03-06 op wmove(body, line, 0);
1511 9a25f829 2021-03-14 op print_vline(vl);
1513 48e9d457 2021-03-06 op if (line == body_lines)
1517 174b3cdf 2021-03-21 op wmove(body, tab->s.curs_y, tab->s.curs_x);
1521 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1523 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1524 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1525 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1526 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1528 bf992581 2021-03-12 op free(ministate.curmesg);
1530 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1531 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1532 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1534 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1535 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1536 9cb0f9ce 2021-03-10 op wrefresh(body);
1537 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1539 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1540 9cb0f9ce 2021-03-10 op wrefresh(body);
1545 740f578b 2021-03-15 op message(const char *fmt, ...)
1549 740f578b 2021-03-15 op va_start(ap, fmt);
1550 740f578b 2021-03-15 op vmessage(fmt, ap);
1555 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1557 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1559 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1560 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1561 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1565 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1567 8af5e5ed 2021-03-08 op struct tab *tab = d;
1569 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1571 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1572 6347dcd0 2021-03-16 op redraw_modeline(tab);
1573 6347dcd0 2021-03-16 op wrefresh(modeline);
1574 6347dcd0 2021-03-16 op wrefresh(body);
1575 6347dcd0 2021-03-16 op if (in_minibuffer)
1576 6347dcd0 2021-03-16 op wrefresh(minibuf);
1579 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1583 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1585 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1587 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1588 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1589 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1591 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1594 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1596 43a1b8d0 2021-03-09 op wrefresh(modeline);
1597 43a1b8d0 2021-03-09 op wrefresh(body);
1598 9ca15951 2021-03-09 op if (in_minibuffer)
1599 9ca15951 2021-03-09 op wrefresh(minibuf);
1603 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1605 43a1b8d0 2021-03-09 op empty_vlist(tab);
1606 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1607 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1608 8af5e5ed 2021-03-08 op load_url(tab, url);
1610 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1611 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1612 43a1b8d0 2021-03-09 op redraw_tab(tab);
1616 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1617 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1619 9ca15951 2021-03-09 op in_minibuffer = 1;
1620 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1621 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1623 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1625 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1626 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1627 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1628 9ca15951 2021-03-09 op ministate.off = 0;
1629 bb4e4ddf 2021-03-12 op ministate.len = 0;
1630 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1632 22268e11 2021-03-11 op ministate.history = hist;
1633 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1634 22268e11 2021-03-11 op ministate.hist_off = 0;
1638 b360ebb3 2021-03-10 op exit_minibuffer(void)
1640 156f1501 2021-03-13 op werase(minibuf);
1642 9ca15951 2021-03-09 op in_minibuffer = 0;
1643 9ca15951 2021-03-09 op base_map = &global_map;
1644 9ca15951 2021-03-09 op current_map = &global_map;
1648 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1650 5cd2ebb1 2021-03-11 op struct tab *t;
1652 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1653 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1656 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1659 b1df9b71 2021-03-12 op static struct tab *
1660 1b81bc33 2021-03-15 op new_tab(const char *url)
1662 754622a2 2021-03-15 op struct tab *tab;
1664 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1665 71105afa 2021-03-16 op event_loopbreak();
1666 71105afa 2021-03-16 op return NULL;
1669 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1671 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1673 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1674 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1676 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1677 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1679 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1681 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1688 941b3761 2021-03-18 op fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1692 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1694 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1697 941b3761 2021-03-18 op while ((ch = getopt(argc, argv, "")) != -1) {
1698 941b3761 2021-03-18 op switch (ch) {
1704 941b3761 2021-03-18 op argc -= optind;
1705 941b3761 2021-03-18 op argv += optind;
1707 941b3761 2021-03-18 op if (argc != 0)
1708 941b3761 2021-03-18 op url = argv[0];
1710 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1712 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1713 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1715 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1717 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1718 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1719 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1721 9ca15951 2021-03-09 op base_map = &global_map;
1722 f832146f 2021-03-09 op current_map = &global_map;
1723 f832146f 2021-03-09 op load_default_keys();
1730 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1732 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1734 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1736 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1738 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1741 48e9d457 2021-03-06 op body_lines = LINES-3;
1742 48e9d457 2021-03-06 op body_cols = COLS;
1744 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1745 48e9d457 2021-03-06 op scrollok(body, TRUE);
1747 5e11c00c 2021-03-02 op /* non-blocking input */
1748 48e9d457 2021-03-06 op wtimeout(body, 0);
1750 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1752 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1753 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1755 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1756 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1758 941b3761 2021-03-18 op new_tab(url);
1764 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1766 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1767 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1769 3d8c2326 2021-03-18 op redraw_tabline();
1770 3d8c2326 2021-03-18 op wrefresh(tabline);
1771 3d8c2326 2021-03-18 op if (in_minibuffer)
1772 3d8c2326 2021-03-18 op wrefresh(minibuf);
1774 3d8c2326 2021-03-18 op wrefresh(body);
1778 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1780 1d08c280 2021-03-06 op wrap_page(tab);
1781 3d8c2326 2021-03-18 op if (tab->flags & TAB_CURRENT)
1782 3d8c2326 2021-03-18 op redraw_tab(tab);
1786 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1788 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1789 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1791 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1792 22268e11 2021-03-11 op &ir_history);
1793 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1794 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1795 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1799 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1803 740f578b 2021-03-15 op va_start(ap, fmt);
1804 740f578b 2021-03-15 op vmessage(fmt, ap);
1809 5e11c00c 2021-03-02 op ui_end(void)