NeoMutt  2025-09-05-7-geaa2bd
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 ExpandoRenderCallback *erc, 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 77 of file node_expando.c.

78{
79 struct ExpandoNode *node = node_new();
80
81 node->type = ENT_EXPANDO;
82 node->did = did;
83 node->uid = uid;
85
86 node->format = fmt;
87
90
91 return node;
92}
int node_expando_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render an Expando Node - Implements ExpandoNode::render() -.
Definition: node_expando.c:403
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:62
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
Definition: node_expando.c:48
Basic Expando Node.
Definition: node.h:67
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:70
void * ndata
Private node data.
Definition: node.h:77
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:92
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 99 of file node_expando.c.

100{
101 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
102 return;
103
104 struct NodeExpandoPrivate *priv = node->ndata;
105
106 priv->color = cid;
107}
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 114 of file node_expando.c.

115{
116 if (!node || (node->type != ENT_EXPANDO) || !node->ndata)
117 return;
118
119 struct NodeExpandoPrivate *priv = node->ndata;
120
121 priv->has_tree = has_tree;
122}
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 135 of file node_expando.c.

137{
138 if (!str || !parsed_until || !err)
139 return NULL;
140
141 const char *start = str;
142
143 struct ExpandoFormat *fmt = MUTT_MEM_CALLOC(1, struct ExpandoFormat);
144
145 fmt->leader = ' ';
147 fmt->min_cols = 0;
148 fmt->max_cols = -1;
149
150 if (*str == '-')
151 {
153 str++;
154 }
155 else if (*str == '=')
156 {
158 str++;
159 }
160
161 if (*str == '0')
162 {
163 // Ignore '0' with left-justification
164 if (fmt->justification != JUSTIFY_LEFT)
165 fmt->leader = '0';
166 str++;
167 }
168
169 // Parse the width (min_cols)
170 if (mutt_isdigit(*str))
171 {
172 unsigned short number = 0;
173 const char *end_ptr = mutt_str_atous(str, &number);
174
175 if (!end_ptr || (number == USHRT_MAX))
176 {
177 err->position = str;
178 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
179 FREE(&fmt);
180 return NULL;
181 }
182
183 fmt->min_cols = number;
184 str = end_ptr;
185 }
186
187 // Parse the precision (max_cols)
188 if (*str == '.')
189 {
190 str++;
191
192 unsigned short number = 1;
193
194 if (mutt_isdigit(*str))
195 {
196 const char *end_ptr = mutt_str_atous(str, &number);
197
198 if (!end_ptr || (number == USHRT_MAX))
199 {
200 err->position = str;
201 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
202 FREE(&fmt);
203 return NULL;
204 }
205
206 str = end_ptr;
207 }
208 else
209 {
210 number = 0;
211 }
212
213 fmt->leader = (number == 0) ? ' ' : '0';
214 fmt->max_cols = number;
215 }
216
217 // A modifier of '_' before the letter means force lower case
218 if (*str == '_')
219 {
220 fmt->lower = true;
221 str++;
222 }
223
224 if (str == start) // Failed to parse anything
225 FREE(&fmt);
226
227 if (fmt && (fmt->min_cols == 0) && (fmt->max_cols == -1) && !fmt->lower)
228 FREE(&fmt);
229
230 *parsed_until = str;
231 return fmt;
232}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:270
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition: ctype.c:65
@ 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:62
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:47
#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 244 of file node_expando.c.

248{
249 if (!str || !defs)
250 return NULL;
251
252 const struct ExpandoDefinition *def = defs;
253 for (; def && def->short_name; def++)
254 {
255 size_t len = mutt_str_len(def->short_name);
256
257 if (mutt_strn_equal(def->short_name, str, len))
258 {
259 if (def->parse && !(flags & EP_NO_CUSTOM_PARSE))
260 {
261 return def->parse(str, fmt, def->did, def->uid, flags, parsed_until, err);
262 }
263 else
264 {
265 *parsed_until = str + len;
266 return node_expando_new(fmt, def->did, def->uid);
267 }
268 }
269 }
270
271 return NULL;
272}
#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:426
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:497
struct ExpandoNode * node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
Definition: node_expando.c:77
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 283 of file node_expando.c.

286{
287 ASSERT(str[0] == '%');
288 str++;
289
290 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
291 if (err->position)
292 {
293 FREE(&fmt);
294 return NULL;
295 }
296
297 str = *parsed_until;
298
299 struct ExpandoNode *node = parse_short_name(str, defs, flags, fmt, parsed_until, err);
300 if (node)
301 return node;
302
303 err->position = *parsed_until;
304 // L10N: e.g. "Unknown expando: %Q"
305 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.1s"), *parsed_until);
306 FREE(&fmt);
307 return NULL;
308}
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:244
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Definition: node_expando.c:135
#define ASSERT(COND)
Definition: signal2.h:60
+ 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 348 of file node_expando.c.

354{
355 str++; // skip opening char
356
357 const char *expando_end = skip_until_ch(str, terminator);
358
359 if (*expando_end != terminator)
360 {
361 err->position = expando_end;
362 snprintf(err->message, sizeof(err->message),
363 // L10N: Expando is missing a terminator character
364 // e.g. "%[..." is missing the final ']'
365 _("Expando is missing terminator: '%c'"), terminator);
366 return NULL;
367 }
368
369 *parsed_until = expando_end + 1;
370
371 struct ExpandoNode *node = node_expando_new(fmt, did, uid);
372
373 struct Buffer *buf = buf_pool_get();
374 for (; str < expando_end; str++)
375 {
376 if (str[0] == '\\')
377 continue;
378 buf_addch(buf, str[0]);
379 }
380
381 node->text = buf_strdup(buf);
382 buf_pool_release(&buf);
383
384 return node;
385}
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:316
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
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: