Blame


1 5e11c00c 2021-03-02 op /*
2 d35e18b3 2024-02-04 op * Copyright (c) 2021, 2024 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 *
27 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
28 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
29 970deec6 2021-04-24 op * ``on-demand'', but it's still missing.
30 1d08c280 2021-03-06 op *
31 1d08c280 2021-03-06 op */
32 786e6deb 2021-07-21 op
33 786e6deb 2021-07-21 op #include "compat.h"
34 1d08c280 2021-03-06 op
35 98d3e6c1 2024-02-18 op #include <sys/time.h>
36 4e32d3a6 2024-06-04 thomas.ad #include <sys/wait.h>
37 98d3e6c1 2024-02-18 op
38 5e11c00c 2021-03-02 op #include <curses.h>
39 4e32d3a6 2024-06-04 thomas.ad #include <errno.h>
40 5e11c00c 2021-03-02 op #include <locale.h>
41 5e11c00c 2021-03-02 op #include <signal.h>
42 7953dd72 2021-03-07 op #include <stdarg.h>
43 eb259e66 2021-03-02 op #include <stdlib.h>
44 eb259e66 2021-03-02 op #include <string.h>
45 f832146f 2021-03-09 op #include <unistd.h>
46 5e11c00c 2021-03-02 op
47 1dc96d88 2024-10-19 op #include <grapheme.h>
48 1dc96d88 2024-10-19 op
49 d252d87c 2024-02-05 op #include "cmd.h"
50 e659558c 2021-07-13 op #include "defaults.h"
51 98d3e6c1 2024-02-18 op #include "ev.h"
52 0548291a 2024-06-25 op #include "exec.h"
53 65c49665 2024-01-23 op #include "hist.h"
54 f7db6d13 2024-02-06 op #include "keymap.h"
55 7b7a69f2 2024-06-13 thomas.ad #include "mailcap.h"
56 d1a0f2a3 2021-07-12 op #include "minibuffer.h"
57 1fce2e75 2021-08-14 op #include "session.h"
58 d1a0f2a3 2021-07-12 op #include "telescope.h"
59 d1a0f2a3 2021-07-12 op #include "ui.h"
60 5caf7d67 2021-07-12 op #include "utf8.h"
61 d1a0f2a3 2021-07-12 op
62 e795e935 2022-01-18 op static void set_scroll_position(struct tab *, size_t, size_t);
63 e795e935 2022-01-18 op
64 0a805b02 2021-07-01 op static void restore_curs_x(struct buffer *);
65 9ca15951 2021-03-09 op
66 8947c1f2 2021-03-21 op static int readkey(void);
67 98d3e6c1 2024-02-18 op static void dispatch_stdio(int, int, void*);
68 329cd624 2024-06-15 op static void handle_signal(int, int, void*);
69 98d3e6c1 2024-02-18 op static void handle_resize_nodelay(int, int, void*);
70 98d3e6c1 2024-02-18 op static void handle_download_refresh(int, int, void *);
71 32c6c75e 2021-07-15 op static void rearrange_windows(void);
72 f853ec6f 2024-10-22 op static void line_prefix_and_text(int, struct vline *, char *, size_t, const char **, const char **, int *);
73 f3bcf8f2 2021-06-21 op static void print_vline(int, int, WINDOW*, struct vline*);
74 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
75 0aef305d 2022-01-10 op static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
76 3b5f459e 2021-11-05 op static void redraw_download(void);
77 bddc7bbd 2021-04-01 op static void redraw_help(void);
78 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
79 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
80 e9af06b9 2021-07-12 op static void redraw_minibuffer(void);
81 e9af06b9 2021-07-12 op static void do_redraw_echoarea(void);
82 e9af06b9 2021-07-12 op static void do_redraw_minibuffer(void);
83 b1e1e41a 2021-07-14 op static void do_redraw_minibuffer_compl(void);
84 55df859f 2021-07-12 op static void place_cursor(int);
85 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
86 98d3e6c1 2024-02-18 op static void update_loading_anim(int, int, void*);
87 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
88 2ddbda6b 2021-07-17 op
89 32c6c75e 2021-07-15 op static int should_rearrange_windows;
90 56168aa8 2021-08-18 op static int show_tab_bar;
91 23f22c83 2021-07-13 op static int too_small;
92 72b18268 2021-06-19 op static int x_offset;
93 72b18268 2021-06-19 op
94 83dce83d 2021-07-17 op struct thiskey thiskey;
95 83dce83d 2021-07-17 op struct tab *current_tab;
96 9ca15951 2021-03-09 op
97 98d3e6c1 2024-02-18 op static unsigned int resize_timer;
98 98d3e6c1 2024-02-18 op static struct timeval resize_tv = { 0, 250000 };
99 831deb20 2021-05-12 op
100 98d3e6c1 2024-02-18 op static unsigned int download_timer;
101 0e7b2e99 2022-05-05 op static struct timeval download_refresh_timer = { 0, 250000 };
102 0e7b2e99 2022-05-05 op
103 e5a2797f 2021-07-13 op static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
104 48e9d457 2021-03-06 op
105 2b2d2872 2021-06-20 op int body_lines, body_cols;
106 2b2d2872 2021-06-20 op
107 bddc7bbd 2021-04-01 op static WINDOW *help;
108 de278567 2021-07-21 op /* not static so we can see them from help.c */
109 de278567 2021-07-21 op struct buffer helpwin;
110 de278567 2021-07-21 op int help_lines, help_cols;
111 bddc7bbd 2021-04-01 op
112 3b5f459e 2021-11-05 op static WINDOW *download;
113 3b5f459e 2021-11-05 op /* not static so we can see them from download.c */
114 3b5f459e 2021-11-05 op struct buffer downloadwin;
115 3b5f459e 2021-11-05 op int download_lines;
116 84892515 2021-11-27 op int download_cols;
117 3b5f459e 2021-11-05 op
118 bddc7bbd 2021-04-01 op static int side_window;
119 65340174 2021-07-21 op static int in_side_window;
120 bddc7bbd 2021-04-01 op
121 98d3e6c1 2024-02-18 op static struct timeval loading_tv = { 0, 250000 };
122 48e9d457 2021-03-06 op
123 7c7d7bb7 2021-03-10 op static char keybuf[64];
124 9ca15951 2021-03-09 op
125 78894e73 2021-08-12 op /* XXX: don't forget to init these in main() */
126 9ca15951 2021-03-09 op struct kmap global_map,
127 fa3fd864 2021-03-10 op minibuffer_map,
128 9ca15951 2021-03-09 op *current_map,
129 9ca15951 2021-03-09 op *base_map;
130 9ca15951 2021-03-09 op
131 72b18268 2021-06-19 op static inline void
132 923f9ce5 2021-06-21 op update_x_offset(void)
133 72b18268 2021-06-19 op {
134 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
135 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
136 72b18268 2021-06-19 op else
137 72b18268 2021-06-19 op x_offset = 0;
138 72b18268 2021-06-19 op }
139 65d9b3ca 2021-03-14 op
140 e795e935 2022-01-18 op static void
141 e795e935 2022-01-18 op set_scroll_position(struct tab *tab, size_t top, size_t cur)
142 e795e935 2022-01-18 op {
143 e795e935 2022-01-18 op struct line *last;
144 e795e935 2022-01-18 op struct vline *vl;
145 e795e935 2022-01-18 op size_t i = 0;
146 6d5348f3 2022-01-21 op int topfound = 0;
147 e795e935 2022-01-18 op
148 c1d27b0e 2024-06-14 op last = TAILQ_FIRST(&tab->buffer.head);
149 c1d27b0e 2024-06-14 op TAILQ_FOREACH(vl, &tab->buffer.vhead, vlines) {
150 e795e935 2022-01-18 op if (last != vl->parent) {
151 e795e935 2022-01-18 op last = vl->parent;
152 e795e935 2022-01-18 op i++;
153 e795e935 2022-01-18 op }
154 e795e935 2022-01-18 op
155 e795e935 2022-01-18 op if (!topfound && i == top) {
156 e795e935 2022-01-18 op topfound = 1;
157 e795e935 2022-01-18 op tab->buffer.top_line = vl;
158 e795e935 2022-01-18 op }
159 e795e935 2022-01-18 op
160 6d5348f3 2022-01-21 op if (i == cur) {
161 e795e935 2022-01-18 op tab->buffer.current_line = vl;
162 e795e935 2022-01-18 op return;
163 e795e935 2022-01-18 op }
164 e795e935 2022-01-18 op }
165 e795e935 2022-01-18 op
166 6d5348f3 2022-01-21 op if (!topfound)
167 c1d27b0e 2024-06-14 op tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.vhead);
168 2ef8fcae 2022-01-21 op
169 6d5348f3 2022-01-21 op tab->buffer.current_line = tab->buffer.top_line;
170 e795e935 2022-01-18 op }
171 e795e935 2022-01-18 op
172 98411855 2021-06-23 op void
173 e795e935 2022-01-18 op get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
174 e795e935 2022-01-18 op {
175 e795e935 2022-01-18 op struct line *l;
176 e795e935 2022-01-18 op int topfound = 0;
177 e795e935 2022-01-18 op
178 e795e935 2022-01-18 op *top = 0;
179 e795e935 2022-01-18 op *cur = 0;
180 e795e935 2022-01-18 op
181 e795e935 2022-01-18 op if (tab->buffer.top_line == NULL ||
182 e795e935 2022-01-18 op tab->buffer.current_line == NULL)
183 e795e935 2022-01-18 op return;
184 e795e935 2022-01-18 op
185 c1d27b0e 2024-06-14 op TAILQ_FOREACH(l, &tab->buffer.head, lines) {
186 e795e935 2022-01-18 op if (tab->buffer.top_line->parent == l)
187 e795e935 2022-01-18 op topfound = 1;
188 e795e935 2022-01-18 op if (tab->buffer.current_line->parent == l)
189 e795e935 2022-01-18 op return;
190 e795e935 2022-01-18 op
191 e795e935 2022-01-18 op if (!topfound)
192 e795e935 2022-01-18 op (*top)++;
193 e795e935 2022-01-18 op (*cur)++;
194 e795e935 2022-01-18 op }
195 e795e935 2022-01-18 op }
196 e795e935 2022-01-18 op
197 e795e935 2022-01-18 op void
198 98411855 2021-06-23 op save_excursion(struct excursion *place, struct buffer *buffer)
199 98411855 2021-06-23 op {
200 98411855 2021-06-23 op place->curs_x = buffer->curs_x;
201 98411855 2021-06-23 op place->curs_y = buffer->curs_y;
202 98411855 2021-06-23 op place->line_off = buffer->line_off;
203 ca783de6 2021-07-18 op place->top_line = buffer->top_line;
204 98411855 2021-06-23 op place->current_line = buffer->current_line;
205 6d24bfb3 2024-10-19 op place->point_offset = buffer->point_offset;
206 98411855 2021-06-23 op }
207 98411855 2021-06-23 op
208 98411855 2021-06-23 op void
209 98411855 2021-06-23 op restore_excursion(struct excursion *place, struct buffer *buffer)
210 98411855 2021-06-23 op {
211 98411855 2021-06-23 op buffer->curs_x = place->curs_x;
212 98411855 2021-06-23 op buffer->curs_y = place->curs_y;
213 98411855 2021-06-23 op buffer->line_off = place->line_off;
214 ca783de6 2021-07-18 op buffer->top_line = place->top_line;
215 98411855 2021-06-23 op buffer->current_line = place->current_line;
216 6d24bfb3 2024-10-19 op buffer->point_offset = place->point_offset;
217 1d08c280 2021-03-06 op }
218 1d08c280 2021-03-06 op
219 0a805b02 2021-07-01 op static void
220 0a805b02 2021-07-01 op restore_curs_x(struct buffer *buffer)
221 48e9d457 2021-03-06 op {
222 452589f7 2021-03-16 op struct vline *vl;
223 f350bc73 2024-05-27 thomas.ad struct lineprefix *lp = line_prefixes;
224 bd644709 2021-07-18 op const char *prfx, *text;
225 f350bc73 2024-05-27 thomas.ad
226 f350bc73 2024-05-27 thomas.ad if (dont_apply_styling)
227 f350bc73 2024-05-27 thomas.ad lp = raw_prefixes;
228 452589f7 2021-03-16 op
229 f853ec6f 2024-10-22 op buffer->curs_x = 0;
230 f853ec6f 2024-10-22 op
231 f853ec6f 2024-10-22 op /* small hack: don't olivetti-mode the download pane */
232 f853ec6f 2024-10-22 op if (buffer != &downloadwin)
233 f853ec6f 2024-10-22 op buffer->curs_x += x_offset;
234 f853ec6f 2024-10-22 op
235 46f6e974 2021-05-17 op vl = buffer->current_line;
236 c1d27b0e 2024-06-14 op if (vl == NULL || vl->len == 0 || vl->parent == NULL)
237 f853ec6f 2024-10-22 op buffer->curs_x += buffer->point_offset = 0;
238 bd644709 2021-07-18 op else if (vl->parent->data != NULL) {
239 bd644709 2021-07-18 op text = vl->parent->data;
240 f853ec6f 2024-10-22 op buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
241 f853ec6f 2024-10-22 op buffer->curs_x);
242 bd4a08a7 2022-11-07 op } else {
243 bd4a08a7 2022-11-07 op text = vl->parent->line + vl->from;
244 f853ec6f 2024-10-22 op buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
245 f853ec6f 2024-10-22 op buffer->curs_x);
246 bd4a08a7 2022-11-07 op }
247 452589f7 2021-03-16 op
248 47aec13e 2021-07-17 op if (vl == NULL)
249 47aec13e 2021-07-17 op return;
250 47aec13e 2021-07-17 op
251 47aec13e 2021-07-17 op if (vl->parent->data != NULL)
252 002b074d 2021-08-14 op buffer->curs_x += utf8_swidth_between(vl->parent->line,
253 f853ec6f 2024-10-22 op vl->parent->data, buffer->curs_x);
254 47aec13e 2021-07-17 op else {
255 f350bc73 2024-05-27 thomas.ad prfx = lp[vl->parent->type].prfx1;
256 f853ec6f 2024-10-22 op buffer->curs_x += utf8_swidth(prfx, buffer->curs_x);
257 2ba66cea 2021-03-22 op }
258 9ca15951 2021-03-09 op }
259 9ca15951 2021-03-09 op
260 d5bdf203 2021-07-08 op void
261 870210fb 2021-03-26 op global_key_unbound(void)
262 870210fb 2021-03-26 op {
263 870210fb 2021-03-26 op message("%s is undefined", keybuf);
264 870210fb 2021-03-26 op }
265 870210fb 2021-03-26 op
266 2b2d2872 2021-06-20 op struct buffer *
267 46f6e974 2021-05-17 op current_buffer(void)
268 2ba66cea 2021-03-22 op {
269 2ba66cea 2021-03-22 op if (in_minibuffer)
270 46f6e974 2021-05-17 op return &ministate.buffer;
271 3b5f459e 2021-11-05 op if (in_side_window & SIDE_WINDOW_LEFT)
272 65340174 2021-07-21 op return &helpwin;
273 3b5f459e 2021-11-05 op if (in_side_window & SIDE_WINDOW_BOTTOM)
274 3b5f459e 2021-11-05 op return &downloadwin;
275 83dce83d 2021-07-17 op return &current_tab->buffer;
276 2ba66cea 2021-03-22 op }
277 2ba66cea 2021-03-22 op
278 8947c1f2 2021-03-21 op static int
279 8947c1f2 2021-03-21 op readkey(void)
280 5e11c00c 2021-03-02 op {
281 8947c1f2 2021-03-21 op uint32_t state = 0;
282 19f1448e 2021-03-08 op
283 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
284 8947c1f2 2021-03-21 op return 0;
285 5e11c00c 2021-03-02 op
286 3f66a33e 2021-12-01 op thiskey.meta = thiskey.key == '\e';
287 8947c1f2 2021-03-21 op if (thiskey.meta) {
288 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
289 3f66a33e 2021-12-01 op if (thiskey.key == ERR || thiskey.key == '\e') {
290 c314a314 2021-03-11 op thiskey.meta = 0;
291 3f66a33e 2021-12-01 op thiskey.key = '\e';
292 c314a314 2021-03-11 op }
293 8947c1f2 2021-03-21 op }
294 8947c1f2 2021-03-21 op
295 8947c1f2 2021-03-21 op thiskey.cp = 0;
296 63ecae6d 2021-08-14 op
297 63ecae6d 2021-08-14 op if ((unsigned int)thiskey.key >= UINT8_MAX)
298 63ecae6d 2021-08-14 op return 1;
299 63ecae6d 2021-08-14 op
300 63ecae6d 2021-08-14 op while (1) {
301 63ecae6d 2021-08-14 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
302 63ecae6d 2021-08-14 op break;
303 63ecae6d 2021-08-14 op if ((thiskey.key = wgetch(body)) == ERR) {
304 63ecae6d 2021-08-14 op message("Error decoding user input");
305 63ecae6d 2021-08-14 op return 0;
306 8947c1f2 2021-03-21 op }
307 8947c1f2 2021-03-21 op }
308 19f1448e 2021-03-08 op
309 8947c1f2 2021-03-21 op return 1;
310 8947c1f2 2021-03-21 op }
311 8947c1f2 2021-03-21 op
312 8947c1f2 2021-03-21 op static void
313 98d3e6c1 2024-02-18 op dispatch_stdio(int fd, int ev, void *d)
314 8947c1f2 2021-03-21 op {
315 57668c86 2021-11-05 op int lk;
316 8947c1f2 2021-03-21 op const char *keyname;
317 8947c1f2 2021-03-21 op char tmp[5] = {0};
318 8947c1f2 2021-03-21 op
319 23f22c83 2021-07-13 op /* TODO: schedule a redraw? */
320 23f22c83 2021-07-13 op if (too_small)
321 23f22c83 2021-07-13 op return;
322 23f22c83 2021-07-13 op
323 8947c1f2 2021-03-21 op if (!readkey())
324 8947c1f2 2021-03-21 op return;
325 8947c1f2 2021-03-21 op
326 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
327 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
328 7c7d7bb7 2021-03-10 op if (thiskey.meta)
329 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
330 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
331 dcbfbf59 2024-10-20 op grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
332 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
333 af29d739 2021-07-13 op } else if ((keyname = unkbd(thiskey.key)) != NULL) {
334 af29d739 2021-07-13 op strlcat(keybuf, keyname, sizeof(keybuf));
335 8947c1f2 2021-03-21 op } else {
336 af29d739 2021-07-13 op tmp[0] = thiskey.key;
337 af29d739 2021-07-13 op strlcat(keybuf, tmp, sizeof(keybuf));
338 7c7d7bb7 2021-03-10 op }
339 7c7d7bb7 2021-03-10 op
340 57668c86 2021-11-05 op lk = lookup_key(&current_map, &thiskey, current_buffer());
341 57668c86 2021-11-05 op if (lk == LK_UNBOUND) {
342 57668c86 2021-11-05 op if (current_map->unhandled_input != NULL)
343 57668c86 2021-11-05 op current_map->unhandled_input();
344 57668c86 2021-11-05 op else
345 57668c86 2021-11-05 op global_key_unbound();
346 eb259e66 2021-03-02 op }
347 57668c86 2021-11-05 op if (lk != LK_ADVANCED_MAP) {
348 57668c86 2021-11-05 op current_map = base_map;
349 57668c86 2021-11-05 op strlcpy(keybuf, "", sizeof(keybuf));
350 57668c86 2021-11-05 op }
351 1d08c280 2021-03-06 op
352 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT)
353 bddc7bbd 2021-04-01 op recompute_help();
354 bddc7bbd 2021-04-01 op
355 32c6c75e 2021-07-15 op if (should_rearrange_windows)
356 32c6c75e 2021-07-15 op rearrange_windows();
357 83dce83d 2021-07-17 op redraw_tab(current_tab);
358 a6d450c1 2021-03-06 op }
359 48e9d457 2021-03-06 op
360 a6d450c1 2021-03-06 op static void
361 329cd624 2024-06-15 op handle_signal(int sig, int ev, void *d)
362 831deb20 2021-05-12 op {
363 329cd624 2024-06-15 op int ret;
364 329cd624 2024-06-15 op
365 329cd624 2024-06-15 op switch (sig) {
366 329cd624 2024-06-15 op case SIGWINCH:
367 329cd624 2024-06-15 op ev_timer_cancel(resize_timer);
368 329cd624 2024-06-15 op resize_timer = ev_timer(&resize_tv, handle_resize_nodelay,
369 329cd624 2024-06-15 op NULL);
370 329cd624 2024-06-15 op break;
371 329cd624 2024-06-15 op case SIGCHLD:
372 329cd624 2024-06-15 op do {
373 329cd624 2024-06-15 op ret = waitpid(-1, NULL, WNOHANG);
374 329cd624 2024-06-15 op } while (ret == -1 && errno == EINTR);
375 329cd624 2024-06-15 op break;
376 329cd624 2024-06-15 op }
377 831deb20 2021-05-12 op }
378 831deb20 2021-05-12 op
379 831deb20 2021-05-12 op static void
380 98d3e6c1 2024-02-18 op handle_resize_nodelay(int s, int ev, void *d)
381 5e11c00c 2021-03-02 op {
382 5e11c00c 2021-03-02 op endwin();
383 5e11c00c 2021-03-02 op refresh();
384 5e11c00c 2021-03-02 op clear();
385 32c6c75e 2021-07-15 op
386 32c6c75e 2021-07-15 op rearrange_windows();
387 32c6c75e 2021-07-15 op }
388 32c6c75e 2021-07-15 op
389 0e7b2e99 2022-05-05 op static void
390 98d3e6c1 2024-02-18 op handle_download_refresh(int s, int v, void *d)
391 0e7b2e99 2022-05-05 op {
392 0e7b2e99 2022-05-05 op if (side_window & SIDE_WINDOW_BOTTOM) {
393 0e7b2e99 2022-05-05 op recompute_downloads();
394 0e7b2e99 2022-05-05 op redraw_tab(current_tab);
395 0e7b2e99 2022-05-05 op }
396 0e7b2e99 2022-05-05 op }
397 0e7b2e99 2022-05-05 op
398 56168aa8 2021-08-18 op static inline int
399 56168aa8 2021-08-18 op should_show_tab_bar(void)
400 56168aa8 2021-08-18 op {
401 56168aa8 2021-08-18 op if (tab_bar_show == -1)
402 56168aa8 2021-08-18 op return 0;
403 56168aa8 2021-08-18 op if (tab_bar_show == 0)
404 56168aa8 2021-08-18 op return 1;
405 56168aa8 2021-08-18 op
406 56168aa8 2021-08-18 op return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
407 56168aa8 2021-08-18 op }
408 56168aa8 2021-08-18 op
409 32c6c75e 2021-07-15 op static void
410 32c6c75e 2021-07-15 op rearrange_windows(void)
411 32c6c75e 2021-07-15 op {
412 32c6c75e 2021-07-15 op int lines;
413 3b5f459e 2021-11-05 op int minibuffer_lines;
414 5e11c00c 2021-03-02 op
415 32c6c75e 2021-07-15 op should_rearrange_windows = 0;
416 56168aa8 2021-08-18 op show_tab_bar = should_show_tab_bar();
417 32c6c75e 2021-07-15 op
418 b3884fbe 2021-07-13 op lines = LINES;
419 b3884fbe 2021-07-13 op
420 45929003 2024-01-13 op /* 3 lines for the ui and 12 for the body and minibuffer */
421 23f22c83 2021-07-13 op if ((too_small = lines < 15)) {
422 23f22c83 2021-07-13 op erase();
423 23f22c83 2021-07-13 op printw("Window too small.");
424 23f22c83 2021-07-13 op refresh();
425 23f22c83 2021-07-13 op return;
426 23f22c83 2021-07-13 op }
427 23f22c83 2021-07-13 op
428 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
429 48e9d457 2021-03-06 op
430 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD) {
431 3b5f459e 2021-11-05 op minibuffer_lines = MIN(10, lines/2);
432 3b5f459e 2021-11-05 op mvwin(minibuffer, lines - minibuffer_lines, 0);
433 3b5f459e 2021-11-05 op wresize(minibuffer, minibuffer_lines, COLS);
434 3b5f459e 2021-11-05 op lines -= minibuffer_lines;
435 b1e1e41a 2021-07-14 op
436 f853ec6f 2024-10-22 op wrap_page(&ministate.compl.buffer, COLS, 0);
437 b1e1e41a 2021-07-14 op }
438 b1e1e41a 2021-07-14 op
439 b3884fbe 2021-07-13 op mvwin(echoarea, --lines, 0);
440 8a7f2683 2021-07-10 op wresize(echoarea, 1, COLS);
441 48e9d457 2021-03-06 op
442 b3884fbe 2021-07-13 op mvwin(modeline, --lines, 0);
443 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
444 48e9d457 2021-03-06 op
445 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_BOTTOM) {
446 3b5f459e 2021-11-05 op download_lines = MIN(5, lines/2);
447 84892515 2021-11-27 op download_cols = COLS;
448 3b5f459e 2021-11-05 op mvwin(download, lines - download_lines, 0);
449 84892515 2021-11-27 op wresize(download, download_lines, download_cols);
450 3b5f459e 2021-11-05 op lines -= download_lines;
451 3b5f459e 2021-11-05 op
452 f853ec6f 2024-10-22 op wrap_page(&downloadwin, download_cols, 0);
453 3b5f459e 2021-11-05 op }
454 3b5f459e 2021-11-05 op
455 56168aa8 2021-08-18 op body_lines = show_tab_bar ? --lines : lines;
456 bd9637e9 2021-03-06 op body_cols = COLS;
457 bddc7bbd 2021-04-01 op
458 56168aa8 2021-08-18 op /*
459 56168aa8 2021-08-18 op * Here we make the assumption that show_tab_bar is either 0
460 56168aa8 2021-08-18 op * or 1, and reuse that as argument to mvwin.
461 56168aa8 2021-08-18 op */
462 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT) {
463 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
464 b3884fbe 2021-07-13 op help_lines = lines;
465 56168aa8 2021-08-18 op mvwin(help, show_tab_bar, 0);
466 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
467 48e9d457 2021-03-06 op
468 f853ec6f 2024-10-22 op wrap_page(&helpwin, help_cols, 0);
469 bddc7bbd 2021-04-01 op
470 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
471 56168aa8 2021-08-18 op mvwin(body, show_tab_bar, help_cols);
472 bddc7bbd 2021-04-01 op } else
473 56168aa8 2021-08-18 op mvwin(body, show_tab_bar, 0);
474 bddc7bbd 2021-04-01 op
475 72b18268 2021-06-19 op update_x_offset();
476 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
477 bddc7bbd 2021-04-01 op
478 56168aa8 2021-08-18 op if (show_tab_bar)
479 56168aa8 2021-08-18 op wresize(tabline, 1, COLS);
480 48e9d457 2021-03-06 op
481 f853ec6f 2024-10-22 op wrap_page(&current_tab->buffer, body_cols, x_offset);
482 83dce83d 2021-07-17 op redraw_tab(current_tab);
483 eb259e66 2021-03-02 op }
484 94ec38e8 2021-06-29 op
485 6ca7e1b6 2021-07-16 op static void
486 f853ec6f 2024-10-22 op line_prefix_and_text(int col, struct vline *vl, char *buf, size_t len,
487 bd4a08a7 2022-11-07 op const char **prfx_ret, const char **text_ret, int *text_len)
488 6ca7e1b6 2021-07-16 op {
489 f350bc73 2024-05-27 thomas.ad struct lineprefix *lp = line_prefixes;
490 fd29e873 2024-01-13 op int type, cont;
491 fd29e873 2024-01-13 op size_t i, width;
492 6ca7e1b6 2021-07-16 op char *space, *t;
493 f350bc73 2024-05-27 thomas.ad
494 f350bc73 2024-05-27 thomas.ad if (dont_apply_styling)
495 f350bc73 2024-05-27 thomas.ad lp = raw_prefixes;
496 6ca7e1b6 2021-07-16 op
497 bd4a08a7 2022-11-07 op if (vl->len == 0) {
498 6ca7e1b6 2021-07-16 op *text_ret = "";
499 bd4a08a7 2022-11-07 op *text_len = 0;
500 bd4a08a7 2022-11-07 op }
501 6ca7e1b6 2021-07-16 op
502 6ca7e1b6 2021-07-16 op cont = vl->flags & L_CONTINUATION;
503 6ca7e1b6 2021-07-16 op type = vl->parent->type;
504 55b0b036 2021-07-16 op if (!cont)
505 f350bc73 2024-05-27 thomas.ad *prfx_ret = lp[type].prfx1;
506 6ca7e1b6 2021-07-16 op else
507 f350bc73 2024-05-27 thomas.ad *prfx_ret = lp[type].prfx2;
508 6ca7e1b6 2021-07-16 op
509 6ca7e1b6 2021-07-16 op space = vl->parent->data;
510 5ab1e3c1 2024-01-13 op *text_ret = vl->parent->line + vl->from;
511 5ab1e3c1 2024-01-13 op *text_len = MIN(INT_MAX, vl->len);
512 bd4a08a7 2022-11-07 op if (!emojify_link || type != LINE_LINK || space == NULL) {
513 6ca7e1b6 2021-07-16 op return;
514 bd4a08a7 2022-11-07 op }
515 5e11c00c 2021-03-02 op
516 6ca7e1b6 2021-07-16 op if (cont) {
517 6ca7e1b6 2021-07-16 op memset(buf, 0, len);
518 f853ec6f 2024-10-22 op width = utf8_swidth_between(vl->parent->line, space, col);
519 fd29e873 2024-01-13 op for (i = 0; i < width + 1 && i < len - 1; ++i)
520 fd29e873 2024-01-13 op buf[i] = ' ';
521 6ca7e1b6 2021-07-16 op } else {
522 5ab1e3c1 2024-01-13 op strlcpy(buf, vl->parent->line, len);
523 6ca7e1b6 2021-07-16 op if ((t = strchr(buf, ' ')) != NULL)
524 6ca7e1b6 2021-07-16 op *t = '\0';
525 6ca7e1b6 2021-07-16 op strlcat(buf, " ", len);
526 6ca7e1b6 2021-07-16 op }
527 6ca7e1b6 2021-07-16 op
528 6ca7e1b6 2021-07-16 op *prfx_ret = buf;
529 6ca7e1b6 2021-07-16 op }
530 b3be07ea 2021-07-18 op
531 b3be07ea 2021-07-18 op static inline void
532 b3be07ea 2021-07-18 op print_vline_descr(int width, WINDOW *window, struct vline *vl)
533 b3be07ea 2021-07-18 op {
534 b3be07ea 2021-07-18 op int x, y, goal;
535 b3be07ea 2021-07-18 op
536 1577540c 2021-11-05 op switch (vl->parent->type) {
537 b8899faf 2021-11-05 op case LINE_COMPL:
538 b8899faf 2021-11-05 op case LINE_COMPL_CURRENT:
539 a7ce8d61 2021-11-05 op goal = width/2;
540 a7ce8d61 2021-11-05 op break;
541 1577540c 2021-11-05 op case LINE_DOWNLOAD:
542 1577540c 2021-11-05 op case LINE_DOWNLOAD_DONE:
543 a7ce8d61 2021-11-05 op goal = width/4;
544 455b4545 2021-11-05 op break;
545 455b4545 2021-11-05 op case LINE_HELP:
546 1577540c 2021-11-05 op goal = 8;
547 1577540c 2021-11-05 op break;
548 1577540c 2021-11-05 op default:
549 b8899faf 2021-11-05 op return;
550 1577540c 2021-11-05 op }
551 b8899faf 2021-11-05 op
552 b8899faf 2021-11-05 op if (vl->parent->alt == NULL)
553 b8899faf 2021-11-05 op return;
554 b8899faf 2021-11-05 op
555 b8899faf 2021-11-05 op (void)y;
556 b8899faf 2021-11-05 op getyx(window, y, x);
557 c6be26e4 2021-07-21 op
558 b3be07ea 2021-07-18 op if (goal <= x)
559 b3be07ea 2021-07-18 op wprintw(window, " ");
560 b3be07ea 2021-07-18 op for (; goal > x; ++x)
561 b3be07ea 2021-07-18 op wprintw(window, " ");
562 b3be07ea 2021-07-18 op
563 b3be07ea 2021-07-18 op wprintw(window, "%s", vl->parent->alt);
564 b3be07ea 2021-07-18 op }
565 b3be07ea 2021-07-18 op
566 fbadd395 2021-07-16 op /*
567 fbadd395 2021-07-16 op * Core part of the rendering. It prints a vline starting from the
568 fbadd395 2021-07-16 op * current cursor position. Printing a vline consists of skipping
569 fbadd395 2021-07-16 op * `off' columns (for olivetti-mode), print the correct prefix (which
570 fbadd395 2021-07-16 op * may be the emoji in case of emojified links-lines), printing the
571 fbadd395 2021-07-16 op * text itself, filling until width - off and filling off columns
572 fbadd395 2021-07-16 op * again.
573 fbadd395 2021-07-16 op */
574 754622a2 2021-03-15 op static void
575 f3bcf8f2 2021-06-21 op print_vline(int off, int width, WINDOW *window, struct vline *vl)
576 1d08c280 2021-03-06 op {
577 fbadd395 2021-07-16 op /*
578 fbadd395 2021-07-16 op * Believe me or not, I've seen emoji ten code points long!
579 fbadd395 2021-07-16 op * That means, to stay large, 4*10 bytes + NUL.
580 fbadd395 2021-07-16 op */
581 fbadd395 2021-07-16 op char emojibuf[41] = {0};
582 fbadd395 2021-07-16 op const char *text, *prfx;
583 2af29222 2021-06-24 op struct line_face *f;
584 bd4a08a7 2022-11-07 op int i, left, x, y, textlen;
585 bd9637e9 2021-03-06 op
586 2af29222 2021-06-24 op f = &line_faces[vl->parent->type];
587 2af29222 2021-06-24 op
588 d89b86d6 2021-06-21 op /* unused, set by getyx */
589 f3bcf8f2 2021-06-21 op (void)y;
590 f3bcf8f2 2021-06-21 op
591 28cfdf8f 2022-01-10 op if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
592 28cfdf8f 2022-01-10 op off = 0;
593 28cfdf8f 2022-01-10 op
594 f853ec6f 2024-10-22 op line_prefix_and_text(off, vl, emojibuf, sizeof(emojibuf), &prfx,
595 bd4a08a7 2022-11-07 op &text, &textlen);
596 768db5bb 2021-03-12 op
597 ab47e7d4 2021-06-24 op wattr_on(window, body_face.left, NULL);
598 f3bcf8f2 2021-06-21 op for (i = 0; i < off; i++)
599 f3bcf8f2 2021-06-21 op waddch(window, ' ');
600 ab47e7d4 2021-06-24 op wattr_off(window, body_face.left, NULL);
601 ab47e7d4 2021-06-24 op
602 2af29222 2021-06-24 op wattr_on(window, f->prefix, NULL);
603 6ca7e1b6 2021-07-16 op wprintw(window, "%s", prfx);
604 2af29222 2021-06-24 op wattr_off(window, f->prefix, NULL);
605 803ff456 2021-03-17 op
606 2af29222 2021-06-24 op wattr_on(window, f->text, NULL);
607 6b630429 2024-02-05 op if (text)
608 6b630429 2024-02-05 op wprintw(window, "%.*s", textlen, text);
609 b3be07ea 2021-07-18 op print_vline_descr(width, window, vl);
610 2af29222 2021-06-24 op wattr_off(window, f->text, NULL);
611 f3bcf8f2 2021-06-21 op
612 f3bcf8f2 2021-06-21 op getyx(window, y, x);
613 f3bcf8f2 2021-06-21 op
614 f3bcf8f2 2021-06-21 op left = width - x;
615 f3bcf8f2 2021-06-21 op
616 2af29222 2021-06-24 op wattr_on(window, f->trail, NULL);
617 04d32eda 2021-07-08 op for (i = 0; i < left - off; ++i)
618 f3bcf8f2 2021-06-21 op waddch(window, ' ');
619 2af29222 2021-06-24 op wattr_off(window, f->trail, NULL);
620 160abe04 2021-06-21 op
621 ab47e7d4 2021-06-24 op wattr_on(window, body_face.right, NULL);
622 160abe04 2021-06-21 op for (i = 0; i < off; i++)
623 160abe04 2021-06-21 op waddch(window, ' ');
624 ab47e7d4 2021-06-24 op wattr_off(window, body_face.right, NULL);
625 1d08c280 2021-03-06 op }
626 1d08c280 2021-03-06 op
627 1d08c280 2021-03-06 op static void
628 8af5e5ed 2021-03-08 op redraw_tabline(void)
629 8af5e5ed 2021-03-08 op {
630 7c7d7bb7 2021-03-10 op struct tab *tab;
631 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
632 ce6d5335 2021-07-16 op int current, y, truncated, pair;
633 dc5df781 2021-03-13 op const char *title;
634 119f393c 2021-03-16 op char buf[25];
635 7c7d7bb7 2021-03-10 op
636 923f9ce5 2021-06-21 op x = 0;
637 923f9ce5 2021-06-21 op
638 2815e3a0 2024-09-01 op /* unused, but set by a getyx */
639 923f9ce5 2021-06-21 op (void)y;
640 923f9ce5 2021-06-21 op
641 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
642 8f127baa 2021-04-22 op space = COLS-2;
643 8f127baa 2021-04-22 op
644 a636f50e 2021-03-16 op toskip = 0;
645 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
646 a636f50e 2021-03-16 op toskip++;
647 83dce83d 2021-07-17 op if (tab == current_tab)
648 a636f50e 2021-03-16 op break;
649 a636f50e 2021-03-16 op }
650 8f127baa 2021-04-22 op
651 aa8228f3 2022-01-04 op if (toskip * tabwidth <= space)
652 8f127baa 2021-04-22 op toskip = 0;
653 8f127baa 2021-04-22 op else {
654 8f127baa 2021-04-22 op ots = toskip;
655 a636f50e 2021-03-16 op toskip--;
656 95a8c791 2021-08-26 op while (toskip != 0 &&
657 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
658 8f127baa 2021-04-22 op toskip--;
659 8f127baa 2021-04-22 op }
660 7c7d7bb7 2021-03-10 op
661 a636f50e 2021-03-16 op werase(tabline);
662 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
663 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
664 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
665 a636f50e 2021-03-16 op
666 a636f50e 2021-03-16 op truncated = 0;
667 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
668 a636f50e 2021-03-16 op if (truncated)
669 a636f50e 2021-03-16 op break;
670 a636f50e 2021-03-16 op if (toskip != 0) {
671 a636f50e 2021-03-16 op toskip--;
672 a636f50e 2021-03-16 op continue;
673 a636f50e 2021-03-16 op }
674 a636f50e 2021-03-16 op
675 a636f50e 2021-03-16 op getyx(tabline, y, x);
676 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
677 a636f50e 2021-03-16 op truncated = 1;
678 a636f50e 2021-03-16 op
679 83dce83d 2021-07-17 op current = tab == current_tab;
680 a329982b 2021-03-11 op
681 c1d27b0e 2024-06-14 op if (*(title = tab->buffer.title) == '\0')
682 65c49665 2024-01-23 op title = hist_cur(tab->hist);
683 dc5df781 2021-03-13 op
684 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
685 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
686 e8a76665 2021-05-12 op else
687 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
688 e8a76665 2021-05-12 op
689 95a8c791 2021-08-26 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
690 119f393c 2021-03-16 op /* truncation happens */
691 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
692 119f393c 2021-03-16 op } else {
693 119f393c 2021-03-16 op /* pad with spaces */
694 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
695 119f393c 2021-03-16 op /* nop */ ;
696 119f393c 2021-03-16 op }
697 119f393c 2021-03-16 op
698 ce6d5335 2021-07-16 op pair = current ? tab_face.current : tab_face.tab;
699 ce6d5335 2021-07-16 op wattr_on(tabline, pair, NULL);
700 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
701 ce6d5335 2021-07-16 op wattr_off(tabline, pair, NULL);
702 ce6d5335 2021-07-16 op
703 ce6d5335 2021-07-16 op wattr_on(tabline, tab_face.background, NULL);
704 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
705 05bc942e 2021-07-23 op wprintw(tabline, "┃");
706 ce6d5335 2021-07-16 op wattr_off(tabline, tab_face.background, NULL);
707 7c7d7bb7 2021-03-10 op }
708 119f393c 2021-03-16 op
709 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
710 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
711 119f393c 2021-03-16 op waddch(tabline, ' ');
712 a636f50e 2021-03-16 op if (truncated)
713 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
714 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
715 10346511 2021-03-17 op }
716 77dd24f4 2021-07-05 op
717 77dd24f4 2021-07-05 op /*
718 77dd24f4 2021-07-05 op * Compute the first visible line around vl. Try to search forward
719 77dd24f4 2021-07-05 op * until the end of the buffer; if a visible line is not found, search
720 77dd24f4 2021-07-05 op * backward. Return NULL if no viable line was found.
721 77dd24f4 2021-07-05 op */
722 e7b982f4 2021-07-14 op struct vline *
723 77dd24f4 2021-07-05 op adjust_line(struct vline *vl, struct buffer *buffer)
724 77dd24f4 2021-07-05 op {
725 77dd24f4 2021-07-05 op struct vline *t;
726 10346511 2021-03-17 op
727 b1e1e41a 2021-07-14 op if (vl == NULL)
728 b1e1e41a 2021-07-14 op return NULL;
729 b1e1e41a 2021-07-14 op
730 77dd24f4 2021-07-05 op if (!(vl->parent->flags & L_HIDDEN))
731 77dd24f4 2021-07-05 op return vl;
732 77dd24f4 2021-07-05 op
733 77dd24f4 2021-07-05 op /* search forward */
734 77dd24f4 2021-07-05 op for (t = vl;
735 77dd24f4 2021-07-05 op t != NULL && t->parent->flags & L_HIDDEN;
736 77dd24f4 2021-07-05 op t = TAILQ_NEXT(t, vlines))
737 77dd24f4 2021-07-05 op ; /* nop */
738 77dd24f4 2021-07-05 op
739 77dd24f4 2021-07-05 op if (t != NULL)
740 77dd24f4 2021-07-05 op return t;
741 77dd24f4 2021-07-05 op
742 77dd24f4 2021-07-05 op /* search backward */
743 77dd24f4 2021-07-05 op for (t = vl;
744 77dd24f4 2021-07-05 op t != NULL && t->parent->flags & L_HIDDEN;
745 77dd24f4 2021-07-05 op t = TAILQ_PREV(t, vhead, vlines))
746 77dd24f4 2021-07-05 op ; /* nop */
747 77dd24f4 2021-07-05 op
748 77dd24f4 2021-07-05 op return t;
749 77dd24f4 2021-07-05 op }
750 77dd24f4 2021-07-05 op
751 bddc7bbd 2021-04-01 op static void
752 002b074d 2021-08-14 op redraw_window(WINDOW *win, int off, int height, int width,
753 0aef305d 2022-01-10 op int show_fringe, struct buffer *buffer)
754 bddc7bbd 2021-04-01 op {
755 1a8f0d27 2021-07-20 op struct vline *vl;
756 0aef305d 2022-01-10 op int onscreen = 0, l = 0;
757 13e8b82f 2021-07-01 op
758 0a805b02 2021-07-01 op restore_curs_x(buffer);
759 a00b4c97 2021-06-20 op
760 a00b4c97 2021-06-20 op /*
761 0a805b02 2021-07-01 op * TODO: ignoring buffer->force_update and always
762 0a805b02 2021-07-01 op * re-rendering. In theory we can recompute the y position
763 0a805b02 2021-07-01 op * without a re-render, and optimize here. It's not the only
764 0a805b02 2021-07-01 op * optimisation possible here, wscrl wolud also be an
765 0a805b02 2021-07-01 op * interesting one.
766 a00b4c97 2021-06-20 op */
767 bddc7bbd 2021-04-01 op
768 2bfa414d 2021-07-01 op again:
769 bddc7bbd 2021-04-01 op werase(win);
770 2bfa414d 2021-07-01 op buffer->curs_y = 0;
771 a00b4c97 2021-06-20 op
772 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
773 77dd24f4 2021-07-05 op goto end;
774 77dd24f4 2021-07-05 op
775 b1e1e41a 2021-07-14 op if (buffer->top_line == NULL)
776 c1d27b0e 2024-06-14 op buffer->top_line = TAILQ_FIRST(&buffer->vhead);
777 b1e1e41a 2021-07-14 op
778 77dd24f4 2021-07-05 op buffer->top_line = adjust_line(buffer->top_line, buffer);
779 77dd24f4 2021-07-05 op if (buffer->top_line == NULL)
780 a00b4c97 2021-06-20 op goto end;
781 bddc7bbd 2021-04-01 op
782 77dd24f4 2021-07-05 op buffer->current_line = adjust_line(buffer->current_line, buffer);
783 77dd24f4 2021-07-05 op
784 94ec38e8 2021-06-29 op for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
785 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
786 963c680c 2021-07-05 op continue;
787 963c680c 2021-07-05 op
788 f3bcf8f2 2021-06-21 op wmove(win, l, 0);
789 ec9aa106 2021-07-14 op print_vline(off, width, win, vl);
790 2bfa414d 2021-07-01 op
791 2bfa414d 2021-07-01 op if (vl == buffer->current_line)
792 2bfa414d 2021-07-01 op onscreen = 1;
793 2bfa414d 2021-07-01 op
794 2bfa414d 2021-07-01 op if (!onscreen)
795 2bfa414d 2021-07-01 op buffer->curs_y++;
796 2bfa414d 2021-07-01 op
797 bddc7bbd 2021-04-01 op l++;
798 bddc7bbd 2021-04-01 op if (l == height)
799 bddc7bbd 2021-04-01 op break;
800 2bfa414d 2021-07-01 op }
801 2bfa414d 2021-07-01 op
802 2bfa414d 2021-07-01 op if (!onscreen) {
803 2bfa414d 2021-07-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
804 2bfa414d 2021-07-01 op if (vl == buffer->current_line)
805 2bfa414d 2021-07-01 op break;
806 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
807 963c680c 2021-07-05 op continue;
808 2bfa414d 2021-07-01 op buffer->line_off++;
809 2bfa414d 2021-07-01 op buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
810 2bfa414d 2021-07-01 op }
811 2bfa414d 2021-07-01 op
812 e7b982f4 2021-07-14 op if (vl != NULL)
813 e7b982f4 2021-07-14 op goto again;
814 bddc7bbd 2021-04-01 op }
815 bddc7bbd 2021-04-01 op
816 2bfa414d 2021-07-01 op buffer->last_line_off = buffer->line_off;
817 2bfa414d 2021-07-01 op buffer->force_redraw = 0;
818 a00b4c97 2021-06-20 op end:
819 0aef305d 2022-01-10 op for (; show_fringe && l < height; l++)
820 28cfdf8f 2022-01-10 op print_vline(off, width, win, &fringe);
821 0aef305d 2022-01-10 op
822 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
823 3b5f459e 2021-11-05 op }
824 3b5f459e 2021-11-05 op
825 3b5f459e 2021-11-05 op static void
826 3b5f459e 2021-11-05 op redraw_download(void)
827 3b5f459e 2021-11-05 op {
828 0aef305d 2022-01-10 op redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
829 bddc7bbd 2021-04-01 op }
830 bddc7bbd 2021-04-01 op
831 bddc7bbd 2021-04-01 op static void
832 bddc7bbd 2021-04-01 op redraw_help(void)
833 bddc7bbd 2021-04-01 op {
834 0aef305d 2022-01-10 op redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
835 bddc7bbd 2021-04-01 op }
836 bddc7bbd 2021-04-01 op
837 bddc7bbd 2021-04-01 op static void
838 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
839 bddc7bbd 2021-04-01 op {
840 3323faaf 2021-06-21 op static struct tab *last_tab;
841 3323faaf 2021-06-21 op
842 3323faaf 2021-06-21 op if (last_tab != tab)
843 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
844 3323faaf 2021-06-21 op last_tab = tab;
845 3323faaf 2021-06-21 op
846 0aef305d 2022-01-10 op redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
847 bddc7bbd 2021-04-01 op }
848 bddc7bbd 2021-04-01 op
849 10346511 2021-03-17 op static inline char
850 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
851 10346511 2021-03-17 op {
852 10346511 2021-03-17 op switch (ts) {
853 de278567 2021-07-21 op case TS_UNKNOWN: return '-';
854 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
855 a2fd3805 2021-07-06 op case TS_TEMP_TRUSTED: return '!';
856 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
857 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
858 923f9ce5 2021-06-21 op default: return 'X';
859 10346511 2021-03-17 op }
860 8af5e5ed 2021-03-08 op }
861 8af5e5ed 2021-03-08 op
862 8af5e5ed 2021-03-08 op static void
863 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
864 1d08c280 2021-03-06 op {
865 65340174 2021-07-21 op struct buffer *buffer;
866 481340cc 2021-03-11 op double pct;
867 48e9d457 2021-03-06 op int x, y, max_x, max_y;
868 65340174 2021-07-21 op const char *mode;
869 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
870 1d08c280 2021-03-06 op
871 65340174 2021-07-21 op buffer = current_buffer();
872 c1d27b0e 2024-06-14 op mode = buffer->mode;
873 65340174 2021-07-21 op
874 156f1501 2021-03-13 op werase(modeline);
875 ab47e7d4 2021-06-24 op wattr_on(modeline, modeline_face.background, NULL);
876 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
877 1d08c280 2021-03-06 op
878 a3e4d56b 2024-05-25 thomas.ad wprintw(modeline, "-%c%c%c%c %s ",
879 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
880 10346511 2021-03-17 op trust_status_char(tab->trust),
881 d35e18b3 2024-02-04 op tab->client_cert ? 'C' : '-',
882 a3e4d56b 2024-05-25 thomas.ad tab->faulty_gemserver ? 'W' : '-',
883 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
884 481340cc 2021-03-11 op
885 65340174 2021-07-21 op pct = (buffer->line_off + buffer->curs_y) * 100.0
886 65340174 2021-07-21 op / buffer->line_max;
887 65340174 2021-07-21 op
888 65340174 2021-07-21 op if (buffer->line_max <= (size_t)body_lines)
889 95a8c791 2021-08-26 op wprintw(modeline, "All ");
890 65340174 2021-07-21 op else if (buffer->line_off == 0)
891 95a8c791 2021-08-26 op wprintw(modeline, "Top ");
892 65340174 2021-07-21 op else if (buffer->line_off + body_lines >= buffer->line_max)
893 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
894 481340cc 2021-03-11 op else
895 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
896 481340cc 2021-03-11 op
897 77e9591f 2022-01-13 op wprintw(modeline, "%zu/%zu %s ",
898 65340174 2021-07-21 op buffer->line_off + buffer->curs_y,
899 65340174 2021-07-21 op buffer->line_max,
900 65c49665 2024-01-23 op hist_cur(tab->hist));
901 481340cc 2021-03-11 op
902 48e9d457 2021-03-06 op getyx(modeline, y, x);
903 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
904 48e9d457 2021-03-06 op
905 48e9d457 2021-03-06 op (void)y;
906 48e9d457 2021-03-06 op (void)max_y;
907 48e9d457 2021-03-06 op
908 48e9d457 2021-03-06 op for (; x < max_x; ++x)
909 48e9d457 2021-03-06 op waddstr(modeline, "-");
910 d5493194 2021-06-15 op
911 ab47e7d4 2021-06-24 op wattr_off(modeline, modeline_face.background, NULL);
912 9ca15951 2021-03-09 op }
913 9ca15951 2021-03-09 op
914 9ca15951 2021-03-09 op static void
915 e9af06b9 2021-07-12 op redraw_minibuffer(void)
916 9ca15951 2021-03-09 op {
917 8a7f2683 2021-07-10 op wattr_on(echoarea, minibuffer_face.background, NULL);
918 8a7f2683 2021-07-10 op werase(echoarea);
919 17e5106b 2021-03-21 op
920 e9af06b9 2021-07-12 op if (in_minibuffer)
921 e9af06b9 2021-07-12 op do_redraw_minibuffer();
922 e9af06b9 2021-07-12 op else
923 e9af06b9 2021-07-12 op do_redraw_echoarea();
924 9cb0f9ce 2021-03-10 op
925 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD)
926 b1e1e41a 2021-07-14 op do_redraw_minibuffer_compl();
927 b1e1e41a 2021-07-14 op
928 e9af06b9 2021-07-12 op wattr_off(echoarea, minibuffer_face.background, NULL);
929 e9af06b9 2021-07-12 op }
930 9cb0f9ce 2021-03-10 op
931 e9af06b9 2021-07-12 op static void
932 e9af06b9 2021-07-12 op do_redraw_echoarea(void)
933 e9af06b9 2021-07-12 op {
934 83dce83d 2021-07-17 op struct vline *vl;
935 9cb0f9ce 2021-03-10 op
936 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
937 95a8c791 2021-08-26 op wprintw(echoarea, "%s", ministate.curmesg);
938 e9af06b9 2021-07-12 op else if (*keybuf != '\0')
939 8a7f2683 2021-07-10 op waddstr(echoarea, keybuf);
940 e9af06b9 2021-07-12 op else {
941 e9af06b9 2021-07-12 op /* If nothing else, show the URL at point */
942 83dce83d 2021-07-17 op vl = current_tab->buffer.current_line;
943 83dce83d 2021-07-17 op if (vl != NULL && vl->parent->type == LINE_LINK)
944 83dce83d 2021-07-17 op waddstr(echoarea, vl->parent->alt);
945 8300dd3c 2021-03-18 op }
946 e9af06b9 2021-07-12 op }
947 91a72220 2021-03-13 op
948 e9af06b9 2021-07-12 op static void
949 e9af06b9 2021-07-12 op do_redraw_minibuffer(void)
950 e9af06b9 2021-07-12 op {
951 738a231a 2021-07-24 op struct buffer *cmplbuf, *buffer;
952 e9af06b9 2021-07-12 op size_t off_y, off_x = 0;
953 e9af06b9 2021-07-12 op const char *start, *c;
954 bd4a08a7 2022-11-07 op char *line;
955 d5493194 2021-06-15 op
956 738a231a 2021-07-24 op cmplbuf = &ministate.compl.buffer;
957 738a231a 2021-07-24 op buffer = &ministate.buffer;
958 738a231a 2021-07-24 op (void)off_y; /* unused, set by getyx */
959 e9af06b9 2021-07-12 op
960 35965938 2021-07-15 op wmove(echoarea, 0, 0);
961 35965938 2021-07-15 op
962 35965938 2021-07-15 op if (in_minibuffer == MB_COMPREAD)
963 77e9591f 2022-01-13 op wprintw(echoarea, "(%2zu) ",
964 738a231a 2021-07-24 op cmplbuf->line_max);
965 35965938 2021-07-15 op
966 35965938 2021-07-15 op wprintw(echoarea, "%s", ministate.prompt);
967 65c49665 2024-01-23 op if (!ministate.editing)
968 e9af06b9 2021-07-12 op wprintw(echoarea, "(%zu/%zu) ",
969 65c49665 2024-01-23 op hist_off(ministate.hist) + 1,
970 65c49665 2024-01-23 op hist_size(ministate.hist));
971 e9af06b9 2021-07-12 op
972 e9af06b9 2021-07-12 op getyx(echoarea, off_y, off_x);
973 e9af06b9 2021-07-12 op
974 65c49665 2024-01-23 op start = ministate.buf;
975 65c49665 2024-01-23 op if (!ministate.editing)
976 65c49665 2024-01-23 op start = hist_cur(ministate.hist);
977 bd4a08a7 2022-11-07 op line = buffer->current_line->parent->line + buffer->current_line->from;
978 6d24bfb3 2024-10-19 op c = line + buffer->point_offset;
979 f853ec6f 2024-10-22 op while (start < c && utf8_swidth_between(start, c, off_x) > (size_t)COLS/2) {
980 1dc96d88 2024-10-19 op start += grapheme_next_character_break_utf8(start, SIZE_MAX);
981 e9af06b9 2021-07-12 op }
982 e9af06b9 2021-07-12 op
983 e9af06b9 2021-07-12 op waddstr(echoarea, start);
984 e9af06b9 2021-07-12 op
985 e9af06b9 2021-07-12 op if (ministate.curmesg != NULL)
986 e9af06b9 2021-07-12 op wprintw(echoarea, " [%s]", ministate.curmesg);
987 e9af06b9 2021-07-12 op
988 f853ec6f 2024-10-22 op wmove(echoarea, 0, off_x + utf8_swidth_between(start, c, off_x));
989 b1e1e41a 2021-07-14 op }
990 b1e1e41a 2021-07-14 op
991 b1e1e41a 2021-07-14 op static void
992 b1e1e41a 2021-07-14 op do_redraw_minibuffer_compl(void)
993 b1e1e41a 2021-07-14 op {
994 0aef305d 2022-01-10 op redraw_window(minibuffer, 0, 10, COLS, 1,
995 b1e1e41a 2021-07-14 op &ministate.compl.buffer);
996 55df859f 2021-07-12 op }
997 55df859f 2021-07-12 op
998 55df859f 2021-07-12 op /*
999 55df859f 2021-07-12 op * Place the cursor in the right ncurses window. If soft is 1, use
1000 4ff12389 2021-07-12 op * wnoutrefresh (which shouldn't cause any I/O); otherwise use
1001 55df859f 2021-07-12 op * wrefresh.
1002 55df859f 2021-07-12 op */
1003 55df859f 2021-07-12 op static void
1004 55df859f 2021-07-12 op place_cursor(int soft)
1005 55df859f 2021-07-12 op {
1006 4ff12389 2021-07-12 op int (*touch)(WINDOW *);
1007 55df859f 2021-07-12 op
1008 55df859f 2021-07-12 op if (soft)
1009 55df859f 2021-07-12 op touch = wnoutrefresh;
1010 55df859f 2021-07-12 op else
1011 55df859f 2021-07-12 op touch = wrefresh;
1012 55df859f 2021-07-12 op
1013 55df859f 2021-07-12 op if (in_minibuffer) {
1014 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT)
1015 65340174 2021-07-21 op touch(help);
1016 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_BOTTOM)
1017 3b5f459e 2021-11-05 op touch(download);
1018 65340174 2021-07-21 op touch(body);
1019 65340174 2021-07-21 op touch(echoarea);
1020 3b5f459e 2021-11-05 op } else if (in_side_window & SIDE_WINDOW_LEFT) {
1021 55df859f 2021-07-12 op touch(body);
1022 55df859f 2021-07-12 op touch(echoarea);
1023 3b5f459e 2021-11-05 op if (in_side_window & SIDE_WINDOW_BOTTOM)
1024 3b5f459e 2021-11-05 op touch(download);
1025 65340174 2021-07-21 op touch(help);
1026 3b5f459e 2021-11-05 op } else if (in_side_window & SIDE_WINDOW_BOTTOM) {
1027 3b5f459e 2021-11-05 op touch(body);
1028 3b5f459e 2021-11-05 op touch(echoarea);
1029 3b5f459e 2021-11-05 op if (in_side_window & SIDE_WINDOW_LEFT)
1030 65340174 2021-07-21 op touch(help);
1031 3b5f459e 2021-11-05 op touch(download);
1032 3b5f459e 2021-11-05 op } else {
1033 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT)
1034 3b5f459e 2021-11-05 op touch(help);
1035 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_BOTTOM)
1036 3b5f459e 2021-11-05 op touch(download);
1037 55df859f 2021-07-12 op touch(echoarea);
1038 55df859f 2021-07-12 op touch(body);
1039 55df859f 2021-07-12 op }
1040 48e9d457 2021-03-06 op }
1041 48e9d457 2021-03-06 op
1042 48e9d457 2021-03-06 op static void
1043 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1044 48e9d457 2021-03-06 op {
1045 23f22c83 2021-07-13 op if (too_small)
1046 23f22c83 2021-07-13 op return;
1047 23f22c83 2021-07-13 op
1048 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT) {
1049 bddc7bbd 2021-04-01 op redraw_help();
1050 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1051 bddc7bbd 2021-04-01 op }
1052 bddc7bbd 2021-04-01 op
1053 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_BOTTOM) {
1054 3b5f459e 2021-11-05 op redraw_download();
1055 3b5f459e 2021-11-05 op wnoutrefresh(download);
1056 3b5f459e 2021-11-05 op }
1057 3b5f459e 2021-11-05 op
1058 56168aa8 2021-08-18 op if (show_tab_bar)
1059 56168aa8 2021-08-18 op redraw_tabline();
1060 56168aa8 2021-08-18 op
1061 e19f9a04 2021-03-11 op redraw_body(tab);
1062 e19f9a04 2021-03-11 op redraw_modeline(tab);
1063 e9af06b9 2021-07-12 op redraw_minibuffer();
1064 e19f9a04 2021-03-11 op
1065 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1066 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1067 e19f9a04 2021-03-11 op
1068 b1e1e41a 2021-07-14 op if (in_minibuffer == MB_COMPREAD)
1069 b1e1e41a 2021-07-14 op wnoutrefresh(minibuffer);
1070 b1e1e41a 2021-07-14 op
1071 55df859f 2021-07-12 op place_cursor(1);
1072 bddc7bbd 2021-04-01 op
1073 bddc7bbd 2021-04-01 op doupdate();
1074 9886bf97 2021-07-17 op
1075 9886bf97 2021-07-17 op if (set_title)
1076 47f9af78 2021-08-14 op dprintf(1, "\033]2;%s - Telescope\a",
1077 c1d27b0e 2024-06-14 op current_tab->buffer.title);
1078 e19f9a04 2021-03-11 op }
1079 48e9d457 2021-03-06 op
1080 7f963c41 2021-06-20 op void
1081 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1082 8af5e5ed 2021-03-08 op {
1083 2ba66cea 2021-03-22 op if (tab->loading_anim)
1084 8af5e5ed 2021-03-08 op return;
1085 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1086 98d3e6c1 2024-02-18 op
1087 98d3e6c1 2024-02-18 op ev_timer_cancel(tab->loading_timer);
1088 98d3e6c1 2024-02-18 op tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1089 8af5e5ed 2021-03-08 op }
1090 8af5e5ed 2021-03-08 op
1091 8af5e5ed 2021-03-08 op static void
1092 98d3e6c1 2024-02-18 op update_loading_anim(int fd, int ev, void *d)
1093 8af5e5ed 2021-03-08 op {
1094 8af5e5ed 2021-03-08 op struct tab *tab = d;
1095 8af5e5ed 2021-03-08 op
1096 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1097 9ca15951 2021-03-09 op
1098 83dce83d 2021-07-17 op if (tab == current_tab) {
1099 6347dcd0 2021-03-16 op redraw_modeline(tab);
1100 6347dcd0 2021-03-16 op wrefresh(modeline);
1101 6347dcd0 2021-03-16 op wrefresh(body);
1102 6347dcd0 2021-03-16 op if (in_minibuffer)
1103 8a7f2683 2021-07-10 op wrefresh(echoarea);
1104 6347dcd0 2021-03-16 op }
1105 8af5e5ed 2021-03-08 op
1106 98d3e6c1 2024-02-18 op tab->loading_timer = ev_timer(&loading_tv, update_loading_anim, tab);
1107 8af5e5ed 2021-03-08 op }
1108 8af5e5ed 2021-03-08 op
1109 8af5e5ed 2021-03-08 op static void
1110 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1111 8af5e5ed 2021-03-08 op {
1112 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1113 8af5e5ed 2021-03-08 op return;
1114 98d3e6c1 2024-02-18 op
1115 98d3e6c1 2024-02-18 op ev_timer_cancel(tab->loading_timer);
1116 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1117 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1118 3d8c2326 2021-03-18 op
1119 83dce83d 2021-07-17 op if (tab != current_tab)
1120 3d8c2326 2021-03-18 op return;
1121 43a1b8d0 2021-03-09 op
1122 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1123 9ca15951 2021-03-09 op
1124 43a1b8d0 2021-03-09 op wrefresh(modeline);
1125 43a1b8d0 2021-03-09 op wrefresh(body);
1126 9ca15951 2021-03-09 op if (in_minibuffer)
1127 8a7f2683 2021-07-10 op wrefresh(echoarea);
1128 5e11c00c 2021-03-02 op }
1129 5e11c00c 2021-03-02 op
1130 941b3761 2021-03-18 op int
1131 ad8e93f5 2024-01-13 op ui_init(void)
1132 941b3761 2021-03-18 op {
1133 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1134 4bc446b9 2021-07-21 op
1135 1fa16111 2021-08-12 op if (TAILQ_EMPTY(&global_map.m)) {
1136 1fa16111 2021-08-12 op fprintf(stderr, "no keys defined!\n");
1137 1fa16111 2021-08-12 op return 0;
1138 1fa16111 2021-08-12 op }
1139 1fa16111 2021-08-12 op
1140 4bc446b9 2021-07-21 op minibuffer_init();
1141 2ba66cea 2021-03-22 op
1142 3b5f459e 2021-11-05 op /* initialize download window */
1143 3b5f459e 2021-11-05 op TAILQ_INIT(&downloadwin.head);
1144 c1d27b0e 2024-06-14 op TAILQ_INIT(&downloadwin.vhead);
1145 3b5f459e 2021-11-05 op
1146 bddc7bbd 2021-04-01 op /* initialize help window */
1147 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1148 c1d27b0e 2024-06-14 op TAILQ_INIT(&helpwin.vhead);
1149 bddc7bbd 2021-04-01 op
1150 9ca15951 2021-03-09 op base_map = &global_map;
1151 f832146f 2021-03-09 op current_map = &global_map;
1152 f832146f 2021-03-09 op
1153 5e11c00c 2021-03-02 op initscr();
1154 74a2587f 2021-06-21 op
1155 74a2587f 2021-06-21 op if (enable_colors) {
1156 74a2587f 2021-06-21 op if (has_colors()) {
1157 74a2587f 2021-06-21 op start_color();
1158 160abe04 2021-06-21 op use_default_colors();
1159 f3bcf8f2 2021-06-21 op } else
1160 74a2587f 2021-06-21 op enable_colors = 0;
1161 f3bcf8f2 2021-06-21 op }
1162 74a2587f 2021-06-21 op
1163 42d61f50 2021-06-24 op config_apply_style();
1164 42d61f50 2021-06-24 op
1165 15e1b108 2021-03-02 op raw();
1166 5e11c00c 2021-03-02 op noecho();
1167 5e11c00c 2021-03-02 op nonl();
1168 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1169 5e11c00c 2021-03-02 op
1170 3cea4ff5 2021-08-18 op if ((tabline = newwin(1, 1, 0, 0)) == NULL)
1171 48e9d457 2021-03-06 op return 0;
1172 3cea4ff5 2021-08-18 op if ((body = newwin(1, 1, 0, 0)) == NULL)
1173 48e9d457 2021-03-06 op return 0;
1174 3cea4ff5 2021-08-18 op if ((modeline = newwin(1, 1, 0, 0)) == NULL)
1175 e5a2797f 2021-07-13 op return 0;
1176 3cea4ff5 2021-08-18 op if ((echoarea = newwin(1, 1, 0, 0)) == NULL)
1177 3cea4ff5 2021-08-18 op return 0;
1178 3cea4ff5 2021-08-18 op if ((minibuffer = newwin(1, 1, 0, 0)) == NULL)
1179 48e9d457 2021-03-06 op return 0;
1180 3b5f459e 2021-11-05 op if ((download = newwin(1, 1, 0, 0)) == NULL)
1181 3b5f459e 2021-11-05 op return 0;
1182 3cea4ff5 2021-08-18 op if ((help = newwin(1, 1, 0, 0)) == NULL)
1183 bddc7bbd 2021-04-01 op return 0;
1184 1d08c280 2021-03-06 op
1185 b598590d 2021-06-21 op wbkgd(body, body_face.body);
1186 1577540c 2021-11-05 op wbkgd(download, download_face.background);
1187 8a7f2683 2021-07-10 op wbkgd(echoarea, minibuffer_face.background);
1188 72b18268 2021-06-19 op
1189 72b18268 2021-06-19 op update_x_offset();
1190 48e9d457 2021-03-06 op
1191 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1192 f3bcf8f2 2021-06-21 op scrollok(body, FALSE);
1193 48e9d457 2021-03-06 op
1194 5e11c00c 2021-03-02 op /* non-blocking input */
1195 48e9d457 2021-03-06 op wtimeout(body, 0);
1196 65340174 2021-07-21 op wtimeout(help, 0);
1197 5e11c00c 2021-03-02 op
1198 12ba5e21 2024-02-19 op wmove(body, 0, 0);
1199 5e11c00c 2021-03-02 op
1200 bb28f1c2 2021-12-30 op return 1;
1201 bb28f1c2 2021-12-30 op }
1202 bb28f1c2 2021-12-30 op
1203 bb28f1c2 2021-12-30 op void
1204 bb28f1c2 2021-12-30 op ui_main_loop(void)
1205 bb28f1c2 2021-12-30 op {
1206 329cd624 2024-06-15 op if (ev_signal(SIGWINCH, handle_signal, NULL) == -1 ||
1207 329cd624 2024-06-15 op ev_signal(SIGCHLD, handle_signal, NULL) == -1 ||
1208 98d3e6c1 2024-02-18 op ev_add(0, EV_READ, dispatch_stdio, NULL) == -1)
1209 98d3e6c1 2024-02-18 op err(1, "ev_signal or ev_add failed");
1210 db1e7fc6 2021-07-16 op
1211 2ddbda6b 2021-07-17 op switch_to_tab(current_tab);
1212 3cea4ff5 2021-08-18 op rearrange_windows();
1213 f63b8f73 2022-04-24 op
1214 98d3e6c1 2024-02-18 op ev_loop();
1215 5e11c00c 2021-03-02 op }
1216 5e11c00c 2021-03-02 op
1217 5e11c00c 2021-03-02 op void
1218 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1219 8af5e5ed 2021-03-08 op {
1220 65c49665 2024-01-23 op size_t line_off, curr_off;
1221 65c49665 2024-01-23 op
1222 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1223 65c49665 2024-01-23 op message("Loaded %s", hist_cur(tab->hist));
1224 3d8c2326 2021-03-18 op
1225 65c49665 2024-01-23 op hist_cur_offs(tab->hist, &line_off, &curr_off);
1226 65c49665 2024-01-23 op if (curr_off != 0 &&
1227 c1d27b0e 2024-06-14 op tab->buffer.current_line == TAILQ_FIRST(&tab->buffer.vhead)) {
1228 65c49665 2024-01-23 op set_scroll_position(tab, line_off, curr_off);
1229 e795e935 2022-01-18 op redraw_tab(tab);
1230 e795e935 2022-01-18 op return;
1231 e795e935 2022-01-18 op }
1232 e795e935 2022-01-18 op
1233 56168aa8 2021-08-18 op if (show_tab_bar)
1234 56168aa8 2021-08-18 op redraw_tabline();
1235 56168aa8 2021-08-18 op
1236 3d8c2326 2021-03-18 op wrefresh(tabline);
1237 55df859f 2021-07-12 op place_cursor(0);
1238 8af5e5ed 2021-03-08 op }
1239 8af5e5ed 2021-03-08 op
1240 8af5e5ed 2021-03-08 op void
1241 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1242 5e11c00c 2021-03-02 op {
1243 f853ec6f 2024-10-22 op wrap_page(&tab->buffer, body_cols, x_offset);
1244 83dce83d 2021-07-17 op if (tab == current_tab)
1245 3d8c2326 2021-03-18 op redraw_tab(tab);
1246 02a74b53 2021-07-01 op else
1247 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1248 7a7f8f40 2024-10-22 op }
1249 7a7f8f40 2024-10-22 op
1250 7a7f8f40 2024-10-22 op void
1251 7a7f8f40 2024-10-22 op ui_force_tab_refresh(struct tab *tab)
1252 7a7f8f40 2024-10-22 op {
1253 7a7f8f40 2024-10-22 op switch_to_tab(tab);
1254 7a7f8f40 2024-10-22 op rearrange_windows();
1255 7a7f8f40 2024-10-22 op redraw_tab(tab);
1256 fcd99a0d 2021-11-05 op }
1257 fcd99a0d 2021-11-05 op
1258 fcd99a0d 2021-11-05 op void
1259 fcd99a0d 2021-11-05 op ui_on_download_refresh(void)
1260 fcd99a0d 2021-11-05 op {
1261 98d3e6c1 2024-02-18 op if (ev_timer_pending(download_timer))
1262 0e7b2e99 2022-05-05 op return;
1263 0e7b2e99 2022-05-05 op
1264 98d3e6c1 2024-02-18 op download_timer = ev_timer(&download_refresh_timer,
1265 98d3e6c1 2024-02-18 op handle_download_refresh, NULL);
1266 5e11c00c 2021-03-02 op }
1267 5e11c00c 2021-03-02 op
1268 2f0ffab4 2024-06-22 op static void
1269 2f0ffab4 2024-06-22 op open_download(int res, void *data)
1270 4e32d3a6 2024-06-04 thomas.ad {
1271 2f0ffab4 2024-06-22 op struct download *d = data;
1272 7b7a69f2 2024-06-13 thomas.ad struct mailcap *mc = NULL;
1273 329cd624 2024-06-15 op enum exec_mode mode = EXEC_BACKGROUND;
1274 4e32d3a6 2024-06-04 thomas.ad
1275 2f0ffab4 2024-06-22 op if (!res)
1276 7b7a69f2 2024-06-13 thomas.ad return;
1277 7b7a69f2 2024-06-13 thomas.ad
1278 2f0ffab4 2024-06-22 op if ((mc = mailcap_cmd_from_mimetype(d->mime_type, d->path)) == NULL)
1279 2f0ffab4 2024-06-22 op return;
1280 2f0ffab4 2024-06-22 op
1281 329cd624 2024-06-15 op if (mc->flags & MAILCAP_NEEDSTERMINAL)
1282 329cd624 2024-06-15 op mode = EXEC_FOREGROUND;
1283 329cd624 2024-06-15 op
1284 2f0ffab4 2024-06-22 op message("Loaded %s with %s", d->mime_type, mc->cmd_argv[0]);
1285 0548291a 2024-06-25 op exec_cmd(mc->cmd_argv, mode);
1286 2f0ffab4 2024-06-22 op }
1287 2f0ffab4 2024-06-22 op
1288 2f0ffab4 2024-06-22 op void
1289 2f0ffab4 2024-06-22 op ui_prompt_download_cmd(struct download *d)
1290 2f0ffab4 2024-06-22 op {
1291 2f0ffab4 2024-06-22 op char prompt[64];
1292 2f0ffab4 2024-06-22 op
1293 2f0ffab4 2024-06-22 op snprintf(prompt, sizeof(prompt), "Open %s?", d->path);
1294 2f0ffab4 2024-06-22 op ui_yornp(prompt, open_download, d);
1295 4e32d3a6 2024-06-04 thomas.ad }
1296 4e32d3a6 2024-06-04 thomas.ad
1297 4e32d3a6 2024-06-04 thomas.ad void
1298 0a987358 2022-02-24 op ui_remotely_open_link(const char *uri)
1299 0a987358 2022-02-24 op {
1300 0a987358 2022-02-24 op new_tab(uri, NULL, NULL);
1301 0a987358 2022-02-24 op ui_on_tab_refresh(current_tab);
1302 0a987358 2022-02-24 op
1303 0a987358 2022-02-24 op /* ring the bell */
1304 0a987358 2022-02-24 op printf("\a");
1305 0a987358 2022-02-24 op fflush(stdout);
1306 0a987358 2022-02-24 op }
1307 0a987358 2022-02-24 op
1308 2b2d2872 2021-06-20 op const char *
1309 2b2d2872 2021-06-20 op ui_keyname(int k)
1310 2b2d2872 2021-06-20 op {
1311 2b2d2872 2021-06-20 op return keyname(k);
1312 2b2d2872 2021-06-20 op }
1313 2b2d2872 2021-06-20 op
1314 5e11c00c 2021-03-02 op void
1315 3b5f459e 2021-11-05 op ui_toggle_side_window(int kind)
1316 2b2d2872 2021-06-20 op {
1317 3b5f459e 2021-11-05 op if (in_side_window & kind)
1318 3b5f459e 2021-11-05 op ui_other_window();
1319 3b5f459e 2021-11-05 op
1320 3b5f459e 2021-11-05 op side_window ^= kind;
1321 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_LEFT)
1322 2b2d2872 2021-06-20 op recompute_help();
1323 3b5f459e 2021-11-05 op if (side_window & SIDE_WINDOW_BOTTOM)
1324 3b5f459e 2021-11-05 op recompute_downloads();
1325 960b01da 2021-06-20 op
1326 960b01da 2021-06-20 op /*
1327 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1328 32c6c75e 2021-07-15 op * until I call rearrange_windows a second time (e.g. via
1329 32c6c75e 2021-07-15 op * C-l). I will be happy to know why something like this is
1330 32c6c75e 2021-07-15 op * needed.
1331 960b01da 2021-06-20 op */
1332 32c6c75e 2021-07-15 op rearrange_windows();
1333 32c6c75e 2021-07-15 op rearrange_windows();
1334 2b2d2872 2021-06-20 op }
1335 2b2d2872 2021-06-20 op
1336 2b2d2872 2021-06-20 op void
1337 fcd99a0d 2021-11-05 op ui_show_downloads_pane(void)
1338 fcd99a0d 2021-11-05 op {
1339 fcd99a0d 2021-11-05 op if (!(side_window & SIDE_WINDOW_BOTTOM))
1340 fcd99a0d 2021-11-05 op ui_toggle_side_window(SIDE_WINDOW_BOTTOM);
1341 fcd99a0d 2021-11-05 op }
1342 fcd99a0d 2021-11-05 op
1343 fcd99a0d 2021-11-05 op void
1344 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1345 2b2d2872 2021-06-20 op {
1346 32c6c75e 2021-07-15 op should_rearrange_windows = 1;
1347 2b2d2872 2021-06-20 op }
1348 2b2d2872 2021-06-20 op
1349 2b2d2872 2021-06-20 op void
1350 0fceedb5 2024-06-07 op ui_require_input(struct tab *tab, int hide, void (*fn)(const char *))
1351 5cd2ebb1 2021-03-11 op {
1352 dfd1efcc 2024-06-23 op struct minibuffer m = {
1353 dfd1efcc 2024-06-23 op .self_insert = sensible_self_insert,
1354 dfd1efcc 2024-06-23 op .done = fn,
1355 dfd1efcc 2024-06-23 op .history = ir_history,
1356 dfd1efcc 2024-06-23 op };
1357 dfd1efcc 2024-06-23 op
1358 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1359 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1360 5cd2ebb1 2021-03-11 op
1361 dfd1efcc 2024-06-23 op enter_minibuffer(&m, "Input required: ");
1362 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1363 5cd2ebb1 2021-03-11 op }
1364 5cd2ebb1 2021-03-11 op
1365 5cd2ebb1 2021-03-11 op void
1366 4bc446b9 2021-07-21 op ui_after_message_hook(void)
1367 4bc446b9 2021-07-21 op {
1368 4bc446b9 2021-07-21 op redraw_minibuffer();
1369 4bc446b9 2021-07-21 op place_cursor(0);
1370 4bc446b9 2021-07-21 op }
1371 4bc446b9 2021-07-21 op
1372 4bc446b9 2021-07-21 op void
1373 057e7eb6 2024-06-22 op ui_yornp(const char *prompt, void (*fn)(int, void *), void *data)
1374 5d1bac73 2021-03-25 op {
1375 84b88039 2021-07-12 op yornp(prompt, fn, data);
1376 83dce83d 2021-07-17 op redraw_tab(current_tab);
1377 5d1bac73 2021-03-25 op }
1378 5d1bac73 2021-03-25 op
1379 5d1bac73 2021-03-25 op void
1380 d1353324 2021-07-13 op ui_read(const char *prompt, void (*fn)(const char*, struct tab *),
1381 bb9912eb 2021-08-29 op struct tab *data, const char *input)
1382 de2a69bb 2021-05-17 op {
1383 479b240a 2024-06-25 op minibuffer_read(prompt, fn, data, input);
1384 83dce83d 2021-07-17 op redraw_tab(current_tab);
1385 8a3b5609 2021-07-15 op }
1386 8a3b5609 2021-07-15 op
1387 8a3b5609 2021-07-15 op void
1388 65340174 2021-07-21 op ui_other_window(void)
1389 65340174 2021-07-21 op {
1390 3b5f459e 2021-11-05 op if (in_side_window & SIDE_WINDOW_LEFT &&
1391 3b5f459e 2021-11-05 op side_window & SIDE_WINDOW_BOTTOM)
1392 3b5f459e 2021-11-05 op in_side_window = SIDE_WINDOW_BOTTOM;
1393 3b5f459e 2021-11-05 op else if (in_side_window)
1394 3b5f459e 2021-11-05 op in_side_window = 0;
1395 3b5f459e 2021-11-05 op else if (!in_side_window && side_window & SIDE_WINDOW_LEFT)
1396 3b5f459e 2021-11-05 op in_side_window = SIDE_WINDOW_LEFT;
1397 3b5f459e 2021-11-05 op else if (!in_side_window && side_window)
1398 3b5f459e 2021-11-05 op in_side_window = SIDE_WINDOW_BOTTOM;
1399 65340174 2021-07-21 op else
1400 65340174 2021-07-21 op message("No other window to select");
1401 65340174 2021-07-21 op }
1402 65340174 2021-07-21 op
1403 65340174 2021-07-21 op void
1404 8a3b5609 2021-07-15 op ui_suspend(void)
1405 8a3b5609 2021-07-15 op {
1406 8a3b5609 2021-07-15 op endwin();
1407 0548291a 2024-06-25 op }
1408 8a3b5609 2021-07-15 op
1409 0548291a 2024-06-25 op void
1410 0548291a 2024-06-25 op ui_resume(void)
1411 0548291a 2024-06-25 op {
1412 8a3b5609 2021-07-15 op refresh();
1413 8a3b5609 2021-07-15 op clear();
1414 8a3b5609 2021-07-15 op rearrange_windows();
1415 740f578b 2021-03-15 op }
1416 740f578b 2021-03-15 op
1417 740f578b 2021-03-15 op void
1418 5e11c00c 2021-03-02 op ui_end(void)
1419 5e11c00c 2021-03-02 op {
1420 4e32d3a6 2024-06-04 thomas.ad endwin();
1421 4e32d3a6 2024-06-04 thomas.ad }