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