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);
56 if (l->type != LINE_COMPL &&
57 l->type != LINE_COMPL_CURRENT)
65 empty_vlist(struct buffer *buffer)
69 buffer->top_line = NULL;
71 buffer->current_line = NULL;
74 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
75 TAILQ_REMOVE(&buffer->head, vl, vlines);
82 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
86 if (!(l->flags & L_HIDDEN))
89 if ((vl = calloc(1, sizeof(*vl))) == NULL)
92 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
99 memcpy(vl->line, buf, len);
102 if (TAILQ_EMPTY(&buffer->head))
103 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
105 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
110 * Similar to wrap_text, but emit only o vline.
113 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
115 struct vline *vl, *t;
118 * be lazy: call wrap_text and then discard the continuations.
121 if (!wrap_text(buffer, prfx, l, width))
124 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
125 if (vl->flags & L_CONTINUATION) {
126 TAILQ_REMOVE(&buffer->head, vl, vlines);
137 * Build a list of visual line by wrapping the given line, assuming
138 * that when printed will have a leading prefix prfx.
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;
149 if ((line = l->line) == NULL)
150 return push_line(buffer, l, NULL, 0, 0);
152 prfxwidth = utf8_swidth(prfx);
158 for (; *line; line++) {
159 if (utf8_decode(&state, &cp, *line))
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))
168 flags = L_CONTINUATION;
170 cur = prfxwidth + utf8_swidth_between(start, lastchar);
171 } else if (strchr(separators, *line) != NULL) {
175 lastchar = utf8_prev_cp(line, l->line);
179 return push_line(buffer, l, start, line - start, flags);
183 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
185 const char *line, *start, *lastchar;
187 uint32_t state = 0, cp = 0;
190 if ((line = l->line) == NULL)
191 return push_line(buffer, l, NULL, 0, 0);
197 for (; *line; line++) {
198 if (utf8_decode(&state, &cp, *line))
200 w = utf8_chwidth(cp);
201 if (cur + w >= width) {
202 if (!push_line(buffer, l, start, lastchar - start, cont))
204 cont = L_CONTINUATION;
211 lastchar = utf8_prev_cp(line, l->line);
215 return push_line(buffer, l, start, line - start, cont);