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.
27 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
28 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
29 970deec6 2021-04-24 op * ``on-demand'', but it's still missing.
33 e2226342 2021-05-12 op #include "telescope.h"
35 bddc7bbd 2021-04-01 op #include <assert.h>
36 5e11c00c 2021-03-02 op #include <curses.h>
37 5e11c00c 2021-03-02 op #include <event.h>
38 c92e529c 2021-06-15 op #include <limits.h>
39 5e11c00c 2021-03-02 op #include <locale.h>
40 5e11c00c 2021-03-02 op #include <signal.h>
41 7953dd72 2021-03-07 op #include <stdarg.h>
42 eb259e66 2021-03-02 op #include <stdlib.h>
43 eb259e66 2021-03-02 op #include <string.h>
44 f832146f 2021-03-09 op #include <unistd.h>
46 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
48 f832146f 2021-03-09 op static void load_default_keys(void);
50 870210fb 2021-03-26 op static void global_key_unbound(void);
51 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
52 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
53 5d1bac73 2021-03-25 op static void yornp_self_insert(void);
54 5d1bac73 2021-03-25 op static void yornp_abort(void);
55 de2a69bb 2021-05-17 op static void read_self_insert(void);
56 de2a69bb 2021-05-17 op static void read_abort(void);
57 de2a69bb 2021-05-17 op static void read_select(void);
59 46f6e974 2021-05-17 op static struct vline *nth_line(struct buffer*, size_t);
60 46f6e974 2021-05-17 op static struct buffer *current_buffer(void);
61 8947c1f2 2021-03-21 op static int readkey(void);
62 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
63 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
64 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
65 a8a482c8 2021-05-17 op static void handle_resize_nodelay(int, short, void*);
66 46f6e974 2021-05-17 op static int wrap_page(struct buffer*, int);
67 f3bcf8f2 2021-06-21 op static void print_vline(int, int, WINDOW*, struct vline*);
68 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
69 f3bcf8f2 2021-06-21 op static void redraw_window(WINDOW*, int, int, struct buffer*);
70 bddc7bbd 2021-04-01 op static void redraw_help(void);
71 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
72 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
73 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
74 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
75 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
76 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
77 bddc7bbd 2021-04-01 op static void recompute_help(void);
78 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
79 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
80 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
81 941b3761 2021-03-18 op static void usage(void);
83 72b18268 2021-06-19 op static int x_offset;
85 2b2d2872 2021-06-20 op struct thiskey thiskey;
87 831deb20 2021-05-12 op static struct event resizeev;
88 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
90 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
92 2b2d2872 2021-06-20 op int body_lines, body_cols;
94 bddc7bbd 2021-04-01 op static WINDOW *help;
95 46f6e974 2021-05-17 op static struct buffer helpwin;
96 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
98 bddc7bbd 2021-04-01 op static int side_window;
100 48e9d457 2021-03-06 op static struct event clminibufev;
101 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
102 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
104 bcb0b073 2021-03-07 op static uint32_t tab_counter;
106 7c7d7bb7 2021-03-10 op static char keybuf[64];
108 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
109 b3575139 2021-04-01 op static unsigned int yornp_data;
111 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
112 de2a69bb 2021-05-17 op static unsigned int read_data;
114 9ca15951 2021-03-09 op struct kmap global_map,
115 fa3fd864 2021-03-10 op minibuffer_map,
116 9ca15951 2021-03-09 op *current_map,
119 2b2d2872 2021-06-20 op struct histhead eecmd_history,
122 de2a69bb 2021-05-17 op read_history;
124 2b2d2872 2021-06-20 op int in_minibuffer;
126 2b2d2872 2021-06-20 op struct ministate ministate;
128 72b18268 2021-06-19 op static inline void
129 923f9ce5 2021-06-21 op update_x_offset(void)
131 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
132 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
134 72b18268 2021-06-19 op x_offset = 0;
137 f832146f 2021-03-09 op static inline void
138 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
140 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
144 9ca15951 2021-03-09 op static inline void
145 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
147 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
152 f832146f 2021-03-09 op load_default_keys(void)
154 9ca15951 2021-03-09 op /* === global map === */
157 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
158 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
159 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
160 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
161 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
162 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
163 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
164 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
166 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
167 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
168 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
169 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
171 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
172 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
174 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
176 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
178 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
180 1c412d48 2021-06-25 op global_set_key("C-c p", cmd_previous_heading);
181 1c412d48 2021-06-25 op global_set_key("C-c n", cmd_next_heading);
183 157d4810 2021-06-24 op global_set_key(">", cmd_load_url);
184 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
185 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
187 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
188 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
189 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
190 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
191 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
192 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
193 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
195 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
196 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
198 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
199 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
201 f832146f 2021-03-09 op /* vi/vi-like */
202 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
203 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
204 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
205 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
206 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
207 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
208 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
209 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
211 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
212 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
214 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
215 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
217 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
218 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
219 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
220 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
221 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
222 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
224 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
225 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
228 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
230 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
232 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
235 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
236 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
237 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
238 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
239 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
240 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
242 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
243 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
245 f832146f 2021-03-09 op /* "ncurses standard" */
246 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
249 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
250 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
251 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
252 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
253 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
254 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
256 fa3fd864 2021-03-10 op /* === minibuffer map === */
257 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
258 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
259 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
260 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
261 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
262 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
263 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
265 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
266 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
267 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
268 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
269 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
270 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
271 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
272 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
273 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
275 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
276 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
277 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
278 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
282 98411855 2021-06-23 op save_excursion(struct excursion *place, struct buffer *buffer)
284 98411855 2021-06-23 op place->curs_x = buffer->curs_x;
285 98411855 2021-06-23 op place->curs_y = buffer->curs_y;
286 98411855 2021-06-23 op place->line_off = buffer->line_off;
287 98411855 2021-06-23 op place->current_line = buffer->current_line;
288 98411855 2021-06-23 op place->cpoff = buffer->cpoff;
292 98411855 2021-06-23 op restore_excursion(struct excursion *place, struct buffer *buffer)
294 98411855 2021-06-23 op buffer->curs_x = place->curs_x;
295 98411855 2021-06-23 op buffer->curs_y = place->curs_y;
296 98411855 2021-06-23 op buffer->line_off = place->line_off;
297 98411855 2021-06-23 op buffer->current_line = place->current_line;
298 98411855 2021-06-23 op buffer->cpoff = place->cpoff;
302 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
304 452589f7 2021-03-16 op struct vline *vl;
305 452589f7 2021-03-16 op const char *prfx;
307 46f6e974 2021-05-17 op vl = buffer->current_line;
308 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
309 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
311 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
313 72b18268 2021-06-19 op buffer->curs_x += x_offset;
315 452589f7 2021-03-16 op if (vl != NULL) {
316 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
317 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
322 870210fb 2021-03-26 op global_key_unbound(void)
324 870210fb 2021-03-26 op message("%s is undefined", keybuf);
328 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
330 3148eeac 2021-03-13 op struct hist *hist;
332 22268e11 2021-03-11 op if (ministate.history == NULL)
335 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
338 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
340 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
341 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
343 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
344 22268e11 2021-03-11 op ministate.history->len++;
348 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
349 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
350 22268e11 2021-03-11 op * navigation" thing.
353 22268e11 2021-03-11 op minibuffer_taint_hist(void)
355 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
358 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
359 22268e11 2021-03-11 op ministate.hist_cur = NULL;
363 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
365 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
368 22268e11 2021-03-11 op minibuffer_taint_hist();
370 17e5106b 2021-03-21 op if (thiskey.cp == 0)
373 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
374 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
375 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
378 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
379 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
380 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
384 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
386 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
387 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
388 5cd2ebb1 2021-03-11 op global_key_unbound();
392 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
396 b360ebb3 2021-03-10 op eecmd_select(void)
398 a3666ed5 2021-06-25 op struct cmd *cmd;
400 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
401 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
402 2fe98292 2021-03-26 op exit_minibuffer();
403 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
404 46f6e974 2021-05-17 op cmd->fn(current_buffer());
409 2fe98292 2021-03-26 op message("No match");
413 5cd2ebb1 2021-03-11 op ir_self_insert(void)
415 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
419 5cd2ebb1 2021-03-11 op ir_select(void)
421 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
422 31f1a758 2021-04-22 op struct phos_uri uri;
423 5cd2ebb1 2021-03-11 op struct tab *tab;
425 5cd2ebb1 2021-03-11 op tab = current_tab();
427 5cd2ebb1 2021-03-11 op exit_minibuffer();
428 22268e11 2021-03-11 op minibuffer_hist_save_entry();
430 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
431 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
432 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
433 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
434 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
438 5cd2ebb1 2021-03-11 op lu_self_insert(void)
440 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
441 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
442 5cd2ebb1 2021-03-11 op global_key_unbound();
446 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
450 5cd2ebb1 2021-03-11 op lu_select(void)
452 5cd2ebb1 2021-03-11 op exit_minibuffer();
453 22268e11 2021-03-11 op minibuffer_hist_save_entry();
454 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
458 740f578b 2021-03-15 op bp_select(void)
460 740f578b 2021-03-15 op exit_minibuffer();
461 740f578b 2021-03-15 op if (*ministate.buf != '\0')
462 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
464 740f578b 2021-03-15 op message("Abort.");
468 5d1bac73 2021-03-25 op yornp_self_insert(void)
470 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
471 5d1bac73 2021-03-25 op message("Please answer y or n");
475 5d1bac73 2021-03-25 op exit_minibuffer();
476 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
480 5d1bac73 2021-03-25 op yornp_abort(void)
482 5d1bac73 2021-03-25 op exit_minibuffer();
483 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
487 de2a69bb 2021-05-17 op read_self_insert(void)
489 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
490 de2a69bb 2021-05-17 op global_key_unbound();
494 de2a69bb 2021-05-17 op minibuffer_self_insert();
498 de2a69bb 2021-05-17 op read_abort(void)
500 de2a69bb 2021-05-17 op exit_minibuffer();
501 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
505 de2a69bb 2021-05-17 op read_select(void)
507 de2a69bb 2021-05-17 op exit_minibuffer();
508 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
509 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
512 9a25f829 2021-03-14 op static struct vline *
513 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
515 9a25f829 2021-03-14 op struct vline *vl;
519 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
525 48e9d457 2021-03-06 op /* unreachable */
530 5e11c00c 2021-03-02 op current_tab(void)
532 5e11c00c 2021-03-02 op struct tab *t;
534 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
535 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
539 5e11c00c 2021-03-02 op /* unreachable */
543 2b2d2872 2021-06-20 op struct buffer *
544 46f6e974 2021-05-17 op current_buffer(void)
546 2ba66cea 2021-03-22 op if (in_minibuffer)
547 46f6e974 2021-05-17 op return &ministate.buffer;
548 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
552 8947c1f2 2021-03-21 op readkey(void)
554 8947c1f2 2021-03-21 op uint32_t state = 0;
556 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
559 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
560 8947c1f2 2021-03-21 op if (thiskey.meta) {
561 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
562 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
563 c314a314 2021-03-11 op thiskey.meta = 0;
564 9ca15951 2021-03-09 op thiskey.key = 27;
568 8947c1f2 2021-03-21 op thiskey.cp = 0;
569 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
571 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
573 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
574 8947c1f2 2021-03-21 op message("Error decoding user input");
584 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
586 8947c1f2 2021-03-21 op struct keymap *k;
587 8947c1f2 2021-03-21 op const char *keyname;
588 8947c1f2 2021-03-21 op char tmp[5] = {0};
590 8947c1f2 2021-03-21 op if (!readkey())
593 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
594 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
595 7c7d7bb7 2021-03-10 op if (thiskey.meta)
596 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
597 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
598 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
599 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
601 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
602 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
604 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
605 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
609 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
610 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
611 9ca15951 2021-03-09 op k->key == thiskey.key) {
612 f832146f 2021-03-09 op if (k->fn == NULL)
613 f832146f 2021-03-09 op current_map = &k->map;
615 9ca15951 2021-03-09 op current_map = base_map;
616 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
617 46f6e974 2021-05-17 op k->fn(current_buffer());
623 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
624 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
626 7c7d7bb7 2021-03-10 op global_key_unbound();
628 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
629 9ca15951 2021-03-09 op current_map = base_map;
632 bddc7bbd 2021-04-01 op if (side_window)
633 bddc7bbd 2021-04-01 op recompute_help();
635 179f0f58 2021-03-11 op redraw_tab(current_tab());
639 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
641 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
642 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
644 9cb0f9ce 2021-03-10 op redraw_minibuffer();
645 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
646 9cb0f9ce 2021-03-10 op wrefresh(body);
647 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
649 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
650 9cb0f9ce 2021-03-10 op wrefresh(body);
655 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
657 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
658 831deb20 2021-05-12 op event_del(&resizeev);
660 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
661 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
665 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
667 1d08c280 2021-03-06 op struct tab *tab;
673 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
675 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
676 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
678 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
679 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
681 48e9d457 2021-03-06 op body_lines = LINES-3;
682 bd9637e9 2021-03-06 op body_cols = COLS;
684 bddc7bbd 2021-04-01 op if (side_window) {
685 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
686 bddc7bbd 2021-04-01 op help_lines = LINES-3;
687 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
688 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
690 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
692 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
693 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
695 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
697 72b18268 2021-06-19 op update_x_offset();
698 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
700 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
702 1d08c280 2021-03-06 op tab = current_tab();
704 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
705 1d08c280 2021-03-06 op redraw_tab(tab);
709 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
711 452589f7 2021-03-16 op struct line *l;
712 452589f7 2021-03-16 op const struct line *orig;
713 d511dc85 2021-03-16 op struct vline *vl;
714 72b18268 2021-06-19 op int pre_width;
715 452589f7 2021-03-16 op const char *prfx;
717 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
719 46f6e974 2021-05-17 op : buffer->current_line->parent;
720 46f6e974 2021-05-17 op buffer->current_line = NULL;
722 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
723 46f6e974 2021-05-17 op buffer->curs_y = 0;
724 46f6e974 2021-05-17 op buffer->line_off = 0;
726 46f6e974 2021-05-17 op empty_vlist(buffer);
728 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
729 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
730 5e11c00c 2021-03-02 op switch (l->type) {
731 5e11c00c 2021-03-02 op case LINE_TEXT:
732 5e11c00c 2021-03-02 op case LINE_LINK:
733 5e11c00c 2021-03-02 op case LINE_TITLE_1:
734 5e11c00c 2021-03-02 op case LINE_TITLE_2:
735 5e11c00c 2021-03-02 op case LINE_TITLE_3:
736 5e11c00c 2021-03-02 op case LINE_ITEM:
737 5e11c00c 2021-03-02 op case LINE_QUOTE:
738 5e11c00c 2021-03-02 op case LINE_PRE_START:
739 5e11c00c 2021-03-02 op case LINE_PRE_END:
740 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
742 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
743 72b18268 2021-06-19 op if (olivetti_mode)
744 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
746 72b18268 2021-06-19 op pre_width = width;
747 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
751 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
752 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
753 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
756 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
757 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
759 46f6e974 2021-05-17 op buffer->current_line = vl;
760 46f6e974 2021-05-17 op buffer->line_off--;
765 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
766 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
772 f3bcf8f2 2021-06-21 op print_vline(int off, int width, WINDOW *window, struct vline *vl)
774 2af29222 2021-06-24 op const char *text;
775 768db5bb 2021-03-12 op const char *prfx;
776 2af29222 2021-06-24 op struct line_face *f;
777 f3bcf8f2 2021-06-21 op int i, left, x, y;
779 2af29222 2021-06-24 op f = &line_faces[vl->parent->type];
781 d89b86d6 2021-06-21 op /* unused, set by getyx */
784 9a25f829 2021-03-14 op if (!vl->flags)
785 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
787 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
789 2af29222 2021-06-24 op text = vl->line;
790 bd9637e9 2021-03-06 op if (text == NULL)
793 ab47e7d4 2021-06-24 op wattr_on(window, body_face.left, NULL);
794 f3bcf8f2 2021-06-21 op for (i = 0; i < off; i++)
795 f3bcf8f2 2021-06-21 op waddch(window, ' ');
796 ab47e7d4 2021-06-24 op wattr_off(window, body_face.left, NULL);
798 2af29222 2021-06-24 op wattr_on(window, f->prefix, NULL);
799 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
800 2af29222 2021-06-24 op wattr_off(window, f->prefix, NULL);
802 2af29222 2021-06-24 op wattr_on(window, f->text, NULL);
803 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
804 2af29222 2021-06-24 op wattr_off(window, f->text, NULL);
806 f3bcf8f2 2021-06-21 op getyx(window, y, x);
808 f3bcf8f2 2021-06-21 op left = width - x;
810 2af29222 2021-06-24 op wattr_on(window, f->trail, NULL);
811 d89b86d6 2021-06-21 op for (i = 0; i < left - off - 1; ++i)
812 f3bcf8f2 2021-06-21 op waddch(window, ' ');
813 2af29222 2021-06-24 op wattr_off(window, f->trail, NULL);
815 ab47e7d4 2021-06-24 op wattr_on(window, body_face.right, NULL);
816 160abe04 2021-06-21 op for (i = 0; i < off; i++)
817 160abe04 2021-06-21 op waddch(window, ' ');
818 ab47e7d4 2021-06-24 op wattr_off(window, body_face.right, NULL);
823 8af5e5ed 2021-03-08 op redraw_tabline(void)
825 7c7d7bb7 2021-03-10 op struct tab *tab;
826 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
827 8f127baa 2021-04-22 op int current, y, truncated;
828 dc5df781 2021-03-13 op const char *title;
829 119f393c 2021-03-16 op char buf[25];
833 923f9ce5 2021-06-21 op /* unused, but setted by a getyx */
836 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
837 8f127baa 2021-04-22 op space = COLS-2;
840 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
842 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
846 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
849 8f127baa 2021-04-22 op ots = toskip;
851 8f127baa 2021-04-22 op while (toskip != 0 &&
852 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
856 a636f50e 2021-03-16 op werase(tabline);
857 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
858 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
859 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
861 a636f50e 2021-03-16 op truncated = 0;
862 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
863 a636f50e 2021-03-16 op if (truncated)
865 a636f50e 2021-03-16 op if (toskip != 0) {
870 a636f50e 2021-03-16 op getyx(tabline, y, x);
871 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
872 a636f50e 2021-03-16 op truncated = 1;
874 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
876 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
877 2051e653 2021-03-13 op title = tab->hist_cur->h;
879 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
880 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
882 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
884 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
885 119f393c 2021-03-16 op /* truncation happens */
886 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
888 119f393c 2021-03-16 op /* pad with spaces */
889 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
894 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.current, NULL);
896 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.tab, NULL);
898 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
899 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
900 119f393c 2021-03-16 op wprintw(tabline, " ");
903 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.current, NULL);
905 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.tab, NULL);
908 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
909 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
910 119f393c 2021-03-16 op waddch(tabline, ' ');
911 a636f50e 2021-03-16 op if (truncated)
912 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
913 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
917 f3bcf8f2 2021-06-21 op redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
919 bddc7bbd 2021-04-01 op struct vline *vl;
923 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
924 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
925 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
927 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
928 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
933 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
934 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
935 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
937 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
941 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
942 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
943 f3bcf8f2 2021-06-21 op wmove(win, l, 0);
944 f3bcf8f2 2021-06-21 op print_vline(x_offset, width, win, vl);
946 bddc7bbd 2021-04-01 op if (l == height)
951 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
955 bddc7bbd 2021-04-01 op redraw_help(void)
957 f3bcf8f2 2021-06-21 op redraw_window(help, help_lines, help_cols, &helpwin);
961 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
963 3323faaf 2021-06-21 op static struct tab *last_tab;
965 3323faaf 2021-06-21 op if (last_tab != tab)
966 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
967 3323faaf 2021-06-21 op last_tab = tab;
969 f3bcf8f2 2021-06-21 op redraw_window(body, body_lines, body_cols, &tab->buffer);
972 10346511 2021-03-17 op static inline char
973 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
975 10346511 2021-03-17 op switch (ts) {
976 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
977 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
978 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
979 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
980 923f9ce5 2021-06-21 op default: return 'X';
985 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
988 48e9d457 2021-03-06 op int x, y, max_x, max_y;
989 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
990 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
992 156f1501 2021-03-13 op werase(modeline);
993 ab47e7d4 2021-06-24 op wattr_on(modeline, modeline_face.background, NULL);
994 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
996 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
997 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
998 10346511 2021-03-17 op trust_status_char(tab->trust),
999 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1001 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1003 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1004 481340cc 2021-03-11 op wprintw(modeline, "All ");
1005 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1006 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1007 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1008 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1010 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1012 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1013 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1014 46f6e974 2021-05-17 op tab->buffer.line_max,
1015 2051e653 2021-03-13 op tab->hist_cur->h);
1017 48e9d457 2021-03-06 op getyx(modeline, y, x);
1018 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1021 48e9d457 2021-03-06 op (void)max_y;
1023 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1024 48e9d457 2021-03-06 op waddstr(modeline, "-");
1026 ab47e7d4 2021-06-24 op wattr_off(modeline, modeline_face.background, NULL);
1030 9ca15951 2021-03-09 op redraw_minibuffer(void)
1032 8300dd3c 2021-03-18 op struct tab *tab;
1033 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1034 923f9ce5 2021-06-21 op char *start = NULL, *c = NULL;
1036 923f9ce5 2021-06-21 op /* unused, but set by getyx */
1037 923f9ce5 2021-06-21 op (void)off_y;
1039 ab47e7d4 2021-06-24 op wattr_on(minibuf, minibuffer_face.background, NULL);
1040 156f1501 2021-03-13 op werase(minibuf);
1042 22268e11 2021-03-11 op if (in_minibuffer) {
1043 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1044 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1045 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1046 22268e11 2021-03-11 op ministate.hist_off + 1,
1047 22268e11 2021-03-11 op ministate.history->len);
1049 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1051 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1052 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1053 17e5106b 2021-03-21 op : ministate.buf;
1054 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1055 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1056 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1057 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1060 17e5106b 2021-03-21 op waddstr(minibuf, start);
1063 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1064 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1065 17e5106b 2021-03-21 op ministate.curmesg);
1067 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1068 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1070 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1071 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1072 8300dd3c 2021-03-18 op tab = current_tab();
1073 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1074 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1075 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1078 2aaf475e 2021-03-13 op if (in_minibuffer)
1079 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1081 ab47e7d4 2021-06-24 op wattr_off(minibuf, minibuffer_face.background, NULL);
1085 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1087 bddc7bbd 2021-04-01 op if (side_window) {
1088 bddc7bbd 2021-04-01 op redraw_help();
1089 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1092 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1094 e19f9a04 2021-03-11 op redraw_tabline();
1095 e19f9a04 2021-03-11 op redraw_body(tab);
1096 e19f9a04 2021-03-11 op redraw_modeline(tab);
1097 e19f9a04 2021-03-11 op redraw_minibuffer();
1099 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1100 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1102 e19f9a04 2021-03-11 op if (in_minibuffer) {
1103 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1104 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1106 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1107 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1114 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1116 bddc7bbd 2021-04-01 op struct line *l;
1117 a3666ed5 2021-06-25 op struct cmd *cmd;
1119 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1120 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1123 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1125 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1128 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1129 bddc7bbd 2021-04-01 op l->alt = NULL;
1131 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1133 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1134 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1136 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1140 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1142 bddc7bbd 2021-04-01 op struct keymap *k;
1144 bddc7bbd 2021-04-01 op const char *kn;
1146 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1147 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1148 bddc7bbd 2021-04-01 op if (*p != '\0')
1149 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1150 bddc7bbd 2021-04-01 op if (k->meta)
1151 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1152 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1153 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1155 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1157 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1158 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1160 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1165 bddc7bbd 2021-04-01 op recompute_help(void)
1167 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1169 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1170 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1171 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1172 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1176 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1178 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1179 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1181 bf992581 2021-03-12 op free(ministate.curmesg);
1182 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1184 2b2d2872 2021-06-20 op if (fmt != NULL) {
1185 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1186 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1188 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1189 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1190 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1193 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1194 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1195 9cb0f9ce 2021-03-10 op wrefresh(body);
1196 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1198 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1199 9cb0f9ce 2021-03-10 op wrefresh(body);
1204 740f578b 2021-03-15 op message(const char *fmt, ...)
1208 740f578b 2021-03-15 op va_start(ap, fmt);
1209 740f578b 2021-03-15 op vmessage(fmt, ap);
1214 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1216 2ba66cea 2021-03-22 op if (tab->loading_anim)
1218 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1219 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1220 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1224 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1226 8af5e5ed 2021-03-08 op struct tab *tab = d;
1228 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1230 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1231 6347dcd0 2021-03-16 op redraw_modeline(tab);
1232 6347dcd0 2021-03-16 op wrefresh(modeline);
1233 6347dcd0 2021-03-16 op wrefresh(body);
1234 6347dcd0 2021-03-16 op if (in_minibuffer)
1235 6347dcd0 2021-03-16 op wrefresh(minibuf);
1238 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1242 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1244 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1246 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1247 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1248 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1250 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1253 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1255 43a1b8d0 2021-03-09 op wrefresh(modeline);
1256 43a1b8d0 2021-03-09 op wrefresh(body);
1257 9ca15951 2021-03-09 op if (in_minibuffer)
1258 9ca15951 2021-03-09 op wrefresh(minibuf);
1262 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1264 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1265 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1266 8af5e5ed 2021-03-08 op load_url(tab, url);
1268 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1269 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1270 43a1b8d0 2021-03-09 op redraw_tab(tab);
1274 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1275 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1277 9ca15951 2021-03-09 op in_minibuffer = 1;
1278 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1279 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1281 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1283 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1284 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1285 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1286 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1287 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1288 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1289 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1291 22268e11 2021-03-11 op ministate.history = hist;
1292 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1293 22268e11 2021-03-11 op ministate.hist_off = 0;
1297 b360ebb3 2021-03-10 op exit_minibuffer(void)
1299 156f1501 2021-03-13 op werase(minibuf);
1301 9ca15951 2021-03-09 op in_minibuffer = 0;
1302 9ca15951 2021-03-09 op base_map = &global_map;
1303 9ca15951 2021-03-09 op current_map = &global_map;
1307 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1309 5cd2ebb1 2021-03-11 op struct tab *t;
1311 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1312 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1315 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1316 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1319 2eef3403 2021-04-22 op unsigned int
1320 2eef3403 2021-04-22 op tab_new_id(void)
1322 2eef3403 2021-04-22 op return tab_counter++;
1325 2b2d2872 2021-06-20 op struct tab *
1326 1b81bc33 2021-03-15 op new_tab(const char *url)
1328 754622a2 2021-03-15 op struct tab *tab;
1330 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1331 71105afa 2021-03-16 op event_loopbreak();
1332 71105afa 2021-03-16 op return NULL;
1334 de2a69bb 2021-05-17 op tab->fd = -1;
1336 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1338 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1340 2eef3403 2021-04-22 op tab->id = tab_new_id();
1341 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1343 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1344 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1346 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1348 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1353 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1355 c7107cec 2021-04-01 op new_tab(url);
1361 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1362 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1366 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1368 c92e529c 2021-06-15 op char path[PATH_MAX];
1369 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1370 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1372 74a2587f 2021-06-21 op if (getenv("NO_COLOR") != NULL)
1373 74a2587f 2021-06-21 op enable_colors = 0;
1375 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1376 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1378 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1379 941b3761 2021-03-18 op switch (ch) {
1382 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1385 c92e529c 2021-06-15 op configtest = 1;
1395 941b3761 2021-03-18 op argc -= optind;
1396 941b3761 2021-03-18 op argv += optind;
1398 e3427d18 2021-06-25 op /* setup keys before reading the config */
1399 e3427d18 2021-06-25 op TAILQ_INIT(&global_map.m);
1400 e3427d18 2021-06-25 op global_map.unhandled_input = global_key_unbound;
1402 e3427d18 2021-06-25 op TAILQ_INIT(&minibuffer_map.m);
1404 e3427d18 2021-06-25 op load_default_keys();
1406 33fc3a8f 2021-06-21 op config_init();
1407 c92e529c 2021-06-15 op parseconfig(path, fonf);
1408 c92e529c 2021-06-15 op if (configtest){
1409 c92e529c 2021-06-15 op puts("config OK");
1413 941b3761 2021-03-18 op if (argc != 0)
1414 941b3761 2021-03-18 op url = argv[0];
1416 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1418 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1419 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1420 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1422 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1423 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1424 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1426 bddc7bbd 2021-04-01 op /* initialize help window */
1427 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1429 9ca15951 2021-03-09 op base_map = &global_map;
1430 f832146f 2021-03-09 op current_map = &global_map;
1434 74a2587f 2021-06-21 op if (enable_colors) {
1435 74a2587f 2021-06-21 op if (has_colors()) {
1436 74a2587f 2021-06-21 op start_color();
1437 160abe04 2021-06-21 op use_default_colors();
1439 74a2587f 2021-06-21 op enable_colors = 0;
1442 42d61f50 2021-06-24 op config_apply_style();
1447 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1449 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1451 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1453 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1455 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1457 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1460 48e9d457 2021-03-06 op body_lines = LINES-3;
1461 48e9d457 2021-03-06 op body_cols = COLS;
1463 b598590d 2021-06-21 op wbkgd(body, body_face.body);
1465 72b18268 2021-06-19 op update_x_offset();
1467 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1468 f3bcf8f2 2021-06-21 op scrollok(body, FALSE);
1470 5e11c00c 2021-03-02 op /* non-blocking input */
1471 48e9d457 2021-03-06 op wtimeout(body, 0);
1473 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1475 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1476 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1478 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1479 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1481 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1482 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1483 c7107cec 2021-04-01 op new_tab(url);
1489 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1491 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1492 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1494 3d8c2326 2021-03-18 op redraw_tabline();
1495 3d8c2326 2021-03-18 op wrefresh(tabline);
1496 3d8c2326 2021-03-18 op if (in_minibuffer)
1497 3d8c2326 2021-03-18 op wrefresh(minibuf);
1499 3d8c2326 2021-03-18 op wrefresh(body);
1503 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1505 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1506 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1507 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1508 3d8c2326 2021-03-18 op redraw_tab(tab);
1510 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1513 2b2d2872 2021-06-20 op const char *
1514 2b2d2872 2021-06-20 op ui_keyname(int k)
1516 2b2d2872 2021-06-20 op return keyname(k);
1520 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1522 2b2d2872 2021-06-20 op side_window = !side_window;
1523 2b2d2872 2021-06-20 op if (side_window)
1524 2b2d2872 2021-06-20 op recompute_help();
1527 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1528 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1529 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1531 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1532 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1536 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1538 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1542 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1544 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1545 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1547 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1548 22268e11 2021-03-11 op &ir_history);
1549 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1550 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1551 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1555 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1556 b3575139 2021-04-01 op unsigned int data)
1560 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1561 6f66d9bf 2021-04-24 op fn(0, data);
1565 5d1bac73 2021-03-25 op yornp_cb = fn;
1566 e49ce157 2021-04-01 op yornp_data = data;
1567 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1568 5d1bac73 2021-03-25 op yornp_abort, NULL);
1570 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1571 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1572 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1573 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1577 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1578 de2a69bb 2021-05-17 op unsigned int data)
1582 de2a69bb 2021-05-17 op if (in_minibuffer)
1585 de2a69bb 2021-05-17 op read_cb = fn;
1586 de2a69bb 2021-05-17 op read_data = data;
1587 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1588 de2a69bb 2021-05-17 op &read_history);
1590 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1591 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1592 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1593 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1597 5e11c00c 2021-03-02 op ui_end(void)