NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_expando.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <ctype.h>
32#include <limits.h>
33#include <stdio.h>
34#include "mutt/lib.h"
35#include "node_expando.h"
36#include "color/lib.h"
37#include "definition.h"
38#include "format.h"
39#include "helpers.h"
40#include "mutt_thread.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 (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}
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++)
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}
274
284struct ExpandoNode *node_expando_parse(const char *str, const struct ExpandoDefinition *defs,
285 ExpandoParserFlags flags, const char **parsed_until,
286 struct ExpandoParseError *err)
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}
310
317const char *skip_until_ch(const char *str, char terminator)
318{
319 while (str[0] != '\0')
320 {
321 if (*str == terminator)
322 break;
323
324 if (str[0] == '\\') // Literal character
325 {
326 if (str[1] == '\0')
327 return str + 1;
328
329 str++;
330 }
331
332 str++;
333 }
334
335 return str;
336}
337
349struct ExpandoNode *node_expando_parse_enclosure(const char *str, int did,
350 int uid, char terminator,
351 struct ExpandoFormat *fmt,
352 const char **parsed_until,
353 struct ExpandoParseError *err)
354
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}
387
393void add_color(struct Buffer *buf, enum ColorId cid)
394{
395 ASSERT(cid < MT_COLOR_MAX);
396
398 buf_addch(buf, cid);
399}
400
404int node_expando_render(const struct ExpandoNode *node,
405 const struct ExpandoRenderCallback *erc, struct Buffer *buf,
406 int max_cols, void *data, MuttFormatFlags flags)
407{
408 ASSERT(node->type == ENT_EXPANDO);
409
410 struct Buffer *buf_expando = buf_pool_get();
411 struct Buffer *buf_format = buf_pool_get();
412
413 const struct ExpandoFormat *fmt = node->format;
414 const struct NodeExpandoPrivate *priv = node->ndata;
415
416 // ---------------------------------------------------------------------------
417 // Numbers and strings get treated slightly differently. We prefer strings.
418 // This allows dates to be stored as 1729850182, but displayed as "2024-10-25".
419
420 const struct ExpandoRenderCallback *erc_match = find_get_string(erc, node->did, node->uid);
421 if (erc_match)
422 {
423 erc_match->get_string(node, data, flags, buf_expando);
424
425 if (fmt && fmt->lower)
426 buf_lower_special(buf_expando);
427 }
428 else
429 {
430 erc_match = find_get_number(erc, node->did, node->uid);
431 ASSERT(erc_match && "Unknown UID");
432
433 const long num = erc_match->get_number(node, data, flags);
434
435 int precision = 1;
436
437 if (fmt)
438 {
439 precision = fmt->max_cols;
440 if ((precision < 0) && (fmt->leader == '0'))
441 precision = fmt->min_cols;
442 }
443
444 if (num < 0)
445 precision--; // Allow space for the '-' sign
446
447 buf_printf(buf_expando, "%.*ld", precision, num);
448 }
449
450 // ---------------------------------------------------------------------------
451
452 int max = max_cols;
453 int min = 0;
454
455 if (fmt)
456 {
457 min = fmt->min_cols;
458 if (fmt->max_cols > 0)
459 max = MIN(max_cols, fmt->max_cols);
460 }
461
462 const enum FormatJustify just = fmt ? fmt->justification : JUSTIFY_LEFT;
463
464 int total_cols = format_string(buf_format, min, max, just, ' ', buf_string(buf_expando),
465 buf_len(buf_expando), priv->has_tree);
466
467 if (!buf_is_empty(buf_format))
468 {
469 if (priv->color > -1)
470 add_color(buf, priv->color);
471
472 buf_addstr(buf, buf_string(buf_format));
473
474 if (priv->color > -1)
476 }
477
478 buf_pool_release(&buf_format);
479 buf_pool_release(&buf_expando);
480
481 return total_cols;
482}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:270
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 colored objects.
Definition: color.h:40
@ MT_COLOR_MAX
Definition: color.h:94
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:83
Define an Expando format string.
#define EP_NO_CUSTOM_PARSE
Don't use the custom parser.
Definition: definition.h:36
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() -.
Definition: node_expando.c:404
#define FREE(x)
Definition: memory.h:55
#define MIN(a, b)
Definition: memory.h:32
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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:425
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
Create/manipulate threading in emails.
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition: mutt_thread.h:73
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
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.
Definition: node_expando.c:284
void node_expando_private_free(void **ptr)
Free Expando private data - Implements ExpandoNode::ndata_free()
Definition: node_expando.c:63
void node_expando_set_color(const struct ExpandoNode *node, int cid)
Set the colour for an Expando.
Definition: node_expando.c:100
void node_expando_set_has_tree(const struct ExpandoNode *node, bool has_tree)
Set the has_tree flag for an Expando.
Definition: node_expando.c:115
void add_color(struct Buffer *buf, enum ColorId cid)
Add a colour code to a buffer.
Definition: node_expando.c:393
struct NodeExpandoPrivate * node_expando_private_new(void)
Create new Expando private data.
Definition: node_expando.c:49
struct ExpandoNode * node_expando_new(struct ExpandoFormat *fmt, int did, int uid)
Create a new Expando ExpandoNode.
Definition: node_expando.c:78
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.
Definition: node_expando.c:349
const char * skip_until_ch(const char *str, char terminator)
Search a string for a terminator character.
Definition: node_expando.c:317
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
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:58
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
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
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
Definition: render.h:80
get_number_t get_number
Definition: render.h:81
Private data for an Expando -.
Definition: node_expando.h:40
int color
ColorId to use.
Definition: node_expando.h:41
bool has_tree
Contains tree characters, used in $index_format's s.
Definition: node_expando.h:42