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