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 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
82 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
83 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
84 941b3761 2021-03-18 op static void usage(void);
86 72b18268 2021-06-19 op static int x_offset;
88 2b2d2872 2021-06-20 op struct thiskey thiskey;
90 831deb20 2021-05-12 op static struct event resizeev;
91 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
93 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
95 2b2d2872 2021-06-20 op int body_lines, body_cols;
97 bddc7bbd 2021-04-01 op static WINDOW *help;
98 46f6e974 2021-05-17 op static struct buffer helpwin;
99 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
101 bddc7bbd 2021-04-01 op static int side_window;
103 48e9d457 2021-03-06 op static struct event clminibufev;
104 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
105 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
107 bcb0b073 2021-03-07 op static uint32_t tab_counter;
109 7c7d7bb7 2021-03-10 op static char keybuf[64];
111 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
112 b3575139 2021-04-01 op static unsigned int yornp_data;
114 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
115 de2a69bb 2021-05-17 op static unsigned int read_data;
117 9ca15951 2021-03-09 op struct kmap global_map,
118 fa3fd864 2021-03-10 op minibuffer_map,
119 9ca15951 2021-03-09 op *current_map,
122 2b2d2872 2021-06-20 op struct histhead eecmd_history,
125 de2a69bb 2021-05-17 op read_history;
127 2b2d2872 2021-06-20 op int in_minibuffer;
129 2b2d2872 2021-06-20 op struct ministate ministate;
131 72b18268 2021-06-19 op static inline void
132 923f9ce5 2021-06-21 op update_x_offset(void)
134 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
135 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
137 72b18268 2021-06-19 op x_offset = 0;
140 f832146f 2021-03-09 op static inline void
141 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
143 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
147 9ca15951 2021-03-09 op static inline void
148 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
150 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
155 f832146f 2021-03-09 op load_default_keys(void)
157 9ca15951 2021-03-09 op /* === global map === */
160 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
161 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
162 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
163 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
164 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
165 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
166 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
167 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
169 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
170 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
171 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
172 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
174 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
175 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
177 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
179 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
181 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
182 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
183 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
185 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
186 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
187 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
188 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
189 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
190 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
191 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
193 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
194 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
196 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
197 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
199 f832146f 2021-03-09 op /* vi/vi-like */
200 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
201 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
202 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
203 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
204 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
205 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
206 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
207 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
209 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
210 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
212 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
213 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
215 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
216 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
217 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
218 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
219 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
220 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
222 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
223 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
226 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
228 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
230 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
233 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
234 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
235 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
236 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
237 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
238 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
240 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
241 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
243 f832146f 2021-03-09 op /* "ncurses standard" */
244 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
247 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
248 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
249 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
250 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
251 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
252 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
254 fa3fd864 2021-03-10 op /* === minibuffer map === */
255 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
256 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
257 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
258 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
259 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
260 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
261 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
263 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
264 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
265 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
266 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
267 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
268 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
269 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
270 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
271 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
273 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
274 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
275 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
276 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
280 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
282 452589f7 2021-03-16 op struct vline *vl;
283 452589f7 2021-03-16 op const char *prfx;
285 46f6e974 2021-05-17 op vl = buffer->current_line;
286 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
287 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
289 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
291 72b18268 2021-06-19 op buffer->curs_x += x_offset;
293 452589f7 2021-03-16 op if (vl != NULL) {
294 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
295 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
300 870210fb 2021-03-26 op global_key_unbound(void)
302 870210fb 2021-03-26 op message("%s is undefined", keybuf);
306 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
308 3148eeac 2021-03-13 op struct hist *hist;
310 22268e11 2021-03-11 op if (ministate.history == NULL)
313 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
316 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
318 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
319 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
321 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
322 22268e11 2021-03-11 op ministate.history->len++;
326 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
327 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
328 22268e11 2021-03-11 op * navigation" thing.
331 22268e11 2021-03-11 op minibuffer_taint_hist(void)
333 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
336 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
337 22268e11 2021-03-11 op ministate.hist_cur = NULL;
341 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
343 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
346 22268e11 2021-03-11 op minibuffer_taint_hist();
348 17e5106b 2021-03-21 op if (thiskey.cp == 0)
351 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
352 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
353 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
356 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
357 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
358 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
362 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
364 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
365 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
366 5cd2ebb1 2021-03-11 op global_key_unbound();
370 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
374 b360ebb3 2021-03-10 op eecmd_select(void)
376 870210fb 2021-03-26 op struct cmds *cmd;
378 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
379 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
380 2fe98292 2021-03-26 op exit_minibuffer();
381 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
382 46f6e974 2021-05-17 op cmd->fn(current_buffer());
387 2fe98292 2021-03-26 op message("No match");
391 5cd2ebb1 2021-03-11 op ir_self_insert(void)
393 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
397 5cd2ebb1 2021-03-11 op ir_select(void)
399 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
400 31f1a758 2021-04-22 op struct phos_uri uri;
401 5cd2ebb1 2021-03-11 op struct tab *tab;
403 5cd2ebb1 2021-03-11 op tab = current_tab();
405 5cd2ebb1 2021-03-11 op exit_minibuffer();
406 22268e11 2021-03-11 op minibuffer_hist_save_entry();
408 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
409 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
410 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
411 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
412 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
416 5cd2ebb1 2021-03-11 op lu_self_insert(void)
418 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
419 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
420 5cd2ebb1 2021-03-11 op global_key_unbound();
424 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
428 5cd2ebb1 2021-03-11 op lu_select(void)
430 5cd2ebb1 2021-03-11 op exit_minibuffer();
431 22268e11 2021-03-11 op minibuffer_hist_save_entry();
432 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
436 740f578b 2021-03-15 op bp_select(void)
438 740f578b 2021-03-15 op exit_minibuffer();
439 740f578b 2021-03-15 op if (*ministate.buf != '\0')
440 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
442 740f578b 2021-03-15 op message("Abort.");
446 5d1bac73 2021-03-25 op yornp_self_insert(void)
448 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
449 5d1bac73 2021-03-25 op message("Please answer y or n");
453 5d1bac73 2021-03-25 op exit_minibuffer();
454 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
458 5d1bac73 2021-03-25 op yornp_abort(void)
460 5d1bac73 2021-03-25 op exit_minibuffer();
461 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
465 de2a69bb 2021-05-17 op read_self_insert(void)
467 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
468 de2a69bb 2021-05-17 op global_key_unbound();
472 de2a69bb 2021-05-17 op minibuffer_self_insert();
476 de2a69bb 2021-05-17 op read_abort(void)
478 de2a69bb 2021-05-17 op exit_minibuffer();
479 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
483 de2a69bb 2021-05-17 op read_select(void)
485 de2a69bb 2021-05-17 op exit_minibuffer();
486 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
487 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
490 9a25f829 2021-03-14 op static struct vline *
491 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
493 9a25f829 2021-03-14 op struct vline *vl;
497 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
503 48e9d457 2021-03-06 op /* unreachable */
508 5e11c00c 2021-03-02 op current_tab(void)
510 5e11c00c 2021-03-02 op struct tab *t;
512 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
513 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
517 5e11c00c 2021-03-02 op /* unreachable */
521 2b2d2872 2021-06-20 op struct buffer *
522 46f6e974 2021-05-17 op current_buffer(void)
524 2ba66cea 2021-03-22 op if (in_minibuffer)
525 46f6e974 2021-05-17 op return &ministate.buffer;
526 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
530 8947c1f2 2021-03-21 op readkey(void)
532 8947c1f2 2021-03-21 op uint32_t state = 0;
534 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
537 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
538 8947c1f2 2021-03-21 op if (thiskey.meta) {
539 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
540 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
541 c314a314 2021-03-11 op thiskey.meta = 0;
542 9ca15951 2021-03-09 op thiskey.key = 27;
546 8947c1f2 2021-03-21 op thiskey.cp = 0;
547 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
549 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
551 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
552 8947c1f2 2021-03-21 op message("Error decoding user input");
562 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
564 8947c1f2 2021-03-21 op struct keymap *k;
565 8947c1f2 2021-03-21 op const char *keyname;
566 8947c1f2 2021-03-21 op char tmp[5] = {0};
568 8947c1f2 2021-03-21 op if (!readkey())
571 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
572 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
573 7c7d7bb7 2021-03-10 op if (thiskey.meta)
574 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
575 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
576 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
577 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
579 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
580 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
582 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
583 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
587 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
588 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
589 9ca15951 2021-03-09 op k->key == thiskey.key) {
590 f832146f 2021-03-09 op if (k->fn == NULL)
591 f832146f 2021-03-09 op current_map = &k->map;
593 9ca15951 2021-03-09 op current_map = base_map;
594 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
595 46f6e974 2021-05-17 op k->fn(current_buffer());
601 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
602 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
604 7c7d7bb7 2021-03-10 op global_key_unbound();
606 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
607 9ca15951 2021-03-09 op current_map = base_map;
610 bddc7bbd 2021-04-01 op if (side_window)
611 bddc7bbd 2021-04-01 op recompute_help();
613 179f0f58 2021-03-11 op redraw_tab(current_tab());
617 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
619 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
620 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
622 9cb0f9ce 2021-03-10 op redraw_minibuffer();
623 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
624 9cb0f9ce 2021-03-10 op wrefresh(body);
625 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
627 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
628 9cb0f9ce 2021-03-10 op wrefresh(body);
633 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
635 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
636 831deb20 2021-05-12 op event_del(&resizeev);
638 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
639 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
643 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
645 1d08c280 2021-03-06 op struct tab *tab;
651 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
653 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
654 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
656 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
657 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
659 48e9d457 2021-03-06 op body_lines = LINES-3;
660 bd9637e9 2021-03-06 op body_cols = COLS;
662 bddc7bbd 2021-04-01 op if (side_window) {
663 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
664 bddc7bbd 2021-04-01 op help_lines = LINES-3;
665 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
666 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
668 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
670 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
671 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
673 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
675 72b18268 2021-06-19 op update_x_offset();
676 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
678 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
680 1d08c280 2021-03-06 op tab = current_tab();
682 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
683 1d08c280 2021-03-06 op redraw_tab(tab);
687 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
689 452589f7 2021-03-16 op struct line *l;
690 452589f7 2021-03-16 op const struct line *orig;
691 d511dc85 2021-03-16 op struct vline *vl;
692 72b18268 2021-06-19 op int pre_width;
693 452589f7 2021-03-16 op const char *prfx;
695 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
697 46f6e974 2021-05-17 op : buffer->current_line->parent;
698 46f6e974 2021-05-17 op buffer->current_line = NULL;
700 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
701 46f6e974 2021-05-17 op buffer->curs_y = 0;
702 46f6e974 2021-05-17 op buffer->line_off = 0;
704 46f6e974 2021-05-17 op empty_vlist(buffer);
706 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
707 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
708 5e11c00c 2021-03-02 op switch (l->type) {
709 5e11c00c 2021-03-02 op case LINE_TEXT:
710 5e11c00c 2021-03-02 op case LINE_LINK:
711 5e11c00c 2021-03-02 op case LINE_TITLE_1:
712 5e11c00c 2021-03-02 op case LINE_TITLE_2:
713 5e11c00c 2021-03-02 op case LINE_TITLE_3:
714 5e11c00c 2021-03-02 op case LINE_ITEM:
715 5e11c00c 2021-03-02 op case LINE_QUOTE:
716 5e11c00c 2021-03-02 op case LINE_PRE_START:
717 5e11c00c 2021-03-02 op case LINE_PRE_END:
718 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
720 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
721 72b18268 2021-06-19 op if (olivetti_mode)
722 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
724 72b18268 2021-06-19 op pre_width = width;
725 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
729 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
730 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
731 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
734 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
735 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
737 46f6e974 2021-05-17 op buffer->current_line = vl;
738 46f6e974 2021-05-17 op buffer->line_off--;
743 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
744 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
750 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
752 9a25f829 2021-03-14 op const char *text = vl->line;
753 768db5bb 2021-03-12 op const char *prfx;
754 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
755 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
757 9a25f829 2021-03-14 op if (!vl->flags)
758 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
760 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
762 bd9637e9 2021-03-06 op if (text == NULL)
765 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
766 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
767 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
769 bddc7bbd 2021-04-01 op wattron(window, text_face);
770 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
771 bddc7bbd 2021-04-01 op wattroff(window, text_face);
775 8af5e5ed 2021-03-08 op redraw_tabline(void)
777 7c7d7bb7 2021-03-10 op struct tab *tab;
778 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
779 8f127baa 2021-04-22 op int current, y, truncated;
780 dc5df781 2021-03-13 op const char *title;
781 119f393c 2021-03-16 op char buf[25];
785 923f9ce5 2021-06-21 op /* unused, but setted by a getyx */
788 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
789 8f127baa 2021-04-22 op space = COLS-2;
792 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
794 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
798 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
801 8f127baa 2021-04-22 op ots = toskip;
803 8f127baa 2021-04-22 op while (toskip != 0 &&
804 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
808 a636f50e 2021-03-16 op werase(tabline);
809 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
810 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
811 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
813 a636f50e 2021-03-16 op truncated = 0;
814 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
815 a636f50e 2021-03-16 op if (truncated)
817 a636f50e 2021-03-16 op if (toskip != 0) {
822 a636f50e 2021-03-16 op getyx(tabline, y, x);
823 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
824 a636f50e 2021-03-16 op truncated = 1;
826 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
828 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
829 2051e653 2021-03-13 op title = tab->hist_cur->h;
831 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
832 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
834 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
836 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
837 119f393c 2021-03-16 op /* truncation happens */
838 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
840 119f393c 2021-03-16 op /* pad with spaces */
841 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
846 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
848 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
850 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
851 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
852 119f393c 2021-03-16 op wprintw(tabline, " ");
855 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
857 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
860 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
861 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
862 119f393c 2021-03-16 op waddch(tabline, ' ');
863 a636f50e 2021-03-16 op if (truncated)
864 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
868 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
870 bddc7bbd 2021-04-01 op struct vline *vl;
874 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
875 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
876 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
878 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
879 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
884 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
885 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
886 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
888 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
892 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
893 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
894 72b18268 2021-06-19 op wmove(win, l, x_offset);
895 bddc7bbd 2021-04-01 op print_vline(win, vl);
897 bddc7bbd 2021-04-01 op if (l == height)
902 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
906 bddc7bbd 2021-04-01 op redraw_help(void)
908 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
912 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
914 3323faaf 2021-06-21 op static struct tab *last_tab;
916 3323faaf 2021-06-21 op if (last_tab != tab)
917 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
918 3323faaf 2021-06-21 op last_tab = tab;
920 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
923 10346511 2021-03-17 op static inline char
924 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
926 10346511 2021-03-17 op switch (ts) {
927 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
928 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
929 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
930 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
931 923f9ce5 2021-06-21 op default: return 'X';
936 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
939 48e9d457 2021-03-06 op int x, y, max_x, max_y;
940 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
941 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
943 156f1501 2021-03-13 op werase(modeline);
944 d5493194 2021-06-15 op wattron(modeline, modeline_face.background);
945 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
947 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
948 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
949 10346511 2021-03-17 op trust_status_char(tab->trust),
950 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
952 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
954 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
955 481340cc 2021-03-11 op wprintw(modeline, "All ");
956 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
957 481340cc 2021-03-11 op wprintw(modeline, "Top ");
958 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
959 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
961 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
963 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
964 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
965 46f6e974 2021-05-17 op tab->buffer.line_max,
966 2051e653 2021-03-13 op tab->hist_cur->h);
968 48e9d457 2021-03-06 op getyx(modeline, y, x);
969 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
974 48e9d457 2021-03-06 op for (; x < max_x; ++x)
975 48e9d457 2021-03-06 op waddstr(modeline, "-");
977 d5493194 2021-06-15 op wattroff(modeline, modeline_face.background);
981 9ca15951 2021-03-09 op redraw_minibuffer(void)
983 8300dd3c 2021-03-18 op struct tab *tab;
984 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
985 923f9ce5 2021-06-21 op char *start = NULL, *c = NULL;
987 923f9ce5 2021-06-21 op /* unused, but set by getyx */
990 d5493194 2021-06-15 op wattron(minibuf, minibuffer_face.background);
991 156f1501 2021-03-13 op werase(minibuf);
993 22268e11 2021-03-11 op if (in_minibuffer) {
994 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
995 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
996 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
997 22268e11 2021-03-11 op ministate.hist_off + 1,
998 22268e11 2021-03-11 op ministate.history->len);
1000 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1002 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1003 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1004 17e5106b 2021-03-21 op : ministate.buf;
1005 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1006 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1007 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1008 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1011 17e5106b 2021-03-21 op waddstr(minibuf, start);
1014 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1015 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1016 17e5106b 2021-03-21 op ministate.curmesg);
1018 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1019 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1021 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1022 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1023 8300dd3c 2021-03-18 op tab = current_tab();
1024 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1025 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1026 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1029 2aaf475e 2021-03-13 op if (in_minibuffer)
1030 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1032 d5493194 2021-06-15 op wattroff(minibuf, minibuffer_face.background);
1036 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1038 bddc7bbd 2021-04-01 op if (side_window) {
1039 bddc7bbd 2021-04-01 op redraw_help();
1040 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1043 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1045 e19f9a04 2021-03-11 op redraw_tabline();
1046 e19f9a04 2021-03-11 op redraw_body(tab);
1047 e19f9a04 2021-03-11 op redraw_modeline(tab);
1048 e19f9a04 2021-03-11 op redraw_minibuffer();
1050 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1051 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1053 e19f9a04 2021-03-11 op if (in_minibuffer) {
1054 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1055 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1057 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1058 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1065 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1067 bddc7bbd 2021-04-01 op struct line *l;
1068 bddc7bbd 2021-04-01 op struct cmds *cmd;
1070 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1071 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1074 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1076 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1079 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1080 bddc7bbd 2021-04-01 op l->alt = NULL;
1082 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1084 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1085 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1087 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1091 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1093 bddc7bbd 2021-04-01 op struct keymap *k;
1095 bddc7bbd 2021-04-01 op const char *kn;
1097 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1098 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1099 bddc7bbd 2021-04-01 op if (*p != '\0')
1100 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1101 bddc7bbd 2021-04-01 op if (k->meta)
1102 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1103 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1104 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1106 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1108 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1109 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1111 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1116 bddc7bbd 2021-04-01 op recompute_help(void)
1118 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1120 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1121 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1122 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1123 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1127 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1129 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1130 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1132 bf992581 2021-03-12 op free(ministate.curmesg);
1133 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1135 2b2d2872 2021-06-20 op if (fmt != NULL) {
1136 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1137 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1139 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1140 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1141 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1144 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1145 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1146 9cb0f9ce 2021-03-10 op wrefresh(body);
1147 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1149 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1150 9cb0f9ce 2021-03-10 op wrefresh(body);
1155 740f578b 2021-03-15 op message(const char *fmt, ...)
1159 740f578b 2021-03-15 op va_start(ap, fmt);
1160 740f578b 2021-03-15 op vmessage(fmt, ap);
1165 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1167 2ba66cea 2021-03-22 op if (tab->loading_anim)
1169 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1170 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1171 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1175 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1177 8af5e5ed 2021-03-08 op struct tab *tab = d;
1179 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1181 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1182 6347dcd0 2021-03-16 op redraw_modeline(tab);
1183 6347dcd0 2021-03-16 op wrefresh(modeline);
1184 6347dcd0 2021-03-16 op wrefresh(body);
1185 6347dcd0 2021-03-16 op if (in_minibuffer)
1186 6347dcd0 2021-03-16 op wrefresh(minibuf);
1189 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1193 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1195 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1197 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1198 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1199 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1201 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1204 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1206 43a1b8d0 2021-03-09 op wrefresh(modeline);
1207 43a1b8d0 2021-03-09 op wrefresh(body);
1208 9ca15951 2021-03-09 op if (in_minibuffer)
1209 9ca15951 2021-03-09 op wrefresh(minibuf);
1213 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1215 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1216 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1217 8af5e5ed 2021-03-08 op load_url(tab, url);
1219 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1220 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1221 43a1b8d0 2021-03-09 op redraw_tab(tab);
1225 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1226 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1228 9ca15951 2021-03-09 op in_minibuffer = 1;
1229 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1230 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1232 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1234 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1235 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1236 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1237 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1238 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1239 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1240 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1242 22268e11 2021-03-11 op ministate.history = hist;
1243 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1244 22268e11 2021-03-11 op ministate.hist_off = 0;
1248 b360ebb3 2021-03-10 op exit_minibuffer(void)
1250 156f1501 2021-03-13 op werase(minibuf);
1252 9ca15951 2021-03-09 op in_minibuffer = 0;
1253 9ca15951 2021-03-09 op base_map = &global_map;
1254 9ca15951 2021-03-09 op current_map = &global_map;
1258 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1260 5cd2ebb1 2021-03-11 op struct tab *t;
1262 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1263 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1266 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1267 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1270 2eef3403 2021-04-22 op unsigned int
1271 2eef3403 2021-04-22 op tab_new_id(void)
1273 2eef3403 2021-04-22 op return tab_counter++;
1276 2b2d2872 2021-06-20 op struct tab *
1277 1b81bc33 2021-03-15 op new_tab(const char *url)
1279 754622a2 2021-03-15 op struct tab *tab;
1281 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1282 71105afa 2021-03-16 op event_loopbreak();
1283 71105afa 2021-03-16 op return NULL;
1285 de2a69bb 2021-05-17 op tab->fd = -1;
1287 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1289 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1291 2eef3403 2021-04-22 op tab->id = tab_new_id();
1292 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1294 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1295 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1297 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1299 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1304 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1306 c7107cec 2021-04-01 op new_tab(url);
1312 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1313 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1317 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1319 c92e529c 2021-06-15 op char path[PATH_MAX];
1320 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1321 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1323 74a2587f 2021-06-21 op if (getenv("NO_COLOR") != NULL)
1324 74a2587f 2021-06-21 op enable_colors = 0;
1326 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1327 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1329 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1330 941b3761 2021-03-18 op switch (ch) {
1333 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1336 c92e529c 2021-06-15 op configtest = 1;
1346 941b3761 2021-03-18 op argc -= optind;
1347 941b3761 2021-03-18 op argv += optind;
1349 c92e529c 2021-06-15 op parseconfig(path, fonf);
1350 c92e529c 2021-06-15 op if (configtest){
1351 c92e529c 2021-06-15 op puts("config OK");
1355 941b3761 2021-03-18 op if (argc != 0)
1356 941b3761 2021-03-18 op url = argv[0];
1358 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1360 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1361 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1363 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1365 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1366 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1367 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1369 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1370 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1371 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1373 bddc7bbd 2021-04-01 op /* initialize help window */
1374 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1376 9ca15951 2021-03-09 op base_map = &global_map;
1377 f832146f 2021-03-09 op current_map = &global_map;
1378 f832146f 2021-03-09 op load_default_keys();
1381 74a2587f 2021-06-21 op /* char c; */
1382 74a2587f 2021-06-21 op /* printf("proceed?"); */
1383 74a2587f 2021-06-21 op /* read(0, &c, 1); */
1388 74a2587f 2021-06-21 op if (enable_colors) {
1389 74a2587f 2021-06-21 op if (has_colors()) {
1390 74a2587f 2021-06-21 op start_color();
1391 74a2587f 2021-06-21 op config_apply_colors();
1393 74a2587f 2021-06-21 op printf("has_color failed\n");
1394 74a2587f 2021-06-21 op enable_colors = 0;
1397 74a2587f 2021-06-21 op printf("color disabled\n");
1402 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1404 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1406 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1408 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1410 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1412 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1415 48e9d457 2021-03-06 op body_lines = LINES-3;
1416 48e9d457 2021-03-06 op body_cols = COLS;
1418 72b18268 2021-06-19 op update_x_offset();
1420 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1421 48e9d457 2021-03-06 op scrollok(body, TRUE);
1423 5e11c00c 2021-03-02 op /* non-blocking input */
1424 48e9d457 2021-03-06 op wtimeout(body, 0);
1426 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1428 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1429 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1431 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1432 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1434 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1435 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1436 c7107cec 2021-04-01 op new_tab(url);
1442 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1444 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1445 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1447 3d8c2326 2021-03-18 op redraw_tabline();
1448 3d8c2326 2021-03-18 op wrefresh(tabline);
1449 3d8c2326 2021-03-18 op if (in_minibuffer)
1450 3d8c2326 2021-03-18 op wrefresh(minibuf);
1452 3d8c2326 2021-03-18 op wrefresh(body);
1456 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1458 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1459 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1460 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1461 3d8c2326 2021-03-18 op redraw_tab(tab);
1463 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1466 2b2d2872 2021-06-20 op const char *
1467 2b2d2872 2021-06-20 op ui_keyname(int k)
1469 2b2d2872 2021-06-20 op return keyname(k);
1473 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1475 2b2d2872 2021-06-20 op side_window = !side_window;
1476 2b2d2872 2021-06-20 op if (side_window)
1477 2b2d2872 2021-06-20 op recompute_help();
1480 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1481 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1482 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1484 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1485 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1489 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1491 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1495 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1497 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1498 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1500 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1501 22268e11 2021-03-11 op &ir_history);
1502 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1503 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1504 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1508 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1509 b3575139 2021-04-01 op unsigned int data)
1513 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1514 6f66d9bf 2021-04-24 op fn(0, data);
1518 5d1bac73 2021-03-25 op yornp_cb = fn;
1519 e49ce157 2021-04-01 op yornp_data = data;
1520 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1521 5d1bac73 2021-03-25 op yornp_abort, NULL);
1523 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1524 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1525 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1526 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1530 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1531 de2a69bb 2021-05-17 op unsigned int data)
1535 de2a69bb 2021-05-17 op if (in_minibuffer)
1538 de2a69bb 2021-05-17 op read_cb = fn;
1539 de2a69bb 2021-05-17 op read_data = data;
1540 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1541 de2a69bb 2021-05-17 op &read_history);
1543 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1544 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1545 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1546 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1550 5e11c00c 2021-03-02 op ui_end(void)