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.
20 #include "telescope.h"
27 * There's a simple text wrapping algorithm.
29 * 1. if it's a line in a pre-formatted block:
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.
40 erase_buffer(struct buffer *buffer)
43 empty_linelist(buffer);
47 empty_linelist(struct buffer *buffer)
51 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
52 TAILQ_REMOVE(&buffer->page.head, l, lines);
55 if (l->type != LINE_COMPL &&
56 l->type != LINE_COMPL_CURRENT)
64 empty_vlist(struct buffer *buffer)
68 buffer->top_line = NULL;
70 buffer->current_line = NULL;
73 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
74 TAILQ_REMOVE(&buffer->head, vl, vlines);
81 push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int flags)
87 if ((vl = calloc(1, sizeof(*vl))) == NULL)
90 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
97 memcpy(vl->line, buf, len);
100 if (TAILQ_EMPTY(&buffer->head))
101 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
103 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
108 * Similar to wrap_text, but emit only o vline.
111 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
113 struct vline *vl, *t;
116 * be lazy: call wrap_text and then discard the continuations.
119 if (!wrap_text(buffer, prfx, l, width))
122 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
123 if (vl->flags & L_CONTINUATION) {
124 TAILQ_REMOVE(&buffer->head, vl, vlines);
134 * Build a list of visual line by wrapping the given line, assuming
135 * that when printed will have a leading prefix prfx.
138 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
140 const char *separators = " \t-";
141 const char *start, *end, *line, *lastsep, *lastchar;
142 uint32_t cp = 0, state = 0;
143 size_t cur, prfxwidth, w;
146 if ((line = l->line) == NULL)
147 return push_line(buffer, l, NULL, 0, 0);
149 prfxwidth = utf8_swidth(prfx);
155 for (; *line; line++) {
156 if (utf8_decode(&state, &cp, *line))
158 w = utf8_chwidth(cp);
159 if (cur + w >= width -1) {
160 end = lastsep == NULL
161 ? utf8_next_cp((char*)lastchar)
162 : utf8_next_cp((char*)lastsep);
163 if (!push_line(buffer, l, start, end - start, flags))
165 flags = L_CONTINUATION;
167 cur = prfxwidth + utf8_swidth_between(start, lastchar);
168 } else if (strchr(separators, *line) != NULL) {
172 lastchar = utf8_prev_cp(line, l->line);
176 return push_line(buffer, l, start, line - start, flags);
180 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
182 const char *line, *start, *lastchar;
184 uint32_t state = 0, cp = 0;
187 if ((line = l->line) == NULL)
188 return push_line(buffer, l, NULL, 0, 0);
194 for (; *line; line++) {
195 if (utf8_decode(&state, &cp, *line))
197 w = utf8_chwidth(cp);
198 if (cur + w >= width) {
199 if (!push_line(buffer, l, start, lastchar - start, cont))
206 lastchar = utf8_prev_cp(line, l->line);
210 return push_line(buffer, l, start, line - start, cont);