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 "telescope.h"
21 #include "utf8.h"
23 /*
24 * Text wrapping
25 * =============
26 *
27 * There's a simple text wrapping algorithm.
28 *
29 * 1. if it's a line in a pre-formatted block:
30 * a. hard wrap.
31 * b. repeat
32 * 2. otherwise advance the line char by char.
33 * 3. when ending the space, split the line at the last occurrence of
34 * a "word separator" (i.e. " \t-") or at point if none.
35 * 4. repeat
36 *
37 */
39 void
40 erase_buffer(struct buffer *buffer)
41 {
42 empty_vlist(buffer);
43 empty_linelist(buffer);
44 }
46 void
47 empty_linelist(struct buffer *buffer)
48 {
49 struct line *l, *lt;
51 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
52 TAILQ_REMOVE(&buffer->page.head, l, lines);
53 free(l->line);
55 if (l->type != LINE_COMPL &&
56 l->type != LINE_COMPL_CURRENT)
57 free(l->meta.alt);
59 free(l);
60 }
61 }
63 void
64 empty_vlist(struct buffer *buffer)
65 {
66 struct vline *vl, *t;
68 buffer->top_line = NULL;
69 buffer->line_off = 0;
70 buffer->current_line = NULL;
71 buffer->line_max = 0;
73 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
74 TAILQ_REMOVE(&buffer->head, vl, vlines);
75 free(vl->line);
76 free(vl);
77 }
78 }
80 static int
81 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
82 {
83 struct vline *vl;
85 if (!(l->flags & L_HIDDEN))
86 buffer->line_max++;
88 if ((vl = calloc(1, sizeof(*vl))) == NULL)
89 return 0;
91 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
92 free(vl);
93 return 0;
94 }
96 vl->parent = l;
97 if (len != 0)
98 memcpy(vl->line, buf, len);
99 vl->flags = flags;
101 if (TAILQ_EMPTY(&buffer->head))
102 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
103 else
104 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
105 return 1;
108 /*
109 * Similar to wrap_text, but emit only o vline.
110 */
111 int
112 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
114 struct vline *vl, *t;
116 /*
117 * be lazy: call wrap_text and then discard the continuations.
118 */
120 if (!wrap_text(buffer, prfx, l, width))
121 return 0;
123 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
124 if (vl->flags & L_CONTINUATION) {
125 TAILQ_REMOVE(&buffer->head, vl, vlines);
126 free(vl->line);
127 free(vl);
128 buffer->line_max--;
132 return 1;
135 /*
136 * Build a list of visual line by wrapping the given line, assuming
137 * that when printed will have a leading prefix prfx.
138 */
139 int
140 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
142 const char *separators = " \t-";
143 const char *start, *end, *line, *lastsep, *lastchar;
144 uint32_t cp = 0, state = 0;
145 size_t cur, prfxwidth, w;
146 int flags;
148 if ((line = l->line) == NULL)
149 return push_line(buffer, l, NULL, 0, 0);
151 prfxwidth = utf8_swidth(prfx);
152 cur = prfxwidth;
153 start = line;
154 lastsep = NULL;
155 lastchar = line;
156 flags = 0;
157 for (; *line; line++) {
158 if (utf8_decode(&state, &cp, *line))
159 continue;
160 w = utf8_chwidth(cp);
161 if (cur + w >= width -1) {
162 end = lastsep == NULL
163 ? utf8_next_cp((char*)lastchar)
164 : utf8_next_cp((char*)lastsep);
165 if (!push_line(buffer, l, start, end - start, flags))
166 return 0;
167 flags = L_CONTINUATION;
168 start = end;
169 cur = prfxwidth + utf8_swidth_between(start, lastchar);
170 } else if (strchr(separators, *line) != NULL) {
171 lastsep = line;
174 lastchar = utf8_prev_cp(line, l->line);
175 cur += w;
178 return push_line(buffer, l, start, line - start, flags);
181 int
182 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
184 const char *line, *start, *lastchar;
185 int cont;
186 uint32_t state = 0, cp = 0;
187 size_t cur, w;
189 if ((line = l->line) == NULL)
190 return push_line(buffer, l, NULL, 0, 0);
192 start = line;
193 lastchar = line;
194 cont = 0;
195 cur = 0;
196 for (; *line; line++) {
197 if (utf8_decode(&state, &cp, *line))
198 continue;
199 w = utf8_chwidth(cp);
200 if (cur + w >= width) {
201 if (!push_line(buffer, l, start, lastchar - start, cont))
202 return 0;
203 cont = 1;
204 cur = 0;
205 start = lastchar;
208 lastchar = utf8_prev_cp(line, l->line);
209 cur += w;
212 return push_line(buffer, l, start, line - start, cont);