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 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
92 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
93 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
94 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
95 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
96 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
97 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
98 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
99 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
100 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
101 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
102 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
103 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
104 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
105 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
106 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
107 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
108 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
109 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
111 9ca15951 2021-03-09 op static void global_key_unbound(void);
113 9ca15951 2021-03-09 op static void cmd_mini_del(struct tab*);
114 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
115 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
116 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
117 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
118 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
119 b360ebb3 2021-03-10 op static void cmd_mini_abort();
120 b360ebb3 2021-03-10 op static void cmd_mini_complete_and_exit();
122 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
123 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
124 b360ebb3 2021-03-10 op static void eecmd_select(void);
125 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
126 5cd2ebb1 2021-03-11 op static void ir_select(void);
127 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
128 5cd2ebb1 2021-03-11 op static void lu_select(void);
130 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
131 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
132 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
133 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
134 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
135 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
136 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
137 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
138 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
139 48e9d457 2021-03-06 op static void print_line(struct line*);
140 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
141 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
142 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
143 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
144 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
145 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
146 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
147 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
148 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
149 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
150 b360ebb3 2021-03-10 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void));
151 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
152 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
153 bcb0b073 2021-03-07 op static void new_tab(void);
155 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
157 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
158 48e9d457 2021-03-06 op static int body_lines, body_cols;
160 48e9d457 2021-03-06 op static struct event clminibufev;
161 48e9d457 2021-03-06 op static int clminibufev_set;
162 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
163 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
165 bcb0b073 2021-03-07 op static uint32_t tab_counter;
167 1d08c280 2021-03-06 op struct ui_state {
170 48e9d457 2021-03-06 op size_t line_off;
171 48e9d457 2021-03-06 op size_t line_max;
173 8af5e5ed 2021-03-08 op short loading_anim;
174 8af5e5ed 2021-03-08 op short loading_anim_step;
175 8af5e5ed 2021-03-08 op struct event loadingev;
177 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
180 7c7d7bb7 2021-03-10 op static char keybuf[64];
182 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
184 a777f81f 2021-03-10 op struct keytable {
187 a777f81f 2021-03-10 op } keytable[] = {
188 a777f81f 2021-03-10 op { "<up>", KEY_UP },
189 a777f81f 2021-03-10 op { "<down>", KEY_DOWN },
190 a777f81f 2021-03-10 op { "<left>", KEY_LEFT },
191 a777f81f 2021-03-10 op { "<right>", KEY_RIGHT },
192 a777f81f 2021-03-10 op { "<prior>", KEY_PPAGE },
193 a777f81f 2021-03-10 op { "<next>", KEY_NPAGE },
194 a777f81f 2021-03-10 op { "<home>", KEY_HOME },
195 a777f81f 2021-03-10 op { "<end>", KEY_END },
197 a777f81f 2021-03-10 op { "del", KEY_BACKSPACE },
198 a777f81f 2021-03-10 op { "esc", 27 },
199 a777f81f 2021-03-10 op { "space", ' ' },
200 a777f81f 2021-03-10 op { "spc", ' ' },
201 a777f81f 2021-03-10 op { "enter", CTRL('m') },
202 a777f81f 2021-03-10 op { "ret", CTRL('m' )},
203 a777f81f 2021-03-10 op { "tab", CTRL('i') },
208 9ca15951 2021-03-09 op struct kmap {
209 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
210 9ca15951 2021-03-09 op void (*unhandled_input)(void);
213 9ca15951 2021-03-09 op struct kmap global_map,
214 fa3fd864 2021-03-10 op minibuffer_map,
215 9ca15951 2021-03-09 op *current_map,
218 f832146f 2021-03-09 op struct keymap {
221 f832146f 2021-03-09 op struct kmap map;
222 f832146f 2021-03-09 op void (*fn)(struct tab*);
224 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
227 9ca15951 2021-03-09 op static int in_minibuffer;
229 9ca15951 2021-03-09 op static struct {
230 9cb0f9ce 2021-03-10 op char *curmesg;
232 040fbdf8 2021-03-10 op char buf[1025];
233 9ca15951 2021-03-09 op size_t off, len;
234 5cd2ebb1 2021-03-11 op char prompt[32];
235 b360ebb3 2021-03-10 op void (*donefn)(void);
236 b360ebb3 2021-03-10 op void (*abortfn)(void);
240 f832146f 2021-03-09 op kbd(const char *key)
242 a777f81f 2021-03-10 op struct keytable *t;
244 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
245 f832146f 2021-03-09 op if (has_prefix(key, t->p))
252 a777f81f 2021-03-10 op static const char *
255 a777f81f 2021-03-10 op struct keytable *t;
257 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
258 a777f81f 2021-03-10 op if (k == t->k)
266 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
268 f832146f 2021-03-09 op int ctrl, meta, k;
269 f832146f 2021-03-09 op struct keymap *entry;
272 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
274 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
276 f832146f 2021-03-09 op if (*key == '\0')
278 f832146f 2021-03-09 op k = kbd(key);
283 f832146f 2021-03-09 op /* skip key & spaces */
284 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
286 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
289 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
290 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
291 f832146f 2021-03-09 op if (*key == '\0') {
292 f832146f 2021-03-09 op entry->fn = fn;
295 f832146f 2021-03-09 op map = &entry->map;
300 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
303 f832146f 2021-03-09 op entry->meta = meta;
304 f832146f 2021-03-09 op entry->key = k;
305 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
307 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
308 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
310 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
312 f832146f 2021-03-09 op if (*key != '\0') {
313 f832146f 2021-03-09 op map = &entry->map;
317 f832146f 2021-03-09 op entry->fn = fn;
320 f832146f 2021-03-09 op static inline void
321 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
323 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
326 9ca15951 2021-03-09 op static inline void
327 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
329 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
333 f832146f 2021-03-09 op load_default_keys(void)
335 9ca15951 2021-03-09 op /* === global map === */
338 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
339 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
340 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
341 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
342 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
343 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
345 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
346 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
348 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
350 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
352 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
353 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
354 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
356 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
357 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
358 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
359 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
361 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
362 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
364 f832146f 2021-03-09 op /* vi/vi-like */
365 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
366 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
367 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
368 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
369 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
370 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
372 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
373 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
375 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
376 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
379 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
381 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
383 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
386 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
387 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
388 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
389 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
390 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
391 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
393 f832146f 2021-03-09 op /* "ncurses standard" */
394 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
397 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
399 fa3fd864 2021-03-10 op /* === minibuffer map === */
400 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
401 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
402 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
403 fa3fd864 2021-03-10 op minibuffer_set_key("del", cmd_mini_del);
405 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
406 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
407 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
408 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
409 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
410 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
411 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
415 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
417 1d08c280 2021-03-06 op struct line *vl;
419 48e9d457 2021-03-06 op tab->s->line_max++;
421 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
424 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
429 1d08c280 2021-03-06 op vl->type = l->type;
430 1d08c280 2021-03-06 op if (len != 0)
431 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
432 1d08c280 2021-03-06 op vl->alt = l->alt;
434 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
435 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
437 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
442 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
444 1d08c280 2021-03-06 op struct line *l, *t;
446 48e9d457 2021-03-06 op tab->s->line_max = 0;
448 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
449 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
450 1d08c280 2021-03-06 op free(l->line);
451 1d08c280 2021-03-06 op /* l->alt references the original line! */
457 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
459 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
463 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
465 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
466 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
467 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
470 48e9d457 2021-03-06 op restore_cursor(tab);
474 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
476 fed61466 2021-03-11 op if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
479 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
480 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
481 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
484 48e9d457 2021-03-06 op restore_cursor(tab);
488 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
490 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
491 48e9d457 2021-03-06 op restore_cursor(tab);
495 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
497 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
498 a329982b 2021-03-11 op restore_cursor(tab);
502 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
504 a329982b 2021-03-11 op tab->s->curs_x = 0;
505 a329982b 2021-03-11 op restore_cursor(tab);
509 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
511 a329982b 2021-03-11 op struct line *line;
514 a329982b 2021-03-11 op off = tab->s->line_off + tab->s->curs_y;
515 a329982b 2021-03-11 op if (off >= tab->s->line_max) {
516 a329982b 2021-03-11 op tab->s->curs_x = 0;
520 a329982b 2021-03-11 op line = nth_line(tab, off);
521 a329982b 2021-03-11 op if (line->line != NULL)
522 a329982b 2021-03-11 op tab->s->curs_x = strlen(line->line);
524 a329982b 2021-03-11 op tab->s->curs_x = 0;
526 a329982b 2021-03-11 op /* TODO: don't hardcode these numbers here. */
527 a329982b 2021-03-11 op switch (line->type) {
528 a329982b 2021-03-11 op case LINE_PRE_START:
529 a329982b 2021-03-11 op case LINE_PRE_END:
530 a329982b 2021-03-11 op tab->s->curs_x += 3;
532 a329982b 2021-03-11 op case LINE_LINK:
533 a329982b 2021-03-11 op tab->s->curs_x += 3;
535 a329982b 2021-03-11 op case LINE_QUOTE:
536 a329982b 2021-03-11 op case LINE_ITEM:
537 a329982b 2021-03-11 op tab->s->curs_x += 2;
544 48e9d457 2021-03-06 op restore_cursor(tab);
548 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
550 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
554 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
556 48e9d457 2021-03-06 op struct line *l;
558 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
561 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
562 48e9d457 2021-03-06 op wscrl(body, -1);
563 48e9d457 2021-03-06 op wmove(body, 0, 0);
564 48e9d457 2021-03-06 op print_line(l);
568 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
570 48e9d457 2021-03-06 op struct line *l;
573 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
576 48e9d457 2021-03-06 op tab->s->line_off++;
577 48e9d457 2021-03-06 op wscrl(body, 1);
579 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
582 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
583 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
584 48e9d457 2021-03-06 op print_line(l);
588 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
592 ed44414d 2021-03-09 op off = body_lines+1;
594 ed44414d 2021-03-09 op for (; off > 0; --off)
595 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
599 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
603 ed44414d 2021-03-09 op off = tab->s->line_off + body_lines;
604 ed44414d 2021-03-09 op off = MIN(tab->s->line_max, off);
606 ed44414d 2021-03-09 op for (; off >= 0; --off)
607 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
611 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
613 e19f9a04 2021-03-11 op tab->s->line_off = 0;
614 e19f9a04 2021-03-11 op tab->s->curs_y = 0;
615 e19f9a04 2021-03-11 op redraw_body(tab);
619 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
623 e19f9a04 2021-03-11 op off = tab->s->line_max - body_lines;
624 e19f9a04 2021-03-11 op off = MAX(0, off);
626 e19f9a04 2021-03-11 op tab->s->line_off = off;
627 e19f9a04 2021-03-11 op tab->s->curs_y = MIN(body_lines, tab->s->line_max);
629 e19f9a04 2021-03-11 op redraw_body(tab);
633 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
635 1d08c280 2021-03-06 op event_loopbreak();
639 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
641 2a4ad912 2021-03-08 op struct line *l;
644 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
645 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
647 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
648 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
651 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
655 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
657 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
661 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
665 b360ebb3 2021-03-10 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer);
667 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
668 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
670 9ca15951 2021-03-09 op if (thiskey.meta)
671 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
673 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
674 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
678 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
680 7c7d7bb7 2021-03-10 op struct tab *t;
682 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
683 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
684 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
688 7c7d7bb7 2021-03-10 op stop_tab(tab);
690 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
691 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
693 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
695 7c7d7bb7 2021-03-10 op free(tab->s);
700 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
706 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
708 7c7d7bb7 2021-03-10 op struct tab *t;
710 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
712 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
713 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
714 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
718 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
720 7c7d7bb7 2021-03-10 op struct tab *t;
722 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
724 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
725 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
726 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
730 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
732 5cd2ebb1 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer);
733 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
737 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
739 5cd2ebb1 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer);
740 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
741 5cd2ebb1 2021-03-11 op strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
742 5cd2ebb1 2021-03-11 op ministate.off = strlen(tab->urlstr);
743 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
747 7c7d7bb7 2021-03-10 op global_key_unbound(void)
749 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
753 9ca15951 2021-03-09 op cmd_mini_del(struct tab *tab)
755 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
758 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
759 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
760 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
761 9ca15951 2021-03-09 op ministate.off--;
762 9ca15951 2021-03-09 op ministate.len--;
766 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
768 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
770 9ca15951 2021-03-09 op ministate.off++;
774 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
776 9ca15951 2021-03-09 op if (ministate.off == 0)
778 9ca15951 2021-03-09 op ministate.off--;
782 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
784 9ca15951 2021-03-09 op ministate.off = ministate.len;
788 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
790 9ca15951 2021-03-09 op ministate.off = 0;
794 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
796 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
798 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
799 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
803 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
805 b360ebb3 2021-03-10 op ministate.abortfn();
809 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
811 b360ebb3 2021-03-10 op ministate.donefn();
815 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
817 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
820 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
822 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
823 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
824 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
825 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
826 9ca15951 2021-03-09 op ministate.off++;
827 9ca15951 2021-03-09 op ministate.len++;
831 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
833 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
834 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
835 5cd2ebb1 2021-03-11 op global_key_unbound();
839 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
843 b360ebb3 2021-03-10 op eecmd_select(void)
845 b360ebb3 2021-03-10 op exit_minibuffer();
846 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
850 5cd2ebb1 2021-03-11 op ir_self_insert(void)
852 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
856 5cd2ebb1 2021-03-11 op ir_select(void)
858 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
859 5cd2ebb1 2021-03-11 op struct url url;
860 5cd2ebb1 2021-03-11 op struct tab *tab;
862 5cd2ebb1 2021-03-11 op tab = current_tab();
864 5cd2ebb1 2021-03-11 op exit_minibuffer();
866 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
867 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
868 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
869 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
870 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
874 5cd2ebb1 2021-03-11 op lu_self_insert(void)
876 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
877 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
878 5cd2ebb1 2021-03-11 op global_key_unbound();
882 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
886 5cd2ebb1 2021-03-11 op lu_select(void)
888 5cd2ebb1 2021-03-11 op exit_minibuffer();
889 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
892 48e9d457 2021-03-06 op static struct line *
893 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
895 48e9d457 2021-03-06 op struct line *l;
899 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
905 48e9d457 2021-03-06 op /* unreachable */
909 5e11c00c 2021-03-02 op static struct tab *
910 5e11c00c 2021-03-02 op current_tab(void)
912 5e11c00c 2021-03-02 op struct tab *t;
914 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
915 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
919 5e11c00c 2021-03-02 op /* unreachable */
924 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
926 481340cc 2021-03-11 op struct tab *tab;
927 f832146f 2021-03-09 op struct keymap *k;
928 7c7d7bb7 2021-03-10 op const char *keyname;
929 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
931 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
932 9ca15951 2021-03-09 op if (thiskey.key == ERR)
934 9ca15951 2021-03-09 op if (thiskey.key == 27) {
935 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
937 9ca15951 2021-03-09 op thiskey.meta = 1;
938 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
939 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
940 c314a314 2021-03-11 op thiskey.meta = 0;
941 9ca15951 2021-03-09 op thiskey.key = 27;
944 9ca15951 2021-03-09 op thiskey.meta = 0;
946 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
947 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
948 7c7d7bb7 2021-03-10 op if (thiskey.meta)
949 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
950 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
951 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
953 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
954 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
957 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
958 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
959 9ca15951 2021-03-09 op k->key == thiskey.key) {
960 f832146f 2021-03-09 op if (k->fn == NULL)
961 f832146f 2021-03-09 op current_map = &k->map;
963 9ca15951 2021-03-09 op current_map = base_map;
964 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
965 f832146f 2021-03-09 op k->fn(current_tab());
971 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
972 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
974 7c7d7bb7 2021-03-10 op global_key_unbound();
977 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
978 9ca15951 2021-03-09 op current_map = base_map;
981 481340cc 2021-03-11 op tab = current_tab();
982 7c7d7bb7 2021-03-10 op redraw_tabline();
983 481340cc 2021-03-11 op redraw_modeline(tab);
984 9ca15951 2021-03-09 op redraw_minibuffer();
985 481340cc 2021-03-11 op restore_cursor(tab);
986 48e9d457 2021-03-06 op wrefresh(tabline);
987 48e9d457 2021-03-06 op wrefresh(modeline);
989 9ca15951 2021-03-09 op if (in_minibuffer) {
990 9ca15951 2021-03-09 op wrefresh(body);
991 9ca15951 2021-03-09 op wrefresh(minibuf);
993 9ca15951 2021-03-09 op wrefresh(minibuf);
994 9ca15951 2021-03-09 op wrefresh(body);
999 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1001 a6d450c1 2021-03-06 op clminibufev_set = 0;
1003 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1004 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1006 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1007 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1008 9cb0f9ce 2021-03-10 op wrefresh(body);
1009 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1011 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1012 9cb0f9ce 2021-03-10 op wrefresh(body);
1017 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1019 1d08c280 2021-03-06 op struct tab *tab;
1025 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1027 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1028 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1030 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1031 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1033 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1034 48e9d457 2021-03-06 op body_lines = LINES-3;
1035 bd9637e9 2021-03-06 op body_cols = COLS;
1037 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1039 1d08c280 2021-03-06 op tab = current_tab();
1041 1d08c280 2021-03-06 op wrap_page(tab);
1042 1d08c280 2021-03-06 op redraw_tab(tab);
1046 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
1047 eb259e66 2021-03-02 op * and the end of the separator after the word.
1050 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1052 eb259e66 2021-03-02 op *endword = s;
1053 eb259e66 2021-03-02 op *endword = s;
1055 eb259e66 2021-03-02 op if (*s == '\0')
1058 eb259e66 2021-03-02 op /* find the end of the current world */
1059 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1060 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
1064 eb259e66 2021-03-02 op *endword = s;
1066 eb259e66 2021-03-02 op /* find the end of the separator */
1067 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1068 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
1072 eb259e66 2021-03-02 op *endspc = s;
1077 1d08c280 2021-03-06 op static inline int
1078 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1079 1d08c280 2021-03-06 op const char **line)
1081 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
1083 1d08c280 2021-03-06 op *line += *off - zero;
1084 eb259e66 2021-03-02 op *off = zero;
1088 eb259e66 2021-03-02 op static inline void
1089 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
1093 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1094 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
1095 eb259e66 2021-03-02 op addch((*s)[i]);
1096 eb259e66 2021-03-02 op *off += len;
1101 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
1102 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
1104 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
1107 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
1109 eb259e66 2021-03-02 op size_t zero, off, len, split;
1110 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
1112 1d08c280 2021-03-06 op zero = strlen(prfx);
1114 1d08c280 2021-03-06 op line = l->line;
1115 1d08c280 2021-03-06 op linestart = l->line;
1117 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
1118 eb259e66 2021-03-02 op len = endword - line;
1119 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1120 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1121 48e9d457 2021-03-06 op while (len >= body_cols) {
1122 eb259e66 2021-03-02 op /* hard wrap */
1123 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1124 48e9d457 2021-03-06 op len -= body_cols-1;
1125 48e9d457 2021-03-06 op line += body_cols-1;
1128 eb259e66 2021-03-02 op if (len != 0)
1133 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
1134 eb259e66 2021-03-02 op len = endspc - endword;
1135 eb259e66 2021-03-02 op /* line = endspc; */
1136 eb259e66 2021-03-02 op if (off != zero) {
1137 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1138 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1139 1d08c280 2021-03-06 op linestart = endspc;
1144 eb259e66 2021-03-02 op line = endspc;
1147 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1151 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
1153 1d08c280 2021-03-06 op size_t off, len;
1154 1d08c280 2021-03-06 op const char *linestart;
1156 1d08c280 2021-03-06 op len = strlen(l->line);
1158 1d08c280 2021-03-06 op linestart = l->line;
1160 1d08c280 2021-03-06 op while (len >= COLS) {
1161 1d08c280 2021-03-06 op len -= COLS-1;
1162 1d08c280 2021-03-06 op off = COLS-1;
1163 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
1167 ede1f142 2021-03-07 op if (len != 0)
1168 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
1174 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1176 1d08c280 2021-03-06 op struct line *l;
1178 1d08c280 2021-03-06 op empty_vlist(tab);
1180 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1181 5e11c00c 2021-03-02 op switch (l->type) {
1182 5e11c00c 2021-03-02 op case LINE_TEXT:
1183 1d08c280 2021-03-06 op wrap_text(tab, "", l);
1185 5e11c00c 2021-03-02 op case LINE_LINK:
1186 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
1188 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1189 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
1191 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1192 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
1194 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1195 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
1197 5e11c00c 2021-03-02 op case LINE_ITEM:
1198 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
1200 5e11c00c 2021-03-02 op case LINE_QUOTE:
1201 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
1203 5e11c00c 2021-03-02 op case LINE_PRE_START:
1204 5e11c00c 2021-03-02 op case LINE_PRE_END:
1205 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
1207 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1208 1d08c280 2021-03-06 op hardwrap_text(tab, l);
1215 1d08c280 2021-03-06 op static inline void
1216 1d08c280 2021-03-06 op print_line(struct line *l)
1218 bd9637e9 2021-03-06 op const char *text = l->line;
1220 bd9637e9 2021-03-06 op if (text == NULL)
1223 1d08c280 2021-03-06 op switch (l->type) {
1224 1d08c280 2021-03-06 op case LINE_TEXT:
1225 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1227 1d08c280 2021-03-06 op case LINE_LINK:
1228 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
1229 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
1230 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
1232 1d08c280 2021-03-06 op case LINE_TITLE_1:
1233 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1234 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
1235 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1237 1d08c280 2021-03-06 op case LINE_TITLE_2:
1238 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1239 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
1240 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1242 bd9637e9 2021-03-06 op case LINE_TITLE_3:
1243 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
1244 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
1245 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
1247 bd9637e9 2021-03-06 op case LINE_ITEM:
1248 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
1250 1d08c280 2021-03-06 op case LINE_QUOTE:
1251 bd9637e9 2021-03-06 op wattron(body, A_DIM);
1252 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
1253 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
1255 1d08c280 2021-03-06 op case LINE_PRE_START:
1256 1d08c280 2021-03-06 op case LINE_PRE_END:
1257 48e9d457 2021-03-06 op wprintw(body, "```");
1259 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
1260 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
1266 8af5e5ed 2021-03-08 op redraw_tabline(void)
1268 7c7d7bb7 2021-03-10 op struct tab *tab;
1269 7c7d7bb7 2021-03-10 op int current;
1271 8af5e5ed 2021-03-08 op wclear(tabline);
1272 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1274 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1275 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1276 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1278 a329982b 2021-03-11 op if (current)
1279 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1281 a329982b 2021-03-11 op wprintw(tabline, "%s%d:todo title ",
1282 a329982b 2021-03-11 op current ? "*" : " ", tab->id);
1284 a329982b 2021-03-11 op if (current)
1285 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1290 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1293 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1294 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
1295 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1297 48e9d457 2021-03-06 op wclear(modeline);
1298 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1299 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1301 481340cc 2021-03-11 op wprintw(modeline, "-%c %s ",
1302 481340cc 2021-03-11 op spin[tab->s->loading_anim_step], mode);
1304 481340cc 2021-03-11 op pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1306 481340cc 2021-03-11 op if (tab->s->line_max <= body_lines)
1307 481340cc 2021-03-11 op wprintw(modeline, "All ");
1308 481340cc 2021-03-11 op else if (tab->s->line_off == 0)
1309 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1310 481340cc 2021-03-11 op else if (tab->s->line_off + body_lines >= tab->s->line_max)
1311 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1313 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1315 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1316 481340cc 2021-03-11 op tab->s->line_off + tab->s->curs_y,
1317 481340cc 2021-03-11 op tab->s->line_max,
1318 481340cc 2021-03-11 op tab->urlstr);
1320 48e9d457 2021-03-06 op getyx(modeline, y, x);
1321 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1324 48e9d457 2021-03-06 op (void)max_y;
1326 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1327 48e9d457 2021-03-06 op waddstr(modeline, "-");
1331 9ca15951 2021-03-09 op redraw_minibuffer(void)
1333 9cb0f9ce 2021-03-10 op size_t skip = 0, off = 0;
1335 9ca15951 2021-03-09 op wclear(minibuf);
1336 9ca15951 2021-03-09 op if (!in_minibuffer)
1337 9cb0f9ce 2021-03-10 op goto message;
1339 9ca15951 2021-03-09 op off = strlen(ministate.prompt);
1341 9cb0f9ce 2021-03-10 op while (ministate.off - skip > COLS / 2) {
1342 9cb0f9ce 2021-03-10 op skip += MIN(ministate.off/4, 1);
1345 9ca15951 2021-03-09 op mvwprintw(minibuf, 0, 0, "%s%s", ministate.prompt,
1346 9cb0f9ce 2021-03-10 op ministate.buf + skip);
1349 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1350 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1351 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1353 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1356 9cb0f9ce 2021-03-10 op wmove(minibuf, 0, off + ministate.off - skip);
1360 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1362 e19f9a04 2021-03-11 op redraw_tabline();
1363 e19f9a04 2021-03-11 op redraw_body(tab);
1364 e19f9a04 2021-03-11 op redraw_modeline(tab);
1365 e19f9a04 2021-03-11 op redraw_minibuffer();
1367 e19f9a04 2021-03-11 op restore_cursor(tab);
1368 e19f9a04 2021-03-11 op wrefresh(tabline);
1369 e19f9a04 2021-03-11 op wrefresh(modeline);
1371 e19f9a04 2021-03-11 op if (in_minibuffer) {
1372 e19f9a04 2021-03-11 op wrefresh(body);
1373 e19f9a04 2021-03-11 op wrefresh(minibuf);
1375 e19f9a04 2021-03-11 op wrefresh(minibuf);
1376 e19f9a04 2021-03-11 op wrefresh(body);
1381 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1383 48e9d457 2021-03-06 op struct line *l;
1386 48e9d457 2021-03-06 op werase(body);
1388 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1389 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1393 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1394 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1395 48e9d457 2021-03-06 op wmove(body, line, 0);
1396 1d08c280 2021-03-06 op print_line(l);
1398 48e9d457 2021-03-06 op if (line == body_lines)
1404 7953dd72 2021-03-07 op message(const char *fmt, ...)
1408 7953dd72 2021-03-07 op if (clminibufev_set)
1409 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1410 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1411 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1412 7953dd72 2021-03-07 op clminibufev_set = 1;
1414 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1415 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1416 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1417 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1420 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1422 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1423 9cb0f9ce 2021-03-10 op wrefresh(body);
1424 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1426 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1427 9cb0f9ce 2021-03-10 op wrefresh(body);
1432 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1434 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1436 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1437 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1438 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1442 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1444 8af5e5ed 2021-03-08 op struct tab *tab = d;
1446 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1448 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1449 8af5e5ed 2021-03-08 op wrefresh(modeline);
1451 8af5e5ed 2021-03-08 op wrefresh(body);
1452 9ca15951 2021-03-09 op if (in_minibuffer)
1453 9ca15951 2021-03-09 op wrefresh(minibuf);
1455 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1459 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1461 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1463 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1464 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1465 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1467 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1469 43a1b8d0 2021-03-09 op wrefresh(modeline);
1470 43a1b8d0 2021-03-09 op wrefresh(body);
1471 9ca15951 2021-03-09 op if (in_minibuffer)
1472 9ca15951 2021-03-09 op wrefresh(minibuf);
1476 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1478 43a1b8d0 2021-03-09 op empty_vlist(tab);
1479 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1480 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1481 8af5e5ed 2021-03-08 op load_url(tab, url);
1483 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1484 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1485 43a1b8d0 2021-03-09 op redraw_tab(tab);
1489 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1490 b360ebb3 2021-03-10 op void (*abortfn)(void))
1492 9ca15951 2021-03-09 op in_minibuffer = 1;
1493 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1494 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1496 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1498 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1499 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1500 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1501 9ca15951 2021-03-09 op ministate.off = 0;
1502 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1506 b360ebb3 2021-03-10 op exit_minibuffer(void)
1508 9ca15951 2021-03-09 op wclear(minibuf);
1510 9ca15951 2021-03-09 op in_minibuffer = 0;
1511 9ca15951 2021-03-09 op base_map = &global_map;
1512 9ca15951 2021-03-09 op current_map = &global_map;
1516 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1518 5cd2ebb1 2021-03-11 op struct tab *t;
1520 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1521 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1524 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1528 bcb0b073 2021-03-07 op new_tab(void)
1530 bcb0b073 2021-03-07 op struct tab *tab, *t;
1531 bcb0b073 2021-03-07 op const char *url = "about:new";
1533 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1536 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1539 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1541 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1542 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1544 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1545 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1547 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1549 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1553 bcb0b073 2021-03-07 op event_loopbreak();
1557 5e11c00c 2021-03-02 op ui_init(void)
1559 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1561 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1562 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1564 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1566 9ca15951 2021-03-09 op base_map = &global_map;
1567 f832146f 2021-03-09 op current_map = &global_map;
1568 f832146f 2021-03-09 op load_default_keys();
1575 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1577 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1579 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1581 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1583 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1586 48e9d457 2021-03-06 op body_lines = LINES-3;
1587 48e9d457 2021-03-06 op body_cols = COLS;
1589 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1590 48e9d457 2021-03-06 op scrollok(body, TRUE);
1592 5e11c00c 2021-03-02 op /* non-blocking input */
1593 48e9d457 2021-03-06 op wtimeout(body, 0);
1595 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1597 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1598 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1600 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1601 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1609 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1611 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1612 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1616 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1618 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1621 1d08c280 2021-03-06 op wrap_page(tab);
1622 5e11c00c 2021-03-02 op redraw_tab(tab);
1626 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1628 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1629 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1631 5cd2ebb1 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer);
1632 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1633 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1634 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1638 5e11c00c 2021-03-02 op ui_end(void)