NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
parse_color.c File Reference

Parse colours. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "parse_color.h"
#include "parse/lib.h"
#include "attr.h"
#include "color.h"
#include "debug.h"
+ Include dependency graph for parse_color.c:

Go to the source code of this file.

Functions

int parse_color_prefix (const char *s, enum ColorPrefix *prefix)
 Parse a colour prefix, e.g.
 
enum CommandResult parse_color_namedcolor (const char *s, struct ColorElement *elem, struct Buffer *err)
 Parse a named colour, e.g.
 
enum CommandResult parse_color_colornnn (const char *s, struct ColorElement *elem, struct Buffer *err)
 Parse a colorNNN, e.g.
 
enum CommandResult parse_color_rrggbb (const char *s, struct ColorElement *elem, struct Buffer *err)
 Parse an RGB colour, e.g.
 
enum CommandResult parse_color_name (const char *s, struct ColorElement *elem, struct Buffer *err)
 Parse a colour name.
 
enum CommandResult parse_color_pair (struct Buffer *buf, struct Buffer *s, struct AttrColor *ac, struct Buffer *err)
 Parse a pair of colours - Implements parser_callback_t -.
 
enum CommandResult parse_attr_spec (struct Buffer *buf, struct Buffer *s, struct AttrColor *ac, struct Buffer *err)
 Parse an attribute description - Implements parser_callback_t -.
 

Variables

const struct Mapping ColorNames []
 Mapping between a colour name and an ncurses colour.
 
static struct Mapping AttributeNames []
 Mapping of attribute names to their IDs.
 

Detailed Description

Parse colours.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file parse_color.c.

Function Documentation

◆ parse_color_prefix()

int parse_color_prefix ( const char *  s,
enum ColorPrefix prefix 
)

Parse a colour prefix, e.g.

"bright"

Parameters
[in]sString to parse
[out]prefixparsed prefix, see ColorPrefix
Return values
numLength of the matched prefix
0No prefix matched

If prefixes should be parsed, but their value is irrelevant, NULL can be passed as 'prefix'.

Definition at line 82 of file parse_color.c.

83{
84 if (!s || !prefix)
85 return 0;
86
87 int clen = 0;
88
89 if ((clen = mutt_istr_startswith(s, "bright")))
90 {
91 color_debug(LL_DEBUG5, "bright\n");
92 if (prefix)
93 *prefix = COLOR_PREFIX_BRIGHT;
94 }
95 else if ((clen = mutt_istr_startswith(s, "alert")))
96 {
97 color_debug(LL_DEBUG5, "alert\n");
98 if (prefix)
99 *prefix = COLOR_PREFIX_ALERT;
100 }
101 else if ((clen = mutt_istr_startswith(s, "light")))
102 {
103 color_debug(LL_DEBUG5, "light\n");
104 if (prefix)
105 *prefix = COLOR_PREFIX_LIGHT;
106 }
107
108 return clen;
109}
@ COLOR_PREFIX_ALERT
"alert" colour prefix
Definition: attr.h:47
@ COLOR_PREFIX_LIGHT
"light" colour prefix
Definition: attr.h:49
@ COLOR_PREFIX_BRIGHT
"bright" colour prefix
Definition: attr.h:48
static int color_debug(enum LogLevel level, const char *format,...)
Definition: debug.h:52
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_namedcolor()

enum CommandResult parse_color_namedcolor ( const char *  s,
struct ColorElement elem,
struct Buffer err 
)

Parse a named colour, e.g.

"brightred"

Parameters
[in]sString to parse
[out]elemColour element to update
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers

Definition at line 119 of file parse_color.c.

121{
122 if (!s || !elem)
123 return MUTT_CMD_ERROR;
124
125 // COLOR_DEFAULT (-1) interferes with mutt_map_get_value()
126 if (mutt_str_equal(s, "default"))
127 {
128 elem->color = COLOR_DEFAULT;
129 elem->type = CT_SIMPLE;
131 return MUTT_CMD_SUCCESS;
132 }
133
134 enum ColorPrefix prefix = COLOR_PREFIX_NONE;
135 s += parse_color_prefix(s, &prefix);
136
137 int color = mutt_map_get_value(s, ColorNames);
138 if (color == -1)
139 return MUTT_CMD_WARNING;
140
141 elem->color = color;
142 elem->type = CT_SIMPLE;
143 elem->prefix = prefix;
144
145 const char *name = mutt_map_get_name(elem->color, ColorNames);
146 if (name)
147 color_debug(LL_DEBUG5, "color: %s\n", name);
148
149 return MUTT_CMD_SUCCESS;
150}
ColorPrefix
Constants for colour prefixes of named colours.
Definition: attr.h:45
@ COLOR_PREFIX_NONE
no prefix
Definition: attr.h:46
@ CT_SIMPLE
Simple colour, e.g. "Red".
Definition: attr.h:36
#define COLOR_DEFAULT
Definition: color.h:96
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
int parse_color_prefix(const char *s, enum ColorPrefix *prefix)
Parse a colour prefix, e.g.
Definition: parse_color.c:82
const struct Mapping ColorNames[]
Mapping between a colour name and an ncurses colour.
Definition: parse_color.c:41
enum ColorType type
Type of Colour.
Definition: attr.h:58
color_t color
Colour.
Definition: attr.h:57
enum ColorPrefix prefix
Optional Colour Modifier.
Definition: attr.h:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_colornnn()

enum CommandResult parse_color_colornnn ( const char *  s,
struct ColorElement elem,
struct Buffer err 
)

Parse a colorNNN, e.g.

"color123"

Parameters
[in]sString to parse
[out]elemColour element to update
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers
MUTT_CMD_ERRORError, colour could not be parsed

On MUTT_CMD_ERROR, an error message will be written to err.

Definition at line 163 of file parse_color.c.

165{
166 if (!s || !elem)
167 return MUTT_CMD_ERROR;
168
169 /* prefixes bright, alert, light are only allowed for named colours and
170 * colorNNN for backwards compatibility. */
171 enum ColorPrefix prefix = COLOR_PREFIX_NONE;
172 s += parse_color_prefix(s, &prefix);
173
174 int clen = 0;
175 /* allow aliases for xterm color resources */
176 if ((clen = mutt_istr_startswith(s, "color")) == 0)
177 return MUTT_CMD_WARNING;
178
179 s += clen;
180 char *eptr = NULL;
181
182 unsigned long color = strtoul(s, &eptr, 10);
183 /* There are only 256 xterm colors. Do not confuse with COLORS which is
184 * the number of colours the terminal supports (usually one of 16, 256,
185 * 16777216 (=24bit)). */
186 if ((*s == '\0') || (*eptr != '\0') || (color >= 256))
187 {
188 buf_printf(err, _("%s: color not supported by term"), s);
189 return MUTT_CMD_ERROR;
190 }
191
192 elem->color = color;
193 elem->type = CT_PALETTE;
194 elem->prefix = prefix;
195
196 color_debug(LL_DEBUG5, "colorNNN %d\n", elem->color);
197 return MUTT_CMD_SUCCESS;
198}
@ CT_PALETTE
Palette colour, e.g. "color207".
Definition: attr.h:37
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
#define _(a)
Definition: message.h:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_rrggbb()

enum CommandResult parse_color_rrggbb ( const char *  s,
struct ColorElement elem,
struct Buffer err 
)

Parse an RGB colour, e.g.

"#12FE45"

Parameters
[in]sString to parse
[out]elemColour element to update
[out]errBuffer for error messages
Return values
MUTT_CMD_SUCCESSColour parsed successfully
MUTT_CMD_WARNINGUnknown colour, try other parsers
MUTT_CMD_ERRORError, colour could not be parsed

On MUTT_CMD_ERROR, an error message will be written to err.

Definition at line 211 of file parse_color.c.

213{
214 if (!s || !elem)
215 return MUTT_CMD_ERROR;
216
217 /* parse #RRGGBB colours */
218 if (s[0] != '#')
219 return MUTT_CMD_WARNING;
220
221 s++;
222 char *eptr = NULL;
223 unsigned long color = strtoul(s, &eptr, 16);
224
225 if ((*s == '\0') || !eptr || (*eptr != '\0') || ((eptr - s) != 6))
226 {
227 buf_printf(err, _("%s: color not supported by term"), s);
228 return MUTT_CMD_ERROR;
229 }
230
231 elem->color = color;
232 elem->type = CT_RGB;
234
235 color_debug(LL_DEBUG5, "#RRGGBB: %ld\n", color);
236 return MUTT_CMD_SUCCESS;
237}
@ CT_RGB
True colour, e.g. "#11AAFF".
Definition: attr.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_color_name()

enum CommandResult parse_color_name ( const char *  s,
struct ColorElement elem,
struct Buffer err 
)

Parse a colour name.

Parameters
[in]sString to parse
[out]elemColour element to update
[out]errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Parse a colour name, such as "red", "brightgreen", "color123", "#12FE45"

Definition at line 248 of file parse_color.c.

250{
251 color_debug(LL_DEBUG5, "Parsing color name: %s\n", s);
252
253 /* Try the different colour syntaxes. A return value of MUTT_CMD_WARNING
254 * means, we should try the next syntax. */
255 enum CommandResult cr;
256
257 /* #RRGGBB */
258 cr = parse_color_rrggbb(s, elem, err);
259 if (cr != MUTT_CMD_WARNING)
260 return cr;
261
262 /* color123 */
263 cr = parse_color_colornnn(s, elem, err);
264 if (cr != MUTT_CMD_WARNING)
265 return cr;
266
267 /* named color, e.g. "brightred" */
268 cr = parse_color_namedcolor(s, elem, err);
269 if (cr != MUTT_CMD_WARNING)
270 return cr;
271
272 buf_printf(err, _("%s: no such color"), s);
273 return MUTT_CMD_WARNING;
274}
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
enum CommandResult parse_color_colornnn(const char *s, struct ColorElement *elem, struct Buffer *err)
Parse a colorNNN, e.g.
Definition: parse_color.c:163
enum CommandResult parse_color_rrggbb(const char *s, struct ColorElement *elem, struct Buffer *err)
Parse an RGB colour, e.g.
Definition: parse_color.c:211
enum CommandResult parse_color_namedcolor(const char *s, struct ColorElement *elem, struct Buffer *err)
Parse a named colour, e.g.
Definition: parse_color.c:119
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ ColorNames

const struct Mapping ColorNames[]
Initial value:
= {
{ "black", COLOR_BLACK },
{ "blue", COLOR_BLUE },
{ "cyan", COLOR_CYAN },
{ "green", COLOR_GREEN },
{ "magenta", COLOR_MAGENTA },
{ "red", COLOR_RED },
{ "white", COLOR_WHITE },
{ "yellow", COLOR_YELLOW },
{ "default", COLOR_DEFAULT },
{ 0, 0 },
}

Mapping between a colour name and an ncurses colour.

Definition at line 41 of file parse_color.c.

◆ AttributeNames

struct Mapping AttributeNames[]
static
Initial value:
= {
{ "bold", A_BOLD },
{ "italic", A_ITALIC },
{ "none", A_NORMAL },
{ "normal", A_NORMAL },
{ "reverse", A_REVERSE },
{ "standout", A_STANDOUT },
{ "underline", A_UNDERLINE },
{ NULL, 0 },
}
#define A_ITALIC
Definition: mutt_curses.h:49

Mapping of attribute names to their IDs.

Definition at line 59 of file parse_color.c.