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