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 8dc60352 2021-06-15 op global_set_key("backtab", cmd_previous_button);
278 a8c28919 2021-03-16 op global_set_key("tab", cmd_next_button);
280 fa3fd864 2021-03-10 op /* === minibuffer map === */
281 b360ebb3 2021-03-10 op minibuffer_set_key("ret", cmd_mini_complete_and_exit);
282 b360ebb3 2021-03-10 op minibuffer_set_key("C-g", cmd_mini_abort);
283 b360ebb3 2021-03-10 op minibuffer_set_key("esc", cmd_mini_abort);
284 d67133bf 2021-03-12 op minibuffer_set_key("C-d", cmd_mini_delete_char);
285 d67133bf 2021-03-12 op minibuffer_set_key("del", cmd_mini_delete_backward_char);
286 3c066721 2021-03-26 op minibuffer_set_key("backspace", cmd_mini_delete_backward_char);
287 3c066721 2021-03-26 op minibuffer_set_key("C-h", cmd_mini_delete_backward_char);
289 2ba66cea 2021-03-22 op minibuffer_set_key("C-b", cmd_backward_char);
290 2ba66cea 2021-03-22 op minibuffer_set_key("C-f", cmd_forward_char);
291 2ba66cea 2021-03-22 op minibuffer_set_key("<left>", cmd_backward_char);
292 2ba66cea 2021-03-22 op minibuffer_set_key("<right>", cmd_forward_char);
293 2ba66cea 2021-03-22 op minibuffer_set_key("C-e", cmd_move_end_of_line);
294 2ba66cea 2021-03-22 op minibuffer_set_key("C-a", cmd_move_beginning_of_line);
295 2ba66cea 2021-03-22 op minibuffer_set_key("<end>", cmd_move_end_of_line);
296 2ba66cea 2021-03-22 op minibuffer_set_key("<home>", cmd_move_beginning_of_line);
297 fa3fd864 2021-03-10 op minibuffer_set_key("C-k", cmd_mini_kill_line);
299 22268e11 2021-03-11 op minibuffer_set_key("M-p", cmd_mini_previous_history_element);
300 22268e11 2021-03-11 op minibuffer_set_key("M-n", cmd_mini_next_history_element);
301 22268e11 2021-03-11 op minibuffer_set_key("<up>", cmd_mini_previous_history_element);
302 22268e11 2021-03-11 op minibuffer_set_key("<down>", cmd_mini_next_history_element);
306 46f6e974 2021-05-17 op restore_cursor(struct buffer *buffer)
308 452589f7 2021-03-16 op struct vline *vl;
309 452589f7 2021-03-16 op const char *prfx;
311 46f6e974 2021-05-17 op vl = buffer->current_line;
312 452589f7 2021-03-16 op if (vl == NULL || vl->line == NULL)
313 46f6e974 2021-05-17 op buffer->curs_x = buffer->cpoff = 0;
315 46f6e974 2021-05-17 op buffer->curs_x = utf8_snwidth(vl->line, buffer->cpoff);
317 452589f7 2021-03-16 op if (vl != NULL) {
318 452589f7 2021-03-16 op prfx = line_prefixes[vl->parent->type].prfx1;
319 46f6e974 2021-05-17 op buffer->curs_x += utf8_swidth(prfx);
324 46f6e974 2021-05-17 op cmd_previous_line(struct buffer *buffer)
326 452589f7 2021-03-16 op struct vline *vl;
328 46f6e974 2021-05-17 op if (buffer->current_line == NULL
329 46f6e974 2021-05-17 op || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
332 46f6e974 2021-05-17 op if (--buffer->curs_y < 0) {
333 46f6e974 2021-05-17 op buffer->curs_y = 0;
334 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
338 46f6e974 2021-05-17 op buffer->current_line = vl;
339 46f6e974 2021-05-17 op restore_cursor(buffer);
343 46f6e974 2021-05-17 op cmd_next_line(struct buffer *buffer)
345 452589f7 2021-03-16 op struct vline *vl;
347 46f6e974 2021-05-17 op if (buffer->current_line == NULL
348 46f6e974 2021-05-17 op || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
351 46f6e974 2021-05-17 op if (++buffer->curs_y > body_lines-1) {
352 46f6e974 2021-05-17 op buffer->curs_y = body_lines-1;
353 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
357 46f6e974 2021-05-17 op buffer->current_line = vl;
358 46f6e974 2021-05-17 op restore_cursor(buffer);
362 46f6e974 2021-05-17 op cmd_backward_char(struct buffer *buffer)
364 46f6e974 2021-05-17 op if (buffer->cpoff != 0)
365 46f6e974 2021-05-17 op buffer->cpoff--;
366 46f6e974 2021-05-17 op restore_cursor(buffer);
370 46f6e974 2021-05-17 op cmd_forward_char(struct buffer *buffer)
372 3a20fb6c 2021-05-12 op size_t len = 0;
374 46f6e974 2021-05-17 op if (buffer->current_line->line != NULL)
375 46f6e974 2021-05-17 op len = utf8_cplen(buffer->current_line->line);
376 46f6e974 2021-05-17 op if (++buffer->cpoff > len)
377 46f6e974 2021-05-17 op buffer->cpoff = len;
378 46f6e974 2021-05-17 op restore_cursor(buffer);
382 46f6e974 2021-05-17 op cmd_backward_paragraph(struct buffer *buffer)
385 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
386 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
387 852d03e8 2021-03-17 op message("No previous paragraph");
390 46f6e974 2021-05-17 op cmd_previous_line(buffer);
391 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
392 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
396 46f6e974 2021-05-17 op cmd_forward_paragraph(struct buffer *buffer)
399 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
400 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
401 852d03e8 2021-03-17 op message("No next paragraph");
404 46f6e974 2021-05-17 op cmd_next_line(buffer);
405 46f6e974 2021-05-17 op } while (buffer->current_line->line != NULL ||
406 46f6e974 2021-05-17 op buffer->current_line->parent->type != LINE_TEXT);
410 46f6e974 2021-05-17 op cmd_move_beginning_of_line(struct buffer *buffer)
412 46f6e974 2021-05-17 op buffer->cpoff = 0;
413 46f6e974 2021-05-17 op restore_cursor(buffer);
417 46f6e974 2021-05-17 op cmd_move_end_of_line(struct buffer *buffer)
419 9a25f829 2021-03-14 op struct vline *vl;
421 46f6e974 2021-05-17 op vl = buffer->current_line;
422 452589f7 2021-03-16 op if (vl->line == NULL)
424 46f6e974 2021-05-17 op buffer->cpoff = utf8_cplen(vl->line);
425 46f6e974 2021-05-17 op restore_cursor(buffer);
429 46f6e974 2021-05-17 op cmd_redraw(struct buffer *buffer)
431 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
435 46f6e974 2021-05-17 op cmd_scroll_line_up(struct buffer *buffer)
437 9a25f829 2021-03-14 op struct vline *vl;
439 46f6e974 2021-05-17 op if (buffer->line_off == 0)
442 46f6e974 2021-05-17 op vl = nth_line(buffer, --buffer->line_off);
443 48e9d457 2021-03-06 op wscrl(body, -1);
444 48e9d457 2021-03-06 op wmove(body, 0, 0);
445 bddc7bbd 2021-04-01 op print_vline(body, vl);
447 46f6e974 2021-05-17 op buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
448 46f6e974 2021-05-17 op restore_cursor(buffer);
452 46f6e974 2021-05-17 op cmd_scroll_line_down(struct buffer *buffer)
454 9a25f829 2021-03-14 op struct vline *vl;
456 46f6e974 2021-05-17 op vl = buffer->current_line;
457 452589f7 2021-03-16 op if ((vl = TAILQ_NEXT(vl, vlines)) == NULL)
459 46f6e974 2021-05-17 op buffer->current_line = vl;
461 46f6e974 2021-05-17 op buffer->line_off++;
462 48e9d457 2021-03-06 op wscrl(body, 1);
464 46f6e974 2021-05-17 op if (buffer->line_max - buffer->line_off < (size_t)body_lines)
467 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off + body_lines-1);
468 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
469 bddc7bbd 2021-04-01 op print_vline(body, vl);
471 46f6e974 2021-05-17 op restore_cursor(buffer);
475 46f6e974 2021-05-17 op cmd_scroll_up(struct buffer *buffer)
479 cce62e74 2021-04-25 op off = body_lines-1;
481 ed44414d 2021-03-09 op for (; off > 0; --off)
482 46f6e974 2021-05-17 op cmd_scroll_line_up(buffer);
486 46f6e974 2021-05-17 op cmd_scroll_down(struct buffer *buffer)
490 cce62e74 2021-04-25 op off = body_lines-1;
492 338eecdc 2021-03-12 op for (; off > 0; --off)
493 46f6e974 2021-05-17 op cmd_scroll_line_down(buffer);
497 46f6e974 2021-05-17 op cmd_beginning_of_buffer(struct buffer *buffer)
499 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
500 46f6e974 2021-05-17 op buffer->line_off = 0;
501 46f6e974 2021-05-17 op buffer->curs_y = 0;
502 46f6e974 2021-05-17 op buffer->cpoff = 0;
503 46f6e974 2021-05-17 op restore_cursor(buffer);
507 46f6e974 2021-05-17 op cmd_end_of_buffer(struct buffer *buffer)
511 46f6e974 2021-05-17 op off = buffer->line_max - body_lines;
512 e19f9a04 2021-03-11 op off = MAX(0, off);
514 46f6e974 2021-05-17 op buffer->line_off = off;
515 46f6e974 2021-05-17 op buffer->curs_y = MIN((size_t)body_lines, buffer->line_max-1);
517 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
518 46f6e974 2021-05-17 op buffer->cpoff = body_cols;
519 46f6e974 2021-05-17 op restore_cursor(buffer);
523 46f6e974 2021-05-17 op cmd_kill_telescope(struct buffer *buffer)
525 c7107cec 2021-04-01 op save_session();
526 1d08c280 2021-03-06 op event_loopbreak();
530 46f6e974 2021-05-17 op cmd_push_button(struct buffer *buffer)
532 9a25f829 2021-03-14 op struct vline *vl;
535 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
536 46f6e974 2021-05-17 op if (nth >= buffer->line_max)
538 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
539 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
542 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), vl->parent->alt);
546 46f6e974 2021-05-17 op cmd_push_button_new_tab(struct buffer *buffer)
548 9a25f829 2021-03-14 op struct vline *vl;
551 46f6e974 2021-05-17 op nth = buffer->line_off + buffer->curs_y;
552 46f6e974 2021-05-17 op if (nth > buffer->line_max)
554 46f6e974 2021-05-17 op vl = nth_line(buffer, nth);
555 9a25f829 2021-03-14 op if (vl->parent->type != LINE_LINK)
558 1b81bc33 2021-03-15 op new_tab(vl->parent->alt);
562 46f6e974 2021-05-17 op cmd_previous_button(struct buffer *buffer)
565 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
566 46f6e974 2021-05-17 op buffer->current_line == TAILQ_FIRST(&buffer->head)) {
567 a8c28919 2021-03-16 op message("No previous link");
570 46f6e974 2021-05-17 op cmd_previous_line(buffer);
571 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
575 46f6e974 2021-05-17 op cmd_next_button(struct buffer *buffer)
578 46f6e974 2021-05-17 op if (buffer->current_line == NULL ||
579 46f6e974 2021-05-17 op buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
580 a8c28919 2021-03-16 op message("No next link");
583 46f6e974 2021-05-17 op cmd_next_line(buffer);
584 46f6e974 2021-05-17 op } while (buffer->current_line->parent->type != LINE_LINK);
588 46f6e974 2021-05-17 op cmd_previous_page(struct buffer *buffer)
590 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
592 2051e653 2021-03-13 op if (!load_previous_page(tab))
593 2051e653 2021-03-13 op message("No previous page");
595 c40c2250 2021-03-14 op start_loading_anim(tab);
599 46f6e974 2021-05-17 op cmd_next_page(struct buffer *buffer)
601 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
603 2051e653 2021-03-13 op if (!load_next_page(tab))
604 2051e653 2021-03-13 op message("No next page");
606 c40c2250 2021-03-14 op start_loading_anim(tab);
610 46f6e974 2021-05-17 op cmd_clear_minibuf(struct buffer *buffer)
612 3b4f9e49 2021-03-11 op handle_clear_minibuf(0, 0, NULL);
616 46f6e974 2021-05-17 op cmd_execute_extended_command(struct buffer *buffer)
620 2ba66cea 2021-03-22 op if (in_minibuffer) {
621 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
625 22268e11 2021-03-11 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
626 22268e11 2021-03-11 op &eecmd_history);
628 9ca15951 2021-03-09 op len = sizeof(ministate.prompt);
629 9ca15951 2021-03-09 op strlcpy(ministate.prompt, "", len);
631 9ca15951 2021-03-09 op if (thiskey.meta)
632 9ca15951 2021-03-09 op strlcat(ministate.prompt, "M-", len);
634 9ca15951 2021-03-09 op strlcat(ministate.prompt, keyname(thiskey.key), len);
636 c3fcefe6 2021-05-12 op if (thiskey.meta)
637 c3fcefe6 2021-05-12 op strlcat(ministate.prompt, " ", len);
641 46f6e974 2021-05-17 op cmd_tab_close(struct buffer *buffer)
643 2ba66cea 2021-03-22 op struct tab *tab, *t;
645 2ba66cea 2021-03-22 op tab = current_tab();
646 7c7d7bb7 2021-03-10 op if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
647 7c7d7bb7 2021-03-10 op TAILQ_NEXT(tab, tabs) == NULL) {
648 7c7d7bb7 2021-03-10 op message("Can't close the only tab.");
652 2ba66cea 2021-03-22 op if (evtimer_pending(&tab->loadingev, NULL))
653 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
655 7c7d7bb7 2021-03-10 op stop_tab(tab);
657 9d24c64f 2021-03-17 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
658 9d24c64f 2021-03-17 op t = TAILQ_NEXT(tab, tabs);
659 7c7d7bb7 2021-03-10 op TAILQ_REMOVE(&tabshead, tab, tabs);
662 9d24c64f 2021-03-17 op switch_to_tab(t);
666 46f6e974 2021-05-17 op cmd_tab_close_other(struct buffer *buffer)
668 2ff75826 2021-03-26 op struct tab *t, *i;
670 fea845b6 2021-03-15 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
671 fea845b6 2021-03-15 op if (t->flags & TAB_CURRENT)
675 fea845b6 2021-03-15 op TAILQ_REMOVE(&tabshead, t, tabs);
681 46f6e974 2021-05-17 op cmd_tab_new(struct buffer *buffer)
683 1b81bc33 2021-03-15 op new_tab(NEW_TAB_URL);
687 46f6e974 2021-05-17 op cmd_tab_next(struct buffer *buffer)
689 2ba66cea 2021-03-22 op struct tab *tab, *t;
691 2ba66cea 2021-03-22 op tab = current_tab();
692 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
694 7c7d7bb7 2021-03-10 op if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
695 7c7d7bb7 2021-03-10 op t = TAILQ_FIRST(&tabshead);
696 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
697 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
701 46f6e974 2021-05-17 op cmd_tab_previous(struct buffer *buffer)
703 2ba66cea 2021-03-22 op struct tab *tab, *t;
705 2ba66cea 2021-03-22 op tab = current_tab();
706 7c7d7bb7 2021-03-10 op tab->flags &= ~TAB_CURRENT;
708 7c7d7bb7 2021-03-10 op if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
709 7c7d7bb7 2021-03-10 op t = TAILQ_LAST(&tabshead, tabshead);
710 7c7d7bb7 2021-03-10 op t->flags |= TAB_CURRENT;
711 e8a76665 2021-05-12 op t->flags &= ~TAB_URGENT;
715 46f6e974 2021-05-17 op cmd_tab_move(struct buffer *buffer)
717 fad72201 2021-03-28 op struct tab *tab, *t;
719 fad72201 2021-03-28 op tab = current_tab();
720 fad72201 2021-03-28 op t = TAILQ_NEXT(tab, tabs);
721 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
723 fad72201 2021-03-28 op if (t == NULL)
724 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
726 fad72201 2021-03-28 op TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
730 46f6e974 2021-05-17 op cmd_tab_move_to(struct buffer *buffer)
732 fad72201 2021-03-28 op struct tab *tab, *t;
734 fad72201 2021-03-28 op tab = current_tab();
735 fad72201 2021-03-28 op t = TAILQ_PREV(tab, tabshead, tabs);
736 fad72201 2021-03-28 op TAILQ_REMOVE(&tabshead, tab, tabs);
738 fad72201 2021-03-28 op if (t == NULL) {
739 fad72201 2021-03-28 op if (TAILQ_EMPTY(&tabshead))
740 fad72201 2021-03-28 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
742 fad72201 2021-03-28 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
744 fad72201 2021-03-28 op TAILQ_INSERT_BEFORE(t, tab, tabs);
748 46f6e974 2021-05-17 op cmd_load_url(struct buffer *buffer)
750 2ba66cea 2021-03-22 op if (in_minibuffer) {
751 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
755 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
756 22268e11 2021-03-11 op &lu_history);
757 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
758 2d13108f 2021-04-30 op strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
759 46f6e974 2021-05-17 op cmd_move_end_of_line(&ministate.buffer);
763 46f6e974 2021-05-17 op cmd_load_current_url(struct buffer *buffer)
765 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
767 2ba66cea 2021-03-22 op if (in_minibuffer) {
768 2ba66cea 2021-03-22 op message("We don't have enable-recursive-minibuffers");
772 22268e11 2021-03-11 op enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
773 22268e11 2021-03-11 op &lu_history);
774 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
775 740f578b 2021-03-15 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
776 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
780 46f6e974 2021-05-17 op cmd_bookmark_page(struct buffer *buffer)
782 2ba66cea 2021-03-22 op struct tab *tab = current_tab();
784 740f578b 2021-03-15 op enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
785 740f578b 2021-03-15 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
786 2051e653 2021-03-13 op strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
787 46f6e974 2021-05-17 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
791 46f6e974 2021-05-17 op cmd_list_bookmarks(struct buffer *buffer)
793 2ba66cea 2021-03-22 op load_url_in_tab(current_tab(), "about:bookmarks");
797 46f6e974 2021-05-17 op cmd_toggle_help(struct buffer *buffer)
799 bddc7bbd 2021-04-01 op side_window = !side_window;
800 bddc7bbd 2021-04-01 op if (side_window)
801 bddc7bbd 2021-04-01 op recompute_help();
804 7cdf12cb 2021-05-12 op * ugly hack, but otherwise the window doesn't get updated
805 bddc7bbd 2021-04-01 op * until I call handle_resize a second time (i.e. C-l). I
806 7cdf12cb 2021-05-12 op * will be happy to know why something like this is needed.
808 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
809 a8a482c8 2021-05-17 op handle_resize_nodelay(0, 0, NULL);
813 46f6e974 2021-05-17 op cmd_mini_delete_char(struct buffer *buffer)
817 2ba66cea 2021-03-22 op if (!in_minibuffer) {
818 2ba66cea 2021-03-22 op message("text is read-only");
822 3eecee9e 2021-03-12 op minibuffer_taint_hist();
824 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
825 2ba66cea 2021-03-22 op if (*c == '\0')
827 2ba66cea 2021-03-22 op n = utf8_next_cp(c);
829 2ba66cea 2021-03-22 op memmove(c, n, strlen(n)+1);
833 46f6e974 2021-05-17 op cmd_mini_delete_backward_char(struct buffer *buffer)
835 2ba66cea 2021-03-22 op char *c, *p, *start;
837 2ba66cea 2021-03-22 op if (!in_minibuffer) {
838 2ba66cea 2021-03-22 op message("text is read-only");
842 3eecee9e 2021-03-12 op minibuffer_taint_hist();
844 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
845 46f6e974 2021-05-17 op start = buffer->current_line->line;
846 2ba66cea 2021-03-22 op if (c == start)
848 2ba66cea 2021-03-22 op p = utf8_prev_cp(c-1, start);
850 2ba66cea 2021-03-22 op memmove(p, c, strlen(c)+1);
851 46f6e974 2021-05-17 op buffer->cpoff--;
855 46f6e974 2021-05-17 op cmd_mini_kill_line(struct buffer *buffer)
859 2ba66cea 2021-03-22 op if (!in_minibuffer) {
860 2ba66cea 2021-03-22 op message("text is read-only");
864 3eecee9e 2021-03-12 op minibuffer_taint_hist();
865 46f6e974 2021-05-17 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
870 46f6e974 2021-05-17 op cmd_mini_abort(struct buffer *buffer)
872 2ba66cea 2021-03-22 op if (!in_minibuffer)
875 17e5106b 2021-03-21 op ministate.abortfn();
879 46f6e974 2021-05-17 op cmd_mini_complete_and_exit(struct buffer *buffer)
881 2ba66cea 2021-03-22 op if (!in_minibuffer)
884 22268e11 2021-03-11 op minibuffer_taint_hist();
885 b360ebb3 2021-03-10 op ministate.donefn();
889 46f6e974 2021-05-17 op cmd_mini_previous_history_element(struct buffer *buffer)
891 22268e11 2021-03-11 op if (ministate.history == NULL) {
892 22268e11 2021-03-11 op message("No history");
896 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
897 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
898 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
899 22268e11 2021-03-11 op ministate.hist_off = ministate.history->len - 1;
900 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
901 22268e11 2021-03-11 op message("No prev item");
903 22268e11 2021-03-11 op ministate.hist_off--;
906 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
907 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
911 46f6e974 2021-05-17 op cmd_mini_next_history_element(struct buffer *buffer)
913 22268e11 2021-03-11 op if (ministate.history == NULL) {
914 22268e11 2021-03-11 op message("No history");
918 22268e11 2021-03-11 op if (ministate.hist_cur == NULL ||
919 22268e11 2021-03-11 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
920 22268e11 2021-03-11 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
921 22268e11 2021-03-11 op ministate.hist_off = 0;
922 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
923 22268e11 2021-03-11 op message("No next item");
925 22268e11 2021-03-11 op ministate.hist_off++;
928 17e5106b 2021-03-21 op if (ministate.hist_cur != NULL)
929 46f6e974 2021-05-17 op buffer->current_line->line = ministate.hist_cur->h;
933 870210fb 2021-03-26 op global_key_unbound(void)
935 870210fb 2021-03-26 op message("%s is undefined", keybuf);
939 22268e11 2021-03-11 op minibuffer_hist_save_entry(void)
941 3148eeac 2021-03-13 op struct hist *hist;
943 22268e11 2021-03-11 op if (ministate.history == NULL)
946 22268e11 2021-03-11 op if ((hist = calloc(1, sizeof(*hist))) == NULL)
949 22268e11 2021-03-11 op strlcpy(hist->h, ministate.buf, sizeof(hist->h));
951 22268e11 2021-03-11 op if (TAILQ_EMPTY(&ministate.history->head))
952 22268e11 2021-03-11 op TAILQ_INSERT_HEAD(&ministate.history->head, hist, entries);
954 22268e11 2021-03-11 op TAILQ_INSERT_TAIL(&ministate.history->head, hist, entries);
955 22268e11 2021-03-11 op ministate.history->len++;
959 22268e11 2021-03-11 op * taint the minibuffer cache: if we're currently showing a history
960 22268e11 2021-03-11 op * element, copy that to the current buf and reset the "history
961 22268e11 2021-03-11 op * navigation" thing.
964 22268e11 2021-03-11 op minibuffer_taint_hist(void)
966 22268e11 2021-03-11 op if (ministate.hist_cur == NULL)
969 22268e11 2021-03-11 op strlcpy(ministate.buf, ministate.hist_cur->h, sizeof(ministate.buf));
970 22268e11 2021-03-11 op ministate.hist_cur = NULL;
974 5cd2ebb1 2021-03-11 op minibuffer_self_insert(void)
976 2ba66cea 2021-03-22 op char *c, tmp[5] = {0};
979 22268e11 2021-03-11 op minibuffer_taint_hist();
981 17e5106b 2021-03-21 op if (thiskey.cp == 0)
984 17e5106b 2021-03-21 op len = utf8_encode(thiskey.cp, tmp);
985 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line, ministate.buffer.cpoff);
986 2ba66cea 2021-03-22 op if (c + len > ministate.buf + sizeof(ministate.buf) - 1)
989 2ba66cea 2021-03-22 op memmove(c + len, c, strlen(c)+1);
990 2ba66cea 2021-03-22 op memcpy(c, tmp, len);
991 46f6e974 2021-05-17 op ministate.buffer.cpoff++;
995 5cd2ebb1 2021-03-11 op eecmd_self_insert(void)
997 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.cp) ||
998 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.cp)) {
999 5cd2ebb1 2021-03-11 op global_key_unbound();
1003 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1007 b360ebb3 2021-03-10 op eecmd_select(void)
1009 870210fb 2021-03-26 op struct cmds *cmd;
1011 870210fb 2021-03-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1012 870210fb 2021-03-26 op if (!strcmp(cmd->cmd, ministate.buf)) {
1013 2fe98292 2021-03-26 op exit_minibuffer();
1014 2fe98292 2021-03-26 op minibuffer_hist_save_entry();
1015 46f6e974 2021-05-17 op cmd->fn(current_buffer());
1020 2fe98292 2021-03-26 op message("No match");
1024 5cd2ebb1 2021-03-11 op ir_self_insert(void)
1026 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1030 5cd2ebb1 2021-03-11 op ir_select(void)
1032 5cd2ebb1 2021-03-11 op char buf[1025] = {0};
1033 31f1a758 2021-04-22 op struct phos_uri uri;
1034 5cd2ebb1 2021-03-11 op struct tab *tab;
1036 5cd2ebb1 2021-03-11 op tab = current_tab();
1038 5cd2ebb1 2021-03-11 op exit_minibuffer();
1039 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1041 5cd2ebb1 2021-03-11 op /* a bit ugly but... */
1042 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
1043 f7a80b3c 2021-04-24 op phos_uri_set_query(&uri, ministate.buf);
1044 31f1a758 2021-04-22 op phos_serialize_uri(&uri, buf, sizeof(buf));
1045 5cd2ebb1 2021-03-11 op load_url_in_tab(tab, buf);
1049 5cd2ebb1 2021-03-11 op lu_self_insert(void)
1051 17e5106b 2021-03-21 op if (thiskey.meta || unicode_isspace(thiskey.key) ||
1052 17e5106b 2021-03-21 op !unicode_isgraph(thiskey.key)) {
1053 5cd2ebb1 2021-03-11 op global_key_unbound();
1057 5cd2ebb1 2021-03-11 op minibuffer_self_insert();
1061 5cd2ebb1 2021-03-11 op lu_select(void)
1063 5cd2ebb1 2021-03-11 op exit_minibuffer();
1064 22268e11 2021-03-11 op minibuffer_hist_save_entry();
1065 5cd2ebb1 2021-03-11 op load_url_in_tab(current_tab(), ministate.buf);
1069 740f578b 2021-03-15 op bp_select(void)
1071 740f578b 2021-03-15 op exit_minibuffer();
1072 740f578b 2021-03-15 op if (*ministate.buf != '\0')
1073 740f578b 2021-03-15 op add_to_bookmarks(ministate.buf);
1075 740f578b 2021-03-15 op message("Abort.");
1079 5d1bac73 2021-03-25 op yornp_self_insert(void)
1081 5d1bac73 2021-03-25 op if (thiskey.key != 'y' && thiskey.key != 'n') {
1082 5d1bac73 2021-03-25 op message("Please answer y or n");
1086 5d1bac73 2021-03-25 op exit_minibuffer();
1087 fef9de8f 2021-04-25 op yornp_cb(thiskey.key == 'y', yornp_data);
1091 5d1bac73 2021-03-25 op yornp_abort(void)
1093 5d1bac73 2021-03-25 op exit_minibuffer();
1094 fef9de8f 2021-04-25 op yornp_cb(0, yornp_data);
1098 de2a69bb 2021-05-17 op read_self_insert(void)
1100 de2a69bb 2021-05-17 op if (thiskey.meta || !unicode_isgraph(thiskey.cp)) {
1101 de2a69bb 2021-05-17 op global_key_unbound();
1105 de2a69bb 2021-05-17 op minibuffer_self_insert();
1109 de2a69bb 2021-05-17 op read_abort(void)
1111 de2a69bb 2021-05-17 op exit_minibuffer();
1112 de2a69bb 2021-05-17 op read_cb(NULL, read_data);
1116 de2a69bb 2021-05-17 op read_select(void)
1118 de2a69bb 2021-05-17 op exit_minibuffer();
1119 de2a69bb 2021-05-17 op minibuffer_hist_save_entry();
1120 de2a69bb 2021-05-17 op read_cb(ministate.buf, read_data);
1123 9a25f829 2021-03-14 op static struct vline *
1124 46f6e974 2021-05-17 op nth_line(struct buffer *buffer, size_t n)
1126 9a25f829 2021-03-14 op struct vline *vl;
1130 46f6e974 2021-05-17 op TAILQ_FOREACH(vl, &buffer->head, vlines) {
1136 48e9d457 2021-03-06 op /* unreachable */
1140 5e11c00c 2021-03-02 op static struct tab *
1141 5e11c00c 2021-03-02 op current_tab(void)
1143 5e11c00c 2021-03-02 op struct tab *t;
1145 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
1146 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
1150 5e11c00c 2021-03-02 op /* unreachable */
1154 46f6e974 2021-05-17 op static struct buffer *
1155 46f6e974 2021-05-17 op current_buffer(void)
1157 2ba66cea 2021-03-22 op if (in_minibuffer)
1158 46f6e974 2021-05-17 op return &ministate.buffer;
1159 46f6e974 2021-05-17 op return ¤t_tab()->buffer;
1163 8947c1f2 2021-03-21 op readkey(void)
1165 8947c1f2 2021-03-21 op uint32_t state = 0;
1167 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR)
1170 8947c1f2 2021-03-21 op thiskey.meta = thiskey.key == 27;
1171 8947c1f2 2021-03-21 op if (thiskey.meta) {
1172 9ca15951 2021-03-09 op thiskey.key = wgetch(body);
1173 c314a314 2021-03-11 op if (thiskey.key == ERR || thiskey.key == 27) {
1174 c314a314 2021-03-11 op thiskey.meta = 0;
1175 9ca15951 2021-03-09 op thiskey.key = 27;
1179 8947c1f2 2021-03-21 op thiskey.cp = 0;
1180 8947c1f2 2021-03-21 op if ((unsigned int)thiskey.key < UINT8_MAX) {
1182 8947c1f2 2021-03-21 op if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
1184 8947c1f2 2021-03-21 op if ((thiskey.key = wgetch(body)) == ERR) {
1185 8947c1f2 2021-03-21 op message("Error decoding user input");
1195 8947c1f2 2021-03-21 op dispatch_stdio(int fd, short ev, void *d)
1197 8947c1f2 2021-03-21 op struct keymap *k;
1198 8947c1f2 2021-03-21 op const char *keyname;
1199 8947c1f2 2021-03-21 op char tmp[5] = {0};
1201 8947c1f2 2021-03-21 op if (!readkey())
1204 7c7d7bb7 2021-03-10 op if (keybuf[0] != '\0')
1205 7c7d7bb7 2021-03-10 op strlcat(keybuf, " ", sizeof(keybuf));
1206 7c7d7bb7 2021-03-10 op if (thiskey.meta)
1207 7c7d7bb7 2021-03-10 op strlcat(keybuf, "M-", sizeof(keybuf));
1208 8947c1f2 2021-03-21 op if (thiskey.cp != 0) {
1209 8947c1f2 2021-03-21 op utf8_encode(thiskey.cp, tmp);
1210 7c7d7bb7 2021-03-10 op strlcat(keybuf, tmp, sizeof(keybuf));
1212 8947c1f2 2021-03-21 op if ((keyname = unkbd(thiskey.key)) != NULL)
1213 8947c1f2 2021-03-21 op strlcat(keybuf, keyname, sizeof(keybuf));
1215 8947c1f2 2021-03-21 op tmp[0] = thiskey.key;
1216 8947c1f2 2021-03-21 op strlcat(keybuf, tmp, sizeof(keybuf));
1220 9ca15951 2021-03-09 op TAILQ_FOREACH(k, ¤t_map->m, keymaps) {
1221 9ca15951 2021-03-09 op if (k->meta == thiskey.meta &&
1222 9ca15951 2021-03-09 op k->key == thiskey.key) {
1223 f832146f 2021-03-09 op if (k->fn == NULL)
1224 f832146f 2021-03-09 op current_map = &k->map;
1226 9ca15951 2021-03-09 op current_map = base_map;
1227 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1228 46f6e974 2021-05-17 op k->fn(current_buffer());
1234 7c7d7bb7 2021-03-10 op if (current_map->unhandled_input != NULL)
1235 7c7d7bb7 2021-03-10 op current_map->unhandled_input();
1237 7c7d7bb7 2021-03-10 op global_key_unbound();
1240 7c7d7bb7 2021-03-10 op strlcpy(keybuf, "", sizeof(keybuf));
1241 9ca15951 2021-03-09 op current_map = base_map;
1244 bddc7bbd 2021-04-01 op if (side_window)
1245 bddc7bbd 2021-04-01 op recompute_help();
1247 179f0f58 2021-03-11 op redraw_tab(current_tab());
1251 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
1253 9cb0f9ce 2021-03-10 op free(ministate.curmesg);
1254 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1256 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1257 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1258 9cb0f9ce 2021-03-10 op wrefresh(body);
1259 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1261 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1262 9cb0f9ce 2021-03-10 op wrefresh(body);
1267 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
1269 831deb20 2021-05-12 op if (event_pending(&resizeev, EV_TIMEOUT, NULL)) {
1270 831deb20 2021-05-12 op event_del(&resizeev);
1272 a8a482c8 2021-05-17 op evtimer_set(&resizeev, handle_resize_nodelay, NULL);
1273 831deb20 2021-05-12 op evtimer_add(&resizeev, &resize_timer);
1277 a8a482c8 2021-05-17 op handle_resize_nodelay(int s, short ev, void *d)
1279 1d08c280 2021-03-06 op struct tab *tab;
1285 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
1287 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
1288 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
1290 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
1291 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
1293 48e9d457 2021-03-06 op body_lines = LINES-3;
1294 bd9637e9 2021-03-06 op body_cols = COLS;
1296 bddc7bbd 2021-04-01 op if (side_window) {
1297 bddc7bbd 2021-04-01 op help_cols = 0.3 * COLS;
1298 bddc7bbd 2021-04-01 op help_lines = LINES-3;
1299 bddc7bbd 2021-04-01 op mvwin(help, 1, 0);
1300 bddc7bbd 2021-04-01 op wresize(help, help_lines, help_cols);
1302 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1304 bddc7bbd 2021-04-01 op body_cols = COLS - help_cols - 1;
1305 bddc7bbd 2021-04-01 op mvwin(body, 1, help_cols);
1307 bddc7bbd 2021-04-01 op mvwin(body, 1, 0);
1309 bddc7bbd 2021-04-01 op wresize(body, body_lines, body_cols);
1311 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
1313 1d08c280 2021-03-06 op tab = current_tab();
1315 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
1316 1d08c280 2021-03-06 op redraw_tab(tab);
1320 46f6e974 2021-05-17 op wrap_page(struct buffer *buffer, int width)
1322 452589f7 2021-03-16 op struct line *l;
1323 452589f7 2021-03-16 op const struct line *orig;
1324 d511dc85 2021-03-16 op struct vline *vl;
1325 452589f7 2021-03-16 op const char *prfx;
1327 46f6e974 2021-05-17 op orig = buffer->current_line == NULL
1329 46f6e974 2021-05-17 op : buffer->current_line->parent;
1330 46f6e974 2021-05-17 op buffer->current_line = NULL;
1332 46f6e974 2021-05-17 op buffer->curs_y = 0;
1333 46f6e974 2021-05-17 op buffer->line_off = 0;
1335 46f6e974 2021-05-17 op empty_vlist(buffer);
1337 46f6e974 2021-05-17 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
1338 0d568960 2021-03-11 op prfx = line_prefixes[l->type].prfx1;
1339 5e11c00c 2021-03-02 op switch (l->type) {
1340 5e11c00c 2021-03-02 op case LINE_TEXT:
1341 5e11c00c 2021-03-02 op case LINE_LINK:
1342 5e11c00c 2021-03-02 op case LINE_TITLE_1:
1343 5e11c00c 2021-03-02 op case LINE_TITLE_2:
1344 5e11c00c 2021-03-02 op case LINE_TITLE_3:
1345 5e11c00c 2021-03-02 op case LINE_ITEM:
1346 5e11c00c 2021-03-02 op case LINE_QUOTE:
1347 5e11c00c 2021-03-02 op case LINE_PRE_START:
1348 5e11c00c 2021-03-02 op case LINE_PRE_END:
1349 46f6e974 2021-05-17 op wrap_text(buffer, prfx, l, width);
1351 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
1352 46f6e974 2021-05-17 op hardwrap_text(buffer, l, width);
1356 46f6e974 2021-05-17 op if (orig == l && buffer->current_line == NULL) {
1357 46f6e974 2021-05-17 op buffer->line_off = buffer->line_max-1;
1358 46f6e974 2021-05-17 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
1361 46f6e974 2021-05-17 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
1362 d511dc85 2021-03-16 op if (vl == NULL || vl->parent != orig)
1364 46f6e974 2021-05-17 op buffer->current_line = vl;
1365 46f6e974 2021-05-17 op buffer->line_off--;
1370 46f6e974 2021-05-17 op if (buffer->current_line == NULL)
1371 46f6e974 2021-05-17 op buffer->current_line = TAILQ_FIRST(&buffer->head);
1377 bddc7bbd 2021-04-01 op print_vline(WINDOW *window, struct vline *vl)
1379 9a25f829 2021-03-14 op const char *text = vl->line;
1380 768db5bb 2021-03-12 op const char *prfx;
1381 803ff456 2021-03-17 op int prefix_face = line_faces[vl->parent->type].prefix_prop;
1382 803ff456 2021-03-17 op int text_face = line_faces[vl->parent->type].text_prop;
1384 9a25f829 2021-03-14 op if (!vl->flags)
1385 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx1;
1387 9a25f829 2021-03-14 op prfx = line_prefixes[vl->parent->type].prfx2;
1389 bd9637e9 2021-03-06 op if (text == NULL)
1392 bddc7bbd 2021-04-01 op wattron(window, prefix_face);
1393 bddc7bbd 2021-04-01 op wprintw(window, "%s", prfx);
1394 bddc7bbd 2021-04-01 op wattroff(window, prefix_face);
1396 bddc7bbd 2021-04-01 op wattron(window, text_face);
1397 bddc7bbd 2021-04-01 op wprintw(window, "%s", text);
1398 bddc7bbd 2021-04-01 op wattroff(window, text_face);
1402 8af5e5ed 2021-03-08 op redraw_tabline(void)
1404 7c7d7bb7 2021-03-10 op struct tab *tab;
1405 8f127baa 2021-04-22 op size_t toskip, ots, tabwidth, space, x;
1406 8f127baa 2021-04-22 op int current, y, truncated;
1407 dc5df781 2021-03-13 op const char *title;
1408 119f393c 2021-03-16 op char buf[25];
1410 8f127baa 2021-04-22 op tabwidth = sizeof(buf)+1;
1411 8f127baa 2021-04-22 op space = COLS-2;
1414 a636f50e 2021-03-16 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1416 a636f50e 2021-03-16 op if (tab->flags & TAB_CURRENT)
1420 8f127baa 2021-04-22 op if (toskip * tabwidth < space)
1423 8f127baa 2021-04-22 op ots = toskip;
1425 8f127baa 2021-04-22 op while (toskip != 0 &&
1426 8f127baa 2021-04-22 op (ots - toskip+1) * tabwidth < space)
1430 a636f50e 2021-03-16 op werase(tabline);
1431 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1432 a636f50e 2021-03-16 op wprintw(tabline, toskip == 0 ? " " : "<");
1433 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.background);
1435 a636f50e 2021-03-16 op truncated = 0;
1436 7c7d7bb7 2021-03-10 op TAILQ_FOREACH(tab, &tabshead, tabs) {
1437 a636f50e 2021-03-16 op if (truncated)
1439 a636f50e 2021-03-16 op if (toskip != 0) {
1444 a636f50e 2021-03-16 op getyx(tabline, y, x);
1445 a636f50e 2021-03-16 op if (x + sizeof(buf)+2 >= (size_t)COLS)
1446 a636f50e 2021-03-16 op truncated = 1;
1448 7c7d7bb7 2021-03-10 op current = tab->flags & TAB_CURRENT;
1450 46f6e974 2021-05-17 op if (*(title = tab->buffer.page.title) == '\0')
1451 2051e653 2021-03-13 op title = tab->hist_cur->h;
1453 e8a76665 2021-05-12 op if (tab->flags & TAB_URGENT)
1454 e8a76665 2021-05-12 op strlcpy(buf, "!", sizeof(buf));
1456 e8a76665 2021-05-12 op strlcpy(buf, " ", sizeof(buf));
1458 119f393c 2021-03-16 op if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
1459 119f393c 2021-03-16 op /* truncation happens */
1460 119f393c 2021-03-16 op strlcpy(&buf[sizeof(buf)-4], "...", 4);
1462 119f393c 2021-03-16 op /* pad with spaces */
1463 119f393c 2021-03-16 op while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
1467 a329982b 2021-03-11 op if (current)
1468 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.current_tab);
1470 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.tab);
1472 119f393c 2021-03-16 op wprintw(tabline, "%s", buf);
1473 119f393c 2021-03-16 op if (TAILQ_NEXT(tab, tabs) != NULL)
1474 119f393c 2021-03-16 op wprintw(tabline, " ");
1476 cb6c7aa0 2021-03-16 op if (current)
1477 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.current_tab);
1479 cb6c7aa0 2021-03-16 op wattroff(tabline, tab_face.tab);
1482 cb6c7aa0 2021-03-16 op wattron(tabline, tab_face.background);
1483 8f127baa 2021-04-22 op for (; x < (size_t)COLS; ++x)
1484 119f393c 2021-03-16 op waddch(tabline, ' ');
1485 a636f50e 2021-03-16 op if (truncated)
1486 a636f50e 2021-03-16 op mvwprintw(tabline, 0, COLS-1, ">");
1490 46f6e974 2021-05-17 op redraw_window(WINDOW *win, int height, struct buffer *buffer)
1492 bddc7bbd 2021-04-01 op struct vline *vl;
1495 bddc7bbd 2021-04-01 op werase(win);
1497 46f6e974 2021-05-17 op buffer->line_off = MIN(buffer->line_max-1, buffer->line_off);
1498 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
1502 46f6e974 2021-05-17 op vl = nth_line(buffer, buffer->line_off);
1503 bddc7bbd 2021-04-01 op for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
1504 bddc7bbd 2021-04-01 op wmove(win, l, 0);
1505 bddc7bbd 2021-04-01 op print_vline(win, vl);
1507 bddc7bbd 2021-04-01 op if (l == height)
1511 46f6e974 2021-05-17 op wmove(win, buffer->curs_y, buffer->curs_x);
1515 bddc7bbd 2021-04-01 op redraw_help(void)
1517 bddc7bbd 2021-04-01 op redraw_window(help, help_lines, &helpwin);
1521 bddc7bbd 2021-04-01 op redraw_body(struct tab *tab)
1523 46f6e974 2021-05-17 op redraw_window(body, body_lines, &tab->buffer);
1526 10346511 2021-03-17 op static inline char
1527 10346511 2021-03-17 op trust_status_char(enum trust_state ts)
1529 10346511 2021-03-17 op switch (ts) {
1530 10346511 2021-03-17 op case TS_UNKNOWN: return 'u';
1531 10346511 2021-03-17 op case TS_UNTRUSTED: return '!';
1532 10346511 2021-03-17 op case TS_TRUSTED: return 'v';
1533 10346511 2021-03-17 op case TS_VERIFIED: return 'V';
1538 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
1541 48e9d457 2021-03-06 op int x, y, max_x, max_y;
1542 46f6e974 2021-05-17 op const char *mode = tab->buffer.page.name;
1543 8af5e5ed 2021-03-08 op const char *spin = "-\\|/";
1545 156f1501 2021-03-13 op werase(modeline);
1546 d5493194 2021-06-15 op wattron(modeline, modeline_face.background);
1547 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
1549 10346511 2021-03-17 op wprintw(modeline, "-%c%c %s ",
1550 2ba66cea 2021-03-22 op spin[tab->loading_anim_step],
1551 10346511 2021-03-17 op trust_status_char(tab->trust),
1552 58c75fab 2021-03-16 op mode == NULL ? "(none)" : mode);
1554 46f6e974 2021-05-17 op pct = (tab->buffer.line_off + tab->buffer.curs_y) * 100.0 / tab->buffer.line_max;
1556 46f6e974 2021-05-17 op if (tab->buffer.line_max <= (size_t)body_lines)
1557 481340cc 2021-03-11 op wprintw(modeline, "All ");
1558 46f6e974 2021-05-17 op else if (tab->buffer.line_off == 0)
1559 481340cc 2021-03-11 op wprintw(modeline, "Top ");
1560 46f6e974 2021-05-17 op else if (tab->buffer.line_off + body_lines >= tab->buffer.line_max)
1561 481340cc 2021-03-11 op wprintw(modeline, "Bottom ");
1563 481340cc 2021-03-11 op wprintw(modeline, "%.0f%% ", pct);
1565 481340cc 2021-03-11 op wprintw(modeline, "%d/%d %s ",
1566 46f6e974 2021-05-17 op tab->buffer.line_off + tab->buffer.curs_y,
1567 46f6e974 2021-05-17 op tab->buffer.line_max,
1568 2051e653 2021-03-13 op tab->hist_cur->h);
1570 48e9d457 2021-03-06 op getyx(modeline, y, x);
1571 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
1574 48e9d457 2021-03-06 op (void)max_y;
1576 48e9d457 2021-03-06 op for (; x < max_x; ++x)
1577 48e9d457 2021-03-06 op waddstr(modeline, "-");
1579 d5493194 2021-06-15 op wattroff(modeline, modeline_face.background);
1583 9ca15951 2021-03-09 op redraw_minibuffer(void)
1585 8300dd3c 2021-03-18 op struct tab *tab;
1586 17e5106b 2021-03-21 op size_t off_y, off_x = 0;
1587 2ba66cea 2021-03-22 op char *start, *c;
1589 d5493194 2021-06-15 op wattron(minibuf, minibuffer_face.background);
1590 156f1501 2021-03-13 op werase(minibuf);
1592 22268e11 2021-03-11 op if (in_minibuffer) {
1593 22268e11 2021-03-11 op mvwprintw(minibuf, 0, 0, "%s", ministate.prompt);
1594 22268e11 2021-03-11 op if (ministate.hist_cur != NULL)
1595 22268e11 2021-03-11 op wprintw(minibuf, "(%zu/%zu) ",
1596 22268e11 2021-03-11 op ministate.hist_off + 1,
1597 22268e11 2021-03-11 op ministate.history->len);
1599 22268e11 2021-03-11 op getyx(minibuf, off_y, off_x);
1601 17e5106b 2021-03-21 op start = ministate.hist_cur != NULL
1602 17e5106b 2021-03-21 op ? ministate.hist_cur->h
1603 17e5106b 2021-03-21 op : ministate.buf;
1604 46f6e974 2021-05-17 op c = utf8_nth(ministate.buffer.current_line->line,
1605 46f6e974 2021-05-17 op ministate.buffer.cpoff);
1606 2ba66cea 2021-03-22 op while (utf8_swidth_between(start, c) > (size_t)COLS/2) {
1607 17e5106b 2021-03-21 op start = utf8_next_cp(start);
1610 17e5106b 2021-03-21 op waddstr(minibuf, start);
1613 17e5106b 2021-03-21 op if (ministate.curmesg != NULL)
1614 17e5106b 2021-03-21 op wprintw(minibuf, in_minibuffer ? " [%s]" : "%s",
1615 17e5106b 2021-03-21 op ministate.curmesg);
1617 91a72220 2021-03-13 op if (!in_minibuffer && ministate.curmesg == NULL)
1618 17e5106b 2021-03-21 op waddstr(minibuf, keybuf);
1620 8300dd3c 2021-03-18 op /* If nothing else, show the URL at point */
1621 8300dd3c 2021-03-18 op if (!in_minibuffer && ministate.curmesg == NULL && *keybuf == '\0') {
1622 8300dd3c 2021-03-18 op tab = current_tab();
1623 46f6e974 2021-05-17 op if (tab->buffer.current_line != NULL &&
1624 46f6e974 2021-05-17 op tab->buffer.current_line->parent->type == LINE_LINK)
1625 46f6e974 2021-05-17 op waddstr(minibuf, tab->buffer.current_line->parent->alt);
1628 2aaf475e 2021-03-13 op if (in_minibuffer)
1629 2ba66cea 2021-03-22 op wmove(minibuf, 0, off_x + utf8_swidth_between(start, c));
1631 d5493194 2021-06-15 op wattroff(minibuf, minibuffer_face.background);
1635 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
1637 bddc7bbd 2021-04-01 op if (side_window) {
1638 bddc7bbd 2021-04-01 op redraw_help();
1639 bddc7bbd 2021-04-01 op wnoutrefresh(help);
1642 e19f9a04 2021-03-11 op redraw_tabline();
1643 e19f9a04 2021-03-11 op redraw_body(tab);
1644 e19f9a04 2021-03-11 op redraw_modeline(tab);
1645 e19f9a04 2021-03-11 op redraw_minibuffer();
1647 bddc7bbd 2021-04-01 op wnoutrefresh(tabline);
1648 bddc7bbd 2021-04-01 op wnoutrefresh(modeline);
1650 e19f9a04 2021-03-11 op if (in_minibuffer) {
1651 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1652 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1654 bddc7bbd 2021-04-01 op wnoutrefresh(minibuf);
1655 bddc7bbd 2021-04-01 op wnoutrefresh(body);
1662 bddc7bbd 2021-04-01 op emit_help_item(char *prfx, void *fn)
1664 bddc7bbd 2021-04-01 op struct line *l;
1665 bddc7bbd 2021-04-01 op struct cmds *cmd;
1667 bddc7bbd 2021-04-01 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
1668 bddc7bbd 2021-04-01 op if (fn == cmd->fn)
1671 bddc7bbd 2021-04-01 op assert(cmd != NULL);
1673 bddc7bbd 2021-04-01 op if ((l = calloc(1, sizeof(*l))) == NULL)
1676 bddc7bbd 2021-04-01 op l->type = LINE_TEXT;
1677 bddc7bbd 2021-04-01 op l->alt = NULL;
1679 bddc7bbd 2021-04-01 op asprintf(&l->line, "%s %s", prfx, cmd->cmd);
1681 bddc7bbd 2021-04-01 op if (TAILQ_EMPTY(&helpwin.page.head))
1682 bddc7bbd 2021-04-01 op TAILQ_INSERT_HEAD(&helpwin.page.head, l, lines);
1684 bddc7bbd 2021-04-01 op TAILQ_INSERT_TAIL(&helpwin.page.head, l, lines);
1688 bddc7bbd 2021-04-01 op rec_compute_help(struct kmap *keymap, char *prfx, size_t len)
1690 bddc7bbd 2021-04-01 op struct keymap *k;
1692 bddc7bbd 2021-04-01 op const char *kn;
1694 bddc7bbd 2021-04-01 op TAILQ_FOREACH(k, &keymap->m, keymaps) {
1695 bddc7bbd 2021-04-01 op strlcpy(p, prfx, sizeof(p));
1696 bddc7bbd 2021-04-01 op if (*p != '\0')
1697 bddc7bbd 2021-04-01 op strlcat(p, " ", sizeof(p));
1698 bddc7bbd 2021-04-01 op if (k->meta)
1699 bddc7bbd 2021-04-01 op strlcat(p, "M-", sizeof(p));
1700 bddc7bbd 2021-04-01 op if ((kn = unkbd(k->key)) != NULL)
1701 bddc7bbd 2021-04-01 op strlcat(p, kn, sizeof(p));
1703 bddc7bbd 2021-04-01 op strlcat(p, keyname(k->key), sizeof(p));
1705 bddc7bbd 2021-04-01 op if (k->fn == NULL)
1706 bddc7bbd 2021-04-01 op rec_compute_help(&k->map, p, sizeof(p));
1708 bddc7bbd 2021-04-01 op emit_help_item(p, k->fn);
1713 bddc7bbd 2021-04-01 op recompute_help(void)
1715 bddc7bbd 2021-04-01 op char p[32] = { 0 };
1717 bddc7bbd 2021-04-01 op empty_vlist(&helpwin);
1718 bddc7bbd 2021-04-01 op empty_linelist(&helpwin);
1719 bddc7bbd 2021-04-01 op rec_compute_help(current_map, p, sizeof(p));
1720 bddc7bbd 2021-04-01 op wrap_page(&helpwin, help_cols);
1724 740f578b 2021-03-15 op vmessage(const char *fmt, va_list ap)
1726 e0e26735 2021-03-16 op if (evtimer_pending(&clminibufev, NULL))
1727 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
1728 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
1729 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
1731 bf992581 2021-03-12 op free(ministate.curmesg);
1733 9cb0f9ce 2021-03-10 op /* TODO: what to do if the allocation fails here? */
1734 9cb0f9ce 2021-03-10 op if (vasprintf(&ministate.curmesg, fmt, ap) == -1)
1735 9cb0f9ce 2021-03-10 op ministate.curmesg = NULL;
1737 9cb0f9ce 2021-03-10 op redraw_minibuffer();
1738 9cb0f9ce 2021-03-10 op if (in_minibuffer) {
1739 9cb0f9ce 2021-03-10 op wrefresh(body);
1740 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1742 9cb0f9ce 2021-03-10 op wrefresh(minibuf);
1743 9cb0f9ce 2021-03-10 op wrefresh(body);
1748 740f578b 2021-03-15 op message(const char *fmt, ...)
1752 740f578b 2021-03-15 op va_start(ap, fmt);
1753 740f578b 2021-03-15 op vmessage(fmt, ap);
1758 8af5e5ed 2021-03-08 op start_loading_anim(struct tab *tab)
1760 2ba66cea 2021-03-22 op if (tab->loading_anim)
1762 2ba66cea 2021-03-22 op tab->loading_anim = 1;
1763 2ba66cea 2021-03-22 op evtimer_set(&tab->loadingev, update_loading_anim, tab);
1764 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1768 8af5e5ed 2021-03-08 op update_loading_anim(int fd, short ev, void *d)
1770 8af5e5ed 2021-03-08 op struct tab *tab = d;
1772 2ba66cea 2021-03-22 op tab->loading_anim_step = (tab->loading_anim_step+1)%4;
1774 6347dcd0 2021-03-16 op if (tab->flags & TAB_CURRENT) {
1775 6347dcd0 2021-03-16 op redraw_modeline(tab);
1776 6347dcd0 2021-03-16 op wrefresh(modeline);
1777 6347dcd0 2021-03-16 op wrefresh(body);
1778 6347dcd0 2021-03-16 op if (in_minibuffer)
1779 6347dcd0 2021-03-16 op wrefresh(minibuf);
1782 2ba66cea 2021-03-22 op evtimer_add(&tab->loadingev, &loadingev_timer);
1786 8af5e5ed 2021-03-08 op stop_loading_anim(struct tab *tab)
1788 2ba66cea 2021-03-22 op if (!tab->loading_anim)
1790 2ba66cea 2021-03-22 op evtimer_del(&tab->loadingev);
1791 2ba66cea 2021-03-22 op tab->loading_anim = 0;
1792 2ba66cea 2021-03-22 op tab->loading_anim_step = 0;
1794 3d8c2326 2021-03-18 op if (!(tab->flags & TAB_CURRENT))
1797 43a1b8d0 2021-03-09 op redraw_modeline(tab);
1799 43a1b8d0 2021-03-09 op wrefresh(modeline);
1800 43a1b8d0 2021-03-09 op wrefresh(body);
1801 9ca15951 2021-03-09 op if (in_minibuffer)
1802 9ca15951 2021-03-09 op wrefresh(minibuf);
1806 43a1b8d0 2021-03-09 op load_url_in_tab(struct tab *tab, const char *url)
1808 8af5e5ed 2021-03-08 op message("Loading %s...", url);
1809 8af5e5ed 2021-03-08 op start_loading_anim(tab);
1810 8af5e5ed 2021-03-08 op load_url(tab, url);
1812 46f6e974 2021-05-17 op tab->buffer.curs_x = 0;
1813 46f6e974 2021-05-17 op tab->buffer.curs_y = 0;
1814 43a1b8d0 2021-03-09 op redraw_tab(tab);
1818 b360ebb3 2021-03-10 op enter_minibuffer(void (*self_insert_fn)(void), void (*donefn)(void),
1819 3148eeac 2021-03-13 op void (*abortfn)(void), struct histhead *hist)
1821 9ca15951 2021-03-09 op in_minibuffer = 1;
1822 fa3fd864 2021-03-10 op base_map = &minibuffer_map;
1823 fa3fd864 2021-03-10 op current_map = &minibuffer_map;
1825 fa3fd864 2021-03-10 op base_map->unhandled_input = self_insert_fn;
1827 b360ebb3 2021-03-10 op ministate.donefn = donefn;
1828 b360ebb3 2021-03-10 op ministate.abortfn = abortfn;
1829 9ca15951 2021-03-09 op memset(ministate.buf, 0, sizeof(ministate.buf));
1830 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1831 46f6e974 2021-05-17 op ministate.buffer.current_line->line = ministate.buf;
1832 46f6e974 2021-05-17 op ministate.buffer.cpoff = 0;
1833 9ca15951 2021-03-09 op strlcpy(ministate.buf, "", sizeof(ministate.prompt));
1835 22268e11 2021-03-11 op ministate.history = hist;
1836 22268e11 2021-03-11 op ministate.hist_cur = NULL;
1837 22268e11 2021-03-11 op ministate.hist_off = 0;
1841 b360ebb3 2021-03-10 op exit_minibuffer(void)
1843 156f1501 2021-03-13 op werase(minibuf);
1845 9ca15951 2021-03-09 op in_minibuffer = 0;
1846 9ca15951 2021-03-09 op base_map = &global_map;
1847 9ca15951 2021-03-09 op current_map = &global_map;
1851 5cd2ebb1 2021-03-11 op switch_to_tab(struct tab *tab)
1853 5cd2ebb1 2021-03-11 op struct tab *t;
1855 5cd2ebb1 2021-03-11 op TAILQ_FOREACH(t, &tabshead, tabs) {
1856 5cd2ebb1 2021-03-11 op t->flags &= ~TAB_CURRENT;
1859 5cd2ebb1 2021-03-11 op tab->flags |= TAB_CURRENT;
1860 cf25a90f 2021-06-11 op tab->flags &= ~TAB_URGENT;
1863 2eef3403 2021-04-22 op unsigned int
1864 2eef3403 2021-04-22 op tab_new_id(void)
1866 2eef3403 2021-04-22 op return tab_counter++;
1869 b1df9b71 2021-03-12 op static struct tab *
1870 1b81bc33 2021-03-15 op new_tab(const char *url)
1872 754622a2 2021-03-15 op struct tab *tab;
1874 71105afa 2021-03-16 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
1875 71105afa 2021-03-16 op event_loopbreak();
1876 71105afa 2021-03-16 op return NULL;
1878 de2a69bb 2021-05-17 op tab->fd = -1;
1880 2051e653 2021-03-13 op TAILQ_INIT(&tab->hist.head);
1882 46f6e974 2021-05-17 op TAILQ_INIT(&tab->buffer.head);
1884 2eef3403 2021-04-22 op tab->id = tab_new_id();
1885 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
1887 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
1888 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
1890 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
1892 43a1b8d0 2021-03-09 op load_url_in_tab(tab, url);
1897 c7107cec 2021-04-01 op session_new_tab_cb(const char *url)
1899 c7107cec 2021-04-01 op new_tab(url);
1905 c92e529c 2021-06-15 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
1906 c92e529c 2021-06-15 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1910 6cd6a9e1 2021-03-20 op ui_init(int argc, char * const *argv)
1912 c92e529c 2021-06-15 op char path[PATH_MAX];
1913 941b3761 2021-03-18 op const char *url = NEW_TAB_URL;
1914 c92e529c 2021-06-15 op int ch, configtest = 0, fonf = 0;
1916 c92e529c 2021-06-15 op strlcpy(path, getenv("HOME"), sizeof(path));
1917 c92e529c 2021-06-15 op strlcat(path, "/.telescope/config", sizeof(path));
1919 c92e529c 2021-06-15 op while ((ch = getopt(argc, argv, "c:hn")) != -1) {
1920 941b3761 2021-03-18 op switch (ch) {
1923 c92e529c 2021-06-15 op strlcpy(path, optarg, sizeof(path));
1926 c92e529c 2021-06-15 op configtest = 1;
1936 941b3761 2021-03-18 op argc -= optind;
1937 941b3761 2021-03-18 op argv += optind;
1939 c92e529c 2021-06-15 op parseconfig(path, fonf);
1940 c92e529c 2021-06-15 op if (configtest){
1941 c92e529c 2021-06-15 op puts("config OK");
1945 941b3761 2021-03-18 op if (argc != 0)
1946 941b3761 2021-03-18 op url = argv[0];
1948 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
1950 9ca15951 2021-03-09 op TAILQ_INIT(&global_map.m);
1951 9ca15951 2021-03-09 op global_map.unhandled_input = global_key_unbound;
1953 fa3fd864 2021-03-10 op TAILQ_INIT(&minibuffer_map.m);
1955 22268e11 2021-03-11 op TAILQ_INIT(&eecmd_history.head);
1956 22268e11 2021-03-11 op TAILQ_INIT(&ir_history.head);
1957 22268e11 2021-03-11 op TAILQ_INIT(&lu_history.head);
1959 2ba66cea 2021-03-22 op ministate.line.type = LINE_TEXT;
1960 2ba66cea 2021-03-22 op ministate.vline.parent = &ministate.line;
1961 46f6e974 2021-05-17 op ministate.buffer.current_line = &ministate.vline;
1963 bddc7bbd 2021-04-01 op /* initialize help window */
1964 bddc7bbd 2021-04-01 op TAILQ_INIT(&helpwin.head);
1966 9ca15951 2021-03-09 op base_map = &global_map;
1967 f832146f 2021-03-09 op current_map = &global_map;
1968 f832146f 2021-03-09 op load_default_keys();
1975 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
1977 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
1979 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
1981 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
1983 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
1985 bddc7bbd 2021-04-01 op if ((help = newwin(1, 1, 1, 0)) == NULL)
1988 48e9d457 2021-03-06 op body_lines = LINES-3;
1989 48e9d457 2021-03-06 op body_cols = COLS;
1991 43a1b8d0 2021-03-09 op keypad(body, TRUE);
1992 48e9d457 2021-03-06 op scrollok(body, TRUE);
1994 5e11c00c 2021-03-02 op /* non-blocking input */
1995 48e9d457 2021-03-06 op wtimeout(body, 0);
1997 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
1999 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
2000 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
2002 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
2003 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
2005 c7107cec 2021-04-01 op load_last_session(session_new_tab_cb);
2006 c7107cec 2021-04-01 op if (strcmp(url, NEW_TAB_URL) || TAILQ_EMPTY(&tabshead))
2007 c7107cec 2021-04-01 op new_tab(url);
2013 8af5e5ed 2021-03-08 op ui_on_tab_loaded(struct tab *tab)
2015 8af5e5ed 2021-03-08 op stop_loading_anim(tab);
2016 2051e653 2021-03-13 op message("Loaded %s", tab->hist_cur->h);
2018 3d8c2326 2021-03-18 op redraw_tabline();
2019 3d8c2326 2021-03-18 op wrefresh(tabline);
2020 3d8c2326 2021-03-18 op if (in_minibuffer)
2021 3d8c2326 2021-03-18 op wrefresh(minibuf);
2023 3d8c2326 2021-03-18 op wrefresh(body);
2027 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
2029 46f6e974 2021-05-17 op wrap_page(&tab->buffer, body_cols);
2030 3e433958 2021-03-21 op if (tab->flags & TAB_CURRENT) {
2031 46f6e974 2021-05-17 op restore_cursor(&tab->buffer);
2032 3d8c2326 2021-03-18 op redraw_tab(tab);
2034 e8a76665 2021-05-12 op tab->flags |= TAB_URGENT;
2038 5cd2ebb1 2021-03-11 op ui_require_input(struct tab *tab, int hide)
2040 5cd2ebb1 2021-03-11 op /* TODO: hard-switching to another tab is ugly */
2041 5cd2ebb1 2021-03-11 op switch_to_tab(tab);
2043 22268e11 2021-03-11 op enter_minibuffer(ir_self_insert, ir_select, exit_minibuffer,
2044 22268e11 2021-03-11 op &ir_history);
2045 5cd2ebb1 2021-03-11 op strlcpy(ministate.prompt, "Input required: ",
2046 5cd2ebb1 2021-03-11 op sizeof(ministate.prompt));
2047 5cd2ebb1 2021-03-11 op redraw_tab(tab);
2051 b3575139 2021-04-01 op ui_yornp(const char *prompt, void (*fn)(int, unsigned int),
2052 b3575139 2021-04-01 op unsigned int data)
2056 6f66d9bf 2021-04-24 op if (in_minibuffer) {
2057 6f66d9bf 2021-04-24 op fn(0, data);
2061 5d1bac73 2021-03-25 op yornp_cb = fn;
2062 e49ce157 2021-04-01 op yornp_data = data;
2063 5d1bac73 2021-03-25 op enter_minibuffer(yornp_self_insert, yornp_self_insert,
2064 5d1bac73 2021-03-25 op yornp_abort, NULL);
2066 5d1bac73 2021-03-25 op len = sizeof(ministate.prompt);
2067 5d1bac73 2021-03-25 op strlcpy(ministate.prompt, prompt, len);
2068 5d1bac73 2021-03-25 op strlcat(ministate.prompt, " (y or n) ", len);
2069 5d1bac73 2021-03-25 op redraw_tab(current_tab());
2073 de2a69bb 2021-05-17 op ui_read(const char *prompt, void (*fn)(const char*, unsigned int),
2074 de2a69bb 2021-05-17 op unsigned int data)
2078 de2a69bb 2021-05-17 op if (in_minibuffer)
2081 de2a69bb 2021-05-17 op read_cb = fn;
2082 de2a69bb 2021-05-17 op read_data = data;
2083 de2a69bb 2021-05-17 op enter_minibuffer(read_self_insert, read_select, read_abort,
2084 de2a69bb 2021-05-17 op &read_history);
2086 de2a69bb 2021-05-17 op len = sizeof(ministate.prompt);
2087 de2a69bb 2021-05-17 op strlcpy(ministate.prompt, prompt, len);
2088 de2a69bb 2021-05-17 op strlcat(ministate.prompt, ": ", len);
2089 de2a69bb 2021-05-17 op redraw_tab(current_tab());
2093 740f578b 2021-03-15 op ui_notify(const char *fmt, ...)
2097 740f578b 2021-03-15 op va_start(ap, fmt);
2098 740f578b 2021-03-15 op vmessage(fmt, ap);
2103 5e11c00c 2021-03-02 op ui_end(void)