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*);
84 9ca15951 2021-03-09 op static void global_key_unbound(void);
86 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
87 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
88 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
89 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
90 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
91 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
92 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
93 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
94 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
95 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
96 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
98 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
99 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
100 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
101 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
102 b360ebb3 2021-03-10 op static void eecmd_select(void);
103 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
104 5cd2ebb1 2021-03-11 op static void ir_select(void);
105 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
106 5cd2ebb1 2021-03-11 op static void lu_select(void);
108 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
109 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
110 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
111 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
112 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
113 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
114 9a25f829 2021-03-14 op static void print_vline(struct vline*);
115 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
116 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
117 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
118 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
119 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
120 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
121 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
122 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
123 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
124 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
125 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
126 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
127 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
128 b1df9b71 2021-03-12 op static struct tab *new_tab(void);
130 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
132 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
133 48e9d457 2021-03-06 op static int body_lines, body_cols;
135 48e9d457 2021-03-06 op static struct event clminibufev;
136 48e9d457 2021-03-06 op static int clminibufev_set;
137 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
138 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
140 bcb0b073 2021-03-07 op static uint32_t tab_counter;
142 7c7d7bb7 2021-03-10 op static char keybuf[64];
144 9ca15951 2021-03-09 op struct kmap global_map,
145 fa3fd864 2021-03-10 op minibuffer_map,
146 9ca15951 2021-03-09 op *current_map,
149 3148eeac 2021-03-13 op static struct histhead eecmd_history,
153 9ca15951 2021-03-09 op static int in_minibuffer;
155 9ca15951 2021-03-09 op static struct {
156 9cb0f9ce 2021-03-10 op char *curmesg;
158 040fbdf8 2021-03-10 op char buf[1025];
159 9ca15951 2021-03-09 op size_t off, len;
160 5cd2ebb1 2021-03-11 op char prompt[32];
161 b360ebb3 2021-03-10 op void (*donefn)(void);
162 b360ebb3 2021-03-10 op void (*abortfn)(void);
164 3148eeac 2021-03-13 op struct histhead *history;
165 3148eeac 2021-03-13 op struct hist *hist_cur;
166 22268e11 2021-03-11 op size_t hist_off;
169 f95d82dd 2021-03-11 op struct lineprefix {
170 f95d82dd 2021-03-11 op const char *prfx1;
171 f95d82dd 2021-03-11 op const char *prfx2;
172 f95d82dd 2021-03-11 op } line_prefixes[] = {
173 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
174 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
175 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
176 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
177 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
178 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
179 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
180 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
181 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
182 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
185 d3d56d8a 2021-03-11 op struct line_face {
187 d3d56d8a 2021-03-11 op } line_faces[] = {
188 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
189 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
190 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
191 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
192 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
193 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
194 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
195 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
196 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
197 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
201 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
203 65d9b3ca 2021-03-14 op struct vline *vl, *t;
205 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
207 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
208 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
209 65d9b3ca 2021-03-14 op free(vl->line);
214 f832146f 2021-03-09 op static inline void
215 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
217 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
221 9ca15951 2021-03-09 op static inline void
222 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
224 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
229 f832146f 2021-03-09 op load_default_keys(void)
231 9ca15951 2021-03-09 op /* === global map === */
234 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
235 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
236 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
237 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
238 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
239 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
241 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
242 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
243 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
244 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
246 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
248 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
250 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
251 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
252 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
254 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
255 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
256 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
257 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
259 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
260 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
262 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
263 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
265 f832146f 2021-03-09 op /* vi/vi-like */
266 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
267 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
268 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
269 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
270 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
271 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
273 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
274 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
276 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
277 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
279 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
280 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
283 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
285 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
287 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
290 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
291 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
292 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
293 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
294 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
295 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
297 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
298 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
300 f832146f 2021-03-09 op /* "ncurses standard" */
301 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
304 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
305 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
307 fa3fd864 2021-03-10 op /* === minibuffer map === */
308 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
309 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
310 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
311 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
312 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
314 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
315 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
316 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
317 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
318 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
319 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
320 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
321 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
322 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
324 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
325 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
326 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
327 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
331 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
333 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
337 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
339 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
340 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
341 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
344 48e9d457 2021-03-06 op restore_cursor(tab);
348 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
350 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
353 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
354 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
355 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
358 48e9d457 2021-03-06 op restore_cursor(tab);
362 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
364 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
365 48e9d457 2021-03-06 op restore_cursor(tab);
369 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
371 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
372 a329982b 2021-03-11 op restore_cursor(tab);
376 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
378 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
379 a329982b 2021-03-11 op restore_cursor(tab);
383 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
385 9a25f829 2021-03-14 op struct vline *vl;
387 f95d82dd 2021-03-11 op const char *prfx;
389 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
390 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
391 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
395 9a25f829 2021-03-14 op vl = nth_line(tab, off);
396 9a25f829 2021-03-14 op if (vl->line != NULL)
397 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
399 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
401 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
402 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
405 48e9d457 2021-03-06 op restore_cursor(tab);
409 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
411 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
415 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
417 9a25f829 2021-03-14 op struct vline *vl;
419 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
422 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
423 48e9d457 2021-03-06 op wscrl(body, -1);
424 48e9d457 2021-03-06 op wmove(body, 0, 0);
425 9a25f829 2021-03-14 op print_vline(vl);
429 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
431 9a25f829 2021-03-14 op struct vline *vl;
434 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
437 65d9b3ca 2021-03-14 op tab->s.line_off++;
438 48e9d457 2021-03-06 op wscrl(body, 1);
440 65d9b3ca 2021-03-14 op if (tab->s.line_max - tab->s.line_off < body_lines)
443 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
444 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
445 9a25f829 2021-03-14 op print_vline(vl);
449 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
453 ed44414d 2021-03-09 op off = body_lines+1;
455 ed44414d 2021-03-09 op for (; off > 0; --off)
456 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
460 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
464 338eecdc 2021-03-12 op off = body_lines+1;
466 338eecdc 2021-03-12 op for (; off > 0; --off)
467 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
471 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
473 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
474 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
475 e19f9a04 2021-03-11 op redraw_body(tab);
479 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
483 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
484 e19f9a04 2021-03-11 op off = MAX(0, off);
486 65d9b3ca 2021-03-14 op tab->s.line_off = off;
487 65d9b3ca 2021-03-14 op tab->s.curs_y = MIN(body_lines, tab->s.line_max);
489 e19f9a04 2021-03-11 op redraw_body(tab);
493 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
495 1d08c280 2021-03-06 op event_loopbreak();
499 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
501 9a25f829 2021-03-14 op struct vline *vl;
504 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
505 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
507 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
508 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
511 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
515 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
517 b1df9b71 2021-03-12 op struct tab *t;
518 9a25f829 2021-03-14 op struct vline *vl;
521 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
522 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
524 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
525 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
528 b1df9b71 2021-03-12 op t = new_tab();
529 b1df9b71 2021-03-12 op memcpy(&t->url, &tab->url, sizeof(tab->url));
530 9a25f829 2021-03-14 op load_url_in_tab(t, vl->parent->alt);
534 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
536 2051e653 2021-03-13 op if (!load_previous_page(tab))
537 2051e653 2021-03-13 op message("No previous page");
539 c40c2250 2021-03-14 op start_loading_anim(tab);
543 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
545 2051e653 2021-03-13 op if (!load_next_page(tab))
546 2051e653 2021-03-13 op message("No next page");
548 c40c2250 2021-03-14 op start_loading_anim(tab);
552 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
554 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
558 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
562 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
563 22268e11 2021-03-11 op &eecmd_history);
565 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
566 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
568 9ca15951 2021-03-09 op if (thiskey.meta)
569 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
571 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
572 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
576 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
578 7c7d7bb7 2021-03-10 op struct tab *t;
580 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
581 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
582 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
586 7c7d7bb7 2021-03-10 op stop_tab(tab);
588 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
589 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
591 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
597 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
603 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
605 7c7d7bb7 2021-03-10 op struct tab *t;
607 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
609 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
610 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
611 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
615 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
617 7c7d7bb7 2021-03-10 op struct tab *t;
619 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
621 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
622 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
623 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
627 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
629 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
630 22268e11 2021-03-11 op &lu_history);
631 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
635 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
637 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
638 22268e11 2021-03-11 op &lu_history);
639 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
640 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
641 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
642 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
646 7c7d7bb7 2021-03-10 op global_key_unbound(void)
648 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
652 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
654 3eecee9e 2021-03-12 op minibuffer_taint_hist();
656 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
659 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
660 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
661 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
662 d67133bf 2021-03-12 op ministate.len--;
666 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
668 3eecee9e 2021-03-12 op minibuffer_taint_hist();
670 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
673 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
674 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
675 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
676 9ca15951 2021-03-09 op ministate.off--;
677 9ca15951 2021-03-09 op ministate.len--;
681 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
683 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
685 9ca15951 2021-03-09 op ministate.off++;
689 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
691 9ca15951 2021-03-09 op if (ministate.off == 0)
693 9ca15951 2021-03-09 op ministate.off--;
697 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
699 9ca15951 2021-03-09 op ministate.off = ministate.len;
703 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
705 9ca15951 2021-03-09 op ministate.off = 0;
709 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
711 3eecee9e 2021-03-12 op minibuffer_taint_hist();
713 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
715 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
716 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
720 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
722 b360ebb3 2021-03-10 op ministate.abortfn();
726 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
728 22268e11 2021-03-11 op minibuffer_taint_hist();
729 b360ebb3 2021-03-10 op ministate.donefn();
733 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
735 22268e11 2021-03-11 op if (ministate.history == NULL) {
736 22268e11 2021-03-11 op message("No history");
740 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
741 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
742 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
743 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
744 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
745 22268e11 2021-03-11 op message("No prev item");
747 22268e11 2021-03-11 op ministate.hist_off--;
750 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
751 22268e11 2021-03-11 op ministate.off = 0;
752 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
757 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
759 22268e11 2021-03-11 op if (ministate.history == NULL) {
760 22268e11 2021-03-11 op message("No history");
764 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
765 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
766 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
767 22268e11 2021-03-11 op ministate.hist_off = 0;
768 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
769 22268e11 2021-03-11 op message("No next item");
771 22268e11 2021-03-11 op ministate.hist_off++;
774 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
775 22268e11 2021-03-11 op ministate.off = 0;
776 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
781 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
783 3148eeac 2021-03-13 op struct hist *hist;
785 22268e11 2021-03-11 op if (ministate.history == NULL)
788 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
791 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
793 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
794 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
796 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
797 22268e11 2021-03-11 op ministate.history->len++;
801 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
802 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
803 22268e11 2021-03-11 op * navigation" thing.
806 22268e11 2021-03-11 op minibuffer_taint_hist(void)
808 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
811 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
812 22268e11 2021-03-11 op ministate.hist_cur = NULL;
816 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
818 22268e11 2021-03-11 op minibuffer_taint_hist();
820 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
823 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
825 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
826 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
827 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
828 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
829 9ca15951 2021-03-09 op ministate.off++;
830 9ca15951 2021-03-09 op ministate.len++;
834 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
836 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
837 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
838 5cd2ebb1 2021-03-11 op global_key_unbound();
842 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
846 b360ebb3 2021-03-10 op eecmd_select(void)
848 b360ebb3 2021-03-10 op exit_minibuffer();
849 22268e11 2021-03-11 op minibuffer_hist_save_entry();
850 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
854 5cd2ebb1 2021-03-11 op ir_self_insert(void)
856 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
860 5cd2ebb1 2021-03-11 op ir_select(void)
862 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
863 5cd2ebb1 2021-03-11 op struct url url;
864 5cd2ebb1 2021-03-11 op struct tab *tab;
866 5cd2ebb1 2021-03-11 op tab = current_tab();
868 5cd2ebb1 2021-03-11 op exit_minibuffer();
869 22268e11 2021-03-11 op minibuffer_hist_save_entry();
871 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
872 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
873 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
874 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
875 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
879 5cd2ebb1 2021-03-11 op lu_self_insert(void)
881 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
882 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
883 5cd2ebb1 2021-03-11 op global_key_unbound();
887 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
891 5cd2ebb1 2021-03-11 op lu_select(void)
893 5cd2ebb1 2021-03-11 op exit_minibuffer();
894 22268e11 2021-03-11 op minibuffer_hist_save_entry();
895 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
898 9a25f829 2021-03-14 op static struct vline *
899 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
901 9a25f829 2021-03-14 op struct vline *vl;
905 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
911 48e9d457 2021-03-06 op /* unreachable */
915 5e11c00c 2021-03-02 op static struct tab *
916 5e11c00c 2021-03-02 op current_tab(void)
918 5e11c00c 2021-03-02 op struct tab *t;
920 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
921 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
925 5e11c00c 2021-03-02 op /* unreachable */
930 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
932 481340cc 2021-03-11 op struct tab *tab;
933 f832146f 2021-03-09 op struct keymap *k;
934 7c7d7bb7 2021-03-10 op const char *keyname;
935 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
937 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
938 9ca15951 2021-03-09 op if (thiskey.key == ERR)
940 9ca15951 2021-03-09 op if (thiskey.key == 27) {
941 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
943 9ca15951 2021-03-09 op thiskey.meta = 1;
944 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
945 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
946 c314a314 2021-03-11 op thiskey.meta = 0;
947 9ca15951 2021-03-09 op thiskey.key = 27;
950 9ca15951 2021-03-09 op thiskey.meta = 0;
952 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
953 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
954 7c7d7bb7 2021-03-10 op if (thiskey.meta)
955 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
956 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
957 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
959 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
960 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
963 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
964 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
965 9ca15951 2021-03-09 op k->key == thiskey.key) {
966 f832146f 2021-03-09 op if (k->fn == NULL)
967 f832146f 2021-03-09 op current_map = &k->map;
969 9ca15951 2021-03-09 op current_map = base_map;
970 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
971 f832146f 2021-03-09 op k->fn(current_tab());
977 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
978 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
980 7c7d7bb7 2021-03-10 op global_key_unbound();
983 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
984 9ca15951 2021-03-09 op current_map = base_map;
987 179f0f58 2021-03-11 op redraw_tab(current_tab());
991 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
993 a6d450c1 2021-03-06 op clminibufev_set = 0;
995 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
996 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
998 9cb0f9ce 2021-03-10 op redraw_minibuffer();
999 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1000 9cb0f9ce 2021-03-10 op wrefresh(body);
1001 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1003 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1004 9cb0f9ce 2021-03-10 op wrefresh(body);
1009 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1011 1d08c280 2021-03-06 op struct tab *tab;
1017 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1019 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1020 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1022 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1023 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1025 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1026 48e9d457 2021-03-06 op body_lines = LINES-3;
1027 bd9637e9 2021-03-06 op body_cols = COLS;
1029 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1031 1d08c280 2021-03-06 op tab = current_tab();
1033 1d08c280 2021-03-06 op wrap_page(tab);
1034 1d08c280 2021-03-06 op redraw_tab(tab);
1038 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1040 1d08c280 2021-03-06 op struct line *l;
1041 0d568960 2021-03-11 op const char *prfx;
1043 1d08c280 2021-03-06 op empty_vlist(tab);
1045 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1046 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1047 5e11c00c 2021-03-02 op switch (l->type) {
1048 5e11c00c 2021-03-02 op case LINE_TEXT:
1049 5e11c00c 2021-03-02 op case LINE_LINK:
1050 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1051 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1052 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1053 5e11c00c 2021-03-02 op case LINE_ITEM:
1054 5e11c00c 2021-03-02 op case LINE_QUOTE:
1055 5e11c00c 2021-03-02 op case LINE_PRE_START:
1056 5e11c00c 2021-03-02 op case LINE_PRE_END:
1057 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1058 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1060 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1061 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1068 1d08c280 2021-03-06 op static inline void
1069 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1071 9a25f829 2021-03-14 op const char *text = vl->line;
1072 768db5bb 2021-03-12 op const char *prfx;
1073 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1075 9a25f829 2021-03-14 op if (!vl->flags)
1076 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1078 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1080 bd9637e9 2021-03-06 op if (text == NULL)
1083 d3d56d8a 2021-03-11 op if (face != 0)
1084 d3d56d8a 2021-03-11 op wattron(body, face);
1085 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1086 d3d56d8a 2021-03-11 op if (face != 0)
1087 d3d56d8a 2021-03-11 op wattroff(body, face);
1091 8af5e5ed 2021-03-08 op redraw_tabline(void)
1093 7c7d7bb7 2021-03-10 op struct tab *tab;
1094 7c7d7bb7 2021-03-10 op int current;
1095 dc5df781 2021-03-13 op const char *title;
1097 156f1501 2021-03-13 op werase(tabline);
1098 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1100 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1101 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1102 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1104 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1105 2051e653 2021-03-13 op title = tab->hist_cur->h;
1107 a329982b 2021-03-11 op if (current)
1108 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1110 dc5df781 2021-03-13 op wprintw(tabline, "%s%d: %s",
1111 dc5df781 2021-03-13 op current ? "*" : " ", tab->id, title);
1113 a329982b 2021-03-11 op if (current)
1114 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1119 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1122 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1123 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1124 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1126 156f1501 2021-03-13 op werase(modeline);
1127 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1128 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1130 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1131 65d9b3ca 2021-03-14 op spin[tab->s.loading_anim_step], mode);
1133 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1135 65d9b3ca 2021-03-14 op if (tab->s.line_max <= body_lines)
1136 481340cc 2021-03-11 op wprintw(modeline, "All ");
1137 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1138 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1139 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1140 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1142 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1144 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1145 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1146 65d9b3ca 2021-03-14 op tab->s.line_max,
1147 2051e653 2021-03-13 op tab->hist_cur->h);
1149 48e9d457 2021-03-06 op getyx(modeline, y, x);
1150 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1153 48e9d457 2021-03-06 op (void)max_y;
1155 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1156 48e9d457 2021-03-06 op waddstr(modeline, "-");
1160 9ca15951 2021-03-09 op redraw_minibuffer(void)
1162 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1164 156f1501 2021-03-13 op werase(minibuf);
1165 22268e11 2021-03-11 op if (in_minibuffer) {
1166 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1167 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1168 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1169 22268e11 2021-03-11 op ministate.hist_off + 1,
1170 22268e11 2021-03-11 op ministate.history->len);
1172 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1174 22268e11 2021-03-11 op while (ministate.off - skip > COLS / 2) {
1175 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1178 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1179 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1181 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1184 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1185 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1186 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1188 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1191 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1192 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1194 2aaf475e 2021-03-13 op if (in_minibuffer)
1195 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1199 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1201 e19f9a04 2021-03-11 op redraw_tabline();
1202 e19f9a04 2021-03-11 op redraw_body(tab);
1203 e19f9a04 2021-03-11 op redraw_modeline(tab);
1204 e19f9a04 2021-03-11 op redraw_minibuffer();
1206 e19f9a04 2021-03-11 op restore_cursor(tab);
1207 e19f9a04 2021-03-11 op wrefresh(tabline);
1208 e19f9a04 2021-03-11 op wrefresh(modeline);
1210 e19f9a04 2021-03-11 op if (in_minibuffer) {
1211 e19f9a04 2021-03-11 op wrefresh(body);
1212 e19f9a04 2021-03-11 op wrefresh(minibuf);
1214 e19f9a04 2021-03-11 op wrefresh(minibuf);
1215 e19f9a04 2021-03-11 op wrefresh(body);
1220 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1222 9a25f829 2021-03-14 op struct vline *vl;
1225 48e9d457 2021-03-06 op werase(body);
1227 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1228 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1232 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1233 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1234 48e9d457 2021-03-06 op wmove(body, line, 0);
1235 9a25f829 2021-03-14 op print_vline(vl);
1237 48e9d457 2021-03-06 op if (line == body_lines)
1243 7953dd72 2021-03-07 op message(const char *fmt, ...)
1247 7953dd72 2021-03-07 op if (clminibufev_set)
1248 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1249 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1250 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1251 7953dd72 2021-03-07 op clminibufev_set = 1;
1253 bf992581 2021-03-12 op free(ministate.curmesg);
1255 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1256 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1257 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1258 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1261 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1263 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1264 9cb0f9ce 2021-03-10 op wrefresh(body);
1265 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1267 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1268 9cb0f9ce 2021-03-10 op wrefresh(body);
1273 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1275 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1277 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1278 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1279 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1283 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1285 8af5e5ed 2021-03-08 op struct tab *tab = d;
1287 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1289 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1290 8af5e5ed 2021-03-08 op wrefresh(modeline);
1292 8af5e5ed 2021-03-08 op wrefresh(body);
1293 9ca15951 2021-03-09 op if (in_minibuffer)
1294 9ca15951 2021-03-09 op wrefresh(minibuf);
1296 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1300 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1302 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1304 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1305 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1306 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1308 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1310 43a1b8d0 2021-03-09 op wrefresh(modeline);
1311 43a1b8d0 2021-03-09 op wrefresh(body);
1312 9ca15951 2021-03-09 op if (in_minibuffer)
1313 9ca15951 2021-03-09 op wrefresh(minibuf);
1317 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1319 43a1b8d0 2021-03-09 op empty_vlist(tab);
1320 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1321 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1322 8af5e5ed 2021-03-08 op load_url(tab, url);
1324 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1325 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1326 43a1b8d0 2021-03-09 op redraw_tab(tab);
1330 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1331 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1333 9ca15951 2021-03-09 op in_minibuffer = 1;
1334 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1335 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1337 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1339 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1340 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1341 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1342 9ca15951 2021-03-09 op ministate.off = 0;
1343 bb4e4ddf 2021-03-12 op ministate.len = 0;
1344 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1346 22268e11 2021-03-11 op ministate.history = hist;
1347 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1348 22268e11 2021-03-11 op ministate.hist_off = 0;
1352 b360ebb3 2021-03-10 op exit_minibuffer(void)
1354 156f1501 2021-03-13 op werase(minibuf);
1356 9ca15951 2021-03-09 op in_minibuffer = 0;
1357 9ca15951 2021-03-09 op base_map = &global_map;
1358 9ca15951 2021-03-09 op current_map = &global_map;
1362 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1364 5cd2ebb1 2021-03-11 op struct tab *t;
1366 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1367 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1370 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1373 b1df9b71 2021-03-12 op static struct tab *
1374 bcb0b073 2021-03-07 op new_tab(void)
1376 bcb0b073 2021-03-07 op struct tab *tab, *t;
1377 bcb0b073 2021-03-07 op const char *url = "about:new";
1379 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1382 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1384 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1386 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1387 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1389 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1390 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1392 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1394 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1398 bcb0b073 2021-03-07 op event_loopbreak();
1399 b1df9b71 2021-03-12 op return NULL;
1403 5e11c00c 2021-03-02 op ui_init(void)
1405 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1407 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1408 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1410 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1412 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1413 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1414 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1416 9ca15951 2021-03-09 op base_map = &global_map;
1417 f832146f 2021-03-09 op current_map = &global_map;
1418 f832146f 2021-03-09 op load_default_keys();
1425 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1427 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1429 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1431 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1433 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1436 48e9d457 2021-03-06 op body_lines = LINES-3;
1437 48e9d457 2021-03-06 op body_cols = COLS;
1439 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1440 48e9d457 2021-03-06 op scrollok(body, TRUE);
1442 5e11c00c 2021-03-02 op /* non-blocking input */
1443 48e9d457 2021-03-06 op wtimeout(body, 0);
1445 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1447 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1448 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1450 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1451 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1459 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1461 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1462 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1466 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1468 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1471 1d08c280 2021-03-06 op wrap_page(tab);
1472 5e11c00c 2021-03-02 op redraw_tab(tab);
1476 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1478 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1479 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1481 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1482 22268e11 2021-03-11 op &ir_history);
1483 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1484 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1485 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1489 5e11c00c 2021-03-02 op ui_end(void)