1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* $OpenBSD: style.c,v 1.22 2019/07/01 06:56:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
* 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.
*/
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
/* 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, { { ' ' }, 0, 1, 1 } },
8,
STYLE_ALIGN_DEFAULT,
STYLE_LIST_OFF,
STYLE_RANGE_NONE, 0
};
/*
* 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
style_parse(struct style *sy, const struct grid_cell *base, const char *in)
{
struct style saved;
const char delimiters[] = " ,", *cp;
char tmp[256], *found;
int value;
size_t end;
if (*in == '\0')
return (0);
style_copy(&saved, sy);
do {
while (*in != '\0' && strchr(delimiters, *in) != NULL)
in++;
if (*in == '\0')
break;
end = strcspn(in, delimiters);
if (end > (sizeof tmp) - 1)
goto error;
memcpy(tmp, in, end);
tmp[end] = '\0';
if (strcasecmp(tmp, "default") == 0) {
sy->gc.fg = base->fg;
sy->gc.bg = base->bg;
sy->gc.attr = base->attr;
sy->gc.flags = base->flags;
} else if (strcasecmp(tmp, "nolist") == 0)
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;
} else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
if ((value = colour_fromstring(tmp + 5)) == -1)
goto error;
sy->fill = value;
} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
if ((value = colour_fromstring(tmp + 3)) == -1)
goto error;
if (*in == 'f' || *in == 'F') {
if (value != 8)
sy->gc.fg = value;
else
sy->gc.fg = base->fg;
} else if (*in == 'b' || *in == 'B') {
if (value != 8)
sy->gc.bg = value;
else
sy->gc.bg = base->bg;
} else
goto error;
} else if (strcasecmp(tmp, "none") == 0)
sy->gc.attr = 0;
else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
if ((value = attributes_fromstring(tmp + 2)) == -1)
goto error;
sy->gc.attr &= ~value;
} else {
if ((value = attributes_fromstring(tmp)) == -1)
goto error;
sy->gc.attr |= value;
}
in += end + strspn(in + end, delimiters);
} while (*in != '\0');
return (0);
error:
style_copy(sy, &saved);
return (-1);
}
/* Convert style to a string. */
const char *
style_tostring(struct style *sy)
{
struct grid_cell *gc = &sy->gc;
int off = 0;
const char *comma = "", *tmp = "";
static char s[256];
char b[16];
*s = '\0';
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 = ",";
}
if (sy->fill != 8) {
off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
colour_tostring(sy->fill));
comma = ",";
}
if (gc->fg != 8) {
off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
colour_tostring(gc->fg));
comma = ",";
}
if (gc->bg != 8) {
off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
colour_tostring(gc->bg));
comma = ",";
}
if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
xsnprintf(s + off, sizeof s - off, "%s%s", comma,
attributes_tostring(gc->attr));
comma = ",";
}
if (*s == '\0')
return ("default");
return (s);
}
/* Apply a style. */
void
style_apply(struct grid_cell *gc, struct options *oo, const char *name)
{
struct style *sy;
memcpy(gc, &grid_default_cell, sizeof *gc);
sy = options_get_style(oo, name);
gc->fg = sy->gc.fg;
gc->bg = sy->gc.bg;
gc->attr |= sy->gc.attr;
}
/* Apply a style, updating if default. */
void
style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
{
struct style *sy;
sy = options_get_style(oo, name);
if (sy->gc.fg != 8)
gc->fg = sy->gc.fg;
if (sy->gc.bg != 8)
gc->bg = sy->gc.bg;
if (sy->gc.attr != 0)
gc->attr |= sy->gc.attr;
}
/* Initialize style from cell. */
void
style_set(struct style *sy, const struct grid_cell *gc)
{
memcpy(sy, &style_default, 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 (visibly) the same. */
int
style_equal(struct style *sy1, struct style *sy2)
{
struct grid_cell *gc1 = &sy1->gc;
struct grid_cell *gc2 = &sy2->gc;
if (gc1->fg != gc2->fg)
return (0);
if (gc1->bg != gc2->bg)
return (0);
if ((gc1->attr & STYLE_ATTR_MASK) != (gc2->attr & STYLE_ATTR_MASK))
return (0);
if (sy1->fill != sy2->fill)
return (0);
if (sy1->align != sy2->align)
return (0);
return (1);
}
/* Is this style default? */
int
style_is_default(struct style *sy)
{
return (style_equal(sy, &style_default));
}
|