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.
27 #include "telescope.h"
31 erase_buffer(struct buffer *buffer)
34 empty_linelist(buffer);
38 empty_linelist(struct buffer *buffer)
42 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
43 TAILQ_REMOVE(&buffer->page.head, l, lines);
46 if (l->type != LINE_COMPL &&
47 l->type != LINE_COMPL_CURRENT &&
56 empty_vlist(struct buffer *buffer)
60 buffer->top_line = NULL;
62 buffer->current_line = NULL;
65 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
66 TAILQ_REMOVE(&buffer->head, vl, vlines);
72 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
77 /* omit trailing spaces */
79 for (end = buf + len - 1;
80 end > buf && isspace(*end);
85 if (!(l->flags & L_HIDDEN))
88 if ((vl = calloc(1, sizeof(*vl))) == NULL)
93 vl->from = buf - l->line;
95 vl->cplen = utf8_ncplen(buf, vl->len);
99 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
104 * Build a list of visual line by wrapping the given line, assuming
105 * that when printed will have a leading prefix prfx.
108 wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
109 size_t width, int oneline)
111 const char *line, *space;
112 size_t ret, off, start, cur, prfxwidth;
115 if ((line = l->line) == NULL || *line == '\0')
116 return push_line(buffer, l, NULL, 0, 0);
118 prfxwidth = utf8_swidth(prfx);
123 if (l->type == LINE_LINK && emojify_link &&
124 emojied_line(l->line, &space)) {
125 prfxwidth = utf8_swidth_between(l->line, space);
130 for (off = 0; line[off] != '\0'; off += ret) {
133 ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
134 t = utf8_swidth_between(&line[off], &line[off + ret]);
136 if (cur + t <= width) {
141 if (!push_line(buffer, l, &line[start], off - start, flags))
147 flags = L_CONTINUATION;
153 return push_line(buffer, l, &line[start], off - start, flags);
158 wrap_page(struct buffer *buffer, int width)
161 const struct line *top_orig, *orig;
165 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
166 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
168 buffer->top_line = NULL;
169 buffer->current_line = NULL;
171 buffer->force_redraw = 1;
173 buffer->line_off = 0;
177 TAILQ_FOREACH(l, &buffer->page.head, lines) {
178 prfx = line_prefixes[l->type].prfx1;
189 case LINE_PRE_CONTENT:
192 case LINE_PATCH_HUNK_HDR:
195 wrap_text(buffer, prfx, l, MIN(fill_column, width),
199 case LINE_COMPL_CURRENT:
202 case LINE_DOWNLOAD_DONE:
203 case LINE_DOWNLOAD_INFO:
204 wrap_text(buffer, prfx, l, width, 1);
207 /* never, ever wrapped */
211 if (top_orig == l && buffer->top_line == NULL) {
212 buffer->line_off = buffer->line_max-1;
213 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
216 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
217 if (vl == NULL || vl->parent != orig)
219 buffer->top_line = vl;
224 if (orig == l && buffer->current_line == NULL) {
225 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
228 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
229 if (vl == NULL || vl->parent != orig)
231 buffer->current_line = vl;
236 if (buffer->current_line == NULL)
237 buffer->current_line = TAILQ_FIRST(&buffer->head);
239 if (buffer->top_line == NULL)
240 buffer->top_line = buffer->current_line;