Merge branch 'obsd-master'
[tmux.git] / style.c
blob9d7c585df7d91f117b18b674bd70c4f578527e59
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5 * Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net>
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.
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.
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "tmux.h"
28 /* Mask for bits not included in style. */
29 #define STYLE_ATTR_MASK (~0)
31 /* Default style. */
32 static struct style style_default = {
33 { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0, 0 },
37 STYLE_ALIGN_DEFAULT,
38 STYLE_LIST_OFF,
40 STYLE_RANGE_NONE, 0, "",
42 STYLE_DEFAULT_BASE
45 /* Set range string. */
46 static void
47 style_set_range_string(struct style *sy, const char *s)
49 strlcpy(sy->range_string, s, sizeof sy->range_string);
53 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note
54 * that this adds onto the given style, so it must have been initialized
55 * already.
57 int
58 style_parse(struct style *sy, const struct grid_cell *base, const char *in)
60 struct style saved;
61 const char delimiters[] = " ,\n", *errstr;
62 char tmp[256], *found;
63 int value;
64 size_t end;
65 u_int n;
67 if (*in == '\0')
68 return (0);
69 style_copy(&saved, sy);
71 log_debug("%s: %s", __func__, in);
72 do {
73 while (*in != '\0' && strchr(delimiters, *in) != NULL)
74 in++;
75 if (*in == '\0')
76 break;
78 end = strcspn(in, delimiters);
79 if (end > (sizeof tmp) - 1)
80 goto error;
81 memcpy(tmp, in, end);
82 tmp[end] = '\0';
84 log_debug("%s: %s", __func__, tmp);
85 if (strcasecmp(tmp, "default") == 0) {
86 sy->gc.fg = base->fg;
87 sy->gc.bg = base->bg;
88 sy->gc.us = base->us;
89 sy->gc.attr = base->attr;
90 sy->gc.flags = base->flags;
91 } else if (strcasecmp(tmp, "ignore") == 0)
92 sy->ignore = 1;
93 else if (strcasecmp(tmp, "noignore") == 0)
94 sy->ignore = 0;
95 else if (strcasecmp(tmp, "push-default") == 0)
96 sy->default_type = STYLE_DEFAULT_PUSH;
97 else if (strcasecmp(tmp, "pop-default") == 0)
98 sy->default_type = STYLE_DEFAULT_POP;
99 else if (strcasecmp(tmp, "nolist") == 0)
100 sy->list = STYLE_LIST_OFF;
101 else if (strncasecmp(tmp, "list=", 5) == 0) {
102 if (strcasecmp(tmp + 5, "on") == 0)
103 sy->list = STYLE_LIST_ON;
104 else if (strcasecmp(tmp + 5, "focus") == 0)
105 sy->list = STYLE_LIST_FOCUS;
106 else if (strcasecmp(tmp + 5, "left-marker") == 0)
107 sy->list = STYLE_LIST_LEFT_MARKER;
108 else if (strcasecmp(tmp + 5, "right-marker") == 0)
109 sy->list = STYLE_LIST_RIGHT_MARKER;
110 else
111 goto error;
112 } else if (strcasecmp(tmp, "norange") == 0) {
113 sy->range_type = style_default.range_type;
114 sy->range_argument = style_default.range_type;
115 strlcpy(sy->range_string, style_default.range_string,
116 sizeof sy->range_string);
117 } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
118 found = strchr(tmp + 6, '|');
119 if (found != NULL) {
120 *found++ = '\0';
121 if (*found == '\0')
122 goto error;
124 if (strcasecmp(tmp + 6, "left") == 0) {
125 if (found != NULL)
126 goto error;
127 sy->range_type = STYLE_RANGE_LEFT;
128 sy->range_argument = 0;
129 style_set_range_string(sy, "");
130 } else if (strcasecmp(tmp + 6, "right") == 0) {
131 if (found != NULL)
132 goto error;
133 sy->range_type = STYLE_RANGE_RIGHT;
134 sy->range_argument = 0;
135 style_set_range_string(sy, "");
136 } else if (strcasecmp(tmp + 6, "pane") == 0) {
137 if (found == NULL)
138 goto error;
139 if (*found != '%' || found[1] == '\0')
140 goto error;
141 n = strtonum(found + 1, 0, UINT_MAX, &errstr);
142 if (errstr != NULL)
143 goto error;
144 sy->range_type = STYLE_RANGE_PANE;
145 sy->range_argument = n;
146 style_set_range_string(sy, "");
147 } else if (strcasecmp(tmp + 6, "window") == 0) {
148 if (found == NULL)
149 goto error;
150 n = strtonum(found, 0, UINT_MAX, &errstr);
151 if (errstr != NULL)
152 goto error;
153 sy->range_type = STYLE_RANGE_WINDOW;
154 sy->range_argument = n;
155 style_set_range_string(sy, "");
156 } else if (strcasecmp(tmp + 6, "session") == 0) {
157 if (found == NULL)
158 goto error;
159 if (*found != '$' || found[1] == '\0')
160 goto error;
161 n = strtonum(found + 1, 0, UINT_MAX, &errstr);
162 if (errstr != NULL)
163 goto error;
164 sy->range_type = STYLE_RANGE_SESSION;
165 sy->range_argument = n;
166 style_set_range_string(sy, "");
167 } else if (strcasecmp(tmp + 6, "user") == 0) {
168 if (found == NULL)
169 goto error;
170 sy->range_type = STYLE_RANGE_USER;
171 sy->range_argument = 0;
172 style_set_range_string(sy, found);
174 } else if (strcasecmp(tmp, "noalign") == 0)
175 sy->align = style_default.align;
176 else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
177 if (strcasecmp(tmp + 6, "left") == 0)
178 sy->align = STYLE_ALIGN_LEFT;
179 else if (strcasecmp(tmp + 6, "centre") == 0)
180 sy->align = STYLE_ALIGN_CENTRE;
181 else if (strcasecmp(tmp + 6, "right") == 0)
182 sy->align = STYLE_ALIGN_RIGHT;
183 else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
184 sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
185 else
186 goto error;
187 } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
188 if ((value = colour_fromstring(tmp + 5)) == -1)
189 goto error;
190 sy->fill = value;
191 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
192 if ((value = colour_fromstring(tmp + 3)) == -1)
193 goto error;
194 if (*in == 'f' || *in == 'F') {
195 if (value != 8)
196 sy->gc.fg = value;
197 else
198 sy->gc.fg = base->fg;
199 } else if (*in == 'b' || *in == 'B') {
200 if (value != 8)
201 sy->gc.bg = value;
202 else
203 sy->gc.bg = base->bg;
204 } else
205 goto error;
206 } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) {
207 if ((value = colour_fromstring(tmp + 3)) == -1)
208 goto error;
209 if (value != 8)
210 sy->gc.us = value;
211 else
212 sy->gc.us = base->us;
213 } else if (strcasecmp(tmp, "none") == 0)
214 sy->gc.attr = 0;
215 else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
216 if ((value = attributes_fromstring(tmp + 2)) == -1)
217 goto error;
218 sy->gc.attr &= ~value;
219 } else {
220 if ((value = attributes_fromstring(tmp)) == -1)
221 goto error;
222 sy->gc.attr |= value;
225 in += end + strspn(in + end, delimiters);
226 } while (*in != '\0');
228 return (0);
230 error:
231 style_copy(sy, &saved);
232 return (-1);
235 /* Convert style to a string. */
236 const char *
237 style_tostring(struct style *sy)
239 struct grid_cell *gc = &sy->gc;
240 int off = 0;
241 const char *comma = "", *tmp = "";
242 static char s[256];
243 char b[21];
245 *s = '\0';
247 if (sy->list != STYLE_LIST_OFF) {
248 if (sy->list == STYLE_LIST_ON)
249 tmp = "on";
250 else if (sy->list == STYLE_LIST_FOCUS)
251 tmp = "focus";
252 else if (sy->list == STYLE_LIST_LEFT_MARKER)
253 tmp = "left-marker";
254 else if (sy->list == STYLE_LIST_RIGHT_MARKER)
255 tmp = "right-marker";
256 off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
257 tmp);
258 comma = ",";
260 if (sy->range_type != STYLE_RANGE_NONE) {
261 if (sy->range_type == STYLE_RANGE_LEFT)
262 tmp = "left";
263 else if (sy->range_type == STYLE_RANGE_RIGHT)
264 tmp = "right";
265 else if (sy->range_type == STYLE_RANGE_PANE) {
266 snprintf(b, sizeof b, "pane|%%%u", sy->range_argument);
267 tmp = b;
268 } else if (sy->range_type == STYLE_RANGE_WINDOW) {
269 snprintf(b, sizeof b, "window|%u", sy->range_argument);
270 tmp = b;
271 } else if (sy->range_type == STYLE_RANGE_SESSION) {
272 snprintf(b, sizeof b, "session|$%u",
273 sy->range_argument);
274 tmp = b;
275 } else if (sy->range_type == STYLE_RANGE_USER) {
276 snprintf(b, sizeof b, "user|%s", sy->range_string);
277 tmp = b;
279 off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
280 tmp);
281 comma = ",";
283 if (sy->align != STYLE_ALIGN_DEFAULT) {
284 if (sy->align == STYLE_ALIGN_LEFT)
285 tmp = "left";
286 else if (sy->align == STYLE_ALIGN_CENTRE)
287 tmp = "centre";
288 else if (sy->align == STYLE_ALIGN_RIGHT)
289 tmp = "right";
290 else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
291 tmp = "absolute-centre";
292 off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
293 tmp);
294 comma = ",";
296 if (sy->default_type != STYLE_DEFAULT_BASE) {
297 if (sy->default_type == STYLE_DEFAULT_PUSH)
298 tmp = "push-default";
299 else if (sy->default_type == STYLE_DEFAULT_POP)
300 tmp = "pop-default";
301 off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
302 comma = ",";
304 if (sy->fill != 8) {
305 off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
306 colour_tostring(sy->fill));
307 comma = ",";
309 if (gc->fg != 8) {
310 off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
311 colour_tostring(gc->fg));
312 comma = ",";
314 if (gc->bg != 8) {
315 off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
316 colour_tostring(gc->bg));
317 comma = ",";
319 if (gc->us != 8) {
320 off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma,
321 colour_tostring(gc->us));
322 comma = ",";
324 if (gc->attr != 0) {
325 xsnprintf(s + off, sizeof s - off, "%s%s", comma,
326 attributes_tostring(gc->attr));
327 comma = ",";
330 if (*s == '\0')
331 return ("default");
332 return (s);
335 /* Apply a style on top of the given style. */
336 void
337 style_add(struct grid_cell *gc, struct options *oo, const char *name,
338 struct format_tree *ft)
340 struct style *sy;
341 struct format_tree *ft0 = NULL;
343 if (ft == NULL)
344 ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
346 sy = options_string_to_style(oo, name, ft);
347 if (sy == NULL)
348 sy = &style_default;
349 if (sy->gc.fg != 8)
350 gc->fg = sy->gc.fg;
351 if (sy->gc.bg != 8)
352 gc->bg = sy->gc.bg;
353 if (sy->gc.us != 8)
354 gc->us = sy->gc.us;
355 gc->attr |= sy->gc.attr;
357 if (ft0 != NULL)
358 format_free(ft0);
361 /* Apply a style on top of the default style. */
362 void
363 style_apply(struct grid_cell *gc, struct options *oo, const char *name,
364 struct format_tree *ft)
366 memcpy(gc, &grid_default_cell, sizeof *gc);
367 style_add(gc, oo, name, ft);
370 /* Initialize style from cell. */
371 void
372 style_set(struct style *sy, const struct grid_cell *gc)
374 memcpy(sy, &style_default, sizeof *sy);
375 memcpy(&sy->gc, gc, sizeof sy->gc);
378 /* Copy style. */
379 void
380 style_copy(struct style *dst, struct style *src)
382 memcpy(dst, src, sizeof *dst);