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 b3be07ea 2021-07-18 op l->type != LINE_COMPL_CURRENT)
61 b3be07ea 2021-07-18 op free(l->alt);
62 b3be07ea 2021-07-18 op
63 ca5da938 2021-04-01 op free(l);
64 ca5da938 2021-04-01 op }
65 ca5da938 2021-04-01 op }
66 ca5da938 2021-04-01 op
67 ca5da938 2021-04-01 op void
68 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
69 ca5da938 2021-04-01 op {
70 ca5da938 2021-04-01 op struct vline *vl, *t;
71 ca5da938 2021-04-01 op
72 4ea9160a 2021-07-01 op buffer->top_line = NULL;
73 4ea9160a 2021-07-01 op buffer->line_off = 0;
74 46f6e974 2021-05-17 op buffer->current_line = NULL;
75 46f6e974 2021-05-17 op buffer->line_max = 0;
76 ca5da938 2021-04-01 op
77 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
78 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
79 ca5da938 2021-04-01 op free(vl->line);
80 ca5da938 2021-04-01 op free(vl);
81 ca5da938 2021-04-01 op }
82 ca5da938 2021-04-01 op }
83 ca5da938 2021-04-01 op
84 65d9b3ca 2021-03-14 op static int
85 87f7918d 2021-07-14 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
86 65d9b3ca 2021-03-14 op {
87 65d9b3ca 2021-03-14 op struct vline *vl;
88 a0507108 2021-07-18 op const char *end;
89 65d9b3ca 2021-03-14 op
90 a0507108 2021-07-18 op /* omit trailing spaces */
91 a0507108 2021-07-18 op if (len != 0) {
92 a0507108 2021-07-18 op for (end = buf + len - 1;
93 a0507108 2021-07-18 op end > buf && isspace(*end);
94 a0507108 2021-07-18 op end--, len--)
95 a0507108 2021-07-18 op ; /* nop */
96 a0507108 2021-07-18 op }
97 a0507108 2021-07-18 op
98 6e6c6afb 2021-07-14 op if (!(l->flags & L_HIDDEN))
99 6e6c6afb 2021-07-14 op buffer->line_max++;
100 65d9b3ca 2021-03-14 op
101 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
102 65d9b3ca 2021-03-14 op return 0;
103 65d9b3ca 2021-03-14 op
104 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
105 65d9b3ca 2021-03-14 op free(vl);
106 65d9b3ca 2021-03-14 op return 0;
107 65d9b3ca 2021-03-14 op }
108 65d9b3ca 2021-03-14 op
109 65d9b3ca 2021-03-14 op vl->parent = l;
110 65d9b3ca 2021-03-14 op if (len != 0)
111 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
112 5492b68f 2021-07-05 op vl->flags = flags;
113 65d9b3ca 2021-03-14 op
114 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
115 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
116 65d9b3ca 2021-03-14 op else
117 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
118 65d9b3ca 2021-03-14 op return 1;
119 65d9b3ca 2021-03-14 op }
120 65d9b3ca 2021-03-14 op
121 65d9b3ca 2021-03-14 op /*
122 e7b982f4 2021-07-14 op * Similar to wrap_text, but emit only o vline.
123 e7b982f4 2021-07-14 op */
124 e7b982f4 2021-07-14 op int
125 e7b982f4 2021-07-14 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
126 e7b982f4 2021-07-14 op {
127 e7b982f4 2021-07-14 op struct vline *vl, *t;
128 e7b982f4 2021-07-14 op
129 e7b982f4 2021-07-14 op /*
130 e7b982f4 2021-07-14 op * be lazy: call wrap_text and then discard the continuations.
131 e7b982f4 2021-07-14 op */
132 e7b982f4 2021-07-14 op
133 e7b982f4 2021-07-14 op if (!wrap_text(buffer, prfx, l, width))
134 e7b982f4 2021-07-14 op return 0;
135 e7b982f4 2021-07-14 op
136 e7b982f4 2021-07-14 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
137 e7b982f4 2021-07-14 op if (vl->flags & L_CONTINUATION) {
138 e7b982f4 2021-07-14 op TAILQ_REMOVE(&buffer->head, vl, vlines);
139 e7b982f4 2021-07-14 op free(vl->line);
140 e7b982f4 2021-07-14 op free(vl);
141 6eddfe3c 2021-07-15 op buffer->line_max--;
142 e7b982f4 2021-07-14 op }
143 e7b982f4 2021-07-14 op }
144 e7b982f4 2021-07-14 op
145 e7b982f4 2021-07-14 op return 1;
146 e7b982f4 2021-07-14 op }
147 e7b982f4 2021-07-14 op
148 e7b982f4 2021-07-14 op /*
149 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
150 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
151 65d9b3ca 2021-03-14 op */
152 676fba6f 2021-03-21 op int
153 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
154 65d9b3ca 2021-03-14 op {
155 676fba6f 2021-03-21 op const char *separators = " \t-";
156 218ac7f6 2021-07-16 op const char *start, *end, *line, *lastsep, *lastchar, *space;
157 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
158 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
159 5492b68f 2021-07-05 op int flags;
160 65d9b3ca 2021-03-14 op
161 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
162 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
163 65d9b3ca 2021-03-14 op
164 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
165 676fba6f 2021-03-21 op cur = prfxwidth;
166 676fba6f 2021-03-21 op start = line;
167 676fba6f 2021-03-21 op lastsep = NULL;
168 676fba6f 2021-03-21 op lastchar = line;
169 5492b68f 2021-07-05 op flags = 0;
170 218ac7f6 2021-07-16 op
171 218ac7f6 2021-07-16 op if (l->type == LINE_LINK && emojify_link &&
172 218ac7f6 2021-07-16 op emojied_line(l->line, &space)) {
173 218ac7f6 2021-07-16 op prfxwidth = utf8_swidth_between(l->line, space);
174 218ac7f6 2021-07-16 op cur = prfxwidth;
175 218ac7f6 2021-07-16 op line = space + 1;
176 218ac7f6 2021-07-16 op }
177 218ac7f6 2021-07-16 op
178 676fba6f 2021-03-21 op for (; *line; line++) {
179 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
180 676fba6f 2021-03-21 op continue;
181 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
182 1247b8cc 2021-07-16 op if (cur + w > width) {
183 676fba6f 2021-03-21 op end = lastsep == NULL
184 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
185 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
186 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
187 676fba6f 2021-03-21 op return 0;
188 963c680c 2021-07-05 op flags = L_CONTINUATION;
189 676fba6f 2021-03-21 op start = end;
190 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
191 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
192 5b010818 2021-07-05 op lastsep = line;
193 65d9b3ca 2021-03-14 op }
194 65d9b3ca 2021-03-14 op
195 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
196 676fba6f 2021-03-21 op cur += w;
197 65d9b3ca 2021-03-14 op }
198 65d9b3ca 2021-03-14 op
199 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
200 65d9b3ca 2021-03-14 op }
201 65d9b3ca 2021-03-14 op
202 65d9b3ca 2021-03-14 op int
203 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
204 65d9b3ca 2021-03-14 op {
205 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
206 65d9b3ca 2021-03-14 op int cont;
207 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
208 676fba6f 2021-03-21 op size_t cur, w;
209 65d9b3ca 2021-03-14 op
210 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
211 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
212 166712b4 2021-03-14 op
213 676fba6f 2021-03-21 op start = line;
214 676fba6f 2021-03-21 op lastchar = line;
215 676fba6f 2021-03-21 op cont = 0;
216 676fba6f 2021-03-21 op cur = 0;
217 676fba6f 2021-03-21 op for (; *line; line++) {
218 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
219 676fba6f 2021-03-21 op continue;
220 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
221 676fba6f 2021-03-21 op if (cur + w >= width) {
222 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
223 676fba6f 2021-03-21 op return 0;
224 88693f61 2021-07-15 op cont = L_CONTINUATION;
225 88693f61 2021-07-15 op if (dont_wrap_pre)
226 88693f61 2021-07-15 op return 1;
227 676fba6f 2021-03-21 op cur = 0;
228 676fba6f 2021-03-21 op start = lastchar;
229 676fba6f 2021-03-21 op }
230 65d9b3ca 2021-03-14 op
231 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
232 676fba6f 2021-03-21 op cur += w;
233 65d9b3ca 2021-03-14 op }
234 65d9b3ca 2021-03-14 op
235 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
236 de278567 2021-07-21 op }
237 de278567 2021-07-21 op
238 de278567 2021-07-21 op int
239 de278567 2021-07-21 op wrap_page(struct buffer *buffer, int width)
240 de278567 2021-07-21 op {
241 de278567 2021-07-21 op struct line *l;
242 de278567 2021-07-21 op const struct line *top_orig, *orig;
243 de278567 2021-07-21 op struct vline *vl;
244 de278567 2021-07-21 op int pre_width;
245 de278567 2021-07-21 op const char *prfx;
246 de278567 2021-07-21 op
247 de278567 2021-07-21 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
248 de278567 2021-07-21 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
249 de278567 2021-07-21 op
250 de278567 2021-07-21 op buffer->top_line = NULL;
251 de278567 2021-07-21 op buffer->current_line = NULL;
252 de278567 2021-07-21 op
253 de278567 2021-07-21 op buffer->force_redraw = 1;
254 de278567 2021-07-21 op buffer->curs_y = 0;
255 de278567 2021-07-21 op buffer->line_off = 0;
256 de278567 2021-07-21 op
257 de278567 2021-07-21 op empty_vlist(buffer);
258 de278567 2021-07-21 op
259 de278567 2021-07-21 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
260 de278567 2021-07-21 op prfx = line_prefixes[l->type].prfx1;
261 de278567 2021-07-21 op switch (l->type) {
262 de278567 2021-07-21 op case LINE_TEXT:
263 de278567 2021-07-21 op case LINE_LINK:
264 de278567 2021-07-21 op case LINE_TITLE_1:
265 de278567 2021-07-21 op case LINE_TITLE_2:
266 de278567 2021-07-21 op case LINE_TITLE_3:
267 de278567 2021-07-21 op case LINE_ITEM:
268 de278567 2021-07-21 op case LINE_QUOTE:
269 de278567 2021-07-21 op case LINE_PRE_START:
270 de278567 2021-07-21 op case LINE_PRE_END:
271 de278567 2021-07-21 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
272 de278567 2021-07-21 op break;
273 de278567 2021-07-21 op case LINE_PRE_CONTENT:
274 de278567 2021-07-21 op if (olivetti_mode)
275 de278567 2021-07-21 op pre_width = MIN(fill_column, width);
276 de278567 2021-07-21 op else
277 de278567 2021-07-21 op pre_width = width;
278 de278567 2021-07-21 op hardwrap_text(buffer, l, pre_width);
279 de278567 2021-07-21 op break;
280 de278567 2021-07-21 op case LINE_COMPL:
281 de278567 2021-07-21 op case LINE_COMPL_CURRENT:
282 de278567 2021-07-21 op wrap_one(buffer, prfx, l, width);
283 de278567 2021-07-21 op break;
284 de278567 2021-07-21 op }
285 de278567 2021-07-21 op
286 de278567 2021-07-21 op if (top_orig == l && buffer->top_line == NULL) {
287 de278567 2021-07-21 op buffer->line_off = buffer->line_max-1;
288 de278567 2021-07-21 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
289 de278567 2021-07-21 op
290 de278567 2021-07-21 op while (1) {
291 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
292 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
293 de278567 2021-07-21 op break;
294 de278567 2021-07-21 op buffer->top_line = vl;
295 de278567 2021-07-21 op buffer->line_off--;
296 de278567 2021-07-21 op }
297 de278567 2021-07-21 op }
298 de278567 2021-07-21 op
299 de278567 2021-07-21 op if (orig == l && buffer->current_line == NULL) {
300 de278567 2021-07-21 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
301 de278567 2021-07-21 op
302 de278567 2021-07-21 op while (1) {
303 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
304 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
305 de278567 2021-07-21 op break;
306 de278567 2021-07-21 op buffer->current_line = vl;
307 de278567 2021-07-21 op }
308 de278567 2021-07-21 op }
309 de278567 2021-07-21 op }
310 de278567 2021-07-21 op
311 de278567 2021-07-21 op if (buffer->current_line == NULL)
312 de278567 2021-07-21 op buffer->current_line = TAILQ_FIRST(&buffer->head);
313 de278567 2021-07-21 op
314 de278567 2021-07-21 op if (buffer->top_line == NULL)
315 de278567 2021-07-21 op buffer->top_line = buffer->current_line;
316 de278567 2021-07-21 op
317 de278567 2021-07-21 op return 1;
318 65d9b3ca 2021-03-14 op }