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*);
84 de0b2139 2021-03-15 op static void cmd_goto_bookmarks(struct tab*);
86 9ca15951 2021-03-09 op static void global_key_unbound(void);
88 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
89 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
90 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
91 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
92 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
93 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
94 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
95 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
96 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
97 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
98 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
100 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
101 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
102 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
103 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
104 b360ebb3 2021-03-10 op static void eecmd_select(void);
105 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
106 5cd2ebb1 2021-03-11 op static void ir_select(void);
107 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
108 5cd2ebb1 2021-03-11 op static void lu_select(void);
109 740f578b 2021-03-15 op static void bp_select(void);
111 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
112 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
113 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
114 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
115 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
116 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
117 9a25f829 2021-03-14 op static void print_vline(struct vline*);
118 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
119 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
120 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
121 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
122 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
123 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
124 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
125 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
126 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
127 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
128 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
129 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
130 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
131 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
132 b1df9b71 2021-03-12 op static struct tab *new_tab(void);
134 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
136 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
137 48e9d457 2021-03-06 op static int body_lines, body_cols;
139 48e9d457 2021-03-06 op static struct event clminibufev;
140 48e9d457 2021-03-06 op static int clminibufev_set;
141 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
142 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
144 bcb0b073 2021-03-07 op static uint32_t tab_counter;
146 7c7d7bb7 2021-03-10 op static char keybuf[64];
148 9ca15951 2021-03-09 op struct kmap global_map,
149 fa3fd864 2021-03-10 op minibuffer_map,
150 9ca15951 2021-03-09 op *current_map,
153 3148eeac 2021-03-13 op static struct histhead eecmd_history,
157 9ca15951 2021-03-09 op static int in_minibuffer;
159 9ca15951 2021-03-09 op static struct {
160 9cb0f9ce 2021-03-10 op char *curmesg;
162 040fbdf8 2021-03-10 op char buf[1025];
163 9ca15951 2021-03-09 op size_t off, len;
164 5cd2ebb1 2021-03-11 op char prompt[32];
165 b360ebb3 2021-03-10 op void (*donefn)(void);
166 b360ebb3 2021-03-10 op void (*abortfn)(void);
168 3148eeac 2021-03-13 op struct histhead *history;
169 3148eeac 2021-03-13 op struct hist *hist_cur;
170 22268e11 2021-03-11 op size_t hist_off;
173 f95d82dd 2021-03-11 op struct lineprefix {
174 f95d82dd 2021-03-11 op const char *prfx1;
175 f95d82dd 2021-03-11 op const char *prfx2;
176 f95d82dd 2021-03-11 op } line_prefixes[] = {
177 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
178 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
179 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
180 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
181 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
182 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
183 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
184 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
185 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
186 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
189 d3d56d8a 2021-03-11 op struct line_face {
191 d3d56d8a 2021-03-11 op } line_faces[] = {
192 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
193 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
194 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
195 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
196 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
197 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
198 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
199 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
200 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
201 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
205 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
207 65d9b3ca 2021-03-14 op struct vline *vl, *t;
209 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
211 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
212 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
213 65d9b3ca 2021-03-14 op free(vl->line);
218 f832146f 2021-03-09 op static inline void
219 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
221 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
225 9ca15951 2021-03-09 op static inline void
226 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
228 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
233 f832146f 2021-03-09 op load_default_keys(void)
235 9ca15951 2021-03-09 op /* === global map === */
238 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
239 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
240 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
241 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
242 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
243 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
245 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
246 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
247 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
248 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
250 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
252 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
254 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
255 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
256 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
258 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
259 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
260 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
261 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
263 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
264 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
266 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
267 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
269 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
270 de0b2139 2021-03-15 op global_set_key("<f7> <f7>", cmd_goto_bookmarks);
272 f832146f 2021-03-09 op /* vi/vi-like */
273 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
274 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
275 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
276 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
277 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
278 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
280 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
281 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
283 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
284 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
285 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
286 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
288 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
289 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
291 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
292 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
295 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
297 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
299 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
302 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
303 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
304 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
305 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
306 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
307 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
309 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
310 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
312 f832146f 2021-03-09 op /* "ncurses standard" */
313 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
316 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
317 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
319 fa3fd864 2021-03-10 op /* === minibuffer map === */
320 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
321 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
322 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
323 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
324 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
326 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
327 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
328 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
329 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
330 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
331 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
332 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
333 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
334 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
336 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
337 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
338 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
339 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
343 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
345 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
349 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
351 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
352 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
353 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
356 48e9d457 2021-03-06 op restore_cursor(tab);
360 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
362 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
365 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
366 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
367 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
370 48e9d457 2021-03-06 op restore_cursor(tab);
374 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
376 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
377 48e9d457 2021-03-06 op restore_cursor(tab);
381 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
383 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
384 a329982b 2021-03-11 op restore_cursor(tab);
388 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
390 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
391 a329982b 2021-03-11 op restore_cursor(tab);
395 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
397 9a25f829 2021-03-14 op struct vline *vl;
399 f95d82dd 2021-03-11 op const char *prfx;
401 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
402 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
403 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
407 9a25f829 2021-03-14 op vl = nth_line(tab, off);
408 9a25f829 2021-03-14 op if (vl->line != NULL)
409 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
411 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
413 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
414 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
417 48e9d457 2021-03-06 op restore_cursor(tab);
421 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
423 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
427 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
429 9a25f829 2021-03-14 op struct vline *vl;
431 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
434 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
435 48e9d457 2021-03-06 op wscrl(body, -1);
436 48e9d457 2021-03-06 op wmove(body, 0, 0);
437 9a25f829 2021-03-14 op print_vline(vl);
441 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
443 9a25f829 2021-03-14 op struct vline *vl;
445 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
448 65d9b3ca 2021-03-14 op tab->s.line_off++;
449 48e9d457 2021-03-06 op wscrl(body, 1);
451 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
454 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
455 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
456 9a25f829 2021-03-14 op print_vline(vl);
460 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
464 ed44414d 2021-03-09 op off = body_lines+1;
466 ed44414d 2021-03-09 op for (; off > 0; --off)
467 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
471 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
475 338eecdc 2021-03-12 op off = body_lines+1;
477 338eecdc 2021-03-12 op for (; off > 0; --off)
478 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
482 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
484 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
485 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
486 e19f9a04 2021-03-11 op redraw_body(tab);
490 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
494 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
495 e19f9a04 2021-03-11 op off = MAX(0, off);
497 65d9b3ca 2021-03-14 op tab->s.line_off = off;
498 754622a2 2021-03-15 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
500 e19f9a04 2021-03-11 op redraw_body(tab);
504 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
506 1d08c280 2021-03-06 op event_loopbreak();
510 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
512 9a25f829 2021-03-14 op struct vline *vl;
515 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
516 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
518 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
519 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
522 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
526 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
528 b1df9b71 2021-03-12 op struct tab *t;
529 9a25f829 2021-03-14 op struct vline *vl;
532 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
533 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
535 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
536 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
539 b1df9b71 2021-03-12 op t = new_tab();
540 b1df9b71 2021-03-12 op memcpy(&t->url, &tab->url, sizeof(tab->url));
541 9a25f829 2021-03-14 op load_url_in_tab(t, vl->parent->alt);
545 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
547 2051e653 2021-03-13 op if (!load_previous_page(tab))
548 2051e653 2021-03-13 op message("No previous page");
550 c40c2250 2021-03-14 op start_loading_anim(tab);
554 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
556 2051e653 2021-03-13 op if (!load_next_page(tab))
557 2051e653 2021-03-13 op message("No next page");
559 c40c2250 2021-03-14 op start_loading_anim(tab);
563 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
565 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
569 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
573 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
574 22268e11 2021-03-11 op &eecmd_history);
576 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
577 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
579 9ca15951 2021-03-09 op if (thiskey.meta)
580 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
582 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
583 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
587 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
589 7c7d7bb7 2021-03-10 op struct tab *t;
591 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
592 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
593 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
597 7c7d7bb7 2021-03-10 op stop_tab(tab);
599 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
600 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
602 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
608 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
614 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
616 7c7d7bb7 2021-03-10 op struct tab *t;
618 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
620 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
621 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
622 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
626 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
628 7c7d7bb7 2021-03-10 op struct tab *t;
630 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
632 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
633 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
634 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
638 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
640 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
641 22268e11 2021-03-11 op &lu_history);
642 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
646 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
648 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
649 22268e11 2021-03-11 op &lu_history);
650 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
651 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
652 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
653 740f578b 2021-03-15 op ministate.len = ministate.off;
657 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
659 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
660 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
661 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
662 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
663 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
667 de0b2139 2021-03-15 op cmd_goto_bookmarks(struct tab *tab)
669 de0b2139 2021-03-15 op load_url_in_tab(tab, "about:bookmarks");
673 7c7d7bb7 2021-03-10 op global_key_unbound(void)
675 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
679 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
681 3eecee9e 2021-03-12 op minibuffer_taint_hist();
683 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
686 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
687 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
688 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
689 d67133bf 2021-03-12 op ministate.len--;
693 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
695 3eecee9e 2021-03-12 op minibuffer_taint_hist();
697 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
700 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
701 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
702 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
703 9ca15951 2021-03-09 op ministate.off--;
704 9ca15951 2021-03-09 op ministate.len--;
708 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
710 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
712 9ca15951 2021-03-09 op ministate.off++;
716 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
718 9ca15951 2021-03-09 op if (ministate.off == 0)
720 9ca15951 2021-03-09 op ministate.off--;
724 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
726 9ca15951 2021-03-09 op ministate.off = ministate.len;
730 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
732 9ca15951 2021-03-09 op ministate.off = 0;
736 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
738 3eecee9e 2021-03-12 op minibuffer_taint_hist();
740 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
742 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
743 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
747 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
749 b360ebb3 2021-03-10 op ministate.abortfn();
753 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
755 22268e11 2021-03-11 op minibuffer_taint_hist();
756 b360ebb3 2021-03-10 op ministate.donefn();
760 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
762 22268e11 2021-03-11 op if (ministate.history == NULL) {
763 22268e11 2021-03-11 op message("No history");
767 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
768 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
769 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
770 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
771 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
772 22268e11 2021-03-11 op message("No prev item");
774 22268e11 2021-03-11 op ministate.hist_off--;
777 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
778 22268e11 2021-03-11 op ministate.off = 0;
779 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
784 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
786 22268e11 2021-03-11 op if (ministate.history == NULL) {
787 22268e11 2021-03-11 op message("No history");
791 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
792 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
793 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
794 22268e11 2021-03-11 op ministate.hist_off = 0;
795 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
796 22268e11 2021-03-11 op message("No next item");
798 22268e11 2021-03-11 op ministate.hist_off++;
801 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
802 22268e11 2021-03-11 op ministate.off = 0;
803 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
808 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
810 3148eeac 2021-03-13 op struct hist *hist;
812 22268e11 2021-03-11 op if (ministate.history == NULL)
815 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
818 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
820 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
821 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
823 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
824 22268e11 2021-03-11 op ministate.history->len++;
828 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
829 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
830 22268e11 2021-03-11 op * navigation" thing.
833 22268e11 2021-03-11 op minibuffer_taint_hist(void)
835 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
838 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
839 22268e11 2021-03-11 op ministate.hist_cur = NULL;
843 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
845 22268e11 2021-03-11 op minibuffer_taint_hist();
847 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
850 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
852 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
853 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
854 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
855 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
856 9ca15951 2021-03-09 op ministate.off++;
857 9ca15951 2021-03-09 op ministate.len++;
861 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
863 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
864 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
865 5cd2ebb1 2021-03-11 op global_key_unbound();
869 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
873 b360ebb3 2021-03-10 op eecmd_select(void)
875 b360ebb3 2021-03-10 op exit_minibuffer();
876 22268e11 2021-03-11 op minibuffer_hist_save_entry();
877 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
881 5cd2ebb1 2021-03-11 op ir_self_insert(void)
883 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
887 5cd2ebb1 2021-03-11 op ir_select(void)
889 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
890 5cd2ebb1 2021-03-11 op struct url url;
891 5cd2ebb1 2021-03-11 op struct tab *tab;
893 5cd2ebb1 2021-03-11 op tab = current_tab();
895 5cd2ebb1 2021-03-11 op exit_minibuffer();
896 22268e11 2021-03-11 op minibuffer_hist_save_entry();
898 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
899 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
900 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
901 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
902 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
906 5cd2ebb1 2021-03-11 op lu_self_insert(void)
908 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
909 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
910 5cd2ebb1 2021-03-11 op global_key_unbound();
914 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
918 5cd2ebb1 2021-03-11 op lu_select(void)
920 5cd2ebb1 2021-03-11 op exit_minibuffer();
921 22268e11 2021-03-11 op minibuffer_hist_save_entry();
922 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
926 740f578b 2021-03-15 op bp_select(void)
928 740f578b 2021-03-15 op exit_minibuffer();
929 740f578b 2021-03-15 op if (*ministate.buf != '\0')
930 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
932 740f578b 2021-03-15 op message("Abort.");
935 9a25f829 2021-03-14 op static struct vline *
936 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
938 9a25f829 2021-03-14 op struct vline *vl;
942 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
948 48e9d457 2021-03-06 op /* unreachable */
952 5e11c00c 2021-03-02 op static struct tab *
953 5e11c00c 2021-03-02 op current_tab(void)
955 5e11c00c 2021-03-02 op struct tab *t;
957 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
958 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
962 5e11c00c 2021-03-02 op /* unreachable */
967 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
969 f832146f 2021-03-09 op struct keymap *k;
970 7c7d7bb7 2021-03-10 op const char *keyname;
971 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
973 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
974 9ca15951 2021-03-09 op if (thiskey.key == ERR)
976 9ca15951 2021-03-09 op if (thiskey.key == 27) {
977 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
979 9ca15951 2021-03-09 op thiskey.meta = 1;
980 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
981 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
982 c314a314 2021-03-11 op thiskey.meta = 0;
983 9ca15951 2021-03-09 op thiskey.key = 27;
986 9ca15951 2021-03-09 op thiskey.meta = 0;
988 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
989 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
990 7c7d7bb7 2021-03-10 op if (thiskey.meta)
991 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
992 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
993 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
995 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
996 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
999 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1000 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1001 9ca15951 2021-03-09 op k->key == thiskey.key) {
1002 f832146f 2021-03-09 op if (k->fn == NULL)
1003 f832146f 2021-03-09 op current_map = &k->map;
1005 9ca15951 2021-03-09 op current_map = base_map;
1006 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1007 f832146f 2021-03-09 op k->fn(current_tab());
1013 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1014 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1016 7c7d7bb7 2021-03-10 op global_key_unbound();
1019 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1020 9ca15951 2021-03-09 op current_map = base_map;
1023 179f0f58 2021-03-11 op redraw_tab(current_tab());
1027 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1029 a6d450c1 2021-03-06 op clminibufev_set = 0;
1031 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1032 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1034 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1035 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1036 9cb0f9ce 2021-03-10 op wrefresh(body);
1037 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1039 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1040 9cb0f9ce 2021-03-10 op wrefresh(body);
1045 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1047 1d08c280 2021-03-06 op struct tab *tab;
1053 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1055 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1056 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1058 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1059 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1061 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1062 48e9d457 2021-03-06 op body_lines = LINES-3;
1063 bd9637e9 2021-03-06 op body_cols = COLS;
1065 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1067 1d08c280 2021-03-06 op tab = current_tab();
1069 1d08c280 2021-03-06 op wrap_page(tab);
1070 1d08c280 2021-03-06 op redraw_tab(tab);
1074 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1076 1d08c280 2021-03-06 op struct line *l;
1077 0d568960 2021-03-11 op const char *prfx;
1079 1d08c280 2021-03-06 op empty_vlist(tab);
1081 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1082 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1083 5e11c00c 2021-03-02 op switch (l->type) {
1084 5e11c00c 2021-03-02 op case LINE_TEXT:
1085 5e11c00c 2021-03-02 op case LINE_LINK:
1086 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1087 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1088 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1089 5e11c00c 2021-03-02 op case LINE_ITEM:
1090 5e11c00c 2021-03-02 op case LINE_QUOTE:
1091 5e11c00c 2021-03-02 op case LINE_PRE_START:
1092 5e11c00c 2021-03-02 op case LINE_PRE_END:
1093 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1094 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1096 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1097 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1105 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1107 9a25f829 2021-03-14 op const char *text = vl->line;
1108 768db5bb 2021-03-12 op const char *prfx;
1109 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1111 9a25f829 2021-03-14 op if (!vl->flags)
1112 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1114 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1116 bd9637e9 2021-03-06 op if (text == NULL)
1119 d3d56d8a 2021-03-11 op if (face != 0)
1120 d3d56d8a 2021-03-11 op wattron(body, face);
1121 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1122 d3d56d8a 2021-03-11 op if (face != 0)
1123 d3d56d8a 2021-03-11 op wattroff(body, face);
1127 8af5e5ed 2021-03-08 op redraw_tabline(void)
1129 7c7d7bb7 2021-03-10 op struct tab *tab;
1130 7c7d7bb7 2021-03-10 op int current;
1131 dc5df781 2021-03-13 op const char *title;
1133 156f1501 2021-03-13 op werase(tabline);
1134 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1136 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1137 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1138 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1140 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1141 2051e653 2021-03-13 op title = tab->hist_cur->h;
1143 a329982b 2021-03-11 op if (current)
1144 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1146 dc5df781 2021-03-13 op wprintw(tabline, "%s%d: %s",
1147 dc5df781 2021-03-13 op current ? "*" : " ", tab->id, title);
1149 a329982b 2021-03-11 op if (current)
1150 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1155 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1158 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1159 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1160 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1162 156f1501 2021-03-13 op werase(modeline);
1163 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1164 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1166 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1167 65d9b3ca 2021-03-14 op spin[tab->s.loading_anim_step], mode);
1169 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1171 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1172 481340cc 2021-03-11 op wprintw(modeline, "All ");
1173 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1174 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1175 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1176 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1178 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1180 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1181 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1182 65d9b3ca 2021-03-14 op tab->s.line_max,
1183 2051e653 2021-03-13 op tab->hist_cur->h);
1185 48e9d457 2021-03-06 op getyx(modeline, y, x);
1186 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1189 48e9d457 2021-03-06 op (void)max_y;
1191 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1192 48e9d457 2021-03-06 op waddstr(modeline, "-");
1196 9ca15951 2021-03-09 op redraw_minibuffer(void)
1198 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1200 156f1501 2021-03-13 op werase(minibuf);
1201 22268e11 2021-03-11 op if (in_minibuffer) {
1202 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1203 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1204 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1205 22268e11 2021-03-11 op ministate.hist_off + 1,
1206 22268e11 2021-03-11 op ministate.history->len);
1208 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1210 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1211 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1214 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1215 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1217 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1220 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1221 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1222 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1224 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1227 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1228 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1230 2aaf475e 2021-03-13 op if (in_minibuffer)
1231 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1235 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1237 e19f9a04 2021-03-11 op redraw_tabline();
1238 e19f9a04 2021-03-11 op redraw_body(tab);
1239 e19f9a04 2021-03-11 op redraw_modeline(tab);
1240 e19f9a04 2021-03-11 op redraw_minibuffer();
1242 e19f9a04 2021-03-11 op restore_cursor(tab);
1243 e19f9a04 2021-03-11 op wrefresh(tabline);
1244 e19f9a04 2021-03-11 op wrefresh(modeline);
1246 e19f9a04 2021-03-11 op if (in_minibuffer) {
1247 e19f9a04 2021-03-11 op wrefresh(body);
1248 e19f9a04 2021-03-11 op wrefresh(minibuf);
1250 e19f9a04 2021-03-11 op wrefresh(minibuf);
1251 e19f9a04 2021-03-11 op wrefresh(body);
1256 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1258 9a25f829 2021-03-14 op struct vline *vl;
1261 48e9d457 2021-03-06 op werase(body);
1263 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1264 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1268 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1269 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1270 48e9d457 2021-03-06 op wmove(body, line, 0);
1271 9a25f829 2021-03-14 op print_vline(vl);
1273 48e9d457 2021-03-06 op if (line == body_lines)
1279 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1281 7953dd72 2021-03-07 op if (clminibufev_set)
1282 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1283 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1284 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1285 7953dd72 2021-03-07 op clminibufev_set = 1;
1287 bf992581 2021-03-12 op free(ministate.curmesg);
1289 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1290 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1291 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1293 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1294 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1295 9cb0f9ce 2021-03-10 op wrefresh(body);
1296 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1298 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1299 9cb0f9ce 2021-03-10 op wrefresh(body);
1304 740f578b 2021-03-15 op message(const char *fmt, ...)
1308 740f578b 2021-03-15 op va_start(ap, fmt);
1309 740f578b 2021-03-15 op vmessage(fmt, ap);
1314 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1316 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1318 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1319 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1320 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1324 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1326 8af5e5ed 2021-03-08 op struct tab *tab = d;
1328 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1330 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1331 8af5e5ed 2021-03-08 op wrefresh(modeline);
1333 8af5e5ed 2021-03-08 op wrefresh(body);
1334 9ca15951 2021-03-09 op if (in_minibuffer)
1335 9ca15951 2021-03-09 op wrefresh(minibuf);
1337 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1341 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1343 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1345 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1346 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1347 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1349 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1351 43a1b8d0 2021-03-09 op wrefresh(modeline);
1352 43a1b8d0 2021-03-09 op wrefresh(body);
1353 9ca15951 2021-03-09 op if (in_minibuffer)
1354 9ca15951 2021-03-09 op wrefresh(minibuf);
1358 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1360 43a1b8d0 2021-03-09 op empty_vlist(tab);
1361 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1362 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1363 8af5e5ed 2021-03-08 op load_url(tab, url);
1365 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1366 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1367 43a1b8d0 2021-03-09 op redraw_tab(tab);
1371 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1372 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1374 9ca15951 2021-03-09 op in_minibuffer = 1;
1375 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1376 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1378 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1380 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1381 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1382 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1383 9ca15951 2021-03-09 op ministate.off = 0;
1384 bb4e4ddf 2021-03-12 op ministate.len = 0;
1385 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1387 22268e11 2021-03-11 op ministate.history = hist;
1388 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1389 22268e11 2021-03-11 op ministate.hist_off = 0;
1393 b360ebb3 2021-03-10 op exit_minibuffer(void)
1395 156f1501 2021-03-13 op werase(minibuf);
1397 9ca15951 2021-03-09 op in_minibuffer = 0;
1398 9ca15951 2021-03-09 op base_map = &global_map;
1399 9ca15951 2021-03-09 op current_map = &global_map;
1403 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1405 5cd2ebb1 2021-03-11 op struct tab *t;
1407 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1408 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1411 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1414 b1df9b71 2021-03-12 op static struct tab *
1415 bcb0b073 2021-03-07 op new_tab(void)
1417 754622a2 2021-03-15 op struct tab *tab;
1418 bcb0b073 2021-03-07 op const char *url = "about:new";
1420 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1423 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1425 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1427 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1428 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1430 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1431 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1433 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1435 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1439 bcb0b073 2021-03-07 op event_loopbreak();
1440 b1df9b71 2021-03-12 op return NULL;
1444 5e11c00c 2021-03-02 op ui_init(void)
1446 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1448 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1449 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1451 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1453 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1454 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1455 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1457 9ca15951 2021-03-09 op base_map = &global_map;
1458 f832146f 2021-03-09 op current_map = &global_map;
1459 f832146f 2021-03-09 op load_default_keys();
1466 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1468 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1470 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1472 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1474 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1477 48e9d457 2021-03-06 op body_lines = LINES-3;
1478 48e9d457 2021-03-06 op body_cols = COLS;
1480 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1481 48e9d457 2021-03-06 op scrollok(body, TRUE);
1483 5e11c00c 2021-03-02 op /* non-blocking input */
1484 48e9d457 2021-03-06 op wtimeout(body, 0);
1486 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1488 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1489 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1491 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1492 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1500 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1502 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1503 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1507 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1509 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1512 1d08c280 2021-03-06 op wrap_page(tab);
1513 5e11c00c 2021-03-02 op redraw_tab(tab);
1517 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1519 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1520 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1522 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1523 22268e11 2021-03-11 op &ir_history);
1524 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1525 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1526 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1530 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1534 740f578b 2021-03-15 op va_start(ap, fmt);
1535 740f578b 2021-03-15 op vmessage(fmt, ap);
1540 5e11c00c 2021-03-02 op ui_end(void)