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*);
99 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
100 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
101 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
102 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
104 9ca15951 2021-03-09 op static void global_key_unbound(void);
106 9ca15951 2021-03-09 op static void cmd_mini_del(struct tab*);
107 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
108 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
109 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
110 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
111 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
112 b360ebb3 2021-03-10 op static void cmd_mini_abort();
113 b360ebb3 2021-03-10 op static void cmd_mini_complete_and_exit();
115 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
116 b360ebb3 2021-03-10 op static void eecmd_select(void);
118 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
119 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
120 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
121 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
122 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
123 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
124 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
125 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
126 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
127 48e9d457 2021-03-06 op static void print_line(struct line*);
128 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
129 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
130 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
131 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
132 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
133 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
134 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
135 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
136 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
137 b360ebb3 2021-03-10 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
138 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
139 bcb0b073 2021-03-07 op static void new_tab(void);
141 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
143 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
144 48e9d457 2021-03-06 op static int body_lines, body_cols;
146 48e9d457 2021-03-06 op static struct event clminibufev;
147 48e9d457 2021-03-06 op static int clminibufev_set;
148 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
149 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
151 bcb0b073 2021-03-07 op static uint32_t tab_counter;
153 1d08c280 2021-03-06 op struct ui_state {
156 48e9d457 2021-03-06 op size_t line_off;
157 48e9d457 2021-03-06 op size_t line_max;
159 8af5e5ed 2021-03-08 op short loading_anim;
160 8af5e5ed 2021-03-08 op short loading_anim_step;
161 8af5e5ed 2021-03-08 op struct event loadingev;
163 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
166 7c7d7bb7 2021-03-10 op static char keybuf[64];
168 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
170 a777f81f 2021-03-10 op struct keytable {
173 a777f81f 2021-03-10 op } keytable[] = {
174 a777f81f 2021-03-10 op { "<up>", KEY_UP },
175 a777f81f 2021-03-10 op { "<down>", KEY_DOWN },
176 a777f81f 2021-03-10 op { "<left>", KEY_LEFT },
177 a777f81f 2021-03-10 op { "<right>", KEY_RIGHT },
178 a777f81f 2021-03-10 op { "<prior>", KEY_PPAGE },
179 a777f81f 2021-03-10 op { "<next>", KEY_NPAGE },
180 a777f81f 2021-03-10 op { "<home>", KEY_HOME },
181 a777f81f 2021-03-10 op { "<end>", KEY_END },
183 a777f81f 2021-03-10 op { "del", KEY_BACKSPACE },
184 a777f81f 2021-03-10 op { "esc", 27 },
185 a777f81f 2021-03-10 op { "space", ' ' },
186 a777f81f 2021-03-10 op { "spc", ' ' },
187 a777f81f 2021-03-10 op { "enter", CTRL('m') },
188 a777f81f 2021-03-10 op { "ret", CTRL('m' )},
189 a777f81f 2021-03-10 op { "tab", CTRL('i') },
194 9ca15951 2021-03-09 op struct kmap {
195 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
196 9ca15951 2021-03-09 op void (*unhandled_input)(void);
199 9ca15951 2021-03-09 op struct kmap global_map,
200 fa3fd864 2021-03-10 op minibuffer_map,
201 9ca15951 2021-03-09 op *current_map,
204 f832146f 2021-03-09 op struct keymap {
207 f832146f 2021-03-09 op struct kmap map;
208 f832146f 2021-03-09 op void (*fn)(struct tab*);
210 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
213 9ca15951 2021-03-09 op static int in_minibuffer;
215 9ca15951 2021-03-09 op static struct {
216 9cb0f9ce 2021-03-10 op char *curmesg;
218 040fbdf8 2021-03-10 op char buf[1025];
219 9ca15951 2021-03-09 op size_t off, len;
220 9ca15951 2021-03-09 op char prompt[16];
221 b360ebb3 2021-03-10 op void (*donefn)(void);
222 b360ebb3 2021-03-10 op void (*abortfn)(void);
226 f832146f 2021-03-09 op kbd(const char *key)
228 a777f81f 2021-03-10 op struct keytable *t;
230 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
231 f832146f 2021-03-09 op if (has_prefix(key, t->p))
238 a777f81f 2021-03-10 op static const char *
241 a777f81f 2021-03-10 op struct keytable *t;
243 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
244 a777f81f 2021-03-10 op if (k == t->k)
252 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
254 f832146f 2021-03-09 op int ctrl, meta, k;
255 f832146f 2021-03-09 op struct keymap *entry;
258 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
260 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
262 f832146f 2021-03-09 op if (*key == '\0')
264 f832146f 2021-03-09 op k = kbd(key);
269 f832146f 2021-03-09 op /* skip key & spaces */
270 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
272 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
275 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
276 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
277 f832146f 2021-03-09 op if (*key == '\0') {
278 f832146f 2021-03-09 op entry->fn = fn;
281 f832146f 2021-03-09 op map = &entry->map;
286 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
289 f832146f 2021-03-09 op entry->meta = meta;
290 f832146f 2021-03-09 op entry->key = k;
291 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
293 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
294 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
296 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
298 f832146f 2021-03-09 op if (*key != '\0') {
299 f832146f 2021-03-09 op map = &entry->map;
303 f832146f 2021-03-09 op entry->fn = fn;
306 f832146f 2021-03-09 op static inline void
307 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
309 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
312 9ca15951 2021-03-09 op static inline void
313 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
315 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
319 f832146f 2021-03-09 op load_default_keys(void)
321 9ca15951 2021-03-09 op /* === global map === */
324 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
325 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
326 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
327 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
329 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
330 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
332 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
334 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
336 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
337 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
338 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
339 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
341 f832146f 2021-03-09 op /* vi/vi-like */
342 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
343 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
344 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
345 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
347 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
348 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
351 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
353 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
356 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
357 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
358 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
359 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
360 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
361 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
363 f832146f 2021-03-09 op /* "ncurses standard" */
364 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
367 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
369 fa3fd864 2021-03-10 op /* === minibuffer map === */
370 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
371 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
372 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
373 fa3fd864 2021-03-10 op minibuffer_set_key("del", cmd_mini_del);
375 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
376 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
377 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
378 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
379 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
380 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
381 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
385 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
387 1d08c280 2021-03-06 op struct line *vl;
389 48e9d457 2021-03-06 op tab->s->line_max++;
391 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
394 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
399 1d08c280 2021-03-06 op vl->type = l->type;
400 1d08c280 2021-03-06 op if (len != 0)
401 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
402 1d08c280 2021-03-06 op vl->alt = l->alt;
404 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
405 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
407 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
412 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
414 1d08c280 2021-03-06 op struct line *l, *t;
416 48e9d457 2021-03-06 op tab->s->line_max = 0;
418 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
419 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
420 1d08c280 2021-03-06 op free(l->line);
421 1d08c280 2021-03-06 op /* l->alt references the original line! */
427 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
429 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
433 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
435 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
436 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
437 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
440 48e9d457 2021-03-06 op restore_cursor(tab);
444 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
446 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
447 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
448 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
451 48e9d457 2021-03-06 op restore_cursor(tab);
455 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
457 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
458 48e9d457 2021-03-06 op restore_cursor(tab);
462 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
464 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
465 48e9d457 2021-03-06 op restore_cursor(tab);
469 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
471 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
475 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
477 48e9d457 2021-03-06 op struct line *l;
479 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
482 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
483 48e9d457 2021-03-06 op wscrl(body, -1);
484 48e9d457 2021-03-06 op wmove(body, 0, 0);
485 48e9d457 2021-03-06 op print_line(l);
489 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
491 48e9d457 2021-03-06 op struct line *l;
494 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
497 48e9d457 2021-03-06 op tab->s->line_off++;
498 48e9d457 2021-03-06 op wscrl(body, 1);
500 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
503 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
504 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
505 48e9d457 2021-03-06 op print_line(l);
509 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
513 ed44414d 2021-03-09 op off = body_lines+1;
515 ed44414d 2021-03-09 op for (; off > 0; --off)
516 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
520 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
524 ed44414d 2021-03-09 op off = tab->s->line_off + body_lines;
525 ed44414d 2021-03-09 op off = MIN(tab->s->line_max, off);
527 ed44414d 2021-03-09 op for (; off >= 0; --off)
528 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
532 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
534 1d08c280 2021-03-06 op event_loopbreak();
538 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
540 2a4ad912 2021-03-08 op struct line *l;
543 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
544 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
546 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
547 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
550 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
554 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
558 b360ebb3 2021-03-10 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
560 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
561 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
563 9ca15951 2021-03-09 op if (thiskey.meta)
564 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
566 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
567 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
571 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
573 7c7d7bb7 2021-03-10 op struct tab *t;
575 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
576 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
577 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
581 7c7d7bb7 2021-03-10 op stop_tab(tab);
583 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
584 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
586 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
588 7c7d7bb7 2021-03-10 op free(tab->s);
593 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
599 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
601 7c7d7bb7 2021-03-10 op struct tab *t;
603 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
605 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
606 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
607 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
611 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
613 7c7d7bb7 2021-03-10 op struct tab *t;
615 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
617 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
618 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
619 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
623 7c7d7bb7 2021-03-10 op global_key_unbound(void)
625 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
629 9ca15951 2021-03-09 op cmd_mini_del(struct tab *tab)
631 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
634 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
635 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
636 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
637 9ca15951 2021-03-09 op ministate.off--;
638 9ca15951 2021-03-09 op ministate.len--;
642 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
644 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
646 9ca15951 2021-03-09 op ministate.off++;
650 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
652 9ca15951 2021-03-09 op if (ministate.off == 0)
654 9ca15951 2021-03-09 op ministate.off--;
658 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
660 9ca15951 2021-03-09 op ministate.off = ministate.len;
664 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
666 9ca15951 2021-03-09 op ministate.off = 0;
670 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
672 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
674 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
675 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
679 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
681 b360ebb3 2021-03-10 op ministate.abortfn();
685 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
687 b360ebb3 2021-03-10 op ministate.donefn();
691 9ca15951 2021-03-09 op eecmd_self_insert(void)
693 9ca15951 2021-03-09 op if (thiskey.meta || isspace(thiskey.key) ||
694 9ca15951 2021-03-09 op !isgraph(thiskey.key)) {
695 9ca15951 2021-03-09 op global_key_unbound();
699 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
702 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
704 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
705 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
706 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
707 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
708 9ca15951 2021-03-09 op ministate.off++;
709 9ca15951 2021-03-09 op ministate.len++;
713 b360ebb3 2021-03-10 op eecmd_select(void)
715 b360ebb3 2021-03-10 op exit_minibuffer();
716 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
719 48e9d457 2021-03-06 op static struct line *
720 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
722 48e9d457 2021-03-06 op struct line *l;
726 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
732 48e9d457 2021-03-06 op /* unreachable */
736 5e11c00c 2021-03-02 op static struct tab *
737 5e11c00c 2021-03-02 op current_tab(void)
739 5e11c00c 2021-03-02 op struct tab *t;
741 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
742 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
746 5e11c00c 2021-03-02 op /* unreachable */
751 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
753 f832146f 2021-03-09 op struct keymap *k;
754 7c7d7bb7 2021-03-10 op const char *keyname;
755 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
757 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
758 9ca15951 2021-03-09 op if (thiskey.key == ERR)
760 9ca15951 2021-03-09 op if (thiskey.key == 27) {
761 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
763 9ca15951 2021-03-09 op thiskey.meta = 1;
764 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
765 9ca15951 2021-03-09 op if (thiskey.key == ERR)
766 9ca15951 2021-03-09 op thiskey.key = 27;
768 9ca15951 2021-03-09 op thiskey.meta = 0;
770 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
771 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
772 7c7d7bb7 2021-03-10 op if (thiskey.meta)
773 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
774 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
775 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
777 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
778 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
781 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
782 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
783 9ca15951 2021-03-09 op k->key == thiskey.key) {
784 f832146f 2021-03-09 op if (k->fn == NULL)
785 f832146f 2021-03-09 op current_map = &k->map;
787 9ca15951 2021-03-09 op current_map = base_map;
788 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
789 f832146f 2021-03-09 op k->fn(current_tab());
795 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
796 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
798 7c7d7bb7 2021-03-10 op global_key_unbound();
801 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
802 9ca15951 2021-03-09 op current_map = base_map;
805 7c7d7bb7 2021-03-10 op redraw_tabline();
806 9ca15951 2021-03-09 op redraw_minibuffer();
807 48e9d457 2021-03-06 op restore_cursor(current_tab());
808 48e9d457 2021-03-06 op wrefresh(tabline);
809 48e9d457 2021-03-06 op wrefresh(modeline);
811 9ca15951 2021-03-09 op if (in_minibuffer) {
812 9ca15951 2021-03-09 op wrefresh(body);
813 9ca15951 2021-03-09 op wrefresh(minibuf);
815 9ca15951 2021-03-09 op wrefresh(minibuf);
816 9ca15951 2021-03-09 op wrefresh(body);
821 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
823 a6d450c1 2021-03-06 op clminibufev_set = 0;
825 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
826 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
828 9cb0f9ce 2021-03-10 op redraw_minibuffer();
829 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
830 9cb0f9ce 2021-03-10 op wrefresh(body);
831 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
833 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
834 9cb0f9ce 2021-03-10 op wrefresh(body);
839 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
841 1d08c280 2021-03-06 op struct tab *tab;
847 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
849 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
850 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
852 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
853 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
855 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
856 48e9d457 2021-03-06 op body_lines = LINES-3;
857 bd9637e9 2021-03-06 op body_cols = COLS;
859 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
861 1d08c280 2021-03-06 op tab = current_tab();
863 1d08c280 2021-03-06 op wrap_page(tab);
864 1d08c280 2021-03-06 op redraw_tab(tab);
868 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
869 eb259e66 2021-03-02 op * and the end of the separator after the word.
872 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
874 eb259e66 2021-03-02 op *endword = s;
875 eb259e66 2021-03-02 op *endword = s;
877 eb259e66 2021-03-02 op if (*s == '\0')
880 eb259e66 2021-03-02 op /* find the end of the current world */
881 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
882 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
886 eb259e66 2021-03-02 op *endword = s;
888 eb259e66 2021-03-02 op /* find the end of the separator */
889 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
890 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
899 1d08c280 2021-03-06 op static inline int
900 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
901 1d08c280 2021-03-06 op const char **line)
903 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
905 1d08c280 2021-03-06 op *line += *off - zero;
910 eb259e66 2021-03-02 op static inline void
911 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
915 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
916 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
917 eb259e66 2021-03-02 op addch((*s)[i]);
923 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
924 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
926 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
929 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
931 eb259e66 2021-03-02 op size_t zero, off, len, split;
932 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
934 1d08c280 2021-03-06 op zero = strlen(prfx);
936 1d08c280 2021-03-06 op line = l->line;
937 1d08c280 2021-03-06 op linestart = l->line;
939 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
940 eb259e66 2021-03-02 op len = endword - line;
941 48e9d457 2021-03-06 op if (off + len >= body_cols) {
942 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
943 48e9d457 2021-03-06 op while (len >= body_cols) {
944 eb259e66 2021-03-02 op /* hard wrap */
945 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
946 48e9d457 2021-03-06 op len -= body_cols-1;
947 48e9d457 2021-03-06 op line += body_cols-1;
950 eb259e66 2021-03-02 op if (len != 0)
955 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
956 eb259e66 2021-03-02 op len = endspc - endword;
957 eb259e66 2021-03-02 op /* line = endspc; */
958 eb259e66 2021-03-02 op if (off != zero) {
959 48e9d457 2021-03-06 op if (off + len >= body_cols) {
960 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
961 1d08c280 2021-03-06 op linestart = endspc;
966 eb259e66 2021-03-02 op line = endspc;
969 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
973 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
975 1d08c280 2021-03-06 op size_t off, len;
976 1d08c280 2021-03-06 op const char *linestart;
978 1d08c280 2021-03-06 op len = strlen(l->line);
980 1d08c280 2021-03-06 op linestart = l->line;
982 1d08c280 2021-03-06 op while (len >= COLS) {
983 1d08c280 2021-03-06 op len -= COLS-1;
984 1d08c280 2021-03-06 op off = COLS-1;
985 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
989 ede1f142 2021-03-07 op if (len != 0)
990 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
996 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
998 1d08c280 2021-03-06 op struct line *l;
1000 1d08c280 2021-03-06 op empty_vlist(tab);
1002 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1003 5e11c00c 2021-03-02 op switch (l->type) {
1004 5e11c00c 2021-03-02 op case LINE_TEXT:
1005 1d08c280 2021-03-06 op wrap_text(tab, "", l);
1007 5e11c00c 2021-03-02 op case LINE_LINK:
1008 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
1010 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1011 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
1013 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1014 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
1016 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1017 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
1019 5e11c00c 2021-03-02 op case LINE_ITEM:
1020 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
1022 5e11c00c 2021-03-02 op case LINE_QUOTE:
1023 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
1025 5e11c00c 2021-03-02 op case LINE_PRE_START:
1026 5e11c00c 2021-03-02 op case LINE_PRE_END:
1027 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
1029 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1030 1d08c280 2021-03-06 op hardwrap_text(tab, l);
1037 1d08c280 2021-03-06 op static inline void
1038 1d08c280 2021-03-06 op print_line(struct line *l)
1040 bd9637e9 2021-03-06 op const char *text = l->line;
1042 bd9637e9 2021-03-06 op if (text == NULL)
1045 1d08c280 2021-03-06 op switch (l->type) {
1046 1d08c280 2021-03-06 op case LINE_TEXT:
1047 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1049 1d08c280 2021-03-06 op case LINE_LINK:
1050 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
1051 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
1052 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
1054 1d08c280 2021-03-06 op case LINE_TITLE_1:
1055 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1056 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
1057 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1059 1d08c280 2021-03-06 op case LINE_TITLE_2:
1060 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1061 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
1062 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1064 bd9637e9 2021-03-06 op case LINE_TITLE_3:
1065 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1066 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
1067 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1069 bd9637e9 2021-03-06 op case LINE_ITEM:
1070 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
1072 1d08c280 2021-03-06 op case LINE_QUOTE:
1073 bd9637e9 2021-03-06 op wattron(body, A_DIM);
1074 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
1075 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
1077 1d08c280 2021-03-06 op case LINE_PRE_START:
1078 1d08c280 2021-03-06 op case LINE_PRE_END:
1079 48e9d457 2021-03-06 op wprintw(body, "```");
1081 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
1082 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1088 8af5e5ed 2021-03-08 op redraw_tabline(void)
1090 7c7d7bb7 2021-03-10 op struct tab *tab;
1091 7c7d7bb7 2021-03-10 op int current;
1093 8af5e5ed 2021-03-08 op wclear(tabline);
1094 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1096 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1097 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1098 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1099 7c7d7bb7 2021-03-10 op if (current) {
1100 7c7d7bb7 2021-03-10 op wattroff(tabline, A_REVERSE);
1101 7c7d7bb7 2021-03-10 op wattron(tabline, A_STANDOUT);
1104 7c7d7bb7 2021-03-10 op wprintw(tabline, " %d:todo title ",
1107 7c7d7bb7 2021-03-10 op if (current) {
1108 7c7d7bb7 2021-03-10 op wattron(tabline, A_REVERSE);
1109 7c7d7bb7 2021-03-10 op wattroff(tabline, A_STANDOUT);
1115 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1117 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1118 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
1119 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1121 48e9d457 2021-03-06 op wclear(modeline);
1122 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1123 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1125 8af5e5ed 2021-03-08 op wprintw(modeline, "-%c %s %s ",
1126 4d3785b1 2021-03-09 op spin[tab->s->loading_anim_step], mode, tab->urlstr);
1127 48e9d457 2021-03-06 op getyx(modeline, y, x);
1128 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1131 48e9d457 2021-03-06 op (void)max_y;
1133 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1134 48e9d457 2021-03-06 op waddstr(modeline, "-");
1138 9ca15951 2021-03-09 op redraw_minibuffer(void)
1140 9cb0f9ce 2021-03-10 op size_t skip = 0, off = 0;
1142 9ca15951 2021-03-09 op wclear(minibuf);
1143 9ca15951 2021-03-09 op if (!in_minibuffer)
1144 9cb0f9ce 2021-03-10 op goto message;
1146 9ca15951 2021-03-09 op off = strlen(ministate.prompt);
1148 9cb0f9ce 2021-03-10 op while (ministate.off - skip > COLS / 2) {
1149 9cb0f9ce 2021-03-10 op skip += MIN(ministate.off/4, 1);
1152 9ca15951 2021-03-09 op mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1153 9cb0f9ce 2021-03-10 op ministate.buf + skip);
1156 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1157 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1158 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1160 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1163 9cb0f9ce 2021-03-10 op wmove(minibuf, 0, off + ministate.off - skip);
1167 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1169 48e9d457 2021-03-06 op struct line *l;
1172 48e9d457 2021-03-06 op werase(body);
1174 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1175 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1179 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1180 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1181 48e9d457 2021-03-06 op wmove(body, line, 0);
1182 1d08c280 2021-03-06 op print_line(l);
1184 48e9d457 2021-03-06 op if (line == body_lines)
1188 8af5e5ed 2021-03-08 op redraw_tabline();
1189 48e9d457 2021-03-06 op redraw_modeline(tab);
1190 9ca15951 2021-03-09 op redraw_minibuffer();
1192 48e9d457 2021-03-06 op restore_cursor(tab);
1193 48e9d457 2021-03-06 op wrefresh(tabline);
1194 48e9d457 2021-03-06 op wrefresh(modeline);
1196 9ca15951 2021-03-09 op if (in_minibuffer) {
1197 9ca15951 2021-03-09 op wrefresh(body);
1198 9ca15951 2021-03-09 op wrefresh(minibuf);
1200 9ca15951 2021-03-09 op wrefresh(minibuf);
1201 9ca15951 2021-03-09 op wrefresh(body);
1206 7953dd72 2021-03-07 op message(const char *fmt, ...)
1210 7953dd72 2021-03-07 op if (clminibufev_set)
1211 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1212 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1213 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1214 7953dd72 2021-03-07 op clminibufev_set = 1;
1216 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1217 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1218 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1219 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1222 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1224 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1225 9cb0f9ce 2021-03-10 op wrefresh(body);
1226 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1228 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1229 9cb0f9ce 2021-03-10 op wrefresh(body);
1234 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1236 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1238 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1239 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1240 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1244 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1246 8af5e5ed 2021-03-08 op struct tab *tab = d;
1248 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1250 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1251 8af5e5ed 2021-03-08 op wrefresh(modeline);
1253 8af5e5ed 2021-03-08 op wrefresh(body);
1254 9ca15951 2021-03-09 op if (in_minibuffer)
1255 9ca15951 2021-03-09 op wrefresh(minibuf);
1257 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1261 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1263 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1265 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1266 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1267 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1269 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1271 43a1b8d0 2021-03-09 op wrefresh(modeline);
1272 43a1b8d0 2021-03-09 op wrefresh(body);
1273 9ca15951 2021-03-09 op if (in_minibuffer)
1274 9ca15951 2021-03-09 op wrefresh(minibuf);
1278 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1280 43a1b8d0 2021-03-09 op empty_vlist(tab);
1281 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1282 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1283 8af5e5ed 2021-03-08 op load_url(tab, url);
1285 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1286 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1287 43a1b8d0 2021-03-09 op redraw_tab(tab);
1291 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1292 b360ebb3 2021-03-10 op void (*abortfn)(void))
1294 9ca15951 2021-03-09 op in_minibuffer = 1;
1295 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1296 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1298 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1300 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1301 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1302 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1303 9ca15951 2021-03-09 op ministate.off = 0;
1304 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1308 b360ebb3 2021-03-10 op exit_minibuffer(void)
1310 9ca15951 2021-03-09 op wclear(minibuf);
1312 9ca15951 2021-03-09 op in_minibuffer = 0;
1313 9ca15951 2021-03-09 op base_map = &global_map;
1314 9ca15951 2021-03-09 op current_map = &global_map;
1318 bcb0b073 2021-03-07 op new_tab(void)
1320 bcb0b073 2021-03-07 op struct tab *tab, *t;
1321 bcb0b073 2021-03-07 op const char *url = "about:new";
1323 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1326 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1329 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1330 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
1331 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
1334 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1335 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
1337 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1338 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1340 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1342 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1346 bcb0b073 2021-03-07 op event_loopbreak();
1350 5e11c00c 2021-03-02 op ui_init(void)
1352 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1354 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1355 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1357 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1359 9ca15951 2021-03-09 op base_map = &global_map;
1360 f832146f 2021-03-09 op current_map = &global_map;
1361 f832146f 2021-03-09 op load_default_keys();
1368 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1370 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1372 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1374 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1376 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1379 48e9d457 2021-03-06 op body_lines = LINES-3;
1380 48e9d457 2021-03-06 op body_cols = COLS;
1382 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1383 48e9d457 2021-03-06 op scrollok(body, TRUE);
1385 5e11c00c 2021-03-02 op /* non-blocking input */
1386 48e9d457 2021-03-06 op wtimeout(body, 0);
1388 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1390 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1391 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1393 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1394 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1402 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1404 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1405 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1409 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1411 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1414 1d08c280 2021-03-06 op wrap_page(tab);
1415 5e11c00c 2021-03-02 op redraw_tab(tab);
1419 5e11c00c 2021-03-02 op ui_end(void)