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 65d9b3ca 2021-03-14 op #include "telescope.h"
19 65d9b3ca 2021-03-14 op #include <stdlib.h>
20 65d9b3ca 2021-03-14 op #include <string.h>
23 65d9b3ca 2021-03-14 op * Text wrapping
24 65d9b3ca 2021-03-14 op * =============
26 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
28 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
29 65d9b3ca 2021-03-14 op * a. hard wrap.
31 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
32 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
33 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
39 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
41 ca5da938 2021-04-01 op struct line *l, *lt;
43 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
44 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
45 ca5da938 2021-04-01 op free(l->line);
52 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
54 ca5da938 2021-04-01 op struct vline *vl, *t;
56 46f6e974 2021-05-17 op buffer->current_line = NULL;
57 46f6e974 2021-05-17 op buffer->line_max = 0;
59 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
60 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
61 ca5da938 2021-04-01 op free(vl->line);
67 46f6e974 2021-05-17 op push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int cont)
69 65d9b3ca 2021-03-14 op struct vline *vl;
71 46f6e974 2021-05-17 op buffer->line_max++;
73 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
76 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
81 65d9b3ca 2021-03-14 op vl->parent = l;
83 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
84 65d9b3ca 2021-03-14 op vl->flags = cont;
86 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
87 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
89 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
94 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
95 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
98 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
100 676fba6f 2021-03-21 op const char *separators = " \t-";
101 676fba6f 2021-03-21 op const char *start, *end, *line, *lastsep, *lastchar;
102 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
103 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
106 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
107 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
109 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
110 676fba6f 2021-03-21 op cur = prfxwidth;
111 676fba6f 2021-03-21 op start = line;
112 676fba6f 2021-03-21 op lastsep = NULL;
113 676fba6f 2021-03-21 op lastchar = line;
115 676fba6f 2021-03-21 op for (; *line; line++) {
116 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
118 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
119 676fba6f 2021-03-21 op if (cur + w >= width -1) {
120 676fba6f 2021-03-21 op end = lastsep == NULL
121 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
122 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
123 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, end - start, cont))
127 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
129 676fba6f 2021-03-21 op if (strchr(separators, *line) != NULL)
130 676fba6f 2021-03-21 op lastsep = line;
133 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
137 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
141 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
143 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
145 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
146 676fba6f 2021-03-21 op size_t cur, w;
148 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
149 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
151 676fba6f 2021-03-21 op start = line;
152 676fba6f 2021-03-21 op lastchar = line;
155 676fba6f 2021-03-21 op for (; *line; line++) {
156 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
158 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
159 676fba6f 2021-03-21 op if (cur + w >= width) {
160 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
164 676fba6f 2021-03-21 op start = lastchar;
167 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
171 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);