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

1.25    ! nicm        1: /* $OpenBSD: style.c,v 1.24 2019/09/15 21:42:57 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.23      nicm       33:        { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0  },
1.18      nicm       34:
1.22      nicm       35:        8,
1.18      nicm       36:        STYLE_ALIGN_DEFAULT,
                     37:        STYLE_LIST_OFF,
                     38:
1.24      nicm       39:        STYLE_RANGE_NONE, 0,
                     40:
                     41:        STYLE_DEFAULT_BASE
1.15      nicm       42: };
                     43:
                     44: /*
1.24      nicm       45:  * Parse an embedded style of the form "fg=colour,bg=colour,bright,...".  Note
                     46:  * that this adds onto the given style, so it must have been initialized
                     47:  * already.
1.15      nicm       48:  */
1.1       nicm       49: int
1.15      nicm       50: style_parse(struct style *sy, const struct grid_cell *base, const char *in)
1.1       nicm       51: {
1.17      nicm       52:        struct style    saved;
1.18      nicm       53:        const char      delimiters[] = " ,", *cp;
                     54:        char            tmp[256], *found;
1.17      nicm       55:        int             value;
                     56:        size_t          end;
1.1       nicm       57:
                     58:        if (*in == '\0')
                     59:                return (0);
1.16      nicm       60:        style_copy(&saved, sy);
1.1       nicm       61:
1.17      nicm       62:        do {
1.20      nicm       63:                while (*in != '\0' && strchr(delimiters, *in) != NULL)
1.17      nicm       64:                        in++;
                     65:                if (*in == '\0')
                     66:                        break;
1.15      nicm       67:
1.1       nicm       68:                end = strcspn(in, delimiters);
                     69:                if (end > (sizeof tmp) - 1)
1.5       nicm       70:                        goto error;
1.1       nicm       71:                memcpy(tmp, in, end);
                     72:                tmp[end] = '\0';
                     73:
                     74:                if (strcasecmp(tmp, "default") == 0) {
1.17      nicm       75:                        sy->gc.fg = base->fg;
                     76:                        sy->gc.bg = base->bg;
                     77:                        sy->gc.attr = base->attr;
                     78:                        sy->gc.flags = base->flags;
1.24      nicm       79:                } else if (strcasecmp(tmp, "push-default") == 0)
                     80:                        sy->default_type = STYLE_DEFAULT_PUSH;
                     81:                else if (strcasecmp(tmp, "pop-default") == 0)
                     82:                        sy->default_type = STYLE_DEFAULT_POP;
                     83:                else if (strcasecmp(tmp, "nolist") == 0)
1.18      nicm       84:                        sy->list = STYLE_LIST_OFF;
                     85:                else if (strncasecmp(tmp, "list=", 5) == 0) {
                     86:                        if (strcasecmp(tmp + 5, "on") == 0)
                     87:                                sy->list = STYLE_LIST_ON;
                     88:                        else if (strcasecmp(tmp + 5, "focus") == 0)
                     89:                                sy->list = STYLE_LIST_FOCUS;
                     90:                        else if (strcasecmp(tmp + 5, "left-marker") == 0)
                     91:                                sy->list = STYLE_LIST_LEFT_MARKER;
                     92:                        else if (strcasecmp(tmp + 5, "right-marker") == 0)
                     93:                                sy->list = STYLE_LIST_RIGHT_MARKER;
                     94:                        else
                     95:                                goto error;
                     96:                } else if (strcasecmp(tmp, "norange") == 0) {
                     97:                        sy->range_type = style_default.range_type;
                     98:                        sy->range_argument = style_default.range_type;
                     99:                } else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
                    100:                        found = strchr(tmp + 6, '|');
                    101:                        if (found != NULL) {
                    102:                                *found++ = '\0';
                    103:                                if (*found == '\0')
                    104:                                        goto error;
                    105:                                for (cp = found; *cp != '\0'; cp++) {
                    106:                                        if (!isdigit((u_char)*cp))
                    107:                                                goto error;
                    108:                                }
                    109:                        }
                    110:                        if (strcasecmp(tmp + 6, "left") == 0) {
                    111:                                if (found != NULL)
                    112:                                        goto error;
                    113:                                sy->range_type = STYLE_RANGE_LEFT;
                    114:                                sy->range_argument = 0;
                    115:                        } else if (strcasecmp(tmp + 6, "right") == 0) {
                    116:                                if (found != NULL)
                    117:                                        goto error;
                    118:                                sy->range_type = STYLE_RANGE_RIGHT;
                    119:                                sy->range_argument = 0;
                    120:                        } else if (strcasecmp(tmp + 6, "window") == 0) {
                    121:                                if (found == NULL)
                    122:                                        goto error;
                    123:                                sy->range_type = STYLE_RANGE_WINDOW;
                    124:                                sy->range_argument = atoi(found);
                    125:                        }
                    126:                } else if (strcasecmp(tmp, "noalign") == 0)
                    127:                        sy->align = style_default.align;
                    128:                else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
                    129:                        if (strcasecmp(tmp + 6, "left") == 0)
                    130:                                sy->align = STYLE_ALIGN_LEFT;
                    131:                        else if (strcasecmp(tmp + 6, "centre") == 0)
                    132:                                sy->align = STYLE_ALIGN_CENTRE;
                    133:                        else if (strcasecmp(tmp + 6, "right") == 0)
                    134:                                sy->align = STYLE_ALIGN_RIGHT;
                    135:                        else
                    136:                                goto error;
1.22      nicm      137:                } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
                    138:                        if ((value = colour_fromstring(tmp + 5)) == -1)
                    139:                                goto error;
                    140:                        sy->fill = value;
1.1       nicm      141:                } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
1.15      nicm      142:                        if ((value = colour_fromstring(tmp + 3)) == -1)
1.5       nicm      143:                                goto error;
1.1       nicm      144:                        if (*in == 'f' || *in == 'F') {
1.15      nicm      145:                                if (value != 8)
1.17      nicm      146:                                        sy->gc.fg = value;
1.10      nicm      147:                                else
1.17      nicm      148:                                        sy->gc.fg = base->fg;
1.1       nicm      149:                        } else if (*in == 'b' || *in == 'B') {
1.15      nicm      150:                                if (value != 8)
1.17      nicm      151:                                        sy->gc.bg = value;
1.10      nicm      152:                                else
1.17      nicm      153:                                        sy->gc.bg = base->bg;
1.1       nicm      154:                        } else
1.5       nicm      155:                                goto error;
1.1       nicm      156:                } else if (strcasecmp(tmp, "none") == 0)
1.17      nicm      157:                        sy->gc.attr = 0;
1.1       nicm      158:                else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
1.15      nicm      159:                        if ((value = attributes_fromstring(tmp + 2)) == -1)
1.5       nicm      160:                                goto error;
1.17      nicm      161:                        sy->gc.attr &= ~value;
1.1       nicm      162:                } else {
1.15      nicm      163:                        if ((value = attributes_fromstring(tmp)) == -1)
1.5       nicm      164:                                goto error;
1.17      nicm      165:                        sy->gc.attr |= value;
1.1       nicm      166:                }
                    167:
                    168:                in += end + strspn(in + end, delimiters);
                    169:        } while (*in != '\0');
1.15      nicm      170:
1.1       nicm      171:        return (0);
1.5       nicm      172:
                    173: error:
1.16      nicm      174:        style_copy(sy, &saved);
1.5       nicm      175:        return (-1);
1.1       nicm      176: }
                    177:
                    178: /* Convert style to a string. */
                    179: const char *
1.15      nicm      180: style_tostring(struct style *sy)
1.1       nicm      181: {
1.15      nicm      182:        struct grid_cell        *gc = &sy->gc;
                    183:        int                      off = 0;
1.19      nicm      184:        const char              *comma = "", *tmp = "";
1.15      nicm      185:        static char              s[256];
1.18      nicm      186:        char                     b[16];
1.1       nicm      187:
                    188:        *s = '\0';
                    189:
1.18      nicm      190:        if (sy->list != STYLE_LIST_OFF) {
                    191:                if (sy->list == STYLE_LIST_ON)
                    192:                        tmp = "on";
                    193:                else if (sy->list == STYLE_LIST_FOCUS)
                    194:                        tmp = "focus";
                    195:                else if (sy->list == STYLE_LIST_LEFT_MARKER)
                    196:                        tmp = "left-marker";
                    197:                else if (sy->list == STYLE_LIST_RIGHT_MARKER)
                    198:                        tmp = "right-marker";
                    199:                off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
                    200:                    tmp);
                    201:                comma = ",";
                    202:        }
                    203:        if (sy->range_type != STYLE_RANGE_NONE) {
                    204:                if (sy->range_type == STYLE_RANGE_LEFT)
                    205:                        tmp = "left";
                    206:                else if (sy->range_type == STYLE_RANGE_RIGHT)
                    207:                        tmp = "right";
                    208:                else if (sy->range_type == STYLE_RANGE_WINDOW) {
                    209:                        snprintf(b, sizeof b, "window|%u", sy->range_argument);
                    210:                        tmp = b;
                    211:                }
                    212:                off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
                    213:                    tmp);
                    214:                comma = ",";
                    215:        }
                    216:        if (sy->align != STYLE_ALIGN_DEFAULT) {
                    217:                if (sy->align == STYLE_ALIGN_LEFT)
                    218:                        tmp = "left";
                    219:                else if (sy->align == STYLE_ALIGN_CENTRE)
                    220:                        tmp = "centre";
                    221:                else if (sy->align == STYLE_ALIGN_RIGHT)
                    222:                        tmp = "right";
                    223:                off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
                    224:                    tmp);
                    225:                comma = ",";
                    226:        }
1.24      nicm      227:        if (sy->default_type != STYLE_DEFAULT_BASE) {
                    228:                if (sy->default_type == STYLE_DEFAULT_PUSH)
                    229:                        tmp = "push-default";
                    230:                else if (sy->default_type == STYLE_DEFAULT_POP)
                    231:                        tmp = "pop-default";
                    232:                off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
                    233:                comma = ",";
                    234:        }
1.22      nicm      235:        if (sy->fill != 8) {
                    236:                off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
                    237:                    colour_tostring(sy->fill));
                    238:                comma = ",";
                    239:        }
1.10      nicm      240:        if (gc->fg != 8) {
1.17      nicm      241:                off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
                    242:                    colour_tostring(gc->fg));
1.15      nicm      243:                comma = ",";
1.1       nicm      244:        }
1.10      nicm      245:        if (gc->bg != 8) {
1.17      nicm      246:                off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
                    247:                    colour_tostring(gc->bg));
1.15      nicm      248:                comma = ",";
1.1       nicm      249:        }
1.25    ! nicm      250:        if (gc->attr != 0) {
1.17      nicm      251:                xsnprintf(s + off, sizeof s - off, "%s%s", comma,
                    252:                    attributes_tostring(gc->attr));
1.15      nicm      253:                comma = ",";
1.1       nicm      254:        }
                    255:
                    256:        if (*s == '\0')
                    257:                return ("default");
                    258:        return (s);
                    259: }
                    260:
                    261: /* Apply a style. */
                    262: void
                    263: style_apply(struct grid_cell *gc, struct options *oo, const char *name)
                    264: {
1.15      nicm      265:        struct style    *sy;
1.1       nicm      266:
                    267:        memcpy(gc, &grid_default_cell, sizeof *gc);
1.15      nicm      268:        sy = options_get_style(oo, name);
                    269:        gc->fg = sy->gc.fg;
                    270:        gc->bg = sy->gc.bg;
                    271:        gc->attr |= sy->gc.attr;
                    272: }
                    273:
                    274: /* Initialize style from cell. */
                    275: void
                    276: style_set(struct style *sy, const struct grid_cell *gc)
                    277: {
1.17      nicm      278:        memcpy(sy, &style_default, sizeof *sy);
1.15      nicm      279:        memcpy(&sy->gc, gc, sizeof sy->gc);
                    280: }
1.1       nicm      281:
1.15      nicm      282: /* Copy style. */
                    283: void
                    284: style_copy(struct style *dst, struct style *src)
                    285: {
                    286:        memcpy(dst, src, sizeof *dst);
1.8       nicm      287: }
                    288:
1.18      nicm      289: /* Check if two styles are (visibly) the same. */
1.8       nicm      290: int
1.15      nicm      291: style_equal(struct style *sy1, struct style *sy2)
                    292: {
                    293:        struct grid_cell        *gc1 = &sy1->gc;
                    294:        struct grid_cell        *gc2 = &sy2->gc;
                    295:
                    296:        if (gc1->fg != gc2->fg)
                    297:                return (0);
                    298:        if (gc1->bg != gc2->bg)
                    299:                return (0);
                    300:        if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK))
1.22      nicm      301:                return (0);
                    302:        if (sy1->fill != sy2->fill)
1.18      nicm      303:                return (0);
                    304:        if (sy1->align != sy2->align)
1.15      nicm      305:                return (0);
                    306:        return (1);
                    307: }
                    308:
                    309: /* Is this style default? */
                    310: int
                    311: style_is_default(struct style *sy)
1.8       nicm      312: {
1.15      nicm      313:        return (style_equal(sy, &style_default));
1.1       nicm      314: }