Merge branch 'obsd-master'
[tmux.git] / style.c
blobc9e5b15e4bc2fc3f4e278c21e79c0a6c9cfb57cc
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_WIDTH_DEFAULT, STYLE_PAD_DEFAULT,
44 STYLE_DEFAULT_BASE
47 /* Set range string. */
48 static void
49 style_set_range_string(struct style *sy, const char *s)
51 strlcpy(sy->range_string, s, sizeof sy->range_string);
55 * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note
56 * that this adds onto the given style, so it must have been initialized
57 * already.
59 int
60 style_parse(struct style *sy, const struct grid_cell *base, const char *in)
62 struct style saved;
63 const char delimiters[] = " ,\n", *errstr;
64 char tmp[256], *found;
65 int value;
66 size_t end;
67 u_int n;
69 if (*in == '\0')
70 return (0);
71 style_copy(&saved, sy);
73 log_debug("%s: %s", __func__, in);
74 do {
75 while (*in != '\0' && strchr(delimiters, *in) != NULL)
76 in++;
77 if (*in == '\0')
78 break;
80 end = strcspn(in, delimiters);
81 if (end > (sizeof tmp) - 1)
82 goto error;
83 memcpy(tmp, in, end);
84 tmp[end] = '\0';
86 log_debug("%s: %s", __func__, tmp);
87 if (strcasecmp(tmp, "default") == 0) {
88 sy->gc.fg = base->fg;
89 sy->gc.bg = base->bg;
90 sy->gc.us = base->us;
91 sy->gc.attr = base->attr;
92 sy->gc.flags = base->flags;
93 } else if (strcasecmp(tmp, "ignore") == 0)
94 sy->ignore = 1;
95 else if (strcasecmp(tmp, "noignore") == 0)
96 sy->ignore = 0;
97 else if (strcasecmp(tmp, "push-default") == 0)
98 sy->default_type = STYLE_DEFAULT_PUSH;
99 else if (strcasecmp(tmp, "pop-default") == 0)
100 sy->default_type = STYLE_DEFAULT_POP;
101 else if (strcasecmp(tmp, "nolist") == 0)
102 sy->list = STYLE_LIST_OFF;
103 else if (strncasecmp(tmp, "list=", 5) == 0) {
104 if (strcasecmp(tmp + 5, "on") == 0)
105 sy->list = STYLE_LIST_ON;
106 else if (strcasecmp(tmp + 5, "focus") == 0)
107 sy->list = STYLE_LIST_FOCUS;
108 else if (strcasecmp(tmp + 5, "left-marker") == 0)
109 sy->list = STYLE_LIST_LEFT_MARKER;
110 else if (strcasecmp(tmp + 5, "right-marker") == 0)
111 sy->list = STYLE_LIST_RIGHT_MARKER;
112 else
113 goto error;
114 } else if (strcasecmp(tmp, "norange") == 0) {
115 sy->range_type = style_default.range_type;
116 sy->range_argument = style_default.range_type;
117 strlcpy(sy->range_string, style_default.range_string,
118 sizeof sy->range_string);
119 } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
120 found = strchr(tmp + 6, '|');
121 if (found != NULL) {
122 *found++ = '\0';
123 if (*found == '\0')
124 goto error;
126 if (strcasecmp(tmp + 6, "left") == 0) {
127 if (found != NULL)
128 goto error;
129 sy->range_type = STYLE_RANGE_LEFT;
130 sy->range_argument = 0;
131 style_set_range_string(sy, "");
132 } else if (strcasecmp(tmp + 6, "right") == 0) {
133 if (found != NULL)
134 goto error;
135 sy->range_type = STYLE_RANGE_RIGHT;
136 sy->range_argument = 0;
137 style_set_range_string(sy, "");
138 } else if (strcasecmp(tmp + 6, "pane") == 0) {
139 if (found == NULL)
140 goto error;
141 if (*found != '%' || found[1] == '\0')
142 goto error;
143 n = strtonum(found + 1, 0, UINT_MAX, &errstr);
144 if (errstr != NULL)
145 goto error;
146 sy->range_type = STYLE_RANGE_PANE;
147 sy->range_argument = n;
148 style_set_range_string(sy, "");
149 } else if (strcasecmp(tmp + 6, "window") == 0) {
150 if (found == NULL)
151 goto error;
152 n = strtonum(found, 0, UINT_MAX, &errstr);
153 if (errstr != NULL)
154 goto error;
155 sy->range_type = STYLE_RANGE_WINDOW;
156 sy->range_argument = n;
157 style_set_range_string(sy, "");
158 } else if (strcasecmp(tmp + 6, "session") == 0) {
159 if (found == NULL)
160 goto error;
161 if (*found != '$' || found[1] == '\0')
162 goto error;
163 n = strtonum(found + 1, 0, UINT_MAX, &errstr);
164 if (errstr != NULL)
165 goto error;
166 sy->range_type = STYLE_RANGE_SESSION;
167 sy->range_argument = n;
168 style_set_range_string(sy, "");
169 } else if (strcasecmp(tmp + 6, "user") == 0) {
170 if (found == NULL)
171 goto error;
172 sy->range_type = STYLE_RANGE_USER;
173 sy->range_argument = 0;
174 style_set_range_string(sy, found);
176 } else if (strcasecmp(tmp, "noalign") == 0)
177 sy->align = style_default.align;
178 else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
179 if (strcasecmp(tmp + 6, "left") == 0)
180 sy->align = STYLE_ALIGN_LEFT;
181 else if (strcasecmp(tmp + 6, "centre") == 0)
182 sy->align = STYLE_ALIGN_CENTRE;
183 else if (strcasecmp(tmp + 6, "right") == 0)
184 sy->align = STYLE_ALIGN_RIGHT;
185 else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
186 sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
187 else
188 goto error;
189 } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
190 if ((value = colour_fromstring(tmp + 5)) == -1)
191 goto error;
192 sy->fill = value;
193 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
194 if ((value = colour_fromstring(tmp + 3)) == -1)
195 goto error;
196 if (*in == 'f' || *in == 'F') {
197 if (value != 8)
198 sy->gc.fg = value;
199 else
200 sy->gc.fg = base->fg;
201 } else if (*in == 'b' || *in == 'B') {
202 if (value != 8)
203 sy->gc.bg = value;
204 else
205 sy->gc.bg = base->bg;
206 } else
207 goto error;
208 } else if (end > 3 && strncasecmp(tmp, "us=", 3) == 0) {
209 if ((value = colour_fromstring(tmp + 3)) == -1)
210 goto error;
211 if (value != 8)
212 sy->gc.us = value;
213 else
214 sy->gc.us = base->us;
215 } else if (strcasecmp(tmp, "none") == 0)
216 sy->gc.attr = 0;
217 else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
218 if ((value = attributes_fromstring(tmp + 2)) == -1)
219 goto error;
220 sy->gc.attr &= ~value;
221 } else if (end > 6 && strncasecmp(tmp, "width=", 6) == 0) {
222 n = strtonum(tmp + 6, 0, UINT_MAX, &errstr);
223 if (errstr != NULL)
224 goto error;
225 sy->width = (int)n;
226 } else if (end > 4 && strncasecmp(tmp, "pad=", 4) == 0) {
227 n = strtonum(tmp + 4, 0, UINT_MAX, &errstr);
228 if (errstr != NULL)
229 goto error;
230 sy->pad = (int)n;
231 } else {
232 if ((value = attributes_fromstring(tmp)) == -1)
233 goto error;
234 sy->gc.attr |= value;
237 in += end + strspn(in + end, delimiters);
238 } while (*in != '\0');
240 return (0);
242 error:
243 style_copy(sy, &saved);
244 return (-1);
247 /* Convert style to a string. */
248 const char *
249 style_tostring(struct style *sy)
251 struct grid_cell *gc = &sy->gc;
252 int off = 0;
253 const char *comma = "", *tmp = "";
254 static char s[256];
255 char b[21];
257 *s = '\0';
259 if (sy->list != STYLE_LIST_OFF) {
260 if (sy->list == STYLE_LIST_ON)
261 tmp = "on";
262 else if (sy->list == STYLE_LIST_FOCUS)
263 tmp = "focus";
264 else if (sy->list == STYLE_LIST_LEFT_MARKER)
265 tmp = "left-marker";
266 else if (sy->list == STYLE_LIST_RIGHT_MARKER)
267 tmp = "right-marker";
268 off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
269 tmp);
270 comma = ",";
272 if (sy->range_type != STYLE_RANGE_NONE) {
273 if (sy->range_type == STYLE_RANGE_LEFT)
274 tmp = "left";
275 else if (sy->range_type == STYLE_RANGE_RIGHT)
276 tmp = "right";
277 else if (sy->range_type == STYLE_RANGE_PANE) {
278 snprintf(b, sizeof b, "pane|%%%u", sy->range_argument);
279 tmp = b;
280 } else if (sy->range_type == STYLE_RANGE_WINDOW) {
281 snprintf(b, sizeof b, "window|%u", sy->range_argument);
282 tmp = b;
283 } else if (sy->range_type == STYLE_RANGE_SESSION) {
284 snprintf(b, sizeof b, "session|$%u",
285 sy->range_argument);
286 tmp = b;
287 } else if (sy->range_type == STYLE_RANGE_USER) {
288 snprintf(b, sizeof b, "user|%s", sy->range_string);
289 tmp = b;
291 off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
292 tmp);
293 comma = ",";
295 if (sy->align != STYLE_ALIGN_DEFAULT) {
296 if (sy->align == STYLE_ALIGN_LEFT)
297 tmp = "left";
298 else if (sy->align == STYLE_ALIGN_CENTRE)
299 tmp = "centre";
300 else if (sy->align == STYLE_ALIGN_RIGHT)
301 tmp = "right";
302 else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
303 tmp = "absolute-centre";
304 off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
305 tmp);
306 comma = ",";
308 if (sy->default_type != STYLE_DEFAULT_BASE) {
309 if (sy->default_type == STYLE_DEFAULT_PUSH)
310 tmp = "push-default";
311 else if (sy->default_type == STYLE_DEFAULT_POP)
312 tmp = "pop-default";
313 off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
314 comma = ",";
316 if (sy->fill != 8) {
317 off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
318 colour_tostring(sy->fill));
319 comma = ",";
321 if (gc->fg != 8) {
322 off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
323 colour_tostring(gc->fg));
324 comma = ",";
326 if (gc->bg != 8) {
327 off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
328 colour_tostring(gc->bg));
329 comma = ",";
331 if (gc->us != 8) {
332 off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma,
333 colour_tostring(gc->us));
334 comma = ",";
336 if (gc->attr != 0) {
337 xsnprintf(s + off, sizeof s - off, "%s%s", comma,
338 attributes_tostring(gc->attr));
339 comma = ",";
341 if (sy->width >= 0) {
342 xsnprintf(s + off, sizeof s - off, "%swidth=%u", comma,
343 sy->width);
344 comma = ",";
346 if (sy->pad >= 0) {
347 xsnprintf(s + off, sizeof s - off, "%spad=%u", comma,
348 sy->pad);
349 comma = ",";
351 if (*s == '\0')
352 return ("default");
353 return (s);
356 /* Apply a style on top of the given style. */
357 void
358 style_add(struct grid_cell *gc, struct options *oo, const char *name,
359 struct format_tree *ft)
361 struct style *sy;
362 struct format_tree *ft0 = NULL;
364 if (ft == NULL)
365 ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
367 sy = options_string_to_style(oo, name, ft);
368 if (sy == NULL)
369 sy = &style_default;
370 if (sy->gc.fg != 8)
371 gc->fg = sy->gc.fg;
372 if (sy->gc.bg != 8)
373 gc->bg = sy->gc.bg;
374 if (sy->gc.us != 8)
375 gc->us = sy->gc.us;
376 gc->attr |= sy->gc.attr;
378 if (ft0 != NULL)
379 format_free(ft0);
382 /* Apply a style on top of the default style. */
383 void
384 style_apply(struct grid_cell *gc, struct options *oo, const char *name,
385 struct format_tree *ft)
387 memcpy(gc, &grid_default_cell, sizeof *gc);
388 style_add(gc, oo, name, ft);
391 /* Initialize style from cell. */
392 void
393 style_set(struct style *sy, const struct grid_cell *gc)
395 memcpy(sy, &style_default, sizeof *sy);
396 memcpy(&sy->gc, gc, sizeof sy->gc);
399 /* Copy style. */
400 void
401 style_copy(struct style *dst, struct style *src)
403 memcpy(dst, src, sizeof *dst);
406 void
407 style_set_scrollbar_style_from_option(struct style *sb_style, struct options *oo)
409 struct style *sy;
411 sy = options_string_to_style(oo, "pane-scrollbars-style", NULL);
412 if (sy == NULL) {
413 style_set(sb_style, &grid_default_cell);
414 sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
415 sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
416 utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);
417 } else {
418 style_copy(sb_style, sy);
419 if (sb_style->width < 1)
420 sb_style->width = PANE_SCROLLBARS_DEFAULT_WIDTH;
421 if (sb_style->pad < 0)
422 sb_style->pad = PANE_SCROLLBARS_DEFAULT_PADDING;
423 utf8_set(&sb_style->gc.data, PANE_SCROLLBARS_CHARACTER);