Blame


1 65d9b3ca 2021-03-14 op /*
2 65d9b3ca 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 65d9b3ca 2021-03-14 op *
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.
7 65d9b3ca 2021-03-14 op *
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.
15 65d9b3ca 2021-03-14 op */
16 65d9b3ca 2021-03-14 op
17 65d9b3ca 2021-03-14 op #include <stdlib.h>
18 65d9b3ca 2021-03-14 op #include <string.h>
19 65d9b3ca 2021-03-14 op
20 5caf7d67 2021-07-12 op #include "telescope.h"
21 5caf7d67 2021-07-12 op #include "utf8.h"
22 5caf7d67 2021-07-12 op
23 65d9b3ca 2021-03-14 op /*
24 65d9b3ca 2021-03-14 op * Text wrapping
25 65d9b3ca 2021-03-14 op * =============
26 65d9b3ca 2021-03-14 op *
27 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
28 65d9b3ca 2021-03-14 op *
29 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
30 65d9b3ca 2021-03-14 op * a. hard wrap.
31 65d9b3ca 2021-03-14 op * b. repeat
32 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
33 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
34 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
35 65d9b3ca 2021-03-14 op * 4. repeat
36 65d9b3ca 2021-03-14 op *
37 65d9b3ca 2021-03-14 op */
38 65d9b3ca 2021-03-14 op
39 ca5da938 2021-04-01 op void
40 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
41 bca92a4c 2021-07-01 op {
42 bca92a4c 2021-07-01 op empty_vlist(buffer);
43 bca92a4c 2021-07-01 op empty_linelist(buffer);
44 bca92a4c 2021-07-01 op }
45 bca92a4c 2021-07-01 op
46 bca92a4c 2021-07-01 op void
47 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
48 ca5da938 2021-04-01 op {
49 ca5da938 2021-04-01 op struct line *l, *lt;
50 ca5da938 2021-04-01 op
51 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
52 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
53 ca5da938 2021-04-01 op free(l->line);
54 e9beee55 2021-07-14 op
55 e9beee55 2021-07-14 op if (l->type != LINE_COMPL &&
56 e9beee55 2021-07-14 op l->type != LINE_COMPL_CURRENT)
57 e9beee55 2021-07-14 op free(l->meta.alt);
58 e9beee55 2021-07-14 op
59 ca5da938 2021-04-01 op free(l);
60 ca5da938 2021-04-01 op }
61 ca5da938 2021-04-01 op }
62 ca5da938 2021-04-01 op
63 ca5da938 2021-04-01 op void
64 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
65 ca5da938 2021-04-01 op {
66 ca5da938 2021-04-01 op struct vline *vl, *t;
67 ca5da938 2021-04-01 op
68 4ea9160a 2021-07-01 op buffer->top_line = NULL;
69 4ea9160a 2021-07-01 op buffer->line_off = 0;
70 46f6e974 2021-05-17 op buffer->current_line = NULL;
71 46f6e974 2021-05-17 op buffer->line_max = 0;
72 ca5da938 2021-04-01 op
73 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
74 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
75 ca5da938 2021-04-01 op free(vl->line);
76 ca5da938 2021-04-01 op free(vl);
77 ca5da938 2021-04-01 op }
78 ca5da938 2021-04-01 op }
79 ca5da938 2021-04-01 op
80 65d9b3ca 2021-03-14 op static int
81 5492b68f 2021-07-05 op push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int flags)
82 65d9b3ca 2021-03-14 op {
83 65d9b3ca 2021-03-14 op struct vline *vl;
84 65d9b3ca 2021-03-14 op
85 46f6e974 2021-05-17 op buffer->line_max++;
86 65d9b3ca 2021-03-14 op
87 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
88 65d9b3ca 2021-03-14 op return 0;
89 65d9b3ca 2021-03-14 op
90 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
91 65d9b3ca 2021-03-14 op free(vl);
92 65d9b3ca 2021-03-14 op return 0;
93 65d9b3ca 2021-03-14 op }
94 65d9b3ca 2021-03-14 op
95 65d9b3ca 2021-03-14 op vl->parent = l;
96 65d9b3ca 2021-03-14 op if (len != 0)
97 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
98 5492b68f 2021-07-05 op vl->flags = flags;
99 65d9b3ca 2021-03-14 op
100 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
101 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
102 65d9b3ca 2021-03-14 op else
103 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
104 65d9b3ca 2021-03-14 op return 1;
105 65d9b3ca 2021-03-14 op }
106 65d9b3ca 2021-03-14 op
107 65d9b3ca 2021-03-14 op /*
108 e7b982f4 2021-07-14 op * Similar to wrap_text, but emit only o vline.
109 e7b982f4 2021-07-14 op */
110 e7b982f4 2021-07-14 op int
111 e7b982f4 2021-07-14 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
112 e7b982f4 2021-07-14 op {
113 e7b982f4 2021-07-14 op struct vline *vl, *t;
114 e7b982f4 2021-07-14 op
115 e7b982f4 2021-07-14 op /*
116 e7b982f4 2021-07-14 op * be lazy: call wrap_text and then discard the continuations.
117 e7b982f4 2021-07-14 op */
118 e7b982f4 2021-07-14 op
119 e7b982f4 2021-07-14 op if (!wrap_text(buffer, prfx, l, width))
120 e7b982f4 2021-07-14 op return 0;
121 e7b982f4 2021-07-14 op
122 e7b982f4 2021-07-14 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
123 e7b982f4 2021-07-14 op if (vl->flags & L_CONTINUATION) {
124 e7b982f4 2021-07-14 op TAILQ_REMOVE(&buffer->head, vl, vlines);
125 e7b982f4 2021-07-14 op free(vl->line);
126 e7b982f4 2021-07-14 op free(vl);
127 e7b982f4 2021-07-14 op }
128 e7b982f4 2021-07-14 op }
129 e7b982f4 2021-07-14 op
130 e7b982f4 2021-07-14 op return 1;
131 e7b982f4 2021-07-14 op }
132 e7b982f4 2021-07-14 op
133 e7b982f4 2021-07-14 op /*
134 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
135 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
136 65d9b3ca 2021-03-14 op */
137 676fba6f 2021-03-21 op int
138 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
139 65d9b3ca 2021-03-14 op {
140 676fba6f 2021-03-21 op const char *separators = " \t-";
141 676fba6f 2021-03-21 op const char *start, *end, *line, *lastsep, *lastchar;
142 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
143 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
144 5492b68f 2021-07-05 op int flags;
145 65d9b3ca 2021-03-14 op
146 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
147 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
148 65d9b3ca 2021-03-14 op
149 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
150 676fba6f 2021-03-21 op cur = prfxwidth;
151 676fba6f 2021-03-21 op start = line;
152 676fba6f 2021-03-21 op lastsep = NULL;
153 676fba6f 2021-03-21 op lastchar = line;
154 5492b68f 2021-07-05 op flags = 0;
155 676fba6f 2021-03-21 op for (; *line; line++) {
156 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
157 676fba6f 2021-03-21 op continue;
158 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
159 676fba6f 2021-03-21 op if (cur + w >= width -1) {
160 676fba6f 2021-03-21 op end = lastsep == NULL
161 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
162 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
163 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
164 676fba6f 2021-03-21 op return 0;
165 963c680c 2021-07-05 op flags = L_CONTINUATION;
166 676fba6f 2021-03-21 op start = end;
167 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
168 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
169 5b010818 2021-07-05 op lastsep = line;
170 65d9b3ca 2021-03-14 op }
171 65d9b3ca 2021-03-14 op
172 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
173 676fba6f 2021-03-21 op cur += w;
174 65d9b3ca 2021-03-14 op }
175 65d9b3ca 2021-03-14 op
176 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
177 65d9b3ca 2021-03-14 op }
178 65d9b3ca 2021-03-14 op
179 65d9b3ca 2021-03-14 op int
180 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
181 65d9b3ca 2021-03-14 op {
182 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
183 65d9b3ca 2021-03-14 op int cont;
184 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
185 676fba6f 2021-03-21 op size_t cur, w;
186 65d9b3ca 2021-03-14 op
187 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
188 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
189 166712b4 2021-03-14 op
190 676fba6f 2021-03-21 op start = line;
191 676fba6f 2021-03-21 op lastchar = line;
192 676fba6f 2021-03-21 op cont = 0;
193 676fba6f 2021-03-21 op cur = 0;
194 676fba6f 2021-03-21 op for (; *line; line++) {
195 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
196 676fba6f 2021-03-21 op continue;
197 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
198 676fba6f 2021-03-21 op if (cur + w >= width) {
199 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
200 676fba6f 2021-03-21 op return 0;
201 676fba6f 2021-03-21 op cont = 1;
202 676fba6f 2021-03-21 op cur = 0;
203 676fba6f 2021-03-21 op start = lastchar;
204 676fba6f 2021-03-21 op }
205 65d9b3ca 2021-03-14 op
206 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
207 676fba6f 2021-03-21 op cur += w;
208 65d9b3ca 2021-03-14 op }
209 65d9b3ca 2021-03-14 op
210 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
211 65d9b3ca 2021-03-14 op }