version 1.14, 2017/03/22 07:16:54 |
version 1.15, 2019/03/14 09:53:52 |
|
|
|
|
#include "tmux.h" |
#include "tmux.h" |
|
|
/* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */ |
/* Mask for bits not included in style. */ |
|
#define STYLE_ATTR_MASK (~GRID_ATTR_CHARSET) |
|
|
|
/* Default style. */ |
|
static struct style style_default = { |
|
{ 0, 0, 8, 8, { { ' ' }, 0, 1, 1 } } |
|
}; |
|
|
|
/* |
|
* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". |
|
* Note that this adds onto the given style, so it must have been initialized |
|
* alredy. |
|
*/ |
int |
int |
style_parse(const struct grid_cell *defgc, struct grid_cell *gc, |
style_parse(struct style *sy, const struct grid_cell *base, const char *in) |
const char *in) |
|
{ |
{ |
struct grid_cell savedgc; |
struct grid_cell *gc = &sy->gc; |
const char delimiters[] = " ,"; |
struct grid_cell saved; |
char tmp[32]; |
const char delimiters[] = " ,"; |
int val, fg, bg, attr, flags; |
char tmp[32]; |
size_t end; |
int value, fg, bg, attr, flags; |
|
size_t end; |
|
|
if (*in == '\0') |
if (*in == '\0') |
return (0); |
return (0); |
if (strchr(delimiters, in[strlen(in) - 1]) != NULL) |
if (strchr(delimiters, in[strlen(in) - 1]) != NULL) |
return (-1); |
return (-1); |
memcpy(&savedgc, gc, sizeof savedgc); |
memcpy(&saved, base, sizeof saved); |
|
|
fg = gc->fg; |
fg = gc->fg; |
bg = gc->bg; |
bg = gc->bg; |
attr = gc->attr; |
attr = gc->attr; |
flags = gc->flags; |
flags = gc->flags; |
|
|
do { |
do { |
end = strcspn(in, delimiters); |
end = strcspn(in, delimiters); |
if (end > (sizeof tmp) - 1) |
if (end > (sizeof tmp) - 1) |
|
|
tmp[end] = '\0'; |
tmp[end] = '\0'; |
|
|
if (strcasecmp(tmp, "default") == 0) { |
if (strcasecmp(tmp, "default") == 0) { |
fg = defgc->fg; |
fg = base->fg; |
bg = defgc->bg; |
bg = base->bg; |
attr = defgc->attr; |
attr = base->attr; |
flags = defgc->flags; |
flags = base->flags; |
} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { |
} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { |
if ((val = colour_fromstring(tmp + 3)) == -1) |
if ((value = colour_fromstring(tmp + 3)) == -1) |
goto error; |
goto error; |
if (*in == 'f' || *in == 'F') { |
if (*in == 'f' || *in == 'F') { |
if (val != 8) |
if (value != 8) |
fg = val; |
fg = value; |
else |
else |
fg = defgc->fg; |
fg = base->fg; |
} else if (*in == 'b' || *in == 'B') { |
} else if (*in == 'b' || *in == 'B') { |
if (val != 8) |
if (value != 8) |
bg = val; |
bg = value; |
else |
else |
bg = defgc->bg; |
bg = base->bg; |
} else |
} else |
goto error; |
goto error; |
} else if (strcasecmp(tmp, "none") == 0) |
} else if (strcasecmp(tmp, "none") == 0) |
attr = 0; |
attr = 0; |
else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { |
else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) { |
if ((val = attributes_fromstring(tmp + 2)) == -1) |
if ((value = attributes_fromstring(tmp + 2)) == -1) |
goto error; |
goto error; |
attr &= ~val; |
attr &= ~value; |
} else { |
} else { |
if ((val = attributes_fromstring(tmp)) == -1) |
if ((value = attributes_fromstring(tmp)) == -1) |
goto error; |
goto error; |
attr |= val; |
attr |= value; |
} |
} |
|
|
in += end + strspn(in + end, delimiters); |
in += end + strspn(in + end, delimiters); |
} while (*in != '\0'); |
} while (*in != '\0'); |
|
|
gc->fg = fg; |
gc->fg = fg; |
gc->bg = bg; |
gc->bg = bg; |
gc->attr = attr; |
gc->attr = attr; |
|
|
return (0); |
return (0); |
|
|
error: |
error: |
memcpy(gc, &savedgc, sizeof *gc); |
memcpy(gc, &saved, sizeof *gc); |
return (-1); |
return (-1); |
} |
} |
|
|
/* Convert style to a string. */ |
/* Convert style to a string. */ |
const char * |
const char * |
style_tostring(struct grid_cell *gc) |
style_tostring(struct style *sy) |
{ |
{ |
int off = 0, comma = 0; |
struct grid_cell *gc = &sy->gc; |
static char s[256]; |
int off = 0; |
|
const char *comma = ""; |
|
static char s[256]; |
|
|
*s = '\0'; |
*s = '\0'; |
|
|
if (gc->fg != 8) { |
if (gc->fg != 8) { |
off += xsnprintf(s, sizeof s, "fg=%s", colour_tostring(gc->fg)); |
off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", |
comma = 1; |
comma, colour_tostring(gc->fg)); |
|
comma = ","; |
} |
} |
|
|
if (gc->bg != 8) { |
if (gc->bg != 8) { |
off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", |
off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", |
comma ? "," : "", colour_tostring(gc->bg)); |
comma, colour_tostring(gc->bg)); |
comma = 1; |
comma = ","; |
} |
} |
|
|
if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { |
if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) { |
xsnprintf(s + off, sizeof s - off, "%s%s", |
xsnprintf(s + off, sizeof s - off, "%s%s", |
comma ? "," : "", attributes_tostring(gc->attr)); |
comma, attributes_tostring(gc->attr)); |
|
comma = ","; |
} |
} |
|
|
if (*s == '\0') |
if (*s == '\0') |
|
|
void |
void |
style_apply(struct grid_cell *gc, struct options *oo, const char *name) |
style_apply(struct grid_cell *gc, struct options *oo, const char *name) |
{ |
{ |
const struct grid_cell *gcp; |
struct style *sy; |
|
|
memcpy(gc, &grid_default_cell, sizeof *gc); |
memcpy(gc, &grid_default_cell, sizeof *gc); |
gcp = options_get_style(oo, name); |
sy = options_get_style(oo, name); |
gc->fg = gcp->fg; |
gc->fg = sy->gc.fg; |
gc->bg = gcp->bg; |
gc->bg = sy->gc.bg; |
gc->attr |= gcp->attr; |
gc->attr |= sy->gc.attr; |
} |
} |
|
|
/* Apply a style, updating if default. */ |
/* Apply a style, updating if default. */ |
void |
void |
style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) |
style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) |
{ |
{ |
const struct grid_cell *gcp; |
struct style *sy; |
|
|
gcp = options_get_style(oo, name); |
sy = options_get_style(oo, name); |
if (gcp->fg != 8) |
if (sy->gc.fg != 8) |
gc->fg = gcp->fg; |
gc->fg = sy->gc.fg; |
if (gcp->bg != 8) |
if (sy->gc.bg != 8) |
gc->bg = gcp->bg; |
gc->bg = sy->gc.bg; |
if (gcp->attr != 0) |
if (sy->gc.attr != 0) |
gc->attr |= gcp->attr; |
gc->attr |= sy->gc.attr; |
} |
} |
|
|
|
/* Initialize style from cell. */ |
|
void |
|
style_set(struct style *sy, const struct grid_cell *gc) |
|
{ |
|
memset(sy, 0, sizeof *sy); |
|
memcpy(&sy->gc, gc, sizeof sy->gc); |
|
} |
|
|
|
/* Copy style. */ |
|
void |
|
style_copy(struct style *dst, struct style *src) |
|
{ |
|
memcpy(dst, src, sizeof *dst); |
|
} |
|
|
/* Check if two styles are the same. */ |
/* Check if two styles are the same. */ |
int |
int |
style_equal(const struct grid_cell *gc1, const struct grid_cell *gc2) |
style_equal(struct style *sy1, struct style *sy2) |
{ |
{ |
return (gc1->fg == gc2->fg && |
struct grid_cell *gc1 = &sy1->gc; |
gc1->bg == gc2->bg && |
struct grid_cell *gc2 = &sy2->gc; |
(gc1->flags & ~GRID_FLAG_PADDING) == |
|
(gc2->flags & ~GRID_FLAG_PADDING) && |
if (gc1->fg != gc2->fg) |
(gc1->attr & ~GRID_ATTR_CHARSET) == |
return (0); |
(gc2->attr & ~GRID_ATTR_CHARSET)); |
if (gc1->bg != gc2->bg) |
|
return (0); |
|
if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK)) |
|
return (0); |
|
return (1); |
|
} |
|
|
|
/* Is this style default? */ |
|
int |
|
style_is_default(struct style *sy) |
|
{ |
|
return (style_equal(sy, &style_default)); |
} |
} |