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