4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5 * Copyright (c) 2014 Tiago Cunha <tcunha@users.sourceforge.net>
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.
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.
24 /* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
26 style_parse(const struct grid_cell
*defgc
, struct grid_cell
*gc
,
29 const char delimiters
[] = " ,";
33 u_char fg
, bg
, attr
, flags
;
37 if (strchr(delimiters
, in
[strlen(in
) - 1]) != NULL
)
45 end
= strcspn(in
, delimiters
);
46 if (end
> (sizeof tmp
) - 1)
51 if (strcasecmp(tmp
, "default") == 0) {
55 flags
&= ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
57 defgc
->flags
& (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
58 } else if (end
> 3 && strncasecmp(tmp
+ 1, "g=", 2) == 0) {
59 if ((val
= colour_fromstring(tmp
+ 3)) == -1)
61 if (*in
== 'f' || *in
== 'F') {
64 flags
|= GRID_FLAG_FG256
;
67 flags
&= ~GRID_FLAG_FG256
;
71 flags
&= ~GRID_FLAG_FG256
;
72 flags
|= defgc
->flags
& GRID_FLAG_FG256
;
74 } else if (*in
== 'b' || *in
== 'B') {
77 flags
|= GRID_FLAG_BG256
;
80 flags
&= ~GRID_FLAG_BG256
;
84 flags
&= ~GRID_FLAG_BG256
;
85 flags
|= defgc
->flags
& GRID_FLAG_BG256
;
89 } else if (strcasecmp(tmp
, "none") == 0)
91 else if (end
> 2 && strncasecmp(tmp
, "no", 2) == 0) {
92 if ((val
= attributes_fromstring(tmp
+ 2)) == -1)
96 if ((val
= attributes_fromstring(tmp
)) == -1)
101 in
+= end
+ strspn(in
+ end
, delimiters
);
102 } while (*in
!= '\0');
111 /* Convert style to a string. */
113 style_tostring(struct grid_cell
*gc
)
115 int c
, off
= 0, comma
= 0;
121 if (gc
->flags
& GRID_FLAG_FG256
)
125 off
+= xsnprintf(s
, sizeof s
, "fg=%s", colour_tostring(c
));
130 if (gc
->flags
& GRID_FLAG_BG256
)
134 off
+= xsnprintf(s
+ off
, sizeof s
- off
, "%sbg=%s",
135 comma
? "," : "", colour_tostring(c
));
139 if (gc
->attr
!= 0 && gc
->attr
!= GRID_ATTR_CHARSET
) {
140 xsnprintf(s
+ off
, sizeof s
- off
, "%s%s",
141 comma
? "," : "", attributes_tostring(gc
->attr
));
149 /* Synchronize new -style option with the old one. */
151 style_update_new(struct options
*oo
, const char *name
, const char *newname
)
154 struct grid_cell
*gc
;
156 /* It's a colour or attribute, but with no -style equivalent. */
160 gc
= options_get_style(oo
, newname
);
161 value
= options_get_number(oo
, name
);
163 if (strstr(name
, "-bg") != NULL
)
164 colour_set_bg(gc
, value
);
165 else if (strstr(name
, "-fg") != NULL
)
166 colour_set_fg(gc
, value
);
167 else if (strstr(name
, "-attr") != NULL
)
171 /* Synchronize all the old options with the new -style one. */
173 style_update_old(struct options
*oo
, const char *name
, struct grid_cell
*gc
)
178 size
= strrchr(name
, '-') - name
;
180 if (gc
->flags
& GRID_FLAG_BG256
)
184 xsnprintf(newname
, sizeof newname
, "%.*s-bg", size
, name
);
185 options_set_number(oo
, newname
, c
);
187 if (gc
->flags
& GRID_FLAG_FG256
)
191 xsnprintf(newname
, sizeof newname
, "%.*s-fg", size
, name
);
192 options_set_number(oo
, newname
, c
);
194 xsnprintf(newname
, sizeof newname
, "%.*s-attr", size
, name
);
195 options_set_number(oo
, newname
, gc
->attr
);
200 style_apply(struct grid_cell
*gc
, struct options
*oo
, const char *name
)
202 struct grid_cell
*gcp
;
204 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
205 gcp
= options_get_style(oo
, name
);
206 if (gcp
->flags
& GRID_FLAG_FG256
)
207 colour_set_fg(gc
, gcp
->fg
| 0x100);
209 colour_set_fg(gc
, gcp
->fg
);
210 if (gcp
->flags
& GRID_FLAG_BG256
)
211 colour_set_bg(gc
, gcp
->bg
| 0x100);
213 colour_set_bg(gc
, gcp
->bg
);
214 gc
->attr
|= gcp
->attr
;
217 /* Apply a style, updating if default. */
219 style_apply_update(struct grid_cell
*gc
, struct options
*oo
, const char *name
)
221 struct grid_cell
*gcp
;
223 gcp
= options_get_style(oo
, name
);
225 if (gcp
->flags
& GRID_FLAG_FG256
)
226 colour_set_fg(gc
, gcp
->fg
| 0x100);
228 colour_set_fg(gc
, gcp
->fg
);
231 if (gcp
->flags
& GRID_FLAG_BG256
)
232 colour_set_bg(gc
, gcp
->bg
| 0x100);
234 colour_set_bg(gc
, gcp
->bg
);
237 gc
->attr
|= gcp
->attr
;