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