Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
7 5e11c00c 2021-03-02 op *
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 1d08c280 2021-03-06 op /*
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
19 1d08c280 2021-03-06 op *
20 1d08c280 2021-03-06 op *
21 1d08c280 2021-03-06 op * Text wrapping
22 1d08c280 2021-03-06 op * =============
23 1d08c280 2021-03-06 op *
24 1d08c280 2021-03-06 op * There's a simple text wrapping algorithm.
25 1d08c280 2021-03-06 op *
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.
28 1d08c280 2021-03-06 op * b. repeat
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
38 1d08c280 2021-03-06 op * forget them
39 1d08c280 2021-03-06 op * 4. repeat
40 1d08c280 2021-03-06 op *
41 1d08c280 2021-03-06 op *
42 1d08c280 2021-03-06 op * Text scrolling
43 1d08c280 2021-03-06 op * ==============
44 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!
50 1d08c280 2021-03-06 op *
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''.
54 1d08c280 2021-03-06 op *
55 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
56 1d08c280 2021-03-06 op *
57 1d08c280 2021-03-06 op */
58 1d08c280 2021-03-06 op
59 5e11c00c 2021-03-02 op #include <telescope.h>
60 5e11c00c 2021-03-02 op
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>
70 5e11c00c 2021-03-02 op
71 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
72 5e11c00c 2021-03-02 op
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))
75 1d08c280 2021-03-06 op
76 f832146f 2021-03-09 op struct kmap;
77 19f1448e 2021-03-08 op
78 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
79 5e11c00c 2021-03-02 op
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);
114 5e11c00c 2021-03-02 op
115 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
116 48e9d457 2021-03-06 op static int body_lines, body_cols;
117 48e9d457 2021-03-06 op
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 };
122 48e9d457 2021-03-06 op
123 bcb0b073 2021-03-07 op static uint32_t tab_counter;
124 bcb0b073 2021-03-07 op
125 1d08c280 2021-03-06 op struct ui_state {
126 1d08c280 2021-03-06 op int curs_x;
127 1d08c280 2021-03-06 op int curs_y;
128 48e9d457 2021-03-06 op size_t line_off;
129 48e9d457 2021-03-06 op size_t line_max;
130 1d08c280 2021-03-06 op
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;
134 8af5e5ed 2021-03-08 op
135 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
136 1d08c280 2021-03-06 op };
137 1d08c280 2021-03-06 op
138 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
139 f832146f 2021-03-09 op
140 f832146f 2021-03-09 op static TAILQ_HEAD(kmap, keymap) global_map, *current_map;
141 f832146f 2021-03-09 op struct keymap {
142 f832146f 2021-03-09 op int meta;
143 f832146f 2021-03-09 op int key;
144 f832146f 2021-03-09 op struct kmap map;
145 f832146f 2021-03-09 op void (*fn)(struct tab*);
146 f832146f 2021-03-09 op
147 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
148 19f1448e 2021-03-08 op };
149 19f1448e 2021-03-08 op
150 f832146f 2021-03-09 op static int
151 f832146f 2021-03-09 op kbd(const char *key)
152 f832146f 2021-03-09 op {
153 f832146f 2021-03-09 op struct table {
154 f832146f 2021-03-09 op char *p;
155 f832146f 2021-03-09 op int k;
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 },
161 f832146f 2021-03-09 op /* ... */
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') },
166 f832146f 2021-03-09 op /* ... */
167 f832146f 2021-03-09 op { NULL, 0 },
168 f832146f 2021-03-09 op }, *t;
169 19f1448e 2021-03-08 op
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))
172 f832146f 2021-03-09 op return t->k;
173 f832146f 2021-03-09 op }
174 43a1b8d0 2021-03-09 op
175 f832146f 2021-03-09 op return *key;
176 f832146f 2021-03-09 op }
177 48e9d457 2021-03-06 op
178 f832146f 2021-03-09 op static void
179 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
180 f832146f 2021-03-09 op {
181 f832146f 2021-03-09 op int ctrl, meta, k;
182 f832146f 2021-03-09 op struct keymap *entry;
183 48e9d457 2021-03-06 op
184 f832146f 2021-03-09 op again:
185 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
186 f832146f 2021-03-09 op key += 2;
187 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
188 f832146f 2021-03-09 op key += 2;
189 f832146f 2021-03-09 op if (*key == '\0')
190 f832146f 2021-03-09 op _exit(1);
191 f832146f 2021-03-09 op k = kbd(key);
192 48e9d457 2021-03-06 op
193 f832146f 2021-03-09 op if (ctrl)
194 f832146f 2021-03-09 op k = CTRL(k);
195 19f1448e 2021-03-08 op
196 f832146f 2021-03-09 op /* skip key & spaces */
197 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
198 f832146f 2021-03-09 op ++key;
199 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
200 f832146f 2021-03-09 op ++key;
201 48e9d457 2021-03-06 op
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;
206 f832146f 2021-03-09 op return;
207 f832146f 2021-03-09 op }
208 f832146f 2021-03-09 op map = &entry->map;
209 f832146f 2021-03-09 op goto again;
210 f832146f 2021-03-09 op }
211 f832146f 2021-03-09 op }
212 f832146f 2021-03-09 op
213 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
214 f832146f 2021-03-09 op abort();
215 f832146f 2021-03-09 op
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);
219 f832146f 2021-03-09 op
220 f832146f 2021-03-09 op if (TAILQ_EMPTY(map))
221 f832146f 2021-03-09 op TAILQ_INSERT_HEAD(map, entry, keymaps);
222 f832146f 2021-03-09 op else
223 f832146f 2021-03-09 op TAILQ_INSERT_TAIL(map, entry, keymaps);
224 f832146f 2021-03-09 op
225 f832146f 2021-03-09 op if (*key != '\0') {
226 f832146f 2021-03-09 op map = &entry->map;
227 f832146f 2021-03-09 op goto again;
228 f832146f 2021-03-09 op }
229 f832146f 2021-03-09 op
230 f832146f 2021-03-09 op entry->fn = fn;
231 f832146f 2021-03-09 op }
232 f832146f 2021-03-09 op
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*))
235 f832146f 2021-03-09 op {
236 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
237 f832146f 2021-03-09 op }
238 f832146f 2021-03-09 op
239 f832146f 2021-03-09 op static void
240 f832146f 2021-03-09 op load_default_keys(void)
241 f832146f 2021-03-09 op {
242 f832146f 2021-03-09 op /* emacs */
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);
247 f832146f 2021-03-09 op
248 f832146f 2021-03-09 op /* tmp */
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);
251 f832146f 2021-03-09 op
252 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
253 f832146f 2021-03-09 op
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);
259 f832146f 2021-03-09 op
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);
262 f832146f 2021-03-09 op
263 f832146f 2021-03-09 op /* tmp */
264 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
265 f832146f 2021-03-09 op
266 f832146f 2021-03-09 op /* cua */
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);
271 f832146f 2021-03-09 op
272 f832146f 2021-03-09 op /* "ncurses standard" */
273 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
274 f832146f 2021-03-09 op
275 f832146f 2021-03-09 op /* global */
276 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
277 f832146f 2021-03-09 op }
278 f832146f 2021-03-09 op
279 1d08c280 2021-03-06 op static int
280 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
281 1d08c280 2021-03-06 op {
282 1d08c280 2021-03-06 op struct line *vl;
283 1d08c280 2021-03-06 op
284 48e9d457 2021-03-06 op tab->s->line_max++;
285 48e9d457 2021-03-06 op
286 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
287 1d08c280 2021-03-06 op return 0;
288 1d08c280 2021-03-06 op
289 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
290 1d08c280 2021-03-06 op free(vl);
291 1d08c280 2021-03-06 op return 0;
292 1d08c280 2021-03-06 op }
293 1d08c280 2021-03-06 op
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;
298 1d08c280 2021-03-06 op
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);
301 1d08c280 2021-03-06 op else
302 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
303 1d08c280 2021-03-06 op return 1;
304 1d08c280 2021-03-06 op }
305 1d08c280 2021-03-06 op
306 1d08c280 2021-03-06 op static void
307 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
308 1d08c280 2021-03-06 op {
309 1d08c280 2021-03-06 op struct line *l, *t;
310 1d08c280 2021-03-06 op
311 48e9d457 2021-03-06 op tab->s->line_max = 0;
312 48e9d457 2021-03-06 op
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! */
317 1d08c280 2021-03-06 op free(l);
318 1d08c280 2021-03-06 op }
319 1d08c280 2021-03-06 op }
320 1d08c280 2021-03-06 op
321 48e9d457 2021-03-06 op static void
322 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
323 48e9d457 2021-03-06 op {
324 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
325 48e9d457 2021-03-06 op }
326 1d08c280 2021-03-06 op
327 1d08c280 2021-03-06 op static void
328 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
329 1d08c280 2021-03-06 op {
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);
333 4dd664ce 2021-03-06 op }
334 4dd664ce 2021-03-06 op
335 48e9d457 2021-03-06 op restore_cursor(tab);
336 1d08c280 2021-03-06 op }
337 1d08c280 2021-03-06 op
338 1d08c280 2021-03-06 op static void
339 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
340 1d08c280 2021-03-06 op {
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);
344 4dd664ce 2021-03-06 op }
345 4dd664ce 2021-03-06 op
346 48e9d457 2021-03-06 op restore_cursor(tab);
347 1d08c280 2021-03-06 op }
348 1d08c280 2021-03-06 op
349 1d08c280 2021-03-06 op static void
350 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
351 1d08c280 2021-03-06 op {
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);
354 1d08c280 2021-03-06 op }
355 1d08c280 2021-03-06 op
356 1d08c280 2021-03-06 op static void
357 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
358 1d08c280 2021-03-06 op {
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);
361 1d08c280 2021-03-06 op }
362 1d08c280 2021-03-06 op
363 1d08c280 2021-03-06 op static void
364 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
365 1d08c280 2021-03-06 op {
366 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
367 1d08c280 2021-03-06 op }
368 1d08c280 2021-03-06 op
369 1d08c280 2021-03-06 op static void
370 46a9311e 2021-03-08 op cmd_scroll_up(struct tab *tab)
371 1d08c280 2021-03-06 op {
372 48e9d457 2021-03-06 op struct line *l;
373 48e9d457 2021-03-06 op
374 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
375 48e9d457 2021-03-06 op return;
376 48e9d457 2021-03-06 op
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);
381 1d08c280 2021-03-06 op }
382 1d08c280 2021-03-06 op
383 1d08c280 2021-03-06 op static void
384 46a9311e 2021-03-08 op cmd_scroll_down(struct tab *tab)
385 1d08c280 2021-03-06 op {
386 48e9d457 2021-03-06 op struct line *l;
387 48e9d457 2021-03-06 op size_t n;
388 48e9d457 2021-03-06 op
389 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
390 48e9d457 2021-03-06 op return;
391 48e9d457 2021-03-06 op
392 48e9d457 2021-03-06 op tab->s->line_off++;
393 48e9d457 2021-03-06 op wscrl(body, 1);
394 48e9d457 2021-03-06 op
395 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
396 48e9d457 2021-03-06 op return;
397 48e9d457 2021-03-06 op
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);
401 1d08c280 2021-03-06 op }
402 1d08c280 2021-03-06 op
403 1d08c280 2021-03-06 op static void
404 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
405 1d08c280 2021-03-06 op {
406 1d08c280 2021-03-06 op event_loopbreak();
407 1d08c280 2021-03-06 op }
408 1d08c280 2021-03-06 op
409 1d08c280 2021-03-06 op static void
410 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
411 2a4ad912 2021-03-08 op {
412 2a4ad912 2021-03-08 op struct line *l;
413 2a4ad912 2021-03-08 op size_t nth;
414 2a4ad912 2021-03-08 op
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)
417 2a4ad912 2021-03-08 op return;
418 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
419 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
420 43a1b8d0 2021-03-09 op return;
421 43a1b8d0 2021-03-09 op
422 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
423 2a4ad912 2021-03-08 op }
424 2a4ad912 2021-03-08 op
425 48e9d457 2021-03-06 op static struct line *
426 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
427 48e9d457 2021-03-06 op {
428 48e9d457 2021-03-06 op struct line *l;
429 48e9d457 2021-03-06 op size_t i;
430 48e9d457 2021-03-06 op
431 48e9d457 2021-03-06 op i = 0;
432 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
433 48e9d457 2021-03-06 op if (i == n)
434 48e9d457 2021-03-06 op return l;
435 48e9d457 2021-03-06 op i++;
436 48e9d457 2021-03-06 op }
437 48e9d457 2021-03-06 op
438 48e9d457 2021-03-06 op /* unreachable */
439 48e9d457 2021-03-06 op abort();
440 48e9d457 2021-03-06 op }
441 48e9d457 2021-03-06 op
442 5e11c00c 2021-03-02 op static struct tab *
443 5e11c00c 2021-03-02 op current_tab(void)
444 5e11c00c 2021-03-02 op {
445 5e11c00c 2021-03-02 op struct tab *t;
446 5e11c00c 2021-03-02 op
447 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
448 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
449 5e11c00c 2021-03-02 op return t;
450 5e11c00c 2021-03-02 op }
451 5e11c00c 2021-03-02 op
452 5e11c00c 2021-03-02 op /* unreachable */
453 5e11c00c 2021-03-02 op abort();
454 5e11c00c 2021-03-02 op }
455 5e11c00c 2021-03-02 op
456 5e11c00c 2021-03-02 op static void
457 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
458 5e11c00c 2021-03-02 op {
459 f832146f 2021-03-09 op struct keymap *k;
460 f832146f 2021-03-09 op int meta, key;
461 19f1448e 2021-03-08 op
462 f832146f 2021-03-09 op key = wgetch(body);
463 f832146f 2021-03-09 op if (key == ERR)
464 5e11c00c 2021-03-02 op return;
465 f832146f 2021-03-09 op if (key == 27) {
466 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
467 5e11c00c 2021-03-02 op
468 f832146f 2021-03-09 op meta = 1;
469 f832146f 2021-03-09 op key = wgetch(body);
470 f832146f 2021-03-09 op if (key == ERR)
471 f832146f 2021-03-09 op key = 27;
472 b0ea6f26 2021-03-08 op } else
473 f832146f 2021-03-09 op meta = 0;
474 19f1448e 2021-03-08 op
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;
479 f832146f 2021-03-09 op else {
480 f832146f 2021-03-09 op current_map = &global_map;
481 f832146f 2021-03-09 op k->fn(current_tab());
482 f832146f 2021-03-09 op }
483 1d08c280 2021-03-06 op goto done;
484 1d08c280 2021-03-06 op }
485 eb259e66 2021-03-02 op }
486 eb259e66 2021-03-02 op
487 f832146f 2021-03-09 op current_map = &global_map;
488 1d08c280 2021-03-06 op
489 f832146f 2021-03-09 op message("%s%c is undefined",
490 f832146f 2021-03-09 op meta ? "M-" : "", key);
491 f832146f 2021-03-09 op
492 1d08c280 2021-03-06 op done:
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);
497 a6d450c1 2021-03-06 op
498 a6d450c1 2021-03-06 op wrefresh(body);
499 a6d450c1 2021-03-06 op }
500 48e9d457 2021-03-06 op
501 a6d450c1 2021-03-06 op static void
502 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
503 a6d450c1 2021-03-06 op {
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);
508 5e11c00c 2021-03-02 op }
509 5e11c00c 2021-03-02 op
510 5e11c00c 2021-03-02 op static void
511 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
512 5e11c00c 2021-03-02 op {
513 1d08c280 2021-03-06 op struct tab *tab;
514 1d08c280 2021-03-06 op
515 5e11c00c 2021-03-02 op endwin();
516 5e11c00c 2021-03-02 op refresh();
517 5e11c00c 2021-03-02 op clear();
518 5e11c00c 2021-03-02 op
519 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
520 48e9d457 2021-03-06 op
521 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
522 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
523 48e9d457 2021-03-06 op
524 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
525 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
526 48e9d457 2021-03-06 op
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;
530 48e9d457 2021-03-06 op
531 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
532 48e9d457 2021-03-06 op
533 1d08c280 2021-03-06 op tab = current_tab();
534 1d08c280 2021-03-06 op
535 1d08c280 2021-03-06 op wrap_page(tab);
536 1d08c280 2021-03-06 op redraw_tab(tab);
537 5e11c00c 2021-03-02 op }
538 5e11c00c 2021-03-02 op
539 eb259e66 2021-03-02 op /*
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.
542 eb259e66 2021-03-02 op */
543 eb259e66 2021-03-02 op static int
544 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
545 eb259e66 2021-03-02 op {
546 eb259e66 2021-03-02 op *endword = s;
547 eb259e66 2021-03-02 op *endword = s;
548 eb259e66 2021-03-02 op
549 eb259e66 2021-03-02 op if (*s == '\0')
550 eb259e66 2021-03-02 op return 0;
551 eb259e66 2021-03-02 op
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)
555 eb259e66 2021-03-02 op break;
556 eb259e66 2021-03-02 op }
557 eb259e66 2021-03-02 op
558 eb259e66 2021-03-02 op *endword = s;
559 eb259e66 2021-03-02 op
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)
563 eb259e66 2021-03-02 op break;
564 eb259e66 2021-03-02 op }
565 eb259e66 2021-03-02 op
566 eb259e66 2021-03-02 op *endspc = s;
567 eb259e66 2021-03-02 op
568 eb259e66 2021-03-02 op return 1;
569 eb259e66 2021-03-02 op }
570 eb259e66 2021-03-02 op
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)
574 eb259e66 2021-03-02 op {
575 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
576 1d08c280 2021-03-06 op return 0;
577 1d08c280 2021-03-06 op *line += *off - zero;
578 eb259e66 2021-03-02 op *off = zero;
579 1d08c280 2021-03-06 op return 1;
580 eb259e66 2021-03-02 op }
581 eb259e66 2021-03-02 op
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)
584 eb259e66 2021-03-02 op {
585 eb259e66 2021-03-02 op size_t i;
586 eb259e66 2021-03-02 op
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]);
590 eb259e66 2021-03-02 op *off += len;
591 eb259e66 2021-03-02 op *s += len;
592 eb259e66 2021-03-02 op }
593 eb259e66 2021-03-02 op
594 eb259e66 2021-03-02 op /*
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.
597 eb259e66 2021-03-02 op *
598 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
599 eb259e66 2021-03-02 op */
600 5e11c00c 2021-03-02 op static void
601 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
602 eb259e66 2021-03-02 op {
603 eb259e66 2021-03-02 op size_t zero, off, len, split;
604 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
605 eb259e66 2021-03-02 op
606 1d08c280 2021-03-06 op zero = strlen(prfx);
607 eb259e66 2021-03-02 op off = zero;
608 1d08c280 2021-03-06 op line = l->line;
609 1d08c280 2021-03-06 op linestart = l->line;
610 eb259e66 2021-03-02 op
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;
620 eb259e66 2021-03-02 op }
621 eb259e66 2021-03-02 op
622 eb259e66 2021-03-02 op if (len != 0)
623 1d08c280 2021-03-06 op off += len;
624 1d08c280 2021-03-06 op } else
625 1d08c280 2021-03-06 op off += len;
626 eb259e66 2021-03-02 op
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;
634 1d08c280 2021-03-06 op } else
635 1d08c280 2021-03-06 op off += len;
636 eb259e66 2021-03-02 op }
637 eb259e66 2021-03-02 op
638 eb259e66 2021-03-02 op line = endspc;
639 eb259e66 2021-03-02 op }
640 eb259e66 2021-03-02 op
641 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
642 eb259e66 2021-03-02 op }
643 eb259e66 2021-03-02 op
644 1d08c280 2021-03-06 op static int
645 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
646 5e11c00c 2021-03-02 op {
647 1d08c280 2021-03-06 op size_t off, len;
648 1d08c280 2021-03-06 op const char *linestart;
649 5e11c00c 2021-03-02 op
650 1d08c280 2021-03-06 op len = strlen(l->line);
651 1d08c280 2021-03-06 op off = 0;
652 1d08c280 2021-03-06 op linestart = l->line;
653 1d08c280 2021-03-06 op
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))
658 1d08c280 2021-03-06 op return 0;
659 1d08c280 2021-03-06 op }
660 1d08c280 2021-03-06 op
661 ede1f142 2021-03-07 op if (len != 0)
662 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
663 ede1f142 2021-03-07 op
664 1d08c280 2021-03-06 op return 1;
665 1d08c280 2021-03-06 op }
666 5e11c00c 2021-03-02 op
667 1d08c280 2021-03-06 op static int
668 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
669 1d08c280 2021-03-06 op {
670 1d08c280 2021-03-06 op struct line *l;
671 1d08c280 2021-03-06 op
672 1d08c280 2021-03-06 op empty_vlist(tab);
673 1d08c280 2021-03-06 op
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);
678 5e11c00c 2021-03-02 op break;
679 5e11c00c 2021-03-02 op case LINE_LINK:
680 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
681 5e11c00c 2021-03-02 op break;
682 5e11c00c 2021-03-02 op case LINE_TITLE_1:
683 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
684 5e11c00c 2021-03-02 op break;
685 5e11c00c 2021-03-02 op case LINE_TITLE_2:
686 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
687 5e11c00c 2021-03-02 op break;
688 5e11c00c 2021-03-02 op case LINE_TITLE_3:
689 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
690 5e11c00c 2021-03-02 op break;
691 5e11c00c 2021-03-02 op case LINE_ITEM:
692 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
693 5e11c00c 2021-03-02 op break;
694 5e11c00c 2021-03-02 op case LINE_QUOTE:
695 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
696 5e11c00c 2021-03-02 op break;
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);
700 5e11c00c 2021-03-02 op break;
701 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
702 1d08c280 2021-03-06 op hardwrap_text(tab, l);
703 5e11c00c 2021-03-02 op break;
704 5e11c00c 2021-03-02 op }
705 5e11c00c 2021-03-02 op }
706 1d08c280 2021-03-06 op return 1;
707 1d08c280 2021-03-06 op }
708 5e11c00c 2021-03-02 op
709 1d08c280 2021-03-06 op static inline void
710 1d08c280 2021-03-06 op print_line(struct line *l)
711 1d08c280 2021-03-06 op {
712 bd9637e9 2021-03-06 op const char *text = l->line;
713 bd9637e9 2021-03-06 op
714 bd9637e9 2021-03-06 op if (text == NULL)
715 bd9637e9 2021-03-06 op text = "";
716 bd9637e9 2021-03-06 op
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);
720 1d08c280 2021-03-06 op break;
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);
725 bd9637e9 2021-03-06 op return;
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);
730 bd9637e9 2021-03-06 op return;
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);
735 bd9637e9 2021-03-06 op return;
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);
740 bd9637e9 2021-03-06 op return;
741 bd9637e9 2021-03-06 op case LINE_ITEM:
742 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
743 bd9637e9 2021-03-06 op return;
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);
748 bd9637e9 2021-03-06 op return;
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, "```");
752 bd9637e9 2021-03-06 op return;
753 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
754 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
755 bd9637e9 2021-03-06 op return;
756 1d08c280 2021-03-06 op }
757 1d08c280 2021-03-06 op }
758 1d08c280 2021-03-06 op
759 1d08c280 2021-03-06 op static void
760 8af5e5ed 2021-03-08 op redraw_tabline(void)
761 8af5e5ed 2021-03-08 op {
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");
765 8af5e5ed 2021-03-08 op }
766 8af5e5ed 2021-03-08 op
767 8af5e5ed 2021-03-08 op static void
768 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
769 1d08c280 2021-03-06 op {
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 = "-\\|/";
773 1d08c280 2021-03-06 op
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);
777 1d08c280 2021-03-06 op
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);
782 48e9d457 2021-03-06 op
783 48e9d457 2021-03-06 op (void)y;
784 48e9d457 2021-03-06 op (void)max_y;
785 48e9d457 2021-03-06 op
786 48e9d457 2021-03-06 op for (; x < max_x; ++x)
787 48e9d457 2021-03-06 op waddstr(modeline, "-");
788 48e9d457 2021-03-06 op }
789 48e9d457 2021-03-06 op
790 48e9d457 2021-03-06 op static void
791 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
792 48e9d457 2021-03-06 op {
793 48e9d457 2021-03-06 op struct line *l;
794 48e9d457 2021-03-06 op int line;
795 48e9d457 2021-03-06 op
796 48e9d457 2021-03-06 op werase(body);
797 48e9d457 2021-03-06 op
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))
800 48e9d457 2021-03-06 op return;
801 48e9d457 2021-03-06 op
802 48e9d457 2021-03-06 op line = 0;
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);
807 48e9d457 2021-03-06 op line++;
808 48e9d457 2021-03-06 op if (line == body_lines)
809 48e9d457 2021-03-06 op break;
810 1d08c280 2021-03-06 op }
811 1d08c280 2021-03-06 op
812 8af5e5ed 2021-03-08 op redraw_tabline();
813 48e9d457 2021-03-06 op redraw_modeline(tab);
814 48e9d457 2021-03-06 op
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);
819 7953dd72 2021-03-07 op
820 7953dd72 2021-03-07 op wrefresh(body);
821 7953dd72 2021-03-07 op }
822 7953dd72 2021-03-07 op
823 7953dd72 2021-03-07 op static void
824 7953dd72 2021-03-07 op message(const char *fmt, ...)
825 7953dd72 2021-03-07 op {
826 7953dd72 2021-03-07 op va_list ap;
827 48e9d457 2021-03-06 op
828 7953dd72 2021-03-07 op va_start(ap, fmt);
829 7953dd72 2021-03-07 op
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;
835 7953dd72 2021-03-07 op
836 7953dd72 2021-03-07 op werase(minibuf);
837 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
838 7953dd72 2021-03-07 op
839 7953dd72 2021-03-07 op wrefresh(minibuf);
840 48e9d457 2021-03-06 op wrefresh(body);
841 7953dd72 2021-03-07 op
842 7953dd72 2021-03-07 op va_end(ap);
843 bcb0b073 2021-03-07 op }
844 bcb0b073 2021-03-07 op
845 bcb0b073 2021-03-07 op static void
846 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
847 8af5e5ed 2021-03-08 op {
848 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
849 8af5e5ed 2021-03-08 op return;
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);
853 8af5e5ed 2021-03-08 op }
854 8af5e5ed 2021-03-08 op
855 8af5e5ed 2021-03-08 op static void
856 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
857 8af5e5ed 2021-03-08 op {
858 8af5e5ed 2021-03-08 op struct tab *tab = d;
859 8af5e5ed 2021-03-08 op
860 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
861 8af5e5ed 2021-03-08 op
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);
865 8af5e5ed 2021-03-08 op
866 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
867 8af5e5ed 2021-03-08 op }
868 8af5e5ed 2021-03-08 op
869 8af5e5ed 2021-03-08 op static void
870 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
871 8af5e5ed 2021-03-08 op {
872 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
873 8af5e5ed 2021-03-08 op return;
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;
877 43a1b8d0 2021-03-09 op
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);
881 8af5e5ed 2021-03-08 op }
882 8af5e5ed 2021-03-08 op
883 8af5e5ed 2021-03-08 op static void
884 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
885 8af5e5ed 2021-03-08 op {
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);
890 43a1b8d0 2021-03-09 op
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);
894 8af5e5ed 2021-03-08 op }
895 8af5e5ed 2021-03-08 op
896 8af5e5ed 2021-03-08 op static void
897 bcb0b073 2021-03-07 op new_tab(void)
898 bcb0b073 2021-03-07 op {
899 bcb0b073 2021-03-07 op struct tab *tab, *t;
900 bcb0b073 2021-03-07 op const char *url = "about:new";
901 bcb0b073 2021-03-07 op
902 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
903 bcb0b073 2021-03-07 op goto err;
904 bcb0b073 2021-03-07 op
905 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
906 bcb0b073 2021-03-07 op goto err;
907 bcb0b073 2021-03-07 op
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;
911 bcb0b073 2021-03-07 op }
912 bcb0b073 2021-03-07 op
913 bcb0b073 2021-03-07 op tab->id = tab_counter++;
914 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
915 bcb0b073 2021-03-07 op
916 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
917 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
918 bcb0b073 2021-03-07 op else
919 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
920 bcb0b073 2021-03-07 op
921 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
922 bcb0b073 2021-03-07 op return;
923 bcb0b073 2021-03-07 op
924 bcb0b073 2021-03-07 op err:
925 bcb0b073 2021-03-07 op event_loopbreak();
926 5e11c00c 2021-03-02 op }
927 5e11c00c 2021-03-02 op
928 5e11c00c 2021-03-02 op int
929 5e11c00c 2021-03-02 op ui_init(void)
930 5e11c00c 2021-03-02 op {
931 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
932 5e11c00c 2021-03-02 op
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();
936 f832146f 2021-03-09 op
937 5e11c00c 2021-03-02 op initscr();
938 15e1b108 2021-03-02 op raw();
939 5e11c00c 2021-03-02 op noecho();
940 5e11c00c 2021-03-02 op
941 5e11c00c 2021-03-02 op nonl();
942 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
943 5e11c00c 2021-03-02 op
944 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
945 48e9d457 2021-03-06 op return 0;
946 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
947 48e9d457 2021-03-06 op return 0;
948 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
949 48e9d457 2021-03-06 op return 0;
950 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
951 48e9d457 2021-03-06 op return 0;
952 1d08c280 2021-03-06 op
953 48e9d457 2021-03-06 op body_lines = LINES-3;
954 48e9d457 2021-03-06 op body_cols = COLS;
955 48e9d457 2021-03-06 op
956 43a1b8d0 2021-03-09 op keypad(body, TRUE);
957 48e9d457 2021-03-06 op scrollok(body, TRUE);
958 48e9d457 2021-03-06 op
959 5e11c00c 2021-03-02 op /* non-blocking input */
960 48e9d457 2021-03-06 op wtimeout(body, 0);
961 5e11c00c 2021-03-02 op
962 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
963 5e11c00c 2021-03-02 op
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);
966 5e11c00c 2021-03-02 op
967 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
968 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
969 5e11c00c 2021-03-02 op
970 bcb0b073 2021-03-07 op new_tab();
971 5e11c00c 2021-03-02 op
972 1d08c280 2021-03-06 op return 1;
973 5e11c00c 2021-03-02 op }
974 5e11c00c 2021-03-02 op
975 5e11c00c 2021-03-02 op void
976 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
977 8af5e5ed 2021-03-08 op {
978 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
979 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
980 8af5e5ed 2021-03-08 op }
981 8af5e5ed 2021-03-08 op
982 8af5e5ed 2021-03-08 op void
983 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
984 5e11c00c 2021-03-02 op {
985 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
986 5e11c00c 2021-03-02 op return;
987 5e11c00c 2021-03-02 op
988 1d08c280 2021-03-06 op wrap_page(tab);
989 5e11c00c 2021-03-02 op redraw_tab(tab);
990 5e11c00c 2021-03-02 op }
991 5e11c00c 2021-03-02 op
992 5e11c00c 2021-03-02 op void
993 5e11c00c 2021-03-02 op ui_end(void)
994 5e11c00c 2021-03-02 op {
995 5e11c00c 2021-03-02 op endwin();
996 5e11c00c 2021-03-02 op }