NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enriched.c File Reference

Rich text handler. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include "mutt/lib.h"
#include "enriched.h"
#include "body.h"
+ Include dependency graph for enriched.c:

Go to the source code of this file.

Data Structures

struct  Etags
 Enriched text tags. More...
 
struct  EnrichedState
 State of enriched-text parser. More...
 

Macros

#define INDENT_SIZE   4
 A (not so) minimal implementation of RFC1563.
 

Enumerations

enum  RichAttribs {
  RICH_PARAM = 0 , RICH_BOLD , RICH_UNDERLINE , RICH_ITALIC ,
  RICH_NOFILL , RICH_INDENT , RICH_INDENT_RIGHT , RICH_EXCERPT ,
  RICH_CENTER , RICH_FLUSHLEFT , RICH_FLUSHRIGHT , RICH_COLOR ,
  RICH_MAX
}
 Rich text attributes. More...
 

Functions

static void enriched_wrap (struct EnrichedState *enriched)
 Wrap enriched text.
 
static void enriched_flush (struct EnrichedState *enriched, bool wrap)
 Write enriched text to the State.
 
static void enriched_putwc (wchar_t c, struct EnrichedState *enriched)
 Write one wide character to the state.
 
static void enriched_puts (const char *s, struct EnrichedState *enriched)
 Write an enriched text string to the State.
 
static void enriched_set_flags (const wchar_t *tag, struct EnrichedState *enriched)
 Set flags on the enriched text state.
 
int text_enriched_handler (struct Body *b_email, struct State *state)
 Handler for enriched text - Implements handler_t -.
 

Variables

static const struct Etags EnrichedTags []
 EnrichedTags - Lookup table of tags allowed in enriched text.
 

Detailed Description

Rich text handler.

Authors
  • 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 enriched.c.

Macro Definition Documentation

◆ INDENT_SIZE

#define INDENT_SIZE   4

A (not so) minimal implementation of RFC1563.

Number of spaces for indentation in enriched text

Definition at line 43 of file enriched.c.

Enumeration Type Documentation

◆ RichAttribs

Rich text attributes.

Enumerator
RICH_PARAM 

Parameter label.

RICH_BOLD 

Bold text.

RICH_UNDERLINE 

Underlined text.

RICH_ITALIC 

Italic text.

RICH_NOFILL 

Text will not be reformatted.

RICH_INDENT 

Indented text.

RICH_INDENT_RIGHT 

Right-indented text.

RICH_EXCERPT 

Excerpt text.

RICH_CENTER 

Centred text.

RICH_FLUSHLEFT 

Left-justified text.

RICH_FLUSHRIGHT 

Right-justified text.

RICH_COLOR 

Coloured text.

RICH_MAX 

Definition at line 48 of file enriched.c.

49{
50 RICH_PARAM = 0,
51 RICH_BOLD,
63};
@ RICH_FLUSHRIGHT
Right-justified text.
Definition enriched.c:60
@ RICH_NOFILL
Text will not be reformatted.
Definition enriched.c:54
@ RICH_COLOR
Coloured text.
Definition enriched.c:61
@ RICH_PARAM
Parameter label.
Definition enriched.c:50
@ RICH_INDENT
Indented text.
Definition enriched.c:55
@ RICH_BOLD
Bold text.
Definition enriched.c:51
@ RICH_CENTER
Centred text.
Definition enriched.c:58
@ RICH_ITALIC
Italic text.
Definition enriched.c:53
@ RICH_UNDERLINE
Underlined text.
Definition enriched.c:52
@ RICH_EXCERPT
Excerpt text.
Definition enriched.c:57
@ RICH_INDENT_RIGHT
Right-indented text.
Definition enriched.c:56
@ RICH_MAX
Definition enriched.c:62
@ RICH_FLUSHLEFT
Left-justified text.
Definition enriched.c:59

Function Documentation

◆ enriched_wrap()

static void enriched_wrap ( struct EnrichedState * enriched)
static

Wrap enriched text.

Parameters
enrichedState of enriched text

Definition at line 121 of file enriched.c.

122{
123 if (!enriched)
124 return;
125
126 int x;
127
128 /* Output the current line content, applying center/flush-right alignment
129 * by stripping whitespace and adding leading spaces as needed */
130 if (enriched->line_len)
131 {
132 if (enriched->tag_level[RICH_CENTER] || enriched->tag_level[RICH_FLUSHRIGHT])
133 {
134 /* Strip trailing white space */
135 size_t y = enriched->line_used - 1;
136
137 while (y && iswspace(enriched->line[y]))
138 {
139 enriched->line[y] = (wchar_t) '\0';
140 y--;
141 enriched->line_used--;
142 enriched->line_len--;
143 }
144 if (enriched->tag_level[RICH_CENTER])
145 {
146 /* Strip leading whitespace */
147 y = 0;
148
149 while (enriched->line[y] && iswspace(enriched->line[y]))
150 y++;
151 if (y)
152 {
153 for (size_t z = y; z <= enriched->line_used; z++)
154 {
155 enriched->line[z - y] = enriched->line[z];
156 }
157
158 enriched->line_len -= y;
159 enriched->line_used -= y;
160 }
161 }
162 }
163
164 const int extra = enriched->wrap_margin - enriched->line_len - enriched->indent_len -
166 if (extra > 0)
167 {
168 if (enriched->tag_level[RICH_CENTER])
169 {
170 x = extra / 2;
171 while (x)
172 {
173 state_putc(enriched->state, ' ');
174 x--;
175 }
176 }
177 else if (enriched->tag_level[RICH_FLUSHRIGHT])
178 {
179 x = extra - 1;
180 while (x)
181 {
182 state_putc(enriched->state, ' ');
183 x--;
184 }
185 }
186 }
187 state_putws(enriched->state, (const wchar_t *) enriched->line);
188 }
189
190 /* Start a new line: reset line buffer and apply indentation for
191 * the current nesting level (prefix, excerpt markers, indent spaces) */
192 state_putc(enriched->state, '\n');
193 enriched->line[0] = (wchar_t) '\0';
194 enriched->line_len = 0;
195 enriched->line_used = 0;
196 enriched->indent_len = 0;
197 if (enriched->state->prefix)
198 {
199 state_puts(enriched->state, enriched->state->prefix);
200 enriched->indent_len += mutt_str_len(enriched->state->prefix);
201 }
202
203 if (enriched->tag_level[RICH_EXCERPT])
204 {
205 x = enriched->tag_level[RICH_EXCERPT];
206 while (x)
207 {
208 if (enriched->state->prefix)
209 {
210 state_puts(enriched->state, enriched->state->prefix);
211 enriched->indent_len += mutt_str_len(enriched->state->prefix);
212 }
213 else
214 {
215 state_puts(enriched->state, "> ");
216 enriched->indent_len += mutt_str_len("> ");
217 }
218 x--;
219 }
220 }
221 else
222 {
223 enriched->indent_len = 0;
224 }
225 if (enriched->tag_level[RICH_INDENT])
226 {
227 x = enriched->tag_level[RICH_INDENT] * INDENT_SIZE;
228 enriched->indent_len += x;
229 while (x)
230 {
231 state_putc(enriched->state, ' ');
232 x--;
233 }
234 }
235}
#define INDENT_SIZE
A (not so) minimal implementation of RFC1563.
Definition enriched.c:43
int state_putws(struct State *state, const wchar_t *ws)
Write a wide string to the state.
Definition state.c:147
#define state_puts(STATE, STR)
Definition state.h:58
#define state_putc(STATE, STR)
Definition state.h:59
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
wchar_t * line
Current line.
Definition enriched.c:101
int tag_level[RICH_MAX]
Nesting level of each tag type.
Definition enriched.c:112
size_t line_used
Used bytes in line buffer.
Definition enriched.c:105
struct State * state
State wrapper.
Definition enriched.c:114
size_t line_len
Capacity of line buffer.
Definition enriched.c:104
int wrap_margin
Wrap margin.
Definition enriched.c:113
size_t indent_len
Current indentation level.
Definition enriched.c:107
const char * prefix
String to add to the beginning of each output line.
Definition state.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enriched_flush()

static void enriched_flush ( struct EnrichedState * enriched,
bool wrap )
static

Write enriched text to the State.

Parameters
enrichedState of Enriched text
wraptrue if the text should be wrapped

Definition at line 242 of file enriched.c.

243{
244 if (!enriched || !enriched->buffer)
245 return;
246
247 if (!enriched->tag_level[RICH_NOFILL] &&
248 ((enriched->line_len + enriched->word_len) >
249 (enriched->wrap_margin - (enriched->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE) -
250 enriched->indent_len)))
251 {
252 enriched_wrap(enriched);
253 }
254
255 if (enriched->buf_used)
256 {
257 enriched->buffer[enriched->buf_used] = (wchar_t) '\0';
258 enriched->line_used += enriched->buf_used;
259 if (enriched->line_used > enriched->line_max)
260 {
261 enriched->line_max = enriched->line_used;
262 MUTT_MEM_REALLOC(&enriched->line, enriched->line_max + 1, wchar_t);
263 }
264 wcscat(enriched->line, enriched->buffer);
265 enriched->line_len += enriched->word_len;
266 enriched->word_len = 0;
267 enriched->buf_used = 0;
268 }
269 if (wrap)
270 enriched_wrap(enriched);
271 fflush(enriched->state->fp_out);
272}
static void enriched_wrap(struct EnrichedState *enriched)
Wrap enriched text.
Definition enriched.c:121
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
wchar_t * buffer
Output buffer.
Definition enriched.c:100
size_t buf_used
Used bytes in output buffer.
Definition enriched.c:109
size_t word_len
Current word length.
Definition enriched.c:108
size_t line_max
Maximum line width.
Definition enriched.c:106
FILE * fp_out
File to write to.
Definition state.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enriched_putwc()

static void enriched_putwc ( wchar_t c,
struct EnrichedState * enriched )
static

Write one wide character to the state.

Parameters
cCharacter to write
enrichedState of Enriched text

Definition at line 279 of file enriched.c.

280{
281 if (!enriched)
282 return;
283
284 if (enriched->tag_level[RICH_PARAM])
285 {
286 if (enriched->tag_level[RICH_COLOR])
287 {
288 if ((enriched->param_used + 1) >= enriched->param_len)
289 {
290 enriched->param_len += 256;
291 MUTT_MEM_REALLOC(&enriched->param, enriched->param_len, wchar_t);
292 }
293
294 enriched->param[enriched->param_used++] = c;
295 }
296 return; /* nothing to do */
297 }
298
299 /* see if more space is needed (plus extra for possible rich characters) */
300 if ((enriched->buf_len < (enriched->buf_used + 3)) || !enriched->buffer)
301 {
302 enriched->buf_len += 1024;
303 MUTT_MEM_REALLOC(&enriched->buffer, enriched->buf_len + 1, wchar_t);
304 }
305
306 if ((!enriched->tag_level[RICH_NOFILL] && iswspace(c)) || (c == (wchar_t) '\0'))
307 {
308 if (c == (wchar_t) '\t')
309 enriched->word_len += 8 - (enriched->line_len + enriched->word_len) % 8;
310 else
311 enriched->word_len++;
312
313 enriched->buffer[enriched->buf_used++] = c;
314 enriched_flush(enriched, false);
315 }
316 else
317 {
318 if (enriched->state->flags & STATE_DISPLAY)
319 {
320 if (enriched->tag_level[RICH_BOLD])
321 {
322 enriched->buffer[enriched->buf_used++] = c;
323 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
324 enriched->buffer[enriched->buf_used++] = c;
325 }
326 else if (enriched->tag_level[RICH_UNDERLINE])
327 {
328 enriched->buffer[enriched->buf_used++] = '_';
329 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
330 enriched->buffer[enriched->buf_used++] = c;
331 }
332 else if (enriched->tag_level[RICH_ITALIC])
333 {
334 enriched->buffer[enriched->buf_used++] = c;
335 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
336 enriched->buffer[enriched->buf_used++] = '_';
337 }
338 else
339 {
340 enriched->buffer[enriched->buf_used++] = c;
341 }
342 }
343 else
344 {
345 enriched->buffer[enriched->buf_used++] = c;
346 }
347 enriched->word_len++;
348 }
349}
static void enriched_flush(struct EnrichedState *enriched, bool wrap)
Write enriched text to the State.
Definition enriched.c:242
#define STATE_DISPLAY
Output is displayed to the user.
Definition state.h:33
size_t buf_len
Length of buffer.
Definition enriched.c:103
size_t param_used
Used bytes in param buffer.
Definition enriched.c:110
wchar_t * param
Current parameter.
Definition enriched.c:102
size_t param_len
Capacity of param buffer.
Definition enriched.c:111
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition state.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enriched_puts()

static void enriched_puts ( const char * s,
struct EnrichedState * enriched )
static

Write an enriched text string to the State.

Parameters
sString to write
enrichedState of Enriched text

Definition at line 356 of file enriched.c.

357{
358 if (!enriched)
359 return;
360
361 const char *c = NULL;
362
363 if ((enriched->buf_len < (enriched->buf_used + mutt_str_len(s))) || !enriched->buffer)
364 {
365 enriched->buf_len += 1024;
366 MUTT_MEM_REALLOC(&enriched->buffer, enriched->buf_len + 1, wchar_t);
367 }
368 c = s;
369 while (*c)
370 {
371 enriched->buffer[enriched->buf_used++] = (wchar_t) *c;
372 c++;
373 }
374}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enriched_set_flags()

static void enriched_set_flags ( const wchar_t * tag,
struct EnrichedState * enriched )
static

Set flags on the enriched text state.

Parameters
tagTag to set
enrichedState of Enriched text

Definition at line 381 of file enriched.c.

382{
383 if (!enriched)
384 return;
385
386 const wchar_t *tagptr = tag;
387 int i, j;
388
389 if (*tagptr == (wchar_t) '/')
390 tagptr++;
391
392 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
393 {
394 if (wcscasecmp(EnrichedTags[i].tag_name, tagptr) == 0)
395 {
396 j = EnrichedTags[i].index;
397 break;
398 }
399 }
400
401 if (j != -1)
402 {
403 if ((j == RICH_CENTER) || (j == RICH_FLUSHLEFT) || (j == RICH_FLUSHRIGHT))
404 enriched_flush(enriched, true);
405
406 if (*tag == (wchar_t) '/')
407 {
408 if (enriched->tag_level[j]) /* make sure not to go negative */
409 enriched->tag_level[j]--;
410 if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_PARAM) &&
411 enriched->tag_level[RICH_COLOR])
412 {
413 enriched->param[enriched->param_used] = (wchar_t) '\0';
414 if (wcscasecmp(L"black", enriched->param) == 0)
415 {
416 enriched_puts("\033[30m", enriched); // Escape
417 }
418 else if (wcscasecmp(L"red", enriched->param) == 0)
419 {
420 enriched_puts("\033[31m", enriched); // Escape
421 }
422 else if (wcscasecmp(L"green", enriched->param) == 0)
423 {
424 enriched_puts("\033[32m", enriched); // Escape
425 }
426 else if (wcscasecmp(L"yellow", enriched->param) == 0)
427 {
428 enriched_puts("\033[33m", enriched); // Escape
429 }
430 else if (wcscasecmp(L"blue", enriched->param) == 0)
431 {
432 enriched_puts("\033[34m", enriched); // Escape
433 }
434 else if (wcscasecmp(L"magenta", enriched->param) == 0)
435 {
436 enriched_puts("\033[35m", enriched); // Escape
437 }
438 else if (wcscasecmp(L"cyan", enriched->param) == 0)
439 {
440 enriched_puts("\033[36m", enriched); // Escape
441 }
442 else if (wcscasecmp(L"white", enriched->param) == 0)
443 {
444 enriched_puts("\033[37m", enriched); // Escape
445 }
446 }
447 if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_COLOR))
448 {
449 enriched_puts("\033[0m", enriched); // Escape
450 }
451
452 /* flush parameter buffer when closing the tag */
453 if (j == RICH_PARAM)
454 {
455 enriched->param_used = 0;
456 enriched->param[0] = (wchar_t) '\0';
457 }
458 }
459 else
460 {
461 enriched->tag_level[j]++;
462 }
463
464 if (j == RICH_EXCERPT)
465 enriched_flush(enriched, true);
466 }
467}
static const struct Etags EnrichedTags[]
EnrichedTags - Lookup table of tags allowed in enriched text.
Definition enriched.c:75
static void enriched_puts(const char *s, struct EnrichedState *enriched)
Write an enriched text string to the State.
Definition enriched.c:356
int wcscasecmp(const wchar_t *a, const wchar_t *b)
Compare two wide-character strings, ignoring case.
Definition wcscasecmp.c:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ EnrichedTags

const struct Etags EnrichedTags[]
static
Initial value:
= {
{ L"param", RICH_PARAM },
{ L"bold", RICH_BOLD },
{ L"italic", RICH_ITALIC },
{ L"underline", RICH_UNDERLINE },
{ L"nofill", RICH_NOFILL },
{ L"excerpt", RICH_EXCERPT },
{ L"indent", RICH_INDENT },
{ L"indentright", RICH_INDENT_RIGHT },
{ L"center", RICH_CENTER },
{ L"flushleft", RICH_FLUSHLEFT },
{ L"flushright", RICH_FLUSHRIGHT },
{ L"flushboth", RICH_FLUSHLEFT },
{ L"color", RICH_COLOR },
{ L"x-color", RICH_COLOR },
{ NULL, -1 },
}

EnrichedTags - Lookup table of tags allowed in enriched text.

Definition at line 75 of file enriched.c.

75 {
76 // clang-format off
77 { L"param", RICH_PARAM },
78 { L"bold", RICH_BOLD },
79 { L"italic", RICH_ITALIC },
80 { L"underline", RICH_UNDERLINE },
81 { L"nofill", RICH_NOFILL },
82 { L"excerpt", RICH_EXCERPT },
83 { L"indent", RICH_INDENT },
84 { L"indentright", RICH_INDENT_RIGHT },
85 { L"center", RICH_CENTER },
86 { L"flushleft", RICH_FLUSHLEFT },
87 { L"flushright", RICH_FLUSHRIGHT },
88 { L"flushboth", RICH_FLUSHLEFT },
89 { L"color", RICH_COLOR },
90 { L"x-color", RICH_COLOR },
91 { NULL, -1 },
92 // clang-format on
93};