Line | Count | Source |
1 | | /* $OpenBSD: style.c,v 1.48 2026/10/16 07:59:06 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net> |
6 | | * |
7 | | * Permission to use, copy, modify, and distribute this software for any |
8 | | * purpose with or without fee is hereby granted, provided that the above |
9 | | * copyright notice and this permission notice appear in all copies. |
10 | | * |
11 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
16 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
17 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 | | */ |
19 | | |
20 | | #include <sys/types.h> |
21 | | |
22 | | #include <ctype.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "tmux.h" |
27 | | |
28 | | /* Mask for bits not included in style. */ |
29 | | #define STYLE_ATTR_MASK (~0) |
30 | | |
31 | | /* Default style. */ |
32 | | static struct style style_default = { |
33 | | { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0, 0 }, |
34 | | 0, |
35 | | 0, |
36 | | |
37 | | 8, |
38 | | STYLE_ALIGN_DEFAULT, |
39 | | STYLE_LIST_OFF, |
40 | | |
41 | | STYLE_RANGE_NONE, 0, "", |
42 | | |
43 | | STYLE_WIDTH_DEFAULT, 0, STYLE_PAD_DEFAULT, |
44 | | 0, |
45 | | 0, |
46 | | |
47 | | STYLE_DEFAULT_BASE |
48 | | }; |
49 | | |
50 | | /* |
51 | | * Global hyperlink set holding the URIs for #[link=...] styles, so a style |
52 | | * only needs to store a small ID rather than the URI itself. |
53 | | */ |
54 | | static struct hyperlinks *style_hyperlinks; |
55 | | |
56 | | /* Set range string. */ |
57 | | static void |
58 | | style_set_range_string(struct style *sy, const char *s) |
59 | 2.87k | { |
60 | 2.87k | strlcpy(sy->range_string, s, sizeof sy->range_string); |
61 | 2.87k | } |
62 | | |
63 | | /* |
64 | | * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note |
65 | | * that this adds onto the given style, so it must have been initialized |
66 | | * already. |
67 | | */ |
68 | | int |
69 | | style_parse(struct style *sy, const struct grid_cell *base, const char *in) |
70 | 1.02M | { |
71 | 1.02M | struct style saved; |
72 | 1.02M | const char delimiters[] = " ,\n", *errstr; |
73 | 1.02M | char tmp[256], *found; |
74 | 1.02M | int value; |
75 | 1.02M | size_t end; |
76 | 1.02M | u_int n; |
77 | | |
78 | 1.02M | if (*in == '\0') |
79 | 486 | return (0); |
80 | 1.02M | style_copy(&saved, sy); |
81 | | |
82 | 1.02M | log_debug("%s: %s", __func__, in); |
83 | 1.10M | do { |
84 | 1.33M | while (*in != '\0' && strchr(delimiters, *in) != NULL) |
85 | 234k | in++; |
86 | 1.10M | if (*in == '\0') |
87 | 456 | break; |
88 | | |
89 | 1.10M | end = strcspn(in, delimiters); |
90 | 1.10M | if (end > (sizeof tmp) - 1) |
91 | 830 | goto error; |
92 | 1.09M | memcpy(tmp, in, end); |
93 | 1.09M | tmp[end] = '\0'; |
94 | | |
95 | 1.09M | log_debug("%s: %s", __func__, tmp); |
96 | 1.09M | if (strcasecmp(tmp, "default") == 0) { |
97 | 5.84k | sy->gc.fg = base->fg; |
98 | 5.84k | sy->gc.bg = base->bg; |
99 | 5.84k | sy->gc.us = base->us; |
100 | 5.84k | sy->gc.attr = base->attr; |
101 | 5.84k | sy->gc.flags = base->flags; |
102 | 5.84k | sy->link = 0; |
103 | 1.09M | } else if (strcasecmp(tmp, "ignore") == 0) |
104 | 220 | sy->ignore = 1; |
105 | 1.09M | else if (strcasecmp(tmp, "noignore") == 0) |
106 | 221 | sy->ignore = 0; |
107 | 1.09M | else if (strcasecmp(tmp, "push-default") == 0) |
108 | 4.67k | sy->default_type = STYLE_DEFAULT_PUSH; |
109 | 1.08M | else if (strcasecmp(tmp, "pop-default") == 0) |
110 | 4.66k | sy->default_type = STYLE_DEFAULT_POP; |
111 | 1.08M | else if (strcasecmp(tmp, "set-default") == 0) |
112 | 17.4k | sy->default_type = STYLE_DEFAULT_SET; |
113 | 1.06M | else if (strcasecmp(tmp, "nl") == 0) |
114 | 0 | sy->nl = 1; |
115 | 1.06M | else if (strcasecmp(tmp, "nolist") == 0) |
116 | 584 | sy->list = STYLE_LIST_OFF; |
117 | 1.06M | else if (strncasecmp(tmp, "list=", 5) == 0) { |
118 | 45.4k | if (strcasecmp(tmp + 5, "on") == 0) |
119 | 6.42k | sy->list = STYLE_LIST_ON; |
120 | 39.0k | else if (strcasecmp(tmp + 5, "focus") == 0) |
121 | 194 | sy->list = STYLE_LIST_FOCUS; |
122 | 38.8k | else if (strcasecmp(tmp + 5, "left-marker") == 0) |
123 | 1.75k | sy->list = STYLE_LIST_LEFT_MARKER; |
124 | 37.1k | else if (strcasecmp(tmp + 5, "right-marker") == 0) |
125 | 1.75k | sy->list = STYLE_LIST_RIGHT_MARKER; |
126 | 35.3k | else |
127 | 35.3k | goto error; |
128 | 1.02M | } else if (strcasecmp(tmp, "norange") == 0) { |
129 | 5.83k | sy->range_type = style_default.range_type; |
130 | 5.83k | sy->range_argument = style_default.range_type; |
131 | 5.83k | strlcpy(sy->range_string, style_default.range_string, |
132 | 5.83k | sizeof sy->range_string); |
133 | 1.01M | } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) { |
134 | 31.0k | found = strchr(tmp + 6, '|'); |
135 | 31.0k | if (found != NULL) { |
136 | 25.9k | *found++ = '\0'; |
137 | 25.9k | if (*found == '\0') |
138 | 307 | goto error; |
139 | 25.9k | } |
140 | 30.7k | if (strcasecmp(tmp + 6, "left") == 0) { |
141 | 883 | if (found != NULL) |
142 | 297 | goto error; |
143 | 586 | sy->range_type = STYLE_RANGE_LEFT; |
144 | 586 | sy->range_argument = 0; |
145 | 586 | style_set_range_string(sy, ""); |
146 | 29.8k | } else if (strcasecmp(tmp + 6, "right") == 0) { |
147 | 879 | if (found != NULL) |
148 | 297 | goto error; |
149 | 582 | sy->range_type = STYLE_RANGE_RIGHT; |
150 | 582 | sy->range_argument = 0; |
151 | 582 | style_set_range_string(sy, ""); |
152 | 28.9k | } else if (strcasecmp(tmp + 6, "control") == 0) { |
153 | 679 | if (found == NULL) |
154 | 283 | goto error; |
155 | 396 | n = strtonum(found, 0, 9, &errstr); |
156 | 396 | if (errstr != NULL) |
157 | 198 | goto error; |
158 | 198 | sy->range_type = STYLE_RANGE_CONTROL; |
159 | 198 | sy->range_argument = n; |
160 | 198 | style_set_range_string(sy, ""); |
161 | 28.2k | } else if (strcasecmp(tmp + 6, "pane") == 0) { |
162 | 2.17k | if (found == NULL) |
163 | 198 | goto error; |
164 | 1.98k | if (*found != '%' || found[1] == '\0') |
165 | 1.36k | goto error; |
166 | 612 | n = strtonum(found + 1, 0, UINT_MAX, &errstr); |
167 | 612 | if (errstr != NULL) |
168 | 277 | goto error; |
169 | 335 | sy->range_type = STYLE_RANGE_PANE; |
170 | 335 | sy->range_argument = n; |
171 | 335 | style_set_range_string(sy, ""); |
172 | 26.1k | } else if (strcasecmp(tmp + 6, "window") == 0) { |
173 | 1.96k | if (found == NULL) |
174 | 221 | goto error; |
175 | 1.74k | n = strtonum(found, 0, UINT_MAX, &errstr); |
176 | 1.74k | if (errstr != NULL) |
177 | 1.38k | goto error; |
178 | 361 | sy->range_type = STYLE_RANGE_WINDOW; |
179 | 361 | sy->range_argument = n; |
180 | 361 | style_set_range_string(sy, ""); |
181 | 24.1k | } else if (strcasecmp(tmp + 6, "session") == 0) { |
182 | 2.26k | if (found == NULL) |
183 | 297 | goto error; |
184 | 1.96k | if (*found != '$' || found[1] == '\0') |
185 | 1.43k | goto error; |
186 | 533 | n = strtonum(found + 1, 0, UINT_MAX, &errstr); |
187 | 533 | if (errstr != NULL) |
188 | 202 | goto error; |
189 | 331 | sy->range_type = STYLE_RANGE_SESSION; |
190 | 331 | sy->range_argument = n; |
191 | 331 | style_set_range_string(sy, ""); |
192 | 21.8k | } else if (strcasecmp(tmp + 6, "user") == 0) { |
193 | 756 | if (found == NULL) |
194 | 278 | goto error; |
195 | 478 | sy->range_type = STYLE_RANGE_USER; |
196 | 478 | sy->range_argument = 0; |
197 | 478 | style_set_range_string(sy, found); |
198 | 478 | } |
199 | 983k | } else if (strcasecmp(tmp, "noalign") == 0) |
200 | 282 | sy->align = style_default.align; |
201 | 983k | else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) { |
202 | 5.94k | if (strcasecmp(tmp + 6, "left") == 0) |
203 | 1.77k | sy->align = STYLE_ALIGN_LEFT; |
204 | 4.16k | else if (strcasecmp(tmp + 6, "centre") == 0) |
205 | 395 | sy->align = STYLE_ALIGN_CENTRE; |
206 | 3.77k | else if (strcasecmp(tmp + 6, "right") == 0) |
207 | 593 | sy->align = STYLE_ALIGN_RIGHT; |
208 | 3.18k | else if (strcasecmp(tmp + 6, "absolute-centre") == 0) |
209 | 569 | sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE; |
210 | 2.61k | else |
211 | 2.61k | goto error; |
212 | 977k | } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) { |
213 | 16.7k | if ((value = colour_fromstring(tmp + 5)) == -1) |
214 | 16.4k | goto error; |
215 | 346 | sy->fill = value; |
216 | 960k | } else if (end > 4 && strncasecmp(tmp, "dim=", 4) == 0) { |
217 | 1.02k | if (tmp[end - 1] == '%') |
218 | 413 | tmp[end - 1] = '\0'; |
219 | 1.02k | n = strtonum(tmp + 4, 0, 100, &errstr); |
220 | 1.02k | if (errstr != NULL) |
221 | 823 | goto error; |
222 | 200 | sy->dim = n; |
223 | 959k | } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { |
224 | 16.0k | if ((value = colour_fromstring(tmp + 3)) == -1) |
225 | 4.70k | goto error; |
226 | 11.3k | if (*in == 'f' || *in == 'F') { |
227 | 952 | if (value != 8) |
228 | 677 | sy->gc.fg = value; |
229 | 275 | else |
230 | 275 | sy->gc.fg = base->fg; |
231 | 10.4k | } else if (*in == 'b' || *in == 'B') { |
232 | 10.0k | if (value != 8) |
233 | 9.68k | sy->gc.bg = value; |
234 | 368 | else |
235 | 368 | sy->gc.bg = base->bg; |
236 | 10.0k | } else |
237 | 374 | goto error; |
238 | 943k | } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) { |
239 | 1.02k | if ((value = colour_fromstring(tmp + 3)) == -1) |
240 | 455 | goto error; |
241 | 569 | if (value != 8) |
242 | 303 | sy->gc.us = value; |
243 | 266 | else |
244 | 266 | sy->gc.us = base->us; |
245 | 942k | } else if (strcasecmp(tmp, "none") == 0) |
246 | 203 | sy->gc.attr = 0; |
247 | 942k | else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { |
248 | 40.9k | if (strcmp(tmp + 2, "link") == 0) |
249 | 278 | sy->link = 0; |
250 | 40.7k | else if (strcmp(tmp + 2, "attr") == 0) |
251 | 205 | sy->gc.attr |= GRID_ATTR_NOATTR; |
252 | 40.5k | else { |
253 | 40.5k | value = attributes_fromstring(tmp + 2); |
254 | 40.5k | if (value == -1) |
255 | 40.0k | goto error; |
256 | 477 | sy->gc.attr &= ~value; |
257 | 477 | } |
258 | 901k | } else if (end > 6 && strncasecmp(tmp, "width=", 6) == 0) { |
259 | 72.8k | if (end > 7 && tmp[end - 1] == '%') { |
260 | 2.76k | tmp[end - 1] = '\0'; |
261 | 2.76k | n = strtonum(tmp + 6, 0, 100, &errstr); |
262 | 2.76k | if (errstr != NULL) |
263 | 1.38k | goto error; |
264 | 1.38k | sy->width = (int)n; |
265 | 1.38k | sy->width_percentage = 1; |
266 | 70.0k | } else { |
267 | 70.0k | n = strtonum(tmp + 6, 0, UINT_MAX, &errstr); |
268 | 70.0k | if (errstr != NULL) |
269 | 69.8k | goto error; |
270 | 205 | sy->width = (int)n; |
271 | 205 | sy->width_percentage = 0; |
272 | 205 | } |
273 | 828k | } else if (end > 4 && strncasecmp(tmp, "pad=", 4) == 0) { |
274 | 897 | n = strtonum(tmp + 4, 0, UINT_MAX, &errstr); |
275 | 897 | if (errstr != NULL) |
276 | 543 | goto error; |
277 | 354 | sy->pad = (int)n; |
278 | 827k | } else if (strncasecmp(tmp, "link=", 5) == 0) { |
279 | 529k | if (tmp[5] == '\0') |
280 | 1.47k | sy->link = 0; |
281 | 528k | else { |
282 | 528k | if (style_hyperlinks == NULL) |
283 | 1 | style_hyperlinks = hyperlinks_init(); |
284 | 528k | sy->link = hyperlinks_put(style_hyperlinks, |
285 | 528k | tmp + 5, tmp + 5); |
286 | 528k | } |
287 | 529k | } else { |
288 | 297k | if ((value = attributes_fromstring(tmp)) == -1) |
289 | 297k | goto error; |
290 | 542 | sy->gc.attr |= value; |
291 | 542 | } |
292 | | |
293 | 622k | in += end + strspn(in + end, delimiters); |
294 | 622k | } while (*in != '\0'); |
295 | | |
296 | 550k | return (0); |
297 | | |
298 | 477k | error: |
299 | 477k | style_copy(sy, &saved); |
300 | 477k | return (-1); |
301 | 1.02M | } |
302 | | |
303 | | /* Convert style to a string. */ |
304 | | const char * |
305 | | style_tostring(struct style *sy) |
306 | 0 | { |
307 | 0 | struct grid_cell *gc = &sy->gc; |
308 | 0 | int off = 0; |
309 | 0 | const char *comma = "", *tmp = "", *uri; |
310 | 0 | static char s[2048]; |
311 | 0 | char b[21]; |
312 | |
|
313 | 0 | *s = '\0'; |
314 | |
|
315 | 0 | if (sy->list != STYLE_LIST_OFF) { |
316 | 0 | if (sy->list == STYLE_LIST_ON) |
317 | 0 | tmp = "on"; |
318 | 0 | else if (sy->list == STYLE_LIST_FOCUS) |
319 | 0 | tmp = "focus"; |
320 | 0 | else if (sy->list == STYLE_LIST_LEFT_MARKER) |
321 | 0 | tmp = "left-marker"; |
322 | 0 | else if (sy->list == STYLE_LIST_RIGHT_MARKER) |
323 | 0 | tmp = "right-marker"; |
324 | 0 | off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma, |
325 | 0 | tmp); |
326 | 0 | comma = ","; |
327 | 0 | } |
328 | 0 | if (sy->range_type != STYLE_RANGE_NONE) { |
329 | 0 | if (sy->range_type == STYLE_RANGE_LEFT) |
330 | 0 | tmp = "left"; |
331 | 0 | else if (sy->range_type == STYLE_RANGE_RIGHT) |
332 | 0 | tmp = "right"; |
333 | 0 | else if (sy->range_type == STYLE_RANGE_PANE) { |
334 | 0 | snprintf(b, sizeof b, "pane|%%%u", sy->range_argument); |
335 | 0 | tmp = b; |
336 | 0 | } else if (sy->range_type == STYLE_RANGE_WINDOW) { |
337 | 0 | snprintf(b, sizeof b, "window|%u", sy->range_argument); |
338 | 0 | tmp = b; |
339 | 0 | } else if (sy->range_type == STYLE_RANGE_SESSION) { |
340 | 0 | snprintf(b, sizeof b, "session|$%u", |
341 | 0 | sy->range_argument); |
342 | 0 | tmp = b; |
343 | 0 | } else if (sy->range_type == STYLE_RANGE_USER) { |
344 | 0 | snprintf(b, sizeof b, "user|%s", sy->range_string); |
345 | 0 | tmp = b; |
346 | 0 | } |
347 | 0 | off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma, |
348 | 0 | tmp); |
349 | 0 | comma = ","; |
350 | 0 | } |
351 | 0 | if (sy->align != STYLE_ALIGN_DEFAULT) { |
352 | 0 | if (sy->align == STYLE_ALIGN_LEFT) |
353 | 0 | tmp = "left"; |
354 | 0 | else if (sy->align == STYLE_ALIGN_CENTRE) |
355 | 0 | tmp = "centre"; |
356 | 0 | else if (sy->align == STYLE_ALIGN_RIGHT) |
357 | 0 | tmp = "right"; |
358 | 0 | else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE) |
359 | 0 | tmp = "absolute-centre"; |
360 | 0 | off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma, |
361 | 0 | tmp); |
362 | 0 | comma = ","; |
363 | 0 | } |
364 | 0 | if (sy->default_type != STYLE_DEFAULT_BASE) { |
365 | 0 | if (sy->default_type == STYLE_DEFAULT_PUSH) |
366 | 0 | tmp = "push-default"; |
367 | 0 | else if (sy->default_type == STYLE_DEFAULT_POP) |
368 | 0 | tmp = "pop-default"; |
369 | 0 | else if (sy->default_type == STYLE_DEFAULT_SET) |
370 | 0 | tmp = "set-default"; |
371 | 0 | off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp); |
372 | 0 | comma = ","; |
373 | 0 | } |
374 | 0 | if (sy->fill != 8) { |
375 | 0 | off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma, |
376 | 0 | colour_tostring(sy->fill)); |
377 | 0 | comma = ","; |
378 | 0 | } |
379 | 0 | if (sy->dim != 0) { |
380 | 0 | off += xsnprintf(s + off, sizeof s - off, "%sdim=%d%%", comma, |
381 | 0 | sy->dim); |
382 | 0 | comma = ","; |
383 | 0 | } |
384 | 0 | if (gc->fg != 8) { |
385 | 0 | off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma, |
386 | 0 | colour_tostring(gc->fg)); |
387 | 0 | comma = ","; |
388 | 0 | } |
389 | 0 | if (gc->bg != 8) { |
390 | 0 | off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma, |
391 | 0 | colour_tostring(gc->bg)); |
392 | 0 | comma = ","; |
393 | 0 | } |
394 | 0 | if (gc->us != 8) { |
395 | 0 | off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma, |
396 | 0 | colour_tostring(gc->us)); |
397 | 0 | comma = ","; |
398 | 0 | } |
399 | 0 | if (gc->attr != 0) { |
400 | 0 | off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, |
401 | 0 | attributes_tostring(gc->attr)); |
402 | 0 | comma = ","; |
403 | 0 | } |
404 | 0 | if (sy->width >= 0) { |
405 | 0 | if (sy->width_percentage) |
406 | 0 | off += xsnprintf(s + off, sizeof s - off, |
407 | 0 | "%swidth=%u%%", comma, sy->width); |
408 | 0 | else |
409 | 0 | off += xsnprintf(s + off, sizeof s - off, |
410 | 0 | "%swidth=%u", comma, sy->width); |
411 | 0 | comma = ","; |
412 | 0 | } |
413 | 0 | if (sy->pad >= 0) { |
414 | 0 | off += xsnprintf(s + off, sizeof s - off, "%spad=%u", comma, |
415 | 0 | sy->pad); |
416 | 0 | comma = ","; |
417 | 0 | } |
418 | 0 | uri = style_link(sy); |
419 | 0 | if (uri != NULL) { |
420 | 0 | xsnprintf(s + off, sizeof s - off, "%slink=%s", comma, uri); |
421 | 0 | comma = ","; |
422 | 0 | } |
423 | 0 | if (*s == '\0') |
424 | 0 | return ("default"); |
425 | 0 | return (s); |
426 | 0 | } |
427 | | |
428 | | /* Get the hyperlink URI for a style, or NULL if it has none. */ |
429 | | const char * |
430 | | style_link(struct style *sy) |
431 | 0 | { |
432 | 0 | const char *uri; |
433 | |
|
434 | 0 | if (sy->link == 0 || style_hyperlinks == NULL) |
435 | 0 | return (NULL); |
436 | 0 | if (!hyperlinks_get(style_hyperlinks, sy->link, &uri, NULL, NULL)) |
437 | 0 | return (NULL); |
438 | 0 | return (uri); |
439 | 0 | } |
440 | | |
441 | | /* Apply a style on top of the given style. */ |
442 | | struct style * |
443 | | style_add(struct grid_cell *gc, struct options *oo, const char *name, |
444 | | struct format_tree *ft) |
445 | 0 | { |
446 | 0 | struct style *sy; |
447 | 0 | struct format_tree *ft0 = NULL; |
448 | |
|
449 | 0 | if (ft == NULL) |
450 | 0 | ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS); |
451 | |
|
452 | 0 | sy = options_string_to_style(oo, name, ft); |
453 | 0 | if (sy == NULL) |
454 | 0 | sy = &style_default; |
455 | 0 | if (sy->gc.fg != 8) |
456 | 0 | gc->fg = sy->gc.fg; |
457 | 0 | if (sy->gc.bg != 8) |
458 | 0 | gc->bg = sy->gc.bg; |
459 | 0 | if (sy->gc.us != 8) |
460 | 0 | gc->us = sy->gc.us; |
461 | 0 | gc->attr |= sy->gc.attr; |
462 | |
|
463 | 0 | if (ft0 != NULL) |
464 | 0 | format_free(ft0); |
465 | 0 | return (sy); |
466 | 0 | } |
467 | | |
468 | | /* Apply a style on top of the default style. */ |
469 | | void |
470 | | style_apply(struct grid_cell *gc, struct options *oo, const char *name, |
471 | | struct format_tree *ft) |
472 | 0 | { |
473 | 0 | memcpy(gc, &grid_default_cell, sizeof *gc); |
474 | 0 | style_add(gc, oo, name, ft); |
475 | 0 | } |
476 | | |
477 | | /* Parse a single colour into a style */ |
478 | | int |
479 | | style_parse_colour(struct style *sy, const struct grid_cell *base, |
480 | | const char *s) |
481 | 0 | { |
482 | 0 | int c; |
483 | |
|
484 | 0 | style_set(sy, base); |
485 | |
|
486 | 0 | if (*s == '\0') { |
487 | 0 | sy->gc.fg = -1; |
488 | 0 | return (0); |
489 | 0 | } |
490 | | |
491 | 0 | if ((c = colour_fromstring(s)) == -1) |
492 | 0 | return (-1); |
493 | 0 | if (c == 8) |
494 | 0 | sy->gc.fg = base->fg; |
495 | 0 | else |
496 | 0 | sy->gc.fg = c; |
497 | 0 | return (0); |
498 | 0 | } |
499 | | |
500 | | /* Initialize style from cell. */ |
501 | | void |
502 | | style_set(struct style *sy, const struct grid_cell *gc) |
503 | 65.8k | { |
504 | 65.8k | memcpy(sy, &style_default, sizeof *sy); |
505 | 65.8k | memcpy(&sy->gc, gc, sizeof sy->gc); |
506 | 65.8k | } |
507 | | |
508 | | /* Copy style. */ |
509 | | void |
510 | | style_copy(struct style *dst, struct style *src) |
511 | 1.50M | { |
512 | 1.50M | memcpy(dst, src, sizeof *dst); |
513 | 1.50M | } |
514 | | |
515 | | /* Set scrollbar style from an option. */ |
516 | | void |
517 | | style_set_scrollbar_style_from_option(struct style *sb_style, |
518 | | struct options *oo) |
519 | 0 | { |
520 | 0 | const struct options_table_entry *oe; |
521 | 0 | struct options_entry *o; |
522 | 0 | const char *s; |
523 | 0 | char *style, *expanded; |
524 | |
|
525 | 0 | style_set(sb_style, &grid_default_cell); |
526 | 0 | o = options_get(oo, "pane-scrollbars-style"); |
527 | 0 | if (o == NULL) |
528 | 0 | fatalx("missing pane-scrollbars-style"); |
529 | 0 | oe = options_table_entry(o); |
530 | 0 | style = format_single(NULL, oe->default_str, NULL, NULL, NULL, NULL); |
531 | 0 | if (style_parse(sb_style, &grid_default_cell, style) != 0) |
532 | 0 | fatalx("bad pane-scrollbars-style default"); |
533 | | |
534 | 0 | s = options_get_string(oo, "pane-scrollbars-style"); |
535 | 0 | if (s != NULL) { |
536 | 0 | expanded = format_single(NULL, s, NULL, NULL, NULL, NULL); |
537 | 0 | if (style_parse(sb_style, &grid_default_cell, expanded) != 0) |
538 | 0 | style_parse(sb_style, &grid_default_cell, style); |
539 | 0 | free(expanded); |
540 | 0 | } |
541 | 0 | free(style); |
542 | 0 | if (sb_style->width < 1) |
543 | 0 | sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH; |
544 | 0 | if (sb_style->pad < 0) |
545 | 0 | sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING; |
546 | 0 | utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER); |
547 | 0 | } |
548 | | |
549 | | /* Initialize style ranges. */ |
550 | | void |
551 | | style_ranges_init(struct style_ranges *srs) |
552 | 0 | { |
553 | 0 | TAILQ_INIT(srs); |
554 | 0 | } |
555 | | |
556 | | /* Free style ranges. */ |
557 | | void |
558 | | style_ranges_free(struct style_ranges *srs) |
559 | 0 | { |
560 | 0 | struct style_range *sr, *sr1; |
561 | |
|
562 | 0 | TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) { |
563 | 0 | TAILQ_REMOVE(srs, sr, entry); |
564 | 0 | free(sr); |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | | /* Get range for position from style ranges. */ |
569 | | struct style_range * |
570 | | style_ranges_get_range(struct style_ranges *srs, u_int x) |
571 | 0 | { |
572 | 0 | struct style_range *sr; |
573 | |
|
574 | 0 | if (srs == NULL) |
575 | 0 | return (NULL); |
576 | 0 | TAILQ_FOREACH(sr, srs, entry) { |
577 | 0 | if (x >= sr->start && x < sr->end) |
578 | 0 | return (sr); |
579 | 0 | } |
580 | 0 | return (NULL); |
581 | 0 | } |
582 | | |
583 | | /* Get range for position and row from style ranges. */ |
584 | | struct style_range * |
585 | | style_ranges_get_range_at(struct style_ranges *srs, u_int x, u_int y) |
586 | 0 | { |
587 | 0 | struct style_range *sr; |
588 | |
|
589 | 0 | if (srs == NULL) |
590 | 0 | return (NULL); |
591 | 0 | TAILQ_FOREACH(sr, srs, entry) { |
592 | 0 | if (x >= sr->start && x < sr->end && y == sr->y) |
593 | 0 | return (sr); |
594 | 0 | } |
595 | 0 | return (NULL); |
596 | 0 | } |