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.
20 1d08c280 2021-03-06 op * Text scrolling
21 1d08c280 2021-03-06 op * ==============
23 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
24 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
25 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
26 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
27 1d08c280 2021-03-06 op * first/last line!
29 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
30 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
31 970deec6 2021-04-24 op * ``on-demand'', but it's still missing.
35 e2226342 2021-05-12 op #include "telescope.h"
36 09312eb3 2021-05-14 op #include "cmd.gen.h"
38 bddc7bbd 2021-04-01 op #include <assert.h>
39 5e11c00c 2021-03-02 op #include <curses.h>
40 5e11c00c 2021-03-02 op #include <event.h>
41 c92e529c 2021-06-15 op #include <limits.h>
42 5e11c00c 2021-03-02 op #include <locale.h>
43 5e11c00c 2021-03-02 op #include <signal.h>
44 7953dd72 2021-03-07 op #include <stdarg.h>
45 eb259e66 2021-03-02 op #include <stdlib.h>
46 eb259e66 2021-03-02 op #include <string.h>
47 f832146f 2021-03-09 op #include <unistd.h>
49 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
51 f832146f 2021-03-09 op static void load_default_keys(void);
53 870210fb 2021-03-26 op static void global_key_unbound(void);
54 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
55 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
56 5d1bac73 2021-03-25 op static void yornp_self_insert(void);
57 5d1bac73 2021-03-25 op static void yornp_abort(void);
58 de2a69bb 2021-05-17 op static void read_self_insert(void);
59 de2a69bb 2021-05-17 op static void read_abort(void);
60 de2a69bb 2021-05-17 op static void read_select(void);
62 46f6e974 2021-05-17 op static struct vline *nth_line(struct buffer*, size_t);
63 46f6e974 2021-05-17 op static struct buffer *current_buffer(void);
64 8947c1f2 2021-03-21 op static int readkey(void);
65 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
66 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
67 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
68 a8a482c8 2021-05-17 op static void handle_resize_nodelay(int, short, void*);
69 46f6e974 2021-05-17 op static int wrap_page(struct buffer*, int);
70 bddc7bbd 2021-04-01 op static void print_vline(WINDOW*, struct vline*);
71 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
72 46f6e974 2021-05-17 op static void redraw_window(WINDOW*, int, struct buffer*);
73 bddc7bbd 2021-04-01 op static void redraw_help(void);
74 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
75 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
76 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
77 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
78 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
79 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
80 bddc7bbd 2021-04-01 op static void recompute_help(void);
81 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
82 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
83 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
84 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
85 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
86 941b3761 2021-03-18 op static void usage(void);
88 72b18268 2021-06-19 op static int x_offset;
90 2b2d2872 2021-06-20 op struct thiskey thiskey;
92 831deb20 2021-05-12 op static struct event resizeev;
93 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
95 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
97 2b2d2872 2021-06-20 op int body_lines, body_cols;
99 bddc7bbd 2021-04-01 op static WINDOW *help;
100 46f6e974 2021-05-17 op static struct buffer helpwin;
101 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
103 bddc7bbd 2021-04-01 op static int side_window;
105 48e9d457 2021-03-06 op static struct event clminibufev;
106 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
107 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
109 bcb0b073 2021-03-07 op static uint32_t tab_counter;
111 7c7d7bb7 2021-03-10 op static char keybuf[64];
113 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
114 b3575139 2021-04-01 op static unsigned int yornp_data;
116 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
117 de2a69bb 2021-05-17 op static unsigned int read_data;
119 9ca15951 2021-03-09 op struct kmap global_map,
120 fa3fd864 2021-03-10 op minibuffer_map,
121 9ca15951 2021-03-09 op *current_map,
124 2b2d2872 2021-06-20 op struct histhead eecmd_history,
127 de2a69bb 2021-05-17 op read_history;
129 2b2d2872 2021-06-20 op int in_minibuffer;
131 2b2d2872 2021-06-20 op struct ministate ministate;
133 72b18268 2021-06-19 op static inline void
134 72b18268 2021-06-19 op update_x_offset()
136 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
137 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
139 72b18268 2021-06-19 op x_offset = 0;
142 f832146f 2021-03-09 op static inline void
143 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
145 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
149 9ca15951 2021-03-09 op static inline void
150 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
152 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
157 f832146f 2021-03-09 op load_default_keys(void)
159 9ca15951 2021-03-09 op /* === global map === */
162 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
163 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
164 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
165 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
166 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
167 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
168 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
169 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
171 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
172 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
173 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
174 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
176 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
177 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
179 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
181 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
183 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
184 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
185 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
187 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
188 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
189 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
190 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
191 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
192 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
193 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
195 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
196 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
198 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
199 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
201 f832146f 2021-03-09 op /* vi/vi-like */
202 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
203 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
204 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
205 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
206 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
207 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
208 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
209 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
211 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
212 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
214 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
215 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
217 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
218 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
219 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
220 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
221 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
222 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
224 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
225 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
228 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
230 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
232 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
235 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
236 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
237 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
238 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
239 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
240 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
242 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
243 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
245 f832146f 2021-03-09 op /* "ncurses standard" */
246 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
249 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
250 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
251 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
252 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
253 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
254 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
256 fa3fd864 2021-03-10 op /* === minibuffer map === */
257 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
258 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
259 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
260 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
261 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
262 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
263 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
265 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
266 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
267 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
268 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
269 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
270 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
271 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
272 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
273 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
275 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
276 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
277 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
278 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
282 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
284 452589f7 2021-03-16 op struct vline *vl;
285 452589f7 2021-03-16 op const char *prfx;
287 46f6e974 2021-05-17 op vl = buffer->current_line;
288 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
289 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
291 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
293 72b18268 2021-06-19 op buffer->curs_x += x_offset;
295 452589f7 2021-03-16 op if (vl != NULL) {
296 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
297 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
302 870210fb 2021-03-26 op global_key_unbound(void)
304 870210fb 2021-03-26 op message("%s is undefined", keybuf);
308 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
310 3148eeac 2021-03-13 op struct hist *hist;
312 22268e11 2021-03-11 op if (ministate.history == NULL)
315 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
318 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
320 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
321 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
323 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
324 22268e11 2021-03-11 op ministate.history->len++;
328 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
329 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
330 22268e11 2021-03-11 op * navigation" thing.
333 22268e11 2021-03-11 op minibuffer_taint_hist(void)
335 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
338 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
339 22268e11 2021-03-11 op ministate.hist_cur = NULL;
343 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
345 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
348 22268e11 2021-03-11 op minibuffer_taint_hist();
350 17e5106b 2021-03-21 op if (thiskey.cp == 0)
353 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
354 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
355 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
358 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
359 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
360 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
364 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
366 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
367 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
368 5cd2ebb1 2021-03-11 op global_key_unbound();
372 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
376 b360ebb3 2021-03-10 op eecmd_select(void)
378 870210fb 2021-03-26 op struct cmds *cmd;
380 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
381 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
382 2fe98292 2021-03-26 op exit_minibuffer();
383 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
384 46f6e974 2021-05-17 op cmd->fn(current_buffer());
389 2fe98292 2021-03-26 op message("No match");
393 5cd2ebb1 2021-03-11 op ir_self_insert(void)
395 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
399 5cd2ebb1 2021-03-11 op ir_select(void)
401 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
402 31f1a758 2021-04-22 op struct phos_uri uri;
403 5cd2ebb1 2021-03-11 op struct tab *tab;
405 5cd2ebb1 2021-03-11 op tab = current_tab();
407 5cd2ebb1 2021-03-11 op exit_minibuffer();
408 22268e11 2021-03-11 op minibuffer_hist_save_entry();
410 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
411 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
412 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
413 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
414 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
418 5cd2ebb1 2021-03-11 op lu_self_insert(void)
420 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
421 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
422 5cd2ebb1 2021-03-11 op global_key_unbound();
426 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
430 5cd2ebb1 2021-03-11 op lu_select(void)
432 5cd2ebb1 2021-03-11 op exit_minibuffer();
433 22268e11 2021-03-11 op minibuffer_hist_save_entry();
434 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
438 740f578b 2021-03-15 op bp_select(void)
440 740f578b 2021-03-15 op exit_minibuffer();
441 740f578b 2021-03-15 op if (*ministate.buf != '\0')
442 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
444 740f578b 2021-03-15 op message("Abort.");
448 5d1bac73 2021-03-25 op yornp_self_insert(void)
450 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
451 5d1bac73 2021-03-25 op message("Please answer y or n");
455 5d1bac73 2021-03-25 op exit_minibuffer();
456 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
460 5d1bac73 2021-03-25 op yornp_abort(void)
462 5d1bac73 2021-03-25 op exit_minibuffer();
463 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
467 de2a69bb 2021-05-17 op read_self_insert(void)
469 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
470 de2a69bb 2021-05-17 op global_key_unbound();
474 de2a69bb 2021-05-17 op minibuffer_self_insert();
478 de2a69bb 2021-05-17 op read_abort(void)
480 de2a69bb 2021-05-17 op exit_minibuffer();
481 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
485 de2a69bb 2021-05-17 op read_select(void)
487 de2a69bb 2021-05-17 op exit_minibuffer();
488 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
489 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
492 9a25f829 2021-03-14 op static struct vline *
493 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
495 9a25f829 2021-03-14 op struct vline *vl;
499 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
505 48e9d457 2021-03-06 op /* unreachable */
510 5e11c00c 2021-03-02 op current_tab(void)
512 5e11c00c 2021-03-02 op struct tab *t;
514 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
515 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
519 5e11c00c 2021-03-02 op /* unreachable */
523 2b2d2872 2021-06-20 op struct buffer *
524 46f6e974 2021-05-17 op current_buffer(void)
526 2ba66cea 2021-03-22 op if (in_minibuffer)
527 46f6e974 2021-05-17 op return &ministate.buffer;
528 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
532 8947c1f2 2021-03-21 op readkey(void)
534 8947c1f2 2021-03-21 op uint32_t state = 0;
536 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
539 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
540 8947c1f2 2021-03-21 op if (thiskey.meta) {
541 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
542 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
543 c314a314 2021-03-11 op thiskey.meta = 0;
544 9ca15951 2021-03-09 op thiskey.key = 27;
548 8947c1f2 2021-03-21 op thiskey.cp = 0;
549 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
551 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
553 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
554 8947c1f2 2021-03-21 op message("Error decoding user input");
564 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
566 8947c1f2 2021-03-21 op struct keymap *k;
567 8947c1f2 2021-03-21 op const char *keyname;
568 8947c1f2 2021-03-21 op char tmp[5] = {0};
570 8947c1f2 2021-03-21 op if (!readkey())
573 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
574 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
575 7c7d7bb7 2021-03-10 op if (thiskey.meta)
576 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
577 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
578 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
579 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
581 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
582 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
584 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
585 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
589 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
590 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
591 9ca15951 2021-03-09 op k->key == thiskey.key) {
592 f832146f 2021-03-09 op if (k->fn == NULL)
593 f832146f 2021-03-09 op current_map = &k->map;
595 9ca15951 2021-03-09 op current_map = base_map;
596 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
597 46f6e974 2021-05-17 op k->fn(current_buffer());
603 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
604 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
606 7c7d7bb7 2021-03-10 op global_key_unbound();
608 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
609 9ca15951 2021-03-09 op current_map = base_map;
612 bddc7bbd 2021-04-01 op if (side_window)
613 bddc7bbd 2021-04-01 op recompute_help();
615 179f0f58 2021-03-11 op redraw_tab(current_tab());
619 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
621 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
622 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
624 9cb0f9ce 2021-03-10 op redraw_minibuffer();
625 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
626 9cb0f9ce 2021-03-10 op wrefresh(body);
627 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
629 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
630 9cb0f9ce 2021-03-10 op wrefresh(body);
635 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
637 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
638 831deb20 2021-05-12 op event_del(&resizeev);
640 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
641 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
645 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
647 1d08c280 2021-03-06 op struct tab *tab;
653 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
655 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
656 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
658 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
659 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
661 48e9d457 2021-03-06 op body_lines = LINES-3;
662 bd9637e9 2021-03-06 op body_cols = COLS;
664 bddc7bbd 2021-04-01 op if (side_window) {
665 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
666 bddc7bbd 2021-04-01 op help_lines = LINES-3;
667 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
668 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
670 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
672 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
673 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
675 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
677 72b18268 2021-06-19 op update_x_offset();
678 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
680 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
682 1d08c280 2021-03-06 op tab = current_tab();
684 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
685 1d08c280 2021-03-06 op redraw_tab(tab);
689 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
691 452589f7 2021-03-16 op struct line *l;
692 452589f7 2021-03-16 op const struct line *orig;
693 d511dc85 2021-03-16 op struct vline *vl;
694 72b18268 2021-06-19 op int pre_width;
695 452589f7 2021-03-16 op const char *prfx;
697 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
699 46f6e974 2021-05-17 op : buffer->current_line->parent;
700 46f6e974 2021-05-17 op buffer->current_line = NULL;
702 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
703 46f6e974 2021-05-17 op buffer->curs_y = 0;
704 46f6e974 2021-05-17 op buffer->line_off = 0;
706 46f6e974 2021-05-17 op empty_vlist(buffer);
708 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
709 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
710 5e11c00c 2021-03-02 op switch (l->type) {
711 5e11c00c 2021-03-02 op case LINE_TEXT:
712 5e11c00c 2021-03-02 op case LINE_LINK:
713 5e11c00c 2021-03-02 op case LINE_TITLE_1:
714 5e11c00c 2021-03-02 op case LINE_TITLE_2:
715 5e11c00c 2021-03-02 op case LINE_TITLE_3:
716 5e11c00c 2021-03-02 op case LINE_ITEM:
717 5e11c00c 2021-03-02 op case LINE_QUOTE:
718 5e11c00c 2021-03-02 op case LINE_PRE_START:
719 5e11c00c 2021-03-02 op case LINE_PRE_END:
720 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
722 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
723 72b18268 2021-06-19 op if (olivetti_mode)
724 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
726 72b18268 2021-06-19 op pre_width = width;
727 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
731 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
732 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
733 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
736 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
737 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
739 46f6e974 2021-05-17 op buffer->current_line = vl;
740 46f6e974 2021-05-17 op buffer->line_off--;
745 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
746 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
752 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
754 9a25f829 2021-03-14 op const char *text = vl->line;
755 768db5bb 2021-03-12 op const char *prfx;
756 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
757 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
759 9a25f829 2021-03-14 op if (!vl->flags)
760 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
762 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
764 bd9637e9 2021-03-06 op if (text == NULL)
767 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
768 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
769 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
771 bddc7bbd 2021-04-01 op wattron(window, text_face);
772 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
773 bddc7bbd 2021-04-01 op wattroff(window, text_face);
777 8af5e5ed 2021-03-08 op redraw_tabline(void)
779 7c7d7bb7 2021-03-10 op struct tab *tab;
780 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
781 8f127baa 2021-04-22 op int current, y, truncated;
782 dc5df781 2021-03-13 op const char *title;
783 119f393c 2021-03-16 op char buf[25];
785 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
786 8f127baa 2021-04-22 op space = COLS-2;
789 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
791 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
795 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
798 8f127baa 2021-04-22 op ots = toskip;
800 8f127baa 2021-04-22 op while (toskip != 0 &&
801 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
805 a636f50e 2021-03-16 op werase(tabline);
806 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
807 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
808 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
810 a636f50e 2021-03-16 op truncated = 0;
811 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
812 a636f50e 2021-03-16 op if (truncated)
814 a636f50e 2021-03-16 op if (toskip != 0) {
819 a636f50e 2021-03-16 op getyx(tabline, y, x);
820 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
821 a636f50e 2021-03-16 op truncated = 1;
823 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
825 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
826 2051e653 2021-03-13 op title = tab->hist_cur->h;
828 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
829 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
831 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
833 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
834 119f393c 2021-03-16 op /* truncation happens */
835 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
837 119f393c 2021-03-16 op /* pad with spaces */
838 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
843 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
845 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
847 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
848 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
849 119f393c 2021-03-16 op wprintw(tabline, " ");
852 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
854 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
857 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
858 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
859 119f393c 2021-03-16 op waddch(tabline, ' ');
860 a636f50e 2021-03-16 op if (truncated)
861 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
865 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
867 bddc7bbd 2021-04-01 op struct vline *vl;
871 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
872 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
873 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
875 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
876 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
881 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
882 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
883 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
885 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
889 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
890 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
891 72b18268 2021-06-19 op wmove(win, l, x_offset);
892 bddc7bbd 2021-04-01 op print_vline(win, vl);
894 bddc7bbd 2021-04-01 op if (l == height)
899 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
903 bddc7bbd 2021-04-01 op redraw_help(void)
905 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
909 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
911 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
914 10346511 2021-03-17 op static inline char
915 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
917 10346511 2021-03-17 op switch (ts) {
918 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
919 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
920 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
921 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
926 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
929 48e9d457 2021-03-06 op int x, y, max_x, max_y;
930 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
931 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
933 156f1501 2021-03-13 op werase(modeline);
934 d5493194 2021-06-15 op wattron(modeline, modeline_face.background);
935 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
937 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
938 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
939 10346511 2021-03-17 op trust_status_char(tab->trust),
940 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
942 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
944 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
945 481340cc 2021-03-11 op wprintw(modeline, "All ");
946 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
947 481340cc 2021-03-11 op wprintw(modeline, "Top ");
948 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
949 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
951 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
953 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
954 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
955 46f6e974 2021-05-17 op tab->buffer.line_max,
956 2051e653 2021-03-13 op tab->hist_cur->h);
958 48e9d457 2021-03-06 op getyx(modeline, y, x);
959 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
964 48e9d457 2021-03-06 op for (; x < max_x; ++x)
965 48e9d457 2021-03-06 op waddstr(modeline, "-");
967 d5493194 2021-06-15 op wattroff(modeline, modeline_face.background);
971 9ca15951 2021-03-09 op redraw_minibuffer(void)
973 8300dd3c 2021-03-18 op struct tab *tab;
974 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
975 2ba66cea 2021-03-22 op char *start, *c;
977 d5493194 2021-06-15 op wattron(minibuf, minibuffer_face.background);
978 156f1501 2021-03-13 op werase(minibuf);
980 22268e11 2021-03-11 op if (in_minibuffer) {
981 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
982 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
983 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
984 22268e11 2021-03-11 op ministate.hist_off + 1,
985 22268e11 2021-03-11 op ministate.history->len);
987 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
989 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
990 17e5106b 2021-03-21 op ? ministate.hist_cur->h
991 17e5106b 2021-03-21 op : ministate.buf;
992 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
993 46f6e974 2021-05-17 op ministate.buffer.cpoff);
994 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
995 17e5106b 2021-03-21 op start = utf8_next_cp(start);
998 17e5106b 2021-03-21 op waddstr(minibuf, start);
1001 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1002 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1003 17e5106b 2021-03-21 op ministate.curmesg);
1005 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1006 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1008 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1009 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1010 8300dd3c 2021-03-18 op tab = current_tab();
1011 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1012 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1013 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1016 2aaf475e 2021-03-13 op if (in_minibuffer)
1017 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1019 d5493194 2021-06-15 op wattroff(minibuf, minibuffer_face.background);
1023 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1025 bddc7bbd 2021-04-01 op if (side_window) {
1026 bddc7bbd 2021-04-01 op redraw_help();
1027 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1030 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1032 e19f9a04 2021-03-11 op redraw_tabline();
1033 e19f9a04 2021-03-11 op redraw_body(tab);
1034 e19f9a04 2021-03-11 op redraw_modeline(tab);
1035 e19f9a04 2021-03-11 op redraw_minibuffer();
1037 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1038 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1040 e19f9a04 2021-03-11 op if (in_minibuffer) {
1041 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1042 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1044 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1045 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1052 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1054 bddc7bbd 2021-04-01 op struct line *l;
1055 bddc7bbd 2021-04-01 op struct cmds *cmd;
1057 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1058 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1061 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1063 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1066 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1067 bddc7bbd 2021-04-01 op l->alt = NULL;
1069 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1071 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1072 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1074 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1078 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1080 bddc7bbd 2021-04-01 op struct keymap *k;
1082 bddc7bbd 2021-04-01 op const char *kn;
1084 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1085 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1086 bddc7bbd 2021-04-01 op if (*p != '\0')
1087 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1088 bddc7bbd 2021-04-01 op if (k->meta)
1089 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1090 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1091 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1093 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1095 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1096 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1098 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1103 bddc7bbd 2021-04-01 op recompute_help(void)
1105 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1107 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1108 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1109 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1110 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1114 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1116 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1117 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1119 bf992581 2021-03-12 op free(ministate.curmesg);
1120 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1122 2b2d2872 2021-06-20 op if (fmt != NULL) {
1123 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1124 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1126 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1127 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1128 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1131 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1132 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1133 9cb0f9ce 2021-03-10 op wrefresh(body);
1134 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1136 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1137 9cb0f9ce 2021-03-10 op wrefresh(body);
1142 740f578b 2021-03-15 op message(const char *fmt, ...)
1146 740f578b 2021-03-15 op va_start(ap, fmt);
1147 740f578b 2021-03-15 op vmessage(fmt, ap);
1152 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1154 2ba66cea 2021-03-22 op if (tab->loading_anim)
1156 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1157 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1158 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1162 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1164 8af5e5ed 2021-03-08 op struct tab *tab = d;
1166 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1168 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1169 6347dcd0 2021-03-16 op redraw_modeline(tab);
1170 6347dcd0 2021-03-16 op wrefresh(modeline);
1171 6347dcd0 2021-03-16 op wrefresh(body);
1172 6347dcd0 2021-03-16 op if (in_minibuffer)
1173 6347dcd0 2021-03-16 op wrefresh(minibuf);
1176 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1180 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1182 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1184 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1185 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1186 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1188 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1191 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1193 43a1b8d0 2021-03-09 op wrefresh(modeline);
1194 43a1b8d0 2021-03-09 op wrefresh(body);
1195 9ca15951 2021-03-09 op if (in_minibuffer)
1196 9ca15951 2021-03-09 op wrefresh(minibuf);
1200 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1202 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1203 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1204 8af5e5ed 2021-03-08 op load_url(tab, url);
1206 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1207 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1208 43a1b8d0 2021-03-09 op redraw_tab(tab);
1212 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1213 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1215 9ca15951 2021-03-09 op in_minibuffer = 1;
1216 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1217 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1219 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1221 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1222 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1223 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1224 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1225 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1226 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1227 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1229 22268e11 2021-03-11 op ministate.history = hist;
1230 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1231 22268e11 2021-03-11 op ministate.hist_off = 0;
1235 b360ebb3 2021-03-10 op exit_minibuffer(void)
1237 156f1501 2021-03-13 op werase(minibuf);
1239 9ca15951 2021-03-09 op in_minibuffer = 0;
1240 9ca15951 2021-03-09 op base_map = &global_map;
1241 9ca15951 2021-03-09 op current_map = &global_map;
1245 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1247 5cd2ebb1 2021-03-11 op struct tab *t;
1249 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1250 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1253 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1254 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1257 2eef3403 2021-04-22 op unsigned int
1258 2eef3403 2021-04-22 op tab_new_id(void)
1260 2eef3403 2021-04-22 op return tab_counter++;
1263 2b2d2872 2021-06-20 op struct tab *
1264 1b81bc33 2021-03-15 op new_tab(const char *url)
1266 754622a2 2021-03-15 op struct tab *tab;
1268 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1269 71105afa 2021-03-16 op event_loopbreak();
1270 71105afa 2021-03-16 op return NULL;
1272 de2a69bb 2021-05-17 op tab->fd = -1;
1274 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1276 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1278 2eef3403 2021-04-22 op tab->id = tab_new_id();
1279 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1281 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1282 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1284 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1286 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1291 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1293 c7107cec 2021-04-01 op new_tab(url);
1299 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1300 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1304 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1306 c92e529c 2021-06-15 op char path[PATH_MAX];
1307 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1308 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1310 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1311 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1313 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1314 941b3761 2021-03-18 op switch (ch) {
1317 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1320 c92e529c 2021-06-15 op configtest = 1;
1330 941b3761 2021-03-18 op argc -= optind;
1331 941b3761 2021-03-18 op argv += optind;
1333 c92e529c 2021-06-15 op parseconfig(path, fonf);
1334 c92e529c 2021-06-15 op if (configtest){
1335 c92e529c 2021-06-15 op puts("config OK");
1339 941b3761 2021-03-18 op if (argc != 0)
1340 941b3761 2021-03-18 op url = argv[0];
1342 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1344 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1345 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1347 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1349 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1350 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1351 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1353 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1354 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1355 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1357 bddc7bbd 2021-04-01 op /* initialize help window */
1358 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1360 9ca15951 2021-03-09 op base_map = &global_map;
1361 f832146f 2021-03-09 op current_map = &global_map;
1362 f832146f 2021-03-09 op load_default_keys();
1369 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1371 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1373 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1375 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1377 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1379 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1382 48e9d457 2021-03-06 op body_lines = LINES-3;
1383 48e9d457 2021-03-06 op body_cols = COLS;
1385 72b18268 2021-06-19 op update_x_offset();
1387 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1388 48e9d457 2021-03-06 op scrollok(body, TRUE);
1390 5e11c00c 2021-03-02 op /* non-blocking input */
1391 48e9d457 2021-03-06 op wtimeout(body, 0);
1393 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1395 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1396 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1398 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1399 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1401 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1402 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1403 c7107cec 2021-04-01 op new_tab(url);
1409 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1411 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1412 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1414 3d8c2326 2021-03-18 op redraw_tabline();
1415 3d8c2326 2021-03-18 op wrefresh(tabline);
1416 3d8c2326 2021-03-18 op if (in_minibuffer)
1417 3d8c2326 2021-03-18 op wrefresh(minibuf);
1419 3d8c2326 2021-03-18 op wrefresh(body);
1423 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1425 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1426 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1427 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1428 3d8c2326 2021-03-18 op redraw_tab(tab);
1430 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1433 2b2d2872 2021-06-20 op const char *
1434 2b2d2872 2021-06-20 op ui_keyname(int k)
1436 2b2d2872 2021-06-20 op return keyname(k);
1440 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1442 2b2d2872 2021-06-20 op side_window = !side_window;
1443 2b2d2872 2021-06-20 op if (side_window)
1444 2b2d2872 2021-06-20 op recompute_help();
1448 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1450 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1454 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1456 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1457 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1459 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1460 22268e11 2021-03-11 op &ir_history);
1461 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1462 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1463 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1467 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1468 b3575139 2021-04-01 op unsigned int data)
1472 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1473 6f66d9bf 2021-04-24 op fn(0, data);
1477 5d1bac73 2021-03-25 op yornp_cb = fn;
1478 e49ce157 2021-04-01 op yornp_data = data;
1479 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1480 5d1bac73 2021-03-25 op yornp_abort, NULL);
1482 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1483 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1484 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1485 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1489 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1490 de2a69bb 2021-05-17 op unsigned int data)
1494 de2a69bb 2021-05-17 op if (in_minibuffer)
1497 de2a69bb 2021-05-17 op read_cb = fn;
1498 de2a69bb 2021-05-17 op read_data = data;
1499 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1500 de2a69bb 2021-05-17 op &read_history);
1502 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1503 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1504 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1505 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1509 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
1513 740f578b 2021-03-15 op va_start(ap, fmt);
1514 740f578b 2021-03-15 op vmessage(fmt, ap);
1519 5e11c00c 2021-03-02 op ui_end(void)