2014-01-28 23:07:09 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2014-01-28 23:07:09 +00:00
|
|
|
* Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 23:27:14 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-03-18 20:53:33 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-28 23:07:09 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2019-03-14 09:53:52 +00:00
|
|
|
/* Mask for bits not included in style. */
|
2020-04-23 06:15:17 +00:00
|
|
|
#define STYLE_ATTR_MASK (~0)
|
2019-03-14 09:53:52 +00:00
|
|
|
|
|
|
|
/* Default style. */
|
|
|
|
static struct style style_default = {
|
2019-07-06 20:37:29 +00:00
|
|
|
{ { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0 },
|
2019-03-18 20:53:33 +00:00
|
|
|
|
2019-07-01 06:56:00 +00:00
|
|
|
8,
|
2019-03-18 20:53:33 +00:00
|
|
|
STYLE_ALIGN_DEFAULT,
|
|
|
|
STYLE_LIST_OFF,
|
|
|
|
|
2019-09-15 21:42:57 +00:00
|
|
|
STYLE_RANGE_NONE, 0,
|
|
|
|
|
|
|
|
STYLE_DEFAULT_BASE
|
2019-03-14 09:53:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2019-09-15 21:42:57 +00:00
|
|
|
* 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
|
|
|
|
* already.
|
2019-03-14 09:53:52 +00:00
|
|
|
*/
|
2014-01-28 23:07:09 +00:00
|
|
|
int
|
2019-03-14 09:53:52 +00:00
|
|
|
style_parse(struct style *sy, const struct grid_cell *base, const char *in)
|
2014-01-28 23:07:09 +00:00
|
|
|
{
|
2019-03-14 21:46:08 +00:00
|
|
|
struct style saved;
|
2019-03-18 20:53:33 +00:00
|
|
|
const char delimiters[] = " ,", *cp;
|
|
|
|
char tmp[256], *found;
|
2019-03-14 21:46:08 +00:00
|
|
|
int value;
|
|
|
|
size_t end;
|
2014-01-28 23:07:09 +00:00
|
|
|
|
|
|
|
if (*in == '\0')
|
|
|
|
return (0);
|
2019-03-14 10:19:52 +00:00
|
|
|
style_copy(&saved, sy);
|
2014-01-28 23:07:09 +00:00
|
|
|
|
2020-04-27 14:15:12 +00:00
|
|
|
log_debug("%s: %s", __func__, in);
|
2014-01-28 23:07:09 +00:00
|
|
|
do {
|
2019-05-12 18:16:33 +00:00
|
|
|
while (*in != '\0' && strchr(delimiters, *in) != NULL)
|
2019-03-14 21:46:08 +00:00
|
|
|
in++;
|
|
|
|
if (*in == '\0')
|
|
|
|
break;
|
|
|
|
|
2014-01-28 23:07:09 +00:00
|
|
|
end = strcspn(in, delimiters);
|
|
|
|
if (end > (sizeof tmp) - 1)
|
2015-02-18 15:32:37 +00:00
|
|
|
goto error;
|
2014-01-28 23:07:09 +00:00
|
|
|
memcpy(tmp, in, end);
|
|
|
|
tmp[end] = '\0';
|
|
|
|
|
2020-04-27 14:15:12 +00:00
|
|
|
log_debug("%s: %s", __func__, tmp);
|
2014-01-28 23:07:09 +00:00
|
|
|
if (strcasecmp(tmp, "default") == 0) {
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.fg = base->fg;
|
|
|
|
sy->gc.bg = base->bg;
|
|
|
|
sy->gc.attr = base->attr;
|
|
|
|
sy->gc.flags = base->flags;
|
2019-09-15 21:42:57 +00:00
|
|
|
} else if (strcasecmp(tmp, "push-default") == 0)
|
|
|
|
sy->default_type = STYLE_DEFAULT_PUSH;
|
|
|
|
else if (strcasecmp(tmp, "pop-default") == 0)
|
|
|
|
sy->default_type = STYLE_DEFAULT_POP;
|
|
|
|
else if (strcasecmp(tmp, "nolist") == 0)
|
2019-03-18 20:53:33 +00:00
|
|
|
sy->list = STYLE_LIST_OFF;
|
|
|
|
else if (strncasecmp(tmp, "list=", 5) == 0) {
|
|
|
|
if (strcasecmp(tmp + 5, "on") == 0)
|
|
|
|
sy->list = STYLE_LIST_ON;
|
|
|
|
else if (strcasecmp(tmp + 5, "focus") == 0)
|
|
|
|
sy->list = STYLE_LIST_FOCUS;
|
|
|
|
else if (strcasecmp(tmp + 5, "left-marker") == 0)
|
|
|
|
sy->list = STYLE_LIST_LEFT_MARKER;
|
|
|
|
else if (strcasecmp(tmp + 5, "right-marker") == 0)
|
|
|
|
sy->list = STYLE_LIST_RIGHT_MARKER;
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
} else if (strcasecmp(tmp, "norange") == 0) {
|
|
|
|
sy->range_type = style_default.range_type;
|
|
|
|
sy->range_argument = style_default.range_type;
|
|
|
|
} else if (end > 6 && strncasecmp(tmp, "range=", 6) == 0) {
|
|
|
|
found = strchr(tmp + 6, '|');
|
|
|
|
if (found != NULL) {
|
|
|
|
*found++ = '\0';
|
|
|
|
if (*found == '\0')
|
|
|
|
goto error;
|
|
|
|
for (cp = found; *cp != '\0'; cp++) {
|
|
|
|
if (!isdigit((u_char)*cp))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcasecmp(tmp + 6, "left") == 0) {
|
|
|
|
if (found != NULL)
|
|
|
|
goto error;
|
|
|
|
sy->range_type = STYLE_RANGE_LEFT;
|
|
|
|
sy->range_argument = 0;
|
|
|
|
} else if (strcasecmp(tmp + 6, "right") == 0) {
|
|
|
|
if (found != NULL)
|
|
|
|
goto error;
|
|
|
|
sy->range_type = STYLE_RANGE_RIGHT;
|
|
|
|
sy->range_argument = 0;
|
|
|
|
} else if (strcasecmp(tmp + 6, "window") == 0) {
|
|
|
|
if (found == NULL)
|
|
|
|
goto error;
|
|
|
|
sy->range_type = STYLE_RANGE_WINDOW;
|
|
|
|
sy->range_argument = atoi(found);
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(tmp, "noalign") == 0)
|
|
|
|
sy->align = style_default.align;
|
|
|
|
else if (end > 6 && strncasecmp(tmp, "align=", 6) == 0) {
|
|
|
|
if (strcasecmp(tmp + 6, "left") == 0)
|
|
|
|
sy->align = STYLE_ALIGN_LEFT;
|
|
|
|
else if (strcasecmp(tmp + 6, "centre") == 0)
|
|
|
|
sy->align = STYLE_ALIGN_CENTRE;
|
|
|
|
else if (strcasecmp(tmp + 6, "right") == 0)
|
|
|
|
sy->align = STYLE_ALIGN_RIGHT;
|
|
|
|
else
|
|
|
|
goto error;
|
2019-07-01 06:56:00 +00:00
|
|
|
} else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
|
|
|
|
if ((value = colour_fromstring(tmp + 5)) == -1)
|
|
|
|
goto error;
|
|
|
|
sy->fill = value;
|
2014-01-28 23:07:09 +00:00
|
|
|
} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
|
2019-03-14 09:53:52 +00:00
|
|
|
if ((value = colour_fromstring(tmp + 3)) == -1)
|
2015-02-18 15:32:37 +00:00
|
|
|
goto error;
|
2014-01-28 23:07:09 +00:00
|
|
|
if (*in == 'f' || *in == 'F') {
|
2019-03-14 09:53:52 +00:00
|
|
|
if (value != 8)
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.fg = value;
|
2016-07-15 00:42:56 +00:00
|
|
|
else
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.fg = base->fg;
|
2014-01-28 23:07:09 +00:00
|
|
|
} else if (*in == 'b' || *in == 'B') {
|
2019-03-14 09:53:52 +00:00
|
|
|
if (value != 8)
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.bg = value;
|
2016-07-15 00:42:56 +00:00
|
|
|
else
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.bg = base->bg;
|
2014-01-28 23:07:09 +00:00
|
|
|
} else
|
2015-02-18 15:32:37 +00:00
|
|
|
goto error;
|
2014-01-28 23:07:09 +00:00
|
|
|
} else if (strcasecmp(tmp, "none") == 0)
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.attr = 0;
|
2014-01-28 23:07:09 +00:00
|
|
|
else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
|
2019-03-14 09:53:52 +00:00
|
|
|
if ((value = attributes_fromstring(tmp + 2)) == -1)
|
2015-02-18 15:32:37 +00:00
|
|
|
goto error;
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.attr &= ~value;
|
2014-01-28 23:07:09 +00:00
|
|
|
} else {
|
2019-03-14 09:53:52 +00:00
|
|
|
if ((value = attributes_fromstring(tmp)) == -1)
|
2015-02-18 15:32:37 +00:00
|
|
|
goto error;
|
2019-03-14 21:46:08 +00:00
|
|
|
sy->gc.attr |= value;
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in += end + strspn(in + end, delimiters);
|
|
|
|
} while (*in != '\0');
|
2019-03-14 09:53:52 +00:00
|
|
|
|
2014-01-28 23:07:09 +00:00
|
|
|
return (0);
|
2015-02-18 15:32:37 +00:00
|
|
|
|
|
|
|
error:
|
2019-03-14 10:19:52 +00:00
|
|
|
style_copy(sy, &saved);
|
2015-02-18 15:32:37 +00:00
|
|
|
return (-1);
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert style to a string. */
|
|
|
|
const char *
|
2019-03-14 09:53:52 +00:00
|
|
|
style_tostring(struct style *sy)
|
2014-01-28 23:07:09 +00:00
|
|
|
{
|
2019-03-14 09:53:52 +00:00
|
|
|
struct grid_cell *gc = &sy->gc;
|
|
|
|
int off = 0;
|
2019-04-05 20:32:31 +00:00
|
|
|
const char *comma = "", *tmp = "";
|
2019-03-14 09:53:52 +00:00
|
|
|
static char s[256];
|
2019-03-18 20:53:33 +00:00
|
|
|
char b[16];
|
2014-01-28 23:07:09 +00:00
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
|
2019-03-18 20:53:33 +00:00
|
|
|
if (sy->list != STYLE_LIST_OFF) {
|
|
|
|
if (sy->list == STYLE_LIST_ON)
|
|
|
|
tmp = "on";
|
|
|
|
else if (sy->list == STYLE_LIST_FOCUS)
|
|
|
|
tmp = "focus";
|
|
|
|
else if (sy->list == STYLE_LIST_LEFT_MARKER)
|
|
|
|
tmp = "left-marker";
|
|
|
|
else if (sy->list == STYLE_LIST_RIGHT_MARKER)
|
|
|
|
tmp = "right-marker";
|
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
|
|
|
|
tmp);
|
|
|
|
comma = ",";
|
|
|
|
}
|
|
|
|
if (sy->range_type != STYLE_RANGE_NONE) {
|
|
|
|
if (sy->range_type == STYLE_RANGE_LEFT)
|
|
|
|
tmp = "left";
|
|
|
|
else if (sy->range_type == STYLE_RANGE_RIGHT)
|
|
|
|
tmp = "right";
|
|
|
|
else if (sy->range_type == STYLE_RANGE_WINDOW) {
|
|
|
|
snprintf(b, sizeof b, "window|%u", sy->range_argument);
|
|
|
|
tmp = b;
|
|
|
|
}
|
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
|
|
|
|
tmp);
|
|
|
|
comma = ",";
|
|
|
|
}
|
|
|
|
if (sy->align != STYLE_ALIGN_DEFAULT) {
|
|
|
|
if (sy->align == STYLE_ALIGN_LEFT)
|
|
|
|
tmp = "left";
|
|
|
|
else if (sy->align == STYLE_ALIGN_CENTRE)
|
|
|
|
tmp = "centre";
|
|
|
|
else if (sy->align == STYLE_ALIGN_RIGHT)
|
|
|
|
tmp = "right";
|
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
|
|
|
|
tmp);
|
|
|
|
comma = ",";
|
|
|
|
}
|
2019-09-15 21:42:57 +00:00
|
|
|
if (sy->default_type != STYLE_DEFAULT_BASE) {
|
|
|
|
if (sy->default_type == STYLE_DEFAULT_PUSH)
|
|
|
|
tmp = "push-default";
|
|
|
|
else if (sy->default_type == STYLE_DEFAULT_POP)
|
|
|
|
tmp = "pop-default";
|
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
|
|
|
|
comma = ",";
|
|
|
|
}
|
2019-07-01 06:56:00 +00:00
|
|
|
if (sy->fill != 8) {
|
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
|
|
|
|
colour_tostring(sy->fill));
|
|
|
|
comma = ",";
|
|
|
|
}
|
2016-07-15 00:42:56 +00:00
|
|
|
if (gc->fg != 8) {
|
2019-03-14 21:46:08 +00:00
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
|
|
|
|
colour_tostring(gc->fg));
|
2019-03-14 09:53:52 +00:00
|
|
|
comma = ",";
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
2016-07-15 00:42:56 +00:00
|
|
|
if (gc->bg != 8) {
|
2019-03-14 21:46:08 +00:00
|
|
|
off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
|
|
|
|
colour_tostring(gc->bg));
|
2019-03-14 09:53:52 +00:00
|
|
|
comma = ",";
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
2020-04-23 06:15:17 +00:00
|
|
|
if (gc->attr != 0) {
|
2019-03-14 21:46:08 +00:00
|
|
|
xsnprintf(s + off, sizeof s - off, "%s%s", comma,
|
|
|
|
attributes_tostring(gc->attr));
|
2019-03-14 09:53:52 +00:00
|
|
|
comma = ",";
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == '\0')
|
|
|
|
return ("default");
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:50:07 +00:00
|
|
|
/* Apply a style on top of the given style. */
|
2014-01-28 23:07:09 +00:00
|
|
|
void
|
2020-04-28 12:50:07 +00:00
|
|
|
style_add(struct grid_cell *gc, struct options *oo, const char *name,
|
|
|
|
struct format_tree *ft)
|
2014-01-28 23:07:09 +00:00
|
|
|
{
|
2020-04-28 12:50:07 +00:00
|
|
|
struct style *sy;
|
|
|
|
struct format_tree *ft0 = NULL;
|
2014-01-28 23:07:09 +00:00
|
|
|
|
2020-04-28 12:50:07 +00:00
|
|
|
if (ft == NULL)
|
|
|
|
ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
|
|
|
|
|
|
|
|
sy = options_string_to_style(oo, name, ft);
|
|
|
|
if (sy == NULL)
|
|
|
|
sy = &style_default;
|
|
|
|
if (sy->gc.fg != 8)
|
|
|
|
gc->fg = sy->gc.fg;
|
|
|
|
if (sy->gc.bg != 8)
|
|
|
|
gc->bg = sy->gc.bg;
|
2019-03-14 09:53:52 +00:00
|
|
|
gc->attr |= sy->gc.attr;
|
2020-04-28 12:50:07 +00:00
|
|
|
|
|
|
|
if (ft0 != NULL)
|
|
|
|
format_free(ft0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply a style on top of the default style. */
|
|
|
|
void
|
|
|
|
style_apply(struct grid_cell *gc, struct options *oo, const char *name,
|
|
|
|
struct format_tree *ft)
|
|
|
|
{
|
|
|
|
memcpy(gc, &grid_default_cell, sizeof *gc);
|
|
|
|
style_add(gc, oo, name, ft);
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 09:53:52 +00:00
|
|
|
/* Initialize style from cell. */
|
|
|
|
void
|
|
|
|
style_set(struct style *sy, const struct grid_cell *gc)
|
|
|
|
{
|
2019-03-14 21:46:08 +00:00
|
|
|
memcpy(sy, &style_default, sizeof *sy);
|
2019-03-14 09:53:52 +00:00
|
|
|
memcpy(&sy->gc, gc, sizeof sy->gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy style. */
|
|
|
|
void
|
|
|
|
style_copy(struct style *dst, struct style *src)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, sizeof *dst);
|
2014-01-28 23:07:09 +00:00
|
|
|
}
|