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