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 * Build a list of visual line by wrapping the given line, assuming
108 * that when printed will have a leading prefix prfx.
109 */
110 int
111 wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
112 size_t width, int oneline)
114 const char *line, *space;
115 size_t ret, off, start, cur, prfxwidth;
116 int flags;
118 if ((line = l->line) == NULL || *line == '\0')
119 return push_line(buffer, l, NULL, 0, 0);
121 prfxwidth = utf8_swidth(prfx);
122 cur = prfxwidth;
123 start = 0;
124 flags = 0;
126 if (l->type == LINE_LINK && emojify_link &&
127 emojied_line(l->line, &space)) {
128 prfxwidth = utf8_swidth_between(l->line, space);
129 cur = prfxwidth;
130 line = space + 1;
133 for (off = 0; line[off] != '\0'; off += ret) {
134 size_t t;
136 ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
137 t = utf8_swidth_between(&line[off], &line[off + ret]);
139 if (cur + t <= width) {
140 cur += t;
141 continue;
144 if (!push_line(buffer, l, &line[start], off - start, flags))
145 return 0;
147 if (oneline)
148 return 0;
150 flags = L_CONTINUATION;
151 start = off;
152 cur = t;
155 if (off != start)
156 return push_line(buffer, l, &line[start], off - start, flags);
157 return 0;
160 int
161 wrap_page(struct buffer *buffer, int width)
163 struct line *l;
164 const struct line *top_orig, *orig;
165 struct vline *vl;
166 const char *prfx;
168 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
169 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
171 buffer->top_line = NULL;
172 buffer->current_line = NULL;
174 buffer->force_redraw = 1;
175 buffer->curs_y = 0;
176 buffer->line_off = 0;
178 empty_vlist(buffer);
180 TAILQ_FOREACH(l, &buffer->page.head, lines) {
181 prfx = line_prefixes[l->type].prfx1;
182 switch (l->type) {
183 case LINE_TEXT:
184 case LINE_LINK:
185 case LINE_TITLE_1:
186 case LINE_TITLE_2:
187 case LINE_TITLE_3:
188 case LINE_ITEM:
189 case LINE_QUOTE:
190 case LINE_PRE_START:
191 case LINE_PRE_END:
192 case LINE_PRE_CONTENT:
193 case LINE_PATCH:
194 case LINE_PATCH_HDR:
195 case LINE_PATCH_HUNK_HDR:
196 case LINE_PATCH_ADD:
197 case LINE_PATCH_DEL:
198 wrap_text(buffer, prfx, l, MIN(fill_column, width),
199 0);
200 break;
201 case LINE_COMPL:
202 case LINE_COMPL_CURRENT:
203 case LINE_HELP:
204 case LINE_DOWNLOAD:
205 case LINE_DOWNLOAD_DONE:
206 case LINE_DOWNLOAD_INFO:
207 wrap_text(buffer, prfx, l, width, 1);
208 break;
209 case LINE_FRINGE:
210 /* never, ever wrapped */
211 break;
214 if (top_orig == l && buffer->top_line == NULL) {
215 buffer->line_off = buffer->line_max-1;
216 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
218 while (1) {
219 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
220 if (vl == NULL || vl->parent != orig)
221 break;
222 buffer->top_line = vl;
223 buffer->line_off--;
227 if (orig == l && buffer->current_line == NULL) {
228 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
230 while (1) {
231 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
232 if (vl == NULL || vl->parent != orig)
233 break;
234 buffer->current_line = vl;
239 if (buffer->current_line == NULL)
240 buffer->current_line = TAILQ_FIRST(&buffer->head);
242 if (buffer->top_line == NULL)
243 buffer->top_line = buffer->current_line;
245 return 1;