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);
68 }
69 }
71 static int
72 push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
73 {
74 struct vline *vl;
75 const char *end;
77 /* omit trailing spaces */
78 if (len != 0) {
79 for (end = buf + len - 1;
80 end > buf && isspace(*end);
81 end--, len--)
82 ; /* nop */
83 }
85 if (!(l->flags & L_HIDDEN))
86 buffer->line_max++;
88 if ((vl = calloc(1, sizeof(*vl))) == NULL)
89 return 0;
91 vl->parent = l;
92 if (len != 0) {
93 vl->from = buf - l->line;
94 vl->len = len;
95 vl->cplen = utf8_ncplen(buf, vl->len);
96 }
97 vl->flags = flags;
99 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
100 return 1;
103 /*
104 * Build a list of visual line by wrapping the given line, assuming
105 * that when printed will have a leading prefix prfx.
106 */
107 int
108 wrap_text(struct buffer *buffer, const char *prfx, struct line *l,
109 size_t width, int oneline)
111 const char *line, *space;
112 size_t ret, off, start, cur, prfxwidth;
113 int flags;
115 if ((line = l->line) == NULL || *line == '\0')
116 return push_line(buffer, l, NULL, 0, 0);
118 prfxwidth = utf8_swidth(prfx);
119 cur = prfxwidth;
120 start = 0;
121 flags = 0;
123 if (l->type == LINE_LINK && emojify_link &&
124 emojied_line(l->line, &space)) {
125 prfxwidth = utf8_swidth_between(l->line, space);
126 cur = prfxwidth;
127 line = space + 1;
130 for (off = 0; line[off] != '\0'; off += ret) {
131 size_t t;
133 ret = grapheme_next_line_break_utf8(&line[off], SIZE_MAX);
134 t = utf8_swidth_between(&line[off], &line[off + ret]);
136 if (cur + t <= width) {
137 cur += t;
138 continue;
141 if (!push_line(buffer, l, &line[start], off - start, flags))
142 return 0;
144 if (oneline)
145 return 0;
147 flags = L_CONTINUATION;
148 start = off;
149 cur = t + prfxwidth;
152 if (off != start)
153 return push_line(buffer, l, &line[start], off - start, flags);
154 return 0;
157 int
158 wrap_page(struct buffer *buffer, int width)
160 struct line *l;
161 const struct line *top_orig, *orig;
162 struct vline *vl;
163 const char *prfx;
165 top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
166 orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
168 buffer->top_line = NULL;
169 buffer->current_line = NULL;
171 buffer->force_redraw = 1;
172 buffer->curs_y = 0;
173 buffer->line_off = 0;
175 empty_vlist(buffer);
177 TAILQ_FOREACH(l, &buffer->page.head, lines) {
178 prfx = line_prefixes[l->type].prfx1;
179 switch (l->type) {
180 case LINE_TEXT:
181 case LINE_LINK:
182 case LINE_TITLE_1:
183 case LINE_TITLE_2:
184 case LINE_TITLE_3:
185 case LINE_ITEM:
186 case LINE_QUOTE:
187 case LINE_PRE_START:
188 case LINE_PRE_END:
189 case LINE_PRE_CONTENT:
190 case LINE_PATCH:
191 case LINE_PATCH_HDR:
192 case LINE_PATCH_HUNK_HDR:
193 case LINE_PATCH_ADD:
194 case LINE_PATCH_DEL:
195 wrap_text(buffer, prfx, l, MIN(fill_column, width),
196 0);
197 break;
198 case LINE_COMPL:
199 case LINE_COMPL_CURRENT:
200 case LINE_HELP:
201 case LINE_DOWNLOAD:
202 case LINE_DOWNLOAD_DONE:
203 case LINE_DOWNLOAD_INFO:
204 wrap_text(buffer, prfx, l, width, 1);
205 break;
206 case LINE_FRINGE:
207 /* never, ever wrapped */
208 break;
211 if (top_orig == l && buffer->top_line == NULL) {
212 buffer->line_off = buffer->line_max-1;
213 buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
215 while (1) {
216 vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
217 if (vl == NULL || vl->parent != orig)
218 break;
219 buffer->top_line = vl;
220 buffer->line_off--;
224 if (orig == l && buffer->current_line == NULL) {
225 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
227 while (1) {
228 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
229 if (vl == NULL || vl->parent != orig)
230 break;
231 buffer->current_line = vl;
236 if (buffer->current_line == NULL)
237 buffer->current_line = TAILQ_FIRST(&buffer->head);
239 if (buffer->top_line == NULL)
240 buffer->top_line = buffer->current_line;
242 return 1;