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_forward_char(struct tab*);
62 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
63 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
64 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
65 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
66 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
67 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
68 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
69 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
70 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
71 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
72 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
73 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
74 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
75 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
76 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
77 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
78 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
79 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
80 fea845b6 2021-03-15 op static void cmd_tab_close_other(struct tab*);
81 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
82 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
83 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
84 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
85 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
86 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
87 de0b2139 2021-03-15 op static void cmd_goto_bookmarks(struct tab*);
89 9ca15951 2021-03-09 op static void global_key_unbound(void);
91 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
92 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
93 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
94 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
95 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
96 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
97 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
98 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
99 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
100 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
101 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
103 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
104 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
105 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
106 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
107 b360ebb3 2021-03-10 op static void eecmd_select(void);
108 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
109 5cd2ebb1 2021-03-11 op static void ir_select(void);
110 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
111 5cd2ebb1 2021-03-11 op static void lu_select(void);
112 740f578b 2021-03-15 op static void bp_select(void);
114 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
115 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
116 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
117 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
118 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
119 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
120 9a25f829 2021-03-14 op static void print_vline(struct vline*);
121 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
122 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
123 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
124 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
125 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
126 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
127 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
128 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
129 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
130 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
131 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
132 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
133 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
134 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
135 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
137 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
139 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
140 48e9d457 2021-03-06 op static int body_lines, body_cols;
142 48e9d457 2021-03-06 op static struct event clminibufev;
143 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
144 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
146 bcb0b073 2021-03-07 op static uint32_t tab_counter;
148 7c7d7bb7 2021-03-10 op static char keybuf[64];
150 9ca15951 2021-03-09 op struct kmap global_map,
151 fa3fd864 2021-03-10 op minibuffer_map,
152 9ca15951 2021-03-09 op *current_map,
155 3148eeac 2021-03-13 op static struct histhead eecmd_history,
159 9ca15951 2021-03-09 op static int in_minibuffer;
161 9ca15951 2021-03-09 op static struct {
162 9cb0f9ce 2021-03-10 op char *curmesg;
164 040fbdf8 2021-03-10 op char buf[1025];
165 9ca15951 2021-03-09 op size_t off, len;
166 5cd2ebb1 2021-03-11 op char prompt[32];
167 b360ebb3 2021-03-10 op void (*donefn)(void);
168 b360ebb3 2021-03-10 op void (*abortfn)(void);
170 3148eeac 2021-03-13 op struct histhead *history;
171 3148eeac 2021-03-13 op struct hist *hist_cur;
172 22268e11 2021-03-11 op size_t hist_off;
175 f95d82dd 2021-03-11 op struct lineprefix {
176 f95d82dd 2021-03-11 op const char *prfx1;
177 f95d82dd 2021-03-11 op const char *prfx2;
178 f95d82dd 2021-03-11 op } line_prefixes[] = {
179 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
180 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
181 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
182 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
183 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
184 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
185 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
186 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
187 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
188 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
191 31840026 2021-03-16 op static struct line_face {
193 d3d56d8a 2021-03-11 op } line_faces[] = {
194 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
195 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
196 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
197 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
198 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
199 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
200 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
201 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
202 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
203 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
206 cb6c7aa0 2021-03-16 op static struct tab_face {
207 cb6c7aa0 2021-03-16 op int background, tab, current_tab;
208 cb6c7aa0 2021-03-16 op } tab_face = {
209 cb6c7aa0 2021-03-16 op A_REVERSE, A_REVERSE, A_NORMAL
213 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
215 65d9b3ca 2021-03-14 op struct vline *vl, *t;
217 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
219 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
220 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
221 65d9b3ca 2021-03-14 op free(vl->line);
226 f832146f 2021-03-09 op static inline void
227 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
229 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
233 9ca15951 2021-03-09 op static inline void
234 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
236 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
241 f832146f 2021-03-09 op load_default_keys(void)
243 9ca15951 2021-03-09 op /* === global map === */
246 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
247 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
248 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
249 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
250 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
251 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
253 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
254 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
255 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
256 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
258 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
260 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
262 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
263 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
264 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
266 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
267 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
268 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
269 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
270 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
272 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
273 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
275 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
276 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
278 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
279 de0b2139 2021-03-15 op global_set_key("<f7> <f7>", cmd_goto_bookmarks);
281 f832146f 2021-03-09 op /* vi/vi-like */
282 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
283 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
284 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
285 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
286 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
287 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
289 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
290 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
292 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
293 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
294 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
295 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
297 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
298 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
300 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
301 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
304 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
306 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
308 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
311 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
312 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
313 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
314 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
315 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
316 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
318 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
319 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
321 f832146f 2021-03-09 op /* "ncurses standard" */
322 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
325 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
326 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
328 fa3fd864 2021-03-10 op /* === minibuffer map === */
329 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
330 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
331 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
332 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
333 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
335 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
336 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
337 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
338 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
339 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
340 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
341 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
342 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
343 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
345 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
346 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
347 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
348 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
352 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
354 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
358 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
360 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
361 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
362 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
365 48e9d457 2021-03-06 op restore_cursor(tab);
369 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
371 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
374 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
375 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
376 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
379 48e9d457 2021-03-06 op restore_cursor(tab);
383 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
385 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
386 48e9d457 2021-03-06 op restore_cursor(tab);
390 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
392 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
393 a329982b 2021-03-11 op restore_cursor(tab);
397 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
399 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
400 a329982b 2021-03-11 op restore_cursor(tab);
404 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
406 9a25f829 2021-03-14 op struct vline *vl;
408 f95d82dd 2021-03-11 op const char *prfx;
410 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
411 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
412 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
416 9a25f829 2021-03-14 op vl = nth_line(tab, off);
417 9a25f829 2021-03-14 op if (vl->line != NULL)
418 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
420 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
422 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
423 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
426 48e9d457 2021-03-06 op restore_cursor(tab);
430 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
432 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
436 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
438 9a25f829 2021-03-14 op struct vline *vl;
440 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
443 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
444 48e9d457 2021-03-06 op wscrl(body, -1);
445 48e9d457 2021-03-06 op wmove(body, 0, 0);
446 9a25f829 2021-03-14 op print_vline(vl);
450 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
452 9a25f829 2021-03-14 op struct vline *vl;
454 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
457 65d9b3ca 2021-03-14 op tab->s.line_off++;
458 48e9d457 2021-03-06 op wscrl(body, 1);
460 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
463 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
464 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
465 9a25f829 2021-03-14 op print_vline(vl);
469 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
473 ed44414d 2021-03-09 op off = body_lines+1;
475 ed44414d 2021-03-09 op for (; off > 0; --off)
476 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
480 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
484 338eecdc 2021-03-12 op off = body_lines+1;
486 338eecdc 2021-03-12 op for (; off > 0; --off)
487 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
491 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
493 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
494 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
495 e19f9a04 2021-03-11 op redraw_body(tab);
499 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
503 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
504 e19f9a04 2021-03-11 op off = MAX(0, off);
506 65d9b3ca 2021-03-14 op tab->s.line_off = off;
507 754622a2 2021-03-15 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
509 e19f9a04 2021-03-11 op redraw_body(tab);
513 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
515 1d08c280 2021-03-06 op event_loopbreak();
519 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
521 9a25f829 2021-03-14 op struct vline *vl;
524 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
525 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
527 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
528 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
531 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
535 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
537 9a25f829 2021-03-14 op struct vline *vl;
540 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
541 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
543 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
544 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
547 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
551 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
553 2051e653 2021-03-13 op if (!load_previous_page(tab))
554 2051e653 2021-03-13 op message("No previous page");
556 c40c2250 2021-03-14 op start_loading_anim(tab);
560 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
562 2051e653 2021-03-13 op if (!load_next_page(tab))
563 2051e653 2021-03-13 op message("No next page");
565 c40c2250 2021-03-14 op start_loading_anim(tab);
569 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
571 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
575 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
579 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
580 22268e11 2021-03-11 op &eecmd_history);
582 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
583 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
585 9ca15951 2021-03-09 op if (thiskey.meta)
586 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
588 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
589 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
593 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
595 7c7d7bb7 2021-03-10 op struct tab *t;
597 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
598 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
599 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
603 7c7d7bb7 2021-03-10 op stop_tab(tab);
605 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
606 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
608 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
614 fea845b6 2021-03-15 op cmd_tab_close_other(struct tab *tab)
616 fea845b6 2021-03-15 op struct tab *t, *i;
618 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
619 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
623 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
629 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
631 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
635 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
637 7c7d7bb7 2021-03-10 op struct tab *t;
639 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
641 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
642 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
643 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
647 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
649 7c7d7bb7 2021-03-10 op struct tab *t;
651 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
653 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
654 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
655 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
659 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
661 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
662 22268e11 2021-03-11 op &lu_history);
663 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
667 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
669 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
670 22268e11 2021-03-11 op &lu_history);
671 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
672 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
673 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
674 740f578b 2021-03-15 op ministate.len = ministate.off;
678 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
680 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
681 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
682 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
683 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
684 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
688 de0b2139 2021-03-15 op cmd_goto_bookmarks(struct tab *tab)
690 de0b2139 2021-03-15 op load_url_in_tab(tab, "about:bookmarks");
694 7c7d7bb7 2021-03-10 op global_key_unbound(void)
696 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
700 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
702 3eecee9e 2021-03-12 op minibuffer_taint_hist();
704 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
707 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
708 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
709 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
710 d67133bf 2021-03-12 op ministate.len--;
714 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
716 3eecee9e 2021-03-12 op minibuffer_taint_hist();
718 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
721 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
722 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
723 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
724 9ca15951 2021-03-09 op ministate.off--;
725 9ca15951 2021-03-09 op ministate.len--;
729 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
731 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
733 9ca15951 2021-03-09 op ministate.off++;
737 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
739 9ca15951 2021-03-09 op if (ministate.off == 0)
741 9ca15951 2021-03-09 op ministate.off--;
745 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
747 9ca15951 2021-03-09 op ministate.off = ministate.len;
751 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
753 9ca15951 2021-03-09 op ministate.off = 0;
757 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
759 3eecee9e 2021-03-12 op minibuffer_taint_hist();
761 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
763 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
764 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
768 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
770 b360ebb3 2021-03-10 op ministate.abortfn();
774 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
776 22268e11 2021-03-11 op minibuffer_taint_hist();
777 b360ebb3 2021-03-10 op ministate.donefn();
781 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
783 22268e11 2021-03-11 op if (ministate.history == NULL) {
784 22268e11 2021-03-11 op message("No history");
788 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
789 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
790 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
791 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
792 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
793 22268e11 2021-03-11 op message("No prev item");
795 22268e11 2021-03-11 op ministate.hist_off--;
798 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
799 22268e11 2021-03-11 op ministate.off = 0;
800 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
805 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
807 22268e11 2021-03-11 op if (ministate.history == NULL) {
808 22268e11 2021-03-11 op message("No history");
812 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
813 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
814 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
815 22268e11 2021-03-11 op ministate.hist_off = 0;
816 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
817 22268e11 2021-03-11 op message("No next item");
819 22268e11 2021-03-11 op ministate.hist_off++;
822 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
823 22268e11 2021-03-11 op ministate.off = 0;
824 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
829 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
831 3148eeac 2021-03-13 op struct hist *hist;
833 22268e11 2021-03-11 op if (ministate.history == NULL)
836 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
839 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
841 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
842 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
844 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
845 22268e11 2021-03-11 op ministate.history->len++;
849 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
850 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
851 22268e11 2021-03-11 op * navigation" thing.
854 22268e11 2021-03-11 op minibuffer_taint_hist(void)
856 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
859 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
860 22268e11 2021-03-11 op ministate.hist_cur = NULL;
864 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
866 22268e11 2021-03-11 op minibuffer_taint_hist();
868 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
871 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
873 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
874 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
875 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
876 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
877 9ca15951 2021-03-09 op ministate.off++;
878 9ca15951 2021-03-09 op ministate.len++;
882 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
884 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
885 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
886 5cd2ebb1 2021-03-11 op global_key_unbound();
890 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
894 b360ebb3 2021-03-10 op eecmd_select(void)
896 b360ebb3 2021-03-10 op exit_minibuffer();
897 22268e11 2021-03-11 op minibuffer_hist_save_entry();
898 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
902 5cd2ebb1 2021-03-11 op ir_self_insert(void)
904 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
908 5cd2ebb1 2021-03-11 op ir_select(void)
910 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
911 5cd2ebb1 2021-03-11 op struct url url;
912 5cd2ebb1 2021-03-11 op struct tab *tab;
914 5cd2ebb1 2021-03-11 op tab = current_tab();
916 5cd2ebb1 2021-03-11 op exit_minibuffer();
917 22268e11 2021-03-11 op minibuffer_hist_save_entry();
919 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
920 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
921 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
922 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
923 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
927 5cd2ebb1 2021-03-11 op lu_self_insert(void)
929 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
930 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
931 5cd2ebb1 2021-03-11 op global_key_unbound();
935 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
939 5cd2ebb1 2021-03-11 op lu_select(void)
941 5cd2ebb1 2021-03-11 op exit_minibuffer();
942 22268e11 2021-03-11 op minibuffer_hist_save_entry();
943 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
947 740f578b 2021-03-15 op bp_select(void)
949 740f578b 2021-03-15 op exit_minibuffer();
950 740f578b 2021-03-15 op if (*ministate.buf != '\0')
951 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
953 740f578b 2021-03-15 op message("Abort.");
956 9a25f829 2021-03-14 op static struct vline *
957 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
959 9a25f829 2021-03-14 op struct vline *vl;
963 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
969 48e9d457 2021-03-06 op /* unreachable */
973 5e11c00c 2021-03-02 op static struct tab *
974 5e11c00c 2021-03-02 op current_tab(void)
976 5e11c00c 2021-03-02 op struct tab *t;
978 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
979 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
983 5e11c00c 2021-03-02 op /* unreachable */
988 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
990 f832146f 2021-03-09 op struct keymap *k;
991 7c7d7bb7 2021-03-10 op const char *keyname;
992 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
994 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
995 9ca15951 2021-03-09 op if (thiskey.key == ERR)
997 9ca15951 2021-03-09 op if (thiskey.key == 27) {
998 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
1000 9ca15951 2021-03-09 op thiskey.meta = 1;
1001 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1002 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1003 c314a314 2021-03-11 op thiskey.meta = 0;
1004 9ca15951 2021-03-09 op thiskey.key = 27;
1007 9ca15951 2021-03-09 op thiskey.meta = 0;
1009 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1010 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1011 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1012 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1013 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
1014 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
1016 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
1017 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1020 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1021 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1022 9ca15951 2021-03-09 op k->key == thiskey.key) {
1023 f832146f 2021-03-09 op if (k->fn == NULL)
1024 f832146f 2021-03-09 op current_map = &k->map;
1026 9ca15951 2021-03-09 op current_map = base_map;
1027 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1028 f832146f 2021-03-09 op k->fn(current_tab());
1034 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1035 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1037 7c7d7bb7 2021-03-10 op global_key_unbound();
1040 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1041 9ca15951 2021-03-09 op current_map = base_map;
1044 179f0f58 2021-03-11 op redraw_tab(current_tab());
1048 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1050 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1051 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1053 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1054 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1055 9cb0f9ce 2021-03-10 op wrefresh(body);
1056 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1058 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1059 9cb0f9ce 2021-03-10 op wrefresh(body);
1064 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1066 1d08c280 2021-03-06 op struct tab *tab;
1072 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1074 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1075 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1077 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1078 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1080 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1081 48e9d457 2021-03-06 op body_lines = LINES-3;
1082 bd9637e9 2021-03-06 op body_cols = COLS;
1084 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1086 1d08c280 2021-03-06 op tab = current_tab();
1088 1d08c280 2021-03-06 op wrap_page(tab);
1089 1d08c280 2021-03-06 op redraw_tab(tab);
1093 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1095 1d08c280 2021-03-06 op struct line *l;
1096 0d568960 2021-03-11 op const char *prfx;
1098 1d08c280 2021-03-06 op empty_vlist(tab);
1100 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1101 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1102 5e11c00c 2021-03-02 op switch (l->type) {
1103 5e11c00c 2021-03-02 op case LINE_TEXT:
1104 5e11c00c 2021-03-02 op case LINE_LINK:
1105 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1106 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1107 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1108 5e11c00c 2021-03-02 op case LINE_ITEM:
1109 5e11c00c 2021-03-02 op case LINE_QUOTE:
1110 5e11c00c 2021-03-02 op case LINE_PRE_START:
1111 5e11c00c 2021-03-02 op case LINE_PRE_END:
1112 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1113 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1115 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1116 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1124 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1126 9a25f829 2021-03-14 op const char *text = vl->line;
1127 768db5bb 2021-03-12 op const char *prfx;
1128 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1130 9a25f829 2021-03-14 op if (!vl->flags)
1131 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1133 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1135 bd9637e9 2021-03-06 op if (text == NULL)
1138 d3d56d8a 2021-03-11 op if (face != 0)
1139 d3d56d8a 2021-03-11 op wattron(body, face);
1140 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1141 d3d56d8a 2021-03-11 op if (face != 0)
1142 d3d56d8a 2021-03-11 op wattroff(body, face);
1146 8af5e5ed 2021-03-08 op redraw_tabline(void)
1148 7c7d7bb7 2021-03-10 op struct tab *tab;
1149 a636f50e 2021-03-16 op size_t toskip;
1150 a636f50e 2021-03-16 op int current, x, y, truncated;
1151 dc5df781 2021-03-13 op const char *title;
1152 119f393c 2021-03-16 op char buf[25];
1156 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1157 a636f50e 2021-03-16 op x += sizeof(buf) + 1;
1159 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1162 a636f50e 2021-03-16 op if (x < COLS-2)
1167 a636f50e 2021-03-16 op werase(tabline);
1168 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1169 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1170 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1172 a636f50e 2021-03-16 op truncated = 0;
1173 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1174 a636f50e 2021-03-16 op if (truncated)
1176 a636f50e 2021-03-16 op if (toskip != 0) {
1181 a636f50e 2021-03-16 op getyx(tabline, y, x);
1182 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1183 a636f50e 2021-03-16 op truncated = 1;
1185 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1187 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1188 2051e653 2021-03-13 op title = tab->hist_cur->h;
1190 119f393c 2021-03-16 op strlcpy(buf, " ", sizeof(buf));
1191 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1192 119f393c 2021-03-16 op /* truncation happens */
1193 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1195 119f393c 2021-03-16 op /* pad with spaces */
1196 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1200 a329982b 2021-03-11 op if (current)
1201 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1203 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1205 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1206 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1207 119f393c 2021-03-16 op wprintw(tabline, " ");
1209 cb6c7aa0 2021-03-16 op if (current)
1210 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1212 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1215 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1216 119f393c 2021-03-16 op for (; x < COLS; ++x)
1217 119f393c 2021-03-16 op waddch(tabline, ' ');
1218 a636f50e 2021-03-16 op if (truncated)
1219 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1223 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1226 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1227 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1228 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1230 156f1501 2021-03-13 op werase(modeline);
1231 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1232 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1234 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1235 58c75fab 2021-03-16 op spin[tab->s.loading_anim_step],
1236 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1238 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1240 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1241 481340cc 2021-03-11 op wprintw(modeline, "All ");
1242 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1243 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1244 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1245 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1247 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1249 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1250 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1251 65d9b3ca 2021-03-14 op tab->s.line_max,
1252 2051e653 2021-03-13 op tab->hist_cur->h);
1254 48e9d457 2021-03-06 op getyx(modeline, y, x);
1255 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1258 48e9d457 2021-03-06 op (void)max_y;
1260 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1261 48e9d457 2021-03-06 op waddstr(modeline, "-");
1265 9ca15951 2021-03-09 op redraw_minibuffer(void)
1267 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1269 156f1501 2021-03-13 op werase(minibuf);
1270 22268e11 2021-03-11 op if (in_minibuffer) {
1271 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1272 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1273 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1274 22268e11 2021-03-11 op ministate.hist_off + 1,
1275 22268e11 2021-03-11 op ministate.history->len);
1277 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1279 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1280 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1283 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1284 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1286 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1289 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1290 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1291 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1293 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1296 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1297 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1299 2aaf475e 2021-03-13 op if (in_minibuffer)
1300 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1304 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1306 e19f9a04 2021-03-11 op redraw_tabline();
1307 e19f9a04 2021-03-11 op redraw_body(tab);
1308 e19f9a04 2021-03-11 op redraw_modeline(tab);
1309 e19f9a04 2021-03-11 op redraw_minibuffer();
1311 e19f9a04 2021-03-11 op restore_cursor(tab);
1312 e19f9a04 2021-03-11 op wrefresh(tabline);
1313 e19f9a04 2021-03-11 op wrefresh(modeline);
1315 e19f9a04 2021-03-11 op if (in_minibuffer) {
1316 e19f9a04 2021-03-11 op wrefresh(body);
1317 e19f9a04 2021-03-11 op wrefresh(minibuf);
1319 e19f9a04 2021-03-11 op wrefresh(minibuf);
1320 e19f9a04 2021-03-11 op wrefresh(body);
1325 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1327 9a25f829 2021-03-14 op struct vline *vl;
1330 48e9d457 2021-03-06 op werase(body);
1332 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1333 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1337 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1338 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1339 48e9d457 2021-03-06 op wmove(body, line, 0);
1340 9a25f829 2021-03-14 op print_vline(vl);
1342 48e9d457 2021-03-06 op if (line == body_lines)
1348 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1350 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1351 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1352 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1353 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1355 bf992581 2021-03-12 op free(ministate.curmesg);
1357 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1358 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1359 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1361 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1362 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1363 9cb0f9ce 2021-03-10 op wrefresh(body);
1364 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1366 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1367 9cb0f9ce 2021-03-10 op wrefresh(body);
1372 740f578b 2021-03-15 op message(const char *fmt, ...)
1376 740f578b 2021-03-15 op va_start(ap, fmt);
1377 740f578b 2021-03-15 op vmessage(fmt, ap);
1382 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1384 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1386 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1387 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1388 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1392 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1394 8af5e5ed 2021-03-08 op struct tab *tab = d;
1396 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1398 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1399 6347dcd0 2021-03-16 op redraw_modeline(tab);
1400 6347dcd0 2021-03-16 op wrefresh(modeline);
1401 6347dcd0 2021-03-16 op wrefresh(body);
1402 6347dcd0 2021-03-16 op if (in_minibuffer)
1403 6347dcd0 2021-03-16 op wrefresh(minibuf);
1406 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1410 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1412 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1414 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1415 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1416 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1418 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1420 43a1b8d0 2021-03-09 op wrefresh(modeline);
1421 43a1b8d0 2021-03-09 op wrefresh(body);
1422 9ca15951 2021-03-09 op if (in_minibuffer)
1423 9ca15951 2021-03-09 op wrefresh(minibuf);
1427 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1429 43a1b8d0 2021-03-09 op empty_vlist(tab);
1430 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1431 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1432 8af5e5ed 2021-03-08 op load_url(tab, url);
1434 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1435 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1436 43a1b8d0 2021-03-09 op redraw_tab(tab);
1440 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1441 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1443 9ca15951 2021-03-09 op in_minibuffer = 1;
1444 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1445 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1447 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1449 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1450 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1451 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1452 9ca15951 2021-03-09 op ministate.off = 0;
1453 bb4e4ddf 2021-03-12 op ministate.len = 0;
1454 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1456 22268e11 2021-03-11 op ministate.history = hist;
1457 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1458 22268e11 2021-03-11 op ministate.hist_off = 0;
1462 b360ebb3 2021-03-10 op exit_minibuffer(void)
1464 156f1501 2021-03-13 op werase(minibuf);
1466 9ca15951 2021-03-09 op in_minibuffer = 0;
1467 9ca15951 2021-03-09 op base_map = &global_map;
1468 9ca15951 2021-03-09 op current_map = &global_map;
1472 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1474 5cd2ebb1 2021-03-11 op struct tab *t;
1476 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1477 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1480 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1483 b1df9b71 2021-03-12 op static struct tab *
1484 1b81bc33 2021-03-15 op new_tab(const char *url)
1486 754622a2 2021-03-15 op struct tab *tab;
1488 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1491 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1493 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1495 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1496 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1498 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1499 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1501 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1503 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1507 bcb0b073 2021-03-07 op event_loopbreak();
1508 b1df9b71 2021-03-12 op return NULL;
1512 5e11c00c 2021-03-02 op ui_init(void)
1514 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1516 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1517 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1519 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1521 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1522 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1523 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1525 9ca15951 2021-03-09 op base_map = &global_map;
1526 f832146f 2021-03-09 op current_map = &global_map;
1527 f832146f 2021-03-09 op load_default_keys();
1534 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1536 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1538 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1540 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1542 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1545 48e9d457 2021-03-06 op body_lines = LINES-3;
1546 48e9d457 2021-03-06 op body_cols = COLS;
1548 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1549 48e9d457 2021-03-06 op scrollok(body, TRUE);
1551 5e11c00c 2021-03-02 op /* non-blocking input */
1552 48e9d457 2021-03-06 op wtimeout(body, 0);
1554 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1556 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1557 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1559 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1560 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1562 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
1568 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1570 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1571 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1575 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1577 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1580 1d08c280 2021-03-06 op wrap_page(tab);
1581 5e11c00c 2021-03-02 op redraw_tab(tab);
1585 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1587 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1588 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1590 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1591 22268e11 2021-03-11 op &ir_history);
1592 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1593 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1594 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1598 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1602 740f578b 2021-03-15 op va_start(ap, fmt);
1603 740f578b 2021-03-15 op vmessage(fmt, ap);
1608 5e11c00c 2021-03-02 op ui_end(void)