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->meta.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;
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;
158 for (; *line; line++) {
159 if (utf8_decode(&state, &cp, *line))
160 continue;
161 w = utf8_chwidth(cp);
162 if (cur + w >= width -1) {
163 end = lastsep == NULL
164 ? utf8_next_cp((char*)lastchar)
165 : utf8_next_cp((char*)lastsep);
166 if (!push_line(buffer, l, start, end - start, flags))
167 return 0;
168 flags = L_CONTINUATION;
169 start = end;
170 cur = prfxwidth + utf8_swidth_between(start, lastchar);
171 } else if (strchr(separators, *line) != NULL) {
172 lastsep = line;
175 lastchar = utf8_prev_cp(line, l->line);
176 cur += w;
179 return push_line(buffer, l, start, line - start, flags);
182 int
183 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
185 const char *line, *start, *lastchar;
186 int cont;
187 uint32_t state = 0, cp = 0;
188 size_t cur, w;
190 if ((line = l->line) == NULL)
191 return push_line(buffer, l, NULL, 0, 0);
193 start = line;
194 lastchar = line;
195 cont = 0;
196 cur = 0;
197 for (; *line; line++) {
198 if (utf8_decode(&state, &cp, *line))
199 continue;
200 w = utf8_chwidth(cp);
201 if (cur + w >= width) {
202 if (!push_line(buffer, l, start, lastchar - start, cont))
203 return 0;
204 cont = L_CONTINUATION;
205 if (dont_wrap_pre)
206 return 1;
207 cur = 0;
208 start = lastchar;
211 lastchar = utf8_prev_cp(line, l->line);
212 cur += w;
215 return push_line(buffer, l, start, line - start, cont);