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.
22 #include "telescope.h"
29 * There's a simple text wrapping algorithm.
31 * 1. if it's a line in a pre-formatted block:
34 * 2. otherwise advance the line char by char.
35 * 3. when ending the space, split the line at the last occurrence of
36 * a "word separator" (i.e. " \t-") or at point if none.
42 erase_buffer(struct buffer *buffer)
45 empty_linelist(buffer);
49 empty_linelist(struct buffer *buffer)
53 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
54 TAILQ_REMOVE(&buffer->page.head, l, lines);
57 if (l->type != LINE_COMPL &&
58 l->type != LINE_COMPL_CURRENT)
66 empty_vlist(struct buffer *buffer)
70 buffer->top_line = NULL;
72 buffer->current_line = NULL;
75 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
76 TAILQ_REMOVE(&buffer->head, vl, vlines);
83 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
88 /* omit trailing spaces */
90 for (end = buf + len - 1;
91 end > buf && isspace(*end);
96 if (!(l->flags & L_HIDDEN))
99 if ((vl = calloc(1, sizeof(*vl))) == NULL)
102 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
109 memcpy(vl->line, buf, len);
112 if (TAILQ_EMPTY(&buffer->head))
113 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
115 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
120 * Similar to wrap_text, but emit only o vline.
123 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
125 struct vline *vl, *t;
128 * be lazy: call wrap_text and then discard the continuations.
131 if (!wrap_text(buffer, prfx, l, width))
134 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
135 if (vl->flags & L_CONTINUATION) {
136 TAILQ_REMOVE(&buffer->head, vl, vlines);
147 * Build a list of visual line by wrapping the given line, assuming
148 * that when printed will have a leading prefix prfx.
151 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
153 const char *separators = " \t-";
154 const char *start, *end, *line, *lastsep, *lastchar, *space;
155 uint32_t cp = 0, state = 0;
156 size_t cur, prfxwidth, w;
159 if ((line = l->line) == NULL)
160 return push_line(buffer, l, NULL, 0, 0);
162 prfxwidth = utf8_swidth(prfx);
169 if (l->type == LINE_LINK && emojify_link &&
170 emojied_line(l->line, &space)) {
171 prfxwidth = utf8_swidth_between(l->line, space);
176 for (; *line; line++) {
177 if (utf8_decode(&state, &cp, *line))
179 w = utf8_chwidth(cp);
180 if (cur + w > width) {
181 end = lastsep == NULL
182 ? utf8_next_cp((char*)lastchar)
183 : utf8_next_cp((char*)lastsep);
184 if (!push_line(buffer, l, start, end - start, flags))
186 flags = L_CONTINUATION;
188 cur = prfxwidth + utf8_swidth_between(start, lastchar);
189 } else if (strchr(separators, *line) != NULL) {
193 lastchar = utf8_prev_cp(line, l->line);
197 return push_line(buffer, l, start, line - start, flags);
201 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
203 const char *line, *start, *lastchar;
205 uint32_t state = 0, cp = 0;
208 if ((line = l->line) == NULL)
209 return push_line(buffer, l, NULL, 0, 0);
215 for (; *line; line++) {
216 if (utf8_decode(&state, &cp, *line))
218 w = utf8_chwidth(cp);
219 if (cur + w >= width) {
220 if (!push_line(buffer, l, start, lastchar - start, cont))
222 cont = L_CONTINUATION;
229 lastchar = utf8_prev_cp(line, l->line);
233 return push_line(buffer, l, start, line - start, cont);