2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
20 1d08c280 2021-03-06 op * Text scrolling
21 1d08c280 2021-03-06 op * ==============
23 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
24 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
25 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
26 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
27 1d08c280 2021-03-06 op * first/last line!
29 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
30 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
31 1d08c280 2021-03-06 op * ``on-demand''.
33 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
37 5e11c00c 2021-03-02 op #include <telescope.h>
39 f832146f 2021-03-09 op #include <ctype.h>
40 5e11c00c 2021-03-02 op #include <curses.h>
41 5e11c00c 2021-03-02 op #include <event.h>
42 5e11c00c 2021-03-02 op #include <locale.h>
43 5e11c00c 2021-03-02 op #include <signal.h>
44 7953dd72 2021-03-07 op #include <stdarg.h>
45 eb259e66 2021-03-02 op #include <stdlib.h>
46 eb259e66 2021-03-02 op #include <string.h>
47 f832146f 2021-03-09 op #include <unistd.h>
49 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
51 1b81bc33 2021-03-15 op #define NEW_TAB_URL "about:new"
53 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
55 f832146f 2021-03-09 op static void load_default_keys(void);
56 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
57 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
59 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
60 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
61 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
62 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
63 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
64 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
65 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
66 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
67 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
68 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
69 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
70 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
71 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
72 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
73 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
74 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
75 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
76 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
77 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
78 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
79 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
80 fea845b6 2021-03-15 op static void cmd_tab_close_other(struct tab*);
81 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
82 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
83 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
84 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
85 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
86 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
87 de0b2139 2021-03-15 op static void cmd_goto_bookmarks(struct tab*);
89 9ca15951 2021-03-09 op static void global_key_unbound(void);
91 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
92 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
93 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
94 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
95 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
96 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
97 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
98 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
99 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
100 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
101 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
103 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
104 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
105 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
106 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
107 b360ebb3 2021-03-10 op static void eecmd_select(void);
108 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
109 5cd2ebb1 2021-03-11 op static void ir_select(void);
110 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
111 5cd2ebb1 2021-03-11 op static void lu_select(void);
112 740f578b 2021-03-15 op static void bp_select(void);
114 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
115 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
116 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
117 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
118 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
119 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
120 9a25f829 2021-03-14 op static void print_vline(struct vline*);
121 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
122 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
123 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
124 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
125 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
126 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
127 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
128 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
129 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
130 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
131 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
132 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
133 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
134 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
135 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
137 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
139 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
140 48e9d457 2021-03-06 op static int body_lines, body_cols;
142 48e9d457 2021-03-06 op static struct event clminibufev;
143 48e9d457 2021-03-06 op static int clminibufev_set;
144 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
145 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
147 bcb0b073 2021-03-07 op static uint32_t tab_counter;
149 7c7d7bb7 2021-03-10 op static char keybuf[64];
151 9ca15951 2021-03-09 op struct kmap global_map,
152 fa3fd864 2021-03-10 op minibuffer_map,
153 9ca15951 2021-03-09 op *current_map,
156 3148eeac 2021-03-13 op static struct histhead eecmd_history,
160 9ca15951 2021-03-09 op static int in_minibuffer;
162 9ca15951 2021-03-09 op static struct {
163 9cb0f9ce 2021-03-10 op char *curmesg;
165 040fbdf8 2021-03-10 op char buf[1025];
166 9ca15951 2021-03-09 op size_t off, len;
167 5cd2ebb1 2021-03-11 op char prompt[32];
168 b360ebb3 2021-03-10 op void (*donefn)(void);
169 b360ebb3 2021-03-10 op void (*abortfn)(void);
171 3148eeac 2021-03-13 op struct histhead *history;
172 3148eeac 2021-03-13 op struct hist *hist_cur;
173 22268e11 2021-03-11 op size_t hist_off;
176 f95d82dd 2021-03-11 op struct lineprefix {
177 f95d82dd 2021-03-11 op const char *prfx1;
178 f95d82dd 2021-03-11 op const char *prfx2;
179 f95d82dd 2021-03-11 op } line_prefixes[] = {
180 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
181 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
182 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
183 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
184 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
185 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
186 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
187 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
188 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
189 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
192 d3d56d8a 2021-03-11 op struct line_face {
194 d3d56d8a 2021-03-11 op } line_faces[] = {
195 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
196 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
197 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
198 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
199 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
200 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
201 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
202 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
203 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
204 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
208 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
210 65d9b3ca 2021-03-14 op struct vline *vl, *t;
212 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
214 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
215 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
216 65d9b3ca 2021-03-14 op free(vl->line);
221 f832146f 2021-03-09 op static inline void
222 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
224 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
228 9ca15951 2021-03-09 op static inline void
229 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
231 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
236 f832146f 2021-03-09 op load_default_keys(void)
238 9ca15951 2021-03-09 op /* === global map === */
241 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
242 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
243 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
244 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
245 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
246 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
248 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
249 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
250 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
251 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
253 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
255 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
257 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
258 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
259 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
261 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
262 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
263 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
264 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
265 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
267 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
268 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
270 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
271 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
273 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
274 de0b2139 2021-03-15 op global_set_key("<f7> <f7>", cmd_goto_bookmarks);
276 f832146f 2021-03-09 op /* vi/vi-like */
277 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
278 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
279 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
280 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
281 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
282 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
284 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
285 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
287 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
288 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
289 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
290 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
292 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
293 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
295 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
296 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
299 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
301 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
303 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
306 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
307 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
308 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
309 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
310 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
311 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
313 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
314 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
316 f832146f 2021-03-09 op /* "ncurses standard" */
317 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
320 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
321 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
323 fa3fd864 2021-03-10 op /* === minibuffer map === */
324 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
325 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
326 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
327 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
328 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
330 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
331 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
332 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
333 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
334 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
335 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
336 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
337 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
338 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
340 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
341 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
342 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
343 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
347 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
349 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
353 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
355 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
356 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
357 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
360 48e9d457 2021-03-06 op restore_cursor(tab);
364 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
366 65d9b3ca 2021-03-14 op if (tab->s.line_off + tab->s.curs_y >= tab->s.line_max)
369 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
370 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
371 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
374 48e9d457 2021-03-06 op restore_cursor(tab);
378 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
380 65d9b3ca 2021-03-14 op tab->s.curs_x = MIN(body_cols-1, tab->s.curs_x+1);
381 48e9d457 2021-03-06 op restore_cursor(tab);
385 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
387 65d9b3ca 2021-03-14 op tab->s.curs_x = MAX(0, tab->s.curs_x-1);
388 a329982b 2021-03-11 op restore_cursor(tab);
392 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
394 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
395 a329982b 2021-03-11 op restore_cursor(tab);
399 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
401 9a25f829 2021-03-14 op struct vline *vl;
403 f95d82dd 2021-03-11 op const char *prfx;
405 65d9b3ca 2021-03-14 op off = tab->s.line_off + tab->s.curs_y;
406 65d9b3ca 2021-03-14 op if (off >= tab->s.line_max) {
407 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
411 9a25f829 2021-03-14 op vl = nth_line(tab, off);
412 9a25f829 2021-03-14 op if (vl->line != NULL)
413 65d9b3ca 2021-03-14 op tab->s.curs_x = strlen(vl->line);
415 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
417 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
418 65d9b3ca 2021-03-14 op tab->s.curs_x += strlen(prfx);
421 48e9d457 2021-03-06 op restore_cursor(tab);
425 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
427 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
431 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
433 9a25f829 2021-03-14 op struct vline *vl;
435 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
438 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
439 48e9d457 2021-03-06 op wscrl(body, -1);
440 48e9d457 2021-03-06 op wmove(body, 0, 0);
441 9a25f829 2021-03-14 op print_vline(vl);
445 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
447 9a25f829 2021-03-14 op struct vline *vl;
449 65d9b3ca 2021-03-14 op if (tab->s.line_max == 0 || tab->s.line_off == tab->s.line_max-1)
452 65d9b3ca 2021-03-14 op tab->s.line_off++;
453 48e9d457 2021-03-06 op wscrl(body, 1);
455 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
458 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
459 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
460 9a25f829 2021-03-14 op print_vline(vl);
464 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
468 ed44414d 2021-03-09 op off = body_lines+1;
470 ed44414d 2021-03-09 op for (; off > 0; --off)
471 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
475 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
479 338eecdc 2021-03-12 op off = body_lines+1;
481 338eecdc 2021-03-12 op for (; off > 0; --off)
482 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
486 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
488 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
489 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
490 e19f9a04 2021-03-11 op redraw_body(tab);
494 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
498 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
499 e19f9a04 2021-03-11 op off = MAX(0, off);
501 65d9b3ca 2021-03-14 op tab->s.line_off = off;
502 754622a2 2021-03-15 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max);
504 e19f9a04 2021-03-11 op redraw_body(tab);
508 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
510 1d08c280 2021-03-06 op event_loopbreak();
514 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
516 9a25f829 2021-03-14 op struct vline *vl;
519 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
520 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
522 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
523 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
526 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
530 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
532 9a25f829 2021-03-14 op struct vline *vl;
535 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
536 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
538 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
539 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
542 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
546 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
548 2051e653 2021-03-13 op if (!load_previous_page(tab))
549 2051e653 2021-03-13 op message("No previous page");
551 c40c2250 2021-03-14 op start_loading_anim(tab);
555 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
557 2051e653 2021-03-13 op if (!load_next_page(tab))
558 2051e653 2021-03-13 op message("No next page");
560 c40c2250 2021-03-14 op start_loading_anim(tab);
564 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
566 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
570 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
574 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
575 22268e11 2021-03-11 op &eecmd_history);
577 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
578 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
580 9ca15951 2021-03-09 op if (thiskey.meta)
581 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
583 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
584 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
588 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
590 7c7d7bb7 2021-03-10 op struct tab *t;
592 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
593 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
594 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
598 7c7d7bb7 2021-03-10 op stop_tab(tab);
600 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
601 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
603 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
609 fea845b6 2021-03-15 op cmd_tab_close_other(struct tab *tab)
611 fea845b6 2021-03-15 op struct tab *t, *i;
613 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
614 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
618 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
624 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
626 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
630 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
632 7c7d7bb7 2021-03-10 op struct tab *t;
634 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
636 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
637 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
638 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
642 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
644 7c7d7bb7 2021-03-10 op struct tab *t;
646 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
648 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
649 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
650 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
654 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
656 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
657 22268e11 2021-03-11 op &lu_history);
658 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
662 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
664 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
665 22268e11 2021-03-11 op &lu_history);
666 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
667 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
668 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
669 740f578b 2021-03-15 op ministate.len = ministate.off;
673 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
675 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
676 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
677 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
678 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
679 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
683 de0b2139 2021-03-15 op cmd_goto_bookmarks(struct tab *tab)
685 de0b2139 2021-03-15 op load_url_in_tab(tab, "about:bookmarks");
689 7c7d7bb7 2021-03-10 op global_key_unbound(void)
691 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
695 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
697 3eecee9e 2021-03-12 op minibuffer_taint_hist();
699 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
702 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
703 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
704 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
705 d67133bf 2021-03-12 op ministate.len--;
709 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
711 3eecee9e 2021-03-12 op minibuffer_taint_hist();
713 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
716 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
717 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
718 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
719 9ca15951 2021-03-09 op ministate.off--;
720 9ca15951 2021-03-09 op ministate.len--;
724 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
726 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
728 9ca15951 2021-03-09 op ministate.off++;
732 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
734 9ca15951 2021-03-09 op if (ministate.off == 0)
736 9ca15951 2021-03-09 op ministate.off--;
740 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
742 9ca15951 2021-03-09 op ministate.off = ministate.len;
746 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
748 9ca15951 2021-03-09 op ministate.off = 0;
752 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
754 3eecee9e 2021-03-12 op minibuffer_taint_hist();
756 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
758 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
759 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
763 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
765 b360ebb3 2021-03-10 op ministate.abortfn();
769 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
771 22268e11 2021-03-11 op minibuffer_taint_hist();
772 b360ebb3 2021-03-10 op ministate.donefn();
776 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
778 22268e11 2021-03-11 op if (ministate.history == NULL) {
779 22268e11 2021-03-11 op message("No history");
783 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
784 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
785 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
786 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
787 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
788 22268e11 2021-03-11 op message("No prev item");
790 22268e11 2021-03-11 op ministate.hist_off--;
793 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
794 22268e11 2021-03-11 op ministate.off = 0;
795 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
800 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
802 22268e11 2021-03-11 op if (ministate.history == NULL) {
803 22268e11 2021-03-11 op message("No history");
807 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
808 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
809 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
810 22268e11 2021-03-11 op ministate.hist_off = 0;
811 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
812 22268e11 2021-03-11 op message("No next item");
814 22268e11 2021-03-11 op ministate.hist_off++;
817 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
818 22268e11 2021-03-11 op ministate.off = 0;
819 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
824 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
826 3148eeac 2021-03-13 op struct hist *hist;
828 22268e11 2021-03-11 op if (ministate.history == NULL)
831 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
834 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
836 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
837 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
839 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
840 22268e11 2021-03-11 op ministate.history->len++;
844 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
845 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
846 22268e11 2021-03-11 op * navigation" thing.
849 22268e11 2021-03-11 op minibuffer_taint_hist(void)
851 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
854 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
855 22268e11 2021-03-11 op ministate.hist_cur = NULL;
859 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
861 22268e11 2021-03-11 op minibuffer_taint_hist();
863 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
866 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
868 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
869 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
870 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
871 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
872 9ca15951 2021-03-09 op ministate.off++;
873 9ca15951 2021-03-09 op ministate.len++;
877 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
879 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
880 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
881 5cd2ebb1 2021-03-11 op global_key_unbound();
885 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
889 b360ebb3 2021-03-10 op eecmd_select(void)
891 b360ebb3 2021-03-10 op exit_minibuffer();
892 22268e11 2021-03-11 op minibuffer_hist_save_entry();
893 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
897 5cd2ebb1 2021-03-11 op ir_self_insert(void)
899 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
903 5cd2ebb1 2021-03-11 op ir_select(void)
905 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
906 5cd2ebb1 2021-03-11 op struct url url;
907 5cd2ebb1 2021-03-11 op struct tab *tab;
909 5cd2ebb1 2021-03-11 op tab = current_tab();
911 5cd2ebb1 2021-03-11 op exit_minibuffer();
912 22268e11 2021-03-11 op minibuffer_hist_save_entry();
914 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
915 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
916 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
917 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
918 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
922 5cd2ebb1 2021-03-11 op lu_self_insert(void)
924 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
925 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
926 5cd2ebb1 2021-03-11 op global_key_unbound();
930 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
934 5cd2ebb1 2021-03-11 op lu_select(void)
936 5cd2ebb1 2021-03-11 op exit_minibuffer();
937 22268e11 2021-03-11 op minibuffer_hist_save_entry();
938 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
942 740f578b 2021-03-15 op bp_select(void)
944 740f578b 2021-03-15 op exit_minibuffer();
945 740f578b 2021-03-15 op if (*ministate.buf != '\0')
946 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
948 740f578b 2021-03-15 op message("Abort.");
951 9a25f829 2021-03-14 op static struct vline *
952 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
954 9a25f829 2021-03-14 op struct vline *vl;
958 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
964 48e9d457 2021-03-06 op /* unreachable */
968 5e11c00c 2021-03-02 op static struct tab *
969 5e11c00c 2021-03-02 op current_tab(void)
971 5e11c00c 2021-03-02 op struct tab *t;
973 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
974 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
978 5e11c00c 2021-03-02 op /* unreachable */
983 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
985 f832146f 2021-03-09 op struct keymap *k;
986 7c7d7bb7 2021-03-10 op const char *keyname;
987 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
989 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
990 9ca15951 2021-03-09 op if (thiskey.key == ERR)
992 9ca15951 2021-03-09 op if (thiskey.key == 27) {
993 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
995 9ca15951 2021-03-09 op thiskey.meta = 1;
996 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
997 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
998 c314a314 2021-03-11 op thiskey.meta = 0;
999 9ca15951 2021-03-09 op thiskey.key = 27;
1002 9ca15951 2021-03-09 op thiskey.meta = 0;
1004 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1005 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1006 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1007 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1008 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
1009 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
1011 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
1012 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1015 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1016 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1017 9ca15951 2021-03-09 op k->key == thiskey.key) {
1018 f832146f 2021-03-09 op if (k->fn == NULL)
1019 f832146f 2021-03-09 op current_map = &k->map;
1021 9ca15951 2021-03-09 op current_map = base_map;
1022 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1023 f832146f 2021-03-09 op k->fn(current_tab());
1029 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1030 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1032 7c7d7bb7 2021-03-10 op global_key_unbound();
1035 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1036 9ca15951 2021-03-09 op current_map = base_map;
1039 179f0f58 2021-03-11 op redraw_tab(current_tab());
1043 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1045 a6d450c1 2021-03-06 op clminibufev_set = 0;
1047 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1048 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1050 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1051 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1052 9cb0f9ce 2021-03-10 op wrefresh(body);
1053 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1055 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1056 9cb0f9ce 2021-03-10 op wrefresh(body);
1061 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1063 1d08c280 2021-03-06 op struct tab *tab;
1069 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1071 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1072 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1074 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1075 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1077 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1078 48e9d457 2021-03-06 op body_lines = LINES-3;
1079 bd9637e9 2021-03-06 op body_cols = COLS;
1081 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1083 1d08c280 2021-03-06 op tab = current_tab();
1085 1d08c280 2021-03-06 op wrap_page(tab);
1086 1d08c280 2021-03-06 op redraw_tab(tab);
1090 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1092 1d08c280 2021-03-06 op struct line *l;
1093 0d568960 2021-03-11 op const char *prfx;
1095 1d08c280 2021-03-06 op empty_vlist(tab);
1097 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1098 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1099 5e11c00c 2021-03-02 op switch (l->type) {
1100 5e11c00c 2021-03-02 op case LINE_TEXT:
1101 5e11c00c 2021-03-02 op case LINE_LINK:
1102 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1103 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1104 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1105 5e11c00c 2021-03-02 op case LINE_ITEM:
1106 5e11c00c 2021-03-02 op case LINE_QUOTE:
1107 5e11c00c 2021-03-02 op case LINE_PRE_START:
1108 5e11c00c 2021-03-02 op case LINE_PRE_END:
1109 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1110 65d9b3ca 2021-03-14 op /* push_line(tab, l, NULL, 0, 0); */
1112 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1113 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1121 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1123 9a25f829 2021-03-14 op const char *text = vl->line;
1124 768db5bb 2021-03-12 op const char *prfx;
1125 9a25f829 2021-03-14 op int face = line_faces[vl->parent->type].prop;
1127 9a25f829 2021-03-14 op if (!vl->flags)
1128 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1130 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1132 bd9637e9 2021-03-06 op if (text == NULL)
1135 d3d56d8a 2021-03-11 op if (face != 0)
1136 d3d56d8a 2021-03-11 op wattron(body, face);
1137 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1138 d3d56d8a 2021-03-11 op if (face != 0)
1139 d3d56d8a 2021-03-11 op wattroff(body, face);
1143 8af5e5ed 2021-03-08 op redraw_tabline(void)
1145 7c7d7bb7 2021-03-10 op struct tab *tab;
1146 a636f50e 2021-03-16 op size_t toskip;
1147 a636f50e 2021-03-16 op int current, x, y, truncated;
1148 dc5df781 2021-03-13 op const char *title;
1149 119f393c 2021-03-16 op char buf[25];
1153 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1154 a636f50e 2021-03-16 op x += sizeof(buf) + 1;
1156 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1159 a636f50e 2021-03-16 op if (x < COLS-2)
1164 a636f50e 2021-03-16 op werase(tabline);
1165 119f393c 2021-03-16 op wattron(tabline, A_REVERSE);
1166 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1168 a636f50e 2021-03-16 op truncated = 0;
1169 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1170 a636f50e 2021-03-16 op if (truncated)
1172 a636f50e 2021-03-16 op if (toskip != 0) {
1177 a636f50e 2021-03-16 op getyx(tabline, y, x);
1178 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1179 a636f50e 2021-03-16 op truncated = 1;
1181 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1183 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1184 2051e653 2021-03-13 op title = tab->hist_cur->h;
1186 119f393c 2021-03-16 op strlcpy(buf, " ", sizeof(buf));
1187 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1188 119f393c 2021-03-16 op /* truncation happens */
1189 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1191 119f393c 2021-03-16 op /* pad with spaces */
1192 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1196 a329982b 2021-03-11 op if (current)
1197 119f393c 2021-03-16 op wattroff(tabline, A_REVERSE);
1199 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1201 119f393c 2021-03-16 op if (current)
1202 119f393c 2021-03-16 op wattron(tabline, A_REVERSE);
1204 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1205 119f393c 2021-03-16 op wprintw(tabline, " ");
1208 119f393c 2021-03-16 op for (; x < COLS; ++x)
1209 119f393c 2021-03-16 op waddch(tabline, ' ');
1210 a636f50e 2021-03-16 op if (truncated)
1211 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1215 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1218 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1219 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1220 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1222 156f1501 2021-03-13 op werase(modeline);
1223 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1224 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1226 3710f27a 2021-03-14 op wprintw(modeline, "-%c %s ",
1227 58c75fab 2021-03-16 op spin[tab->s.loading_anim_step],
1228 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1230 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1232 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1233 481340cc 2021-03-11 op wprintw(modeline, "All ");
1234 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1235 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1236 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1237 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1239 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1241 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1242 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1243 65d9b3ca 2021-03-14 op tab->s.line_max,
1244 2051e653 2021-03-13 op tab->hist_cur->h);
1246 48e9d457 2021-03-06 op getyx(modeline, y, x);
1247 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1250 48e9d457 2021-03-06 op (void)max_y;
1252 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1253 48e9d457 2021-03-06 op waddstr(modeline, "-");
1257 9ca15951 2021-03-09 op redraw_minibuffer(void)
1259 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1261 156f1501 2021-03-13 op werase(minibuf);
1262 22268e11 2021-03-11 op if (in_minibuffer) {
1263 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1264 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1265 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1266 22268e11 2021-03-11 op ministate.hist_off + 1,
1267 22268e11 2021-03-11 op ministate.history->len);
1269 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1271 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1272 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1275 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1276 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1278 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1281 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1282 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1283 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1285 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1288 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1289 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1291 2aaf475e 2021-03-13 op if (in_minibuffer)
1292 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1296 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1298 e19f9a04 2021-03-11 op redraw_tabline();
1299 e19f9a04 2021-03-11 op redraw_body(tab);
1300 e19f9a04 2021-03-11 op redraw_modeline(tab);
1301 e19f9a04 2021-03-11 op redraw_minibuffer();
1303 e19f9a04 2021-03-11 op restore_cursor(tab);
1304 e19f9a04 2021-03-11 op wrefresh(tabline);
1305 e19f9a04 2021-03-11 op wrefresh(modeline);
1307 e19f9a04 2021-03-11 op if (in_minibuffer) {
1308 e19f9a04 2021-03-11 op wrefresh(body);
1309 e19f9a04 2021-03-11 op wrefresh(minibuf);
1311 e19f9a04 2021-03-11 op wrefresh(minibuf);
1312 e19f9a04 2021-03-11 op wrefresh(body);
1317 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1319 9a25f829 2021-03-14 op struct vline *vl;
1322 48e9d457 2021-03-06 op werase(body);
1324 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1325 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1329 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1330 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1331 48e9d457 2021-03-06 op wmove(body, line, 0);
1332 9a25f829 2021-03-14 op print_vline(vl);
1334 48e9d457 2021-03-06 op if (line == body_lines)
1340 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1342 7953dd72 2021-03-07 op if (clminibufev_set)
1343 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1344 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1345 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1346 7953dd72 2021-03-07 op clminibufev_set = 1;
1348 bf992581 2021-03-12 op free(ministate.curmesg);
1350 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1351 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1352 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1354 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1355 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1356 9cb0f9ce 2021-03-10 op wrefresh(body);
1357 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1359 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1360 9cb0f9ce 2021-03-10 op wrefresh(body);
1365 740f578b 2021-03-15 op message(const char *fmt, ...)
1369 740f578b 2021-03-15 op va_start(ap, fmt);
1370 740f578b 2021-03-15 op vmessage(fmt, ap);
1375 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1377 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1379 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1380 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1381 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1385 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1387 8af5e5ed 2021-03-08 op struct tab *tab = d;
1389 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1391 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1392 8af5e5ed 2021-03-08 op wrefresh(modeline);
1394 8af5e5ed 2021-03-08 op wrefresh(body);
1395 9ca15951 2021-03-09 op if (in_minibuffer)
1396 9ca15951 2021-03-09 op wrefresh(minibuf);
1398 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1402 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1404 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1406 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1407 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1408 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1410 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1412 43a1b8d0 2021-03-09 op wrefresh(modeline);
1413 43a1b8d0 2021-03-09 op wrefresh(body);
1414 9ca15951 2021-03-09 op if (in_minibuffer)
1415 9ca15951 2021-03-09 op wrefresh(minibuf);
1419 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1421 43a1b8d0 2021-03-09 op empty_vlist(tab);
1422 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1423 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1424 8af5e5ed 2021-03-08 op load_url(tab, url);
1426 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1427 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1428 43a1b8d0 2021-03-09 op redraw_tab(tab);
1432 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1433 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1435 9ca15951 2021-03-09 op in_minibuffer = 1;
1436 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1437 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1439 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1441 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1442 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1443 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1444 9ca15951 2021-03-09 op ministate.off = 0;
1445 bb4e4ddf 2021-03-12 op ministate.len = 0;
1446 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1448 22268e11 2021-03-11 op ministate.history = hist;
1449 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1450 22268e11 2021-03-11 op ministate.hist_off = 0;
1454 b360ebb3 2021-03-10 op exit_minibuffer(void)
1456 156f1501 2021-03-13 op werase(minibuf);
1458 9ca15951 2021-03-09 op in_minibuffer = 0;
1459 9ca15951 2021-03-09 op base_map = &global_map;
1460 9ca15951 2021-03-09 op current_map = &global_map;
1464 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1466 5cd2ebb1 2021-03-11 op struct tab *t;
1468 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1469 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1472 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1475 b1df9b71 2021-03-12 op static struct tab *
1476 1b81bc33 2021-03-15 op new_tab(const char *url)
1478 754622a2 2021-03-15 op struct tab *tab;
1480 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1483 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1485 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1487 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1488 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1490 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1491 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1493 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1495 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1499 bcb0b073 2021-03-07 op event_loopbreak();
1500 b1df9b71 2021-03-12 op return NULL;
1504 5e11c00c 2021-03-02 op ui_init(void)
1506 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1508 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1509 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1511 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1513 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1514 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1515 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1517 9ca15951 2021-03-09 op base_map = &global_map;
1518 f832146f 2021-03-09 op current_map = &global_map;
1519 f832146f 2021-03-09 op load_default_keys();
1526 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1528 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1530 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1532 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1534 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1537 48e9d457 2021-03-06 op body_lines = LINES-3;
1538 48e9d457 2021-03-06 op body_cols = COLS;
1540 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1541 48e9d457 2021-03-06 op scrollok(body, TRUE);
1543 5e11c00c 2021-03-02 op /* non-blocking input */
1544 48e9d457 2021-03-06 op wtimeout(body, 0);
1546 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1548 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1549 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1551 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1552 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1554 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
1560 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1562 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1563 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1567 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1569 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1572 1d08c280 2021-03-06 op wrap_page(tab);
1573 5e11c00c 2021-03-02 op redraw_tab(tab);
1577 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1579 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1580 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1582 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1583 22268e11 2021-03-11 op &ir_history);
1584 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1585 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1586 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1590 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1594 740f578b 2021-03-15 op va_start(ap, fmt);
1595 740f578b 2021-03-15 op vmessage(fmt, ap);
1600 5e11c00c 2021-03-02 op ui_end(void)