NeoMutt  2025-12-11-435-g4ac674
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 if (len == 0)
258 continue;
259
260 if (mutt_strn_equal(def->short_name, str, len))
261 {
262 if (def->parse)
263 {
264 return def->parse(str, fmt, def->did, def->uid, flags, parsed_until, err);
265 }
266 else
267 {
268 *parsed_until = str + len;
269 return node_expando_new(fmt, def->did, def->uid);
270 }
271 }
272 }
273
274 return NULL;
275}
276
287struct ExpandoNode *parse_long_name(const char *str, const struct ExpandoDefinition *defs,
288 ExpandoParserFlags flags,
289 struct ExpandoFormat *fmt, const char **parsed_until,
290 struct ExpandoParseError *err)
291{
292 if (!str || !defs)
293 return NULL;
294
295 const struct ExpandoDefinition *def = defs;
296 for (; def && (def->short_name || def->long_name); def++)
297 {
298 if (!def->long_name)
299 continue;
300
301 size_t len = mutt_str_len(def->long_name);
302
303 if (mutt_strn_equal(def->long_name, str, len))
304 {
305 *parsed_until = str + len;
306 if (def->parse)
307 {
308 struct ExpandoNode *node = def->parse(str, fmt, def->did, def->uid,
309 flags, parsed_until, err);
310 if (node || (err->message[0] != '\0'))
311 return node;
312 }
313 else
314 {
315 if (str[len] != '}') // Not an exact match
316 continue;
317
318 return node_expando_new(fmt, def->did, def->uid);
319 }
320 }
321 }
322
323 return NULL;
324}
325
335struct ExpandoNode *node_expando_parse(const char *str, const struct ExpandoDefinition *defs,
336 ExpandoParserFlags flags, const char **parsed_until,
337 struct ExpandoParseError *err)
338{
339 ASSERT(str[0] == '%');
340 str++;
341
342 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
343 if (err->position)
344 {
345 FREE(&fmt);
346 return NULL;
347 }
348
349 str = *parsed_until;
350
351 struct ExpandoNode *node = parse_short_name(str, defs, flags, fmt, parsed_until, err);
352 if (node)
353 return node;
354
355 if (!err->position)
356 {
357 err->position = *parsed_until;
358 // L10N: e.g. "Unknown expando: %Q"
359 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.1s"), *parsed_until);
360 }
361
362 FREE(&fmt);
363 return NULL;
364}
365
375struct ExpandoNode *node_expando_parse_name(const char *str,
376 const struct ExpandoDefinition *defs,
377 ExpandoParserFlags flags, const char **parsed_until,
378 struct ExpandoParseError *err)
379{
380 ASSERT(str[0] == '%');
381 str++;
382
383 struct ExpandoFormat *fmt = parse_format(str, parsed_until, err);
384 if (err->position)
385 goto fail;
386
387 str = *parsed_until;
388
389 if (str[0] != '{')
390 goto fail;
391
392 str++;
393
394 struct ExpandoNode *node = parse_long_name(str, defs, flags, fmt, parsed_until, err);
395 if (!node)
396 goto fail;
397
398 fmt = NULL; // owned by the node, now
399
400 if ((*parsed_until)[0] == '}')
401 {
402 (*parsed_until)++;
403 return node;
404 }
405
406 node_free(&node);
407
408fail:
409 FREE(&fmt);
410 return NULL;
411}
412
419const char *skip_until_ch(const char *str, char terminator)
420{
421 while (str[0] != '\0')
422 {
423 if (*str == terminator)
424 break;
425
426 if (str[0] == '\\') // Literal character
427 {
428 if (str[1] == '\0')
429 return str + 1;
430
431 str++;
432 }
433
434 str++;
435 }
436
437 return str;
438}
439
451struct ExpandoNode *node_expando_parse_enclosure(const char *str, int did,
452 int uid, char terminator,
453 struct ExpandoFormat *fmt,
454 const char **parsed_until,
455 struct ExpandoParseError *err)
456
457{
458 str++; // skip opening char
459
460 const char *expando_end = skip_until_ch(str, terminator);
461
462 if (*expando_end != terminator)
463 {
464 err->position = expando_end;
465 snprintf(err->message, sizeof(err->message),
466 // L10N: Expando is missing a terminator character
467 // e.g. "%[..." is missing the final ']'
468 _("Expando is missing terminator: '%c'"), terminator);
469 return NULL;
470 }
471
472 *parsed_until = expando_end + 1;
473
474 struct ExpandoNode *node = node_expando_new(fmt, did, uid);
475
476 struct Buffer *buf = buf_pool_get();
477 for (; str < expando_end; str++)
478 {
479 if (str[0] == '\\')
480 continue;
481 buf_addch(buf, str[0]);
482 }
483
484 node->text = buf_strdup(buf);
485 buf_pool_release(&buf);
486
487 return node;
488}
489
495void add_color(struct Buffer *buf, enum ColorId cid)
496{
497 ASSERT(cid < MT_COLOR_MAX);
498
500 buf_addch(buf, cid);
501}
502
506int node_expando_render(const struct ExpandoNode *node,
507 const struct ExpandoRenderCallback *erc, struct Buffer *buf,
508 int max_cols, void *data, MuttFormatFlags flags)
509{
510 ASSERT(node->type == ENT_EXPANDO);
511
512 struct Buffer *buf_expando = buf_pool_get();
513 struct Buffer *buf_format = buf_pool_get();
514
515 const struct ExpandoFormat *fmt = node->format;
516 const struct NodeExpandoPrivate *priv = node->ndata;
517
518 // ---------------------------------------------------------------------------
519 // Numbers and strings get treated slightly differently. We prefer strings.
520 // This allows dates to be stored as 1729850182, but displayed as "2024-10-25".
521
522 const struct ExpandoRenderCallback *erc_match = find_get_string(erc, node->did, node->uid);
523 if (erc_match)
524 {
525 erc_match->get_string(node, data, flags, buf_expando);
526
527 if (fmt && fmt->lower)
528 buf_lower_special(buf_expando);
529 }
530 else
531 {
532 erc_match = find_get_number(erc, node->did, node->uid);
533 ASSERT(erc_match && "Unknown UID");
534
535 const long num = erc_match->get_number(node, data, flags);
536
537 int precision = 1;
538
539 if (fmt)
540 {
541 precision = fmt->max_cols;
542 if ((precision < 0) && (fmt->leader == '0'))
543 precision = fmt->min_cols;
544 }
545
546 if (num < 0)
547 precision--; // Allow space for the '-' sign
548
549 buf_printf(buf_expando, "%.*ld", precision, num);
550 }
551
552 // ---------------------------------------------------------------------------
553
554 int max = max_cols;
555 int min = 0;
556
557 if (fmt)
558 {
559 min = fmt->min_cols;
560 if (fmt->max_cols > 0)
561 max = MIN(max_cols, fmt->max_cols);
562 }
563
564 const enum FormatJustify just = fmt ? fmt->justification : JUSTIFY_LEFT;
565
566 int total_cols = format_string(buf_format, min, max, just, ' ', buf_string(buf_expando),
567 buf_len(buf_expando), priv->has_tree);
568
569 if (!buf_is_empty(buf_format))
570 {
571 if (priv->color > -1)
572 add_color(buf, priv->color);
573
574 buf_addstr(buf, buf_string(buf_format));
575
576 if (priv->color > -1)
578 }
579
580 buf_pool_release(&buf_format);
581 buf_pool_release(&buf_expando);
582
583 return total_cols;
584}
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:35
@ MT_COLOR_MAX
Definition color.h:97
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:86
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)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MIN(a, b)
Return the minimum of two values.
Definition memory.h:40
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
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:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
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:59
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.