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 "telescope.h"
18 65d9b3ca 2021-03-14 op
19 65d9b3ca 2021-03-14 op #include <stdlib.h>
20 65d9b3ca 2021-03-14 op #include <string.h>
21 65d9b3ca 2021-03-14 op
22 65d9b3ca 2021-03-14 op /*
23 65d9b3ca 2021-03-14 op * Text wrapping
24 65d9b3ca 2021-03-14 op * =============
25 65d9b3ca 2021-03-14 op *
26 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
27 65d9b3ca 2021-03-14 op *
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.
30 65d9b3ca 2021-03-14 op * b. repeat
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.
34 65d9b3ca 2021-03-14 op * 4. repeat
35 65d9b3ca 2021-03-14 op *
36 65d9b3ca 2021-03-14 op */
37 65d9b3ca 2021-03-14 op
38 ca5da938 2021-04-01 op void
39 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
40 bca92a4c 2021-07-01 op {
41 bca92a4c 2021-07-01 op empty_vlist(buffer);
42 bca92a4c 2021-07-01 op empty_linelist(buffer);
43 bca92a4c 2021-07-01 op }
44 bca92a4c 2021-07-01 op
45 bca92a4c 2021-07-01 op void
46 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
47 ca5da938 2021-04-01 op {
48 ca5da938 2021-04-01 op struct line *l, *lt;
49 ca5da938 2021-04-01 op
50 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
51 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
52 ca5da938 2021-04-01 op free(l->line);
53 ca5da938 2021-04-01 op free(l->alt);
54 ca5da938 2021-04-01 op free(l);
55 ca5da938 2021-04-01 op }
56 ca5da938 2021-04-01 op }
57 ca5da938 2021-04-01 op
58 ca5da938 2021-04-01 op void
59 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
60 ca5da938 2021-04-01 op {
61 ca5da938 2021-04-01 op struct vline *vl, *t;
62 ca5da938 2021-04-01 op
63 4ea9160a 2021-07-01 op buffer->top_line = NULL;
64 4ea9160a 2021-07-01 op buffer->line_off = 0;
65 46f6e974 2021-05-17 op buffer->current_line = NULL;
66 46f6e974 2021-05-17 op buffer->line_max = 0;
67 ca5da938 2021-04-01 op
68 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
69 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
70 ca5da938 2021-04-01 op free(vl->line);
71 ca5da938 2021-04-01 op free(vl);
72 ca5da938 2021-04-01 op }
73 ca5da938 2021-04-01 op }
74 ca5da938 2021-04-01 op
75 65d9b3ca 2021-03-14 op static int
76 5492b68f 2021-07-05 op push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int flags)
77 65d9b3ca 2021-03-14 op {
78 65d9b3ca 2021-03-14 op struct vline *vl;
79 65d9b3ca 2021-03-14 op
80 46f6e974 2021-05-17 op buffer->line_max++;
81 65d9b3ca 2021-03-14 op
82 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
83 65d9b3ca 2021-03-14 op return 0;
84 65d9b3ca 2021-03-14 op
85 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
86 65d9b3ca 2021-03-14 op free(vl);
87 65d9b3ca 2021-03-14 op return 0;
88 65d9b3ca 2021-03-14 op }
89 65d9b3ca 2021-03-14 op
90 65d9b3ca 2021-03-14 op vl->parent = l;
91 65d9b3ca 2021-03-14 op if (len != 0)
92 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
93 5492b68f 2021-07-05 op vl->flags = flags;
94 65d9b3ca 2021-03-14 op
95 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
96 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
97 65d9b3ca 2021-03-14 op else
98 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
99 65d9b3ca 2021-03-14 op return 1;
100 65d9b3ca 2021-03-14 op }
101 65d9b3ca 2021-03-14 op
102 65d9b3ca 2021-03-14 op /*
103 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
104 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
105 65d9b3ca 2021-03-14 op */
106 676fba6f 2021-03-21 op int
107 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
108 65d9b3ca 2021-03-14 op {
109 676fba6f 2021-03-21 op const char *separators = " \t-";
110 676fba6f 2021-03-21 op const char *start, *end, *line, *lastsep, *lastchar;
111 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
112 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
113 5492b68f 2021-07-05 op int flags;
114 65d9b3ca 2021-03-14 op
115 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
116 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
117 65d9b3ca 2021-03-14 op
118 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
119 676fba6f 2021-03-21 op cur = prfxwidth;
120 676fba6f 2021-03-21 op start = line;
121 676fba6f 2021-03-21 op lastsep = NULL;
122 676fba6f 2021-03-21 op lastchar = line;
123 5492b68f 2021-07-05 op flags = 0;
124 676fba6f 2021-03-21 op for (; *line; line++) {
125 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
126 676fba6f 2021-03-21 op continue;
127 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
128 676fba6f 2021-03-21 op if (cur + w >= width -1) {
129 676fba6f 2021-03-21 op end = lastsep == NULL
130 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
131 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
132 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
133 676fba6f 2021-03-21 op return 0;
134 963c680c 2021-07-05 op flags = L_CONTINUATION;
135 676fba6f 2021-03-21 op start = end;
136 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
137 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
138 5b010818 2021-07-05 op lastsep = line;
139 65d9b3ca 2021-03-14 op }
140 65d9b3ca 2021-03-14 op
141 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
142 676fba6f 2021-03-21 op cur += w;
143 65d9b3ca 2021-03-14 op }
144 65d9b3ca 2021-03-14 op
145 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
146 65d9b3ca 2021-03-14 op }
147 65d9b3ca 2021-03-14 op
148 65d9b3ca 2021-03-14 op int
149 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
150 65d9b3ca 2021-03-14 op {
151 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
152 65d9b3ca 2021-03-14 op int cont;
153 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
154 676fba6f 2021-03-21 op size_t cur, w;
155 65d9b3ca 2021-03-14 op
156 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
157 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
158 166712b4 2021-03-14 op
159 676fba6f 2021-03-21 op start = line;
160 676fba6f 2021-03-21 op lastchar = line;
161 676fba6f 2021-03-21 op cont = 0;
162 676fba6f 2021-03-21 op cur = 0;
163 676fba6f 2021-03-21 op for (; *line; line++) {
164 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
165 676fba6f 2021-03-21 op continue;
166 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
167 676fba6f 2021-03-21 op if (cur + w >= width) {
168 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
169 676fba6f 2021-03-21 op return 0;
170 676fba6f 2021-03-21 op cont = 1;
171 676fba6f 2021-03-21 op cur = 0;
172 676fba6f 2021-03-21 op start = lastchar;
173 676fba6f 2021-03-21 op }
174 65d9b3ca 2021-03-14 op
175 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
176 676fba6f 2021-03-21 op cur += w;
177 65d9b3ca 2021-03-14 op }
178 65d9b3ca 2021-03-14 op
179 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);
180 65d9b3ca 2021-03-14 op }