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 65d9b3ca 2021-03-14 op
17 a0507108 2021-07-18 op #include <ctype.h>
18 65d9b3ca 2021-03-14 op #include <stdlib.h>
19 65d9b3ca 2021-03-14 op #include <string.h>
20 65d9b3ca 2021-03-14 op
21 88693f61 2021-07-15 op #include "defaults.h"
22 5caf7d67 2021-07-12 op #include "telescope.h"
23 5caf7d67 2021-07-12 op #include "utf8.h"
24 5caf7d67 2021-07-12 op
25 65d9b3ca 2021-03-14 op /*
26 65d9b3ca 2021-03-14 op * Text wrapping
27 65d9b3ca 2021-03-14 op * =============
28 65d9b3ca 2021-03-14 op *
29 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
30 65d9b3ca 2021-03-14 op *
31 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
32 65d9b3ca 2021-03-14 op * a. hard wrap.
33 65d9b3ca 2021-03-14 op * b. repeat
34 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
35 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
36 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
37 65d9b3ca 2021-03-14 op * 4. repeat
38 65d9b3ca 2021-03-14 op *
39 65d9b3ca 2021-03-14 op */
40 65d9b3ca 2021-03-14 op
41 ca5da938 2021-04-01 op void
42 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
43 bca92a4c 2021-07-01 op {
44 bca92a4c 2021-07-01 op empty_vlist(buffer);
45 bca92a4c 2021-07-01 op empty_linelist(buffer);
46 bca92a4c 2021-07-01 op }
47 bca92a4c 2021-07-01 op
48 bca92a4c 2021-07-01 op void
49 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
50 ca5da938 2021-04-01 op {
51 ca5da938 2021-04-01 op struct line *l, *lt;
52 ca5da938 2021-04-01 op
53 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
54 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
55 ca5da938 2021-04-01 op free(l->line);
56 b3be07ea 2021-07-18 op
57 b3be07ea 2021-07-18 op if (l->type != LINE_COMPL &&
58 b3be07ea 2021-07-18 op l->type != LINE_COMPL_CURRENT)
59 b3be07ea 2021-07-18 op free(l->alt);
60 b3be07ea 2021-07-18 op
61 ca5da938 2021-04-01 op free(l);
62 ca5da938 2021-04-01 op }
63 ca5da938 2021-04-01 op }
64 ca5da938 2021-04-01 op
65 ca5da938 2021-04-01 op void
66 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
67 ca5da938 2021-04-01 op {
68 ca5da938 2021-04-01 op struct vline *vl, *t;
69 ca5da938 2021-04-01 op
70 4ea9160a 2021-07-01 op buffer->top_line = NULL;
71 4ea9160a 2021-07-01 op buffer->line_off = 0;
72 46f6e974 2021-05-17 op buffer->current_line = NULL;
73 46f6e974 2021-05-17 op buffer->line_max = 0;
74 ca5da938 2021-04-01 op
75 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
76 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
77 ca5da938 2021-04-01 op free(vl->line);
78 ca5da938 2021-04-01 op free(vl);
79 ca5da938 2021-04-01 op }
80 ca5da938 2021-04-01 op }
81 ca5da938 2021-04-01 op
82 65d9b3ca 2021-03-14 op static int
83 87f7918d 2021-07-14 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
84 65d9b3ca 2021-03-14 op {
85 65d9b3ca 2021-03-14 op struct vline *vl;
86 a0507108 2021-07-18 op const char *end;
87 65d9b3ca 2021-03-14 op
88 a0507108 2021-07-18 op /* omit trailing spaces */
89 a0507108 2021-07-18 op if (len != 0) {
90 a0507108 2021-07-18 op for (end = buf + len - 1;
91 a0507108 2021-07-18 op end > buf && isspace(*end);
92 a0507108 2021-07-18 op end--, len--)
93 a0507108 2021-07-18 op ; /* nop */
94 a0507108 2021-07-18 op }
95 a0507108 2021-07-18 op
96 6e6c6afb 2021-07-14 op if (!(l->flags & L_HIDDEN))
97 6e6c6afb 2021-07-14 op buffer->line_max++;
98 65d9b3ca 2021-03-14 op
99 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
100 65d9b3ca 2021-03-14 op return 0;
101 65d9b3ca 2021-03-14 op
102 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
103 65d9b3ca 2021-03-14 op free(vl);
104 65d9b3ca 2021-03-14 op return 0;
105 65d9b3ca 2021-03-14 op }
106 65d9b3ca 2021-03-14 op
107 65d9b3ca 2021-03-14 op vl->parent = l;
108 65d9b3ca 2021-03-14 op if (len != 0)
109 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
110 5492b68f 2021-07-05 op vl->flags = flags;
111 65d9b3ca 2021-03-14 op
112 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
113 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
114 65d9b3ca 2021-03-14 op else
115 46f6e974 2021-05-17 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 676fba6f 2021-03-21 op if (cur + w >= width) {
220 46f6e974 2021-05-17 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 88693f61 2021-07-15 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 int pre_width;
243 de278567 2021-07-21 op const char *prfx;
244 de278567 2021-07-21 op
245 de278567 2021-07-21 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
246 de278567 2021-07-21 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
247 de278567 2021-07-21 op
248 de278567 2021-07-21 op buffer->top_line = NULL;
249 de278567 2021-07-21 op buffer->current_line = NULL;
250 de278567 2021-07-21 op
251 de278567 2021-07-21 op buffer->force_redraw = 1;
252 de278567 2021-07-21 op buffer->curs_y = 0;
253 de278567 2021-07-21 op buffer->line_off = 0;
254 de278567 2021-07-21 op
255 de278567 2021-07-21 op empty_vlist(buffer);
256 de278567 2021-07-21 op
257 de278567 2021-07-21 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
258 de278567 2021-07-21 op prfx = line_prefixes[l->type].prfx1;
259 de278567 2021-07-21 op switch (l->type) {
260 de278567 2021-07-21 op case LINE_TEXT:
261 de278567 2021-07-21 op case LINE_LINK:
262 de278567 2021-07-21 op case LINE_TITLE_1:
263 de278567 2021-07-21 op case LINE_TITLE_2:
264 de278567 2021-07-21 op case LINE_TITLE_3:
265 de278567 2021-07-21 op case LINE_ITEM:
266 de278567 2021-07-21 op case LINE_QUOTE:
267 de278567 2021-07-21 op case LINE_PRE_START:
268 de278567 2021-07-21 op case LINE_PRE_END:
269 de278567 2021-07-21 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
270 de278567 2021-07-21 op break;
271 de278567 2021-07-21 op case LINE_PRE_CONTENT:
272 de278567 2021-07-21 op if (olivetti_mode)
273 de278567 2021-07-21 op pre_width = MIN(fill_column, width);
274 de278567 2021-07-21 op else
275 de278567 2021-07-21 op pre_width = width;
276 de278567 2021-07-21 op hardwrap_text(buffer, l, pre_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 de278567 2021-07-21 op wrap_one(buffer, prfx, l, width);
281 de278567 2021-07-21 op break;
282 de278567 2021-07-21 op }
283 de278567 2021-07-21 op
284 de278567 2021-07-21 op if (top_orig == l && buffer->top_line == NULL) {
285 de278567 2021-07-21 op buffer->line_off = buffer->line_max-1;
286 de278567 2021-07-21 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
287 de278567 2021-07-21 op
288 de278567 2021-07-21 op while (1) {
289 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
290 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
291 de278567 2021-07-21 op break;
292 de278567 2021-07-21 op buffer->top_line = vl;
293 de278567 2021-07-21 op buffer->line_off--;
294 de278567 2021-07-21 op }
295 de278567 2021-07-21 op }
296 de278567 2021-07-21 op
297 de278567 2021-07-21 op if (orig == l && buffer->current_line == NULL) {
298 de278567 2021-07-21 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
299 de278567 2021-07-21 op
300 de278567 2021-07-21 op while (1) {
301 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
302 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
303 de278567 2021-07-21 op break;
304 de278567 2021-07-21 op buffer->current_line = vl;
305 de278567 2021-07-21 op }
306 de278567 2021-07-21 op }
307 de278567 2021-07-21 op }
308 de278567 2021-07-21 op
309 de278567 2021-07-21 op if (buffer->current_line == NULL)
310 de278567 2021-07-21 op buffer->current_line = TAILQ_FIRST(&buffer->head);
311 de278567 2021-07-21 op
312 de278567 2021-07-21 op if (buffer->top_line == NULL)
313 de278567 2021-07-21 op buffer->top_line = buffer->current_line;
314 de278567 2021-07-21 op
315 de278567 2021-07-21 op return 1;
316 65d9b3ca 2021-03-14 op }