2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
20 1d08c280 2021-03-06 op * Text scrolling
21 1d08c280 2021-03-06 op * ==============
23 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
24 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
25 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
26 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
27 1d08c280 2021-03-06 op * first/last line!
29 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
30 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
31 1d08c280 2021-03-06 op * ``on-demand''.
33 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
37 5e11c00c 2021-03-02 op #include <telescope.h>
39 f832146f 2021-03-09 op #include <ctype.h>
40 5e11c00c 2021-03-02 op #include <curses.h>
41 5e11c00c 2021-03-02 op #include <event.h>
42 5e11c00c 2021-03-02 op #include <locale.h>
43 5e11c00c 2021-03-02 op #include <signal.h>
44 7953dd72 2021-03-07 op #include <stdarg.h>
45 eb259e66 2021-03-02 op #include <stdlib.h>
46 eb259e66 2021-03-02 op #include <string.h>
47 f832146f 2021-03-09 op #include <unistd.h>
49 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
51 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
53 f832146f 2021-03-09 op static void load_default_keys(void);
54 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
55 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
57 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
58 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
59 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
60 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
61 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
62 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
63 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
64 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
65 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
66 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
67 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
68 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
69 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
70 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
71 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
72 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
73 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
74 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
75 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
76 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
77 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
78 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
79 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
80 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
81 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
82 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
83 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
85 9ca15951 2021-03-09 op static void global_key_unbound(void);
87 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
88 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
89 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
90 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
91 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
92 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
93 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
94 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
95 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
96 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
97 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
99 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
100 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
101 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
102 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
103 b360ebb3 2021-03-10 op static void eecmd_select(void);
104 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
105 5cd2ebb1 2021-03-11 op static void ir_select(void);
106 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
107 5cd2ebb1 2021-03-11 op static void lu_select(void);
108 740f578b 2021-03-15 op static void bp_select(void);
110 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
111 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
112 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
113 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
114 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
115 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
116 9a25f829 2021-03-14 op static void print_vline(struct vline*);
117 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
118 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
119 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
120 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
121 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
122 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
123 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
124 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
125 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
126 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
127 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
128 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
129 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
130 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
131 b1df9b71 2021-03-12 op static struct tab *new_tab(void);
133 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
135 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
136 48e9d457 2021-03-06 op static int body_lines, body_cols;
138 48e9d457 2021-03-06 op static struct event clminibufev;
139 48e9d457 2021-03-06 op static int clminibufev_set;
140 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
141 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
143 bcb0b073 2021-03-07 op static uint32_t tab_counter;
145 7c7d7bb7 2021-03-10 op static char keybuf[64];
147 9ca15951 2021-03-09 op struct kmap global_map,
148 fa3fd864 2021-03-10 op minibuffer_map,
149 9ca15951 2021-03-09 op *current_map,
152 3148eeac 2021-03-13 op static struct histhead eecmd_history,
156 9ca15951 2021-03-09 op static int in_minibuffer;
158 9ca15951 2021-03-09 op static struct {
159 9cb0f9ce 2021-03-10 op char *curmesg;
161 040fbdf8 2021-03-10 op char buf[1025];
162 9ca15951 2021-03-09 op size_t off, len;
163 5cd2ebb1 2021-03-11 op char prompt[32];
164 b360ebb3 2021-03-10 op void (*donefn)(void);
165 b360ebb3 2021-03-10 op void (*abortfn)(void);
167 3148eeac 2021-03-13 op struct histhead *history;
168 3148eeac 2021-03-13 op struct hist *hist_cur;
169 22268e11 2021-03-11 op size_t hist_off;
172 f95d82dd 2021-03-11 op struct lineprefix {
173 f95d82dd 2021-03-11 op const char *prfx1;
174 f95d82dd 2021-03-11 op const char *prfx2;
175 f95d82dd 2021-03-11 op } line_prefixes[] = {
176 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
177 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
178 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
179 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
180 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
181 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
182 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
183 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
184 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
185 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
188 d3d56d8a 2021-03-11 op struct line_face {
190 d3d56d8a 2021-03-11 op } line_faces[] = {
191 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
192 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
193 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
194 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
195 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
196 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
197 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
198 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
199 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
200 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
204 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
206 65d9b3ca 2021-03-14 op struct vline *vl, *t;
208 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
210 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
211 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
212 65d9b3ca 2021-03-14 op free(vl->line);
217 f832146f 2021-03-09 op static inline void
218 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
220 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
224 9ca15951 2021-03-09 op static inline void
225 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
227 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
232 f832146f 2021-03-09 op load_default_keys(void)
234 9ca15951 2021-03-09 op /* === global map === */
237 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
238 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
239 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
240 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
241 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
242 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
244 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
245 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
246 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
247 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
249 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
251 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
253 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
254 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
255 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
257 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
258 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
259 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
260 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
262 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
263 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
265 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
266 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
268 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
270 f832146f 2021-03-09 op /* vi/vi-like */
271 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
272 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
273 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
274 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
275 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
276 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
278 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
279 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
281 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
282 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
284 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
285 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
288 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
290 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
292 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
295 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
296 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
297 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
298 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
299 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
300 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
302 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
303 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
305 f832146f 2021-03-09 op /* "ncurses standard" */
306 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
309 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
310 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
312 fa3fd864 2021-03-10 op /* === minibuffer map === */
313 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
314 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
315 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
316 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
317 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
319 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
320 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
321 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
322 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
323 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
324 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
325 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
326 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
327 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
329 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
330 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
331 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
332 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
336 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
338 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
342 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
344 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
345 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
346 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
349 48e9d457 2021-03-06 op restore_cursor(tab);
353 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
355 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
358 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
359 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
360 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
363 48e9d457 2021-03-06 op restore_cursor(tab);
367 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
369 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
370 48e9d457 2021-03-06 op restore_cursor(tab);
374 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
376 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
377 a329982b 2021-03-11 op restore_cursor(tab);
381 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
383 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
384 a329982b 2021-03-11 op restore_cursor(tab);
388 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
390 9a25f829 2021-03-14 op struct vline *vl;
392 f95d82dd 2021-03-11 op const char *prfx;
394 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
395 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
396 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
400 9a25f829 2021-03-14 op vl = nth_line(tab, off);
401 9a25f829 2021-03-14 op if (vl->line != NULL)
402 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
404 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
406 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
407 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
410 48e9d457 2021-03-06 op restore_cursor(tab);
414 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
416 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
420 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
422 9a25f829 2021-03-14 op struct vline *vl;
424 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
427 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
428 48e9d457 2021-03-06 op wscrl(body, -1);
429 48e9d457 2021-03-06 op wmove(body, 0, 0);
430 9a25f829 2021-03-14 op print_vline(vl);
434 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
436 9a25f829 2021-03-14 op struct vline *vl;
438 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
441 65d9b3ca 2021-03-14 op tab->s.line_off++;
442 48e9d457 2021-03-06 op wscrl(body, 1);
444 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
447 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
448 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
449 9a25f829 2021-03-14 op print_vline(vl);
453 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
457 ed44414d 2021-03-09 op off = body_lines+1;
459 ed44414d 2021-03-09 op for (; off > 0; --off)
460 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
464 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
468 338eecdc 2021-03-12 op off = body_lines+1;
470 338eecdc 2021-03-12 op for (; off > 0; --off)
471 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
475 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
477 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
478 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
479 e19f9a04 2021-03-11 op redraw_body(tab);
483 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
487 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
488 e19f9a04 2021-03-11 op off = MAX(0, off);
490 65d9b3ca 2021-03-14 op tab->s.line_off = off;
491 754622a2 2021-03-15 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
493 e19f9a04 2021-03-11 op redraw_body(tab);
497 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
499 1d08c280 2021-03-06 op event_loopbreak();
503 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
505 9a25f829 2021-03-14 op struct vline *vl;
508 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
509 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
511 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
512 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
515 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
519 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
521 b1df9b71 2021-03-12 op struct tab *t;
522 9a25f829 2021-03-14 op struct vline *vl;
525 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
526 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
528 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
529 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
532 b1df9b71 2021-03-12 op t = new_tab();
533 b1df9b71 2021-03-12 op memcpy(&t->url, &tab->url, sizeof(tab->url));
534 9a25f829 2021-03-14 op load_url_in_tab(t, vl->parent->alt);
538 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
540 2051e653 2021-03-13 op if (!load_previous_page(tab))
541 2051e653 2021-03-13 op message("No previous page");
543 c40c2250 2021-03-14 op start_loading_anim(tab);
547 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
549 2051e653 2021-03-13 op if (!load_next_page(tab))
550 2051e653 2021-03-13 op message("No next page");
552 c40c2250 2021-03-14 op start_loading_anim(tab);
556 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
558 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
562 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
566 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
567 22268e11 2021-03-11 op &eecmd_history);
569 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
570 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
572 9ca15951 2021-03-09 op if (thiskey.meta)
573 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
575 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
576 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
580 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
582 7c7d7bb7 2021-03-10 op struct tab *t;
584 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
585 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
586 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
590 7c7d7bb7 2021-03-10 op stop_tab(tab);
592 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
593 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
595 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
601 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
607 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
609 7c7d7bb7 2021-03-10 op struct tab *t;
611 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
613 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
614 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
615 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
619 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
621 7c7d7bb7 2021-03-10 op struct tab *t;
623 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
625 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
626 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
627 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
631 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
633 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
634 22268e11 2021-03-11 op &lu_history);
635 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
639 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
641 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
642 22268e11 2021-03-11 op &lu_history);
643 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
644 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
645 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
646 740f578b 2021-03-15 op ministate.len = ministate.off;
650 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
652 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
653 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
654 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
655 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
656 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
660 7c7d7bb7 2021-03-10 op global_key_unbound(void)
662 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
666 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
668 3eecee9e 2021-03-12 op minibuffer_taint_hist();
670 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
673 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
674 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
675 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
676 d67133bf 2021-03-12 op ministate.len--;
680 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
682 3eecee9e 2021-03-12 op minibuffer_taint_hist();
684 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
687 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
688 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
689 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
690 9ca15951 2021-03-09 op ministate.off--;
691 9ca15951 2021-03-09 op ministate.len--;
695 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
697 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
699 9ca15951 2021-03-09 op ministate.off++;
703 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
705 9ca15951 2021-03-09 op if (ministate.off == 0)
707 9ca15951 2021-03-09 op ministate.off--;
711 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
713 9ca15951 2021-03-09 op ministate.off = ministate.len;
717 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
719 9ca15951 2021-03-09 op ministate.off = 0;
723 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
725 3eecee9e 2021-03-12 op minibuffer_taint_hist();
727 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
729 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
730 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
734 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
736 b360ebb3 2021-03-10 op ministate.abortfn();
740 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
742 22268e11 2021-03-11 op minibuffer_taint_hist();
743 b360ebb3 2021-03-10 op ministate.donefn();
747 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
749 22268e11 2021-03-11 op if (ministate.history == NULL) {
750 22268e11 2021-03-11 op message("No history");
754 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
755 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
756 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
757 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
758 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
759 22268e11 2021-03-11 op message("No prev item");
761 22268e11 2021-03-11 op ministate.hist_off--;
764 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
765 22268e11 2021-03-11 op ministate.off = 0;
766 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
771 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
773 22268e11 2021-03-11 op if (ministate.history == NULL) {
774 22268e11 2021-03-11 op message("No history");
778 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
779 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
780 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
781 22268e11 2021-03-11 op ministate.hist_off = 0;
782 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
783 22268e11 2021-03-11 op message("No next item");
785 22268e11 2021-03-11 op ministate.hist_off++;
788 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
789 22268e11 2021-03-11 op ministate.off = 0;
790 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
795 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
797 3148eeac 2021-03-13 op struct hist *hist;
799 22268e11 2021-03-11 op if (ministate.history == NULL)
802 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
805 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
807 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
808 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
810 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
811 22268e11 2021-03-11 op ministate.history->len++;
815 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
816 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
817 22268e11 2021-03-11 op * navigation" thing.
820 22268e11 2021-03-11 op minibuffer_taint_hist(void)
822 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
825 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
826 22268e11 2021-03-11 op ministate.hist_cur = NULL;
830 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
832 22268e11 2021-03-11 op minibuffer_taint_hist();
834 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
837 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
839 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
840 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
841 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
842 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
843 9ca15951 2021-03-09 op ministate.off++;
844 9ca15951 2021-03-09 op ministate.len++;
848 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
850 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
851 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
852 5cd2ebb1 2021-03-11 op global_key_unbound();
856 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
860 b360ebb3 2021-03-10 op eecmd_select(void)
862 b360ebb3 2021-03-10 op exit_minibuffer();
863 22268e11 2021-03-11 op minibuffer_hist_save_entry();
864 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
868 5cd2ebb1 2021-03-11 op ir_self_insert(void)
870 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
874 5cd2ebb1 2021-03-11 op ir_select(void)
876 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
877 5cd2ebb1 2021-03-11 op struct url url;
878 5cd2ebb1 2021-03-11 op struct tab *tab;
880 5cd2ebb1 2021-03-11 op tab = current_tab();
882 5cd2ebb1 2021-03-11 op exit_minibuffer();
883 22268e11 2021-03-11 op minibuffer_hist_save_entry();
885 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
886 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
887 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
888 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
889 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
893 5cd2ebb1 2021-03-11 op lu_self_insert(void)
895 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
896 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
897 5cd2ebb1 2021-03-11 op global_key_unbound();
901 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
905 5cd2ebb1 2021-03-11 op lu_select(void)
907 5cd2ebb1 2021-03-11 op exit_minibuffer();
908 22268e11 2021-03-11 op minibuffer_hist_save_entry();
909 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
913 740f578b 2021-03-15 op bp_select(void)
915 740f578b 2021-03-15 op exit_minibuffer();
916 740f578b 2021-03-15 op if (*ministate.buf != '\0')
917 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
919 740f578b 2021-03-15 op message("Abort.");
922 9a25f829 2021-03-14 op static struct vline *
923 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
925 9a25f829 2021-03-14 op struct vline *vl;
929 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
935 48e9d457 2021-03-06 op /* unreachable */
939 5e11c00c 2021-03-02 op static struct tab *
940 5e11c00c 2021-03-02 op current_tab(void)
942 5e11c00c 2021-03-02 op struct tab *t;
944 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
945 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
949 5e11c00c 2021-03-02 op /* unreachable */
954 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
956 f832146f 2021-03-09 op struct keymap *k;
957 7c7d7bb7 2021-03-10 op const char *keyname;
958 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
960 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
961 9ca15951 2021-03-09 op if (thiskey.key == ERR)
963 9ca15951 2021-03-09 op if (thiskey.key == 27) {
964 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
966 9ca15951 2021-03-09 op thiskey.meta = 1;
967 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
968 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
969 c314a314 2021-03-11 op thiskey.meta = 0;
970 9ca15951 2021-03-09 op thiskey.key = 27;
973 9ca15951 2021-03-09 op thiskey.meta = 0;
975 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
976 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
977 7c7d7bb7 2021-03-10 op if (thiskey.meta)
978 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
979 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
980 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
982 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
983 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
986 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
987 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
988 9ca15951 2021-03-09 op k->key == thiskey.key) {
989 f832146f 2021-03-09 op if (k->fn == NULL)
990 f832146f 2021-03-09 op current_map = &k->map;
992 9ca15951 2021-03-09 op current_map = base_map;
993 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
994 f832146f 2021-03-09 op k->fn(current_tab());
1000 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1001 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1003 7c7d7bb7 2021-03-10 op global_key_unbound();
1006 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1007 9ca15951 2021-03-09 op current_map = base_map;
1010 179f0f58 2021-03-11 op redraw_tab(current_tab());
1014 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1016 a6d450c1 2021-03-06 op clminibufev_set = 0;
1018 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1019 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1021 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1022 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1023 9cb0f9ce 2021-03-10 op wrefresh(body);
1024 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1026 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1027 9cb0f9ce 2021-03-10 op wrefresh(body);
1032 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1034 1d08c280 2021-03-06 op struct tab *tab;
1040 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1042 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1043 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1045 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1046 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1048 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1049 48e9d457 2021-03-06 op body_lines = LINES-3;
1050 bd9637e9 2021-03-06 op body_cols = COLS;
1052 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1054 1d08c280 2021-03-06 op tab = current_tab();
1056 1d08c280 2021-03-06 op wrap_page(tab);
1057 1d08c280 2021-03-06 op redraw_tab(tab);
1061 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1063 1d08c280 2021-03-06 op struct line *l;
1064 0d568960 2021-03-11 op const char *prfx;
1066 1d08c280 2021-03-06 op empty_vlist(tab);
1068 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1069 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1070 5e11c00c 2021-03-02 op switch (l->type) {
1071 5e11c00c 2021-03-02 op case LINE_TEXT:
1072 5e11c00c 2021-03-02 op case LINE_LINK:
1073 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1074 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1075 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1076 5e11c00c 2021-03-02 op case LINE_ITEM:
1077 5e11c00c 2021-03-02 op case LINE_QUOTE:
1078 5e11c00c 2021-03-02 op case LINE_PRE_START:
1079 5e11c00c 2021-03-02 op case LINE_PRE_END:
1080 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1081 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1083 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1084 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1092 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1094 9a25f829 2021-03-14 op const char *text = vl->line;
1095 768db5bb 2021-03-12 op const char *prfx;
1096 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1098 9a25f829 2021-03-14 op if (!vl->flags)
1099 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1101 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1103 bd9637e9 2021-03-06 op if (text == NULL)
1106 d3d56d8a 2021-03-11 op if (face != 0)
1107 d3d56d8a 2021-03-11 op wattron(body, face);
1108 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1109 d3d56d8a 2021-03-11 op if (face != 0)
1110 d3d56d8a 2021-03-11 op wattroff(body, face);
1114 8af5e5ed 2021-03-08 op redraw_tabline(void)
1116 7c7d7bb7 2021-03-10 op struct tab *tab;
1117 7c7d7bb7 2021-03-10 op int current;
1118 dc5df781 2021-03-13 op const char *title;
1120 156f1501 2021-03-13 op werase(tabline);
1121 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1123 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1124 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1125 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1127 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1128 2051e653 2021-03-13 op title = tab->hist_cur->h;
1130 a329982b 2021-03-11 op if (current)
1131 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1133 dc5df781 2021-03-13 op wprintw(tabline, "%s%d: %s",
1134 dc5df781 2021-03-13 op current ? "*" : " ", tab->id, title);
1136 a329982b 2021-03-11 op if (current)
1137 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1142 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1145 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1146 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1147 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1149 156f1501 2021-03-13 op werase(modeline);
1150 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1151 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1153 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1154 65d9b3ca 2021-03-14 op spin[tab->s.loading_anim_step], mode);
1156 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1158 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1159 481340cc 2021-03-11 op wprintw(modeline, "All ");
1160 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1161 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1162 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1163 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1165 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1167 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1168 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1169 65d9b3ca 2021-03-14 op tab->s.line_max,
1170 2051e653 2021-03-13 op tab->hist_cur->h);
1172 48e9d457 2021-03-06 op getyx(modeline, y, x);
1173 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1176 48e9d457 2021-03-06 op (void)max_y;
1178 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1179 48e9d457 2021-03-06 op waddstr(modeline, "-");
1183 9ca15951 2021-03-09 op redraw_minibuffer(void)
1185 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1187 156f1501 2021-03-13 op werase(minibuf);
1188 22268e11 2021-03-11 op if (in_minibuffer) {
1189 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1190 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1191 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1192 22268e11 2021-03-11 op ministate.hist_off + 1,
1193 22268e11 2021-03-11 op ministate.history->len);
1195 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1197 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1198 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1201 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1202 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1204 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1207 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1208 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1209 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1211 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1214 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1215 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1217 2aaf475e 2021-03-13 op if (in_minibuffer)
1218 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1222 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1224 e19f9a04 2021-03-11 op redraw_tabline();
1225 e19f9a04 2021-03-11 op redraw_body(tab);
1226 e19f9a04 2021-03-11 op redraw_modeline(tab);
1227 e19f9a04 2021-03-11 op redraw_minibuffer();
1229 e19f9a04 2021-03-11 op restore_cursor(tab);
1230 e19f9a04 2021-03-11 op wrefresh(tabline);
1231 e19f9a04 2021-03-11 op wrefresh(modeline);
1233 e19f9a04 2021-03-11 op if (in_minibuffer) {
1234 e19f9a04 2021-03-11 op wrefresh(body);
1235 e19f9a04 2021-03-11 op wrefresh(minibuf);
1237 e19f9a04 2021-03-11 op wrefresh(minibuf);
1238 e19f9a04 2021-03-11 op wrefresh(body);
1243 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1245 9a25f829 2021-03-14 op struct vline *vl;
1248 48e9d457 2021-03-06 op werase(body);
1250 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1251 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1255 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1256 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1257 48e9d457 2021-03-06 op wmove(body, line, 0);
1258 9a25f829 2021-03-14 op print_vline(vl);
1260 48e9d457 2021-03-06 op if (line == body_lines)
1266 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1268 7953dd72 2021-03-07 op if (clminibufev_set)
1269 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1270 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1271 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1272 7953dd72 2021-03-07 op clminibufev_set = 1;
1274 bf992581 2021-03-12 op free(ministate.curmesg);
1276 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1277 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1278 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1280 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1281 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1282 9cb0f9ce 2021-03-10 op wrefresh(body);
1283 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1285 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1286 9cb0f9ce 2021-03-10 op wrefresh(body);
1291 740f578b 2021-03-15 op message(const char *fmt, ...)
1295 740f578b 2021-03-15 op va_start(ap, fmt);
1296 740f578b 2021-03-15 op vmessage(fmt, ap);
1301 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1303 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1305 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1306 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1307 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1311 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1313 8af5e5ed 2021-03-08 op struct tab *tab = d;
1315 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1317 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1318 8af5e5ed 2021-03-08 op wrefresh(modeline);
1320 8af5e5ed 2021-03-08 op wrefresh(body);
1321 9ca15951 2021-03-09 op if (in_minibuffer)
1322 9ca15951 2021-03-09 op wrefresh(minibuf);
1324 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1328 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1330 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1332 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1333 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1334 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1336 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1338 43a1b8d0 2021-03-09 op wrefresh(modeline);
1339 43a1b8d0 2021-03-09 op wrefresh(body);
1340 9ca15951 2021-03-09 op if (in_minibuffer)
1341 9ca15951 2021-03-09 op wrefresh(minibuf);
1345 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1347 43a1b8d0 2021-03-09 op empty_vlist(tab);
1348 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1349 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1350 8af5e5ed 2021-03-08 op load_url(tab, url);
1352 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1353 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1354 43a1b8d0 2021-03-09 op redraw_tab(tab);
1358 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1359 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1361 9ca15951 2021-03-09 op in_minibuffer = 1;
1362 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1363 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1365 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1367 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1368 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1369 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1370 9ca15951 2021-03-09 op ministate.off = 0;
1371 bb4e4ddf 2021-03-12 op ministate.len = 0;
1372 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1374 22268e11 2021-03-11 op ministate.history = hist;
1375 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1376 22268e11 2021-03-11 op ministate.hist_off = 0;
1380 b360ebb3 2021-03-10 op exit_minibuffer(void)
1382 156f1501 2021-03-13 op werase(minibuf);
1384 9ca15951 2021-03-09 op in_minibuffer = 0;
1385 9ca15951 2021-03-09 op base_map = &global_map;
1386 9ca15951 2021-03-09 op current_map = &global_map;
1390 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1392 5cd2ebb1 2021-03-11 op struct tab *t;
1394 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1395 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1398 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1401 b1df9b71 2021-03-12 op static struct tab *
1402 bcb0b073 2021-03-07 op new_tab(void)
1404 754622a2 2021-03-15 op struct tab *tab;
1405 bcb0b073 2021-03-07 op const char *url = "about:new";
1407 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1410 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1412 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1414 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1415 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1417 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1418 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1420 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1422 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1426 bcb0b073 2021-03-07 op event_loopbreak();
1427 b1df9b71 2021-03-12 op return NULL;
1431 5e11c00c 2021-03-02 op ui_init(void)
1433 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1435 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1436 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1438 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1440 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1441 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1442 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1444 9ca15951 2021-03-09 op base_map = &global_map;
1445 f832146f 2021-03-09 op current_map = &global_map;
1446 f832146f 2021-03-09 op load_default_keys();
1453 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1455 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1457 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1459 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1461 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1464 48e9d457 2021-03-06 op body_lines = LINES-3;
1465 48e9d457 2021-03-06 op body_cols = COLS;
1467 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1468 48e9d457 2021-03-06 op scrollok(body, TRUE);
1470 5e11c00c 2021-03-02 op /* non-blocking input */
1471 48e9d457 2021-03-06 op wtimeout(body, 0);
1473 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1475 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1476 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1478 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1479 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1487 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1489 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1490 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1494 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1496 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1499 1d08c280 2021-03-06 op wrap_page(tab);
1500 5e11c00c 2021-03-02 op redraw_tab(tab);
1504 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1506 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1507 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1509 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1510 22268e11 2021-03-11 op &ir_history);
1511 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1512 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1513 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1517 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1521 740f578b 2021-03-15 op va_start(ap, fmt);
1522 740f578b 2021-03-15 op vmessage(fmt, ap);
1527 5e11c00c 2021-03-02 op ui_end(void)