Annotation of src/usr.bin/tmux/style.c, Revision 1.16
1.16 ! nicm 1: /* $OpenBSD: style.c,v 1.15 2019/03/14 09:53:52 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:
22: #include <string.h>
23:
24: #include "tmux.h"
25:
1.15 nicm 26: /* Mask for bits not included in style. */
27: #define STYLE_ATTR_MASK (~GRID_ATTR_CHARSET)
28:
29: /* Default style. */
30: static struct style style_default = {
31: { 0, 0, 8, 8, { { ' ' }, 0, 1, 1 } }
32: };
33:
34: /*
35: * Parse an embedded style of the form "fg=colour,bg=colour,bright,...".
36: * Note that this adds onto the given style, so it must have been initialized
37: * alredy.
38: */
1.1 nicm 39: int
1.15 nicm 40: style_parse(struct style *sy, const struct grid_cell *base, const char *in)
1.1 nicm 41: {
1.16 ! nicm 42: struct style saved;
! 43: const char delimiters[] = " ,";
! 44: char tmp[32];
! 45: int value, fg, bg, attr, flags;
! 46: size_t end;
1.1 nicm 47:
48: if (*in == '\0')
49: return (0);
50: if (strchr(delimiters, in[strlen(in) - 1]) != NULL)
51: return (-1);
1.16 ! nicm 52: style_copy(&saved, sy);
1.1 nicm 53:
1.16 ! nicm 54: fg = sy->gc.fg;
! 55: bg = sy->gc.bg;
! 56: attr = sy->gc.attr;
! 57: flags = sy->gc.flags;
1.15 nicm 58:
1.1 nicm 59: do {
60: end = strcspn(in, delimiters);
61: if (end > (sizeof tmp) - 1)
1.5 nicm 62: goto error;
1.1 nicm 63: memcpy(tmp, in, end);
64: tmp[end] = '\0';
65:
66: if (strcasecmp(tmp, "default") == 0) {
1.15 nicm 67: fg = base->fg;
68: bg = base->bg;
69: attr = base->attr;
70: flags = base->flags;
1.1 nicm 71: } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
1.15 nicm 72: if ((value = colour_fromstring(tmp + 3)) == -1)
1.5 nicm 73: goto error;
1.1 nicm 74: if (*in == 'f' || *in == 'F') {
1.15 nicm 75: if (value != 8)
76: fg = value;
1.10 nicm 77: else
1.15 nicm 78: fg = base->fg;
1.1 nicm 79: } else if (*in == 'b' || *in == 'B') {
1.15 nicm 80: if (value != 8)
81: bg = value;
1.10 nicm 82: else
1.15 nicm 83: bg = base->bg;
1.1 nicm 84: } else
1.5 nicm 85: goto error;
1.1 nicm 86: } else if (strcasecmp(tmp, "none") == 0)
87: attr = 0;
88: else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
1.15 nicm 89: if ((value = attributes_fromstring(tmp + 2)) == -1)
1.5 nicm 90: goto error;
1.15 nicm 91: attr &= ~value;
1.1 nicm 92: } else {
1.15 nicm 93: if ((value = attributes_fromstring(tmp)) == -1)
1.5 nicm 94: goto error;
1.15 nicm 95: attr |= value;
1.1 nicm 96: }
97:
98: in += end + strspn(in + end, delimiters);
99: } while (*in != '\0');
1.15 nicm 100:
1.16 ! nicm 101: sy->gc.fg = fg;
! 102: sy->gc.bg = bg;
! 103: sy->gc.attr = attr;
! 104: sy->gc.flags = flags;
1.1 nicm 105:
106: return (0);
1.5 nicm 107:
108: error:
1.16 ! nicm 109: style_copy(sy, &saved);
1.5 nicm 110: return (-1);
1.1 nicm 111: }
112:
113: /* Convert style to a string. */
114: const char *
1.15 nicm 115: style_tostring(struct style *sy)
1.1 nicm 116: {
1.15 nicm 117: struct grid_cell *gc = &sy->gc;
118: int off = 0;
119: const char *comma = "";
120: static char s[256];
1.1 nicm 121:
122: *s = '\0';
123:
1.10 nicm 124: if (gc->fg != 8) {
1.15 nicm 125: off += xsnprintf(s + off, sizeof s - off, "%sfg=%s",
126: comma, colour_tostring(gc->fg));
127: comma = ",";
1.1 nicm 128: }
1.10 nicm 129: if (gc->bg != 8) {
1.1 nicm 130: off += xsnprintf(s + off, sizeof s - off, "%sbg=%s",
1.15 nicm 131: comma, colour_tostring(gc->bg));
132: comma = ",";
1.1 nicm 133: }
134: if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
135: xsnprintf(s + off, sizeof s - off, "%s%s",
1.15 nicm 136: comma, attributes_tostring(gc->attr));
137: comma = ",";
1.1 nicm 138: }
139:
140: if (*s == '\0')
141: return ("default");
142: return (s);
143: }
144:
145: /* Apply a style. */
146: void
147: style_apply(struct grid_cell *gc, struct options *oo, const char *name)
148: {
1.15 nicm 149: struct style *sy;
1.1 nicm 150:
151: memcpy(gc, &grid_default_cell, sizeof *gc);
1.15 nicm 152: sy = options_get_style(oo, name);
153: gc->fg = sy->gc.fg;
154: gc->bg = sy->gc.bg;
155: gc->attr |= sy->gc.attr;
1.1 nicm 156: }
157:
158: /* Apply a style, updating if default. */
159: void
160: style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
161: {
1.15 nicm 162: struct style *sy;
163:
164: sy = options_get_style(oo, name);
165: if (sy->gc.fg != 8)
166: gc->fg = sy->gc.fg;
167: if (sy->gc.bg != 8)
168: gc->bg = sy->gc.bg;
169: if (sy->gc.attr != 0)
170: gc->attr |= sy->gc.attr;
171: }
172:
173: /* Initialize style from cell. */
174: void
175: style_set(struct style *sy, const struct grid_cell *gc)
176: {
177: memset(sy, 0, sizeof *sy);
178: memcpy(&sy->gc, gc, sizeof sy->gc);
179: }
1.1 nicm 180:
1.15 nicm 181: /* Copy style. */
182: void
183: style_copy(struct style *dst, struct style *src)
184: {
185: memcpy(dst, src, sizeof *dst);
1.8 nicm 186: }
187:
188: /* Check if two styles are the same. */
189: int
1.15 nicm 190: style_equal(struct style *sy1, struct style *sy2)
191: {
192: struct grid_cell *gc1 = &sy1->gc;
193: struct grid_cell *gc2 = &sy2->gc;
194:
195: if (gc1->fg != gc2->fg)
196: return (0);
197: if (gc1->bg != gc2->bg)
198: return (0);
199: if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK))
200: return (0);
201: return (1);
202: }
203:
204: /* Is this style default? */
205: int
206: style_is_default(struct style *sy)
1.8 nicm 207: {
1.15 nicm 208: return (style_equal(sy, &style_default));
1.1 nicm 209: }