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 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
97 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
98 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
100 9ca15951 2021-03-09 op static void global_key_unbound(void);
102 9ca15951 2021-03-09 op static void cmd_mini_del(struct tab*);
103 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
104 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
105 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
106 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
107 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
108 b360ebb3 2021-03-10 op static void cmd_mini_abort();
109 b360ebb3 2021-03-10 op static void cmd_mini_complete_and_exit();
111 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
112 b360ebb3 2021-03-10 op static void eecmd_select(void);
114 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
115 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
116 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
117 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
118 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
119 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
120 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
121 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
122 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
123 48e9d457 2021-03-06 op static void print_line(struct line*);
124 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
125 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
126 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
127 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
128 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
129 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
130 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
131 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
132 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
133 b360ebb3 2021-03-10 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
134 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
135 bcb0b073 2021-03-07 op static void new_tab(void);
137 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
139 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
140 48e9d457 2021-03-06 op static int body_lines, body_cols;
142 48e9d457 2021-03-06 op static struct event clminibufev;
143 48e9d457 2021-03-06 op static int clminibufev_set;
144 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
145 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
147 bcb0b073 2021-03-07 op static uint32_t tab_counter;
149 1d08c280 2021-03-06 op struct ui_state {
152 48e9d457 2021-03-06 op size_t line_off;
153 48e9d457 2021-03-06 op size_t line_max;
155 8af5e5ed 2021-03-08 op short loading_anim;
156 8af5e5ed 2021-03-08 op short loading_anim_step;
157 8af5e5ed 2021-03-08 op struct event loadingev;
159 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
162 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
164 9ca15951 2021-03-09 op struct kmap {
165 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
166 9ca15951 2021-03-09 op void (*unhandled_input)(void);
169 9ca15951 2021-03-09 op struct kmap global_map,
170 fa3fd864 2021-03-10 op minibuffer_map,
171 9ca15951 2021-03-09 op *current_map,
174 f832146f 2021-03-09 op struct keymap {
177 f832146f 2021-03-09 op struct kmap map;
178 f832146f 2021-03-09 op void (*fn)(struct tab*);
180 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
183 9ca15951 2021-03-09 op static int in_minibuffer;
185 9ca15951 2021-03-09 op static struct {
186 9cb0f9ce 2021-03-10 op char *curmesg;
188 9ca15951 2021-03-09 op char buf[1024];
189 9ca15951 2021-03-09 op size_t off, len;
190 9ca15951 2021-03-09 op char prompt[16];
191 b360ebb3 2021-03-10 op void (*donefn)(void);
192 b360ebb3 2021-03-10 op void (*abortfn)(void);
196 f832146f 2021-03-09 op kbd(const char *key)
198 f832146f 2021-03-09 op struct table {
201 f832146f 2021-03-09 op } table[] = {
202 f832146f 2021-03-09 op { "<up>", KEY_UP },
203 f832146f 2021-03-09 op { "<down>", KEY_DOWN },
204 f832146f 2021-03-09 op { "<left>", KEY_LEFT },
205 f832146f 2021-03-09 op { "<right>", KEY_RIGHT },
206 ed44414d 2021-03-09 op { "<prior>", KEY_PPAGE },
207 ed44414d 2021-03-09 op { "<next>", KEY_NPAGE },
208 9ca15951 2021-03-09 op { "<home>", KEY_HOME },
209 9ca15951 2021-03-09 op { "<end>", KEY_END },
211 9ca15951 2021-03-09 op { "del", KEY_BACKSPACE },
212 9ca15951 2021-03-09 op { "esc", 27 },
213 f832146f 2021-03-09 op { "space", ' ' },
214 f832146f 2021-03-09 op { "spc", ' ' },
215 f832146f 2021-03-09 op { "enter", CTRL('m') },
216 b360ebb3 2021-03-10 op { "ret", CTRL('m' )},
217 f832146f 2021-03-09 op { "tab", CTRL('i') },
222 f832146f 2021-03-09 op for (t = table; t->p != NULL; ++t) {
223 f832146f 2021-03-09 op if (has_prefix(key, t->p))
231 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
233 f832146f 2021-03-09 op int ctrl, meta, k;
234 f832146f 2021-03-09 op struct keymap *entry;
237 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
239 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
241 f832146f 2021-03-09 op if (*key == '\0')
243 f832146f 2021-03-09 op k = kbd(key);
248 f832146f 2021-03-09 op /* skip key & spaces */
249 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
251 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
254 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
255 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
256 f832146f 2021-03-09 op if (*key == '\0') {
257 f832146f 2021-03-09 op entry->fn = fn;
260 f832146f 2021-03-09 op map = &entry->map;
265 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
268 f832146f 2021-03-09 op entry->meta = meta;
269 f832146f 2021-03-09 op entry->key = k;
270 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
272 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
273 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
275 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
277 f832146f 2021-03-09 op if (*key != '\0') {
278 f832146f 2021-03-09 op map = &entry->map;
282 f832146f 2021-03-09 op entry->fn = fn;
285 f832146f 2021-03-09 op static inline void
286 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
288 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
291 9ca15951 2021-03-09 op static inline void
292 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
294 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
298 f832146f 2021-03-09 op load_default_keys(void)
300 9ca15951 2021-03-09 op /* === global map === */
303 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
304 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
305 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
306 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
308 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
309 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
311 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
313 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
315 f832146f 2021-03-09 op /* vi/vi-like */
316 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
317 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
318 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
319 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
321 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
322 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
325 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
327 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
330 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
331 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
332 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
333 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
334 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
335 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
337 f832146f 2021-03-09 op /* "ncurses standard" */
338 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
341 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
343 fa3fd864 2021-03-10 op /* === minibuffer map === */
344 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
345 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
346 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
347 fa3fd864 2021-03-10 op minibuffer_set_key("del", cmd_mini_del);
349 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
350 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
351 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
352 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
353 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
354 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
355 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
359 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
361 1d08c280 2021-03-06 op struct line *vl;
363 48e9d457 2021-03-06 op tab->s->line_max++;
365 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
368 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
373 1d08c280 2021-03-06 op vl->type = l->type;
374 1d08c280 2021-03-06 op if (len != 0)
375 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
376 1d08c280 2021-03-06 op vl->alt = l->alt;
378 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
379 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
381 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
386 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
388 1d08c280 2021-03-06 op struct line *l, *t;
390 48e9d457 2021-03-06 op tab->s->line_max = 0;
392 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
393 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
394 1d08c280 2021-03-06 op free(l->line);
395 1d08c280 2021-03-06 op /* l->alt references the original line! */
401 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
403 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
407 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
409 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
410 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
411 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
414 48e9d457 2021-03-06 op restore_cursor(tab);
418 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
420 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
421 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
422 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
425 48e9d457 2021-03-06 op restore_cursor(tab);
429 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
431 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
432 48e9d457 2021-03-06 op restore_cursor(tab);
436 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
438 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
439 48e9d457 2021-03-06 op restore_cursor(tab);
443 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
445 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
449 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
451 48e9d457 2021-03-06 op struct line *l;
453 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
456 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
457 48e9d457 2021-03-06 op wscrl(body, -1);
458 48e9d457 2021-03-06 op wmove(body, 0, 0);
459 48e9d457 2021-03-06 op print_line(l);
463 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
465 48e9d457 2021-03-06 op struct line *l;
468 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
471 48e9d457 2021-03-06 op tab->s->line_off++;
472 48e9d457 2021-03-06 op wscrl(body, 1);
474 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
477 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
478 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
479 48e9d457 2021-03-06 op print_line(l);
483 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
487 ed44414d 2021-03-09 op off = body_lines+1;
489 ed44414d 2021-03-09 op for (; off > 0; --off)
490 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
494 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
498 ed44414d 2021-03-09 op off = tab->s->line_off + body_lines;
499 ed44414d 2021-03-09 op off = MIN(tab->s->line_max, off);
501 ed44414d 2021-03-09 op for (; off >= 0; --off)
502 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
506 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
508 1d08c280 2021-03-06 op event_loopbreak();
512 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
514 2a4ad912 2021-03-08 op struct line *l;
517 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
518 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
520 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
521 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
524 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
528 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
532 b360ebb3 2021-03-10 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
534 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
535 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
537 9ca15951 2021-03-09 op if (thiskey.meta)
538 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
540 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
541 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
545 9ca15951 2021-03-09 op global_key_unbound(void)
547 9ca15951 2021-03-09 op message("%s%c is undefined",
548 9ca15951 2021-03-09 op thiskey.meta ? "M-" : "",
549 9ca15951 2021-03-09 op thiskey.key);
553 9ca15951 2021-03-09 op cmd_mini_del(struct tab *tab)
555 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
558 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
559 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
560 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
561 9ca15951 2021-03-09 op ministate.off--;
562 9ca15951 2021-03-09 op ministate.len--;
566 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
568 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
570 9ca15951 2021-03-09 op ministate.off++;
574 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
576 9ca15951 2021-03-09 op if (ministate.off == 0)
578 9ca15951 2021-03-09 op ministate.off--;
582 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
584 9ca15951 2021-03-09 op ministate.off = ministate.len;
588 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
590 9ca15951 2021-03-09 op ministate.off = 0;
594 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
596 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
598 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
599 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
603 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
605 b360ebb3 2021-03-10 op ministate.abortfn();
609 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
611 b360ebb3 2021-03-10 op ministate.donefn();
615 9ca15951 2021-03-09 op eecmd_self_insert(void)
617 9ca15951 2021-03-09 op if (thiskey.meta || isspace(thiskey.key) ||
618 9ca15951 2021-03-09 op !isgraph(thiskey.key)) {
619 9ca15951 2021-03-09 op global_key_unbound();
623 9ca15951 2021-03-09 op if (ministate.len == sizeof(ministate.buf))
626 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
628 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
629 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
630 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
631 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
632 9ca15951 2021-03-09 op ministate.off++;
633 9ca15951 2021-03-09 op ministate.len++;
637 b360ebb3 2021-03-10 op eecmd_select(void)
639 b360ebb3 2021-03-10 op exit_minibuffer();
640 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
643 48e9d457 2021-03-06 op static struct line *
644 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
646 48e9d457 2021-03-06 op struct line *l;
650 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
656 48e9d457 2021-03-06 op /* unreachable */
660 5e11c00c 2021-03-02 op static struct tab *
661 5e11c00c 2021-03-02 op current_tab(void)
663 5e11c00c 2021-03-02 op struct tab *t;
665 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
666 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
670 5e11c00c 2021-03-02 op /* unreachable */
675 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
677 f832146f 2021-03-09 op struct keymap *k;
679 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
680 9ca15951 2021-03-09 op if (thiskey.key == ERR)
682 9ca15951 2021-03-09 op if (thiskey.key == 27) {
683 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
685 9ca15951 2021-03-09 op thiskey.meta = 1;
686 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
687 9ca15951 2021-03-09 op if (thiskey.key == ERR)
688 9ca15951 2021-03-09 op thiskey.key = 27;
690 9ca15951 2021-03-09 op thiskey.meta = 0;
692 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
693 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
694 9ca15951 2021-03-09 op k->key == thiskey.key) {
695 f832146f 2021-03-09 op if (k->fn == NULL)
696 f832146f 2021-03-09 op current_map = &k->map;
698 9ca15951 2021-03-09 op current_map = base_map;
699 f832146f 2021-03-09 op k->fn(current_tab());
705 9ca15951 2021-03-09 op current_map->unhandled_input();
706 9ca15951 2021-03-09 op current_map = base_map;
709 9ca15951 2021-03-09 op redraw_minibuffer();
710 48e9d457 2021-03-06 op restore_cursor(current_tab());
711 48e9d457 2021-03-06 op wrefresh(tabline);
712 48e9d457 2021-03-06 op wrefresh(modeline);
714 9ca15951 2021-03-09 op if (in_minibuffer) {
715 9ca15951 2021-03-09 op wrefresh(body);
716 9ca15951 2021-03-09 op wrefresh(minibuf);
718 9ca15951 2021-03-09 op wrefresh(minibuf);
719 9ca15951 2021-03-09 op wrefresh(body);
724 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
726 a6d450c1 2021-03-06 op clminibufev_set = 0;
728 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
729 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
731 9cb0f9ce 2021-03-10 op redraw_minibuffer();
732 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
733 9cb0f9ce 2021-03-10 op wrefresh(body);
734 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
736 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
737 9cb0f9ce 2021-03-10 op wrefresh(body);
742 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
744 1d08c280 2021-03-06 op struct tab *tab;
750 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
752 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
753 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
755 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
756 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
758 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
759 48e9d457 2021-03-06 op body_lines = LINES-3;
760 bd9637e9 2021-03-06 op body_cols = COLS;
762 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
764 1d08c280 2021-03-06 op tab = current_tab();
766 1d08c280 2021-03-06 op wrap_page(tab);
767 1d08c280 2021-03-06 op redraw_tab(tab);
771 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
772 eb259e66 2021-03-02 op * and the end of the separator after the word.
775 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
777 eb259e66 2021-03-02 op *endword = s;
778 eb259e66 2021-03-02 op *endword = s;
780 eb259e66 2021-03-02 op if (*s == '\0')
783 eb259e66 2021-03-02 op /* find the end of the current world */
784 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
785 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
789 eb259e66 2021-03-02 op *endword = s;
791 eb259e66 2021-03-02 op /* find the end of the separator */
792 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
793 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
802 1d08c280 2021-03-06 op static inline int
803 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
804 1d08c280 2021-03-06 op const char **line)
806 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
808 1d08c280 2021-03-06 op *line += *off - zero;
813 eb259e66 2021-03-02 op static inline void
814 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
818 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
819 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
820 eb259e66 2021-03-02 op addch((*s)[i]);
826 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
827 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
829 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
832 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
834 eb259e66 2021-03-02 op size_t zero, off, len, split;
835 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
837 1d08c280 2021-03-06 op zero = strlen(prfx);
839 1d08c280 2021-03-06 op line = l->line;
840 1d08c280 2021-03-06 op linestart = l->line;
842 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
843 eb259e66 2021-03-02 op len = endword - line;
844 48e9d457 2021-03-06 op if (off + len >= body_cols) {
845 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
846 48e9d457 2021-03-06 op while (len >= body_cols) {
847 eb259e66 2021-03-02 op /* hard wrap */
848 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
849 48e9d457 2021-03-06 op len -= body_cols-1;
850 48e9d457 2021-03-06 op line += body_cols-1;
853 eb259e66 2021-03-02 op if (len != 0)
858 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
859 eb259e66 2021-03-02 op len = endspc - endword;
860 eb259e66 2021-03-02 op /* line = endspc; */
861 eb259e66 2021-03-02 op if (off != zero) {
862 48e9d457 2021-03-06 op if (off + len >= body_cols) {
863 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
864 1d08c280 2021-03-06 op linestart = endspc;
869 eb259e66 2021-03-02 op line = endspc;
872 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
876 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
878 1d08c280 2021-03-06 op size_t off, len;
879 1d08c280 2021-03-06 op const char *linestart;
881 1d08c280 2021-03-06 op len = strlen(l->line);
883 1d08c280 2021-03-06 op linestart = l->line;
885 1d08c280 2021-03-06 op while (len >= COLS) {
886 1d08c280 2021-03-06 op len -= COLS-1;
887 1d08c280 2021-03-06 op off = COLS-1;
888 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
892 ede1f142 2021-03-07 op if (len != 0)
893 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
899 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
901 1d08c280 2021-03-06 op struct line *l;
903 1d08c280 2021-03-06 op empty_vlist(tab);
905 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
906 5e11c00c 2021-03-02 op switch (l->type) {
907 5e11c00c 2021-03-02 op case LINE_TEXT:
908 1d08c280 2021-03-06 op wrap_text(tab, "", l);
910 5e11c00c 2021-03-02 op case LINE_LINK:
911 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
913 5e11c00c 2021-03-02 op case LINE_TITLE_1:
914 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
916 5e11c00c 2021-03-02 op case LINE_TITLE_2:
917 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
919 5e11c00c 2021-03-02 op case LINE_TITLE_3:
920 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
922 5e11c00c 2021-03-02 op case LINE_ITEM:
923 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
925 5e11c00c 2021-03-02 op case LINE_QUOTE:
926 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
928 5e11c00c 2021-03-02 op case LINE_PRE_START:
929 5e11c00c 2021-03-02 op case LINE_PRE_END:
930 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
932 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
933 1d08c280 2021-03-06 op hardwrap_text(tab, l);
940 1d08c280 2021-03-06 op static inline void
941 1d08c280 2021-03-06 op print_line(struct line *l)
943 bd9637e9 2021-03-06 op const char *text = l->line;
945 bd9637e9 2021-03-06 op if (text == NULL)
948 1d08c280 2021-03-06 op switch (l->type) {
949 1d08c280 2021-03-06 op case LINE_TEXT:
950 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
952 1d08c280 2021-03-06 op case LINE_LINK:
953 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
954 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
955 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
957 1d08c280 2021-03-06 op case LINE_TITLE_1:
958 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
959 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
960 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
962 1d08c280 2021-03-06 op case LINE_TITLE_2:
963 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
964 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
965 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
967 bd9637e9 2021-03-06 op case LINE_TITLE_3:
968 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
969 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
970 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
972 bd9637e9 2021-03-06 op case LINE_ITEM:
973 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
975 1d08c280 2021-03-06 op case LINE_QUOTE:
976 bd9637e9 2021-03-06 op wattron(body, A_DIM);
977 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
978 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
980 1d08c280 2021-03-06 op case LINE_PRE_START:
981 1d08c280 2021-03-06 op case LINE_PRE_END:
982 48e9d457 2021-03-06 op wprintw(body, "```");
984 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
985 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
991 8af5e5ed 2021-03-08 op redraw_tabline(void)
993 8af5e5ed 2021-03-08 op wclear(tabline);
994 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
995 8af5e5ed 2021-03-08 op mvwprintw(tabline, 0, 0, "TODO: tabs here");
999 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1001 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1002 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
1003 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1005 48e9d457 2021-03-06 op wclear(modeline);
1006 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1007 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1009 8af5e5ed 2021-03-08 op wprintw(modeline, "-%c %s %s ",
1010 4d3785b1 2021-03-09 op spin[tab->s->loading_anim_step], mode, tab->urlstr);
1011 48e9d457 2021-03-06 op getyx(modeline, y, x);
1012 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1015 48e9d457 2021-03-06 op (void)max_y;
1017 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1018 48e9d457 2021-03-06 op waddstr(modeline, "-");
1022 9ca15951 2021-03-09 op redraw_minibuffer(void)
1024 9cb0f9ce 2021-03-10 op size_t skip = 0, off = 0;
1026 9ca15951 2021-03-09 op wclear(minibuf);
1027 9ca15951 2021-03-09 op if (!in_minibuffer)
1028 9cb0f9ce 2021-03-10 op goto message;
1030 9ca15951 2021-03-09 op off = strlen(ministate.prompt);
1032 9cb0f9ce 2021-03-10 op while (ministate.off - skip > COLS / 2) {
1033 9cb0f9ce 2021-03-10 op skip += MIN(ministate.off/4, 1);
1036 9ca15951 2021-03-09 op mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1037 9cb0f9ce 2021-03-10 op ministate.buf + skip);
1040 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1041 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1042 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1044 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1047 9cb0f9ce 2021-03-10 op wmove(minibuf, 0, off + ministate.off - skip);
1051 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1053 48e9d457 2021-03-06 op struct line *l;
1056 48e9d457 2021-03-06 op werase(body);
1058 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1059 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1063 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1064 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1065 48e9d457 2021-03-06 op wmove(body, line, 0);
1066 1d08c280 2021-03-06 op print_line(l);
1068 48e9d457 2021-03-06 op if (line == body_lines)
1072 8af5e5ed 2021-03-08 op redraw_tabline();
1073 48e9d457 2021-03-06 op redraw_modeline(tab);
1074 9ca15951 2021-03-09 op redraw_minibuffer();
1076 48e9d457 2021-03-06 op restore_cursor(tab);
1077 48e9d457 2021-03-06 op wrefresh(tabline);
1078 48e9d457 2021-03-06 op wrefresh(modeline);
1080 9ca15951 2021-03-09 op if (in_minibuffer) {
1081 9ca15951 2021-03-09 op wrefresh(body);
1082 9ca15951 2021-03-09 op wrefresh(minibuf);
1084 9ca15951 2021-03-09 op wrefresh(minibuf);
1085 9ca15951 2021-03-09 op wrefresh(body);
1090 7953dd72 2021-03-07 op message(const char *fmt, ...)
1094 7953dd72 2021-03-07 op if (clminibufev_set)
1095 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1096 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1097 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1098 7953dd72 2021-03-07 op clminibufev_set = 1;
1100 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1101 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1102 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1103 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1106 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1108 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1109 9cb0f9ce 2021-03-10 op wrefresh(body);
1110 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1112 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1113 9cb0f9ce 2021-03-10 op wrefresh(body);
1118 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1120 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1122 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1123 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1124 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1128 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1130 8af5e5ed 2021-03-08 op struct tab *tab = d;
1132 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1134 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1135 8af5e5ed 2021-03-08 op wrefresh(modeline);
1137 8af5e5ed 2021-03-08 op wrefresh(body);
1138 9ca15951 2021-03-09 op if (in_minibuffer)
1139 9ca15951 2021-03-09 op wrefresh(minibuf);
1141 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1145 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1147 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1149 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1150 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1151 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1153 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1155 43a1b8d0 2021-03-09 op wrefresh(modeline);
1156 43a1b8d0 2021-03-09 op wrefresh(body);
1157 9ca15951 2021-03-09 op if (in_minibuffer)
1158 9ca15951 2021-03-09 op wrefresh(minibuf);
1162 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1164 43a1b8d0 2021-03-09 op empty_vlist(tab);
1165 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1166 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1167 8af5e5ed 2021-03-08 op load_url(tab, url);
1169 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1170 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1171 43a1b8d0 2021-03-09 op redraw_tab(tab);
1175 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1176 b360ebb3 2021-03-10 op void (*abortfn)(void))
1178 9ca15951 2021-03-09 op in_minibuffer = 1;
1179 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1180 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1182 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1184 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1185 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1186 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1187 9ca15951 2021-03-09 op ministate.off = 0;
1188 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1192 b360ebb3 2021-03-10 op exit_minibuffer(void)
1194 9ca15951 2021-03-09 op wclear(minibuf);
1196 9ca15951 2021-03-09 op in_minibuffer = 0;
1197 9ca15951 2021-03-09 op base_map = &global_map;
1198 9ca15951 2021-03-09 op current_map = &global_map;
1202 bcb0b073 2021-03-07 op new_tab(void)
1204 bcb0b073 2021-03-07 op struct tab *tab, *t;
1205 bcb0b073 2021-03-07 op const char *url = "about:new";
1207 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1210 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1213 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1214 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
1215 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
1218 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1219 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
1221 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1222 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1224 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1226 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1230 bcb0b073 2021-03-07 op event_loopbreak();
1234 5e11c00c 2021-03-02 op ui_init(void)
1236 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1238 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1239 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1241 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1243 9ca15951 2021-03-09 op base_map = &global_map;
1244 f832146f 2021-03-09 op current_map = &global_map;
1245 f832146f 2021-03-09 op load_default_keys();
1252 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1254 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1256 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1258 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1260 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1263 48e9d457 2021-03-06 op body_lines = LINES-3;
1264 48e9d457 2021-03-06 op body_cols = COLS;
1266 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1267 48e9d457 2021-03-06 op scrollok(body, TRUE);
1269 5e11c00c 2021-03-02 op /* non-blocking input */
1270 48e9d457 2021-03-06 op wtimeout(body, 0);
1272 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1274 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1275 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1277 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1278 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1286 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1288 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1289 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1293 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1295 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1298 1d08c280 2021-03-06 op wrap_page(tab);
1299 5e11c00c 2021-03-02 op redraw_tab(tab);
1303 5e11c00c 2021-03-02 op ui_end(void)