Annotation of src/usr.bin/tmux/style.c, Revision 1.20
1.20 ! nicm 1: /* $OpenBSD: style.c,v 1.19 2019/04/05 20:32:31 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 {
1.20 ! nicm 60: while (*in != '\0' && strchr(delimiters, *in) != NULL)
1.17 nicm 61: in++;
62: if (*in == '\0')
63: break;
1.15 nicm 64:
1.1 nicm 65: end = strcspn(in, delimiters);
66: if (end > (sizeof tmp) - 1)
1.5 nicm 67: goto error;
1.1 nicm 68: memcpy(tmp, in, end);
69: tmp[end] = '\0';
70:
71: if (strcasecmp(tmp, "default") == 0) {
1.17 nicm 72: sy->gc.fg = base->fg;
73: sy->gc.bg = base->bg;
74: sy->gc.attr = base->attr;
75: sy->gc.flags = base->flags;
1.18 nicm 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;
1.1 nicm 130: } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
1.15 nicm 131: if ((value = colour_fromstring(tmp + 3)) == -1)
1.5 nicm 132: goto error;
1.1 nicm 133: if (*in == 'f' || *in == 'F') {
1.15 nicm 134: if (value != 8)
1.17 nicm 135: sy->gc.fg = value;
1.10 nicm 136: else
1.17 nicm 137: sy->gc.fg = base->fg;
1.1 nicm 138: } else if (*in == 'b' || *in == 'B') {
1.15 nicm 139: if (value != 8)
1.17 nicm 140: sy->gc.bg = value;
1.10 nicm 141: else
1.17 nicm 142: sy->gc.bg = base->bg;
1.1 nicm 143: } else
1.5 nicm 144: goto error;
1.1 nicm 145: } else if (strcasecmp(tmp, "none") == 0)
1.17 nicm 146: sy->gc.attr = 0;
1.1 nicm 147: else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
1.15 nicm 148: if ((value = attributes_fromstring(tmp + 2)) == -1)
1.5 nicm 149: goto error;
1.17 nicm 150: sy->gc.attr &= ~value;
1.1 nicm 151: } else {
1.15 nicm 152: if ((value = attributes_fromstring(tmp)) == -1)
1.5 nicm 153: goto error;
1.17 nicm 154: sy->gc.attr |= value;
1.1 nicm 155: }
156:
157: in += end + strspn(in + end, delimiters);
158: } while (*in != '\0');
1.15 nicm 159:
1.1 nicm 160: return (0);
1.5 nicm 161:
162: error:
1.16 nicm 163: style_copy(sy, &saved);
1.5 nicm 164: return (-1);
1.1 nicm 165: }
166:
167: /* Convert style to a string. */
168: const char *
1.15 nicm 169: style_tostring(struct style *sy)
1.1 nicm 170: {
1.15 nicm 171: struct grid_cell *gc = &sy->gc;
172: int off = 0;
1.19 nicm 173: const char *comma = "", *tmp = "";
1.15 nicm 174: static char s[256];
1.18 nicm 175: char b[16];
1.1 nicm 176:
177: *s = '\0';
178:
1.18 nicm 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: }
1.10 nicm 216: if (gc->fg != 8) {
1.17 nicm 217: off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
218: colour_tostring(gc->fg));
1.15 nicm 219: comma = ",";
1.1 nicm 220: }
1.10 nicm 221: if (gc->bg != 8) {
1.17 nicm 222: off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
223: colour_tostring(gc->bg));
1.15 nicm 224: comma = ",";
1.1 nicm 225: }
226: if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
1.17 nicm 227: xsnprintf(s + off, sizeof s - off, "%s%s", comma,
228: attributes_tostring(gc->attr));
1.15 nicm 229: comma = ",";
1.1 nicm 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: {
1.15 nicm 241: struct style *sy;
1.1 nicm 242:
243: memcpy(gc, &grid_default_cell, sizeof *gc);
1.15 nicm 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;
1.1 nicm 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: {
1.15 nicm 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: {
1.17 nicm 269: memcpy(sy, &style_default, sizeof *sy);
1.15 nicm 270: memcpy(&sy->gc, gc, sizeof sy->gc);
271: }
1.1 nicm 272:
1.15 nicm 273: /* Copy style. */
274: void
275: style_copy(struct style *dst, struct style *src)
276: {
277: memcpy(dst, src, sizeof *dst);
1.8 nicm 278: }
279:
1.18 nicm 280: /* Check if two styles are (visibly) the same. */
1.8 nicm 281: int
1.15 nicm 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))
1.18 nicm 292: return (0);
293: if (sy1->align != sy2->align)
1.15 nicm 294: return (0);
295: return (1);
296: }
297:
298: /* Is this style default? */
299: int
300: style_is_default(struct style *sy)
1.8 nicm 301: {
1.15 nicm 302: return (style_equal(sy, &style_default));
1.1 nicm 303: }