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 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>
46 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
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);
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);
81 72b18268 2021-06-19 op static int x_offset;
83 2b2d2872 2021-06-20 op struct thiskey thiskey;
85 831deb20 2021-05-12 op static struct event resizeev;
86 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
88 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
90 2b2d2872 2021-06-20 op int body_lines, body_cols;
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;
96 bddc7bbd 2021-04-01 op static int side_window;
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 };
102 bcb0b073 2021-03-07 op static uint32_t tab_counter;
104 7c7d7bb7 2021-03-10 op static char keybuf[64];
106 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
107 b3575139 2021-04-01 op static unsigned int yornp_data;
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;
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,
117 2b2d2872 2021-06-20 op struct histhead eecmd_history,
120 de2a69bb 2021-05-17 op read_history;
122 2b2d2872 2021-06-20 op int in_minibuffer;
124 2b2d2872 2021-06-20 op struct ministate ministate;
126 72b18268 2021-06-19 op static inline void
127 923f9ce5 2021-06-21 op update_x_offset(void)
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;
132 72b18268 2021-06-19 op x_offset = 0;
136 98411855 2021-06-23 op save_excursion(struct excursion *place, struct buffer *buffer)
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;
146 98411855 2021-06-23 op restore_excursion(struct excursion *place, struct buffer *buffer)
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;
156 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
158 452589f7 2021-03-16 op struct vline *vl;
159 452589f7 2021-03-16 op const char *prfx;
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;
165 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
167 72b18268 2021-06-19 op buffer->curs_x += x_offset;
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);
176 870210fb 2021-03-26 op global_key_unbound(void)
178 870210fb 2021-03-26 op message("%s is undefined", keybuf);
182 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
184 3148eeac 2021-03-13 op struct hist *hist;
186 22268e11 2021-03-11 op if (ministate.history == NULL)
189 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
192 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
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);
197 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
198 22268e11 2021-03-11 op ministate.history->len++;
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.
207 22268e11 2021-03-11 op minibuffer_taint_hist(void)
209 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
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;
217 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
219 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
222 22268e11 2021-03-11 op minibuffer_taint_hist();
224 17e5106b 2021-03-21 op if (thiskey.cp == 0)
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)
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++;
238 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
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();
246 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
250 b360ebb3 2021-03-10 op eecmd_select(void)
252 a3666ed5 2021-06-25 op struct cmd *cmd;
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());
263 2fe98292 2021-03-26 op message("No match");
267 5cd2ebb1 2021-03-11 op ir_self_insert(void)
269 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
273 5cd2ebb1 2021-03-11 op ir_select(void)
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;
279 5cd2ebb1 2021-03-11 op tab = current_tab();
281 5cd2ebb1 2021-03-11 op exit_minibuffer();
282 22268e11 2021-03-11 op minibuffer_hist_save_entry();
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);
292 5cd2ebb1 2021-03-11 op lu_self_insert(void)
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();
300 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
304 5cd2ebb1 2021-03-11 op lu_select(void)
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);
312 740f578b 2021-03-15 op bp_select(void)
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);
318 740f578b 2021-03-15 op message("Abort.");
322 5d1bac73 2021-03-25 op yornp_self_insert(void)
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");
329 5d1bac73 2021-03-25 op exit_minibuffer();
330 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
334 5d1bac73 2021-03-25 op yornp_abort(void)
336 5d1bac73 2021-03-25 op exit_minibuffer();
337 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
341 de2a69bb 2021-05-17 op read_self_insert(void)
343 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
344 de2a69bb 2021-05-17 op global_key_unbound();
348 de2a69bb 2021-05-17 op minibuffer_self_insert();
352 de2a69bb 2021-05-17 op read_abort(void)
354 de2a69bb 2021-05-17 op exit_minibuffer();
355 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
359 de2a69bb 2021-05-17 op read_select(void)
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);
366 9a25f829 2021-03-14 op static struct vline *
367 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
369 9a25f829 2021-03-14 op struct vline *vl;
373 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
379 48e9d457 2021-03-06 op /* unreachable */
384 5e11c00c 2021-03-02 op current_tab(void)
386 5e11c00c 2021-03-02 op struct tab *t;
388 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
389 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
393 5e11c00c 2021-03-02 op /* unreachable */
397 2b2d2872 2021-06-20 op struct buffer *
398 46f6e974 2021-05-17 op current_buffer(void)
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 ¤t_tab()->buffer;
406 8947c1f2 2021-03-21 op readkey(void)
408 8947c1f2 2021-03-21 op uint32_t state = 0;
410 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
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;
422 8947c1f2 2021-03-21 op thiskey.cp = 0;
423 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
425 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
427 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
428 8947c1f2 2021-03-21 op message("Error decoding user input");
438 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
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};
444 8947c1f2 2021-03-21 op if (!readkey())
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));
455 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
456 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
458 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
459 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
463 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_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;
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());
477 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
478 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
480 7c7d7bb7 2021-03-10 op global_key_unbound();
482 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
483 9ca15951 2021-03-09 op current_map = base_map;
486 bddc7bbd 2021-04-01 op if (side_window)
487 bddc7bbd 2021-04-01 op recompute_help();
489 179f0f58 2021-03-11 op redraw_tab(current_tab());
493 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
495 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
496 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
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);
503 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
504 9cb0f9ce 2021-03-10 op wrefresh(body);
509 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
511 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
512 831deb20 2021-05-12 op event_del(&resizeev);
514 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
515 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
519 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
521 1d08c280 2021-03-06 op struct tab *tab;
527 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
529 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
530 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
532 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
533 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
535 48e9d457 2021-03-06 op body_lines = LINES-3;
536 bd9637e9 2021-03-06 op body_cols = COLS;
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);
544 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
546 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
547 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
549 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
551 72b18268 2021-06-19 op update_x_offset();
552 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
554 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
556 1d08c280 2021-03-06 op tab = current_tab();
558 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
559 1d08c280 2021-03-06 op redraw_tab(tab);
563 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
565 452589f7 2021-03-16 op struct line *l;
566 452589f7 2021-03-16 op const struct line *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;
571 94ec38e8 2021-06-29 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
573 94ec38e8 2021-06-29 op buffer->top_line = NULL;
574 46f6e974 2021-05-17 op buffer->current_line = NULL;
576 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
577 46f6e974 2021-05-17 op buffer->curs_y = 0;
578 46f6e974 2021-05-17 op buffer->line_off = 0;
580 46f6e974 2021-05-17 op empty_vlist(buffer);
582 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
583 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
584 5e11c00c 2021-03-02 op switch (l->type) {
585 5e11c00c 2021-03-02 op case LINE_TEXT:
586 5e11c00c 2021-03-02 op case LINE_LINK:
587 5e11c00c 2021-03-02 op case LINE_TITLE_1:
588 5e11c00c 2021-03-02 op case LINE_TITLE_2:
589 5e11c00c 2021-03-02 op case LINE_TITLE_3:
590 5e11c00c 2021-03-02 op case LINE_ITEM:
591 5e11c00c 2021-03-02 op case LINE_QUOTE:
592 5e11c00c 2021-03-02 op case LINE_PRE_START:
593 5e11c00c 2021-03-02 op case LINE_PRE_END:
594 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
596 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
597 72b18268 2021-06-19 op if (olivetti_mode)
598 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
600 72b18268 2021-06-19 op pre_width = width;
601 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
605 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
606 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
607 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
610 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
611 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
613 46f6e974 2021-05-17 op buffer->current_line = vl;
614 46f6e974 2021-05-17 op buffer->line_off--;
619 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
620 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
622 94ec38e8 2021-06-29 op buffer->top_line = buffer->current_line;
628 f3bcf8f2 2021-06-21 op print_vline(int off, int width, WINDOW *window, struct vline *vl)
630 2af29222 2021-06-24 op const char *text;
631 768db5bb 2021-03-12 op const char *prfx;
632 2af29222 2021-06-24 op struct line_face *f;
633 f3bcf8f2 2021-06-21 op int i, left, x, y;
635 2af29222 2021-06-24 op f = &line_faces[vl->parent->type];
637 d89b86d6 2021-06-21 op /* unused, set by getyx */
640 9a25f829 2021-03-14 op if (!vl->flags)
641 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
643 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
645 2af29222 2021-06-24 op text = vl->line;
646 bd9637e9 2021-03-06 op if (text == NULL)
649 ab47e7d4 2021-06-24 op wattr_on(window, body_face.left, NULL);
650 f3bcf8f2 2021-06-21 op for (i = 0; i < off; i++)
651 f3bcf8f2 2021-06-21 op waddch(window, ' ');
652 ab47e7d4 2021-06-24 op wattr_off(window, body_face.left, NULL);
654 2af29222 2021-06-24 op wattr_on(window, f->prefix, NULL);
655 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
656 2af29222 2021-06-24 op wattr_off(window, f->prefix, NULL);
658 2af29222 2021-06-24 op wattr_on(window, f->text, NULL);
659 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
660 2af29222 2021-06-24 op wattr_off(window, f->text, NULL);
662 f3bcf8f2 2021-06-21 op getyx(window, y, x);
664 f3bcf8f2 2021-06-21 op left = width - x;
666 2af29222 2021-06-24 op wattr_on(window, f->trail, NULL);
667 d89b86d6 2021-06-21 op for (i = 0; i < left - off - 1; ++i)
668 f3bcf8f2 2021-06-21 op waddch(window, ' ');
669 2af29222 2021-06-24 op wattr_off(window, f->trail, NULL);
671 ab47e7d4 2021-06-24 op wattr_on(window, body_face.right, NULL);
672 160abe04 2021-06-21 op for (i = 0; i < off; i++)
673 160abe04 2021-06-21 op waddch(window, ' ');
674 ab47e7d4 2021-06-24 op wattr_off(window, body_face.right, NULL);
679 8af5e5ed 2021-03-08 op redraw_tabline(void)
681 7c7d7bb7 2021-03-10 op struct tab *tab;
682 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
683 8f127baa 2021-04-22 op int current, y, truncated;
684 dc5df781 2021-03-13 op const char *title;
685 119f393c 2021-03-16 op char buf[25];
689 923f9ce5 2021-06-21 op /* unused, but setted by a getyx */
692 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
693 8f127baa 2021-04-22 op space = COLS-2;
696 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
698 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
702 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
705 8f127baa 2021-04-22 op ots = toskip;
707 8f127baa 2021-04-22 op while (toskip != 0 &&
708 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
712 a636f50e 2021-03-16 op werase(tabline);
713 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
714 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
715 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
717 a636f50e 2021-03-16 op truncated = 0;
718 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
719 a636f50e 2021-03-16 op if (truncated)
721 a636f50e 2021-03-16 op if (toskip != 0) {
726 a636f50e 2021-03-16 op getyx(tabline, y, x);
727 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
728 a636f50e 2021-03-16 op truncated = 1;
730 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
732 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
733 2051e653 2021-03-13 op title = tab->hist_cur->h;
735 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
736 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
738 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
740 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
741 119f393c 2021-03-16 op /* truncation happens */
742 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
744 119f393c 2021-03-16 op /* pad with spaces */
745 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
750 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.current, NULL);
752 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.tab, NULL);
754 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
755 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
756 119f393c 2021-03-16 op wprintw(tabline, " ");
759 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.current, NULL);
761 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.tab, NULL);
764 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
765 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
766 119f393c 2021-03-16 op waddch(tabline, ' ');
767 a636f50e 2021-03-16 op if (truncated)
768 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
769 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
773 f3bcf8f2 2021-06-21 op redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
775 bddc7bbd 2021-04-01 op struct vline *vl;
779 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
780 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
781 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
783 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
784 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
789 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
790 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
791 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
793 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
797 94ec38e8 2021-06-29 op for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
798 f3bcf8f2 2021-06-21 op wmove(win, l, 0);
799 f3bcf8f2 2021-06-21 op print_vline(x_offset, width, win, vl);
801 bddc7bbd 2021-04-01 op if (l == height)
806 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
810 bddc7bbd 2021-04-01 op redraw_help(void)
812 f3bcf8f2 2021-06-21 op redraw_window(help, help_lines, help_cols, &helpwin);
816 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
818 3323faaf 2021-06-21 op static struct tab *last_tab;
820 3323faaf 2021-06-21 op if (last_tab != tab)
821 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
822 3323faaf 2021-06-21 op last_tab = tab;
824 f3bcf8f2 2021-06-21 op redraw_window(body, body_lines, body_cols, &tab->buffer);
827 10346511 2021-03-17 op static inline char
828 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
830 10346511 2021-03-17 op switch (ts) {
831 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
832 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
833 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
834 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
835 923f9ce5 2021-06-21 op default: return 'X';
840 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
843 48e9d457 2021-03-06 op int x, y, max_x, max_y;
844 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
845 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
847 156f1501 2021-03-13 op werase(modeline);
848 ab47e7d4 2021-06-24 op wattr_on(modeline, modeline_face.background, NULL);
849 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
851 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
852 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
853 10346511 2021-03-17 op trust_status_char(tab->trust),
854 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
856 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
858 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
859 481340cc 2021-03-11 op wprintw(modeline, "All ");
860 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
861 481340cc 2021-03-11 op wprintw(modeline, "Top ");
862 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
863 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
865 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
867 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
868 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
869 46f6e974 2021-05-17 op tab->buffer.line_max,
870 2051e653 2021-03-13 op tab->hist_cur->h);
872 48e9d457 2021-03-06 op getyx(modeline, y, x);
873 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
878 48e9d457 2021-03-06 op for (; x < max_x; ++x)
879 48e9d457 2021-03-06 op waddstr(modeline, "-");
881 ab47e7d4 2021-06-24 op wattr_off(modeline, modeline_face.background, NULL);
885 9ca15951 2021-03-09 op redraw_minibuffer(void)
887 8300dd3c 2021-03-18 op struct tab *tab;
888 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
889 923f9ce5 2021-06-21 op char *start = NULL, *c = NULL;
891 923f9ce5 2021-06-21 op /* unused, but set by getyx */
894 ab47e7d4 2021-06-24 op wattr_on(minibuf, minibuffer_face.background, NULL);
895 156f1501 2021-03-13 op werase(minibuf);
897 22268e11 2021-03-11 op if (in_minibuffer) {
898 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
899 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
900 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
901 22268e11 2021-03-11 op ministate.hist_off + 1,
902 22268e11 2021-03-11 op ministate.history->len);
904 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
906 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
907 17e5106b 2021-03-21 op ? ministate.hist_cur->h
908 17e5106b 2021-03-21 op : ministate.buf;
909 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
910 46f6e974 2021-05-17 op ministate.buffer.cpoff);
911 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
912 17e5106b 2021-03-21 op start = utf8_next_cp(start);
915 17e5106b 2021-03-21 op waddstr(minibuf, start);
918 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
919 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
920 17e5106b 2021-03-21 op ministate.curmesg);
922 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
923 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
925 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
926 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
927 8300dd3c 2021-03-18 op tab = current_tab();
928 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
929 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
930 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
933 2aaf475e 2021-03-13 op if (in_minibuffer)
934 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
936 ab47e7d4 2021-06-24 op wattr_off(minibuf, minibuffer_face.background, NULL);
940 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
942 bddc7bbd 2021-04-01 op if (side_window) {
943 bddc7bbd 2021-04-01 op redraw_help();
944 bddc7bbd 2021-04-01 op wnoutrefresh(help);
947 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
949 e19f9a04 2021-03-11 op redraw_tabline();
950 e19f9a04 2021-03-11 op redraw_body(tab);
951 e19f9a04 2021-03-11 op redraw_modeline(tab);
952 e19f9a04 2021-03-11 op redraw_minibuffer();
954 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
955 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
957 e19f9a04 2021-03-11 op if (in_minibuffer) {
958 bddc7bbd 2021-04-01 op wnoutrefresh(body);
959 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
961 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
962 bddc7bbd 2021-04-01 op wnoutrefresh(body);
969 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
971 bddc7bbd 2021-04-01 op struct line *l;
972 a3666ed5 2021-06-25 op struct cmd *cmd;
974 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
975 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
978 bddc7bbd 2021-04-01 op assert(cmd != NULL);
980 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
983 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
984 bddc7bbd 2021-04-01 op l->alt = NULL;
986 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
988 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
989 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
991 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
995 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
997 bddc7bbd 2021-04-01 op struct keymap *k;
999 bddc7bbd 2021-04-01 op const char *kn;
1001 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1002 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1003 bddc7bbd 2021-04-01 op if (*p != '\0')
1004 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1005 bddc7bbd 2021-04-01 op if (k->meta)
1006 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1007 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1008 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1010 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1012 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1013 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1015 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1020 bddc7bbd 2021-04-01 op recompute_help(void)
1022 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1024 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1025 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1026 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1027 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1031 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1033 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1034 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1036 bf992581 2021-03-12 op free(ministate.curmesg);
1037 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1039 2b2d2872 2021-06-20 op if (fmt != NULL) {
1040 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1041 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1043 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1044 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1045 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1048 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1049 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1050 9cb0f9ce 2021-03-10 op wrefresh(body);
1051 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1053 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1054 9cb0f9ce 2021-03-10 op wrefresh(body);
1059 740f578b 2021-03-15 op message(const char *fmt, ...)
1063 740f578b 2021-03-15 op va_start(ap, fmt);
1064 740f578b 2021-03-15 op vmessage(fmt, ap);
1069 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1071 2ba66cea 2021-03-22 op if (tab->loading_anim)
1073 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1074 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1075 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1079 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1081 8af5e5ed 2021-03-08 op struct tab *tab = d;
1083 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1085 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1086 6347dcd0 2021-03-16 op redraw_modeline(tab);
1087 6347dcd0 2021-03-16 op wrefresh(modeline);
1088 6347dcd0 2021-03-16 op wrefresh(body);
1089 6347dcd0 2021-03-16 op if (in_minibuffer)
1090 6347dcd0 2021-03-16 op wrefresh(minibuf);
1093 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1097 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1099 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1101 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1102 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1103 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1105 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1108 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1110 43a1b8d0 2021-03-09 op wrefresh(modeline);
1111 43a1b8d0 2021-03-09 op wrefresh(body);
1112 9ca15951 2021-03-09 op if (in_minibuffer)
1113 9ca15951 2021-03-09 op wrefresh(minibuf);
1117 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1119 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1120 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1121 8af5e5ed 2021-03-08 op load_url(tab, url);
1123 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1124 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1125 43a1b8d0 2021-03-09 op redraw_tab(tab);
1129 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1130 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1132 9ca15951 2021-03-09 op in_minibuffer = 1;
1133 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1134 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1136 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1138 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1139 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1140 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1141 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1142 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1143 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1144 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1146 22268e11 2021-03-11 op ministate.history = hist;
1147 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1148 22268e11 2021-03-11 op ministate.hist_off = 0;
1152 b360ebb3 2021-03-10 op exit_minibuffer(void)
1154 156f1501 2021-03-13 op werase(minibuf);
1156 9ca15951 2021-03-09 op in_minibuffer = 0;
1157 9ca15951 2021-03-09 op base_map = &global_map;
1158 9ca15951 2021-03-09 op current_map = &global_map;
1162 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1164 5cd2ebb1 2021-03-11 op struct tab *t;
1166 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1167 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1170 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1171 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1174 2eef3403 2021-04-22 op unsigned int
1175 2eef3403 2021-04-22 op tab_new_id(void)
1177 2eef3403 2021-04-22 op return tab_counter++;
1180 2b2d2872 2021-06-20 op struct tab *
1181 1b81bc33 2021-03-15 op new_tab(const char *url)
1183 754622a2 2021-03-15 op struct tab *tab;
1185 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1186 71105afa 2021-03-16 op event_loopbreak();
1187 71105afa 2021-03-16 op return NULL;
1189 de2a69bb 2021-05-17 op tab->fd = -1;
1191 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1193 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1195 2eef3403 2021-04-22 op tab->id = tab_new_id();
1196 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1198 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1199 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1201 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1203 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1208 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1210 c7107cec 2021-04-01 op new_tab(url);
1216 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1217 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1221 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1223 c92e529c 2021-06-15 op char path[PATH_MAX];
1224 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1225 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1227 74a2587f 2021-06-21 op if (getenv("NO_COLOR") != NULL)
1228 74a2587f 2021-06-21 op enable_colors = 0;
1230 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1231 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1233 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1234 941b3761 2021-03-18 op switch (ch) {
1237 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1240 c92e529c 2021-06-15 op configtest = 1;
1250 941b3761 2021-03-18 op argc -= optind;
1251 941b3761 2021-03-18 op argv += optind;
1253 e3427d18 2021-06-25 op /* setup keys before reading the config */
1254 e3427d18 2021-06-25 op TAILQ_INIT(&global_map.m);
1255 e3427d18 2021-06-25 op global_map.unhandled_input = global_key_unbound;
1257 e3427d18 2021-06-25 op TAILQ_INIT(&minibuffer_map.m);
1259 33fc3a8f 2021-06-21 op config_init();
1260 c92e529c 2021-06-15 op parseconfig(path, fonf);
1261 c92e529c 2021-06-15 op if (configtest){
1262 c92e529c 2021-06-15 op puts("config OK");
1266 941b3761 2021-03-18 op if (argc != 0)
1267 941b3761 2021-03-18 op url = argv[0];
1269 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1271 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1272 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1273 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1275 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1276 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1277 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1279 bddc7bbd 2021-04-01 op /* initialize help window */
1280 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1282 9ca15951 2021-03-09 op base_map = &global_map;
1283 f832146f 2021-03-09 op current_map = &global_map;
1287 74a2587f 2021-06-21 op if (enable_colors) {
1288 74a2587f 2021-06-21 op if (has_colors()) {
1289 74a2587f 2021-06-21 op start_color();
1290 160abe04 2021-06-21 op use_default_colors();
1292 74a2587f 2021-06-21 op enable_colors = 0;
1295 42d61f50 2021-06-24 op config_apply_style();
1300 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1302 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1304 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1306 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1308 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1310 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1313 48e9d457 2021-03-06 op body_lines = LINES-3;
1314 48e9d457 2021-03-06 op body_cols = COLS;
1316 b598590d 2021-06-21 op wbkgd(body, body_face.body);
1317 24221637 2021-06-25 op wbkgd(minibuf, minibuffer_face.background);
1319 72b18268 2021-06-19 op update_x_offset();
1321 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1322 f3bcf8f2 2021-06-21 op scrollok(body, FALSE);
1324 5e11c00c 2021-03-02 op /* non-blocking input */
1325 48e9d457 2021-03-06 op wtimeout(body, 0);
1327 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1329 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1330 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1332 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1333 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1335 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1336 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1337 c7107cec 2021-04-01 op new_tab(url);
1343 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1345 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1346 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1348 3d8c2326 2021-03-18 op redraw_tabline();
1349 3d8c2326 2021-03-18 op wrefresh(tabline);
1350 3d8c2326 2021-03-18 op if (in_minibuffer)
1351 3d8c2326 2021-03-18 op wrefresh(minibuf);
1353 3d8c2326 2021-03-18 op wrefresh(body);
1357 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1359 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1360 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1361 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1362 3d8c2326 2021-03-18 op redraw_tab(tab);
1364 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1367 2b2d2872 2021-06-20 op const char *
1368 2b2d2872 2021-06-20 op ui_keyname(int k)
1370 2b2d2872 2021-06-20 op return keyname(k);
1374 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1376 2b2d2872 2021-06-20 op side_window = !side_window;
1377 2b2d2872 2021-06-20 op if (side_window)
1378 2b2d2872 2021-06-20 op recompute_help();
1381 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1382 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1383 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1385 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1386 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1390 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1392 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1396 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1398 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1399 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1401 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1402 22268e11 2021-03-11 op &ir_history);
1403 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1404 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1405 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1409 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1410 b3575139 2021-04-01 op unsigned int data)
1414 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1415 6f66d9bf 2021-04-24 op fn(0, data);
1419 5d1bac73 2021-03-25 op yornp_cb = fn;
1420 e49ce157 2021-04-01 op yornp_data = data;
1421 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1422 5d1bac73 2021-03-25 op yornp_abort, NULL);
1424 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1425 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1426 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1427 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1431 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1432 de2a69bb 2021-05-17 op unsigned int data)
1436 de2a69bb 2021-05-17 op if (in_minibuffer)
1439 de2a69bb 2021-05-17 op read_cb = fn;
1440 de2a69bb 2021-05-17 op read_data = data;
1441 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1442 de2a69bb 2021-05-17 op &read_history);
1444 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1445 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1446 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1447 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1451 5e11c00c 2021-03-02 op ui_end(void)