2 65d9b3ca 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 65d9b3ca 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 65d9b3ca 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 65d9b3ca 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 65d9b3ca 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 65d9b3ca 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 65d9b3ca 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 65d9b3ca 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 65d9b3ca 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 65d9b3ca 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 65d9b3ca 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 786e6deb 2021-07-21 op #include "compat.h"
19 a0507108 2021-07-18 op #include <ctype.h>
20 65d9b3ca 2021-03-14 op #include <stdlib.h>
21 65d9b3ca 2021-03-14 op #include <string.h>
23 88693f61 2021-07-15 op #include "defaults.h"
24 5caf7d67 2021-07-12 op #include "telescope.h"
25 5caf7d67 2021-07-12 op #include "utf8.h"
28 65d9b3ca 2021-03-14 op * Text wrapping
29 65d9b3ca 2021-03-14 op * =============
31 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
33 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
34 65d9b3ca 2021-03-14 op * a. hard wrap.
36 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
37 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
38 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
44 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
46 bca92a4c 2021-07-01 op empty_vlist(buffer);
47 bca92a4c 2021-07-01 op empty_linelist(buffer);
51 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
53 ca5da938 2021-04-01 op struct line *l, *lt;
55 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
56 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
57 ca5da938 2021-04-01 op free(l->line);
59 b3be07ea 2021-07-18 op if (l->type != LINE_COMPL &&
60 c6be26e4 2021-07-21 op l->type != LINE_COMPL_CURRENT &&
61 c6be26e4 2021-07-21 op l->type != LINE_HELP)
69 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
71 ca5da938 2021-04-01 op struct vline *vl, *t;
73 4ea9160a 2021-07-01 op buffer->top_line = NULL;
74 4ea9160a 2021-07-01 op buffer->line_off = 0;
75 46f6e974 2021-05-17 op buffer->current_line = NULL;
76 46f6e974 2021-05-17 op buffer->line_max = 0;
78 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
79 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
80 ca5da938 2021-04-01 op free(vl->line);
86 87f7918d 2021-07-14 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
88 65d9b3ca 2021-03-14 op struct vline *vl;
89 a0507108 2021-07-18 op const char *end;
91 a0507108 2021-07-18 op /* omit trailing spaces */
92 a0507108 2021-07-18 op if (len != 0) {
93 a0507108 2021-07-18 op for (end = buf + len - 1;
94 a0507108 2021-07-18 op end > buf && isspace(*end);
99 6e6c6afb 2021-07-14 op if (!(l->flags & L_HIDDEN))
100 6e6c6afb 2021-07-14 op buffer->line_max++;
102 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
105 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
110 65d9b3ca 2021-03-14 op vl->parent = l;
111 65d9b3ca 2021-03-14 op if (len != 0)
112 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
113 5492b68f 2021-07-05 op vl->flags = flags;
115 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
116 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
118 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
123 e7b982f4 2021-07-14 op * Similar to wrap_text, but emit only o vline.
126 e7b982f4 2021-07-14 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
128 e7b982f4 2021-07-14 op struct vline *vl, *t;
131 e7b982f4 2021-07-14 op * be lazy: call wrap_text and then discard the continuations.
134 e7b982f4 2021-07-14 op if (!wrap_text(buffer, prfx, l, width))
137 e7b982f4 2021-07-14 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
138 e7b982f4 2021-07-14 op if (vl->flags & L_CONTINUATION) {
139 e7b982f4 2021-07-14 op TAILQ_REMOVE(&buffer->head, vl, vlines);
140 e7b982f4 2021-07-14 op free(vl->line);
142 6eddfe3c 2021-07-15 op buffer->line_max--;
150 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
151 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
154 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
156 676fba6f 2021-03-21 op const char *separators = " \t-";
157 218ac7f6 2021-07-16 op const char *start, *end, *line, *lastsep, *lastchar, *space;
158 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
159 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
162 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
163 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
165 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
166 676fba6f 2021-03-21 op cur = prfxwidth;
167 676fba6f 2021-03-21 op start = line;
168 676fba6f 2021-03-21 op lastsep = NULL;
169 676fba6f 2021-03-21 op lastchar = line;
172 218ac7f6 2021-07-16 op if (l->type == LINE_LINK && emojify_link &&
173 218ac7f6 2021-07-16 op emojied_line(l->line, &space)) {
174 218ac7f6 2021-07-16 op prfxwidth = utf8_swidth_between(l->line, space);
175 218ac7f6 2021-07-16 op cur = prfxwidth;
176 218ac7f6 2021-07-16 op line = space + 1;
179 676fba6f 2021-03-21 op for (; *line; line++) {
180 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
182 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
183 1247b8cc 2021-07-16 op if (cur + w > width) {
184 676fba6f 2021-03-21 op end = lastsep == NULL
185 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
186 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
187 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
189 963c680c 2021-07-05 op flags = L_CONTINUATION;
191 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
192 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
193 5b010818 2021-07-05 op lastsep = line;
196 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
200 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
204 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
206 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
208 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
209 676fba6f 2021-03-21 op size_t cur, w;
211 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
212 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
214 676fba6f 2021-03-21 op start = line;
215 676fba6f 2021-03-21 op lastchar = line;
218 676fba6f 2021-03-21 op for (; *line; line++) {
219 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
221 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
222 676fba6f 2021-03-21 op if (cur + w >= width) {
223 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
225 88693f61 2021-07-15 op cont = L_CONTINUATION;
226 88693f61 2021-07-15 op if (dont_wrap_pre)
229 676fba6f 2021-03-21 op start = lastchar;
232 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
236 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
240 de278567 2021-07-21 op wrap_page(struct buffer *buffer, int width)
242 de278567 2021-07-21 op struct line *l;
243 de278567 2021-07-21 op const struct line *top_orig, *orig;
244 de278567 2021-07-21 op struct vline *vl;
245 de278567 2021-07-21 op int pre_width;
246 de278567 2021-07-21 op const char *prfx;
248 de278567 2021-07-21 op top_orig = buffer->top_line == NULL ? NULL : buffer->top_line->parent;
249 de278567 2021-07-21 op orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent;
251 de278567 2021-07-21 op buffer->top_line = NULL;
252 de278567 2021-07-21 op buffer->current_line = NULL;
254 de278567 2021-07-21 op buffer->force_redraw = 1;
255 de278567 2021-07-21 op buffer->curs_y = 0;
256 de278567 2021-07-21 op buffer->line_off = 0;
258 de278567 2021-07-21 op empty_vlist(buffer);
260 de278567 2021-07-21 op TAILQ_FOREACH(l, &buffer->page.head, lines) {
261 de278567 2021-07-21 op prfx = line_prefixes[l->type].prfx1;
262 de278567 2021-07-21 op switch (l->type) {
263 de278567 2021-07-21 op case LINE_TEXT:
264 de278567 2021-07-21 op case LINE_LINK:
265 de278567 2021-07-21 op case LINE_TITLE_1:
266 de278567 2021-07-21 op case LINE_TITLE_2:
267 de278567 2021-07-21 op case LINE_TITLE_3:
268 de278567 2021-07-21 op case LINE_ITEM:
269 de278567 2021-07-21 op case LINE_QUOTE:
270 de278567 2021-07-21 op case LINE_PRE_START:
271 de278567 2021-07-21 op case LINE_PRE_END:
272 de278567 2021-07-21 op wrap_text(buffer, prfx, l, MIN(fill_column, width));
274 de278567 2021-07-21 op case LINE_PRE_CONTENT:
275 de278567 2021-07-21 op if (olivetti_mode)
276 de278567 2021-07-21 op pre_width = MIN(fill_column, width);
278 de278567 2021-07-21 op pre_width = width;
279 de278567 2021-07-21 op hardwrap_text(buffer, l, pre_width);
281 de278567 2021-07-21 op case LINE_COMPL:
282 de278567 2021-07-21 op case LINE_COMPL_CURRENT:
283 c6be26e4 2021-07-21 op case LINE_HELP:
284 de278567 2021-07-21 op wrap_one(buffer, prfx, l, width);
288 de278567 2021-07-21 op if (top_orig == l && buffer->top_line == NULL) {
289 de278567 2021-07-21 op buffer->line_off = buffer->line_max-1;
290 de278567 2021-07-21 op buffer->top_line = TAILQ_LAST(&buffer->head, vhead);
293 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
294 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
296 de278567 2021-07-21 op buffer->top_line = vl;
297 de278567 2021-07-21 op buffer->line_off--;
301 de278567 2021-07-21 op if (orig == l && buffer->current_line == NULL) {
302 de278567 2021-07-21 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
305 de278567 2021-07-21 op vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
306 de278567 2021-07-21 op if (vl == NULL || vl->parent != orig)
308 de278567 2021-07-21 op buffer->current_line = vl;
313 de278567 2021-07-21 op if (buffer->current_line == NULL)
314 de278567 2021-07-21 op buffer->current_line = TAILQ_FIRST(&buffer->head);
316 de278567 2021-07-21 op if (buffer->top_line == NULL)
317 de278567 2021-07-21 op buffer->top_line = buffer->current_line;