Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
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.
7 5e11c00c 2021-03-02 op *
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.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 1d08c280 2021-03-06 op /*
18 1d08c280 2021-03-06 op * Ncurses UI for telescope.
19 1d08c280 2021-03-06 op *
20 1d08c280 2021-03-06 op *
21 1d08c280 2021-03-06 op * Text wrapping
22 1d08c280 2021-03-06 op * =============
23 1d08c280 2021-03-06 op *
24 1d08c280 2021-03-06 op * There's a simple text wrapping algorithm.
25 1d08c280 2021-03-06 op *
26 1d08c280 2021-03-06 op * 1. if it's a line in a pre-formatted block:
27 1d08c280 2021-03-06 op * a. hard wrap.
28 1d08c280 2021-03-06 op * b. repeat
29 1d08c280 2021-03-06 op * 2. there is enough room for the next word?
30 1d08c280 2021-03-06 op * a. yes: render it
31 1d08c280 2021-03-06 op * b. no: break the current line.
32 1d08c280 2021-03-06 op * i. while there isn't enough space to draw the current
33 1d08c280 2021-03-06 op * word, hard-wrap it
34 1d08c280 2021-03-06 op * ii. draw the remainder of the current word (can be the
35 1d08c280 2021-03-06 op * the entirely)
36 1d08c280 2021-03-06 op * 3. render the spaces after the word
37 1d08c280 2021-03-06 op * a. but if there is not enough room break the line and
38 1d08c280 2021-03-06 op * forget them
39 1d08c280 2021-03-06 op * 4. repeat
40 1d08c280 2021-03-06 op *
41 1d08c280 2021-03-06 op *
42 1d08c280 2021-03-06 op * Text scrolling
43 1d08c280 2021-03-06 op * ==============
44 1d08c280 2021-03-06 op *
45 1d08c280 2021-03-06 op * ncurses allows you to scroll a window, but when a line goes out of
46 1d08c280 2021-03-06 op * the visible area it's forgotten. We keep a list of formatted lines
47 1d08c280 2021-03-06 op * (``visual lines'') that we know fits in the window, and draw them.
48 1d08c280 2021-03-06 op * This way is easy to scroll: just call wscrl and then render the
49 1d08c280 2021-03-06 op * first/last line!
50 1d08c280 2021-03-06 op *
51 1d08c280 2021-03-06 op * This means that on every resize we have to clear our list of lines
52 1d08c280 2021-03-06 op * and re-render everything. A clever approach would be to do this
53 1d08c280 2021-03-06 op * ``on-demand''.
54 1d08c280 2021-03-06 op *
55 1d08c280 2021-03-06 op * TODO: make the text formatting on-demand.
56 1d08c280 2021-03-06 op *
57 1d08c280 2021-03-06 op */
58 1d08c280 2021-03-06 op
59 5e11c00c 2021-03-02 op #include <telescope.h>
60 5e11c00c 2021-03-02 op
61 5e11c00c 2021-03-02 op #include <curses.h>
62 5e11c00c 2021-03-02 op #include <event.h>
63 5e11c00c 2021-03-02 op #include <locale.h>
64 5e11c00c 2021-03-02 op #include <signal.h>
65 7953dd72 2021-03-07 op #include <stdarg.h>
66 eb259e66 2021-03-02 op #include <stdlib.h>
67 eb259e66 2021-03-02 op #include <string.h>
68 5e11c00c 2021-03-02 op
69 5e11c00c 2021-03-02 op #define TAB_CURRENT 0x1
70 5e11c00c 2021-03-02 op
71 1d08c280 2021-03-06 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 1d08c280 2021-03-06 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
73 1d08c280 2021-03-06 op
74 19f1448e 2021-03-08 op struct key;
75 19f1448e 2021-03-08 op
76 5e11c00c 2021-03-02 op static struct event stdioev, winchev;
77 5e11c00c 2021-03-02 op
78 1d08c280 2021-03-06 op static int push_line(struct tab*, const struct line*, const char*, size_t);
79 1d08c280 2021-03-06 op static void empty_vlist(struct tab*);
80 48e9d457 2021-03-06 op static void restore_cursor(struct tab *);
81 48e9d457 2021-03-06 op static void cmd_previous_line(int);
82 48e9d457 2021-03-06 op static void cmd_next_line(int);
83 48e9d457 2021-03-06 op static void cmd_forward_char(int);
84 48e9d457 2021-03-06 op static void cmd_backward_char(int);
85 48e9d457 2021-03-06 op static void cmd_redraw(int);
86 48e9d457 2021-03-06 op static void cmd_scroll_down(int);
87 48e9d457 2021-03-06 op static void cmd_scroll_up(int);
88 48e9d457 2021-03-06 op static void cmd_kill_telescope(int);
89 2a4ad912 2021-03-08 op static void cmd_push_button(int);
90 19f1448e 2021-03-08 op static void cmd_unbound(struct key);
91 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
92 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
93 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
94 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
95 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
96 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
97 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
98 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
99 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
100 48e9d457 2021-03-06 op static void print_line(struct line*);
101 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
102 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
103 bcb0b073 2021-03-07 op static void new_tab(void);
104 5e11c00c 2021-03-02 op
105 1d08c280 2021-03-06 op typedef void (*interactivefn)(int);
106 1d08c280 2021-03-06 op
107 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
108 48e9d457 2021-03-06 op static int body_lines, body_cols;
109 48e9d457 2021-03-06 op
110 48e9d457 2021-03-06 op static struct event clminibufev;
111 48e9d457 2021-03-06 op static int clminibufev_set;
112 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
113 48e9d457 2021-03-06 op
114 bcb0b073 2021-03-07 op static uint32_t tab_counter;
115 bcb0b073 2021-03-07 op
116 1d08c280 2021-03-06 op struct ui_state {
117 1d08c280 2021-03-06 op int curs_x;
118 1d08c280 2021-03-06 op int curs_y;
119 48e9d457 2021-03-06 op size_t line_off;
120 48e9d457 2021-03-06 op size_t line_max;
121 1d08c280 2021-03-06 op
122 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
123 1d08c280 2021-03-06 op };
124 1d08c280 2021-03-06 op
125 19f1448e 2021-03-08 op struct key {
126 19f1448e 2021-03-08 op #define CTRLFLG 0x1
127 19f1448e 2021-03-08 op #define METAFLG 0x2
128 19f1448e 2021-03-08 op int flags;
129 19f1448e 2021-03-08 op int key;
130 19f1448e 2021-03-08 op };
131 19f1448e 2021-03-08 op
132 19f1448e 2021-03-08 op #define KEY(n) ((struct key){0, n})
133 19f1448e 2021-03-08 op #define CKEY(n) ((struct key){0, (n) & 0x1F})
134 19f1448e 2021-03-08 op #define MKEY(n) ((struct key){METAFLG, n})
135 19f1448e 2021-03-08 op #define CMKEY(n) ((struct key){METAFLG, (n) & 0x1F})
136 19f1448e 2021-03-08 op
137 48e9d457 2021-03-06 op struct binding {
138 19f1448e 2021-03-08 op struct key key;
139 48e9d457 2021-03-06 op interactivefn fn;
140 48e9d457 2021-03-06 op } bindings[] = {
141 19f1448e 2021-03-08 op { CKEY('p'), cmd_previous_line, },
142 19f1448e 2021-03-08 op { CKEY('n'), cmd_next_line, },
143 19f1448e 2021-03-08 op { CKEY('f'), cmd_forward_char, },
144 19f1448e 2021-03-08 op { CKEY('b'), cmd_backward_char, },
145 48e9d457 2021-03-06 op
146 19f1448e 2021-03-08 op { CKEY('L'), cmd_redraw, },
147 48e9d457 2021-03-06 op
148 19f1448e 2021-03-08 op { KEY('J'), cmd_scroll_down, },
149 19f1448e 2021-03-08 op { KEY('K'), cmd_scroll_up, },
150 48e9d457 2021-03-06 op
151 19f1448e 2021-03-08 op { KEY('q'), cmd_kill_telescope, },
152 19f1448e 2021-03-08 op { CKEY('m'), cmd_push_button, },
153 19f1448e 2021-03-08 op
154 19f1448e 2021-03-08 op { {0, 0}, NULL, },
155 48e9d457 2021-03-06 op };
156 48e9d457 2021-03-06 op
157 1d08c280 2021-03-06 op static int
158 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
159 1d08c280 2021-03-06 op {
160 1d08c280 2021-03-06 op struct line *vl;
161 1d08c280 2021-03-06 op
162 48e9d457 2021-03-06 op tab->s->line_max++;
163 48e9d457 2021-03-06 op
164 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
165 1d08c280 2021-03-06 op return 0;
166 1d08c280 2021-03-06 op
167 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
168 1d08c280 2021-03-06 op free(vl);
169 1d08c280 2021-03-06 op return 0;
170 1d08c280 2021-03-06 op }
171 1d08c280 2021-03-06 op
172 1d08c280 2021-03-06 op vl->type = l->type;
173 1d08c280 2021-03-06 op if (len != 0)
174 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
175 1d08c280 2021-03-06 op vl->alt = l->alt;
176 1d08c280 2021-03-06 op
177 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
178 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
179 1d08c280 2021-03-06 op else
180 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
181 1d08c280 2021-03-06 op return 1;
182 1d08c280 2021-03-06 op }
183 1d08c280 2021-03-06 op
184 1d08c280 2021-03-06 op static void
185 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
186 1d08c280 2021-03-06 op {
187 1d08c280 2021-03-06 op struct line *l, *t;
188 1d08c280 2021-03-06 op
189 48e9d457 2021-03-06 op tab->s->line_max = 0;
190 48e9d457 2021-03-06 op
191 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
192 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
193 1d08c280 2021-03-06 op free(l->line);
194 1d08c280 2021-03-06 op /* l->alt references the original line! */
195 1d08c280 2021-03-06 op free(l);
196 1d08c280 2021-03-06 op }
197 1d08c280 2021-03-06 op }
198 1d08c280 2021-03-06 op
199 48e9d457 2021-03-06 op static void
200 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
201 48e9d457 2021-03-06 op {
202 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
203 48e9d457 2021-03-06 op }
204 1d08c280 2021-03-06 op
205 1d08c280 2021-03-06 op static void
206 1d08c280 2021-03-06 op cmd_previous_line(int k)
207 1d08c280 2021-03-06 op {
208 1d08c280 2021-03-06 op struct tab *tab;
209 1d08c280 2021-03-06 op
210 1d08c280 2021-03-06 op tab = current_tab();
211 4dd664ce 2021-03-06 op
212 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
213 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
214 4dd664ce 2021-03-06 op cmd_scroll_up(k);
215 4dd664ce 2021-03-06 op }
216 4dd664ce 2021-03-06 op
217 48e9d457 2021-03-06 op restore_cursor(tab);
218 1d08c280 2021-03-06 op }
219 1d08c280 2021-03-06 op
220 1d08c280 2021-03-06 op static void
221 1d08c280 2021-03-06 op cmd_next_line(int k)
222 1d08c280 2021-03-06 op {
223 1d08c280 2021-03-06 op struct tab *tab;
224 1d08c280 2021-03-06 op
225 1d08c280 2021-03-06 op tab = current_tab();
226 4dd664ce 2021-03-06 op
227 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
228 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
229 4dd664ce 2021-03-06 op cmd_scroll_down(k);
230 4dd664ce 2021-03-06 op }
231 4dd664ce 2021-03-06 op
232 48e9d457 2021-03-06 op restore_cursor(tab);
233 1d08c280 2021-03-06 op }
234 1d08c280 2021-03-06 op
235 1d08c280 2021-03-06 op static void
236 1d08c280 2021-03-06 op cmd_forward_char(int k)
237 1d08c280 2021-03-06 op {
238 b2cd5e06 2021-03-06 op struct tab *tab;
239 b2cd5e06 2021-03-06 op
240 b2cd5e06 2021-03-06 op tab = current_tab();
241 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
242 48e9d457 2021-03-06 op restore_cursor(tab);
243 1d08c280 2021-03-06 op }
244 1d08c280 2021-03-06 op
245 1d08c280 2021-03-06 op static void
246 1d08c280 2021-03-06 op cmd_backward_char(int k)
247 1d08c280 2021-03-06 op {
248 b2cd5e06 2021-03-06 op struct tab *tab;
249 b2cd5e06 2021-03-06 op
250 b2cd5e06 2021-03-06 op tab = current_tab();
251 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
252 48e9d457 2021-03-06 op restore_cursor(tab);
253 1d08c280 2021-03-06 op }
254 1d08c280 2021-03-06 op
255 1d08c280 2021-03-06 op static void
256 1d08c280 2021-03-06 op cmd_redraw(int k)
257 1d08c280 2021-03-06 op {
258 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
259 1d08c280 2021-03-06 op }
260 1d08c280 2021-03-06 op
261 1d08c280 2021-03-06 op static void
262 48e9d457 2021-03-06 op cmd_scroll_up(int k)
263 1d08c280 2021-03-06 op {
264 48e9d457 2021-03-06 op struct tab *tab;
265 48e9d457 2021-03-06 op struct line *l;
266 48e9d457 2021-03-06 op
267 48e9d457 2021-03-06 op tab = current_tab();
268 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
269 48e9d457 2021-03-06 op return;
270 48e9d457 2021-03-06 op
271 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
272 48e9d457 2021-03-06 op wscrl(body, -1);
273 48e9d457 2021-03-06 op wmove(body, 0, 0);
274 48e9d457 2021-03-06 op print_line(l);
275 1d08c280 2021-03-06 op }
276 1d08c280 2021-03-06 op
277 1d08c280 2021-03-06 op static void
278 48e9d457 2021-03-06 op cmd_scroll_down(int k)
279 1d08c280 2021-03-06 op {
280 48e9d457 2021-03-06 op struct tab *tab;
281 48e9d457 2021-03-06 op struct line *l;
282 48e9d457 2021-03-06 op size_t n;
283 48e9d457 2021-03-06 op
284 48e9d457 2021-03-06 op tab = current_tab();
285 48e9d457 2021-03-06 op
286 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
287 48e9d457 2021-03-06 op return;
288 48e9d457 2021-03-06 op
289 48e9d457 2021-03-06 op tab->s->line_off++;
290 48e9d457 2021-03-06 op wscrl(body, 1);
291 48e9d457 2021-03-06 op
292 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
293 48e9d457 2021-03-06 op return;
294 48e9d457 2021-03-06 op
295 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
296 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
297 48e9d457 2021-03-06 op print_line(l);
298 1d08c280 2021-03-06 op }
299 1d08c280 2021-03-06 op
300 1d08c280 2021-03-06 op static void
301 1d08c280 2021-03-06 op cmd_kill_telescope(int k)
302 1d08c280 2021-03-06 op {
303 1d08c280 2021-03-06 op event_loopbreak();
304 1d08c280 2021-03-06 op }
305 1d08c280 2021-03-06 op
306 1d08c280 2021-03-06 op static void
307 2a4ad912 2021-03-08 op cmd_push_button(int k)
308 2a4ad912 2021-03-08 op {
309 2a4ad912 2021-03-08 op struct tab *tab;
310 2a4ad912 2021-03-08 op struct line *l;
311 2a4ad912 2021-03-08 op size_t nth;
312 2a4ad912 2021-03-08 op
313 2a4ad912 2021-03-08 op tab = current_tab();
314 2a4ad912 2021-03-08 op
315 2a4ad912 2021-03-08 op nth = tab->s->line_off + tab->s->curs_y;
316 2a4ad912 2021-03-08 op if (nth > tab->s->line_max)
317 2a4ad912 2021-03-08 op return;
318 2a4ad912 2021-03-08 op l = nth_line(tab, nth);
319 2a4ad912 2021-03-08 op message("Enter on line: \"%s\"", l->line ? l->line : "");
320 2a4ad912 2021-03-08 op }
321 2a4ad912 2021-03-08 op
322 2a4ad912 2021-03-08 op static void
323 19f1448e 2021-03-08 op cmd_unbound(struct key k)
324 1d08c280 2021-03-06 op {
325 19f1448e 2021-03-08 op message("%s%c is undefined",
326 19f1448e 2021-03-08 op (k.flags & METAFLG) ? "M-" : "",
327 19f1448e 2021-03-08 op k.key);
328 1d08c280 2021-03-06 op }
329 1d08c280 2021-03-06 op
330 48e9d457 2021-03-06 op static struct line *
331 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
332 48e9d457 2021-03-06 op {
333 48e9d457 2021-03-06 op struct line *l;
334 48e9d457 2021-03-06 op size_t i;
335 48e9d457 2021-03-06 op
336 48e9d457 2021-03-06 op i = 0;
337 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
338 48e9d457 2021-03-06 op if (i == n)
339 48e9d457 2021-03-06 op return l;
340 48e9d457 2021-03-06 op i++;
341 48e9d457 2021-03-06 op }
342 48e9d457 2021-03-06 op
343 48e9d457 2021-03-06 op /* unreachable */
344 48e9d457 2021-03-06 op abort();
345 48e9d457 2021-03-06 op }
346 48e9d457 2021-03-06 op
347 5e11c00c 2021-03-02 op static struct tab *
348 5e11c00c 2021-03-02 op current_tab(void)
349 5e11c00c 2021-03-02 op {
350 5e11c00c 2021-03-02 op struct tab *t;
351 5e11c00c 2021-03-02 op
352 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
353 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
354 5e11c00c 2021-03-02 op return t;
355 5e11c00c 2021-03-02 op }
356 5e11c00c 2021-03-02 op
357 5e11c00c 2021-03-02 op /* unreachable */
358 5e11c00c 2021-03-02 op abort();
359 5e11c00c 2021-03-02 op }
360 5e11c00c 2021-03-02 op
361 5e11c00c 2021-03-02 op static void
362 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
363 5e11c00c 2021-03-02 op {
364 1d08c280 2021-03-06 op struct binding *b;
365 19f1448e 2021-03-08 op struct key key;
366 19f1448e 2021-03-08 op int k, j;
367 19f1448e 2021-03-08 op
368 19f1448e 2021-03-08 op key.flags = 0;
369 5e11c00c 2021-03-02 op
370 48e9d457 2021-03-06 op k = wgetch(body);
371 5e11c00c 2021-03-02 op
372 1d08c280 2021-03-06 op if (k == ERR)
373 5e11c00c 2021-03-02 op return;
374 5e11c00c 2021-03-02 op
375 19f1448e 2021-03-08 op /* look for a M-key */
376 19f1448e 2021-03-08 op if (k == 27) {
377 19f1448e 2021-03-08 op wtimeout(body, 0); /* TODO: make escape-time customizable */
378 19f1448e 2021-03-08 op j = wgetch(body);
379 19f1448e 2021-03-08 op if (j != ERR) {
380 19f1448e 2021-03-08 op k = j;
381 19f1448e 2021-03-08 op key.flags = METAFLG;
382 19f1448e 2021-03-08 op }
383 19f1448e 2021-03-08 op }
384 19f1448e 2021-03-08 op
385 19f1448e 2021-03-08 op key.key = k;
386 19f1448e 2021-03-08 op
387 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
388 19f1448e 2021-03-08 op if (key.flags == b->key.flags &&
389 19f1448e 2021-03-08 op key.key == b->key.key) {
390 1d08c280 2021-03-06 op b->fn(k);
391 1d08c280 2021-03-06 op goto done;
392 1d08c280 2021-03-06 op }
393 eb259e66 2021-03-02 op }
394 eb259e66 2021-03-02 op
395 19f1448e 2021-03-08 op cmd_unbound(key);
396 1d08c280 2021-03-06 op
397 1d08c280 2021-03-06 op done:
398 48e9d457 2021-03-06 op restore_cursor(current_tab());
399 48e9d457 2021-03-06 op wrefresh(tabline);
400 48e9d457 2021-03-06 op wrefresh(modeline);
401 48e9d457 2021-03-06 op wrefresh(minibuf);
402 a6d450c1 2021-03-06 op
403 a6d450c1 2021-03-06 op wrefresh(body);
404 a6d450c1 2021-03-06 op }
405 48e9d457 2021-03-06 op
406 a6d450c1 2021-03-06 op static void
407 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
408 a6d450c1 2021-03-06 op {
409 a6d450c1 2021-03-06 op clminibufev_set = 0;
410 a6d450c1 2021-03-06 op werase(minibuf);
411 a6d450c1 2021-03-06 op wrefresh(minibuf);
412 48e9d457 2021-03-06 op wrefresh(body);
413 5e11c00c 2021-03-02 op }
414 5e11c00c 2021-03-02 op
415 5e11c00c 2021-03-02 op static void
416 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
417 5e11c00c 2021-03-02 op {
418 1d08c280 2021-03-06 op struct tab *tab;
419 1d08c280 2021-03-06 op
420 5e11c00c 2021-03-02 op endwin();
421 5e11c00c 2021-03-02 op refresh();
422 5e11c00c 2021-03-02 op clear();
423 5e11c00c 2021-03-02 op
424 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
425 48e9d457 2021-03-06 op
426 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
427 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
428 48e9d457 2021-03-06 op
429 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
430 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
431 48e9d457 2021-03-06 op
432 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
433 48e9d457 2021-03-06 op body_lines = LINES-3;
434 bd9637e9 2021-03-06 op body_cols = COLS;
435 48e9d457 2021-03-06 op
436 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
437 48e9d457 2021-03-06 op
438 1d08c280 2021-03-06 op tab = current_tab();
439 1d08c280 2021-03-06 op
440 1d08c280 2021-03-06 op wrap_page(tab);
441 1d08c280 2021-03-06 op redraw_tab(tab);
442 5e11c00c 2021-03-02 op }
443 5e11c00c 2021-03-02 op
444 eb259e66 2021-03-02 op /*
445 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
446 eb259e66 2021-03-02 op * and the end of the separator after the word.
447 eb259e66 2021-03-02 op */
448 eb259e66 2021-03-02 op static int
449 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
450 eb259e66 2021-03-02 op {
451 eb259e66 2021-03-02 op *endword = s;
452 eb259e66 2021-03-02 op *endword = s;
453 eb259e66 2021-03-02 op
454 eb259e66 2021-03-02 op if (*s == '\0')
455 eb259e66 2021-03-02 op return 0;
456 eb259e66 2021-03-02 op
457 eb259e66 2021-03-02 op /* find the end of the current world */
458 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
459 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
460 eb259e66 2021-03-02 op break;
461 eb259e66 2021-03-02 op }
462 eb259e66 2021-03-02 op
463 eb259e66 2021-03-02 op *endword = s;
464 eb259e66 2021-03-02 op
465 eb259e66 2021-03-02 op /* find the end of the separator */
466 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
467 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
468 eb259e66 2021-03-02 op break;
469 eb259e66 2021-03-02 op }
470 eb259e66 2021-03-02 op
471 eb259e66 2021-03-02 op *endspc = s;
472 eb259e66 2021-03-02 op
473 eb259e66 2021-03-02 op return 1;
474 eb259e66 2021-03-02 op }
475 eb259e66 2021-03-02 op
476 1d08c280 2021-03-06 op static inline int
477 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
478 1d08c280 2021-03-06 op const char **line)
479 eb259e66 2021-03-02 op {
480 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
481 1d08c280 2021-03-06 op return 0;
482 1d08c280 2021-03-06 op *line += *off - zero;
483 eb259e66 2021-03-02 op *off = zero;
484 1d08c280 2021-03-06 op return 1;
485 eb259e66 2021-03-02 op }
486 eb259e66 2021-03-02 op
487 eb259e66 2021-03-02 op static inline void
488 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
489 eb259e66 2021-03-02 op {
490 eb259e66 2021-03-02 op size_t i;
491 eb259e66 2021-03-02 op
492 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
493 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
494 eb259e66 2021-03-02 op addch((*s)[i]);
495 eb259e66 2021-03-02 op *off += len;
496 eb259e66 2021-03-02 op *s += len;
497 eb259e66 2021-03-02 op }
498 eb259e66 2021-03-02 op
499 eb259e66 2021-03-02 op /*
500 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
501 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
502 eb259e66 2021-03-02 op *
503 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
504 eb259e66 2021-03-02 op */
505 5e11c00c 2021-03-02 op static void
506 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
507 eb259e66 2021-03-02 op {
508 eb259e66 2021-03-02 op size_t zero, off, len, split;
509 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
510 eb259e66 2021-03-02 op
511 1d08c280 2021-03-06 op zero = strlen(prfx);
512 eb259e66 2021-03-02 op off = zero;
513 1d08c280 2021-03-06 op line = l->line;
514 1d08c280 2021-03-06 op linestart = l->line;
515 eb259e66 2021-03-02 op
516 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
517 eb259e66 2021-03-02 op len = endword - line;
518 48e9d457 2021-03-06 op if (off + len >= body_cols) {
519 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
520 48e9d457 2021-03-06 op while (len >= body_cols) {
521 eb259e66 2021-03-02 op /* hard wrap */
522 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
523 48e9d457 2021-03-06 op len -= body_cols-1;
524 48e9d457 2021-03-06 op line += body_cols-1;
525 eb259e66 2021-03-02 op }
526 eb259e66 2021-03-02 op
527 eb259e66 2021-03-02 op if (len != 0)
528 1d08c280 2021-03-06 op off += len;
529 1d08c280 2021-03-06 op } else
530 1d08c280 2021-03-06 op off += len;
531 eb259e66 2021-03-02 op
532 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
533 eb259e66 2021-03-02 op len = endspc - endword;
534 eb259e66 2021-03-02 op /* line = endspc; */
535 eb259e66 2021-03-02 op if (off != zero) {
536 48e9d457 2021-03-06 op if (off + len >= body_cols) {
537 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
538 1d08c280 2021-03-06 op linestart = endspc;
539 1d08c280 2021-03-06 op } else
540 1d08c280 2021-03-06 op off += len;
541 eb259e66 2021-03-02 op }
542 eb259e66 2021-03-02 op
543 eb259e66 2021-03-02 op line = endspc;
544 eb259e66 2021-03-02 op }
545 eb259e66 2021-03-02 op
546 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
547 eb259e66 2021-03-02 op }
548 eb259e66 2021-03-02 op
549 1d08c280 2021-03-06 op static int
550 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
551 5e11c00c 2021-03-02 op {
552 1d08c280 2021-03-06 op size_t off, len;
553 1d08c280 2021-03-06 op const char *linestart;
554 5e11c00c 2021-03-02 op
555 1d08c280 2021-03-06 op len = strlen(l->line);
556 1d08c280 2021-03-06 op off = 0;
557 1d08c280 2021-03-06 op linestart = l->line;
558 1d08c280 2021-03-06 op
559 1d08c280 2021-03-06 op while (len >= COLS) {
560 1d08c280 2021-03-06 op len -= COLS-1;
561 1d08c280 2021-03-06 op off = COLS-1;
562 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
563 1d08c280 2021-03-06 op return 0;
564 1d08c280 2021-03-06 op }
565 1d08c280 2021-03-06 op
566 ede1f142 2021-03-07 op if (len != 0)
567 ede1f142 2021-03-07 op return emitline(tab, 0, &len, l, &linestart);
568 ede1f142 2021-03-07 op
569 1d08c280 2021-03-06 op return 1;
570 1d08c280 2021-03-06 op }
571 5e11c00c 2021-03-02 op
572 1d08c280 2021-03-06 op static int
573 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
574 1d08c280 2021-03-06 op {
575 1d08c280 2021-03-06 op struct line *l;
576 1d08c280 2021-03-06 op
577 1d08c280 2021-03-06 op empty_vlist(tab);
578 1d08c280 2021-03-06 op
579 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
580 5e11c00c 2021-03-02 op switch (l->type) {
581 5e11c00c 2021-03-02 op case LINE_TEXT:
582 1d08c280 2021-03-06 op wrap_text(tab, "", l);
583 5e11c00c 2021-03-02 op break;
584 5e11c00c 2021-03-02 op case LINE_LINK:
585 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
586 5e11c00c 2021-03-02 op break;
587 5e11c00c 2021-03-02 op case LINE_TITLE_1:
588 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
589 5e11c00c 2021-03-02 op break;
590 5e11c00c 2021-03-02 op case LINE_TITLE_2:
591 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
592 5e11c00c 2021-03-02 op break;
593 5e11c00c 2021-03-02 op case LINE_TITLE_3:
594 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
595 5e11c00c 2021-03-02 op break;
596 5e11c00c 2021-03-02 op case LINE_ITEM:
597 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
598 5e11c00c 2021-03-02 op break;
599 5e11c00c 2021-03-02 op case LINE_QUOTE:
600 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
601 5e11c00c 2021-03-02 op break;
602 5e11c00c 2021-03-02 op case LINE_PRE_START:
603 5e11c00c 2021-03-02 op case LINE_PRE_END:
604 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
605 5e11c00c 2021-03-02 op break;
606 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
607 1d08c280 2021-03-06 op hardwrap_text(tab, l);
608 5e11c00c 2021-03-02 op break;
609 5e11c00c 2021-03-02 op }
610 5e11c00c 2021-03-02 op }
611 1d08c280 2021-03-06 op return 1;
612 1d08c280 2021-03-06 op }
613 5e11c00c 2021-03-02 op
614 1d08c280 2021-03-06 op static inline void
615 1d08c280 2021-03-06 op print_line(struct line *l)
616 1d08c280 2021-03-06 op {
617 bd9637e9 2021-03-06 op const char *text = l->line;
618 bd9637e9 2021-03-06 op
619 bd9637e9 2021-03-06 op if (text == NULL)
620 bd9637e9 2021-03-06 op text = "";
621 bd9637e9 2021-03-06 op
622 1d08c280 2021-03-06 op switch (l->type) {
623 1d08c280 2021-03-06 op case LINE_TEXT:
624 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
625 1d08c280 2021-03-06 op break;
626 1d08c280 2021-03-06 op case LINE_LINK:
627 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
628 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
629 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
630 bd9637e9 2021-03-06 op return;
631 1d08c280 2021-03-06 op case LINE_TITLE_1:
632 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
633 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
634 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
635 bd9637e9 2021-03-06 op return;
636 1d08c280 2021-03-06 op case LINE_TITLE_2:
637 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
638 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
639 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
640 bd9637e9 2021-03-06 op return;
641 bd9637e9 2021-03-06 op case LINE_TITLE_3:
642 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
643 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
644 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
645 bd9637e9 2021-03-06 op return;
646 bd9637e9 2021-03-06 op case LINE_ITEM:
647 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
648 bd9637e9 2021-03-06 op return;
649 1d08c280 2021-03-06 op case LINE_QUOTE:
650 bd9637e9 2021-03-06 op wattron(body, A_DIM);
651 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
652 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
653 bd9637e9 2021-03-06 op return;
654 1d08c280 2021-03-06 op case LINE_PRE_START:
655 1d08c280 2021-03-06 op case LINE_PRE_END:
656 48e9d457 2021-03-06 op wprintw(body, "```");
657 bd9637e9 2021-03-06 op return;
658 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
659 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
660 bd9637e9 2021-03-06 op return;
661 1d08c280 2021-03-06 op }
662 1d08c280 2021-03-06 op }
663 1d08c280 2021-03-06 op
664 1d08c280 2021-03-06 op static void
665 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
666 1d08c280 2021-03-06 op {
667 48e9d457 2021-03-06 op int x, y, max_x, max_y;
668 48e9d457 2021-03-06 op const char *url = "TODO:url";
669 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
670 1d08c280 2021-03-06 op
671 48e9d457 2021-03-06 op wclear(modeline);
672 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
673 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
674 1d08c280 2021-03-06 op
675 48e9d457 2021-03-06 op wprintw(modeline, "-- %s %s ", mode, url);
676 48e9d457 2021-03-06 op getyx(modeline, y, x);
677 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
678 48e9d457 2021-03-06 op
679 48e9d457 2021-03-06 op (void)y;
680 48e9d457 2021-03-06 op (void)max_y;
681 48e9d457 2021-03-06 op
682 48e9d457 2021-03-06 op for (; x < max_x; ++x)
683 48e9d457 2021-03-06 op waddstr(modeline, "-");
684 48e9d457 2021-03-06 op }
685 48e9d457 2021-03-06 op
686 48e9d457 2021-03-06 op static void
687 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
688 48e9d457 2021-03-06 op {
689 48e9d457 2021-03-06 op struct line *l;
690 48e9d457 2021-03-06 op int line;
691 48e9d457 2021-03-06 op
692 48e9d457 2021-03-06 op werase(body);
693 48e9d457 2021-03-06 op
694 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
695 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
696 48e9d457 2021-03-06 op return;
697 48e9d457 2021-03-06 op
698 48e9d457 2021-03-06 op line = 0;
699 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
700 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
701 48e9d457 2021-03-06 op wmove(body, line, 0);
702 1d08c280 2021-03-06 op print_line(l);
703 48e9d457 2021-03-06 op line++;
704 48e9d457 2021-03-06 op if (line == body_lines)
705 48e9d457 2021-03-06 op break;
706 1d08c280 2021-03-06 op }
707 1d08c280 2021-03-06 op
708 48e9d457 2021-03-06 op redraw_modeline(tab);
709 48e9d457 2021-03-06 op
710 48e9d457 2021-03-06 op restore_cursor(tab);
711 48e9d457 2021-03-06 op wrefresh(tabline);
712 48e9d457 2021-03-06 op wrefresh(modeline);
713 48e9d457 2021-03-06 op wrefresh(minibuf);
714 7953dd72 2021-03-07 op
715 7953dd72 2021-03-07 op wrefresh(body);
716 7953dd72 2021-03-07 op }
717 7953dd72 2021-03-07 op
718 7953dd72 2021-03-07 op static void
719 7953dd72 2021-03-07 op message(const char *fmt, ...)
720 7953dd72 2021-03-07 op {
721 7953dd72 2021-03-07 op va_list ap;
722 48e9d457 2021-03-06 op
723 7953dd72 2021-03-07 op va_start(ap, fmt);
724 7953dd72 2021-03-07 op
725 7953dd72 2021-03-07 op if (clminibufev_set)
726 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
727 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
728 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
729 7953dd72 2021-03-07 op clminibufev_set = 1;
730 7953dd72 2021-03-07 op
731 7953dd72 2021-03-07 op werase(minibuf);
732 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
733 7953dd72 2021-03-07 op
734 7953dd72 2021-03-07 op wrefresh(minibuf);
735 48e9d457 2021-03-06 op wrefresh(body);
736 7953dd72 2021-03-07 op
737 7953dd72 2021-03-07 op va_end(ap);
738 bcb0b073 2021-03-07 op }
739 bcb0b073 2021-03-07 op
740 bcb0b073 2021-03-07 op static void
741 bcb0b073 2021-03-07 op new_tab(void)
742 bcb0b073 2021-03-07 op {
743 bcb0b073 2021-03-07 op struct tab *tab, *t;
744 bcb0b073 2021-03-07 op const char *url = "about:new";
745 bcb0b073 2021-03-07 op
746 bcb0b073 2021-03-07 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
747 bcb0b073 2021-03-07 op goto err;
748 bcb0b073 2021-03-07 op
749 bcb0b073 2021-03-07 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
750 bcb0b073 2021-03-07 op goto err;
751 bcb0b073 2021-03-07 op
752 bcb0b073 2021-03-07 op TAILQ_INIT(&tab->s->head);
753 bcb0b073 2021-03-07 op TAILQ_FOREACH(t, &tabshead, tabs) {
754 bcb0b073 2021-03-07 op t->flags &= ~TAB_CURRENT;
755 bcb0b073 2021-03-07 op }
756 bcb0b073 2021-03-07 op
757 bcb0b073 2021-03-07 op tab->id = tab_counter++;
758 bcb0b073 2021-03-07 op tab->flags = TAB_CURRENT;
759 bcb0b073 2021-03-07 op
760 bcb0b073 2021-03-07 op if (TAILQ_EMPTY(&tabshead))
761 bcb0b073 2021-03-07 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
762 bcb0b073 2021-03-07 op else
763 bcb0b073 2021-03-07 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
764 bcb0b073 2021-03-07 op
765 bcb0b073 2021-03-07 op load_url(tab, url);
766 bcb0b073 2021-03-07 op return;
767 bcb0b073 2021-03-07 op
768 bcb0b073 2021-03-07 op err:
769 bcb0b073 2021-03-07 op event_loopbreak();
770 5e11c00c 2021-03-02 op }
771 5e11c00c 2021-03-02 op
772 5e11c00c 2021-03-02 op int
773 5e11c00c 2021-03-02 op ui_init(void)
774 5e11c00c 2021-03-02 op {
775 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
776 5e11c00c 2021-03-02 op
777 5e11c00c 2021-03-02 op initscr();
778 15e1b108 2021-03-02 op raw();
779 5e11c00c 2021-03-02 op noecho();
780 5e11c00c 2021-03-02 op
781 5e11c00c 2021-03-02 op nonl();
782 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
783 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
784 5e11c00c 2021-03-02 op
785 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
786 48e9d457 2021-03-06 op return 0;
787 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
788 48e9d457 2021-03-06 op return 0;
789 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
790 48e9d457 2021-03-06 op return 0;
791 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
792 48e9d457 2021-03-06 op return 0;
793 1d08c280 2021-03-06 op
794 48e9d457 2021-03-06 op body_lines = LINES-3;
795 48e9d457 2021-03-06 op body_cols = COLS;
796 48e9d457 2021-03-06 op
797 48e9d457 2021-03-06 op scrollok(body, TRUE);
798 48e9d457 2021-03-06 op
799 5e11c00c 2021-03-02 op /* non-blocking input */
800 48e9d457 2021-03-06 op wtimeout(body, 0);
801 5e11c00c 2021-03-02 op
802 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
803 5e11c00c 2021-03-02 op
804 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
805 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
806 5e11c00c 2021-03-02 op
807 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
808 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
809 5e11c00c 2021-03-02 op
810 bcb0b073 2021-03-07 op new_tab();
811 5e11c00c 2021-03-02 op
812 1d08c280 2021-03-06 op return 1;
813 5e11c00c 2021-03-02 op }
814 5e11c00c 2021-03-02 op
815 5e11c00c 2021-03-02 op void
816 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
817 5e11c00c 2021-03-02 op {
818 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
819 5e11c00c 2021-03-02 op return;
820 5e11c00c 2021-03-02 op
821 1d08c280 2021-03-06 op wrap_page(tab);
822 5e11c00c 2021-03-02 op redraw_tab(tab);
823 5e11c00c 2021-03-02 op }
824 5e11c00c 2021-03-02 op
825 5e11c00c 2021-03-02 op void
826 5e11c00c 2021-03-02 op ui_end(void)
827 5e11c00c 2021-03-02 op {
828 5e11c00c 2021-03-02 op endwin();
829 5e11c00c 2021-03-02 op }