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 * Similar to wrap_text, but emit only o vline.
110 4cd67caa 2022-02-09 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
112 4cd67caa 2022-02-09 op struct vline *vl, *t;
115 4cd67caa 2022-02-09 op * be lazy: call wrap_text and then discard the continuations.
118 4cd67caa 2022-02-09 op if (!wrap_text(buffer, prfx, l, width))
121 4cd67caa 2022-02-09 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
122 4cd67caa 2022-02-09 op if (vl->flags & L_CONTINUATION) {
123 4cd67caa 2022-02-09 op TAILQ_REMOVE(&buffer->head, vl, vlines);
124 4cd67caa 2022-02-09 op free(vl->line);
126 4cd67caa 2022-02-09 op buffer->line_max--;
134 4cd67caa 2022-02-09 op * Build a list of visual line by wrapping the given line, assuming
135 4cd67caa 2022-02-09 op * that when printed will have a leading prefix prfx.
138 4cd67caa 2022-02-09 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
140 d5e0ee08 2022-10-09 op const char *line, *space;
141 d5e0ee08 2022-10-09 op size_t ret, off, start, cur, prfxwidth;
144 d5e0ee08 2022-10-09 op if ((line = l->line) == NULL || *line == '\0')
145 4cd67caa 2022-02-09 op return push_line(buffer, l, NULL, 0, 0);
147 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth(prfx);
148 4cd67caa 2022-02-09 op cur = prfxwidth;
152 4cd67caa 2022-02-09 op if (l->type == LINE_LINK && emojify_link &&
153 4cd67caa 2022-02-09 op emojied_line(l->line, &space)) {
154 4cd67caa 2022-02-09 op prfxwidth = utf8_swidth_between(l->line, space);
155 4cd67caa 2022-02-09 op cur = prfxwidth;
156 4cd67caa 2022-02-09 op line = space + 1;
159 d5e0ee08 2022-10-09 op for (off = 0; line[off] != '\0'; off += ret) {
162 d5e0ee08 2022-10-09 op ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
163 d5e0ee08 2022-10-09 op t = utf8_swidth_between(&line[off], &line[off + ret]);
165 d5e0ee08 2022-10-09 op if (cur + t <= width) {
170 d5e0ee08 2022-10-09 op if (!push_line(buffer, l, &line[start], off - start, flags))
173 d5e0ee08 2022-10-09 op flags = L_CONTINUATION;
178 d5e0ee08 2022-10-09 op if (off != start)
179 d5e0ee08 2022-10-09 op return push_line(buffer, l, &line[start], off - start, flags);
184 4cd67caa 2022-02-09 op wrap_page(struct buffer *buffer, int width)
186 4cd67caa 2022-02-09 op struct line *l;
187 4cd67caa 2022-02-09 op const struct line *top_orig, *orig;
188 4cd67caa 2022-02-09 op struct vline *vl;
189 4cd67caa 2022-02-09 op const char *prfx;
191 4cd67caa 2022-02-09 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
192 4cd67caa 2022-02-09 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
194 4cd67caa 2022-02-09 op buffer->top_line = NULL;
195 4cd67caa 2022-02-09 op buffer->current_line = NULL;
197 4cd67caa 2022-02-09 op buffer->force_redraw = 1;
198 4cd67caa 2022-02-09 op buffer->curs_y = 0;
199 4cd67caa 2022-02-09 op buffer->line_off = 0;
201 4cd67caa 2022-02-09 op empty_vlist(buffer);
203 4cd67caa 2022-02-09 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
204 4cd67caa 2022-02-09 op prfx = line_prefixes[l->type].prfx1;
205 4cd67caa 2022-02-09 op switch (l->type) {
206 4cd67caa 2022-02-09 op case LINE_TEXT:
207 4cd67caa 2022-02-09 op case LINE_LINK:
208 4cd67caa 2022-02-09 op case LINE_TITLE_1:
209 4cd67caa 2022-02-09 op case LINE_TITLE_2:
210 4cd67caa 2022-02-09 op case LINE_TITLE_3:
211 4cd67caa 2022-02-09 op case LINE_ITEM:
212 4cd67caa 2022-02-09 op case LINE_QUOTE:
213 4cd67caa 2022-02-09 op case LINE_PRE_START:
214 4cd67caa 2022-02-09 op case LINE_PRE_END:
215 4cd67caa 2022-02-09 op case LINE_PRE_CONTENT:
216 4cd67caa 2022-02-09 op case LINE_PATCH:
217 4cd67caa 2022-02-09 op case LINE_PATCH_HDR:
218 4cd67caa 2022-02-09 op case LINE_PATCH_HUNK_HDR:
219 4cd67caa 2022-02-09 op case LINE_PATCH_ADD:
220 4cd67caa 2022-02-09 op case LINE_PATCH_DEL:
221 d5e0ee08 2022-10-09 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
223 4cd67caa 2022-02-09 op case LINE_COMPL:
224 4cd67caa 2022-02-09 op case LINE_COMPL_CURRENT:
225 4cd67caa 2022-02-09 op case LINE_HELP:
226 4cd67caa 2022-02-09 op case LINE_DOWNLOAD:
227 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_DONE:
228 4cd67caa 2022-02-09 op case LINE_DOWNLOAD_INFO:
229 4cd67caa 2022-02-09 op wrap_one(buffer, prfx, l, width);
231 4cd67caa 2022-02-09 op case LINE_FRINGE:
232 4cd67caa 2022-02-09 op /* never, ever wrapped */
236 4cd67caa 2022-02-09 op if (top_orig == l && buffer->top_line == NULL) {
237 4cd67caa 2022-02-09 op buffer->line_off = buffer->line_max-1;
238 4cd67caa 2022-02-09 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
241 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
242 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
244 4cd67caa 2022-02-09 op buffer->top_line = vl;
245 4cd67caa 2022-02-09 op buffer->line_off--;
249 4cd67caa 2022-02-09 op if (orig == l && buffer->current_line == NULL) {
250 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
253 4cd67caa 2022-02-09 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
254 4cd67caa 2022-02-09 op if (vl == NULL || vl->parent != orig)
256 4cd67caa 2022-02-09 op buffer->current_line = vl;
261 4cd67caa 2022-02-09 op if (buffer->current_line == NULL)
262 4cd67caa 2022-02-09 op buffer->current_line = TAILQ_FIRST(&buffer->head);
264 4cd67caa 2022-02-09 op if (buffer->top_line == NULL)
265 4cd67caa 2022-02-09 op buffer->top_line = buffer->current_line;