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->line);
68 4cd67caa 2022-02-09 op free(vl);
69 4cd67caa 2022-02-09 op }
70 4cd67caa 2022-02-09 op }
71 4cd67caa 2022-02-09 op
72 4cd67caa 2022-02-09 op static int
73 4cd67caa 2022-02-09 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
74 4cd67caa 2022-02-09 op {
75 4cd67caa 2022-02-09 op struct vline *vl;
76 4cd67caa 2022-02-09 op const char *end;
77 4cd67caa 2022-02-09 op
78 4cd67caa 2022-02-09 op /* omit trailing spaces */
79 4cd67caa 2022-02-09 op if (len != 0) {
80 4cd67caa 2022-02-09 op for (end = buf + len - 1;
81 4cd67caa 2022-02-09 op end > buf && isspace(*end);
82 4cd67caa 2022-02-09 op end--, len--)
83 4cd67caa 2022-02-09 op ; /* nop */
84 4cd67caa 2022-02-09 op }
85 4cd67caa 2022-02-09 op
86 4cd67caa 2022-02-09 op if (!(l->flags & L_HIDDEN))
87 4cd67caa 2022-02-09 op buffer->line_max++;
88 4cd67caa 2022-02-09 op
89 4cd67caa 2022-02-09 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
90 4cd67caa 2022-02-09 op return 0;
91 4cd67caa 2022-02-09 op
92 4cd67caa 2022-02-09 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
93 4cd67caa 2022-02-09 op free(vl);
94 4cd67caa 2022-02-09 op return 0;
95 4cd67caa 2022-02-09 op }
96 4cd67caa 2022-02-09 op
97 4cd67caa 2022-02-09 op vl->parent = l;
98 4cd67caa 2022-02-09 op if (len != 0)
99 4cd67caa 2022-02-09 op memcpy(vl->line, buf, len);
100 4cd67caa 2022-02-09 op vl->flags = flags;
101 4cd67caa 2022-02-09 op
102 4cd67caa 2022-02-09 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
103 4cd67caa 2022-02-09 op return 1;
104 4cd67caa 2022-02-09 op }
105 4cd67caa 2022-02-09 op
106 4cd67caa 2022-02-09 op /*
107 4cd67caa 2022-02-09 op * Build a list of visual line by wrapping the given line, assuming
108 4cd67caa 2022-02-09 op * that when printed will have a leading prefix prfx.
109 4cd67caa 2022-02-09 op */
110 4cd67caa 2022-02-09 op int
111 6b91e104 2022-10-09 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
112 6b91e104 2022-10-09 op size_t width, int oneline)
113 4cd67caa 2022-02-09 op {
114 d5e0ee08 2022-10-09 op const char *line, *space;
115 d5e0ee08 2022-10-09 op size_t ret, off, start, cur, prfxwidth;
116 4cd67caa 2022-02-09 op int flags;
117 4cd67caa 2022-02-09 op
118 d5e0ee08 2022-10-09 op if ((line = l->line) == NULL || *line == '\0')
119 4cd67caa 2022-02-09 op return push_line(buffer, l, NULL, 0, 0);
120 4cd67caa 2022-02-09 op
121 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth(prfx);
122 4cd67caa 2022-02-09 op cur = prfxwidth;
123 d5e0ee08 2022-10-09 op start = 0;
124 4cd67caa 2022-02-09 op flags = 0;
125 4cd67caa 2022-02-09 op
126 4cd67caa 2022-02-09 op if (l->type == LINE_LINK && emojify_link &&
127 4cd67caa 2022-02-09 op emojied_line(l->line, &space)) {
128 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth_between(l->line, space);
129 4cd67caa 2022-02-09 op cur = prfxwidth;
130 4cd67caa 2022-02-09 op line = space + 1;
131 4cd67caa 2022-02-09 op }
132 4cd67caa 2022-02-09 op
133 d5e0ee08 2022-10-09 op for (off = 0; line[off] != '\0'; off += ret) {
134 d5e0ee08 2022-10-09 op size_t t;
135 4cd67caa 2022-02-09 op
136 d5e0ee08 2022-10-09 op ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
137 d5e0ee08 2022-10-09 op t = utf8_swidth_between(&line[off], &line[off + ret]);
138 4cd67caa 2022-02-09 op
139 d5e0ee08 2022-10-09 op if (cur + t <= width) {
140 d5e0ee08 2022-10-09 op cur += t;
141 4cd67caa 2022-02-09 op continue;
142 4cd67caa 2022-02-09 op }
143 4cd67caa 2022-02-09 op
144 d5e0ee08 2022-10-09 op if (!push_line(buffer, l, &line[start], off - start, flags))
145 d5e0ee08 2022-10-09 op return 0;
146 d5e0ee08 2022-10-09 op
147 6b91e104 2022-10-09 op if (oneline)
148 6b91e104 2022-10-09 op return 0;
149 6b91e104 2022-10-09 op
150 d5e0ee08 2022-10-09 op flags = L_CONTINUATION;
151 d5e0ee08 2022-10-09 op start = off;
152 b0266e30 2022-11-02 op cur = t + prfxwidth;
153 4cd67caa 2022-02-09 op }
154 4cd67caa 2022-02-09 op
155 d5e0ee08 2022-10-09 op if (off != start)
156 d5e0ee08 2022-10-09 op return push_line(buffer, l, &line[start], off - start, flags);
157 d5e0ee08 2022-10-09 op return 0;
158 4cd67caa 2022-02-09 op }
159 4cd67caa 2022-02-09 op
160 4cd67caa 2022-02-09 op int
161 4cd67caa 2022-02-09 op wrap_page(struct buffer *buffer, int width)
162 4cd67caa 2022-02-09 op {
163 4cd67caa 2022-02-09 op struct line *l;
164 4cd67caa 2022-02-09 op const struct line *top_orig, *orig;
165 4cd67caa 2022-02-09 op struct vline *vl;
166 4cd67caa 2022-02-09 op const char *prfx;
167 4cd67caa 2022-02-09 op
168 4cd67caa 2022-02-09 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
169 4cd67caa 2022-02-09 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
170 4cd67caa 2022-02-09 op
171 4cd67caa 2022-02-09 op buffer->top_line = NULL;
172 4cd67caa 2022-02-09 op buffer->current_line = NULL;
173 4cd67caa 2022-02-09 op
174 4cd67caa 2022-02-09 op buffer->force_redraw = 1;
175 4cd67caa 2022-02-09 op buffer->curs_y = 0;
176 4cd67caa 2022-02-09 op buffer->line_off = 0;
177 4cd67caa 2022-02-09 op
178 4cd67caa 2022-02-09 op empty_vlist(buffer);
179 4cd67caa 2022-02-09 op
180 4cd67caa 2022-02-09 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
181 4cd67caa 2022-02-09 op prfx = line_prefixes[l->type].prfx1;
182 4cd67caa 2022-02-09 op switch (l->type) {
183 4cd67caa 2022-02-09 op case LINE_TEXT:
184 4cd67caa 2022-02-09 op case LINE_LINK:
185 4cd67caa 2022-02-09 op case LINE_TITLE_1:
186 4cd67caa 2022-02-09 op case LINE_TITLE_2:
187 4cd67caa 2022-02-09 op case LINE_TITLE_3:
188 4cd67caa 2022-02-09 op case LINE_ITEM:
189 4cd67caa 2022-02-09 op case LINE_QUOTE:
190 4cd67caa 2022-02-09 op case LINE_PRE_START:
191 4cd67caa 2022-02-09 op case LINE_PRE_END:
192 4cd67caa 2022-02-09 op case LINE_PRE_CONTENT:
193 4cd67caa 2022-02-09 op case LINE_PATCH:
194 4cd67caa 2022-02-09 op case LINE_PATCH_HDR:
195 4cd67caa 2022-02-09 op case LINE_PATCH_HUNK_HDR:
196 4cd67caa 2022-02-09 op case LINE_PATCH_ADD:
197 4cd67caa 2022-02-09 op case LINE_PATCH_DEL:
198 6b91e104 2022-10-09 op wrap_text(buffer, prfx, l, MIN(fill_column, width),
199 6b91e104 2022-10-09 op 0);
200 4cd67caa 2022-02-09 op break;
201 4cd67caa 2022-02-09 op case LINE_COMPL:
202 4cd67caa 2022-02-09 op case LINE_COMPL_CURRENT:
203 4cd67caa 2022-02-09 op case LINE_HELP:
204 4cd67caa 2022-02-09 op case LINE_DOWNLOAD:
205 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_DONE:
206 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_INFO:
207 6b91e104 2022-10-09 op wrap_text(buffer, prfx, l, width, 1);
208 4cd67caa 2022-02-09 op break;
209 4cd67caa 2022-02-09 op case LINE_FRINGE:
210 4cd67caa 2022-02-09 op /* never, ever wrapped */
211 4cd67caa 2022-02-09 op break;
212 4cd67caa 2022-02-09 op }
213 4cd67caa 2022-02-09 op
214 4cd67caa 2022-02-09 op if (top_orig == l && buffer->top_line == NULL) {
215 4cd67caa 2022-02-09 op buffer->line_off = buffer->line_max-1;
216 4cd67caa 2022-02-09 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
217 4cd67caa 2022-02-09 op
218 4cd67caa 2022-02-09 op while (1) {
219 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
220 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
221 4cd67caa 2022-02-09 op break;
222 4cd67caa 2022-02-09 op buffer->top_line = vl;
223 4cd67caa 2022-02-09 op buffer->line_off--;
224 4cd67caa 2022-02-09 op }
225 4cd67caa 2022-02-09 op }
226 4cd67caa 2022-02-09 op
227 4cd67caa 2022-02-09 op if (orig == l && buffer->current_line == NULL) {
228 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
229 4cd67caa 2022-02-09 op
230 4cd67caa 2022-02-09 op while (1) {
231 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
232 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
233 4cd67caa 2022-02-09 op break;
234 4cd67caa 2022-02-09 op buffer->current_line = vl;
235 4cd67caa 2022-02-09 op }
236 4cd67caa 2022-02-09 op }
237 4cd67caa 2022-02-09 op }
238 4cd67caa 2022-02-09 op
239 4cd67caa 2022-02-09 op if (buffer->current_line == NULL)
240 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_FIRST(&buffer->head);
241 4cd67caa 2022-02-09 op
242 4cd67caa 2022-02-09 op if (buffer->top_line == NULL)
243 4cd67caa 2022-02-09 op buffer->top_line = buffer->current_line;
244 4cd67caa 2022-02-09 op
245 4cd67caa 2022-02-09 op return 1;
246 4cd67caa 2022-02-09 op }