Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
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.
7 5e11c00c 2021-03-02 op *
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.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 1d08c280 2021-03-06 op /*
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
19 1d08c280 2021-03-06 op *
20 1d08c280 2021-03-06 op * Text scrolling
21 1d08c280 2021-03-06 op * ==============
22 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!
28 1d08c280 2021-03-06 op *
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''.
32 1d08c280 2021-03-06 op *
33 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
34 1d08c280 2021-03-06 op *
35 1d08c280 2021-03-06 op */
36 1d08c280 2021-03-06 op
37 5e11c00c 2021-03-02 op #include <telescope.h>
38 5e11c00c 2021-03-02 op
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>
48 5e11c00c 2021-03-02 op
49 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
50 5e11c00c 2021-03-02 op
51 1b81bc33 2021-03-15 op #define NEW_TAB_URL "about:new"
52 1b81bc33 2021-03-15 op
53 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
54 5e11c00c 2021-03-02 op
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 *);
58 9ca15951 2021-03-09 op
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_backward_char(struct tab*);
62 88597eaf 2021-03-17 op static void cmd_forward_char(struct tab*);
63 852d03e8 2021-03-17 op static void cmd_backward_paragraph(struct tab*);
64 852d03e8 2021-03-17 op static void cmd_forward_paragraph(struct tab*);
65 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
66 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
67 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
68 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
69 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
70 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
71 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
72 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
73 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
74 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
75 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
76 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
77 a8c28919 2021-03-16 op static void cmd_previous_button(struct tab*);
78 a8c28919 2021-03-16 op static void cmd_next_button(struct tab*);
79 2051e653 2021-03-13 op static void cmd_previous_page(struct tab*);
80 2051e653 2021-03-13 op static void cmd_next_page(struct tab*);
81 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
82 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
83 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
84 fea845b6 2021-03-15 op static void cmd_tab_close_other(struct tab*);
85 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
86 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
87 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
88 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
89 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
90 740f578b 2021-03-15 op static void cmd_bookmark_page(struct tab*);
91 de0b2139 2021-03-15 op static void cmd_goto_bookmarks(struct tab*);
92 9ca15951 2021-03-09 op
93 9ca15951 2021-03-09 op static void global_key_unbound(void);
94 9ca15951 2021-03-09 op
95 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
96 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
97 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
98 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
99 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
100 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
101 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
102 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
103 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
104 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
105 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
106 b360ebb3 2021-03-10 op
107 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
108 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
109 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
110 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
111 b360ebb3 2021-03-10 op static void eecmd_select(void);
112 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
113 5cd2ebb1 2021-03-11 op static void ir_select(void);
114 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
115 5cd2ebb1 2021-03-11 op static void lu_select(void);
116 740f578b 2021-03-15 op static void bp_select(void);
117 9ca15951 2021-03-09 op
118 9a25f829 2021-03-14 op static struct vline *nth_line(struct tab*, size_t);
119 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
120 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
121 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
122 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
123 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
124 9a25f829 2021-03-14 op static void print_vline(struct vline*);
125 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
126 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
127 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
128 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
129 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
130 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
131 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
132 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
133 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
134 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
135 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
136 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
137 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
138 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
139 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
140 941b3761 2021-03-18 op static void usage(void);
141 5e11c00c 2021-03-02 op
142 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
143 9ca15951 2021-03-09 op
144 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
145 48e9d457 2021-03-06 op static int body_lines, body_cols;
146 48e9d457 2021-03-06 op
147 48e9d457 2021-03-06 op static struct event clminibufev;
148 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
149 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
150 48e9d457 2021-03-06 op
151 bcb0b073 2021-03-07 op static uint32_t tab_counter;
152 bcb0b073 2021-03-07 op
153 7c7d7bb7 2021-03-10 op static char keybuf[64];
154 9ca15951 2021-03-09 op
155 9ca15951 2021-03-09 op struct kmap global_map,
156 fa3fd864 2021-03-10 op minibuffer_map,
157 9ca15951 2021-03-09 op *current_map,
158 9ca15951 2021-03-09 op *base_map;
159 9ca15951 2021-03-09 op
160 3148eeac 2021-03-13 op static struct histhead eecmd_history,
161 22268e11 2021-03-11 op ir_history,
162 22268e11 2021-03-11 op lu_history;
163 22268e11 2021-03-11 op
164 9ca15951 2021-03-09 op static int in_minibuffer;
165 9ca15951 2021-03-09 op
166 9ca15951 2021-03-09 op static struct {
167 9cb0f9ce 2021-03-10 op char *curmesg;
168 9cb0f9ce 2021-03-10 op
169 040fbdf8 2021-03-10 op char buf[1025];
170 9ca15951 2021-03-09 op size_t off, len;
171 5cd2ebb1 2021-03-11 op char prompt[32];
172 b360ebb3 2021-03-10 op void (*donefn)(void);
173 b360ebb3 2021-03-10 op void (*abortfn)(void);
174 22268e11 2021-03-11 op
175 3148eeac 2021-03-13 op struct histhead *history;
176 3148eeac 2021-03-13 op struct hist *hist_cur;
177 22268e11 2021-03-11 op size_t hist_off;
178 9ca15951 2021-03-09 op } ministate;
179 9ca15951 2021-03-09 op
180 f95d82dd 2021-03-11 op struct lineprefix {
181 f95d82dd 2021-03-11 op const char *prfx1;
182 f95d82dd 2021-03-11 op const char *prfx2;
183 f95d82dd 2021-03-11 op } line_prefixes[] = {
184 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
185 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
186 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
187 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
188 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
189 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
190 71d5deec 2021-03-19 op [LINE_QUOTE] = { "> ", " " },
191 803ff456 2021-03-17 op [LINE_PRE_START] = { "```", " " },
192 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
193 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
194 d3d56d8a 2021-03-11 op };
195 d3d56d8a 2021-03-11 op
196 31840026 2021-03-16 op static struct line_face {
197 803ff456 2021-03-17 op int prefix_prop;
198 803ff456 2021-03-17 op int text_prop;
199 d3d56d8a 2021-03-11 op } line_faces[] = {
200 803ff456 2021-03-17 op [LINE_TEXT] = { 0, 0 },
201 803ff456 2021-03-17 op [LINE_LINK] = { 0, A_UNDERLINE },
202 803ff456 2021-03-17 op [LINE_TITLE_1] = { A_BOLD, A_BOLD },
203 803ff456 2021-03-17 op [LINE_TITLE_2] = { A_BOLD, A_BOLD },
204 803ff456 2021-03-17 op [LINE_TITLE_3] = { A_BOLD, A_BOLD },
205 803ff456 2021-03-17 op [LINE_ITEM] = { 0, 0 },
206 803ff456 2021-03-17 op [LINE_QUOTE] = { 0, A_DIM },
207 803ff456 2021-03-17 op [LINE_PRE_START] = { 0, 0 },
208 803ff456 2021-03-17 op [LINE_PRE_CONTENT] = { 0, 0 },
209 803ff456 2021-03-17 op [LINE_PRE_END] = { 0, 0 },
210 cb6c7aa0 2021-03-16 op };
211 cb6c7aa0 2021-03-16 op
212 cb6c7aa0 2021-03-16 op static struct tab_face {
213 cb6c7aa0 2021-03-16 op int background, tab, current_tab;
214 cb6c7aa0 2021-03-16 op } tab_face = {
215 cb6c7aa0 2021-03-16 op A_REVERSE, A_REVERSE, A_NORMAL
216 f95d82dd 2021-03-11 op };
217 f95d82dd 2021-03-11 op
218 65d9b3ca 2021-03-14 op static void
219 65d9b3ca 2021-03-14 op empty_vlist(struct tab *tab)
220 65d9b3ca 2021-03-14 op {
221 65d9b3ca 2021-03-14 op struct vline *vl, *t;
222 65d9b3ca 2021-03-14 op
223 452589f7 2021-03-16 op tab->s.current_line = NULL;
224 65d9b3ca 2021-03-14 op tab->s.line_max = 0;
225 65d9b3ca 2021-03-14 op
226 65d9b3ca 2021-03-14 op TAILQ_FOREACH_SAFE(vl, &tab->s.head, vlines, t) {
227 65d9b3ca 2021-03-14 op TAILQ_REMOVE(&tab->s.head, vl, vlines);
228 65d9b3ca 2021-03-14 op free(vl->line);
229 65d9b3ca 2021-03-14 op free(vl);
230 65d9b3ca 2021-03-14 op }
231 65d9b3ca 2021-03-14 op }
232 65d9b3ca 2021-03-14 op
233 f832146f 2021-03-09 op static inline void
234 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
235 f832146f 2021-03-09 op {
236 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
237 67c8ed7f 2021-03-13 op _exit(1);
238 f832146f 2021-03-09 op }
239 f832146f 2021-03-09 op
240 9ca15951 2021-03-09 op static inline void
241 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
242 9ca15951 2021-03-09 op {
243 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
244 67c8ed7f 2021-03-13 op _exit(1);
245 9ca15951 2021-03-09 op }
246 9ca15951 2021-03-09 op
247 f832146f 2021-03-09 op static void
248 f832146f 2021-03-09 op load_default_keys(void)
249 f832146f 2021-03-09 op {
250 9ca15951 2021-03-09 op /* === global map === */
251 9ca15951 2021-03-09 op
252 f832146f 2021-03-09 op /* emacs */
253 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
254 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
255 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
256 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
257 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
258 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
259 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
260 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
261 f832146f 2021-03-09 op
262 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
263 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
264 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
265 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
266 f832146f 2021-03-09 op
267 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
268 f832146f 2021-03-09 op
269 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
270 3b4f9e49 2021-03-11 op
271 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
272 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
273 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
274 9ca15951 2021-03-09 op
275 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
276 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
277 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
278 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
279 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
280 7c7d7bb7 2021-03-10 op
281 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
282 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
283 e19f9a04 2021-03-11 op
284 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
285 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
286 2051e653 2021-03-13 op
287 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
288 de0b2139 2021-03-15 op global_set_key("<f7> <f7>", cmd_goto_bookmarks);
289 740f578b 2021-03-15 op
290 f832146f 2021-03-09 op /* vi/vi-like */
291 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
292 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
293 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
294 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
295 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
296 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
297 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
298 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
299 f832146f 2021-03-09 op
300 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
301 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
302 f832146f 2021-03-09 op
303 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
304 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
305 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
306 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
307 a02335e9 2021-03-15 op
308 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
309 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
310 e19f9a04 2021-03-11 op
311 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
312 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
313 2051e653 2021-03-13 op
314 f832146f 2021-03-09 op /* tmp */
315 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
316 f832146f 2021-03-09 op
317 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
318 3b4f9e49 2021-03-11 op
319 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
320 9ca15951 2021-03-09 op
321 f832146f 2021-03-09 op /* cua */
322 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
323 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
324 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
325 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
326 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
327 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
328 f832146f 2021-03-09 op
329 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
330 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
331 2051e653 2021-03-13 op
332 f832146f 2021-03-09 op /* "ncurses standard" */
333 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
334 f832146f 2021-03-09 op
335 f832146f 2021-03-09 op /* global */
336 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
337 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
338 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
339 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
340 9ca15951 2021-03-09 op
341 fa3fd864 2021-03-10 op /* === minibuffer map === */
342 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
343 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
344 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
345 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
346 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
347 9ca15951 2021-03-09 op
348 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
349 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
350 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
351 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
352 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
353 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
354 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
355 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
356 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
357 22268e11 2021-03-11 op
358 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
359 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
360 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
361 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
362 1d08c280 2021-03-06 op }
363 1d08c280 2021-03-06 op
364 48e9d457 2021-03-06 op static void
365 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
366 48e9d457 2021-03-06 op {
367 452589f7 2021-03-16 op struct vline *vl;
368 452589f7 2021-03-16 op const char *prfx;
369 452589f7 2021-03-16 op
370 452589f7 2021-03-16 op vl =tab->s.current_line;
371 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
372 452589f7 2021-03-16 op tab->s.curs_x = tab->s.line_x = 0;
373 452589f7 2021-03-16 op else
374 452589f7 2021-03-16 op tab->s.line_x = MIN(tab->s.line_x, strlen(vl->line));
375 452589f7 2021-03-16 op
376 452589f7 2021-03-16 op if (vl != NULL) {
377 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
378 452589f7 2021-03-16 op tab->s.curs_x = tab->s.line_x + strlen(prfx);
379 452589f7 2021-03-16 op }
380 452589f7 2021-03-16 op
381 65d9b3ca 2021-03-14 op wmove(body, tab->s.curs_y, tab->s.curs_x);
382 48e9d457 2021-03-06 op }
383 1d08c280 2021-03-06 op
384 1d08c280 2021-03-06 op static void
385 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
386 1d08c280 2021-03-06 op {
387 452589f7 2021-03-16 op struct vline *vl;
388 452589f7 2021-03-16 op
389 452589f7 2021-03-16 op if (tab->s.current_line == NULL
390 452589f7 2021-03-16 op || (vl = TAILQ_PREV(tab->s.current_line, vhead, vlines)) == NULL)
391 452589f7 2021-03-16 op return;
392 452589f7 2021-03-16 op
393 65d9b3ca 2021-03-14 op if (--tab->s.curs_y < 0) {
394 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
395 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
396 452589f7 2021-03-16 op return;
397 4dd664ce 2021-03-06 op }
398 4dd664ce 2021-03-06 op
399 452589f7 2021-03-16 op tab->s.current_line = vl;
400 48e9d457 2021-03-06 op restore_cursor(tab);
401 1d08c280 2021-03-06 op }
402 1d08c280 2021-03-06 op
403 1d08c280 2021-03-06 op static void
404 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
405 1d08c280 2021-03-06 op {
406 452589f7 2021-03-16 op struct vline *vl;
407 452589f7 2021-03-16 op
408 452589f7 2021-03-16 op if (tab->s.current_line == NULL
409 452589f7 2021-03-16 op || (vl = TAILQ_NEXT(tab->s.current_line, vlines)) == NULL)
410 fed61466 2021-03-11 op return;
411 fed61466 2021-03-11 op
412 65d9b3ca 2021-03-14 op if (++tab->s.curs_y > body_lines-1) {
413 65d9b3ca 2021-03-14 op tab->s.curs_y = body_lines-1;
414 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
415 452589f7 2021-03-16 op return;
416 4dd664ce 2021-03-06 op }
417 4dd664ce 2021-03-06 op
418 452589f7 2021-03-16 op tab->s.current_line = vl;
419 48e9d457 2021-03-06 op restore_cursor(tab);
420 1d08c280 2021-03-06 op }
421 1d08c280 2021-03-06 op
422 1d08c280 2021-03-06 op static void
423 88597eaf 2021-03-17 op cmd_backward_char(struct tab *tab)
424 1d08c280 2021-03-06 op {
425 88597eaf 2021-03-17 op if (tab->s.line_x != 0)
426 88597eaf 2021-03-17 op tab->s.line_x--;
427 48e9d457 2021-03-06 op restore_cursor(tab);
428 1d08c280 2021-03-06 op }
429 1d08c280 2021-03-06 op
430 1d08c280 2021-03-06 op static void
431 88597eaf 2021-03-17 op cmd_forward_char(struct tab *tab)
432 1d08c280 2021-03-06 op {
433 88597eaf 2021-03-17 op tab->s.line_x++;
434 a329982b 2021-03-11 op restore_cursor(tab);
435 852d03e8 2021-03-17 op }
436 852d03e8 2021-03-17 op
437 852d03e8 2021-03-17 op static void
438 852d03e8 2021-03-17 op cmd_backward_paragraph(struct tab *tab)
439 852d03e8 2021-03-17 op {
440 852d03e8 2021-03-17 op do {
441 852d03e8 2021-03-17 op if (tab->s.current_line == NULL ||
442 852d03e8 2021-03-17 op tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
443 852d03e8 2021-03-17 op message("No previous paragraph");
444 852d03e8 2021-03-17 op return;
445 852d03e8 2021-03-17 op }
446 852d03e8 2021-03-17 op cmd_previous_line(tab);
447 852d03e8 2021-03-17 op } while (tab->s.current_line->line != NULL ||
448 852d03e8 2021-03-17 op tab->s.current_line->parent->type != LINE_TEXT);
449 852d03e8 2021-03-17 op }
450 852d03e8 2021-03-17 op
451 852d03e8 2021-03-17 op static void
452 852d03e8 2021-03-17 op cmd_forward_paragraph(struct tab *tab)
453 852d03e8 2021-03-17 op {
454 852d03e8 2021-03-17 op do {
455 852d03e8 2021-03-17 op if (tab->s.current_line == NULL ||
456 852d03e8 2021-03-17 op tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
457 852d03e8 2021-03-17 op message("No next paragraph");
458 852d03e8 2021-03-17 op return;
459 852d03e8 2021-03-17 op }
460 852d03e8 2021-03-17 op cmd_next_line(tab);
461 852d03e8 2021-03-17 op } while (tab->s.current_line->line != NULL ||
462 852d03e8 2021-03-17 op tab->s.current_line->parent->type != LINE_TEXT);
463 a329982b 2021-03-11 op }
464 a329982b 2021-03-11 op
465 a329982b 2021-03-11 op static void
466 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
467 a329982b 2021-03-11 op {
468 452589f7 2021-03-16 op tab->s.line_x = 0;
469 a329982b 2021-03-11 op restore_cursor(tab);
470 a329982b 2021-03-11 op }
471 a329982b 2021-03-11 op
472 a329982b 2021-03-11 op static void
473 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
474 a329982b 2021-03-11 op {
475 9a25f829 2021-03-14 op struct vline *vl;
476 a329982b 2021-03-11 op
477 452589f7 2021-03-16 op vl = tab->s.current_line;
478 452589f7 2021-03-16 op if (vl->line == NULL)
479 452589f7 2021-03-16 op return;
480 452589f7 2021-03-16 op tab->s.line_x = body_cols;
481 48e9d457 2021-03-06 op restore_cursor(tab);
482 1d08c280 2021-03-06 op }
483 1d08c280 2021-03-06 op
484 1d08c280 2021-03-06 op static void
485 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
486 1d08c280 2021-03-06 op {
487 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
488 1d08c280 2021-03-06 op }
489 1d08c280 2021-03-06 op
490 1d08c280 2021-03-06 op static void
491 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
492 1d08c280 2021-03-06 op {
493 9a25f829 2021-03-14 op struct vline *vl;
494 48e9d457 2021-03-06 op
495 65d9b3ca 2021-03-14 op if (tab->s.line_off == 0)
496 48e9d457 2021-03-06 op return;
497 48e9d457 2021-03-06 op
498 65d9b3ca 2021-03-14 op vl = nth_line(tab, --tab->s.line_off);
499 48e9d457 2021-03-06 op wscrl(body, -1);
500 48e9d457 2021-03-06 op wmove(body, 0, 0);
501 9a25f829 2021-03-14 op print_vline(vl);
502 452589f7 2021-03-16 op
503 452589f7 2021-03-16 op tab->s.current_line = TAILQ_PREV(tab->s.current_line, vhead, vlines);
504 1d08c280 2021-03-06 op }
505 1d08c280 2021-03-06 op
506 1d08c280 2021-03-06 op static void
507 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
508 1d08c280 2021-03-06 op {
509 9a25f829 2021-03-14 op struct vline *vl;
510 48e9d457 2021-03-06 op
511 452589f7 2021-03-16 op vl = tab->s.current_line;
512 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
513 48e9d457 2021-03-06 op return;
514 452589f7 2021-03-16 op tab->s.current_line = vl;
515 48e9d457 2021-03-06 op
516 65d9b3ca 2021-03-14 op tab->s.line_off++;
517 48e9d457 2021-03-06 op wscrl(body, 1);
518 48e9d457 2021-03-06 op
519 754622a2 2021-03-15 op if (tab->s.line_max - tab->s.line_off < (size_t)body_lines)
520 48e9d457 2021-03-06 op return;
521 48e9d457 2021-03-06 op
522 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off + body_lines-1);
523 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
524 9a25f829 2021-03-14 op print_vline(vl);
525 ed44414d 2021-03-09 op }
526 ed44414d 2021-03-09 op
527 ed44414d 2021-03-09 op static void
528 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
529 ed44414d 2021-03-09 op {
530 ed44414d 2021-03-09 op size_t off;
531 ed44414d 2021-03-09 op
532 ed44414d 2021-03-09 op off = body_lines+1;
533 ed44414d 2021-03-09 op
534 ed44414d 2021-03-09 op for (; off > 0; --off)
535 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
536 1d08c280 2021-03-06 op }
537 1d08c280 2021-03-06 op
538 1d08c280 2021-03-06 op static void
539 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
540 ed44414d 2021-03-09 op {
541 338eecdc 2021-03-12 op size_t off;
542 ed44414d 2021-03-09 op
543 338eecdc 2021-03-12 op off = body_lines+1;
544 ed44414d 2021-03-09 op
545 338eecdc 2021-03-12 op for (; off > 0; --off)
546 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
547 e19f9a04 2021-03-11 op }
548 e19f9a04 2021-03-11 op
549 e19f9a04 2021-03-11 op static void
550 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
551 e19f9a04 2021-03-11 op {
552 452589f7 2021-03-16 op tab->s.current_line = TAILQ_FIRST(&tab->s.head);
553 65d9b3ca 2021-03-14 op tab->s.line_off = 0;
554 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
555 452589f7 2021-03-16 op tab->s.line_x = 0;
556 452589f7 2021-03-16 op restore_cursor(tab);
557 e19f9a04 2021-03-11 op redraw_body(tab);
558 e19f9a04 2021-03-11 op }
559 e19f9a04 2021-03-11 op
560 e19f9a04 2021-03-11 op static void
561 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
562 e19f9a04 2021-03-11 op {
563 e19f9a04 2021-03-11 op ssize_t off;
564 e19f9a04 2021-03-11 op
565 65d9b3ca 2021-03-14 op off = tab->s.line_max - body_lines;
566 e19f9a04 2021-03-11 op off = MAX(0, off);
567 e19f9a04 2021-03-11 op
568 65d9b3ca 2021-03-14 op tab->s.line_off = off;
569 452589f7 2021-03-16 op tab->s.curs_y = MIN((size_t)body_lines, tab->s.line_max-1);
570 e19f9a04 2021-03-11 op
571 452589f7 2021-03-16 op tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
572 452589f7 2021-03-16 op tab->s.line_x = body_cols;
573 452589f7 2021-03-16 op restore_cursor(tab);
574 e19f9a04 2021-03-11 op redraw_body(tab);
575 ed44414d 2021-03-09 op }
576 ed44414d 2021-03-09 op
577 ed44414d 2021-03-09 op static void
578 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
579 1d08c280 2021-03-06 op {
580 1d08c280 2021-03-06 op event_loopbreak();
581 1d08c280 2021-03-06 op }
582 1d08c280 2021-03-06 op
583 1d08c280 2021-03-06 op static void
584 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
585 2a4ad912 2021-03-08 op {
586 9a25f829 2021-03-14 op struct vline *vl;
587 2a4ad912 2021-03-08 op size_t nth;
588 2a4ad912 2021-03-08 op
589 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
590 65d9b3ca 2021-03-14 op if (nth >= tab->s.line_max)
591 2a4ad912 2021-03-08 op return;
592 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
593 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
594 43a1b8d0 2021-03-09 op return;
595 43a1b8d0 2021-03-09 op
596 9a25f829 2021-03-14 op load_url_in_tab(tab, vl->parent->alt);
597 3b4f9e49 2021-03-11 op }
598 3b4f9e49 2021-03-11 op
599 3b4f9e49 2021-03-11 op static void
600 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
601 b1df9b71 2021-03-12 op {
602 9a25f829 2021-03-14 op struct vline *vl;
603 b1df9b71 2021-03-12 op size_t nth;
604 b1df9b71 2021-03-12 op
605 65d9b3ca 2021-03-14 op nth = tab->s.line_off + tab->s.curs_y;
606 65d9b3ca 2021-03-14 op if (nth > tab->s.line_max)
607 b1df9b71 2021-03-12 op return;
608 9a25f829 2021-03-14 op vl = nth_line(tab, nth);
609 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
610 b1df9b71 2021-03-12 op return;
611 b1df9b71 2021-03-12 op
612 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
613 a8c28919 2021-03-16 op }
614 a8c28919 2021-03-16 op
615 a8c28919 2021-03-16 op static void
616 a8c28919 2021-03-16 op cmd_previous_button(struct tab *tab)
617 a8c28919 2021-03-16 op {
618 a8c28919 2021-03-16 op do {
619 a8c28919 2021-03-16 op if (tab->s.current_line == NULL ||
620 a8c28919 2021-03-16 op tab->s.current_line == TAILQ_FIRST(&tab->s.head)) {
621 a8c28919 2021-03-16 op message("No previous link");
622 a8c28919 2021-03-16 op return;
623 a8c28919 2021-03-16 op }
624 a8c28919 2021-03-16 op cmd_previous_line(tab);
625 a8c28919 2021-03-16 op } while (tab->s.current_line->parent->type != LINE_LINK);
626 a8c28919 2021-03-16 op }
627 a8c28919 2021-03-16 op
628 a8c28919 2021-03-16 op static void
629 a8c28919 2021-03-16 op cmd_next_button(struct tab *tab)
630 a8c28919 2021-03-16 op {
631 a8c28919 2021-03-16 op do {
632 a8c28919 2021-03-16 op if (tab->s.current_line == NULL ||
633 a8c28919 2021-03-16 op tab->s.current_line == TAILQ_LAST(&tab->s.head, vhead)) {
634 a8c28919 2021-03-16 op message("No next link");
635 a8c28919 2021-03-16 op return;
636 a8c28919 2021-03-16 op }
637 a8c28919 2021-03-16 op cmd_next_line(tab);
638 a8c28919 2021-03-16 op } while (tab->s.current_line->parent->type != LINE_LINK);
639 2051e653 2021-03-13 op }
640 2051e653 2021-03-13 op
641 2051e653 2021-03-13 op static void
642 2051e653 2021-03-13 op cmd_previous_page(struct tab *tab)
643 2051e653 2021-03-13 op {
644 2051e653 2021-03-13 op if (!load_previous_page(tab))
645 2051e653 2021-03-13 op message("No previous page");
646 c40c2250 2021-03-14 op else
647 c40c2250 2021-03-14 op start_loading_anim(tab);
648 2051e653 2021-03-13 op }
649 2051e653 2021-03-13 op
650 2051e653 2021-03-13 op static void
651 2051e653 2021-03-13 op cmd_next_page(struct tab *tab)
652 2051e653 2021-03-13 op {
653 2051e653 2021-03-13 op if (!load_next_page(tab))
654 2051e653 2021-03-13 op message("No next page");
655 c40c2250 2021-03-14 op else
656 c40c2250 2021-03-14 op start_loading_anim(tab);
657 b1df9b71 2021-03-12 op }
658 b1df9b71 2021-03-12 op
659 b1df9b71 2021-03-12 op static void
660 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
661 3b4f9e49 2021-03-11 op {
662 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
663 9ca15951 2021-03-09 op }
664 9ca15951 2021-03-09 op
665 9ca15951 2021-03-09 op static void
666 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
667 9ca15951 2021-03-09 op {
668 9ca15951 2021-03-09 op size_t len;
669 9ca15951 2021-03-09 op
670 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
671 22268e11 2021-03-11 op &eecmd_history);
672 9ca15951 2021-03-09 op
673 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
674 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
675 9ca15951 2021-03-09 op
676 9ca15951 2021-03-09 op if (thiskey.meta)
677 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
678 9ca15951 2021-03-09 op
679 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
680 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
681 9ca15951 2021-03-09 op }
682 9ca15951 2021-03-09 op
683 9ca15951 2021-03-09 op static void
684 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
685 7c7d7bb7 2021-03-10 op {
686 7c7d7bb7 2021-03-10 op struct tab *t;
687 a777f81f 2021-03-10 op
688 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
689 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
690 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
691 a777f81f 2021-03-10 op return;
692 a777f81f 2021-03-10 op }
693 a777f81f 2021-03-10 op
694 d87ffbdc 2021-03-17 op if (evtimer_pending(&tab->s.loadingev, NULL))
695 d87ffbdc 2021-03-17 op evtimer_del(&tab->s.loadingev);
696 d87ffbdc 2021-03-17 op
697 7c7d7bb7 2021-03-10 op stop_tab(tab);
698 7c7d7bb7 2021-03-10 op
699 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
700 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
701 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
702 7c7d7bb7 2021-03-10 op free(tab);
703 9d24c64f 2021-03-17 op
704 9d24c64f 2021-03-17 op switch_to_tab(t);
705 9ca15951 2021-03-09 op }
706 9ca15951 2021-03-09 op
707 9ca15951 2021-03-09 op static void
708 fea845b6 2021-03-15 op cmd_tab_close_other(struct tab *tab)
709 fea845b6 2021-03-15 op {
710 fea845b6 2021-03-15 op struct tab *t, *i;
711 fea845b6 2021-03-15 op
712 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
713 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
714 fea845b6 2021-03-15 op continue;
715 fea845b6 2021-03-15 op
716 fea845b6 2021-03-15 op stop_tab(t);
717 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
718 fea845b6 2021-03-15 op free(t);
719 fea845b6 2021-03-15 op }
720 fea845b6 2021-03-15 op }
721 fea845b6 2021-03-15 op
722 fea845b6 2021-03-15 op static void
723 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
724 7c7d7bb7 2021-03-10 op {
725 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
726 7c7d7bb7 2021-03-10 op }
727 7c7d7bb7 2021-03-10 op
728 7c7d7bb7 2021-03-10 op static void
729 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
730 7c7d7bb7 2021-03-10 op {
731 7c7d7bb7 2021-03-10 op struct tab *t;
732 7c7d7bb7 2021-03-10 op
733 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
734 7c7d7bb7 2021-03-10 op
735 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
736 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
737 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
738 7c7d7bb7 2021-03-10 op }
739 7c7d7bb7 2021-03-10 op
740 7c7d7bb7 2021-03-10 op static void
741 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
742 7c7d7bb7 2021-03-10 op {
743 7c7d7bb7 2021-03-10 op struct tab *t;
744 7c7d7bb7 2021-03-10 op
745 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
746 7c7d7bb7 2021-03-10 op
747 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
748 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
749 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
750 5cd2ebb1 2021-03-11 op }
751 5cd2ebb1 2021-03-11 op
752 5cd2ebb1 2021-03-11 op static void
753 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
754 5cd2ebb1 2021-03-11 op {
755 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
756 22268e11 2021-03-11 op &lu_history);
757 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
758 7c7d7bb7 2021-03-10 op }
759 7c7d7bb7 2021-03-10 op
760 7c7d7bb7 2021-03-10 op static void
761 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
762 5cd2ebb1 2021-03-11 op {
763 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
764 22268e11 2021-03-11 op &lu_history);
765 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
766 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
767 740f578b 2021-03-15 op ministate.off = strlen(tab->hist_cur->h);
768 740f578b 2021-03-15 op ministate.len = ministate.off;
769 740f578b 2021-03-15 op }
770 740f578b 2021-03-15 op
771 740f578b 2021-03-15 op static void
772 740f578b 2021-03-15 op cmd_bookmark_page(struct tab *tab)
773 740f578b 2021-03-15 op {
774 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
775 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
776 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
777 2051e653 2021-03-13 op ministate.off = strlen(tab->hist_cur->h);
778 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
779 de0b2139 2021-03-15 op }
780 de0b2139 2021-03-15 op
781 de0b2139 2021-03-15 op static void
782 de0b2139 2021-03-15 op cmd_goto_bookmarks(struct tab *tab)
783 de0b2139 2021-03-15 op {
784 de0b2139 2021-03-15 op load_url_in_tab(tab, "about:bookmarks");
785 5cd2ebb1 2021-03-11 op }
786 5cd2ebb1 2021-03-11 op
787 5cd2ebb1 2021-03-11 op static void
788 7c7d7bb7 2021-03-10 op global_key_unbound(void)
789 7c7d7bb7 2021-03-10 op {
790 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
791 7c7d7bb7 2021-03-10 op }
792 7c7d7bb7 2021-03-10 op
793 7c7d7bb7 2021-03-10 op static void
794 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
795 d67133bf 2021-03-12 op {
796 3eecee9e 2021-03-12 op minibuffer_taint_hist();
797 3eecee9e 2021-03-12 op
798 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
799 d67133bf 2021-03-12 op return;
800 d67133bf 2021-03-12 op
801 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
802 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
803 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
804 d67133bf 2021-03-12 op ministate.len--;
805 d67133bf 2021-03-12 op }
806 d67133bf 2021-03-12 op
807 d67133bf 2021-03-12 op static void
808 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
809 9ca15951 2021-03-09 op {
810 3eecee9e 2021-03-12 op minibuffer_taint_hist();
811 3eecee9e 2021-03-12 op
812 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
813 9ca15951 2021-03-09 op return;
814 9ca15951 2021-03-09 op
815 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
816 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
817 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
818 9ca15951 2021-03-09 op ministate.off--;
819 9ca15951 2021-03-09 op ministate.len--;
820 9ca15951 2021-03-09 op }
821 9ca15951 2021-03-09 op
822 9ca15951 2021-03-09 op static void
823 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
824 9ca15951 2021-03-09 op {
825 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
826 9ca15951 2021-03-09 op return;
827 9ca15951 2021-03-09 op ministate.off++;
828 9ca15951 2021-03-09 op }
829 9ca15951 2021-03-09 op
830 9ca15951 2021-03-09 op static void
831 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
832 9ca15951 2021-03-09 op {
833 9ca15951 2021-03-09 op if (ministate.off == 0)
834 9ca15951 2021-03-09 op return;
835 9ca15951 2021-03-09 op ministate.off--;
836 9ca15951 2021-03-09 op }
837 9ca15951 2021-03-09 op
838 9ca15951 2021-03-09 op static void
839 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
840 9ca15951 2021-03-09 op {
841 9ca15951 2021-03-09 op ministate.off = ministate.len;
842 9ca15951 2021-03-09 op }
843 9ca15951 2021-03-09 op
844 9ca15951 2021-03-09 op static void
845 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
846 9ca15951 2021-03-09 op {
847 9ca15951 2021-03-09 op ministate.off = 0;
848 9ca15951 2021-03-09 op }
849 9ca15951 2021-03-09 op
850 9ca15951 2021-03-09 op static void
851 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
852 fa3fd864 2021-03-10 op {
853 3eecee9e 2021-03-12 op minibuffer_taint_hist();
854 3eecee9e 2021-03-12 op
855 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
856 fa3fd864 2021-03-10 op return;
857 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
858 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
859 fa3fd864 2021-03-10 op }
860 fa3fd864 2021-03-10 op
861 fa3fd864 2021-03-10 op static void
862 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
863 b360ebb3 2021-03-10 op {
864 b360ebb3 2021-03-10 op ministate.abortfn();
865 b360ebb3 2021-03-10 op }
866 b360ebb3 2021-03-10 op
867 b360ebb3 2021-03-10 op static void
868 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
869 b360ebb3 2021-03-10 op {
870 22268e11 2021-03-11 op minibuffer_taint_hist();
871 b360ebb3 2021-03-10 op ministate.donefn();
872 22268e11 2021-03-11 op }
873 22268e11 2021-03-11 op
874 22268e11 2021-03-11 op static void
875 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
876 22268e11 2021-03-11 op {
877 22268e11 2021-03-11 op if (ministate.history == NULL) {
878 22268e11 2021-03-11 op message("No history");
879 22268e11 2021-03-11 op return;
880 22268e11 2021-03-11 op }
881 22268e11 2021-03-11 op
882 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
883 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
884 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
885 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
886 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
887 22268e11 2021-03-11 op message("No prev item");
888 22268e11 2021-03-11 op } else {
889 22268e11 2021-03-11 op ministate.hist_off--;
890 22268e11 2021-03-11 op }
891 22268e11 2021-03-11 op
892 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
893 22268e11 2021-03-11 op ministate.off = 0;
894 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
895 22268e11 2021-03-11 op }
896 b360ebb3 2021-03-10 op }
897 b360ebb3 2021-03-10 op
898 b360ebb3 2021-03-10 op static void
899 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
900 22268e11 2021-03-11 op {
901 22268e11 2021-03-11 op if (ministate.history == NULL) {
902 22268e11 2021-03-11 op message("No history");
903 22268e11 2021-03-11 op return;
904 22268e11 2021-03-11 op }
905 22268e11 2021-03-11 op
906 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
907 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
908 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
909 22268e11 2021-03-11 op ministate.hist_off = 0;
910 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
911 22268e11 2021-03-11 op message("No next item");
912 22268e11 2021-03-11 op } else {
913 22268e11 2021-03-11 op ministate.hist_off++;
914 22268e11 2021-03-11 op }
915 22268e11 2021-03-11 op
916 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
917 22268e11 2021-03-11 op ministate.off = 0;
918 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
919 22268e11 2021-03-11 op }
920 22268e11 2021-03-11 op }
921 22268e11 2021-03-11 op
922 22268e11 2021-03-11 op static void
923 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
924 22268e11 2021-03-11 op {
925 3148eeac 2021-03-13 op struct hist *hist;
926 22268e11 2021-03-11 op
927 22268e11 2021-03-11 op if (ministate.history == NULL)
928 22268e11 2021-03-11 op return;
929 22268e11 2021-03-11 op
930 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
931 22268e11 2021-03-11 op abort();
932 22268e11 2021-03-11 op
933 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
934 22268e11 2021-03-11 op
935 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
936 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
937 22268e11 2021-03-11 op else
938 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
939 22268e11 2021-03-11 op ministate.history->len++;
940 22268e11 2021-03-11 op }
941 22268e11 2021-03-11 op
942 22268e11 2021-03-11 op /*
943 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
944 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
945 22268e11 2021-03-11 op * navigation" thing.
946 22268e11 2021-03-11 op */
947 22268e11 2021-03-11 op static void
948 22268e11 2021-03-11 op minibuffer_taint_hist(void)
949 22268e11 2021-03-11 op {
950 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
951 22268e11 2021-03-11 op return;
952 22268e11 2021-03-11 op
953 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
954 22268e11 2021-03-11 op ministate.hist_cur = NULL;
955 22268e11 2021-03-11 op }
956 22268e11 2021-03-11 op
957 22268e11 2021-03-11 op static void
958 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
959 9ca15951 2021-03-09 op {
960 22268e11 2021-03-11 op minibuffer_taint_hist();
961 22268e11 2021-03-11 op
962 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
963 9ca15951 2021-03-09 op return;
964 9ca15951 2021-03-09 op
965 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
966 9ca15951 2021-03-09 op
967 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
968 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
969 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
970 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
971 9ca15951 2021-03-09 op ministate.off++;
972 9ca15951 2021-03-09 op ministate.len++;
973 b360ebb3 2021-03-10 op }
974 b360ebb3 2021-03-10 op
975 b360ebb3 2021-03-10 op static void
976 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
977 5cd2ebb1 2021-03-11 op {
978 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
979 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
980 5cd2ebb1 2021-03-11 op global_key_unbound();
981 5cd2ebb1 2021-03-11 op return;
982 5cd2ebb1 2021-03-11 op }
983 5cd2ebb1 2021-03-11 op
984 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
985 5cd2ebb1 2021-03-11 op }
986 5cd2ebb1 2021-03-11 op
987 5cd2ebb1 2021-03-11 op static void
988 b360ebb3 2021-03-10 op eecmd_select(void)
989 b360ebb3 2021-03-10 op {
990 b360ebb3 2021-03-10 op exit_minibuffer();
991 22268e11 2021-03-11 op minibuffer_hist_save_entry();
992 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
993 5cd2ebb1 2021-03-11 op }
994 5cd2ebb1 2021-03-11 op
995 5cd2ebb1 2021-03-11 op static void
996 5cd2ebb1 2021-03-11 op ir_self_insert(void)
997 5cd2ebb1 2021-03-11 op {
998 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
999 5cd2ebb1 2021-03-11 op }
1000 5cd2ebb1 2021-03-11 op
1001 5cd2ebb1 2021-03-11 op static void
1002 5cd2ebb1 2021-03-11 op ir_select(void)
1003 5cd2ebb1 2021-03-11 op {
1004 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1005 5cd2ebb1 2021-03-11 op struct url url;
1006 5cd2ebb1 2021-03-11 op struct tab *tab;
1007 5cd2ebb1 2021-03-11 op
1008 5cd2ebb1 2021-03-11 op tab = current_tab();
1009 5cd2ebb1 2021-03-11 op
1010 5cd2ebb1 2021-03-11 op exit_minibuffer();
1011 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1012 5cd2ebb1 2021-03-11 op
1013 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1014 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
1015 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
1016 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
1017 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1018 5cd2ebb1 2021-03-11 op }
1019 5cd2ebb1 2021-03-11 op
1020 5cd2ebb1 2021-03-11 op static void
1021 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1022 5cd2ebb1 2021-03-11 op {
1023 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
1024 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
1025 5cd2ebb1 2021-03-11 op global_key_unbound();
1026 5cd2ebb1 2021-03-11 op return;
1027 5cd2ebb1 2021-03-11 op }
1028 5cd2ebb1 2021-03-11 op
1029 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1030 5cd2ebb1 2021-03-11 op }
1031 5cd2ebb1 2021-03-11 op
1032 5cd2ebb1 2021-03-11 op static void
1033 5cd2ebb1 2021-03-11 op lu_select(void)
1034 5cd2ebb1 2021-03-11 op {
1035 5cd2ebb1 2021-03-11 op exit_minibuffer();
1036 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1037 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1038 740f578b 2021-03-15 op }
1039 740f578b 2021-03-15 op
1040 740f578b 2021-03-15 op static void
1041 740f578b 2021-03-15 op bp_select(void)
1042 740f578b 2021-03-15 op {
1043 740f578b 2021-03-15 op exit_minibuffer();
1044 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1045 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1046 740f578b 2021-03-15 op else
1047 740f578b 2021-03-15 op message("Abort.");
1048 2a4ad912 2021-03-08 op }
1049 2a4ad912 2021-03-08 op
1050 9a25f829 2021-03-14 op static struct vline *
1051 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
1052 48e9d457 2021-03-06 op {
1053 9a25f829 2021-03-14 op struct vline *vl;
1054 48e9d457 2021-03-06 op size_t i;
1055 48e9d457 2021-03-06 op
1056 48e9d457 2021-03-06 op i = 0;
1057 65d9b3ca 2021-03-14 op TAILQ_FOREACH(vl, &tab->s.head, vlines) {
1058 48e9d457 2021-03-06 op if (i == n)
1059 9a25f829 2021-03-14 op return vl;
1060 48e9d457 2021-03-06 op i++;
1061 48e9d457 2021-03-06 op }
1062 48e9d457 2021-03-06 op
1063 48e9d457 2021-03-06 op /* unreachable */
1064 48e9d457 2021-03-06 op abort();
1065 48e9d457 2021-03-06 op }
1066 48e9d457 2021-03-06 op
1067 5e11c00c 2021-03-02 op static struct tab *
1068 5e11c00c 2021-03-02 op current_tab(void)
1069 5e11c00c 2021-03-02 op {
1070 5e11c00c 2021-03-02 op struct tab *t;
1071 5e11c00c 2021-03-02 op
1072 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1073 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1074 5e11c00c 2021-03-02 op return t;
1075 5e11c00c 2021-03-02 op }
1076 5e11c00c 2021-03-02 op
1077 5e11c00c 2021-03-02 op /* unreachable */
1078 5e11c00c 2021-03-02 op abort();
1079 5e11c00c 2021-03-02 op }
1080 5e11c00c 2021-03-02 op
1081 5e11c00c 2021-03-02 op static void
1082 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
1083 5e11c00c 2021-03-02 op {
1084 f832146f 2021-03-09 op struct keymap *k;
1085 7c7d7bb7 2021-03-10 op const char *keyname;
1086 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
1087 19f1448e 2021-03-08 op
1088 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1089 9ca15951 2021-03-09 op if (thiskey.key == ERR)
1090 5e11c00c 2021-03-02 op return;
1091 9ca15951 2021-03-09 op if (thiskey.key == 27) {
1092 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
1093 5e11c00c 2021-03-02 op
1094 9ca15951 2021-03-09 op thiskey.meta = 1;
1095 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1096 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1097 c314a314 2021-03-11 op thiskey.meta = 0;
1098 9ca15951 2021-03-09 op thiskey.key = 27;
1099 c314a314 2021-03-11 op }
1100 b0ea6f26 2021-03-08 op } else
1101 9ca15951 2021-03-09 op thiskey.meta = 0;
1102 19f1448e 2021-03-08 op
1103 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1104 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1105 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1106 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1107 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
1108 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
1109 7c7d7bb7 2021-03-10 op else {
1110 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
1111 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1112 7c7d7bb7 2021-03-10 op }
1113 7c7d7bb7 2021-03-10 op
1114 9ca15951 2021-03-09 op TAILQ_FOREACH(k, &current_map->m, keymaps) {
1115 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1116 9ca15951 2021-03-09 op k->key == thiskey.key) {
1117 f832146f 2021-03-09 op if (k->fn == NULL)
1118 f832146f 2021-03-09 op current_map = &k->map;
1119 f832146f 2021-03-09 op else {
1120 9ca15951 2021-03-09 op current_map = base_map;
1121 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1122 f832146f 2021-03-09 op k->fn(current_tab());
1123 f832146f 2021-03-09 op }
1124 1d08c280 2021-03-06 op goto done;
1125 1d08c280 2021-03-06 op }
1126 eb259e66 2021-03-02 op }
1127 eb259e66 2021-03-02 op
1128 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1129 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1130 7c7d7bb7 2021-03-10 op else {
1131 7c7d7bb7 2021-03-10 op global_key_unbound();
1132 7c7d7bb7 2021-03-10 op }
1133 7c7d7bb7 2021-03-10 op
1134 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1135 9ca15951 2021-03-09 op current_map = base_map;
1136 1d08c280 2021-03-06 op
1137 1d08c280 2021-03-06 op done:
1138 179f0f58 2021-03-11 op redraw_tab(current_tab());
1139 a6d450c1 2021-03-06 op }
1140 48e9d457 2021-03-06 op
1141 a6d450c1 2021-03-06 op static void
1142 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1143 a6d450c1 2021-03-06 op {
1144 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1145 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1146 9cb0f9ce 2021-03-10 op
1147 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1148 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1149 9cb0f9ce 2021-03-10 op wrefresh(body);
1150 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1151 9cb0f9ce 2021-03-10 op } else {
1152 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1153 9cb0f9ce 2021-03-10 op wrefresh(body);
1154 9cb0f9ce 2021-03-10 op }
1155 5e11c00c 2021-03-02 op }
1156 5e11c00c 2021-03-02 op
1157 5e11c00c 2021-03-02 op static void
1158 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1159 5e11c00c 2021-03-02 op {
1160 1d08c280 2021-03-06 op struct tab *tab;
1161 1d08c280 2021-03-06 op
1162 5e11c00c 2021-03-02 op endwin();
1163 5e11c00c 2021-03-02 op refresh();
1164 5e11c00c 2021-03-02 op clear();
1165 5e11c00c 2021-03-02 op
1166 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1167 48e9d457 2021-03-06 op
1168 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1169 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1170 48e9d457 2021-03-06 op
1171 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1172 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1173 48e9d457 2021-03-06 op
1174 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1175 48e9d457 2021-03-06 op body_lines = LINES-3;
1176 bd9637e9 2021-03-06 op body_cols = COLS;
1177 48e9d457 2021-03-06 op
1178 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1179 48e9d457 2021-03-06 op
1180 1d08c280 2021-03-06 op tab = current_tab();
1181 1d08c280 2021-03-06 op
1182 1d08c280 2021-03-06 op wrap_page(tab);
1183 1d08c280 2021-03-06 op redraw_tab(tab);
1184 eb259e66 2021-03-02 op }
1185 eb259e66 2021-03-02 op
1186 1d08c280 2021-03-06 op static int
1187 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1188 1d08c280 2021-03-06 op {
1189 452589f7 2021-03-16 op struct line *l;
1190 452589f7 2021-03-16 op const struct line *orig;
1191 d511dc85 2021-03-16 op struct vline *vl;
1192 452589f7 2021-03-16 op const char *prfx;
1193 452589f7 2021-03-16 op
1194 452589f7 2021-03-16 op orig = tab->s.current_line == NULL
1195 452589f7 2021-03-16 op ? NULL
1196 452589f7 2021-03-16 op : tab->s.current_line->parent;
1197 452589f7 2021-03-16 op tab->s.current_line = NULL;
1198 452589f7 2021-03-16 op
1199 452589f7 2021-03-16 op tab->s.curs_y = 0;
1200 452589f7 2021-03-16 op tab->s.line_off = 0;
1201 1d08c280 2021-03-06 op
1202 1d08c280 2021-03-06 op empty_vlist(tab);
1203 1d08c280 2021-03-06 op
1204 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1205 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1206 5e11c00c 2021-03-02 op switch (l->type) {
1207 5e11c00c 2021-03-02 op case LINE_TEXT:
1208 5e11c00c 2021-03-02 op case LINE_LINK:
1209 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1210 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1211 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1212 5e11c00c 2021-03-02 op case LINE_ITEM:
1213 5e11c00c 2021-03-02 op case LINE_QUOTE:
1214 5e11c00c 2021-03-02 op case LINE_PRE_START:
1215 5e11c00c 2021-03-02 op case LINE_PRE_END:
1216 65d9b3ca 2021-03-14 op wrap_text(tab, prfx, l, body_cols);
1217 5e11c00c 2021-03-02 op break;
1218 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1219 65d9b3ca 2021-03-14 op hardwrap_text(tab, l, body_cols);
1220 5e11c00c 2021-03-02 op break;
1221 452589f7 2021-03-16 op }
1222 452589f7 2021-03-16 op
1223 452589f7 2021-03-16 op if (orig == l && tab->s.current_line == NULL) {
1224 452589f7 2021-03-16 op tab->s.line_off = tab->s.line_max-1;
1225 452589f7 2021-03-16 op tab->s.current_line = TAILQ_LAST(&tab->s.head, vhead);
1226 d511dc85 2021-03-16 op
1227 d511dc85 2021-03-16 op while (1) {
1228 d511dc85 2021-03-16 op vl = TAILQ_PREV(tab->s.current_line, vhead, vlines);
1229 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1230 d511dc85 2021-03-16 op break;
1231 d511dc85 2021-03-16 op tab->s.current_line = vl;
1232 d511dc85 2021-03-16 op tab->s.line_off--;
1233 d511dc85 2021-03-16 op }
1234 5e11c00c 2021-03-02 op }
1235 5e11c00c 2021-03-02 op }
1236 452589f7 2021-03-16 op
1237 452589f7 2021-03-16 op if (tab->s.current_line == NULL)
1238 452589f7 2021-03-16 op tab->s.current_line = TAILQ_FIRST(&tab->s.head);
1239 452589f7 2021-03-16 op
1240 1d08c280 2021-03-06 op return 1;
1241 1d08c280 2021-03-06 op }
1242 5e11c00c 2021-03-02 op
1243 754622a2 2021-03-15 op static void
1244 9a25f829 2021-03-14 op print_vline(struct vline *vl)
1245 1d08c280 2021-03-06 op {
1246 9a25f829 2021-03-14 op const char *text = vl->line;
1247 768db5bb 2021-03-12 op const char *prfx;
1248 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1249 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1250 bd9637e9 2021-03-06 op
1251 9a25f829 2021-03-14 op if (!vl->flags)
1252 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1253 768db5bb 2021-03-12 op else
1254 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1255 768db5bb 2021-03-12 op
1256 bd9637e9 2021-03-06 op if (text == NULL)
1257 bd9637e9 2021-03-06 op text = "";
1258 bd9637e9 2021-03-06 op
1259 803ff456 2021-03-17 op wattron(body, prefix_face);
1260 803ff456 2021-03-17 op wprintw(body, "%s", prfx);
1261 803ff456 2021-03-17 op wattroff(body, prefix_face);
1262 803ff456 2021-03-17 op
1263 803ff456 2021-03-17 op wattron(body, text_face);
1264 803ff456 2021-03-17 op wprintw(body, "%s", text);
1265 803ff456 2021-03-17 op wattroff(body, text_face);
1266 1d08c280 2021-03-06 op }
1267 1d08c280 2021-03-06 op
1268 1d08c280 2021-03-06 op static void
1269 8af5e5ed 2021-03-08 op redraw_tabline(void)
1270 8af5e5ed 2021-03-08 op {
1271 7c7d7bb7 2021-03-10 op struct tab *tab;
1272 a636f50e 2021-03-16 op size_t toskip;
1273 a636f50e 2021-03-16 op int current, x, y, truncated;
1274 dc5df781 2021-03-13 op const char *title;
1275 119f393c 2021-03-16 op char buf[25];
1276 7c7d7bb7 2021-03-10 op
1277 a636f50e 2021-03-16 op toskip = 0;
1278 a636f50e 2021-03-16 op x = 1;
1279 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1280 a636f50e 2021-03-16 op x += sizeof(buf) + 1;
1281 a636f50e 2021-03-16 op toskip++;
1282 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1283 a636f50e 2021-03-16 op break;
1284 a636f50e 2021-03-16 op }
1285 a636f50e 2021-03-16 op if (x < COLS-2)
1286 a636f50e 2021-03-16 op toskip = 0;
1287 a636f50e 2021-03-16 op else
1288 a636f50e 2021-03-16 op toskip--;
1289 7c7d7bb7 2021-03-10 op
1290 a636f50e 2021-03-16 op werase(tabline);
1291 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1292 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1293 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1294 a636f50e 2021-03-16 op
1295 a636f50e 2021-03-16 op truncated = 0;
1296 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1297 a636f50e 2021-03-16 op if (truncated)
1298 a636f50e 2021-03-16 op break;
1299 a636f50e 2021-03-16 op if (toskip != 0) {
1300 a636f50e 2021-03-16 op toskip--;
1301 a636f50e 2021-03-16 op continue;
1302 a636f50e 2021-03-16 op }
1303 a636f50e 2021-03-16 op
1304 a636f50e 2021-03-16 op getyx(tabline, y, x);
1305 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1306 a636f50e 2021-03-16 op truncated = 1;
1307 a636f50e 2021-03-16 op
1308 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1309 a329982b 2021-03-11 op
1310 dc5df781 2021-03-13 op if (*(title = tab->page.title) == '\0')
1311 2051e653 2021-03-13 op title = tab->hist_cur->h;
1312 dc5df781 2021-03-13 op
1313 119f393c 2021-03-16 op strlcpy(buf, " ", sizeof(buf));
1314 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1315 119f393c 2021-03-16 op /* truncation happens */
1316 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1317 119f393c 2021-03-16 op } else {
1318 119f393c 2021-03-16 op /* pad with spaces */
1319 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1320 119f393c 2021-03-16 op /* nop */ ;
1321 119f393c 2021-03-16 op }
1322 a329982b 2021-03-11 op
1323 a329982b 2021-03-11 op if (current)
1324 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1325 cb6c7aa0 2021-03-16 op else
1326 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1327 119f393c 2021-03-16 op
1328 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1329 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1330 119f393c 2021-03-16 op wprintw(tabline, " ");
1331 cb6c7aa0 2021-03-16 op
1332 cb6c7aa0 2021-03-16 op if (current)
1333 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1334 cb6c7aa0 2021-03-16 op else
1335 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1336 7c7d7bb7 2021-03-10 op }
1337 119f393c 2021-03-16 op
1338 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1339 119f393c 2021-03-16 op for (; x < COLS; ++x)
1340 119f393c 2021-03-16 op waddch(tabline, ' ');
1341 a636f50e 2021-03-16 op if (truncated)
1342 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1343 10346511 2021-03-17 op }
1344 10346511 2021-03-17 op
1345 10346511 2021-03-17 op static inline char
1346 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1347 10346511 2021-03-17 op {
1348 10346511 2021-03-17 op switch (ts) {
1349 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1350 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1351 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1352 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1353 10346511 2021-03-17 op }
1354 8af5e5ed 2021-03-08 op }
1355 8af5e5ed 2021-03-08 op
1356 8af5e5ed 2021-03-08 op static void
1357 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1358 1d08c280 2021-03-06 op {
1359 481340cc 2021-03-11 op double pct;
1360 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1361 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1362 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1363 1d08c280 2021-03-06 op
1364 156f1501 2021-03-13 op werase(modeline);
1365 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1366 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1367 1d08c280 2021-03-06 op
1368 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1369 58c75fab 2021-03-16 op spin[tab->s.loading_anim_step],
1370 10346511 2021-03-17 op trust_status_char(tab->trust),
1371 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1372 481340cc 2021-03-11 op
1373 65d9b3ca 2021-03-14 op pct = (tab->s.line_off + tab->s.curs_y) * 100.0 / tab->s.line_max;
1374 481340cc 2021-03-11 op
1375 754622a2 2021-03-15 op if (tab->s.line_max <= (size_t)body_lines)
1376 481340cc 2021-03-11 op wprintw(modeline, "All ");
1377 65d9b3ca 2021-03-14 op else if (tab->s.line_off == 0)
1378 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1379 65d9b3ca 2021-03-14 op else if (tab->s.line_off + body_lines >= tab->s.line_max)
1380 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1381 481340cc 2021-03-11 op else
1382 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1383 481340cc 2021-03-11 op
1384 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1385 65d9b3ca 2021-03-14 op tab->s.line_off + tab->s.curs_y,
1386 65d9b3ca 2021-03-14 op tab->s.line_max,
1387 2051e653 2021-03-13 op tab->hist_cur->h);
1388 481340cc 2021-03-11 op
1389 48e9d457 2021-03-06 op getyx(modeline, y, x);
1390 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1391 48e9d457 2021-03-06 op
1392 48e9d457 2021-03-06 op (void)y;
1393 48e9d457 2021-03-06 op (void)max_y;
1394 48e9d457 2021-03-06 op
1395 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1396 48e9d457 2021-03-06 op waddstr(modeline, "-");
1397 9ca15951 2021-03-09 op }
1398 9ca15951 2021-03-09 op
1399 9ca15951 2021-03-09 op static void
1400 9ca15951 2021-03-09 op redraw_minibuffer(void)
1401 9ca15951 2021-03-09 op {
1402 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1403 8300dd3c 2021-03-18 op struct tab *tab;
1404 9ca15951 2021-03-09 op
1405 156f1501 2021-03-13 op werase(minibuf);
1406 22268e11 2021-03-11 op if (in_minibuffer) {
1407 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1408 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1409 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1410 22268e11 2021-03-11 op ministate.hist_off + 1,
1411 22268e11 2021-03-11 op ministate.history->len);
1412 9ca15951 2021-03-09 op
1413 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1414 9cb0f9ce 2021-03-10 op
1415 754622a2 2021-03-15 op while (ministate.off - skip > (size_t)COLS / 2) {
1416 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1417 22268e11 2021-03-11 op }
1418 9cb0f9ce 2021-03-10 op
1419 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1420 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1421 22268e11 2021-03-11 op else
1422 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1423 22268e11 2021-03-11 op }
1424 9cb0f9ce 2021-03-10 op
1425 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1426 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1427 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1428 9cb0f9ce 2021-03-10 op else
1429 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1430 9cb0f9ce 2021-03-10 op }
1431 9cb0f9ce 2021-03-10 op
1432 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1433 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1434 8300dd3c 2021-03-18 op
1435 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1436 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1437 8300dd3c 2021-03-18 op tab = current_tab();
1438 8300dd3c 2021-03-18 op if (tab->s.current_line != NULL &&
1439 8300dd3c 2021-03-18 op tab->s.current_line->parent->type == LINE_LINK)
1440 8300dd3c 2021-03-18 op wprintw(minibuf, "%s",
1441 8300dd3c 2021-03-18 op tab->s.current_line->parent->alt);
1442 8300dd3c 2021-03-18 op }
1443 91a72220 2021-03-13 op
1444 2aaf475e 2021-03-13 op if (in_minibuffer)
1445 2aaf475e 2021-03-13 op wmove(minibuf, 0, off_x + ministate.off - skip);
1446 48e9d457 2021-03-06 op }
1447 48e9d457 2021-03-06 op
1448 48e9d457 2021-03-06 op static void
1449 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1450 48e9d457 2021-03-06 op {
1451 e19f9a04 2021-03-11 op redraw_tabline();
1452 e19f9a04 2021-03-11 op redraw_body(tab);
1453 e19f9a04 2021-03-11 op redraw_modeline(tab);
1454 e19f9a04 2021-03-11 op redraw_minibuffer();
1455 e19f9a04 2021-03-11 op
1456 e19f9a04 2021-03-11 op restore_cursor(tab);
1457 e19f9a04 2021-03-11 op wrefresh(tabline);
1458 e19f9a04 2021-03-11 op wrefresh(modeline);
1459 e19f9a04 2021-03-11 op
1460 e19f9a04 2021-03-11 op if (in_minibuffer) {
1461 e19f9a04 2021-03-11 op wrefresh(body);
1462 e19f9a04 2021-03-11 op wrefresh(minibuf);
1463 e19f9a04 2021-03-11 op } else {
1464 e19f9a04 2021-03-11 op wrefresh(minibuf);
1465 e19f9a04 2021-03-11 op wrefresh(body);
1466 e19f9a04 2021-03-11 op }
1467 e19f9a04 2021-03-11 op }
1468 e19f9a04 2021-03-11 op
1469 e19f9a04 2021-03-11 op static void
1470 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1471 e19f9a04 2021-03-11 op {
1472 9a25f829 2021-03-14 op struct vline *vl;
1473 48e9d457 2021-03-06 op int line;
1474 48e9d457 2021-03-06 op
1475 48e9d457 2021-03-06 op werase(body);
1476 48e9d457 2021-03-06 op
1477 65d9b3ca 2021-03-14 op tab->s.line_off = MIN(tab->s.line_max-1, tab->s.line_off);
1478 65d9b3ca 2021-03-14 op if (TAILQ_EMPTY(&tab->s.head))
1479 48e9d457 2021-03-06 op return;
1480 48e9d457 2021-03-06 op
1481 48e9d457 2021-03-06 op line = 0;
1482 65d9b3ca 2021-03-14 op vl = nth_line(tab, tab->s.line_off);
1483 9a25f829 2021-03-14 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1484 48e9d457 2021-03-06 op wmove(body, line, 0);
1485 9a25f829 2021-03-14 op print_vline(vl);
1486 48e9d457 2021-03-06 op line++;
1487 48e9d457 2021-03-06 op if (line == body_lines)
1488 48e9d457 2021-03-06 op break;
1489 9ca15951 2021-03-09 op }
1490 7953dd72 2021-03-07 op }
1491 7953dd72 2021-03-07 op
1492 7953dd72 2021-03-07 op static void
1493 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1494 7953dd72 2021-03-07 op {
1495 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1496 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1497 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1498 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1499 bf992581 2021-03-12 op
1500 bf992581 2021-03-12 op free(ministate.curmesg);
1501 7953dd72 2021-03-07 op
1502 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1503 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1504 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1505 9cb0f9ce 2021-03-10 op
1506 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1507 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1508 9cb0f9ce 2021-03-10 op wrefresh(body);
1509 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1510 9cb0f9ce 2021-03-10 op } else {
1511 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1512 9cb0f9ce 2021-03-10 op wrefresh(body);
1513 9cb0f9ce 2021-03-10 op }
1514 bcb0b073 2021-03-07 op }
1515 bcb0b073 2021-03-07 op
1516 bcb0b073 2021-03-07 op static void
1517 740f578b 2021-03-15 op message(const char *fmt, ...)
1518 740f578b 2021-03-15 op {
1519 740f578b 2021-03-15 op va_list ap;
1520 740f578b 2021-03-15 op
1521 740f578b 2021-03-15 op va_start(ap, fmt);
1522 740f578b 2021-03-15 op vmessage(fmt, ap);
1523 740f578b 2021-03-15 op va_end(ap);
1524 740f578b 2021-03-15 op }
1525 740f578b 2021-03-15 op
1526 740f578b 2021-03-15 op static void
1527 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1528 8af5e5ed 2021-03-08 op {
1529 65d9b3ca 2021-03-14 op if (tab->s.loading_anim)
1530 8af5e5ed 2021-03-08 op return;
1531 65d9b3ca 2021-03-14 op tab->s.loading_anim = 1;
1532 65d9b3ca 2021-03-14 op evtimer_set(&tab->s.loadingev, update_loading_anim, tab);
1533 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1534 8af5e5ed 2021-03-08 op }
1535 8af5e5ed 2021-03-08 op
1536 8af5e5ed 2021-03-08 op static void
1537 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1538 8af5e5ed 2021-03-08 op {
1539 8af5e5ed 2021-03-08 op struct tab *tab = d;
1540 8af5e5ed 2021-03-08 op
1541 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = (tab->s.loading_anim_step+1)%4;
1542 9ca15951 2021-03-09 op
1543 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1544 6347dcd0 2021-03-16 op redraw_modeline(tab);
1545 6347dcd0 2021-03-16 op wrefresh(modeline);
1546 6347dcd0 2021-03-16 op wrefresh(body);
1547 6347dcd0 2021-03-16 op if (in_minibuffer)
1548 6347dcd0 2021-03-16 op wrefresh(minibuf);
1549 6347dcd0 2021-03-16 op }
1550 8af5e5ed 2021-03-08 op
1551 65d9b3ca 2021-03-14 op evtimer_add(&tab->s.loadingev, &loadingev_timer);
1552 8af5e5ed 2021-03-08 op }
1553 8af5e5ed 2021-03-08 op
1554 8af5e5ed 2021-03-08 op static void
1555 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1556 8af5e5ed 2021-03-08 op {
1557 65d9b3ca 2021-03-14 op if (!tab->s.loading_anim)
1558 8af5e5ed 2021-03-08 op return;
1559 65d9b3ca 2021-03-14 op evtimer_del(&tab->s.loadingev);
1560 65d9b3ca 2021-03-14 op tab->s.loading_anim = 0;
1561 65d9b3ca 2021-03-14 op tab->s.loading_anim_step = 0;
1562 3d8c2326 2021-03-18 op
1563 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1564 3d8c2326 2021-03-18 op return;
1565 43a1b8d0 2021-03-09 op
1566 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1567 9ca15951 2021-03-09 op
1568 43a1b8d0 2021-03-09 op wrefresh(modeline);
1569 43a1b8d0 2021-03-09 op wrefresh(body);
1570 9ca15951 2021-03-09 op if (in_minibuffer)
1571 9ca15951 2021-03-09 op wrefresh(minibuf);
1572 8af5e5ed 2021-03-08 op }
1573 8af5e5ed 2021-03-08 op
1574 8af5e5ed 2021-03-08 op static void
1575 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1576 8af5e5ed 2021-03-08 op {
1577 43a1b8d0 2021-03-09 op empty_vlist(tab);
1578 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1579 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1580 8af5e5ed 2021-03-08 op load_url(tab, url);
1581 43a1b8d0 2021-03-09 op
1582 65d9b3ca 2021-03-14 op tab->s.curs_x = 0;
1583 65d9b3ca 2021-03-14 op tab->s.curs_y = 0;
1584 43a1b8d0 2021-03-09 op redraw_tab(tab);
1585 8af5e5ed 2021-03-08 op }
1586 8af5e5ed 2021-03-08 op
1587 8af5e5ed 2021-03-08 op static void
1588 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1589 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1590 9ca15951 2021-03-09 op {
1591 9ca15951 2021-03-09 op in_minibuffer = 1;
1592 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1593 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1594 9ca15951 2021-03-09 op
1595 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1596 b360ebb3 2021-03-10 op
1597 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1598 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1599 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1600 9ca15951 2021-03-09 op ministate.off = 0;
1601 bb4e4ddf 2021-03-12 op ministate.len = 0;
1602 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1603 22268e11 2021-03-11 op
1604 22268e11 2021-03-11 op ministate.history = hist;
1605 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1606 22268e11 2021-03-11 op ministate.hist_off = 0;
1607 9ca15951 2021-03-09 op }
1608 9ca15951 2021-03-09 op
1609 9ca15951 2021-03-09 op static void
1610 b360ebb3 2021-03-10 op exit_minibuffer(void)
1611 9ca15951 2021-03-09 op {
1612 156f1501 2021-03-13 op werase(minibuf);
1613 9ca15951 2021-03-09 op
1614 9ca15951 2021-03-09 op in_minibuffer = 0;
1615 9ca15951 2021-03-09 op base_map = &global_map;
1616 9ca15951 2021-03-09 op current_map = &global_map;
1617 9ca15951 2021-03-09 op }
1618 9ca15951 2021-03-09 op
1619 9ca15951 2021-03-09 op static void
1620 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1621 5cd2ebb1 2021-03-11 op {
1622 5cd2ebb1 2021-03-11 op struct tab *t;
1623 5cd2ebb1 2021-03-11 op
1624 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1625 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1626 5cd2ebb1 2021-03-11 op }
1627 5cd2ebb1 2021-03-11 op
1628 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1629 5cd2ebb1 2021-03-11 op }
1630 5cd2ebb1 2021-03-11 op
1631 b1df9b71 2021-03-12 op static struct tab *
1632 1b81bc33 2021-03-15 op new_tab(const char *url)
1633 bcb0b073 2021-03-07 op {
1634 754622a2 2021-03-15 op struct tab *tab;
1635 bcb0b073 2021-03-07 op
1636 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1637 71105afa 2021-03-16 op event_loopbreak();
1638 71105afa 2021-03-16 op return NULL;
1639 71105afa 2021-03-16 op }
1640 bcb0b073 2021-03-07 op
1641 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1642 2051e653 2021-03-13 op
1643 65d9b3ca 2021-03-14 op TAILQ_INIT(&tab->s.head);
1644 bcb0b073 2021-03-07 op
1645 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1646 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1647 bcb0b073 2021-03-07 op
1648 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1649 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1650 bcb0b073 2021-03-07 op else
1651 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1652 bcb0b073 2021-03-07 op
1653 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1654 b1df9b71 2021-03-12 op return tab;
1655 5e11c00c 2021-03-02 op }
1656 5e11c00c 2021-03-02 op
1657 941b3761 2021-03-18 op static void
1658 941b3761 2021-03-18 op usage(void)
1659 5e11c00c 2021-03-02 op {
1660 941b3761 2021-03-18 op fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1661 941b3761 2021-03-18 op }
1662 941b3761 2021-03-18 op
1663 941b3761 2021-03-18 op int
1664 941b3761 2021-03-18 op ui_init(int argc, const char **argv)
1665 941b3761 2021-03-18 op {
1666 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1667 941b3761 2021-03-18 op int ch;
1668 941b3761 2021-03-18 op
1669 941b3761 2021-03-18 op while ((ch = getopt(argc, argv, "")) != -1) {
1670 941b3761 2021-03-18 op switch (ch) {
1671 941b3761 2021-03-18 op default:
1672 941b3761 2021-03-18 op usage();
1673 941b3761 2021-03-18 op return 0;
1674 941b3761 2021-03-18 op }
1675 941b3761 2021-03-18 op }
1676 941b3761 2021-03-18 op argc -= optind;
1677 941b3761 2021-03-18 op argv += optind;
1678 941b3761 2021-03-18 op
1679 941b3761 2021-03-18 op if (argc != 0)
1680 941b3761 2021-03-18 op url = argv[0];
1681 941b3761 2021-03-18 op
1682 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1683 5e11c00c 2021-03-02 op
1684 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1685 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1686 9ca15951 2021-03-09 op
1687 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1688 9ca15951 2021-03-09 op
1689 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1690 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1691 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1692 22268e11 2021-03-11 op
1693 9ca15951 2021-03-09 op base_map = &global_map;
1694 f832146f 2021-03-09 op current_map = &global_map;
1695 f832146f 2021-03-09 op load_default_keys();
1696 f832146f 2021-03-09 op
1697 5e11c00c 2021-03-02 op initscr();
1698 15e1b108 2021-03-02 op raw();
1699 5e11c00c 2021-03-02 op noecho();
1700 5e11c00c 2021-03-02 op
1701 5e11c00c 2021-03-02 op nonl();
1702 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1703 5e11c00c 2021-03-02 op
1704 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1705 48e9d457 2021-03-06 op return 0;
1706 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1707 48e9d457 2021-03-06 op return 0;
1708 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1709 48e9d457 2021-03-06 op return 0;
1710 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1711 48e9d457 2021-03-06 op return 0;
1712 1d08c280 2021-03-06 op
1713 48e9d457 2021-03-06 op body_lines = LINES-3;
1714 48e9d457 2021-03-06 op body_cols = COLS;
1715 48e9d457 2021-03-06 op
1716 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1717 48e9d457 2021-03-06 op scrollok(body, TRUE);
1718 48e9d457 2021-03-06 op
1719 5e11c00c 2021-03-02 op /* non-blocking input */
1720 48e9d457 2021-03-06 op wtimeout(body, 0);
1721 5e11c00c 2021-03-02 op
1722 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1723 5e11c00c 2021-03-02 op
1724 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1725 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1726 5e11c00c 2021-03-02 op
1727 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1728 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1729 5e11c00c 2021-03-02 op
1730 941b3761 2021-03-18 op new_tab(url);
1731 5e11c00c 2021-03-02 op
1732 1d08c280 2021-03-06 op return 1;
1733 5e11c00c 2021-03-02 op }
1734 5e11c00c 2021-03-02 op
1735 5e11c00c 2021-03-02 op void
1736 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1737 8af5e5ed 2021-03-08 op {
1738 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1739 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1740 3d8c2326 2021-03-18 op
1741 3d8c2326 2021-03-18 op redraw_tabline();
1742 3d8c2326 2021-03-18 op wrefresh(tabline);
1743 3d8c2326 2021-03-18 op if (in_minibuffer)
1744 3d8c2326 2021-03-18 op wrefresh(minibuf);
1745 3d8c2326 2021-03-18 op else
1746 3d8c2326 2021-03-18 op wrefresh(body);
1747 8af5e5ed 2021-03-08 op }
1748 8af5e5ed 2021-03-08 op
1749 8af5e5ed 2021-03-08 op void
1750 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1751 5e11c00c 2021-03-02 op {
1752 1d08c280 2021-03-06 op wrap_page(tab);
1753 3d8c2326 2021-03-18 op if (tab->flags & TAB_CURRENT)
1754 3d8c2326 2021-03-18 op redraw_tab(tab);
1755 5e11c00c 2021-03-02 op }
1756 5e11c00c 2021-03-02 op
1757 5e11c00c 2021-03-02 op void
1758 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1759 5cd2ebb1 2021-03-11 op {
1760 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1761 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1762 5cd2ebb1 2021-03-11 op
1763 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1764 22268e11 2021-03-11 op &ir_history);
1765 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1766 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1767 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1768 5cd2ebb1 2021-03-11 op }
1769 5cd2ebb1 2021-03-11 op
1770 5cd2ebb1 2021-03-11 op void
1771 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1772 740f578b 2021-03-15 op {
1773 740f578b 2021-03-15 op va_list ap;
1774 740f578b 2021-03-15 op
1775 740f578b 2021-03-15 op va_start(ap, fmt);
1776 740f578b 2021-03-15 op vmessage(fmt, ap);
1777 740f578b 2021-03-15 op va_end(ap);
1778 740f578b 2021-03-15 op }
1779 740f578b 2021-03-15 op
1780 740f578b 2021-03-15 op void
1781 5e11c00c 2021-03-02 op ui_end(void)
1782 5e11c00c 2021-03-02 op {
1783 5e11c00c 2021-03-02 op endwin();
1784 5e11c00c 2021-03-02 op }