xref: /openbsd/usr.bin/tmux/style.c (revision 274d7c50)
1 /* $OpenBSD: style.c,v 1.24 2019/09/15 21:42:57 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 (~GRID_ATTR_CHARSET)
30 
31 /* Default style. */
32 static struct style style_default = {
33 	{ { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0  },
34 
35 	8,
36 	STYLE_ALIGN_DEFAULT,
37 	STYLE_LIST_OFF,
38 
39 	STYLE_RANGE_NONE, 0,
40 
41 	STYLE_DEFAULT_BASE
42 };
43 
44 /*
45  * Parse an embedded style of the form "fg=colour,bg=colour,bright,...".  Note
46  * that this adds onto the given style, so it must have been initialized
47  * already.
48  */
49 int
50 style_parse(struct style *sy, const struct grid_cell *base, const char *in)
51 {
52 	struct style	saved;
53 	const char	delimiters[] = " ,", *cp;
54 	char		tmp[256], *found;
55 	int		value;
56 	size_t		end;
57 
58 	if (*in == '\0')
59 		return (0);
60 	style_copy(&saved, sy);
61 
62 	do {
63 		while (*in != '\0' && strchr(delimiters, *in) != NULL)
64 			in++;
65 		if (*in == '\0')
66 			break;
67 
68 		end = strcspn(in, delimiters);
69 		if (end > (sizeof tmp) - 1)
70 			goto error;
71 		memcpy(tmp, in, end);
72 		tmp[end] = '\0';
73 
74 		if (strcasecmp(tmp, "default") == 0) {
75 			sy->gc.fg = base->fg;
76 			sy->gc.bg = base->bg;
77 			sy->gc.attr = base->attr;
78 			sy->gc.flags = base->flags;
79 		} else if (strcasecmp(tmp, "push-default") == 0)
80 			sy->default_type = STYLE_DEFAULT_PUSH;
81 		else if (strcasecmp(tmp, "pop-default") == 0)
82 			sy->default_type = STYLE_DEFAULT_POP;
83 		else if (strcasecmp(tmp, "nolist") == 0)
84 			sy->list = STYLE_LIST_OFF;
85 		else if (strncasecmp(tmp, "list=", 5) == 0) {
86 			if (strcasecmp(tmp + 5, "on") == 0)
87 				sy->list = STYLE_LIST_ON;
88 			else if (strcasecmp(tmp + 5, "focus") == 0)
89 				sy->list = STYLE_LIST_FOCUS;
90 			else if (strcasecmp(tmp + 5, "left-marker") == 0)
91 				sy->list = STYLE_LIST_LEFT_MARKER;
92 			else if (strcasecmp(tmp + 5, "right-marker") == 0)
93 				sy->list = STYLE_LIST_RIGHT_MARKER;
94 			else
95 				goto error;
96 		} else if (strcasecmp(tmp, "norange") == 0) {
97 			sy->range_type = style_default.range_type;
98 			sy->range_argument = style_default.range_type;
99 		} else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
100 			found = strchr(tmp + 6, '|');
101 			if (found != NULL) {
102 				*found++ = '\0';
103 				if (*found == '\0')
104 					goto error;
105 				for (cp = found; *cp != '\0'; cp++) {
106 					if (!isdigit((u_char)*cp))
107 						goto error;
108 				}
109 			}
110 			if (strcasecmp(tmp + 6, "left") == 0) {
111 				if (found != NULL)
112 					goto error;
113 				sy->range_type = STYLE_RANGE_LEFT;
114 				sy->range_argument = 0;
115 			} else if (strcasecmp(tmp + 6, "right") == 0) {
116 				if (found != NULL)
117 					goto error;
118 				sy->range_type = STYLE_RANGE_RIGHT;
119 				sy->range_argument = 0;
120 			} else if (strcasecmp(tmp + 6, "window") == 0) {
121 				if (found == NULL)
122 					goto error;
123 				sy->range_type = STYLE_RANGE_WINDOW;
124 				sy->range_argument = atoi(found);
125 			}
126 		} else if (strcasecmp(tmp, "noalign") == 0)
127 			sy->align = style_default.align;
128 		else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
129 			if (strcasecmp(tmp + 6, "left") == 0)
130 				sy->align = STYLE_ALIGN_LEFT;
131 			else if (strcasecmp(tmp + 6, "centre") == 0)
132 				sy->align = STYLE_ALIGN_CENTRE;
133 			else if (strcasecmp(tmp + 6, "right") == 0)
134 				sy->align = STYLE_ALIGN_RIGHT;
135 			else
136 				goto error;
137 		} else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
138 			if ((value = colour_fromstring(tmp + 5)) == -1)
139 				goto error;
140 			sy->fill = value;
141 		} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
142 			if ((value = colour_fromstring(tmp + 3)) == -1)
143 				goto error;
144 			if (*in == 'f' || *in == 'F') {
145 				if (value != 8)
146 					sy->gc.fg = value;
147 				else
148 					sy->gc.fg = base->fg;
149 			} else if (*in == 'b' || *in == 'B') {
150 				if (value != 8)
151 					sy->gc.bg = value;
152 				else
153 					sy->gc.bg = base->bg;
154 			} else
155 				goto error;
156 		} else if (strcasecmp(tmp, "none") == 0)
157 			sy->gc.attr = 0;
158 		else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
159 			if ((value = attributes_fromstring(tmp + 2)) == -1)
160 				goto error;
161 			sy->gc.attr &= ~value;
162 		} else {
163 			if ((value = attributes_fromstring(tmp)) == -1)
164 				goto error;
165 			sy->gc.attr |= value;
166 		}
167 
168 		in += end + strspn(in + end, delimiters);
169 	} while (*in != '\0');
170 
171 	return (0);
172 
173 error:
174 	style_copy(sy, &saved);
175 	return (-1);
176 }
177 
178 /* Convert style to a string. */
179 const char *
180 style_tostring(struct style *sy)
181 {
182 	struct grid_cell	*gc = &sy->gc;
183 	int			 off = 0;
184 	const char		*comma = "", *tmp = "";
185 	static char		 s[256];
186 	char			 b[16];
187 
188 	*s = '\0';
189 
190 	if (sy->list != STYLE_LIST_OFF) {
191 		if (sy->list == STYLE_LIST_ON)
192 			tmp = "on";
193 		else if (sy->list == STYLE_LIST_FOCUS)
194 			tmp = "focus";
195 		else if (sy->list == STYLE_LIST_LEFT_MARKER)
196 			tmp = "left-marker";
197 		else if (sy->list == STYLE_LIST_RIGHT_MARKER)
198 			tmp = "right-marker";
199 		off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
200 		    tmp);
201 		comma = ",";
202 	}
203 	if (sy->range_type != STYLE_RANGE_NONE) {
204 		if (sy->range_type == STYLE_RANGE_LEFT)
205 			tmp = "left";
206 		else if (sy->range_type == STYLE_RANGE_RIGHT)
207 			tmp = "right";
208 		else if (sy->range_type == STYLE_RANGE_WINDOW) {
209 			snprintf(b, sizeof b, "window|%u", sy->range_argument);
210 			tmp = b;
211 		}
212 		off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
213 		    tmp);
214 		comma = ",";
215 	}
216 	if (sy->align != STYLE_ALIGN_DEFAULT) {
217 		if (sy->align == STYLE_ALIGN_LEFT)
218 			tmp = "left";
219 		else if (sy->align == STYLE_ALIGN_CENTRE)
220 			tmp = "centre";
221 		else if (sy->align == STYLE_ALIGN_RIGHT)
222 			tmp = "right";
223 		off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
224 		    tmp);
225 		comma = ",";
226 	}
227 	if (sy->default_type != STYLE_DEFAULT_BASE) {
228 		if (sy->default_type == STYLE_DEFAULT_PUSH)
229 			tmp = "push-default";
230 		else if (sy->default_type == STYLE_DEFAULT_POP)
231 			tmp = "pop-default";
232 		off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
233 		comma = ",";
234 	}
235 	if (sy->fill != 8) {
236 		off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
237 		    colour_tostring(sy->fill));
238 		comma = ",";
239 	}
240 	if (gc->fg != 8) {
241 		off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
242 		    colour_tostring(gc->fg));
243 		comma = ",";
244 	}
245 	if (gc->bg != 8) {
246 		off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
247 		    colour_tostring(gc->bg));
248 		comma = ",";
249 	}
250 	if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
251 		xsnprintf(s + off, sizeof s - off, "%s%s", comma,
252 		    attributes_tostring(gc->attr));
253 		comma = ",";
254 	}
255 
256 	if (*s == '\0')
257 		return ("default");
258 	return (s);
259 }
260 
261 /* Apply a style. */
262 void
263 style_apply(struct grid_cell *gc, struct options *oo, const char *name)
264 {
265 	struct style	*sy;
266 
267 	memcpy(gc, &grid_default_cell, sizeof *gc);
268 	sy = options_get_style(oo, name);
269 	gc->fg = sy->gc.fg;
270 	gc->bg = sy->gc.bg;
271 	gc->attr |= sy->gc.attr;
272 }
273 
274 /* Initialize style from cell. */
275 void
276 style_set(struct style *sy, const struct grid_cell *gc)
277 {
278 	memcpy(sy, &style_default, sizeof *sy);
279 	memcpy(&sy->gc, gc, sizeof sy->gc);
280 }
281 
282 /* Copy style. */
283 void
284 style_copy(struct style *dst, struct style *src)
285 {
286 	memcpy(dst, src, sizeof *dst);
287 }
288 
289 /* Check if two styles are (visibly) the same. */
290 int
291 style_equal(struct style *sy1, struct style *sy2)
292 {
293 	struct grid_cell	*gc1 = &sy1->gc;
294 	struct grid_cell	*gc2 = &sy2->gc;
295 
296 	if (gc1->fg != gc2->fg)
297 		return (0);
298 	if (gc1->bg != gc2->bg)
299 		return (0);
300 	if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK))
301 		return (0);
302 	if (sy1->fill != sy2->fill)
303 		return (0);
304 	if (sy1->align != sy2->align)
305 		return (0);
306 	return (1);
307 }
308 
309 /* Is this style default? */
310 int
311 style_is_default(struct style *sy)
312 {
313 	return (style_equal(sy, &style_default));
314 }
315