2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
20 1d08c280 2021-03-06 op * Text scrolling
21 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.
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.
33 e2226342 2021-05-12 op #include "telescope.h"
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 5e11c00c 2021-03-02 op #include <locale.h>
39 5e11c00c 2021-03-02 op #include <signal.h>
40 7953dd72 2021-03-07 op #include <stdarg.h>
41 eb259e66 2021-03-02 op #include <stdlib.h>
42 eb259e66 2021-03-02 op #include <string.h>
43 f832146f 2021-03-09 op #include <unistd.h>
45 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
47 0a805b02 2021-07-01 op static void restore_curs_x(struct buffer *);
48 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
49 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
50 5d1bac73 2021-03-25 op static void yornp_self_insert(void);
51 5d1bac73 2021-03-25 op static void yornp_abort(void);
52 de2a69bb 2021-05-17 op static void read_self_insert(void);
53 de2a69bb 2021-05-17 op static void read_abort(void);
54 de2a69bb 2021-05-17 op static void read_select(void);
56 46f6e974 2021-05-17 op static struct vline *nth_line(struct buffer*, size_t);
57 46f6e974 2021-05-17 op static struct buffer *current_buffer(void);
58 8947c1f2 2021-03-21 op static int readkey(void);
59 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
60 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
61 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
62 a8a482c8 2021-05-17 op static void handle_resize_nodelay(int, short, void*);
63 46f6e974 2021-05-17 op static int wrap_page(struct buffer*, int);
64 f3bcf8f2 2021-06-21 op static void print_vline(int, int, WINDOW*, struct vline*);
65 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
66 f3bcf8f2 2021-06-21 op static void redraw_window(WINDOW*, int, int, struct buffer*);
67 bddc7bbd 2021-04-01 op static void redraw_help(void);
68 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
69 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
70 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
71 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
72 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
73 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
74 bddc7bbd 2021-04-01 op static void recompute_help(void);
75 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
76 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
78 72b18268 2021-06-19 op static int x_offset;
80 2b2d2872 2021-06-20 op struct thiskey thiskey;
82 831deb20 2021-05-12 op static struct event resizeev;
83 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
85 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
87 2b2d2872 2021-06-20 op int body_lines, body_cols;
89 bddc7bbd 2021-04-01 op static WINDOW *help;
90 46f6e974 2021-05-17 op static struct buffer helpwin;
91 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
93 bddc7bbd 2021-04-01 op static int side_window;
95 48e9d457 2021-03-06 op static struct event clminibufev;
96 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
97 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
99 bcb0b073 2021-03-07 op static uint32_t tab_counter;
101 7c7d7bb7 2021-03-10 op static char keybuf[64];
103 a2fd3805 2021-07-06 op static void (*yornp_cb)(int, struct tab *);
104 a2fd3805 2021-07-06 op static struct tab *yornp_data;
106 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
107 de2a69bb 2021-05-17 op static unsigned int read_data;
109 9ca15951 2021-03-09 op struct kmap global_map,
110 fa3fd864 2021-03-10 op minibuffer_map,
111 9ca15951 2021-03-09 op *current_map,
114 2b2d2872 2021-06-20 op struct histhead eecmd_history,
117 de2a69bb 2021-05-17 op read_history;
119 2b2d2872 2021-06-20 op int in_minibuffer;
121 2b2d2872 2021-06-20 op struct ministate ministate;
123 72b18268 2021-06-19 op static inline void
124 923f9ce5 2021-06-21 op update_x_offset(void)
126 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
127 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
129 72b18268 2021-06-19 op x_offset = 0;
133 98411855 2021-06-23 op save_excursion(struct excursion *place, struct buffer *buffer)
135 98411855 2021-06-23 op place->curs_x = buffer->curs_x;
136 98411855 2021-06-23 op place->curs_y = buffer->curs_y;
137 98411855 2021-06-23 op place->line_off = buffer->line_off;
138 98411855 2021-06-23 op place->current_line = buffer->current_line;
139 98411855 2021-06-23 op place->cpoff = buffer->cpoff;
143 98411855 2021-06-23 op restore_excursion(struct excursion *place, struct buffer *buffer)
145 98411855 2021-06-23 op buffer->curs_x = place->curs_x;
146 98411855 2021-06-23 op buffer->curs_y = place->curs_y;
147 98411855 2021-06-23 op buffer->line_off = place->line_off;
148 98411855 2021-06-23 op buffer->current_line = place->current_line;
149 98411855 2021-06-23 op buffer->cpoff = place->cpoff;
153 0a805b02 2021-07-01 op restore_curs_x(struct buffer *buffer)
155 452589f7 2021-03-16 op struct vline *vl;
156 452589f7 2021-03-16 op const char *prfx;
158 46f6e974 2021-05-17 op vl = buffer->current_line;
159 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
160 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
162 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
164 72b18268 2021-06-19 op buffer->curs_x += x_offset;
166 452589f7 2021-03-16 op if (vl != NULL) {
167 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
168 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
173 870210fb 2021-03-26 op global_key_unbound(void)
175 870210fb 2021-03-26 op message("%s is undefined", keybuf);
179 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
181 3148eeac 2021-03-13 op struct hist *hist;
183 22268e11 2021-03-11 op if (ministate.history == NULL)
186 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
189 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
191 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
192 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
194 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
195 22268e11 2021-03-11 op ministate.history->len++;
199 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
200 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
201 22268e11 2021-03-11 op * navigation" thing.
204 22268e11 2021-03-11 op minibuffer_taint_hist(void)
206 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
209 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
210 22268e11 2021-03-11 op ministate.hist_cur = NULL;
214 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
216 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
219 22268e11 2021-03-11 op minibuffer_taint_hist();
221 17e5106b 2021-03-21 op if (thiskey.cp == 0)
224 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
225 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
226 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
229 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
230 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
231 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
235 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
237 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
238 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
239 5cd2ebb1 2021-03-11 op global_key_unbound();
243 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
247 b360ebb3 2021-03-10 op eecmd_select(void)
249 a3666ed5 2021-06-25 op struct cmd *cmd;
251 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
252 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
253 2fe98292 2021-03-26 op exit_minibuffer();
254 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
255 46f6e974 2021-05-17 op cmd->fn(current_buffer());
260 2fe98292 2021-03-26 op message("No match");
264 5cd2ebb1 2021-03-11 op ir_self_insert(void)
266 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
270 5cd2ebb1 2021-03-11 op ir_select(void)
272 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
273 31f1a758 2021-04-22 op struct phos_uri uri;
274 5cd2ebb1 2021-03-11 op struct tab *tab;
276 5cd2ebb1 2021-03-11 op tab = current_tab();
278 5cd2ebb1 2021-03-11 op exit_minibuffer();
279 22268e11 2021-03-11 op minibuffer_hist_save_entry();
281 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
282 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
283 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
284 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
285 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
289 5cd2ebb1 2021-03-11 op lu_self_insert(void)
291 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
292 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
293 5cd2ebb1 2021-03-11 op global_key_unbound();
297 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
301 5cd2ebb1 2021-03-11 op lu_select(void)
303 5cd2ebb1 2021-03-11 op exit_minibuffer();
304 22268e11 2021-03-11 op minibuffer_hist_save_entry();
305 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
309 740f578b 2021-03-15 op bp_select(void)
311 740f578b 2021-03-15 op exit_minibuffer();
312 740f578b 2021-03-15 op if (*ministate.buf != '\0')
313 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
315 740f578b 2021-03-15 op message("Abort.");
319 5d1bac73 2021-03-25 op yornp_self_insert(void)
321 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
322 5d1bac73 2021-03-25 op message("Please answer y or n");
326 5d1bac73 2021-03-25 op exit_minibuffer();
327 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
331 5d1bac73 2021-03-25 op yornp_abort(void)
333 5d1bac73 2021-03-25 op exit_minibuffer();
334 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
338 de2a69bb 2021-05-17 op read_self_insert(void)
340 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
341 de2a69bb 2021-05-17 op global_key_unbound();
345 de2a69bb 2021-05-17 op minibuffer_self_insert();
349 de2a69bb 2021-05-17 op read_abort(void)
351 de2a69bb 2021-05-17 op exit_minibuffer();
352 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
356 de2a69bb 2021-05-17 op read_select(void)
358 de2a69bb 2021-05-17 op exit_minibuffer();
359 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
360 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
363 9a25f829 2021-03-14 op static struct vline *
364 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
366 9a25f829 2021-03-14 op struct vline *vl;
370 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
376 48e9d457 2021-03-06 op /* unreachable */
381 5e11c00c 2021-03-02 op current_tab(void)
383 5e11c00c 2021-03-02 op struct tab *t;
385 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
386 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
390 5e11c00c 2021-03-02 op /* unreachable */
394 2b2d2872 2021-06-20 op struct buffer *
395 46f6e974 2021-05-17 op current_buffer(void)
397 2ba66cea 2021-03-22 op if (in_minibuffer)
398 46f6e974 2021-05-17 op return &ministate.buffer;
399 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
403 8947c1f2 2021-03-21 op readkey(void)
405 8947c1f2 2021-03-21 op uint32_t state = 0;
407 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
410 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
411 8947c1f2 2021-03-21 op if (thiskey.meta) {
412 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
413 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
414 c314a314 2021-03-11 op thiskey.meta = 0;
415 9ca15951 2021-03-09 op thiskey.key = 27;
419 8947c1f2 2021-03-21 op thiskey.cp = 0;
420 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
422 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
424 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
425 8947c1f2 2021-03-21 op message("Error decoding user input");
435 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
437 8947c1f2 2021-03-21 op struct keymap *k;
438 8947c1f2 2021-03-21 op const char *keyname;
439 8947c1f2 2021-03-21 op char tmp[5] = {0};
441 8947c1f2 2021-03-21 op if (!readkey())
444 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
445 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
446 7c7d7bb7 2021-03-10 op if (thiskey.meta)
447 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
448 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
449 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
450 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
452 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
453 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
455 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
456 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
460 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
461 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
462 9ca15951 2021-03-09 op k->key == thiskey.key) {
463 f832146f 2021-03-09 op if (k->fn == NULL)
464 f832146f 2021-03-09 op current_map = &k->map;
466 9ca15951 2021-03-09 op current_map = base_map;
467 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
468 46f6e974 2021-05-17 op k->fn(current_buffer());
474 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
475 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
477 7c7d7bb7 2021-03-10 op global_key_unbound();
479 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
480 9ca15951 2021-03-09 op current_map = base_map;
483 bddc7bbd 2021-04-01 op if (side_window)
484 bddc7bbd 2021-04-01 op recompute_help();
486 179f0f58 2021-03-11 op redraw_tab(current_tab());
490 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
492 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
493 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
495 9cb0f9ce 2021-03-10 op redraw_minibuffer();
496 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
497 9cb0f9ce 2021-03-10 op wrefresh(body);
498 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
500 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
501 9cb0f9ce 2021-03-10 op wrefresh(body);
506 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
508 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
509 831deb20 2021-05-12 op event_del(&resizeev);
511 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
512 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
516 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
518 1d08c280 2021-03-06 op struct tab *tab;
524 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
526 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
527 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
529 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
530 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
532 48e9d457 2021-03-06 op body_lines = LINES-3;
533 bd9637e9 2021-03-06 op body_cols = COLS;
535 bddc7bbd 2021-04-01 op if (side_window) {
536 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
537 bddc7bbd 2021-04-01 op help_lines = LINES-3;
538 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
539 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
541 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
543 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
544 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
546 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
548 72b18268 2021-06-19 op update_x_offset();
549 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
551 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
553 1d08c280 2021-03-06 op tab = current_tab();
555 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
556 1d08c280 2021-03-06 op redraw_tab(tab);
560 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
562 452589f7 2021-03-16 op struct line *l;
563 2bfa414d 2021-07-01 op const struct line *top_orig, *orig;
564 d511dc85 2021-03-16 op struct vline *vl;
565 72b18268 2021-06-19 op int pre_width;
566 452589f7 2021-03-16 op const char *prfx;
568 2bfa414d 2021-07-01 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
569 94ec38e8 2021-06-29 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
571 94ec38e8 2021-06-29 op buffer->top_line = NULL;
572 46f6e974 2021-05-17 op buffer->current_line = NULL;
574 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
575 46f6e974 2021-05-17 op buffer->curs_y = 0;
576 46f6e974 2021-05-17 op buffer->line_off = 0;
578 46f6e974 2021-05-17 op empty_vlist(buffer);
580 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
581 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
582 5e11c00c 2021-03-02 op switch (l->type) {
583 5e11c00c 2021-03-02 op case LINE_TEXT:
584 5e11c00c 2021-03-02 op case LINE_LINK:
585 5e11c00c 2021-03-02 op case LINE_TITLE_1:
586 5e11c00c 2021-03-02 op case LINE_TITLE_2:
587 5e11c00c 2021-03-02 op case LINE_TITLE_3:
588 5e11c00c 2021-03-02 op case LINE_ITEM:
589 5e11c00c 2021-03-02 op case LINE_QUOTE:
590 5e11c00c 2021-03-02 op case LINE_PRE_START:
591 5e11c00c 2021-03-02 op case LINE_PRE_END:
592 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
594 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
595 72b18268 2021-06-19 op if (olivetti_mode)
596 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
598 72b18268 2021-06-19 op pre_width = width;
599 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
603 2bfa414d 2021-07-01 op if (top_orig == l && buffer->top_line == NULL) {
604 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
605 2bfa414d 2021-07-01 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
608 2bfa414d 2021-07-01 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
609 2bfa414d 2021-07-01 op if (vl == NULL || vl->parent != orig)
611 2bfa414d 2021-07-01 op buffer->top_line = vl;
612 2bfa414d 2021-07-01 op buffer->line_off--;
616 2bfa414d 2021-07-01 op if (orig == l && buffer->current_line == NULL) {
617 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
620 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
621 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
623 46f6e974 2021-05-17 op buffer->current_line = vl;
628 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
629 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
631 2bfa414d 2021-07-01 op if (buffer->top_line == NULL)
632 2bfa414d 2021-07-01 op buffer->top_line = buffer->current_line;
638 f3bcf8f2 2021-06-21 op print_vline(int off, int width, WINDOW *window, struct vline *vl)
640 2af29222 2021-06-24 op const char *text;
641 768db5bb 2021-03-12 op const char *prfx;
642 2af29222 2021-06-24 op struct line_face *f;
643 f3bcf8f2 2021-06-21 op int i, left, x, y;
645 2af29222 2021-06-24 op f = &line_faces[vl->parent->type];
647 d89b86d6 2021-06-21 op /* unused, set by getyx */
650 9a25f829 2021-03-14 op if (!vl->flags)
651 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
653 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
655 2af29222 2021-06-24 op text = vl->line;
656 bd9637e9 2021-03-06 op if (text == NULL)
659 ab47e7d4 2021-06-24 op wattr_on(window, body_face.left, NULL);
660 f3bcf8f2 2021-06-21 op for (i = 0; i < off; i++)
661 f3bcf8f2 2021-06-21 op waddch(window, ' ');
662 ab47e7d4 2021-06-24 op wattr_off(window, body_face.left, NULL);
664 2af29222 2021-06-24 op wattr_on(window, f->prefix, NULL);
665 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
666 2af29222 2021-06-24 op wattr_off(window, f->prefix, NULL);
668 2af29222 2021-06-24 op wattr_on(window, f->text, NULL);
669 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
670 2af29222 2021-06-24 op wattr_off(window, f->text, NULL);
672 f3bcf8f2 2021-06-21 op getyx(window, y, x);
674 f3bcf8f2 2021-06-21 op left = width - x;
676 2af29222 2021-06-24 op wattr_on(window, f->trail, NULL);
677 04d32eda 2021-07-08 op for (i = 0; i < left - off; ++i)
678 f3bcf8f2 2021-06-21 op waddch(window, ' ');
679 2af29222 2021-06-24 op wattr_off(window, f->trail, NULL);
681 ab47e7d4 2021-06-24 op wattr_on(window, body_face.right, NULL);
682 160abe04 2021-06-21 op for (i = 0; i < off; i++)
683 160abe04 2021-06-21 op waddch(window, ' ');
684 ab47e7d4 2021-06-24 op wattr_off(window, body_face.right, NULL);
689 8af5e5ed 2021-03-08 op redraw_tabline(void)
691 7c7d7bb7 2021-03-10 op struct tab *tab;
692 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
693 8f127baa 2021-04-22 op int current, y, truncated;
694 dc5df781 2021-03-13 op const char *title;
695 119f393c 2021-03-16 op char buf[25];
699 923f9ce5 2021-06-21 op /* unused, but setted by a getyx */
702 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
703 8f127baa 2021-04-22 op space = COLS-2;
706 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
708 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
712 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
715 8f127baa 2021-04-22 op ots = toskip;
717 8f127baa 2021-04-22 op while (toskip != 0 &&
718 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
722 a636f50e 2021-03-16 op werase(tabline);
723 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
724 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
725 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
727 a636f50e 2021-03-16 op truncated = 0;
728 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
729 a636f50e 2021-03-16 op if (truncated)
731 a636f50e 2021-03-16 op if (toskip != 0) {
736 a636f50e 2021-03-16 op getyx(tabline, y, x);
737 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
738 a636f50e 2021-03-16 op truncated = 1;
740 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
742 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
743 2051e653 2021-03-13 op title = tab->hist_cur->h;
745 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
746 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
748 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
750 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
751 119f393c 2021-03-16 op /* truncation happens */
752 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
754 119f393c 2021-03-16 op /* pad with spaces */
755 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
760 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.current, NULL);
762 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.tab, NULL);
764 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
765 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
766 119f393c 2021-03-16 op wprintw(tabline, " ");
769 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.current, NULL);
771 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.tab, NULL);
774 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
775 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
776 119f393c 2021-03-16 op waddch(tabline, ' ');
777 a636f50e 2021-03-16 op if (truncated)
778 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
779 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
783 77dd24f4 2021-07-05 op * Compute the first visible line around vl. Try to search forward
784 77dd24f4 2021-07-05 op * until the end of the buffer; if a visible line is not found, search
785 77dd24f4 2021-07-05 op * backward. Return NULL if no viable line was found.
787 77dd24f4 2021-07-05 op static inline struct vline *
788 77dd24f4 2021-07-05 op adjust_line(struct vline *vl, struct buffer *buffer)
790 77dd24f4 2021-07-05 op struct vline *t;
792 77dd24f4 2021-07-05 op if (!(vl->parent->flags & L_HIDDEN))
795 77dd24f4 2021-07-05 op /* search forward */
797 77dd24f4 2021-07-05 op t != NULL && t->parent->flags & L_HIDDEN;
798 77dd24f4 2021-07-05 op t = TAILQ_NEXT(t, vlines))
801 77dd24f4 2021-07-05 op if (t != NULL)
804 77dd24f4 2021-07-05 op /* search backward */
806 77dd24f4 2021-07-05 op t != NULL && t->parent->flags & L_HIDDEN;
807 77dd24f4 2021-07-05 op t = TAILQ_PREV(t, vhead, vlines))
814 f3bcf8f2 2021-06-21 op redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
816 2bfa414d 2021-07-01 op struct vline *vl;
817 2bfa414d 2021-07-01 op int l, onscreen;
819 0a805b02 2021-07-01 op restore_curs_x(buffer);
822 0a805b02 2021-07-01 op * TODO: ignoring buffer->force_update and always
823 0a805b02 2021-07-01 op * re-rendering. In theory we can recompute the y position
824 0a805b02 2021-07-01 op * without a re-render, and optimize here. It's not the only
825 0a805b02 2021-07-01 op * optimisation possible here, wscrl wolud also be an
826 0a805b02 2021-07-01 op * interesting one.
831 2bfa414d 2021-07-01 op buffer->curs_y = 0;
833 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
836 77dd24f4 2021-07-05 op buffer->top_line = adjust_line(buffer->top_line, buffer);
837 77dd24f4 2021-07-05 op if (buffer->top_line == NULL)
840 77dd24f4 2021-07-05 op buffer->current_line = adjust_line(buffer->current_line, buffer);
843 2bfa414d 2021-07-01 op onscreen = 0;
844 94ec38e8 2021-06-29 op for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
845 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
848 f3bcf8f2 2021-06-21 op wmove(win, l, 0);
849 f3bcf8f2 2021-06-21 op print_vline(x_offset, width, win, vl);
851 2bfa414d 2021-07-01 op if (vl == buffer->current_line)
852 2bfa414d 2021-07-01 op onscreen = 1;
854 2bfa414d 2021-07-01 op if (!onscreen)
855 2bfa414d 2021-07-01 op buffer->curs_y++;
858 bddc7bbd 2021-04-01 op if (l == height)
862 2bfa414d 2021-07-01 op if (!onscreen) {
863 2bfa414d 2021-07-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
864 2bfa414d 2021-07-01 op if (vl == buffer->current_line)
866 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
868 2bfa414d 2021-07-01 op buffer->line_off++;
869 2bfa414d 2021-07-01 op buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
875 2bfa414d 2021-07-01 op buffer->last_line_off = buffer->line_off;
876 2bfa414d 2021-07-01 op buffer->force_redraw = 0;
878 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
882 bddc7bbd 2021-04-01 op redraw_help(void)
884 f3bcf8f2 2021-06-21 op redraw_window(help, help_lines, help_cols, &helpwin);
888 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
890 3323faaf 2021-06-21 op static struct tab *last_tab;
892 3323faaf 2021-06-21 op if (last_tab != tab)
893 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
894 3323faaf 2021-06-21 op last_tab = tab;
896 f3bcf8f2 2021-06-21 op redraw_window(body, body_lines, body_cols, &tab->buffer);
899 10346511 2021-03-17 op static inline char
900 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
902 10346511 2021-03-17 op switch (ts) {
903 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
904 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
905 a2fd3805 2021-07-06 op case TS_TEMP_TRUSTED: return '!';
906 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
907 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
908 923f9ce5 2021-06-21 op default: return 'X';
913 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
916 48e9d457 2021-03-06 op int x, y, max_x, max_y;
917 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
918 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
920 156f1501 2021-03-13 op werase(modeline);
921 ab47e7d4 2021-06-24 op wattr_on(modeline, modeline_face.background, NULL);
922 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
924 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
925 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
926 10346511 2021-03-17 op trust_status_char(tab->trust),
927 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
929 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
931 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
932 481340cc 2021-03-11 op wprintw(modeline, "All ");
933 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
934 481340cc 2021-03-11 op wprintw(modeline, "Top ");
935 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
936 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
938 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
940 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
941 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
942 46f6e974 2021-05-17 op tab->buffer.line_max,
943 2051e653 2021-03-13 op tab->hist_cur->h);
945 48e9d457 2021-03-06 op getyx(modeline, y, x);
946 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
951 48e9d457 2021-03-06 op for (; x < max_x; ++x)
952 48e9d457 2021-03-06 op waddstr(modeline, "-");
954 ab47e7d4 2021-06-24 op wattr_off(modeline, modeline_face.background, NULL);
958 9ca15951 2021-03-09 op redraw_minibuffer(void)
960 8300dd3c 2021-03-18 op struct tab *tab;
961 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
962 923f9ce5 2021-06-21 op char *start = NULL, *c = NULL;
964 923f9ce5 2021-06-21 op /* unused, but set by getyx */
967 ab47e7d4 2021-06-24 op wattr_on(minibuf, minibuffer_face.background, NULL);
968 156f1501 2021-03-13 op werase(minibuf);
970 22268e11 2021-03-11 op if (in_minibuffer) {
971 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
972 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
973 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
974 22268e11 2021-03-11 op ministate.hist_off + 1,
975 22268e11 2021-03-11 op ministate.history->len);
977 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
979 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
980 17e5106b 2021-03-21 op ? ministate.hist_cur->h
981 17e5106b 2021-03-21 op : ministate.buf;
982 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
983 46f6e974 2021-05-17 op ministate.buffer.cpoff);
984 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
985 17e5106b 2021-03-21 op start = utf8_next_cp(start);
988 17e5106b 2021-03-21 op waddstr(minibuf, start);
991 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
992 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
993 17e5106b 2021-03-21 op ministate.curmesg);
995 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
996 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
998 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
999 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1000 8300dd3c 2021-03-18 op tab = current_tab();
1001 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1002 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1003 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1006 2aaf475e 2021-03-13 op if (in_minibuffer)
1007 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1009 ab47e7d4 2021-06-24 op wattr_off(minibuf, minibuffer_face.background, NULL);
1013 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1015 bddc7bbd 2021-04-01 op if (side_window) {
1016 bddc7bbd 2021-04-01 op redraw_help();
1017 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1020 e19f9a04 2021-03-11 op redraw_tabline();
1021 e19f9a04 2021-03-11 op redraw_body(tab);
1022 e19f9a04 2021-03-11 op redraw_modeline(tab);
1023 e19f9a04 2021-03-11 op redraw_minibuffer();
1025 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1026 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1028 e19f9a04 2021-03-11 op if (in_minibuffer) {
1029 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1030 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1032 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1033 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1040 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1042 bddc7bbd 2021-04-01 op struct line *l;
1043 a3666ed5 2021-06-25 op struct cmd *cmd;
1045 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1046 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1049 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1051 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1054 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1055 bddc7bbd 2021-04-01 op l->alt = NULL;
1057 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1059 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1060 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1062 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1066 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1068 bddc7bbd 2021-04-01 op struct keymap *k;
1070 bddc7bbd 2021-04-01 op const char *kn;
1072 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1073 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1074 bddc7bbd 2021-04-01 op if (*p != '\0')
1075 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1076 bddc7bbd 2021-04-01 op if (k->meta)
1077 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1078 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1079 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1081 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1083 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1084 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1086 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1091 bddc7bbd 2021-04-01 op recompute_help(void)
1093 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1095 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1096 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1097 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1098 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1102 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1104 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1105 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1107 bf992581 2021-03-12 op free(ministate.curmesg);
1108 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1110 2b2d2872 2021-06-20 op if (fmt != NULL) {
1111 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1112 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1114 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1115 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1116 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1119 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1120 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1121 9cb0f9ce 2021-03-10 op wrefresh(body);
1122 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1124 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1125 9cb0f9ce 2021-03-10 op wrefresh(body);
1130 740f578b 2021-03-15 op message(const char *fmt, ...)
1134 740f578b 2021-03-15 op va_start(ap, fmt);
1135 740f578b 2021-03-15 op vmessage(fmt, ap);
1140 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1142 2ba66cea 2021-03-22 op if (tab->loading_anim)
1144 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1145 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1146 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1150 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1152 8af5e5ed 2021-03-08 op struct tab *tab = d;
1154 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1156 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1157 6347dcd0 2021-03-16 op redraw_modeline(tab);
1158 6347dcd0 2021-03-16 op wrefresh(modeline);
1159 6347dcd0 2021-03-16 op wrefresh(body);
1160 6347dcd0 2021-03-16 op if (in_minibuffer)
1161 6347dcd0 2021-03-16 op wrefresh(minibuf);
1164 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1168 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1170 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1172 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1173 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1174 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1176 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1179 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1181 43a1b8d0 2021-03-09 op wrefresh(modeline);
1182 43a1b8d0 2021-03-09 op wrefresh(body);
1183 9ca15951 2021-03-09 op if (in_minibuffer)
1184 9ca15951 2021-03-09 op wrefresh(minibuf);
1188 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1190 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1191 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1192 8af5e5ed 2021-03-08 op load_url(tab, url);
1194 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1195 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1196 43a1b8d0 2021-03-09 op redraw_tab(tab);
1200 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1201 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1203 9ca15951 2021-03-09 op in_minibuffer = 1;
1204 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1205 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1207 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1209 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1210 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1211 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1212 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1213 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1214 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1215 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1217 22268e11 2021-03-11 op ministate.history = hist;
1218 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1219 22268e11 2021-03-11 op ministate.hist_off = 0;
1223 b360ebb3 2021-03-10 op exit_minibuffer(void)
1225 156f1501 2021-03-13 op werase(minibuf);
1227 9ca15951 2021-03-09 op in_minibuffer = 0;
1228 9ca15951 2021-03-09 op base_map = &global_map;
1229 9ca15951 2021-03-09 op current_map = &global_map;
1233 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1235 5cd2ebb1 2021-03-11 op struct tab *t;
1237 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1238 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1241 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1242 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1245 2eef3403 2021-04-22 op unsigned int
1246 2eef3403 2021-04-22 op tab_new_id(void)
1248 2eef3403 2021-04-22 op return tab_counter++;
1251 2b2d2872 2021-06-20 op struct tab *
1252 1b81bc33 2021-03-15 op new_tab(const char *url)
1254 754622a2 2021-03-15 op struct tab *tab;
1256 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1257 71105afa 2021-03-16 op event_loopbreak();
1258 71105afa 2021-03-16 op return NULL;
1260 de2a69bb 2021-05-17 op tab->fd = -1;
1262 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1264 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1266 2eef3403 2021-04-22 op tab->id = tab_new_id();
1267 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1269 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1270 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1272 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1274 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1281 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1283 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1284 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1285 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1287 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1288 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1289 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1291 bddc7bbd 2021-04-01 op /* initialize help window */
1292 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1294 9ca15951 2021-03-09 op base_map = &global_map;
1295 f832146f 2021-03-09 op current_map = &global_map;
1299 74a2587f 2021-06-21 op if (enable_colors) {
1300 74a2587f 2021-06-21 op if (has_colors()) {
1301 74a2587f 2021-06-21 op start_color();
1302 160abe04 2021-06-21 op use_default_colors();
1304 74a2587f 2021-06-21 op enable_colors = 0;
1307 42d61f50 2021-06-24 op config_apply_style();
1312 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1314 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1316 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1318 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1320 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1322 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1325 48e9d457 2021-03-06 op body_lines = LINES-3;
1326 48e9d457 2021-03-06 op body_cols = COLS;
1328 b598590d 2021-06-21 op wbkgd(body, body_face.body);
1329 24221637 2021-06-25 op wbkgd(minibuf, minibuffer_face.background);
1331 72b18268 2021-06-19 op update_x_offset();
1333 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1334 f3bcf8f2 2021-06-21 op scrollok(body, FALSE);
1336 5e11c00c 2021-03-02 op /* non-blocking input */
1337 48e9d457 2021-03-06 op wtimeout(body, 0);
1339 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1341 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1342 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1344 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1345 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1351 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1353 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1354 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1356 3d8c2326 2021-03-18 op redraw_tabline();
1357 3d8c2326 2021-03-18 op wrefresh(tabline);
1358 3d8c2326 2021-03-18 op if (in_minibuffer)
1359 3d8c2326 2021-03-18 op wrefresh(minibuf);
1361 3d8c2326 2021-03-18 op wrefresh(body);
1365 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1367 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1368 02a74b53 2021-07-01 op if (tab->flags & TAB_CURRENT)
1369 3d8c2326 2021-03-18 op redraw_tab(tab);
1371 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1374 2b2d2872 2021-06-20 op const char *
1375 2b2d2872 2021-06-20 op ui_keyname(int k)
1377 2b2d2872 2021-06-20 op return keyname(k);
1381 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1383 2b2d2872 2021-06-20 op side_window = !side_window;
1384 2b2d2872 2021-06-20 op if (side_window)
1385 2b2d2872 2021-06-20 op recompute_help();
1388 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1389 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1390 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1392 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1393 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1397 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1399 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1403 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1405 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1406 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1408 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1409 22268e11 2021-03-11 op &ir_history);
1410 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1411 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1412 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1416 a2fd3805 2021-07-06 op ui_yornp(const char *prompt, void (*fn)(int, struct tab *),
1417 a2fd3805 2021-07-06 op struct tab *data)
1421 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1422 6f66d9bf 2021-04-24 op fn(0, data);
1426 5d1bac73 2021-03-25 op yornp_cb = fn;
1427 e49ce157 2021-04-01 op yornp_data = data;
1428 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1429 5d1bac73 2021-03-25 op yornp_abort, NULL);
1431 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1432 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1433 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1434 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1438 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1439 de2a69bb 2021-05-17 op unsigned int data)
1443 de2a69bb 2021-05-17 op if (in_minibuffer)
1446 de2a69bb 2021-05-17 op read_cb = fn;
1447 de2a69bb 2021-05-17 op read_data = data;
1448 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1449 de2a69bb 2021-05-17 op &read_history);
1451 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1452 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1453 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1454 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1458 5e11c00c 2021-03-02 op ui_end(void)