Blob


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