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.
24 #include "telescope.h"
31 * There's a simple text wrapping algorithm.
33 * 1. if it's a line in a pre-formatted block:
36 * 2. otherwise advance the line char by char.
37 * 3. when ending the space, split the line at the last occurrence of
38 * a "word separator" (i.e. " \t-") or at point if none.
44 erase_buffer(struct buffer *buffer)
47 empty_linelist(buffer);
51 empty_linelist(struct buffer *buffer)
55 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
56 TAILQ_REMOVE(&buffer->page.head, l, lines);
59 if (l->type != LINE_COMPL &&
60 l->type != LINE_COMPL_CURRENT)
68 empty_vlist(struct buffer *buffer)
72 buffer->top_line = NULL;
74 buffer->current_line = NULL;
77 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
78 TAILQ_REMOVE(&buffer->head, vl, vlines);
85 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
90 /* omit trailing spaces */
92 for (end = buf + len - 1;
93 end > buf && isspace(*end);
98 if (!(l->flags & L_HIDDEN))
101 if ((vl = calloc(1, sizeof(*vl))) == NULL)
104 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
111 memcpy(vl->line, buf, len);
114 if (TAILQ_EMPTY(&buffer->head))
115 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
117 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
122 * Similar to wrap_text, but emit only o vline.
125 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
127 struct vline *vl, *t;
130 * be lazy: call wrap_text and then discard the continuations.
133 if (!wrap_text(buffer, prfx, l, width))
136 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
137 if (vl->flags & L_CONTINUATION) {
138 TAILQ_REMOVE(&buffer->head, vl, vlines);
149 * Build a list of visual line by wrapping the given line, assuming
150 * that when printed will have a leading prefix prfx.
153 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
155 const char *separators = " \t-";
156 const char *start, *end, *line, *lastsep, *lastchar, *space;
157 uint32_t cp = 0, state = 0;
158 size_t cur, prfxwidth, w;
161 if ((line = l->line) == NULL)
162 return push_line(buffer, l, NULL, 0, 0);
164 prfxwidth = utf8_swidth(prfx);
171 if (l->type == LINE_LINK && emojify_link &&
172 emojied_line(l->line, &space)) {
173 prfxwidth = utf8_swidth_between(l->line, space);
178 for (; *line; line++) {
179 if (utf8_decode(&state, &cp, *line))
181 w = utf8_chwidth(cp);
182 if (cur + w > width) {
183 end = lastsep == NULL
184 ? utf8_next_cp((char*)lastchar)
185 : utf8_next_cp((char*)lastsep);
186 if (!push_line(buffer, l, start, end - start, flags))
188 flags = L_CONTINUATION;
190 cur = prfxwidth + utf8_swidth_between(start, lastchar);
191 } else if (strchr(separators, *line) != NULL) {
195 lastchar = utf8_prev_cp(line, l->line);
199 return push_line(buffer, l, start, line - start, flags);
203 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
205 const char *line, *start, *lastchar;
207 uint32_t state = 0, cp = 0;
210 if ((line = l->line) == NULL)
211 return push_line(buffer, l, NULL, 0, 0);
217 for (; *line; line++) {
218 if (utf8_decode(&state, &cp, *line))
220 w = utf8_chwidth(cp);
221 if (cur + w >= width) {
222 if (!push_line(buffer, l, start, lastchar - start, cont))
224 cont = L_CONTINUATION;
231 lastchar = utf8_prev_cp(line, l->line);
235 return push_line(buffer, l, start, line - start, cont);
239 wrap_page(struct buffer *buffer, int width)
242 const struct line *top_orig, *orig;
247 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
248 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
250 buffer->top_line = NULL;
251 buffer->current_line = NULL;
253 buffer->force_redraw = 1;
255 buffer->line_off = 0;
259 TAILQ_FOREACH(l, &buffer->page.head, lines) {
260 prfx = line_prefixes[l->type].prfx1;
271 wrap_text(buffer, prfx, l, MIN(fill_column, width));
273 case LINE_PRE_CONTENT:
275 pre_width = MIN(fill_column, width);
278 hardwrap_text(buffer, l, pre_width);
281 case LINE_COMPL_CURRENT:
282 wrap_one(buffer, prfx, l, width);
286 if (top_orig == l && buffer->top_line == NULL) {
287 buffer->line_off = buffer->line_max-1;
288 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
291 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
292 if (vl == NULL || vl->parent != orig)
294 buffer->top_line = vl;
299 if (orig == l && buffer->current_line == NULL) {
300 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
303 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
304 if (vl == NULL || vl->parent != orig)
306 buffer->current_line = vl;
311 if (buffer->current_line == NULL)
312 buffer->current_line = TAILQ_FIRST(&buffer->head);
314 if (buffer->top_line == NULL)
315 buffer->top_line = buffer->current_line;