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 <stdlib.h>
18 #include <string.h>
20 #include "defaults.h"
21 #include "telescope.h"
22 #include "utf8.h"
24 /*
25 * Text wrapping
26 * =============
27 *
28 * There's a simple text wrapping algorithm.
29 *
30 * 1. if it's a line in a pre-formatted block:
31 * a. hard wrap.
32 * b. repeat
33 * 2. otherwise advance the line char by char.
34 * 3. when ending the space, split the line at the last occurrence of
35 * a "word separator" (i.e. " \t-") or at point if none.
36 * 4. repeat
37 *
38 */
40 void
41 erase_buffer(struct buffer *buffer)
42 {
43 empty_vlist(buffer);
44 empty_linelist(buffer);
45 }
47 void
48 empty_linelist(struct buffer *buffer)
49 {
50 struct line *l, *lt;
52 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
53 TAILQ_REMOVE(&buffer->page.head, l, lines);
54 free(l->line);
56 if (l->type != LINE_COMPL &&
57 l->type != LINE_COMPL_CURRENT)
58 free(l->alt);
60 free(l);
61 }
62 }
64 void
65 empty_vlist(struct buffer *buffer)
66 {
67 struct vline *vl, *t;
69 buffer->top_line = NULL;
70 buffer->line_off = 0;
71 buffer->current_line = NULL;
72 buffer->line_max = 0;
74 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
75 TAILQ_REMOVE(&buffer->head, vl, vlines);
76 free(vl->line);
77 free(vl);
78 }
79 }
81 static int
82 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
83 {
84 struct vline *vl;
86 if (!(l->flags & L_HIDDEN))
87 buffer->line_max++;
89 if ((vl = calloc(1, sizeof(*vl))) == NULL)
90 return 0;
92 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
93 free(vl);
94 return 0;
95 }
97 vl->parent = l;
98 if (len != 0)
99 memcpy(vl->line, buf, len);
100 vl->flags = flags;
102 if (TAILQ_EMPTY(&buffer->head))
103 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
104 else
105 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
106 return 1;
109 /*
110 * Similar to wrap_text, but emit only o vline.
111 */
112 int
113 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
115 struct vline *vl, *t;
117 /*
118 * be lazy: call wrap_text and then discard the continuations.
119 */
121 if (!wrap_text(buffer, prfx, l, width))
122 return 0;
124 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
125 if (vl->flags & L_CONTINUATION) {
126 TAILQ_REMOVE(&buffer->head, vl, vlines);
127 free(vl->line);
128 free(vl);
129 buffer->line_max--;
133 return 1;
136 /*
137 * Build a list of visual line by wrapping the given line, assuming
138 * that when printed will have a leading prefix prfx.
139 */
140 int
141 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
143 const char *separators = " \t-";
144 const char *start, *end, *line, *lastsep, *lastchar, *space;
145 uint32_t cp = 0, state = 0;
146 size_t cur, prfxwidth, w;
147 int flags;
149 if ((line = l->line) == NULL)
150 return push_line(buffer, l, NULL, 0, 0);
152 prfxwidth = utf8_swidth(prfx);
153 cur = prfxwidth;
154 start = line;
155 lastsep = NULL;
156 lastchar = line;
157 flags = 0;
159 if (l->type == LINE_LINK && emojify_link &&
160 emojied_line(l->line, &space)) {
161 prfxwidth = utf8_swidth_between(l->line, space);
162 cur = prfxwidth;
163 line = space + 1;
166 for (; *line; line++) {
167 if (utf8_decode(&state, &cp, *line))
168 continue;
169 w = utf8_chwidth(cp);
170 if (cur + w > width) {
171 end = lastsep == NULL
172 ? utf8_next_cp((char*)lastchar)
173 : utf8_next_cp((char*)lastsep);
174 if (!push_line(buffer, l, start, end - start, flags))
175 return 0;
176 flags = L_CONTINUATION;
177 start = end;
178 cur = prfxwidth + utf8_swidth_between(start, lastchar);
179 } else if (strchr(separators, *line) != NULL) {
180 lastsep = line;
183 lastchar = utf8_prev_cp(line, l->line);
184 cur += w;
187 return push_line(buffer, l, start, line - start, flags);
190 int
191 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
193 const char *line, *start, *lastchar;
194 int cont;
195 uint32_t state = 0, cp = 0;
196 size_t cur, w;
198 if ((line = l->line) == NULL)
199 return push_line(buffer, l, NULL, 0, 0);
201 start = line;
202 lastchar = line;
203 cont = 0;
204 cur = 0;
205 for (; *line; line++) {
206 if (utf8_decode(&state, &cp, *line))
207 continue;
208 w = utf8_chwidth(cp);
209 if (cur + w >= width) {
210 if (!push_line(buffer, l, start, lastchar - start, cont))
211 return 0;
212 cont = L_CONTINUATION;
213 if (dont_wrap_pre)
214 return 1;
215 cur = 0;
216 start = lastchar;
219 lastchar = utf8_prev_cp(line, l->line);
220 cur += w;
223 return push_line(buffer, l, start, line - start, cont);