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 *);
87 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
88 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
89 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
90 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
91 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
92 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
93 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
94 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
95 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
96 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
97 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
98 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
99 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
100 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
101 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
102 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
103 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
104 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
105 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
106 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
108 9ca15951 2021-03-09 op static void global_key_unbound(void);
110 9ca15951 2021-03-09 op static void cmd_mini_del(struct tab*);
111 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
112 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
113 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
114 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
115 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
116 b360ebb3 2021-03-10 op static void cmd_mini_abort();
117 b360ebb3 2021-03-10 op static void cmd_mini_complete_and_exit();
119 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
120 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
121 b360ebb3 2021-03-10 op static void eecmd_select(void);
122 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
123 5cd2ebb1 2021-03-11 op static void ir_select(void);
124 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
125 5cd2ebb1 2021-03-11 op static void lu_select(void);
127 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
128 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
129 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
130 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
131 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
132 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
133 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
134 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
135 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
136 48e9d457 2021-03-06 op static void print_line(struct line*);
137 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
138 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
139 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
140 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
141 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
142 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
143 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
144 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
145 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
146 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
147 b360ebb3 2021-03-10 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
148 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
149 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
150 bcb0b073 2021-03-07 op static void new_tab(void);
152 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
154 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
155 48e9d457 2021-03-06 op static int body_lines, body_cols;
157 48e9d457 2021-03-06 op static struct event clminibufev;
158 48e9d457 2021-03-06 op static int clminibufev_set;
159 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
160 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
162 bcb0b073 2021-03-07 op static uint32_t tab_counter;
164 1d08c280 2021-03-06 op struct ui_state {
167 48e9d457 2021-03-06 op size_t line_off;
168 48e9d457 2021-03-06 op size_t line_max;
170 8af5e5ed 2021-03-08 op short loading_anim;
171 8af5e5ed 2021-03-08 op short loading_anim_step;
172 8af5e5ed 2021-03-08 op struct event loadingev;
174 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
177 7c7d7bb7 2021-03-10 op static char keybuf[64];
179 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
181 a777f81f 2021-03-10 op struct keytable {
184 a777f81f 2021-03-10 op } keytable[] = {
185 a777f81f 2021-03-10 op { "<up>", KEY_UP },
186 a777f81f 2021-03-10 op { "<down>", KEY_DOWN },
187 a777f81f 2021-03-10 op { "<left>", KEY_LEFT },
188 a777f81f 2021-03-10 op { "<right>", KEY_RIGHT },
189 a777f81f 2021-03-10 op { "<prior>", KEY_PPAGE },
190 a777f81f 2021-03-10 op { "<next>", KEY_NPAGE },
191 a777f81f 2021-03-10 op { "<home>", KEY_HOME },
192 a777f81f 2021-03-10 op { "<end>", KEY_END },
194 a777f81f 2021-03-10 op { "del", KEY_BACKSPACE },
195 a777f81f 2021-03-10 op { "esc", 27 },
196 a777f81f 2021-03-10 op { "space", ' ' },
197 a777f81f 2021-03-10 op { "spc", ' ' },
198 a777f81f 2021-03-10 op { "enter", CTRL('m') },
199 a777f81f 2021-03-10 op { "ret", CTRL('m' )},
200 a777f81f 2021-03-10 op { "tab", CTRL('i') },
205 9ca15951 2021-03-09 op struct kmap {
206 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
207 9ca15951 2021-03-09 op void (*unhandled_input)(void);
210 9ca15951 2021-03-09 op struct kmap global_map,
211 fa3fd864 2021-03-10 op minibuffer_map,
212 9ca15951 2021-03-09 op *current_map,
215 f832146f 2021-03-09 op struct keymap {
218 f832146f 2021-03-09 op struct kmap map;
219 f832146f 2021-03-09 op void (*fn)(struct tab*);
221 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
224 9ca15951 2021-03-09 op static int in_minibuffer;
226 9ca15951 2021-03-09 op static struct {
227 9cb0f9ce 2021-03-10 op char *curmesg;
229 040fbdf8 2021-03-10 op char buf[1025];
230 9ca15951 2021-03-09 op size_t off, len;
231 5cd2ebb1 2021-03-11 op char prompt[32];
232 b360ebb3 2021-03-10 op void (*donefn)(void);
233 b360ebb3 2021-03-10 op void (*abortfn)(void);
237 f832146f 2021-03-09 op kbd(const char *key)
239 a777f81f 2021-03-10 op struct keytable *t;
241 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
242 f832146f 2021-03-09 op if (has_prefix(key, t->p))
249 a777f81f 2021-03-10 op static const char *
252 a777f81f 2021-03-10 op struct keytable *t;
254 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
255 a777f81f 2021-03-10 op if (k == t->k)
263 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
265 f832146f 2021-03-09 op int ctrl, meta, k;
266 f832146f 2021-03-09 op struct keymap *entry;
269 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
271 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
273 f832146f 2021-03-09 op if (*key == '\0')
275 f832146f 2021-03-09 op k = kbd(key);
280 f832146f 2021-03-09 op /* skip key & spaces */
281 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
283 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
286 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
287 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
288 f832146f 2021-03-09 op if (*key == '\0') {
289 f832146f 2021-03-09 op entry->fn = fn;
292 f832146f 2021-03-09 op map = &entry->map;
297 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
300 f832146f 2021-03-09 op entry->meta = meta;
301 f832146f 2021-03-09 op entry->key = k;
302 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
304 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
305 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
307 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
309 f832146f 2021-03-09 op if (*key != '\0') {
310 f832146f 2021-03-09 op map = &entry->map;
314 f832146f 2021-03-09 op entry->fn = fn;
317 f832146f 2021-03-09 op static inline void
318 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
320 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
323 9ca15951 2021-03-09 op static inline void
324 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
326 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
330 f832146f 2021-03-09 op load_default_keys(void)
332 9ca15951 2021-03-09 op /* === global map === */
335 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
336 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
337 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
338 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
340 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
341 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
343 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
345 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
346 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
347 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
349 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
350 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
351 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
352 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
354 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
355 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
357 f832146f 2021-03-09 op /* vi/vi-like */
358 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
359 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
360 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
361 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
363 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
364 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
366 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
367 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
370 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
372 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
375 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
376 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
377 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
378 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
379 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
380 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
382 f832146f 2021-03-09 op /* "ncurses standard" */
383 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
386 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
388 fa3fd864 2021-03-10 op /* === minibuffer map === */
389 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
390 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
391 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
392 fa3fd864 2021-03-10 op minibuffer_set_key("del", cmd_mini_del);
394 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
395 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
396 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
397 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
398 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
399 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
400 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
404 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
406 1d08c280 2021-03-06 op struct line *vl;
408 48e9d457 2021-03-06 op tab->s->line_max++;
410 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
413 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
418 1d08c280 2021-03-06 op vl->type = l->type;
419 1d08c280 2021-03-06 op if (len != 0)
420 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
421 1d08c280 2021-03-06 op vl->alt = l->alt;
423 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
424 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
426 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
431 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
433 1d08c280 2021-03-06 op struct line *l, *t;
435 48e9d457 2021-03-06 op tab->s->line_max = 0;
437 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
438 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
439 1d08c280 2021-03-06 op free(l->line);
440 1d08c280 2021-03-06 op /* l->alt references the original line! */
446 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
448 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
452 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
454 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
455 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
456 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
459 48e9d457 2021-03-06 op restore_cursor(tab);
463 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
465 fed61466 2021-03-11 op if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
468 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
469 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
470 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
473 48e9d457 2021-03-06 op restore_cursor(tab);
477 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
479 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
480 48e9d457 2021-03-06 op restore_cursor(tab);
484 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
486 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
487 48e9d457 2021-03-06 op restore_cursor(tab);
491 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
493 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
497 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
499 48e9d457 2021-03-06 op struct line *l;
501 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
504 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
505 48e9d457 2021-03-06 op wscrl(body, -1);
506 48e9d457 2021-03-06 op wmove(body, 0, 0);
507 48e9d457 2021-03-06 op print_line(l);
511 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
513 48e9d457 2021-03-06 op struct line *l;
516 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
519 48e9d457 2021-03-06 op tab->s->line_off++;
520 48e9d457 2021-03-06 op wscrl(body, 1);
522 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
525 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
526 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
527 48e9d457 2021-03-06 op print_line(l);
531 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
535 ed44414d 2021-03-09 op off = body_lines+1;
537 ed44414d 2021-03-09 op for (; off > 0; --off)
538 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
542 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
546 ed44414d 2021-03-09 op off = tab->s->line_off + body_lines;
547 ed44414d 2021-03-09 op off = MIN(tab->s->line_max, off);
549 ed44414d 2021-03-09 op for (; off >= 0; --off)
550 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
554 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
556 e19f9a04 2021-03-11 op tab->s->line_off = 0;
557 e19f9a04 2021-03-11 op tab->s->curs_y = 0;
558 e19f9a04 2021-03-11 op redraw_body(tab);
562 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
566 e19f9a04 2021-03-11 op off = tab->s->line_max - body_lines;
567 e19f9a04 2021-03-11 op off = MAX(0, off);
569 e19f9a04 2021-03-11 op tab->s->line_off = off;
570 e19f9a04 2021-03-11 op tab->s->curs_y = MIN(body_lines, tab->s->line_max);
572 e19f9a04 2021-03-11 op redraw_body(tab);
576 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
578 1d08c280 2021-03-06 op event_loopbreak();
582 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
584 2a4ad912 2021-03-08 op struct line *l;
587 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
588 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
590 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
591 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
594 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
598 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
602 b360ebb3 2021-03-10 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
604 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
605 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
607 9ca15951 2021-03-09 op if (thiskey.meta)
608 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
610 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
611 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
615 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
617 7c7d7bb7 2021-03-10 op struct tab *t;
619 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
620 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
621 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
625 7c7d7bb7 2021-03-10 op stop_tab(tab);
627 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
628 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
630 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
632 7c7d7bb7 2021-03-10 op free(tab->s);
637 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
643 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
645 7c7d7bb7 2021-03-10 op struct tab *t;
647 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
649 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
650 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
651 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
655 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
657 7c7d7bb7 2021-03-10 op struct tab *t;
659 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
661 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
662 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
663 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
667 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
669 5cd2ebb1 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer);
670 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
674 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
676 5cd2ebb1 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer);
677 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
678 5cd2ebb1 2021-03-11 op strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
679 5cd2ebb1 2021-03-11 op ministate.off = strlen(tab->urlstr);
680 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
684 7c7d7bb7 2021-03-10 op global_key_unbound(void)
686 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
690 9ca15951 2021-03-09 op cmd_mini_del(struct tab *tab)
692 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
695 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
696 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
697 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
698 9ca15951 2021-03-09 op ministate.off--;
699 9ca15951 2021-03-09 op ministate.len--;
703 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
705 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
707 9ca15951 2021-03-09 op ministate.off++;
711 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
713 9ca15951 2021-03-09 op if (ministate.off == 0)
715 9ca15951 2021-03-09 op ministate.off--;
719 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
721 9ca15951 2021-03-09 op ministate.off = ministate.len;
725 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
727 9ca15951 2021-03-09 op ministate.off = 0;
731 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
733 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
735 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
736 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
740 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
742 b360ebb3 2021-03-10 op ministate.abortfn();
746 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
748 b360ebb3 2021-03-10 op ministate.donefn();
752 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
754 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
757 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
759 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
760 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
761 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
762 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
763 9ca15951 2021-03-09 op ministate.off++;
764 9ca15951 2021-03-09 op ministate.len++;
768 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
770 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
771 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
772 5cd2ebb1 2021-03-11 op global_key_unbound();
776 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
780 b360ebb3 2021-03-10 op eecmd_select(void)
782 b360ebb3 2021-03-10 op exit_minibuffer();
783 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
787 5cd2ebb1 2021-03-11 op ir_self_insert(void)
789 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
793 5cd2ebb1 2021-03-11 op ir_select(void)
795 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
796 5cd2ebb1 2021-03-11 op struct url url;
797 5cd2ebb1 2021-03-11 op struct tab *tab;
799 5cd2ebb1 2021-03-11 op tab = current_tab();
801 5cd2ebb1 2021-03-11 op exit_minibuffer();
803 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
804 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
805 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
806 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
807 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
811 5cd2ebb1 2021-03-11 op lu_self_insert(void)
813 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
814 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
815 5cd2ebb1 2021-03-11 op global_key_unbound();
819 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
823 5cd2ebb1 2021-03-11 op lu_select(void)
825 5cd2ebb1 2021-03-11 op exit_minibuffer();
826 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
829 48e9d457 2021-03-06 op static struct line *
830 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
832 48e9d457 2021-03-06 op struct line *l;
836 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
842 48e9d457 2021-03-06 op /* unreachable */
846 5e11c00c 2021-03-02 op static struct tab *
847 5e11c00c 2021-03-02 op current_tab(void)
849 5e11c00c 2021-03-02 op struct tab *t;
851 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
852 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
856 5e11c00c 2021-03-02 op /* unreachable */
861 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
863 481340cc 2021-03-11 op struct tab *tab;
864 f832146f 2021-03-09 op struct keymap *k;
865 7c7d7bb7 2021-03-10 op const char *keyname;
866 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
868 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
869 9ca15951 2021-03-09 op if (thiskey.key == ERR)
871 9ca15951 2021-03-09 op if (thiskey.key == 27) {
872 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
874 9ca15951 2021-03-09 op thiskey.meta = 1;
875 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
876 9ca15951 2021-03-09 op if (thiskey.key == ERR)
877 9ca15951 2021-03-09 op thiskey.key = 27;
879 9ca15951 2021-03-09 op thiskey.meta = 0;
881 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
882 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
883 7c7d7bb7 2021-03-10 op if (thiskey.meta)
884 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
885 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
886 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
888 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
889 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
892 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
893 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
894 9ca15951 2021-03-09 op k->key == thiskey.key) {
895 f832146f 2021-03-09 op if (k->fn == NULL)
896 f832146f 2021-03-09 op current_map = &k->map;
898 9ca15951 2021-03-09 op current_map = base_map;
899 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
900 f832146f 2021-03-09 op k->fn(current_tab());
906 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
907 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
909 7c7d7bb7 2021-03-10 op global_key_unbound();
912 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
913 9ca15951 2021-03-09 op current_map = base_map;
916 481340cc 2021-03-11 op tab = current_tab();
917 7c7d7bb7 2021-03-10 op redraw_tabline();
918 481340cc 2021-03-11 op redraw_modeline(tab);
919 9ca15951 2021-03-09 op redraw_minibuffer();
920 481340cc 2021-03-11 op restore_cursor(tab);
921 48e9d457 2021-03-06 op wrefresh(tabline);
922 48e9d457 2021-03-06 op wrefresh(modeline);
924 9ca15951 2021-03-09 op if (in_minibuffer) {
925 9ca15951 2021-03-09 op wrefresh(body);
926 9ca15951 2021-03-09 op wrefresh(minibuf);
928 9ca15951 2021-03-09 op wrefresh(minibuf);
929 9ca15951 2021-03-09 op wrefresh(body);
934 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
936 a6d450c1 2021-03-06 op clminibufev_set = 0;
938 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
939 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
941 9cb0f9ce 2021-03-10 op redraw_minibuffer();
942 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
943 9cb0f9ce 2021-03-10 op wrefresh(body);
944 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
946 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
947 9cb0f9ce 2021-03-10 op wrefresh(body);
952 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
954 1d08c280 2021-03-06 op struct tab *tab;
960 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
962 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
963 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
965 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
966 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
968 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
969 48e9d457 2021-03-06 op body_lines = LINES-3;
970 bd9637e9 2021-03-06 op body_cols = COLS;
972 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
974 1d08c280 2021-03-06 op tab = current_tab();
976 1d08c280 2021-03-06 op wrap_page(tab);
977 1d08c280 2021-03-06 op redraw_tab(tab);
981 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
982 eb259e66 2021-03-02 op * and the end of the separator after the word.
985 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
987 eb259e66 2021-03-02 op *endword = s;
988 eb259e66 2021-03-02 op *endword = s;
990 eb259e66 2021-03-02 op if (*s == '\0')
993 eb259e66 2021-03-02 op /* find the end of the current world */
994 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
995 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
999 eb259e66 2021-03-02 op *endword = s;
1001 eb259e66 2021-03-02 op /* find the end of the separator */
1002 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1003 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
1007 eb259e66 2021-03-02 op *endspc = s;
1012 1d08c280 2021-03-06 op static inline int
1013 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1014 1d08c280 2021-03-06 op const char **line)
1016 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
1018 1d08c280 2021-03-06 op *line += *off - zero;
1019 eb259e66 2021-03-02 op *off = zero;
1023 eb259e66 2021-03-02 op static inline void
1024 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
1028 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1029 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
1030 eb259e66 2021-03-02 op addch((*s)[i]);
1031 eb259e66 2021-03-02 op *off += len;
1036 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
1037 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
1039 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
1042 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
1044 eb259e66 2021-03-02 op size_t zero, off, len, split;
1045 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
1047 1d08c280 2021-03-06 op zero = strlen(prfx);
1049 1d08c280 2021-03-06 op line = l->line;
1050 1d08c280 2021-03-06 op linestart = l->line;
1052 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
1053 eb259e66 2021-03-02 op len = endword - line;
1054 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1055 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1056 48e9d457 2021-03-06 op while (len >= body_cols) {
1057 eb259e66 2021-03-02 op /* hard wrap */
1058 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1059 48e9d457 2021-03-06 op len -= body_cols-1;
1060 48e9d457 2021-03-06 op line += body_cols-1;
1063 eb259e66 2021-03-02 op if (len != 0)
1068 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
1069 eb259e66 2021-03-02 op len = endspc - endword;
1070 eb259e66 2021-03-02 op /* line = endspc; */
1071 eb259e66 2021-03-02 op if (off != zero) {
1072 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1073 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1074 1d08c280 2021-03-06 op linestart = endspc;
1079 eb259e66 2021-03-02 op line = endspc;
1082 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1086 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
1088 1d08c280 2021-03-06 op size_t off, len;
1089 1d08c280 2021-03-06 op const char *linestart;
1091 1d08c280 2021-03-06 op len = strlen(l->line);
1093 1d08c280 2021-03-06 op linestart = l->line;
1095 1d08c280 2021-03-06 op while (len >= COLS) {
1096 1d08c280 2021-03-06 op len -= COLS-1;
1097 1d08c280 2021-03-06 op off = COLS-1;
1098 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
1102 ede1f142 2021-03-07 op if (len != 0)
1103 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
1109 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1111 1d08c280 2021-03-06 op struct line *l;
1113 1d08c280 2021-03-06 op empty_vlist(tab);
1115 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1116 5e11c00c 2021-03-02 op switch (l->type) {
1117 5e11c00c 2021-03-02 op case LINE_TEXT:
1118 1d08c280 2021-03-06 op wrap_text(tab, "", l);
1120 5e11c00c 2021-03-02 op case LINE_LINK:
1121 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
1123 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1124 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
1126 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1127 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
1129 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1130 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
1132 5e11c00c 2021-03-02 op case LINE_ITEM:
1133 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
1135 5e11c00c 2021-03-02 op case LINE_QUOTE:
1136 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
1138 5e11c00c 2021-03-02 op case LINE_PRE_START:
1139 5e11c00c 2021-03-02 op case LINE_PRE_END:
1140 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
1142 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1143 1d08c280 2021-03-06 op hardwrap_text(tab, l);
1150 1d08c280 2021-03-06 op static inline void
1151 1d08c280 2021-03-06 op print_line(struct line *l)
1153 bd9637e9 2021-03-06 op const char *text = l->line;
1155 bd9637e9 2021-03-06 op if (text == NULL)
1158 1d08c280 2021-03-06 op switch (l->type) {
1159 1d08c280 2021-03-06 op case LINE_TEXT:
1160 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1162 1d08c280 2021-03-06 op case LINE_LINK:
1163 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
1164 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
1165 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
1167 1d08c280 2021-03-06 op case LINE_TITLE_1:
1168 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1169 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
1170 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1172 1d08c280 2021-03-06 op case LINE_TITLE_2:
1173 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1174 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
1175 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1177 bd9637e9 2021-03-06 op case LINE_TITLE_3:
1178 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1179 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
1180 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1182 bd9637e9 2021-03-06 op case LINE_ITEM:
1183 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
1185 1d08c280 2021-03-06 op case LINE_QUOTE:
1186 bd9637e9 2021-03-06 op wattron(body, A_DIM);
1187 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
1188 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
1190 1d08c280 2021-03-06 op case LINE_PRE_START:
1191 1d08c280 2021-03-06 op case LINE_PRE_END:
1192 48e9d457 2021-03-06 op wprintw(body, "```");
1194 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
1195 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1201 8af5e5ed 2021-03-08 op redraw_tabline(void)
1203 7c7d7bb7 2021-03-10 op struct tab *tab;
1204 7c7d7bb7 2021-03-10 op int current;
1206 8af5e5ed 2021-03-08 op wclear(tabline);
1207 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1209 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1210 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1211 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1212 446b8c27 2021-03-11 op wprintw(tabline, " %s%d:todo title ",
1213 446b8c27 2021-03-11 op current ? "*" : "", tab->id);
1218 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1221 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1222 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
1223 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1225 48e9d457 2021-03-06 op wclear(modeline);
1226 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1227 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1229 481340cc 2021-03-11 op wprintw(modeline, "-%c %s ",
1230 481340cc 2021-03-11 op spin[tab->s->loading_anim_step], mode);
1232 481340cc 2021-03-11 op pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1234 481340cc 2021-03-11 op if (tab->s->line_max <= body_lines)
1235 481340cc 2021-03-11 op wprintw(modeline, "All ");
1236 481340cc 2021-03-11 op else if (tab->s->line_off == 0)
1237 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1238 481340cc 2021-03-11 op else if (tab->s->line_off + body_lines >= tab->s->line_max)
1239 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1241 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1243 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1244 481340cc 2021-03-11 op tab->s->line_off + tab->s->curs_y,
1245 481340cc 2021-03-11 op tab->s->line_max,
1246 481340cc 2021-03-11 op tab->urlstr);
1248 48e9d457 2021-03-06 op getyx(modeline, y, x);
1249 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1252 48e9d457 2021-03-06 op (void)max_y;
1254 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1255 48e9d457 2021-03-06 op waddstr(modeline, "-");
1259 9ca15951 2021-03-09 op redraw_minibuffer(void)
1261 9cb0f9ce 2021-03-10 op size_t skip = 0, off = 0;
1263 9ca15951 2021-03-09 op wclear(minibuf);
1264 9ca15951 2021-03-09 op if (!in_minibuffer)
1265 9cb0f9ce 2021-03-10 op goto message;
1267 9ca15951 2021-03-09 op off = strlen(ministate.prompt);
1269 9cb0f9ce 2021-03-10 op while (ministate.off - skip > COLS / 2) {
1270 9cb0f9ce 2021-03-10 op skip += MIN(ministate.off/4, 1);
1273 9ca15951 2021-03-09 op mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1274 9cb0f9ce 2021-03-10 op ministate.buf + skip);
1277 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1278 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1279 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1281 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1284 9cb0f9ce 2021-03-10 op wmove(minibuf, 0, off + ministate.off - skip);
1288 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1290 e19f9a04 2021-03-11 op redraw_tabline();
1291 e19f9a04 2021-03-11 op redraw_body(tab);
1292 e19f9a04 2021-03-11 op redraw_modeline(tab);
1293 e19f9a04 2021-03-11 op redraw_minibuffer();
1295 e19f9a04 2021-03-11 op restore_cursor(tab);
1296 e19f9a04 2021-03-11 op wrefresh(tabline);
1297 e19f9a04 2021-03-11 op wrefresh(modeline);
1299 e19f9a04 2021-03-11 op if (in_minibuffer) {
1300 e19f9a04 2021-03-11 op wrefresh(body);
1301 e19f9a04 2021-03-11 op wrefresh(minibuf);
1303 e19f9a04 2021-03-11 op wrefresh(minibuf);
1304 e19f9a04 2021-03-11 op wrefresh(body);
1309 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1311 48e9d457 2021-03-06 op struct line *l;
1314 48e9d457 2021-03-06 op werase(body);
1316 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1317 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1321 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1322 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1323 48e9d457 2021-03-06 op wmove(body, line, 0);
1324 1d08c280 2021-03-06 op print_line(l);
1326 48e9d457 2021-03-06 op if (line == body_lines)
1332 7953dd72 2021-03-07 op message(const char *fmt, ...)
1336 7953dd72 2021-03-07 op if (clminibufev_set)
1337 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1338 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1339 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1340 7953dd72 2021-03-07 op clminibufev_set = 1;
1342 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1343 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1344 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1345 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1348 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1350 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1351 9cb0f9ce 2021-03-10 op wrefresh(body);
1352 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1354 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1355 9cb0f9ce 2021-03-10 op wrefresh(body);
1360 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1362 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1364 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1365 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1366 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1370 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1372 8af5e5ed 2021-03-08 op struct tab *tab = d;
1374 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1376 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1377 8af5e5ed 2021-03-08 op wrefresh(modeline);
1379 8af5e5ed 2021-03-08 op wrefresh(body);
1380 9ca15951 2021-03-09 op if (in_minibuffer)
1381 9ca15951 2021-03-09 op wrefresh(minibuf);
1383 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1387 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1389 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1391 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1392 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1393 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1395 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1397 43a1b8d0 2021-03-09 op wrefresh(modeline);
1398 43a1b8d0 2021-03-09 op wrefresh(body);
1399 9ca15951 2021-03-09 op if (in_minibuffer)
1400 9ca15951 2021-03-09 op wrefresh(minibuf);
1404 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1406 43a1b8d0 2021-03-09 op empty_vlist(tab);
1407 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1408 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1409 8af5e5ed 2021-03-08 op load_url(tab, url);
1411 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1412 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1413 43a1b8d0 2021-03-09 op redraw_tab(tab);
1417 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1418 b360ebb3 2021-03-10 op void (*abortfn)(void))
1420 9ca15951 2021-03-09 op in_minibuffer = 1;
1421 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1422 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1424 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1426 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1427 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1428 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1429 9ca15951 2021-03-09 op ministate.off = 0;
1430 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1434 b360ebb3 2021-03-10 op exit_minibuffer(void)
1436 9ca15951 2021-03-09 op wclear(minibuf);
1438 9ca15951 2021-03-09 op in_minibuffer = 0;
1439 9ca15951 2021-03-09 op base_map = &global_map;
1440 9ca15951 2021-03-09 op current_map = &global_map;
1444 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1446 5cd2ebb1 2021-03-11 op struct tab *t;
1448 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1449 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1452 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1456 bcb0b073 2021-03-07 op new_tab(void)
1458 bcb0b073 2021-03-07 op struct tab *tab, *t;
1459 bcb0b073 2021-03-07 op const char *url = "about:new";
1461 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1464 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1467 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1469 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1470 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1472 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1473 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1475 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1477 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1481 bcb0b073 2021-03-07 op event_loopbreak();
1485 5e11c00c 2021-03-02 op ui_init(void)
1487 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1489 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1490 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1492 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1494 9ca15951 2021-03-09 op base_map = &global_map;
1495 f832146f 2021-03-09 op current_map = &global_map;
1496 f832146f 2021-03-09 op load_default_keys();
1503 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1505 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1507 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1509 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1511 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1514 48e9d457 2021-03-06 op body_lines = LINES-3;
1515 48e9d457 2021-03-06 op body_cols = COLS;
1517 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1518 48e9d457 2021-03-06 op scrollok(body, TRUE);
1520 5e11c00c 2021-03-02 op /* non-blocking input */
1521 48e9d457 2021-03-06 op wtimeout(body, 0);
1523 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1525 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1526 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1528 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1529 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1537 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1539 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1540 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1544 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1546 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1549 1d08c280 2021-03-06 op wrap_page(tab);
1550 5e11c00c 2021-03-02 op redraw_tab(tab);
1554 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1556 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1557 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1559 5cd2ebb1 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer);
1560 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1561 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1562 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1566 5e11c00c 2021-03-02 op ui_end(void)