Blame


1 4cd67caa 2022-02-09 op /*
2 4cd67caa 2022-02-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 4cd67caa 2022-02-09 op *
4 4cd67caa 2022-02-09 op * Permission to use, copy, modify, and distribute this software for any
5 4cd67caa 2022-02-09 op * purpose with or without fee is hereby granted, provided that the above
6 4cd67caa 2022-02-09 op * copyright notice and this permission notice appear in all copies.
7 4cd67caa 2022-02-09 op *
8 4cd67caa 2022-02-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4cd67caa 2022-02-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4cd67caa 2022-02-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4cd67caa 2022-02-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4cd67caa 2022-02-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4cd67caa 2022-02-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4cd67caa 2022-02-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 4cd67caa 2022-02-09 op */
16 4cd67caa 2022-02-09 op
17 4cd67caa 2022-02-09 op #include "compat.h"
18 4cd67caa 2022-02-09 op
19 4cd67caa 2022-02-09 op #include <ctype.h>
20 d5e0ee08 2022-10-09 op #include <stdint.h>
21 4cd67caa 2022-02-09 op #include <stdlib.h>
22 4cd67caa 2022-02-09 op #include <string.h>
23 4cd67caa 2022-02-09 op
24 d5e0ee08 2022-10-09 op #include <grapheme.h>
25 d5e0ee08 2022-10-09 op
26 4cd67caa 2022-02-09 op #include "defaults.h"
27 4cd67caa 2022-02-09 op #include "telescope.h"
28 4cd67caa 2022-02-09 op #include "utf8.h"
29 4cd67caa 2022-02-09 op
30 4cd67caa 2022-02-09 op void
31 4cd67caa 2022-02-09 op erase_buffer(struct buffer *buffer)
32 4cd67caa 2022-02-09 op {
33 4cd67caa 2022-02-09 op empty_vlist(buffer);
34 4cd67caa 2022-02-09 op empty_linelist(buffer);
35 4cd67caa 2022-02-09 op }
36 4cd67caa 2022-02-09 op
37 4cd67caa 2022-02-09 op void
38 4cd67caa 2022-02-09 op empty_linelist(struct buffer *buffer)
39 4cd67caa 2022-02-09 op {
40 4cd67caa 2022-02-09 op struct line *l, *lt;
41 4cd67caa 2022-02-09 op
42 4cd67caa 2022-02-09 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
43 4cd67caa 2022-02-09 op TAILQ_REMOVE(&buffer->page.head, l, lines);
44 4cd67caa 2022-02-09 op free(l->line);
45 4cd67caa 2022-02-09 op
46 4cd67caa 2022-02-09 op if (l->type != LINE_COMPL &&
47 4cd67caa 2022-02-09 op l->type != LINE_COMPL_CURRENT &&
48 4cd67caa 2022-02-09 op l->type != LINE_HELP)
49 4cd67caa 2022-02-09 op free(l->alt);
50 4cd67caa 2022-02-09 op
51 4cd67caa 2022-02-09 op free(l);
52 4cd67caa 2022-02-09 op }
53 4cd67caa 2022-02-09 op }
54 4cd67caa 2022-02-09 op
55 4cd67caa 2022-02-09 op void
56 4cd67caa 2022-02-09 op empty_vlist(struct buffer *buffer)
57 4cd67caa 2022-02-09 op {
58 4cd67caa 2022-02-09 op struct vline *vl, *t;
59 4cd67caa 2022-02-09 op
60 4cd67caa 2022-02-09 op buffer->top_line = NULL;
61 4cd67caa 2022-02-09 op buffer->line_off = 0;
62 4cd67caa 2022-02-09 op buffer->current_line = NULL;
63 4cd67caa 2022-02-09 op buffer->line_max = 0;
64 4cd67caa 2022-02-09 op
65 4cd67caa 2022-02-09 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
66 4cd67caa 2022-02-09 op TAILQ_REMOVE(&buffer->head, vl, vlines);
67 4cd67caa 2022-02-09 op free(vl);
68 4cd67caa 2022-02-09 op }
69 4cd67caa 2022-02-09 op }
70 4cd67caa 2022-02-09 op
71 4cd67caa 2022-02-09 op static int
72 4cd67caa 2022-02-09 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
73 4cd67caa 2022-02-09 op {
74 4cd67caa 2022-02-09 op struct vline *vl;
75 4cd67caa 2022-02-09 op const char *end;
76 4cd67caa 2022-02-09 op
77 4cd67caa 2022-02-09 op /* omit trailing spaces */
78 4cd67caa 2022-02-09 op if (len != 0) {
79 4cd67caa 2022-02-09 op for (end = buf + len - 1;
80 4cd67caa 2022-02-09 op end > buf && isspace(*end);
81 4cd67caa 2022-02-09 op end--, len--)
82 4cd67caa 2022-02-09 op ; /* nop */
83 4cd67caa 2022-02-09 op }
84 4cd67caa 2022-02-09 op
85 4cd67caa 2022-02-09 op if (!(l->flags & L_HIDDEN))
86 4cd67caa 2022-02-09 op buffer->line_max++;
87 4cd67caa 2022-02-09 op
88 4cd67caa 2022-02-09 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
89 4cd67caa 2022-02-09 op return 0;
90 4cd67caa 2022-02-09 op
91 4cd67caa 2022-02-09 op vl->parent = l;
92 bd4a08a7 2022-11-07 op if (len != 0) {
93 bd4a08a7 2022-11-07 op vl->from = buf - l->line;
94 bd4a08a7 2022-11-07 op vl->len = len;
95 bd4a08a7 2022-11-07 op vl->cplen = utf8_ncplen(buf, vl->len);
96 bd4a08a7 2022-11-07 op }
97 4cd67caa 2022-02-09 op vl->flags = flags;
98 4cd67caa 2022-02-09 op
99 4cd67caa 2022-02-09 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
100 4cd67caa 2022-02-09 op return 1;
101 4cd67caa 2022-02-09 op }
102 4cd67caa 2022-02-09 op
103 4cd67caa 2022-02-09 op /*
104 4cd67caa 2022-02-09 op * Build a list of visual line by wrapping the given line, assuming
105 4cd67caa 2022-02-09 op * that when printed will have a leading prefix prfx.
106 4cd67caa 2022-02-09 op */
107 4cd67caa 2022-02-09 op int
108 6b91e104 2022-10-09 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
109 6b91e104 2022-10-09 op size_t width, int oneline)
110 4cd67caa 2022-02-09 op {
111 d5e0ee08 2022-10-09 op const char *line, *space;
112 d5e0ee08 2022-10-09 op size_t ret, off, start, cur, prfxwidth;
113 4cd67caa 2022-02-09 op int flags;
114 4cd67caa 2022-02-09 op
115 d5e0ee08 2022-10-09 op if ((line = l->line) == NULL || *line == '\0')
116 4cd67caa 2022-02-09 op return push_line(buffer, l, NULL, 0, 0);
117 4cd67caa 2022-02-09 op
118 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth(prfx);
119 4cd67caa 2022-02-09 op cur = prfxwidth;
120 d5e0ee08 2022-10-09 op start = 0;
121 4cd67caa 2022-02-09 op flags = 0;
122 4cd67caa 2022-02-09 op
123 4cd67caa 2022-02-09 op if (l->type == LINE_LINK && emojify_link &&
124 4cd67caa 2022-02-09 op emojied_line(l->line, &space)) {
125 bd4a08a7 2022-11-07 op prfxwidth = utf8_swidth_between(l->line, space);
126 4cd67caa 2022-02-09 op cur = prfxwidth;
127 4cd67caa 2022-02-09 op line = space + 1;
128 4cd67caa 2022-02-09 op }
129 4cd67caa 2022-02-09 op
130 d5e0ee08 2022-10-09 op for (off = 0; line[off] != '\0'; off += ret) {
131 d5e0ee08 2022-10-09 op size_t t;
132 4cd67caa 2022-02-09 op
133 d5e0ee08 2022-10-09 op ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
134 d5e0ee08 2022-10-09 op t = utf8_swidth_between(&line[off], &line[off + ret]);
135 4cd67caa 2022-02-09 op
136 d5e0ee08 2022-10-09 op if (cur + t <= width) {
137 d5e0ee08 2022-10-09 op cur += t;
138 4cd67caa 2022-02-09 op continue;
139 4cd67caa 2022-02-09 op }
140 4cd67caa 2022-02-09 op
141 d5e0ee08 2022-10-09 op if (!push_line(buffer, l, &line[start], off - start, flags))
142 d5e0ee08 2022-10-09 op return 0;
143 d5e0ee08 2022-10-09 op
144 6b91e104 2022-10-09 op if (oneline)
145 6b91e104 2022-10-09 op return 0;
146 6b91e104 2022-10-09 op
147 d5e0ee08 2022-10-09 op flags = L_CONTINUATION;
148 d5e0ee08 2022-10-09 op start = off;
149 b0266e30 2022-11-02 op cur = t + prfxwidth;
150 4cd67caa 2022-02-09 op }
151 4cd67caa 2022-02-09 op
152 d5e0ee08 2022-10-09 op if (off != start)
153 d5e0ee08 2022-10-09 op return push_line(buffer, l, &line[start], off - start, flags);
154 d5e0ee08 2022-10-09 op return 0;
155 4cd67caa 2022-02-09 op }
156 4cd67caa 2022-02-09 op
157 4cd67caa 2022-02-09 op int
158 4cd67caa 2022-02-09 op wrap_page(struct buffer *buffer, int width)
159 4cd67caa 2022-02-09 op {
160 4cd67caa 2022-02-09 op struct line *l;
161 4cd67caa 2022-02-09 op const struct line *top_orig, *orig;
162 4cd67caa 2022-02-09 op struct vline *vl;
163 4cd67caa 2022-02-09 op const char *prfx;
164 4cd67caa 2022-02-09 op
165 4cd67caa 2022-02-09 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
166 4cd67caa 2022-02-09 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
167 4cd67caa 2022-02-09 op
168 4cd67caa 2022-02-09 op buffer->top_line = NULL;
169 4cd67caa 2022-02-09 op buffer->current_line = NULL;
170 4cd67caa 2022-02-09 op
171 4cd67caa 2022-02-09 op buffer->force_redraw = 1;
172 4cd67caa 2022-02-09 op buffer->curs_y = 0;
173 4cd67caa 2022-02-09 op buffer->line_off = 0;
174 4cd67caa 2022-02-09 op
175 4cd67caa 2022-02-09 op empty_vlist(buffer);
176 4cd67caa 2022-02-09 op
177 4cd67caa 2022-02-09 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
178 4cd67caa 2022-02-09 op prfx = line_prefixes[l->type].prfx1;
179 4cd67caa 2022-02-09 op switch (l->type) {
180 4cd67caa 2022-02-09 op case LINE_TEXT:
181 4cd67caa 2022-02-09 op case LINE_LINK:
182 4cd67caa 2022-02-09 op case LINE_TITLE_1:
183 4cd67caa 2022-02-09 op case LINE_TITLE_2:
184 4cd67caa 2022-02-09 op case LINE_TITLE_3:
185 4cd67caa 2022-02-09 op case LINE_ITEM:
186 4cd67caa 2022-02-09 op case LINE_QUOTE:
187 4cd67caa 2022-02-09 op case LINE_PRE_START:
188 4cd67caa 2022-02-09 op case LINE_PRE_END:
189 4cd67caa 2022-02-09 op case LINE_PRE_CONTENT:
190 4cd67caa 2022-02-09 op case LINE_PATCH:
191 4cd67caa 2022-02-09 op case LINE_PATCH_HDR:
192 4cd67caa 2022-02-09 op case LINE_PATCH_HUNK_HDR:
193 4cd67caa 2022-02-09 op case LINE_PATCH_ADD:
194 4cd67caa 2022-02-09 op case LINE_PATCH_DEL:
195 6b91e104 2022-10-09 op wrap_text(buffer, prfx, l, MIN(fill_column, width),
196 6b91e104 2022-10-09 op 0);
197 4cd67caa 2022-02-09 op break;
198 4cd67caa 2022-02-09 op case LINE_COMPL:
199 4cd67caa 2022-02-09 op case LINE_COMPL_CURRENT:
200 4cd67caa 2022-02-09 op case LINE_HELP:
201 4cd67caa 2022-02-09 op case LINE_DOWNLOAD:
202 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_DONE:
203 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_INFO:
204 6b91e104 2022-10-09 op wrap_text(buffer, prfx, l, width, 1);
205 4cd67caa 2022-02-09 op break;
206 4cd67caa 2022-02-09 op case LINE_FRINGE:
207 4cd67caa 2022-02-09 op /* never, ever wrapped */
208 4cd67caa 2022-02-09 op break;
209 4cd67caa 2022-02-09 op }
210 4cd67caa 2022-02-09 op
211 4cd67caa 2022-02-09 op if (top_orig == l && buffer->top_line == NULL) {
212 4cd67caa 2022-02-09 op buffer->line_off = buffer->line_max-1;
213 4cd67caa 2022-02-09 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
214 4cd67caa 2022-02-09 op
215 4cd67caa 2022-02-09 op while (1) {
216 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
217 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
218 4cd67caa 2022-02-09 op break;
219 4cd67caa 2022-02-09 op buffer->top_line = vl;
220 4cd67caa 2022-02-09 op buffer->line_off--;
221 4cd67caa 2022-02-09 op }
222 4cd67caa 2022-02-09 op }
223 4cd67caa 2022-02-09 op
224 4cd67caa 2022-02-09 op if (orig == l && buffer->current_line == NULL) {
225 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
226 4cd67caa 2022-02-09 op
227 4cd67caa 2022-02-09 op while (1) {
228 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
229 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
230 4cd67caa 2022-02-09 op break;
231 4cd67caa 2022-02-09 op buffer->current_line = vl;
232 4cd67caa 2022-02-09 op }
233 4cd67caa 2022-02-09 op }
234 4cd67caa 2022-02-09 op }
235 4cd67caa 2022-02-09 op
236 4cd67caa 2022-02-09 op if (buffer->current_line == NULL)
237 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_FIRST(&buffer->head);
238 4cd67caa 2022-02-09 op
239 4cd67caa 2022-02-09 op if (buffer->top_line == NULL)
240 4cd67caa 2022-02-09 op buffer->top_line = buffer->current_line;
241 4cd67caa 2022-02-09 op
242 4cd67caa 2022-02-09 op return 1;
243 4cd67caa 2022-02-09 op }