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 eb259e66 2021-03-02 op #define CTRL(x) ((x)&0x1F)
72 eb259e66 2021-03-02 op
73 1d08c280 2021-03-06 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 1d08c280 2021-03-06 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
75 1d08c280 2021-03-06 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 48e9d457 2021-03-06 op static struct line *nth_line(struct tab*, size_t);
90 5e11c00c 2021-03-02 op static struct tab *current_tab(void);
91 5e11c00c 2021-03-02 op static void dispatch_stdio(int, short, void*);
92 a6d450c1 2021-03-06 op static void handle_clear_minibuf(int, short, void*);
93 5e11c00c 2021-03-02 op static void handle_resize(int, short, void*);
94 eb259e66 2021-03-02 op static int word_bourdaries(const char*, const char*, const char**, const char**);
95 1d08c280 2021-03-06 op static void wrap_text(struct tab*, const char*, struct line*);
96 1d08c280 2021-03-06 op static int hardwrap_text(struct tab*, struct line*);
97 1d08c280 2021-03-06 op static int wrap_page(struct tab*);
98 48e9d457 2021-03-06 op static void print_line(struct line*);
99 5e11c00c 2021-03-02 op static void redraw_tab(struct tab*);
100 7953dd72 2021-03-07 op static void message(const char*, ...) __attribute__((format(printf, 1, 2)));
101 5e11c00c 2021-03-02 op
102 1d08c280 2021-03-06 op typedef void (*interactivefn)(int);
103 1d08c280 2021-03-06 op
104 1d08c280 2021-03-06 op static void cmd_unbound(int);
105 1d08c280 2021-03-06 op
106 48e9d457 2021-03-06 op static WINDOW *tabline, *body, *modeline, *minibuf;
107 48e9d457 2021-03-06 op static int body_lines, body_cols;
108 48e9d457 2021-03-06 op
109 48e9d457 2021-03-06 op static struct event clminibufev;
110 48e9d457 2021-03-06 op static int clminibufev_set;
111 a6d450c1 2021-03-06 op static struct timeval clminibufev_timer = { 5, 0 };
112 48e9d457 2021-03-06 op
113 1d08c280 2021-03-06 op struct ui_state {
114 1d08c280 2021-03-06 op int curs_x;
115 1d08c280 2021-03-06 op int curs_y;
116 48e9d457 2021-03-06 op size_t line_off;
117 48e9d457 2021-03-06 op size_t line_max;
118 1d08c280 2021-03-06 op
119 1d08c280 2021-03-06 op TAILQ_HEAD(, line) head;
120 1d08c280 2021-03-06 op };
121 1d08c280 2021-03-06 op
122 48e9d457 2021-03-06 op struct binding {
123 48e9d457 2021-03-06 op int key;
124 48e9d457 2021-03-06 op interactivefn fn;
125 48e9d457 2021-03-06 op } bindings[] = {
126 48e9d457 2021-03-06 op { CTRL('p'), cmd_previous_line, },
127 48e9d457 2021-03-06 op { CTRL('n'), cmd_next_line, },
128 48e9d457 2021-03-06 op { CTRL('f'), cmd_forward_char, },
129 48e9d457 2021-03-06 op { CTRL('b'), cmd_backward_char, },
130 48e9d457 2021-03-06 op
131 48e9d457 2021-03-06 op { CTRL('L'), cmd_redraw, },
132 48e9d457 2021-03-06 op
133 48e9d457 2021-03-06 op { 'J', cmd_scroll_down, },
134 48e9d457 2021-03-06 op { 'K', cmd_scroll_up, },
135 48e9d457 2021-03-06 op
136 48e9d457 2021-03-06 op { 'q', cmd_kill_telescope, },
137 48e9d457 2021-03-06 op
138 48e9d457 2021-03-06 op { 0, NULL, },
139 48e9d457 2021-03-06 op };
140 48e9d457 2021-03-06 op
141 1d08c280 2021-03-06 op static int
142 1d08c280 2021-03-06 op push_line(struct tab *tab, const struct line *l, const char *buf, size_t len)
143 1d08c280 2021-03-06 op {
144 1d08c280 2021-03-06 op struct line *vl;
145 1d08c280 2021-03-06 op
146 48e9d457 2021-03-06 op tab->s->line_max++;
147 48e9d457 2021-03-06 op
148 1d08c280 2021-03-06 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
149 1d08c280 2021-03-06 op return 0;
150 1d08c280 2021-03-06 op
151 1d08c280 2021-03-06 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
152 1d08c280 2021-03-06 op free(vl);
153 1d08c280 2021-03-06 op return 0;
154 1d08c280 2021-03-06 op }
155 1d08c280 2021-03-06 op
156 1d08c280 2021-03-06 op vl->type = l->type;
157 1d08c280 2021-03-06 op if (len != 0)
158 1d08c280 2021-03-06 op memcpy(vl->line, buf, len);
159 1d08c280 2021-03-06 op vl->alt = l->alt;
160 1d08c280 2021-03-06 op
161 1d08c280 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
162 1d08c280 2021-03-06 op TAILQ_INSERT_HEAD(&tab->s->head, vl, lines);
163 1d08c280 2021-03-06 op else
164 1d08c280 2021-03-06 op TAILQ_INSERT_TAIL(&tab->s->head, vl, lines);
165 1d08c280 2021-03-06 op return 1;
166 1d08c280 2021-03-06 op }
167 1d08c280 2021-03-06 op
168 1d08c280 2021-03-06 op static void
169 1d08c280 2021-03-06 op empty_vlist(struct tab *tab)
170 1d08c280 2021-03-06 op {
171 1d08c280 2021-03-06 op struct line *l, *t;
172 1d08c280 2021-03-06 op
173 48e9d457 2021-03-06 op tab->s->line_max = 0;
174 48e9d457 2021-03-06 op
175 1d08c280 2021-03-06 op TAILQ_FOREACH_SAFE(l, &tab->s->head, lines, t) {
176 1d08c280 2021-03-06 op TAILQ_REMOVE(&tab->s->head, l, lines);
177 1d08c280 2021-03-06 op free(l->line);
178 1d08c280 2021-03-06 op /* l->alt references the original line! */
179 1d08c280 2021-03-06 op free(l);
180 1d08c280 2021-03-06 op }
181 1d08c280 2021-03-06 op }
182 1d08c280 2021-03-06 op
183 48e9d457 2021-03-06 op static void
184 48e9d457 2021-03-06 op restore_cursor(struct tab *tab)
185 48e9d457 2021-03-06 op {
186 48e9d457 2021-03-06 op wmove(body, tab->s->curs_y, tab->s->curs_x);
187 48e9d457 2021-03-06 op }
188 1d08c280 2021-03-06 op
189 1d08c280 2021-03-06 op static void
190 1d08c280 2021-03-06 op cmd_previous_line(int k)
191 1d08c280 2021-03-06 op {
192 1d08c280 2021-03-06 op struct tab *tab;
193 1d08c280 2021-03-06 op
194 1d08c280 2021-03-06 op tab = current_tab();
195 4dd664ce 2021-03-06 op
196 4dd664ce 2021-03-06 op if (--tab->s->curs_y < 0) {
197 4dd664ce 2021-03-06 op tab->s->curs_y = 0;
198 4dd664ce 2021-03-06 op cmd_scroll_up(k);
199 4dd664ce 2021-03-06 op }
200 4dd664ce 2021-03-06 op
201 48e9d457 2021-03-06 op restore_cursor(tab);
202 1d08c280 2021-03-06 op }
203 1d08c280 2021-03-06 op
204 1d08c280 2021-03-06 op static void
205 1d08c280 2021-03-06 op cmd_next_line(int k)
206 1d08c280 2021-03-06 op {
207 1d08c280 2021-03-06 op struct tab *tab;
208 1d08c280 2021-03-06 op
209 1d08c280 2021-03-06 op tab = current_tab();
210 4dd664ce 2021-03-06 op
211 4dd664ce 2021-03-06 op if (++tab->s->curs_y > body_lines-1) {
212 4dd664ce 2021-03-06 op tab->s->curs_y = body_lines-1;
213 4dd664ce 2021-03-06 op cmd_scroll_down(k);
214 4dd664ce 2021-03-06 op }
215 4dd664ce 2021-03-06 op
216 48e9d457 2021-03-06 op restore_cursor(tab);
217 1d08c280 2021-03-06 op }
218 1d08c280 2021-03-06 op
219 1d08c280 2021-03-06 op static void
220 1d08c280 2021-03-06 op cmd_forward_char(int k)
221 1d08c280 2021-03-06 op {
222 b2cd5e06 2021-03-06 op struct tab *tab;
223 b2cd5e06 2021-03-06 op
224 b2cd5e06 2021-03-06 op tab = current_tab();
225 4dd664ce 2021-03-06 op tab->s->curs_x = MIN(body_cols-1, tab->s->curs_x+1);
226 48e9d457 2021-03-06 op restore_cursor(tab);
227 1d08c280 2021-03-06 op }
228 1d08c280 2021-03-06 op
229 1d08c280 2021-03-06 op static void
230 1d08c280 2021-03-06 op cmd_backward_char(int k)
231 1d08c280 2021-03-06 op {
232 b2cd5e06 2021-03-06 op struct tab *tab;
233 b2cd5e06 2021-03-06 op
234 b2cd5e06 2021-03-06 op tab = current_tab();
235 b2cd5e06 2021-03-06 op tab->s->curs_x = MAX(0, tab->s->curs_x-1);
236 48e9d457 2021-03-06 op restore_cursor(tab);
237 1d08c280 2021-03-06 op }
238 1d08c280 2021-03-06 op
239 1d08c280 2021-03-06 op static void
240 1d08c280 2021-03-06 op cmd_redraw(int k)
241 1d08c280 2021-03-06 op {
242 b1738d2e 2021-03-06 op handle_resize(0, 0, NULL);
243 1d08c280 2021-03-06 op }
244 1d08c280 2021-03-06 op
245 1d08c280 2021-03-06 op static void
246 48e9d457 2021-03-06 op cmd_scroll_up(int k)
247 1d08c280 2021-03-06 op {
248 48e9d457 2021-03-06 op struct tab *tab;
249 48e9d457 2021-03-06 op struct line *l;
250 48e9d457 2021-03-06 op
251 48e9d457 2021-03-06 op tab = current_tab();
252 48e9d457 2021-03-06 op if (tab->s->line_off == 0)
253 48e9d457 2021-03-06 op return;
254 48e9d457 2021-03-06 op
255 48e9d457 2021-03-06 op l = nth_line(tab, --tab->s->line_off);
256 48e9d457 2021-03-06 op wscrl(body, -1);
257 48e9d457 2021-03-06 op wmove(body, 0, 0);
258 48e9d457 2021-03-06 op print_line(l);
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_down(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 size_t n;
267 48e9d457 2021-03-06 op
268 48e9d457 2021-03-06 op tab = current_tab();
269 48e9d457 2021-03-06 op
270 48e9d457 2021-03-06 op if (tab->s->line_max == 0 || tab->s->line_off == tab->s->line_max-1)
271 48e9d457 2021-03-06 op return;
272 48e9d457 2021-03-06 op
273 48e9d457 2021-03-06 op tab->s->line_off++;
274 48e9d457 2021-03-06 op wscrl(body, 1);
275 48e9d457 2021-03-06 op
276 48e9d457 2021-03-06 op if (tab->s->line_max - tab->s->line_off < body_lines)
277 48e9d457 2021-03-06 op return;
278 48e9d457 2021-03-06 op
279 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off + body_lines-1);
280 48e9d457 2021-03-06 op wmove(body, body_lines-1, 0);
281 48e9d457 2021-03-06 op print_line(l);
282 1d08c280 2021-03-06 op }
283 1d08c280 2021-03-06 op
284 1d08c280 2021-03-06 op static void
285 1d08c280 2021-03-06 op cmd_kill_telescope(int k)
286 1d08c280 2021-03-06 op {
287 1d08c280 2021-03-06 op event_loopbreak();
288 1d08c280 2021-03-06 op }
289 1d08c280 2021-03-06 op
290 1d08c280 2021-03-06 op static void
291 1d08c280 2021-03-06 op cmd_unbound(int k)
292 1d08c280 2021-03-06 op {
293 7953dd72 2021-03-07 op message("%c is undefined", k);
294 1d08c280 2021-03-06 op }
295 1d08c280 2021-03-06 op
296 48e9d457 2021-03-06 op static struct line *
297 48e9d457 2021-03-06 op nth_line(struct tab *tab, size_t n)
298 48e9d457 2021-03-06 op {
299 48e9d457 2021-03-06 op struct line *l;
300 48e9d457 2021-03-06 op size_t i;
301 48e9d457 2021-03-06 op
302 48e9d457 2021-03-06 op i = 0;
303 48e9d457 2021-03-06 op TAILQ_FOREACH(l, &tab->s->head, lines) {
304 48e9d457 2021-03-06 op if (i == n)
305 48e9d457 2021-03-06 op return l;
306 48e9d457 2021-03-06 op i++;
307 48e9d457 2021-03-06 op }
308 48e9d457 2021-03-06 op
309 48e9d457 2021-03-06 op /* unreachable */
310 48e9d457 2021-03-06 op abort();
311 48e9d457 2021-03-06 op }
312 48e9d457 2021-03-06 op
313 5e11c00c 2021-03-02 op static struct tab *
314 5e11c00c 2021-03-02 op current_tab(void)
315 5e11c00c 2021-03-02 op {
316 5e11c00c 2021-03-02 op struct tab *t;
317 5e11c00c 2021-03-02 op
318 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
319 5e11c00c 2021-03-02 op if (t->flags & TAB_CURRENT)
320 5e11c00c 2021-03-02 op return t;
321 5e11c00c 2021-03-02 op }
322 5e11c00c 2021-03-02 op
323 5e11c00c 2021-03-02 op /* unreachable */
324 5e11c00c 2021-03-02 op abort();
325 5e11c00c 2021-03-02 op }
326 5e11c00c 2021-03-02 op
327 5e11c00c 2021-03-02 op static void
328 5e11c00c 2021-03-02 op dispatch_stdio(int fd, short ev, void *d)
329 5e11c00c 2021-03-02 op {
330 1d08c280 2021-03-06 op struct binding *b;
331 1d08c280 2021-03-06 op int k;
332 5e11c00c 2021-03-02 op
333 48e9d457 2021-03-06 op k = wgetch(body);
334 5e11c00c 2021-03-02 op
335 1d08c280 2021-03-06 op if (k == ERR)
336 5e11c00c 2021-03-02 op return;
337 5e11c00c 2021-03-02 op
338 1d08c280 2021-03-06 op for (b = bindings; b->fn != NULL; ++b) {
339 1d08c280 2021-03-06 op if (k == b->key) {
340 1d08c280 2021-03-06 op b->fn(k);
341 1d08c280 2021-03-06 op goto done;
342 1d08c280 2021-03-06 op }
343 eb259e66 2021-03-02 op }
344 eb259e66 2021-03-02 op
345 1d08c280 2021-03-06 op cmd_unbound(k);
346 1d08c280 2021-03-06 op
347 1d08c280 2021-03-06 op done:
348 48e9d457 2021-03-06 op restore_cursor(current_tab());
349 48e9d457 2021-03-06 op wrefresh(tabline);
350 48e9d457 2021-03-06 op wrefresh(modeline);
351 48e9d457 2021-03-06 op wrefresh(minibuf);
352 a6d450c1 2021-03-06 op
353 a6d450c1 2021-03-06 op wrefresh(body);
354 a6d450c1 2021-03-06 op }
355 48e9d457 2021-03-06 op
356 a6d450c1 2021-03-06 op static void
357 a6d450c1 2021-03-06 op handle_clear_minibuf(int fd, short ev, void *d)
358 a6d450c1 2021-03-06 op {
359 a6d450c1 2021-03-06 op clminibufev_set = 0;
360 a6d450c1 2021-03-06 op werase(minibuf);
361 a6d450c1 2021-03-06 op wrefresh(minibuf);
362 48e9d457 2021-03-06 op wrefresh(body);
363 5e11c00c 2021-03-02 op }
364 5e11c00c 2021-03-02 op
365 5e11c00c 2021-03-02 op static void
366 5e11c00c 2021-03-02 op handle_resize(int sig, short ev, void *d)
367 5e11c00c 2021-03-02 op {
368 1d08c280 2021-03-06 op struct tab *tab;
369 1d08c280 2021-03-06 op
370 5e11c00c 2021-03-02 op endwin();
371 5e11c00c 2021-03-02 op refresh();
372 5e11c00c 2021-03-02 op clear();
373 5e11c00c 2021-03-02 op
374 48e9d457 2021-03-06 op /* move and resize the windows, in reverse order! */
375 48e9d457 2021-03-06 op
376 b1738d2e 2021-03-06 op mvwin(minibuf, LINES-1, 0);
377 48e9d457 2021-03-06 op wresize(minibuf, 1, COLS);
378 48e9d457 2021-03-06 op
379 48e9d457 2021-03-06 op mvwin(modeline, LINES-2, 0);
380 48e9d457 2021-03-06 op wresize(modeline, 1, COLS);
381 48e9d457 2021-03-06 op
382 48e9d457 2021-03-06 op wresize(body, LINES-3, COLS);
383 48e9d457 2021-03-06 op body_lines = LINES-3;
384 bd9637e9 2021-03-06 op body_cols = COLS;
385 48e9d457 2021-03-06 op
386 48e9d457 2021-03-06 op wresize(tabline, 1, COLS);
387 48e9d457 2021-03-06 op
388 1d08c280 2021-03-06 op tab = current_tab();
389 1d08c280 2021-03-06 op
390 1d08c280 2021-03-06 op wrap_page(tab);
391 1d08c280 2021-03-06 op redraw_tab(tab);
392 5e11c00c 2021-03-02 op }
393 5e11c00c 2021-03-02 op
394 eb259e66 2021-03-02 op /*
395 eb259e66 2021-03-02 op * Helper function for wrap_text. Find the end of the current word
396 eb259e66 2021-03-02 op * and the end of the separator after the word.
397 eb259e66 2021-03-02 op */
398 eb259e66 2021-03-02 op static int
399 eb259e66 2021-03-02 op word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
400 eb259e66 2021-03-02 op {
401 eb259e66 2021-03-02 op *endword = s;
402 eb259e66 2021-03-02 op *endword = s;
403 eb259e66 2021-03-02 op
404 eb259e66 2021-03-02 op if (*s == '\0')
405 eb259e66 2021-03-02 op return 0;
406 eb259e66 2021-03-02 op
407 eb259e66 2021-03-02 op /* find the end of the current world */
408 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
409 eb259e66 2021-03-02 op if (strchr(sep, *s) != NULL)
410 eb259e66 2021-03-02 op break;
411 eb259e66 2021-03-02 op }
412 eb259e66 2021-03-02 op
413 eb259e66 2021-03-02 op *endword = s;
414 eb259e66 2021-03-02 op
415 eb259e66 2021-03-02 op /* find the end of the separator */
416 eb259e66 2021-03-02 op for (; *s != '\0'; ++s) {
417 eb259e66 2021-03-02 op if (strchr(sep, *s) == NULL)
418 eb259e66 2021-03-02 op break;
419 eb259e66 2021-03-02 op }
420 eb259e66 2021-03-02 op
421 eb259e66 2021-03-02 op *endspc = s;
422 eb259e66 2021-03-02 op
423 eb259e66 2021-03-02 op return 1;
424 eb259e66 2021-03-02 op }
425 eb259e66 2021-03-02 op
426 1d08c280 2021-03-06 op static inline int
427 1d08c280 2021-03-06 op emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
428 1d08c280 2021-03-06 op const char **line)
429 eb259e66 2021-03-02 op {
430 1d08c280 2021-03-06 op if (!push_line(tab, l, *line, *off - zero))
431 1d08c280 2021-03-06 op return 0;
432 1d08c280 2021-03-06 op *line += *off - zero;
433 eb259e66 2021-03-02 op *off = zero;
434 1d08c280 2021-03-06 op return 1;
435 eb259e66 2021-03-02 op }
436 eb259e66 2021-03-02 op
437 eb259e66 2021-03-02 op static inline void
438 eb259e66 2021-03-02 op emitstr(const char **s, size_t len, size_t *off)
439 eb259e66 2021-03-02 op {
440 eb259e66 2021-03-02 op size_t i;
441 eb259e66 2021-03-02 op
442 eb259e66 2021-03-02 op /* printw("%*s", ...) doesn't seem to respect the precision, so... */
443 eb259e66 2021-03-02 op for (i = 0; i < len; ++i)
444 eb259e66 2021-03-02 op addch((*s)[i]);
445 eb259e66 2021-03-02 op *off += len;
446 eb259e66 2021-03-02 op *s += len;
447 eb259e66 2021-03-02 op }
448 eb259e66 2021-03-02 op
449 eb259e66 2021-03-02 op /*
450 1d08c280 2021-03-06 op * Build a list of visual line by wrapping the given line, assuming
451 1d08c280 2021-03-06 op * that when printed will have a leading prefix prfx.
452 eb259e66 2021-03-02 op *
453 eb259e66 2021-03-02 op * TODO: it considers each byte one cell on the screen!
454 eb259e66 2021-03-02 op */
455 5e11c00c 2021-03-02 op static void
456 1d08c280 2021-03-06 op wrap_text(struct tab *tab, const char *prfx, struct line *l)
457 eb259e66 2021-03-02 op {
458 eb259e66 2021-03-02 op size_t zero, off, len, split;
459 1d08c280 2021-03-06 op const char *endword, *endspc, *line, *linestart;
460 eb259e66 2021-03-02 op
461 1d08c280 2021-03-06 op zero = strlen(prfx);
462 eb259e66 2021-03-02 op off = zero;
463 1d08c280 2021-03-06 op line = l->line;
464 1d08c280 2021-03-06 op linestart = l->line;
465 eb259e66 2021-03-02 op
466 1d08c280 2021-03-06 op while (word_boundaries(line, " \t-", &endword, &endspc)) {
467 eb259e66 2021-03-02 op len = endword - line;
468 48e9d457 2021-03-06 op if (off + len >= body_cols) {
469 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
470 48e9d457 2021-03-06 op while (len >= body_cols) {
471 eb259e66 2021-03-02 op /* hard wrap */
472 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
473 48e9d457 2021-03-06 op len -= body_cols-1;
474 48e9d457 2021-03-06 op line += body_cols-1;
475 eb259e66 2021-03-02 op }
476 eb259e66 2021-03-02 op
477 eb259e66 2021-03-02 op if (len != 0)
478 1d08c280 2021-03-06 op off += len;
479 1d08c280 2021-03-06 op } else
480 1d08c280 2021-03-06 op off += len;
481 eb259e66 2021-03-02 op
482 eb259e66 2021-03-02 op /* print the spaces iff not at bol */
483 eb259e66 2021-03-02 op len = endspc - endword;
484 eb259e66 2021-03-02 op /* line = endspc; */
485 eb259e66 2021-03-02 op if (off != zero) {
486 48e9d457 2021-03-06 op if (off + len >= body_cols) {
487 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
488 1d08c280 2021-03-06 op linestart = endspc;
489 1d08c280 2021-03-06 op } else
490 1d08c280 2021-03-06 op off += len;
491 eb259e66 2021-03-02 op }
492 eb259e66 2021-03-02 op
493 eb259e66 2021-03-02 op line = endspc;
494 eb259e66 2021-03-02 op }
495 eb259e66 2021-03-02 op
496 1d08c280 2021-03-06 op emitline(tab, zero, &off, l, &linestart);
497 eb259e66 2021-03-02 op }
498 eb259e66 2021-03-02 op
499 1d08c280 2021-03-06 op static int
500 1d08c280 2021-03-06 op hardwrap_text(struct tab *tab, struct line *l)
501 5e11c00c 2021-03-02 op {
502 1d08c280 2021-03-06 op size_t off, len;
503 1d08c280 2021-03-06 op const char *linestart;
504 5e11c00c 2021-03-02 op
505 1d08c280 2021-03-06 op len = strlen(l->line);
506 1d08c280 2021-03-06 op off = 0;
507 1d08c280 2021-03-06 op linestart = l->line;
508 1d08c280 2021-03-06 op
509 1d08c280 2021-03-06 op while (len >= COLS) {
510 1d08c280 2021-03-06 op len -= COLS-1;
511 1d08c280 2021-03-06 op off = COLS-1;
512 1d08c280 2021-03-06 op if (!emitline(tab, 0, &off, l, &linestart))
513 1d08c280 2021-03-06 op return 0;
514 1d08c280 2021-03-06 op }
515 1d08c280 2021-03-06 op
516 1d08c280 2021-03-06 op return 1;
517 1d08c280 2021-03-06 op }
518 5e11c00c 2021-03-02 op
519 1d08c280 2021-03-06 op static int
520 1d08c280 2021-03-06 op wrap_page(struct tab *tab)
521 1d08c280 2021-03-06 op {
522 1d08c280 2021-03-06 op struct line *l;
523 1d08c280 2021-03-06 op
524 1d08c280 2021-03-06 op empty_vlist(tab);
525 1d08c280 2021-03-06 op
526 5e11c00c 2021-03-02 op TAILQ_FOREACH(l, &tab->page.head, lines) {
527 5e11c00c 2021-03-02 op switch (l->type) {
528 5e11c00c 2021-03-02 op case LINE_TEXT:
529 1d08c280 2021-03-06 op wrap_text(tab, "", l);
530 5e11c00c 2021-03-02 op break;
531 5e11c00c 2021-03-02 op case LINE_LINK:
532 1d08c280 2021-03-06 op wrap_text(tab, "=> ", l);
533 5e11c00c 2021-03-02 op break;
534 5e11c00c 2021-03-02 op case LINE_TITLE_1:
535 1d08c280 2021-03-06 op wrap_text(tab, "# ", l);
536 5e11c00c 2021-03-02 op break;
537 5e11c00c 2021-03-02 op case LINE_TITLE_2:
538 1d08c280 2021-03-06 op wrap_text(tab, "## ", l);
539 5e11c00c 2021-03-02 op break;
540 5e11c00c 2021-03-02 op case LINE_TITLE_3:
541 1d08c280 2021-03-06 op wrap_text(tab, "### ", l);
542 5e11c00c 2021-03-02 op break;
543 5e11c00c 2021-03-02 op case LINE_ITEM:
544 1d08c280 2021-03-06 op wrap_text(tab, "* ", l);
545 5e11c00c 2021-03-02 op break;
546 5e11c00c 2021-03-02 op case LINE_QUOTE:
547 1d08c280 2021-03-06 op wrap_text(tab, "> ", l);
548 5e11c00c 2021-03-02 op break;
549 5e11c00c 2021-03-02 op case LINE_PRE_START:
550 5e11c00c 2021-03-02 op case LINE_PRE_END:
551 1d08c280 2021-03-06 op push_line(tab, l, NULL, 0);
552 5e11c00c 2021-03-02 op break;
553 5e11c00c 2021-03-02 op case LINE_PRE_CONTENT:
554 1d08c280 2021-03-06 op hardwrap_text(tab, l);
555 5e11c00c 2021-03-02 op break;
556 5e11c00c 2021-03-02 op }
557 5e11c00c 2021-03-02 op }
558 1d08c280 2021-03-06 op return 1;
559 1d08c280 2021-03-06 op }
560 5e11c00c 2021-03-02 op
561 1d08c280 2021-03-06 op static inline void
562 1d08c280 2021-03-06 op print_line(struct line *l)
563 1d08c280 2021-03-06 op {
564 bd9637e9 2021-03-06 op const char *text = l->line;
565 bd9637e9 2021-03-06 op
566 bd9637e9 2021-03-06 op if (text == NULL)
567 bd9637e9 2021-03-06 op text = "";
568 bd9637e9 2021-03-06 op
569 1d08c280 2021-03-06 op switch (l->type) {
570 1d08c280 2021-03-06 op case LINE_TEXT:
571 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
572 1d08c280 2021-03-06 op break;
573 1d08c280 2021-03-06 op case LINE_LINK:
574 bd9637e9 2021-03-06 op wattron(body, A_UNDERLINE);
575 bd9637e9 2021-03-06 op wprintw(body, "=> %s", text);
576 bd9637e9 2021-03-06 op wattroff(body, A_UNDERLINE);
577 bd9637e9 2021-03-06 op return;
578 1d08c280 2021-03-06 op case LINE_TITLE_1:
579 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
580 bd9637e9 2021-03-06 op wprintw(body, "# %s", text);
581 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
582 bd9637e9 2021-03-06 op return;
583 1d08c280 2021-03-06 op case LINE_TITLE_2:
584 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
585 bd9637e9 2021-03-06 op wprintw(body, "## %s", text);
586 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
587 bd9637e9 2021-03-06 op return;
588 bd9637e9 2021-03-06 op case LINE_TITLE_3:
589 bd9637e9 2021-03-06 op wattron(body, A_BOLD);
590 bd9637e9 2021-03-06 op wprintw(body, "### %s", text);
591 bd9637e9 2021-03-06 op wattroff(body, A_BOLD);
592 bd9637e9 2021-03-06 op return;
593 bd9637e9 2021-03-06 op case LINE_ITEM:
594 bd9637e9 2021-03-06 op wprintw(body, "* %s", text);
595 bd9637e9 2021-03-06 op return;
596 1d08c280 2021-03-06 op case LINE_QUOTE:
597 bd9637e9 2021-03-06 op wattron(body, A_DIM);
598 bd9637e9 2021-03-06 op wprintw(body, "> %s", text);
599 bd9637e9 2021-03-06 op wattroff(body, A_DIM);
600 bd9637e9 2021-03-06 op return;
601 1d08c280 2021-03-06 op case LINE_PRE_START:
602 1d08c280 2021-03-06 op case LINE_PRE_END:
603 48e9d457 2021-03-06 op wprintw(body, "```");
604 bd9637e9 2021-03-06 op return;
605 1d08c280 2021-03-06 op case LINE_PRE_CONTENT:
606 bd9637e9 2021-03-06 op wprintw(body, "%s", text);
607 bd9637e9 2021-03-06 op return;
608 1d08c280 2021-03-06 op }
609 1d08c280 2021-03-06 op }
610 1d08c280 2021-03-06 op
611 1d08c280 2021-03-06 op static void
612 48e9d457 2021-03-06 op redraw_modeline(struct tab *tab)
613 1d08c280 2021-03-06 op {
614 48e9d457 2021-03-06 op int x, y, max_x, max_y;
615 48e9d457 2021-03-06 op const char *url = "TODO:url";
616 48e9d457 2021-03-06 op const char *mode = "text/gemini-mode";
617 1d08c280 2021-03-06 op
618 48e9d457 2021-03-06 op wclear(modeline);
619 48e9d457 2021-03-06 op wattron(modeline, A_REVERSE);
620 48e9d457 2021-03-06 op wmove(modeline, 0, 0);
621 1d08c280 2021-03-06 op
622 48e9d457 2021-03-06 op wprintw(modeline, "-- %s %s ", mode, url);
623 48e9d457 2021-03-06 op getyx(modeline, y, x);
624 48e9d457 2021-03-06 op getmaxyx(modeline, max_y, max_x);
625 48e9d457 2021-03-06 op
626 48e9d457 2021-03-06 op (void)y;
627 48e9d457 2021-03-06 op (void)max_y;
628 48e9d457 2021-03-06 op
629 48e9d457 2021-03-06 op for (; x < max_x; ++x)
630 48e9d457 2021-03-06 op waddstr(modeline, "-");
631 48e9d457 2021-03-06 op }
632 48e9d457 2021-03-06 op
633 48e9d457 2021-03-06 op static void
634 48e9d457 2021-03-06 op redraw_tab(struct tab *tab)
635 48e9d457 2021-03-06 op {
636 48e9d457 2021-03-06 op struct line *l;
637 48e9d457 2021-03-06 op int line;
638 48e9d457 2021-03-06 op
639 48e9d457 2021-03-06 op werase(body);
640 48e9d457 2021-03-06 op
641 48e9d457 2021-03-06 op tab->s->line_off = MIN(tab->s->line_max, tab->s->line_off);
642 48e9d457 2021-03-06 op if (TAILQ_EMPTY(&tab->s->head))
643 48e9d457 2021-03-06 op return;
644 48e9d457 2021-03-06 op
645 48e9d457 2021-03-06 op line = 0;
646 48e9d457 2021-03-06 op l = nth_line(tab, tab->s->line_off);
647 48e9d457 2021-03-06 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
648 48e9d457 2021-03-06 op wmove(body, line, 0);
649 1d08c280 2021-03-06 op print_line(l);
650 48e9d457 2021-03-06 op line++;
651 48e9d457 2021-03-06 op if (line == body_lines)
652 48e9d457 2021-03-06 op break;
653 1d08c280 2021-03-06 op }
654 1d08c280 2021-03-06 op
655 48e9d457 2021-03-06 op redraw_modeline(tab);
656 48e9d457 2021-03-06 op
657 48e9d457 2021-03-06 op restore_cursor(tab);
658 48e9d457 2021-03-06 op wrefresh(tabline);
659 48e9d457 2021-03-06 op wrefresh(modeline);
660 48e9d457 2021-03-06 op wrefresh(minibuf);
661 7953dd72 2021-03-07 op
662 7953dd72 2021-03-07 op wrefresh(body);
663 7953dd72 2021-03-07 op }
664 7953dd72 2021-03-07 op
665 7953dd72 2021-03-07 op static void
666 7953dd72 2021-03-07 op message(const char *fmt, ...)
667 7953dd72 2021-03-07 op {
668 7953dd72 2021-03-07 op va_list ap;
669 48e9d457 2021-03-06 op
670 7953dd72 2021-03-07 op va_start(ap, fmt);
671 7953dd72 2021-03-07 op
672 7953dd72 2021-03-07 op if (clminibufev_set)
673 7953dd72 2021-03-07 op evtimer_del(&clminibufev);
674 7953dd72 2021-03-07 op evtimer_set(&clminibufev, handle_clear_minibuf, NULL);
675 7953dd72 2021-03-07 op evtimer_add(&clminibufev, &clminibufev_timer);
676 7953dd72 2021-03-07 op clminibufev_set = 1;
677 7953dd72 2021-03-07 op
678 7953dd72 2021-03-07 op werase(minibuf);
679 7953dd72 2021-03-07 op vw_printw(minibuf, fmt, ap);
680 7953dd72 2021-03-07 op
681 7953dd72 2021-03-07 op wrefresh(minibuf);
682 48e9d457 2021-03-06 op wrefresh(body);
683 7953dd72 2021-03-07 op
684 7953dd72 2021-03-07 op va_end(ap);
685 5e11c00c 2021-03-02 op }
686 5e11c00c 2021-03-02 op
687 5e11c00c 2021-03-02 op int
688 5e11c00c 2021-03-02 op ui_init(void)
689 5e11c00c 2021-03-02 op {
690 5e11c00c 2021-03-02 op setlocale(LC_ALL, "");
691 5e11c00c 2021-03-02 op
692 5e11c00c 2021-03-02 op initscr();
693 15e1b108 2021-03-02 op raw();
694 5e11c00c 2021-03-02 op noecho();
695 5e11c00c 2021-03-02 op
696 5e11c00c 2021-03-02 op nonl();
697 5e11c00c 2021-03-02 op intrflush(stdscr, FALSE);
698 5e11c00c 2021-03-02 op keypad(stdscr, TRUE);
699 5e11c00c 2021-03-02 op
700 48e9d457 2021-03-06 op if ((tabline = newwin(1, COLS, 0, 0)) == NULL)
701 48e9d457 2021-03-06 op return 0;
702 48e9d457 2021-03-06 op if ((body = newwin(LINES - 3, COLS, 1, 0)) == NULL)
703 48e9d457 2021-03-06 op return 0;
704 48e9d457 2021-03-06 op if ((modeline = newwin(1, COLS, LINES-2, 0)) == NULL)
705 48e9d457 2021-03-06 op return 0;
706 48e9d457 2021-03-06 op if ((minibuf = newwin(1, COLS, LINES-1, 0)) == NULL)
707 48e9d457 2021-03-06 op return 0;
708 1d08c280 2021-03-06 op
709 48e9d457 2021-03-06 op body_lines = LINES-3;
710 48e9d457 2021-03-06 op body_cols = COLS;
711 48e9d457 2021-03-06 op
712 48e9d457 2021-03-06 op scrollok(body, TRUE);
713 48e9d457 2021-03-06 op
714 5e11c00c 2021-03-02 op /* non-blocking input */
715 48e9d457 2021-03-06 op wtimeout(body, 0);
716 5e11c00c 2021-03-02 op
717 48e9d457 2021-03-06 op mvwprintw(body, 0, 0, "");
718 5e11c00c 2021-03-02 op
719 5e11c00c 2021-03-02 op event_set(&stdioev, 0, EV_READ | EV_PERSIST, dispatch_stdio, NULL);
720 5e11c00c 2021-03-02 op event_add(&stdioev, NULL);
721 5e11c00c 2021-03-02 op
722 5e11c00c 2021-03-02 op signal_set(&winchev, SIGWINCH, handle_resize, NULL);
723 5e11c00c 2021-03-02 op signal_add(&winchev, NULL);
724 5e11c00c 2021-03-02 op
725 5e11c00c 2021-03-02 op return 1;
726 5e11c00c 2021-03-02 op }
727 5e11c00c 2021-03-02 op
728 1d08c280 2021-03-06 op int
729 5e11c00c 2021-03-02 op ui_on_new_tab(struct tab *tab)
730 5e11c00c 2021-03-02 op {
731 5e11c00c 2021-03-02 op struct tab *t;
732 5e11c00c 2021-03-02 op
733 1d08c280 2021-03-06 op if ((tab->s = calloc(1, sizeof(*t->s))) == NULL)
734 1d08c280 2021-03-06 op return 0;
735 1d08c280 2021-03-06 op
736 1d08c280 2021-03-06 op TAILQ_INIT(&tab->s->head);
737 1d08c280 2021-03-06 op
738 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
739 5e11c00c 2021-03-02 op t->flags &= ~TAB_CURRENT;
740 5e11c00c 2021-03-02 op }
741 5e11c00c 2021-03-02 op tab->flags = TAB_CURRENT;
742 5e11c00c 2021-03-02 op
743 5e11c00c 2021-03-02 op /* TODO: redraw the tab list */
744 1d08c280 2021-03-06 op /* TODO: switch to the new tab */
745 1d08c280 2021-03-06 op
746 48e9d457 2021-03-06 op wmove(body, 0, 0);
747 1d08c280 2021-03-06 op
748 1d08c280 2021-03-06 op return 1;
749 5e11c00c 2021-03-02 op }
750 5e11c00c 2021-03-02 op
751 5e11c00c 2021-03-02 op void
752 5e11c00c 2021-03-02 op ui_on_tab_refresh(struct tab *tab)
753 5e11c00c 2021-03-02 op {
754 5e11c00c 2021-03-02 op if (!(tab->flags & TAB_CURRENT))
755 5e11c00c 2021-03-02 op return;
756 5e11c00c 2021-03-02 op
757 1d08c280 2021-03-06 op wrap_page(tab);
758 5e11c00c 2021-03-02 op redraw_tab(tab);
759 5e11c00c 2021-03-02 op }
760 5e11c00c 2021-03-02 op
761 5e11c00c 2021-03-02 op void
762 5e11c00c 2021-03-02 op ui_end(void)
763 5e11c00c 2021-03-02 op {
764 5e11c00c 2021-03-02 op endwin();
765 5e11c00c 2021-03-02 op }