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

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