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