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"
29 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
32 1ac119fb 2024-01-23 op erase_buffer(struct buffer *buffer)
34 1ac119fb 2024-01-23 op empty_vlist(buffer);
35 1ac119fb 2024-01-23 op empty_linelist(buffer);
39 1ac119fb 2024-01-23 op empty_linelist(struct buffer *buffer)
41 1ac119fb 2024-01-23 op struct line *l, *lt;
43 c1d27b0e 2024-06-14 op TAILQ_FOREACH_SAFE(l, &buffer->head, lines, lt) {
44 c1d27b0e 2024-06-14 op TAILQ_REMOVE(&buffer->head, l, lines);
45 1ac119fb 2024-01-23 op free(l->line);
47 1ac119fb 2024-01-23 op if (l->type != LINE_COMPL &&
48 1ac119fb 2024-01-23 op l->type != LINE_COMPL_CURRENT &&
49 1ac119fb 2024-01-23 op l->type != LINE_HELP)
57 1ac119fb 2024-01-23 op empty_vlist(struct buffer *buffer)
59 1ac119fb 2024-01-23 op struct vline *vl, *t;
61 1ac119fb 2024-01-23 op buffer->top_line = NULL;
62 1ac119fb 2024-01-23 op buffer->line_off = 0;
63 1ac119fb 2024-01-23 op buffer->current_line = NULL;
64 1ac119fb 2024-01-23 op buffer->line_max = 0;
66 c1d27b0e 2024-06-14 op TAILQ_FOREACH_SAFE(vl, &buffer->vhead, vlines, t) {
67 c1d27b0e 2024-06-14 op TAILQ_REMOVE(&buffer->vhead, vl, vlines);
73 1ac119fb 2024-01-23 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
75 1ac119fb 2024-01-23 op struct vline *vl;
76 1ac119fb 2024-01-23 op const char *end;
78 1ac119fb 2024-01-23 op /* omit trailing spaces */
79 1ac119fb 2024-01-23 op if (len != 0) {
80 1ac119fb 2024-01-23 op for (end = buf + len - 1;
81 1ac119fb 2024-01-23 op end > buf && isspace(*end);
86 1ac119fb 2024-01-23 op if (!(l->flags & L_HIDDEN))
87 1ac119fb 2024-01-23 op buffer->line_max++;
89 3d89457c 2024-06-18 thomas.ad vl = xcalloc(1, sizeof(*vl));
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;
96 1ac119fb 2024-01-23 op vl->flags = flags;
98 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&buffer->vhead, vl, vlines);
103 1ac119fb 2024-01-23 op * Build a list of visual line by wrapping the given line, assuming
104 1ac119fb 2024-01-23 op * that when printed will have a leading prefix prfx.
107 1ac119fb 2024-01-23 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
108 f853ec6f 2024-10-22 op size_t width, int base_offset, int oneline)
110 1ac119fb 2024-01-23 op const char *line, *space;
111 1ac119fb 2024-01-23 op size_t ret, off, start, cur, prfxwidth;
114 1ac119fb 2024-01-23 op if ((line = l->line) == NULL || *line == '\0')
115 1ac119fb 2024-01-23 op return push_line(buffer, l, NULL, 0, 0);
117 f853ec6f 2024-10-22 op prfxwidth = utf8_swidth(prfx, base_offset);
118 f853ec6f 2024-10-22 op cur = base_offset + prfxwidth;
122 1ac119fb 2024-01-23 op if (l->type == LINE_LINK && emojify_link &&
123 1ac119fb 2024-01-23 op emojied_line(l->line, &space)) {
124 f853ec6f 2024-10-22 op prfxwidth = utf8_swidth_between(l->line, space, base_offset);
125 f853ec6f 2024-10-22 op cur = base_offset + prfxwidth;
126 1ac119fb 2024-01-23 op line = space + 1;
129 1ac119fb 2024-01-23 op for (off = 0; line[off] != '\0'; off += ret) {
132 1ac119fb 2024-01-23 op ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
133 f853ec6f 2024-10-22 op t = utf8_swidth_between(&line[off], &line[off + ret],
134 f853ec6f 2024-10-22 op base_offset);
136 7c9e12df 2024-10-20 op /* we can't reach the last column */
137 7c9e12df 2024-10-20 op if (cur + t < width) {
142 1ac119fb 2024-01-23 op if (!push_line(buffer, l, &line[start], off - start, flags))
148 1ac119fb 2024-01-23 op flags = L_CONTINUATION;
150 f853ec6f 2024-10-22 op cur = base_offset + prfxwidth + t;
153 1ac119fb 2024-01-23 op if (off != start)
154 1ac119fb 2024-01-23 op return push_line(buffer, l, &line[start], off - start, flags);
159 f853ec6f 2024-10-22 op wrap_page(struct buffer *buffer, int width, int x_offset)
161 1ac119fb 2024-01-23 op struct line *l;
162 1ac119fb 2024-01-23 op const struct line *top_orig, *orig;
163 1ac119fb 2024-01-23 op struct vline *vl;
164 1ac119fb 2024-01-23 op const char *prfx;
166 1ac119fb 2024-01-23 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
167 1ac119fb 2024-01-23 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
169 1ac119fb 2024-01-23 op buffer->top_line = NULL;
170 1ac119fb 2024-01-23 op buffer->current_line = NULL;
172 1ac119fb 2024-01-23 op buffer->force_redraw = 1;
173 1ac119fb 2024-01-23 op buffer->curs_y = 0;
174 1ac119fb 2024-01-23 op buffer->line_off = 0;
176 1ac119fb 2024-01-23 op empty_vlist(buffer);
178 c1d27b0e 2024-06-14 op TAILQ_FOREACH(l, &buffer->head, lines) {
179 1ac119fb 2024-01-23 op prfx = line_prefixes[l->type].prfx1;
180 1ac119fb 2024-01-23 op switch (l->type) {
181 1ac119fb 2024-01-23 op case LINE_TEXT:
182 1ac119fb 2024-01-23 op case LINE_LINK:
183 1ac119fb 2024-01-23 op case LINE_TITLE_1:
184 1ac119fb 2024-01-23 op case LINE_TITLE_2:
185 1ac119fb 2024-01-23 op case LINE_TITLE_3:
186 1ac119fb 2024-01-23 op case LINE_ITEM:
187 1ac119fb 2024-01-23 op case LINE_QUOTE:
188 1ac119fb 2024-01-23 op case LINE_PRE_START:
189 1ac119fb 2024-01-23 op case LINE_PRE_END:
190 1ac119fb 2024-01-23 op case LINE_PRE_CONTENT:
191 1ac119fb 2024-01-23 op case LINE_PATCH:
192 1ac119fb 2024-01-23 op case LINE_PATCH_HDR:
193 1ac119fb 2024-01-23 op case LINE_PATCH_HUNK_HDR:
194 1ac119fb 2024-01-23 op case LINE_PATCH_ADD:
195 1ac119fb 2024-01-23 op case LINE_PATCH_DEL:
196 1ac119fb 2024-01-23 op wrap_text(buffer, prfx, l, MIN(fill_column, width),
197 f853ec6f 2024-10-22 op x_offset, 0);
199 1ac119fb 2024-01-23 op case LINE_COMPL:
200 1ac119fb 2024-01-23 op case LINE_COMPL_CURRENT:
201 1ac119fb 2024-01-23 op case LINE_HELP:
202 1ac119fb 2024-01-23 op case LINE_DOWNLOAD:
203 1ac119fb 2024-01-23 op case LINE_DOWNLOAD_DONE:
204 1ac119fb 2024-01-23 op case LINE_DOWNLOAD_INFO:
205 f853ec6f 2024-10-22 op wrap_text(buffer, prfx, l, width, x_offset, 1);
207 1ac119fb 2024-01-23 op case LINE_FRINGE:
208 1ac119fb 2024-01-23 op /* never, ever wrapped */
212 1ac119fb 2024-01-23 op if (top_orig == l && buffer->top_line == NULL) {
213 1ac119fb 2024-01-23 op buffer->line_off = buffer->line_max-1;
214 c1d27b0e 2024-06-14 op buffer->top_line = TAILQ_LAST(&buffer->vhead, vhead);
217 1ac119fb 2024-01-23 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
218 1ac119fb 2024-01-23 op if (vl == NULL || vl->parent != orig)
220 1ac119fb 2024-01-23 op buffer->top_line = vl;
221 1ac119fb 2024-01-23 op buffer->line_off--;
225 1ac119fb 2024-01-23 op if (orig == l && buffer->current_line == NULL) {
226 c1d27b0e 2024-06-14 op buffer->current_line = TAILQ_LAST(&buffer->vhead, vhead);
229 1ac119fb 2024-01-23 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
230 1ac119fb 2024-01-23 op if (vl == NULL || vl->parent != orig)
232 1ac119fb 2024-01-23 op buffer->current_line = vl;
237 1ac119fb 2024-01-23 op if (buffer->current_line == NULL)
238 c1d27b0e 2024-06-14 op buffer->current_line = TAILQ_FIRST(&buffer->vhead);
240 1ac119fb 2024-01-23 op if (buffer->top_line == NULL)
241 1ac119fb 2024-01-23 op buffer->top_line = buffer->current_line;