Blob


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