Annotation of src/usr.bin/tmux/style.c, Revision 1.33
1.33 ! nicm 1: /* $OpenBSD: style.c,v 1.32 2023/06/26 07:17:40 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.33 ! nicm 40: STYLE_RANGE_NONE, 0, "",
1.24 nicm 41:
42: STYLE_DEFAULT_BASE
1.15 nicm 43: };
44:
1.33 ! nicm 45: /* Set range string. */
! 46: static void
! 47: style_set_range_string(struct style *sy, const char *s)
! 48: {
! 49: strlcpy(sy->range_string, s, sizeof sy->range_string);
! 50: }
! 51:
1.15 nicm 52: /*
1.24 nicm 53: * Parse an embedded style of the form "fg=colour,bg=colour,bright,...". Note
54: * that this adds onto the given style, so it must have been initialized
55: * already.
1.15 nicm 56: */
1.1 nicm 57: int
1.15 nicm 58: style_parse(struct style *sy, const struct grid_cell *base, const char *in)
1.1 nicm 59: {
1.17 nicm 60: struct style saved;
1.30 nicm 61: const char delimiters[] = " ,\n", *cp;
1.18 nicm 62: char tmp[256], *found;
1.17 nicm 63: int value;
64: size_t end;
1.1 nicm 65:
66: if (*in == '\0')
67: return (0);
1.16 nicm 68: style_copy(&saved, sy);
1.1 nicm 69:
1.26 nicm 70: log_debug("%s: %s", __func__, in);
1.17 nicm 71: do {
1.20 nicm 72: while (*in != '\0' && strchr(delimiters, *in) != NULL)
1.17 nicm 73: in++;
74: if (*in == '\0')
75: break;
1.15 nicm 76:
1.1 nicm 77: end = strcspn(in, delimiters);
78: if (end > (sizeof tmp) - 1)
1.5 nicm 79: goto error;
1.1 nicm 80: memcpy(tmp, in, end);
81: tmp[end] = '\0';
82:
1.26 nicm 83: log_debug("%s: %s", __func__, tmp);
1.1 nicm 84: if (strcasecmp(tmp, "default") == 0) {
1.17 nicm 85: sy->gc.fg = base->fg;
86: sy->gc.bg = base->bg;
1.32 nicm 87: sy->gc.us = base->us;
1.17 nicm 88: sy->gc.attr = base->attr;
89: sy->gc.flags = base->flags;
1.28 nicm 90: } else if (strcasecmp(tmp, "ignore") == 0)
91: sy->ignore = 1;
92: else if (strcasecmp(tmp, "noignore") == 0)
93: sy->ignore = 0;
94: else if (strcasecmp(tmp, "push-default") == 0)
1.24 nicm 95: sy->default_type = STYLE_DEFAULT_PUSH;
96: else if (strcasecmp(tmp, "pop-default") == 0)
97: sy->default_type = STYLE_DEFAULT_POP;
98: else if (strcasecmp(tmp, "nolist") == 0)
1.18 nicm 99: sy->list = STYLE_LIST_OFF;
100: else if (strncasecmp(tmp, "list=", 5) == 0) {
101: if (strcasecmp(tmp + 5, "on") == 0)
102: sy->list = STYLE_LIST_ON;
103: else if (strcasecmp(tmp + 5, "focus") == 0)
104: sy->list = STYLE_LIST_FOCUS;
105: else if (strcasecmp(tmp + 5, "left-marker") == 0)
106: sy->list = STYLE_LIST_LEFT_MARKER;
107: else if (strcasecmp(tmp + 5, "right-marker") == 0)
108: sy->list = STYLE_LIST_RIGHT_MARKER;
109: else
110: goto error;
111: } else if (strcasecmp(tmp, "norange") == 0) {
112: sy->range_type = style_default.range_type;
113: sy->range_argument = style_default.range_type;
1.33 ! nicm 114: strlcpy(sy->range_string, style_default.range_string,
! 115: sizeof sy->range_string);
1.18 nicm 116: } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
117: found = strchr(tmp + 6, '|');
118: if (found != NULL) {
119: *found++ = '\0';
120: if (*found == '\0')
121: goto error;
122: }
123: if (strcasecmp(tmp + 6, "left") == 0) {
124: if (found != NULL)
125: goto error;
126: sy->range_type = STYLE_RANGE_LEFT;
127: sy->range_argument = 0;
1.33 ! nicm 128: style_set_range_string(sy, "");
1.18 nicm 129: } else if (strcasecmp(tmp + 6, "right") == 0) {
130: if (found != NULL)
131: goto error;
132: sy->range_type = STYLE_RANGE_RIGHT;
133: sy->range_argument = 0;
1.33 ! nicm 134: style_set_range_string(sy, "");
! 135: } else if (strcasecmp(tmp + 6, "pane") == 0) {
! 136: if (found == NULL)
! 137: goto error;
! 138: if (*found != '%' || found[1] == '\0')
! 139: goto error;
! 140: for (cp = found + 1; *cp != '\0'; cp++) {
! 141: if (!isdigit((u_char)*cp))
! 142: goto error;
! 143: }
! 144: sy->range_type = STYLE_RANGE_PANE;
! 145: sy->range_argument = atoi(found + 1);
! 146: style_set_range_string(sy, "");
1.18 nicm 147: } else if (strcasecmp(tmp + 6, "window") == 0) {
148: if (found == NULL)
149: goto error;
1.33 ! nicm 150: for (cp = found; *cp != '\0'; cp++) {
! 151: if (!isdigit((u_char)*cp))
! 152: goto error;
! 153: }
1.18 nicm 154: sy->range_type = STYLE_RANGE_WINDOW;
155: sy->range_argument = atoi(found);
1.33 ! nicm 156: style_set_range_string(sy, "");
! 157: } else if (strcasecmp(tmp + 6, "session") == 0) {
! 158: if (found == NULL)
! 159: goto error;
! 160: if (*found != '$' || found[1] == '\0')
! 161: goto error;
! 162: for (cp = found + 1; *cp != '\0'; cp++) {
! 163: if (!isdigit((u_char)*cp))
! 164: goto error;
! 165: }
! 166: sy->range_type = STYLE_RANGE_SESSION;
! 167: sy->range_argument = atoi(found + 1);
! 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);
1.18 nicm 175: }
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;
1.29 nicm 185: else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
186: sy->align = STYLE_ALIGN_ABSOLUTE_CENTRE;
1.18 nicm 187: else
188: goto error;
1.22 nicm 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;
1.1 nicm 193: } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
1.15 nicm 194: if ((value = colour_fromstring(tmp + 3)) == -1)
1.5 nicm 195: goto error;
1.1 nicm 196: if (*in == 'f' || *in == 'F') {
1.15 nicm 197: if (value != 8)
1.17 nicm 198: sy->gc.fg = value;
1.10 nicm 199: else
1.17 nicm 200: sy->gc.fg = base->fg;
1.1 nicm 201: } else if (*in == 'b' || *in == 'B') {
1.15 nicm 202: if (value != 8)
1.17 nicm 203: sy->gc.bg = value;
1.10 nicm 204: else
1.17 nicm 205: sy->gc.bg = base->bg;
1.1 nicm 206: } else
1.5 nicm 207: goto error;
1.32 nicm 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;
1.1 nicm 215: } else if (strcasecmp(tmp, "none") == 0)
1.17 nicm 216: sy->gc.attr = 0;
1.1 nicm 217: else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
1.15 nicm 218: if ((value = attributes_fromstring(tmp + 2)) == -1)
1.5 nicm 219: goto error;
1.17 nicm 220: sy->gc.attr &= ~value;
1.1 nicm 221: } else {
1.15 nicm 222: if ((value = attributes_fromstring(tmp)) == -1)
1.5 nicm 223: goto error;
1.17 nicm 224: sy->gc.attr |= value;
1.1 nicm 225: }
226:
227: in += end + strspn(in + end, delimiters);
228: } while (*in != '\0');
1.15 nicm 229:
1.1 nicm 230: return (0);
1.5 nicm 231:
232: error:
1.16 nicm 233: style_copy(sy, &saved);
1.5 nicm 234: return (-1);
1.1 nicm 235: }
236:
237: /* Convert style to a string. */
238: const char *
1.15 nicm 239: style_tostring(struct style *sy)
1.1 nicm 240: {
1.15 nicm 241: struct grid_cell *gc = &sy->gc;
242: int off = 0;
1.19 nicm 243: const char *comma = "", *tmp = "";
1.15 nicm 244: static char s[256];
1.18 nicm 245: char b[16];
1.1 nicm 246:
247: *s = '\0';
248:
1.18 nicm 249: if (sy->list != STYLE_LIST_OFF) {
250: if (sy->list == STYLE_LIST_ON)
251: tmp = "on";
252: else if (sy->list == STYLE_LIST_FOCUS)
253: tmp = "focus";
254: else if (sy->list == STYLE_LIST_LEFT_MARKER)
255: tmp = "left-marker";
256: else if (sy->list == STYLE_LIST_RIGHT_MARKER)
257: tmp = "right-marker";
258: off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
259: tmp);
260: comma = ",";
261: }
262: if (sy->range_type != STYLE_RANGE_NONE) {
263: if (sy->range_type == STYLE_RANGE_LEFT)
264: tmp = "left";
265: else if (sy->range_type == STYLE_RANGE_RIGHT)
266: tmp = "right";
1.33 ! nicm 267: else if (sy->range_type == STYLE_RANGE_PANE) {
! 268: snprintf(b, sizeof b, "pane|%%%u", sy->range_argument);
! 269: tmp = b;
! 270: } else if (sy->range_type == STYLE_RANGE_WINDOW) {
1.18 nicm 271: snprintf(b, sizeof b, "window|%u", sy->range_argument);
1.33 ! nicm 272: tmp = b;
! 273: } else if (sy->range_type == STYLE_RANGE_SESSION) {
! 274: snprintf(b, sizeof b, "session|$%u",
! 275: sy->range_argument);
! 276: tmp = b;
! 277: } else if (sy->range_type == STYLE_RANGE_USER) {
! 278: snprintf(b, sizeof b, "user|%s", sy->range_string);
1.18 nicm 279: tmp = b;
280: }
281: off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
282: tmp);
283: comma = ",";
284: }
285: if (sy->align != STYLE_ALIGN_DEFAULT) {
286: if (sy->align == STYLE_ALIGN_LEFT)
287: tmp = "left";
288: else if (sy->align == STYLE_ALIGN_CENTRE)
289: tmp = "centre";
290: else if (sy->align == STYLE_ALIGN_RIGHT)
291: tmp = "right";
1.29 nicm 292: else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
293: tmp = "absolute-centre";
1.18 nicm 294: off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
295: tmp);
296: comma = ",";
297: }
1.24 nicm 298: if (sy->default_type != STYLE_DEFAULT_BASE) {
299: if (sy->default_type == STYLE_DEFAULT_PUSH)
300: tmp = "push-default";
301: else if (sy->default_type == STYLE_DEFAULT_POP)
302: tmp = "pop-default";
303: off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
304: comma = ",";
305: }
1.22 nicm 306: if (sy->fill != 8) {
307: off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
308: colour_tostring(sy->fill));
309: comma = ",";
310: }
1.10 nicm 311: if (gc->fg != 8) {
1.17 nicm 312: off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
313: colour_tostring(gc->fg));
1.15 nicm 314: comma = ",";
1.1 nicm 315: }
1.10 nicm 316: if (gc->bg != 8) {
1.17 nicm 317: off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
318: colour_tostring(gc->bg));
1.15 nicm 319: comma = ",";
1.1 nicm 320: }
1.32 nicm 321: if (gc->us != 8) {
322: off += xsnprintf(s + off, sizeof s - off, "%sus=%s", comma,
323: colour_tostring(gc->us));
324: comma = ",";
325: }
1.25 nicm 326: if (gc->attr != 0) {
1.17 nicm 327: xsnprintf(s + off, sizeof s - off, "%s%s", comma,
328: attributes_tostring(gc->attr));
1.15 nicm 329: comma = ",";
1.1 nicm 330: }
331:
332: if (*s == '\0')
333: return ("default");
334: return (s);
335: }
336:
1.27 nicm 337: /* Apply a style on top of the given style. */
1.1 nicm 338: void
1.27 nicm 339: style_add(struct grid_cell *gc, struct options *oo, const char *name,
340: struct format_tree *ft)
1.1 nicm 341: {
1.27 nicm 342: struct style *sy;
343: struct format_tree *ft0 = NULL;
1.1 nicm 344:
1.27 nicm 345: if (ft == NULL)
346: ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
347:
348: sy = options_string_to_style(oo, name, ft);
349: if (sy == NULL)
350: sy = &style_default;
351: if (sy->gc.fg != 8)
352: gc->fg = sy->gc.fg;
353: if (sy->gc.bg != 8)
354: gc->bg = sy->gc.bg;
1.32 nicm 355: if (sy->gc.us != 8)
356: gc->us = sy->gc.us;
1.27 nicm 357: gc->attr |= sy->gc.attr;
358:
359: if (ft0 != NULL)
360: format_free(ft0);
361: }
362:
363: /* Apply a style on top of the default style. */
364: void
365: style_apply(struct grid_cell *gc, struct options *oo, const char *name,
366: struct format_tree *ft)
367: {
1.1 nicm 368: memcpy(gc, &grid_default_cell, sizeof *gc);
1.27 nicm 369: style_add(gc, oo, name, ft);
1.15 nicm 370: }
371:
372: /* Initialize style from cell. */
373: void
374: style_set(struct style *sy, const struct grid_cell *gc)
375: {
1.17 nicm 376: memcpy(sy, &style_default, sizeof *sy);
1.15 nicm 377: memcpy(&sy->gc, gc, sizeof sy->gc);
378: }
1.1 nicm 379:
1.15 nicm 380: /* Copy style. */
381: void
382: style_copy(struct style *dst, struct style *src)
383: {
384: memcpy(dst, src, sizeof *dst);
1.1 nicm 385: }