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))
77 22268e11 2021-03-11 op struct minibuf_histhead;
79 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
81 f832146f 2021-03-09 op static int kbd(const char*);
82 f832146f 2021-03-09 op static void kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
83 f832146f 2021-03-09 op static void load_default_keys(void);
84 1d08c280 2021-03-06 op static int push_line(struct tab*, const struct line*, const char*, size_t);
85 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
86 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
88 46a9311e 2021-03-08 op static void cmd_previous_line(struct tab*);
89 46a9311e 2021-03-08 op static void cmd_next_line(struct tab*);
90 46a9311e 2021-03-08 op static void cmd_forward_char(struct tab*);
91 46a9311e 2021-03-08 op static void cmd_backward_char(struct tab*);
92 a329982b 2021-03-11 op static void cmd_move_beginning_of_line(struct tab*);
93 a329982b 2021-03-11 op static void cmd_move_end_of_line(struct tab*);
94 46a9311e 2021-03-08 op static void cmd_redraw(struct tab*);
95 ed44414d 2021-03-09 op static void cmd_scroll_line_down(struct tab*);
96 ed44414d 2021-03-09 op static void cmd_scroll_line_up(struct tab*);
97 46a9311e 2021-03-08 op static void cmd_scroll_up(struct tab*);
98 ed44414d 2021-03-09 op static void cmd_scroll_down(struct tab*);
99 e19f9a04 2021-03-11 op static void cmd_beginning_of_buffer(struct tab*);
100 e19f9a04 2021-03-11 op static void cmd_end_of_buffer(struct tab*);
101 46a9311e 2021-03-08 op static void cmd_kill_telescope(struct tab*);
102 46a9311e 2021-03-08 op static void cmd_push_button(struct tab*);
103 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
104 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
105 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
106 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
107 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
108 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
109 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
110 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
112 9ca15951 2021-03-09 op static void global_key_unbound(void);
114 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
115 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
116 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
117 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
118 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
119 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
120 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
121 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
122 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
123 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
124 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
126 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
127 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
128 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
129 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
130 b360ebb3 2021-03-10 op static void eecmd_select(void);
131 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
132 5cd2ebb1 2021-03-11 op static void ir_select(void);
133 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
134 5cd2ebb1 2021-03-11 op static void lu_select(void);
136 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
137 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
138 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
139 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
140 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
141 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
142 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
143 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
144 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
145 48e9d457 2021-03-06 op static void print_line(struct line*);
146 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
147 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
148 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
149 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
150 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
151 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
152 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
153 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
154 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
155 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
156 22268e11 2021-03-11 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct minibuf_histhead*);
157 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
158 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
159 bcb0b073 2021-03-07 op static void new_tab(void);
161 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
163 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
164 48e9d457 2021-03-06 op static int body_lines, body_cols;
166 48e9d457 2021-03-06 op static struct event clminibufev;
167 48e9d457 2021-03-06 op static int clminibufev_set;
168 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
169 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
171 bcb0b073 2021-03-07 op static uint32_t tab_counter;
173 1d08c280 2021-03-06 op struct ui_state {
176 48e9d457 2021-03-06 op size_t line_off;
177 48e9d457 2021-03-06 op size_t line_max;
179 8af5e5ed 2021-03-08 op short loading_anim;
180 8af5e5ed 2021-03-08 op short loading_anim_step;
181 8af5e5ed 2021-03-08 op struct event loadingev;
183 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
186 7c7d7bb7 2021-03-10 op static char keybuf[64];
188 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
190 a777f81f 2021-03-10 op struct keytable {
193 a777f81f 2021-03-10 op } keytable[] = {
194 a777f81f 2021-03-10 op { "<up>", KEY_UP },
195 a777f81f 2021-03-10 op { "<down>", KEY_DOWN },
196 a777f81f 2021-03-10 op { "<left>", KEY_LEFT },
197 a777f81f 2021-03-10 op { "<right>", KEY_RIGHT },
198 a777f81f 2021-03-10 op { "<prior>", KEY_PPAGE },
199 a777f81f 2021-03-10 op { "<next>", KEY_NPAGE },
200 a777f81f 2021-03-10 op { "<home>", KEY_HOME },
201 a777f81f 2021-03-10 op { "<end>", KEY_END },
203 a777f81f 2021-03-10 op { "del", KEY_BACKSPACE },
204 a777f81f 2021-03-10 op { "esc", 27 },
205 a777f81f 2021-03-10 op { "space", ' ' },
206 a777f81f 2021-03-10 op { "spc", ' ' },
207 a777f81f 2021-03-10 op { "enter", CTRL('m') },
208 a777f81f 2021-03-10 op { "ret", CTRL('m' )},
209 a777f81f 2021-03-10 op { "tab", CTRL('i') },
214 9ca15951 2021-03-09 op struct kmap {
215 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
216 9ca15951 2021-03-09 op void (*unhandled_input)(void);
219 9ca15951 2021-03-09 op struct kmap global_map,
220 fa3fd864 2021-03-10 op minibuffer_map,
221 9ca15951 2021-03-09 op *current_map,
224 f832146f 2021-03-09 op struct keymap {
227 f832146f 2021-03-09 op struct kmap map;
228 f832146f 2021-03-09 op void (*fn)(struct tab*);
230 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
233 22268e11 2021-03-11 op /* TODO: limit to a maximum number of entries */
234 22268e11 2021-03-11 op struct minibuf_histhead {
235 22268e11 2021-03-11 op TAILQ_HEAD(mhisthead, minibuf_hist) head;
238 22268e11 2021-03-11 op struct minibuf_hist {
239 22268e11 2021-03-11 op char h[1025];
240 22268e11 2021-03-11 op TAILQ_ENTRY(minibuf_hist) entries;
243 22268e11 2021-03-11 op static struct minibuf_histhead eecmd_history,
247 9ca15951 2021-03-09 op static int in_minibuffer;
249 9ca15951 2021-03-09 op static struct {
250 9cb0f9ce 2021-03-10 op char *curmesg;
252 040fbdf8 2021-03-10 op char buf[1025];
253 9ca15951 2021-03-09 op size_t off, len;
254 5cd2ebb1 2021-03-11 op char prompt[32];
255 b360ebb3 2021-03-10 op void (*donefn)(void);
256 b360ebb3 2021-03-10 op void (*abortfn)(void);
258 22268e11 2021-03-11 op struct minibuf_histhead *history;
259 22268e11 2021-03-11 op struct minibuf_hist *hist_cur;
260 22268e11 2021-03-11 op size_t hist_off;
263 f95d82dd 2021-03-11 op struct lineprefix {
264 f95d82dd 2021-03-11 op const char *prfx1;
265 f95d82dd 2021-03-11 op const char *prfx2;
266 f95d82dd 2021-03-11 op } line_prefixes[] = {
267 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
268 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
269 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
270 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
271 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
272 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
273 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
274 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
275 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
276 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
279 d3d56d8a 2021-03-11 op struct line_face {
281 d3d56d8a 2021-03-11 op } line_faces[] = {
282 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
283 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
284 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
285 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
286 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
287 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
288 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
289 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
290 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
291 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
295 f832146f 2021-03-09 op kbd(const char *key)
297 a777f81f 2021-03-10 op struct keytable *t;
299 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
300 f832146f 2021-03-09 op if (has_prefix(key, t->p))
307 a777f81f 2021-03-10 op static const char *
310 a777f81f 2021-03-10 op struct keytable *t;
312 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
313 a777f81f 2021-03-10 op if (k == t->k)
321 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
323 f832146f 2021-03-09 op int ctrl, meta, k;
324 f832146f 2021-03-09 op struct keymap *entry;
327 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
329 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
331 f832146f 2021-03-09 op if (*key == '\0')
333 f832146f 2021-03-09 op k = kbd(key);
338 f832146f 2021-03-09 op /* skip key & spaces */
339 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
341 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
344 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
345 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
346 f832146f 2021-03-09 op if (*key == '\0') {
347 f832146f 2021-03-09 op entry->fn = fn;
350 f832146f 2021-03-09 op map = &entry->map;
355 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
358 f832146f 2021-03-09 op entry->meta = meta;
359 f832146f 2021-03-09 op entry->key = k;
360 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
362 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
363 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
365 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
367 f832146f 2021-03-09 op if (*key != '\0') {
368 f832146f 2021-03-09 op map = &entry->map;
372 f832146f 2021-03-09 op entry->fn = fn;
375 f832146f 2021-03-09 op static inline void
376 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
378 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
381 9ca15951 2021-03-09 op static inline void
382 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
384 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
388 f832146f 2021-03-09 op load_default_keys(void)
390 9ca15951 2021-03-09 op /* === global map === */
393 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
394 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
395 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
396 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
397 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
398 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
400 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
401 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
403 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
405 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
407 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
408 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
409 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
411 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
412 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
413 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
414 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
416 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
417 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
419 f832146f 2021-03-09 op /* vi/vi-like */
420 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
421 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
422 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
423 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
424 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
425 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
427 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
428 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
430 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
431 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
434 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
436 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
438 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
441 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
442 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
443 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
444 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
445 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
446 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
448 f832146f 2021-03-09 op /* "ncurses standard" */
449 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
452 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
454 fa3fd864 2021-03-10 op /* === minibuffer map === */
455 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
456 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
457 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
458 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
459 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
461 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
462 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
463 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
464 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
465 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
466 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
467 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
468 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
469 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
471 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
472 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
473 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
474 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
478 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
480 1d08c280 2021-03-06 op struct line *vl;
482 48e9d457 2021-03-06 op tab->s->line_max++;
484 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
487 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
492 1d08c280 2021-03-06 op vl->type = l->type;
493 1d08c280 2021-03-06 op if (len != 0)
494 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
495 1d08c280 2021-03-06 op vl->alt = l->alt;
497 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
498 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
500 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
505 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
507 1d08c280 2021-03-06 op struct line *l, *t;
509 48e9d457 2021-03-06 op tab->s->line_max = 0;
511 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
512 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
513 1d08c280 2021-03-06 op free(l->line);
514 1d08c280 2021-03-06 op /* l->alt references the original line! */
520 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
522 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
526 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
528 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
529 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
530 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
533 48e9d457 2021-03-06 op restore_cursor(tab);
537 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
539 fed61466 2021-03-11 op if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
542 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
543 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
544 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
547 48e9d457 2021-03-06 op restore_cursor(tab);
551 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
553 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
554 48e9d457 2021-03-06 op restore_cursor(tab);
558 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
560 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
561 a329982b 2021-03-11 op restore_cursor(tab);
565 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
567 a329982b 2021-03-11 op tab->s->curs_x = 0;
568 a329982b 2021-03-11 op restore_cursor(tab);
572 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
574 a329982b 2021-03-11 op struct line *line;
576 f95d82dd 2021-03-11 op const char *prfx;
578 a329982b 2021-03-11 op off = tab->s->line_off + tab->s->curs_y;
579 a329982b 2021-03-11 op if (off >= tab->s->line_max) {
580 a329982b 2021-03-11 op tab->s->curs_x = 0;
584 a329982b 2021-03-11 op line = nth_line(tab, off);
585 a329982b 2021-03-11 op if (line->line != NULL)
586 a329982b 2021-03-11 op tab->s->curs_x = strlen(line->line);
588 a329982b 2021-03-11 op tab->s->curs_x = 0;
590 f95d82dd 2021-03-11 op prfx = line_prefixes[line->type].prfx1;
591 f95d82dd 2021-03-11 op tab->s->curs_x += strlen(prfx);
594 48e9d457 2021-03-06 op restore_cursor(tab);
598 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
600 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
604 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
606 48e9d457 2021-03-06 op struct line *l;
608 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
611 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
612 48e9d457 2021-03-06 op wscrl(body, -1);
613 48e9d457 2021-03-06 op wmove(body, 0, 0);
614 48e9d457 2021-03-06 op print_line(l);
618 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
620 48e9d457 2021-03-06 op struct line *l;
623 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
626 48e9d457 2021-03-06 op tab->s->line_off++;
627 48e9d457 2021-03-06 op wscrl(body, 1);
629 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
632 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
633 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
634 48e9d457 2021-03-06 op print_line(l);
638 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
642 ed44414d 2021-03-09 op off = body_lines+1;
644 ed44414d 2021-03-09 op for (; off > 0; --off)
645 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
649 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
653 ed44414d 2021-03-09 op off = tab->s->line_off + body_lines;
654 ed44414d 2021-03-09 op off = MIN(tab->s->line_max, off);
656 ed44414d 2021-03-09 op for (; off >= 0; --off)
657 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
661 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
663 e19f9a04 2021-03-11 op tab->s->line_off = 0;
664 e19f9a04 2021-03-11 op tab->s->curs_y = 0;
665 e19f9a04 2021-03-11 op redraw_body(tab);
669 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
673 e19f9a04 2021-03-11 op off = tab->s->line_max - body_lines;
674 e19f9a04 2021-03-11 op off = MAX(0, off);
676 e19f9a04 2021-03-11 op tab->s->line_off = off;
677 e19f9a04 2021-03-11 op tab->s->curs_y = MIN(body_lines, tab->s->line_max);
679 e19f9a04 2021-03-11 op redraw_body(tab);
683 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
685 1d08c280 2021-03-06 op event_loopbreak();
689 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
691 2a4ad912 2021-03-08 op struct line *l;
694 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
695 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
697 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
698 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
701 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
705 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
707 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
711 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
715 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
716 22268e11 2021-03-11 op &eecmd_history);
718 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
719 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
721 9ca15951 2021-03-09 op if (thiskey.meta)
722 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
724 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
725 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
729 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
731 7c7d7bb7 2021-03-10 op struct tab *t;
733 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
734 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
735 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
739 7c7d7bb7 2021-03-10 op stop_tab(tab);
741 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
742 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
744 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
746 7c7d7bb7 2021-03-10 op free(tab->s);
751 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
757 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
759 7c7d7bb7 2021-03-10 op struct tab *t;
761 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
763 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
764 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
765 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
769 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
771 7c7d7bb7 2021-03-10 op struct tab *t;
773 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
775 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
776 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
777 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
781 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
783 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
784 22268e11 2021-03-11 op &lu_history);
785 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
789 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
791 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
792 22268e11 2021-03-11 op &lu_history);
793 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
794 5cd2ebb1 2021-03-11 op strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
795 5cd2ebb1 2021-03-11 op ministate.off = strlen(tab->urlstr);
796 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
800 7c7d7bb7 2021-03-10 op global_key_unbound(void)
802 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
806 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
808 3eecee9e 2021-03-12 op minibuffer_taint_hist();
810 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
813 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
814 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
815 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
816 d67133bf 2021-03-12 op ministate.len--;
820 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
822 3eecee9e 2021-03-12 op minibuffer_taint_hist();
824 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
827 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
828 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
829 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
830 9ca15951 2021-03-09 op ministate.off--;
831 9ca15951 2021-03-09 op ministate.len--;
835 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
837 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
839 9ca15951 2021-03-09 op ministate.off++;
843 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
845 9ca15951 2021-03-09 op if (ministate.off == 0)
847 9ca15951 2021-03-09 op ministate.off--;
851 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
853 9ca15951 2021-03-09 op ministate.off = ministate.len;
857 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
859 9ca15951 2021-03-09 op ministate.off = 0;
863 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
865 3eecee9e 2021-03-12 op minibuffer_taint_hist();
867 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
869 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
870 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
874 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
876 b360ebb3 2021-03-10 op ministate.abortfn();
880 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
882 22268e11 2021-03-11 op minibuffer_taint_hist();
883 b360ebb3 2021-03-10 op ministate.donefn();
887 22268e11 2021-03-11 op cmd_mini_previous_history_element(struct tab *tab)
889 22268e11 2021-03-11 op if (ministate.history == NULL) {
890 22268e11 2021-03-11 op message("No history");
894 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
895 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
896 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
897 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
898 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
899 22268e11 2021-03-11 op message("No prev item");
901 22268e11 2021-03-11 op ministate.hist_off--;
904 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
905 22268e11 2021-03-11 op ministate.off = 0;
906 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
911 22268e11 2021-03-11 op cmd_mini_next_history_element(struct tab *tab)
913 22268e11 2021-03-11 op if (ministate.history == NULL) {
914 22268e11 2021-03-11 op message("No history");
918 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
919 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
920 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
921 22268e11 2021-03-11 op ministate.hist_off = 0;
922 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
923 22268e11 2021-03-11 op message("No next item");
925 22268e11 2021-03-11 op ministate.hist_off++;
928 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
929 22268e11 2021-03-11 op ministate.off = 0;
930 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
935 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
937 22268e11 2021-03-11 op struct minibuf_hist *hist;
939 22268e11 2021-03-11 op if (ministate.history == NULL)
942 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
945 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
947 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
948 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
950 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
951 22268e11 2021-03-11 op ministate.history->len++;
955 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
956 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
957 22268e11 2021-03-11 op * navigation" thing.
960 22268e11 2021-03-11 op minibuffer_taint_hist(void)
962 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
965 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
966 22268e11 2021-03-11 op ministate.hist_cur = NULL;
970 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
972 22268e11 2021-03-11 op minibuffer_taint_hist();
974 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
977 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
979 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
980 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
981 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
982 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
983 9ca15951 2021-03-09 op ministate.off++;
984 9ca15951 2021-03-09 op ministate.len++;
988 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
990 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
991 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
992 5cd2ebb1 2021-03-11 op global_key_unbound();
996 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1000 b360ebb3 2021-03-10 op eecmd_select(void)
1002 b360ebb3 2021-03-10 op exit_minibuffer();
1003 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1004 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
1008 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1010 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1014 5cd2ebb1 2021-03-11 op ir_select(void)
1016 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1017 5cd2ebb1 2021-03-11 op struct url url;
1018 5cd2ebb1 2021-03-11 op struct tab *tab;
1020 5cd2ebb1 2021-03-11 op tab = current_tab();
1022 5cd2ebb1 2021-03-11 op exit_minibuffer();
1023 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1025 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1026 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
1027 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
1028 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
1029 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1033 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1035 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
1036 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
1037 5cd2ebb1 2021-03-11 op global_key_unbound();
1041 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1045 5cd2ebb1 2021-03-11 op lu_select(void)
1047 5cd2ebb1 2021-03-11 op exit_minibuffer();
1048 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1049 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1052 48e9d457 2021-03-06 op static struct line *
1053 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
1055 48e9d457 2021-03-06 op struct line *l;
1059 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
1065 48e9d457 2021-03-06 op /* unreachable */
1069 5e11c00c 2021-03-02 op static struct tab *
1070 5e11c00c 2021-03-02 op current_tab(void)
1072 5e11c00c 2021-03-02 op struct tab *t;
1074 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1075 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1079 5e11c00c 2021-03-02 op /* unreachable */
1084 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
1086 481340cc 2021-03-11 op struct tab *tab;
1087 f832146f 2021-03-09 op struct keymap *k;
1088 7c7d7bb7 2021-03-10 op const char *keyname;
1089 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
1091 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1092 9ca15951 2021-03-09 op if (thiskey.key == ERR)
1094 9ca15951 2021-03-09 op if (thiskey.key == 27) {
1095 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
1097 9ca15951 2021-03-09 op thiskey.meta = 1;
1098 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1099 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1100 c314a314 2021-03-11 op thiskey.meta = 0;
1101 9ca15951 2021-03-09 op thiskey.key = 27;
1104 9ca15951 2021-03-09 op thiskey.meta = 0;
1106 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1107 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1108 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1109 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1110 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
1111 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
1113 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
1114 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1117 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1118 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1119 9ca15951 2021-03-09 op k->key == thiskey.key) {
1120 f832146f 2021-03-09 op if (k->fn == NULL)
1121 f832146f 2021-03-09 op current_map = &k->map;
1123 9ca15951 2021-03-09 op current_map = base_map;
1124 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1125 f832146f 2021-03-09 op k->fn(current_tab());
1131 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1132 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1134 7c7d7bb7 2021-03-10 op global_key_unbound();
1137 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1138 9ca15951 2021-03-09 op current_map = base_map;
1141 179f0f58 2021-03-11 op redraw_tab(current_tab());
1145 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1147 a6d450c1 2021-03-06 op clminibufev_set = 0;
1149 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1150 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1152 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1153 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1154 9cb0f9ce 2021-03-10 op wrefresh(body);
1155 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1157 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1158 9cb0f9ce 2021-03-10 op wrefresh(body);
1163 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1165 1d08c280 2021-03-06 op struct tab *tab;
1171 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1173 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1174 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1176 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1177 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1179 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1180 48e9d457 2021-03-06 op body_lines = LINES-3;
1181 bd9637e9 2021-03-06 op body_cols = COLS;
1183 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1185 1d08c280 2021-03-06 op tab = current_tab();
1187 1d08c280 2021-03-06 op wrap_page(tab);
1188 1d08c280 2021-03-06 op redraw_tab(tab);
1192 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
1193 eb259e66 2021-03-02 op * and the end of the separator after the word.
1196 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1198 eb259e66 2021-03-02 op *endword = s;
1199 eb259e66 2021-03-02 op *endword = s;
1201 eb259e66 2021-03-02 op if (*s == '\0')
1204 eb259e66 2021-03-02 op /* find the end of the current world */
1205 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1206 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
1210 eb259e66 2021-03-02 op *endword = s;
1212 eb259e66 2021-03-02 op /* find the end of the separator */
1213 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1214 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
1218 eb259e66 2021-03-02 op *endspc = s;
1223 1d08c280 2021-03-06 op static inline int
1224 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1225 1d08c280 2021-03-06 op const char **line)
1227 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
1229 1d08c280 2021-03-06 op *line += *off - zero;
1230 eb259e66 2021-03-02 op *off = zero;
1234 eb259e66 2021-03-02 op static inline void
1235 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
1239 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1240 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
1241 eb259e66 2021-03-02 op addch((*s)[i]);
1242 eb259e66 2021-03-02 op *off += len;
1247 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
1248 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
1250 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
1253 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
1255 eb259e66 2021-03-02 op size_t zero, off, len, split;
1256 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
1258 1d08c280 2021-03-06 op zero = strlen(prfx);
1260 1d08c280 2021-03-06 op line = l->line;
1261 1d08c280 2021-03-06 op linestart = l->line;
1263 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
1264 eb259e66 2021-03-02 op len = endword - line;
1265 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1266 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1267 48e9d457 2021-03-06 op while (len >= body_cols) {
1268 eb259e66 2021-03-02 op /* hard wrap */
1269 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1270 48e9d457 2021-03-06 op len -= body_cols-1;
1271 48e9d457 2021-03-06 op line += body_cols-1;
1274 eb259e66 2021-03-02 op if (len != 0)
1279 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
1280 eb259e66 2021-03-02 op len = endspc - endword;
1281 eb259e66 2021-03-02 op /* line = endspc; */
1282 eb259e66 2021-03-02 op if (off != zero) {
1283 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1284 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1285 1d08c280 2021-03-06 op linestart = endspc;
1290 eb259e66 2021-03-02 op line = endspc;
1293 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
1297 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
1299 1d08c280 2021-03-06 op size_t off, len;
1300 1d08c280 2021-03-06 op const char *linestart;
1302 c49d61bc 2021-03-12 op if (l->line == NULL)
1303 c49d61bc 2021-03-12 op return emitline(tab, 0, &off, l, &linestart);
1305 1d08c280 2021-03-06 op len = strlen(l->line);
1307 1d08c280 2021-03-06 op linestart = l->line;
1309 1d08c280 2021-03-06 op while (len >= COLS) {
1310 1d08c280 2021-03-06 op len -= COLS-1;
1311 1d08c280 2021-03-06 op off = COLS-1;
1312 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
1316 ede1f142 2021-03-07 op if (len != 0)
1317 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
1323 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1325 1d08c280 2021-03-06 op struct line *l;
1326 0d568960 2021-03-11 op const char *prfx;
1328 1d08c280 2021-03-06 op empty_vlist(tab);
1330 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1331 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1332 5e11c00c 2021-03-02 op switch (l->type) {
1333 5e11c00c 2021-03-02 op case LINE_TEXT:
1334 5e11c00c 2021-03-02 op case LINE_LINK:
1335 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1336 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1337 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1338 5e11c00c 2021-03-02 op case LINE_ITEM:
1339 5e11c00c 2021-03-02 op case LINE_QUOTE:
1340 0d568960 2021-03-11 op wrap_text(tab, prfx, l);
1342 5e11c00c 2021-03-02 op case LINE_PRE_START:
1343 5e11c00c 2021-03-02 op case LINE_PRE_END:
1344 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
1346 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1347 1d08c280 2021-03-06 op hardwrap_text(tab, l);
1354 1d08c280 2021-03-06 op static inline void
1355 1d08c280 2021-03-06 op print_line(struct line *l)
1357 bd9637e9 2021-03-06 op const char *text = l->line;
1358 f95d82dd 2021-03-11 op const char *prfx = line_prefixes[l->type].prfx1;
1359 d3d56d8a 2021-03-11 op int face = line_faces[l->type].prop;
1361 bd9637e9 2021-03-06 op if (text == NULL)
1364 d3d56d8a 2021-03-11 op if (face != 0)
1365 d3d56d8a 2021-03-11 op wattron(body, face);
1366 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1367 d3d56d8a 2021-03-11 op if (face != 0)
1368 d3d56d8a 2021-03-11 op wattroff(body, face);
1372 8af5e5ed 2021-03-08 op redraw_tabline(void)
1374 7c7d7bb7 2021-03-10 op struct tab *tab;
1375 7c7d7bb7 2021-03-10 op int current;
1377 8af5e5ed 2021-03-08 op wclear(tabline);
1378 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1380 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1381 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1382 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1384 a329982b 2021-03-11 op if (current)
1385 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1387 a329982b 2021-03-11 op wprintw(tabline, "%s%d:todo title ",
1388 a329982b 2021-03-11 op current ? "*" : " ", tab->id);
1390 a329982b 2021-03-11 op if (current)
1391 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1396 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1399 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1400 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1401 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1403 48e9d457 2021-03-06 op wclear(modeline);
1404 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1405 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1407 fc43eadd 2021-03-12 op wprintw(modeline, "-%c %s-mode ",
1408 481340cc 2021-03-11 op spin[tab->s->loading_anim_step], mode);
1410 481340cc 2021-03-11 op pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1412 481340cc 2021-03-11 op if (tab->s->line_max <= body_lines)
1413 481340cc 2021-03-11 op wprintw(modeline, "All ");
1414 481340cc 2021-03-11 op else if (tab->s->line_off == 0)
1415 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1416 481340cc 2021-03-11 op else if (tab->s->line_off + body_lines >= tab->s->line_max)
1417 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1419 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1421 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1422 481340cc 2021-03-11 op tab->s->line_off + tab->s->curs_y,
1423 481340cc 2021-03-11 op tab->s->line_max,
1424 481340cc 2021-03-11 op tab->urlstr);
1426 48e9d457 2021-03-06 op getyx(modeline, y, x);
1427 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1430 48e9d457 2021-03-06 op (void)max_y;
1432 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1433 48e9d457 2021-03-06 op waddstr(modeline, "-");
1437 9ca15951 2021-03-09 op redraw_minibuffer(void)
1439 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1441 9ca15951 2021-03-09 op wclear(minibuf);
1442 22268e11 2021-03-11 op if (in_minibuffer) {
1443 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1444 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1445 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1446 22268e11 2021-03-11 op ministate.hist_off + 1,
1447 22268e11 2021-03-11 op ministate.history->len);
1449 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1451 22268e11 2021-03-11 op while (ministate.off - skip > COLS / 2) {
1452 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1455 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1456 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1458 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1461 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1462 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1463 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1465 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1468 22268e11 2021-03-11 op wmove(minibuf, 0, off_x + ministate.off - skip);
1472 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1474 e19f9a04 2021-03-11 op redraw_tabline();
1475 e19f9a04 2021-03-11 op redraw_body(tab);
1476 e19f9a04 2021-03-11 op redraw_modeline(tab);
1477 e19f9a04 2021-03-11 op redraw_minibuffer();
1479 e19f9a04 2021-03-11 op restore_cursor(tab);
1480 e19f9a04 2021-03-11 op wrefresh(tabline);
1481 e19f9a04 2021-03-11 op wrefresh(modeline);
1483 e19f9a04 2021-03-11 op if (in_minibuffer) {
1484 e19f9a04 2021-03-11 op wrefresh(body);
1485 e19f9a04 2021-03-11 op wrefresh(minibuf);
1487 e19f9a04 2021-03-11 op wrefresh(minibuf);
1488 e19f9a04 2021-03-11 op wrefresh(body);
1493 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1495 48e9d457 2021-03-06 op struct line *l;
1498 48e9d457 2021-03-06 op werase(body);
1500 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1501 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1505 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1506 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1507 48e9d457 2021-03-06 op wmove(body, line, 0);
1508 1d08c280 2021-03-06 op print_line(l);
1510 48e9d457 2021-03-06 op if (line == body_lines)
1516 7953dd72 2021-03-07 op message(const char *fmt, ...)
1520 7953dd72 2021-03-07 op if (clminibufev_set)
1521 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1522 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1523 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1524 7953dd72 2021-03-07 op clminibufev_set = 1;
1526 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1527 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1528 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1529 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1532 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1534 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1535 9cb0f9ce 2021-03-10 op wrefresh(body);
1536 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1538 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1539 9cb0f9ce 2021-03-10 op wrefresh(body);
1544 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1546 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1548 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1549 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1550 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1554 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1556 8af5e5ed 2021-03-08 op struct tab *tab = d;
1558 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1560 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1561 8af5e5ed 2021-03-08 op wrefresh(modeline);
1563 8af5e5ed 2021-03-08 op wrefresh(body);
1564 9ca15951 2021-03-09 op if (in_minibuffer)
1565 9ca15951 2021-03-09 op wrefresh(minibuf);
1567 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1571 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1573 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1575 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1576 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1577 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1579 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1581 43a1b8d0 2021-03-09 op wrefresh(modeline);
1582 43a1b8d0 2021-03-09 op wrefresh(body);
1583 9ca15951 2021-03-09 op if (in_minibuffer)
1584 9ca15951 2021-03-09 op wrefresh(minibuf);
1588 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1590 43a1b8d0 2021-03-09 op empty_vlist(tab);
1591 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1592 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1593 8af5e5ed 2021-03-08 op load_url(tab, url);
1595 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1596 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1597 43a1b8d0 2021-03-09 op redraw_tab(tab);
1601 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1602 22268e11 2021-03-11 op void (*abortfn)(void), struct minibuf_histhead *hist)
1604 9ca15951 2021-03-09 op in_minibuffer = 1;
1605 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1606 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1608 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1610 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1611 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1612 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1613 9ca15951 2021-03-09 op ministate.off = 0;
1614 bb4e4ddf 2021-03-12 op ministate.len = 0;
1615 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1617 22268e11 2021-03-11 op ministate.history = hist;
1618 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1619 22268e11 2021-03-11 op ministate.hist_off = 0;
1623 b360ebb3 2021-03-10 op exit_minibuffer(void)
1625 9ca15951 2021-03-09 op wclear(minibuf);
1627 9ca15951 2021-03-09 op in_minibuffer = 0;
1628 9ca15951 2021-03-09 op base_map = &global_map;
1629 9ca15951 2021-03-09 op current_map = &global_map;
1633 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1635 5cd2ebb1 2021-03-11 op struct tab *t;
1637 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1638 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1641 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1645 bcb0b073 2021-03-07 op new_tab(void)
1647 bcb0b073 2021-03-07 op struct tab *tab, *t;
1648 bcb0b073 2021-03-07 op const char *url = "about:new";
1650 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1653 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1656 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1658 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1659 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1661 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1662 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1664 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1666 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1670 bcb0b073 2021-03-07 op event_loopbreak();
1674 5e11c00c 2021-03-02 op ui_init(void)
1676 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1678 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1679 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1681 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1683 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1684 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1685 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1687 9ca15951 2021-03-09 op base_map = &global_map;
1688 f832146f 2021-03-09 op current_map = &global_map;
1689 f832146f 2021-03-09 op load_default_keys();
1696 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1698 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1700 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1702 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1704 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1707 48e9d457 2021-03-06 op body_lines = LINES-3;
1708 48e9d457 2021-03-06 op body_cols = COLS;
1710 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1711 48e9d457 2021-03-06 op scrollok(body, TRUE);
1713 5e11c00c 2021-03-02 op /* non-blocking input */
1714 48e9d457 2021-03-06 op wtimeout(body, 0);
1716 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1718 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1719 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1721 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1722 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1730 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1732 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1733 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1737 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1739 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1742 1d08c280 2021-03-06 op wrap_page(tab);
1743 5e11c00c 2021-03-02 op redraw_tab(tab);
1747 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1749 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1750 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1752 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1753 22268e11 2021-03-11 op &ir_history);
1754 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1755 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1756 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1760 5e11c00c 2021-03-02 op ui_end(void)