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 768db5bb 2021-03-12 op static int push_line(struct tab*, const struct line*, const char*, size_t, int);
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 b1df9b71 2021-03-12 op static void cmd_push_button_new_tab(struct tab*);
104 3b4f9e49 2021-03-11 op static void cmd_clear_minibuf(struct tab*);
105 9ca15951 2021-03-09 op static void cmd_execute_extended_command(struct tab*);
106 7c7d7bb7 2021-03-10 op static void cmd_tab_close(struct tab*);
107 7c7d7bb7 2021-03-10 op static void cmd_tab_new(struct tab*);
108 7c7d7bb7 2021-03-10 op static void cmd_tab_next(struct tab*);
109 7c7d7bb7 2021-03-10 op static void cmd_tab_previous(struct tab*);
110 5cd2ebb1 2021-03-11 op static void cmd_load_url(struct tab*);
111 5cd2ebb1 2021-03-11 op static void cmd_load_current_url(struct tab*);
113 9ca15951 2021-03-09 op static void global_key_unbound(void);
115 d67133bf 2021-03-12 op static void cmd_mini_delete_char(struct tab*);
116 d67133bf 2021-03-12 op static void cmd_mini_delete_backward_char(struct tab*);
117 9ca15951 2021-03-09 op static void cmd_mini_forward_char(struct tab*);
118 9ca15951 2021-03-09 op static void cmd_mini_backward_char(struct tab*);
119 9ca15951 2021-03-09 op static void cmd_mini_move_end_of_line(struct tab*);
120 9ca15951 2021-03-09 op static void cmd_mini_move_beginning_of_line(struct tab*);
121 fa3fd864 2021-03-10 op static void cmd_mini_kill_line(struct tab*);
122 22268e11 2021-03-11 op static void cmd_mini_abort(struct tab*);
123 22268e11 2021-03-11 op static void cmd_mini_complete_and_exit(struct tab*);
124 22268e11 2021-03-11 op static void cmd_mini_previous_history_element(struct tab*);
125 22268e11 2021-03-11 op static void cmd_mini_next_history_element(struct tab*);
127 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
128 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
129 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
130 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
131 b360ebb3 2021-03-10 op static void eecmd_select(void);
132 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
133 5cd2ebb1 2021-03-11 op static void ir_select(void);
134 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
135 5cd2ebb1 2021-03-11 op static void lu_select(void);
137 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
138 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
139 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
140 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
141 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
142 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
143 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
144 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
145 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
146 48e9d457 2021-03-06 op static void print_line(struct line*);
147 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
148 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
149 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
150 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
151 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
152 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
153 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
154 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
155 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
156 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
157 22268e11 2021-03-11 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct minibuf_histhead*);
158 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
159 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
160 b1df9b71 2021-03-12 op static struct tab *new_tab(void);
162 9ca15951 2021-03-09 op static struct { int meta, key; } thiskey;
164 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
165 48e9d457 2021-03-06 op static int body_lines, body_cols;
167 48e9d457 2021-03-06 op static struct event clminibufev;
168 48e9d457 2021-03-06 op static int clminibufev_set;
169 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
170 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
172 bcb0b073 2021-03-07 op static uint32_t tab_counter;
174 1d08c280 2021-03-06 op struct ui_state {
177 48e9d457 2021-03-06 op size_t line_off;
178 48e9d457 2021-03-06 op size_t line_max;
180 8af5e5ed 2021-03-08 op short loading_anim;
181 8af5e5ed 2021-03-08 op short loading_anim_step;
182 8af5e5ed 2021-03-08 op struct event loadingev;
184 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
187 7c7d7bb7 2021-03-10 op static char keybuf[64];
189 f832146f 2021-03-09 op #define CTRL(n) ((n)&0x1F)
191 a777f81f 2021-03-10 op struct keytable {
194 a777f81f 2021-03-10 op } keytable[] = {
195 a777f81f 2021-03-10 op { "<up>", KEY_UP },
196 a777f81f 2021-03-10 op { "<down>", KEY_DOWN },
197 a777f81f 2021-03-10 op { "<left>", KEY_LEFT },
198 a777f81f 2021-03-10 op { "<right>", KEY_RIGHT },
199 a777f81f 2021-03-10 op { "<prior>", KEY_PPAGE },
200 a777f81f 2021-03-10 op { "<next>", KEY_NPAGE },
201 a777f81f 2021-03-10 op { "<home>", KEY_HOME },
202 a777f81f 2021-03-10 op { "<end>", KEY_END },
204 a777f81f 2021-03-10 op { "del", KEY_BACKSPACE },
205 a777f81f 2021-03-10 op { "esc", 27 },
206 a777f81f 2021-03-10 op { "space", ' ' },
207 a777f81f 2021-03-10 op { "spc", ' ' },
208 a777f81f 2021-03-10 op { "enter", CTRL('m') },
209 a777f81f 2021-03-10 op { "ret", CTRL('m' )},
210 a777f81f 2021-03-10 op { "tab", CTRL('i') },
215 9ca15951 2021-03-09 op struct kmap {
216 9ca15951 2021-03-09 op TAILQ_HEAD(map, keymap) m;
217 9ca15951 2021-03-09 op void (*unhandled_input)(void);
220 9ca15951 2021-03-09 op struct kmap global_map,
221 fa3fd864 2021-03-10 op minibuffer_map,
222 9ca15951 2021-03-09 op *current_map,
225 f832146f 2021-03-09 op struct keymap {
228 f832146f 2021-03-09 op struct kmap map;
229 f832146f 2021-03-09 op void (*fn)(struct tab*);
231 f832146f 2021-03-09 op TAILQ_ENTRY(keymap) keymaps;
234 22268e11 2021-03-11 op /* TODO: limit to a maximum number of entries */
235 22268e11 2021-03-11 op struct minibuf_histhead {
236 22268e11 2021-03-11 op TAILQ_HEAD(mhisthead, minibuf_hist) head;
239 22268e11 2021-03-11 op struct minibuf_hist {
240 22268e11 2021-03-11 op char h[1025];
241 22268e11 2021-03-11 op TAILQ_ENTRY(minibuf_hist) entries;
244 22268e11 2021-03-11 op static struct minibuf_histhead eecmd_history,
248 9ca15951 2021-03-09 op static int in_minibuffer;
250 9ca15951 2021-03-09 op static struct {
251 9cb0f9ce 2021-03-10 op char *curmesg;
253 040fbdf8 2021-03-10 op char buf[1025];
254 9ca15951 2021-03-09 op size_t off, len;
255 5cd2ebb1 2021-03-11 op char prompt[32];
256 b360ebb3 2021-03-10 op void (*donefn)(void);
257 b360ebb3 2021-03-10 op void (*abortfn)(void);
259 22268e11 2021-03-11 op struct minibuf_histhead *history;
260 22268e11 2021-03-11 op struct minibuf_hist *hist_cur;
261 22268e11 2021-03-11 op size_t hist_off;
264 f95d82dd 2021-03-11 op struct lineprefix {
265 f95d82dd 2021-03-11 op const char *prfx1;
266 f95d82dd 2021-03-11 op const char *prfx2;
267 f95d82dd 2021-03-11 op } line_prefixes[] = {
268 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
269 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
270 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
271 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
272 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
273 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
274 f95d82dd 2021-03-11 op [LINE_QUOTE] = { "> ", "> " },
275 f95d82dd 2021-03-11 op [LINE_PRE_START] = { "```", "```" },
276 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
277 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
280 d3d56d8a 2021-03-11 op struct line_face {
282 d3d56d8a 2021-03-11 op } line_faces[] = {
283 d3d56d8a 2021-03-11 op [LINE_TEXT] = { 0 },
284 d3d56d8a 2021-03-11 op [LINE_LINK] = { A_UNDERLINE },
285 d3d56d8a 2021-03-11 op [LINE_TITLE_1] = { A_BOLD },
286 d3d56d8a 2021-03-11 op [LINE_TITLE_2] = { A_BOLD },
287 d3d56d8a 2021-03-11 op [LINE_TITLE_3] = { A_BOLD },
288 d3d56d8a 2021-03-11 op [LINE_ITEM] = { 0 },
289 d3d56d8a 2021-03-11 op [LINE_QUOTE] = { A_DIM },
290 d3d56d8a 2021-03-11 op [LINE_PRE_START] = { 0 },
291 d3d56d8a 2021-03-11 op [LINE_PRE_CONTENT] = { 0 },
292 d3d56d8a 2021-03-11 op [LINE_PRE_END] = { 0 },
296 f832146f 2021-03-09 op kbd(const char *key)
298 a777f81f 2021-03-10 op struct keytable *t;
300 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
301 f832146f 2021-03-09 op if (has_prefix(key, t->p))
308 a777f81f 2021-03-10 op static const char *
311 a777f81f 2021-03-10 op struct keytable *t;
313 a777f81f 2021-03-10 op for (t = keytable; t->p != NULL; ++t) {
314 a777f81f 2021-03-10 op if (k == t->k)
322 f832146f 2021-03-09 op kmap_define_key(struct kmap *map, const char *key, void (*fn)(struct tab*))
324 f832146f 2021-03-09 op int ctrl, meta, k;
325 f832146f 2021-03-09 op struct keymap *entry;
328 f832146f 2021-03-09 op if ((ctrl = has_prefix(key, "C-")))
330 f832146f 2021-03-09 op if ((meta = has_prefix(key, "M-")))
332 f832146f 2021-03-09 op if (*key == '\0')
334 f832146f 2021-03-09 op k = kbd(key);
339 f832146f 2021-03-09 op /* skip key & spaces */
340 f832146f 2021-03-09 op while (*key != '\0' && !isspace(*key))
342 f832146f 2021-03-09 op while (*key != '\0' && isspace(*key))
345 9ca15951 2021-03-09 op TAILQ_FOREACH(entry, &map->m, keymaps) {
346 f832146f 2021-03-09 op if (entry->meta == meta && entry->key == k) {
347 f832146f 2021-03-09 op if (*key == '\0') {
348 f832146f 2021-03-09 op entry->fn = fn;
351 f832146f 2021-03-09 op map = &entry->map;
356 f832146f 2021-03-09 op if ((entry = calloc(1, sizeof(*entry))) == NULL)
359 f832146f 2021-03-09 op entry->meta = meta;
360 f832146f 2021-03-09 op entry->key = k;
361 9ca15951 2021-03-09 op TAILQ_INIT(&entry->map.m);
363 9ca15951 2021-03-09 op if (TAILQ_EMPTY(&map->m))
364 9ca15951 2021-03-09 op TAILQ_INSERT_HEAD(&map->m, entry, keymaps);
366 9ca15951 2021-03-09 op TAILQ_INSERT_TAIL(&map->m, entry, keymaps);
368 f832146f 2021-03-09 op if (*key != '\0') {
369 f832146f 2021-03-09 op map = &entry->map;
373 f832146f 2021-03-09 op entry->fn = fn;
376 f832146f 2021-03-09 op static inline void
377 f832146f 2021-03-09 op global_set_key(const char *key, void (*fn)(struct tab*))
379 f832146f 2021-03-09 op kmap_define_key(&global_map, key, fn);
382 9ca15951 2021-03-09 op static inline void
383 fa3fd864 2021-03-10 op minibuffer_set_key(const char *key, void (*fn)(struct tab*))
385 fa3fd864 2021-03-10 op kmap_define_key(&minibuffer_map, key, fn);
389 f832146f 2021-03-09 op load_default_keys(void)
391 9ca15951 2021-03-09 op /* === global map === */
394 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
395 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
396 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
397 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
398 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
399 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
401 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
402 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
403 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
404 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
406 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
408 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
410 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
411 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
412 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
414 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
415 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
416 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
417 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
419 e19f9a04 2021-03-11 op global_set_key("M-<", cmd_beginning_of_buffer);
420 e19f9a04 2021-03-11 op global_set_key("M->", cmd_end_of_buffer);
422 f832146f 2021-03-09 op /* vi/vi-like */
423 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
424 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
425 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
426 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
427 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
428 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
430 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
431 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
433 e19f9a04 2021-03-11 op global_set_key("g g", cmd_beginning_of_buffer);
434 e19f9a04 2021-03-11 op global_set_key("G", cmd_end_of_buffer);
437 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
439 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
441 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
444 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
445 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
446 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
447 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
448 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
449 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
451 f832146f 2021-03-09 op /* "ncurses standard" */
452 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
455 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
456 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
458 fa3fd864 2021-03-10 op /* === minibuffer map === */
459 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
460 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
461 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
462 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
463 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
465 fa3fd864 2021-03-10 op minibuffer_set_key("C-f", cmd_mini_forward_char);
466 fa3fd864 2021-03-10 op minibuffer_set_key("C-b", cmd_mini_backward_char);
467 5b2bf704 2021-03-12 op minibuffer_set_key("<right>", cmd_mini_forward_char);
468 5b2bf704 2021-03-12 op minibuffer_set_key("<left>", cmd_mini_backward_char);
469 fa3fd864 2021-03-10 op minibuffer_set_key("C-e", cmd_mini_move_end_of_line);
470 fa3fd864 2021-03-10 op minibuffer_set_key("C-a", cmd_mini_move_beginning_of_line);
471 fa3fd864 2021-03-10 op minibuffer_set_key("<end>", cmd_mini_move_end_of_line);
472 fa3fd864 2021-03-10 op minibuffer_set_key("<home>", cmd_mini_move_beginning_of_line);
473 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
475 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
476 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
477 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
478 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
482 768db5bb 2021-03-12 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len, int cont)
484 1d08c280 2021-03-06 op struct line *vl;
486 48e9d457 2021-03-06 op tab->s->line_max++;
488 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
491 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
496 1d08c280 2021-03-06 op vl->type = l->type;
497 1d08c280 2021-03-06 op if (len != 0)
498 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
499 1d08c280 2021-03-06 op vl->alt = l->alt;
500 768db5bb 2021-03-12 op vl->flags = cont;
502 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
503 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
505 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
510 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
512 1d08c280 2021-03-06 op struct line *l, *t;
514 48e9d457 2021-03-06 op tab->s->line_max = 0;
516 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
517 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
518 1d08c280 2021-03-06 op free(l->line);
519 1d08c280 2021-03-06 op /* l->alt references the original line! */
525 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
527 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
531 46a9311e 2021-03-08 op cmd_previous_line(struct tab *tab)
533 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
534 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
535 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
538 48e9d457 2021-03-06 op restore_cursor(tab);
542 46a9311e 2021-03-08 op cmd_next_line(struct tab *tab)
544 fed61466 2021-03-11 op if (tab->s->line_off + tab->s->curs_y >= tab->s->line_max)
547 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
548 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
549 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
552 48e9d457 2021-03-06 op restore_cursor(tab);
556 46a9311e 2021-03-08 op cmd_forward_char(struct tab *tab)
558 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
559 48e9d457 2021-03-06 op restore_cursor(tab);
563 46a9311e 2021-03-08 op cmd_backward_char(struct tab *tab)
565 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
566 a329982b 2021-03-11 op restore_cursor(tab);
570 a329982b 2021-03-11 op cmd_move_beginning_of_line(struct tab *tab)
572 a329982b 2021-03-11 op tab->s->curs_x = 0;
573 a329982b 2021-03-11 op restore_cursor(tab);
577 a329982b 2021-03-11 op cmd_move_end_of_line(struct tab *tab)
579 a329982b 2021-03-11 op struct line *line;
581 f95d82dd 2021-03-11 op const char *prfx;
583 a329982b 2021-03-11 op off = tab->s->line_off + tab->s->curs_y;
584 a329982b 2021-03-11 op if (off >= tab->s->line_max) {
585 a329982b 2021-03-11 op tab->s->curs_x = 0;
589 a329982b 2021-03-11 op line = nth_line(tab, off);
590 a329982b 2021-03-11 op if (line->line != NULL)
591 a329982b 2021-03-11 op tab->s->curs_x = strlen(line->line);
593 a329982b 2021-03-11 op tab->s->curs_x = 0;
595 f95d82dd 2021-03-11 op prfx = line_prefixes[line->type].prfx1;
596 f95d82dd 2021-03-11 op tab->s->curs_x += strlen(prfx);
599 48e9d457 2021-03-06 op restore_cursor(tab);
603 46a9311e 2021-03-08 op cmd_redraw(struct tab *tab)
605 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
609 ed44414d 2021-03-09 op cmd_scroll_line_up(struct tab *tab)
611 48e9d457 2021-03-06 op struct line *l;
613 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
616 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
617 48e9d457 2021-03-06 op wscrl(body, -1);
618 48e9d457 2021-03-06 op wmove(body, 0, 0);
619 48e9d457 2021-03-06 op print_line(l);
623 ed44414d 2021-03-09 op cmd_scroll_line_down(struct tab *tab)
625 48e9d457 2021-03-06 op struct line *l;
628 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
631 48e9d457 2021-03-06 op tab->s->line_off++;
632 48e9d457 2021-03-06 op wscrl(body, 1);
634 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
637 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
638 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
639 48e9d457 2021-03-06 op print_line(l);
643 ed44414d 2021-03-09 op cmd_scroll_up(struct tab *tab)
647 ed44414d 2021-03-09 op off = body_lines+1;
649 ed44414d 2021-03-09 op for (; off > 0; --off)
650 ed44414d 2021-03-09 op cmd_scroll_line_up(tab);
654 ed44414d 2021-03-09 op cmd_scroll_down(struct tab *tab)
658 338eecdc 2021-03-12 op off = body_lines+1;
660 338eecdc 2021-03-12 op for (; off > 0; --off)
661 ed44414d 2021-03-09 op cmd_scroll_line_down(tab);
665 e19f9a04 2021-03-11 op cmd_beginning_of_buffer(struct tab *tab)
667 e19f9a04 2021-03-11 op tab->s->line_off = 0;
668 e19f9a04 2021-03-11 op tab->s->curs_y = 0;
669 e19f9a04 2021-03-11 op redraw_body(tab);
673 e19f9a04 2021-03-11 op cmd_end_of_buffer(struct tab *tab)
677 e19f9a04 2021-03-11 op off = tab->s->line_max - body_lines;
678 e19f9a04 2021-03-11 op off = MAX(0, off);
680 e19f9a04 2021-03-11 op tab->s->line_off = off;
681 e19f9a04 2021-03-11 op tab->s->curs_y = MIN(body_lines, tab->s->line_max);
683 e19f9a04 2021-03-11 op redraw_body(tab);
687 46a9311e 2021-03-08 op cmd_kill_telescope(struct tab *tab)
689 1d08c280 2021-03-06 op event_loopbreak();
693 46a9311e 2021-03-08 op cmd_push_button(struct tab *tab)
695 2a4ad912 2021-03-08 op struct line *l;
698 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
699 cb8d5d9b 2021-03-13 op if (nth >= tab->s->line_max)
701 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
702 43a1b8d0 2021-03-09 op if (l->type != LINE_LINK)
705 43a1b8d0 2021-03-09 op load_url_in_tab(tab, l->alt);
709 b1df9b71 2021-03-12 op cmd_push_button_new_tab(struct tab *tab)
711 b1df9b71 2021-03-12 op struct tab *t;
712 b1df9b71 2021-03-12 op struct line *l;
715 b1df9b71 2021-03-12 op nth = tab->s->line_off + tab->s->curs_y;
716 b1df9b71 2021-03-12 op if (nth > tab->s->line_max)
718 b1df9b71 2021-03-12 op l = nth_line(tab, nth);
719 b1df9b71 2021-03-12 op if (l->type != LINE_LINK)
722 b1df9b71 2021-03-12 op t = new_tab();
723 b1df9b71 2021-03-12 op memcpy(&t->url, &tab->url, sizeof(tab->url));
724 b1df9b71 2021-03-12 op memcpy(&t->urlstr, &tab->urlstr, sizeof(tab->urlstr));
725 b1df9b71 2021-03-12 op load_url_in_tab(t, l->alt);
729 3b4f9e49 2021-03-11 op cmd_clear_minibuf(struct tab *tab)
731 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
735 9ca15951 2021-03-09 op cmd_execute_extended_command(struct tab *tab)
739 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
740 22268e11 2021-03-11 op &eecmd_history);
742 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
743 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
745 9ca15951 2021-03-09 op if (thiskey.meta)
746 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
748 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
749 9ca15951 2021-03-09 op strlcat(ministate.prompt, " ", len);
753 7c7d7bb7 2021-03-10 op cmd_tab_close(struct tab *tab)
755 7c7d7bb7 2021-03-10 op struct tab *t;
757 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
758 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
759 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
763 7c7d7bb7 2021-03-10 op stop_tab(tab);
765 7c7d7bb7 2021-03-10 op t = TAILQ_PREV(tab, tabshead, tabs);
766 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
768 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
770 7c7d7bb7 2021-03-10 op free(tab->s);
775 7c7d7bb7 2021-03-10 op cmd_tab_new(struct tab *tab)
781 7c7d7bb7 2021-03-10 op cmd_tab_next(struct tab *tab)
783 7c7d7bb7 2021-03-10 op struct tab *t;
785 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
787 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
788 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
789 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
793 7c7d7bb7 2021-03-10 op cmd_tab_previous(struct tab *tab)
795 7c7d7bb7 2021-03-10 op struct tab *t;
797 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
799 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
800 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
801 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
805 5cd2ebb1 2021-03-11 op cmd_load_url(struct tab *tab)
807 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
808 22268e11 2021-03-11 op &lu_history);
809 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
813 5cd2ebb1 2021-03-11 op cmd_load_current_url(struct tab *tab)
815 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
816 22268e11 2021-03-11 op &lu_history);
817 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
818 5cd2ebb1 2021-03-11 op strlcpy(ministate.buf, tab->urlstr, sizeof(ministate.buf));
819 5cd2ebb1 2021-03-11 op ministate.off = strlen(tab->urlstr);
820 5cd2ebb1 2021-03-11 op ministate.len = ministate.off;
824 7c7d7bb7 2021-03-10 op global_key_unbound(void)
826 7c7d7bb7 2021-03-10 op message("%s is undefined", keybuf);
830 d67133bf 2021-03-12 op cmd_mini_delete_char(struct tab *tab)
832 3eecee9e 2021-03-12 op minibuffer_taint_hist();
834 d67133bf 2021-03-12 op if (ministate.len == 0 || ministate.off == ministate.len)
837 d67133bf 2021-03-12 op memmove(&ministate.buf[ministate.off],
838 d67133bf 2021-03-12 op &ministate.buf[ministate.off+1],
839 d67133bf 2021-03-12 op ministate.len - ministate.off + 1);
840 d67133bf 2021-03-12 op ministate.len--;
844 d67133bf 2021-03-12 op cmd_mini_delete_backward_char(struct tab *tab)
846 3eecee9e 2021-03-12 op minibuffer_taint_hist();
848 9ca15951 2021-03-09 op if (ministate.len == 0 || ministate.off == 0)
851 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off-1],
852 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
853 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
854 9ca15951 2021-03-09 op ministate.off--;
855 9ca15951 2021-03-09 op ministate.len--;
859 9ca15951 2021-03-09 op cmd_mini_forward_char(struct tab *tab)
861 9ca15951 2021-03-09 op if (ministate.off == ministate.len)
863 9ca15951 2021-03-09 op ministate.off++;
867 9ca15951 2021-03-09 op cmd_mini_backward_char(struct tab *tab)
869 9ca15951 2021-03-09 op if (ministate.off == 0)
871 9ca15951 2021-03-09 op ministate.off--;
875 9ca15951 2021-03-09 op cmd_mini_move_end_of_line(struct tab *tab)
877 9ca15951 2021-03-09 op ministate.off = ministate.len;
881 9ca15951 2021-03-09 op cmd_mini_move_beginning_of_line(struct tab *tab)
883 9ca15951 2021-03-09 op ministate.off = 0;
887 fa3fd864 2021-03-10 op cmd_mini_kill_line(struct tab *tab)
889 3eecee9e 2021-03-12 op minibuffer_taint_hist();
891 fa3fd864 2021-03-10 op if (ministate.off == ministate.len)
893 fa3fd864 2021-03-10 op ministate.buf[ministate.off] = '\0';
894 fa3fd864 2021-03-10 op ministate.len -= ministate.off;
898 b360ebb3 2021-03-10 op cmd_mini_abort(struct tab *tab)
900 b360ebb3 2021-03-10 op ministate.abortfn();
904 b360ebb3 2021-03-10 op cmd_mini_complete_and_exit(struct tab *tab)
906 22268e11 2021-03-11 op minibuffer_taint_hist();
907 b360ebb3 2021-03-10 op ministate.donefn();
911 22268e11 2021-03-11 op cmd_mini_previous_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_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
920 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
921 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
922 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
923 22268e11 2021-03-11 op message("No prev 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 cmd_mini_next_history_element(struct tab *tab)
937 22268e11 2021-03-11 op if (ministate.history == NULL) {
938 22268e11 2021-03-11 op message("No history");
942 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
943 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
944 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
945 22268e11 2021-03-11 op ministate.hist_off = 0;
946 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
947 22268e11 2021-03-11 op message("No next item");
949 22268e11 2021-03-11 op ministate.hist_off++;
952 22268e11 2021-03-11 op if (ministate.hist_cur != NULL) {
953 22268e11 2021-03-11 op ministate.off = 0;
954 22268e11 2021-03-11 op ministate.len = strlen(ministate.hist_cur->h);
959 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
961 22268e11 2021-03-11 op struct minibuf_hist *hist;
963 22268e11 2021-03-11 op if (ministate.history == NULL)
966 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
969 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
971 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
972 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
974 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
975 22268e11 2021-03-11 op ministate.history->len++;
979 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
980 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
981 22268e11 2021-03-11 op * navigation" thing.
984 22268e11 2021-03-11 op minibuffer_taint_hist(void)
986 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
989 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
990 22268e11 2021-03-11 op ministate.hist_cur = NULL;
994 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
996 22268e11 2021-03-11 op minibuffer_taint_hist();
998 040fbdf8 2021-03-10 op if (ministate.len == sizeof(ministate.buf) -1)
1001 9ca15951 2021-03-09 op /* TODO: utf8 handling! */
1003 9ca15951 2021-03-09 op memmove(&ministate.buf[ministate.off+1],
1004 9ca15951 2021-03-09 op &ministate.buf[ministate.off],
1005 9ca15951 2021-03-09 op ministate.len - ministate.off + 1);
1006 9ca15951 2021-03-09 op ministate.buf[ministate.off] = thiskey.key;
1007 9ca15951 2021-03-09 op ministate.off++;
1008 9ca15951 2021-03-09 op ministate.len++;
1012 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
1014 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
1015 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
1016 5cd2ebb1 2021-03-11 op global_key_unbound();
1020 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1024 b360ebb3 2021-03-10 op eecmd_select(void)
1026 b360ebb3 2021-03-10 op exit_minibuffer();
1027 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1028 b360ebb3 2021-03-10 op message("TODO: try to execute %s", ministate.buf);
1032 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1034 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1038 5cd2ebb1 2021-03-11 op ir_select(void)
1040 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1041 5cd2ebb1 2021-03-11 op struct url url;
1042 5cd2ebb1 2021-03-11 op struct tab *tab;
1044 5cd2ebb1 2021-03-11 op tab = current_tab();
1046 5cd2ebb1 2021-03-11 op exit_minibuffer();
1047 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1049 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1050 5cd2ebb1 2021-03-11 op memcpy(&url, &tab->url, sizeof(tab->url));
1051 5cd2ebb1 2021-03-11 op url_set_query(&url, ministate.buf);
1052 5cd2ebb1 2021-03-11 op url_unparse(&url, buf, sizeof(buf));
1053 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1057 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1059 5cd2ebb1 2021-03-11 op if (thiskey.meta || isspace(thiskey.key) ||
1060 5cd2ebb1 2021-03-11 op !isgraph(thiskey.key)) {
1061 5cd2ebb1 2021-03-11 op global_key_unbound();
1065 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1069 5cd2ebb1 2021-03-11 op lu_select(void)
1071 5cd2ebb1 2021-03-11 op exit_minibuffer();
1072 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1073 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1076 48e9d457 2021-03-06 op static struct line *
1077 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
1079 48e9d457 2021-03-06 op struct line *l;
1083 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
1089 48e9d457 2021-03-06 op /* unreachable */
1093 5e11c00c 2021-03-02 op static struct tab *
1094 5e11c00c 2021-03-02 op current_tab(void)
1096 5e11c00c 2021-03-02 op struct tab *t;
1098 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1099 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1103 5e11c00c 2021-03-02 op /* unreachable */
1108 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
1110 481340cc 2021-03-11 op struct tab *tab;
1111 f832146f 2021-03-09 op struct keymap *k;
1112 7c7d7bb7 2021-03-10 op const char *keyname;
1113 7c7d7bb7 2021-03-10 op char tmp[2] = {0};
1115 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1116 9ca15951 2021-03-09 op if (thiskey.key == ERR)
1118 9ca15951 2021-03-09 op if (thiskey.key == 27) {
1119 f832146f 2021-03-09 op /* TODO: make escape-time customizable */
1121 9ca15951 2021-03-09 op thiskey.meta = 1;
1122 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1123 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1124 c314a314 2021-03-11 op thiskey.meta = 0;
1125 9ca15951 2021-03-09 op thiskey.key = 27;
1128 9ca15951 2021-03-09 op thiskey.meta = 0;
1130 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1131 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1132 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1133 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1134 7c7d7bb7 2021-03-10 op if ((keyname = unkbd(thiskey.key)) != NULL)
1135 7c7d7bb7 2021-03-10 op strlcat(keybuf, keyname, sizeof(keybuf));
1137 7c7d7bb7 2021-03-10 op tmp[0] = thiskey.key;
1138 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1141 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1142 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1143 9ca15951 2021-03-09 op k->key == thiskey.key) {
1144 f832146f 2021-03-09 op if (k->fn == NULL)
1145 f832146f 2021-03-09 op current_map = &k->map;
1147 9ca15951 2021-03-09 op current_map = base_map;
1148 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1149 f832146f 2021-03-09 op k->fn(current_tab());
1155 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1156 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1158 7c7d7bb7 2021-03-10 op global_key_unbound();
1161 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1162 9ca15951 2021-03-09 op current_map = base_map;
1165 179f0f58 2021-03-11 op redraw_tab(current_tab());
1169 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1171 a6d450c1 2021-03-06 op clminibufev_set = 0;
1173 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1174 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1176 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1177 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1178 9cb0f9ce 2021-03-10 op wrefresh(body);
1179 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1181 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1182 9cb0f9ce 2021-03-10 op wrefresh(body);
1187 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1189 1d08c280 2021-03-06 op struct tab *tab;
1195 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1197 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1198 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1200 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1201 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1203 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
1204 48e9d457 2021-03-06 op body_lines = LINES-3;
1205 bd9637e9 2021-03-06 op body_cols = COLS;
1207 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1209 1d08c280 2021-03-06 op tab = current_tab();
1211 1d08c280 2021-03-06 op wrap_page(tab);
1212 1d08c280 2021-03-06 op redraw_tab(tab);
1216 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
1217 eb259e66 2021-03-02 op * and the end of the separator after the word.
1220 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
1222 eb259e66 2021-03-02 op *endword = s;
1223 eb259e66 2021-03-02 op *endword = s;
1225 eb259e66 2021-03-02 op if (*s == '\0')
1228 eb259e66 2021-03-02 op /* find the end of the current world */
1229 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1230 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
1234 eb259e66 2021-03-02 op *endword = s;
1236 eb259e66 2021-03-02 op /* find the end of the separator */
1237 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
1238 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
1242 eb259e66 2021-03-02 op *endspc = s;
1247 1d08c280 2021-03-06 op static inline int
1248 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
1249 768db5bb 2021-03-12 op const char **line, int *cont)
1251 768db5bb 2021-03-12 op if (!push_line(tab, l, *line, *off - zero, *cont))
1255 1d08c280 2021-03-06 op *line += *off - zero;
1256 eb259e66 2021-03-02 op *off = zero;
1260 eb259e66 2021-03-02 op static inline void
1261 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
1265 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
1266 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
1267 eb259e66 2021-03-02 op addch((*s)[i]);
1268 eb259e66 2021-03-02 op *off += len;
1273 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
1274 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
1276 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
1279 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
1281 eb259e66 2021-03-02 op size_t zero, off, len, split;
1282 768db5bb 2021-03-12 op int cont = 0;
1283 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
1285 1d08c280 2021-03-06 op zero = strlen(prfx);
1287 1d08c280 2021-03-06 op line = l->line;
1288 1d08c280 2021-03-06 op linestart = l->line;
1290 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
1291 eb259e66 2021-03-02 op len = endword - line;
1292 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1293 768db5bb 2021-03-12 op emitline(tab, zero, &off, l, &linestart, &cont);
1294 48e9d457 2021-03-06 op while (len >= body_cols) {
1295 eb259e66 2021-03-02 op /* hard wrap */
1296 768db5bb 2021-03-12 op emitline(tab, zero, &off, l, &linestart, &cont);
1297 48e9d457 2021-03-06 op len -= body_cols-1;
1298 48e9d457 2021-03-06 op line += body_cols-1;
1301 eb259e66 2021-03-02 op if (len != 0)
1306 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
1307 eb259e66 2021-03-02 op len = endspc - endword;
1308 eb259e66 2021-03-02 op /* line = endspc; */
1309 eb259e66 2021-03-02 op if (off != zero) {
1310 48e9d457 2021-03-06 op if (off + len >= body_cols) {
1311 768db5bb 2021-03-12 op emitline(tab, zero, &off, l, &linestart, &cont);
1312 1d08c280 2021-03-06 op linestart = endspc;
1317 eb259e66 2021-03-02 op line = endspc;
1320 768db5bb 2021-03-12 op emitline(tab, zero, &off, l, &linestart, &cont);
1324 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
1326 1d08c280 2021-03-06 op size_t off, len;
1328 1d08c280 2021-03-06 op const char *linestart;
1330 c49d61bc 2021-03-12 op if (l->line == NULL)
1331 768db5bb 2021-03-12 op return emitline(tab, 0, &off, l, &linestart, &cont);
1333 1d08c280 2021-03-06 op len = strlen(l->line);
1335 1d08c280 2021-03-06 op linestart = l->line;
1337 1d08c280 2021-03-06 op while (len >= COLS) {
1338 1d08c280 2021-03-06 op len -= COLS-1;
1339 1d08c280 2021-03-06 op off = COLS-1;
1340 768db5bb 2021-03-12 op if (!emitline(tab, 0, &off, l, &linestart, &cont))
1344 ede1f142 2021-03-07 op if (len != 0)
1345 768db5bb 2021-03-12 op return emitline(tab, 0, &len, l, &linestart, &cont);
1351 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
1353 1d08c280 2021-03-06 op struct line *l;
1354 0d568960 2021-03-11 op const char *prfx;
1356 1d08c280 2021-03-06 op empty_vlist(tab);
1358 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
1359 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1360 5e11c00c 2021-03-02 op switch (l->type) {
1361 5e11c00c 2021-03-02 op case LINE_TEXT:
1362 5e11c00c 2021-03-02 op case LINE_LINK:
1363 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1364 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1365 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1366 5e11c00c 2021-03-02 op case LINE_ITEM:
1367 5e11c00c 2021-03-02 op case LINE_QUOTE:
1368 0d568960 2021-03-11 op wrap_text(tab, prfx, l);
1370 5e11c00c 2021-03-02 op case LINE_PRE_START:
1371 5e11c00c 2021-03-02 op case LINE_PRE_END:
1372 768db5bb 2021-03-12 op push_line(tab, l, NULL, 0, 0);
1374 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1375 1d08c280 2021-03-06 op hardwrap_text(tab, l);
1382 1d08c280 2021-03-06 op static inline void
1383 1d08c280 2021-03-06 op print_line(struct line *l)
1385 bd9637e9 2021-03-06 op const char *text = l->line;
1386 768db5bb 2021-03-12 op const char *prfx;
1387 d3d56d8a 2021-03-11 op int face = line_faces[l->type].prop;
1389 768db5bb 2021-03-12 op if (!l->flags)
1390 768db5bb 2021-03-12 op prfx = line_prefixes[l->type].prfx1;
1392 768db5bb 2021-03-12 op prfx = line_prefixes[l->type].prfx2;
1394 bd9637e9 2021-03-06 op if (text == NULL)
1397 d3d56d8a 2021-03-11 op if (face != 0)
1398 d3d56d8a 2021-03-11 op wattron(body, face);
1399 d3d56d8a 2021-03-11 op wprintw(body, "%s%s", prfx, text);
1400 d3d56d8a 2021-03-11 op if (face != 0)
1401 d3d56d8a 2021-03-11 op wattroff(body, face);
1405 8af5e5ed 2021-03-08 op redraw_tabline(void)
1407 7c7d7bb7 2021-03-10 op struct tab *tab;
1408 7c7d7bb7 2021-03-10 op int current;
1410 156f1501 2021-03-13 op werase(tabline);
1411 8af5e5ed 2021-03-08 op wbkgd(tabline, A_REVERSE);
1413 7c7d7bb7 2021-03-10 op wprintw(tabline, " ");
1414 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1415 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1417 a329982b 2021-03-11 op if (current)
1418 a329982b 2021-03-11 op wattron(tabline, A_UNDERLINE);
1420 a329982b 2021-03-11 op wprintw(tabline, "%s%d:todo title ",
1421 a329982b 2021-03-11 op current ? "*" : " ", tab->id);
1423 a329982b 2021-03-11 op if (current)
1424 a329982b 2021-03-11 op wattroff(tabline, A_UNDERLINE);
1429 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1432 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1433 fc43eadd 2021-03-12 op const char *mode = tab->page.name;
1434 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1436 156f1501 2021-03-13 op werase(modeline);
1437 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1438 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1440 fc43eadd 2021-03-12 op wprintw(modeline, "-%c %s-mode ",
1441 481340cc 2021-03-11 op spin[tab->s->loading_anim_step], mode);
1443 481340cc 2021-03-11 op pct = (tab->s->line_off + tab->s->curs_y) * 100.0 / tab->s->line_max;
1445 481340cc 2021-03-11 op if (tab->s->line_max <= body_lines)
1446 481340cc 2021-03-11 op wprintw(modeline, "All ");
1447 481340cc 2021-03-11 op else if (tab->s->line_off == 0)
1448 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1449 481340cc 2021-03-11 op else if (tab->s->line_off + body_lines >= tab->s->line_max)
1450 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1452 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1454 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1455 481340cc 2021-03-11 op tab->s->line_off + tab->s->curs_y,
1456 481340cc 2021-03-11 op tab->s->line_max,
1457 481340cc 2021-03-11 op tab->urlstr);
1459 48e9d457 2021-03-06 op getyx(modeline, y, x);
1460 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1463 48e9d457 2021-03-06 op (void)max_y;
1465 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1466 48e9d457 2021-03-06 op waddstr(modeline, "-");
1470 9ca15951 2021-03-09 op redraw_minibuffer(void)
1472 22268e11 2021-03-11 op size_t skip = 0, off_x = 0, off_y = 0;
1474 156f1501 2021-03-13 op werase(minibuf);
1475 22268e11 2021-03-11 op if (in_minibuffer) {
1476 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1477 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1478 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1479 22268e11 2021-03-11 op ministate.hist_off + 1,
1480 22268e11 2021-03-11 op ministate.history->len);
1482 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1484 22268e11 2021-03-11 op while (ministate.off - skip > COLS / 2) {
1485 22268e11 2021-03-11 op skip += MIN(ministate.off/4, 1);
1488 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1489 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.hist_cur->h + skip);
1491 22268e11 2021-03-11 op wprintw(minibuf, "%s", ministate.buf + skip);
1494 9cb0f9ce 2021-03-10 op if (ministate.curmesg != NULL) {
1495 9cb0f9ce 2021-03-10 op if (in_minibuffer)
1496 9cb0f9ce 2021-03-10 op wprintw(minibuf, " [%s]", ministate.curmesg);
1498 9cb0f9ce 2021-03-10 op wprintw(minibuf, "%s", ministate.curmesg);
1501 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1502 91a72220 2021-03-13 op wprintw(minibuf, "%s", keybuf);
1504 22268e11 2021-03-11 op wmove(minibuf, 0, off_x + ministate.off - skip);
1508 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1510 e19f9a04 2021-03-11 op redraw_tabline();
1511 e19f9a04 2021-03-11 op redraw_body(tab);
1512 e19f9a04 2021-03-11 op redraw_modeline(tab);
1513 e19f9a04 2021-03-11 op redraw_minibuffer();
1515 e19f9a04 2021-03-11 op restore_cursor(tab);
1516 e19f9a04 2021-03-11 op wrefresh(tabline);
1517 e19f9a04 2021-03-11 op wrefresh(modeline);
1519 e19f9a04 2021-03-11 op if (in_minibuffer) {
1520 e19f9a04 2021-03-11 op wrefresh(body);
1521 e19f9a04 2021-03-11 op wrefresh(minibuf);
1523 e19f9a04 2021-03-11 op wrefresh(minibuf);
1524 e19f9a04 2021-03-11 op wrefresh(body);
1529 e19f9a04 2021-03-11 op redraw_body(struct tab *tab)
1531 48e9d457 2021-03-06 op struct line *l;
1534 48e9d457 2021-03-06 op werase(body);
1536 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
1537 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
1541 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
1542 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
1543 48e9d457 2021-03-06 op wmove(body, line, 0);
1544 1d08c280 2021-03-06 op print_line(l);
1546 48e9d457 2021-03-06 op if (line == body_lines)
1552 7953dd72 2021-03-07 op message(const char *fmt, ...)
1556 7953dd72 2021-03-07 op if (clminibufev_set)
1557 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1558 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1559 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1560 7953dd72 2021-03-07 op clminibufev_set = 1;
1562 bf992581 2021-03-12 op free(ministate.curmesg);
1564 9cb0f9ce 2021-03-10 op va_start(ap, fmt);
1565 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1566 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1567 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1570 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1572 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1573 9cb0f9ce 2021-03-10 op wrefresh(body);
1574 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1576 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1577 9cb0f9ce 2021-03-10 op wrefresh(body);
1582 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1584 8af5e5ed 2021-03-08 op if (tab->s->loading_anim)
1586 8af5e5ed 2021-03-08 op tab->s->loading_anim = 1;
1587 8af5e5ed 2021-03-08 op evtimer_set(&tab->s->loadingev, update_loading_anim, tab);
1588 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1592 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1594 8af5e5ed 2021-03-08 op struct tab *tab = d;
1596 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = (tab->s->loading_anim_step+1)%4;
1598 8af5e5ed 2021-03-08 op redraw_modeline(tab);
1599 8af5e5ed 2021-03-08 op wrefresh(modeline);
1601 8af5e5ed 2021-03-08 op wrefresh(body);
1602 9ca15951 2021-03-09 op if (in_minibuffer)
1603 9ca15951 2021-03-09 op wrefresh(minibuf);
1605 8af5e5ed 2021-03-08 op evtimer_add(&tab->s->loadingev, &loadingev_timer);
1609 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1611 8af5e5ed 2021-03-08 op if (!tab->s->loading_anim)
1613 8af5e5ed 2021-03-08 op evtimer_del(&tab->s->loadingev);
1614 8af5e5ed 2021-03-08 op tab->s->loading_anim = 0;
1615 8af5e5ed 2021-03-08 op tab->s->loading_anim_step = 0;
1617 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1619 43a1b8d0 2021-03-09 op wrefresh(modeline);
1620 43a1b8d0 2021-03-09 op wrefresh(body);
1621 9ca15951 2021-03-09 op if (in_minibuffer)
1622 9ca15951 2021-03-09 op wrefresh(minibuf);
1626 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1628 43a1b8d0 2021-03-09 op empty_vlist(tab);
1629 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1630 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1631 8af5e5ed 2021-03-08 op load_url(tab, url);
1633 43a1b8d0 2021-03-09 op tab->s->curs_x = 0;
1634 43a1b8d0 2021-03-09 op tab->s->curs_y = 0;
1635 43a1b8d0 2021-03-09 op redraw_tab(tab);
1639 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1640 22268e11 2021-03-11 op void (*abortfn)(void), struct minibuf_histhead *hist)
1642 9ca15951 2021-03-09 op in_minibuffer = 1;
1643 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1644 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1646 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1648 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1649 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1650 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1651 9ca15951 2021-03-09 op ministate.off = 0;
1652 bb4e4ddf 2021-03-12 op ministate.len = 0;
1653 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1655 22268e11 2021-03-11 op ministate.history = hist;
1656 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1657 22268e11 2021-03-11 op ministate.hist_off = 0;
1661 b360ebb3 2021-03-10 op exit_minibuffer(void)
1663 156f1501 2021-03-13 op werase(minibuf);
1665 9ca15951 2021-03-09 op in_minibuffer = 0;
1666 9ca15951 2021-03-09 op base_map = &global_map;
1667 9ca15951 2021-03-09 op current_map = &global_map;
1671 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1673 5cd2ebb1 2021-03-11 op struct tab *t;
1675 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1676 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1679 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1682 b1df9b71 2021-03-12 op static struct tab *
1683 bcb0b073 2021-03-07 op new_tab(void)
1685 bcb0b073 2021-03-07 op struct tab *tab, *t;
1686 bcb0b073 2021-03-07 op const char *url = "about:new";
1688 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
1691 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
1694 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
1696 bcb0b073 2021-03-07 op tab->id = tab_counter++;
1697 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1699 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1700 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1702 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1704 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1708 bcb0b073 2021-03-07 op event_loopbreak();
1709 b1df9b71 2021-03-12 op return NULL;
1713 5e11c00c 2021-03-02 op ui_init(void)
1715 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1717 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1718 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1720 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1722 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1723 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1724 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1726 9ca15951 2021-03-09 op base_map = &global_map;
1727 f832146f 2021-03-09 op current_map = &global_map;
1728 f832146f 2021-03-09 op load_default_keys();
1735 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1737 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1739 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1741 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1743 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1746 48e9d457 2021-03-06 op body_lines = LINES-3;
1747 48e9d457 2021-03-06 op body_cols = COLS;
1749 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1750 48e9d457 2021-03-06 op scrollok(body, TRUE);
1752 5e11c00c 2021-03-02 op /* non-blocking input */
1753 48e9d457 2021-03-06 op wtimeout(body, 0);
1755 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1757 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1758 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1760 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1761 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1769 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1771 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1772 4d3785b1 2021-03-09 op message("Loaded %s", tab->urlstr);
1776 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1778 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
1781 1d08c280 2021-03-06 op wrap_page(tab);
1782 5e11c00c 2021-03-02 op redraw_tab(tab);
1786 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1788 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1789 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1791 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1792 22268e11 2021-03-11 op &ir_history);
1793 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1794 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1795 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1799 5e11c00c 2021-03-02 op ui_end(void)