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