tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

style.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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  */
19 
20 #include <sys/types.h>
21 
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "tmux.h"
27 
28 /* Mask for bits not included in style. */
29 #define STYLE_ATTR_MASK (~0)
30 
31 /* Default style. */
32 static struct style style_default = {
33  { { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0 },
34  0,
35 
36  8,
39 
41 
43 };
44 
45 /*
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.
49  */
50 int
51 style_parse(struct style *sy, const struct grid_cell *base, const char *in)
52 {
53  struct style saved;
54  const char delimiters[] = " ,", *cp;
55  char tmp[256], *found;
56  int value;
57  size_t end;
58 
59  if (*in == '\0')
60  return (0);
61  style_copy(&saved, sy);
62 
63  log_debug("%s: %s", __func__, in);
64  do {
65  while (*in != '\0' && strchr(delimiters, *in) != NULL)
66  in++;
67  if (*in == '\0')
68  break;
69 
70  end = strcspn(in, delimiters);
71  if (end > (sizeof tmp) - 1)
72  goto error;
73  memcpy(tmp, in, end);
74  tmp[end] = '\0';
75 
76  log_debug("%s: %s", __func__, tmp);
77  if (strcasecmp(tmp, "default") == 0) {
78  sy->gc.fg = base->fg;
79  sy->gc.bg = base->bg;
80  sy->gc.attr = base->attr;
81  sy->gc.flags = base->flags;
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)
88  else if (strcasecmp(tmp, "pop-default") == 0)
90  else if (strcasecmp(tmp, "nolist") == 0)
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)
99  else if (strcasecmp(tmp + 5, "right-marker") == 0)
101  else
102  goto error;
103  } else if (strcasecmp(tmp, "norange") == 0) {
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;
121  sy->range_argument = 0;
122  } else if (strcasecmp(tmp + 6, "right") == 0) {
123  if (found != NULL)
124  goto error;
126  sy->range_argument = 0;
127  } else if (strcasecmp(tmp + 6, "window") == 0) {
128  if (found == NULL)
129  goto error;
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)
140  else if (strcasecmp(tmp + 6, "right") == 0)
141  sy->align = STYLE_ALIGN_RIGHT;
142  else if (strcasecmp(tmp + 6, "absolute-centre") == 0)
144  else
145  goto error;
146  } else if (end > 5 && strncasecmp(tmp, "fill=", 5) == 0) {
147  if ((value = colour_fromstring(tmp + 5)) == -1)
148  goto error;
149  sy->fill = value;
150  } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
151  if ((value = colour_fromstring(tmp + 3)) == -1)
152  goto error;
153  if (*in == 'f' || *in == 'F') {
154  if (value != 8)
155  sy->gc.fg = value;
156  else
157  sy->gc.fg = base->fg;
158  } else if (*in == 'b' || *in == 'B') {
159  if (value != 8)
160  sy->gc.bg = value;
161  else
162  sy->gc.bg = base->bg;
163  } else
164  goto error;
165  } else if (strcasecmp(tmp, "none") == 0)
166  sy->gc.attr = 0;
167  else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
168  if ((value = attributes_fromstring(tmp + 2)) == -1)
169  goto error;
170  sy->gc.attr &= ~~value;
171  } else {
172  if ((value = attributes_fromstring(tmp)) == -1)
173  goto error;
174  sy->gc.attr |= value;
175  }
176 
177  in += end + strspn(in + end, delimiters);
178  } while (*in != '\0');
179 
180  return (0);
181 
182 error:
183  style_copy(sy, &saved);
184  return (-1);
185 }
186 
187 /* Convert style to a string. */
188 const char *
190 {
191  struct grid_cell *gc = &sy->gc;
192  int off = 0;
193  const char *comma = "", *tmp = "";
194  static char s[256];
195  char b[16];
196 
197  *s = '\0';
198 
199  if (sy->list != STYLE_LIST_OFF) {
200  if (sy->list == STYLE_LIST_ON)
201  tmp = "on";
202  else if (sy->list == STYLE_LIST_FOCUS)
203  tmp = "focus";
204  else if (sy->list == STYLE_LIST_LEFT_MARKER)
205  tmp = "left-marker";
206  else if (sy->list == STYLE_LIST_RIGHT_MARKER)
207  tmp = "right-marker";
208  off += xsnprintf(s + off, sizeof s - off, "%slist=%s", comma,
209  tmp);
210  comma = ",";
211  }
212  if (sy->range_type != STYLE_RANGE_NONE) {
213  if (sy->range_type == STYLE_RANGE_LEFT)
214  tmp = "left";
215  else if (sy->range_type == STYLE_RANGE_RIGHT)
216  tmp = "right";
217  else if (sy->range_type == STYLE_RANGE_WINDOW) {
218  snprintf(b, sizeof b, "window|%u", sy->range_argument);
219  tmp = b;
220  }
221  off += xsnprintf(s + off, sizeof s - off, "%srange=%s", comma,
222  tmp);
223  comma = ",";
224  }
225  if (sy->align != STYLE_ALIGN_DEFAULT) {
226  if (sy->align == STYLE_ALIGN_LEFT)
227  tmp = "left";
228  else if (sy->align == STYLE_ALIGN_CENTRE)
229  tmp = "centre";
230  else if (sy->align == STYLE_ALIGN_RIGHT)
231  tmp = "right";
232  else if (sy->align == STYLE_ALIGN_ABSOLUTE_CENTRE)
233  tmp = "absolute-centre";
234  off += xsnprintf(s + off, sizeof s - off, "%salign=%s", comma,
235  tmp);
236  comma = ",";
237  }
238  if (sy->default_type != STYLE_DEFAULT_BASE) {
239  if (sy->default_type == STYLE_DEFAULT_PUSH)
240  tmp = "push-default";
241  else if (sy->default_type == STYLE_DEFAULT_POP)
242  tmp = "pop-default";
243  off += xsnprintf(s + off, sizeof s - off, "%s%s", comma, tmp);
244  comma = ",";
245  }
246  if (sy->fill != 8) {
247  off += xsnprintf(s + off, sizeof s - off, "%sfill=%s", comma,
248  colour_tostring(sy->fill));
249  comma = ",";
250  }
251  if (gc->fg != 8) {
252  off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
253  colour_tostring(gc->fg));
254  comma = ",";
255  }
256  if (gc->bg != 8) {
257  off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
258  colour_tostring(gc->bg));
259  comma = ",";
260  }
261  if (gc->attr != 0) {
262  xsnprintf(s + off, sizeof s - off, "%s%s", comma,
264  comma = ",";
265  }
266 
267  if (*s == '\0')
268  return ("default");
269  return (s);
270 }
271 
272 /* Apply a style on top of the given style. */
273 void
274 style_add(struct grid_cell *gc, struct options *oo, const char *name,
275  struct format_tree *ft)
276 {
277  struct style *sy;
278  struct format_tree *ft0 = NULL;
279 
280  if (ft == NULL)
281  ft = ft0 = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
282 
283  sy = options_string_to_style(oo, name, ft);
284  if (sy == NULL)
285  sy = &style_default;
286  if (sy->gc.fg != 8)
287  gc->fg = sy->gc.fg;
288  if (sy->gc.bg != 8)
289  gc->bg = sy->gc.bg;
290  gc->attr |= sy->gc.attr;
291 
292  if (ft0 != NULL)
293  format_free(ft0);
294 }
295 
296 /* Apply a style on top of the default style. */
297 void
298 style_apply(struct grid_cell *gc, struct options *oo, const char *name,
299  struct format_tree *ft)
300 {
301  memcpy(gc, &grid_default_cell, sizeof *gc);
302  style_add(gc, oo, name, ft);
303 }
304 
305 /* Initialize style from cell. */
306 void
307 style_set(struct style *sy, const struct grid_cell *gc)
308 {
309  memcpy(sy, &style_default, sizeof *sy);
310  memcpy(&sy->gc, gc, sizeof sy->gc);
311 }
312 
313 /* Copy style. */
314 void
315 style_copy(struct style *dst, struct style *src)
316 {
317  memcpy(dst, src, sizeof *dst);
318 }
int attributes_fromstring(const char *str)
Definition: attributes.c:56
const char * attributes_tostring(int attr)
Definition: attributes.c:26
int colour_fromstring(const char *s)
Definition: colour.c:172
const char * colour_tostring(int c)
Definition: colour.c:110
struct format_tree * format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
Definition: format.c:3042
void format_free(struct format_tree *ft)
Definition: format.c:3066
const struct grid_cell grid_default_cell
Definition: grid.c:39
const char * name
Definition: layout-set.c:38
void log_debug(const char *msg,...)
Definition: log.c:130
struct style * options_string_to_style(struct options *oo, const char *name, struct format_tree *ft)
Definition: options.c:899
u_char flags
Definition: tmux.h:691
int bg
Definition: tmux.h:693
u_short attr
Definition: tmux.h:690
int fg
Definition: tmux.h:692
Definition: tmux.h:799
enum style_align align
Definition: tmux.h:804
u_int range_argument
Definition: tmux.h:808
struct grid_cell gc
Definition: tmux.h:800
int fill
Definition: tmux.h:803
int ignore
Definition: tmux.h:801
enum style_default_type default_type
Definition: tmux.h:810
enum style_range_type range_type
Definition: tmux.h:807
enum style_list list
Definition: tmux.h:805
void style_apply(struct grid_cell *gc, struct options *oo, const char *name, struct format_tree *ft)
Definition: style.c:298
void style_copy(struct style *dst, struct style *src)
Definition: style.c:315
const char * style_tostring(struct style *sy)
Definition: style.c:189
void style_add(struct grid_cell *gc, struct options *oo, const char *name, struct format_tree *ft)
Definition: style.c:274
void style_set(struct style *sy, const struct grid_cell *gc)
Definition: style.c:307
static struct style style_default
Definition: style.c:32
int style_parse(struct style *sy, const struct grid_cell *base, const char *in)
Definition: style.c:51
@ STYLE_LIST_ON
Definition: tmux.h:767
@ STYLE_LIST_FOCUS
Definition: tmux.h:768
@ STYLE_LIST_OFF
Definition: tmux.h:766
@ STYLE_LIST_RIGHT_MARKER
Definition: tmux.h:770
@ STYLE_LIST_LEFT_MARKER
Definition: tmux.h:769
#define FORMAT_NOJOBS
Definition: tmux.h:1971
@ STYLE_DEFAULT_BASE
Definition: tmux.h:793
@ STYLE_DEFAULT_POP
Definition: tmux.h:795
@ STYLE_DEFAULT_PUSH
Definition: tmux.h:794
@ STYLE_ALIGN_ABSOLUTE_CENTRE
Definition: tmux.h:761
@ STYLE_ALIGN_RIGHT
Definition: tmux.h:760
@ STYLE_ALIGN_CENTRE
Definition: tmux.h:759
@ STYLE_ALIGN_DEFAULT
Definition: tmux.h:757
@ STYLE_ALIGN_LEFT
Definition: tmux.h:758
@ STYLE_RANGE_NONE
Definition: tmux.h:775
@ STYLE_RANGE_LEFT
Definition: tmux.h:776
@ STYLE_RANGE_WINDOW
Definition: tmux.h:778
@ STYLE_RANGE_RIGHT
Definition: tmux.h:777
int xsnprintf(char *str, size_t len, const char *fmt,...)
Definition: xmalloc.c:135