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 72b18268 2021-06-19 op update_x_offset()
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];
783 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
784 8f127baa 2021-04-22 op space = COLS-2;
787 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
789 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
793 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
796 8f127baa 2021-04-22 op ots = toskip;
798 8f127baa 2021-04-22 op while (toskip != 0 &&
799 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
803 a636f50e 2021-03-16 op werase(tabline);
804 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
805 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
806 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
808 a636f50e 2021-03-16 op truncated = 0;
809 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
810 a636f50e 2021-03-16 op if (truncated)
812 a636f50e 2021-03-16 op if (toskip != 0) {
817 a636f50e 2021-03-16 op getyx(tabline, y, x);
818 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
819 a636f50e 2021-03-16 op truncated = 1;
821 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
823 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
824 2051e653 2021-03-13 op title = tab->hist_cur->h;
826 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
827 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
829 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
831 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
832 119f393c 2021-03-16 op /* truncation happens */
833 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
835 119f393c 2021-03-16 op /* pad with spaces */
836 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
841 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
843 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
845 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
846 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
847 119f393c 2021-03-16 op wprintw(tabline, " ");
850 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
852 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
855 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
856 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
857 119f393c 2021-03-16 op waddch(tabline, ' ');
858 a636f50e 2021-03-16 op if (truncated)
859 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
863 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
865 bddc7bbd 2021-04-01 op struct vline *vl;
869 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
870 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
871 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
873 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
874 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
879 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
880 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
881 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
883 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
887 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
888 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
889 72b18268 2021-06-19 op wmove(win, l, x_offset);
890 bddc7bbd 2021-04-01 op print_vline(win, vl);
892 bddc7bbd 2021-04-01 op if (l == height)
897 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
901 bddc7bbd 2021-04-01 op redraw_help(void)
903 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
907 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
909 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
912 10346511 2021-03-17 op static inline char
913 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
915 10346511 2021-03-17 op switch (ts) {
916 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
917 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
918 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
919 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
924 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
927 48e9d457 2021-03-06 op int x, y, max_x, max_y;
928 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
929 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
931 156f1501 2021-03-13 op werase(modeline);
932 d5493194 2021-06-15 op wattron(modeline, modeline_face.background);
933 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
935 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
936 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
937 10346511 2021-03-17 op trust_status_char(tab->trust),
938 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
940 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
942 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
943 481340cc 2021-03-11 op wprintw(modeline, "All ");
944 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
945 481340cc 2021-03-11 op wprintw(modeline, "Top ");
946 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
947 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
949 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
951 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
952 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
953 46f6e974 2021-05-17 op tab->buffer.line_max,
954 2051e653 2021-03-13 op tab->hist_cur->h);
956 48e9d457 2021-03-06 op getyx(modeline, y, x);
957 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
962 48e9d457 2021-03-06 op for (; x < max_x; ++x)
963 48e9d457 2021-03-06 op waddstr(modeline, "-");
965 d5493194 2021-06-15 op wattroff(modeline, modeline_face.background);
969 9ca15951 2021-03-09 op redraw_minibuffer(void)
971 8300dd3c 2021-03-18 op struct tab *tab;
972 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
973 2ba66cea 2021-03-22 op char *start, *c;
975 d5493194 2021-06-15 op wattron(minibuf, minibuffer_face.background);
976 156f1501 2021-03-13 op werase(minibuf);
978 22268e11 2021-03-11 op if (in_minibuffer) {
979 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
980 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
981 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
982 22268e11 2021-03-11 op ministate.hist_off + 1,
983 22268e11 2021-03-11 op ministate.history->len);
985 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
987 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
988 17e5106b 2021-03-21 op ? ministate.hist_cur->h
989 17e5106b 2021-03-21 op : ministate.buf;
990 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
991 46f6e974 2021-05-17 op ministate.buffer.cpoff);
992 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
993 17e5106b 2021-03-21 op start = utf8_next_cp(start);
996 17e5106b 2021-03-21 op waddstr(minibuf, start);
999 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1000 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1001 17e5106b 2021-03-21 op ministate.curmesg);
1003 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1004 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1006 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1007 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1008 8300dd3c 2021-03-18 op tab = current_tab();
1009 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1010 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1011 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1014 2aaf475e 2021-03-13 op if (in_minibuffer)
1015 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1017 d5493194 2021-06-15 op wattroff(minibuf, minibuffer_face.background);
1021 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1023 bddc7bbd 2021-04-01 op if (side_window) {
1024 bddc7bbd 2021-04-01 op redraw_help();
1025 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1028 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1030 e19f9a04 2021-03-11 op redraw_tabline();
1031 e19f9a04 2021-03-11 op redraw_body(tab);
1032 e19f9a04 2021-03-11 op redraw_modeline(tab);
1033 e19f9a04 2021-03-11 op redraw_minibuffer();
1035 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1036 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1038 e19f9a04 2021-03-11 op if (in_minibuffer) {
1039 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1040 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1042 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1043 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1050 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1052 bddc7bbd 2021-04-01 op struct line *l;
1053 bddc7bbd 2021-04-01 op struct cmds *cmd;
1055 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1056 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1059 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1061 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1064 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1065 bddc7bbd 2021-04-01 op l->alt = NULL;
1067 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1069 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1070 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1072 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1076 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1078 bddc7bbd 2021-04-01 op struct keymap *k;
1080 bddc7bbd 2021-04-01 op const char *kn;
1082 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1083 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1084 bddc7bbd 2021-04-01 op if (*p != '\0')
1085 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1086 bddc7bbd 2021-04-01 op if (k->meta)
1087 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1088 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1089 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1091 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1093 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1094 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1096 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1101 bddc7bbd 2021-04-01 op recompute_help(void)
1103 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1105 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1106 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1107 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1108 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1112 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1114 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1115 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1117 bf992581 2021-03-12 op free(ministate.curmesg);
1118 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1120 2b2d2872 2021-06-20 op if (fmt != NULL) {
1121 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1122 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1124 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1125 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1126 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1129 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1130 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1131 9cb0f9ce 2021-03-10 op wrefresh(body);
1132 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1134 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1135 9cb0f9ce 2021-03-10 op wrefresh(body);
1140 740f578b 2021-03-15 op message(const char *fmt, ...)
1144 740f578b 2021-03-15 op va_start(ap, fmt);
1145 740f578b 2021-03-15 op vmessage(fmt, ap);
1150 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1152 2ba66cea 2021-03-22 op if (tab->loading_anim)
1154 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1155 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1156 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1160 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1162 8af5e5ed 2021-03-08 op struct tab *tab = d;
1164 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1166 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1167 6347dcd0 2021-03-16 op redraw_modeline(tab);
1168 6347dcd0 2021-03-16 op wrefresh(modeline);
1169 6347dcd0 2021-03-16 op wrefresh(body);
1170 6347dcd0 2021-03-16 op if (in_minibuffer)
1171 6347dcd0 2021-03-16 op wrefresh(minibuf);
1174 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1178 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1180 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1182 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1183 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1184 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1186 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1189 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1191 43a1b8d0 2021-03-09 op wrefresh(modeline);
1192 43a1b8d0 2021-03-09 op wrefresh(body);
1193 9ca15951 2021-03-09 op if (in_minibuffer)
1194 9ca15951 2021-03-09 op wrefresh(minibuf);
1198 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1200 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1201 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1202 8af5e5ed 2021-03-08 op load_url(tab, url);
1204 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1205 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1206 43a1b8d0 2021-03-09 op redraw_tab(tab);
1210 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1211 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1213 9ca15951 2021-03-09 op in_minibuffer = 1;
1214 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1215 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1217 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1219 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1220 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1221 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1222 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1223 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1224 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1225 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1227 22268e11 2021-03-11 op ministate.history = hist;
1228 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1229 22268e11 2021-03-11 op ministate.hist_off = 0;
1233 b360ebb3 2021-03-10 op exit_minibuffer(void)
1235 156f1501 2021-03-13 op werase(minibuf);
1237 9ca15951 2021-03-09 op in_minibuffer = 0;
1238 9ca15951 2021-03-09 op base_map = &global_map;
1239 9ca15951 2021-03-09 op current_map = &global_map;
1243 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1245 5cd2ebb1 2021-03-11 op struct tab *t;
1247 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1248 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1251 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1252 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1255 2eef3403 2021-04-22 op unsigned int
1256 2eef3403 2021-04-22 op tab_new_id(void)
1258 2eef3403 2021-04-22 op return tab_counter++;
1261 2b2d2872 2021-06-20 op struct tab *
1262 1b81bc33 2021-03-15 op new_tab(const char *url)
1264 754622a2 2021-03-15 op struct tab *tab;
1266 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1267 71105afa 2021-03-16 op event_loopbreak();
1268 71105afa 2021-03-16 op return NULL;
1270 de2a69bb 2021-05-17 op tab->fd = -1;
1272 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1274 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1276 2eef3403 2021-04-22 op tab->id = tab_new_id();
1277 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1279 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1280 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1282 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1284 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1289 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1291 c7107cec 2021-04-01 op new_tab(url);
1297 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1298 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1302 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1304 c92e529c 2021-06-15 op char path[PATH_MAX];
1305 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1306 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1308 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1309 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1311 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1312 941b3761 2021-03-18 op switch (ch) {
1315 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1318 c92e529c 2021-06-15 op configtest = 1;
1328 941b3761 2021-03-18 op argc -= optind;
1329 941b3761 2021-03-18 op argv += optind;
1331 c92e529c 2021-06-15 op parseconfig(path, fonf);
1332 c92e529c 2021-06-15 op if (configtest){
1333 c92e529c 2021-06-15 op puts("config OK");
1337 941b3761 2021-03-18 op if (argc != 0)
1338 941b3761 2021-03-18 op url = argv[0];
1340 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1342 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1343 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1345 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1347 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1348 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1349 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1351 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1352 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1353 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1355 bddc7bbd 2021-04-01 op /* initialize help window */
1356 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1358 9ca15951 2021-03-09 op base_map = &global_map;
1359 f832146f 2021-03-09 op current_map = &global_map;
1360 f832146f 2021-03-09 op load_default_keys();
1367 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1369 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1371 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1373 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1375 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1377 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1380 48e9d457 2021-03-06 op body_lines = LINES-3;
1381 48e9d457 2021-03-06 op body_cols = COLS;
1383 72b18268 2021-06-19 op update_x_offset();
1385 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1386 48e9d457 2021-03-06 op scrollok(body, TRUE);
1388 5e11c00c 2021-03-02 op /* non-blocking input */
1389 48e9d457 2021-03-06 op wtimeout(body, 0);
1391 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1393 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1394 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1396 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1397 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1399 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1400 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1401 c7107cec 2021-04-01 op new_tab(url);
1407 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1409 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1410 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1412 3d8c2326 2021-03-18 op redraw_tabline();
1413 3d8c2326 2021-03-18 op wrefresh(tabline);
1414 3d8c2326 2021-03-18 op if (in_minibuffer)
1415 3d8c2326 2021-03-18 op wrefresh(minibuf);
1417 3d8c2326 2021-03-18 op wrefresh(body);
1421 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1423 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1424 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1425 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1426 3d8c2326 2021-03-18 op redraw_tab(tab);
1428 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1431 2b2d2872 2021-06-20 op const char *
1432 2b2d2872 2021-06-20 op ui_keyname(int k)
1434 2b2d2872 2021-06-20 op return keyname(k);
1438 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1440 2b2d2872 2021-06-20 op side_window = !side_window;
1441 2b2d2872 2021-06-20 op if (side_window)
1442 2b2d2872 2021-06-20 op recompute_help();
1445 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1446 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1447 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1449 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1450 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1454 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1456 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1460 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1462 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1463 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1465 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1466 22268e11 2021-03-11 op &ir_history);
1467 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1468 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1469 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1473 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1474 b3575139 2021-04-01 op unsigned int data)
1478 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1479 6f66d9bf 2021-04-24 op fn(0, data);
1483 5d1bac73 2021-03-25 op yornp_cb = fn;
1484 e49ce157 2021-04-01 op yornp_data = data;
1485 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1486 5d1bac73 2021-03-25 op yornp_abort, NULL);
1488 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1489 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1490 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1491 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1495 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1496 de2a69bb 2021-05-17 op unsigned int data)
1500 de2a69bb 2021-05-17 op if (in_minibuffer)
1503 de2a69bb 2021-05-17 op read_cb = fn;
1504 de2a69bb 2021-05-17 op read_data = data;
1505 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1506 de2a69bb 2021-05-17 op &read_history);
1508 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1509 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1510 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1511 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1515 5e11c00c 2021-03-02 op ui_end(void)