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