NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_text.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h>
33#include "mutt/lib.h"
34#include "node_text.h"
35#include "format.h"
36#include "node.h"
37#include "render.h"
38
42static int node_text_render(const struct ExpandoNode *node,
43 const struct ExpandoRenderData *rdata, struct Buffer *buf,
44 int max_cols, void *data, MuttFormatFlags flags)
45{
46 ASSERT(node->type == ENT_TEXT);
47
48 return format_string(buf, 0, max_cols, JUSTIFY_LEFT, ' ', node->text,
49 mutt_str_len(node->text), false);
50}
51
59struct ExpandoNode *node_text_new(const char *text)
60{
61 struct ExpandoNode *node = node_new();
62
63 node->type = ENT_TEXT;
64 node->text = mutt_str_dup(text);
66
67 return node;
68}
69
86struct ExpandoNode *node_text_parse(const char *str, NodeTextTermFlags term_chars,
87 const char **parsed_until)
88{
89 if (!str || (str[0] == '\0') || !parsed_until)
90 return NULL;
91
92 struct Buffer *text = buf_pool_get();
93
94 while (str[0] != '\0')
95 {
96 if (str[0] == '\\') // Literal character
97 {
98 if (str[1] == '\0')
99 {
100 buf_addch(text, '\\');
101 str++;
102 break;
103 }
104
105 buf_addch(text, str[1]);
106 str += 2;
107 continue;
108 }
109
110 if ((str[0] == '%') && (str[1] == '%')) // Literal %
111 {
112 buf_addch(text, '%');
113 str += 2;
114 continue;
115 }
116
117 if (str[0] == '%') // '%' is always special
118 break;
119
120 if ((str[0] == '&') && (term_chars & NTE_AMPERSAND))
121 break;
122
123 if ((str[0] == '>') && (term_chars & NTE_GREATER))
124 break;
125
126 if ((str[0] == '?') && (term_chars & NTE_QUESTION))
127 break;
128
129 buf_addch(text, str[0]); // Plain text
130 str++;
131 }
132
133 *parsed_until = str; // First unused character
134
135 struct ExpandoNode *node = NULL;
136 if (!buf_is_empty(text))
138
140
141 return node;
142}
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
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
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.
@ JUSTIFY_LEFT
Left justify the text.
Definition: format.h:34
static int node_text_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a Text Node - Implements ExpandoNode::render() -.
Definition: node_text.c:42
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
Basic Expando Node.
@ ENT_TEXT
Plain text.
Definition: node.h:38
struct ExpandoNode * node_text_new(const char *text)
Create a new Text ExpandoNode.
Definition: node_text.c:59
struct ExpandoNode * node_text_parse(const char *str, NodeTextTermFlags term_chars, const char **parsed_until)
Extract a block of text.
Definition: node_text.c:86
Expando Node for Text.
#define NTE_QUESTION
'?' Question mark
Definition: node_text.h:36
#define NTE_GREATER
'>' Greater than
Definition: node_text.h:35
uint8_t NodeTextTermFlags
Special characters that end a text string.
Definition: node_text.h:32
#define NTE_AMPERSAND
'&' Ampersand
Definition: node_text.h:34
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
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
Basic Expando Node.
Definition: node.h:67
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
const char * text
Node-specific text.
Definition: node.h:73
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68