Blame


1 1ac119fb 2024-01-23 op /*
2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 1ac119fb 2024-01-23 op *
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.
7 1ac119fb 2024-01-23 op *
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.
15 1ac119fb 2024-01-23 op */
16 1ac119fb 2024-01-23 op
17 1ac119fb 2024-01-23 op #include "compat.h"
18 1ac119fb 2024-01-23 op
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>
23 1ac119fb 2024-01-23 op
24 1ac119fb 2024-01-23 op #include <grapheme.h>
25 1ac119fb 2024-01-23 op
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 1ac119fb 2024-01-23 op
30 1ac119fb 2024-01-23 op void
31 1ac119fb 2024-01-23 op erase_buffer(struct buffer *buffer)
32 1ac119fb 2024-01-23 op {
33 1ac119fb 2024-01-23 op empty_vlist(buffer);
34 1ac119fb 2024-01-23 op empty_linelist(buffer);
35 1ac119fb 2024-01-23 op }
36 1ac119fb 2024-01-23 op
37 1ac119fb 2024-01-23 op void
38 1ac119fb 2024-01-23 op empty_linelist(struct buffer *buffer)
39 1ac119fb 2024-01-23 op {
40 1ac119fb 2024-01-23 op struct line *l, *lt;
41 1ac119fb 2024-01-23 op
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);
45 1ac119fb 2024-01-23 op
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)
49 1ac119fb 2024-01-23 op free(l->alt);
50 1ac119fb 2024-01-23 op
51 1ac119fb 2024-01-23 op free(l);
52 1ac119fb 2024-01-23 op }
53 1ac119fb 2024-01-23 op }
54 1ac119fb 2024-01-23 op
55 1ac119fb 2024-01-23 op void
56 1ac119fb 2024-01-23 op empty_vlist(struct buffer *buffer)
57 1ac119fb 2024-01-23 op {
58 1ac119fb 2024-01-23 op struct vline *vl, *t;
59 1ac119fb 2024-01-23 op
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;
64 1ac119fb 2024-01-23 op
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);
67 1ac119fb 2024-01-23 op free(vl);
68 1ac119fb 2024-01-23 op }
69 1ac119fb 2024-01-23 op }
70 1ac119fb 2024-01-23 op
71 1ac119fb 2024-01-23 op static int
72 1ac119fb 2024-01-23 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
73 1ac119fb 2024-01-23 op {
74 1ac119fb 2024-01-23 op struct vline *vl;
75 1ac119fb 2024-01-23 op const char *end;
76 1ac119fb 2024-01-23 op
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);
81 1ac119fb 2024-01-23 op end--, len--)
82 1ac119fb 2024-01-23 op ; /* nop */
83 1ac119fb 2024-01-23 op }
84 1ac119fb 2024-01-23 op
85 1ac119fb 2024-01-23 op if (!(l->flags & L_HIDDEN))
86 1ac119fb 2024-01-23 op buffer->line_max++;
87 1ac119fb 2024-01-23 op
88 1ac119fb 2024-01-23 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
89 1ac119fb 2024-01-23 op return 0;
90 1ac119fb 2024-01-23 op
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);
96 1ac119fb 2024-01-23 op }
97 1ac119fb 2024-01-23 op vl->flags = flags;
98 1ac119fb 2024-01-23 op
99 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
100 1ac119fb 2024-01-23 op return 1;
101 1ac119fb 2024-01-23 op }
102 1ac119fb 2024-01-23 op
103 1ac119fb 2024-01-23 op /*
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.
106 1ac119fb 2024-01-23 op */
107 1ac119fb 2024-01-23 op int
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)
110 1ac119fb 2024-01-23 op {
111 1ac119fb 2024-01-23 op const char *line, *space;
112 1ac119fb 2024-01-23 op size_t ret, off, start, cur, prfxwidth;
113 1ac119fb 2024-01-23 op int flags;
114 1ac119fb 2024-01-23 op
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);
117 1ac119fb 2024-01-23 op
118 1ac119fb 2024-01-23 op prfxwidth = utf8_swidth(prfx);
119 1ac119fb 2024-01-23 op cur = prfxwidth;
120 1ac119fb 2024-01-23 op start = 0;
121 1ac119fb 2024-01-23 op flags = 0;
122 1ac119fb 2024-01-23 op
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;
128 1ac119fb 2024-01-23 op }
129 1ac119fb 2024-01-23 op
130 1ac119fb 2024-01-23 op for (off = 0; line[off] != '\0'; off += ret) {
131 1ac119fb 2024-01-23 op size_t t;
132 1ac119fb 2024-01-23 op
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]);
135 1ac119fb 2024-01-23 op
136 1ac119fb 2024-01-23 op if (cur + t <= width) {
137 1ac119fb 2024-01-23 op cur += t;
138 1ac119fb 2024-01-23 op continue;
139 1ac119fb 2024-01-23 op }
140 1ac119fb 2024-01-23 op
141 1ac119fb 2024-01-23 op if (!push_line(buffer, l, &line[start], off - start, flags))
142 1ac119fb 2024-01-23 op return 0;
143 1ac119fb 2024-01-23 op
144 1ac119fb 2024-01-23 op if (oneline)
145 1ac119fb 2024-01-23 op return 0;
146 1ac119fb 2024-01-23 op
147 1ac119fb 2024-01-23 op flags = L_CONTINUATION;
148 1ac119fb 2024-01-23 op start = off;
149 1ac119fb 2024-01-23 op cur = t + prfxwidth;
150 1ac119fb 2024-01-23 op }
151 1ac119fb 2024-01-23 op
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);
154 1ac119fb 2024-01-23 op return 0;
155 1ac119fb 2024-01-23 op }
156 1ac119fb 2024-01-23 op
157 1ac119fb 2024-01-23 op int
158 1ac119fb 2024-01-23 op wrap_page(struct buffer *buffer, int width)
159 1ac119fb 2024-01-23 op {
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;
164 1ac119fb 2024-01-23 op
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;
167 1ac119fb 2024-01-23 op
168 1ac119fb 2024-01-23 op buffer->top_line = NULL;
169 1ac119fb 2024-01-23 op buffer->current_line = NULL;
170 1ac119fb 2024-01-23 op
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;
174 1ac119fb 2024-01-23 op
175 1ac119fb 2024-01-23 op empty_vlist(buffer);
176 1ac119fb 2024-01-23 op
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),
196 1ac119fb 2024-01-23 op 0);
197 1ac119fb 2024-01-23 op break;
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);
205 1ac119fb 2024-01-23 op break;
206 1ac119fb 2024-01-23 op case LINE_FRINGE:
207 1ac119fb 2024-01-23 op /* never, ever wrapped */
208 1ac119fb 2024-01-23 op break;
209 1ac119fb 2024-01-23 op }
210 1ac119fb 2024-01-23 op
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);
214 1ac119fb 2024-01-23 op
215 1ac119fb 2024-01-23 op while (1) {
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)
218 1ac119fb 2024-01-23 op break;
219 1ac119fb 2024-01-23 op buffer->top_line = vl;
220 1ac119fb 2024-01-23 op buffer->line_off--;
221 1ac119fb 2024-01-23 op }
222 1ac119fb 2024-01-23 op }
223 1ac119fb 2024-01-23 op
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);
226 1ac119fb 2024-01-23 op
227 1ac119fb 2024-01-23 op while (1) {
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)
230 1ac119fb 2024-01-23 op break;
231 1ac119fb 2024-01-23 op buffer->current_line = vl;
232 1ac119fb 2024-01-23 op }
233 1ac119fb 2024-01-23 op }
234 1ac119fb 2024-01-23 op }
235 1ac119fb 2024-01-23 op
236 1ac119fb 2024-01-23 op if (buffer->current_line == NULL)
237 1ac119fb 2024-01-23 op buffer->current_line = TAILQ_FIRST(&buffer->head);
238 1ac119fb 2024-01-23 op
239 1ac119fb 2024-01-23 op if (buffer->top_line == NULL)
240 1ac119fb 2024-01-23 op buffer->top_line = buffer->current_line;
241 1ac119fb 2024-01-23 op
242 1ac119fb 2024-01-23 op return 1;
243 1ac119fb 2024-01-23 op }