[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.15

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