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 if (TAILQ_EMPTY(&buffer->head))
116 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
117 else
118 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
119 return 1;
122 /*
123 * Similar to wrap_text, but emit only o vline.
124 */
125 int
126 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
128 struct vline *vl, *t;
130 /*
131 * be lazy: call wrap_text and then discard the continuations.
132 */
134 if (!wrap_text(buffer, prfx, l, width))
135 return 0;
137 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
138 if (vl->flags & L_CONTINUATION) {
139 TAILQ_REMOVE(&buffer->head, vl, vlines);
140 free(vl->line);
141 free(vl);
142 buffer->line_max--;
146 return 1;
149 /*
150 * Build a list of visual line by wrapping the given line, assuming
151 * that when printed will have a leading prefix prfx.
152 */
153 int
154 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
156 const char *separators = " \t-";
157 const char *start, *end, *line, *lastsep, *lastchar, *space;
158 uint32_t cp = 0, state = 0;
159 size_t cur, prfxwidth, w;
160 int flags;
162 if ((line = l->line) == NULL)
163 return push_line(buffer, l, NULL, 0, 0);
165 prfxwidth = utf8_swidth(prfx);
166 cur = prfxwidth;
167 start = line;
168 lastsep = NULL;
169 lastchar = line;
170 flags = 0;
172 if (l->type == LINE_LINK && emojify_link &&
173 emojied_line(l->line, &space)) {
174 prfxwidth = utf8_swidth_between(l->line, space);
175 cur = prfxwidth;
176 line = space + 1;
179 for (; *line; line++) {
180 if (utf8_decode(&state, &cp, *line))
181 continue;
182 w = utf8_chwidth(cp);
183 if (cur + w > width) {
184 end = lastsep == NULL
185 ? utf8_next_cp((char*)lastchar)
186 : utf8_next_cp((char*)lastsep);
187 if (!push_line(buffer, l, start, end - start, flags))
188 return 0;
189 flags = L_CONTINUATION;
190 start = end;
191 cur = prfxwidth + utf8_swidth_between(start, lastchar);
192 } else if (strchr(separators, *line) != NULL) {
193 lastsep = line;
196 lastchar = utf8_prev_cp(line, l->line);
197 cur += w;
200 return push_line(buffer, l, start, line - start, flags);
203 int
204 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
206 const char *line, *start, *lastchar;
207 int cont;
208 uint32_t state = 0, cp = 0;
209 size_t cur, w;
211 if ((line = l->line) == NULL)
212 return push_line(buffer, l, NULL, 0, 0);
214 start = line;
215 lastchar = line;
216 cont = 0;
217 cur = 0;
218 for (; *line; line++) {
219 if (utf8_decode(&state, &cp, *line))
220 continue;
221 w = utf8_chwidth(cp);
222 if (cur + w >= width) {
223 if (!push_line(buffer, l, start, lastchar - start, cont))
224 return 0;
225 cont = L_CONTINUATION;
226 if (dont_wrap_pre)
227 return 1;
228 cur = 0;
229 start = lastchar;
232 lastchar = utf8_prev_cp(line, l->line);
233 cur += w;
236 return push_line(buffer, l, start, line - start, cont);
239 int
240 wrap_page(struct buffer *buffer, int width)
242 struct line *l;
243 const struct line *top_orig, *orig;
244 struct vline *vl;
245 int pre_width;
246 const char *prfx;
248 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
249 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
251 buffer->top_line = NULL;
252 buffer->current_line = NULL;
254 buffer->force_redraw = 1;
255 buffer->curs_y = 0;
256 buffer->line_off = 0;
258 empty_vlist(buffer);
260 TAILQ_FOREACH(l, &buffer->page.head, lines) {
261 prfx = line_prefixes[l->type].prfx1;
262 switch (l->type) {
263 case LINE_TEXT:
264 case LINE_LINK:
265 case LINE_TITLE_1:
266 case LINE_TITLE_2:
267 case LINE_TITLE_3:
268 case LINE_ITEM:
269 case LINE_QUOTE:
270 case LINE_PRE_START:
271 case LINE_PRE_END:
272 wrap_text(buffer, prfx, l, MIN(fill_column, width));
273 break;
274 case LINE_PRE_CONTENT:
275 if (olivetti_mode)
276 pre_width = MIN(fill_column, width);
277 else
278 pre_width = width;
279 hardwrap_text(buffer, l, pre_width);
280 break;
281 case LINE_COMPL:
282 case LINE_COMPL_CURRENT:
283 case LINE_HELP:
284 wrap_one(buffer, prfx, l, width);
285 break;
288 if (top_orig == l && buffer->top_line == NULL) {
289 buffer->line_off = buffer->line_max-1;
290 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
292 while (1) {
293 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
294 if (vl == NULL || vl->parent != orig)
295 break;
296 buffer->top_line = vl;
297 buffer->line_off--;
301 if (orig == l && buffer->current_line == NULL) {
302 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
304 while (1) {
305 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
306 if (vl == NULL || vl->parent != orig)
307 break;
308 buffer->current_line = vl;
313 if (buffer->current_line == NULL)
314 buffer->current_line = TAILQ_FIRST(&buffer->head);
316 if (buffer->top_line == NULL)
317 buffer->top_line = buffer->current_line;
319 return 1;