[BACK]Return to style.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/style.c, Revision 1.17

1.17    ! nicm        1: /* $OpenBSD: style.c,v 1.16 2019/03/14 10:19: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.17    ! nicm       42:        struct style    saved;
        !            43:        const char      delimiters[] = " ,";
        !            44:        char            tmp[32];
        !            45:        int             value;
        !            46:        size_t          end;
1.1       nicm       47:
                     48:        if (*in == '\0')
                     49:                return (0);
1.16      nicm       50:        style_copy(&saved, sy);
1.1       nicm       51:
1.17    ! nicm       52:        do {
        !            53:                while (*in != '\0' && strchr(delimiters, *in) != NULL) {
        !            54:                        in++;
        !            55:                        end--;
        !            56:                }
        !            57:                if (*in == '\0')
        !            58:                        break;
1.15      nicm       59:
1.1       nicm       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.17    ! nicm       67:                        sy->gc.fg = base->fg;
        !            68:                        sy->gc.bg = base->bg;
        !            69:                        sy->gc.attr = base->attr;
        !            70:                        sy->gc.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)
1.17    ! nicm       76:                                        sy->gc.fg = value;
1.10      nicm       77:                                else
1.17    ! nicm       78:                                        sy->gc.fg = base->fg;
1.1       nicm       79:                        } else if (*in == 'b' || *in == 'B') {
1.15      nicm       80:                                if (value != 8)
1.17    ! nicm       81:                                        sy->gc.bg = value;
1.10      nicm       82:                                else
1.17    ! nicm       83:                                        sy->gc.bg = base->bg;
1.1       nicm       84:                        } else
1.5       nicm       85:                                goto error;
1.1       nicm       86:                } else if (strcasecmp(tmp, "none") == 0)
1.17    ! nicm       87:                        sy->gc.attr = 0;
1.1       nicm       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.17    ! nicm       91:                        sy->gc.attr &= ~value;
1.1       nicm       92:                } else {
1.15      nicm       93:                        if ((value = attributes_fromstring(tmp)) == -1)
1.5       nicm       94:                                goto error;
1.17    ! nicm       95:                        sy->gc.attr |= value;
1.1       nicm       96:                }
                     97:
                     98:                in += end + strspn(in + end, delimiters);
                     99:        } while (*in != '\0');
1.15      nicm      100:
1.1       nicm      101:        return (0);
1.5       nicm      102:
                    103: error:
1.16      nicm      104:        style_copy(sy, &saved);
1.5       nicm      105:        return (-1);
1.1       nicm      106: }
                    107:
                    108: /* Convert style to a string. */
                    109: const char *
1.15      nicm      110: style_tostring(struct style *sy)
1.1       nicm      111: {
1.15      nicm      112:        struct grid_cell        *gc = &sy->gc;
                    113:        int                      off = 0;
                    114:        const char              *comma = "";
                    115:        static char              s[256];
1.1       nicm      116:
                    117:        *s = '\0';
                    118:
1.10      nicm      119:        if (gc->fg != 8) {
1.17    ! nicm      120:                off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
        !           121:                    colour_tostring(gc->fg));
1.15      nicm      122:                comma = ",";
1.1       nicm      123:        }
1.10      nicm      124:        if (gc->bg != 8) {
1.17    ! nicm      125:                off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
        !           126:                    colour_tostring(gc->bg));
1.15      nicm      127:                comma = ",";
1.1       nicm      128:        }
                    129:        if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
1.17    ! nicm      130:                xsnprintf(s + off, sizeof s - off, "%s%s", comma,
        !           131:                    attributes_tostring(gc->attr));
1.15      nicm      132:                comma = ",";
1.1       nicm      133:        }
                    134:
                    135:        if (*s == '\0')
                    136:                return ("default");
                    137:        return (s);
                    138: }
                    139:
                    140: /* Apply a style. */
                    141: void
                    142: style_apply(struct grid_cell *gc, struct options *oo, const char *name)
                    143: {
1.15      nicm      144:        struct style    *sy;
1.1       nicm      145:
                    146:        memcpy(gc, &grid_default_cell, sizeof *gc);
1.15      nicm      147:        sy = options_get_style(oo, name);
                    148:        gc->fg = sy->gc.fg;
                    149:        gc->bg = sy->gc.bg;
                    150:        gc->attr |= sy->gc.attr;
1.1       nicm      151: }
                    152:
                    153: /* Apply a style, updating if default. */
                    154: void
                    155: style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
                    156: {
1.15      nicm      157:        struct style    *sy;
                    158:
                    159:        sy = options_get_style(oo, name);
                    160:        if (sy->gc.fg != 8)
                    161:                gc->fg = sy->gc.fg;
                    162:        if (sy->gc.bg != 8)
                    163:                gc->bg = sy->gc.bg;
                    164:        if (sy->gc.attr != 0)
                    165:                gc->attr |= sy->gc.attr;
                    166: }
                    167:
                    168: /* Initialize style from cell. */
                    169: void
                    170: style_set(struct style *sy, const struct grid_cell *gc)
                    171: {
1.17    ! nicm      172:        memcpy(sy, &style_default, sizeof *sy);
1.15      nicm      173:        memcpy(&sy->gc, gc, sizeof sy->gc);
                    174: }
1.1       nicm      175:
1.15      nicm      176: /* Copy style. */
                    177: void
                    178: style_copy(struct style *dst, struct style *src)
                    179: {
                    180:        memcpy(dst, src, sizeof *dst);
1.8       nicm      181: }
                    182:
                    183: /* Check if two styles are the same. */
                    184: int
1.15      nicm      185: style_equal(struct style *sy1, struct style *sy2)
                    186: {
                    187:        struct grid_cell        *gc1 = &sy1->gc;
                    188:        struct grid_cell        *gc2 = &sy2->gc;
                    189:
                    190:        if (gc1->fg != gc2->fg)
                    191:                return (0);
                    192:        if (gc1->bg != gc2->bg)
                    193:                return (0);
                    194:        if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK))
                    195:                return (0);
                    196:        return (1);
                    197: }
                    198:
                    199: /* Is this style default? */
                    200: int
                    201: style_is_default(struct style *sy)
1.8       nicm      202: {
1.15      nicm      203:        return (style_equal(sy, &style_default));
1.1       nicm      204: }