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 5e11c00c 2021-03-02 op #include <locale.h>
42 5e11c00c 2021-03-02 op #include <signal.h>
43 7953dd72 2021-03-07 op #include <stdarg.h>
44 eb259e66 2021-03-02 op #include <stdlib.h>
45 eb259e66 2021-03-02 op #include <string.h>
46 f832146f 2021-03-09 op #include <unistd.h>
48 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
49 e8a76665 2021-05-12 op #define TAB_URGENT 0x2
51 1b81bc33 2021-03-15 op #define NEW_TAB_URL "about:new"
53 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
55 f832146f 2021-03-09 op static void load_default_keys(void);
56 46f6e974 2021-05-17 op static void restore_cursor(struct buffer*);
58 870210fb 2021-03-26 op static void global_key_unbound(void);
59 22268e11 2021-03-11 op static void minibuffer_hist_save_entry(void);
60 22268e11 2021-03-11 op static void minibuffer_taint_hist(void);
61 5cd2ebb1 2021-03-11 op static void minibuffer_self_insert(void);
62 9ca15951 2021-03-09 op static void eecmd_self_insert(void);
63 b360ebb3 2021-03-10 op static void eecmd_select(void);
64 5cd2ebb1 2021-03-11 op static void ir_self_insert(void);
65 5cd2ebb1 2021-03-11 op static void ir_select(void);
66 5cd2ebb1 2021-03-11 op static void lu_self_insert(void);
67 5cd2ebb1 2021-03-11 op static void lu_select(void);
68 740f578b 2021-03-15 op static void bp_select(void);
69 5d1bac73 2021-03-25 op static void yornp_self_insert(void);
70 5d1bac73 2021-03-25 op static void yornp_abort(void);
71 de2a69bb 2021-05-17 op static void read_self_insert(void);
72 de2a69bb 2021-05-17 op static void read_abort(void);
73 de2a69bb 2021-05-17 op static void read_select(void);
75 46f6e974 2021-05-17 op static struct vline *nth_line(struct buffer*, size_t);
76 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
77 46f6e974 2021-05-17 op static struct buffer *current_buffer(void);
78 8947c1f2 2021-03-21 op static int readkey(void);
79 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
80 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
81 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
82 a8a482c8 2021-05-17 op static void handle_resize_nodelay(int, short, void*);
83 46f6e974 2021-05-17 op static int wrap_page(struct buffer*, int);
84 bddc7bbd 2021-04-01 op static void print_vline(WINDOW*, struct vline*);
85 8af5e5ed 2021-03-08 op static void redraw_tabline(void);
86 46f6e974 2021-05-17 op static void redraw_window(WINDOW*, int, struct buffer*);
87 bddc7bbd 2021-04-01 op static void redraw_help(void);
88 e19f9a04 2021-03-11 op static void redraw_body(struct tab*);
89 8af5e5ed 2021-03-08 op static void redraw_modeline(struct tab*);
90 9ca15951 2021-03-09 op static void redraw_minibuffer(void);
91 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
92 bddc7bbd 2021-04-01 op static void emit_help_item(char*, void*);
93 bddc7bbd 2021-04-01 op static void rec_compute_help(struct kmap*, char*, size_t);
94 bddc7bbd 2021-04-01 op static void recompute_help(void);
95 740f578b 2021-03-15 op static void vmessage(const char*, va_list);
96 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
97 8af5e5ed 2021-03-08 op static void start_loading_anim(struct tab*);
98 8af5e5ed 2021-03-08 op static void update_loading_anim(int, short, void*);
99 8af5e5ed 2021-03-08 op static void stop_loading_anim(struct tab*);
100 43a1b8d0 2021-03-09 op static void load_url_in_tab(struct tab*, const char*);
101 3148eeac 2021-03-13 op static void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead*);
102 b360ebb3 2021-03-10 op static void exit_minibuffer(void);
103 5cd2ebb1 2021-03-11 op static void switch_to_tab(struct tab*);
104 1b81bc33 2021-03-15 op static struct tab *new_tab(const char*);
105 c7107cec 2021-04-01 op static void session_new_tab_cb(const char*);
106 941b3761 2021-03-18 op static void usage(void);
108 8947c1f2 2021-03-21 op static struct { short meta; int key; uint32_t cp; } thiskey;
110 831deb20 2021-05-12 op static struct event resizeev;
111 831deb20 2021-05-12 op static struct timeval resize_timer = { 0, 250000 };
113 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
114 48e9d457 2021-03-06 op static int body_lines, body_cols;
116 bddc7bbd 2021-04-01 op static WINDOW *help;
117 46f6e974 2021-05-17 op static struct buffer helpwin;
118 bddc7bbd 2021-04-01 op static int help_lines, help_cols;
120 bddc7bbd 2021-04-01 op static int side_window;
122 48e9d457 2021-03-06 op static struct event clminibufev;
123 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
124 8af5e5ed 2021-03-08 op static struct timeval loadingev_timer = { 0, 250000 };
126 bcb0b073 2021-03-07 op static uint32_t tab_counter;
128 7c7d7bb7 2021-03-10 op static char keybuf[64];
130 b3575139 2021-04-01 op static void (*yornp_cb)(int, unsigned int);
131 b3575139 2021-04-01 op static unsigned int yornp_data;
133 de2a69bb 2021-05-17 op static void (*read_cb)(const char*, unsigned int);
134 de2a69bb 2021-05-17 op static unsigned int read_data;
136 9ca15951 2021-03-09 op struct kmap global_map,
137 fa3fd864 2021-03-10 op minibuffer_map,
138 9ca15951 2021-03-09 op *current_map,
141 3148eeac 2021-03-13 op static struct histhead eecmd_history,
144 de2a69bb 2021-05-17 op read_history;
146 9ca15951 2021-03-09 op static int in_minibuffer;
148 9ca15951 2021-03-09 op static struct {
149 2ba66cea 2021-03-22 op char *curmesg;
151 5d1bac73 2021-03-25 op char prompt[64];
152 2ba66cea 2021-03-22 op void (*donefn)(void);
153 2ba66cea 2021-03-22 op void (*abortfn)(void);
155 2ba66cea 2021-03-22 op char buf[1025];
156 2ba66cea 2021-03-22 op struct line line;
157 2ba66cea 2021-03-22 op struct vline vline;
158 46f6e974 2021-05-17 op struct buffer buffer;
160 3148eeac 2021-03-13 op struct histhead *history;
161 3148eeac 2021-03-13 op struct hist *hist_cur;
162 17e5106b 2021-03-21 op size_t hist_off;
165 f95d82dd 2021-03-11 op struct lineprefix {
166 f95d82dd 2021-03-11 op const char *prfx1;
167 f95d82dd 2021-03-11 op const char *prfx2;
168 f95d82dd 2021-03-11 op } line_prefixes[] = {
169 f95d82dd 2021-03-11 op [LINE_TEXT] = { "", "" },
170 f95d82dd 2021-03-11 op [LINE_LINK] = { "=> ", " " },
171 f95d82dd 2021-03-11 op [LINE_TITLE_1] = { "# ", " " },
172 f95d82dd 2021-03-11 op [LINE_TITLE_2] = { "## ", " " },
173 f95d82dd 2021-03-11 op [LINE_TITLE_3] = { "### ", " " },
174 f95d82dd 2021-03-11 op [LINE_ITEM] = { "* ", " " },
175 71d5deec 2021-03-19 op [LINE_QUOTE] = { "> ", " " },
176 803ff456 2021-03-17 op [LINE_PRE_START] = { "```", " " },
177 f95d82dd 2021-03-11 op [LINE_PRE_CONTENT] = { "", "" },
178 f95d82dd 2021-03-11 op [LINE_PRE_END] = { "```", "```" },
181 31840026 2021-03-16 op static struct line_face {
182 803ff456 2021-03-17 op int prefix_prop;
183 803ff456 2021-03-17 op int text_prop;
184 d3d56d8a 2021-03-11 op } line_faces[] = {
185 803ff456 2021-03-17 op [LINE_TEXT] = { 0, 0 },
186 803ff456 2021-03-17 op [LINE_LINK] = { 0, A_UNDERLINE },
187 803ff456 2021-03-17 op [LINE_TITLE_1] = { A_BOLD, A_BOLD },
188 803ff456 2021-03-17 op [LINE_TITLE_2] = { A_BOLD, A_BOLD },
189 803ff456 2021-03-17 op [LINE_TITLE_3] = { A_BOLD, A_BOLD },
190 803ff456 2021-03-17 op [LINE_ITEM] = { 0, 0 },
191 803ff456 2021-03-17 op [LINE_QUOTE] = { 0, A_DIM },
192 803ff456 2021-03-17 op [LINE_PRE_START] = { 0, 0 },
193 803ff456 2021-03-17 op [LINE_PRE_CONTENT] = { 0, 0 },
194 803ff456 2021-03-17 op [LINE_PRE_END] = { 0, 0 },
197 cb6c7aa0 2021-03-16 op static struct tab_face {
198 cb6c7aa0 2021-03-16 op int background, tab, current_tab;
199 cb6c7aa0 2021-03-16 op } tab_face = {
200 cb6c7aa0 2021-03-16 op A_REVERSE, A_REVERSE, A_NORMAL
203 f832146f 2021-03-09 op static inline void
204 46f6e974 2021-05-17 op global_set_key(const char *key, void (*fn)(struct buffer*))
206 67c8ed7f 2021-03-13 op if (!kmap_define_key(&global_map, key, fn))
210 9ca15951 2021-03-09 op static inline void
211 46f6e974 2021-05-17 op minibuffer_set_key(const char *key, void (*fn)(struct buffer*))
213 67c8ed7f 2021-03-13 op if (!kmap_define_key(&minibuffer_map, key, fn))
218 f832146f 2021-03-09 op load_default_keys(void)
220 9ca15951 2021-03-09 op /* === global map === */
223 f832146f 2021-03-09 op global_set_key("C-p", cmd_previous_line);
224 f832146f 2021-03-09 op global_set_key("C-n", cmd_next_line);
225 f832146f 2021-03-09 op global_set_key("C-f", cmd_forward_char);
226 f832146f 2021-03-09 op global_set_key("C-b", cmd_backward_char);
227 852d03e8 2021-03-17 op global_set_key("M-{", cmd_backward_paragraph);
228 852d03e8 2021-03-17 op global_set_key("M-}", cmd_forward_paragraph);
229 a329982b 2021-03-11 op global_set_key("C-a", cmd_move_beginning_of_line);
230 a329982b 2021-03-11 op global_set_key("C-e", cmd_move_end_of_line);
232 f832146f 2021-03-09 op global_set_key("M-v", cmd_scroll_up);
233 f832146f 2021-03-09 op global_set_key("C-v", cmd_scroll_down);
234 929c2d9f 2021-03-13 op global_set_key("M-space", cmd_scroll_up);
235 929c2d9f 2021-03-13 op global_set_key("space", cmd_scroll_down);
237 da191746 2021-03-28 op global_set_key("M-<", cmd_beginning_of_buffer);
238 da191746 2021-03-28 op global_set_key("M->", cmd_end_of_buffer);
240 f832146f 2021-03-09 op global_set_key("C-x C-c", cmd_kill_telescope);
242 3b4f9e49 2021-03-11 op global_set_key("C-g", cmd_clear_minibuf);
244 9ca15951 2021-03-09 op global_set_key("M-x", cmd_execute_extended_command);
245 5cd2ebb1 2021-03-11 op global_set_key("C-x C-f", cmd_load_url);
246 5cd2ebb1 2021-03-11 op global_set_key("C-x M-f", cmd_load_current_url);
248 7c7d7bb7 2021-03-10 op global_set_key("C-x t 0", cmd_tab_close);
249 fea845b6 2021-03-15 op global_set_key("C-x t 1", cmd_tab_close_other);
250 7c7d7bb7 2021-03-10 op global_set_key("C-x t 2", cmd_tab_new);
251 7c7d7bb7 2021-03-10 op global_set_key("C-x t o", cmd_tab_next);
252 7c7d7bb7 2021-03-10 op global_set_key("C-x t O", cmd_tab_previous);
253 fad72201 2021-03-28 op global_set_key("C-x t m", cmd_tab_move);
254 fad72201 2021-03-28 op global_set_key("C-x t M", cmd_tab_move_to);
256 2051e653 2021-03-13 op global_set_key("C-M-b", cmd_previous_page);
257 2051e653 2021-03-13 op global_set_key("C-M-f", cmd_next_page);
259 740f578b 2021-03-15 op global_set_key("<f7> a", cmd_bookmark_page);
260 63875195 2021-04-01 op global_set_key("<f7> <f7>", cmd_list_bookmarks);
262 f832146f 2021-03-09 op /* vi/vi-like */
263 f832146f 2021-03-09 op global_set_key("k", cmd_previous_line);
264 f832146f 2021-03-09 op global_set_key("j", cmd_next_line);
265 f832146f 2021-03-09 op global_set_key("l", cmd_forward_char);
266 f832146f 2021-03-09 op global_set_key("h", cmd_backward_char);
267 852d03e8 2021-03-17 op global_set_key("{", cmd_backward_paragraph);
268 852d03e8 2021-03-17 op global_set_key("}", cmd_forward_paragraph);
269 a329982b 2021-03-11 op global_set_key("^", cmd_move_beginning_of_line);
270 a329982b 2021-03-11 op global_set_key("$", cmd_move_end_of_line);
272 ed44414d 2021-03-09 op global_set_key("K", cmd_scroll_line_up);
273 ed44414d 2021-03-09 op global_set_key("J", cmd_scroll_line_down);
275 da191746 2021-03-28 op global_set_key("g g", cmd_beginning_of_buffer);
276 da191746 2021-03-28 op global_set_key("G", cmd_end_of_buffer);
278 a02335e9 2021-03-15 op global_set_key("g D", cmd_tab_close);
279 a02335e9 2021-03-15 op global_set_key("g N", cmd_tab_new);
280 a02335e9 2021-03-15 op global_set_key("g t", cmd_tab_next);
281 a02335e9 2021-03-15 op global_set_key("g T", cmd_tab_previous);
282 fad72201 2021-03-28 op global_set_key("g M-t", cmd_tab_move);
283 fad72201 2021-03-28 op global_set_key("g M-T", cmd_tab_move_to);
285 2051e653 2021-03-13 op global_set_key("H", cmd_previous_page);
286 2051e653 2021-03-13 op global_set_key("L", cmd_next_page);
289 f832146f 2021-03-09 op global_set_key("q", cmd_kill_telescope);
291 3b4f9e49 2021-03-11 op global_set_key("esc", cmd_clear_minibuf);
293 9ca15951 2021-03-09 op global_set_key(":", cmd_execute_extended_command);
296 f832146f 2021-03-09 op global_set_key("<up>", cmd_previous_line);
297 f832146f 2021-03-09 op global_set_key("<down>", cmd_next_line);
298 f832146f 2021-03-09 op global_set_key("<right>", cmd_forward_char);
299 f832146f 2021-03-09 op global_set_key("<left>", cmd_backward_char);
300 ed44414d 2021-03-09 op global_set_key("<prior>", cmd_scroll_up);
301 ed44414d 2021-03-09 op global_set_key("<next>", cmd_scroll_down);
303 2051e653 2021-03-13 op global_set_key("M-<left>", cmd_previous_page);
304 2051e653 2021-03-13 op global_set_key("M-<right>", cmd_next_page);
306 f832146f 2021-03-09 op /* "ncurses standard" */
307 f832146f 2021-03-09 op global_set_key("C-l", cmd_redraw);
310 bddc7bbd 2021-04-01 op global_set_key("<f1>", cmd_toggle_help);
311 f832146f 2021-03-09 op global_set_key("C-m", cmd_push_button);
312 b1df9b71 2021-03-12 op global_set_key("M-enter", cmd_push_button_new_tab);
313 a8c28919 2021-03-16 op global_set_key("M-tab", cmd_previous_button);
314 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
316 fa3fd864 2021-03-10 op /* === minibuffer map === */
317 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
318 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
319 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
320 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
321 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
322 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
323 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
325 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
326 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
327 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
328 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
329 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
330 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
331 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
332 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
333 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
335 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
336 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
337 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
338 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
342 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
344 452589f7 2021-03-16 op struct vline *vl;
345 452589f7 2021-03-16 op const char *prfx;
347 46f6e974 2021-05-17 op vl = buffer->current_line;
348 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
349 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
351 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
353 452589f7 2021-03-16 op if (vl != NULL) {
354 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
355 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
360 46f6e974 2021-05-17 op cmd_previous_line(struct buffer *buffer)
362 452589f7 2021-03-16 op struct vline *vl;
364 46f6e974 2021-05-17 op if (buffer->current_line == NULL
365 46f6e974 2021-05-17 op || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
368 46f6e974 2021-05-17 op if (--buffer->curs_y < 0) {
369 46f6e974 2021-05-17 op buffer->curs_y = 0;
370 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
374 46f6e974 2021-05-17 op buffer->current_line = vl;
375 46f6e974 2021-05-17 op restore_cursor(buffer);
379 46f6e974 2021-05-17 op cmd_next_line(struct buffer *buffer)
381 452589f7 2021-03-16 op struct vline *vl;
383 46f6e974 2021-05-17 op if (buffer->current_line == NULL
384 46f6e974 2021-05-17 op || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
387 46f6e974 2021-05-17 op if (++buffer->curs_y > body_lines-1) {
388 46f6e974 2021-05-17 op buffer->curs_y = body_lines-1;
389 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
393 46f6e974 2021-05-17 op buffer->current_line = vl;
394 46f6e974 2021-05-17 op restore_cursor(buffer);
398 46f6e974 2021-05-17 op cmd_backward_char(struct buffer *buffer)
400 46f6e974 2021-05-17 op if (buffer->cpoff != 0)
401 46f6e974 2021-05-17 op buffer->cpoff--;
402 46f6e974 2021-05-17 op restore_cursor(buffer);
406 46f6e974 2021-05-17 op cmd_forward_char(struct buffer *buffer)
408 3a20fb6c 2021-05-12 op size_t len = 0;
410 46f6e974 2021-05-17 op if (buffer->current_line->line != NULL)
411 46f6e974 2021-05-17 op len = utf8_cplen(buffer->current_line->line);
412 46f6e974 2021-05-17 op if (++buffer->cpoff > len)
413 46f6e974 2021-05-17 op buffer->cpoff = len;
414 46f6e974 2021-05-17 op restore_cursor(buffer);
418 46f6e974 2021-05-17 op cmd_backward_paragraph(struct buffer *buffer)
421 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
422 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
423 852d03e8 2021-03-17 op message("No previous paragraph");
426 46f6e974 2021-05-17 op cmd_previous_line(buffer);
427 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
428 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
432 46f6e974 2021-05-17 op cmd_forward_paragraph(struct buffer *buffer)
435 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
436 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
437 852d03e8 2021-03-17 op message("No next paragraph");
440 46f6e974 2021-05-17 op cmd_next_line(buffer);
441 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
442 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
446 46f6e974 2021-05-17 op cmd_move_beginning_of_line(struct buffer *buffer)
448 46f6e974 2021-05-17 op buffer->cpoff = 0;
449 46f6e974 2021-05-17 op restore_cursor(buffer);
453 46f6e974 2021-05-17 op cmd_move_end_of_line(struct buffer *buffer)
455 9a25f829 2021-03-14 op struct vline *vl;
457 46f6e974 2021-05-17 op vl = buffer->current_line;
458 452589f7 2021-03-16 op if (vl->line == NULL)
460 46f6e974 2021-05-17 op buffer->cpoff = utf8_cplen(vl->line);
461 46f6e974 2021-05-17 op restore_cursor(buffer);
465 46f6e974 2021-05-17 op cmd_redraw(struct buffer *buffer)
467 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
471 46f6e974 2021-05-17 op cmd_scroll_line_up(struct buffer *buffer)
473 9a25f829 2021-03-14 op struct vline *vl;
475 46f6e974 2021-05-17 op if (buffer->line_off == 0)
478 46f6e974 2021-05-17 op vl = nth_line(buffer, --buffer->line_off);
479 48e9d457 2021-03-06 op wscrl(body, -1);
480 48e9d457 2021-03-06 op wmove(body, 0, 0);
481 bddc7bbd 2021-04-01 op print_vline(body, vl);
483 46f6e974 2021-05-17 op buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
484 46f6e974 2021-05-17 op restore_cursor(buffer);
488 46f6e974 2021-05-17 op cmd_scroll_line_down(struct buffer *buffer)
490 9a25f829 2021-03-14 op struct vline *vl;
492 46f6e974 2021-05-17 op vl = buffer->current_line;
493 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
495 46f6e974 2021-05-17 op buffer->current_line = vl;
497 46f6e974 2021-05-17 op buffer->line_off++;
498 48e9d457 2021-03-06 op wscrl(body, 1);
500 46f6e974 2021-05-17 op if (buffer->line_max - buffer->line_off < (size_t)body_lines)
503 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off + body_lines-1);
504 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
505 bddc7bbd 2021-04-01 op print_vline(body, vl);
507 46f6e974 2021-05-17 op restore_cursor(buffer);
511 46f6e974 2021-05-17 op cmd_scroll_up(struct buffer *buffer)
515 cce62e74 2021-04-25 op off = body_lines-1;
517 ed44414d 2021-03-09 op for (; off > 0; --off)
518 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
522 46f6e974 2021-05-17 op cmd_scroll_down(struct buffer *buffer)
526 cce62e74 2021-04-25 op off = body_lines-1;
528 338eecdc 2021-03-12 op for (; off > 0; --off)
529 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
533 46f6e974 2021-05-17 op cmd_beginning_of_buffer(struct buffer *buffer)
535 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
536 46f6e974 2021-05-17 op buffer->line_off = 0;
537 46f6e974 2021-05-17 op buffer->curs_y = 0;
538 46f6e974 2021-05-17 op buffer->cpoff = 0;
539 46f6e974 2021-05-17 op restore_cursor(buffer);
543 46f6e974 2021-05-17 op cmd_end_of_buffer(struct buffer *buffer)
547 46f6e974 2021-05-17 op off = buffer->line_max - body_lines;
548 e19f9a04 2021-03-11 op off = MAX(0, off);
550 46f6e974 2021-05-17 op buffer->line_off = off;
551 46f6e974 2021-05-17 op buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
553 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
554 46f6e974 2021-05-17 op buffer->cpoff = body_cols;
555 46f6e974 2021-05-17 op restore_cursor(buffer);
559 46f6e974 2021-05-17 op cmd_kill_telescope(struct buffer *buffer)
561 c7107cec 2021-04-01 op save_session();
562 1d08c280 2021-03-06 op event_loopbreak();
566 46f6e974 2021-05-17 op cmd_push_button(struct buffer *buffer)
568 9a25f829 2021-03-14 op struct vline *vl;
571 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
572 46f6e974 2021-05-17 op if (nth >= buffer->line_max)
574 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
575 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
578 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), vl->parent->alt);
582 46f6e974 2021-05-17 op cmd_push_button_new_tab(struct buffer *buffer)
584 9a25f829 2021-03-14 op struct vline *vl;
587 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
588 46f6e974 2021-05-17 op if (nth > buffer->line_max)
590 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
591 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
594 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
598 46f6e974 2021-05-17 op cmd_previous_button(struct buffer *buffer)
601 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
602 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
603 a8c28919 2021-03-16 op message("No previous link");
606 46f6e974 2021-05-17 op cmd_previous_line(buffer);
607 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
611 46f6e974 2021-05-17 op cmd_next_button(struct buffer *buffer)
614 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
615 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
616 a8c28919 2021-03-16 op message("No next link");
619 46f6e974 2021-05-17 op cmd_next_line(buffer);
620 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
624 46f6e974 2021-05-17 op cmd_previous_page(struct buffer *buffer)
626 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
628 2051e653 2021-03-13 op if (!load_previous_page(tab))
629 2051e653 2021-03-13 op message("No previous page");
631 c40c2250 2021-03-14 op start_loading_anim(tab);
635 46f6e974 2021-05-17 op cmd_next_page(struct buffer *buffer)
637 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
639 2051e653 2021-03-13 op if (!load_next_page(tab))
640 2051e653 2021-03-13 op message("No next page");
642 c40c2250 2021-03-14 op start_loading_anim(tab);
646 46f6e974 2021-05-17 op cmd_clear_minibuf(struct buffer *buffer)
648 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
652 46f6e974 2021-05-17 op cmd_execute_extended_command(struct buffer *buffer)
656 2ba66cea 2021-03-22 op if (in_minibuffer) {
657 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
661 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
662 22268e11 2021-03-11 op &eecmd_history);
664 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
665 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
667 9ca15951 2021-03-09 op if (thiskey.meta)
668 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
670 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
672 c3fcefe6 2021-05-12 op if (thiskey.meta)
673 c3fcefe6 2021-05-12 op strlcat(ministate.prompt, " ", len);
677 46f6e974 2021-05-17 op cmd_tab_close(struct buffer *buffer)
679 2ba66cea 2021-03-22 op struct tab *tab, *t;
681 2ba66cea 2021-03-22 op tab = current_tab();
682 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
683 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
684 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
688 2ba66cea 2021-03-22 op if (evtimer_pending(&tab->loadingev, NULL))
689 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
691 7c7d7bb7 2021-03-10 op stop_tab(tab);
693 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
694 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
695 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
698 9d24c64f 2021-03-17 op switch_to_tab(t);
702 46f6e974 2021-05-17 op cmd_tab_close_other(struct buffer *buffer)
704 2ff75826 2021-03-26 op struct tab *t, *i;
706 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
707 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
711 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
717 46f6e974 2021-05-17 op cmd_tab_new(struct buffer *buffer)
719 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
723 46f6e974 2021-05-17 op cmd_tab_next(struct buffer *buffer)
725 2ba66cea 2021-03-22 op struct tab *tab, *t;
727 2ba66cea 2021-03-22 op tab = current_tab();
728 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
730 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
731 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
732 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
733 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
737 46f6e974 2021-05-17 op cmd_tab_previous(struct buffer *buffer)
739 2ba66cea 2021-03-22 op struct tab *tab, *t;
741 2ba66cea 2021-03-22 op tab = current_tab();
742 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
744 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
745 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
746 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
747 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
751 46f6e974 2021-05-17 op cmd_tab_move(struct buffer *buffer)
753 fad72201 2021-03-28 op struct tab *tab, *t;
755 fad72201 2021-03-28 op tab = current_tab();
756 fad72201 2021-03-28 op t = TAILQ_NEXT(tab, tabs);
757 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
759 fad72201 2021-03-28 op if (t == NULL)
760 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
762 fad72201 2021-03-28 op TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
766 46f6e974 2021-05-17 op cmd_tab_move_to(struct buffer *buffer)
768 fad72201 2021-03-28 op struct tab *tab, *t;
770 fad72201 2021-03-28 op tab = current_tab();
771 fad72201 2021-03-28 op t = TAILQ_PREV(tab, tabshead, tabs);
772 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
774 fad72201 2021-03-28 op if (t == NULL) {
775 fad72201 2021-03-28 op if (TAILQ_EMPTY(&tabshead))
776 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
778 fad72201 2021-03-28 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
780 fad72201 2021-03-28 op TAILQ_INSERT_BEFORE(t, tab, tabs);
784 46f6e974 2021-05-17 op cmd_load_url(struct buffer *buffer)
786 2ba66cea 2021-03-22 op if (in_minibuffer) {
787 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
791 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
792 22268e11 2021-03-11 op &lu_history);
793 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
794 2d13108f 2021-04-30 op strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
795 46f6e974 2021-05-17 op cmd_move_end_of_line(&ministate.buffer);
799 46f6e974 2021-05-17 op cmd_load_current_url(struct buffer *buffer)
801 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
803 2ba66cea 2021-03-22 op if (in_minibuffer) {
804 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
808 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
809 22268e11 2021-03-11 op &lu_history);
810 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
811 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
812 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
816 46f6e974 2021-05-17 op cmd_bookmark_page(struct buffer *buffer)
818 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
820 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
821 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
822 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
823 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
827 46f6e974 2021-05-17 op cmd_list_bookmarks(struct buffer *buffer)
829 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), "about:bookmarks");
833 46f6e974 2021-05-17 op cmd_toggle_help(struct buffer *buffer)
835 bddc7bbd 2021-04-01 op side_window = !side_window;
836 bddc7bbd 2021-04-01 op if (side_window)
837 bddc7bbd 2021-04-01 op recompute_help();
840 7cdf12cb 2021-05-12 op * ugly hack, but otherwise the window doesn't get updated
841 bddc7bbd 2021-04-01 op * until I call handle_resize a second time (i.e. C-l). I
842 7cdf12cb 2021-05-12 op * will be happy to know why something like this is needed.
844 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
845 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
849 46f6e974 2021-05-17 op cmd_mini_delete_char(struct buffer *buffer)
853 2ba66cea 2021-03-22 op if (!in_minibuffer) {
854 2ba66cea 2021-03-22 op message("text is read-only");
858 3eecee9e 2021-03-12 op minibuffer_taint_hist();
860 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
861 2ba66cea 2021-03-22 op if (*c == '\0')
863 2ba66cea 2021-03-22 op n = utf8_next_cp(c);
865 2ba66cea 2021-03-22 op memmove(c, n, strlen(n)+1);
869 46f6e974 2021-05-17 op cmd_mini_delete_backward_char(struct buffer *buffer)
871 2ba66cea 2021-03-22 op char *c, *p, *start;
873 2ba66cea 2021-03-22 op if (!in_minibuffer) {
874 2ba66cea 2021-03-22 op message("text is read-only");
878 3eecee9e 2021-03-12 op minibuffer_taint_hist();
880 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
881 46f6e974 2021-05-17 op start = buffer->current_line->line;
882 2ba66cea 2021-03-22 op if (c == start)
884 2ba66cea 2021-03-22 op p = utf8_prev_cp(c-1, start);
886 2ba66cea 2021-03-22 op memmove(p, c, strlen(c)+1);
887 46f6e974 2021-05-17 op buffer->cpoff--;
891 46f6e974 2021-05-17 op cmd_mini_kill_line(struct buffer *buffer)
895 2ba66cea 2021-03-22 op if (!in_minibuffer) {
896 2ba66cea 2021-03-22 op message("text is read-only");
900 3eecee9e 2021-03-12 op minibuffer_taint_hist();
901 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
906 46f6e974 2021-05-17 op cmd_mini_abort(struct buffer *buffer)
908 2ba66cea 2021-03-22 op if (!in_minibuffer)
911 17e5106b 2021-03-21 op ministate.abortfn();
915 46f6e974 2021-05-17 op cmd_mini_complete_and_exit(struct buffer *buffer)
917 2ba66cea 2021-03-22 op if (!in_minibuffer)
920 22268e11 2021-03-11 op minibuffer_taint_hist();
921 b360ebb3 2021-03-10 op ministate.donefn();
925 46f6e974 2021-05-17 op cmd_mini_previous_history_element(struct buffer *buffer)
927 22268e11 2021-03-11 op if (ministate.history == NULL) {
928 22268e11 2021-03-11 op message("No history");
932 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
933 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
934 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
935 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
936 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
937 22268e11 2021-03-11 op message("No prev item");
939 22268e11 2021-03-11 op ministate.hist_off--;
942 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
943 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
947 46f6e974 2021-05-17 op cmd_mini_next_history_element(struct buffer *buffer)
949 22268e11 2021-03-11 op if (ministate.history == NULL) {
950 22268e11 2021-03-11 op message("No history");
954 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
955 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
956 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
957 22268e11 2021-03-11 op ministate.hist_off = 0;
958 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
959 22268e11 2021-03-11 op message("No next item");
961 22268e11 2021-03-11 op ministate.hist_off++;
964 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
965 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
969 870210fb 2021-03-26 op global_key_unbound(void)
971 870210fb 2021-03-26 op message("%s is undefined", keybuf);
975 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
977 3148eeac 2021-03-13 op struct hist *hist;
979 22268e11 2021-03-11 op if (ministate.history == NULL)
982 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
985 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
987 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
988 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
990 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
991 22268e11 2021-03-11 op ministate.history->len++;
995 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
996 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
997 22268e11 2021-03-11 op * navigation" thing.
1000 22268e11 2021-03-11 op minibuffer_taint_hist(void)
1002 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
1005 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
1006 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1010 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
1012 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
1015 22268e11 2021-03-11 op minibuffer_taint_hist();
1017 17e5106b 2021-03-21 op if (thiskey.cp == 0)
1020 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
1021 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
1022 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
1025 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
1026 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
1027 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
1031 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
1033 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
1034 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
1035 5cd2ebb1 2021-03-11 op global_key_unbound();
1039 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1043 b360ebb3 2021-03-10 op eecmd_select(void)
1045 870210fb 2021-03-26 op struct cmds *cmd;
1047 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1048 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
1049 2fe98292 2021-03-26 op exit_minibuffer();
1050 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
1051 46f6e974 2021-05-17 op cmd->fn(current_buffer());
1056 2fe98292 2021-03-26 op message("No match");
1060 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1062 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1066 5cd2ebb1 2021-03-11 op ir_select(void)
1068 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1069 31f1a758 2021-04-22 op struct phos_uri uri;
1070 5cd2ebb1 2021-03-11 op struct tab *tab;
1072 5cd2ebb1 2021-03-11 op tab = current_tab();
1074 5cd2ebb1 2021-03-11 op exit_minibuffer();
1075 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1077 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1078 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
1079 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
1080 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
1081 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1085 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1087 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
1088 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
1089 5cd2ebb1 2021-03-11 op global_key_unbound();
1093 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1097 5cd2ebb1 2021-03-11 op lu_select(void)
1099 5cd2ebb1 2021-03-11 op exit_minibuffer();
1100 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1101 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1105 740f578b 2021-03-15 op bp_select(void)
1107 740f578b 2021-03-15 op exit_minibuffer();
1108 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1109 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1111 740f578b 2021-03-15 op message("Abort.");
1115 5d1bac73 2021-03-25 op yornp_self_insert(void)
1117 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
1118 5d1bac73 2021-03-25 op message("Please answer y or n");
1122 5d1bac73 2021-03-25 op exit_minibuffer();
1123 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
1127 5d1bac73 2021-03-25 op yornp_abort(void)
1129 5d1bac73 2021-03-25 op exit_minibuffer();
1130 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
1134 de2a69bb 2021-05-17 op read_self_insert(void)
1136 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1137 de2a69bb 2021-05-17 op global_key_unbound();
1141 de2a69bb 2021-05-17 op minibuffer_self_insert();
1145 de2a69bb 2021-05-17 op read_abort(void)
1147 de2a69bb 2021-05-17 op exit_minibuffer();
1148 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
1152 de2a69bb 2021-05-17 op read_select(void)
1154 de2a69bb 2021-05-17 op exit_minibuffer();
1155 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
1156 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
1159 9a25f829 2021-03-14 op static struct vline *
1160 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
1162 9a25f829 2021-03-14 op struct vline *vl;
1166 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
1172 48e9d457 2021-03-06 op /* unreachable */
1176 5e11c00c 2021-03-02 op static struct tab *
1177 5e11c00c 2021-03-02 op current_tab(void)
1179 5e11c00c 2021-03-02 op struct tab *t;
1181 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1182 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1186 5e11c00c 2021-03-02 op /* unreachable */
1190 46f6e974 2021-05-17 op static struct buffer *
1191 46f6e974 2021-05-17 op current_buffer(void)
1193 2ba66cea 2021-03-22 op if (in_minibuffer)
1194 46f6e974 2021-05-17 op return &ministate.buffer;
1195 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
1199 8947c1f2 2021-03-21 op readkey(void)
1201 8947c1f2 2021-03-21 op uint32_t state = 0;
1203 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
1206 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
1207 8947c1f2 2021-03-21 op if (thiskey.meta) {
1208 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1209 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1210 c314a314 2021-03-11 op thiskey.meta = 0;
1211 9ca15951 2021-03-09 op thiskey.key = 27;
1215 8947c1f2 2021-03-21 op thiskey.cp = 0;
1216 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
1218 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1220 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
1221 8947c1f2 2021-03-21 op message("Error decoding user input");
1231 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
1233 8947c1f2 2021-03-21 op struct keymap *k;
1234 8947c1f2 2021-03-21 op const char *keyname;
1235 8947c1f2 2021-03-21 op char tmp[5] = {0};
1237 8947c1f2 2021-03-21 op if (!readkey())
1240 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1241 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1242 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1243 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1244 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
1245 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
1246 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1248 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
1249 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
1251 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
1252 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
1256 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1257 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1258 9ca15951 2021-03-09 op k->key == thiskey.key) {
1259 f832146f 2021-03-09 op if (k->fn == NULL)
1260 f832146f 2021-03-09 op current_map = &k->map;
1262 9ca15951 2021-03-09 op current_map = base_map;
1263 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1264 46f6e974 2021-05-17 op k->fn(current_buffer());
1270 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1271 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1273 7c7d7bb7 2021-03-10 op global_key_unbound();
1276 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1277 9ca15951 2021-03-09 op current_map = base_map;
1280 bddc7bbd 2021-04-01 op if (side_window)
1281 bddc7bbd 2021-04-01 op recompute_help();
1283 179f0f58 2021-03-11 op redraw_tab(current_tab());
1287 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1289 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1290 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1292 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1293 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1294 9cb0f9ce 2021-03-10 op wrefresh(body);
1295 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1297 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1298 9cb0f9ce 2021-03-10 op wrefresh(body);
1303 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1305 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1306 831deb20 2021-05-12 op event_del(&resizeev);
1308 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1309 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
1313 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
1315 1d08c280 2021-03-06 op struct tab *tab;
1321 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1323 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1324 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1326 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1327 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1329 48e9d457 2021-03-06 op body_lines = LINES-3;
1330 bd9637e9 2021-03-06 op body_cols = COLS;
1332 bddc7bbd 2021-04-01 op if (side_window) {
1333 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
1334 bddc7bbd 2021-04-01 op help_lines = LINES-3;
1335 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
1336 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
1338 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1340 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
1341 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
1343 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
1345 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
1347 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1349 1d08c280 2021-03-06 op tab = current_tab();
1351 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1352 1d08c280 2021-03-06 op redraw_tab(tab);
1356 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
1358 452589f7 2021-03-16 op struct line *l;
1359 452589f7 2021-03-16 op const struct line *orig;
1360 d511dc85 2021-03-16 op struct vline *vl;
1361 452589f7 2021-03-16 op const char *prfx;
1363 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
1365 46f6e974 2021-05-17 op : buffer->current_line->parent;
1366 46f6e974 2021-05-17 op buffer->current_line = NULL;
1368 46f6e974 2021-05-17 op buffer->curs_y = 0;
1369 46f6e974 2021-05-17 op buffer->line_off = 0;
1371 46f6e974 2021-05-17 op empty_vlist(buffer);
1373 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
1374 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1375 5e11c00c 2021-03-02 op switch (l->type) {
1376 5e11c00c 2021-03-02 op case LINE_TEXT:
1377 5e11c00c 2021-03-02 op case LINE_LINK:
1378 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1379 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1380 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1381 5e11c00c 2021-03-02 op case LINE_ITEM:
1382 5e11c00c 2021-03-02 op case LINE_QUOTE:
1383 5e11c00c 2021-03-02 op case LINE_PRE_START:
1384 5e11c00c 2021-03-02 op case LINE_PRE_END:
1385 46f6e974 2021-05-17 op wrap_text(buffer, prfx, l, width);
1387 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1388 46f6e974 2021-05-17 op hardwrap_text(buffer, l, width);
1392 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
1393 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
1394 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1397 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1398 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1400 46f6e974 2021-05-17 op buffer->current_line = vl;
1401 46f6e974 2021-05-17 op buffer->line_off--;
1406 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
1407 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
1413 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
1415 9a25f829 2021-03-14 op const char *text = vl->line;
1416 768db5bb 2021-03-12 op const char *prfx;
1417 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1418 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1420 9a25f829 2021-03-14 op if (!vl->flags)
1421 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1423 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1425 bd9637e9 2021-03-06 op if (text == NULL)
1428 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
1429 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
1430 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
1432 bddc7bbd 2021-04-01 op wattron(window, text_face);
1433 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
1434 bddc7bbd 2021-04-01 op wattroff(window, text_face);
1438 8af5e5ed 2021-03-08 op redraw_tabline(void)
1440 7c7d7bb7 2021-03-10 op struct tab *tab;
1441 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
1442 8f127baa 2021-04-22 op int current, y, truncated;
1443 dc5df781 2021-03-13 op const char *title;
1444 119f393c 2021-03-16 op char buf[25];
1446 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
1447 8f127baa 2021-04-22 op space = COLS-2;
1450 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1452 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1456 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
1459 8f127baa 2021-04-22 op ots = toskip;
1461 8f127baa 2021-04-22 op while (toskip != 0 &&
1462 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
1466 a636f50e 2021-03-16 op werase(tabline);
1467 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1468 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1469 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1471 a636f50e 2021-03-16 op truncated = 0;
1472 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1473 a636f50e 2021-03-16 op if (truncated)
1475 a636f50e 2021-03-16 op if (toskip != 0) {
1480 a636f50e 2021-03-16 op getyx(tabline, y, x);
1481 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1482 a636f50e 2021-03-16 op truncated = 1;
1484 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1486 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
1487 2051e653 2021-03-13 op title = tab->hist_cur->h;
1489 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
1490 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
1492 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
1494 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1495 119f393c 2021-03-16 op /* truncation happens */
1496 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1498 119f393c 2021-03-16 op /* pad with spaces */
1499 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1503 a329982b 2021-03-11 op if (current)
1504 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1506 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1508 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1509 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1510 119f393c 2021-03-16 op wprintw(tabline, " ");
1512 cb6c7aa0 2021-03-16 op if (current)
1513 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1515 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1518 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1519 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
1520 119f393c 2021-03-16 op waddch(tabline, ' ');
1521 a636f50e 2021-03-16 op if (truncated)
1522 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1526 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
1528 bddc7bbd 2021-04-01 op struct vline *vl;
1531 bddc7bbd 2021-04-01 op werase(win);
1533 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1534 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
1538 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
1539 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1540 bddc7bbd 2021-04-01 op wmove(win, l, 0);
1541 bddc7bbd 2021-04-01 op print_vline(win, vl);
1543 bddc7bbd 2021-04-01 op if (l == height)
1547 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
1551 bddc7bbd 2021-04-01 op redraw_help(void)
1553 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
1557 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
1559 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
1562 10346511 2021-03-17 op static inline char
1563 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1565 10346511 2021-03-17 op switch (ts) {
1566 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1567 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1568 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1569 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1574 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1577 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1578 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
1579 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1581 156f1501 2021-03-13 op werase(modeline);
1582 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
1583 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1585 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1586 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
1587 10346511 2021-03-17 op trust_status_char(tab->trust),
1588 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1590 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1592 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1593 481340cc 2021-03-11 op wprintw(modeline, "All ");
1594 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1595 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1596 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1597 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1599 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1601 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1602 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1603 46f6e974 2021-05-17 op tab->buffer.line_max,
1604 2051e653 2021-03-13 op tab->hist_cur->h);
1606 48e9d457 2021-03-06 op getyx(modeline, y, x);
1607 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1610 48e9d457 2021-03-06 op (void)max_y;
1612 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1613 48e9d457 2021-03-06 op waddstr(modeline, "-");
1617 9ca15951 2021-03-09 op redraw_minibuffer(void)
1619 8300dd3c 2021-03-18 op struct tab *tab;
1620 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1621 2ba66cea 2021-03-22 op char *start, *c;
1623 156f1501 2021-03-13 op werase(minibuf);
1625 22268e11 2021-03-11 op if (in_minibuffer) {
1626 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1627 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1628 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1629 22268e11 2021-03-11 op ministate.hist_off + 1,
1630 22268e11 2021-03-11 op ministate.history->len);
1632 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1634 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1635 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1636 17e5106b 2021-03-21 op : ministate.buf;
1637 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1638 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1639 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1640 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1643 17e5106b 2021-03-21 op waddstr(minibuf, start);
1646 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1647 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1648 17e5106b 2021-03-21 op ministate.curmesg);
1650 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1651 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1653 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1654 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1655 8300dd3c 2021-03-18 op tab = current_tab();
1656 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1657 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1658 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1661 2aaf475e 2021-03-13 op if (in_minibuffer)
1662 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1666 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1668 bddc7bbd 2021-04-01 op if (side_window) {
1669 bddc7bbd 2021-04-01 op redraw_help();
1670 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1673 e19f9a04 2021-03-11 op redraw_tabline();
1674 e19f9a04 2021-03-11 op redraw_body(tab);
1675 e19f9a04 2021-03-11 op redraw_modeline(tab);
1676 e19f9a04 2021-03-11 op redraw_minibuffer();
1678 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1679 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1681 e19f9a04 2021-03-11 op if (in_minibuffer) {
1682 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1683 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1685 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1686 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1693 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1695 bddc7bbd 2021-04-01 op struct line *l;
1696 bddc7bbd 2021-04-01 op struct cmds *cmd;
1698 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1699 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1702 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1704 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1707 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1708 bddc7bbd 2021-04-01 op l->alt = NULL;
1710 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1712 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1713 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1715 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1719 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1721 bddc7bbd 2021-04-01 op struct keymap *k;
1723 bddc7bbd 2021-04-01 op const char *kn;
1725 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1726 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1727 bddc7bbd 2021-04-01 op if (*p != '\0')
1728 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1729 bddc7bbd 2021-04-01 op if (k->meta)
1730 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1731 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1732 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1734 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1736 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1737 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1739 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1744 bddc7bbd 2021-04-01 op recompute_help(void)
1746 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1748 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1749 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1750 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1751 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1755 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1757 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1758 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1759 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1760 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1762 bf992581 2021-03-12 op free(ministate.curmesg);
1764 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1765 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1766 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1768 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1769 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1770 9cb0f9ce 2021-03-10 op wrefresh(body);
1771 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1773 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1774 9cb0f9ce 2021-03-10 op wrefresh(body);
1779 740f578b 2021-03-15 op message(const char *fmt, ...)
1783 740f578b 2021-03-15 op va_start(ap, fmt);
1784 740f578b 2021-03-15 op vmessage(fmt, ap);
1789 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1791 2ba66cea 2021-03-22 op if (tab->loading_anim)
1793 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1794 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1795 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1799 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1801 8af5e5ed 2021-03-08 op struct tab *tab = d;
1803 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1805 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1806 6347dcd0 2021-03-16 op redraw_modeline(tab);
1807 6347dcd0 2021-03-16 op wrefresh(modeline);
1808 6347dcd0 2021-03-16 op wrefresh(body);
1809 6347dcd0 2021-03-16 op if (in_minibuffer)
1810 6347dcd0 2021-03-16 op wrefresh(minibuf);
1813 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1817 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1819 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1821 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1822 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1823 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1825 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1828 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1830 43a1b8d0 2021-03-09 op wrefresh(modeline);
1831 43a1b8d0 2021-03-09 op wrefresh(body);
1832 9ca15951 2021-03-09 op if (in_minibuffer)
1833 9ca15951 2021-03-09 op wrefresh(minibuf);
1837 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1839 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1840 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1841 8af5e5ed 2021-03-08 op load_url(tab, url);
1843 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1844 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1845 43a1b8d0 2021-03-09 op redraw_tab(tab);
1849 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1850 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1852 9ca15951 2021-03-09 op in_minibuffer = 1;
1853 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1854 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1856 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1858 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1859 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1860 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1861 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1862 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1863 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1864 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1866 22268e11 2021-03-11 op ministate.history = hist;
1867 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1868 22268e11 2021-03-11 op ministate.hist_off = 0;
1872 b360ebb3 2021-03-10 op exit_minibuffer(void)
1874 156f1501 2021-03-13 op werase(minibuf);
1876 9ca15951 2021-03-09 op in_minibuffer = 0;
1877 9ca15951 2021-03-09 op base_map = &global_map;
1878 9ca15951 2021-03-09 op current_map = &global_map;
1882 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1884 5cd2ebb1 2021-03-11 op struct tab *t;
1886 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1887 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1890 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1891 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1894 2eef3403 2021-04-22 op unsigned int
1895 2eef3403 2021-04-22 op tab_new_id(void)
1897 2eef3403 2021-04-22 op return tab_counter++;
1900 b1df9b71 2021-03-12 op static struct tab *
1901 1b81bc33 2021-03-15 op new_tab(const char *url)
1903 754622a2 2021-03-15 op struct tab *tab;
1905 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1906 71105afa 2021-03-16 op event_loopbreak();
1907 71105afa 2021-03-16 op return NULL;
1909 de2a69bb 2021-05-17 op tab->fd = -1;
1911 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1913 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1915 2eef3403 2021-04-22 op tab->id = tab_new_id();
1916 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1918 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1919 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1921 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1923 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1928 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1930 c7107cec 2021-04-01 op new_tab(url);
1936 941b3761 2021-03-18 op fprintf(stderr, "USAGE: %s [url]\n", getprogname());
1940 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1942 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1945 941b3761 2021-03-18 op while ((ch = getopt(argc, argv, "")) != -1) {
1946 941b3761 2021-03-18 op switch (ch) {
1952 941b3761 2021-03-18 op argc -= optind;
1953 941b3761 2021-03-18 op argv += optind;
1955 941b3761 2021-03-18 op if (argc != 0)
1956 941b3761 2021-03-18 op url = argv[0];
1958 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1960 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1961 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1963 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1965 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1966 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1967 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1969 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1970 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1971 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1973 bddc7bbd 2021-04-01 op /* initialize help window */
1974 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1976 9ca15951 2021-03-09 op base_map = &global_map;
1977 f832146f 2021-03-09 op current_map = &global_map;
1978 f832146f 2021-03-09 op load_default_keys();
1985 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1987 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1989 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1991 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1993 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1995 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1998 48e9d457 2021-03-06 op body_lines = LINES-3;
1999 48e9d457 2021-03-06 op body_cols = COLS;
2001 43a1b8d0 2021-03-09 op keypad(body, TRUE);
2002 48e9d457 2021-03-06 op scrollok(body, TRUE);
2004 5e11c00c 2021-03-02 op /* non-blocking input */
2005 48e9d457 2021-03-06 op wtimeout(body, 0);
2007 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
2009 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2010 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
2012 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2013 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
2015 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
2016 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2017 c7107cec 2021-04-01 op new_tab(url);
2023 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
2025 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
2026 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
2028 3d8c2326 2021-03-18 op redraw_tabline();
2029 3d8c2326 2021-03-18 op wrefresh(tabline);
2030 3d8c2326 2021-03-18 op if (in_minibuffer)
2031 3d8c2326 2021-03-18 op wrefresh(minibuf);
2033 3d8c2326 2021-03-18 op wrefresh(body);
2037 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
2039 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
2040 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
2041 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
2042 3d8c2326 2021-03-18 op redraw_tab(tab);
2044 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
2048 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
2050 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
2051 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
2053 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2054 22268e11 2021-03-11 op &ir_history);
2055 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
2056 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
2057 5cd2ebb1 2021-03-11 op redraw_tab(tab);
2061 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2062 b3575139 2021-04-01 op unsigned int data)
2066 6f66d9bf 2021-04-24 op if (in_minibuffer) {
2067 6f66d9bf 2021-04-24 op fn(0, data);
2071 5d1bac73 2021-03-25 op yornp_cb = fn;
2072 e49ce157 2021-04-01 op yornp_data = data;
2073 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
2074 5d1bac73 2021-03-25 op yornp_abort, NULL);
2076 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
2077 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
2078 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
2079 5d1bac73 2021-03-25 op redraw_tab(current_tab());
2083 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2084 de2a69bb 2021-05-17 op unsigned int data)
2088 de2a69bb 2021-05-17 op if (in_minibuffer)
2091 de2a69bb 2021-05-17 op read_cb = fn;
2092 de2a69bb 2021-05-17 op read_data = data;
2093 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
2094 de2a69bb 2021-05-17 op &read_history);
2096 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
2097 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
2098 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
2099 de2a69bb 2021-05-17 op redraw_tab(current_tab());
2103 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
2107 740f578b 2021-03-15 op va_start(ap, fmt);
2108 740f578b 2021-03-15 op vmessage(fmt, ap);
2113 5e11c00c 2021-03-02 op ui_end(void)