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