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 f3bcf8f2 2021-06-21 op static void print_vline(int, int, WINDOW*, struct vline*);
71 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
72 f3bcf8f2 2021-06-21 op static void redraw_window(WINDOW*, int, int, struct buffer*);
73 bddc7bbd 2021-04-01 op static void redraw_help(void);
74 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
75 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
76 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
77 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
78 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
79 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
80 bddc7bbd 2021-04-01 op static void recompute_help(void);
81 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
82 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
83 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
84 941b3761 2021-03-18 op static void usage(void);
86 72b18268 2021-06-19 op static int x_offset;
88 2b2d2872 2021-06-20 op struct thiskey thiskey;
90 831deb20 2021-05-12 op static struct event resizeev;
91 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
93 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
95 2b2d2872 2021-06-20 op int body_lines, body_cols;
97 bddc7bbd 2021-04-01 op static WINDOW *help;
98 46f6e974 2021-05-17 op static struct buffer helpwin;
99 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
101 bddc7bbd 2021-04-01 op static int side_window;
103 48e9d457 2021-03-06 op static struct event clminibufev;
104 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
105 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
107 bcb0b073 2021-03-07 op static uint32_t tab_counter;
109 7c7d7bb7 2021-03-10 op static char keybuf[64];
111 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
112 b3575139 2021-04-01 op static unsigned int yornp_data;
114 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
115 de2a69bb 2021-05-17 op static unsigned int read_data;
117 9ca15951 2021-03-09 op struct kmap global_map,
118 fa3fd864 2021-03-10 op minibuffer_map,
119 9ca15951 2021-03-09 op *current_map,
122 2b2d2872 2021-06-20 op struct histhead eecmd_history,
125 de2a69bb 2021-05-17 op read_history;
127 2b2d2872 2021-06-20 op int in_minibuffer;
129 2b2d2872 2021-06-20 op struct ministate ministate;
131 72b18268 2021-06-19 op static inline void
132 923f9ce5 2021-06-21 op update_x_offset(void)
134 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
135 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
137 72b18268 2021-06-19 op x_offset = 0;
140 f832146f 2021-03-09 op static inline void
141 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
143 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
147 9ca15951 2021-03-09 op static inline void
148 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
150 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
155 f832146f 2021-03-09 op load_default_keys(void)
157 9ca15951 2021-03-09 op /* === global map === */
160 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
161 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
162 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
163 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
164 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
165 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
166 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
167 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
169 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
170 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
171 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
172 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
174 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
175 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
177 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
179 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
181 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
182 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
183 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
185 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
186 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
187 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
188 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
189 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
190 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
191 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
193 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
194 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
196 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
197 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
199 f832146f 2021-03-09 op /* vi/vi-like */
200 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
201 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
202 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
203 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
204 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
205 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
206 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
207 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
209 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
210 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
212 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
213 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
215 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
216 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
217 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
218 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
219 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
220 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
222 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
223 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
226 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
228 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
230 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
233 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
234 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
235 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
236 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
237 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
238 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
240 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
241 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
243 f832146f 2021-03-09 op /* "ncurses standard" */
244 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
247 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
248 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
249 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
250 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
251 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
252 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
254 fa3fd864 2021-03-10 op /* === minibuffer map === */
255 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
256 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
257 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
258 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
259 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
260 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
261 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
263 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
264 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
265 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
266 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
267 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
268 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
269 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
270 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
271 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
273 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
274 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
275 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
276 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
280 98411855 2021-06-23 op save_excursion(struct excursion *place, struct buffer *buffer)
282 98411855 2021-06-23 op place->curs_x = buffer->curs_x;
283 98411855 2021-06-23 op place->curs_y = buffer->curs_y;
284 98411855 2021-06-23 op place->line_off = buffer->line_off;
285 98411855 2021-06-23 op place->current_line = buffer->current_line;
286 98411855 2021-06-23 op place->cpoff = buffer->cpoff;
290 98411855 2021-06-23 op restore_excursion(struct excursion *place, struct buffer *buffer)
292 98411855 2021-06-23 op buffer->curs_x = place->curs_x;
293 98411855 2021-06-23 op buffer->curs_y = place->curs_y;
294 98411855 2021-06-23 op buffer->line_off = place->line_off;
295 98411855 2021-06-23 op buffer->current_line = place->current_line;
296 98411855 2021-06-23 op buffer->cpoff = place->cpoff;
300 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
302 452589f7 2021-03-16 op struct vline *vl;
303 452589f7 2021-03-16 op const char *prfx;
305 46f6e974 2021-05-17 op vl = buffer->current_line;
306 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
307 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
309 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
311 72b18268 2021-06-19 op buffer->curs_x += x_offset;
313 452589f7 2021-03-16 op if (vl != NULL) {
314 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
315 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
320 870210fb 2021-03-26 op global_key_unbound(void)
322 870210fb 2021-03-26 op message("%s is undefined", keybuf);
326 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
328 3148eeac 2021-03-13 op struct hist *hist;
330 22268e11 2021-03-11 op if (ministate.history == NULL)
333 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
336 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
338 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
339 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
341 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
342 22268e11 2021-03-11 op ministate.history->len++;
346 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
347 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
348 22268e11 2021-03-11 op * navigation" thing.
351 22268e11 2021-03-11 op minibuffer_taint_hist(void)
353 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
356 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
357 22268e11 2021-03-11 op ministate.hist_cur = NULL;
361 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
363 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
366 22268e11 2021-03-11 op minibuffer_taint_hist();
368 17e5106b 2021-03-21 op if (thiskey.cp == 0)
371 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
372 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
373 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
376 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
377 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
378 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
382 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
384 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
385 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
386 5cd2ebb1 2021-03-11 op global_key_unbound();
390 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
394 b360ebb3 2021-03-10 op eecmd_select(void)
396 870210fb 2021-03-26 op struct cmds *cmd;
398 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
399 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
400 2fe98292 2021-03-26 op exit_minibuffer();
401 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
402 46f6e974 2021-05-17 op cmd->fn(current_buffer());
407 2fe98292 2021-03-26 op message("No match");
411 5cd2ebb1 2021-03-11 op ir_self_insert(void)
413 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
417 5cd2ebb1 2021-03-11 op ir_select(void)
419 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
420 31f1a758 2021-04-22 op struct phos_uri uri;
421 5cd2ebb1 2021-03-11 op struct tab *tab;
423 5cd2ebb1 2021-03-11 op tab = current_tab();
425 5cd2ebb1 2021-03-11 op exit_minibuffer();
426 22268e11 2021-03-11 op minibuffer_hist_save_entry();
428 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
429 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
430 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
431 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
432 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
436 5cd2ebb1 2021-03-11 op lu_self_insert(void)
438 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
439 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
440 5cd2ebb1 2021-03-11 op global_key_unbound();
444 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
448 5cd2ebb1 2021-03-11 op lu_select(void)
450 5cd2ebb1 2021-03-11 op exit_minibuffer();
451 22268e11 2021-03-11 op minibuffer_hist_save_entry();
452 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
456 740f578b 2021-03-15 op bp_select(void)
458 740f578b 2021-03-15 op exit_minibuffer();
459 740f578b 2021-03-15 op if (*ministate.buf != '\0')
460 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
462 740f578b 2021-03-15 op message("Abort.");
466 5d1bac73 2021-03-25 op yornp_self_insert(void)
468 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
469 5d1bac73 2021-03-25 op message("Please answer y or n");
473 5d1bac73 2021-03-25 op exit_minibuffer();
474 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
478 5d1bac73 2021-03-25 op yornp_abort(void)
480 5d1bac73 2021-03-25 op exit_minibuffer();
481 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
485 de2a69bb 2021-05-17 op read_self_insert(void)
487 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
488 de2a69bb 2021-05-17 op global_key_unbound();
492 de2a69bb 2021-05-17 op minibuffer_self_insert();
496 de2a69bb 2021-05-17 op read_abort(void)
498 de2a69bb 2021-05-17 op exit_minibuffer();
499 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
503 de2a69bb 2021-05-17 op read_select(void)
505 de2a69bb 2021-05-17 op exit_minibuffer();
506 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
507 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
510 9a25f829 2021-03-14 op static struct vline *
511 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
513 9a25f829 2021-03-14 op struct vline *vl;
517 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
523 48e9d457 2021-03-06 op /* unreachable */
528 5e11c00c 2021-03-02 op current_tab(void)
530 5e11c00c 2021-03-02 op struct tab *t;
532 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
533 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
537 5e11c00c 2021-03-02 op /* unreachable */
541 2b2d2872 2021-06-20 op struct buffer *
542 46f6e974 2021-05-17 op current_buffer(void)
544 2ba66cea 2021-03-22 op if (in_minibuffer)
545 46f6e974 2021-05-17 op return &ministate.buffer;
546 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
550 8947c1f2 2021-03-21 op readkey(void)
552 8947c1f2 2021-03-21 op uint32_t state = 0;
554 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
557 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
558 8947c1f2 2021-03-21 op if (thiskey.meta) {
559 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
560 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
561 c314a314 2021-03-11 op thiskey.meta = 0;
562 9ca15951 2021-03-09 op thiskey.key = 27;
566 8947c1f2 2021-03-21 op thiskey.cp = 0;
567 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
569 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
571 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
572 8947c1f2 2021-03-21 op message("Error decoding user input");
582 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
584 8947c1f2 2021-03-21 op struct keymap *k;
585 8947c1f2 2021-03-21 op const char *keyname;
586 8947c1f2 2021-03-21 op char tmp[5] = {0};
588 8947c1f2 2021-03-21 op if (!readkey())
591 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
592 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
593 7c7d7bb7 2021-03-10 op if (thiskey.meta)
594 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
595 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
596 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
597 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
599 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
600 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
602 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
603 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
607 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
608 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
609 9ca15951 2021-03-09 op k->key == thiskey.key) {
610 f832146f 2021-03-09 op if (k->fn == NULL)
611 f832146f 2021-03-09 op current_map = &k->map;
613 9ca15951 2021-03-09 op current_map = base_map;
614 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
615 46f6e974 2021-05-17 op k->fn(current_buffer());
621 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
622 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
624 7c7d7bb7 2021-03-10 op global_key_unbound();
626 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
627 9ca15951 2021-03-09 op current_map = base_map;
630 bddc7bbd 2021-04-01 op if (side_window)
631 bddc7bbd 2021-04-01 op recompute_help();
633 179f0f58 2021-03-11 op redraw_tab(current_tab());
637 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
639 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
640 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
642 9cb0f9ce 2021-03-10 op redraw_minibuffer();
643 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
644 9cb0f9ce 2021-03-10 op wrefresh(body);
645 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
647 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
648 9cb0f9ce 2021-03-10 op wrefresh(body);
653 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
655 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
656 831deb20 2021-05-12 op event_del(&resizeev);
658 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
659 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
663 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
665 1d08c280 2021-03-06 op struct tab *tab;
671 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
673 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
674 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
676 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
677 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
679 48e9d457 2021-03-06 op body_lines = LINES-3;
680 bd9637e9 2021-03-06 op body_cols = COLS;
682 bddc7bbd 2021-04-01 op if (side_window) {
683 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
684 bddc7bbd 2021-04-01 op help_lines = LINES-3;
685 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
686 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
688 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
690 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
691 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
693 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
695 72b18268 2021-06-19 op update_x_offset();
696 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
698 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
700 1d08c280 2021-03-06 op tab = current_tab();
702 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
703 1d08c280 2021-03-06 op redraw_tab(tab);
707 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
709 452589f7 2021-03-16 op struct line *l;
710 452589f7 2021-03-16 op const struct line *orig;
711 d511dc85 2021-03-16 op struct vline *vl;
712 72b18268 2021-06-19 op int pre_width;
713 452589f7 2021-03-16 op const char *prfx;
715 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
717 46f6e974 2021-05-17 op : buffer->current_line->parent;
718 46f6e974 2021-05-17 op buffer->current_line = NULL;
720 a00b4c97 2021-06-20 op buffer->force_redraw = 1;
721 46f6e974 2021-05-17 op buffer->curs_y = 0;
722 46f6e974 2021-05-17 op buffer->line_off = 0;
724 46f6e974 2021-05-17 op empty_vlist(buffer);
726 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
727 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
728 5e11c00c 2021-03-02 op switch (l->type) {
729 5e11c00c 2021-03-02 op case LINE_TEXT:
730 5e11c00c 2021-03-02 op case LINE_LINK:
731 5e11c00c 2021-03-02 op case LINE_TITLE_1:
732 5e11c00c 2021-03-02 op case LINE_TITLE_2:
733 5e11c00c 2021-03-02 op case LINE_TITLE_3:
734 5e11c00c 2021-03-02 op case LINE_ITEM:
735 5e11c00c 2021-03-02 op case LINE_QUOTE:
736 5e11c00c 2021-03-02 op case LINE_PRE_START:
737 5e11c00c 2021-03-02 op case LINE_PRE_END:
738 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
740 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
741 72b18268 2021-06-19 op if (olivetti_mode)
742 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
744 72b18268 2021-06-19 op pre_width = width;
745 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
749 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
750 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
751 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
754 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
755 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
757 46f6e974 2021-05-17 op buffer->current_line = vl;
758 46f6e974 2021-05-17 op buffer->line_off--;
763 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
764 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
770 f3bcf8f2 2021-06-21 op print_vline(int off, int width, WINDOW *window, struct vline *vl)
772 9a25f829 2021-03-14 op const char *text = vl->line;
773 768db5bb 2021-03-12 op const char *prfx;
774 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
775 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
776 d89b86d6 2021-06-21 op int trail_face = line_faces[vl->parent->type].trail_prop;
777 f3bcf8f2 2021-06-21 op int i, left, x, y;
779 d89b86d6 2021-06-21 op /* unused, set by getyx */
782 9a25f829 2021-03-14 op if (!vl->flags)
783 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
785 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
787 bd9637e9 2021-03-06 op if (text == NULL)
790 ab47e7d4 2021-06-24 op wattr_on(window, body_face.left, NULL);
791 f3bcf8f2 2021-06-21 op for (i = 0; i < off; i++)
792 f3bcf8f2 2021-06-21 op waddch(window, ' ');
793 ab47e7d4 2021-06-24 op wattr_off(window, body_face.left, NULL);
795 ab47e7d4 2021-06-24 op wattr_on(window, prefix_face, NULL);
796 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
797 ab47e7d4 2021-06-24 op wattr_off(window, prefix_face, NULL);
799 ab47e7d4 2021-06-24 op wattr_on(window, text_face, NULL);
800 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
801 ab47e7d4 2021-06-24 op wattr_off(window, text_face, NULL);
803 f3bcf8f2 2021-06-21 op getyx(window, y, x);
805 f3bcf8f2 2021-06-21 op left = width - x;
807 ab47e7d4 2021-06-24 op wattr_on(window, trail_face, NULL);
808 d89b86d6 2021-06-21 op for (i = 0; i < left - off - 1; ++i)
809 f3bcf8f2 2021-06-21 op waddch(window, ' ');
810 ab47e7d4 2021-06-24 op wattr_off(window, trail_face, NULL);
812 ab47e7d4 2021-06-24 op wattr_on(window, body_face.right, NULL);
813 160abe04 2021-06-21 op for (i = 0; i < off; i++)
814 160abe04 2021-06-21 op waddch(window, ' ');
815 ab47e7d4 2021-06-24 op wattr_off(window, body_face.right, NULL);
820 8af5e5ed 2021-03-08 op redraw_tabline(void)
822 7c7d7bb7 2021-03-10 op struct tab *tab;
823 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
824 8f127baa 2021-04-22 op int current, y, truncated;
825 dc5df781 2021-03-13 op const char *title;
826 119f393c 2021-03-16 op char buf[25];
830 923f9ce5 2021-06-21 op /* unused, but setted by a getyx */
833 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
834 8f127baa 2021-04-22 op space = COLS-2;
837 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
839 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
843 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
846 8f127baa 2021-04-22 op ots = toskip;
848 8f127baa 2021-04-22 op while (toskip != 0 &&
849 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
853 a636f50e 2021-03-16 op werase(tabline);
854 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
855 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
856 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
858 a636f50e 2021-03-16 op truncated = 0;
859 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
860 a636f50e 2021-03-16 op if (truncated)
862 a636f50e 2021-03-16 op if (toskip != 0) {
867 a636f50e 2021-03-16 op getyx(tabline, y, x);
868 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
869 a636f50e 2021-03-16 op truncated = 1;
871 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
873 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
874 2051e653 2021-03-13 op title = tab->hist_cur->h;
876 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
877 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
879 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
881 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
882 119f393c 2021-03-16 op /* truncation happens */
883 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
885 119f393c 2021-03-16 op /* pad with spaces */
886 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
891 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.current, NULL);
893 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.tab, NULL);
895 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
896 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
897 119f393c 2021-03-16 op wprintw(tabline, " ");
900 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.current, NULL);
902 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.tab, NULL);
905 ab47e7d4 2021-06-24 op wattr_on(tabline, tab_face.background, NULL);
906 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
907 119f393c 2021-03-16 op waddch(tabline, ' ');
908 a636f50e 2021-03-16 op if (truncated)
909 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
910 ab47e7d4 2021-06-24 op wattr_off(tabline, tab_face.background, NULL);
914 f3bcf8f2 2021-06-21 op redraw_window(WINDOW *win, int height, int width, struct buffer *buffer)
916 bddc7bbd 2021-04-01 op struct vline *vl;
920 a00b4c97 2021-06-20 op * Don't bother redraw the body if nothing changed. Cursor
921 a00b4c97 2021-06-20 op * movements count as "nothing changed" if it hasn't produced
922 a00b4c97 2021-06-20 op * a scroll. Ensure that wmove is called though!
924 a00b4c97 2021-06-20 op if (!buffer->force_redraw &&
925 a00b4c97 2021-06-20 op buffer->last_line_off == buffer->line_off)
930 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
931 a00b4c97 2021-06-20 op buffer->force_redraw = 0;
932 a00b4c97 2021-06-20 op buffer->last_line_off = buffer->line_off;
934 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
938 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
939 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
940 f3bcf8f2 2021-06-21 op wmove(win, l, 0);
941 f3bcf8f2 2021-06-21 op print_vline(x_offset, width, win, vl);
943 bddc7bbd 2021-04-01 op if (l == height)
948 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
952 bddc7bbd 2021-04-01 op redraw_help(void)
954 f3bcf8f2 2021-06-21 op redraw_window(help, help_lines, help_cols, &helpwin);
958 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
960 3323faaf 2021-06-21 op static struct tab *last_tab;
962 3323faaf 2021-06-21 op if (last_tab != tab)
963 3323faaf 2021-06-21 op tab->buffer.force_redraw =1;
964 3323faaf 2021-06-21 op last_tab = tab;
966 f3bcf8f2 2021-06-21 op redraw_window(body, body_lines, body_cols, &tab->buffer);
969 10346511 2021-03-17 op static inline char
970 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
972 10346511 2021-03-17 op switch (ts) {
973 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
974 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
975 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
976 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
977 923f9ce5 2021-06-21 op default: return 'X';
982 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
985 48e9d457 2021-03-06 op int x, y, max_x, max_y;
986 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
987 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
989 156f1501 2021-03-13 op werase(modeline);
990 ab47e7d4 2021-06-24 op wattr_on(modeline, modeline_face.background, NULL);
991 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
993 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
994 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
995 10346511 2021-03-17 op trust_status_char(tab->trust),
996 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
998 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1000 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1001 481340cc 2021-03-11 op wprintw(modeline, "All ");
1002 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1003 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1004 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1005 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1007 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1009 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1010 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1011 46f6e974 2021-05-17 op tab->buffer.line_max,
1012 2051e653 2021-03-13 op tab->hist_cur->h);
1014 48e9d457 2021-03-06 op getyx(modeline, y, x);
1015 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1018 48e9d457 2021-03-06 op (void)max_y;
1020 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1021 48e9d457 2021-03-06 op waddstr(modeline, "-");
1023 ab47e7d4 2021-06-24 op wattr_off(modeline, modeline_face.background, NULL);
1027 9ca15951 2021-03-09 op redraw_minibuffer(void)
1029 8300dd3c 2021-03-18 op struct tab *tab;
1030 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1031 923f9ce5 2021-06-21 op char *start = NULL, *c = NULL;
1033 923f9ce5 2021-06-21 op /* unused, but set by getyx */
1034 923f9ce5 2021-06-21 op (void)off_y;
1036 ab47e7d4 2021-06-24 op wattr_on(minibuf, minibuffer_face.background, NULL);
1037 156f1501 2021-03-13 op werase(minibuf);
1039 22268e11 2021-03-11 op if (in_minibuffer) {
1040 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1041 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1042 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1043 22268e11 2021-03-11 op ministate.hist_off + 1,
1044 22268e11 2021-03-11 op ministate.history->len);
1046 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1048 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1049 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1050 17e5106b 2021-03-21 op : ministate.buf;
1051 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1052 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1053 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1054 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1057 17e5106b 2021-03-21 op waddstr(minibuf, start);
1060 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1061 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1062 17e5106b 2021-03-21 op ministate.curmesg);
1064 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1065 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1067 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1068 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1069 8300dd3c 2021-03-18 op tab = current_tab();
1070 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1071 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1072 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1075 2aaf475e 2021-03-13 op if (in_minibuffer)
1076 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1078 ab47e7d4 2021-06-24 op wattr_off(minibuf, minibuffer_face.background, NULL);
1082 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1084 bddc7bbd 2021-04-01 op if (side_window) {
1085 bddc7bbd 2021-04-01 op redraw_help();
1086 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1089 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1091 e19f9a04 2021-03-11 op redraw_tabline();
1092 e19f9a04 2021-03-11 op redraw_body(tab);
1093 e19f9a04 2021-03-11 op redraw_modeline(tab);
1094 e19f9a04 2021-03-11 op redraw_minibuffer();
1096 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1097 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1099 e19f9a04 2021-03-11 op if (in_minibuffer) {
1100 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1101 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1103 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1104 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1111 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1113 bddc7bbd 2021-04-01 op struct line *l;
1114 bddc7bbd 2021-04-01 op struct cmds *cmd;
1116 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1117 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1120 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1122 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1125 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1126 bddc7bbd 2021-04-01 op l->alt = NULL;
1128 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1130 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1131 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1133 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1137 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1139 bddc7bbd 2021-04-01 op struct keymap *k;
1141 bddc7bbd 2021-04-01 op const char *kn;
1143 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1144 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1145 bddc7bbd 2021-04-01 op if (*p != '\0')
1146 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1147 bddc7bbd 2021-04-01 op if (k->meta)
1148 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1149 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1150 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1152 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1154 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1155 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1157 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1162 bddc7bbd 2021-04-01 op recompute_help(void)
1164 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1166 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1167 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1168 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1169 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1173 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1175 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1176 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1178 bf992581 2021-03-12 op free(ministate.curmesg);
1179 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1181 2b2d2872 2021-06-20 op if (fmt != NULL) {
1182 2b2d2872 2021-06-20 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1183 2b2d2872 2021-06-20 op evtimer_add(&clminibufev, &clminibufev_timer);
1185 2b2d2872 2021-06-20 op /* TODO: what to do if the allocation fails here? */
1186 2b2d2872 2021-06-20 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1187 2b2d2872 2021-06-20 op ministate.curmesg = NULL;
1190 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1191 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1192 9cb0f9ce 2021-03-10 op wrefresh(body);
1193 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1195 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1196 9cb0f9ce 2021-03-10 op wrefresh(body);
1201 740f578b 2021-03-15 op message(const char *fmt, ...)
1205 740f578b 2021-03-15 op va_start(ap, fmt);
1206 740f578b 2021-03-15 op vmessage(fmt, ap);
1211 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1213 2ba66cea 2021-03-22 op if (tab->loading_anim)
1215 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1216 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1217 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1221 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1223 8af5e5ed 2021-03-08 op struct tab *tab = d;
1225 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1227 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1228 6347dcd0 2021-03-16 op redraw_modeline(tab);
1229 6347dcd0 2021-03-16 op wrefresh(modeline);
1230 6347dcd0 2021-03-16 op wrefresh(body);
1231 6347dcd0 2021-03-16 op if (in_minibuffer)
1232 6347dcd0 2021-03-16 op wrefresh(minibuf);
1235 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1239 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1241 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1243 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1244 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1245 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1247 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1250 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1252 43a1b8d0 2021-03-09 op wrefresh(modeline);
1253 43a1b8d0 2021-03-09 op wrefresh(body);
1254 9ca15951 2021-03-09 op if (in_minibuffer)
1255 9ca15951 2021-03-09 op wrefresh(minibuf);
1259 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1261 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1262 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1263 8af5e5ed 2021-03-08 op load_url(tab, url);
1265 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1266 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1267 43a1b8d0 2021-03-09 op redraw_tab(tab);
1271 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1272 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1274 9ca15951 2021-03-09 op in_minibuffer = 1;
1275 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1276 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1278 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1280 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1281 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1282 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1283 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1284 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1285 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1286 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1288 22268e11 2021-03-11 op ministate.history = hist;
1289 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1290 22268e11 2021-03-11 op ministate.hist_off = 0;
1294 b360ebb3 2021-03-10 op exit_minibuffer(void)
1296 156f1501 2021-03-13 op werase(minibuf);
1298 9ca15951 2021-03-09 op in_minibuffer = 0;
1299 9ca15951 2021-03-09 op base_map = &global_map;
1300 9ca15951 2021-03-09 op current_map = &global_map;
1304 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1306 5cd2ebb1 2021-03-11 op struct tab *t;
1308 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1309 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1312 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1313 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1316 2eef3403 2021-04-22 op unsigned int
1317 2eef3403 2021-04-22 op tab_new_id(void)
1319 2eef3403 2021-04-22 op return tab_counter++;
1322 2b2d2872 2021-06-20 op struct tab *
1323 1b81bc33 2021-03-15 op new_tab(const char *url)
1325 754622a2 2021-03-15 op struct tab *tab;
1327 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1328 71105afa 2021-03-16 op event_loopbreak();
1329 71105afa 2021-03-16 op return NULL;
1331 de2a69bb 2021-05-17 op tab->fd = -1;
1333 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1335 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1337 2eef3403 2021-04-22 op tab->id = tab_new_id();
1338 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1340 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1341 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1343 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1345 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1350 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1352 c7107cec 2021-04-01 op new_tab(url);
1358 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1359 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1363 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1365 c92e529c 2021-06-15 op char path[PATH_MAX];
1366 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1367 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1369 74a2587f 2021-06-21 op if (getenv("NO_COLOR") != NULL)
1370 74a2587f 2021-06-21 op enable_colors = 0;
1372 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1373 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1375 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1376 941b3761 2021-03-18 op switch (ch) {
1379 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1382 c92e529c 2021-06-15 op configtest = 1;
1392 941b3761 2021-03-18 op argc -= optind;
1393 941b3761 2021-03-18 op argv += optind;
1395 33fc3a8f 2021-06-21 op config_init();
1396 c92e529c 2021-06-15 op parseconfig(path, fonf);
1397 c92e529c 2021-06-15 op if (configtest){
1398 c92e529c 2021-06-15 op puts("config OK");
1402 941b3761 2021-03-18 op if (argc != 0)
1403 941b3761 2021-03-18 op url = argv[0];
1405 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1407 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1408 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1410 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1412 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1413 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1414 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1416 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1417 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1418 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1420 bddc7bbd 2021-04-01 op /* initialize help window */
1421 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1423 9ca15951 2021-03-09 op base_map = &global_map;
1424 f832146f 2021-03-09 op current_map = &global_map;
1425 f832146f 2021-03-09 op load_default_keys();
1429 74a2587f 2021-06-21 op if (enable_colors) {
1430 74a2587f 2021-06-21 op if (has_colors()) {
1431 74a2587f 2021-06-21 op start_color();
1432 160abe04 2021-06-21 op use_default_colors();
1434 74a2587f 2021-06-21 op enable_colors = 0;
1437 42d61f50 2021-06-24 op config_apply_style();
1442 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1444 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1446 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1448 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1450 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1452 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1455 48e9d457 2021-03-06 op body_lines = LINES-3;
1456 48e9d457 2021-03-06 op body_cols = COLS;
1458 b598590d 2021-06-21 op wbkgd(body, body_face.body);
1460 72b18268 2021-06-19 op update_x_offset();
1462 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1463 f3bcf8f2 2021-06-21 op scrollok(body, FALSE);
1465 5e11c00c 2021-03-02 op /* non-blocking input */
1466 48e9d457 2021-03-06 op wtimeout(body, 0);
1468 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1470 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1471 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1473 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1474 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1476 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
1477 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
1478 c7107cec 2021-04-01 op new_tab(url);
1484 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
1486 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
1487 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
1489 3d8c2326 2021-03-18 op redraw_tabline();
1490 3d8c2326 2021-03-18 op wrefresh(tabline);
1491 3d8c2326 2021-03-18 op if (in_minibuffer)
1492 3d8c2326 2021-03-18 op wrefresh(minibuf);
1494 3d8c2326 2021-03-18 op wrefresh(body);
1498 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
1500 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1501 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
1502 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
1503 3d8c2326 2021-03-18 op redraw_tab(tab);
1505 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
1508 2b2d2872 2021-06-20 op const char *
1509 2b2d2872 2021-06-20 op ui_keyname(int k)
1511 2b2d2872 2021-06-20 op return keyname(k);
1515 2b2d2872 2021-06-20 op ui_toggle_side_window(void)
1517 2b2d2872 2021-06-20 op side_window = !side_window;
1518 2b2d2872 2021-06-20 op if (side_window)
1519 2b2d2872 2021-06-20 op recompute_help();
1522 960b01da 2021-06-20 op * ugly hack, but otherwise the window doesn't get updated
1523 960b01da 2021-06-20 op * until I call handle_resize a second time (i.e. C-l). I
1524 960b01da 2021-06-20 op * will be happy to know why something like this is needed.
1526 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1527 960b01da 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1531 2b2d2872 2021-06-20 op ui_schedule_redraw(void)
1533 2b2d2872 2021-06-20 op handle_resize_nodelay(0, 0, NULL);
1537 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
1539 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
1540 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1542 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
1543 22268e11 2021-03-11 op &ir_history);
1544 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
1545 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
1546 5cd2ebb1 2021-03-11 op redraw_tab(tab);
1550 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
1551 b3575139 2021-04-01 op unsigned int data)
1555 6f66d9bf 2021-04-24 op if (in_minibuffer) {
1556 6f66d9bf 2021-04-24 op fn(0, data);
1560 5d1bac73 2021-03-25 op yornp_cb = fn;
1561 e49ce157 2021-04-01 op yornp_data = data;
1562 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
1563 5d1bac73 2021-03-25 op yornp_abort, NULL);
1565 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
1566 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
1567 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
1568 5d1bac73 2021-03-25 op redraw_tab(current_tab());
1572 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
1573 de2a69bb 2021-05-17 op unsigned int data)
1577 de2a69bb 2021-05-17 op if (in_minibuffer)
1580 de2a69bb 2021-05-17 op read_cb = fn;
1581 de2a69bb 2021-05-17 op read_data = data;
1582 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
1583 de2a69bb 2021-05-17 op &read_history);
1585 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
1586 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
1587 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
1588 de2a69bb 2021-05-17 op redraw_tab(current_tab());
1592 5e11c00c 2021-03-02 op ui_end(void)