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);
73 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
78 /* omit trailing spaces */
80 for (end = buf + len - 1;
81 end > buf && isspace(*end);
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 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
107 * Build a list of visual line by wrapping the given line, assuming
108 * that when printed will have a leading prefix prfx.
111 wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
112 size_t width, int oneline)
114 const char *line, *space;
115 size_t ret, off, start, cur, prfxwidth;
118 if ((line = l->line) == NULL || *line == '\0')
119 return push_line(buffer, l, NULL, 0, 0);
121 prfxwidth = utf8_swidth(prfx);
126 if (l->type == LINE_LINK && emojify_link &&
127 emojied_line(l->line, &space)) {
128 prfxwidth = utf8_swidth_between(l->line, space);
133 for (off = 0; line[off] != '\0'; off += ret) {
136 ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
137 t = utf8_swidth_between(&line[off], &line[off + ret]);
139 if (cur + t <= width) {
144 if (!push_line(buffer, l, &line[start], off - start, flags))
150 flags = L_CONTINUATION;
156 return push_line(buffer, l, &line[start], off - start, flags);
161 wrap_page(struct buffer *buffer, int width)
164 const struct line *top_orig, *orig;
168 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
169 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
171 buffer->top_line = NULL;
172 buffer->current_line = NULL;
174 buffer->force_redraw = 1;
176 buffer->line_off = 0;
180 TAILQ_FOREACH(l, &buffer->page.head, lines) {
181 prfx = line_prefixes[l->type].prfx1;
192 case LINE_PRE_CONTENT:
195 case LINE_PATCH_HUNK_HDR:
198 wrap_text(buffer, prfx, l, MIN(fill_column, width),
202 case LINE_COMPL_CURRENT:
205 case LINE_DOWNLOAD_DONE:
206 case LINE_DOWNLOAD_INFO:
207 wrap_text(buffer, prfx, l, width, 1);
210 /* never, ever wrapped */
214 if (top_orig == l && buffer->top_line == NULL) {
215 buffer->line_off = buffer->line_max-1;
216 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
219 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
220 if (vl == NULL || vl->parent != orig)
222 buffer->top_line = vl;
227 if (orig == l && buffer->current_line == NULL) {
228 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
231 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
232 if (vl == NULL || vl->parent != orig)
234 buffer->current_line = vl;
239 if (buffer->current_line == NULL)
240 buffer->current_line = TAILQ_FIRST(&buffer->head);
242 if (buffer->top_line == NULL)
243 buffer->top_line = buffer->current_line;