NeoMutt  2025-12-11-872-g385a04
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
display.c File Reference

Pager Display. More...

#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "display.h"
#include "lib.h"
#include "color/lib.h"
#include "module_data.h"
#include "private_data.h"
+ Include dependency graph for display.c:

Go to the source code of this file.

Functions

static int check_sig (const char *s, struct Line *info, int offset)
 Check for an email signature.
 
static int comp_syntax_t (const void *m1, const void *m2)
 Search for a Syntax using bsearch(3)
 
static void resolve_color (struct MuttWindow *win, struct Line *lines, int line_num, int cnt, PagerFlags flags, int special, struct AnsiColor *ansi)
 Set the colour for a line of text.
 
static void append_line (struct Line *lines, int line_num, int cnt)
 Add a new Line to the array.
 
static int check_marker (const char *q, const char *p)
 Check that the unique marker is present.
 
static int check_attachment_marker (const char *p)
 Check that the unique marker is present.
 
static int check_protected_header_marker (const char *p)
 Check that the unique marker is present.
 
bool mutt_is_quote_line (char *line, regmatch_t *pmatch)
 Is a line of message text a quote?
 
static void match_body_patterns (char *pat, struct Line *lines, int line_num)
 Match body patterns, e.g.
 
bool color_is_header (enum ColorId cid)
 Colour is for an Email header.
 
static void resolve_types (struct MuttWindow *win, char *buf, char *raw, struct Line *lines, int line_num, int lines_used, struct QuoteStyle **quote_list, int *q_level, bool *force_redraw, bool q_classify)
 Determine the style for a line of text.
 
void buf_strip_formatting (struct Buffer *dest, const char *src, bool strip_markers)
 Removes ANSI and backspace formatting.
 
static int fill_buffer (FILE *fp, LOFF_T *bytes_read, LOFF_T offset, unsigned char **buf, unsigned char **fmt, size_t *blen, int *buf_ready)
 Fill a buffer from a file.
 
static int format_line (struct MuttWindow *win, struct Line **lines, int line_num, unsigned char *buf, PagerFlags flags, struct AnsiColor *ansi, int cnt, int *pspace, int *pvch, int *pcol, int *pspecial, int width, struct AttrColorList *ansi_list)
 Display a line of text in the pager.
 
int display_line (FILE *fp, LOFF_T *bytes_read, struct Line **lines, int line_num, int *lines_used, int *lines_max, PagerFlags flags, struct QuoteStyle **quote_list, int *q_level, bool *force_redraw, regex_t *search_re, struct MuttWindow *win_pager, struct AttrColorList *ansi_list)
 Print a line on screen.
 

Detailed Description

Pager Display.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Tóth János
  • Thomas Klausner

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 display.c.

Function Documentation

◆ check_sig()

static int check_sig ( const char * s,
struct Line * info,
int offset )
static

Check for an email signature.

Parameters
sText to examine
infoLine info array to update
offsetAn offset line to start the check from
Return values
0Success
-1Error

Definition at line 58 of file display.c.

59{
60 const unsigned int NUM_SIG_LINES = 4; // The amount of lines a signature takes
61 unsigned int count = 0;
62
63 while ((offset > 0) && (count <= NUM_SIG_LINES))
64 {
65 if (info[offset].cid != MT_COLOR_SIGNATURE)
66 break;
67 count++;
68 offset--;
69 }
70
71 if (count == 0)
72 return -1;
73
74 if (count > NUM_SIG_LINES)
75 {
76 /* check for a blank line */
77 while (*s)
78 {
79 if (!mutt_isspace(*s))
80 return 0;
81 s++;
82 }
83
84 return -1;
85 }
86
87 return 0;
88}
@ MT_COLOR_SIGNATURE
Pager: signature lines.
Definition color.h:77
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:96
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ comp_syntax_t()

static int comp_syntax_t ( const void * m1,
const void * m2 )
static

Search for a Syntax using bsearch(3)

Parameters
m1Search key
m2Array member
Return values
-1m1 precedes m2
0m1 matches m2
1m2 precedes m1

Definition at line 98 of file display.c.

99{
100 const int *cnt = (const int *) m1;
101 const struct TextSyntax *stx = (const struct TextSyntax *) m2;
102
103 if (*cnt < stx->first)
104 return -1;
105 if (*cnt >= stx->last)
106 return 1;
107 return 0;
108}
Highlighting for a piece of text.
Definition display.h:39
int last
Last character in line to be coloured (not included)
Definition display.h:42
int first
First character in line to be coloured.
Definition display.h:41
+ Here is the caller graph for this function:

◆ resolve_color()

static void resolve_color ( struct MuttWindow * win,
struct Line * lines,
int line_num,
int cnt,
PagerFlags flags,
int special,
struct AnsiColor * ansi )
static

Set the colour for a line of text.

Parameters
winWindow
linesLine info array
line_numLine Number (index into lines)
cntIf true, this is a continuation line
flagsFlags, see PagerFlags
specialFlags, e.g. A_BOLD
ansiANSI attributes

Definition at line 120 of file display.c.

122{
123 struct AttrColor def_color = { 0 }; /* color without syntax highlight */
124 struct AttrColor color = { 0 }; /* final color */
125 static struct AttrColor last_color = { 0 }; /* last color set */
126 bool search = false;
127 int m;
128 struct TextSyntax *matching_chunk = NULL;
129
130 if (cnt == 0)
131 {
132 last_color.curses_color = NULL;
133 last_color.attrs = A_NORMAL;
134 }
135
136 if (lines[line_num].cont_line)
137 {
138 const bool c_markers = cs_subset_bool(NeoMutt->sub, "markers");
139 if (!cnt && c_markers)
140 {
142 mutt_window_addch(win, '+');
143 }
144 m = (lines[line_num].syntax)[0].first;
145 cnt += (lines[line_num].syntax)[0].last;
146 }
147 else
148 {
149 m = line_num;
150 }
151 if (flags & MUTT_PAGER_LOGS)
152 {
153 def_color = *(lines[line_num].syntax[0].attr_color);
154 }
155 else if (!(flags & MUTT_SHOWCOLOR))
156 {
157 if (flags & MUTT_PAGER_STRIPES)
158 {
159 def_color = *simple_color_get(((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD :
161 }
162 else
163 {
164 def_color = *simple_color_get(MT_COLOR_NORMAL);
165 }
166 }
167 else if ((lines[m].cid == MT_COLOR_HEADER) && lines[m].syntax[0].attr_color)
168 {
169 def_color = *lines[m].syntax[0].attr_color;
170 }
171 else
172 {
173 def_color = *simple_color_get(lines[m].cid);
174 }
175
176 if ((flags & MUTT_SHOWCOLOR) && COLOR_QUOTED(lines[m].cid))
177 {
178 struct QuoteStyle *qc = lines[m].quote;
179
180 if (qc)
181 {
182 def_color = attr_color_copy(qc->attr_color);
183
184 while (qc && (qc->prefix_len > cnt))
185 {
186 def_color = attr_color_copy(qc->attr_color);
187 qc = qc->up;
188 }
189 }
190 }
191
192 color = def_color;
193 if ((flags & MUTT_SHOWCOLOR) && lines[m].syntax)
194 {
195 matching_chunk = bsearch(&cnt, lines[m].syntax, lines[m].syntax_arr_size,
196 sizeof(struct TextSyntax), comp_syntax_t);
197 if (matching_chunk && (cnt >= matching_chunk->first) &&
198 (cnt < matching_chunk->last))
199 {
200 if (matching_chunk->attr_color)
201 color = *matching_chunk->attr_color;
202 }
203 }
204
205 if ((flags & MUTT_SEARCH) && lines[m].search)
206 {
207 matching_chunk = bsearch(&cnt, lines[m].search, lines[m].search_arr_size,
208 sizeof(struct TextSyntax), comp_syntax_t);
209 if (matching_chunk && (cnt >= matching_chunk->first) &&
210 (cnt < matching_chunk->last))
211 {
213 search = true;
214 }
215 }
216
217 /* handle "special" bold & underlined characters */
218 if (special & A_BOLD)
219 {
222 else
223 color.attrs |= A_BOLD;
224 }
225 else if (special & A_UNDERLINE)
226 {
229 else
230 color.attrs |= A_UNDERLINE;
231 }
232 else if (special & A_ITALIC)
233 {
236 else
237 color.attrs |= A_ITALIC;
238 }
239 else if (ansi->attr_color)
240 {
241 color = *ansi->attr_color;
242 }
243
244 if (!attr_color_match(&color, &last_color))
245 {
247 &color);
248 mutt_curses_set_color(ac_merge);
249 last_color = color;
250 }
251}
bool attr_color_match(struct AttrColor *ac1, struct AttrColor *ac2)
Do the colours match?
Definition attr.c:192
struct AttrColor attr_color_copy(const struct AttrColor *ac)
Copy a colour.
Definition attr.c:165
bool simple_color_is_set(enum ColorId cid)
Is the object coloured?
Definition simple.c:120
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition simple.c:98
@ MT_COLOR_MARKERS
Pager: markers, line continuation.
Definition color.h:51
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition color.h:48
@ MT_COLOR_STRIPE_EVEN
Stripes: even lines of the Help Page.
Definition color.h:79
@ MT_COLOR_BOLD
Bold text.
Definition color.h:40
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:53
@ MT_COLOR_SEARCH
Pager: search matches.
Definition color.h:67
@ MT_COLOR_ITALIC
Italic text.
Definition color.h:50
@ MT_COLOR_STRIPE_ODD
Stripes: odd lines of the Help Page.
Definition color.h:80
@ MT_COLOR_UNDERLINE
Underlined text.
Definition color.h:83
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
static int comp_syntax_t(const void *m1, const void *m2)
Search for a Syntax using bsearch(3)
Definition display.c:98
static int search(struct Menu *menu, int op, int *match)
Search a menu.
Definition functions.c:59
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition merged.c:110
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:79
void mutt_curses_set_color(const struct AttrColor *ac)
Set the colour and attributes for text.
Definition mutt_curses.c:38
#define A_ITALIC
Definition mutt_curses.h:48
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
#define MUTT_PAGER_STRIPES
Striped highlighting.
Definition lib.h:77
#define MUTT_SHOWCOLOR
Show characters in color otherwise don't show characters.
Definition lib.h:65
#define MUTT_PAGER_LOGS
Logview mode.
Definition lib.h:75
#define MUTT_SEARCH
Resolve search patterns.
Definition lib.h:67
#define COLOR_QUOTED(cid)
Definition quoted.h:28
const struct AttrColor * attr_color
Curses colour of text.
Definition ansi.h:39
A curses colour and its attributes.
Definition attr.h:65
int attrs
Text attributes, e.g. A_BOLD.
Definition attr.h:68
struct CursesColor * curses_color
Underlying Curses colour.
Definition attr.h:69
struct QuoteStyle * quote
Quoting style for this line (pointer into PagerPrivateData->quote_list)
Definition display.h:62
struct TextSyntax * syntax
Array of coloured text in the line.
Definition display.h:57
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Style of quoted text.
Definition qstyle.h:55
struct AttrColor * attr_color
Colour and attribute of the text.
Definition qstyle.h:57
struct QuoteStyle * up
Definition qstyle.h:61
size_t prefix_len
Length of the prefix string.
Definition qstyle.h:59
const struct AttrColor * attr_color
Curses colour of text.
Definition display.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ append_line()

static void append_line ( struct Line * lines,
int line_num,
int cnt )
static

Add a new Line to the array.

Parameters
linesArray of Line info
line_numLine number to add
cntOffset of the overflow if line is a continuation

Definition at line 259 of file display.c.

260{
261 int m;
262
263 lines[line_num + 1].cid = lines[line_num].cid;
264 (lines[line_num + 1].syntax)[0].attr_color = (lines[line_num].syntax)[0].attr_color;
265 lines[line_num + 1].cont_line = true;
266
267 /* find the real start of the line */
268 for (m = line_num; m >= 0; m--)
269 if (!lines[m].cont_line)
270 break;
271
272 (lines[line_num + 1].syntax)[0].first = m;
273 (lines[line_num + 1].syntax)[0].last = (lines[line_num].cont_line) ?
274 cnt + (lines[line_num].syntax)[0].last :
275 cnt;
276}
bool cont_line
Continuation of a previous line (wrapped by NeoMutt)
Definition display.h:53
short cid
Default line colour, e.g. MT_COLOR_SIGNATURE.
Definition display.h:52
+ Here is the caller graph for this function:

◆ check_marker()

static int check_marker ( const char * q,
const char * p )
static

Check that the unique marker is present.

Parameters
qMarker string
pString to check
Return values
numOffset of marker

Definition at line 284 of file display.c.

285{
286 for (; (p[0] == q[0]) && (q[0] != '\0') && (p[0] != '\0') && (q[0] != '\a') &&
287 (p[0] != '\a');
288 p++, q++)
289 {
290 }
291
292 return (int) (*p - *q);
293}
+ Here is the caller graph for this function:

◆ check_attachment_marker()

static int check_attachment_marker ( const char * p)
static

Check that the unique marker is present.

Parameters
pString to check
Return values
numOffset of marker

Definition at line 300 of file display.c.

301{
303}
static int check_marker(const char *q, const char *p)
Check that the unique marker is present.
Definition display.c:284
const char * state_attachment_marker(void)
Get a unique (per-run) ANSI string to mark PGP messages in an email.
Definition state.c:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_protected_header_marker()

static int check_protected_header_marker ( const char * p)
static

Check that the unique marker is present.

Parameters
pString to check
Return values
numOffset of marker

Definition at line 310 of file display.c.

311{
313}
const char * state_protected_header_marker(void)
Get a unique (per-run) ANSI string to mark protected headers in an email.
Definition state.c:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_is_quote_line()

bool mutt_is_quote_line ( char * line,
regmatch_t * pmatch )

Is a line of message text a quote?

Parameters
[in]lineLine to test
[out]pmatchRegex sub-matches
Return values
trueLine is quoted

Checks if line matches the $quote_regex and doesn't match $smileys. This is used by the pager for calling qstyle_classify.

Definition at line 324 of file display.c.

325{
326 bool is_quote = false;
327 const struct Regex *c_smileys = cs_subset_regex(NeoMutt->sub, "smileys");
328 regmatch_t pmatch_internal[1] = { 0 };
329
330 if (!pmatch)
331 pmatch = pmatch_internal;
332
333 const struct Regex *c_quote_regex = cs_subset_regex(NeoMutt->sub, "quote_regex");
334 if (mutt_regex_capture(c_quote_regex, line, 1, pmatch))
335 {
336 regmatch_t smatch[1] = { 0 };
337 if (mutt_regex_capture(c_smileys, line, 1, smatch))
338 {
339 if (smatch[0].rm_so > 0)
340 {
341 char c = line[smatch[0].rm_so];
342 line[smatch[0].rm_so] = 0;
343
344 if (mutt_regex_capture(c_quote_regex, line, 1, pmatch))
345 is_quote = true;
346
347 line[smatch[0].rm_so] = c;
348 }
349 }
350 else
351 {
352 is_quote = true;
353 }
354 }
355
356 return is_quote;
357}
const struct Regex * cs_subset_regex(const struct ConfigSubset *sub, const char *name)
Get a regex config item by name.
Definition helpers.c:217
bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t nmatch, regmatch_t matches[])
Match a regex against a string, with provided options.
Definition regex.c:597
Cached regular expression.
Definition regex3.h:85
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ match_body_patterns()

static void match_body_patterns ( char * pat,
struct Line * lines,
int line_num )
static

Match body patterns, e.g.

color quoted

Parameters
patPattern to match
linesLines of text in the pager
line_numCurrent line number

Definition at line 365 of file display.c.

366{
367 // don't consider line endings part of the buffer for regex matching
368 bool has_nl = false;
369 size_t buflen = mutt_str_len(pat);
370 if ((buflen > 0) && (pat[buflen - 1] == '\n'))
371 {
372 has_nl = true;
373 pat[buflen - 1] = '\0';
374 }
375
376 int i = 0;
377 int offset = 0;
378 struct RegexColor *color_line = NULL;
379 struct RegexColorList *head = NULL;
380 bool found = false;
381 bool null_rx = false;
382 regmatch_t pmatch[1] = { 0 };
383
384 lines[line_num].syntax_arr_size = 0;
385 if (lines[line_num].cid == MT_COLOR_HDRDEFAULT)
386 {
388 }
389 else
390 {
392 }
393 STAILQ_FOREACH(color_line, head, entries)
394 {
395 color_line->stop_matching = false;
396 }
397
398 do
399 {
400 /* if has_nl, we've stripped off a trailing newline */
401 if (offset >= (buflen - has_nl))
402 break;
403
404 if (!pat[offset])
405 break;
406
407 found = false;
408 null_rx = false;
409 STAILQ_FOREACH(color_line, head, entries)
410 {
411 if (color_line->stop_matching)
412 continue;
413
414 if ((regexec(&color_line->regex, pat + offset, 1, pmatch,
415 ((offset != 0) ? REG_NOTBOL : 0)) != 0))
416 {
417 /* Once a regex fails to match, don't try matching it again.
418 * On very long lines this can cause a performance issue if there
419 * are other regexes that have many matches. */
420 color_line->stop_matching = true;
421 continue;
422 }
423
424 if (pmatch[0].rm_eo == pmatch[0].rm_so)
425 {
426 null_rx = true; // empty regex; don't add it, but keep looking
427 continue;
428 }
429
430 if (!found)
431 {
432 // Abort if we fill up chunks. Yes, this really happened.
433 if (lines[line_num].syntax_arr_size == SHRT_MAX)
434 {
435 null_rx = false;
436 break;
437 }
438 if (++(lines[line_num].syntax_arr_size) > 1)
439 {
440 MUTT_MEM_REALLOC(&(lines[line_num].syntax),
441 lines[line_num].syntax_arr_size, struct TextSyntax);
442 // Zero the new entry
443 const int index = lines[line_num].syntax_arr_size - 1;
444 struct TextSyntax *ts = &lines[line_num].syntax[index];
445 memset(ts, 0, sizeof(*ts));
446 }
447 }
448 i = lines[line_num].syntax_arr_size - 1;
449 pmatch[0].rm_so += offset;
450 pmatch[0].rm_eo += offset;
451
452 if (!found || (pmatch[0].rm_so < (lines[line_num].syntax)[i].first) ||
453 ((pmatch[0].rm_so == (lines[line_num].syntax)[i].first) &&
454 (pmatch[0].rm_eo > (lines[line_num].syntax)[i].last)))
455 {
456 (lines[line_num].syntax)[i].attr_color = &color_line->attr_color;
457 (lines[line_num].syntax)[i].first = pmatch[0].rm_so;
458 (lines[line_num].syntax)[i].last = pmatch[0].rm_eo;
459 }
460 else if ((pmatch[0].rm_so == (lines[line_num].syntax)[i].first) &&
461 (pmatch[0].rm_eo == (lines[line_num].syntax)[i].last))
462 {
463 (lines[line_num].syntax)[i].attr_color = merged_color_overlay(
464 (lines[line_num].syntax)[i].attr_color, &color_line->attr_color);
465 (lines[line_num].syntax)[i].first = pmatch[0].rm_so;
466 (lines[line_num].syntax)[i].last = pmatch[0].rm_eo;
467 }
468
469 found = true;
470 null_rx = false;
471 }
472
473 if (null_rx)
474 offset++; /* avoid degenerate cases */
475 else
476 offset = (lines[line_num].syntax)[i].last;
477 } while (found || null_rx);
478
479 if (has_nl)
480 pat[buflen - 1] = '\n';
481}
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a Colour ID.
Definition regex.c:191
@ MT_COLOR_BODY
Pager: highlight body of message (takes a pattern)
Definition color.h:39
@ MT_COLOR_HDRDEFAULT
Header default colour.
Definition color.h:47
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
short syntax_arr_size
Number of items in syntax array.
Definition display.h:56
A regular expression and a color to highlight a line.
Definition regex4.h:37
regex_t regex
Compiled regex.
Definition regex4.h:40
struct AttrColor attr_color
Colour and attributes to apply.
Definition regex4.h:38
bool stop_matching
Used by the pager for body patterns, to prevent the color from being retried once it fails.
Definition regex4.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ color_is_header()

bool color_is_header ( enum ColorId cid)

Colour is for an Email header.

Parameters
cidColour ID, e.g. MT_COLOR_HEADER
Return values
trueColour is for an Email header

Definition at line 488 of file display.c.

489{
490 return (cid == MT_COLOR_HEADER) || (cid == MT_COLOR_HDRDEFAULT);
491}
+ Here is the caller graph for this function:

◆ resolve_types()

static void resolve_types ( struct MuttWindow * win,
char * buf,
char * raw,
struct Line * lines,
int line_num,
int lines_used,
struct QuoteStyle ** quote_list,
int * q_level,
bool * force_redraw,
bool q_classify )
static

Determine the style for a line of text.

Parameters
[in]winWindow
[in]bufFormatted text
[in]rawRaw text
[in]linesLine info array
[in]line_numLine number (index into lines)
[in]lines_usedLast line
[out]quote_listList of quote colours
[out]q_levelQuote level
[out]force_redrawSet to true if a screen redraw is needed
[in]q_classifyIf true, style the text

Definition at line 506 of file display.c.

510{
511 struct RegexColor *color_line = NULL;
512 regmatch_t pmatch[1] = { 0 };
513 const bool c_header_color_partial = cs_subset_bool(NeoMutt->sub, "header_color_partial");
514 int offset, i = 0;
515
516 if ((line_num == 0) || color_is_header(lines[line_num - 1].cid) ||
518 {
519 if (buf[0] == '\n') /* end of header */
520 {
521 lines[line_num].cid = MT_COLOR_NORMAL;
523 mutt_window_get_coords(win, &mod_data->braille_row, &mod_data->braille_col);
524 }
525 else
526 {
527 /* if this is a continuation of the previous line, use the previous
528 * line's color as default. */
529 if ((line_num > 0) && ((buf[0] == ' ') || (buf[0] == '\t')))
530 {
531 lines[line_num].cid = lines[line_num - 1].cid; /* wrapped line */
532 if (!c_header_color_partial)
533 {
534 (lines[line_num].syntax)[0].attr_color =
535 (lines[line_num - 1].syntax)[0].attr_color;
536 lines[line_num].cont_header = true;
537 }
538 }
539 else
540 {
541 lines[line_num].cid = MT_COLOR_HDRDEFAULT;
542 }
543
544 /* When this option is unset, we color the entire header the
545 * same color. Otherwise, we handle the header patterns just
546 * like body patterns (further below). */
547 if (!c_header_color_partial)
548 {
550 {
551 if (regexec(&color_line->regex, buf, 0, NULL, 0) == 0)
552 {
553 lines[line_num].cid = MT_COLOR_HEADER;
554 lines[line_num].syntax[0].attr_color =
555 merged_color_overlay(lines[line_num].syntax[0].attr_color,
557 lines[line_num].syntax[0].attr_color = merged_color_overlay(
558 lines[line_num].syntax[0].attr_color, &color_line->attr_color);
559 if (lines[line_num].cont_header)
560 {
561 /* adjust the previous continuation lines to reflect the color of this continuation line */
562 int j;
563 for (j = line_num - 1; j >= 0 && lines[j].cont_header; j--)
564 {
565 lines[j].cid = lines[line_num].cid;
566 lines[j].syntax[0].attr_color = lines[line_num].syntax[0].attr_color;
567 }
568 /* now adjust the first line of this header field */
569 if (j >= 0)
570 {
571 lines[j].cid = lines[line_num].cid;
572 lines[j].syntax[0].attr_color = lines[line_num].syntax[0].attr_color;
573 }
574 *force_redraw = true; /* the previous lines have already been drawn on the screen */
575 }
576 }
577 }
578 }
579 }
580 }
581 else if (mutt_str_startswith(raw, "\033[0m")) // Escape: a little hack...
582 {
583 lines[line_num].cid = MT_COLOR_NORMAL;
584 }
585 else if (check_attachment_marker((char *) raw) == 0)
586 {
587 lines[line_num].cid = MT_COLOR_ATTACHMENT;
588 }
589 else if (mutt_str_equal("-- \n", buf) || mutt_str_equal("-- \r\n", buf))
590 {
591 i = line_num + 1;
592
593 lines[line_num].cid = MT_COLOR_SIGNATURE;
594 while ((i < lines_used) && (check_sig(buf, lines, i - 1) == 0) &&
595 ((lines[i].cid == MT_COLOR_NORMAL) || COLOR_QUOTED(lines[i].cid) ||
596 (lines[i].cid == MT_COLOR_HEADER)))
597 {
598 /* oops... */
599 if (lines[i].syntax_arr_size)
600 {
601 lines[i].syntax_arr_size = 0;
602 MUTT_MEM_REALLOC(&(lines[i].syntax), 1, struct TextSyntax);
603 lines[i].syntax[0].attr_color = NULL;
604 lines[i].syntax[0].first = -1;
605 lines[i].syntax[0].last = -1;
606 }
607 lines[i++].cid = MT_COLOR_SIGNATURE;
608 }
609 }
610 else if (check_sig(buf, lines, line_num - 1) == 0)
611 {
612 lines[line_num].cid = MT_COLOR_SIGNATURE;
613 }
614 else if (mutt_is_quote_line(buf, pmatch))
615 {
616 if (q_classify && !lines[line_num].quote)
617 {
618 lines[line_num].quote = qstyle_classify(quote_list, buf + pmatch[0].rm_so,
619 pmatch[0].rm_eo - pmatch[0].rm_so,
620 force_redraw, q_level);
621 }
622 lines[line_num].cid = MT_COLOR_QUOTED0;
623 }
624 else
625 {
626 lines[line_num].cid = MT_COLOR_NORMAL;
627 }
628
629 /* body patterns */
630 if ((lines[line_num].cid == MT_COLOR_NORMAL) || COLOR_QUOTED(lines[line_num].cid) ||
631 ((lines[line_num].cid == MT_COLOR_HDRDEFAULT) && c_header_color_partial))
632 {
633 match_body_patterns(buf, lines, line_num);
634 }
635
636 /* attachment patterns */
637 if (lines[line_num].cid == MT_COLOR_ATTACHMENT)
638 {
639 size_t nl;
640
641 /* don't consider line endings part of the buffer for regex matching */
642 nl = mutt_str_len(buf);
643 if ((nl > 0) && (buf[nl - 1] == '\n'))
644 buf[nl - 1] = '\0';
645
646 i = 0;
647 offset = 0;
648 lines[line_num].syntax_arr_size = 0;
650 bool found = false;
651 bool null_rx = false;
652 do
653 {
654 if (!buf[offset])
655 break;
656
657 found = false;
658 null_rx = false;
660 {
661 if (regexec(&color_line->regex, buf + offset, 1, pmatch,
662 ((offset != 0) ? REG_NOTBOL : 0)) != 0)
663 {
664 continue;
665 }
666
667 if (pmatch[0].rm_eo != pmatch[0].rm_so)
668 {
669 if (!found)
670 {
671 if (++(lines[line_num].syntax_arr_size) > 1)
672 {
673 MUTT_MEM_REALLOC(&(lines[line_num].syntax),
674 lines[line_num].syntax_arr_size, struct TextSyntax);
675 // Zero the new entry
676 const int index = lines[line_num].syntax_arr_size - 1;
677 struct TextSyntax *ts = &lines[line_num].syntax[index];
678 memset(ts, 0, sizeof(*ts));
679 }
680 }
681 i = lines[line_num].syntax_arr_size - 1;
682 pmatch[0].rm_so += offset;
683 pmatch[0].rm_eo += offset;
684 if (!found || (pmatch[0].rm_so < (lines[line_num].syntax)[i].first) ||
685 ((pmatch[0].rm_so == (lines[line_num].syntax)[i].first) &&
686 (pmatch[0].rm_eo > (lines[line_num].syntax)[i].last)))
687 {
688 if (!(lines[line_num].syntax)[i].attr_color)
689 (lines[line_num].syntax)[i].attr_color = ac_attach;
690
691 (lines[line_num].syntax)[i].attr_color = merged_color_overlay(
692 (lines[line_num].syntax)[i].attr_color, &color_line->attr_color);
693 (lines[line_num].syntax)[i].first = pmatch[0].rm_so;
694 (lines[line_num].syntax)[i].last = pmatch[0].rm_eo;
695 }
696 else if ((pmatch[0].rm_so == (lines[line_num].syntax)[i].first) &&
697 (pmatch[0].rm_eo == (lines[line_num].syntax)[i].last))
698 {
699 (lines[line_num].syntax)[i].attr_color = merged_color_overlay(
700 (lines[line_num].syntax)[i].attr_color, &color_line->attr_color);
701 (lines[line_num].syntax)[i].first = pmatch[0].rm_so;
702 (lines[line_num].syntax)[i].last = pmatch[0].rm_eo;
703 }
704 found = 1;
705 null_rx = false;
706 }
707 else
708 {
709 null_rx = true; /* empty regex; don't add it, but keep looking */
710 }
711 }
712
713 if (null_rx)
714 offset++; /* avoid degenerate cases */
715 else
716 offset = (lines[line_num].syntax)[i].last;
717 } while (found || null_rx);
718 if (nl > 0)
719 buf[nl - 1] = '\n';
720 }
721}
@ MT_COLOR_QUOTED0
Pager: quoted text, level 0.
Definition color.h:57
@ MT_COLOR_ATTACH_HEADERS
MIME attachment test (takes a pattern)
Definition color.h:38
@ MT_COLOR_ATTACHMENT
MIME attachments text (entire line)
Definition color.h:37
static int check_protected_header_marker(const char *p)
Check that the unique marker is present.
Definition display.c:310
bool mutt_is_quote_line(char *line, regmatch_t *pmatch)
Is a line of message text a quote?
Definition display.c:324
static int check_sig(const char *s, struct Line *info, int offset)
Check for an email signature.
Definition display.c:58
static void match_body_patterns(char *pat, struct Line *lines, int line_num)
Match body patterns, e.g.
Definition display.c:365
bool color_is_header(enum ColorId cid)
Colour is for an Email header.
Definition display.c:488
static int check_attachment_marker(const char *p)
Check that the unique marker is present.
Definition display.c:300
@ MODULE_ID_PAGER
ModulePager, Pager
Definition module_api.h:83
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
void mutt_window_get_coords(struct MuttWindow *win, int *row, int *col)
Get the cursor position in the Window.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
struct QuoteStyle * qstyle_classify(struct QuoteStyle **quote_list, const char *qptr, size_t length, bool *force_redraw, int *q_level)
Find a style for a string.
Definition qstyle.c:136
bool cont_header
Continuation of a header line (wrapped by MTA)
Definition display.h:54
Pager private Module data.
Definition module_data.h:30
int braille_row
Braille strobe row.
Definition module_data.h:33
int braille_col
Braille strobe column.
Definition module_data.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_strip_formatting()

void buf_strip_formatting ( struct Buffer * dest,
const char * src,
bool strip_markers )

Removes ANSI and backspace formatting.

Parameters
destBuffer for the result
srcString to strip
strip_markersRemove

Removes ANSI and backspace formatting, and optionally markers. This is separated out so that it can be used both by the pager and the autoview handler.

This logic is pulled from the pager fill_buffer() function, for use in stripping reply-quoted autoview output of ansi sequences.

Definition at line 736 of file display.c.

737{
738 const char *s = src;
739
740 buf_reset(dest);
741
742 if (!s)
743 return;
744
745 while (s[0] != '\0')
746 {
747 if ((s[0] == '\010') && (s > src))
748 {
749 if (s[1] == '_') /* underline */
750 {
751 s += 2;
752 }
753 else if (s[1] && buf_len(dest)) /* bold or overstrike */
754 {
755 dest->dptr--;
756 buf_addch(dest, s[1]);
757 s += 2;
758 }
759 else /* ^H */
760 {
761 buf_addch(dest, *s++);
762 }
763 continue;
764 }
765
766 int len = ansi_color_seq_length(s);
767 if (len > 0)
768 {
769 s += len;
770 }
771 else if (strip_markers && (s[0] == '\033') && (s[1] == ']') &&
773 {
774 mutt_debug(LL_DEBUG2, "Seen attachment marker\n");
775 while (*s++ != '\a')
776 ; /* skip pseudo-ANSI sequence */
777 }
778 else
779 {
780 buf_addch(dest, *s++);
781 }
782 }
783}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
int ansi_color_seq_length(const char *str)
Is this an ANSI escape sequence?
Definition parse_ansi.c:78
char * dptr
Current read/write position.
Definition buffer.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fill_buffer()

static int fill_buffer ( FILE * fp,
LOFF_T * bytes_read,
LOFF_T offset,
unsigned char ** buf,
unsigned char ** fmt,
size_t * blen,
int * buf_ready )
static

Fill a buffer from a file.

Parameters
[in]fpFile to read from
[in,out]bytes_readEnd of last read
[in]offsetPosition start reading from
[out]bufBuffer to fill
[out]fmtCopy of buffer, stripped of attributes
[out]blenLength of the buffer
[in,out]buf_readytrue if the buffer already has data in it
Return values
>=0Bytes read
-1Error

Definition at line 797 of file display.c.

799{
800 static int b_read = 0;
801
802 if (*buf_ready == 0)
803 {
804 if (offset != *bytes_read)
805 {
806 if (!mutt_file_seek(fp, offset, SEEK_SET))
807 {
808 return -1;
809 }
810 }
811
812 *buf = (unsigned char *) mutt_file_read_line((char *) *buf, blen, fp, NULL, MUTT_RL_EOL);
813 if (!*buf)
814 {
815 fmt[0] = NULL;
816 return -1;
817 }
818
819 *bytes_read = ftello(fp);
820 LOFF_T diff = *bytes_read - offset;
821 b_read = (diff > INT_MAX) ? INT_MAX : (int) diff;
822 *buf_ready = 1;
823
824 struct Buffer *stripped = buf_pool_get();
825 buf_alloc(stripped, *blen);
826 buf_strip_formatting(stripped, (const char *) *buf, 1);
827 /* This should be a noop, because *fmt should be NULL */
828 FREE(fmt);
829 *fmt = (unsigned char *) buf_strdup(stripped);
830 buf_pool_release(&stripped);
831 }
832
833 return b_read;
834}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition buffer.c:337
void buf_strip_formatting(struct Buffer *dest, const char *src, bool strip_markers)
Removes ANSI and backspace formatting.
Definition display.c:736
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition file.c:678
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:648
@ MUTT_RL_EOL
don't strip \n / \r\n
Definition file.h:45
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
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
String manipulation buffer.
Definition buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ format_line()

static int format_line ( struct MuttWindow * win,
struct Line ** lines,
int line_num,
unsigned char * buf,
PagerFlags flags,
struct AnsiColor * ansi,
int cnt,
int * pspace,
int * pvch,
int * pcol,
int * pspecial,
int width,
struct AttrColorList * ansi_list )
static

Display a line of text in the pager.

Parameters
[in]winWindow
[out]linesLine info
[in]line_numLine number (index into lines)
[in]bufText to display
[in]flagsFlags, see PagerFlags
[out]ansiANSI attributes used
[in]cntLength of text buffer
[out]pspaceIndex of last whitespace character
[out]pvchNumber of bytes read
[out]pcolNumber of columns used
[out]pspecialAttribute flags, e.g. A_UNDERLINE
[in]widthWidth of screen (to wrap to)
[out]ansi_listList of unique Ansi colours
Return values
numNumber of characters displayed

Definition at line 853 of file display.c.

857{
858 int space = -1; /* index of the last space or TAB */
859 const bool c_markers = cs_subset_bool(NeoMutt->sub, "markers");
860 size_t col = c_markers ? (*lines)[line_num].cont_line : 0;
861 size_t k;
862 int ch, vch, last_special = -1, special = 0, t;
863 wchar_t wc = 0;
864 mbstate_t mbstate = { 0 }; // FIXME: this should come from lines
865 const size_t c_wrap = cs_subset_number(NeoMutt->sub, "wrap");
866 size_t wrap_cols = mutt_window_wrap_cols(width, (flags & MUTT_PAGER_NOWRAP) ? 0 : c_wrap);
867
868 if (check_attachment_marker((char *) buf) == 0)
869 wrap_cols = width;
870
871 struct PagerPrivateData *priv = win->parent->wdata;
872 enum PagerMode mode = priv->pview->mode;
873 const bool c_allow_ansi = (mode == PAGER_MODE_OTHER) ||
874 cs_subset_bool(NeoMutt->sub, "allow_ansi");
875
876 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
877 {
878 /* Handle ANSI sequences */
879 if (buf[ch] == '\033') // Escape
880 {
881 int len = ansi_color_parse((const char *) buf + ch, ansi, ansi_list, !c_allow_ansi);
882 ch += len;
883 }
884
885 while ((cnt - ch >= 2) && (buf[ch] == '\033') && (buf[ch + 1] == ']') && // Escape
886 ((check_attachment_marker((char *) buf + ch) == 0) ||
887 (check_protected_header_marker((char *) buf + ch) == 0)))
888 {
889 while (buf[ch++] != '\a')
890 if (ch >= cnt)
891 break;
892 }
893
894 /* is anything left to do? */
895 if (ch >= cnt)
896 break;
897
898 k = mbrtowc(&wc, (char *) buf + ch, cnt - ch, &mbstate);
899 if ((k == ICONV_BUF_TOO_SMALL) || (k == ICONV_ILLEGAL_SEQ))
900 {
901 if (k == ICONV_ILLEGAL_SEQ)
902 memset(&mbstate, 0, sizeof(mbstate));
903 mutt_debug(LL_DEBUG1, "mbrtowc returned %zu; errno = %d\n", k, errno);
904 if ((col + 4) > wrap_cols)
905 break;
906 col += 4;
907 if (ansi)
908 mutt_window_printf(win, "\\%03o", buf[ch]);
909 k = 1;
910 continue;
911 }
912 if (k == 0)
913 k = 1;
914
915 if (CharsetIsUtf8)
916 {
917 /* zero width space, zero with non-joiner, zero width no-break space */
918 if ((wc == 0x200B) || (wc == 0x200C) || (wc == 0xFEFF))
919 {
920 mutt_debug(LL_DEBUG3, "skip zero-width character U+%04X\n", (unsigned short) wc);
921 continue;
922 }
924 {
925 mutt_debug(LL_DEBUG3, "filtered U+%04X\n", (unsigned short) wc);
926 continue;
927 }
928 }
929
930 /* Handle backspace */
931 special = 0;
932 if (IsWPrint(wc))
933 {
934 wchar_t wc1 = 0;
935 mbstate_t mbstate1 = mbstate;
936 size_t k1 = mbrtowc(&wc1, (char *) buf + ch + k, cnt - ch - k, &mbstate1);
937 while ((k1 != ICONV_BUF_TOO_SMALL) && (k1 != ICONV_ILLEGAL_SEQ) &&
938 (k1 > 0) && (wc1 == '\b'))
939 {
940 const size_t k2 = mbrtowc(&wc1, (char *) buf + ch + k + k1,
941 cnt - ch - k - k1, &mbstate1);
942 if ((k2 == ICONV_BUF_TOO_SMALL) || (k2 == ICONV_ILLEGAL_SEQ) ||
943 (k2 == 0) || (!IsWPrint(wc1)))
944 {
945 break;
946 }
947
948 if (wc == wc1)
949 {
950 special |= ((wc == '_') && (special & A_UNDERLINE)) ? A_UNDERLINE : A_BOLD;
951 }
952 else if ((wc == '_') || (wc1 == '_'))
953 {
954 special |= A_UNDERLINE;
955 wc = (wc1 == '_') ? wc : wc1;
956 }
957 else
958 {
959 /* special = 0; / * overstrike: nothing to do! */
960 wc = wc1;
961 }
962
963 ch += k + k1;
964 k = k2;
965 mbstate = mbstate1;
966 k1 = mbrtowc(&wc1, (char *) buf + ch + k, cnt - ch - k, &mbstate1);
967 }
968 }
969
970 if (ansi && ((flags & (MUTT_SHOWCOLOR | MUTT_SEARCH | MUTT_PAGER_MARKER)) ||
971 special || last_special || (ansi->attrs != A_NORMAL)))
972 {
973 resolve_color(win, *lines, line_num, vch, flags, special, ansi);
974 last_special = special;
975 }
976
977 /* no-break space, narrow no-break space */
978 if (IsWPrint(wc) || (CharsetIsUtf8 && ((wc == 0x00A0) || (wc == 0x202F))))
979 {
980 if (wc == ' ')
981 {
982 space = ch;
983 }
984 t = wcwidth(wc);
985 if (col + t > wrap_cols)
986 break;
987 col += t;
988 if (ansi)
989 mutt_addwch(win, wc);
990 }
991 else if (wc == '\n')
992 {
993 break;
994 }
995 else if (wc == '\t')
996 {
997 space = ch;
998 t = (col & ~7) + 8;
999 if (t > wrap_cols)
1000 break;
1001 if (ansi)
1002 for (; col < t; col++)
1003 mutt_window_addch(win, ' ');
1004 else
1005 col = t;
1006 }
1007 else if ((wc < 0x20) || (wc == 0x7f))
1008 {
1009 if ((col + 2) > wrap_cols)
1010 break;
1011 col += 2;
1012 if (ansi)
1013 mutt_window_printf(win, "^%c", (char) (('@' + wc) & 0x7f));
1014 }
1015 else if (wc < 0x100)
1016 {
1017 if ((col + 4) > wrap_cols)
1018 break;
1019 col += 4;
1020 if (ansi)
1021 mutt_window_printf(win, "\\%03lo", (long) wc);
1022 }
1023 else
1024 {
1025 if ((col + 1) > wrap_cols)
1026 break;
1027 col += k;
1028 if (ansi)
1030 }
1031 }
1032 *pspace = space;
1033 *pcol = col;
1034 *pvch = vch;
1035 *pspecial = special;
1036 return ch;
1037}
int ansi_color_parse(const char *str, struct AnsiColor *ansi, struct AttrColorList *acl, bool dry_run)
Parse a string of ANSI escape sequence.
Definition ansi.c:118
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
int mutt_addwch(struct MuttWindow *win, wchar_t wc)
Addwch would be provided by an up-to-date curses library.
Definition curs_lib.c:321
static void resolve_color(struct MuttWindow *win, struct Line *lines, int line_num, int cnt, PagerFlags flags, int special, struct AnsiColor *ansi)
Set the colour for a line of text.
Definition display.c:120
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
bool mutt_mb_is_display_corrupting_utf8(wchar_t wc)
Will this character corrupt the display?
Definition mbyte.c:386
#define IsWPrint(wc)
Definition mbyte.h:40
bool CharsetIsUtf8
Is the user's current character set utf-8?
Definition charset.c:66
wchar_t ReplacementChar
When a Unicode character can't be displayed, use this instead.
Definition charset.c:61
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:116
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:114
int mutt_window_printf(struct MuttWindow *win, const char *fmt,...)
Write a formatted string to a Window.
int mutt_window_wrap_cols(int width, short wrap)
Calculate the wrap column for a given screen width.
#define MUTT_PAGER_NOWRAP
Format for term width, ignore $wrap.
Definition lib.h:74
#define MUTT_PAGER_MARKER
Use markers if option is set.
Definition lib.h:72
PagerMode
Determine the behaviour of the Pager.
Definition lib.h:136
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:143
int attrs
Text attributes, e.g. A_BOLD.
Definition ansi.h:38
void * wdata
Private data.
struct MuttWindow * parent
Parent Window.
Private state data for the Pager.
struct Line * lines
Array of text lines in pager.
struct PagerView * pview
Object to view in the pager.
struct AttrColorList ansi_list
List of ANSI colours used in the Pager.
enum PagerMode mode
Pager mode.
Definition lib.h:175
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ display_line()

int display_line ( FILE * fp,
LOFF_T * bytes_read,
struct Line ** lines,
int line_num,
int * lines_used,
int * lines_max,
PagerFlags flags,
struct QuoteStyle ** quote_list,
int * q_level,
bool * force_redraw,
regex_t * search_re,
struct MuttWindow * win_pager,
struct AttrColorList * ansi_list )

Print a line on screen.

Parameters
[in]fpFile to read from
[out]bytes_readOffset into file
[out]linesLine attributes
[in]line_numLine number
[out]lines_usedLast line
[out]lines_maxMaximum number of lines
[in]flagsFlags, see PagerFlags
[out]quote_listEmail quoting style
[out]q_levelLevel of quoting
[out]force_redrawForce a repaint
[out]search_reRegex to highlight
[in]win_pagerWindow to draw into
[in]ansi_listList of ANSI colours/attributes
Return values
-1EOF was reached
0normal exit, line was not displayed
>0normal exit, line was displayed

Definition at line 1058 of file display.c.

1063{
1064 unsigned char *buf = NULL, *fmt = NULL;
1065 size_t buflen = 0;
1066 unsigned char *buf_ptr = NULL;
1067 int ch, vch, col, cnt, b_read;
1068 int buf_ready = 0;
1069 bool change_last = false;
1070 int special;
1071 int offset;
1072 const struct AttrColor *def_color = NULL;
1073 int m;
1074 int rc = -1;
1075 struct AnsiColor ansi = { { COLOR_DEFAULT, 0, 0 }, { COLOR_DEFAULT, 0, 0 }, 0, NULL };
1076 regmatch_t pmatch[1] = { 0 };
1077
1078 struct PagerPrivateData *priv = win_pager->parent->wdata;
1079 enum PagerMode mode = priv->pview->mode;
1080
1081 if (line_num == *lines_used)
1082 {
1083 (*lines_used)++;
1084 change_last = true;
1085 }
1086
1087 if (*lines_used == *lines_max)
1088 {
1089 *lines_max += LINES;
1091 for (ch = *lines_used; ch < *lines_max; ch++)
1092 {
1093 memset(&((*lines)[ch]), 0, sizeof(struct Line));
1094 (*lines)[ch].cid = -1;
1095 (*lines)[ch].search_arr_size = -1;
1096 (*lines)[ch].syntax = MUTT_MEM_CALLOC(1, struct TextSyntax);
1097 ((*lines)[ch].syntax)[0].first = -1;
1098 ((*lines)[ch].syntax)[0].last = -1;
1099 }
1100 }
1101
1102 struct Line *const cur_line = &(*lines)[line_num];
1103
1104 if (flags & MUTT_PAGER_LOGS)
1105 {
1106 /* determine the line class */
1107 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1108 {
1109 if (change_last)
1110 (*lines_used)--;
1111 goto out;
1112 }
1113
1114 if ((cur_line->cont_line) && (line_num > 0))
1115 {
1116 struct Line *const old_line = &(*lines)[line_num - 1];
1117 cur_line->cid = old_line->cid;
1118 cur_line->syntax[0].attr_color = old_line->syntax[0].attr_color;
1119 }
1120 else
1121 {
1122 cur_line->cid = MT_COLOR_NORMAL;
1123 size_t blen = mutt_str_len((const char *) buf);
1124 if ((blen > 11) && (buf[11] == 'M'))
1126 else if ((blen > 11) && (buf[11] == 'W'))
1128 else if ((blen > 11) && (buf[11] == 'E'))
1130 else
1132 }
1133 }
1134
1135 /* only do color highlighting if we are viewing a message */
1136 if (flags & (MUTT_SHOWCOLOR | MUTT_TYPES))
1137 {
1138 if (cur_line->cid == -1)
1139 {
1140 /* determine the line class */
1141 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1142 {
1143 if (change_last)
1144 (*lines_used)--;
1145 goto out;
1146 }
1147
1148 if (mode == PAGER_MODE_EMAIL)
1149 {
1150 resolve_types(win_pager, (char *) fmt, (char *) buf, *lines, line_num, *lines_used,
1151 quote_list, q_level, force_redraw, flags & MUTT_SHOWCOLOR);
1152 }
1153 else
1154 {
1155 (*lines)[line_num].cid = MT_COLOR_NORMAL;
1156 }
1157
1158 /* avoid race condition for continuation lines when scrolling up */
1159 for (m = line_num + 1;
1160 m < *lines_used && (*lines)[m].offset && (*lines)[m].cont_line; m++)
1161 {
1162 (*lines)[m].cid = cur_line->cid;
1163 }
1164 }
1165
1166 /* this also prevents searching through the hidden lines */
1167 const short c_toggle_quoted_show_levels = cs_subset_number(NeoMutt->sub, "toggle_quoted_show_levels");
1168 if ((flags & MUTT_HIDE) && COLOR_QUOTED(cur_line->cid) &&
1169 (!cur_line->quote || (cur_line->quote->quote_n >= c_toggle_quoted_show_levels)))
1170 {
1171 flags = 0; /* MUTT_NOSHOW */
1172 }
1173 }
1174
1175 /* At this point, (*lines[line_num]).quote may still be undefined. We
1176 * don't want to compute it every time MUTT_TYPES is set, since this
1177 * would slow down the "bottom" function unacceptably. A compromise
1178 * solution is hence to call regexec() again, just to find out the
1179 * length of the quote prefix. */
1180 if ((flags & MUTT_SHOWCOLOR) && !cur_line->cont_line &&
1181 COLOR_QUOTED(cur_line->cid) && !cur_line->quote)
1182 {
1183 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1184 {
1185 if (change_last)
1186 (*lines_used)--;
1187 goto out;
1188 }
1189
1190 const struct Regex *c_quote_regex = cs_subset_regex(NeoMutt->sub, "quote_regex");
1191 if (mutt_regex_capture(c_quote_regex, (char *) fmt, 1, pmatch))
1192 {
1193 cur_line->quote = qstyle_classify(quote_list, (char *) fmt + pmatch[0].rm_so,
1194 pmatch[0].rm_eo - pmatch[0].rm_so,
1195 force_redraw, q_level);
1196 }
1197 else
1198 {
1199 goto out;
1200 }
1201 }
1202
1203 if ((flags & MUTT_SEARCH) && !cur_line->cont_line && (cur_line->search_arr_size == -1))
1204 {
1205 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1206 {
1207 if (change_last)
1208 (*lines_used)--;
1209 goto out;
1210 }
1211
1212 offset = 0;
1213 cur_line->search_arr_size = 0;
1214 while (regexec(search_re, (char *) fmt + offset, 1, pmatch,
1215 (offset ? REG_NOTBOL : 0)) == 0)
1216 {
1217 if (++(cur_line->search_arr_size) > 1)
1218 {
1219 MUTT_MEM_REALLOC(&(cur_line->search), cur_line->search_arr_size, struct TextSyntax);
1220 // Zero the new entry
1221 const int index = cur_line->search_arr_size - 1;
1222 struct TextSyntax *ts = &cur_line->search[index];
1223 memset(ts, 0, sizeof(*ts));
1224 }
1225 else
1226 {
1227 cur_line->search = MUTT_MEM_CALLOC(1, struct TextSyntax);
1228 }
1229 pmatch[0].rm_so += offset;
1230 pmatch[0].rm_eo += offset;
1231 (cur_line->search)[cur_line->search_arr_size - 1].first = pmatch[0].rm_so;
1232 (cur_line->search)[cur_line->search_arr_size - 1].last = pmatch[0].rm_eo;
1233
1234 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1235 offset++; /* avoid degenerate cases */
1236 else
1237 offset = pmatch[0].rm_eo;
1238 if (!fmt[offset])
1239 break;
1240 }
1241 }
1242
1243 if (!(flags & MUTT_SHOW) && ((*lines)[line_num + 1].offset > 0))
1244 {
1245 /* we've already scanned this line, so just exit */
1246 rc = 0;
1247 goto out;
1248 }
1249 if ((flags & MUTT_SHOWCOLOR) && *force_redraw && ((*lines)[line_num + 1].offset > 0))
1250 {
1251 /* no need to try to display this line... */
1252 rc = 1;
1253 goto out; /* fake display */
1254 }
1255
1256 b_read = fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready);
1257 if (b_read < 0)
1258 {
1259 if (change_last)
1260 (*lines_used)--;
1261 goto out;
1262 }
1263
1264 /* now chose a good place to break the line */
1265 cnt = format_line(win_pager, lines, line_num, buf, flags, NULL, b_read, &ch,
1266 &vch, &col, &special, win_pager->state.cols, ansi_list);
1267 buf_ptr = buf + cnt;
1268
1269 /* move the break point only if smart_wrap is set */
1270 const bool c_smart_wrap = cs_subset_bool(NeoMutt->sub, "smart_wrap");
1271 if (c_smart_wrap)
1272 {
1273 if ((cnt < b_read) && (ch != -1) && !color_is_header(cur_line->cid) &&
1274 !mutt_isspace(buf[cnt]))
1275 {
1276 buf_ptr = buf + ch;
1277 /* skip trailing blanks */
1278 while (ch && ((buf[ch] == ' ') || (buf[ch] == '\t') || (buf[ch] == '\r')))
1279 ch--;
1280
1281 /* A folded header with a single long word shouldn't be smartwrapped.
1282 * So just disable smart_wrap if it would wrap at the beginning of the line. */
1283 if (ch == 0)
1284 buf_ptr = buf + cnt;
1285 else
1286 cnt = ch + 1;
1287 }
1288
1289 /* skip leading blanks on the next line too */
1290 while ((*buf_ptr == ' ') || (*buf_ptr == '\t'))
1291 buf_ptr++;
1292 }
1293
1294 if (*buf_ptr == '\r')
1295 buf_ptr++;
1296 if (*buf_ptr == '\n')
1297 buf_ptr++;
1298
1299 if (((int) (buf_ptr - buf) < b_read) && !(*lines)[line_num + 1].cont_line)
1300 append_line(*lines, line_num, (int) (buf_ptr - buf));
1301 (*lines)[line_num + 1].offset = cur_line->offset + (long) (buf_ptr - buf);
1302
1303 /* if we don't need to display the line we are done */
1304 if (!(flags & MUTT_SHOW))
1305 {
1306 rc = 0;
1307 goto out;
1308 }
1309
1310 if (flags & MUTT_PAGER_STRIPES)
1311 {
1312 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1314 }
1315
1316 /* display the line */
1317 format_line(win_pager, lines, line_num, buf, flags, &ansi, cnt, &ch, &vch,
1318 &col, &special, win_pager->state.cols, ansi_list);
1319
1320 /* avoid a bug in ncurses... */
1321 if (col == 0)
1322 {
1323 if (flags & MUTT_PAGER_STRIPES)
1324 {
1325 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1327 }
1328 else
1329 {
1331 }
1332
1333 mutt_window_addch(win_pager, ' ');
1334 }
1335
1336 /* Fill the blank space at the end of the line with the prevailing color.
1337 * ncurses does an implicit clrtoeol() when you do mutt_window_addch('\n') so we have
1338 * to make sure to reset the color *after* that */
1339 if (flags & MUTT_SHOWCOLOR)
1340 {
1341 m = (cur_line->cont_line) ? (cur_line->syntax)[0].first : line_num;
1342 if ((*lines)[m].cid == MT_COLOR_HEADER)
1343 {
1344 def_color = ((*lines)[m].syntax)[0].attr_color;
1345 }
1346 else
1347 {
1348 def_color = simple_color_get((*lines)[m].cid);
1349 }
1350 const struct AttrColor *ac_normal = simple_color_get(MT_COLOR_NORMAL);
1351 const struct AttrColor *ac_eol = NULL;
1352 if (def_color)
1353 ac_eol = merged_color_overlay(ac_normal, def_color);
1354 else
1355 ac_eol = ac_normal;
1356 mutt_curses_set_color(ac_eol);
1357 }
1358
1359 if (col < win_pager->state.cols)
1360 {
1361 if (flags & MUTT_PAGER_STRIPES)
1362 {
1363 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1364 const struct AttrColor *ac_normal = simple_color_get(MT_COLOR_NORMAL);
1365 const struct AttrColor *stripe_color = simple_color_get(cid);
1366 const struct AttrColor *ac_eol = merged_color_overlay(ac_normal, stripe_color);
1367 mutt_curses_set_color(ac_eol);
1368 }
1369 else
1370 {
1371 // Colours may be disabled, but we may still need to end the "search" colour
1372 if (!(flags & MUTT_SHOWCOLOR))
1374 }
1375 mutt_window_clrtoeol(win_pager);
1376 }
1377
1378 /* reset the color back to normal. This *must* come after the
1379 * clrtoeol, otherwise the color for this line will not be
1380 * filled to the right margin. */
1381 if (flags & MUTT_SHOWCOLOR)
1383
1384 /* build a return code */
1385 if (!(flags & MUTT_SHOW))
1386 flags = 0;
1387
1388 rc = flags;
1389
1390out:
1391 FREE(&buf);
1392 FREE(&fmt);
1393 return rc;
1394}
#define COLOR_DEFAULT
Definition color.h:102
ColorId
List of all coloured objects.
Definition color.h:35
@ MT_COLOR_MESSAGE
Informational message.
Definition color.h:52
@ MT_COLOR_ERROR
Error message.
Definition color.h:46
@ MT_COLOR_WARNING
Warning messages.
Definition color.h:84
static int format_line(struct MuttWindow *win, struct Line **lines, int line_num, unsigned char *buf, PagerFlags flags, struct AnsiColor *ansi, int cnt, int *pspace, int *pvch, int *pcol, int *pspecial, int width, struct AttrColorList *ansi_list)
Display a line of text in the pager.
Definition display.c:853
static int fill_buffer(FILE *fp, LOFF_T *bytes_read, LOFF_T offset, unsigned char **buf, unsigned char **fmt, size_t *blen, int *buf_ready)
Fill a buffer from a file.
Definition display.c:797
static void resolve_types(struct MuttWindow *win, char *buf, char *raw, struct Line *lines, int line_num, int lines_used, struct QuoteStyle **quote_list, int *q_level, bool *force_redraw, bool q_classify)
Determine the style for a line of text.
Definition display.c:506
static void append_line(struct Line *lines, int line_num, int cnt)
Add a new Line to the array.
Definition display.c:259
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
const struct AttrColor * mutt_curses_set_normal_backed_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:63
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
#define MUTT_HIDE
Don't show quoted text.
Definition lib.h:66
#define MUTT_TYPES
Compute line's type.
Definition lib.h:68
@ PAGER_MODE_EMAIL
Pager is invoked via 1st path. The mime part is selected automatically.
Definition lib.h:139
#define MUTT_SHOW
Definition lib.h:69
An ANSI escape sequence.
Definition ansi.h:35
A line of text in the pager.
Definition display.h:50
short search_arr_size
Number of items in search array.
Definition display.h:59
struct TextSyntax * search
Array of search text in the line.
Definition display.h:60
LOFF_T offset
Offset into Email file (PagerPrivateData->fp)
Definition display.h:51
struct WindowState state
Current state of the Window.
int lines_used
Size of lines array (used entries)
int lines_max
Capacity of lines array (total entries)
bool first
First time flag for toggle-new.
int quote_n
The quoteN colour index for this level.
Definition qstyle.h:56
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function: