NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_expando.h File Reference

Expando Node for an Expando. More...

#include <stdbool.h>
#include "definition.h"
#include "render.h"
+ Include dependency graph for node_expando.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  NodeExpandoPrivate
 Private data for an Expando -. More...
 

Functions

struct ExpandoNodenode_expando_new (struct ExpandoFormat *fmt, int did, int uid)
 Create a new Expando ExpandoNode.
 
void node_expando_set_color (const struct ExpandoNode *node, int cid)
 Set the colour for an Expando.
 
void node_expando_set_has_tree (const struct ExpandoNode *node, bool has_tree)
 Set the has_tree flag for an Expando.
 
struct ExpandoFormatparse_format (const char *str, const char **parsed_until, struct ExpandoParseError *err)
 Parse a format string.
 
struct ExpandoNodeparse_short_name (const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
 Create an expando by its short name.
 
struct ExpandoNodenode_expando_parse (const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
 Parse an Expando format string.
 
int node_expando_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render an Expando Node - Implements ExpandoNode::render() -.
 
struct ExpandoNodenode_expando_parse_enclosure (const char *str, int did, int uid, char terminator, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
 Parse an enclosed Expando.
 

Detailed Description

Expando Node for an Expando.

Authors
  • Tóth János
  • 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 node_expando.h.

Function Documentation

◆ node_expando_new()

struct ExpandoNode * node_expando_new ( struct ExpandoFormat fmt,
int  did,
int  uid 
)

Create a new Expando ExpandoNode.

Parameters
fmtFormatting data
didDomain ID
uidUnique ID
Return values
ptrNew Expando ExpandoNode

Definition at line 78 of file node_expando.c.

79{
80 struct ExpandoNode *node = node_new();
81
82 node->type = ENT_EXPANDO;
83 node->did = did;
84 node->uid = uid;
86
87 node->format = fmt;
88
91
92 return node;
93}
int node_expando_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render an Expando Node - Implements ExpandoNode::render() -.
Definition: node_expando.c:404
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition: node.h:39
void node_expando_private_free(void **ptr)
Free Expando private data - Implements ExpandoNode::ndata_free()
Definition: node_expando.c:63
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
Definition: node_expando.c:49
Basic Expando Node.
Definition: node.h:67
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:70
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
void * ndata
Private node data.
Definition: node.h:77
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
int did
Domain ID, e.g. ED_EMAIL.
Definition: node.h:69
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_set_color()

void node_expando_set_color ( const struct ExpandoNode node,
int  cid 
)

Set the colour for an Expando.

Parameters
nodeNode to alter
cidColour

Definition at line 100 of file node_expando.c.

101{
102 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
103 return;
104
105 struct NodeExpandoPrivate *priv = node->ndata;
106
107 priv->color = cid;
108}
Private data for an Expando -.
Definition: node_expando.h:40
int color
ColorId to use.
Definition: node_expando.h:41
+ Here is the caller graph for this function:

◆ node_expando_set_has_tree()

void node_expando_set_has_tree ( const struct ExpandoNode node,
bool  has_tree 
)

Set the has_tree flag for an Expando.

Parameters
nodeNode to alter
has_treeFlag to set

Definition at line 115 of file node_expando.c.

116{
117 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
118 return;
119
120 struct NodeExpandoPrivate *priv = node->ndata;
121
122 priv->has_tree = has_tree;
123}
bool has_tree
Contains tree characters, used in $index_format's s.
Definition: node_expando.h:42
+ Here is the caller graph for this function:

◆ parse_format()

struct ExpandoFormat * parse_format ( const char *  str,
const char **  parsed_until,
struct ExpandoParseError err 
)

Parse a format string.

Parameters
[in]strString to parse
[out]parsed_untilFirst character after the parsed string
[out]errBuffer for errors
Return values
ptrNew ExpandoFormat object

Parse a printf()-style format, e.g. '-15.20x'

Note
A trailing _ (underscore) means lowercase the string

Definition at line 136 of file node_expando.c.

138{
139 if (!str || !parsed_until || !err)
140 return NULL;
141
142 const char *start = str;
143
144 struct ExpandoFormat *fmt = MUTT_MEM_CALLOC(1, struct ExpandoFormat);
145
146 fmt->leader = ' ';
148 fmt->min_cols = 0;
149 fmt->max_cols = -1;
150
151 if (*str == '-')
152 {
154 str++;
155 }
156 else if (*str == '=')
157 {
159 str++;
160 }
161
162 if (*str == '0')
163 {
164 // Ignore '0' with left-justification
165 if (fmt->justification != JUSTIFY_LEFT)
166 fmt->leader = '0';
167 str++;
168 }
169
170 // Parse the width (min_cols)
171 if (isdigit(*str))
172 {
173 unsigned short number = 0;
174 const char *end_ptr = mutt_str_atous(str, &number);
175
176 if (!end_ptr || (number == USHRT_MAX))
177 {
178 err->position = str;
179 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
180 FREE(&fmt);
181 return NULL;
182 }
183
184 fmt->min_cols = number;
185 str = end_ptr;
186 }
187
188 // Parse the precision (max_cols)
189 if (*str == '.')
190 {
191 str++;
192
193 unsigned short number = 1;
194
195 if (isdigit(*str))
196 {
197 const char *end_ptr = mutt_str_atous(str, &number);
198
199 if (!end_ptr || (number == USHRT_MAX))
200 {
201 err->position = str;
202 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
203 FREE(&fmt);
204 return NULL;
205 }
206
207 str = end_ptr;
208 }
209 else
210 {
211 number = 0;
212 }
213
214 fmt->leader = (number == 0) ? ' ' : '0';
215 fmt->max_cols = number;
216 }
217
218 // A modifier of '_' before the letter means force lower case
219 if (*str == '_')
220 {
221 fmt->lower = true;
222 str++;
223 }
224
225 if (str == start) // Failed to parse anything
226 FREE(&fmt);
227
228 if (fmt && (fmt->min_cols == 0) && (fmt->max_cols == -1) && !fmt->lower)
229 FREE(&fmt);
230
231 *parsed_until = str;
232 return fmt;
233}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:270
@ JUSTIFY_RIGHT
Right justify the text.
Definition: format.h:36
@ JUSTIFY_LEFT
Left justify the text.
Definition: format.h:34
@ JUSTIFY_CENTER
Centre the text.
Definition: format.h:35
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#define _(a)
Definition: message.h:28
Formatting information for an Expando.
Definition: node.h:53
char leader
Leader character, 0 or space.
Definition: node.h:57
enum FormatJustify justification
Justification: left, centre, right.
Definition: node.h:56
int min_cols
Minimum number of screen columns.
Definition: node.h:54
int max_cols
Maximum number of screen columns.
Definition: node.h:55
bool lower
Display in lower case.
Definition: node.h:58
char message[1024]
Error message.
Definition: parse.h:38
const char * position
Position of error in original string.
Definition: parse.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_short_name()

struct ExpandoNode * parse_short_name ( const char *  str,
const struct ExpandoDefinition defs,
ExpandoParserFlags  flags,
struct ExpandoFormat fmt,
const char **  parsed_until,
struct ExpandoParseError err 
)

Create an expando by its short name.

Parameters
[in]strString to parse
[in]defsExpando definitions
[in]flagsFlag for conditional expandos
[in]fmtFormatting info
[out]parsed_untilFirst character after parsed string
[out]errBuffer for errors
Return values
ptrNew ExpandoNode

Definition at line 245 of file node_expando.c.

249{
250 if (!str || !defs)
251 return NULL;
252
253 const struct ExpandoDefinition *def = defs;
254 for (; def && def->short_name; def++)
255 {
256 size_t len = mutt_str_len(def->short_name);
257
258 if (mutt_strn_equal(def->short_name, str, len))
259 {
260 if (def->parse && !(flags & EP_NO_CUSTOM_PARSE))
261 {
262 return def->parse(str, fmt, def->did, def->uid, flags, parsed_until, err);
263 }
264 else
265 {
266 *parsed_until = str + len;
267 return node_expando_new(fmt, def->did, def->uid);
268 }
269 }
270 }
271
272 return NULL;
273}
#define EP_NO_CUSTOM_PARSE
Don't use the custom parser.
Definition: definition.h:36
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:425
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
struct ExpandoNode * node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
Definition: node_expando.c:78
Definition of a format string.
Definition: definition.h:44
short uid
Unique ID in domain.
Definition: definition.h:48
struct ExpandoNode *(* parse)(const char *str, struct ExpandoFormat *fmt, int did, int uid, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Definition: definition.h:63
short did
Domain ID.
Definition: definition.h:47
const char * short_name
Short Expando name, e.g. "n".
Definition: definition.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_parse()

struct ExpandoNode * node_expando_parse ( const char *  str,
const struct ExpandoDefinition defs,
ExpandoParserFlags  flags,
const char **  parsed_until,
struct ExpandoParseError err 
)

Parse an Expando format string.

Parameters
[in]strString to parse
[in]defsExpando definitions
[in]flagsFlag for conditional expandos
[out]parsed_untilFirst character after parsed string
[out]errBuffer for errors
Return values
ptrNew ExpandoNode

Definition at line 284 of file node_expando.c.

287{
288 ASSERT(str[0] == '%');
289 str++;
290
291 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
292 if (err->position)
293 {
294 FREE(&fmt);
295 return NULL;
296 }
297
298 str = *parsed_until;
299
300 struct ExpandoNode *node = parse_short_name(str, defs, flags, fmt, parsed_until, err);
301 if (node)
302 return node;
303
304 err->position = *parsed_until;
305 // L10N: e.g. "Unknown expando: %Q"
306 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.1s"), *parsed_until);
307 FREE(&fmt);
308 return NULL;
309}
struct ExpandoNode * parse_short_name(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, struct ExpandoFormat *fmt, const char **parsed_until, struct ExpandoParseError *err)
Create an expando by its short name.
Definition: node_expando.c:245
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Definition: node_expando.c:136
#define ASSERT(COND)
Definition: signal2.h:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_parse_enclosure()

struct ExpandoNode * node_expando_parse_enclosure ( const char *  str,
int  did,
int  uid,
char  terminator,
struct ExpandoFormat fmt,
const char **  parsed_until,
struct ExpandoParseError err 
)

Parse an enclosed Expando.

Parameters
[in]strString to parse
[in]didDomain ID
[in]uidUnique ID
[in]terminatorTerminating character
[in]fmtFormatting info
[out]parsed_untilFirst character after the parsed string
[out]errBuffer for errors
Return values
ptrNew ExpandoNode

Definition at line 349 of file node_expando.c.

355{
356 str++; // skip opening char
357
358 const char *expando_end = skip_until_ch(str, terminator);
359
360 if (*expando_end != terminator)
361 {
362 err->position = expando_end;
363 snprintf(err->message, sizeof(err->message),
364 // L10N: Expando is missing a terminator character
365 // e.g. "%[..." is missing the final ']'
366 _("Expando is missing terminator: '%c'"), terminator);
367 return NULL;
368 }
369
370 *parsed_until = expando_end + 1;
371
372 struct ExpandoNode *node = node_expando_new(fmt, did, uid);
373
374 struct Buffer *buf = buf_pool_get();
375 for (; str < expando_end; str++)
376 {
377 if (str[0] == '\\')
378 continue;
379 buf_addch(buf, str[0]);
380 }
381
382 node->text = buf_strdup(buf);
383 buf_pool_release(&buf);
384
385 return node;
386}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
const char * skip_until_ch(const char *str, char terminator)
Search a string for a terminator character.
Definition: node_expando.c:317
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:36
const char * text
Node-specific text.
Definition: node.h:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function: