2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
21 #include "telescope.h"
28 * There's a simple text wrapping algorithm.
30 * 1. if it's a line in a pre-formatted block:
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.
41 erase_buffer(struct buffer *buffer)
44 empty_linelist(buffer);
48 empty_linelist(struct buffer *buffer)
52 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
53 TAILQ_REMOVE(&buffer->page.head, l, lines);
61 empty_vlist(struct buffer *buffer)
65 buffer->top_line = NULL;
67 buffer->current_line = NULL;
70 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
71 TAILQ_REMOVE(&buffer->head, vl, vlines);
78 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
82 if (!(l->flags & L_HIDDEN))
85 if ((vl = calloc(1, sizeof(*vl))) == NULL)
88 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
95 memcpy(vl->line, buf, len);
98 if (TAILQ_EMPTY(&buffer->head))
99 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
101 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
106 * Similar to wrap_text, but emit only o vline.
109 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
111 struct vline *vl, *t;
114 * be lazy: call wrap_text and then discard the continuations.
117 if (!wrap_text(buffer, prfx, l, width))
120 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
121 if (vl->flags & L_CONTINUATION) {
122 TAILQ_REMOVE(&buffer->head, vl, vlines);
133 * Build a list of visual line by wrapping the given line, assuming
134 * that when printed will have a leading prefix prfx.
137 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
139 const char *separators = " \t-";
140 const char *start, *end, *line, *lastsep, *lastchar, *space;
141 uint32_t cp = 0, state = 0;
142 size_t cur, prfxwidth, w;
145 if ((line = l->line) == NULL)
146 return push_line(buffer, l, NULL, 0, 0);
148 prfxwidth = utf8_swidth(prfx);
155 if (l->type == LINE_LINK && emojify_link &&
156 emojied_line(l->line, &space)) {
157 prfxwidth = utf8_swidth_between(l->line, space);
162 for (; *line; line++) {
163 if (utf8_decode(&state, &cp, *line))
165 w = utf8_chwidth(cp);
166 if (cur + w > width) {
167 end = lastsep == NULL
168 ? utf8_next_cp((char*)lastchar)
169 : utf8_next_cp((char*)lastsep);
170 if (!push_line(buffer, l, start, end - start, flags))
172 flags = L_CONTINUATION;
174 cur = prfxwidth + utf8_swidth_between(start, lastchar);
175 } else if (strchr(separators, *line) != NULL) {
179 lastchar = utf8_prev_cp(line, l->line);
183 return push_line(buffer, l, start, line - start, flags);
187 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
189 const char *line, *start, *lastchar;
191 uint32_t state = 0, cp = 0;
194 if ((line = l->line) == NULL)
195 return push_line(buffer, l, NULL, 0, 0);
201 for (; *line; line++) {
202 if (utf8_decode(&state, &cp, *line))
204 w = utf8_chwidth(cp);
205 if (cur + w >= width) {
206 if (!push_line(buffer, l, start, lastchar - start, cont))
208 cont = L_CONTINUATION;
215 lastchar = utf8_prev_cp(line, l->line);
219 return push_line(buffer, l, start, line - start, cont);