2 4cd67caa 2022-02-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 4cd67caa 2022-02-09 op #include "compat.h"
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>
24 d5e0ee08 2022-10-09 op #include <grapheme.h>
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"
31 4cd67caa 2022-02-09 op erase_buffer(struct buffer *buffer)
33 4cd67caa 2022-02-09 op empty_vlist(buffer);
34 4cd67caa 2022-02-09 op empty_linelist(buffer);
38 4cd67caa 2022-02-09 op empty_linelist(struct buffer *buffer)
40 4cd67caa 2022-02-09 op struct line *l, *lt;
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);
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)
56 4cd67caa 2022-02-09 op empty_vlist(struct buffer *buffer)
58 4cd67caa 2022-02-09 op struct vline *vl, *t;
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;
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);
73 4cd67caa 2022-02-09 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
75 4cd67caa 2022-02-09 op struct vline *vl;
76 4cd67caa 2022-02-09 op const char *end;
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);
86 4cd67caa 2022-02-09 op if (!(l->flags & L_HIDDEN))
87 4cd67caa 2022-02-09 op buffer->line_max++;
89 4cd67caa 2022-02-09 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
92 4cd67caa 2022-02-09 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
97 4cd67caa 2022-02-09 op vl->parent = l;
99 4cd67caa 2022-02-09 op memcpy(vl->line, buf, len);
100 4cd67caa 2022-02-09 op vl->flags = flags;
102 4cd67caa 2022-02-09 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
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.
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)
114 d5e0ee08 2022-10-09 op const char *line, *space;
115 d5e0ee08 2022-10-09 op size_t ret, off, start, cur, prfxwidth;
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);
121 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth(prfx);
122 4cd67caa 2022-02-09 op cur = prfxwidth;
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;
133 d5e0ee08 2022-10-09 op for (off = 0; line[off] != '\0'; off += ret) {
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]);
139 d5e0ee08 2022-10-09 op if (cur + t <= width) {
144 d5e0ee08 2022-10-09 op if (!push_line(buffer, l, &line[start], off - start, flags))
150 d5e0ee08 2022-10-09 op flags = L_CONTINUATION;
152 b0266e30 2022-11-02 op cur = t + prfxwidth;
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);
161 4cd67caa 2022-02-09 op wrap_page(struct buffer *buffer, int width)
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;
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;
171 4cd67caa 2022-02-09 op buffer->top_line = NULL;
172 4cd67caa 2022-02-09 op buffer->current_line = NULL;
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;
178 4cd67caa 2022-02-09 op empty_vlist(buffer);
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),
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);
209 4cd67caa 2022-02-09 op case LINE_FRINGE:
210 4cd67caa 2022-02-09 op /* never, ever wrapped */
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);
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)
222 4cd67caa 2022-02-09 op buffer->top_line = vl;
223 4cd67caa 2022-02-09 op buffer->line_off--;
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);
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)
234 4cd67caa 2022-02-09 op buffer->current_line = vl;
239 4cd67caa 2022-02-09 op if (buffer->current_line == NULL)
240 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_FIRST(&buffer->head);
242 4cd67caa 2022-02-09 op if (buffer->top_line == NULL)
243 4cd67caa 2022-02-09 op buffer->top_line = buffer->current_line;