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 8947c1f2 2021-03-21 op static struct { short meta; int key; uint32_t cp; } thiskey;
111 831deb20 2021-05-12 op static struct event resizeev;
112 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
114 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
115 48e9d457 2021-03-06 op static int body_lines, body_cols;
117 bddc7bbd 2021-04-01 op static WINDOW *help;
118 46f6e974 2021-05-17 op static struct buffer helpwin;
119 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
121 bddc7bbd 2021-04-01 op static int side_window;
123 48e9d457 2021-03-06 op static struct event clminibufev;
124 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
125 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
127 bcb0b073 2021-03-07 op static uint32_t tab_counter;
129 7c7d7bb7 2021-03-10 op static char keybuf[64];
131 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
132 b3575139 2021-04-01 op static unsigned int yornp_data;
134 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
135 de2a69bb 2021-05-17 op static unsigned int read_data;
137 9ca15951 2021-03-09 op struct kmap global_map,
138 fa3fd864 2021-03-10 op minibuffer_map,
139 9ca15951 2021-03-09 op *current_map,
142 3148eeac 2021-03-13 op static struct histhead eecmd_history,
145 de2a69bb 2021-05-17 op read_history;
147 9ca15951 2021-03-09 op static int in_minibuffer;
149 9ca15951 2021-03-09 op static struct {
150 2ba66cea 2021-03-22 op char *curmesg;
152 5d1bac73 2021-03-25 op char prompt[64];
153 2ba66cea 2021-03-22 op void (*donefn)(void);
154 2ba66cea 2021-03-22 op void (*abortfn)(void);
156 2ba66cea 2021-03-22 op char buf[1025];
157 2ba66cea 2021-03-22 op struct line line;
158 2ba66cea 2021-03-22 op struct vline vline;
159 46f6e974 2021-05-17 op struct buffer buffer;
161 3148eeac 2021-03-13 op struct histhead *history;
162 3148eeac 2021-03-13 op struct hist *hist_cur;
163 17e5106b 2021-03-21 op size_t hist_off;
166 f832146f 2021-03-09 op static inline void
167 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
169 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
173 9ca15951 2021-03-09 op static inline void
174 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
176 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
181 f832146f 2021-03-09 op load_default_keys(void)
183 9ca15951 2021-03-09 op /* === global map === */
186 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
187 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
188 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
189 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
190 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
191 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
192 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
193 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
195 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
196 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
197 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
198 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
200 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
201 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
203 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
205 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
207 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
208 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
209 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
211 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
212 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
213 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
214 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
215 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
216 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
217 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
219 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
220 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
222 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
223 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
225 f832146f 2021-03-09 op /* vi/vi-like */
226 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
227 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
228 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
229 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
230 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
231 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
232 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
233 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
235 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
236 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
238 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
239 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
241 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
242 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
243 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
244 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
245 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
246 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
248 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
249 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
252 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
254 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
256 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
259 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
260 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
261 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
262 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
263 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
264 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
266 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
267 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
269 f832146f 2021-03-09 op /* "ncurses standard" */
270 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
273 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
274 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
275 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
276 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
277 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
279 fa3fd864 2021-03-10 op /* === minibuffer map === */
280 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
281 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
282 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
283 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
284 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
285 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
286 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
288 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
289 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
290 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
291 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
292 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
293 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
294 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
295 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
296 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
298 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
299 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
300 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
301 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
305 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
307 452589f7 2021-03-16 op struct vline *vl;
308 452589f7 2021-03-16 op const char *prfx;
310 46f6e974 2021-05-17 op vl = buffer->current_line;
311 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
312 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
314 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
316 452589f7 2021-03-16 op if (vl != NULL) {
317 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
318 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
323 46f6e974 2021-05-17 op cmd_previous_line(struct buffer *buffer)
325 452589f7 2021-03-16 op struct vline *vl;
327 46f6e974 2021-05-17 op if (buffer->current_line == NULL
328 46f6e974 2021-05-17 op || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
331 46f6e974 2021-05-17 op if (--buffer->curs_y < 0) {
332 46f6e974 2021-05-17 op buffer->curs_y = 0;
333 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
337 46f6e974 2021-05-17 op buffer->current_line = vl;
338 46f6e974 2021-05-17 op restore_cursor(buffer);
342 46f6e974 2021-05-17 op cmd_next_line(struct buffer *buffer)
344 452589f7 2021-03-16 op struct vline *vl;
346 46f6e974 2021-05-17 op if (buffer->current_line == NULL
347 46f6e974 2021-05-17 op || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
350 46f6e974 2021-05-17 op if (++buffer->curs_y > body_lines-1) {
351 46f6e974 2021-05-17 op buffer->curs_y = body_lines-1;
352 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
356 46f6e974 2021-05-17 op buffer->current_line = vl;
357 46f6e974 2021-05-17 op restore_cursor(buffer);
361 46f6e974 2021-05-17 op cmd_backward_char(struct buffer *buffer)
363 46f6e974 2021-05-17 op if (buffer->cpoff != 0)
364 46f6e974 2021-05-17 op buffer->cpoff--;
365 46f6e974 2021-05-17 op restore_cursor(buffer);
369 46f6e974 2021-05-17 op cmd_forward_char(struct buffer *buffer)
371 3a20fb6c 2021-05-12 op size_t len = 0;
373 46f6e974 2021-05-17 op if (buffer->current_line->line != NULL)
374 46f6e974 2021-05-17 op len = utf8_cplen(buffer->current_line->line);
375 46f6e974 2021-05-17 op if (++buffer->cpoff > len)
376 46f6e974 2021-05-17 op buffer->cpoff = len;
377 46f6e974 2021-05-17 op restore_cursor(buffer);
381 46f6e974 2021-05-17 op cmd_backward_paragraph(struct buffer *buffer)
384 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
385 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
386 852d03e8 2021-03-17 op message("No previous paragraph");
389 46f6e974 2021-05-17 op cmd_previous_line(buffer);
390 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
391 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
395 46f6e974 2021-05-17 op cmd_forward_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_LAST(&buffer->head, vhead)) {
400 852d03e8 2021-03-17 op message("No next paragraph");
403 46f6e974 2021-05-17 op cmd_next_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_move_beginning_of_line(struct buffer *buffer)
411 46f6e974 2021-05-17 op buffer->cpoff = 0;
412 46f6e974 2021-05-17 op restore_cursor(buffer);
416 46f6e974 2021-05-17 op cmd_move_end_of_line(struct buffer *buffer)
418 9a25f829 2021-03-14 op struct vline *vl;
420 46f6e974 2021-05-17 op vl = buffer->current_line;
421 452589f7 2021-03-16 op if (vl->line == NULL)
423 46f6e974 2021-05-17 op buffer->cpoff = utf8_cplen(vl->line);
424 46f6e974 2021-05-17 op restore_cursor(buffer);
428 46f6e974 2021-05-17 op cmd_redraw(struct buffer *buffer)
430 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
434 46f6e974 2021-05-17 op cmd_scroll_line_up(struct buffer *buffer)
436 9a25f829 2021-03-14 op struct vline *vl;
438 46f6e974 2021-05-17 op if (buffer->line_off == 0)
441 46f6e974 2021-05-17 op vl = nth_line(buffer, --buffer->line_off);
442 48e9d457 2021-03-06 op wscrl(body, -1);
443 48e9d457 2021-03-06 op wmove(body, 0, 0);
444 bddc7bbd 2021-04-01 op print_vline(body, vl);
446 46f6e974 2021-05-17 op buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
447 46f6e974 2021-05-17 op restore_cursor(buffer);
451 46f6e974 2021-05-17 op cmd_scroll_line_down(struct buffer *buffer)
453 9a25f829 2021-03-14 op struct vline *vl;
455 46f6e974 2021-05-17 op vl = buffer->current_line;
456 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
458 46f6e974 2021-05-17 op buffer->current_line = vl;
460 46f6e974 2021-05-17 op buffer->line_off++;
461 48e9d457 2021-03-06 op wscrl(body, 1);
463 46f6e974 2021-05-17 op if (buffer->line_max - buffer->line_off < (size_t)body_lines)
466 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off + body_lines-1);
467 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
468 bddc7bbd 2021-04-01 op print_vline(body, vl);
470 46f6e974 2021-05-17 op restore_cursor(buffer);
474 46f6e974 2021-05-17 op cmd_scroll_up(struct buffer *buffer)
478 cce62e74 2021-04-25 op off = body_lines-1;
480 ed44414d 2021-03-09 op for (; off > 0; --off)
481 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
485 46f6e974 2021-05-17 op cmd_scroll_down(struct buffer *buffer)
489 cce62e74 2021-04-25 op off = body_lines-1;
491 338eecdc 2021-03-12 op for (; off > 0; --off)
492 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
496 46f6e974 2021-05-17 op cmd_beginning_of_buffer(struct buffer *buffer)
498 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
499 46f6e974 2021-05-17 op buffer->line_off = 0;
500 46f6e974 2021-05-17 op buffer->curs_y = 0;
501 46f6e974 2021-05-17 op buffer->cpoff = 0;
502 46f6e974 2021-05-17 op restore_cursor(buffer);
506 46f6e974 2021-05-17 op cmd_end_of_buffer(struct buffer *buffer)
510 46f6e974 2021-05-17 op off = buffer->line_max - body_lines;
511 e19f9a04 2021-03-11 op off = MAX(0, off);
513 46f6e974 2021-05-17 op buffer->line_off = off;
514 46f6e974 2021-05-17 op buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
516 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
517 46f6e974 2021-05-17 op buffer->cpoff = body_cols;
518 46f6e974 2021-05-17 op restore_cursor(buffer);
522 46f6e974 2021-05-17 op cmd_kill_telescope(struct buffer *buffer)
524 c7107cec 2021-04-01 op save_session();
525 1d08c280 2021-03-06 op event_loopbreak();
529 46f6e974 2021-05-17 op cmd_push_button(struct buffer *buffer)
531 9a25f829 2021-03-14 op struct vline *vl;
534 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
535 46f6e974 2021-05-17 op if (nth >= buffer->line_max)
537 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
538 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
541 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), vl->parent->alt);
545 46f6e974 2021-05-17 op cmd_push_button_new_tab(struct buffer *buffer)
547 9a25f829 2021-03-14 op struct vline *vl;
550 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
551 46f6e974 2021-05-17 op if (nth > buffer->line_max)
553 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
554 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
557 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
561 46f6e974 2021-05-17 op cmd_previous_button(struct buffer *buffer)
564 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
565 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
566 a8c28919 2021-03-16 op message("No previous link");
569 46f6e974 2021-05-17 op cmd_previous_line(buffer);
570 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
574 46f6e974 2021-05-17 op cmd_next_button(struct buffer *buffer)
577 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
578 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
579 a8c28919 2021-03-16 op message("No next link");
582 46f6e974 2021-05-17 op cmd_next_line(buffer);
583 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
587 46f6e974 2021-05-17 op cmd_previous_page(struct buffer *buffer)
589 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
591 2051e653 2021-03-13 op if (!load_previous_page(tab))
592 2051e653 2021-03-13 op message("No previous page");
594 c40c2250 2021-03-14 op start_loading_anim(tab);
598 46f6e974 2021-05-17 op cmd_next_page(struct buffer *buffer)
600 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
602 2051e653 2021-03-13 op if (!load_next_page(tab))
603 2051e653 2021-03-13 op message("No next page");
605 c40c2250 2021-03-14 op start_loading_anim(tab);
609 46f6e974 2021-05-17 op cmd_clear_minibuf(struct buffer *buffer)
611 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
615 46f6e974 2021-05-17 op cmd_execute_extended_command(struct buffer *buffer)
619 2ba66cea 2021-03-22 op if (in_minibuffer) {
620 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
624 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
625 22268e11 2021-03-11 op &eecmd_history);
627 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
628 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
630 9ca15951 2021-03-09 op if (thiskey.meta)
631 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
633 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
635 c3fcefe6 2021-05-12 op if (thiskey.meta)
636 c3fcefe6 2021-05-12 op strlcat(ministate.prompt, " ", len);
640 46f6e974 2021-05-17 op cmd_tab_close(struct buffer *buffer)
642 2ba66cea 2021-03-22 op struct tab *tab, *t;
644 2ba66cea 2021-03-22 op tab = current_tab();
645 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
646 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
647 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
651 2ba66cea 2021-03-22 op if (evtimer_pending(&tab->loadingev, NULL))
652 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
654 7c7d7bb7 2021-03-10 op stop_tab(tab);
656 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
657 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
658 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
661 9d24c64f 2021-03-17 op switch_to_tab(t);
665 46f6e974 2021-05-17 op cmd_tab_close_other(struct buffer *buffer)
667 2ff75826 2021-03-26 op struct tab *t, *i;
669 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
670 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
674 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
680 46f6e974 2021-05-17 op cmd_tab_new(struct buffer *buffer)
682 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
686 46f6e974 2021-05-17 op cmd_tab_next(struct buffer *buffer)
688 2ba66cea 2021-03-22 op struct tab *tab, *t;
690 2ba66cea 2021-03-22 op tab = current_tab();
691 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
693 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
694 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
695 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
696 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
700 46f6e974 2021-05-17 op cmd_tab_previous(struct buffer *buffer)
702 2ba66cea 2021-03-22 op struct tab *tab, *t;
704 2ba66cea 2021-03-22 op tab = current_tab();
705 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
707 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
708 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
709 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
710 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
714 46f6e974 2021-05-17 op cmd_tab_move(struct buffer *buffer)
716 fad72201 2021-03-28 op struct tab *tab, *t;
718 fad72201 2021-03-28 op tab = current_tab();
719 fad72201 2021-03-28 op t = TAILQ_NEXT(tab, tabs);
720 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
722 fad72201 2021-03-28 op if (t == NULL)
723 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
725 fad72201 2021-03-28 op TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
729 46f6e974 2021-05-17 op cmd_tab_move_to(struct buffer *buffer)
731 fad72201 2021-03-28 op struct tab *tab, *t;
733 fad72201 2021-03-28 op tab = current_tab();
734 fad72201 2021-03-28 op t = TAILQ_PREV(tab, tabshead, tabs);
735 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
737 fad72201 2021-03-28 op if (t == NULL) {
738 fad72201 2021-03-28 op if (TAILQ_EMPTY(&tabshead))
739 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
741 fad72201 2021-03-28 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
743 fad72201 2021-03-28 op TAILQ_INSERT_BEFORE(t, tab, tabs);
747 46f6e974 2021-05-17 op cmd_load_url(struct buffer *buffer)
749 2ba66cea 2021-03-22 op if (in_minibuffer) {
750 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
754 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
755 22268e11 2021-03-11 op &lu_history);
756 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
757 2d13108f 2021-04-30 op strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
758 46f6e974 2021-05-17 op cmd_move_end_of_line(&ministate.buffer);
762 46f6e974 2021-05-17 op cmd_load_current_url(struct buffer *buffer)
764 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
766 2ba66cea 2021-03-22 op if (in_minibuffer) {
767 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
771 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
772 22268e11 2021-03-11 op &lu_history);
773 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
774 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
775 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
779 46f6e974 2021-05-17 op cmd_bookmark_page(struct buffer *buffer)
781 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
783 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
784 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
785 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
786 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
790 46f6e974 2021-05-17 op cmd_list_bookmarks(struct buffer *buffer)
792 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), "about:bookmarks");
796 46f6e974 2021-05-17 op cmd_toggle_help(struct buffer *buffer)
798 bddc7bbd 2021-04-01 op side_window = !side_window;
799 bddc7bbd 2021-04-01 op if (side_window)
800 bddc7bbd 2021-04-01 op recompute_help();
803 7cdf12cb 2021-05-12 op * ugly hack, but otherwise the window doesn't get updated
804 bddc7bbd 2021-04-01 op * until I call handle_resize a second time (i.e. C-l). I
805 7cdf12cb 2021-05-12 op * will be happy to know why something like this is needed.
807 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
808 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
812 46f6e974 2021-05-17 op cmd_mini_delete_char(struct buffer *buffer)
816 2ba66cea 2021-03-22 op if (!in_minibuffer) {
817 2ba66cea 2021-03-22 op message("text is read-only");
821 3eecee9e 2021-03-12 op minibuffer_taint_hist();
823 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
824 2ba66cea 2021-03-22 op if (*c == '\0')
826 2ba66cea 2021-03-22 op n = utf8_next_cp(c);
828 2ba66cea 2021-03-22 op memmove(c, n, strlen(n)+1);
832 46f6e974 2021-05-17 op cmd_mini_delete_backward_char(struct buffer *buffer)
834 2ba66cea 2021-03-22 op char *c, *p, *start;
836 2ba66cea 2021-03-22 op if (!in_minibuffer) {
837 2ba66cea 2021-03-22 op message("text is read-only");
841 3eecee9e 2021-03-12 op minibuffer_taint_hist();
843 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
844 46f6e974 2021-05-17 op start = buffer->current_line->line;
845 2ba66cea 2021-03-22 op if (c == start)
847 2ba66cea 2021-03-22 op p = utf8_prev_cp(c-1, start);
849 2ba66cea 2021-03-22 op memmove(p, c, strlen(c)+1);
850 46f6e974 2021-05-17 op buffer->cpoff--;
854 46f6e974 2021-05-17 op cmd_mini_kill_line(struct buffer *buffer)
858 2ba66cea 2021-03-22 op if (!in_minibuffer) {
859 2ba66cea 2021-03-22 op message("text is read-only");
863 3eecee9e 2021-03-12 op minibuffer_taint_hist();
864 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
869 46f6e974 2021-05-17 op cmd_mini_abort(struct buffer *buffer)
871 2ba66cea 2021-03-22 op if (!in_minibuffer)
874 17e5106b 2021-03-21 op ministate.abortfn();
878 46f6e974 2021-05-17 op cmd_mini_complete_and_exit(struct buffer *buffer)
880 2ba66cea 2021-03-22 op if (!in_minibuffer)
883 22268e11 2021-03-11 op minibuffer_taint_hist();
884 b360ebb3 2021-03-10 op ministate.donefn();
888 46f6e974 2021-05-17 op cmd_mini_previous_history_element(struct buffer *buffer)
890 22268e11 2021-03-11 op if (ministate.history == NULL) {
891 22268e11 2021-03-11 op message("No history");
895 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
896 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
897 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
898 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
899 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
900 22268e11 2021-03-11 op message("No prev item");
902 22268e11 2021-03-11 op ministate.hist_off--;
905 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
906 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
910 46f6e974 2021-05-17 op cmd_mini_next_history_element(struct buffer *buffer)
912 22268e11 2021-03-11 op if (ministate.history == NULL) {
913 22268e11 2021-03-11 op message("No history");
917 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
918 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
919 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
920 22268e11 2021-03-11 op ministate.hist_off = 0;
921 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
922 22268e11 2021-03-11 op message("No next item");
924 22268e11 2021-03-11 op ministate.hist_off++;
927 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
928 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
932 870210fb 2021-03-26 op global_key_unbound(void)
934 870210fb 2021-03-26 op message("%s is undefined", keybuf);
938 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
940 3148eeac 2021-03-13 op struct hist *hist;
942 22268e11 2021-03-11 op if (ministate.history == NULL)
945 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
948 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
950 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
951 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
953 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
954 22268e11 2021-03-11 op ministate.history->len++;
958 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
959 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
960 22268e11 2021-03-11 op * navigation" thing.
963 22268e11 2021-03-11 op minibuffer_taint_hist(void)
965 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
968 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
969 22268e11 2021-03-11 op ministate.hist_cur = NULL;
973 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
975 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
978 22268e11 2021-03-11 op minibuffer_taint_hist();
980 17e5106b 2021-03-21 op if (thiskey.cp == 0)
983 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
984 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
985 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
988 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
989 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
990 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
994 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
996 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
997 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
998 5cd2ebb1 2021-03-11 op global_key_unbound();
1002 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1006 b360ebb3 2021-03-10 op eecmd_select(void)
1008 870210fb 2021-03-26 op struct cmds *cmd;
1010 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1011 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
1012 2fe98292 2021-03-26 op exit_minibuffer();
1013 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
1014 46f6e974 2021-05-17 op cmd->fn(current_buffer());
1019 2fe98292 2021-03-26 op message("No match");
1023 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1025 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1029 5cd2ebb1 2021-03-11 op ir_select(void)
1031 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1032 31f1a758 2021-04-22 op struct phos_uri uri;
1033 5cd2ebb1 2021-03-11 op struct tab *tab;
1035 5cd2ebb1 2021-03-11 op tab = current_tab();
1037 5cd2ebb1 2021-03-11 op exit_minibuffer();
1038 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1040 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1041 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
1042 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
1043 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
1044 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1048 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1050 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
1051 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
1052 5cd2ebb1 2021-03-11 op global_key_unbound();
1056 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1060 5cd2ebb1 2021-03-11 op lu_select(void)
1062 5cd2ebb1 2021-03-11 op exit_minibuffer();
1063 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1064 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1068 740f578b 2021-03-15 op bp_select(void)
1070 740f578b 2021-03-15 op exit_minibuffer();
1071 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1072 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1074 740f578b 2021-03-15 op message("Abort.");
1078 5d1bac73 2021-03-25 op yornp_self_insert(void)
1080 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
1081 5d1bac73 2021-03-25 op message("Please answer y or n");
1085 5d1bac73 2021-03-25 op exit_minibuffer();
1086 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
1090 5d1bac73 2021-03-25 op yornp_abort(void)
1092 5d1bac73 2021-03-25 op exit_minibuffer();
1093 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
1097 de2a69bb 2021-05-17 op read_self_insert(void)
1099 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1100 de2a69bb 2021-05-17 op global_key_unbound();
1104 de2a69bb 2021-05-17 op minibuffer_self_insert();
1108 de2a69bb 2021-05-17 op read_abort(void)
1110 de2a69bb 2021-05-17 op exit_minibuffer();
1111 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
1115 de2a69bb 2021-05-17 op read_select(void)
1117 de2a69bb 2021-05-17 op exit_minibuffer();
1118 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
1119 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
1122 9a25f829 2021-03-14 op static struct vline *
1123 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
1125 9a25f829 2021-03-14 op struct vline *vl;
1129 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
1135 48e9d457 2021-03-06 op /* unreachable */
1139 5e11c00c 2021-03-02 op static struct tab *
1140 5e11c00c 2021-03-02 op current_tab(void)
1142 5e11c00c 2021-03-02 op struct tab *t;
1144 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1145 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1149 5e11c00c 2021-03-02 op /* unreachable */
1153 46f6e974 2021-05-17 op static struct buffer *
1154 46f6e974 2021-05-17 op current_buffer(void)
1156 2ba66cea 2021-03-22 op if (in_minibuffer)
1157 46f6e974 2021-05-17 op return &ministate.buffer;
1158 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
1162 8947c1f2 2021-03-21 op readkey(void)
1164 8947c1f2 2021-03-21 op uint32_t state = 0;
1166 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
1169 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
1170 8947c1f2 2021-03-21 op if (thiskey.meta) {
1171 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1172 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1173 c314a314 2021-03-11 op thiskey.meta = 0;
1174 9ca15951 2021-03-09 op thiskey.key = 27;
1178 8947c1f2 2021-03-21 op thiskey.cp = 0;
1179 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
1181 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1183 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
1184 8947c1f2 2021-03-21 op message("Error decoding user input");
1194 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
1196 8947c1f2 2021-03-21 op struct keymap *k;
1197 8947c1f2 2021-03-21 op const char *keyname;
1198 8947c1f2 2021-03-21 op char tmp[5] = {0};
1200 8947c1f2 2021-03-21 op if (!readkey())
1203 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1204 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1205 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1206 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1207 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
1208 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
1209 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1211 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
1212 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
1214 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
1215 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
1219 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1220 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1221 9ca15951 2021-03-09 op k->key == thiskey.key) {
1222 f832146f 2021-03-09 op if (k->fn == NULL)
1223 f832146f 2021-03-09 op current_map = &k->map;
1225 9ca15951 2021-03-09 op current_map = base_map;
1226 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1227 46f6e974 2021-05-17 op k->fn(current_buffer());
1233 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1234 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1236 7c7d7bb7 2021-03-10 op global_key_unbound();
1239 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1240 9ca15951 2021-03-09 op current_map = base_map;
1243 bddc7bbd 2021-04-01 op if (side_window)
1244 bddc7bbd 2021-04-01 op recompute_help();
1246 179f0f58 2021-03-11 op redraw_tab(current_tab());
1250 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1252 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1253 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1255 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1256 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1257 9cb0f9ce 2021-03-10 op wrefresh(body);
1258 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1260 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1261 9cb0f9ce 2021-03-10 op wrefresh(body);
1266 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1268 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1269 831deb20 2021-05-12 op event_del(&resizeev);
1271 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1272 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
1276 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
1278 1d08c280 2021-03-06 op struct tab *tab;
1284 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1286 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1287 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1289 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1290 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1292 48e9d457 2021-03-06 op body_lines = LINES-3;
1293 bd9637e9 2021-03-06 op body_cols = COLS;
1295 bddc7bbd 2021-04-01 op if (side_window) {
1296 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
1297 bddc7bbd 2021-04-01 op help_lines = LINES-3;
1298 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
1299 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
1301 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1303 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
1304 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
1306 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
1308 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
1310 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1312 1d08c280 2021-03-06 op tab = current_tab();
1314 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1315 1d08c280 2021-03-06 op redraw_tab(tab);
1319 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
1321 452589f7 2021-03-16 op struct line *l;
1322 452589f7 2021-03-16 op const struct line *orig;
1323 d511dc85 2021-03-16 op struct vline *vl;
1324 452589f7 2021-03-16 op const char *prfx;
1326 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
1328 46f6e974 2021-05-17 op : buffer->current_line->parent;
1329 46f6e974 2021-05-17 op buffer->current_line = NULL;
1331 46f6e974 2021-05-17 op buffer->curs_y = 0;
1332 46f6e974 2021-05-17 op buffer->line_off = 0;
1334 46f6e974 2021-05-17 op empty_vlist(buffer);
1336 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
1337 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1338 5e11c00c 2021-03-02 op switch (l->type) {
1339 5e11c00c 2021-03-02 op case LINE_TEXT:
1340 5e11c00c 2021-03-02 op case LINE_LINK:
1341 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1342 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1343 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1344 5e11c00c 2021-03-02 op case LINE_ITEM:
1345 5e11c00c 2021-03-02 op case LINE_QUOTE:
1346 5e11c00c 2021-03-02 op case LINE_PRE_START:
1347 5e11c00c 2021-03-02 op case LINE_PRE_END:
1348 46f6e974 2021-05-17 op wrap_text(buffer, prfx, l, width);
1350 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1351 46f6e974 2021-05-17 op hardwrap_text(buffer, l, width);
1355 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
1356 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
1357 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1360 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1361 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1363 46f6e974 2021-05-17 op buffer->current_line = vl;
1364 46f6e974 2021-05-17 op buffer->line_off--;
1369 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
1370 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
1376 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
1378 9a25f829 2021-03-14 op const char *text = vl->line;
1379 768db5bb 2021-03-12 op const char *prfx;
1380 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1381 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1383 9a25f829 2021-03-14 op if (!vl->flags)
1384 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1386 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1388 bd9637e9 2021-03-06 op if (text == NULL)
1391 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
1392 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
1393 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
1395 bddc7bbd 2021-04-01 op wattron(window, text_face);
1396 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
1397 bddc7bbd 2021-04-01 op wattroff(window, text_face);
1401 8af5e5ed 2021-03-08 op redraw_tabline(void)
1403 7c7d7bb7 2021-03-10 op struct tab *tab;
1404 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
1405 8f127baa 2021-04-22 op int current, y, truncated;
1406 dc5df781 2021-03-13 op const char *title;
1407 119f393c 2021-03-16 op char buf[25];
1409 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
1410 8f127baa 2021-04-22 op space = COLS-2;
1413 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1415 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1419 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
1422 8f127baa 2021-04-22 op ots = toskip;
1424 8f127baa 2021-04-22 op while (toskip != 0 &&
1425 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
1429 a636f50e 2021-03-16 op werase(tabline);
1430 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1431 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1432 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1434 a636f50e 2021-03-16 op truncated = 0;
1435 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1436 a636f50e 2021-03-16 op if (truncated)
1438 a636f50e 2021-03-16 op if (toskip != 0) {
1443 a636f50e 2021-03-16 op getyx(tabline, y, x);
1444 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1445 a636f50e 2021-03-16 op truncated = 1;
1447 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1449 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
1450 2051e653 2021-03-13 op title = tab->hist_cur->h;
1452 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
1453 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
1455 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
1457 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1458 119f393c 2021-03-16 op /* truncation happens */
1459 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1461 119f393c 2021-03-16 op /* pad with spaces */
1462 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1466 a329982b 2021-03-11 op if (current)
1467 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1469 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1471 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1472 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1473 119f393c 2021-03-16 op wprintw(tabline, " ");
1475 cb6c7aa0 2021-03-16 op if (current)
1476 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1478 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1481 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1482 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
1483 119f393c 2021-03-16 op waddch(tabline, ' ');
1484 a636f50e 2021-03-16 op if (truncated)
1485 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1489 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
1491 bddc7bbd 2021-04-01 op struct vline *vl;
1494 bddc7bbd 2021-04-01 op werase(win);
1496 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1497 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
1501 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
1502 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1503 bddc7bbd 2021-04-01 op wmove(win, l, 0);
1504 bddc7bbd 2021-04-01 op print_vline(win, vl);
1506 bddc7bbd 2021-04-01 op if (l == height)
1510 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
1514 bddc7bbd 2021-04-01 op redraw_help(void)
1516 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
1520 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
1522 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
1525 10346511 2021-03-17 op static inline char
1526 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1528 10346511 2021-03-17 op switch (ts) {
1529 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1530 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1531 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1532 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1537 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1540 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1541 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
1542 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1544 156f1501 2021-03-13 op werase(modeline);
1545 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1546 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1548 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1549 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
1550 10346511 2021-03-17 op trust_status_char(tab->trust),
1551 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1553 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1555 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1556 481340cc 2021-03-11 op wprintw(modeline, "All ");
1557 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1558 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1559 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1560 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1562 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1564 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1565 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1566 46f6e974 2021-05-17 op tab->buffer.line_max,
1567 2051e653 2021-03-13 op tab->hist_cur->h);
1569 48e9d457 2021-03-06 op getyx(modeline, y, x);
1570 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1573 48e9d457 2021-03-06 op (void)max_y;
1575 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1576 48e9d457 2021-03-06 op waddstr(modeline, "-");
1580 9ca15951 2021-03-09 op redraw_minibuffer(void)
1582 8300dd3c 2021-03-18 op struct tab *tab;
1583 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1584 2ba66cea 2021-03-22 op char *start, *c;
1586 156f1501 2021-03-13 op werase(minibuf);
1588 22268e11 2021-03-11 op if (in_minibuffer) {
1589 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1590 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1591 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1592 22268e11 2021-03-11 op ministate.hist_off + 1,
1593 22268e11 2021-03-11 op ministate.history->len);
1595 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1597 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1598 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1599 17e5106b 2021-03-21 op : ministate.buf;
1600 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1601 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1602 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1603 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1606 17e5106b 2021-03-21 op waddstr(minibuf, start);
1609 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1610 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1611 17e5106b 2021-03-21 op ministate.curmesg);
1613 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1614 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1616 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1617 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1618 8300dd3c 2021-03-18 op tab = current_tab();
1619 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1620 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1621 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1624 2aaf475e 2021-03-13 op if (in_minibuffer)
1625 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1629 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1631 bddc7bbd 2021-04-01 op if (side_window) {
1632 bddc7bbd 2021-04-01 op redraw_help();
1633 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1636 e19f9a04 2021-03-11 op redraw_tabline();
1637 e19f9a04 2021-03-11 op redraw_body(tab);
1638 e19f9a04 2021-03-11 op redraw_modeline(tab);
1639 e19f9a04 2021-03-11 op redraw_minibuffer();
1641 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1642 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1644 e19f9a04 2021-03-11 op if (in_minibuffer) {
1645 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1646 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1648 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1649 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1656 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1658 bddc7bbd 2021-04-01 op struct line *l;
1659 bddc7bbd 2021-04-01 op struct cmds *cmd;
1661 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1662 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1665 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1667 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1670 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1671 bddc7bbd 2021-04-01 op l->alt = NULL;
1673 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1675 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1676 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1678 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1682 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1684 bddc7bbd 2021-04-01 op struct keymap *k;
1686 bddc7bbd 2021-04-01 op const char *kn;
1688 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1689 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1690 bddc7bbd 2021-04-01 op if (*p != '\0')
1691 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1692 bddc7bbd 2021-04-01 op if (k->meta)
1693 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1694 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1695 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1697 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1699 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1700 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1702 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1707 bddc7bbd 2021-04-01 op recompute_help(void)
1709 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1711 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1712 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1713 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1714 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1718 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1720 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1721 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1722 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1723 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1725 bf992581 2021-03-12 op free(ministate.curmesg);
1727 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1728 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1729 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1731 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1732 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1733 9cb0f9ce 2021-03-10 op wrefresh(body);
1734 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1736 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1737 9cb0f9ce 2021-03-10 op wrefresh(body);
1742 740f578b 2021-03-15 op message(const char *fmt, ...)
1746 740f578b 2021-03-15 op va_start(ap, fmt);
1747 740f578b 2021-03-15 op vmessage(fmt, ap);
1752 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1754 2ba66cea 2021-03-22 op if (tab->loading_anim)
1756 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1757 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1758 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1762 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1764 8af5e5ed 2021-03-08 op struct tab *tab = d;
1766 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1768 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1769 6347dcd0 2021-03-16 op redraw_modeline(tab);
1770 6347dcd0 2021-03-16 op wrefresh(modeline);
1771 6347dcd0 2021-03-16 op wrefresh(body);
1772 6347dcd0 2021-03-16 op if (in_minibuffer)
1773 6347dcd0 2021-03-16 op wrefresh(minibuf);
1776 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1780 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1782 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1784 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1785 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1786 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1788 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1791 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1793 43a1b8d0 2021-03-09 op wrefresh(modeline);
1794 43a1b8d0 2021-03-09 op wrefresh(body);
1795 9ca15951 2021-03-09 op if (in_minibuffer)
1796 9ca15951 2021-03-09 op wrefresh(minibuf);
1800 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1802 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1803 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1804 8af5e5ed 2021-03-08 op load_url(tab, url);
1806 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1807 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1808 43a1b8d0 2021-03-09 op redraw_tab(tab);
1812 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1813 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1815 9ca15951 2021-03-09 op in_minibuffer = 1;
1816 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1817 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1819 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1821 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1822 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1823 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1824 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1825 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1826 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1827 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1829 22268e11 2021-03-11 op ministate.history = hist;
1830 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1831 22268e11 2021-03-11 op ministate.hist_off = 0;
1835 b360ebb3 2021-03-10 op exit_minibuffer(void)
1837 156f1501 2021-03-13 op werase(minibuf);
1839 9ca15951 2021-03-09 op in_minibuffer = 0;
1840 9ca15951 2021-03-09 op base_map = &global_map;
1841 9ca15951 2021-03-09 op current_map = &global_map;
1845 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1847 5cd2ebb1 2021-03-11 op struct tab *t;
1849 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1850 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1853 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1854 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1857 2eef3403 2021-04-22 op unsigned int
1858 2eef3403 2021-04-22 op tab_new_id(void)
1860 2eef3403 2021-04-22 op return tab_counter++;
1863 b1df9b71 2021-03-12 op static struct tab *
1864 1b81bc33 2021-03-15 op new_tab(const char *url)
1866 754622a2 2021-03-15 op struct tab *tab;
1868 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1869 71105afa 2021-03-16 op event_loopbreak();
1870 71105afa 2021-03-16 op return NULL;
1872 de2a69bb 2021-05-17 op tab->fd = -1;
1874 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1876 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1878 2eef3403 2021-04-22 op tab->id = tab_new_id();
1879 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1881 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1882 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1884 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1886 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1891 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1893 c7107cec 2021-04-01 op new_tab(url);
1899 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1900 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1904 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1906 c92e529c 2021-06-15 op char path[PATH_MAX];
1907 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1908 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1910 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1911 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1913 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1914 941b3761 2021-03-18 op switch (ch) {
1917 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1920 c92e529c 2021-06-15 op configtest = 1;
1930 941b3761 2021-03-18 op argc -= optind;
1931 941b3761 2021-03-18 op argv += optind;
1933 c92e529c 2021-06-15 op parseconfig(path, fonf);
1934 c92e529c 2021-06-15 op if (configtest){
1935 c92e529c 2021-06-15 op puts("config OK");
1939 941b3761 2021-03-18 op if (argc != 0)
1940 941b3761 2021-03-18 op url = argv[0];
1942 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1944 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1945 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1947 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1949 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1950 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1951 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1953 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1954 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1955 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1957 bddc7bbd 2021-04-01 op /* initialize help window */
1958 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1960 9ca15951 2021-03-09 op base_map = &global_map;
1961 f832146f 2021-03-09 op current_map = &global_map;
1962 f832146f 2021-03-09 op load_default_keys();
1969 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1971 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1973 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1975 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1977 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1979 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1982 48e9d457 2021-03-06 op body_lines = LINES-3;
1983 48e9d457 2021-03-06 op body_cols = COLS;
1985 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1986 48e9d457 2021-03-06 op scrollok(body, TRUE);
1988 5e11c00c 2021-03-02 op /* non-blocking input */
1989 48e9d457 2021-03-06 op wtimeout(body, 0);
1991 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1993 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
1994 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
1996 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
1997 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
1999 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
2000 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2001 c7107cec 2021-04-01 op new_tab(url);
2007 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
2009 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
2010 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
2012 3d8c2326 2021-03-18 op redraw_tabline();
2013 3d8c2326 2021-03-18 op wrefresh(tabline);
2014 3d8c2326 2021-03-18 op if (in_minibuffer)
2015 3d8c2326 2021-03-18 op wrefresh(minibuf);
2017 3d8c2326 2021-03-18 op wrefresh(body);
2021 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
2023 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
2024 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
2025 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
2026 3d8c2326 2021-03-18 op redraw_tab(tab);
2028 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
2032 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
2034 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
2035 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
2037 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2038 22268e11 2021-03-11 op &ir_history);
2039 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
2040 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
2041 5cd2ebb1 2021-03-11 op redraw_tab(tab);
2045 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2046 b3575139 2021-04-01 op unsigned int data)
2050 6f66d9bf 2021-04-24 op if (in_minibuffer) {
2051 6f66d9bf 2021-04-24 op fn(0, data);
2055 5d1bac73 2021-03-25 op yornp_cb = fn;
2056 e49ce157 2021-04-01 op yornp_data = data;
2057 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
2058 5d1bac73 2021-03-25 op yornp_abort, NULL);
2060 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
2061 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
2062 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
2063 5d1bac73 2021-03-25 op redraw_tab(current_tab());
2067 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2068 de2a69bb 2021-05-17 op unsigned int data)
2072 de2a69bb 2021-05-17 op if (in_minibuffer)
2075 de2a69bb 2021-05-17 op read_cb = fn;
2076 de2a69bb 2021-05-17 op read_data = data;
2077 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
2078 de2a69bb 2021-05-17 op &read_history);
2080 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
2081 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
2082 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
2083 de2a69bb 2021-05-17 op redraw_tab(current_tab());
2087 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
2091 740f578b 2021-03-15 op va_start(ap, fmt);
2092 740f578b 2021-03-15 op vmessage(fmt, ap);
2097 5e11c00c 2021-03-02 op ui_end(void)