Blame


1 65d9b3ca 2021-03-14 op /*
2 65d9b3ca 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 65d9b3ca 2021-03-14 op *
4 65d9b3ca 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 65d9b3ca 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 65d9b3ca 2021-03-14 op * copyright notice and this permission notice appear in all copies.
7 65d9b3ca 2021-03-14 op *
8 65d9b3ca 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 65d9b3ca 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 65d9b3ca 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 65d9b3ca 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 65d9b3ca 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 65d9b3ca 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 65d9b3ca 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 65d9b3ca 2021-03-14 op */
16 786e6deb 2021-07-21 op
17 786e6deb 2021-07-21 op #include "compat.h"
18 65d9b3ca 2021-03-14 op
19 a0507108 2021-07-18 op #include <ctype.h>
20 65d9b3ca 2021-03-14 op #include <stdlib.h>
21 65d9b3ca 2021-03-14 op #include <string.h>
22 65d9b3ca 2021-03-14 op
23 88693f61 2021-07-15 op #include "defaults.h"
24 5caf7d67 2021-07-12 op #include "telescope.h"
25 5caf7d67 2021-07-12 op #include "utf8.h"
26 5caf7d67 2021-07-12 op
27 65d9b3ca 2021-03-14 op /*
28 65d9b3ca 2021-03-14 op * Text wrapping
29 65d9b3ca 2021-03-14 op * =============
30 65d9b3ca 2021-03-14 op *
31 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
32 65d9b3ca 2021-03-14 op *
33 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
34 65d9b3ca 2021-03-14 op * a. hard wrap.
35 65d9b3ca 2021-03-14 op * b. repeat
36 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
37 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
38 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
39 65d9b3ca 2021-03-14 op * 4. repeat
40 65d9b3ca 2021-03-14 op *
41 65d9b3ca 2021-03-14 op */
42 65d9b3ca 2021-03-14 op
43 ca5da938 2021-04-01 op void
44 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
45 bca92a4c 2021-07-01 op {
46 bca92a4c 2021-07-01 op empty_vlist(buffer);
47 bca92a4c 2021-07-01 op empty_linelist(buffer);
48 bca92a4c 2021-07-01 op }
49 bca92a4c 2021-07-01 op
50 bca92a4c 2021-07-01 op void
51 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
52 ca5da938 2021-04-01 op {
53 ca5da938 2021-04-01 op struct line *l, *lt;
54 ca5da938 2021-04-01 op
55 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
56 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
57 ca5da938 2021-04-01 op free(l->line);
58 b3be07ea 2021-07-18 op
59 b3be07ea 2021-07-18 op if (l->type != LINE_COMPL &&
60 c6be26e4 2021-07-21 op l->type != LINE_COMPL_CURRENT &&
61 c6be26e4 2021-07-21 op l->type != LINE_HELP)
62 b3be07ea 2021-07-18 op free(l->alt);
63 b3be07ea 2021-07-18 op
64 ca5da938 2021-04-01 op free(l);
65 ca5da938 2021-04-01 op }
66 ca5da938 2021-04-01 op }
67 ca5da938 2021-04-01 op
68 ca5da938 2021-04-01 op void
69 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
70 ca5da938 2021-04-01 op {
71 ca5da938 2021-04-01 op struct vline *vl, *t;
72 ca5da938 2021-04-01 op
73 4ea9160a 2021-07-01 op buffer->top_line = NULL;
74 4ea9160a 2021-07-01 op buffer->line_off = 0;
75 46f6e974 2021-05-17 op buffer->current_line = NULL;
76 46f6e974 2021-05-17 op buffer->line_max = 0;
77 ca5da938 2021-04-01 op
78 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
79 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
80 ca5da938 2021-04-01 op free(vl->line);
81 ca5da938 2021-04-01 op free(vl);
82 ca5da938 2021-04-01 op }
83 ca5da938 2021-04-01 op }
84 ca5da938 2021-04-01 op
85 65d9b3ca 2021-03-14 op static int
86 87f7918d 2021-07-14 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
87 65d9b3ca 2021-03-14 op {
88 65d9b3ca 2021-03-14 op struct vline *vl;
89 a0507108 2021-07-18 op const char *end;
90 65d9b3ca 2021-03-14 op
91 a0507108 2021-07-18 op /* omit trailing spaces */
92 a0507108 2021-07-18 op if (len != 0) {
93 a0507108 2021-07-18 op for (end = buf + len - 1;
94 a0507108 2021-07-18 op end > buf && isspace(*end);
95 a0507108 2021-07-18 op end--, len--)
96 a0507108 2021-07-18 op ; /* nop */
97 a0507108 2021-07-18 op }
98 a0507108 2021-07-18 op
99 6e6c6afb 2021-07-14 op if (!(l->flags & L_HIDDEN))
100 6e6c6afb 2021-07-14 op buffer->line_max++;
101 65d9b3ca 2021-03-14 op
102 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
103 65d9b3ca 2021-03-14 op return 0;
104 65d9b3ca 2021-03-14 op
105 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
106 65d9b3ca 2021-03-14 op free(vl);
107 65d9b3ca 2021-03-14 op return 0;
108 65d9b3ca 2021-03-14 op }
109 65d9b3ca 2021-03-14 op
110 65d9b3ca 2021-03-14 op vl->parent = l;
111 65d9b3ca 2021-03-14 op if (len != 0)
112 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
113 5492b68f 2021-07-05 op vl->flags = flags;
114 65d9b3ca 2021-03-14 op
115 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
116 65d9b3ca 2021-03-14 op return 1;
117 65d9b3ca 2021-03-14 op }
118 65d9b3ca 2021-03-14 op
119 65d9b3ca 2021-03-14 op /*
120 e7b982f4 2021-07-14 op * Similar to wrap_text, but emit only o vline.
121 e7b982f4 2021-07-14 op */
122 e7b982f4 2021-07-14 op int
123 e7b982f4 2021-07-14 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
124 e7b982f4 2021-07-14 op {
125 e7b982f4 2021-07-14 op struct vline *vl, *t;
126 e7b982f4 2021-07-14 op
127 e7b982f4 2021-07-14 op /*
128 e7b982f4 2021-07-14 op * be lazy: call wrap_text and then discard the continuations.
129 e7b982f4 2021-07-14 op */
130 e7b982f4 2021-07-14 op
131 e7b982f4 2021-07-14 op if (!wrap_text(buffer, prfx, l, width))
132 e7b982f4 2021-07-14 op return 0;
133 e7b982f4 2021-07-14 op
134 e7b982f4 2021-07-14 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
135 e7b982f4 2021-07-14 op if (vl->flags & L_CONTINUATION) {
136 e7b982f4 2021-07-14 op TAILQ_REMOVE(&buffer->head, vl, vlines);
137 e7b982f4 2021-07-14 op free(vl->line);
138 e7b982f4 2021-07-14 op free(vl);
139 6eddfe3c 2021-07-15 op buffer->line_max--;
140 e7b982f4 2021-07-14 op }
141 e7b982f4 2021-07-14 op }
142 e7b982f4 2021-07-14 op
143 e7b982f4 2021-07-14 op return 1;
144 e7b982f4 2021-07-14 op }
145 e7b982f4 2021-07-14 op
146 e7b982f4 2021-07-14 op /*
147 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
148 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
149 65d9b3ca 2021-03-14 op */
150 676fba6f 2021-03-21 op int
151 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
152 65d9b3ca 2021-03-14 op {
153 676fba6f 2021-03-21 op const char *separators = " \t-";
154 218ac7f6 2021-07-16 op const char *start, *end, *line, *lastsep, *lastchar, *space;
155 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
156 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
157 5492b68f 2021-07-05 op int flags;
158 65d9b3ca 2021-03-14 op
159 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
160 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
161 65d9b3ca 2021-03-14 op
162 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
163 676fba6f 2021-03-21 op cur = prfxwidth;
164 676fba6f 2021-03-21 op start = line;
165 676fba6f 2021-03-21 op lastsep = NULL;
166 676fba6f 2021-03-21 op lastchar = line;
167 5492b68f 2021-07-05 op flags = 0;
168 218ac7f6 2021-07-16 op
169 218ac7f6 2021-07-16 op if (l->type == LINE_LINK && emojify_link &&
170 218ac7f6 2021-07-16 op emojied_line(l->line, &space)) {
171 218ac7f6 2021-07-16 op prfxwidth = utf8_swidth_between(l->line, space);
172 218ac7f6 2021-07-16 op cur = prfxwidth;
173 218ac7f6 2021-07-16 op line = space + 1;
174 218ac7f6 2021-07-16 op }
175 218ac7f6 2021-07-16 op
176 676fba6f 2021-03-21 op for (; *line; line++) {
177 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
178 676fba6f 2021-03-21 op continue;
179 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
180 1247b8cc 2021-07-16 op if (cur + w > width) {
181 676fba6f 2021-03-21 op end = lastsep == NULL
182 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
183 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
184 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
185 676fba6f 2021-03-21 op return 0;
186 963c680c 2021-07-05 op flags = L_CONTINUATION;
187 676fba6f 2021-03-21 op start = end;
188 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
189 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
190 5b010818 2021-07-05 op lastsep = line;
191 65d9b3ca 2021-03-14 op }
192 65d9b3ca 2021-03-14 op
193 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
194 676fba6f 2021-03-21 op cur += w;
195 65d9b3ca 2021-03-14 op }
196 65d9b3ca 2021-03-14 op
197 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
198 65d9b3ca 2021-03-14 op }
199 65d9b3ca 2021-03-14 op
200 65d9b3ca 2021-03-14 op int
201 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
202 65d9b3ca 2021-03-14 op {
203 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
204 65d9b3ca 2021-03-14 op int cont;
205 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
206 676fba6f 2021-03-21 op size_t cur, w;
207 65d9b3ca 2021-03-14 op
208 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
209 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
210 166712b4 2021-03-14 op
211 676fba6f 2021-03-21 op start = line;
212 676fba6f 2021-03-21 op lastchar = line;
213 676fba6f 2021-03-21 op cont = 0;
214 676fba6f 2021-03-21 op cur = 0;
215 676fba6f 2021-03-21 op for (; *line; line++) {
216 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
217 676fba6f 2021-03-21 op continue;
218 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
219 ea00395a 2021-08-24 op if (cur + w > width) {
220 cf8e0783 2021-08-24 op if (!push_line(buffer, l, start, lastchar-start, cont))
221 676fba6f 2021-03-21 op return 0;
222 88693f61 2021-07-15 op cont = L_CONTINUATION;
223 88693f61 2021-07-15 op if (dont_wrap_pre)
224 95a8c791 2021-08-26 op return 1;
225 676fba6f 2021-03-21 op cur = 0;
226 676fba6f 2021-03-21 op start = lastchar;
227 676fba6f 2021-03-21 op }
228 65d9b3ca 2021-03-14 op
229 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
230 676fba6f 2021-03-21 op cur += w;
231 65d9b3ca 2021-03-14 op }
232 65d9b3ca 2021-03-14 op
233 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
234 de278567 2021-07-21 op }
235 de278567 2021-07-21 op
236 de278567 2021-07-21 op int
237 de278567 2021-07-21 op wrap_page(struct buffer *buffer, int width)
238 de278567 2021-07-21 op {
239 de278567 2021-07-21 op struct line *l;
240 de278567 2021-07-21 op const struct line *top_orig, *orig;
241 de278567 2021-07-21 op struct vline *vl;
242 de278567 2021-07-21 op const char *prfx;
243 de278567 2021-07-21 op
244 de278567 2021-07-21 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
245 de278567 2021-07-21 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
246 de278567 2021-07-21 op
247 de278567 2021-07-21 op buffer->top_line = NULL;
248 de278567 2021-07-21 op buffer->current_line = NULL;
249 de278567 2021-07-21 op
250 de278567 2021-07-21 op buffer->force_redraw = 1;
251 de278567 2021-07-21 op buffer->curs_y = 0;
252 de278567 2021-07-21 op buffer->line_off = 0;
253 de278567 2021-07-21 op
254 de278567 2021-07-21 op empty_vlist(buffer);
255 de278567 2021-07-21 op
256 de278567 2021-07-21 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
257 de278567 2021-07-21 op prfx = line_prefixes[l->type].prfx1;
258 de278567 2021-07-21 op switch (l->type) {
259 de278567 2021-07-21 op case LINE_TEXT:
260 de278567 2021-07-21 op case LINE_LINK:
261 de278567 2021-07-21 op case LINE_TITLE_1:
262 de278567 2021-07-21 op case LINE_TITLE_2:
263 de278567 2021-07-21 op case LINE_TITLE_3:
264 de278567 2021-07-21 op case LINE_ITEM:
265 de278567 2021-07-21 op case LINE_QUOTE:
266 de278567 2021-07-21 op case LINE_PRE_START:
267 de278567 2021-07-21 op case LINE_PRE_END:
268 de278567 2021-07-21 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
269 de278567 2021-07-21 op break;
270 de278567 2021-07-21 op case LINE_PRE_CONTENT:
271 ba49fdf9 2021-07-30 op case LINE_PATCH:
272 ba49fdf9 2021-07-30 op case LINE_PATCH_HDR:
273 ba49fdf9 2021-07-30 op case LINE_PATCH_HUNK_HDR:
274 ba49fdf9 2021-07-30 op case LINE_PATCH_ADD:
275 ba49fdf9 2021-07-30 op case LINE_PATCH_DEL:
276 f3c7d960 2021-08-24 op hardwrap_text(buffer, l, MIN(fill_column, width));
277 de278567 2021-07-21 op break;
278 de278567 2021-07-21 op case LINE_COMPL:
279 de278567 2021-07-21 op case LINE_COMPL_CURRENT:
280 c6be26e4 2021-07-21 op case LINE_HELP:
281 1577540c 2021-11-05 op case LINE_DOWNLOAD:
282 1577540c 2021-11-05 op case LINE_DOWNLOAD_DONE:
283 1577540c 2021-11-05 op case LINE_DOWNLOAD_INFO:
284 de278567 2021-07-21 op wrap_one(buffer, prfx, l, width);
285 de278567 2021-07-21 op break;
286 0aef305d 2022-01-10 op case LINE_FRINGE:
287 0aef305d 2022-01-10 op /* never, ever wrapped */
288 0aef305d 2022-01-10 op break;
289 de278567 2021-07-21 op }
290 de278567 2021-07-21 op
291 de278567 2021-07-21 op if (top_orig == l && buffer->top_line == NULL) {
292 de278567 2021-07-21 op buffer->line_off = buffer->line_max-1;
293 de278567 2021-07-21 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
294 de278567 2021-07-21 op
295 de278567 2021-07-21 op while (1) {
296 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
297 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
298 de278567 2021-07-21 op break;
299 de278567 2021-07-21 op buffer->top_line = vl;
300 de278567 2021-07-21 op buffer->line_off--;
301 de278567 2021-07-21 op }
302 de278567 2021-07-21 op }
303 de278567 2021-07-21 op
304 de278567 2021-07-21 op if (orig == l && buffer->current_line == NULL) {
305 de278567 2021-07-21 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
306 de278567 2021-07-21 op
307 de278567 2021-07-21 op while (1) {
308 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
309 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
310 de278567 2021-07-21 op break;
311 de278567 2021-07-21 op buffer->current_line = vl;
312 de278567 2021-07-21 op }
313 de278567 2021-07-21 op }
314 de278567 2021-07-21 op }
315 de278567 2021-07-21 op
316 de278567 2021-07-21 op if (buffer->current_line == NULL)
317 de278567 2021-07-21 op buffer->current_line = TAILQ_FIRST(&buffer->head);
318 de278567 2021-07-21 op
319 de278567 2021-07-21 op if (buffer->top_line == NULL)
320 de278567 2021-07-21 op buffer->top_line = buffer->current_line;
321 de278567 2021-07-21 op
322 de278567 2021-07-21 op return 1;
323 65d9b3ca 2021-03-14 op }