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 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
30 1ac119fb 2024-01-23 op
31 1ac119fb 2024-01-23 op void
32 1ac119fb 2024-01-23 op erase_buffer(struct buffer *buffer)
33 1ac119fb 2024-01-23 op {
34 1ac119fb 2024-01-23 op empty_vlist(buffer);
35 1ac119fb 2024-01-23 op empty_linelist(buffer);
36 1ac119fb 2024-01-23 op }
37 1ac119fb 2024-01-23 op
38 1ac119fb 2024-01-23 op void
39 1ac119fb 2024-01-23 op empty_linelist(struct buffer *buffer)
40 1ac119fb 2024-01-23 op {
41 1ac119fb 2024-01-23 op struct line *l, *lt;
42 1ac119fb 2024-01-23 op
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);
46 1ac119fb 2024-01-23 op
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)
50 1ac119fb 2024-01-23 op free(l->alt);
51 1ac119fb 2024-01-23 op
52 1ac119fb 2024-01-23 op free(l);
53 1ac119fb 2024-01-23 op }
54 1ac119fb 2024-01-23 op }
55 1ac119fb 2024-01-23 op
56 1ac119fb 2024-01-23 op void
57 1ac119fb 2024-01-23 op empty_vlist(struct buffer *buffer)
58 1ac119fb 2024-01-23 op {
59 1ac119fb 2024-01-23 op struct vline *vl, *t;
60 1ac119fb 2024-01-23 op
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;
65 1ac119fb 2024-01-23 op
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);
68 1ac119fb 2024-01-23 op free(vl);
69 1ac119fb 2024-01-23 op }
70 1ac119fb 2024-01-23 op }
71 1ac119fb 2024-01-23 op
72 1ac119fb 2024-01-23 op static int
73 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 {
75 1ac119fb 2024-01-23 op struct vline *vl;
76 1ac119fb 2024-01-23 op const char *end;
77 1ac119fb 2024-01-23 op
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);
82 1ac119fb 2024-01-23 op end--, len--)
83 1ac119fb 2024-01-23 op ; /* nop */
84 1ac119fb 2024-01-23 op }
85 1ac119fb 2024-01-23 op
86 1ac119fb 2024-01-23 op if (!(l->flags & L_HIDDEN))
87 1ac119fb 2024-01-23 op buffer->line_max++;
88 1ac119fb 2024-01-23 op
89 3d89457c 2024-06-18 thomas.ad vl = xcalloc(1, sizeof(*vl));
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 }
96 1ac119fb 2024-01-23 op vl->flags = flags;
97 1ac119fb 2024-01-23 op
98 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&buffer->vhead, vl, vlines);
99 1ac119fb 2024-01-23 op return 1;
100 1ac119fb 2024-01-23 op }
101 1ac119fb 2024-01-23 op
102 1ac119fb 2024-01-23 op /*
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.
105 1ac119fb 2024-01-23 op */
106 1ac119fb 2024-01-23 op int
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)
109 1ac119fb 2024-01-23 op {
110 1ac119fb 2024-01-23 op const char *line, *space;
111 1ac119fb 2024-01-23 op size_t ret, off, start, cur, prfxwidth;
112 1ac119fb 2024-01-23 op int flags;
113 1ac119fb 2024-01-23 op
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);
116 1ac119fb 2024-01-23 op
117 f853ec6f 2024-10-22 op prfxwidth = utf8_swidth(prfx, base_offset);
118 f853ec6f 2024-10-22 op cur = base_offset + prfxwidth;
119 1ac119fb 2024-01-23 op start = 0;
120 1ac119fb 2024-01-23 op flags = 0;
121 1ac119fb 2024-01-23 op
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;
127 1ac119fb 2024-01-23 op }
128 1ac119fb 2024-01-23 op
129 1ac119fb 2024-01-23 op for (off = 0; line[off] != '\0'; off += ret) {
130 1ac119fb 2024-01-23 op size_t t;
131 1ac119fb 2024-01-23 op
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);
135 1ac119fb 2024-01-23 op
136 7c9e12df 2024-10-20 op /* we can't reach the last column */
137 7c9e12df 2024-10-20 op if (cur + t < width) {
138 1ac119fb 2024-01-23 op cur += t;
139 1ac119fb 2024-01-23 op continue;
140 1ac119fb 2024-01-23 op }
141 1ac119fb 2024-01-23 op
142 1ac119fb 2024-01-23 op if (!push_line(buffer, l, &line[start], off - start, flags))
143 1ac119fb 2024-01-23 op return 0;
144 1ac119fb 2024-01-23 op
145 1ac119fb 2024-01-23 op if (oneline)
146 1ac119fb 2024-01-23 op return 0;
147 1ac119fb 2024-01-23 op
148 1ac119fb 2024-01-23 op flags = L_CONTINUATION;
149 1ac119fb 2024-01-23 op start = off;
150 f853ec6f 2024-10-22 op cur = base_offset + prfxwidth + t;
151 1ac119fb 2024-01-23 op }
152 1ac119fb 2024-01-23 op
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);
155 1ac119fb 2024-01-23 op return 0;
156 1ac119fb 2024-01-23 op }
157 1ac119fb 2024-01-23 op
158 1ac119fb 2024-01-23 op int
159 f853ec6f 2024-10-22 op wrap_page(struct buffer *buffer, int width, int x_offset)
160 1ac119fb 2024-01-23 op {
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;
165 1ac119fb 2024-01-23 op
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;
168 1ac119fb 2024-01-23 op
169 1ac119fb 2024-01-23 op buffer->top_line = NULL;
170 1ac119fb 2024-01-23 op buffer->current_line = NULL;
171 1ac119fb 2024-01-23 op
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;
175 1ac119fb 2024-01-23 op
176 1ac119fb 2024-01-23 op empty_vlist(buffer);
177 1ac119fb 2024-01-23 op
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);
198 1ac119fb 2024-01-23 op break;
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);
206 1ac119fb 2024-01-23 op break;
207 1ac119fb 2024-01-23 op case LINE_FRINGE:
208 1ac119fb 2024-01-23 op /* never, ever wrapped */
209 1ac119fb 2024-01-23 op break;
210 1ac119fb 2024-01-23 op }
211 1ac119fb 2024-01-23 op
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);
215 1ac119fb 2024-01-23 op
216 1ac119fb 2024-01-23 op while (1) {
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)
219 1ac119fb 2024-01-23 op break;
220 1ac119fb 2024-01-23 op buffer->top_line = vl;
221 1ac119fb 2024-01-23 op buffer->line_off--;
222 1ac119fb 2024-01-23 op }
223 1ac119fb 2024-01-23 op }
224 1ac119fb 2024-01-23 op
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);
227 1ac119fb 2024-01-23 op
228 1ac119fb 2024-01-23 op while (1) {
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)
231 1ac119fb 2024-01-23 op break;
232 1ac119fb 2024-01-23 op buffer->current_line = vl;
233 1ac119fb 2024-01-23 op }
234 1ac119fb 2024-01-23 op }
235 1ac119fb 2024-01-23 op }
236 1ac119fb 2024-01-23 op
237 1ac119fb 2024-01-23 op if (buffer->current_line == NULL)
238 c1d27b0e 2024-06-14 op buffer->current_line = TAILQ_FIRST(&buffer->vhead);
239 1ac119fb 2024-01-23 op
240 1ac119fb 2024-01-23 op if (buffer->top_line == NULL)
241 1ac119fb 2024-01-23 op buffer->top_line = buffer->current_line;
242 1ac119fb 2024-01-23 op
243 1ac119fb 2024-01-23 op return 1;
244 1ac119fb 2024-01-23 op }