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

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