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"
29 #include "xwrapper.h"
31 void
32 erase_buffer(struct buffer *buffer)
33 {
34 empty_vlist(buffer);
35 empty_linelist(buffer);
36 }
38 void
39 empty_linelist(struct buffer *buffer)
40 {
41 struct line *l, *lt;
43 TAILQ_FOREACH_SAFE(l, &buffer->head, lines, lt) {
44 TAILQ_REMOVE(&buffer->head, l, lines);
45 free(l->line);
47 if (l->type != LINE_COMPL &&
48 l->type != LINE_COMPL_CURRENT &&
49 l->type != LINE_HELP)
50 free(l->alt);
52 free(l);
53 }
54 }
56 void
57 empty_vlist(struct buffer *buffer)
58 {
59 struct vline *vl, *t;
61 buffer->top_line = NULL;
62 buffer->line_off = 0;
63 buffer->current_line = NULL;
64 buffer->line_max = 0;
66 TAILQ_FOREACH_SAFE(vl, &buffer->vhead, vlines, t) {
67 TAILQ_REMOVE(&buffer->vhead, vl, vlines);
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 vl = xcalloc(1, sizeof(*vl));
91 vl->parent = l;
92 if (len != 0) {
93 vl->from = buf - l->line;
94 vl->len = len;
95 }
96 vl->flags = flags;
98 TAILQ_INSERT_TAIL(&buffer->vhead, vl, vlines);
99 return 1;
102 /*
103 * Build a list of visual line by wrapping the given line, assuming
104 * that when printed will have a leading prefix prfx.
105 */
106 int
107 wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
108 size_t width, int base_offset, int oneline)
110 const char *line, *space;
111 size_t ret, off, start, cur, prfxwidth;
112 int flags;
114 if ((line = l->line) == NULL || *line == '\0')
115 return push_line(buffer, l, NULL, 0, 0);
117 prfxwidth = utf8_swidth(prfx, base_offset);
118 cur = base_offset + prfxwidth;
119 start = 0;
120 flags = 0;
122 if (l->type == LINE_LINK && emojify_link &&
123 emojied_line(l->line, &space)) {
124 prfxwidth = utf8_swidth_between(l->line, space, base_offset);
125 cur = base_offset + prfxwidth;
126 line = space + 1;
129 for (off = 0; line[off] != '\0'; off += ret) {
130 size_t t;
132 ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
133 t = utf8_swidth_between(&line[off], &line[off + ret],
134 base_offset);
136 /* we can't reach the last column */
137 if (cur + t < width) {
138 cur += t;
139 continue;
142 if (!push_line(buffer, l, &line[start], off - start, flags))
143 return 0;
145 if (oneline)
146 return 0;
148 flags = L_CONTINUATION;
149 start = off;
150 cur = base_offset + prfxwidth + t;
153 if (off != start)
154 return push_line(buffer, l, &line[start], off - start, flags);
155 return 0;
158 int
159 wrap_page(struct buffer *buffer, int width, int x_offset)
161 struct line *l;
162 const struct line *top_orig, *orig;
163 struct vline *vl;
164 const char *prfx;
166 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
167 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
169 buffer->top_line = NULL;
170 buffer->current_line = NULL;
172 buffer->force_redraw = 1;
173 buffer->curs_y = 0;
174 buffer->line_off = 0;
176 empty_vlist(buffer);
178 TAILQ_FOREACH(l, &buffer->head, lines) {
179 prfx = line_prefixes[l->type].prfx1;
180 switch (l->type) {
181 case LINE_TEXT:
182 case LINE_LINK:
183 case LINE_TITLE_1:
184 case LINE_TITLE_2:
185 case LINE_TITLE_3:
186 case LINE_ITEM:
187 case LINE_QUOTE:
188 case LINE_PRE_START:
189 case LINE_PRE_END:
190 case LINE_PRE_CONTENT:
191 case LINE_PATCH:
192 case LINE_PATCH_HDR:
193 case LINE_PATCH_HUNK_HDR:
194 case LINE_PATCH_ADD:
195 case LINE_PATCH_DEL:
196 wrap_text(buffer, prfx, l, MIN(fill_column, width),
197 x_offset, 0);
198 break;
199 case LINE_COMPL:
200 case LINE_COMPL_CURRENT:
201 case LINE_HELP:
202 case LINE_DOWNLOAD:
203 case LINE_DOWNLOAD_DONE:
204 case LINE_DOWNLOAD_INFO:
205 wrap_text(buffer, prfx, l, width, x_offset, 1);
206 break;
207 case LINE_FRINGE:
208 /* never, ever wrapped */
209 break;
212 if (top_orig == l && buffer->top_line == NULL) {
213 buffer->line_off = buffer->line_max-1;
214 buffer->top_line = TAILQ_LAST(&buffer->vhead, vhead);
216 while (1) {
217 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
218 if (vl == NULL || vl->parent != orig)
219 break;
220 buffer->top_line = vl;
221 buffer->line_off--;
225 if (orig == l && buffer->current_line == NULL) {
226 buffer->current_line = TAILQ_LAST(&buffer->vhead, vhead);
228 while (1) {
229 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
230 if (vl == NULL || vl->parent != orig)
231 break;
232 buffer->current_line = vl;
237 if (buffer->current_line == NULL)
238 buffer->current_line = TAILQ_FIRST(&buffer->vhead);
240 if (buffer->top_line == NULL)
241 buffer->top_line = buffer->current_line;
243 return 1;