NeoMutt  2025-12-11-177-g48e272
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_expando.c
Go to the documentation of this file.
1
24
30
31#include "config.h"
32#include <limits.h>
33#include <stdio.h>
34#include "mutt/lib.h"
35#include "gui/lib.h"
36#include "node_expando.h"
37#include "color/lib.h"
38#include "definition.h"
39#include "format.h"
40#include "helpers.h"
41#include "node.h"
42#include "parse.h"
43#include "render.h"
44
50{
52
53 // NOTE(g0mb4): Expando definition should contain this
54 priv->color = -1;
55
56 return priv;
57}
58
64{
65 if (!ptr || !*ptr)
66 return;
67
68 FREE(ptr);
69}
70
78struct ExpandoNode *node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
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}
94
100void node_expando_set_color(const struct ExpandoNode *node, int cid)
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}
109
115void node_expando_set_has_tree(const struct ExpandoNode *node, bool has_tree)
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}
124
136struct ExpandoFormat *parse_format(const char *str, const char **parsed_until,
137 struct ExpandoParseError *err)
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 (mutt_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 (mutt_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}
234
245struct ExpandoNode *parse_short_name(const char *str, const struct ExpandoDefinition *defs,
246 ExpandoParserFlags flags,
247 struct ExpandoFormat *fmt, const char **parsed_until,
248 struct ExpandoParseError *err)
249{
250 if (!str || !defs)
251 return NULL;
252
253 const struct ExpandoDefinition *def = defs;
254 for (; def && (def->short_name || def->long_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)
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}
274
285struct ExpandoNode *parse_long_name(const char *str, const struct ExpandoDefinition *defs,
286 ExpandoParserFlags flags,
287 struct ExpandoFormat *fmt, const char **parsed_until,
288 struct ExpandoParseError *err)
289{
290 if (!str || !defs)
291 return NULL;
292
293 const struct ExpandoDefinition *def = defs;
294 for (; def && (def->short_name || def->long_name); def++)
295 {
296 if (!def->long_name)
297 continue;
298
299 size_t len = mutt_str_len(def->long_name);
300
301 if (mutt_strn_equal(def->long_name, str, len))
302 {
303 *parsed_until = str + len;
304 if (def->parse)
305 {
306 struct ExpandoNode *node = def->parse(str, fmt, def->did, def->uid,
307 flags, parsed_until, err);
308 if (node || (err->message[0] != '\0'))
309 return node;
310 }
311 else
312 {
313 if (str[len] != '}') // Not an exact match
314 continue;
315
316 return node_expando_new(fmt, def->did, def->uid);
317 }
318 }
319 }
320
321 return NULL;
322}
323
333struct ExpandoNode *node_expando_parse(const char *str, const struct ExpandoDefinition *defs,
334 ExpandoParserFlags flags, const char **parsed_until,
335 struct ExpandoParseError *err)
336{
337 ASSERT(str[0] == '%');
338 str++;
339
340 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
341 if (err->position)
342 {
343 FREE(&fmt);
344 return NULL;
345 }
346
347 str = *parsed_until;
348
349 struct ExpandoNode *node = parse_short_name(str, defs, flags, fmt, parsed_until, err);
350 if (node)
351 return node;
352
353 if (!err->position)
354 {
355 err->position = *parsed_until;
356 // L10N: e.g. "Unknown expando: %Q"
357 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.1s"), *parsed_until);
358 }
359
360 FREE(&fmt);
361 return NULL;
362}
363
373struct ExpandoNode *node_expando_parse_name(const char *str,
374 const struct ExpandoDefinition *defs,
375 ExpandoParserFlags flags, const char **parsed_until,
376 struct ExpandoParseError *err)
377{
378 ASSERT(str[0] == '%');
379 str++;
380
381 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
382 if (err->position)
383 goto fail;
384
385 str = *parsed_until;
386
387 if (str[0] != '{')
388 goto fail;
389
390 str++;
391
392 struct ExpandoNode *node = parse_long_name(str, defs, flags, fmt, parsed_until, err);
393 if (!node)
394 goto fail;
395
396 fmt = NULL; // owned by the node, now
397
398 if ((*parsed_until)[0] == '}')
399 {
400 (*parsed_until)++;
401 return node;
402 }
403
404 node_free(&node);
405
406fail:
407 FREE(&fmt);
408 return NULL;
409}
410
417const char *skip_until_ch(const char *str, char terminator)
418{
419 while (str[0] != '\0')
420 {
421 if (*str == terminator)
422 break;
423
424 if (str[0] == '\\') // Literal character
425 {
426 if (str[1] == '\0')
427 return str + 1;
428
429 str++;
430 }
431
432 str++;
433 }
434
435 return str;
436}
437
449struct ExpandoNode *node_expando_parse_enclosure(const char *str, int did,
450 int uid, char terminator,
451 struct ExpandoFormat *fmt,
452 const char **parsed_until,
453 struct ExpandoParseError *err)
454
455{
456 str++; // skip opening char
457
458 const char *expando_end = skip_until_ch(str, terminator);
459
460 if (*expando_end != terminator)
461 {
462 err->position = expando_end;
463 snprintf(err->message, sizeof(err->message),
464 // L10N: Expando is missing a terminator character
465 // e.g. "%[..." is missing the final ']'
466 _("Expando is missing terminator: '%c'"), terminator);
467 return NULL;
468 }
469
470 *parsed_until = expando_end + 1;
471
472 struct ExpandoNode *node = node_expando_new(fmt, did, uid);
473
474 struct Buffer *buf = buf_pool_get();
475 for (; str < expando_end; str++)
476 {
477 if (str[0] == '\\')
478 continue;
479 buf_addch(buf, str[0]);
480 }
481
482 node->text = buf_strdup(buf);
483 buf_pool_release(&buf);
484
485 return node;
486}
487
493void add_color(struct Buffer *buf, enum ColorId cid)
494{
495 ASSERT(cid < MT_COLOR_MAX);
496
498 buf_addch(buf, cid);
499}
500
504int node_expando_render(const struct ExpandoNode *node,
505 const struct ExpandoRenderCallback *erc, struct Buffer *buf,
506 int max_cols, void *data, MuttFormatFlags flags)
507{
508 ASSERT(node->type == ENT_EXPANDO);
509
510 struct Buffer *buf_expando = buf_pool_get();
511 struct Buffer *buf_format = buf_pool_get();
512
513 const struct ExpandoFormat *fmt = node->format;
514 const struct NodeExpandoPrivate *priv = node->ndata;
515
516 // ---------------------------------------------------------------------------
517 // Numbers and strings get treated slightly differently. We prefer strings.
518 // This allows dates to be stored as 1729850182, but displayed as "2024-10-25".
519
520 const struct ExpandoRenderCallback *erc_match = find_get_string(erc, node->did, node->uid);
521 if (erc_match)
522 {
523 erc_match->get_string(node, data, flags, buf_expando);
524
525 if (fmt && fmt->lower)
526 buf_lower_special(buf_expando);
527 }
528 else
529 {
530 erc_match = find_get_number(erc, node->did, node->uid);
531 ASSERT(erc_match && "Unknown UID");
532
533 const long num = erc_match->get_number(node, data, flags);
534
535 int precision = 1;
536
537 if (fmt)
538 {
539 precision = fmt->max_cols;
540 if ((precision < 0) && (fmt->leader == '0'))
541 precision = fmt->min_cols;
542 }
543
544 if (num < 0)
545 precision--; // Allow space for the '-' sign
546
547 buf_printf(buf_expando, "%.*ld", precision, num);
548 }
549
550 // ---------------------------------------------------------------------------
551
552 int max = max_cols;
553 int min = 0;
554
555 if (fmt)
556 {
557 min = fmt->min_cols;
558 if (fmt->max_cols > 0)
559 max = MIN(max_cols, fmt->max_cols);
560 }
561
562 const enum FormatJustify just = fmt ? fmt->justification : JUSTIFY_LEFT;
563
564 int total_cols = format_string(buf_format, min, max, just, ' ', buf_string(buf_expando),
565 buf_len(buf_expando), priv->has_tree);
566
567 if (!buf_is_empty(buf_format))
568 {
569 if (priv->color > -1)
570 add_color(buf, priv->color);
571
572 buf_addstr(buf, buf_string(buf_format));
573
574 if (priv->color > -1)
576 }
577
578 buf_pool_release(&buf_format);
579 buf_pool_release(&buf_expando);
580
581 return total_cols;
582}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition atoi.c:269
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
ColorId
List of all coloured objects.
Definition color.h:36
@ MT_COLOR_MAX
Definition color.h:98
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:87
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition ctype.c:66
Define an Expando format string.
uint8_t ExpandoParserFlags
Flags for expando_parse(), e.g. EP_CONDITIONAL.
Definition definition.h:33
const struct ExpandoRenderCallback * find_get_string(const struct ExpandoRenderCallback *erc, int did, int uid)
Find a get_string() callback function.
Definition helpers.c:69
void buf_lower_special(struct Buffer *buf)
Convert to lowercase, excluding special characters.
Definition helpers.c:92
const struct ExpandoRenderCallback * find_get_number(const struct ExpandoRenderCallback *erc, int did, int uid)
Find a get_number() callback function.
Definition helpers.c:45
Shared code.
Expando Parsing.
int format_string(struct Buffer *buf, int min_cols, int max_cols, enum FormatJustify justify, char pad_char, const char *str, size_t n, bool arboreal)
Format a string, like snprintf()
Definition format.c:108
Simple string formatting.
FormatJustify
Alignment for format_string()
Definition format.h:33
@ 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
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() -.
Convenience wrapper for the gui headers.
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition thread.h:72
#define FREE(x)
Definition memory.h:63
#define MIN(a, b)
Definition memory.h:38
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:48
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
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:429
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition node.c:39
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition node.c:48
Basic Expando Node.
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition node.h:39
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.
void node_expando_private_free(void **ptr)
Free Expando private data - Implements ExpandoNode::ndata_free()
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 ExpandoNode * parse_long_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 long name.
void add_color(struct Buffer *buf, enum ColorId cid)
Add a colour code to a buffer.
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
struct ExpandoNode * node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
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.
struct ExpandoNode * node_expando_parse_name(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Parse an Expando format string.
const char * skip_until_ch(const char *str, char terminator)
Search a string for a terminator character.
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.
struct ExpandoFormat * parse_format(const char *str, const char **parsed_until, struct ExpandoParseError *err)
Parse a format string.
Expando Node for an Expando.
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
Render Expandos using Data.
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition render.h:32
#define ASSERT(COND)
Definition signal2.h:60
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
Definition of a format string.
Definition definition.h:43
short uid
Unique ID in domain.
Definition definition.h:47
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:62
const char * long_name
Long Expando name, e.g. "name".
Definition definition.h:45
short did
Domain ID.
Definition definition.h:46
const char * short_name
Short Expando name, e.g. "n".
Definition definition.h:44
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
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
const char * text
Node-specific text.
Definition node.h:73
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
Buffer for parsing errors.
Definition parse.h:37
char message[1024]
Error message.
Definition parse.h:38
const char * position
Position of error in original string.
Definition parse.h:39
get_string_t get_string
Callback function to get a string.
Definition render.h:80
get_number_t get_number
Callback function to get a number.
Definition render.h:81
Private data for an Expando -.
int color
ColorId to use.
bool has_tree
Contains tree characters, used in $index_format's s.