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.
21 1d08c280 2021-03-06 op * Text wrapping
22 1d08c280 2021-03-06 op * =============
24 1d08c280 2021-03-06 op * There's a simple text wrapping algorithm.
26 1d08c280 2021-03-06 op * 1. if it's a line in a pre-formatted block:
27 1d08c280 2021-03-06 op * a. hard wrap.
29 1d08c280 2021-03-06 op * 2. there is enough room for the next word?
30 1d08c280 2021-03-06 op * a. yes: render it
31 1d08c280 2021-03-06 op * b. no: break the current line.
32 1d08c280 2021-03-06 op * i. while there isn't enough space to draw the current
33 1d08c280 2021-03-06 op * word, hard-wrap it
34 1d08c280 2021-03-06 op * ii. draw the remainder of the current word (can be the
35 1d08c280 2021-03-06 op * the entirely)
36 1d08c280 2021-03-06 op * 3. render the spaces after the word
37 1d08c280 2021-03-06 op * a. but if there is not enough room break the line and
42 1d08c280 2021-03-06 op * Text scrolling
43 1d08c280 2021-03-06 op * ==============
45 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
46 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
47 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
48 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
49 1d08c280 2021-03-06 op * first/last line!
51 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
52 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
53 1d08c280 2021-03-06 op * ``on-demand''.
55 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
59 5e11c00c 2021-03-02 op #include <telescope.h>
61 f832146f 2021-03-09 op #include <ctype.h>
62 5e11c00c 2021-03-02 op #include <curses.h>
63 5e11c00c 2021-03-02 op #include <event.h>
64 5e11c00c 2021-03-02 op #include <locale.h>
65 5e11c00c 2021-03-02 op #include <signal.h>
66 7953dd72 2021-03-07 op #include <stdarg.h>
67 eb259e66 2021-03-02 op #include <stdlib.h>
68 eb259e66 2021-03-02 op #include <string.h>
69 f832146f 2021-03-09 op #include <unistd.h>
71 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
73 1d08c280 2021-03-06 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 1d08c280 2021-03-06 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
78 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
80 f832146f 2021-03-09 op static int kbd(const char*);
81 f832146f 2021-03-09 op static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
82 f832146f 2021-03-09 op static void load_default_keys(void);
83 1d08c280 2021-03-06 op static int push_line(struct tab*, const struct line*, const char*, size_t);
84 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
85 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
86 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
87 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
88 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
89 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
90 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
91 46a9311e 2021-03-08 op static void cmd_scroll_down(struct tab*);
92 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
93 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
94 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
95 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
96 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
97 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
98 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
99 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
100 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
101 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
102 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
103 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
104 48e9d457 2021-03-06 op static void print_line(struct line*);
105 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
106 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
107 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
108 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
109 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
110 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
111 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
112 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
113 bcb0b073 2021-03-07 op static void new_tab(void);
115 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
116 48e9d457 2021-03-06 op static int body_lines, body_cols;
118 48e9d457 2021-03-06 op static struct event clminibufev;
119 48e9d457 2021-03-06 op static int clminibufev_set;
120 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
121 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
123 bcb0b073 2021-03-07 op static uint32_t tab_counter;
125 1d08c280 2021-03-06 op struct ui_state {
128 48e9d457 2021-03-06 op size_t line_off;
129 48e9d457 2021-03-06 op size_t line_max;
131 8af5e5ed 2021-03-08 op short loading_anim;
132 8af5e5ed 2021-03-08 op short loading_anim_step;
133 8af5e5ed 2021-03-08 op struct event loadingev;
135 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
138 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
140 f832146f 2021-03-09 op static TAILQ_HEAD(kmap, keymap) global_map, *current_map;
141 f832146f 2021-03-09 op struct keymap {
144 f832146f 2021-03-09 op struct kmap map;
145 f832146f 2021-03-09 op void (*fn)(struct tab*);
147 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
151 f832146f 2021-03-09 op kbd(const char *key)
153 f832146f 2021-03-09 op struct table {
156 f832146f 2021-03-09 op } table[] = {
157 f832146f 2021-03-09 op { "<up>", KEY_UP },
158 f832146f 2021-03-09 op { "<down>", KEY_DOWN },
159 f832146f 2021-03-09 op { "<left>", KEY_LEFT },
160 f832146f 2021-03-09 op { "<right>", KEY_RIGHT },
162 f832146f 2021-03-09 op { "space", ' ' },
163 f832146f 2021-03-09 op { "spc", ' ' },
164 f832146f 2021-03-09 op { "enter", CTRL('m') },
165 f832146f 2021-03-09 op { "tab", CTRL('i') },
170 f832146f 2021-03-09 op for (t = table; t->p != NULL; ++t) {
171 f832146f 2021-03-09 op if (has_prefix(key, t->p))
179 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
181 f832146f 2021-03-09 op int ctrl, meta, k;
182 f832146f 2021-03-09 op struct keymap *entry;
185 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
187 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
189 f832146f 2021-03-09 op if (*key == '\0')
191 f832146f 2021-03-09 op k = kbd(key);
196 f832146f 2021-03-09 op /* skip key & spaces */
197 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
199 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
202 f832146f 2021-03-09 op TAILQ_FOREACH(entry, map, keymaps) {
203 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
204 f832146f 2021-03-09 op if (*key == '\0') {
205 f832146f 2021-03-09 op entry->fn = fn;
208 f832146f 2021-03-09 op map = &entry->map;
213 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
216 f832146f 2021-03-09 op entry->meta = meta;
217 f832146f 2021-03-09 op entry->key = k;
218 f832146f 2021-03-09 op TAILQ_INIT(&entry->map);
220 f832146f 2021-03-09 op if (TAILQ_EMPTY(map))
221 f832146f 2021-03-09 op TAILQ_INSERT_HEAD(map, entry, keymaps);
223 f832146f 2021-03-09 op TAILQ_INSERT_TAIL(map, entry, keymaps);
225 f832146f 2021-03-09 op if (*key != '\0') {
226 f832146f 2021-03-09 op map = &entry->map;
230 f832146f 2021-03-09 op entry->fn = fn;
233 f832146f 2021-03-09 op static inline void
234 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
236 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
240 f832146f 2021-03-09 op load_default_keys(void)
243 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
244 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
245 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
246 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
249 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
250 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
252 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
254 f832146f 2021-03-09 op /* vi/vi-like */
255 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
256 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
257 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
258 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
260 f832146f 2021-03-09 op global_set_key("K", cmd_scroll_up);
261 f832146f 2021-03-09 op global_set_key("J", cmd_scroll_down);
264 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
267 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
268 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
269 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
270 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
272 f832146f 2021-03-09 op /* "ncurses standard" */
273 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
276 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
280 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
282 1d08c280 2021-03-06 op struct line *vl;
284 48e9d457 2021-03-06 op tab->s->line_max++;
286 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
289 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
294 1d08c280 2021-03-06 op vl->type = l->type;
295 1d08c280 2021-03-06 op if (len != 0)
296 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
297 1d08c280 2021-03-06 op vl->alt = l->alt;
299 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
300 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
302 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
307 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
309 1d08c280 2021-03-06 op struct line *l, *t;
311 48e9d457 2021-03-06 op tab->s->line_max = 0;
313 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
314 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
315 1d08c280 2021-03-06 op free(l->line);
316 1d08c280 2021-03-06 op /* l->alt references the original line! */
322 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
324 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
328 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
330 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
331 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
332 46a9311e 2021-03-08 op cmd_scroll_up(tab);
335 48e9d457 2021-03-06 op restore_cursor(tab);
339 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
341 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
342 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
343 46a9311e 2021-03-08 op cmd_scroll_down(tab);
346 48e9d457 2021-03-06 op restore_cursor(tab);
350 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
352 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
353 48e9d457 2021-03-06 op restore_cursor(tab);
357 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
359 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
360 48e9d457 2021-03-06 op restore_cursor(tab);
364 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
366 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
370 46a9311e 2021-03-08 op cmd_scroll_up(struct tab *tab)
372 48e9d457 2021-03-06 op struct line *l;
374 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
377 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
378 48e9d457 2021-03-06 op wscrl(body, -1);
379 48e9d457 2021-03-06 op wmove(body, 0, 0);
380 48e9d457 2021-03-06 op print_line(l);
384 46a9311e 2021-03-08 op cmd_scroll_down(struct tab *tab)
386 48e9d457 2021-03-06 op struct line *l;
389 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
392 48e9d457 2021-03-06 op tab->s->line_off++;
393 48e9d457 2021-03-06 op wscrl(body, 1);
395 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
398 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
399 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
400 48e9d457 2021-03-06 op print_line(l);
404 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
406 1d08c280 2021-03-06 op event_loopbreak();
410 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
412 2a4ad912 2021-03-08 op struct line *l;
415 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
416 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
418 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
419 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
422 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
425 48e9d457 2021-03-06 op static struct line *
426 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
428 48e9d457 2021-03-06 op struct line *l;
432 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
438 48e9d457 2021-03-06 op /* unreachable */
442 5e11c00c 2021-03-02 op static struct tab *
443 5e11c00c 2021-03-02 op current_tab(void)
445 5e11c00c 2021-03-02 op struct tab *t;
447 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
448 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
452 5e11c00c 2021-03-02 op /* unreachable */
457 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
459 f832146f 2021-03-09 op struct keymap *k;
460 f832146f 2021-03-09 op int meta, key;
462 f832146f 2021-03-09 op key = wgetch(body);
463 f832146f 2021-03-09 op if (key == ERR)
465 f832146f 2021-03-09 op if (key == 27) {
466 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
469 f832146f 2021-03-09 op key = wgetch(body);
470 f832146f 2021-03-09 op if (key == ERR)
475 f832146f 2021-03-09 op TAILQ_FOREACH(k, current_map, keymaps) {
476 f832146f 2021-03-09 op if (k->meta == meta && k->key == key) {
477 f832146f 2021-03-09 op if (k->fn == NULL)
478 f832146f 2021-03-09 op current_map = &k->map;
480 f832146f 2021-03-09 op current_map = &global_map;
481 f832146f 2021-03-09 op k->fn(current_tab());
487 f832146f 2021-03-09 op current_map = &global_map;
489 f832146f 2021-03-09 op message("%s%c is undefined",
490 f832146f 2021-03-09 op meta ? "M-" : "", key);
493 48e9d457 2021-03-06 op restore_cursor(current_tab());
494 48e9d457 2021-03-06 op wrefresh(tabline);
495 48e9d457 2021-03-06 op wrefresh(modeline);
496 48e9d457 2021-03-06 op wrefresh(minibuf);
498 a6d450c1 2021-03-06 op wrefresh(body);
502 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
504 a6d450c1 2021-03-06 op clminibufev_set = 0;
505 a6d450c1 2021-03-06 op werase(minibuf);
506 a6d450c1 2021-03-06 op wrefresh(minibuf);
507 48e9d457 2021-03-06 op wrefresh(body);
511 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
513 1d08c280 2021-03-06 op struct tab *tab;
519 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
521 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
522 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
524 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
525 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
527 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
528 48e9d457 2021-03-06 op body_lines = LINES-3;
529 bd9637e9 2021-03-06 op body_cols = COLS;
531 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
533 1d08c280 2021-03-06 op tab = current_tab();
535 1d08c280 2021-03-06 op wrap_page(tab);
536 1d08c280 2021-03-06 op redraw_tab(tab);
540 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
541 eb259e66 2021-03-02 op * and the end of the separator after the word.
544 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
546 eb259e66 2021-03-02 op *endword = s;
547 eb259e66 2021-03-02 op *endword = s;
549 eb259e66 2021-03-02 op if (*s == '\0')
552 eb259e66 2021-03-02 op /* find the end of the current world */
553 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
554 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
558 eb259e66 2021-03-02 op *endword = s;
560 eb259e66 2021-03-02 op /* find the end of the separator */
561 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
562 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
571 1d08c280 2021-03-06 op static inline int
572 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
573 1d08c280 2021-03-06 op const char **line)
575 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
577 1d08c280 2021-03-06 op *line += *off - zero;
582 eb259e66 2021-03-02 op static inline void
583 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
587 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
588 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
589 eb259e66 2021-03-02 op addch((*s)[i]);
595 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
596 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
598 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
601 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
603 eb259e66 2021-03-02 op size_t zero, off, len, split;
604 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
606 1d08c280 2021-03-06 op zero = strlen(prfx);
608 1d08c280 2021-03-06 op line = l->line;
609 1d08c280 2021-03-06 op linestart = l->line;
611 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
612 eb259e66 2021-03-02 op len = endword - line;
613 48e9d457 2021-03-06 op if (off + len >= body_cols) {
614 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
615 48e9d457 2021-03-06 op while (len >= body_cols) {
616 eb259e66 2021-03-02 op /* hard wrap */
617 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
618 48e9d457 2021-03-06 op len -= body_cols-1;
619 48e9d457 2021-03-06 op line += body_cols-1;
622 eb259e66 2021-03-02 op if (len != 0)
627 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
628 eb259e66 2021-03-02 op len = endspc - endword;
629 eb259e66 2021-03-02 op /* line = endspc; */
630 eb259e66 2021-03-02 op if (off != zero) {
631 48e9d457 2021-03-06 op if (off + len >= body_cols) {
632 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
633 1d08c280 2021-03-06 op linestart = endspc;
638 eb259e66 2021-03-02 op line = endspc;
641 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
645 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
647 1d08c280 2021-03-06 op size_t off, len;
648 1d08c280 2021-03-06 op const char *linestart;
650 1d08c280 2021-03-06 op len = strlen(l->line);
652 1d08c280 2021-03-06 op linestart = l->line;
654 1d08c280 2021-03-06 op while (len >= COLS) {
655 1d08c280 2021-03-06 op len -= COLS-1;
656 1d08c280 2021-03-06 op off = COLS-1;
657 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
661 ede1f142 2021-03-07 op if (len != 0)
662 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
668 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
670 1d08c280 2021-03-06 op struct line *l;
672 1d08c280 2021-03-06 op empty_vlist(tab);
674 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
675 5e11c00c 2021-03-02 op switch (l->type) {
676 5e11c00c 2021-03-02 op case LINE_TEXT:
677 1d08c280 2021-03-06 op wrap_text(tab, "", l);
679 5e11c00c 2021-03-02 op case LINE_LINK:
680 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
682 5e11c00c 2021-03-02 op case LINE_TITLE_1:
683 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
685 5e11c00c 2021-03-02 op case LINE_TITLE_2:
686 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
688 5e11c00c 2021-03-02 op case LINE_TITLE_3:
689 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
691 5e11c00c 2021-03-02 op case LINE_ITEM:
692 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
694 5e11c00c 2021-03-02 op case LINE_QUOTE:
695 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
697 5e11c00c 2021-03-02 op case LINE_PRE_START:
698 5e11c00c 2021-03-02 op case LINE_PRE_END:
699 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
701 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
702 1d08c280 2021-03-06 op hardwrap_text(tab, l);
709 1d08c280 2021-03-06 op static inline void
710 1d08c280 2021-03-06 op print_line(struct line *l)
712 bd9637e9 2021-03-06 op const char *text = l->line;
714 bd9637e9 2021-03-06 op if (text == NULL)
717 1d08c280 2021-03-06 op switch (l->type) {
718 1d08c280 2021-03-06 op case LINE_TEXT:
719 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
721 1d08c280 2021-03-06 op case LINE_LINK:
722 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
723 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
724 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
726 1d08c280 2021-03-06 op case LINE_TITLE_1:
727 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
728 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
729 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
731 1d08c280 2021-03-06 op case LINE_TITLE_2:
732 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
733 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
734 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
736 bd9637e9 2021-03-06 op case LINE_TITLE_3:
737 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
738 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
739 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
741 bd9637e9 2021-03-06 op case LINE_ITEM:
742 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
744 1d08c280 2021-03-06 op case LINE_QUOTE:
745 bd9637e9 2021-03-06 op wattron(body, A_DIM);
746 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
747 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
749 1d08c280 2021-03-06 op case LINE_PRE_START:
750 1d08c280 2021-03-06 op case LINE_PRE_END:
751 48e9d457 2021-03-06 op wprintw(body, "```");
753 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
754 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
760 8af5e5ed 2021-03-08 op redraw_tabline(void)
762 8af5e5ed 2021-03-08 op wclear(tabline);
763 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
764 8af5e5ed 2021-03-08 op mvwprintw(tabline, 0, 0, "TODO: tabs here");
768 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
770 48e9d457 2021-03-06 op int x, y, max_x, max_y;
771 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
772 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
774 48e9d457 2021-03-06 op wclear(modeline);
775 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
776 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
778 8af5e5ed 2021-03-08 op wprintw(modeline, "-%c %s %s ",
779 4d3785b1 2021-03-09 op spin[tab->s->loading_anim_step], mode, tab->urlstr);
780 48e9d457 2021-03-06 op getyx(modeline, y, x);
781 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
786 48e9d457 2021-03-06 op for (; x < max_x; ++x)
787 48e9d457 2021-03-06 op waddstr(modeline, "-");
791 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
793 48e9d457 2021-03-06 op struct line *l;
796 48e9d457 2021-03-06 op werase(body);
798 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
799 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
803 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
804 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
805 48e9d457 2021-03-06 op wmove(body, line, 0);
806 1d08c280 2021-03-06 op print_line(l);
808 48e9d457 2021-03-06 op if (line == body_lines)
812 8af5e5ed 2021-03-08 op redraw_tabline();
813 48e9d457 2021-03-06 op redraw_modeline(tab);
815 48e9d457 2021-03-06 op restore_cursor(tab);
816 48e9d457 2021-03-06 op wrefresh(tabline);
817 48e9d457 2021-03-06 op wrefresh(modeline);
818 48e9d457 2021-03-06 op wrefresh(minibuf);
820 7953dd72 2021-03-07 op wrefresh(body);
824 7953dd72 2021-03-07 op message(const char *fmt, ...)
828 7953dd72 2021-03-07 op va_start(ap, fmt);
830 7953dd72 2021-03-07 op if (clminibufev_set)
831 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
832 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
833 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
834 7953dd72 2021-03-07 op clminibufev_set = 1;
836 7953dd72 2021-03-07 op werase(minibuf);
837 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
839 7953dd72 2021-03-07 op wrefresh(minibuf);
840 48e9d457 2021-03-06 op wrefresh(body);
846 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
848 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
850 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
851 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
852 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
856 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
858 8af5e5ed 2021-03-08 op struct tab *tab = d;
860 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
862 8af5e5ed 2021-03-08 op redraw_modeline(tab);
863 8af5e5ed 2021-03-08 op wrefresh(modeline);
864 8af5e5ed 2021-03-08 op wrefresh(body);
866 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
870 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
872 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
874 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
875 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
876 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
878 43a1b8d0 2021-03-09 op redraw_modeline(tab);
879 43a1b8d0 2021-03-09 op wrefresh(modeline);
880 43a1b8d0 2021-03-09 op wrefresh(body);
884 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
886 43a1b8d0 2021-03-09 op empty_vlist(tab);
887 8af5e5ed 2021-03-08 op message("Loading %s...", url);
888 8af5e5ed 2021-03-08 op start_loading_anim(tab);
889 8af5e5ed 2021-03-08 op load_url(tab, url);
891 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
892 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
893 43a1b8d0 2021-03-09 op redraw_tab(tab);
897 bcb0b073 2021-03-07 op new_tab(void)
899 bcb0b073 2021-03-07 op struct tab *tab, *t;
900 bcb0b073 2021-03-07 op const char *url = "about:new";
902 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
905 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
908 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
909 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
910 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
913 bcb0b073 2021-03-07 op tab->id = tab_counter++;
914 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
916 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
917 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
919 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
921 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
925 bcb0b073 2021-03-07 op event_loopbreak();
929 5e11c00c 2021-03-02 op ui_init(void)
931 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
933 f832146f 2021-03-09 op TAILQ_INIT(&global_map);
934 f832146f 2021-03-09 op current_map = &global_map;
935 f832146f 2021-03-09 op load_default_keys();
942 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
944 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
946 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
948 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
950 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
953 48e9d457 2021-03-06 op body_lines = LINES-3;
954 48e9d457 2021-03-06 op body_cols = COLS;
956 43a1b8d0 2021-03-09 op keypad(body, TRUE);
957 48e9d457 2021-03-06 op scrollok(body, TRUE);
959 5e11c00c 2021-03-02 op /* non-blocking input */
960 48e9d457 2021-03-06 op wtimeout(body, 0);
962 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
964 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
965 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
967 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
968 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
976 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
978 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
979 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
983 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
985 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
988 1d08c280 2021-03-06 op wrap_page(tab);
989 5e11c00c 2021-03-02 op redraw_tab(tab);