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 free(l->alt);
63 free(l);
64 }
65 }
67 void
68 empty_vlist(struct buffer *buffer)
69 {
70 struct vline *vl, *t;
72 buffer->top_line = NULL;
73 buffer->line_off = 0;
74 buffer->current_line = NULL;
75 buffer->line_max = 0;
77 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
78 TAILQ_REMOVE(&buffer->head, vl, vlines);
79 free(vl->line);
80 free(vl);
81 }
82 }
84 static int
85 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
86 {
87 struct vline *vl;
88 const char *end;
90 /* omit trailing spaces */
91 if (len != 0) {
92 for (end = buf + len - 1;
93 end > buf && isspace(*end);
94 end--, len--)
95 ; /* nop */
96 }
98 if (!(l->flags & L_HIDDEN))
99 buffer->line_max++;
101 if ((vl = calloc(1, sizeof(*vl))) == NULL)
102 return 0;
104 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
105 free(vl);
106 return 0;
109 vl->parent = l;
110 if (len != 0)
111 memcpy(vl->line, buf, len);
112 vl->flags = flags;
114 if (TAILQ_EMPTY(&buffer->head))
115 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
116 else
117 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
118 return 1;
121 /*
122 * Similar to wrap_text, but emit only o vline.
123 */
124 int
125 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
127 struct vline *vl, *t;
129 /*
130 * be lazy: call wrap_text and then discard the continuations.
131 */
133 if (!wrap_text(buffer, prfx, l, width))
134 return 0;
136 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
137 if (vl->flags & L_CONTINUATION) {
138 TAILQ_REMOVE(&buffer->head, vl, vlines);
139 free(vl->line);
140 free(vl);
141 buffer->line_max--;
145 return 1;
148 /*
149 * Build a list of visual line by wrapping the given line, assuming
150 * that when printed will have a leading prefix prfx.
151 */
152 int
153 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
155 const char *separators = " \t-";
156 const char *start, *end, *line, *lastsep, *lastchar, *space;
157 uint32_t cp = 0, state = 0;
158 size_t cur, prfxwidth, w;
159 int flags;
161 if ((line = l->line) == NULL)
162 return push_line(buffer, l, NULL, 0, 0);
164 prfxwidth = utf8_swidth(prfx);
165 cur = prfxwidth;
166 start = line;
167 lastsep = NULL;
168 lastchar = line;
169 flags = 0;
171 if (l->type == LINE_LINK && emojify_link &&
172 emojied_line(l->line, &space)) {
173 prfxwidth = utf8_swidth_between(l->line, space);
174 cur = prfxwidth;
175 line = space + 1;
178 for (; *line; line++) {
179 if (utf8_decode(&state, &cp, *line))
180 continue;
181 w = utf8_chwidth(cp);
182 if (cur + w > width) {
183 end = lastsep == NULL
184 ? utf8_next_cp((char*)lastchar)
185 : utf8_next_cp((char*)lastsep);
186 if (!push_line(buffer, l, start, end - start, flags))
187 return 0;
188 flags = L_CONTINUATION;
189 start = end;
190 cur = prfxwidth + utf8_swidth_between(start, lastchar);
191 } else if (strchr(separators, *line) != NULL) {
192 lastsep = line;
195 lastchar = utf8_prev_cp(line, l->line);
196 cur += w;
199 return push_line(buffer, l, start, line - start, flags);
202 int
203 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
205 const char *line, *start, *lastchar;
206 int cont;
207 uint32_t state = 0, cp = 0;
208 size_t cur, w;
210 if ((line = l->line) == NULL)
211 return push_line(buffer, l, NULL, 0, 0);
213 start = line;
214 lastchar = line;
215 cont = 0;
216 cur = 0;
217 for (; *line; line++) {
218 if (utf8_decode(&state, &cp, *line))
219 continue;
220 w = utf8_chwidth(cp);
221 if (cur + w >= width) {
222 if (!push_line(buffer, l, start, lastchar - start, cont))
223 return 0;
224 cont = L_CONTINUATION;
225 if (dont_wrap_pre)
226 return 1;
227 cur = 0;
228 start = lastchar;
231 lastchar = utf8_prev_cp(line, l->line);
232 cur += w;
235 return push_line(buffer, l, start, line - start, cont);
238 int
239 wrap_page(struct buffer *buffer, int width)
241 struct line *l;
242 const struct line *top_orig, *orig;
243 struct vline *vl;
244 int pre_width;
245 const char *prfx;
247 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
248 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
250 buffer->top_line = NULL;
251 buffer->current_line = NULL;
253 buffer->force_redraw = 1;
254 buffer->curs_y = 0;
255 buffer->line_off = 0;
257 empty_vlist(buffer);
259 TAILQ_FOREACH(l, &buffer->page.head, lines) {
260 prfx = line_prefixes[l->type].prfx1;
261 switch (l->type) {
262 case LINE_TEXT:
263 case LINE_LINK:
264 case LINE_TITLE_1:
265 case LINE_TITLE_2:
266 case LINE_TITLE_3:
267 case LINE_ITEM:
268 case LINE_QUOTE:
269 case LINE_PRE_START:
270 case LINE_PRE_END:
271 wrap_text(buffer, prfx, l, MIN(fill_column, width));
272 break;
273 case LINE_PRE_CONTENT:
274 if (olivetti_mode)
275 pre_width = MIN(fill_column, width);
276 else
277 pre_width = width;
278 hardwrap_text(buffer, l, pre_width);
279 break;
280 case LINE_COMPL:
281 case LINE_COMPL_CURRENT:
282 wrap_one(buffer, prfx, l, width);
283 break;
286 if (top_orig == l && buffer->top_line == NULL) {
287 buffer->line_off = buffer->line_max-1;
288 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
290 while (1) {
291 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
292 if (vl == NULL || vl->parent != orig)
293 break;
294 buffer->top_line = vl;
295 buffer->line_off--;
299 if (orig == l && buffer->current_line == NULL) {
300 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
302 while (1) {
303 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
304 if (vl == NULL || vl->parent != orig)
305 break;
306 buffer->current_line = vl;
311 if (buffer->current_line == NULL)
312 buffer->current_line = TAILQ_FIRST(&buffer->head);
314 if (buffer->top_line == NULL)
315 buffer->top_line = buffer->current_line;
317 return 1;