NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
display.h File Reference

Pager Display. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "lib.h"
+ Include dependency graph for display.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TextSyntax
 Highlighting for a piece of text. More...
 
struct  Line
 A line of text in the pager. More...
 

Functions

 ARRAY_HEAD (TextSyntaxArray, struct TextSyntax)
 
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

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.h.

Function Documentation

◆ ARRAY_HEAD()

ARRAY_HEAD ( TextSyntaxArray  ,
struct TextSyntax   
)

◆ 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 1013 of file display.c.

1018{
1019 unsigned char *buf = NULL, *fmt = NULL;
1020 size_t buflen = 0;
1021 unsigned char *buf_ptr = NULL;
1022 int ch, vch, col, cnt, b_read;
1023 int buf_ready = 0;
1024 bool change_last = false;
1025 int special;
1026 int offset;
1027 const struct AttrColor *def_color = NULL;
1028 int m;
1029 int rc = -1;
1030 struct AnsiColor ansi = { NULL, 0, COLOR_DEFAULT, COLOR_DEFAULT };
1031 regmatch_t pmatch[1];
1032
1033 if (line_num == *lines_used)
1034 {
1035 (*lines_used)++;
1036 change_last = true;
1037 }
1038
1039 if (*lines_used == *lines_max)
1040 {
1041 mutt_mem_realloc(lines, sizeof(struct Line) * (*lines_max += LINES));
1042 for (ch = *lines_used; ch < *lines_max; ch++)
1043 {
1044 memset(&((*lines)[ch]), 0, sizeof(struct Line));
1045 (*lines)[ch].cid = -1;
1046 (*lines)[ch].search_arr_size = -1;
1047 (*lines)[ch].syntax = mutt_mem_calloc(1, sizeof(struct TextSyntax));
1048 ((*lines)[ch].syntax)[0].first = -1;
1049 ((*lines)[ch].syntax)[0].last = -1;
1050 }
1051 }
1052
1053 struct Line *const cur_line = &(*lines)[line_num];
1054
1055 if (flags & MUTT_PAGER_LOGS)
1056 {
1057 /* determine the line class */
1058 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1059 {
1060 if (change_last)
1061 (*lines_used)--;
1062 goto out;
1063 }
1064
1065 cur_line->cid = MT_COLOR_MESSAGE_LOG;
1066 if (buf[11] == 'M')
1068 else if (buf[11] == 'W')
1070 else if (buf[11] == 'E')
1072 else
1074 }
1075
1076 /* only do color highlighting if we are viewing a message */
1077 if (flags & (MUTT_SHOWCOLOR | MUTT_TYPES))
1078 {
1079 if (cur_line->cid == -1)
1080 {
1081 /* determine the line class */
1082 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1083 {
1084 if (change_last)
1085 (*lines_used)--;
1086 goto out;
1087 }
1088
1089 resolve_types(win_pager, (char *) fmt, (char *) buf, *lines, line_num, *lines_used,
1090 quote_list, q_level, force_redraw, flags & MUTT_SHOWCOLOR);
1091
1092 /* avoid race condition for continuation lines when scrolling up */
1093 for (m = line_num + 1;
1094 m < *lines_used && (*lines)[m].offset && (*lines)[m].cont_line; m++)
1095 {
1096 (*lines)[m].cid = cur_line->cid;
1097 }
1098 }
1099
1100 /* this also prevents searching through the hidden lines */
1101 const short c_toggle_quoted_show_levels = cs_subset_number(NeoMutt->sub, "toggle_quoted_show_levels");
1102 if ((flags & MUTT_HIDE) && (cur_line->cid == MT_COLOR_QUOTED) &&
1103 (!cur_line->quote || (cur_line->quote->quote_n >= c_toggle_quoted_show_levels)))
1104 {
1105 flags = 0; /* MUTT_NOSHOW */
1106 }
1107 }
1108
1109 /* At this point, (*lines[line_num]).quote may still be undefined. We
1110 * don't want to compute it every time MUTT_TYPES is set, since this
1111 * would slow down the "bottom" function unacceptably. A compromise
1112 * solution is hence to call regexec() again, just to find out the
1113 * length of the quote prefix. */
1114 if ((flags & MUTT_SHOWCOLOR) && !cur_line->cont_line &&
1115 (cur_line->cid == MT_COLOR_QUOTED) && !cur_line->quote)
1116 {
1117 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1118 {
1119 if (change_last)
1120 (*lines_used)--;
1121 goto out;
1122 }
1123
1124 const struct Regex *c_quote_regex = cs_subset_regex(NeoMutt->sub, "quote_regex");
1125 if (mutt_regex_capture(c_quote_regex, (char *) fmt, 1, pmatch))
1126 {
1127 cur_line->quote = qstyle_classify(quote_list, (char *) fmt + pmatch[0].rm_so,
1128 pmatch[0].rm_eo - pmatch[0].rm_so,
1129 force_redraw, q_level);
1130 }
1131 else
1132 {
1133 goto out;
1134 }
1135 }
1136
1137 if ((flags & MUTT_SEARCH) && !cur_line->cont_line && (cur_line->search_arr_size == -1))
1138 {
1139 if (fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready) < 0)
1140 {
1141 if (change_last)
1142 (*lines_used)--;
1143 goto out;
1144 }
1145
1146 offset = 0;
1147 cur_line->search_arr_size = 0;
1148 while (regexec(search_re, (char *) fmt + offset, 1, pmatch,
1149 (offset ? REG_NOTBOL : 0)) == 0)
1150 {
1151 if (++(cur_line->search_arr_size) > 1)
1152 {
1153 mutt_mem_realloc(&(cur_line->search),
1154 (cur_line->search_arr_size) * sizeof(struct TextSyntax));
1155 // Zero the new entry
1156 const int index = cur_line->search_arr_size - 1;
1157 struct TextSyntax *ts = &cur_line->search[index];
1158 memset(ts, 0, sizeof(*ts));
1159 }
1160 else
1161 {
1162 cur_line->search = mutt_mem_calloc(1, sizeof(struct TextSyntax));
1163 }
1164 pmatch[0].rm_so += offset;
1165 pmatch[0].rm_eo += offset;
1166 (cur_line->search)[cur_line->search_arr_size - 1].first = pmatch[0].rm_so;
1167 (cur_line->search)[cur_line->search_arr_size - 1].last = pmatch[0].rm_eo;
1168
1169 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1170 offset++; /* avoid degenerate cases */
1171 else
1172 offset = pmatch[0].rm_eo;
1173 if (!fmt[offset])
1174 break;
1175 }
1176 }
1177
1178 if (!(flags & MUTT_SHOW) && ((*lines)[line_num + 1].offset > 0))
1179 {
1180 /* we've already scanned this line, so just exit */
1181 rc = 0;
1182 goto out;
1183 }
1184 if ((flags & MUTT_SHOWCOLOR) && *force_redraw && ((*lines)[line_num + 1].offset > 0))
1185 {
1186 /* no need to try to display this line... */
1187 rc = 1;
1188 goto out; /* fake display */
1189 }
1190
1191 b_read = fill_buffer(fp, bytes_read, cur_line->offset, &buf, &fmt, &buflen, &buf_ready);
1192 if (b_read < 0)
1193 {
1194 if (change_last)
1195 (*lines_used)--;
1196 goto out;
1197 }
1198
1199 /* now chose a good place to break the line */
1200 cnt = format_line(win_pager, lines, line_num, buf, flags, NULL, b_read, &ch,
1201 &vch, &col, &special, win_pager->state.cols, ansi_list);
1202 buf_ptr = buf + cnt;
1203
1204 /* move the break point only if smart_wrap is set */
1205 const bool c_smart_wrap = cs_subset_bool(NeoMutt->sub, "smart_wrap");
1206 if (c_smart_wrap)
1207 {
1208 if ((cnt < b_read) && (ch != -1) &&
1209 !simple_color_is_header(cur_line->cid) && !isspace(buf[cnt]))
1210 {
1211 buf_ptr = buf + ch;
1212 /* skip trailing blanks */
1213 while (ch && ((buf[ch] == ' ') || (buf[ch] == '\t') || (buf[ch] == '\r')))
1214 ch--;
1215 /* A very long word with leading spaces causes infinite
1216 * wrapping when MUTT_PAGER_NSKIP is set. A folded header
1217 * with a single long word shouldn't be smartwrapped
1218 * either. So just disable smart_wrap if it would wrap at the
1219 * beginning of the line. */
1220 if (ch == 0)
1221 buf_ptr = buf + cnt;
1222 else
1223 cnt = ch + 1;
1224 }
1225 if (!(flags & MUTT_PAGER_NSKIP))
1226 {
1227 /* skip leading blanks on the next line too */
1228 while ((*buf_ptr == ' ') || (*buf_ptr == '\t'))
1229 buf_ptr++;
1230 }
1231 }
1232
1233 if (*buf_ptr == '\r')
1234 buf_ptr++;
1235 if (*buf_ptr == '\n')
1236 buf_ptr++;
1237
1238 if (((int) (buf_ptr - buf) < b_read) && !(*lines)[line_num + 1].cont_line)
1239 append_line(*lines, line_num, (int) (buf_ptr - buf));
1240 (*lines)[line_num + 1].offset = cur_line->offset + (long) (buf_ptr - buf);
1241
1242 /* if we don't need to display the line we are done */
1243 if (!(flags & MUTT_SHOW))
1244 {
1245 rc = 0;
1246 goto out;
1247 }
1248
1249 if (flags & MUTT_PAGER_STRIPES)
1250 {
1251 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1253 }
1254
1255 /* display the line */
1256 format_line(win_pager, lines, line_num, buf, flags, &ansi, cnt, &ch, &vch,
1257 &col, &special, win_pager->state.cols, ansi_list);
1258
1259 /* avoid a bug in ncurses... */
1260 if (col == 0)
1261 {
1262 if (flags & MUTT_PAGER_STRIPES)
1263 {
1264 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1266 }
1267 else
1268 {
1270 }
1271
1272 mutt_window_addch(win_pager, ' ');
1273 }
1274
1275 /* Fill the blank space at the end of the line with the prevailing color.
1276 * ncurses does an implicit clrtoeol() when you do mutt_window_addch('\n') so we have
1277 * to make sure to reset the color *after* that */
1278 if (flags & MUTT_SHOWCOLOR)
1279 {
1280 m = (cur_line->cont_line) ? (cur_line->syntax)[0].first : line_num;
1281 if ((*lines)[m].cid == MT_COLOR_HEADER)
1282 {
1283 def_color = ((*lines)[m].syntax)[0].attr_color;
1284 }
1285 else
1286 {
1287 def_color = simple_color_get((*lines)[m].cid);
1288 }
1289 const struct AttrColor *ac_normal = simple_color_get(MT_COLOR_NORMAL);
1290 const struct AttrColor *ac_eol = NULL;
1291 if (def_color)
1292 ac_eol = merged_color_overlay(ac_normal, def_color);
1293 else
1294 ac_eol = ac_normal;
1295 mutt_curses_set_color(ac_eol);
1296 }
1297
1298 if (col < win_pager->state.cols)
1299 {
1300 if (flags & MUTT_PAGER_STRIPES)
1301 {
1302 const enum ColorId cid = ((line_num % 2) == 0) ? MT_COLOR_STRIPE_ODD : MT_COLOR_STRIPE_EVEN;
1304 }
1305 else
1306 {
1308 }
1309 mutt_window_clrtoeol(win_pager);
1310 }
1311
1312 /* reset the color back to normal. This *must* come after the
1313 * clrtoeol, otherwise the color for this line will not be
1314 * filled to the right margin. */
1315 if (flags & MUTT_SHOWCOLOR)
1317
1318 /* build a return code */
1319 if (!(flags & MUTT_SHOW))
1320 flags = 0;
1321
1322 rc = flags;
1323
1324out:
1325 FREE(&buf);
1326 FREE(&fmt);
1327 return rc;
1328}
bool simple_color_is_header(enum ColorId cid)
Colour is for an Email header.
Definition: simple.c:112
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition: simple.c:81
#define COLOR_DEFAULT
Definition: color.h:104
ColorId
List of all colored objects.
Definition: color.h:38
@ MT_COLOR_MESSAGE
Informational message.
Definition: color.h:55
@ MT_COLOR_QUOTED
Pager: quoted text.
Definition: color.h:61
@ MT_COLOR_HEADER
Message headers (takes a pattern)
Definition: color.h:51
@ MT_COLOR_STRIPE_EVEN
Stripes: even lines of the Help Page.
Definition: color.h:76
@ MT_COLOR_ERROR
Error message.
Definition: color.h:49
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
@ MT_COLOR_MESSAGE_LOG
Menu showing log messages.
Definition: color.h:56
@ MT_COLOR_STRIPE_ODD
Stripes: odd lines of the Help Page.
Definition: color.h:77
@ MT_COLOR_WARNING
Warning messages.
Definition: color.h:81
const struct Regex * cs_subset_regex(const struct ConfigSubset *sub, const char *name)
Get a regex config item by name.
Definition: helpers.c:218
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
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:812
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:757
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:368
static void append_line(struct Line *lines, int line_num, int cnt)
Add a new Line to the array.
Definition: display.c:255
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:45
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition: merged.c:109
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:619
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:81
void mutt_curses_set_color(const struct AttrColor *ac)
Set the colour and attributes for text.
Definition: mutt_curses.c:40
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:244
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
Definition: mutt_window.c:388
#define MUTT_PAGER_NSKIP
Preserve whitespace with smartwrap.
Definition: lib.h:68
#define MUTT_HIDE
Don't show quoted text.
Definition: lib.h:62
#define MUTT_TYPES
Compute line's type.
Definition: lib.h:64
#define MUTT_PAGER_STRIPES
Striped highlighting.
Definition: lib.h:75
#define MUTT_SHOWCOLOR
Show characters in color otherwise don't show characters.
Definition: lib.h:61
#define MUTT_PAGER_LOGS
Logview mode.
Definition: lib.h:73
#define MUTT_SEARCH
Resolve search patterns.
Definition: lib.h:63
#define MUTT_SHOW
Definition: lib.h:65
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: quoted.c:291
An ANSI escape sequence.
Definition: ansi.h:34
A curses colour and its attributes.
Definition: attr.h:35
A line of text in the pager.
Definition: display.h:51
short search_arr_size
Number of items in search array.
Definition: display.h:60
struct TextSyntax * search
Array of search text in the line.
Definition: display.h:61
bool cont_line
Continuation of a previous line (wrapped by NeoMutt)
Definition: display.h:54
short cid
Default line colour, e.g. MT_COLOR_QUOTED.
Definition: display.h:53
struct QuoteStyle * quote
Quoting style for this line (pointer into PagerPrivateData->quote_list)
Definition: display.h:63
LOFF_T offset
Offset into Email file (PagerPrivateData->fp)
Definition: display.h:52
struct TextSyntax * syntax
Array of coloured text in the line.
Definition: display.h:58
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
int quote_n
The quoteN colour index for this level.
Definition: quoted.h:69
Cached regular expression.
Definition: regex3.h:89
Highlighting for a piece of text.
Definition: display.h:40
const struct AttrColor * attr_color
Curses colour of text.
Definition: display.h:41
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: