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