2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 1ac119fb 2024-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 1ac119fb 2024-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 1ac119fb 2024-01-23 op * copyright notice and this permission notice appear in all copies.
8 1ac119fb 2024-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ac119fb 2024-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ac119fb 2024-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ac119fb 2024-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ac119fb 2024-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ac119fb 2024-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ac119fb 2024-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1ac119fb 2024-01-23 op #include "compat.h"
19 1ac119fb 2024-01-23 op #include <ctype.h>
20 1ac119fb 2024-01-23 op #include <stdint.h>
21 1ac119fb 2024-01-23 op #include <stdlib.h>
22 1ac119fb 2024-01-23 op #include <string.h>
24 1ac119fb 2024-01-23 op #include <grapheme.h>
26 1ac119fb 2024-01-23 op #include "defaults.h"
27 1ac119fb 2024-01-23 op #include "telescope.h"
28 1ac119fb 2024-01-23 op #include "utf8.h"
31 1ac119fb 2024-01-23 op erase_buffer(struct buffer *buffer)
33 1ac119fb 2024-01-23 op empty_vlist(buffer);
34 1ac119fb 2024-01-23 op empty_linelist(buffer);
38 1ac119fb 2024-01-23 op empty_linelist(struct buffer *buffer)
40 1ac119fb 2024-01-23 op struct line *l, *lt;
42 1ac119fb 2024-01-23 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
43 1ac119fb 2024-01-23 op TAILQ_REMOVE(&buffer->page.head, l, lines);
44 1ac119fb 2024-01-23 op free(l->line);
46 1ac119fb 2024-01-23 op if (l->type != LINE_COMPL &&
47 1ac119fb 2024-01-23 op l->type != LINE_COMPL_CURRENT &&
48 1ac119fb 2024-01-23 op l->type != LINE_HELP)
56 1ac119fb 2024-01-23 op empty_vlist(struct buffer *buffer)
58 1ac119fb 2024-01-23 op struct vline *vl, *t;
60 1ac119fb 2024-01-23 op buffer->top_line = NULL;
61 1ac119fb 2024-01-23 op buffer->line_off = 0;
62 1ac119fb 2024-01-23 op buffer->current_line = NULL;
63 1ac119fb 2024-01-23 op buffer->line_max = 0;
65 1ac119fb 2024-01-23 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
66 1ac119fb 2024-01-23 op TAILQ_REMOVE(&buffer->head, vl, vlines);
72 1ac119fb 2024-01-23 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
74 1ac119fb 2024-01-23 op struct vline *vl;
75 1ac119fb 2024-01-23 op const char *end;
77 1ac119fb 2024-01-23 op /* omit trailing spaces */
78 1ac119fb 2024-01-23 op if (len != 0) {
79 1ac119fb 2024-01-23 op for (end = buf + len - 1;
80 1ac119fb 2024-01-23 op end > buf && isspace(*end);
85 1ac119fb 2024-01-23 op if (!(l->flags & L_HIDDEN))
86 1ac119fb 2024-01-23 op buffer->line_max++;
88 1ac119fb 2024-01-23 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
91 1ac119fb 2024-01-23 op vl->parent = l;
92 1ac119fb 2024-01-23 op if (len != 0) {
93 1ac119fb 2024-01-23 op vl->from = buf - l->line;
94 1ac119fb 2024-01-23 op vl->len = len;
95 1ac119fb 2024-01-23 op vl->cplen = utf8_ncplen(buf, vl->len);
97 1ac119fb 2024-01-23 op vl->flags = flags;
99 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
104 1ac119fb 2024-01-23 op * Build a list of visual line by wrapping the given line, assuming
105 1ac119fb 2024-01-23 op * that when printed will have a leading prefix prfx.
108 1ac119fb 2024-01-23 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
109 1ac119fb 2024-01-23 op size_t width, int oneline)
111 1ac119fb 2024-01-23 op const char *line, *space;
112 1ac119fb 2024-01-23 op size_t ret, off, start, cur, prfxwidth;
115 1ac119fb 2024-01-23 op if ((line = l->line) == NULL || *line == '\0')
116 1ac119fb 2024-01-23 op return push_line(buffer, l, NULL, 0, 0);
118 1ac119fb 2024-01-23 op prfxwidth = utf8_swidth(prfx);
119 1ac119fb 2024-01-23 op cur = prfxwidth;
123 1ac119fb 2024-01-23 op if (l->type == LINE_LINK && emojify_link &&
124 1ac119fb 2024-01-23 op emojied_line(l->line, &space)) {
125 1ac119fb 2024-01-23 op prfxwidth = utf8_swidth_between(l->line, space);
126 1ac119fb 2024-01-23 op cur = prfxwidth;
127 1ac119fb 2024-01-23 op line = space + 1;
130 1ac119fb 2024-01-23 op for (off = 0; line[off] != '\0'; off += ret) {
133 1ac119fb 2024-01-23 op ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
134 1ac119fb 2024-01-23 op t = utf8_swidth_between(&line[off], &line[off + ret]);
136 1ac119fb 2024-01-23 op if (cur + t <= width) {
141 1ac119fb 2024-01-23 op if (!push_line(buffer, l, &line[start], off - start, flags))
147 1ac119fb 2024-01-23 op flags = L_CONTINUATION;
149 1ac119fb 2024-01-23 op cur = t + prfxwidth;
152 1ac119fb 2024-01-23 op if (off != start)
153 1ac119fb 2024-01-23 op return push_line(buffer, l, &line[start], off - start, flags);
158 1ac119fb 2024-01-23 op wrap_page(struct buffer *buffer, int width)
160 1ac119fb 2024-01-23 op struct line *l;
161 1ac119fb 2024-01-23 op const struct line *top_orig, *orig;
162 1ac119fb 2024-01-23 op struct vline *vl;
163 1ac119fb 2024-01-23 op const char *prfx;
165 1ac119fb 2024-01-23 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
166 1ac119fb 2024-01-23 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
168 1ac119fb 2024-01-23 op buffer->top_line = NULL;
169 1ac119fb 2024-01-23 op buffer->current_line = NULL;
171 1ac119fb 2024-01-23 op buffer->force_redraw = 1;
172 1ac119fb 2024-01-23 op buffer->curs_y = 0;
173 1ac119fb 2024-01-23 op buffer->line_off = 0;
175 1ac119fb 2024-01-23 op empty_vlist(buffer);
177 1ac119fb 2024-01-23 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
178 1ac119fb 2024-01-23 op prfx = line_prefixes[l->type].prfx1;
179 1ac119fb 2024-01-23 op switch (l->type) {
180 1ac119fb 2024-01-23 op case LINE_TEXT:
181 1ac119fb 2024-01-23 op case LINE_LINK:
182 1ac119fb 2024-01-23 op case LINE_TITLE_1:
183 1ac119fb 2024-01-23 op case LINE_TITLE_2:
184 1ac119fb 2024-01-23 op case LINE_TITLE_3:
185 1ac119fb 2024-01-23 op case LINE_ITEM:
186 1ac119fb 2024-01-23 op case LINE_QUOTE:
187 1ac119fb 2024-01-23 op case LINE_PRE_START:
188 1ac119fb 2024-01-23 op case LINE_PRE_END:
189 1ac119fb 2024-01-23 op case LINE_PRE_CONTENT:
190 1ac119fb 2024-01-23 op case LINE_PATCH:
191 1ac119fb 2024-01-23 op case LINE_PATCH_HDR:
192 1ac119fb 2024-01-23 op case LINE_PATCH_HUNK_HDR:
193 1ac119fb 2024-01-23 op case LINE_PATCH_ADD:
194 1ac119fb 2024-01-23 op case LINE_PATCH_DEL:
195 1ac119fb 2024-01-23 op wrap_text(buffer, prfx, l, MIN(fill_column, width),
198 1ac119fb 2024-01-23 op case LINE_COMPL:
199 1ac119fb 2024-01-23 op case LINE_COMPL_CURRENT:
200 1ac119fb 2024-01-23 op case LINE_HELP:
201 1ac119fb 2024-01-23 op case LINE_DOWNLOAD:
202 1ac119fb 2024-01-23 op case LINE_DOWNLOAD_DONE:
203 1ac119fb 2024-01-23 op case LINE_DOWNLOAD_INFO:
204 1ac119fb 2024-01-23 op wrap_text(buffer, prfx, l, width, 1);
206 1ac119fb 2024-01-23 op case LINE_FRINGE:
207 1ac119fb 2024-01-23 op /* never, ever wrapped */
211 1ac119fb 2024-01-23 op if (top_orig == l && buffer->top_line == NULL) {
212 1ac119fb 2024-01-23 op buffer->line_off = buffer->line_max-1;
213 1ac119fb 2024-01-23 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
216 1ac119fb 2024-01-23 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
217 1ac119fb 2024-01-23 op if (vl == NULL || vl->parent != orig)
219 1ac119fb 2024-01-23 op buffer->top_line = vl;
220 1ac119fb 2024-01-23 op buffer->line_off--;
224 1ac119fb 2024-01-23 op if (orig == l && buffer->current_line == NULL) {
225 1ac119fb 2024-01-23 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
228 1ac119fb 2024-01-23 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
229 1ac119fb 2024-01-23 op if (vl == NULL || vl->parent != orig)
231 1ac119fb 2024-01-23 op buffer->current_line = vl;
236 1ac119fb 2024-01-23 op if (buffer->current_line == NULL)
237 1ac119fb 2024-01-23 op buffer->current_line = TAILQ_FIRST(&buffer->head);
239 1ac119fb 2024-01-23 op if (buffer->top_line == NULL)
240 1ac119fb 2024-01-23 op buffer->top_line = buffer->current_line;