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 #define TAB_CURRENT 0x1
50 e8a76665 2021-05-12 op #define TAB_URGENT 0x2
52 1b81bc33 2021-03-15 op #define NEW_TAB_URL "about:new"
54 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
56 f832146f 2021-03-09 op static void load_default_keys(void);
57 46f6e974 2021-05-17 op static void restore_cursor(struct buffer*);
59 870210fb 2021-03-26 op static void global_key_unbound(void);
60 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
61 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
62 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
63 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
64 b360ebb3 2021-03-10 op static void eecmd_select(void);
65 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
66 5cd2ebb1 2021-03-11 op static void ir_select(void);
67 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
68 5cd2ebb1 2021-03-11 op static void lu_select(void);
69 740f578b 2021-03-15 op static void bp_select(void);
70 5d1bac73 2021-03-25 op static void yornp_self_insert(void);
71 5d1bac73 2021-03-25 op static void yornp_abort(void);
72 de2a69bb 2021-05-17 op static void read_self_insert(void);
73 de2a69bb 2021-05-17 op static void read_abort(void);
74 de2a69bb 2021-05-17 op static void read_select(void);
76 46f6e974 2021-05-17 op static struct vline *nth_line(struct buffer*, size_t);
77 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
78 46f6e974 2021-05-17 op static struct buffer *current_buffer(void);
79 8947c1f2 2021-03-21 op static int readkey(void);
80 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
81 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
82 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
83 a8a482c8 2021-05-17 op static void handle_resize_nodelay(int, short, void*);
84 46f6e974 2021-05-17 op static int wrap_page(struct buffer*, int);
85 bddc7bbd 2021-04-01 op static void print_vline(WINDOW*, struct vline*);
86 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
87 46f6e974 2021-05-17 op static void redraw_window(WINDOW*, int, struct buffer*);
88 bddc7bbd 2021-04-01 op static void redraw_help(void);
89 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
90 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
91 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
92 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
93 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
94 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
95 bddc7bbd 2021-04-01 op static void recompute_help(void);
96 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
97 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
98 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
99 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
100 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
101 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
102 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
103 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
104 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
105 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
106 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
107 941b3761 2021-03-18 op static void usage(void);
109 72b18268 2021-06-19 op static int x_offset;
111 8947c1f2 2021-03-21 op static struct { short meta; int key; uint32_t cp; } thiskey;
113 831deb20 2021-05-12 op static struct event resizeev;
114 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
116 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
117 48e9d457 2021-03-06 op static int body_lines, body_cols;
119 bddc7bbd 2021-04-01 op static WINDOW *help;
120 46f6e974 2021-05-17 op static struct buffer helpwin;
121 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
123 bddc7bbd 2021-04-01 op static int side_window;
125 48e9d457 2021-03-06 op static struct event clminibufev;
126 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
127 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
129 bcb0b073 2021-03-07 op static uint32_t tab_counter;
131 7c7d7bb7 2021-03-10 op static char keybuf[64];
133 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
134 b3575139 2021-04-01 op static unsigned int yornp_data;
136 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
137 de2a69bb 2021-05-17 op static unsigned int read_data;
139 9ca15951 2021-03-09 op struct kmap global_map,
140 fa3fd864 2021-03-10 op minibuffer_map,
141 9ca15951 2021-03-09 op *current_map,
144 3148eeac 2021-03-13 op static struct histhead eecmd_history,
147 de2a69bb 2021-05-17 op read_history;
149 9ca15951 2021-03-09 op static int in_minibuffer;
151 9ca15951 2021-03-09 op static struct {
152 2ba66cea 2021-03-22 op char *curmesg;
154 5d1bac73 2021-03-25 op char prompt[64];
155 2ba66cea 2021-03-22 op void (*donefn)(void);
156 2ba66cea 2021-03-22 op void (*abortfn)(void);
158 2ba66cea 2021-03-22 op char buf[1025];
159 2ba66cea 2021-03-22 op struct line line;
160 2ba66cea 2021-03-22 op struct vline vline;
161 46f6e974 2021-05-17 op struct buffer buffer;
163 3148eeac 2021-03-13 op struct histhead *history;
164 3148eeac 2021-03-13 op struct hist *hist_cur;
165 17e5106b 2021-03-21 op size_t hist_off;
168 72b18268 2021-06-19 op static inline void
169 72b18268 2021-06-19 op update_x_offset()
171 72b18268 2021-06-19 op if (olivetti_mode && fill_column < body_cols)
172 72b18268 2021-06-19 op x_offset = (body_cols - fill_column)/2;
174 72b18268 2021-06-19 op x_offset = 0;
177 f832146f 2021-03-09 op static inline void
178 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
180 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
184 9ca15951 2021-03-09 op static inline void
185 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
187 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
192 f832146f 2021-03-09 op load_default_keys(void)
194 9ca15951 2021-03-09 op /* === global map === */
197 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
198 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
199 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
200 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
201 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
202 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
203 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
204 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
206 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
207 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
208 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
209 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
211 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
212 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
214 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
216 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
218 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
219 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
220 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
222 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
223 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
224 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
225 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
226 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
227 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
228 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
230 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
231 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
233 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
234 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
236 f832146f 2021-03-09 op /* vi/vi-like */
237 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
238 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
239 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
240 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
241 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
242 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
243 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
244 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
246 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
247 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
249 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
250 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
252 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
253 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
254 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
255 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
256 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
257 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
259 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
260 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
263 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
265 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
267 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
270 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
271 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
272 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
273 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
274 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
275 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
277 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
278 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
280 f832146f 2021-03-09 op /* "ncurses standard" */
281 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
284 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
285 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
286 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
287 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
288 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
289 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
291 fa3fd864 2021-03-10 op /* === minibuffer map === */
292 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
293 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
294 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
295 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
296 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
297 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
298 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
300 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
301 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
302 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
303 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
304 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
305 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
306 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
307 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
308 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
310 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
311 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
312 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
313 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
317 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
319 452589f7 2021-03-16 op struct vline *vl;
320 452589f7 2021-03-16 op const char *prfx;
322 46f6e974 2021-05-17 op vl = buffer->current_line;
323 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
324 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
326 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
328 72b18268 2021-06-19 op buffer->curs_x += x_offset;
330 452589f7 2021-03-16 op if (vl != NULL) {
331 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
332 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
337 46f6e974 2021-05-17 op cmd_previous_line(struct buffer *buffer)
339 452589f7 2021-03-16 op struct vline *vl;
341 46f6e974 2021-05-17 op if (buffer->current_line == NULL
342 46f6e974 2021-05-17 op || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
345 46f6e974 2021-05-17 op if (--buffer->curs_y < 0) {
346 46f6e974 2021-05-17 op buffer->curs_y = 0;
347 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
351 46f6e974 2021-05-17 op buffer->current_line = vl;
352 46f6e974 2021-05-17 op restore_cursor(buffer);
356 46f6e974 2021-05-17 op cmd_next_line(struct buffer *buffer)
358 452589f7 2021-03-16 op struct vline *vl;
360 46f6e974 2021-05-17 op if (buffer->current_line == NULL
361 46f6e974 2021-05-17 op || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
364 46f6e974 2021-05-17 op if (++buffer->curs_y > body_lines-1) {
365 46f6e974 2021-05-17 op buffer->curs_y = body_lines-1;
366 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
370 46f6e974 2021-05-17 op buffer->current_line = vl;
371 46f6e974 2021-05-17 op restore_cursor(buffer);
375 46f6e974 2021-05-17 op cmd_backward_char(struct buffer *buffer)
377 46f6e974 2021-05-17 op if (buffer->cpoff != 0)
378 46f6e974 2021-05-17 op buffer->cpoff--;
379 46f6e974 2021-05-17 op restore_cursor(buffer);
383 46f6e974 2021-05-17 op cmd_forward_char(struct buffer *buffer)
385 3a20fb6c 2021-05-12 op size_t len = 0;
387 46f6e974 2021-05-17 op if (buffer->current_line->line != NULL)
388 46f6e974 2021-05-17 op len = utf8_cplen(buffer->current_line->line);
389 46f6e974 2021-05-17 op if (++buffer->cpoff > len)
390 46f6e974 2021-05-17 op buffer->cpoff = len;
391 46f6e974 2021-05-17 op restore_cursor(buffer);
395 46f6e974 2021-05-17 op cmd_backward_paragraph(struct buffer *buffer)
398 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
399 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
400 852d03e8 2021-03-17 op message("No previous paragraph");
403 46f6e974 2021-05-17 op cmd_previous_line(buffer);
404 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
405 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
409 46f6e974 2021-05-17 op cmd_forward_paragraph(struct buffer *buffer)
412 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
413 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
414 852d03e8 2021-03-17 op message("No next paragraph");
417 46f6e974 2021-05-17 op cmd_next_line(buffer);
418 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
419 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
423 46f6e974 2021-05-17 op cmd_move_beginning_of_line(struct buffer *buffer)
425 46f6e974 2021-05-17 op buffer->cpoff = 0;
426 46f6e974 2021-05-17 op restore_cursor(buffer);
430 46f6e974 2021-05-17 op cmd_move_end_of_line(struct buffer *buffer)
432 9a25f829 2021-03-14 op struct vline *vl;
434 46f6e974 2021-05-17 op vl = buffer->current_line;
435 452589f7 2021-03-16 op if (vl->line == NULL)
437 46f6e974 2021-05-17 op buffer->cpoff = utf8_cplen(vl->line);
438 46f6e974 2021-05-17 op restore_cursor(buffer);
442 46f6e974 2021-05-17 op cmd_redraw(struct buffer *buffer)
444 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
448 46f6e974 2021-05-17 op cmd_scroll_line_up(struct buffer *buffer)
450 9a25f829 2021-03-14 op struct vline *vl;
452 46f6e974 2021-05-17 op if (buffer->line_off == 0)
455 46f6e974 2021-05-17 op vl = nth_line(buffer, --buffer->line_off);
456 48e9d457 2021-03-06 op wscrl(body, -1);
457 48e9d457 2021-03-06 op wmove(body, 0, 0);
458 bddc7bbd 2021-04-01 op print_vline(body, vl);
460 46f6e974 2021-05-17 op buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
461 46f6e974 2021-05-17 op restore_cursor(buffer);
465 46f6e974 2021-05-17 op cmd_scroll_line_down(struct buffer *buffer)
467 9a25f829 2021-03-14 op struct vline *vl;
469 46f6e974 2021-05-17 op vl = buffer->current_line;
470 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
472 46f6e974 2021-05-17 op buffer->current_line = vl;
474 46f6e974 2021-05-17 op buffer->line_off++;
475 48e9d457 2021-03-06 op wscrl(body, 1);
477 46f6e974 2021-05-17 op if (buffer->line_max - buffer->line_off < (size_t)body_lines)
480 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off + body_lines-1);
481 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
482 bddc7bbd 2021-04-01 op print_vline(body, vl);
484 46f6e974 2021-05-17 op restore_cursor(buffer);
488 46f6e974 2021-05-17 op cmd_scroll_up(struct buffer *buffer)
492 cce62e74 2021-04-25 op off = body_lines-1;
494 ed44414d 2021-03-09 op for (; off > 0; --off)
495 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
499 46f6e974 2021-05-17 op cmd_scroll_down(struct buffer *buffer)
503 cce62e74 2021-04-25 op off = body_lines-1;
505 338eecdc 2021-03-12 op for (; off > 0; --off)
506 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
510 46f6e974 2021-05-17 op cmd_beginning_of_buffer(struct buffer *buffer)
512 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
513 46f6e974 2021-05-17 op buffer->line_off = 0;
514 46f6e974 2021-05-17 op buffer->curs_y = 0;
515 46f6e974 2021-05-17 op buffer->cpoff = 0;
516 46f6e974 2021-05-17 op restore_cursor(buffer);
520 46f6e974 2021-05-17 op cmd_end_of_buffer(struct buffer *buffer)
524 46f6e974 2021-05-17 op off = buffer->line_max - body_lines;
525 e19f9a04 2021-03-11 op off = MAX(0, off);
527 46f6e974 2021-05-17 op buffer->line_off = off;
528 46f6e974 2021-05-17 op buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
530 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
531 46f6e974 2021-05-17 op buffer->cpoff = body_cols;
532 46f6e974 2021-05-17 op restore_cursor(buffer);
536 46f6e974 2021-05-17 op cmd_kill_telescope(struct buffer *buffer)
538 c7107cec 2021-04-01 op save_session();
539 1d08c280 2021-03-06 op event_loopbreak();
543 46f6e974 2021-05-17 op cmd_push_button(struct buffer *buffer)
545 9a25f829 2021-03-14 op struct vline *vl;
548 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
549 46f6e974 2021-05-17 op if (nth >= buffer->line_max)
551 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
552 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
555 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), vl->parent->alt);
559 46f6e974 2021-05-17 op cmd_push_button_new_tab(struct buffer *buffer)
561 9a25f829 2021-03-14 op struct vline *vl;
564 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
565 46f6e974 2021-05-17 op if (nth > buffer->line_max)
567 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
568 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
571 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
575 46f6e974 2021-05-17 op cmd_previous_button(struct buffer *buffer)
578 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
579 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
580 a8c28919 2021-03-16 op message("No previous link");
583 46f6e974 2021-05-17 op cmd_previous_line(buffer);
584 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
588 46f6e974 2021-05-17 op cmd_next_button(struct buffer *buffer)
591 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
592 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
593 a8c28919 2021-03-16 op message("No next link");
596 46f6e974 2021-05-17 op cmd_next_line(buffer);
597 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
601 46f6e974 2021-05-17 op cmd_previous_page(struct buffer *buffer)
603 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
605 2051e653 2021-03-13 op if (!load_previous_page(tab))
606 2051e653 2021-03-13 op message("No previous page");
608 c40c2250 2021-03-14 op start_loading_anim(tab);
612 46f6e974 2021-05-17 op cmd_next_page(struct buffer *buffer)
614 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
616 2051e653 2021-03-13 op if (!load_next_page(tab))
617 2051e653 2021-03-13 op message("No next page");
619 c40c2250 2021-03-14 op start_loading_anim(tab);
623 46f6e974 2021-05-17 op cmd_clear_minibuf(struct buffer *buffer)
625 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
629 46f6e974 2021-05-17 op cmd_execute_extended_command(struct buffer *buffer)
633 2ba66cea 2021-03-22 op if (in_minibuffer) {
634 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
638 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
639 22268e11 2021-03-11 op &eecmd_history);
641 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
642 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
644 9ca15951 2021-03-09 op if (thiskey.meta)
645 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
647 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
649 c3fcefe6 2021-05-12 op if (thiskey.meta)
650 c3fcefe6 2021-05-12 op strlcat(ministate.prompt, " ", len);
654 46f6e974 2021-05-17 op cmd_tab_close(struct buffer *buffer)
656 2ba66cea 2021-03-22 op struct tab *tab, *t;
658 2ba66cea 2021-03-22 op tab = current_tab();
659 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
660 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
661 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
665 2ba66cea 2021-03-22 op if (evtimer_pending(&tab->loadingev, NULL))
666 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
668 7c7d7bb7 2021-03-10 op stop_tab(tab);
670 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
671 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
672 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
675 9d24c64f 2021-03-17 op switch_to_tab(t);
679 46f6e974 2021-05-17 op cmd_tab_close_other(struct buffer *buffer)
681 2ff75826 2021-03-26 op struct tab *t, *i;
683 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
684 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
688 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
694 46f6e974 2021-05-17 op cmd_tab_new(struct buffer *buffer)
696 db7cc27a 2021-06-19 op const char *url;
698 db7cc27a 2021-06-19 op if ((url = new_tab_url) == NULL)
699 db7cc27a 2021-06-19 op url = NEW_TAB_URL;
701 db7cc27a 2021-06-19 op new_tab(url);
705 46f6e974 2021-05-17 op cmd_tab_next(struct buffer *buffer)
707 2ba66cea 2021-03-22 op struct tab *tab, *t;
709 2ba66cea 2021-03-22 op tab = current_tab();
710 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
712 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
713 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
714 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
715 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
719 46f6e974 2021-05-17 op cmd_tab_previous(struct buffer *buffer)
721 2ba66cea 2021-03-22 op struct tab *tab, *t;
723 2ba66cea 2021-03-22 op tab = current_tab();
724 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
726 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
727 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
728 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
729 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
733 46f6e974 2021-05-17 op cmd_tab_move(struct buffer *buffer)
735 fad72201 2021-03-28 op struct tab *tab, *t;
737 fad72201 2021-03-28 op tab = current_tab();
738 fad72201 2021-03-28 op t = TAILQ_NEXT(tab, tabs);
739 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
741 fad72201 2021-03-28 op if (t == NULL)
742 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
744 fad72201 2021-03-28 op TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
748 46f6e974 2021-05-17 op cmd_tab_move_to(struct buffer *buffer)
750 fad72201 2021-03-28 op struct tab *tab, *t;
752 fad72201 2021-03-28 op tab = current_tab();
753 fad72201 2021-03-28 op t = TAILQ_PREV(tab, tabshead, tabs);
754 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
756 fad72201 2021-03-28 op if (t == NULL) {
757 fad72201 2021-03-28 op if (TAILQ_EMPTY(&tabshead))
758 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
760 fad72201 2021-03-28 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
762 fad72201 2021-03-28 op TAILQ_INSERT_BEFORE(t, tab, tabs);
766 46f6e974 2021-05-17 op cmd_load_url(struct buffer *buffer)
768 2ba66cea 2021-03-22 op if (in_minibuffer) {
769 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
773 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
774 22268e11 2021-03-11 op &lu_history);
775 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
776 2d13108f 2021-04-30 op strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
777 46f6e974 2021-05-17 op cmd_move_end_of_line(&ministate.buffer);
781 46f6e974 2021-05-17 op cmd_load_current_url(struct buffer *buffer)
783 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
785 2ba66cea 2021-03-22 op if (in_minibuffer) {
786 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
790 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
791 22268e11 2021-03-11 op &lu_history);
792 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
793 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
794 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
798 46f6e974 2021-05-17 op cmd_bookmark_page(struct buffer *buffer)
800 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
802 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
803 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
804 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
805 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
809 46f6e974 2021-05-17 op cmd_list_bookmarks(struct buffer *buffer)
811 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), "about:bookmarks");
815 46f6e974 2021-05-17 op cmd_toggle_help(struct buffer *buffer)
817 bddc7bbd 2021-04-01 op side_window = !side_window;
818 bddc7bbd 2021-04-01 op if (side_window)
819 bddc7bbd 2021-04-01 op recompute_help();
822 7cdf12cb 2021-05-12 op * ugly hack, but otherwise the window doesn't get updated
823 bddc7bbd 2021-04-01 op * until I call handle_resize a second time (i.e. C-l). I
824 7cdf12cb 2021-05-12 op * will be happy to know why something like this is needed.
826 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
827 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
831 46f6e974 2021-05-17 op cmd_mini_delete_char(struct buffer *buffer)
835 2ba66cea 2021-03-22 op if (!in_minibuffer) {
836 2ba66cea 2021-03-22 op message("text is read-only");
840 3eecee9e 2021-03-12 op minibuffer_taint_hist();
842 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
843 2ba66cea 2021-03-22 op if (*c == '\0')
845 2ba66cea 2021-03-22 op n = utf8_next_cp(c);
847 2ba66cea 2021-03-22 op memmove(c, n, strlen(n)+1);
851 46f6e974 2021-05-17 op cmd_mini_delete_backward_char(struct buffer *buffer)
853 2ba66cea 2021-03-22 op char *c, *p, *start;
855 2ba66cea 2021-03-22 op if (!in_minibuffer) {
856 2ba66cea 2021-03-22 op message("text is read-only");
860 3eecee9e 2021-03-12 op minibuffer_taint_hist();
862 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
863 46f6e974 2021-05-17 op start = buffer->current_line->line;
864 2ba66cea 2021-03-22 op if (c == start)
866 2ba66cea 2021-03-22 op p = utf8_prev_cp(c-1, start);
868 2ba66cea 2021-03-22 op memmove(p, c, strlen(c)+1);
869 46f6e974 2021-05-17 op buffer->cpoff--;
873 46f6e974 2021-05-17 op cmd_mini_kill_line(struct buffer *buffer)
877 2ba66cea 2021-03-22 op if (!in_minibuffer) {
878 2ba66cea 2021-03-22 op message("text is read-only");
882 3eecee9e 2021-03-12 op minibuffer_taint_hist();
883 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
888 46f6e974 2021-05-17 op cmd_mini_abort(struct buffer *buffer)
890 2ba66cea 2021-03-22 op if (!in_minibuffer)
893 17e5106b 2021-03-21 op ministate.abortfn();
897 46f6e974 2021-05-17 op cmd_mini_complete_and_exit(struct buffer *buffer)
899 2ba66cea 2021-03-22 op if (!in_minibuffer)
902 22268e11 2021-03-11 op minibuffer_taint_hist();
903 b360ebb3 2021-03-10 op ministate.donefn();
907 46f6e974 2021-05-17 op cmd_mini_previous_history_element(struct buffer *buffer)
909 22268e11 2021-03-11 op if (ministate.history == NULL) {
910 22268e11 2021-03-11 op message("No history");
914 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
915 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
916 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
917 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
918 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
919 22268e11 2021-03-11 op message("No prev item");
921 22268e11 2021-03-11 op ministate.hist_off--;
924 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
925 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
929 46f6e974 2021-05-17 op cmd_mini_next_history_element(struct buffer *buffer)
931 22268e11 2021-03-11 op if (ministate.history == NULL) {
932 22268e11 2021-03-11 op message("No history");
936 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
937 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
938 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
939 22268e11 2021-03-11 op ministate.hist_off = 0;
940 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
941 22268e11 2021-03-11 op message("No next item");
943 22268e11 2021-03-11 op ministate.hist_off++;
946 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
947 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
951 870210fb 2021-03-26 op global_key_unbound(void)
953 870210fb 2021-03-26 op message("%s is undefined", keybuf);
957 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
959 3148eeac 2021-03-13 op struct hist *hist;
961 22268e11 2021-03-11 op if (ministate.history == NULL)
964 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
967 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
969 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
970 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
972 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
973 22268e11 2021-03-11 op ministate.history->len++;
977 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
978 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
979 22268e11 2021-03-11 op * navigation" thing.
982 22268e11 2021-03-11 op minibuffer_taint_hist(void)
984 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
987 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
988 22268e11 2021-03-11 op ministate.hist_cur = NULL;
992 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
994 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
997 22268e11 2021-03-11 op minibuffer_taint_hist();
999 17e5106b 2021-03-21 op if (thiskey.cp == 0)
1002 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
1003 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1004 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1007 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
1008 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
1009 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
1013 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
1015 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1016 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
1017 5cd2ebb1 2021-03-11 op global_key_unbound();
1021 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1025 b360ebb3 2021-03-10 op eecmd_select(void)
1027 870210fb 2021-03-26 op struct cmds *cmd;
1029 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1030 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
1031 2fe98292 2021-03-26 op exit_minibuffer();
1032 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
1033 46f6e974 2021-05-17 op cmd->fn(current_buffer());
1038 2fe98292 2021-03-26 op message("No match");
1042 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1044 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1048 5cd2ebb1 2021-03-11 op ir_select(void)
1050 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1051 31f1a758 2021-04-22 op struct phos_uri uri;
1052 5cd2ebb1 2021-03-11 op struct tab *tab;
1054 5cd2ebb1 2021-03-11 op tab = current_tab();
1056 5cd2ebb1 2021-03-11 op exit_minibuffer();
1057 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1059 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1060 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
1061 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
1062 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
1063 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1067 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1069 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
1070 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
1071 5cd2ebb1 2021-03-11 op global_key_unbound();
1075 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1079 5cd2ebb1 2021-03-11 op lu_select(void)
1081 5cd2ebb1 2021-03-11 op exit_minibuffer();
1082 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1083 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1087 740f578b 2021-03-15 op bp_select(void)
1089 740f578b 2021-03-15 op exit_minibuffer();
1090 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1091 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1093 740f578b 2021-03-15 op message("Abort.");
1097 5d1bac73 2021-03-25 op yornp_self_insert(void)
1099 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
1100 5d1bac73 2021-03-25 op message("Please answer y or n");
1104 5d1bac73 2021-03-25 op exit_minibuffer();
1105 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
1109 5d1bac73 2021-03-25 op yornp_abort(void)
1111 5d1bac73 2021-03-25 op exit_minibuffer();
1112 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
1116 de2a69bb 2021-05-17 op read_self_insert(void)
1118 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1119 de2a69bb 2021-05-17 op global_key_unbound();
1123 de2a69bb 2021-05-17 op minibuffer_self_insert();
1127 de2a69bb 2021-05-17 op read_abort(void)
1129 de2a69bb 2021-05-17 op exit_minibuffer();
1130 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
1134 de2a69bb 2021-05-17 op read_select(void)
1136 de2a69bb 2021-05-17 op exit_minibuffer();
1137 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
1138 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
1141 9a25f829 2021-03-14 op static struct vline *
1142 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
1144 9a25f829 2021-03-14 op struct vline *vl;
1148 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
1154 48e9d457 2021-03-06 op /* unreachable */
1158 5e11c00c 2021-03-02 op static struct tab *
1159 5e11c00c 2021-03-02 op current_tab(void)
1161 5e11c00c 2021-03-02 op struct tab *t;
1163 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1164 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1168 5e11c00c 2021-03-02 op /* unreachable */
1172 46f6e974 2021-05-17 op static struct buffer *
1173 46f6e974 2021-05-17 op current_buffer(void)
1175 2ba66cea 2021-03-22 op if (in_minibuffer)
1176 46f6e974 2021-05-17 op return &ministate.buffer;
1177 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
1181 8947c1f2 2021-03-21 op readkey(void)
1183 8947c1f2 2021-03-21 op uint32_t state = 0;
1185 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
1188 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
1189 8947c1f2 2021-03-21 op if (thiskey.meta) {
1190 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1191 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1192 c314a314 2021-03-11 op thiskey.meta = 0;
1193 9ca15951 2021-03-09 op thiskey.key = 27;
1197 8947c1f2 2021-03-21 op thiskey.cp = 0;
1198 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
1200 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1202 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
1203 8947c1f2 2021-03-21 op message("Error decoding user input");
1213 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
1215 8947c1f2 2021-03-21 op struct keymap *k;
1216 8947c1f2 2021-03-21 op const char *keyname;
1217 8947c1f2 2021-03-21 op char tmp[5] = {0};
1219 8947c1f2 2021-03-21 op if (!readkey())
1222 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1223 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1224 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1225 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1226 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
1227 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
1228 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1230 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
1231 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
1233 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
1234 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
1238 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1239 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1240 9ca15951 2021-03-09 op k->key == thiskey.key) {
1241 f832146f 2021-03-09 op if (k->fn == NULL)
1242 f832146f 2021-03-09 op current_map = &k->map;
1244 9ca15951 2021-03-09 op current_map = base_map;
1245 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1246 46f6e974 2021-05-17 op k->fn(current_buffer());
1252 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1253 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1255 7c7d7bb7 2021-03-10 op global_key_unbound();
1258 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1259 9ca15951 2021-03-09 op current_map = base_map;
1262 bddc7bbd 2021-04-01 op if (side_window)
1263 bddc7bbd 2021-04-01 op recompute_help();
1265 179f0f58 2021-03-11 op redraw_tab(current_tab());
1269 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1271 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1272 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1274 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1275 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1276 9cb0f9ce 2021-03-10 op wrefresh(body);
1277 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1279 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1280 9cb0f9ce 2021-03-10 op wrefresh(body);
1285 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1287 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1288 831deb20 2021-05-12 op event_del(&resizeev);
1290 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1291 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
1295 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
1297 1d08c280 2021-03-06 op struct tab *tab;
1303 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1305 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1306 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1308 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1309 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1311 48e9d457 2021-03-06 op body_lines = LINES-3;
1312 bd9637e9 2021-03-06 op body_cols = COLS;
1314 bddc7bbd 2021-04-01 op if (side_window) {
1315 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
1316 bddc7bbd 2021-04-01 op help_lines = LINES-3;
1317 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
1318 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
1320 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1322 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
1323 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
1325 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
1327 72b18268 2021-06-19 op update_x_offset();
1328 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
1330 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1332 1d08c280 2021-03-06 op tab = current_tab();
1334 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1335 1d08c280 2021-03-06 op redraw_tab(tab);
1339 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
1341 452589f7 2021-03-16 op struct line *l;
1342 452589f7 2021-03-16 op const struct line *orig;
1343 d511dc85 2021-03-16 op struct vline *vl;
1344 72b18268 2021-06-19 op int pre_width;
1345 452589f7 2021-03-16 op const char *prfx;
1347 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
1349 46f6e974 2021-05-17 op : buffer->current_line->parent;
1350 46f6e974 2021-05-17 op buffer->current_line = NULL;
1352 46f6e974 2021-05-17 op buffer->curs_y = 0;
1353 46f6e974 2021-05-17 op buffer->line_off = 0;
1355 46f6e974 2021-05-17 op empty_vlist(buffer);
1357 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
1358 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1359 5e11c00c 2021-03-02 op switch (l->type) {
1360 5e11c00c 2021-03-02 op case LINE_TEXT:
1361 5e11c00c 2021-03-02 op case LINE_LINK:
1362 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1363 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1364 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1365 5e11c00c 2021-03-02 op case LINE_ITEM:
1366 5e11c00c 2021-03-02 op case LINE_QUOTE:
1367 5e11c00c 2021-03-02 op case LINE_PRE_START:
1368 5e11c00c 2021-03-02 op case LINE_PRE_END:
1369 1a99965e 2021-06-19 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
1371 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1372 72b18268 2021-06-19 op if (olivetti_mode)
1373 72b18268 2021-06-19 op pre_width = MIN(fill_column, width);
1375 72b18268 2021-06-19 op pre_width = width;
1376 72b18268 2021-06-19 op hardwrap_text(buffer, l, pre_width);
1380 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
1381 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
1382 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1385 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1386 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1388 46f6e974 2021-05-17 op buffer->current_line = vl;
1389 46f6e974 2021-05-17 op buffer->line_off--;
1394 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
1395 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
1401 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
1403 9a25f829 2021-03-14 op const char *text = vl->line;
1404 768db5bb 2021-03-12 op const char *prfx;
1405 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1406 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1408 9a25f829 2021-03-14 op if (!vl->flags)
1409 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1411 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1413 bd9637e9 2021-03-06 op if (text == NULL)
1416 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
1417 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
1418 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
1420 bddc7bbd 2021-04-01 op wattron(window, text_face);
1421 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
1422 bddc7bbd 2021-04-01 op wattroff(window, text_face);
1426 8af5e5ed 2021-03-08 op redraw_tabline(void)
1428 7c7d7bb7 2021-03-10 op struct tab *tab;
1429 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
1430 8f127baa 2021-04-22 op int current, y, truncated;
1431 dc5df781 2021-03-13 op const char *title;
1432 119f393c 2021-03-16 op char buf[25];
1434 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
1435 8f127baa 2021-04-22 op space = COLS-2;
1438 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1440 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1444 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
1447 8f127baa 2021-04-22 op ots = toskip;
1449 8f127baa 2021-04-22 op while (toskip != 0 &&
1450 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
1454 a636f50e 2021-03-16 op werase(tabline);
1455 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1456 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1457 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1459 a636f50e 2021-03-16 op truncated = 0;
1460 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1461 a636f50e 2021-03-16 op if (truncated)
1463 a636f50e 2021-03-16 op if (toskip != 0) {
1468 a636f50e 2021-03-16 op getyx(tabline, y, x);
1469 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1470 a636f50e 2021-03-16 op truncated = 1;
1472 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1474 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
1475 2051e653 2021-03-13 op title = tab->hist_cur->h;
1477 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
1478 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
1480 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
1482 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1483 119f393c 2021-03-16 op /* truncation happens */
1484 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1486 119f393c 2021-03-16 op /* pad with spaces */
1487 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1491 a329982b 2021-03-11 op if (current)
1492 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1494 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1496 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1497 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1498 119f393c 2021-03-16 op wprintw(tabline, " ");
1500 cb6c7aa0 2021-03-16 op if (current)
1501 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1503 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1506 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1507 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
1508 119f393c 2021-03-16 op waddch(tabline, ' ');
1509 a636f50e 2021-03-16 op if (truncated)
1510 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1514 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
1516 bddc7bbd 2021-04-01 op struct vline *vl;
1519 bddc7bbd 2021-04-01 op werase(win);
1521 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1522 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
1526 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
1527 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1528 72b18268 2021-06-19 op wmove(win, l, x_offset);
1529 bddc7bbd 2021-04-01 op print_vline(win, vl);
1531 bddc7bbd 2021-04-01 op if (l == height)
1535 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
1539 bddc7bbd 2021-04-01 op redraw_help(void)
1541 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
1545 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
1547 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
1550 10346511 2021-03-17 op static inline char
1551 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1553 10346511 2021-03-17 op switch (ts) {
1554 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1555 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1556 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1557 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1562 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1565 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1566 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
1567 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1569 156f1501 2021-03-13 op werase(modeline);
1570 d5493194 2021-06-15 op wattron(modeline, modeline_face.background);
1571 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1573 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1574 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
1575 10346511 2021-03-17 op trust_status_char(tab->trust),
1576 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1578 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1580 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1581 481340cc 2021-03-11 op wprintw(modeline, "All ");
1582 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1583 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1584 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1585 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1587 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1589 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1590 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1591 46f6e974 2021-05-17 op tab->buffer.line_max,
1592 2051e653 2021-03-13 op tab->hist_cur->h);
1594 48e9d457 2021-03-06 op getyx(modeline, y, x);
1595 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1598 48e9d457 2021-03-06 op (void)max_y;
1600 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1601 48e9d457 2021-03-06 op waddstr(modeline, "-");
1603 d5493194 2021-06-15 op wattroff(modeline, modeline_face.background);
1607 9ca15951 2021-03-09 op redraw_minibuffer(void)
1609 8300dd3c 2021-03-18 op struct tab *tab;
1610 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1611 2ba66cea 2021-03-22 op char *start, *c;
1613 d5493194 2021-06-15 op wattron(minibuf, minibuffer_face.background);
1614 156f1501 2021-03-13 op werase(minibuf);
1616 22268e11 2021-03-11 op if (in_minibuffer) {
1617 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1618 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1619 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1620 22268e11 2021-03-11 op ministate.hist_off + 1,
1621 22268e11 2021-03-11 op ministate.history->len);
1623 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1625 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1626 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1627 17e5106b 2021-03-21 op : ministate.buf;
1628 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1629 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1630 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1631 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1634 17e5106b 2021-03-21 op waddstr(minibuf, start);
1637 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1638 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1639 17e5106b 2021-03-21 op ministate.curmesg);
1641 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1642 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1644 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1645 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1646 8300dd3c 2021-03-18 op tab = current_tab();
1647 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1648 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1649 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1652 2aaf475e 2021-03-13 op if (in_minibuffer)
1653 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1655 d5493194 2021-06-15 op wattroff(minibuf, minibuffer_face.background);
1659 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1661 bddc7bbd 2021-04-01 op if (side_window) {
1662 bddc7bbd 2021-04-01 op redraw_help();
1663 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1666 72b18268 2021-06-19 op restore_cursor(&tab->buffer);
1668 e19f9a04 2021-03-11 op redraw_tabline();
1669 e19f9a04 2021-03-11 op redraw_body(tab);
1670 e19f9a04 2021-03-11 op redraw_modeline(tab);
1671 e19f9a04 2021-03-11 op redraw_minibuffer();
1673 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1674 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1676 e19f9a04 2021-03-11 op if (in_minibuffer) {
1677 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1678 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1680 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1681 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1688 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1690 bddc7bbd 2021-04-01 op struct line *l;
1691 bddc7bbd 2021-04-01 op struct cmds *cmd;
1693 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1694 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1697 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1699 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1702 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1703 bddc7bbd 2021-04-01 op l->alt = NULL;
1705 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1707 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1708 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1710 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1714 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1716 bddc7bbd 2021-04-01 op struct keymap *k;
1718 bddc7bbd 2021-04-01 op const char *kn;
1720 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1721 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1722 bddc7bbd 2021-04-01 op if (*p != '\0')
1723 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1724 bddc7bbd 2021-04-01 op if (k->meta)
1725 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1726 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1727 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1729 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1731 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1732 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1734 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1739 bddc7bbd 2021-04-01 op recompute_help(void)
1741 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1743 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1744 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1745 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1746 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1750 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1752 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1753 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1754 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1755 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1757 bf992581 2021-03-12 op free(ministate.curmesg);
1759 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1760 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1761 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1763 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1764 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1765 9cb0f9ce 2021-03-10 op wrefresh(body);
1766 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1768 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1769 9cb0f9ce 2021-03-10 op wrefresh(body);
1774 740f578b 2021-03-15 op message(const char *fmt, ...)
1778 740f578b 2021-03-15 op va_start(ap, fmt);
1779 740f578b 2021-03-15 op vmessage(fmt, ap);
1784 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1786 2ba66cea 2021-03-22 op if (tab->loading_anim)
1788 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1789 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1790 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1794 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1796 8af5e5ed 2021-03-08 op struct tab *tab = d;
1798 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1800 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1801 6347dcd0 2021-03-16 op redraw_modeline(tab);
1802 6347dcd0 2021-03-16 op wrefresh(modeline);
1803 6347dcd0 2021-03-16 op wrefresh(body);
1804 6347dcd0 2021-03-16 op if (in_minibuffer)
1805 6347dcd0 2021-03-16 op wrefresh(minibuf);
1808 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1812 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1814 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1816 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1817 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1818 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1820 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1823 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1825 43a1b8d0 2021-03-09 op wrefresh(modeline);
1826 43a1b8d0 2021-03-09 op wrefresh(body);
1827 9ca15951 2021-03-09 op if (in_minibuffer)
1828 9ca15951 2021-03-09 op wrefresh(minibuf);
1832 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1834 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1835 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1836 8af5e5ed 2021-03-08 op load_url(tab, url);
1838 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1839 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1840 43a1b8d0 2021-03-09 op redraw_tab(tab);
1844 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1845 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1847 9ca15951 2021-03-09 op in_minibuffer = 1;
1848 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1849 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1851 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1853 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1854 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1855 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1856 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1857 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1858 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1859 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1861 22268e11 2021-03-11 op ministate.history = hist;
1862 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1863 22268e11 2021-03-11 op ministate.hist_off = 0;
1867 b360ebb3 2021-03-10 op exit_minibuffer(void)
1869 156f1501 2021-03-13 op werase(minibuf);
1871 9ca15951 2021-03-09 op in_minibuffer = 0;
1872 9ca15951 2021-03-09 op base_map = &global_map;
1873 9ca15951 2021-03-09 op current_map = &global_map;
1877 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1879 5cd2ebb1 2021-03-11 op struct tab *t;
1881 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1882 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1885 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1886 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1889 2eef3403 2021-04-22 op unsigned int
1890 2eef3403 2021-04-22 op tab_new_id(void)
1892 2eef3403 2021-04-22 op return tab_counter++;
1895 b1df9b71 2021-03-12 op static struct tab *
1896 1b81bc33 2021-03-15 op new_tab(const char *url)
1898 754622a2 2021-03-15 op struct tab *tab;
1900 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1901 71105afa 2021-03-16 op event_loopbreak();
1902 71105afa 2021-03-16 op return NULL;
1904 de2a69bb 2021-05-17 op tab->fd = -1;
1906 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1908 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1910 2eef3403 2021-04-22 op tab->id = tab_new_id();
1911 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1913 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1914 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1916 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1918 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1923 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1925 c7107cec 2021-04-01 op new_tab(url);
1931 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1932 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1936 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1938 c92e529c 2021-06-15 op char path[PATH_MAX];
1939 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1940 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1942 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1943 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1945 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1946 941b3761 2021-03-18 op switch (ch) {
1949 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1952 c92e529c 2021-06-15 op configtest = 1;
1962 941b3761 2021-03-18 op argc -= optind;
1963 941b3761 2021-03-18 op argv += optind;
1965 c92e529c 2021-06-15 op parseconfig(path, fonf);
1966 c92e529c 2021-06-15 op if (configtest){
1967 c92e529c 2021-06-15 op puts("config OK");
1971 941b3761 2021-03-18 op if (argc != 0)
1972 941b3761 2021-03-18 op url = argv[0];
1974 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1976 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1977 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1979 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1981 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1982 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1983 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1985 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1986 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1987 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1989 bddc7bbd 2021-04-01 op /* initialize help window */
1990 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1992 9ca15951 2021-03-09 op base_map = &global_map;
1993 f832146f 2021-03-09 op current_map = &global_map;
1994 f832146f 2021-03-09 op load_default_keys();
2001 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
2003 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
2005 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
2007 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
2009 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
2011 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
2014 48e9d457 2021-03-06 op body_lines = LINES-3;
2015 48e9d457 2021-03-06 op body_cols = COLS;
2017 72b18268 2021-06-19 op update_x_offset();
2019 43a1b8d0 2021-03-09 op keypad(body, TRUE);
2020 48e9d457 2021-03-06 op scrollok(body, TRUE);
2022 5e11c00c 2021-03-02 op /* non-blocking input */
2023 48e9d457 2021-03-06 op wtimeout(body, 0);
2025 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
2027 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2028 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
2030 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2031 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
2033 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
2034 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2035 c7107cec 2021-04-01 op new_tab(url);
2041 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
2043 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
2044 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
2046 3d8c2326 2021-03-18 op redraw_tabline();
2047 3d8c2326 2021-03-18 op wrefresh(tabline);
2048 3d8c2326 2021-03-18 op if (in_minibuffer)
2049 3d8c2326 2021-03-18 op wrefresh(minibuf);
2051 3d8c2326 2021-03-18 op wrefresh(body);
2055 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
2057 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
2058 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
2059 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
2060 3d8c2326 2021-03-18 op redraw_tab(tab);
2062 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
2066 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
2068 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
2069 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
2071 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2072 22268e11 2021-03-11 op &ir_history);
2073 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
2074 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
2075 5cd2ebb1 2021-03-11 op redraw_tab(tab);
2079 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2080 b3575139 2021-04-01 op unsigned int data)
2084 6f66d9bf 2021-04-24 op if (in_minibuffer) {
2085 6f66d9bf 2021-04-24 op fn(0, data);
2089 5d1bac73 2021-03-25 op yornp_cb = fn;
2090 e49ce157 2021-04-01 op yornp_data = data;
2091 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
2092 5d1bac73 2021-03-25 op yornp_abort, NULL);
2094 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
2095 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
2096 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
2097 5d1bac73 2021-03-25 op redraw_tab(current_tab());
2101 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2102 de2a69bb 2021-05-17 op unsigned int data)
2106 de2a69bb 2021-05-17 op if (in_minibuffer)
2109 de2a69bb 2021-05-17 op read_cb = fn;
2110 de2a69bb 2021-05-17 op read_data = data;
2111 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
2112 de2a69bb 2021-05-17 op &read_history);
2114 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
2115 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
2116 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
2117 de2a69bb 2021-05-17 op redraw_tab(current_tab());
2121 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
2125 740f578b 2021-03-15 op va_start(ap, fmt);
2126 740f578b 2021-03-15 op vmessage(fmt, ap);
2131 5e11c00c 2021-03-02 op ui_end(void)